setup-res.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/kernel.h>
  17. #include <linux/export.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. bool disable;
  28. u16 cmd;
  29. u32 new, check, mask;
  30. int reg;
  31. enum pci_bar_type type;
  32. struct resource *res = dev->resource + resno;
  33. /*
  34. * Ignore resources for unimplemented BARs and unused resource slots
  35. * for 64 bit BARs.
  36. */
  37. if (!res->flags)
  38. return;
  39. if (res->flags & IORESOURCE_UNSET)
  40. return;
  41. /*
  42. * Ignore non-moveable resources. This might be legacy resources for
  43. * which no functional BAR register exists or another important
  44. * system resource we shouldn't move around.
  45. */
  46. if (res->flags & IORESOURCE_PCI_FIXED)
  47. return;
  48. pcibios_resource_to_bus(dev->bus, &region, res);
  49. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  50. if (res->flags & IORESOURCE_IO)
  51. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  52. else
  53. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  54. reg = pci_resource_bar(dev, resno, &type);
  55. if (!reg)
  56. return;
  57. if (type != pci_bar_unknown) {
  58. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  59. return;
  60. new |= PCI_ROM_ADDRESS_ENABLE;
  61. }
  62. /*
  63. * We can't update a 64-bit BAR atomically, so when possible,
  64. * disable decoding so that a half-updated BAR won't conflict
  65. * with another device.
  66. */
  67. disable = (res->flags & IORESOURCE_MEM_64) && !dev->mmio_always_on;
  68. if (disable) {
  69. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  70. pci_write_config_word(dev, PCI_COMMAND,
  71. cmd & ~PCI_COMMAND_MEMORY);
  72. }
  73. pci_write_config_dword(dev, reg, new);
  74. pci_read_config_dword(dev, reg, &check);
  75. if ((new ^ check) & mask) {
  76. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  77. resno, new, check);
  78. }
  79. if (res->flags & IORESOURCE_MEM_64) {
  80. new = region.start >> 16 >> 16;
  81. pci_write_config_dword(dev, reg + 4, new);
  82. pci_read_config_dword(dev, reg + 4, &check);
  83. if (check != new) {
  84. dev_err(&dev->dev, "BAR %d: error updating (high %#08x != %#08x)\n",
  85. resno, new, check);
  86. }
  87. }
  88. if (disable)
  89. pci_write_config_word(dev, PCI_COMMAND, cmd);
  90. }
  91. int pci_claim_resource(struct pci_dev *dev, int resource)
  92. {
  93. struct resource *res = &dev->resource[resource];
  94. struct resource *root, *conflict;
  95. if (res->flags & IORESOURCE_UNSET) {
  96. dev_info(&dev->dev, "can't claim BAR %d %pR: no address assigned\n",
  97. resource, res);
  98. return -EINVAL;
  99. }
  100. root = pci_find_parent_resource(dev, res);
  101. if (!root) {
  102. dev_info(&dev->dev, "can't claim BAR %d %pR: no compatible bridge window\n",
  103. resource, res);
  104. res->flags |= IORESOURCE_UNSET;
  105. return -EINVAL;
  106. }
  107. conflict = request_resource_conflict(root, res);
  108. if (conflict) {
  109. dev_info(&dev->dev, "can't claim BAR %d %pR: address conflict with %s %pR\n",
  110. resource, res, conflict->name, conflict);
  111. res->flags |= IORESOURCE_UNSET;
  112. return -EBUSY;
  113. }
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(pci_claim_resource);
  117. void pci_disable_bridge_window(struct pci_dev *dev)
  118. {
  119. dev_info(&dev->dev, "disabling bridge mem windows\n");
  120. /* MMIO Base/Limit */
  121. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  122. /* Prefetchable MMIO Base/Limit */
  123. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  124. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  125. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  126. }
  127. /*
  128. * Generic function that returns a value indicating that the device's
  129. * original BIOS BAR address was not saved and so is not available for
  130. * reinstatement.
  131. *
  132. * Can be over-ridden by architecture specific code that implements
  133. * reinstatement functionality rather than leaving it disabled when
  134. * normal allocation attempts fail.
  135. */
  136. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  137. {
  138. return 0;
  139. }
  140. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  141. int resno, resource_size_t size)
  142. {
  143. struct resource *root, *conflict;
  144. resource_size_t fw_addr, start, end;
  145. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  146. if (!fw_addr)
  147. return -ENOMEM;
  148. start = res->start;
  149. end = res->end;
  150. res->start = fw_addr;
  151. res->end = res->start + size - 1;
  152. root = pci_find_parent_resource(dev, res);
  153. if (!root) {
  154. if (res->flags & IORESOURCE_IO)
  155. root = &ioport_resource;
  156. else
  157. root = &iomem_resource;
  158. }
  159. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  160. resno, res);
  161. conflict = request_resource_conflict(root, res);
  162. if (conflict) {
  163. dev_info(&dev->dev, "BAR %d: %pR conflicts with %s %pR\n",
  164. resno, res, conflict->name, conflict);
  165. res->start = start;
  166. res->end = end;
  167. return -EBUSY;
  168. }
  169. return 0;
  170. }
  171. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  172. int resno, resource_size_t size, resource_size_t align)
  173. {
  174. struct resource *res = dev->resource + resno;
  175. resource_size_t min;
  176. int ret;
  177. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  178. /*
  179. * First, try exact prefetching match. Even if a 64-bit
  180. * prefetchable bridge window is below 4GB, we can't put a 32-bit
  181. * prefetchable resource in it because pbus_size_mem() assumes a
  182. * 64-bit window will contain no 32-bit resources. If we assign
  183. * things differently than they were sized, not everything will fit.
  184. */
  185. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  186. IORESOURCE_PREFETCH | IORESOURCE_MEM_64,
  187. pcibios_align_resource, dev);
  188. if (ret == 0)
  189. return 0;
  190. /*
  191. * If the prefetchable window is only 32 bits wide, we can put
  192. * 64-bit prefetchable resources in it.
  193. */
  194. if ((res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) ==
  195. (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) {
  196. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  197. IORESOURCE_PREFETCH,
  198. pcibios_align_resource, dev);
  199. if (ret == 0)
  200. return 0;
  201. }
  202. /*
  203. * If we didn't find a better match, we can put any memory resource
  204. * in a non-prefetchable window. If this resource is 32 bits and
  205. * non-prefetchable, the first call already tried the only possibility
  206. * so we don't need to try again.
  207. */
  208. if (res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64))
  209. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  210. pcibios_align_resource, dev);
  211. return ret;
  212. }
  213. static int _pci_assign_resource(struct pci_dev *dev, int resno,
  214. resource_size_t size, resource_size_t min_align)
  215. {
  216. struct pci_bus *bus;
  217. int ret;
  218. bus = dev->bus;
  219. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  220. if (!bus->parent || !bus->self->transparent)
  221. break;
  222. bus = bus->parent;
  223. }
  224. return ret;
  225. }
  226. int pci_assign_resource(struct pci_dev *dev, int resno)
  227. {
  228. struct resource *res = dev->resource + resno;
  229. resource_size_t align, size;
  230. int ret;
  231. res->flags |= IORESOURCE_UNSET;
  232. align = pci_resource_alignment(dev, res);
  233. if (!align) {
  234. dev_info(&dev->dev, "BAR %d: can't assign %pR (bogus alignment)\n",
  235. resno, res);
  236. return -EINVAL;
  237. }
  238. size = resource_size(res);
  239. ret = _pci_assign_resource(dev, resno, size, align);
  240. /*
  241. * If we failed to assign anything, let's try the address
  242. * where firmware left it. That at least has a chance of
  243. * working, which is better than just leaving it disabled.
  244. */
  245. if (ret < 0) {
  246. dev_info(&dev->dev, "BAR %d: no space for %pR\n", resno, res);
  247. ret = pci_revert_fw_address(res, dev, resno, size);
  248. }
  249. if (ret < 0) {
  250. dev_info(&dev->dev, "BAR %d: failed to assign %pR\n", resno,
  251. res);
  252. return ret;
  253. }
  254. res->flags &= ~IORESOURCE_UNSET;
  255. res->flags &= ~IORESOURCE_STARTALIGN;
  256. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  257. if (resno < PCI_BRIDGE_RESOURCES)
  258. pci_update_resource(dev, resno);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(pci_assign_resource);
  262. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  263. resource_size_t min_align)
  264. {
  265. struct resource *res = dev->resource + resno;
  266. unsigned long flags;
  267. resource_size_t new_size;
  268. int ret;
  269. flags = res->flags;
  270. res->flags |= IORESOURCE_UNSET;
  271. if (!res->parent) {
  272. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR\n",
  273. resno, res);
  274. return -EINVAL;
  275. }
  276. /* already aligned with min_align */
  277. new_size = resource_size(res) + addsize;
  278. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  279. if (ret) {
  280. res->flags = flags;
  281. dev_info(&dev->dev, "BAR %d: %pR (failed to expand by %#llx)\n",
  282. resno, res, (unsigned long long) addsize);
  283. return ret;
  284. }
  285. res->flags &= ~IORESOURCE_UNSET;
  286. res->flags &= ~IORESOURCE_STARTALIGN;
  287. dev_info(&dev->dev, "BAR %d: reassigned %pR (expanded by %#llx)\n",
  288. resno, res, (unsigned long long) addsize);
  289. if (resno < PCI_BRIDGE_RESOURCES)
  290. pci_update_resource(dev, resno);
  291. return 0;
  292. }
  293. int pci_enable_resources(struct pci_dev *dev, int mask)
  294. {
  295. u16 cmd, old_cmd;
  296. int i;
  297. struct resource *r;
  298. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  299. old_cmd = cmd;
  300. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  301. if (!(mask & (1 << i)))
  302. continue;
  303. r = &dev->resource[i];
  304. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  305. continue;
  306. if ((i == PCI_ROM_RESOURCE) &&
  307. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  308. continue;
  309. if (r->flags & IORESOURCE_UNSET) {
  310. dev_err(&dev->dev, "can't enable device: BAR %d %pR not assigned\n",
  311. i, r);
  312. return -EINVAL;
  313. }
  314. if (!r->parent) {
  315. dev_err(&dev->dev, "can't enable device: BAR %d %pR not claimed\n",
  316. i, r);
  317. return -EINVAL;
  318. }
  319. if (r->flags & IORESOURCE_IO)
  320. cmd |= PCI_COMMAND_IO;
  321. if (r->flags & IORESOURCE_MEM)
  322. cmd |= PCI_COMMAND_MEMORY;
  323. }
  324. if (cmd != old_cmd) {
  325. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  326. old_cmd, cmd);
  327. pci_write_config_word(dev, PCI_COMMAND, cmd);
  328. }
  329. return 0;
  330. }