summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/gpu/drm/vmwgfx/vmwgfx_so.c
blob: 5a73eebd0f35f153d498d29f638d1acf2a59e30b (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**************************************************************************
 * Copyright © 2014-2015 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * 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"
#include "vmwgfx_so.h"
#include "vmwgfx_binding.h"

/*
 * The currently only reason we need to keep track of views is that if we
 * destroy a hardware surface, all views pointing to it must also be destroyed,
 * otherwise the device will error.
 * So in particuar if a surface is evicted, we must destroy all views pointing
 * to it, and all context bindings of that view. Similarly we must restore
 * the view bindings, views and surfaces pointed to by the views when a
 * context is referenced in the command stream.
 */

/**
 * struct vmw_view - view metadata
 *
 * @res: The struct vmw_resource we derive from
 * @ctx: Non-refcounted pointer to the context this view belongs to.
 * @srf: Refcounted pointer to the surface pointed to by this view.
 * @cotable: Refcounted pointer to the cotable holding this view.
 * @srf_head: List head for the surface-to-view list.
 * @cotable_head: List head for the cotable-to_view list.
 * @view_type: View type.
 * @view_id: User-space per context view id. Currently used also as per
 * context device view id.
 * @cmd_size: Size of the SVGA3D define view command that we've copied from the
 * command stream.
 * @committed: Whether the view is actually created or pending creation at the
 * device level.
 * @cmd: The SVGA3D define view command copied from the command stream.
 */
struct vmw_view {
	struct rcu_head rcu;
	struct vmw_resource res;
	struct vmw_resource *ctx;      /* Immutable */
	struct vmw_resource *srf;      /* Immutable */
	struct vmw_resource *cotable;  /* Immutable */
	struct list_head srf_head;     /* Protected by binding_mutex */
	struct list_head cotable_head; /* Protected by binding_mutex */
	unsigned view_type;            /* Immutable */
	unsigned view_id;              /* Immutable */
	u32 cmd_size;                  /* Immutable */
	bool committed;                /* Protected by binding_mutex */
	u32 cmd[1];                    /* Immutable */
};

static int vmw_view_create(struct vmw_resource *res);
static int vmw_view_destroy(struct vmw_resource *res);
static void vmw_hw_view_destroy(struct vmw_resource *res);
static void vmw_view_commit_notify(struct vmw_resource *res,
				   enum vmw_cmdbuf_res_state state);

static const struct vmw_res_func vmw_view_func = {
	.res_type = vmw_res_view,
	.needs_backup = false,
	.may_evict = false,
	.type_name = "DX view",
	.backup_placement = NULL,
	.create = vmw_view_create,
	.commit_notify = vmw_view_commit_notify,
};

/**
 * struct vmw_view - view define command body stub
 *
 * @view_id: The device id of the view being defined
 * @sid: The surface id of the view being defined
 *
 * This generic struct is used by the code to change @view_id and @sid of a
 * saved view define command.
 */
struct vmw_view_define {
	uint32 view_id;
	uint32 sid;
};

/**
 * vmw_view - Convert a struct vmw_resource to a struct vmw_view
 *
 * @res: Pointer to the resource to convert.
 *
 * Returns a pointer to a struct vmw_view.
 */
static struct vmw_view *vmw_view(struct vmw_resource *res)
{
	return container_of(res, struct vmw_view, res);
}

/**
 * vmw_view_commit_notify - Notify that a view operation has been committed to
 * hardware from a user-supplied command stream.
 *
 * @res: Pointer to the view resource.
 * @state: Indicating whether a creation or removal has been committed.
 *
 */
static void vmw_view_commit_notify(struct vmw_resource *res,
				   enum vmw_cmdbuf_res_state state)
{
	struct vmw_view *view = vmw_view(res);
	struct vmw_private *dev_priv = res->dev_priv;

	mutex_lock(&dev_priv->binding_mutex);
	if (state == VMW_CMDBUF_RES_ADD) {
		struct vmw_surface *srf = vmw_res_to_srf(view->srf);

		list_add_tail(&view->srf_head, &srf->view_list);
		vmw_cotable_add_resource(view->cotable, &view->cotable_head);
		view->committed = true;
		res->id = view->view_id;

	} else {
		list_del_init(&view->cotable_head);
		list_del_init(&view->srf_head);
		view->committed = false;
		res->id = -1;
	}
	mutex_unlock(&dev_priv->binding_mutex);
}

/**
 * vmw_view_create - Create a hardware view.
 *
 * @res: Pointer to the view resource.
 *
 * Create a hardware view. Typically used if that view has previously been
 * destroyed by an eviction operation.
 */
static int vmw_view_create(struct vmw_resource *res)
{
	struct vmw_view *view = vmw_view(res);
	struct vmw_surface *srf = vmw_res_to_srf(view->srf);
	struct vmw_private *dev_priv = res->dev_priv;
	struct {
		SVGA3dCmdHeader header;
		struct vmw_view_define body;
	} *cmd;

	mutex_lock(&dev_priv->binding_mutex);
	if (!view->committed) {
		mutex_unlock(&dev_priv->binding_mutex);
		return 0;
	}

	cmd = vmw_fifo_reserve_dx(res->dev_priv, view->cmd_size,
				  view->ctx->id);
	if (!cmd) {
		DRM_ERROR("Failed reserving FIFO space for view creation.\n");
		mutex_unlock(&dev_priv->binding_mutex);
		return -ENOMEM;
	}
	memcpy(cmd, &view->cmd, view->cmd_size);
	WARN_ON(cmd->body.view_id != view->view_id);
	/* Sid may have changed due to surface eviction. */
	WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
	cmd->body.sid = view->srf->id;
	vmw_fifo_commit(res->dev_priv, view->cmd_size);
	res->id = view->view_id;
	list_add_tail(&view->srf_head, &srf->view_list);
	vmw_cotable_add_resource(view->cotable, &view->cotable_head);
	mutex_unlock(&dev_priv->binding_mutex);

	return 0;
}

/**
 * vmw_view_destroy - Destroy a hardware view.
 *
 * @res: Pointer to the view resource.
 *
 * Destroy a hardware view. Typically used on unexpected termination of the
 * owning process or if the surface the view is pointing to is destroyed.
 */
static int vmw_view_destroy(struct vmw_resource *res)
{
	struct vmw_private *dev_priv = res->dev_priv;
	struct vmw_view *view = vmw_view(res);
	struct {
		SVGA3dCmdHeader header;
		union vmw_view_destroy body;
	} *cmd;

	WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
	vmw_binding_res_list_scrub(&res->binding_head);

	if (!view->committed || res->id == -1)
		return 0;

	cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), view->ctx->id);
	if (!cmd) {
		DRM_ERROR("Failed reserving FIFO space for view "
			  "destruction.\n");
		return -ENOMEM;
	}

	cmd->header.id = vmw_view_destroy_cmds[view->view_type];
	cmd->header.size = sizeof(cmd->body);
	cmd->body.view_id = view->view_id;
	vmw_fifo_commit(dev_priv, sizeof(*cmd));
	res->id = -1;
	list_del_init(&view->cotable_head);
	list_del_init(&view->srf_head);

	return 0;
}

