Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hans de Goede | 7f7b423 | 2021-12-17 15:13:48 +0100 | [diff] [blame] | 2 | #include <linux/dmi.h> |
Bjorn Helgaas | 30919b0 | 2010-12-16 10:38:51 -0700 | [diff] [blame] | 3 | #include <linux/ioport.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 4 | #include <asm/e820/api.h> |
Bjorn Helgaas | 30919b0 | 2010-12-16 10:38:51 -0700 | [diff] [blame] | 5 | |
Bjorn Helgaas | 4dc2287 | 2010-12-16 10:38:56 -0700 | [diff] [blame] | 6 | static void resource_clip(struct resource *res, resource_size_t start, |
| 7 | resource_size_t end) |
| 8 | { |
| 9 | resource_size_t low = 0, high = 0; |
| 10 | |
| 11 | if (res->end < start || res->start > end) |
| 12 | return; /* no conflict */ |
| 13 | |
| 14 | if (res->start < start) |
| 15 | low = start - res->start; |
| 16 | |
| 17 | if (res->end > end) |
| 18 | high = res->end - end; |
| 19 | |
| 20 | /* Keep the area above or below the conflict, whichever is larger */ |
| 21 | if (low > high) |
| 22 | res->end = start - 1; |
| 23 | else |
| 24 | res->start = end + 1; |
| 25 | } |
| 26 | |
Hans de Goede | 7f7b423 | 2021-12-17 15:13:48 +0100 | [diff] [blame] | 27 | /* |
| 28 | * Some BIOS-es contain a bug where they add addresses which map to |
| 29 | * system RAM in the PCI host bridge window returned by the ACPI _CRS |
| 30 | * method, see commit 4dc2287c1805 ("x86: avoid E820 regions when |
| 31 | * allocating address space"). To avoid this Linux by default excludes |
| 32 | * E820 reservations when allocating addresses since 2010. |
| 33 | * In 2019 some systems have shown-up with E820 reservations which cover |
| 34 | * the entire _CRS returned PCI host bridge window, causing all attempts |
| 35 | * to assign memory to PCI BARs to fail if Linux uses E820 reservations. |
| 36 | * |
| 37 | * Ideally Linux would fully stop using E820 reservations, but then |
| 38 | * the old systems this was added for will regress. |
| 39 | * Instead keep the old behavior for old systems, while ignoring the |
| 40 | * E820 reservations for any systems from now on. |
| 41 | */ |
Bjorn Helgaas | 4dc2287 | 2010-12-16 10:38:56 -0700 | [diff] [blame] | 42 | static void remove_e820_regions(struct resource *avail) |
| 43 | { |
Hans de Goede | 7f7b423 | 2021-12-17 15:13:48 +0100 | [diff] [blame] | 44 | int i, year = dmi_get_bios_year(); |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 45 | struct e820_entry *entry; |
Bjorn Helgaas | 4dc2287 | 2010-12-16 10:38:56 -0700 | [diff] [blame] | 46 | |
Hans de Goede | 7f7b423 | 2021-12-17 15:13:48 +0100 | [diff] [blame] | 47 | if (year >= 2018) |
| 48 | return; |
| 49 | |
| 50 | pr_info_once("PCI: Removing E820 reservations from host bridge windows\n"); |
| 51 | |
Ingo Molnar | bf49557 | 2017-01-27 14:06:21 +0100 | [diff] [blame] | 52 | for (i = 0; i < e820_table->nr_entries; i++) { |
| 53 | entry = &e820_table->entries[i]; |
Bjorn Helgaas | 4dc2287 | 2010-12-16 10:38:56 -0700 | [diff] [blame] | 54 | |
| 55 | resource_clip(avail, entry->addr, |
| 56 | entry->addr + entry->size - 1); |
| 57 | } |
| 58 | } |
| 59 | |
Bjorn Helgaas | 30919b0 | 2010-12-16 10:38:51 -0700 | [diff] [blame] | 60 | void arch_remove_reservations(struct resource *avail) |
| 61 | { |
Christoph Schulz | cbace46 | 2014-07-16 10:00:57 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Trim out BIOS area (high 2MB) and E820 regions. We do not remove |
| 64 | * the low 1MB unconditionally, as this area is needed for some ISA |
| 65 | * cards requiring a memory range, e.g. the i82365 PCMCIA controller. |
| 66 | */ |
Bjorn Helgaas | 30919b0 | 2010-12-16 10:38:51 -0700 | [diff] [blame] | 67 | if (avail->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | a2c606d | 2010-12-16 10:39:02 -0700 | [diff] [blame] | 68 | resource_clip(avail, BIOS_ROM_BASE, BIOS_ROM_END); |
Bjorn Helgaas | 4dc2287 | 2010-12-16 10:38:56 -0700 | [diff] [blame] | 69 | |
| 70 | remove_e820_regions(avail); |
Bjorn Helgaas | 30919b0 | 2010-12-16 10:38:51 -0700 | [diff] [blame] | 71 | } |
| 72 | } |