setup-res.c 6.9 KB

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