1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR MIT
2*4882a593Smuzhiyun /**************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
7*4882a593Smuzhiyun * copy of this software and associated documentation files (the
8*4882a593Smuzhiyun * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun * without limitation the rights to use, copy, modify, merge, publish,
10*4882a593Smuzhiyun * distribute, sub license, and/or sell copies of the Software, and to
11*4882a593Smuzhiyun * permit persons to whom the Software is furnished to do so, subject to
12*4882a593Smuzhiyun * the following conditions:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun * next paragraph) shall be included in all copies or substantial portions
16*4882a593Smuzhiyun * of the Software.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*4882a593Smuzhiyun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*4882a593Smuzhiyun * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*4882a593Smuzhiyun * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun **************************************************************************/
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include "vmwgfx_drv.h"
29*4882a593Smuzhiyun #include "vmwgfx_resource_priv.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun * struct vmw_cmdbuf_res - Command buffer managed resource entry.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * @res: Refcounted pointer to a struct vmw_resource.
37*4882a593Smuzhiyun * @hash: Hash entry for the manager hash table.
38*4882a593Smuzhiyun * @head: List head used either by the staging list or the manager list
39*4882a593Smuzhiyun * of commited resources.
40*4882a593Smuzhiyun * @state: Staging state of this resource entry.
41*4882a593Smuzhiyun * @man: Pointer to a resource manager for this entry.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun struct vmw_cmdbuf_res {
44*4882a593Smuzhiyun struct vmw_resource *res;
45*4882a593Smuzhiyun struct drm_hash_item hash;
46*4882a593Smuzhiyun struct list_head head;
47*4882a593Smuzhiyun enum vmw_cmdbuf_res_state state;
48*4882a593Smuzhiyun struct vmw_cmdbuf_res_manager *man;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * @resources: Hash table containing staged and commited command buffer
55*4882a593Smuzhiyun * resources
56*4882a593Smuzhiyun * @list: List of commited command buffer resources.
57*4882a593Smuzhiyun * @dev_priv: Pointer to a device private structure.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * @resources and @list are protected by the cmdbuf mutex for now.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun struct vmw_cmdbuf_res_manager {
62*4882a593Smuzhiyun struct drm_open_hash resources;
63*4882a593Smuzhiyun struct list_head list;
64*4882a593Smuzhiyun struct vmw_private *dev_priv;
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun * vmw_cmdbuf_res_lookup - Look up a command buffer resource
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * @man: Pointer to the command buffer resource manager
72*4882a593Smuzhiyun * @resource_type: The resource type, that combined with the user key
73*4882a593Smuzhiyun * identifies the resource.
74*4882a593Smuzhiyun * @user_key: The user key.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * Returns a valid refcounted struct vmw_resource pointer on success,
77*4882a593Smuzhiyun * an error pointer on failure.
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun struct vmw_resource *
vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key)80*4882a593Smuzhiyun vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
81*4882a593Smuzhiyun enum vmw_cmdbuf_res_type res_type,
82*4882a593Smuzhiyun u32 user_key)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun struct drm_hash_item *hash;
85*4882a593Smuzhiyun int ret;
86*4882a593Smuzhiyun unsigned long key = user_key | (res_type << 24);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun ret = drm_ht_find_item(&man->resources, key, &hash);
89*4882a593Smuzhiyun if (unlikely(ret != 0))
90*4882a593Smuzhiyun return ERR_PTR(ret);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun * vmw_cmdbuf_res_free - Free a command buffer resource.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * @man: Pointer to the command buffer resource manager
99*4882a593Smuzhiyun * @entry: Pointer to a struct vmw_cmdbuf_res.
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
102*4882a593Smuzhiyun * struct vmw_resource.
103*4882a593Smuzhiyun */
vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager * man,struct vmw_cmdbuf_res * entry)104*4882a593Smuzhiyun static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
105*4882a593Smuzhiyun struct vmw_cmdbuf_res *entry)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun list_del(&entry->head);
108*4882a593Smuzhiyun WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
109*4882a593Smuzhiyun vmw_resource_unreference(&entry->res);
110*4882a593Smuzhiyun kfree(entry);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * @list: Caller's list of command buffer resource actions.
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * This function commits a list of command buffer resource
119*4882a593Smuzhiyun * additions or removals.
120*4882a593Smuzhiyun * It is typically called when the execbuf ioctl call triggering these
121*4882a593Smuzhiyun * actions has commited the fifo contents to the device.
122*4882a593Smuzhiyun */
vmw_cmdbuf_res_commit(struct list_head * list)123*4882a593Smuzhiyun void vmw_cmdbuf_res_commit(struct list_head *list)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun struct vmw_cmdbuf_res *entry, *next;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun list_for_each_entry_safe(entry, next, list, head) {
128*4882a593Smuzhiyun list_del(&entry->head);
129*4882a593Smuzhiyun if (entry->res->func->commit_notify)
130*4882a593Smuzhiyun entry->res->func->commit_notify(entry->res,
131*4882a593Smuzhiyun entry->state);
132*4882a593Smuzhiyun switch (entry->state) {
133*4882a593Smuzhiyun case VMW_CMDBUF_RES_ADD:
134*4882a593Smuzhiyun entry->state = VMW_CMDBUF_RES_COMMITTED;
135*4882a593Smuzhiyun list_add_tail(&entry->head, &entry->man->list);
136*4882a593Smuzhiyun break;
137*4882a593Smuzhiyun case VMW_CMDBUF_RES_DEL:
138*4882a593Smuzhiyun vmw_resource_unreference(&entry->res);
139*4882a593Smuzhiyun kfree(entry);
140*4882a593Smuzhiyun break;
141*4882a593Smuzhiyun default:
142*4882a593Smuzhiyun BUG();
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /**
149*4882a593Smuzhiyun * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * @man: Pointer to the command buffer resource manager
152*4882a593Smuzhiyun * @list: Caller's list of command buffer resource action
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * This function reverts a list of command buffer resource
155*4882a593Smuzhiyun * additions or removals.
156*4882a593Smuzhiyun * It is typically called when the execbuf ioctl call triggering these
157*4882a593Smuzhiyun * actions failed for some reason, and the command stream was never
158*4882a593Smuzhiyun * submitted.
159*4882a593Smuzhiyun */
vmw_cmdbuf_res_revert(struct list_head * list)160*4882a593Smuzhiyun void vmw_cmdbuf_res_revert(struct list_head *list)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct vmw_cmdbuf_res *entry, *next;
163*4882a593Smuzhiyun int ret;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun list_for_each_entry_safe(entry, next, list, head) {
166*4882a593Smuzhiyun switch (entry->state) {
167*4882a593Smuzhiyun case VMW_CMDBUF_RES_ADD:
168*4882a593Smuzhiyun vmw_cmdbuf_res_free(entry->man, entry);
169*4882a593Smuzhiyun break;
170*4882a593Smuzhiyun case VMW_CMDBUF_RES_DEL:
171*4882a593Smuzhiyun ret = drm_ht_insert_item(&entry->man->resources,
172*4882a593Smuzhiyun &entry->hash);
173*4882a593Smuzhiyun list_del(&entry->head);
174*4882a593Smuzhiyun list_add_tail(&entry->head, &entry->man->list);
175*4882a593Smuzhiyun entry->state = VMW_CMDBUF_RES_COMMITTED;
176*4882a593Smuzhiyun break;
177*4882a593Smuzhiyun default:
178*4882a593Smuzhiyun BUG();
179*4882a593Smuzhiyun break;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * @man: Pointer to the command buffer resource manager.
188*4882a593Smuzhiyun * @res_type: The resource type.
189*4882a593Smuzhiyun * @user_key: The user-space id of the resource.
190*4882a593Smuzhiyun * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
191*4882a593Smuzhiyun * @list: The staging list.
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun * This function allocates a struct vmw_cmdbuf_res entry and adds the
194*4882a593Smuzhiyun * resource to the hash table of the manager identified by @man. The
195*4882a593Smuzhiyun * entry is then put on the staging list identified by @list.
196*4882a593Smuzhiyun */
vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key,struct vmw_resource * res,struct list_head * list)197*4882a593Smuzhiyun int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
198*4882a593Smuzhiyun enum vmw_cmdbuf_res_type res_type,
199*4882a593Smuzhiyun u32 user_key,
200*4882a593Smuzhiyun struct vmw_resource *res,
201*4882a593Smuzhiyun struct list_head *list)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun struct vmw_cmdbuf_res *cres;
204*4882a593Smuzhiyun int ret;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun cres = kzalloc(sizeof(*cres), GFP_KERNEL);
207*4882a593Smuzhiyun if (unlikely(!cres))
208*4882a593Smuzhiyun return -ENOMEM;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun cres->hash.key = user_key | (res_type << 24);
211*4882a593Smuzhiyun ret = drm_ht_insert_item(&man->resources, &cres->hash);
212*4882a593Smuzhiyun if (unlikely(ret != 0)) {
213*4882a593Smuzhiyun kfree(cres);
214*4882a593Smuzhiyun goto out_invalid_key;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun cres->state = VMW_CMDBUF_RES_ADD;
218*4882a593Smuzhiyun cres->res = vmw_resource_reference(res);
219*4882a593Smuzhiyun cres->man = man;
220*4882a593Smuzhiyun list_add_tail(&cres->head, list);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun out_invalid_key:
223*4882a593Smuzhiyun return ret;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * @man: Pointer to the command buffer resource manager.
230*4882a593Smuzhiyun * @res_type: The resource type.
231*4882a593Smuzhiyun * @user_key: The user-space id of the resource.
232*4882a593Smuzhiyun * @list: The staging list.
233*4882a593Smuzhiyun * @res_p: If the resource is in an already committed state, points to the
234*4882a593Smuzhiyun * struct vmw_resource on successful return. The pointer will be
235*4882a593Smuzhiyun * non ref-counted.
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * This function looks up the struct vmw_cmdbuf_res entry from the manager
238*4882a593Smuzhiyun * hash table and, if it exists, removes it. Depending on its current staging
239*4882a593Smuzhiyun * state it then either removes the entry from the staging list or adds it
240*4882a593Smuzhiyun * to it with a staging state of removal.
241*4882a593Smuzhiyun */
vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key,struct list_head * list,struct vmw_resource ** res_p)242*4882a593Smuzhiyun int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
243*4882a593Smuzhiyun enum vmw_cmdbuf_res_type res_type,
244*4882a593Smuzhiyun u32 user_key,
245*4882a593Smuzhiyun struct list_head *list,
246*4882a593Smuzhiyun struct vmw_resource **res_p)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun struct vmw_cmdbuf_res *entry;
249*4882a593Smuzhiyun struct drm_hash_item *hash;
250*4882a593Smuzhiyun int ret;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
253*4882a593Smuzhiyun &hash);
254*4882a593Smuzhiyun if (likely(ret != 0))
255*4882a593Smuzhiyun return -EINVAL;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun switch (entry->state) {
260*4882a593Smuzhiyun case VMW_CMDBUF_RES_ADD:
261*4882a593Smuzhiyun vmw_cmdbuf_res_free(man, entry);
262*4882a593Smuzhiyun *res_p = NULL;
263*4882a593Smuzhiyun break;
264*4882a593Smuzhiyun case VMW_CMDBUF_RES_COMMITTED:
265*4882a593Smuzhiyun (void) drm_ht_remove_item(&man->resources, &entry->hash);
266*4882a593Smuzhiyun list_del(&entry->head);
267*4882a593Smuzhiyun entry->state = VMW_CMDBUF_RES_DEL;
268*4882a593Smuzhiyun list_add_tail(&entry->head, list);
269*4882a593Smuzhiyun *res_p = entry->res;
270*4882a593Smuzhiyun break;
271*4882a593Smuzhiyun default:
272*4882a593Smuzhiyun BUG();
273*4882a593Smuzhiyun break;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun return 0;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /**
280*4882a593Smuzhiyun * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
281*4882a593Smuzhiyun * manager.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * @dev_priv: Pointer to a struct vmw_private
284*4882a593Smuzhiyun *
285*4882a593Smuzhiyun * Allocates and initializes a command buffer managed resource manager. Returns
286*4882a593Smuzhiyun * an error pointer on failure.
287*4882a593Smuzhiyun */
288*4882a593Smuzhiyun struct vmw_cmdbuf_res_manager *
vmw_cmdbuf_res_man_create(struct vmw_private * dev_priv)289*4882a593Smuzhiyun vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun struct vmw_cmdbuf_res_manager *man;
292*4882a593Smuzhiyun int ret;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun man = kzalloc(sizeof(*man), GFP_KERNEL);
295*4882a593Smuzhiyun if (!man)
296*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun man->dev_priv = dev_priv;
299*4882a593Smuzhiyun INIT_LIST_HEAD(&man->list);
300*4882a593Smuzhiyun ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
301*4882a593Smuzhiyun if (ret == 0)
302*4882a593Smuzhiyun return man;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun kfree(man);
305*4882a593Smuzhiyun return ERR_PTR(ret);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun /**
309*4882a593Smuzhiyun * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
310*4882a593Smuzhiyun * manager.
311*4882a593Smuzhiyun *
312*4882a593Smuzhiyun * @man: Pointer to the manager to destroy.
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * This function destroys a command buffer managed resource manager and
315*4882a593Smuzhiyun * unreferences / frees all command buffer managed resources and -entries
316*4882a593Smuzhiyun * associated with it.
317*4882a593Smuzhiyun */
vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager * man)318*4882a593Smuzhiyun void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun struct vmw_cmdbuf_res *entry, *next;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun list_for_each_entry_safe(entry, next, &man->list, head)
323*4882a593Smuzhiyun vmw_cmdbuf_res_free(man, entry);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun drm_ht_remove(&man->resources);
326*4882a593Smuzhiyun kfree(man);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
332*4882a593Smuzhiyun * resource manager
333*4882a593Smuzhiyun *
334*4882a593Smuzhiyun * Returns the approximate allocation size of a command buffer managed
335*4882a593Smuzhiyun * resource manager.
336*4882a593Smuzhiyun */
vmw_cmdbuf_res_man_size(void)337*4882a593Smuzhiyun size_t vmw_cmdbuf_res_man_size(void)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun static size_t res_man_size;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if (unlikely(res_man_size == 0))
342*4882a593Smuzhiyun res_man_size =
343*4882a593Smuzhiyun ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
344*4882a593Smuzhiyun ttm_round_pot(sizeof(struct hlist_head) <<
345*4882a593Smuzhiyun VMW_CMDBUF_RES_MAN_HT_ORDER);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun return res_man_size;
348*4882a593Smuzhiyun }
349