blob: 90167586d9db81318afc7368eacdfe689b73d5d7 [file] [log] [blame]
Marek Szyprowski55bb0332011-12-29 13:09:51 +01001/*
2 * Contiguous Memory Allocator for DMA mapping framework
3 * Copyright (c) 2010-2011 by Samsung Electronics.
4 * Written by:
5 * Marek Szyprowski <m.szyprowski@samsung.com>
6 * Michal Nazarewicz <mina86@mina86.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License or (at your optional) any later version of the license.
12 */
13
14#define pr_fmt(fmt) "cma: " fmt
15
16#ifdef CONFIG_CMA_DEBUG
17#ifndef DEBUG
18# define DEBUG
19#endif
20#endif
21
22#include <asm/page.h>
23#include <asm/dma-contiguous.h>
24
25#include <linux/memblock.h>
26#include <linux/err.h>
27#include <linux/mm.h>
28#include <linux/mutex.h>
29#include <linux/page-isolation.h>
30#include <linux/slab.h>
31#include <linux/swap.h>
32#include <linux/mm_types.h>
33#include <linux/dma-contiguous.h>
Liam Markcc2d4bd2013-01-16 10:14:40 -080034#include <trace/events/kmem.h>
Marek Szyprowski55bb0332011-12-29 13:09:51 +010035
36#ifndef SZ_1M
37#define SZ_1M (1 << 20)
38#endif
39
40struct cma {
41 unsigned long base_pfn;
42 unsigned long count;
43 unsigned long *bitmap;
44};
45
46struct cma *dma_contiguous_default_area;
47
48#ifdef CONFIG_CMA_SIZE_MBYTES
49#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
50#else
51#define CMA_SIZE_MBYTES 0
52#endif
53
54/*
55 * Default global CMA area size can be defined in kernel's .config.
56 * This is usefull mainly for distro maintainers to create a kernel
57 * that works correctly for most supported systems.
58 * The size can be set in bytes or as a percentage of the total memory
59 * in the system.
60 *
61 * Users, who want to set the size of global CMA area for their system
62 * should use cma= kernel parameter.
63 */
Vitaly Andrianov2ee01742012-12-05 09:29:25 -050064static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M;
65static phys_addr_t size_cmdline = -1;
Marek Szyprowski55bb0332011-12-29 13:09:51 +010066
67static int __init early_cma(char *p)
68{
69 pr_debug("%s(%s)\n", __func__, p);
70 size_cmdline = memparse(p, &p);
71 return 0;
72}
73early_param("cma", early_cma);
74
75#ifdef CONFIG_CMA_SIZE_PERCENTAGE
76
Vitaly Andrianov2ee01742012-12-05 09:29:25 -050077static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
Marek Szyprowski55bb0332011-12-29 13:09:51 +010078{
79 struct memblock_region *reg;
80 unsigned long total_pages = 0;
81
82 /*
83 * We cannot use memblock_phys_mem_size() here, because
84 * memblock_analyze() has not been called yet.
85 */
86 for_each_memblock(memory, reg)
87 total_pages += memblock_region_memory_end_pfn(reg) -
88 memblock_region_memory_base_pfn(reg);
89
90 return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
91}
92
93#else
94
Vitaly Andrianov2ee01742012-12-05 09:29:25 -050095static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
Marek Szyprowski55bb0332011-12-29 13:09:51 +010096{
97 return 0;
98}
99
100#endif
101
102/**
103 * dma_contiguous_reserve() - reserve area for contiguous memory handling
104 * @limit: End address of the reserved memory (optional, 0 for any).
105 *
106 * This function reserves memory from early allocator. It should be
107 * called by arch specific code once the early allocator (memblock or bootmem)
108 * has been activated and all other subsystems have already allocated/reserved
109 * memory.
110 */
111void __init dma_contiguous_reserve(phys_addr_t limit)
112{
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500113 phys_addr_t selected_size = 0;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100114
115 pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
116
117 if (size_cmdline != -1) {
118 selected_size = size_cmdline;
119 } else {
120#ifdef CONFIG_CMA_SIZE_SEL_MBYTES
121 selected_size = size_bytes;
122#elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
123 selected_size = cma_early_percent_memory();
124#elif defined(CONFIG_CMA_SIZE_SEL_MIN)
125 selected_size = min(size_bytes, cma_early_percent_memory());
126#elif defined(CONFIG_CMA_SIZE_SEL_MAX)
127 selected_size = max(size_bytes, cma_early_percent_memory());
128#endif
129 }
130
131 if (selected_size) {
132 pr_debug("%s: reserving %ld MiB for global area\n", __func__,
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500133 (unsigned long)selected_size / SZ_1M);
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100134
135 dma_declare_contiguous(NULL, selected_size, 0, limit);
136 }
137};
138
139static DEFINE_MUTEX(cma_mutex);
140
141static __init int cma_activate_area(unsigned long base_pfn, unsigned long count)
142{
143 unsigned long pfn = base_pfn;
144 unsigned i = count >> pageblock_order;
145 struct zone *zone;
146
147 WARN_ON_ONCE(!pfn_valid(pfn));
148 zone = page_zone(pfn_to_page(pfn));
149
150 do {
151 unsigned j;
152 base_pfn = pfn;
153 for (j = pageblock_nr_pages; j; --j, pfn++) {
154 WARN_ON_ONCE(!pfn_valid(pfn));
155 if (page_zone(pfn_to_page(pfn)) != zone)
156 return -EINVAL;
157 }
158 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
159 } while (--i);
160 return 0;
161}
162
163static __init struct cma *cma_create_area(unsigned long base_pfn,
164 unsigned long count)
165{
166 int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
167 struct cma *cma;
168 int ret = -ENOMEM;
169
170 pr_debug("%s(base %08lx, count %lx)\n", __func__, base_pfn, count);
171
172 cma = kmalloc(sizeof *cma, GFP_KERNEL);
173 if (!cma)
174 return ERR_PTR(-ENOMEM);
175
176 cma->base_pfn = base_pfn;
177 cma->count = count;
178 cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
179
180 if (!cma->bitmap)
181 goto no_mem;
182
183 ret = cma_activate_area(base_pfn, count);
184 if (ret)
185 goto error;
186
187 pr_debug("%s: returned %p\n", __func__, (void *)cma);
188 return cma;
189
190error:
191 kfree(cma->bitmap);
192no_mem:
193 kfree(cma);
194 return ERR_PTR(ret);
195}
196
197static struct cma_reserved {
198 phys_addr_t start;
199 unsigned long size;
200 struct device *dev;
201} cma_reserved[MAX_CMA_AREAS] __initdata;
202static unsigned cma_reserved_count __initdata;
203
204static int __init cma_init_reserved_areas(void)
205{
206 struct cma_reserved *r = cma_reserved;
207 unsigned i = cma_reserved_count;
208
209 pr_debug("%s()\n", __func__);
210
211 for (; i; --i, ++r) {
212 struct cma *cma;
213 cma = cma_create_area(PFN_DOWN(r->start),
214 r->size >> PAGE_SHIFT);
215 if (!IS_ERR(cma))
216 dev_set_cma_area(r->dev, cma);
217 }
218 return 0;
219}
220core_initcall(cma_init_reserved_areas);
221
222/**
223 * dma_declare_contiguous() - reserve area for contiguous memory handling
224 * for particular device
225 * @dev: Pointer to device structure.
226 * @size: Size of the reserved memory.
227 * @base: Start address of the reserved memory (optional, 0 for any).
228 * @limit: End address of the reserved memory (optional, 0 for any).
229 *
230 * This function reserves memory for specified device. It should be
231 * called by board specific code when early allocator (memblock or bootmem)
232 * is still activate.
233 */
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500234int __init dma_declare_contiguous(struct device *dev, phys_addr_t size,
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100235 phys_addr_t base, phys_addr_t limit)
236{
237 struct cma_reserved *r = &cma_reserved[cma_reserved_count];
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500238 phys_addr_t alignment;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100239
240 pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__,
241 (unsigned long)size, (unsigned long)base,
242 (unsigned long)limit);
243
244 /* Sanity checks */
245 if (cma_reserved_count == ARRAY_SIZE(cma_reserved)) {
246 pr_err("Not enough slots for CMA reserved regions!\n");
247 return -ENOSPC;
248 }
249
250 if (!size)
251 return -EINVAL;
252
253 /* Sanitise input arguments */
Marek Szyprowski12731372012-08-27 20:27:19 +0200254 alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100255 base = ALIGN(base, alignment);
256 size = ALIGN(size, alignment);
257 limit &= ~(alignment - 1);
258
259 /* Reserve memory */
260 if (base) {
261 if (memblock_is_region_reserved(base, size) ||
262 memblock_reserve(base, size) < 0) {
263 base = -EBUSY;
264 goto err;
265 }
266 } else {
267 /*
268 * Use __memblock_alloc_base() since
269 * memblock_alloc_base() panic()s.
270 */
271 phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);
272 if (!addr) {
273 base = -ENOMEM;
274 goto err;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100275 } else {
276 base = addr;
277 }
278 }
279
280 /*
281 * Each reserved area must be initialised later, when more kernel
282 * subsystems (like slab allocator) are available.
283 */
284 r->start = base;
285 r->size = size;
286 r->dev = dev;
287 cma_reserved_count++;
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500288 pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100289 (unsigned long)base);
290
291 /* Architecture specific contiguous memory fixup. */
292 dma_contiguous_early_fixup(base, size);
293 return 0;
294err:
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500295 pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100296 return base;
297}
298
299/**
300 * dma_alloc_from_contiguous() - allocate pages from contiguous area
301 * @dev: Pointer to device for which the allocation is performed.
302 * @count: Requested number of pages.
303 * @align: Requested alignment of pages (in PAGE_SIZE order).
304 *
305 * This function allocates memory buffer for specified device. It uses
306 * device specific contiguous memory area if available or the default
307 * global one. Requires architecture specific get_dev_cma_area() helper
308 * function.
309 */
310struct page *dma_alloc_from_contiguous(struct device *dev, int count,
311 unsigned int align)
312{
313 unsigned long mask, pfn, pageno, start = 0;
314 struct cma *cma = dev_get_cma_area(dev);
315 int ret;
Liam Markcc2d4bd2013-01-16 10:14:40 -0800316 int tries = 0;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100317
318 if (!cma || !cma->count)
319 return NULL;
320
321 if (align > CONFIG_CMA_ALIGNMENT)
322 align = CONFIG_CMA_ALIGNMENT;
323
324 pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
325 count, align);
326
327 if (!count)
328 return NULL;
329
330 mask = (1 << align) - 1;
331
332 mutex_lock(&cma_mutex);
333
334 for (;;) {
335 pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,
336 start, count, mask);
337 if (pageno >= cma->count) {
338 ret = -ENOMEM;
339 goto error;
340 }
341
342 pfn = cma->base_pfn + pageno;
343 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
344 if (ret == 0) {
345 bitmap_set(cma->bitmap, pageno, count);
346 break;
347 } else if (ret != -EBUSY) {
348 goto error;
349 }
Liam Markcc2d4bd2013-01-16 10:14:40 -0800350 tries++;
351 trace_dma_alloc_contiguous_retry(tries);
352
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100353 pr_debug("%s(): memory range at %p is busy, retrying\n",
354 __func__, pfn_to_page(pfn));
355 /* try again with a bit different memory target */
356 start = pageno + mask + 1;
357 }
358
359 mutex_unlock(&cma_mutex);
360
361 pr_debug("%s(): returned %p\n", __func__, pfn_to_page(pfn));
362 return pfn_to_page(pfn);
363error:
364 mutex_unlock(&cma_mutex);
365 return NULL;
366}
367
368/**
369 * dma_release_from_contiguous() - release allocated pages
370 * @dev: Pointer to device for which the pages were allocated.
371 * @pages: Allocated pages.
372 * @count: Number of allocated pages.
373 *
374 * This function releases memory allocated by dma_alloc_from_contiguous().
375 * It returns false when provided pages do not belong to contiguous area and
376 * true otherwise.
377 */
378bool dma_release_from_contiguous(struct device *dev, struct page *pages,
379 int count)
380{
381 struct cma *cma = dev_get_cma_area(dev);
382 unsigned long pfn;
383
384 if (!cma || !pages)
385 return false;
386
387 pr_debug("%s(page %p)\n", __func__, (void *)pages);
388
389 pfn = page_to_pfn(pages);
390
391 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
392 return false;
393
394 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
395
396 mutex_lock(&cma_mutex);
397 bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count);
398 free_contig_range(pfn, count);
399 mutex_unlock(&cma_mutex);
400
401 return true;
402}