i915_gem_context.c 30 KB

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