setup-res.c 9.9 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 "
  85. "(high %#08x != %#08x)\n", 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. int ret = 0;
  144. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  145. if (!fw_addr)
  146. return 1;
  147. start = res->start;
  148. end = res->end;
  149. res->start = fw_addr;
  150. res->end = res->start + size - 1;
  151. root = pci_find_parent_resource(dev, res);
  152. if (!root) {
  153. if (res->flags & IORESOURCE_IO)
  154. root = &ioport_resource;
  155. else
  156. root = &iomem_resource;
  157. }
  158. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  159. resno, res);
  160. conflict = request_resource_conflict(root, res);
  161. if (conflict) {
  162. dev_info(&dev->dev,
  163. "BAR %d: %pR conflicts with %s %pR\n", resno,
  164. res, conflict->name, conflict);
  165. res->start = start;
  166. res->end = end;
  167. ret = 1;
  168. }
  169. return ret;
  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 resource *res = dev->resource + resno;
  217. struct pci_bus *bus;
  218. int ret;
  219. char *type;
  220. bus = dev->bus;
  221. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  222. if (!bus->parent || !bus->self->transparent)
  223. break;
  224. bus = bus->parent;
  225. }
  226. if (ret) {
  227. if (res->flags & IORESOURCE_MEM)
  228. if (res->flags & IORESOURCE_PREFETCH)
  229. type = "mem pref";
  230. else
  231. type = "mem";
  232. else if (res->flags & IORESOURCE_IO)
  233. type = "io";
  234. else
  235. type = "unknown";
  236. dev_info(&dev->dev,
  237. "BAR %d: can't assign %s (size %#llx)\n",
  238. resno, type, (unsigned long long) resource_size(res));
  239. }
  240. return ret;
  241. }
  242. int pci_assign_resource(struct pci_dev *dev, int resno)
  243. {
  244. struct resource *res = dev->resource + resno;
  245. resource_size_t align, size;
  246. int ret;
  247. res->flags |= IORESOURCE_UNSET;
  248. align = pci_resource_alignment(dev, res);
  249. if (!align) {
  250. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  251. "(bogus alignment)\n", resno, res);
  252. return -EINVAL;
  253. }
  254. size = resource_size(res);
  255. ret = _pci_assign_resource(dev, resno, size, align);
  256. /*
  257. * If we failed to assign anything, let's try the address
  258. * where firmware left it. That at least has a chance of
  259. * working, which is better than just leaving it disabled.
  260. */
  261. if (ret < 0)
  262. ret = pci_revert_fw_address(res, dev, resno, size);
  263. if (!ret) {
  264. res->flags &= ~IORESOURCE_UNSET;
  265. res->flags &= ~IORESOURCE_STARTALIGN;
  266. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  267. if (resno < PCI_BRIDGE_RESOURCES)
  268. pci_update_resource(dev, resno);
  269. }
  270. return ret;
  271. }
  272. EXPORT_SYMBOL(pci_assign_resource);
  273. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  274. resource_size_t min_align)
  275. {
  276. struct resource *res = dev->resource + resno;
  277. resource_size_t new_size;
  278. int ret;
  279. res->flags |= IORESOURCE_UNSET;
  280. if (!res->parent) {
  281. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
  282. "\n", resno, res);
  283. return -EINVAL;
  284. }
  285. /* already aligned with min_align */
  286. new_size = resource_size(res) + addsize;
  287. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  288. if (!ret) {
  289. res->flags &= ~IORESOURCE_UNSET;
  290. res->flags &= ~IORESOURCE_STARTALIGN;
  291. dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
  292. if (resno < PCI_BRIDGE_RESOURCES)
  293. pci_update_resource(dev, resno);
  294. }
  295. return ret;
  296. }
  297. int pci_enable_resources(struct pci_dev *dev, int mask)
  298. {
  299. u16 cmd, old_cmd;
  300. int i;
  301. struct resource *r;
  302. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  303. old_cmd = cmd;
  304. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  305. if (!(mask & (1 << i)))
  306. continue;
  307. r = &dev->resource[i];
  308. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  309. continue;
  310. if ((i == PCI_ROM_RESOURCE) &&
  311. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  312. continue;
  313. if (r->flags & IORESOURCE_UNSET) {
  314. dev_err(&dev->dev, "can't enable device: BAR %d %pR not assigned\n",
  315. i, r);
  316. return -EINVAL;
  317. }
  318. if (!r->parent) {
  319. dev_err(&dev->dev, "can't enable device: BAR %d %pR not claimed\n",
  320. i, r);
  321. return -EINVAL;
  322. }
  323. if (r->flags & IORESOURCE_IO)
  324. cmd |= PCI_COMMAND_IO;
  325. if (r->flags & IORESOURCE_MEM)
  326. cmd |= PCI_COMMAND_MEMORY;
  327. }
  328. if (cmd != old_cmd) {
  329. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  330. old_cmd, cmd);
  331. pci_write_config_word(dev, PCI_COMMAND, cmd);
  332. }
  333. return 0;
  334. }