i915_gem_context.c 32 KB

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