i386.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/pci.h>
  29. #include <linux/init.h>
  30. #include <linux/ioport.h>
  31. #include <linux/errno.h>
  32. #include <linux/bootmem.h>
  33. #include <asm/pat.h>
  34. #include <asm/e820.h>
  35. #include <asm/pci_x86.h>
  36. #include <asm/io_apic.h>
  37. static int
  38. skip_isa_ioresource_align(struct pci_dev *dev) {
  39. if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
  40. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  41. return 1;
  42. return 0;
  43. }
  44. /*
  45. * We need to avoid collisions with `mirrored' VGA ports
  46. * and other strange ISA hardware, so we always want the
  47. * addresses to be allocated in the 0x000-0x0ff region
  48. * modulo 0x400.
  49. *
  50. * Why? Because some silly external IO cards only decode
  51. * the low 10 bits of the IO address. The 0x00-0xff region
  52. * is reserved for motherboard devices that decode all 16
  53. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  54. * but we want to try to avoid allocating at 0x2900-0x2bff
  55. * which might have be mirrored at 0x0100-0x03ff..
  56. */
  57. resource_size_t
  58. pcibios_align_resource(void *data, const struct resource *res,
  59. resource_size_t size, resource_size_t align)
  60. {
  61. struct pci_dev *dev = data;
  62. resource_size_t start = round_down(res->end - size + 1, align);
  63. if (res->flags & IORESOURCE_IO) {
  64. /*
  65. * If we're avoiding ISA aliases, the largest contiguous I/O
  66. * port space is 256 bytes. Clearing bits 9 and 10 preserves
  67. * all 256-byte and smaller alignments, so the result will
  68. * still be correctly aligned.
  69. */
  70. if (!skip_isa_ioresource_align(dev))
  71. start &= ~0x300;
  72. } else if (res->flags & IORESOURCE_MEM) {
  73. if (start < BIOS_END)
  74. start = res->end; /* fail; no space */
  75. }
  76. return start;
  77. }
  78. EXPORT_SYMBOL(pcibios_align_resource);
  79. /*
  80. * Handle resources of PCI devices. If the world were perfect, we could
  81. * just allocate all the resource regions and do nothing more. It isn't.
  82. * On the other hand, we cannot just re-allocate all devices, as it would
  83. * require us to know lots of host bridge internals. So we attempt to
  84. * keep as much of the original configuration as possible, but tweak it
  85. * when it's found to be wrong.
  86. *
  87. * Known BIOS problems we have to work around:
  88. * - I/O or memory regions not configured
  89. * - regions configured, but not enabled in the command register
  90. * - bogus I/O addresses above 64K used
  91. * - expansion ROMs left enabled (this may sound harmless, but given
  92. * the fact the PCI specs explicitly allow address decoders to be
  93. * shared between expansion ROMs and other resource regions, it's
  94. * at least dangerous)
  95. * - bad resource sizes or overlaps with other regions
  96. *
  97. * Our solution:
  98. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  99. * This gives us fixed barriers on where we can allocate.
  100. * (2) Allocate resources for all enabled devices. If there is
  101. * a collision, just mark the resource as unallocated. Also
  102. * disable expansion ROMs during this step.
  103. * (3) Try to allocate resources for disabled devices. If the
  104. * resources were assigned correctly, everything goes well,
  105. * if they weren't, they won't disturb allocation of other
  106. * resources.
  107. * (4) Assign new addresses to resources which were either
  108. * not configured at all or misconfigured. If explicitly
  109. * requested by the user, configure expansion ROM address
  110. * as well.
  111. */
  112. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  113. {
  114. struct pci_bus *bus;
  115. struct pci_dev *dev;
  116. int idx;
  117. struct resource *r;
  118. /* Depth-First Search on bus tree */
  119. list_for_each_entry(bus, bus_list, node) {
  120. if ((dev = bus->self)) {
  121. for (idx = PCI_BRIDGE_RESOURCES;
  122. idx < PCI_NUM_RESOURCES; idx++) {
  123. r = &dev->resource[idx];
  124. if (!r->flags)
  125. continue;
  126. if (!r->start ||
  127. pci_claim_resource(dev, idx) < 0) {
  128. /*
  129. * Something is wrong with the region.
  130. * Invalidate the resource to prevent
  131. * child resource allocations in this
  132. * range.
  133. */
  134. r->start = r->end = 0;
  135. r->flags = 0;
  136. }
  137. }
  138. }
  139. pcibios_allocate_bus_resources(&bus->children);
  140. }
  141. }
  142. struct pci_check_idx_range {
  143. int start;
  144. int end;
  145. };
  146. static void __init pcibios_allocate_resources(int pass)
  147. {
  148. struct pci_dev *dev = NULL;
  149. int idx, disabled, i;
  150. u16 command;
  151. struct resource *r;
  152. struct pci_check_idx_range idx_range[] = {
  153. { PCI_STD_RESOURCES, PCI_STD_RESOURCE_END },
  154. #ifdef CONFIG_PCI_IOV
  155. { PCI_IOV_RESOURCES, PCI_IOV_RESOURCE_END },
  156. #endif
  157. };
  158. for_each_pci_dev(dev) {
  159. pci_read_config_word(dev, PCI_COMMAND, &command);
  160. for (i = 0; i < ARRAY_SIZE(idx_range); i++)
  161. for (idx = idx_range[i].start; idx <= idx_range[i].end; idx++) {
  162. r = &dev->resource[idx];
  163. if (r->parent) /* Already allocated */
  164. continue;
  165. if (!r->start) /* Address not assigned at all */
  166. continue;
  167. if (r->flags & IORESOURCE_IO)
  168. disabled = !(command & PCI_COMMAND_IO);
  169. else
  170. disabled = !(command & PCI_COMMAND_MEMORY);
  171. if (pass == disabled) {
  172. dev_dbg(&dev->dev,
  173. "BAR %d: reserving %pr (d=%d, p=%d)\n",
  174. idx, r, disabled, pass);
  175. if (pci_claim_resource(dev, idx) < 0) {
  176. /* We'll assign a new address later */
  177. dev->fw_addr[idx] = r->start;
  178. r->end -= r->start;
  179. r->start = 0;
  180. }
  181. }
  182. }
  183. if (!pass) {
  184. r = &dev->resource[PCI_ROM_RESOURCE];
  185. if (r->flags & IORESOURCE_ROM_ENABLE) {
  186. /* Turn the ROM off, leave the resource region,
  187. * but keep it unregistered. */
  188. u32 reg;
  189. dev_dbg(&dev->dev, "disabling ROM %pR\n", r);
  190. r->flags &= ~IORESOURCE_ROM_ENABLE;
  191. pci_read_config_dword(dev,
  192. dev->rom_base_reg, &reg);
  193. pci_write_config_dword(dev, dev->rom_base_reg,
  194. reg & ~PCI_ROM_ADDRESS_ENABLE);
  195. }
  196. }
  197. }
  198. }
  199. static int __init pcibios_assign_resources(void)
  200. {
  201. struct pci_dev *dev = NULL;
  202. struct resource *r;
  203. if (!(pci_probe & PCI_ASSIGN_ROMS)) {
  204. /*
  205. * Try to use BIOS settings for ROMs, otherwise let
  206. * pci_assign_unassigned_resources() allocate the new
  207. * addresses.
  208. */
  209. for_each_pci_dev(dev) {
  210. r = &dev->resource[PCI_ROM_RESOURCE];
  211. if (!r->flags || !r->start)
  212. continue;
  213. if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {
  214. r->end -= r->start;
  215. r->start = 0;
  216. }
  217. }
  218. }
  219. pci_assign_unassigned_resources();
  220. return 0;
  221. }
  222. void __init pcibios_resource_survey(void)
  223. {
  224. DBG("PCI: Allocating resources\n");
  225. pcibios_allocate_bus_resources(&pci_root_buses);
  226. pcibios_allocate_resources(0);
  227. pcibios_allocate_resources(1);
  228. e820_reserve_resources_late();
  229. /*
  230. * Insert the IO APIC resources after PCI initialization has
  231. * occured to handle IO APICS that are mapped in on a BAR in
  232. * PCI space, but before trying to assign unassigned pci res.
  233. */
  234. ioapic_insert_resources();
  235. }
  236. /**
  237. * called in fs_initcall (one below subsys_initcall),
  238. * give a chance for motherboard reserve resources
  239. */
  240. fs_initcall(pcibios_assign_resources);
  241. /*
  242. * If we set up a device for bus mastering, we need to check the latency
  243. * timer as certain crappy BIOSes forget to set it properly.
  244. */
  245. unsigned int pcibios_max_latency = 255;
  246. void pcibios_set_master(struct pci_dev *dev)
  247. {
  248. u8 lat;
  249. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  250. if (lat < 16)
  251. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  252. else if (lat > pcibios_max_latency)
  253. lat = pcibios_max_latency;
  254. else
  255. return;
  256. dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
  257. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  258. }
  259. static const struct vm_operations_struct pci_mmap_ops = {
  260. .access = generic_access_phys,
  261. };
  262. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  263. enum pci_mmap_state mmap_state, int write_combine)
  264. {
  265. unsigned long prot;
  266. /* I/O space cannot be accessed via normal processor loads and
  267. * stores on this platform.
  268. */
  269. if (mmap_state == pci_mmap_io)
  270. return -EINVAL;
  271. prot = pgprot_val(vma->vm_page_prot);
  272. /*
  273. * Return error if pat is not enabled and write_combine is requested.
  274. * Caller can followup with UC MINUS request and add a WC mtrr if there
  275. * is a free mtrr slot.
  276. */
  277. if (!pat_enabled && write_combine)
  278. return -EINVAL;
  279. if (pat_enabled && write_combine)
  280. prot |= _PAGE_CACHE_WC;
  281. else if (pat_enabled || boot_cpu_data.x86 > 3)
  282. /*
  283. * ioremap() and ioremap_nocache() defaults to UC MINUS for now.
  284. * To avoid attribute conflicts, request UC MINUS here
  285. * aswell.
  286. */
  287. prot |= _PAGE_CACHE_UC_MINUS;
  288. prot |= _PAGE_IOMAP; /* creating a mapping for IO */
  289. vma->vm_page_prot = __pgprot(prot);
  290. if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  291. vma->vm_end - vma->vm_start,
  292. vma->vm_page_prot))
  293. return -EAGAIN;
  294. vma->vm_ops = &pci_mmap_ops;
  295. return 0;
  296. }