xref: /linux-6.15/arch/x86/kernel/resource.c (revision 31bf0f43)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ioport.h>
3 #include <linux/printk.h>
4 #include <asm/e820/api.h>
5 
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 
27 static void remove_e820_regions(struct resource *avail)
28 {
29 	int i;
30 	struct e820_entry *entry;
31 	u64 e820_start, e820_end;
32 	struct resource orig = *avail;
33 
34 	for (i = 0; i < e820_table->nr_entries; i++) {
35 		entry = &e820_table->entries[i];
36 		e820_start = entry->addr;
37 		e820_end = entry->addr + entry->size - 1;
38 
39 		resource_clip(avail, e820_start, e820_end);
40 		if (orig.start != avail->start || orig.end != avail->end) {
41 			pr_info("clipped %pR to %pR for e820 entry [mem %#010Lx-%#010Lx]\n",
42 				 &orig, avail, e820_start, e820_end);
43 			orig = *avail;
44 		}
45 	}
46 }
47 
48 void arch_remove_reservations(struct resource *avail)
49 {
50 	/*
51 	 * Trim out BIOS area (high 2MB) and E820 regions. We do not remove
52 	 * the low 1MB unconditionally, as this area is needed for some ISA
53 	 * cards requiring a memory range, e.g. the i82365 PCMCIA controller.
54 	 */
55 	if (avail->flags & IORESOURCE_MEM) {
56 		resource_clip(avail, BIOS_ROM_BASE, BIOS_ROM_END);
57 
58 		remove_e820_regions(avail);
59 	}
60 }
61