setup-res.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 (res->flags & IORESOURCE_MEM_64) {
  65. new = region.start >> 16 >> 16;
  66. pci_write_config_dword(dev, reg + 4, new);
  67. pci_read_config_dword(dev, reg + 4, &check);
  68. if (check != new) {
  69. dev_err(&dev->dev, "BAR %d: error updating "
  70. "(high %#08x != %#08x)\n", resno, new, check);
  71. }
  72. }
  73. res->flags &= ~IORESOURCE_UNSET;
  74. dev_info(&dev->dev, "BAR %d: set to %pR (PCI address [%#llx-%#llx])\n",
  75. resno, res, (unsigned long long)region.start,
  76. (unsigned long long)region.end);
  77. }
  78. int pci_claim_resource(struct pci_dev *dev, int resource)
  79. {
  80. struct resource *res = &dev->resource[resource];
  81. struct resource *root, *conflict;
  82. root = pci_find_parent_resource(dev, res);
  83. if (!root) {
  84. dev_info(&dev->dev, "no compatible bridge window for %pR\n",
  85. res);
  86. return -EINVAL;
  87. }
  88. conflict = request_resource_conflict(root, res);
  89. if (conflict) {
  90. dev_info(&dev->dev,
  91. "address space collision: %pR conflicts with %s %pR\n",
  92. res, conflict->name, conflict);
  93. return -EBUSY;
  94. }
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(pci_claim_resource);
  98. #ifdef CONFIG_PCI_QUIRKS
  99. void pci_disable_bridge_window(struct pci_dev *dev)
  100. {
  101. dev_info(&dev->dev, "disabling bridge mem windows\n");
  102. /* MMIO Base/Limit */
  103. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  104. /* Prefetchable MMIO Base/Limit */
  105. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  106. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  107. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  108. }
  109. #endif /* CONFIG_PCI_QUIRKS */
  110. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  111. int resno)
  112. {
  113. struct resource *res = dev->resource + resno;
  114. resource_size_t size, min, align;
  115. int ret;
  116. size = resource_size(res);
  117. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  118. align = pci_resource_alignment(dev, res);
  119. /* First, try exact prefetching match.. */
  120. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  121. IORESOURCE_PREFETCH,
  122. pcibios_align_resource, dev);
  123. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  124. /*
  125. * That failed.
  126. *
  127. * But a prefetching area can handle a non-prefetching
  128. * window (it will just not perform as well).
  129. */
  130. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  131. pcibios_align_resource, dev);
  132. }
  133. if (ret < 0 && dev->fw_addr[resno]) {
  134. struct resource *root, *conflict;
  135. resource_size_t start, end;
  136. /*
  137. * If we failed to assign anything, let's try the address
  138. * where firmware left it. That at least has a chance of
  139. * working, which is better than just leaving it disabled.
  140. */
  141. if (res->flags & IORESOURCE_IO)
  142. root = &ioport_resource;
  143. else
  144. root = &iomem_resource;
  145. start = res->start;
  146. end = res->end;
  147. res->start = dev->fw_addr[resno];
  148. res->end = res->start + size - 1;
  149. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  150. resno, res);
  151. conflict = request_resource_conflict(root, res);
  152. if (conflict) {
  153. dev_info(&dev->dev,
  154. "BAR %d: %pR conflicts with %s %pR\n", resno,
  155. res, conflict->name, conflict);
  156. res->start = start;
  157. res->end = end;
  158. } else
  159. ret = 0;
  160. }
  161. if (!ret) {
  162. res->flags &= ~IORESOURCE_STARTALIGN;
  163. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  164. if (resno < PCI_BRIDGE_RESOURCES)
  165. pci_update_resource(dev, resno);
  166. }
  167. return ret;
  168. }
  169. int pci_assign_resource(struct pci_dev *dev, int resno)
  170. {
  171. struct resource *res = dev->resource + resno;
  172. resource_size_t align;
  173. struct pci_bus *bus;
  174. int ret;
  175. char *type;
  176. align = pci_resource_alignment(dev, res);
  177. if (!align) {
  178. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  179. "(bogus alignment)\n", resno, res);
  180. return -EINVAL;
  181. }
  182. bus = dev->bus;
  183. while ((ret = __pci_assign_resource(bus, dev, resno))) {
  184. if (bus->parent && bus->self->transparent)
  185. bus = bus->parent;
  186. else
  187. bus = NULL;
  188. if (bus)
  189. continue;
  190. break;
  191. }
  192. if (ret) {
  193. if (res->flags & IORESOURCE_MEM)
  194. if (res->flags & IORESOURCE_PREFETCH)
  195. type = "mem pref";
  196. else
  197. type = "mem";
  198. else if (res->flags & IORESOURCE_IO)
  199. type = "io";
  200. else
  201. type = "unknown";
  202. dev_info(&dev->dev,
  203. "BAR %d: can't assign %s (size %#llx)\n",
  204. resno, type, (unsigned long long) resource_size(res));
  205. }
  206. return ret;
  207. }
  208. /* Sort resources by alignment */
  209. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  210. {
  211. int i;
  212. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  213. struct resource *r;
  214. struct resource_list *list, *tmp;
  215. resource_size_t r_align;
  216. r = &dev->resource[i];
  217. if (r->flags & IORESOURCE_PCI_FIXED)
  218. continue;
  219. if (!(r->flags) || r->parent)
  220. continue;
  221. r_align = pci_resource_alignment(dev, r);
  222. if (!r_align) {
  223. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  224. i, r);
  225. continue;
  226. }
  227. for (list = head; ; list = list->next) {
  228. resource_size_t align = 0;
  229. struct resource_list *ln = list->next;
  230. if (ln)
  231. align = pci_resource_alignment(ln->dev, ln->res);
  232. if (r_align > align) {
  233. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  234. if (!tmp)
  235. panic("pdev_sort_resources(): "
  236. "kmalloc() failed!\n");
  237. tmp->next = ln;
  238. tmp->res = r;
  239. tmp->dev = dev;
  240. list->next = tmp;
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. int pci_enable_resources(struct pci_dev *dev, int mask)
  247. {
  248. u16 cmd, old_cmd;
  249. int i;
  250. struct resource *r;
  251. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  252. old_cmd = cmd;
  253. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  254. if (!(mask & (1 << i)))
  255. continue;
  256. r = &dev->resource[i];
  257. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  258. continue;
  259. if ((i == PCI_ROM_RESOURCE) &&
  260. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  261. continue;
  262. if (!r->parent) {
  263. dev_err(&dev->dev, "device not available "
  264. "(can't reserve %pR)\n", r);
  265. return -EINVAL;
  266. }
  267. if (r->flags & IORESOURCE_IO)
  268. cmd |= PCI_COMMAND_IO;
  269. if (r->flags & IORESOURCE_MEM)
  270. cmd |= PCI_COMMAND_MEMORY;
  271. }
  272. if (cmd != old_cmd) {
  273. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  274. old_cmd, cmd);
  275. pci_write_config_word(dev, PCI_COMMAND, cmd);
  276. }
  277. return 0;
  278. }