[PATCH] Add a section about inlining to Documentation/CodingStyle
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / pci / pcbios.c
blobb9d65f0bc2d161411322d5a94d1456919f66b81f
1 /*
2 * BIOS32 and PCI BIOS handling.
3 */
5 #include <linux/pci.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include "pci.h"
9 #include "pci-functions.h"
12 /* BIOS32 signature: "_32_" */
13 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
15 /* PCI signature: "PCI " */
16 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
18 /* PCI service signature: "$PCI" */
19 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
21 /* PCI BIOS hardware mechanism flags */
22 #define PCIBIOS_HW_TYPE1 0x01
23 #define PCIBIOS_HW_TYPE2 0x02
24 #define PCIBIOS_HW_TYPE1_SPEC 0x10
25 #define PCIBIOS_HW_TYPE2_SPEC 0x20
28 * This is the standard structure used to identify the entry point
29 * to the BIOS32 Service Directory, as documented in
30 * Standard BIOS 32-bit Service Directory Proposal
31 * Revision 0.4 May 24, 1993
32 * Phoenix Technologies Ltd.
33 * Norwood, MA
34 * and the PCI BIOS specification.
37 union bios32 {
38 struct {
39 unsigned long signature; /* _32_ */
40 unsigned long entry; /* 32 bit physical address */
41 unsigned char revision; /* Revision level, 0 */
42 unsigned char length; /* Length in paragraphs should be 01 */
43 unsigned char checksum; /* All bytes must add up to zero */
44 unsigned char reserved[5]; /* Must be zero */
45 } fields;
46 char chars[16];
50 * Physical address of the service directory. I don't know if we're
51 * allowed to have more than one of these or not, so just in case
52 * we'll make pcibios_present() take a memory start parameter and store
53 * the array there.
56 static struct {
57 unsigned long address;
58 unsigned short segment;
59 } bios32_indirect = { 0, __KERNEL_CS };
62 * Returns the entry point for the given service, NULL on error
65 static unsigned long bios32_service(unsigned long service)
67 unsigned char return_code; /* %al */
68 unsigned long address; /* %ebx */
69 unsigned long length; /* %ecx */
70 unsigned long entry; /* %edx */
71 unsigned long flags;
73 local_irq_save(flags);
74 __asm__("lcall *(%%edi); cld"
75 : "=a" (return_code),
76 "=b" (address),
77 "=c" (length),
78 "=d" (entry)
79 : "0" (service),
80 "1" (0),
81 "D" (&bios32_indirect));
82 local_irq_restore(flags);
84 switch (return_code) {
85 case 0:
86 return address + entry;
87 case 0x80: /* Not present */
88 printk(KERN_WARNING "bios32_service(0x%lx): not present\n", service);
89 return 0;
90 default: /* Shouldn't happen */
91 printk(KERN_WARNING "bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
92 service, return_code);
93 return 0;
97 static struct {
98 unsigned long address;
99 unsigned short segment;
100 } pci_indirect = { 0, __KERNEL_CS };
102 static int pci_bios_present;
104 static int __devinit check_pcibios(void)
106 u32 signature, eax, ebx, ecx;
107 u8 status, major_ver, minor_ver, hw_mech;
108 unsigned long flags, pcibios_entry;
110 if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
111 pci_indirect.address = pcibios_entry + PAGE_OFFSET;
113 local_irq_save(flags);
114 __asm__(
115 "lcall *(%%edi); cld\n\t"
116 "jc 1f\n\t"
117 "xor %%ah, %%ah\n"
118 "1:"
119 : "=d" (signature),
120 "=a" (eax),
121 "=b" (ebx),
122 "=c" (ecx)
123 : "1" (PCIBIOS_PCI_BIOS_PRESENT),
124 "D" (&pci_indirect)
125 : "memory");
126 local_irq_restore(flags);
128 status = (eax >> 8) & 0xff;
129 hw_mech = eax & 0xff;
130 major_ver = (ebx >> 8) & 0xff;
131 minor_ver = ebx & 0xff;
132 if (pcibios_last_bus < 0)
133 pcibios_last_bus = ecx & 0xff;
134 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
135 status, hw_mech, major_ver, minor_ver, pcibios_last_bus);
136 if (status || signature != PCI_SIGNATURE) {
137 printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found\n",
138 status, signature);
139 return 0;
141 printk(KERN_INFO "PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
142 major_ver, minor_ver, pcibios_entry, pcibios_last_bus);
143 #ifdef CONFIG_PCI_DIRECT
144 if (!(hw_mech & PCIBIOS_HW_TYPE1))
145 pci_probe &= ~PCI_PROBE_CONF1;
146 if (!(hw_mech & PCIBIOS_HW_TYPE2))
147 pci_probe &= ~PCI_PROBE_CONF2;
148 #endif
149 return 1;
151 return 0;
154 static int __devinit pci_bios_find_device (unsigned short vendor, unsigned short device_id,
155 unsigned short index, unsigned char *bus, unsigned char *device_fn)
157 unsigned short bx;
158 unsigned short ret;
160 __asm__("lcall *(%%edi); cld\n\t"
161 "jc 1f\n\t"
162 "xor %%ah, %%ah\n"
163 "1:"
164 : "=b" (bx),
165 "=a" (ret)
166 : "1" (PCIBIOS_FIND_PCI_DEVICE),
167 "c" (device_id),
168 "d" (vendor),
169 "S" ((int) index),
170 "D" (&pci_indirect));
171 *bus = (bx >> 8) & 0xff;
172 *device_fn = bx & 0xff;
173 return (int) (ret & 0xff00) >> 8;
176 static int pci_bios_read(unsigned int seg, unsigned int bus,
177 unsigned int devfn, int reg, int len, u32 *value)
179 unsigned long result = 0;
180 unsigned long flags;
181 unsigned long bx = (bus << 8) | devfn;
183 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
184 return -EINVAL;
186 spin_lock_irqsave(&pci_config_lock, flags);
188 switch (len) {
189 case 1:
190 __asm__("lcall *(%%esi); cld\n\t"
191 "jc 1f\n\t"
192 "xor %%ah, %%ah\n"
193 "1:"
194 : "=c" (*value),
195 "=a" (result)
196 : "1" (PCIBIOS_READ_CONFIG_BYTE),
197 "b" (bx),
198 "D" ((long)reg),
199 "S" (&pci_indirect));
200 break;
201 case 2:
202 __asm__("lcall *(%%esi); cld\n\t"
203 "jc 1f\n\t"
204 "xor %%ah, %%ah\n"
205 "1:"
206 : "=c" (*value),
207 "=a" (result)
208 : "1" (PCIBIOS_READ_CONFIG_WORD),
209 "b" (bx),
210 "D" ((long)reg),
211 "S" (&pci_indirect));
212 break;
213 case 4:
214 __asm__("lcall *(%%esi); cld\n\t"
215 "jc 1f\n\t"
216 "xor %%ah, %%ah\n"
217 "1:"
218 : "=c" (*value),
219 "=a" (result)
220 : "1" (PCIBIOS_READ_CONFIG_DWORD),
221 "b" (bx),
222 "D" ((long)reg),
223 "S" (&pci_indirect));
224 break;
227 spin_unlock_irqrestore(&pci_config_lock, flags);
229 return (int)((result & 0xff00) >> 8);
232 static int pci_bios_write(unsigned int seg, unsigned int bus,
233 unsigned int devfn, int reg, int len, u32 value)
235 unsigned long result = 0;
236 unsigned long flags;
237 unsigned long bx = (bus << 8) | devfn;
239 if ((bus > 255) || (devfn > 255) || (reg > 255))
240 return -EINVAL;
242 spin_lock_irqsave(&pci_config_lock, flags);
244 switch (len) {
245 case 1:
246 __asm__("lcall *(%%esi); cld\n\t"
247 "jc 1f\n\t"
248 "xor %%ah, %%ah\n"
249 "1:"
250 : "=a" (result)
251 : "0" (PCIBIOS_WRITE_CONFIG_BYTE),
252 "c" (value),
253 "b" (bx),
254 "D" ((long)reg),
255 "S" (&pci_indirect));
256 break;
257 case 2:
258 __asm__("lcall *(%%esi); cld\n\t"
259 "jc 1f\n\t"
260 "xor %%ah, %%ah\n"
261 "1:"
262 : "=a" (result)
263 : "0" (PCIBIOS_WRITE_CONFIG_WORD),
264 "c" (value),
265 "b" (bx),
266 "D" ((long)reg),
267 "S" (&pci_indirect));
268 break;
269 case 4:
270 __asm__("lcall *(%%esi); cld\n\t"
271 "jc 1f\n\t"
272 "xor %%ah, %%ah\n"
273 "1:"
274 : "=a" (result)
275 : "0" (PCIBIOS_WRITE_CONFIG_DWORD),
276 "c" (value),
277 "b" (bx),
278 "D" ((long)reg),
279 "S" (&pci_indirect));
280 break;
283 spin_unlock_irqrestore(&pci_config_lock, flags);
285 return (int)((result & 0xff00) >> 8);
290 * Function table for BIOS32 access
293 static struct pci_raw_ops pci_bios_access = {
294 .read = pci_bios_read,
295 .write = pci_bios_write
299 * Try to find PCI BIOS.
302 static struct pci_raw_ops * __devinit pci_find_bios(void)
304 union bios32 *check;
305 unsigned char sum;
306 int i, length;
309 * Follow the standard procedure for locating the BIOS32 Service
310 * directory by scanning the permissible address range from
311 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
314 for (check = (union bios32 *) __va(0xe0000);
315 check <= (union bios32 *) __va(0xffff0);
316 ++check) {
317 if (check->fields.signature != BIOS32_SIGNATURE)
318 continue;
319 length = check->fields.length * 16;
320 if (!length)
321 continue;
322 sum = 0;
323 for (i = 0; i < length ; ++i)
324 sum += check->chars[i];
325 if (sum != 0)
326 continue;
327 if (check->fields.revision != 0) {
328 printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
329 check->fields.revision, check);
330 continue;
332 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
333 if (check->fields.entry >= 0x100000) {
334 printk("PCI: BIOS32 entry (0x%p) in high memory, cannot use.\n", check);
335 return NULL;
336 } else {
337 unsigned long bios32_entry = check->fields.entry;
338 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n", bios32_entry);
339 bios32_indirect.address = bios32_entry + PAGE_OFFSET;
340 if (check_pcibios())
341 return &pci_bios_access;
343 break; /* Hopefully more than one BIOS32 cannot happen... */
346 return NULL;
350 * Sort the device list according to PCI BIOS. Nasty hack, but since some
351 * fool forgot to define the `correct' device order in the PCI BIOS specs
352 * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
353 * which used BIOS ordering, we are bound to do this...
356 void __devinit pcibios_sort(void)
358 LIST_HEAD(sorted_devices);
359 struct list_head *ln;
360 struct pci_dev *dev, *d;
361 int idx, found;
362 unsigned char bus, devfn;
364 DBG("PCI: Sorting device list...\n");
365 while (!list_empty(&pci_devices)) {
366 ln = pci_devices.next;
367 dev = pci_dev_g(ln);
368 idx = found = 0;
369 while (pci_bios_find_device(dev->vendor, dev->device, idx, &bus, &devfn) == PCIBIOS_SUCCESSFUL) {
370 idx++;
371 list_for_each(ln, &pci_devices) {
372 d = pci_dev_g(ln);
373 if (d->bus->number == bus && d->devfn == devfn) {
374 list_del(&d->global_list);
375 list_add_tail(&d->global_list, &sorted_devices);
376 if (d == dev)
377 found = 1;
378 break;
381 if (ln == &pci_devices) {
382 printk(KERN_WARNING "PCI: BIOS reporting unknown device %02x:%02x\n", bus, devfn);
384 * We must not continue scanning as several buggy BIOSes
385 * return garbage after the last device. Grr.
387 break;
390 if (!found) {
391 printk(KERN_WARNING "PCI: Device %s not found by BIOS\n",
392 pci_name(dev));
393 list_del(&dev->global_list);
394 list_add_tail(&dev->global_list, &sorted_devices);
397 list_splice(&sorted_devices, &pci_devices);
401 * BIOS Functions for IRQ Routing
404 struct irq_routing_options {
405 u16 size;
406 struct irq_info *table;
407 u16 segment;
408 } __attribute__((packed));
410 struct irq_routing_table * __devinit pcibios_get_irq_routing_table(void)
412 struct irq_routing_options opt;
413 struct irq_routing_table *rt = NULL;
414 int ret, map;
415 unsigned long page;
417 if (!pci_bios_present)
418 return NULL;
419 page = __get_free_page(GFP_KERNEL);
420 if (!page)
421 return NULL;
422 opt.table = (struct irq_info *) page;
423 opt.size = PAGE_SIZE;
424 opt.segment = __KERNEL_DS;
426 DBG("PCI: Fetching IRQ routing table... ");
427 __asm__("push %%es\n\t"
428 "push %%ds\n\t"
429 "pop %%es\n\t"
430 "lcall *(%%esi); cld\n\t"
431 "pop %%es\n\t"
432 "jc 1f\n\t"
433 "xor %%ah, %%ah\n"
434 "1:"
435 : "=a" (ret),
436 "=b" (map),
437 "=m" (opt)
438 : "0" (PCIBIOS_GET_ROUTING_OPTIONS),
439 "1" (0),
440 "D" ((long) &opt),
441 "S" (&pci_indirect),
442 "m" (opt)
443 : "memory");
444 DBG("OK ret=%d, size=%d, map=%x\n", ret, opt.size, map);
445 if (ret & 0xff00)
446 printk(KERN_ERR "PCI: Error %02x when fetching IRQ routing table.\n", (ret >> 8) & 0xff);
447 else if (opt.size) {
448 rt = kmalloc(sizeof(struct irq_routing_table) + opt.size, GFP_KERNEL);
449 if (rt) {
450 memset(rt, 0, sizeof(struct irq_routing_table));
451 rt->size = opt.size + sizeof(struct irq_routing_table);
452 rt->exclusive_irqs = map;
453 memcpy(rt->slots, (void *) page, opt.size);
454 printk(KERN_INFO "PCI: Using BIOS Interrupt Routing Table\n");
457 free_page(page);
458 return rt;
460 EXPORT_SYMBOL(pcibios_get_irq_routing_table);
462 int pcibios_set_irq_routing(struct pci_dev *dev, int pin, int irq)
464 int ret;
466 __asm__("lcall *(%%esi); cld\n\t"
467 "jc 1f\n\t"
468 "xor %%ah, %%ah\n"
469 "1:"
470 : "=a" (ret)
471 : "0" (PCIBIOS_SET_PCI_HW_INT),
472 "b" ((dev->bus->number << 8) | dev->devfn),
473 "c" ((irq << 8) | (pin + 10)),
474 "S" (&pci_indirect));
475 return !(ret & 0xff00);
477 EXPORT_SYMBOL(pcibios_set_irq_routing);
479 static int __init pci_pcbios_init(void)
481 if ((pci_probe & PCI_PROBE_BIOS)
482 && ((raw_pci_ops = pci_find_bios()))) {
483 pci_probe |= PCI_BIOS_SORT;
484 pci_bios_present = 1;
486 return 0;
489 arch_initcall(pci_pcbios_init);