vmwgfx: Implement a proper GMR eviction mechanism
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / vmwgfx / vmwgfx_gmrid_manager.c
blobac6e0d1bd629c17b57a1dce297c5a04e67f1f402
1 /**************************************************************************
3 * Copyright (c) 2007-2010 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 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "vmwgfx_drv.h"
32 #include "ttm/ttm_module.h"
33 #include "ttm/ttm_bo_driver.h"
34 #include "ttm/ttm_placement.h"
35 #include <linux/idr.h>
36 #include <linux/spinlock.h>
37 #include <linux/kernel.h>
39 struct vmwgfx_gmrid_man {
40 spinlock_t lock;
41 struct ida gmr_ida;
42 uint32_t max_gmr_ids;
45 static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
46 struct ttm_buffer_object *bo,
47 struct ttm_placement *placement,
48 struct ttm_mem_reg *mem)
50 struct vmwgfx_gmrid_man *gman =
51 (struct vmwgfx_gmrid_man *)man->priv;
52 int ret;
53 int id;
55 mem->mm_node = NULL;
57 do {
58 if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0))
59 return -ENOMEM;
61 spin_lock(&gman->lock);
62 ret = ida_get_new(&gman->gmr_ida, &id);
64 if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) {
65 ida_remove(&gman->gmr_ida, id);
66 spin_unlock(&gman->lock);
67 return 0;
70 spin_unlock(&gman->lock);
72 } while (ret == -EAGAIN);
74 if (likely(ret == 0)) {
75 mem->mm_node = gman;
76 mem->start = id;
79 return ret;
82 static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
83 struct ttm_mem_reg *mem)
85 struct vmwgfx_gmrid_man *gman =
86 (struct vmwgfx_gmrid_man *)man->priv;
88 if (mem->mm_node) {
89 spin_lock(&gman->lock);
90 ida_remove(&gman->gmr_ida, mem->start);
91 spin_unlock(&gman->lock);
92 mem->mm_node = NULL;
96 static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
97 unsigned long p_size)
99 struct vmwgfx_gmrid_man *gman =
100 kzalloc(sizeof(*gman), GFP_KERNEL);
102 if (unlikely(gman == NULL))
103 return -ENOMEM;
105 spin_lock_init(&gman->lock);
106 ida_init(&gman->gmr_ida);
107 gman->max_gmr_ids = p_size;
108 man->priv = (void *) gman;
109 return 0;
112 static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
114 struct vmwgfx_gmrid_man *gman =
115 (struct vmwgfx_gmrid_man *)man->priv;
117 if (gman) {
118 ida_destroy(&gman->gmr_ida);
119 kfree(gman);
121 return 0;
124 static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
125 const char *prefix)
127 printk(KERN_INFO "%s: No debug info available for the GMR "
128 "id manager.\n", prefix);
131 const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
132 vmw_gmrid_man_init,
133 vmw_gmrid_man_takedown,
134 vmw_gmrid_man_get_node,
135 vmw_gmrid_man_put_node,
136 vmw_gmrid_man_debug