i915_gem_context.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. int ret;
  401. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  402. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  403. * explicitly, so we rely on the value at ring init, stored in
  404. * itlb_before_ctx_switch.
  405. */
  406. if (IS_GEN6(ring->dev)) {
  407. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, 0);
  408. if (ret)
  409. return ret;
  410. }
  411. /* These flags are for resource streamer on HSW+ */
  412. if (!IS_HASWELL(ring->dev) && INTEL_INFO(ring->dev)->gen < 8)
  413. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  414. ret = intel_ring_begin(ring, 6);
  415. if (ret)
  416. return ret;
  417. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  418. if (INTEL_INFO(ring->dev)->gen >= 7)
  419. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  420. else
  421. intel_ring_emit(ring, MI_NOOP);
  422. intel_ring_emit(ring, MI_NOOP);
  423. intel_ring_emit(ring, MI_SET_CONTEXT);
  424. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(new_context->legacy_hw_ctx.rcs_state) |
  425. flags);
  426. /*
  427. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  428. * WaMiSetContext_Hang:snb,ivb,vlv
  429. */
  430. intel_ring_emit(ring, MI_NOOP);
  431. if (INTEL_INFO(ring->dev)->gen >= 7)
  432. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  433. else
  434. intel_ring_emit(ring, MI_NOOP);
  435. intel_ring_advance(ring);
  436. return ret;
  437. }
  438. static int do_switch(struct intel_engine_cs *ring,
  439. struct intel_context *to)
  440. {
  441. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  442. struct intel_context *from = ring->last_context;
  443. u32 hw_flags = 0;
  444. bool uninitialized = false;
  445. int ret, i;
  446. if (from != NULL && ring == &dev_priv->ring[RCS]) {
  447. BUG_ON(from->legacy_hw_ctx.rcs_state == NULL);
  448. BUG_ON(!i915_gem_obj_is_pinned(from->legacy_hw_ctx.rcs_state));
  449. }
  450. if (from == to && !to->remap_slice)
  451. return 0;
  452. /* Trying to pin first makes error handling easier. */
  453. if (ring == &dev_priv->ring[RCS]) {
  454. ret = i915_gem_obj_ggtt_pin(to->legacy_hw_ctx.rcs_state,
  455. get_context_alignment(ring->dev), 0);
  456. if (ret)
  457. return ret;
  458. }
  459. /*
  460. * Pin can switch back to the default context if we end up calling into
  461. * evict_everything - as a last ditch gtt defrag effort that also
  462. * switches to the default context. Hence we need to reload from here.
  463. */
  464. from = ring->last_context;
  465. if (to->ppgtt) {
  466. ret = to->ppgtt->switch_mm(to->ppgtt, ring);
  467. if (ret)
  468. goto unpin_out;
  469. }
  470. if (ring != &dev_priv->ring[RCS]) {
  471. if (from)
  472. i915_gem_context_unreference(from);
  473. goto done;
  474. }
  475. /*
  476. * Clear this page out of any CPU caches for coherent swap-in/out. Note
  477. * that thanks to write = false in this call and us not setting any gpu
  478. * write domains when putting a context object onto the active list
  479. * (when switching away from it), this won't block.
  480. *
  481. * XXX: We need a real interface to do this instead of trickery.
  482. */
  483. ret = i915_gem_object_set_to_gtt_domain(to->legacy_hw_ctx.rcs_state, false);
  484. if (ret)
  485. goto unpin_out;
  486. if (!to->legacy_hw_ctx.rcs_state->has_global_gtt_mapping) {
  487. struct i915_vma *vma = i915_gem_obj_to_vma(to->legacy_hw_ctx.rcs_state,
  488. &dev_priv->gtt.base);
  489. vma->bind_vma(vma, to->legacy_hw_ctx.rcs_state->cache_level, GLOBAL_BIND);
  490. }
  491. if (!to->legacy_hw_ctx.initialized || i915_gem_context_is_default(to))
  492. hw_flags |= MI_RESTORE_INHIBIT;
  493. ret = mi_set_context(ring, to, hw_flags);
  494. if (ret)
  495. goto unpin_out;
  496. for (i = 0; i < MAX_L3_SLICES; i++) {
  497. if (!(to->remap_slice & (1<<i)))
  498. continue;
  499. ret = i915_gem_l3_remap(ring, i);
  500. /* If it failed, try again next round */
  501. if (ret)
  502. DRM_DEBUG_DRIVER("L3 remapping failed\n");
  503. else
  504. to->remap_slice &= ~(1<<i);
  505. }
  506. /* The backing object for the context is done after switching to the
  507. * *next* context. Therefore we cannot retire the previous context until
  508. * the next context has already started running. In fact, the below code
  509. * is a bit suboptimal because the retiring can occur simply after the
  510. * MI_SET_CONTEXT instead of when the next seqno has completed.
  511. */
  512. if (from != NULL) {
  513. from->legacy_hw_ctx.rcs_state->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
  514. i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->legacy_hw_ctx.rcs_state), ring);
  515. /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the
  516. * whole damn pipeline, we don't need to explicitly mark the
  517. * object dirty. The only exception is that the context must be
  518. * correct in case the object gets swapped out. Ideally we'd be
  519. * able to defer doing this until we know the object would be
  520. * swapped, but there is no way to do that yet.
  521. */
  522. from->legacy_hw_ctx.rcs_state->dirty = 1;
  523. BUG_ON(from->legacy_hw_ctx.rcs_state->ring != ring);
  524. /* obj is kept alive until the next request by its active ref */
  525. i915_gem_object_ggtt_unpin(from->legacy_hw_ctx.rcs_state);
  526. i915_gem_context_unreference(from);
  527. }
  528. uninitialized = !to->legacy_hw_ctx.initialized && from == NULL;
  529. to->legacy_hw_ctx.initialized = true;
  530. done:
  531. i915_gem_context_reference(to);
  532. ring->last_context = to;
  533. if (uninitialized) {
  534. if (ring->init_context) {
  535. ret = ring->init_context(ring);
  536. if (ret)
  537. DRM_ERROR("ring init context: %d\n", ret);
  538. }
  539. ret = i915_gem_render_state_init(ring);
  540. if (ret)
  541. DRM_ERROR("init render state: %d\n", ret);
  542. }
  543. return 0;
  544. unpin_out:
  545. if (ring->id == RCS)
  546. i915_gem_object_ggtt_unpin(to->legacy_hw_ctx.rcs_state);
  547. return ret;
  548. }
  549. /**
  550. * i915_switch_context() - perform a GPU context switch.
  551. * @ring: ring for which we'll execute the context switch
  552. * @to: the context to switch to
  553. *
  554. * The context life cycle is simple. The context refcount is incremented and
  555. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  556. * it will have a refcount > 1. This allows us to destroy the context abstract
  557. * object while letting the normal object tracking destroy the backing BO.
  558. *
  559. * This function should not be used in execlists mode. Instead the context is
  560. * switched by writing to the ELSP and requests keep a reference to their
  561. * context.
  562. */
  563. int i915_switch_context(struct intel_engine_cs *ring,
  564. struct intel_context *to)
  565. {
  566. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  567. WARN_ON(i915.enable_execlists);
  568. WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  569. if (to->legacy_hw_ctx.rcs_state == NULL) { /* We have the fake context */
  570. if (to != ring->last_context) {
  571. i915_gem_context_reference(to);
  572. if (ring->last_context)
  573. i915_gem_context_unreference(ring->last_context);
  574. ring->last_context = to;
  575. }
  576. return 0;
  577. }
  578. return do_switch(ring, to);
  579. }
  580. static bool contexts_enabled(struct drm_device *dev)
  581. {
  582. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  583. }
  584. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  585. struct drm_file *file)
  586. {
  587. struct drm_i915_gem_context_create *args = data;
  588. struct drm_i915_file_private *file_priv = file->driver_priv;
  589. struct intel_context *ctx;
  590. int ret;
  591. if (!contexts_enabled(dev))
  592. return -ENODEV;
  593. ret = i915_mutex_lock_interruptible(dev);
  594. if (ret)
  595. return ret;
  596. ctx = i915_gem_create_context(dev, file_priv);
  597. mutex_unlock(&dev->struct_mutex);
  598. if (IS_ERR(ctx))
  599. return PTR_ERR(ctx);
  600. args->ctx_id = ctx->user_handle;
  601. DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id);
  602. return 0;
  603. }
  604. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  605. struct drm_file *file)
  606. {
  607. struct drm_i915_gem_context_destroy *args = data;
  608. struct drm_i915_file_private *file_priv = file->driver_priv;
  609. struct intel_context *ctx;
  610. int ret;
  611. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  612. return -ENOENT;
  613. ret = i915_mutex_lock_interruptible(dev);
  614. if (ret)
  615. return ret;
  616. ctx = i915_gem_context_get(file_priv, args->ctx_id);
  617. if (IS_ERR(ctx)) {
  618. mutex_unlock(&dev->struct_mutex);
  619. return PTR_ERR(ctx);
  620. }
  621. idr_remove(&ctx->file_priv->context_idr, ctx->user_handle);
  622. i915_gem_context_unreference(ctx);
  623. mutex_unlock(&dev->struct_mutex);
  624. DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id);
  625. return 0;
  626. }