1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <dev/drm2/drmP.h>
32 #include <dev/drm2/ttm/ttm_memory.h>
33 #include <dev/drm2/ttm/ttm_module.h>
34 #include <dev/drm2/ttm/ttm_page_alloc.h>
36 #define TTM_MEMORY_ALLOC_RETRIES 4
40 struct ttm_mem_global *glob;
49 MALLOC_DEFINE(M_TTM_ZONE, "ttm_zone", "TTM Zone");
51 static void ttm_mem_zone_kobj_release(struct ttm_mem_zone *zone)
54 printf("[TTM] Zone %7s: Used memory at exit: %llu kiB\n",
55 zone->name, (unsigned long long)zone->used_mem >> 10);
56 free(zone, M_TTM_ZONE);
61 static ssize_t ttm_mem_zone_show(struct ttm_mem_zone *zone;
62 struct attribute *attr,
67 mtx_lock(&zone->glob->lock);
68 if (attr == &ttm_mem_sys)
70 else if (attr == &ttm_mem_emer)
72 else if (attr == &ttm_mem_max)
74 else if (attr == &ttm_mem_swap)
75 val = zone->swap_limit;
76 else if (attr == &ttm_mem_used)
78 mtx_unlock(&zone->glob->lock);
80 return snprintf(buffer, PAGE_SIZE, "%llu\n",
81 (unsigned long long) val >> 10);
85 static void ttm_check_swapping(struct ttm_mem_global *glob);
89 static ssize_t ttm_mem_zone_store(struct ttm_mem_zone *zone,
90 struct attribute *attr,
98 chars = sscanf(buffer, "%lu", &val);
105 mtx_lock(&zone->glob->lock);
106 if (val64 > zone->zone_mem)
107 val64 = zone->zone_mem;
108 if (attr == &ttm_mem_emer) {
109 zone->emer_mem = val64;
110 if (zone->max_mem > val64)
111 zone->max_mem = val64;
112 } else if (attr == &ttm_mem_max) {
113 zone->max_mem = val64;
114 if (zone->emer_mem < val64)
115 zone->emer_mem = val64;
116 } else if (attr == &ttm_mem_swap)
117 zone->swap_limit = val64;
118 mtx_unlock(&zone->glob->lock);
120 ttm_check_swapping(zone->glob);
126 static void ttm_mem_global_kobj_release(struct ttm_mem_global *glob)
130 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
131 bool from_wq, uint64_t extra)
134 struct ttm_mem_zone *zone;
137 for (i = 0; i < glob->num_zones; ++i) {
138 zone = glob->zones[i];
141 target = zone->swap_limit;
142 else if (priv_check(curthread, PRIV_VM_MLOCK) == 0)
143 target = zone->emer_mem;
145 target = zone->max_mem;
147 target = (extra > target) ? 0ULL : target;
149 if (zone->used_mem > target)
156 * At this point we only support a single shrink callback.
157 * Extend this if needed, perhaps using a linked list of callbacks.
158 * Note that this function is reentrant:
159 * many threads may try to swap out at any given time.
162 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
166 struct ttm_mem_shrink *shrink;
168 mtx_lock(&glob->lock);
169 if (glob->shrink == NULL)
172 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
173 shrink = glob->shrink;
174 mtx_unlock(&glob->lock);
175 ret = shrink->do_shrink(shrink);
176 mtx_lock(&glob->lock);
177 if (unlikely(ret != 0))
181 mtx_unlock(&glob->lock);
186 static void ttm_shrink_work(void *arg, int pending __unused)
188 struct ttm_mem_global *glob = arg;
190 ttm_shrink(glob, true, 0ULL);
193 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
196 struct ttm_mem_zone *zone;
198 zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
200 zone->name = "kernel";
201 zone->zone_mem = mem;
202 zone->max_mem = mem >> 1;
203 zone->emer_mem = (mem >> 1) + (mem >> 2);
204 zone->swap_limit = zone->max_mem - (mem >> 3);
207 glob->zone_kernel = zone;
208 refcount_init(&zone->kobj_ref, 1);
209 glob->zones[glob->num_zones++] = zone;
213 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
216 struct ttm_mem_zone *zone;
218 zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
221 * No special dma32 zone needed.
224 if (mem <= ((uint64_t) 1ULL << 32)) {
225 free(zone, M_TTM_ZONE);
230 * Limit max dma32 memory to 4GB for now
231 * until we can figure out how big this
235 mem = ((uint64_t) 1ULL << 32);
236 zone->name = "dma32";
237 zone->zone_mem = mem;
238 zone->max_mem = mem >> 1;
239 zone->emer_mem = (mem >> 1) + (mem >> 2);
240 zone->swap_limit = zone->max_mem - (mem >> 3);
243 glob->zone_dma32 = zone;
244 refcount_init(&zone->kobj_ref, 1);
245 glob->zones[glob->num_zones++] = zone;
249 int ttm_mem_global_init(struct ttm_mem_global *glob)
254 struct ttm_mem_zone *zone;
256 mtx_init(&glob->lock, "ttmgz", NULL, MTX_DEF);
257 glob->swap_queue = taskqueue_create("ttm_swap", M_WAITOK,
258 taskqueue_thread_enqueue, &glob->swap_queue);
259 taskqueue_start_threads(&glob->swap_queue, 1, PVM, "ttm swap");
260 TASK_INIT(&glob->work, 0, ttm_shrink_work, glob);
262 refcount_init(&glob->kobj_ref, 1);
264 mem = physmem * PAGE_SIZE;
266 ret = ttm_mem_init_kernel_zone(glob, mem);
267 if (unlikely(ret != 0))
269 ret = ttm_mem_init_dma32_zone(glob, mem);
270 if (unlikely(ret != 0))
272 for (i = 0; i < glob->num_zones; ++i) {
273 zone = glob->zones[i];
274 printf("[TTM] Zone %7s: Available graphics memory: %llu kiB\n",
275 zone->name, (unsigned long long)zone->max_mem >> 10);
277 ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
278 ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
281 ttm_mem_global_release(glob);
285 void ttm_mem_global_release(struct ttm_mem_global *glob)
288 struct ttm_mem_zone *zone;
290 /* let the page allocator first stop the shrink work. */
291 ttm_page_alloc_fini();
292 ttm_dma_page_alloc_fini();
294 taskqueue_drain(glob->swap_queue, &glob->work);
295 taskqueue_free(glob->swap_queue);
296 glob->swap_queue = NULL;
297 for (i = 0; i < glob->num_zones; ++i) {
298 zone = glob->zones[i];
299 if (refcount_release(&zone->kobj_ref))
300 ttm_mem_zone_kobj_release(zone);
302 if (refcount_release(&glob->kobj_ref))
303 ttm_mem_global_kobj_release(glob);
306 static void ttm_check_swapping(struct ttm_mem_global *glob)
308 bool needs_swapping = false;
310 struct ttm_mem_zone *zone;
312 mtx_lock(&glob->lock);
313 for (i = 0; i < glob->num_zones; ++i) {
314 zone = glob->zones[i];
315 if (zone->used_mem > zone->swap_limit) {
316 needs_swapping = true;
321 mtx_unlock(&glob->lock);
323 if (unlikely(needs_swapping))
324 taskqueue_enqueue(glob->swap_queue, &glob->work);
328 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
329 struct ttm_mem_zone *single_zone,
333 struct ttm_mem_zone *zone;
335 mtx_lock(&glob->lock);
336 for (i = 0; i < glob->num_zones; ++i) {
337 zone = glob->zones[i];
338 if (single_zone && zone != single_zone)
340 zone->used_mem -= amount;
342 mtx_unlock(&glob->lock);
345 void ttm_mem_global_free(struct ttm_mem_global *glob,
348 return ttm_mem_global_free_zone(glob, NULL, amount);
351 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
352 struct ttm_mem_zone *single_zone,
353 uint64_t amount, bool reserve)
358 struct ttm_mem_zone *zone;
360 mtx_lock(&glob->lock);
361 for (i = 0; i < glob->num_zones; ++i) {
362 zone = glob->zones[i];
363 if (single_zone && zone != single_zone)
366 limit = (priv_check(curthread, PRIV_VM_MLOCK) == 0) ?
367 zone->emer_mem : zone->max_mem;
369 if (zone->used_mem > limit)
374 for (i = 0; i < glob->num_zones; ++i) {
375 zone = glob->zones[i];
376 if (single_zone && zone != single_zone)
378 zone->used_mem += amount;
384 mtx_unlock(&glob->lock);
385 ttm_check_swapping(glob);
391 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
392 struct ttm_mem_zone *single_zone,
394 bool no_wait, bool interruptible)
396 int count = TTM_MEMORY_ALLOC_RETRIES;
398 while (unlikely(ttm_mem_global_reserve(glob,
404 if (unlikely(count-- == 0))
406 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
412 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
413 bool no_wait, bool interruptible)
416 * Normal allocations of kernel memory are registered in
420 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
424 #define page_to_pfn(pp) OFF_TO_IDX(VM_PAGE_TO_PHYS(pp))
426 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
427 struct vm_page *page,
428 bool no_wait, bool interruptible)
431 struct ttm_mem_zone *zone = NULL;
434 * Page allocations may be registed in a single zone
435 * only if highmem or !dma32.
438 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
439 zone = glob->zone_kernel;
440 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
444 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct vm_page *page)
446 struct ttm_mem_zone *zone = NULL;
448 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
449 zone = glob->zone_kernel;
450 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
454 size_t ttm_round_pot(size_t size)
456 if ((size & (size - 1)) == 0)
458 else if (size > PAGE_SIZE)
459 return PAGE_ALIGN(size);
463 while (tmp_size < size)