bus.c 8.2 KB

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