Home | History | Annotate | Line # | Download | only in gem
      1 /*	$NetBSD: i915_gem_context.h,v 1.2 2021/12/18 23:45:30 riastradh Exp $	*/
      2 
      3 /*
      4  * SPDX-License-Identifier: MIT
      5  *
      6  * Copyright  2016 Intel Corporation
      7  */
      8 
      9 #ifndef __I915_GEM_CONTEXT_H__
     10 #define __I915_GEM_CONTEXT_H__
     11 
     12 #include "i915_gem_context_types.h"
     13 
     14 #include "gt/intel_context.h"
     15 
     16 #include "i915_drv.h"
     17 #include "i915_gem.h"
     18 #include "i915_scheduler.h"
     19 #include "intel_device_info.h"
     20 
     21 struct drm_device;
     22 struct drm_file;
     23 
     24 static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
     25 {
     26 	return test_bit(CONTEXT_CLOSED, &ctx->flags);
     27 }
     28 
     29 static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
     30 {
     31 	GEM_BUG_ON(i915_gem_context_is_closed(ctx));
     32 	set_bit(CONTEXT_CLOSED, &ctx->flags);
     33 }
     34 
     35 static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
     36 {
     37 	return test_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
     38 }
     39 
     40 static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
     41 {
     42 	set_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
     43 }
     44 
     45 static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
     46 {
     47 	clear_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
     48 }
     49 
     50 static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
     51 {
     52 	return test_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
     53 }
     54 
     55 static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
     56 {
     57 	set_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
     58 }
     59 
     60 static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
     61 {
     62 	clear_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
     63 }
     64 
     65 static inline bool i915_gem_context_is_recoverable(const struct i915_gem_context *ctx)
     66 {
     67 	return test_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
     68 }
     69 
     70 static inline void i915_gem_context_set_recoverable(struct i915_gem_context *ctx)
     71 {
     72 	set_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
     73 }
     74 
     75 static inline void i915_gem_context_clear_recoverable(struct i915_gem_context *ctx)
     76 {
     77 	clear_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
     78 }
     79 
     80 static inline bool i915_gem_context_is_persistent(const struct i915_gem_context *ctx)
     81 {
     82 	return test_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
     83 }
     84 
     85 static inline void i915_gem_context_set_persistence(struct i915_gem_context *ctx)
     86 {
     87 	set_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
     88 }
     89 
     90 static inline void i915_gem_context_clear_persistence(struct i915_gem_context *ctx)
     91 {
     92 	clear_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
     93 }
     94 
     95 static inline bool
     96 i915_gem_context_user_engines(const struct i915_gem_context *ctx)
     97 {
     98 	return test_bit(CONTEXT_USER_ENGINES, &ctx->flags);
     99 }
    100 
    101 static inline void
    102 i915_gem_context_set_user_engines(struct i915_gem_context *ctx)
    103 {
    104 	set_bit(CONTEXT_USER_ENGINES, &ctx->flags);
    105 }
    106 
    107 static inline void
    108 i915_gem_context_clear_user_engines(struct i915_gem_context *ctx)
    109 {
    110 	clear_bit(CONTEXT_USER_ENGINES, &ctx->flags);
    111 }
    112 
    113 /* i915_gem_context.c */
    114 void i915_gem_init__contexts(struct drm_i915_private *i915);
    115 void i915_gem_driver_release__contexts(struct drm_i915_private *i915);
    116 
    117 int i915_gem_context_open(struct drm_i915_private *i915,
    118 			  struct drm_file *file);
    119 void i915_gem_context_close(struct drm_file *file);
    120 
    121 void i915_gem_context_release(struct kref *ctx_ref);
    122 
    123 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
    124 			     struct drm_file *file);
    125 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
    126 			      struct drm_file *file);
    127 
    128 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
    129 				  struct drm_file *file);
    130 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
    131 				   struct drm_file *file);
    132 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
    133 				    struct drm_file *file_priv);
    134 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
    135 				    struct drm_file *file_priv);
    136 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
    137 				       struct drm_file *file);
    138 
    139 static inline struct i915_gem_context *
    140 i915_gem_context_get(struct i915_gem_context *ctx)
    141 {
    142 	kref_get(&ctx->ref);
    143 	return ctx;
    144 }
    145 
    146 static inline void i915_gem_context_put(struct i915_gem_context *ctx)
    147 {
    148 	kref_put(&ctx->ref, i915_gem_context_release);
    149 }
    150 
    151 static inline struct i915_address_space *
    152 i915_gem_context_vm(struct i915_gem_context *ctx)
    153 {
    154 	return rcu_dereference_protected(ctx->vm, lockdep_is_held(&ctx->mutex));
    155 }
    156 
    157 static inline struct i915_address_space *
    158 i915_gem_context_get_vm_rcu(struct i915_gem_context *ctx)
    159 {
    160 	struct i915_address_space *vm;
    161 
    162 	rcu_read_lock();
    163 	vm = rcu_dereference(ctx->vm);
    164 	if (!vm)
    165 		vm = &ctx->i915->ggtt.vm;
    166 	vm = i915_vm_get(vm);
    167 	rcu_read_unlock();
    168 
    169 	return vm;
    170 }
    171 
    172 static inline struct i915_gem_engines *
    173 i915_gem_context_engines(struct i915_gem_context *ctx)
    174 {
    175 	return rcu_dereference_protected(ctx->engines,
    176 					 lockdep_is_held(&ctx->engines_mutex));
    177 }
    178 
    179 static inline struct i915_gem_engines *
    180 i915_gem_context_lock_engines(struct i915_gem_context *ctx)
    181 	__acquires(&ctx->engines_mutex)
    182 {
    183 	mutex_lock(&ctx->engines_mutex);
    184 	return i915_gem_context_engines(ctx);
    185 }
    186 
    187 static inline void
    188 i915_gem_context_unlock_engines(struct i915_gem_context *ctx)
    189 	__releases(&ctx->engines_mutex)
    190 {
    191 	mutex_unlock(&ctx->engines_mutex);
    192 }
    193 
    194 static inline struct intel_context *
    195 i915_gem_context_get_engine(struct i915_gem_context *ctx, unsigned int idx)
    196 {
    197 	struct intel_context *ce = ERR_PTR(-EINVAL);
    198 
    199 	rcu_read_lock(); {
    200 		struct i915_gem_engines *e = rcu_dereference(ctx->engines);
    201 		if (likely(idx < e->num_engines && e->engines[idx]))
    202 			ce = intel_context_get(e->engines[idx]);
    203 	} rcu_read_unlock();
    204 
    205 	return ce;
    206 }
    207 
    208 static inline void
    209 i915_gem_engines_iter_init(struct i915_gem_engines_iter *it,
    210 			   struct i915_gem_engines *engines)
    211 {
    212 	GEM_BUG_ON(!engines);
    213 	it->engines = engines;
    214 	it->idx = 0;
    215 }
    216 
    217 struct intel_context *
    218 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it);
    219 
    220 #define for_each_gem_engine(ce, engines, it) \
    221 	for (i915_gem_engines_iter_init(&(it), (engines)); \
    222 	     ((ce) = i915_gem_engines_iter_next(&(it)));)
    223 
    224 struct i915_lut_handle *i915_lut_handle_alloc(void);
    225 void i915_lut_handle_free(struct i915_lut_handle *lut);
    226 
    227 #endif /* !__I915_GEM_CONTEXT_H__ */
    228