Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp226130lqp; Tue, 11 Jun 2024 22:24:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVdtg+e45XDCt+Qpnvxv1WWLU629frx6NrmjA+GFZVi2irwdTgz3+ijotqPPvAAu4+pnTS0oW1k/7jz32U68+HJELeWhAQbD4/tIug1pw== X-Google-Smtp-Source: AGHT+IGpZiSw2FfGg8EiNZpV0TVISEnU8LIfaKglLt9VN2Qtt7pRkO+jpKQkmpsEp136uWPM7t2N X-Received: by 2002:a05:6a00:244d:b0:704:b8c0:42d1 with SMTP id d2e1a72fcca58-705bceac2e0mr788247b3a.21.1718169840129; Tue, 11 Jun 2024 22:24:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718169840; cv=pass; d=google.com; s=arc-20160816; b=FxlL9YPbN4gZW8yZtPeb9usUQeFNVgpwaie4/EMCBdjXMsZ7odX//PF92Udy61qckp p9mDruIOo2o14P94ulB6ktXh1P3td2EDGcU16xISXxzISUX3iIyH840+CA632+wigtJ0 fv9Mb9bGKdGC0iK8Y+svLj7x5pXYksshSR3PQDPRdaBh5R2Zx5VZYU8roFXrtnnm2jDV 1iPi9mjM0VrStLkT2a+Du0lKf1iV+UVepgR2VNDDaiK5ZoF+zT3USVuAGIA1G4EP0ybP hhJsF8nPUPNc6Pl+GGHjvLcZdHE9RpLH+bIXjbB+EuYtAmuNlIvq1Y+iIsh6V7fkGO03 OQKA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=dhTcYBsCM+Mbg92p9SQKrJMtUFN9/26AX/bqLUidx8Y=; fh=46e+BTbQV9holWFjyOd++Hop8ivGs3ejfXys8g/E+LI=; b=T8SIHTZC3zNwBKeI8bmC+I+2cGtVbRLkqSLVI87lk1MdDif5e+rb5QoFQLPQ7PBqmW GZatScXJkmGltL0W157EzPtt9xuFqqmK5J0E7dSzDbPy8ProhzFi/fUV+mrRdq2K0QKM UiSK3pp5RRO26mqv3Gr7UXkArouem15Gbl3uYbLQUSHAmQWpNByfGeB8CGEYCO/oecgE 2f+5g7M02zaVhWYw7cFUnG0la9V7XAeyQjv+qlu7N9BzVblbB4Lm6gr1s5UURXJXetq1 BQub8UL/3gbqLswsE2ujhU4UkV4JsKPA58gfTLbe8NxAkm6oVbYKxyCiQ//D6vOX4qne NVpA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="AvHtM/Aq"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-210943-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210943-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6ea73407ce5si5993564a12.162.2024.06.11.22.23.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 22:24:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-210943-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="AvHtM/Aq"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-210943-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210943-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 243DAB219F1 for ; Wed, 12 Jun 2024 05:23:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0C6A0376EC; Wed, 12 Jun 2024 05:22:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AvHtM/Aq" Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 2754E36124 for ; Wed, 12 Jun 2024 05:22:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718169769; cv=none; b=o+oQurxafVrR/QpKMS8JUYfLuEKx1Zl2o5Yb7XzXJ22wDfQeD4G0CQ3g7L8UaSig8h9wboI1yUxLwyw9YsNEm5q1Kc6voC3lewKETrkRmUMX5B4MtWsV9Q4LGhdUACGpNI/itQ9ruQjb+PIJrRcGflVYACTdV56MFlB+kuYQDJQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718169769; c=relaxed/simple; bh=r9wMIeC0DedienMoh4i0ppIt0OayZ+q1adMyvBDCkMM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FpnolQ8vZ5IMt5Yr+y+2h2PPj+fYIIyeEyvCyUWjmaUAfRmGfYuEJN0qDahXgi4NtElJN0BBw0UfWz4lahqw6xvhewlVbjqXF0H7QkV7BU0bMqz4UIr3181+AwqYukyEbp+CN16qHprgUnao4Px+jd2NhZCe2w1FC/jK7mPWTXk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AvHtM/Aq; arc=none smtp.client-ip=209.85.128.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-62fffd5d36bso1129607b3.2 for ; Tue, 11 Jun 2024 22:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718169767; x=1718774567; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=dhTcYBsCM+Mbg92p9SQKrJMtUFN9/26AX/bqLUidx8Y=; b=AvHtM/AqQTzWCt/jpMqkv9LBhQKEzmGMoh6FTzT1uL1j4iOFeoZE5gYMMj+uu5Osj5 +GHoOuwi9AJMkwN79KwEeoH1+wMwihEzEuHqgQ+qMaqL5mYkepXIMC1pEo6VkR6nUMNq x5v8wnjt54JtF4Pwd9/QO7DDGXFpVTlft6pWA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718169767; x=1718774567; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=dhTcYBsCM+Mbg92p9SQKrJMtUFN9/26AX/bqLUidx8Y=; b=ictD5qQGFP19XuL2oRdE4FoG5y1mXHllNzoJT0ztWOANmx98itV4o5bcRONYHU/IMF BLT9+CqGClLv+j43TWhuk26cDLpFahtjIz2Ceg6luogxdZKCxb4NSEr+d2sfhfORrI45 w87BxWY/wsxNeWr9q/KDJ5wkEungTuR5ZG59UwPmy3WiVQvQ1S4XZZVRAoSsoiyjSR0k S+iwcNh4v36jy0C5lOzJ+V3MMluMIJbMeyF2fCDzTou8CveZDI+jiQOMvJBLBFyDAfTv xLmXjvP5HhVAgfFgBPoxAAi3vmXRBYuGb6njF/AjdFR/10AXWdgiL/dLHxsuMYFXrZ++ oCCw== X-Forwarded-Encrypted: i=1; AJvYcCUn+ffgzw8uwypNkFtgEzyY+lKo8VShp1OhGWz3llxTBCCwNJd7V0UGqtuwKEChbQSETbw3EaxwZEqpCAinjXNrUG9C9MDuL8O9C6fx X-Gm-Message-State: AOJu0YxbgOrJhj/yveRsCaIlCWk1nkEnh/wv+7NwVMfKJ6k2koFYGoS2 QsfNquOCwYmesmxwtDdwbgJU50jUHNQ2lwIKJBBuGw5Gm/CEx9TONpiFe20ndg== X-Received: by 2002:a05:690c:6612:b0:61b:14a8:7944 with SMTP id 00721157ae682-62fc9ebfa84mr8344767b3.39.1718169766976; Tue, 11 Jun 2024 22:22:46 -0700 (PDT) Received: from chromium.org (174.71.80.34.bc.googleusercontent.com. [34.80.71.174]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6ef51579csm76355645ad.112.2024.06.11.22.22.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 22:22:46 -0700 (PDT) Date: Wed, 12 Jun 2024 14:22:40 +0900 From: Tomasz Figa To: Yunfei Dong Cc: Jeffrey Kardatzke , =?utf-8?B?TsOtY29sYXMgRiAuIFIgLiBBIC4=?= Prado , Nathan Hebert , Nicolas Dufresne , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Sebastian Fricke , Mauro Carvalho Chehab , Marek Szyprowski , Chen-Yu Tsai , Yong Wu , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , Steve Cho , Sumit Semwal , Brian Starkey , John Stultz , "T . J . Mercier" , Christian =?utf-8?B?S8O2bmln?= , Matthias Brugger , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, Project_Global_Chrome_Upstream_Group@mediatek.com Subject: Re: [PATCH v6,12/24] media: mediatek: vcodec: add interface to allocate/free secure memory Message-ID: <4a5tf2cl744xzqslox4ddzmdpuvwksr54g3qk2jl4soatdts45@e6xmmm2ijmv6> References: <20240516122102.16379-1-yunfei.dong@mediatek.com> <20240516122102.16379-13-yunfei.dong@mediatek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240516122102.16379-13-yunfei.dong@mediatek.com> On Thu, May 16, 2024 at 08:20:50PM +0800, Yunfei Dong wrote: > Need to call dma heap interface to allocate/free secure memory when playing > secure video. > > Signed-off-by: Yunfei Dong > --- > .../media/platform/mediatek/vcodec/Kconfig | 1 + > .../mediatek/vcodec/common/mtk_vcodec_util.c | 122 +++++++++++++++++- > .../mediatek/vcodec/common/mtk_vcodec_util.h | 3 + > 3 files changed, 123 insertions(+), 3 deletions(-) > > diff --git a/drivers/media/platform/mediatek/vcodec/Kconfig b/drivers/media/platform/mediatek/vcodec/Kconfig > index bc8292232530..707865703e61 100644 > --- a/drivers/media/platform/mediatek/vcodec/Kconfig > +++ b/drivers/media/platform/mediatek/vcodec/Kconfig > @@ -17,6 +17,7 @@ config VIDEO_MEDIATEK_VCODEC > depends on VIDEO_MEDIATEK_VPU || !VIDEO_MEDIATEK_VPU > depends on MTK_SCP || !MTK_SCP > depends on MTK_SMI || (COMPILE_TEST && MTK_SMI=n) > + depends on DMABUF_HEAPS > select VIDEOBUF2_DMA_CONTIG > select V4L2_MEM2MEM_DEV > select VIDEO_MEDIATEK_VCODEC_VPU if VIDEO_MEDIATEK_VPU > diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.c > index c60e4c193b25..5958dcd7965a 100644 > --- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.c > +++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.c > @@ -5,9 +5,11 @@ > * Tiffany Lin > */ > > +#include > #include > #include > #include > +#include > > #include "../decoder/mtk_vcodec_dec_drv.h" > #include "../encoder/mtk_vcodec_enc_drv.h" > @@ -45,7 +47,7 @@ int mtk_vcodec_write_vdecsys(struct mtk_vcodec_dec_ctx *ctx, unsigned int reg, > } > EXPORT_SYMBOL(mtk_vcodec_write_vdecsys); > > -int mtk_vcodec_mem_alloc(void *priv, struct mtk_vcodec_mem *mem) > +static int mtk_vcodec_mem_alloc_nor(void *priv, struct mtk_vcodec_mem *mem) > { > enum mtk_instance_type inst_type = *((unsigned int *)priv); > struct platform_device *plat_dev; > @@ -75,9 +77,71 @@ int mtk_vcodec_mem_alloc(void *priv, struct mtk_vcodec_mem *mem) > > return 0; > } > -EXPORT_SYMBOL(mtk_vcodec_mem_alloc); > > -void mtk_vcodec_mem_free(void *priv, struct mtk_vcodec_mem *mem) > +static int mtk_vcodec_mem_alloc_sec(struct mtk_vcodec_dec_ctx *ctx, struct mtk_vcodec_mem *mem) > +{ > + struct device *dev = &ctx->dev->plat_dev->dev; > + struct dma_buf *dma_buffer; > + struct dma_heap *vdec_heap; > + struct dma_buf_attachment *attach; > + struct sg_table *sgt; > + unsigned long size = mem->size; > + int ret = 0; > + > + if (!size) > + return -EINVAL; > + > + vdec_heap = dma_heap_find("restricted_mtk_cma"); > + if (!vdec_heap) { > + mtk_v4l2_vdec_err(ctx, "dma heap find failed!"); > + return -EPERM; > + } How is the heap name determined here? My recollection is that the heap name comes from the heap node in the DT, so it may vary depending on the board. Is the heap name documented anywhere in the DT bindings? Shouldn't we rather query DT for a phandle to the right heap? > + > + dma_buffer = dma_heap_buffer_alloc(vdec_heap, size, DMA_HEAP_VALID_FD_FLAGS, > + DMA_HEAP_VALID_HEAP_FLAGS); > + if (IS_ERR_OR_NULL(dma_buffer)) { > + mtk_v4l2_vdec_err(ctx, "dma heap alloc size=0x%lx failed!", size); > + return PTR_ERR(dma_buffer); This will be incorrect if NULL was returned, because the function will return 0. Does dma_heap_buffer_alloc() actually return NULL? > + } > + > + attach = dma_buf_attach(dma_buffer, dev); > + if (IS_ERR_OR_NULL(attach)) { > + mtk_v4l2_vdec_err(ctx, "dma attach size=0x%lx failed!", size); > + ret = PTR_ERR(attach); Ditto. > + goto err_attach; > + } > + > + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); > + if (IS_ERR_OR_NULL(sgt)) { > + mtk_v4l2_vdec_err(ctx, "dma map attach size=0x%lx failed!", size); > + ret = PTR_ERR(sgt); Ditto. > + goto err_sgt; > + } > + > + mem->va = dma_buffer; Isn't this field supposed to point to the kernel mapping of the buffer itself? If we need to store the dma_buf pointer, we should probably add a separate field to avoid (potentially serious) bugs. > + mem->dma_addr = (dma_addr_t)sg_dma_address((sgt)->sgl); Why is this type cast necessary here? > + > + if (!mem->va || !mem->dma_addr) { I don't think any of these 2 conditions are possible, since we already checked for successful completion of the functions above. Also 0 is a valid DMA address, so it shouldn't be considered an error. > + mtk_v4l2_vdec_err(ctx, "dma buffer size=0x%lx failed!", size); > + ret = -EPERM; > + goto err_addr; > + } > + > + mem->attach = attach; > + mem->sgt = sgt; > + > + return 0; > +err_addr: Please name the labels according to the clean-up step they perform first, because it will make it much easier to validate at the goto point whether it jumps to the right place. > + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); > +err_sgt: > + dma_buf_detach(dma_buffer, attach); > +err_attach: > + dma_buf_put(dma_buffer); > + > + return ret; > +} > + > +static void mtk_vcodec_mem_free_nor(void *priv, struct mtk_vcodec_mem *mem) > { > enum mtk_instance_type inst_type = *((unsigned int *)priv); > struct platform_device *plat_dev; > @@ -110,6 +174,57 @@ void mtk_vcodec_mem_free(void *priv, struct mtk_vcodec_mem *mem) > mem->dma_addr = 0; > mem->size = 0; > } > + > +static void mtk_vcodec_mem_free_sec(struct mtk_vcodec_mem *mem) > +{ > + if (mem->sgt) > + dma_buf_unmap_attachment(mem->attach, mem->sgt, DMA_BIDIRECTIONAL); > + dma_buf_detach((struct dma_buf *)mem->va, mem->attach); > + dma_buf_put((struct dma_buf *)mem->va); > + > + mem->attach = NULL; > + mem->sgt = NULL; > + mem->va = NULL; > + mem->dma_addr = 0; > + mem->size = 0; > +} > + > +int mtk_vcodec_mem_alloc(void *priv, struct mtk_vcodec_mem *mem) > +{ > + enum mtk_instance_type inst_type = *((unsigned int *)priv); > + int ret; > + > + if (inst_type == MTK_INST_DECODER) { > + struct mtk_vcodec_dec_ctx *dec_ctx = priv; > + > + if (dec_ctx->is_secure_playback) { > + ret = mtk_vcodec_mem_alloc_sec(dec_ctx, mem); > + goto alloc_end; Why not just return here directly? Best regards, Tomasz > + } > + } > + > + ret = mtk_vcodec_mem_alloc_nor(priv, mem); > +alloc_end: > + > + return ret; > +} > +EXPORT_SYMBOL(mtk_vcodec_mem_alloc); > + > +void mtk_vcodec_mem_free(void *priv, struct mtk_vcodec_mem *mem) > +{ > + enum mtk_instance_type inst_type = *((unsigned int *)priv); > + > + if (inst_type == MTK_INST_DECODER) { > + struct mtk_vcodec_dec_ctx *dec_ctx = priv; > + > + if (dec_ctx->is_secure_playback) { > + mtk_vcodec_mem_free_sec(mem); > + return; > + } > + } > + > + mtk_vcodec_mem_free_nor(priv, mem); > +} > EXPORT_SYMBOL(mtk_vcodec_mem_free); > > void *mtk_vcodec_get_hw_dev(struct mtk_vcodec_dec_dev *dev, int hw_idx) > @@ -171,3 +286,4 @@ EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx); > > MODULE_LICENSE("GPL v2"); > MODULE_DESCRIPTION("Mediatek video codec driver"); > +MODULE_IMPORT_NS(DMA_BUF); > diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.h b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.h > index 85f615cdd4d3..22078e757ed0 100644 > --- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.h > +++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_util.h > @@ -18,6 +18,9 @@ struct mtk_vcodec_mem { > size_t size; > void *va; > dma_addr_t dma_addr; > + > + struct dma_buf_attachment *attach; > + struct sg_table *sgt; > }; > > struct mtk_vcodec_fb { > -- > 2.25.1 >