dma-mapping.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. /*
  15. * Managed DMA API
  16. */
  17. struct dma_devres {
  18. size_t size;
  19. void *vaddr;
  20. dma_addr_t dma_handle;
  21. };
  22. static void dmam_coherent_release(struct device *dev, void *res)
  23. {
  24. struct dma_devres *this = res;
  25. dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  26. }
  27. static void dmam_noncoherent_release(struct device *dev, void *res)
  28. {
  29. struct dma_devres *this = res;
  30. dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
  31. }
  32. static int dmam_match(struct device *dev, void *res, void *match_data)
  33. {
  34. struct dma_devres *this = res, *match = match_data;
  35. if (this->vaddr == match->vaddr) {
  36. WARN_ON(this->size != match->size ||
  37. this->dma_handle != match->dma_handle);
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. /**
  43. * dmam_alloc_coherent - Managed dma_alloc_coherent()
  44. * @dev: Device to allocate coherent memory for
  45. * @size: Size of allocation
  46. * @dma_handle: Out argument for allocated DMA handle
  47. * @gfp: Allocation flags
  48. *
  49. * Managed dma_alloc_coherent(). Memory allocated using this function
  50. * will be automatically released on driver detach.
  51. *
  52. * RETURNS:
  53. * Pointer to allocated memory on success, NULL on failure.
  54. */
  55. void *dmam_alloc_coherent(struct device *dev, size_t size,
  56. dma_addr_t *dma_handle, gfp_t gfp)
  57. {
  58. struct dma_devres *dr;
  59. void *vaddr;
  60. dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
  61. if (!dr)
  62. return NULL;
  63. vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
  64. if (!vaddr) {
  65. devres_free(dr);
  66. return NULL;
  67. }
  68. dr->vaddr = vaddr;
  69. dr->dma_handle = *dma_handle;
  70. dr->size = size;
  71. devres_add(dev, dr);
  72. return vaddr;
  73. }
  74. EXPORT_SYMBOL(dmam_alloc_coherent);
  75. /**
  76. * dmam_free_coherent - Managed dma_free_coherent()
  77. * @dev: Device to free coherent memory for
  78. * @size: Size of allocation
  79. * @vaddr: Virtual address of the memory to free
  80. * @dma_handle: DMA handle of the memory to free
  81. *
  82. * Managed dma_free_coherent().
  83. */
  84. void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  85. dma_addr_t dma_handle)
  86. {
  87. struct dma_devres match_data = { size, vaddr, dma_handle };
  88. dma_free_coherent(dev, size, vaddr, dma_handle);
  89. WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
  90. &match_data));
  91. }
  92. EXPORT_SYMBOL(dmam_free_coherent);
  93. /**
  94. * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
  95. * @dev: Device to allocate non_coherent memory for
  96. * @size: Size of allocation
  97. * @dma_handle: Out argument for allocated DMA handle
  98. * @gfp: Allocation flags
  99. *
  100. * Managed dma_alloc_non_coherent(). Memory allocated using this
  101. * function will be automatically released on driver detach.
  102. *
  103. * RETURNS:
  104. * Pointer to allocated memory on success, NULL on failure.
  105. */
  106. void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  107. dma_addr_t *dma_handle, gfp_t gfp)
  108. {
  109. struct dma_devres *dr;
  110. void *vaddr;
  111. dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
  112. if (!dr)
  113. return NULL;
  114. vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
  115. if (!vaddr) {
  116. devres_free(dr);
  117. return NULL;
  118. }
  119. dr->vaddr = vaddr;
  120. dr->dma_handle = *dma_handle;
  121. dr->size = size;
  122. devres_add(dev, dr);
  123. return vaddr;
  124. }
  125. EXPORT_SYMBOL(dmam_alloc_noncoherent);
  126. /**
  127. * dmam_free_coherent - Managed dma_free_noncoherent()
  128. * @dev: Device to free noncoherent memory for
  129. * @size: Size of allocation
  130. * @vaddr: Virtual address of the memory to free
  131. * @dma_handle: DMA handle of the memory to free
  132. *
  133. * Managed dma_free_noncoherent().
  134. */
  135. void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  136. dma_addr_t dma_handle)
  137. {
  138. struct dma_devres match_data = { size, vaddr, dma_handle };
  139. dma_free_noncoherent(dev, size, vaddr, dma_handle);
  140. WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
  141. &match_data));
  142. }
  143. EXPORT_SYMBOL(dmam_free_noncoherent);
  144. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  145. static void dmam_coherent_decl_release(struct device *dev, void *res)
  146. {
  147. dma_release_declared_memory(dev);
  148. }
  149. /**
  150. * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
  151. * @dev: Device to declare coherent memory for
  152. * @phys_addr: Physical address of coherent memory to be declared
  153. * @device_addr: Device address of coherent memory to be declared
  154. * @size: Size of coherent memory to be declared
  155. * @flags: Flags
  156. *
  157. * Managed dma_declare_coherent_memory().
  158. *
  159. * RETURNS:
  160. * 0 on success, -errno on failure.
  161. */
  162. int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  163. dma_addr_t device_addr, size_t size, int flags)
  164. {
  165. void *res;
  166. int rc;
  167. res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
  168. if (!res)
  169. return -ENOMEM;
  170. rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
  171. flags);
  172. if (rc) {
  173. devres_add(dev, res);
  174. rc = 0;
  175. } else {
  176. devres_free(res);
  177. rc = -ENOMEM;
  178. }
  179. return rc;
  180. }
  181. EXPORT_SYMBOL(dmam_declare_coherent_memory);
  182. /**
  183. * dmam_release_declared_memory - Managed dma_release_declared_memory().
  184. * @dev: Device to release declared coherent memory for
  185. *
  186. * Managed dmam_release_declared_memory().
  187. */
  188. void dmam_release_declared_memory(struct device *dev)
  189. {
  190. WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
  191. }
  192. EXPORT_SYMBOL(dmam_release_declared_memory);
  193. #endif
  194. /*
  195. * Create scatter-list for the already allocated DMA buffer.
  196. */
  197. int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  198. void *cpu_addr, dma_addr_t handle, size_t size)
  199. {
  200. struct page *page = virt_to_page(cpu_addr);
  201. int ret;
  202. ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
  203. if (unlikely(ret))
  204. return ret;
  205. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(dma_common_get_sgtable);
  209. /*
  210. * Create userspace mapping for the DMA-coherent memory.
  211. */
  212. int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  213. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  214. {
  215. int ret = -ENXIO;
  216. #if defined(CONFIG_MMU) && !defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP)
  217. unsigned long user_count = vma_pages(vma);
  218. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  219. unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
  220. unsigned long off = vma->vm_pgoff;
  221. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  222. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  223. return ret;
  224. if (off < count && user_count <= (count - off)) {
  225. ret = remap_pfn_range(vma, vma->vm_start,
  226. pfn + off,
  227. user_count << PAGE_SHIFT,
  228. vma->vm_page_prot);
  229. }
  230. #endif /* CONFIG_MMU && !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
  231. return ret;
  232. }
  233. EXPORT_SYMBOL(dma_common_mmap);
  234. #ifdef CONFIG_MMU
  235. /*
  236. * remaps an array of PAGE_SIZE pages into another vm_area
  237. * Cannot be used in non-sleeping contexts
  238. */
  239. void *dma_common_pages_remap(struct page **pages, size_t size,
  240. unsigned long vm_flags, pgprot_t prot,
  241. const void *caller)
  242. {
  243. struct vm_struct *area;
  244. area = get_vm_area_caller(size, vm_flags, caller);
  245. if (!area)
  246. return NULL;
  247. area->pages = pages;
  248. if (map_vm_area(area, prot, pages)) {
  249. vunmap(area->addr);
  250. return NULL;
  251. }
  252. return area->addr;
  253. }
  254. /*
  255. * remaps an allocated contiguous region into another vm_area.
  256. * Cannot be used in non-sleeping contexts
  257. */
  258. void *dma_common_contiguous_remap(struct page *page, size_t size,
  259. unsigned long vm_flags,
  260. pgprot_t prot, const void *caller)
  261. {
  262. int i;
  263. struct page **pages;
  264. void *ptr;
  265. unsigned long pfn;
  266. pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
  267. if (!pages)
  268. return NULL;
  269. for (i = 0, pfn = page_to_pfn(page); i < (size >> PAGE_SHIFT); i++)
  270. pages[i] = pfn_to_page(pfn + i);
  271. ptr = dma_common_pages_remap(pages, size, vm_flags, prot, caller);
  272. kfree(pages);
  273. return ptr;
  274. }
  275. /*
  276. * unmaps a range previously mapped by dma_common_*_remap
  277. */
  278. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
  279. {
  280. struct vm_struct *area = find_vm_area(cpu_addr);
  281. if (!area || (area->flags & vm_flags) != vm_flags) {
  282. WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
  283. return;
  284. }
  285. unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
  286. vunmap(cpu_addr);
  287. }
  288. #endif