i386.c 9.8 KB

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