Commit | Line | Data |
---|---|---|
543831cf TH |
1 | /************************************************************************** |
2 | * | |
3 | * Copyright © 2009-2012 VMware, Inc., Palo Alto, CA., USA | |
4 | * All Rights Reserved. | |
5 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice (including the | |
15 | * next paragraph) shall be included in all copies or substantial portions | |
16 | * of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | **************************************************************************/ | |
27 | ||
28 | #include "vmwgfx_drv.h" | |
29 | #include "vmwgfx_resource_priv.h" | |
d80efd5c | 30 | #include "vmwgfx_binding.h" |
543831cf TH |
31 | #include "ttm/ttm_placement.h" |
32 | ||
33 | struct vmw_user_context { | |
34 | struct ttm_base_object base; | |
35 | struct vmw_resource res; | |
d80efd5c | 36 | struct vmw_ctx_binding_state *cbs; |
18e4a466 | 37 | struct vmw_cmdbuf_res_manager *man; |
d80efd5c TH |
38 | struct vmw_resource *cotables[SVGA_COTABLE_DX10_MAX]; |
39 | spinlock_t cotable_lock; | |
2f633e5e | 40 | struct vmw_dma_buffer *dx_query_mob; |
543831cf TH |
41 | }; |
42 | ||
43 | static void vmw_user_context_free(struct vmw_resource *res); | |
44 | static struct vmw_resource * | |
45 | vmw_user_context_base_to_res(struct ttm_base_object *base); | |
46 | ||
58a0c5f0 TH |
47 | static int vmw_gb_context_create(struct vmw_resource *res); |
48 | static int vmw_gb_context_bind(struct vmw_resource *res, | |
49 | struct ttm_validate_buffer *val_buf); | |
50 | static int vmw_gb_context_unbind(struct vmw_resource *res, | |
51 | bool readback, | |
52 | struct ttm_validate_buffer *val_buf); | |
53 | static int vmw_gb_context_destroy(struct vmw_resource *res); | |
d80efd5c TH |
54 | static int vmw_dx_context_create(struct vmw_resource *res); |
55 | static int vmw_dx_context_bind(struct vmw_resource *res, | |
56 | struct ttm_validate_buffer *val_buf); | |
57 | static int vmw_dx_context_unbind(struct vmw_resource *res, | |
58 | bool readback, | |
59 | struct ttm_validate_buffer *val_buf); | |
60 | static int vmw_dx_context_destroy(struct vmw_resource *res); | |
61 | ||
543831cf TH |
62 | static uint64_t vmw_user_context_size; |
63 | ||
64 | static const struct vmw_user_resource_conv user_context_conv = { | |
65 | .object_type = VMW_RES_CONTEXT, | |
66 | .base_obj_to_res = vmw_user_context_base_to_res, | |
67 | .res_free = vmw_user_context_free | |
68 | }; | |
69 | ||
70 | const struct vmw_user_resource_conv *user_context_converter = | |
71 | &user_context_conv; | |
72 | ||
73 | ||
74 | static const struct vmw_res_func vmw_legacy_context_func = { | |
75 | .res_type = vmw_res_context, | |
76 | .needs_backup = false, | |
77 | .may_evict = false, | |
78 | .type_name = "legacy contexts", | |
79 | .backup_placement = NULL, | |
80 | .create = NULL, | |
81 | .destroy = NULL, | |
82 | .bind = NULL, | |
83 | .unbind = NULL | |
84 | }; | |
85 | ||
58a0c5f0 TH |
86 | static const struct vmw_res_func vmw_gb_context_func = { |
87 | .res_type = vmw_res_context, | |
88 | .needs_backup = true, | |
89 | .may_evict = true, | |
90 | .type_name = "guest backed contexts", | |
91 | .backup_placement = &vmw_mob_placement, | |
92 | .create = vmw_gb_context_create, | |
93 | .destroy = vmw_gb_context_destroy, | |
94 | .bind = vmw_gb_context_bind, | |
95 | .unbind = vmw_gb_context_unbind | |
96 | }; | |
97 | ||
d80efd5c TH |
98 | static const struct vmw_res_func vmw_dx_context_func = { |
99 | .res_type = vmw_res_dx_context, | |
100 | .needs_backup = true, | |
101 | .may_evict = true, | |
102 | .type_name = "dx contexts", | |
103 | .backup_placement = &vmw_mob_placement, | |
104 | .create = vmw_dx_context_create, | |
105 | .destroy = vmw_dx_context_destroy, | |
106 | .bind = vmw_dx_context_bind, | |
107 | .unbind = vmw_dx_context_unbind | |
108 | }; | |
b5c3b1a6 | 109 | |
543831cf TH |
110 | /** |
111 | * Context management: | |
112 | */ | |
113 | ||
d80efd5c TH |
114 | static void vmw_context_cotables_unref(struct vmw_user_context *uctx) |
115 | { | |
116 | struct vmw_resource *res; | |
117 | int i; | |
118 | ||
119 | for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) { | |
120 | spin_lock(&uctx->cotable_lock); | |
121 | res = uctx->cotables[i]; | |
122 | uctx->cotables[i] = NULL; | |
123 | spin_unlock(&uctx->cotable_lock); | |
124 | vmw_resource_unreference(&res); | |
125 | } | |
126 | } | |
127 | ||
543831cf TH |
128 | static void vmw_hw_context_destroy(struct vmw_resource *res) |
129 | { | |
18e4a466 TH |
130 | struct vmw_user_context *uctx = |
131 | container_of(res, struct vmw_user_context, res); | |
543831cf TH |
132 | struct vmw_private *dev_priv = res->dev_priv; |
133 | struct { | |
134 | SVGA3dCmdHeader header; | |
135 | SVGA3dCmdDestroyContext body; | |
136 | } *cmd; | |
137 | ||
138 | ||
d80efd5c TH |
139 | if (res->func->destroy == vmw_gb_context_destroy || |
140 | res->func->destroy == vmw_dx_context_destroy) { | |
58a0c5f0 | 141 | mutex_lock(&dev_priv->cmdbuf_mutex); |
18e4a466 | 142 | vmw_cmdbuf_res_man_destroy(uctx->man); |
30f82d81 | 143 | mutex_lock(&dev_priv->binding_mutex); |
d80efd5c TH |
144 | vmw_binding_state_kill(uctx->cbs); |
145 | (void) res->func->destroy(res); | |
c8e5e010 | 146 | mutex_unlock(&dev_priv->binding_mutex); |
58a0c5f0 TH |
147 | if (dev_priv->pinned_bo != NULL && |
148 | !dev_priv->query_cid_valid) | |
149 | __vmw_execbuf_release_pinned_bo(dev_priv, NULL); | |
150 | mutex_unlock(&dev_priv->cmdbuf_mutex); | |
d80efd5c | 151 | vmw_context_cotables_unref(uctx); |
58a0c5f0 TH |
152 | return; |
153 | } | |
154 | ||
543831cf TH |
155 | vmw_execbuf_release_pinned_bo(dev_priv); |
156 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
157 | if (unlikely(cmd == NULL)) { | |
158 | DRM_ERROR("Failed reserving FIFO space for surface " | |
159 | "destruction.\n"); | |
160 | return; | |
161 | } | |
162 | ||
b9eb1a61 TH |
163 | cmd->header.id = SVGA_3D_CMD_CONTEXT_DESTROY; |
164 | cmd->header.size = sizeof(cmd->body); | |
165 | cmd->body.cid = res->id; | |
543831cf TH |
166 | |
167 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
153b3d5b | 168 | vmw_fifo_resource_dec(dev_priv); |
543831cf TH |
169 | } |
170 | ||
58a0c5f0 | 171 | static int vmw_gb_context_init(struct vmw_private *dev_priv, |
d80efd5c | 172 | bool dx, |
58a0c5f0 | 173 | struct vmw_resource *res, |
d80efd5c | 174 | void (*res_free)(struct vmw_resource *res)) |
58a0c5f0 | 175 | { |
d80efd5c | 176 | int ret, i; |
173fb7d4 TH |
177 | struct vmw_user_context *uctx = |
178 | container_of(res, struct vmw_user_context, res); | |
58a0c5f0 | 179 | |
d80efd5c TH |
180 | res->backup_size = (dx ? sizeof(SVGADXContextMobFormat) : |
181 | SVGA3D_CONTEXT_DATA_SIZE); | |
58a0c5f0 | 182 | ret = vmw_resource_init(dev_priv, res, true, |
d80efd5c TH |
183 | res_free, |
184 | dx ? &vmw_dx_context_func : | |
185 | &vmw_gb_context_func); | |
18e4a466 TH |
186 | if (unlikely(ret != 0)) |
187 | goto out_err; | |
58a0c5f0 | 188 | |
18e4a466 TH |
189 | if (dev_priv->has_mob) { |
190 | uctx->man = vmw_cmdbuf_res_man_create(dev_priv); | |
191 | if (unlikely(IS_ERR(uctx->man))) { | |
192 | ret = PTR_ERR(uctx->man); | |
193 | uctx->man = NULL; | |
194 | goto out_err; | |
195 | } | |
58a0c5f0 TH |
196 | } |
197 | ||
d80efd5c TH |
198 | uctx->cbs = vmw_binding_state_alloc(dev_priv); |
199 | if (IS_ERR(uctx->cbs)) { | |
200 | ret = PTR_ERR(uctx->cbs); | |
201 | goto out_err; | |
202 | } | |
203 | ||
204 | spin_lock_init(&uctx->cotable_lock); | |
205 | ||
206 | if (dx) { | |
207 | for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) { | |
208 | uctx->cotables[i] = vmw_cotable_alloc(dev_priv, | |
209 | &uctx->res, i); | |
210 | if (unlikely(uctx->cotables[i] == NULL)) { | |
211 | ret = -ENOMEM; | |
212 | goto out_cotables; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | ||
173fb7d4 | 218 | |
58a0c5f0 TH |
219 | vmw_resource_activate(res, vmw_hw_context_destroy); |
220 | return 0; | |
18e4a466 | 221 | |
d80efd5c TH |
222 | out_cotables: |
223 | vmw_context_cotables_unref(uctx); | |
18e4a466 TH |
224 | out_err: |
225 | if (res_free) | |
226 | res_free(res); | |
227 | else | |
228 | kfree(res); | |
229 | return ret; | |
58a0c5f0 TH |
230 | } |
231 | ||
543831cf TH |
232 | static int vmw_context_init(struct vmw_private *dev_priv, |
233 | struct vmw_resource *res, | |
d80efd5c TH |
234 | void (*res_free)(struct vmw_resource *res), |
235 | bool dx) | |
543831cf TH |
236 | { |
237 | int ret; | |
238 | ||
239 | struct { | |
240 | SVGA3dCmdHeader header; | |
241 | SVGA3dCmdDefineContext body; | |
242 | } *cmd; | |
243 | ||
58a0c5f0 | 244 | if (dev_priv->has_mob) |
d80efd5c | 245 | return vmw_gb_context_init(dev_priv, dx, res, res_free); |
58a0c5f0 | 246 | |
543831cf TH |
247 | ret = vmw_resource_init(dev_priv, res, false, |
248 | res_free, &vmw_legacy_context_func); | |
249 | ||
250 | if (unlikely(ret != 0)) { | |
251 | DRM_ERROR("Failed to allocate a resource id.\n"); | |
252 | goto out_early; | |
253 | } | |
254 | ||
255 | if (unlikely(res->id >= SVGA3D_MAX_CONTEXT_IDS)) { | |
256 | DRM_ERROR("Out of hw context ids.\n"); | |
257 | vmw_resource_unreference(&res); | |
258 | return -ENOMEM; | |
259 | } | |
260 | ||
261 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
262 | if (unlikely(cmd == NULL)) { | |
263 | DRM_ERROR("Fifo reserve failed.\n"); | |
264 | vmw_resource_unreference(&res); | |
265 | return -ENOMEM; | |
266 | } | |
267 | ||
b9eb1a61 TH |
268 | cmd->header.id = SVGA_3D_CMD_CONTEXT_DEFINE; |
269 | cmd->header.size = sizeof(cmd->body); | |
270 | cmd->body.cid = res->id; | |
543831cf TH |
271 | |
272 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
153b3d5b | 273 | vmw_fifo_resource_inc(dev_priv); |
543831cf TH |
274 | vmw_resource_activate(res, vmw_hw_context_destroy); |
275 | return 0; | |
276 | ||
277 | out_early: | |
278 | if (res_free == NULL) | |
279 | kfree(res); | |
280 | else | |
281 | res_free(res); | |
282 | return ret; | |
283 | } | |
284 | ||
543831cf | 285 | |
d80efd5c TH |
286 | /* |
287 | * GB context. | |
288 | */ | |
58a0c5f0 TH |
289 | |
290 | static int vmw_gb_context_create(struct vmw_resource *res) | |
291 | { | |
292 | struct vmw_private *dev_priv = res->dev_priv; | |
293 | int ret; | |
294 | struct { | |
295 | SVGA3dCmdHeader header; | |
296 | SVGA3dCmdDefineGBContext body; | |
297 | } *cmd; | |
298 | ||
299 | if (likely(res->id != -1)) | |
300 | return 0; | |
301 | ||
302 | ret = vmw_resource_alloc_id(res); | |
303 | if (unlikely(ret != 0)) { | |
304 | DRM_ERROR("Failed to allocate a context id.\n"); | |
305 | goto out_no_id; | |
306 | } | |
307 | ||
308 | if (unlikely(res->id >= VMWGFX_NUM_GB_CONTEXT)) { | |
309 | ret = -EBUSY; | |
310 | goto out_no_fifo; | |
311 | } | |
312 | ||
313 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
314 | if (unlikely(cmd == NULL)) { | |
315 | DRM_ERROR("Failed reserving FIFO space for context " | |
316 | "creation.\n"); | |
317 | ret = -ENOMEM; | |
318 | goto out_no_fifo; | |
319 | } | |
320 | ||
321 | cmd->header.id = SVGA_3D_CMD_DEFINE_GB_CONTEXT; | |
322 | cmd->header.size = sizeof(cmd->body); | |
323 | cmd->body.cid = res->id; | |
324 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
153b3d5b | 325 | vmw_fifo_resource_inc(dev_priv); |
58a0c5f0 TH |
326 | |
327 | return 0; | |
328 | ||
329 | out_no_fifo: | |
330 | vmw_resource_release_id(res); | |
331 | out_no_id: | |
332 | return ret; | |
333 | } | |
334 | ||
335 | static int vmw_gb_context_bind(struct vmw_resource *res, | |
336 | struct ttm_validate_buffer *val_buf) | |
337 | { | |
338 | struct vmw_private *dev_priv = res->dev_priv; | |
339 | struct { | |
340 | SVGA3dCmdHeader header; | |
341 | SVGA3dCmdBindGBContext body; | |
342 | } *cmd; | |
343 | struct ttm_buffer_object *bo = val_buf->bo; | |
344 | ||
345 | BUG_ON(bo->mem.mem_type != VMW_PL_MOB); | |
346 | ||
347 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
348 | if (unlikely(cmd == NULL)) { | |
349 | DRM_ERROR("Failed reserving FIFO space for context " | |
350 | "binding.\n"); | |
351 | return -ENOMEM; | |
352 | } | |
58a0c5f0 TH |
353 | cmd->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT; |
354 | cmd->header.size = sizeof(cmd->body); | |
355 | cmd->body.cid = res->id; | |
356 | cmd->body.mobid = bo->mem.start; | |
357 | cmd->body.validContents = res->backup_dirty; | |
358 | res->backup_dirty = false; | |
359 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | static int vmw_gb_context_unbind(struct vmw_resource *res, | |
365 | bool readback, | |
366 | struct ttm_validate_buffer *val_buf) | |
367 | { | |
368 | struct vmw_private *dev_priv = res->dev_priv; | |
369 | struct ttm_buffer_object *bo = val_buf->bo; | |
370 | struct vmw_fence_obj *fence; | |
173fb7d4 TH |
371 | struct vmw_user_context *uctx = |
372 | container_of(res, struct vmw_user_context, res); | |
58a0c5f0 TH |
373 | |
374 | struct { | |
375 | SVGA3dCmdHeader header; | |
376 | SVGA3dCmdReadbackGBContext body; | |
377 | } *cmd1; | |
378 | struct { | |
379 | SVGA3dCmdHeader header; | |
380 | SVGA3dCmdBindGBContext body; | |
381 | } *cmd2; | |
382 | uint32_t submit_size; | |
383 | uint8_t *cmd; | |
384 | ||
385 | ||
386 | BUG_ON(bo->mem.mem_type != VMW_PL_MOB); | |
387 | ||
173fb7d4 | 388 | mutex_lock(&dev_priv->binding_mutex); |
d80efd5c | 389 | vmw_binding_state_scrub(uctx->cbs); |
173fb7d4 | 390 | |
58a0c5f0 TH |
391 | submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0); |
392 | ||
393 | cmd = vmw_fifo_reserve(dev_priv, submit_size); | |
394 | if (unlikely(cmd == NULL)) { | |
395 | DRM_ERROR("Failed reserving FIFO space for context " | |
396 | "unbinding.\n"); | |
173fb7d4 | 397 | mutex_unlock(&dev_priv->binding_mutex); |
58a0c5f0 TH |
398 | return -ENOMEM; |
399 | } | |
400 | ||
401 | cmd2 = (void *) cmd; | |
402 | if (readback) { | |
403 | cmd1 = (void *) cmd; | |
404 | cmd1->header.id = SVGA_3D_CMD_READBACK_GB_CONTEXT; | |
405 | cmd1->header.size = sizeof(cmd1->body); | |
406 | cmd1->body.cid = res->id; | |
407 | cmd2 = (void *) (&cmd1[1]); | |
408 | } | |
409 | cmd2->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT; | |
410 | cmd2->header.size = sizeof(cmd2->body); | |
411 | cmd2->body.cid = res->id; | |
412 | cmd2->body.mobid = SVGA3D_INVALID_ID; | |
413 | ||
414 | vmw_fifo_commit(dev_priv, submit_size); | |
173fb7d4 | 415 | mutex_unlock(&dev_priv->binding_mutex); |
58a0c5f0 TH |
416 | |
417 | /* | |
418 | * Create a fence object and fence the backup buffer. | |
419 | */ | |
420 | ||
421 | (void) vmw_execbuf_fence_commands(NULL, dev_priv, | |
422 | &fence, NULL); | |
423 | ||
424 | vmw_fence_single_bo(bo, fence); | |
425 | ||
426 | if (likely(fence != NULL)) | |
427 | vmw_fence_obj_unreference(&fence); | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static int vmw_gb_context_destroy(struct vmw_resource *res) | |
433 | { | |
434 | struct vmw_private *dev_priv = res->dev_priv; | |
435 | struct { | |
436 | SVGA3dCmdHeader header; | |
437 | SVGA3dCmdDestroyGBContext body; | |
438 | } *cmd; | |
439 | ||
440 | if (likely(res->id == -1)) | |
441 | return 0; | |
442 | ||
443 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
444 | if (unlikely(cmd == NULL)) { | |
445 | DRM_ERROR("Failed reserving FIFO space for context " | |
446 | "destruction.\n"); | |
447 | return -ENOMEM; | |
448 | } | |
449 | ||
450 | cmd->header.id = SVGA_3D_CMD_DESTROY_GB_CONTEXT; | |
451 | cmd->header.size = sizeof(cmd->body); | |
452 | cmd->body.cid = res->id; | |
453 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
454 | if (dev_priv->query_cid == res->id) | |
455 | dev_priv->query_cid_valid = false; | |
456 | vmw_resource_release_id(res); | |
153b3d5b | 457 | vmw_fifo_resource_dec(dev_priv); |
58a0c5f0 TH |
458 | |
459 | return 0; | |
460 | } | |
461 | ||
d80efd5c TH |
462 | /* |
463 | * DX context. | |
464 | */ | |
465 | ||
466 | static int vmw_dx_context_create(struct vmw_resource *res) | |
467 | { | |
468 | struct vmw_private *dev_priv = res->dev_priv; | |
469 | int ret; | |
470 | struct { | |
471 | SVGA3dCmdHeader header; | |
472 | SVGA3dCmdDXDefineContext body; | |
473 | } *cmd; | |
474 | ||
475 | if (likely(res->id != -1)) | |
476 | return 0; | |
477 | ||
478 | ret = vmw_resource_alloc_id(res); | |
479 | if (unlikely(ret != 0)) { | |
480 | DRM_ERROR("Failed to allocate a context id.\n"); | |
481 | goto out_no_id; | |
482 | } | |
483 | ||
484 | if (unlikely(res->id >= VMWGFX_NUM_DXCONTEXT)) { | |
485 | ret = -EBUSY; | |
486 | goto out_no_fifo; | |
487 | } | |
488 | ||
489 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
490 | if (unlikely(cmd == NULL)) { | |
491 | DRM_ERROR("Failed reserving FIFO space for context " | |
492 | "creation.\n"); | |
493 | ret = -ENOMEM; | |
494 | goto out_no_fifo; | |
495 | } | |
496 | ||
497 | cmd->header.id = SVGA_3D_CMD_DX_DEFINE_CONTEXT; | |
498 | cmd->header.size = sizeof(cmd->body); | |
499 | cmd->body.cid = res->id; | |
500 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
501 | vmw_fifo_resource_inc(dev_priv); | |
502 | ||
503 | return 0; | |
504 | ||
505 | out_no_fifo: | |
506 | vmw_resource_release_id(res); | |
507 | out_no_id: | |
508 | return ret; | |
509 | } | |
510 | ||
511 | static int vmw_dx_context_bind(struct vmw_resource *res, | |
512 | struct ttm_validate_buffer *val_buf) | |
513 | { | |
514 | struct vmw_private *dev_priv = res->dev_priv; | |
515 | struct { | |
516 | SVGA3dCmdHeader header; | |
517 | SVGA3dCmdDXBindContext body; | |
518 | } *cmd; | |
519 | struct ttm_buffer_object *bo = val_buf->bo; | |
520 | ||
521 | BUG_ON(bo->mem.mem_type != VMW_PL_MOB); | |
522 | ||
523 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
524 | if (unlikely(cmd == NULL)) { | |
525 | DRM_ERROR("Failed reserving FIFO space for context " | |
526 | "binding.\n"); | |
527 | return -ENOMEM; | |
528 | } | |
529 | ||
530 | cmd->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT; | |
531 | cmd->header.size = sizeof(cmd->body); | |
532 | cmd->body.cid = res->id; | |
533 | cmd->body.mobid = bo->mem.start; | |
534 | cmd->body.validContents = res->backup_dirty; | |
535 | res->backup_dirty = false; | |
536 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
537 | ||
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | /** | |
543 | * vmw_dx_context_scrub_cotables - Scrub all bindings and | |
544 | * cotables from a context | |
545 | * | |
546 | * @ctx: Pointer to the context resource | |
547 | * @readback: Whether to save the otable contents on scrubbing. | |
548 | * | |
549 | * COtables must be unbound before their context, but unbinding requires | |
550 | * the backup buffer being reserved, whereas scrubbing does not. | |
551 | * This function scrubs all cotables of a context, potentially reading back | |
552 | * the contents into their backup buffers. However, scrubbing cotables | |
553 | * also makes the device context invalid, so scrub all bindings first so | |
554 | * that doesn't have to be done later with an invalid context. | |
555 | */ | |
556 | void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx, | |
557 | bool readback) | |
558 | { | |
559 | struct vmw_user_context *uctx = | |
560 | container_of(ctx, struct vmw_user_context, res); | |
561 | int i; | |
562 | ||
563 | vmw_binding_state_scrub(uctx->cbs); | |
564 | for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) { | |
565 | struct vmw_resource *res; | |
566 | ||
567 | /* Avoid racing with ongoing cotable destruction. */ | |
568 | spin_lock(&uctx->cotable_lock); | |
569 | res = uctx->cotables[vmw_cotable_scrub_order[i]]; | |
570 | if (res) | |
571 | res = vmw_resource_reference_unless_doomed(res); | |
572 | spin_unlock(&uctx->cotable_lock); | |
573 | if (!res) | |
574 | continue; | |
575 | ||
576 | WARN_ON(vmw_cotable_scrub(res, readback)); | |
577 | vmw_resource_unreference(&res); | |
578 | } | |
579 | } | |
580 | ||
581 | static int vmw_dx_context_unbind(struct vmw_resource *res, | |
582 | bool readback, | |
583 | struct ttm_validate_buffer *val_buf) | |
584 | { | |
585 | struct vmw_private *dev_priv = res->dev_priv; | |
586 | struct ttm_buffer_object *bo = val_buf->bo; | |
587 | struct vmw_fence_obj *fence; | |
588 | ||
589 | struct { | |
590 | SVGA3dCmdHeader header; | |
591 | SVGA3dCmdDXReadbackContext body; | |
592 | } *cmd1; | |
593 | struct { | |
594 | SVGA3dCmdHeader header; | |
595 | SVGA3dCmdDXBindContext body; | |
596 | } *cmd2; | |
597 | uint32_t submit_size; | |
598 | uint8_t *cmd; | |
599 | ||
600 | ||
601 | BUG_ON(bo->mem.mem_type != VMW_PL_MOB); | |
602 | ||
603 | mutex_lock(&dev_priv->binding_mutex); | |
604 | vmw_dx_context_scrub_cotables(res, readback); | |
605 | ||
606 | submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0); | |
607 | ||
608 | cmd = vmw_fifo_reserve(dev_priv, submit_size); | |
609 | if (unlikely(cmd == NULL)) { | |
610 | DRM_ERROR("Failed reserving FIFO space for context " | |
611 | "unbinding.\n"); | |
612 | mutex_unlock(&dev_priv->binding_mutex); | |
613 | return -ENOMEM; | |
614 | } | |
615 | ||
616 | cmd2 = (void *) cmd; | |
617 | if (readback) { | |
618 | cmd1 = (void *) cmd; | |
619 | cmd1->header.id = SVGA_3D_CMD_DX_READBACK_CONTEXT; | |
620 | cmd1->header.size = sizeof(cmd1->body); | |
621 | cmd1->body.cid = res->id; | |
622 | cmd2 = (void *) (&cmd1[1]); | |
623 | } | |
624 | cmd2->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT; | |
625 | cmd2->header.size = sizeof(cmd2->body); | |
626 | cmd2->body.cid = res->id; | |
627 | cmd2->body.mobid = SVGA3D_INVALID_ID; | |
628 | ||
629 | vmw_fifo_commit(dev_priv, submit_size); | |
630 | mutex_unlock(&dev_priv->binding_mutex); | |
631 | ||
632 | /* | |
633 | * Create a fence object and fence the backup buffer. | |
634 | */ | |
635 | ||
636 | (void) vmw_execbuf_fence_commands(NULL, dev_priv, | |
637 | &fence, NULL); | |
638 | ||
639 | vmw_fence_single_bo(bo, fence); | |
640 | ||
641 | if (likely(fence != NULL)) | |
642 | vmw_fence_obj_unreference(&fence); | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
647 | static int vmw_dx_context_destroy(struct vmw_resource *res) | |
648 | { | |
649 | struct vmw_private *dev_priv = res->dev_priv; | |
650 | struct { | |
651 | SVGA3dCmdHeader header; | |
652 | SVGA3dCmdDXDestroyContext body; | |
653 | } *cmd; | |
654 | ||
655 | if (likely(res->id == -1)) | |
656 | return 0; | |
657 | ||
658 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); | |
659 | if (unlikely(cmd == NULL)) { | |
660 | DRM_ERROR("Failed reserving FIFO space for context " | |
661 | "destruction.\n"); | |
662 | return -ENOMEM; | |
663 | } | |
664 | ||
665 | cmd->header.id = SVGA_3D_CMD_DX_DESTROY_CONTEXT; | |
666 | cmd->header.size = sizeof(cmd->body); | |
667 | cmd->body.cid = res->id; | |
668 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); | |
669 | if (dev_priv->query_cid == res->id) | |
670 | dev_priv->query_cid_valid = false; | |
671 | vmw_resource_release_id(res); | |
672 | vmw_fifo_resource_dec(dev_priv); | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
543831cf TH |
677 | /** |
678 | * User-space context management: | |
679 | */ | |
680 | ||
681 | static struct vmw_resource * | |
682 | vmw_user_context_base_to_res(struct ttm_base_object *base) | |
683 | { | |
684 | return &(container_of(base, struct vmw_user_context, base)->res); | |
685 | } | |
686 | ||
687 | static void vmw_user_context_free(struct vmw_resource *res) | |
688 | { | |
689 | struct vmw_user_context *ctx = | |
690 | container_of(res, struct vmw_user_context, res); | |
691 | struct vmw_private *dev_priv = res->dev_priv; | |
692 | ||
d80efd5c TH |
693 | if (ctx->cbs) |
694 | vmw_binding_state_free(ctx->cbs); | |
543831cf TH |
695 | ttm_base_object_kfree(ctx, base); |
696 | ttm_mem_global_free(vmw_mem_glob(dev_priv), | |
697 | vmw_user_context_size); | |
698 | } | |
699 | ||
700 | /** | |
701 | * This function is called when user space has no more references on the | |
702 | * base object. It releases the base-object's reference on the resource object. | |
703 | */ | |
704 | ||
705 | static void vmw_user_context_base_release(struct ttm_base_object **p_base) | |
706 | { | |
707 | struct ttm_base_object *base = *p_base; | |
708 | struct vmw_user_context *ctx = | |
709 | container_of(base, struct vmw_user_context, base); | |
710 | struct vmw_resource *res = &ctx->res; | |
711 | ||
712 | *p_base = NULL; | |
713 | vmw_resource_unreference(&res); | |
714 | } | |
715 | ||
716 | int vmw_context_destroy_ioctl(struct drm_device *dev, void *data, | |
717 | struct drm_file *file_priv) | |
718 | { | |
719 | struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data; | |
720 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; | |
721 | ||
722 | return ttm_ref_object_base_unref(tfile, arg->cid, TTM_REF_USAGE); | |
723 | } | |
724 | ||
d80efd5c TH |
725 | static int vmw_context_define(struct drm_device *dev, void *data, |
726 | struct drm_file *file_priv, bool dx) | |
543831cf TH |
727 | { |
728 | struct vmw_private *dev_priv = vmw_priv(dev); | |
729 | struct vmw_user_context *ctx; | |
730 | struct vmw_resource *res; | |
731 | struct vmw_resource *tmp; | |
732 | struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data; | |
733 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; | |
543831cf TH |
734 | int ret; |
735 | ||
d80efd5c TH |
736 | if (!dev_priv->has_dx && dx) { |
737 | DRM_ERROR("DX contexts not supported by device.\n"); | |
738 | return -EINVAL; | |
739 | } | |
543831cf TH |
740 | |
741 | /* | |
742 | * Approximate idr memory usage with 128 bytes. It will be limited | |
743 | * by maximum number_of contexts anyway. | |
744 | */ | |
745 | ||
746 | if (unlikely(vmw_user_context_size == 0)) | |
18e4a466 TH |
747 | vmw_user_context_size = ttm_round_pot(sizeof(*ctx)) + 128 + |
748 | ((dev_priv->has_mob) ? vmw_cmdbuf_res_man_size() : 0); | |
543831cf | 749 | |
294adf7d | 750 | ret = ttm_read_lock(&dev_priv->reservation_sem, true); |
543831cf TH |
751 | if (unlikely(ret != 0)) |
752 | return ret; | |
753 | ||
754 | ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), | |
755 | vmw_user_context_size, | |
756 | false, true); | |
757 | if (unlikely(ret != 0)) { | |
758 | if (ret != -ERESTARTSYS) | |
759 | DRM_ERROR("Out of graphics memory for context" | |
760 | " creation.\n"); | |
761 | goto out_unlock; | |
762 | } | |
763 | ||
764 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
765 | if (unlikely(ctx == NULL)) { | |
766 | ttm_mem_global_free(vmw_mem_glob(dev_priv), | |
767 | vmw_user_context_size); | |
768 | ret = -ENOMEM; | |
769 | goto out_unlock; | |
770 | } | |
771 | ||
772 | res = &ctx->res; | |
773 | ctx->base.shareable = false; | |
774 | ctx->base.tfile = NULL; | |
775 | ||
776 | /* | |
777 | * From here on, the destructor takes over resource freeing. | |
778 | */ | |
779 | ||
d80efd5c | 780 | ret = vmw_context_init(dev_priv, res, vmw_user_context_free, dx); |
543831cf TH |
781 | if (unlikely(ret != 0)) |
782 | goto out_unlock; | |
783 | ||
784 | tmp = vmw_resource_reference(&ctx->res); | |
785 | ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT, | |
786 | &vmw_user_context_base_release, NULL); | |
787 | ||
788 | if (unlikely(ret != 0)) { | |
789 | vmw_resource_unreference(&tmp); | |
790 | goto out_err; | |
791 | } | |
792 | ||
793 | arg->cid = ctx->base.hash.key; | |
794 | out_err: | |
795 | vmw_resource_unreference(&res); | |
796 | out_unlock: | |
294adf7d | 797 | ttm_read_unlock(&dev_priv->reservation_sem); |
543831cf | 798 | return ret; |
b5c3b1a6 TH |
799 | } |
800 | ||
d80efd5c TH |
801 | int vmw_context_define_ioctl(struct drm_device *dev, void *data, |
802 | struct drm_file *file_priv) | |
b5c3b1a6 | 803 | { |
d80efd5c | 804 | return vmw_context_define(dev, data, file_priv, false); |
b5c3b1a6 TH |
805 | } |
806 | ||
d80efd5c TH |
807 | int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data, |
808 | struct drm_file *file_priv) | |
b5c3b1a6 | 809 | { |
d80efd5c TH |
810 | union drm_vmw_extended_context_arg *arg = (typeof(arg)) data; |
811 | struct drm_vmw_context_arg *rep = &arg->rep; | |
812 | ||
813 | switch (arg->req) { | |
814 | case drm_vmw_context_legacy: | |
815 | return vmw_context_define(dev, rep, file_priv, false); | |
816 | case drm_vmw_context_dx: | |
817 | return vmw_context_define(dev, rep, file_priv, true); | |
b5c3b1a6 | 818 | default: |
173fb7d4 | 819 | break; |
30f82d81 | 820 | } |
d80efd5c | 821 | return -EINVAL; |
30f82d81 TH |
822 | } |
823 | ||
173fb7d4 | 824 | /** |
d80efd5c | 825 | * vmw_context_binding_list - Return a list of context bindings |
173fb7d4 | 826 | * |
d80efd5c | 827 | * @ctx: The context resource |
173fb7d4 | 828 | * |
d80efd5c TH |
829 | * Returns the current list of bindings of the given context. Note that |
830 | * this list becomes stale as soon as the dev_priv::binding_mutex is unlocked. | |
173fb7d4 | 831 | */ |
d80efd5c | 832 | struct list_head *vmw_context_binding_list(struct vmw_resource *ctx) |
173fb7d4 TH |
833 | { |
834 | struct vmw_user_context *uctx = | |
835 | container_of(ctx, struct vmw_user_context, res); | |
173fb7d4 | 836 | |
d80efd5c | 837 | return vmw_binding_state_list(uctx->cbs); |
b5c3b1a6 | 838 | } |
30f82d81 | 839 | |
d80efd5c | 840 | struct vmw_cmdbuf_res_manager *vmw_context_res_man(struct vmw_resource *ctx) |
30f82d81 | 841 | { |
d80efd5c TH |
842 | return container_of(ctx, struct vmw_user_context, res)->man; |
843 | } | |
30f82d81 | 844 | |
d80efd5c TH |
845 | struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx, |
846 | SVGACOTableType cotable_type) | |
847 | { | |
848 | if (cotable_type >= SVGA_COTABLE_DX10_MAX) | |
849 | return ERR_PTR(-EINVAL); | |
30f82d81 | 850 | |
d80efd5c TH |
851 | return vmw_resource_reference |
852 | (container_of(ctx, struct vmw_user_context, res)-> | |
853 | cotables[cotable_type]); | |
30f82d81 TH |
854 | } |
855 | ||
856 | /** | |
d80efd5c TH |
857 | * vmw_context_binding_state - |
858 | * Return a pointer to a context binding state structure | |
30f82d81 TH |
859 | * |
860 | * @ctx: The context resource | |
861 | * | |
d80efd5c TH |
862 | * Returns the current state of bindings of the given context. Note that |
863 | * this state becomes stale as soon as the dev_priv::binding_mutex is unlocked. | |
30f82d81 | 864 | */ |
d80efd5c TH |
865 | struct vmw_ctx_binding_state * |
866 | vmw_context_binding_state(struct vmw_resource *ctx) | |
18e4a466 | 867 | { |
d80efd5c | 868 | return container_of(ctx, struct vmw_user_context, res)->cbs; |
18e4a466 | 869 | } |