]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c
Some messing with error codes to return 0 on out id's and match
[karo-tx-linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_gmrid_manager.c
1 /**************************************************************************
2  *
3  * Copyright (c) 2007-2010 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  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30
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>
38
39 struct vmwgfx_gmrid_man {
40         struct ida gmr_ida;
41         uint32_t max_gmr_ids;
42 };
43
44 static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
45                                   struct ttm_buffer_object *bo,
46                                   struct ttm_placement *placement,
47                                   struct ttm_mem_reg *mem)
48 {
49         struct vmwgfx_gmrid_man *gman =
50                 (struct vmwgfx_gmrid_man *)man->priv;
51         int id;
52
53         mem->mm_node = NULL;
54
55         id = ida_simple_get(&gman->gmr_ida, 0, gman->max_gmr_ids, GFP_KERNEL);
56         if (id < 0) {
57                 if (id == -ENOSPC)
58                         return 0;
59                 return id;
60         }
61         mem->mm_node = gman;
62         mem->start = id;
63
64         return 0;
65 }
66
67 static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
68                                    struct ttm_mem_reg *mem)
69 {
70         struct vmwgfx_gmrid_man *gman =
71                 (struct vmwgfx_gmrid_man *)man->priv;
72
73         if (mem->mm_node) {
74                 ida_simple_remove(&gman->gmr_ida, mem->start);
75                 mem->mm_node = NULL;
76         }
77 }
78
79 static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
80                               unsigned long p_size)
81 {
82         struct vmwgfx_gmrid_man *gman =
83                 kzalloc(sizeof(*gman), GFP_KERNEL);
84
85         if (unlikely(gman == NULL))
86                 return -ENOMEM;
87
88         ida_init(&gman->gmr_ida);
89         gman->max_gmr_ids = p_size;
90         man->priv = (void *) gman;
91         return 0;
92 }
93
94 static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
95 {
96         struct vmwgfx_gmrid_man *gman =
97                 (struct vmwgfx_gmrid_man *)man->priv;
98
99         if (gman) {
100                 ida_destroy(&gman->gmr_ida);
101                 kfree(gman);
102         }
103         return 0;
104 }
105
106 static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
107                                 const char *prefix)
108 {
109         printk(KERN_INFO "%s: No debug info available for the GMR "
110                "id manager.\n", prefix);
111 }
112
113 const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
114         vmw_gmrid_man_init,
115         vmw_gmrid_man_takedown,
116         vmw_gmrid_man_get_node,
117         vmw_gmrid_man_put_node,
118         vmw_gmrid_man_debug
119 };