From nobody Thu Sep 19 23:08:30 2024 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E34815886A for ; Fri, 30 Aug 2024 07:04:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725001487; cv=none; b=N8q6knwanzoXmy3mVO/rUmpMcdfkK0K/0dag4T4dpvyzhhIdtGDtTz6SnS0axNCR+2oHq9OicT59/tRYIsJCAXTYB16k5vOSaLasNzR/8Op0j83fSwjgpZzu1XPeFO8FI3nfc2sxd8XU/RUFBmnf8hK4QH9Ve3eRm3e9RBxyCa8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725001487; c=relaxed/simple; bh=kI/UXHauiF8++61m2LvIvOun+fgX4sFxPktQctCIRkA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hWexib4BVoDX2fMuLkUT4hulh5UjYax7P7YXCtwKz9cGnbVYdAWRejVsObF1hef3AckB/ShSbnfJBN83QugPw65rWK2xVMuYsea31EuqyIQCdYoBGTHNpYjQ/oCrCxTqyRbuckvRnBeanJAXfqgQShHqwybgHFYxM1mvQKBQytk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=MB8koRi2; arc=none smtp.client-ip=209.85.218.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="MB8koRi2" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a7a9cf7d3f3so165976166b.1 for ; Fri, 30 Aug 2024 00:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1725001483; x=1725606283; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tldNdGEbq2JT8UwGW+1a5h0B/fw6VCiuTMNVKqNaJKU=; b=MB8koRi26jgg1r2oCnPajH5326DFHXf6ekjG33sJyJbokGUD/dlWT/LUAJE3blR7aW BKSeoGOlD+Ybt2sqZn7Gl9SMPhLf18sxiQh97kTbFbcWRNdzMNcGSfmQOFRcWZrMcDLP QhXn6WohxlmNILoGuRbMdQ5zWevR4sDqOK2ah4c1iadOkkFZ3ds6v1QhwdTb1KyPUxvU Rwr8hbIiofzcCIiKOqNXlzDHQo26J8mRvH/1tQeLIPJIoII4GGk3CRuYPxsgwuw56VtV U05P/YdTapl4vMftTpVO5zogAVeixol16FZ/YPNoHIoRflyIBcWclxlKprcEsjvztaC1 o8DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725001483; x=1725606283; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tldNdGEbq2JT8UwGW+1a5h0B/fw6VCiuTMNVKqNaJKU=; b=n3BYNgfakBajC24kKVw9g/kf7cR0EZILDL7zC4RtFRf2JKE8OiS6h8/uxkk6RJqC40 zlat1KAY8XKsSJF7wF3tkS+mF9ob0FIDdTCVV7LA3bZ1QFYzO79+5p4MAIzKo9b7rUci X6V62WocC/0eSriVZE70nNjPLIU3Ni2vJbMOln4eCJSgGqxwIn7CHrLPRi9HH4AgpZtt nFXA+iVcj2EDiBLWCXSHXVoxZBtXXpknYxCP96LZ2vqURV3jU8zDzjnLS5A+hn4BwbKi QqO74GZST0rO9m7vFqEQEFUXL/wDLmInnBwHPIAVm7tXbmXUnoaJNSfc3CpMlPKh+zNr gR9A== X-Gm-Message-State: AOJu0YzVO6X2XpchPnPTNZ8nRhCo0lFdbL7wUMhbO/aflxoquiBVkSkm Q0wbrDrS/4LMUQIo1WoLBri1sFRMyaLi8GR9wybyBOyz/7BjX8iAu2b0ayHn/OuC2oN3eCam+LL FUy+O9w== X-Google-Smtp-Source: AGHT+IEjl8tdNQ8jnRsKsySyLyIRKzAgwSRLlK6Mh9bwxDbOAC7WmuH6DFlPs4IQfyp+DQUGhLPnww== X-Received: by 2002:a17:907:2d25:b0:a86:80a8:cee7 with SMTP id a640c23a62f3a-a897f836ce9mr393312366b.27.1725001482962; Fri, 30 Aug 2024 00:04:42 -0700 (PDT) Received: from rayden.urgonet (h-217-31-164-171.A175.priv.bahnhof.se. [217.31.164.171]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a89892232c7sm178026866b.222.2024.08.30.00.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:04:42 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jens Wiklander Subject: [RFC PATCH 4/4] dma-buf: heaps: add Linaro restricted dmabuf heap support Date: Fri, 30 Aug 2024 09:03:51 +0200 Message-Id: <20240830070351.2855919-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240830070351.2855919-1-jens.wiklander@linaro.org> References: <20240830070351.2855919-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a Linaro restricted heap using the linaro,restricted-heap bindings implemented based on the generic restricted heap. The bindings defines a range of physical restricted memory. The heap manages this address range using genalloc. The allocated dma-buf file descriptor can later be registered with the TEE subsystem for later use via Trusted Applications in the secure world. Co-developed-by: Olivier Masse Signed-off-by: Olivier Masse Signed-off-by: Jens Wiklander --- drivers/dma-buf/heaps/Kconfig | 10 ++ drivers/dma-buf/heaps/Makefile | 1 + .../dma-buf/heaps/restricted_heap_linaro.c | 165 ++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 drivers/dma-buf/heaps/restricted_heap_linaro.c diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index 58903bc62ac8..82e2c5d09242 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -28,3 +28,13 @@ config DMABUF_HEAPS_RESTRICTED_MTK help Enable restricted dma-buf heaps for MediaTek platform. This heap is bac= ked by TEE client interfaces. If in doubt, say N. + +config DMABUF_HEAPS_RESTRICTED_LINARO + bool "Linaro DMA-BUF Restricted Heap" + depends on DMABUF_HEAPS_RESTRICTED + help + Choose this option to enable the Linaro restricted dma-buf heap. + The restricted heap pools are defined according to the DT. Heaps + are allocated in the pools using gen allocater. + If in doubt, say N. + diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile index 0028aa9d875f..66b2f67c47b5 100644 --- a/drivers/dma-buf/heaps/Makefile +++ b/drivers/dma-buf/heaps/Makefile @@ -2,4 +2,5 @@ obj-$(CONFIG_DMABUF_HEAPS_CMA) +=3D cma_heap.o obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED) +=3D restricted_heap.o obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED_MTK) +=3D restricted_heap_mtk.o +obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED_LINARO) +=3D restricted_heap_linaro.o obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) +=3D system_heap.o diff --git a/drivers/dma-buf/heaps/restricted_heap_linaro.c b/drivers/dma-b= uf/heaps/restricted_heap_linaro.c new file mode 100644 index 000000000000..4b08ed514023 --- /dev/null +++ b/drivers/dma-buf/heaps/restricted_heap_linaro.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DMABUF secure heap exporter + * + * Copyright 2021 NXP. + * Copyright 2024 Linaro Limited. + */ + +#define pr_fmt(fmt) "rheap_linaro: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "restricted_heap.h" + +#define MAX_HEAP_COUNT 2 +#define HEAP_NAME_LEN 32 + +struct resmem_restricted { + phys_addr_t base; + phys_addr_t size; + + char name[HEAP_NAME_LEN]; + + bool no_map; +}; + +static struct resmem_restricted restricted_data[MAX_HEAP_COUNT] =3D {0}; +static unsigned int restricted_data_count; + +static int linaro_restricted_memory_allocate(struct restricted_heap *heap, + struct restricted_buffer *buf) +{ + struct gen_pool *pool =3D heap->priv_data; + unsigned long pa; + int ret; + + buf->size =3D ALIGN(buf->size, PAGE_SIZE); + pa =3D gen_pool_alloc(pool, buf->size); + if (!pa) + return -ENOMEM; + + ret =3D sg_alloc_table(&buf->sg_table, 1, GFP_KERNEL); + if (ret) { + gen_pool_free(pool, pa, buf->size); + return ret; + } + + sg_set_page(buf->sg_table.sgl, phys_to_page(pa), buf->size, 0); + + return 0; +} + +static void linaro_restricted_memory_free(struct restricted_heap *heap, + struct restricted_buffer *buf) +{ + struct gen_pool *pool =3D heap->priv_data; + struct scatterlist *sg; + unsigned int i; + + for_each_sg(buf->sg_table.sgl, sg, buf->sg_table.nents, i) + gen_pool_free(pool, page_to_phys(sg_page(sg)), sg->length); + sg_free_table(&buf->sg_table); +} + +static const struct restricted_heap_ops linaro_restricted_heap_ops =3D { + .alloc =3D linaro_restricted_memory_allocate, + .free =3D linaro_restricted_memory_free, +}; + +static int add_heap(struct resmem_restricted *mem) +{ + struct restricted_heap *heap; + struct gen_pool *pool; + int ret; + + if (mem->base =3D=3D 0 || mem->size =3D=3D 0) { + pr_err("restricted_data base or size is not correct\n"); + return -EINVAL; + } + + heap =3D kzalloc(sizeof(*heap), GFP_KERNEL); + if (!heap) + return -ENOMEM; + + pool =3D gen_pool_create(PAGE_SHIFT, -1); + if (!pool) { + ret =3D -ENOMEM; + goto err_free_heap; + } + + ret =3D gen_pool_add(pool, mem->base, mem->size, -1); + if (ret) + goto err_free_pool; + + heap->no_map =3D mem->no_map; + heap->priv_data =3D pool; + heap->name =3D mem->name; + heap->ops =3D &linaro_restricted_heap_ops; + + ret =3D restricted_heap_add(heap); + if (ret) + goto err_free_pool; + + return 0; + +err_free_pool: + gen_pool_destroy(pool); +err_free_heap: + kfree(heap); + + return ret; +} + +static int __init rmem_restricted_heap_setup(struct reserved_mem *rmem) +{ + size_t len =3D HEAP_NAME_LEN; + const char *s; + bool no_map; + + if (WARN_ONCE(restricted_data_count >=3D MAX_HEAP_COUNT, + "Cannot handle more than %u restricted heaps\n", + MAX_HEAP_COUNT)) + return -EINVAL; + + no_map =3D of_get_flat_dt_prop(rmem->fdt_node, "no-map", NULL); + s =3D strchr(rmem->name, '@'); + if (s) + len =3D umin(s - rmem->name + 1, len); + + restricted_data[restricted_data_count].base =3D rmem->base; + restricted_data[restricted_data_count].size =3D rmem->size; + restricted_data[restricted_data_count].no_map =3D no_map; + strscpy(restricted_data[restricted_data_count].name, rmem->name, len); + + restricted_data_count++; + return 0; +} + +RESERVEDMEM_OF_DECLARE(linaro_restricted_heap, "linaro,restricted-heap", + rmem_restricted_heap_setup); + +static int linaro_restricted_heap_init(void) +{ + unsigned int i; + int ret; + + for (i =3D 0; i < restricted_data_count; i++) { + ret =3D add_heap(&restricted_data[i]); + if (ret) + return ret; + } + return 0; +} + +module_init(linaro_restricted_heap_init); +MODULE_DESCRIPTION("Linaro Restricted Heap Driver"); +MODULE_LICENSE("GPL"); --=20 2.34.1