pcbios.c 11 KB

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