setup-res.c 11 KB

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