setup-res.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
  46. "flags %#lx\n", resno, res,
  47. (unsigned long long)region.start,
  48. (unsigned long long)region.end,
  49. (unsigned long)res->flags);
  50. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  51. if (res->flags & IORESOURCE_IO)
  52. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  53. else
  54. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  55. reg = pci_resource_bar(dev, resno, &type);
  56. if (!reg)
  57. return;
  58. if (type != pci_bar_unknown) {
  59. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  60. return;
  61. new |= PCI_ROM_ADDRESS_ENABLE;
  62. }
  63. pci_write_config_dword(dev, reg, new);
  64. pci_read_config_dword(dev, reg, &check);
  65. if ((new ^ check) & mask) {
  66. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  67. resno, new, check);
  68. }
  69. if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  70. (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  71. new = region.start >> 16 >> 16;
  72. pci_write_config_dword(dev, reg + 4, new);
  73. pci_read_config_dword(dev, reg + 4, &check);
  74. if (check != new) {
  75. dev_err(&dev->dev, "BAR %d: error updating "
  76. "(high %#08x != %#08x)\n", resno, new, check);
  77. }
  78. }
  79. res->flags &= ~IORESOURCE_UNSET;
  80. dev_dbg(&dev->dev, "BAR %d: moved to %pR (bus addr [%#llx-%#llx])\n",
  81. resno, res, (unsigned long long)region.start,
  82. (unsigned long long)region.end);
  83. }
  84. int pci_claim_resource(struct pci_dev *dev, int resource)
  85. {
  86. struct resource *res = &dev->resource[resource];
  87. struct resource *root;
  88. int err;
  89. root = pci_find_parent_resource(dev, res);
  90. err = -EINVAL;
  91. if (root != NULL)
  92. err = request_resource(root, res);
  93. if (err) {
  94. const char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  95. dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
  96. resource,
  97. root ? "address space collision on" :
  98. "no parent found for",
  99. dtype, res);
  100. }
  101. return err;
  102. }
  103. EXPORT_SYMBOL(pci_claim_resource);
  104. #ifdef CONFIG_PCI_QUIRKS
  105. void pci_disable_bridge_window(struct pci_dev *dev)
  106. {
  107. dev_dbg(&dev->dev, "Disabling bridge window.\n");
  108. /* MMIO Base/Limit */
  109. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  110. /* Prefetchable MMIO Base/Limit */
  111. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  112. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  113. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  114. }
  115. #endif /* CONFIG_PCI_QUIRKS */
  116. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  117. int resno)
  118. {
  119. struct resource *res = dev->resource + resno;
  120. resource_size_t size, min, align;
  121. int ret;
  122. size = resource_size(res);
  123. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  124. align = pci_resource_alignment(dev, res);
  125. /* First, try exact prefetching match.. */
  126. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  127. IORESOURCE_PREFETCH,
  128. pcibios_align_resource, dev);
  129. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  130. /*
  131. * That failed.
  132. *
  133. * But a prefetching area can handle a non-prefetching
  134. * window (it will just not perform as well).
  135. */
  136. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  137. pcibios_align_resource, dev);
  138. }
  139. if (!ret) {
  140. res->flags &= ~IORESOURCE_STARTALIGN;
  141. if (resno < PCI_BRIDGE_RESOURCES)
  142. pci_update_resource(dev, resno);
  143. }
  144. return ret;
  145. }
  146. int pci_assign_resource(struct pci_dev *dev, int resno)
  147. {
  148. struct resource *res = dev->resource + resno;
  149. resource_size_t align;
  150. struct pci_bus *bus;
  151. int ret;
  152. align = pci_resource_alignment(dev, res);
  153. if (!align) {
  154. dev_info(&dev->dev, "BAR %d: can't allocate %pR "
  155. "(bogus alignment)\n", resno, res);
  156. return -EINVAL;
  157. }
  158. bus = dev->bus;
  159. while ((ret = __pci_assign_resource(bus, dev, resno))) {
  160. if (bus->parent && bus->self->transparent)
  161. bus = bus->parent;
  162. else
  163. bus = NULL;
  164. if (bus)
  165. continue;
  166. break;
  167. }
  168. if (ret)
  169. dev_info(&dev->dev, "BAR %d: can't allocate %pR\n",
  170. resno, res);
  171. return ret;
  172. }
  173. /* Sort resources by alignment */
  174. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  175. {
  176. int i;
  177. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  178. struct resource *r;
  179. struct resource_list *list, *tmp;
  180. resource_size_t r_align;
  181. r = &dev->resource[i];
  182. if (r->flags & IORESOURCE_PCI_FIXED)
  183. continue;
  184. if (!(r->flags) || r->parent)
  185. continue;
  186. r_align = pci_resource_alignment(dev, r);
  187. if (!r_align) {
  188. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  189. i, r);
  190. continue;
  191. }
  192. for (list = head; ; list = list->next) {
  193. resource_size_t align = 0;
  194. struct resource_list *ln = list->next;
  195. if (ln)
  196. align = pci_resource_alignment(ln->dev, ln->res);
  197. if (r_align > align) {
  198. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  199. if (!tmp)
  200. panic("pdev_sort_resources(): "
  201. "kmalloc() failed!\n");
  202. tmp->next = ln;
  203. tmp->res = r;
  204. tmp->dev = dev;
  205. list->next = tmp;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. int pci_enable_resources(struct pci_dev *dev, int mask)
  212. {
  213. u16 cmd, old_cmd;
  214. int i;
  215. struct resource *r;
  216. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  217. old_cmd = cmd;
  218. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  219. if (!(mask & (1 << i)))
  220. continue;
  221. r = &dev->resource[i];
  222. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  223. continue;
  224. if ((i == PCI_ROM_RESOURCE) &&
  225. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  226. continue;
  227. if (!r->parent) {
  228. dev_err(&dev->dev, "device not available because of "
  229. "BAR %d %pR collisions\n", i, r);
  230. return -EINVAL;
  231. }
  232. if (r->flags & IORESOURCE_IO)
  233. cmd |= PCI_COMMAND_IO;
  234. if (r->flags & IORESOURCE_MEM)
  235. cmd |= PCI_COMMAND_MEMORY;
  236. }
  237. if (cmd != old_cmd) {
  238. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  239. old_cmd, cmd);
  240. pci_write_config_word(dev, PCI_COMMAND, cmd);
  241. }
  242. return 0;
  243. }