blob: 1937db8b59170cef009ed5e762cf71dc2712e999 [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
Marek Szyprowskidb7909c2013-02-14 13:45:27 +010046static DEFINE_MUTEX(cma_mutex);
47
48struct cma *dma_contiguous_def_area;
49phys_addr_t dma_contiguous_def_base;
50
51static struct cma_area {
52 phys_addr_t base;
53 unsigned long size;
54 struct cma *cma;
55} cma_areas[MAX_CMA_AREAS] __initdata;
56static unsigned cma_area_count __initdata;
57
58
59static struct cma_map {
60 phys_addr_t base;
61 struct device *dev;
62} cma_maps[MAX_CMA_AREAS] __initdata;
63static unsigned cma_map_count __initdata;
64
65static struct cma *cma_get_area(phys_addr_t base)
66{
67 int i;
68 for (i = 0; i < cma_area_count; i++)
69 if (cma_areas[i].base == base)
70 return cma_areas[i].cma;
71 return NULL;
72}
Marek Szyprowski55bb0332011-12-29 13:09:51 +010073
74#ifdef CONFIG_CMA_SIZE_MBYTES
75#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
76#else
77#define CMA_SIZE_MBYTES 0
78#endif
79
80/*
81 * Default global CMA area size can be defined in kernel's .config.
82 * This is usefull mainly for distro maintainers to create a kernel
83 * that works correctly for most supported systems.
84 * The size can be set in bytes or as a percentage of the total memory
85 * in the system.
86 *
87 * Users, who want to set the size of global CMA area for their system
88 * should use cma= kernel parameter.
89 */
Vitaly Andrianov2ee01742012-12-05 09:29:25 -050090static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M;
91static phys_addr_t size_cmdline = -1;
Marek Szyprowski55bb0332011-12-29 13:09:51 +010092
93static int __init early_cma(char *p)
94{
95 pr_debug("%s(%s)\n", __func__, p);
96 size_cmdline = memparse(p, &p);
97 return 0;
98}
99early_param("cma", early_cma);
100
101#ifdef CONFIG_CMA_SIZE_PERCENTAGE
102
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500103static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100104{
105 struct memblock_region *reg;
106 unsigned long total_pages = 0;
107
108 /*
109 * We cannot use memblock_phys_mem_size() here, because
110 * memblock_analyze() has not been called yet.
111 */
112 for_each_memblock(memory, reg)
113 total_pages += memblock_region_memory_end_pfn(reg) -
114 memblock_region_memory_base_pfn(reg);
115
116 return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
117}
118
119#else
120
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500121static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100122{
123 return 0;
124}
125
126#endif
127
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100128static __init int cma_activate_area(unsigned long base_pfn, unsigned long count)
129{
130 unsigned long pfn = base_pfn;
131 unsigned i = count >> pageblock_order;
132 struct zone *zone;
133
134 WARN_ON_ONCE(!pfn_valid(pfn));
135 zone = page_zone(pfn_to_page(pfn));
136
137 do {
138 unsigned j;
139 base_pfn = pfn;
140 for (j = pageblock_nr_pages; j; --j, pfn++) {
141 WARN_ON_ONCE(!pfn_valid(pfn));
142 if (page_zone(pfn_to_page(pfn)) != zone)
143 return -EINVAL;
144 }
145 init_cma_reserved_pageblock(pfn_to_page(base_pfn));
146 } while (--i);
147 return 0;
148}
149
150static __init struct cma *cma_create_area(unsigned long base_pfn,
151 unsigned long count)
152{
153 int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
154 struct cma *cma;
155 int ret = -ENOMEM;
156
157 pr_debug("%s(base %08lx, count %lx)\n", __func__, base_pfn, count);
158
159 cma = kmalloc(sizeof *cma, GFP_KERNEL);
160 if (!cma)
161 return ERR_PTR(-ENOMEM);
162
163 cma->base_pfn = base_pfn;
164 cma->count = count;
165 cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
166
167 if (!cma->bitmap)
168 goto no_mem;
169
170 ret = cma_activate_area(base_pfn, count);
171 if (ret)
172 goto error;
173
174 pr_debug("%s: returned %p\n", __func__, (void *)cma);
175 return cma;
176
177error:
178 kfree(cma->bitmap);
179no_mem:
180 kfree(cma);
181 return ERR_PTR(ret);
182}
183
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100184/**
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100185 * dma_contiguous_reserve() - reserve area for contiguous memory handling
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100186 * @limit: End address of the reserved memory (optional, 0 for any).
187 *
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100188 * This function reserves memory from early allocator. It should be
189 * called by arch specific code once the early allocator (memblock or bootmem)
190 * has been activated and all other subsystems have already allocated/reserved
191 * memory. It reserves contiguous areas for global, device independent
192 * allocations and (optionally) all areas defined in device tree structures.
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100193 */
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100194void __init dma_contiguous_reserve(phys_addr_t limit)
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100195{
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100196 phys_addr_t sel_size = 0;
197
198 pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
199
200 if (size_cmdline != -1) {
201 sel_size = size_cmdline;
202 } else {
203#ifdef CONFIG_CMA_SIZE_SEL_MBYTES
204 sel_size = size_bytes;
205#elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
206 sel_size = cma_early_percent_memory();
207#elif defined(CONFIG_CMA_SIZE_SEL_MIN)
208 sel_size = min(size_bytes, cma_early_percent_memory());
209#elif defined(CONFIG_CMA_SIZE_SEL_MAX)
210 sel_size = max(size_bytes, cma_early_percent_memory());
211#endif
212 }
213
214 if (sel_size) {
215 phys_addr_t base = 0;
216 pr_debug("%s: reserving %ld MiB for global area\n", __func__,
217 (unsigned long)sel_size / SZ_1M);
218
219 if (dma_contiguous_reserve_area(sel_size, &base, limit) == 0)
220 dma_contiguous_def_base = base;
221 }
222};
223
224/**
225 * dma_contiguous_reserve_area() - reserve custom contiguous area
226 * @size: Size of the reserved area (in bytes),
227 * @base: Pointer to the base address of the reserved area, also used to return
228 * base address of the actually reserved area, optional, use pointer to
229 * 0 for any
230 * @limit: End address of the reserved memory (optional, 0 for any).
231 *
232 * This function reserves memory from early allocator. It should be
233 * called by arch specific code once the early allocator (memblock or bootmem)
234 * has been activated and all other subsystems have already allocated/reserved
235 * memory. This function allows to create custom reserved areas for specific
236 * devices.
237 */
238int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t *res_base,
239 phys_addr_t limit)
240{
241 phys_addr_t base = *res_base;
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500242 phys_addr_t alignment;
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100243 int ret = 0;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100244
245 pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__,
246 (unsigned long)size, (unsigned long)base,
247 (unsigned long)limit);
248
249 /* Sanity checks */
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100250 if (cma_area_count == ARRAY_SIZE(cma_areas)) {
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100251 pr_err("Not enough slots for CMA reserved regions!\n");
252 return -ENOSPC;
253 }
254
255 if (!size)
256 return -EINVAL;
257
258 /* Sanitise input arguments */
Marek Szyprowski12731372012-08-27 20:27:19 +0200259 alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100260 base = ALIGN(base, alignment);
261 size = ALIGN(size, alignment);
262 limit &= ~(alignment - 1);
263
264 /* Reserve memory */
265 if (base) {
266 if (memblock_is_region_reserved(base, size) ||
267 memblock_reserve(base, size) < 0) {
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100268 ret = -EBUSY;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100269 goto err;
270 }
271 } else {
272 /*
273 * Use __memblock_alloc_base() since
274 * memblock_alloc_base() panic()s.
275 */
276 phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);
277 if (!addr) {
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100278 ret = -ENOMEM;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100279 goto err;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100280 } else {
281 base = addr;
282 }
283 }
284
285 /*
286 * Each reserved area must be initialised later, when more kernel
287 * subsystems (like slab allocator) are available.
288 */
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100289 cma_areas[cma_area_count].base = base;
290 cma_areas[cma_area_count].size = size;
291 cma_area_count++;
292 *res_base = base;
293
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500294 pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100295 (unsigned long)base);
296
297 /* Architecture specific contiguous memory fixup. */
298 dma_contiguous_early_fixup(base, size);
299 return 0;
300err:
Vitaly Andrianov2ee01742012-12-05 09:29:25 -0500301 pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100302 return ret;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100303}
304
305/**
Marek Szyprowskidb7909c2013-02-14 13:45:27 +0100306 * dma_contiguous_add_device() - add device to custom contiguous reserved area
307 * @dev: Pointer to device structure.
308 * @base: Pointer to the base address of the reserved area returned by
309 * dma_contiguous_reserve_area() function, also used to return
310 *
311 * This function assigns the given device to the contiguous memory area
312 * reserved earlier by dma_contiguous_reserve_area() function.
313 */
314int __init dma_contiguous_add_device(struct device *dev, phys_addr_t base)
315{
316 if (cma_map_count == ARRAY_SIZE(cma_maps)) {
317 pr_err("Not enough slots for CMA reserved regions!\n");
318 return -ENOSPC;
319 }
320 cma_maps[cma_map_count].dev = dev;
321 cma_maps[cma_map_count].base = base;
322 cma_map_count++;
323 return 0;
324}
325
326static int __init cma_init_reserved_areas(void)
327{
328 struct cma *cma;
329 int i;
330
331 for (i = 0; i < cma_area_count; i++) {
332 phys_addr_t base = PFN_DOWN(cma_areas[i].base);
333 unsigned int count = cma_areas[i].size >> PAGE_SHIFT;
334
335 cma = cma_create_area(base, count);
336 if (!IS_ERR(cma))
337 cma_areas[i].cma = cma;
338 }
339
340 dma_contiguous_def_area = cma_get_area(dma_contiguous_def_base);
341
342 for (i = 0; i < cma_map_count; i++) {
343 cma = cma_get_area(cma_maps[i].base);
344 dev_set_cma_area(cma_maps[i].dev, cma);
345 }
346
347 return 0;
348}
349core_initcall(cma_init_reserved_areas);
350
351/**
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100352 * dma_alloc_from_contiguous() - allocate pages from contiguous area
353 * @dev: Pointer to device for which the allocation is performed.
354 * @count: Requested number of pages.
355 * @align: Requested alignment of pages (in PAGE_SIZE order).
356 *
357 * This function allocates memory buffer for specified device. It uses
358 * device specific contiguous memory area if available or the default
359 * global one. Requires architecture specific get_dev_cma_area() helper
360 * function.
361 */
362struct page *dma_alloc_from_contiguous(struct device *dev, int count,
363 unsigned int align)
364{
365 unsigned long mask, pfn, pageno, start = 0;
366 struct cma *cma = dev_get_cma_area(dev);
Michal Nazarewicz81e10962012-09-05 07:50:41 +0200367 struct page *page = NULL;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100368 int ret;
Liam Markcc2d4bd2013-01-16 10:14:40 -0800369 int tries = 0;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100370
371 if (!cma || !cma->count)
372 return NULL;
373
374 if (align > CONFIG_CMA_ALIGNMENT)
375 align = CONFIG_CMA_ALIGNMENT;
376
377 pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
378 count, align);
379
380 if (!count)
381 return NULL;
382
383 mask = (1 << align) - 1;
384
385 mutex_lock(&cma_mutex);
386
387 for (;;) {
388 pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,
389 start, count, mask);
Michal Nazarewicz81e10962012-09-05 07:50:41 +0200390 if (pageno >= cma->count)
391 break;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100392
393 pfn = cma->base_pfn + pageno;
394 ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
395 if (ret == 0) {
396 bitmap_set(cma->bitmap, pageno, count);
Michal Nazarewicz81e10962012-09-05 07:50:41 +0200397 page = pfn_to_page(pfn);
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100398 break;
399 } else if (ret != -EBUSY) {
Michal Nazarewicz81e10962012-09-05 07:50:41 +0200400 break;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100401 }
Liam Markcc2d4bd2013-01-16 10:14:40 -0800402 tries++;
403 trace_dma_alloc_contiguous_retry(tries);
404
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100405 pr_debug("%s(): memory range at %p is busy, retrying\n",
406 __func__, pfn_to_page(pfn));
407 /* try again with a bit different memory target */
408 start = pageno + mask + 1;
409 }
410
411 mutex_unlock(&cma_mutex);
Michal Nazarewicz81e10962012-09-05 07:50:41 +0200412 pr_debug("%s(): returned %p\n", __func__, page);
413 return page;
Marek Szyprowski55bb0332011-12-29 13:09:51 +0100414}
415
416/**
417 * dma_release_from_contiguous() - release allocated pages
418 * @dev: Pointer to device for which the pages were allocated.
419 * @pages: Allocated pages.
420 * @count: Number of allocated pages.
421 *
422 * This function releases memory allocated by dma_alloc_from_contiguous().
423 * It returns false when provided pages do not belong to contiguous area and
424 * true otherwise.
425 */
426bool dma_release_from_contiguous(struct device *dev, struct page *pages,
427 int count)
428{
429 struct cma *cma = dev_get_cma_area(dev);
430 unsigned long pfn;
431
432 if (!cma || !pages)
433 return false;
434
435 pr_debug("%s(page %p)\n", __func__, (void *)pages);
436
437 pfn = page_to_pfn(pages);
438
439 if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
440 return false;
441
442 VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
443
444 mutex_lock(&cma_mutex);
445 bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count);
446 free_contig_range(pfn, count);
447 mutex_unlock(&cma_mutex);
448
449 return true;
450}