bus.c 9.5 KB

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