dma-coherent.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Coherent per-device memory handling.
  3. * Borrowed from i386
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/dma-mapping.h>
  9. struct dma_coherent_mem {
  10. void *virt_base;
  11. dma_addr_t device_base;
  12. phys_addr_t pfn_base;
  13. int size;
  14. int flags;
  15. unsigned long *bitmap;
  16. };
  17. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  18. dma_addr_t device_addr, size_t size, int flags)
  19. {
  20. void __iomem *mem_base = NULL;
  21. int pages = size >> PAGE_SHIFT;
  22. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  23. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  24. goto out;
  25. if (!size)
  26. goto out;
  27. if (dev->dma_mem)
  28. goto out;
  29. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  30. mem_base = ioremap(bus_addr, size);
  31. if (!mem_base)
  32. goto out;
  33. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  34. if (!dev->dma_mem)
  35. goto out;
  36. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  37. if (!dev->dma_mem->bitmap)
  38. goto free1_out;
  39. dev->dma_mem->virt_base = mem_base;
  40. dev->dma_mem->device_base = device_addr;
  41. dev->dma_mem->pfn_base = PFN_DOWN(bus_addr);
  42. dev->dma_mem->size = pages;
  43. dev->dma_mem->flags = flags;
  44. if (flags & DMA_MEMORY_MAP)
  45. return DMA_MEMORY_MAP;
  46. return DMA_MEMORY_IO;
  47. free1_out:
  48. kfree(dev->dma_mem);
  49. out:
  50. if (mem_base)
  51. iounmap(mem_base);
  52. return 0;
  53. }
  54. EXPORT_SYMBOL(dma_declare_coherent_memory);
  55. void dma_release_declared_memory(struct device *dev)
  56. {
  57. struct dma_coherent_mem *mem = dev->dma_mem;
  58. if (!mem)
  59. return;
  60. dev->dma_mem = NULL;
  61. iounmap(mem->virt_base);
  62. kfree(mem->bitmap);
  63. kfree(mem);
  64. }
  65. EXPORT_SYMBOL(dma_release_declared_memory);
  66. void *dma_mark_declared_memory_occupied(struct device *dev,
  67. dma_addr_t device_addr, size_t size)
  68. {
  69. struct dma_coherent_mem *mem = dev->dma_mem;
  70. int pos, err;
  71. size += device_addr & ~PAGE_MASK;
  72. if (!mem)
  73. return ERR_PTR(-EINVAL);
  74. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  75. err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
  76. if (err != 0)
  77. return ERR_PTR(err);
  78. return mem->virt_base + (pos << PAGE_SHIFT);
  79. }
  80. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  81. /**
  82. * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area
  83. *
  84. * @dev: device from which we allocate memory
  85. * @size: size of requested memory area
  86. * @dma_handle: This will be filled with the correct dma handle
  87. * @ret: This pointer will be filled with the virtual address
  88. * to allocated area.
  89. *
  90. * This function should be only called from per-arch dma_alloc_coherent()
  91. * to support allocation from per-device coherent memory pools.
  92. *
  93. * Returns 0 if dma_alloc_coherent should continue with allocating from
  94. * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
  95. */
  96. int dma_alloc_from_coherent(struct device *dev, ssize_t size,
  97. dma_addr_t *dma_handle, void **ret)
  98. {
  99. struct dma_coherent_mem *mem;
  100. int order = get_order(size);
  101. int pageno;
  102. if (!dev)
  103. return 0;
  104. mem = dev->dma_mem;
  105. if (!mem)
  106. return 0;
  107. *ret = NULL;
  108. if (unlikely(size > (mem->size << PAGE_SHIFT)))
  109. goto err;
  110. pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
  111. if (unlikely(pageno < 0))
  112. goto err;
  113. /*
  114. * Memory was found in the per-device area.
  115. */
  116. *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
  117. *ret = mem->virt_base + (pageno << PAGE_SHIFT);
  118. memset(*ret, 0, size);
  119. return 1;
  120. err:
  121. /*
  122. * In the case where the allocation can not be satisfied from the
  123. * per-device area, try to fall back to generic memory if the
  124. * constraints allow it.
  125. */
  126. return mem->flags & DMA_MEMORY_EXCLUSIVE;
  127. }
  128. EXPORT_SYMBOL(dma_alloc_from_coherent);
  129. /**
  130. * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
  131. * @dev: device from which the memory was allocated
  132. * @order: the order of pages allocated
  133. * @vaddr: virtual address of allocated pages
  134. *
  135. * This checks whether the memory was allocated from the per-device
  136. * coherent memory pool and if so, releases that memory.
  137. *
  138. * Returns 1 if we correctly released the memory, or 0 if
  139. * dma_release_coherent() should proceed with releasing memory from
  140. * generic pools.
  141. */
  142. int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
  143. {
  144. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  145. if (mem && vaddr >= mem->virt_base && vaddr <
  146. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  147. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  148. bitmap_release_region(mem->bitmap, page, order);
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(dma_release_from_coherent);
  154. /**
  155. * dma_mmap_from_coherent() - try to mmap the memory allocated from
  156. * per-device coherent memory pool to userspace
  157. * @dev: device from which the memory was allocated
  158. * @vma: vm_area for the userspace memory
  159. * @vaddr: cpu address returned by dma_alloc_from_coherent
  160. * @size: size of the memory buffer allocated by dma_alloc_from_coherent
  161. *
  162. * This checks whether the memory was allocated from the per-device
  163. * coherent memory pool and if so, maps that memory to the provided vma.
  164. *
  165. * Returns 1 if we correctly mapped the memory, or 0 if
  166. * dma_release_coherent() should proceed with mapping memory from
  167. * generic pools.
  168. */
  169. int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
  170. void *vaddr, size_t size, int *ret)
  171. {
  172. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  173. if (mem && vaddr >= mem->virt_base && vaddr + size <=
  174. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  175. unsigned long off = vma->vm_pgoff;
  176. int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  177. int user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  178. int count = size >> PAGE_SHIFT;
  179. *ret = -ENXIO;
  180. if (off < count && user_count <= count - off) {
  181. unsigned pfn = mem->pfn_base + start + off;
  182. *ret = remap_pfn_range(vma, vma->vm_start, pfn,
  183. user_count << PAGE_SHIFT,
  184. vma->vm_page_prot);
  185. }
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(dma_mmap_from_coherent);