linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Alexandre Bailon <abailon@baylibre.com>
To: airlied@gmail.com, daniel@ffwll.ch,
	maarten.lankhorst@linux.intel.com, mripard@kernel.org,
	tzimmermann@suse.de
Cc: robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org,
	conor+dt@kernel.org, matthias.bgg@gmail.com,
	angelogioacchino.delregno@collabora.com, sumit.semwal@linaro.org,
	christian.koenig@amd.com, jstephan@baylibre.com,
	dri-devel@lists.freedesktop.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org, linux-media@vger.kernel.org,
	linaro-mm-sig@lists.linaro.org, khilman@baylibre.com,
	nbelin@baylibre.com, bero@baylibre.com
Subject: [PATCH 6/7] drm/apu: Add support for a simulated APU
Date: Wed, 17 May 2023 16:52:36 +0200	[thread overview]
Message-ID: <20230517145237.295461-7-abailon@baylibre.com> (raw)
In-Reply-To: <20230517145237.295461-1-abailon@baylibre.com>

From: Julien Stephan <jstephan@baylibre.com>

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.

Signed-off-by: Julien Stephan <jstephan@baylibre.com>
---
 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

diff --git a/drivers/gpu/drm/apu/Kconfig b/drivers/gpu/drm/apu/Kconfig
index a769df42091c..e0ffc166497c 100644
--- a/drivers/gpu/drm/apu/Kconfig
+++ b/drivers/gpu/drm/apu/Kconfig
@@ -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.
diff --git a/drivers/gpu/drm/apu/Makefile b/drivers/gpu/drm/apu/Makefile
index fc8d6380fc38..0b007854a07f 100644
--- a/drivers/gpu/drm/apu/Makefile
+++ b/drivers/gpu/drm/apu/Makefile
@@ -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
diff --git a/drivers/gpu/drm/apu/simu_apu.c b/drivers/gpu/drm/apu/simu_apu.c
new file mode 100644
index 000000000000..5557f8b78a83
--- /dev/null
+++ b/drivers/gpu/drm/apu/simu_apu.c
@@ -0,0 +1,313 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Copyright 2023 BayLibre SAS
+
+#include <linux/module.h>
+#include <linux/netlink.h>
+#include <linux/platform_device.h>
+#include <linux/skbuff.h>
+
+#include <net/sock.h>
+
+#include <drm/apu_drm.h>
+
+#include "apu_internal.h"
+
+
+#define MYPROTO 17
+#define MYGRP 17
+
+#define DRIVER_NAME "SIMU APU DRIVER"
+
+/*
+ * Firmware request, must be aligned with the one defined in firmware.
+ * @id: Request id, used in the case of reply, to find the pending request
+ * @cmd: The command id to execute in the firmware
+ * @result: The result of the command executed on the firmware
+ * @size: The size of the data available in this request
+ * @count: The number of shared buffer
+ * @data: Contains the data attached with the request if size is greater than
+ *	zero, and the addresses of shared buffers if count is greater than
+ *	zero. Both the data and the shared buffer could be read and write
+ *	by the APU.
+ */
+struct  apu_dev_request {
+	u16 id;
+	u16 cmd;
+	u16 result;
+	u16 size_in;
+	u16 size_out;
+	u16 count;
+	u8 data[0];
+} __packed;
+
+struct platform_device *platform;
+struct apu_core *apu_core;
+static int pid = -1;
+struct sock *nl_sock;
+
+static int apu_netlink_read(struct sk_buff *skb, struct apu_dev_request **msg_ptr, int *pid)
+{
+	struct nlmsghdr *nlh;
+
+	nlh = (struct nlmsghdr *)skb->data;
+	*pid = nlh->nlmsg_pid; /* pid of sending process */
+	*msg_ptr = nlmsg_data(nlh);
+
+	return nlh->nlmsg_len - NLMSG_HDRLEN;
+}
+
+static int apu_netlink_write(void *msg_ptr, int msg_size, int pid)
+{
+	struct sk_buff *skb_out;
+	struct nlmsghdr *nlh;
+	int res;
+
+	skb_out = nlmsg_new(msg_size, 0);
+	if (!skb_out)
+		return -ENOMEM;
+
+	nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0);
+	NETLINK_CB(skb_out).dst_group = 0; /* not in multicast group */
+	memcpy(nlmsg_data(nlh), msg_ptr, msg_size);
+
+	res = nlmsg_unicast(nl_sock, skb_out, pid);
+
+	if (res < 0)
+		return res;
+	else
+		return nlh->nlmsg_len - NLMSG_HDRLEN;
+}
+
+static void netlink_recv_msg(struct sk_buff *skb)
+{
+	int msg_size;
+	struct apu_dev_request *hdr;
+	int nlmsg_pid;
+
+	msg_size = apu_netlink_read(skb, &hdr, &nlmsg_pid);
+
+	if (pid == -1) {
+		// No device registered yet, the first message should be
+		// "READY"
+		if (!strncmp((char *)hdr, "READY", strlen("READY"))) {
+
+			pid = nlmsg_pid;
+			if (apu_core_register(&platform->dev, apu_core, apu_core->apu))
+				pr_err("cannot register SIMU APU\n");
+		}
+	} else if (pid == nlmsg_pid) {
+		if (!strncmp((char *)hdr, "STOP", strlen("STOP"))) {
+			pid = -1;
+			apu_core_remove(apu_core);
+		} else
+			apu_drm_callback(apu_core, hdr->id, hdr, msg_size);
+	} else {
+		pr_err("%s: Only one core is supported for now\n", DRIVER_NAME);
+	}
+}
+
+static int netlink_setup(void)
+{
+	int ret = 0;
+	struct netlink_kernel_cfg cfg = {
+		.input = netlink_recv_msg,
+	};
+
+	nl_sock = netlink_kernel_create(&init_net, MYPROTO, &cfg);
+	if (!nl_sock)
+		ret = -ENOMEM;
+
+	return ret;
+}
+
+static int simu_apu_send(struct apu_job *job)
+{
+	return apu_netlink_write((void *)(job->request_data), job->request_len, pid);
+}
+
+static int simu_apu_handle_request(struct apu_job *job, void *data, int len)
+{
+	struct apu_dev_request *hdr = data;
+
+	job->result = hdr->result;
+	if (job->size_out)
+		memcpy(job->data_out, hdr->data + job->size_in,
+			min(job->size_out, hdr->size_out));
+	job->size_out = hdr->size_out;
+	return 0;
+}
+
+static int simu_apu_alloc_request(struct apu_job *job)
+{
+	struct apu_dev_request *dev_req;
+
+	int size;
+	u64 *dev_req_da;
+	u32 *dev_req_buffer_size;
+	int i;
+
+	size = sizeof(*dev_req) + (sizeof(u64) + sizeof(u32)) * job->bo_count * 2 +
+		job->size_in + job->size_out;
+	dev_req = kmalloc(size, GFP_KERNEL);
+	if (!dev_req)
+		return -ENOMEM;
+
+	dev_req->cmd = job->cmd;
+	dev_req->size_in = job->size_in;
+	dev_req->size_out = job->size_out;
+	dev_req->count = job->bo_count;
+	dev_req_da =
+	    (u64 *) (dev_req->data + dev_req->size_in + dev_req->size_out);
+	dev_req_buffer_size = (u32 *) (dev_req_da + dev_req->count);
+	memcpy(dev_req->data, job->data_in, job->size_in);
+
+	for (i = 0; i < job->bo_count; i++) {
+		struct apu_gem_object *obj = to_apu_bo(job->bos[i]);
+
+		dev_req_da[i] = drm_vma_node_offset_addr(&obj->base.base.vma_node);
+		dev_req_buffer_size[i] = obj->size;
+	}
+
+	dev_req->id = job->id;
+
+	job->request_data = dev_req;
+	job->request_len = size;
+	return 0;
+}
+
+static int simu_apu_ready(struct apu_core *core)
+{
+	if (pid == -1)
+		return 0;
+
+	return 1;
+}
+
+/**
+ * simu_apu_gem_mmap
+ *
+ * this is directly based on drm_gem_mmap() function but removing the permission
+ * check before mapping a buffer. This is useful here to be able to easily
+ * share buffers between libapu host application and libapu device application
+ * (simulation use case)
+ *
+ */
+static int simu_apu_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 drm_gem_object *obj = NULL;
+	struct drm_vma_offset_node *node;
+	int ret;
+
+	if (drm_dev_is_unplugged(dev))
+		return -ENODEV;
+
+	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
+	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
+						  vma->vm_pgoff,
+						  vma_pages(vma));
+	if (likely(node)) {
+		obj = container_of(node, struct drm_gem_object, vma_node);
+		/*
+		 * When the object is being freed, after it hits 0-refcnt it
+		 * proceeds to tear down the object. In the process it will
+		 * attempt to remove the VMA offset and so acquire this
+		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
+		 * that matches our range, we know it is in the process of being
+		 * destroyed and will be freed as soon as we release the lock -
+		 * so we have to check for the 0-refcnted object and treat it as
+		 * invalid.
+		 */
+		if (!kref_get_unless_zero(&obj->refcount)) {
+			obj = NULL;
+			pr_err("DTC: %s: %d\n", __func__, __LINE__);
+		}
+	}
+	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
+
+	if (!obj)
+		return -EINVAL;
+
+	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
+			       vma);
+
+	drm_gem_object_put(obj);
+
+	return ret;
+}
+
+static struct apu_core_ops simu_apu_ops = {
+	.alloc_prepare_request = simu_apu_alloc_request,
+	.send_request = simu_apu_send,
+	.handle_request = simu_apu_handle_request,
+	.is_ready = simu_apu_ready,
+};
+
+static int __init apu_platform_init(void)
+{
+	int ret;
+	struct apu_drm *apu;
+
+	platform = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
+	if (IS_ERR(platform))
+		return PTR_ERR(platform);
+
+	if (!devres_open_group(&platform->dev, NULL, GFP_KERNEL)) {
+		ret = -ENOMEM;
+		goto out_unregister;
+	}
+
+	apu = apu_dev_alloc(&platform->dev);
+	if (!apu) {
+		ret = -ENOMEM;
+		goto out_devres;
+	}
+
+	apu_core = apu_core_alloc(apu, &simu_apu_ops, apu);
+	if (!apu_core) {
+		ret = -ENOMEM;
+		goto out_devres;
+	}
+
+	ret = apu_dev_register(apu);
+	if (ret)
+		goto out_apu_core_free;
+
+	apu->mmap = simu_apu_gem_mmap;
+
+	ret = netlink_setup();
+	if (ret)
+		goto out_apu_dev_unregister;
+
+	return 0;
+
+out_apu_dev_unregister:
+	apu_dev_unregister(apu);
+out_apu_core_free:
+	apu_core_free(apu_core);
+out_devres:
+	devres_release_group(&platform->dev, NULL);
+out_unregister:
+	platform_device_unregister(platform);
+	return ret;
+}
+
+static void __exit apu_platform_exit(void)
+{
+	netlink_kernel_release(nl_sock);
+	apu_core_remove(apu_core);
+	apu_core_free(apu_core);
+	apu_dev_unregister((struct apu_drm *)apu_core->apu);
+	devres_release_group(&platform->dev, NULL);
+	platform_device_unregister(platform);
+}
+
+
+module_init(apu_platform_init);
+module_exit(apu_platform_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Julien Stephan");
+MODULE_DESCRIPTION(DRIVER_NAME);
-- 
2.39.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2023-05-17 15:40 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 14:52 [PATCH 0/7] Add a DRM driver to support AI Processing Unit (APU) Alexandre Bailon
2023-05-17 14:52 ` [PATCH 1/7] drm: Add support of AI Processor " Alexandre Bailon
2023-05-17 14:52 ` [PATCH 2/7] drm/apu: Add memory allocator Alexandre Bailon
2023-05-17 14:52 ` [PATCH 3/7] drm/apu: Add support of requests Alexandre Bailon
2023-05-17 14:52 ` [PATCH 4/7] drm/apu: Add support of IOMMU Alexandre Bailon
2023-05-18 13:24   ` Robin Murphy
2023-05-17 14:52 ` [PATCH 5/7] drm/apu: allow platform driver to implement their own mmap function Alexandre Bailon
2023-05-17 19:45   ` Krzysztof Kozlowski
2023-05-26 15:08     ` Alexandre Bailon
2023-05-17 14:52 ` Alexandre Bailon [this message]
2023-05-17 14:52 ` [PATCH 7/7] dt-bindings: Add bidings for mtk,apu-drm Alexandre Bailon
2023-05-17 15:04   ` AngeloGioacchino Del Regno
2023-05-17 17:28     ` Conor Dooley
2023-05-22  8:53     ` Alexandre Bailon
2023-05-17 15:28   ` Rob Herring
2023-05-17 16:53   ` Krzysztof Kozlowski
2023-05-17 19:38   ` Krzysztof Kozlowski
2023-05-17 19:41     ` Krzysztof Kozlowski
2023-05-17 15:05 ` [PATCH 0/7] Add a DRM driver to support AI Processing Unit (APU) Thomas Zimmermann
2023-05-17 15:12 ` Jeffrey Hugo
2023-05-23 23:34   ` Kevin Hilman
2023-05-24 10:27     ` Oded Gabbay
2023-05-24 10:40       ` Daniel Vetter
2023-05-26 15:45         ` Alexandre Bailon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230517145237.295461-7-abailon@baylibre.com \
    --to=abailon@baylibre.com \
    --cc=airlied@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=bero@baylibre.com \
    --cc=christian.koenig@amd.com \
    --cc=conor+dt@kernel.org \
    --cc=daniel@ffwll.ch \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jstephan@baylibre.com \
    --cc=khilman@baylibre.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthias.bgg@gmail.com \
    --cc=mripard@kernel.org \
    --cc=nbelin@baylibre.com \
    --cc=robh+dt@kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).