/**
 * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
 *
 * @res: Pointer to the view resource.
 *
 * Destroy a hardware view if it's still present.
 */
static void vmw_hw_view_destroy(struct vmw_resource *res)
{
	struct vmw_private *dev_priv = res->dev_priv;

	mutex_lock(&dev_priv->binding_mutex);
	WARN_ON(vmw_view_destroy(res));
	res->id = -1;
	mutex_unlock(&dev_priv->binding_mutex);
}

/**
 * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
 *
 * @user_key: The user-space id used for the view.
 * @view_type: The view type.
 *
 * Destroy a hardware view if it's still present.
 */
static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
{
	return user_key | (view_type << 20);
}

/**
 * vmw_view_id_ok - Basic view id and type range checks.
 *
 * @user_key: The user-space id used for the view.
 * @view_type: The view type.
 *
 * Checks that the view id and type (typically provided by user-space) is
 * valid.
 */
static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
{
	return (user_key < SVGA_COTABLE_MAX_IDS &&
		view_type < vmw_view_max);
}

/**
 * vmw_view_res_free - resource res_free callback for view resources
 *
 * @res: Pointer to a struct vmw_resource
 *
 * Frees memory and memory accounting held by a struct vmw_view.
 */
static void vmw_view_res_free(struct vmw_resource *res)
{
	struct vmw_view *view = vmw_view(res);
	size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size;
	struct vmw_private *dev_priv = res->dev_priv;

	vmw_resource_unreference(&view->cotable);
	vmw_resource_unreference(&view->srf);
	kfree_rcu(view, rcu);
	ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
}

