From patchwork Fri Sep 2 08:22:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 9310555 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 37AB560756 for ; Fri, 2 Sep 2016 08:23:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 287382970C for ; Fri, 2 Sep 2016 08:23:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D4282970E; Fri, 2 Sep 2016 08:23:26 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 4AE0F2970C for ; Fri, 2 Sep 2016 08:23:25 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CC3B86EA30; Fri, 2 Sep 2016 08:23:23 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 99FD46EA30 for ; Fri, 2 Sep 2016 08:23:22 +0000 (UTC) Received: by mail-wm0-x244.google.com with SMTP id w207so1850198wmw.0 for ; Fri, 02 Sep 2016 01:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1c9zd5+VtW+Ecdfyru0ltQSvV3ZUlIJ3KT2ZOzJxEmk=; b=nAdGDIHjt40y91wHBkxmKIFyi3usfDkLpMsecTdI3KFAO6Ui6SF9lPInvqYY2DSd/X oHKFSOtBr3p191GiMru3pTEDus4V1/wRGu9KVKoXl3SGZbmaZWHMItbP4FrOLYXhNrsH jEgQMxAWAk4PCLOH4VzJ20a8/OaELsHTRu1l16shfNaawP9w/C7EgNoSXp5BZFY7oT66 opVofd4x6pFtKY5NoWoiZGHVVWv5wgnpqEGXWTANq5hUD1W2FXAYoW8bkZiliQToyW87 DKOpwJl9D9Iv+WJLE64F+MJkcOqC3iHC0Q4jch29O6kvU+IvKHPddYTYPGgv2HPpUlTn trzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1c9zd5+VtW+Ecdfyru0ltQSvV3ZUlIJ3KT2ZOzJxEmk=; b=WdUkbxbgBd0fkK9lNSuqg4b5pXozqTAF8ErGghum2mRNX7SARqWrrODQQVEUrNDXxb il7nByWw81UE2fuwt3pOG5Gr5F2pc6ok2pTeeTJcbtmU6jnhIxGD0bfp2iJE0qJpNMj0 SfKwtHw0keQcgayM7E0dybYn+EUZF8zJ5KdyYa4mT1JPfddJNX5WWSxpwxs4Ofn7VwlK OUSF+HTdWgkwq92TrAKMq5mwJzH8mluwhsK7AoENACnrByccRLbEIjdjz2wAjoSWJOHX Bd+zQ4mbK+DC7KEwvwgVtFmMABed+i4AiWLCmEllUEwOoK2y522JKmdC/1CrmBVfe4Zc 9l+w== X-Gm-Message-State: AE9vXwN448OmeniM5KXavfTVomvoUJtVyqQlD7Lroczf5AXqZdnkRI+ZQ1coyIPNKuI1/A== X-Received: by 10.194.77.97 with SMTP id r1mr18031856wjw.83.1472804601130; Fri, 02 Sep 2016 01:23:21 -0700 (PDT) Received: from localhost.localdomain ([37.120.93.201]) by smtp.gmail.com with ESMTPSA id gg10sm9325662wjd.4.2016.09.02.01.23.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Sep 2016 01:23:20 -0700 (PDT) From: David Herrmann To: dri-devel@lists.freedesktop.org Subject: [PATCH v5 4/7] video: add generic framebuffer eviction Date: Fri, 2 Sep 2016 10:22:42 +0200 Message-Id: <20160902082245.7119-5-dh.herrmann@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20160902082245.7119-1-dh.herrmann@gmail.com> References: <20160902082245.7119-1-dh.herrmann@gmail.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP There are several situations where we want hardware handover from an early boot GFX driver (e.g., vgacon, vesafb, efifb, simplefb) to a full fletched GFX driver (e.g., most DRM drivers). So far, we relied on remove_conflicting_framebuffers() to do this for us, however, this had a bunch of downsides: o It only removes conflicting fbdev drivers. It does not drop vgacon, early boot console drivers, conflicting DRM drivers, etc. o It only unloads the fbdev driver, it does not modify the underlying device or resources. In case of "screen_info" drivers (e.g., efifb) this is fine, since no resources are pinned. However, if the driver binds to a platform-device like "simple-framebuffer", we must make sure to unregister that device as well. Otherwise, pinned resources like IORESOURCE_MEM stay around, triggering WARN_ONs if the following driver requests those resources. o It is only available if CONFIG_FB is selected. This commit adds a new infrastructure that manages system-framebuffers (short: sysfb). The initial commit provides conflict-resolution for system-framebuffers. At its core it provides sysfb_evict_conflicts(), which implements conflict detection and removal for all known types of GFX driver hand-overs. So far, this includes platform-device removal, fbdev-firmware-device removal, vgacon removal and VBE detection. To further simplify the callers, it also provides helpers to figure out what hand-over to do, based on the device the new drivers binds to: o PCI drivers can use sysfb_evict_conflicts_pci(), which will figure out the apertures automatically, and does VGA/VBE detection. o Generic firmware drivers that might be shadowed at any address in memory can use sysfb_evict_conflicts_firmware(), basically removing *all* firmware framebuffers in effect. This only adds the generic sysfb helpers. No users are converted, yet. Signed-off-by: David Herrmann Reviewed-by: Tom Gundersen --- drivers/video/Kconfig | 4 + drivers/video/Makefile | 1 + drivers/video/sysfb.c | 327 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/sysfb.h | 34 +++++ 4 files changed, 366 insertions(+) create mode 100644 drivers/video/sysfb.c create mode 100644 include/linux/sysfb.h diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 3c20af9..56a8294 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -36,6 +36,10 @@ config VIDEOMODE_HELPERS config HDMI bool +config SYSFB + bool + select DUMMY_CONSOLE if VT + if VT source "drivers/video/console/Kconfig" endif diff --git a/drivers/video/Makefile b/drivers/video/Makefile index 9ad3c17..df7bd75 100644 --- a/drivers/video/Makefile +++ b/drivers/video/Makefile @@ -1,5 +1,6 @@ obj-$(CONFIG_VGASTATE) += vgastate.o obj-$(CONFIG_HDMI) += hdmi.o +obj-$(CONFIG_SYSFB) += sysfb.o obj-$(CONFIG_VT) += console/ obj-$(CONFIG_LOGO) += logo/ diff --git a/drivers/video/sysfb.c b/drivers/video/sysfb.c new file mode 100644 index 0000000..00585c9 --- /dev/null +++ b/drivers/video/sysfb.c @@ -0,0 +1,327 @@ +/* + * Copyright (C) 2013-2016 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + */ + +#define pr_fmt(fmt) "sysfb: " fmt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool sysfb_evict_match_resource(struct sysfb_evict_ctx *ctx, + struct resource *mem) +{ + struct aperture *g; + unsigned int i; + + for (i = 0; i < ctx->ap->count; ++i) { + g = &ctx->ap->ranges[i]; + + if (mem->start == g->base) + return true; + if (mem->start >= g->base && mem->end < g->base + g->size) + return true; + if ((ctx->flags & SYSFB_EVICT_VBE) && mem->start == 0xA0000) + return true; + } + + return false; +} + +static int sysfb_evict_platform_device(struct device *dev, void *userdata) +{ + struct sysfb_evict_ctx *ctx = userdata; + struct platform_device *pdev = to_platform_device(dev); + struct resource *mem; + + if (!pdev->name) + return 0; + + if (!strcmp(pdev->name, "simple-framebuffer")) { + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!mem) + return 0; + if (!sysfb_evict_match_resource(ctx, mem)) + return 0; + +#ifdef CONFIG_OF_ADDRESS + if (dev->of_node) + of_platform_device_destroy(dev); + else +#endif + if (dev_get_platdata(&pdev->dev)) + platform_device_del(pdev); + } + + return 0; +} + +static int sysfb_evict_platform(struct sysfb_evict_ctx *ctx) +{ + /* + * Early-boot architecture setup and boot-loarders sometimes create + * preliminary platform devices with a generic framebuffer setup. This + * allows graphics access during boot-up, without a real graphics + * driver loaded. However, once a real graphics driver takes over, we + * have to destroy those platform devices. In the legacy fbdev case, we + * just used to unload the fbdev driver. However, to make sure any kind + * of driver is unloaded, the platform-eviction code here simply + * removes any conflicting platform device directly. This causes any + * bound driver to be detached, and then removes the device entirely so + * it cannot be bound to later on. + * + * Please note that any such platform device must be registered by + * early architecture setup code. If they are registered after regular + * GFX drivers, this will fail horribly. + */ + + static DEFINE_MUTEX(lock); + int ret; + + /* + * In case of static platform-devices, we must iterate the bus and + * remove them manually. We know that we're the only code that might + * remove them, so a simple static lock serializes all calls here. + */ + mutex_lock(&lock); + ret = bus_for_each_dev(&platform_bus_type, NULL, ctx, + sysfb_evict_platform_device); + mutex_unlock(&lock); + return ret; +} + +static int sysfb_evict_fbdev(struct sysfb_evict_ctx *ctx) +{ + /* + * Usually, evicting platform devices should be enough to also trigger + * fbdev unloading. However, some legacy devices (e.g., uvesafb) have + * no platform devices that can be evicted, so we still fall back to + * the legacy fbdev removal code. Note that this only removes fbdev + * devices marked as FBINFO_MISC_FIRMWARE. Anything else is left + * untouched. + * + * As usual, this only works if the fbdev device is probed early, + * before any real GFX driver wants to take over. + */ + + int ret = 0; + +#ifdef CONFIG_FB + ret = remove_conflicting_framebuffers(ctx->ap, "sysfb", + ctx->flags & SYSFB_EVICT_VBE); +#endif + + return ret; +} + +static int sysfb_evict_vgacon(struct sysfb_evict_ctx *ctx) +{ + /* + * The VGACON console driver pokes at VGA registers randomly. If a GFX + * driver cannot keep the VGA support alive, it better makes sure to + * unload VGACON before probing. + * + * Unloading VGACON requires us to first force dummycon to take over + * from vgacon (but only if vgacon is really in use), followed by a + * deregistration of vgacon. Note that this prevents vgacon from being + * used again after the GFX driver is unloaded. But that is usually + * fine, since VGA state is rarely restored on driver-unload, anyway. + * + * Note that we rely on VGACON to be probed in early boot (actually + * done by ARCH setup code). If it is probed after GFX drivers, this + * will fail horribly. You better make sure VGACON is probed early and + * GFX drivers are probed as normal modules. + */ + + int ret = 0; + +#ifdef CONFIG_VGA_CONSOLE + console_lock(); + if (con_is_bound(&vga_con)) + ret = do_take_over_console(&dummy_con, 0, + MAX_NR_CONSOLES - 1, 1); + if (ret == 0) { + ret = do_unregister_con_driver(&vga_con); + if (ret == -ENODEV) /* ignore "already unregistered" */ + ret = 0; + } + console_unlock(); +#endif + + return ret; +} + +/** + * sysfb_evict_conflicts - remove any conflicting system-framebuffers + * @ctx: eviction context + * + * This function evicts any conflicting system-framebuffers and their bound + * drivers, according to the data given in @ctx. + * + * Depending on @ctx->flags, the following operations are performed: + * + * SYSFB_EVICT_PLATFORM: Firmware framebuffer platform devices (eg., + * 'simple-framebuffer') that overlap @ctx are removed + * from the system, causing drivers to be unbound. + * If SYSFB_EVICT_VBE is given, this also affects + * devices that own the VGA region. + * + * SYSFB_EVICT_FBDEV: Any firmware fbdev drivers that overlap @ctx are + * unloaded. + * Furthermore, if SYSFB_EVICT_VBE is given as well, any + * fbdev driver that maps the VGA region is unloaded. + * + * SYSFB_EVICT_VGACON: The vgacon console driver is unbound and unregistered. + * + * This might call into fbdev driver unregistration, or even device_del() on + * some buses. Hence, make sure you call this from your top-level + * probe-callbacks, rather than with any gfx-subsystem locks held. + * + * RETURNS: + * 0 on success, negative error code on failure. + */ +int sysfb_evict_conflicts(struct sysfb_evict_ctx *ctx) +{ + int ret; + + if (WARN_ON(!ctx || !ctx->ap)) + return -EINVAL; + + pr_info("removing conflicts (sysfb%s%s%s%s)\n", + (ctx->flags & SYSFB_EVICT_PLATFORM) ? ", platform" : "", + (ctx->flags & SYSFB_EVICT_FBDEV) ? ", fbdev" : "", + (ctx->flags & SYSFB_EVICT_VGACON) ? ", vgacon" : "", + (ctx->flags & SYSFB_EVICT_VBE) ? ", vbe" : ""); + + if (ctx->flags & SYSFB_EVICT_PLATFORM) { + ret = sysfb_evict_platform(ctx); + if (ret < 0) + return ret; + } + + if (ctx->flags & SYSFB_EVICT_FBDEV) { + ret = sysfb_evict_fbdev(ctx); + if (ret < 0) + return ret; + } + + if (ctx->flags & SYSFB_EVICT_VGACON) { + ret = sysfb_evict_vgacon(ctx); + if (ret < 0) + return ret; + } + + return 0; +} +EXPORT_SYMBOL(sysfb_evict_conflicts); + +/** + * sysfb_evict_conflicts_firmware() - remove all firmware framebuffers + * + * This is similar to sysfb_evict_conflicts() but uses a fake aperture spanning + * the entire address-space. This is suitable for any GFX driver that just + * wants to get rid of all available firmware framebuffers. + * + * RETURNS: + * 0 on success, negative error code on failure. + */ +int sysfb_evict_conflicts_firmware(void) +{ + struct sysfb_evict_ctx ctx = {}; + int ret; + + ctx.ap = alloc_apertures(1); + if (!ctx.ap) + return -ENOMEM; + + ctx.ap->ranges[0].base = 0; + ctx.ap->ranges[0].size = ~0; + + ctx.flags |= SYSFB_EVICT_FBDEV | SYSFB_EVICT_PLATFORM; + ret = sysfb_evict_conflicts(&ctx); + + kfree(ctx.ap); + return ret; +} +EXPORT_SYMBOL(sysfb_evict_conflicts_firmware); + +#ifdef CONFIG_PCI +/** + * sysfb_evict_conflicts_pci() - remove all system framebuffers conflicting + * with the given pci device + * @pdev: pci device + * + * This is similar to sysfb_evict_conflicts() but generates the eviction + * context based on the given pci device @pdev. + * + * RETURNS: + * 0 on success, negative error code on failure. + */ +int sysfb_evict_conflicts_pci(struct pci_dev *pdev) +{ + struct sysfb_evict_ctx ctx = {}; + size_t i, n, offset; + int ret; + + /* + * If this device is used as primary VGA device, it is shadowed at the + * VBE base address, so make sure to include it in the apertures. + */ + if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) + ctx.flags |= SYSFB_EVICT_VBE; + + /* + * If a device is a VGA device, make sure to kick out vgacon. We cannot + * rely on the IORESOURCE_ROM_SHADOW, since vgacon can switch between + * vga devices at runtime. So kick out vgacon anyway. + */ + if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) + ctx.flags |= SYSFB_EVICT_VGACON; + + /* + * Allocate apertures for all standard PCI resources. Skip them in case + * they are empty. + */ + ctx.ap = alloc_apertures(PCI_STD_RESOURCE_END - PCI_STD_RESOURCES + 1); + if (!ctx.ap) + return -ENOMEM; + + offset = PCI_STD_RESOURCES; + for (n = 0, i = 0; i < ctx.ap->count; ++i) { + if (pci_resource_len(pdev, offset + i) < 1) + continue; + + ctx.ap->ranges[n].base = pci_resource_start(pdev, offset + i); + ctx.ap->ranges[n].size = pci_resource_len(pdev, offset + i); + ++n; + } + ctx.ap->count = n; + + /* + * Evict all matching fbdev devices, VBE devices if they shadow this + * device, vgacon if this is a vga device, and platform devices if they + * match. + */ + ctx.flags |= SYSFB_EVICT_FBDEV | SYSFB_EVICT_PLATFORM; + ret = sysfb_evict_conflicts(&ctx); + + kfree(ctx.ap); + return ret; +} +EXPORT_SYMBOL(sysfb_evict_conflicts_pci); +#endif /* CONFIG_PCI */ diff --git a/include/linux/sysfb.h b/include/linux/sysfb.h new file mode 100644 index 0000000..b67c74a --- /dev/null +++ b/include/linux/sysfb.h @@ -0,0 +1,34 @@ +#ifndef __LINUX_SYSFB_H +#define __LINUX_SYSFB_H + +/* + * Copyright (C) 2013-2016 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + */ + +#include + +struct apertures_struct; +struct pci_dev; + +enum { + SYSFB_EVICT_PLATFORM = (1U << 0), + SYSFB_EVICT_FBDEV = (1U << 1), + SYSFB_EVICT_VGACON = (1U << 2), + SYSFB_EVICT_VBE = (1U << 3), +}; + +struct sysfb_evict_ctx { + struct apertures_struct *ap; + unsigned int flags; +}; + +int sysfb_evict_conflicts(struct sysfb_evict_ctx *ctx); +int sysfb_evict_conflicts_firmware(void); +int sysfb_evict_conflicts_pci(struct pci_dev *pdev); + +#endif /* __LINUX_SYSFB_H */