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