i915_gem_context.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. ctx->hang_stats.ban_period_seconds = DRM_I915_CTX_BAN_PERIOD;
  202. return ctx;
  203. err_out:
  204. i915_gem_context_unreference(ctx);
  205. return ERR_PTR(ret);
  206. }
  207. /**
  208. * The default context needs to exist per ring that uses contexts. It stores the
  209. * context state of the GPU for applications that don't utilize HW contexts, as
  210. * well as an idle case.
  211. */
  212. static struct intel_context *
  213. i915_gem_create_context(struct drm_device *dev,
  214. struct drm_i915_file_private *file_priv)
  215. {
  216. const bool is_global_default_ctx = file_priv == NULL;
  217. struct intel_context *ctx;
  218. int ret = 0;
  219. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  220. ctx = __create_hw_context(dev, file_priv);
  221. if (IS_ERR(ctx))
  222. return ctx;
  223. if (is_global_default_ctx && ctx->legacy_hw_ctx.rcs_state) {
  224. /* We may need to do things with the shrinker which
  225. * require us to immediately switch back to the default
  226. * context. This can cause a problem as pinning the
  227. * default context also requires GTT space which may not
  228. * be available. To avoid this we always pin the default
  229. * context.
  230. */
  231. ret = i915_gem_obj_ggtt_pin(ctx->legacy_hw_ctx.rcs_state,
  232. get_context_alignment(dev), 0);
  233. if (ret) {
  234. DRM_DEBUG_DRIVER("Couldn't pin %d\n", ret);
  235. goto err_destroy;
  236. }
  237. }
  238. if (USES_FULL_PPGTT(dev)) {
  239. struct i915_hw_ppgtt *ppgtt = i915_ppgtt_create(dev, file_priv);
  240. if (IS_ERR_OR_NULL(ppgtt)) {
  241. DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
  242. PTR_ERR(ppgtt));
  243. ret = PTR_ERR(ppgtt);
  244. goto err_unpin;
  245. }
  246. ctx->ppgtt = ppgtt;
  247. }
  248. trace_i915_context_create(ctx);
  249. return ctx;
  250. err_unpin:
  251. if (is_global_default_ctx && ctx->legacy_hw_ctx.rcs_state)
  252. i915_gem_object_ggtt_unpin(ctx->legacy_hw_ctx.rcs_state);
  253. err_destroy:
  254. i915_gem_context_unreference(ctx);
  255. return ERR_PTR(ret);
  256. }
  257. void i915_gem_context_reset(struct drm_device *dev)
  258. {
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. int i;
  261. if (i915.enable_execlists) {
  262. struct intel_context *ctx;
  263. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  264. intel_lr_context_reset(dev, ctx);
  265. }
  266. return;
  267. }
  268. for (i = 0; i < I915_NUM_RINGS; i++) {
  269. struct intel_engine_cs *ring = &dev_priv->ring[i];
  270. struct intel_context *lctx = ring->last_context;
  271. if (lctx) {
  272. if (lctx->legacy_hw_ctx.rcs_state && i == RCS)
  273. i915_gem_object_ggtt_unpin(lctx->legacy_hw_ctx.rcs_state);
  274. i915_gem_context_unreference(lctx);
  275. ring->last_context = NULL;
  276. }
  277. }
  278. }
  279. int i915_gem_context_init(struct drm_device *dev)
  280. {
  281. struct drm_i915_private *dev_priv = dev->dev_private;
  282. struct intel_context *ctx;
  283. int i;
  284. /* Init should only be called once per module load. Eventually the
  285. * restriction on the context_disabled check can be loosened. */
  286. if (WARN_ON(dev_priv->ring[RCS].default_context))
  287. return 0;
  288. if (i915.enable_execlists) {
  289. /* NB: intentionally left blank. We will allocate our own
  290. * backing objects as we need them, thank you very much */
  291. dev_priv->hw_context_size = 0;
  292. } else if (HAS_HW_CONTEXTS(dev)) {
  293. dev_priv->hw_context_size = round_up(get_context_size(dev), 4096);
  294. if (dev_priv->hw_context_size > (1<<20)) {
  295. DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n",
  296. dev_priv->hw_context_size);
  297. dev_priv->hw_context_size = 0;
  298. }
  299. }
  300. ctx = i915_gem_create_context(dev, NULL);
  301. if (IS_ERR(ctx)) {
  302. DRM_ERROR("Failed to create default global context (error %ld)\n",
  303. PTR_ERR(ctx));
  304. return PTR_ERR(ctx);
  305. }
  306. for (i = 0; i < I915_NUM_RINGS; i++) {
  307. struct intel_engine_cs *ring = &dev_priv->ring[i];
  308. /* NB: RCS will hold a ref for all rings */
  309. ring->default_context = ctx;
  310. }
  311. DRM_DEBUG_DRIVER("%s context support initialized\n",
  312. i915.enable_execlists ? "LR" :
  313. dev_priv->hw_context_size ? "HW" : "fake");
  314. return 0;
  315. }
  316. void i915_gem_context_fini(struct drm_device *dev)
  317. {
  318. struct drm_i915_private *dev_priv = dev->dev_private;
  319. struct intel_context *dctx = dev_priv->ring[RCS].default_context;
  320. int i;
  321. if (dctx->legacy_hw_ctx.rcs_state) {
  322. /* The only known way to stop the gpu from accessing the hw context is
  323. * to reset it. Do this as the very last operation to avoid confusing
  324. * other code, leading to spurious errors. */
  325. intel_gpu_reset(dev);
  326. /* When default context is created and switched to, base object refcount
  327. * will be 2 (+1 from object creation and +1 from do_switch()).
  328. * i915_gem_context_fini() will be called after gpu_idle() has switched
  329. * to default context. So we need to unreference the base object once
  330. * to offset the do_switch part, so that i915_gem_context_unreference()
  331. * can then free the base object correctly. */
  332. WARN_ON(!dev_priv->ring[RCS].last_context);
  333. if (dev_priv->ring[RCS].last_context == dctx) {
  334. /* Fake switch to NULL context */
  335. WARN_ON(dctx->legacy_hw_ctx.rcs_state->active);
  336. i915_gem_object_ggtt_unpin(dctx->legacy_hw_ctx.rcs_state);
  337. i915_gem_context_unreference(dctx);
  338. dev_priv->ring[RCS].last_context = NULL;
  339. }
  340. i915_gem_object_ggtt_unpin(dctx->legacy_hw_ctx.rcs_state);
  341. }
  342. for (i = 0; i < I915_NUM_RINGS; i++) {
  343. struct intel_engine_cs *ring = &dev_priv->ring[i];
  344. if (ring->last_context)
  345. i915_gem_context_unreference(ring->last_context);
  346. ring->default_context = NULL;
  347. ring->last_context = NULL;
  348. }
  349. i915_gem_context_unreference(dctx);
  350. }
  351. int i915_gem_context_enable(struct drm_i915_private *dev_priv)
  352. {
  353. struct intel_engine_cs *ring;
  354. int ret, i;
  355. BUG_ON(!dev_priv->ring[RCS].default_context);
  356. if (i915.enable_execlists) {
  357. for_each_ring(ring, dev_priv, i) {
  358. if (ring->init_context) {
  359. ret = ring->init_context(ring,
  360. ring->default_context);
  361. if (ret) {
  362. DRM_ERROR("ring init context: %d\n",
  363. ret);
  364. return ret;
  365. }
  366. }
  367. }
  368. } else
  369. for_each_ring(ring, dev_priv, i) {
  370. ret = i915_switch_context(ring, ring->default_context);
  371. if (ret)
  372. return ret;
  373. }
  374. return 0;
  375. }
  376. static int context_idr_cleanup(int id, void *p, void *data)
  377. {
  378. struct intel_context *ctx = p;
  379. i915_gem_context_unreference(ctx);
  380. return 0;
  381. }
  382. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  383. {
  384. struct drm_i915_file_private *file_priv = file->driver_priv;
  385. struct intel_context *ctx;
  386. idr_init(&file_priv->context_idr);
  387. mutex_lock(&dev->struct_mutex);
  388. ctx = i915_gem_create_context(dev, file_priv);
  389. mutex_unlock(&dev->struct_mutex);
  390. if (IS_ERR(ctx)) {
  391. idr_destroy(&file_priv->context_idr);
  392. return PTR_ERR(ctx);
  393. }
  394. return 0;
  395. }
  396. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  397. {
  398. struct drm_i915_file_private *file_priv = file->driver_priv;
  399. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  400. idr_destroy(&file_priv->context_idr);
  401. }
  402. struct intel_context *
  403. i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
  404. {
  405. struct intel_context *ctx;
  406. ctx = (struct intel_context *)idr_find(&file_priv->context_idr, id);
  407. if (!ctx)
  408. return ERR_PTR(-ENOENT);
  409. return ctx;
  410. }
  411. static inline int
  412. mi_set_context(struct intel_engine_cs *ring,
  413. struct intel_context *new_context,
  414. u32 hw_flags)
  415. {
  416. u32 flags = hw_flags | MI_MM_SPACE_GTT;
  417. const int num_rings =
  418. /* Use an extended w/a on ivb+ if signalling from other rings */
  419. i915_semaphore_is_enabled(ring->dev) ?
  420. hweight32(INTEL_INFO(ring->dev)->ring_mask) - 1 :
  421. 0;
  422. int len, i, ret;
  423. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  424. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  425. * explicitly, so we rely on the value at ring init, stored in
  426. * itlb_before_ctx_switch.
  427. */
  428. if (IS_GEN6(ring->dev)) {
  429. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, 0);
  430. if (ret)
  431. return ret;
  432. }
  433. /* These flags are for resource streamer on HSW+ */
  434. if (!IS_HASWELL(ring->dev) && INTEL_INFO(ring->dev)->gen < 8)
  435. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  436. len = 4;
  437. if (INTEL_INFO(ring->dev)->gen >= 7)
  438. len += 2 + (num_rings ? 4*num_rings + 2 : 0);
  439. ret = intel_ring_begin(ring, len);
  440. if (ret)
  441. return ret;
  442. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  443. if (INTEL_INFO(ring->dev)->gen >= 7) {
  444. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  445. if (num_rings) {
  446. struct intel_engine_cs *signaller;
  447. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_rings));
  448. for_each_ring(signaller, to_i915(ring->dev), i) {
  449. if (signaller == ring)
  450. continue;
  451. intel_ring_emit(ring, RING_PSMI_CTL(signaller->mmio_base));
  452. intel_ring_emit(ring, _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  453. }
  454. }
  455. }
  456. intel_ring_emit(ring, MI_NOOP);
  457. intel_ring_emit(ring, MI_SET_CONTEXT);
  458. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(new_context->legacy_hw_ctx.rcs_state) |
  459. flags);
  460. /*
  461. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  462. * WaMiSetContext_Hang:snb,ivb,vlv
  463. */
  464. intel_ring_emit(ring, MI_NOOP);
  465. if (INTEL_INFO(ring->dev)->gen >= 7) {
  466. if (num_rings) {
  467. struct intel_engine_cs *signaller;
  468. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_rings));
  469. for_each_ring(signaller, to_i915(ring->dev), i) {
  470. if (signaller == ring)
  471. continue;
  472. intel_ring_emit(ring, RING_PSMI_CTL(signaller->mmio_base));
  473. intel_ring_emit(ring, _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  474. }
  475. }
  476. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  477. }
  478. intel_ring_advance(ring);
  479. return ret;
  480. }
  481. static inline bool should_skip_switch(struct intel_engine_cs *ring,
  482. struct intel_context *from,
  483. struct intel_context *to)
  484. {
  485. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  486. if (to->remap_slice)
  487. return false;
  488. if (to->ppgtt) {
  489. if (from == to && !test_bit(ring->id,
  490. &to->ppgtt->pd_dirty_rings))
  491. return true;
  492. } else if (dev_priv->mm.aliasing_ppgtt) {
  493. if (from == to && !test_bit(ring->id,
  494. &dev_priv->mm.aliasing_ppgtt->pd_dirty_rings))
  495. return true;
  496. }
  497. return false;
  498. }
  499. static bool
  500. needs_pd_load_pre(struct intel_engine_cs *ring, struct intel_context *to)
  501. {
  502. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  503. if (!to->ppgtt)
  504. return false;
  505. if (INTEL_INFO(ring->dev)->gen < 8)
  506. return true;
  507. if (ring != &dev_priv->ring[RCS])
  508. return true;
  509. return false;
  510. }
  511. static bool
  512. needs_pd_load_post(struct intel_engine_cs *ring, struct intel_context *to,
  513. u32 hw_flags)
  514. {
  515. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  516. if (!to->ppgtt)
  517. return false;
  518. if (!IS_GEN8(ring->dev))
  519. return false;
  520. if (ring != &dev_priv->ring[RCS])
  521. return false;
  522. if (hw_flags & MI_RESTORE_INHIBIT)
  523. return true;
  524. return false;
  525. }
  526. static int do_switch(struct intel_engine_cs *ring,
  527. struct intel_context *to)
  528. {
  529. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  530. struct intel_context *from = ring->last_context;
  531. u32 hw_flags = 0;
  532. bool uninitialized = false;
  533. struct i915_vma *vma;
  534. int ret, i;
  535. if (from != NULL && ring == &dev_priv->ring[RCS]) {
  536. BUG_ON(from->legacy_hw_ctx.rcs_state == NULL);
  537. BUG_ON(!i915_gem_obj_is_pinned(from->legacy_hw_ctx.rcs_state));
  538. }
  539. if (should_skip_switch(ring, from, to))
  540. return 0;
  541. /* Trying to pin first makes error handling easier. */
  542. if (ring == &dev_priv->ring[RCS]) {
  543. ret = i915_gem_obj_ggtt_pin(to->legacy_hw_ctx.rcs_state,
  544. get_context_alignment(ring->dev), 0);
  545. if (ret)
  546. return ret;
  547. }
  548. /*
  549. * Pin can switch back to the default context if we end up calling into
  550. * evict_everything - as a last ditch gtt defrag effort that also
  551. * switches to the default context. Hence we need to reload from here.
  552. */
  553. from = ring->last_context;
  554. if (needs_pd_load_pre(ring, to)) {
  555. /* Older GENs and non render rings still want the load first,
  556. * "PP_DCLV followed by PP_DIR_BASE register through Load
  557. * Register Immediate commands in Ring Buffer before submitting
  558. * a context."*/
  559. trace_switch_mm(ring, to);
  560. ret = to->ppgtt->switch_mm(to->ppgtt, ring);
  561. if (ret)
  562. goto unpin_out;
  563. /* Doing a PD load always reloads the page dirs */
  564. clear_bit(ring->id, &to->ppgtt->pd_dirty_rings);
  565. }
  566. if (ring != &dev_priv->ring[RCS]) {
  567. if (from)
  568. i915_gem_context_unreference(from);
  569. goto done;
  570. }
  571. /*
  572. * Clear this page out of any CPU caches for coherent swap-in/out. Note
  573. * that thanks to write = false in this call and us not setting any gpu
  574. * write domains when putting a context object onto the active list
  575. * (when switching away from it), this won't block.
  576. *
  577. * XXX: We need a real interface to do this instead of trickery.
  578. */
  579. ret = i915_gem_object_set_to_gtt_domain(to->legacy_hw_ctx.rcs_state, false);
  580. if (ret)
  581. goto unpin_out;
  582. vma = i915_gem_obj_to_ggtt(to->legacy_hw_ctx.rcs_state);
  583. if (!(vma->bound & GLOBAL_BIND)) {
  584. ret = i915_vma_bind(vma,
  585. to->legacy_hw_ctx.rcs_state->cache_level,
  586. GLOBAL_BIND);
  587. /* This shouldn't ever fail. */
  588. if (WARN_ONCE(ret, "GGTT context bind failed!"))
  589. goto unpin_out;
  590. }
  591. if (!to->legacy_hw_ctx.initialized) {
  592. hw_flags |= MI_RESTORE_INHIBIT;
  593. /* NB: If we inhibit the restore, the context is not allowed to
  594. * die because future work may end up depending on valid address
  595. * space. This means we must enforce that a page table load
  596. * occur when this occurs. */
  597. } else if (to->ppgtt &&
  598. test_and_clear_bit(ring->id, &to->ppgtt->pd_dirty_rings))
  599. hw_flags |= MI_FORCE_RESTORE;
  600. /* We should never emit switch_mm more than once */
  601. WARN_ON(needs_pd_load_pre(ring, to) &&
  602. needs_pd_load_post(ring, to, hw_flags));
  603. ret = mi_set_context(ring, to, hw_flags);
  604. if (ret)
  605. goto unpin_out;
  606. /* GEN8 does *not* require an explicit reload if the PDPs have been
  607. * setup, and we do not wish to move them.
  608. */
  609. if (needs_pd_load_post(ring, to, hw_flags)) {
  610. trace_switch_mm(ring, to);
  611. ret = to->ppgtt->switch_mm(to->ppgtt, ring);
  612. /* The hardware context switch is emitted, but we haven't
  613. * actually changed the state - so it's probably safe to bail
  614. * here. Still, let the user know something dangerous has
  615. * happened.
  616. */
  617. if (ret) {
  618. DRM_ERROR("Failed to change address space on context switch\n");
  619. goto unpin_out;
  620. }
  621. }
  622. for (i = 0; i < MAX_L3_SLICES; i++) {
  623. if (!(to->remap_slice & (1<<i)))
  624. continue;
  625. ret = i915_gem_l3_remap(ring, i);
  626. /* If it failed, try again next round */
  627. if (ret)
  628. DRM_DEBUG_DRIVER("L3 remapping failed\n");
  629. else
  630. to->remap_slice &= ~(1<<i);
  631. }
  632. /* The backing object for the context is done after switching to the
  633. * *next* context. Therefore we cannot retire the previous context until
  634. * the next context has already started running. In fact, the below code
  635. * is a bit suboptimal because the retiring can occur simply after the
  636. * MI_SET_CONTEXT instead of when the next seqno has completed.
  637. */
  638. if (from != NULL) {
  639. from->legacy_hw_ctx.rcs_state->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
  640. i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->legacy_hw_ctx.rcs_state), ring);
  641. /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the
  642. * whole damn pipeline, we don't need to explicitly mark the
  643. * object dirty. The only exception is that the context must be
  644. * correct in case the object gets swapped out. Ideally we'd be
  645. * able to defer doing this until we know the object would be
  646. * swapped, but there is no way to do that yet.
  647. */
  648. from->legacy_hw_ctx.rcs_state->dirty = 1;
  649. BUG_ON(i915_gem_request_get_ring(
  650. from->legacy_hw_ctx.rcs_state->last_read_req) != ring);
  651. /* obj is kept alive until the next request by its active ref */
  652. i915_gem_object_ggtt_unpin(from->legacy_hw_ctx.rcs_state);
  653. i915_gem_context_unreference(from);
  654. }
  655. uninitialized = !to->legacy_hw_ctx.initialized;
  656. to->legacy_hw_ctx.initialized = true;
  657. done:
  658. i915_gem_context_reference(to);
  659. ring->last_context = to;
  660. if (uninitialized) {
  661. if (ring->init_context) {
  662. ret = ring->init_context(ring, to);
  663. if (ret)
  664. DRM_ERROR("ring init context: %d\n", ret);
  665. }
  666. }
  667. return 0;
  668. unpin_out:
  669. if (ring->id == RCS)
  670. i915_gem_object_ggtt_unpin(to->legacy_hw_ctx.rcs_state);
  671. return ret;
  672. }
  673. /**
  674. * i915_switch_context() - perform a GPU context switch.
  675. * @ring: ring for which we'll execute the context switch
  676. * @to: the context to switch to
  677. *
  678. * The context life cycle is simple. The context refcount is incremented and
  679. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  680. * it will have a refcount > 1. This allows us to destroy the context abstract
  681. * object while letting the normal object tracking destroy the backing BO.
  682. *
  683. * This function should not be used in execlists mode. Instead the context is
  684. * switched by writing to the ELSP and requests keep a reference to their
  685. * context.
  686. */
  687. int i915_switch_context(struct intel_engine_cs *ring,
  688. struct intel_context *to)
  689. {
  690. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  691. WARN_ON(i915.enable_execlists);
  692. WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  693. if (to->legacy_hw_ctx.rcs_state == NULL) { /* We have the fake context */
  694. if (to != ring->last_context) {
  695. i915_gem_context_reference(to);
  696. if (ring->last_context)
  697. i915_gem_context_unreference(ring->last_context);
  698. ring->last_context = to;
  699. }
  700. return 0;
  701. }
  702. return do_switch(ring, to);
  703. }
  704. static bool contexts_enabled(struct drm_device *dev)
  705. {
  706. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  707. }
  708. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  709. struct drm_file *file)
  710. {
  711. struct drm_i915_gem_context_create *args = data;
  712. struct drm_i915_file_private *file_priv = file->driver_priv;
  713. struct intel_context *ctx;
  714. int ret;
  715. if (!contexts_enabled(dev))
  716. return -ENODEV;
  717. ret = i915_mutex_lock_interruptible(dev);
  718. if (ret)
  719. return ret;
  720. ctx = i915_gem_create_context(dev, file_priv);
  721. mutex_unlock(&dev->struct_mutex);
  722. if (IS_ERR(ctx))
  723. return PTR_ERR(ctx);
  724. args->ctx_id = ctx->user_handle;
  725. DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id);
  726. return 0;
  727. }
  728. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  729. struct drm_file *file)
  730. {
  731. struct drm_i915_gem_context_destroy *args = data;
  732. struct drm_i915_file_private *file_priv = file->driver_priv;
  733. struct intel_context *ctx;
  734. int ret;
  735. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  736. return -ENOENT;
  737. ret = i915_mutex_lock_interruptible(dev);
  738. if (ret)
  739. return ret;
  740. ctx = i915_gem_context_get(file_priv, args->ctx_id);
  741. if (IS_ERR(ctx)) {
  742. mutex_unlock(&dev->struct_mutex);
  743. return PTR_ERR(ctx);
  744. }
  745. idr_remove(&ctx->file_priv->context_idr, ctx->user_handle);
  746. i915_gem_context_unreference(ctx);
  747. mutex_unlock(&dev->struct_mutex);
  748. DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id);
  749. return 0;
  750. }
  751. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  752. struct drm_file *file)
  753. {
  754. struct drm_i915_file_private *file_priv = file->driver_priv;
  755. struct drm_i915_gem_context_param *args = data;
  756. struct intel_context *ctx;
  757. int ret;
  758. ret = i915_mutex_lock_interruptible(dev);
  759. if (ret)
  760. return ret;
  761. ctx = i915_gem_context_get(file_priv, args->ctx_id);
  762. if (IS_ERR(ctx)) {
  763. mutex_unlock(&dev->struct_mutex);
  764. return PTR_ERR(ctx);
  765. }
  766. args->size = 0;
  767. switch (args->param) {
  768. case I915_CONTEXT_PARAM_BAN_PERIOD:
  769. args->value = ctx->hang_stats.ban_period_seconds;
  770. break;
  771. default:
  772. ret = -EINVAL;
  773. break;
  774. }
  775. mutex_unlock(&dev->struct_mutex);
  776. return ret;
  777. }
  778. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  779. struct drm_file *file)
  780. {
  781. struct drm_i915_file_private *file_priv = file->driver_priv;
  782. struct drm_i915_gem_context_param *args = data;
  783. struct intel_context *ctx;
  784. int ret;
  785. ret = i915_mutex_lock_interruptible(dev);
  786. if (ret)
  787. return ret;
  788. ctx = i915_gem_context_get(file_priv, args->ctx_id);
  789. if (IS_ERR(ctx)) {
  790. mutex_unlock(&dev->struct_mutex);
  791. return PTR_ERR(ctx);
  792. }
  793. switch (args->param) {
  794. case I915_CONTEXT_PARAM_BAN_PERIOD:
  795. if (args->size)
  796. ret = -EINVAL;
  797. else if (args->value < ctx->hang_stats.ban_period_seconds &&
  798. !capable(CAP_SYS_ADMIN))
  799. ret = -EPERM;
  800. else
  801. ctx->hang_stats.ban_period_seconds = args->value;
  802. break;
  803. default:
  804. ret = -EINVAL;
  805. break;
  806. }
  807. mutex_unlock(&dev->struct_mutex);
  808. return ret;
  809. }