Skip to contentSkip to conversation

Add a DRM driver to support AI Processing Unit (APU)

PATCH In review Alexandre Bailon 11 participants 24 messages May 17, 2023 – May 26, 2023

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Add a DRM driver to support AI Processing Unit (APU)

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

For the people interested by the firmware or userspace library, the sources are available here:
https://gitlab.baylibre.com/baylibre/libapu/libapu

The support of APU has to be upstreamed to libdrm. Until this is done, you could find the source here:
https://gitlab.baylibre.com/baylibre/libapu/libdrm/-/tree/abailon/main

The driver for mt8183 depends on this series (which is currently blocked):
https://patchwork.kernel.org/project/linux-arm-kernel/list/?series=620429

Alexandre Bailon (5):
drm: Add support of AI Processor Unit (APU)
drm/apu: Add memory allocator
drm/apu: Add support of requests
drm/apu: Add support of IOMMU
dt-bindings: Add bidings for mtk,apu-drm

Julien Stephan (2):
drm/apu: allow platform driver to implement their own mmap function drm/apu: Add support for a simulated APU

 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
 drivers/gpu/drm/Kconfig                       |   2 +
 drivers/gpu/drm/Makefile                      |   1 +
 drivers/gpu/drm/apu/Kconfig                   |  22 +
 drivers/gpu/drm/apu/Makefile                  |  10 +
 drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
 drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
 drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
 drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
 drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
 include/uapi/drm/apu_drm.h                    |  81 +++
 11 files changed, 1776 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml
 create mode 100644 drivers/gpu/drm/apu/Kconfig
 create mode 100644 drivers/gpu/drm/apu/Makefile
 create mode 100644 drivers/gpu/drm/apu/apu_drv.c
 create mode 100644 drivers/gpu/drm/apu/apu_gem.c
 create mode 100644 drivers/gpu/drm/apu/apu_internal.h
 create mode 100644 drivers/gpu/drm/apu/apu_sched.c
 create mode 100644 drivers/gpu/drm/apu/simu_apu.c
 create mode 100644 include/uapi/drm/apu_drm.h
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/7 drm: Add support of AI Processor Unit (APU)

Many AI Processur Unit (APU) have a similar architecture. This driver intends helping supporting them.
This relies on DRM and provides some abstractions useful for AI accelerators.
Currently, this provides the infrastructure to alloc an APU device and register one or many cores.
The driver will takes care to register itself to DRM.

---
 drivers/gpu/drm/Kconfig            |   2 +
 drivers/gpu/drm/Makefile           |   1 +
 drivers/gpu/drm/apu/Kconfig        |  12 ++
 drivers/gpu/drm/apu/Makefile       |   5 +
 drivers/gpu/drm/apu/apu_drv.c      | 272 +++++++++++++++++++++++++++++
 drivers/gpu/drm/apu/apu_internal.h |  68 ++++++++
 include/uapi/drm/apu_drm.h         |  28 +++
 7 files changed, 388 insertions(+)
 create mode 100644 drivers/gpu/drm/apu/Kconfig
 create mode 100644 drivers/gpu/drm/apu/Makefile
 create mode 100644 drivers/gpu/drm/apu/apu_drv.c
 create mode 100644 drivers/gpu/drm/apu/apu_internal.h
 create mode 100644 include/uapi/drm/apu_drm.h
drivers/gpu/drm/Kconfig +2 -0
index ba3fb04bb691..32ffa66a8b54 100644
@@ -371,6 +371,8 @@ source "drivers/gpu/drm/solomon/Kconfig"
 
 source "drivers/gpu/drm/sprd/Kconfig"
 
+source "drivers/gpu/drm/apu/Kconfig"
+
 config DRM_HYPERV
 	tristate "DRM Support for Hyper-V synthetic video device"
 	depends on DRM && PCI && MMU && HYPERV
drivers/gpu/drm/Makefile +1 -0
index a33257d2bc7f..7cd8c0f3936a 100644
@@ -191,6 +191,7 @@ obj-$(CONFIG_DRM_MCDE) += mcde/
 obj-$(CONFIG_DRM_TIDSS) += tidss/
 obj-y			+= xlnx/
 obj-y			+= gud/
+obj-$(CONFIG_DRM_APU) += apu/
 obj-$(CONFIG_DRM_HYPERV) += hyperv/
 obj-y			+= solomon/
 obj-$(CONFIG_DRM_SPRD) += sprd/
drivers/gpu/drm/apu/Kconfig new file +12 -0
new file mode 100644
index 000000000000..226dcf072115
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+
+config DRM_APU
+	tristate "APU (AI Processor Unit)"
+	select DRM_GEM_DMA_HELPER
+	select DRM_KMS_HELPER
+	help
+	  This provides a DRM driver that provides some facilities to
+	  communicate with an AI Processor Unit (APU).
+	  The driver intends to provide a common infrastructure that may be
+	  used to support many different APU.
drivers/gpu/drm/apu/Makefile new file +5 -0
new file mode 100644
index 000000000000..ad85b88a8b52
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+drm_apu-y += apu_drv.o
+
+obj-$(CONFIG_DRM_APU) += drm_apu.o
drivers/gpu/drm/apu/apu_drv.c new file +272 -0

272 lines not on this page · raw message

drivers/gpu/drm/apu/apu_internal.h new file +68 -0

68 lines not on this page · raw message

include/uapi/drm/apu_drm.h new file +28 -0
new file mode 100644
index 000000000000..d50c63d1b813
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note*/
+
+#ifndef __UAPI_APU_DRM_H__
+#define __UAPI_APU_DRM_H__
+
+#include "drm.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define APU_ONLINE		BIT(0)
+
+struct drm_apu_state {
+	__u32 device;
+	__u32 flags;
+};
+
+#define DRM_APU_STATE			0x00
+#define DRM_APU_NUM_IOCTLS		0x01
+
+#define DRM_IOCTL_APU_STATE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_STATE, struct drm_apu_state)
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __UAPI_APU_DRM_H__ */
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/7 drm/apu: Add memory allocator

In reply to Alexandre Bailon

This adds a new ioctl to allocate GEM object.

---
 drivers/gpu/drm/apu/Makefile       |  1 +
 drivers/gpu/drm/apu/apu_drv.c      |  2 ++
 drivers/gpu/drm/apu/apu_gem.c      | 56 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/apu/apu_internal.h | 30 ++++++++++++++++
 include/uapi/drm/apu_drm.h         | 16 ++++++++-
 5 files changed, 104 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/apu/apu_gem.c
drivers/gpu/drm/apu/Makefile +1 -0
index ad85b88a8b52..91894250d4c1 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
 drm_apu-y += apu_drv.o
+drm_apu-y += apu_gem.o
 
 obj-$(CONFIG_DRM_APU) += drm_apu.o
