dma-mapping.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
  3. *
  4. * Copyright (c) 2006 SUSE Linux Products GmbH
  5. * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/export.h>
  11. #include <linux/gfp.h>
  12. /*
  13. * Managed DMA API
  14. */
  15. struct dma_devres {
  16. size_t size;
  17. void *vaddr;
  18. dma_addr_t dma_handle;
  19. };
  20. static void dmam_coherent_release(struct device *dev, void *res)
  21. {
  22. struct dma_devres *this = res;
  23. dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  24. }
  25. static void dmam_noncoherent_release(struct device *dev, void *res)
  26. {
  27. struct dma_devres *this = res;
  28. dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
  29. }
  30. static int dmam_match(struct device *dev, void *res, void *match_data)
  31. {
  32. struct dma_devres *this = res, *match = match_data;
  33. if (this->vaddr == match->vaddr) {
  34. WARN_ON(this->size != match->size ||
  35. this->dma_handle != match->dma_handle);
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. /**
  41. * dmam_alloc_coherent - Managed dma_alloc_coherent()
  42. * @dev: Device to allocate coherent memory for
  43. * @size: Size of allocation
  44. * @dma_handle: Out argument for allocated DMA handle
  45. * @gfp: Allocation flags
  46. *
  47. * Managed dma_alloc_coherent(). Memory allocated using this function
  48. * will be automatically released on driver detach.
  49. *
  50. * RETURNS:
  51. * Pointer to allocated memory on success, NULL on failure.
  52. */
  53. void * dmam_alloc_coherent(struct device *dev, size_t size,
  54. dma_addr_t *dma_handle, gfp_t gfp)
  55. {
  56. struct dma_devres *dr;
  57. void *vaddr;
  58. dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
  59. if (!dr)
  60. return NULL;
  61. vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
  62. if (!vaddr) {
  63. devres_free(dr);
  64. return NULL;
  65. }
  66. dr->vaddr = vaddr;
  67. dr->dma_handle = *dma_handle;
  68. dr->size = size;
  69. devres_add(dev, dr);
  70. return vaddr;
  71. }
  72. EXPORT_SYMBOL(dmam_alloc_coherent);
  73. /**
  74. * dmam_free_coherent - Managed dma_free_coherent()
  75. * @dev: Device to free coherent memory for
  76. * @size: Size of allocation
  77. * @vaddr: Virtual address of the memory to free
  78. * @dma_handle: DMA handle of the memory to free
  79. *
  80. * Managed dma_free_coherent().
  81. */
  82. void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  83. dma_addr_t dma_handle)
  84. {
  85. struct dma_devres match_data = { size, vaddr, dma_handle };
  86. dma_free_coherent(dev, size, vaddr, dma_handle);
  87. WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
  88. &match_data));
  89. }
  90. EXPORT_SYMBOL(dmam_free_coherent);
  91. /**
  92. * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
  93. * @dev: Device to allocate non_coherent memory for
  94. * @size: Size of allocation
  95. * @dma_handle: Out argument for allocated DMA handle
  96. * @gfp: Allocation flags
  97. *
  98. * Managed dma_alloc_non_coherent(). Memory allocated using this
  99. * function will be automatically released on driver detach.
  100. *
  101. * RETURNS:
  102. * Pointer to allocated memory on success, NULL on failure.
  103. */
  104. void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  105. dma_addr_t *dma_handle, gfp_t gfp)
  106. {
  107. struct dma_devres *dr;
  108. void *vaddr;
  109. dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
  110. if (!dr)
  111. return NULL;
  112. vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
  113. if (!vaddr) {
  114. devres_free(dr);
  115. return NULL;
  116. }
  117. dr->vaddr = vaddr;
  118. dr->dma_handle = *dma_handle;
  119. dr->size = size;
  120. devres_add(dev, dr);
  121. return vaddr;
  122. }
  123. EXPORT_SYMBOL(dmam_alloc_noncoherent);
  124. /**
  125. * dmam_free_coherent - Managed dma_free_noncoherent()
  126. * @dev: Device to free noncoherent memory for
  127. * @size: Size of allocation
  128. * @vaddr: Virtual address of the memory to free
  129. * @dma_handle: DMA handle of the memory to free
  130. *
  131. * Managed dma_free_noncoherent().
  132. */
  133. void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  134. dma_addr_t dma_handle)
  135. {
  136. struct dma_devres match_data = { size, vaddr, dma_handle };
  137. dma_free_noncoherent(dev, size, vaddr, dma_handle);
  138. WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
  139. &match_data));
  140. }
  141. EXPORT_SYMBOL(dmam_free_noncoherent);
  142. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  143. static void dmam_coherent_decl_release(struct device *dev, void *res)
  144. {
  145. dma_release_declared_memory(dev);
  146. }
  147. /**
  148. * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
  149. * @dev: Device to declare coherent memory for
  150. * @bus_addr: Bus address of coherent memory to be declared
  151. * @device_addr: Device address of coherent memory to be declared
  152. * @size: Size of coherent memory to be declared
  153. * @flags: Flags
  154. *
  155. * Managed dma_declare_coherent_memory().
  156. *
  157. * RETURNS:
  158. * 0 on success, -errno on failure.
  159. */
  160. int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  161. dma_addr_t device_addr, size_t size, int flags)
  162. {
  163. void *res;
  164. int rc;
  165. res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
  166. if (!res)
  167. return -ENOMEM;
  168. rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size,
  169. flags);
  170. if (rc == 0)
  171. devres_add(dev, res);
  172. else
  173. devres_free(res);
  174. return rc;
  175. }
  176. EXPORT_SYMBOL(dmam_declare_coherent_memory);
  177. /**
  178. * dmam_release_declared_memory - Managed dma_release_declared_memory().
  179. * @dev: Device to release declared coherent memory for
  180. *
  181. * Managed dmam_release_declared_memory().
  182. */
  183. void dmam_release_declared_memory(struct device *dev)
  184. {
  185. WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
  186. }
  187. EXPORT_SYMBOL(dmam_release_declared_memory);
  188. #endif