bus.c 5.8 KB

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