2 * probe_roms - scan for Adapter ROMS
4 * (based on linux-2.6:arch/x86/kernel/probe_roms_32.c)
6 * Copyright (C) 2008 Intel Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "probe_roms.h"
29 #include <sys/types.h>
30 #include <asm/types.h>
32 static void *rom_mem = MAP_FAILED;
33 static int rom_fd = -1;
34 static const int rom_len = 0xf0000 - 0xc0000; /* option-rom memory region */
36 static unsigned long rom_align;
38 static void sigbus(int sig)
43 static int probe_address8(const __u8 *ptr, __u8 *val)
55 static int probe_address16(const __u16 *ptr, __u16 *val)
67 void probe_roms_exit(void)
69 signal(SIGBUS, SIG_DFL);
74 if (rom_mem != MAP_FAILED) {
75 munmap(rom_mem, rom_len);
80 int probe_roms_init(unsigned long align)
85 /* valid values are 2048 and 512. 512 is for PCI-3.0 compliant
86 * systems, or systems that do not have dangerous/legacy ISA
87 * devices. 2048 should always be safe
89 if (align == 512 || align == 2048)
94 if (signal(SIGBUS, sigbus) == SIG_ERR)
97 fd = open("/dev/mem", O_RDONLY);
102 rom_mem = mmap(NULL, rom_len, PROT_READ, MAP_PRIVATE, fd, 0xc0000);
103 if (rom_mem == MAP_FAILED)
118 * isa_bus_to_virt - convert physical address to mmap'd region
119 * @addr - address to convert
121 * Only valid between a successful call to probe_roms_init and the
122 * corresponding probe_roms_exit
124 static void *isa_bus_to_virt(unsigned long addr)
126 return rom_mem + (addr - 0xc0000);
136 static struct resource system_rom_resource = {
137 .name = "System ROM",
143 static struct resource extension_rom_resource = {
144 .name = "Extension ROM",
150 static struct resource adapter_rom_resources[] = { {
151 .name = "Adapter ROM",
156 .name = "Adapter ROM",
161 .name = "Adapter ROM",
166 .name = "Adapter ROM",
171 .name = "Adapter ROM",
176 .name = "Adapter ROM",
182 static struct resource video_rom_resource = {
189 #define ROMSIGNATURE 0xaa55
191 static int romsignature(const unsigned char *rom)
193 const unsigned short * const ptr = (const unsigned short *)rom;
194 unsigned short sig = 0;
196 return probe_address16(ptr, &sig) == 0 && sig == ROMSIGNATURE;
199 static int romchecksum(const unsigned char *rom, unsigned long length)
201 unsigned char sum, c;
203 for (sum = 0; length && probe_address8(rom++, &c) == 0; length--)
205 return !length && !sum;
208 int scan_adapter_roms(scan_fn fn)
210 /* let scan_fn examing each of the adapter roms found by probe_roms */
218 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
219 struct resource *res = &adapter_rom_resources[i];
222 found = fn(isa_bus_to_virt(res->start),
223 isa_bus_to_virt(res->end),
224 isa_bus_to_virt(res->data));
234 static unsigned long align(unsigned long addr, unsigned long alignment)
236 return (addr + alignment - 1) & ~(alignment - 1);
239 void probe_roms(void)
242 unsigned long start, length, upper;
251 upper = adapter_rom_resources[0].start;
252 for (start = video_rom_resource.start; start < upper; start += rom_align) {
253 rom = isa_bus_to_virt(start);
254 if (!romsignature(rom))
257 video_rom_resource.start = start;
259 if (probe_address8(rom + 2, &c) != 0)
262 /* 0 < length <= 0x7f * 512, historically */
265 /* if checksum okay, trust length byte */
266 if (length && romchecksum(rom, length))
267 video_rom_resource.end = start + length - 1;
271 start = align(video_rom_resource.end + 1, rom_align);
276 upper = system_rom_resource.start;
278 /* check for extension rom (ignore length byte!) */
279 rom = isa_bus_to_virt(extension_rom_resource.start);
280 if (romsignature(rom)) {
281 length = extension_rom_resource.end - extension_rom_resource.start + 1;
282 if (romchecksum(rom, length))
283 upper = extension_rom_resource.start;
286 /* check for adapter roms on 2k boundaries */
287 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += rom_align) {
288 rom = isa_bus_to_virt(start);
289 if (!romsignature(rom))
292 if (probe_address8(rom + 2, &c) != 0)
295 /* 0 < length <= 0x7f * 512, historically */
298 /* Retrieve 16-bit pointer to PCI Data Structure (offset 18h-19h)
299 * The data can be within 64KB forward of the first location
300 * of this code image. The pointer is in little-endian order
303 if (probe_address16(rom + 0x18, &val) != 0)
305 val = __le16_to_cpu(val);
307 /* but accept any length that fits if checksum okay */
308 if (!length || start + length > upper || !romchecksum(rom, length))
311 adapter_rom_resources[i].start = start;
312 adapter_rom_resources[i].data = start + (unsigned long) val;
313 adapter_rom_resources[i].end = start + length - 1;
315 start = adapter_rom_resources[i++].end & ~(rom_align - 1);