1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018-2020 Christoph Hellwig. 4 * 5 * DMA operations that map physical memory directly without using an IOMMU. 6 */ 7 #include <linux/memblock.h> /* for max_pfn */ 8 #include <linux/export.h> 9 #include <linux/mm.h> 10 #include <linux/dma-map-ops.h> 11 #include <linux/scatterlist.h> 12 #include <linux/pfn.h> 13 #include <linux/vmalloc.h> 14 #include <linux/set_memory.h> 15 #include <linux/slab.h> 16 #include "direct.h" 17 18 /* 19 * Most architectures use ZONE_DMA for the first 16 Megabytes, but some use 20 * it for entirely different regions. In that case the arch code needs to 21 * override the variable below for dma-direct to work properly. 22 */ 23 unsigned int zone_dma_bits __ro_after_init = 24; 24 25 static inline dma_addr_t phys_to_dma_direct(struct device *dev, 26 phys_addr_t phys) 27 { 28 if (force_dma_unencrypted(dev)) 29 return phys_to_dma_unencrypted(dev, phys); 30 return phys_to_dma(dev, phys); 31 } 32 33 static inline struct page *dma_direct_to_page(struct device *dev, 34 dma_addr_t dma_addr) 35 { 36 return pfn_to_page(PHYS_PFN(dma_to_phys(dev, dma_addr))); 37 } 38 39 u64 dma_direct_get_required_mask(struct device *dev) 40 { 41 phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT; 42 u64 max_dma = phys_to_dma_direct(dev, phys); 43 44 return (1ULL << (fls64(max_dma) - 1)) * 2 - 1; 45 } 46 47 static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask, 48 u64 *phys_limit) 49 { 50 u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit); 51 52 /* 53 * Optimistically try the zone that the physical address mask falls 54 * into first. If that returns memory that isn't actually addressable 55 * we will fallback to the next lower zone and try again. 56 * 57 * Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding 58 * zones. 59 */ 60 *phys_limit = dma_to_phys(dev, dma_limit); 61 if (*phys_limit <= DMA_BIT_MASK(zone_dma_bits)) 62 return GFP_DMA; 63 if (*phys_limit <= DMA_BIT_MASK(32)) 64 return GFP_DMA32; 65 return 0; 66 } 67 68 static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) 69 { 70 dma_addr_t dma_addr = phys_to_dma_direct(dev, phys); 71 72 if (dma_addr == DMA_MAPPING_ERROR) 73 return false; 74 return dma_addr + size - 1 <= 75 min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit); 76 } 77 78 static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size) 79 { 80 if (!force_dma_unencrypted(dev)) 81 return 0; 82 return set_memory_decrypted((unsigned long)vaddr, 1 << get_order(size)); 83 } 84 85 static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size) 86 { 87 if (!force_dma_unencrypted(dev)) 88 return 0; 89 return set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size)); 90 } 91 92 static void __dma_direct_free_pages(struct device *dev, struct page *page, 93 size_t size) 94 { 95 if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) && 96 swiotlb_free(dev, page, size)) 97 return; 98 dma_free_contiguous(dev, page, size); 99 } 100 101 static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, 102 gfp_t gfp, bool allow_highmem) 103 { 104 int node = dev_to_node(dev); 105 struct page *page = NULL; 106 u64 phys_limit; 107 108 WARN_ON_ONCE(!PAGE_ALIGNED(size)); 109 110 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, 111 &phys_limit); 112 if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) && 113 is_swiotlb_for_alloc(dev)) { 114 page = swiotlb_alloc(dev, size); 115 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { 116 __dma_direct_free_pages(dev, page, size); 117 return NULL; 118 } 119 return page; 120 } 121 122 page = dma_alloc_contiguous(dev, size, gfp); 123 if (page) { 124 if (!dma_coherent_ok(dev, page_to_phys(page), size) || 125 (!allow_highmem && PageHighMem(page))) { 126 dma_free_contiguous(dev, page, size); 127 page = NULL; 128 } 129 } 130 again: 131 if (!page) 132 page = alloc_pages_node(node, gfp, get_order(size)); 133 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { 134 dma_free_contiguous(dev, page, size); 135 page = NULL; 136 137 if (IS_ENABLED(CONFIG_ZONE_DMA32) && 138 phys_limit < DMA_BIT_MASK(64) && 139 !(gfp & (GFP_DMA32 | GFP_DMA))) { 140 gfp |= GFP_DMA32; 141 goto again; 142 } 143 144 if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) { 145 gfp = (gfp & ~GFP_DMA32) | GFP_DMA; 146 goto again; 147 } 148 } 149 150 return page; 151 } 152 153 static void *dma_direct_alloc_from_pool(struct device *dev, size_t size, 154 dma_addr_t *dma_handle, gfp_t gfp) 155 { 156 struct page *page; 157 u64 phys_mask; 158 void *ret; 159 160 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, 161 &phys_mask); 162 page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok); 163 if (!page) 164 return NULL; 165 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); 166 return ret; 167 } 168 169 static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size, 170 dma_addr_t *dma_handle, gfp_t gfp) 171 { 172 struct page *page; 173 174 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true); 175 if (!page) 176 return NULL; 177 178 /* remove any dirty cache lines on the kernel alias */ 179 if (!PageHighMem(page)) 180 arch_dma_prep_coherent(page, size); 181 182 /* return the page pointer as the opaque cookie */ 183 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); 184 return page; 185 } 186 187 void *dma_direct_alloc(struct device *dev, size_t size, 188 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 189 { 190 struct page *page; 191 void *ret; 192 193 size = PAGE_ALIGN(size); 194 if (attrs & DMA_ATTR_NO_WARN) 195 gfp |= __GFP_NOWARN; 196 197 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && 198 !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) 199 return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp); 200 201 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && 202 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && 203 !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && 204 !dev_is_dma_coherent(dev) && 205 !is_swiotlb_for_alloc(dev)) 206 return arch_dma_alloc(dev, size, dma_handle, gfp, attrs); 207 208 if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && 209 !dev_is_dma_coherent(dev)) 210 return dma_alloc_from_global_coherent(dev, size, dma_handle); 211 212 /* 213 * Remapping or decrypting memory may block. If either is required and 214 * we can't block, allocate the memory from the atomic pools. 215 * If restricted DMA (i.e., is_swiotlb_for_alloc) is required, one must 216 * set up another device coherent pool by shared-dma-pool and use 217 * dma_alloc_from_dev_coherent instead. 218 */ 219 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && 220 !gfpflags_allow_blocking(gfp) && 221 (force_dma_unencrypted(dev) || 222 (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && 223 !dev_is_dma_coherent(dev))) && 224 !is_swiotlb_for_alloc(dev)) 225 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); 226 227 /* we always manually zero the memory once we are done */ 228 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true); 229 if (!page) 230 return NULL; 231 232 if ((IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && 233 !dev_is_dma_coherent(dev)) || 234 (IS_ENABLED(CONFIG_DMA_REMAP) && PageHighMem(page))) { 235 /* remove any dirty cache lines on the kernel alias */ 236 arch_dma_prep_coherent(page, size); 237 238 /* create a coherent mapping */ 239 ret = dma_common_contiguous_remap(page, size, 240 dma_pgprot(dev, PAGE_KERNEL, attrs), 241 __builtin_return_address(0)); 242 if (!ret) 243 goto out_free_pages; 244 memset(ret, 0, size); 245 goto done; 246 } 247 248 if (PageHighMem(page)) { 249 /* 250 * Depending on the cma= arguments and per-arch setup 251 * dma_alloc_contiguous could return highmem pages. 252 * Without remapping there is no way to return them here, 253 * so log an error and fail. 254 */ 255 dev_info(dev, "Rejecting highmem page from CMA.\n"); 256 goto out_free_pages; 257 } 258 259 ret = page_address(page); 260 if (dma_set_decrypted(dev, ret, size)) 261 goto out_free_pages; 262 memset(ret, 0, size); 263 264 if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && 265 !dev_is_dma_coherent(dev)) { 266 arch_dma_prep_coherent(page, size); 267 ret = arch_dma_set_uncached(ret, size); 268 if (IS_ERR(ret)) 269 goto out_encrypt_pages; 270 } 271 done: 272 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); 273 return ret; 274 275 out_encrypt_pages: 276 /* If memory cannot be re-encrypted, it must be leaked */ 277 if (dma_set_encrypted(dev, page_address(page), size)) 278 return NULL; 279 out_free_pages: 280 __dma_direct_free_pages(dev, page, size); 281 return NULL; 282 } 283 284 void dma_direct_free(struct device *dev, size_t size, 285 void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) 286 { 287 unsigned int page_order = get_order(size); 288 289 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && 290 !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) { 291 /* cpu_addr is a struct page cookie, not a kernel address */ 292 dma_free_contiguous(dev, cpu_addr, size); 293 return; 294 } 295 296 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && 297 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && 298 !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && 299 !dev_is_dma_coherent(dev) && 300 !is_swiotlb_for_alloc(dev)) { 301 arch_dma_free(dev, size, cpu_addr, dma_addr, attrs); 302 return; 303 } 304 305 if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && 306 !dev_is_dma_coherent(dev)) { 307 if (!dma_release_from_global_coherent(page_order, cpu_addr)) 308 WARN_ON_ONCE(1); 309 return; 310 } 311 312 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ 313 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && 314 dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size))) 315 return; 316 317 if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) { 318 vunmap(cpu_addr); 319 } else { 320 if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED)) 321 arch_dma_clear_uncached(cpu_addr, size); 322 dma_set_encrypted(dev, cpu_addr, 1 << page_order); 323 } 324 325 __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size); 326 } 327 328 struct page *dma_direct_alloc_pages(struct device *dev, size_t size, 329 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp) 330 { 331 struct page *page; 332 void *ret; 333 334 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && 335 force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp) && 336 !is_swiotlb_for_alloc(dev)) 337 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); 338 339 page = __dma_direct_alloc_pages(dev, size, gfp, false); 340 if (!page) 341 return NULL; 342 343 ret = page_address(page); 344 if (dma_set_decrypted(dev, ret, size)) 345 goto out_free_pages; 346 memset(ret, 0, size); 347 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); 348 return page; 349 out_free_pages: 350 __dma_direct_free_pages(dev, page, size); 351 return NULL; 352 } 353 354 void dma_direct_free_pages(struct device *dev, size_t size, 355 struct page *page, dma_addr_t dma_addr, 356 enum dma_data_direction dir) 357 { 358 unsigned int page_order = get_order(size); 359 void *vaddr = page_address(page); 360 361 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ 362 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && 363 dma_free_from_pool(dev, vaddr, size)) 364 return; 365 366 dma_set_encrypted(dev, vaddr, 1 << page_order); 367 __dma_direct_free_pages(dev, page, size); 368 } 369 370 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ 371 defined(CONFIG_SWIOTLB) 372 void dma_direct_sync_sg_for_device(struct device *dev, 373 struct scatterlist *sgl, int nents, enum dma_data_direction dir) 374 { 375 struct scatterlist *sg; 376 int i; 377 378 for_each_sg(sgl, sg, nents, i) { 379 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); 380 381 if (unlikely(is_swiotlb_buffer(dev, paddr))) 382 swiotlb_sync_single_for_device(dev, paddr, sg->length, 383 dir); 384 385 if (!dev_is_dma_coherent(dev)) 386 arch_sync_dma_for_device(paddr, sg->length, 387 dir); 388 } 389 } 390 #endif 391 392 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ 393 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \ 394 defined(CONFIG_SWIOTLB) 395 void dma_direct_sync_sg_for_cpu(struct device *dev, 396 struct scatterlist *sgl, int nents, enum dma_data_direction dir) 397 { 398 struct scatterlist *sg; 399 int i; 400 401 for_each_sg(sgl, sg, nents, i) { 402 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); 403 404 if (!dev_is_dma_coherent(dev)) 405 arch_sync_dma_for_cpu(paddr, sg->length, dir); 406 407 if (unlikely(is_swiotlb_buffer(dev, paddr))) 408 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, 409 dir); 410 411 if (dir == DMA_FROM_DEVICE) 412 arch_dma_mark_clean(paddr, sg->length); 413 } 414 415 if (!dev_is_dma_coherent(dev)) 416 arch_sync_dma_for_cpu_all(); 417 } 418 419 void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, 420 int nents, enum dma_data_direction dir, unsigned long attrs) 421 { 422 struct scatterlist *sg; 423 int i; 424 425 for_each_sg(sgl, sg, nents, i) 426 dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, 427 attrs); 428 } 429 #endif 430 431 int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, 432 enum dma_data_direction dir, unsigned long attrs) 433 { 434 int i; 435 struct scatterlist *sg; 436 437 for_each_sg(sgl, sg, nents, i) { 438 sg->dma_address = dma_direct_map_page(dev, sg_page(sg), 439 sg->offset, sg->length, dir, attrs); 440 if (sg->dma_address == DMA_MAPPING_ERROR) 441 goto out_unmap; 442 sg_dma_len(sg) = sg->length; 443 } 444 445 return nents; 446 447 out_unmap: 448 dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); 449 return -EIO; 450 } 451 452 dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, 453 size_t size, enum dma_data_direction dir, unsigned long attrs) 454 { 455 dma_addr_t dma_addr = paddr; 456 457 if (unlikely(!dma_capable(dev, dma_addr, size, false))) { 458 dev_err_once(dev, 459 "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n", 460 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit); 461 WARN_ON_ONCE(1); 462 return DMA_MAPPING_ERROR; 463 } 464 465 return dma_addr; 466 } 467 468 int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt, 469 void *cpu_addr, dma_addr_t dma_addr, size_t size, 470 unsigned long attrs) 471 { 472 struct page *page = dma_direct_to_page(dev, dma_addr); 473 int ret; 474 475 ret = sg_alloc_table(sgt, 1, GFP_KERNEL); 476 if (!ret) 477 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); 478 return ret; 479 } 480 481 bool dma_direct_can_mmap(struct device *dev) 482 { 483 return dev_is_dma_coherent(dev) || 484 IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP); 485 } 486 487 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, 488 void *cpu_addr, dma_addr_t dma_addr, size_t size, 489 unsigned long attrs) 490 { 491 unsigned long user_count = vma_pages(vma); 492 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; 493 unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr)); 494 int ret = -ENXIO; 495 496 vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs); 497 498 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) 499 return ret; 500 if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret)) 501 return ret; 502 503 if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff) 504 return -ENXIO; 505 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, 506 user_count << PAGE_SHIFT, vma->vm_page_prot); 507 } 508 509 int dma_direct_supported(struct device *dev, u64 mask) 510 { 511 u64 min_mask = (max_pfn - 1) << PAGE_SHIFT; 512 513 /* 514 * Because 32-bit DMA masks are so common we expect every architecture 515 * to be able to satisfy them - either by not supporting more physical 516 * memory, or by providing a ZONE_DMA32. If neither is the case, the 517 * architecture needs to use an IOMMU instead of the direct mapping. 518 */ 519 if (mask >= DMA_BIT_MASK(32)) 520 return 1; 521 522 /* 523 * This check needs to be against the actual bit mask value, so use 524 * phys_to_dma_unencrypted() here so that the SME encryption mask isn't 525 * part of the check. 526 */ 527 if (IS_ENABLED(CONFIG_ZONE_DMA)) 528 min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits)); 529 return mask >= phys_to_dma_unencrypted(dev, min_mask); 530 } 531 532 size_t dma_direct_max_mapping_size(struct device *dev) 533 { 534 /* If SWIOTLB is active, use its maximum mapping size */ 535 if (is_swiotlb_active(dev) && 536 (dma_addressing_limited(dev) || is_swiotlb_force_bounce(dev))) 537 return swiotlb_max_mapping_size(dev); 538 return SIZE_MAX; 539 } 540 541 bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr) 542 { 543 return !dev_is_dma_coherent(dev) || 544 is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr)); 545 } 546 547 /** 548 * dma_direct_set_offset - Assign scalar offset for a single DMA range. 549 * @dev: device pointer; needed to "own" the alloced memory. 550 * @cpu_start: beginning of memory region covered by this offset. 551 * @dma_start: beginning of DMA/PCI region covered by this offset. 552 * @size: size of the region. 553 * 554 * This is for the simple case of a uniform offset which cannot 555 * be discovered by "dma-ranges". 556 * 557 * It returns -ENOMEM if out of memory, -EINVAL if a map 558 * already exists, 0 otherwise. 559 * 560 * Note: any call to this from a driver is a bug. The mapping needs 561 * to be described by the device tree or other firmware interfaces. 562 */ 563 int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start, 564 dma_addr_t dma_start, u64 size) 565 { 566 struct bus_dma_region *map; 567 u64 offset = (u64)cpu_start - (u64)dma_start; 568 569 if (dev->dma_range_map) { 570 dev_err(dev, "attempt to add DMA range to existing map\n"); 571 return -EINVAL; 572 } 573 574 if (!offset) 575 return 0; 576 577 map = kcalloc(2, sizeof(*map), GFP_KERNEL); 578 if (!map) 579 return -ENOMEM; 580 map[0].cpu_start = cpu_start; 581 map[0].dma_start = dma_start; 582 map[0].offset = offset; 583 map[0].size = size; 584 dev->dma_range_map = map; 585 return 0; 586 } 587