drm/ttm: Fixes for "Make parts of a struct ttm_bo_device global"
[linux-2.6-block.git] / drivers / gpu / drm / ttm / ttm_memory.c
CommitLineData
ba4e7d97
TH
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 "ttm/ttm_memory.h"
5fd9cbad 29#include "ttm/ttm_module.h"
ba4e7d97
TH
30#include <linux/spinlock.h>
31#include <linux/sched.h>
32#include <linux/wait.h>
33#include <linux/mm.h>
34#include <linux/module.h>
35
ba4e7d97
TH
36#define TTM_MEMORY_ALLOC_RETRIES 4
37
5fd9cbad
TH
38struct ttm_mem_zone {
39 struct kobject kobj;
40 struct ttm_mem_global *glob;
41 const char *name;
42 uint64_t zone_mem;
43 uint64_t emer_mem;
44 uint64_t max_mem;
45 uint64_t swap_limit;
46 uint64_t used_mem;
47};
48
49static struct attribute ttm_mem_sys = {
50 .name = "zone_memory",
51 .mode = S_IRUGO
52};
53static struct attribute ttm_mem_emer = {
54 .name = "emergency_memory",
55 .mode = S_IRUGO | S_IWUSR
56};
57static struct attribute ttm_mem_max = {
58 .name = "available_memory",
59 .mode = S_IRUGO | S_IWUSR
60};
61static struct attribute ttm_mem_swap = {
62 .name = "swap_limit",
63 .mode = S_IRUGO | S_IWUSR
64};
65static struct attribute ttm_mem_used = {
66 .name = "used_memory",
67 .mode = S_IRUGO
68};
69
70static void ttm_mem_zone_kobj_release(struct kobject *kobj)
71{
72 struct ttm_mem_zone *zone =
73 container_of(kobj, struct ttm_mem_zone, kobj);
74
75 printk(KERN_INFO TTM_PFX
76 "Zone %7s: Used memory at exit: %llu kiB.\n",
77 zone->name, (unsigned long long) zone->used_mem >> 10);
78 kfree(zone);
79}
80
81static ssize_t ttm_mem_zone_show(struct kobject *kobj,
82 struct attribute *attr,
83 char *buffer)
84{
85 struct ttm_mem_zone *zone =
86 container_of(kobj, struct ttm_mem_zone, kobj);
87 uint64_t val = 0;
88
89 spin_lock(&zone->glob->lock);
90 if (attr == &ttm_mem_sys)
91 val = zone->zone_mem;
92 else if (attr == &ttm_mem_emer)
93 val = zone->emer_mem;
94 else if (attr == &ttm_mem_max)
95 val = zone->max_mem;
96 else if (attr == &ttm_mem_swap)
97 val = zone->swap_limit;
98 else if (attr == &ttm_mem_used)
99 val = zone->used_mem;
100 spin_unlock(&zone->glob->lock);
101
102 return snprintf(buffer, PAGE_SIZE, "%llu\n",
103 (unsigned long long) val >> 10);
104}
105
106static void ttm_check_swapping(struct ttm_mem_global *glob);
107
108static ssize_t ttm_mem_zone_store(struct kobject *kobj,
109 struct attribute *attr,
110 const char *buffer,
111 size_t size)
112{
113 struct ttm_mem_zone *zone =
114 container_of(kobj, struct ttm_mem_zone, kobj);
115 int chars;
116 unsigned long val;
117 uint64_t val64;
118
119 chars = sscanf(buffer, "%lu", &val);
120 if (chars == 0)
121 return size;
122
123 val64 = val;
124 val64 <<= 10;
125
126 spin_lock(&zone->glob->lock);
127 if (val64 > zone->zone_mem)
128 val64 = zone->zone_mem;
129 if (attr == &ttm_mem_emer) {
130 zone->emer_mem = val64;
131 if (zone->max_mem > val64)
132 zone->max_mem = val64;
133 } else if (attr == &ttm_mem_max) {
134 zone->max_mem = val64;
135 if (zone->emer_mem < val64)
136 zone->emer_mem = val64;
137 } else if (attr == &ttm_mem_swap)
138 zone->swap_limit = val64;
139 spin_unlock(&zone->glob->lock);
140
141 ttm_check_swapping(zone->glob);
142
143 return size;
144}
145
146static struct attribute *ttm_mem_zone_attrs[] = {
147 &ttm_mem_sys,
148 &ttm_mem_emer,
149 &ttm_mem_max,
150 &ttm_mem_swap,
151 &ttm_mem_used,
152 NULL
153};
154
155static struct sysfs_ops ttm_mem_zone_ops = {
156 .show = &ttm_mem_zone_show,
157 .store = &ttm_mem_zone_store
158};
159
160static struct kobj_type ttm_mem_zone_kobj_type = {
161 .release = &ttm_mem_zone_kobj_release,
162 .sysfs_ops = &ttm_mem_zone_ops,
163 .default_attrs = ttm_mem_zone_attrs,
164};
165
166static void ttm_mem_global_kobj_release(struct kobject *kobj)
167{
168 struct ttm_mem_global *glob =
169 container_of(kobj, struct ttm_mem_global, kobj);
170
171 kfree(glob);
172}
173
174static struct kobj_type ttm_mem_glob_kobj_type = {
175 .release = &ttm_mem_global_kobj_release,
176};
177
178static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
179 bool from_wq, uint64_t extra)
180{
181 unsigned int i;
182 struct ttm_mem_zone *zone;
183 uint64_t target;
184
185 for (i = 0; i < glob->num_zones; ++i) {
186 zone = glob->zones[i];
187
188 if (from_wq)
189 target = zone->swap_limit;
190 else if (capable(CAP_SYS_ADMIN))
191 target = zone->emer_mem;
192 else
193 target = zone->max_mem;
194
195 target = (extra > target) ? 0ULL : target;
196
197 if (zone->used_mem > target)
198 return true;
199 }
200 return false;
201}
202
ba4e7d97
TH
203/**
204 * At this point we only support a single shrink callback.
205 * Extend this if needed, perhaps using a linked list of callbacks.
206 * Note that this function is reentrant:
207 * many threads may try to swap out at any given time.
208 */
209
5fd9cbad 210static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
ba4e7d97
TH
211 uint64_t extra)
212{
213 int ret;
214 struct ttm_mem_shrink *shrink;
ba4e7d97
TH
215
216 spin_lock(&glob->lock);
217 if (glob->shrink == NULL)
218 goto out;
219
5fd9cbad 220 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
ba4e7d97
TH
221 shrink = glob->shrink;
222 spin_unlock(&glob->lock);
223 ret = shrink->do_shrink(shrink);
224 spin_lock(&glob->lock);
225 if (unlikely(ret != 0))
226 goto out;
227 }
228out:
229 spin_unlock(&glob->lock);
230}
231
5fd9cbad
TH
232
233
ba4e7d97
TH
234static void ttm_shrink_work(struct work_struct *work)
235{
236 struct ttm_mem_global *glob =
237 container_of(work, struct ttm_mem_global, work);
238
239 ttm_shrink(glob, true, 0ULL);
240}
241
5fd9cbad
TH
242static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
243 const struct sysinfo *si)
244{
245 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
246 uint64_t mem;
247
248 if (unlikely(!zone))
249 return -ENOMEM;
250
251 mem = si->totalram - si->totalhigh;
252 mem *= si->mem_unit;
253
254 zone->name = "kernel";
255 zone->zone_mem = mem;
256 zone->max_mem = mem >> 1;
257 zone->emer_mem = (mem >> 1) + (mem >> 2);
258 zone->swap_limit = zone->max_mem - (mem >> 3);
259 zone->used_mem = 0;
260 zone->glob = glob;
261 glob->zone_kernel = zone;
262 glob->zones[glob->num_zones++] = zone;
263 kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
264 return kobject_add(&zone->kobj, &glob->kobj, zone->name);
265}
266
267#ifdef CONFIG_HIGHMEM
268static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
269 const struct sysinfo *si)
270{
271 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
272 uint64_t mem;
273
274 if (unlikely(!zone))
275 return -ENOMEM;
276
277 if (si->totalhigh == 0)
278 return 0;
279
280 mem = si->totalram;
281 mem *= si->mem_unit;
282
283 zone->name = "highmem";
284 zone->zone_mem = mem;
285 zone->max_mem = mem >> 1;
286 zone->emer_mem = (mem >> 1) + (mem >> 2);
287 zone->swap_limit = zone->max_mem - (mem >> 3);
288 zone->used_mem = 0;
289 zone->glob = glob;
290 glob->zone_highmem = zone;
291 glob->zones[glob->num_zones++] = zone;
292 kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
293 return kobject_add(&zone->kobj, &glob->kobj, zone->name);
294}
295#else
296static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
297 const struct sysinfo *si)
298{
299 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
300 uint64_t mem;
301
302 if (unlikely(!zone))
303 return -ENOMEM;
304
305 mem = si->totalram;
306 mem *= si->mem_unit;
307
308 /**
309 * No special dma32 zone needed.
310 */
311
312 if (mem <= ((uint64_t) 1ULL << 32))
313 return 0;
314
315 /*
316 * Limit max dma32 memory to 4GB for now
317 * until we can figure out how big this
318 * zone really is.
319 */
320
321 mem = ((uint64_t) 1ULL << 32);
322 zone->name = "dma32";
323 zone->zone_mem = mem;
324 zone->max_mem = mem >> 1;
325 zone->emer_mem = (mem >> 1) + (mem >> 2);
326 zone->swap_limit = zone->max_mem - (mem >> 3);
327 zone->used_mem = 0;
328 zone->glob = glob;
329 glob->zone_dma32 = zone;
330 glob->zones[glob->num_zones++] = zone;
331 kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
332 return kobject_add(&zone->kobj, &glob->kobj, zone->name);
333}
334#endif
335
ba4e7d97
TH
336int ttm_mem_global_init(struct ttm_mem_global *glob)
337{
338 struct sysinfo si;
5fd9cbad
TH
339 int ret;
340 int i;
341 struct ttm_mem_zone *zone;
ba4e7d97
TH
342
343 spin_lock_init(&glob->lock);
344 glob->swap_queue = create_singlethread_workqueue("ttm_swap");
345 INIT_WORK(&glob->work, ttm_shrink_work);
346 init_waitqueue_head(&glob->queue);
5fd9cbad
TH
347 kobject_init(&glob->kobj, &ttm_mem_glob_kobj_type);
348 ret = kobject_add(&glob->kobj,
349 ttm_get_kobj(),
350 "memory_accounting");
351 if (unlikely(ret != 0))
352 goto out_no_zone;
ba4e7d97
TH
353
354 si_meminfo(&si);
355
5fd9cbad
TH
356 ret = ttm_mem_init_kernel_zone(glob, &si);
357 if (unlikely(ret != 0))
358 goto out_no_zone;
359#ifdef CONFIG_HIGHMEM
360 ret = ttm_mem_init_highmem_zone(glob, &si);
361 if (unlikely(ret != 0))
362 goto out_no_zone;
363#else
364 ret = ttm_mem_init_dma32_zone(glob, &si);
365 if (unlikely(ret != 0))
366 goto out_no_zone;
367#endif
368 for (i = 0; i < glob->num_zones; ++i) {
369 zone = glob->zones[i];
370 printk(KERN_INFO TTM_PFX
371 "Zone %7s: Available graphics memory: %llu kiB.\n",
372 zone->name, (unsigned long long) zone->max_mem >> 10);
373 }
ba4e7d97 374 return 0;
5fd9cbad
TH
375out_no_zone:
376 ttm_mem_global_release(glob);
377 return ret;
ba4e7d97
TH
378}
379EXPORT_SYMBOL(ttm_mem_global_init);
380
381void ttm_mem_global_release(struct ttm_mem_global *glob)
382{
5fd9cbad
TH
383 unsigned int i;
384 struct ttm_mem_zone *zone;
385
ba4e7d97
TH
386 flush_workqueue(glob->swap_queue);
387 destroy_workqueue(glob->swap_queue);
388 glob->swap_queue = NULL;
5fd9cbad
TH
389 for (i = 0; i < glob->num_zones; ++i) {
390 zone = glob->zones[i];
391 kobject_del(&zone->kobj);
392 kobject_put(&zone->kobj);
393 }
394 kobject_del(&glob->kobj);
395 kobject_put(&glob->kobj);
ba4e7d97
TH
396}
397EXPORT_SYMBOL(ttm_mem_global_release);
398
5fd9cbad 399static void ttm_check_swapping(struct ttm_mem_global *glob)
ba4e7d97 400{
5fd9cbad
TH
401 bool needs_swapping = false;
402 unsigned int i;
403 struct ttm_mem_zone *zone;
ba4e7d97
TH
404
405 spin_lock(&glob->lock);
5fd9cbad
TH
406 for (i = 0; i < glob->num_zones; ++i) {
407 zone = glob->zones[i];
408 if (zone->used_mem > zone->swap_limit) {
409 needs_swapping = true;
410 break;
411 }
412 }
413
ba4e7d97
TH
414 spin_unlock(&glob->lock);
415
416 if (unlikely(needs_swapping))
417 (void)queue_work(glob->swap_queue, &glob->work);
418
419}
420
5fd9cbad
TH
421static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
422 struct ttm_mem_zone *single_zone,
423 uint64_t amount)
ba4e7d97 424{
5fd9cbad
TH
425 unsigned int i;
426 struct ttm_mem_zone *zone;
427
ba4e7d97 428 spin_lock(&glob->lock);
5fd9cbad
TH
429 for (i = 0; i < glob->num_zones; ++i) {
430 zone = glob->zones[i];
431 if (single_zone && zone != single_zone)
432 continue;
433 zone->used_mem -= amount;
434 }
ba4e7d97
TH
435 spin_unlock(&glob->lock);
436}
437
5fd9cbad
TH
438void ttm_mem_global_free(struct ttm_mem_global *glob,
439 uint64_t amount)
440{
441 return ttm_mem_global_free_zone(glob, NULL, amount);
442}
443
ba4e7d97 444static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
5fd9cbad
TH
445 struct ttm_mem_zone *single_zone,
446 uint64_t amount, bool reserve)
ba4e7d97
TH
447{
448 uint64_t limit;
ba4e7d97 449 int ret = -ENOMEM;
5fd9cbad
TH
450 unsigned int i;
451 struct ttm_mem_zone *zone;
ba4e7d97
TH
452
453 spin_lock(&glob->lock);
5fd9cbad
TH
454 for (i = 0; i < glob->num_zones; ++i) {
455 zone = glob->zones[i];
456 if (single_zone && zone != single_zone)
457 continue;
ba4e7d97 458
5fd9cbad
TH
459 limit = (capable(CAP_SYS_ADMIN)) ?
460 zone->emer_mem : zone->max_mem;
ba4e7d97 461
5fd9cbad
TH
462 if (zone->used_mem > limit)
463 goto out_unlock;
464 }
ba4e7d97
TH
465
466 if (reserve) {
5fd9cbad
TH
467 for (i = 0; i < glob->num_zones; ++i) {
468 zone = glob->zones[i];
469 if (single_zone && zone != single_zone)
470 continue;
471 zone->used_mem += amount;
472 }
ba4e7d97 473 }
5fd9cbad 474
ba4e7d97
TH
475 ret = 0;
476out_unlock:
477 spin_unlock(&glob->lock);
478 ttm_check_swapping(glob);
479
480 return ret;
481}
482
5fd9cbad
TH
483
484static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
485 struct ttm_mem_zone *single_zone,
486 uint64_t memory,
487 bool no_wait, bool interruptible)
ba4e7d97
TH
488{
489 int count = TTM_MEMORY_ALLOC_RETRIES;
490
5fd9cbad
TH
491 while (unlikely(ttm_mem_global_reserve(glob,
492 single_zone,
493 memory, true)
ba4e7d97
TH
494 != 0)) {
495 if (no_wait)
496 return -ENOMEM;
497 if (unlikely(count-- == 0))
498 return -ENOMEM;
499 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
500 }
501
502 return 0;
503}
504
5fd9cbad
TH
505int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
506 bool no_wait, bool interruptible)
507{
508 /**
509 * Normal allocations of kernel memory are registered in
510 * all zones.
511 */
512
513 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
514 interruptible);
515}
516
517int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
518 struct page *page,
519 bool no_wait, bool interruptible)
520{
521
522 struct ttm_mem_zone *zone = NULL;
523
524 /**
525 * Page allocations may be registed in a single zone
526 * only if highmem or !dma32.
527 */
528
529#ifdef CONFIG_HIGHMEM
530 if (PageHighMem(page) && glob->zone_highmem != NULL)
531 zone = glob->zone_highmem;
532#else
533 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
534 zone = glob->zone_kernel;
535#endif
536 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
537 interruptible);
538}
539
540void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
541{
542 struct ttm_mem_zone *zone = NULL;
543
544#ifdef CONFIG_HIGHMEM
545 if (PageHighMem(page) && glob->zone_highmem != NULL)
546 zone = glob->zone_highmem;
547#else
548 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
549 zone = glob->zone_kernel;
550#endif
551 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
552}
553
554
ba4e7d97
TH
555size_t ttm_round_pot(size_t size)
556{
557 if ((size & (size - 1)) == 0)
558 return size;
559 else if (size > PAGE_SIZE)
560 return PAGE_ALIGN(size);
561 else {
562 size_t tmp_size = 4;
563
564 while (tmp_size < size)
565 tmp_size <<= 1;
566
567 return tmp_size;
568 }
569 return 0;
570}