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