dma-coherent.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/dma-mapping.h>
  8. struct dma_coherent_mem {
  9. void *virt_base;
  10. dma_addr_t device_base;
  11. int size;
  12. int flags;
  13. unsigned long *bitmap;
  14. };
  15. int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  16. dma_addr_t device_addr, size_t size, int flags)
  17. {
  18. void __iomem *mem_base = NULL;
  19. int pages = size >> PAGE_SHIFT;
  20. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  21. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  22. goto out;
  23. if (!size)
  24. goto out;
  25. if (dev->dma_mem)
  26. goto out;
  27. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  28. mem_base = ioremap(bus_addr, size);
  29. if (!mem_base)
  30. goto out;
  31. dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  32. if (!dev->dma_mem)
  33. goto out;
  34. dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  35. if (!dev->dma_mem->bitmap)
  36. goto free1_out;
  37. dev->dma_mem->virt_base = mem_base;
  38. dev->dma_mem->device_base = device_addr;
  39. dev->dma_mem->size = pages;
  40. dev->dma_mem->flags = flags;
  41. if (flags & DMA_MEMORY_MAP)
  42. return DMA_MEMORY_MAP;
  43. return DMA_MEMORY_IO;
  44. free1_out:
  45. kfree(dev->dma_mem);
  46. out:
  47. if (mem_base)
  48. iounmap(mem_base);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(dma_declare_coherent_memory);
  52. void dma_release_declared_memory(struct device *dev)
  53. {
  54. struct dma_coherent_mem *mem = dev->dma_mem;
  55. if (!mem)
  56. return;
  57. dev->dma_mem = NULL;
  58. iounmap(mem->virt_base);
  59. kfree(mem->bitmap);
  60. kfree(mem);
  61. }
  62. EXPORT_SYMBOL(dma_release_declared_memory);
  63. void *dma_mark_declared_memory_occupied(struct device *dev,
  64. dma_addr_t device_addr, size_t size)
  65. {
  66. struct dma_coherent_mem *mem = dev->dma_mem;
  67. int pos, err;
  68. size += device_addr & ~PAGE_MASK;
  69. if (!mem)
  70. return ERR_PTR(-EINVAL);
  71. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  72. err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
  73. if (err != 0)
  74. return ERR_PTR(err);
  75. return mem->virt_base + (pos << PAGE_SHIFT);
  76. }
  77. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  78. /**
  79. * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area
  80. *
  81. * @dev: device from which we allocate memory
  82. * @size: size of requested memory area
  83. * @dma_handle: This will be filled with the correct dma handle
  84. * @ret: This pointer will be filled with the virtual address
  85. * to allocated area.
  86. *
  87. * This function should be only called from per-arch dma_alloc_coherent()
  88. * to support allocation from per-device coherent memory pools.
  89. *
  90. * Returns 0 if dma_alloc_coherent should continue with allocating from
  91. * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
  92. */
  93. int dma_alloc_from_coherent(struct device *dev, ssize_t size,
  94. dma_addr_t *dma_handle, void **ret)
  95. {
  96. struct dma_coherent_mem *mem;
  97. int order = get_order(size);
  98. int pageno;
  99. if (!dev)
  100. return 0;
  101. mem = dev->dma_mem;
  102. if (!mem)
  103. return 0;
  104. *ret = NULL;
  105. if (unlikely(size > (mem->size << PAGE_SHIFT)))
  106. goto err;
  107. pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
  108. if (unlikely(pageno < 0))
  109. goto err;
  110. /*
  111. * Memory was found in the per-device area.
  112. */
  113. *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
  114. *ret = mem->virt_base + (pageno << PAGE_SHIFT);
  115. memset(*ret, 0, size);
  116. return 1;
  117. err:
  118. /*
  119. * In the case where the allocation can not be satisfied from the
  120. * per-device area, try to fall back to generic memory if the
  121. * constraints allow it.
  122. */
  123. return mem->flags & DMA_MEMORY_EXCLUSIVE;
  124. }
  125. EXPORT_SYMBOL(dma_alloc_from_coherent);
  126. /**
  127. * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
  128. * @dev: device from which the memory was allocated
  129. * @order: the order of pages allocated
  130. * @vaddr: virtual address of allocated pages
  131. *
  132. * This checks whether the memory was allocated from the per-device
  133. * coherent memory pool and if so, releases that memory.
  134. *
  135. * Returns 1 if we correctly released the memory, or 0 if
  136. * dma_release_coherent() should proceed with releasing memory from
  137. * generic pools.
  138. */
  139. int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
  140. {
  141. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  142. if (mem && vaddr >= mem->virt_base && vaddr <
  143. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  144. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  145. bitmap_release_region(mem->bitmap, page, order);
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(dma_release_from_coherent);