i915_gem_context.c 31 KB

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