GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / gpu / drm / ttm / ttm_memory.c
blobe70ddd82dc022e38c42b10bb4bce1b60ff511f04
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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 "ttm/ttm_memory.h"
29 #include "ttm/ttm_module.h"
30 #include "ttm/ttm_page_alloc.h"
31 #include <linux/spinlock.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
38 #define TTM_MEMORY_ALLOC_RETRIES 4
40 struct ttm_mem_zone {
41 struct kobject kobj;
42 struct ttm_mem_global *glob;
43 const char *name;
44 uint64_t zone_mem;
45 uint64_t emer_mem;
46 uint64_t max_mem;
47 uint64_t swap_limit;
48 uint64_t used_mem;
51 static struct attribute ttm_mem_sys = {
52 .name = "zone_memory",
53 .mode = S_IRUGO
55 static struct attribute ttm_mem_emer = {
56 .name = "emergency_memory",
57 .mode = S_IRUGO | S_IWUSR
59 static struct attribute ttm_mem_max = {
60 .name = "available_memory",
61 .mode = S_IRUGO | S_IWUSR
63 static struct attribute ttm_mem_swap = {
64 .name = "swap_limit",
65 .mode = S_IRUGO | S_IWUSR
67 static struct attribute ttm_mem_used = {
68 .name = "used_memory",
69 .mode = S_IRUGO
72 static void ttm_mem_zone_kobj_release(struct kobject *kobj)
74 struct ttm_mem_zone *zone =
75 container_of(kobj, struct ttm_mem_zone, kobj);
77 printk(KERN_INFO TTM_PFX
78 "Zone %7s: Used memory at exit: %llu kiB.\n",
79 zone->name, (unsigned long long) zone->used_mem >> 10);
80 kfree(zone);
83 static ssize_t ttm_mem_zone_show(struct kobject *kobj,
84 struct attribute *attr,
85 char *buffer)
87 struct ttm_mem_zone *zone =
88 container_of(kobj, struct ttm_mem_zone, kobj);
89 uint64_t val = 0;
91 spin_lock(&zone->glob->lock);
92 if (attr == &ttm_mem_sys)
93 val = zone->zone_mem;
94 else if (attr == &ttm_mem_emer)
95 val = zone->emer_mem;
96 else if (attr == &ttm_mem_max)
97 val = zone->max_mem;
98 else if (attr == &ttm_mem_swap)
99 val = zone->swap_limit;
100 else if (attr == &ttm_mem_used)
101 val = zone->used_mem;
102 spin_unlock(&zone->glob->lock);
104 return snprintf(buffer, PAGE_SIZE, "%llu\n",
105 (unsigned long long) val >> 10);
108 static void ttm_check_swapping(struct ttm_mem_global *glob);
110 static ssize_t ttm_mem_zone_store(struct kobject *kobj,
111 struct attribute *attr,
112 const char *buffer,
113 size_t size)
115 struct ttm_mem_zone *zone =
116 container_of(kobj, struct ttm_mem_zone, kobj);
117 int chars;
118 unsigned long val;
119 uint64_t val64;
121 chars = sscanf(buffer, "%lu", &val);
122 if (chars == 0)
123 return size;
125 val64 = val;
126 val64 <<= 10;
128 spin_lock(&zone->glob->lock);
129 if (val64 > zone->zone_mem)
130 val64 = zone->zone_mem;
131 if (attr == &ttm_mem_emer) {
132 zone->emer_mem = val64;
133 if (zone->max_mem > val64)
134 zone->max_mem = val64;
135 } else if (attr == &ttm_mem_max) {
136 zone->max_mem = val64;
137 if (zone->emer_mem < val64)
138 zone->emer_mem = val64;
139 } else if (attr == &ttm_mem_swap)
140 zone->swap_limit = val64;
141 spin_unlock(&zone->glob->lock);
143 ttm_check_swapping(zone->glob);
145 return size;
148 static struct attribute *ttm_mem_zone_attrs[] = {
149 &ttm_mem_sys,
150 &ttm_mem_emer,
151 &ttm_mem_max,
152 &ttm_mem_swap,
153 &ttm_mem_used,
154 NULL
157 static const struct sysfs_ops ttm_mem_zone_ops = {
158 .show = &ttm_mem_zone_show,
159 .store = &ttm_mem_zone_store
162 static struct kobj_type ttm_mem_zone_kobj_type = {
163 .release = &ttm_mem_zone_kobj_release,
164 .sysfs_ops = &ttm_mem_zone_ops,
165 .default_attrs = ttm_mem_zone_attrs,
168 static void ttm_mem_global_kobj_release(struct kobject *kobj)
170 struct ttm_mem_global *glob =
171 container_of(kobj, struct ttm_mem_global, kobj);
173 kfree(glob);
176 static struct kobj_type ttm_mem_glob_kobj_type = {
177 .release = &ttm_mem_global_kobj_release,
180 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
181 bool from_wq, uint64_t extra)
183 unsigned int i;
184 struct ttm_mem_zone *zone;
185 uint64_t target;
187 for (i = 0; i < glob->num_zones; ++i) {
188 zone = glob->zones[i];
190 if (from_wq)
191 target = zone->swap_limit;
192 else if (capable(CAP_SYS_ADMIN))
193 target = zone->emer_mem;
194 else
195 target = zone->max_mem;
197 target = (extra > target) ? 0ULL : target;
199 if (zone->used_mem > target)
200 return true;
202 return false;
206 * At this point we only support a single shrink callback.
207 * Extend this if needed, perhaps using a linked list of callbacks.
208 * Note that this function is reentrant:
209 * many threads may try to swap out at any given time.
212 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
213 uint64_t extra)
215 int ret;
216 struct ttm_mem_shrink *shrink;
218 spin_lock(&glob->lock);
219 if (glob->shrink == NULL)
220 goto out;
222 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
223 shrink = glob->shrink;
224 spin_unlock(&glob->lock);
225 ret = shrink->do_shrink(shrink);
226 spin_lock(&glob->lock);
227 if (unlikely(ret != 0))
228 goto out;
230 out:
231 spin_unlock(&glob->lock);
236 static void ttm_shrink_work(struct work_struct *work)
238 struct ttm_mem_global *glob =
239 container_of(work, struct ttm_mem_global, work);
241 ttm_shrink(glob, true, 0ULL);
244 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
245 const struct sysinfo *si)
247 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
248 uint64_t mem;
249 int ret;
251 if (unlikely(!zone))
252 return -ENOMEM;
254 mem = si->totalram - si->totalhigh;
255 mem *= si->mem_unit;
257 zone->name = "kernel";
258 zone->zone_mem = mem;
259 zone->max_mem = mem >> 1;
260 zone->emer_mem = (mem >> 1) + (mem >> 2);
261 zone->swap_limit = zone->max_mem - (mem >> 3);
262 zone->used_mem = 0;
263 zone->glob = glob;
264 glob->zone_kernel = zone;
265 ret = kobject_init_and_add(
266 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
267 if (unlikely(ret != 0)) {
268 kobject_put(&zone->kobj);
269 return ret;
271 glob->zones[glob->num_zones++] = zone;
272 return 0;
275 #ifdef CONFIG_HIGHMEM
276 static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
277 const struct sysinfo *si)
279 struct ttm_mem_zone *zone;
280 uint64_t mem;
281 int ret;
283 if (si->totalhigh == 0)
284 return 0;
286 zone = kzalloc(sizeof(*zone), GFP_KERNEL);
287 if (unlikely(!zone))
288 return -ENOMEM;
290 mem = si->totalram;
291 mem *= si->mem_unit;
293 zone->name = "highmem";
294 zone->zone_mem = mem;
295 zone->max_mem = mem >> 1;
296 zone->emer_mem = (mem >> 1) + (mem >> 2);
297 zone->swap_limit = zone->max_mem - (mem >> 3);
298 zone->used_mem = 0;
299 zone->glob = glob;
300 glob->zone_highmem = zone;
301 ret = kobject_init_and_add(
302 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
303 if (unlikely(ret != 0)) {
304 kobject_put(&zone->kobj);
305 return ret;
307 glob->zones[glob->num_zones++] = zone;
308 return 0;
310 #else
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);
315 uint64_t mem;
316 int ret;
318 if (unlikely(!zone))
319 return -ENOMEM;
321 mem = si->totalram;
322 mem *= si->mem_unit;
325 * No special dma32 zone needed.
328 if (mem <= ((uint64_t) 1ULL << 32)) {
329 kfree(zone);
330 return 0;
334 * Limit max dma32 memory to 4GB for now
335 * until we can figure out how big this
336 * zone really is.
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);
345 zone->used_mem = 0;
346 zone->glob = glob;
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);
352 return ret;
354 glob->zones[glob->num_zones++] = zone;
355 return 0;
357 #endif
359 int ttm_mem_global_init(struct ttm_mem_global *glob)
361 struct sysinfo si;
362 int ret;
363 int i;
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 init_waitqueue_head(&glob->queue);
370 ret = kobject_init_and_add(
371 &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
372 if (unlikely(ret != 0)) {
373 kobject_put(&glob->kobj);
374 return ret;
377 si_meminfo(&si);
379 ret = ttm_mem_init_kernel_zone(glob, &si);
380 if (unlikely(ret != 0))
381 goto out_no_zone;
382 #ifdef CONFIG_HIGHMEM
383 ret = ttm_mem_init_highmem_zone(glob, &si);
384 if (unlikely(ret != 0))
385 goto out_no_zone;
386 #else
387 ret = ttm_mem_init_dma32_zone(glob, &si);
388 if (unlikely(ret != 0))
389 goto out_no_zone;
390 #endif
391 for (i = 0; i < glob->num_zones; ++i) {
392 zone = glob->zones[i];
393 printk(KERN_INFO TTM_PFX
394 "Zone %7s: Available graphics memory: %llu kiB.\n",
395 zone->name, (unsigned long long) zone->max_mem >> 10);
397 ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
398 return 0;
399 out_no_zone:
400 ttm_mem_global_release(glob);
401 return ret;
403 EXPORT_SYMBOL(ttm_mem_global_init);
405 void ttm_mem_global_release(struct ttm_mem_global *glob)
407 unsigned int i;
408 struct ttm_mem_zone *zone;
410 /* let the page allocator first stop the shrink work. */
411 ttm_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;
429 unsigned int i;
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;
437 break;
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,
450 uint64_t amount)
452 unsigned int i;
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)
459 continue;
460 zone->used_mem -= amount;
462 spin_unlock(&glob->lock);
465 void ttm_mem_global_free(struct ttm_mem_global *glob,
466 uint64_t amount)
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)
476 uint64_t limit;
477 int ret = -ENOMEM;
478 unsigned int i;
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)
485 continue;
487 limit = (capable(CAP_SYS_ADMIN)) ?
488 zone->emer_mem : zone->max_mem;
490 if (zone->used_mem > limit)
491 goto out_unlock;
494 if (reserve) {
495 for (i = 0; i < glob->num_zones; ++i) {
496 zone = glob->zones[i];
497 if (single_zone && zone != single_zone)
498 continue;
499 zone->used_mem += amount;
503 ret = 0;
504 out_unlock:
505 spin_unlock(&glob->lock);
506 ttm_check_swapping(glob);
508 return ret;
512 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
513 struct ttm_mem_zone *single_zone,
514 uint64_t memory,
515 bool no_wait, bool interruptible)
517 int count = TTM_MEMORY_ALLOC_RETRIES;
519 while (unlikely(ttm_mem_global_reserve(glob,
520 single_zone,
521 memory, true)
522 != 0)) {
523 if (no_wait)
524 return -ENOMEM;
525 if (unlikely(count-- == 0))
526 return -ENOMEM;
527 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
530 return 0;
533 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
534 bool no_wait, bool interruptible)
537 * Normal allocations of kernel memory are registered in
538 * all zones.
541 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
542 interruptible);
544 EXPORT_SYMBOL(ttm_mem_global_alloc);
546 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
547 struct page *page,
548 bool no_wait, bool interruptible)
551 struct ttm_mem_zone *zone = NULL;
554 * Page allocations may be registed in a single zone
555 * only if highmem or !dma32.
558 #ifdef CONFIG_HIGHMEM
559 if (PageHighMem(page) && glob->zone_highmem != NULL)
560 zone = glob->zone_highmem;
561 #else
562 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
563 zone = glob->zone_kernel;
564 #endif
565 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
566 interruptible);
569 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
571 struct ttm_mem_zone *zone = NULL;
573 #ifdef CONFIG_HIGHMEM
574 if (PageHighMem(page) && glob->zone_highmem != NULL)
575 zone = glob->zone_highmem;
576 #else
577 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
578 zone = glob->zone_kernel;
579 #endif
580 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
584 size_t ttm_round_pot(size_t size)
586 if ((size & (size - 1)) == 0)
587 return size;
588 else if (size > PAGE_SIZE)
589 return PAGE_ALIGN(size);
590 else {
591 size_t tmp_size = 4;
593 while (tmp_size < size)
594 tmp_size <<= 1;
596 return tmp_size;
598 return 0;
600 EXPORT_SYMBOL(ttm_round_pot);