devres.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <linux/pci.h>
  2. #include <linux/io.h>
  3. #include <linux/gfp.h>
  4. #include <linux/module.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. iounmap(addr);
  72. WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
  73. (void *)addr));
  74. }
  75. EXPORT_SYMBOL(devm_iounmap);
  76. #ifdef CONFIG_HAS_IOPORT
  77. /*
  78. * Generic iomap devres
  79. */
  80. static void devm_ioport_map_release(struct device *dev, void *res)
  81. {
  82. ioport_unmap(*(void __iomem **)res);
  83. }
  84. static int devm_ioport_map_match(struct device *dev, void *res,
  85. void *match_data)
  86. {
  87. return *(void **)res == match_data;
  88. }
  89. /**
  90. * devm_ioport_map - Managed ioport_map()
  91. * @dev: Generic device to map ioport for
  92. * @port: Port to map
  93. * @nr: Number of ports to map
  94. *
  95. * Managed ioport_map(). Map is automatically unmapped on driver
  96. * detach.
  97. */
  98. void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
  99. unsigned int nr)
  100. {
  101. void __iomem **ptr, *addr;
  102. ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
  103. if (!ptr)
  104. return NULL;
  105. addr = ioport_map(port, nr);
  106. if (addr) {
  107. *ptr = addr;
  108. devres_add(dev, ptr);
  109. } else
  110. devres_free(ptr);
  111. return addr;
  112. }
  113. EXPORT_SYMBOL(devm_ioport_map);
  114. /**
  115. * devm_ioport_unmap - Managed ioport_unmap()
  116. * @dev: Generic device to unmap for
  117. * @addr: Address to unmap
  118. *
  119. * Managed ioport_unmap(). @addr must have been mapped using
  120. * devm_ioport_map().
  121. */
  122. void devm_ioport_unmap(struct device *dev, void __iomem *addr)
  123. {
  124. ioport_unmap(addr);
  125. WARN_ON(devres_destroy(dev, devm_ioport_map_release,
  126. devm_ioport_map_match, (void *)addr));
  127. }
  128. EXPORT_SYMBOL(devm_ioport_unmap);
  129. #ifdef CONFIG_PCI
  130. /*
  131. * PCI iomap devres
  132. */
  133. #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
  134. struct pcim_iomap_devres {
  135. void __iomem *table[PCIM_IOMAP_MAX];
  136. };
  137. static void pcim_iomap_release(struct device *gendev, void *res)
  138. {
  139. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  140. struct pcim_iomap_devres *this = res;
  141. int i;
  142. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  143. if (this->table[i])
  144. pci_iounmap(dev, this->table[i]);
  145. }
  146. /**
  147. * pcim_iomap_table - access iomap allocation table
  148. * @pdev: PCI device to access iomap table for
  149. *
  150. * Access iomap allocation table for @dev. If iomap table doesn't
  151. * exist and @pdev is managed, it will be allocated. All iomaps
  152. * recorded in the iomap table are automatically unmapped on driver
  153. * detach.
  154. *
  155. * This function might sleep when the table is first allocated but can
  156. * be safely called without context and guaranteed to succed once
  157. * allocated.
  158. */
  159. void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
  160. {
  161. struct pcim_iomap_devres *dr, *new_dr;
  162. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  163. if (dr)
  164. return dr->table;
  165. new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
  166. if (!new_dr)
  167. return NULL;
  168. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  169. return dr->table;
  170. }
  171. EXPORT_SYMBOL(pcim_iomap_table);
  172. /**
  173. * pcim_iomap - Managed pcim_iomap()
  174. * @pdev: PCI device to iomap for
  175. * @bar: BAR to iomap
  176. * @maxlen: Maximum length of iomap
  177. *
  178. * Managed pci_iomap(). Map is automatically unmapped on driver
  179. * detach.
  180. */
  181. void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  182. {
  183. void __iomem **tbl;
  184. BUG_ON(bar >= PCIM_IOMAP_MAX);
  185. tbl = (void __iomem **)pcim_iomap_table(pdev);
  186. if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
  187. return NULL;
  188. tbl[bar] = pci_iomap(pdev, bar, maxlen);
  189. return tbl[bar];
  190. }
  191. EXPORT_SYMBOL(pcim_iomap);
  192. /**
  193. * pcim_iounmap - Managed pci_iounmap()
  194. * @pdev: PCI device to iounmap for
  195. * @addr: Address to unmap
  196. *
  197. * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
  198. */
  199. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  200. {
  201. void __iomem **tbl;
  202. int i;
  203. pci_iounmap(pdev, addr);
  204. tbl = (void __iomem **)pcim_iomap_table(pdev);
  205. BUG_ON(!tbl);
  206. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  207. if (tbl[i] == addr) {
  208. tbl[i] = NULL;
  209. return;
  210. }
  211. WARN_ON(1);
  212. }
  213. EXPORT_SYMBOL(pcim_iounmap);
  214. /**
  215. * pcim_iomap_regions - Request and iomap PCI BARs
  216. * @pdev: PCI device to map IO resources for
  217. * @mask: Mask of BARs to request and iomap
  218. * @name: Name used when requesting regions
  219. *
  220. * Request and iomap regions specified by @mask.
  221. */
  222. int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name)
  223. {
  224. void __iomem * const *iomap;
  225. int i, rc;
  226. iomap = pcim_iomap_table(pdev);
  227. if (!iomap)
  228. return -ENOMEM;
  229. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  230. unsigned long len;
  231. if (!(mask & (1 << i)))
  232. continue;
  233. rc = -EINVAL;
  234. len = pci_resource_len(pdev, i);
  235. if (!len)
  236. goto err_inval;
  237. rc = pci_request_region(pdev, i, name);
  238. if (rc)
  239. goto err_inval;
  240. rc = -ENOMEM;
  241. if (!pcim_iomap(pdev, i, 0))
  242. goto err_region;
  243. }
  244. return 0;
  245. err_region:
  246. pci_release_region(pdev, i);
  247. err_inval:
  248. while (--i >= 0) {
  249. if (!(mask & (1 << i)))
  250. continue;
  251. pcim_iounmap(pdev, iomap[i]);
  252. pci_release_region(pdev, i);
  253. }
  254. return rc;
  255. }
  256. EXPORT_SYMBOL(pcim_iomap_regions);
  257. /**
  258. * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
  259. * @pdev: PCI device to map IO resources for
  260. * @mask: Mask of BARs to iomap
  261. * @name: Name used when requesting regions
  262. *
  263. * Request all PCI BARs and iomap regions specified by @mask.
  264. */
  265. int pcim_iomap_regions_request_all(struct pci_dev *pdev, u16 mask,
  266. const char *name)
  267. {
  268. int request_mask = ((1 << 6) - 1) & ~mask;
  269. int rc;
  270. rc = pci_request_selected_regions(pdev, request_mask, name);
  271. if (rc)
  272. return rc;
  273. rc = pcim_iomap_regions(pdev, mask, name);
  274. if (rc)
  275. pci_release_selected_regions(pdev, request_mask);
  276. return rc;
  277. }
  278. EXPORT_SYMBOL(pcim_iomap_regions_request_all);
  279. /**
  280. * pcim_iounmap_regions - Unmap and release PCI BARs
  281. * @pdev: PCI device to map IO resources for
  282. * @mask: Mask of BARs to unmap and release
  283. *
  284. * Unmap and release regions specified by @mask.
  285. */
  286. void pcim_iounmap_regions(struct pci_dev *pdev, u16 mask)
  287. {
  288. void __iomem * const *iomap;
  289. int i;
  290. iomap = pcim_iomap_table(pdev);
  291. if (!iomap)
  292. return;
  293. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  294. if (!(mask & (1 << i)))
  295. continue;
  296. pcim_iounmap(pdev, iomap[i]);
  297. pci_release_region(pdev, i);
  298. }
  299. }
  300. EXPORT_SYMBOL(pcim_iounmap_regions);
  301. #endif
  302. #endif