dma-contiguous.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/mm.h>
  24. #include <linux/mutex.h>
  25. #include <linux/page-isolation.h>
  26. #include <linux/sizes.h>
  27. #include <linux/slab.h>
  28. #include <linux/swap.h>
  29. #include <linux/mm_types.h>
  30. #include <linux/dma-contiguous.h>
  31. struct cma {
  32. unsigned long base_pfn;
  33. unsigned long count;
  34. unsigned long *bitmap;
  35. };
  36. struct cma *dma_contiguous_default_area;
  37. #ifdef CONFIG_CMA_SIZE_MBYTES
  38. #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
  39. #else
  40. #define CMA_SIZE_MBYTES 0
  41. #endif
  42. /*
  43. * Default global CMA area size can be defined in kernel's .config.
  44. * This is usefull mainly for distro maintainers to create a kernel
  45. * that works correctly for most supported systems.
  46. * The size can be set in bytes or as a percentage of the total memory
  47. * in the system.
  48. *
  49. * Users, who want to set the size of global CMA area for their system
  50. * should use cma= kernel parameter.
  51. */
  52. static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M;
  53. static phys_addr_t size_cmdline = -1;
  54. static int __init early_cma(char *p)
  55. {
  56. pr_debug("%s(%s)\n", __func__, p);
  57. size_cmdline = memparse(p, &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. pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
  94. if (size_cmdline != -1) {
  95. selected_size = size_cmdline;
  96. } else {
  97. #ifdef CONFIG_CMA_SIZE_SEL_MBYTES
  98. selected_size = size_bytes;
  99. #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
  100. selected_size = cma_early_percent_memory();
  101. #elif defined(CONFIG_CMA_SIZE_SEL_MIN)
  102. selected_size = min(size_bytes, cma_early_percent_memory());
  103. #elif defined(CONFIG_CMA_SIZE_SEL_MAX)
  104. selected_size = max(size_bytes, cma_early_percent_memory());
  105. #endif
  106. }
  107. if (selected_size && !dma_contiguous_default_area) {
  108. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  109. (unsigned long)selected_size / SZ_1M);
  110. dma_contiguous_reserve_area(selected_size, 0, limit,
  111. &dma_contiguous_default_area);
  112. }
  113. };
  114. static DEFINE_MUTEX(cma_mutex);
  115. static int __init cma_activate_area(struct cma *cma)
  116. {
  117. int bitmap_size = BITS_TO_LONGS(cma->count) * sizeof(long);
  118. unsigned long base_pfn = cma->base_pfn, pfn = base_pfn;
  119. unsigned i = cma->count >> pageblock_order;
  120. struct zone *zone;
  121. cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  122. if (!cma->bitmap)
  123. return -ENOMEM;
  124. WARN_ON_ONCE(!pfn_valid(pfn));
  125. zone = page_zone(pfn_to_page(pfn));
  126. do {
  127. unsigned j;
  128. base_pfn = pfn;
  129. for (j = pageblock_nr_pages; j; --j, pfn++) {
  130. WARN_ON_ONCE(!pfn_valid(pfn));
  131. if (page_zone(pfn_to_page(pfn)) != zone)
  132. return -EINVAL;
  133. }
  134. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  135. } while (--i);
  136. return 0;
  137. }
  138. static struct cma cma_areas[MAX_CMA_AREAS];
  139. static unsigned cma_area_count;
  140. static int __init cma_init_reserved_areas(void)
  141. {
  142. int i;
  143. for (i = 0; i < cma_area_count; i++) {
  144. int ret = cma_activate_area(&cma_areas[i]);
  145. if (ret)
  146. return ret;
  147. }
  148. return 0;
  149. }
  150. core_initcall(cma_init_reserved_areas);
  151. /**
  152. * dma_contiguous_reserve_area() - reserve custom contiguous area
  153. * @size: Size of the reserved area (in bytes),
  154. * @base: Base address of the reserved area optional, use 0 for any
  155. * @limit: End address of the reserved memory (optional, 0 for any).
  156. * @res_cma: Pointer to store the created cma region.
  157. *
  158. * This function reserves memory from early allocator. It should be
  159. * called by arch specific code once the early allocator (memblock or bootmem)
  160. * has been activated and all other subsystems have already allocated/reserved
  161. * memory. This function allows to create custom reserved areas for specific
  162. * devices.
  163. */
  164. int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
  165. phys_addr_t limit, struct cma **res_cma)
  166. {
  167. struct cma *cma = &cma_areas[cma_area_count];
  168. phys_addr_t alignment;
  169. int ret = 0;
  170. pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__,
  171. (unsigned long)size, (unsigned long)base,
  172. (unsigned long)limit);
  173. /* Sanity checks */
  174. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  175. pr_err("Not enough slots for CMA reserved regions!\n");
  176. return -ENOSPC;
  177. }
  178. if (!size)
  179. return -EINVAL;
  180. /* Sanitise input arguments */
  181. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  182. base = ALIGN(base, alignment);
  183. size = ALIGN(size, alignment);
  184. limit &= ~(alignment - 1);
  185. /* Reserve memory */
  186. if (base) {
  187. if (memblock_is_region_reserved(base, size) ||
  188. memblock_reserve(base, size) < 0) {
  189. ret = -EBUSY;
  190. goto err;
  191. }
  192. } else {
  193. /*
  194. * Use __memblock_alloc_base() since
  195. * memblock_alloc_base() panic()s.
  196. */
  197. phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);
  198. if (!addr) {
  199. ret = -ENOMEM;
  200. goto err;
  201. } else {
  202. base = addr;
  203. }
  204. }
  205. /*
  206. * Each reserved area must be initialised later, when more kernel
  207. * subsystems (like slab allocator) are available.
  208. */
  209. cma->base_pfn = PFN_DOWN(base);
  210. cma->count = size >> PAGE_SHIFT;
  211. *res_cma = cma;
  212. cma_area_count++;
  213. pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
  214. (unsigned long)base);
  215. /* Architecture specific contiguous memory fixup. */
  216. dma_contiguous_early_fixup(base, size);
  217. return 0;
  218. err:
  219. pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  220. return ret;
  221. }
  222. /**
  223. * dma_alloc_from_contiguous() - allocate pages from contiguous area
  224. * @dev: Pointer to device for which the allocation is performed.
  225. * @count: Requested number of pages.
  226. * @align: Requested alignment of pages (in PAGE_SIZE order).
  227. *
  228. * This function allocates memory buffer for specified device. It uses
  229. * device specific contiguous memory area if available or the default
  230. * global one. Requires architecture specific get_dev_cma_area() helper
  231. * function.
  232. */
  233. struct page *dma_alloc_from_contiguous(struct device *dev, int count,
  234. unsigned int align)
  235. {
  236. unsigned long mask, pfn, pageno, start = 0;
  237. struct cma *cma = dev_get_cma_area(dev);
  238. struct page *page = NULL;
  239. int ret;
  240. if (!cma || !cma->count)
  241. return NULL;
  242. if (align > CONFIG_CMA_ALIGNMENT)
  243. align = CONFIG_CMA_ALIGNMENT;
  244. pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
  245. count, align);
  246. if (!count)
  247. return NULL;
  248. mask = (1 << align) - 1;
  249. mutex_lock(&cma_mutex);
  250. for (;;) {
  251. pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,
  252. start, count, mask);
  253. if (pageno >= cma->count)
  254. break;
  255. pfn = cma->base_pfn + pageno;
  256. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  257. if (ret == 0) {
  258. bitmap_set(cma->bitmap, pageno, count);
  259. page = pfn_to_page(pfn);
  260. break;
  261. } else if (ret != -EBUSY) {
  262. break;
  263. }
  264. pr_debug("%s(): memory range at %p is busy, retrying\n",
  265. __func__, pfn_to_page(pfn));
  266. /* try again with a bit different memory target */
  267. start = pageno + mask + 1;
  268. }
  269. mutex_unlock(&cma_mutex);
  270. pr_debug("%s(): returned %p\n", __func__, page);
  271. return page;
  272. }
  273. /**
  274. * dma_release_from_contiguous() - release allocated pages
  275. * @dev: Pointer to device for which the pages were allocated.
  276. * @pages: Allocated pages.
  277. * @count: Number of allocated pages.
  278. *
  279. * This function releases memory allocated by dma_alloc_from_contiguous().
  280. * It returns false when provided pages do not belong to contiguous area and
  281. * true otherwise.
  282. */
  283. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  284. int count)
  285. {
  286. struct cma *cma = dev_get_cma_area(dev);
  287. unsigned long pfn;
  288. if (!cma || !pages)
  289. return false;
  290. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  291. pfn = page_to_pfn(pages);
  292. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  293. return false;
  294. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  295. mutex_lock(&cma_mutex);
  296. bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count);
  297. free_contig_range(pfn, count);
  298. mutex_unlock(&cma_mutex);
  299. return true;
  300. }