Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4987834rwb; Mon, 21 Nov 2022 14:59:22 -0800 (PST) X-Google-Smtp-Source: AA0mqf4+eixemhu4k7CgWA1M1Jb7ADPRGRdGsr5AWjFbGU7sJptiTH+M2QgVCVi0C0a0tUiQgmmt X-Received: by 2002:a17:90a:f598:b0:213:5c5f:f440 with SMTP id ct24-20020a17090af59800b002135c5ff440mr22828635pjb.15.1669071561856; Mon, 21 Nov 2022 14:59:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669071561; cv=none; d=google.com; s=arc-20160816; b=WmEKCI/SAfIKUuB+Kmwsyf6tSZw5oW0S9NUqXZWP+CPm5+sUR9JHTc0PoIZoS7BDnE PCCND1gQosVizT9PMYeKUk7tvdWNdAMe5ScCsw8nb5QxJiRIo/lRR+u7ILfWROKjVRr5 1yZanj1mMSj6xAfV7VWfMlniYKaXzbHmjAuCuI6i+jOKJP+18rFzNbPktr4zbQYFDr// gsak4y9KYuhXXadnleQsjM/HZyP3/HQcnMhGa7Vhb7VlHjQ/CElFCQtFvv45ZEe1gQJ0 Eq4Kdh1P7o8D63HtMT2iMcOcrk/bCnJNVlESnGiHPDcq3RCVDpMeeY+Znq2xnXjvcS+T Rb5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aa2aO55ocCe428mxD8nTmb1E3ngpMrhQWSPNqBvoK8M=; b=B8Xa+TcujQ2jXnrgafomzWK6nRiolYWrfclZdsjSWeCBT5V0CCKc4+uSPW9xitI7Yc J4VJhEy/cmYWxg9502Cqa+ZO8E+EFLLbh1JpXwo0KZnbli+E8KsP+r9ownOlqj+UXMoC FbJxp/FHxjF+Vy/1fmea23obCKAoJsQghJpeHn1Wc7iQWNFNuZvphxyWB0Xkd0gXMBDx C5V3Jgcs/+wTIN5lQZxGU9lJdJ22ccdThZuSm1O+WIsHVt61u6OUYcRnZLHGmWnlDweP kplb+f21fNcyBA4w7wrE7Su8/cgAofQQdt8Dh0fifeQEDHsHhuQA1SzE8IF6wqASdXXJ +bHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=jZPYzY37; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i13-20020a170902cf0d00b001789a178e33si12049746plg.428.2022.11.21.14.59.10; Mon, 21 Nov 2022 14:59:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=jZPYzY37; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231846AbiKUVuB (ORCPT + 91 others); Mon, 21 Nov 2022 16:50:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231953AbiKUVti (ORCPT ); Mon, 21 Nov 2022 16:49:38 -0500 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22E7539D; Mon, 21 Nov 2022 13:49:30 -0800 (PST) Received: from umang.jainideasonboard.com (unknown [103.86.18.138]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 443B1987; Mon, 21 Nov 2022 22:49:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1669067368; bh=rqiQKe8x/h66bZANY3KpcWpfF4EvfCOq2HA27IwSvbE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jZPYzY372R/CFrN83we0RxTdQS3YW5vWs7JZAIA9NOiyJS64W7QXeAdyeXcEId+XG da5UPSy+sC5QDfKZewfoU6JWCWMw6pvnRo/ZAFqcBssUJC4IEyQvt5eEVyGOtSExrf lwyikWBUSLkE4+8WqkMU0ptAxGxi88xDdM8AtwNA= From: Umang Jain To: linux-media@vger.kernel.org, kernel-list@raspberrypi.com, linux-kernel@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-staging@lists.linux.dev, Broadcom internal kernel review list Cc: Dave Stevenson , Florian Fainelli , Naushir Patuck , David Plowman , Kieran Bingham , Laurent Pinchart , Umang Jain Subject: [PATCH 05/14] staging: mmal-vchiq: Use vc-sm-cma to support zero copy Date: Tue, 22 Nov 2022 03:17:13 +0530 Message-Id: <20221121214722.22563-6-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221121214722.22563-1-umang.jain@ideasonboard.com> References: <20221121214722.22563-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dave Stevenson With the vc-sm-cma driver we can support zero copy of buffers between the kernel and VPU. Add this support to mmal-vchiq. Signed-off-by: Dave Stevenson Signed-off-by: Umang Jain --- .../staging/vc04_services/vchiq-mmal/Kconfig | 1 + .../vc04_services/vchiq-mmal/mmal-common.h | 4 + .../vc04_services/vchiq-mmal/mmal-vchiq.c | 83 +++++++++++++++++-- .../vc04_services/vchiq-mmal/mmal-vchiq.h | 1 + 4 files changed, 82 insertions(+), 7 deletions(-) diff --git a/drivers/staging/vc04_services/vchiq-mmal/Kconfig b/drivers/staging/vc04_services/vchiq-mmal/Kconfig index c99525a0bb45..a7c1a7bf516e 100644 --- a/drivers/staging/vc04_services/vchiq-mmal/Kconfig +++ b/drivers/staging/vc04_services/vchiq-mmal/Kconfig @@ -1,6 +1,7 @@ config BCM2835_VCHIQ_MMAL tristate "BCM2835 MMAL VCHIQ service" depends on BCM2835_VCHIQ + select BCM_VC_SM_CMA help Enables the MMAL API over VCHIQ interface as used for the majority of the multimedia services on VideoCore. diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h b/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h index 0443be8198ea..a643cad54b12 100644 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h @@ -50,6 +50,10 @@ struct mmal_buffer { struct mmal_msg_context *msg_context; + struct dma_buf *dma_buf;/* Exported dmabuf fd from videobuf2 */ + void *vcsm_handle; /* VCSM handle having imported the dmabuf */ + u32 vc_handle; /* VC handle to that dmabuf */ + u32 cmd; /* MMAL command. 0=data. */ unsigned long length; u32 mmal_flags; diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c index 1c0047eead7e..f26fb64a2785 100644 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c @@ -27,9 +27,11 @@ #include #include "mmal-common.h" +#include "mmal-parameters.h" #include "mmal-vchiq.h" #include "mmal-msg.h" +#include "vc-sm-cma/vc_sm_knl.h" /* * maximum number of components supported. * This matches the maximum permitted by default on the VPU @@ -416,14 +418,27 @@ buffer_from_host(struct vchiq_mmal_instance *instance, /* buffer header */ m.u.buffer_from_host.buffer_header.cmd = 0; - m.u.buffer_from_host.buffer_header.data = - (u32)(unsigned long)buf->buffer; + if (port->zero_copy) { + m.u.buffer_from_host.buffer_header.data = buf->vc_handle; + } else { + m.u.buffer_from_host.buffer_header.data = + (u32)(unsigned long)buf->buffer; + } + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size; - m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */ - m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */ - m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */ - m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN; - m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN; + if (port->type == MMAL_PORT_TYPE_OUTPUT) { + m.u.buffer_from_host.buffer_header.length = 0; + m.u.buffer_from_host.buffer_header.offset = 0; + m.u.buffer_from_host.buffer_header.flags = 0; + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN; + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN; + } else { + m.u.buffer_from_host.buffer_header.length = buf->length; + m.u.buffer_from_host.buffer_header.offset = 0; + m.u.buffer_from_host.buffer_header.flags = buf->mmal_flags; + m.u.buffer_from_host.buffer_header.pts = buf->pts; + m.u.buffer_from_host.buffer_header.dts = buf->dts; + } /* clear buffer type specific data */ memset(&m.u.buffer_from_host.buffer_header_type_specific, 0, @@ -575,6 +590,22 @@ static void buffer_to_host_cb(struct vchiq_mmal_instance *instance, msg_context->u.bulk.status = msg->h.status; + } else if (msg->u.buffer_from_host.is_zero_copy) { + /* + * Zero copy buffer, so nothing to do. + * Copy buffer info and make callback. + */ + msg_context->u.bulk.buffer_used = + msg->u.buffer_from_host.buffer_header.length; + msg_context->u.bulk.mmal_flags = + msg->u.buffer_from_host.buffer_header.flags; + msg_context->u.bulk.dts = + msg->u.buffer_from_host.buffer_header.dts; + msg_context->u.bulk.pts = + msg->u.buffer_from_host.buffer_header.pts; + msg_context->u.bulk.cmd = + msg->u.buffer_from_host.buffer_header.cmd; + } else if (msg->u.buffer_from_host.buffer_header.length == 0) { /* empty buffer */ if (msg->u.buffer_from_host.buffer_header.flags & @@ -1520,6 +1551,9 @@ int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance, mutex_unlock(&instance->vchiq_mutex); + if (parameter == MMAL_PARAMETER_ZERO_COPY && !ret) + port->zero_copy = !!(*(bool *)value); + return ret; } EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set); @@ -1688,6 +1722,31 @@ int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance, unsigned long flags = 0; int ret; + /* + * We really want to do this in mmal_vchi_buffer_init but can't as + * videobuf2 won't let us have the dmabuf there. + */ + if (port->zero_copy && buffer->dma_buf && !buffer->vcsm_handle) { + pr_debug("%s: import dmabuf %p\n", __func__, buffer->dma_buf); + ret = vc_sm_cma_import_dmabuf(buffer->dma_buf, + &buffer->vcsm_handle); + if (ret) { + pr_err("%s: vc_sm_import_dmabuf_fd failed, ret %d\n", + __func__, ret); + return ret; + } + + buffer->vc_handle = vc_sm_cma_int_handle(buffer->vcsm_handle); + if (!buffer->vc_handle) { + pr_err("%s: vc_sm_int_handle failed %d\n", + __func__, ret); + vc_sm_cma_free(buffer->vcsm_handle); + return ret; + } + pr_debug("%s: import dmabuf %p - got vc handle %08X\n", + __func__, buffer->dma_buf, buffer->vc_handle); + } + ret = buffer_from_host(instance, port, buffer); if (ret == -EINVAL) { /* Port is disabled. Queue for when it is enabled. */ @@ -1721,6 +1780,16 @@ int mmal_vchi_buffer_cleanup(struct mmal_buffer *buf) release_msg_context(msg_context); buf->msg_context = NULL; + if (buf->vcsm_handle) { + int ret; + + pr_debug("%s: vc_sm_cma_free on handle %p\n", __func__, + buf->vcsm_handle); + ret = vc_sm_cma_free(buf->vcsm_handle); + if (ret) + pr_err("%s: vcsm_free failed, ret %d\n", __func__, ret); + buf->vcsm_handle = 0; + } return 0; } EXPORT_SYMBOL_GPL(mmal_vchi_buffer_cleanup); diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h index e3ecd70b9951..9addd627ff5b 100644 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h @@ -49,6 +49,7 @@ typedef void (*vchiq_mmal_buffer_cb)( struct vchiq_mmal_port { u32 enabled:1; + u32 zero_copy:1; u32 handle; u32 type; /* port type, cached to use on port info set */ u32 index; /* port index, cached to use on port info set */ -- 2.37.3