devres.c 12 KB

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