blob: 6665fa03c0d1a851a9b838035d0780dfd018790f [file] [log] [blame]
Thomas Gleixner8607a962019-05-22 09:51:44 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Marek Szyprowskic64be2b2011-12-29 13:09:51 +01002#ifndef __LINUX_CMA_H
3#define __LINUX_CMA_H
4
5/*
6 * Contiguous Memory Allocator for DMA mapping framework
7 * Copyright (c) 2010-2011 by Samsung Electronics.
8 * Written by:
9 * Marek Szyprowski <m.szyprowski@samsung.com>
10 * Michal Nazarewicz <mina86@mina86.com>
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010011 */
12
13/*
14 * Contiguous Memory Allocator
15 *
16 * The Contiguous Memory Allocator (CMA) makes it possible to
17 * allocate big contiguous chunks of memory after the system has
18 * booted.
19 *
20 * Why is it needed?
21 *
22 * Various devices on embedded systems have no scatter-getter and/or
23 * IO map support and require contiguous blocks of memory to
24 * operate. They include devices such as cameras, hardware video
25 * coders, etc.
26 *
27 * Such devices often require big memory buffers (a full HD frame
28 * is, for instance, more then 2 mega pixels large, i.e. more than 6
29 * MB of memory), which makes mechanisms such as kmalloc() or
30 * alloc_page() ineffective.
31 *
32 * At the same time, a solution where a big memory region is
33 * reserved for a device is suboptimal since often more memory is
34 * reserved then strictly required and, moreover, the memory is
35 * inaccessible to page system even if device drivers don't use it.
36 *
37 * CMA tries to solve this issue by operating on memory regions
38 * where only movable pages can be allocated from. This way, kernel
39 * can use the memory for pagecache and when device driver requests
40 * it, allocated pages can be migrated.
41 *
42 * Driver usage
43 *
44 * CMA should not be used by the device drivers directly. It is
45 * only a helper framework for dma-mapping subsystem.
46 *
Christoph Hellwigcf65a0f2018-06-12 19:01:45 +020047 * For more information, see kernel-docs in kernel/dma/contiguous.c
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010048 */
49
50#ifdef __KERNEL__
51
Joonsoo Kima2541292014-08-06 16:05:25 -070052#include <linux/device.h>
53
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010054struct cma;
55struct page;
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010056
Aneesh Kumar K.Vf825c732013-07-02 11:15:15 +053057#ifdef CONFIG_DMA_CMA
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010058
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010059extern struct cma *dma_contiguous_default_area;
60
Marek Szyprowskia2547382013-07-29 14:31:45 +020061static inline struct cma *dev_get_cma_area(struct device *dev)
62{
63 if (dev && dev->cma_area)
64 return dev->cma_area;
65 return dma_contiguous_default_area;
66}
67
68static inline void dev_set_cma_area(struct device *dev, struct cma *cma)
69{
70 if (dev)
71 dev->cma_area = cma;
72}
73
74static inline void dma_contiguous_set_default(struct cma *cma)
75{
76 dma_contiguous_default_area = cma;
77}
78
Marek Szyprowskic64be2b2011-12-29 13:09:51 +010079void dma_contiguous_reserve(phys_addr_t addr_limit);
Marek Szyprowskia2547382013-07-29 14:31:45 +020080
81int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
Akinobu Mita5ea3b1b2014-06-04 16:06:54 -070082 phys_addr_t limit, struct cma **res_cma,
83 bool fixed);
Marek Szyprowskia2547382013-07-29 14:31:45 +020084
85/**
86 * dma_declare_contiguous() - reserve area for contiguous memory handling
87 * for particular device
88 * @dev: Pointer to device structure.
89 * @size: Size of the reserved memory.
90 * @base: Start address of the reserved memory (optional, 0 for any).
91 * @limit: End address of the reserved memory (optional, 0 for any).
92 *
93 * This function reserves memory for specified device. It should be
94 * called by board specific code when early allocator (memblock or bootmem)
95 * is still activate.
96 */
97
98static inline int dma_declare_contiguous(struct device *dev, phys_addr_t size,
99 phys_addr_t base, phys_addr_t limit)
100{
101 struct cma *cma;
102 int ret;
Akinobu Mita5ea3b1b2014-06-04 16:06:54 -0700103 ret = dma_contiguous_reserve_area(size, base, limit, &cma, true);
Marek Szyprowskia2547382013-07-29 14:31:45 +0200104 if (ret == 0)
105 dev_set_cma_area(dev, cma);
106
107 return ret;
108}
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100109
Rohit Vaswani67a2e2132015-10-22 13:32:11 -0700110struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
Marek Szyprowskid834c5a2018-08-17 15:49:00 -0700111 unsigned int order, bool no_warn);
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100112bool dma_release_from_contiguous(struct device *dev, struct page *pages,
113 int count);
114
115#else
116
Marek Szyprowskia2547382013-07-29 14:31:45 +0200117static inline struct cma *dev_get_cma_area(struct device *dev)
118{
119 return NULL;
120}
121
122static inline void dev_set_cma_area(struct device *dev, struct cma *cma) { }
123
124static inline void dma_contiguous_set_default(struct cma *cma) { }
125
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100126static inline void dma_contiguous_reserve(phys_addr_t limit) { }
127
Marek Szyprowskia2547382013-07-29 14:31:45 +0200128static inline int dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
Akinobu Mita5ea3b1b2014-06-04 16:06:54 -0700129 phys_addr_t limit, struct cma **res_cma,
130 bool fixed)
131{
Marek Szyprowskia2547382013-07-29 14:31:45 +0200132 return -ENOSYS;
133}
134
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100135static inline
Vitaly Andrianov40097932012-12-05 09:29:25 -0500136int dma_declare_contiguous(struct device *dev, phys_addr_t size,
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100137 phys_addr_t base, phys_addr_t limit)
138{
139 return -ENOSYS;
140}
141
142static inline
Rohit Vaswani67a2e2132015-10-22 13:32:11 -0700143struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
Marek Szyprowskid834c5a2018-08-17 15:49:00 -0700144 unsigned int order, bool no_warn)
Marek Szyprowskic64be2b2011-12-29 13:09:51 +0100145{
146 return NULL;
147}
148
149static inline
150bool dma_release_from_contiguous(struct device *dev, struct page *pages,
151 int count)
152{
153 return false;
154}
155
156#endif
157
158#endif
159
160#endif