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