xref: /freebsd/sys/dev/drm2/ttm/ttm_memory.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include <sys/cdefs.h>
29 #include <dev/drm2/drmP.h>
30 #include <dev/drm2/ttm/ttm_memory.h>
31 #include <dev/drm2/ttm/ttm_module.h>
32 #include <dev/drm2/ttm/ttm_page_alloc.h>
33 
34 #define TTM_MEMORY_ALLOC_RETRIES 4
35 
36 struct ttm_mem_zone {
37 	u_int kobj_ref;
38 	struct ttm_mem_global *glob;
39 	const char *name;
40 	uint64_t zone_mem;
41 	uint64_t emer_mem;
42 	uint64_t max_mem;
43 	uint64_t swap_limit;
44 	uint64_t used_mem;
45 };
46 
47 MALLOC_DEFINE(M_TTM_ZONE, "ttm_zone", "TTM Zone");
48 
49 static void ttm_mem_zone_kobj_release(struct ttm_mem_zone *zone)
50 {
51 
52 	printf("[TTM] Zone %7s: Used memory at exit: %llu kiB\n",
53 		zone->name, (unsigned long long)zone->used_mem >> 10);
54 	free(zone, M_TTM_ZONE);
55 }
56 
57 #if 0
58 /* XXXKIB sysctl */
59 static ssize_t ttm_mem_zone_show(struct ttm_mem_zone *zone;
60 				 struct attribute *attr,
61 				 char *buffer)
62 {
63 	uint64_t val = 0;
64 
65 	mtx_lock(&zone->glob->lock);
66 	if (attr == &ttm_mem_sys)
67 		val = zone->zone_mem;
68 	else if (attr == &ttm_mem_emer)
69 		val = zone->emer_mem;
70 	else if (attr == &ttm_mem_max)
71 		val = zone->max_mem;
72 	else if (attr == &ttm_mem_swap)
73 		val = zone->swap_limit;
74 	else if (attr == &ttm_mem_used)
75 		val = zone->used_mem;
76 	mtx_unlock(&zone->glob->lock);
77 
78 	return snprintf(buffer, PAGE_SIZE, "%llu\n",
79 			(unsigned long long) val >> 10);
80 }
81 #endif
82 
83 static void ttm_check_swapping(struct ttm_mem_global *glob);
84 
85 #if 0
86 /* XXXKIB sysctl */
87 static ssize_t ttm_mem_zone_store(struct ttm_mem_zone *zone,
88 				  struct attribute *attr,
89 				  const char *buffer,
90 				  size_t size)
91 {
92 	int chars;
93 	unsigned long val;
94 	uint64_t val64;
95 
96 	chars = sscanf(buffer, "%lu", &val);
97 	if (chars == 0)
98 		return size;
99 
100 	val64 = val;
101 	val64 <<= 10;
102 
103 	mtx_lock(&zone->glob->lock);
104 	if (val64 > zone->zone_mem)
105 		val64 = zone->zone_mem;
106 	if (attr == &ttm_mem_emer) {
107 		zone->emer_mem = val64;
108 		if (zone->max_mem > val64)
109 			zone->max_mem = val64;
110 	} else if (attr == &ttm_mem_max) {
111 		zone->max_mem = val64;
112 		if (zone->emer_mem < val64)
113 			zone->emer_mem = val64;
114 	} else if (attr == &ttm_mem_swap)
115 		zone->swap_limit = val64;
116 	mtx_unlock(&zone->glob->lock);
117 
118 	ttm_check_swapping(zone->glob);
119 
120 	return size;
121 }
122 #endif
123 
124 static void ttm_mem_global_kobj_release(struct ttm_mem_global *glob)
125 {
126 }
127 
128 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
129 					bool from_wq, uint64_t extra)
130 {
131 	unsigned int i;
132 	struct ttm_mem_zone *zone;
133 	uint64_t target;
134 
135 	for (i = 0; i < glob->num_zones; ++i) {
136 		zone = glob->zones[i];
137 
138 		if (from_wq)
139 			target = zone->swap_limit;
140 		else if (priv_check(curthread, PRIV_VM_MLOCK) == 0)
141 			target = zone->emer_mem;
142 		else
143 			target = zone->max_mem;
144 
145 		target = (extra > target) ? 0ULL : target;
146 
147 		if (zone->used_mem > target)
148 			return true;
149 	}
150 	return false;
151 }
152 
153 /**
154  * At this point we only support a single shrink callback.
155  * Extend this if needed, perhaps using a linked list of callbacks.
156  * Note that this function is reentrant:
157  * many threads may try to swap out at any given time.
158  */
159 
160 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
161 		       uint64_t extra)
162 {
163 	int ret;
164 	struct ttm_mem_shrink *shrink;
165 
166 	mtx_lock(&glob->lock);
167 	if (glob->shrink == NULL)
168 		goto out;
169 
170 	while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
171 		shrink = glob->shrink;
172 		mtx_unlock(&glob->lock);
173 		ret = shrink->do_shrink(shrink);
174 		mtx_lock(&glob->lock);
175 		if (unlikely(ret != 0))
176 			goto out;
177 	}
178 out:
179 	mtx_unlock(&glob->lock);
180 }
181 
182 
183 
184 static void ttm_shrink_work(void *arg, int pending __unused)
185 {
186 	struct ttm_mem_global *glob = arg;
187 
188 	ttm_shrink(glob, true, 0ULL);
189 }
190 
191 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
192     uint64_t mem)
193 {
194 	struct ttm_mem_zone *zone;
195 
196 	zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
197 
198 	zone->name = "kernel";
199 	zone->zone_mem = mem;
200 	zone->max_mem = mem >> 1;
201 	zone->emer_mem = (mem >> 1) + (mem >> 2);
202 	zone->swap_limit = zone->max_mem - (mem >> 3);
203 	zone->used_mem = 0;
204 	zone->glob = glob;
205 	glob->zone_kernel = zone;
206 	refcount_init(&zone->kobj_ref, 1);
207 	glob->zones[glob->num_zones++] = zone;
208 	return 0;
209 }
210 
211 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
212     uint64_t mem)
213 {
214 	struct ttm_mem_zone *zone;
215 
216 	zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
217 
218 	/**
219 	 * No special dma32 zone needed.
220 	 */
221 
222 	if (mem <= ((uint64_t) 1ULL << 32)) {
223 		free(zone, M_TTM_ZONE);
224 		return 0;
225 	}
226 
227 	/*
228 	 * Limit max dma32 memory to 4GB for now
229 	 * until we can figure out how big this
230 	 * zone really is.
231 	 */
232 
233 	mem = ((uint64_t) 1ULL << 32);
234 	zone->name = "dma32";
235 	zone->zone_mem = mem;
236 	zone->max_mem = mem >> 1;
237 	zone->emer_mem = (mem >> 1) + (mem >> 2);
238 	zone->swap_limit = zone->max_mem - (mem >> 3);
239 	zone->used_mem = 0;
240 	zone->glob = glob;
241 	glob->zone_dma32 = zone;
242 	refcount_init(&zone->kobj_ref, 1);
243 	glob->zones[glob->num_zones++] = zone;
244 	return 0;
245 }
246 
247 int ttm_mem_global_init(struct ttm_mem_global *glob)
248 {
249 	u_int64_t mem;
250 	int ret;
251 	int i;
252 	struct ttm_mem_zone *zone;
253 
254 	mtx_init(&glob->lock, "ttmgz", NULL, MTX_DEF);
255 	glob->swap_queue = taskqueue_create("ttm_swap", M_WAITOK,
256 	    taskqueue_thread_enqueue, &glob->swap_queue);
257 	taskqueue_start_threads(&glob->swap_queue, 1, PVM, "ttm swap");
258 	TASK_INIT(&glob->work, 0, ttm_shrink_work, glob);
259 
260 	refcount_init(&glob->kobj_ref, 1);
261 
262 	mem = physmem * PAGE_SIZE;
263 
264 	ret = ttm_mem_init_kernel_zone(glob, mem);
265 	if (unlikely(ret != 0))
266 		goto out_no_zone;
267 	ret = ttm_mem_init_dma32_zone(glob, mem);
268 	if (unlikely(ret != 0))
269 		goto out_no_zone;
270 	for (i = 0; i < glob->num_zones; ++i) {
271 		zone = glob->zones[i];
272 		printf("[TTM] Zone %7s: Available graphics memory: %llu kiB\n",
273 			zone->name, (unsigned long long)zone->max_mem >> 10);
274 	}
275 	ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
276 	ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
277 	return 0;
278 out_no_zone:
279 	ttm_mem_global_release(glob);
280 	return ret;
281 }
282 
283 void ttm_mem_global_release(struct ttm_mem_global *glob)
284 {
285 	unsigned int i;
286 	struct ttm_mem_zone *zone;
287 
288 	/* let the page allocator first stop the shrink work. */
289 	ttm_page_alloc_fini();
290 	ttm_dma_page_alloc_fini();
291 
292 	taskqueue_drain(glob->swap_queue, &glob->work);
293 	taskqueue_free(glob->swap_queue);
294 	glob->swap_queue = NULL;
295 	for (i = 0; i < glob->num_zones; ++i) {
296 		zone = glob->zones[i];
297 		if (refcount_release(&zone->kobj_ref))
298 			ttm_mem_zone_kobj_release(zone);
299 	}
300 	if (refcount_release(&glob->kobj_ref))
301 		ttm_mem_global_kobj_release(glob);
302 }
303 
304 static void ttm_check_swapping(struct ttm_mem_global *glob)
305 {
306 	bool needs_swapping = false;
307 	unsigned int i;
308 	struct ttm_mem_zone *zone;
309 
310 	mtx_lock(&glob->lock);
311 	for (i = 0; i < glob->num_zones; ++i) {
312 		zone = glob->zones[i];
313 		if (zone->used_mem > zone->swap_limit) {
314 			needs_swapping = true;
315 			break;
316 		}
317 	}
318 
319 	mtx_unlock(&glob->lock);
320 
321 	if (unlikely(needs_swapping))
322 		taskqueue_enqueue(glob->swap_queue, &glob->work);
323 
324 }
325 
326 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
327 				     struct ttm_mem_zone *single_zone,
328 				     uint64_t amount)
329 {
330 	unsigned int i;
331 	struct ttm_mem_zone *zone;
332 
333 	mtx_lock(&glob->lock);
334 	for (i = 0; i < glob->num_zones; ++i) {
335 		zone = glob->zones[i];
336 		if (single_zone && zone != single_zone)
337 			continue;
338 		zone->used_mem -= amount;
339 	}
340 	mtx_unlock(&glob->lock);
341 }
342 
343 void ttm_mem_global_free(struct ttm_mem_global *glob,
344 			 uint64_t amount)
345 {
346 	return ttm_mem_global_free_zone(glob, NULL, amount);
347 }
348 
349 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
350 				  struct ttm_mem_zone *single_zone,
351 				  uint64_t amount, bool reserve)
352 {
353 	uint64_t limit;
354 	int ret = -ENOMEM;
355 	unsigned int i;
356 	struct ttm_mem_zone *zone;
357 
358 	mtx_lock(&glob->lock);
359 	for (i = 0; i < glob->num_zones; ++i) {
360 		zone = glob->zones[i];
361 		if (single_zone && zone != single_zone)
362 			continue;
363 
364 		limit = (priv_check(curthread, PRIV_VM_MLOCK) == 0) ?
365 			zone->emer_mem : zone->max_mem;
366 
367 		if (zone->used_mem > limit)
368 			goto out_unlock;
369 	}
370 
371 	if (reserve) {
372 		for (i = 0; i < glob->num_zones; ++i) {
373 			zone = glob->zones[i];
374 			if (single_zone && zone != single_zone)
375 				continue;
376 			zone->used_mem += amount;
377 		}
378 	}
379 
380 	ret = 0;
381 out_unlock:
382 	mtx_unlock(&glob->lock);
383 	ttm_check_swapping(glob);
384 
385 	return ret;
386 }
387 
388 
389 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
390 				     struct ttm_mem_zone *single_zone,
391 				     uint64_t memory,
392 				     bool no_wait, bool interruptible)
393 {
394 	int count = TTM_MEMORY_ALLOC_RETRIES;
395 
396 	while (unlikely(ttm_mem_global_reserve(glob,
397 					       single_zone,
398 					       memory, true)
399 			!= 0)) {
400 		if (no_wait)
401 			return -ENOMEM;
402 		if (unlikely(count-- == 0))
403 			return -ENOMEM;
404 		ttm_shrink(glob, false, memory + (memory >> 2) + 16);
405 	}
406 
407 	return 0;
408 }
409 
410 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
411 			 bool no_wait, bool interruptible)
412 {
413 	/**
414 	 * Normal allocations of kernel memory are registered in
415 	 * all zones.
416 	 */
417 
418 	return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
419 					 interruptible);
420 }
421 
422 #define page_to_pfn(pp) OFF_TO_IDX(VM_PAGE_TO_PHYS(pp))
423 
424 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
425 			      struct vm_page *page,
426 			      bool no_wait, bool interruptible)
427 {
428 
429 	struct ttm_mem_zone *zone = NULL;
430 
431 	/**
432 	 * Page allocations may be registed in a single zone
433 	 * only if highmem or !dma32.
434 	 */
435 
436 	if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
437 		zone = glob->zone_kernel;
438 	return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
439 					 interruptible);
440 }
441 
442 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct vm_page *page)
443 {
444 	struct ttm_mem_zone *zone = NULL;
445 
446 	if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
447 		zone = glob->zone_kernel;
448 	ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
449 }
450 
451 
452 size_t ttm_round_pot(size_t size)
453 {
454 	if ((size & (size - 1)) == 0)
455 		return size;
456 	else if (size > PAGE_SIZE)
457 		return PAGE_ALIGN(size);
458 	else {
459 		size_t tmp_size = 4;
460 
461 		while (tmp_size < size)
462 			tmp_size <<= 1;
463 
464 		return tmp_size;
465 	}
466 	return 0;
467 }
468