linux/drivers/gpu/drm/i915/gem/i915_gem_context_types.h
<<
>>
Prefs
   1/*
   2 * SPDX-License-Identifier: MIT
   3 *
   4 * Copyright © 2019 Intel Corporation
   5 */
   6
   7#ifndef __I915_GEM_CONTEXT_TYPES_H__
   8#define __I915_GEM_CONTEXT_TYPES_H__
   9
  10#include <linux/atomic.h>
  11#include <linux/list.h>
  12#include <linux/llist.h>
  13#include <linux/kref.h>
  14#include <linux/mutex.h>
  15#include <linux/radix-tree.h>
  16#include <linux/rbtree.h>
  17#include <linux/rcupdate.h>
  18#include <linux/types.h>
  19
  20#include "gt/intel_context_types.h"
  21
  22#include "i915_scheduler.h"
  23#include "i915_sw_fence.h"
  24
  25struct pid;
  26
  27struct drm_i915_private;
  28struct drm_i915_file_private;
  29struct i915_address_space;
  30struct intel_timeline;
  31struct intel_ring;
  32
  33/**
  34 * struct i915_gem_engines - A set of engines
  35 */
  36struct i915_gem_engines {
  37        union {
  38                /** @link: Link in i915_gem_context::stale::engines */
  39                struct list_head link;
  40
  41                /** @rcu: RCU to use when freeing */
  42                struct rcu_head rcu;
  43        };
  44
  45        /** @fence: Fence used for delayed destruction of engines */
  46        struct i915_sw_fence fence;
  47
  48        /** @ctx: i915_gem_context backpointer */
  49        struct i915_gem_context *ctx;
  50
  51        /** @num_engines: Number of engines in this set */
  52        unsigned int num_engines;
  53
  54        /** @engines: Array of engines */
  55        struct intel_context *engines[];
  56};
  57
  58/**
  59 * struct i915_gem_engines_iter - Iterator for an i915_gem_engines set
  60 */
  61struct i915_gem_engines_iter {
  62        /** @idx: Index into i915_gem_engines::engines */
  63        unsigned int idx;
  64
  65        /** @engines: Engine set being iterated */
  66        const struct i915_gem_engines *engines;
  67};
  68
  69/**
  70 * enum i915_gem_engine_type - Describes the type of an i915_gem_proto_engine
  71 */
  72enum i915_gem_engine_type {
  73        /** @I915_GEM_ENGINE_TYPE_INVALID: An invalid engine */
  74        I915_GEM_ENGINE_TYPE_INVALID = 0,
  75
  76        /** @I915_GEM_ENGINE_TYPE_PHYSICAL: A single physical engine */
  77        I915_GEM_ENGINE_TYPE_PHYSICAL,
  78
  79        /** @I915_GEM_ENGINE_TYPE_BALANCED: A load-balanced engine set */
  80        I915_GEM_ENGINE_TYPE_BALANCED,
  81};
  82
  83/**
  84 * struct i915_gem_proto_engine - prototype engine
  85 *
  86 * This struct describes an engine that a context may contain.  Engines
  87 * have three types:
  88 *
  89 *  - I915_GEM_ENGINE_TYPE_INVALID: Invalid engines can be created but they
  90 *    show up as a NULL in i915_gem_engines::engines[i] and any attempt to
  91 *    use them by the user results in -EINVAL.  They are also useful during
  92 *    proto-context construction because the client may create invalid
  93 *    engines and then set them up later as virtual engines.
  94 *
  95 *  - I915_GEM_ENGINE_TYPE_PHYSICAL: A single physical engine, described by
  96 *    i915_gem_proto_engine::engine.
  97 *
  98 *  - I915_GEM_ENGINE_TYPE_BALANCED: A load-balanced engine set, described
  99 *    i915_gem_proto_engine::num_siblings and i915_gem_proto_engine::siblings.
 100 */
 101struct i915_gem_proto_engine {
 102        /** @type: Type of this engine */
 103        enum i915_gem_engine_type type;
 104
 105        /** @engine: Engine, for physical */
 106        struct intel_engine_cs *engine;
 107
 108        /** @num_siblings: Number of balanced siblings */
 109        unsigned int num_siblings;
 110
 111        /** @siblings: Balanced siblings */
 112        struct intel_engine_cs **siblings;
 113
 114        /** @sseu: Client-set SSEU parameters */
 115        struct intel_sseu sseu;
 116};
 117
 118/**
 119 * struct i915_gem_proto_context - prototype context
 120 *
 121 * The struct i915_gem_proto_context represents the creation parameters for
 122 * a struct i915_gem_context.  This is used to gather parameters provided
 123 * either through creation flags or via SET_CONTEXT_PARAM so that, when we
 124 * create the final i915_gem_context, those parameters can be immutable.
 125 *
 126 * The context uAPI allows for two methods of setting context parameters:
 127 * SET_CONTEXT_PARAM and CONTEXT_CREATE_EXT_SETPARAM.  The former is
 128 * allowed to be called at any time while the later happens as part of
 129 * GEM_CONTEXT_CREATE.  When these were initially added, Currently,
 130 * everything settable via one is settable via the other.  While some
 131 * params are fairly simple and setting them on a live context is harmless
 132 * such the context priority, others are far trickier such as the VM or the
 133 * set of engines.  To avoid some truly nasty race conditions, we don't
 134 * allow setting the VM or the set of engines on live contexts.
 135 *
 136 * The way we dealt with this without breaking older userspace that sets
 137 * the VM or engine set via SET_CONTEXT_PARAM is to delay the creation of
 138 * the actual context until after the client is done configuring it with
 139 * SET_CONTEXT_PARAM.  From the perspective of the client, it has the same
 140 * u32 context ID the whole time.  From the perspective of i915, however,
 141 * it's an i915_gem_proto_context right up until the point where we attempt
 142 * to do something which the proto-context can't handle at which point the
 143 * real context gets created.
 144 *
 145 * This is accomplished via a little xarray dance.  When GEM_CONTEXT_CREATE
 146 * is called, we create a proto-context, reserve a slot in context_xa but
 147 * leave it NULL, the proto-context in the corresponding slot in
 148 * proto_context_xa.  Then, whenever we go to look up a context, we first
 149 * check context_xa.  If it's there, we return the i915_gem_context and
 150 * we're done.  If it's not, we look in proto_context_xa and, if we find it
 151 * there, we create the actual context and kill the proto-context.
 152 *
 153 * At the time we made this change (April, 2021), we did a fairly complete
 154 * audit of existing userspace to ensure this wouldn't break anything:
 155 *
 156 *  - Mesa/i965 didn't use the engines or VM APIs at all
 157 *
 158 *  - Mesa/ANV used the engines API but via CONTEXT_CREATE_EXT_SETPARAM and
 159 *    didn't use the VM API.
 160 *
 161 *  - Mesa/iris didn't use the engines or VM APIs at all
 162 *
 163 *  - The open-source compute-runtime didn't yet use the engines API but
 164 *    did use the VM API via SET_CONTEXT_PARAM.  However, CONTEXT_SETPARAM
 165 *    was always the second ioctl on that context, immediately following
 166 *    GEM_CONTEXT_CREATE.
 167 *
 168 *  - The media driver sets engines and bonding/balancing via
 169 *    SET_CONTEXT_PARAM.  However, CONTEXT_SETPARAM to set the VM was
 170 *    always the second ioctl on that context, immediately following
 171 *    GEM_CONTEXT_CREATE and setting engines immediately followed that.
 172 *
 173 * In order for this dance to work properly, any modification to an
 174 * i915_gem_proto_context that is exposed to the client via
 175 * drm_i915_file_private::proto_context_xa must be guarded by
 176 * drm_i915_file_private::proto_context_lock.  The exception is when a
 177 * proto-context has not yet been exposed such as when handling
 178 * CONTEXT_CREATE_SET_PARAM during GEM_CONTEXT_CREATE.
 179 */
 180struct i915_gem_proto_context {
 181        /** @vm: See &i915_gem_context.vm */
 182        struct i915_address_space *vm;
 183
 184        /** @user_flags: See &i915_gem_context.user_flags */
 185        unsigned long user_flags;
 186
 187        /** @sched: See &i915_gem_context.sched */
 188        struct i915_sched_attr sched;
 189
 190        /** @num_user_engines: Number of user-specified engines or -1 */
 191        int num_user_engines;
 192
 193        /** @user_engines: User-specified engines */
 194        struct i915_gem_proto_engine *user_engines;
 195
 196        /** @legacy_rcs_sseu: Client-set SSEU parameters for the legacy RCS */
 197        struct intel_sseu legacy_rcs_sseu;
 198
 199        /** @single_timeline: See See &i915_gem_context.syncobj */
 200        bool single_timeline;
 201};
 202
 203/**
 204 * struct i915_gem_context - client state
 205 *
 206 * The struct i915_gem_context represents the combined view of the driver and
 207 * logical hardware state for a particular client.
 208 */
 209struct i915_gem_context {
 210        /** @i915: i915 device backpointer */
 211        struct drm_i915_private *i915;
 212
 213        /** @file_priv: owning file descriptor */
 214        struct drm_i915_file_private *file_priv;
 215
 216        /**
 217         * @engines: User defined engines for this context
 218         *
 219         * Various uAPI offer the ability to lookup up an
 220         * index from this array to select an engine operate on.
 221         *
 222         * Multiple logically distinct instances of the same engine
 223         * may be defined in the array, as well as composite virtual
 224         * engines.
 225         *
 226         * Execbuf uses the I915_EXEC_RING_MASK as an index into this
 227         * array to select which HW context + engine to execute on. For
 228         * the default array, the user_ring_map[] is used to translate
 229         * the legacy uABI onto the approprate index (e.g. both
 230         * I915_EXEC_DEFAULT and I915_EXEC_RENDER select the same
 231         * context, and I915_EXEC_BSD is weird). For a use defined
 232         * array, execbuf uses I915_EXEC_RING_MASK as a plain index.
 233         *
 234         * User defined by I915_CONTEXT_PARAM_ENGINE (when the
 235         * CONTEXT_USER_ENGINES flag is set).
 236         */
 237        struct i915_gem_engines __rcu *engines;
 238
 239        /** @engines_mutex: guards writes to engines */
 240        struct mutex engines_mutex;
 241
 242        /**
 243         * @syncobj: Shared timeline syncobj
 244         *
 245         * When the SHARED_TIMELINE flag is set on context creation, we
 246         * emulate a single timeline across all engines using this syncobj.
 247         * For every execbuffer2 call, this syncobj is used as both an in-
 248         * and out-fence.  Unlike the real intel_timeline, this doesn't
 249         * provide perfect atomic in-order guarantees if the client races
 250         * with itself by calling execbuffer2 twice concurrently.  However,
 251         * if userspace races with itself, that's not likely to yield well-
 252         * defined results anyway so we choose to not care.
 253         */
 254        struct drm_syncobj *syncobj;
 255
 256        /**
 257         * @vm: unique address space (GTT)
 258         *
 259         * In full-ppgtt mode, each context has its own address space ensuring
 260         * complete seperation of one client from all others.
 261         *
 262         * In other modes, this is a NULL pointer with the expectation that
 263         * the caller uses the shared global GTT.
 264         */
 265        struct i915_address_space __rcu *vm;
 266
 267        /**
 268         * @pid: process id of creator
 269         *
 270         * Note that who created the context may not be the principle user,
 271         * as the context may be shared across a local socket. However,
 272         * that should only affect the default context, all contexts created
 273         * explicitly by the client are expected to be isolated.
 274         */
 275        struct pid *pid;
 276
 277        /** @link: place with &drm_i915_private.context_list */
 278        struct list_head link;
 279
 280        /**
 281         * @ref: reference count
 282         *
 283         * A reference to a context is held by both the client who created it
 284         * and on each request submitted to the hardware using the request
 285         * (to ensure the hardware has access to the state until it has
 286         * finished all pending writes). See i915_gem_context_get() and
 287         * i915_gem_context_put() for access.
 288         */
 289        struct kref ref;
 290
 291        /**
 292         * @rcu: rcu_head for deferred freeing.
 293         */
 294        struct rcu_head rcu;
 295
 296        /**
 297         * @user_flags: small set of booleans controlled by the user
 298         */
 299        unsigned long user_flags;
 300#define UCONTEXT_NO_ERROR_CAPTURE       1
 301#define UCONTEXT_BANNABLE               2
 302#define UCONTEXT_RECOVERABLE            3
 303#define UCONTEXT_PERSISTENCE            4
 304
 305        /**
 306         * @flags: small set of booleans
 307         */
 308        unsigned long flags;
 309#define CONTEXT_CLOSED                  0
 310#define CONTEXT_USER_ENGINES            1
 311
 312        /** @mutex: guards everything that isn't engines or handles_vma */
 313        struct mutex mutex;
 314
 315        /** @sched: scheduler parameters */
 316        struct i915_sched_attr sched;
 317
 318        /** @guilty_count: How many times this context has caused a GPU hang. */
 319        atomic_t guilty_count;
 320        /**
 321         * @active_count: How many times this context was active during a GPU
 322         * hang, but did not cause it.
 323         */
 324        atomic_t active_count;
 325
 326        /**
 327         * @hang_timestamp: The last time(s) this context caused a GPU hang
 328         */
 329        unsigned long hang_timestamp[2];
 330#define CONTEXT_FAST_HANG_JIFFIES (120 * HZ) /* 3 hangs within 120s? Banned! */
 331
 332        /** @remap_slice: Bitmask of cache lines that need remapping */
 333        u8 remap_slice;
 334
 335        /**
 336         * @handles_vma: rbtree to look up our context specific obj/vma for
 337         * the user handle. (user handles are per fd, but the binding is
 338         * per vm, which may be one per context or shared with the global GTT)
 339         */
 340        struct radix_tree_root handles_vma;
 341
 342        /** @lut_mutex: Locks handles_vma */
 343        struct mutex lut_mutex;
 344
 345        /**
 346         * @name: arbitrary name, used for user debug
 347         *
 348         * A name is constructed for the context from the creator's process
 349         * name, pid and user handle in order to uniquely identify the
 350         * context in messages.
 351         */
 352        char name[TASK_COMM_LEN + 8];
 353
 354        /** @stale: tracks stale engines to be destroyed */
 355        struct {
 356                /** @lock: guards engines */
 357                spinlock_t lock;
 358                /** @engines: list of stale engines */
 359                struct list_head engines;
 360        } stale;
 361};
 362
 363#endif /* __I915_GEM_CONTEXT_TYPES_H__ */
 364