From patchwork Mon Sep 11 02:30:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WW9uZyBXdSAo5ZC05YuHKQ==?= X-Patchwork-Id: 721633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F313EEB580 for ; Mon, 11 Sep 2023 02:31:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232768AbjIKCbR (ORCPT ); Sun, 10 Sep 2023 22:31:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbjIKCbQ (ORCPT ); Sun, 10 Sep 2023 22:31:16 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E243A2; Sun, 10 Sep 2023 19:31:03 -0700 (PDT) X-UUID: 3cfb1752504b11eea33bb35ae8d461a2-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=tqaOGRS+JQDIuzzt0M0qRpuYeYNUeGNZRFNvhFxjdJw=; b=nUXzSvGdq6T/Rj9WO7iIU/0Qc95BVuwXr17RkGtAc5bdVZez7s36q5alIau7iGM5+23IOO+S7Rw+el2D6tEQtc+93ime0FUOpBwd/WmEBjDT2Zu7hej0KoyUW8oCwPFfBT5t5kOhItRUIIYicEfcg1lVqJ5JW1kKfL1XYQQtOH0=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31, REQID:85983e19-0f4e-4e71-aa79-51f3b5b468c4, IP:0, U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:0ad78a4, CLOUDID:a3b089ef-9a6e-4c39-b73e-f2bc08ca3dc5, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 3cfb1752504b11eea33bb35ae8d461a2-20230911 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 470886193; Mon, 11 Sep 2023 10:30:57 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs13n1.mediatek.inc (172.21.101.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 10:30:55 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 10:30:54 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , Subject: [PATCH 1/9] dma-buf: heaps: Deduplicate docs and adopt common format Date: Mon, 11 Sep 2023 10:30:30 +0800 Message-ID: <20230911023038.30649-2-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911023038.30649-1-yong.wu@mediatek.com> References: <20230911023038.30649-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: "T.J. Mercier" The docs for dma_heap_get_name were incorrect, and since they were duplicated in the implementation file they were wrong there too. The docs formatting was inconsistent so I tried to make it more consistent across functions since I'm already in here doing cleanup. Remove multiple unused includes. Signed-off-by: T.J. Mercier Signed-off-by: Yong Wu [Yong: Just add a comment for "priv" to mute build warning] --- drivers/dma-buf/dma-heap.c | 29 +++++++---------------------- include/linux/dma-heap.h | 11 +++++------ 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 84ae708fafe7..51030f6c9d6e 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -7,17 +7,15 @@ */ #include -#include #include #include +#include #include -#include #include -#include #include -#include #include -#include +#include +#include #include #define DEVNAME "dma_heap" @@ -28,9 +26,10 @@ * struct dma_heap - represents a dmabuf heap in the system * @name: used for debugging/device-node name * @ops: ops struct for this heap - * @heap_devt heap device node - * @list list head connecting to list of heaps - * @heap_cdev heap char device + * @priv: private data for this heap + * @heap_devt: heap device node + * @list: list head connecting to list of heaps + * @heap_cdev: heap char device * * Represents a heap of memory from which buffers can be made. */ @@ -192,25 +191,11 @@ static const struct file_operations dma_heap_fops = { #endif }; -/** - * dma_heap_get_drvdata() - get per-subdriver data for the heap - * @heap: DMA-Heap to retrieve private data for - * - * Returns: - * The per-subdriver data for the heap. - */ void *dma_heap_get_drvdata(struct dma_heap *heap) { return heap->priv; } -/** - * dma_heap_get_name() - get heap name - * @heap: DMA-Heap to retrieve private data for - * - * Returns: - * The char* for the heap name. - */ const char *dma_heap_get_name(struct dma_heap *heap) { return heap->name; diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 0c05561cad6e..c7c29b724ad6 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -9,14 +9,13 @@ #ifndef _DMA_HEAPS_H #define _DMA_HEAPS_H -#include #include struct dma_heap; /** * struct dma_heap_ops - ops to operate on a given heap - * @allocate: allocate dmabuf and return struct dma_buf ptr + * @allocate: allocate dmabuf and return struct dma_buf ptr * * allocate returns dmabuf on success, ERR_PTR(-errno) on error. */ @@ -42,7 +41,7 @@ struct dma_heap_export_info { }; /** - * dma_heap_get_drvdata() - get per-heap driver data + * dma_heap_get_drvdata - get per-heap driver data * @heap: DMA-Heap to retrieve private data for * * Returns: @@ -51,8 +50,8 @@ struct dma_heap_export_info { void *dma_heap_get_drvdata(struct dma_heap *heap); /** - * dma_heap_get_name() - get heap name - * @heap: DMA-Heap to retrieve private data for + * dma_heap_get_name - get heap name + * @heap: DMA-Heap to retrieve the name of * * Returns: * The char* for the heap name. @@ -61,7 +60,7 @@ const char *dma_heap_get_name(struct dma_heap *heap); /** * dma_heap_add - adds a heap to dmabuf heaps - * @exp_info: information needed to register this heap + * @exp_info: information needed to register this heap */ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); From patchwork Mon Sep 11 02:30:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WW9uZyBXdSAo5ZC05YuHKQ==?= X-Patchwork-Id: 721632 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86627EE49A4 for ; Mon, 11 Sep 2023 02:31:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232941AbjIKCbj (ORCPT ); Sun, 10 Sep 2023 22:31:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233115AbjIKCbd (ORCPT ); Sun, 10 Sep 2023 22:31:33 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1529211B; Sun, 10 Sep 2023 19:31:22 -0700 (PDT) X-UUID: 47ac74e8504b11ee8051498923ad61e6-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=CUJ9ELlafUDabP9pJTFwquJ8GeCYxQW7Xz1vuQdcyBM=; b=JJruc5tsMWh8hFGlyl3tDD4r5uCARVVB9mQ67GdDTj5IA5UFGCaiqXz1N7TcKnRG8zvJbqHuYg3N9VDULiGze/Iqmdz+XDfjQzDt4XmWzahsvxBiYOcOTTVXF5IrhdXAcsUD/AibsvIvPJYereaoJ8kD0/DwYH5NZvXhAGAVRgA=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31, REQID:472d4f0e-470a-42a4-8e9d-a5bf8ac750ab, IP:0, U RL:0,TC:0,Content:-25,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-50 X-CID-META: VersionHash:0ad78a4, CLOUDID:1283c713-4929-4845-9571-38c601e9c3c9, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:1,IP:nil,UR L:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,D KR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 47ac74e8504b11ee8051498923ad61e6-20230911 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1632540821; Mon, 11 Sep 2023 10:31:15 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by MTKMBS14N2.mediatek.inc (172.21.101.76) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 10:31:13 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 10:31:12 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , Subject: [PATCH 3/9] dma-heap: Provide accessors so that in-kernel drivers can allocate dmabufs from specific heaps Date: Mon, 11 Sep 2023 10:30:32 +0800 Message-ID: <20230911023038.30649-4-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911023038.30649-1-yong.wu@mediatek.com> References: <20230911023038.30649-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--7.359800-8.000000 X-TMASE-MatchedRID: YiCTc/DoVWftt0HEL3BUV4lD2T5imTkJ2qBSQHAh8pg0QmmUihPzrFJS 0b8z/9TB8AyWk2NFMNbijpjet3oGSJCoy9iDotiwNNHZMWDTEbe4UO5+xwKkcStjI02a+7m1q3c ttlDobAxG26EiMHWORL4tP830vR4Ai8ICQO6ibxThG1IOMb7PsOWNJG9IamrcfmHrLgoJIlxnXT ApdRZectbsbJMjcyybVDhg+HFzjttT4Q98GKrcb7E3FpMbg63SKx5ICGp/WtFLgo8+IIHbcNDvp yUuLSMV+F1V82TD38h/OSL2yLHcRZH0YXYnbGozFEUknJ/kEl7dB/CxWTRRu4Gh9SYKzkjuUpXq gxV1N6mhT243gu1N3ZrBizs+kLqF+9kKfC9LZkrreFgbzoDRQm39xATaiFQjftwZ3X11IV0= X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--7.359800-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: E407940FB271E0BC0AEB9E656D12D42CD9BD2BF3E21ED0673D5E5E0724D604882000:8 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: John Stultz This allows drivers who don't want to create their own DMA-BUF exporter to be able to allocate DMA-BUFs directly from existing DMA-BUF Heaps. There is some concern that the premise of DMA-BUF heaps is that userland knows better about what type of heap memory is needed for a pipeline, so it would likely be best for drivers to import and fill DMA-BUFs allocated by userland instead of allocating one themselves, but this is still up for debate. Signed-off-by: John Stultz Signed-off-by: T.J. Mercier Signed-off-by: Yong Wu [Yong: Fix the checkpatch alignment warning] --- drivers/dma-buf/dma-heap.c | 60 ++++++++++++++++++++++++++++---------- include/linux/dma-heap.h | 25 ++++++++++++++++ 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index dcc0e38c61fa..908bb30dc864 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -53,12 +53,15 @@ static dev_t dma_heap_devt; static struct class *dma_heap_class; static DEFINE_XARRAY_ALLOC(dma_heap_minors); -static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, - unsigned int fd_flags, - unsigned int heap_flags) +struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, + unsigned int fd_flags, + unsigned int heap_flags) { - struct dma_buf *dmabuf; - int fd; + if (fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) + return ERR_PTR(-EINVAL); + + if (heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) + return ERR_PTR(-EINVAL); /* * Allocations from all heaps have to begin @@ -66,9 +69,20 @@ static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, */ len = PAGE_ALIGN(len); if (!len) - return -EINVAL; + return ERR_PTR(-EINVAL); - dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags); + return heap->ops->allocate(heap, len, fd_flags, heap_flags); +} +EXPORT_SYMBOL_GPL(dma_heap_buffer_alloc); + +static int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len, + unsigned int fd_flags, + unsigned int heap_flags) +{ + struct dma_buf *dmabuf; + int fd; + + dmabuf = dma_heap_buffer_alloc(heap, len, fd_flags, heap_flags); if (IS_ERR(dmabuf)) return PTR_ERR(dmabuf); @@ -106,15 +120,9 @@ static long dma_heap_ioctl_allocate(struct file *file, void *data) if (heap_allocation->fd) return -EINVAL; - if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) - return -EINVAL; - - if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) - return -EINVAL; - - fd = dma_heap_buffer_alloc(heap, heap_allocation->len, - heap_allocation->fd_flags, - heap_allocation->heap_flags); + fd = dma_heap_bufferfd_alloc(heap, heap_allocation->len, + heap_allocation->fd_flags, + heap_allocation->heap_flags); if (fd < 0) return fd; @@ -205,6 +213,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) { return heap->name; } +EXPORT_SYMBOL_GPL(dma_heap_get_name); struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) { @@ -290,6 +299,24 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) kfree(heap); return err_ret; } +EXPORT_SYMBOL_GPL(dma_heap_add); + +struct dma_heap *dma_heap_find(const char *name) +{ + struct dma_heap *h; + + mutex_lock(&heap_list_lock); + list_for_each_entry(h, &heap_list, list) { + if (!strcmp(h->name, name)) { + kref_get(&h->refcount); + mutex_unlock(&heap_list_lock); + return h; + } + } + mutex_unlock(&heap_list_lock); + return NULL; +} +EXPORT_SYMBOL_GPL(dma_heap_find); static void dma_heap_release(struct kref *ref) { @@ -315,6 +342,7 @@ void dma_heap_put(struct dma_heap *h) kref_put(&h->refcount, dma_heap_release); mutex_unlock(&heap_list_lock); } +EXPORT_SYMBOL_GPL(dma_heap_put); static char *dma_heap_devnode(const struct device *dev, umode_t *mode) { diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index f3c678892c5c..59e70f6c7a60 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -64,10 +64,35 @@ const char *dma_heap_get_name(struct dma_heap *heap); */ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); +/** + * dma_heap_find - get the heap registered with the specified name + * @name: Name of the DMA-Heap to find + * + * Returns: + * The DMA-Heap with the provided name. + * + * NOTE: DMA-Heaps returned from this function MUST be released using + * dma_heap_put() when the user is done to enable the heap to be unloaded. + */ +struct dma_heap *dma_heap_find(const char *name); + /** * dma_heap_put - drops a reference to a dmabuf heap, potentially freeing it * @heap: the heap whose reference count to decrement */ void dma_heap_put(struct dma_heap *heap); +/** + * dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap + * @heap: DMA-Heap to allocate from + * @len: size to allocate in bytes + * @fd_flags: flags to set on returned dma-buf fd + * @heap_flags: flags to pass to the dma heap + * + * This is for internal dma-buf allocations only. Free returned buffers with dma_buf_put(). + */ +struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, + unsigned int fd_flags, + unsigned int heap_flags); + #endif /* _DMA_HEAPS_H */ From patchwork Mon Sep 11 02:30:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WW9uZyBXdSAo5ZC05YuHKQ==?= X-Patchwork-Id: 721631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7D47C71153 for ; Mon, 11 Sep 2023 02:32:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233017AbjIKCcM (ORCPT ); Sun, 10 Sep 2023 22:32:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233077AbjIKCcF (ORCPT ); Sun, 10 Sep 2023 22:32:05 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35590A2; Sun, 10 Sep 2023 19:31:37 -0700 (PDT) X-UUID: 5298d310504b11eea33bb35ae8d461a2-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=L6zWqvDk0Fsnt2Ma8JXPtwJh9E2ddprvdWAqFylWOEw=; b=AjYGBkFukPEVdu366IDlNJ+YGppirqQmq2L0ezfIBzD9zxclvq99bnjTXO3QfkJur+6I9FlUeRhO13P98lcV7Eftgbtq/2t8Hs+o3WzC1ekj5520Kt9i0x/L5iGRl9tV/pgh50jFFT/S0CdsosOmG7UuFve1HabJXEzsoOSJvGo=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31, REQID:e4f22dec-147c-4f57-a4a6-10d1ba2fc415, IP:0, U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:0ad78a4, CLOUDID:9e0adec2-1e57-4345-9d31-31ad9818b39f, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 5298d310504b11eea33bb35ae8d461a2-20230911 Received: from mtkmbs11n1.mediatek.inc [(172.21.101.185)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 66661310; Mon, 11 Sep 2023 10:31:33 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 10:31:31 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 10:31:30 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , Subject: [PATCH 5/9] dma-buf: heaps: mtk_sec_heap: Initialise tee session Date: Mon, 11 Sep 2023 10:30:34 +0800 Message-ID: <20230911023038.30649-6-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911023038.30649-1-yong.wu@mediatek.com> References: <20230911023038.30649-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--5.016500-8.000000 X-TMASE-MatchedRID: 0PAXRdLSM2zREKzvS64+ofSG/+sPtZVkuLwbhNl9B5WctWHPLT5FfVdL ugvPA8FSTiQecD9aKoRSWg4YBc2bATeE3zHLyrsykDpLRKO9xhTt/okBLaEo+IVoIKH/TMNvQDz RyendZRslFsCeg04xsFJVKV9HqTcMYhdzVnzdRn0ZXJLztZviXH0tCKdnhB589yM15V5aWpj6C0 ePs7A07RD7Pzr2apMzGKTftmZH3RdZ52F24lhYnZBNWIuzQwwp8KmSMO90LShDwZzxAYa4ncNrT E0oNMe+ X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--5.016500-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: 4BF5F12A7380CC06BB42E9353EC5550318E6AC621E5F586C1684C714131CAF542000:8 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org The TEE probe later than dma-buf heap, and PROBE_DEDER doesn't work here since this is not a platform driver, therefore initialise the TEE context/session while we allocate the first secure buffer. Signed-off-by: Yong Wu --- drivers/dma-buf/heaps/mtk_secure_heap.c | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/drivers/dma-buf/heaps/mtk_secure_heap.c b/drivers/dma-buf/heaps/mtk_secure_heap.c index bbf1c8dce23e..e3da33a3d083 100644 --- a/drivers/dma-buf/heaps/mtk_secure_heap.c +++ b/drivers/dma-buf/heaps/mtk_secure_heap.c @@ -10,6 +10,12 @@ #include #include #include +#include +#include + +#define TZ_TA_MEM_UUID "4477588a-8476-11e2-ad15-e41f1390d676" + +#define MTK_TEE_PARAM_NUM 4 /* * MediaTek secure (chunk) memory type @@ -28,17 +34,72 @@ struct mtk_secure_heap_buffer { struct mtk_secure_heap { const char *name; const enum kree_mem_type mem_type; + u32 mem_session; + struct tee_context *tee_ctx; }; +static int mtk_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) +{ + return ver->impl_id == TEE_IMPL_ID_OPTEE; +} + +static int mtk_kree_secure_session_init(struct mtk_secure_heap *sec_heap) +{ + struct tee_param t_param[MTK_TEE_PARAM_NUM] = {0}; + struct tee_ioctl_open_session_arg arg = {0}; + uuid_t ta_mem_uuid; + int ret; + + sec_heap->tee_ctx = tee_client_open_context(NULL, mtk_optee_ctx_match, + NULL, NULL); + if (IS_ERR(sec_heap->tee_ctx)) { + pr_err("%s: open context failed, ret=%ld\n", sec_heap->name, + PTR_ERR(sec_heap->tee_ctx)); + return -ENODEV; + } + + arg.num_params = MTK_TEE_PARAM_NUM; + arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC; + ret = uuid_parse(TZ_TA_MEM_UUID, &ta_mem_uuid); + if (ret) + goto close_context; + memcpy(&arg.uuid, &ta_mem_uuid.b, sizeof(ta_mem_uuid)); + + ret = tee_client_open_session(sec_heap->tee_ctx, &arg, t_param); + if (ret < 0 || arg.ret) { + pr_err("%s: open session failed, ret=%d:%d\n", + sec_heap->name, ret, arg.ret); + ret = -EINVAL; + goto close_context; + } + sec_heap->mem_session = arg.session; + return 0; + +close_context: + tee_client_close_context(sec_heap->tee_ctx); + return ret; +} + static struct dma_buf * mtk_sec_heap_allocate(struct dma_heap *heap, size_t size, unsigned long fd_flags, unsigned long heap_flags) { + struct mtk_secure_heap *sec_heap = dma_heap_get_drvdata(heap); struct mtk_secure_heap_buffer *sec_buf; DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct dma_buf *dmabuf; int ret; + /* + * TEE probe may be late. Initialise the secure session in the first + * allocating secure buffer. + */ + if (!sec_heap->mem_session) { + ret = mtk_kree_secure_session_init(sec_heap); + if (ret) + return ERR_PTR(ret); + } + sec_buf = kzalloc(sizeof(*sec_buf), GFP_KERNEL); if (!sec_buf) return ERR_PTR(-ENOMEM); From patchwork Mon Sep 11 02:30:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WW9uZyBXdSAo5ZC05YuHKQ==?= X-Patchwork-Id: 721630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD808EE49A4 for ; Mon, 11 Sep 2023 02:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233083AbjIKCcu (ORCPT ); Sun, 10 Sep 2023 22:32:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233068AbjIKCcs (ORCPT ); Sun, 10 Sep 2023 22:32:48 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9656CF3; Sun, 10 Sep 2023 19:32:10 -0700 (PDT) X-UUID: 5c982c6c504b11eea33bb35ae8d461a2-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=39hq2JQ9Bk1iNY8+HqzlPlrfG4UkI1hzWFrtdQcPBtk=; b=B7Tp4T9yld+gJdHBdP0ercG89KYUNLAvSjF4UXgWMi+By/DyC5GAQB2W7gkORtxlGPvVojD5Ixw+6Q5mspcpzV32/P/6SsDIMMfsJzw9X6+uj3sF6X0LB6W5YqsOpFlY2qOtCeRG6G7iqBnMtstS2CpPhTZTzJR2prJrzizJZSs=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31, REQID:87da238f-9593-4e40-8d78-ec5ff6694d2e, IP:0, U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:0ad78a4, CLOUDID:2685c713-4929-4845-9571-38c601e9c3c9, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 5c982c6c504b11eea33bb35ae8d461a2-20230911 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1891629504; Mon, 11 Sep 2023 10:31:50 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 10:31:48 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 10:31:47 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , , Anan Sun Subject: [PATCH 7/9] dma-buf: heaps: mtk_sec_heap: Add dma_ops Date: Mon, 11 Sep 2023 10:30:36 +0800 Message-ID: <20230911023038.30649-8-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911023038.30649-1-yong.wu@mediatek.com> References: <20230911023038.30649-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add the dma_ops for this secure heap. a) For secure buffer, cache_ops/mmap are not allowed, thus return EPERM for them. b) The secure buffer can't be accessed in kernel, thus it doesn't have va/dma_address for it. Use the dma_address property to save the "secure handle". Signed-off-by: Anan Sun Signed-off-by: Yong Wu --- drivers/dma-buf/heaps/mtk_secure_heap.c | 116 ++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/drivers/dma-buf/heaps/mtk_secure_heap.c b/drivers/dma-buf/heaps/mtk_secure_heap.c index 14c2a16a7164..daf6cf2121a1 100644 --- a/drivers/dma-buf/heaps/mtk_secure_heap.c +++ b/drivers/dma-buf/heaps/mtk_secure_heap.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,10 @@ struct mtk_secure_heap { struct tee_context *tee_ctx; }; +struct mtk_secure_heap_attachment { + struct sg_table *table; +}; + static int mtk_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) { return ver->impl_id == TEE_IMPL_ID_OPTEE; @@ -142,6 +147,116 @@ static void mtk_sec_mem_release(struct mtk_secure_heap *sec_heap, TZCMD_MEM_SECURECM_UNREF, params); } +static int mtk_sec_heap_attach(struct dma_buf *dmabuf, struct dma_buf_attachment *attachment) +{ + struct mtk_secure_heap_buffer *sec_buf = dmabuf->priv; + struct mtk_secure_heap_attachment *a; + struct sg_table *table; + int ret = 0; + + a = kzalloc(sizeof(*a), GFP_KERNEL); + if (!a) + return -ENOMEM; + + table = kzalloc(sizeof(*table), GFP_KERNEL); + if (!table) { + ret = -ENOMEM; + goto err_free_attach; + } + + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret) + goto err_free_sgt; + sg_set_page(table->sgl, 0, sec_buf->size, 0); + + a->table = table; + attachment->priv = a; + + return 0; + +err_free_sgt: + kfree(table); +err_free_attach: + kfree(a); + return ret; +} + +static void mtk_sec_heap_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attachment) +{ + struct mtk_secure_heap_attachment *a = attachment->priv; + + sg_free_table(a->table); + kfree(a->table); + kfree(a); +} + +static struct sg_table * +mtk_sec_heap_map_dma_buf(struct dma_buf_attachment *attachment, enum dma_data_direction direction) +{ + struct mtk_secure_heap_attachment *a = attachment->priv; + struct dma_buf *dmabuf = attachment->dmabuf; + struct mtk_secure_heap_buffer *sec_buf = dmabuf->priv; + struct sg_table *table = a->table; + + /* + * Technically dma_address refers to the address used by HW, But for secure buffer + * we don't know its dma_address in kernel, Instead, we only know its "secure handle". + * Thus use this property to save the "secure handle", and the user will use it to + * obtain the real address in secure world. + */ + sg_dma_address(table->sgl) = sec_buf->sec_handle; + sg_dma_len(table->sgl) = sec_buf->size; + + return table; +} + +static void +mtk_sec_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, + enum dma_data_direction direction) +{ + struct mtk_secure_heap_attachment *a = attachment->priv; + + WARN_ON(a->table != table); + sg_dma_address(table->sgl) = 0; +} + +static int +mtk_sec_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, enum dma_data_direction direction) +{ + return -EPERM; +} + +static int +mtk_sec_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, enum dma_data_direction direction) +{ + return -EPERM; +} + +static int mtk_sec_heap_dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) +{ + return -EPERM; +} + +static void mtk_sec_heap_free(struct dma_buf *dmabuf) +{ + struct mtk_secure_heap_buffer *sec_buf = dmabuf->priv; + struct mtk_secure_heap *sec_heap = dma_heap_get_drvdata(sec_buf->heap); + + mtk_sec_mem_release(sec_heap, sec_buf); + kfree(sec_buf); +} + +static const struct dma_buf_ops mtk_sec_heap_buf_ops = { + .attach = mtk_sec_heap_attach, + .detach = mtk_sec_heap_detach, + .map_dma_buf = mtk_sec_heap_map_dma_buf, + .unmap_dma_buf = mtk_sec_heap_unmap_dma_buf, + .begin_cpu_access = mtk_sec_heap_dma_buf_begin_cpu_access, + .end_cpu_access = mtk_sec_heap_dma_buf_end_cpu_access, + .mmap = mtk_sec_heap_dma_buf_mmap, + .release = mtk_sec_heap_free, +}; + static struct dma_buf * mtk_sec_heap_allocate(struct dma_heap *heap, size_t size, unsigned long fd_flags, unsigned long heap_flags) @@ -173,6 +288,7 @@ mtk_sec_heap_allocate(struct dma_heap *heap, size_t size, if (ret) goto err_free_buf; exp_info.exp_name = dma_heap_get_name(heap); + exp_info.ops = &mtk_sec_heap_buf_ops; exp_info.size = sec_buf->size; exp_info.flags = fd_flags; exp_info.priv = sec_buf; From patchwork Mon Sep 11 02:30:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WW9uZyBXdSAo5ZC05YuHKQ==?= X-Patchwork-Id: 721629 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 650A9C71153 for ; Mon, 11 Sep 2023 02:33:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233172AbjIKCdI (ORCPT ); Sun, 10 Sep 2023 22:33:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230007AbjIKCdD (ORCPT ); Sun, 10 Sep 2023 22:33:03 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1177BCD5; Sun, 10 Sep 2023 19:32:19 -0700 (PDT) X-UUID: 6428c00e504b11ee8051498923ad61e6-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=k7eKKqiU9u1VTQEhbL4g+T+8Z/Im5zTAmaqVcrzlJog=; b=oKAqoGGgHnTwU2Nrzr77Lv7TzaetUwm2zBYVB2mGQH8flZ/fZGDLB2cPKgwRMRTzdzgWxH2poaxG79v5uQ+IVM6mcX2FUiQzRvCQ1UbaPN7RiFQij01a502CP/yNilRPd3s/shqR3mVWpLZ9o4FqblhdHwlWX2yZpexGuDIZUqw=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31, REQID:2b9c6210-8e26-4923-9451-89365963a96c, IP:0, U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:0ad78a4, CLOUDID:dc85c713-4929-4845-9571-38c601e9c3c9, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 6428c00e504b11ee8051498923ad61e6-20230911 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 650081513; Mon, 11 Sep 2023 10:32:02 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by MTKMBS14N2.mediatek.inc (172.21.101.76) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 10:32:01 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 10:32:00 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , Subject: [PATCH 9/9] dma_buf: heaps: mtk_sec_heap: Add a new CMA heap Date: Mon, 11 Sep 2023 10:30:38 +0800 Message-ID: <20230911023038.30649-10-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911023038.30649-1-yong.wu@mediatek.com> References: <20230911023038.30649-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--7.150000-8.000000 X-TMASE-MatchedRID: l/nE+SiO7Ibv9W9IPgV+ekKcYi5Qw/RVJNtuyL6mpIWgaf0+XUrNk6jp YrigUiQ3RgA/t103tXOp3RVRr+fKXKawq2pdPD7UA9lly13c/gG94JvJnfFrHg6QlBHhBZuwYXo 6e6cMykyCrhXL3UcSds/8MSwnHgK56fubsV+A+k+yntSjDrb64SQwGQSJ46NmXCmcAC8DBrPrio 8O85DXBtuU0Ipyd28f+kFR4g8YiAE/eX/eRWk3RZA6S0SjvcYUmyqQJWNsukna+IH8mvgPVEttN R/47hK8ALglGcpZqpxRomVnnmsja5e/bF1ays2S4RtSDjG+z7BzijlDBYeD/Jsoi2XrUn/Jn6Kd MrRsL14qtq5d3cxkNTx61AcxE5RmHl89tzhQNcPdxB32r2oE1NRT50T+gtKLgL5DmE5InGnToZA 9tPKpDJ6oP1a0mRIj X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--7.150000-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: D012446BA56B603C0A9C42A245B2817DF015B66332AAB4883A6336DF0FC547C72000:8 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Create a new mtk_svp_cma heap from the CMA reserved buffer. When the first allocating buffer, use cma_alloc to prepare whole the CMA range, then send its range to TEE to protect and manage. For the later allocating, we just adds the cma_used_size. When SVP done, cma_release will release the buffer, then kernel may reuse it. Signed-off-by: Yong Wu --- drivers/dma-buf/heaps/Kconfig | 2 +- drivers/dma-buf/heaps/mtk_secure_heap.c | 121 +++++++++++++++++++++++- 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index 729c0cf3eb7c..e101f788ecbf 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -15,7 +15,7 @@ config DMABUF_HEAPS_CMA config DMABUF_HEAPS_MTK_SECURE bool "DMA-BUF MediaTek Secure Heap" - depends on DMABUF_HEAPS && TEE + depends on DMABUF_HEAPS && TEE && CMA help Choose this option to enable dma-buf MediaTek secure heap for Secure Video Path. This heap is backed by TEE client interfaces. If in diff --git a/drivers/dma-buf/heaps/mtk_secure_heap.c b/drivers/dma-buf/heaps/mtk_secure_heap.c index daf6cf2121a1..3f568fe6b569 100644 --- a/drivers/dma-buf/heaps/mtk_secure_heap.c +++ b/drivers/dma-buf/heaps/mtk_secure_heap.c @@ -4,11 +4,12 @@ * * Copyright (C) 2023 MediaTek Inc. */ - +#include #include #include #include #include +#include #include #include #include @@ -25,9 +26,11 @@ * MediaTek secure (chunk) memory type * * @KREE_MEM_SEC_CM_TZ: static chunk memory carved out for trustzone. + * @KREE_MEM_SEC_CM_CMA: dynamic chunk memory carved out from CMA. */ enum kree_mem_type { KREE_MEM_SEC_CM_TZ = 1, + KREE_MEM_SEC_CM_CMA, }; struct mtk_secure_heap_buffer { @@ -42,6 +45,13 @@ struct mtk_secure_heap { const enum kree_mem_type mem_type; u32 mem_session; struct tee_context *tee_ctx; + + struct cma *cma; + struct page *cma_page; + unsigned long cma_paddr; + unsigned long cma_size; + unsigned long cma_used_size; + struct mutex lock; /* lock for cma_used_size */ }; struct mtk_secure_heap_attachment { @@ -90,6 +100,42 @@ static int mtk_kree_secure_session_init(struct mtk_secure_heap *sec_heap) return ret; } +static int mtk_sec_mem_cma_allocate(struct mtk_secure_heap *sec_heap, size_t size) +{ + /* + * Allocate CMA only when allocating buffer for the first time, and just + * increase cma_used_size at the other times. + */ + mutex_lock(&sec_heap->lock); + if (sec_heap->cma_used_size) + goto add_size; + + mutex_unlock(&sec_heap->lock); + sec_heap->cma_page = cma_alloc(sec_heap->cma, sec_heap->cma_size >> PAGE_SHIFT, + get_order(PAGE_SIZE), false); + if (!sec_heap->cma_page) + return -ENOMEM; + + mutex_lock(&sec_heap->lock); +add_size: + sec_heap->cma_used_size += size; + mutex_unlock(&sec_heap->lock); + return sec_heap->cma_used_size; +} + +static void mtk_sec_mem_cma_free(struct mtk_secure_heap *sec_heap, size_t size) +{ + bool cma_is_empty; + + mutex_lock(&sec_heap->lock); + sec_heap->cma_used_size -= size; + cma_is_empty = !sec_heap->cma_used_size; + mutex_unlock(&sec_heap->lock); + + if (cma_is_empty) + cma_release(sec_heap->cma, sec_heap->cma_page, sec_heap->cma_size >> PAGE_SHIFT); +} + static int mtk_sec_mem_tee_service_call(struct tee_context *tee_ctx, u32 session, unsigned int command, struct tee_param *params) @@ -114,23 +160,47 @@ static int mtk_sec_mem_allocate(struct mtk_secure_heap *sec_heap, { struct tee_param params[MTK_TEE_PARAM_NUM] = {0}; u32 mem_session = sec_heap->mem_session; + bool cma_frst_alloc = false; int ret; + if (sec_heap->cma) { + ret = mtk_sec_mem_cma_allocate(sec_heap, sec_buf->size); + if (ret < 0) + return ret; + /* + * When CMA allocates for the first time, pass the CMA range to TEE + * to protect it. It's the first allocating if the cma_used_size is equal + * to this required buffer size. + */ + cma_frst_alloc = (ret == sec_buf->size); + } + params[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; params[0].u.value.a = SZ_4K; /* alignment */ params[0].u.value.b = sec_heap->mem_type; /* memory type */ params[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; params[1].u.value.a = sec_buf->size; params[2].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT; + if (sec_heap->cma && cma_frst_alloc) { + params[2].u.value.a = sec_heap->cma_paddr; + params[2].u.value.b = sec_heap->cma_size; + } /* Always request zeroed buffer */ ret = mtk_sec_mem_tee_service_call(sec_heap->tee_ctx, mem_session, TZCMD_MEM_SECURECM_ZALLOC, params); - if (ret) - return -ENOMEM; + if (ret) { + ret = -ENOMEM; + goto free_cma; + } sec_buf->sec_handle = params[2].u.value.a; return 0; + +free_cma: + if (sec_heap->cma) + mtk_sec_mem_cma_free(sec_heap, sec_buf->size); + return ret; } static void mtk_sec_mem_release(struct mtk_secure_heap *sec_heap, @@ -145,6 +215,9 @@ static void mtk_sec_mem_release(struct mtk_secure_heap *sec_heap, mtk_sec_mem_tee_service_call(sec_heap->tee_ctx, mem_session, TZCMD_MEM_SECURECM_UNREF, params); + + if (sec_heap->cma) + mtk_sec_mem_cma_free(sec_heap, sec_buf->size); } static int mtk_sec_heap_attach(struct dma_buf *dmabuf, struct dma_buf_attachment *attachment) @@ -317,8 +390,41 @@ static struct mtk_secure_heap mtk_sec_heap[] = { .name = "mtk_svp", .mem_type = KREE_MEM_SEC_CM_TZ, }, + { + .name = "mtk_svp_cma", + .mem_type = KREE_MEM_SEC_CM_CMA, + }, }; +static int __init mtk_secure_cma_init(struct reserved_mem *rmem) +{ + struct mtk_secure_heap *sec_heap = NULL; + int ret, i; + + for (i = 0; i < ARRAY_SIZE(mtk_sec_heap); i++) { + if (mtk_sec_heap[i].mem_type != KREE_MEM_SEC_CM_CMA) + continue; + sec_heap = &mtk_sec_heap[i]; + break; + } + if (!sec_heap) + return -ENOENT; + + ret = cma_init_reserved_mem(rmem->base, rmem->size, 0, sec_heap->name, + &sec_heap->cma); + if (ret) { + pr_err("%s: %s set up CMA fail\n", __func__, rmem->name); + return ret; + } + sec_heap->cma_paddr = rmem->base; + sec_heap->cma_size = rmem->size; + + return 0; +} + +RESERVEDMEM_OF_DECLARE(mtk_secure_cma, "mediatek,secure_cma_chunkmem", + mtk_secure_cma_init); + static int mtk_sec_heap_init(void) { struct mtk_secure_heap *sec_heap = mtk_sec_heap; @@ -331,6 +437,15 @@ static int mtk_sec_heap_init(void) exp_info.ops = &mtk_sec_heap_ops; exp_info.priv = (void *)sec_heap; + if (sec_heap->mem_type == KREE_MEM_SEC_CM_CMA) { + if (!sec_heap->cma) { + pr_err("CMA is not ready for %s.\n", sec_heap->name); + continue; + } else { + mutex_init(&sec_heap->lock); + } + } + heap = dma_heap_add(&exp_info); if (IS_ERR(heap)) return PTR_ERR(heap);