dma-contiguous.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. * The Linux Foundation chooses to take subject only to the GPLv2 license
  14. * terms, and distributes only under these terms.
  15. */
  16. #define pr_fmt(fmt) "cma: " fmt
  17. #ifdef CONFIG_CMA_DEBUG
  18. #ifndef DEBUG
  19. # define DEBUG
  20. #endif
  21. #endif
  22. #include <asm/page.h>
  23. #include <asm/dma-contiguous.h>
  24. #include <linux/memblock.h>
  25. #include <linux/err.h>
  26. #include <linux/of.h>
  27. #include <linux/of_fdt.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/page-isolation.h>
  32. #include <linux/slab.h>
  33. #include <linux/swap.h>
  34. #include <linux/mm_types.h>
  35. #include <linux/dma-contiguous.h>
  36. #include <trace/events/kmem.h>
  37. #ifndef SZ_1M
  38. #define SZ_1M (1 << 20)
  39. #endif
  40. struct cma {
  41. unsigned long base_pfn;
  42. unsigned long count;
  43. unsigned long *bitmap;
  44. bool in_system;
  45. struct mutex lock;
  46. };
  47. static DEFINE_MUTEX(cma_mutex);
  48. struct cma *dma_contiguous_def_area;
  49. phys_addr_t dma_contiguous_def_base;
  50. static struct cma_area {
  51. phys_addr_t base;
  52. unsigned long size;
  53. struct cma *cma;
  54. const char *name;
  55. bool to_system;
  56. } cma_areas[MAX_CMA_AREAS];
  57. static unsigned cma_area_count;
  58. static struct cma_map {
  59. phys_addr_t base;
  60. struct device *dev;
  61. } cma_maps[MAX_CMA_AREAS] __initdata;
  62. static unsigned cma_map_count __initdata;
  63. static struct cma *cma_get_area(phys_addr_t base)
  64. {
  65. int i;
  66. for (i = 0; i < cma_area_count; i++)
  67. if (cma_areas[i].base == base)
  68. return cma_areas[i].cma;
  69. return NULL;
  70. }
  71. static struct cma *cma_get_area_by_name(const char *name)
  72. {
  73. int i;
  74. if (!name)
  75. return NULL;
  76. for (i = 0; i < cma_area_count; i++)
  77. if (cma_areas[i].name && strcmp(cma_areas[i].name, name) == 0)
  78. return cma_areas[i].cma;
  79. return NULL;
  80. }
  81. #ifdef CONFIG_CMA_SIZE_MBYTES
  82. #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
  83. #else
  84. #define CMA_SIZE_MBYTES 0
  85. #endif
  86. /*
  87. * Default global CMA area size can be defined in kernel's .config.
  88. * This is usefull mainly for distro maintainers to create a kernel
  89. * that works correctly for most supported systems.
  90. * The size can be set in bytes or as a percentage of the total memory
  91. * in the system.
  92. *
  93. * Users, who want to set the size of global CMA area for their system
  94. * should use cma= kernel parameter.
  95. */
  96. static const phys_addr_t size_bytes = CMA_SIZE_MBYTES * SZ_1M;
  97. static phys_addr_t size_cmdline = -1;
  98. static int __init early_cma(char *p)
  99. {
  100. pr_debug("%s(%s)\n", __func__, p);
  101. size_cmdline = memparse(p, &p);
  102. return 0;
  103. }
  104. early_param("cma", early_cma);
  105. #ifdef CONFIG_CMA_SIZE_PERCENTAGE
  106. static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)
  107. {
  108. struct memblock_region *reg;
  109. unsigned long total_pages = 0;
  110. /*
  111. * We cannot use memblock_phys_mem_size() here, because
  112. * memblock_analyze() has not been called yet.
  113. */
  114. for_each_memblock(memory, reg)
  115. total_pages += memblock_region_memory_end_pfn(reg) -
  116. memblock_region_memory_base_pfn(reg);
  117. return (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) << PAGE_SHIFT;
  118. }
  119. #else
  120. static inline __maybe_unused phys_addr_t cma_early_percent_memory(void)
  121. {
  122. return 0;
  123. }
  124. #endif
  125. static __init int cma_activate_area(unsigned long base_pfn, unsigned long count)
  126. {
  127. unsigned long pfn = base_pfn;
  128. unsigned i = count >> pageblock_order;
  129. struct zone *zone;
  130. WARN_ON_ONCE(!pfn_valid(pfn));
  131. zone = page_zone(pfn_to_page(pfn));
  132. do {
  133. unsigned j;
  134. base_pfn = pfn;
  135. for (j = pageblock_nr_pages; j; --j, pfn++) {
  136. WARN_ON_ONCE(!pfn_valid(pfn));
  137. if (page_zone(pfn_to_page(pfn)) != zone)
  138. return -EINVAL;
  139. }
  140. init_cma_reserved_pageblock(pfn_to_page(base_pfn));
  141. } while (--i);
  142. return 0;
  143. }
  144. static __init struct cma *cma_create_area(unsigned long base_pfn,
  145. unsigned long count, bool system)
  146. {
  147. int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  148. struct cma *cma;
  149. int ret = -ENOMEM;
  150. pr_debug("%s(base %08lx, count %lx)\n", __func__, base_pfn, count);
  151. cma = kmalloc(sizeof *cma, GFP_KERNEL);
  152. if (!cma)
  153. return ERR_PTR(-ENOMEM);
  154. cma->base_pfn = base_pfn;
  155. cma->count = count;
  156. cma->in_system = system;
  157. cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  158. if (!cma->bitmap)
  159. goto no_mem;
  160. if (cma->in_system) {
  161. ret = cma_activate_area(base_pfn, count);
  162. if (ret)
  163. goto error;
  164. }
  165. mutex_init(&cma->lock);
  166. pr_debug("%s: returned %p\n", __func__, (void *)cma);
  167. return cma;
  168. error:
  169. kfree(cma->bitmap);
  170. no_mem:
  171. kfree(cma);
  172. return ERR_PTR(ret);
  173. }
  174. /*****************************************************************************/
  175. #ifdef CONFIG_OF
  176. int __init cma_fdt_scan(unsigned long node, const char *uname,
  177. int depth, void *data)
  178. {
  179. phys_addr_t base, size;
  180. unsigned long len;
  181. __be32 *prop;
  182. char *name;
  183. bool in_system;
  184. phys_addr_t limit = MEMBLOCK_ALLOC_ANYWHERE;
  185. if (!of_get_flat_dt_prop(node, "linux,contiguous-region", NULL))
  186. return 0;
  187. prop = of_get_flat_dt_prop(node, "reg", &len);
  188. if (!prop || (len != 2 * sizeof(unsigned long)))
  189. return 0;
  190. base = be32_to_cpu(prop[0]);
  191. size = be32_to_cpu(prop[1]);
  192. name = of_get_flat_dt_prop(node, "label", NULL);
  193. in_system =
  194. of_get_flat_dt_prop(node, "linux,reserve-region", NULL) ? 0 : 1;
  195. prop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  196. if (prop)
  197. limit = be32_to_cpu(prop[0]);
  198. pr_info("Found %s, memory base %lx, size %ld MiB, limit %pa\n", uname,
  199. (unsigned long)base, (unsigned long)size / SZ_1M, &limit);
  200. dma_contiguous_reserve_area(size, &base, limit, name,
  201. in_system);
  202. return 0;
  203. }
  204. #endif
  205. /**
  206. * dma_contiguous_reserve() - reserve area for contiguous memory handling
  207. * @limit: End address of the reserved memory (optional, 0 for any).
  208. *
  209. * This function reserves memory from early allocator. It should be
  210. * called by arch specific code once the early allocator (memblock or bootmem)
  211. * has been activated and all other subsystems have already allocated/reserved
  212. * memory. It reserves contiguous areas for global, device independent
  213. * allocations and (optionally) all areas defined in device tree structures.
  214. */
  215. void __init dma_contiguous_reserve(phys_addr_t limit)
  216. {
  217. phys_addr_t sel_size = 0;
  218. pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit);
  219. if (size_cmdline != -1) {
  220. sel_size = size_cmdline;
  221. } else {
  222. #ifdef CONFIG_CMA_SIZE_SEL_MBYTES
  223. sel_size = size_bytes;
  224. #elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)
  225. sel_size = cma_early_percent_memory();
  226. #elif defined(CONFIG_CMA_SIZE_SEL_MIN)
  227. sel_size = min(size_bytes, cma_early_percent_memory());
  228. #elif defined(CONFIG_CMA_SIZE_SEL_MAX)
  229. sel_size = max(size_bytes, cma_early_percent_memory());
  230. #endif
  231. }
  232. if (sel_size) {
  233. phys_addr_t base = 0;
  234. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  235. (unsigned long)sel_size / SZ_1M);
  236. if (dma_contiguous_reserve_area(sel_size, &base, limit, NULL,
  237. true) == 0)
  238. dma_contiguous_def_base = base;
  239. }
  240. #ifdef CONFIG_OF
  241. of_scan_flat_dt(cma_fdt_scan, NULL);
  242. #endif
  243. };
  244. /**
  245. * dma_contiguous_reserve_area() - reserve custom contiguous area
  246. * @size: Size of the reserved area (in bytes),
  247. * @base: Pointer to the base address of the reserved area, also used to return
  248. * base address of the actually reserved area, optional, use pointer to
  249. * 0 for any
  250. * @limit: End address of the reserved memory (optional, 0 for any).
  251. *
  252. * This function reserves memory from early allocator. It should be
  253. * called by arch specific code once the early allocator (memblock or bootmem)
  254. * has been activated and all other subsystems have already allocated/reserved
  255. * memory. This function allows to create custom reserved areas for specific
  256. * devices.
  257. */
  258. int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t *res_base,
  259. phys_addr_t limit, const char *name,
  260. bool to_system)
  261. {
  262. phys_addr_t base = *res_base;
  263. phys_addr_t alignment;
  264. int ret = 0;
  265. pr_debug("%s(size %lx, base %08lx, limit %08lx)\n", __func__,
  266. (unsigned long)size, (unsigned long)base,
  267. (unsigned long)limit);
  268. /* Sanity checks */
  269. if (cma_area_count == ARRAY_SIZE(cma_areas)) {
  270. pr_err("Not enough slots for CMA reserved regions!\n");
  271. return -ENOSPC;
  272. }
  273. if (!size)
  274. return -EINVAL;
  275. /* Sanitise input arguments */
  276. alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
  277. base = ALIGN(base, alignment);
  278. size = ALIGN(size, alignment);
  279. limit &= ~(alignment - 1);
  280. /* Reserve memory */
  281. if (base) {
  282. if (memblock_is_region_reserved(base, size) ||
  283. memblock_reserve(base, size) < 0) {
  284. ret = -EBUSY;
  285. goto err;
  286. }
  287. } else {
  288. /*
  289. * Use __memblock_alloc_base() since
  290. * memblock_alloc_base() panic()s.
  291. */
  292. phys_addr_t addr = __memblock_alloc_base(size, alignment, limit);
  293. if (!addr) {
  294. ret = -ENOMEM;
  295. goto err;
  296. } else {
  297. base = addr;
  298. }
  299. }
  300. /*
  301. * Each reserved area must be initialised later, when more kernel
  302. * subsystems (like slab allocator) are available.
  303. */
  304. cma_areas[cma_area_count].base = base;
  305. cma_areas[cma_area_count].size = size;
  306. cma_areas[cma_area_count].name = name;
  307. cma_areas[cma_area_count].to_system = to_system;
  308. cma_area_count++;
  309. *res_base = base;
  310. pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
  311. (unsigned long)base);
  312. /* Architecture specific contiguous memory fixup. */
  313. dma_contiguous_early_fixup(base, size);
  314. return 0;
  315. err:
  316. pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M);
  317. return ret;
  318. }
  319. /**
  320. * dma_contiguous_add_device() - add device to custom contiguous reserved area
  321. * @dev: Pointer to device structure.
  322. * @base: Pointer to the base address of the reserved area returned by
  323. * dma_contiguous_reserve_area() function, also used to return
  324. *
  325. * This function assigns the given device to the contiguous memory area
  326. * reserved earlier by dma_contiguous_reserve_area() function.
  327. */
  328. int __init dma_contiguous_add_device(struct device *dev, phys_addr_t base)
  329. {
  330. if (cma_map_count == ARRAY_SIZE(cma_maps)) {
  331. pr_err("Not enough slots for CMA reserved regions!\n");
  332. return -ENOSPC;
  333. }
  334. cma_maps[cma_map_count].dev = dev;
  335. cma_maps[cma_map_count].base = base;
  336. cma_map_count++;
  337. return 0;
  338. }
  339. #ifdef CONFIG_OF
  340. static void cma_assign_device_from_dt(struct device *dev)
  341. {
  342. struct device_node *node;
  343. struct cma *cma;
  344. const char *name;
  345. u32 value;
  346. node = of_parse_phandle(dev->of_node, "linux,contiguous-region", 0);
  347. if (!node)
  348. return;
  349. if (of_property_read_u32(node, "reg", &value) && !value)
  350. return;
  351. if (of_property_read_string(node, "label", &name))
  352. return;
  353. cma = cma_get_area_by_name(name);
  354. if (!cma)
  355. return;
  356. dev_set_cma_area(dev, cma);
  357. pr_info("Assigned CMA region at %lx to %s device\n", (unsigned long)value, dev_name(dev));
  358. }
  359. static int cma_device_init_notifier_call(struct notifier_block *nb,
  360. unsigned long event, void *data)
  361. {
  362. struct device *dev = data;
  363. if (event == BUS_NOTIFY_ADD_DEVICE && dev->of_node)
  364. cma_assign_device_from_dt(dev);
  365. return NOTIFY_DONE;
  366. }
  367. static struct notifier_block cma_dev_init_nb = {
  368. .notifier_call = cma_device_init_notifier_call,
  369. };
  370. #endif
  371. static int __init cma_init_reserved_areas(void)
  372. {
  373. struct cma *cma;
  374. int i;
  375. for (i = 0; i < cma_area_count; i++) {
  376. phys_addr_t base = PFN_DOWN(cma_areas[i].base);
  377. unsigned int count = cma_areas[i].size >> PAGE_SHIFT;
  378. bool system = cma_areas[i].to_system;
  379. cma = cma_create_area(base, count, system);
  380. if (!IS_ERR(cma))
  381. cma_areas[i].cma = cma;
  382. }
  383. dma_contiguous_def_area = cma_get_area(dma_contiguous_def_base);
  384. for (i = 0; i < cma_map_count; i++) {
  385. cma = cma_get_area(cma_maps[i].base);
  386. dev_set_cma_area(cma_maps[i].dev, cma);
  387. }
  388. #ifdef CONFIG_OF
  389. bus_register_notifier(&platform_bus_type, &cma_dev_init_nb);
  390. #endif
  391. return 0;
  392. }
  393. core_initcall(cma_init_reserved_areas);
  394. phys_addr_t cma_get_base(struct device *dev)
  395. {
  396. struct cma *cma = dev_get_cma_area(dev);
  397. return cma->base_pfn << PAGE_SHIFT;
  398. }
  399. static void clear_cma_bitmap(struct cma *cma, unsigned long pfn, int count)
  400. {
  401. mutex_lock(&cma->lock);
  402. bitmap_clear(cma->bitmap, pfn - cma->base_pfn, count);
  403. mutex_unlock(&cma->lock);
  404. }
  405. /**
  406. * dma_alloc_from_contiguous() - allocate pages from contiguous area
  407. * @dev: Pointer to device for which the allocation is performed.
  408. * @count: Requested number of pages.
  409. * @align: Requested alignment of pages (in PAGE_SIZE order).
  410. *
  411. * This function allocates memory buffer for specified device. It uses
  412. * device specific contiguous memory area if available or the default
  413. * global one. Requires architecture specific get_dev_cma_area() helper
  414. * function.
  415. */
  416. struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
  417. unsigned int align)
  418. {
  419. unsigned long mask, pfn, pageno, start = 0;
  420. struct cma *cma = dev_get_cma_area(dev);
  421. struct page *page = NULL;
  422. int ret = 0;
  423. int tries = 0;
  424. if (!cma || !cma->count)
  425. return NULL;
  426. if (align > CONFIG_CMA_ALIGNMENT)
  427. align = CONFIG_CMA_ALIGNMENT;
  428. pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
  429. count, align);
  430. if (!count)
  431. return NULL;
  432. mask = (1 << align) - 1;
  433. for (;;) {
  434. mutex_lock(&cma->lock);
  435. pageno = bitmap_find_next_zero_area(cma->bitmap, cma->count,
  436. start, count, mask);
  437. if (pageno >= cma->count) {
  438. mutex_unlock(&cma->lock);
  439. break;
  440. }
  441. bitmap_set(cma->bitmap, pageno, count);
  442. /*
  443. * It's safe to drop the lock here. We've marked this region for
  444. * our exclusive use. If the migration fails we will take the
  445. * lock again and unmark it.
  446. */
  447. mutex_unlock(&cma->lock);
  448. pfn = cma->base_pfn + pageno;
  449. mutex_lock(&cma_mutex);
  450. if (cma->in_system)
  451. ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA);
  452. mutex_unlock(&cma_mutex);
  453. if (ret == 0) {
  454. page = pfn_to_page(pfn);
  455. break;
  456. } else if (ret != -EBUSY) {
  457. clear_cma_bitmap(cma, pfn, count);
  458. pfn = 0;
  459. break;
  460. }
  461. clear_cma_bitmap(cma, pfn, count);
  462. tries++;
  463. trace_dma_alloc_contiguous_retry(tries);
  464. pr_debug("%s(): memory range at %p is busy, retrying\n",
  465. __func__, pfn_to_page(pfn));
  466. /* try again with a bit different memory target */
  467. start = pageno + mask + 1;
  468. }
  469. pr_debug("%s(): returned %p\n", __func__, page);
  470. return page;
  471. }
  472. /**
  473. * dma_release_from_contiguous() - release allocated pages
  474. * @dev: Pointer to device for which the pages were allocated.
  475. * @pages: Allocated pages.
  476. * @count: Number of allocated pages.
  477. *
  478. * This function releases memory allocated by dma_alloc_from_contiguous().
  479. * It returns false when provided pages do not belong to contiguous area and
  480. * true otherwise.
  481. */
  482. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  483. int count)
  484. {
  485. struct cma *cma = dev_get_cma_area(dev);
  486. unsigned long pfn;
  487. if (!cma || !pages)
  488. return false;
  489. pr_debug("%s(page %p)\n", __func__, (void *)pages);
  490. pfn = page_to_pfn(pages);
  491. if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
  492. return false;
  493. VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);
  494. if (cma->in_system)
  495. free_contig_range(pfn, count);
  496. clear_cma_bitmap(cma, pfn, count);
  497. return true;
  498. }