i386.c 8.2 KB

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