setup-res.c 8.8 KB

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