xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_validation.h (revision db5d28c0bfe566908719bec8e25443aabecbb802)
1038ecc50SThomas Hellstrom /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2038ecc50SThomas Hellstrom /**************************************************************************
3038ecc50SThomas Hellstrom  *
49e931f2eSMaaz Mombasawala  * Copyright © 2018 - 2022 VMware, Inc., Palo Alto, CA., USA
5038ecc50SThomas Hellstrom  * All Rights Reserved.
6038ecc50SThomas Hellstrom  *
7038ecc50SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
8038ecc50SThomas Hellstrom  * copy of this software and associated documentation files (the
9038ecc50SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
10038ecc50SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
11038ecc50SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
12038ecc50SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
13038ecc50SThomas Hellstrom  * the following conditions:
14038ecc50SThomas Hellstrom  *
15038ecc50SThomas Hellstrom  * The above copyright notice and this permission notice (including the
16038ecc50SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
17038ecc50SThomas Hellstrom  * of the Software.
18038ecc50SThomas Hellstrom  *
19038ecc50SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20038ecc50SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21038ecc50SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22038ecc50SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23038ecc50SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24038ecc50SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25038ecc50SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26038ecc50SThomas Hellstrom  *
27038ecc50SThomas Hellstrom  **************************************************************************/
28038ecc50SThomas Hellstrom #ifndef _VMWGFX_VALIDATION_H_
29038ecc50SThomas Hellstrom #define _VMWGFX_VALIDATION_H_
30038ecc50SThomas Hellstrom 
31038ecc50SThomas Hellstrom #include <linux/list.h>
329e931f2eSMaaz Mombasawala #include <linux/hashtable.h>
33038ecc50SThomas Hellstrom #include <linux/ww_mutex.h>
34d5c1f011SSam Ravnborg 
35038ecc50SThomas Hellstrom #include <drm/ttm/ttm_execbuf_util.h>
36038ecc50SThomas Hellstrom 
37a9f58c45SThomas Hellstrom #define VMW_RES_DIRTY_NONE 0
38a9f58c45SThomas Hellstrom #define VMW_RES_DIRTY_SET BIT(0)
39a9f58c45SThomas Hellstrom #define VMW_RES_DIRTY_CLEAR BIT(1)
40a9f58c45SThomas Hellstrom 
41038ecc50SThomas Hellstrom /**
42038ecc50SThomas Hellstrom  * struct vmw_validation_context - Per command submission validation context
43038ecc50SThomas Hellstrom  * @ht: Hash table used to find resource- or buffer object duplicates
44038ecc50SThomas Hellstrom  * @resource_list: List head for resource validation metadata
45038ecc50SThomas Hellstrom  * @resource_ctx_list: List head for resource validation metadata for
46038ecc50SThomas Hellstrom  * resources that need to be validated before those in @resource_list
47038ecc50SThomas Hellstrom  * @bo_list: List head for buffer objects
48fc18afcfSThomas Hellstrom  * @page_list: List of pages used by the memory allocator
49038ecc50SThomas Hellstrom  * @ticket: Ticked used for ww mutex locking
50038ecc50SThomas Hellstrom  * @res_mutex: Pointer to mutex used for resource reserving
51038ecc50SThomas Hellstrom  * @merge_dups: Whether to merge metadata for duplicate resources or
52038ecc50SThomas Hellstrom  * buffer objects
53fc18afcfSThomas Hellstrom  * @mem_size_left: Free memory left in the last page in @page_list
54fc18afcfSThomas Hellstrom  * @page_address: Kernel virtual address of the last page in @page_list
55038ecc50SThomas Hellstrom  */
56038ecc50SThomas Hellstrom struct vmw_validation_context {
579e931f2eSMaaz Mombasawala 	struct vmw_sw_context *sw_context;
58038ecc50SThomas Hellstrom 	struct list_head resource_list;
59038ecc50SThomas Hellstrom 	struct list_head resource_ctx_list;
60038ecc50SThomas Hellstrom 	struct list_head bo_list;
61fc18afcfSThomas Hellstrom 	struct list_head page_list;
62038ecc50SThomas Hellstrom 	struct ww_acquire_ctx ticket;
63038ecc50SThomas Hellstrom 	struct mutex *res_mutex;
64038ecc50SThomas Hellstrom 	unsigned int merge_dups;
65fc18afcfSThomas Hellstrom 	unsigned int mem_size_left;
66fc18afcfSThomas Hellstrom 	u8 *page_address;
67038ecc50SThomas Hellstrom };
68038ecc50SThomas Hellstrom 
6909881d29SZack Rusin struct vmw_bo;
70038ecc50SThomas Hellstrom struct vmw_resource;
71038ecc50SThomas Hellstrom struct vmw_fence_obj;
72038ecc50SThomas Hellstrom 
73038ecc50SThomas Hellstrom #if 0
74038ecc50SThomas Hellstrom /**
75038ecc50SThomas Hellstrom  * DECLARE_VAL_CONTEXT - Declare a validation context with initialization
76038ecc50SThomas Hellstrom  * @_name: The name of the variable
779e931f2eSMaaz Mombasawala  * @_sw_context: Contains the hash table used to find dups or NULL if none
78038ecc50SThomas Hellstrom  * @_merge_dups: Whether to merge duplicate buffer object- or resource
79038ecc50SThomas Hellstrom  * entries. If set to true, ideally a hash table pointer should be supplied
80038ecc50SThomas Hellstrom  * as well unless the number of resources and buffer objects per validation
81038ecc50SThomas Hellstrom  * is known to be very small
82038ecc50SThomas Hellstrom  */
83038ecc50SThomas Hellstrom #endif
849e931f2eSMaaz Mombasawala #define DECLARE_VAL_CONTEXT(_name, _sw_context, _merge_dups)		\
85038ecc50SThomas Hellstrom 	struct vmw_validation_context _name =				\
869e931f2eSMaaz Mombasawala 	{ .sw_context = _sw_context,					\
87038ecc50SThomas Hellstrom 	  .resource_list = LIST_HEAD_INIT((_name).resource_list),	\
88038ecc50SThomas Hellstrom 	  .resource_ctx_list = LIST_HEAD_INIT((_name).resource_ctx_list), \
89038ecc50SThomas Hellstrom 	  .bo_list = LIST_HEAD_INIT((_name).bo_list),			\
90fc18afcfSThomas Hellstrom 	  .page_list = LIST_HEAD_INIT((_name).page_list),		\
91fc18afcfSThomas Hellstrom 	  .res_mutex = NULL,						\
92038ecc50SThomas Hellstrom 	  .merge_dups = _merge_dups,					\
93fc18afcfSThomas Hellstrom 	  .mem_size_left = 0,						\
94038ecc50SThomas Hellstrom 	}
95038ecc50SThomas Hellstrom 
96038ecc50SThomas Hellstrom /**
97038ecc50SThomas Hellstrom  * vmw_validation_has_bos - return whether the validation context has
98038ecc50SThomas Hellstrom  * any buffer objects registered.
99038ecc50SThomas Hellstrom  *
100038ecc50SThomas Hellstrom  * @ctx: The validation context
101038ecc50SThomas Hellstrom  * Returns: Whether any buffer objects are registered
102038ecc50SThomas Hellstrom  */
103038ecc50SThomas Hellstrom static inline bool
vmw_validation_has_bos(struct vmw_validation_context * ctx)104038ecc50SThomas Hellstrom vmw_validation_has_bos(struct vmw_validation_context *ctx)
105038ecc50SThomas Hellstrom {
106038ecc50SThomas Hellstrom 	return !list_empty(&ctx->bo_list);
107038ecc50SThomas Hellstrom }
108038ecc50SThomas Hellstrom 
109038ecc50SThomas Hellstrom /**
110038ecc50SThomas Hellstrom  * vmw_validation_bo_reserve - Reserve buffer objects registered with a
111038ecc50SThomas Hellstrom  * validation context
112038ecc50SThomas Hellstrom  * @ctx: The validation context
113038ecc50SThomas Hellstrom  * @intr: Perform waits interruptible
114038ecc50SThomas Hellstrom  *
115038ecc50SThomas Hellstrom  * Return: Zero on success, -ERESTARTSYS when interrupted, negative error
116038ecc50SThomas Hellstrom  * code on failure
117038ecc50SThomas Hellstrom  */
118038ecc50SThomas Hellstrom static inline int
vmw_validation_bo_reserve(struct vmw_validation_context * ctx,bool intr)119038ecc50SThomas Hellstrom vmw_validation_bo_reserve(struct vmw_validation_context *ctx,
120038ecc50SThomas Hellstrom 			  bool intr)
121038ecc50SThomas Hellstrom {
122038ecc50SThomas Hellstrom 	return ttm_eu_reserve_buffers(&ctx->ticket, &ctx->bo_list, intr,
1239165fb87SChristian König 				      NULL);
124038ecc50SThomas Hellstrom }
125038ecc50SThomas Hellstrom 
126038ecc50SThomas Hellstrom /**
127038ecc50SThomas Hellstrom  * vmw_validation_bo_fence - Unreserve and fence buffer objects registered
128038ecc50SThomas Hellstrom  * with a validation context
129038ecc50SThomas Hellstrom  * @ctx: The validation context
130038ecc50SThomas Hellstrom  *
131038ecc50SThomas Hellstrom  * This function unreserves the buffer objects previously reserved using
132038ecc50SThomas Hellstrom  * vmw_validation_bo_reserve, and fences them with a fence object.
133038ecc50SThomas Hellstrom  */
134038ecc50SThomas Hellstrom static inline void
vmw_validation_bo_fence(struct vmw_validation_context * ctx,struct vmw_fence_obj * fence)135038ecc50SThomas Hellstrom vmw_validation_bo_fence(struct vmw_validation_context *ctx,
136038ecc50SThomas Hellstrom 			struct vmw_fence_obj *fence)
137038ecc50SThomas Hellstrom {
138038ecc50SThomas Hellstrom 	ttm_eu_fence_buffer_objects(&ctx->ticket, &ctx->bo_list,
139038ecc50SThomas Hellstrom 				    (void *) fence);
140038ecc50SThomas Hellstrom }
141038ecc50SThomas Hellstrom 
142038ecc50SThomas Hellstrom /**
14364ad2abfSThomas Hellstrom  * vmw_validation_align - Align a validation memory allocation
14464ad2abfSThomas Hellstrom  * @val: The size to be aligned
14564ad2abfSThomas Hellstrom  *
14664ad2abfSThomas Hellstrom  * Returns: @val aligned to the granularity used by the validation memory
14764ad2abfSThomas Hellstrom  * allocator.
14864ad2abfSThomas Hellstrom  */
vmw_validation_align(unsigned int val)14964ad2abfSThomas Hellstrom static inline unsigned int vmw_validation_align(unsigned int val)
15064ad2abfSThomas Hellstrom {
15164ad2abfSThomas Hellstrom 	return ALIGN(val, sizeof(long));
15264ad2abfSThomas Hellstrom }
15364ad2abfSThomas Hellstrom 
154038ecc50SThomas Hellstrom int vmw_validation_add_bo(struct vmw_validation_context *ctx,
155*39985eeaSZack Rusin 			  struct vmw_bo *vbo);
156038ecc50SThomas Hellstrom int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr);
157038ecc50SThomas Hellstrom void vmw_validation_unref_lists(struct vmw_validation_context *ctx);
158038ecc50SThomas Hellstrom int vmw_validation_add_resource(struct vmw_validation_context *ctx,
159038ecc50SThomas Hellstrom 				struct vmw_resource *res,
160038ecc50SThomas Hellstrom 				size_t priv_size,
161a9f58c45SThomas Hellstrom 				u32 dirty,
162038ecc50SThomas Hellstrom 				void **p_node,
163038ecc50SThomas Hellstrom 				bool *first_usage);
164038ecc50SThomas Hellstrom void vmw_validation_drop_ht(struct vmw_validation_context *ctx);
165038ecc50SThomas Hellstrom int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
166038ecc50SThomas Hellstrom 			       bool intr);
167038ecc50SThomas Hellstrom void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
168038ecc50SThomas Hellstrom 				  bool backoff);
169038ecc50SThomas Hellstrom void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
170038ecc50SThomas Hellstrom 				      void *val_private,
17109881d29SZack Rusin 				      struct vmw_bo *vbo,
172038ecc50SThomas Hellstrom 				      unsigned long backup_offset);
173038ecc50SThomas Hellstrom int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr);
174038ecc50SThomas Hellstrom 
175038ecc50SThomas Hellstrom int vmw_validation_prepare(struct vmw_validation_context *ctx,
176038ecc50SThomas Hellstrom 			   struct mutex *mutex, bool intr);
177038ecc50SThomas Hellstrom void vmw_validation_revert(struct vmw_validation_context *ctx);
178038ecc50SThomas Hellstrom void vmw_validation_done(struct vmw_validation_context *ctx,
179038ecc50SThomas Hellstrom 			 struct vmw_fence_obj *fence);
180038ecc50SThomas Hellstrom 
18164ad2abfSThomas Hellstrom void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
18264ad2abfSThomas Hellstrom 			       unsigned int size);
18364ad2abfSThomas Hellstrom int vmw_validation_preload_bo(struct vmw_validation_context *ctx);
18464ad2abfSThomas Hellstrom int vmw_validation_preload_res(struct vmw_validation_context *ctx,
18564ad2abfSThomas Hellstrom 			       unsigned int size);
186a9f58c45SThomas Hellstrom void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
187a9f58c45SThomas Hellstrom 				  void *val_private, u32 dirty);
188b7468b15SThomas Hellstrom void vmw_validation_bo_backoff(struct vmw_validation_context *ctx);
189b7468b15SThomas Hellstrom 
190038ecc50SThomas Hellstrom #endif
191