Message ID | 20231002120511.594737-2-suijingfeng@loongson.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1393823vqb; Mon, 2 Oct 2023 05:35:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4fVH3Y8wKkg61bWcj3BN9CRjOSI9iZpZ5t9SVBm+a+0RrxPLo0AjS8s5DUzmfIKmG8e1+ X-Received: by 2002:a05:6a20:3c86:b0:13e:90aa:8c8b with SMTP id b6-20020a056a203c8600b0013e90aa8c8bmr16499769pzj.4.1696250111854; Mon, 02 Oct 2023 05:35:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696250111; cv=none; d=google.com; s=arc-20160816; b=j4seLFxX6nnNnkGZ8o8FY71Z0zmgubkXTD3qSIRPiCADBvehC03oNKfPJ0ZUX30c7S /CQ/kgIfK1Pn4ll/V/9sJ5SE9lGudcHRNir2FK3eZ7r9rWOOcJN9oF0ltIAPwH4pAnPX eWR3r3SkRtqgnp88mOQKsqfsWWfC6x7FJ1TdecHN5pSznmP2BgviAphYkupiRFc2e8db AZ0+XCGPNaPe93CnsmA/ogIUyZB2UpS+4Pkwa7Faa6hHEvve6X/Bt+KR5Ibsc7X4jeil fMvGA1Z3exrjPYw+g8biK0TTM32eZ8Ne2Pgd8hkFzuf0sPqLsaHGVWssWhpx9F8cTzM7 G4MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=kKr9Svy9ECSD/6R2SLPBe1FHDK8bnWGUKs6lEJfcE6Y=; fh=0qCxjCvixYFjgYFDzgmT1u2HwZtsHsuy4jQzZ1FeYPg=; b=B1vc++g69Bpzn3I7y45ldb6ozbc/838wytF2TvsbiFY1c8fSoVmk2+IH8Dj9Os3UEI QKlDRQv6XtXkcgQ16BcZhPrq8UySgCZ81rc/SOONu25THDh4X9GIiWI67ziPagU8Gmsn RxjqRi+1VER+p/FJ21Wb+8IMnXb2PFoTIAVDKX2dZuhxq4zcCAj581wdTADYQ9NIcKkE Px8H1wSlcLMnDvatN8pE06rGVMreoxU8yTSOVBZ55QCgeLank8w5Ew7sEfnFE8lQ64tb zo1vlTngrGRIgKtNivYoNYcx8BzEmOr8gPV/7jlrR1ReE6oW2cnxNqhKh7EcRc3QzLAN SQlA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u62-20020a638541000000b00588e13f4cc8si5319796pgd.519.2023.10.02.05.35.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 05:35:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 606B580A993D; Mon, 2 Oct 2023 05:05:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236894AbjJBMFX (ORCPT <rfc822;pusanteemu@gmail.com> + 18 others); Mon, 2 Oct 2023 08:05:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236843AbjJBMFT (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 2 Oct 2023 08:05:19 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8AB11D3; Mon, 2 Oct 2023 05:05:15 -0700 (PDT) Received: from loongson.cn (unknown [10.20.42.43]) by gateway (Coremail) with SMTP id _____8AxV_H4sRply5MuAA--.24516S3; Mon, 02 Oct 2023 20:05:12 +0800 (CST) Received: from openarena.loongson.cn (unknown [10.20.42.43]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxK9z3sRplVSEXAA--.47905S3; Mon, 02 Oct 2023 20:05:11 +0800 (CST) From: Sui Jingfeng <suijingfeng@loongson.cn> To: Bjorn Helgaas <bhelgaas@google.com> Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH pci-next v6 1/2] PCI/VGA: Make the vga_is_firmware_default() less arch-dependent Date: Mon, 2 Oct 2023 20:05:10 +0800 Message-Id: <20231002120511.594737-2-suijingfeng@loongson.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231002120511.594737-1-suijingfeng@loongson.cn> References: <20231002120511.594737-1-suijingfeng@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8BxK9z3sRplVSEXAA--.47905S3 X-CM-SenderInfo: xvxlyxpqjiv03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBj93XoWxCFWrCF1xXr4xWFWUZw1kJFc_yoW5tF48pr WfGFWrtrs5Gw4fGrW3ta10qFn09F93C340kFW3uwn3CF17AFykWr1FkFZ0qry5G397XF43 XF4ayF1DGayDXFXCm3ZEXasCq-sJn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4j6r4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc 02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAF wI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28IcxkI7V AKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUXVWUAwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAI w20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x 0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU8HSoJUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:05:40 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778647156282410792 X-GMAIL-MSGID: 1778647157320049637 |
Series |
PCI/VGA: Make the vga_is_firmware_default() less arch-dependent
|
|
Commit Message
Sui Jingfeng
Oct. 2, 2023, 12:05 p.m. UTC
Currently, the vga_is_firmware_default() function only works on x86 and
ia64, it is a no-op on the rest of the architectures. This patch completes
the implementation for it, the added code tries to capture the PCI (e) VGA
device that owns the firmware framebuffer, since only one GPU could own
the firmware fb, things are almost done once we have determined the boot
VGA device. As the PCI resource relocation do have a influence on the
results of identification, we make it available on architectures where PCI
resource relocation does happen at first. Because this patch is more
important for those architectures(such as arm, arm64, loongarch, mips and
risc-v etc).
Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>
---
drivers/pci/vgaarb.c | 76 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
Comments
On Mon, Oct 02, 2023 at 08:05:10PM +0800, Sui Jingfeng wrote: > Currently, the vga_is_firmware_default() function only works on x86 and > ia64, it is a no-op on the rest of the architectures. This patch completes > the implementation for it, the added code tries to capture the PCI (e) VGA > device that owns the firmware framebuffer, since only one GPU could own > the firmware fb, things are almost done once we have determined the boot > VGA device. As the PCI resource relocation do have a influence on the > results of identification, we make it available on architectures where PCI > resource relocation does happen at first. Because this patch is more > important for those architectures(such as arm, arm64, loongarch, mips and > risc-v etc). There's a little too much going on this this patch. The problem is very simple: currently we compare firmware BAR assignments with BARs that may have been reassigned by Linux. What if we did something like the patch below? I think it will be less confusing if we only have one copy of the code related to screen_info. Bjorn > Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> > --- > drivers/pci/vgaarb.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 76 insertions(+) > > diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c > index 5e6b1eb54c64..02821c0f4cd0 100644 > --- a/drivers/pci/vgaarb.c > +++ b/drivers/pci/vgaarb.c > @@ -60,6 +60,9 @@ static bool vga_arbiter_used; > static DEFINE_SPINLOCK(vga_lock); > static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); > > +/* The PCI(e) VGA device which owns the firmware framebuffer */ > +static struct pci_dev *pdev_boot_vga; > + > static const char *vga_iostate_to_str(unsigned int iostate) > { > /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ > @@ -582,6 +585,9 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > > return true; > } > +#else > + if (pdev_boot_vga && pdev_boot_vga == pdev) > + return true; > #endif > return false; > } > @@ -1557,3 +1563,73 @@ static int __init vga_arb_device_init(void) > return rc; > } > subsys_initcall_sync(vga_arb_device_init); > + > +/* > + * Get the physical address range that the firmware framebuffer occupies. > + * > + * Note that the global screen_info is arch-specific, thus CONFIG_SYSFB is > + * chosen as compile-time conditional to suppress linkage problems on non-x86 > + * architectures. > + * > + * Returns true on success, otherwise return false. > + */ > +static bool vga_arb_get_firmware_fb_range(u64 *start, u64 *end) > +{ > + u64 fb_start = 0; > + u64 fb_size = 0; > + u64 fb_end; > + > +#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_SYSFB) > + fb_start = screen_info.lfb_base; > + if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) > + fb_start |= (u64)screen_info.ext_lfb_base << 32; > + > + fb_size = screen_info.lfb_size; > +#endif > + > + /* No firmware framebuffer support */ > + if (!fb_start || !fb_size) > + return false; > + > + fb_end = fb_start + fb_size - 1; > + > + *start = fb_start; > + *end = fb_end; > + > + return true; > +} > + > +/* > + * Identify the PCI VGA device that contains the firmware framebuffer > + */ > +static void pci_boot_vga_capturer(struct pci_dev *pdev) > +{ > + u64 fb_start, fb_end; > + struct resource *res; > + unsigned int i; > + > + if (pdev_boot_vga) > + return; > + > + if (!vga_arb_get_firmware_fb_range(&fb_start, &fb_end)) > + return; > + > + pci_dev_for_each_resource(pdev, res, i) { > + if (resource_type(res) != IORESOURCE_MEM) > + continue; > + > + if (!res->start || !res->end) > + continue; > + > + if (res->start <= fb_start && fb_end <= res->end) { > + pdev_boot_vga = pdev; > + > + vgaarb_info(&pdev->dev, > + "BAR %u: %pR contains firmware FB [0x%llx-0x%llx]\n", > + i, res, fb_start, fb_end); > + break; > + } > + } > +} > +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, > + 8, pci_boot_vga_capturer); PCI/VGA: Match firmware framebuffer before BAR reassignment vga_is_firmware_default() decides a device is the firmware default VGA device if it has a BAR that contains the framebuffer described by screen_info. Previously this was unreliable because the screen_info framebuffer address comes from firmware, and the Linux PCI core may reassign device BARs before vga_is_firmware_default() runs. This reassignment means the BAR may not match the screen_info values, but we still want to select the device as the firmware default. Make vga_is_firmware_default() more reliable by running it as a quirk so it happens before any BAR reassignment. diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c index 5e6b1eb54c64..4a53e76caddd 100644 --- a/drivers/pci/vgaarb.c +++ b/drivers/pci/vgaarb.c @@ -60,6 +60,8 @@ static bool vga_arbiter_used; static DEFINE_SPINLOCK(vga_lock); static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); +static struct pci_dev *vga_firmware_device; + static const char *vga_iostate_to_str(unsigned int iostate) { /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ @@ -560,6 +562,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) u64 base = screen_info.lfb_base; u64 size = screen_info.lfb_size; struct resource *r; + unsigned int i; u64 limit; /* Select the device owning the boot framebuffer if there is one */ @@ -570,7 +573,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) limit = base + size; /* Does firmware framebuffer belong to us? */ - pci_dev_for_each_resource(pdev, r) { + pci_dev_for_each_resource(pdev, r, i) { if (resource_type(r) != IORESOURCE_MEM) continue; @@ -580,6 +583,8 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) if (base < r->start || limit >= r->end) continue; + vgaarb_info(&pdev->dev, "BAR %u: %pR contains firmware framebuffer [%#010llx-%#010llx]\n", + i, r, base, limit - 1); return true; } #endif @@ -623,7 +628,7 @@ static bool vga_is_boot_device(struct vga_device *vgadev) if (boot_vga && boot_vga->is_firmware_default) return false; - if (vga_is_firmware_default(pdev)) { + if (pdev == vga_firmware_device) { vgadev->is_firmware_default = true; return true; } @@ -1557,3 +1562,14 @@ static int __init vga_arb_device_init(void) return rc; } subsys_initcall_sync(vga_arb_device_init); + +static void vga_match_firmware_framebuffer(struct pci_dev *pdev) +{ + if (vga_firmware_device) + return; + + if (vga_is_firmware_default(pdev)) + vga_firmware_device = pdev; +} +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, + 8, vga_match_firmware_framebuffer);
Hi, On 2023/10/3 23:54, Bjorn Helgaas wrote: > On Mon, Oct 02, 2023 at 08:05:10PM +0800, Sui Jingfeng wrote: >> Currently, the vga_is_firmware_default() function only works on x86 and >> ia64, it is a no-op on the rest of the architectures. This patch completes >> the implementation for it, the added code tries to capture the PCI (e) VGA >> device that owns the firmware framebuffer, since only one GPU could own >> the firmware fb, things are almost done once we have determined the boot >> VGA device. As the PCI resource relocation do have a influence on the >> results of identification, we make it available on architectures where PCI >> resource relocation does happen at first. Because this patch is more >> important for those architectures(such as arm, arm64, loongarch, mips and >> risc-v etc). > There's a little too much going on this this patch. The problem is > very simple: currently we compare firmware BAR assignments with BARs > that may have been reassigned by Linux. > > What if we did something like the patch below? I think it will be > less confusing if we only have one copy of the code related to > screen_info. > > Bjorn > >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> --- >> drivers/pci/vgaarb.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 76 insertions(+) >> >> diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c >> index 5e6b1eb54c64..02821c0f4cd0 100644 >> --- a/drivers/pci/vgaarb.c >> +++ b/drivers/pci/vgaarb.c >> @@ -60,6 +60,9 @@ static bool vga_arbiter_used; >> static DEFINE_SPINLOCK(vga_lock); >> static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); >> >> +/* The PCI(e) VGA device which owns the firmware framebuffer */ >> +static struct pci_dev *pdev_boot_vga; >> + >> static const char *vga_iostate_to_str(unsigned int iostate) >> { >> /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ >> @@ -582,6 +585,9 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) >> >> return true; >> } >> +#else >> + if (pdev_boot_vga && pdev_boot_vga == pdev) >> + return true; >> #endif >> return false; >> } >> @@ -1557,3 +1563,73 @@ static int __init vga_arb_device_init(void) >> return rc; >> } >> subsys_initcall_sync(vga_arb_device_init); >> + >> +/* >> + * Get the physical address range that the firmware framebuffer occupies. >> + * >> + * Note that the global screen_info is arch-specific, thus CONFIG_SYSFB is >> + * chosen as compile-time conditional to suppress linkage problems on non-x86 >> + * architectures. >> + * >> + * Returns true on success, otherwise return false. >> + */ >> +static bool vga_arb_get_firmware_fb_range(u64 *start, u64 *end) >> +{ >> + u64 fb_start = 0; >> + u64 fb_size = 0; >> + u64 fb_end; >> + >> +#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_SYSFB) >> + fb_start = screen_info.lfb_base; >> + if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) >> + fb_start |= (u64)screen_info.ext_lfb_base << 32; >> + >> + fb_size = screen_info.lfb_size; >> +#endif >> + >> + /* No firmware framebuffer support */ >> + if (!fb_start || !fb_size) >> + return false; >> + >> + fb_end = fb_start + fb_size - 1; >> + >> + *start = fb_start; >> + *end = fb_end; >> + >> + return true; >> +} >> + >> +/* >> + * Identify the PCI VGA device that contains the firmware framebuffer >> + */ >> +static void pci_boot_vga_capturer(struct pci_dev *pdev) >> +{ >> + u64 fb_start, fb_end; >> + struct resource *res; >> + unsigned int i; >> + >> + if (pdev_boot_vga) >> + return; >> + >> + if (!vga_arb_get_firmware_fb_range(&fb_start, &fb_end)) >> + return; >> + >> + pci_dev_for_each_resource(pdev, res, i) { >> + if (resource_type(res) != IORESOURCE_MEM) >> + continue; >> + >> + if (!res->start || !res->end) >> + continue; >> + >> + if (res->start <= fb_start && fb_end <= res->end) { >> + pdev_boot_vga = pdev; >> + >> + vgaarb_info(&pdev->dev, >> + "BAR %u: %pR contains firmware FB [0x%llx-0x%llx]\n", >> + i, res, fb_start, fb_end); >> + break; >> + } >> + } >> +} >> +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, >> + 8, pci_boot_vga_capturer); > > PCI/VGA: Match firmware framebuffer before BAR reassignment > > vga_is_firmware_default() decides a device is the firmware default VGA > device if it has a BAR that contains the framebuffer described by > screen_info. > > Previously this was unreliable because the screen_info framebuffer address > comes from firmware, and the Linux PCI core may reassign device BARs before > vga_is_firmware_default() runs. This reassignment means the BAR may not > match the screen_info values, but we still want to select the device as the > firmware default. > > Make vga_is_firmware_default() more reliable by running it as a quirk so it > happens before any BAR reassignment. > > diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c > index 5e6b1eb54c64..4a53e76caddd 100644 > --- a/drivers/pci/vgaarb.c > +++ b/drivers/pci/vgaarb.c > @@ -60,6 +60,8 @@ static bool vga_arbiter_used; > static DEFINE_SPINLOCK(vga_lock); > static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); > > +static struct pci_dev *vga_firmware_device; > + > static const char *vga_iostate_to_str(unsigned int iostate) > { > /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ > @@ -560,6 +562,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > u64 base = screen_info.lfb_base; > u64 size = screen_info.lfb_size; > struct resource *r; > + unsigned int i; > u64 limit; > > /* Select the device owning the boot framebuffer if there is one */ > @@ -570,7 +573,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > limit = base + size; > > /* Does firmware framebuffer belong to us? */ > - pci_dev_for_each_resource(pdev, r) { > + pci_dev_for_each_resource(pdev, r, i) { > if (resource_type(r) != IORESOURCE_MEM) > continue; > > @@ -580,6 +583,8 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > if (base < r->start || limit >= r->end) > continue; > > + vgaarb_info(&pdev->dev, "BAR %u: %pR contains firmware framebuffer [%#010llx-%#010llx]\n", > + i, r, base, limit - 1); > return true; > } > #endif > @@ -623,7 +628,7 @@ static bool vga_is_boot_device(struct vga_device *vgadev) > if (boot_vga && boot_vga->is_firmware_default) > return false; > > - if (vga_is_firmware_default(pdev)) { > + if (pdev == vga_firmware_device) { > vgadev->is_firmware_default = true; > return true; > } > @@ -1557,3 +1562,14 @@ static int __init vga_arb_device_init(void) > return rc; > } > subsys_initcall_sync(vga_arb_device_init); > + > +static void vga_match_firmware_framebuffer(struct pci_dev *pdev) > +{ > + if (vga_firmware_device) > + return; > + > + if (vga_is_firmware_default(pdev)) > + vga_firmware_device = pdev; > +} > +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, > + 8, vga_match_firmware_framebuffer); Q: What if we did something like the patch below? A: But the vga_is_firmware_default() function only works on X86 and IA64, you patch doesn't solve the problems on ARM64 and LoongArch. Q: I think it will be less confusing if we only have one copy of the code related to screen_info. A: Since you have already know everything, you can do anything with those two patch if you would like to pick them up. As I have spend enough more time on this, if two copy of the screen_info really matters, Would you mind to squash those two patch into one?
Hi, On 2023/10/3 23:54, Bjorn Helgaas wrote: > PCI/VGA: Match firmware framebuffer before BAR reassignment > > vga_is_firmware_default() decides a device is the firmware default VGA > device if it has a BAR that contains the framebuffer described by > screen_info. > > Previously this was unreliable because the screen_info framebuffer address > comes from firmware, and the Linux PCI core may reassign device BARs before > vga_is_firmware_default() runs. This reassignment means the BAR may not > match the screen_info values, but we still want to select the device as the > firmware default. > > Make vga_is_firmware_default() more reliable by running it as a quirk so it > happens before any BAR reassignment. On a specific architecture, the kernel have its own copy of screen_info. We may choose to rely on the global screen_info, but I don't hope vgaarb should completely depend on the firmware which without any flexibility. After all, there are always have the old BIOS with the newer Kernel or new graphics card combinations. That is to say that the BIOS(firmware) is released first, then the new graphics comes after. So, we can't rely on the BIOS know all of the graphics card in the world at the time of BIOS release. If a BIOS don't know a specific video card, then then BIOS is unlikely initialize a firmware framebuffer(UEFI GOP or something like that) on it successfully. I hope vgaarb can work without fully rely on the firmware, don't mention the word 'firmware' as less as possible. Because, we can even modify the kernel's screen_info by hardcode or fill it by parsing DT. We are only rely on the global screen_info here. while how does the global screen_info being filled is a kernel side and arch specific thing. The global screen_info may rely on the BIOS, but this is a thing of the kernel side, vgaarb belong to drivers directory. Beside, the word 'firmware' have multiple meanings, for the patch at here, it refer to the UEFI or uboot or something similar. But for the GPU domain, it may refer to any binary executable on the macro-controller embedded in the GPU IP. So I think the names 'is_firmware_default' and 'vga_is_firmware_default' are putting *too much emphasis* the firmware. So I want to remove it. vgaarb already have been exposed to userspace via sysfs interface (/sys/class/misc/vga_arbiter). So the original spirit is actually allow user to tune or control. I hope vgaarb become more standalone and more flexible so that there are some ways to rescue even in the worse case. If someone (who have a better background or have better understanding toward a specific case only) don't see this as a problem, then it is not my problem. I'm not good at debating with English.
On Wed, Oct 04, 2023 at 08:55:04PM +0800, Sui Jingfeng wrote: > On 2023/10/3 23:54, Bjorn Helgaas wrote: > > On Mon, Oct 02, 2023 at 08:05:10PM +0800, Sui Jingfeng wrote: > > > Currently, the vga_is_firmware_default() function only works on x86 and > > > ia64, it is a no-op on the rest of the architectures. This patch completes > > > the implementation for it, the added code tries to capture the PCI (e) VGA > > > device that owns the firmware framebuffer, since only one GPU could own > > > the firmware fb, things are almost done once we have determined the boot > > > VGA device. As the PCI resource relocation do have a influence on the > > > results of identification, we make it available on architectures where PCI > > > resource relocation does happen at first. Because this patch is more > > > important for those architectures(such as arm, arm64, loongarch, mips and > > > risc-v etc). > > > > There's a little too much going on this this patch. The problem is > > very simple: currently we compare firmware BAR assignments with BARs > > that may have been reassigned by Linux. > > > > What if we did something like the patch below? I think it will be > > less confusing if we only have one copy of the code related to > > screen_info. > > > > > Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> > > > --- > > > drivers/pci/vgaarb.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ > > > 1 file changed, 76 insertions(+) > > > > > > diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c > > > index 5e6b1eb54c64..02821c0f4cd0 100644 > > > --- a/drivers/pci/vgaarb.c > > > +++ b/drivers/pci/vgaarb.c > > > @@ -60,6 +60,9 @@ static bool vga_arbiter_used; > > > static DEFINE_SPINLOCK(vga_lock); > > > static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); > > > +/* The PCI(e) VGA device which owns the firmware framebuffer */ > > > +static struct pci_dev *pdev_boot_vga; > > > + > > > static const char *vga_iostate_to_str(unsigned int iostate) > > > { > > > /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ > > > @@ -582,6 +585,9 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > > > return true; > > > } > > > +#else > > > + if (pdev_boot_vga && pdev_boot_vga == pdev) > > > + return true; > > > #endif > > > return false; > > > } > > > @@ -1557,3 +1563,73 @@ static int __init vga_arb_device_init(void) > > > return rc; > > > } > > > subsys_initcall_sync(vga_arb_device_init); > > > + > > > +/* > > > + * Get the physical address range that the firmware framebuffer occupies. > > > + * > > > + * Note that the global screen_info is arch-specific, thus CONFIG_SYSFB is > > > + * chosen as compile-time conditional to suppress linkage problems on non-x86 > > > + * architectures. > > > + * > > > + * Returns true on success, otherwise return false. > > > + */ > > > +static bool vga_arb_get_firmware_fb_range(u64 *start, u64 *end) > > > +{ > > > + u64 fb_start = 0; > > > + u64 fb_size = 0; > > > + u64 fb_end; > > > + > > > +#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_SYSFB) > > > + fb_start = screen_info.lfb_base; > > > + if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) > > > + fb_start |= (u64)screen_info.ext_lfb_base << 32; > > > + > > > + fb_size = screen_info.lfb_size; > > > +#endif > > > + > > > + /* No firmware framebuffer support */ > > > + if (!fb_start || !fb_size) > > > + return false; > > > + > > > + fb_end = fb_start + fb_size - 1; > > > + > > > + *start = fb_start; > > > + *end = fb_end; > > > + > > > + return true; > > > +} > > > + > > > +/* > > > + * Identify the PCI VGA device that contains the firmware framebuffer > > > + */ > > > +static void pci_boot_vga_capturer(struct pci_dev *pdev) > > > +{ > > > + u64 fb_start, fb_end; > > > + struct resource *res; > > > + unsigned int i; > > > + > > > + if (pdev_boot_vga) > > > + return; > > > + > > > + if (!vga_arb_get_firmware_fb_range(&fb_start, &fb_end)) > > > + return; > > > + > > > + pci_dev_for_each_resource(pdev, res, i) { > > > + if (resource_type(res) != IORESOURCE_MEM) > > > + continue; > > > + > > > + if (!res->start || !res->end) > > > + continue; > > > + > > > + if (res->start <= fb_start && fb_end <= res->end) { > > > + pdev_boot_vga = pdev; > > > + > > > + vgaarb_info(&pdev->dev, > > > + "BAR %u: %pR contains firmware FB [0x%llx-0x%llx]\n", > > > + i, res, fb_start, fb_end); > > > + break; > > > + } > > > + } > > > +} > > > +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, > > > + 8, pci_boot_vga_capturer); > > > > PCI/VGA: Match firmware framebuffer before BAR reassignment > > > > vga_is_firmware_default() decides a device is the firmware default VGA > > device if it has a BAR that contains the framebuffer described by > > screen_info. > > > > Previously this was unreliable because the screen_info framebuffer address > > comes from firmware, and the Linux PCI core may reassign device BARs before > > vga_is_firmware_default() runs. This reassignment means the BAR may not > > match the screen_info values, but we still want to select the device as the > > firmware default. > > > > Make vga_is_firmware_default() more reliable by running it as a quirk so it > > happens before any BAR reassignment. > > > > diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c > > index 5e6b1eb54c64..4a53e76caddd 100644 > > --- a/drivers/pci/vgaarb.c > > +++ b/drivers/pci/vgaarb.c > > @@ -60,6 +60,8 @@ static bool vga_arbiter_used; > > static DEFINE_SPINLOCK(vga_lock); > > static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); > > +static struct pci_dev *vga_firmware_device; > > + > > static const char *vga_iostate_to_str(unsigned int iostate) > > { > > /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ > > @@ -560,6 +562,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > > u64 base = screen_info.lfb_base; > > u64 size = screen_info.lfb_size; > > struct resource *r; > > + unsigned int i; > > u64 limit; > > /* Select the device owning the boot framebuffer if there is one */ > > @@ -570,7 +573,7 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > > limit = base + size; > > /* Does firmware framebuffer belong to us? */ > > - pci_dev_for_each_resource(pdev, r) { > > + pci_dev_for_each_resource(pdev, r, i) { > > if (resource_type(r) != IORESOURCE_MEM) > > continue; > > @@ -580,6 +583,8 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) > > if (base < r->start || limit >= r->end) > > continue; > > + vgaarb_info(&pdev->dev, "BAR %u: %pR contains firmware framebuffer [%#010llx-%#010llx]\n", > > + i, r, base, limit - 1); > > return true; > > } > > #endif > > @@ -623,7 +628,7 @@ static bool vga_is_boot_device(struct vga_device *vgadev) > > if (boot_vga && boot_vga->is_firmware_default) > > return false; > > - if (vga_is_firmware_default(pdev)) { > > + if (pdev == vga_firmware_device) { > > vgadev->is_firmware_default = true; > > return true; > > } > > @@ -1557,3 +1562,14 @@ static int __init vga_arb_device_init(void) > > return rc; > > } > > subsys_initcall_sync(vga_arb_device_init); > > + > > +static void vga_match_firmware_framebuffer(struct pci_dev *pdev) > > +{ > > + if (vga_firmware_device) > > + return; > > + > > + if (vga_is_firmware_default(pdev)) > > + vga_firmware_device = pdev; > > +} > > +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, > > + 8, vga_match_firmware_framebuffer); > > > Q: What if we did something like the patch below? > > A: > > But the vga_is_firmware_default() function only works on X86 and IA64, > you patch doesn't solve the problems on ARM64 and LoongArch. Yes, that's true. Ideally a patch solves a single problem. This one solves an issue for x86 and ia64. A subsequent patch can solve the problems on ARM64 and LoongArch. Doing them separately means that each patch is easier to understand and if we accidentally break something, a bisection can give more specific information about what broke. Bjorn
diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c index 5e6b1eb54c64..02821c0f4cd0 100644 --- a/drivers/pci/vgaarb.c +++ b/drivers/pci/vgaarb.c @@ -60,6 +60,9 @@ static bool vga_arbiter_used; static DEFINE_SPINLOCK(vga_lock); static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); +/* The PCI(e) VGA device which owns the firmware framebuffer */ +static struct pci_dev *pdev_boot_vga; + static const char *vga_iostate_to_str(unsigned int iostate) { /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ @@ -582,6 +585,9 @@ static bool vga_is_firmware_default(struct pci_dev *pdev) return true; } +#else + if (pdev_boot_vga && pdev_boot_vga == pdev) + return true; #endif return false; } @@ -1557,3 +1563,73 @@ static int __init vga_arb_device_init(void) return rc; } subsys_initcall_sync(vga_arb_device_init); + +/* + * Get the physical address range that the firmware framebuffer occupies. + * + * Note that the global screen_info is arch-specific, thus CONFIG_SYSFB is + * chosen as compile-time conditional to suppress linkage problems on non-x86 + * architectures. + * + * Returns true on success, otherwise return false. + */ +static bool vga_arb_get_firmware_fb_range(u64 *start, u64 *end) +{ + u64 fb_start = 0; + u64 fb_size = 0; + u64 fb_end; + +#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_SYSFB) + fb_start = screen_info.lfb_base; + if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) + fb_start |= (u64)screen_info.ext_lfb_base << 32; + + fb_size = screen_info.lfb_size; +#endif + + /* No firmware framebuffer support */ + if (!fb_start || !fb_size) + return false; + + fb_end = fb_start + fb_size - 1; + + *start = fb_start; + *end = fb_end; + + return true; +} + +/* + * Identify the PCI VGA device that contains the firmware framebuffer + */ +static void pci_boot_vga_capturer(struct pci_dev *pdev) +{ + u64 fb_start, fb_end; + struct resource *res; + unsigned int i; + + if (pdev_boot_vga) + return; + + if (!vga_arb_get_firmware_fb_range(&fb_start, &fb_end)) + return; + + pci_dev_for_each_resource(pdev, res, i) { + if (resource_type(res) != IORESOURCE_MEM) + continue; + + if (!res->start || !res->end) + continue; + + if (res->start <= fb_start && fb_end <= res->end) { + pdev_boot_vga = pdev; + + vgaarb_info(&pdev->dev, + "BAR %u: %pR contains firmware FB [0x%llx-0x%llx]\n", + i, res, fb_start, fb_end); + break; + } + } +} +DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA, + 8, pci_boot_vga_capturer);