1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2012 ARM Ltd. 4 * Copyright (C) 2020 Google LLC 5 */ 6 #include <linux/cma.h> 7 #include <linux/debugfs.h> 8 #include <linux/dma-map-ops.h> 9 #include <linux/dma-direct.h> 10 #include <linux/init.h> 11 #include <linux/genalloc.h> 12 #include <linux/set_memory.h> 13 #include <linux/slab.h> 14 #include <linux/workqueue.h> 15 #include <linux/cc_platform.h> 16 17 struct dma_gen_pool { 18 bool cc_shared; 19 struct gen_pool *pool; 20 }; 21 22 static struct dma_gen_pool atomic_pool_dma __ro_after_init; 23 static unsigned long pool_size_dma; 24 static struct dma_gen_pool atomic_pool_dma32 __ro_after_init; 25 static unsigned long pool_size_dma32; 26 static struct dma_gen_pool atomic_pool_kernel __ro_after_init; 27 static unsigned long pool_size_kernel; 28 29 /* Size can be defined by the coherent_pool command line */ 30 static size_t atomic_pool_size; 31 32 /* Dynamic background expansion when the atomic pool is near capacity */ 33 static struct work_struct atomic_pool_work; 34 35 static int __init early_coherent_pool(char *p) 36 { 37 atomic_pool_size = memparse(p, &p); 38 return 0; 39 } 40 early_param("coherent_pool", early_coherent_pool); 41 42 static void __init dma_atomic_pool_debugfs_init(void) 43 { 44 struct dentry *root; 45 46 root = debugfs_create_dir("dma_pools", NULL); 47 debugfs_create_ulong("pool_size_dma", 0400, root, &pool_size_dma); 48 debugfs_create_ulong("pool_size_dma32", 0400, root, &pool_size_dma32); 49 debugfs_create_ulong("pool_size_kernel", 0400, root, &pool_size_kernel); 50 } 51 52 static void dma_atomic_pool_size_add(gfp_t gfp, size_t size) 53 { 54 if (gfp & __GFP_DMA) 55 pool_size_dma += size; 56 else if (gfp & __GFP_DMA32) 57 pool_size_dma32 += size; 58 else 59 pool_size_kernel += size; 60 } 61 62 static bool cma_in_zone(gfp_t gfp) 63 { 64 unsigned long size; 65 phys_addr_t end; 66 struct cma *cma; 67 68 cma = dev_get_cma_area(NULL); 69 if (!cma) 70 return false; 71 72 size = cma_get_size(cma); 73 if (!size) 74 return false; 75 76 /* CMA can't cross zone boundaries, see cma_activate_area() */ 77 end = cma_get_base(cma) + size - 1; 78 if (IS_ENABLED(CONFIG_ZONE_DMA) && (gfp & GFP_DMA)) 79 return end <= zone_dma_limit; 80 if (IS_ENABLED(CONFIG_ZONE_DMA32) && (gfp & GFP_DMA32)) 81 return end <= max(DMA_BIT_MASK(32), zone_dma_limit); 82 return true; 83 } 84 85 static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size, 86 gfp_t gfp) 87 { 88 unsigned int order; 89 struct page *page = NULL; 90 bool leak_pages = false; 91 void *addr; 92 int ret = -ENOMEM; 93 pgprot_t prot __maybe_unused; 94 95 /* Cannot allocate larger than MAX_PAGE_ORDER */ 96 order = min(get_order(pool_size), MAX_PAGE_ORDER); 97 98 do { 99 pool_size = 1 << (PAGE_SHIFT + order); 100 if (cma_in_zone(gfp)) 101 page = dma_alloc_from_contiguous(NULL, 1 << order, 102 order, false); 103 if (!page) 104 page = alloc_pages(gfp | __GFP_NOWARN, order); 105 } while (!page && order-- > 0); 106 if (!page) 107 goto out; 108 109 arch_dma_prep_coherent(page, pool_size); 110 111 #ifdef CONFIG_DMA_DIRECT_REMAP 112 if (dma_pool->cc_shared) 113 prot = pgprot_decrypted(pgprot_dmacoherent(PAGE_KERNEL)); 114 else 115 prot = pgprot_dmacoherent(PAGE_KERNEL); 116 117 addr = dma_common_contiguous_remap(page, pool_size, prot, 118 __builtin_return_address(0)); 119 if (!addr) 120 goto free_page; 121 #else 122 addr = page_to_virt(page); 123 #endif 124 /* 125 * Memory in the atomic DMA pools must be unencrypted, the pools do not 126 * shrink so no re-encryption occurs in dma_direct_free(). 127 */ 128 if (dma_pool->cc_shared) { 129 ret = set_memory_decrypted((unsigned long)page_to_virt(page), 130 1 << order); 131 if (ret) { 132 leak_pages = true; 133 goto remove_mapping; 134 } 135 } 136 137 ret = gen_pool_add_virt(dma_pool->pool, (unsigned long)addr, 138 page_to_phys(page), pool_size, NUMA_NO_NODE); 139 if (ret) 140 goto encrypt_mapping; 141 142 dma_atomic_pool_size_add(gfp, pool_size); 143 return 0; 144 145 encrypt_mapping: 146 if (dma_pool->cc_shared && 147 set_memory_encrypted((unsigned long)page_to_virt(page), 1 << order)) 148 leak_pages = true; 149 150 remove_mapping: 151 #ifdef CONFIG_DMA_DIRECT_REMAP 152 dma_common_free_remap(addr, pool_size); 153 free_page: 154 #endif 155 if (!leak_pages) 156 __free_pages(page, order); 157 out: 158 return ret; 159 } 160 161 static void atomic_pool_resize(struct dma_gen_pool *dma_pool, gfp_t gfp) 162 { 163 if (dma_pool->pool && gen_pool_avail(dma_pool->pool) < atomic_pool_size) 164 atomic_pool_expand(dma_pool, gen_pool_size(dma_pool->pool), gfp); 165 } 166 167 static void atomic_pool_work_fn(struct work_struct *work) 168 { 169 if (IS_ENABLED(CONFIG_ZONE_DMA)) 170 atomic_pool_resize(&atomic_pool_dma, 171 GFP_KERNEL | GFP_DMA); 172 if (IS_ENABLED(CONFIG_ZONE_DMA32)) 173 atomic_pool_resize(&atomic_pool_dma32, 174 GFP_KERNEL | GFP_DMA32); 175 atomic_pool_resize(&atomic_pool_kernel, GFP_KERNEL); 176 } 177 178 static __init struct dma_gen_pool *__dma_atomic_pool_init(struct dma_gen_pool *dma_pool, 179 size_t pool_size, gfp_t gfp) 180 { 181 int ret; 182 183 dma_pool->pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE); 184 if (!dma_pool->pool) 185 return NULL; 186 187 gen_pool_set_algo(dma_pool->pool, gen_pool_first_fit_order_align, NULL); 188 189 /* if platform is using memory encryption atomic pools are by default shared. */ 190 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) 191 dma_pool->cc_shared = true; 192 else 193 dma_pool->cc_shared = false; 194 195 ret = atomic_pool_expand(dma_pool, pool_size, gfp); 196 if (ret) { 197 gen_pool_destroy(dma_pool->pool); 198 dma_pool->pool = NULL; 199 pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n", 200 pool_size >> 10, &gfp); 201 return NULL; 202 } 203 204 pr_info("DMA: preallocated %zu KiB %pGg pool for atomic allocations\n", 205 gen_pool_size(dma_pool->pool) >> 10, &gfp); 206 return dma_pool; 207 } 208 209 #ifdef CONFIG_ZONE_DMA32 210 #define has_managed_dma32 has_managed_zone(ZONE_DMA32) 211 #else 212 #define has_managed_dma32 false 213 #endif 214 215 static int __init dma_atomic_pool_init(void) 216 { 217 int ret = 0; 218 219 /* 220 * If coherent_pool was not used on the command line, default the pool 221 * sizes to 128KB per 1GB of memory, min 128KB, max MAX_PAGE_ORDER. 222 */ 223 if (!atomic_pool_size) { 224 unsigned long pages = totalram_pages() / (SZ_1G / SZ_128K); 225 pages = min_t(unsigned long, pages, MAX_ORDER_NR_PAGES); 226 atomic_pool_size = max_t(size_t, pages << PAGE_SHIFT, SZ_128K); 227 } 228 INIT_WORK(&atomic_pool_work, atomic_pool_work_fn); 229 230 /* All memory might be in the DMA zone(s) to begin with */ 231 if (has_managed_zone(ZONE_NORMAL)) { 232 __dma_atomic_pool_init(&atomic_pool_kernel, atomic_pool_size, GFP_KERNEL); 233 if (!atomic_pool_kernel.pool) 234 ret = -ENOMEM; 235 } 236 237 if (has_managed_dma()) { 238 __dma_atomic_pool_init(&atomic_pool_dma, atomic_pool_size, 239 GFP_KERNEL | GFP_DMA); 240 if (!atomic_pool_dma.pool) 241 ret = -ENOMEM; 242 } 243 244 if (has_managed_dma32) { 245 __dma_atomic_pool_init(&atomic_pool_dma32, atomic_pool_size, 246 GFP_KERNEL | GFP_DMA32); 247 if (!atomic_pool_dma32.pool) 248 ret = -ENOMEM; 249 } 250 251 dma_atomic_pool_debugfs_init(); 252 return ret; 253 } 254 postcore_initcall(dma_atomic_pool_init); 255 256 static inline struct dma_gen_pool *__dma_guess_pool(struct dma_gen_pool *first, 257 struct dma_gen_pool *second, struct dma_gen_pool *third) 258 { 259 if (first->pool) 260 return first; 261 if (second && second->pool) 262 return second; 263 if (third && third->pool) 264 return third; 265 return NULL; 266 } 267 268 static inline struct dma_gen_pool *dma_guess_pool(struct dma_gen_pool *prev, 269 gfp_t gfp) 270 { 271 if (!prev) { 272 if (gfp & GFP_DMA) 273 return __dma_guess_pool(&atomic_pool_dma, 274 &atomic_pool_dma32, 275 &atomic_pool_kernel); 276 277 if (gfp & GFP_DMA32) 278 return __dma_guess_pool(&atomic_pool_dma32, 279 &atomic_pool_dma, 280 &atomic_pool_kernel); 281 282 return __dma_guess_pool(&atomic_pool_kernel, 283 &atomic_pool_dma32, 284 &atomic_pool_dma); 285 } 286 287 if (prev == &atomic_pool_kernel) 288 return __dma_guess_pool(&atomic_pool_dma32, 289 &atomic_pool_dma, NULL); 290 291 if (prev == &atomic_pool_dma32) 292 return __dma_guess_pool(&atomic_pool_dma, NULL, NULL); 293 294 return NULL; 295 } 296 297 static struct page *__dma_alloc_from_pool(struct device *dev, size_t size, 298 struct gen_pool *pool, void **cpu_addr, 299 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t)) 300 { 301 unsigned long addr; 302 phys_addr_t phys; 303 304 addr = gen_pool_alloc(pool, size); 305 if (!addr) 306 return NULL; 307 308 phys = gen_pool_virt_to_phys(pool, addr); 309 if (phys_addr_ok && !phys_addr_ok(dev, phys, size)) { 310 gen_pool_free(pool, addr, size); 311 return NULL; 312 } 313 314 if (gen_pool_avail(pool) < atomic_pool_size) 315 schedule_work(&atomic_pool_work); 316 317 *cpu_addr = (void *)addr; 318 memset(*cpu_addr, 0, size); 319 return pfn_to_page(__phys_to_pfn(phys)); 320 } 321 322 struct page *dma_alloc_from_pool(struct device *dev, size_t size, 323 void **cpu_addr, gfp_t gfp, unsigned long attrs, 324 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t)) 325 { 326 struct dma_gen_pool *dma_pool = NULL; 327 struct page *page; 328 bool pool_found = false; 329 330 while ((dma_pool = dma_guess_pool(dma_pool, gfp))) { 331 332 if (dma_pool->cc_shared != !!(attrs & __DMA_ATTR_ALLOC_CC_SHARED)) 333 continue; 334 335 pool_found = true; 336 page = __dma_alloc_from_pool(dev, size, dma_pool->pool, cpu_addr, 337 phys_addr_ok); 338 if (page) 339 return page; 340 } 341 342 if (pool_found) 343 WARN(!(gfp & __GFP_NOWARN), "DMA pool exhausted for %s\n", dev_name(dev)); 344 else 345 WARN(1, "Failed to get suitable pool for %s\n", dev_name(dev)); 346 return NULL; 347 } 348 349 bool dma_free_from_pool(struct device *dev, void *start, size_t size) 350 { 351 struct dma_gen_pool *dma_pool = NULL; 352 353 while ((dma_pool = dma_guess_pool(dma_pool, 0))) { 354 355 if (!gen_pool_has_addr(dma_pool->pool, (unsigned long)start, size)) 356 continue; 357 358 gen_pool_free(dma_pool->pool, (unsigned long)start, size); 359 return true; 360 } 361 362 return false; 363 } 364 365 struct dma_pool_phys_match { 366 phys_addr_t phys; 367 size_t size; 368 unsigned long addr; 369 bool found; 370 }; 371 372 static void dma_pool_find_phys(struct gen_pool *pool, struct gen_pool_chunk *chunk, 373 void *data) 374 { 375 struct dma_pool_phys_match *match = data; 376 phys_addr_t end = match->phys + match->size - 1; 377 phys_addr_t chunk_end; 378 379 if (match->found) 380 return; 381 382 chunk_end = chunk->phys_addr + (chunk->end_addr - chunk->start_addr); 383 if (match->phys < chunk->phys_addr || end > chunk_end) 384 return; 385 386 match->addr = chunk->start_addr + (match->phys - chunk->phys_addr); 387 match->found = true; 388 } 389 390 static bool dma_free_from_pool_phys(struct dma_gen_pool *dma_pool, phys_addr_t phys, 391 size_t size) 392 { 393 struct dma_pool_phys_match match = { 394 .phys = phys, 395 .size = size, 396 }; 397 398 gen_pool_for_each_chunk(dma_pool->pool, dma_pool_find_phys, &match); 399 if (!match.found) 400 return false; 401 402 gen_pool_free(dma_pool->pool, match.addr, size); 403 return true; 404 } 405 406 /* 407 * FIXME: We could avoid this by storing the remapped virtual address in 408 * struct page and using that for lookup. 409 */ 410 bool dma_free_from_pool_page(struct device *dev, struct page *page, size_t size) 411 { 412 struct dma_gen_pool *dma_pool = NULL; 413 phys_addr_t phys = page_to_phys(page); 414 415 if (!IS_ENABLED(CONFIG_DMA_DIRECT_REMAP)) 416 return dma_free_from_pool(dev, page_address(page), size); 417 418 while ((dma_pool = dma_guess_pool(dma_pool, 0))) { 419 if (dma_free_from_pool_phys(dma_pool, phys, size)) 420 return true; 421 } 422 423 return false; 424 } 425