bus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/init.h>
  17. #include <linux/slab.h>
  18. #include "pci.h"
  19. void pci_add_resource(struct list_head *resources, struct resource *res)
  20. {
  21. struct pci_bus_resource *bus_res;
  22. bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
  23. if (!bus_res) {
  24. printk(KERN_ERR "PCI: can't add bus resource %pR\n", res);
  25. return;
  26. }
  27. bus_res->res = res;
  28. list_add_tail(&bus_res->list, resources);
  29. }
  30. EXPORT_SYMBOL(pci_add_resource);
  31. void pci_free_resource_list(struct list_head *resources)
  32. {
  33. struct pci_bus_resource *bus_res, *tmp;
  34. list_for_each_entry_safe(bus_res, tmp, resources, list) {
  35. list_del(&bus_res->list);
  36. kfree(bus_res);
  37. }
  38. }
  39. EXPORT_SYMBOL(pci_free_resource_list);
  40. void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
  41. unsigned int flags)
  42. {
  43. struct pci_bus_resource *bus_res;
  44. bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
  45. if (!bus_res) {
  46. dev_err(&bus->dev, "can't add %pR resource\n", res);
  47. return;
  48. }
  49. bus_res->res = res;
  50. bus_res->flags = flags;
  51. list_add_tail(&bus_res->list, &bus->resources);
  52. }
  53. struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
  54. {
  55. struct pci_bus_resource *bus_res;
  56. if (n < PCI_BRIDGE_RESOURCE_NUM)
  57. return bus->resource[n];
  58. n -= PCI_BRIDGE_RESOURCE_NUM;
  59. list_for_each_entry(bus_res, &bus->resources, list) {
  60. if (n-- == 0)
  61. return bus_res->res;
  62. }
  63. return NULL;
  64. }
  65. EXPORT_SYMBOL_GPL(pci_bus_resource_n);
  66. void pci_bus_remove_resources(struct pci_bus *bus)
  67. {
  68. int i;
  69. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  70. bus->resource[i] = NULL;
  71. pci_free_resource_list(&bus->resources);
  72. }
  73. /**
  74. * pci_bus_alloc_resource - allocate a resource from a parent bus
  75. * @bus: PCI bus
  76. * @res: resource to allocate
  77. * @size: size of resource to allocate
  78. * @align: alignment of resource to allocate
  79. * @min: minimum /proc/iomem address to allocate
  80. * @type_mask: IORESOURCE_* type flags
  81. * @alignf: resource alignment function
  82. * @alignf_data: data argument for resource alignment function
  83. *
  84. * Given the PCI bus a device resides on, the size, minimum address,
  85. * alignment and type, try to find an acceptable resource allocation
  86. * for a specific device resource.
  87. */
  88. int
  89. pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  90. resource_size_t size, resource_size_t align,
  91. resource_size_t min, unsigned int type_mask,
  92. resource_size_t (*alignf)(void *,
  93. const struct resource *,
  94. resource_size_t,
  95. resource_size_t),
  96. void *alignf_data)
  97. {
  98. int i, ret = -ENOMEM;
  99. struct resource *r;
  100. resource_size_t max = -1;
  101. type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
  102. /* don't allocate too high if the pref mem doesn't support 64bit*/
  103. if (!(res->flags & IORESOURCE_MEM_64))
  104. max = PCIBIOS_MAX_MEM_32;
  105. pci_bus_for_each_resource(bus, r, i) {
  106. if (!r)
  107. continue;
  108. /* type_mask must match */
  109. if ((res->flags ^ r->flags) & type_mask)
  110. continue;
  111. /* We cannot allocate a non-prefetching resource
  112. from a pre-fetching area */
  113. if ((r->flags & IORESOURCE_PREFETCH) &&
  114. !(res->flags & IORESOURCE_PREFETCH))
  115. continue;
  116. /* Ok, try it out.. */
  117. ret = allocate_resource(r, res, size,
  118. r->start ? : min,
  119. max, align,
  120. alignf, alignf_data);
  121. if (ret == 0)
  122. break;
  123. }
  124. return ret;
  125. }
  126. /**
  127. * pci_bus_add_device - add a single device
  128. * @dev: device to add
  129. *
  130. * This adds a single pci device to the global
  131. * device list and adds sysfs and procfs entries
  132. */
  133. int pci_bus_add_device(struct pci_dev *dev)
  134. {
  135. int retval;
  136. retval = device_add(&dev->dev);
  137. if (retval)
  138. return retval;
  139. dev->is_added = 1;
  140. pci_proc_attach_device(dev);
  141. pci_create_sysfs_dev_files(dev);
  142. return 0;
  143. }
  144. /**
  145. * pci_bus_add_child - add a child bus
  146. * @bus: bus to add
  147. *
  148. * This adds sysfs entries for a single bus
  149. */
  150. int pci_bus_add_child(struct pci_bus *bus)
  151. {
  152. int retval;
  153. if (bus->bridge)
  154. bus->dev.parent = bus->bridge;
  155. retval = device_register(&bus->dev);
  156. if (retval)
  157. return retval;
  158. bus->is_added = 1;
  159. /* Create legacy_io and legacy_mem files for this bus */
  160. pci_create_legacy_files(bus);
  161. return retval;
  162. }
  163. /**
  164. * pci_bus_add_devices - insert newly discovered PCI devices
  165. * @bus: bus to check for new devices
  166. *
  167. * Add newly discovered PCI devices (which are on the bus->devices
  168. * list) to the global PCI device list, add the sysfs and procfs
  169. * entries. Where a bridge is found, add the discovered bus to
  170. * the parents list of child buses, and recurse (breadth-first
  171. * to be compatible with 2.4)
  172. *
  173. * Call hotplug for each new devices.
  174. */
  175. void pci_bus_add_devices(const struct pci_bus *bus)
  176. {
  177. struct pci_dev *dev;
  178. struct pci_bus *child;
  179. int retval;
  180. list_for_each_entry(dev, &bus->devices, bus_list) {
  181. /* Skip already-added devices */
  182. if (dev->is_added)
  183. continue;
  184. retval = pci_bus_add_device(dev);
  185. if (retval)
  186. dev_err(&dev->dev, "Error adding device, continuing\n");
  187. }
  188. list_for_each_entry(dev, &bus->devices, bus_list) {
  189. BUG_ON(!dev->is_added);
  190. child = dev->subordinate;
  191. /*
  192. * If there is an unattached subordinate bus, attach
  193. * it and then scan for unattached PCI devices.
  194. */
  195. if (!child)
  196. continue;
  197. if (list_empty(&child->node)) {
  198. down_write(&pci_bus_sem);
  199. list_add_tail(&child->node, &dev->bus->children);
  200. up_write(&pci_bus_sem);
  201. }
  202. pci_bus_add_devices(child);
  203. /*
  204. * register the bus with sysfs as the parent is now
  205. * properly registered.
  206. */
  207. if (child->is_added)
  208. continue;
  209. retval = pci_bus_add_child(child);
  210. if (retval)
  211. dev_err(&dev->dev, "Error adding bus, continuing\n");
  212. }
  213. }
  214. void pci_enable_bridges(struct pci_bus *bus)
  215. {
  216. struct pci_dev *dev;
  217. int retval;
  218. list_for_each_entry(dev, &bus->devices, bus_list) {
  219. if (dev->subordinate) {
  220. if (!pci_is_enabled(dev)) {
  221. retval = pci_enable_device(dev);
  222. if (retval)
  223. dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n", retval);
  224. pci_set_master(dev);
  225. }
  226. pci_enable_bridges(dev->subordinate);
  227. }
  228. }
  229. }
  230. /** pci_walk_bus - walk devices on/under bus, calling callback.
  231. * @top bus whose devices should be walked
  232. * @cb callback to be called for each device found
  233. * @userdata arbitrary pointer to be passed to callback.
  234. *
  235. * Walk the given bus, including any bridged devices
  236. * on buses under this bus. Call the provided callback
  237. * on each device found.
  238. *
  239. * We check the return of @cb each time. If it returns anything
  240. * other than 0, we break out.
  241. *
  242. */
  243. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  244. void *userdata)
  245. {
  246. struct pci_dev *dev;
  247. struct pci_bus *bus;
  248. struct list_head *next;
  249. int retval;
  250. bus = top;
  251. down_read(&pci_bus_sem);
  252. next = top->devices.next;
  253. for (;;) {
  254. if (next == &bus->devices) {
  255. /* end of this bus, go up or finish */
  256. if (bus == top)
  257. break;
  258. next = bus->self->bus_list.next;
  259. bus = bus->self->bus;
  260. continue;
  261. }
  262. dev = list_entry(next, struct pci_dev, bus_list);
  263. if (dev->subordinate) {
  264. /* this is a pci-pci bridge, do its devices next */
  265. next = dev->subordinate->devices.next;
  266. bus = dev->subordinate;
  267. } else
  268. next = dev->bus_list.next;
  269. /* Run device routines with the device locked */
  270. device_lock(&dev->dev);
  271. retval = cb(dev, userdata);
  272. device_unlock(&dev->dev);
  273. if (retval)
  274. break;
  275. }
  276. up_read(&pci_bus_sem);
  277. }
  278. EXPORT_SYMBOL_GPL(pci_walk_bus);
  279. EXPORT_SYMBOL(pci_bus_alloc_resource);
  280. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  281. EXPORT_SYMBOL(pci_bus_add_devices);
  282. EXPORT_SYMBOL(pci_enable_bridges);