i386.c 8.7 KB

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