i915_gem_context.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. struct llist_node free_link;
  78. /**
  79. * @ref: reference count
  80. *
  81. * A reference to a context is held by both the client who created it
  82. * and on each request submitted to the hardware using the request
  83. * (to ensure the hardware has access to the state until it has
  84. * finished all pending writes). See i915_gem_context_get() and
  85. * i915_gem_context_put() for access.
  86. */
  87. struct kref ref;
  88. /**
  89. * @rcu: rcu_head for deferred freeing.
  90. */
  91. struct rcu_head rcu;
  92. /**
  93. * @flags: small set of booleans
  94. */
  95. unsigned long flags;
  96. #define CONTEXT_NO_ZEROMAP BIT(0)
  97. #define CONTEXT_NO_ERROR_CAPTURE 1
  98. #define CONTEXT_CLOSED 2
  99. #define CONTEXT_BANNABLE 3
  100. #define CONTEXT_BANNED 4
  101. #define CONTEXT_FORCE_SINGLE_SUBMISSION 5
  102. /**
  103. * @hw_id: - unique identifier for the context
  104. *
  105. * The hardware needs to uniquely identify the context for a few
  106. * functions like fault reporting, PASID, scheduling. The
  107. * &drm_i915_private.context_hw_ida is used to assign a unqiue
  108. * id for the lifetime of the context.
  109. */
  110. unsigned int hw_id;
  111. /**
  112. * @user_handle: userspace identifier
  113. *
  114. * A unique per-file identifier is generated from
  115. * &drm_i915_file_private.contexts.
  116. */
  117. u32 user_handle;
  118. /**
  119. * @priority: execution and service priority
  120. *
  121. * All clients are equal, but some are more equal than others!
  122. *
  123. * Requests from a context with a greater (more positive) value of
  124. * @priority will be executed before those with a lower @priority
  125. * value, forming a simple QoS.
  126. *
  127. * The &drm_i915_private.kernel_context is assigned the lowest priority.
  128. */
  129. int priority;
  130. /** ggtt_offset_bias: placement restriction for context objects */
  131. u32 ggtt_offset_bias;
  132. struct i915_gem_context_vma_lut {
  133. /** ht_size: last request size to allocate the hashtable for. */
  134. unsigned int ht_size;
  135. #define I915_CTX_RESIZE_IN_PROGRESS BIT(0)
  136. /** ht_bits: real log2(size) of hashtable. */
  137. unsigned int ht_bits;
  138. /** ht_count: current number of entries inside the hashtable */
  139. unsigned int ht_count;
  140. /** ht: the array of buckets comprising the simple hashtable */
  141. struct hlist_head *ht;
  142. /**
  143. * resize: After an execbuf completes, we check the load factor
  144. * of the hashtable. If the hashtable is too full, or too empty,
  145. * we schedule a task to resize the hashtable. During the
  146. * resize, the entries are moved between different buckets and
  147. * so we cannot simultaneously read the hashtable as it is
  148. * being resized (unlike rhashtable). Therefore we treat the
  149. * active work as a strong barrier, pausing a subsequent
  150. * execbuf to wait for the resize worker to complete, if
  151. * required.
  152. */
  153. struct work_struct resize;
  154. } vma_lut;
  155. /** engine: per-engine logical HW state */
  156. struct intel_context {
  157. struct i915_vma *state;
  158. struct intel_ring *ring;
  159. u32 *lrc_reg_state;
  160. u64 lrc_desc;
  161. int pin_count;
  162. bool initialised;
  163. } engine[I915_NUM_ENGINES];
  164. /** ring_size: size for allocating the per-engine ring buffer */
  165. u32 ring_size;
  166. /** desc_template: invariant fields for the HW context descriptor */
  167. u32 desc_template;
  168. /** guilty_count: How many times this context has caused a GPU hang. */
  169. atomic_t guilty_count;
  170. /**
  171. * @active_count: How many times this context was active during a GPU
  172. * hang, but did not cause it.
  173. */
  174. atomic_t active_count;
  175. #define CONTEXT_SCORE_GUILTY 10
  176. #define CONTEXT_SCORE_BAN_THRESHOLD 40
  177. /** ban_score: Accumulated score of all hangs caused by this context. */
  178. atomic_t ban_score;
  179. /** remap_slice: Bitmask of cache lines that need remapping */
  180. u8 remap_slice;
  181. };
  182. static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
  183. {
  184. return test_bit(CONTEXT_CLOSED, &ctx->flags);
  185. }
  186. static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
  187. {
  188. GEM_BUG_ON(i915_gem_context_is_closed(ctx));
  189. __set_bit(CONTEXT_CLOSED, &ctx->flags);
  190. }
  191. static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
  192. {
  193. return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  194. }
  195. static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
  196. {
  197. __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  198. }
  199. static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
  200. {
  201. __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
  202. }
  203. static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
  204. {
  205. return test_bit(CONTEXT_BANNABLE, &ctx->flags);
  206. }
  207. static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
  208. {
  209. __set_bit(CONTEXT_BANNABLE, &ctx->flags);
  210. }
  211. static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
  212. {
  213. __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
  214. }
  215. static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
  216. {
  217. return test_bit(CONTEXT_BANNED, &ctx->flags);
  218. }
  219. static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
  220. {
  221. __set_bit(CONTEXT_BANNED, &ctx->flags);
  222. }
  223. static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
  224. {
  225. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  226. }
  227. static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
  228. {
  229. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  230. }
  231. static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
  232. {
  233. return c->user_handle == DEFAULT_CONTEXT_HANDLE;
  234. }
  235. static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
  236. {
  237. return !ctx->file_priv;
  238. }
  239. /* i915_gem_context.c */
  240. int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
  241. void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
  242. void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
  243. int i915_gem_context_open(struct drm_i915_private *i915,
  244. struct drm_file *file);
  245. void i915_gem_context_close(struct drm_file *file);
  246. int i915_switch_context(struct drm_i915_gem_request *req);
  247. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
  248. void i915_gem_context_release(struct kref *ctx_ref);
  249. struct i915_gem_context *
  250. i915_gem_context_create_gvt(struct drm_device *dev);
  251. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  252. struct drm_file *file);
  253. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  254. struct drm_file *file);
  255. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  256. struct drm_file *file_priv);
  257. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  258. struct drm_file *file_priv);
  259. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
  260. struct drm_file *file);
  261. static inline struct i915_gem_context *
  262. i915_gem_context_get(struct i915_gem_context *ctx)
  263. {
  264. kref_get(&ctx->ref);
  265. return ctx;
  266. }
  267. static inline void i915_gem_context_put(struct i915_gem_context *ctx)
  268. {
  269. kref_put(&ctx->ref, i915_gem_context_release);
  270. }
  271. #endif /* !__I915_GEM_CONTEXT_H__ */