/**
 * vmw_view_add - Create a view resource and stage it for addition
 * as a command buffer managed resource.
 *
 * @man: Pointer to the compat shader manager identifying the shader namespace.
 * @ctx: Pointer to a struct vmw_resource identifying the active context.
 * @srf: Pointer to a struct vmw_resource identifying the surface the view
 * points to.
 * @view_type: The view type deduced from the view create command.
 * @user_key: The key that is used to identify the shader. The key is
 * unique to the view type and to the context.
 * @cmd: Pointer to the view create command in the command stream.
 * @cmd_size: Size of the view create command in the command stream.
 * @list: Caller's list of staged command buffer resource actions.
 */
int vmw_view_add(struct vmw_cmdbuf_res_manager *man,
		 struct vmw_resource *ctx,
		 struct vmw_resource *srf,
		 enum vmw_view_type view_type,
		 u32 user_key,
		 const void *cmd,
		 size_t cmd_size,
		 struct list_head *list)
{
	static const size_t vmw_view_define_sizes[] = {
		[vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
		[vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
		[vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView)
	};

	struct vmw_private *dev_priv = ctx->dev_priv;
	struct vmw_resource *res;
	struct vmw_view *view;
	size_t size;
	int ret;

	if (cmd_size != vmw_view_define_sizes[view_type] +
	    sizeof(SVGA3dCmdHeader)) {
		DRM_ERROR("Illegal view create command size.\n");
		return -EINVAL;
	}

	if (!vmw_view_id_ok(user_key, view_type)) {
		DRM_ERROR("Illegal view add view id.\n");
		return -EINVAL;
	}

	size = offsetof(struct vmw_view, cmd) + cmd_size;

	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, false, true);
	if (ret) {
		if (ret != -ERESTARTSYS)
			DRM_ERROR("Out of graphics memory for view"
				  " creation.\n");
		return ret;
	}

	view = kmalloc(size, GFP_KERNEL);
	if (!view) {
		ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
		return -ENOMEM;
	}

	res = &view->res;
	view->ctx = ctx;
	view->srf = vmw_resource_reference(srf);
	view->cotable = vmw_context_cotable(ctx, vmw_view_cotables[view_type]);
	view->view_type = view_type;
	view->view_id = user_key;
	view->cmd_size = cmd_size;
	view->committed = false;
	INIT_LIST_HEAD(&view->srf_head);
	INIT_LIST_HEAD(&view->cotable_head);
	memcpy(&view->cmd, cmd, cmd_size);
	ret = vmw_resource_init(dev_priv, res, true,
				vmw_view_res_free, &vmw_view_func);
	if (ret)
		goto out_resource_init;

	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
				 vmw_view_key(user_key, view_type),
				 res, list);
	if (ret)
		goto out_resource_init;

