bus.c 11 KB

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