drivers/gpu/drm/apu/apu_drv.c +2 -0
index b420b13a9ffd..323e267b0f53 100644
@@ -20,6 +20,8 @@ static int ioctl_apu_state(struct drm_device *dev, void *data,
 static const struct drm_ioctl_desc ioctls[] = {
 	DRM_IOCTL_DEF_DRV(APU_STATE, ioctl_apu_state,
 			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(APU_GEM_NEW, ioctl_gem_new,
+			  DRM_RENDER_ALLOW),
 };
 
 DEFINE_DRM_GEM_DMA_FOPS(apu_drm_ops);
drivers/gpu/drm/apu/apu_gem.c new file +56 -0

56 lines not on this page · raw message

drivers/gpu/drm/apu/apu_internal.h +30 -0
index 58d93a16c68f..203aadc58b72 100644
@@ -3,6 +3,14 @@
 #define __APU_INTERNAL_H__
 
 #include <drm/drm_drv.h>
+#include <drm/drm_gem_dma_helper.h>
+
+struct apu_gem_object {
+	struct drm_gem_dma_object base;
+	struct mutex mutex;
+	size_t size;
+	u32 offset;
+};
 
 struct apu_core {
 	int device_id;
@@ -54,6 +62,17 @@ struct apu_core_ops {
 	int (*is_ready)(struct apu_core *core);
 };
 
+static inline struct apu_gem_object *to_apu_bo(struct drm_gem_object *obj)
+{
+	return container_of(to_drm_gem_dma_obj(obj), struct apu_gem_object,
+			    base);
+}
+
+static inline void *apu_drm_priv(struct apu_core *apu_core)
+{
+	return apu_core->dev_priv;
+}
+
 struct apu_drm *apu_dev_alloc(struct device *dev);
 int apu_dev_register(struct apu_drm *apu);
 void apu_dev_unregister(struct apu_drm *apu);
@@ -65,4 +84,15 @@ int apu_core_register(struct device *dev, struct apu_core *core, void *priv);
 void apu_core_remove(struct apu_core *core);
 struct apu_core *apu_find_core_by_priv(void *priv);
 
+struct apu_gem_object *to_apu_bo(struct drm_gem_object *obj);
+struct drm_gem_object *apu_gem_create_object(struct drm_device *dev,
+					     size_t size);
+
+int ioctl_gem_new(struct drm_device *dev, void *data,
+		  struct drm_file *file_priv);
+int ioctl_gem_user_new(struct drm_device *dev, void *data,
+		       struct drm_file *file_priv);
+struct dma_buf *apu_gem_prime_export(struct drm_gem_object *gem,
+				     int flags);
+
 #endif /* __APU_INTERNAL_H__ */
include/uapi/drm/apu_drm.h +15 -1
index d50c63d1b813..14fc478f45dc 100644
@@ -9,6 +9,18 @@
 extern "C" {
 #endif
 
+/*
+ * Please note that modifications to all structs defined here are
+ * subject to backwards-compatibility constraints.
+ */
+
+struct drm_apu_gem_new {
+	__u32 size;			/* in */
+	__u32 flags;			/* in */
+	__u32 handle;			/* out */
+	__u64 offset;			/* out */
+};
+
 #define APU_ONLINE		BIT(0)
 
 struct drm_apu_state {
@@ -17,9 +29,11 @@ struct drm_apu_state {
 };
 
 #define DRM_APU_STATE			0x00
-#define DRM_APU_NUM_IOCTLS		0x01
+#define DRM_APU_GEM_NEW			0x01
+#define DRM_APU_NUM_IOCTLS		0x02
 
 #define DRM_IOCTL_APU_STATE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_STATE, struct drm_apu_state)
+#define DRM_IOCTL_APU_GEM_NEW		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_NEW, struct drm_apu_gem_new)
 
 #if defined(__cplusplus)
 }
Signature · 1 line
2.39.2
#3 lore raw

PATCH 3/7 drm/apu: Add support of requests

In reply to Alexandre Bailon

This updates the APU driver to with two new ioctls to queue and dequeue requests.
This uses DRM scheduler to manage the requests. The requests allocation and send and receive operations are platform specifics and must be implemented as callback.

---
 drivers/gpu/drm/apu/Kconfig        |   1 +
 drivers/gpu/drm/apu/Makefile       |   1 +
 drivers/gpu/drm/apu/apu_drv.c      |  52 +--
 drivers/gpu/drm/apu/apu_internal.h |  93 ++++-
 drivers/gpu/drm/apu/apu_sched.c    | 564 +++++++++++++++++++++++++++++
 include/uapi/drm/apu_drm.h         |  31 +-
 6 files changed, 697 insertions(+), 45 deletions(-)
 create mode 100644 drivers/gpu/drm/apu/apu_sched.c
drivers/gpu/drm/apu/Kconfig +1 -0
index 226dcf072115..a769df42091c 100644
@@ -5,6 +5,7 @@ config DRM_APU
 	tristate "APU (AI Processor Unit)"
 	select DRM_GEM_DMA_HELPER
 	select DRM_KMS_HELPER
+	select DRM_SCHED
 	help
 	  This provides a DRM driver that provides some facilities to
 	  communicate with an AI Processor Unit (APU).
drivers/gpu/drm/apu/Makefile +1 -0
index 91894250d4c1..fc8d6380fc38 100644
@@ -2,5 +2,6 @@
 
 drm_apu-y += apu_drv.o
 drm_apu-y += apu_gem.o
+drm_apu-y += apu_sched.o
 
 obj-$(CONFIG_DRM_APU) += drm_apu.o
drivers/gpu/drm/apu/apu_drv.c +10 -42

92 lines not on this page · raw message

drivers/gpu/drm/apu/apu_internal.h +91 -2

139 lines not on this page · raw message

drivers/gpu/drm/apu/apu_sched.c new file +564 -0

564 lines not on this page · raw message

include/uapi/drm/apu_drm.h +30 -1
index 14fc478f45dc..c47000097040 100644
@@ -9,6 +9,8 @@
 extern "C" {
 #endif
 
+#define APU_JOB_COMPLETED 0x80000000
+
 /*
  * Please note that modifications to all structs defined here are
  * subject to backwards-compatibility constraints.
@@ -21,6 +23,29 @@ struct drm_apu_gem_new {
 	__u64 offset;			/* out */
 };
 
+struct drm_apu_gem_queue {
+	__u32 device;
+	__u32 cmd;
+	__u32 out_sync;
+	__u64 bo_handles;
+	__u32 bo_handle_count;
+	__u16 size_in;
+	__u16 size_out;
+	__u64 data;
+};
+
+struct drm_apu_gem_dequeue {
+	__u32 out_sync;
+	__u16 result;
+	__u16 size;
+	__u64 data;
+};
+
+struct apu_job_event {
+	struct drm_event base;
+	__u32 out_sync;
+};
+
 #define APU_ONLINE		BIT(0)
 
 struct drm_apu_state {
@@ -30,10 +55,14 @@ struct drm_apu_state {
 
 #define DRM_APU_STATE			0x00
 #define DRM_APU_GEM_NEW			0x01
-#define DRM_APU_NUM_IOCTLS		0x02
+#define DRM_APU_GEM_QUEUE		0x02
+#define DRM_APU_GEM_DEQUEUE		0x03
+#define DRM_APU_NUM_IOCTLS		0x04
 
 #define DRM_IOCTL_APU_STATE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_STATE, struct drm_apu_state)
 #define DRM_IOCTL_APU_GEM_NEW		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_NEW, struct drm_apu_gem_new)
+#define DRM_IOCTL_APU_GEM_QUEUE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_QUEUE, struct drm_apu_gem_queue)
+#define DRM_IOCTL_APU_GEM_DEQUEUE	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_DEQUEUE, struct drm_apu_gem_dequeue)
 
 #if defined(__cplusplus)
 }
Signature · 1 line
2.39.2
#4 lore raw

PATCH 4/7 drm/apu: Add support of IOMMU

In reply to Alexandre Bailon

Some APU devices are behind an IOMMU.
For some of these devices, we can't use DMA API because they use static addresses so we have to manually use IOMMU API to correctly map the buffers.
This adds support of IOMMU.

---
 drivers/gpu/drm/apu/apu_drv.c      |   4 +
 drivers/gpu/drm/apu/apu_gem.c      | 174 +++++++++++++++++++++++++++++
 drivers/gpu/drm/apu/apu_internal.h |  16 +++
 drivers/gpu/drm/apu/apu_sched.c    |  28 +++++
 include/uapi/drm/apu_drm.h         |  12 +-
 5 files changed, 233 insertions(+), 1 deletion(-)
drivers/gpu/drm/apu/apu_drv.c +4 -0
index b6bd340b2bc8..a0dce785a02a 100644
@@ -23,6 +23,10 @@ static const struct drm_ioctl_desc ioctls[] = {
 			  DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(APU_GEM_DEQUEUE, ioctl_gem_dequeue,
 			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(APU_GEM_IOMMU_MAP, ioctl_gem_iommu_map,
+			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(APU_GEM_IOMMU_UNMAP, ioctl_gem_iommu_unmap,
+			  DRM_RENDER_ALLOW),
 };
 
 DEFINE_DRM_GEM_DMA_FOPS(apu_drm_ops);
drivers/gpu/drm/apu/apu_gem.c +174 -0

189 lines not on this page · raw message

drivers/gpu/drm/apu/apu_internal.h +16 -0
index 021a3efdedf2..ea4183f3fb15 100644
@@ -2,6 +2,9 @@
 #ifndef __APU_INTERNAL_H__
 #define __APU_INTERNAL_H__
 
+#include <linux/iommu.h>
+#include <linux/iova.h>
+
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_dma_helper.h>
 #include <drm/gpu_scheduler.h>
@@ -9,7 +12,10 @@
 struct apu_gem_object {
 	struct drm_gem_dma_object base;
 	struct mutex mutex;
+	struct sg_table *iommu_sgt;
+	int iommu_refcount;
 	size_t size;
+	u32 iova;
 	u32 offset;
 };
 
@@ -35,6 +41,10 @@ struct apu_drm {
 	struct drm_device base;
 	struct device *dev;
 
+	struct iommu_domain *domain;
+	struct iova_domain iovad;
+	int iova_limit_pfn;
+
 	struct list_head cores;
 	struct list_head node;
 
@@ -165,12 +175,18 @@ struct apu_gem_object *to_apu_bo(struct drm_gem_object *obj);
 struct drm_gem_object *apu_gem_create_object(struct drm_device *dev,
 					     size_t size);
 
+int apu_bo_iommu_map(struct apu_drm *apu_drm, struct drm_gem_object *obj);
+void apu_bo_iommu_unmap(struct apu_drm *apu_drm, struct apu_gem_object *obj);
 int ioctl_gem_new(struct drm_device *dev, void *data,
 		  struct drm_file *file_priv);
 int ioctl_gem_user_new(struct drm_device *dev, void *data,
 		       struct drm_file *file_priv);
 struct dma_buf *apu_gem_prime_export(struct drm_gem_object *gem,
 				     int flags);
+int ioctl_gem_iommu_map(struct drm_device *dev, void *data,
+			struct drm_file *file_priv);
+int ioctl_gem_iommu_unmap(struct drm_device *dev, void *data,
+			  struct drm_file *file_priv);
 int ioctl_gem_queue(struct drm_device *dev, void *data,
 		    struct drm_file *file_priv);
 int ioctl_gem_dequeue(struct drm_device *dev, void *data,
drivers/gpu/drm/apu/apu_sched.c +28 -0
index 13b6fbd00bd8..716d4b7f2d55 100644
@@ -117,6 +117,8 @@ static void apu_job_cleanup(struct kref *ref)
 			struct apu_gem_object *apu_obj;
 
 			apu_obj = to_apu_bo(job->bos[i]);
+			if (job->apu->domain)
+				apu_bo_iommu_unmap(job->apu, apu_obj);
 			drm_gem_object_put(job->bos[i]);
 		}
 
@@ -397,6 +399,7 @@ static int apu_lookup_bos(struct drm_device *dev, struct drm_file *file_priv,
 			  struct drm_apu_gem_queue *args, struct apu_job *job)
 {
 	void __user *bo_handles;
+	unsigned int i;
 	int ret;
 
 	job->bo_count = args->bo_handle_count;
@@ -413,6 +416,31 @@ static int apu_lookup_bos(struct drm_device *dev, struct drm_file *file_priv,
 	bo_handles = (void __user *)(uintptr_t) args->bo_handles;
 	ret = drm_gem_objects_lookup(file_priv, bo_handles,
 				     job->bo_count, &job->bos);
+	if (ret)
+		return ret;
+
+	if (!job->apu->domain)
+		return 0;
+
+	for (i = 0; i < job->bo_count; i++) {
+		ret = apu_bo_iommu_map(job->apu, job->bos[i]);
+		if (ret)
+			goto err_iommu_map;
+	}
+
+	return ret;
+
+err_iommu_map:
+	kvfree(job->implicit_fences);
+	for (i = 0; i < job->bo_count; i++) {
+		struct apu_gem_object *apu_obj;
+
+		apu_obj = to_apu_bo(job->bos[i]);
+		if (job->apu->domain)
+			apu_bo_iommu_unmap(job->apu, apu_obj);
+		drm_gem_object_put(job->bos[i]);
+	}
+	kvfree(job->bos);
 
 	return ret;
 }
include/uapi/drm/apu_drm.h +11 -1
index c47000097040..0ecc739d8aed 100644
@@ -41,6 +41,12 @@ struct drm_apu_gem_dequeue {
 	__u64 data;
 };
 
+struct drm_apu_gem_iommu_map {
+	__u64 bo_handles;
+	__u32 bo_handle_count;
+	__u64 bo_device_addresses;
+};
+
 struct apu_job_event {
 	struct drm_event base;
 	__u32 out_sync;
@@ -57,12 +63,16 @@ struct drm_apu_state {
 #define DRM_APU_GEM_NEW			0x01
 #define DRM_APU_GEM_QUEUE		0x02
 #define DRM_APU_GEM_DEQUEUE		0x03
-#define DRM_APU_NUM_IOCTLS		0x04
+#define DRM_APU_GEM_IOMMU_MAP		0x04
+#define DRM_APU_GEM_IOMMU_UNMAP		0x05
+#define DRM_APU_NUM_IOCTLS		0x06
 
 #define DRM_IOCTL_APU_STATE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_STATE, struct drm_apu_state)
 #define DRM_IOCTL_APU_GEM_NEW		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_NEW, struct drm_apu_gem_new)
 #define DRM_IOCTL_APU_GEM_QUEUE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_QUEUE, struct drm_apu_gem_queue)
 #define DRM_IOCTL_APU_GEM_DEQUEUE	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_DEQUEUE, struct drm_apu_gem_dequeue)
+#define DRM_IOCTL_APU_GEM_IOMMU_MAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_IOMMU_MAP, struct drm_apu_gem_iommu_map)
+#define DRM_IOCTL_APU_GEM_IOMMU_UNMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_IOMMU_UNMAP, struct drm_apu_gem_iommu_map)
 
 #if defined(__cplusplus)
 }
Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 4/7] drm/apu: Add support of IOMMU

Quoted 4 lines · Alexandre Bailon

Some APU devices are behind an IOMMU.
For some of these devices, we can't use DMA API because they use static addresses so we have to manually use IOMMU API to correctly map the buffers.

Except you still need to use the DMA for the sake of cache coherency and any other aspects :(

Quoted 60 lines

This adds support of IOMMU.

---
  drivers/gpu/drm/apu/apu_drv.c      |   4 +
  drivers/gpu/drm/apu/apu_gem.c      | 174 +++++++++++++++++++++++++++++
  drivers/gpu/drm/apu/apu_internal.h |  16 +++
  drivers/gpu/drm/apu/apu_sched.c    |  28 +++++
  include/uapi/drm/apu_drm.h         |  12 +-
  5 files changed, 233 insertions(+), 1 deletion(-)
drivers/gpu/drm/apu/apu_drv.c +4 -0
index b6bd340b2bc8..a0dce785a02a 100644
@@ -23,6 +23,10 @@ static const struct drm_ioctl_desc ioctls[] = {
  			  DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(APU_GEM_DEQUEUE, ioctl_gem_dequeue,
  			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(APU_GEM_IOMMU_MAP, ioctl_gem_iommu_map,
+			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(APU_GEM_IOMMU_UNMAP, ioctl_gem_iommu_unmap,
+			  DRM_RENDER_ALLOW),
  };
 
  DEFINE_DRM_GEM_DMA_FOPS(apu_drm_ops);
drivers/gpu/drm/apu/apu_gem.c +11 -0
index 0e7b3b27942c..0a91363754c5 100644
@@ -2,6 +2,9 @@
  //
  // Copyright 2020 BayLibre SAS
 
+#include <linux/iommu.h>
+#include <linux/iova.h>
+
  #include <drm/drm_gem_dma_helper.h>
 
  #include <uapi/drm/apu_drm.h>
@@ -42,6 +45,7 @@ int ioctl_gem_new(struct drm_device *dev, void *data,
  	 */
  	apu_obj->size = args->size;
  	apu_obj->offset = 0;
+	apu_obj->iommu_refcount = 0;
  	mutex_init(&apu_obj->mutex);
 
  	ret = drm_gem_handle_create(file_priv, gem_obj, &args->handle);
@@ -54,3 +58,173 @@ int ioctl_gem_new(struct drm_device *dev, void *data,
 
  	return 0;
  }
+
+void apu_bo_iommu_unmap(struct apu_drm *apu_drm, struct apu_gem_object *obj)
+{
+	int iova_pfn;
+	int i;
+
+	if (!obj->iommu_sgt)

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 77

Quoted 10 lines
drivers/gpu/drm/apu/apu_gem.c +10 -0
+		return;
+
+	mutex_lock(&obj->mutex);
+	obj->iommu_refcount--;
+	if (obj->iommu_refcount) {
+		mutex_unlock(&obj->mutex);
+		return;
+	}
+
+	iova_pfn = PHYS_PFN(obj->iova);

Using mm layer operations on IOVAs looks wrong. In practice I don't think it's ultimately harmful, other than potentially making less efficient use of IOVA space if the CPU page size is larger than the IOMMU page size, but it's still a bad code smell when you're using an IOVA abstraction that is deliberately decoupled from CPU pages.

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 81

Quoted 4 lines
drivers/gpu/drm/apu/apu_gem.c +4 -0
+	for (i = 0; i < obj->iommu_sgt->nents; i++) {
+		iommu_unmap(apu_drm->domain, PFN_PHYS(iova_pfn),
+			    PAGE_ALIGN(obj->iommu_sgt->sgl[i].length));
+		iova_pfn += PHYS_PFN(PAGE_ALIGN(obj->iommu_sgt->sgl[i].length));

You can unmap a set of IOVA-contiguous mappings as a single range with one call.

Quoted 34 lines
drivers/gpu/drm/apu/apu_gem.c +34 -0
+	}
+
+	sg_free_table(obj->iommu_sgt);
+	kfree(obj->iommu_sgt);
+
+	free_iova(&apu_drm->iovad, PHYS_PFN(obj->iova));
+	mutex_unlock(&obj->mutex);
+}
+
+static struct sg_table *apu_get_sg_table(struct drm_gem_object *obj)
+{
+	if (obj->funcs)
+		return obj->funcs->get_sg_table(obj);
+	return NULL;
+}
+
+int apu_bo_iommu_map(struct apu_drm *apu_drm, struct drm_gem_object *obj)
+{
+	struct apu_gem_object *apu_obj = to_apu_bo(obj);
+	struct scatterlist *sgl;
+	phys_addr_t phys;
+	int total_buf_space;
+	int iova_pfn;
+	int iova;
+	int ret;
+	int i;
+
+	mutex_lock(&apu_obj->mutex);
+	apu_obj->iommu_refcount++;
+	if (apu_obj->iommu_refcount != 1) {
+		mutex_unlock(&apu_obj->mutex);
+		return 0;
+	}
+

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 125

Quoted 10 lines
drivers/gpu/drm/apu/apu_gem.c +10 -0
+	apu_obj->iommu_sgt = apu_get_sg_table(obj);
+	if (IS_ERR(apu_obj->iommu_sgt)) {
+		mutex_unlock(&apu_obj->mutex);
+		return PTR_ERR(apu_obj->iommu_sgt);
+	}
+
+	total_buf_space = obj->size;
+	iova_pfn = alloc_iova_fast(&apu_drm->iovad,
+				   total_buf_space >> PAGE_SHIFT,
+				   apu_drm->iova_limit_pfn, true);

If you need things mapped at specific addresses like the commit message claims, the DMA IOVA allocator is a terrible tool for the job. DRM already has its own more flexible abstraction for address space management in the form of drm_mm, so as a DRM driver it would seem a lot more sensible to use one of those.

And even if you could justify using this allocator, I can't imagine there's any way you'd need the _fast version (further illustrated by the fact that you're freeing the IOVAs wrongly for that).

Quoted 14 lines
drivers/gpu/drm/apu/apu_gem.c +14 -0
+	apu_obj->iova = PFN_PHYS(iova_pfn);
+
+	if (!iova_pfn) {
+		dev_err(apu_drm->dev, "Failed to allocate iova address\n");
+		mutex_unlock(&apu_obj->mutex);
+		return -ENOMEM;
+	}
+
+	iova = apu_obj->iova;
+	sgl = apu_obj->iommu_sgt->sgl;
+	for (i = 0; i < apu_obj->iommu_sgt->nents; i++) {
+		phys = page_to_phys(sg_page(&sgl[i]));
+		ret =
+		    iommu_map(apu_drm->domain, PFN_PHYS(iova_pfn), phys,

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 149

Quoted 10 lines
drivers/gpu/drm/apu/apu_gem.c +10 -0
+			      PAGE_ALIGN(sgl[i].length), IOMMU_READ | IOMMU_WRITE,
+			      GFP_KERNEL);
+		if (ret) {
+			dev_err(apu_drm->dev, "Failed to iommu map\n");
+			free_iova(&apu_drm->iovad, iova_pfn);
+			mutex_unlock(&apu_obj->mutex);
+			return ret;
+		}
+		iova += sgl[i].offset + sgl[i].length;
+		iova_pfn += PHYS_PFN(PAGE_ALIGN(sgl[i].length));

This looks a lot like it should just be iommu_map_sg(). Also it makes me suspicious of the relationship between obj->size and the sgtable - if the size is already pre-calculated to include any required padding then why can't the caller just provide aligned SG segments in the first place? Conversely if it's the original un-padded size, then any padding you *do* add at this point means you're going to overrun the allocated IOVA space.

Quoted 11 lines
drivers/gpu/drm/apu/apu_gem.c +11 -0
+	}
+	mutex_unlock(&apu_obj->mutex);
+
+	return 0;
+}
+
+int ioctl_gem_iommu_map(struct drm_device *dev, void *data,
+			struct drm_file *file_priv)
+{
+	struct apu_drm *apu_drm = dev->dev_private;
+	struct drm_apu_gem_iommu_map *args = data;

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 170

Quoted 10 lines
drivers/gpu/drm/apu/apu_gem.c +10 -0
+	struct drm_gem_object **bos;
+	void __user *bo_handles;
+	u64 *das;
+	int ret;
+	int i;
+
+	if (!apu_drm->domain)
+		return -ENODEV;
+
+	das = kvmalloc_array(args->bo_handle_count, sizeof(*das), GFP_KERNEL);

Does anything prevent userspace passing random numbers and being able to cause arbitrarily large allocations of unaccounted kernel memory here?

Quoted 5 lines
drivers/gpu/drm/apu/apu_gem.c +5 -0
+	if (!das)
+		return -ENOMEM;
+
+	bo_handles = (void __user *)(uintptr_t) args->bo_handles;
+	ret = drm_gem_objects_lookup(file_priv, bo_handles,

drivers/gpu/drm/apu/apu_gem.c int ioctl_gem_new(struct drm_device *dev, void *data, · line 185

Quoted 10 lines
drivers/gpu/drm/apu/apu_gem.c +10 -0
+				     args->bo_handle_count, &bos);
+	if (ret) {
+		kvfree(das);
+		return ret;
+	}
+
+	for (i = 0; i < args->bo_handle_count; i++) {
+		ret = apu_bo_iommu_map(apu_drm, bos[i]);
+		if (ret) {
+			/* TODO: handle error */

Yes, that would be a good thing to do.

Quoted 60 lines
drivers/gpu/drm/apu/apu_gem.c +45 -0
+			break;
+		}
+		das[i] = to_apu_bo(bos[i])->iova + to_apu_bo(bos[i])->offset;
+	}
+
+	if (copy_to_user((void *)args->bo_device_addresses, das,
+			 args->bo_handle_count * sizeof(u64))) {
+		ret = -EFAULT;
+		DRM_DEBUG("Failed to copy device addresses\n");
+		goto out;
+	}
+
+out:
+	kvfree(das);
+	kvfree(bos);
+
+	return 0;
+}
+
+int ioctl_gem_iommu_unmap(struct drm_device *dev, void *data,
+			  struct drm_file *file_priv)
+{
+	struct apu_drm *apu_drm = dev->dev_private;
+	struct drm_apu_gem_iommu_map *args = data;
+	struct drm_gem_object **bos;
+	void __user *bo_handles;
+	int ret;
+	int i;
+
+	if (!apu_drm->domain)
+		return -ENODEV;
+
+	bo_handles = (void __user *)(uintptr_t) args->bo_handles;
+	ret = drm_gem_objects_lookup(file_priv, bo_handles,
+				     args->bo_handle_count, &bos);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < args->bo_handle_count; i++)
+		apu_bo_iommu_unmap(apu_drm, to_apu_bo(bos[i]));
+
+	kvfree(bos);
+
+	return 0;
+}
drivers/gpu/drm/apu/apu_internal.h +3 -0
index 021a3efdedf2..ea4183f3fb15 100644
@@ -2,6 +2,9 @@
  #ifndef __APU_INTERNAL_H__
  #define __APU_INTERNAL_H__
 
+#include <linux/iommu.h>
+#include <linux/iova.h>
+
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_dma_helper.h>
  #include <drm/gpu_scheduler.h>

drivers/gpu/drm/apu/apu_internal.h line 18

Quoted 7 lines
drivers/gpu/drm/apu/apu_internal.h +3 -0
@@ -9,7 +12,10 @@
  struct apu_gem_object {
  	struct drm_gem_dma_object base;
  	struct mutex mutex;
+	struct sg_table *iommu_sgt;
+	int iommu_refcount;
  	size_t size;
+	u32 iova;

Really? "Common infrastructure that could be re-used to support many accelerators", in 2023, that still assumes 32-bit addressing?

drivers/gpu/drm/apu/apu_internal.h struct apu_drm { · line 44

Quoted 8 lines
drivers/gpu/drm/apu/apu_internal.h +1 -0
  	u32 offset;
  };
 
@@ -35,6 +41,10 @@ struct apu_drm {
  	struct drm_device base;
  	struct device *dev;
 
+	struct iommu_domain *domain;

Oh, nothing ever allocates this domain or attaches to it, so this is all dead code :(

drivers/gpu/drm/apu/apu_internal.h struct apu_drm { · line 46

Quoted 2 lines
drivers/gpu/drm/apu/apu_internal.h +2 -0
+	struct iova_domain iovad;
+	int iova_limit_pfn;

(and nothing initialises these either)

Quoted 59 lines
drivers/gpu/drm/apu/apu_internal.h +7 -0
+
  	struct list_head cores;
  	struct list_head node;
 
@@ -165,12 +175,18 @@ struct apu_gem_object *to_apu_bo(struct drm_gem_object *obj);
  struct drm_gem_object *apu_gem_create_object(struct drm_device *dev,
  					     size_t size);
 
+int apu_bo_iommu_map(struct apu_drm *apu_drm, struct drm_gem_object *obj);
+void apu_bo_iommu_unmap(struct apu_drm *apu_drm, struct apu_gem_object *obj);
  int ioctl_gem_new(struct drm_device *dev, void *data,
  		  struct drm_file *file_priv);
  int ioctl_gem_user_new(struct drm_device *dev, void *data,
  		       struct drm_file *file_priv);
  struct dma_buf *apu_gem_prime_export(struct drm_gem_object *gem,
  				     int flags);
+int ioctl_gem_iommu_map(struct drm_device *dev, void *data,
+			struct drm_file *file_priv);
+int ioctl_gem_iommu_unmap(struct drm_device *dev, void *data,
+			  struct drm_file *file_priv);
  int ioctl_gem_queue(struct drm_device *dev, void *data,
  		    struct drm_file *file_priv);
  int ioctl_gem_dequeue(struct drm_device *dev, void *data,
drivers/gpu/drm/apu/apu_sched.c +14 -0
index 13b6fbd00bd8..716d4b7f2d55 100644
@@ -117,6 +117,8 @@ static void apu_job_cleanup(struct kref *ref)
  			struct apu_gem_object *apu_obj;
 
  			apu_obj = to_apu_bo(job->bos[i]);
+			if (job->apu->domain)
+				apu_bo_iommu_unmap(job->apu, apu_obj);
  			drm_gem_object_put(job->bos[i]);
  		}
 
@@ -397,6 +399,7 @@ static int apu_lookup_bos(struct drm_device *dev, struct drm_file *file_priv,
  			  struct drm_apu_gem_queue *args, struct apu_job *job)
  {
  	void __user *bo_handles;
+	unsigned int i;
  	int ret;
 
  	job->bo_count = args->bo_handle_count;
@@ -413,6 +416,31 @@ static int apu_lookup_bos(struct drm_device *dev, struct drm_file *file_priv,
  	bo_handles = (void __user *)(uintptr_t) args->bo_handles;
  	ret = drm_gem_objects_lookup(file_priv, bo_handles,
  				     job->bo_count, &job->bos);
+	if (ret)
+		return ret;
+
+	if (!job->apu->domain)
+		return 0;
+
+	for (i = 0; i < job->bo_count; i++) {
+		ret = apu_bo_iommu_map(job->apu, job->bos[i]);
+		if (ret)
+			goto err_iommu_map;
+	}

drivers/gpu/drm/apu/apu_sched.c static int apu_lookup_bos(struct drm_device *dev, struct drm_file *file_priv, · line 439

Quoted 10 lines
drivers/gpu/drm/apu/apu_sched.c +10 -0
+
+	return ret;
+
+err_iommu_map:
+	kvfree(job->implicit_fences);
+	for (i = 0; i < job->bo_count; i++) {
+		struct apu_gem_object *apu_obj;
+
+		apu_obj = to_apu_bo(job->bos[i]);
+		if (job->apu->domain)

If the domain *did* ever exist, but could suddenly disappear at any point after you've decided to go ahead and start mapping things into it, then there is a heck of a lot of sychronisation missing from this whole infrastructure.

Thanks,
Robin.

Quoted 42 lines
drivers/gpu/drm/apu/apu_sched.c +4 -0
+			apu_bo_iommu_unmap(job->apu, apu_obj);
+		drm_gem_object_put(job->bos[i]);
+	}
+	kvfree(job->bos);
 
  	return ret;
  }
include/uapi/drm/apu_drm.h +11 -1
index c47000097040..0ecc739d8aed 100644
@@ -41,6 +41,12 @@ struct drm_apu_gem_dequeue {
  	__u64 data;
  };
 
+struct drm_apu_gem_iommu_map {
+	__u64 bo_handles;
+	__u32 bo_handle_count;
+	__u64 bo_device_addresses;
+};
+
  struct apu_job_event {
  	struct drm_event base;
  	__u32 out_sync;
@@ -57,12 +63,16 @@ struct drm_apu_state {
  #define DRM_APU_GEM_NEW			0x01
  #define DRM_APU_GEM_QUEUE		0x02
  #define DRM_APU_GEM_DEQUEUE		0x03
-#define DRM_APU_NUM_IOCTLS		0x04
+#define DRM_APU_GEM_IOMMU_MAP		0x04
+#define DRM_APU_GEM_IOMMU_UNMAP		0x05
+#define DRM_APU_NUM_IOCTLS		0x06
 
  #define DRM_IOCTL_APU_STATE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_STATE, struct drm_apu_state)
  #define DRM_IOCTL_APU_GEM_NEW		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_NEW, struct drm_apu_gem_new)
  #define DRM_IOCTL_APU_GEM_QUEUE		DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_QUEUE, struct drm_apu_gem_queue)
  #define DRM_IOCTL_APU_GEM_DEQUEUE	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_DEQUEUE, struct drm_apu_gem_dequeue)
+#define DRM_IOCTL_APU_GEM_IOMMU_MAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_IOMMU_MAP, struct drm_apu_gem_iommu_map)
+#define DRM_IOCTL_APU_GEM_IOMMU_UNMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_APU_GEM_IOMMU_UNMAP, struct drm_apu_gem_iommu_map)
 
  #if defined(__cplusplus)
  }
#6 lore raw

PATCH 5/7 drm/apu: allow platform driver to implement their own mmap function

In reply to Alexandre Bailon

From: Julien Stephan <[email protected]>

By default we will call drm_gem_mmap() unless the apu driver has declared it's own mmap handler.

---
 drivers/gpu/drm/apu/apu_drv.c      | 38 +++++++++++++++++++++++++++++-
 drivers/gpu/drm/apu/apu_internal.h |  2 ++
 2 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/apu/apu_drv.c +37 -1
index a0dce785a02a..703d4515f075 100644
@@ -29,7 +29,20 @@ static const struct drm_ioctl_desc ioctls[] = {
 			  DRM_RENDER_ALLOW),
 };
 
-DEFINE_DRM_GEM_DMA_FOPS(apu_drm_ops);
+static int apu_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
+
+static const struct file_operations apu_drm_ops = {
+	.owner          = THIS_MODULE,
+	.open           = drm_open,
+	.release        = drm_release,
+	.unlocked_ioctl = drm_ioctl,
+	.compat_ioctl   = drm_compat_ioctl,
+	.poll           = drm_poll,
+	.read           = drm_read,
+	.llseek         = noop_llseek,
+	.mmap           = apu_drm_gem_mmap,
+	DRM_GEM_DMA_UNMAPPED_AREA_FOPS
+};
 
 static struct drm_driver apu_drm_driver = {
 	.driver_features = DRIVER_GEM | DRIVER_SYNCOBJ,
@@ -45,6 +58,29 @@ static struct drm_driver apu_drm_driver = {
 	DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_dma_dumb_create),
 };
 
+/**
+ * apu_drm_gem_mmap()
+ *
+ * @filp: DRM file pointer
+ * @vma: VMA for the area to be mapped
+ *
+ * by default will call drm_gem_mmap() unless the apu driver has declared it's
+ * own mmap handler
+ *
+ */
+static int apu_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct drm_file *priv = filp->private_data;
+	struct drm_device *dev = priv->minor->dev;
+	struct apu_drm *apu = dev->dev_private;
+
+	if (apu->mmap)
+		return apu->mmap(filp, vma);
+	else
+		return drm_gem_mmap(filp, vma);
+}
+
+
 /**
  * apu_dev_alloc() - Allocate a new APU device
  *
drivers/gpu/drm/apu/apu_internal.h +2 -0
index ea4183f3fb15..46e0b2be7821 100644
@@ -45,6 +45,8 @@ struct apu_drm {
 	struct iova_domain iovad;
 	int iova_limit_pfn;
 
+	int (*mmap)(struct file *filp, struct vm_area_struct *vma);
+
 	struct list_head cores;
 	struct list_head node;
 
Signature · 1 line
2.39.2
#7 lore raw

Re: [PATCH 5/7] drm/apu: allow platform driver to implement their own mmap function

Quoted 7 lines · Alexandre Bailon

From: Julien Stephan <[email protected]>

By default we will call drm_gem_mmap() unless the apu driver has declared it's own mmap handler.

One does not have to review own code. We all assume that we send good code which we do not have to review by ourselves (by the author). We also assume we make mistakes, which we cannot find, thus other person's review is important.

Adding own review tag suggests you added them mechanically, so I doubt that they really happened.

Anyway, your SoB is missing.

Best regards,
Krzysztof

#8 lore raw

Re: [PATCH 5/7] drm/apu: allow platform driver to implement their own mmap function

Quoted 6 lines · Krzysztof Kozlowski
Quoted 5 lines · Alexandre Bailon

From: Julien Stephan <[email protected]>

By default we will call drm_gem_mmap() unless the apu driver has declared it's own mmap handler.

Quoted 7 lines
Quoted 2 lines

One does not have to review own code. We all assume that we send good code which we do not have to review by ourselves (by the author). We also assume we make mistakes, which we cannot find, thus other person's review is important.

I am sorry, I am the one who made the misstake.
I squashed this patch with another one I made, lost my signedof and left the reviewed by which indeed doesn't make any sense.

Best Regards,
Alexandre

Quoted 9 lines

Adding own review tag suggests you added them mechanically, so I doubt that they really happened.

Anyway, your SoB is missing.

Best regards,
Krzysztof

#9 lore raw

PATCH 6/7 drm/apu: Add support for a simulated APU

In reply to Alexandre Bailon

From: Julien Stephan <[email protected]>

This implements a driver to use with a simulation APU.

This is useful for testing purpose and can be used as a basis to implement real platform driver.
Communication between the simulated APU and the driver is done using netlink socket.

---
 drivers/gpu/drm/apu/Kconfig    |   9 +
 drivers/gpu/drm/apu/Makefile   |   3 +
 drivers/gpu/drm/apu/simu_apu.c | 313 +++++++++++++++++++++++++++++++++
 3 files changed, 325 insertions(+)
 create mode 100644 drivers/gpu/drm/apu/simu_apu.c
drivers/gpu/drm/apu/Kconfig +9 -0
index a769df42091c..e0ffc166497c 100644
@@ -11,3 +11,12 @@ config DRM_APU
 	  communicate with an AI Processor Unit (APU).
 	  The driver intends to provide a common infrastructure that may be
 	  used to support many different APU.
+
+config DRM_SIMU_APU
+	tristate "SIMULATION APU DRM driver"
+	depends on DRM_APU
+	default n
+	help
+	  This provides a driver using netlink socket to communicate
+	  with a simu APU.
+	  This is useful for simulation and testing of libAPU stack.
drivers/gpu/drm/apu/Makefile +3 -0
index fc8d6380fc38..0b007854a07f 100644
@@ -4,4 +4,7 @@ drm_apu-y += apu_drv.o
 drm_apu-y += apu_gem.o
 drm_apu-y += apu_sched.o
 
+drm_simu_apu-y += simu_apu.o
+
 obj-$(CONFIG_DRM_APU) += drm_apu.o
+obj-$(CONFIG_DRM_SIMU_APU) += drm_simu_apu.o
drivers/gpu/drm/apu/simu_apu.c new file +313 -0

313 lines not on this page · raw message

Signature · 1 line
2.39.2
#10 lore raw

PATCH 7/7 dt-bindings: Add bidings for mtk,apu-drm

In reply to Alexandre Bailon

This adds the device tree bindings for the APU DRM driver.

---
 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +38 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm
+
+  remoteproc:
+    maxItems: 2
+    description:
+      Handle to remoteproc devices controlling the APU
+
+  iova:
+    maxItems: 1
+    description:
+      Address and size of virtual memory that could used by the APU
+
+required:
+  - compatible
+  - remoteproc
+  - iova
+
+additionalProperties: false
+
+examples:
+  - |
+    apu@0 {
+      compatible = "mediatek,apu-drm";
+      remoteproc = <&vpu0>, <&vpu1>;
+      iova = <0 0x60000000 0 0x10000000>;
+    };
+
+...
Signature · 1 line
2.39.2
#11 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

Il 17/05/23 16:52, Alexandre Bailon ha scritto:

Quoted 6 lines

This adds the device tree bindings for the APU DRM driver.

---
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++

mediatek,mt(model)-apu.yaml

Quoted 10 lines
  1 file changed, 38 insertions(+)

create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +1 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 11

Quoted 10 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +10 -0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm

const: mediatek,mt8195-apu (or whatever else).

...besides, I don't think that this patch even belongs to this series? :-) Spoiler alert! :-)

Cheers,
Angelo

#12 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

Quoted 24 lines · AngeloGioacchino Del Regno

Il 17/05/23 16:52, Alexandre Bailon ha scritto:

Quoted 6 lines

This adds the device tree bindings for the APU DRM driver.

---
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++

mediatek,mt(model)-apu.yaml

Quoted 14 lines
  1 file changed, 38 insertions(+)

create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +5 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
Quoted 8 lines

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 11

Quoted 6 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +6 -0
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm

const: mediatek,mt8195-apu (or whatever else).

Aye, and drop the references to DRM in the title field too (and add the vendor name?).

Quoted 3 lines

...besides, I don't think that this patch even belongs to this series? :-) Spoiler alert! :-)

Well, I do not know what this means - but if it is being respun as part of some other work, a description field should be added to the binding.

Cheers,
Conor.

#13 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

In reply to AngeloGioacchino Del Regno
Quoted 33 lines · AngeloGioacchino Del Regno

Il 17/05/23 16:52, Alexandre Bailon ha scritto:

Quoted 6 lines

This adds the device tree bindings for the APU DRM driver.

---
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++

mediatek,mt(model)-apu.yaml

Quoted 10 lines
  1 file changed, 38 insertions(+)

  create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +1 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 11

Quoted 10 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +10 -0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm

const: mediatek,mt8195-apu (or whatever else).

Quoted 2 lines

...besides, I don't think that this patch even belongs to this series? :-) Spoiler alert! :-)

Actually, it does!
I forgot to send the patch that adds the platform driver ^^'

Thanks,
Alexandre

Quoted 5 lines

Cheers,
Angelo

maintainer
#14 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

In reply to Alexandre Bailon
Quoted 9 lines · Alexandre Bailon

This adds the device tree bindings for the APU DRM driver.

---
 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#
./Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml: $id: relative path/filename doesn't match actual path or filename
expected: http://devicetree.org/schemas/gpu/mtk,apu-drm.yaml#
Documentation/devicetree/bindings/gpu/mtk,apu-drm.example.dts:18.15-22.11: Warning (unit_address_vs_reg): /example-0/apu@0: node has a unit name, but no reg or ranges property /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.example.dtb: apu@0: remoteproc: [[4294967295, 4294967295]] is too short
From schema: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#15 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

In reply to Alexandre Bailon
Quoted 9 lines · Alexandre Bailon

This adds the device tree bindings for the APU DRM driver.

---
 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml: 'maintainers' is a required property
hint: Metaschema for devicetree binding documentation
from schema $id: http://devicetree.org/meta-schemas/base.yaml#
./Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml: $id: relative path/filename doesn't match actual path or filename
expected: http://devicetree.org/schemas/gpu/mtk,apu-drm.yaml#
Documentation/devicetree/bindings/gpu/mtk,apu-drm.example.dts:18.15-22.11: Warning (unit_address_vs_reg): /example-0/apu@0: node has a unit name, but no reg or ranges property /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.example.dtb: apu@0: remoteproc: [[4294967295, 4294967295]] is too short
From schema: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/1782720

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

#16 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

In reply to Alexandre Bailon
Quoted 4 lines · Alexandre Bailon

This adds the device tree bindings for the APU DRM driver.

There are so many errors in this patch... that for sure it was not

Quoted 12 lines · except what was already
---
 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +1 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 11

Quoted 10 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +10 -0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm

drm is not hardware. Drop everywhere or explain the acronym. If you explain it like Linux explains, then: drm is not hardware.

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 16

Quoted 5 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +5 -0
+
+  remoteproc:
+    maxItems: 2
+    description:
+      Handle to remoteproc devices controlling the APU

Missing type/ref. Does not look like generic property, so missing vendor prefix.

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 21

Quoted 5 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +5 -0
+
+  iova:
+    maxItems: 1
+    description:
+      Address and size of virtual memory that could used by the APU

So it is a reg?

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 32

Quoted 11 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +11 -0
+
+required:
+  - compatible
+  - remoteproc
+  - iova
+
+additionalProperties: false
+
+examples:
+  - |
+    apu@0 {

Where is reg? @0 says you have it...

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 35

Quoted 3 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +3 -0
+      compatible = "mediatek,apu-drm";
+      remoteproc = <&vpu0>, <&vpu1>;
+      iova = <0 0x60000000 0 0x10000000>;

Why would you store virtual address, not real, in DT? Let's say you have some randomization like KASLR. How is it going to work? Drop, it is not hardware property.

Best regards,
Krzysztof

#17 lore raw

Re: [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm

Quoted 66 lines · Krzysztof Kozlowski
Quoted 4 lines · Alexandre Bailon

This adds the device tree bindings for the APU DRM driver.

There are so many errors in this patch... that for sure it was not

Quoted 12 lines · except what was already
---
 .../devicetree/bindings/gpu/mtk,apu-drm.yaml  | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +1 -0
new file mode 100644
index 000000000000..6f432d3ea478
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 11

Quoted 10 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +10 -0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/gpu/mediatek,apu-drm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AI Processor Unit DRM
+
+properties:
+  compatible:
+    const: mediatek,apu-drm

drm is not hardware. Drop everywhere or explain the acronym. If you explain it like Linux explains, then: drm is not hardware.

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 16

Quoted 5 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +5 -0
+
+  remoteproc:
+    maxItems: 2
+    description:
+      Handle to remoteproc devices controlling the APU

Missing type/ref. Does not look like generic property, so missing vendor prefix.

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 21

Quoted 5 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +5 -0
+
+  iova:
+    maxItems: 1
+    description:
+      Address and size of virtual memory that could used by the APU

So it is a reg?

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 32

Quoted 11 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +11 -0
+
+required:
+  - compatible
+  - remoteproc
+  - iova
+
+additionalProperties: false
+
+examples:
+  - |
+    apu@0 {

Where is reg? @0 says you have it...

Quoted 7 lines

Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml line 35

Quoted 3 lines
Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml new file +3 -0
+      compatible = "mediatek,apu-drm";
+      remoteproc = <&vpu0>, <&vpu1>;
+      iova = <0 0x60000000 0 0x10000000>;

Why would you store virtual address, not real, in DT? Let's say you have some randomization like KASLR. How is it going to work? Drop, it is not hardware property.

Actually RANDOMIZE_BASE. KASLR randomizes the physical.

Best regards,
Krzysztof

#18 lore raw
In reply to Alexandre Bailon

Hi,

it looks like this driver belongs into driver/accel.

Best regards
Thomas

Am 17.05.23 um 16:52 schrieb Alexandre Bailon:

Quoted 52 lines

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

For the people interested by the firmware or userspace library, the sources are available here:
https://gitlab.baylibre.com/baylibre/libapu/libapu

The support of APU has to be upstreamed to libdrm. Until this is done, you could find the source here:
https://gitlab.baylibre.com/baylibre/libapu/libdrm/-/tree/abailon/main

The driver for mt8183 depends on this series (which is currently blocked):
https://patchwork.kernel.org/project/linux-arm-kernel/list/?series=620429

Alexandre Bailon (5):
drm: Add support of AI Processor Unit (APU)
drm/apu: Add memory allocator
drm/apu: Add support of requests
drm/apu: Add support of IOMMU
dt-bindings: Add bidings for mtk,apu-drm

Julien Stephan (2):
drm/apu: allow platform driver to implement their own mmap function drm/apu: Add support for a simulated APU

  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
  drivers/gpu/drm/Kconfig                       |   2 +
  drivers/gpu/drm/Makefile                      |   1 +
  drivers/gpu/drm/apu/Kconfig                   |  22 +
  drivers/gpu/drm/apu/Makefile                  |  10 +
  drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
  drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
  drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
  drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
  drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
  include/uapi/drm/apu_drm.h                    |  81 +++
  11 files changed, 1776 insertions(+)

create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml create mode 100644 drivers/gpu/drm/apu/Kconfig
create mode 100644 drivers/gpu/drm/apu/Makefile
create mode 100644 drivers/gpu/drm/apu/apu_drv.c
create mode 100644 drivers/gpu/drm/apu/apu_gem.c
create mode 100644 drivers/gpu/drm/apu/apu_internal.h
create mode 100644 drivers/gpu/drm/apu/apu_sched.c
create mode 100644 drivers/gpu/drm/apu/simu_apu.c
create mode 100644 include/uapi/drm/apu_drm.h

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#19 lore raw
In reply to Alexandre Bailon
Quoted 7 lines · Alexandre Bailon

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

This looks like the 3 existing Accel drivers. Why is this in DRM?

Quoted 3 lines

For the people interested by the firmware or userspace library, the sources are available here:
https://gitlab.baylibre.com/baylibre/libapu/libapu

I don't see a compiler. What am I missing?

Quoted 18 lines

The support of APU has to be upstreamed to libdrm. Until this is done, you could find the source here:
https://gitlab.baylibre.com/baylibre/libapu/libdrm/-/tree/abailon/main

The driver for mt8183 depends on this series (which is currently blocked):
https://patchwork.kernel.org/project/linux-arm-kernel/list/?series=620429

Alexandre Bailon (5):
drm: Add support of AI Processor Unit (APU)
drm/apu: Add memory allocator
drm/apu: Add support of requests
drm/apu: Add support of IOMMU
dt-bindings: Add bidings for mtk,apu-drm

Julien Stephan (2):
drm/apu: allow platform driver to implement their own mmap function drm/apu: Add support for a simulated APU

Quoted 11 lines
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
  drivers/gpu/drm/Kconfig                       |   2 +
  drivers/gpu/drm/Makefile                      |   1 +
  drivers/gpu/drm/apu/Kconfig                   |  22 +
  drivers/gpu/drm/apu/Makefile                  |  10 +
  drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
  drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
  drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
  drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
  drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
  include/uapi/drm/apu_drm.h                    |  81 +++

"apu" seems too generic. We already have 3 "AI processing units" over in drivers/accel already...

Quoted 11 lines
  11 files changed, 1776 insertions(+)

create mode 100644 Documentation/devicetree/bindings/gpu/mtk,apu-drm.yaml create mode 100644 drivers/gpu/drm/apu/Kconfig
create mode 100644 drivers/gpu/drm/apu/Makefile
create mode 100644 drivers/gpu/drm/apu/apu_drv.c
create mode 100644 drivers/gpu/drm/apu/apu_gem.c
create mode 100644 drivers/gpu/drm/apu/apu_internal.h
create mode 100644 drivers/gpu/drm/apu/apu_sched.c
create mode 100644 drivers/gpu/drm/apu/simu_apu.c
create mode 100644 include/uapi/drm/apu_drm.h

I feel like device/driver based documentation in Documentation/ would really help in reviews.

-Jeff

#20 lore raw
Quoted 10 lines · Jeffrey Hugo
Quoted 7 lines · Alexandre Bailon

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

This looks like the 3 existing Accel drivers. Why is this in DRM?

Yes, this belongs in accel. I think Alex had some issues around the infra in accel with device nodes not appearing/opening properly, but I'll let him comment there. But either way, the right approach should be to fix any issues in accel and move it there.

[...]

Quoted 12 lines
Quoted 11 lines
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
  drivers/gpu/drm/Kconfig                       |   2 +
  drivers/gpu/drm/Makefile                      |   1 +
  drivers/gpu/drm/apu/Kconfig                   |  22 +
  drivers/gpu/drm/apu/Makefile                  |  10 +
  drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
  drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
  drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
  drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
  drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
  include/uapi/drm/apu_drm.h                    |  81 +++
Quoted 2 lines

"apu" seems too generic. We already have 3 "AI processing units" over in drivers/accel already...

Indeed, it is generic, but that's kind of the point for this driver since it's targetted at generalizing the interface with "AI processing units" on a growing number of embedded SoCs (ARM, RISC-V, etc.) In addition, the generic naming is intentional because the goal is bigger than the kernel and is working towards a generic, shared "libAPU" userspace[1], but also common firmware for DSP-style inference engines (e.g. analgous Sound Open Firmware for audio DSPs.)

As usual, the various SoC vendors use different names (APU, NPU, NN unit, etc.) but we'd like a generic name for the class of devices targetted by this driver. And unfortunately, it looks like the equally generic "Versatile processing unit" is already taken Intel's drivers/accel/ivpu. :)

Maybe since this is more about generalizing the interface between the CPU running linux and the APU, what about the name apu_if? But I guess that applies to the other 3 drivers in drivers/accell also. Hmmm...

Naming things is hard[2], so we're definitly open to other ideas. Any suggestions?

Kevin

[1] https://gitlab.baylibre.com/baylibre/libapu/libapu

[2]
"There are 2 hard problems in computer science: cache invalidation,
naming things and off-by-1 errors."
-- https://twitter.com/secretGeek/status/7269997868

#21 lore raw
Quoted 50 lines · Kevin Hilman
Quoted 10 lines · Jeffrey Hugo
Quoted 7 lines · Alexandre Bailon

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

This looks like the 3 existing Accel drivers. Why is this in DRM?

Yes, this belongs in accel. I think Alex had some issues around the infra in accel with device nodes not appearing/opening properly, but I'll let him comment there. But either way, the right approach should be to fix any issues in accel and move it there.

[...]

Quoted 12 lines
Quoted 11 lines
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
  drivers/gpu/drm/Kconfig                       |   2 +
  drivers/gpu/drm/Makefile                      |   1 +
  drivers/gpu/drm/apu/Kconfig                   |  22 +
  drivers/gpu/drm/apu/Makefile                  |  10 +
  drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
  drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
  drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
  drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
  drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
  include/uapi/drm/apu_drm.h                    |  81 +++
Quoted 2 lines

"apu" seems too generic. We already have 3 "AI processing units" over in drivers/accel already...

Indeed, it is generic, but that's kind of the point for this driver since it's targetted at generalizing the interface with "AI processing units" on a growing number of embedded SoCs (ARM, RISC-V, etc.) In addition, the generic naming is intentional because the goal is bigger than the kernel and is working towards a generic, shared "libAPU" userspace[1], but also common firmware for DSP-style inference engines (e.g. analgous Sound Open Firmware for audio DSPs.)

As usual, the various SoC vendors use different names (APU, NPU, NN unit, etc.) but we'd like a generic name for the class of devices targetted by this driver. And unfortunately, it looks like the equally generic "Versatile processing unit" is already taken Intel's drivers/accel/ivpu. :)

Quoted 6 lines

Maybe since this is more about generalizing the interface between the CPU running linux and the APU, what about the name apu_if? But I guess that applies to the other 3 drivers in drivers/accell also. Hmmm...

Naming things is hard[2], so we're definitly open to other ideas. Any suggestions?

Maybe model it according to the tiny driver in drm display ? You can then call it tiny_apu :-)
Disclosure: It was Daniel's suggestion, he can chime in with more details on the tiny driver concept.
Oded

Quoted 10 lines

Kevin

[1] https://gitlab.baylibre.com/baylibre/libapu/libapu

[2]
"There are 2 hard problems in computer science: cache invalidation,
naming things and off-by-1 errors."
-- https://twitter.com/secretGeek/status/7269997868

#22 lore raw
Quoted 55 lines · Oded Gabbay
Quoted 54 lines · Kevin Hilman
Quoted 10 lines · Jeffrey Hugo
Quoted 7 lines · Alexandre Bailon

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

This looks like the 3 existing Accel drivers. Why is this in DRM?

Yes, this belongs in accel. I think Alex had some issues around the infra in accel with device nodes not appearing/opening properly, but I'll let him comment there. But either way, the right approach should be to fix any issues in accel and move it there.

[...]

Quoted 12 lines
Quoted 11 lines
  .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
  drivers/gpu/drm/Kconfig                       |   2 +
  drivers/gpu/drm/Makefile                      |   1 +
  drivers/gpu/drm/apu/Kconfig                   |  22 +
  drivers/gpu/drm/apu/Makefile                  |  10 +
  drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
  drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
  drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
  drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
  drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
  include/uapi/drm/apu_drm.h                    |  81 +++
Quoted 2 lines

"apu" seems too generic. We already have 3 "AI processing units" over in drivers/accel already...

Indeed, it is generic, but that's kind of the point for this driver since it's targetted at generalizing the interface with "AI processing units" on a growing number of embedded SoCs (ARM, RISC-V, etc.) In addition, the generic naming is intentional because the goal is bigger than the kernel and is working towards a generic, shared "libAPU" userspace[1], but also common firmware for DSP-style inference engines (e.g. analgous Sound Open Firmware for audio DSPs.)

As usual, the various SoC vendors use different names (APU, NPU, NN unit, etc.) but we'd like a generic name for the class of devices targetted by this driver. And unfortunately, it looks like the equally generic "Versatile processing unit" is already taken Intel's drivers/accel/ivpu. :)

Maybe since this is more about generalizing the interface between the CPU running linux and the APU, what about the name apu_if? But I guess that applies to the other 3 drivers in drivers/accell also. Hmmm...

Quoted 6 lines
Quoted 2 lines

Naming things is hard[2], so we're definitly open to other ideas. Any suggestions?

Maybe model it according to the tiny driver in drm display ? You can then call it tiny_apu :-)
Disclosure: It was Daniel's suggestion, he can chime in with more details on the tiny driver concept.

Yeah so maybe a bit more detail on my thoughts:

First this smells like a need bypass of the entire "we want open userspace for accel drivers" rule. The rule isn't quite a strict as for drm gpu drivers (not sure we ended up documenting exactly what, but iirc the consensus was that for build-time only dependencies we're ok with downstream compilers), but it's still there.

And at least from a quick look apu.ko and libapu just look like a generic accel interface, and that's not enough.

For the big training engines it's more or less "enough to run pytorch, but it can be really slow", not sure what the right standard for these inference-only drivers should be.

So that's the first reason why I don't like this.

The other is that I think if we do end up with a pile of tiny accel drivers, we should probably look into something like simmpledrm for the tiny display drivers. Probably still IP specific ioctls (at least most) so that IP specific job knows and all that are easy, but then just pass to a framework that simplifies a drm gem driver to "write ptes" and "run job" callback, maybe with an optional "create/destroy vm/ctx" for hw which can do that.

So maybe we end up with a drivers/accel/tiny and a bunch more helpers around the existing gem ones. The rule we have for drm/tiny is "1 file, less than 1kloc", and there's a bunch of them. I do think we can achieve the same for tiny accel inference engines (but it's still a bit a road). Maybe tiny accel is more like "less than 5kloc" since you need a bit more glue for the driver specific ioctl stuff - maybe that's only needed for the submit ioctl, maybe also for buffer map/unmap and creation.

Also note that there's an entire pile of in-flight work for adding new helpers to the gem world to make this all easier. Once we have gpuva and exec helpers there not much glue left to tie it all together with the scheduler.

But the real crux is that an accel inference driver really needs to have enough userspace to do an actual inference job with some android/cros/whatever framework for inference (there's just too many). -Daniel

Quoted 12 lines

Oded

Quoted 10 lines

Kevin

[1] https://gitlab.baylibre.com/baylibre/libapu/libapu

[2]
"There are 2 hard problems in computer science: cache invalidation,
naming things and off-by-1 errors."
-- https://twitter.com/secretGeek/status/7269997868

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#23 lore raw
Quoted 63 lines · Daniel Vetter
Quoted 55 lines · Oded Gabbay
Quoted 54 lines · Kevin Hilman
Quoted 10 lines · Jeffrey Hugo
Quoted 7 lines · Alexandre Bailon

This adds a DRM driver that implements communication between the CPU and an APU. The driver target embedded device that usually run inference using some prebuilt models. The goal is to provide common infrastructure that could be re-used to support many accelerators. Both kernel, userspace and firmware tries to use standard and existing to leverage the development and maintenance effort. The series implements two platform drivers, one for simulation and another one for the mt8183 (compatible with mt8365).

This looks like the 3 existing Accel drivers. Why is this in DRM?

Yes, this belongs in accel. I think Alex had some issues around the infra in accel with device nodes not appearing/opening properly, but I'll let him comment there. But either way, the right approach should be to fix any issues in accel and move it there.

[...]

Quoted 12 lines
Quoted 11 lines
   .../devicetree/bindings/gpu/mtk,apu-drm.yaml  |  38 ++
   drivers/gpu/drm/Kconfig                       |   2 +
   drivers/gpu/drm/Makefile                      |   1 +
   drivers/gpu/drm/apu/Kconfig                   |  22 +
   drivers/gpu/drm/apu/Makefile                  |  10 +
   drivers/gpu/drm/apu/apu_drv.c                 | 282 +++++++++
   drivers/gpu/drm/apu/apu_gem.c                 | 230 +++++++
   drivers/gpu/drm/apu/apu_internal.h            | 205 ++++++
   drivers/gpu/drm/apu/apu_sched.c               | 592 ++++++++++++++++++
   drivers/gpu/drm/apu/simu_apu.c                | 313 +++++++++
   include/uapi/drm/apu_drm.h                    |  81 +++
Quoted 2 lines

"apu" seems too generic. We already have 3 "AI processing units" over in drivers/accel already...

Indeed, it is generic, but that's kind of the point for this driver since it's targetted at generalizing the interface with "AI processing units" on a growing number of embedded SoCs (ARM, RISC-V, etc.) In addition, the generic naming is intentional because the goal is bigger than the kernel and is working towards a generic, shared "libAPU" userspace[1], but also common firmware for DSP-style inference engines (e.g. analgous Sound Open Firmware for audio DSPs.)

As usual, the various SoC vendors use different names (APU, NPU, NN unit, etc.) but we'd like a generic name for the class of devices targetted by this driver. And unfortunately, it looks like the equally generic "Versatile processing unit" is already taken Intel's drivers/accel/ivpu. :)

Maybe since this is more about generalizing the interface between the CPU running linux and the APU, what about the name apu_if? But I guess that applies to the other 3 drivers in drivers/accell also. Hmmm...

Quoted 6 lines
Quoted 2 lines

Naming things is hard[2], so we're definitly open to other ideas. Any suggestions?

Maybe model it according to the tiny driver in drm display ? You can then call it tiny_apu :-)
Disclosure: It was Daniel's suggestion, he can chime in with more details on the tiny driver concept.

Quoted 7 lines

Yeah so maybe a bit more detail on my thoughts:

First this smells like a need bypass of the entire "we want open userspace for accel drivers" rule. The rule isn't quite a strict as for drm gpu drivers (not sure we ended up documenting exactly what, but iirc the consensus was that for build-time only dependencies we're ok with downstream compilers), but it's still there.

What is letting you think that we want to bypass open source requirements ?
Although the neural network firmware and userspace application are not yet
opensource, our intention is to develop a full open source stack.
Currently, we only support Mediatek APU (an Xtensa VP6) and we have to use closed source sotfware to execute inferences on the accelerator. As far I know, there software stack similar to mesa where we could add
support of a new accelerator (this is also true for firmware).
That is actually what we would like to do. But this will take a lot of time and we consider this driver as a first (small) step.

Quoted 7 lines

And at least from a quick look apu.ko and libapu just look like a generic accel interface, and that's not enough.

For the big training engines it's more or less "enough to run pytorch, but it can be really slow", not sure what the right standard for these inference-only drivers should be.

To be honest, I don't know what would be required for training engines.
We only target accelerators for embedded device that usually only run inferences. In my opinion, this is 2 different use cases and I don't think we could address them in the same way.

Quoted 11 lines

So that's the first reason why I don't like this.

The other is that I think if we do end up with a pile of tiny accel drivers, we should probably look into something like simmpledrm for the tiny display drivers. Probably still IP specific ioctls (at least most) so that IP specific job knows and all that are easy, but then just pass to a framework that simplifies a drm gem driver to "write ptes" and "run job" callback, maybe with an optional "create/destroy vm/ctx" for hw which can do that.

Quoted 7 lines

So maybe we end up with a drivers/accel/tiny and a bunch more helpers around the existing gem ones. The rule we have for drm/tiny is "1 file, less than 1kloc", and there's a bunch of them. I do think we can achieve the same for tiny accel inference engines (but it's still a bit a road). Maybe tiny accel is more like "less than 5kloc" since you need a bit more glue for the driver specific ioctl stuff - maybe that's only needed for the submit ioctl, maybe also for buffer map/unmap and creation.

This makes sense to me.

Quoted 5 lines

Also note that there's an entire pile of in-flight work for adding new helpers to the gem world to make this all easier. Once we have gpuva and exec helpers there not much glue left to tie it all together with the scheduler.

I wrote this series a long time ago and just rebased it recently.
I will take some time to see the in-flight work and see if that something I could start using.

Quoted 4 lines

But the real crux is that an accel inference driver really needs to have enough userspace to do an actual inference job with some android/cros/whatever framework for inference (there's just too many).

We are currently stuck with closed source fimrware, userspace applications and toolchains (works with android and linux). We are looking for a solution but implementing something will take some time.

Alexandre

Quoted 15 lines

-Daniel

Quoted 12 lines

Oded

Quoted 10 lines

Kevin

[1] https://gitlab.baylibre.com/baylibre/libapu/libapu

[2]
"There are 2 hard problems in computer science: cache invalidation,
naming things and off-by-1 errors."
-- https://twitter.com/secretGeek/status/7269997868