i915_gem_context.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. struct pid;
  30. struct drm_device;
  31. struct drm_file;
  32. struct drm_i915_private;
  33. struct drm_i915_file_private;
  34. struct i915_hw_ppgtt;
  35. struct i915_vma;
  36. struct intel_ring;
  37. #define DEFAULT_CONTEXT_HANDLE 0
  38. /**
  39. * struct i915_gem_context - client state
  40. *
  41. * The struct i915_gem_context represents the combined view of the driver and
  42. * logical hardware state for a particular client.
  43. */
  44. struct i915_gem_context {
  45. /** i915: i915 device backpointer */
  46. struct drm_i915_private *i915;
  47. /** file_priv: owning file descriptor */
  48. struct drm_i915_file_private *file_priv;
  49. /**
  50. * @ppgtt: unique address space (GTT)
  51. *
  52. * In full-ppgtt mode, each context has its own address space ensuring
  53. * complete seperation of one client from all others.
  54. *
  55. * In other modes, this is a NULL pointer with the expectation that
  56. * the caller uses the shared global GTT.
  57. */
  58. struct i915_hw_ppgtt *ppgtt;
  59. /**
  60. * @pid: process id of creator
  61. *
  62. * Note that who created the context may not be the principle user,
  63. * as the context may be shared across a local socket. However,
  64. * that should only affect the default context, all contexts created
  65. * explicitly by the client are expected to be isolated.
  66. */
  67. struct pid *pid;
  68. /**
  69. * @name: arbitrary name
  70. *
  71. * A name is constructed for the context from the creator's process
  72. * name, pid and user handle in order to uniquely identify the
  73. * context in messages.
  74. */
  75. const char *name;
  76. /** link: place with &drm_i915_private.context_list */
  77. struct list_head link;
  78. struct llist_node free_link;
  79. /**
  80. * @ref: reference count
  81. *
  82. * A reference to a context is held by both the client who created it
  83. * and on each request submitted to the hardware using the request
  84. * (to ensure the hardware has access to the state until it has
  85. * finished all pending writes). See i915_gem_context_get() and
  86. * i915_gem_context_put() for access.
  87. */
  88. struct kref ref;
  89. /**
  90. * @rcu: rcu_head for deferred freeing.
  91. */
  92. struct rcu_head rcu;
  93. /**
  94. * @flags: small set of booleans
  95. */
  96. unsigned long flags;
  97. #define CONTEXT_NO_ZEROMAP BIT(0)
  98. #define CONTEXT_NO_ERROR_CAPTURE 1
  99. #define CONTEXT_CLOSED 2
  100. #define CONTEXT_BANNABLE 3
  101. #define CONTEXT_BANNED 4
  102. #define CONTEXT_FORCE_SINGLE_SUBMISSION 5
  103. /**
  104. * @hw_id: - unique identifier for the context
  105. *
  106. * The hardware needs to uniquely identify the context for a few
  107. * functions like fault reporting, PASID, scheduling. The
  108. * &drm_i915_private.context_hw_ida is used to assign a unqiue
  109. * id for the lifetime of the context.
  110. */
  111. unsigned int hw_id;
  112. /**
  113. * @user_handle: userspace identifier
  114. *
  115. * A unique per-file identifier is generated from
  116. * &drm_i915_file_private.contexts.
  117. */
  118. u32 user_handle;
  119. /**
  120. * @priority: execution and service priority
  121. *
  122. * All clients are equal, but some are more equal than others!
  123. *
  124. * Requests from a context with a greater (more positive) value of
  125. * @priority will be executed before those with a lower @priority
  126. * value, forming a simple QoS.
  127. *
  128. * The &drm_i915_private.kernel_context is assigned the lowest priority.
  129. */
  130. int priority;
  131. /** ggtt_offset_bias: placement restriction for context objects */
  132. u32 ggtt_offset_bias;
  133. /** engine: per-engine logical HW state */
  134. struct intel_context {
  135. struct i915_vma *state;
  136. struct intel_ring *ring;
  137. u32 *lrc_reg_state;
  138. u64 lrc_desc;
  139. int pin_count;
  140. bool initialised;
  141. } engine[I915_NUM_ENGINES];
  142. /** ring_size: size for allocating the per-engine ring buffer */
  143. u32 ring_size;
  144. /** desc_template: invariant fields for the HW context descriptor */
  145. u32 desc_template;
  146. /** guilty_count: How many times this context has caused a GPU hang. */
  147. atomic_t guilty_count;
  148. /**
  149. * @active_count: How many times this context was active during a GPU
  150. * hang, but did not cause it.
  151. */
  152. atomic_t active_count;
  153. #define CONTEXT_SCORE_GUILTY 10
  154. #define CONTEXT_SCORE_BAN_THRESHOLD 40
  155. /** ban_score: Accumulated score of all hangs caused by this context. */
  156. atomic_t ban_score;
  157. /** remap_slice: Bitmask of cache lines that need remapping */
  158. u8 remap_slice;
  159. /** handles_vma: rbtree to look up our context specific obj/vma for
  160. * the user handle. (user handles are per fd, but the binding is
  161. * per vm, which may be one per context or shared with the global GTT)
  162. */
  163. struct radix_tree_root handles_vma;
  164. /** handles_list: reverse list of all the rbtree entries in use for
  165. * this context, which allows us to free all the allocations on
  166. * context close.
  167. */
  168. struct list_head handles_list;
  169. };
  170. static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
  171. {
  172. return test_bit(CONTEXT_CLOSED, &ctx->flags);
  173. }
  174. static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
  175. {
  176. GEM_BUG_ON(i915_gem_context_is_closed(ctx));
  177. __set_bit(CONTEXT_CLOSED, &ctx->flags);
  178. }
  179. static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
  180. {
  181. return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  182. }
  183. static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
  184. {
  185. __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  186. }
  187. static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
  188. {
  189. __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  190. }
  191. static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
  192. {
  193. return test_bit(CONTEXT_BANNABLE, &ctx->flags);
  194. }
  195. static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
  196. {
  197. __set_bit(CONTEXT_BANNABLE, &ctx->flags);
  198. }
  199. static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
  200. {
  201. __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
  202. }
  203. static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
  204. {
  205. return test_bit(CONTEXT_BANNED, &ctx->flags);
  206. }
  207. static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
  208. {
  209. __set_bit(CONTEXT_BANNED, &ctx->flags);
  210. }
  211. static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
  212. {
  213. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  214. }
  215. static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
  216. {
  217. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  218. }
  219. static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
  220. {
  221. return c->user_handle == DEFAULT_CONTEXT_HANDLE;
  222. }
  223. static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
  224. {
  225. return !ctx->file_priv;
  226. }
  227. /* i915_gem_context.c */
  228. int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
  229. void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
  230. void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
  231. int i915_gem_context_open(struct drm_i915_private *i915,
  232. struct drm_file *file);
  233. void i915_gem_context_close(struct drm_file *file);
  234. int i915_switch_context(struct drm_i915_gem_request *req);
  235. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
  236. void i915_gem_context_release(struct kref *ctx_ref);
  237. struct i915_gem_context *
  238. i915_gem_context_create_gvt(struct drm_device *dev);
  239. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  240. struct drm_file *file);
  241. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  242. struct drm_file *file);
  243. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  244. struct drm_file *file_priv);
  245. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  246. struct drm_file *file_priv);
  247. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
  248. struct drm_file *file);
  249. static inline struct i915_gem_context *
  250. i915_gem_context_get(struct i915_gem_context *ctx)
  251. {
  252. kref_get(&ctx->ref);
  253. return ctx;
  254. }
  255. static inline void i915_gem_context_put(struct i915_gem_context *ctx)
  256. {
  257. kref_put(&ctx->ref, i915_gem_context_release);
  258. }
  259. #endif /* !__I915_GEM_CONTEXT_H__ */