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 #define pr_fmt(fmt) "[TTM] " fmt
30 #include <drm/ttm/ttm_memory.h>
31 #include <drm/ttm/ttm_module.h>
32 #include <drm/ttm/ttm_page_alloc.h>
33 #include <linux/spinlock.h>
34 #include <linux/sched.h>
35 #include <linux/wait.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
40 #define TTM_MEMORY_ALLOC_RETRIES 4
44 struct ttm_mem_global
*glob
;
53 static struct attribute ttm_mem_sys
= {
54 .name
= "zone_memory",
57 static struct attribute ttm_mem_emer
= {
58 .name
= "emergency_memory",
59 .mode
= S_IRUGO
| S_IWUSR
61 static struct attribute ttm_mem_max
= {
62 .name
= "available_memory",
63 .mode
= S_IRUGO
| S_IWUSR
65 static struct attribute ttm_mem_swap
= {
67 .mode
= S_IRUGO
| S_IWUSR
69 static struct attribute ttm_mem_used
= {
70 .name
= "used_memory",
74 static void ttm_mem_zone_kobj_release(struct kobject
*kobj
)
76 struct ttm_mem_zone
*zone
=
77 container_of(kobj
, struct ttm_mem_zone
, kobj
);
79 pr_info("Zone %7s: Used memory at exit: %llu kiB\n",
80 zone
->name
, (unsigned long long)zone
->used_mem
>> 10);
84 static ssize_t
ttm_mem_zone_show(struct kobject
*kobj
,
85 struct attribute
*attr
,
88 struct ttm_mem_zone
*zone
=
89 container_of(kobj
, struct ttm_mem_zone
, kobj
);
92 spin_lock(&zone
->glob
->lock
);
93 if (attr
== &ttm_mem_sys
)
95 else if (attr
== &ttm_mem_emer
)
97 else if (attr
== &ttm_mem_max
)
99 else if (attr
== &ttm_mem_swap
)
100 val
= zone
->swap_limit
;
101 else if (attr
== &ttm_mem_used
)
102 val
= zone
->used_mem
;
103 spin_unlock(&zone
->glob
->lock
);
105 return snprintf(buffer
, PAGE_SIZE
, "%llu\n",
106 (unsigned long long) val
>> 10);
109 static void ttm_check_swapping(struct ttm_mem_global
*glob
);
111 static ssize_t
ttm_mem_zone_store(struct kobject
*kobj
,
112 struct attribute
*attr
,
116 struct ttm_mem_zone
*zone
=
117 container_of(kobj
, struct ttm_mem_zone
, kobj
);
122 chars
= sscanf(buffer
, "%lu", &val
);
129 spin_lock(&zone
->glob
->lock
);
130 if (val64
> zone
->zone_mem
)
131 val64
= zone
->zone_mem
;
132 if (attr
== &ttm_mem_emer
) {
133 zone
->emer_mem
= val64
;
134 if (zone
->max_mem
> val64
)
135 zone
->max_mem
= val64
;
136 } else if (attr
== &ttm_mem_max
) {
137 zone
->max_mem
= val64
;
138 if (zone
->emer_mem
< val64
)
139 zone
->emer_mem
= val64
;
140 } else if (attr
== &ttm_mem_swap
)
141 zone
->swap_limit
= val64
;
142 spin_unlock(&zone
->glob
->lock
);
144 ttm_check_swapping(zone
->glob
);
149 static struct attribute
*ttm_mem_zone_attrs
[] = {
158 static const struct sysfs_ops ttm_mem_zone_ops
= {
159 .show
= &ttm_mem_zone_show
,
160 .store
= &ttm_mem_zone_store
163 static struct kobj_type ttm_mem_zone_kobj_type
= {
164 .release
= &ttm_mem_zone_kobj_release
,
165 .sysfs_ops
= &ttm_mem_zone_ops
,
166 .default_attrs
= ttm_mem_zone_attrs
,
169 static void ttm_mem_global_kobj_release(struct kobject
*kobj
)
171 struct ttm_mem_global
*glob
=
172 container_of(kobj
, struct ttm_mem_global
, kobj
);
177 static struct kobj_type ttm_mem_glob_kobj_type
= {
178 .release
= &ttm_mem_global_kobj_release
,
181 static bool ttm_zones_above_swap_target(struct ttm_mem_global
*glob
,
182 bool from_wq
, uint64_t extra
)
185 struct ttm_mem_zone
*zone
;
188 for (i
= 0; i
< glob
->num_zones
; ++i
) {
189 zone
= glob
->zones
[i
];
192 target
= zone
->swap_limit
;
193 else if (capable(CAP_SYS_ADMIN
))
194 target
= zone
->emer_mem
;
196 target
= zone
->max_mem
;
198 target
= (extra
> target
) ? 0ULL : target
;
200 if (zone
->used_mem
> target
)
207 * At this point we only support a single shrink callback.
208 * Extend this if needed, perhaps using a linked list of callbacks.
209 * Note that this function is reentrant:
210 * many threads may try to swap out at any given time.
213 static void ttm_shrink(struct ttm_mem_global
*glob
, bool from_wq
,
214 uint64_t extra
, struct ttm_operation_ctx
*ctx
)
218 spin_lock(&glob
->lock
);
220 while (ttm_zones_above_swap_target(glob
, from_wq
, extra
)) {
221 spin_unlock(&glob
->lock
);
222 ret
= ttm_bo_swapout(glob
->bo_glob
, ctx
);
223 spin_lock(&glob
->lock
);
224 if (unlikely(ret
!= 0))
228 spin_unlock(&glob
->lock
);
231 static void ttm_shrink_work(struct work_struct
*work
)
233 struct ttm_operation_ctx ctx
= {
234 .interruptible
= false,
237 struct ttm_mem_global
*glob
=
238 container_of(work
, struct ttm_mem_global
, work
);
240 ttm_shrink(glob
, true, 0ULL, &ctx
);
243 static int ttm_mem_init_kernel_zone(struct ttm_mem_global
*glob
,
244 const struct sysinfo
*si
)
246 struct ttm_mem_zone
*zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
253 mem
= si
->totalram
- si
->totalhigh
;
256 zone
->name
= "kernel";
257 zone
->zone_mem
= mem
;
258 zone
->max_mem
= mem
>> 1;
259 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
260 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
263 glob
->zone_kernel
= zone
;
264 ret
= kobject_init_and_add(
265 &zone
->kobj
, &ttm_mem_zone_kobj_type
, &glob
->kobj
, zone
->name
);
266 if (unlikely(ret
!= 0)) {
267 kobject_put(&zone
->kobj
);
270 glob
->zones
[glob
->num_zones
++] = zone
;
274 #ifdef CONFIG_HIGHMEM
275 static int ttm_mem_init_highmem_zone(struct ttm_mem_global
*glob
,
276 const struct sysinfo
*si
)
278 struct ttm_mem_zone
*zone
;
282 if (si
->totalhigh
== 0)
285 zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
292 zone
->name
= "highmem";
293 zone
->zone_mem
= mem
;
294 zone
->max_mem
= mem
>> 1;
295 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
296 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
299 glob
->zone_highmem
= zone
;
300 ret
= kobject_init_and_add(
301 &zone
->kobj
, &ttm_mem_zone_kobj_type
, &glob
->kobj
, "%s",
303 if (unlikely(ret
!= 0)) {
304 kobject_put(&zone
->kobj
);
307 glob
->zones
[glob
->num_zones
++] = zone
;
311 static int ttm_mem_init_dma32_zone(struct ttm_mem_global
*glob
,
312 const struct sysinfo
*si
)
314 struct ttm_mem_zone
*zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
325 * No special dma32 zone needed.
328 if (mem
<= ((uint64_t) 1ULL << 32)) {
334 * Limit max dma32 memory to 4GB for now
335 * until we can figure out how big this
339 mem
= ((uint64_t) 1ULL << 32);
340 zone
->name
= "dma32";
341 zone
->zone_mem
= mem
;
342 zone
->max_mem
= mem
>> 1;
343 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
344 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
347 glob
->zone_dma32
= zone
;
348 ret
= kobject_init_and_add(
349 &zone
->kobj
, &ttm_mem_zone_kobj_type
, &glob
->kobj
, zone
->name
);
350 if (unlikely(ret
!= 0)) {
351 kobject_put(&zone
->kobj
);
354 glob
->zones
[glob
->num_zones
++] = zone
;
359 int ttm_mem_global_init(struct ttm_mem_global
*glob
)
364 struct ttm_mem_zone
*zone
;
366 spin_lock_init(&glob
->lock
);
367 glob
->swap_queue
= create_singlethread_workqueue("ttm_swap");
368 INIT_WORK(&glob
->work
, ttm_shrink_work
);
369 ret
= kobject_init_and_add(
370 &glob
->kobj
, &ttm_mem_glob_kobj_type
, ttm_get_kobj(), "memory_accounting");
371 if (unlikely(ret
!= 0)) {
372 kobject_put(&glob
->kobj
);
378 ret
= ttm_mem_init_kernel_zone(glob
, &si
);
379 if (unlikely(ret
!= 0))
381 #ifdef CONFIG_HIGHMEM
382 ret
= ttm_mem_init_highmem_zone(glob
, &si
);
383 if (unlikely(ret
!= 0))
386 ret
= ttm_mem_init_dma32_zone(glob
, &si
);
387 if (unlikely(ret
!= 0))
390 for (i
= 0; i
< glob
->num_zones
; ++i
) {
391 zone
= glob
->zones
[i
];
392 pr_info("Zone %7s: Available graphics memory: %llu kiB\n",
393 zone
->name
, (unsigned long long)zone
->max_mem
>> 10);
395 ttm_page_alloc_init(glob
, glob
->zone_kernel
->max_mem
/(2*PAGE_SIZE
));
396 ttm_dma_page_alloc_init(glob
, glob
->zone_kernel
->max_mem
/(2*PAGE_SIZE
));
399 ttm_mem_global_release(glob
);
402 EXPORT_SYMBOL(ttm_mem_global_init
);
404 void ttm_mem_global_release(struct ttm_mem_global
*glob
)
407 struct ttm_mem_zone
*zone
;
409 /* let the page allocator first stop the shrink work. */
410 ttm_page_alloc_fini();
411 ttm_dma_page_alloc_fini();
413 flush_workqueue(glob
->swap_queue
);
414 destroy_workqueue(glob
->swap_queue
);
415 glob
->swap_queue
= NULL
;
416 for (i
= 0; i
< glob
->num_zones
; ++i
) {
417 zone
= glob
->zones
[i
];
418 kobject_del(&zone
->kobj
);
419 kobject_put(&zone
->kobj
);
421 kobject_del(&glob
->kobj
);
422 kobject_put(&glob
->kobj
);
424 EXPORT_SYMBOL(ttm_mem_global_release
);
426 static void ttm_check_swapping(struct ttm_mem_global
*glob
)
428 bool needs_swapping
= false;
430 struct ttm_mem_zone
*zone
;
432 spin_lock(&glob
->lock
);
433 for (i
= 0; i
< glob
->num_zones
; ++i
) {
434 zone
= glob
->zones
[i
];
435 if (zone
->used_mem
> zone
->swap_limit
) {
436 needs_swapping
= true;
441 spin_unlock(&glob
->lock
);
443 if (unlikely(needs_swapping
))
444 (void)queue_work(glob
->swap_queue
, &glob
->work
);
448 static void ttm_mem_global_free_zone(struct ttm_mem_global
*glob
,
449 struct ttm_mem_zone
*single_zone
,
453 struct ttm_mem_zone
*zone
;
455 spin_lock(&glob
->lock
);
456 for (i
= 0; i
< glob
->num_zones
; ++i
) {
457 zone
= glob
->zones
[i
];
458 if (single_zone
&& zone
!= single_zone
)
460 zone
->used_mem
-= amount
;
462 spin_unlock(&glob
->lock
);
465 void ttm_mem_global_free(struct ttm_mem_global
*glob
,
468 return ttm_mem_global_free_zone(glob
, NULL
, amount
);
470 EXPORT_SYMBOL(ttm_mem_global_free
);
472 static int ttm_mem_global_reserve(struct ttm_mem_global
*glob
,
473 struct ttm_mem_zone
*single_zone
,
474 uint64_t amount
, bool reserve
)
479 struct ttm_mem_zone
*zone
;
481 spin_lock(&glob
->lock
);
482 for (i
= 0; i
< glob
->num_zones
; ++i
) {
483 zone
= glob
->zones
[i
];
484 if (single_zone
&& zone
!= single_zone
)
487 limit
= (capable(CAP_SYS_ADMIN
)) ?
488 zone
->emer_mem
: zone
->max_mem
;
490 if (zone
->used_mem
> limit
)
495 for (i
= 0; i
< glob
->num_zones
; ++i
) {
496 zone
= glob
->zones
[i
];
497 if (single_zone
&& zone
!= single_zone
)
499 zone
->used_mem
+= amount
;
505 spin_unlock(&glob
->lock
);
506 ttm_check_swapping(glob
);
512 static int ttm_mem_global_alloc_zone(struct ttm_mem_global
*glob
,
513 struct ttm_mem_zone
*single_zone
,
515 struct ttm_operation_ctx
*ctx
)
517 int count
= TTM_MEMORY_ALLOC_RETRIES
;
519 while (unlikely(ttm_mem_global_reserve(glob
,
523 if (ctx
->no_wait_gpu
)
525 if (unlikely(count
-- == 0))
527 ttm_shrink(glob
, false, memory
+ (memory
>> 2) + 16, ctx
);
533 int ttm_mem_global_alloc(struct ttm_mem_global
*glob
, uint64_t memory
,
534 struct ttm_operation_ctx
*ctx
)
537 * Normal allocations of kernel memory are registered in
541 return ttm_mem_global_alloc_zone(glob
, NULL
, memory
, ctx
);
543 EXPORT_SYMBOL(ttm_mem_global_alloc
);
545 int ttm_mem_global_alloc_page(struct ttm_mem_global
*glob
,
546 struct page
*page
, uint64_t size
,
547 struct ttm_operation_ctx
*ctx
)
549 struct ttm_mem_zone
*zone
= NULL
;
552 * Page allocations may be registed in a single zone
553 * only if highmem or !dma32.
556 #ifdef CONFIG_HIGHMEM
557 if (PageHighMem(page
) && glob
->zone_highmem
!= NULL
)
558 zone
= glob
->zone_highmem
;
560 if (glob
->zone_dma32
&& page_to_pfn(page
) > 0x00100000UL
)
561 zone
= glob
->zone_kernel
;
563 return ttm_mem_global_alloc_zone(glob
, zone
, size
, ctx
);
566 void ttm_mem_global_free_page(struct ttm_mem_global
*glob
, struct page
*page
,
569 struct ttm_mem_zone
*zone
= NULL
;
571 #ifdef CONFIG_HIGHMEM
572 if (PageHighMem(page
) && glob
->zone_highmem
!= NULL
)
573 zone
= glob
->zone_highmem
;
575 if (glob
->zone_dma32
&& page_to_pfn(page
) > 0x00100000UL
)
576 zone
= glob
->zone_kernel
;
578 ttm_mem_global_free_zone(glob
, zone
, size
);
581 size_t ttm_round_pot(size_t size
)
583 if ((size
& (size
- 1)) == 0)
585 else if (size
> PAGE_SIZE
)
586 return PAGE_ALIGN(size
);
590 while (tmp_size
< size
)
597 EXPORT_SYMBOL(ttm_round_pot
);
599 uint64_t ttm_get_kernel_zone_memory_size(struct ttm_mem_global
*glob
)
601 return glob
->zone_kernel
->max_mem
;
603 EXPORT_SYMBOL(ttm_get_kernel_zone_memory_size
);