i915_gem_context.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "i915_scheduler.h"
  31. struct pid;
  32. struct drm_device;
  33. struct drm_file;
  34. struct drm_i915_private;
  35. struct drm_i915_file_private;
  36. struct i915_hw_ppgtt;
  37. struct i915_request;
  38. struct i915_vma;
  39. struct intel_ring;
  40. #define DEFAULT_CONTEXT_HANDLE 0
  41. struct intel_context;
  42. struct intel_context_ops {
  43. void (*unpin)(struct intel_context *ce);
  44. void (*destroy)(struct intel_context *ce);
  45. };
  46. /**
  47. * struct i915_gem_context - client state
  48. *
  49. * The struct i915_gem_context represents the combined view of the driver and
  50. * logical hardware state for a particular client.
  51. */
  52. struct i915_gem_context {
  53. /** i915: i915 device backpointer */
  54. struct drm_i915_private *i915;
  55. /** file_priv: owning file descriptor */
  56. struct drm_i915_file_private *file_priv;
  57. /**
  58. * @ppgtt: unique address space (GTT)
  59. *
  60. * In full-ppgtt mode, each context has its own address space ensuring
  61. * complete seperation of one client from all others.
  62. *
  63. * In other modes, this is a NULL pointer with the expectation that
  64. * the caller uses the shared global GTT.
  65. */
  66. struct i915_hw_ppgtt *ppgtt;
  67. /**
  68. * @pid: process id of creator
  69. *
  70. * Note that who created the context may not be the principle user,
  71. * as the context may be shared across a local socket. However,
  72. * that should only affect the default context, all contexts created
  73. * explicitly by the client are expected to be isolated.
  74. */
  75. struct pid *pid;
  76. /**
  77. * @name: arbitrary name
  78. *
  79. * A name is constructed for the context from the creator's process
  80. * name, pid and user handle in order to uniquely identify the
  81. * context in messages.
  82. */
  83. const char *name;
  84. /** link: place with &drm_i915_private.context_list */
  85. struct list_head link;
  86. struct llist_node free_link;
  87. /**
  88. * @ref: reference count
  89. *
  90. * A reference to a context is held by both the client who created it
  91. * and on each request submitted to the hardware using the request
  92. * (to ensure the hardware has access to the state until it has
  93. * finished all pending writes). See i915_gem_context_get() and
  94. * i915_gem_context_put() for access.
  95. */
  96. struct kref ref;
  97. /**
  98. * @rcu: rcu_head for deferred freeing.
  99. */
  100. struct rcu_head rcu;
  101. /**
  102. * @user_flags: small set of booleans controlled by the user
  103. */
  104. unsigned long user_flags;
  105. #define UCONTEXT_NO_ZEROMAP 0
  106. #define UCONTEXT_NO_ERROR_CAPTURE 1
  107. #define UCONTEXT_BANNABLE 2
  108. /**
  109. * @flags: small set of booleans
  110. */
  111. unsigned long flags;
  112. #define CONTEXT_BANNED 0
  113. #define CONTEXT_CLOSED 1
  114. #define CONTEXT_FORCE_SINGLE_SUBMISSION 2
  115. /**
  116. * @hw_id: - unique identifier for the context
  117. *
  118. * The hardware needs to uniquely identify the context for a few
  119. * functions like fault reporting, PASID, scheduling. The
  120. * &drm_i915_private.context_hw_ida is used to assign a unqiue
  121. * id for the lifetime of the context.
  122. *
  123. * @hw_id_pin_count: - number of times this context had been pinned
  124. * for use (should be, at most, once per engine).
  125. *
  126. * @hw_id_link: - all contexts with an assigned id are tracked
  127. * for possible repossession.
  128. */
  129. unsigned int hw_id;
  130. atomic_t hw_id_pin_count;
  131. struct list_head hw_id_link;
  132. /**
  133. * @user_handle: userspace identifier
  134. *
  135. * A unique per-file identifier is generated from
  136. * &drm_i915_file_private.contexts.
  137. */
  138. u32 user_handle;
  139. struct i915_sched_attr sched;
  140. /** engine: per-engine logical HW state */
  141. struct intel_context {
  142. struct i915_gem_context *gem_context;
  143. struct i915_vma *state;
  144. struct intel_ring *ring;
  145. u32 *lrc_reg_state;
  146. u64 lrc_desc;
  147. int pin_count;
  148. const struct intel_context_ops *ops;
  149. } __engine[I915_NUM_ENGINES];
  150. /** ring_size: size for allocating the per-engine ring buffer */
  151. u32 ring_size;
  152. /** desc_template: invariant fields for the HW context descriptor */
  153. u32 desc_template;
  154. /** guilty_count: How many times this context has caused a GPU hang. */
  155. atomic_t guilty_count;
  156. /**
  157. * @active_count: How many times this context was active during a GPU
  158. * hang, but did not cause it.
  159. */
  160. atomic_t active_count;
  161. #define CONTEXT_SCORE_GUILTY 10
  162. #define CONTEXT_SCORE_BAN_THRESHOLD 40
  163. /** ban_score: Accumulated score of all hangs caused by this context. */
  164. atomic_t ban_score;
  165. /** remap_slice: Bitmask of cache lines that need remapping */
  166. u8 remap_slice;
  167. /** handles_vma: rbtree to look up our context specific obj/vma for
  168. * the user handle. (user handles are per fd, but the binding is
  169. * per vm, which may be one per context or shared with the global GTT)
  170. */
  171. struct radix_tree_root handles_vma;
  172. /** handles_list: reverse list of all the rbtree entries in use for
  173. * this context, which allows us to free all the allocations on
  174. * context close.
  175. */
  176. struct list_head handles_list;
  177. };
  178. static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
  179. {
  180. return test_bit(CONTEXT_CLOSED, &ctx->flags);
  181. }
  182. static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
  183. {
  184. GEM_BUG_ON(i915_gem_context_is_closed(ctx));
  185. set_bit(CONTEXT_CLOSED, &ctx->flags);
  186. }
  187. static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
  188. {
  189. return test_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
  190. }
  191. static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
  192. {
  193. set_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
  194. }
  195. static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
  196. {
  197. clear_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
  198. }
  199. static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
  200. {
  201. return test_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
  202. }
  203. static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
  204. {
  205. set_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
  206. }
  207. static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
  208. {
  209. clear_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
  210. }
  211. static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
  212. {
  213. return test_bit(CONTEXT_BANNED, &ctx->flags);
  214. }
  215. static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
  216. {
  217. set_bit(CONTEXT_BANNED, &ctx->flags);
  218. }
  219. static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
  220. {
  221. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  222. }
  223. static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
  224. {
  225. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
  226. }
  227. int __i915_gem_context_pin_hw_id(struct i915_gem_context *ctx);
  228. static inline int i915_gem_context_pin_hw_id(struct i915_gem_context *ctx)
  229. {
  230. if (atomic_inc_not_zero(&ctx->hw_id_pin_count))
  231. return 0;
  232. return __i915_gem_context_pin_hw_id(ctx);
  233. }
  234. static inline void i915_gem_context_unpin_hw_id(struct i915_gem_context *ctx)
  235. {
  236. GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count) == 0u);
  237. atomic_dec(&ctx->hw_id_pin_count);
  238. }
  239. static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
  240. {
  241. return c->user_handle == DEFAULT_CONTEXT_HANDLE;
  242. }
  243. static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
  244. {
  245. return !ctx->file_priv;
  246. }
  247. static inline struct intel_context *
  248. to_intel_context(struct i915_gem_context *ctx,
  249. const struct intel_engine_cs *engine)
  250. {
  251. return &ctx->__engine[engine->id];
  252. }
  253. static inline struct intel_context *
  254. intel_context_pin(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
  255. {
  256. return engine->context_pin(engine, ctx);
  257. }
  258. static inline void __intel_context_pin(struct intel_context *ce)
  259. {
  260. GEM_BUG_ON(!ce->pin_count);
  261. ce->pin_count++;
  262. }
  263. static inline void intel_context_unpin(struct intel_context *ce)
  264. {
  265. GEM_BUG_ON(!ce->pin_count);
  266. if (--ce->pin_count)
  267. return;
  268. GEM_BUG_ON(!ce->ops);
  269. ce->ops->unpin(ce);
  270. }
  271. /* i915_gem_context.c */
  272. int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
  273. void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
  274. void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
  275. int i915_gem_context_open(struct drm_i915_private *i915,
  276. struct drm_file *file);
  277. void i915_gem_context_close(struct drm_file *file);
  278. int i915_switch_context(struct i915_request *rq);
  279. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
  280. void i915_gem_context_release(struct kref *ctx_ref);
  281. struct i915_gem_context *
  282. i915_gem_context_create_gvt(struct drm_device *dev);
  283. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  284. struct drm_file *file);
  285. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  286. struct drm_file *file);
  287. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  288. struct drm_file *file_priv);
  289. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  290. struct drm_file *file_priv);
  291. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
  292. struct drm_file *file);
  293. struct i915_gem_context *
  294. i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio);
  295. static inline struct i915_gem_context *
  296. i915_gem_context_get(struct i915_gem_context *ctx)
  297. {
  298. kref_get(&ctx->ref);
  299. return ctx;
  300. }
  301. static inline void i915_gem_context_put(struct i915_gem_context *ctx)
  302. {
  303. kref_put(&ctx->ref, i915_gem_context_release);
  304. }
  305. #endif /* !__I915_GEM_CONTEXT_H__ */