setup-res.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. return -EINVAL;
  105. }
  106. conflict = request_resource_conflict(root, res);
  107. if (conflict) {
  108. dev_info(&dev->dev, "can't claim BAR %d %pR: address conflict with %s %pR\n",
  109. resource, res, conflict->name, conflict);
  110. return -EBUSY;
  111. }
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(pci_claim_resource);
  115. void pci_disable_bridge_window(struct pci_dev *dev)
  116. {
  117. dev_info(&dev->dev, "disabling bridge mem windows\n");
  118. /* MMIO Base/Limit */
  119. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  120. /* Prefetchable MMIO Base/Limit */
  121. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  122. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  123. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  124. }
  125. /*
  126. * Generic function that returns a value indicating that the device's
  127. * original BIOS BAR address was not saved and so is not available for
  128. * reinstatement.
  129. *
  130. * Can be over-ridden by architecture specific code that implements
  131. * reinstatement functionality rather than leaving it disabled when
  132. * normal allocation attempts fail.
  133. */
  134. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  135. {
  136. return 0;
  137. }
  138. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  139. int resno, resource_size_t size)
  140. {
  141. struct resource *root, *conflict;
  142. resource_size_t fw_addr, start, end;
  143. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  144. if (!fw_addr)
  145. return -ENOMEM;
  146. start = res->start;
  147. end = res->end;
  148. res->start = fw_addr;
  149. res->end = res->start + size - 1;
  150. root = pci_find_parent_resource(dev, res);
  151. if (!root) {
  152. if (res->flags & IORESOURCE_IO)
  153. root = &ioport_resource;
  154. else
  155. root = &iomem_resource;
  156. }
  157. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  158. resno, res);
  159. conflict = request_resource_conflict(root, res);
  160. if (conflict) {
  161. dev_info(&dev->dev, "BAR %d: %pR conflicts with %s %pR\n",
  162. resno, res, conflict->name, conflict);
  163. res->start = start;
  164. res->end = end;
  165. return -EBUSY;
  166. }
  167. return 0;
  168. }
  169. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  170. int resno, resource_size_t size, resource_size_t align)
  171. {
  172. struct resource *res = dev->resource + resno;
  173. resource_size_t min;
  174. int ret;
  175. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  176. /*
  177. * First, try exact prefetching match. Even if a 64-bit
  178. * prefetchable bridge window is below 4GB, we can't put a 32-bit
  179. * prefetchable resource in it because pbus_size_mem() assumes a
  180. * 64-bit window will contain no 32-bit resources. If we assign
  181. * things differently than they were sized, not everything will fit.
  182. */
  183. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  184. IORESOURCE_PREFETCH | IORESOURCE_MEM_64,
  185. pcibios_align_resource, dev);
  186. if (ret == 0)
  187. return 0;
  188. /*
  189. * If the prefetchable window is only 32 bits wide, we can put
  190. * 64-bit prefetchable resources in it.
  191. */
  192. if ((res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) ==
  193. (IORESOURCE_PREFETCH | IORESOURCE_MEM_64)) {
  194. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  195. IORESOURCE_PREFETCH,
  196. pcibios_align_resource, dev);
  197. if (ret == 0)
  198. return 0;
  199. }
  200. /*
  201. * If we didn't find a better match, we can put any memory resource
  202. * in a non-prefetchable window. If this resource is 32 bits and
  203. * non-prefetchable, the first call already tried the only possibility
  204. * so we don't need to try again.
  205. */
  206. if (res->flags & (IORESOURCE_PREFETCH | IORESOURCE_MEM_64))
  207. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  208. pcibios_align_resource, dev);
  209. return ret;
  210. }
  211. static int _pci_assign_resource(struct pci_dev *dev, int resno,
  212. resource_size_t size, resource_size_t min_align)
  213. {
  214. struct pci_bus *bus;
  215. int ret;
  216. bus = dev->bus;
  217. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  218. if (!bus->parent || !bus->self->transparent)
  219. break;
  220. bus = bus->parent;
  221. }
  222. return ret;
  223. }
  224. int pci_assign_resource(struct pci_dev *dev, int resno)
  225. {
  226. struct resource *res = dev->resource + resno;
  227. resource_size_t align, size;
  228. int ret;
  229. res->flags |= IORESOURCE_UNSET;
  230. align = pci_resource_alignment(dev, res);
  231. if (!align) {
  232. dev_info(&dev->dev, "BAR %d: can't assign %pR (bogus alignment)\n",
  233. resno, res);
  234. return -EINVAL;
  235. }
  236. size = resource_size(res);
  237. ret = _pci_assign_resource(dev, resno, size, align);
  238. /*
  239. * If we failed to assign anything, let's try the address
  240. * where firmware left it. That at least has a chance of
  241. * working, which is better than just leaving it disabled.
  242. */
  243. if (ret < 0) {
  244. dev_info(&dev->dev, "BAR %d: no space for %pR\n", resno, res);
  245. ret = pci_revert_fw_address(res, dev, resno, size);
  246. }
  247. if (ret < 0) {
  248. dev_info(&dev->dev, "BAR %d: failed to assign %pR\n", resno,
  249. res);
  250. return ret;
  251. }
  252. res->flags &= ~IORESOURCE_UNSET;
  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. return 0;
  258. }
  259. EXPORT_SYMBOL(pci_assign_resource);
  260. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  261. resource_size_t min_align)
  262. {
  263. struct resource *res = dev->resource + resno;
  264. unsigned long flags;
  265. resource_size_t new_size;
  266. int ret;
  267. flags = res->flags;
  268. res->flags |= IORESOURCE_UNSET;
  269. if (!res->parent) {
  270. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR\n",
  271. resno, res);
  272. return -EINVAL;
  273. }
  274. /* already aligned with min_align */
  275. new_size = resource_size(res) + addsize;
  276. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  277. if (ret) {
  278. res->flags = flags;
  279. dev_info(&dev->dev, "BAR %d: %pR (failed to expand by %#llx)\n",
  280. resno, res, (unsigned long long) addsize);
  281. return ret;
  282. }
  283. res->flags &= ~IORESOURCE_UNSET;
  284. res->flags &= ~IORESOURCE_STARTALIGN;
  285. dev_info(&dev->dev, "BAR %d: reassigned %pR (expanded by %#llx)\n",
  286. resno, res, (unsigned long long) addsize);
  287. if (resno < PCI_BRIDGE_RESOURCES)
  288. pci_update_resource(dev, resno);
  289. return 0;
  290. }
  291. int pci_enable_resources(struct pci_dev *dev, int mask)
  292. {
  293. u16 cmd, old_cmd;
  294. int i;
  295. struct resource *r;
  296. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  297. old_cmd = cmd;
  298. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  299. if (!(mask & (1 << i)))
  300. continue;
  301. r = &dev->resource[i];
  302. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  303. continue;
  304. if ((i == PCI_ROM_RESOURCE) &&
  305. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  306. continue;
  307. if (r->flags & IORESOURCE_UNSET) {
  308. dev_err(&dev->dev, "can't enable device: BAR %d %pR not assigned\n",
  309. i, r);
  310. return -EINVAL;
  311. }
  312. if (!r->parent) {
  313. dev_err(&dev->dev, "can't enable device: BAR %d %pR not claimed\n",
  314. i, r);
  315. return -EINVAL;
  316. }
  317. if (r->flags & IORESOURCE_IO)
  318. cmd |= PCI_COMMAND_IO;
  319. if (r->flags & IORESOURCE_MEM)
  320. cmd |= PCI_COMMAND_MEMORY;
  321. }
  322. if (cmd != old_cmd) {
  323. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  324. old_cmd, cmd);
  325. pci_write_config_word(dev, PCI_COMMAND, cmd);
  326. }
  327. return 0;
  328. }