Commit | Line | Data |
---|---|---|
c64be2bb MS |
1 | #ifndef __LINUX_CMA_H |
2 | #define __LINUX_CMA_H | |
3 | ||
4 | /* | |
5 | * Contiguous Memory Allocator for DMA mapping framework | |
6 | * Copyright (c) 2010-2011 by Samsung Electronics. | |
7 | * Written by: | |
8 | * Marek Szyprowski <m.szyprowski@samsung.com> | |
9 | * Michal Nazarewicz <mina86@mina86.com> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 of the | |
14 | * License or (at your optional) any later version of the license. | |
15 | */ | |
16 | ||
17 | /* | |
18 | * Contiguous Memory Allocator | |
19 | * | |
20 | * The Contiguous Memory Allocator (CMA) makes it possible to | |
21 | * allocate big contiguous chunks of memory after the system has | |
22 | * booted. | |
23 | * | |
24 | * Why is it needed? | |
25 | * | |
26 | * Various devices on embedded systems have no scatter-getter and/or | |
27 | * IO map support and require contiguous blocks of memory to | |
28 | * operate. They include devices such as cameras, hardware video | |
29 | * coders, etc. | |
30 | * | |
31 | * Such devices often require big memory buffers (a full HD frame | |
32 | * is, for instance, more then 2 mega pixels large, i.e. more than 6 | |
33 | * MB of memory), which makes mechanisms such as kmalloc() or | |
34 | * alloc_page() ineffective. | |
35 | * | |
36 | * At the same time, a solution where a big memory region is | |
37 | * reserved for a device is suboptimal since often more memory is | |
38 | * reserved then strictly required and, moreover, the memory is | |
39 | * inaccessible to page system even if device drivers don't use it. | |
40 | * | |
41 | * CMA tries to solve this issue by operating on memory regions | |
42 | * where only movable pages can be allocated from. This way, kernel | |
43 | * can use the memory for pagecache and when device driver requests | |
44 | * it, allocated pages can be migrated. | |
45 | * | |
46 | * Driver usage | |
47 | * | |
48 | * CMA should not be used by the device drivers directly. It is | |
49 | * only a helper framework for dma-mapping subsystem. | |
50 | * | |
51 | * For more information, see kernel-docs in drivers/base/dma-contiguous.c | |
52 | */ | |
53 | ||
54 | #ifdef __KERNEL__ | |
55 | ||
56 | struct cma; | |
57 | struct page; | |
58 | struct device; | |
59 | ||
f825c736 | 60 | #ifdef CONFIG_DMA_CMA |
c64be2bb MS |
61 | |
62 | /* | |
63 | * There is always at least global CMA area and a few optional device | |
64 | * private areas configured in kernel .config. | |
65 | */ | |
66 | #define MAX_CMA_AREAS (1 + CONFIG_CMA_AREAS) | |
67 | ||
68 | extern struct cma *dma_contiguous_default_area; | |
69 | ||
a2547380 MS |
70 | static inline struct cma *dev_get_cma_area(struct device *dev) |
71 | { | |
72 | if (dev && dev->cma_area) | |
73 | return dev->cma_area; | |
74 | return dma_contiguous_default_area; | |
75 | } | |
76 | ||
77 | static inline void dev_set_cma_area(struct device *dev, struct cma *cma) | |
78 | { | |
79 | if (dev) | |
80 | dev->cma_area = cma; | |
81 | } | |
82 | ||
83 | static inline void dma_contiguous_set_default(struct cma *cma) | |
84 | { | |
85 | dma_contiguous_default_area = cma; | |
86 | } | |
87 | ||
c64be2bb | 88 | void dma_contiguous_reserve(phys_addr_t addr_limit); |
a2547380 MS |
89 | |
90 | int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, | |
5ea3b1b2 AM |
91 | phys_addr_t limit, struct cma **res_cma, |
92 | bool fixed); | |
a2547380 MS |
93 | |
94 | /** | |
95 | * dma_declare_contiguous() - reserve area for contiguous memory handling | |
96 | * for particular device | |
97 | * @dev: Pointer to device structure. | |
98 | * @size: Size of the reserved memory. | |
99 | * @base: Start address of the reserved memory (optional, 0 for any). | |
100 | * @limit: End address of the reserved memory (optional, 0 for any). | |
101 | * | |
102 | * This function reserves memory for specified device. It should be | |
103 | * called by board specific code when early allocator (memblock or bootmem) | |
104 | * is still activate. | |
105 | */ | |
106 | ||
107 | static inline int dma_declare_contiguous(struct device *dev, phys_addr_t size, | |
108 | phys_addr_t base, phys_addr_t limit) | |
109 | { | |
110 | struct cma *cma; | |
111 | int ret; | |
5ea3b1b2 | 112 | ret = dma_contiguous_reserve_area(size, base, limit, &cma, true); |
a2547380 MS |
113 | if (ret == 0) |
114 | dev_set_cma_area(dev, cma); | |
115 | ||
116 | return ret; | |
117 | } | |
c64be2bb MS |
118 | |
119 | struct page *dma_alloc_from_contiguous(struct device *dev, int count, | |
120 | unsigned int order); | |
121 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, | |
122 | int count); | |
123 | ||
124 | #else | |
125 | ||
126 | #define MAX_CMA_AREAS (0) | |
127 | ||
a2547380 MS |
128 | static inline struct cma *dev_get_cma_area(struct device *dev) |
129 | { | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | static inline void dev_set_cma_area(struct device *dev, struct cma *cma) { } | |
134 | ||
135 | static inline void dma_contiguous_set_default(struct cma *cma) { } | |
136 | ||
c64be2bb MS |
137 | static inline void dma_contiguous_reserve(phys_addr_t limit) { } |
138 | ||
a2547380 | 139 | static inline int dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, |
5ea3b1b2 AM |
140 | phys_addr_t limit, struct cma **res_cma, |
141 | bool fixed) | |
142 | { | |
a2547380 MS |
143 | return -ENOSYS; |
144 | } | |
145 | ||
c64be2bb | 146 | static inline |
4009793e | 147 | int dma_declare_contiguous(struct device *dev, phys_addr_t size, |
c64be2bb MS |
148 | phys_addr_t base, phys_addr_t limit) |
149 | { | |
150 | return -ENOSYS; | |
151 | } | |
152 | ||
153 | static inline | |
154 | struct page *dma_alloc_from_contiguous(struct device *dev, int count, | |
155 | unsigned int order) | |
156 | { | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | static inline | |
161 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, | |
162 | int count) | |
163 | { | |
164 | return false; | |
165 | } | |
166 | ||
167 | #endif | |
168 | ||
169 | #endif | |
170 | ||
171 | #endif |