i386.c 11 KB

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