contiguous.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #define pr_fmt(fmt) "cma: " fmt
  38. #include <asm/page.h>
  39. #include <linux/memblock.h>
  40. #include <linux/err.h>
  41. #include <linux/sizes.h>
  42. #include <linux/dma-map-ops.h>
  43. #include <linux/cma.h>
  44. #include <linux/nospec.h>
  45. #ifdef CONFIG_CMA_SIZE_MBYTES
  46. #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
  47. #else
  48. #define CMA_SIZE_MBYTES 0
  49. #endif
  50. struct cma *dma_contiguous_default_area;
  51. /*
  52. * Default global CMA area size can be defined in kernel's .config.
  53. * This is useful mainly for distro maintainers to create a kernel
  54. * that works correctly for most supported systems.
  55. * The size can be set in bytes or as a percentage of the total memory
  56. * in the system.
  57. *
  58. * Users, who want to set the size of global CMA area for their system
  59. * should use cma= kernel parameter.
  60. */
  61. static const phys_addr_t size_bytes __initconst =
  62. (phys_addr_t)CMA_SIZE_MBYTES * SZ_1M;
  63. static phys_addr_t size_cmdline __initdata = -1;
  64. static phys_addr_t base_cmdline __initdata;
  65. static phys_addr_t limit_cmdline __initdata;
  66. static int __init early_cma(char *p)
  67. {
  68. if (!p) {
  69. pr_err("Config string not provided\n");
  70. return -EINVAL;
  71. }
  72. size_cmdline = memparse(p, &p);
  73. if (*p != '@')
  74. return 0;
  75. base_cmdline = memparse(p + 1, &p);
  76. if (*p != '-') {
  77. limit_cmdline = base_cmdline + size_cmdline;
  78. return 0;
  79. }
  80. limit_cmdline = memparse(p + 1, &p);
  81. return 0;
  82. }
  83. early_param("cma", early_cma);
  84. #ifdef CONFIG_DMA_NUMA_CMA
  85. static struct cma *dma_contiguous_numa_area[MAX_NUMNODES];
  86. static phys_addr_t numa_cma_size[MAX_NUMNODES] __initdata;
  87. static struct cma *dma_contiguous_pernuma_area[MAX_NUMNODES];
  88. static phys_addr_t pernuma_size_bytes __initdata;
  89. static int __init early_numa_cma(char *p)
  90. {
  91. int nid, count = 0;
  92. unsigned long tmp;
  93. char *s = p;
  94. while (*s) {
  95. if (sscanf(s, "%lu%n", &tmp, &count) != 1)
  96. break;
  97. if (s[count] == ':') {
  98. if (tmp >= MAX_NUMNODES)
  99. break;
  100. nid = array_index_nospec(tmp, MAX_NUMNODES);
  101. s += count + 1;
  102. tmp = memparse(s, &s);
  103. numa_cma_size[nid] = tmp;
  104. if (*s == ',')
  105. s++;
  106. else
  107. break;
  108. } else
  109. break;
  110. }
  111. return 0;
  112. }
  113. early_param("numa_cma", early_numa_cma);
  114. static int __init early_cma_pernuma(char *p)
  115. {
  116. pernuma_size_bytes = memparse(p, &p);
  117. return 0;
  118. }
  119. early_param("cma_pernuma", early_cma_pernuma);
  120. #endif
  121. #ifdef CONFIG_CMA_SIZE_PERCENTAGE
  122. static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
  123. {
  124. unsigned long total_pages = PHYS_PFN(memblock_phys_mem_size());
  125. return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
  126. }
  127. #else
  128. static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
  129. {
  130. return 0;
  131. }
  132. #endif
  133. #ifdef CONFIG_DMA_NUMA_CMA
  134. static void __init dma_numa_cma_reserve(void)
  135. {
  136. int nid;
  137. for_each_node(nid) {
  138. int ret;
  139. char name[CMA_MAX_NAME];
  140. struct cma **cma;
  141. if (!node_online(nid)) {
  142. if (pernuma_size_bytes || numa_cma_size[nid])
  143. pr_warn("invalid node %d specified\n", nid);
  144. continue;
  145. }
  146. if (pernuma_size_bytes) {
  147. cma = &dma_contiguous_pernuma_area[nid];
  148. snprintf(name, sizeof(name), "pernuma%d", nid);
  149. ret = cma_declare_contiguous_nid(0, pernuma_size_bytes, 0, 0,
  150. 0, false, name, cma, nid);
  151. if (ret)
  152. pr_warn("%s: reservation failed: err %d, node %d", __func__,
  153. ret, nid);
  154. }
  155. if (numa_cma_size[nid]) {
  156. cma = &dma_contiguous_numa_area[nid];
  157. snprintf(name, sizeof(name), "numa%d", nid);
  158. ret = cma_declare_contiguous_nid(0, numa_cma_size[nid], 0, 0, 0, false,
  159. name, cma, nid);
  160. if (ret)
  161. pr_warn("%s: reservation failed: err %d, node %d", __func__,
  162. ret, nid);
  163. }
  164. }
  165. }
  166. #else
  167. static inline void __init dma_numa_cma_reserve(void)
  168. {
  169. }
  170. #endif
  171. /**
  172. * dma_contiguous_reserve() - reserve area(s) for contiguous memory handling
  173. * @limit: End address of the reserved memory (optional, 0 for any).
  174. *
  175. * This function reserves memory from early allocator. It should be
  176. * called by arch specific code once the early allocator (memblock or bootmem)
  177. * has been activated and all other subsystems have already allocated/reserved
  178. * memory.
  179. */
  180. void __init dma_contiguous_reserve(phys_addr_t limit)
  181. {
  182. phys_addr_t selected_size = 0;
  183. phys_addr_t selected_base = 0;
  184. phys_addr_t selected_limit = limit;
  185. bool fixed = false;
  186. dma_numa_cma_reserve();
  187. pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
  188. if (size_cmdline != -1) {
  189. selected_size = size_cmdline;
  190. selected_base = base_cmdline;
  191. selected_limit = min_not_zero(limit_cmdline, limit);
  192. if (base_cmdline + size_cmdline == limit_cmdline)
  193. fixed = true;
  194. } else {
  195. #ifdef CONFIG_CMA_SIZE_SEL_MBYTES
  196. selected_size = size_bytes;
  197. #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
  198. selected_size = cma_early_percent_memory();
  199. #elif defined(CONFIG_CMA_SIZE_SEL_MIN)
  200. selected_size = min(size_bytes, cma_early_percent_memory());
  201. #elif defined(CONFIG_CMA_SIZE_SEL_MAX)
  202. selected_size = max(size_bytes, cma_early_percent_memory());
  203. #endif
  204. }
  205. if (selected_size && !dma_contiguous_default_area) {
  206. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  207. (unsigned long)selected_size / SZ_1M);
  208. dma_contiguous_reserve_area(selected_size, selected_base,
  209. selected_limit,
  210. &dma_contiguous_default_area,
  211. fixed);
  212. }
  213. }
  214. void __weak
  215. dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
  216. {
  217. }
  218. /**
  219. * dma_contiguous_reserve_area() - reserve custom contiguous area
  220. * @size: Size of the reserved area (in bytes),
  221. * @base: Base address of the reserved area optional, use 0 for any
  222. * @limit: End address of the reserved memory (optional, 0 for any).
  223. * @res_cma: Pointer to store the created cma region.
  224. * @fixed: hint about where to place the reserved area
  225. *
  226. * This function reserves memory from early allocator. It should be
  227. * called by arch specific code once the early allocator (memblock or bootmem)
  228. * has been activated and all other subsystems have already allocated/reserved
  229. * memory. This function allows to create custom reserved areas for specific
  230. * devices.
  231. *
  232. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  233. * reserve in range from @base to @limit.
  234. */
  235. int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
  236. phys_addr_t limit, struct cma **res_cma,
  237. bool fixed)
  238. {
  239. int ret;
  240. ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed,
  241. "reserved", res_cma);
  242. if (ret)
  243. return ret;
  244. /* Architecture specific contiguous memory fixup. */
  245. dma_contiguous_early_fixup(cma_get_base(*res_cma),
  246. cma_get_size(*res_cma));
  247. return 0;
  248. }
  249. /**
  250. * dma_alloc_from_contiguous() - allocate pages from contiguous area
  251. * @dev: Pointer to device for which the allocation is performed.
  252. * @count: Requested number of pages.
  253. * @align: Requested alignment of pages (in PAGE_SIZE order).
  254. * @no_warn: Avoid printing message about failed allocation.
  255. *
  256. * This function allocates memory buffer for specified device. It uses
  257. * device specific contiguous memory area if available or the default
  258. * global one. Requires architecture specific dev_get_cma_area() helper
  259. * function.
  260. */
  261. struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
  262. unsigned int align, bool no_warn)
  263. {
  264. if (align > CONFIG_CMA_ALIGNMENT)
  265. align = CONFIG_CMA_ALIGNMENT;
  266. return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
  267. }
  268. /**
  269. * dma_release_from_contiguous() - release allocated pages
  270. * @dev: Pointer to device for which the pages were allocated.
  271. * @pages: Allocated pages.
  272. * @count: Number of allocated pages.
  273. *
  274. * This function releases memory allocated by dma_alloc_from_contiguous().
  275. * It returns false when provided pages do not belong to contiguous area and
  276. * true otherwise.
  277. */
  278. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  279. int count)
  280. {
  281. return cma_release(dev_get_cma_area(dev), pages, count);
  282. }
  283. static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
  284. {
  285. unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);
  286. return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN);
  287. }
  288. /**
  289. * dma_alloc_contiguous() - allocate contiguous pages
  290. * @dev: Pointer to device for which the allocation is performed.
  291. * @size: Requested allocation size.
  292. * @gfp: Allocation flags.
  293. *
  294. * tries to use device specific contiguous memory area if available, or it
  295. * tries to use per-numa cma, if the allocation fails, it will fallback to
  296. * try default global one.
  297. *
  298. * Note that it bypass one-page size of allocations from the per-numa and
  299. * global area as the addresses within one page are always contiguous, so
  300. * there is no need to waste CMA pages for that kind; it also helps reduce
  301. * fragmentations.
  302. */
  303. struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
  304. {
  305. #ifdef CONFIG_DMA_NUMA_CMA
  306. int nid = dev_to_node(dev);
  307. #endif
  308. /* CMA can be used only in the context which permits sleeping */
  309. if (!gfpflags_allow_blocking(gfp))
  310. return NULL;
  311. if (dev->cma_area)
  312. return cma_alloc_aligned(dev->cma_area, size, gfp);
  313. if (size <= PAGE_SIZE)
  314. return NULL;
  315. #ifdef CONFIG_DMA_NUMA_CMA
  316. if (nid != NUMA_NO_NODE && !(gfp & (GFP_DMA | GFP_DMA32))) {
  317. struct cma *cma = dma_contiguous_pernuma_area[nid];
  318. struct page *page;
  319. if (cma) {
  320. page = cma_alloc_aligned(cma, size, gfp);
  321. if (page)
  322. return page;
  323. }
  324. cma = dma_contiguous_numa_area[nid];
  325. if (cma) {
  326. page = cma_alloc_aligned(cma, size, gfp);
  327. if (page)
  328. return page;
  329. }
  330. }
  331. #endif
  332. if (!dma_contiguous_default_area)
  333. return NULL;
  334. return cma_alloc_aligned(dma_contiguous_default_area, size, gfp);
  335. }
  336. /**
  337. * dma_free_contiguous() - release allocated pages
  338. * @dev: Pointer to device for which the pages were allocated.
  339. * @page: Pointer to the allocated pages.
  340. * @size: Size of allocated pages.
  341. *
  342. * This function releases memory allocated by dma_alloc_contiguous(). As the
  343. * cma_release returns false when provided pages do not belong to contiguous
  344. * area and true otherwise, this function then does a fallback __free_pages()
  345. * upon a false-return.
  346. */
  347. void dma_free_contiguous(struct device *dev, struct page *page, size_t size)
  348. {
  349. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  350. /* if dev has its own cma, free page from there */
  351. if (dev->cma_area) {
  352. if (cma_release(dev->cma_area, page, count))
  353. return;
  354. } else {
  355. /*
  356. * otherwise, page is from either per-numa cma or default cma
  357. */
  358. #ifdef CONFIG_DMA_NUMA_CMA
  359. if (cma_release(dma_contiguous_pernuma_area[page_to_nid(page)],
  360. page, count))
  361. return;
  362. if (cma_release(dma_contiguous_numa_area[page_to_nid(page)],
  363. page, count))
  364. return;
  365. #endif
  366. if (cma_release(dma_contiguous_default_area, page, count))
  367. return;
  368. }
  369. /* not in any cma, free from buddy */
  370. __free_pages(page, get_order(size));
  371. }
  372. /*
  373. * Support for reserved memory regions defined in device tree
  374. */
  375. #ifdef CONFIG_OF_RESERVED_MEM
  376. #include <linux/of.h>
  377. #include <linux/of_fdt.h>
  378. #include <linux/of_reserved_mem.h>
  379. #undef pr_fmt
  380. #define pr_fmt(fmt) fmt
  381. static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
  382. {
  383. dev->cma_area = rmem->priv;
  384. return 0;
  385. }
  386. static void rmem_cma_device_release(struct reserved_mem *rmem,
  387. struct device *dev)
  388. {
  389. dev->cma_area = NULL;
  390. }
  391. static const struct reserved_mem_ops rmem_cma_ops = {
  392. .device_init = rmem_cma_device_init,
  393. .device_release = rmem_cma_device_release,
  394. };
  395. static int __init rmem_cma_setup(struct reserved_mem *rmem)
  396. {
  397. unsigned long node = rmem->fdt_node;
  398. bool default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
  399. struct cma *cma;
  400. int err;
  401. if (size_cmdline != -1 && default_cma) {
  402. pr_info("Reserved memory: bypass %s node, using cmdline CMA params instead\n",
  403. rmem->name);
  404. return -EBUSY;
  405. }
  406. if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
  407. of_get_flat_dt_prop(node, "no-map", NULL))
  408. return -EINVAL;
  409. if (!IS_ALIGNED(rmem->base | rmem->size, CMA_MIN_ALIGNMENT_BYTES)) {
  410. pr_err("Reserved memory: incorrect alignment of CMA region\n");
  411. return -EINVAL;
  412. }
  413. err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma);
  414. if (err) {
  415. pr_err("Reserved memory: unable to setup CMA region\n");
  416. return err;
  417. }
  418. /* Architecture specific contiguous memory fixup. */
  419. dma_contiguous_early_fixup(rmem->base, rmem->size);
  420. if (default_cma)
  421. dma_contiguous_default_area = cma;
  422. rmem->ops = &rmem_cma_ops;
  423. rmem->priv = cma;
  424. pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
  425. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  426. return 0;
  427. }
  428. RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
  429. #endif