	res->id = view->view_id;
	vmw_resource_activate(res, vmw_hw_view_destroy);

out_resource_init:
	vmw_resource_unreference(&res);

	return ret;
}

/**
 * vmw_view_remove - Stage a view for removal.
 *
 * @man: Pointer to the view manager identifying the shader namespace.
 * @user_key: The key that is used to identify the view. The key is
 * unique to the view type.
 * @view_type: View type
 * @list: Caller's list of staged command buffer resource actions.
 * @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.
 */
int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
		    u32 user_key, enum vmw_view_type view_type,
		    struct list_head *list,
		    struct vmw_resource **res_p)
{
	if (!vmw_view_id_ok(user_key, view_type)) {
		DRM_ERROR("Illegal view remove view id.\n");
		return -EINVAL;
	}

	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
				     vmw_view_key(user_key, view_type),
				     list, res_p);
}

/**
 * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
 *
 * @dev_priv: Pointer to a device private struct.
 * @list: List of views belonging to a cotable.
 * @readback: Unused. Needed for function interface only.
 *
 * This function evicts all views belonging to a cotable.
 * It must be called with the binding_mutex held, and the caller must hold
 * a reference to the view resource. This is typically called before the
 * cotable is paged out.
 */
void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
				   struct list_head *list,
				   bool readback)
{
	struct vmw_view *entry, *next;

	WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));

	list_for_each_entry_safe(entry, next, list, cotable_head)
		WARN_ON(vmw_view_destroy(&entry->res));
}

/**
 * vmw_view_surface_list_destroy - Evict all views pointing to a surface
 *
 * @dev_priv: Pointer to a device private struct.
 * @list: List of views pointing to a surface.
 *
 * This function evicts all views pointing to a surface. This is typically
 * called before the surface is evicted.
 */
void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
				   struct list_head *list)
{
	struct vmw_view *entry, *next;

	WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));

	list_for_each_entry_safe(entry, next, list, srf_head)
		WARN_ON(vmw_view_destroy(&entry->res));
}

/**
 * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
 * pointing to.
 *
 * @res: pointer to a view resource.
 *
 * Note that the view itself is holding a reference, so as long
 * the view resource is alive, the surface resource will be.
 */
struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
{
	return vmw_view(res)->srf;
}

/**
 * vmw_view_lookup - Look up a view.
 *
 * @man: The context's cmdbuf ref manager.
 * @view_type: The view type.
 * @user_key: The view user id.
 *
 * returns a refcounted pointer to a view or an error pointer if not found.
 */
struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
				     enum vmw_view_type view_type,
				     u32 user_key)
{
	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
				     vmw_view_key(user_key, view_type));
}

const u32 vmw_view_destroy_cmds[] = {
	[vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
	[vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
	[vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
};

const SVGACOTableType vmw_view_cotables[] = {
	[vmw_view_sr] = SVGA_COTABLE_SRVIEW,
	[vmw_view_rt] = SVGA_COTABLE_RTVIEW,
	[vmw_view_ds] = SVGA_COTABLE_DSVIEW,
};

const SVGACOTableType vmw_so_cotables[] = {
	[vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
	[vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
	[vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
	[vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
	[vmw_so_ss] = SVGA_COTABLE_SAMPLER,
	[vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
};


/* To remove unused function warning */
static void vmw_so_build_asserts(void) __attribute__((used));


/*
 * This function is unused at run-time, and only used to dump various build
 * asserts important for code optimization assumptions.
 */
static void vmw_so_build_asserts(void)
{
	/* Assert that our vmw_view_cmd_to_type() function is correct. */
	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);

	/* Assert that our "one body fits all" assumption is valid */
	BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));

	/* Assert that the view key space can hold all view ids. */
	BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));

	/*
	 * Assert that the offset of sid in all view define commands
	 * is what we assume it to be.
	 */
	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
		     offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
		     offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
		     offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
}