Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/dma-mapping.c - arch-independent dma-mapping routines |
| 3 | * |
| 4 | * Copyright (c) 2006 SUSE Linux Products GmbH |
| 5 | * Copyright (c) 2006 Tejun Heo <teheo@suse.de> |
| 6 | * |
| 7 | * This file is released under the GPLv2. |
| 8 | */ |
| 9 | |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 11 | #include <linux/dma-mapping.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Laura Abbott | 513510d | 2014-10-09 15:26:40 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/vmalloc.h> |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Managed DMA API |
| 20 | */ |
| 21 | struct dma_devres { |
| 22 | size_t size; |
| 23 | void *vaddr; |
| 24 | dma_addr_t dma_handle; |
| 25 | }; |
| 26 | |
| 27 | static void dmam_coherent_release(struct device *dev, void *res) |
| 28 | { |
| 29 | struct dma_devres *this = res; |
| 30 | |
| 31 | dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle); |
| 32 | } |
| 33 | |
| 34 | static void dmam_noncoherent_release(struct device *dev, void *res) |
| 35 | { |
| 36 | struct dma_devres *this = res; |
| 37 | |
| 38 | dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle); |
| 39 | } |
| 40 | |
| 41 | static int dmam_match(struct device *dev, void *res, void *match_data) |
| 42 | { |
| 43 | struct dma_devres *this = res, *match = match_data; |
| 44 | |
| 45 | if (this->vaddr == match->vaddr) { |
| 46 | WARN_ON(this->size != match->size || |
| 47 | this->dma_handle != match->dma_handle); |
| 48 | return 1; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * dmam_alloc_coherent - Managed dma_alloc_coherent() |
| 55 | * @dev: Device to allocate coherent memory for |
| 56 | * @size: Size of allocation |
| 57 | * @dma_handle: Out argument for allocated DMA handle |
| 58 | * @gfp: Allocation flags |
| 59 | * |
| 60 | * Managed dma_alloc_coherent(). Memory allocated using this function |
| 61 | * will be automatically released on driver detach. |
| 62 | * |
| 63 | * RETURNS: |
| 64 | * Pointer to allocated memory on success, NULL on failure. |
| 65 | */ |
Marius Cristian Eseanu | 6d42d79 | 2015-03-08 12:34:14 +0200 | [diff] [blame] | 66 | void *dmam_alloc_coherent(struct device *dev, size_t size, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 67 | dma_addr_t *dma_handle, gfp_t gfp) |
| 68 | { |
| 69 | struct dma_devres *dr; |
| 70 | void *vaddr; |
| 71 | |
| 72 | dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp); |
| 73 | if (!dr) |
| 74 | return NULL; |
| 75 | |
| 76 | vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp); |
| 77 | if (!vaddr) { |
| 78 | devres_free(dr); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | dr->vaddr = vaddr; |
| 83 | dr->dma_handle = *dma_handle; |
| 84 | dr->size = size; |
| 85 | |
| 86 | devres_add(dev, dr); |
| 87 | |
| 88 | return vaddr; |
| 89 | } |
| 90 | EXPORT_SYMBOL(dmam_alloc_coherent); |
| 91 | |
| 92 | /** |
| 93 | * dmam_free_coherent - Managed dma_free_coherent() |
| 94 | * @dev: Device to free coherent memory for |
| 95 | * @size: Size of allocation |
| 96 | * @vaddr: Virtual address of the memory to free |
| 97 | * @dma_handle: DMA handle of the memory to free |
| 98 | * |
| 99 | * Managed dma_free_coherent(). |
| 100 | */ |
| 101 | void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 102 | dma_addr_t dma_handle) |
| 103 | { |
| 104 | struct dma_devres match_data = { size, vaddr, dma_handle }; |
| 105 | |
| 106 | dma_free_coherent(dev, size, vaddr, dma_handle); |
| 107 | WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match, |
| 108 | &match_data)); |
| 109 | } |
| 110 | EXPORT_SYMBOL(dmam_free_coherent); |
| 111 | |
| 112 | /** |
Florian Fainelli | cd74da9 | 2016-11-29 17:21:05 -0800 | [diff] [blame] | 113 | * dmam_alloc_non_coherent - Managed dma_alloc_noncoherent() |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 114 | * @dev: Device to allocate non_coherent memory for |
| 115 | * @size: Size of allocation |
| 116 | * @dma_handle: Out argument for allocated DMA handle |
| 117 | * @gfp: Allocation flags |
| 118 | * |
Florian Fainelli | cd74da9 | 2016-11-29 17:21:05 -0800 | [diff] [blame] | 119 | * Managed dma_alloc_noncoherent(). Memory allocated using this |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 120 | * function will be automatically released on driver detach. |
| 121 | * |
| 122 | * RETURNS: |
| 123 | * Pointer to allocated memory on success, NULL on failure. |
| 124 | */ |
| 125 | void *dmam_alloc_noncoherent(struct device *dev, size_t size, |
| 126 | dma_addr_t *dma_handle, gfp_t gfp) |
| 127 | { |
| 128 | struct dma_devres *dr; |
| 129 | void *vaddr; |
| 130 | |
| 131 | dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp); |
| 132 | if (!dr) |
| 133 | return NULL; |
| 134 | |
| 135 | vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp); |
| 136 | if (!vaddr) { |
| 137 | devres_free(dr); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | dr->vaddr = vaddr; |
| 142 | dr->dma_handle = *dma_handle; |
| 143 | dr->size = size; |
| 144 | |
| 145 | devres_add(dev, dr); |
| 146 | |
| 147 | return vaddr; |
| 148 | } |
| 149 | EXPORT_SYMBOL(dmam_alloc_noncoherent); |
| 150 | |
| 151 | /** |
| 152 | * dmam_free_coherent - Managed dma_free_noncoherent() |
| 153 | * @dev: Device to free noncoherent memory for |
| 154 | * @size: Size of allocation |
| 155 | * @vaddr: Virtual address of the memory to free |
| 156 | * @dma_handle: DMA handle of the memory to free |
| 157 | * |
| 158 | * Managed dma_free_noncoherent(). |
| 159 | */ |
| 160 | void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr, |
| 161 | dma_addr_t dma_handle) |
| 162 | { |
| 163 | struct dma_devres match_data = { size, vaddr, dma_handle }; |
| 164 | |
| 165 | dma_free_noncoherent(dev, size, vaddr, dma_handle); |
| 166 | WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match, |
| 167 | &match_data)); |
| 168 | } |
| 169 | EXPORT_SYMBOL(dmam_free_noncoherent); |
| 170 | |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 171 | #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 172 | |
| 173 | static void dmam_coherent_decl_release(struct device *dev, void *res) |
| 174 | { |
| 175 | dma_release_declared_memory(dev); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory() |
| 180 | * @dev: Device to declare coherent memory for |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 181 | * @phys_addr: Physical address of coherent memory to be declared |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 182 | * @device_addr: Device address of coherent memory to be declared |
| 183 | * @size: Size of coherent memory to be declared |
| 184 | * @flags: Flags |
| 185 | * |
| 186 | * Managed dma_declare_coherent_memory(). |
| 187 | * |
| 188 | * RETURNS: |
| 189 | * 0 on success, -errno on failure. |
| 190 | */ |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 191 | int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 192 | dma_addr_t device_addr, size_t size, int flags) |
| 193 | { |
| 194 | void *res; |
| 195 | int rc; |
| 196 | |
| 197 | res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL); |
| 198 | if (!res) |
| 199 | return -ENOMEM; |
| 200 | |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 201 | rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 202 | flags); |
Vyacheslav V. Yurkov | 775115c | 2016-06-14 09:58:37 +0200 | [diff] [blame] | 203 | if (rc) { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 204 | devres_add(dev, res); |
Vyacheslav V. Yurkov | 775115c | 2016-06-14 09:58:37 +0200 | [diff] [blame] | 205 | rc = 0; |
| 206 | } else { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 207 | devres_free(res); |
Vyacheslav V. Yurkov | 775115c | 2016-06-14 09:58:37 +0200 | [diff] [blame] | 208 | rc = -ENOMEM; |
| 209 | } |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 210 | |
| 211 | return rc; |
| 212 | } |
| 213 | EXPORT_SYMBOL(dmam_declare_coherent_memory); |
| 214 | |
| 215 | /** |
| 216 | * dmam_release_declared_memory - Managed dma_release_declared_memory(). |
| 217 | * @dev: Device to release declared coherent memory for |
| 218 | * |
| 219 | * Managed dmam_release_declared_memory(). |
| 220 | */ |
| 221 | void dmam_release_declared_memory(struct device *dev) |
| 222 | { |
| 223 | WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL)); |
| 224 | } |
| 225 | EXPORT_SYMBOL(dmam_release_declared_memory); |
| 226 | |
Marek Szyprowski | c6c2295 | 2012-11-26 10:41:48 -0300 | [diff] [blame] | 227 | #endif |
| 228 | |
Marek Szyprowski | d2b7428 | 2012-06-13 10:05:52 +0200 | [diff] [blame] | 229 | /* |
| 230 | * Create scatter-list for the already allocated DMA buffer. |
| 231 | */ |
| 232 | int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 233 | void *cpu_addr, dma_addr_t handle, size_t size) |
| 234 | { |
| 235 | struct page *page = virt_to_page(cpu_addr); |
| 236 | int ret; |
| 237 | |
| 238 | ret = sg_alloc_table(sgt, 1, GFP_KERNEL); |
| 239 | if (unlikely(ret)) |
| 240 | return ret; |
| 241 | |
| 242 | sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); |
| 243 | return 0; |
| 244 | } |
| 245 | EXPORT_SYMBOL(dma_common_get_sgtable); |
| 246 | |
Marek Szyprowski | 64ccc9c | 2012-06-14 13:03:04 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Create userspace mapping for the DMA-coherent memory. |
| 249 | */ |
| 250 | int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, |
| 251 | void *cpu_addr, dma_addr_t dma_addr, size_t size) |
| 252 | { |
| 253 | int ret = -ENXIO; |
Christoph Hellwig | 0d4a619 | 2016-01-20 15:01:22 -0800 | [diff] [blame] | 254 | #if defined(CONFIG_MMU) && !defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) |
Muhammad Falak R Wani | 95da00e | 2016-05-21 18:52:22 +0530 | [diff] [blame] | 255 | unsigned long user_count = vma_pages(vma); |
Marek Szyprowski | 64ccc9c | 2012-06-14 13:03:04 +0200 | [diff] [blame] | 256 | unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 257 | unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); |
| 258 | unsigned long off = vma->vm_pgoff; |
| 259 | |
| 260 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 261 | |
| 262 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) |
| 263 | return ret; |
| 264 | |
| 265 | if (off < count && user_count <= (count - off)) { |
| 266 | ret = remap_pfn_range(vma, vma->vm_start, |
| 267 | pfn + off, |
| 268 | user_count << PAGE_SHIFT, |
| 269 | vma->vm_page_prot); |
| 270 | } |
Christoph Hellwig | 0d4a619 | 2016-01-20 15:01:22 -0800 | [diff] [blame] | 271 | #endif /* CONFIG_MMU && !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */ |
Marek Szyprowski | 64ccc9c | 2012-06-14 13:03:04 +0200 | [diff] [blame] | 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | EXPORT_SYMBOL(dma_common_mmap); |
Laura Abbott | 513510d | 2014-10-09 15:26:40 -0700 | [diff] [blame] | 276 | |
| 277 | #ifdef CONFIG_MMU |
| 278 | /* |
| 279 | * remaps an array of PAGE_SIZE pages into another vm_area |
| 280 | * Cannot be used in non-sleeping contexts |
| 281 | */ |
| 282 | void *dma_common_pages_remap(struct page **pages, size_t size, |
| 283 | unsigned long vm_flags, pgprot_t prot, |
| 284 | const void *caller) |
| 285 | { |
| 286 | struct vm_struct *area; |
| 287 | |
| 288 | area = get_vm_area_caller(size, vm_flags, caller); |
| 289 | if (!area) |
| 290 | return NULL; |
| 291 | |
| 292 | area->pages = pages; |
| 293 | |
| 294 | if (map_vm_area(area, prot, pages)) { |
| 295 | vunmap(area->addr); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | return area->addr; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * remaps an allocated contiguous region into another vm_area. |
| 304 | * Cannot be used in non-sleeping contexts |
| 305 | */ |
| 306 | |
| 307 | void *dma_common_contiguous_remap(struct page *page, size_t size, |
| 308 | unsigned long vm_flags, |
| 309 | pgprot_t prot, const void *caller) |
| 310 | { |
| 311 | int i; |
| 312 | struct page **pages; |
| 313 | void *ptr; |
Laura Abbott | 513510d | 2014-10-09 15:26:40 -0700 | [diff] [blame] | 314 | |
| 315 | pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL); |
| 316 | if (!pages) |
| 317 | return NULL; |
| 318 | |
Geliang Tang | 0dd8911 | 2017-03-24 22:10:49 +0800 | [diff] [blame] | 319 | for (i = 0; i < (size >> PAGE_SHIFT); i++) |
| 320 | pages[i] = nth_page(page, i); |
Laura Abbott | 513510d | 2014-10-09 15:26:40 -0700 | [diff] [blame] | 321 | |
| 322 | ptr = dma_common_pages_remap(pages, size, vm_flags, prot, caller); |
| 323 | |
| 324 | kfree(pages); |
| 325 | |
| 326 | return ptr; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * unmaps a range previously mapped by dma_common_*_remap |
| 331 | */ |
| 332 | void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags) |
| 333 | { |
| 334 | struct vm_struct *area = find_vm_area(cpu_addr); |
| 335 | |
| 336 | if (!area || (area->flags & vm_flags) != vm_flags) { |
| 337 | WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr); |
| 338 | return; |
| 339 | } |
| 340 | |
Peng Fan | 8571410 | 2016-07-21 16:04:21 +0800 | [diff] [blame] | 341 | unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size)); |
Laura Abbott | 513510d | 2014-10-09 15:26:40 -0700 | [diff] [blame] | 342 | vunmap(cpu_addr); |
| 343 | } |
| 344 | #endif |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Common configuration to enable DMA API use for a device |
| 348 | */ |
| 349 | #include <linux/pci.h> |
| 350 | |
| 351 | int dma_configure(struct device *dev) |
| 352 | { |
| 353 | struct device *bridge = NULL, *dma_dev = dev; |
| 354 | enum dev_dma_attr attr; |
Laurent Pinchart | 7b07cbe | 2017-04-10 16:51:02 +0530 | [diff] [blame] | 355 | int ret = 0; |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 356 | |
| 357 | if (dev_is_pci(dev)) { |
| 358 | bridge = pci_get_host_bridge_device(to_pci_dev(dev)); |
| 359 | dma_dev = bridge; |
| 360 | if (IS_ENABLED(CONFIG_OF) && dma_dev->parent && |
| 361 | dma_dev->parent->of_node) |
| 362 | dma_dev = dma_dev->parent; |
| 363 | } |
| 364 | |
| 365 | if (dma_dev->of_node) { |
Laurent Pinchart | 7b07cbe | 2017-04-10 16:51:02 +0530 | [diff] [blame] | 366 | ret = of_dma_configure(dev, dma_dev->of_node); |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 367 | } else if (has_acpi_companion(dma_dev)) { |
| 368 | attr = acpi_get_dma_attr(to_acpi_device_node(dma_dev->fwnode)); |
| 369 | if (attr != DEV_DMA_NOT_SUPPORTED) |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 370 | ret = acpi_dma_configure(dev, attr); |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (bridge) |
| 374 | pci_put_host_bridge_device(bridge); |
| 375 | |
Laurent Pinchart | 7b07cbe | 2017-04-10 16:51:02 +0530 | [diff] [blame] | 376 | return ret; |
Sricharan R | 09515ef | 2017-04-10 16:51:01 +0530 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void dma_deconfigure(struct device *dev) |
| 380 | { |
| 381 | of_dma_deconfigure(dev); |
| 382 | acpi_dma_deconfigure(dev); |
| 383 | } |