1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/sched.h>
4 #include <linux/uaccess.h>
5 #include <linux/mmzone.h>
6 #include <linux/ioport.h>
7 #include <linux/seq_file.h>
8 #include <linux/console.h>
9 #include <linux/init.h>
10 #include <linux/edd.h>
11 #include <linux/dmi.h>
12 #include <linux/pfn.h>
13 #include <linux/pci.h>
14 #include <linux/export.h>
16 #include <asm/probe_roms.h>
17 #include <asm/pci-direct.h>
18 #include <asm/e820/api.h>
19 #include <asm/mmzone.h>
20 #include <asm/setup.h>
21 #include <asm/sections.h>
23 #include <asm/setup_arch.h>
26 static struct resource system_rom_resource = {
30 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
33 static struct resource extension_rom_resource = {
34 .name = "Extension ROM",
37 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
40 static struct resource adapter_rom_resources[] = { {
41 .name = "Adapter ROM",
44 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
46 .name = "Adapter ROM",
49 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
51 .name = "Adapter ROM",
54 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
56 .name = "Adapter ROM",
59 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
61 .name = "Adapter ROM",
64 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
66 .name = "Adapter ROM",
69 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
72 static struct resource video_rom_resource = {
76 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
79 /* does this oprom support the given pci device, or any of the devices
80 * that the driver supports?
82 static bool match_id(struct pci_dev *pdev, unsigned short vendor, unsigned short device)
84 struct pci_driver *drv = to_pci_driver(pdev->dev.driver);
85 const struct pci_device_id *id;
87 if (pdev->vendor == vendor && pdev->device == device)
90 for (id = drv ? drv->id_table : NULL; id && id->vendor; id++)
91 if (id->vendor == vendor && id->device == device)
94 return id && id->vendor;
97 static bool probe_list(struct pci_dev *pdev, unsigned short vendor,
100 unsigned short device;
103 if (get_kernel_nofault(device, rom_list) != 0)
106 if (device && match_id(pdev, vendor, device))
115 static struct resource *find_oprom(struct pci_dev *pdev)
117 struct resource *oprom = NULL;
120 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
121 struct resource *res = &adapter_rom_resources[i];
122 unsigned short offset, vendor, device, list, rev;
128 rom = isa_bus_to_virt(res->start);
129 if (get_kernel_nofault(offset, rom + 0x18) != 0)
132 if (get_kernel_nofault(vendor, rom + offset + 0x4) != 0)
135 if (get_kernel_nofault(device, rom + offset + 0x6) != 0)
138 if (match_id(pdev, vendor, device)) {
143 if (get_kernel_nofault(list, rom + offset + 0x8) == 0 &&
144 get_kernel_nofault(rev, rom + offset + 0xc) == 0 &&
146 probe_list(pdev, vendor, rom + offset + list)) {
155 void __iomem *pci_map_biosrom(struct pci_dev *pdev)
157 struct resource *oprom = find_oprom(pdev);
162 return ioremap(oprom->start, resource_size(oprom));
164 EXPORT_SYMBOL(pci_map_biosrom);
166 void pci_unmap_biosrom(void __iomem *image)
170 EXPORT_SYMBOL(pci_unmap_biosrom);
172 size_t pci_biosrom_size(struct pci_dev *pdev)
174 struct resource *oprom = find_oprom(pdev);
176 return oprom ? resource_size(oprom) : 0;
178 EXPORT_SYMBOL(pci_biosrom_size);
180 #define ROMSIGNATURE 0xaa55
182 static int __init romsignature(const unsigned char *rom)
184 const unsigned short * const ptr = (const unsigned short *)rom;
187 return get_kernel_nofault(sig, ptr) == 0 && sig == ROMSIGNATURE;
190 static int __init romchecksum(const unsigned char *rom, unsigned long length)
192 unsigned char sum, c;
194 for (sum = 0; length && get_kernel_nofault(c, rom++) == 0; length--)
196 return !length && !sum;
199 void __init probe_roms(void)
201 unsigned long start, length, upper;
202 const unsigned char *rom;
207 * The ROM memory range is not part of the e820 table and is therefore not
208 * pre-validated by BIOS. The kernel page table maps the ROM region as encrypted
209 * memory, and SNP requires encrypted memory to be validated before access.
212 snp_prep_memory(video_rom_resource.start,
213 ((system_rom_resource.end + 1) - video_rom_resource.start),
214 SNP_PAGE_STATE_PRIVATE);
217 upper = adapter_rom_resources[0].start;
218 for (start = video_rom_resource.start; start < upper; start += 2048) {
219 rom = isa_bus_to_virt(start);
220 if (!romsignature(rom))
223 video_rom_resource.start = start;
225 if (get_kernel_nofault(c, rom + 2) != 0)
228 /* 0 < length <= 0x7f * 512, historically */
231 /* if checksum okay, trust length byte */
232 if (length && romchecksum(rom, length))
233 video_rom_resource.end = start + length - 1;
235 request_resource(&iomem_resource, &video_rom_resource);
239 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
244 request_resource(&iomem_resource, &system_rom_resource);
245 upper = system_rom_resource.start;
247 /* check for extension rom (ignore length byte!) */
248 rom = isa_bus_to_virt(extension_rom_resource.start);
249 if (romsignature(rom)) {
250 length = resource_size(&extension_rom_resource);
251 if (romchecksum(rom, length)) {
252 request_resource(&iomem_resource, &extension_rom_resource);
253 upper = extension_rom_resource.start;
257 /* check for adapter roms on 2k boundaries */
258 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
259 rom = isa_bus_to_virt(start);
260 if (!romsignature(rom))
263 if (get_kernel_nofault(c, rom + 2) != 0)
266 /* 0 < length <= 0x7f * 512, historically */
269 /* but accept any length that fits if checksum okay */
270 if (!length || start + length > upper || !romchecksum(rom, length))
273 adapter_rom_resources[i].start = start;
274 adapter_rom_resources[i].end = start + length - 1;
275 request_resource(&iomem_resource, &adapter_rom_resources[i]);
277 start = adapter_rom_resources[i++].end & ~2047UL;