Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Zimmermann <tzimmermann@suse.de>
To: simona@ffwll.ch, airlied@gmail.com, jfalempe@redhat.com,
	javierm@redhat.com
Cc: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org, nouveau@lists.freedesktop.org,
	Thomas Zimmermann <tzimmermann@suse.de>
Subject: [PATCH v4 03/80] drm/fbdev: Add memory-agnostic fbdev client
Date: Mon,  9 Sep 2024 13:30:09 +0200	[thread overview]
Message-ID: <20240909113633.595465-4-tzimmermann@suse.de> (raw)
In-Reply-To: <20240909113633.595465-1-tzimmermann@suse.de>

Add an fbdev client that can work with any memory manager. The
client implementation is the same as existing code in fbdev-dma or
fbdev-shmem.

Provide struct drm_driver.fbdev_probe for the new client to allocate
the surface GEM buffer. The new callback replaces fb_probe of struct
drm_fb_helper_funcs, which does the same.

To use the new client, DRM drivers set fbdev_probe in their struct
drm_driver instance and call drm_fbdev_client_setup(). Probing and
creating the fbdev surface buffer is now independent from the other
operations in struct drm_fb_helper. For the pixel format, the fbdev
client either uses a specified format, the value in preferred_depth
or 32-bit RGB.

v2:
- test for struct drm_fb_helper.funcs for NULL (Sui)
- respect struct drm_mode_config.preferred_depth for default format

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
---
 drivers/gpu/drm/Makefile           |   4 +-
 drivers/gpu/drm/drm_fb_helper.c    |  15 +--
 drivers/gpu/drm/drm_fbdev_client.c | 141 +++++++++++++++++++++++++++++
 include/drm/drm_drv.h              |  18 ++++
 include/drm/drm_fbdev_client.h     |  19 ++++
 5 files changed, 190 insertions(+), 7 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_fbdev_client.c
 create mode 100644 include/drm/drm_fbdev_client.h

diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index 784229d4504d..d7a684ae6a59 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -144,7 +144,9 @@ drm_kms_helper-y := \
 	drm_self_refresh_helper.o \
 	drm_simple_kms_helper.o
 drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
-drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
+drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += \
+	drm_fbdev_client.o \
+	drm_fb_helper.o
 obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
 
 #
diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index 550fa69311cb..d5e8994345bb 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -492,8 +492,8 @@ EXPORT_SYMBOL(drm_fb_helper_init);
  * @fb_helper: driver-allocated fbdev helper
  *
  * A helper to alloc fb_info and the member cmap. Called by the driver
- * within the fb_probe fb_helper callback function. Drivers do not
- * need to release the allocated fb_info structure themselves, this is
+ * within the struct &drm_driver.fbdev_probe callback function. Drivers do
+ * not need to release the allocated fb_info structure themselves, this is
  * automatically done when calling drm_fb_helper_fini().
  *
  * RETURNS:
@@ -1612,7 +1612,7 @@ static int drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper,
 
 /*
  * Allocates the backing storage and sets up the fbdev info structure through
- * the ->fb_probe callback.
+ * the ->fbdev_probe callback.
  */
 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
 {
@@ -1631,7 +1631,10 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
 	}
 
 	/* push down into drivers */
-	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
+	if (dev->driver->fbdev_probe)
+		ret = dev->driver->fbdev_probe(fb_helper, &sizes);
+	else if (fb_helper->funcs)
+		ret = fb_helper->funcs->fb_probe(fb_helper, &sizes);
 	if (ret < 0)
 		return ret;
 
