i386.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Low-Level PCI Access for i386 machines
  4. *
  5. * Copyright 1993, 1994 Drew Eckhardt
  6. * Visionary Computing
  7. * (Unix and Linux consulting and custom programming)
  8. * Drew@Colorado.EDU
  9. * +1 (303) 786-7975
  10. *
  11. * Drew's work was sponsored by:
  12. * iX Multiuser Multitasking Magazine
  13. * Hannover, Germany
  14. * hm@ix.de
  15. *
  16. * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
  17. *
  18. * For more information, please consult the following manuals (look at
  19. * http://www.pcisig.com/ for how to get them):
  20. *
  21. * PCI BIOS Specification
  22. * PCI Local Bus Specification
  23. * PCI to PCI Bridge Specification
  24. * PCI System Design Guide
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/export.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/ioport.h>
  33. #include <linux/errno.h>
  34. #include <linux/bootmem.h>
  35. #include <asm/pat.h>
  36. #include <asm/e820/api.h>
  37. #include <asm/pci_x86.h>
  38. #include <asm/io_apic.h>
  39. /*
  40. * This list of dynamic mappings is for temporarily maintaining
  41. * original BIOS BAR addresses for possible reinstatement.
  42. */
  43. struct pcibios_fwaddrmap {
  44. struct list_head list;
  45. struct pci_dev *dev;
  46. resource_size_t fw_addr[DEVICE_COUNT_RESOURCE];
  47. };
  48. static LIST_HEAD(pcibios_fwaddrmappings);
  49. static DEFINE_SPINLOCK(pcibios_fwaddrmap_lock);
  50. static bool pcibios_fw_addr_done;
  51. /* Must be called with 'pcibios_fwaddrmap_lock' lock held. */
  52. static struct pcibios_fwaddrmap *pcibios_fwaddrmap_lookup(struct pci_dev *dev)
  53. {
  54. struct pcibios_fwaddrmap *map;
  55. WARN_ON_SMP(!spin_is_locked(&pcibios_fwaddrmap_lock));
  56. list_for_each_entry(map, &pcibios_fwaddrmappings, list)
  57. if (map->dev == dev)
  58. return map;
  59. return NULL;
  60. }
  61. static void
  62. pcibios_save_fw_addr(struct pci_dev *dev, int idx, resource_size_t fw_addr)
  63. {
  64. unsigned long flags;
  65. struct pcibios_fwaddrmap *map;
  66. if (pcibios_fw_addr_done)
  67. return;
  68. spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
  69. map = pcibios_fwaddrmap_lookup(dev);
  70. if (!map) {
  71. spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
  72. map = kzalloc(sizeof(*map), GFP_KERNEL);
  73. if (!map)
  74. return;
  75. map->dev = pci_dev_get(dev);
  76. map->fw_addr[idx] = fw_addr;
  77. INIT_LIST_HEAD(&map->list);
  78. spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
  79. list_add_tail(&map->list, &pcibios_fwaddrmappings);
  80. } else
  81. map->fw_addr[idx] = fw_addr;
  82. spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
  83. }
  84. resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  85. {
  86. unsigned long flags;
  87. struct pcibios_fwaddrmap *map;
  88. resource_size_t fw_addr = 0;
  89. if (pcibios_fw_addr_done)
  90. return 0;
  91. spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
  92. map = pcibios_fwaddrmap_lookup(dev);
  93. if (map)
  94. fw_addr = map->fw_addr[idx];
  95. spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
  96. return fw_addr;
  97. }
  98. static void __init pcibios_fw_addr_list_del(void)
  99. {
  100. unsigned long flags;
  101. struct pcibios_fwaddrmap *entry, *next;
  102. spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
  103. list_for_each_entry_safe(entry, next, &pcibios_fwaddrmappings, list) {
  104. list_del(&entry->list);
  105. pci_dev_put(entry->dev);
  106. kfree(entry);
  107. }
  108. spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
  109. pcibios_fw_addr_done = true;
  110. }
  111. static int
  112. skip_isa_ioresource_align(struct pci_dev *dev) {
  113. if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
  114. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  115. return 1;
  116. return 0;
  117. }
  118. /*
  119. * We need to avoid collisions with `mirrored' VGA ports
  120. * and other strange ISA hardware, so we always want the
  121. * addresses to be allocated in the 0x000-0x0ff region
  122. * modulo 0x400.
  123. *
  124. * Why? Because some silly external IO cards only decode
  125. * the low 10 bits of the IO address. The 0x00-0xff region
  126. * is reserved for motherboard devices that decode all 16
  127. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  128. * but we want to try to avoid allocating at 0x2900-0x2bff
  129. * which might have be mirrored at 0x0100-0x03ff..
  130. */
  131. resource_size_t
  132. pcibios_align_resource(void *data, const struct resource *res,
  133. resource_size_t size, resource_size_t align)
  134. {
  135. struct pci_dev *dev = data;
  136. resource_size_t start = res->start;
  137. if (res->flags & IORESOURCE_IO) {
  138. if (skip_isa_ioresource_align(dev))
  139. return start;
  140. if (start & 0x300)
  141. start = (start + 0x3ff) & ~0x3ff;
  142. } else if (res->flags & IORESOURCE_MEM) {
  143. /* The low 1MB range is reserved for ISA cards */
  144. if (start < BIOS_END)
  145. start = BIOS_END;
  146. }
  147. return start;
  148. }
  149. EXPORT_SYMBOL(pcibios_align_resource);
  150. /*
  151. * Handle resources of PCI devices. If the world were perfect, we could
  152. * just allocate all the resource regions and do nothing more. It isn't.
  153. * On the other hand, we cannot just re-allocate all devices, as it would
  154. * require us to know lots of host bridge internals. So we attempt to
  155. * keep as much of the original configuration as possible, but tweak it
  156. * when it's found to be wrong.
  157. *
  158. * Known BIOS problems we have to work around:
  159. * - I/O or memory regions not configured
  160. * - regions configured, but not enabled in the command register
  161. * - bogus I/O addresses above 64K used
  162. * - expansion ROMs left enabled (this may sound harmless, but given
  163. * the fact the PCI specs explicitly allow address decoders to be
  164. * shared between expansion ROMs and other resource regions, it's
  165. * at least dangerous)
  166. * - bad resource sizes or overlaps with other regions
  167. *
  168. * Our solution:
  169. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  170. * This gives us fixed barriers on where we can allocate.
  171. * (2) Allocate resources for all enabled devices. If there is
  172. * a collision, just mark the resource as unallocated. Also
  173. * disable expansion ROMs during this step.
  174. * (3) Try to allocate resources for disabled devices. If the
  175. * resources were assigned correctly, everything goes well,
  176. * if they weren't, they won't disturb allocation of other
  177. * resources.
  178. * (4) Assign new addresses to resources which were either
  179. * not configured at all or misconfigured. If explicitly
  180. * requested by the user, configure expansion ROM address
  181. * as well.
  182. */
  183. static void pcibios_allocate_bridge_resources(struct pci_dev *dev)
  184. {
  185. int idx;
  186. struct resource *r;
  187. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  188. r = &dev->resource[idx];
  189. if (!r->flags)
  190. continue;
  191. if (r->parent) /* Already allocated */
  192. continue;
  193. if (!r->start || pci_claim_bridge_resource(dev, idx) < 0) {
  194. /*
  195. * Something is wrong with the region.
  196. * Invalidate the resource to prevent
  197. * child resource allocations in this
  198. * range.
  199. */
  200. r->start = r->end = 0;
  201. r->flags = 0;
  202. }
  203. }
  204. }
  205. static void pcibios_allocate_bus_resources(struct pci_bus *bus)
  206. {
  207. struct pci_bus *child;
  208. /* Depth-First Search on bus tree */
  209. if (bus->self)
  210. pcibios_allocate_bridge_resources(bus->self);
  211. list_for_each_entry(child, &bus->children, node)
  212. pcibios_allocate_bus_resources(child);
  213. }
  214. struct pci_check_idx_range {
  215. int start;
  216. int end;
  217. };
  218. static void pcibios_allocate_dev_resources(struct pci_dev *dev, int pass)
  219. {
  220. int idx, disabled, i;
  221. u16 command;
  222. struct resource *r;
  223. struct pci_check_idx_range idx_range[] = {
  224. { PCI_STD_RESOURCES, PCI_STD_RESOURCE_END },
  225. #ifdef CONFIG_PCI_IOV
  226. { PCI_IOV_RESOURCES, PCI_IOV_RESOURCE_END },
  227. #endif
  228. };
  229. pci_read_config_word(dev, PCI_COMMAND, &command);
  230. for (i = 0; i < ARRAY_SIZE(idx_range); i++)
  231. for (idx = idx_range[i].start; idx <= idx_range[i].end; idx++) {
  232. r = &dev->resource[idx];
  233. if (r->parent) /* Already allocated */
  234. continue;
  235. if (!r->start) /* Address not assigned at all */
  236. continue;
  237. if (r->flags & IORESOURCE_IO)
  238. disabled = !(command & PCI_COMMAND_IO);
  239. else
  240. disabled = !(command & PCI_COMMAND_MEMORY);
  241. if (pass == disabled) {
  242. dev_dbg(&dev->dev,
  243. "BAR %d: reserving %pr (d=%d, p=%d)\n",
  244. idx, r, disabled, pass);
  245. if (pci_claim_resource(dev, idx) < 0) {
  246. if (r->flags & IORESOURCE_PCI_FIXED) {
  247. dev_info(&dev->dev, "BAR %d %pR is immovable\n",
  248. idx, r);
  249. } else {
  250. /* We'll assign a new address later */
  251. pcibios_save_fw_addr(dev,
  252. idx, r->start);
  253. r->end -= r->start;
  254. r->start = 0;
  255. }
  256. }
  257. }
  258. }
  259. if (!pass) {
  260. r = &dev->resource[PCI_ROM_RESOURCE];
  261. if (r->flags & IORESOURCE_ROM_ENABLE) {
  262. /* Turn the ROM off, leave the resource region,
  263. * but keep it unregistered. */
  264. u32 reg;
  265. dev_dbg(&dev->dev, "disabling ROM %pR\n", r);
  266. r->flags &= ~IORESOURCE_ROM_ENABLE;
  267. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  268. pci_write_config_dword(dev, dev->rom_base_reg,
  269. reg & ~PCI_ROM_ADDRESS_ENABLE);
  270. }
  271. }
  272. }
  273. static void pcibios_allocate_resources(struct pci_bus *bus, int pass)
  274. {
  275. struct pci_dev *dev;
  276. struct pci_bus *child;
  277. list_for_each_entry(dev, &bus->devices, bus_list) {
  278. pcibios_allocate_dev_resources(dev, pass);
  279. child = dev->subordinate;
  280. if (child)
  281. pcibios_allocate_resources(child, pass);
  282. }
  283. }
  284. static void pcibios_allocate_dev_rom_resource(struct pci_dev *dev)
  285. {
  286. struct resource *r;
  287. /*
  288. * Try to use BIOS settings for ROMs, otherwise let
  289. * pci_assign_unassigned_resources() allocate the new
  290. * addresses.
  291. */
  292. r = &dev->resource[PCI_ROM_RESOURCE];
  293. if (!r->flags || !r->start)
  294. return;
  295. if (r->parent) /* Already allocated */
  296. return;
  297. if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {
  298. r->end -= r->start;
  299. r->start = 0;
  300. }
  301. }
  302. static void pcibios_allocate_rom_resources(struct pci_bus *bus)
  303. {
  304. struct pci_dev *dev;
  305. struct pci_bus *child;
  306. list_for_each_entry(dev, &bus->devices, bus_list) {
  307. pcibios_allocate_dev_rom_resource(dev);
  308. child = dev->subordinate;
  309. if (child)
  310. pcibios_allocate_rom_resources(child);
  311. }
  312. }
  313. static int __init pcibios_assign_resources(void)
  314. {
  315. struct pci_bus *bus;
  316. if (!(pci_probe & PCI_ASSIGN_ROMS))
  317. list_for_each_entry(bus, &pci_root_buses, node)
  318. pcibios_allocate_rom_resources(bus);
  319. pci_assign_unassigned_resources();
  320. pcibios_fw_addr_list_del();
  321. return 0;
  322. }
  323. /**
  324. * called in fs_initcall (one below subsys_initcall),
  325. * give a chance for motherboard reserve resources
  326. */
  327. fs_initcall(pcibios_assign_resources);
  328. void pcibios_resource_survey_bus(struct pci_bus *bus)
  329. {
  330. dev_printk(KERN_DEBUG, &bus->dev, "Allocating resources\n");
  331. pcibios_allocate_bus_resources(bus);
  332. pcibios_allocate_resources(bus, 0);
  333. pcibios_allocate_resources(bus, 1);
  334. if (!(pci_probe & PCI_ASSIGN_ROMS))
  335. pcibios_allocate_rom_resources(bus);
  336. }
  337. void __init pcibios_resource_survey(void)
  338. {
  339. struct pci_bus *bus;
  340. DBG("PCI: Allocating resources\n");
  341. list_for_each_entry(bus, &pci_root_buses, node)
  342. pcibios_allocate_bus_resources(bus);
  343. list_for_each_entry(bus, &pci_root_buses, node)
  344. pcibios_allocate_resources(bus, 0);
  345. list_for_each_entry(bus, &pci_root_buses, node)
  346. pcibios_allocate_resources(bus, 1);
  347. e820__reserve_resources_late();
  348. /*
  349. * Insert the IO APIC resources after PCI initialization has
  350. * occurred to handle IO APICS that are mapped in on a BAR in
  351. * PCI space, but before trying to assign unassigned pci res.
  352. */
  353. ioapic_insert_resources();
  354. }