From patchwork Fri Sep 1 07:27:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 9935423 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 29C3E6021C for ; Sat, 2 Sep 2017 00:30:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B7792852A for ; Sat, 2 Sep 2017 00:30:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1021A28550; Sat, 2 Sep 2017 00:30:14 +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_SIGNED, 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 5CE7C28543 for ; Sat, 2 Sep 2017 00:30:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D249D6E902; Sat, 2 Sep 2017 00:28:57 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pg0-x242.google.com (mail-pg0-x242.google.com [IPv6:2607:f8b0:400e:c05::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 56A836E7E4 for ; Fri, 1 Sep 2017 07:28:33 +0000 (UTC) Received: by mail-pg0-x242.google.com with SMTP id 83so1239964pgb.3 for ; Fri, 01 Sep 2017 00:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wnD4PPKKYv/V53yhQhN0UzLmztzhzz/GAnd2khbo4L8=; b=ByoVjlt3ySIGfavIGSU2wdm95L+g9XCtiJou+7cMIlFr/nV9K/7smV7uuI3wtZ1dRa 9FXxW8OZ2mzowpEQRfEB/TlyV7Ps2Ta5IE9RWJ6J21O0gkb/Prbt7/mX+fUUKw/g1j7X HN/k2IDrr4pLPWzOtRiuODoxOzLi8ZzVY1oY4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wnD4PPKKYv/V53yhQhN0UzLmztzhzz/GAnd2khbo4L8=; b=COQWbEn85bWuNgPOMBL73mGONr7/I8RcvMLtV+GtMTc7Q/h18gAV+S0PbA1w8TLqZe fa3PBjBzDBtyJU5Jllalado+lqe3/eYPXqupWikcSZt4S0Dz5Vhg42JfLGhcRhbwHH3S x7o2XHl9oKkTCfTxaOiowXAbkhkRW4xUllVTY5u41+B7fb7kVkGyrscJ413w3MOhqC6y /X8HSnc47Zp+S2kqxXmcmR2D9oSKWvTnS1E+gm3ZoPvuxslwNmDFiSXwo+UYSuZHRoG7 VAUlzAFKZiumFnInWnEUYqiCe90Tuyn+snlm+EEl5p+ijDEr2J+lgMsEwoTezuNlicPL 3ZSw== X-Gm-Message-State: AHPjjUj5kSaf/Pb+oTXSa+1W1R+u311NfKZmd3UcSyUCn1z4VOqLckzv u7IzCqpkl//EXHoN X-Google-Smtp-Source: ADKCNb5G47z8NOzuV/WeooAYxaoGWu6SXt4g8gRYioqvc5CB27cgtFx6+P+cIgy9IkCwU5+OzPvTsg== X-Received: by 10.98.59.11 with SMTP id i11mr1242701pfa.237.1504250912740; Fri, 01 Sep 2017 00:28:32 -0700 (PDT) Received: from localhost.localdomain (124-171-202-56.dyn.iinet.net.au. [124.171.202.56]) by smtp.gmail.com with ESMTPSA id l30sm2430300pgc.61.2017.09.01.00.28.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Sep 2017 00:28:32 -0700 (PDT) From: Daniel Axtens To: linux-pci@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 2/3] drm: add fallback default device detection Date: Fri, 1 Sep 2017 17:27:43 +1000 Message-Id: <20170901072744.2409-3-dja@axtens.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170901072744.2409-1-dja@axtens.net> References: <20170901072744.2409-1-dja@axtens.net> X-Mailman-Approved-At: Sat, 02 Sep 2017 00:28:54 +0000 Cc: lorenzo.pieralisi@arm.com, gabriele.paoloni@huawei.com, ard.biesheuvel@linaro.org, will.deacon@arm.com, dri-devel@lists.freedesktop.org, z.liuxinliang@hisilicon.com, alex.williamson@redhat.com, helgaas@kernel.org, catalin.marinas@arm.com, zourongrong@gmail.com, daniel.vetter@intel.com, Daniel Axtens 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 The VGA arbiter selects a default VGA device that is enabled and reachable via the legacy VGA resources (mem 0xa0000-0xbffff, io 0x3b0-0x3bb, io 0x3c0-0x3df, etc). (As a special case for x86 and IA64, this can be overridden by EFI.) If there is no such device, e.g., because there's no enabled VGA device, the host bridge doesn't support access to those legacy resources, or a PCI-PCI bridge doesn't have VGA Enable set, a platform may select an arbitrary device by calling pci_set_default_display(). powerpc does this, for example. If there is also no platform hook, there will be no default device nominated. This is not necessarily what we want. Add handling for devices that aren't handled by the vga arbiter or platform by adding a late initcall and a class enable hook. If there is no default from vgaarb or the platform then the first VGA card that is enabled, has a driver bound, and can decode memory or I/O will be marked as default. This means single-card setups on systems without access to legacy areas and without arch hooks will work. Multi-card setups on these systems will nominate an arbitrary device, rather than no devices. Signed-off-by: Daniel Axtens Tested-by: Andrew Donnellan --- v3: Split out from re-organisation for simplicity. Add better description and better documentaion. Thanks to (in no particular order), Daniel Vetter, Lorenzo Pieralisi, Ard Biesheuvel and Dave Airlie. Special thanks to Ben Herrenschmidt and Bjorn Helgass, whose prose I have borrowed. v1: Tested on: - x86_64 laptop - arm64 D05 board with hibmc card - qemu powerpc with tcg and bochs std-vga I know this adds another config option and that's a bit sad, but we can't include it unconditionally as it depends on PCI. Suggestions welcome. --- drivers/gpu/vga/default_display.c | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/drivers/gpu/vga/default_display.c b/drivers/gpu/vga/default_display.c index 99e4723360da..b8e4a5af38e8 100644 --- a/drivers/gpu/vga/default_display.c +++ b/drivers/gpu/vga/default_display.c @@ -42,6 +42,10 @@ * * 2) Anything specified by an arch hook, * e.g. arch/powerpc/kernel/pci-common.c::fixup_vga() + * + * 3) If neither of those, then we still want to pick something. For + * now, pick the first PCI VGA device with a driver bound and with + * memory or I/O control on. */ #include @@ -53,6 +57,12 @@ static struct pci_dev *vga_default; +/* + * only go active after the late initcall so as not to interfere with + * the arbiter + */ +static bool vga_default_active = false; + /** * pci_default_display - return the default display device * @@ -84,3 +94,70 @@ void pci_set_default_display(struct pci_dev *pdev) pci_dev_put(vga_default); vga_default = pci_dev_get(pdev); } + +static bool vga_default_try_device(struct pci_dev *pdev) +{ + u16 cmd; + + /* Only deal with VGA class devices */ + if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) + return false; + + /* Only deal with devices with drivers bound */ + if (!pdev->driver) + return false; + + /* Require I/O or memory control */ + pci_read_config_word(pdev, PCI_COMMAND, &cmd); + if (!(cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))) + return false; + + dev_info(&pdev->dev, "vga_default: setting as default device\n"); + pci_set_default_display(pdev); + return true; +} + +static int __init vga_default_init(void) +{ + struct pci_dev *pdev; + + vga_default_active = true; + + if (pci_default_display()) + return 0; + + pdev = NULL; + while ((pdev = + pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, + PCI_ANY_ID, pdev)) != NULL) { + if (vga_default_try_device(pdev)) + return 0; + } + + return 0; +} +late_initcall(vga_default_init); + +/* + * A driver could be loaded much later than late_initcall, for example + * if it's in a module. + * + * We want to pick that up. However, we want to make sure this does + * not interfere with the arbiter - it should only activate if the + * arbiter has already had a chance to operate. To ensure this, we set + * vga_default_active in the late_initcall: as the vga arbiter is a + * subsys initcall, it is guaranteed to fire first. + */ +static void vga_default_enable_hook(struct pci_dev *pdev) +{ + if (!vga_default_active) + return; + + if (pci_default_display()) + return; + + vga_default_try_device(pdev); +} +DECLARE_PCI_FIXUP_CLASS_ENABLE(PCI_ANY_ID, PCI_ANY_ID, + PCI_CLASS_DISPLAY_VGA, 8, + vga_default_enable_hook)