Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /**************************************************************************
0003  *
0004  * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
0005  *
0006  * Permission is hereby granted, free of charge, to any person obtaining a
0007  * copy of this software and associated documentation files (the
0008  * "Software"), to deal in the Software without restriction, including
0009  * without limitation the rights to use, copy, modify, merge, publish,
0010  * distribute, sub license, and/or sell copies of the Software, and to
0011  * permit persons to whom the Software is furnished to do so, subject to
0012  * the following conditions:
0013  *
0014  * The above copyright notice and this permission notice (including the
0015  * next paragraph) shall be included in all copies or substantial portions
0016  * of the Software.
0017  *
0018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  **************************************************************************/
0027 
0028 #include "vmwgfx_drv.h"
0029 #include "vmwgfx_resource_priv.h"
0030 
0031 #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
0032 
0033 /**
0034  * struct vmw_cmdbuf_res - Command buffer managed resource entry.
0035  *
0036  * @res: Refcounted pointer to a struct vmw_resource.
0037  * @hash: Hash entry for the manager hash table.
0038  * @head: List head used either by the staging list or the manager list
0039  * of commited resources.
0040  * @state: Staging state of this resource entry.
0041  * @man: Pointer to a resource manager for this entry.
0042  */
0043 struct vmw_cmdbuf_res {
0044     struct vmw_resource *res;
0045     struct vmwgfx_hash_item hash;
0046     struct list_head head;
0047     enum vmw_cmdbuf_res_state state;
0048     struct vmw_cmdbuf_res_manager *man;
0049 };
0050 
0051 /**
0052  * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
0053  *
0054  * @resources: Hash table containing staged and commited command buffer
0055  * resources
0056  * @list: List of commited command buffer resources.
0057  * @dev_priv: Pointer to a device private structure.
0058  *
0059  * @resources and @list are protected by the cmdbuf mutex for now.
0060  */
0061 struct vmw_cmdbuf_res_manager {
0062     struct vmwgfx_open_hash resources;
0063     struct list_head list;
0064     struct vmw_private *dev_priv;
0065 };
0066 
0067 
0068 /**
0069  * vmw_cmdbuf_res_lookup - Look up a command buffer resource
0070  *
0071  * @man: Pointer to the command buffer resource manager
0072  * @res_type: The resource type, that combined with the user key
0073  * identifies the resource.
0074  * @user_key: The user key.
0075  *
0076  * Returns a valid refcounted struct vmw_resource pointer on success,
0077  * an error pointer on failure.
0078  */
0079 struct vmw_resource *
0080 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
0081               enum vmw_cmdbuf_res_type res_type,
0082               u32 user_key)
0083 {
0084     struct vmwgfx_hash_item *hash;
0085     int ret;
0086     unsigned long key = user_key | (res_type << 24);
0087 
0088     ret = vmwgfx_ht_find_item(&man->resources, key, &hash);
0089     if (unlikely(ret != 0))
0090         return ERR_PTR(ret);
0091 
0092     return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
0093 }
0094 
0095 /**
0096  * vmw_cmdbuf_res_free - Free a command buffer resource.
0097  *
0098  * @man: Pointer to the command buffer resource manager
0099  * @entry: Pointer to a struct vmw_cmdbuf_res.
0100  *
0101  * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
0102  * struct vmw_resource.
0103  */
0104 static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
0105                 struct vmw_cmdbuf_res *entry)
0106 {
0107     list_del(&entry->head);
0108     WARN_ON(vmwgfx_ht_remove_item(&man->resources, &entry->hash));
0109     vmw_resource_unreference(&entry->res);
0110     kfree(entry);
0111 }
0112 
0113 /**
0114  * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
0115  *
0116  * @list: Caller's list of command buffer resource actions.
0117  *
0118  * This function commits a list of command buffer resource
0119  * additions or removals.
0120  * It is typically called when the execbuf ioctl call triggering these
0121  * actions has commited the fifo contents to the device.
0122  */
0123 void vmw_cmdbuf_res_commit(struct list_head *list)
0124 {
0125     struct vmw_cmdbuf_res *entry, *next;
0126 
0127     list_for_each_entry_safe(entry, next, list, head) {
0128         list_del(&entry->head);
0129         if (entry->res->func->commit_notify)
0130             entry->res->func->commit_notify(entry->res,
0131                             entry->state);
0132         switch (entry->state) {
0133         case VMW_CMDBUF_RES_ADD:
0134             entry->state = VMW_CMDBUF_RES_COMMITTED;
0135             list_add_tail(&entry->head, &entry->man->list);
0136             break;
0137         case VMW_CMDBUF_RES_DEL:
0138             vmw_resource_unreference(&entry->res);
0139             kfree(entry);
0140             break;
0141         default:
0142             BUG();
0143             break;
0144         }
0145     }
0146 }
0147 
0148 /**
0149  * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
0150  *
0151  * @list: Caller's list of command buffer resource action
0152  *
0153  * This function reverts a list of command buffer resource
0154  * additions or removals.
0155  * It is typically called when the execbuf ioctl call triggering these
0156  * actions failed for some reason, and the command stream was never
0157  * submitted.
0158  */
0159 void vmw_cmdbuf_res_revert(struct list_head *list)
0160 {
0161     struct vmw_cmdbuf_res *entry, *next;
0162     int ret;
0163 
0164     list_for_each_entry_safe(entry, next, list, head) {
0165         switch (entry->state) {
0166         case VMW_CMDBUF_RES_ADD:
0167             vmw_cmdbuf_res_free(entry->man, entry);
0168             break;
0169         case VMW_CMDBUF_RES_DEL:
0170             ret = vmwgfx_ht_insert_item(&entry->man->resources, &entry->hash);
0171             BUG_ON(ret);
0172             list_move_tail(&entry->head, &entry->man->list);
0173             entry->state = VMW_CMDBUF_RES_COMMITTED;
0174             break;
0175         default:
0176             BUG();
0177             break;
0178         }
0179     }
0180 }
0181 
0182 /**
0183  * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
0184  *
0185  * @man: Pointer to the command buffer resource manager.
0186  * @res_type: The resource type.
0187  * @user_key: The user-space id of the resource.
0188  * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
0189  * @list: The staging list.
0190  *
0191  * This function allocates a struct vmw_cmdbuf_res entry and adds the
0192  * resource to the hash table of the manager identified by @man. The
0193  * entry is then put on the staging list identified by @list.
0194  */
0195 int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
0196                enum vmw_cmdbuf_res_type res_type,
0197                u32 user_key,
0198                struct vmw_resource *res,
0199                struct list_head *list)
0200 {
0201     struct vmw_cmdbuf_res *cres;
0202     int ret;
0203 
0204     cres = kzalloc(sizeof(*cres), GFP_KERNEL);
0205     if (unlikely(!cres))
0206         return -ENOMEM;
0207 
0208     cres->hash.key = user_key | (res_type << 24);
0209     ret = vmwgfx_ht_insert_item(&man->resources, &cres->hash);
0210     if (unlikely(ret != 0)) {
0211         kfree(cres);
0212         goto out_invalid_key;
0213     }
0214 
0215     cres->state = VMW_CMDBUF_RES_ADD;
0216     cres->res = vmw_resource_reference(res);
0217     cres->man = man;
0218     list_add_tail(&cres->head, list);
0219 
0220 out_invalid_key:
0221     return ret;
0222 }
0223 
0224 /**
0225  * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
0226  *
0227  * @man: Pointer to the command buffer resource manager.
0228  * @res_type: The resource type.
0229  * @user_key: The user-space id of the resource.
0230  * @list: The staging list.
0231  * @res_p: If the resource is in an already committed state, points to the
0232  * struct vmw_resource on successful return. The pointer will be
0233  * non ref-counted.
0234  *
0235  * This function looks up the struct vmw_cmdbuf_res entry from the manager
0236  * hash table and, if it exists, removes it. Depending on its current staging
0237  * state it then either removes the entry from the staging list or adds it
0238  * to it with a staging state of removal.
0239  */
0240 int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
0241               enum vmw_cmdbuf_res_type res_type,
0242               u32 user_key,
0243               struct list_head *list,
0244               struct vmw_resource **res_p)
0245 {
0246     struct vmw_cmdbuf_res *entry;
0247     struct vmwgfx_hash_item *hash;
0248     int ret;
0249 
0250     ret = vmwgfx_ht_find_item(&man->resources, user_key | (res_type << 24),
0251                    &hash);
0252     if (likely(ret != 0))
0253         return -EINVAL;
0254 
0255     entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
0256 
0257     switch (entry->state) {
0258     case VMW_CMDBUF_RES_ADD:
0259         vmw_cmdbuf_res_free(man, entry);
0260         *res_p = NULL;
0261         break;
0262     case VMW_CMDBUF_RES_COMMITTED:
0263         (void) vmwgfx_ht_remove_item(&man->resources, &entry->hash);
0264         list_del(&entry->head);
0265         entry->state = VMW_CMDBUF_RES_DEL;
0266         list_add_tail(&entry->head, list);
0267         *res_p = entry->res;
0268         break;
0269     default:
0270         BUG();
0271         break;
0272     }
0273 
0274     return 0;
0275 }
0276 
0277 /**
0278  * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
0279  * manager.
0280  *
0281  * @dev_priv: Pointer to a struct vmw_private
0282  *
0283  * Allocates and initializes a command buffer managed resource manager. Returns
0284  * an error pointer on failure.
0285  */
0286 struct vmw_cmdbuf_res_manager *
0287 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
0288 {
0289     struct vmw_cmdbuf_res_manager *man;
0290     int ret;
0291 
0292     man = kzalloc(sizeof(*man), GFP_KERNEL);
0293     if (!man)
0294         return ERR_PTR(-ENOMEM);
0295 
0296     man->dev_priv = dev_priv;
0297     INIT_LIST_HEAD(&man->list);
0298     ret = vmwgfx_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
0299     if (ret == 0)
0300         return man;
0301 
0302     kfree(man);
0303     return ERR_PTR(ret);
0304 }
0305 
0306 /**
0307  * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
0308  * manager.
0309  *
0310  * @man: Pointer to the  manager to destroy.
0311  *
0312  * This function destroys a command buffer managed resource manager and
0313  * unreferences / frees all command buffer managed resources and -entries
0314  * associated with it.
0315  */
0316 void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
0317 {
0318     struct vmw_cmdbuf_res *entry, *next;
0319 
0320     list_for_each_entry_safe(entry, next, &man->list, head)
0321         vmw_cmdbuf_res_free(man, entry);
0322 
0323     vmwgfx_ht_remove(&man->resources);
0324     kfree(man);
0325 }
0326