devres.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/err.h>
  3. #include <linux/pci.h>
  4. #include <linux/io.h>
  5. #include <linux/gfp.h>
  6. #include <linux/export.h>
  7. #include <linux/of_address.h>
  8. enum devm_ioremap_type {
  9. DEVM_IOREMAP = 0,
  10. DEVM_IOREMAP_NC,
  11. DEVM_IOREMAP_UC,
  12. DEVM_IOREMAP_WC,
  13. };
  14. void devm_ioremap_release(struct device *dev, void *res)
  15. {
  16. iounmap(*(void __iomem **)res);
  17. }
  18. static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
  19. {
  20. return *(void **)res == match_data;
  21. }
  22. static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
  23. resource_size_t size,
  24. enum devm_ioremap_type type)
  25. {
  26. void __iomem **ptr, *addr = NULL;
  27. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  28. if (!ptr)
  29. return NULL;
  30. switch (type) {
  31. case DEVM_IOREMAP:
  32. addr = ioremap(offset, size);
  33. break;
  34. case DEVM_IOREMAP_NC:
  35. addr = ioremap_nocache(offset, size);
  36. break;
  37. case DEVM_IOREMAP_UC:
  38. addr = ioremap_uc(offset, size);
  39. break;
  40. case DEVM_IOREMAP_WC:
  41. addr = ioremap_wc(offset, size);
  42. break;
  43. }
  44. if (addr) {
  45. *ptr = addr;
  46. devres_add(dev, ptr);
  47. } else
  48. devres_free(ptr);
  49. return addr;
  50. }
  51. /**
  52. * devm_ioremap - Managed ioremap()
  53. * @dev: Generic device to remap IO address for
  54. * @offset: Resource address to map
  55. * @size: Size of map
  56. *
  57. * Managed ioremap(). Map is automatically unmapped on driver detach.
  58. */
  59. void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
  60. resource_size_t size)
  61. {
  62. return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
  63. }
  64. EXPORT_SYMBOL(devm_ioremap);
  65. /**
  66. * devm_ioremap_uc - Managed ioremap_uc()
  67. * @dev: Generic device to remap IO address for
  68. * @offset: Resource address to map
  69. * @size: Size of map
  70. *
  71. * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
  72. */
  73. void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
  74. resource_size_t size)
  75. {
  76. return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_UC);
  77. }
  78. EXPORT_SYMBOL_GPL(devm_ioremap_uc);
  79. /**
  80. * devm_ioremap_nocache - Managed ioremap_nocache()
  81. * @dev: Generic device to remap IO address for
  82. * @offset: Resource address to map
  83. * @size: Size of map
  84. *
  85. * Managed ioremap_nocache(). Map is automatically unmapped on driver
  86. * detach.
  87. */
  88. void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
  89. resource_size_t size)
  90. {
  91. return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
  92. }
  93. EXPORT_SYMBOL(devm_ioremap_nocache);
  94. /**
  95. * devm_ioremap_wc - Managed ioremap_wc()
  96. * @dev: Generic device to remap IO address for
  97. * @offset: Resource address to map
  98. * @size: Size of map
  99. *
  100. * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
  101. */
  102. void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
  103. resource_size_t size)
  104. {
  105. return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
  106. }
  107. EXPORT_SYMBOL(devm_ioremap_wc);
  108. /**
  109. * devm_iounmap - Managed iounmap()
  110. * @dev: Generic device to unmap for
  111. * @addr: Address to unmap
  112. *
  113. * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
  114. */
  115. void devm_iounmap(struct device *dev, void __iomem *addr)
  116. {
  117. WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
  118. (__force void *)addr));
  119. iounmap(addr);
  120. }
  121. EXPORT_SYMBOL(devm_iounmap);
  122. /**
  123. * devm_ioremap_resource() - check, request region, and ioremap resource
  124. * @dev: generic device to handle the resource for
  125. * @res: resource to be handled
  126. *
  127. * Checks that a resource is a valid memory region, requests the memory
  128. * region and ioremaps it. All operations are managed and will be undone
  129. * on driver detach.
  130. *
  131. * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
  132. * on failure. Usage example:
  133. *
  134. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  135. * base = devm_ioremap_resource(&pdev->dev, res);
  136. * if (IS_ERR(base))
  137. * return PTR_ERR(base);
  138. */
  139. void __iomem *devm_ioremap_resource(struct device *dev,
  140. const struct resource *res)
  141. {
  142. resource_size_t size;
  143. const char *name;
  144. void __iomem *dest_ptr;
  145. BUG_ON(!dev);
  146. if (!res || resource_type(res) != IORESOURCE_MEM) {
  147. dev_err(dev, "invalid resource\n");
  148. return IOMEM_ERR_PTR(-EINVAL);
  149. }
  150. size = resource_size(res);
  151. name = res->name ?: dev_name(dev);
  152. if (!devm_request_mem_region(dev, res->start, size, name)) {
  153. dev_err(dev, "can't request region for resource %pR\n", res);
  154. return IOMEM_ERR_PTR(-EBUSY);
  155. }
  156. dest_ptr = devm_ioremap(dev, res->start, size);
  157. if (!dest_ptr) {
  158. dev_err(dev, "ioremap failed for resource %pR\n", res);
  159. devm_release_mem_region(dev, res->start, size);
  160. dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
  161. }
  162. return dest_ptr;
  163. }
  164. EXPORT_SYMBOL(devm_ioremap_resource);
  165. /*
  166. * devm_of_iomap - Requests a resource and maps the memory mapped IO
  167. * for a given device_node managed by a given device
  168. *
  169. * Checks that a resource is a valid memory region, requests the memory
  170. * region and ioremaps it. All operations are managed and will be undone
  171. * on driver detach of the device.
  172. *
  173. * This is to be used when a device requests/maps resources described
  174. * by other device tree nodes (children or otherwise).
  175. *
  176. * @dev: The device "managing" the resource
  177. * @node: The device-tree node where the resource resides
  178. * @index: index of the MMIO range in the "reg" property
  179. * @size: Returns the size of the resource (pass NULL if not needed)
  180. * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
  181. * error code on failure. Usage example:
  182. *
  183. * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
  184. * if (IS_ERR(base))
  185. * return PTR_ERR(base);
  186. */
  187. void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
  188. resource_size_t *size)
  189. {
  190. struct resource res;
  191. if (of_address_to_resource(node, index, &res))
  192. return IOMEM_ERR_PTR(-EINVAL);
  193. if (size)
  194. *size = resource_size(&res);
  195. return devm_ioremap_resource(dev, &res);
  196. }
  197. EXPORT_SYMBOL(devm_of_iomap);
  198. #ifdef CONFIG_HAS_IOPORT_MAP
  199. /*
  200. * Generic iomap devres
  201. */
  202. static void devm_ioport_map_release(struct device *dev, void *res)
  203. {
  204. ioport_unmap(*(void __iomem **)res);
  205. }
  206. static int devm_ioport_map_match(struct device *dev, void *res,
  207. void *match_data)
  208. {
  209. return *(void **)res == match_data;
  210. }
  211. /**
  212. * devm_ioport_map - Managed ioport_map()
  213. * @dev: Generic device to map ioport for
  214. * @port: Port to map
  215. * @nr: Number of ports to map
  216. *
  217. * Managed ioport_map(). Map is automatically unmapped on driver
  218. * detach.
  219. */
  220. void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
  221. unsigned int nr)
  222. {
  223. void __iomem **ptr, *addr;
  224. ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
  225. if (!ptr)
  226. return NULL;
  227. addr = ioport_map(port, nr);
  228. if (addr) {
  229. *ptr = addr;
  230. devres_add(dev, ptr);
  231. } else
  232. devres_free(ptr);
  233. return addr;
  234. }
  235. EXPORT_SYMBOL(devm_ioport_map);
  236. /**
  237. * devm_ioport_unmap - Managed ioport_unmap()
  238. * @dev: Generic device to unmap for
  239. * @addr: Address to unmap
  240. *
  241. * Managed ioport_unmap(). @addr must have been mapped using
  242. * devm_ioport_map().
  243. */
  244. void devm_ioport_unmap(struct device *dev, void __iomem *addr)
  245. {
  246. ioport_unmap(addr);
  247. WARN_ON(devres_destroy(dev, devm_ioport_map_release,
  248. devm_ioport_map_match, (__force void *)addr));
  249. }
  250. EXPORT_SYMBOL(devm_ioport_unmap);
  251. #endif /* CONFIG_HAS_IOPORT_MAP */
  252. #ifdef CONFIG_PCI
  253. /*
  254. * PCI iomap devres
  255. */
  256. #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
  257. struct pcim_iomap_devres {
  258. void __iomem *table[PCIM_IOMAP_MAX];
  259. };
  260. static void pcim_iomap_release(struct device *gendev, void *res)
  261. {
  262. struct pci_dev *dev = to_pci_dev(gendev);
  263. struct pcim_iomap_devres *this = res;
  264. int i;
  265. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  266. if (this->table[i])
  267. pci_iounmap(dev, this->table[i]);
  268. }
  269. /**
  270. * pcim_iomap_table - access iomap allocation table
  271. * @pdev: PCI device to access iomap table for
  272. *
  273. * Access iomap allocation table for @dev. If iomap table doesn't
  274. * exist and @pdev is managed, it will be allocated. All iomaps
  275. * recorded in the iomap table are automatically unmapped on driver
  276. * detach.
  277. *
  278. * This function might sleep when the table is first allocated but can
  279. * be safely called without context and guaranteed to succed once
  280. * allocated.
  281. */
  282. void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
  283. {
  284. struct pcim_iomap_devres *dr, *new_dr;
  285. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  286. if (dr)
  287. return dr->table;
  288. new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
  289. if (!new_dr)
  290. return NULL;
  291. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  292. return dr->table;
  293. }
  294. EXPORT_SYMBOL(pcim_iomap_table);
  295. /**
  296. * pcim_iomap - Managed pcim_iomap()
  297. * @pdev: PCI device to iomap for
  298. * @bar: BAR to iomap
  299. * @maxlen: Maximum length of iomap
  300. *
  301. * Managed pci_iomap(). Map is automatically unmapped on driver
  302. * detach.
  303. */
  304. void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  305. {
  306. void __iomem **tbl;
  307. BUG_ON(bar >= PCIM_IOMAP_MAX);
  308. tbl = (void __iomem **)pcim_iomap_table(pdev);
  309. if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
  310. return NULL;
  311. tbl[bar] = pci_iomap(pdev, bar, maxlen);
  312. return tbl[bar];
  313. }
  314. EXPORT_SYMBOL(pcim_iomap);
  315. /**
  316. * pcim_iounmap - Managed pci_iounmap()
  317. * @pdev: PCI device to iounmap for
  318. * @addr: Address to unmap
  319. *
  320. * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
  321. */
  322. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  323. {
  324. void __iomem **tbl;
  325. int i;
  326. pci_iounmap(pdev, addr);
  327. tbl = (void __iomem **)pcim_iomap_table(pdev);
  328. BUG_ON(!tbl);
  329. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  330. if (tbl[i] == addr) {
  331. tbl[i] = NULL;
  332. return;
  333. }
  334. WARN_ON(1);
  335. }
  336. EXPORT_SYMBOL(pcim_iounmap);
  337. /**
  338. * pcim_iomap_regions - Request and iomap PCI BARs
  339. * @pdev: PCI device to map IO resources for
  340. * @mask: Mask of BARs to request and iomap
  341. * @name: Name used when requesting regions
  342. *
  343. * Request and iomap regions specified by @mask.
  344. */
  345. int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
  346. {
  347. void __iomem * const *iomap;
  348. int i, rc;
  349. iomap = pcim_iomap_table(pdev);
  350. if (!iomap)
  351. return -ENOMEM;
  352. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  353. unsigned long len;
  354. if (!(mask & (1 << i)))
  355. continue;
  356. rc = -EINVAL;
  357. len = pci_resource_len(pdev, i);
  358. if (!len)
  359. goto err_inval;
  360. rc = pci_request_region(pdev, i, name);
  361. if (rc)
  362. goto err_inval;
  363. rc = -ENOMEM;
  364. if (!pcim_iomap(pdev, i, 0))
  365. goto err_region;
  366. }
  367. return 0;
  368. err_region:
  369. pci_release_region(pdev, i);
  370. err_inval:
  371. while (--i >= 0) {
  372. if (!(mask & (1 << i)))
  373. continue;
  374. pcim_iounmap(pdev, iomap[i]);
  375. pci_release_region(pdev, i);
  376. }
  377. return rc;
  378. }
  379. EXPORT_SYMBOL(pcim_iomap_regions);
  380. /**
  381. * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
  382. * @pdev: PCI device to map IO resources for
  383. * @mask: Mask of BARs to iomap
  384. * @name: Name used when requesting regions
  385. *
  386. * Request all PCI BARs and iomap regions specified by @mask.
  387. */
  388. int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
  389. const char *name)
  390. {
  391. int request_mask = ((1 << 6) - 1) & ~mask;
  392. int rc;
  393. rc = pci_request_selected_regions(pdev, request_mask, name);
  394. if (rc)
  395. return rc;
  396. rc = pcim_iomap_regions(pdev, mask, name);
  397. if (rc)
  398. pci_release_selected_regions(pdev, request_mask);
  399. return rc;
  400. }
  401. EXPORT_SYMBOL(pcim_iomap_regions_request_all);
  402. /**
  403. * pcim_iounmap_regions - Unmap and release PCI BARs
  404. * @pdev: PCI device to map IO resources for
  405. * @mask: Mask of BARs to unmap and release
  406. *
  407. * Unmap and release regions specified by @mask.
  408. */
  409. void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
  410. {
  411. void __iomem * const *iomap;
  412. int i;
  413. iomap = pcim_iomap_table(pdev);
  414. if (!iomap)
  415. return;
  416. for (i = 0; i < PCIM_IOMAP_MAX; i++) {
  417. if (!(mask & (1 << i)))
  418. continue;
  419. pcim_iounmap(pdev, iomap[i]);
  420. pci_release_region(pdev, i);
  421. }
  422. }
  423. EXPORT_SYMBOL(pcim_iounmap_regions);
  424. #endif /* CONFIG_PCI */