2 * Coherent per-device memory handling.
5 #include <linux/slab.h>
6 #include <linux/kernel.h>
7 #include <linux/dma-mapping.h>
9 struct dma_coherent_mem {
11 dma_addr_t device_base;
14 unsigned long *bitmap;
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)
20 void __iomem *mem_base = NULL;
21 int pages = size >> PAGE_SHIFT;
22 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
24 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
31 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
33 mem_base = ioremap(bus_addr, size);
37 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
40 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
41 if (!dev->dma_mem->bitmap)
44 dev->dma_mem->virt_base = mem_base;
45 dev->dma_mem->device_base = device_addr;
46 dev->dma_mem->size = pages;
47 dev->dma_mem->flags = flags;
49 if (flags & DMA_MEMORY_MAP)
50 return DMA_MEMORY_MAP;
61 EXPORT_SYMBOL(dma_declare_coherent_memory);
63 void dma_release_declared_memory(struct device *dev)
65 struct dma_coherent_mem *mem = dev->dma_mem;
70 iounmap(mem->virt_base);
74 EXPORT_SYMBOL(dma_release_declared_memory);
76 void *dma_mark_declared_memory_occupied(struct device *dev,
77 dma_addr_t device_addr, size_t size)
79 struct dma_coherent_mem *mem = dev->dma_mem;
82 size += device_addr & ~PAGE_MASK;
85 return ERR_PTR(-EINVAL);
87 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
88 err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
91 return mem->virt_base + (pos << PAGE_SHIFT);
93 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
96 * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area
98 * @dev: device from which we allocate memory
99 * @size: size of requested memory area
100 * @dma_handle: This will be filled with the correct dma handle
101 * @ret: This pointer will be filled with the virtual address
104 * This function should be only called from per-arch dma_alloc_coherent()
105 * to support allocation from per-device coherent memory pools.
107 * Returns 0 if dma_alloc_coherent should continue with allocating from
108 * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
110 int dma_alloc_from_coherent(struct device *dev, ssize_t size,
111 dma_addr_t *dma_handle, void **ret)
113 struct dma_coherent_mem *mem;
114 int order = get_order(size);
125 if (unlikely(size > (mem->size << PAGE_SHIFT)))
128 pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
129 if (unlikely(pageno < 0))
133 * Memory was found in the per-device area.
135 *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
136 *ret = mem->virt_base + (pageno << PAGE_SHIFT);
137 memset(*ret, 0, size);
143 * In the case where the allocation can not be satisfied from the
144 * per-device area, try to fall back to generic memory if the
145 * constraints allow it.
147 return mem->flags & DMA_MEMORY_EXCLUSIVE;
149 EXPORT_SYMBOL(dma_alloc_from_coherent);
152 * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
153 * @dev: device from which the memory was allocated
154 * @order: the order of pages allocated
155 * @vaddr: virtual address of allocated pages
157 * This checks whether the memory was allocated from the per-device
158 * coherent memory pool and if so, releases that memory.
160 * Returns 1 if we correctly released the memory, or 0 if
161 * dma_release_coherent() should proceed with releasing memory from
164 int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
166 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
168 if (mem && vaddr >= mem->virt_base && vaddr <
169 (mem->virt_base + (mem->size << PAGE_SHIFT))) {
170 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
172 bitmap_release_region(mem->bitmap, page, order);
177 EXPORT_SYMBOL(dma_release_from_coherent);