bus.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
  85. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  86. static struct pci_bus_region pci_64_bit = {0,
  87. (dma_addr_t) 0xffffffffffffffffULL};
  88. static struct pci_bus_region pci_high = {(dma_addr_t) 0x100000000ULL,
  89. (dma_addr_t) 0xffffffffffffffffULL};
  90. #endif
  91. /*
  92. * @res contains CPU addresses. Clip it so the corresponding bus addresses
  93. * on @bus are entirely within @region. This is used to control the bus
  94. * addresses of resources we allocate, e.g., we may need a resource that
  95. * can be mapped by a 32-bit BAR.
  96. */
  97. static void pci_clip_resource_to_region(struct pci_bus *bus,
  98. struct resource *res,
  99. struct pci_bus_region *region)
  100. {
  101. struct pci_bus_region r;
  102. pcibios_resource_to_bus(bus, &r, res);
  103. if (r.start < region->start)
  104. r.start = region->start;
  105. if (r.end > region->end)
  106. r.end = region->end;
  107. if (r.end < r.start)
  108. res->end = res->start - 1;
  109. else
  110. pcibios_bus_to_resource(bus, res, &r);
  111. }
  112. static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
  113. resource_size_t size, resource_size_t align,
  114. resource_size_t min, unsigned int type_mask,
  115. resource_size_t (*alignf)(void *,
  116. const struct resource *,
  117. resource_size_t,
  118. resource_size_t),
  119. void *alignf_data,
  120. struct pci_bus_region *region)
  121. {
  122. int i, ret;
  123. struct resource *r, avail;
  124. resource_size_t max;
  125. type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
  126. pci_bus_for_each_resource(bus, r, i) {
  127. if (!r)
  128. continue;
  129. /* type_mask must match */
  130. if ((res->flags ^ r->flags) & type_mask)
  131. continue;
  132. /* We cannot allocate a non-prefetching resource
  133. from a pre-fetching area */
  134. if ((r->flags & IORESOURCE_PREFETCH) &&
  135. !(res->flags & IORESOURCE_PREFETCH))
  136. continue;
  137. avail = *r;
  138. pci_clip_resource_to_region(bus, &avail, region);
  139. /*
  140. * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
  141. * protect badly documented motherboard resources, but if
  142. * this is an already-configured bridge window, its start
  143. * overrides "min".
  144. */
  145. if (avail.start)
  146. min = avail.start;
  147. max = avail.end;
  148. /* Ok, try it out.. */
  149. ret = allocate_resource(r, res, size, min, max,
  150. align, alignf, alignf_data);
  151. if (ret == 0)
  152. return 0;
  153. }
  154. return -ENOMEM;
  155. }
  156. /**
  157. * pci_bus_alloc_resource - allocate a resource from a parent bus
  158. * @bus: PCI bus
  159. * @res: resource to allocate
  160. * @size: size of resource to allocate
  161. * @align: alignment of resource to allocate
  162. * @min: minimum /proc/iomem address to allocate
  163. * @type_mask: IORESOURCE_* type flags
  164. * @alignf: resource alignment function
  165. * @alignf_data: data argument for resource alignment function
  166. *
  167. * Given the PCI bus a device resides on, the size, minimum address,
  168. * alignment and type, try to find an acceptable resource allocation
  169. * for a specific device resource.
  170. */
  171. int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  172. resource_size_t size, resource_size_t align,
  173. resource_size_t min, unsigned int type_mask,
  174. resource_size_t (*alignf)(void *,
  175. const struct resource *,
  176. resource_size_t,
  177. resource_size_t),
  178. void *alignf_data)
  179. {
  180. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  181. int rc;
  182. if (res->flags & IORESOURCE_MEM_64) {
  183. rc = pci_bus_alloc_from_region(bus, res, size, align, min,
  184. type_mask, alignf, alignf_data,
  185. &pci_high);
  186. if (rc == 0)
  187. return 0;
  188. return pci_bus_alloc_from_region(bus, res, size, align, min,
  189. type_mask, alignf, alignf_data,
  190. &pci_64_bit);
  191. }
  192. #endif
  193. return pci_bus_alloc_from_region(bus, res, size, align, min,
  194. type_mask, alignf, alignf_data,
  195. &pci_32_bit);
  196. }
  197. void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
  198. /**
  199. * pci_bus_add_device - start driver for a single device
  200. * @dev: device to add
  201. *
  202. * This adds add sysfs entries and start device drivers
  203. */
  204. int pci_bus_add_device(struct pci_dev *dev)
  205. {
  206. int retval;
  207. /*
  208. * Can not put in pci_device_add yet because resources
  209. * are not assigned yet for some devices.
  210. */
  211. pci_fixup_device(pci_fixup_final, dev);
  212. pci_create_sysfs_dev_files(dev);
  213. pci_proc_attach_device(dev);
  214. dev->match_driver = true;
  215. retval = device_attach(&dev->dev);
  216. WARN_ON(retval < 0);
  217. dev->is_added = 1;
  218. return 0;
  219. }
  220. /**
  221. * pci_bus_add_devices - start driver for PCI devices
  222. * @bus: bus to check for new devices
  223. *
  224. * Start driver for PCI devices and add some sysfs entries.
  225. */
  226. void pci_bus_add_devices(const struct pci_bus *bus)
  227. {
  228. struct pci_dev *dev;
  229. struct pci_bus *child;
  230. int retval;
  231. list_for_each_entry(dev, &bus->devices, bus_list) {
  232. /* Skip already-added devices */
  233. if (dev->is_added)
  234. continue;
  235. retval = pci_bus_add_device(dev);
  236. if (retval)
  237. dev_err(&dev->dev, "Error adding device (%d)\n",
  238. retval);
  239. }
  240. list_for_each_entry(dev, &bus->devices, bus_list) {
  241. BUG_ON(!dev->is_added);
  242. child = dev->subordinate;
  243. if (child)
  244. pci_bus_add_devices(child);
  245. }
  246. }
  247. /** pci_walk_bus - walk devices on/under bus, calling callback.
  248. * @top bus whose devices should be walked
  249. * @cb callback to be called for each device found
  250. * @userdata arbitrary pointer to be passed to callback.
  251. *
  252. * Walk the given bus, including any bridged devices
  253. * on buses under this bus. Call the provided callback
  254. * on each device found.
  255. *
  256. * We check the return of @cb each time. If it returns anything
  257. * other than 0, we break out.
  258. *
  259. */
  260. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  261. void *userdata)
  262. {
  263. struct pci_dev *dev;
  264. struct pci_bus *bus;
  265. struct list_head *next;
  266. int retval;
  267. bus = top;
  268. down_read(&pci_bus_sem);
  269. next = top->devices.next;
  270. for (;;) {
  271. if (next == &bus->devices) {
  272. /* end of this bus, go up or finish */
  273. if (bus == top)
  274. break;
  275. next = bus->self->bus_list.next;
  276. bus = bus->self->bus;
  277. continue;
  278. }
  279. dev = list_entry(next, struct pci_dev, bus_list);
  280. if (dev->subordinate) {
  281. /* this is a pci-pci bridge, do its devices next */
  282. next = dev->subordinate->devices.next;
  283. bus = dev->subordinate;
  284. } else
  285. next = dev->bus_list.next;
  286. retval = cb(dev, userdata);
  287. if (retval)
  288. break;
  289. }
  290. up_read(&pci_bus_sem);
  291. }
  292. EXPORT_SYMBOL_GPL(pci_walk_bus);
  293. struct pci_bus *pci_bus_get(struct pci_bus *bus)
  294. {
  295. if (bus)
  296. get_device(&bus->dev);
  297. return bus;
  298. }
  299. EXPORT_SYMBOL(pci_bus_get);
  300. void pci_bus_put(struct pci_bus *bus)
  301. {
  302. if (bus)
  303. put_device(&bus->dev);
  304. }
  305. EXPORT_SYMBOL(pci_bus_put);
  306. EXPORT_SYMBOL(pci_bus_alloc_resource);
  307. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  308. EXPORT_SYMBOL(pci_bus_add_devices);