bus.c 11 KB

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