bus.c 6.2 KB

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