diff mbox series

[15/16] drm/client: Hack: Add bootsplash example

Message ID 20190326175546.18126-16-noralf@tronnes.org (mailing list archive)
State New, archived
Headers show
Series drm/fb-helper: Move modesetting code to drm_client | expand

Commit Message

Noralf Trønnes March 26, 2019, 5:55 p.m. UTC
An example to showcase the client API.

TODO:
A bootsplash client needs a way to tell drm_fb_helper to stay away,
otherwise it will chime in on setup and hotplug.
Most DRM drivers register fbdev before calling drm_dev_register() (the
generic emulation is an exception). This have to be reversed for
bootsplash to fend off fbdev.

Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
---
 drivers/gpu/drm/Kconfig          |   5 +
 drivers/gpu/drm/Makefile         |   1 +
 drivers/gpu/drm/drm_bootsplash.c | 216 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_client.c     |   7 +
 drivers/gpu/drm/drm_drv.c        |   4 +
 include/drm/drm_client.h         |   3 +
 6 files changed, 236 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_bootsplash.c
diff mbox series

Patch

diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
index 5e1bc630b885..a7019c54e2a2 100644
--- a/drivers/gpu/drm/Kconfig
+++ b/drivers/gpu/drm/Kconfig
@@ -65,6 +65,11 @@  config DRM_DEBUG_SELFTEST
 
 	  If in doubt, say "N".
 
+config DRM_CLIENT_BOOTSPLASH
+	bool "DRM Bootsplash"
+	help
+	  DRM Bootsplash
+
 config DRM_KMS_HELPER
 	tristate
 	depends on DRM
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e630eccb951c..ac0573023842 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -32,6 +32,7 @@  drm-$(CONFIG_OF) += drm_of.o
 drm-$(CONFIG_AGP) += drm_agpsupport.o
 drm-$(CONFIG_DEBUG_FS) += drm_debugfs.o drm_debugfs_crc.o
 drm-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
+drm-$(CONFIG_DRM_CLIENT_BOOTSPLASH) += drm_bootsplash.o
 
 drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper.o \
 		drm_plane_helper.o drm_dp_mst_topology.o drm_atomic_helper.o \
