From patchwork Fri Aug 26 00:00:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 9300691 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 3588F607D8 for ; Fri, 26 Aug 2016 00:01:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 24FA729298 for ; Fri, 26 Aug 2016 00:01:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 19428292AB; Fri, 26 Aug 2016 00:01:16 +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 0948E292A8 for ; Fri, 26 Aug 2016 00:01:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 986BA6EA4F; Fri, 26 Aug 2016 00:01:13 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id E86566EA4F for ; Fri, 26 Aug 2016 00:01:11 +0000 (UTC) Received: by mail-wm0-x242.google.com with SMTP id q128so9326686wma.1 for ; Thu, 25 Aug 2016 17:01:11 -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; bh=lIOmJRcHlwhEM53tipNVSaCNRBQo9wcwN17b9XWEXZ0=; b=mtDUIaS6MIV19J7qCgY8vU+v7rmA0//ZFKerY4emkRAlHoU+heI1XTGM+e8VOdviXW 7c5+AFTScGk5ctbdy47R6nytZyU+QE5k1/EjGGci7THnq8L8r+OlHJqsd783pGUDgNah pKk1UWQ+dx5vRn4iKZMDp1/6+oLBN4eOM80aEsFwqeiabHNIwW3DSkg+Obq5NWx9InbG vyZKDN3e1jfDsvUL6TmOetV7gkPPZ6zw3lpAPzo7qNCZF0NpLWVJOlUFJ821IA+toGHA mx1NeGdCUGZtSOd0V2nmP+tW98ONRgTCeRVHPE+SYRmtUFWOTCoc/UhC6ufAj1+5UwUS 8aHw== 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; bh=lIOmJRcHlwhEM53tipNVSaCNRBQo9wcwN17b9XWEXZ0=; b=b3eqSVn0yMVPpMYMWiuAx2raA0WWSuKtjprOla20PRT01ntaecWKjJOkVYjdKKqki+ cgbxafduAZm2XprQmpCp3fMpLFgK8UKMILIIk9RrvbcUJaz2FWwzWExBWQTwYaO8QKbp WuyueqCriLRlPPCSojgfy7wRy7LIuF06aPoEcZiK5u6AvLNcKP0IVFVBa5OUyGTbygVX Dkq4aR5BL9rD3AcwqX5uieGAjSD0zY33Z1+UxcXrd4/N8wmIHFbRlhWAW9Se2xfTN2fm asciroeLKpo5Oirdi8t0FF8JytSCL4X+MA18wQDAHxVKu/50tymNHNkWULtiFFk7iXeD Pg2g== X-Gm-Message-State: AEkoouswv5Cq3x8WH7UXzFfWWmU8dPFXu43SiLDd1o9fmw8qSRcSgQPe/phh1IdkMmGYIg== X-Received: by 10.28.24.5 with SMTP id 5mr32865284wmy.6.1472169670329; Thu, 25 Aug 2016 17:01:10 -0700 (PDT) Received: from localhost.localdomain ([37.120.99.16]) by smtp.gmail.com with ESMTPSA id lv9sm17375480wjb.22.2016.08.25.17.01.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Aug 2016 17:01:09 -0700 (PDT) From: David Herrmann To: dri-devel@lists.freedesktop.org Subject: [RFC] drm: implement generic firmware eviction Date: Fri, 26 Aug 2016 02:00:56 +0200 Message-Id: <20160826000056.12806-1-dh.herrmann@gmail.com> X-Mailer: git-send-email 2.9.3 Cc: devicetree@vger.kernel.org, Daniel Vetter , Rob Herring , Hans de Goede , Maxime Ripard 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 Provide a generic DRM helper that evicts all conflicting firmware framebuffers, devices, and drivers. The new helper is called drm_evict_firmware(), and takes a flagset controlling which firmware to kick out. This is meant to be used by drivers in their ->probe() callbacks of their parent bus, before calling into drm_dev_register(). Signed-off-by: David Herrmann --- Hey This is just compile-tested for now. I just want to get some comments on the design. I decided to drop the sysfb infrastructure and rather just use this generic helper. It keeps things simple and should work just fine for all reasonable use-cases. This will work with SimpleDRM out-of-the-box on x86. Architectures with dynamic simple-framebuffer devices are not supported yet. I actually have no idea what the strategy there is? Can the DeviceTree people come up with something? Am I supposed to call of_platform_depopulate()? Or of_detach_node()? Or what? Looking at drivers/of/{platform,dynamic}.c, I cannot see how this is supposed to work at all. Who protects platform_device_del() from being called in parallel? Also: Does any platform make use the of 'display:' entry in 'simple-framebuffer' DT nodes? If yes, how do I get access to it? And why don't vc4/sun4i make use of it, rather than falling back to remove_conflicting_framebuffers()? Thanks David drivers/gpu/drm/drm_drv.c | 257 ++++++++++++++++++++++++++++++++++++++++++++++ include/drm/drmP.h | 13 +++ 2 files changed, 270 insertions(+) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 0773547..581c342 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -26,12 +26,16 @@ * DEALINGS IN THE SOFTWARE. */ +#include #include +#include #include #include #include #include +#include #include +#include #include #include "drm_crtc_internal.h" #include "drm_legacy.h" @@ -881,6 +885,259 @@ void drm_dev_unregister(struct drm_device *dev) } EXPORT_SYMBOL(drm_dev_unregister); +struct drm_evict_ctx { + struct apertures_struct *ap; + unsigned int flags; +}; + +static bool drm_evict_match_resource(struct drm_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 & DRM_EVICT_VBE) && mem->start == 0xA0000) + return true; + } + + return false; +} + +static int drm_evict_platform_device(struct device *dev, void *userdata) +{ + struct drm_evict_ctx *ctx = userdata; + struct platform_device *pdev = to_platform_device(dev); + struct resource *mem; + + /* + * We are only interested in static devices here (i.e., they carry + * information via a statically allocated platform data field). Any + * dynamically allocated devices have separate ownership models and + * must be handled via their respective management calls. + */ + if (!dev_get_platdata(&pdev->dev)) + return 0; + if (!pdev->name) + return 0; + + if (!strcmp(pdev->name, "simple-framebuffer")) { + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!mem) + return 0; + if (!drm_evict_match_resource(ctx, mem)) + return 0; + + platform_device_del(pdev); + } + + return 0; +} + +static int drm_evict_platform(struct drm_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 + * DRM 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. We + * must make sure our eviction callback leaves any non-static platform + * devices untouched, though. + * + * XXX: ARM and other OF users should really make sure to add their own + * eviction code here, possibly using the 'display' DT-handle as + * defined by the 'simple-framebuffer' OF-node. + */ + mutex_lock(&lock); + ret = bus_for_each_dev(&platform_bus_type, NULL, ctx, + drm_evict_platform_device); + mutex_unlock(&lock); + return ret; +} + +static int drm_evict_fbdev(struct drm_evict_ctx *ctx) +{ + /* + * We have 2 different cases where fbdev drivers can operate on the + * same devices as DRM drivers: + * + * o Two drivers exist for the same device, one DRM driver and one + * fbdev driver (e.g., nvidiafb and nouveau). In those cases, both + * drivers bind to the same real hw-device and driver core takes + * care of any conflicts. + * + * o A generic fbdev driver uses some pseudo-device to provide + * early-boot graphics access or some other kind of generic display + * driver. At the same time, a DRM driver exists that can run the + * real hardware (e.g., vesafb/efifb and i915). + * + * If possible, driver core takes care of any conflicts and prevents + * two drivers from being bound to the same device. However, in some + * cases this is not possible. We then use + * remove_conflicting_framebuffers() to match the apertures of the DRM + * device against all existing fbdev devices, evicting any conflicting + * fbdev drivers. Additionally, if requested, any VBE driver is evicted + * as well. + * + * Note that this breaks horribly if an fbdev driver is probed after a + * DRM driver. So make sure whenever such conflicts are possible, the + * fbdev drivers must be probed in early boot. If you cannot guarantee + * that, you better make sure that the driver core prevents both + * drivers from being probed in parallel. + */ + +#if !defined(CONFIG_FB) + return 0; +#else + return remove_conflicting_framebuffers(ctx->ap, "drm", + !!(ctx->flags & DRM_EVICT_VBE)); +#endif +} + +static int drm_evict_vgacon(void) +{ + /* + * The VGACON console driver pokes at VGA registers randomly. If a DRM + * 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 DRM 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 DRM drivers, this + * will fail horribly. You better make sure VGACON is probed early and + * DRM drivers are probed as normal modules. + */ + +#if !defined(CONFIG_VGA_CONSOLE) + return 0; +#elif !defined(CONFIG_DUMMY_CONSOLE) +# warning "Dummy console is disabled, but required to kick out VGACON" + return -ENODEV; +#else + int ret = 0; + + DRM_INFO("Replacing VGA console driver\n"); + + 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(); + + return ret; +#endif +} + +/** + * drm_evict_firmware - remove any conflicting firmware-framebuffers + * @ap: apertures claimed by driver, or NULL + * @flags: eviction flags + * + * This function evicts any conflicting firmware-framebuffers and their bound + * drivers, according to the data given as @ap and @flags. + * + * Depending on @flags, the following operations are performed: + * + * DRM_EVICT_FBDEV: Any fbdev drivers that overlap @ap are unloaded. + * Furthermore, if DRM_EVICT_VBE is given as well, any fbdev + * driver that maps the VGA region is unloaded. + * + * DRM_EVICT_PLATFORM: Firmware framebuffer platform devices (eg., + * 'simple-framebuffer') that overlap @ap are removed + * from the system, causing drivers to be unbound. + * If DRM_EVICT_VBE is given, this also affects devices + * that map the VGA region. + * + * DRM_EVICT_VGACON: The vgacon console driver is unbound and unregistered. + * + * The caller must provide their apertures as @ap. If it is NULL, an empty set + * is assumed, except if DRM_EVICT_ANYWHERE is given, in which case a fake + * aperture across the entire address range is used. + * + * RETURNS: + * 0 on success, negative error code on failure. + */ +int drm_evict_firmware(struct apertures_struct *ap, unsigned int flags) +{ + struct drm_evict_ctx ctx; + struct apertures_struct *allocated_ap = NULL; + int ret; + + WARN_ON(ap && (flags & DRM_EVICT_ANYWHERE)); + + if (!ap) { + allocated_ap = alloc_apertures(!!(flags & DRM_EVICT_ANYWHERE)); + if (!allocated_ap) + return -ENOMEM; + + ap = allocated_ap; + + if (flags & DRM_EVICT_ANYWHERE) { + ap->ranges[0].base = 0; + ap->ranges[0].size = ~0; + } + } + + ctx.ap = ap; + ctx.flags = flags; + + if (flags & DRM_EVICT_PLATFORM) { + ret = drm_evict_platform(&ctx); + if (ret < 0) + return ret; + } + + if (flags & DRM_EVICT_FBDEV) { + ret = drm_evict_fbdev(&ctx); + if (ret < 0) + return ret; + } + + if (flags & DRM_EVICT_VGACON) { + ret = drm_evict_vgacon(); + if (ret < 0) + return ret; + } + + kfree(allocated_ap); + return 0; +} + /* * DRM Core * The DRM core module initializes all global DRM objects and makes them diff --git a/include/drm/drmP.h b/include/drm/drmP.h index 2197ab1..6c8bcf5 100644 --- a/include/drm/drmP.h +++ b/include/drm/drmP.h @@ -88,6 +88,7 @@ struct drm_gem_object; struct drm_master; struct drm_vblank_crtc; +struct apertures_struct; struct device_node; struct videomode; struct reservation_object; @@ -971,6 +972,18 @@ void drm_dev_unref(struct drm_device *dev); int drm_dev_register(struct drm_device *dev, unsigned long flags); void drm_dev_unregister(struct drm_device *dev); +enum { + DRM_EVICT_FBDEV = (1U << 0), + DRM_EVICT_PLATFORM = (1U << 1), + DRM_EVICT_VGACON = (1U << 2), + DRM_EVICT_VBE = (1U << 3), + DRM_EVICT_ANYWHERE = (1U << 4), + + DRM_EVICT_DEFAULT = DRM_EVICT_FBDEV, +}; + +int drm_evict_firmware(struct apertures_struct *ap, unsigned int flags); + struct drm_minor *drm_minor_acquire(unsigned int minor_id); void drm_minor_release(struct drm_minor *minor);