bus.c 5.8 KB

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