1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_resource_priv.h"
31 #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
34 * struct vmw_cmdbuf_res - Command buffer managed resource entry.
36 * @res: Refcounted pointer to a struct vmw_resource.
37 * @hash: Hash entry for the manager hash table.
38 * @head: List head used either by the staging list or the manager list
39 * of commited resources.
40 * @state: Staging state of this resource entry.
41 * @man: Pointer to a resource manager for this entry.
43 struct 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;
52 * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
54 * @resources: Hash table containing staged and commited command buffer
56 * @list: List of commited command buffer resources.
57 * @dev_priv: Pointer to a device private structure.
59 * @resources and @list are protected by the cmdbuf mutex for now.
61 struct vmw_cmdbuf_res_manager {
62 struct drm_open_hash resources;
63 struct list_head list;
64 struct vmw_private *dev_priv;
69 * vmw_cmdbuf_res_lookup - Look up a command buffer resource
71 * @man: Pointer to the command buffer resource manager
72 * @res_type: The resource type, that combined with the user key
73 * identifies the resource.
74 * @user_key: The user key.
76 * Returns a valid refcounted struct vmw_resource pointer on success,
77 * an error pointer on failure.
80 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
81 enum vmw_cmdbuf_res_type res_type,
84 struct drm_hash_item *hash;
86 unsigned long key = user_key | (res_type << 24);
88 ret = drm_ht_find_item(&man->resources, key, &hash);
89 if (unlikely(ret != 0))
92 return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
96 * vmw_cmdbuf_res_free - Free a command buffer resource.
98 * @man: Pointer to the command buffer resource manager
99 * @entry: Pointer to a struct vmw_cmdbuf_res.
101 * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
102 * struct vmw_resource.
104 static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
105 struct vmw_cmdbuf_res *entry)
107 list_del(&entry->head);
108 WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
109 vmw_resource_unreference(&entry->res);
114 * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
116 * @list: Caller's list of command buffer resource actions.
118 * This function commits a list of command buffer resource
119 * additions or removals.
120 * It is typically called when the execbuf ioctl call triggering these
121 * actions has commited the fifo contents to the device.
123 void vmw_cmdbuf_res_commit(struct list_head *list)
125 struct vmw_cmdbuf_res *entry, *next;
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,
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);
137 case VMW_CMDBUF_RES_DEL:
138 vmw_resource_unreference(&entry->res);
149 * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
151 * @list: Caller's list of command buffer resource action
153 * This function reverts a list of command buffer resource
154 * additions or removals.
155 * It is typically called when the execbuf ioctl call triggering these
156 * actions failed for some reason, and the command stream was never
159 void vmw_cmdbuf_res_revert(struct list_head *list)
161 struct vmw_cmdbuf_res *entry, *next;
163 list_for_each_entry_safe(entry, next, list, head) {
164 switch (entry->state) {
165 case VMW_CMDBUF_RES_ADD:
166 vmw_cmdbuf_res_free(entry->man, entry);
168 case VMW_CMDBUF_RES_DEL:
169 drm_ht_insert_item(&entry->man->resources, &entry->hash);
170 list_del(&entry->head);
171 list_add_tail(&entry->head, &entry->man->list);
172 entry->state = VMW_CMDBUF_RES_COMMITTED;
182 * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
184 * @man: Pointer to the command buffer resource manager.
185 * @res_type: The resource type.
186 * @user_key: The user-space id of the resource.
187 * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
188 * @list: The staging list.
190 * This function allocates a struct vmw_cmdbuf_res entry and adds the
191 * resource to the hash table of the manager identified by @man. The
192 * entry is then put on the staging list identified by @list.
194 int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
195 enum vmw_cmdbuf_res_type res_type,
197 struct vmw_resource *res,
198 struct list_head *list)
200 struct vmw_cmdbuf_res *cres;
203 cres = kzalloc(sizeof(*cres), GFP_KERNEL);
207 cres->hash.key = user_key | (res_type << 24);
208 ret = drm_ht_insert_item(&man->resources, &cres->hash);
209 if (unlikely(ret != 0)) {
211 goto out_invalid_key;
214 cres->state = VMW_CMDBUF_RES_ADD;
215 cres->res = vmw_resource_reference(res);
217 list_add_tail(&cres->head, list);
224 * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
226 * @man: Pointer to the command buffer resource manager.
227 * @res_type: The resource type.
228 * @user_key: The user-space id of the resource.
229 * @list: The staging list.
230 * @res_p: If the resource is in an already committed state, points to the
231 * struct vmw_resource on successful return. The pointer will be
234 * This function looks up the struct vmw_cmdbuf_res entry from the manager
235 * hash table and, if it exists, removes it. Depending on its current staging
236 * state it then either removes the entry from the staging list or adds it
237 * to it with a staging state of removal.
239 int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
240 enum vmw_cmdbuf_res_type res_type,
242 struct list_head *list,
243 struct vmw_resource **res_p)
245 struct vmw_cmdbuf_res *entry;
246 struct drm_hash_item *hash;
249 ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
251 if (likely(ret != 0))
254 entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
256 switch (entry->state) {
257 case VMW_CMDBUF_RES_ADD:
258 vmw_cmdbuf_res_free(man, entry);
261 case VMW_CMDBUF_RES_COMMITTED:
262 (void) drm_ht_remove_item(&man->resources, &entry->hash);
263 list_del(&entry->head);
264 entry->state = VMW_CMDBUF_RES_DEL;
265 list_add_tail(&entry->head, list);
277 * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
280 * @dev_priv: Pointer to a struct vmw_private
282 * Allocates and initializes a command buffer managed resource manager. Returns
283 * an error pointer on failure.
285 struct vmw_cmdbuf_res_manager *
286 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
288 struct vmw_cmdbuf_res_manager *man;
291 man = kzalloc(sizeof(*man), GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
295 man->dev_priv = dev_priv;
296 INIT_LIST_HEAD(&man->list);
297 ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
306 * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
309 * @man: Pointer to the manager to destroy.
311 * This function destroys a command buffer managed resource manager and
312 * unreferences / frees all command buffer managed resources and -entries
313 * associated with it.
315 void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
317 struct vmw_cmdbuf_res *entry, *next;
319 list_for_each_entry_safe(entry, next, &man->list, head)
320 vmw_cmdbuf_res_free(man, entry);
322 drm_ht_remove(&man->resources);
327 * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
330 * Returns the approximate allocation size of a command buffer managed
333 size_t vmw_cmdbuf_res_man_size(void)
335 static size_t res_man_size;
337 if (unlikely(res_man_size == 0))
339 ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
340 ttm_round_pot(sizeof(struct hlist_head) <<
341 VMW_CMDBUF_RES_MAN_HT_ORDER);