setup-res.c 8.3 KB

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