1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
42
43
44
45
46struct ttm_range_manager {
47 struct ttm_resource_manager manager;
48 struct drm_mm mm;
49 spinlock_t lock;
50};
51
52static 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
57static 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
98static 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
113static const struct ttm_resource_manager_func ttm_range_manager_func;
114
115int 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}
140EXPORT_SYMBOL(ttm_range_man_init);
141
142int 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 ttm_resource_manager_set_used(man, false);
151
152 ret = ttm_resource_manager_force_list_clean(bdev, man);
153 if (ret)
154 return ret;
155
156 spin_lock(&rman->lock);
157 drm_mm_clean(mm);
158 drm_mm_takedown(mm);
159 spin_unlock(&rman->lock);
160
161 ttm_resource_manager_cleanup(man);
162 ttm_set_driver_manager(bdev, type, NULL);
163 kfree(rman);
164 return 0;
165}
166EXPORT_SYMBOL(ttm_range_man_fini);
167
168static void ttm_range_man_debug(struct ttm_resource_manager *man,
169 struct drm_printer *printer)
170{
171 struct ttm_range_manager *rman = to_range_manager(man);
172
173 spin_lock(&rman->lock);
174 drm_mm_print(&rman->mm, printer);
175 spin_unlock(&rman->lock);
176}
177
178static const struct ttm_resource_manager_func ttm_range_manager_func = {
179 .alloc = ttm_range_man_alloc,
180 .free = ttm_range_man_free,
181 .debug = ttm_range_man_debug
182};
183