bus.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * 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 __devinit 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. down_write(&pci_bus_sem);
  79. list_add_tail(&dev->global_list, &pci_devices);
  80. up_write(&pci_bus_sem);
  81. pci_proc_attach_device(dev);
  82. pci_create_sysfs_dev_files(dev);
  83. return 0;
  84. }
  85. /**
  86. * pci_bus_add_devices - insert newly discovered PCI devices
  87. * @bus: bus to check for new devices
  88. *
  89. * Add newly discovered PCI devices (which are on the bus->devices
  90. * list) to the global PCI device list, add the sysfs and procfs
  91. * entries. Where a bridge is found, add the discovered bus to
  92. * the parents list of child buses, and recurse (breadth-first
  93. * to be compatible with 2.4)
  94. *
  95. * Call hotplug for each new devices.
  96. */
  97. void __devinit pci_bus_add_devices(struct pci_bus *bus)
  98. {
  99. struct pci_dev *dev;
  100. int retval;
  101. list_for_each_entry(dev, &bus->devices, bus_list) {
  102. /*
  103. * Skip already-present devices (which are on the
  104. * global device list.)
  105. */
  106. if (!list_empty(&dev->global_list))
  107. continue;
  108. retval = pci_bus_add_device(dev);
  109. if (retval)
  110. dev_err(&dev->dev, "Error adding device, continuing\n");
  111. }
  112. list_for_each_entry(dev, &bus->devices, bus_list) {
  113. BUG_ON(list_empty(&dev->global_list));
  114. /*
  115. * If there is an unattached subordinate bus, attach
  116. * it and then scan for unattached PCI devices.
  117. */
  118. if (dev->subordinate) {
  119. if (list_empty(&dev->subordinate->node)) {
  120. down_write(&pci_bus_sem);
  121. list_add_tail(&dev->subordinate->node,
  122. &dev->bus->children);
  123. up_write(&pci_bus_sem);
  124. }
  125. pci_bus_add_devices(dev->subordinate);
  126. retval = sysfs_create_link(&dev->subordinate->class_dev.kobj,
  127. &dev->dev.kobj, "bridge");
  128. if (retval)
  129. dev_err(&dev->dev, "Error creating sysfs "
  130. "bridge symlink, continuing...\n");
  131. }
  132. }
  133. }
  134. void pci_enable_bridges(struct pci_bus *bus)
  135. {
  136. struct pci_dev *dev;
  137. int retval;
  138. list_for_each_entry(dev, &bus->devices, bus_list) {
  139. if (dev->subordinate) {
  140. retval = pci_enable_device(dev);
  141. pci_set_master(dev);
  142. pci_enable_bridges(dev->subordinate);
  143. }
  144. }
  145. }
  146. /** pci_walk_bus - walk devices on/under bus, calling callback.
  147. * @top bus whose devices should be walked
  148. * @cb callback to be called for each device found
  149. * @userdata arbitrary pointer to be passed to callback.
  150. *
  151. * Walk the given bus, including any bridged devices
  152. * on buses under this bus. Call the provided callback
  153. * on each device found.
  154. */
  155. void pci_walk_bus(struct pci_bus *top, void (*cb)(struct pci_dev *, void *),
  156. void *userdata)
  157. {
  158. struct pci_dev *dev;
  159. struct pci_bus *bus;
  160. struct list_head *next;
  161. bus = top;
  162. down_read(&pci_bus_sem);
  163. next = top->devices.next;
  164. for (;;) {
  165. if (next == &bus->devices) {
  166. /* end of this bus, go up or finish */
  167. if (bus == top)
  168. break;
  169. next = bus->self->bus_list.next;
  170. bus = bus->self->bus;
  171. continue;
  172. }
  173. dev = list_entry(next, struct pci_dev, bus_list);
  174. if (dev->subordinate) {
  175. /* this is a pci-pci bridge, do its devices next */
  176. next = dev->subordinate->devices.next;
  177. bus = dev->subordinate;
  178. } else
  179. next = dev->bus_list.next;
  180. /* Run device routines with the device locked */
  181. down(&dev->dev.sem);
  182. cb(dev, userdata);
  183. up(&dev->dev.sem);
  184. }
  185. up_read(&pci_bus_sem);
  186. }
  187. EXPORT_SYMBOL_GPL(pci_walk_bus);
  188. EXPORT_SYMBOL(pci_bus_alloc_resource);
  189. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  190. EXPORT_SYMBOL(pci_bus_add_devices);
  191. EXPORT_SYMBOL(pci_enable_bridges);