summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/vmwgfx/vmwgfx_cmdbuf_res.c
blob: 9487faff522932448199ba52f071f862420ec1be (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-License-Identifier: GPL-2.0 OR MIT
/**************************************************************************
 *
 * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "vmwgfx_drv.h"
#include "vmwgfx_resource_priv.h"

#define VMW_CMDBUF_RES_MAN_HT_ORDER 12

/**
 * struct vmw_cmdbuf_res - Command buffer managed resource entry.
 *
 * @res: Refcounted pointer to a struct vmw_resource.
 * @hash: Hash entry for the manager hash table.
 * @head: List head used either by the staging list or the manager list
 * of commited resources.
 * @state: Staging state of this resource entry.
 * @man: Pointer to a resource manager for this entry.
 */
struct vmw_cmdbuf_res {
	struct vmw_resource *res;
	struct drm_hash_item hash;
	struct list_head head;
	enum vmw_cmdbuf_res_state state;
	struct vmw_cmdbuf_res_manager *man;
};

/**
 * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
 *
 * @resources: Hash table containing staged and commited command buffer
 * resources
 * @list: List of commited command buffer resources.
 * @dev_priv: Pointer to a device private structure.
 *
 * @resources and @list are protected by the cmdbuf mutex for now.
 */
struct vmw_cmdbuf_res_manager {
	struct drm_open_hash resources;
	struct list_head list;
	struct vmw_private *dev_priv;
};


/**
 * vmw_cmdbuf_res_lookup - Look up a command buffer resource
 *
 * @man: Pointer to the command buffer resource manager
 * @res_type: The resource type, that combined with the user key
 * identifies the resource.
 * @user_key: The user key.
 *
 * Returns a valid refcounted struct vmw_resource pointer on success,
 * an error pointer on failure.
 */
struct vmw_resource *
vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
		      enum vmw_cmdbuf_res_type res_type,
		      u32 user_key)
{
	struct drm_hash_item *hash;
	int ret;
	unsigned long key = user_key | (res_type << 24);

	ret = drm_ht_find_item(&man->resources, key, &hash);
	if (unlikely(ret != 0))
		return ERR_PTR(ret);

	return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
}

/**
 * vmw_cmdbuf_res_free - Free a command buffer resource.
 *
 * @man: Pointer to the command buffer resource manager
 * @entry: Pointer to a struct vmw_cmdbuf_res.
 *
 * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
 * struct vmw_resource.
 */
static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
				struct vmw_cmdbuf_res *entry)
{
	list_del(&entry->head);
	WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
	vmw_resource_unreference(&entry->res);
	kfree(entry);
}

/**
 * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
 *
 * @list: Caller's list of command buffer resource actions.
 *
 * This function commits a list of command buffer resource
 * additions or removals.
 * It is typically called when the execbuf ioctl call triggering these
 * actions has commited the fifo contents to the device.
 */
void vmw_cmdbuf_res_commit(struct list_head *list)
{
	struct vmw_cmdbuf_res *entry, *next;

	list_for_each_entry_safe(entry, next, list, head) {
		list_del(&entry->head);
		if (entry->res->func->commit_notify)
			entry->res->func->commit_notify(entry->res,
							entry->state);
		switch (entry->state) {
		case VMW_CMDBUF_RES_ADD:
			entry->state = VMW_CMDBUF_RES_COMMITTED;
			list_add_tail(&entry->head, &entry->man->list);
			break;
		case VMW_CMDBUF_RES_DEL:
			vmw_resource_unreference(&entry->res);
			kfree(entry);
			break;
		default:
			BUG();
			break;
		}
	}
}

/**
 * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
 *
 * @list: Caller's list of command buffer resource action
 *
 * This function reverts a list of command buffer resource
 * additions or removals.
 * It is typically called when the execbuf ioctl call triggering these
 * actions failed for some reason, and the command stream was never
 * submitted.
 */
void vmw_cmdbuf_res_revert(struct list_head *list)
{
	struct vmw_cmdbuf_res *entry, *next;
	int ret;

	list_for_each_entry_safe(entry, next, list, head) {
		switch (entry->state) {
		case VMW_CMDBUF_RES_ADD:
			vmw_cmdbuf_res_free(entry->man, entry);
			break;
		case VMW_CMDBUF_RES_DEL:
			ret = drm_ht_insert_item(&entry->man->resources, &entry->hash);
			BUG_ON(ret);
			list_del(&entry->head);
			list_add_tail(&entry->head, &entry->man->list);
			entry->state = VMW_CMDBUF_RES_COMMITTED;
			break;
		default:
			BUG();
			break;
		}
	}
}

