setup-res.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/export.h>
  19. #include <linux/pci.h>
  20. #include <linux/errno.h>
  21. #include <linux/ioport.h>
  22. #include <linux/cache.h>
  23. #include <linux/slab.h>
  24. #include "pci.h"
  25. void pci_update_resource(struct pci_dev *dev, int resno)
  26. {
  27. struct pci_bus_region region;
  28. bool disable;
  29. u16 cmd;
  30. u32 new, check, mask;
  31. int reg;
  32. enum pci_bar_type type;
  33. struct resource *res = dev->resource + resno;
  34. /*
  35. * Ignore resources for unimplemented BARs and unused resource slots
  36. * for 64 bit BARs.
  37. */
  38. if (!res->flags)
  39. return;
  40. if (res->flags & IORESOURCE_UNSET)
  41. return;
  42. /*
  43. * Ignore non-moveable resources. This might be legacy resources for
  44. * which no functional BAR register exists or another important
  45. * system resource we shouldn't move around.
  46. */
  47. if (res->flags & IORESOURCE_PCI_FIXED)
  48. return;
  49. pcibios_resource_to_bus(dev->bus, &region, res);
  50. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  51. if (res->flags & IORESOURCE_IO)
  52. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  53. else
  54. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  55. reg = pci_resource_bar(dev, resno, &type);
  56. if (!reg)
  57. return;
  58. if (type != pci_bar_unknown) {
  59. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  60. return;
  61. new |= PCI_ROM_ADDRESS_ENABLE;
  62. }
  63. /*
  64. * We can't update a 64-bit BAR atomically, so when possible,
  65. * disable decoding so that a half-updated BAR won't conflict
  66. * with another device.
  67. */
  68. disable = (res->flags & IORESOURCE_MEM_64) && !dev->mmio_always_on;
  69. if (disable) {
  70. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  71. pci_write_config_word(dev, PCI_COMMAND,
  72. cmd & ~PCI_COMMAND_MEMORY);
  73. }
  74. pci_write_config_dword(dev, reg, new);
  75. pci_read_config_dword(dev, reg, &check);
  76. if ((new ^ check) & mask) {
  77. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  78. resno, new, check);
  79. }
  80. if (res->flags & IORESOURCE_MEM_64) {
  81. new = region.start >> 16 >> 16;
  82. pci_write_config_dword(dev, reg + 4, new);
  83. pci_read_config_dword(dev, reg + 4, &check);
  84. if (check != new) {
  85. dev_err(&dev->dev, "BAR %d: error updating "
  86. "(high %#08x != %#08x)\n", resno, new, check);
  87. }
  88. }
  89. if (disable)
  90. pci_write_config_word(dev, PCI_COMMAND, cmd);
  91. }
  92. int pci_claim_resource(struct pci_dev *dev, int resource)
  93. {
  94. struct resource *res = &dev->resource[resource];
  95. struct resource *root, *conflict;
  96. if (res->flags & IORESOURCE_UNSET) {
  97. dev_info(&dev->dev, "can't claim BAR %d %pR: no address assigned\n",
  98. resource, res);
  99. return -EINVAL;
  100. }
  101. root = pci_find_parent_resource(dev, res);
  102. if (!root) {
  103. dev_info(&dev->dev, "can't claim BAR %d %pR: no compatible bridge window\n",
  104. resource, res);
  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. return -EBUSY;
  112. }
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(pci_claim_resource);
  116. void pci_disable_bridge_window(struct pci_dev *dev)
  117. {
  118. dev_info(&dev->dev, "disabling bridge mem windows\n");
  119. /* MMIO Base/Limit */
  120. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  121. /* Prefetchable MMIO Base/Limit */
  122. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  123. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  124. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  125. }
  126. /*
  127. * Generic function that returns a value indicating that the device's
  128. * original BIOS BAR address was not saved and so is not available for
  129. * reinstatement.
  130. *
  131. * Can be over-ridden by architecture specific code that implements
  132. * reinstatement functionality rather than leaving it disabled when
  133. * normal allocation attempts fail.
  134. */
  135. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  136. {
  137. return 0;
  138. }
  139. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  140. int resno, resource_size_t size)
  141. {
  142. struct resource *root, *conflict;
  143. resource_size_t fw_addr, start, end;
  144. int ret = 0;
  145. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  146. if (!fw_addr)
  147. return 1;
  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,
  164. "BAR %d: %pR conflicts with %s %pR\n", resno,
  165. res, conflict->name, conflict);
  166. res->start = start;
  167. res->end = end;
  168. ret = 1;
  169. }
  170. return ret;
  171. }
  172. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  173. int resno, resource_size_t size, resource_size_t align)
  174. {
  175. struct resource *res = dev->resource + resno;
  176. resource_size_t min;
  177. int ret;
  178. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  179. /* First, try exact prefetching match.. */
  180. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  181. IORESOURCE_PREFETCH,
  182. pcibios_align_resource, dev);
  183. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  184. /*
  185. * That failed.
  186. *
  187. * But a prefetching area can handle a non-prefetching
  188. * window (it will just not perform as well).
  189. */
  190. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  191. pcibios_align_resource, dev);
  192. }
  193. return ret;
  194. }
  195. static int _pci_assign_resource(struct pci_dev *dev, int resno,
  196. resource_size_t size, resource_size_t min_align)
  197. {
  198. struct resource *res = dev->resource + resno;
  199. struct pci_bus *bus;
  200. int ret;
  201. char *type;
  202. bus = dev->bus;
  203. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  204. if (!bus->parent || !bus->self->transparent)
  205. break;
  206. bus = bus->parent;
  207. }
  208. if (ret) {
  209. if (res->flags & IORESOURCE_MEM)
  210. if (res->flags & IORESOURCE_PREFETCH)
  211. type = "mem pref";
  212. else
  213. type = "mem";
  214. else if (res->flags & IORESOURCE_IO)
  215. type = "io";
  216. else
  217. type = "unknown";
  218. dev_info(&dev->dev,
  219. "BAR %d: can't assign %s (size %#llx)\n",
  220. resno, type, (unsigned long long) resource_size(res));
  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 "
  233. "(bogus alignment)\n", 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. ret = pci_revert_fw_address(res, dev, resno, size);
  245. if (!ret) {
  246. res->flags &= ~IORESOURCE_UNSET;
  247. res->flags &= ~IORESOURCE_STARTALIGN;
  248. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  249. if (resno < PCI_BRIDGE_RESOURCES)
  250. pci_update_resource(dev, resno);
  251. }
  252. return ret;
  253. }
  254. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  255. resource_size_t min_align)
  256. {
  257. struct resource *res = dev->resource + resno;
  258. resource_size_t new_size;
  259. int ret;
  260. res->flags |= IORESOURCE_UNSET;
  261. if (!res->parent) {
  262. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
  263. "\n", resno, res);
  264. return -EINVAL;
  265. }
  266. /* already aligned with min_align */
  267. new_size = resource_size(res) + addsize;
  268. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  269. if (!ret) {
  270. res->flags &= ~IORESOURCE_UNSET;
  271. res->flags &= ~IORESOURCE_STARTALIGN;
  272. dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
  273. if (resno < PCI_BRIDGE_RESOURCES)
  274. pci_update_resource(dev, resno);
  275. }
  276. return ret;
  277. }
  278. int pci_enable_resources(struct pci_dev *dev, int mask)
  279. {
  280. u16 cmd, old_cmd;
  281. int i;
  282. struct resource *r;
  283. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  284. old_cmd = cmd;
  285. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  286. if (!(mask & (1 << i)))
  287. continue;
  288. r = &dev->resource[i];
  289. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  290. continue;
  291. if ((i == PCI_ROM_RESOURCE) &&
  292. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  293. continue;
  294. if (r->flags & IORESOURCE_UNSET) {
  295. dev_err(&dev->dev, "can't enable device: BAR %d %pR not assigned\n",
  296. i, r);
  297. return -EINVAL;
  298. }
  299. if (!r->parent) {
  300. dev_err(&dev->dev, "can't enable device: BAR %d %pR not claimed\n",
  301. i, r);
  302. return -EINVAL;
  303. }
  304. if (r->flags & IORESOURCE_IO)
  305. cmd |= PCI_COMMAND_IO;
  306. if (r->flags & IORESOURCE_MEM)
  307. cmd |= PCI_COMMAND_MEMORY;
  308. }
  309. if (cmd != old_cmd) {
  310. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  311. old_cmd, cmd);
  312. pci_write_config_word(dev, PCI_COMMAND, cmd);
  313. }
  314. return 0;
  315. }