dma-coherent.c 4.4 KB

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