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: 9933663 X-Patchwork-Delegate: bhelgaas@google.com 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 9A3FD6016C for ; Fri, 1 Sep 2017 07:28:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C0472857D for ; Fri, 1 Sep 2017 07:28:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8066E28582; Fri, 1 Sep 2017 07:28:35 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D13412857D for ; Fri, 1 Sep 2017 07:28:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751398AbdIAH2e (ORCPT ); Fri, 1 Sep 2017 03:28:34 -0400 Received: from mail-pg0-f68.google.com ([74.125.83.68]:37601 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751354AbdIAH2d (ORCPT ); Fri, 1 Sep 2017 03:28:33 -0400 Received: by mail-pg0-f68.google.com with SMTP id t193so1236431pgc.4 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=sieXNIyQIG1a1WozXZZOzG9IeO8/q+bW1oqv3MH1pheduvhyYefk6lfuDm23zHruLd XMqR75h35y4JUekvUQOW51hcpjauq7bQdJ5FHskLsn0TeqAkYO+4owUTk4MEwQB5paRA AnZhsr/6Pze2HmcbwzsZ+EfT5HLdgpakfaXdgBFLXWCvMq2IN1e0ustSP/mXMLpyFNdy Oa6vW+oPb2JxOdROT+g3Y3YB09HoGRyQN7pKasM2eq0LC+ML/R2UOxBubYGWbVvADle7 cD4MkMsRoSFzVds9KpWaMEaPg2YKbRNVVroHC7Jtv0vkHjF27KWZoXpudDClym9k8KYK n43w== X-Gm-Message-State: AHPjjUg+Q/YWILT91+sY3O6Hrnuhroc5+TM86SygCCy9EzkpzHbnE5fK BBQc9UHg8jyCizsiAfELDQ== 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 Cc: benh@kernel.crashing.org, z.liuxinliang@hisilicon.com, zourongrong@gmail.com, catalin.marinas@arm.com, will.deacon@arm.com, gabriele.paoloni@huawei.com, helgaas@kernel.org, airlied@linux.ie, daniel.vetter@intel.com, alex.williamson@redhat.com, dri-devel@lists.freedesktop.org, lukas@wunner.de, ard.biesheuvel@linaro.org, lorenzo.pieralisi@arm.com, Daniel Axtens 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> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org 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)