dma-mapping.c 5.3 KB

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