Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
0002 /**************************************************************************
0003  *
0004  * Copyright © 2018 VMware, Inc., Palo Alto, CA., USA
0005  * All Rights Reserved.
0006  *
0007  * Permission is hereby granted, free of charge, to any person obtaining a
0008  * copy of this software and associated documentation files (the
0009  * "Software"), to deal in the Software without restriction, including
0010  * without limitation the rights to use, copy, modify, merge, publish,
0011  * distribute, sub license, and/or sell copies of the Software, and to
0012  * permit persons to whom the Software is furnished to do so, subject to
0013  * the following conditions:
0014  *
0015  * The above copyright notice and this permission notice (including the
0016  * next paragraph) shall be included in all copies or substantial portions
0017  * of the Software.
0018  *
0019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0022  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0023  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0024  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0025  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0026  *
0027  **************************************************************************/
0028 #ifndef _VMWGFX_VALIDATION_H_
0029 #define _VMWGFX_VALIDATION_H_
0030 
0031 #include <linux/list.h>
0032 #include <linux/ww_mutex.h>
0033 
0034 #include <drm/ttm/ttm_execbuf_util.h>
0035 
0036 #include "vmwgfx_hashtab.h"
0037 
0038 #define VMW_RES_DIRTY_NONE 0
0039 #define VMW_RES_DIRTY_SET BIT(0)
0040 #define VMW_RES_DIRTY_CLEAR BIT(1)
0041 
0042 /**
0043  * struct vmw_validation_context - Per command submission validation context
0044  * @ht: Hash table used to find resource- or buffer object duplicates
0045  * @resource_list: List head for resource validation metadata
0046  * @resource_ctx_list: List head for resource validation metadata for
0047  * resources that need to be validated before those in @resource_list
0048  * @bo_list: List head for buffer objects
0049  * @page_list: List of pages used by the memory allocator
0050  * @ticket: Ticked used for ww mutex locking
0051  * @res_mutex: Pointer to mutex used for resource reserving
0052  * @merge_dups: Whether to merge metadata for duplicate resources or
0053  * buffer objects
0054  * @mem_size_left: Free memory left in the last page in @page_list
0055  * @page_address: Kernel virtual address of the last page in @page_list
0056  * @vm: A pointer to the memory reservation interface or NULL if no
0057  * memory reservation is needed.
0058  * @vm_size_left: Amount of reserved memory that so far has not been allocated.
0059  * @total_mem: Amount of reserved memory.
0060  */
0061 struct vmw_validation_context {
0062     struct vmwgfx_open_hash *ht;
0063     struct list_head resource_list;
0064     struct list_head resource_ctx_list;
0065     struct list_head bo_list;
0066     struct list_head page_list;
0067     struct ww_acquire_ctx ticket;
0068     struct mutex *res_mutex;
0069     unsigned int merge_dups;
0070     unsigned int mem_size_left;
0071     u8 *page_address;
0072     struct vmw_validation_mem *vm;
0073     size_t vm_size_left;
0074     size_t total_mem;
0075 };
0076 
0077 struct vmw_buffer_object;
0078 struct vmw_resource;
0079 struct vmw_fence_obj;
0080 
0081 #if 0
0082 /**
0083  * DECLARE_VAL_CONTEXT - Declare a validation context with initialization
0084  * @_name: The name of the variable
0085  * @_ht: The hash table used to find dups or NULL if none
0086  * @_merge_dups: Whether to merge duplicate buffer object- or resource
0087  * entries. If set to true, ideally a hash table pointer should be supplied
0088  * as well unless the number of resources and buffer objects per validation
0089  * is known to be very small
0090  */
0091 #endif
0092 #define DECLARE_VAL_CONTEXT(_name, _ht, _merge_dups)            \
0093     struct vmw_validation_context _name =               \
0094     { .ht = _ht,                            \
0095       .resource_list = LIST_HEAD_INIT((_name).resource_list),   \
0096       .resource_ctx_list = LIST_HEAD_INIT((_name).resource_ctx_list), \
0097       .bo_list = LIST_HEAD_INIT((_name).bo_list),           \
0098       .page_list = LIST_HEAD_INIT((_name).page_list),       \
0099       .res_mutex = NULL,                        \
0100       .merge_dups = _merge_dups,                    \
0101       .mem_size_left = 0,                       \
0102     }
0103 
0104 /**
0105  * vmw_validation_has_bos - return whether the validation context has
0106  * any buffer objects registered.
0107  *
0108  * @ctx: The validation context
0109  * Returns: Whether any buffer objects are registered
0110  */
0111 static inline bool
0112 vmw_validation_has_bos(struct vmw_validation_context *ctx)
0113 {
0114     return !list_empty(&ctx->bo_list);
0115 }
0116 
0117 /**
0118  * vmw_validation_set_ht - Register a hash table for duplicate finding
0119  * @ctx: The validation context
0120  * @ht: Pointer to a hash table to use for duplicate finding
0121  * This function is intended to be used if the hash table wasn't
0122  * available at validation context declaration time
0123  */
0124 static inline void vmw_validation_set_ht(struct vmw_validation_context *ctx,
0125                      struct vmwgfx_open_hash *ht)
0126 {
0127     ctx->ht = ht;
0128 }
0129 
0130 /**
0131  * vmw_validation_bo_reserve - Reserve buffer objects registered with a
0132  * validation context
0133  * @ctx: The validation context
0134  * @intr: Perform waits interruptible
0135  *
0136  * Return: Zero on success, -ERESTARTSYS when interrupted, negative error
0137  * code on failure
0138  */
0139 static inline int
0140 vmw_validation_bo_reserve(struct vmw_validation_context *ctx,
0141               bool intr)
0142 {
0143     return ttm_eu_reserve_buffers(&ctx->ticket, &ctx->bo_list, intr,
0144                       NULL);
0145 }
0146 
0147 /**
0148  * vmw_validation_bo_fence - Unreserve and fence buffer objects registered
0149  * with a validation context
0150  * @ctx: The validation context
0151  *
0152  * This function unreserves the buffer objects previously reserved using
0153  * vmw_validation_bo_reserve, and fences them with a fence object.
0154  */
0155 static inline void
0156 vmw_validation_bo_fence(struct vmw_validation_context *ctx,
0157             struct vmw_fence_obj *fence)
0158 {
0159     ttm_eu_fence_buffer_objects(&ctx->ticket, &ctx->bo_list,
0160                     (void *) fence);
0161 }
0162 
0163 /**
0164  * vmw_validation_align - Align a validation memory allocation
0165  * @val: The size to be aligned
0166  *
0167  * Returns: @val aligned to the granularity used by the validation memory
0168  * allocator.
0169  */
0170 static inline unsigned int vmw_validation_align(unsigned int val)
0171 {
0172     return ALIGN(val, sizeof(long));
0173 }
0174 
0175 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
0176               struct vmw_buffer_object *vbo,
0177               bool as_mob, bool cpu_blit);
0178 int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
0179                       bool interruptible,
0180                       bool validate_as_mob);
0181 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr);
0182 void vmw_validation_unref_lists(struct vmw_validation_context *ctx);
0183 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
0184                 struct vmw_resource *res,
0185                 size_t priv_size,
0186                 u32 dirty,
0187                 void **p_node,
0188                 bool *first_usage);
0189 void vmw_validation_drop_ht(struct vmw_validation_context *ctx);
0190 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
0191                    bool intr);
0192 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
0193                   bool backoff);
0194 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
0195                       void *val_private,
0196                       struct vmw_buffer_object *vbo,
0197                       unsigned long backup_offset);
0198 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr);
0199 
0200 int vmw_validation_prepare(struct vmw_validation_context *ctx,
0201                struct mutex *mutex, bool intr);
0202 void vmw_validation_revert(struct vmw_validation_context *ctx);
0203 void vmw_validation_done(struct vmw_validation_context *ctx,
0204              struct vmw_fence_obj *fence);
0205 
0206 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
0207                    unsigned int size);
0208 int vmw_validation_preload_bo(struct vmw_validation_context *ctx);
0209 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
0210                    unsigned int size);
0211 void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
0212                   void *val_private, u32 dirty);
0213 void vmw_validation_bo_backoff(struct vmw_validation_context *ctx);
0214 
0215 #endif