xref: /linux/kernel/dma/contiguous.c (revision 93b694d096cc10994c817730d4d50288f9ae3d66)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Contiguous Memory Allocator for DMA mapping framework
4  * Copyright (c) 2010-2011 by Samsung Electronics.
5  * Written by:
6  *	Marek Szyprowski <m.szyprowski@samsung.com>
7  *	Michal Nazarewicz <mina86@mina86.com>
8  */
9 
10 #define pr_fmt(fmt) "cma: " fmt
11 
12 #ifdef CONFIG_CMA_DEBUG
13 #ifndef DEBUG
14 #  define DEBUG
15 #endif
16 #endif
17 
18 #include <asm/page.h>
19 #include <asm/dma-contiguous.h>
20 
21 #include <linux/memblock.h>
22 #include <linux/err.h>
23 #include <linux/sizes.h>
24 #include <linux/dma-contiguous.h>
25 #include <linux/cma.h>
26 
27 #ifdef CONFIG_CMA_SIZE_MBYTES
28 #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
29 #else
30 #define CMA_SIZE_MBYTES 0
31 #endif
32 
33 struct cma *dma_contiguous_default_area;
34 
35 /*
36  * Default global CMA area size can be defined in kernel's .config.
37  * This is useful mainly for distro maintainers to create a kernel
38  * that works correctly for most supported systems.
39  * The size can be set in bytes or as a percentage of the total memory
40  * in the system.
41  *
42  * Users, who want to set the size of global CMA area for their system
43  * should use cma= kernel parameter.
44  */
45 static const phys_addr_t size_bytes __initconst =
46 	(phys_addr_t)CMA_SIZE_MBYTES * SZ_1M;
47 static phys_addr_t  size_cmdline __initdata = -1;
48 static phys_addr_t base_cmdline __initdata;
49 static phys_addr_t limit_cmdline __initdata;
50 
51 static int __init early_cma(char *p)
52 {
53 	if (!p) {
54 		pr_err("Config string not provided\n");
55 		return -EINVAL;
56 	}
57 
58 	size_cmdline = memparse(p, &p);
59 	if (*p != '@')
60 		return 0;
61 	base_cmdline = memparse(p + 1, &p);
62 	if (*p != '-') {
63 		limit_cmdline = base_cmdline + size_cmdline;
64 		return 0;
65 	}
66 	limit_cmdline = memparse(p + 1, &p);
67 
68 	return 0;
69 }
70 early_param("cma", early_cma);
71 
72 #ifdef CONFIG_CMA_SIZE_PERCENTAGE
73 
74 static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
75 {
76 	unsigned long total_pages = PHYS_PFN(memblock_phys_mem_size());
77 
78 	return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
79 }
80 
81 #else
82 
83 static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
84 {
85 	return 0;
86 }
87 
88 #endif
89 
90 /**
91  * dma_contiguous_reserve() - reserve area(s) for contiguous memory handling
92  * @limit: End address of the reserved memory (optional, 0 for any).
93  *
94  * This function reserves memory from early allocator. It should be
95  * called by arch specific code once the early allocator (memblock or bootmem)
96  * has been activated and all other subsystems have already allocated/reserved
97  * memory.
98  */
99 void __init dma_contiguous_reserve(phys_addr_t limit)
100 {
101 	phys_addr_t selected_size = 0;
102 	phys_addr_t selected_base = 0;
103 	phys_addr_t selected_limit = limit;
104 	bool fixed = false;
105 
106 	pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
107 
108 	if (size_cmdline != -1) {
109 		selected_size = size_cmdline;
110 		selected_base = base_cmdline;
111 		selected_limit = min_not_zero(limit_cmdline, limit);
112 		if (base_cmdline + size_cmdline == limit_cmdline)
113 			fixed = true;
114 	} else {
115 #ifdef CONFIG_CMA_SIZE_SEL_MBYTES
116 		selected_size = size_bytes;
117 #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
118 		selected_size = cma_early_percent_memory();
119 #elif defined(CONFIG_CMA_SIZE_SEL_MIN)
120 		selected_size = min(size_bytes, cma_early_percent_memory());
121 #elif defined(CONFIG_CMA_SIZE_SEL_MAX)
122 		selected_size = max(size_bytes, cma_early_percent_memory());
123 #endif
124 	}
125 
126 	if (selected_size && !dma_contiguous_default_area) {
127 		pr_debug("%s: reserving %ld MiB for global area\n", __func__,
128 			 (unsigned long)selected_size / SZ_1M);
129 
130 		dma_contiguous_reserve_area(selected_size, selected_base,
131 					    selected_limit,
132 					    &dma_contiguous_default_area,
133 					    fixed);
134 	}
135 }
136 
137 /**
138  * dma_contiguous_reserve_area() - reserve custom contiguous area
139  * @size: Size of the reserved area (in bytes),
140  * @base: Base address of the reserved area optional, use 0 for any
141  * @limit: End address of the reserved memory (optional, 0 for any).
142  * @res_cma: Pointer to store the created cma region.
143  * @fixed: hint about where to place the reserved area
144  *
145  * This function reserves memory from early allocator. It should be
146  * called by arch specific code once the early allocator (memblock or bootmem)
147  * has been activated and all other subsystems have already allocated/reserved
148  * memory. This function allows to create custom reserved areas for specific
149  * devices.
150  *
151  * If @fixed is true, reserve contiguous area at exactly @base.  If false,
152  * reserve in range from @base to @limit.
153  */
154 int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
155 				       phys_addr_t limit, struct cma **res_cma,
156 				       bool fixed)
157 {
158 	int ret;
159 
160 	ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed,
161 					"reserved", res_cma);
162 	if (ret)
163 		return ret;
164 
165 	/* Architecture specific contiguous memory fixup. */
166 	dma_contiguous_early_fixup(cma_get_base(*res_cma),
167 				cma_get_size(*res_cma));
168 
169 	return 0;
170 }
171 
172 /**
173  * dma_alloc_from_contiguous() - allocate pages from contiguous area
174  * @dev:   Pointer to device for which the allocation is performed.
175  * @count: Requested number of pages.
176  * @align: Requested alignment of pages (in PAGE_SIZE order).
177  * @no_warn: Avoid printing message about failed allocation.
178  *
179  * This function allocates memory buffer for specified device. It uses
180  * device specific contiguous memory area if available or the default
181  * global one. Requires architecture specific dev_get_cma_area() helper
182  * function.
183  */
184 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
185 				       unsigned int align, bool no_warn)
186 {
187 	if (align > CONFIG_CMA_ALIGNMENT)
188 		align = CONFIG_CMA_ALIGNMENT;
189 
190 	return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
191 }
192 
193 /**
194  * dma_release_from_contiguous() - release allocated pages
195  * @dev:   Pointer to device for which the pages were allocated.
196  * @pages: Allocated pages.
197  * @count: Number of allocated pages.
198  *
199  * This function releases memory allocated by dma_alloc_from_contiguous().
200  * It returns false when provided pages do not belong to contiguous area and
201  * true otherwise.
202  */
203 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
204 				 int count)
205 {
206 	return cma_release(dev_get_cma_area(dev), pages, count);
207 }
208 
209 static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
210 {
211 	unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);
212 
213 	return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN);
214 }
215 
216 /**
217  * dma_alloc_contiguous() - allocate contiguous pages
218  * @dev:   Pointer to device for which the allocation is performed.
219  * @size:  Requested allocation size.
220  * @gfp:   Allocation flags.
221  *
222  * This function allocates contiguous memory buffer for specified device. It
223  * tries to use device specific contiguous memory area if available, or the
224  * default global one.
225  *
226  * Note that it byapss one-page size of allocations from the global area as
227  * the addresses within one page are always contiguous, so there is no need
228  * to waste CMA pages for that kind; it also helps reduce fragmentations.
229  */
230 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
231 {
232 	/* CMA can be used only in the context which permits sleeping */
233 	if (!gfpflags_allow_blocking(gfp))
234 		return NULL;
235 	if (dev->cma_area)
236 		return cma_alloc_aligned(dev->cma_area, size, gfp);
237 	if (size <= PAGE_SIZE || !dma_contiguous_default_area)
238 		return NULL;
239 	return cma_alloc_aligned(dma_contiguous_default_area, size, gfp);
240 }
241 
242 /**
243  * dma_free_contiguous() - release allocated pages
244  * @dev:   Pointer to device for which the pages were allocated.
245  * @page:  Pointer to the allocated pages.
246  * @size:  Size of allocated pages.
247  *
248  * This function releases memory allocated by dma_alloc_contiguous(). As the
249  * cma_release returns false when provided pages do not belong to contiguous
250  * area and true otherwise, this function then does a fallback __free_pages()
251  * upon a false-return.
252  */
253 void dma_free_contiguous(struct device *dev, struct page *page, size_t size)
254 {
255 	if (!cma_release(dev_get_cma_area(dev), page,
256 			 PAGE_ALIGN(size) >> PAGE_SHIFT))
257 		__free_pages(page, get_order(size));
258 }
259 
260 /*
261  * Support for reserved memory regions defined in device tree
262  */
263 #ifdef CONFIG_OF_RESERVED_MEM
264 #include <linux/of.h>
265 #include <linux/of_fdt.h>
266 #include <linux/of_reserved_mem.h>
267 
268 #undef pr_fmt
269 #define pr_fmt(fmt) fmt
270 
271 static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
272 {
273 	dev_set_cma_area(dev, rmem->priv);
274 	return 0;
275 }
276 
277 static void rmem_cma_device_release(struct reserved_mem *rmem,
278 				    struct device *dev)
279 {
280 	dev_set_cma_area(dev, NULL);
281 }
282 
283 static const struct reserved_mem_ops rmem_cma_ops = {
284 	.device_init	= rmem_cma_device_init,
285 	.device_release = rmem_cma_device_release,
286 };
287 
288 static int __init rmem_cma_setup(struct reserved_mem *rmem)
289 {
290 	phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
291 	phys_addr_t mask = align - 1;
292 	unsigned long node = rmem->fdt_node;
293 	bool default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
294 	struct cma *cma;
295 	int err;
296 
297 	if (size_cmdline != -1 && default_cma) {
298 		pr_info("Reserved memory: bypass %s node, using cmdline CMA params instead\n",
299 			rmem->name);
300 		return -EBUSY;
301 	}
302 
303 	if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
304 	    of_get_flat_dt_prop(node, "no-map", NULL))
305 		return -EINVAL;
306 
307 	if ((rmem->base & mask) || (rmem->size & mask)) {
308 		pr_err("Reserved memory: incorrect alignment of CMA region\n");
309 		return -EINVAL;
310 	}
311 
312 	err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma);
313 	if (err) {
314 		pr_err("Reserved memory: unable to setup CMA region\n");
315 		return err;
316 	}
317 	/* Architecture specific contiguous memory fixup. */
318 	dma_contiguous_early_fixup(rmem->base, rmem->size);
319 
320 	if (default_cma)
321 		dma_contiguous_set_default(cma);
322 
323 	rmem->ops = &rmem_cma_ops;
324 	rmem->priv = cma;
325 
326 	pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
327 		&rmem->base, (unsigned long)rmem->size / SZ_1M);
328 
329 	return 0;
330 }
331 RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
332 #endif
333