contiguous.c 13 KB

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