dma-contiguous.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 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) {
  108. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  109. (unsigned long)selected_size / SZ_1M);
  110. dma_declare_contiguous(NULL, selected_size, 0, limit);
  111. }
  112. };
  113. static DEFINE_MUTEX(cma_mutex);
  114. static __init int cma_activate_area(unsigned long base_pfn, unsigned long count)
  115. {
  116. unsigned long pfn = base_pfn;
  117. unsigned i = count >> pageblock_order;
  118. struct zone *zone;
  119. WARN_ON_ONCE(!pfn_valid(pfn));
  120. zone = page_zone(pfn_to_page(pfn));
  121. do {
  122. unsigned j;
  123. base_pfn = pfn;
  124. for (j = pageblock_nr_pages; j; --j, pfn++) {
  125. WARN_ON_ONCE(!pfn_valid(pfn));
  126. if (page_zone(pfn_to_page(pfn)) != zone)
  127. return -EINVAL;
  128. }
  129. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  130. } while (--i);
  131. return 0;
  132. }
  133. static __init struct cma *cma_create_area(unsigned long base_pfn,
  134. unsigned long count)
  135. {
  136. int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  137. struct cma *cma;
  138. int ret = -ENOMEM;
  139. pr_debug("%s(base %08lx, count %lx)\n", __func__, base_pfn, count);
  140. cma = kmalloc(sizeof *cma, GFP_KERNEL);
  141. if (!cma)
  142. return ERR_PTR(-ENOMEM);
  143. cma->base_pfn = base_pfn;
  144. cma->count = count;
  145. cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  146. if (!cma->bitmap)
  147. goto no_mem;
  148. ret = cma_activate_area(base_pfn, count);
  149. if (ret)
  150. goto error;
  151. pr_debug("%s: returned %p\n", __func__, (void *)cma);
  152. return cma;
  153. error:
  154. kfree(cma->bitmap);
  155. no_mem:
  156. kfree(cma);
  157. return ERR_PTR(ret);
  158. }
  159. static struct cma_reserved {
  160. phys_addr_t start;
  161. unsigned long size;
  162. struct device *dev;
  163. } cma_reserved[MAX_CMA_AREAS] __initdata;
  164. static unsigned cma_reserved_count __initdata;
  165. static int __init cma_init_reserved_areas(void)
  166. {
  167. struct cma_reserved *r = cma_reserved;
  168. unsigned i = cma_reserved_count;
  169. pr_debug("%s()\n", __func__);
  170. for (; i; --i, ++r) {
  171. struct cma *cma;
  172. cma = cma_create_area(PFN_DOWN(r->start),
  173. r->size >> PAGE_SHIFT);
  174. if (!IS_ERR(cma))
  175. dev_set_cma_area(r->dev, cma);
  176. }
  177. return 0;
  178. }
  179. core_initcall(cma_init_reserved_areas);
  180. /**
  181. * dma_declare_contiguous() - reserve area for contiguous memory handling
  182. * for particular device
  183. * @dev: Pointer to device structure.
  184. * @size: Size of the reserved memory.
  185. * @base: Start address of the reserved memory (optional, 0 for any).
  186. * @limit: End address of the reserved memory (optional, 0 for any).
  187. *
  188. * This function reserves memory for specified device. It should be
  189. * called by board specific code when early allocator (memblock or bootmem)
  190. * is still activate.
  191. */
  192. int __init dma_declare_contiguous(struct device *dev, phys_addr_t size,
  193. phys_addr_t base, phys_addr_t limit)
  194. {
  195. struct cma_reserved *r = &cma_reserved[cma_reserved_count];
  196. phys_addr_t alignment;
  197. pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__,
  198. (unsigned long)size, (unsigned long)base,
  199. (unsigned long)limit);
  200. /* Sanity checks */
  201. if (cma_reserved_count == ARRAY_SIZE(cma_reserved)) {
  202. pr_err("Not enough slots for CMA reserved regions!\n");
  203. return -ENOSPC;
  204. }
  205. if (!size)
  206. return -EINVAL;
  207. /* Sanitise input arguments */
  208. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  209. base = ALIGN(base, alignment);
  210. size = ALIGN(size, alignment);
  211. limit &= ~(alignment - 1);
  212. /* Reserve memory */
  213. if (base) {
  214. if (memblock_is_region_reserved(base, size) ||
  215. memblock_reserve(base, size) < 0) {
  216. base = -EBUSY;
  217. goto err;
  218. }
  219. } else {
  220. /*
  221. * Use __memblock_alloc_base() since
  222. * memblock_alloc_base() panic()s.
  223. */
  224. phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);
  225. if (!addr) {
  226. base = -ENOMEM;
  227. goto err;
  228. } else {
  229. base = addr;
  230. }
  231. }
  232. /*
  233. * Each reserved area must be initialised later, when more kernel
  234. * subsystems (like slab allocator) are available.
  235. */
  236. r->start = base;
  237. r->size = size;
  238. r->dev = dev;
  239. cma_reserved_count++;
  240. pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
  241. (unsigned long)base);
  242. /* Architecture specific contiguous memory fixup. */
  243. dma_contiguous_early_fixup(base, size);
  244. return 0;
  245. err:
  246. pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  247. return base;
  248. }
  249. /**
  250. * dma_alloc_from_contiguous() - allocate pages from contiguous area
  251. * @dev: Pointer to device for which the allocation is performed.
  252. * @count: Requested number of pages.
  253. * @align: Requested alignment of pages (in PAGE_SIZE order).
  254. *
  255. * This function allocates memory buffer for specified device. It uses
  256. * device specific contiguous memory area if available or the default
  257. * global one. Requires architecture specific get_dev_cma_area() helper
  258. * function.
  259. */
  260. struct page *dma_alloc_from_contiguous(struct device *dev, int count,
  261. unsigned int align)
  262. {
  263. unsigned long mask, pfn, pageno, start = 0;
  264. struct cma *cma = dev_get_cma_area(dev);
  265. struct page *page = NULL;
  266. int ret;
  267. if (!cma || !cma->count)
  268. return NULL;
  269. if (align > CONFIG_CMA_ALIGNMENT)
  270. align = CONFIG_CMA_ALIGNMENT;
  271. pr_debug("%s(cma %p, count %d, align %d)\n", __func__, (void *)cma,
  272. count, align);
  273. if (!count)
  274. return NULL;
  275. mask = (1 << align) - 1;
  276. mutex_lock(&cma_mutex);
  277. for (;;) {
  278. pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,
  279. start, count, mask);
  280. if (pageno >= cma->count)
  281. break;
  282. pfn = cma->base_pfn + pageno;
  283. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  284. if (ret == 0) {
  285. bitmap_set(cma->bitmap, pageno, count);
  286. page = pfn_to_page(pfn);
  287. break;
  288. } else if (ret != -EBUSY) {
  289. break;
  290. }
  291. pr_debug("%s(): memory range at %p is busy, retrying\n",
  292. __func__, pfn_to_page(pfn));
  293. /* try again with a bit different memory target */
  294. start = pageno + mask + 1;
  295. }
  296. mutex_unlock(&cma_mutex);
  297. pr_debug("%s(): returned %p\n", __func__, page);
  298. return page;
  299. }
  300. /**
  301. * dma_release_from_contiguous() - release allocated pages
  302. * @dev: Pointer to device for which the pages were allocated.
  303. * @pages: Allocated pages.
  304. * @count: Number of allocated pages.
  305. *
  306. * This function releases memory allocated by dma_alloc_from_contiguous().
  307. * It returns false when provided pages do not belong to contiguous area and
  308. * true otherwise.
  309. */
  310. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  311. int count)
  312. {
  313. struct cma *cma = dev_get_cma_area(dev);
  314. unsigned long pfn;
  315. if (!cma || !pages)
  316. return false;
  317. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  318. pfn = page_to_pfn(pages);
  319. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  320. return false;
  321. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  322. mutex_lock(&cma_mutex);
  323. bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count);
  324. free_contig_range(pfn, count);
  325. mutex_unlock(&cma_mutex);
  326. return true;
  327. }