Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Coherent per-device memory handling. |
| 3 | * Borrowed from i386 |
| 4 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Paul Gortmaker | 08a999c | 2011-07-01 16:07:32 -0400 | [diff] [blame] | 7 | #include <linux/module.h> |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 8 | #include <linux/dma-mapping.h> |
| 9 | |
| 10 | struct dma_coherent_mem { |
| 11 | void *virt_base; |
Marin Mitov | ed1d218 | 2010-05-31 13:03:04 +0300 | [diff] [blame] | 12 | dma_addr_t device_base; |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 13 | unsigned long pfn_base; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 14 | int size; |
| 15 | int flags; |
| 16 | unsigned long *bitmap; |
| 17 | }; |
| 18 | |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 19 | int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 20 | dma_addr_t device_addr, size_t size, int flags) |
| 21 | { |
| 22 | void __iomem *mem_base = NULL; |
| 23 | int pages = size >> PAGE_SHIFT; |
| 24 | int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); |
| 25 | |
| 26 | if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0) |
| 27 | goto out; |
| 28 | if (!size) |
| 29 | goto out; |
| 30 | if (dev->dma_mem) |
| 31 | goto out; |
| 32 | |
| 33 | /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */ |
| 34 | |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 35 | mem_base = ioremap(phys_addr, size); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 36 | if (!mem_base) |
| 37 | goto out; |
| 38 | |
| 39 | dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); |
| 40 | if (!dev->dma_mem) |
| 41 | goto out; |
| 42 | dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 43 | if (!dev->dma_mem->bitmap) |
| 44 | goto free1_out; |
| 45 | |
| 46 | dev->dma_mem->virt_base = mem_base; |
| 47 | dev->dma_mem->device_base = device_addr; |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 48 | dev->dma_mem->pfn_base = PFN_DOWN(phys_addr); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 49 | dev->dma_mem->size = pages; |
| 50 | dev->dma_mem->flags = flags; |
| 51 | |
| 52 | if (flags & DMA_MEMORY_MAP) |
| 53 | return DMA_MEMORY_MAP; |
| 54 | |
| 55 | return DMA_MEMORY_IO; |
| 56 | |
| 57 | free1_out: |
| 58 | kfree(dev->dma_mem); |
| 59 | out: |
| 60 | if (mem_base) |
| 61 | iounmap(mem_base); |
| 62 | return 0; |
| 63 | } |
| 64 | EXPORT_SYMBOL(dma_declare_coherent_memory); |
| 65 | |
| 66 | void dma_release_declared_memory(struct device *dev) |
| 67 | { |
| 68 | struct dma_coherent_mem *mem = dev->dma_mem; |
| 69 | |
| 70 | if (!mem) |
| 71 | return; |
| 72 | dev->dma_mem = NULL; |
| 73 | iounmap(mem->virt_base); |
| 74 | kfree(mem->bitmap); |
| 75 | kfree(mem); |
| 76 | } |
| 77 | EXPORT_SYMBOL(dma_release_declared_memory); |
| 78 | |
| 79 | void *dma_mark_declared_memory_occupied(struct device *dev, |
| 80 | dma_addr_t device_addr, size_t size) |
| 81 | { |
| 82 | struct dma_coherent_mem *mem = dev->dma_mem; |
| 83 | int pos, err; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 84 | |
Jan Beulich | d2dc1f4 | 2008-08-05 13:01:31 -0700 | [diff] [blame] | 85 | size += device_addr & ~PAGE_MASK; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 86 | |
| 87 | if (!mem) |
| 88 | return ERR_PTR(-EINVAL); |
| 89 | |
| 90 | pos = (device_addr - mem->device_base) >> PAGE_SHIFT; |
Jan Beulich | d2dc1f4 | 2008-08-05 13:01:31 -0700 | [diff] [blame] | 91 | err = bitmap_allocate_region(mem->bitmap, pos, get_order(size)); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 92 | if (err != 0) |
| 93 | return ERR_PTR(err); |
| 94 | return mem->virt_base + (pos << PAGE_SHIFT); |
| 95 | } |
| 96 | EXPORT_SYMBOL(dma_mark_declared_memory_occupied); |
| 97 | |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 98 | /** |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 99 | * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 100 | * |
| 101 | * @dev: device from which we allocate memory |
| 102 | * @size: size of requested memory area |
| 103 | * @dma_handle: This will be filled with the correct dma handle |
| 104 | * @ret: This pointer will be filled with the virtual address |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 105 | * to allocated area. |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 106 | * |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 107 | * This function should be only called from per-arch dma_alloc_coherent() |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 108 | * to support allocation from per-device coherent memory pools. |
| 109 | * |
| 110 | * Returns 0 if dma_alloc_coherent should continue with allocating from |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 111 | * generic memory areas, or !0 if dma_alloc_coherent should return @ret. |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 112 | */ |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 113 | int dma_alloc_from_coherent(struct device *dev, ssize_t size, |
| 114 | dma_addr_t *dma_handle, void **ret) |
| 115 | { |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 116 | struct dma_coherent_mem *mem; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 117 | int order = get_order(size); |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 118 | int pageno; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 119 | |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 120 | if (!dev) |
| 121 | return 0; |
| 122 | mem = dev->dma_mem; |
| 123 | if (!mem) |
| 124 | return 0; |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 125 | |
| 126 | *ret = NULL; |
| 127 | |
Adrian McMenamin | cdf57ca | 2009-01-21 18:47:38 +0900 | [diff] [blame] | 128 | if (unlikely(size > (mem->size << PAGE_SHIFT))) |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 129 | goto err; |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 130 | |
| 131 | pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 132 | if (unlikely(pageno < 0)) |
| 133 | goto err; |
| 134 | |
| 135 | /* |
| 136 | * Memory was found in the per-device area. |
| 137 | */ |
| 138 | *dma_handle = mem->device_base + (pageno << PAGE_SHIFT); |
| 139 | *ret = mem->virt_base + (pageno << PAGE_SHIFT); |
| 140 | memset(*ret, 0, size); |
| 141 | |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 142 | return 1; |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 143 | |
| 144 | err: |
| 145 | /* |
| 146 | * In the case where the allocation can not be satisfied from the |
| 147 | * per-device area, try to fall back to generic memory if the |
| 148 | * constraints allow it. |
| 149 | */ |
| 150 | return mem->flags & DMA_MEMORY_EXCLUSIVE; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 151 | } |
Ingo Molnar | a38409f | 2008-08-20 12:16:09 +0200 | [diff] [blame] | 152 | EXPORT_SYMBOL(dma_alloc_from_coherent); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 153 | |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 154 | /** |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 155 | * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 156 | * @dev: device from which the memory was allocated |
| 157 | * @order: the order of pages allocated |
| 158 | * @vaddr: virtual address of allocated pages |
| 159 | * |
| 160 | * This checks whether the memory was allocated from the per-device |
| 161 | * coherent memory pool and if so, releases that memory. |
| 162 | * |
| 163 | * Returns 1 if we correctly released the memory, or 0 if |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 164 | * dma_release_coherent() should proceed with releasing memory from |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 165 | * generic pools. |
| 166 | */ |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 167 | int dma_release_from_coherent(struct device *dev, int order, void *vaddr) |
| 168 | { |
| 169 | struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL; |
| 170 | |
| 171 | if (mem && vaddr >= mem->virt_base && vaddr < |
| 172 | (mem->virt_base + (mem->size << PAGE_SHIFT))) { |
| 173 | int page = (vaddr - mem->virt_base) >> PAGE_SHIFT; |
| 174 | |
| 175 | bitmap_release_region(mem->bitmap, page, order); |
| 176 | return 1; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
Ingo Molnar | a38409f | 2008-08-20 12:16:09 +0200 | [diff] [blame] | 180 | EXPORT_SYMBOL(dma_release_from_coherent); |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * dma_mmap_from_coherent() - try to mmap the memory allocated from |
| 184 | * per-device coherent memory pool to userspace |
| 185 | * @dev: device from which the memory was allocated |
| 186 | * @vma: vm_area for the userspace memory |
| 187 | * @vaddr: cpu address returned by dma_alloc_from_coherent |
| 188 | * @size: size of the memory buffer allocated by dma_alloc_from_coherent |
Randy Dunlap | 6e7b4a5 | 2012-06-09 15:02:59 -0700 | [diff] [blame] | 189 | * @ret: result from remap_pfn_range() |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 190 | * |
| 191 | * This checks whether the memory was allocated from the per-device |
| 192 | * coherent memory pool and if so, maps that memory to the provided vma. |
| 193 | * |
Laurent Pinchart | ba4d93b | 2012-10-18 09:29:44 +0200 | [diff] [blame] | 194 | * Returns 1 if we correctly mapped the memory, or 0 if the caller should |
| 195 | * proceed with mapping memory from generic pools. |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 196 | */ |
| 197 | int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma, |
| 198 | void *vaddr, size_t size, int *ret) |
| 199 | { |
| 200 | struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL; |
| 201 | |
| 202 | if (mem && vaddr >= mem->virt_base && vaddr + size <= |
| 203 | (mem->virt_base + (mem->size << PAGE_SHIFT))) { |
| 204 | unsigned long off = vma->vm_pgoff; |
| 205 | int start = (vaddr - mem->virt_base) >> PAGE_SHIFT; |
| 206 | int user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 207 | int count = size >> PAGE_SHIFT; |
| 208 | |
| 209 | *ret = -ENXIO; |
| 210 | if (off < count && user_count <= count - off) { |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 211 | unsigned long pfn = mem->pfn_base + start + off; |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 212 | *ret = remap_pfn_range(vma, vma->vm_start, pfn, |
| 213 | user_count << PAGE_SHIFT, |
| 214 | vma->vm_page_prot); |
| 215 | } |
| 216 | return 1; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | EXPORT_SYMBOL(dma_mmap_from_coherent); |