ttm_bo_manager.c revision 1.3 1 /* $NetBSD: ttm_bo_manager.c,v 1.3 2018/08/27 04:58:37 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 /*
30 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ttm_bo_manager.c,v 1.3 2018/08/27 04:58:37 riastradh Exp $");
35
36 #include <drm/ttm/ttm_module.h>
37 #include <drm/ttm/ttm_bo_driver.h>
38 #include <drm/ttm/ttm_placement.h>
39 #include <drm/drm_mm.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/module.h>
43 #include <linux/export.h>
44
45 /**
46 * Currently we use a spinlock for the lock, but a mutex *may* be
47 * more appropriate to reduce scheduling latency if the range manager
48 * ends up with very fragmented allocation patterns.
49 */
50
51 struct ttm_range_manager {
52 struct drm_mm mm;
53 spinlock_t lock;
54 };
55
56 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
57 struct ttm_buffer_object *bo,
58 const struct ttm_place *place,
59 struct ttm_mem_reg *mem)
60 {
61 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
62 struct drm_mm *mm = &rman->mm;
63 struct drm_mm_node *node = NULL;
64 enum drm_mm_search_flags sflags = DRM_MM_SEARCH_BEST;
65 enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT;
66 unsigned long lpfn;
67 int ret;
68
69 lpfn = place->lpfn;
70 if (!lpfn)
71 lpfn = man->size;
72
73 node = kzalloc(sizeof(*node), GFP_KERNEL);
74 if (!node)
75 return -ENOMEM;
76
77 if (place->flags & TTM_PL_FLAG_TOPDOWN) {
78 sflags = DRM_MM_SEARCH_BELOW;
79 aflags = DRM_MM_CREATE_TOP;
80 }
81
82 spin_lock(&rman->lock);
83 ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages,
84 mem->page_alignment, 0,
85 place->fpfn, lpfn,
86 sflags, aflags);
87 spin_unlock(&rman->lock);
88
89 if (unlikely(ret)) {
90 kfree(node);
91 } else {
92 mem->mm_node = node;
93 mem->start = node->start;
94 }
95
96 return 0;
97 }
98
99 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
100 struct ttm_mem_reg *mem)
101 {
102 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
103
104 if (mem->mm_node) {
105 spin_lock(&rman->lock);
106 drm_mm_remove_node(mem->mm_node);
107 spin_unlock(&rman->lock);
108
109 kfree(mem->mm_node);
110 mem->mm_node = NULL;
111 }
112 }
113
114 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
115 unsigned long p_size)
116 {
117 struct ttm_range_manager *rman;
118
119 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
120 if (!rman)
121 return -ENOMEM;
122
123 drm_mm_init(&rman->mm, 0, p_size);
124 spin_lock_init(&rman->lock);
125 man->priv = rman;
126 return 0;
127 }
128
129 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
130 {
131 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
132 struct drm_mm *mm = &rman->mm;
133
134 spin_lock(&rman->lock);
135 if (drm_mm_clean(mm)) {
136 drm_mm_takedown(mm);
137 spin_unlock(&rman->lock);
138 kfree(rman);
139 man->priv = NULL;
140 return 0;
141 }
142 spin_unlock(&rman->lock);
143 return -EBUSY;
144 }
145
146 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
147 const char *prefix)
148 {
149 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
150
151 spin_lock(&rman->lock);
152 drm_mm_debug_table(&rman->mm, prefix);
153 spin_unlock(&rman->lock);
154 }
155
156 const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
157 ttm_bo_man_init,
158 ttm_bo_man_takedown,
159 ttm_bo_man_get_node,
160 ttm_bo_man_put_node,
161 ttm_bo_man_debug
162 };
163 EXPORT_SYMBOL(ttm_bo_manager_func);
164