setup-res.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * drivers/pci/setup-res.c
  3. *
  4. * Extruded from code written by
  5. * Dave Rusling (david.rusling@reo.mts.dec.com)
  6. * David Mosberger (davidm@cs.arizona.edu)
  7. * David Miller (davem@redhat.com)
  8. *
  9. * Support routines for initializing a PCI subsystem.
  10. */
  11. /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
  12. /*
  13. * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  14. * Resource sorting
  15. */
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioport.h>
  21. #include <linux/cache.h>
  22. #include <linux/slab.h>
  23. #include "pci.h"
  24. void pci_update_resource(struct pci_dev *dev, int resno)
  25. {
  26. struct pci_bus_region region;
  27. u32 new, check, mask;
  28. int reg;
  29. enum pci_bar_type type;
  30. struct resource *res = dev->resource + resno;
  31. /*
  32. * Ignore resources for unimplemented BARs and unused resource slots
  33. * for 64 bit BARs.
  34. */
  35. if (!res->flags)
  36. return;
  37. /*
  38. * Ignore non-moveable resources. This might be legacy resources for
  39. * which no functional BAR register exists or another important
  40. * system resource we shouldn't move around.
  41. */
  42. if (res->flags & IORESOURCE_PCI_FIXED)
  43. return;
  44. pcibios_resource_to_bus(dev, &region, res);
  45. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  46. if (res->flags & IORESOURCE_IO)
  47. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  48. else
  49. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  50. reg = pci_resource_bar(dev, resno, &type);
  51. if (!reg)
  52. return;
  53. if (type != pci_bar_unknown) {
  54. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  55. return;
  56. new |= PCI_ROM_ADDRESS_ENABLE;
  57. }
  58. pci_write_config_dword(dev, reg, new);
  59. pci_read_config_dword(dev, reg, &check);
  60. if ((new ^ check) & mask) {
  61. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  62. resno, new, check);
  63. }
  64. if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  65. (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  66. new = region.start >> 16 >> 16;
  67. pci_write_config_dword(dev, reg + 4, new);
  68. pci_read_config_dword(dev, reg + 4, &check);
  69. if (check != new) {
  70. dev_err(&dev->dev, "BAR %d: error updating "
  71. "(high %#08x != %#08x)\n", resno, new, check);
  72. }
  73. }
  74. res->flags &= ~IORESOURCE_UNSET;
  75. dev_info(&dev->dev, "BAR %d: set to %pR (PCI address [%#llx-%#llx]\n",
  76. resno, res, (unsigned long long)region.start,
  77. (unsigned long long)region.end);
  78. }
  79. int pci_claim_resource(struct pci_dev *dev, int resource)
  80. {
  81. struct resource *res = &dev->resource[resource];
  82. struct resource *root, *conflict;
  83. root = pci_find_parent_resource(dev, res);
  84. if (!root) {
  85. dev_err(&dev->dev, "no compatible bridge window for %pR\n",
  86. res);
  87. return -EINVAL;
  88. }
  89. conflict = request_resource_conflict(root, res);
  90. if (conflict) {
  91. dev_err(&dev->dev,
  92. "address space collision: %pR conflicts with %s %pR\n",
  93. res, conflict->name, conflict);
  94. return -EBUSY;
  95. }
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(pci_claim_resource);
  99. #ifdef CONFIG_PCI_QUIRKS
  100. void pci_disable_bridge_window(struct pci_dev *dev)
  101. {
  102. dev_info(&dev->dev, "disabling bridge mem windows\n");
  103. /* MMIO Base/Limit */
  104. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  105. /* Prefetchable MMIO Base/Limit */
  106. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  107. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  108. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  109. }
  110. #endif /* CONFIG_PCI_QUIRKS */
  111. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  112. int resno)
  113. {
  114. struct resource *res = dev->resource + resno;
  115. resource_size_t size, min, align;
  116. int ret;
  117. size = resource_size(res);
  118. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  119. align = pci_resource_alignment(dev, res);
  120. /* First, try exact prefetching match.. */
  121. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  122. IORESOURCE_PREFETCH,
  123. pcibios_align_resource, dev);
  124. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  125. /*
  126. * That failed.
  127. *
  128. * But a prefetching area can handle a non-prefetching
  129. * window (it will just not perform as well).
  130. */
  131. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  132. pcibios_align_resource, dev);
  133. }
  134. if (!ret) {
  135. res->flags &= ~IORESOURCE_STARTALIGN;
  136. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  137. if (resno < PCI_BRIDGE_RESOURCES)
  138. pci_update_resource(dev, resno);
  139. }
  140. return ret;
  141. }
  142. int pci_assign_resource(struct pci_dev *dev, int resno)
  143. {
  144. struct resource *res = dev->resource + resno;
  145. resource_size_t align;
  146. struct pci_bus *bus;
  147. int ret;
  148. char *type;
  149. align = pci_resource_alignment(dev, res);
  150. if (!align) {
  151. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  152. "(bogus alignment)\n", resno, res);
  153. return -EINVAL;
  154. }
  155. bus = dev->bus;
  156. while ((ret = __pci_assign_resource(bus, dev, resno))) {
  157. if (bus->parent && bus->self->transparent)
  158. bus = bus->parent;
  159. else
  160. bus = NULL;
  161. if (bus)
  162. continue;
  163. break;
  164. }
  165. if (ret) {
  166. if (res->flags & IORESOURCE_MEM)
  167. if (res->flags & IORESOURCE_PREFETCH)
  168. type = "mem pref";
  169. else
  170. type = "mem";
  171. else if (res->flags & IORESOURCE_IO)
  172. type = "io";
  173. else
  174. type = "unknown";
  175. dev_info(&dev->dev,
  176. "BAR %d: can't assign %s (size %#llx)\n",
  177. resno, type, (unsigned long long) resource_size(res));
  178. }
  179. return ret;
  180. }
  181. /* Sort resources by alignment */
  182. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  183. {
  184. int i;
  185. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  186. struct resource *r;
  187. struct resource_list *list, *tmp;
  188. resource_size_t r_align;
  189. r = &dev->resource[i];
  190. if (r->flags & IORESOURCE_PCI_FIXED)
  191. continue;
  192. if (!(r->flags) || r->parent)
  193. continue;
  194. r_align = pci_resource_alignment(dev, r);
  195. if (!r_align) {
  196. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  197. i, r);
  198. continue;
  199. }
  200. for (list = head; ; list = list->next) {
  201. resource_size_t align = 0;
  202. struct resource_list *ln = list->next;
  203. if (ln)
  204. align = pci_resource_alignment(ln->dev, ln->res);
  205. if (r_align > align) {
  206. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  207. if (!tmp)
  208. panic("pdev_sort_resources(): "
  209. "kmalloc() failed!\n");
  210. tmp->next = ln;
  211. tmp->res = r;
  212. tmp->dev = dev;
  213. list->next = tmp;
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. int pci_enable_resources(struct pci_dev *dev, int mask)
  220. {
  221. u16 cmd, old_cmd;
  222. int i;
  223. struct resource *r;
  224. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  225. old_cmd = cmd;
  226. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  227. if (!(mask & (1 << i)))
  228. continue;
  229. r = &dev->resource[i];
  230. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  231. continue;
  232. if ((i == PCI_ROM_RESOURCE) &&
  233. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  234. continue;
  235. if (!r->parent) {
  236. dev_err(&dev->dev, "device not available "
  237. "(can't reserve %pR)\n", r);
  238. return -EINVAL;
  239. }
  240. if (r->flags & IORESOURCE_IO)
  241. cmd |= PCI_COMMAND_IO;
  242. if (r->flags & IORESOURCE_MEM)
  243. cmd |= PCI_COMMAND_MEMORY;
  244. }
  245. if (cmd != old_cmd) {
  246. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  247. old_cmd, cmd);
  248. pci_write_config_word(dev, PCI_COMMAND, cmd);
  249. }
  250. return 0;
  251. }