Re:[PATCH v2 1/4] efi/x86: Remove EfiMemoryMappedIO from E820 map

Baowen Zheng
2023-01-13T10:49:42Z

Hello Bjorn Helgaas:

When we tested our NIC, we found we will fail to access pcie extent configuration space.
Actually we can only access the 256 Bytes of the pcie configuration space.
You can make check with command of "lspci -s XXXX -vv"
It seems relates to this change, could you please help to make a check?

On Thu, 8 Dec 2022 13:03:38 Bjorn Helgaas wrote:
>Firmware can use EfiMemoryMappedIO to request that MMIO regions be mapped
>by the OS so they can be accessed by EFI runtime services, but should have
>no other significance to the OS (UEFI r2.10, sec 7.2). However, most
>bootloaders and EFI stubs convert EfiMemoryMappedIO regions to
>E820_TYPE_RESERVED entries, which prevent Linux from allocating space from
>them (see remove_e820_regions()).
>
>Some platforms use EfiMemoryMappedIO entries for PCI MMCONFIG space and PCI
>host bridge windows, which means Linux can't allocate BAR space for
>hot-added devices.
>
>Remove large EfiMemoryMappedIO regions from the E820 map to avoid this
>problem.
>

...

>
>Link: https://bugzilla.kernel.org/show_bug.cgi?id=216565 Clevo NL4XLU
>Link: https://bugzilla.kernel.org/show_bug.cgi?id=206459#c78 Clevo X170KM-G
>Link: https://bugzilla.redhat.com/show_bug.cgi?id=1868899 Ideapad Slim 3
>Link: https://bugzilla.redhat.com/show_bug.cgi?id=2029207 X1 Carbon
>Reported-by: Florent DELAHAYE <kernelorg@undead.fr>
>Tested-by: Konrad J Hambrick <kjhambrick@gmail.com>
>Tested-by: Matt Hansen <2lprbe78@duck.com>
>Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
>Cc: Hans de Goede <hdegoede@redhat.com>
>---
> arch/x86/platform/efi/efi.c | 46 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 46 insertions(+)
>
>diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
>index ebc98a68c400..dee1852e95cd 100644
>--- a/arch/x86/platform/efi/efi.c
>+++ b/arch/x86/platform/efi/efi.c
>@@ -303,6 +303,50 @@ static void __init efi_clean_memmap(void)
> }
> }
>
>+/*
>+ * Firmware can use EfiMemoryMappedIO to request that MMIO regions be
>+ * mapped by the OS so they can be accessed by EFI runtime services, but
>+ * should have no other significance to the OS (UEFI r2.10, sec 7.2).
>+ * However, most bootloaders and EFI stubs convert EfiMemoryMappedIO
>+ * regions to E820_TYPE_RESERVED entries, which prevent Linux from
>+ * allocating space from them (see remove_e820_regions()).
>+ *
>+ * Some platforms use EfiMemoryMappedIO entries for PCI MMCONFIG space and
>+ * PCI host bridge windows, which means Linux can't allocate BAR space for
>+ * hot-added devices.
>+ *
>+ * Remove large EfiMemoryMappedIO regions from the E820 map to avoid this
>+ * problem.
>+ *
>+ * Retain small EfiMemoryMappedIO regions because on some platforms, these
>+ * describe non-window space that's included in host bridge _CRS. If we
>+ * assign that space to PCI devices, they don't work.
>+ */
>+static void __init efi_remove_e820_mmio(void)
>+{
>+ efi_memory_desc_t *md;
>+ u64 size, start, end;
>+ int i = 0;
>+
>+ for_each_efi_memory_desc(md) {
>+ if (md->type == EFI_MEMORY_MAPPED_IO) {
>+ size = md->num_pages << EFI_PAGE_SHIFT;
>+ if (size >= 256*1024) {
>+ start = md->phys_addr;
>+ end = start + size - 1;
>+ pr_info("Remove mem%02u: MMIO range=[0x%08llx-0x%08llx] (%lluMB) from e820 map\n",
>+ i, start, end, size >> 20);
>+ e820__range_remove(start, size,
>+ E820_TYPE_RESERVED, 1);
>+ } else {
>+ pr_info("Not removing mem%02u: MMIO range=[0x%08llx-0x%08llx] (%lluKB) from e820 map\n",
>+ i, start, end, size >> 10);
>+ }
>+ }
>+ i++;
>+ }
>+}
>+
> void __init efi_print_memmap(void)
> {
> efi_memory_desc_t *md;
>@@ -474,6 +518,8 @@ void __init efi_init(void)
> set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
> efi_clean_memmap();
>
>+ efi_remove_e820_mmio();
>+
> if (efi_enabled(EFI_DBG))
> efi_print_memmap();
> }