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#include "vmwgfx_drv.h"
29#include "vmwgfx_resource_priv.h"
30
31#define VMW_CMDBUF_RES_MAN_HT_ORDER 12
32
33
34
35
36
37
38
39
40
41
42
43struct vmw_cmdbuf_res {
44 struct vmw_resource *res;
45 struct drm_hash_item hash;
46 struct list_head head;
47 enum vmw_cmdbuf_res_state state;
48 struct vmw_cmdbuf_res_manager *man;
49};
50
51
52
53
54
55
56
57
58
59
60
61struct vmw_cmdbuf_res_manager {
62 struct drm_open_hash resources;
63 struct list_head list;
64 struct vmw_private *dev_priv;
65};
66
67
68
69
70
71
72
73
74
75
76
77
78
79struct vmw_resource *
80vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
81 enum vmw_cmdbuf_res_type res_type,
82 u32 user_key)
83{
84 struct drm_hash_item *hash;
85 int ret;
86 unsigned long key = user_key | (res_type << 24);
87
88 ret = drm_ht_find_item(&man->resources, key, &hash);
89 if (unlikely(ret != 0))
90 return ERR_PTR(ret);
91
92 return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
93}
94
95
96
97
98
99
100
101
102
103
104static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
105 struct vmw_cmdbuf_res *entry)
106{
107 list_del(&entry->head);
108 WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
109 vmw_resource_unreference(&entry->res);
110 kfree(entry);
111}
112
113
114
115
116
117
118
119
120
121
122
123void vmw_cmdbuf_res_commit(struct list_head *list)
124{
125 struct vmw_cmdbuf_res *entry, *next;
126
127 list_for_each_entry_safe(entry, next, list, head) {
128 list_del(&entry->head);
129 if (entry->res->func->commit_notify)
130 entry->res->func->commit_notify(entry->res,
131 entry->state);
132 switch (entry->state) {
133 case VMW_CMDBUF_RES_ADD:
134 entry->state = VMW_CMDBUF_RES_COMMITTED;
135 list_add_tail(&entry->head, &entry->man->list);
136 break;
137 case VMW_CMDBUF_RES_DEL:
138 vmw_resource_unreference(&entry->res);
139 kfree(entry);
140 break;
141 default:
142 BUG();
143 break;
144 }
145 }
146}
147
148
149
150
151
152
153
154
155
156
157
158
159
160void vmw_cmdbuf_res_revert(struct list_head *list)
161{
162 struct vmw_cmdbuf_res *entry, *next;
163 int ret;
164
165 list_for_each_entry_safe(entry, next, list, head) {
166 switch (entry->state) {
167 case VMW_CMDBUF_RES_ADD:
168 vmw_cmdbuf_res_free(entry->man, entry);
169 break;
170 case VMW_CMDBUF_RES_DEL:
171 ret = drm_ht_insert_item(&entry->man->resources,
172 &entry->hash);
173 list_del(&entry->head);
174 list_add_tail(&entry->head, &entry->man->list);
175 entry->state = VMW_CMDBUF_RES_COMMITTED;
176 break;
177 default:
178 BUG();
179 break;
180 }
181 }
182}
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
198 enum vmw_cmdbuf_res_type res_type,
199 u32 user_key,
200 struct vmw_resource *res,
201 struct list_head *list)
202{
203 struct vmw_cmdbuf_res *cres;
204 int ret;
205
206 cres = kzalloc(sizeof(*cres), GFP_KERNEL);
207 if (unlikely(!cres))
208 return -ENOMEM;
209
210 cres->hash.key = user_key | (res_type << 24);
211 ret = drm_ht_insert_item(&man->resources, &cres->hash);
212 if (unlikely(ret != 0)) {
213 kfree(cres);
214 goto out_invalid_key;
215 }
216
217 cres->state = VMW_CMDBUF_RES_ADD;
218 cres->res = vmw_resource_reference(res);
219 cres->man = man;
220 list_add_tail(&cres->head, list);
221
222out_invalid_key:
223 return ret;
224}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
243 enum vmw_cmdbuf_res_type res_type,
244 u32 user_key,
245 struct list_head *list,
246 struct vmw_resource **res_p)
247{
248 struct vmw_cmdbuf_res *entry;
249 struct drm_hash_item *hash;
250 int ret;
251
252 ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
253 &hash);
254 if (likely(ret != 0))
255 return -EINVAL;
256
257 entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
258
259 switch (entry->state) {
260 case VMW_CMDBUF_RES_ADD:
261 vmw_cmdbuf_res_free(man, entry);
262 *res_p = NULL;
263 break;
264 case VMW_CMDBUF_RES_COMMITTED:
265 (void) drm_ht_remove_item(&man->resources, &entry->hash);
266 list_del(&entry->head);
267 entry->state = VMW_CMDBUF_RES_DEL;
268 list_add_tail(&entry->head, list);
269 *res_p = entry->res;
270 break;
271 default:
272 BUG();
273 break;
274 }
275
276 return 0;
277}
278
279
280
281
282
283
284
285
286
287
288struct vmw_cmdbuf_res_manager *
289vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
290{
291 struct vmw_cmdbuf_res_manager *man;
292 int ret;
293
294 man = kzalloc(sizeof(*man), GFP_KERNEL);
295 if (!man)
296 return ERR_PTR(-ENOMEM);
297
298 man->dev_priv = dev_priv;
299 INIT_LIST_HEAD(&man->list);
300 ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
301 if (ret == 0)
302 return man;
303
304 kfree(man);
305 return ERR_PTR(ret);
306}
307
308
309
310
311
312
313
314
315
316
317
318void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
319{
320 struct vmw_cmdbuf_res *entry, *next;
321
322 list_for_each_entry_safe(entry, next, &man->list, head)
323 vmw_cmdbuf_res_free(man, entry);
324
325 drm_ht_remove(&man->resources);
326 kfree(man);
327}
328
329
330
331
332
333
334
335
336
337size_t vmw_cmdbuf_res_man_size(void)
338{
339 static size_t res_man_size;
340
341 if (unlikely(res_man_size == 0))
342 res_man_size =
343 ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
344 ttm_round_pot(sizeof(struct hlist_head) <<
345 VMW_CMDBUF_RES_MAN_HT_ORDER);
346
347 return res_man_size;
348}
349