dma-contiguous.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License or (at your optional) any later version of the license.
  13. */
  14. #define pr_fmt(fmt) "cma: " fmt
  15. #ifdef CONFIG_CMA_DEBUG
  16. #ifndef DEBUG
  17. # define DEBUG
  18. #endif
  19. #endif
  20. #include <asm/page.h>
  21. #include <asm/dma-contiguous.h>
  22. #include <linux/memblock.h>
  23. #include <linux/err.h>
  24. #include <linux/sizes.h>
  25. #include <linux/dma-contiguous.h>
  26. #include <linux/cma.h>
  27. #ifdef CONFIG_CMA_SIZE_MBYTES
  28. #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
  29. #else
  30. #define CMA_SIZE_MBYTES 0
  31. #endif
  32. struct cma *dma_contiguous_default_area;
  33. /*
  34. * Default global CMA area size can be defined in kernel's .config.
  35. * This is useful mainly for distro maintainers to create a kernel
  36. * that works correctly for most supported systems.
  37. * The size can be set in bytes or as a percentage of the total memory
  38. * in the system.
  39. *
  40. * Users, who want to set the size of global CMA area for their system
  41. * should use cma= kernel parameter.
  42. */
  43. static const phys_addr_t size_bytes = (phys_addr_t)CMA_SIZE_MBYTES * SZ_1M;
  44. static phys_addr_t size_cmdline = -1;
  45. static phys_addr_t base_cmdline;
  46. static phys_addr_t limit_cmdline;
  47. static int __init early_cma(char *p)
  48. {
  49. pr_debug("%s(%s)\n", __func__, p);
  50. size_cmdline = memparse(p, &p);
  51. if (*p != '@')
  52. return 0;
  53. base_cmdline = memparse(p + 1, &p);
  54. if (*p != '-') {
  55. limit_cmdline = base_cmdline + size_cmdline;
  56. return 0;
  57. }
  58. limit_cmdline = memparse(p + 1, &p);
  59. return 0;
  60. }
  61. early_param("cma", early_cma);
  62. #ifdef CONFIG_CMA_SIZE_PERCENTAGE
  63. static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
  64. {
  65. struct memblock_region *reg;
  66. unsigned long total_pages = 0;
  67. /*
  68. * We cannot use memblock_phys_mem_size() here, because
  69. * memblock_analyze() has not been called yet.
  70. */
  71. for_each_memblock(memory, reg)
  72. total_pages += memblock_region_memory_end_pfn(reg) -
  73. memblock_region_memory_base_pfn(reg);
  74. return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
  75. }
  76. #else
  77. static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
  78. {
  79. return 0;
  80. }
  81. #endif
  82. /**
  83. * dma_contiguous_reserve() - reserve area(s) for contiguous memory handling
  84. * @limit: End address of the reserved memory (optional, 0 for any).
  85. *
  86. * This function reserves memory from early allocator. It should be
  87. * called by arch specific code once the early allocator (memblock or bootmem)
  88. * has been activated and all other subsystems have already allocated/reserved
  89. * memory.
  90. */
  91. void __init dma_contiguous_reserve(phys_addr_t limit)
  92. {
  93. phys_addr_t selected_size = 0;
  94. phys_addr_t selected_base = 0;
  95. phys_addr_t selected_limit = limit;
  96. bool fixed = false;
  97. pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
  98. if (size_cmdline != -1) {
  99. selected_size = size_cmdline;
  100. selected_base = base_cmdline;
  101. selected_limit = min_not_zero(limit_cmdline, limit);
  102. if (base_cmdline + size_cmdline == limit_cmdline)
  103. fixed = true;
  104. } else {
  105. #ifdef CONFIG_CMA_SIZE_SEL_MBYTES
  106. selected_size = size_bytes;
  107. #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
  108. selected_size = cma_early_percent_memory();
  109. #elif defined(CONFIG_CMA_SIZE_SEL_MIN)
  110. selected_size = min(size_bytes, cma_early_percent_memory());
  111. #elif defined(CONFIG_CMA_SIZE_SEL_MAX)
  112. selected_size = max(size_bytes, cma_early_percent_memory());
  113. #endif
  114. }
  115. if (selected_size && !dma_contiguous_default_area) {
  116. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  117. (unsigned long)selected_size / SZ_1M);
  118. dma_contiguous_reserve_area(selected_size, selected_base,
  119. selected_limit,
  120. &dma_contiguous_default_area,
  121. fixed);
  122. }
  123. }
  124. /**
  125. * dma_contiguous_reserve_area() - reserve custom contiguous area
  126. * @size: Size of the reserved area (in bytes),
  127. * @base: Base address of the reserved area optional, use 0 for any
  128. * @limit: End address of the reserved memory (optional, 0 for any).
  129. * @res_cma: Pointer to store the created cma region.
  130. * @fixed: hint about where to place the reserved area
  131. *
  132. * This function reserves memory from early allocator. It should be
  133. * called by arch specific code once the early allocator (memblock or bootmem)
  134. * has been activated and all other subsystems have already allocated/reserved
  135. * memory. This function allows to create custom reserved areas for specific
  136. * devices.
  137. *
  138. * If @fixed is true, reserve contiguous area at exactly @base. If false,
  139. * reserve in range from @base to @limit.
  140. */
  141. int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
  142. phys_addr_t limit, struct cma **res_cma,
  143. bool fixed)
  144. {
  145. int ret;
  146. ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed,
  147. "reserved", res_cma);
  148. if (ret)
  149. return ret;
  150. /* Architecture specific contiguous memory fixup. */
  151. dma_contiguous_early_fixup(cma_get_base(*res_cma),
  152. cma_get_size(*res_cma));
  153. return 0;
  154. }
  155. /**
  156. * dma_alloc_from_contiguous() - allocate pages from contiguous area
  157. * @dev: Pointer to device for which the allocation is performed.
  158. * @count: Requested number of pages.
  159. * @align: Requested alignment of pages (in PAGE_SIZE order).
  160. * @gfp_mask: GFP flags to use for this allocation.
  161. *
  162. * This function allocates memory buffer for specified device. It uses
  163. * device specific contiguous memory area if available or the default
  164. * global one. Requires architecture specific dev_get_cma_area() helper
  165. * function.
  166. */
  167. struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
  168. unsigned int align, gfp_t gfp_mask)
  169. {
  170. if (align > CONFIG_CMA_ALIGNMENT)
  171. align = CONFIG_CMA_ALIGNMENT;
  172. return cma_alloc(dev_get_cma_area(dev), count, align, gfp_mask);
  173. }
  174. /**
  175. * dma_release_from_contiguous() - release allocated pages
  176. * @dev: Pointer to device for which the pages were allocated.
  177. * @pages: Allocated pages.
  178. * @count: Number of allocated pages.
  179. *
  180. * This function releases memory allocated by dma_alloc_from_contiguous().
  181. * It returns false when provided pages do not belong to contiguous area and
  182. * true otherwise.
  183. */
  184. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  185. int count)
  186. {
  187. return cma_release(dev_get_cma_area(dev), pages, count);
  188. }
  189. /*
  190. * Support for reserved memory regions defined in device tree
  191. */
  192. #ifdef CONFIG_OF_RESERVED_MEM
  193. #include <linux/of.h>
  194. #include <linux/of_fdt.h>
  195. #include <linux/of_reserved_mem.h>
  196. #undef pr_fmt
  197. #define pr_fmt(fmt) fmt
  198. static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
  199. {
  200. dev_set_cma_area(dev, rmem->priv);
  201. return 0;
  202. }
  203. static void rmem_cma_device_release(struct reserved_mem *rmem,
  204. struct device *dev)
  205. {
  206. dev_set_cma_area(dev, NULL);
  207. }
  208. static const struct reserved_mem_ops rmem_cma_ops = {
  209. .device_init = rmem_cma_device_init,
  210. .device_release = rmem_cma_device_release,
  211. };
  212. static int __init rmem_cma_setup(struct reserved_mem *rmem)
  213. {
  214. phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  215. phys_addr_t mask = align - 1;
  216. unsigned long node = rmem->fdt_node;
  217. struct cma *cma;
  218. int err;
  219. if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
  220. of_get_flat_dt_prop(node, "no-map", NULL))
  221. return -EINVAL;
  222. if ((rmem->base & mask) || (rmem->size & mask)) {
  223. pr_err("Reserved memory: incorrect alignment of CMA region\n");
  224. return -EINVAL;
  225. }
  226. err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma);
  227. if (err) {
  228. pr_err("Reserved memory: unable to setup CMA region\n");
  229. return err;
  230. }
  231. /* Architecture specific contiguous memory fixup. */
  232. dma_contiguous_early_fixup(rmem->base, rmem->size);
  233. if (of_get_flat_dt_prop(node, "linux,cma-default", NULL))
  234. dma_contiguous_set_default(cma);
  235. rmem->ops = &rmem_cma_ops;
  236. rmem->priv = cma;
  237. pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
  238. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  239. return 0;
  240. }
  241. RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
  242. #endif