setup-res.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/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. void pci_update_resource(struct pci_dev *dev, int resno)
  25. {
  26. struct pci_bus_region region;
  27. u32 new, check, mask;
  28. int reg;
  29. enum pci_bar_type type;
  30. struct resource *res = dev->resource + resno;
  31. /*
  32. * Ignore resources for unimplemented BARs and unused resource slots
  33. * for 64 bit BARs.
  34. */
  35. if (!res->flags)
  36. return;
  37. /*
  38. * Ignore non-moveable resources. This might be legacy resources for
  39. * which no functional BAR register exists or another important
  40. * system resource we shouldn't move around.
  41. */
  42. if (res->flags & IORESOURCE_PCI_FIXED)
  43. return;
  44. pcibios_resource_to_bus(dev, &region, res);
  45. dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] "
  46. "flags %#lx\n", resno, res,
  47. (unsigned long long)region.start,
  48. (unsigned long long)region.end,
  49. (unsigned long)res->flags);
  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. pci_write_config_dword(dev, reg, new);
  64. pci_read_config_dword(dev, reg, &check);
  65. if ((new ^ check) & mask) {
  66. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  67. resno, new, check);
  68. }
  69. if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  70. (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  71. new = region.start >> 16 >> 16;
  72. pci_write_config_dword(dev, reg + 4, new);
  73. pci_read_config_dword(dev, reg + 4, &check);
  74. if (check != new) {
  75. dev_err(&dev->dev, "BAR %d: error updating "
  76. "(high %#08x != %#08x)\n", resno, new, check);
  77. }
  78. }
  79. res->flags &= ~IORESOURCE_UNSET;
  80. dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
  81. resno, (unsigned long long)region.start,
  82. (unsigned long long)region.end, res->flags);
  83. }
  84. int pci_claim_resource(struct pci_dev *dev, int resource)
  85. {
  86. struct resource *res = &dev->resource[resource];
  87. struct resource *root = NULL;
  88. char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  89. int err;
  90. root = pcibios_select_root(dev, res);
  91. err = -EINVAL;
  92. if (root != NULL)
  93. err = insert_resource(root, res);
  94. if (err) {
  95. dev_err(&dev->dev, "BAR %d: %s of %s %pR\n",
  96. resource,
  97. root ? "address space collision on" :
  98. "no parent found for",
  99. dtype, res);
  100. }
  101. return err;
  102. }
  103. int pci_assign_resource(struct pci_dev *dev, int resno)
  104. {
  105. struct pci_bus *bus = dev->bus;
  106. struct resource *res = dev->resource + resno;
  107. resource_size_t size, min, align;
  108. int ret;
  109. size = resource_size(res);
  110. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  111. align = resource_alignment(res);
  112. if (!align) {
  113. dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus "
  114. "alignment) %pR flags %#lx\n",
  115. resno, res, res->flags);
  116. return -EINVAL;
  117. }
  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. if (ret) {
  133. dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
  134. resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
  135. } else {
  136. res->flags &= ~IORESOURCE_STARTALIGN;
  137. if (resno < PCI_BRIDGE_RESOURCES)
  138. pci_update_resource(dev, resno);
  139. }
  140. return ret;
  141. }
  142. #if 0
  143. int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
  144. {
  145. struct pci_bus *bus = dev->bus;
  146. struct resource *res = dev->resource + resno;
  147. unsigned int type_mask;
  148. int i, ret = -EBUSY;
  149. type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  150. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  151. struct resource *r = bus->resource[i];
  152. if (!r)
  153. continue;
  154. /* type_mask must match */
  155. if ((res->flags ^ r->flags) & type_mask)
  156. continue;
  157. ret = request_resource(r, res);
  158. if (ret == 0)
  159. break;
  160. }
  161. if (ret) {
  162. dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n",
  163. resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res);
  164. } else if (resno < PCI_BRIDGE_RESOURCES) {
  165. pci_update_resource(dev, resno);
  166. }
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
  170. #endif
  171. /* Sort resources by alignment */
  172. void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
  173. {
  174. int i;
  175. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  176. struct resource *r;
  177. struct resource_list *list, *tmp;
  178. resource_size_t r_align;
  179. r = &dev->resource[i];
  180. if (r->flags & IORESOURCE_PCI_FIXED)
  181. continue;
  182. if (!(r->flags) || r->parent)
  183. continue;
  184. r_align = resource_alignment(r);
  185. if (!r_align) {
  186. dev_warn(&dev->dev, "BAR %d: bogus alignment "
  187. "%pR flags %#lx\n",
  188. i, r, r->flags);
  189. continue;
  190. }
  191. for (list = head; ; list = list->next) {
  192. resource_size_t align = 0;
  193. struct resource_list *ln = list->next;
  194. if (ln)
  195. align = resource_alignment(ln->res);
  196. if (r_align > align) {
  197. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  198. if (!tmp)
  199. panic("pdev_sort_resources(): "
  200. "kmalloc() failed!\n");
  201. tmp->next = ln;
  202. tmp->res = r;
  203. tmp->dev = dev;
  204. list->next = tmp;
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. int pci_enable_resources(struct pci_dev *dev, int mask)
  211. {
  212. u16 cmd, old_cmd;
  213. int i;
  214. struct resource *r;
  215. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  216. old_cmd = cmd;
  217. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  218. if (!(mask & (1 << i)))
  219. continue;
  220. r = &dev->resource[i];
  221. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  222. continue;
  223. if ((i == PCI_ROM_RESOURCE) &&
  224. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  225. continue;
  226. if (!r->parent) {
  227. dev_err(&dev->dev, "device not available because of "
  228. "BAR %d %pR collisions\n", i, r);
  229. return -EINVAL;
  230. }
  231. if (r->flags & IORESOURCE_IO)
  232. cmd |= PCI_COMMAND_IO;
  233. if (r->flags & IORESOURCE_MEM)
  234. cmd |= PCI_COMMAND_MEMORY;
  235. }
  236. if (cmd != old_cmd) {
  237. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  238. old_cmd, cmd);
  239. pci_write_config_word(dev, PCI_COMMAND, cmd);
  240. }
  241. return 0;
  242. }