i915_gem_context.c 23 KB

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