1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Contiguous Memory Allocator for DMA mapping framework 4 * Copyright (c) 2010-2011 by Samsung Electronics. 5 * Written by: 6 * Marek Szyprowski <m.szyprowski@samsung.com> 7 * Michal Nazarewicz <mina86@mina86.com> 8 * 9 * Contiguous Memory Allocator 10 * 11 * The Contiguous Memory Allocator (CMA) makes it possible to 12 * allocate big contiguous chunks of memory after the system has 13 * booted. 14 * 15 * Why is it needed? 16 * 17 * Various devices on embedded systems have no scatter-getter and/or 18 * IO map support and require contiguous blocks of memory to 19 * operate. They include devices such as cameras, hardware video 20 * coders, etc. 21 * 22 * Such devices often require big memory buffers (a full HD frame 23 * is, for instance, more than 2 mega pixels large, i.e. more than 6 24 * MB of memory), which makes mechanisms such as kmalloc() or 25 * alloc_page() ineffective. 26 * 27 * At the same time, a solution where a big memory region is 28 * reserved for a device is suboptimal since often more memory is 29 * reserved then strictly required and, moreover, the memory is 30 * inaccessible to page system even if device drivers don't use it. 31 * 32 * CMA tries to solve this issue by operating on memory regions 33 * where only movable pages can be allocated from. This way, kernel 34 * can use the memory for pagecache and when device driver requests 35 * it, allocated pages can be migrated. 36 */ 37 38 #define pr_fmt(fmt) "cma: " fmt 39 40 #include <asm/page.h> 41 42 #include <linux/memblock.h> 43 #include <linux/err.h> 44 #include <linux/sizes.h> 45 #include <linux/dma-buf/heaps/cma.h> 46 #include <linux/dma-map-ops.h> 47 #include <linux/cma.h> 48 #include <linux/nospec.h> 49 50 #ifdef CONFIG_CMA_SIZE_MBYTES 51 #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES 52 #else 53 #define CMA_SIZE_MBYTES 0 54 #endif 55 56 struct cma *dma_contiguous_default_area; 57 58 /* 59 * Default global CMA area size can be defined in kernel's .config. 60 * This is useful mainly for distro maintainers to create a kernel 61 * that works correctly for most supported systems. 62 * The size can be set in bytes or as a percentage of the total memory 63 * in the system. 64 * 65 * Users, who want to set the size of global CMA area for their system 66 * should use cma= kernel parameter. 67 */ 68 #define size_bytes ((phys_addr_t)CMA_SIZE_MBYTES * SZ_1M) 69 static phys_addr_t size_cmdline __initdata = -1; 70 static phys_addr_t base_cmdline __initdata; 71 static phys_addr_t limit_cmdline __initdata; 72 73 static int __init early_cma(char *p) 74 { 75 if (!p) { 76 pr_err("Config string not provided\n"); 77 return -EINVAL; 78 } 79 80 size_cmdline = memparse(p, &p); 81 if (*p != '@') 82 return 0; 83 base_cmdline = memparse(p + 1, &p); 84 if (*p != '-') { 85 limit_cmdline = base_cmdline + size_cmdline; 86 return 0; 87 } 88 limit_cmdline = memparse(p + 1, &p); 89 90 return 0; 91 } 92 early_param("cma", early_cma); 93 94 /* 95 * cma_skip_dt_default_reserved_mem - This is called from the 96 * reserved_mem framework to detect if the default cma region is being 97 * set by the "cma=" kernel parameter. 98 */ 99 bool __init cma_skip_dt_default_reserved_mem(void) 100 { 101 return size_cmdline != -1; 102 } 103 104 #ifdef CONFIG_DMA_NUMA_CMA 105 106 static struct cma *dma_contiguous_numa_area[MAX_NUMNODES]; 107 static phys_addr_t numa_cma_size[MAX_NUMNODES] __initdata; 108 static struct cma *dma_contiguous_pernuma_area[MAX_NUMNODES]; 109 static phys_addr_t pernuma_size_bytes __initdata; 110 111 static int __init early_numa_cma(char *p) 112 { 113 int nid, count = 0; 114 unsigned long tmp; 115 char *s = p; 116 117 while (*s) { 118 if (sscanf(s, "%lu%n", &tmp, &count) != 1) 119 break; 120 121 if (s[count] == ':') { 122 if (tmp >= MAX_NUMNODES) 123 break; 124 nid = array_index_nospec(tmp, MAX_NUMNODES); 125 126 s += count + 1; 127 tmp = memparse(s, &s); 128 numa_cma_size[nid] = tmp; 129 130 if (*s == ',') 131 s++; 132 else 133 break; 134 } else 135 break; 136 } 137 138 return 0; 139 } 140 early_param("numa_cma", early_numa_cma); 141 142 static int __init early_cma_pernuma(char *p) 143 { 144 pernuma_size_bytes = memparse(p, &p); 145 return 0; 146 } 147 early_param("cma_pernuma", early_cma_pernuma); 148 #endif 149 150 #ifdef CONFIG_CMA_SIZE_PERCENTAGE 151 152 static phys_addr_t __init __maybe_unused cma_early_percent_memory(void) 153 { 154 unsigned long total_pages = PHYS_PFN(memblock_phys_mem_size()); 155 156 return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT; 157 } 158 159 #else 160 161 static inline __maybe_unused phys_addr_t cma_early_percent_memory(void) 162 { 163 return 0; 164 } 165 166 #endif 167 168 #ifdef CONFIG_DMA_NUMA_CMA 169 static void __init dma_numa_cma_reserve(void) 170 { 171 int nid; 172 173 for_each_node(nid) { 174 int ret; 175 char name[CMA_MAX_NAME]; 176 struct cma **cma; 177 178 if (!node_online(nid)) { 179 if (pernuma_size_bytes || numa_cma_size[nid]) 180 pr_warn("invalid node %d specified\n", nid); 181 continue; 182 } 183 184 if (pernuma_size_bytes) { 185 186 cma = &dma_contiguous_pernuma_area[nid]; 187 snprintf(name, sizeof(name), "pernuma%d", nid); 188 ret = cma_declare_contiguous_nid(0, pernuma_size_bytes, 0, 0, 189 0, false, name, cma, nid); 190 if (ret) 191 pr_warn("%s: reservation failed: err %d, node %d", __func__, 192 ret, nid); 193 } 194 195 if (numa_cma_size[nid]) { 196 197 cma = &dma_contiguous_numa_area[nid]; 198 snprintf(name, sizeof(name), "numa%d", nid); 199 ret = cma_declare_contiguous_nid(0, numa_cma_size[nid], 0, 0, 0, false, 200 name, cma, nid); 201 if (ret) 202 pr_warn("%s: reservation failed: err %d, node %d", __func__, 203 ret, nid); 204 } 205 } 206 } 207 #else 208 static inline void __init dma_numa_cma_reserve(void) 209 { 210 } 211 #endif 212 213 /** 214 * dma_contiguous_reserve() - reserve area(s) for contiguous memory handling 215 * @limit: End address of the reserved memory (optional, 0 for any). 216 * 217 * This function reserves memory from early allocator. It should be 218 * called by arch specific code once the early allocator (memblock or bootmem) 219 * has been activated and all other subsystems have already allocated/reserved 220 * memory. 221 */ 222 void __init dma_contiguous_reserve(phys_addr_t limit) 223 { 224 phys_addr_t selected_size = 0; 225 phys_addr_t selected_base = 0; 226 phys_addr_t selected_limit = limit; 227 bool fixed = false; 228 229 dma_numa_cma_reserve(); 230 231 pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit); 232 233 if (size_cmdline != -1) { 234 selected_size = size_cmdline; 235 selected_base = base_cmdline; 236 237 /* Hornor the user setup dma address limit */ 238 selected_limit = limit_cmdline ?: limit; 239 240 if (base_cmdline + size_cmdline == limit_cmdline) 241 fixed = true; 242 } else { 243 #ifdef CONFIG_CMA_SIZE_SEL_MBYTES 244 selected_size = size_bytes; 245 #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE) 246 selected_size = cma_early_percent_memory(); 247 #elif defined(CONFIG_CMA_SIZE_SEL_MIN) 248 selected_size = min(size_bytes, cma_early_percent_memory()); 249 #elif defined(CONFIG_CMA_SIZE_SEL_MAX) 250 selected_size = max(size_bytes, cma_early_percent_memory()); 251 #endif 252 } 253 254 if (selected_size && !dma_contiguous_default_area) { 255 int ret; 256 257 pr_debug("%s: reserving %ld MiB for global area\n", __func__, 258 (unsigned long)selected_size / SZ_1M); 259 260 ret = dma_contiguous_reserve_area(selected_size, selected_base, 261 selected_limit, 262 &dma_contiguous_default_area, 263 fixed); 264 if (ret) 265 return; 266 267 ret = dma_heap_cma_register_heap(dma_contiguous_default_area); 268 if (ret) 269 pr_warn("Couldn't register default CMA heap."); 270 } 271 } 272 273 void __weak 274 dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) 275 { 276 } 277 278 /** 279 * dma_contiguous_reserve_area() - reserve custom contiguous area 280 * @size: Size of the reserved area (in bytes), 281 * @base: Base address of the reserved area optional, use 0 for any 282 * @limit: End address of the reserved memory (optional, 0 for any). 283 * @res_cma: Pointer to store the created cma region. 284 * @fixed: hint about where to place the reserved area 285 * 286 * This function reserves memory from early allocator. It should be 287 * called by arch specific code once the early allocator (memblock or bootmem) 288 * has been activated and all other subsystems have already allocated/reserved 289 * memory. This function allows to create custom reserved areas for specific 290 * devices. 291 * 292 * If @fixed is true, reserve contiguous area at exactly @base. If false, 293 * reserve in range from @base to @limit. 294 */ 295 int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, 296 phys_addr_t limit, struct cma **res_cma, 297 bool fixed) 298 { 299 int ret; 300 301 ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed, 302 "reserved", res_cma); 303 if (ret) 304 return ret; 305 306 /* Architecture specific contiguous memory fixup. */ 307 dma_contiguous_early_fixup(cma_get_base(*res_cma), 308 cma_get_size(*res_cma)); 309 310 return 0; 311 } 312 313 /** 314 * dma_alloc_from_contiguous() - allocate pages from contiguous area 315 * @dev: Pointer to device for which the allocation is performed. 316 * @count: Requested number of pages. 317 * @align: Requested alignment of pages (in PAGE_SIZE order). 318 * @no_warn: Avoid printing message about failed allocation. 319 * 320 * This function allocates memory buffer for specified device. It uses 321 * device specific contiguous memory area if available or the default 322 * global one. Requires architecture specific dev_get_cma_area() helper 323 * function. 324 */ 325 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, 326 unsigned int align, bool no_warn) 327 { 328 if (align > CONFIG_CMA_ALIGNMENT) 329 align = CONFIG_CMA_ALIGNMENT; 330 331 return cma_alloc(dev_get_cma_area(dev), count, align, no_warn); 332 } 333 334 /** 335 * dma_release_from_contiguous() - release allocated pages 336 * @dev: Pointer to device for which the pages were allocated. 337 * @pages: Allocated pages. 338 * @count: Number of allocated pages. 339 * 340 * This function releases memory allocated by dma_alloc_from_contiguous(). 341 * It returns false when provided pages do not belong to contiguous area and 342 * true otherwise. 343 */ 344 bool dma_release_from_contiguous(struct device *dev, struct page *pages, 345 int count) 346 { 347 return cma_release(dev_get_cma_area(dev), pages, count); 348 } 349 350 static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp) 351 { 352 unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT); 353 354 return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN); 355 } 356 357 /** 358 * dma_alloc_contiguous() - allocate contiguous pages 359 * @dev: Pointer to device for which the allocation is performed. 360 * @size: Requested allocation size. 361 * @gfp: Allocation flags. 362 * 363 * tries to use device specific contiguous memory area if available, or it 364 * tries to use per-numa cma, if the allocation fails, it will fallback to 365 * try default global one. 366 * 367 * Note that it bypass one-page size of allocations from the per-numa and 368 * global area as the addresses within one page are always contiguous, so 369 * there is no need to waste CMA pages for that kind; it also helps reduce 370 * fragmentations. 371 */ 372 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) 373 { 374 #ifdef CONFIG_DMA_NUMA_CMA 375 int nid = dev_to_node(dev); 376 #endif 377 378 /* CMA can be used only in the context which permits sleeping */ 379 if (!gfpflags_allow_blocking(gfp)) 380 return NULL; 381 if (dev->cma_area) 382 return cma_alloc_aligned(dev->cma_area, size, gfp); 383 if (size <= PAGE_SIZE) 384 return NULL; 385 386 #ifdef CONFIG_DMA_NUMA_CMA 387 if (nid != NUMA_NO_NODE && !(gfp & (GFP_DMA | GFP_DMA32))) { 388 struct cma *cma = dma_contiguous_pernuma_area[nid]; 389 struct page *page; 390 391 if (cma) { 392 page = cma_alloc_aligned(cma, size, gfp); 393 if (page) 394 return page; 395 } 396 397 cma = dma_contiguous_numa_area[nid]; 398 if (cma) { 399 page = cma_alloc_aligned(cma, size, gfp); 400 if (page) 401 return page; 402 } 403 } 404 #endif 405 if (!dma_contiguous_default_area) 406 return NULL; 407 408 return cma_alloc_aligned(dma_contiguous_default_area, size, gfp); 409 } 410 411 /** 412 * dma_free_contiguous() - release allocated pages 413 * @dev: Pointer to device for which the pages were allocated. 414 * @page: Pointer to the allocated pages. 415 * @size: Size of allocated pages. 416 * 417 * This function releases memory allocated by dma_alloc_contiguous(). As the 418 * cma_release returns false when provided pages do not belong to contiguous 419 * area and true otherwise, this function then does a fallback __free_pages() 420 * upon a false-return. 421 */ 422 void dma_free_contiguous(struct device *dev, struct page *page, size_t size) 423 { 424 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; 425 426 /* if dev has its own cma, free page from there */ 427 if (dev->cma_area) { 428 if (cma_release(dev->cma_area, page, count)) 429 return; 430 } else { 431 /* 432 * otherwise, page is from either per-numa cma or default cma 433 */ 434 #ifdef CONFIG_DMA_NUMA_CMA 435 if (cma_release(dma_contiguous_pernuma_area[page_to_nid(page)], 436 page, count)) 437 return; 438 if (cma_release(dma_contiguous_numa_area[page_to_nid(page)], 439 page, count)) 440 return; 441 #endif 442 if (cma_release(dma_contiguous_default_area, page, count)) 443 return; 444 } 445 446 /* not in any cma, free from buddy */ 447 __free_pages(page, get_order(size)); 448 } 449 450 /* 451 * Support for reserved memory regions defined in device tree 452 */ 453 #ifdef CONFIG_OF_RESERVED_MEM 454 #include <linux/of.h> 455 #include <linux/of_fdt.h> 456 #include <linux/of_reserved_mem.h> 457 458 #undef pr_fmt 459 #define pr_fmt(fmt) fmt 460 461 static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev) 462 { 463 dev->cma_area = rmem->priv; 464 return 0; 465 } 466 467 static void rmem_cma_device_release(struct reserved_mem *rmem, 468 struct device *dev) 469 { 470 dev->cma_area = NULL; 471 } 472 473 static const struct reserved_mem_ops rmem_cma_ops = { 474 .device_init = rmem_cma_device_init, 475 .device_release = rmem_cma_device_release, 476 }; 477 478 static int __init rmem_cma_setup(struct reserved_mem *rmem) 479 { 480 unsigned long node = rmem->fdt_node; 481 bool default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL); 482 struct cma *cma; 483 int err; 484 485 if (!of_get_flat_dt_prop(node, "reusable", NULL) || 486 of_get_flat_dt_prop(node, "no-map", NULL)) 487 return -EINVAL; 488 489 if (!IS_ALIGNED(rmem->base | rmem->size, CMA_MIN_ALIGNMENT_BYTES)) { 490 pr_err("Reserved memory: incorrect alignment of CMA region\n"); 491 return -EINVAL; 492 } 493 494 err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma); 495 if (err) { 496 pr_err("Reserved memory: unable to setup CMA region\n"); 497 return err; 498 } 499 500 if (default_cma) 501 dma_contiguous_default_area = cma; 502 503 rmem->ops = &rmem_cma_ops; 504 rmem->priv = cma; 505 506 pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n", 507 &rmem->base, (unsigned long)rmem->size / SZ_1M); 508 509 err = dma_heap_cma_register_heap(cma); 510 if (err) 511 pr_warn("Couldn't register CMA heap."); 512 513 return 0; 514 } 515 RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup); 516 #endif 517