1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * arch-independent dma-mapping routines 4 * 5 * Copyright (c) 2006 SUSE Linux Products GmbH 6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de> 7 */ 8 #include <linux/memblock.h> /* for max_pfn */ 9 #include <linux/acpi.h> 10 #include <linux/dma-map-ops.h> 11 #include <linux/export.h> 12 #include <linux/gfp.h> 13 #include <linux/iommu-dma.h> 14 #include <linux/kmsan.h> 15 #include <linux/of_device.h> 16 #include <linux/slab.h> 17 #include <linux/vmalloc.h> 18 #include "debug.h" 19 #include "direct.h" 20 21 #define CREATE_TRACE_POINTS 22 #include <trace/events/dma.h> 23 24 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ 25 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ 26 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) 27 bool dma_default_coherent = IS_ENABLED(CONFIG_ARCH_DMA_DEFAULT_COHERENT); 28 #endif 29 30 /* 31 * Managed DMA API 32 */ 33 struct dma_devres { 34 size_t size; 35 void *vaddr; 36 dma_addr_t dma_handle; 37 unsigned long attrs; 38 }; 39 40 static void dmam_release(struct device *dev, void *res) 41 { 42 struct dma_devres *this = res; 43 44 dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle, 45 this->attrs); 46 } 47 48 static int dmam_match(struct device *dev, void *res, void *match_data) 49 { 50 struct dma_devres *this = res, *match = match_data; 51 52 if (this->vaddr == match->vaddr) { 53 WARN_ON(this->size != match->size || 54 this->dma_handle != match->dma_handle); 55 return 1; 56 } 57 return 0; 58 } 59 60 /** 61 * dmam_free_coherent - Managed dma_free_coherent() 62 * @dev: Device to free coherent memory for 63 * @size: Size of allocation 64 * @vaddr: Virtual address of the memory to free 65 * @dma_handle: DMA handle of the memory to free 66 * 67 * Managed dma_free_coherent(). 68 */ 69 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, 70 dma_addr_t dma_handle) 71 { 72 struct dma_devres match_data = { size, vaddr, dma_handle }; 73 74 WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data)); 75 dma_free_coherent(dev, size, vaddr, dma_handle); 76 } 77 EXPORT_SYMBOL(dmam_free_coherent); 78 79 /** 80 * dmam_alloc_attrs - Managed dma_alloc_attrs() 81 * @dev: Device to allocate non_coherent memory for 82 * @size: Size of allocation 83 * @dma_handle: Out argument for allocated DMA handle 84 * @gfp: Allocation flags 85 * @attrs: Flags in the DMA_ATTR_* namespace. 86 * 87 * Managed dma_alloc_attrs(). Memory allocated using this function will be 88 * automatically released on driver detach. 89 * 90 * RETURNS: 91 * Pointer to allocated memory on success, NULL on failure. 92 */ 93 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, 94 gfp_t gfp, unsigned long attrs) 95 { 96 struct dma_devres *dr; 97 void *vaddr; 98 99 dr = devres_alloc(dmam_release, sizeof(*dr), gfp); 100 if (!dr) 101 return NULL; 102 103 vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs); 104 if (!vaddr) { 105 devres_free(dr); 106 return NULL; 107 } 108 109 dr->vaddr = vaddr; 110 dr->dma_handle = *dma_handle; 111 dr->size = size; 112 dr->attrs = attrs; 113 114 devres_add(dev, dr); 115 116 return vaddr; 117 } 118 EXPORT_SYMBOL(dmam_alloc_attrs); 119 120 static bool dma_go_direct(struct device *dev, dma_addr_t mask, 121 const struct dma_map_ops *ops) 122 { 123 if (use_dma_iommu(dev)) 124 return false; 125 126 if (likely(!ops)) 127 return true; 128 129 if (IS_ENABLED(CONFIG_DMA_OPS_BYPASS) && dev_dma_ops_bypass(dev)) 130 return min_not_zero(mask, dev->bus_dma_limit) >= 131 dma_direct_get_required_mask(dev); 132 return false; 133 } 134 135 136 /* 137 * Check if the devices uses a direct mapping for streaming DMA operations. 138 * This allows IOMMU drivers to set a bypass mode if the DMA mask is large 139 * enough. 140 */ 141 static inline bool dma_alloc_direct(struct device *dev, 142 const struct dma_map_ops *ops) 143 { 144 return dma_go_direct(dev, dev->coherent_dma_mask, ops); 145 } 146 147 static inline bool dma_map_direct(struct device *dev, 148 const struct dma_map_ops *ops) 149 { 150 return dma_go_direct(dev, *dev->dma_mask, ops); 151 } 152 153 dma_addr_t dma_map_phys(struct device *dev, phys_addr_t phys, size_t size, 154 enum dma_data_direction dir, unsigned long attrs) 155 { 156 const struct dma_map_ops *ops = get_dma_ops(dev); 157 bool is_mmio = attrs & DMA_ATTR_MMIO; 158 bool is_cc_shared = attrs & DMA_ATTR_CC_SHARED; 159 dma_addr_t addr = DMA_MAPPING_ERROR; 160 161 BUG_ON(!valid_dma_direction(dir)); 162 163 if (WARN_ON_ONCE(!dev->dma_mask)) 164 return DMA_MAPPING_ERROR; 165 166 if (!dev_is_dma_coherent(dev) && (attrs & DMA_ATTR_REQUIRE_COHERENT)) 167 return DMA_MAPPING_ERROR; 168 169 if (dma_map_direct(dev, ops) || 170 (!is_mmio && !is_cc_shared && 171 arch_dma_map_phys_direct(dev, phys + size))) 172 addr = dma_direct_map_phys(dev, phys, size, dir, attrs, true); 173 else if (is_cc_shared) 174 return DMA_MAPPING_ERROR; 175 else if (use_dma_iommu(dev)) 176 addr = iommu_dma_map_phys(dev, phys, size, dir, attrs); 177 else if (ops->map_phys) 178 addr = ops->map_phys(dev, phys, size, dir, attrs); 179 180 if (!is_mmio) 181 kmsan_handle_dma(phys, size, dir); 182 trace_dma_map_phys(dev, phys, addr, size, dir, attrs); 183 debug_dma_map_phys(dev, phys, size, dir, addr, attrs); 184 185 return addr; 186 } 187 EXPORT_SYMBOL_GPL(dma_map_phys); 188 189 dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, 190 size_t offset, size_t size, enum dma_data_direction dir, 191 unsigned long attrs) 192 { 193 phys_addr_t phys = page_to_phys(page) + offset; 194 195 if (unlikely(attrs & DMA_ATTR_MMIO)) 196 return DMA_MAPPING_ERROR; 197 198 if (IS_ENABLED(CONFIG_DMA_API_DEBUG) && 199 WARN_ON_ONCE(is_zone_device_page(page))) 200 return DMA_MAPPING_ERROR; 201 202 return dma_map_phys(dev, phys, size, dir, attrs); 203 } 204 EXPORT_SYMBOL(dma_map_page_attrs); 205 206 void dma_unmap_phys(struct device *dev, dma_addr_t addr, size_t size, 207 enum dma_data_direction dir, unsigned long attrs) 208 { 209 const struct dma_map_ops *ops = get_dma_ops(dev); 210 bool is_mmio = attrs & DMA_ATTR_MMIO; 211 bool is_cc_shared = attrs & DMA_ATTR_CC_SHARED; 212 213 BUG_ON(!valid_dma_direction(dir)); 214 215 if (dma_map_direct(dev, ops) || 216 (!is_mmio && !is_cc_shared && 217 arch_dma_unmap_phys_direct(dev, addr + size))) 218 dma_direct_unmap_phys(dev, addr, size, dir, attrs, true); 219 else if (is_cc_shared) 220 return; 221 else if (use_dma_iommu(dev)) 222 iommu_dma_unmap_phys(dev, addr, size, dir, attrs); 223 else if (ops->unmap_phys) 224 ops->unmap_phys(dev, addr, size, dir, attrs); 225 trace_dma_unmap_phys(dev, addr, size, dir, attrs); 226 debug_dma_unmap_phys(dev, addr, size, dir, attrs); 227 } 228 EXPORT_SYMBOL_GPL(dma_unmap_phys); 229 230 void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size, 231 enum dma_data_direction dir, unsigned long attrs) 232 { 233 if (unlikely(attrs & DMA_ATTR_MMIO)) 234 return; 235 236 dma_unmap_phys(dev, addr, size, dir, attrs); 237 } 238 EXPORT_SYMBOL(dma_unmap_page_attrs); 239 240 static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, 241 int nents, enum dma_data_direction dir, unsigned long attrs) 242 { 243 const struct dma_map_ops *ops = get_dma_ops(dev); 244 int ents; 245 246 BUG_ON(!valid_dma_direction(dir)); 247 248 if (!dev_is_dma_coherent(dev) && (attrs & DMA_ATTR_REQUIRE_COHERENT)) 249 return -EOPNOTSUPP; 250 251 if (WARN_ON_ONCE(!dev->dma_mask)) 252 return 0; 253 254 if (dma_map_direct(dev, ops) || 255 arch_dma_map_sg_direct(dev, sg, nents)) 256 ents = dma_direct_map_sg(dev, sg, nents, dir, attrs); 257 else if (use_dma_iommu(dev)) 258 ents = iommu_dma_map_sg(dev, sg, nents, dir, attrs); 259 else 260 ents = ops->map_sg(dev, sg, nents, dir, attrs); 261 262 if (ents > 0) { 263 kmsan_handle_dma_sg(sg, nents, dir); 264 trace_dma_map_sg(dev, sg, nents, ents, dir, attrs); 265 debug_dma_map_sg(dev, sg, nents, ents, dir, attrs); 266 } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && 267 ents != -EIO && ents != -EREMOTEIO)) { 268 trace_dma_map_sg_err(dev, sg, nents, ents, dir, attrs); 269 return -EIO; 270 } 271 272 return ents; 273 } 274 275 /** 276 * dma_map_sg_attrs - Map the given buffer for DMA 277 * @dev: The device for which to perform the DMA operation 278 * @sg: The sg_table object describing the buffer 279 * @nents: Number of entries to map 280 * @dir: DMA direction 281 * @attrs: Optional DMA attributes for the map operation 282 * 283 * Maps a buffer described by a scatterlist passed in the sg argument with 284 * nents segments for the @dir DMA operation by the @dev device. 285 * 286 * Returns the number of mapped entries (which can be less than nents) 287 * on success. Zero is returned for any error. 288 * 289 * dma_unmap_sg_attrs() should be used to unmap the buffer with the 290 * original sg and original nents (not the value returned by this funciton). 291 */ 292 unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, 293 int nents, enum dma_data_direction dir, unsigned long attrs) 294 { 295 int ret; 296 297 ret = __dma_map_sg_attrs(dev, sg, nents, dir, attrs); 298 if (ret < 0) 299 return 0; 300 return ret; 301 } 302 EXPORT_SYMBOL(dma_map_sg_attrs); 303 304 /** 305 * dma_map_sgtable - Map the given buffer for DMA 306 * @dev: The device for which to perform the DMA operation 307 * @sgt: The sg_table object describing the buffer 308 * @dir: DMA direction 309 * @attrs: Optional DMA attributes for the map operation 310 * 311 * Maps a buffer described by a scatterlist stored in the given sg_table 312 * object for the @dir DMA operation by the @dev device. After success, the 313 * ownership for the buffer is transferred to the DMA domain. One has to 314 * call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the 315 * ownership of the buffer back to the CPU domain before touching the 316 * buffer by the CPU. 317 * 318 * Returns 0 on success or a negative error code on error. The following 319 * error codes are supported with the given meaning: 320 * 321 * -EINVAL An invalid argument, unaligned access or other error 322 * in usage. Will not succeed if retried. 323 * -ENOMEM Insufficient resources (like memory or IOVA space) to 324 * complete the mapping. Should succeed if retried later. 325 * -EIO Legacy error code with an unknown meaning. eg. this is 326 * returned if a lower level call returned 327 * DMA_MAPPING_ERROR. 328 * -EREMOTEIO The DMA device cannot access P2PDMA memory specified 329 * in the sg_table. This will not succeed if retried. 330 */ 331 int dma_map_sgtable(struct device *dev, struct sg_table *sgt, 332 enum dma_data_direction dir, unsigned long attrs) 333 { 334 int nents; 335 336 nents = __dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); 337 if (nents < 0) 338 return nents; 339 sgt->nents = nents; 340 return 0; 341 } 342 EXPORT_SYMBOL_GPL(dma_map_sgtable); 343 344 void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, 345 int nents, enum dma_data_direction dir, 346 unsigned long attrs) 347 { 348 const struct dma_map_ops *ops = get_dma_ops(dev); 349 350 BUG_ON(!valid_dma_direction(dir)); 351 trace_dma_unmap_sg(dev, sg, nents, dir, attrs); 352 debug_dma_unmap_sg(dev, sg, nents, dir, attrs); 353 if (dma_map_direct(dev, ops) || 354 arch_dma_unmap_sg_direct(dev, sg, nents)) 355 dma_direct_unmap_sg(dev, sg, nents, dir, attrs); 356 else if (use_dma_iommu(dev)) 357 iommu_dma_unmap_sg(dev, sg, nents, dir, attrs); 358 else if (ops->unmap_sg) 359 ops->unmap_sg(dev, sg, nents, dir, attrs); 360 } 361 EXPORT_SYMBOL(dma_unmap_sg_attrs); 362 363 dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr, 364 size_t size, enum dma_data_direction dir, unsigned long attrs) 365 { 366 return dma_map_phys(dev, phys_addr, size, dir, attrs | DMA_ATTR_MMIO); 367 } 368 EXPORT_SYMBOL(dma_map_resource); 369 370 void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, 371 enum dma_data_direction dir, unsigned long attrs) 372 { 373 dma_unmap_phys(dev, addr, size, dir, attrs | DMA_ATTR_MMIO); 374 } 375 EXPORT_SYMBOL(dma_unmap_resource); 376 377 #ifdef CONFIG_DMA_NEED_SYNC 378 void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, 379 enum dma_data_direction dir) 380 { 381 const struct dma_map_ops *ops = get_dma_ops(dev); 382 383 BUG_ON(!valid_dma_direction(dir)); 384 if (dma_map_direct(dev, ops)) 385 dma_direct_sync_single_for_cpu(dev, addr, size, dir, true); 386 else if (use_dma_iommu(dev)) 387 iommu_dma_sync_single_for_cpu(dev, addr, size, dir); 388 else if (ops->sync_single_for_cpu) 389 ops->sync_single_for_cpu(dev, addr, size, dir); 390 trace_dma_sync_single_for_cpu(dev, addr, size, dir); 391 debug_dma_sync_single_for_cpu(dev, addr, size, dir); 392 } 393 EXPORT_SYMBOL(__dma_sync_single_for_cpu); 394 395 void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr, 396 size_t size, enum dma_data_direction dir) 397 { 398 const struct dma_map_ops *ops = get_dma_ops(dev); 399 400 BUG_ON(!valid_dma_direction(dir)); 401 if (dma_map_direct(dev, ops)) 402 dma_direct_sync_single_for_device(dev, addr, size, dir); 403 else if (use_dma_iommu(dev)) 404 iommu_dma_sync_single_for_device(dev, addr, size, dir); 405 else if (ops->sync_single_for_device) 406 ops->sync_single_for_device(dev, addr, size, dir); 407 trace_dma_sync_single_for_device(dev, addr, size, dir); 408 debug_dma_sync_single_for_device(dev, addr, size, dir); 409 } 410 EXPORT_SYMBOL(__dma_sync_single_for_device); 411 412 void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 413 int nelems, enum dma_data_direction dir) 414 { 415 const struct dma_map_ops *ops = get_dma_ops(dev); 416 417 BUG_ON(!valid_dma_direction(dir)); 418 if (dma_map_direct(dev, ops)) 419 dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir); 420 else if (use_dma_iommu(dev)) 421 iommu_dma_sync_sg_for_cpu(dev, sg, nelems, dir); 422 else if (ops->sync_sg_for_cpu) 423 ops->sync_sg_for_cpu(dev, sg, nelems, dir); 424 trace_dma_sync_sg_for_cpu(dev, sg, nelems, dir); 425 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir); 426 } 427 EXPORT_SYMBOL(__dma_sync_sg_for_cpu); 428 429 void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 430 int nelems, enum dma_data_direction dir) 431 { 432 const struct dma_map_ops *ops = get_dma_ops(dev); 433 434 BUG_ON(!valid_dma_direction(dir)); 435 if (dma_map_direct(dev, ops)) 436 dma_direct_sync_sg_for_device(dev, sg, nelems, dir); 437 else if (use_dma_iommu(dev)) 438 iommu_dma_sync_sg_for_device(dev, sg, nelems, dir); 439 else if (ops->sync_sg_for_device) 440 ops->sync_sg_for_device(dev, sg, nelems, dir); 441 trace_dma_sync_sg_for_device(dev, sg, nelems, dir); 442 debug_dma_sync_sg_for_device(dev, sg, nelems, dir); 443 } 444 EXPORT_SYMBOL(__dma_sync_sg_for_device); 445 446 bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr) 447 { 448 const struct dma_map_ops *ops = get_dma_ops(dev); 449 450 if (dma_map_direct(dev, ops)) 451 /* 452 * dma_skip_sync could've been reset on first SWIOTLB buffer 453 * mapping, but @dma_addr is not necessary an SWIOTLB buffer. 454 * In this case, fall back to more granular check. 455 */ 456 return dma_direct_need_sync(dev, dma_addr); 457 return true; 458 } 459 EXPORT_SYMBOL_GPL(__dma_need_sync); 460 461 /** 462 * dma_need_unmap - does this device need dma_unmap_* operations 463 * @dev: device to check 464 * 465 * If this function returns %false, drivers can skip calling dma_unmap_* after 466 * finishing an I/O. This function must be called after all mappings that might 467 * need to be unmapped have been performed. 468 */ 469 bool dma_need_unmap(struct device *dev) 470 { 471 if (!dma_map_direct(dev, get_dma_ops(dev))) 472 return true; 473 if (!dev_dma_skip_sync(dev)) 474 return true; 475 return IS_ENABLED(CONFIG_DMA_API_DEBUG); 476 } 477 EXPORT_SYMBOL_GPL(dma_need_unmap); 478 479 static void dma_setup_need_sync(struct device *dev) 480 { 481 const struct dma_map_ops *ops = get_dma_ops(dev); 482 483 if (dma_map_direct(dev, ops) || use_dma_iommu(dev)) 484 /* 485 * dma_skip_sync will be reset to %false on first SWIOTLB buffer 486 * mapping, if any. During the device initialization, it's 487 * enough to check only for the DMA coherence. 488 */ 489 dev_assign_dma_skip_sync(dev, dev_is_dma_coherent(dev)); 490 else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu && 491 !ops->sync_sg_for_device && !ops->sync_sg_for_cpu) 492 /* 493 * Synchronization is not possible when none of DMA sync ops 494 * is set. 495 */ 496 dev_set_dma_skip_sync(dev); 497 else 498 dev_clear_dma_skip_sync(dev); 499 } 500 #else /* !CONFIG_DMA_NEED_SYNC */ 501 static inline void dma_setup_need_sync(struct device *dev) { } 502 #endif /* !CONFIG_DMA_NEED_SYNC */ 503 504 /* 505 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems 506 * that the intention is to allow exporting memory allocated via the 507 * coherent DMA APIs through the dma_buf API, which only accepts a 508 * scattertable. This presents a couple of problems: 509 * 1. Not all memory allocated via the coherent DMA APIs is backed by 510 * a struct page 511 * 2. Passing coherent DMA memory into the streaming APIs is not allowed 512 * as we will try to flush the memory through a different alias to that 513 * actually being used (and the flushes are redundant.) 514 */ 515 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, 516 void *cpu_addr, dma_addr_t dma_addr, size_t size, 517 unsigned long attrs) 518 { 519 const struct dma_map_ops *ops = get_dma_ops(dev); 520 521 if (dma_alloc_direct(dev, ops)) 522 return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr, 523 size, attrs); 524 if (use_dma_iommu(dev)) 525 return iommu_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr, 526 size, attrs); 527 if (!ops->get_sgtable) 528 return -ENXIO; 529 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs); 530 } 531 EXPORT_SYMBOL(dma_get_sgtable_attrs); 532 533 #ifdef CONFIG_MMU 534 /* 535 * Return the page attributes used for mapping dma_alloc_* memory, either in 536 * kernel space if remapping is needed, or to userspace through dma_mmap_*. 537 */ 538 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs) 539 { 540 if (dev_is_dma_coherent(dev)) 541 return prot; 542 #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE 543 if (attrs & DMA_ATTR_WRITE_COMBINE) 544 return pgprot_writecombine(prot); 545 #endif 546 return pgprot_dmacoherent(prot); 547 } 548 #endif /* CONFIG_MMU */ 549 550 /** 551 * dma_can_mmap - check if a given device supports dma_mmap_* 552 * @dev: device to check 553 * 554 * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to 555 * map DMA allocations to userspace. 556 */ 557 bool dma_can_mmap(struct device *dev) 558 { 559 const struct dma_map_ops *ops = get_dma_ops(dev); 560 561 if (dma_alloc_direct(dev, ops)) 562 return dma_direct_can_mmap(dev); 563 if (use_dma_iommu(dev)) 564 return true; 565 return ops->mmap != NULL; 566 } 567 EXPORT_SYMBOL_GPL(dma_can_mmap); 568 569 /** 570 * dma_mmap_attrs - map a coherent DMA allocation into user space 571 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 572 * @vma: vm_area_struct describing requested user mapping 573 * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs 574 * @dma_addr: device-view address returned from dma_alloc_attrs 575 * @size: size of memory originally requested in dma_alloc_attrs 576 * @attrs: attributes of mapping properties requested in dma_alloc_attrs 577 * 578 * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user 579 * space. The coherent DMA buffer must not be freed by the driver until the 580 * user space mapping has been released. 581 */ 582 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, 583 void *cpu_addr, dma_addr_t dma_addr, size_t size, 584 unsigned long attrs) 585 { 586 const struct dma_map_ops *ops = get_dma_ops(dev); 587 588 if (dma_alloc_direct(dev, ops)) 589 return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size, 590 attrs); 591 if (use_dma_iommu(dev)) 592 return iommu_dma_mmap(dev, vma, cpu_addr, dma_addr, size, 593 attrs); 594 if (!ops->mmap) 595 return -ENXIO; 596 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs); 597 } 598 EXPORT_SYMBOL(dma_mmap_attrs); 599 600 u64 dma_get_required_mask(struct device *dev) 601 { 602 const struct dma_map_ops *ops = get_dma_ops(dev); 603 604 if (dma_alloc_direct(dev, ops)) 605 return dma_direct_get_required_mask(dev); 606 607 if (use_dma_iommu(dev)) 608 return DMA_BIT_MASK(32); 609 610 if (ops->get_required_mask) 611 return ops->get_required_mask(dev); 612 613 /* 614 * We require every DMA ops implementation to at least support a 32-bit 615 * DMA mask (and use bounce buffering if that isn't supported in 616 * hardware). As the direct mapping code has its own routine to 617 * actually report an optimal mask we default to 32-bit here as that 618 * is the right thing for most IOMMUs, and at least not actively 619 * harmful in general. 620 */ 621 return DMA_BIT_MASK(32); 622 } 623 EXPORT_SYMBOL_GPL(dma_get_required_mask); 624 625 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, 626 gfp_t flag, unsigned long attrs) 627 { 628 const struct dma_map_ops *ops = get_dma_ops(dev); 629 void *cpu_addr; 630 631 WARN_ON_ONCE(!dev->coherent_dma_mask); 632 633 /* 634 * DMA allocations can never be turned back into a page pointer, so 635 * requesting compound pages doesn't make sense (and can't even be 636 * supported at all by various backends). 637 */ 638 if (WARN_ON_ONCE(flag & __GFP_COMP)) 639 return NULL; 640 641 if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) { 642 trace_dma_alloc(dev, cpu_addr, *dma_handle, size, 643 DMA_BIDIRECTIONAL, flag, attrs); 644 return cpu_addr; 645 } 646 647 /* let the implementation decide on the zone to allocate from: */ 648 flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM); 649 650 if (dma_alloc_direct(dev, ops) || arch_dma_alloc_direct(dev)) { 651 cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs); 652 } else if (use_dma_iommu(dev)) { 653 cpu_addr = iommu_dma_alloc(dev, size, dma_handle, flag, attrs); 654 } else if (ops->alloc) { 655 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs); 656 } else { 657 trace_dma_alloc(dev, NULL, 0, size, DMA_BIDIRECTIONAL, flag, 658 attrs); 659 return NULL; 660 } 661 662 trace_dma_alloc(dev, cpu_addr, *dma_handle, size, DMA_BIDIRECTIONAL, 663 flag, attrs); 664 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr, attrs); 665 return cpu_addr; 666 } 667 EXPORT_SYMBOL(dma_alloc_attrs); 668 669 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr, 670 dma_addr_t dma_handle, unsigned long attrs) 671 { 672 const struct dma_map_ops *ops = get_dma_ops(dev); 673 674 if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr)) 675 return; 676 /* 677 * On non-coherent platforms which implement DMA-coherent buffers via 678 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting 679 * this far in IRQ context is a) at risk of a BUG_ON() or trying to 680 * sleep on some machines, and b) an indication that the driver is 681 * probably misusing the coherent API anyway. 682 */ 683 WARN_ON(irqs_disabled()); 684 685 trace_dma_free(dev, cpu_addr, dma_handle, size, DMA_BIDIRECTIONAL, 686 attrs); 687 if (!cpu_addr) 688 return; 689 690 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle, attrs); 691 if (dma_alloc_direct(dev, ops) || arch_dma_free_direct(dev, dma_handle)) 692 dma_direct_free(dev, size, cpu_addr, dma_handle, attrs); 693 else if (use_dma_iommu(dev)) 694 iommu_dma_free(dev, size, cpu_addr, dma_handle, attrs); 695 else if (ops->free) 696 ops->free(dev, size, cpu_addr, dma_handle, attrs); 697 } 698 EXPORT_SYMBOL(dma_free_attrs); 699 700 static struct page *__dma_alloc_pages(struct device *dev, size_t size, 701 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp) 702 { 703 const struct dma_map_ops *ops = get_dma_ops(dev); 704 705 if (WARN_ON_ONCE(!dev->coherent_dma_mask)) 706 return NULL; 707 if (WARN_ON_ONCE(gfp & (__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM))) 708 return NULL; 709 if (WARN_ON_ONCE(gfp & __GFP_COMP)) 710 return NULL; 711 712 size = PAGE_ALIGN(size); 713 if (dma_alloc_direct(dev, ops)) 714 return dma_direct_alloc_pages(dev, size, dma_handle, dir, gfp); 715 if (use_dma_iommu(dev)) 716 return dma_common_alloc_pages(dev, size, dma_handle, dir, gfp); 717 if (!ops->alloc_pages_op) 718 return NULL; 719 return ops->alloc_pages_op(dev, size, dma_handle, dir, gfp); 720 } 721 722 struct page *dma_alloc_pages(struct device *dev, size_t size, 723 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp) 724 { 725 struct page *page = __dma_alloc_pages(dev, size, dma_handle, dir, gfp); 726 727 if (page) { 728 trace_dma_alloc_pages(dev, page_to_virt(page), *dma_handle, 729 size, dir, gfp, 0); 730 debug_dma_alloc_pages(dev, page, size, dir, *dma_handle); 731 } else { 732 trace_dma_alloc_pages(dev, NULL, 0, size, dir, gfp, 0); 733 } 734 return page; 735 } 736 EXPORT_SYMBOL_GPL(dma_alloc_pages); 737 738 static void __dma_free_pages(struct device *dev, size_t size, struct page *page, 739 dma_addr_t dma_handle, enum dma_data_direction dir) 740 { 741 const struct dma_map_ops *ops = get_dma_ops(dev); 742 743 size = PAGE_ALIGN(size); 744 if (dma_alloc_direct(dev, ops)) 745 dma_direct_free_pages(dev, size, page, dma_handle, dir); 746 else if (use_dma_iommu(dev)) 747 dma_common_free_pages(dev, size, page, dma_handle, dir); 748 else if (ops->free_pages) 749 ops->free_pages(dev, size, page, dma_handle, dir); 750 } 751 752 void dma_free_pages(struct device *dev, size_t size, struct page *page, 753 dma_addr_t dma_handle, enum dma_data_direction dir) 754 { 755 trace_dma_free_pages(dev, page_to_virt(page), dma_handle, size, dir, 0); 756 debug_dma_free_pages(dev, page, size, dir, dma_handle); 757 __dma_free_pages(dev, size, page, dma_handle, dir); 758 } 759 EXPORT_SYMBOL_GPL(dma_free_pages); 760 761 int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma, 762 size_t size, struct page *page) 763 { 764 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; 765 766 if (vma->vm_pgoff >= count || vma_pages(vma) > count - vma->vm_pgoff) 767 return -ENXIO; 768 return remap_pfn_range(vma, vma->vm_start, 769 page_to_pfn(page) + vma->vm_pgoff, 770 vma_pages(vma) << PAGE_SHIFT, vma->vm_page_prot); 771 } 772 EXPORT_SYMBOL_GPL(dma_mmap_pages); 773 774 static struct sg_table *alloc_single_sgt(struct device *dev, size_t size, 775 enum dma_data_direction dir, gfp_t gfp) 776 { 777 struct sg_table *sgt; 778 struct page *page; 779 780 sgt = kmalloc_obj(*sgt, gfp); 781 if (!sgt) 782 return NULL; 783 if (sg_alloc_table(sgt, 1, gfp)) 784 goto out_free_sgt; 785 page = __dma_alloc_pages(dev, size, &sgt->sgl->dma_address, dir, gfp); 786 if (!page) 787 goto out_free_table; 788 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); 789 sg_dma_len(sgt->sgl) = sgt->sgl->length; 790 return sgt; 791 out_free_table: 792 sg_free_table(sgt); 793 out_free_sgt: 794 kfree(sgt); 795 return NULL; 796 } 797 798 struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size, 799 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs) 800 { 801 struct sg_table *sgt; 802 803 if (WARN_ON_ONCE(attrs & ~DMA_ATTR_ALLOC_SINGLE_PAGES)) 804 return NULL; 805 if (WARN_ON_ONCE(gfp & __GFP_COMP)) 806 return NULL; 807 808 if (use_dma_iommu(dev)) 809 sgt = iommu_dma_alloc_noncontiguous(dev, size, dir, gfp, attrs); 810 else 811 sgt = alloc_single_sgt(dev, size, dir, gfp); 812 813 if (sgt) { 814 sgt->nents = 1; 815 trace_dma_alloc_sgt(dev, sgt, size, dir, gfp, attrs); 816 debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); 817 } else { 818 trace_dma_alloc_sgt_err(dev, NULL, 0, size, dir, gfp, attrs); 819 } 820 return sgt; 821 } 822 EXPORT_SYMBOL_GPL(dma_alloc_noncontiguous); 823 824 static void free_single_sgt(struct device *dev, size_t size, 825 struct sg_table *sgt, enum dma_data_direction dir) 826 { 827 __dma_free_pages(dev, size, sg_page(sgt->sgl), sgt->sgl->dma_address, 828 dir); 829 sg_free_table(sgt); 830 kfree(sgt); 831 } 832 833 void dma_free_noncontiguous(struct device *dev, size_t size, 834 struct sg_table *sgt, enum dma_data_direction dir) 835 { 836 trace_dma_free_sgt(dev, sgt, size, dir); 837 debug_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir, 0); 838 839 if (use_dma_iommu(dev)) 840 iommu_dma_free_noncontiguous(dev, size, sgt, dir); 841 else 842 free_single_sgt(dev, size, sgt, dir); 843 } 844 EXPORT_SYMBOL_GPL(dma_free_noncontiguous); 845 846 void *dma_vmap_noncontiguous(struct device *dev, size_t size, 847 struct sg_table *sgt) 848 { 849 850 if (use_dma_iommu(dev)) 851 return iommu_dma_vmap_noncontiguous(dev, size, sgt); 852 853 return page_address(sg_page(sgt->sgl)); 854 } 855 EXPORT_SYMBOL_GPL(dma_vmap_noncontiguous); 856 857 void dma_vunmap_noncontiguous(struct device *dev, void *vaddr) 858 { 859 if (use_dma_iommu(dev)) 860 iommu_dma_vunmap_noncontiguous(dev, vaddr); 861 } 862 EXPORT_SYMBOL_GPL(dma_vunmap_noncontiguous); 863 864 int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, 865 size_t size, struct sg_table *sgt) 866 { 867 if (use_dma_iommu(dev)) 868 return iommu_dma_mmap_noncontiguous(dev, vma, size, sgt); 869 return dma_mmap_pages(dev, vma, size, sg_page(sgt->sgl)); 870 } 871 EXPORT_SYMBOL_GPL(dma_mmap_noncontiguous); 872 873 static int dma_supported(struct device *dev, u64 mask) 874 { 875 const struct dma_map_ops *ops = get_dma_ops(dev); 876 877 if (use_dma_iommu(dev)) { 878 if (WARN_ON(ops)) 879 return false; 880 return true; 881 } 882 883 /* 884 * ->dma_supported sets and clears the bypass flag, so ignore it here 885 * and always call into the method if there is one. 886 */ 887 if (ops) { 888 if (!ops->dma_supported) 889 return true; 890 return ops->dma_supported(dev, mask); 891 } 892 893 return dma_direct_supported(dev, mask); 894 } 895 896 bool dma_pci_p2pdma_supported(struct device *dev) 897 { 898 const struct dma_map_ops *ops = get_dma_ops(dev); 899 900 /* 901 * Note: dma_ops_bypass is not checked here because P2PDMA should 902 * not be used with dma mapping ops that do not have support even 903 * if the specific device is bypassing them. 904 */ 905 906 /* if ops is not set, dma direct and default IOMMU support P2PDMA */ 907 return !ops; 908 } 909 EXPORT_SYMBOL_GPL(dma_pci_p2pdma_supported); 910 911 int dma_set_mask(struct device *dev, u64 mask) 912 { 913 /* 914 * Truncate the mask to the actually supported dma_addr_t width to 915 * avoid generating unsupportable addresses. 916 */ 917 mask = (dma_addr_t)mask; 918 919 if (!dev->dma_mask || !dma_supported(dev, mask)) 920 return -EIO; 921 922 arch_dma_set_mask(dev, mask); 923 *dev->dma_mask = mask; 924 dma_setup_need_sync(dev); 925 926 return 0; 927 } 928 EXPORT_SYMBOL(dma_set_mask); 929 930 int dma_set_coherent_mask(struct device *dev, u64 mask) 931 { 932 /* 933 * Truncate the mask to the actually supported dma_addr_t width to 934 * avoid generating unsupportable addresses. 935 */ 936 mask = (dma_addr_t)mask; 937 938 if (!dma_supported(dev, mask)) 939 return -EIO; 940 941 dev->coherent_dma_mask = mask; 942 return 0; 943 } 944 EXPORT_SYMBOL(dma_set_coherent_mask); 945 946 static bool __dma_addressing_limited(struct device *dev) 947 { 948 const struct dma_map_ops *ops = get_dma_ops(dev); 949 950 if (min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) < 951 dma_get_required_mask(dev)) 952 return true; 953 954 if (unlikely(ops) || use_dma_iommu(dev)) 955 return false; 956 return !dma_direct_all_ram_mapped(dev); 957 } 958 959 /** 960 * dma_addressing_limited - return if the device is addressing limited 961 * @dev: device to check 962 * 963 * Return %true if the devices DMA mask is too small to address all memory in 964 * the system, else %false. Lack of addressing bits is the prime reason for 965 * bounce buffering, but might not be the only one. 966 */ 967 bool dma_addressing_limited(struct device *dev) 968 { 969 if (!__dma_addressing_limited(dev)) 970 return false; 971 972 dev_dbg(dev, "device is DMA addressing limited\n"); 973 return true; 974 } 975 EXPORT_SYMBOL_GPL(dma_addressing_limited); 976 977 size_t dma_max_mapping_size(struct device *dev) 978 { 979 const struct dma_map_ops *ops = get_dma_ops(dev); 980 size_t size = SIZE_MAX; 981 982 if (dma_map_direct(dev, ops)) 983 size = dma_direct_max_mapping_size(dev); 984 else if (use_dma_iommu(dev)) 985 size = iommu_dma_max_mapping_size(dev); 986 else if (ops && ops->max_mapping_size) 987 size = ops->max_mapping_size(dev); 988 989 return size; 990 } 991 EXPORT_SYMBOL_GPL(dma_max_mapping_size); 992 993 size_t dma_opt_mapping_size(struct device *dev) 994 { 995 const struct dma_map_ops *ops = get_dma_ops(dev); 996 size_t size = SIZE_MAX; 997 998 if (use_dma_iommu(dev)) 999 size = iommu_dma_opt_mapping_size(); 1000 else if (ops && ops->opt_mapping_size) 1001 size = ops->opt_mapping_size(); 1002 1003 return min(dma_max_mapping_size(dev), size); 1004 } 1005 EXPORT_SYMBOL_GPL(dma_opt_mapping_size); 1006 1007 unsigned long dma_get_merge_boundary(struct device *dev) 1008 { 1009 const struct dma_map_ops *ops = get_dma_ops(dev); 1010 1011 if (use_dma_iommu(dev)) 1012 return iommu_dma_get_merge_boundary(dev); 1013 1014 if (!ops || !ops->get_merge_boundary) 1015 return 0; /* can't merge */ 1016 1017 return ops->get_merge_boundary(dev); 1018 } 1019 EXPORT_SYMBOL_GPL(dma_get_merge_boundary); 1020