/**
 * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
 *
 * @man: Pointer to the command buffer resource manager.
 * @res_type: The resource type.
 * @user_key: The user-space id of the resource.
 * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
 * @list: The staging list.
 *
 * This function allocates a struct vmw_cmdbuf_res entry and adds the
 * resource to the hash table of the manager identified by @man. The
 * entry is then put on the staging list identified by @list.
 */
int 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)
{
	struct vmw_cmdbuf_res *cres;
	int ret;

	cres = kzalloc(sizeof(*cres), GFP_KERNEL);
	if (unlikely(!cres))
		return -ENOMEM;

	cres->hash.key = user_key | (res_type << 24);
	ret = drm_ht_insert_item(&man->resources, &cres->hash);
	if (unlikely(ret != 0)) {
		kfree(cres);
		goto out_invalid_key;
	}

	cres->state = VMW_CMDBUF_RES_ADD;
	cres->res = vmw_resource_reference(res);
	cres->man = man;
	list_add_tail(&cres->head, list);

out_invalid_key:
	return ret;
}

/**
 * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
 *
 * @man: Pointer to the command buffer resource manager.
 * @res_type: The resource type.
 * @user_key: The user-space id of the resource.
 * @list: The staging list.
 * @res_p: If the resource is in an already committed state, points to the
 * struct vmw_resource on successful return. The pointer will be
 * non ref-counted.
 *
 * This function looks up the struct vmw_cmdbuf_res entry from the manager
 * hash table and, if it exists, removes it. Depending on its current staging
 * state it then either removes the entry from the staging list or adds it
 * to it with a staging state of removal.
 */
int 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)
{
	struct vmw_cmdbuf_res *entry;
	struct drm_hash_item *hash;
	int ret;

	ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
			       &hash);
	if (likely(ret != 0))
		return -EINVAL;

	entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);

	switch (entry->state) {
	case VMW_CMDBUF_RES_ADD:
		vmw_cmdbuf_res_free(man, entry);
		*res_p = NULL;
		break;
	case VMW_CMDBUF_RES_COMMITTED:
		(void) drm_ht_remove_item(&man->resources, &entry->hash);
		list_del(&entry->head);
		entry->state = VMW_CMDBUF_RES_DEL;
		list_add_tail(&entry->head, list);
		*res_p = entry->res;
		break;
	default:
		BUG();
		break;
	}

	return 0;
}

/**
 * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
 * manager.
 *
 * @dev_priv: Pointer to a struct vmw_private
 *
 * Allocates and initializes a command buffer managed resource manager. Returns
 * an error pointer on failure.
 */
struct vmw_cmdbuf_res_manager *
vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
{
	struct vmw_cmdbuf_res_manager *man;
	int ret;

	man = kzalloc(sizeof(*man), GFP_KERNEL);
	if (!man)
		return ERR_PTR(-ENOMEM);

	man->dev_priv = dev_priv;
	INIT_LIST_HEAD(&man->list);
	ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
	if (ret == 0)
		return man;

	kfree(man);
	return ERR_PTR(ret);
}

/**
 * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
 * manager.
 *
 * @man: Pointer to the  manager to destroy.
 *
 * This function destroys a command buffer managed resource manager and
 * unreferences / frees all command buffer managed resources and -entries
 * associated with it.
 */
void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
{
	struct vmw_cmdbuf_res *entry, *next;

	list_for_each_entry_safe(entry, next, &man->list, head)
		vmw_cmdbuf_res_free(man, entry);

	drm_ht_remove(&man->resources);
	kfree(man);
}

/**
 * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
 * resource manager
 *
 * Returns the approximate allocation size of a command buffer managed
 * resource manager.
 */
size_t vmw_cmdbuf_res_man_size(void)
{
	static size_t res_man_size;

	if (unlikely(res_man_size == 0))
		res_man_size =
			ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
			ttm_round_pot(sizeof(struct hlist_head) <<
				      VMW_CMDBUF_RES_MAN_HT_ORDER);

	return res_man_size;
}