Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Coherent per-device memory handling. |
| 3 | * Borrowed from i386 |
| 4 | */ |
Brian Starkey | 6b03ae0 | 2016-03-22 14:28:03 -0700 | [diff] [blame] | 5 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Paul Gortmaker | 08a999c | 2011-07-01 16:07:32 -0400 | [diff] [blame] | 8 | #include <linux/module.h> |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 9 | #include <linux/dma-mapping.h> |
| 10 | |
| 11 | struct dma_coherent_mem { |
| 12 | void *virt_base; |
Marin Mitov | ed1d218 | 2010-05-31 13:03:04 +0300 | [diff] [blame] | 13 | dma_addr_t device_base; |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 14 | unsigned long pfn_base; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 15 | int size; |
| 16 | int flags; |
| 17 | unsigned long *bitmap; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 18 | spinlock_t spinlock; |
Vladimir Murzin | c41f9ea | 2017-06-26 10:18:57 +0100 | [diff] [blame] | 19 | bool use_dev_dma_pfn_offset; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 20 | }; |
| 21 | |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 22 | static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init; |
| 23 | |
| 24 | static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev) |
| 25 | { |
| 26 | if (dev && dev->dma_mem) |
| 27 | return dev->dma_mem; |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 28 | return NULL; |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Vladimir Murzin | c41f9ea | 2017-06-26 10:18:57 +0100 | [diff] [blame] | 31 | static inline dma_addr_t dma_get_device_base(struct device *dev, |
| 32 | struct dma_coherent_mem * mem) |
| 33 | { |
| 34 | if (mem->use_dev_dma_pfn_offset) |
| 35 | return (mem->pfn_base - dev->dma_pfn_offset) << PAGE_SHIFT; |
| 36 | else |
| 37 | return mem->device_base; |
| 38 | } |
| 39 | |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 40 | static bool dma_init_coherent_memory( |
| 41 | phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags, |
| 42 | struct dma_coherent_mem **mem) |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 43 | { |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 44 | struct dma_coherent_mem *dma_mem = NULL; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 45 | void __iomem *mem_base = NULL; |
| 46 | int pages = size >> PAGE_SHIFT; |
| 47 | int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); |
| 48 | |
| 49 | if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0) |
| 50 | goto out; |
| 51 | if (!size) |
| 52 | goto out; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 53 | |
Brian Starkey | 6b03ae0 | 2016-03-22 14:28:03 -0700 | [diff] [blame] | 54 | if (flags & DMA_MEMORY_MAP) |
| 55 | mem_base = memremap(phys_addr, size, MEMREMAP_WC); |
| 56 | else |
| 57 | mem_base = ioremap(phys_addr, size); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 58 | if (!mem_base) |
| 59 | goto out; |
| 60 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 61 | dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); |
| 62 | if (!dma_mem) |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 63 | goto out; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 64 | dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 65 | if (!dma_mem->bitmap) |
| 66 | goto out; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 67 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 68 | dma_mem->virt_base = mem_base; |
| 69 | dma_mem->device_base = device_addr; |
| 70 | dma_mem->pfn_base = PFN_DOWN(phys_addr); |
| 71 | dma_mem->size = pages; |
| 72 | dma_mem->flags = flags; |
| 73 | spin_lock_init(&dma_mem->spinlock); |
| 74 | |
| 75 | *mem = dma_mem; |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 76 | return true; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 77 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 78 | out: |
| 79 | kfree(dma_mem); |
Brian Starkey | 6b03ae0 | 2016-03-22 14:28:03 -0700 | [diff] [blame] | 80 | if (mem_base) { |
| 81 | if (flags & DMA_MEMORY_MAP) |
| 82 | memunmap(mem_base); |
| 83 | else |
| 84 | iounmap(mem_base); |
| 85 | } |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 86 | return false; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 87 | } |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 88 | |
| 89 | static void dma_release_coherent_memory(struct dma_coherent_mem *mem) |
| 90 | { |
| 91 | if (!mem) |
| 92 | return; |
Brian Starkey | 6b03ae0 | 2016-03-22 14:28:03 -0700 | [diff] [blame] | 93 | |
| 94 | if (mem->flags & DMA_MEMORY_MAP) |
| 95 | memunmap(mem->virt_base); |
| 96 | else |
| 97 | iounmap(mem->virt_base); |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 98 | kfree(mem->bitmap); |
| 99 | kfree(mem); |
| 100 | } |
| 101 | |
| 102 | static int dma_assign_coherent_memory(struct device *dev, |
| 103 | struct dma_coherent_mem *mem) |
| 104 | { |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 105 | if (!dev) |
| 106 | return -ENODEV; |
| 107 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 108 | if (dev->dma_mem) |
| 109 | return -EBUSY; |
| 110 | |
| 111 | dev->dma_mem = mem; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
| 116 | dma_addr_t device_addr, size_t size, int flags) |
| 117 | { |
| 118 | struct dma_coherent_mem *mem; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 119 | |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 120 | if (!dma_init_coherent_memory(phys_addr, device_addr, size, flags, |
| 121 | &mem)) |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | |
| 124 | if (dma_assign_coherent_memory(dev, mem) == 0) |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 125 | return flags & DMA_MEMORY_MAP ? DMA_MEMORY_MAP : DMA_MEMORY_IO; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 126 | |
| 127 | dma_release_coherent_memory(mem); |
| 128 | return 0; |
| 129 | } |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 130 | EXPORT_SYMBOL(dma_declare_coherent_memory); |
| 131 | |
| 132 | void dma_release_declared_memory(struct device *dev) |
| 133 | { |
| 134 | struct dma_coherent_mem *mem = dev->dma_mem; |
| 135 | |
| 136 | if (!mem) |
| 137 | return; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 138 | dma_release_coherent_memory(mem); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 139 | dev->dma_mem = NULL; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 140 | } |
| 141 | EXPORT_SYMBOL(dma_release_declared_memory); |
| 142 | |
| 143 | void *dma_mark_declared_memory_occupied(struct device *dev, |
| 144 | dma_addr_t device_addr, size_t size) |
| 145 | { |
| 146 | struct dma_coherent_mem *mem = dev->dma_mem; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 147 | unsigned long flags; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 148 | int pos, err; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 149 | |
Jan Beulich | d2dc1f4 | 2008-08-05 13:01:31 -0700 | [diff] [blame] | 150 | size += device_addr & ~PAGE_MASK; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 151 | |
| 152 | if (!mem) |
| 153 | return ERR_PTR(-EINVAL); |
| 154 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 155 | spin_lock_irqsave(&mem->spinlock, flags); |
Vladimir Murzin | c41f9ea | 2017-06-26 10:18:57 +0100 | [diff] [blame] | 156 | pos = PFN_DOWN(device_addr - dma_get_device_base(dev, mem)); |
Jan Beulich | d2dc1f4 | 2008-08-05 13:01:31 -0700 | [diff] [blame] | 157 | err = bitmap_allocate_region(mem->bitmap, pos, get_order(size)); |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 158 | spin_unlock_irqrestore(&mem->spinlock, flags); |
| 159 | |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 160 | if (err != 0) |
| 161 | return ERR_PTR(err); |
| 162 | return mem->virt_base + (pos << PAGE_SHIFT); |
| 163 | } |
| 164 | EXPORT_SYMBOL(dma_mark_declared_memory_occupied); |
| 165 | |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 166 | static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, |
| 167 | ssize_t size, dma_addr_t *dma_handle) |
| 168 | { |
| 169 | int order = get_order(size); |
| 170 | unsigned long flags; |
| 171 | int pageno; |
| 172 | int dma_memory_map; |
| 173 | void *ret; |
| 174 | |
| 175 | spin_lock_irqsave(&mem->spinlock, flags); |
| 176 | |
| 177 | if (unlikely(size > (mem->size << PAGE_SHIFT))) |
| 178 | goto err; |
| 179 | |
| 180 | pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); |
| 181 | if (unlikely(pageno < 0)) |
| 182 | goto err; |
| 183 | |
| 184 | /* |
| 185 | * Memory was found in the coherent area. |
| 186 | */ |
| 187 | *dma_handle = mem->device_base + (pageno << PAGE_SHIFT); |
| 188 | ret = mem->virt_base + (pageno << PAGE_SHIFT); |
| 189 | dma_memory_map = (mem->flags & DMA_MEMORY_MAP); |
| 190 | spin_unlock_irqrestore(&mem->spinlock, flags); |
| 191 | if (dma_memory_map) |
| 192 | memset(ret, 0, size); |
| 193 | else |
| 194 | memset_io(ret, 0, size); |
| 195 | |
| 196 | return ret; |
| 197 | |
| 198 | err: |
| 199 | spin_unlock_irqrestore(&mem->spinlock, flags); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 203 | /** |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 204 | * dma_alloc_from_dev_coherent() - allocate memory from device coherent pool |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 205 | * @dev: device from which we allocate memory |
| 206 | * @size: size of requested memory area |
| 207 | * @dma_handle: This will be filled with the correct dma handle |
| 208 | * @ret: This pointer will be filled with the virtual address |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 209 | * to allocated area. |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 210 | * |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 211 | * This function should be only called from per-arch dma_alloc_coherent() |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 212 | * to support allocation from per-device coherent memory pools. |
| 213 | * |
| 214 | * Returns 0 if dma_alloc_coherent should continue with allocating from |
Dmitry Baryshkov | cb3952b | 2008-07-30 14:46:50 +0400 | [diff] [blame] | 215 | * generic memory areas, or !0 if dma_alloc_coherent should return @ret. |
Dmitry Baryshkov | b6d4f7e | 2008-07-20 15:01:10 +0400 | [diff] [blame] | 216 | */ |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 217 | int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, |
| 218 | dma_addr_t *dma_handle, void **ret) |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 219 | { |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 220 | struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 221 | |
Andrew Morton | eccd83e | 2009-01-06 14:43:09 -0800 | [diff] [blame] | 222 | if (!mem) |
| 223 | return 0; |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 224 | |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 225 | *ret = __dma_alloc_from_coherent(mem, size, dma_handle); |
| 226 | if (*ret) |
| 227 | return 1; |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 228 | |
Paul Mundt | 0609697 | 2009-01-21 18:51:53 +0900 | [diff] [blame] | 229 | /* |
| 230 | * In the case where the allocation can not be satisfied from the |
| 231 | * per-device area, try to fall back to generic memory if the |
| 232 | * constraints allow it. |
| 233 | */ |
| 234 | return mem->flags & DMA_MEMORY_EXCLUSIVE; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 235 | } |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 236 | EXPORT_SYMBOL(dma_alloc_from_dev_coherent); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 237 | |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 238 | void *dma_alloc_from_global_coherent(ssize_t size, dma_addr_t *dma_handle) |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 239 | { |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 240 | if (!dma_coherent_default_memory) |
| 241 | return NULL; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 242 | |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 243 | return __dma_alloc_from_coherent(dma_coherent_default_memory, size, |
| 244 | dma_handle); |
| 245 | } |
| 246 | |
| 247 | static int __dma_release_from_coherent(struct dma_coherent_mem *mem, |
| 248 | int order, void *vaddr) |
| 249 | { |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 250 | if (mem && vaddr >= mem->virt_base && vaddr < |
| 251 | (mem->virt_base + (mem->size << PAGE_SHIFT))) { |
| 252 | int page = (vaddr - mem->virt_base) >> PAGE_SHIFT; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 253 | unsigned long flags; |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 254 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 255 | spin_lock_irqsave(&mem->spinlock, flags); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 256 | bitmap_release_region(mem->bitmap, page, order); |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 257 | spin_unlock_irqrestore(&mem->spinlock, flags); |
Dmitry Baryshkov | ee7e551 | 2008-06-29 14:18:46 +0400 | [diff] [blame] | 258 | return 1; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 262 | |
| 263 | /** |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 264 | * dma_release_from_dev_coherent() - free memory to device coherent memory pool |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 265 | * @dev: device from which the memory was allocated |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 266 | * @order: the order of pages allocated |
| 267 | * @vaddr: virtual address of allocated pages |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 268 | * |
| 269 | * This checks whether the memory was allocated from the per-device |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 270 | * coherent memory pool and if so, releases that memory. |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 271 | * |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 272 | * Returns 1 if we correctly released the memory, or 0 if the caller should |
| 273 | * proceed with releasing memory from generic pools. |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 274 | */ |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 275 | int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr) |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 276 | { |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 277 | struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 278 | |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 279 | return __dma_release_from_coherent(mem, order, vaddr); |
| 280 | } |
| 281 | EXPORT_SYMBOL(dma_release_from_dev_coherent); |
| 282 | |
| 283 | int dma_release_from_global_coherent(int order, void *vaddr) |
| 284 | { |
| 285 | if (!dma_coherent_default_memory) |
| 286 | return 0; |
| 287 | |
| 288 | return __dma_release_from_coherent(dma_coherent_default_memory, order, |
| 289 | vaddr); |
| 290 | } |
| 291 | |
| 292 | static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem, |
| 293 | struct vm_area_struct *vma, void *vaddr, size_t size, int *ret) |
| 294 | { |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 295 | if (mem && vaddr >= mem->virt_base && vaddr + size <= |
| 296 | (mem->virt_base + (mem->size << PAGE_SHIFT))) { |
| 297 | unsigned long off = vma->vm_pgoff; |
| 298 | int start = (vaddr - mem->virt_base) >> PAGE_SHIFT; |
Muhammad Falak R Wani | e688f14 | 2016-05-21 18:52:57 +0530 | [diff] [blame] | 299 | int user_count = vma_pages(vma); |
George G. Davis | 9ca5d4f | 2016-09-28 08:51:56 +0100 | [diff] [blame] | 300 | int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 301 | |
| 302 | *ret = -ENXIO; |
| 303 | if (off < count && user_count <= count - off) { |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 304 | unsigned long pfn = mem->pfn_base + start + off; |
Marek Szyprowski | bca0fa5 | 2012-03-23 13:05:14 +0100 | [diff] [blame] | 305 | *ret = remap_pfn_range(vma, vma->vm_start, pfn, |
| 306 | user_count << PAGE_SHIFT, |
| 307 | vma->vm_page_prot); |
| 308 | } |
| 309 | return 1; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
Vladimir Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * dma_mmap_from_dev_coherent() - mmap memory from the device coherent pool |
| 316 | * @dev: device from which the memory was allocated |
| 317 | * @vma: vm_area for the userspace memory |
| 318 | * @vaddr: cpu address returned by dma_alloc_from_dev_coherent |
| 319 | * @size: size of the memory buffer allocated |
| 320 | * @ret: result from remap_pfn_range() |
| 321 | * |
| 322 | * This checks whether the memory was allocated from the per-device |
| 323 | * coherent memory pool and if so, maps that memory to the provided vma. |
| 324 | * |
| 325 | * Returns 1 if we correctly mapped the memory, or 0 if the caller should |
| 326 | * proceed with mapping memory from generic pools. |
| 327 | */ |
| 328 | int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, |
| 329 | void *vaddr, size_t size, int *ret) |
| 330 | { |
| 331 | struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); |
| 332 | |
| 333 | return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret); |
| 334 | } |
| 335 | EXPORT_SYMBOL(dma_mmap_from_dev_coherent); |
| 336 | |
| 337 | int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *vaddr, |
| 338 | size_t size, int *ret) |
| 339 | { |
| 340 | if (!dma_coherent_default_memory) |
| 341 | return 0; |
| 342 | |
| 343 | return __dma_mmap_from_coherent(dma_coherent_default_memory, vma, |
| 344 | vaddr, size, ret); |
| 345 | } |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Support for reserved memory regions defined in device tree |
| 349 | */ |
| 350 | #ifdef CONFIG_OF_RESERVED_MEM |
| 351 | #include <linux/of.h> |
| 352 | #include <linux/of_fdt.h> |
| 353 | #include <linux/of_reserved_mem.h> |
| 354 | |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 355 | static struct reserved_mem *dma_reserved_default_memory __initdata; |
| 356 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 357 | static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) |
| 358 | { |
| 359 | struct dma_coherent_mem *mem = rmem->priv; |
| 360 | |
| 361 | if (!mem && |
Michal Nazarewicz | 9e5b3d6 | 2016-01-04 22:36:40 +0100 | [diff] [blame] | 362 | !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, |
| 363 | DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE, |
| 364 | &mem)) { |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 365 | pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n", |
| 366 | &rmem->base, (unsigned long)rmem->size / SZ_1M); |
| 367 | return -ENODEV; |
| 368 | } |
Vladimir Murzin | c41f9ea | 2017-06-26 10:18:57 +0100 | [diff] [blame] | 369 | mem->use_dev_dma_pfn_offset = true; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 370 | rmem->priv = mem; |
| 371 | dma_assign_coherent_memory(dev, mem); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static void rmem_dma_device_release(struct reserved_mem *rmem, |
| 376 | struct device *dev) |
| 377 | { |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 378 | if (dev) |
| 379 | dev->dma_mem = NULL; |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static const struct reserved_mem_ops rmem_dma_ops = { |
| 383 | .device_init = rmem_dma_device_init, |
| 384 | .device_release = rmem_dma_device_release, |
| 385 | }; |
| 386 | |
| 387 | static int __init rmem_dma_setup(struct reserved_mem *rmem) |
| 388 | { |
| 389 | unsigned long node = rmem->fdt_node; |
| 390 | |
| 391 | if (of_get_flat_dt_prop(node, "reusable", NULL)) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | #ifdef CONFIG_ARM |
| 395 | if (!of_get_flat_dt_prop(node, "no-map", NULL)) { |
| 396 | pr_err("Reserved memory: regions without no-map are not yet supported\n"); |
| 397 | return -EINVAL; |
| 398 | } |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 399 | |
| 400 | if (of_get_flat_dt_prop(node, "linux,dma-default", NULL)) { |
| 401 | WARN(dma_reserved_default_memory, |
| 402 | "Reserved memory: region for default DMA coherent area is redefined\n"); |
| 403 | dma_reserved_default_memory = rmem; |
| 404 | } |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 405 | #endif |
| 406 | |
| 407 | rmem->ops = &rmem_dma_ops; |
| 408 | pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n", |
| 409 | &rmem->base, (unsigned long)rmem->size / SZ_1M); |
| 410 | return 0; |
| 411 | } |
Vladimir Murzin | 93228b4 | 2017-06-26 10:18:58 +0100 | [diff] [blame] | 412 | |
| 413 | static int __init dma_init_reserved_memory(void) |
| 414 | { |
| 415 | const struct reserved_mem_ops *ops; |
| 416 | int ret; |
| 417 | |
| 418 | if (!dma_reserved_default_memory) |
| 419 | return -ENOMEM; |
| 420 | |
| 421 | ops = dma_reserved_default_memory->ops; |
| 422 | |
| 423 | /* |
| 424 | * We rely on rmem_dma_device_init() does not propagate error of |
| 425 | * dma_assign_coherent_memory() for "NULL" device. |
| 426 | */ |
| 427 | ret = ops->device_init(dma_reserved_default_memory, NULL); |
| 428 | |
| 429 | if (!ret) { |
| 430 | dma_coherent_default_memory = dma_reserved_default_memory->priv; |
| 431 | pr_info("DMA: default coherent area is set\n"); |
| 432 | } |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | core_initcall(dma_init_reserved_memory); |
| 438 | |
Marek Szyprowski | 7bfa5ab | 2014-10-13 15:51:07 -0700 | [diff] [blame] | 439 | RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup); |
| 440 | #endif |