devres.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <linux/pci.h>
  2. #include <linux/io.h>
  3. #include <linux/module.h>
  4. static void devm_ioremap_release(struct device *dev, void *res)
  5. {
  6. iounmap(*(void __iomem **)res);
  7. }
  8. static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
  9. {
  10. return *(void **)res == match_data;
  11. }
  12. /**
  13. * devm_ioremap - Managed ioremap()
  14. * @dev: Generic device to remap IO address for
  15. * @offset: BUS offset to map
  16. * @size: Size of map
  17. *
  18. * Managed ioremap(). Map is automatically unmapped on driver detach.
  19. */
  20. void __iomem *devm_ioremap(struct device *dev, unsigned long offset,
  21. unsigned long size)
  22. {
  23. void __iomem **ptr, *addr;
  24. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  25. if (!ptr)
  26. return NULL;
  27. addr = ioremap(offset, size);
  28. if (addr) {
  29. *ptr = addr;
  30. devres_add(dev, ptr);
  31. } else
  32. devres_free(ptr);
  33. return addr;
  34. }
  35. EXPORT_SYMBOL(devm_ioremap);
  36. /**
  37. * devm_ioremap_nocache - Managed ioremap_nocache()
  38. * @dev: Generic device to remap IO address for
  39. * @offset: BUS offset to map
  40. * @size: Size of map
  41. *
  42. * Managed ioremap_nocache(). Map is automatically unmapped on driver
  43. * detach.
  44. */
  45. void __iomem *devm_ioremap_nocache(struct device *dev, unsigned long offset,
  46. unsigned long size)
  47. {
  48. void __iomem **ptr, *addr;
  49. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  50. if (!ptr)
  51. return NULL;
  52. addr = ioremap_nocache(offset, size);
  53. if (addr) {
  54. *ptr = addr;
  55. devres_add(dev, ptr);
  56. } else
  57. devres_free(ptr);
  58. return addr;
  59. }
  60. EXPORT_SYMBOL(devm_ioremap_nocache);
  61. /**
  62. * devm_iounmap - Managed iounmap()
  63. * @dev: Generic device to unmap for
  64. * @addr: Address to unmap
  65. *
  66. * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
  67. */
  68. void devm_iounmap(struct device *dev, void __iomem *addr)
  69. {
  70. iounmap(addr);
  71. WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
  72. (void *)addr));
  73. }
  74. EXPORT_SYMBOL(devm_iounmap);
  75. #ifdef CONFIG_HAS_IOPORT
  76. /*
  77. * Generic iomap devres
  78. */
  79. static void devm_ioport_map_release(struct device *dev, void *res)
  80. {
  81. ioport_unmap(*(void __iomem **)res);
  82. }
  83. static int devm_ioport_map_match(struct device *dev, void *res,
  84. void *match_data)
  85. {
  86. return *(void **)res == match_data;
  87. }
  88. /**
  89. * devm_ioport_map - Managed ioport_map()
  90. * @dev: Generic device to map ioport for
  91. * @port: Port to map
  92. * @nr: Number of ports to map
  93. *
  94. * Managed ioport_map(). Map is automatically unmapped on driver
  95. * detach.
  96. */
  97. void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
  98. unsigned int nr)
  99. {
  100. void __iomem **ptr, *addr;
  101. ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
  102. if (!ptr)
  103. return NULL;
  104. addr = ioport_map(port, nr);
  105. if (addr) {
  106. *ptr = addr;
  107. devres_add(dev, ptr);
  108. } else
  109. devres_free(ptr);
  110. return addr;
  111. }
  112. EXPORT_SYMBOL(devm_ioport_map);
  113. /**
  114. * devm_ioport_unmap - Managed ioport_unmap()
  115. * @dev: Generic device to unmap for
  116. * @addr: Address to unmap
  117. *
  118. * Managed ioport_unmap(). @addr must have been mapped using
  119. * devm_ioport_map().
  120. */
  121. void devm_ioport_unmap(struct device *dev, void __iomem *addr)
  122. {
  123. ioport_unmap(addr);
  124. WARN_ON(devres_destroy(dev, devm_ioport_map_release,
  125. devm_ioport_map_match, (void *)addr));
  126. }
  127. EXPORT_SYMBOL(devm_ioport_unmap);
  128. #ifdef CONFIG_PCI
  129. /*
  130. * PCI iomap devres
  131. */
  132. #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
  133. struct pcim_iomap_devres {
  134. void __iomem *table[PCIM_IOMAP_MAX];
  135. };
  136. static void pcim_iomap_release(struct device *gendev, void *res)
  137. {
  138. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  139. struct pcim_iomap_devres *this = res;
  140. int i;
  141. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  142. if (this->table[i])
  143. pci_iounmap(dev, this->table[i]);
  144. }
  145. /**
  146. * pcim_iomap_table - access iomap allocation table
  147. * @pdev: PCI device to access iomap table for
  148. *
  149. * Access iomap allocation table for @dev. If iomap table doesn't
  150. * exist and @pdev is managed, it will be allocated. All iomaps
  151. * recorded in the iomap table are automatically unmapped on driver
  152. * detach.
  153. *
  154. * This function might sleep when the table is first allocated but can
  155. * be safely called without context and guaranteed to succed once
  156. * allocated.
  157. */
  158. void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
  159. {
  160. struct pcim_iomap_devres *dr, *new_dr;
  161. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  162. if (dr)
  163. return dr->table;
  164. new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
  165. if (!new_dr)
  166. return NULL;
  167. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  168. return dr->table;
  169. }
  170. EXPORT_SYMBOL(pcim_iomap_table);
  171. /**
  172. * pcim_iomap - Managed pcim_iomap()
  173. * @pdev: PCI device to iomap for
  174. * @bar: BAR to iomap
  175. * @maxlen: Maximum length of iomap
  176. *
  177. * Managed pci_iomap(). Map is automatically unmapped on driver
  178. * detach.
  179. */
  180. void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  181. {
  182. void __iomem **tbl;
  183. BUG_ON(bar >= PCIM_IOMAP_MAX);
  184. tbl = (void __iomem **)pcim_iomap_table(pdev);
  185. if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
  186. return NULL;
  187. tbl[bar] = pci_iomap(pdev, bar, maxlen);
  188. return tbl[bar];
  189. }
  190. EXPORT_SYMBOL(pcim_iomap);
  191. /**
  192. * pcim_iounmap - Managed pci_iounmap()
  193. * @pdev: PCI device to iounmap for
  194. * @addr: Address to unmap
  195. *
  196. * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
  197. */
  198. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  199. {
  200. void __iomem **tbl;
  201. int i;
  202. pci_iounmap(pdev, addr);
  203. tbl = (void __iomem **)pcim_iomap_table(pdev);
  204. BUG_ON(!tbl);
  205. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  206. if (tbl[i] == addr) {
  207. tbl[i] = NULL;
  208. return;
  209. }
  210. WARN_ON(1);
  211. }
  212. EXPORT_SYMBOL(pcim_iounmap);
  213. /**
  214. * pcim_iomap_regions - Request and iomap PCI BARs
  215. * @pdev: PCI device to map IO resources for
  216. * @mask: Mask of BARs to request and iomap
  217. * @name: Name used when requesting regions
  218. *
  219. * Request and iomap regions specified by @mask.
  220. */
  221. int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name)
  222. {
  223. void __iomem * const *iomap;
  224. int i, rc;
  225. iomap = pcim_iomap_table(pdev);
  226. if (!iomap)
  227. return -ENOMEM;
  228. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  229. unsigned long len;
  230. if (!(mask & (1 << i)))
  231. continue;
  232. rc = -EINVAL;
  233. len = pci_resource_len(pdev, i);
  234. if (!len)
  235. goto err_inval;
  236. rc = pci_request_region(pdev, i, name);
  237. if (rc)
  238. goto err_inval;
  239. rc = -ENOMEM;
  240. if (!pcim_iomap(pdev, i, 0))
  241. goto err_region;
  242. }
  243. return 0;
  244. err_region:
  245. pci_release_region(pdev, i);
  246. err_inval:
  247. while (--i >= 0) {
  248. if (!(mask & (1 << i)))
  249. continue;
  250. pcim_iounmap(pdev, iomap[i]);
  251. pci_release_region(pdev, i);
  252. }
  253. return rc;
  254. }
  255. EXPORT_SYMBOL(pcim_iomap_regions);
  256. #endif
  257. #endif