vmwgfx_cmdbuf_res.c revision 1.2.6.2 1 /* $NetBSD: vmwgfx_cmdbuf_res.c,v 1.2.6.2 2019/06/10 22:08:28 christos Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright 2014-2015 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 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_cmdbuf_res.c,v 1.2.6.2 2019/06/10 22:08:28 christos Exp $");
32
33 #include "vmwgfx_drv.h"
34 #include "vmwgfx_resource_priv.h"
35
36 #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
37
38 /**
39 * struct vmw_cmdbuf_res - Command buffer managed resource entry.
40 *
41 * @res: Refcounted pointer to a struct vmw_resource.
42 * @hash: Hash entry for the manager hash table.
43 * @head: List head used either by the staging list or the manager list
44 * of commited resources.
45 * @state: Staging state of this resource entry.
46 * @man: Pointer to a resource manager for this entry.
47 */
48 struct vmw_cmdbuf_res {
49 struct vmw_resource *res;
50 struct drm_hash_item hash;
51 struct list_head head;
52 enum vmw_cmdbuf_res_state state;
53 struct vmw_cmdbuf_res_manager *man;
54 };
55
56 /**
57 * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
58 *
59 * @resources: Hash table containing staged and commited command buffer
60 * resources
61 * @list: List of commited command buffer resources.
62 * @dev_priv: Pointer to a device private structure.
63 *
64 * @resources and @list are protected by the cmdbuf mutex for now.
65 */
66 struct vmw_cmdbuf_res_manager {
67 struct drm_open_hash resources;
68 struct list_head list;
69 struct vmw_private *dev_priv;
70 };
71
72
73 /**
74 * vmw_cmdbuf_res_lookup - Look up a command buffer resource
75 *
76 * @man: Pointer to the command buffer resource manager
77 * @resource_type: The resource type, that combined with the user key
78 * identifies the resource.
79 * @user_key: The user key.
80 *
81 * Returns a valid refcounted struct vmw_resource pointer on success,
82 * an error pointer on failure.
83 */
84 struct vmw_resource *
85 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
86 enum vmw_cmdbuf_res_type res_type,
87 u32 user_key)
88 {
89 struct drm_hash_item *hash;
90 int ret;
91 unsigned long key = user_key | (res_type << 24);
92
93 ret = drm_ht_find_item(&man->resources, key, &hash);
94 if (unlikely(ret != 0))
95 return ERR_PTR(ret);
96
97 return vmw_resource_reference
98 (drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res);
99 }
100
101 /**
102 * vmw_cmdbuf_res_free - Free a command buffer resource.
103 *
104 * @man: Pointer to the command buffer resource manager
105 * @entry: Pointer to a struct vmw_cmdbuf_res.
106 *
107 * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
108 * struct vmw_resource.
109 */
110 static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
111 struct vmw_cmdbuf_res *entry)
112 {
113 list_del(&entry->head);
114 WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
115 vmw_resource_unreference(&entry->res);
116 kfree(entry);
117 }
118
119 /**
120 * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
121 *
122 * @list: Caller's list of command buffer resource actions.
123 *
124 * This function commits a list of command buffer resource
125 * additions or removals.
126 * It is typically called when the execbuf ioctl call triggering these
127 * actions has commited the fifo contents to the device.
128 */
129 void vmw_cmdbuf_res_commit(struct list_head *list)
130 {
131 struct vmw_cmdbuf_res *entry, *next;
132
133 list_for_each_entry_safe(entry, next, list, head) {
134 list_del(&entry->head);
135 if (entry->res->func->commit_notify)
136 entry->res->func->commit_notify(entry->res,
137 entry->state);
138 switch (entry->state) {
139 case VMW_CMDBUF_RES_ADD:
140 entry->state = VMW_CMDBUF_RES_COMMITTED;
141 list_add_tail(&entry->head, &entry->man->list);
142 break;
143 case VMW_CMDBUF_RES_DEL:
144 vmw_resource_unreference(&entry->res);
145 kfree(entry);
146 break;
147 default:
148 BUG();
149 break;
150 }
151 }
152 }
153
154 /**
155 * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
156 *
157 * @man: Pointer to the command buffer resource manager
158 * @list: Caller's list of command buffer resource action
159 *
160 * This function reverts a list of command buffer resource
161 * additions or removals.
162 * It is typically called when the execbuf ioctl call triggering these
163 * actions failed for some reason, and the command stream was never
164 * submitted.
165 */
166 void vmw_cmdbuf_res_revert(struct list_head *list)
167 {
168 struct vmw_cmdbuf_res *entry, *next;
169 int ret;
170
171 list_for_each_entry_safe(entry, next, list, head) {
172 switch (entry->state) {
173 case VMW_CMDBUF_RES_ADD:
174 vmw_cmdbuf_res_free(entry->man, entry);
175 break;
176 case VMW_CMDBUF_RES_DEL:
177 ret = drm_ht_insert_item(&entry->man->resources,
178 &entry->hash);
179 list_del(&entry->head);
180 list_add_tail(&entry->head, &entry->man->list);
181 entry->state = VMW_CMDBUF_RES_COMMITTED;
182 break;
183 default:
184 BUG();
185 break;
186 }
187 }
188 }
189
190 /**
191 * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
192 *
193 * @man: Pointer to the command buffer resource manager.
194 * @res_type: The resource type.
195 * @user_key: The user-space id of the resource.
196 * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
197 * @list: The staging list.
198 *
199 * This function allocates a struct vmw_cmdbuf_res entry and adds the
200 * resource to the hash table of the manager identified by @man. The
201 * entry is then put on the staging list identified by @list.
202 */
203 int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
204 enum vmw_cmdbuf_res_type res_type,
205 u32 user_key,
206 struct vmw_resource *res,
207 struct list_head *list)
208 {
209 struct vmw_cmdbuf_res *cres;
210 int ret;
211
212 cres = kzalloc(sizeof(*cres), GFP_KERNEL);
213 if (unlikely(cres == NULL))
214 return -ENOMEM;
215
216 cres->hash.key = user_key | (res_type << 24);
217 ret = drm_ht_insert_item(&man->resources, &cres->hash);
218 if (unlikely(ret != 0))
219 goto out_invalid_key;
220
221 cres->state = VMW_CMDBUF_RES_ADD;
222 cres->res = vmw_resource_reference(res);
223 cres->man = man;
224 list_add_tail(&cres->head, list);
225
226 out_invalid_key:
227 return ret;
228 }
229
230 /**
231 * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
232 *
233 * @man: Pointer to the command buffer resource manager.
234 * @res_type: The resource type.
235 * @user_key: The user-space id of the resource.
236 * @list: The staging list.
237 * @res_p: If the resource is in an already committed state, points to the
238 * struct vmw_resource on successful return. The pointer will be
239 * non ref-counted.
240 *
241 * This function looks up the struct vmw_cmdbuf_res entry from the manager
242 * hash table and, if it exists, removes it. Depending on its current staging
243 * state it then either removes the entry from the staging list or adds it
244 * to it with a staging state of removal.
245 */
246 int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
247 enum vmw_cmdbuf_res_type res_type,
248 u32 user_key,
249 struct list_head *list,
250 struct vmw_resource **res_p)
251 {
252 struct vmw_cmdbuf_res *entry;
253 struct drm_hash_item *hash;
254 int ret;
255
256 ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
257 &hash);
258 if (likely(ret != 0))
259 return -EINVAL;
260
261 entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
262
263 switch (entry->state) {
264 case VMW_CMDBUF_RES_ADD:
265 vmw_cmdbuf_res_free(man, entry);
266 *res_p = NULL;
267 break;
268 case VMW_CMDBUF_RES_COMMITTED:
269 (void) drm_ht_remove_item(&man->resources, &entry->hash);
270 list_del(&entry->head);
271 entry->state = VMW_CMDBUF_RES_DEL;
272 list_add_tail(&entry->head, list);
273 *res_p = entry->res;
274 break;
275 default:
276 BUG();
277 break;
278 }
279
280 return 0;
281 }
282
283 /**
284 * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
285 * manager.
286 *
287 * @dev_priv: Pointer to a struct vmw_private
288 *
289 * Allocates and initializes a command buffer managed resource manager. Returns
290 * an error pointer on failure.
291 */
292 struct vmw_cmdbuf_res_manager *
293 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
294 {
295 struct vmw_cmdbuf_res_manager *man;
296 int ret;
297
298 man = kzalloc(sizeof(*man), GFP_KERNEL);
299 if (man == NULL)
300 return ERR_PTR(-ENOMEM);
301
302 man->dev_priv = dev_priv;
303 INIT_LIST_HEAD(&man->list);
304 ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
305 if (ret == 0)
306 return man;
307
308 kfree(man);
309 return ERR_PTR(ret);
310 }
311
312 /**
313 * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
314 * manager.
315 *
316 * @man: Pointer to the manager to destroy.
317 *
318 * This function destroys a command buffer managed resource manager and
319 * unreferences / frees all command buffer managed resources and -entries
320 * associated with it.
321 */
322 void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
323 {
324 struct vmw_cmdbuf_res *entry, *next;
325
326 list_for_each_entry_safe(entry, next, &man->list, head)
327 vmw_cmdbuf_res_free(man, entry);
328
329 drm_ht_remove(&man->resources);
330 kfree(man);
331 }
332
333 /**
334 *
335 * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
336 * resource manager
337 *
338 * Returns the approximate allocation size of a command buffer managed
339 * resource manager.
340 */
341 size_t vmw_cmdbuf_res_man_size(void)
342 {
343 static size_t res_man_size;
344
345 if (unlikely(res_man_size == 0))
346 res_man_size =
347 ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
348 ttm_round_pot(sizeof(struct hlist_head) <<
349 VMW_CMDBUF_RES_MAN_HT_ORDER);
350
351 return res_man_size;
352 }
353