1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
218e4a466SThomas Hellstrom /**************************************************************************
318e4a466SThomas Hellstrom  *
443531dc6SMaaz Mombasawala  * Copyright 2014-2022 VMware, Inc., Palo Alto, CA., USA
518e4a466SThomas Hellstrom  *
618e4a466SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
718e4a466SThomas Hellstrom  * copy of this software and associated documentation files (the
818e4a466SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
918e4a466SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
1018e4a466SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
1118e4a466SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
1218e4a466SThomas Hellstrom  * the following conditions:
1318e4a466SThomas Hellstrom  *
1418e4a466SThomas Hellstrom  * The above copyright notice and this permission notice (including the
1518e4a466SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
1618e4a466SThomas Hellstrom  * of the Software.
1718e4a466SThomas Hellstrom  *
1818e4a466SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1918e4a466SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2018e4a466SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
2118e4a466SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2218e4a466SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2318e4a466SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2418e4a466SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
2518e4a466SThomas Hellstrom  *
2618e4a466SThomas Hellstrom  **************************************************************************/
2718e4a466SThomas Hellstrom 
2818e4a466SThomas Hellstrom #include "vmwgfx_drv.h"
29d80efd5cSThomas Hellstrom #include "vmwgfx_resource_priv.h"
3018e4a466SThomas Hellstrom 
3143531dc6SMaaz Mombasawala #include <linux/hashtable.h>
3243531dc6SMaaz Mombasawala 
3318e4a466SThomas Hellstrom #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
3418e4a466SThomas Hellstrom 
3518e4a466SThomas Hellstrom /**
3618e4a466SThomas Hellstrom  * struct vmw_cmdbuf_res - Command buffer managed resource entry.
3718e4a466SThomas Hellstrom  *
3818e4a466SThomas Hellstrom  * @res: Refcounted pointer to a struct vmw_resource.
3918e4a466SThomas Hellstrom  * @hash: Hash entry for the manager hash table.
4018e4a466SThomas Hellstrom  * @head: List head used either by the staging list or the manager list
4105436815STom Rix  * of committed resources.
4218e4a466SThomas Hellstrom  * @state: Staging state of this resource entry.
4318e4a466SThomas Hellstrom  * @man: Pointer to a resource manager for this entry.
4418e4a466SThomas Hellstrom  */
4518e4a466SThomas Hellstrom struct vmw_cmdbuf_res {
4618e4a466SThomas Hellstrom 	struct vmw_resource *res;
472985c964SThomas Zimmermann 	struct vmwgfx_hash_item hash;
4818e4a466SThomas Hellstrom 	struct list_head head;
4918e4a466SThomas Hellstrom 	enum vmw_cmdbuf_res_state state;
5018e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res_manager *man;
5118e4a466SThomas Hellstrom };
5218e4a466SThomas Hellstrom 
5318e4a466SThomas Hellstrom /**
5418e4a466SThomas Hellstrom  * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
5518e4a466SThomas Hellstrom  *
5605436815STom Rix  * @resources: Hash table containing staged and committed command buffer
5718e4a466SThomas Hellstrom  * resources
5805436815STom Rix  * @list: List of committed command buffer resources.
5918e4a466SThomas Hellstrom  * @dev_priv: Pointer to a device private structure.
6018e4a466SThomas Hellstrom  *
6118e4a466SThomas Hellstrom  * @resources and @list are protected by the cmdbuf mutex for now.
6218e4a466SThomas Hellstrom  */
6318e4a466SThomas Hellstrom struct vmw_cmdbuf_res_manager {
6443531dc6SMaaz Mombasawala 	DECLARE_HASHTABLE(resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
6518e4a466SThomas Hellstrom 	struct list_head list;
6618e4a466SThomas Hellstrom 	struct vmw_private *dev_priv;
6718e4a466SThomas Hellstrom };
6818e4a466SThomas Hellstrom 
6918e4a466SThomas Hellstrom 
7018e4a466SThomas Hellstrom /**
7118e4a466SThomas Hellstrom  * vmw_cmdbuf_res_lookup - Look up a command buffer resource
7218e4a466SThomas Hellstrom  *
7318e4a466SThomas Hellstrom  * @man: Pointer to the command buffer resource manager
743a79c5e3SLee Jones  * @res_type: The resource type, that combined with the user key
7518e4a466SThomas Hellstrom  * identifies the resource.
7618e4a466SThomas Hellstrom  * @user_key: The user key.
7718e4a466SThomas Hellstrom  *
7818e4a466SThomas Hellstrom  * Returns a valid refcounted struct vmw_resource pointer on success,
7918e4a466SThomas Hellstrom  * an error pointer on failure.
8018e4a466SThomas Hellstrom  */
8118e4a466SThomas Hellstrom struct vmw_resource *
vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key)8218e4a466SThomas Hellstrom vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
8318e4a466SThomas Hellstrom 		      enum vmw_cmdbuf_res_type res_type,
8418e4a466SThomas Hellstrom 		      u32 user_key)
8518e4a466SThomas Hellstrom {
862985c964SThomas Zimmermann 	struct vmwgfx_hash_item *hash;
8718e4a466SThomas Hellstrom 	unsigned long key = user_key | (res_type << 24);
8818e4a466SThomas Hellstrom 
8943531dc6SMaaz Mombasawala 	hash_for_each_possible_rcu(man->resources, hash, head, key) {
9043531dc6SMaaz Mombasawala 		if (hash->key == key)
91*9da30cddSMaaz Mombasawala 			return hlist_entry(hash, struct vmw_cmdbuf_res, hash)->res;
9218e4a466SThomas Hellstrom 	}
9343531dc6SMaaz Mombasawala 	return ERR_PTR(-EINVAL);
9443531dc6SMaaz Mombasawala }
9518e4a466SThomas Hellstrom 
9618e4a466SThomas Hellstrom /**
9718e4a466SThomas Hellstrom  * vmw_cmdbuf_res_free - Free a command buffer resource.
9818e4a466SThomas Hellstrom  *
9918e4a466SThomas Hellstrom  * @man: Pointer to the command buffer resource manager
10018e4a466SThomas Hellstrom  * @entry: Pointer to a struct vmw_cmdbuf_res.
10118e4a466SThomas Hellstrom  *
10218e4a466SThomas Hellstrom  * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
10318e4a466SThomas Hellstrom  * struct vmw_resource.
10418e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager * man,struct vmw_cmdbuf_res * entry)10518e4a466SThomas Hellstrom static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
10618e4a466SThomas Hellstrom 				struct vmw_cmdbuf_res *entry)
10718e4a466SThomas Hellstrom {
10818e4a466SThomas Hellstrom 	list_del(&entry->head);
10943531dc6SMaaz Mombasawala 	hash_del_rcu(&entry->hash.head);
11018e4a466SThomas Hellstrom 	vmw_resource_unreference(&entry->res);
11118e4a466SThomas Hellstrom 	kfree(entry);
11218e4a466SThomas Hellstrom }
11318e4a466SThomas Hellstrom 
11418e4a466SThomas Hellstrom /**
11518e4a466SThomas Hellstrom  * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
11618e4a466SThomas Hellstrom  *
11718e4a466SThomas Hellstrom  * @list: Caller's list of command buffer resource actions.
11818e4a466SThomas Hellstrom  *
11918e4a466SThomas Hellstrom  * This function commits a list of command buffer resource
12018e4a466SThomas Hellstrom  * additions or removals.
12118e4a466SThomas Hellstrom  * It is typically called when the execbuf ioctl call triggering these
12205436815STom Rix  * actions has committed the fifo contents to the device.
12318e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_commit(struct list_head * list)12418e4a466SThomas Hellstrom void vmw_cmdbuf_res_commit(struct list_head *list)
12518e4a466SThomas Hellstrom {
12618e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res *entry, *next;
12718e4a466SThomas Hellstrom 
12818e4a466SThomas Hellstrom 	list_for_each_entry_safe(entry, next, list, head) {
12918e4a466SThomas Hellstrom 		list_del(&entry->head);
130d80efd5cSThomas Hellstrom 		if (entry->res->func->commit_notify)
131d80efd5cSThomas Hellstrom 			entry->res->func->commit_notify(entry->res,
132d80efd5cSThomas Hellstrom 							entry->state);
13318e4a466SThomas Hellstrom 		switch (entry->state) {
13418e4a466SThomas Hellstrom 		case VMW_CMDBUF_RES_ADD:
135d80efd5cSThomas Hellstrom 			entry->state = VMW_CMDBUF_RES_COMMITTED;
13618e4a466SThomas Hellstrom 			list_add_tail(&entry->head, &entry->man->list);
13718e4a466SThomas Hellstrom 			break;
13818e4a466SThomas Hellstrom 		case VMW_CMDBUF_RES_DEL:
13918e4a466SThomas Hellstrom 			vmw_resource_unreference(&entry->res);
14018e4a466SThomas Hellstrom 			kfree(entry);
14118e4a466SThomas Hellstrom 			break;
14218e4a466SThomas Hellstrom 		default:
14318e4a466SThomas Hellstrom 			BUG();
14418e4a466SThomas Hellstrom 			break;
14518e4a466SThomas Hellstrom 		}
14618e4a466SThomas Hellstrom 	}
14718e4a466SThomas Hellstrom }
14818e4a466SThomas Hellstrom 
14918e4a466SThomas Hellstrom /**
15018e4a466SThomas Hellstrom  * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
15118e4a466SThomas Hellstrom  *
15218e4a466SThomas Hellstrom  * @list: Caller's list of command buffer resource action
15318e4a466SThomas Hellstrom  *
15418e4a466SThomas Hellstrom  * This function reverts a list of command buffer resource
15518e4a466SThomas Hellstrom  * additions or removals.
15618e4a466SThomas Hellstrom  * It is typically called when the execbuf ioctl call triggering these
15718e4a466SThomas Hellstrom  * actions failed for some reason, and the command stream was never
15818e4a466SThomas Hellstrom  * submitted.
15918e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_revert(struct list_head * list)16018e4a466SThomas Hellstrom void vmw_cmdbuf_res_revert(struct list_head *list)
16118e4a466SThomas Hellstrom {
16218e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res *entry, *next;
16318e4a466SThomas Hellstrom 
16418e4a466SThomas Hellstrom 	list_for_each_entry_safe(entry, next, list, head) {
16518e4a466SThomas Hellstrom 		switch (entry->state) {
16618e4a466SThomas Hellstrom 		case VMW_CMDBUF_RES_ADD:
16718e4a466SThomas Hellstrom 			vmw_cmdbuf_res_free(entry->man, entry);
16818e4a466SThomas Hellstrom 			break;
16918e4a466SThomas Hellstrom 		case VMW_CMDBUF_RES_DEL:
17043531dc6SMaaz Mombasawala 			hash_add_rcu(entry->man->resources, &entry->hash.head,
17143531dc6SMaaz Mombasawala 						entry->hash.key);
1721cb48cf3SBaokun Li 			list_move_tail(&entry->head, &entry->man->list);
173d80efd5cSThomas Hellstrom 			entry->state = VMW_CMDBUF_RES_COMMITTED;
17418e4a466SThomas Hellstrom 			break;
17518e4a466SThomas Hellstrom 		default:
17618e4a466SThomas Hellstrom 			BUG();
17718e4a466SThomas Hellstrom 			break;
17818e4a466SThomas Hellstrom 		}
17918e4a466SThomas Hellstrom 	}
18018e4a466SThomas Hellstrom }
18118e4a466SThomas Hellstrom 
18218e4a466SThomas Hellstrom /**
18318e4a466SThomas Hellstrom  * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
18418e4a466SThomas Hellstrom  *
18518e4a466SThomas Hellstrom  * @man: Pointer to the command buffer resource manager.
18618e4a466SThomas Hellstrom  * @res_type: The resource type.
18718e4a466SThomas Hellstrom  * @user_key: The user-space id of the resource.
18818e4a466SThomas Hellstrom  * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
18918e4a466SThomas Hellstrom  * @list: The staging list.
19018e4a466SThomas Hellstrom  *
19118e4a466SThomas Hellstrom  * This function allocates a struct vmw_cmdbuf_res entry and adds the
19218e4a466SThomas Hellstrom  * resource to the hash table of the manager identified by @man. The
19318e4a466SThomas Hellstrom  * entry is then put on the staging list identified by @list.
19418e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key,struct vmw_resource * res,struct list_head * list)19518e4a466SThomas Hellstrom int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
19618e4a466SThomas Hellstrom 		       enum vmw_cmdbuf_res_type res_type,
19718e4a466SThomas Hellstrom 		       u32 user_key,
19818e4a466SThomas Hellstrom 		       struct vmw_resource *res,
19918e4a466SThomas Hellstrom 		       struct list_head *list)
20018e4a466SThomas Hellstrom {
20118e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res *cres;
20218e4a466SThomas Hellstrom 
20318e4a466SThomas Hellstrom 	cres = kzalloc(sizeof(*cres), GFP_KERNEL);
2041a4adb05SRavikant B Sharma 	if (unlikely(!cres))
20518e4a466SThomas Hellstrom 		return -ENOMEM;
20618e4a466SThomas Hellstrom 
20718e4a466SThomas Hellstrom 	cres->hash.key = user_key | (res_type << 24);
20843531dc6SMaaz Mombasawala 	hash_add_rcu(man->resources, &cres->hash.head, cres->hash.key);
20918e4a466SThomas Hellstrom 
21018e4a466SThomas Hellstrom 	cres->state = VMW_CMDBUF_RES_ADD;
21118e4a466SThomas Hellstrom 	cres->res = vmw_resource_reference(res);
21218e4a466SThomas Hellstrom 	cres->man = man;
21318e4a466SThomas Hellstrom 	list_add_tail(&cres->head, list);
21418e4a466SThomas Hellstrom 
21543531dc6SMaaz Mombasawala 	return 0;
21618e4a466SThomas Hellstrom }
21718e4a466SThomas Hellstrom 
21818e4a466SThomas Hellstrom /**
21918e4a466SThomas Hellstrom  * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
22018e4a466SThomas Hellstrom  *
22118e4a466SThomas Hellstrom  * @man: Pointer to the command buffer resource manager.
22218e4a466SThomas Hellstrom  * @res_type: The resource type.
22318e4a466SThomas Hellstrom  * @user_key: The user-space id of the resource.
22418e4a466SThomas Hellstrom  * @list: The staging list.
225d80efd5cSThomas Hellstrom  * @res_p: If the resource is in an already committed state, points to the
226d80efd5cSThomas Hellstrom  * struct vmw_resource on successful return. The pointer will be
227d80efd5cSThomas Hellstrom  * non ref-counted.
22818e4a466SThomas Hellstrom  *
22918e4a466SThomas Hellstrom  * This function looks up the struct vmw_cmdbuf_res entry from the manager
23018e4a466SThomas Hellstrom  * hash table and, if it exists, removes it. Depending on its current staging
23118e4a466SThomas Hellstrom  * state it then either removes the entry from the staging list or adds it
23218e4a466SThomas Hellstrom  * to it with a staging state of removal.
23318e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager * man,enum vmw_cmdbuf_res_type res_type,u32 user_key,struct list_head * list,struct vmw_resource ** res_p)23418e4a466SThomas Hellstrom int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
23518e4a466SThomas Hellstrom 			  enum vmw_cmdbuf_res_type res_type,
23618e4a466SThomas Hellstrom 			  u32 user_key,
237d80efd5cSThomas Hellstrom 			  struct list_head *list,
238d80efd5cSThomas Hellstrom 			  struct vmw_resource **res_p)
23918e4a466SThomas Hellstrom {
24043531dc6SMaaz Mombasawala 	struct vmw_cmdbuf_res *entry = NULL;
2412985c964SThomas Zimmermann 	struct vmwgfx_hash_item *hash;
24243531dc6SMaaz Mombasawala 	unsigned long key = user_key | (res_type << 24);
24318e4a466SThomas Hellstrom 
24443531dc6SMaaz Mombasawala 	hash_for_each_possible_rcu(man->resources, hash, head, key) {
24543531dc6SMaaz Mombasawala 		if (hash->key == key) {
246*9da30cddSMaaz Mombasawala 			entry = hlist_entry(hash, struct vmw_cmdbuf_res, hash);
24743531dc6SMaaz Mombasawala 			break;
24843531dc6SMaaz Mombasawala 		}
24943531dc6SMaaz Mombasawala 	}
25043531dc6SMaaz Mombasawala 	if (unlikely(!entry))
25143531dc6SMaaz Mombasawala 		return -EINVAL;
25218e4a466SThomas Hellstrom 
25318e4a466SThomas Hellstrom 	switch (entry->state) {
25418e4a466SThomas Hellstrom 	case VMW_CMDBUF_RES_ADD:
25518e4a466SThomas Hellstrom 		vmw_cmdbuf_res_free(man, entry);
256d80efd5cSThomas Hellstrom 		*res_p = NULL;
25718e4a466SThomas Hellstrom 		break;
258d80efd5cSThomas Hellstrom 	case VMW_CMDBUF_RES_COMMITTED:
25943531dc6SMaaz Mombasawala 		hash_del_rcu(&entry->hash.head);
26018e4a466SThomas Hellstrom 		list_del(&entry->head);
26118e4a466SThomas Hellstrom 		entry->state = VMW_CMDBUF_RES_DEL;
26218e4a466SThomas Hellstrom 		list_add_tail(&entry->head, list);
263d80efd5cSThomas Hellstrom 		*res_p = entry->res;
26418e4a466SThomas Hellstrom 		break;
26518e4a466SThomas Hellstrom 	default:
26618e4a466SThomas Hellstrom 		BUG();
26718e4a466SThomas Hellstrom 		break;
26818e4a466SThomas Hellstrom 	}
26918e4a466SThomas Hellstrom 
27018e4a466SThomas Hellstrom 	return 0;
27118e4a466SThomas Hellstrom }
27218e4a466SThomas Hellstrom 
27318e4a466SThomas Hellstrom /**
27418e4a466SThomas Hellstrom  * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
27518e4a466SThomas Hellstrom  * manager.
27618e4a466SThomas Hellstrom  *
27718e4a466SThomas Hellstrom  * @dev_priv: Pointer to a struct vmw_private
27818e4a466SThomas Hellstrom  *
27918e4a466SThomas Hellstrom  * Allocates and initializes a command buffer managed resource manager. Returns
28018e4a466SThomas Hellstrom  * an error pointer on failure.
28118e4a466SThomas Hellstrom  */
28218e4a466SThomas Hellstrom struct vmw_cmdbuf_res_manager *
vmw_cmdbuf_res_man_create(struct vmw_private * dev_priv)28318e4a466SThomas Hellstrom vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
28418e4a466SThomas Hellstrom {
28518e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res_manager *man;
28618e4a466SThomas Hellstrom 
28718e4a466SThomas Hellstrom 	man = kzalloc(sizeof(*man), GFP_KERNEL);
2881a4adb05SRavikant B Sharma 	if (!man)
28918e4a466SThomas Hellstrom 		return ERR_PTR(-ENOMEM);
29018e4a466SThomas Hellstrom 
29118e4a466SThomas Hellstrom 	man->dev_priv = dev_priv;
29218e4a466SThomas Hellstrom 	INIT_LIST_HEAD(&man->list);
29343531dc6SMaaz Mombasawala 	hash_init(man->resources);
29418e4a466SThomas Hellstrom 	return man;
29518e4a466SThomas Hellstrom }
29618e4a466SThomas Hellstrom 
29718e4a466SThomas Hellstrom /**
29818e4a466SThomas Hellstrom  * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
29918e4a466SThomas Hellstrom  * manager.
30018e4a466SThomas Hellstrom  *
30118e4a466SThomas Hellstrom  * @man: Pointer to the  manager to destroy.
30218e4a466SThomas Hellstrom  *
30318e4a466SThomas Hellstrom  * This function destroys a command buffer managed resource manager and
30418e4a466SThomas Hellstrom  * unreferences / frees all command buffer managed resources and -entries
30518e4a466SThomas Hellstrom  * associated with it.
30618e4a466SThomas Hellstrom  */
vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager * man)30718e4a466SThomas Hellstrom void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
30818e4a466SThomas Hellstrom {
30918e4a466SThomas Hellstrom 	struct vmw_cmdbuf_res *entry, *next;
31018e4a466SThomas Hellstrom 
31118e4a466SThomas Hellstrom 	list_for_each_entry_safe(entry, next, &man->list, head)
31218e4a466SThomas Hellstrom 		vmw_cmdbuf_res_free(man, entry);
31318e4a466SThomas Hellstrom 
31418e4a466SThomas Hellstrom 	kfree(man);
31518e4a466SThomas Hellstrom }
31618e4a466SThomas Hellstrom 
317