bus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * drivers/pci/bus.c
  3. *
  4. * From setup-res.c, by:
  5. * Dave Rusling (david.rusling@reo.mts.dec.com)
  6. * David Mosberger (davidm@cs.arizona.edu)
  7. * David Miller (davem@redhat.com)
  8. * Ivan Kokshaysky (ink@jurassic.park.msu.ru)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/errno.h>
  14. #include <linux/ioport.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/slab.h>
  17. #include "pci.h"
  18. void pci_add_resource_offset(struct list_head *resources, struct resource *res,
  19. resource_size_t offset)
  20. {
  21. struct resource_entry *entry;
  22. entry = resource_list_create_entry(res, 0);
  23. if (!entry) {
  24. printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
  25. return;
  26. }
  27. entry->offset = offset;
  28. resource_list_add_tail(entry, resources);
  29. }
  30. EXPORT_SYMBOL(pci_add_resource_offset);
  31. void pci_add_resource(struct list_head *resources, struct resource *res)
  32. {
  33. pci_add_resource_offset(resources, res, 0);
  34. }
  35. EXPORT_SYMBOL(pci_add_resource);
  36. void pci_free_resource_list(struct list_head *resources)
  37. {
  38. resource_list_free(resources);
  39. }
  40. EXPORT_SYMBOL(pci_free_resource_list);
  41. void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
  42. unsigned int flags)
  43. {
  44. struct pci_bus_resource *bus_res;
  45. bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
  46. if (!bus_res) {
  47. dev_err(&bus->dev, "can't add %pR resource\n", res);
  48. return;
  49. }
  50. bus_res->res = res;
  51. bus_res->flags = flags;
  52. list_add_tail(&bus_res->list, &bus->resources);
  53. }
  54. struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
  55. {
  56. struct pci_bus_resource *bus_res;
  57. if (n < PCI_BRIDGE_RESOURCE_NUM)
  58. return bus->resource[n];
  59. n -= PCI_BRIDGE_RESOURCE_NUM;
  60. list_for_each_entry(bus_res, &bus->resources, list) {
  61. if (n-- == 0)
  62. return bus_res->res;
  63. }
  64. return NULL;
  65. }
  66. EXPORT_SYMBOL_GPL(pci_bus_resource_n);
  67. void pci_bus_remove_resources(struct pci_bus *bus)
  68. {
  69. int i;
  70. struct pci_bus_resource *bus_res, *tmp;
  71. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  72. bus->resource[i] = NULL;
  73. list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
  74. list_del(&bus_res->list);
  75. kfree(bus_res);
  76. }
  77. }
  78. static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
  79. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  80. static struct pci_bus_region pci_64_bit = {0,
  81. (pci_bus_addr_t) 0xffffffffffffffffULL};
  82. static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
  83. (pci_bus_addr_t) 0xffffffffffffffffULL};
  84. #endif
  85. /*
  86. * @res contains CPU addresses. Clip it so the corresponding bus addresses
  87. * on @bus are entirely within @region. This is used to control the bus
  88. * addresses of resources we allocate, e.g., we may need a resource that
  89. * can be mapped by a 32-bit BAR.
  90. */
  91. static void pci_clip_resource_to_region(struct pci_bus *bus,
  92. struct resource *res,
  93. struct pci_bus_region *region)
  94. {
  95. struct pci_bus_region r;
  96. pcibios_resource_to_bus(bus, &r, res);
  97. if (r.start < region->start)
  98. r.start = region->start;
  99. if (r.end > region->end)
  100. r.end = region->end;
  101. if (r.end < r.start)
  102. res->end = res->start - 1;
  103. else
  104. pcibios_bus_to_resource(bus, res, &r);
  105. }
  106. static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
  107. resource_size_t size, resource_size_t align,
  108. resource_size_t min, unsigned long type_mask,
  109. resource_size_t (*alignf)(void *,
  110. const struct resource *,
  111. resource_size_t,
  112. resource_size_t),
  113. void *alignf_data,
  114. struct pci_bus_region *region)
  115. {
  116. int i, ret;
  117. struct resource *r, avail;
  118. resource_size_t max;
  119. type_mask |= IORESOURCE_TYPE_BITS;
  120. pci_bus_for_each_resource(bus, r, i) {
  121. resource_size_t min_used = min;
  122. if (!r)
  123. continue;
  124. /* type_mask must match */
  125. if ((res->flags ^ r->flags) & type_mask)
  126. continue;
  127. /* We cannot allocate a non-prefetching resource
  128. from a pre-fetching area */
  129. if ((r->flags & IORESOURCE_PREFETCH) &&
  130. !(res->flags & IORESOURCE_PREFETCH))
  131. continue;
  132. avail = *r;
  133. pci_clip_resource_to_region(bus, &avail, region);
  134. /*
  135. * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
  136. * protect badly documented motherboard resources, but if
  137. * this is an already-configured bridge window, its start
  138. * overrides "min".
  139. */
  140. if (avail.start)
  141. min_used = avail.start;
  142. max = avail.end;
  143. /* Ok, try it out.. */
  144. ret = allocate_resource(r, res, size, min_used, max,
  145. align, alignf, alignf_data);
  146. if (ret == 0)
  147. return 0;
  148. }
  149. return -ENOMEM;
  150. }
  151. /**
  152. * pci_bus_alloc_resource - allocate a resource from a parent bus
  153. * @bus: PCI bus
  154. * @res: resource to allocate
  155. * @size: size of resource to allocate
  156. * @align: alignment of resource to allocate
  157. * @min: minimum /proc/iomem address to allocate
  158. * @type_mask: IORESOURCE_* type flags
  159. * @alignf: resource alignment function
  160. * @alignf_data: data argument for resource alignment function
  161. *
  162. * Given the PCI bus a device resides on, the size, minimum address,
  163. * alignment and type, try to find an acceptable resource allocation
  164. * for a specific device resource.
  165. */
  166. int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  167. resource_size_t size, resource_size_t align,
  168. resource_size_t min, unsigned long type_mask,
  169. resource_size_t (*alignf)(void *,
  170. const struct resource *,
  171. resource_size_t,
  172. resource_size_t),
  173. void *alignf_data)
  174. {
  175. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  176. int rc;
  177. if (res->flags & IORESOURCE_MEM_64) {
  178. rc = pci_bus_alloc_from_region(bus, res, size, align, min,
  179. type_mask, alignf, alignf_data,
  180. &pci_high);
  181. if (rc == 0)
  182. return 0;
  183. return pci_bus_alloc_from_region(bus, res, size, align, min,
  184. type_mask, alignf, alignf_data,
  185. &pci_64_bit);
  186. }
  187. #endif
  188. return pci_bus_alloc_from_region(bus, res, size, align, min,
  189. type_mask, alignf, alignf_data,
  190. &pci_32_bit);
  191. }
  192. EXPORT_SYMBOL(pci_bus_alloc_resource);
  193. /*
  194. * The @idx resource of @dev should be a PCI-PCI bridge window. If this
  195. * resource fits inside a window of an upstream bridge, do nothing. If it
  196. * overlaps an upstream window but extends outside it, clip the resource so
  197. * it fits completely inside.
  198. */
  199. bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
  200. {
  201. struct pci_bus *bus = dev->bus;
  202. struct resource *res = &dev->resource[idx];
  203. struct resource orig_res = *res;
  204. struct resource *r;
  205. int i;
  206. pci_bus_for_each_resource(bus, r, i) {
  207. resource_size_t start, end;
  208. if (!r)
  209. continue;
  210. if (resource_type(res) != resource_type(r))
  211. continue;
  212. start = max(r->start, res->start);
  213. end = min(r->end, res->end);
  214. if (start > end)
  215. continue; /* no overlap */
  216. if (res->start == start && res->end == end)
  217. return false; /* no change */
  218. res->start = start;
  219. res->end = end;
  220. res->flags &= ~IORESOURCE_UNSET;
  221. orig_res.flags &= ~IORESOURCE_UNSET;
  222. dev_printk(KERN_DEBUG, &dev->dev, "%pR clipped to %pR\n",
  223. &orig_res, res);
  224. return true;
  225. }
  226. return false;
  227. }
  228. void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
  229. /**
  230. * pci_bus_add_device - start driver for a single device
  231. * @dev: device to add
  232. *
  233. * This adds add sysfs entries and start device drivers
  234. */
  235. void pci_bus_add_device(struct pci_dev *dev)
  236. {
  237. int retval;
  238. /*
  239. * Can not put in pci_device_add yet because resources
  240. * are not assigned yet for some devices.
  241. */
  242. pci_fixup_device(pci_fixup_final, dev);
  243. pci_create_sysfs_dev_files(dev);
  244. pci_proc_attach_device(dev);
  245. dev->match_driver = true;
  246. retval = device_attach(&dev->dev);
  247. WARN_ON(retval < 0);
  248. dev->is_added = 1;
  249. }
  250. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  251. /**
  252. * pci_bus_add_devices - start driver for PCI devices
  253. * @bus: bus to check for new devices
  254. *
  255. * Start driver for PCI devices and add some sysfs entries.
  256. */
  257. void pci_bus_add_devices(const struct pci_bus *bus)
  258. {
  259. struct pci_dev *dev;
  260. struct pci_bus *child;
  261. list_for_each_entry(dev, &bus->devices, bus_list) {
  262. /* Skip already-added devices */
  263. if (dev->is_added)
  264. continue;
  265. pci_bus_add_device(dev);
  266. }
  267. list_for_each_entry(dev, &bus->devices, bus_list) {
  268. BUG_ON(!dev->is_added);
  269. child = dev->subordinate;
  270. if (child)
  271. pci_bus_add_devices(child);
  272. }
  273. }
  274. EXPORT_SYMBOL(pci_bus_add_devices);
  275. /** pci_walk_bus - walk devices on/under bus, calling callback.
  276. * @top bus whose devices should be walked
  277. * @cb callback to be called for each device found
  278. * @userdata arbitrary pointer to be passed to callback.
  279. *
  280. * Walk the given bus, including any bridged devices
  281. * on buses under this bus. Call the provided callback
  282. * on each device found.
  283. *
  284. * We check the return of @cb each time. If it returns anything
  285. * other than 0, we break out.
  286. *
  287. */
  288. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  289. void *userdata)
  290. {
  291. struct pci_dev *dev;
  292. struct pci_bus *bus;
  293. struct list_head *next;
  294. int retval;
  295. bus = top;
  296. down_read(&pci_bus_sem);
  297. next = top->devices.next;
  298. for (;;) {
  299. if (next == &bus->devices) {
  300. /* end of this bus, go up or finish */
  301. if (bus == top)
  302. break;
  303. next = bus->self->bus_list.next;
  304. bus = bus->self->bus;
  305. continue;
  306. }
  307. dev = list_entry(next, struct pci_dev, bus_list);
  308. if (dev->subordinate) {
  309. /* this is a pci-pci bridge, do its devices next */
  310. next = dev->subordinate->devices.next;
  311. bus = dev->subordinate;
  312. } else
  313. next = dev->bus_list.next;
  314. retval = cb(dev, userdata);
  315. if (retval)
  316. break;
  317. }
  318. up_read(&pci_bus_sem);
  319. }
  320. EXPORT_SYMBOL_GPL(pci_walk_bus);
  321. struct pci_bus *pci_bus_get(struct pci_bus *bus)
  322. {
  323. if (bus)
  324. get_device(&bus->dev);
  325. return bus;
  326. }
  327. EXPORT_SYMBOL(pci_bus_get);
  328. void pci_bus_put(struct pci_bus *bus)
  329. {
  330. if (bus)
  331. put_device(&bus->dev);
  332. }
  333. EXPORT_SYMBOL(pci_bus_put);