setup-res.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #ifdef CONFIG_PCI_QUIRKS
  100. void pci_disable_bridge_window(struct pci_dev *dev)
  101. {
  102. dev_info(&dev->dev, "disabling bridge mem windows\n");
  103. /* MMIO Base/Limit */
  104. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  105. /* Prefetchable MMIO Base/Limit */
  106. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  107. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  108. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  109. }
  110. #endif /* CONFIG_PCI_QUIRKS */
  111. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  112. int resno, resource_size_t size, resource_size_t align)
  113. {
  114. struct resource *res = dev->resource + resno;
  115. resource_size_t min;
  116. int ret;
  117. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  118. /* First, try exact prefetching match.. */
  119. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  120. IORESOURCE_PREFETCH,
  121. pcibios_align_resource, dev);
  122. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  123. /*
  124. * That failed.
  125. *
  126. * But a prefetching area can handle a non-prefetching
  127. * window (it will just not perform as well).
  128. */
  129. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  130. pcibios_align_resource, dev);
  131. }
  132. return ret;
  133. }
  134. /*
  135. * Generic function that returns a value indicating that the device's
  136. * original BIOS BAR address was not saved and so is not available for
  137. * reinstatement.
  138. *
  139. * Can be over-ridden by architecture specific code that implements
  140. * reinstatement functionality rather than leaving it disabled when
  141. * normal allocation attempts fail.
  142. */
  143. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  144. {
  145. return 0;
  146. }
  147. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  148. int resno, resource_size_t size)
  149. {
  150. struct resource *root, *conflict;
  151. resource_size_t fw_addr, start, end;
  152. int ret = 0;
  153. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  154. if (!fw_addr)
  155. return 1;
  156. start = res->start;
  157. end = res->end;
  158. res->start = fw_addr;
  159. res->end = res->start + size - 1;
  160. root = pci_find_parent_resource(dev, res);
  161. if (!root) {
  162. if (res->flags & IORESOURCE_IO)
  163. root = &ioport_resource;
  164. else
  165. root = &iomem_resource;
  166. }
  167. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  168. resno, res);
  169. conflict = request_resource_conflict(root, res);
  170. if (conflict) {
  171. dev_info(&dev->dev,
  172. "BAR %d: %pR conflicts with %s %pR\n", resno,
  173. res, conflict->name, conflict);
  174. res->start = start;
  175. res->end = end;
  176. ret = 1;
  177. }
  178. return ret;
  179. }
  180. static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
  181. {
  182. struct resource *res = dev->resource + resno;
  183. struct pci_bus *bus;
  184. int ret;
  185. char *type;
  186. bus = dev->bus;
  187. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  188. if (!bus->parent || !bus->self->transparent)
  189. break;
  190. bus = bus->parent;
  191. }
  192. if (ret) {
  193. if (res->flags & IORESOURCE_MEM)
  194. if (res->flags & IORESOURCE_PREFETCH)
  195. type = "mem pref";
  196. else
  197. type = "mem";
  198. else if (res->flags & IORESOURCE_IO)
  199. type = "io";
  200. else
  201. type = "unknown";
  202. dev_info(&dev->dev,
  203. "BAR %d: can't assign %s (size %#llx)\n",
  204. resno, type, (unsigned long long) resource_size(res));
  205. }
  206. return ret;
  207. }
  208. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  209. resource_size_t min_align)
  210. {
  211. struct resource *res = dev->resource + resno;
  212. resource_size_t new_size;
  213. int ret;
  214. if (!res->parent) {
  215. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
  216. "\n", resno, res);
  217. return -EINVAL;
  218. }
  219. new_size = resource_size(res) + addsize + min_align;
  220. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  221. if (!ret) {
  222. res->flags &= ~IORESOURCE_STARTALIGN;
  223. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  224. if (resno < PCI_BRIDGE_RESOURCES)
  225. pci_update_resource(dev, resno);
  226. }
  227. return ret;
  228. }
  229. int pci_assign_resource(struct pci_dev *dev, int resno)
  230. {
  231. struct resource *res = dev->resource + resno;
  232. resource_size_t align, size;
  233. struct pci_bus *bus;
  234. int ret;
  235. align = pci_resource_alignment(dev, res);
  236. if (!align) {
  237. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  238. "(bogus alignment)\n", resno, res);
  239. return -EINVAL;
  240. }
  241. bus = dev->bus;
  242. size = resource_size(res);
  243. ret = _pci_assign_resource(dev, resno, size, align);
  244. /*
  245. * If we failed to assign anything, let's try the address
  246. * where firmware left it. That at least has a chance of
  247. * working, which is better than just leaving it disabled.
  248. */
  249. if (ret < 0)
  250. ret = pci_revert_fw_address(res, dev, resno, size);
  251. if (!ret) {
  252. res->flags &= ~IORESOURCE_STARTALIGN;
  253. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  254. if (resno < PCI_BRIDGE_RESOURCES)
  255. pci_update_resource(dev, resno);
  256. }
  257. return ret;
  258. }
  259. /* Sort resources by alignment */
  260. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  261. {
  262. int i;
  263. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  264. struct resource *r;
  265. struct resource_list *list, *tmp;
  266. resource_size_t r_align;
  267. r = &dev->resource[i];
  268. if (r->flags & IORESOURCE_PCI_FIXED)
  269. continue;
  270. if (!(r->flags) || r->parent)
  271. continue;
  272. r_align = pci_resource_alignment(dev, r);
  273. if (!r_align) {
  274. dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
  275. i, r);
  276. continue;
  277. }
  278. for (list = head; ; list = list->next) {
  279. resource_size_t align = 0;
  280. struct resource_list *ln = list->next;
  281. if (ln)
  282. align = pci_resource_alignment(ln->dev, ln->res);
  283. if (r_align > align) {
  284. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  285. if (!tmp)
  286. panic("pdev_sort_resources(): "
  287. "kmalloc() failed!\n");
  288. tmp->next = ln;
  289. tmp->res = r;
  290. tmp->dev = dev;
  291. list->next = tmp;
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. int pci_enable_resources(struct pci_dev *dev, int mask)
  298. {
  299. u16 cmd, old_cmd;
  300. int i;
  301. struct resource *r;
  302. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  303. old_cmd = cmd;
  304. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  305. if (!(mask & (1 << i)))
  306. continue;
  307. r = &dev->resource[i];
  308. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  309. continue;
  310. if ((i == PCI_ROM_RESOURCE) &&
  311. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  312. continue;
  313. if (!r->parent) {
  314. dev_err(&dev->dev, "device not available "
  315. "(can't reserve %pR)\n", r);
  316. return -EINVAL;
  317. }
  318. if (r->flags & IORESOURCE_IO)
  319. cmd |= PCI_COMMAND_IO;
  320. if (r->flags & IORESOURCE_MEM)
  321. cmd |= PCI_COMMAND_MEMORY;
  322. }
  323. if (cmd != old_cmd) {
  324. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  325. old_cmd, cmd);
  326. pci_write_config_word(dev, PCI_COMMAND, cmd);
  327. }
  328. return 0;
  329. }