bus.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
  19. unsigned int flags)
  20. {
  21. struct pci_bus_resource *bus_res;
  22. bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
  23. if (!bus_res) {
  24. dev_err(&bus->dev, "can't add %pR resource\n", res);
  25. return;
  26. }
  27. bus_res->res = res;
  28. bus_res->flags = flags;
  29. list_add_tail(&bus_res->list, &bus->resources);
  30. }
  31. struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
  32. {
  33. struct pci_bus_resource *bus_res;
  34. if (n < PCI_BRIDGE_RESOURCE_NUM)
  35. return bus->resource[n];
  36. n -= PCI_BRIDGE_RESOURCE_NUM;
  37. list_for_each_entry(bus_res, &bus->resources, list) {
  38. if (n-- == 0)
  39. return bus_res->res;
  40. }
  41. return NULL;
  42. }
  43. EXPORT_SYMBOL_GPL(pci_bus_resource_n);
  44. void pci_bus_remove_resources(struct pci_bus *bus)
  45. {
  46. struct pci_bus_resource *bus_res, *tmp;
  47. int i;
  48. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  49. bus->resource[i] = 0;
  50. list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
  51. list_del(&bus_res->list);
  52. kfree(bus_res);
  53. }
  54. }
  55. /**
  56. * pci_bus_alloc_resource - allocate a resource from a parent bus
  57. * @bus: PCI bus
  58. * @res: resource to allocate
  59. * @size: size of resource to allocate
  60. * @align: alignment of resource to allocate
  61. * @min: minimum /proc/iomem address to allocate
  62. * @type_mask: IORESOURCE_* type flags
  63. * @alignf: resource alignment function
  64. * @alignf_data: data argument for resource alignment function
  65. *
  66. * Given the PCI bus a device resides on, the size, minimum address,
  67. * alignment and type, try to find an acceptable resource allocation
  68. * for a specific device resource.
  69. */
  70. int
  71. pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  72. resource_size_t size, resource_size_t align,
  73. resource_size_t min, unsigned int type_mask,
  74. resource_size_t (*alignf)(void *,
  75. const struct resource *,
  76. resource_size_t,
  77. resource_size_t),
  78. void *alignf_data)
  79. {
  80. int i, ret = -ENOMEM;
  81. struct resource *r;
  82. resource_size_t max = -1;
  83. type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
  84. /* don't allocate too high if the pref mem doesn't support 64bit*/
  85. if (!(res->flags & IORESOURCE_MEM_64))
  86. max = PCIBIOS_MAX_MEM_32;
  87. pci_bus_for_each_resource(bus, r, i) {
  88. if (!r)
  89. continue;
  90. /* type_mask must match */
  91. if ((res->flags ^ r->flags) & type_mask)
  92. continue;
  93. /* We cannot allocate a non-prefetching resource
  94. from a pre-fetching area */
  95. if ((r->flags & IORESOURCE_PREFETCH) &&
  96. !(res->flags & IORESOURCE_PREFETCH))
  97. continue;
  98. /* Ok, try it out.. */
  99. ret = allocate_resource(r, res, size,
  100. r->start ? : min,
  101. max, align,
  102. alignf, alignf_data);
  103. if (ret == 0)
  104. break;
  105. }
  106. return ret;
  107. }
  108. /**
  109. * pci_bus_add_device - add a single device
  110. * @dev: device to add
  111. *
  112. * This adds a single pci device to the global
  113. * device list and adds sysfs and procfs entries
  114. */
  115. int pci_bus_add_device(struct pci_dev *dev)
  116. {
  117. int retval;
  118. retval = device_add(&dev->dev);
  119. if (retval)
  120. return retval;
  121. dev->is_added = 1;
  122. pci_proc_attach_device(dev);
  123. pci_create_sysfs_dev_files(dev);
  124. return 0;
  125. }
  126. /**
  127. * pci_bus_add_child - add a child bus
  128. * @bus: bus to add
  129. *
  130. * This adds sysfs entries for a single bus
  131. */
  132. int pci_bus_add_child(struct pci_bus *bus)
  133. {
  134. int retval;
  135. if (bus->bridge)
  136. bus->dev.parent = bus->bridge;
  137. retval = device_register(&bus->dev);
  138. if (retval)
  139. return retval;
  140. bus->is_added = 1;
  141. retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity);
  142. if (retval)
  143. return retval;
  144. retval = device_create_file(&bus->dev, &dev_attr_cpulistaffinity);
  145. /* Create legacy_io and legacy_mem files for this bus */
  146. pci_create_legacy_files(bus);
  147. return retval;
  148. }
  149. /**
  150. * pci_bus_add_devices - insert newly discovered PCI devices
  151. * @bus: bus to check for new devices
  152. *
  153. * Add newly discovered PCI devices (which are on the bus->devices
  154. * list) to the global PCI device list, add the sysfs and procfs
  155. * entries. Where a bridge is found, add the discovered bus to
  156. * the parents list of child buses, and recurse (breadth-first
  157. * to be compatible with 2.4)
  158. *
  159. * Call hotplug for each new devices.
  160. */
  161. void pci_bus_add_devices(const struct pci_bus *bus)
  162. {
  163. struct pci_dev *dev;
  164. struct pci_bus *child;
  165. int retval;
  166. list_for_each_entry(dev, &bus->devices, bus_list) {
  167. /* Skip already-added devices */
  168. if (dev->is_added)
  169. continue;
  170. retval = pci_bus_add_device(dev);
  171. if (retval)
  172. dev_err(&dev->dev, "Error adding device, continuing\n");
  173. }
  174. list_for_each_entry(dev, &bus->devices, bus_list) {
  175. BUG_ON(!dev->is_added);
  176. child = dev->subordinate;
  177. /*
  178. * If there is an unattached subordinate bus, attach
  179. * it and then scan for unattached PCI devices.
  180. */
  181. if (!child)
  182. continue;
  183. if (list_empty(&child->node)) {
  184. down_write(&pci_bus_sem);
  185. list_add_tail(&child->node, &dev->bus->children);
  186. up_write(&pci_bus_sem);
  187. }
  188. pci_bus_add_devices(child);
  189. /*
  190. * register the bus with sysfs as the parent is now
  191. * properly registered.
  192. */
  193. if (child->is_added)
  194. continue;
  195. retval = pci_bus_add_child(child);
  196. if (retval)
  197. dev_err(&dev->dev, "Error adding bus, continuing\n");
  198. }
  199. }
  200. void pci_enable_bridges(struct pci_bus *bus)
  201. {
  202. struct pci_dev *dev;
  203. int retval;
  204. list_for_each_entry(dev, &bus->devices, bus_list) {
  205. if (dev->subordinate) {
  206. if (!pci_is_enabled(dev)) {
  207. retval = pci_enable_device(dev);
  208. pci_set_master(dev);
  209. }
  210. pci_enable_bridges(dev->subordinate);
  211. }
  212. }
  213. }
  214. /** pci_walk_bus - walk devices on/under bus, calling callback.
  215. * @top bus whose devices should be walked
  216. * @cb callback to be called for each device found
  217. * @userdata arbitrary pointer to be passed to callback.
  218. *
  219. * Walk the given bus, including any bridged devices
  220. * on buses under this bus. Call the provided callback
  221. * on each device found.
  222. *
  223. * We check the return of @cb each time. If it returns anything
  224. * other than 0, we break out.
  225. *
  226. */
  227. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  228. void *userdata)
  229. {
  230. struct pci_dev *dev;
  231. struct pci_bus *bus;
  232. struct list_head *next;
  233. int retval;
  234. bus = top;
  235. down_read(&pci_bus_sem);
  236. next = top->devices.next;
  237. for (;;) {
  238. if (next == &bus->devices) {
  239. /* end of this bus, go up or finish */
  240. if (bus == top)
  241. break;
  242. next = bus->self->bus_list.next;
  243. bus = bus->self->bus;
  244. continue;
  245. }
  246. dev = list_entry(next, struct pci_dev, bus_list);
  247. if (dev->subordinate) {
  248. /* this is a pci-pci bridge, do its devices next */
  249. next = dev->subordinate->devices.next;
  250. bus = dev->subordinate;
  251. } else
  252. next = dev->bus_list.next;
  253. /* Run device routines with the device locked */
  254. device_lock(&dev->dev);
  255. retval = cb(dev, userdata);
  256. device_unlock(&dev->dev);
  257. if (retval)
  258. break;
  259. }
  260. up_read(&pci_bus_sem);
  261. }
  262. EXPORT_SYMBOL(pci_bus_alloc_resource);
  263. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  264. EXPORT_SYMBOL(pci_bus_add_devices);
  265. EXPORT_SYMBOL(pci_enable_bridges);