2 * Coherent per-device memory handling.
5 #include <linux/kernel.h>
6 #include <linux/dma-mapping.h>
8 struct dma_coherent_mem {
13 unsigned long *bitmap;
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)
19 void __iomem *mem_base = NULL;
20 int pages = size >> PAGE_SHIFT;
21 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
23 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
30 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
32 mem_base = ioremap(bus_addr, size);
36 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
39 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
40 if (!dev->dma_mem->bitmap)
43 dev->dma_mem->virt_base = mem_base;
44 dev->dma_mem->device_base = device_addr;
45 dev->dma_mem->size = pages;
46 dev->dma_mem->flags = flags;
48 if (flags & DMA_MEMORY_MAP)
49 return DMA_MEMORY_MAP;
60 EXPORT_SYMBOL(dma_declare_coherent_memory);
62 void dma_release_declared_memory(struct device *dev)
64 struct dma_coherent_mem *mem = dev->dma_mem;
69 iounmap(mem->virt_base);
73 EXPORT_SYMBOL(dma_release_declared_memory);
75 void *dma_mark_declared_memory_occupied(struct device *dev,
76 dma_addr_t device_addr, size_t size)
78 struct dma_coherent_mem *mem = dev->dma_mem;
81 size += device_addr & ~PAGE_MASK;
84 return ERR_PTR(-EINVAL);
86 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
87 err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
90 return mem->virt_base + (pos << PAGE_SHIFT);
92 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
95 * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area
97 * @dev: device from which we allocate memory
98 * @size: size of requested memory area
99 * @dma_handle: This will be filled with the correct dma handle
100 * @ret: This pointer will be filled with the virtual address
103 * This function should be only called from per-arch dma_alloc_coherent()
104 * to support allocation from per-device coherent memory pools.
106 * Returns 0 if dma_alloc_coherent should continue with allocating from
107 * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
109 int dma_alloc_from_coherent(struct device *dev, ssize_t size,
110 dma_addr_t *dma_handle, void **ret)
112 struct dma_coherent_mem *mem;
113 int order = get_order(size);
124 if (unlikely(size > (mem->size << PAGE_SHIFT)))
127 pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
128 if (unlikely(pageno < 0))
132 * Memory was found in the per-device area.
134 *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
135 *ret = mem->virt_base + (pageno << PAGE_SHIFT);
136 memset(*ret, 0, size);
142 * In the case where the allocation can not be satisfied from the
143 * per-device area, try to fall back to generic memory if the
144 * constraints allow it.
146 return mem->flags & DMA_MEMORY_EXCLUSIVE;
148 EXPORT_SYMBOL(dma_alloc_from_coherent);
151 * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
152 * @dev: device from which the memory was allocated
153 * @order: the order of pages allocated
154 * @vaddr: virtual address of allocated pages
156 * This checks whether the memory was allocated from the per-device
157 * coherent memory pool and if so, releases that memory.
159 * Returns 1 if we correctly released the memory, or 0 if
160 * dma_release_coherent() should proceed with releasing memory from
163 int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
165 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
167 if (mem && vaddr >= mem->virt_base && vaddr <
168 (mem->virt_base + (mem->size << PAGE_SHIFT))) {
169 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
171 bitmap_release_region(mem->bitmap, page, order);
176 EXPORT_SYMBOL(dma_release_from_coherent);