dma-contiguous.c 10.0 KB

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