@@ -1705,7 +1708,7 @@ static void drm_fb_helper_fill_var(struct fb_info *info,
  * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
  *
  * Drivers should call this (or their equivalent setup code) from their
- * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
+ * &drm_driver.fbdev_probe callback after having allocated the fbdev
  * backing storage framebuffer.
  */
 void drm_fb_helper_fill_info(struct fb_info *info,
@@ -1861,7 +1864,7 @@ __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
  * Note that this also registers the fbdev and so allows userspace to call into
  * the driver through the fbdev interfaces.
  *
- * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
+ * This function will call down into the &drm_driver.fbdev_probe callback
  * to let the driver allocate and initialize the fbdev info structure and the
  * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
  * as a helper to setup simple default values for the fbdev info structure.
diff --git a/drivers/gpu/drm/drm_fbdev_client.c b/drivers/gpu/drm/drm_fbdev_client.c
new file mode 100644
index 000000000000..a09382afe2fb
--- /dev/null
+++ b/drivers/gpu/drm/drm_fbdev_client.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: MIT
+
+#include <drm/drm_client.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_fbdev_client.h>
+#include <drm/drm_fb_helper.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_print.h>
+
+/*
+ * struct drm_client_funcs
+ */
+
+static void drm_fbdev_client_unregister(struct drm_client_dev *client)
+{
+	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
+
+	if (fb_helper->info) {
+		drm_fb_helper_unregister_info(fb_helper);
+	} else {
+		drm_client_release(&fb_helper->client);
+		drm_fb_helper_unprepare(fb_helper);
+		kfree(fb_helper);
+	}
+}
+
+static int drm_fbdev_client_restore(struct drm_client_dev *client)
+{
+	drm_fb_helper_lastclose(client->dev);
+
+	return 0;
+}
+
+static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
+{
+	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
+	struct drm_device *dev = client->dev;
+	int ret;
+
+	if (dev->fb_helper)
+		return drm_fb_helper_hotplug_event(dev->fb_helper);
+
+	ret = drm_fb_helper_init(dev, fb_helper);
+	if (ret)
+		goto err_drm_err;
+
+	if (!drm_drv_uses_atomic_modeset(dev))
+		drm_helper_disable_unused_functions(dev);
+
+	ret = drm_fb_helper_initial_config(fb_helper);
+	if (ret)
+		goto err_drm_fb_helper_fini;
+
+	return 0;
+
+err_drm_fb_helper_fini:
+	drm_fb_helper_fini(fb_helper);
+err_drm_err:
+	drm_err(dev, "fbdev: Failed to setup emulation (ret=%d)\n", ret);
+	return ret;
+}
+
+static const struct drm_client_funcs drm_fbdev_client_funcs = {
+	.owner		= THIS_MODULE,
+	.unregister	= drm_fbdev_client_unregister,
+	.restore	= drm_fbdev_client_restore,
+	.hotplug	= drm_fbdev_client_hotplug,
+};
+
+/**
+ * drm_fbdev_client_setup() - Setup fbdev emulation
+ * @dev: DRM device
+ * @format: Preferred color format for the device. DRM_FORMAT_XRGB8888
+ *          is used if this is zero.
+ *
+ * This function sets up fbdev emulation. Restore, hotplug events and
+ * teardown are all taken care of. Drivers that do suspend/resume need
+ * to call drm_fb_helper_set_suspend_unlocked() themselves. Simple
+ * drivers might use drm_mode_config_helper_suspend().
+ *
+ * This function is safe to call even when there are no connectors present.
+ * Setup will be retried on the next hotplug event.
+ *
+ * The fbdev client is destroyed by drm_dev_unregister().
+ *
+ * Returns:
+ * 0 on success, or a negative errno code otherwise.
+ */
+int drm_fbdev_client_setup(struct drm_device *dev, const struct drm_format_info *format)
+{
+	struct drm_fb_helper *fb_helper;
+	unsigned int color_mode;
+	int ret;
+
+	/* TODO: Use format info throughout DRM */
+	if (format) {
+		unsigned int bpp = drm_format_info_bpp(format, 0);
+
+		switch (bpp) {
+		case 16:
+			color_mode = format->depth; // could also be 15
+			break;
+		default:
+			color_mode = bpp;
+		}
+	} else {
+		switch (dev->mode_config.preferred_depth) {
+		case 0:
+		case 24:
+			color_mode = 32;
+			break;
+		default:
+			color_mode = dev->mode_config.preferred_depth;
+		}
+	}
+
+	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
+	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
+
+	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
+	if (!fb_helper)
+		return -ENOMEM;
+	drm_fb_helper_prepare(dev, fb_helper, color_mode, NULL);
+
+	ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
+	if (ret) {
+		drm_err(dev, "Failed to register client: %d\n", ret);
+		goto err_drm_client_init;
+	}
+
+	drm_client_register(&fb_helper->client);
+
+	return 0;
+
+err_drm_client_init:
+	drm_fb_helper_unprepare(fb_helper);
+	kfree(fb_helper);
+	return ret;
+}
+EXPORT_SYMBOL(drm_fbdev_client_setup);
diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
index 02ea4e3248fd..36a606af4ba1 100644
--- a/include/drm/drm_drv.h
+++ b/include/drm/drm_drv.h
@@ -34,6 +34,8 @@
 
 #include <drm/drm_device.h>
 
+struct drm_fb_helper;
+struct drm_fb_helper_surface_size;
 struct drm_file;
 struct drm_gem_object;
 struct drm_master;
@@ -366,6 +368,22 @@ struct drm_driver {
 			       struct drm_device *dev, uint32_t handle,
 			       uint64_t *offset);
 
+	/**
+	 * @fbdev_probe
+	 *
+	 * Allocates and initialize the fb_info structure for fbdev emulation.
+	 * Furthermore it also needs to allocate the DRM framebuffer used to
+	 * back the fbdev.
+	 *
+	 * This callback is mandatory for fbdev support.
+	 *
+	 * Returns:
+	 *
+	 * 0 on success ot a negative error code otherwise.
+	 */
+	int (*fbdev_probe)(struct drm_fb_helper *fbdev_helper,
+			   struct drm_fb_helper_surface_size *sizes);
+
 	/**
 	 * @show_fdinfo:
 	 *
diff --git a/include/drm/drm_fbdev_client.h b/include/drm/drm_fbdev_client.h
new file mode 100644
index 000000000000..e11a5614f127
--- /dev/null
+++ b/include/drm/drm_fbdev_client.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: MIT */
+
+#ifndef DRM_FBDEV_CLIENT_H
+#define DRM_FBDEV_CLIENT_H
+
+struct drm_device;
+struct drm_format_info;
+
+#ifdef CONFIG_DRM_FBDEV_EMULATION
+int drm_fbdev_client_setup(struct drm_device *dev, const struct drm_format_info *format);
+#else
+static inline int drm_fbdev_client_setup(struct drm_device *dev,
+					 const struct drm_format_info *format)
+{
+	return 0;
+}
+#endif
+
+#endif
-- 
2.46.0


  parent reply	other threads:[~2024-09-09 11:37 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-09 11:30 [PATCH v4 00/80] drm: Provide client setup helper and convert drivers Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 01/80] drm/fbdev-helper: Move color-mode lookup into 4CC format helper Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 02/80] drm/fbdev-helper: Set and clear VGA switcheroo client from fb_info Thomas Zimmermann
2024-09-09 11:30 ` Thomas Zimmermann [this message]
2024-09-09 11:30 ` [PATCH v4 04/80] drm: Add client-agnostic setup helper Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 05/80] drm/fbdev-dma: Support struct drm_driver.fbdev_probe Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 06/80] drm/arcgpu: Run DRM default client setup Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 07/80] drm/arm/komeda: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 08/80] drm/arm/hdlcd: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 09/80] drm/arm/malidp: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 10/80] drm/aspeed: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 11/80] drm/atmel-hdlcd: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 12/80] drm/fsl-dcu: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 13/80] drm/hisilicon/kirin: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 14/80] drm/hx8357d: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 15/80] drm/ili9163: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 16/80] drm/ili9225: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 17/80] drm/ili9341: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 18/80] drm/ili9486: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 19/80] drm/imx/dcss: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 20/80] drm/imx/ipuv3: " Thomas Zimmermann
2024-09-10  8:30   ` Philipp Zabel
2024-09-09 11:30 ` [PATCH v4 21/80] drm/imx/lcdc: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 22/80] drm/ingenic: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 23/80] drm/kmb: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 24/80] drm/logicvc: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 25/80] drm/mcde: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 26/80] drm/mediatek: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 27/80] drm/meson: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 28/80] drm/mi0283qt: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 29/80] drm/mxsfb/lcdif: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 30/80] drm/mxsfb: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 31/80] drm/panel-mipi-dbi: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 32/80] drm/pl111: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 33/80] drm/renesas/rcar-du: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 34/80] drm/renesas/rz-du: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 35/80] drm/renesas/shmobile: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 36/80] drm/repaper: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 37/80] drm/rockchip: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 38/80] drm/sti: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 39/80] drm/stm: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 40/80] drm/sun4i: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 41/80] drm/tidss: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 42/80] drm/tilcdc: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 43/80] drm/st7586: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 44/80] drm/st7735r: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 45/80] drm/tve200: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 46/80] drm/vc4: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 47/80] drm/xlnx: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 48/80] drm/fbdev-dma: Remove obsolete setup function Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 49/80] drm/fbdev-shmem: Support struct drm_driver.fbdev_probe Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 50/80] drm/ast: Run DRM default client setup Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 51/80] drm/bochs: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 52/80] drm/cirrus: " Thomas Zimmermann
2024-09-09 11:30 ` [PATCH v4 53/80] drm/gm12u320: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 54/80] drm/gud: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 55/80] drm/hyperv_drm: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 56/80] drm/mgag200: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 57/80] drm/ofdrm: Use " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 58/80] drm/simpledrm: Run " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 59/80] drm/solomon: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 60/80] drm/udl: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 61/80] drm/virtgpu: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 62/80] drm/vkms: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 63/80] drm/fbdev-shmem: Remove obsolete setup function Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 64/80] drm/fbdev-ttm: Support struct drm_driver.fbdev_probe Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 65/80] drm/amdgpu: Run DRM default client setup Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 66/80] drm/hisilicon/hibmc: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 67/80] drm/loongson: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 68/80] drm/nouveau: " Thomas Zimmermann
2024-09-12 18:04   ` Lyude Paul
2024-09-13  6:23     ` Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 69/80] drm/qxl: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 70/80] drm/vboxvideo: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 71/80] drm/vmwgfx: " Thomas Zimmermann
2024-09-09 17:52   ` Zack Rusin
2024-09-13  6:28     ` Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 72/80] drm/fbdev-ttm: Remove obsolete setup function Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 73/80] drm/armada: Run DRM default client setup Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 74/80] drm/exynos-drm: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 75/80] drm/gma500: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 76/80] drm/msm: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 77/80] drm/radeon: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 78/80] drm/tegra: " Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 79/80] drm/omapdrm: Remove struct drm_fb_helper from struct omap_fbdev Thomas Zimmermann
2024-09-09 11:31 ` [PATCH v4 80/80] drm/omapdrm: Run DRM default client setup Thomas Zimmermann
2024-09-09 15:43 ` ✗ Fi.CI.CHECKPATCH: warning for drm: Provide client setup helper and convert drivers (rev4) Patchwork
2024-09-09 15:43 ` ✗ Fi.CI.SPARSE: " Patchwork
2024-09-09 16:06 ` ✓ Fi.CI.BAT: success " Patchwork
2024-09-10 19:52 ` ✗ Fi.CI.IGT: failure " Patchwork

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=20240909113633.595465-4-tzimmermann@suse.de \
    --to=tzimmermann@suse.de \
    --cc=airlied@gmail.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=javierm@redhat.com \
    --cc=jfalempe@redhat.com \
    --cc=nouveau@lists.freedesktop.org \
    --cc=simona@ffwll.ch \
    /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