1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 ARM Ltd.
4 * Copyright (C) 2020 Google LLC
7 #include <linux/debugfs.h>
8 #include <linux/dma-contiguous.h>
9 #include <linux/dma-direct.h>
10 #include <linux/dma-noncoherent.h>
11 #include <linux/init.h>
12 #include <linux/genalloc.h>
13 #include <linux/set_memory.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
17 static struct gen_pool *atomic_pool_dma __ro_after_init;
18 static unsigned long pool_size_dma;
19 static struct gen_pool *atomic_pool_dma32 __ro_after_init;
20 static unsigned long pool_size_dma32;
21 static struct gen_pool *atomic_pool_kernel __ro_after_init;
22 static unsigned long pool_size_kernel;
24 /* Size can be defined by the coherent_pool command line */
25 static size_t atomic_pool_size;
27 /* Dynamic background expansion when the atomic pool is near capacity */
28 static struct work_struct atomic_pool_work;
30 static int __init early_coherent_pool(char *p)
32 atomic_pool_size = memparse(p, &p);
35 early_param("coherent_pool", early_coherent_pool);
37 static void __init dma_atomic_pool_debugfs_init(void)
41 root = debugfs_create_dir("dma_pools", NULL);
42 if (IS_ERR_OR_NULL(root))
45 debugfs_create_ulong("pool_size_dma", 0400, root, &pool_size_dma);
46 debugfs_create_ulong("pool_size_dma32", 0400, root, &pool_size_dma32);
47 debugfs_create_ulong("pool_size_kernel", 0400, root, &pool_size_kernel);
50 static void dma_atomic_pool_size_add(gfp_t gfp, size_t size)
53 pool_size_dma += size;
54 else if (gfp & __GFP_DMA32)
55 pool_size_dma32 += size;
57 pool_size_kernel += size;
60 static bool cma_in_zone(gfp_t gfp)
66 cma = dev_get_cma_area(NULL);
70 size = cma_get_size(cma);
74 /* CMA can't cross zone boundaries, see cma_activate_area() */
75 end = cma_get_base(cma) + size - 1;
76 if (IS_ENABLED(CONFIG_ZONE_DMA) && (gfp & GFP_DMA))
77 return end <= DMA_BIT_MASK(zone_dma_bits);
78 if (IS_ENABLED(CONFIG_ZONE_DMA32) && (gfp & GFP_DMA32))
79 return end <= DMA_BIT_MASK(32);
83 static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size,
91 /* Cannot allocate larger than MAX_ORDER-1 */
92 order = min(get_order(pool_size), MAX_ORDER-1);
95 pool_size = 1 << (PAGE_SHIFT + order);
97 page = dma_alloc_from_contiguous(NULL, 1 << order,
100 page = alloc_pages(gfp, order);
101 } while (!page && order-- > 0);
105 arch_dma_prep_coherent(page, pool_size);
107 #ifdef CONFIG_DMA_DIRECT_REMAP
108 addr = dma_common_contiguous_remap(page, pool_size,
109 pgprot_dmacoherent(PAGE_KERNEL),
110 __builtin_return_address(0));
114 addr = page_to_virt(page);
117 * Memory in the atomic DMA pools must be unencrypted, the pools do not
118 * shrink so no re-encryption occurs in dma_direct_free_pages().
120 ret = set_memory_decrypted((unsigned long)page_to_virt(page),
124 ret = gen_pool_add_virt(pool, (unsigned long)addr, page_to_phys(page),
125 pool_size, NUMA_NO_NODE);
127 goto encrypt_mapping;
129 dma_atomic_pool_size_add(gfp, pool_size);
133 ret = set_memory_encrypted((unsigned long)page_to_virt(page),
135 if (WARN_ON_ONCE(ret)) {
136 /* Decrypt succeeded but encrypt failed, purposely leak */
140 #ifdef CONFIG_DMA_DIRECT_REMAP
141 dma_common_free_remap(addr, pool_size);
143 free_page: __maybe_unused
144 __free_pages(page, order);
149 static void atomic_pool_resize(struct gen_pool *pool, gfp_t gfp)
151 if (pool && gen_pool_avail(pool) < atomic_pool_size)
152 atomic_pool_expand(pool, gen_pool_size(pool), gfp);
155 static void atomic_pool_work_fn(struct work_struct *work)
157 if (IS_ENABLED(CONFIG_ZONE_DMA))
158 atomic_pool_resize(atomic_pool_dma,
159 GFP_KERNEL | GFP_DMA);
160 if (IS_ENABLED(CONFIG_ZONE_DMA32))
161 atomic_pool_resize(atomic_pool_dma32,
162 GFP_KERNEL | GFP_DMA32);
163 atomic_pool_resize(atomic_pool_kernel, GFP_KERNEL);
166 static __init struct gen_pool *__dma_atomic_pool_init(size_t pool_size,
169 struct gen_pool *pool;
172 pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE);
176 gen_pool_set_algo(pool, gen_pool_first_fit_order_align, NULL);
178 ret = atomic_pool_expand(pool, pool_size, gfp);
180 gen_pool_destroy(pool);
181 pr_err("DMA: failed to allocate %zu KiB %pGg pool for atomic allocation\n",
182 pool_size >> 10, &gfp);
186 pr_info("DMA: preallocated %zu KiB %pGg pool for atomic allocations\n",
187 gen_pool_size(pool) >> 10, &gfp);
191 static int __init dma_atomic_pool_init(void)
196 * If coherent_pool was not used on the command line, default the pool
197 * sizes to 128KB per 1GB of memory, min 128KB, max MAX_ORDER-1.
199 if (!atomic_pool_size) {
200 unsigned long pages = totalram_pages() / (SZ_1G / SZ_128K);
201 pages = min_t(unsigned long, pages, MAX_ORDER_NR_PAGES);
202 atomic_pool_size = max_t(size_t, pages << PAGE_SHIFT, SZ_128K);
204 INIT_WORK(&atomic_pool_work, atomic_pool_work_fn);
206 atomic_pool_kernel = __dma_atomic_pool_init(atomic_pool_size,
208 if (!atomic_pool_kernel)
210 if (IS_ENABLED(CONFIG_ZONE_DMA)) {
211 atomic_pool_dma = __dma_atomic_pool_init(atomic_pool_size,
212 GFP_KERNEL | GFP_DMA);
213 if (!atomic_pool_dma)
216 if (IS_ENABLED(CONFIG_ZONE_DMA32)) {
217 atomic_pool_dma32 = __dma_atomic_pool_init(atomic_pool_size,
218 GFP_KERNEL | GFP_DMA32);
219 if (!atomic_pool_dma32)
223 dma_atomic_pool_debugfs_init();
226 postcore_initcall(dma_atomic_pool_init);
228 static inline struct gen_pool *dma_guess_pool(struct gen_pool *prev, gfp_t gfp)
231 if (IS_ENABLED(CONFIG_ZONE_DMA32) && (gfp & GFP_DMA32))
232 return atomic_pool_dma32;
233 if (IS_ENABLED(CONFIG_ZONE_DMA) && (gfp & GFP_DMA))
234 return atomic_pool_dma;
235 return atomic_pool_kernel;
237 if (prev == atomic_pool_kernel)
238 return atomic_pool_dma32 ? atomic_pool_dma32 : atomic_pool_dma;
239 if (prev == atomic_pool_dma32)
240 return atomic_pool_dma;
244 static struct page *__dma_alloc_from_pool(struct device *dev, size_t size,
245 struct gen_pool *pool, void **cpu_addr,
246 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t))
251 addr = gen_pool_alloc(pool, size);
255 phys = gen_pool_virt_to_phys(pool, addr);
256 if (phys_addr_ok && !phys_addr_ok(dev, phys, size)) {
257 gen_pool_free(pool, addr, size);
261 if (gen_pool_avail(pool) < atomic_pool_size)
262 schedule_work(&atomic_pool_work);
264 *cpu_addr = (void *)addr;
265 memset(*cpu_addr, 0, size);
266 return pfn_to_page(__phys_to_pfn(phys));
269 struct page *dma_alloc_from_pool(struct device *dev, size_t size,
270 void **cpu_addr, gfp_t gfp,
271 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t))
273 struct gen_pool *pool = NULL;
276 while ((pool = dma_guess_pool(pool, gfp))) {
277 page = __dma_alloc_from_pool(dev, size, pool, cpu_addr,
283 WARN(1, "Failed to get suitable pool for %s\n", dev_name(dev));
287 bool dma_free_from_pool(struct device *dev, void *start, size_t size)
289 struct gen_pool *pool = NULL;
291 while ((pool = dma_guess_pool(pool, 0))) {
292 if (!gen_pool_has_addr(pool, (unsigned long)start, size))
294 gen_pool_free(pool, (unsigned long)start, size);