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