bus.c 5.2 KB

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