dma-mapping.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include <asm-generic/dma-coherent.h>
  13. /*
  14. * Managed DMA API
  15. */
  16. struct dma_devres {
  17. size_t size;
  18. void *vaddr;
  19. dma_addr_t dma_handle;
  20. };
  21. static void dmam_coherent_release(struct device *dev, void *res)
  22. {
  23. struct dma_devres *this = res;
  24. dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  25. }
  26. static void dmam_noncoherent_release(struct device *dev, void *res)
  27. {
  28. struct dma_devres *this = res;
  29. dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
  30. }
  31. static int dmam_match(struct device *dev, void *res, void *match_data)
  32. {
  33. struct dma_devres *this = res, *match = match_data;
  34. if (this->vaddr == match->vaddr) {
  35. WARN_ON(this->size != match->size ||
  36. this->dma_handle != match->dma_handle);
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * dmam_alloc_coherent - Managed dma_alloc_coherent()
  43. * @dev: Device to allocate coherent memory for
  44. * @size: Size of allocation
  45. * @dma_handle: Out argument for allocated DMA handle
  46. * @gfp: Allocation flags
  47. *
  48. * Managed dma_alloc_coherent(). Memory allocated using this function
  49. * will be automatically released on driver detach.
  50. *
  51. * RETURNS:
  52. * Pointer to allocated memory on success, NULL on failure.
  53. */
  54. void * dmam_alloc_coherent(struct device *dev, size_t size,
  55. dma_addr_t *dma_handle, gfp_t gfp)
  56. {
  57. struct dma_devres *dr;
  58. void *vaddr;
  59. dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
  60. if (!dr)
  61. return NULL;
  62. vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
  63. if (!vaddr) {
  64. devres_free(dr);
  65. return NULL;
  66. }
  67. dr->vaddr = vaddr;
  68. dr->dma_handle = *dma_handle;
  69. dr->size = size;
  70. devres_add(dev, dr);
  71. return vaddr;
  72. }
  73. EXPORT_SYMBOL(dmam_alloc_coherent);
  74. /**
  75. * dmam_free_coherent - Managed dma_free_coherent()
  76. * @dev: Device to free coherent memory for
  77. * @size: Size of allocation
  78. * @vaddr: Virtual address of the memory to free
  79. * @dma_handle: DMA handle of the memory to free
  80. *
  81. * Managed dma_free_coherent().
  82. */
  83. void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  84. dma_addr_t dma_handle)
  85. {
  86. struct dma_devres match_data = { size, vaddr, dma_handle };
  87. dma_free_coherent(dev, size, vaddr, dma_handle);
  88. WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
  89. &match_data));
  90. }
  91. EXPORT_SYMBOL(dmam_free_coherent);
  92. /**
  93. * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
  94. * @dev: Device to allocate non_coherent memory for
  95. * @size: Size of allocation
  96. * @dma_handle: Out argument for allocated DMA handle
  97. * @gfp: Allocation flags
  98. *
  99. * Managed dma_alloc_non_coherent(). Memory allocated using this
  100. * function will be automatically released on driver detach.
  101. *
  102. * RETURNS:
  103. * Pointer to allocated memory on success, NULL on failure.
  104. */
  105. void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  106. dma_addr_t *dma_handle, gfp_t gfp)
  107. {
  108. struct dma_devres *dr;
  109. void *vaddr;
  110. dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
  111. if (!dr)
  112. return NULL;
  113. vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
  114. if (!vaddr) {
  115. devres_free(dr);
  116. return NULL;
  117. }
  118. dr->vaddr = vaddr;
  119. dr->dma_handle = *dma_handle;
  120. dr->size = size;
  121. devres_add(dev, dr);
  122. return vaddr;
  123. }
  124. EXPORT_SYMBOL(dmam_alloc_noncoherent);
  125. /**
  126. * dmam_free_coherent - Managed dma_free_noncoherent()
  127. * @dev: Device to free noncoherent memory for
  128. * @size: Size of allocation
  129. * @vaddr: Virtual address of the memory to free
  130. * @dma_handle: DMA handle of the memory to free
  131. *
  132. * Managed dma_free_noncoherent().
  133. */
  134. void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  135. dma_addr_t dma_handle)
  136. {
  137. struct dma_devres match_data = { size, vaddr, dma_handle };
  138. dma_free_noncoherent(dev, size, vaddr, dma_handle);
  139. WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
  140. &match_data));
  141. }
  142. EXPORT_SYMBOL(dmam_free_noncoherent);
  143. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  144. static void dmam_coherent_decl_release(struct device *dev, void *res)
  145. {
  146. dma_release_declared_memory(dev);
  147. }
  148. /**
  149. * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
  150. * @dev: Device to declare coherent memory for
  151. * @bus_addr: Bus address of coherent memory to be declared
  152. * @device_addr: Device address of coherent memory to be declared
  153. * @size: Size of coherent memory to be declared
  154. * @flags: Flags
  155. *
  156. * Managed dma_declare_coherent_memory().
  157. *
  158. * RETURNS:
  159. * 0 on success, -errno on failure.
  160. */
  161. int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  162. dma_addr_t device_addr, size_t size, int flags)
  163. {
  164. void *res;
  165. int rc;
  166. res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
  167. if (!res)
  168. return -ENOMEM;
  169. rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size,
  170. flags);
  171. if (rc == 0)
  172. devres_add(dev, res);
  173. else
  174. devres_free(res);
  175. return rc;
  176. }
  177. EXPORT_SYMBOL(dmam_declare_coherent_memory);
  178. /**
  179. * dmam_release_declared_memory - Managed dma_release_declared_memory().
  180. * @dev: Device to release declared coherent memory for
  181. *
  182. * Managed dmam_release_declared_memory().
  183. */
  184. void dmam_release_declared_memory(struct device *dev)
  185. {
  186. WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
  187. }
  188. EXPORT_SYMBOL(dmam_release_declared_memory);
  189. #endif
  190. /*
  191. * Create scatter-list for the already allocated DMA buffer.
  192. */
  193. int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  194. void *cpu_addr, dma_addr_t handle, size_t size)
  195. {
  196. struct page *page = virt_to_page(cpu_addr);
  197. int ret;
  198. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  199. if (unlikely(ret))
  200. return ret;
  201. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(dma_common_get_sgtable);
  205. /*
  206. * Create userspace mapping for the DMA-coherent memory.
  207. */
  208. int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  209. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  210. {
  211. int ret = -ENXIO;
  212. #ifdef CONFIG_MMU
  213. unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  214. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  215. unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
  216. unsigned long off = vma->vm_pgoff;
  217. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  218. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  219. return ret;
  220. if (off < count && user_count <= (count - off)) {
  221. ret = remap_pfn_range(vma, vma->vm_start,
  222. pfn + off,
  223. user_count << PAGE_SHIFT,
  224. vma->vm_page_prot);
  225. }
  226. #endif /* CONFIG_MMU */
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(dma_common_mmap);