i915_gem_context.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /**
  122. * @priority: execution and service priority
  123. *
  124. * All clients are equal, but some are more equal than others!
  125. *
  126. * Requests from a context with a greater (more positive) value of
  127. * @priority will be executed before those with a lower @priority
  128. * value, forming a simple QoS.
  129. *
  130. * The &drm_i915_private.kernel_context is assigned the lowest priority.
  131. */
  132. int priority;
  133. /** ggtt_offset_bias: placement restriction for context objects */
  134. u32 ggtt_offset_bias;
  135. /** engine: per-engine logical HW state */
  136. struct intel_context {
  137. struct i915_vma *state;
  138. struct intel_ring *ring;
  139. u32 *lrc_reg_state;
  140. u64 lrc_desc;
  141. int pin_count;
  142. } engine[I915_NUM_ENGINES];
  143. /** ring_size: size for allocating the per-engine ring buffer */
  144. u32 ring_size;
  145. /** desc_template: invariant fields for the HW context descriptor */
  146. u32 desc_template;
  147. /** guilty_count: How many times this context has caused a GPU hang. */
  148. atomic_t guilty_count;
  149. /**
  150. * @active_count: How many times this context was active during a GPU
  151. * hang, but did not cause it.
  152. */
  153. atomic_t active_count;
  154. #define CONTEXT_SCORE_GUILTY 10
  155. #define CONTEXT_SCORE_BAN_THRESHOLD 40
  156. /** ban_score: Accumulated score of all hangs caused by this context. */
  157. atomic_t ban_score;
  158. /** remap_slice: Bitmask of cache lines that need remapping */
  159. u8 remap_slice;
  160. /** handles_vma: rbtree to look up our context specific obj/vma for
  161. * the user handle. (user handles are per fd, but the binding is
  162. * per vm, which may be one per context or shared with the global GTT)
  163. */
  164. struct radix_tree_root handles_vma;
  165. /** handles_list: reverse list of all the rbtree entries in use for
  166. * this context, which allows us to free all the allocations on
  167. * context close.
  168. */
  169. struct list_head handles_list;
  170. };
  171. static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
  172. {
  173. return test_bit(CONTEXT_CLOSED, &ctx->flags);
  174. }
  175. static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
  176. {
  177. GEM_BUG_ON(i915_gem_context_is_closed(ctx));
  178. __set_bit(CONTEXT_CLOSED, &ctx->flags);
  179. }
  180. static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
  181. {
  182. return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  183. }
  184. static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
  185. {
  186. __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  187. }
  188. static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
  189. {
  190. __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  191. }
  192. static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
  193. {
  194. return test_bit(CONTEXT_BANNABLE, &ctx->flags);
  195. }
  196. static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
  197. {
  198. __set_bit(CONTEXT_BANNABLE, &ctx->flags);
  199. }
  200. static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
  201. {
  202. __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
  203. }
  204. static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
  205. {
  206. return test_bit(CONTEXT_BANNED, &ctx->flags);
  207. }
  208. static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
  209. {
  210. __set_bit(CONTEXT_BANNED, &ctx->flags);
  211. }
  212. static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
  213. {
  214. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  215. }
  216. static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
  217. {
  218. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  219. }
  220. static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
  221. {
  222. return c->user_handle == DEFAULT_CONTEXT_HANDLE;
  223. }
  224. static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
  225. {
  226. return !ctx->file_priv;
  227. }
  228. /* i915_gem_context.c */
  229. int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
  230. void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
  231. void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
  232. int i915_gem_context_open(struct drm_i915_private *i915,
  233. struct drm_file *file);
  234. void i915_gem_context_close(struct drm_file *file);
  235. int i915_switch_context(struct i915_request *rq);
  236. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
  237. void i915_gem_context_release(struct kref *ctx_ref);
  238. struct i915_gem_context *
  239. i915_gem_context_create_gvt(struct drm_device *dev);
  240. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  241. struct drm_file *file);
  242. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  243. struct drm_file *file);
  244. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  245. struct drm_file *file_priv);
  246. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  247. struct drm_file *file_priv);
  248. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
  249. struct drm_file *file);
  250. struct i915_gem_context *
  251. i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio);
  252. static inline struct i915_gem_context *
  253. i915_gem_context_get(struct i915_gem_context *ctx)
  254. {
  255. kref_get(&ctx->ref);
  256. return ctx;
  257. }
  258. static inline void i915_gem_context_put(struct i915_gem_context *ctx)
  259. {
  260. kref_put(&ctx->ref, i915_gem_context_release);
  261. }
  262. #endif /* !__I915_GEM_CONTEXT_H__ */