i915_gem_context.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
  91. /* This is a HW constraint. The value below is the largest known requirement
  92. * I've seen in a spec to date, and that was a workaround for a non-shipping
  93. * part. It should be safe to decrease this, but it's more future proof as is.
  94. */
  95. #define GEN6_CONTEXT_ALIGN (64<<10)
  96. #define GEN7_CONTEXT_ALIGN 4096
  97. static size_t get_context_alignment(struct drm_i915_private *dev_priv)
  98. {
  99. if (IS_GEN6(dev_priv))
  100. return GEN6_CONTEXT_ALIGN;
  101. return GEN7_CONTEXT_ALIGN;
  102. }
  103. static int get_context_size(struct drm_i915_private *dev_priv)
  104. {
  105. int ret;
  106. u32 reg;
  107. switch (INTEL_GEN(dev_priv)) {
  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_priv))
  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. static void i915_gem_context_clean(struct i915_gem_context *ctx)
  128. {
  129. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  130. struct i915_vma *vma, *next;
  131. if (!ppgtt)
  132. return;
  133. list_for_each_entry_safe(vma, next, &ppgtt->base.inactive_list,
  134. vm_link) {
  135. if (WARN_ON(__i915_vma_unbind_no_wait(vma)))
  136. break;
  137. }
  138. }
  139. void i915_gem_context_free(struct kref *ctx_ref)
  140. {
  141. struct i915_gem_context *ctx = container_of(ctx_ref, typeof(*ctx), ref);
  142. int i;
  143. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  144. trace_i915_context_free(ctx);
  145. /*
  146. * This context is going away and we need to remove all VMAs still
  147. * around. This is to handle imported shared objects for which
  148. * destructor did not run when their handles were closed.
  149. */
  150. i915_gem_context_clean(ctx);
  151. i915_ppgtt_put(ctx->ppgtt);
  152. for (i = 0; i < I915_NUM_ENGINES; i++) {
  153. struct intel_context *ce = &ctx->engine[i];
  154. if (!ce->state)
  155. continue;
  156. WARN_ON(ce->pin_count);
  157. if (ce->ring)
  158. intel_ring_free(ce->ring);
  159. i915_gem_object_put(ce->state);
  160. }
  161. list_del(&ctx->link);
  162. ida_simple_remove(&ctx->i915->context_hw_ida, ctx->hw_id);
  163. kfree(ctx);
  164. }
  165. struct drm_i915_gem_object *
  166. i915_gem_alloc_context_obj(struct drm_device *dev, size_t size)
  167. {
  168. struct drm_i915_gem_object *obj;
  169. int ret;
  170. lockdep_assert_held(&dev->struct_mutex);
  171. obj = i915_gem_object_create(dev, size);
  172. if (IS_ERR(obj))
  173. return obj;
  174. /*
  175. * Try to make the context utilize L3 as well as LLC.
  176. *
  177. * On VLV we don't have L3 controls in the PTEs so we
  178. * shouldn't touch the cache level, especially as that
  179. * would make the object snooped which might have a
  180. * negative performance impact.
  181. *
  182. * Snooping is required on non-llc platforms in execlist
  183. * mode, but since all GGTT accesses use PAT entry 0 we
  184. * get snooping anyway regardless of cache_level.
  185. *
  186. * This is only applicable for Ivy Bridge devices since
  187. * later platforms don't have L3 control bits in the PTE.
  188. */
  189. if (IS_IVYBRIDGE(dev)) {
  190. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  191. /* Failure shouldn't ever happen this early */
  192. if (WARN_ON(ret)) {
  193. i915_gem_object_put(obj);
  194. return ERR_PTR(ret);
  195. }
  196. }
  197. return obj;
  198. }
  199. static int assign_hw_id(struct drm_i915_private *dev_priv, unsigned *out)
  200. {
  201. int ret;
  202. ret = ida_simple_get(&dev_priv->context_hw_ida,
  203. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  204. if (ret < 0) {
  205. /* Contexts are only released when no longer active.
  206. * Flush any pending retires to hopefully release some
  207. * stale contexts and try again.
  208. */
  209. i915_gem_retire_requests(dev_priv);
  210. ret = ida_simple_get(&dev_priv->context_hw_ida,
  211. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. *out = ret;
  216. return 0;
  217. }
  218. static struct i915_gem_context *
  219. __create_hw_context(struct drm_device *dev,
  220. struct drm_i915_file_private *file_priv)
  221. {
  222. struct drm_i915_private *dev_priv = to_i915(dev);
  223. struct i915_gem_context *ctx;
  224. int ret;
  225. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  226. if (ctx == NULL)
  227. return ERR_PTR(-ENOMEM);
  228. ret = assign_hw_id(dev_priv, &ctx->hw_id);
  229. if (ret) {
  230. kfree(ctx);
  231. return ERR_PTR(ret);
  232. }
  233. kref_init(&ctx->ref);
  234. list_add_tail(&ctx->link, &dev_priv->context_list);
  235. ctx->i915 = dev_priv;
  236. ctx->ggtt_alignment = get_context_alignment(dev_priv);
  237. if (dev_priv->hw_context_size) {
  238. struct drm_i915_gem_object *obj =
  239. i915_gem_alloc_context_obj(dev, dev_priv->hw_context_size);
  240. if (IS_ERR(obj)) {
  241. ret = PTR_ERR(obj);
  242. goto err_out;
  243. }
  244. ctx->engine[RCS].state = obj;
  245. }
  246. /* Default context will never have a file_priv */
  247. if (file_priv != NULL) {
  248. ret = idr_alloc(&file_priv->context_idr, ctx,
  249. DEFAULT_CONTEXT_HANDLE, 0, GFP_KERNEL);
  250. if (ret < 0)
  251. goto err_out;
  252. } else
  253. ret = DEFAULT_CONTEXT_HANDLE;
  254. ctx->file_priv = file_priv;
  255. ctx->user_handle = ret;
  256. /* NB: Mark all slices as needing a remap so that when the context first
  257. * loads it will restore whatever remap state already exists. If there
  258. * is no remap info, it will be a NOP. */
  259. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  260. ctx->hang_stats.ban_period_seconds = DRM_I915_CTX_BAN_PERIOD;
  261. ctx->ring_size = 4 * PAGE_SIZE;
  262. ctx->desc_template = GEN8_CTX_ADDRESSING_MODE(dev_priv) <<
  263. GEN8_CTX_ADDRESSING_MODE_SHIFT;
  264. ATOMIC_INIT_NOTIFIER_HEAD(&ctx->status_notifier);
  265. return ctx;
  266. err_out:
  267. i915_gem_context_put(ctx);
  268. return ERR_PTR(ret);
  269. }
  270. /**
  271. * The default context needs to exist per ring that uses contexts. It stores the
  272. * context state of the GPU for applications that don't utilize HW contexts, as
  273. * well as an idle case.
  274. */
  275. static struct i915_gem_context *
  276. i915_gem_create_context(struct drm_device *dev,
  277. struct drm_i915_file_private *file_priv)
  278. {
  279. struct i915_gem_context *ctx;
  280. lockdep_assert_held(&dev->struct_mutex);
  281. ctx = __create_hw_context(dev, file_priv);
  282. if (IS_ERR(ctx))
  283. return ctx;
  284. if (USES_FULL_PPGTT(dev)) {
  285. struct i915_hw_ppgtt *ppgtt = i915_ppgtt_create(dev, file_priv);
  286. if (IS_ERR(ppgtt)) {
  287. DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
  288. PTR_ERR(ppgtt));
  289. idr_remove(&file_priv->context_idr, ctx->user_handle);
  290. i915_gem_context_put(ctx);
  291. return ERR_CAST(ppgtt);
  292. }
  293. ctx->ppgtt = ppgtt;
  294. }
  295. trace_i915_context_create(ctx);
  296. return ctx;
  297. }
  298. /**
  299. * i915_gem_context_create_gvt - create a GVT GEM context
  300. * @dev: drm device *
  301. *
  302. * This function is used to create a GVT specific GEM context.
  303. *
  304. * Returns:
  305. * pointer to i915_gem_context on success, error pointer if failed
  306. *
  307. */
  308. struct i915_gem_context *
  309. i915_gem_context_create_gvt(struct drm_device *dev)
  310. {
  311. struct i915_gem_context *ctx;
  312. int ret;
  313. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  314. return ERR_PTR(-ENODEV);
  315. ret = i915_mutex_lock_interruptible(dev);
  316. if (ret)
  317. return ERR_PTR(ret);
  318. ctx = i915_gem_create_context(dev, NULL);
  319. if (IS_ERR(ctx))
  320. goto out;
  321. ctx->execlists_force_single_submission = true;
  322. ctx->ring_size = 512 * PAGE_SIZE; /* Max ring buffer size */
  323. out:
  324. mutex_unlock(&dev->struct_mutex);
  325. return ctx;
  326. }
  327. static void i915_gem_context_unpin(struct i915_gem_context *ctx,
  328. struct intel_engine_cs *engine)
  329. {
  330. if (i915.enable_execlists) {
  331. intel_lr_context_unpin(ctx, engine);
  332. } else {
  333. struct intel_context *ce = &ctx->engine[engine->id];
  334. if (ce->state)
  335. i915_gem_object_ggtt_unpin(ce->state);
  336. i915_gem_context_put(ctx);
  337. }
  338. }
  339. void i915_gem_context_reset(struct drm_device *dev)
  340. {
  341. struct drm_i915_private *dev_priv = to_i915(dev);
  342. lockdep_assert_held(&dev->struct_mutex);
  343. if (i915.enable_execlists) {
  344. struct i915_gem_context *ctx;
  345. list_for_each_entry(ctx, &dev_priv->context_list, link)
  346. intel_lr_context_reset(dev_priv, ctx);
  347. }
  348. i915_gem_context_lost(dev_priv);
  349. }
  350. int i915_gem_context_init(struct drm_device *dev)
  351. {
  352. struct drm_i915_private *dev_priv = to_i915(dev);
  353. struct i915_gem_context *ctx;
  354. /* Init should only be called once per module load. Eventually the
  355. * restriction on the context_disabled check can be loosened. */
  356. if (WARN_ON(dev_priv->kernel_context))
  357. return 0;
  358. if (intel_vgpu_active(dev_priv) &&
  359. HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  360. if (!i915.enable_execlists) {
  361. DRM_INFO("Only EXECLIST mode is supported in vgpu.\n");
  362. return -EINVAL;
  363. }
  364. }
  365. /* Using the simple ida interface, the max is limited by sizeof(int) */
  366. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
  367. ida_init(&dev_priv->context_hw_ida);
  368. if (i915.enable_execlists) {
  369. /* NB: intentionally left blank. We will allocate our own
  370. * backing objects as we need them, thank you very much */
  371. dev_priv->hw_context_size = 0;
  372. } else if (HAS_HW_CONTEXTS(dev_priv)) {
  373. dev_priv->hw_context_size =
  374. round_up(get_context_size(dev_priv), 4096);
  375. if (dev_priv->hw_context_size > (1<<20)) {
  376. DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n",
  377. dev_priv->hw_context_size);
  378. dev_priv->hw_context_size = 0;
  379. }
  380. }
  381. ctx = i915_gem_create_context(dev, NULL);
  382. if (IS_ERR(ctx)) {
  383. DRM_ERROR("Failed to create default global context (error %ld)\n",
  384. PTR_ERR(ctx));
  385. return PTR_ERR(ctx);
  386. }
  387. dev_priv->kernel_context = ctx;
  388. DRM_DEBUG_DRIVER("%s context support initialized\n",
  389. i915.enable_execlists ? "LR" :
  390. dev_priv->hw_context_size ? "HW" : "fake");
  391. return 0;
  392. }
  393. void i915_gem_context_lost(struct drm_i915_private *dev_priv)
  394. {
  395. struct intel_engine_cs *engine;
  396. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  397. for_each_engine(engine, dev_priv) {
  398. if (engine->last_context) {
  399. i915_gem_context_unpin(engine->last_context, engine);
  400. engine->last_context = NULL;
  401. }
  402. }
  403. /* Force the GPU state to be restored on enabling */
  404. if (!i915.enable_execlists) {
  405. struct i915_gem_context *ctx;
  406. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  407. if (!i915_gem_context_is_default(ctx))
  408. continue;
  409. for_each_engine(engine, dev_priv)
  410. ctx->engine[engine->id].initialised = false;
  411. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  412. }
  413. for_each_engine(engine, dev_priv) {
  414. struct intel_context *kce =
  415. &dev_priv->kernel_context->engine[engine->id];
  416. kce->initialised = true;
  417. }
  418. }
  419. }
  420. void i915_gem_context_fini(struct drm_device *dev)
  421. {
  422. struct drm_i915_private *dev_priv = to_i915(dev);
  423. struct i915_gem_context *dctx = dev_priv->kernel_context;
  424. lockdep_assert_held(&dev->struct_mutex);
  425. i915_gem_context_put(dctx);
  426. dev_priv->kernel_context = NULL;
  427. ida_destroy(&dev_priv->context_hw_ida);
  428. }
  429. static int context_idr_cleanup(int id, void *p, void *data)
  430. {
  431. struct i915_gem_context *ctx = p;
  432. ctx->file_priv = ERR_PTR(-EBADF);
  433. i915_gem_context_put(ctx);
  434. return 0;
  435. }
  436. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  437. {
  438. struct drm_i915_file_private *file_priv = file->driver_priv;
  439. struct i915_gem_context *ctx;
  440. idr_init(&file_priv->context_idr);
  441. mutex_lock(&dev->struct_mutex);
  442. ctx = i915_gem_create_context(dev, file_priv);
  443. mutex_unlock(&dev->struct_mutex);
  444. if (IS_ERR(ctx)) {
  445. idr_destroy(&file_priv->context_idr);
  446. return PTR_ERR(ctx);
  447. }
  448. return 0;
  449. }
  450. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  451. {
  452. struct drm_i915_file_private *file_priv = file->driver_priv;
  453. lockdep_assert_held(&dev->struct_mutex);
  454. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  455. idr_destroy(&file_priv->context_idr);
  456. }
  457. static inline int
  458. mi_set_context(struct drm_i915_gem_request *req, u32 hw_flags)
  459. {
  460. struct drm_i915_private *dev_priv = req->i915;
  461. struct intel_ring *ring = req->ring;
  462. struct intel_engine_cs *engine = req->engine;
  463. u32 flags = hw_flags | MI_MM_SPACE_GTT;
  464. const int num_rings =
  465. /* Use an extended w/a on ivb+ if signalling from other rings */
  466. i915.semaphores ?
  467. hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1 :
  468. 0;
  469. int len, ret;
  470. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  471. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  472. * explicitly, so we rely on the value at ring init, stored in
  473. * itlb_before_ctx_switch.
  474. */
  475. if (IS_GEN6(dev_priv)) {
  476. ret = engine->emit_flush(req, I915_GEM_GPU_DOMAINS, 0);
  477. if (ret)
  478. return ret;
  479. }
  480. /* These flags are for resource streamer on HSW+ */
  481. if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8)
  482. flags |= (HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN);
  483. else if (INTEL_GEN(dev_priv) < 8)
  484. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  485. len = 4;
  486. if (INTEL_GEN(dev_priv) >= 7)
  487. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  488. ret = intel_ring_begin(req, len);
  489. if (ret)
  490. return ret;
  491. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  492. if (INTEL_GEN(dev_priv) >= 7) {
  493. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  494. if (num_rings) {
  495. struct intel_engine_cs *signaller;
  496. intel_ring_emit(ring,
  497. MI_LOAD_REGISTER_IMM(num_rings));
  498. for_each_engine(signaller, dev_priv) {
  499. if (signaller == engine)
  500. continue;
  501. intel_ring_emit_reg(ring,
  502. RING_PSMI_CTL(signaller->mmio_base));
  503. intel_ring_emit(ring,
  504. _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  505. }
  506. }
  507. }
  508. intel_ring_emit(ring, MI_NOOP);
  509. intel_ring_emit(ring, MI_SET_CONTEXT);
  510. intel_ring_emit(ring,
  511. i915_gem_obj_ggtt_offset(req->ctx->engine[RCS].state) |
  512. flags);
  513. /*
  514. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  515. * WaMiSetContext_Hang:snb,ivb,vlv
  516. */
  517. intel_ring_emit(ring, MI_NOOP);
  518. if (INTEL_GEN(dev_priv) >= 7) {
  519. if (num_rings) {
  520. struct intel_engine_cs *signaller;
  521. i915_reg_t last_reg = {}; /* keep gcc quiet */
  522. intel_ring_emit(ring,
  523. MI_LOAD_REGISTER_IMM(num_rings));
  524. for_each_engine(signaller, dev_priv) {
  525. if (signaller == engine)
  526. continue;
  527. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  528. intel_ring_emit_reg(ring, last_reg);
  529. intel_ring_emit(ring,
  530. _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  531. }
  532. /* Insert a delay before the next switch! */
  533. intel_ring_emit(ring,
  534. MI_STORE_REGISTER_MEM |
  535. MI_SRM_LRM_GLOBAL_GTT);
  536. intel_ring_emit_reg(ring, last_reg);
  537. intel_ring_emit(ring, engine->scratch.gtt_offset);
  538. intel_ring_emit(ring, MI_NOOP);
  539. }
  540. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  541. }
  542. intel_ring_advance(ring);
  543. return ret;
  544. }
  545. static int remap_l3(struct drm_i915_gem_request *req, int slice)
  546. {
  547. u32 *remap_info = req->i915->l3_parity.remap_info[slice];
  548. struct intel_ring *ring = req->ring;
  549. int i, ret;
  550. if (!remap_info)
  551. return 0;
  552. ret = intel_ring_begin(req, GEN7_L3LOG_SIZE/4 * 2 + 2);
  553. if (ret)
  554. return ret;
  555. /*
  556. * Note: We do not worry about the concurrent register cacheline hang
  557. * here because no other code should access these registers other than
  558. * at initialization time.
  559. */
  560. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4));
  561. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  562. intel_ring_emit_reg(ring, GEN7_L3LOG(slice, i));
  563. intel_ring_emit(ring, remap_info[i]);
  564. }
  565. intel_ring_emit(ring, MI_NOOP);
  566. intel_ring_advance(ring);
  567. return 0;
  568. }
  569. static inline bool skip_rcs_switch(struct i915_hw_ppgtt *ppgtt,
  570. struct intel_engine_cs *engine,
  571. struct i915_gem_context *to)
  572. {
  573. if (to->remap_slice)
  574. return false;
  575. if (!to->engine[RCS].initialised)
  576. return false;
  577. if (ppgtt && (intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  578. return false;
  579. return to == engine->last_context;
  580. }
  581. static bool
  582. needs_pd_load_pre(struct i915_hw_ppgtt *ppgtt,
  583. struct intel_engine_cs *engine,
  584. struct i915_gem_context *to)
  585. {
  586. if (!ppgtt)
  587. return false;
  588. /* Always load the ppgtt on first use */
  589. if (!engine->last_context)
  590. return true;
  591. /* Same context without new entries, skip */
  592. if (engine->last_context == to &&
  593. !(intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  594. return false;
  595. if (engine->id != RCS)
  596. return true;
  597. if (INTEL_GEN(engine->i915) < 8)
  598. return true;
  599. return false;
  600. }
  601. static bool
  602. needs_pd_load_post(struct i915_hw_ppgtt *ppgtt,
  603. struct i915_gem_context *to,
  604. u32 hw_flags)
  605. {
  606. if (!ppgtt)
  607. return false;
  608. if (!IS_GEN8(to->i915))
  609. return false;
  610. if (hw_flags & MI_RESTORE_INHIBIT)
  611. return true;
  612. return false;
  613. }
  614. static int do_rcs_switch(struct drm_i915_gem_request *req)
  615. {
  616. struct i915_gem_context *to = req->ctx;
  617. struct intel_engine_cs *engine = req->engine;
  618. struct i915_hw_ppgtt *ppgtt = to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  619. struct i915_gem_context *from;
  620. u32 hw_flags;
  621. int ret, i;
  622. if (skip_rcs_switch(ppgtt, engine, to))
  623. return 0;
  624. /* Trying to pin first makes error handling easier. */
  625. ret = i915_gem_obj_ggtt_pin(to->engine[RCS].state,
  626. to->ggtt_alignment,
  627. 0);
  628. if (ret)
  629. return ret;
  630. /*
  631. * Pin can switch back to the default context if we end up calling into
  632. * evict_everything - as a last ditch gtt defrag effort that also
  633. * switches to the default context. Hence we need to reload from here.
  634. *
  635. * XXX: Doing so is painfully broken!
  636. */
  637. from = engine->last_context;
  638. /*
  639. * Clear this page out of any CPU caches for coherent swap-in/out. Note
  640. * that thanks to write = false in this call and us not setting any gpu
  641. * write domains when putting a context object onto the active list
  642. * (when switching away from it), this won't block.
  643. *
  644. * XXX: We need a real interface to do this instead of trickery.
  645. */
  646. ret = i915_gem_object_set_to_gtt_domain(to->engine[RCS].state, false);
  647. if (ret)
  648. goto unpin_out;
  649. if (needs_pd_load_pre(ppgtt, engine, to)) {
  650. /* Older GENs and non render rings still want the load first,
  651. * "PP_DCLV followed by PP_DIR_BASE register through Load
  652. * Register Immediate commands in Ring Buffer before submitting
  653. * a context."*/
  654. trace_switch_mm(engine, to);
  655. ret = ppgtt->switch_mm(ppgtt, req);
  656. if (ret)
  657. goto unpin_out;
  658. }
  659. if (!to->engine[RCS].initialised || i915_gem_context_is_default(to))
  660. /* NB: If we inhibit the restore, the context is not allowed to
  661. * die because future work may end up depending on valid address
  662. * space. This means we must enforce that a page table load
  663. * occur when this occurs. */
  664. hw_flags = MI_RESTORE_INHIBIT;
  665. else if (ppgtt && intel_engine_flag(engine) & ppgtt->pd_dirty_rings)
  666. hw_flags = MI_FORCE_RESTORE;
  667. else
  668. hw_flags = 0;
  669. if (to != from || (hw_flags & MI_FORCE_RESTORE)) {
  670. ret = mi_set_context(req, hw_flags);
  671. if (ret)
  672. goto unpin_out;
  673. }
  674. /* The backing object for the context is done after switching to the
  675. * *next* context. Therefore we cannot retire the previous context until
  676. * the next context has already started running. In fact, the below code
  677. * is a bit suboptimal because the retiring can occur simply after the
  678. * MI_SET_CONTEXT instead of when the next seqno has completed.
  679. */
  680. if (from != NULL) {
  681. from->engine[RCS].state->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
  682. i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->engine[RCS].state), req);
  683. /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the
  684. * whole damn pipeline, we don't need to explicitly mark the
  685. * object dirty. The only exception is that the context must be
  686. * correct in case the object gets swapped out. Ideally we'd be
  687. * able to defer doing this until we know the object would be
  688. * swapped, but there is no way to do that yet.
  689. */
  690. from->engine[RCS].state->dirty = 1;
  691. /* obj is kept alive until the next request by its active ref */
  692. i915_gem_object_ggtt_unpin(from->engine[RCS].state);
  693. i915_gem_context_put(from);
  694. }
  695. engine->last_context = i915_gem_context_get(to);
  696. /* GEN8 does *not* require an explicit reload if the PDPs have been
  697. * setup, and we do not wish to move them.
  698. */
  699. if (needs_pd_load_post(ppgtt, to, hw_flags)) {
  700. trace_switch_mm(engine, to);
  701. ret = ppgtt->switch_mm(ppgtt, req);
  702. /* The hardware context switch is emitted, but we haven't
  703. * actually changed the state - so it's probably safe to bail
  704. * here. Still, let the user know something dangerous has
  705. * happened.
  706. */
  707. if (ret)
  708. return ret;
  709. }
  710. if (ppgtt)
  711. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  712. for (i = 0; i < MAX_L3_SLICES; i++) {
  713. if (!(to->remap_slice & (1<<i)))
  714. continue;
  715. ret = remap_l3(req, i);
  716. if (ret)
  717. return ret;
  718. to->remap_slice &= ~(1<<i);
  719. }
  720. if (!to->engine[RCS].initialised) {
  721. if (engine->init_context) {
  722. ret = engine->init_context(req);
  723. if (ret)
  724. return ret;
  725. }
  726. to->engine[RCS].initialised = true;
  727. }
  728. return 0;
  729. unpin_out:
  730. i915_gem_object_ggtt_unpin(to->engine[RCS].state);
  731. return ret;
  732. }
  733. /**
  734. * i915_switch_context() - perform a GPU context switch.
  735. * @req: request for which we'll execute the context switch
  736. *
  737. * The context life cycle is simple. The context refcount is incremented and
  738. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  739. * it will have a refcount > 1. This allows us to destroy the context abstract
  740. * object while letting the normal object tracking destroy the backing BO.
  741. *
  742. * This function should not be used in execlists mode. Instead the context is
  743. * switched by writing to the ELSP and requests keep a reference to their
  744. * context.
  745. */
  746. int i915_switch_context(struct drm_i915_gem_request *req)
  747. {
  748. struct intel_engine_cs *engine = req->engine;
  749. WARN_ON(i915.enable_execlists);
  750. lockdep_assert_held(&req->i915->drm.struct_mutex);
  751. if (!req->ctx->engine[engine->id].state) {
  752. struct i915_gem_context *to = req->ctx;
  753. struct i915_hw_ppgtt *ppgtt =
  754. to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  755. if (needs_pd_load_pre(ppgtt, engine, to)) {
  756. int ret;
  757. trace_switch_mm(engine, to);
  758. ret = ppgtt->switch_mm(ppgtt, req);
  759. if (ret)
  760. return ret;
  761. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  762. }
  763. if (to != engine->last_context) {
  764. if (engine->last_context)
  765. i915_gem_context_put(engine->last_context);
  766. engine->last_context = i915_gem_context_get(to);
  767. }
  768. return 0;
  769. }
  770. return do_rcs_switch(req);
  771. }
  772. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv)
  773. {
  774. struct intel_engine_cs *engine;
  775. for_each_engine(engine, dev_priv) {
  776. struct drm_i915_gem_request *req;
  777. int ret;
  778. if (engine->last_context == NULL)
  779. continue;
  780. if (engine->last_context == dev_priv->kernel_context)
  781. continue;
  782. req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
  783. if (IS_ERR(req))
  784. return PTR_ERR(req);
  785. ret = 0;
  786. if (!i915.enable_execlists)
  787. ret = i915_switch_context(req);
  788. i915_add_request_no_flush(req);
  789. if (ret)
  790. return ret;
  791. }
  792. return 0;
  793. }
  794. static bool contexts_enabled(struct drm_device *dev)
  795. {
  796. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  797. }
  798. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  799. struct drm_file *file)
  800. {
  801. struct drm_i915_gem_context_create *args = data;
  802. struct drm_i915_file_private *file_priv = file->driver_priv;
  803. struct i915_gem_context *ctx;
  804. int ret;
  805. if (!contexts_enabled(dev))
  806. return -ENODEV;
  807. if (args->pad != 0)
  808. return -EINVAL;
  809. ret = i915_mutex_lock_interruptible(dev);
  810. if (ret)
  811. return ret;
  812. ctx = i915_gem_create_context(dev, file_priv);
  813. mutex_unlock(&dev->struct_mutex);
  814. if (IS_ERR(ctx))
  815. return PTR_ERR(ctx);
  816. args->ctx_id = ctx->user_handle;
  817. DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id);
  818. return 0;
  819. }
  820. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  821. struct drm_file *file)
  822. {
  823. struct drm_i915_gem_context_destroy *args = data;
  824. struct drm_i915_file_private *file_priv = file->driver_priv;
  825. struct i915_gem_context *ctx;
  826. int ret;
  827. if (args->pad != 0)
  828. return -EINVAL;
  829. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  830. return -ENOENT;
  831. ret = i915_mutex_lock_interruptible(dev);
  832. if (ret)
  833. return ret;
  834. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  835. if (IS_ERR(ctx)) {
  836. mutex_unlock(&dev->struct_mutex);
  837. return PTR_ERR(ctx);
  838. }
  839. idr_remove(&file_priv->context_idr, ctx->user_handle);
  840. i915_gem_context_put(ctx);
  841. mutex_unlock(&dev->struct_mutex);
  842. DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id);
  843. return 0;
  844. }
  845. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  846. struct drm_file *file)
  847. {
  848. struct drm_i915_file_private *file_priv = file->driver_priv;
  849. struct drm_i915_gem_context_param *args = data;
  850. struct i915_gem_context *ctx;
  851. int ret;
  852. ret = i915_mutex_lock_interruptible(dev);
  853. if (ret)
  854. return ret;
  855. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  856. if (IS_ERR(ctx)) {
  857. mutex_unlock(&dev->struct_mutex);
  858. return PTR_ERR(ctx);
  859. }
  860. args->size = 0;
  861. switch (args->param) {
  862. case I915_CONTEXT_PARAM_BAN_PERIOD:
  863. args->value = ctx->hang_stats.ban_period_seconds;
  864. break;
  865. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  866. args->value = ctx->flags & CONTEXT_NO_ZEROMAP;
  867. break;
  868. case I915_CONTEXT_PARAM_GTT_SIZE:
  869. if (ctx->ppgtt)
  870. args->value = ctx->ppgtt->base.total;
  871. else if (to_i915(dev)->mm.aliasing_ppgtt)
  872. args->value = to_i915(dev)->mm.aliasing_ppgtt->base.total;
  873. else
  874. args->value = to_i915(dev)->ggtt.base.total;
  875. break;
  876. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  877. args->value = !!(ctx->flags & CONTEXT_NO_ERROR_CAPTURE);
  878. break;
  879. default:
  880. ret = -EINVAL;
  881. break;
  882. }
  883. mutex_unlock(&dev->struct_mutex);
  884. return ret;
  885. }
  886. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  887. struct drm_file *file)
  888. {
  889. struct drm_i915_file_private *file_priv = file->driver_priv;
  890. struct drm_i915_gem_context_param *args = data;
  891. struct i915_gem_context *ctx;
  892. int ret;
  893. ret = i915_mutex_lock_interruptible(dev);
  894. if (ret)
  895. return ret;
  896. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  897. if (IS_ERR(ctx)) {
  898. mutex_unlock(&dev->struct_mutex);
  899. return PTR_ERR(ctx);
  900. }
  901. switch (args->param) {
  902. case I915_CONTEXT_PARAM_BAN_PERIOD:
  903. if (args->size)
  904. ret = -EINVAL;
  905. else if (args->value < ctx->hang_stats.ban_period_seconds &&
  906. !capable(CAP_SYS_ADMIN))
  907. ret = -EPERM;
  908. else
  909. ctx->hang_stats.ban_period_seconds = args->value;
  910. break;
  911. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  912. if (args->size) {
  913. ret = -EINVAL;
  914. } else {
  915. ctx->flags &= ~CONTEXT_NO_ZEROMAP;
  916. ctx->flags |= args->value ? CONTEXT_NO_ZEROMAP : 0;
  917. }
  918. break;
  919. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  920. if (args->size) {
  921. ret = -EINVAL;
  922. } else {
  923. if (args->value)
  924. ctx->flags |= CONTEXT_NO_ERROR_CAPTURE;
  925. else
  926. ctx->flags &= ~CONTEXT_NO_ERROR_CAPTURE;
  927. }
  928. break;
  929. default:
  930. ret = -EINVAL;
  931. break;
  932. }
  933. mutex_unlock(&dev->struct_mutex);
  934. return ret;
  935. }
  936. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
  937. void *data, struct drm_file *file)
  938. {
  939. struct drm_i915_private *dev_priv = to_i915(dev);
  940. struct drm_i915_reset_stats *args = data;
  941. struct i915_ctx_hang_stats *hs;
  942. struct i915_gem_context *ctx;
  943. int ret;
  944. if (args->flags || args->pad)
  945. return -EINVAL;
  946. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE && !capable(CAP_SYS_ADMIN))
  947. return -EPERM;
  948. ret = i915_mutex_lock_interruptible(dev);
  949. if (ret)
  950. return ret;
  951. ctx = i915_gem_context_lookup(file->driver_priv, args->ctx_id);
  952. if (IS_ERR(ctx)) {
  953. mutex_unlock(&dev->struct_mutex);
  954. return PTR_ERR(ctx);
  955. }
  956. hs = &ctx->hang_stats;
  957. if (capable(CAP_SYS_ADMIN))
  958. args->reset_count = i915_reset_count(&dev_priv->gpu_error);
  959. else
  960. args->reset_count = 0;
  961. args->batch_active = hs->batch_active;
  962. args->batch_pending = hs->batch_pending;
  963. mutex_unlock(&dev->struct_mutex);
  964. return 0;
  965. }