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 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_memory.c 248663 2013-03-23 20:46:47Z dumbbell $
27 **************************************************************************/
30 #include <drm/ttm/ttm_memory.h>
31 #include <drm/ttm/ttm_module.h>
32 #include <drm/ttm/ttm_page_alloc.h>
34 #define TTM_MEMORY_ALLOC_RETRIES 4
38 struct ttm_mem_global *glob;
47 MALLOC_DEFINE(M_TTM_ZONE, "ttm_zone", "TTM Zone");
49 static void ttm_mem_zone_kobj_release(struct ttm_mem_zone *zone)
52 kprintf("[TTM] Zone %7s: Used memory at exit: %llu kiB\n",
53 zone->name, (unsigned long long)zone->used_mem >> 10);
54 drm_free(zone, M_TTM_ZONE);
59 static ssize_t ttm_mem_zone_show(struct ttm_mem_zone *zone;
60 struct attribute *attr,
65 mtx_lock(&zone->glob->lock);
66 if (attr == &ttm_mem_sys)
68 else if (attr == &ttm_mem_emer)
70 else if (attr == &ttm_mem_max)
72 else if (attr == &ttm_mem_swap)
73 val = zone->swap_limit;
74 else if (attr == &ttm_mem_used)
76 mtx_unlock(&zone->glob->lock);
78 return snprintf(buffer, PAGE_SIZE, "%llu\n",
79 (unsigned long long) val >> 10);
83 static void ttm_check_swapping(struct ttm_mem_global *glob);
87 static ssize_t ttm_mem_zone_store(struct ttm_mem_zone *zone,
88 struct attribute *attr,
96 chars = sscanf(buffer, "%lu", &val);
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);
118 ttm_check_swapping(zone->glob);
124 static void ttm_mem_global_kobj_release(struct ttm_mem_global *glob)
128 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
129 bool from_wq, uint64_t extra)
132 struct ttm_mem_zone *zone;
135 for (i = 0; i < glob->num_zones; ++i) {
136 zone = glob->zones[i];
139 target = zone->swap_limit;
140 else if (priv_check(curthread, PRIV_VM_MLOCK) == 0)
141 target = zone->emer_mem;
143 target = zone->max_mem;
145 target = (extra > target) ? 0ULL : target;
147 if (zone->used_mem > target)
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.
160 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
164 struct ttm_mem_shrink *shrink;
166 spin_lock(&glob->spin);
167 if (glob->shrink == NULL)
170 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
171 shrink = glob->shrink;
172 spin_lock(&glob->spin);
173 ret = shrink->do_shrink(shrink);
174 spin_unlock(&glob->spin);
175 if (unlikely(ret != 0))
179 spin_unlock(&glob->spin);
184 static void ttm_shrink_work(void *arg, int pending __unused)
186 struct ttm_mem_global *glob = arg;
188 ttm_shrink(glob, true, 0ULL);
191 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
194 struct ttm_mem_zone *zone;
196 zone = kmalloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
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);
205 glob->zone_kernel = zone;
206 refcount_init(&zone->kobj_ref, 1);
207 glob->zones[glob->num_zones++] = zone;
211 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
214 struct ttm_mem_zone *zone;
216 zone = kmalloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO);
219 * No special dma32 zone needed.
222 if (mem <= ((uint64_t) 1ULL << 32)) {
223 drm_free(zone, M_TTM_ZONE);
228 * Limit max dma32 memory to 4GB for now
229 * until we can figure out how big this
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);
241 glob->zone_dma32 = zone;
242 refcount_init(&zone->kobj_ref, 1);
243 glob->zones[glob->num_zones++] = zone;
247 int ttm_mem_global_init(struct ttm_mem_global *glob)
252 struct ttm_mem_zone *zone;
254 spin_init(&glob->spin);
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, 0, -1, "ttm swap");
258 TASK_INIT(&glob->work, 0, ttm_shrink_work, glob);
260 refcount_init(&glob->kobj_ref, 1);
262 mem = physmem * PAGE_SIZE;
264 ret = ttm_mem_init_kernel_zone(glob, mem);
265 if (unlikely(ret != 0))
267 ret = ttm_mem_init_dma32_zone(glob, mem);
268 if (unlikely(ret != 0))
270 for (i = 0; i < glob->num_zones; ++i) {
271 zone = glob->zones[i];
272 kprintf("[TTM] Zone %7s: Available graphics memory: %llu kiB\n",
273 zone->name, (unsigned long long)zone->max_mem >> 10);
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));
279 ttm_mem_global_release(glob);
283 void ttm_mem_global_release(struct ttm_mem_global *glob)
286 struct ttm_mem_zone *zone;
288 /* let the page allocator first stop the shrink work. */
289 ttm_page_alloc_fini();
290 ttm_dma_page_alloc_fini();
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);
300 if (refcount_release(&glob->kobj_ref))
301 ttm_mem_global_kobj_release(glob);
304 static void ttm_check_swapping(struct ttm_mem_global *glob)
306 bool needs_swapping = false;
308 struct ttm_mem_zone *zone;
310 spin_lock(&glob->spin);
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;
318 spin_unlock(&glob->spin);
320 if (unlikely(needs_swapping))
321 taskqueue_enqueue(glob->swap_queue, &glob->work);
325 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
326 struct ttm_mem_zone *single_zone,
330 struct ttm_mem_zone *zone;
332 spin_lock(&glob->spin);
333 for (i = 0; i < glob->num_zones; ++i) {
334 zone = glob->zones[i];
335 if (single_zone && zone != single_zone)
337 zone->used_mem -= amount;
339 spin_unlock(&glob->spin);
342 void ttm_mem_global_free(struct ttm_mem_global *glob,
345 ttm_mem_global_free_zone(glob, NULL, amount);
348 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
349 struct ttm_mem_zone *single_zone,
350 uint64_t amount, bool reserve)
355 struct ttm_mem_zone *zone;
357 spin_lock(&glob->spin);
358 for (i = 0; i < glob->num_zones; ++i) {
359 zone = glob->zones[i];
360 if (single_zone && zone != single_zone)
363 limit = (priv_check(curthread, PRIV_VM_MLOCK) == 0) ?
364 zone->emer_mem : zone->max_mem;
366 if (zone->used_mem > limit)
371 for (i = 0; i < glob->num_zones; ++i) {
372 zone = glob->zones[i];
373 if (single_zone && zone != single_zone)
375 zone->used_mem += amount;
381 spin_unlock(&glob->spin);
382 ttm_check_swapping(glob);
388 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
389 struct ttm_mem_zone *single_zone,
391 bool no_wait, bool interruptible)
393 int count = TTM_MEMORY_ALLOC_RETRIES;
395 while (unlikely(ttm_mem_global_reserve(glob,
401 if (unlikely(count-- == 0))
403 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
409 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
410 bool no_wait, bool interruptible)
413 * Normal allocations of kernel memory are registered in
417 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
421 #define page_to_pfn(pp) OFF_TO_IDX(VM_PAGE_TO_PHYS(pp))
423 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
424 struct vm_page *page,
425 bool no_wait, bool interruptible)
428 struct ttm_mem_zone *zone = NULL;
431 * Page allocations may be registed in a single zone
432 * only if highmem or !dma32.
435 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
436 zone = glob->zone_kernel;
437 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
441 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct vm_page *page)
443 struct ttm_mem_zone *zone = NULL;
445 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
446 zone = glob->zone_kernel;
447 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
451 size_t ttm_round_pot(size_t size)
453 if ((size & (size - 1)) == 0)
455 else if (size > PAGE_SIZE)
456 return PAGE_ALIGN(size);
460 while (tmp_size < size)