Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 1 | /* |
| 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. |
Laura Abbott | 9e821fb | 2013-02-26 10:26:30 -0800 | [diff] [blame] | 12 | * |
| 13 | * The Linux Foundation chooses to take subject only to the GPLv2 license |
| 14 | * terms, and distributes only under these terms. |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #define pr_fmt(fmt) "cma: " fmt |
| 18 | |
| 19 | #ifdef CONFIG_CMA_DEBUG |
| 20 | #ifndef DEBUG |
| 21 | # define DEBUG |
| 22 | #endif |
| 23 | #endif |
| 24 | |
| 25 | #include <asm/page.h> |
| 26 | #include <asm/dma-contiguous.h> |
| 27 | |
| 28 | #include <linux/memblock.h> |
| 29 | #include <linux/err.h> |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_fdt.h> |
| 32 | #include <linux/of_platform.h> |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 33 | #include <linux/mm.h> |
| 34 | #include <linux/mutex.h> |
| 35 | #include <linux/page-isolation.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/swap.h> |
| 38 | #include <linux/mm_types.h> |
| 39 | #include <linux/dma-contiguous.h> |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 40 | #include <trace/events/kmem.h> |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 41 | |
| 42 | #ifndef SZ_1M |
| 43 | #define SZ_1M (1 << 20) |
| 44 | #endif |
| 45 | |
| 46 | struct cma { |
| 47 | unsigned long base_pfn; |
| 48 | unsigned long count; |
| 49 | unsigned long *bitmap; |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 50 | struct mutex lock; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 53 | static DEFINE_MUTEX(cma_mutex); |
| 54 | |
| 55 | struct cma *dma_contiguous_def_area; |
| 56 | phys_addr_t dma_contiguous_def_base; |
| 57 | |
| 58 | static struct cma_area { |
| 59 | phys_addr_t base; |
| 60 | unsigned long size; |
| 61 | struct cma *cma; |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 62 | const char *name; |
Laura Abbott | 9e821fb | 2013-02-26 10:26:30 -0800 | [diff] [blame] | 63 | } cma_areas[MAX_CMA_AREAS]; |
| 64 | static unsigned cma_area_count; |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 65 | |
| 66 | |
| 67 | static struct cma_map { |
| 68 | phys_addr_t base; |
| 69 | struct device *dev; |
| 70 | } cma_maps[MAX_CMA_AREAS] __initdata; |
| 71 | static unsigned cma_map_count __initdata; |
| 72 | |
| 73 | static struct cma *cma_get_area(phys_addr_t base) |
| 74 | { |
| 75 | int i; |
| 76 | for (i = 0; i < cma_area_count; i++) |
| 77 | if (cma_areas[i].base == base) |
| 78 | return cma_areas[i].cma; |
| 79 | return NULL; |
| 80 | } |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 81 | |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 82 | static struct cma *cma_get_area_by_name(const char *name) |
| 83 | { |
| 84 | int i; |
| 85 | if (!name) |
| 86 | return NULL; |
| 87 | |
| 88 | for (i = 0; i < cma_area_count; i++) |
| 89 | if (cma_areas[i].name && strcmp(cma_areas[i].name, name) == 0) |
| 90 | return cma_areas[i].cma; |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 96 | #ifdef CONFIG_CMA_SIZE_MBYTES |
| 97 | #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES |
| 98 | #else |
| 99 | #define CMA_SIZE_MBYTES 0 |
| 100 | #endif |
| 101 | |
| 102 | /* |
| 103 | * Default global CMA area size can be defined in kernel's .config. |
| 104 | * This is usefull mainly for distro maintainers to create a kernel |
| 105 | * that works correctly for most supported systems. |
| 106 | * The size can be set in bytes or as a percentage of the total memory |
| 107 | * in the system. |
| 108 | * |
| 109 | * Users, who want to set the size of global CMA area for their system |
| 110 | * should use cma= kernel parameter. |
| 111 | */ |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 112 | static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M; |
| 113 | static phys_addr_t size_cmdline = -1; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 114 | |
| 115 | static int __init early_cma(char *p) |
| 116 | { |
| 117 | pr_debug("%s(%s)\n", __func__, p); |
| 118 | size_cmdline = memparse(p, &p); |
| 119 | return 0; |
| 120 | } |
| 121 | early_param("cma", early_cma); |
| 122 | |
| 123 | #ifdef CONFIG_CMA_SIZE_PERCENTAGE |
| 124 | |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 125 | static phys_addr_t __init __maybe_unused cma_early_percent_memory(void) |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 126 | { |
| 127 | struct memblock_region *reg; |
| 128 | unsigned long total_pages = 0; |
| 129 | |
| 130 | /* |
| 131 | * We cannot use memblock_phys_mem_size() here, because |
| 132 | * memblock_analyze() has not been called yet. |
| 133 | */ |
| 134 | for_each_memblock(memory, reg) |
| 135 | total_pages += memblock_region_memory_end_pfn(reg) - |
| 136 | memblock_region_memory_base_pfn(reg); |
| 137 | |
| 138 | return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT; |
| 139 | } |
| 140 | |
| 141 | #else |
| 142 | |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 143 | static inline __maybe_unused phys_addr_t cma_early_percent_memory(void) |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 144 | { |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | #endif |
| 149 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 150 | static __init int cma_activate_area(unsigned long base_pfn, unsigned long count) |
| 151 | { |
| 152 | unsigned long pfn = base_pfn; |
| 153 | unsigned i = count >> pageblock_order; |
| 154 | struct zone *zone; |
| 155 | |
| 156 | WARN_ON_ONCE(!pfn_valid(pfn)); |
| 157 | zone = page_zone(pfn_to_page(pfn)); |
| 158 | |
| 159 | do { |
| 160 | unsigned j; |
| 161 | base_pfn = pfn; |
| 162 | for (j = pageblock_nr_pages; j; --j, pfn++) { |
| 163 | WARN_ON_ONCE(!pfn_valid(pfn)); |
| 164 | if (page_zone(pfn_to_page(pfn)) != zone) |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | init_cma_reserved_pageblock(pfn_to_page(base_pfn)); |
| 168 | } while (--i); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static __init struct cma *cma_create_area(unsigned long base_pfn, |
| 173 | unsigned long count) |
| 174 | { |
| 175 | int bitmap_size = BITS_TO_LONGS(count) * sizeof(long); |
| 176 | struct cma *cma; |
| 177 | int ret = -ENOMEM; |
| 178 | |
| 179 | pr_debug("%s(base %08lx, count %lx)\n", __func__, base_pfn, count); |
| 180 | |
| 181 | cma = kmalloc(sizeof *cma, GFP_KERNEL); |
| 182 | if (!cma) |
| 183 | return ERR_PTR(-ENOMEM); |
| 184 | |
| 185 | cma->base_pfn = base_pfn; |
| 186 | cma->count = count; |
| 187 | cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 188 | |
| 189 | if (!cma->bitmap) |
| 190 | goto no_mem; |
| 191 | |
| 192 | ret = cma_activate_area(base_pfn, count); |
| 193 | if (ret) |
| 194 | goto error; |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 195 | mutex_init(&cma->lock); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 196 | |
| 197 | pr_debug("%s: returned %p\n", __func__, (void *)cma); |
| 198 | return cma; |
| 199 | |
| 200 | error: |
| 201 | kfree(cma->bitmap); |
| 202 | no_mem: |
| 203 | kfree(cma); |
| 204 | return ERR_PTR(ret); |
| 205 | } |
| 206 | |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 207 | /*****************************************************************************/ |
| 208 | |
| 209 | #ifdef CONFIG_OF |
| 210 | int __init cma_fdt_scan(unsigned long node, const char *uname, |
| 211 | int depth, void *data) |
| 212 | { |
| 213 | phys_addr_t base, size; |
| 214 | unsigned long len; |
| 215 | __be32 *prop; |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 216 | char *name; |
Laura Abbott | 5df4240 | 2013-09-24 10:41:26 -0700 | [diff] [blame] | 217 | phys_addr_t limit = MEMBLOCK_ALLOC_ANYWHERE; |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 218 | |
Laura Abbott | 670688a | 2013-03-14 19:13:49 -0700 | [diff] [blame] | 219 | if (!of_get_flat_dt_prop(node, "linux,contiguous-region", NULL)) |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 220 | return 0; |
| 221 | |
| 222 | prop = of_get_flat_dt_prop(node, "reg", &len); |
| 223 | if (!prop || (len != 2 * sizeof(unsigned long))) |
| 224 | return 0; |
| 225 | |
| 226 | base = be32_to_cpu(prop[0]); |
| 227 | size = be32_to_cpu(prop[1]); |
| 228 | |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 229 | name = of_get_flat_dt_prop(node, "label", NULL); |
| 230 | |
Laura Abbott | 5df4240 | 2013-09-24 10:41:26 -0700 | [diff] [blame] | 231 | prop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL); |
| 232 | if (prop) |
| 233 | limit = be32_to_cpu(prop[0]); |
| 234 | |
| 235 | pr_info("Found %s, memory base %lx, size %ld MiB, limit %pa\n", uname, |
| 236 | (unsigned long)base, (unsigned long)size / SZ_1M, &limit); |
| 237 | dma_contiguous_reserve_area(size, &base, limit, name); |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | #endif |
| 242 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 243 | /** |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 244 | * dma_contiguous_reserve() - reserve area for contiguous memory handling |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 245 | * @limit: End address of the reserved memory (optional, 0 for any). |
| 246 | * |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 247 | * This function reserves memory from early allocator. It should be |
| 248 | * called by arch specific code once the early allocator (memblock or bootmem) |
| 249 | * has been activated and all other subsystems have already allocated/reserved |
| 250 | * memory. It reserves contiguous areas for global, device independent |
| 251 | * allocations and (optionally) all areas defined in device tree structures. |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 252 | */ |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 253 | void __init dma_contiguous_reserve(phys_addr_t limit) |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 254 | { |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 255 | phys_addr_t sel_size = 0; |
| 256 | |
| 257 | pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit); |
| 258 | |
| 259 | if (size_cmdline != -1) { |
| 260 | sel_size = size_cmdline; |
| 261 | } else { |
| 262 | #ifdef CONFIG_CMA_SIZE_SEL_MBYTES |
| 263 | sel_size = size_bytes; |
| 264 | #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE) |
| 265 | sel_size = cma_early_percent_memory(); |
| 266 | #elif defined(CONFIG_CMA_SIZE_SEL_MIN) |
| 267 | sel_size = min(size_bytes, cma_early_percent_memory()); |
| 268 | #elif defined(CONFIG_CMA_SIZE_SEL_MAX) |
| 269 | sel_size = max(size_bytes, cma_early_percent_memory()); |
| 270 | #endif |
| 271 | } |
| 272 | |
| 273 | if (sel_size) { |
| 274 | phys_addr_t base = 0; |
| 275 | pr_debug("%s: reserving %ld MiB for global area\n", __func__, |
| 276 | (unsigned long)sel_size / SZ_1M); |
| 277 | |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 278 | if (dma_contiguous_reserve_area(sel_size, &base, limit, NULL) |
| 279 | == 0) |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 280 | dma_contiguous_def_base = base; |
| 281 | } |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 282 | #ifdef CONFIG_OF |
| 283 | of_scan_flat_dt(cma_fdt_scan, NULL); |
| 284 | #endif |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | /** |
| 288 | * dma_contiguous_reserve_area() - reserve custom contiguous area |
| 289 | * @size: Size of the reserved area (in bytes), |
| 290 | * @base: Pointer to the base address of the reserved area, also used to return |
| 291 | * base address of the actually reserved area, optional, use pointer to |
| 292 | * 0 for any |
| 293 | * @limit: End address of the reserved memory (optional, 0 for any). |
| 294 | * |
| 295 | * This function reserves memory from early allocator. It should be |
| 296 | * called by arch specific code once the early allocator (memblock or bootmem) |
| 297 | * has been activated and all other subsystems have already allocated/reserved |
| 298 | * memory. This function allows to create custom reserved areas for specific |
| 299 | * devices. |
| 300 | */ |
| 301 | int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t *res_base, |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 302 | phys_addr_t limit, const char *name) |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 303 | { |
| 304 | phys_addr_t base = *res_base; |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 305 | phys_addr_t alignment; |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 306 | int ret = 0; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 307 | |
| 308 | pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__, |
| 309 | (unsigned long)size, (unsigned long)base, |
| 310 | (unsigned long)limit); |
| 311 | |
| 312 | /* Sanity checks */ |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 313 | if (cma_area_count == ARRAY_SIZE(cma_areas)) { |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 314 | pr_err("Not enough slots for CMA reserved regions!\n"); |
| 315 | return -ENOSPC; |
| 316 | } |
| 317 | |
| 318 | if (!size) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | /* Sanitise input arguments */ |
Marek Szyprowski | 1273137 | 2012-08-27 20:27:19 +0200 | [diff] [blame] | 322 | alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 323 | base = ALIGN(base, alignment); |
| 324 | size = ALIGN(size, alignment); |
| 325 | limit &= ~(alignment - 1); |
| 326 | |
| 327 | /* Reserve memory */ |
| 328 | if (base) { |
| 329 | if (memblock_is_region_reserved(base, size) || |
| 330 | memblock_reserve(base, size) < 0) { |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 331 | ret = -EBUSY; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 332 | goto err; |
| 333 | } |
| 334 | } else { |
| 335 | /* |
| 336 | * Use __memblock_alloc_base() since |
| 337 | * memblock_alloc_base() panic()s. |
| 338 | */ |
| 339 | phys_addr_t addr = __memblock_alloc_base(size, alignment, limit); |
| 340 | if (!addr) { |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 341 | ret = -ENOMEM; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 342 | goto err; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 343 | } else { |
| 344 | base = addr; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Each reserved area must be initialised later, when more kernel |
| 350 | * subsystems (like slab allocator) are available. |
| 351 | */ |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 352 | cma_areas[cma_area_count].base = base; |
| 353 | cma_areas[cma_area_count].size = size; |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 354 | cma_areas[cma_area_count].name = name; |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 355 | cma_area_count++; |
| 356 | *res_base = base; |
| 357 | |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 358 | pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M, |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 359 | (unsigned long)base); |
| 360 | |
| 361 | /* Architecture specific contiguous memory fixup. */ |
| 362 | dma_contiguous_early_fixup(base, size); |
| 363 | return 0; |
| 364 | err: |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 365 | pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 366 | return ret; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 370 | * dma_contiguous_add_device() - add device to custom contiguous reserved area |
| 371 | * @dev: Pointer to device structure. |
| 372 | * @base: Pointer to the base address of the reserved area returned by |
| 373 | * dma_contiguous_reserve_area() function, also used to return |
| 374 | * |
| 375 | * This function assigns the given device to the contiguous memory area |
| 376 | * reserved earlier by dma_contiguous_reserve_area() function. |
| 377 | */ |
| 378 | int __init dma_contiguous_add_device(struct device *dev, phys_addr_t base) |
| 379 | { |
| 380 | if (cma_map_count == ARRAY_SIZE(cma_maps)) { |
| 381 | pr_err("Not enough slots for CMA reserved regions!\n"); |
| 382 | return -ENOSPC; |
| 383 | } |
| 384 | cma_maps[cma_map_count].dev = dev; |
| 385 | cma_maps[cma_map_count].base = base; |
| 386 | cma_map_count++; |
| 387 | return 0; |
| 388 | } |
| 389 | |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 390 | #ifdef CONFIG_OF |
| 391 | static void cma_assign_device_from_dt(struct device *dev) |
| 392 | { |
| 393 | struct device_node *node; |
| 394 | struct cma *cma; |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 395 | const char *name; |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 396 | u32 value; |
| 397 | |
| 398 | node = of_parse_phandle(dev->of_node, "linux,contiguous-region", 0); |
| 399 | if (!node) |
| 400 | return; |
| 401 | if (of_property_read_u32(node, "reg", &value) && !value) |
| 402 | return; |
Laura Abbott | 52c4dce | 2013-02-26 10:38:34 -0800 | [diff] [blame] | 403 | |
| 404 | if (of_property_read_string(node, "label", &name)) |
| 405 | return; |
| 406 | |
| 407 | cma = cma_get_area_by_name(name); |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 408 | if (!cma) |
| 409 | return; |
| 410 | |
| 411 | dev_set_cma_area(dev, cma); |
| 412 | pr_info("Assigned CMA region at %lx to %s device\n", (unsigned long)value, dev_name(dev)); |
| 413 | } |
| 414 | |
| 415 | static int cma_device_init_notifier_call(struct notifier_block *nb, |
| 416 | unsigned long event, void *data) |
| 417 | { |
| 418 | struct device *dev = data; |
| 419 | if (event == BUS_NOTIFY_ADD_DEVICE && dev->of_node) |
| 420 | cma_assign_device_from_dt(dev); |
| 421 | return NOTIFY_DONE; |
| 422 | } |
| 423 | |
| 424 | static struct notifier_block cma_dev_init_nb = { |
| 425 | .notifier_call = cma_device_init_notifier_call, |
| 426 | }; |
| 427 | #endif |
| 428 | |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 429 | static int __init cma_init_reserved_areas(void) |
| 430 | { |
| 431 | struct cma *cma; |
| 432 | int i; |
| 433 | |
| 434 | for (i = 0; i < cma_area_count; i++) { |
| 435 | phys_addr_t base = PFN_DOWN(cma_areas[i].base); |
| 436 | unsigned int count = cma_areas[i].size >> PAGE_SHIFT; |
| 437 | |
| 438 | cma = cma_create_area(base, count); |
| 439 | if (!IS_ERR(cma)) |
| 440 | cma_areas[i].cma = cma; |
| 441 | } |
| 442 | |
| 443 | dma_contiguous_def_area = cma_get_area(dma_contiguous_def_base); |
| 444 | |
| 445 | for (i = 0; i < cma_map_count; i++) { |
| 446 | cma = cma_get_area(cma_maps[i].base); |
| 447 | dev_set_cma_area(cma_maps[i].dev, cma); |
| 448 | } |
| 449 | |
Marek Szyprowski | 4b861c6 | 2013-02-14 13:45:28 +0100 | [diff] [blame] | 450 | #ifdef CONFIG_OF |
| 451 | bus_register_notifier(&platform_bus_type, &cma_dev_init_nb); |
| 452 | #endif |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | core_initcall(cma_init_reserved_areas); |
| 456 | |
Laura Abbott | 2dcd9e6 | 2013-06-03 19:14:08 -0700 | [diff] [blame] | 457 | phys_addr_t cma_get_base(struct device *dev) |
| 458 | { |
| 459 | struct cma *cma = dev_get_cma_area(dev); |
| 460 | |
| 461 | return cma->base_pfn << PAGE_SHIFT; |
| 462 | } |
| 463 | |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 464 | static void clear_cma_bitmap(struct cma *cma, unsigned long pfn, int count) |
| 465 | { |
| 466 | mutex_lock(&cma->lock); |
| 467 | bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count); |
| 468 | mutex_unlock(&cma->lock); |
| 469 | } |
| 470 | |
Marek Szyprowski | db7909c | 2013-02-14 13:45:27 +0100 | [diff] [blame] | 471 | /** |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 472 | * dma_alloc_from_contiguous() - allocate pages from contiguous area |
| 473 | * @dev: Pointer to device for which the allocation is performed. |
| 474 | * @count: Requested number of pages. |
| 475 | * @align: Requested alignment of pages (in PAGE_SIZE order). |
| 476 | * |
| 477 | * This function allocates memory buffer for specified device. It uses |
| 478 | * device specific contiguous memory area if available or the default |
| 479 | * global one. Requires architecture specific get_dev_cma_area() helper |
| 480 | * function. |
| 481 | */ |
| 482 | struct page *dma_alloc_from_contiguous(struct device *dev, int count, |
| 483 | unsigned int align) |
| 484 | { |
| 485 | unsigned long mask, pfn, pageno, start = 0; |
| 486 | struct cma *cma = dev_get_cma_area(dev); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 487 | struct page *page = NULL; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 488 | int ret; |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 489 | int tries = 0; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 490 | |
| 491 | if (!cma || !cma->count) |
| 492 | return NULL; |
| 493 | |
| 494 | if (align > CONFIG_CMA_ALIGNMENT) |
| 495 | align = CONFIG_CMA_ALIGNMENT; |
| 496 | |
| 497 | pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma, |
| 498 | count, align); |
| 499 | |
| 500 | if (!count) |
| 501 | return NULL; |
| 502 | |
| 503 | mask = (1 << align) - 1; |
| 504 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 505 | |
| 506 | for (;;) { |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 507 | mutex_lock(&cma->lock); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 508 | pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count, |
| 509 | start, count, mask); |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 510 | if (pageno >= cma->count) { |
Laura Abbott | 2b42116 | 2014-04-02 20:25:51 -0700 | [diff] [blame] | 511 | mutex_unlock(&cma->lock); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 512 | break; |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 513 | } |
| 514 | bitmap_set(cma->bitmap, pageno, count); |
| 515 | /* |
| 516 | * It's safe to drop the lock here. We've marked this region for |
| 517 | * our exclusive use. If the migration fails we will take the |
| 518 | * lock again and unmark it. |
| 519 | */ |
| 520 | mutex_unlock(&cma->lock); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 521 | |
| 522 | pfn = cma->base_pfn + pageno; |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 523 | mutex_lock(&cma_mutex); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 524 | ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA); |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 525 | mutex_unlock(&cma_mutex); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 526 | if (ret == 0) { |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 527 | page = pfn_to_page(pfn); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 528 | break; |
| 529 | } else if (ret != -EBUSY) { |
Chintan Pandya | e840db8 | 2014-04-24 14:07:19 +0530 | [diff] [blame] | 530 | pfn = 0; |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 531 | clear_cma_bitmap(cma, pfn, count); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 532 | break; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 533 | } |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 534 | clear_cma_bitmap(cma, pfn, count); |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 535 | tries++; |
| 536 | trace_dma_alloc_contiguous_retry(tries); |
| 537 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 538 | pr_debug("%s(): memory range at %p is busy, retrying\n", |
| 539 | __func__, pfn_to_page(pfn)); |
| 540 | /* try again with a bit different memory target */ |
| 541 | start = pageno + mask + 1; |
| 542 | } |
| 543 | |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 544 | pr_debug("%s(): returned %p\n", __func__, page); |
| 545 | return page; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * dma_release_from_contiguous() - release allocated pages |
| 550 | * @dev: Pointer to device for which the pages were allocated. |
| 551 | * @pages: Allocated pages. |
| 552 | * @count: Number of allocated pages. |
| 553 | * |
| 554 | * This function releases memory allocated by dma_alloc_from_contiguous(). |
| 555 | * It returns false when provided pages do not belong to contiguous area and |
| 556 | * true otherwise. |
| 557 | */ |
| 558 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 559 | int count) |
| 560 | { |
| 561 | struct cma *cma = dev_get_cma_area(dev); |
| 562 | unsigned long pfn; |
| 563 | |
| 564 | if (!cma || !pages) |
| 565 | return false; |
| 566 | |
| 567 | pr_debug("%s(page %p)\n", __func__, (void *)pages); |
| 568 | |
| 569 | pfn = page_to_pfn(pages); |
| 570 | |
| 571 | if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) |
| 572 | return false; |
| 573 | |
| 574 | VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); |
| 575 | |
Laura Abbott | 5adb04c | 2014-02-27 10:23:51 -0800 | [diff] [blame] | 576 | free_contig_range(pfn, count); |
Laura Abbott | 7761c98 | 2014-03-20 14:21:17 -0700 | [diff] [blame] | 577 | clear_cma_bitmap(cma, pfn, count); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 578 | |
| 579 | return true; |
| 580 | } |