i915_gem_context.h 8.4 KB

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