setup-res.c 11 KB

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