diff options
Diffstat (limited to 'include/nvgpu/kmem.h')
-rw-r--r-- | include/nvgpu/kmem.h | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/include/nvgpu/kmem.h b/include/nvgpu/kmem.h new file mode 100644 index 0000000..61f90bf --- /dev/null +++ b/include/nvgpu/kmem.h | |||
@@ -0,0 +1,285 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #ifndef NVGPU_KMEM_H | ||
24 | #define NVGPU_KMEM_H | ||
25 | |||
26 | #include <nvgpu/types.h> | ||
27 | #include <nvgpu/utils.h> | ||
28 | |||
29 | struct gk20a; | ||
30 | |||
31 | /* | ||
32 | * When there's other implementations make sure they are included instead of | ||
33 | * Linux when not compiling on Linux! | ||
34 | */ | ||
35 | #ifdef __KERNEL__ | ||
36 | #include <nvgpu/linux/kmem.h> | ||
37 | #elif defined(__NVGPU_POSIX__) | ||
38 | #include <nvgpu/posix/kmem.h> | ||
39 | #else | ||
40 | #include <nvgpu_rmos/include/kmem.h> | ||
41 | #endif | ||
42 | |||
43 | /** | ||
44 | * DOC: Kmem cache support | ||
45 | * | ||
46 | * In Linux there is support for the notion of a kmem_cache. It gives better | ||
47 | * memory usage characteristics for lots of allocations of the same size. Think | ||
48 | * structs that get allocated over and over. Normal kmalloc() type routines | ||
49 | * typically round to the next power-of-2 since that's easy. | ||
50 | * | ||
51 | * But if we know the size ahead of time the packing for the allocations can be | ||
52 | * much better. This is the benefit of a slab allocator. This type hides the | ||
53 | * underlying kmem_cache (or absense thereof). | ||
54 | */ | ||
55 | struct nvgpu_kmem_cache; | ||
56 | |||
57 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
58 | /* | ||
59 | * Uncomment this if you want to enable stack traces in the memory profiling. | ||
60 | * Since this is a fairly high overhead operation and is only necessary for | ||
61 | * debugging actual bugs it's left here for developers to enable. | ||
62 | */ | ||
63 | /* #define __NVGPU_SAVE_KALLOC_STACK_TRACES */ | ||
64 | |||
65 | /* | ||
66 | * Defined per-OS. | ||
67 | */ | ||
68 | struct nvgpu_mem_alloc_tracker; | ||
69 | #endif | ||
70 | |||
71 | |||
72 | /** | ||
73 | * nvgpu_kmem_cache_create - create an nvgpu kernel memory cache. | ||
74 | * | ||
75 | * @g The GPU driver struct using this cache. | ||
76 | * @size Size of the object allocated by the cache. | ||
77 | * | ||
78 | * This cache can be used to allocate objects of size @size. Common usage would | ||
79 | * be for a struct that gets allocated a lot. In that case @size should be | ||
80 | * sizeof(struct my_struct). | ||
81 | * | ||
82 | * A given implementation of this need not do anything special. The allocation | ||
83 | * routines can simply be passed on to nvgpu_kzalloc() if desired so packing | ||
84 | * and alignment of the structs cannot be assumed. | ||
85 | */ | ||
86 | struct nvgpu_kmem_cache *nvgpu_kmem_cache_create(struct gk20a *g, size_t size); | ||
87 | |||
88 | /** | ||
89 | * nvgpu_kmem_cache_destroy - destroy a cache created by | ||
90 | * nvgpu_kmem_cache_create(). | ||
91 | * | ||
92 | * @cache The cache to destroy. | ||
93 | */ | ||
94 | void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache); | ||
95 | |||
96 | /** | ||
97 | * nvgpu_kmem_cache_alloc - Allocate an object from the cache | ||
98 | * | ||
99 | * @cache The cache to alloc from. | ||
100 | */ | ||
101 | void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache); | ||
102 | |||
103 | /** | ||
104 | * nvgpu_kmem_cache_free - Free an object back to a cache | ||
105 | * | ||
106 | * @cache The cache to return the object to. | ||
107 | * @ptr Pointer to the object to free. | ||
108 | */ | ||
109 | void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr); | ||
110 | |||
111 | /** | ||
112 | * nvgpu_kmalloc - Allocate from the kernel's allocator. | ||
113 | * | ||
114 | * @g: Current GPU. | ||
115 | * @size: Size of the allocation. | ||
116 | * | ||
117 | * Allocate a chunk of system memory from the kernel. Allocations larger than 1 | ||
118 | * page may fail even when there may appear to be enough memory. | ||
119 | * | ||
120 | * This function may sleep so cannot be used in IRQs. | ||
121 | */ | ||
122 | #define nvgpu_kmalloc(g, size) __nvgpu_kmalloc(g, size, _NVGPU_GET_IP_) | ||
123 | |||
124 | /** | ||
125 | * nvgpu_kzalloc - Allocate from the kernel's allocator. | ||
126 | * | ||
127 | * @g: Current GPU. | ||
128 | * @size: Size of the allocation. | ||
129 | * | ||
130 | * Identical to nvgpu_kalloc() except the memory will be zeroed before being | ||
131 | * returned. | ||
132 | */ | ||
133 | #define nvgpu_kzalloc(g, size) __nvgpu_kzalloc(g, size, _NVGPU_GET_IP_) | ||
134 | |||
135 | /** | ||
136 | * nvgpu_kcalloc - Allocate from the kernel's allocator. | ||
137 | * | ||
138 | * @g: Current GPU. | ||
139 | * @n: Number of objects. | ||
140 | * @size: Size of each object. | ||
141 | * | ||
142 | * Identical to nvgpu_kalloc() except the size of the memory chunk returned is | ||
143 | * @n * @size. | ||
144 | */ | ||
145 | #define nvgpu_kcalloc(g, n, size) \ | ||
146 | __nvgpu_kcalloc(g, n, size, _NVGPU_GET_IP_) | ||
147 | |||
148 | /** | ||
149 | * nvgpu_vmalloc - Allocate memory and return a map to it. | ||
150 | * | ||
151 | * @g: Current GPU. | ||
152 | * @size: Size of the allocation. | ||
153 | * | ||
154 | * Allocate some memory and return a pointer to a virtual memory mapping of | ||
155 | * that memory in the kernel's virtual address space. The underlying physical | ||
156 | * memory is not guaranteed to be contiguous (and indeed likely isn't). This | ||
157 | * allows for much larger allocations to be done without worrying about as much | ||
158 | * about physical memory fragmentation. | ||
159 | * | ||
160 | * This function may sleep. | ||
161 | */ | ||
162 | #define nvgpu_vmalloc(g, size) __nvgpu_vmalloc(g, size, _NVGPU_GET_IP_) | ||
163 | |||
164 | /** | ||
165 | * nvgpu_vzalloc - Allocate memory and return a map to it. | ||
166 | * | ||
167 | * @g: Current GPU. | ||
168 | * @size: Size of the allocation. | ||
169 | * | ||
170 | * Identical to nvgpu_vmalloc() except this will return zero'ed memory. | ||
171 | */ | ||
172 | #define nvgpu_vzalloc(g, size) __nvgpu_vzalloc(g, size, _NVGPU_GET_IP_) | ||
173 | |||
174 | /** | ||
175 | * nvgpu_kfree - Frees an alloc from nvgpu_kmalloc, nvgpu_kzalloc, | ||
176 | * nvgpu_kcalloc. | ||
177 | * | ||
178 | * @g: Current GPU. | ||
179 | * @addr: Address of object to free. | ||
180 | */ | ||
181 | #define nvgpu_kfree(g, addr) __nvgpu_kfree(g, addr) | ||
182 | |||
183 | /** | ||
184 | * nvgpu_vfree - Frees an alloc from nvgpu_vmalloc, nvgpu_vzalloc. | ||
185 | * | ||
186 | * @g: Current GPU. | ||
187 | * @addr: Address of object to free. | ||
188 | */ | ||
189 | #define nvgpu_vfree(g, addr) __nvgpu_vfree(g, addr) | ||
190 | |||
191 | #define kmem_dbg(g, fmt, args...) \ | ||
192 | nvgpu_log(g, gpu_dbg_kmem, fmt, ##args) | ||
193 | |||
194 | /** | ||
195 | * nvgpu_kmem_init - Initialize the kmem tracking stuff. | ||
196 | * | ||
197 | *@g: The driver to init. | ||
198 | * | ||
199 | * Returns non-zero on failure. | ||
200 | */ | ||
201 | int nvgpu_kmem_init(struct gk20a *g); | ||
202 | |||
203 | /** | ||
204 | * nvgpu_kmem_fini - Finalize the kmem tracking code | ||
205 | * | ||
206 | * @g - The GPU. | ||
207 | * @flags - Flags that control operation of this finalization. | ||
208 | * | ||
209 | * Cleanup resources used by nvgpu_kmem. Available flags for cleanup are: | ||
210 | * | ||
211 | * %NVGPU_KMEM_FINI_DO_NOTHING | ||
212 | * %NVGPU_KMEM_FINI_FORCE_CLEANUP | ||
213 | * %NVGPU_KMEM_FINI_DUMP_ALLOCS | ||
214 | * %NVGPU_KMEM_FINI_WARN | ||
215 | * %NVGPU_KMEM_FINI_BUG | ||
216 | * | ||
217 | * %NVGPU_KMEM_FINI_DO_NOTHING will be overridden by anything else specified. | ||
218 | * Put another way don't just add %NVGPU_KMEM_FINI_DO_NOTHING and expect that | ||
219 | * to suppress other flags from doing anything. | ||
220 | */ | ||
221 | void nvgpu_kmem_fini(struct gk20a *g, int flags); | ||
222 | |||
223 | /* | ||
224 | * These will simply be ignored if CONFIG_NVGPU_TRACK_MEM_USAGE is not defined. | ||
225 | */ | ||
226 | #define NVGPU_KMEM_FINI_DO_NOTHING 0 | ||
227 | #define NVGPU_KMEM_FINI_FORCE_CLEANUP (1 << 0) | ||
228 | #define NVGPU_KMEM_FINI_DUMP_ALLOCS (1 << 1) | ||
229 | #define NVGPU_KMEM_FINI_WARN (1 << 2) | ||
230 | #define NVGPU_KMEM_FINI_BUG (1 << 3) | ||
231 | |||
232 | /* | ||
233 | * Implemented by the OS interface. | ||
234 | */ | ||
235 | void *__nvgpu_big_alloc(struct gk20a *g, size_t size, bool clear); | ||
236 | |||
237 | /** | ||
238 | * nvgpu_big_malloc - Pick virtual or physical alloc based on @size | ||
239 | * | ||
240 | * @g - The GPU. | ||
241 | * @size - Size of the allocation. | ||
242 | * | ||
243 | * On some platforms (i.e Linux) it is possible to allocate memory directly | ||
244 | * mapped into the kernel's address space (kmalloc) or allocate discontiguous | ||
245 | * pages which are then mapped into a special kernel address range. Each type | ||
246 | * of allocation has pros and cons. kmalloc() for instance lets you allocate | ||
247 | * small buffers more space efficiently but vmalloc() allows you to successfully | ||
248 | * allocate much larger buffers without worrying about fragmentation as much | ||
249 | * (but will allocate in multiples of page size). | ||
250 | * | ||
251 | * This function aims to provide the right allocation for when buffers are of | ||
252 | * variable size. In some cases the code doesn't know ahead of time if the | ||
253 | * buffer is going to be big or small so this does the check for you and | ||
254 | * provides the right type of memory allocation. | ||
255 | * | ||
256 | * Returns a pointer to a virtual address range that the kernel can access or | ||
257 | * %NULL on failure. | ||
258 | */ | ||
259 | static inline void *nvgpu_big_malloc(struct gk20a *g, size_t size) | ||
260 | { | ||
261 | return __nvgpu_big_alloc(g, size, false); | ||
262 | } | ||
263 | |||
264 | /** | ||
265 | * nvgpu_big_malloc - Pick virtual or physical alloc based on @size | ||
266 | * | ||
267 | * @g - The GPU. | ||
268 | * @size - Size of the allocation. | ||
269 | * | ||
270 | * Zeroed memory version of nvgpu_big_malloc(). | ||
271 | */ | ||
272 | static inline void *nvgpu_big_zalloc(struct gk20a *g, size_t size) | ||
273 | { | ||
274 | return __nvgpu_big_alloc(g, size, true); | ||
275 | } | ||
276 | |||
277 | /** | ||
278 | * nvgpu_big_free - Free and alloc from nvgpu_big_zalloc() or | ||
279 | * nvgpu_big_malloc(). | ||
280 | * @g - The GPU. | ||
281 | * @p - A pointer allocated by nvgpu_big_zalloc() or nvgpu_big_malloc(). | ||
282 | */ | ||
283 | void nvgpu_big_free(struct gk20a *g, void *p); | ||
284 | |||
285 | #endif /* NVGPU_KMEM_H */ | ||