bus.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. static bool pci_bus_resource_better(struct resource *res1, bool pos1,
  57. struct resource *res2, bool pos2)
  58. {
  59. /* If exactly one is positive decode, always prefer that one */
  60. if (pos1 != pos2)
  61. return pos1 ? true : false;
  62. /* Prefer the one that contains the highest address */
  63. if (res1->end != res2->end)
  64. return (res1->end > res2->end) ? true : false;
  65. /* Otherwise, prefer the one with highest "center of gravity" */
  66. if (res1->start != res2->start)
  67. return (res1->start > res2->start) ? true : false;
  68. /* Otherwise, choose one arbitrarily (but consistently) */
  69. return (res1 > res2) ? true : false;
  70. }
  71. static bool pci_bus_resource_positive(struct pci_bus *bus, struct resource *res)
  72. {
  73. struct pci_bus_resource *bus_res;
  74. /*
  75. * This relies on the fact that pci_bus.resource[] refers to P2P or
  76. * CardBus bridge base/limit registers, which are always positively
  77. * decoded. The pci_bus.resources list contains host bridge or
  78. * subtractively decoded resources.
  79. */
  80. list_for_each_entry(bus_res, &bus->resources, list) {
  81. if (bus_res->res == res)
  82. return (bus_res->flags & PCI_SUBTRACTIVE_DECODE) ?
  83. false : true;
  84. }
  85. return true;
  86. }
  87. /*
  88. * Find the next-best bus resource after the cursor "res". If the cursor is
  89. * NULL, return the best resource. "Best" means that we prefer positive
  90. * decode regions over subtractive decode, then those at higher addresses.
  91. */
  92. static struct resource *pci_bus_find_resource_prev(struct pci_bus *bus,
  93. unsigned int type,
  94. struct resource *res)
  95. {
  96. bool res_pos, r_pos, prev_pos = false;
  97. struct resource *r, *prev = NULL;
  98. int i;
  99. res_pos = pci_bus_resource_positive(bus, res);
  100. pci_bus_for_each_resource(bus, r, i) {
  101. if (!r)
  102. continue;
  103. if ((r->flags & IORESOURCE_TYPE_BITS) != type)
  104. continue;
  105. r_pos = pci_bus_resource_positive(bus, r);
  106. if (!res || pci_bus_resource_better(res, res_pos, r, r_pos)) {
  107. if (!prev || pci_bus_resource_better(r, r_pos,
  108. prev, prev_pos)) {
  109. prev = r;
  110. prev_pos = r_pos;
  111. }
  112. }
  113. }
  114. return prev;
  115. }
  116. /**
  117. * pci_bus_alloc_resource - allocate a resource from a parent bus
  118. * @bus: PCI bus
  119. * @res: resource to allocate
  120. * @size: size of resource to allocate
  121. * @align: alignment of resource to allocate
  122. * @min: minimum /proc/iomem address to allocate
  123. * @type_mask: IORESOURCE_* type flags
  124. * @alignf: resource alignment function
  125. * @alignf_data: data argument for resource alignment function
  126. *
  127. * Given the PCI bus a device resides on, the size, minimum address,
  128. * alignment and type, try to find an acceptable resource allocation
  129. * for a specific device resource.
  130. */
  131. int
  132. pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  133. resource_size_t size, resource_size_t align,
  134. resource_size_t min, unsigned int type_mask,
  135. resource_size_t (*alignf)(void *,
  136. const struct resource *,
  137. resource_size_t,
  138. resource_size_t),
  139. void *alignf_data)
  140. {
  141. int ret = -ENOMEM;
  142. struct resource *r;
  143. resource_size_t max = -1;
  144. unsigned int type = res->flags & IORESOURCE_TYPE_BITS;
  145. type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
  146. /* don't allocate too high if the pref mem doesn't support 64bit*/
  147. if (!(res->flags & IORESOURCE_MEM_64))
  148. max = PCIBIOS_MAX_MEM_32;
  149. /* Look for space at highest addresses first */
  150. r = pci_bus_find_resource_prev(bus, type, NULL);
  151. for ( ; r; r = pci_bus_find_resource_prev(bus, type, r)) {
  152. /* type_mask must match */
  153. if ((res->flags ^ r->flags) & type_mask)
  154. continue;
  155. /* We cannot allocate a non-prefetching resource
  156. from a pre-fetching area */
  157. if ((r->flags & IORESOURCE_PREFETCH) &&
  158. !(res->flags & IORESOURCE_PREFETCH))
  159. continue;
  160. /* Ok, try it out.. */
  161. ret = allocate_resource(r, res, size,
  162. r->start ? : min,
  163. max, align,
  164. alignf, alignf_data);
  165. if (ret == 0)
  166. break;
  167. }
  168. return ret;
  169. }
  170. /**
  171. * pci_bus_add_device - add a single device
  172. * @dev: device to add
  173. *
  174. * This adds a single pci device to the global
  175. * device list and adds sysfs and procfs entries
  176. */
  177. int pci_bus_add_device(struct pci_dev *dev)
  178. {
  179. int retval;
  180. retval = device_add(&dev->dev);
  181. if (retval)
  182. return retval;
  183. dev->is_added = 1;
  184. pci_proc_attach_device(dev);
  185. pci_create_sysfs_dev_files(dev);
  186. return 0;
  187. }
  188. /**
  189. * pci_bus_add_child - add a child bus
  190. * @bus: bus to add
  191. *
  192. * This adds sysfs entries for a single bus
  193. */
  194. int pci_bus_add_child(struct pci_bus *bus)
  195. {
  196. int retval;
  197. if (bus->bridge)
  198. bus->dev.parent = bus->bridge;
  199. retval = device_register(&bus->dev);
  200. if (retval)
  201. return retval;
  202. bus->is_added = 1;
  203. retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity);
  204. if (retval)
  205. return retval;
  206. retval = device_create_file(&bus->dev, &dev_attr_cpulistaffinity);
  207. /* Create legacy_io and legacy_mem files for this bus */
  208. pci_create_legacy_files(bus);
  209. return retval;
  210. }
  211. /**
  212. * pci_bus_add_devices - insert newly discovered PCI devices
  213. * @bus: bus to check for new devices
  214. *
  215. * Add newly discovered PCI devices (which are on the bus->devices
  216. * list) to the global PCI device list, add the sysfs and procfs
  217. * entries. Where a bridge is found, add the discovered bus to
  218. * the parents list of child buses, and recurse (breadth-first
  219. * to be compatible with 2.4)
  220. *
  221. * Call hotplug for each new devices.
  222. */
  223. void pci_bus_add_devices(const struct pci_bus *bus)
  224. {
  225. struct pci_dev *dev;
  226. struct pci_bus *child;
  227. int retval;
  228. list_for_each_entry(dev, &bus->devices, bus_list) {
  229. /* Skip already-added devices */
  230. if (dev->is_added)
  231. continue;
  232. retval = pci_bus_add_device(dev);
  233. if (retval)
  234. dev_err(&dev->dev, "Error adding device, continuing\n");
  235. }
  236. list_for_each_entry(dev, &bus->devices, bus_list) {
  237. BUG_ON(!dev->is_added);
  238. child = dev->subordinate;
  239. /*
  240. * If there is an unattached subordinate bus, attach
  241. * it and then scan for unattached PCI devices.
  242. */
  243. if (!child)
  244. continue;
  245. if (list_empty(&child->node)) {
  246. down_write(&pci_bus_sem);
  247. list_add_tail(&child->node, &dev->bus->children);
  248. up_write(&pci_bus_sem);
  249. }
  250. pci_bus_add_devices(child);
  251. /*
  252. * register the bus with sysfs as the parent is now
  253. * properly registered.
  254. */
  255. if (child->is_added)
  256. continue;
  257. retval = pci_bus_add_child(child);
  258. if (retval)
  259. dev_err(&dev->dev, "Error adding bus, continuing\n");
  260. }
  261. }
  262. void pci_enable_bridges(struct pci_bus *bus)
  263. {
  264. struct pci_dev *dev;
  265. int retval;
  266. list_for_each_entry(dev, &bus->devices, bus_list) {
  267. if (dev->subordinate) {
  268. if (!pci_is_enabled(dev)) {
  269. retval = pci_enable_device(dev);
  270. if (retval)
  271. dev_err(&dev->dev, "Error enabling bridge (%d), continuing\n", retval);
  272. pci_set_master(dev);
  273. }
  274. pci_enable_bridges(dev->subordinate);
  275. }
  276. }
  277. }
  278. /** pci_walk_bus - walk devices on/under bus, calling callback.
  279. * @top bus whose devices should be walked
  280. * @cb callback to be called for each device found
  281. * @userdata arbitrary pointer to be passed to callback.
  282. *
  283. * Walk the given bus, including any bridged devices
  284. * on buses under this bus. Call the provided callback
  285. * on each device found.
  286. *
  287. * We check the return of @cb each time. If it returns anything
  288. * other than 0, we break out.
  289. *
  290. */
  291. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  292. void *userdata)
  293. {
  294. struct pci_dev *dev;
  295. struct pci_bus *bus;
  296. struct list_head *next;
  297. int retval;
  298. bus = top;
  299. down_read(&pci_bus_sem);
  300. next = top->devices.next;
  301. for (;;) {
  302. if (next == &bus->devices) {
  303. /* end of this bus, go up or finish */
  304. if (bus == top)
  305. break;
  306. next = bus->self->bus_list.next;
  307. bus = bus->self->bus;
  308. continue;
  309. }
  310. dev = list_entry(next, struct pci_dev, bus_list);
  311. if (dev->subordinate) {
  312. /* this is a pci-pci bridge, do its devices next */
  313. next = dev->subordinate->devices.next;
  314. bus = dev->subordinate;
  315. } else
  316. next = dev->bus_list.next;
  317. /* Run device routines with the device locked */
  318. device_lock(&dev->dev);
  319. retval = cb(dev, userdata);
  320. device_unlock(&dev->dev);
  321. if (retval)
  322. break;
  323. }
  324. up_read(&pci_bus_sem);
  325. }
  326. EXPORT_SYMBOL_GPL(pci_walk_bus);
  327. EXPORT_SYMBOL(pci_bus_alloc_resource);
  328. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  329. EXPORT_SYMBOL(pci_bus_add_devices);
  330. EXPORT_SYMBOL(pci_enable_bridges);