1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA 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 <drm/ttm/ttm_placement.h> 29 30 #include "vmwgfx_binding.h" 31 #include "vmwgfx_bo.h" 32 #include "vmwgfx_drv.h" 33 #include "vmwgfx_resource_priv.h" 34 35 struct vmw_shader { 36 struct vmw_resource res; 37 SVGA3dShaderType type; 38 uint32_t size; 39 uint8_t num_input_sig; 40 uint8_t num_output_sig; 41 }; 42 43 struct vmw_user_shader { 44 struct ttm_base_object base; 45 struct vmw_shader shader; 46 }; 47 48 struct vmw_dx_shader { 49 struct vmw_resource res; 50 struct vmw_resource *ctx; 51 struct vmw_resource *cotable; 52 u32 id; 53 bool committed; 54 struct list_head cotable_head; 55 }; 56 57 static void vmw_user_shader_free(struct vmw_resource *res); 58 static struct vmw_resource * 59 vmw_user_shader_base_to_res(struct ttm_base_object *base); 60 61 static int vmw_gb_shader_create(struct vmw_resource *res); 62 static int vmw_gb_shader_bind(struct vmw_resource *res, 63 struct ttm_validate_buffer *val_buf); 64 static int vmw_gb_shader_unbind(struct vmw_resource *res, 65 bool readback, 66 struct ttm_validate_buffer *val_buf); 67 static int vmw_gb_shader_destroy(struct vmw_resource *res); 68 69 static int vmw_dx_shader_create(struct vmw_resource *res); 70 static int vmw_dx_shader_bind(struct vmw_resource *res, 71 struct ttm_validate_buffer *val_buf); 72 static int vmw_dx_shader_unbind(struct vmw_resource *res, 73 bool readback, 74 struct ttm_validate_buffer *val_buf); 75 static void vmw_dx_shader_commit_notify(struct vmw_resource *res, 76 enum vmw_cmdbuf_res_state state); 77 static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type); 78 static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type); 79 80 static const struct vmw_user_resource_conv user_shader_conv = { 81 .object_type = VMW_RES_SHADER, 82 .base_obj_to_res = vmw_user_shader_base_to_res, 83 .res_free = vmw_user_shader_free 84 }; 85 86 const struct vmw_user_resource_conv *user_shader_converter = 87 &user_shader_conv; 88 89 90 static const struct vmw_res_func vmw_gb_shader_func = { 91 .res_type = vmw_res_shader, 92 .needs_backup = true, 93 .may_evict = true, 94 .prio = 3, 95 .dirty_prio = 3, 96 .type_name = "guest backed shaders", 97 .backup_placement = &vmw_mob_placement, 98 .create = vmw_gb_shader_create, 99 .destroy = vmw_gb_shader_destroy, 100 .bind = vmw_gb_shader_bind, 101 .unbind = vmw_gb_shader_unbind 102 }; 103 104 static const struct vmw_res_func vmw_dx_shader_func = { 105 .res_type = vmw_res_shader, 106 .needs_backup = true, 107 .may_evict = true, 108 .prio = 3, 109 .dirty_prio = 3, 110 .type_name = "dx shaders", 111 .backup_placement = &vmw_mob_placement, 112 .create = vmw_dx_shader_create, 113 /* 114 * The destroy callback is only called with a committed resource on 115 * context destroy, in which case we destroy the cotable anyway, 116 * so there's no need to destroy DX shaders separately. 117 */ 118 .destroy = NULL, 119 .bind = vmw_dx_shader_bind, 120 .unbind = vmw_dx_shader_unbind, 121 .commit_notify = vmw_dx_shader_commit_notify, 122 }; 123 124 /* 125 * Shader management: 126 */ 127 128 static inline struct vmw_shader * 129 vmw_res_to_shader(struct vmw_resource *res) 130 { 131 return container_of(res, struct vmw_shader, res); 132 } 133 134 /** 135 * vmw_res_to_dx_shader - typecast a struct vmw_resource to a 136 * struct vmw_dx_shader 137 * 138 * @res: Pointer to the struct vmw_resource. 139 */ 140 static inline struct vmw_dx_shader * 141 vmw_res_to_dx_shader(struct vmw_resource *res) 142 { 143 return container_of(res, struct vmw_dx_shader, res); 144 } 145 146 static void vmw_hw_shader_destroy(struct vmw_resource *res) 147 { 148 if (likely(res->func->destroy)) 149 (void) res->func->destroy(res); 150 else 151 res->id = -1; 152 } 153 154 155 static int vmw_gb_shader_init(struct vmw_private *dev_priv, 156 struct vmw_resource *res, 157 uint32_t size, 158 uint64_t offset, 159 SVGA3dShaderType type, 160 uint8_t num_input_sig, 161 uint8_t num_output_sig, 162 struct vmw_bo *byte_code, 163 void (*res_free) (struct vmw_resource *res)) 164 { 165 struct vmw_shader *shader = vmw_res_to_shader(res); 166 int ret; 167 168 ret = vmw_resource_init(dev_priv, res, true, res_free, 169 &vmw_gb_shader_func); 170 171 if (unlikely(ret != 0)) { 172 if (res_free) 173 res_free(res); 174 else 175 kfree(res); 176 return ret; 177 } 178 179 res->backup_size = size; 180 if (byte_code) { 181 res->backup = vmw_bo_reference(byte_code); 182 res->backup_offset = offset; 183 } 184 shader->size = size; 185 shader->type = type; 186 shader->num_input_sig = num_input_sig; 187 shader->num_output_sig = num_output_sig; 188 189 res->hw_destroy = vmw_hw_shader_destroy; 190 return 0; 191 } 192 193 /* 194 * GB shader code: 195 */ 196 197 static int vmw_gb_shader_create(struct vmw_resource *res) 198 { 199 struct vmw_private *dev_priv = res->dev_priv; 200 struct vmw_shader *shader = vmw_res_to_shader(res); 201 int ret; 202 struct { 203 SVGA3dCmdHeader header; 204 SVGA3dCmdDefineGBShader body; 205 } *cmd; 206 207 if (likely(res->id != -1)) 208 return 0; 209 210 ret = vmw_resource_alloc_id(res); 211 if (unlikely(ret != 0)) { 212 DRM_ERROR("Failed to allocate a shader id.\n"); 213 goto out_no_id; 214 } 215 216 if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) { 217 ret = -EBUSY; 218 goto out_no_fifo; 219 } 220 221 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 222 if (unlikely(cmd == NULL)) { 223 ret = -ENOMEM; 224 goto out_no_fifo; 225 } 226 227 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER; 228 cmd->header.size = sizeof(cmd->body); 229 cmd->body.shid = res->id; 230 cmd->body.type = shader->type; 231 cmd->body.sizeInBytes = shader->size; 232 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 233 vmw_fifo_resource_inc(dev_priv); 234 235 return 0; 236 237 out_no_fifo: 238 vmw_resource_release_id(res); 239 out_no_id: 240 return ret; 241 } 242 243 static int vmw_gb_shader_bind(struct vmw_resource *res, 244 struct ttm_validate_buffer *val_buf) 245 { 246 struct vmw_private *dev_priv = res->dev_priv; 247 struct { 248 SVGA3dCmdHeader header; 249 SVGA3dCmdBindGBShader body; 250 } *cmd; 251 struct ttm_buffer_object *bo = val_buf->bo; 252 253 BUG_ON(bo->resource->mem_type != VMW_PL_MOB); 254 255 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 256 if (unlikely(cmd == NULL)) 257 return -ENOMEM; 258 259 cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER; 260 cmd->header.size = sizeof(cmd->body); 261 cmd->body.shid = res->id; 262 cmd->body.mobid = bo->resource->start; 263 cmd->body.offsetInBytes = res->backup_offset; 264 res->backup_dirty = false; 265 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 266 267 return 0; 268 } 269 270 static int vmw_gb_shader_unbind(struct vmw_resource *res, 271 bool readback, 272 struct ttm_validate_buffer *val_buf) 273 { 274 struct vmw_private *dev_priv = res->dev_priv; 275 struct { 276 SVGA3dCmdHeader header; 277 SVGA3dCmdBindGBShader body; 278 } *cmd; 279 struct vmw_fence_obj *fence; 280 281 BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB); 282 283 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 284 if (unlikely(cmd == NULL)) 285 return -ENOMEM; 286 287 cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER; 288 cmd->header.size = sizeof(cmd->body); 289 cmd->body.shid = res->id; 290 cmd->body.mobid = SVGA3D_INVALID_ID; 291 cmd->body.offsetInBytes = 0; 292 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 293 294 /* 295 * Create a fence object and fence the backup buffer. 296 */ 297 298 (void) vmw_execbuf_fence_commands(NULL, dev_priv, 299 &fence, NULL); 300 301 vmw_bo_fence_single(val_buf->bo, fence); 302 303 if (likely(fence != NULL)) 304 vmw_fence_obj_unreference(&fence); 305 306 return 0; 307 } 308 309 static int vmw_gb_shader_destroy(struct vmw_resource *res) 310 { 311 struct vmw_private *dev_priv = res->dev_priv; 312 struct { 313 SVGA3dCmdHeader header; 314 SVGA3dCmdDestroyGBShader body; 315 } *cmd; 316 317 if (likely(res->id == -1)) 318 return 0; 319 320 mutex_lock(&dev_priv->binding_mutex); 321 vmw_binding_res_list_scrub(&res->binding_head); 322 323 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 324 if (unlikely(cmd == NULL)) { 325 mutex_unlock(&dev_priv->binding_mutex); 326 return -ENOMEM; 327 } 328 329 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER; 330 cmd->header.size = sizeof(cmd->body); 331 cmd->body.shid = res->id; 332 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 333 mutex_unlock(&dev_priv->binding_mutex); 334 vmw_resource_release_id(res); 335 vmw_fifo_resource_dec(dev_priv); 336 337 return 0; 338 } 339 340 /* 341 * DX shader code: 342 */ 343 344 /** 345 * vmw_dx_shader_commit_notify - Notify that a shader operation has been 346 * committed to hardware from a user-supplied command stream. 347 * 348 * @res: Pointer to the shader resource. 349 * @state: Indicating whether a creation or removal has been committed. 350 * 351 */ 352 static void vmw_dx_shader_commit_notify(struct vmw_resource *res, 353 enum vmw_cmdbuf_res_state state) 354 { 355 struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 356 struct vmw_private *dev_priv = res->dev_priv; 357 358 if (state == VMW_CMDBUF_RES_ADD) { 359 mutex_lock(&dev_priv->binding_mutex); 360 vmw_cotable_add_resource(shader->cotable, 361 &shader->cotable_head); 362 shader->committed = true; 363 res->id = shader->id; 364 mutex_unlock(&dev_priv->binding_mutex); 365 } else { 366 mutex_lock(&dev_priv->binding_mutex); 367 list_del_init(&shader->cotable_head); 368 shader->committed = false; 369 res->id = -1; 370 mutex_unlock(&dev_priv->binding_mutex); 371 } 372 } 373 374 /** 375 * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader. 376 * 377 * @res: The shader resource 378 * 379 * This function reverts a scrub operation. 380 */ 381 static int vmw_dx_shader_unscrub(struct vmw_resource *res) 382 { 383 struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 384 struct vmw_private *dev_priv = res->dev_priv; 385 struct { 386 SVGA3dCmdHeader header; 387 SVGA3dCmdDXBindShader body; 388 } *cmd; 389 390 if (!list_empty(&shader->cotable_head) || !shader->committed) 391 return 0; 392 393 cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id); 394 if (unlikely(cmd == NULL)) 395 return -ENOMEM; 396 397 cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER; 398 cmd->header.size = sizeof(cmd->body); 399 cmd->body.cid = shader->ctx->id; 400 cmd->body.shid = shader->id; 401 cmd->body.mobid = res->backup->base.resource->start; 402 cmd->body.offsetInBytes = res->backup_offset; 403 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 404 405 vmw_cotable_add_resource(shader->cotable, &shader->cotable_head); 406 407 return 0; 408 } 409 410 /** 411 * vmw_dx_shader_create - The DX shader create callback 412 * 413 * @res: The DX shader resource 414 * 415 * The create callback is called as part of resource validation and 416 * makes sure that we unscrub the shader if it's previously been scrubbed. 417 */ 418 static int vmw_dx_shader_create(struct vmw_resource *res) 419 { 420 struct vmw_private *dev_priv = res->dev_priv; 421 struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 422 int ret = 0; 423 424 WARN_ON_ONCE(!shader->committed); 425 426 if (vmw_resource_mob_attached(res)) { 427 mutex_lock(&dev_priv->binding_mutex); 428 ret = vmw_dx_shader_unscrub(res); 429 mutex_unlock(&dev_priv->binding_mutex); 430 } 431 432 res->id = shader->id; 433 return ret; 434 } 435 436 /** 437 * vmw_dx_shader_bind - The DX shader bind callback 438 * 439 * @res: The DX shader resource 440 * @val_buf: Pointer to the validate buffer. 441 * 442 */ 443 static int vmw_dx_shader_bind(struct vmw_resource *res, 444 struct ttm_validate_buffer *val_buf) 445 { 446 struct vmw_private *dev_priv = res->dev_priv; 447 struct ttm_buffer_object *bo = val_buf->bo; 448 449 BUG_ON(bo->resource->mem_type != VMW_PL_MOB); 450 mutex_lock(&dev_priv->binding_mutex); 451 vmw_dx_shader_unscrub(res); 452 mutex_unlock(&dev_priv->binding_mutex); 453 454 return 0; 455 } 456 457 /** 458 * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader. 459 * 460 * @res: The shader resource 461 * 462 * This function unbinds a MOB from the DX shader without requiring the 463 * MOB dma_buffer to be reserved. The driver still considers the MOB bound. 464 * However, once the driver eventually decides to unbind the MOB, it doesn't 465 * need to access the context. 466 */ 467 static int vmw_dx_shader_scrub(struct vmw_resource *res) 468 { 469 struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 470 struct vmw_private *dev_priv = res->dev_priv; 471 struct { 472 SVGA3dCmdHeader header; 473 SVGA3dCmdDXBindShader body; 474 } *cmd; 475 476 if (list_empty(&shader->cotable_head)) 477 return 0; 478 479 WARN_ON_ONCE(!shader->committed); 480 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 481 if (unlikely(cmd == NULL)) 482 return -ENOMEM; 483 484 cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER; 485 cmd->header.size = sizeof(cmd->body); 486 cmd->body.cid = shader->ctx->id; 487 cmd->body.shid = res->id; 488 cmd->body.mobid = SVGA3D_INVALID_ID; 489 cmd->body.offsetInBytes = 0; 490 vmw_cmd_commit(dev_priv, sizeof(*cmd)); 491 res->id = -1; 492 list_del_init(&shader->cotable_head); 493 494 return 0; 495 } 496 497 /** 498 * vmw_dx_shader_unbind - The dx shader unbind callback. 499 * 500 * @res: The shader resource 501 * @readback: Whether this is a readback unbind. Currently unused. 502 * @val_buf: MOB buffer information. 503 */ 504 static int vmw_dx_shader_unbind(struct vmw_resource *res, 505 bool readback, 506 struct ttm_validate_buffer *val_buf) 507 { 508 struct vmw_private *dev_priv = res->dev_priv; 509 struct vmw_fence_obj *fence; 510 int ret; 511 512 BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB); 513 514 mutex_lock(&dev_priv->binding_mutex); 515 ret = vmw_dx_shader_scrub(res); 516 mutex_unlock(&dev_priv->binding_mutex); 517 518 if (ret) 519 return ret; 520 521 (void) vmw_execbuf_fence_commands(NULL, dev_priv, 522 &fence, NULL); 523 vmw_bo_fence_single(val_buf->bo, fence); 524 525 if (likely(fence != NULL)) 526 vmw_fence_obj_unreference(&fence); 527 528 return 0; 529 } 530 531 /** 532 * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for 533 * DX shaders. 534 * 535 * @dev_priv: Pointer to device private structure. 536 * @list: The list of cotable resources. 537 * @readback: Whether the call was part of a readback unbind. 538 * 539 * Scrubs all shader MOBs so that any subsequent shader unbind or shader 540 * destroy operation won't need to swap in the context. 541 */ 542 void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv, 543 struct list_head *list, 544 bool readback) 545 { 546 struct vmw_dx_shader *entry, *next; 547 548 lockdep_assert_held_once(&dev_priv->binding_mutex); 549 550 list_for_each_entry_safe(entry, next, list, cotable_head) { 551 WARN_ON(vmw_dx_shader_scrub(&entry->res)); 552 if (!readback) 553 entry->committed = false; 554 } 555 } 556 557 /** 558 * vmw_dx_shader_res_free - The DX shader free callback 559 * 560 * @res: The shader resource 561 * 562 * Frees the DX shader resource. 563 */ 564 static void vmw_dx_shader_res_free(struct vmw_resource *res) 565 { 566 struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 567 568 vmw_resource_unreference(&shader->cotable); 569 kfree(shader); 570 } 571 572 /** 573 * vmw_dx_shader_add - Add a shader resource as a command buffer managed 574 * resource. 575 * 576 * @man: The command buffer resource manager. 577 * @ctx: Pointer to the context resource. 578 * @user_key: The id used for this shader. 579 * @shader_type: The shader type. 580 * @list: The list of staged command buffer managed resources. 581 */ 582 int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man, 583 struct vmw_resource *ctx, 584 u32 user_key, 585 SVGA3dShaderType shader_type, 586 struct list_head *list) 587 { 588 struct vmw_dx_shader *shader; 589 struct vmw_resource *res; 590 struct vmw_private *dev_priv = ctx->dev_priv; 591 int ret; 592 593 if (!vmw_shader_id_ok(user_key, shader_type)) 594 return -EINVAL; 595 596 shader = kmalloc(sizeof(*shader), GFP_KERNEL); 597 if (!shader) { 598 return -ENOMEM; 599 } 600 601 res = &shader->res; 602 shader->ctx = ctx; 603 shader->cotable = vmw_resource_reference 604 (vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER)); 605 shader->id = user_key; 606 shader->committed = false; 607 INIT_LIST_HEAD(&shader->cotable_head); 608 ret = vmw_resource_init(dev_priv, res, true, 609 vmw_dx_shader_res_free, &vmw_dx_shader_func); 610 if (ret) 611 goto out_resource_init; 612 613 /* 614 * The user_key name-space is not per shader type for DX shaders, 615 * so when hashing, use a single zero shader type. 616 */ 617 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader, 618 vmw_shader_key(user_key, 0), 619 res, list); 620 if (ret) 621 goto out_resource_init; 622 623 res->id = shader->id; 624 res->hw_destroy = vmw_hw_shader_destroy; 625 626 out_resource_init: 627 vmw_resource_unreference(&res); 628 629 return ret; 630 } 631 632 633 634 /* 635 * User-space shader management: 636 */ 637 638 static struct vmw_resource * 639 vmw_user_shader_base_to_res(struct ttm_base_object *base) 640 { 641 return &(container_of(base, struct vmw_user_shader, base)-> 642 shader.res); 643 } 644 645 static void vmw_user_shader_free(struct vmw_resource *res) 646 { 647 struct vmw_user_shader *ushader = 648 container_of(res, struct vmw_user_shader, shader.res); 649 650 ttm_base_object_kfree(ushader, base); 651 } 652 653 static void vmw_shader_free(struct vmw_resource *res) 654 { 655 struct vmw_shader *shader = vmw_res_to_shader(res); 656 657 kfree(shader); 658 } 659 660 /* 661 * This function is called when user space has no more references on the 662 * base object. It releases the base-object's reference on the resource object. 663 */ 664 665 static void vmw_user_shader_base_release(struct ttm_base_object **p_base) 666 { 667 struct ttm_base_object *base = *p_base; 668 struct vmw_resource *res = vmw_user_shader_base_to_res(base); 669 670 *p_base = NULL; 671 vmw_resource_unreference(&res); 672 } 673 674 int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data, 675 struct drm_file *file_priv) 676 { 677 struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data; 678 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 679 680 return ttm_ref_object_base_unref(tfile, arg->handle); 681 } 682 683 static int vmw_user_shader_alloc(struct vmw_private *dev_priv, 684 struct vmw_bo *buffer, 685 size_t shader_size, 686 size_t offset, 687 SVGA3dShaderType shader_type, 688 uint8_t num_input_sig, 689 uint8_t num_output_sig, 690 struct ttm_object_file *tfile, 691 u32 *handle) 692 { 693 struct vmw_user_shader *ushader; 694 struct vmw_resource *res, *tmp; 695 int ret; 696 697 ushader = kzalloc(sizeof(*ushader), GFP_KERNEL); 698 if (unlikely(!ushader)) { 699 ret = -ENOMEM; 700 goto out; 701 } 702 703 res = &ushader->shader.res; 704 ushader->base.shareable = false; 705 ushader->base.tfile = NULL; 706 707 /* 708 * From here on, the destructor takes over resource freeing. 709 */ 710 711 ret = vmw_gb_shader_init(dev_priv, res, shader_size, 712 offset, shader_type, num_input_sig, 713 num_output_sig, buffer, 714 vmw_user_shader_free); 715 if (unlikely(ret != 0)) 716 goto out; 717 718 tmp = vmw_resource_reference(res); 719 ret = ttm_base_object_init(tfile, &ushader->base, false, 720 VMW_RES_SHADER, 721 &vmw_user_shader_base_release); 722 723 if (unlikely(ret != 0)) { 724 vmw_resource_unreference(&tmp); 725 goto out_err; 726 } 727 728 if (handle) 729 *handle = ushader->base.handle; 730 out_err: 731 vmw_resource_unreference(&res); 732 out: 733 return ret; 734 } 735 736 737 static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv, 738 struct vmw_bo *buffer, 739 size_t shader_size, 740 size_t offset, 741 SVGA3dShaderType shader_type) 742 { 743 struct vmw_shader *shader; 744 struct vmw_resource *res; 745 int ret; 746 747 shader = kzalloc(sizeof(*shader), GFP_KERNEL); 748 if (unlikely(!shader)) { 749 ret = -ENOMEM; 750 goto out_err; 751 } 752 753 res = &shader->res; 754 755 /* 756 * From here on, the destructor takes over resource freeing. 757 */ 758 ret = vmw_gb_shader_init(dev_priv, res, shader_size, 759 offset, shader_type, 0, 0, buffer, 760 vmw_shader_free); 761 762 out_err: 763 return ret ? ERR_PTR(ret) : res; 764 } 765 766 767 static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv, 768 enum drm_vmw_shader_type shader_type_drm, 769 u32 buffer_handle, size_t size, size_t offset, 770 uint8_t num_input_sig, uint8_t num_output_sig, 771 uint32_t *shader_handle) 772 { 773 struct vmw_private *dev_priv = vmw_priv(dev); 774 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 775 struct vmw_bo *buffer = NULL; 776 SVGA3dShaderType shader_type; 777 int ret; 778 779 if (buffer_handle != SVGA3D_INVALID_ID) { 780 ret = vmw_user_bo_lookup(file_priv, buffer_handle, &buffer); 781 if (unlikely(ret != 0)) { 782 VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n"); 783 return ret; 784 } 785 786 if ((u64)buffer->base.base.size < (u64)size + (u64)offset) { 787 VMW_DEBUG_USER("Illegal buffer- or shader size.\n"); 788 ret = -EINVAL; 789 goto out_bad_arg; 790 } 791 } 792 793 switch (shader_type_drm) { 794 case drm_vmw_shader_type_vs: 795 shader_type = SVGA3D_SHADERTYPE_VS; 796 break; 797 case drm_vmw_shader_type_ps: 798 shader_type = SVGA3D_SHADERTYPE_PS; 799 break; 800 default: 801 VMW_DEBUG_USER("Illegal shader type.\n"); 802 ret = -EINVAL; 803 goto out_bad_arg; 804 } 805 806 ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset, 807 shader_type, num_input_sig, 808 num_output_sig, tfile, shader_handle); 809 out_bad_arg: 810 vmw_bo_unreference(&buffer); 811 return ret; 812 } 813 814 /** 815 * vmw_shader_id_ok - Check whether a compat shader user key and 816 * shader type are within valid bounds. 817 * 818 * @user_key: User space id of the shader. 819 * @shader_type: Shader type. 820 * 821 * Returns true if valid false if not. 822 */ 823 static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type) 824 { 825 return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16; 826 } 827 828 /** 829 * vmw_shader_key - Compute a hash key suitable for a compat shader. 830 * 831 * @user_key: User space id of the shader. 832 * @shader_type: Shader type. 833 * 834 * Returns a hash key suitable for a command buffer managed resource 835 * manager hash table. 836 */ 837 static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type) 838 { 839 return user_key | (shader_type << 20); 840 } 841 842 /** 843 * vmw_shader_remove - Stage a compat shader for removal. 844 * 845 * @man: Pointer to the compat shader manager identifying the shader namespace. 846 * @user_key: The key that is used to identify the shader. The key is 847 * unique to the shader type. 848 * @shader_type: Shader type. 849 * @list: Caller's list of staged command buffer resource actions. 850 */ 851 int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man, 852 u32 user_key, SVGA3dShaderType shader_type, 853 struct list_head *list) 854 { 855 struct vmw_resource *dummy; 856 857 if (!vmw_shader_id_ok(user_key, shader_type)) 858 return -EINVAL; 859 860 return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader, 861 vmw_shader_key(user_key, shader_type), 862 list, &dummy); 863 } 864 865 /** 866 * vmw_compat_shader_add - Create a compat shader and stage it for addition 867 * as a command buffer managed resource. 868 * 869 * @dev_priv: Pointer to device private structure. 870 * @man: Pointer to the compat shader manager identifying the shader namespace. 871 * @user_key: The key that is used to identify the shader. The key is 872 * unique to the shader type. 873 * @bytecode: Pointer to the bytecode of the shader. 874 * @shader_type: Shader type. 875 * @size: Command size. 876 * @list: Caller's list of staged command buffer resource actions. 877 * 878 */ 879 int vmw_compat_shader_add(struct vmw_private *dev_priv, 880 struct vmw_cmdbuf_res_manager *man, 881 u32 user_key, const void *bytecode, 882 SVGA3dShaderType shader_type, 883 size_t size, 884 struct list_head *list) 885 { 886 struct ttm_operation_ctx ctx = { false, true }; 887 struct vmw_bo *buf; 888 struct ttm_bo_kmap_obj map; 889 bool is_iomem; 890 int ret; 891 struct vmw_resource *res; 892 893 if (!vmw_shader_id_ok(user_key, shader_type)) 894 return -EINVAL; 895 896 ret = vmw_bo_create(dev_priv, size, &vmw_sys_placement, 897 true, true, &buf); 898 if (unlikely(ret != 0)) 899 goto out; 900 901 ret = ttm_bo_reserve(&buf->base, false, true, NULL); 902 if (unlikely(ret != 0)) 903 goto no_reserve; 904 905 /* Map and copy shader bytecode. */ 906 ret = ttm_bo_kmap(&buf->base, 0, PFN_UP(size), &map); 907 if (unlikely(ret != 0)) { 908 ttm_bo_unreserve(&buf->base); 909 goto no_reserve; 910 } 911 912 memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size); 913 WARN_ON(is_iomem); 914 915 ttm_bo_kunmap(&map); 916 ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx); 917 WARN_ON(ret != 0); 918 ttm_bo_unreserve(&buf->base); 919 920 res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type); 921 if (unlikely(ret != 0)) 922 goto no_reserve; 923 924 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader, 925 vmw_shader_key(user_key, shader_type), 926 res, list); 927 vmw_resource_unreference(&res); 928 no_reserve: 929 vmw_bo_unreference(&buf); 930 out: 931 return ret; 932 } 933 934 /** 935 * vmw_shader_lookup - Look up a compat shader 936 * 937 * @man: Pointer to the command buffer managed resource manager identifying 938 * the shader namespace. 939 * @user_key: The user space id of the shader. 940 * @shader_type: The shader type. 941 * 942 * Returns a refcounted pointer to a struct vmw_resource if the shader was 943 * found. An error pointer otherwise. 944 */ 945 struct vmw_resource * 946 vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man, 947 u32 user_key, 948 SVGA3dShaderType shader_type) 949 { 950 if (!vmw_shader_id_ok(user_key, shader_type)) 951 return ERR_PTR(-EINVAL); 952 953 return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader, 954 vmw_shader_key(user_key, shader_type)); 955 } 956 957 int vmw_shader_define_ioctl(struct drm_device *dev, void *data, 958 struct drm_file *file_priv) 959 { 960 struct drm_vmw_shader_create_arg *arg = 961 (struct drm_vmw_shader_create_arg *)data; 962 963 return vmw_shader_define(dev, file_priv, arg->shader_type, 964 arg->buffer_handle, 965 arg->size, arg->offset, 966 0, 0, 967 &arg->shader_handle); 968 } 969