devres.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <linux/pci.h>
  2. #include <linux/io.h>
  3. #include <linux/gfp.h>
  4. #include <linux/export.h>
  5. void devm_ioremap_release(struct device *dev, void *res)
  6. {
  7. iounmap(*(void __iomem **)res);
  8. }
  9. static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
  10. {
  11. return *(void **)res == match_data;
  12. }
  13. /**
  14. * devm_ioremap - Managed ioremap()
  15. * @dev: Generic device to remap IO address for
  16. * @offset: BUS offset to map
  17. * @size: Size of map
  18. *
  19. * Managed ioremap(). Map is automatically unmapped on driver detach.
  20. */
  21. void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
  22. unsigned long size)
  23. {
  24. void __iomem **ptr, *addr;
  25. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  26. if (!ptr)
  27. return NULL;
  28. addr = ioremap(offset, size);
  29. if (addr) {
  30. *ptr = addr;
  31. devres_add(dev, ptr);
  32. } else
  33. devres_free(ptr);
  34. return addr;
  35. }
  36. EXPORT_SYMBOL(devm_ioremap);
  37. /**
  38. * devm_ioremap_nocache - Managed ioremap_nocache()
  39. * @dev: Generic device to remap IO address for
  40. * @offset: BUS offset to map
  41. * @size: Size of map
  42. *
  43. * Managed ioremap_nocache(). Map is automatically unmapped on driver
  44. * detach.
  45. */
  46. void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
  47. unsigned long size)
  48. {
  49. void __iomem **ptr, *addr;
  50. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  51. if (!ptr)
  52. return NULL;
  53. addr = ioremap_nocache(offset, size);
  54. if (addr) {
  55. *ptr = addr;
  56. devres_add(dev, ptr);
  57. } else
  58. devres_free(ptr);
  59. return addr;
  60. }
  61. EXPORT_SYMBOL(devm_ioremap_nocache);
  62. /**
  63. * devm_iounmap - Managed iounmap()
  64. * @dev: Generic device to unmap for
  65. * @addr: Address to unmap
  66. *
  67. * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
  68. */
  69. void devm_iounmap(struct device *dev, void __iomem *addr)
  70. {
  71. WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
  72. (void *)addr));
  73. iounmap(addr);
  74. }
  75. EXPORT_SYMBOL(devm_iounmap);
  76. /**
  77. * devm_request_and_ioremap() - Check, request region, and ioremap resource
  78. * @dev: Generic device to handle the resource for
  79. * @res: resource to be handled
  80. *
  81. * Takes all necessary steps to ioremap a mem resource. Uses managed device, so
  82. * everything is undone on driver detach. Checks arguments, so you can feed
  83. * it the result from e.g. platform_get_resource() directly. Returns the
  84. * remapped pointer or NULL on error. Usage example:
  85. *
  86. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87. * base = devm_request_and_ioremap(&pdev->dev, res);
  88. * if (!base)
  89. * return -EADDRNOTAVAIL;
  90. */
  91. void __iomem *devm_request_and_ioremap(struct device *dev,
  92. struct resource *res)
  93. {
  94. resource_size_t size;
  95. const char *name;
  96. void __iomem *dest_ptr;
  97. BUG_ON(!dev);
  98. if (!res || resource_type(res) != IORESOURCE_MEM) {
  99. dev_err(dev, "invalid resource\n");
  100. return NULL;
  101. }
  102. size = resource_size(res);
  103. name = res->name ?: dev_name(dev);
  104. if (!devm_request_mem_region(dev, res->start, size, name)) {
  105. dev_err(dev, "can't request region for resource %pR\n", res);
  106. return NULL;
  107. }
  108. if (res->flags & IORESOURCE_CACHEABLE)
  109. dest_ptr = devm_ioremap(dev, res->start, size);
  110. else
  111. dest_ptr = devm_ioremap_nocache(dev, res->start, size);
  112. if (!dest_ptr) {
  113. dev_err(dev, "ioremap failed for resource %pR\n", res);
  114. devm_release_mem_region(dev, res->start, size);
  115. }
  116. return dest_ptr;
  117. }
  118. EXPORT_SYMBOL(devm_request_and_ioremap);
  119. #ifdef CONFIG_HAS_IOPORT
  120. /*
  121. * Generic iomap devres
  122. */
  123. static void devm_ioport_map_release(struct device *dev, void *res)
  124. {
  125. ioport_unmap(*(void __iomem **)res);
  126. }
  127. static int devm_ioport_map_match(struct device *dev, void *res,
  128. void *match_data)
  129. {
  130. return *(void **)res == match_data;
  131. }
  132. /**
  133. * devm_ioport_map - Managed ioport_map()
  134. * @dev: Generic device to map ioport for
  135. * @port: Port to map
  136. * @nr: Number of ports to map
  137. *
  138. * Managed ioport_map(). Map is automatically unmapped on driver
  139. * detach.
  140. */
  141. void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
  142. unsigned int nr)
  143. {
  144. void __iomem **ptr, *addr;
  145. ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
  146. if (!ptr)
  147. return NULL;
  148. addr = ioport_map(port, nr);
  149. if (addr) {
  150. *ptr = addr;
  151. devres_add(dev, ptr);
  152. } else
  153. devres_free(ptr);
  154. return addr;
  155. }
  156. EXPORT_SYMBOL(devm_ioport_map);
  157. /**
  158. * devm_ioport_unmap - Managed ioport_unmap()
  159. * @dev: Generic device to unmap for
  160. * @addr: Address to unmap
  161. *
  162. * Managed ioport_unmap(). @addr must have been mapped using
  163. * devm_ioport_map().
  164. */
  165. void devm_ioport_unmap(struct device *dev, void __iomem *addr)
  166. {
  167. ioport_unmap(addr);
  168. WARN_ON(devres_destroy(dev, devm_ioport_map_release,
  169. devm_ioport_map_match, (void *)addr));
  170. }
  171. EXPORT_SYMBOL(devm_ioport_unmap);
  172. #ifdef CONFIG_PCI
  173. /*
  174. * PCI iomap devres
  175. */
  176. #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
  177. struct pcim_iomap_devres {
  178. void __iomem *table[PCIM_IOMAP_MAX];
  179. };
  180. static void pcim_iomap_release(struct device *gendev, void *res)
  181. {
  182. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  183. struct pcim_iomap_devres *this = res;
  184. int i;
  185. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  186. if (this->table[i])
  187. pci_iounmap(dev, this->table[i]);
  188. }
  189. /**
  190. * pcim_iomap_table - access iomap allocation table
  191. * @pdev: PCI device to access iomap table for
  192. *
  193. * Access iomap allocation table for @dev. If iomap table doesn't
  194. * exist and @pdev is managed, it will be allocated. All iomaps
  195. * recorded in the iomap table are automatically unmapped on driver
  196. * detach.
  197. *
  198. * This function might sleep when the table is first allocated but can
  199. * be safely called without context and guaranteed to succed once
  200. * allocated.
  201. */
  202. void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
  203. {
  204. struct pcim_iomap_devres *dr, *new_dr;
  205. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  206. if (dr)
  207. return dr->table;
  208. new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
  209. if (!new_dr)
  210. return NULL;
  211. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  212. return dr->table;
  213. }
  214. EXPORT_SYMBOL(pcim_iomap_table);
  215. /**
  216. * pcim_iomap - Managed pcim_iomap()
  217. * @pdev: PCI device to iomap for
  218. * @bar: BAR to iomap
  219. * @maxlen: Maximum length of iomap
  220. *
  221. * Managed pci_iomap(). Map is automatically unmapped on driver
  222. * detach.
  223. */
  224. void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  225. {
  226. void __iomem **tbl;
  227. BUG_ON(bar >= PCIM_IOMAP_MAX);
  228. tbl = (void __iomem **)pcim_iomap_table(pdev);
  229. if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
  230. return NULL;
  231. tbl[bar] = pci_iomap(pdev, bar, maxlen);
  232. return tbl[bar];
  233. }
  234. EXPORT_SYMBOL(pcim_iomap);
  235. /**
  236. * pcim_iounmap - Managed pci_iounmap()
  237. * @pdev: PCI device to iounmap for
  238. * @addr: Address to unmap
  239. *
  240. * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
  241. */
  242. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  243. {
  244. void __iomem **tbl;
  245. int i;
  246. pci_iounmap(pdev, addr);
  247. tbl = (void __iomem **)pcim_iomap_table(pdev);
  248. BUG_ON(!tbl);
  249. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  250. if (tbl[i] == addr) {
  251. tbl[i] = NULL;
  252. return;
  253. }
  254. WARN_ON(1);
  255. }
  256. EXPORT_SYMBOL(pcim_iounmap);
  257. /**
  258. * pcim_iomap_regions - Request and iomap PCI BARs
  259. * @pdev: PCI device to map IO resources for
  260. * @mask: Mask of BARs to request and iomap
  261. * @name: Name used when requesting regions
  262. *
  263. * Request and iomap regions specified by @mask.
  264. */
  265. int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
  266. {
  267. void __iomem * const *iomap;
  268. int i, rc;
  269. iomap = pcim_iomap_table(pdev);
  270. if (!iomap)
  271. return -ENOMEM;
  272. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  273. unsigned long len;
  274. if (!(mask & (1 << i)))
  275. continue;
  276. rc = -EINVAL;
  277. len = pci_resource_len(pdev, i);
  278. if (!len)
  279. goto err_inval;
  280. rc = pci_request_region(pdev, i, name);
  281. if (rc)
  282. goto err_inval;
  283. rc = -ENOMEM;
  284. if (!pcim_iomap(pdev, i, 0))
  285. goto err_region;
  286. }
  287. return 0;
  288. err_region:
  289. pci_release_region(pdev, i);
  290. err_inval:
  291. while (--i >= 0) {
  292. if (!(mask & (1 << i)))
  293. continue;
  294. pcim_iounmap(pdev, iomap[i]);
  295. pci_release_region(pdev, i);
  296. }
  297. return rc;
  298. }
  299. EXPORT_SYMBOL(pcim_iomap_regions);
  300. /**
  301. * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
  302. * @pdev: PCI device to map IO resources for
  303. * @mask: Mask of BARs to iomap
  304. * @name: Name used when requesting regions
  305. *
  306. * Request all PCI BARs and iomap regions specified by @mask.
  307. */
  308. int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
  309. const char *name)
  310. {
  311. int request_mask = ((1 << 6) - 1) & ~mask;
  312. int rc;
  313. rc = pci_request_selected_regions(pdev, request_mask, name);
  314. if (rc)
  315. return rc;
  316. rc = pcim_iomap_regions(pdev, mask, name);
  317. if (rc)
  318. pci_release_selected_regions(pdev, request_mask);
  319. return rc;
  320. }
  321. EXPORT_SYMBOL(pcim_iomap_regions_request_all);
  322. /**
  323. * pcim_iounmap_regions - Unmap and release PCI BARs
  324. * @pdev: PCI device to map IO resources for
  325. * @mask: Mask of BARs to unmap and release
  326. *
  327. * Unmap and release regions specified by @mask.
  328. */
  329. void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
  330. {
  331. void __iomem * const *iomap;
  332. int i;
  333. iomap = pcim_iomap_table(pdev);
  334. if (!iomap)
  335. return;
  336. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  337. if (!(mask & (1 << i)))
  338. continue;
  339. pcim_iounmap(pdev, iomap[i]);
  340. pci_release_region(pdev, i);
  341. }
  342. }
  343. EXPORT_SYMBOL(pcim_iounmap_regions);
  344. #endif /* CONFIG_PCI */
  345. #endif /* CONFIG_HAS_IOPORT */