dma-contiguous.c 8.1 KB

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