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