bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. int devm_request_pci_bus_resources(struct device *dev,
  79. struct list_head *resources)
  80. {
  81. struct resource_entry *win;
  82. struct resource *parent, *res;
  83. int err;
  84. resource_list_for_each_entry(win, resources) {
  85. res = win->res;
  86. switch (resource_type(res)) {
  87. case IORESOURCE_IO:
  88. parent = &ioport_resource;
  89. break;
  90. case IORESOURCE_MEM:
  91. parent = &iomem_resource;
  92. break;
  93. default:
  94. continue;
  95. }
  96. err = devm_request_resource(dev, parent, res);
  97. if (err)
  98. return err;
  99. }
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(devm_request_pci_bus_resources);
  103. static struct pci_bus_region pci_32_bit = {0, 0xffffffffULL};
  104. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  105. static struct pci_bus_region pci_64_bit = {0,
  106. (pci_bus_addr_t) 0xffffffffffffffffULL};
  107. static struct pci_bus_region pci_high = {(pci_bus_addr_t) 0x100000000ULL,
  108. (pci_bus_addr_t) 0xffffffffffffffffULL};
  109. #endif
  110. /*
  111. * @res contains CPU addresses. Clip it so the corresponding bus addresses
  112. * on @bus are entirely within @region. This is used to control the bus
  113. * addresses of resources we allocate, e.g., we may need a resource that
  114. * can be mapped by a 32-bit BAR.
  115. */
  116. static void pci_clip_resource_to_region(struct pci_bus *bus,
  117. struct resource *res,
  118. struct pci_bus_region *region)
  119. {
  120. struct pci_bus_region r;
  121. pcibios_resource_to_bus(bus, &r, res);
  122. if (r.start < region->start)
  123. r.start = region->start;
  124. if (r.end > region->end)
  125. r.end = region->end;
  126. if (r.end < r.start)
  127. res->end = res->start - 1;
  128. else
  129. pcibios_bus_to_resource(bus, res, &r);
  130. }
  131. static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
  132. resource_size_t size, resource_size_t align,
  133. resource_size_t min, unsigned long type_mask,
  134. resource_size_t (*alignf)(void *,
  135. const struct resource *,
  136. resource_size_t,
  137. resource_size_t),
  138. void *alignf_data,
  139. struct pci_bus_region *region)
  140. {
  141. int i, ret;
  142. struct resource *r, avail;
  143. resource_size_t max;
  144. type_mask |= IORESOURCE_TYPE_BITS;
  145. pci_bus_for_each_resource(bus, r, i) {
  146. resource_size_t min_used = min;
  147. if (!r)
  148. continue;
  149. /* type_mask must match */
  150. if ((res->flags ^ r->flags) & type_mask)
  151. continue;
  152. /* We cannot allocate a non-prefetching resource
  153. from a pre-fetching area */
  154. if ((r->flags & IORESOURCE_PREFETCH) &&
  155. !(res->flags & IORESOURCE_PREFETCH))
  156. continue;
  157. avail = *r;
  158. pci_clip_resource_to_region(bus, &avail, region);
  159. /*
  160. * "min" is typically PCIBIOS_MIN_IO or PCIBIOS_MIN_MEM to
  161. * protect badly documented motherboard resources, but if
  162. * this is an already-configured bridge window, its start
  163. * overrides "min".
  164. */
  165. if (avail.start)
  166. min_used = avail.start;
  167. max = avail.end;
  168. /* Ok, try it out.. */
  169. ret = allocate_resource(r, res, size, min_used, max,
  170. align, alignf, alignf_data);
  171. if (ret == 0)
  172. return 0;
  173. }
  174. return -ENOMEM;
  175. }
  176. /**
  177. * pci_bus_alloc_resource - allocate a resource from a parent bus
  178. * @bus: PCI bus
  179. * @res: resource to allocate
  180. * @size: size of resource to allocate
  181. * @align: alignment of resource to allocate
  182. * @min: minimum /proc/iomem address to allocate
  183. * @type_mask: IORESOURCE_* type flags
  184. * @alignf: resource alignment function
  185. * @alignf_data: data argument for resource alignment function
  186. *
  187. * Given the PCI bus a device resides on, the size, minimum address,
  188. * alignment and type, try to find an acceptable resource allocation
  189. * for a specific device resource.
  190. */
  191. int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res,
  192. resource_size_t size, resource_size_t align,
  193. resource_size_t min, unsigned long type_mask,
  194. resource_size_t (*alignf)(void *,
  195. const struct resource *,
  196. resource_size_t,
  197. resource_size_t),
  198. void *alignf_data)
  199. {
  200. #ifdef CONFIG_PCI_BUS_ADDR_T_64BIT
  201. int rc;
  202. if (res->flags & IORESOURCE_MEM_64) {
  203. rc = pci_bus_alloc_from_region(bus, res, size, align, min,
  204. type_mask, alignf, alignf_data,
  205. &pci_high);
  206. if (rc == 0)
  207. return 0;
  208. return pci_bus_alloc_from_region(bus, res, size, align, min,
  209. type_mask, alignf, alignf_data,
  210. &pci_64_bit);
  211. }
  212. #endif
  213. return pci_bus_alloc_from_region(bus, res, size, align, min,
  214. type_mask, alignf, alignf_data,
  215. &pci_32_bit);
  216. }
  217. EXPORT_SYMBOL(pci_bus_alloc_resource);
  218. /*
  219. * The @idx resource of @dev should be a PCI-PCI bridge window. If this
  220. * resource fits inside a window of an upstream bridge, do nothing. If it
  221. * overlaps an upstream window but extends outside it, clip the resource so
  222. * it fits completely inside.
  223. */
  224. bool pci_bus_clip_resource(struct pci_dev *dev, int idx)
  225. {
  226. struct pci_bus *bus = dev->bus;
  227. struct resource *res = &dev->resource[idx];
  228. struct resource orig_res = *res;
  229. struct resource *r;
  230. int i;
  231. pci_bus_for_each_resource(bus, r, i) {
  232. resource_size_t start, end;
  233. if (!r)
  234. continue;
  235. if (resource_type(res) != resource_type(r))
  236. continue;
  237. start = max(r->start, res->start);
  238. end = min(r->end, res->end);
  239. if (start > end)
  240. continue; /* no overlap */
  241. if (res->start == start && res->end == end)
  242. return false; /* no change */
  243. res->start = start;
  244. res->end = end;
  245. res->flags &= ~IORESOURCE_UNSET;
  246. orig_res.flags &= ~IORESOURCE_UNSET;
  247. dev_printk(KERN_DEBUG, &dev->dev, "%pR clipped to %pR\n",
  248. &orig_res, res);
  249. return true;
  250. }
  251. return false;
  252. }
  253. void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { }
  254. void __weak pcibios_bus_add_device(struct pci_dev *pdev) { }
  255. /**
  256. * pci_bus_add_device - start driver for a single device
  257. * @dev: device to add
  258. *
  259. * This adds add sysfs entries and start device drivers
  260. */
  261. void pci_bus_add_device(struct pci_dev *dev)
  262. {
  263. int retval;
  264. /*
  265. * Can not put in pci_device_add yet because resources
  266. * are not assigned yet for some devices.
  267. */
  268. pcibios_bus_add_device(dev);
  269. pci_fixup_device(pci_fixup_final, dev);
  270. pci_create_sysfs_dev_files(dev);
  271. pci_proc_attach_device(dev);
  272. pci_bridge_d3_device_changed(dev);
  273. dev->match_driver = true;
  274. retval = device_attach(&dev->dev);
  275. if (retval < 0 && retval != -EPROBE_DEFER) {
  276. dev_warn(&dev->dev, "device attach failed (%d)\n", retval);
  277. pci_proc_detach_device(dev);
  278. pci_remove_sysfs_dev_files(dev);
  279. return;
  280. }
  281. dev->is_added = 1;
  282. }
  283. EXPORT_SYMBOL_GPL(pci_bus_add_device);
  284. /**
  285. * pci_bus_add_devices - start driver for PCI devices
  286. * @bus: bus to check for new devices
  287. *
  288. * Start driver for PCI devices and add some sysfs entries.
  289. */
  290. void pci_bus_add_devices(const struct pci_bus *bus)
  291. {
  292. struct pci_dev *dev;
  293. struct pci_bus *child;
  294. list_for_each_entry(dev, &bus->devices, bus_list) {
  295. /* Skip already-added devices */
  296. if (dev->is_added)
  297. continue;
  298. pci_bus_add_device(dev);
  299. }
  300. list_for_each_entry(dev, &bus->devices, bus_list) {
  301. /* Skip if device attach failed */
  302. if (!dev->is_added)
  303. continue;
  304. child = dev->subordinate;
  305. if (child)
  306. pci_bus_add_devices(child);
  307. }
  308. }
  309. EXPORT_SYMBOL(pci_bus_add_devices);
  310. /** pci_walk_bus - walk devices on/under bus, calling callback.
  311. * @top bus whose devices should be walked
  312. * @cb callback to be called for each device found
  313. * @userdata arbitrary pointer to be passed to callback.
  314. *
  315. * Walk the given bus, including any bridged devices
  316. * on buses under this bus. Call the provided callback
  317. * on each device found.
  318. *
  319. * We check the return of @cb each time. If it returns anything
  320. * other than 0, we break out.
  321. *
  322. */
  323. void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
  324. void *userdata)
  325. {
  326. struct pci_dev *dev;
  327. struct pci_bus *bus;
  328. struct list_head *next;
  329. int retval;
  330. bus = top;
  331. down_read(&pci_bus_sem);
  332. next = top->devices.next;
  333. for (;;) {
  334. if (next == &bus->devices) {
  335. /* end of this bus, go up or finish */
  336. if (bus == top)
  337. break;
  338. next = bus->self->bus_list.next;
  339. bus = bus->self->bus;
  340. continue;
  341. }
  342. dev = list_entry(next, struct pci_dev, bus_list);
  343. if (dev->subordinate) {
  344. /* this is a pci-pci bridge, do its devices next */
  345. next = dev->subordinate->devices.next;
  346. bus = dev->subordinate;
  347. } else
  348. next = dev->bus_list.next;
  349. retval = cb(dev, userdata);
  350. if (retval)
  351. break;
  352. }
  353. up_read(&pci_bus_sem);
  354. }
  355. EXPORT_SYMBOL_GPL(pci_walk_bus);
  356. struct pci_bus *pci_bus_get(struct pci_bus *bus)
  357. {
  358. if (bus)
  359. get_device(&bus->dev);
  360. return bus;
  361. }
  362. EXPORT_SYMBOL(pci_bus_get);
  363. void pci_bus_put(struct pci_bus *bus)
  364. {
  365. if (bus)
  366. put_device(&bus->dev);
  367. }
  368. EXPORT_SYMBOL(pci_bus_put);