bus.c 10 KB

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