setup-res.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 bus [%#llx-%#llx] flags %#lx\n",
  81. resno, (unsigned long long)region.start,
  82. (unsigned long long)region.end, res->flags);
  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. char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  89. int err;
  90. root = pci_find_parent_resource(dev, res);
  91. err = -EINVAL;
  92. if (root != NULL)
  93. err = insert_resource(root, res);
  94. if (err) {
  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. #ifdef CONFIG_PCI_QUIRKS
  104. void pci_disable_bridge_window(struct pci_dev *dev)
  105. {
  106. dev_dbg(&dev->dev, "Disabling bridge window.\n");
  107. /* MMIO Base/Limit */
  108. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  109. /* Prefetchable MMIO Base/Limit */
  110. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  111. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  112. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  113. }
  114. #endif /* CONFIG_PCI_QUIRKS */
  115. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  116. int resno)
  117. {
  118. struct resource *res = dev->resource + resno;
  119. resource_size_t size, min, align;
  120. int ret;
  121. size = resource_size(res);
  122. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  123. align = resource_alignment(res);
  124. /* First, try exact prefetching match.. */
  125. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  126. IORESOURCE_PREFETCH,
  127. pcibios_align_resource, dev);
  128. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  129. /*
  130. * That failed.
  131. *
  132. * But a prefetching area can handle a non-prefetching
  133. * window (it will just not perform as well).
  134. */
  135. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  136. pcibios_align_resource, dev);
  137. }
  138. if (!ret) {
  139. res->flags &= ~IORESOURCE_STARTALIGN;
  140. if (resno < PCI_BRIDGE_RESOURCES)
  141. pci_update_resource(dev, resno);
  142. }
  143. return ret;
  144. }
  145. int pci_assign_resource(struct pci_dev *dev, int resno)
  146. {
  147. struct resource *res = dev->resource + resno;
  148. resource_size_t align;
  149. struct pci_bus *bus;
  150. int ret;
  151. align = resource_alignment(res);
  152. if (!align) {
  153. dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus "
  154. "alignment) %pR flags %#lx\n",
  155. resno, res, res->flags);
  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 %s resource %pR\n",
  170. resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
  171. return ret;
  172. }
  173. #if 0
  174. int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
  175. {
  176. struct pci_bus *bus = dev->bus;
  177. struct resource *res = dev->resource + resno;
  178. unsigned int type_mask;
  179. int i, ret = -EBUSY;
  180. type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  181. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  182. struct resource *r = bus->resource[i];
  183. if (!r)
  184. continue;
  185. /* type_mask must match */
  186. if ((res->flags ^ r->flags) & type_mask)
  187. continue;
  188. ret = request_resource(r, res);
  189. if (ret == 0)
  190. break;
  191. }
  192. if (ret) {
  193. dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
  194. resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
  195. } else if (resno < PCI_BRIDGE_RESOURCES) {
  196. pci_update_resource(dev, resno);
  197. }
  198. return ret;
  199. }
  200. EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
  201. #endif
  202. /* Sort resources by alignment */
  203. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  204. {
  205. int i;
  206. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  207. struct resource *r;
  208. struct resource_list *list, *tmp;
  209. resource_size_t r_align;
  210. r = &dev->resource[i];
  211. if (r->flags & IORESOURCE_PCI_FIXED)
  212. continue;
  213. if (!(r->flags) || r->parent)
  214. continue;
  215. r_align = resource_alignment(r);
  216. if (!r_align) {
  217. dev_warn(&dev->dev, "BAR %d: bogus alignment "
  218. "%pR flags %#lx\n",
  219. i, r, r->flags);
  220. continue;
  221. }
  222. for (list = head; ; list = list->next) {
  223. resource_size_t align = 0;
  224. struct resource_list *ln = list->next;
  225. if (ln)
  226. align = resource_alignment(ln->res);
  227. if (r_align > align) {
  228. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  229. if (!tmp)
  230. panic("pdev_sort_resources(): "
  231. "kmalloc() failed!\n");
  232. tmp->next = ln;
  233. tmp->res = r;
  234. tmp->dev = dev;
  235. list->next = tmp;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. int pci_enable_resources(struct pci_dev *dev, int mask)
  242. {
  243. u16 cmd, old_cmd;
  244. int i;
  245. struct resource *r;
  246. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  247. old_cmd = cmd;
  248. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  249. if (!(mask & (1 << i)))
  250. continue;
  251. r = &dev->resource[i];
  252. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  253. continue;
  254. if ((i == PCI_ROM_RESOURCE) &&
  255. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  256. continue;
  257. if (!r->parent) {
  258. dev_err(&dev->dev, "device not available because of "
  259. "BAR %d %pR collisions\n", i, r);
  260. return -EINVAL;
  261. }
  262. if (r->flags & IORESOURCE_IO)
  263. cmd |= PCI_COMMAND_IO;
  264. if (r->flags & IORESOURCE_MEM)
  265. cmd |= PCI_COMMAND_MEMORY;
  266. }
  267. if (cmd != old_cmd) {
  268. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  269. old_cmd, cmd);
  270. pci_write_config_word(dev, PCI_COMMAND, cmd);
  271. }
  272. return 0;
  273. }