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