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