i915_gem_context.c 23 KB

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