i915_gem_context.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef __I915_GEM_CONTEXT_H__
  25. #define __I915_GEM_CONTEXT_H__
  26. #include <linux/bitops.h>
  27. #include <linux/list.h>
  28. #include <linux/radix-tree.h>
  29. #include "i915_gem.h"
  30. struct pid;
  31. struct drm_device;
  32. struct drm_file;
  33. struct drm_i915_private;
  34. struct drm_i915_file_private;
  35. struct i915_hw_ppgtt;
  36. struct i915_request;
  37. struct i915_vma;
  38. struct intel_ring;
  39. #define DEFAULT_CONTEXT_HANDLE 0
  40. /**
  41. * struct i915_gem_context - client state
  42. *
  43. * The struct i915_gem_context represents the combined view of the driver and
  44. * logical hardware state for a particular client.
  45. */
  46. struct i915_gem_context {
  47. /** i915: i915 device backpointer */
  48. struct drm_i915_private *i915;
  49. /** file_priv: owning file descriptor */
  50. struct drm_i915_file_private *file_priv;
  51. /**
  52. * @ppgtt: unique address space (GTT)
  53. *
  54. * In full-ppgtt mode, each context has its own address space ensuring
  55. * complete seperation of one client from all others.
  56. *
  57. * In other modes, this is a NULL pointer with the expectation that
  58. * the caller uses the shared global GTT.
  59. */
  60. struct i915_hw_ppgtt *ppgtt;
  61. /**
  62. * @pid: process id of creator
  63. *
  64. * Note that who created the context may not be the principle user,
  65. * as the context may be shared across a local socket. However,
  66. * that should only affect the default context, all contexts created
  67. * explicitly by the client are expected to be isolated.
  68. */
  69. struct pid *pid;
  70. /**
  71. * @name: arbitrary name
  72. *
  73. * A name is constructed for the context from the creator's process
  74. * name, pid and user handle in order to uniquely identify the
  75. * context in messages.
  76. */
  77. const char *name;
  78. /** link: place with &drm_i915_private.context_list */
  79. struct list_head link;
  80. struct llist_node free_link;
  81. /**
  82. * @ref: reference count
  83. *
  84. * A reference to a context is held by both the client who created it
  85. * and on each request submitted to the hardware using the request
  86. * (to ensure the hardware has access to the state until it has
  87. * finished all pending writes). See i915_gem_context_get() and
  88. * i915_gem_context_put() for access.
  89. */
  90. struct kref ref;
  91. /**
  92. * @rcu: rcu_head for deferred freeing.
  93. */
  94. struct rcu_head rcu;
  95. /**
  96. * @flags: small set of booleans
  97. */
  98. unsigned long flags;
  99. #define CONTEXT_NO_ZEROMAP BIT(0)
  100. #define CONTEXT_NO_ERROR_CAPTURE 1
  101. #define CONTEXT_CLOSED 2
  102. #define CONTEXT_BANNABLE 3
  103. #define CONTEXT_BANNED 4
  104. #define CONTEXT_FORCE_SINGLE_SUBMISSION 5
  105. /**
  106. * @hw_id: - unique identifier for the context
  107. *
  108. * The hardware needs to uniquely identify the context for a few
  109. * functions like fault reporting, PASID, scheduling. The
  110. * &drm_i915_private.context_hw_ida is used to assign a unqiue
  111. * id for the lifetime of the context.
  112. */
  113. unsigned int hw_id;
  114. /**
  115. * @user_handle: userspace identifier
  116. *
  117. * A unique per-file identifier is generated from
  118. * &drm_i915_file_private.contexts.
  119. */
  120. u32 user_handle;
  121. struct i915_sched_attr sched;
  122. /** ggtt_offset_bias: placement restriction for context objects */
  123. u32 ggtt_offset_bias;
  124. /** engine: per-engine logical HW state */
  125. struct intel_context {
  126. struct i915_vma *state;
  127. struct intel_ring *ring;
  128. u32 *lrc_reg_state;
  129. u64 lrc_desc;
  130. int pin_count;
  131. } __engine[I915_NUM_ENGINES];
  132. /** ring_size: size for allocating the per-engine ring buffer */
  133. u32 ring_size;
  134. /** desc_template: invariant fields for the HW context descriptor */
  135. u32 desc_template;
  136. /** guilty_count: How many times this context has caused a GPU hang. */
  137. atomic_t guilty_count;
  138. /**
  139. * @active_count: How many times this context was active during a GPU
  140. * hang, but did not cause it.
  141. */
  142. atomic_t active_count;
  143. #define CONTEXT_SCORE_GUILTY 10
  144. #define CONTEXT_SCORE_BAN_THRESHOLD 40
  145. /** ban_score: Accumulated score of all hangs caused by this context. */
  146. atomic_t ban_score;
  147. /** remap_slice: Bitmask of cache lines that need remapping */
  148. u8 remap_slice;
  149. /** handles_vma: rbtree to look up our context specific obj/vma for
  150. * the user handle. (user handles are per fd, but the binding is
  151. * per vm, which may be one per context or shared with the global GTT)
  152. */
  153. struct radix_tree_root handles_vma;
  154. /** handles_list: reverse list of all the rbtree entries in use for
  155. * this context, which allows us to free all the allocations on
  156. * context close.
  157. */
  158. struct list_head handles_list;
  159. };
  160. static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
  161. {
  162. return test_bit(CONTEXT_CLOSED, &ctx->flags);
  163. }
  164. static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
  165. {
  166. GEM_BUG_ON(i915_gem_context_is_closed(ctx));
  167. __set_bit(CONTEXT_CLOSED, &ctx->flags);
  168. }
  169. static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
  170. {
  171. return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  172. }
  173. static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
  174. {
  175. __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  176. }
  177. static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
  178. {
  179. __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  180. }
  181. static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
  182. {
  183. return test_bit(CONTEXT_BANNABLE, &ctx->flags);
  184. }
  185. static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
  186. {
  187. __set_bit(CONTEXT_BANNABLE, &ctx->flags);
  188. }
  189. static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
  190. {
  191. __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
  192. }
  193. static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
  194. {
  195. return test_bit(CONTEXT_BANNED, &ctx->flags);
  196. }
  197. static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
  198. {
  199. __set_bit(CONTEXT_BANNED, &ctx->flags);
  200. }
  201. static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
  202. {
  203. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  204. }
  205. static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
  206. {
  207. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  208. }
  209. static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
  210. {
  211. return c->user_handle == DEFAULT_CONTEXT_HANDLE;
  212. }
  213. static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
  214. {
  215. return !ctx->file_priv;
  216. }
  217. static inline struct intel_context *
  218. to_intel_context(struct i915_gem_context *ctx,
  219. const struct intel_engine_cs *engine)
  220. {
  221. return &ctx->__engine[engine->id];
  222. }
  223. static inline struct intel_ring *
  224. intel_context_pin(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
  225. {
  226. return engine->context_pin(engine, ctx);
  227. }
  228. static inline void __intel_context_pin(struct i915_gem_context *ctx,
  229. const struct intel_engine_cs *engine)
  230. {
  231. struct intel_context *ce = to_intel_context(ctx, engine);
  232. GEM_BUG_ON(!ce->pin_count);
  233. ce->pin_count++;
  234. }
  235. static inline void intel_context_unpin(struct i915_gem_context *ctx,
  236. struct intel_engine_cs *engine)
  237. {
  238. engine->context_unpin(engine, ctx);
  239. }
  240. /* i915_gem_context.c */
  241. int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
  242. void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
  243. void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
  244. int i915_gem_context_open(struct drm_i915_private *i915,
  245. struct drm_file *file);
  246. void i915_gem_context_close(struct drm_file *file);
  247. int i915_switch_context(struct i915_request *rq);
  248. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
  249. void i915_gem_context_release(struct kref *ctx_ref);
  250. struct i915_gem_context *
  251. i915_gem_context_create_gvt(struct drm_device *dev);
  252. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  253. struct drm_file *file);
  254. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  255. struct drm_file *file);
  256. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  257. struct drm_file *file_priv);
  258. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  259. struct drm_file *file_priv);
  260. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
  261. struct drm_file *file);
  262. struct i915_gem_context *
  263. i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio);
  264. static inline struct i915_gem_context *
  265. i915_gem_context_get(struct i915_gem_context *ctx)
  266. {
  267. kref_get(&ctx->ref);
  268. return ctx;
  269. }
  270. static inline void i915_gem_context_put(struct i915_gem_context *ctx)
  271. {
  272. kref_put(&ctx->ref, i915_gem_context_release);
  273. }
  274. #endif /* !__I915_GEM_CONTEXT_H__ */