i915_gem_context.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 I915_GTT_MIN_ALIGNMENT
  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(!i915_gem_context_is_closed(ctx));
  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_gem_object_release_unless_active(ce->state->obj);
  143. }
  144. kfree(ctx->name);
  145. put_pid(ctx->pid);
  146. list_del(&ctx->link);
  147. ida_simple_remove(&ctx->i915->context_hw_ida, ctx->hw_id);
  148. kfree(ctx);
  149. }
  150. static struct drm_i915_gem_object *
  151. alloc_context_obj(struct drm_i915_private *dev_priv, u64 size)
  152. {
  153. struct drm_i915_gem_object *obj;
  154. int ret;
  155. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  156. obj = i915_gem_object_create(dev_priv, size);
  157. if (IS_ERR(obj))
  158. return obj;
  159. /*
  160. * Try to make the context utilize L3 as well as LLC.
  161. *
  162. * On VLV we don't have L3 controls in the PTEs so we
  163. * shouldn't touch the cache level, especially as that
  164. * would make the object snooped which might have a
  165. * negative performance impact.
  166. *
  167. * Snooping is required on non-llc platforms in execlist
  168. * mode, but since all GGTT accesses use PAT entry 0 we
  169. * get snooping anyway regardless of cache_level.
  170. *
  171. * This is only applicable for Ivy Bridge devices since
  172. * later platforms don't have L3 control bits in the PTE.
  173. */
  174. if (IS_IVYBRIDGE(dev_priv)) {
  175. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  176. /* Failure shouldn't ever happen this early */
  177. if (WARN_ON(ret)) {
  178. i915_gem_object_put(obj);
  179. return ERR_PTR(ret);
  180. }
  181. }
  182. return obj;
  183. }
  184. static void context_close(struct i915_gem_context *ctx)
  185. {
  186. i915_gem_context_set_closed(ctx);
  187. if (ctx->ppgtt)
  188. i915_ppgtt_close(&ctx->ppgtt->base);
  189. ctx->file_priv = ERR_PTR(-EBADF);
  190. i915_gem_context_put(ctx);
  191. }
  192. static int assign_hw_id(struct drm_i915_private *dev_priv, unsigned *out)
  193. {
  194. int ret;
  195. ret = ida_simple_get(&dev_priv->context_hw_ida,
  196. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  197. if (ret < 0) {
  198. /* Contexts are only released when no longer active.
  199. * Flush any pending retires to hopefully release some
  200. * stale contexts and try again.
  201. */
  202. i915_gem_retire_requests(dev_priv);
  203. ret = ida_simple_get(&dev_priv->context_hw_ida,
  204. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  205. if (ret < 0)
  206. return ret;
  207. }
  208. *out = ret;
  209. return 0;
  210. }
  211. static struct i915_gem_context *
  212. __create_hw_context(struct drm_i915_private *dev_priv,
  213. struct drm_i915_file_private *file_priv)
  214. {
  215. struct i915_gem_context *ctx;
  216. int ret;
  217. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  218. if (ctx == NULL)
  219. return ERR_PTR(-ENOMEM);
  220. ret = assign_hw_id(dev_priv, &ctx->hw_id);
  221. if (ret) {
  222. kfree(ctx);
  223. return ERR_PTR(ret);
  224. }
  225. kref_init(&ctx->ref);
  226. list_add_tail(&ctx->link, &dev_priv->context_list);
  227. ctx->i915 = dev_priv;
  228. ctx->ggtt_alignment = get_context_alignment(dev_priv);
  229. if (dev_priv->hw_context_size) {
  230. struct drm_i915_gem_object *obj;
  231. struct i915_vma *vma;
  232. obj = alloc_context_obj(dev_priv, dev_priv->hw_context_size);
  233. if (IS_ERR(obj)) {
  234. ret = PTR_ERR(obj);
  235. goto err_out;
  236. }
  237. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  238. if (IS_ERR(vma)) {
  239. i915_gem_object_put(obj);
  240. ret = PTR_ERR(vma);
  241. goto err_out;
  242. }
  243. ctx->engine[RCS].state = vma;
  244. }
  245. /* Default context will never have a file_priv */
  246. ret = DEFAULT_CONTEXT_HANDLE;
  247. if (file_priv) {
  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. }
  253. ctx->user_handle = ret;
  254. ctx->file_priv = file_priv;
  255. if (file_priv) {
  256. ctx->pid = get_task_pid(current, PIDTYPE_PID);
  257. ctx->name = kasprintf(GFP_KERNEL, "%s[%d]/%x",
  258. current->comm,
  259. pid_nr(ctx->pid),
  260. ctx->user_handle);
  261. if (!ctx->name) {
  262. ret = -ENOMEM;
  263. goto err_pid;
  264. }
  265. }
  266. /* NB: Mark all slices as needing a remap so that when the context first
  267. * loads it will restore whatever remap state already exists. If there
  268. * is no remap info, it will be a NOP. */
  269. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  270. i915_gem_context_set_bannable(ctx);
  271. ctx->ring_size = 4 * PAGE_SIZE;
  272. ctx->desc_template = GEN8_CTX_ADDRESSING_MODE(dev_priv) <<
  273. GEN8_CTX_ADDRESSING_MODE_SHIFT;
  274. /* GuC requires the ring to be placed above GUC_WOPCM_TOP. If GuC is not
  275. * present or not in use we still need a small bias as ring wraparound
  276. * at offset 0 sometimes hangs. No idea why.
  277. */
  278. if (HAS_GUC(dev_priv) && i915.enable_guc_loading)
  279. ctx->ggtt_offset_bias = GUC_WOPCM_TOP;
  280. else
  281. ctx->ggtt_offset_bias = I915_GTT_PAGE_SIZE;
  282. return ctx;
  283. err_pid:
  284. put_pid(ctx->pid);
  285. idr_remove(&file_priv->context_idr, ctx->user_handle);
  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_i915_private *dev_priv,
  297. struct drm_i915_file_private *file_priv)
  298. {
  299. struct i915_gem_context *ctx;
  300. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  301. ctx = __create_hw_context(dev_priv, file_priv);
  302. if (IS_ERR(ctx))
  303. return ctx;
  304. if (USES_FULL_PPGTT(dev_priv)) {
  305. struct i915_hw_ppgtt *ppgtt;
  306. ppgtt = i915_ppgtt_create(dev_priv, file_priv, ctx->name);
  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 = __create_hw_context(to_i915(dev), NULL);
  340. if (IS_ERR(ctx))
  341. goto out;
  342. ctx->file_priv = ERR_PTR(-EBADF);
  343. i915_gem_context_set_closed(ctx); /* not user accessible */
  344. i915_gem_context_clear_bannable(ctx);
  345. i915_gem_context_set_force_single_submission(ctx);
  346. ctx->ring_size = 512 * PAGE_SIZE; /* Max ring buffer size */
  347. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  348. out:
  349. mutex_unlock(&dev->struct_mutex);
  350. return ctx;
  351. }
  352. int i915_gem_context_init(struct drm_i915_private *dev_priv)
  353. {
  354. struct i915_gem_context *ctx;
  355. /* Init should only be called once per module load. Eventually the
  356. * restriction on the context_disabled check can be loosened. */
  357. if (WARN_ON(dev_priv->kernel_context))
  358. return 0;
  359. if (intel_vgpu_active(dev_priv) &&
  360. HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  361. if (!i915.enable_execlists) {
  362. DRM_INFO("Only EXECLIST mode is supported in vgpu.\n");
  363. return -EINVAL;
  364. }
  365. }
  366. /* Using the simple ida interface, the max is limited by sizeof(int) */
  367. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
  368. ida_init(&dev_priv->context_hw_ida);
  369. if (i915.enable_execlists) {
  370. /* NB: intentionally left blank. We will allocate our own
  371. * backing objects as we need them, thank you very much */
  372. dev_priv->hw_context_size = 0;
  373. } else if (HAS_HW_CONTEXTS(dev_priv)) {
  374. dev_priv->hw_context_size =
  375. round_up(get_context_size(dev_priv),
  376. I915_GTT_PAGE_SIZE);
  377. if (dev_priv->hw_context_size > (1<<20)) {
  378. DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n",
  379. dev_priv->hw_context_size);
  380. dev_priv->hw_context_size = 0;
  381. }
  382. }
  383. ctx = i915_gem_create_context(dev_priv, NULL);
  384. if (IS_ERR(ctx)) {
  385. DRM_ERROR("Failed to create default global context (error %ld)\n",
  386. PTR_ERR(ctx));
  387. return PTR_ERR(ctx);
  388. }
  389. i915_gem_context_clear_bannable(ctx);
  390. ctx->priority = I915_PRIORITY_MIN; /* lowest priority; idle task */
  391. dev_priv->kernel_context = ctx;
  392. GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
  393. DRM_DEBUG_DRIVER("%s context support initialized\n",
  394. i915.enable_execlists ? "LR" :
  395. dev_priv->hw_context_size ? "HW" : "fake");
  396. return 0;
  397. }
  398. void i915_gem_context_lost(struct drm_i915_private *dev_priv)
  399. {
  400. struct intel_engine_cs *engine;
  401. enum intel_engine_id id;
  402. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  403. for_each_engine(engine, dev_priv, id) {
  404. engine->legacy_active_context = NULL;
  405. if (!engine->last_retired_context)
  406. continue;
  407. engine->context_unpin(engine, engine->last_retired_context);
  408. engine->last_retired_context = NULL;
  409. }
  410. /* Force the GPU state to be restored on enabling */
  411. if (!i915.enable_execlists) {
  412. struct i915_gem_context *ctx;
  413. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  414. if (!i915_gem_context_is_default(ctx))
  415. continue;
  416. for_each_engine(engine, dev_priv, id)
  417. ctx->engine[engine->id].initialised = false;
  418. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  419. }
  420. for_each_engine(engine, dev_priv, id) {
  421. struct intel_context *kce =
  422. &dev_priv->kernel_context->engine[engine->id];
  423. kce->initialised = true;
  424. }
  425. }
  426. }
  427. void i915_gem_context_fini(struct drm_i915_private *dev_priv)
  428. {
  429. struct i915_gem_context *dctx = dev_priv->kernel_context;
  430. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  431. GEM_BUG_ON(!i915_gem_context_is_kernel(dctx));
  432. context_close(dctx);
  433. dev_priv->kernel_context = NULL;
  434. ida_destroy(&dev_priv->context_hw_ida);
  435. }
  436. static int context_idr_cleanup(int id, void *p, void *data)
  437. {
  438. struct i915_gem_context *ctx = p;
  439. context_close(ctx);
  440. return 0;
  441. }
  442. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  443. {
  444. struct drm_i915_file_private *file_priv = file->driver_priv;
  445. struct i915_gem_context *ctx;
  446. idr_init(&file_priv->context_idr);
  447. mutex_lock(&dev->struct_mutex);
  448. ctx = i915_gem_create_context(to_i915(dev), file_priv);
  449. mutex_unlock(&dev->struct_mutex);
  450. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  451. if (IS_ERR(ctx)) {
  452. idr_destroy(&file_priv->context_idr);
  453. return PTR_ERR(ctx);
  454. }
  455. return 0;
  456. }
  457. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  458. {
  459. struct drm_i915_file_private *file_priv = file->driver_priv;
  460. lockdep_assert_held(&dev->struct_mutex);
  461. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  462. idr_destroy(&file_priv->context_idr);
  463. }
  464. static inline int
  465. mi_set_context(struct drm_i915_gem_request *req, u32 hw_flags)
  466. {
  467. struct drm_i915_private *dev_priv = req->i915;
  468. struct intel_ring *ring = req->ring;
  469. struct intel_engine_cs *engine = req->engine;
  470. enum intel_engine_id id;
  471. u32 flags = hw_flags | MI_MM_SPACE_GTT;
  472. const int num_rings =
  473. /* Use an extended w/a on ivb+ if signalling from other rings */
  474. i915.semaphores ?
  475. INTEL_INFO(dev_priv)->num_rings - 1 :
  476. 0;
  477. int len, ret;
  478. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  479. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  480. * explicitly, so we rely on the value at ring init, stored in
  481. * itlb_before_ctx_switch.
  482. */
  483. if (IS_GEN6(dev_priv)) {
  484. ret = engine->emit_flush(req, EMIT_INVALIDATE);
  485. if (ret)
  486. return ret;
  487. }
  488. /* These flags are for resource streamer on HSW+ */
  489. if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8)
  490. flags |= (HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN);
  491. else if (INTEL_GEN(dev_priv) < 8)
  492. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  493. len = 4;
  494. if (INTEL_GEN(dev_priv) >= 7)
  495. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  496. ret = intel_ring_begin(req, len);
  497. if (ret)
  498. return ret;
  499. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  500. if (INTEL_GEN(dev_priv) >= 7) {
  501. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  502. if (num_rings) {
  503. struct intel_engine_cs *signaller;
  504. intel_ring_emit(ring,
  505. MI_LOAD_REGISTER_IMM(num_rings));
  506. for_each_engine(signaller, dev_priv, id) {
  507. if (signaller == engine)
  508. continue;
  509. intel_ring_emit_reg(ring,
  510. RING_PSMI_CTL(signaller->mmio_base));
  511. intel_ring_emit(ring,
  512. _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  513. }
  514. }
  515. }
  516. intel_ring_emit(ring, MI_NOOP);
  517. intel_ring_emit(ring, MI_SET_CONTEXT);
  518. intel_ring_emit(ring,
  519. i915_ggtt_offset(req->ctx->engine[RCS].state) | flags);
  520. /*
  521. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  522. * WaMiSetContext_Hang:snb,ivb,vlv
  523. */
  524. intel_ring_emit(ring, MI_NOOP);
  525. if (INTEL_GEN(dev_priv) >= 7) {
  526. if (num_rings) {
  527. struct intel_engine_cs *signaller;
  528. i915_reg_t last_reg = {}; /* keep gcc quiet */
  529. intel_ring_emit(ring,
  530. MI_LOAD_REGISTER_IMM(num_rings));
  531. for_each_engine(signaller, dev_priv, id) {
  532. if (signaller == engine)
  533. continue;
  534. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  535. intel_ring_emit_reg(ring, last_reg);
  536. intel_ring_emit(ring,
  537. _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  538. }
  539. /* Insert a delay before the next switch! */
  540. intel_ring_emit(ring,
  541. MI_STORE_REGISTER_MEM |
  542. MI_SRM_LRM_GLOBAL_GTT);
  543. intel_ring_emit_reg(ring, last_reg);
  544. intel_ring_emit(ring,
  545. i915_ggtt_offset(engine->scratch));
  546. intel_ring_emit(ring, MI_NOOP);
  547. }
  548. intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  549. }
  550. intel_ring_advance(ring);
  551. return ret;
  552. }
  553. static int remap_l3(struct drm_i915_gem_request *req, int slice)
  554. {
  555. u32 *remap_info = req->i915->l3_parity.remap_info[slice];
  556. struct intel_ring *ring = req->ring;
  557. int i, ret;
  558. if (!remap_info)
  559. return 0;
  560. ret = intel_ring_begin(req, GEN7_L3LOG_SIZE/4 * 2 + 2);
  561. if (ret)
  562. return ret;
  563. /*
  564. * Note: We do not worry about the concurrent register cacheline hang
  565. * here because no other code should access these registers other than
  566. * at initialization time.
  567. */
  568. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4));
  569. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  570. intel_ring_emit_reg(ring, GEN7_L3LOG(slice, i));
  571. intel_ring_emit(ring, remap_info[i]);
  572. }
  573. intel_ring_emit(ring, MI_NOOP);
  574. intel_ring_advance(ring);
  575. return 0;
  576. }
  577. static inline bool skip_rcs_switch(struct i915_hw_ppgtt *ppgtt,
  578. struct intel_engine_cs *engine,
  579. struct i915_gem_context *to)
  580. {
  581. if (to->remap_slice)
  582. return false;
  583. if (!to->engine[RCS].initialised)
  584. return false;
  585. if (ppgtt && (intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  586. return false;
  587. return to == engine->legacy_active_context;
  588. }
  589. static bool
  590. needs_pd_load_pre(struct i915_hw_ppgtt *ppgtt,
  591. struct intel_engine_cs *engine,
  592. struct i915_gem_context *to)
  593. {
  594. if (!ppgtt)
  595. return false;
  596. /* Always load the ppgtt on first use */
  597. if (!engine->legacy_active_context)
  598. return true;
  599. /* Same context without new entries, skip */
  600. if (engine->legacy_active_context == to &&
  601. !(intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  602. return false;
  603. if (engine->id != RCS)
  604. return true;
  605. if (INTEL_GEN(engine->i915) < 8)
  606. return true;
  607. return false;
  608. }
  609. static bool
  610. needs_pd_load_post(struct i915_hw_ppgtt *ppgtt,
  611. struct i915_gem_context *to,
  612. u32 hw_flags)
  613. {
  614. if (!ppgtt)
  615. return false;
  616. if (!IS_GEN8(to->i915))
  617. return false;
  618. if (hw_flags & MI_RESTORE_INHIBIT)
  619. return true;
  620. return false;
  621. }
  622. static int do_rcs_switch(struct drm_i915_gem_request *req)
  623. {
  624. struct i915_gem_context *to = req->ctx;
  625. struct intel_engine_cs *engine = req->engine;
  626. struct i915_hw_ppgtt *ppgtt = to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  627. struct i915_gem_context *from = engine->legacy_active_context;
  628. u32 hw_flags;
  629. int ret, i;
  630. GEM_BUG_ON(engine->id != RCS);
  631. if (skip_rcs_switch(ppgtt, engine, to))
  632. return 0;
  633. if (needs_pd_load_pre(ppgtt, engine, to)) {
  634. /* Older GENs and non render rings still want the load first,
  635. * "PP_DCLV followed by PP_DIR_BASE register through Load
  636. * Register Immediate commands in Ring Buffer before submitting
  637. * a context."*/
  638. trace_switch_mm(engine, to);
  639. ret = ppgtt->switch_mm(ppgtt, req);
  640. if (ret)
  641. return ret;
  642. }
  643. if (!to->engine[RCS].initialised || i915_gem_context_is_default(to))
  644. /* NB: If we inhibit the restore, the context is not allowed to
  645. * die because future work may end up depending on valid address
  646. * space. This means we must enforce that a page table load
  647. * occur when this occurs. */
  648. hw_flags = MI_RESTORE_INHIBIT;
  649. else if (ppgtt && intel_engine_flag(engine) & ppgtt->pd_dirty_rings)
  650. hw_flags = MI_FORCE_RESTORE;
  651. else
  652. hw_flags = 0;
  653. if (to != from || (hw_flags & MI_FORCE_RESTORE)) {
  654. ret = mi_set_context(req, hw_flags);
  655. if (ret)
  656. return ret;
  657. engine->legacy_active_context = to;
  658. }
  659. /* GEN8 does *not* require an explicit reload if the PDPs have been
  660. * setup, and we do not wish to move them.
  661. */
  662. if (needs_pd_load_post(ppgtt, to, hw_flags)) {
  663. trace_switch_mm(engine, to);
  664. ret = ppgtt->switch_mm(ppgtt, req);
  665. /* The hardware context switch is emitted, but we haven't
  666. * actually changed the state - so it's probably safe to bail
  667. * here. Still, let the user know something dangerous has
  668. * happened.
  669. */
  670. if (ret)
  671. return ret;
  672. }
  673. if (ppgtt)
  674. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  675. for (i = 0; i < MAX_L3_SLICES; i++) {
  676. if (!(to->remap_slice & (1<<i)))
  677. continue;
  678. ret = remap_l3(req, i);
  679. if (ret)
  680. return ret;
  681. to->remap_slice &= ~(1<<i);
  682. }
  683. if (!to->engine[RCS].initialised) {
  684. if (engine->init_context) {
  685. ret = engine->init_context(req);
  686. if (ret)
  687. return ret;
  688. }
  689. to->engine[RCS].initialised = true;
  690. }
  691. return 0;
  692. }
  693. /**
  694. * i915_switch_context() - perform a GPU context switch.
  695. * @req: request for which we'll execute the context switch
  696. *
  697. * The context life cycle is simple. The context refcount is incremented and
  698. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  699. * it will have a refcount > 1. This allows us to destroy the context abstract
  700. * object while letting the normal object tracking destroy the backing BO.
  701. *
  702. * This function should not be used in execlists mode. Instead the context is
  703. * switched by writing to the ELSP and requests keep a reference to their
  704. * context.
  705. */
  706. int i915_switch_context(struct drm_i915_gem_request *req)
  707. {
  708. struct intel_engine_cs *engine = req->engine;
  709. lockdep_assert_held(&req->i915->drm.struct_mutex);
  710. if (i915.enable_execlists)
  711. return 0;
  712. if (!req->ctx->engine[engine->id].state) {
  713. struct i915_gem_context *to = req->ctx;
  714. struct i915_hw_ppgtt *ppgtt =
  715. to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  716. if (needs_pd_load_pre(ppgtt, engine, to)) {
  717. int ret;
  718. trace_switch_mm(engine, to);
  719. ret = ppgtt->switch_mm(ppgtt, req);
  720. if (ret)
  721. return ret;
  722. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  723. }
  724. return 0;
  725. }
  726. return do_rcs_switch(req);
  727. }
  728. static bool engine_has_kernel_context(struct intel_engine_cs *engine)
  729. {
  730. struct i915_gem_timeline *timeline;
  731. list_for_each_entry(timeline, &engine->i915->gt.timelines, link) {
  732. struct intel_timeline *tl;
  733. if (timeline == &engine->i915->gt.global_timeline)
  734. continue;
  735. tl = &timeline->engine[engine->id];
  736. if (i915_gem_active_peek(&tl->last_request,
  737. &engine->i915->drm.struct_mutex))
  738. return false;
  739. }
  740. return (!engine->last_retired_context ||
  741. i915_gem_context_is_kernel(engine->last_retired_context));
  742. }
  743. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv)
  744. {
  745. struct intel_engine_cs *engine;
  746. struct i915_gem_timeline *timeline;
  747. enum intel_engine_id id;
  748. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  749. i915_gem_retire_requests(dev_priv);
  750. for_each_engine(engine, dev_priv, id) {
  751. struct drm_i915_gem_request *req;
  752. int ret;
  753. if (engine_has_kernel_context(engine))
  754. continue;
  755. req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
  756. if (IS_ERR(req))
  757. return PTR_ERR(req);
  758. /* Queue this switch after all other activity */
  759. list_for_each_entry(timeline, &dev_priv->gt.timelines, link) {
  760. struct drm_i915_gem_request *prev;
  761. struct intel_timeline *tl;
  762. tl = &timeline->engine[engine->id];
  763. prev = i915_gem_active_raw(&tl->last_request,
  764. &dev_priv->drm.struct_mutex);
  765. if (prev)
  766. i915_sw_fence_await_sw_fence_gfp(&req->submit,
  767. &prev->submit,
  768. GFP_KERNEL);
  769. }
  770. ret = i915_switch_context(req);
  771. i915_add_request_no_flush(req);
  772. if (ret)
  773. return ret;
  774. }
  775. return 0;
  776. }
  777. static bool contexts_enabled(struct drm_device *dev)
  778. {
  779. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  780. }
  781. static bool client_is_banned(struct drm_i915_file_private *file_priv)
  782. {
  783. return file_priv->context_bans > I915_MAX_CLIENT_CONTEXT_BANS;
  784. }
  785. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  786. struct drm_file *file)
  787. {
  788. struct drm_i915_gem_context_create *args = data;
  789. struct drm_i915_file_private *file_priv = file->driver_priv;
  790. struct i915_gem_context *ctx;
  791. int ret;
  792. if (!contexts_enabled(dev))
  793. return -ENODEV;
  794. if (args->pad != 0)
  795. return -EINVAL;
  796. if (client_is_banned(file_priv)) {
  797. DRM_DEBUG("client %s[%d] banned from creating ctx\n",
  798. current->comm,
  799. pid_nr(get_task_pid(current, PIDTYPE_PID)));
  800. return -EIO;
  801. }
  802. ret = i915_mutex_lock_interruptible(dev);
  803. if (ret)
  804. return ret;
  805. ctx = i915_gem_create_context(to_i915(dev), file_priv);
  806. mutex_unlock(&dev->struct_mutex);
  807. if (IS_ERR(ctx))
  808. return PTR_ERR(ctx);
  809. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  810. args->ctx_id = ctx->user_handle;
  811. DRM_DEBUG("HW context %d created\n", args->ctx_id);
  812. return 0;
  813. }
  814. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  815. struct drm_file *file)
  816. {
  817. struct drm_i915_gem_context_destroy *args = data;
  818. struct drm_i915_file_private *file_priv = file->driver_priv;
  819. struct i915_gem_context *ctx;
  820. int ret;
  821. if (args->pad != 0)
  822. return -EINVAL;
  823. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  824. return -ENOENT;
  825. ret = i915_mutex_lock_interruptible(dev);
  826. if (ret)
  827. return ret;
  828. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  829. if (IS_ERR(ctx)) {
  830. mutex_unlock(&dev->struct_mutex);
  831. return PTR_ERR(ctx);
  832. }
  833. idr_remove(&file_priv->context_idr, ctx->user_handle);
  834. context_close(ctx);
  835. mutex_unlock(&dev->struct_mutex);
  836. DRM_DEBUG("HW context %d destroyed\n", args->ctx_id);
  837. return 0;
  838. }
  839. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  840. struct drm_file *file)
  841. {
  842. struct drm_i915_file_private *file_priv = file->driver_priv;
  843. struct drm_i915_gem_context_param *args = data;
  844. struct i915_gem_context *ctx;
  845. int ret;
  846. ret = i915_mutex_lock_interruptible(dev);
  847. if (ret)
  848. return ret;
  849. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  850. if (IS_ERR(ctx)) {
  851. mutex_unlock(&dev->struct_mutex);
  852. return PTR_ERR(ctx);
  853. }
  854. args->size = 0;
  855. switch (args->param) {
  856. case I915_CONTEXT_PARAM_BAN_PERIOD:
  857. ret = -EINVAL;
  858. break;
  859. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  860. args->value = ctx->flags & CONTEXT_NO_ZEROMAP;
  861. break;
  862. case I915_CONTEXT_PARAM_GTT_SIZE:
  863. if (ctx->ppgtt)
  864. args->value = ctx->ppgtt->base.total;
  865. else if (to_i915(dev)->mm.aliasing_ppgtt)
  866. args->value = to_i915(dev)->mm.aliasing_ppgtt->base.total;
  867. else
  868. args->value = to_i915(dev)->ggtt.base.total;
  869. break;
  870. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  871. args->value = i915_gem_context_no_error_capture(ctx);
  872. break;
  873. case I915_CONTEXT_PARAM_BANNABLE:
  874. args->value = i915_gem_context_is_bannable(ctx);
  875. break;
  876. default:
  877. ret = -EINVAL;
  878. break;
  879. }
  880. mutex_unlock(&dev->struct_mutex);
  881. return ret;
  882. }
  883. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  884. struct drm_file *file)
  885. {
  886. struct drm_i915_file_private *file_priv = file->driver_priv;
  887. struct drm_i915_gem_context_param *args = data;
  888. struct i915_gem_context *ctx;
  889. int ret;
  890. ret = i915_mutex_lock_interruptible(dev);
  891. if (ret)
  892. return ret;
  893. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  894. if (IS_ERR(ctx)) {
  895. mutex_unlock(&dev->struct_mutex);
  896. return PTR_ERR(ctx);
  897. }
  898. switch (args->param) {
  899. case I915_CONTEXT_PARAM_BAN_PERIOD:
  900. ret = -EINVAL;
  901. break;
  902. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  903. if (args->size) {
  904. ret = -EINVAL;
  905. } else {
  906. ctx->flags &= ~CONTEXT_NO_ZEROMAP;
  907. ctx->flags |= args->value ? CONTEXT_NO_ZEROMAP : 0;
  908. }
  909. break;
  910. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  911. if (args->size)
  912. ret = -EINVAL;
  913. else if (args->value)
  914. i915_gem_context_set_no_error_capture(ctx);
  915. else
  916. i915_gem_context_clear_no_error_capture(ctx);
  917. break;
  918. case I915_CONTEXT_PARAM_BANNABLE:
  919. if (args->size)
  920. ret = -EINVAL;
  921. else if (!capable(CAP_SYS_ADMIN) && !args->value)
  922. ret = -EPERM;
  923. else if (args->value)
  924. i915_gem_context_set_bannable(ctx);
  925. else
  926. i915_gem_context_clear_bannable(ctx);
  927. break;
  928. default:
  929. ret = -EINVAL;
  930. break;
  931. }
  932. mutex_unlock(&dev->struct_mutex);
  933. return ret;
  934. }
  935. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
  936. void *data, struct drm_file *file)
  937. {
  938. struct drm_i915_private *dev_priv = to_i915(dev);
  939. struct drm_i915_reset_stats *args = data;
  940. struct i915_gem_context *ctx;
  941. int ret;
  942. if (args->flags || args->pad)
  943. return -EINVAL;
  944. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE && !capable(CAP_SYS_ADMIN))
  945. return -EPERM;
  946. ret = i915_mutex_lock_interruptible(dev);
  947. if (ret)
  948. return ret;
  949. ctx = i915_gem_context_lookup(file->driver_priv, args->ctx_id);
  950. if (IS_ERR(ctx)) {
  951. mutex_unlock(&dev->struct_mutex);
  952. return PTR_ERR(ctx);
  953. }
  954. if (capable(CAP_SYS_ADMIN))
  955. args->reset_count = i915_reset_count(&dev_priv->gpu_error);
  956. else
  957. args->reset_count = 0;
  958. args->batch_active = ctx->guilty_count;
  959. args->batch_pending = ctx->active_count;
  960. mutex_unlock(&dev->struct_mutex);
  961. return 0;
  962. }