bus.c 8.3 KB

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