setup-res.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/export.h>
  19. #include <linux/pci.h>
  20. #include <linux/errno.h>
  21. #include <linux/ioport.h>
  22. #include <linux/cache.h>
  23. #include <linux/slab.h>
  24. #include "pci.h"
  25. void pci_update_resource(struct pci_dev *dev, int resno)
  26. {
  27. struct pci_bus_region region;
  28. u32 new, check, mask;
  29. int reg;
  30. enum pci_bar_type type;
  31. struct resource *res = dev->resource + resno;
  32. /*
  33. * Ignore resources for unimplemented BARs and unused resource slots
  34. * for 64 bit BARs.
  35. */
  36. if (!res->flags)
  37. return;
  38. /*
  39. * Ignore non-moveable resources. This might be legacy resources for
  40. * which no functional BAR register exists or another important
  41. * system resource we shouldn't move around.
  42. */
  43. if (res->flags & IORESOURCE_PCI_FIXED)
  44. return;
  45. pcibios_resource_to_bus(dev, &region, res);
  46. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  47. if (res->flags & IORESOURCE_IO)
  48. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  49. else
  50. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  51. reg = pci_resource_bar(dev, resno, &type);
  52. if (!reg)
  53. return;
  54. if (type != pci_bar_unknown) {
  55. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  56. return;
  57. new |= PCI_ROM_ADDRESS_ENABLE;
  58. }
  59. pci_write_config_dword(dev, reg, new);
  60. pci_read_config_dword(dev, reg, &check);
  61. if ((new ^ check) & mask) {
  62. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  63. resno, new, check);
  64. }
  65. if (res->flags & IORESOURCE_MEM_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_info(&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_info(&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, resource_size_t size, resource_size_t align)
  113. {
  114. struct resource *res = dev->resource + resno;
  115. resource_size_t min;
  116. int ret;
  117. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  118. /* First, try exact prefetching match.. */
  119. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  120. IORESOURCE_PREFETCH,
  121. pcibios_align_resource, dev);
  122. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  123. /*
  124. * That failed.
  125. *
  126. * But a prefetching area can handle a non-prefetching
  127. * window (it will just not perform as well).
  128. */
  129. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  130. pcibios_align_resource, dev);
  131. }
  132. return ret;
  133. }
  134. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  135. int resno, resource_size_t size)
  136. {
  137. struct resource *root, *conflict;
  138. resource_size_t start, end;
  139. int ret = 0;
  140. if (res->flags & IORESOURCE_IO)
  141. root = &ioport_resource;
  142. else
  143. root = &iomem_resource;
  144. start = res->start;
  145. end = res->end;
  146. res->start = dev->fw_addr[resno];
  147. res->end = res->start + size - 1;
  148. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  149. resno, res);
  150. conflict = request_resource_conflict(root, res);
  151. if (conflict) {
  152. dev_info(&dev->dev,
  153. "BAR %d: %pR conflicts with %s %pR\n", resno,
  154. res, conflict->name, conflict);
  155. res->start = start;
  156. res->end = end;
  157. ret = 1;
  158. }
  159. return ret;
  160. }
  161. static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
  162. {
  163. struct resource *res = dev->resource + resno;
  164. struct pci_bus *bus;
  165. int ret;
  166. char *type;
  167. bus = dev->bus;
  168. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  169. if (!bus->parent || !bus->self->transparent)
  170. break;
  171. bus = bus->parent;
  172. }
  173. if (ret) {
  174. if (res->flags & IORESOURCE_MEM)
  175. if (res->flags & IORESOURCE_PREFETCH)
  176. type = "mem pref";
  177. else
  178. type = "mem";
  179. else if (res->flags & IORESOURCE_IO)
  180. type = "io";
  181. else
  182. type = "unknown";
  183. dev_info(&dev->dev,
  184. "BAR %d: can't assign %s (size %#llx)\n",
  185. resno, type, (unsigned long long) resource_size(res));
  186. }
  187. return ret;
  188. }
  189. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  190. resource_size_t min_align)
  191. {
  192. struct resource *res = dev->resource + resno;
  193. resource_size_t new_size;
  194. int ret;
  195. if (!res->parent) {
  196. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resouce %pR "
  197. "\n", resno, res);
  198. return -EINVAL;
  199. }
  200. new_size = resource_size(res) + addsize + min_align;
  201. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  202. if (!ret) {
  203. res->flags &= ~IORESOURCE_STARTALIGN;
  204. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  205. if (resno < PCI_BRIDGE_RESOURCES)
  206. pci_update_resource(dev, resno);
  207. }
  208. return ret;
  209. }
  210. int pci_assign_resource(struct pci_dev *dev, int resno)
  211. {
  212. struct resource *res = dev->resource + resno;
  213. resource_size_t align, size;
  214. struct pci_bus *bus;
  215. int ret;
  216. align = pci_resource_alignment(dev, res);
  217. if (!align) {
  218. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  219. "(bogus alignment)\n", resno, res);
  220. return -EINVAL;
  221. }
  222. bus = dev->bus;
  223. size = resource_size(res);
  224. ret = _pci_assign_resource(dev, resno, size, align);
  225. /*
  226. * If we failed to assign anything, let's try the address
  227. * where firmware left it. That at least has a chance of
  228. * working, which is better than just leaving it disabled.
  229. */
  230. if (ret < 0 && dev->fw_addr[resno])
  231. ret = pci_revert_fw_address(res, dev, resno, size);
  232. if (!ret) {
  233. res->flags &= ~IORESOURCE_STARTALIGN;
  234. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  235. if (resno < PCI_BRIDGE_RESOURCES)
  236. pci_update_resource(dev, resno);
  237. }
  238. return ret;
  239. }
  240. /* Sort resources by alignment */
  241. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  242. {
  243. int i;
  244. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  245. struct resource *r;
  246. struct resource_list *list, *tmp;
  247. resource_size_t r_align;
  248. r = &dev->resource[i];
  249. if (r->flags & IORESOURCE_PCI_FIXED)
  250. continue;
  251. if (!(r->flags) || r->parent)
  252. continue;
  253. r_align = pci_resource_alignment(dev, r);
  254. if (!r_align) {
  255. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  256. i, r);
  257. continue;
  258. }
  259. for (list = head; ; list = list->next) {
  260. resource_size_t align = 0;
  261. struct resource_list *ln = list->next;
  262. if (ln)
  263. align = pci_resource_alignment(ln->dev, ln->res);
  264. if (r_align > align) {
  265. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  266. if (!tmp)
  267. panic("pdev_sort_resources(): "
  268. "kmalloc() failed!\n");
  269. tmp->next = ln;
  270. tmp->res = r;
  271. tmp->dev = dev;
  272. list->next = tmp;
  273. break;
  274. }
  275. }
  276. }
  277. }
  278. int pci_enable_resources(struct pci_dev *dev, int mask)
  279. {
  280. u16 cmd, old_cmd;
  281. int i;
  282. struct resource *r;
  283. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  284. old_cmd = cmd;
  285. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  286. if (!(mask & (1 << i)))
  287. continue;
  288. r = &dev->resource[i];
  289. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  290. continue;
  291. if ((i == PCI_ROM_RESOURCE) &&
  292. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  293. continue;
  294. if (!r->parent) {
  295. dev_err(&dev->dev, "device not available "
  296. "(can't reserve %pR)\n", r);
  297. return -EINVAL;
  298. }
  299. if (r->flags & IORESOURCE_IO)
  300. cmd |= PCI_COMMAND_IO;
  301. if (r->flags & IORESOURCE_MEM)
  302. cmd |= PCI_COMMAND_MEMORY;
  303. }
  304. if (cmd != old_cmd) {
  305. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  306. old_cmd, cmd);
  307. pci_write_config_word(dev, PCI_COMMAND, cmd);
  308. }
  309. return 0;
  310. }