i915_gem_context.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright © 2011-2012 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. * Authors:
  24. * Ben Widawsky <ben@bwidawsk.net>
  25. *
  26. */
  27. /*
  28. * This file implements HW context support. On gen5+ a HW context consists of an
  29. * opaque GPU object which is referenced at times of context saves and restores.
  30. * With RC6 enabled, the context is also referenced as the GPU enters and exists
  31. * from RC6 (GPU has it's own internal power context, except on gen5). Though
  32. * something like a context does exist for the media ring, the code only
  33. * supports contexts for the render ring.
  34. *
  35. * In software, there is a distinction between contexts created by the user,
  36. * and the default HW context. The default HW context is used by GPU clients
  37. * that do not request setup of their own hardware context. The default
  38. * context's state is never restored to help prevent programming errors. This
  39. * would happen if a client ran and piggy-backed off another clients GPU state.
  40. * The default context only exists to give the GPU some offset to load as the
  41. * current to invoke a save of the context we actually care about. In fact, the
  42. * code could likely be constructed, albeit in a more complicated fashion, to
  43. * never use the default context, though that limits the driver's ability to
  44. * swap out, and/or destroy other contexts.
  45. *
  46. * All other contexts are created as a request by the GPU client. These contexts
  47. * store GPU state, and thus allow GPU clients to not re-emit state (and
  48. * potentially query certain state) at any time. The kernel driver makes
  49. * certain that the appropriate commands are inserted.
  50. *
  51. * The context life cycle is semi-complicated in that context BOs may live
  52. * longer than the context itself because of the way the hardware, and object
  53. * tracking works. Below is a very crude representation of the state machine
  54. * describing the context life.
  55. * refcount pincount active
  56. * S0: initial state 0 0 0
  57. * S1: context created 1 0 0
  58. * S2: context is currently running 2 1 X
  59. * S3: GPU referenced, but not current 2 0 1
  60. * S4: context is current, but destroyed 1 1 0
  61. * S5: like S3, but destroyed 1 0 1
  62. *
  63. * The most common (but not all) transitions:
  64. * S0->S1: client creates a context
  65. * S1->S2: client submits execbuf with context
  66. * S2->S3: other clients submits execbuf with context
  67. * S3->S1: context object was retired
  68. * S3->S2: clients submits another execbuf
  69. * S2->S4: context destroy called with current context
  70. * S3->S5->S0: destroy path
  71. * S4->S5->S0: destroy path on current context
  72. *
  73. * There are two confusing terms used above:
  74. * The "current context" means the context which is currently running on the
  75. * GPU. The GPU has loaded its state already and has stored away the gtt
  76. * offset of the BO. The GPU is not actively referencing the data at this
  77. * offset, but it will on the next context switch. The only way to avoid this
  78. * is to do a GPU reset.
  79. *
  80. * An "active context' is one which was previously the "current context" and is
  81. * on the active list waiting for the next context switch to occur. Until this
  82. * happens, the object must remain at the same gtt offset. It is therefore
  83. * possible to destroy a context, but it is still active.
  84. *
  85. */
  86. #include <drm/drmP.h>
  87. #include <drm/i915_drm.h>
  88. #include "i915_drv.h"
  89. #include "i915_trace.h"
  90. /* This is a HW constraint. The value below is the largest known requirement
  91. * I've seen in a spec to date, and that was a workaround for a non-shipping
  92. * part. It should be safe to decrease this, but it's more future proof as is.
  93. */
  94. #define GEN6_CONTEXT_ALIGN (64<<10)
  95. #define GEN7_CONTEXT_ALIGN 4096
  96. static size_t get_context_alignment(struct drm_device *dev)
  97. {
  98. if (IS_GEN6(dev))
  99. return GEN6_CONTEXT_ALIGN;
  100. return GEN7_CONTEXT_ALIGN;
  101. }
  102. static int get_context_size(struct drm_device *dev)
  103. {
  104. struct drm_i915_private *dev_priv = dev->dev_private;
  105. int ret;
  106. u32 reg;
  107. switch (INTEL_INFO(dev)->gen) {
  108. case 6:
  109. reg = I915_READ(CXT_SIZE);
  110. ret = GEN6_CXT_TOTAL_SIZE(reg) * 64;
  111. break;
  112. case 7:
  113. reg = I915_READ(GEN7_CXT_SIZE);
  114. if (IS_HASWELL(dev))
  115. ret = HSW_CXT_TOTAL_SIZE;
  116. else
  117. ret = GEN7_CXT_TOTAL_SIZE(reg) * 64;
  118. break;
  119. case 8:
  120. ret = GEN8_CXT_TOTAL_SIZE;
  121. break;
  122. default:
  123. BUG();
  124. }
  125. return ret;
  126. }
  127. void i915_gem_context_free(struct kref *ctx_ref)
  128. {
  129. struct intel_context *ctx = container_of(ctx_ref,
  130. typeof(*ctx), ref);
  131. trace_i915_context_free(ctx);
  132. if (i915.enable_execlists)
  133. intel_lr_context_free(ctx);
  134. i915_ppgtt_put(ctx->ppgtt);
  135. if (ctx->legacy_hw_ctx.rcs_state)
  136. drm_gem_object_unreference(&ctx->legacy_hw_ctx.rcs_state->base);
  137. list_del(&ctx->link);
  138. kfree(ctx);
  139. }
  140. struct drm_i915_gem_object *
  141. i915_gem_alloc_context_obj(struct drm_device *dev, size_t size)
  142. {
  143. struct drm_i915_gem_object *obj;
  144. int ret;
  145. obj = i915_gem_alloc_object(dev, size);
  146. if (obj == NULL)
  147. return ERR_PTR(-ENOMEM);
  148. /*
  149. * Try to make the context utilize L3 as well as LLC.
  150. *
  151. * On VLV we don't have L3 controls in the PTEs so we
  152. * shouldn't touch the cache level, especially as that
  153. * would make the object snooped which might have a
  154. * negative performance impact.
  155. */
  156. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev)) {
  157. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  158. /* Failure shouldn't ever happen this early */
  159. if (WARN_ON(ret)) {
  160. drm_gem_object_unreference(&obj->base);
  161. return ERR_PTR(ret);
  162. }
  163. }
  164. return obj;
  165. }
  166. static struct intel_context *
  167. __create_hw_context(struct drm_device *dev,
  168. struct drm_i915_file_private *file_priv)
  169. {
  170. struct drm_i915_private *dev_priv = dev->dev_private;
  171. struct intel_context *ctx;
  172. int ret;
  173. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  174. if (ctx == NULL)
  175. return ERR_PTR(-ENOMEM);
  176. kref_init(&ctx->ref);
  177. list_add_tail(&ctx->link, &dev_priv->context_list);
  178. if (dev_priv->hw_context_size) {
  179. struct drm_i915_gem_object *obj =
  180. i915_gem_alloc_context_obj(dev, dev_priv->hw_context_size);
  181. if (IS_ERR(obj)) {
  182. ret = PTR_ERR(obj);
  183. goto err_out;
  184. }
  185. ctx->legacy_hw_ctx.rcs_state = obj;
  186. }
  187. /* Default context will never have a file_priv */
  188. if (file_priv != NULL) {
  189. ret = idr_alloc(&file_priv->context_idr, ctx,
  190. DEFAULT_CONTEXT_HANDLE, 0, GFP_KERNEL);
  191. if (ret < 0)
  192. goto err_out;
  193. } else
  194. ret = DEFAULT_CONTEXT_HANDLE;
  195. ctx->file_priv = file_priv;
  196. ctx->user_handle = ret;
  197. /* NB: Mark all slices as needing a remap so that when the context first
  198. * loads it will restore whatever remap state already exists. If there
  199. * is no remap info, it will be a NOP. */
  200. ctx->remap_slice = (1 << NUM_L3_SLICES(dev)) - 1;
  201. return ctx;
  202. err_out:
  203. i915_gem_context_unreference(ctx);
  204. return ERR_PTR(ret);
  205. }
  206. /**
  207. * The default context needs to exist per ring that uses contexts. It stores the
  208. * context state of the GPU for applications that don't utilize HW contexts, as
  209. * well as an idle case.
  210. */
  211. static struct intel_context *
  212. i915_gem_create_context(struct drm_device *dev,
  213. struct drm_i915_file_private *file_priv)
  214. {
  215. const bool is_global_default_ctx = file_priv == NULL;
  216. struct intel_context *ctx;
  217. int ret = 0;
  218. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  219. ctx = __create_hw_context(dev, file_priv);
  220. if (IS_ERR(ctx))
  221. return ctx;
  222. if (is_global_default_ctx && ctx->legacy_hw_ctx.rcs_state) {
  223. /* We may need to do things with the shrinker which
  224. * require us to immediately switch back to the default
  225. * context. This can cause a problem as pinning the
  226. * default context also requires GTT space which may not
  227. * be available. To avoid this we always pin the default
  228. * context.
  229. */
  230. ret = i915_gem_obj_ggtt_pin(ctx->legacy_hw_ctx.rcs_state,
  231. get_context_alignment(dev), 0);
  232. if (ret) {
  233. DRM_DEBUG_DRIVER("Couldn't pin %d\n", ret);
  234. goto err_destroy;
  235. }
  236. }
  237. if (USES_FULL_PPGTT(dev)) {
  238. struct i915_hw_ppgtt *ppgtt = i915_ppgtt_create(dev, file_priv);
  239. if (IS_ERR_OR_NULL(ppgtt)) {
  240. DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
  241. PTR_ERR(ppgtt));
  242. ret = PTR_ERR(ppgtt);
  243. goto err_unpin;
  244. }
  245. ctx->ppgtt = ppgtt;
  246. }
  247. trace_i915_context_create(ctx);
  248. return ctx;
  249. err_unpin:
  250. if (is_global_default_ctx && ctx->legacy_hw_ctx.rcs_state)
  251. i915_gem_object_ggtt_unpin(ctx->legacy_hw_ctx.rcs_state);
  252. err_destroy:
  253. i915_gem_context_unreference(ctx);
  254. return ERR_PTR(ret);
  255. }
  256. void i915_gem_context_reset(struct drm_device *dev)
  257. {
  258. struct drm_i915_private *dev_priv = dev->dev_private;
  259. int i;
  260. /* In execlists mode we will unreference the context when the execlist
  261. * queue is cleared and the requests destroyed.
  262. */
  263. if (i915.enable_execlists)
  264. return;
  265. for (i = 0; i < I915_NUM_RINGS; i++) {
  266. struct intel_engine_cs *ring = &dev_priv->ring[i];
  267. struct intel_context *lctx = ring->last_context;
  268. if (lctx) {
  269. if (lctx->legacy_hw_ctx.rcs_state && i == RCS)
  270. i915_gem_object_ggtt_unpin(lctx->legacy_hw_ctx.rcs_state);
  271. i915_gem_context_unreference(lctx);
  272. ring->last_context = NULL;
  273. }
  274. }
  275. }
  276. int i915_gem_context_init(struct drm_device *dev)
  277. {
  278. struct drm_i915_private *dev_priv = dev->dev_private;
  279. struct intel_context *ctx;
  280. int i;
  281. /* Init should only be called once per module load. Eventually the
  282. * restriction on the context_disabled check can be loosened. */
  283. if (WARN_ON(dev_priv->ring[RCS].default_context))
  284. return 0;
  285. if (i915.enable_execlists) {
  286. /* NB: intentionally left blank. We will allocate our own
  287. * backing objects as we need them, thank you very much */
  288. dev_priv->hw_context_size = 0;
  289. } else if (HAS_HW_CONTEXTS(dev)) {
  290. dev_priv->hw_context_size = round_up(get_context_size(dev), 4096);
  291. if (dev_priv->hw_context_size > (1<<20)) {
  292. DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n",
  293. dev_priv->hw_context_size);
  294. dev_priv->hw_context_size = 0;
  295. }
  296. }
  297. ctx = i915_gem_create_context(dev, NULL);
  298. if (IS_ERR(ctx)) {
  299. DRM_ERROR("Failed to create default global context (error %ld)\n",
  300. PTR_ERR(ctx));
  301. return PTR_ERR(ctx);
  302. }
  303. for (i = 0; i < I915_NUM_RINGS; i++) {
  304. struct intel_engine_cs *ring = &dev_priv->ring[i];
  305. /* NB: RCS will hold a ref for all rings */
  306. ring->default_context = ctx;
  307. }
  308. DRM_DEBUG_DRIVER("%s context support initialized\n",
  309. i915.enable_execlists ? "LR" :
  310. dev_priv->hw_context_size ? "HW" : "fake");
  311. return 0;
  312. }
  313. void i915_gem_context_fini(struct drm_device *dev)
  314. {
  315. struct drm_i915_private *dev_priv = dev->dev_private;
  316. struct intel_context *dctx = dev_priv->ring[RCS].default_context;
  317. int i;
  318. if (dctx->legacy_hw_ctx.rcs_state) {
  319. /* The only known way to stop the gpu from accessing the hw context is
  320. * to reset it. Do this as the very last operation to avoid confusing
  321. * other code, leading to spurious errors. */
  322. intel_gpu_reset(dev);
  323. /* When default context is created and switched to, base object refcount
  324. * will be 2 (+1 from object creation and +1 from do_switch()).
  325. * i915_gem_context_fini() will be called after gpu_idle() has switched
  326. * to default context. So we need to unreference the base object once
  327. * to offset the do_switch part, so that i915_gem_context_unreference()
  328. * can then free the base object correctly. */
  329. WARN_ON(!dev_priv->ring[RCS].last_context);
  330. if (dev_priv->ring[RCS].last_context == dctx) {
  331. /* Fake switch to NULL context */
  332. WARN_ON(dctx->legacy_hw_ctx.rcs_state->active);
  333. i915_gem_object_ggtt_unpin(dctx->legacy_hw_ctx.rcs_state);
  334. i915_gem_context_unreference(dctx);
  335. dev_priv->ring[RCS].last_context = NULL;
  336. }
  337. i915_gem_object_ggtt_unpin(dctx->legacy_hw_ctx.rcs_state);
  338. }
  339. for (i = 0; i < I915_NUM_RINGS; i++) {
  340. struct intel_engine_cs *ring = &dev_priv->ring[i];
  341. if (ring->last_context)
  342. i915_gem_context_unreference(ring->last_context);
  343. ring->default_context = NULL;
  344. ring->last_context = NULL;
  345. }
  346. i915_gem_context_unreference(dctx);
  347. }
  348. int i915_gem_context_enable(struct drm_i915_private *dev_priv)
  349. {
  350. struct intel_engine_cs *ring;
  351. int ret, i;
  352. BUG_ON(!dev_priv->ring[RCS].default_context);
  353. if (i915.enable_execlists)
  354. return 0;
  355. for_each_ring(ring, dev_priv, i) {
  356. ret = i915_switch_context(ring, ring->default_context);
  357. if (ret)
  358. return ret;
  359. }
  360. return 0;
  361. }
  362. static int context_idr_cleanup(int id, void *p, void *data)
  363. {
  364. struct intel_context *ctx = p;
  365. i915_gem_context_unreference(ctx);
  366. return 0;
  367. }
  368. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  369. {
  370. struct drm_i915_file_private *file_priv = file->driver_priv;
  371. struct intel_context *ctx;
  372. idr_init(&file_priv->context_idr);
  373. mutex_lock(&dev->struct_mutex);
  374. ctx = i915_gem_create_context(dev, file_priv);
  375. mutex_unlock(&dev->struct_mutex);
  376. if (IS_ERR(ctx)) {
  377. idr_destroy(&file_priv->context_idr);
  378. return PTR_ERR(ctx);
  379. }
  380. return 0;
  381. }
  382. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  383. {
  384. struct drm_i915_file_private *file_priv = file->driver_priv;
  385. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  386. idr_destroy(&file_priv->context_idr);
  387. }
  388. struct intel_context *
  389. i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
  390. {
  391. struct intel_context *ctx;
  392. ctx = (struct intel_context *)idr_find(&file_priv->context_idr, id);
  393. if (!ctx)
  394. return ERR_PTR(-ENOENT);
  395. return ctx;
  396. }
  397. static inline int
  398. mi_set_context(struct intel_engine_cs *ring,
  399. struct intel_context *new_context,
  400. u32 hw_flags)
  401. {
  402. u32 flags = hw_flags | MI_MM_SPACE_GTT;
  403. const int num_rings =
  404. /* Use an extended w/a on ivb+ if signalling from other rings */
  405. i915_semaphore_is_enabled(ring->dev) ?
  406. hweight32(INTEL_INFO(ring->dev)->ring_mask) - 1 :
  407. 0;
  408. int len, i, ret;
  409. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  410. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  411. * explicitly, so we rely on the value at ring init, stored in
  412. * itlb_before_ctx_switch.
  413. */
  414. if (IS_GEN6(ring->dev)) {
  415. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, 0);
  416. if (ret)
  417. return ret;
  418. }
  419. /* These flags are for resource streamer on HSW+ */
  420. if (!IS_HASWELL(ring->dev) && INTEL_INFO(ring->dev)->gen < 8)
  421. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  422. len = 4;
  423. if (INTEL_INFO(ring->dev)->gen >= 7)
  424. len += 2 + (num_rings ? 4*num_rings + 2 : 0);
  425. ret = intel_ring_begin(ring, len);
  426. if (ret)
  427. return ret;
  428. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  429. if (INTEL_INFO(ring->dev)->gen >= 7) {
  430. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  431. if (num_rings) {
  432. struct intel_engine_cs *signaller;
  433. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_rings));
  434. for_each_ring(signaller, to_i915(ring->dev), i) {
  435. if (signaller == ring)
  436. continue;
  437. intel_ring_emit(ring, RING_PSMI_CTL(signaller->mmio_base));
  438. intel_ring_emit(ring, _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  439. }
  440. }
  441. }
  442. intel_ring_emit(ring, MI_NOOP);
  443. intel_ring_emit(ring, MI_SET_CONTEXT);
  444. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(new_context->legacy_hw_ctx.rcs_state) |
  445. flags);
  446. /*
  447. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  448. * WaMiSetContext_Hang:snb,ivb,vlv
  449. */
  450. intel_ring_emit(ring, MI_NOOP);
  451. if (INTEL_INFO(ring->dev)->gen >= 7) {
  452. if (num_rings) {
  453. struct intel_engine_cs *signaller;
  454. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_rings));
  455. for_each_ring(signaller, to_i915(ring->dev), i) {
  456. if (signaller == ring)
  457. continue;
  458. intel_ring_emit(ring, RING_PSMI_CTL(signaller->mmio_base));
  459. intel_ring_emit(ring, _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  460. }
  461. }
  462. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  463. }
  464. intel_ring_advance(ring);
  465. return ret;
  466. }
  467. static int do_switch(struct intel_engine_cs *ring,
  468. struct intel_context *to)
  469. {
  470. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  471. struct intel_context *from = ring->last_context;
  472. u32 hw_flags = 0;
  473. bool uninitialized = false;
  474. struct i915_vma *vma;
  475. int ret, i;
  476. if (from != NULL && ring == &dev_priv->ring[RCS]) {
  477. BUG_ON(from->legacy_hw_ctx.rcs_state == NULL);
  478. BUG_ON(!i915_gem_obj_is_pinned(from->legacy_hw_ctx.rcs_state));
  479. }
  480. if (from == to && !to->remap_slice)
  481. return 0;
  482. /* Trying to pin first makes error handling easier. */
  483. if (ring == &dev_priv->ring[RCS]) {
  484. ret = i915_gem_obj_ggtt_pin(to->legacy_hw_ctx.rcs_state,
  485. get_context_alignment(ring->dev), 0);
  486. if (ret)
  487. return ret;
  488. }
  489. /*
  490. * Pin can switch back to the default context if we end up calling into
  491. * evict_everything - as a last ditch gtt defrag effort that also
  492. * switches to the default context. Hence we need to reload from here.
  493. */
  494. from = ring->last_context;
  495. if (to->ppgtt) {
  496. trace_switch_mm(ring, to);
  497. ret = to->ppgtt->switch_mm(to->ppgtt, ring);
  498. if (ret)
  499. goto unpin_out;
  500. }
  501. if (ring != &dev_priv->ring[RCS]) {
  502. if (from)
  503. i915_gem_context_unreference(from);
  504. goto done;
  505. }
  506. /*
  507. * Clear this page out of any CPU caches for coherent swap-in/out. Note
  508. * that thanks to write = false in this call and us not setting any gpu
  509. * write domains when putting a context object onto the active list
  510. * (when switching away from it), this won't block.
  511. *
  512. * XXX: We need a real interface to do this instead of trickery.
  513. */
  514. ret = i915_gem_object_set_to_gtt_domain(to->legacy_hw_ctx.rcs_state, false);
  515. if (ret)
  516. goto unpin_out;
  517. vma = i915_gem_obj_to_ggtt(to->legacy_hw_ctx.rcs_state);
  518. if (!(vma->bound & GLOBAL_BIND))
  519. vma->bind_vma(vma, to->legacy_hw_ctx.rcs_state->cache_level,
  520. GLOBAL_BIND);
  521. if (!to->legacy_hw_ctx.initialized || i915_gem_context_is_default(to))
  522. hw_flags |= MI_RESTORE_INHIBIT;
  523. ret = mi_set_context(ring, to, hw_flags);
  524. if (ret)
  525. goto unpin_out;
  526. for (i = 0; i < MAX_L3_SLICES; i++) {
  527. if (!(to->remap_slice & (1<<i)))
  528. continue;
  529. ret = i915_gem_l3_remap(ring, i);
  530. /* If it failed, try again next round */
  531. if (ret)
  532. DRM_DEBUG_DRIVER("L3 remapping failed\n");
  533. else
  534. to->remap_slice &= ~(1<<i);
  535. }
  536. /* The backing object for the context is done after switching to the
  537. * *next* context. Therefore we cannot retire the previous context until
  538. * the next context has already started running. In fact, the below code
  539. * is a bit suboptimal because the retiring can occur simply after the
  540. * MI_SET_CONTEXT instead of when the next seqno has completed.
  541. */
  542. if (from != NULL) {
  543. from->legacy_hw_ctx.rcs_state->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
  544. i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->legacy_hw_ctx.rcs_state), ring);
  545. /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the
  546. * whole damn pipeline, we don't need to explicitly mark the
  547. * object dirty. The only exception is that the context must be
  548. * correct in case the object gets swapped out. Ideally we'd be
  549. * able to defer doing this until we know the object would be
  550. * swapped, but there is no way to do that yet.
  551. */
  552. from->legacy_hw_ctx.rcs_state->dirty = 1;
  553. BUG_ON(from->legacy_hw_ctx.rcs_state->ring != ring);
  554. /* obj is kept alive until the next request by its active ref */
  555. i915_gem_object_ggtt_unpin(from->legacy_hw_ctx.rcs_state);
  556. i915_gem_context_unreference(from);
  557. }
  558. uninitialized = !to->legacy_hw_ctx.initialized && from == NULL;
  559. to->legacy_hw_ctx.initialized = true;
  560. done:
  561. i915_gem_context_reference(to);
  562. ring->last_context = to;
  563. if (uninitialized) {
  564. if (ring->init_context) {
  565. ret = ring->init_context(ring, to);
  566. if (ret)
  567. DRM_ERROR("ring init context: %d\n", ret);
  568. }
  569. ret = i915_gem_render_state_init(ring);
  570. if (ret)
  571. DRM_ERROR("init render state: %d\n", ret);
  572. }
  573. return 0;
  574. unpin_out:
  575. if (ring->id == RCS)
  576. i915_gem_object_ggtt_unpin(to->legacy_hw_ctx.rcs_state);
  577. return ret;
  578. }
  579. /**
  580. * i915_switch_context() - perform a GPU context switch.
  581. * @ring: ring for which we'll execute the context switch
  582. * @to: the context to switch to
  583. *
  584. * The context life cycle is simple. The context refcount is incremented and
  585. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  586. * it will have a refcount > 1. This allows us to destroy the context abstract
  587. * object while letting the normal object tracking destroy the backing BO.
  588. *
  589. * This function should not be used in execlists mode. Instead the context is
  590. * switched by writing to the ELSP and requests keep a reference to their
  591. * context.
  592. */
  593. int i915_switch_context(struct intel_engine_cs *ring,
  594. struct intel_context *to)
  595. {
  596. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  597. WARN_ON(i915.enable_execlists);
  598. WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  599. if (to->legacy_hw_ctx.rcs_state == NULL) { /* We have the fake context */
  600. if (to != ring->last_context) {
  601. i915_gem_context_reference(to);
  602. if (ring->last_context)
  603. i915_gem_context_unreference(ring->last_context);
  604. ring->last_context = to;
  605. }
  606. return 0;
  607. }
  608. return do_switch(ring, to);
  609. }
  610. static bool contexts_enabled(struct drm_device *dev)
  611. {
  612. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  613. }
  614. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  615. struct drm_file *file)
  616. {
  617. struct drm_i915_gem_context_create *args = data;
  618. struct drm_i915_file_private *file_priv = file->driver_priv;
  619. struct intel_context *ctx;
  620. int ret;
  621. if (!contexts_enabled(dev))
  622. return -ENODEV;
  623. ret = i915_mutex_lock_interruptible(dev);
  624. if (ret)
  625. return ret;
  626. ctx = i915_gem_create_context(dev, file_priv);
  627. mutex_unlock(&dev->struct_mutex);
  628. if (IS_ERR(ctx))
  629. return PTR_ERR(ctx);
  630. args->ctx_id = ctx->user_handle;
  631. DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id);
  632. return 0;
  633. }
  634. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  635. struct drm_file *file)
  636. {
  637. struct drm_i915_gem_context_destroy *args = data;
  638. struct drm_i915_file_private *file_priv = file->driver_priv;
  639. struct intel_context *ctx;
  640. int ret;
  641. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  642. return -ENOENT;
  643. ret = i915_mutex_lock_interruptible(dev);
  644. if (ret)
  645. return ret;
  646. ctx = i915_gem_context_get(file_priv, args->ctx_id);
  647. if (IS_ERR(ctx)) {
  648. mutex_unlock(&dev->struct_mutex);
  649. return PTR_ERR(ctx);
  650. }
  651. idr_remove(&ctx->file_priv->context_idr, ctx->user_handle);
  652. i915_gem_context_unreference(ctx);
  653. mutex_unlock(&dev->struct_mutex);
  654. DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id);
  655. return 0;
  656. }