contiguous.c 7.9 KB

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