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