i915_gem_context.c 27 KB

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