dma-contiguous.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, 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. *
  159. * This function allocates memory buffer for specified device. It uses
  160. * device specific contiguous memory area if available or the default
  161. * global one. Requires architecture specific dev_get_cma_area() helper
  162. * function.
  163. */
  164. struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
  165. unsigned int align)
  166. {
  167. if (align > CONFIG_CMA_ALIGNMENT)
  168. align = CONFIG_CMA_ALIGNMENT;
  169. return cma_alloc(dev_get_cma_area(dev), count, align);
  170. }
  171. /**
  172. * dma_release_from_contiguous() - release allocated pages
  173. * @dev: Pointer to device for which the pages were allocated.
  174. * @pages: Allocated pages.
  175. * @count: Number of allocated pages.
  176. *
  177. * This function releases memory allocated by dma_alloc_from_contiguous().
  178. * It returns false when provided pages do not belong to contiguous area and
  179. * true otherwise.
  180. */
  181. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  182. int count)
  183. {
  184. return cma_release(dev_get_cma_area(dev), pages, count);
  185. }
  186. /*
  187. * Support for reserved memory regions defined in device tree
  188. */
  189. #ifdef CONFIG_OF_RESERVED_MEM
  190. #include <linux/of.h>
  191. #include <linux/of_fdt.h>
  192. #include <linux/of_reserved_mem.h>
  193. #undef pr_fmt
  194. #define pr_fmt(fmt) fmt
  195. static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev)
  196. {
  197. dev_set_cma_area(dev, rmem->priv);
  198. return 0;
  199. }
  200. static void rmem_cma_device_release(struct reserved_mem *rmem,
  201. struct device *dev)
  202. {
  203. dev_set_cma_area(dev, NULL);
  204. }
  205. static const struct reserved_mem_ops rmem_cma_ops = {
  206. .device_init = rmem_cma_device_init,
  207. .device_release = rmem_cma_device_release,
  208. };
  209. static int __init rmem_cma_setup(struct reserved_mem *rmem)
  210. {
  211. phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  212. phys_addr_t mask = align - 1;
  213. unsigned long node = rmem->fdt_node;
  214. struct cma *cma;
  215. int err;
  216. if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
  217. of_get_flat_dt_prop(node, "no-map", NULL))
  218. return -EINVAL;
  219. if ((rmem->base & mask) || (rmem->size & mask)) {
  220. pr_err("Reserved memory: incorrect alignment of CMA region\n");
  221. return -EINVAL;
  222. }
  223. err = cma_init_reserved_mem(rmem->base, rmem->size, 0, &cma);
  224. if (err) {
  225. pr_err("Reserved memory: unable to setup CMA region\n");
  226. return err;
  227. }
  228. /* Architecture specific contiguous memory fixup. */
  229. dma_contiguous_early_fixup(rmem->base, rmem->size);
  230. if (of_get_flat_dt_prop(node, "linux,cma-default", NULL))
  231. dma_contiguous_set_default(cma);
  232. rmem->ops = &rmem_cma_ops;
  233. rmem->priv = cma;
  234. pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n",
  235. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  236. return 0;
  237. }
  238. RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup);
  239. #endif