diff --git a/drivers/gpu/drm/drm_bootsplash.c b/drivers/gpu/drm/drm_bootsplash.c
new file mode 100644
index 000000000000..fdc3349a2496
--- /dev/null
+++ b/drivers/gpu/drm/drm_bootsplash.c
@@ -0,0 +1,216 @@ 
+/* DRM internal client example */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/keyboard.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/workqueue.h>
+
+#include <drm/drm_client.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_fb_helper.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_modes.h>
+#include <drm/drm_print.h>
+
+// drm_lastclose()
+#include "drm_internal.h"
+
+static bool drm_bootsplash_enabled = true;
+module_param_named(bootsplash_enabled, drm_bootsplash_enabled, bool, 0600);
+MODULE_PARM_DESC(bootsplash_enabled, "Enable bootsplash client [default=true]");
+
+struct drm_bootsplash {
+	struct drm_client_dev client;
+	struct mutex lock;
+	struct work_struct worker;
+	bool started;
+	bool stop;
+};
+
+static bool drm_bootsplash_key_pressed;
+
+static int drm_bootsplash_keyboard_notifier_call(struct notifier_block *blk,
+						 unsigned long code, void *_param)
+{
+	/* Any key is good */
+	drm_bootsplash_key_pressed = true;
+
+	return NOTIFY_OK;
+}
+
+static struct notifier_block drm_bootsplash_keyboard_notifier_block = {
+	.notifier_call = drm_bootsplash_keyboard_notifier_call,
+};
+
+static u32 drm_bootsplash_color_table[3] = {
+	0x00ff0000, 0x0000ff00, 0x000000ff,
+};
+
+/* Draw a box with changing colors */
+static void drm_bootsplash_draw(struct drm_client_buffer *buffer, unsigned int sequence)
+{
+	unsigned int width = buffer->fb->width;
+	unsigned int height = buffer->fb->height;
+	unsigned int x, y;
+	u32 *pix;
+
+	pix = buffer->vaddr;
+	pix += ((height / 2) - 50) * width;
+	pix += (width / 2) - 50;
+
+	for (y = 0; y < 100; y++) {
+		for (x = 0; x < 100; x++)
+			*pix++ = drm_bootsplash_color_table[sequence];
+		pix += width - 100;
+	}
+}
+
+static void drm_bootsplash_worker(struct work_struct *work)
+{
+	struct drm_bootsplash *splash = container_of(work, struct drm_bootsplash, worker);
+	struct drm_client_dev *client = &splash->client;
+	struct drm_device *dev = client->dev;
+	struct drm_client_display *display;
+	unsigned int buffer_num = 0, sequence = 0, i;
+	bool stop;
+	int ret = 0;
+
+	while (!drm_bootsplash_key_pressed) {
+		mutex_lock(&splash->lock);
+		stop = splash->stop;
+		mutex_unlock(&splash->lock);
+		if (stop)
+			break;
+
+		buffer_num = !buffer_num;
+
+		mutex_lock(&client->displaylist_mutex);
+
+		ret = -ENOENT;
+
+		i = 0;
+		drm_client_for_each_display(display, client) {
+			DRM_DEBUG_KMS("draw: i=%u, buffer_num=%u, sequence=%u\n", i++, buffer_num, sequence);
+
+			drm_bootsplash_draw(display->buffers[buffer_num], sequence);
+
+			ret = drm_client_display_commit_buffer(display, buffer_num);
+			if (ret == -EBUSY)
+				break;
+
+		}
+		mutex_unlock(&client->displaylist_mutex);
+
+		if (ret == -ENOENT || ret == -EBUSY)
+			break;
+
+		if (++sequence == 3)
+			sequence = 0;
+
+		msleep(500);
+	}
+
+	/* Restore fbdev (or other) on key press. */
+	/* TODO: Check if it's OK to call drm_lastclose here. */
+	if (drm_bootsplash_key_pressed && !splash->stop)
+		drm_lastclose(dev);
+
+	drm_client_release_displays(client);
+
+	DRM_DEV_DEBUG_KMS(dev->dev, "Bootsplash has stopped (key=%u stop=%u, ret=%d).\n",
+			  drm_bootsplash_key_pressed, splash->stop, ret);
+}
+
+static int drm_bootsplash_client_hotplug(struct drm_client_dev *client)
+{
+	struct drm_bootsplash *splash = container_of(client, struct drm_bootsplash, client);
+	int ret;
+
+	if (drm_bootsplash_key_pressed)
+		return 0;
+
+	mutex_lock(&splash->lock);
+
+	ret = drm_client_probe_displays(client, 2, DRM_FORMAT_XRGB8888);
+	if (!ret)
+		ret = -ENOENT;
+	if (ret < 0)
+		goto out_unlock;
+
+	/*
+	 * TODO: Deal with rotated panels, might have to sw rotate
+	if (drm_client_panel_rotation(...))
+	 */
+
+	if (!splash->started) {
+		splash->started = true;
+		schedule_work(&splash->worker);
+	}
+
+out_unlock:
+	mutex_unlock(&splash->lock);
+
+	return ret;
+}
+
+static void drm_bootsplash_client_unregister(struct drm_client_dev *client)
+{
+	struct drm_bootsplash *splash = container_of(client, struct drm_bootsplash, client);
+
+	DRM_DEBUG_KMS("%s: IN\n", __func__);
+
+	mutex_lock(&splash->lock);
+	splash->stop = true;
+	mutex_unlock(&splash->lock);
+
+	flush_work(&splash->worker);
+
+	drm_client_release(client);
+	kfree(splash);
+
+	unregister_keyboard_notifier(&drm_bootsplash_keyboard_notifier_block);
+
+	DRM_DEBUG_KMS("%s: OUT\n", __func__);
+}
+
+static const struct drm_client_funcs drm_bootsplash_client_funcs = {
+	.owner		= THIS_MODULE,
+	.unregister	= drm_bootsplash_client_unregister,
+	.hotplug	= drm_bootsplash_client_hotplug,
+};
+
+void drm_bootsplash_client_register(struct drm_device *dev)
+{
+	struct drm_bootsplash *splash;
+	int ret;
+
+	if (!drm_bootsplash_enabled)
+		return;
+
+	splash = kzalloc(sizeof(*splash), GFP_KERNEL);
+	if (!splash)
+		return;
+
+	ret = drm_client_init(dev, &splash->client, "bootsplash", &drm_bootsplash_client_funcs);
+	if (ret) {
+		DRM_DEV_ERROR(dev->dev, "Failed to create client, ret=%d\n", ret);
+		kfree(splash);
+		return;
+	}
+
+	/* For this simple example only allow the first */
+	drm_bootsplash_enabled = false;
+
+	mutex_init(&splash->lock);
+
+	INIT_WORK(&splash->worker, drm_bootsplash_worker);
+
+	drm_client_add(&splash->client);
+
+	register_keyboard_notifier(&drm_bootsplash_keyboard_notifier_block);
+
+	drm_bootsplash_client_hotplug(&splash->client);
+}
diff --git a/drivers/gpu/drm/drm_client.c b/drivers/gpu/drm/drm_client.c
index ef01a31a9dbe..b7458fac0863 100644
--- a/drivers/gpu/drm/drm_client.c
+++ b/drivers/gpu/drm/drm_client.c
@@ -170,6 +170,13 @@  void drm_client_release(struct drm_client_dev *client)
 }
 EXPORT_SYMBOL(drm_client_release);
 
+void drm_client_dev_register(struct drm_device *dev)
+{
+#if CONFIG_DRM_CLIENT_BOOTSPLASH
+	drm_bootsplash_client_register(dev);
+#endif
+}
+
 void drm_client_dev_unregister(struct drm_device *dev)
 {
 	struct drm_client_dev *client, *tmp;
diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 50d849d1bc6e..fbbb5f998baf 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -1018,6 +1018,10 @@  int drm_dev_register(struct drm_device *dev, unsigned long flags)
 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
 out_unlock:
 	mutex_unlock(&drm_global_mutex);
+
+	if (!ret)
+		drm_client_dev_register(dev);
+
 	return ret;
 }
 EXPORT_SYMBOL(drm_dev_register);
diff --git a/include/drm/drm_client.h b/include/drm/drm_client.h
index ef7a9bd07b3c..cabd3055ba8a 100644
--- a/include/drm/drm_client.h
+++ b/include/drm/drm_client.h
@@ -130,6 +130,7 @@  int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 void drm_client_release(struct drm_client_dev *client);
 void drm_client_add(struct drm_client_dev *client);
 
+void drm_client_dev_register(struct drm_device *dev);
 void drm_client_dev_unregister(struct drm_device *dev);
 void drm_client_dev_hotplug(struct drm_device *dev);
 void drm_client_dev_restore(struct drm_device *dev);
@@ -251,4 +252,6 @@  int drm_client_display_commit_buffer(struct drm_client_display *display, unsigne
 
 int drm_client_debugfs_init(struct drm_minor *minor);
 
+void drm_bootsplash_client_register(struct drm_device *dev);
+
 #endif