bus.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, min, -1, align,
  57. alignf, alignf_data);
  58. if (ret == 0)
  59. break;
  60. }
  61. return ret;
  62. }
  63. /**
  64. * add a single device
  65. * @dev: device to add
  66. *
  67. * This adds a single pci device to the global
  68. * device list and adds sysfs and procfs entries
  69. */
  70. void __devinit pci_bus_add_device(struct pci_dev *dev)
  71. {
  72. device_add(&dev->dev);
  73. spin_lock(&pci_bus_lock);
  74. list_add_tail(&dev->global_list, &pci_devices);
  75. spin_unlock(&pci_bus_lock);
  76. pci_proc_attach_device(dev);
  77. pci_create_sysfs_dev_files(dev);
  78. }
  79. /**
  80. * pci_bus_add_devices - insert newly discovered PCI devices
  81. * @bus: bus to check for new devices
  82. *
  83. * Add newly discovered PCI devices (which are on the bus->devices
  84. * list) to the global PCI device list, add the sysfs and procfs
  85. * entries. Where a bridge is found, add the discovered bus to
  86. * the parents list of child buses, and recurse (breadth-first
  87. * to be compatible with 2.4)
  88. *
  89. * Call hotplug for each new devices.
  90. */
  91. void __devinit pci_bus_add_devices(struct pci_bus *bus)
  92. {
  93. struct pci_dev *dev;
  94. list_for_each_entry(dev, &bus->devices, bus_list) {
  95. /*
  96. * Skip already-present devices (which are on the
  97. * global device list.)
  98. */
  99. if (!list_empty(&dev->global_list))
  100. continue;
  101. pci_bus_add_device(dev);
  102. }
  103. list_for_each_entry(dev, &bus->devices, bus_list) {
  104. BUG_ON(list_empty(&dev->global_list));
  105. /*
  106. * If there is an unattached subordinate bus, attach
  107. * it and then scan for unattached PCI devices.
  108. */
  109. if (dev->subordinate) {
  110. if (list_empty(&dev->subordinate->node)) {
  111. spin_lock(&pci_bus_lock);
  112. list_add_tail(&dev->subordinate->node,
  113. &dev->bus->children);
  114. spin_unlock(&pci_bus_lock);
  115. }
  116. pci_bus_add_devices(dev->subordinate);
  117. sysfs_create_link(&dev->subordinate->class_dev.kobj, &dev->dev.kobj, "bridge");
  118. }
  119. }
  120. }
  121. void pci_enable_bridges(struct pci_bus *bus)
  122. {
  123. struct pci_dev *dev;
  124. list_for_each_entry(dev, &bus->devices, bus_list) {
  125. if (dev->subordinate) {
  126. pci_enable_device(dev);
  127. pci_set_master(dev);
  128. pci_enable_bridges(dev->subordinate);
  129. }
  130. }
  131. }
  132. EXPORT_SYMBOL(pci_bus_alloc_resource);
  133. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  134. EXPORT_SYMBOL(pci_bus_add_devices);
  135. EXPORT_SYMBOL(pci_enable_bridges);