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 dma_contiguous_reserve_area(selected_size, selected_base, 261 selected_limit, 262 &dma_contiguous_default_area, 263 fixed); 264 265 ret = dma_heap_cma_register_heap(dma_contiguous_default_area); 266 if (ret) 267 pr_warn("Couldn't register default CMA heap."); 268 } 269 } 270 271 void __weak 272 dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) 273 { 274 } 275 276 /** 277 * dma_contiguous_reserve_area() - reserve custom contiguous area 278 * @size: Size of the reserved area (in bytes), 279 * @base: Base address of the reserved area optional, use 0 for any 280 * @limit: End address of the reserved memory (optional, 0 for any). 281 * @res_cma: Pointer to store the created cma region. 282 * @fixed: hint about where to place the reserved area 283 * 284 * This function reserves memory from early allocator. It should be 285 * called by arch specific code once the early allocator (memblock or bootmem) 286 * has been activated and all other subsystems have already allocated/reserved 287 * memory. This function allows to create custom reserved areas for specific 288 * devices. 289 * 290 * If @fixed is true, reserve contiguous area at exactly @base. If false, 291 * reserve in range from @base to @limit. 292 */ 293 int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, 294 phys_addr_t limit, struct cma **res_cma, 295 bool fixed) 296 { 297 int ret; 298 299 ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed, 300 "reserved", res_cma); 301 if (ret) 302 return ret; 303 304 /* Architecture specific contiguous memory fixup. */ 305 dma_contiguous_early_fixup(cma_get_base(*res_cma), 306 cma_get_size(*res_cma)); 307 308 return 0; 309 } 310 311 /** 312 * dma_alloc_from_contiguous() - allocate pages from contiguous area 313 * @dev: Pointer to device for which the allocation is performed. 314 * @count: Requested number of pages. 315 * @align: Requested alignment of pages (in PAGE_SIZE order). 316 * @no_warn: Avoid printing message about failed allocation. 317 * 318 * This function allocates memory buffer for specified device. It uses 319 * device specific contiguous memory area if available or the default 320 * global one. Requires architecture specific dev_get_cma_area() helper 321 * function. 322 */ 323 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, 324 unsigned int align, bool no_warn) 325 { 326 if (align > CONFIG_CMA_ALIGNMENT) 327 align = CONFIG_CMA_ALIGNMENT; 328 329 return cma_alloc(dev_get_cma_area(dev), count, align, no_warn); 330 } 331 332 /** 333 * dma_release_from_contiguous() - release allocated pages 334 * @dev: Pointer to device for which the pages were allocated. 335 * @pages: Allocated pages. 336 * @count: Number of allocated pages. 337 * 338 * This function releases memory allocated by dma_alloc_from_contiguous(). 339 * It returns false when provided pages do not belong to contiguous area and 340 * true otherwise. 341 */ 342 bool dma_release_from_contiguous(struct device *dev, struct page *pages, 343 int count) 344 { 345 return cma_release(dev_get_cma_area(dev), pages, count); 346 } 347 348 static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp) 349 { 350 unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT); 351 352 return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN); 353 } 354 355 /** 356 * dma_alloc_contiguous() - allocate contiguous pages 357 * @dev: Pointer to device for which the allocation is performed. 358 * @size: Requested allocation size. 359 * @gfp: Allocation flags. 360 * 361 * tries to use device specific contiguous memory area if available, or it 362 * tries to use per-numa cma, if the allocation fails, it will fallback to 363 * try default global one. 364 * 365 * Note that it bypass one-page size of allocations from the per-numa and 366 * global area as the addresses within one page are always contiguous, so 367 * there is no need to waste CMA pages for that kind; it also helps reduce 368 * fragmentations. 369 */ 370 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp) 371 { 372 #ifdef CONFIG_DMA_NUMA_CMA 373 int nid = dev_to_node(dev); 374 #endif 375 376 /* CMA can be used only in the context which permits sleeping */ 377 if (!gfpflags_allow_blocking(gfp)) 378 return NULL; 379 if (dev->cma_area) 380 return cma_alloc_aligned(dev->cma_area, size, gfp); 381 if (size <= PAGE_SIZE) 382 return NULL; 383 384 #ifdef CONFIG_DMA_NUMA_CMA 385 if (nid != NUMA_NO_NODE && !(gfp & (GFP_DMA | GFP_DMA32))) { 386 struct cma *cma = dma_contiguous_pernuma_area[nid]; 387 struct page *page; 388 389 if (cma) { 390 page = cma_alloc_aligned(cma, size, gfp); 391 if (page) 392 return page; 393 } 394 395 cma = dma_contiguous_numa_area[nid]; 396 if (cma) { 397 page = cma_alloc_aligned(cma, size, gfp); 398 if (page) 399 return page; 400 } 401 } 402 #endif 403 if (!dma_contiguous_default_area) 404 return NULL; 405 406 return cma_alloc_aligned(dma_contiguous_default_area, size, gfp); 407 } 408 409 /** 410 * dma_free_contiguous() - release allocated pages 411 * @dev: Pointer to device for which the pages were allocated. 412 * @page: Pointer to the allocated pages. 413 * @size: Size of allocated pages. 414 * 415 * This function releases memory allocated by dma_alloc_contiguous(). As the 416 * cma_release returns false when provided pages do not belong to contiguous 417 * area and true otherwise, this function then does a fallback __free_pages() 418 * upon a false-return. 419 */ 420 void dma_free_contiguous(struct device *dev, struct page *page, size_t size) 421 { 422 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; 423 424 /* if dev has its own cma, free page from there */ 425 if (dev->cma_area) { 426 if (cma_release(dev->cma_area, page, count)) 427 return; 428 } else { 429 /* 430 * otherwise, page is from either per-numa cma or default cma 431 */ 432 #ifdef CONFIG_DMA_NUMA_CMA 433 if (cma_release(dma_contiguous_pernuma_area[page_to_nid(page)], 434 page, count)) 435 return; 436 if (cma_release(dma_contiguous_numa_area[page_to_nid(page)], 437 page, count)) 438 return; 439 #endif 440 if (cma_release(dma_contiguous_default_area, page, count)) 441 return; 442 } 443 444 /* not in any cma, free from buddy */ 445 __free_pages(page, get_order(size)); 446 } 447 448 /* 449 * Support for reserved memory regions defined in device tree 450 */ 451 #ifdef CONFIG_OF_RESERVED_MEM 452 #include <linux/of.h> 453 #include <linux/of_fdt.h> 454 #include <linux/of_reserved_mem.h> 455 456 #undef pr_fmt 457 #define pr_fmt(fmt) fmt 458 459 static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev) 460 { 461 dev->cma_area = rmem->priv; 462 return 0; 463 } 464 465 static void rmem_cma_device_release(struct reserved_mem *rmem, 466 struct device *dev) 467 { 468 dev->cma_area = NULL; 469 } 470 471 static const struct reserved_mem_ops rmem_cma_ops = { 472 .device_init = rmem_cma_device_init, 473 .device_release = rmem_cma_device_release, 474 }; 475 476 static int __init rmem_cma_setup(struct reserved_mem *rmem) 477 { 478 unsigned long node = rmem->fdt_node; 479 bool default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL); 480 struct cma *cma; 481 int err; 482 483 if (!of_get_flat_dt_prop(node, "reusable", NULL) || 484 of_get_flat_dt_prop(node, "no-map", NULL)) 485 return -EINVAL; 486 487 if (!IS_ALIGNED(rmem->base | rmem->size, CMA_MIN_ALIGNMENT_BYTES)) { 488 pr_err("Reserved memory: incorrect alignment of CMA region\n"); 489 return -EINVAL; 490 } 491 492 err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma); 493 if (err) { 494 pr_err("Reserved memory: unable to setup CMA region\n"); 495 return err; 496 } 497 498 if (default_cma) 499 dma_contiguous_default_area = cma; 500 501 rmem->ops = &rmem_cma_ops; 502 rmem->priv = cma; 503 504 pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n", 505 &rmem->base, (unsigned long)rmem->size / SZ_1M); 506 507 err = dma_heap_cma_register_heap(cma); 508 if (err) 509 pr_warn("Couldn't register CMA heap."); 510 511 return 0; 512 } 513 RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup); 514 #endif 515