bus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/slab.h>
  17. #include "pci.h"
  18. void pci_add_resource_offset(struct list_head *resources, struct resource *res,
  19. resource_size_t offset)
  20. {
  21. struct resource_entry *entry;
  22. entry = resource_list_create_entry(res, 0);
  23. if (!entry) {
  24. printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
  25. return;
  26. }
  27. entry->offset = offset;
  28. resource_list_add_tail(entry, resources);
  29. }
  30. EXPORT_SYMBOL(pci_add_resource_offset);
  31. void pci_add_resource(struct list_head *resources, struct resource *res)
  32. {
  33. pci_add_resource_offset(resources, res, 0);
  34. }
  35. EXPORT_SYMBOL(pci_add_resource);
  36. void pci_free_resource_list(struct list_head *resources)
  37. {
  38. resource_list_free(resources);
  39. }
  40. EXPORT_SYMBOL(pci_free_resource_list);
  41. void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
  42. unsigned int flags)
  43. {
  44. struct pci_bus_resource *bus_res;
  45. bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL);
  46. if (!bus_res) {
  47. dev_err(&bus->dev, "can't add %pR resource\n", res);
  48. return;
  49. }
  50. bus_res->res = res;
  51. bus_res->flags = flags;
  52. list_add_tail(&bus_res->list, &bus->resources);
  53. }
  54. struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n)
  55. {
  56. struct pci_bus_resource *bus_res;
  57. if (n < PCI_BRIDGE_RESOURCE_NUM)
  58. return bus->resource[n];
  59. n -= PCI_BRIDGE_RESOURCE_NUM;
  60. list_for_each_entry(bus_res, &bus->resources, list) {
  61. if (n-- == 0)
  62. return bus_res->res;
  63. }
  64. return NULL;
  65. }
  66. EXPORT_SYMBOL_GPL(pci_bus_resource_n);
  67. void pci_bus_remove_resources(struct pci_bus *bus)
  68. {
  69. int i;
  70. struct pci_bus_resource *bus_res, *tmp;
  71. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  72. bus->resource[i] = NULL;
  73. list_for_each_entry_safe(bus_res, tmp, &bus->resources, list) {
  74. list_del(&bus_res->list);
  75. kfree(bus_res);
  76. }
  77. }
  78. static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
  79. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  80. static struct pci_bus_region pci_64_bit = {0,
  81. (pci_bus_addr_t) 0xffffffffffffffffULL};
  82. static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
  83. (pci_bus_addr_t) 0xffffffffffffffffULL};
  84. #endif
  85. /*
  86. * @res contains CPU addresses. Clip it so the corresponding bus addresses
  87. * on @bus are entirely within @region. This is used to control the bus
  88. * addresses of resources we allocate, e.g., we may need a resource that
  89. * can be mapped by a 32-bit BAR.
  90. */
  91. static void pci_clip_resource_to_region(struct pci_bus *bus,
  92. struct resource *res,
  93. struct pci_bus_region *region)
  94. {
  95. struct pci_bus_region r;
  96. pcibios_resource_to_bus(bus, &r, res);
  97. if (r.start < region->start)
  98. r.start = region->start;
  99. if (r.end > region->end)
  100. r.end = region->end;
  101. if (r.end < r.start)
  102. res->end = res->start - 1;
  103. else
  104. pcibios_bus_to_resource(bus, res, &r);
  105. }
  106. static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
  107. resource_size_t size, resource_size_t align,
  108. resource_size_t min, unsigned long type_mask,
  109. resource_size_t (*alignf)(void *,
  110. const struct resource *,
  111. resource_size_t,
  112. resource_size_t),
  113. void *alignf_data,
  114. struct pci_bus_region *region)
  115. {
  116. int i, ret;
  117. struct resource *r, avail;
  118. resource_size_t max;
  119. type_mask |= IORESOURCE_TYPE_BITS;
  120. pci_bus_for_each_resource(bus, r, i) {
  121. if (!r)
  122. continue;
  123. /* type_mask must match */
  124. if ((res->flags ^ r->flags) & type_mask)
  125. continue;
  126. /* We cannot allocate a non-prefetching resource
  127. from a pre-fetching area */
  128. if ((r->flags & IORESOURCE_PREFETCH) &&
  129. !(res->flags & IORESOURCE_PREFETCH))
  130. continue;
  131. avail = *r;
  132. pci_clip_resource_to_region(bus, &avail, region);
  133. /*
  134. * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
  135. * protect badly documented motherboard resources, but if
  136. * this is an already-configured bridge window, its start
  137. * overrides "min".
  138. */
  139. if (avail.start)
  140. min = avail.start;
  141. max = avail.end;
  142. /* Ok, try it out.. */
  143. ret = allocate_resource(r, res, size, min, max,
  144. align, alignf, alignf_data);
  145. if (ret == 0)
  146. return 0;
  147. }
  148. return -ENOMEM;
  149. }
  150. /**
  151. * pci_bus_alloc_resource - allocate a resource from a parent bus
  152. * @bus: PCI bus
  153. * @res: resource to allocate
  154. * @size: size of resource to allocate
  155. * @align: alignment of resource to allocate
  156. * @min: minimum /proc/iomem address to allocate
  157. * @type_mask: IORESOURCE_* type flags
  158. * @alignf: resource alignment function
  159. * @alignf_data: data argument for resource alignment function
  160. *
  161. * Given the PCI bus a device resides on, the size, minimum address,
  162. * alignment and type, try to find an acceptable resource allocation
  163. * for a specific device resource.
  164. */
  165. int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  166. resource_size_t size, resource_size_t align,
  167. resource_size_t min, unsigned long type_mask,
  168. resource_size_t (*alignf)(void *,
  169. const struct resource *,
  170. resource_size_t,
  171. resource_size_t),
  172. void *alignf_data)
  173. {
  174. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  175. int rc;
  176. if (res->flags & IORESOURCE_MEM_64) {
  177. rc = pci_bus_alloc_from_region(bus, res, size, align, min,
  178. type_mask, alignf, alignf_data,
  179. &pci_high);
  180. if (rc == 0)
  181. return 0;
  182. return pci_bus_alloc_from_region(bus, res, size, align, min,
  183. type_mask, alignf, alignf_data,
  184. &pci_64_bit);
  185. }
  186. #endif
  187. return pci_bus_alloc_from_region(bus, res, size, align, min,
  188. type_mask, alignf, alignf_data,
  189. &pci_32_bit);
  190. }
  191. EXPORT_SYMBOL(pci_bus_alloc_resource);
  192. /*
  193. * The @idx resource of @dev should be a PCI-PCI bridge window. If this
  194. * resource fits inside a window of an upstream bridge, do nothing. If it
  195. * overlaps an upstream window but extends outside it, clip the resource so
  196. * it fits completely inside.
  197. */
  198. bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
  199. {
  200. struct pci_bus *bus = dev->bus;
  201. struct resource *res = &dev->resource[idx];
  202. struct resource orig_res = *res;
  203. struct resource *r;
  204. int i;
  205. pci_bus_for_each_resource(bus, r, i) {
  206. resource_size_t start, end;
  207. if (!r)
  208. continue;
  209. if (resource_type(res) != resource_type(r))
  210. continue;
  211. start = max(r->start, res->start);
  212. end = min(r->end, res->end);
  213. if (start > end)
  214. continue; /* no overlap */
  215. if (res->start == start && res->end == end)
  216. return false; /* no change */
  217. res->start = start;
  218. res->end = end;
  219. res->flags &= ~IORESOURCE_UNSET;
  220. orig_res.flags &= ~IORESOURCE_UNSET;
  221. dev_printk(KERN_DEBUG, &dev->dev, "%pR clipped to %pR\n",
  222. &orig_res, res);
  223. return true;
  224. }
  225. return false;
  226. }
  227. void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
  228. /**
  229. * pci_bus_add_device - start driver for a single device
  230. * @dev: device to add
  231. *
  232. * This adds add sysfs entries and start device drivers
  233. */
  234. void pci_bus_add_device(struct pci_dev *dev)
  235. {
  236. int retval;
  237. /*
  238. * Can not put in pci_device_add yet because resources
  239. * are not assigned yet for some devices.
  240. */
  241. pci_fixup_device(pci_fixup_final, dev);
  242. pci_create_sysfs_dev_files(dev);
  243. pci_proc_attach_device(dev);
  244. dev->match_driver = true;
  245. retval = device_attach(&dev->dev);
  246. WARN_ON(retval < 0);
  247. dev->is_added = 1;
  248. }
  249. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  250. /**
  251. * pci_bus_add_devices - start driver for PCI devices
  252. * @bus: bus to check for new devices
  253. *
  254. * Start driver for PCI devices and add some sysfs entries.
  255. */
  256. void pci_bus_add_devices(const struct pci_bus *bus)
  257. {
  258. struct pci_dev *dev;
  259. struct pci_bus *child;
  260. list_for_each_entry(dev, &bus->devices, bus_list) {
  261. /* Skip already-added devices */
  262. if (dev->is_added)
  263. continue;
  264. pci_bus_add_device(dev);
  265. }
  266. list_for_each_entry(dev, &bus->devices, bus_list) {
  267. BUG_ON(!dev->is_added);
  268. child = dev->subordinate;
  269. if (child)
  270. pci_bus_add_devices(child);
  271. }
  272. }
  273. EXPORT_SYMBOL(pci_bus_add_devices);
  274. /** pci_walk_bus - walk devices on/under bus, calling callback.
  275. * @top bus whose devices should be walked
  276. * @cb callback to be called for each device found
  277. * @userdata arbitrary pointer to be passed to callback.
  278. *
  279. * Walk the given bus, including any bridged devices
  280. * on buses under this bus. Call the provided callback
  281. * on each device found.
  282. *
  283. * We check the return of @cb each time. If it returns anything
  284. * other than 0, we break out.
  285. *
  286. */
  287. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  288. void *userdata)
  289. {
  290. struct pci_dev *dev;
  291. struct pci_bus *bus;
  292. struct list_head *next;
  293. int retval;
  294. bus = top;
  295. down_read(&pci_bus_sem);
  296. next = top->devices.next;
  297. for (;;) {
  298. if (next == &bus->devices) {
  299. /* end of this bus, go up or finish */
  300. if (bus == top)
  301. break;
  302. next = bus->self->bus_list.next;
  303. bus = bus->self->bus;
  304. continue;
  305. }
  306. dev = list_entry(next, struct pci_dev, bus_list);
  307. if (dev->subordinate) {
  308. /* this is a pci-pci bridge, do its devices next */
  309. next = dev->subordinate->devices.next;
  310. bus = dev->subordinate;
  311. } else
  312. next = dev->bus_list.next;
  313. retval = cb(dev, userdata);
  314. if (retval)
  315. break;
  316. }
  317. up_read(&pci_bus_sem);
  318. }
  319. EXPORT_SYMBOL_GPL(pci_walk_bus);
  320. struct pci_bus *pci_bus_get(struct pci_bus *bus)
  321. {
  322. if (bus)
  323. get_device(&bus->dev);
  324. return bus;
  325. }
  326. EXPORT_SYMBOL(pci_bus_get);
  327. void pci_bus_put(struct pci_bus *bus)
  328. {
  329. if (bus)
  330. put_device(&bus->dev);
  331. }
  332. EXPORT_SYMBOL(pci_bus_put);