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. |
| 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 Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 34 | #include <trace/events/kmem.h> |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 35 | |
| 36 | #ifndef SZ_1M |
| 37 | #define SZ_1M (1 << 20) |
| 38 | #endif |
| 39 | |
| 40 | struct cma { |
| 41 | unsigned long base_pfn; |
| 42 | unsigned long count; |
| 43 | unsigned long *bitmap; |
| 44 | }; |
| 45 | |
| 46 | struct 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 Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 64 | static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M; |
| 65 | static phys_addr_t size_cmdline = -1; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 66 | |
| 67 | static 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 | } |
| 73 | early_param("cma", early_cma); |
| 74 | |
| 75 | #ifdef CONFIG_CMA_SIZE_PERCENTAGE |
| 76 | |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 77 | static phys_addr_t __init __maybe_unused cma_early_percent_memory(void) |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 78 | { |
| 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 Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 95 | static inline __maybe_unused phys_addr_t cma_early_percent_memory(void) |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 96 | { |
| 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 | */ |
| 111 | void __init dma_contiguous_reserve(phys_addr_t limit) |
| 112 | { |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 113 | phys_addr_t selected_size = 0; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 114 | |
| 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 Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 133 | (unsigned long)selected_size / SZ_1M); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 134 | |
| 135 | dma_declare_contiguous(NULL, selected_size, 0, limit); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | static DEFINE_MUTEX(cma_mutex); |
| 140 | |
| 141 | static __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 | |
| 163 | static __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 | |
| 190 | error: |
| 191 | kfree(cma->bitmap); |
| 192 | no_mem: |
| 193 | kfree(cma); |
| 194 | return ERR_PTR(ret); |
| 195 | } |
| 196 | |
| 197 | static struct cma_reserved { |
| 198 | phys_addr_t start; |
| 199 | unsigned long size; |
| 200 | struct device *dev; |
| 201 | } cma_reserved[MAX_CMA_AREAS] __initdata; |
| 202 | static unsigned cma_reserved_count __initdata; |
| 203 | |
| 204 | static 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 | } |
| 220 | core_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 Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 234 | int __init dma_declare_contiguous(struct device *dev, phys_addr_t size, |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 235 | phys_addr_t base, phys_addr_t limit) |
| 236 | { |
| 237 | struct cma_reserved *r = &cma_reserved[cma_reserved_count]; |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 238 | phys_addr_t alignment; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 239 | |
| 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 Szyprowski | 1273137 | 2012-08-27 20:27:19 +0200 | [diff] [blame] | 254 | alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 255 | 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 Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 275 | } 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 Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 288 | 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] | 289 | (unsigned long)base); |
| 290 | |
| 291 | /* Architecture specific contiguous memory fixup. */ |
| 292 | dma_contiguous_early_fixup(base, size); |
| 293 | return 0; |
| 294 | err: |
Vitaly Andrianov | 2ee0174 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 295 | pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 296 | 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 | */ |
| 310 | struct 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); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 315 | struct page *page = NULL; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 316 | int ret; |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 317 | int tries = 0; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 318 | |
| 319 | if (!cma || !cma->count) |
| 320 | return NULL; |
| 321 | |
| 322 | if (align > CONFIG_CMA_ALIGNMENT) |
| 323 | align = CONFIG_CMA_ALIGNMENT; |
| 324 | |
| 325 | pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma, |
| 326 | count, align); |
| 327 | |
| 328 | if (!count) |
| 329 | return NULL; |
| 330 | |
| 331 | mask = (1 << align) - 1; |
| 332 | |
| 333 | mutex_lock(&cma_mutex); |
| 334 | |
| 335 | for (;;) { |
| 336 | pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count, |
| 337 | start, count, mask); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 338 | if (pageno >= cma->count) |
| 339 | break; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 340 | |
| 341 | pfn = cma->base_pfn + pageno; |
| 342 | ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA); |
| 343 | if (ret == 0) { |
| 344 | bitmap_set(cma->bitmap, pageno, count); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 345 | page = pfn_to_page(pfn); |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 346 | break; |
| 347 | } else if (ret != -EBUSY) { |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 348 | break; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 349 | } |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 350 | tries++; |
| 351 | trace_dma_alloc_contiguous_retry(tries); |
| 352 | |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 353 | 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); |
Michal Nazarewicz | 81e1096 | 2012-09-05 07:50:41 +0200 | [diff] [blame] | 360 | pr_debug("%s(): returned %p\n", __func__, page); |
| 361 | return page; |
Marek Szyprowski | 55bb033 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * dma_release_from_contiguous() - release allocated pages |
| 366 | * @dev: Pointer to device for which the pages were allocated. |
| 367 | * @pages: Allocated pages. |
| 368 | * @count: Number of allocated pages. |
| 369 | * |
| 370 | * This function releases memory allocated by dma_alloc_from_contiguous(). |
| 371 | * It returns false when provided pages do not belong to contiguous area and |
| 372 | * true otherwise. |
| 373 | */ |
| 374 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 375 | int count) |
| 376 | { |
| 377 | struct cma *cma = dev_get_cma_area(dev); |
| 378 | unsigned long pfn; |
| 379 | |
| 380 | if (!cma || !pages) |
| 381 | return false; |
| 382 | |
| 383 | pr_debug("%s(page %p)\n", __func__, (void *)pages); |
| 384 | |
| 385 | pfn = page_to_pfn(pages); |
| 386 | |
| 387 | if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) |
| 388 | return false; |
| 389 | |
| 390 | VM_BUG_ON(pfn + count > cma->base_pfn + cma->count); |
| 391 | |
| 392 | mutex_lock(&cma_mutex); |
| 393 | bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count); |
| 394 | free_contig_range(pfn, count); |
| 395 | mutex_unlock(&cma_mutex); |
| 396 | |
| 397 | return true; |
| 398 | } |