setup-res.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_dbg(&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. /*
  135. * Generic function that returns a value indicating that the device's
  136. * original BIOS BAR address was not saved and so is not available for
  137. * reinstatement.
  138. *
  139. * Can be over-ridden by architecture specific code that implements
  140. * reinstatement functionality rather than leaving it disabled when
  141. * normal allocation attempts fail.
  142. */
  143. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  144. {
  145. return 0;
  146. }
  147. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  148. int resno, resource_size_t size)
  149. {
  150. struct resource *root, *conflict;
  151. resource_size_t fw_addr, start, end;
  152. int ret = 0;
  153. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  154. if (!fw_addr)
  155. return 1;
  156. start = res->start;
  157. end = res->end;
  158. res->start = fw_addr;
  159. res->end = res->start + size - 1;
  160. root = pci_find_parent_resource(dev, res);
  161. if (!root) {
  162. if (res->flags & IORESOURCE_IO)
  163. root = &ioport_resource;
  164. else
  165. root = &iomem_resource;
  166. }
  167. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  168. resno, res);
  169. conflict = request_resource_conflict(root, res);
  170. if (conflict) {
  171. dev_info(&dev->dev,
  172. "BAR %d: %pR conflicts with %s %pR\n", resno,
  173. res, conflict->name, conflict);
  174. res->start = start;
  175. res->end = end;
  176. ret = 1;
  177. }
  178. return ret;
  179. }
  180. static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
  181. {
  182. struct resource *res = dev->resource + resno;
  183. struct pci_bus *bus;
  184. int ret;
  185. char *type;
  186. bus = dev->bus;
  187. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  188. if (!bus->parent || !bus->self->transparent)
  189. break;
  190. bus = bus->parent;
  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. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  209. resource_size_t min_align)
  210. {
  211. struct resource *res = dev->resource + resno;
  212. resource_size_t new_size;
  213. int ret;
  214. if (!res->parent) {
  215. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
  216. "\n", resno, res);
  217. return -EINVAL;
  218. }
  219. /* already aligned with min_align */
  220. new_size = resource_size(res) + addsize;
  221. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  222. if (!ret) {
  223. res->flags &= ~IORESOURCE_STARTALIGN;
  224. dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
  225. if (resno < PCI_BRIDGE_RESOURCES)
  226. pci_update_resource(dev, resno);
  227. }
  228. return ret;
  229. }
  230. int pci_assign_resource(struct pci_dev *dev, int resno)
  231. {
  232. struct resource *res = dev->resource + resno;
  233. resource_size_t align, size;
  234. struct pci_bus *bus;
  235. int ret;
  236. align = pci_resource_alignment(dev, res);
  237. if (!align) {
  238. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  239. "(bogus alignment)\n", resno, res);
  240. return -EINVAL;
  241. }
  242. bus = dev->bus;
  243. size = resource_size(res);
  244. ret = _pci_assign_resource(dev, resno, size, align);
  245. /*
  246. * If we failed to assign anything, let's try the address
  247. * where firmware left it. That at least has a chance of
  248. * working, which is better than just leaving it disabled.
  249. */
  250. if (ret < 0)
  251. ret = pci_revert_fw_address(res, dev, resno, size);
  252. if (!ret) {
  253. res->flags &= ~IORESOURCE_STARTALIGN;
  254. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  255. if (resno < PCI_BRIDGE_RESOURCES)
  256. pci_update_resource(dev, resno);
  257. }
  258. return ret;
  259. }
  260. int pci_enable_resources(struct pci_dev *dev, int mask)
  261. {
  262. u16 cmd, old_cmd;
  263. int i;
  264. struct resource *r;
  265. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  266. old_cmd = cmd;
  267. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  268. if (!(mask & (1 << i)))
  269. continue;
  270. r = &dev->resource[i];
  271. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  272. continue;
  273. if ((i == PCI_ROM_RESOURCE) &&
  274. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  275. continue;
  276. if (!r->parent) {
  277. dev_err(&dev->dev, "device not available "
  278. "(can't reserve %pR)\n", r);
  279. return -EINVAL;
  280. }
  281. if (r->flags & IORESOURCE_IO)
  282. cmd |= PCI_COMMAND_IO;
  283. if (r->flags & IORESOURCE_MEM)
  284. cmd |= PCI_COMMAND_MEMORY;
  285. }
  286. if (cmd != old_cmd) {
  287. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  288. old_cmd, cmd);
  289. pci_write_config_word(dev, PCI_COMMAND, cmd);
  290. }
  291. return 0;
  292. }