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