Merge tag 'upstream-3.14-rc5' of git://git.infradead.org/linux-ubifs
[deliverable/linux.git] / drivers / gpu / drm / ttm / ttm_bo_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 <drm/ttm/ttm_module.h>
32 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/drm_mm.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/module.h>
38
39 /**
40 * Currently we use a spinlock for the lock, but a mutex *may* be
41 * more appropriate to reduce scheduling latency if the range manager
42 * ends up with very fragmented allocation patterns.
43 */
44
45 struct ttm_range_manager {
46 struct drm_mm mm;
47 spinlock_t lock;
48 };
49
50 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
51 struct ttm_buffer_object *bo,
52 struct ttm_placement *placement,
53 struct ttm_mem_reg *mem)
54 {
55 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
56 struct drm_mm *mm = &rman->mm;
57 struct drm_mm_node *node = NULL;
58 unsigned long lpfn;
59 int ret;
60
61 lpfn = placement->lpfn;
62 if (!lpfn)
63 lpfn = man->size;
64
65 node = kzalloc(sizeof(*node), GFP_KERNEL);
66 if (!node)
67 return -ENOMEM;
68
69 spin_lock(&rman->lock);
70 ret = drm_mm_insert_node_in_range(mm, node, mem->num_pages,
71 mem->page_alignment,
72 placement->fpfn, lpfn,
73 DRM_MM_SEARCH_BEST);
74 spin_unlock(&rman->lock);
75
76 if (unlikely(ret)) {
77 kfree(node);
78 } else {
79 mem->mm_node = node;
80 mem->start = node->start;
81 }
82
83 return 0;
84 }
85
86 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
87 struct ttm_mem_reg *mem)
88 {
89 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
90
91 if (mem->mm_node) {
92 spin_lock(&rman->lock);
93 drm_mm_remove_node(mem->mm_node);
94 spin_unlock(&rman->lock);
95
96 kfree(mem->mm_node);
97 mem->mm_node = NULL;
98 }
99 }
100
101 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
102 unsigned long p_size)
103 {
104 struct ttm_range_manager *rman;
105
106 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
107 if (!rman)
108 return -ENOMEM;
109
110 drm_mm_init(&rman->mm, 0, p_size);
111 spin_lock_init(&rman->lock);
112 man->priv = rman;
113 return 0;
114 }
115
116 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
117 {
118 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
119 struct drm_mm *mm = &rman->mm;
120
121 spin_lock(&rman->lock);
122 if (drm_mm_clean(mm)) {
123 drm_mm_takedown(mm);
124 spin_unlock(&rman->lock);
125 kfree(rman);
126 man->priv = NULL;
127 return 0;
128 }
129 spin_unlock(&rman->lock);
130 return -EBUSY;
131 }
132
133 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
134 const char *prefix)
135 {
136 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
137
138 spin_lock(&rman->lock);
139 drm_mm_debug_table(&rman->mm, prefix);
140 spin_unlock(&rman->lock);
141 }
142
143 const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
144 ttm_bo_man_init,
145 ttm_bo_man_takedown,
146 ttm_bo_man_get_node,
147 ttm_bo_man_put_node,
148 ttm_bo_man_debug
149 };
150 EXPORT_SYMBOL(ttm_bo_manager_func);
This page took 0.039882 seconds and 5 git commands to generate.