pcbios.c 12 KB

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