1/* $NetBSD: vmwgfx_so.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $ */ 2 3// SPDX-License-Identifier: GPL-2.0 OR MIT 4/************************************************************************** 5 * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: vmwgfx_so.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $"); 31 32#include "vmwgfx_drv.h" 33#include "vmwgfx_resource_priv.h" 34#include "vmwgfx_so.h" 35#include "vmwgfx_binding.h" 36 37/* 38 * The currently only reason we need to keep track of views is that if we 39 * destroy a hardware surface, all views pointing to it must also be destroyed, 40 * otherwise the device will error. 41 * So in particuar if a surface is evicted, we must destroy all views pointing 42 * to it, and all context bindings of that view. Similarly we must restore 43 * the view bindings, views and surfaces pointed to by the views when a 44 * context is referenced in the command stream. 45 */ 46 47/** 48 * struct vmw_view - view metadata 49 * 50 * @res: The struct vmw_resource we derive from 51 * @ctx: Non-refcounted pointer to the context this view belongs to. 52 * @srf: Refcounted pointer to the surface pointed to by this view. 53 * @cotable: Refcounted pointer to the cotable holding this view. 54 * @srf_head: List head for the surface-to-view list. 55 * @cotable_head: List head for the cotable-to_view list. 56 * @view_type: View type. 57 * @view_id: User-space per context view id. Currently used also as per 58 * context device view id. 59 * @cmd_size: Size of the SVGA3D define view command that we've copied from the 60 * command stream. 61 * @committed: Whether the view is actually created or pending creation at the 62 * device level. 63 * @cmd: The SVGA3D define view command copied from the command stream. 64 */ 65struct vmw_view { 66 struct rcu_head rcu; 67 struct vmw_resource res; 68 struct vmw_resource *ctx; /* Immutable */ 69 struct vmw_resource *srf; /* Immutable */ 70 struct vmw_resource *cotable; /* Immutable */ 71 struct list_head srf_head; /* Protected by binding_mutex */ 72 struct list_head cotable_head; /* Protected by binding_mutex */ 73 unsigned view_type; /* Immutable */ 74 unsigned view_id; /* Immutable */ 75 u32 cmd_size; /* Immutable */ 76 bool committed; /* Protected by binding_mutex */ 77 u32 cmd[1]; /* Immutable */ 78}; 79 80static int vmw_view_create(struct vmw_resource *res); 81static int vmw_view_destroy(struct vmw_resource *res); 82static void vmw_hw_view_destroy(struct vmw_resource *res); 83static void vmw_view_commit_notify(struct vmw_resource *res, 84 enum vmw_cmdbuf_res_state state); 85 86static const struct vmw_res_func vmw_view_func = { 87 .res_type = vmw_res_view, 88 .needs_backup = false, 89 .may_evict = false, 90 .type_name = "DX view", 91 .backup_placement = NULL, 92 .create = vmw_view_create, 93 .commit_notify = vmw_view_commit_notify, 94}; 95 96/** 97 * struct vmw_view - view define command body stub 98 * 99 * @view_id: The device id of the view being defined 100 * @sid: The surface id of the view being defined 101 * 102 * This generic struct is used by the code to change @view_id and @sid of a 103 * saved view define command. 104 */ 105struct vmw_view_define { 106 uint32 view_id; 107 uint32 sid; 108}; 109 110/** 111 * vmw_view - Convert a struct vmw_resource to a struct vmw_view 112 * 113 * @res: Pointer to the resource to convert. 114 * 115 * Returns a pointer to a struct vmw_view. 116 */ 117static struct vmw_view *vmw_view(struct vmw_resource *res) 118{ 119 return container_of(res, struct vmw_view, res); 120} 121 122/** 123 * vmw_view_commit_notify - Notify that a view operation has been committed to 124 * hardware from a user-supplied command stream. 125 * 126 * @res: Pointer to the view resource. 127 * @state: Indicating whether a creation or removal has been committed. 128 * 129 */ 130static void vmw_view_commit_notify(struct vmw_resource *res, 131 enum vmw_cmdbuf_res_state state) 132{ 133 struct vmw_view *view = vmw_view(res); 134 struct vmw_private *dev_priv = res->dev_priv; 135 136 mutex_lock(&dev_priv->binding_mutex); 137 if (state == VMW_CMDBUF_RES_ADD) { 138 struct vmw_surface *srf = vmw_res_to_srf(view->srf); 139 140 list_add_tail(&view->srf_head, &srf->view_list); 141 vmw_cotable_add_resource(view->cotable, &view->cotable_head); 142 view->committed = true; 143 res->id = view->view_id; 144 145 } else { 146 list_del_init(&view->cotable_head); 147 list_del_init(&view->srf_head); 148 view->committed = false; 149 res->id = -1; 150 } 151 mutex_unlock(&dev_priv->binding_mutex); 152} 153 154/** 155 * vmw_view_create - Create a hardware view. 156 * 157 * @res: Pointer to the view resource. 158 * 159 * Create a hardware view. Typically used if that view has previously been 160 * destroyed by an eviction operation. 161 */ 162static int vmw_view_create(struct vmw_resource *res) 163{ 164 struct vmw_view *view = vmw_view(res); 165 struct vmw_surface *srf = vmw_res_to_srf(view->srf); 166 struct vmw_private *dev_priv = res->dev_priv; 167 struct { 168 SVGA3dCmdHeader header; 169 struct vmw_view_define body; 170 } *cmd; 171 172 mutex_lock(&dev_priv->binding_mutex); 173 if (!view->committed) { 174 mutex_unlock(&dev_priv->binding_mutex); 175 return 0; 176 } 177 178 cmd = VMW_FIFO_RESERVE_DX(res->dev_priv, view->cmd_size, view->ctx->id); 179 if (!cmd) { 180 mutex_unlock(&dev_priv->binding_mutex); 181 return -ENOMEM; 182 } 183 184 memcpy(cmd, &view->cmd, view->cmd_size); 185 WARN_ON(cmd->body.view_id != view->view_id); 186 /* Sid may have changed due to surface eviction. */ 187 WARN_ON(view->srf->id == SVGA3D_INVALID_ID); 188 cmd->body.sid = view->srf->id; 189 vmw_fifo_commit(res->dev_priv, view->cmd_size); 190 res->id = view->view_id; 191 list_add_tail(&view->srf_head, &srf->view_list); 192 vmw_cotable_add_resource(view->cotable, &view->cotable_head); 193 mutex_unlock(&dev_priv->binding_mutex); 194 195 return 0; 196} 197 198/** 199 * vmw_view_destroy - Destroy a hardware view. 200 * 201 * @res: Pointer to the view resource. 202 * 203 * Destroy a hardware view. Typically used on unexpected termination of the 204 * owning process or if the surface the view is pointing to is destroyed. 205 */ 206static int vmw_view_destroy(struct vmw_resource *res) 207{ 208 struct vmw_private *dev_priv = res->dev_priv; 209 struct vmw_view *view = vmw_view(res); 210 struct { 211 SVGA3dCmdHeader header; 212 union vmw_view_destroy body; 213 } *cmd; 214 215 lockdep_assert_held_once(&dev_priv->binding_mutex); 216 vmw_binding_res_list_scrub(&res->binding_head); 217 218 if (!view->committed || res->id == -1) 219 return 0; 220 221 cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), view->ctx->id); 222 if (!cmd) 223 return -ENOMEM; 224 225 cmd->header.id = vmw_view_destroy_cmds[view->view_type]; 226 cmd->header.size = sizeof(cmd->body); 227 cmd->body.view_id = view->view_id; 228 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 229 res->id = -1; 230 list_del_init(&view->cotable_head); 231 list_del_init(&view->srf_head); 232 233 return 0; 234} 235 236/** 237 * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup. 238 * 239 * @res: Pointer to the view resource. 240 * 241 * Destroy a hardware view if it's still present. 242 */ 243static void vmw_hw_view_destroy(struct vmw_resource *res) 244{ 245 struct vmw_private *dev_priv = res->dev_priv; 246 247 mutex_lock(&dev_priv->binding_mutex); 248 WARN_ON(vmw_view_destroy(res)); 249 res->id = -1; 250 mutex_unlock(&dev_priv->binding_mutex); 251} 252 253/** 254 * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager 255 * 256 * @user_key: The user-space id used for the view. 257 * @view_type: The view type. 258 * 259 * Destroy a hardware view if it's still present. 260 */ 261static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type) 262{ 263 return user_key | (view_type << 20); 264} 265 266/** 267 * vmw_view_id_ok - Basic view id and type range checks. 268 * 269 * @user_key: The user-space id used for the view. 270 * @view_type: The view type. 271 * 272 * Checks that the view id and type (typically provided by user-space) is 273 * valid. 274 */ 275static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type) 276{ 277 return (user_key < SVGA_COTABLE_MAX_IDS && 278 view_type < vmw_view_max); 279} 280 281/** 282 * vmw_view_res_free - resource res_free callback for view resources 283 * 284 * @res: Pointer to a struct vmw_resource 285 * 286 * Frees memory and memory accounting held by a struct vmw_view. 287 */ 288static void vmw_view_res_free(struct vmw_resource *res) 289{ 290 struct vmw_view *view = vmw_view(res); 291 size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size; 292 struct vmw_private *dev_priv = res->dev_priv; 293 294 vmw_resource_unreference(&view->cotable); 295 vmw_resource_unreference(&view->srf); 296 kfree_rcu(view, rcu); 297 ttm_mem_global_free(vmw_mem_glob(dev_priv), size); 298} 299 300/** 301 * vmw_view_add - Create a view resource and stage it for addition 302 * as a command buffer managed resource. 303 * 304 * @man: Pointer to the compat shader manager identifying the shader namespace. 305 * @ctx: Pointer to a struct vmw_resource identifying the active context. 306 * @srf: Pointer to a struct vmw_resource identifying the surface the view 307 * points to. 308 * @view_type: The view type deduced from the view create command. 309 * @user_key: The key that is used to identify the shader. The key is 310 * unique to the view type and to the context. 311 * @cmd: Pointer to the view create command in the command stream. 312 * @cmd_size: Size of the view create command in the command stream. 313 * @list: Caller's list of staged command buffer resource actions. 314 */ 315int vmw_view_add(struct vmw_cmdbuf_res_manager *man, 316 struct vmw_resource *ctx, 317 struct vmw_resource *srf, 318 enum vmw_view_type view_type, 319 u32 user_key, 320 const void *cmd, 321 size_t cmd_size, 322 struct list_head *list) 323{ 324 static const size_t vmw_view_define_sizes[] = { 325 [vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView), 326 [vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView), 327 [vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView) 328 }; 329 330 struct vmw_private *dev_priv = ctx->dev_priv; 331 struct vmw_resource *res; 332 struct vmw_view *view; 333 struct ttm_operation_ctx ttm_opt_ctx = { 334 .interruptible = true, 335 .no_wait_gpu = false 336 }; 337 size_t size; 338 int ret; 339 340 if (cmd_size != vmw_view_define_sizes[view_type] + 341 sizeof(SVGA3dCmdHeader)) { 342 VMW_DEBUG_USER("Illegal view create command size.\n"); 343 return -EINVAL; 344 } 345 346 if (!vmw_view_id_ok(user_key, view_type)) { 347 VMW_DEBUG_USER("Illegal view add view id.\n"); 348 return -EINVAL; 349 } 350 351 size = offsetof(struct vmw_view, cmd) + cmd_size; 352 353 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ttm_opt_ctx); 354 if (ret) { 355 if (ret != -ERESTARTSYS) 356 DRM_ERROR("Out of graphics memory for view creation\n"); 357 return ret; 358 } 359 360 view = kmalloc(size, GFP_KERNEL); 361 if (!view) { 362 ttm_mem_global_free(vmw_mem_glob(dev_priv), size); 363 return -ENOMEM; 364 } 365 366 res = &view->res; 367 view->ctx = ctx; 368 view->srf = vmw_resource_reference(srf); 369 view->cotable = vmw_resource_reference 370 (vmw_context_cotable(ctx, vmw_view_cotables[view_type])); 371 view->view_type = view_type; 372 view->view_id = user_key; 373 view->cmd_size = cmd_size; 374 view->committed = false; 375 INIT_LIST_HEAD(&view->srf_head); 376 INIT_LIST_HEAD(&view->cotable_head); 377 memcpy(&view->cmd, cmd, cmd_size); 378 ret = vmw_resource_init(dev_priv, res, true, 379 vmw_view_res_free, &vmw_view_func); 380 if (ret) 381 goto out_resource_init; 382 383 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view, 384 vmw_view_key(user_key, view_type), 385 res, list); 386 if (ret) 387 goto out_resource_init; 388 389 res->id = view->view_id; 390 res->hw_destroy = vmw_hw_view_destroy; 391 392out_resource_init: 393 vmw_resource_unreference(&res); 394 395 return ret; 396} 397 398/** 399 * vmw_view_remove - Stage a view for removal. 400 * 401 * @man: Pointer to the view manager identifying the shader namespace. 402 * @user_key: The key that is used to identify the view. The key is 403 * unique to the view type. 404 * @view_type: View type 405 * @list: Caller's list of staged command buffer resource actions. 406 * @res_p: If the resource is in an already committed state, points to the 407 * struct vmw_resource on successful return. The pointer will be 408 * non ref-counted. 409 */ 410int vmw_view_remove(struct vmw_cmdbuf_res_manager *man, 411 u32 user_key, enum vmw_view_type view_type, 412 struct list_head *list, 413 struct vmw_resource **res_p) 414{ 415 if (!vmw_view_id_ok(user_key, view_type)) { 416 VMW_DEBUG_USER("Illegal view remove view id.\n"); 417 return -EINVAL; 418 } 419 420 return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view, 421 vmw_view_key(user_key, view_type), 422 list, res_p); 423} 424 425/** 426 * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable. 427 * 428 * @dev_priv: Pointer to a device private struct. 429 * @list: List of views belonging to a cotable. 430 * @readback: Unused. Needed for function interface only. 431 * 432 * This function evicts all views belonging to a cotable. 433 * It must be called with the binding_mutex held, and the caller must hold 434 * a reference to the view resource. This is typically called before the 435 * cotable is paged out. 436 */ 437void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv, 438 struct list_head *list, 439 bool readback) 440{ 441 struct vmw_view *entry, *next; 442 443 lockdep_assert_held_once(&dev_priv->binding_mutex); 444 445 list_for_each_entry_safe(entry, next, list, cotable_head) 446 WARN_ON(vmw_view_destroy(&entry->res)); 447} 448 449/** 450 * vmw_view_surface_list_destroy - Evict all views pointing to a surface 451 * 452 * @dev_priv: Pointer to a device private struct. 453 * @list: List of views pointing to a surface. 454 * 455 * This function evicts all views pointing to a surface. This is typically 456 * called before the surface is evicted. 457 */ 458void vmw_view_surface_list_destroy(struct vmw_private *dev_priv, 459 struct list_head *list) 460{ 461 struct vmw_view *entry, *next; 462 463 lockdep_assert_held_once(&dev_priv->binding_mutex); 464 465 list_for_each_entry_safe(entry, next, list, srf_head) 466 WARN_ON(vmw_view_destroy(&entry->res)); 467} 468 469/** 470 * vmw_view_srf - Return a non-refcounted pointer to the surface a view is 471 * pointing to. 472 * 473 * @res: pointer to a view resource. 474 * 475 * Note that the view itself is holding a reference, so as long 476 * the view resource is alive, the surface resource will be. 477 */ 478struct vmw_resource *vmw_view_srf(struct vmw_resource *res) 479{ 480 return vmw_view(res)->srf; 481} 482 483/** 484 * vmw_view_lookup - Look up a view. 485 * 486 * @man: The context's cmdbuf ref manager. 487 * @view_type: The view type. 488 * @user_key: The view user id. 489 * 490 * returns a refcounted pointer to a view or an error pointer if not found. 491 */ 492struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man, 493 enum vmw_view_type view_type, 494 u32 user_key) 495{ 496 return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view, 497 vmw_view_key(user_key, view_type)); 498} 499 500/** 501 * vmw_view_dirtying - Return whether a view type is dirtying its resource 502 * @res: Pointer to the view 503 * 504 * Each time a resource is put on the validation list as the result of a 505 * view pointing to it, we need to determine whether that resource will 506 * be dirtied (written to by the GPU) as a result of the corresponding 507 * GPU operation. Currently only rendertarget- and depth-stencil views are 508 * capable of dirtying its resource. 509 * 510 * Return: Whether the view type of @res dirties the resource it points to. 511 */ 512u32 vmw_view_dirtying(struct vmw_resource *res) 513{ 514 static u32 view_is_dirtying[vmw_view_max] = { 515 [vmw_view_rt] = VMW_RES_DIRTY_SET, 516 [vmw_view_ds] = VMW_RES_DIRTY_SET, 517 }; 518 519 /* Update this function as we add more view types */ 520 BUILD_BUG_ON(vmw_view_max != 3); 521 return view_is_dirtying[vmw_view(res)->view_type]; 522} 523 524const u32 vmw_view_destroy_cmds[] = { 525 [vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW, 526 [vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW, 527 [vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW, 528}; 529 530const SVGACOTableType vmw_view_cotables[] = { 531 [vmw_view_sr] = SVGA_COTABLE_SRVIEW, 532 [vmw_view_rt] = SVGA_COTABLE_RTVIEW, 533 [vmw_view_ds] = SVGA_COTABLE_DSVIEW, 534}; 535 536const SVGACOTableType vmw_so_cotables[] = { 537 [vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT, 538 [vmw_so_bs] = SVGA_COTABLE_BLENDSTATE, 539 [vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL, 540 [vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE, 541 [vmw_so_ss] = SVGA_COTABLE_SAMPLER, 542 [vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT 543}; 544 545 546/* To remove unused function warning */ 547static void vmw_so_build_asserts(void) __attribute__((used)); 548 549 550/* 551 * This function is unused at run-time, and only used to dump various build 552 * asserts important for code optimization assumptions. 553 */ 554static void vmw_so_build_asserts(void) 555{ 556 /* Assert that our vmw_view_cmd_to_type() function is correct. */ 557 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW != 558 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1); 559 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW != 560 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2); 561 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW != 562 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3); 563 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW != 564 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4); 565 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW != 566 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5); 567 568 /* Assert that our "one body fits all" assumption is valid */ 569 BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32)); 570 571 /* Assert that the view key space can hold all view ids. */ 572 BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1)); 573 574 /* 575 * Assert that the offset of sid in all view define commands 576 * is what we assume it to be. 577 */ 578 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) != 579 offsetof(SVGA3dCmdDXDefineShaderResourceView, sid)); 580 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) != 581 offsetof(SVGA3dCmdDXDefineRenderTargetView, sid)); 582 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) != 583 offsetof(SVGA3dCmdDXDefineDepthStencilView, sid)); 584} 585