1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT 2c74c162fSThomas Hellstrom /************************************************************************** 3c74c162fSThomas Hellstrom * 4dff96888SDirk Hohndel (VMware) * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA 5c74c162fSThomas Hellstrom * 6c74c162fSThomas Hellstrom * Permission is hereby granted, free of charge, to any person obtaining a 7c74c162fSThomas Hellstrom * copy of this software and associated documentation files (the 8c74c162fSThomas Hellstrom * "Software"), to deal in the Software without restriction, including 9c74c162fSThomas Hellstrom * without limitation the rights to use, copy, modify, merge, publish, 10c74c162fSThomas Hellstrom * distribute, sub license, and/or sell copies of the Software, and to 11c74c162fSThomas Hellstrom * permit persons to whom the Software is furnished to do so, subject to 12c74c162fSThomas Hellstrom * the following conditions: 13c74c162fSThomas Hellstrom * 14c74c162fSThomas Hellstrom * The above copyright notice and this permission notice (including the 15c74c162fSThomas Hellstrom * next paragraph) shall be included in all copies or substantial portions 16c74c162fSThomas Hellstrom * of the Software. 17c74c162fSThomas Hellstrom * 18c74c162fSThomas Hellstrom * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19c74c162fSThomas Hellstrom * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20c74c162fSThomas Hellstrom * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21c74c162fSThomas Hellstrom * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22c74c162fSThomas Hellstrom * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23c74c162fSThomas Hellstrom * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24c74c162fSThomas Hellstrom * USE OR OTHER DEALINGS IN THE SOFTWARE. 25c74c162fSThomas Hellstrom * 26c74c162fSThomas Hellstrom **************************************************************************/ 27c74c162fSThomas Hellstrom 28008be682SMasahiro Yamada #include <drm/ttm/ttm_placement.h> 29008be682SMasahiro Yamada 30c74c162fSThomas Hellstrom #include "vmwgfx_drv.h" 31c74c162fSThomas Hellstrom #include "vmwgfx_resource_priv.h" 32d80efd5cSThomas Hellstrom #include "vmwgfx_binding.h" 33c74c162fSThomas Hellstrom 34c74c162fSThomas Hellstrom struct vmw_shader { 35c74c162fSThomas Hellstrom struct vmw_resource res; 36c74c162fSThomas Hellstrom SVGA3dShaderType type; 37c74c162fSThomas Hellstrom uint32_t size; 38d80efd5cSThomas Hellstrom uint8_t num_input_sig; 39d80efd5cSThomas Hellstrom uint8_t num_output_sig; 40c74c162fSThomas Hellstrom }; 41c74c162fSThomas Hellstrom 42c74c162fSThomas Hellstrom struct vmw_user_shader { 43c74c162fSThomas Hellstrom struct ttm_base_object base; 44c74c162fSThomas Hellstrom struct vmw_shader shader; 45c74c162fSThomas Hellstrom }; 46c74c162fSThomas Hellstrom 47d80efd5cSThomas Hellstrom struct vmw_dx_shader { 48d80efd5cSThomas Hellstrom struct vmw_resource res; 49d80efd5cSThomas Hellstrom struct vmw_resource *ctx; 50d80efd5cSThomas Hellstrom struct vmw_resource *cotable; 51d80efd5cSThomas Hellstrom u32 id; 52d80efd5cSThomas Hellstrom bool committed; 53d80efd5cSThomas Hellstrom struct list_head cotable_head; 54d80efd5cSThomas Hellstrom }; 55d80efd5cSThomas Hellstrom 56c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res); 57c74c162fSThomas Hellstrom static struct vmw_resource * 58c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base); 59c74c162fSThomas Hellstrom 60c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res); 61c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res, 62c74c162fSThomas Hellstrom struct ttm_validate_buffer *val_buf); 63c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res, 64c74c162fSThomas Hellstrom bool readback, 65c74c162fSThomas Hellstrom struct ttm_validate_buffer *val_buf); 66c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res); 67c74c162fSThomas Hellstrom 68d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res); 69d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res, 70d80efd5cSThomas Hellstrom struct ttm_validate_buffer *val_buf); 71d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res, 72d80efd5cSThomas Hellstrom bool readback, 73d80efd5cSThomas Hellstrom struct ttm_validate_buffer *val_buf); 74d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res, 75d80efd5cSThomas Hellstrom enum vmw_cmdbuf_res_state state); 76d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type); 77d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type); 78d80efd5cSThomas Hellstrom 79c74c162fSThomas Hellstrom static const struct vmw_user_resource_conv user_shader_conv = { 80c74c162fSThomas Hellstrom .object_type = VMW_RES_SHADER, 81c74c162fSThomas Hellstrom .base_obj_to_res = vmw_user_shader_base_to_res, 82c74c162fSThomas Hellstrom .res_free = vmw_user_shader_free 83c74c162fSThomas Hellstrom }; 84c74c162fSThomas Hellstrom 85c74c162fSThomas Hellstrom const struct vmw_user_resource_conv *user_shader_converter = 86c74c162fSThomas Hellstrom &user_shader_conv; 87c74c162fSThomas Hellstrom 88c74c162fSThomas Hellstrom 89c74c162fSThomas Hellstrom static const struct vmw_res_func vmw_gb_shader_func = { 90c74c162fSThomas Hellstrom .res_type = vmw_res_shader, 91c74c162fSThomas Hellstrom .needs_backup = true, 92c74c162fSThomas Hellstrom .may_evict = true, 93a0a63940SThomas Hellstrom .prio = 3, 94a0a63940SThomas Hellstrom .dirty_prio = 3, 95c74c162fSThomas Hellstrom .type_name = "guest backed shaders", 96c74c162fSThomas Hellstrom .backup_placement = &vmw_mob_placement, 97c74c162fSThomas Hellstrom .create = vmw_gb_shader_create, 98c74c162fSThomas Hellstrom .destroy = vmw_gb_shader_destroy, 99c74c162fSThomas Hellstrom .bind = vmw_gb_shader_bind, 100c74c162fSThomas Hellstrom .unbind = vmw_gb_shader_unbind 101c74c162fSThomas Hellstrom }; 102c74c162fSThomas Hellstrom 103d80efd5cSThomas Hellstrom static const struct vmw_res_func vmw_dx_shader_func = { 104d80efd5cSThomas Hellstrom .res_type = vmw_res_shader, 105d80efd5cSThomas Hellstrom .needs_backup = true, 106a0a63940SThomas Hellstrom .may_evict = true, 107a0a63940SThomas Hellstrom .prio = 3, 108a0a63940SThomas Hellstrom .dirty_prio = 3, 109d80efd5cSThomas Hellstrom .type_name = "dx shaders", 110d80efd5cSThomas Hellstrom .backup_placement = &vmw_mob_placement, 111d80efd5cSThomas Hellstrom .create = vmw_dx_shader_create, 112d80efd5cSThomas Hellstrom /* 113d80efd5cSThomas Hellstrom * The destroy callback is only called with a committed resource on 114d80efd5cSThomas Hellstrom * context destroy, in which case we destroy the cotable anyway, 115d80efd5cSThomas Hellstrom * so there's no need to destroy DX shaders separately. 116d80efd5cSThomas Hellstrom */ 117d80efd5cSThomas Hellstrom .destroy = NULL, 118d80efd5cSThomas Hellstrom .bind = vmw_dx_shader_bind, 119d80efd5cSThomas Hellstrom .unbind = vmw_dx_shader_unbind, 120d80efd5cSThomas Hellstrom .commit_notify = vmw_dx_shader_commit_notify, 121d80efd5cSThomas Hellstrom }; 122d80efd5cSThomas Hellstrom 123ad2ae415SLee Jones /* 124c74c162fSThomas Hellstrom * Shader management: 125c74c162fSThomas Hellstrom */ 126c74c162fSThomas Hellstrom 127c74c162fSThomas Hellstrom static inline struct vmw_shader * 128c74c162fSThomas Hellstrom vmw_res_to_shader(struct vmw_resource *res) 129c74c162fSThomas Hellstrom { 130c74c162fSThomas Hellstrom return container_of(res, struct vmw_shader, res); 131c74c162fSThomas Hellstrom } 132c74c162fSThomas Hellstrom 133d80efd5cSThomas Hellstrom /** 134d80efd5cSThomas Hellstrom * vmw_res_to_dx_shader - typecast a struct vmw_resource to a 135d80efd5cSThomas Hellstrom * struct vmw_dx_shader 136d80efd5cSThomas Hellstrom * 137d80efd5cSThomas Hellstrom * @res: Pointer to the struct vmw_resource. 138d80efd5cSThomas Hellstrom */ 139d80efd5cSThomas Hellstrom static inline struct vmw_dx_shader * 140d80efd5cSThomas Hellstrom vmw_res_to_dx_shader(struct vmw_resource *res) 141d80efd5cSThomas Hellstrom { 142d80efd5cSThomas Hellstrom return container_of(res, struct vmw_dx_shader, res); 143d80efd5cSThomas Hellstrom } 144d80efd5cSThomas Hellstrom 145c74c162fSThomas Hellstrom static void vmw_hw_shader_destroy(struct vmw_resource *res) 146c74c162fSThomas Hellstrom { 147d80efd5cSThomas Hellstrom if (likely(res->func->destroy)) 148d80efd5cSThomas Hellstrom (void) res->func->destroy(res); 149d80efd5cSThomas Hellstrom else 150d80efd5cSThomas Hellstrom res->id = -1; 151c74c162fSThomas Hellstrom } 152c74c162fSThomas Hellstrom 153d80efd5cSThomas Hellstrom 154c74c162fSThomas Hellstrom static int vmw_gb_shader_init(struct vmw_private *dev_priv, 155c74c162fSThomas Hellstrom struct vmw_resource *res, 156c74c162fSThomas Hellstrom uint32_t size, 157c74c162fSThomas Hellstrom uint64_t offset, 158c74c162fSThomas Hellstrom SVGA3dShaderType type, 159d80efd5cSThomas Hellstrom uint8_t num_input_sig, 160d80efd5cSThomas Hellstrom uint8_t num_output_sig, 161f1d34bfdSThomas Hellstrom struct vmw_buffer_object *byte_code, 162c74c162fSThomas Hellstrom void (*res_free) (struct vmw_resource *res)) 163c74c162fSThomas Hellstrom { 164c74c162fSThomas Hellstrom struct vmw_shader *shader = vmw_res_to_shader(res); 165c74c162fSThomas Hellstrom int ret; 166c74c162fSThomas Hellstrom 167d80efd5cSThomas Hellstrom ret = vmw_resource_init(dev_priv, res, true, res_free, 168d80efd5cSThomas Hellstrom &vmw_gb_shader_func); 169c74c162fSThomas Hellstrom 170c74c162fSThomas Hellstrom if (unlikely(ret != 0)) { 171c74c162fSThomas Hellstrom if (res_free) 172c74c162fSThomas Hellstrom res_free(res); 173c74c162fSThomas Hellstrom else 174c74c162fSThomas Hellstrom kfree(res); 175c74c162fSThomas Hellstrom return ret; 176c74c162fSThomas Hellstrom } 177c74c162fSThomas Hellstrom 178c74c162fSThomas Hellstrom res->backup_size = size; 179c74c162fSThomas Hellstrom if (byte_code) { 180f1d34bfdSThomas Hellstrom res->backup = vmw_bo_reference(byte_code); 181c74c162fSThomas Hellstrom res->backup_offset = offset; 182c74c162fSThomas Hellstrom } 183c74c162fSThomas Hellstrom shader->size = size; 184c74c162fSThomas Hellstrom shader->type = type; 185d80efd5cSThomas Hellstrom shader->num_input_sig = num_input_sig; 186d80efd5cSThomas Hellstrom shader->num_output_sig = num_output_sig; 187c74c162fSThomas Hellstrom 18813289241SThomas Hellstrom res->hw_destroy = vmw_hw_shader_destroy; 189c74c162fSThomas Hellstrom return 0; 190c74c162fSThomas Hellstrom } 191c74c162fSThomas Hellstrom 192d80efd5cSThomas Hellstrom /* 193d80efd5cSThomas Hellstrom * GB shader code: 194d80efd5cSThomas Hellstrom */ 195d80efd5cSThomas Hellstrom 196c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res) 197c74c162fSThomas Hellstrom { 198c74c162fSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 199c74c162fSThomas Hellstrom struct vmw_shader *shader = vmw_res_to_shader(res); 200c74c162fSThomas Hellstrom int ret; 201c74c162fSThomas Hellstrom struct { 202c74c162fSThomas Hellstrom SVGA3dCmdHeader header; 203c74c162fSThomas Hellstrom SVGA3dCmdDefineGBShader body; 204c74c162fSThomas Hellstrom } *cmd; 205c74c162fSThomas Hellstrom 206c74c162fSThomas Hellstrom if (likely(res->id != -1)) 207c74c162fSThomas Hellstrom return 0; 208c74c162fSThomas Hellstrom 209c74c162fSThomas Hellstrom ret = vmw_resource_alloc_id(res); 210c74c162fSThomas Hellstrom if (unlikely(ret != 0)) { 211c74c162fSThomas Hellstrom DRM_ERROR("Failed to allocate a shader id.\n"); 212c74c162fSThomas Hellstrom goto out_no_id; 213c74c162fSThomas Hellstrom } 214c74c162fSThomas Hellstrom 215c74c162fSThomas Hellstrom if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) { 216c74c162fSThomas Hellstrom ret = -EBUSY; 217c74c162fSThomas Hellstrom goto out_no_fifo; 218c74c162fSThomas Hellstrom } 219c74c162fSThomas Hellstrom 2208426ed9cSZack Rusin cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 221c74c162fSThomas Hellstrom if (unlikely(cmd == NULL)) { 222c74c162fSThomas Hellstrom ret = -ENOMEM; 223c74c162fSThomas Hellstrom goto out_no_fifo; 224c74c162fSThomas Hellstrom } 225c74c162fSThomas Hellstrom 226c74c162fSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER; 227c74c162fSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 228c74c162fSThomas Hellstrom cmd->body.shid = res->id; 229c74c162fSThomas Hellstrom cmd->body.type = shader->type; 230c74c162fSThomas Hellstrom cmd->body.sizeInBytes = shader->size; 2318426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 232153b3d5bSThomas Hellstrom vmw_fifo_resource_inc(dev_priv); 233c74c162fSThomas Hellstrom 234c74c162fSThomas Hellstrom return 0; 235c74c162fSThomas Hellstrom 236c74c162fSThomas Hellstrom out_no_fifo: 237c74c162fSThomas Hellstrom vmw_resource_release_id(res); 238c74c162fSThomas Hellstrom out_no_id: 239c74c162fSThomas Hellstrom return ret; 240c74c162fSThomas Hellstrom } 241c74c162fSThomas Hellstrom 242c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res, 243c74c162fSThomas Hellstrom struct ttm_validate_buffer *val_buf) 244c74c162fSThomas Hellstrom { 245c74c162fSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 246c74c162fSThomas Hellstrom struct { 247c74c162fSThomas Hellstrom SVGA3dCmdHeader header; 248c74c162fSThomas Hellstrom SVGA3dCmdBindGBShader body; 249c74c162fSThomas Hellstrom } *cmd; 250c74c162fSThomas Hellstrom struct ttm_buffer_object *bo = val_buf->bo; 251c74c162fSThomas Hellstrom 252d3116756SChristian König BUG_ON(bo->resource->mem_type != VMW_PL_MOB); 253c74c162fSThomas Hellstrom 2548426ed9cSZack Rusin cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 25511c45419SDeepak Rawat if (unlikely(cmd == NULL)) 256c74c162fSThomas Hellstrom return -ENOMEM; 257c74c162fSThomas Hellstrom 258c74c162fSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER; 259c74c162fSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 260c74c162fSThomas Hellstrom cmd->body.shid = res->id; 261d3116756SChristian König cmd->body.mobid = bo->resource->start; 262b8ccd1e4SThomas Hellstrom cmd->body.offsetInBytes = res->backup_offset; 263c74c162fSThomas Hellstrom res->backup_dirty = false; 2648426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 265c74c162fSThomas Hellstrom 266c74c162fSThomas Hellstrom return 0; 267c74c162fSThomas Hellstrom } 268c74c162fSThomas Hellstrom 269c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res, 270c74c162fSThomas Hellstrom bool readback, 271c74c162fSThomas Hellstrom struct ttm_validate_buffer *val_buf) 272c74c162fSThomas Hellstrom { 273c74c162fSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 274c74c162fSThomas Hellstrom struct { 275c74c162fSThomas Hellstrom SVGA3dCmdHeader header; 276c74c162fSThomas Hellstrom SVGA3dCmdBindGBShader body; 277c74c162fSThomas Hellstrom } *cmd; 278c74c162fSThomas Hellstrom struct vmw_fence_obj *fence; 279c74c162fSThomas Hellstrom 280d3116756SChristian König BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB); 281c74c162fSThomas Hellstrom 2828426ed9cSZack Rusin cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 28311c45419SDeepak Rawat if (unlikely(cmd == NULL)) 284c74c162fSThomas Hellstrom return -ENOMEM; 285c74c162fSThomas Hellstrom 286c74c162fSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER; 287c74c162fSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 288c74c162fSThomas Hellstrom cmd->body.shid = res->id; 289c74c162fSThomas Hellstrom cmd->body.mobid = SVGA3D_INVALID_ID; 290c74c162fSThomas Hellstrom cmd->body.offsetInBytes = 0; 2918426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 292c74c162fSThomas Hellstrom 293c74c162fSThomas Hellstrom /* 294c74c162fSThomas Hellstrom * Create a fence object and fence the backup buffer. 295c74c162fSThomas Hellstrom */ 296c74c162fSThomas Hellstrom 297c74c162fSThomas Hellstrom (void) vmw_execbuf_fence_commands(NULL, dev_priv, 298c74c162fSThomas Hellstrom &fence, NULL); 299c74c162fSThomas Hellstrom 300e9431ea5SThomas Hellstrom vmw_bo_fence_single(val_buf->bo, fence); 301c74c162fSThomas Hellstrom 302c74c162fSThomas Hellstrom if (likely(fence != NULL)) 303c74c162fSThomas Hellstrom vmw_fence_obj_unreference(&fence); 304c74c162fSThomas Hellstrom 305c74c162fSThomas Hellstrom return 0; 306c74c162fSThomas Hellstrom } 307c74c162fSThomas Hellstrom 308c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res) 309c74c162fSThomas Hellstrom { 310c74c162fSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 311c74c162fSThomas Hellstrom struct { 312c74c162fSThomas Hellstrom SVGA3dCmdHeader header; 313c74c162fSThomas Hellstrom SVGA3dCmdDestroyGBShader body; 314c74c162fSThomas Hellstrom } *cmd; 315c74c162fSThomas Hellstrom 316c74c162fSThomas Hellstrom if (likely(res->id == -1)) 317c74c162fSThomas Hellstrom return 0; 318c74c162fSThomas Hellstrom 319173fb7d4SThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 320d80efd5cSThomas Hellstrom vmw_binding_res_list_scrub(&res->binding_head); 321173fb7d4SThomas Hellstrom 3228426ed9cSZack Rusin cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 323c74c162fSThomas Hellstrom if (unlikely(cmd == NULL)) { 3243e894a62SThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 325c74c162fSThomas Hellstrom return -ENOMEM; 326c74c162fSThomas Hellstrom } 327c74c162fSThomas Hellstrom 328c74c162fSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER; 329c74c162fSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 330c74c162fSThomas Hellstrom cmd->body.shid = res->id; 3318426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 332173fb7d4SThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 333c74c162fSThomas Hellstrom vmw_resource_release_id(res); 334153b3d5bSThomas Hellstrom vmw_fifo_resource_dec(dev_priv); 335c74c162fSThomas Hellstrom 336c74c162fSThomas Hellstrom return 0; 337c74c162fSThomas Hellstrom } 338c74c162fSThomas Hellstrom 339d80efd5cSThomas Hellstrom /* 340d80efd5cSThomas Hellstrom * DX shader code: 341d80efd5cSThomas Hellstrom */ 342d80efd5cSThomas Hellstrom 343d80efd5cSThomas Hellstrom /** 344d80efd5cSThomas Hellstrom * vmw_dx_shader_commit_notify - Notify that a shader operation has been 345d80efd5cSThomas Hellstrom * committed to hardware from a user-supplied command stream. 346d80efd5cSThomas Hellstrom * 347d80efd5cSThomas Hellstrom * @res: Pointer to the shader resource. 348d80efd5cSThomas Hellstrom * @state: Indicating whether a creation or removal has been committed. 349d80efd5cSThomas Hellstrom * 350d80efd5cSThomas Hellstrom */ 351d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res, 352d80efd5cSThomas Hellstrom enum vmw_cmdbuf_res_state state) 353d80efd5cSThomas Hellstrom { 354d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 355d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 356d80efd5cSThomas Hellstrom 357d80efd5cSThomas Hellstrom if (state == VMW_CMDBUF_RES_ADD) { 358d80efd5cSThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 359d80efd5cSThomas Hellstrom vmw_cotable_add_resource(shader->cotable, 360d80efd5cSThomas Hellstrom &shader->cotable_head); 361d80efd5cSThomas Hellstrom shader->committed = true; 362d80efd5cSThomas Hellstrom res->id = shader->id; 363d80efd5cSThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 364d80efd5cSThomas Hellstrom } else { 365d80efd5cSThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 366d80efd5cSThomas Hellstrom list_del_init(&shader->cotable_head); 367d80efd5cSThomas Hellstrom shader->committed = false; 368d80efd5cSThomas Hellstrom res->id = -1; 369d80efd5cSThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 370d80efd5cSThomas Hellstrom } 371d80efd5cSThomas Hellstrom } 372d80efd5cSThomas Hellstrom 373d80efd5cSThomas Hellstrom /** 374d80efd5cSThomas Hellstrom * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader. 375d80efd5cSThomas Hellstrom * 376d80efd5cSThomas Hellstrom * @res: The shader resource 377d80efd5cSThomas Hellstrom * 378d80efd5cSThomas Hellstrom * This function reverts a scrub operation. 379d80efd5cSThomas Hellstrom */ 380d80efd5cSThomas Hellstrom static int vmw_dx_shader_unscrub(struct vmw_resource *res) 381d80efd5cSThomas Hellstrom { 382d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 383d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 384d80efd5cSThomas Hellstrom struct { 385d80efd5cSThomas Hellstrom SVGA3dCmdHeader header; 386d80efd5cSThomas Hellstrom SVGA3dCmdDXBindShader body; 387d80efd5cSThomas Hellstrom } *cmd; 388d80efd5cSThomas Hellstrom 389d80efd5cSThomas Hellstrom if (!list_empty(&shader->cotable_head) || !shader->committed) 390d80efd5cSThomas Hellstrom return 0; 391d80efd5cSThomas Hellstrom 3928426ed9cSZack Rusin cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id); 39311c45419SDeepak Rawat if (unlikely(cmd == NULL)) 394d80efd5cSThomas Hellstrom return -ENOMEM; 395d80efd5cSThomas Hellstrom 396d80efd5cSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER; 397d80efd5cSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 398d80efd5cSThomas Hellstrom cmd->body.cid = shader->ctx->id; 399d80efd5cSThomas Hellstrom cmd->body.shid = shader->id; 400d3116756SChristian König cmd->body.mobid = res->backup->base.resource->start; 401d80efd5cSThomas Hellstrom cmd->body.offsetInBytes = res->backup_offset; 4028426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 403d80efd5cSThomas Hellstrom 404d80efd5cSThomas Hellstrom vmw_cotable_add_resource(shader->cotable, &shader->cotable_head); 405d80efd5cSThomas Hellstrom 406d80efd5cSThomas Hellstrom return 0; 407d80efd5cSThomas Hellstrom } 408d80efd5cSThomas Hellstrom 409d80efd5cSThomas Hellstrom /** 410d80efd5cSThomas Hellstrom * vmw_dx_shader_create - The DX shader create callback 411d80efd5cSThomas Hellstrom * 412d80efd5cSThomas Hellstrom * @res: The DX shader resource 413d80efd5cSThomas Hellstrom * 414d80efd5cSThomas Hellstrom * The create callback is called as part of resource validation and 415d80efd5cSThomas Hellstrom * makes sure that we unscrub the shader if it's previously been scrubbed. 416d80efd5cSThomas Hellstrom */ 417d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res) 418d80efd5cSThomas Hellstrom { 419d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 420d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 421d80efd5cSThomas Hellstrom int ret = 0; 422d80efd5cSThomas Hellstrom 423d80efd5cSThomas Hellstrom WARN_ON_ONCE(!shader->committed); 424d80efd5cSThomas Hellstrom 425a0a63940SThomas Hellstrom if (vmw_resource_mob_attached(res)) { 426d80efd5cSThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 427d80efd5cSThomas Hellstrom ret = vmw_dx_shader_unscrub(res); 428d80efd5cSThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 429d80efd5cSThomas Hellstrom } 430d80efd5cSThomas Hellstrom 431d80efd5cSThomas Hellstrom res->id = shader->id; 432d80efd5cSThomas Hellstrom return ret; 433d80efd5cSThomas Hellstrom } 434d80efd5cSThomas Hellstrom 435d80efd5cSThomas Hellstrom /** 436d80efd5cSThomas Hellstrom * vmw_dx_shader_bind - The DX shader bind callback 437d80efd5cSThomas Hellstrom * 438d80efd5cSThomas Hellstrom * @res: The DX shader resource 439d80efd5cSThomas Hellstrom * @val_buf: Pointer to the validate buffer. 440d80efd5cSThomas Hellstrom * 441d80efd5cSThomas Hellstrom */ 442d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res, 443d80efd5cSThomas Hellstrom struct ttm_validate_buffer *val_buf) 444d80efd5cSThomas Hellstrom { 445d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 446d80efd5cSThomas Hellstrom struct ttm_buffer_object *bo = val_buf->bo; 447d80efd5cSThomas Hellstrom 448d3116756SChristian König BUG_ON(bo->resource->mem_type != VMW_PL_MOB); 449d80efd5cSThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 450d80efd5cSThomas Hellstrom vmw_dx_shader_unscrub(res); 451d80efd5cSThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 452d80efd5cSThomas Hellstrom 453d80efd5cSThomas Hellstrom return 0; 454d80efd5cSThomas Hellstrom } 455d80efd5cSThomas Hellstrom 456d80efd5cSThomas Hellstrom /** 457d80efd5cSThomas Hellstrom * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader. 458d80efd5cSThomas Hellstrom * 459d80efd5cSThomas Hellstrom * @res: The shader resource 460d80efd5cSThomas Hellstrom * 461d80efd5cSThomas Hellstrom * This function unbinds a MOB from the DX shader without requiring the 462d80efd5cSThomas Hellstrom * MOB dma_buffer to be reserved. The driver still considers the MOB bound. 463d80efd5cSThomas Hellstrom * However, once the driver eventually decides to unbind the MOB, it doesn't 464d80efd5cSThomas Hellstrom * need to access the context. 465d80efd5cSThomas Hellstrom */ 466d80efd5cSThomas Hellstrom static int vmw_dx_shader_scrub(struct vmw_resource *res) 467d80efd5cSThomas Hellstrom { 468d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 469d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 470d80efd5cSThomas Hellstrom struct { 471d80efd5cSThomas Hellstrom SVGA3dCmdHeader header; 472d80efd5cSThomas Hellstrom SVGA3dCmdDXBindShader body; 473d80efd5cSThomas Hellstrom } *cmd; 474d80efd5cSThomas Hellstrom 475d80efd5cSThomas Hellstrom if (list_empty(&shader->cotable_head)) 476d80efd5cSThomas Hellstrom return 0; 477d80efd5cSThomas Hellstrom 478d80efd5cSThomas Hellstrom WARN_ON_ONCE(!shader->committed); 4798426ed9cSZack Rusin cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd)); 48011c45419SDeepak Rawat if (unlikely(cmd == NULL)) 481d80efd5cSThomas Hellstrom return -ENOMEM; 482d80efd5cSThomas Hellstrom 483d80efd5cSThomas Hellstrom cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER; 484d80efd5cSThomas Hellstrom cmd->header.size = sizeof(cmd->body); 485d80efd5cSThomas Hellstrom cmd->body.cid = shader->ctx->id; 486d80efd5cSThomas Hellstrom cmd->body.shid = res->id; 487d80efd5cSThomas Hellstrom cmd->body.mobid = SVGA3D_INVALID_ID; 488d80efd5cSThomas Hellstrom cmd->body.offsetInBytes = 0; 4898426ed9cSZack Rusin vmw_cmd_commit(dev_priv, sizeof(*cmd)); 490d80efd5cSThomas Hellstrom res->id = -1; 491d80efd5cSThomas Hellstrom list_del_init(&shader->cotable_head); 492d80efd5cSThomas Hellstrom 493d80efd5cSThomas Hellstrom return 0; 494d80efd5cSThomas Hellstrom } 495d80efd5cSThomas Hellstrom 496d80efd5cSThomas Hellstrom /** 497d80efd5cSThomas Hellstrom * vmw_dx_shader_unbind - The dx shader unbind callback. 498d80efd5cSThomas Hellstrom * 499d80efd5cSThomas Hellstrom * @res: The shader resource 500d80efd5cSThomas Hellstrom * @readback: Whether this is a readback unbind. Currently unused. 501d80efd5cSThomas Hellstrom * @val_buf: MOB buffer information. 502d80efd5cSThomas Hellstrom */ 503d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res, 504d80efd5cSThomas Hellstrom bool readback, 505d80efd5cSThomas Hellstrom struct ttm_validate_buffer *val_buf) 506d80efd5cSThomas Hellstrom { 507d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = res->dev_priv; 508d80efd5cSThomas Hellstrom struct vmw_fence_obj *fence; 509d80efd5cSThomas Hellstrom int ret; 510d80efd5cSThomas Hellstrom 511d3116756SChristian König BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB); 512d80efd5cSThomas Hellstrom 513d80efd5cSThomas Hellstrom mutex_lock(&dev_priv->binding_mutex); 514d80efd5cSThomas Hellstrom ret = vmw_dx_shader_scrub(res); 515d80efd5cSThomas Hellstrom mutex_unlock(&dev_priv->binding_mutex); 516d80efd5cSThomas Hellstrom 517d80efd5cSThomas Hellstrom if (ret) 518d80efd5cSThomas Hellstrom return ret; 519d80efd5cSThomas Hellstrom 520d80efd5cSThomas Hellstrom (void) vmw_execbuf_fence_commands(NULL, dev_priv, 521d80efd5cSThomas Hellstrom &fence, NULL); 522e9431ea5SThomas Hellstrom vmw_bo_fence_single(val_buf->bo, fence); 523d80efd5cSThomas Hellstrom 524d80efd5cSThomas Hellstrom if (likely(fence != NULL)) 525d80efd5cSThomas Hellstrom vmw_fence_obj_unreference(&fence); 526d80efd5cSThomas Hellstrom 527d80efd5cSThomas Hellstrom return 0; 528d80efd5cSThomas Hellstrom } 529d80efd5cSThomas Hellstrom 530d80efd5cSThomas Hellstrom /** 531d80efd5cSThomas Hellstrom * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for 532d80efd5cSThomas Hellstrom * DX shaders. 533d80efd5cSThomas Hellstrom * 534d80efd5cSThomas Hellstrom * @dev_priv: Pointer to device private structure. 535d80efd5cSThomas Hellstrom * @list: The list of cotable resources. 536d80efd5cSThomas Hellstrom * @readback: Whether the call was part of a readback unbind. 537d80efd5cSThomas Hellstrom * 538d80efd5cSThomas Hellstrom * Scrubs all shader MOBs so that any subsequent shader unbind or shader 539d80efd5cSThomas Hellstrom * destroy operation won't need to swap in the context. 540d80efd5cSThomas Hellstrom */ 541d80efd5cSThomas Hellstrom void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv, 542d80efd5cSThomas Hellstrom struct list_head *list, 543d80efd5cSThomas Hellstrom bool readback) 544d80efd5cSThomas Hellstrom { 545d80efd5cSThomas Hellstrom struct vmw_dx_shader *entry, *next; 546d80efd5cSThomas Hellstrom 547d76ce03eSThomas Hellstrom lockdep_assert_held_once(&dev_priv->binding_mutex); 548d80efd5cSThomas Hellstrom 549d80efd5cSThomas Hellstrom list_for_each_entry_safe(entry, next, list, cotable_head) { 550d80efd5cSThomas Hellstrom WARN_ON(vmw_dx_shader_scrub(&entry->res)); 551d80efd5cSThomas Hellstrom if (!readback) 552d80efd5cSThomas Hellstrom entry->committed = false; 553d80efd5cSThomas Hellstrom } 554d80efd5cSThomas Hellstrom } 555d80efd5cSThomas Hellstrom 556d80efd5cSThomas Hellstrom /** 557d80efd5cSThomas Hellstrom * vmw_dx_shader_res_free - The DX shader free callback 558d80efd5cSThomas Hellstrom * 559d80efd5cSThomas Hellstrom * @res: The shader resource 560d80efd5cSThomas Hellstrom * 5618aadeb8aSZack Rusin * Frees the DX shader resource. 562d80efd5cSThomas Hellstrom */ 563d80efd5cSThomas Hellstrom static void vmw_dx_shader_res_free(struct vmw_resource *res) 564d80efd5cSThomas Hellstrom { 565d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res); 566d80efd5cSThomas Hellstrom 567d80efd5cSThomas Hellstrom vmw_resource_unreference(&shader->cotable); 568d80efd5cSThomas Hellstrom kfree(shader); 569d80efd5cSThomas Hellstrom } 570d80efd5cSThomas Hellstrom 571d80efd5cSThomas Hellstrom /** 572d80efd5cSThomas Hellstrom * vmw_dx_shader_add - Add a shader resource as a command buffer managed 573d80efd5cSThomas Hellstrom * resource. 574d80efd5cSThomas Hellstrom * 575d80efd5cSThomas Hellstrom * @man: The command buffer resource manager. 576d80efd5cSThomas Hellstrom * @ctx: Pointer to the context resource. 577d80efd5cSThomas Hellstrom * @user_key: The id used for this shader. 578d80efd5cSThomas Hellstrom * @shader_type: The shader type. 579d80efd5cSThomas Hellstrom * @list: The list of staged command buffer managed resources. 580d80efd5cSThomas Hellstrom */ 581d80efd5cSThomas Hellstrom int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man, 582d80efd5cSThomas Hellstrom struct vmw_resource *ctx, 583d80efd5cSThomas Hellstrom u32 user_key, 584d80efd5cSThomas Hellstrom SVGA3dShaderType shader_type, 585d80efd5cSThomas Hellstrom struct list_head *list) 586d80efd5cSThomas Hellstrom { 587d80efd5cSThomas Hellstrom struct vmw_dx_shader *shader; 588d80efd5cSThomas Hellstrom struct vmw_resource *res; 589d80efd5cSThomas Hellstrom struct vmw_private *dev_priv = ctx->dev_priv; 590d80efd5cSThomas Hellstrom int ret; 591d80efd5cSThomas Hellstrom 592d80efd5cSThomas Hellstrom if (!vmw_shader_id_ok(user_key, shader_type)) 593d80efd5cSThomas Hellstrom return -EINVAL; 594d80efd5cSThomas Hellstrom 595d80efd5cSThomas Hellstrom shader = kmalloc(sizeof(*shader), GFP_KERNEL); 596d80efd5cSThomas Hellstrom if (!shader) { 597d80efd5cSThomas Hellstrom return -ENOMEM; 598d80efd5cSThomas Hellstrom } 599d80efd5cSThomas Hellstrom 600d80efd5cSThomas Hellstrom res = &shader->res; 601d80efd5cSThomas Hellstrom shader->ctx = ctx; 6021b9a01d6SThomas Hellstrom shader->cotable = vmw_resource_reference 6031b9a01d6SThomas Hellstrom (vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER)); 604d80efd5cSThomas Hellstrom shader->id = user_key; 605d80efd5cSThomas Hellstrom shader->committed = false; 606d80efd5cSThomas Hellstrom INIT_LIST_HEAD(&shader->cotable_head); 607d80efd5cSThomas Hellstrom ret = vmw_resource_init(dev_priv, res, true, 608d80efd5cSThomas Hellstrom vmw_dx_shader_res_free, &vmw_dx_shader_func); 609d80efd5cSThomas Hellstrom if (ret) 610d80efd5cSThomas Hellstrom goto out_resource_init; 611d80efd5cSThomas Hellstrom 612d80efd5cSThomas Hellstrom /* 613d80efd5cSThomas Hellstrom * The user_key name-space is not per shader type for DX shaders, 614d80efd5cSThomas Hellstrom * so when hashing, use a single zero shader type. 615d80efd5cSThomas Hellstrom */ 616d80efd5cSThomas Hellstrom ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader, 617d80efd5cSThomas Hellstrom vmw_shader_key(user_key, 0), 618d80efd5cSThomas Hellstrom res, list); 619d80efd5cSThomas Hellstrom if (ret) 620d80efd5cSThomas Hellstrom goto out_resource_init; 621d80efd5cSThomas Hellstrom 622d80efd5cSThomas Hellstrom res->id = shader->id; 62313289241SThomas Hellstrom res->hw_destroy = vmw_hw_shader_destroy; 624d80efd5cSThomas Hellstrom 625d80efd5cSThomas Hellstrom out_resource_init: 626d80efd5cSThomas Hellstrom vmw_resource_unreference(&res); 627d80efd5cSThomas Hellstrom 628d80efd5cSThomas Hellstrom return ret; 629d80efd5cSThomas Hellstrom } 630d80efd5cSThomas Hellstrom 631d80efd5cSThomas Hellstrom 632d80efd5cSThomas Hellstrom 633ad2ae415SLee Jones /* 634c74c162fSThomas Hellstrom * User-space shader management: 635c74c162fSThomas Hellstrom */ 636c74c162fSThomas Hellstrom 637c74c162fSThomas Hellstrom static struct vmw_resource * 638c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base) 639c74c162fSThomas Hellstrom { 640c74c162fSThomas Hellstrom return &(container_of(base, struct vmw_user_shader, base)-> 641c74c162fSThomas Hellstrom shader.res); 642c74c162fSThomas Hellstrom } 643c74c162fSThomas Hellstrom 644c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res) 645c74c162fSThomas Hellstrom { 646c74c162fSThomas Hellstrom struct vmw_user_shader *ushader = 647c74c162fSThomas Hellstrom container_of(res, struct vmw_user_shader, shader.res); 648c74c162fSThomas Hellstrom 649c74c162fSThomas Hellstrom ttm_base_object_kfree(ushader, base); 650c74c162fSThomas Hellstrom } 651c74c162fSThomas Hellstrom 65218e4a466SThomas Hellstrom static void vmw_shader_free(struct vmw_resource *res) 65318e4a466SThomas Hellstrom { 65418e4a466SThomas Hellstrom struct vmw_shader *shader = vmw_res_to_shader(res); 65518e4a466SThomas Hellstrom 65618e4a466SThomas Hellstrom kfree(shader); 65718e4a466SThomas Hellstrom } 65818e4a466SThomas Hellstrom 659ad2ae415SLee Jones /* 660c74c162fSThomas Hellstrom * This function is called when user space has no more references on the 661c74c162fSThomas Hellstrom * base object. It releases the base-object's reference on the resource object. 662c74c162fSThomas Hellstrom */ 663c74c162fSThomas Hellstrom 664c74c162fSThomas Hellstrom static void vmw_user_shader_base_release(struct ttm_base_object **p_base) 665c74c162fSThomas Hellstrom { 666c74c162fSThomas Hellstrom struct ttm_base_object *base = *p_base; 667c74c162fSThomas Hellstrom struct vmw_resource *res = vmw_user_shader_base_to_res(base); 668c74c162fSThomas Hellstrom 669c74c162fSThomas Hellstrom *p_base = NULL; 670c74c162fSThomas Hellstrom vmw_resource_unreference(&res); 671c74c162fSThomas Hellstrom } 672c74c162fSThomas Hellstrom 673c74c162fSThomas Hellstrom int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data, 674c74c162fSThomas Hellstrom struct drm_file *file_priv) 675c74c162fSThomas Hellstrom { 676c74c162fSThomas Hellstrom struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data; 677c74c162fSThomas Hellstrom struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 678c74c162fSThomas Hellstrom 679*8afa13a0SZack Rusin return ttm_ref_object_base_unref(tfile, arg->handle); 680c74c162fSThomas Hellstrom } 681c74c162fSThomas Hellstrom 68218e4a466SThomas Hellstrom static int vmw_user_shader_alloc(struct vmw_private *dev_priv, 683f1d34bfdSThomas Hellstrom struct vmw_buffer_object *buffer, 684d5bde956SThomas Hellstrom size_t shader_size, 685d5bde956SThomas Hellstrom size_t offset, 686d5bde956SThomas Hellstrom SVGA3dShaderType shader_type, 687d80efd5cSThomas Hellstrom uint8_t num_input_sig, 688d80efd5cSThomas Hellstrom uint8_t num_output_sig, 689d5bde956SThomas Hellstrom struct ttm_object_file *tfile, 690d5bde956SThomas Hellstrom u32 *handle) 691d5bde956SThomas Hellstrom { 692d5bde956SThomas Hellstrom struct vmw_user_shader *ushader; 693d5bde956SThomas Hellstrom struct vmw_resource *res, *tmp; 694d5bde956SThomas Hellstrom int ret; 695d5bde956SThomas Hellstrom 696d5bde956SThomas Hellstrom ushader = kzalloc(sizeof(*ushader), GFP_KERNEL); 6971a4adb05SRavikant B Sharma if (unlikely(!ushader)) { 698d5bde956SThomas Hellstrom ret = -ENOMEM; 699d5bde956SThomas Hellstrom goto out; 700d5bde956SThomas Hellstrom } 701d5bde956SThomas Hellstrom 702d5bde956SThomas Hellstrom res = &ushader->shader.res; 703d5bde956SThomas Hellstrom ushader->base.shareable = false; 704d5bde956SThomas Hellstrom ushader->base.tfile = NULL; 705d5bde956SThomas Hellstrom 706d5bde956SThomas Hellstrom /* 707d5bde956SThomas Hellstrom * From here on, the destructor takes over resource freeing. 708d5bde956SThomas Hellstrom */ 709d5bde956SThomas Hellstrom 710d5bde956SThomas Hellstrom ret = vmw_gb_shader_init(dev_priv, res, shader_size, 711d80efd5cSThomas Hellstrom offset, shader_type, num_input_sig, 712d80efd5cSThomas Hellstrom num_output_sig, buffer, 713d5bde956SThomas Hellstrom vmw_user_shader_free); 714d5bde956SThomas Hellstrom if (unlikely(ret != 0)) 715d5bde956SThomas Hellstrom goto out; 716d5bde956SThomas Hellstrom 717d5bde956SThomas Hellstrom tmp = vmw_resource_reference(res); 718d5bde956SThomas Hellstrom ret = ttm_base_object_init(tfile, &ushader->base, false, 719d5bde956SThomas Hellstrom VMW_RES_SHADER, 720*8afa13a0SZack Rusin &vmw_user_shader_base_release); 721d5bde956SThomas Hellstrom 722d5bde956SThomas Hellstrom if (unlikely(ret != 0)) { 723d5bde956SThomas Hellstrom vmw_resource_unreference(&tmp); 724d5bde956SThomas Hellstrom goto out_err; 725d5bde956SThomas Hellstrom } 726d5bde956SThomas Hellstrom 727d5bde956SThomas Hellstrom if (handle) 728c7eae626SThomas Hellstrom *handle = ushader->base.handle; 729d5bde956SThomas Hellstrom out_err: 730d5bde956SThomas Hellstrom vmw_resource_unreference(&res); 731d5bde956SThomas Hellstrom out: 732d5bde956SThomas Hellstrom return ret; 733d5bde956SThomas Hellstrom } 734d5bde956SThomas Hellstrom 735d5bde956SThomas Hellstrom 736b9eb1a61SThomas Hellstrom static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv, 737f1d34bfdSThomas Hellstrom struct vmw_buffer_object *buffer, 73818e4a466SThomas Hellstrom size_t shader_size, 73918e4a466SThomas Hellstrom size_t offset, 74018e4a466SThomas Hellstrom SVGA3dShaderType shader_type) 74118e4a466SThomas Hellstrom { 74218e4a466SThomas Hellstrom struct vmw_shader *shader; 74318e4a466SThomas Hellstrom struct vmw_resource *res; 74418e4a466SThomas Hellstrom int ret; 74518e4a466SThomas Hellstrom 74618e4a466SThomas Hellstrom shader = kzalloc(sizeof(*shader), GFP_KERNEL); 7471a4adb05SRavikant B Sharma if (unlikely(!shader)) { 74818e4a466SThomas Hellstrom ret = -ENOMEM; 74918e4a466SThomas Hellstrom goto out_err; 75018e4a466SThomas Hellstrom } 75118e4a466SThomas Hellstrom 75218e4a466SThomas Hellstrom res = &shader->res; 75318e4a466SThomas Hellstrom 75418e4a466SThomas Hellstrom /* 75518e4a466SThomas Hellstrom * From here on, the destructor takes over resource freeing. 75618e4a466SThomas Hellstrom */ 75718e4a466SThomas Hellstrom ret = vmw_gb_shader_init(dev_priv, res, shader_size, 758d80efd5cSThomas Hellstrom offset, shader_type, 0, 0, buffer, 75918e4a466SThomas Hellstrom vmw_shader_free); 76018e4a466SThomas Hellstrom 76118e4a466SThomas Hellstrom out_err: 76218e4a466SThomas Hellstrom return ret ? ERR_PTR(ret) : res; 76318e4a466SThomas Hellstrom } 76418e4a466SThomas Hellstrom 76518e4a466SThomas Hellstrom 766d80efd5cSThomas Hellstrom static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv, 767d80efd5cSThomas Hellstrom enum drm_vmw_shader_type shader_type_drm, 768d80efd5cSThomas Hellstrom u32 buffer_handle, size_t size, size_t offset, 769d80efd5cSThomas Hellstrom uint8_t num_input_sig, uint8_t num_output_sig, 770d80efd5cSThomas Hellstrom uint32_t *shader_handle) 771c74c162fSThomas Hellstrom { 772c74c162fSThomas Hellstrom struct vmw_private *dev_priv = vmw_priv(dev); 773c74c162fSThomas Hellstrom struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 774f1d34bfdSThomas Hellstrom struct vmw_buffer_object *buffer = NULL; 775c74c162fSThomas Hellstrom SVGA3dShaderType shader_type; 776c74c162fSThomas Hellstrom int ret; 777c74c162fSThomas Hellstrom 778d80efd5cSThomas Hellstrom if (buffer_handle != SVGA3D_INVALID_ID) { 779*8afa13a0SZack Rusin ret = vmw_user_bo_lookup(file_priv, buffer_handle, &buffer); 780c74c162fSThomas Hellstrom if (unlikely(ret != 0)) { 7815724f899SDeepak Rawat VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n"); 782c74c162fSThomas Hellstrom return ret; 783c74c162fSThomas Hellstrom } 784c74c162fSThomas Hellstrom 785e11bfb99SChristian König if ((u64)buffer->base.base.size < (u64)size + (u64)offset) { 7865724f899SDeepak Rawat VMW_DEBUG_USER("Illegal buffer- or shader size.\n"); 787c74c162fSThomas Hellstrom ret = -EINVAL; 788c74c162fSThomas Hellstrom goto out_bad_arg; 789c74c162fSThomas Hellstrom } 790c74c162fSThomas Hellstrom } 791c74c162fSThomas Hellstrom 792d80efd5cSThomas Hellstrom switch (shader_type_drm) { 793c74c162fSThomas Hellstrom case drm_vmw_shader_type_vs: 794c74c162fSThomas Hellstrom shader_type = SVGA3D_SHADERTYPE_VS; 795c74c162fSThomas Hellstrom break; 796c74c162fSThomas Hellstrom case drm_vmw_shader_type_ps: 797c74c162fSThomas Hellstrom shader_type = SVGA3D_SHADERTYPE_PS; 798c74c162fSThomas Hellstrom break; 799c74c162fSThomas Hellstrom default: 8005724f899SDeepak Rawat VMW_DEBUG_USER("Illegal shader type.\n"); 801c74c162fSThomas Hellstrom ret = -EINVAL; 802c74c162fSThomas Hellstrom goto out_bad_arg; 803c74c162fSThomas Hellstrom } 804c74c162fSThomas Hellstrom 805d80efd5cSThomas Hellstrom ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset, 806d80efd5cSThomas Hellstrom shader_type, num_input_sig, 807d80efd5cSThomas Hellstrom num_output_sig, tfile, shader_handle); 808c74c162fSThomas Hellstrom out_bad_arg: 809f1d34bfdSThomas Hellstrom vmw_bo_unreference(&buffer); 810d5bde956SThomas Hellstrom return ret; 811d5bde956SThomas Hellstrom } 812c74c162fSThomas Hellstrom 813d5bde956SThomas Hellstrom /** 814d80efd5cSThomas Hellstrom * vmw_shader_id_ok - Check whether a compat shader user key and 81518e4a466SThomas Hellstrom * shader type are within valid bounds. 816d5bde956SThomas Hellstrom * 81718e4a466SThomas Hellstrom * @user_key: User space id of the shader. 81818e4a466SThomas Hellstrom * @shader_type: Shader type. 819d5bde956SThomas Hellstrom * 82018e4a466SThomas Hellstrom * Returns true if valid false if not. 821d5bde956SThomas Hellstrom */ 822d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type) 823d5bde956SThomas Hellstrom { 82418e4a466SThomas Hellstrom return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16; 825d5bde956SThomas Hellstrom } 826d5bde956SThomas Hellstrom 827d5bde956SThomas Hellstrom /** 828d80efd5cSThomas Hellstrom * vmw_shader_key - Compute a hash key suitable for a compat shader. 829d5bde956SThomas Hellstrom * 83018e4a466SThomas Hellstrom * @user_key: User space id of the shader. 83118e4a466SThomas Hellstrom * @shader_type: Shader type. 832d5bde956SThomas Hellstrom * 83318e4a466SThomas Hellstrom * Returns a hash key suitable for a command buffer managed resource 83418e4a466SThomas Hellstrom * manager hash table. 835d5bde956SThomas Hellstrom */ 836d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type) 837d5bde956SThomas Hellstrom { 83818e4a466SThomas Hellstrom return user_key | (shader_type << 20); 839d5bde956SThomas Hellstrom } 840d5bde956SThomas Hellstrom 841d5bde956SThomas Hellstrom /** 842d80efd5cSThomas Hellstrom * vmw_shader_remove - Stage a compat shader for removal. 843d5bde956SThomas Hellstrom * 84418e4a466SThomas Hellstrom * @man: Pointer to the compat shader manager identifying the shader namespace. 845d5bde956SThomas Hellstrom * @user_key: The key that is used to identify the shader. The key is 846d5bde956SThomas Hellstrom * unique to the shader type. 847d5bde956SThomas Hellstrom * @shader_type: Shader type. 84818e4a466SThomas Hellstrom * @list: Caller's list of staged command buffer resource actions. 849d5bde956SThomas Hellstrom */ 850d80efd5cSThomas Hellstrom int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man, 851d5bde956SThomas Hellstrom u32 user_key, SVGA3dShaderType shader_type, 852d5bde956SThomas Hellstrom struct list_head *list) 853d5bde956SThomas Hellstrom { 854d80efd5cSThomas Hellstrom struct vmw_resource *dummy; 855d80efd5cSThomas Hellstrom 856d80efd5cSThomas Hellstrom if (!vmw_shader_id_ok(user_key, shader_type)) 857d5bde956SThomas Hellstrom return -EINVAL; 858d5bde956SThomas Hellstrom 859d80efd5cSThomas Hellstrom return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader, 860d80efd5cSThomas Hellstrom vmw_shader_key(user_key, shader_type), 861d80efd5cSThomas Hellstrom list, &dummy); 862d5bde956SThomas Hellstrom } 863d5bde956SThomas Hellstrom 864d5bde956SThomas Hellstrom /** 86518e4a466SThomas Hellstrom * vmw_compat_shader_add - Create a compat shader and stage it for addition 86618e4a466SThomas Hellstrom * as a command buffer managed resource. 867d5bde956SThomas Hellstrom * 868ad2ae415SLee Jones * @dev_priv: Pointer to device private structure. 86918e4a466SThomas Hellstrom * @man: Pointer to the compat shader manager identifying the shader namespace. 870d5bde956SThomas Hellstrom * @user_key: The key that is used to identify the shader. The key is 871d5bde956SThomas Hellstrom * unique to the shader type. 872d5bde956SThomas Hellstrom * @bytecode: Pointer to the bytecode of the shader. 873d5bde956SThomas Hellstrom * @shader_type: Shader type. 874ad2ae415SLee Jones * @size: Command size. 87518e4a466SThomas Hellstrom * @list: Caller's list of staged command buffer resource actions. 876d5bde956SThomas Hellstrom * 877d5bde956SThomas Hellstrom */ 87818e4a466SThomas Hellstrom int vmw_compat_shader_add(struct vmw_private *dev_priv, 87918e4a466SThomas Hellstrom struct vmw_cmdbuf_res_manager *man, 880d5bde956SThomas Hellstrom u32 user_key, const void *bytecode, 881d5bde956SThomas Hellstrom SVGA3dShaderType shader_type, 882d5bde956SThomas Hellstrom size_t size, 883d5bde956SThomas Hellstrom struct list_head *list) 884d5bde956SThomas Hellstrom { 88519be5570SChristian König struct ttm_operation_ctx ctx = { false, true }; 886f1d34bfdSThomas Hellstrom struct vmw_buffer_object *buf; 887d5bde956SThomas Hellstrom struct ttm_bo_kmap_obj map; 888d5bde956SThomas Hellstrom bool is_iomem; 889d5bde956SThomas Hellstrom int ret; 89018e4a466SThomas Hellstrom struct vmw_resource *res; 891d5bde956SThomas Hellstrom 892d80efd5cSThomas Hellstrom if (!vmw_shader_id_ok(user_key, shader_type)) 893d5bde956SThomas Hellstrom return -EINVAL; 894d5bde956SThomas Hellstrom 895*8afa13a0SZack Rusin ret = vmw_bo_create(dev_priv, size, &vmw_sys_placement, 896*8afa13a0SZack Rusin true, true, vmw_bo_bo_free, &buf); 897d5bde956SThomas Hellstrom if (unlikely(ret != 0)) 898d5bde956SThomas Hellstrom goto out; 899d5bde956SThomas Hellstrom 900dfd5e50eSChristian König ret = ttm_bo_reserve(&buf->base, false, true, NULL); 901d5bde956SThomas Hellstrom if (unlikely(ret != 0)) 902d5bde956SThomas Hellstrom goto no_reserve; 903d5bde956SThomas Hellstrom 904d5bde956SThomas Hellstrom /* Map and copy shader bytecode. */ 905bc65754cSCai Huoqing ret = ttm_bo_kmap(&buf->base, 0, PFN_UP(size), &map); 906d5bde956SThomas Hellstrom if (unlikely(ret != 0)) { 907d5bde956SThomas Hellstrom ttm_bo_unreserve(&buf->base); 908d5bde956SThomas Hellstrom goto no_reserve; 909d5bde956SThomas Hellstrom } 910d5bde956SThomas Hellstrom 911d5bde956SThomas Hellstrom memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size); 912d5bde956SThomas Hellstrom WARN_ON(is_iomem); 913d5bde956SThomas Hellstrom 914d5bde956SThomas Hellstrom ttm_bo_kunmap(&map); 91519be5570SChristian König ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx); 916d5bde956SThomas Hellstrom WARN_ON(ret != 0); 917d5bde956SThomas Hellstrom ttm_bo_unreserve(&buf->base); 918d5bde956SThomas Hellstrom 91918e4a466SThomas Hellstrom res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type); 920d5bde956SThomas Hellstrom if (unlikely(ret != 0)) 921d5bde956SThomas Hellstrom goto no_reserve; 922d5bde956SThomas Hellstrom 923d80efd5cSThomas Hellstrom ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader, 924d80efd5cSThomas Hellstrom vmw_shader_key(user_key, shader_type), 92518e4a466SThomas Hellstrom res, list); 92618e4a466SThomas Hellstrom vmw_resource_unreference(&res); 927d5bde956SThomas Hellstrom no_reserve: 928f1d34bfdSThomas Hellstrom vmw_bo_unreference(&buf); 929d5bde956SThomas Hellstrom out: 930d5bde956SThomas Hellstrom return ret; 931d5bde956SThomas Hellstrom } 932d5bde956SThomas Hellstrom 933d5bde956SThomas Hellstrom /** 934d80efd5cSThomas Hellstrom * vmw_shader_lookup - Look up a compat shader 935d5bde956SThomas Hellstrom * 93618e4a466SThomas Hellstrom * @man: Pointer to the command buffer managed resource manager identifying 93718e4a466SThomas Hellstrom * the shader namespace. 93818e4a466SThomas Hellstrom * @user_key: The user space id of the shader. 93918e4a466SThomas Hellstrom * @shader_type: The shader type. 940d5bde956SThomas Hellstrom * 94118e4a466SThomas Hellstrom * Returns a refcounted pointer to a struct vmw_resource if the shader was 94218e4a466SThomas Hellstrom * found. An error pointer otherwise. 943d5bde956SThomas Hellstrom */ 94418e4a466SThomas Hellstrom struct vmw_resource * 945d80efd5cSThomas Hellstrom vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man, 94618e4a466SThomas Hellstrom u32 user_key, 94718e4a466SThomas Hellstrom SVGA3dShaderType shader_type) 948d5bde956SThomas Hellstrom { 949d80efd5cSThomas Hellstrom if (!vmw_shader_id_ok(user_key, shader_type)) 95018e4a466SThomas Hellstrom return ERR_PTR(-EINVAL); 951d5bde956SThomas Hellstrom 952d80efd5cSThomas Hellstrom return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader, 953d80efd5cSThomas Hellstrom vmw_shader_key(user_key, shader_type)); 954d80efd5cSThomas Hellstrom } 955d80efd5cSThomas Hellstrom 956d80efd5cSThomas Hellstrom int vmw_shader_define_ioctl(struct drm_device *dev, void *data, 957d80efd5cSThomas Hellstrom struct drm_file *file_priv) 958d80efd5cSThomas Hellstrom { 959d80efd5cSThomas Hellstrom struct drm_vmw_shader_create_arg *arg = 960d80efd5cSThomas Hellstrom (struct drm_vmw_shader_create_arg *)data; 961d80efd5cSThomas Hellstrom 962d80efd5cSThomas Hellstrom return vmw_shader_define(dev, file_priv, arg->shader_type, 963d80efd5cSThomas Hellstrom arg->buffer_handle, 964d80efd5cSThomas Hellstrom arg->size, arg->offset, 965d80efd5cSThomas Hellstrom 0, 0, 966d80efd5cSThomas Hellstrom &arg->shader_handle); 967c74c162fSThomas Hellstrom } 968