contiguous.c 14 KB

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