setup-res.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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, resource_size_t size, resource_size_t align)
  112. {
  113. struct resource *res = dev->resource + resno;
  114. resource_size_t min;
  115. int ret;
  116. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  117. /* First, try exact prefetching match.. */
  118. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  119. IORESOURCE_PREFETCH,
  120. pcibios_align_resource, dev);
  121. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  122. /*
  123. * That failed.
  124. *
  125. * But a prefetching area can handle a non-prefetching
  126. * window (it will just not perform as well).
  127. */
  128. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  129. pcibios_align_resource, dev);
  130. }
  131. return ret;
  132. }
  133. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  134. int resno, resource_size_t size)
  135. {
  136. struct resource *root, *conflict;
  137. resource_size_t start, end;
  138. int ret = 0;
  139. if (res->flags & IORESOURCE_IO)
  140. root = &ioport_resource;
  141. else
  142. root = &iomem_resource;
  143. start = res->start;
  144. end = res->end;
  145. res->start = dev->fw_addr[resno];
  146. res->end = res->start + size - 1;
  147. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  148. resno, res);
  149. conflict = request_resource_conflict(root, res);
  150. if (conflict) {
  151. dev_info(&dev->dev,
  152. "BAR %d: %pR conflicts with %s %pR\n", resno,
  153. res, conflict->name, conflict);
  154. res->start = start;
  155. res->end = end;
  156. ret = 1;
  157. }
  158. return ret;
  159. }
  160. static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
  161. {
  162. struct resource *res = dev->resource + resno;
  163. struct pci_bus *bus;
  164. int ret;
  165. char *type;
  166. bus = dev->bus;
  167. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  168. if (!bus->parent || !bus->self->transparent)
  169. break;
  170. bus = bus->parent;
  171. }
  172. if (ret) {
  173. if (res->flags & IORESOURCE_MEM)
  174. if (res->flags & IORESOURCE_PREFETCH)
  175. type = "mem pref";
  176. else
  177. type = "mem";
  178. else if (res->flags & IORESOURCE_IO)
  179. type = "io";
  180. else
  181. type = "unknown";
  182. dev_info(&dev->dev,
  183. "BAR %d: can't assign %s (size %#llx)\n",
  184. resno, type, (unsigned long long) resource_size(res));
  185. }
  186. return ret;
  187. }
  188. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  189. resource_size_t min_align)
  190. {
  191. struct resource *res = dev->resource + resno;
  192. resource_size_t new_size;
  193. int ret;
  194. if (!res->parent) {
  195. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resouce %pR "
  196. "\n", resno, res);
  197. return -EINVAL;
  198. }
  199. new_size = resource_size(res) + addsize + min_align;
  200. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  201. if (!ret) {
  202. res->flags &= ~IORESOURCE_STARTALIGN;
  203. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  204. if (resno < PCI_BRIDGE_RESOURCES)
  205. pci_update_resource(dev, resno);
  206. }
  207. return ret;
  208. }
  209. int pci_assign_resource(struct pci_dev *dev, int resno)
  210. {
  211. struct resource *res = dev->resource + resno;
  212. resource_size_t align, size;
  213. struct pci_bus *bus;
  214. int ret;
  215. align = pci_resource_alignment(dev, res);
  216. if (!align) {
  217. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  218. "(bogus alignment)\n", resno, res);
  219. return -EINVAL;
  220. }
  221. bus = dev->bus;
  222. size = resource_size(res);
  223. ret = _pci_assign_resource(dev, resno, size, align);
  224. /*
  225. * If we failed to assign anything, let's try the address
  226. * where firmware left it. That at least has a chance of
  227. * working, which is better than just leaving it disabled.
  228. */
  229. if (ret < 0 && dev->fw_addr[resno])
  230. ret = pci_revert_fw_address(res, dev, resno, size);
  231. if (!ret) {
  232. res->flags &= ~IORESOURCE_STARTALIGN;
  233. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  234. if (resno < PCI_BRIDGE_RESOURCES)
  235. pci_update_resource(dev, resno);
  236. }
  237. return ret;
  238. }
  239. /* Sort resources by alignment */
  240. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  241. {
  242. int i;
  243. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  244. struct resource *r;
  245. struct resource_list *list, *tmp;
  246. resource_size_t r_align;
  247. r = &dev->resource[i];
  248. if (r->flags & IORESOURCE_PCI_FIXED)
  249. continue;
  250. if (!(r->flags) || r->parent)
  251. continue;
  252. r_align = pci_resource_alignment(dev, r);
  253. if (!r_align) {
  254. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  255. i, r);
  256. continue;
  257. }
  258. for (list = head; ; list = list->next) {
  259. resource_size_t align = 0;
  260. struct resource_list *ln = list->next;
  261. if (ln)
  262. align = pci_resource_alignment(ln->dev, ln->res);
  263. if (r_align > align) {
  264. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  265. if (!tmp)
  266. panic("pdev_sort_resources(): "
  267. "kmalloc() failed!\n");
  268. tmp->next = ln;
  269. tmp->res = r;
  270. tmp->dev = dev;
  271. list->next = tmp;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. int pci_enable_resources(struct pci_dev *dev, int mask)
  278. {
  279. u16 cmd, old_cmd;
  280. int i;
  281. struct resource *r;
  282. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  283. old_cmd = cmd;
  284. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  285. if (!(mask & (1 << i)))
  286. continue;
  287. r = &dev->resource[i];
  288. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  289. continue;
  290. if ((i == PCI_ROM_RESOURCE) &&
  291. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  292. continue;
  293. if (!r->parent) {
  294. dev_err(&dev->dev, "device not available "
  295. "(can't reserve %pR)\n", r);
  296. return -EINVAL;
  297. }
  298. if (r->flags & IORESOURCE_IO)
  299. cmd |= PCI_COMMAND_IO;
  300. if (r->flags & IORESOURCE_MEM)
  301. cmd |= PCI_COMMAND_MEMORY;
  302. }
  303. if (cmd != old_cmd) {
  304. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  305. old_cmd, cmd);
  306. pci_write_config_word(dev, PCI_COMMAND, cmd);
  307. }
  308. return 0;
  309. }