Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / probe_roms.c
1 #include <linux/sched.h>
2 #include <linux/mm.h>
3 #include <linux/uaccess.h>
4 #include <linux/mmzone.h>
5 #include <linux/ioport.h>
6 #include <linux/seq_file.h>
7 #include <linux/console.h>
8 #include <linux/init.h>
9 #include <linux/edd.h>
10 #include <linux/dmi.h>
11 #include <linux/pfn.h>
12 #include <linux/pci.h>
13 #include <linux/export.h>
14
15 #include <asm/probe_roms.h>
16 #include <asm/pci-direct.h>
17 #include <asm/e820.h>
18 #include <asm/mmzone.h>
19 #include <asm/setup.h>
20 #include <asm/sections.h>
21 #include <asm/io.h>
22 #include <asm/setup_arch.h>
23
24 static struct resource system_rom_resource = {
25         .name   = "System ROM",
26         .start  = 0xf0000,
27         .end    = 0xfffff,
28         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
29 };
30
31 static struct resource extension_rom_resource = {
32         .name   = "Extension ROM",
33         .start  = 0xe0000,
34         .end    = 0xeffff,
35         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
36 };
37
38 static struct resource adapter_rom_resources[] = { {
39         .name   = "Adapter ROM",
40         .start  = 0xc8000,
41         .end    = 0,
42         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
43 }, {
44         .name   = "Adapter ROM",
45         .start  = 0,
46         .end    = 0,
47         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
48 }, {
49         .name   = "Adapter ROM",
50         .start  = 0,
51         .end    = 0,
52         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
53 }, {
54         .name   = "Adapter ROM",
55         .start  = 0,
56         .end    = 0,
57         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
58 }, {
59         .name   = "Adapter ROM",
60         .start  = 0,
61         .end    = 0,
62         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
63 }, {
64         .name   = "Adapter ROM",
65         .start  = 0,
66         .end    = 0,
67         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
68 } };
69
70 static struct resource video_rom_resource = {
71         .name   = "Video ROM",
72         .start  = 0xc0000,
73         .end    = 0xc7fff,
74         .flags  = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
75 };
76
77 /* does this oprom support the given pci device, or any of the devices
78  * that the driver supports?
79  */
80 static bool match_id(struct pci_dev *pdev, unsigned short vendor, unsigned short device)
81 {
82         struct pci_driver *drv = pdev->driver;
83         const struct pci_device_id *id;
84
85         if (pdev->vendor == vendor && pdev->device == device)
86                 return true;
87
88         for (id = drv ? drv->id_table : NULL; id && id->vendor; id++)
89                 if (id->vendor == vendor && id->device == device)
90                         break;
91
92         return id && id->vendor;
93 }
94
95 static bool probe_list(struct pci_dev *pdev, unsigned short vendor,
96                        const unsigned char *rom_list)
97 {
98         unsigned short device;
99
100         do {
101                 if (probe_kernel_address(rom_list, device) != 0)
102                         device = 0;
103
104                 if (device && match_id(pdev, vendor, device))
105                         break;
106
107                 rom_list += 2;
108         } while (device);
109
110         return !!device;
111 }
112
113 static struct resource *find_oprom(struct pci_dev *pdev)
114 {
115         struct resource *oprom = NULL;
116         int i;
117
118 #ifdef CONFIG_XEN
119         if (!is_initial_xendomain())
120                 return NULL;
121 #endif
122
123         for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
124                 struct resource *res = &adapter_rom_resources[i];
125                 unsigned short offset, vendor, device, list, rev;
126                 const unsigned char *rom;
127
128                 if (res->end == 0)
129                         break;
130
131                 rom = isa_bus_to_virt(res->start);
132                 if (probe_kernel_address(rom + 0x18, offset) != 0)
133                         continue;
134
135                 if (probe_kernel_address(rom + offset + 0x4, vendor) != 0)
136                         continue;
137
138                 if (probe_kernel_address(rom + offset + 0x6, device) != 0)
139                         continue;
140
141                 if (match_id(pdev, vendor, device)) {
142                         oprom = res;
143                         break;
144                 }
145
146                 if (probe_kernel_address(rom + offset + 0x8, list) == 0 &&
147                     probe_kernel_address(rom + offset + 0xc, rev) == 0 &&
148                     rev >= 3 && list &&
149                     probe_list(pdev, vendor, rom + offset + list)) {
150                         oprom = res;
151                         break;
152                 }
153         }
154
155         return oprom;
156 }
157
158 void *pci_map_biosrom(struct pci_dev *pdev)
159 {
160         struct resource *oprom = find_oprom(pdev);
161
162         if (!oprom)
163                 return NULL;
164
165         return ioremap(oprom->start, resource_size(oprom));
166 }
167 EXPORT_SYMBOL(pci_map_biosrom);
168
169 void pci_unmap_biosrom(void __iomem *image)
170 {
171         iounmap(image);
172 }
173 EXPORT_SYMBOL(pci_unmap_biosrom);
174
175 size_t pci_biosrom_size(struct pci_dev *pdev)
176 {
177         struct resource *oprom = find_oprom(pdev);
178
179         return oprom ? resource_size(oprom) : 0;
180 }
181 EXPORT_SYMBOL(pci_biosrom_size);
182
183 #define ROMSIGNATURE 0xaa55
184
185 static int __init romsignature(const unsigned char *rom)
186 {
187         const unsigned short * const ptr = (const unsigned short *)rom;
188         unsigned short sig;
189
190         return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
191 }
192
193 static int __init romchecksum(const unsigned char *rom, unsigned long length)
194 {
195         unsigned char sum, c;
196
197         for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
198                 sum += c;
199         return !length && !sum;
200 }
201
202 void __init probe_roms(void)
203 {
204         const unsigned char *rom;
205         unsigned long start, length, upper;
206         unsigned char c;
207         int i;
208
209         /* video rom */
210         upper = adapter_rom_resources[0].start;
211         for (start = video_rom_resource.start; start < upper; start += 2048) {
212                 rom = isa_bus_to_virt(start);
213                 if (!romsignature(rom))
214                         continue;
215
216                 video_rom_resource.start = start;
217
218                 if (probe_kernel_address(rom + 2, c) != 0)
219                         continue;
220
221                 /* 0 < length <= 0x7f * 512, historically */
222                 length = c * 512;
223
224                 /* if checksum okay, trust length byte */
225                 if (length && romchecksum(rom, length))
226                         video_rom_resource.end = start + length - 1;
227
228                 request_resource(&iomem_resource, &video_rom_resource);
229                 break;
230         }
231
232         start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
233         if (start < upper)
234                 start = upper;
235
236         /* system rom */
237         request_resource(&iomem_resource, &system_rom_resource);
238         upper = system_rom_resource.start;
239
240         /* check for extension rom (ignore length byte!) */
241         rom = isa_bus_to_virt((unsigned long)extension_rom_resource.start);
242         if (romsignature(rom)) {
243                 length = resource_size(&extension_rom_resource);
244                 if (romchecksum(rom, length)) {
245                         request_resource(&iomem_resource, &extension_rom_resource);
246                         upper = extension_rom_resource.start;
247                 }
248         }
249
250         /* check for adapter roms on 2k boundaries */
251         for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
252                 rom = isa_bus_to_virt(start);
253                 if (!romsignature(rom))
254                         continue;
255
256                 if (probe_kernel_address(rom + 2, c) != 0)
257                         continue;
258
259                 /* 0 < length <= 0x7f * 512, historically */
260                 length = c * 512;
261
262                 /* but accept any length that fits if checksum okay */
263                 if (!length || start + length > upper || !romchecksum(rom, length))
264                         continue;
265
266                 adapter_rom_resources[i].start = start;
267                 adapter_rom_resources[i].end = start + length - 1;
268                 request_resource(&iomem_resource, &adapter_rom_resources[i]);
269
270                 start = adapter_rom_resources[i++].end & ~2047UL;
271         }
272 }
273