pcbios.c 12 KB

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