1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3 *
4 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28 /*
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 */
31
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <drm/drm_mm.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/module.h>
39
40 /**
41 * Currently we use a spinlock for the lock, but a mutex *may* be
42 * more appropriate to reduce scheduling latency if the range manager
43 * ends up with very fragmented allocation patterns.
44 */
45
46 struct ttm_range_manager {
47 struct ttm_resource_manager manager;
48 struct drm_mm mm;
49 spinlock_t lock;
50 };
51
to_range_manager(struct ttm_resource_manager * man)52 static inline struct ttm_range_manager *to_range_manager(struct ttm_resource_manager *man)
53 {
54 return container_of(man, struct ttm_range_manager, manager);
55 }
56
ttm_range_man_alloc(struct ttm_resource_manager * man,struct ttm_buffer_object * bo,const struct ttm_place * place,struct ttm_resource * mem)57 static int ttm_range_man_alloc(struct ttm_resource_manager *man,
58 struct ttm_buffer_object *bo,
59 const struct ttm_place *place,
60 struct ttm_resource *mem)
61 {
62 struct ttm_range_manager *rman = to_range_manager(man);
63 struct drm_mm *mm = &rman->mm;
64 struct drm_mm_node *node;
65 enum drm_mm_insert_mode mode;
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 mode = DRM_MM_INSERT_BEST;
78 if (place->flags & TTM_PL_FLAG_TOPDOWN)
79 mode = DRM_MM_INSERT_HIGH;
80
81 spin_lock(&rman->lock);
82 ret = drm_mm_insert_node_in_range(mm, node,
83 mem->num_pages,
84 mem->page_alignment, 0,
85 place->fpfn, lpfn, mode);
86 spin_unlock(&rman->lock);
87
88 if (unlikely(ret)) {
89 kfree(node);
90 } else {
91 mem->mm_node = node;
92 mem->start = node->start;
93 }
94
95 return ret;
96 }
97
ttm_range_man_free(struct ttm_resource_manager * man,struct ttm_resource * mem)98 static void ttm_range_man_free(struct ttm_resource_manager *man,
99 struct ttm_resource *mem)
100 {
101 struct ttm_range_manager *rman = to_range_manager(man);
102
103 if (mem->mm_node) {
104 spin_lock(&rman->lock);
105 drm_mm_remove_node(mem->mm_node);
106 spin_unlock(&rman->lock);
107
108 kfree(mem->mm_node);
109 mem->mm_node = NULL;
110 }
111 }
112
113 static const struct ttm_resource_manager_func ttm_range_manager_func;
114
ttm_range_man_init(struct ttm_bo_device * bdev,unsigned type,bool use_tt,unsigned long p_size)115 int ttm_range_man_init(struct ttm_bo_device *bdev,
116 unsigned type, bool use_tt,
117 unsigned long p_size)
118 {
119 struct ttm_resource_manager *man;
120 struct ttm_range_manager *rman;
121
122 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
123 if (!rman)
124 return -ENOMEM;
125
126 man = &rman->manager;
127 man->use_tt = use_tt;
128
129 man->func = &ttm_range_manager_func;
130
131 ttm_resource_manager_init(man, p_size);
132
133 drm_mm_init(&rman->mm, 0, p_size);
134 spin_lock_init(&rman->lock);
135
136 ttm_set_driver_manager(bdev, type, &rman->manager);
137 ttm_resource_manager_set_used(man, true);
138 return 0;
139 }
140 EXPORT_SYMBOL(ttm_range_man_init);
141
ttm_range_man_fini(struct ttm_bo_device * bdev,unsigned type)142 int ttm_range_man_fini(struct ttm_bo_device *bdev,
143 unsigned type)
144 {
145 struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
146 struct ttm_range_manager *rman = to_range_manager(man);
147 struct drm_mm *mm = &rman->mm;
148 int ret;
149
150 if (!man)
151 return 0;
152
153 ttm_resource_manager_set_used(man, false);
154
155 ret = ttm_resource_manager_force_list_clean(bdev, man);
156 if (ret)
157 return ret;
158
159 spin_lock(&rman->lock);
160 drm_mm_clean(mm);
161 drm_mm_takedown(mm);
162 spin_unlock(&rman->lock);
163
164 ttm_resource_manager_cleanup(man);
165 ttm_set_driver_manager(bdev, type, NULL);
166 kfree(rman);
167 return 0;
168 }
169 EXPORT_SYMBOL(ttm_range_man_fini);
170
ttm_range_man_debug(struct ttm_resource_manager * man,struct drm_printer * printer)171 static void ttm_range_man_debug(struct ttm_resource_manager *man,
172 struct drm_printer *printer)
173 {
174 struct ttm_range_manager *rman = to_range_manager(man);
175
176 spin_lock(&rman->lock);
177 drm_mm_print(&rman->mm, printer);
178 spin_unlock(&rman->lock);
179 }
180
181 static const struct ttm_resource_manager_func ttm_range_manager_func = {
182 .alloc = ttm_range_man_alloc,
183 .free = ttm_range_man_free,
184 .debug = ttm_range_man_debug
185 };
186