From patchwork Sat Apr 20 10:45:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Noralf_Tr=C3=B8nnes?= X-Patchwork-Id: 10910201 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5E5E31515 for ; Sat, 20 Apr 2019 10:47:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E9A328961 for ; Sat, 20 Apr 2019 10:47:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 42FEA28D3F; Sat, 20 Apr 2019 10:47:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 60DD128905 for ; Sat, 20 Apr 2019 10:47:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7619A89189; Sat, 20 Apr 2019 10:47:20 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from smtp.domeneshop.no (smtp.domeneshop.no [IPv6:2a01:5b40:0:3005::1]) by gabe.freedesktop.org (Postfix) with ESMTPS id 43ED589189; Sat, 20 Apr 2019 10:47:19 +0000 (UTC) Received: from 211.81-166-168.customer.lyse.net ([81.166.168.211]:49438 helo=localhost.localdomain) by smtp.domeneshop.no with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.84_2) (envelope-from ) id 1hHnVZ-000820-T3; Sat, 20 Apr 2019 12:46:09 +0200 From: =?utf-8?q?Noralf_Tr=C3=B8nnes?= To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 11/11] drm/client: Hack: Add bootsplash example Date: Sat, 20 Apr 2019 12:45:56 +0200 Message-Id: <20190420104556.31127-12-noralf@tronnes.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190420104556.31127-1-noralf@tronnes.org> References: <20190420104556.31127-1-noralf@tronnes.org> MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tronnes.org; s=ds201810; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=vM4eUgZjLOP5vgzyGb8kcfbSeWhnSsuD4ACkFYh661w=; b=YxdjeprjBtP+Ek1fvREu87AZ6A+DMy54NMOFEFMm8VSYPd4vMbMx+3x6fT0T1kY1pY+78PnvTnzeX/SpymmVyjUHPdwAdpFee1wK8ACPSRqY++4uJXpl7grHFq4Qroj6g8LhioHfHvfDJGLZX5veldbTvT6GXdntVQeXq3eMBk6+5++ez0ZdfKuthZEA5HlE20LBWeuU8b56jxPiSGvxHwiugipv4/i8cTRIh55X6nIIC09S9i3G/otb0mUPY9m5C3yt9ISyzYZYSSgrEI166M6YxbHg0Qa3e8nTLf1ouzjYuF7/sOFhTlvLs8dqNpttJOYczDwBtqAc+jSYQ7rBpg==; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: maxime.ripard@bootlin.com, daniel.vetter@ffwll.ch, intel-gfx@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- drivers/gpu/drm/Kconfig | 5 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_bootsplash.c | 362 +++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_client.c | 7 + drivers/gpu/drm/drm_drv.c | 4 + include/drm/drm_client.h | 3 + 6 files changed, 382 insertions(+) create mode 100644 drivers/gpu/drm/drm_bootsplash.c diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig index 39d5f7562f1c..23678a2f36be 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 228ac9e3b645..be25caee734a 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..caf7d4813eae --- /dev/null +++ b/drivers/gpu/drm/drm_bootsplash.c @@ -0,0 +1,362 @@ +/* DRM internal client example */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +// 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; + + unsigned int modeset_mask; + struct drm_client_buffer *buffers[2]; +}; + +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 void drm_bootsplash_buffer_delete(struct drm_bootsplash *splash) +{ + unsigned int i; + + for (i = 0; i < 2; i++) { + if (!IS_ERR_OR_NULL(splash->buffers[i])) + drm_client_framebuffer_delete(splash->buffers[i]); + splash->buffers[i] = NULL; + } +} + +static int drm_bootsplash_buffer_create(struct drm_bootsplash *splash, u32 width, u32 height) +{ + unsigned int i; + + for (i = 0; i < 2; i++) { + splash->buffers[i] = drm_client_framebuffer_create(&splash->client, width, height, DRM_FORMAT_XRGB8888); + if (IS_ERR(splash->buffers[i])) { + drm_bootsplash_buffer_delete(splash); + return PTR_ERR(splash->buffers[i]); + } + } + + return 0; +} + +static int drm_bootsplash_display_probe(struct drm_bootsplash *splash) +{ + struct drm_client_dev *client = &splash->client; + unsigned int width = 0, height = 0; + unsigned int num_non_tiled = 0, i; + struct drm_mode_set *modeset; + bool tiled = false; + int ret; + + splash->modeset_mask = 0; + + ret = drm_client_modeset_probe(client, 0, 0); + if (ret) + return ret; + + mutex_lock(&client->modeset_mutex); + + drm_client_for_each_modeset(modeset, client) { + if (!modeset->mode) + continue; + + if (modeset->connectors[0]->has_tile) + tiled = true; + else + num_non_tiled++; + } + + if (!tiled && !num_non_tiled) { + drm_bootsplash_buffer_delete(splash); + ret = -ENOENT; + goto out; + } + + /* Assume only one tiled monitor is possible */ + if (tiled) { + int hdisplay = 0, vdisplay = 0; + + i = 0; + drm_client_for_each_modeset(modeset, client) { + i++; + if (!modeset->connectors[0]->has_tile) + continue; + + if (!modeset->y) + hdisplay += modeset->mode->hdisplay; + if (!modeset->x) + vdisplay += modeset->mode->vdisplay; + splash->modeset_mask |= BIT(i - 1); + } + + width = hdisplay; + height = vdisplay; + + goto trim; + } + + /* The rest have one display per modeset, pick the largest */ + i = 0; + drm_client_for_each_modeset(modeset, client) { + i++; + if (!modeset->mode || modeset->connectors[0]->has_tile) + continue; + + if (modeset->mode->hdisplay * modeset->mode->vdisplay > width * height) { + width = modeset->mode->hdisplay; + height = modeset->mode->vdisplay; + splash->modeset_mask = BIT(i - 1); + } + } + +trim: + /* Remove unused modesets (warning in __drm_atomic_helper_set_config()) */ + i = 0; + drm_client_for_each_modeset(modeset, client) { + unsigned int j; + + if (splash->modeset_mask & BIT(i++)) + continue; + + drm_mode_destroy(client->dev, modeset->mode); + modeset->mode = NULL; + + for (j = 0; j < modeset->num_connectors; j++) { + drm_connector_put(modeset->connectors[j]); + modeset->connectors[j] = NULL; + } + modeset->num_connectors = 0; + } + + if (!splash->buffers[0] || + splash->buffers[0]->fb->width != width || splash->buffers[0]->fb->height != height) { + drm_bootsplash_buffer_delete(splash); + ret = drm_bootsplash_buffer_create(splash, width, height); + } + +out: + mutex_unlock(&client->modeset_mutex); + + return ret; +} + +static int drm_bootsplash_display_commit_buffer(struct drm_bootsplash *splash, unsigned int num) +{ + struct drm_client_dev *client = &splash->client; + struct drm_mode_set *modeset; + unsigned int i = 0; + + mutex_lock(&client->modeset_mutex); + drm_client_for_each_modeset(modeset, client) { + if (splash->modeset_mask & BIT(i++)) + modeset->fb = splash->buffers[num]->fb; + } + mutex_unlock(&client->modeset_mutex); + + return drm_client_modeset_commit(client); +} + +static u32 drm_bootsplash_color_table[3] = { + 0x00ff0000, 0x0000ff00, 0x000000ff, +}; + +/* Draw a box with changing colors */ +static void drm_bootsplash_draw_box(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 int drm_bootsplash_draw(struct drm_bootsplash *splash, unsigned int sequence, unsigned int buffer_num) +{ + if (!splash->buffers[buffer_num]) + return -ENOENT; + + DRM_DEBUG_KMS("draw: buffer_num=%u, sequence=%u\n", buffer_num, sequence); + + drm_bootsplash_draw_box(splash->buffers[buffer_num], sequence); + + return drm_bootsplash_display_commit_buffer(splash, buffer_num); +} + +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; + unsigned int buffer_num = 0, sequence = 0; + bool stop = false; + int ret = 0; + + while (!drm_bootsplash_key_pressed) { + mutex_lock(&splash->lock); + + stop = splash->stop; + + buffer_num = !buffer_num; + + ret = drm_bootsplash_draw(splash, sequence, buffer_num); + + mutex_unlock(&splash->lock); + + if (stop || 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_bootsplash_buffer_delete(splash); + + 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 = 0; + + if (drm_bootsplash_key_pressed) + return 0; + + mutex_lock(&splash->lock); + + if (splash->stop) + goto out_unlock; + + ret = drm_bootsplash_display_probe(splash); + if (ret < 0) { + if (splash->started && ret == -ENOENT) + splash->stop = true; + 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); + + register_keyboard_notifier(&drm_bootsplash_keyboard_notifier_block); + + drm_bootsplash_client_hotplug(&splash->client); + + drm_client_register(&splash->client); +} diff --git a/drivers/gpu/drm/drm_client.c b/drivers/gpu/drm/drm_client.c index 3dd08c6b264d..f8b554b0a6b1 100644 --- a/drivers/gpu/drm/drm_client.c +++ b/drivers/gpu/drm/drm_client.c @@ -161,6 +161,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 f2d5ed745733..e9eb770093fb 100644 --- a/include/drm/drm_client.h +++ b/include/drm/drm_client.h @@ -107,6 +107,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_register(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); @@ -183,4 +184,6 @@ int drm_client_modeset_dpms(struct drm_client_dev *client, int mode); int drm_client_debugfs_init(struct drm_minor *minor); +void drm_bootsplash_client_register(struct drm_device *dev); + #endif