i386.c 8.9 KB

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