From patchwork Wed Jun 12 13:53:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomeu Vizoso X-Patchwork-Id: 13695045 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2EB8C27C7B for ; Wed, 12 Jun 2024 13:53:27 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A775E10E853; Wed, 12 Jun 2024 13:53:26 +0000 (UTC) Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by gabe.freedesktop.org (Postfix) with ESMTPS id A054F10E856 for ; Wed, 12 Jun 2024 13:53:18 +0000 (UTC) Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-421798185f0so38147195e9.1 for ; Wed, 12 Jun 2024 06:53:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718200397; x=1718805197; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T7r3kSvbbTjgJVEwctFCbvRTGPSHV3jcoNvncpcvHqY=; b=IAKzXo7/qA8NumqtqToIFaCusrT3gfvIpDXVLQyMyxrbJFfL9BWunichceO9xaacW5 JDcStAIxrUepZB42HcZwSoNhi/YCsa1DcqFV/OwaEE7UHe2YH7V9yjC7TX595mW6Ab4T QAOjR5ZP+0JNBx08l6pIVSa7B6vMmmIqCNRF+EJFq3sSGy6czlERDoUUo1goHoIP/bnT Gt6JoSSrfgKEh3drvhCTeX0mIzPIx4tj3IXRlDHcIqfJdzOlOEcGKPHFn99mfuY6qLnt zLHCK/r4UXQ7HpJJg18/KsmaZnSxmWT+eGb+2/7rl/ixIvOu1KI9UVyYCgFlN68yymx3 91VQ== X-Forwarded-Encrypted: i=1; AJvYcCW1LAM7nyCByOsnjkg0GwtrPHkZ4+wk5LDYXkTqN3gQJ8nu2h5klYfD/4cCxqPFAft50EygEstqCikCwE8Fp3TNSchlDKEADElbQKknpELy X-Gm-Message-State: AOJu0YwC5U/nm0+k/aQB93WRFAAPy91+/mvv2DQJ0wiQUE1FqMyMMsz1 cIQxc66F2S0fOTEUDtB9BTaCD8wXfooQFKwwFvAC4MXB4+S1ZKsx X-Google-Smtp-Source: AGHT+IGUBVZP2NwuY2tIGmEPKUE7vkawNnEjSQW1ugPVM6t6UrOumHBZsDMeIXzxIvdwgzeSUxgDrQ== X-Received: by 2002:a05:600c:4ecf:b0:421:7aad:370a with SMTP id 5b1f17b1804b1-422863adb55mr15148235e9.9.1718200396853; Wed, 12 Jun 2024 06:53:16 -0700 (PDT) Received: from ramallet.home (cst-prg-45-36.cust.vodafone.cz. [46.135.45.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-422871ec9e6sm28201695e9.38.2024.06.12.06.53.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 06:53:16 -0700 (PDT) From: Tomeu Vizoso Date: Wed, 12 Jun 2024 15:53:00 +0200 Subject: [PATCH 7/9] accel/rocket: Add IOCTL for BO creation MIME-Version: 1.0 Message-Id: <20240612-6-10-rocket-v1-7-060e48eea250@tomeuvizoso.net> References: <20240612-6-10-rocket-v1-0-060e48eea250@tomeuvizoso.net> In-Reply-To: <20240612-6-10-rocket-v1-0-060e48eea250@tomeuvizoso.net> To: Joerg Roedel , Will Deacon , Robin Murphy , Heiko Stuebner , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Oded Gabbay , Tomeu Vizoso , David Airlie , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Philipp Zabel , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= Cc: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, Tomeu Vizoso X-Mailer: b4 0.13.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This uses the SHMEM DRM helpers and we map right away to the CPU and NPU sides, as all buffers are expected to be accessed from both. Signed-off-by: Tomeu Vizoso Reviewed-by: Jeffrey Hugo --- drivers/accel/rocket/Makefile | 3 +- drivers/accel/rocket/rocket_drv.c | 7 +++- drivers/accel/rocket/rocket_gem.c | 68 +++++++++++++++++++++++++++++++++++++++ drivers/accel/rocket/rocket_gem.h | 28 ++++++++++++++++ include/uapi/drm/rocket_accel.h | 24 ++++++++++++++ 5 files changed, 128 insertions(+), 2 deletions(-) diff --git a/drivers/accel/rocket/Makefile b/drivers/accel/rocket/Makefile index 73a7280d260c..875cac2243d9 100644 --- a/drivers/accel/rocket/Makefile +++ b/drivers/accel/rocket/Makefile @@ -5,4 +5,5 @@ obj-$(CONFIG_DRM_ACCEL_ROCKET) := rocket.o rocket-y := \ rocket_core.o \ rocket_device.o \ - rocket_drv.o + rocket_drv.o \ + rocket_gem.o diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index e339c337fcf9..42196417030e 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -11,9 +11,11 @@ #include #include #include +#include #include "rocket_drv.h" #include "rocket_device.h" +#include "rocket_gem.h" static const char * const rk3588_pm_domains[] = { "npu0", "npu1", "npu2" }; static const char * const rk3588_resets_a[] = { "srst_a0", "srst_a1", "srst_a2" }; @@ -58,6 +60,8 @@ rocket_postclose(struct drm_device *dev, struct drm_file *file) static const struct drm_ioctl_desc rocket_drm_driver_ioctls[] = { #define ROCKET_IOCTL(n, func) \ DRM_IOCTL_DEF_DRV(ROCKET_##n, rocket_ioctl_##func, 0) + + ROCKET_IOCTL(CREATE_BO, create_bo), }; static const struct file_operations rocket_drm_driver_fops = { @@ -70,9 +74,10 @@ static const struct file_operations rocket_drm_driver_fops = { * - 1.0 - initial interface */ static const struct drm_driver rocket_drm_driver = { - .driver_features = DRIVER_COMPUTE_ACCEL, + .driver_features = DRIVER_COMPUTE_ACCEL | DRIVER_GEM, .open = rocket_open, .postclose = rocket_postclose, + .gem_create_object = rocket_gem_create_object, .ioctls = rocket_drm_driver_ioctls, .num_ioctls = ARRAY_SIZE(rocket_drm_driver_ioctls), .fops = &rocket_drm_driver_fops, diff --git a/drivers/accel/rocket/rocket_gem.c b/drivers/accel/rocket/rocket_gem.c new file mode 100644 index 000000000000..e10eb886f150 --- /dev/null +++ b/drivers/accel/rocket/rocket_gem.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright 2024 Tomeu Vizoso */ + +#include +#include + +#include "rocket_gem.h" + +/** + * rocket_gem_create_object - Implementation of driver->gem_create_object. + * @dev: DRM device + * @size: Size in bytes of the memory the object will reference + * + * This lets the GEM helpers allocate object structs for us, and keep + * our BO stats correct. + */ +struct drm_gem_object *rocket_gem_create_object(struct drm_device *dev, size_t size) +{ + struct rocket_gem_object *obj; + + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return ERR_PTR(-ENOMEM); + + return &obj->base.base; +} + +int rocket_ioctl_create_bo(struct drm_device *dev, void *data, struct drm_file *file) +{ + struct drm_rocket_create_bo *args = data; + struct drm_gem_shmem_object *shmem_obj; + struct rocket_gem_object *rkt_obj; + struct drm_gem_object *gem_obj; + struct sg_table *sgt; + int ret; + + shmem_obj = drm_gem_shmem_create(dev, args->size); + if (IS_ERR(shmem_obj)) + return PTR_ERR(shmem_obj); + + gem_obj = &shmem_obj->base; + rkt_obj = to_rocket_bo(gem_obj); + + rkt_obj->size = args->size; + rkt_obj->offset = 0; + mutex_init(&rkt_obj->mutex); + + ret = drm_gem_handle_create(file, gem_obj, &args->handle); + drm_gem_object_put(gem_obj); + if (ret) + goto err; + + sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); + if (IS_ERR(sgt)) { + ret = PTR_ERR(sgt); + goto err; + } + + args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); + args->dma_address = sg_dma_address(shmem_obj->sgt->sgl); + + return 0; + +err: + drm_gem_shmem_object_free(gem_obj); + + return ret; +} diff --git a/drivers/accel/rocket/rocket_gem.h b/drivers/accel/rocket/rocket_gem.h new file mode 100644 index 000000000000..2cb294f25c19 --- /dev/null +++ b/drivers/accel/rocket/rocket_gem.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright 2024 Tomeu Vizoso */ + +#ifndef __ROCKET_GEM_H__ +#define __ROCKET_GEM_H__ + +#include +#include + +struct rocket_gem_object { + struct drm_gem_shmem_object base; + + struct mutex mutex; + size_t size; + u32 offset; +}; + +struct drm_gem_object *rocket_gem_create_object(struct drm_device *dev, size_t size); + +int rocket_ioctl_create_bo(struct drm_device *dev, void *data, struct drm_file *file); + +static inline +struct rocket_gem_object *to_rocket_bo(struct drm_gem_object *obj) +{ + return container_of(to_drm_gem_shmem_obj(obj), struct rocket_gem_object, base); +} + +#endif \ No newline at end of file diff --git a/include/uapi/drm/rocket_accel.h b/include/uapi/drm/rocket_accel.h index 7a179731ffe4..8338726a83c3 100644 --- a/include/uapi/drm/rocket_accel.h +++ b/include/uapi/drm/rocket_accel.h @@ -11,6 +11,30 @@ extern "C" { #endif +#define DRM_ROCKET_CREATE_BO 0x00 + +#define DRM_IOCTL_ROCKET_CREATE_BO DRM_IOWR(DRM_COMMAND_BASE + DRM_ROCKET_CREATE_BO, struct drm_rocket_create_bo) + +/** + * struct drm_rocket_create_bo - ioctl argument for creating Rocket BOs. + * + */ +struct drm_rocket_create_bo { + __u32 size; + + /** Returned GEM handle for the BO. */ + __u32 handle; + + /** + * Returned DMA address for the BO in the NPU address space. This address + * is private to the DRM fd and is valid for the lifetime of the GEM + * handle. + */ + __u64 dma_address; + + /** Offset into the drm node to use for subsequent mmap call. */ + __u64 offset; +}; #if defined(__cplusplus) }