pcbios.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * BIOS32 and PCI BIOS handling.
  3. */
  4. #include <linux/pci.h>
  5. #include <linux/init.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/pci_x86.h>
  10. #include <asm/pci-functions.h>
  11. #include <asm/cacheflush.h>
  12. /* BIOS32 signature: "_32_" */
  13. #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
  14. /* PCI signature: "PCI " */
  15. #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
  16. /* PCI service signature: "$PCI" */
  17. #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
  18. /* PCI BIOS hardware mechanism flags */
  19. #define PCIBIOS_HW_TYPE1 0x01
  20. #define PCIBIOS_HW_TYPE2 0x02
  21. #define PCIBIOS_HW_TYPE1_SPEC 0x10
  22. #define PCIBIOS_HW_TYPE2_SPEC 0x20
  23. int pcibios_enabled;
  24. /* According to the BIOS specification at:
  25. * http://members.datafast.net.au/dft0802/specs/bios21.pdf, we could
  26. * restrict the x zone to some pages and make it ro. But this may be
  27. * broken on some bios, complex to handle with static_protections.
  28. * We could make the 0xe0000-0x100000 range rox, but this can break
  29. * some ISA mapping.
  30. *
  31. * So we let's an rw and x hole when pcibios is used. This shouldn't
  32. * happen for modern system with mmconfig, and if you don't want it
  33. * you could disable pcibios...
  34. */
  35. static inline void set_bios_x(void)
  36. {
  37. pcibios_enabled = 1;
  38. set_memory_x(PAGE_OFFSET + BIOS_BEGIN, (BIOS_END - BIOS_BEGIN) >> PAGE_SHIFT);
  39. if (__supported_pte_mask & _PAGE_NX)
  40. printk(KERN_INFO "PCI : PCI BIOS aera is rw and x. Use pci=nobios if you want it NX.\n");
  41. }
  42. /*
  43. * This is the standard structure used to identify the entry point
  44. * to the BIOS32 Service Directory, as documented in
  45. * Standard BIOS 32-bit Service Directory Proposal
  46. * Revision 0.4 May 24, 1993
  47. * Phoenix Technologies Ltd.
  48. * Norwood, MA
  49. * and the PCI BIOS specification.
  50. */
  51. union bios32 {
  52. struct {
  53. unsigned long signature; /* _32_ */
  54. unsigned long entry; /* 32 bit physical address */
  55. unsigned char revision; /* Revision level, 0 */
  56. unsigned char length; /* Length in paragraphs should be 01 */
  57. unsigned char checksum; /* All bytes must add up to zero */
  58. unsigned char reserved[5]; /* Must be zero */
  59. } fields;
  60. char chars[16];
  61. };
  62. /*
  63. * Physical address of the service directory. I don't know if we're
  64. * allowed to have more than one of these or not, so just in case
  65. * we'll make pcibios_present() take a memory start parameter and store
  66. * the array there.
  67. */
  68. static struct {
  69. unsigned long address;
  70. unsigned short segment;
  71. } bios32_indirect = { 0, __KERNEL_CS };
  72. /*
  73. * Returns the entry point for the given service, NULL on error
  74. */
  75. static unsigned long bios32_service(unsigned long service)
  76. {
  77. unsigned char return_code; /* %al */
  78. unsigned long address; /* %ebx */
  79. unsigned long length; /* %ecx */
  80. unsigned long entry; /* %edx */
  81. unsigned long flags;
  82. local_irq_save(flags);
  83. __asm__("lcall *(%%edi); cld"
  84. : "=a" (return_code),
  85. "=b" (address),
  86. "=c" (length),
  87. "=d" (entry)
  88. : "0" (service),
  89. "1" (0),
  90. "D" (&bios32_indirect));
  91. local_irq_restore(flags);
  92. switch (return_code) {
  93. case 0:
  94. return address + entry;
  95. case 0x80: /* Not present */
  96. printk(KERN_WARNING "bios32_service(0x%lx): not present\n", service);
  97. return 0;
  98. default: /* Shouldn't happen */
  99. printk(KERN_WARNING "bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
  100. service, return_code);
  101. return 0;
  102. }
  103. }
  104. static struct {
  105. unsigned long address;
  106. unsigned short segment;
  107. } pci_indirect = { 0, __KERNEL_CS };
  108. static int pci_bios_present;
  109. static int __devinit check_pcibios(void)
  110. {
  111. u32 signature, eax, ebx, ecx;
  112. u8 status, major_ver, minor_ver, hw_mech;
  113. unsigned long flags, pcibios_entry;
  114. if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
  115. pci_indirect.address = pcibios_entry + PAGE_OFFSET;
  116. local_irq_save(flags);
  117. __asm__(
  118. "lcall *(%%edi); cld\n\t"
  119. "jc 1f\n\t"
  120. "xor %%ah, %%ah\n"
  121. "1:"
  122. : "=d" (signature),
  123. "=a" (eax),
  124. "=b" (ebx),
  125. "=c" (ecx)
  126. : "1" (PCIBIOS_PCI_BIOS_PRESENT),
  127. "D" (&pci_indirect)
  128. : "memory");
  129. local_irq_restore(flags);
  130. status = (eax >> 8) & 0xff;
  131. hw_mech = eax & 0xff;
  132. major_ver = (ebx >> 8) & 0xff;
  133. minor_ver = ebx & 0xff;
  134. if (pcibios_last_bus < 0)
  135. pcibios_last_bus = ecx & 0xff;
  136. DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
  137. status, hw_mech, major_ver, minor_ver, pcibios_last_bus);
  138. if (status || signature != PCI_SIGNATURE) {
  139. printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found\n",
  140. status, signature);
  141. return 0;
  142. }
  143. printk(KERN_INFO "PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
  144. major_ver, minor_ver, pcibios_entry, pcibios_last_bus);
  145. #ifdef CONFIG_PCI_DIRECT
  146. if (!(hw_mech & PCIBIOS_HW_TYPE1))
  147. pci_probe &= ~PCI_PROBE_CONF1;
  148. if (!(hw_mech & PCIBIOS_HW_TYPE2))
  149. pci_probe &= ~PCI_PROBE_CONF2;
  150. #endif
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. static int pci_bios_read(unsigned int seg, unsigned int bus,
  156. unsigned int devfn, int reg, int len, u32 *value)
  157. {
  158. unsigned long result = 0;
  159. unsigned long flags;
  160. unsigned long bx = (bus << 8) | devfn;
  161. if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
  162. return -EINVAL;
  163. raw_spin_lock_irqsave(&pci_config_lock, flags);
  164. switch (len) {
  165. case 1:
  166. __asm__("lcall *(%%esi); cld\n\t"
  167. "jc 1f\n\t"
  168. "xor %%ah, %%ah\n"
  169. "1:"
  170. : "=c" (*value),
  171. "=a" (result)
  172. : "1" (PCIBIOS_READ_CONFIG_BYTE),
  173. "b" (bx),
  174. "D" ((long)reg),
  175. "S" (&pci_indirect));
  176. /*
  177. * Zero-extend the result beyond 8 bits, do not trust the
  178. * BIOS having done it:
  179. */
  180. *value &= 0xff;
  181. break;
  182. case 2:
  183. __asm__("lcall *(%%esi); cld\n\t"
  184. "jc 1f\n\t"
  185. "xor %%ah, %%ah\n"
  186. "1:"
  187. : "=c" (*value),
  188. "=a" (result)
  189. : "1" (PCIBIOS_READ_CONFIG_WORD),
  190. "b" (bx),
  191. "D" ((long)reg),
  192. "S" (&pci_indirect));
  193. /*
  194. * Zero-extend the result beyond 16 bits, do not trust the
  195. * BIOS having done it:
  196. */
  197. *value &= 0xffff;
  198. break;
  199. case 4:
  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_DWORD),
  207. "b" (bx),
  208. "D" ((long)reg),
  209. "S" (&pci_indirect));
  210. break;
  211. }
  212. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  213. return (int)((result & 0xff00) >> 8);
  214. }
  215. static int pci_bios_write(unsigned int seg, unsigned int bus,
  216. unsigned int devfn, int reg, int len, u32 value)
  217. {
  218. unsigned long result = 0;
  219. unsigned long flags;
  220. unsigned long bx = (bus << 8) | devfn;
  221. if ((bus > 255) || (devfn > 255) || (reg > 255))
  222. return -EINVAL;
  223. raw_spin_lock_irqsave(&pci_config_lock, flags);
  224. switch (len) {
  225. case 1:
  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_BYTE),
  232. "c" (value),
  233. "b" (bx),
  234. "D" ((long)reg),
  235. "S" (&pci_indirect));
  236. break;
  237. case 2:
  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_WORD),
  244. "c" (value),
  245. "b" (bx),
  246. "D" ((long)reg),
  247. "S" (&pci_indirect));
  248. break;
  249. case 4:
  250. __asm__("lcall *(%%esi); cld\n\t"
  251. "jc 1f\n\t"
  252. "xor %%ah, %%ah\n"
  253. "1:"
  254. : "=a" (result)
  255. : "0" (PCIBIOS_WRITE_CONFIG_DWORD),
  256. "c" (value),
  257. "b" (bx),
  258. "D" ((long)reg),
  259. "S" (&pci_indirect));
  260. break;
  261. }
  262. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  263. return (int)((result & 0xff00) >> 8);
  264. }
  265. /*
  266. * Function table for BIOS32 access
  267. */
  268. static struct pci_raw_ops pci_bios_access = {
  269. .read = pci_bios_read,
  270. .write = pci_bios_write
  271. };
  272. /*
  273. * Try to find PCI BIOS.
  274. */
  275. static struct pci_raw_ops * __devinit pci_find_bios(void)
  276. {
  277. union bios32 *check;
  278. unsigned char sum;
  279. int i, length;
  280. /*
  281. * Follow the standard procedure for locating the BIOS32 Service
  282. * directory by scanning the permissible address range from
  283. * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  284. */
  285. for (check = (union bios32 *) __va(0xe0000);
  286. check <= (union bios32 *) __va(0xffff0);
  287. ++check) {
  288. long sig;
  289. if (probe_kernel_address(&check->fields.signature, sig))
  290. continue;
  291. if (check->fields.signature != BIOS32_SIGNATURE)
  292. continue;
  293. length = check->fields.length * 16;
  294. if (!length)
  295. continue;
  296. sum = 0;
  297. for (i = 0; i < length ; ++i)
  298. sum += check->chars[i];
  299. if (sum != 0)
  300. continue;
  301. if (check->fields.revision != 0) {
  302. printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
  303. check->fields.revision, check);
  304. continue;
  305. }
  306. DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
  307. if (check->fields.entry >= 0x100000) {
  308. printk("PCI: BIOS32 entry (0x%p) in high memory, "
  309. "cannot use.\n", check);
  310. return NULL;
  311. } else {
  312. unsigned long bios32_entry = check->fields.entry;
  313. DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n",
  314. bios32_entry);
  315. bios32_indirect.address = bios32_entry + PAGE_OFFSET;
  316. set_bios_x();
  317. if (check_pcibios())
  318. return &pci_bios_access;
  319. }
  320. break; /* Hopefully more than one BIOS32 cannot happen... */
  321. }
  322. return NULL;
  323. }
  324. /*
  325. * BIOS Functions for IRQ Routing
  326. */
  327. struct irq_routing_options {
  328. u16 size;
  329. struct irq_info *table;
  330. u16 segment;
  331. } __attribute__((packed));
  332. struct irq_routing_table * pcibios_get_irq_routing_table(void)
  333. {
  334. struct irq_routing_options opt;
  335. struct irq_routing_table *rt = NULL;
  336. int ret, map;
  337. unsigned long page;
  338. if (!pci_bios_present)
  339. return NULL;
  340. page = __get_free_page(GFP_KERNEL);
  341. if (!page)
  342. return NULL;
  343. opt.table = (struct irq_info *) page;
  344. opt.size = PAGE_SIZE;
  345. opt.segment = __KERNEL_DS;
  346. DBG("PCI: Fetching IRQ routing table... ");
  347. __asm__("push %%es\n\t"
  348. "push %%ds\n\t"
  349. "pop %%es\n\t"
  350. "lcall *(%%esi); cld\n\t"
  351. "pop %%es\n\t"
  352. "jc 1f\n\t"
  353. "xor %%ah, %%ah\n"
  354. "1:"
  355. : "=a" (ret),
  356. "=b" (map),
  357. "=m" (opt)
  358. : "0" (PCIBIOS_GET_ROUTING_OPTIONS),
  359. "1" (0),
  360. "D" ((long) &opt),
  361. "S" (&pci_indirect),
  362. "m" (opt)
  363. : "memory");
  364. DBG("OK ret=%d, size=%d, map=%x\n", ret, opt.size, map);
  365. if (ret & 0xff00)
  366. printk(KERN_ERR "PCI: Error %02x when fetching IRQ routing table.\n", (ret >> 8) & 0xff);
  367. else if (opt.size) {
  368. rt = kmalloc(sizeof(struct irq_routing_table) + opt.size, GFP_KERNEL);
  369. if (rt) {
  370. memset(rt, 0, sizeof(struct irq_routing_table));
  371. rt->size = opt.size + sizeof(struct irq_routing_table);
  372. rt->exclusive_irqs = map;
  373. memcpy(rt->slots, (void *) page, opt.size);
  374. printk(KERN_INFO "PCI: Using BIOS Interrupt Routing Table\n");
  375. }
  376. }
  377. free_page(page);
  378. return rt;
  379. }
  380. EXPORT_SYMBOL(pcibios_get_irq_routing_table);
  381. int pcibios_set_irq_routing(struct pci_dev *dev, int pin, int irq)
  382. {
  383. int ret;
  384. __asm__("lcall *(%%esi); cld\n\t"
  385. "jc 1f\n\t"
  386. "xor %%ah, %%ah\n"
  387. "1:"
  388. : "=a" (ret)
  389. : "0" (PCIBIOS_SET_PCI_HW_INT),
  390. "b" ((dev->bus->number << 8) | dev->devfn),
  391. "c" ((irq << 8) | (pin + 10)),
  392. "S" (&pci_indirect));
  393. return !(ret & 0xff00);
  394. }
  395. EXPORT_SYMBOL(pcibios_set_irq_routing);
  396. void __init pci_pcbios_init(void)
  397. {
  398. if ((pci_probe & PCI_PROBE_BIOS)
  399. && ((raw_pci_ops = pci_find_bios()))) {
  400. pci_bios_present = 1;
  401. }
  402. }