i915_gem_context.c 31 KB

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