i915_gem_context.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Copyright © 2011-2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Ben Widawsky <ben@bwidawsk.net>
  25. *
  26. */
  27. /*
  28. * This file implements HW context support. On gen5+ a HW context consists of an
  29. * opaque GPU object which is referenced at times of context saves and restores.
  30. * With RC6 enabled, the context is also referenced as the GPU enters and exists
  31. * from RC6 (GPU has it's own internal power context, except on gen5). Though
  32. * something like a context does exist for the media ring, the code only
  33. * supports contexts for the render ring.
  34. *
  35. * In software, there is a distinction between contexts created by the user,
  36. * and the default HW context. The default HW context is used by GPU clients
  37. * that do not request setup of their own hardware context. The default
  38. * context's state is never restored to help prevent programming errors. This
  39. * would happen if a client ran and piggy-backed off another clients GPU state.
  40. * The default context only exists to give the GPU some offset to load as the
  41. * current to invoke a save of the context we actually care about. In fact, the
  42. * code could likely be constructed, albeit in a more complicated fashion, to
  43. * never use the default context, though that limits the driver's ability to
  44. * swap out, and/or destroy other contexts.
  45. *
  46. * All other contexts are created as a request by the GPU client. These contexts
  47. * store GPU state, and thus allow GPU clients to not re-emit state (and
  48. * potentially query certain state) at any time. The kernel driver makes
  49. * certain that the appropriate commands are inserted.
  50. *
  51. * The context life cycle is semi-complicated in that context BOs may live
  52. * longer than the context itself because of the way the hardware, and object
  53. * tracking works. Below is a very crude representation of the state machine
  54. * describing the context life.
  55. * refcount pincount active
  56. * S0: initial state 0 0 0
  57. * S1: context created 1 0 0
  58. * S2: context is currently running 2 1 X
  59. * S3: GPU referenced, but not current 2 0 1
  60. * S4: context is current, but destroyed 1 1 0
  61. * S5: like S3, but destroyed 1 0 1
  62. *
  63. * The most common (but not all) transitions:
  64. * S0->S1: client creates a context
  65. * S1->S2: client submits execbuf with context
  66. * S2->S3: other clients submits execbuf with context
  67. * S3->S1: context object was retired
  68. * S3->S2: clients submits another execbuf
  69. * S2->S4: context destroy called with current context
  70. * S3->S5->S0: destroy path
  71. * S4->S5->S0: destroy path on current context
  72. *
  73. * There are two confusing terms used above:
  74. * The "current context" means the context which is currently running on the
  75. * GPU. The GPU has loaded its state already and has stored away the gtt
  76. * offset of the BO. The GPU is not actively referencing the data at this
  77. * offset, but it will on the next context switch. The only way to avoid this
  78. * is to do a GPU reset.
  79. *
  80. * An "active context' is one which was previously the "current context" and is
  81. * on the active list waiting for the next context switch to occur. Until this
  82. * happens, the object must remain at the same gtt offset. It is therefore
  83. * possible to destroy a context, but it is still active.
  84. *
  85. */
  86. #include <drm/drmP.h>
  87. #include <drm/i915_drm.h>
  88. #include "i915_drv.h"
  89. #include "i915_trace.h"
  90. #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
  91. void i915_gem_context_free(struct kref *ctx_ref)
  92. {
  93. struct i915_gem_context *ctx = container_of(ctx_ref, typeof(*ctx), ref);
  94. int i;
  95. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  96. trace_i915_context_free(ctx);
  97. GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
  98. i915_ppgtt_put(ctx->ppgtt);
  99. for (i = 0; i < I915_NUM_ENGINES; i++) {
  100. struct intel_context *ce = &ctx->engine[i];
  101. if (!ce->state)
  102. continue;
  103. WARN_ON(ce->pin_count);
  104. if (ce->ring)
  105. intel_ring_free(ce->ring);
  106. __i915_gem_object_release_unless_active(ce->state->obj);
  107. }
  108. kfree(ctx->name);
  109. put_pid(ctx->pid);
  110. list_del(&ctx->link);
  111. ida_simple_remove(&ctx->i915->context_hw_ida, ctx->hw_id);
  112. kfree(ctx);
  113. }
  114. static void context_close(struct i915_gem_context *ctx)
  115. {
  116. i915_gem_context_set_closed(ctx);
  117. if (ctx->ppgtt)
  118. i915_ppgtt_close(&ctx->ppgtt->base);
  119. ctx->file_priv = ERR_PTR(-EBADF);
  120. i915_gem_context_put(ctx);
  121. }
  122. static int assign_hw_id(struct drm_i915_private *dev_priv, unsigned *out)
  123. {
  124. int ret;
  125. ret = ida_simple_get(&dev_priv->context_hw_ida,
  126. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  127. if (ret < 0) {
  128. /* Contexts are only released when no longer active.
  129. * Flush any pending retires to hopefully release some
  130. * stale contexts and try again.
  131. */
  132. i915_gem_retire_requests(dev_priv);
  133. ret = ida_simple_get(&dev_priv->context_hw_ida,
  134. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  135. if (ret < 0)
  136. return ret;
  137. }
  138. *out = ret;
  139. return 0;
  140. }
  141. static u32 default_desc_template(const struct drm_i915_private *i915,
  142. const struct i915_hw_ppgtt *ppgtt)
  143. {
  144. u32 address_mode;
  145. u32 desc;
  146. desc = GEN8_CTX_VALID | GEN8_CTX_PRIVILEGE;
  147. address_mode = INTEL_LEGACY_32B_CONTEXT;
  148. if (ppgtt && i915_vm_is_48bit(&ppgtt->base))
  149. address_mode = INTEL_LEGACY_64B_CONTEXT;
  150. desc |= address_mode << GEN8_CTX_ADDRESSING_MODE_SHIFT;
  151. if (IS_GEN8(i915))
  152. desc |= GEN8_CTX_L3LLC_COHERENT;
  153. /* TODO: WaDisableLiteRestore when we start using semaphore
  154. * signalling between Command Streamers
  155. * ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
  156. */
  157. return desc;
  158. }
  159. static struct i915_gem_context *
  160. __create_hw_context(struct drm_i915_private *dev_priv,
  161. struct drm_i915_file_private *file_priv)
  162. {
  163. struct i915_gem_context *ctx;
  164. int ret;
  165. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  166. if (ctx == NULL)
  167. return ERR_PTR(-ENOMEM);
  168. ret = assign_hw_id(dev_priv, &ctx->hw_id);
  169. if (ret) {
  170. kfree(ctx);
  171. return ERR_PTR(ret);
  172. }
  173. kref_init(&ctx->ref);
  174. list_add_tail(&ctx->link, &dev_priv->context_list);
  175. ctx->i915 = dev_priv;
  176. /* Default context will never have a file_priv */
  177. ret = DEFAULT_CONTEXT_HANDLE;
  178. if (file_priv) {
  179. ret = idr_alloc(&file_priv->context_idr, ctx,
  180. DEFAULT_CONTEXT_HANDLE, 0, GFP_KERNEL);
  181. if (ret < 0)
  182. goto err_out;
  183. }
  184. ctx->user_handle = ret;
  185. ctx->file_priv = file_priv;
  186. if (file_priv) {
  187. ctx->pid = get_task_pid(current, PIDTYPE_PID);
  188. ctx->name = kasprintf(GFP_KERNEL, "%s[%d]/%x",
  189. current->comm,
  190. pid_nr(ctx->pid),
  191. ctx->user_handle);
  192. if (!ctx->name) {
  193. ret = -ENOMEM;
  194. goto err_pid;
  195. }
  196. }
  197. /* NB: Mark all slices as needing a remap so that when the context first
  198. * loads it will restore whatever remap state already exists. If there
  199. * is no remap info, it will be a NOP. */
  200. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  201. i915_gem_context_set_bannable(ctx);
  202. ctx->ring_size = 4 * PAGE_SIZE;
  203. ctx->desc_template =
  204. default_desc_template(dev_priv, dev_priv->mm.aliasing_ppgtt);
  205. /* GuC requires the ring to be placed above GUC_WOPCM_TOP. If GuC is not
  206. * present or not in use we still need a small bias as ring wraparound
  207. * at offset 0 sometimes hangs. No idea why.
  208. */
  209. if (HAS_GUC(dev_priv) && i915.enable_guc_loading)
  210. ctx->ggtt_offset_bias = GUC_WOPCM_TOP;
  211. else
  212. ctx->ggtt_offset_bias = I915_GTT_PAGE_SIZE;
  213. return ctx;
  214. err_pid:
  215. put_pid(ctx->pid);
  216. idr_remove(&file_priv->context_idr, ctx->user_handle);
  217. err_out:
  218. context_close(ctx);
  219. return ERR_PTR(ret);
  220. }
  221. static void __destroy_hw_context(struct i915_gem_context *ctx,
  222. struct drm_i915_file_private *file_priv)
  223. {
  224. idr_remove(&file_priv->context_idr, ctx->user_handle);
  225. context_close(ctx);
  226. }
  227. /**
  228. * The default context needs to exist per ring that uses contexts. It stores the
  229. * context state of the GPU for applications that don't utilize HW contexts, as
  230. * well as an idle case.
  231. */
  232. static struct i915_gem_context *
  233. i915_gem_create_context(struct drm_i915_private *dev_priv,
  234. struct drm_i915_file_private *file_priv)
  235. {
  236. struct i915_gem_context *ctx;
  237. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  238. ctx = __create_hw_context(dev_priv, file_priv);
  239. if (IS_ERR(ctx))
  240. return ctx;
  241. if (USES_FULL_PPGTT(dev_priv)) {
  242. struct i915_hw_ppgtt *ppgtt;
  243. ppgtt = i915_ppgtt_create(dev_priv, file_priv, ctx->name);
  244. if (IS_ERR(ppgtt)) {
  245. DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
  246. PTR_ERR(ppgtt));
  247. __destroy_hw_context(ctx, file_priv);
  248. return ERR_CAST(ppgtt);
  249. }
  250. ctx->ppgtt = ppgtt;
  251. ctx->desc_template = default_desc_template(dev_priv, ppgtt);
  252. }
  253. trace_i915_context_create(ctx);
  254. return ctx;
  255. }
  256. /**
  257. * i915_gem_context_create_gvt - create a GVT GEM context
  258. * @dev: drm device *
  259. *
  260. * This function is used to create a GVT specific GEM context.
  261. *
  262. * Returns:
  263. * pointer to i915_gem_context on success, error pointer if failed
  264. *
  265. */
  266. struct i915_gem_context *
  267. i915_gem_context_create_gvt(struct drm_device *dev)
  268. {
  269. struct i915_gem_context *ctx;
  270. int ret;
  271. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  272. return ERR_PTR(-ENODEV);
  273. ret = i915_mutex_lock_interruptible(dev);
  274. if (ret)
  275. return ERR_PTR(ret);
  276. ctx = __create_hw_context(to_i915(dev), NULL);
  277. if (IS_ERR(ctx))
  278. goto out;
  279. ctx->file_priv = ERR_PTR(-EBADF);
  280. i915_gem_context_set_closed(ctx); /* not user accessible */
  281. i915_gem_context_clear_bannable(ctx);
  282. i915_gem_context_set_force_single_submission(ctx);
  283. if (!i915.enable_guc_submission)
  284. ctx->ring_size = 512 * PAGE_SIZE; /* Max ring buffer size */
  285. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  286. out:
  287. mutex_unlock(&dev->struct_mutex);
  288. return ctx;
  289. }
  290. int i915_gem_context_init(struct drm_i915_private *dev_priv)
  291. {
  292. struct i915_gem_context *ctx;
  293. /* Init should only be called once per module load. Eventually the
  294. * restriction on the context_disabled check can be loosened. */
  295. if (WARN_ON(dev_priv->kernel_context))
  296. return 0;
  297. if (intel_vgpu_active(dev_priv) &&
  298. HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  299. if (!i915.enable_execlists) {
  300. DRM_INFO("Only EXECLIST mode is supported in vgpu.\n");
  301. return -EINVAL;
  302. }
  303. }
  304. /* Using the simple ida interface, the max is limited by sizeof(int) */
  305. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
  306. ida_init(&dev_priv->context_hw_ida);
  307. ctx = i915_gem_create_context(dev_priv, NULL);
  308. if (IS_ERR(ctx)) {
  309. DRM_ERROR("Failed to create default global context (error %ld)\n",
  310. PTR_ERR(ctx));
  311. return PTR_ERR(ctx);
  312. }
  313. /* For easy recognisablity, we want the kernel context to be 0 and then
  314. * all user contexts will have non-zero hw_id.
  315. */
  316. GEM_BUG_ON(ctx->hw_id);
  317. i915_gem_context_clear_bannable(ctx);
  318. ctx->priority = I915_PRIORITY_MIN; /* lowest priority; idle task */
  319. dev_priv->kernel_context = ctx;
  320. GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
  321. DRM_DEBUG_DRIVER("%s context support initialized\n",
  322. dev_priv->engine[RCS]->context_size ? "logical" :
  323. "fake");
  324. return 0;
  325. }
  326. void i915_gem_context_lost(struct drm_i915_private *dev_priv)
  327. {
  328. struct intel_engine_cs *engine;
  329. enum intel_engine_id id;
  330. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  331. for_each_engine(engine, dev_priv, id) {
  332. engine->legacy_active_context = NULL;
  333. if (!engine->last_retired_context)
  334. continue;
  335. engine->context_unpin(engine, engine->last_retired_context);
  336. engine->last_retired_context = NULL;
  337. }
  338. /* Force the GPU state to be restored on enabling */
  339. if (!i915.enable_execlists) {
  340. struct i915_gem_context *ctx;
  341. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  342. if (!i915_gem_context_is_default(ctx))
  343. continue;
  344. for_each_engine(engine, dev_priv, id)
  345. ctx->engine[engine->id].initialised = false;
  346. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  347. }
  348. for_each_engine(engine, dev_priv, id) {
  349. struct intel_context *kce =
  350. &dev_priv->kernel_context->engine[engine->id];
  351. kce->initialised = true;
  352. }
  353. }
  354. }
  355. void i915_gem_context_fini(struct drm_i915_private *dev_priv)
  356. {
  357. struct i915_gem_context *dctx = dev_priv->kernel_context;
  358. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  359. GEM_BUG_ON(!i915_gem_context_is_kernel(dctx));
  360. context_close(dctx);
  361. dev_priv->kernel_context = NULL;
  362. ida_destroy(&dev_priv->context_hw_ida);
  363. }
  364. static int context_idr_cleanup(int id, void *p, void *data)
  365. {
  366. struct i915_gem_context *ctx = p;
  367. context_close(ctx);
  368. return 0;
  369. }
  370. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  371. {
  372. struct drm_i915_file_private *file_priv = file->driver_priv;
  373. struct i915_gem_context *ctx;
  374. idr_init(&file_priv->context_idr);
  375. mutex_lock(&dev->struct_mutex);
  376. ctx = i915_gem_create_context(to_i915(dev), file_priv);
  377. mutex_unlock(&dev->struct_mutex);
  378. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  379. if (IS_ERR(ctx)) {
  380. idr_destroy(&file_priv->context_idr);
  381. return PTR_ERR(ctx);
  382. }
  383. return 0;
  384. }
  385. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  386. {
  387. struct drm_i915_file_private *file_priv = file->driver_priv;
  388. lockdep_assert_held(&dev->struct_mutex);
  389. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  390. idr_destroy(&file_priv->context_idr);
  391. }
  392. static inline int
  393. mi_set_context(struct drm_i915_gem_request *req, u32 flags)
  394. {
  395. struct drm_i915_private *dev_priv = req->i915;
  396. struct intel_engine_cs *engine = req->engine;
  397. enum intel_engine_id id;
  398. const int num_rings =
  399. /* Use an extended w/a on gen7 if signalling from other rings */
  400. (i915.semaphores && INTEL_GEN(dev_priv) == 7) ?
  401. INTEL_INFO(dev_priv)->num_rings - 1 :
  402. 0;
  403. int len;
  404. u32 *cs;
  405. flags |= MI_MM_SPACE_GTT;
  406. if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8)
  407. /* These flags are for resource streamer on HSW+ */
  408. flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
  409. else
  410. flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
  411. len = 4;
  412. if (INTEL_GEN(dev_priv) >= 7)
  413. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  414. cs = intel_ring_begin(req, len);
  415. if (IS_ERR(cs))
  416. return PTR_ERR(cs);
  417. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  418. if (INTEL_GEN(dev_priv) >= 7) {
  419. *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  420. if (num_rings) {
  421. struct intel_engine_cs *signaller;
  422. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  423. for_each_engine(signaller, dev_priv, id) {
  424. if (signaller == engine)
  425. continue;
  426. *cs++ = i915_mmio_reg_offset(
  427. RING_PSMI_CTL(signaller->mmio_base));
  428. *cs++ = _MASKED_BIT_ENABLE(
  429. GEN6_PSMI_SLEEP_MSG_DISABLE);
  430. }
  431. }
  432. }
  433. *cs++ = MI_NOOP;
  434. *cs++ = MI_SET_CONTEXT;
  435. *cs++ = i915_ggtt_offset(req->ctx->engine[RCS].state) | flags;
  436. /*
  437. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  438. * WaMiSetContext_Hang:snb,ivb,vlv
  439. */
  440. *cs++ = MI_NOOP;
  441. if (INTEL_GEN(dev_priv) >= 7) {
  442. if (num_rings) {
  443. struct intel_engine_cs *signaller;
  444. i915_reg_t last_reg = {}; /* keep gcc quiet */
  445. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  446. for_each_engine(signaller, dev_priv, id) {
  447. if (signaller == engine)
  448. continue;
  449. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  450. *cs++ = i915_mmio_reg_offset(last_reg);
  451. *cs++ = _MASKED_BIT_DISABLE(
  452. GEN6_PSMI_SLEEP_MSG_DISABLE);
  453. }
  454. /* Insert a delay before the next switch! */
  455. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  456. *cs++ = i915_mmio_reg_offset(last_reg);
  457. *cs++ = i915_ggtt_offset(engine->scratch);
  458. *cs++ = MI_NOOP;
  459. }
  460. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  461. }
  462. intel_ring_advance(req, cs);
  463. return 0;
  464. }
  465. static int remap_l3(struct drm_i915_gem_request *req, int slice)
  466. {
  467. u32 *cs, *remap_info = req->i915->l3_parity.remap_info[slice];
  468. int i;
  469. if (!remap_info)
  470. return 0;
  471. cs = intel_ring_begin(req, GEN7_L3LOG_SIZE/4 * 2 + 2);
  472. if (IS_ERR(cs))
  473. return PTR_ERR(cs);
  474. /*
  475. * Note: We do not worry about the concurrent register cacheline hang
  476. * here because no other code should access these registers other than
  477. * at initialization time.
  478. */
  479. *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
  480. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  481. *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
  482. *cs++ = remap_info[i];
  483. }
  484. *cs++ = MI_NOOP;
  485. intel_ring_advance(req, cs);
  486. return 0;
  487. }
  488. static inline bool skip_rcs_switch(struct i915_hw_ppgtt *ppgtt,
  489. struct intel_engine_cs *engine,
  490. struct i915_gem_context *to)
  491. {
  492. if (to->remap_slice)
  493. return false;
  494. if (!to->engine[RCS].initialised)
  495. return false;
  496. if (ppgtt && (intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  497. return false;
  498. return to == engine->legacy_active_context;
  499. }
  500. static bool
  501. needs_pd_load_pre(struct i915_hw_ppgtt *ppgtt,
  502. struct intel_engine_cs *engine,
  503. struct i915_gem_context *to)
  504. {
  505. if (!ppgtt)
  506. return false;
  507. /* Always load the ppgtt on first use */
  508. if (!engine->legacy_active_context)
  509. return true;
  510. /* Same context without new entries, skip */
  511. if (engine->legacy_active_context == to &&
  512. !(intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  513. return false;
  514. if (engine->id != RCS)
  515. return true;
  516. if (INTEL_GEN(engine->i915) < 8)
  517. return true;
  518. return false;
  519. }
  520. static bool
  521. needs_pd_load_post(struct i915_hw_ppgtt *ppgtt,
  522. struct i915_gem_context *to,
  523. u32 hw_flags)
  524. {
  525. if (!ppgtt)
  526. return false;
  527. if (!IS_GEN8(to->i915))
  528. return false;
  529. if (hw_flags & MI_RESTORE_INHIBIT)
  530. return true;
  531. return false;
  532. }
  533. static int do_rcs_switch(struct drm_i915_gem_request *req)
  534. {
  535. struct i915_gem_context *to = req->ctx;
  536. struct intel_engine_cs *engine = req->engine;
  537. struct i915_hw_ppgtt *ppgtt = to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  538. struct i915_gem_context *from = engine->legacy_active_context;
  539. u32 hw_flags;
  540. int ret, i;
  541. GEM_BUG_ON(engine->id != RCS);
  542. if (skip_rcs_switch(ppgtt, engine, to))
  543. return 0;
  544. if (needs_pd_load_pre(ppgtt, engine, to)) {
  545. /* Older GENs and non render rings still want the load first,
  546. * "PP_DCLV followed by PP_DIR_BASE register through Load
  547. * Register Immediate commands in Ring Buffer before submitting
  548. * a context."*/
  549. trace_switch_mm(engine, to);
  550. ret = ppgtt->switch_mm(ppgtt, req);
  551. if (ret)
  552. return ret;
  553. }
  554. if (!to->engine[RCS].initialised || i915_gem_context_is_default(to))
  555. /* NB: If we inhibit the restore, the context is not allowed to
  556. * die because future work may end up depending on valid address
  557. * space. This means we must enforce that a page table load
  558. * occur when this occurs. */
  559. hw_flags = MI_RESTORE_INHIBIT;
  560. else if (ppgtt && intel_engine_flag(engine) & ppgtt->pd_dirty_rings)
  561. hw_flags = MI_FORCE_RESTORE;
  562. else
  563. hw_flags = 0;
  564. if (to != from || (hw_flags & MI_FORCE_RESTORE)) {
  565. ret = mi_set_context(req, hw_flags);
  566. if (ret)
  567. return ret;
  568. engine->legacy_active_context = to;
  569. }
  570. /* GEN8 does *not* require an explicit reload if the PDPs have been
  571. * setup, and we do not wish to move them.
  572. */
  573. if (needs_pd_load_post(ppgtt, to, hw_flags)) {
  574. trace_switch_mm(engine, to);
  575. ret = ppgtt->switch_mm(ppgtt, req);
  576. /* The hardware context switch is emitted, but we haven't
  577. * actually changed the state - so it's probably safe to bail
  578. * here. Still, let the user know something dangerous has
  579. * happened.
  580. */
  581. if (ret)
  582. return ret;
  583. }
  584. if (ppgtt)
  585. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  586. for (i = 0; i < MAX_L3_SLICES; i++) {
  587. if (!(to->remap_slice & (1<<i)))
  588. continue;
  589. ret = remap_l3(req, i);
  590. if (ret)
  591. return ret;
  592. to->remap_slice &= ~(1<<i);
  593. }
  594. if (!to->engine[RCS].initialised) {
  595. if (engine->init_context) {
  596. ret = engine->init_context(req);
  597. if (ret)
  598. return ret;
  599. }
  600. to->engine[RCS].initialised = true;
  601. }
  602. return 0;
  603. }
  604. /**
  605. * i915_switch_context() - perform a GPU context switch.
  606. * @req: request for which we'll execute the context switch
  607. *
  608. * The context life cycle is simple. The context refcount is incremented and
  609. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  610. * it will have a refcount > 1. This allows us to destroy the context abstract
  611. * object while letting the normal object tracking destroy the backing BO.
  612. *
  613. * This function should not be used in execlists mode. Instead the context is
  614. * switched by writing to the ELSP and requests keep a reference to their
  615. * context.
  616. */
  617. int i915_switch_context(struct drm_i915_gem_request *req)
  618. {
  619. struct intel_engine_cs *engine = req->engine;
  620. lockdep_assert_held(&req->i915->drm.struct_mutex);
  621. if (i915.enable_execlists)
  622. return 0;
  623. if (!req->ctx->engine[engine->id].state) {
  624. struct i915_gem_context *to = req->ctx;
  625. struct i915_hw_ppgtt *ppgtt =
  626. to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  627. if (needs_pd_load_pre(ppgtt, engine, to)) {
  628. int ret;
  629. trace_switch_mm(engine, to);
  630. ret = ppgtt->switch_mm(ppgtt, req);
  631. if (ret)
  632. return ret;
  633. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  634. }
  635. return 0;
  636. }
  637. return do_rcs_switch(req);
  638. }
  639. static bool engine_has_kernel_context(struct intel_engine_cs *engine)
  640. {
  641. struct i915_gem_timeline *timeline;
  642. list_for_each_entry(timeline, &engine->i915->gt.timelines, link) {
  643. struct intel_timeline *tl;
  644. if (timeline == &engine->i915->gt.global_timeline)
  645. continue;
  646. tl = &timeline->engine[engine->id];
  647. if (i915_gem_active_peek(&tl->last_request,
  648. &engine->i915->drm.struct_mutex))
  649. return false;
  650. }
  651. return (!engine->last_retired_context ||
  652. i915_gem_context_is_kernel(engine->last_retired_context));
  653. }
  654. int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv)
  655. {
  656. struct intel_engine_cs *engine;
  657. struct i915_gem_timeline *timeline;
  658. enum intel_engine_id id;
  659. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  660. i915_gem_retire_requests(dev_priv);
  661. for_each_engine(engine, dev_priv, id) {
  662. struct drm_i915_gem_request *req;
  663. int ret;
  664. if (engine_has_kernel_context(engine))
  665. continue;
  666. req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
  667. if (IS_ERR(req))
  668. return PTR_ERR(req);
  669. /* Queue this switch after all other activity */
  670. list_for_each_entry(timeline, &dev_priv->gt.timelines, link) {
  671. struct drm_i915_gem_request *prev;
  672. struct intel_timeline *tl;
  673. tl = &timeline->engine[engine->id];
  674. prev = i915_gem_active_raw(&tl->last_request,
  675. &dev_priv->drm.struct_mutex);
  676. if (prev)
  677. i915_sw_fence_await_sw_fence_gfp(&req->submit,
  678. &prev->submit,
  679. GFP_KERNEL);
  680. }
  681. ret = i915_switch_context(req);
  682. i915_add_request(req);
  683. if (ret)
  684. return ret;
  685. }
  686. return 0;
  687. }
  688. static bool client_is_banned(struct drm_i915_file_private *file_priv)
  689. {
  690. return file_priv->context_bans > I915_MAX_CLIENT_CONTEXT_BANS;
  691. }
  692. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  693. struct drm_file *file)
  694. {
  695. struct drm_i915_private *dev_priv = to_i915(dev);
  696. struct drm_i915_gem_context_create *args = data;
  697. struct drm_i915_file_private *file_priv = file->driver_priv;
  698. struct i915_gem_context *ctx;
  699. int ret;
  700. if (!dev_priv->engine[RCS]->context_size)
  701. return -ENODEV;
  702. if (args->pad != 0)
  703. return -EINVAL;
  704. if (client_is_banned(file_priv)) {
  705. DRM_DEBUG("client %s[%d] banned from creating ctx\n",
  706. current->comm,
  707. pid_nr(get_task_pid(current, PIDTYPE_PID)));
  708. return -EIO;
  709. }
  710. ret = i915_mutex_lock_interruptible(dev);
  711. if (ret)
  712. return ret;
  713. ctx = i915_gem_create_context(dev_priv, file_priv);
  714. mutex_unlock(&dev->struct_mutex);
  715. if (IS_ERR(ctx))
  716. return PTR_ERR(ctx);
  717. GEM_BUG_ON(i915_gem_context_is_kernel(ctx));
  718. args->ctx_id = ctx->user_handle;
  719. DRM_DEBUG("HW context %d created\n", args->ctx_id);
  720. return 0;
  721. }
  722. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  723. struct drm_file *file)
  724. {
  725. struct drm_i915_gem_context_destroy *args = data;
  726. struct drm_i915_file_private *file_priv = file->driver_priv;
  727. struct i915_gem_context *ctx;
  728. int ret;
  729. if (args->pad != 0)
  730. return -EINVAL;
  731. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  732. return -ENOENT;
  733. ret = i915_mutex_lock_interruptible(dev);
  734. if (ret)
  735. return ret;
  736. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  737. if (IS_ERR(ctx)) {
  738. mutex_unlock(&dev->struct_mutex);
  739. return PTR_ERR(ctx);
  740. }
  741. __destroy_hw_context(ctx, file_priv);
  742. mutex_unlock(&dev->struct_mutex);
  743. DRM_DEBUG("HW context %d destroyed\n", args->ctx_id);
  744. return 0;
  745. }
  746. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  747. struct drm_file *file)
  748. {
  749. struct drm_i915_file_private *file_priv = file->driver_priv;
  750. struct drm_i915_gem_context_param *args = data;
  751. struct i915_gem_context *ctx;
  752. int ret;
  753. ret = i915_mutex_lock_interruptible(dev);
  754. if (ret)
  755. return ret;
  756. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  757. if (IS_ERR(ctx)) {
  758. mutex_unlock(&dev->struct_mutex);
  759. return PTR_ERR(ctx);
  760. }
  761. args->size = 0;
  762. switch (args->param) {
  763. case I915_CONTEXT_PARAM_BAN_PERIOD:
  764. ret = -EINVAL;
  765. break;
  766. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  767. args->value = ctx->flags & CONTEXT_NO_ZEROMAP;
  768. break;
  769. case I915_CONTEXT_PARAM_GTT_SIZE:
  770. if (ctx->ppgtt)
  771. args->value = ctx->ppgtt->base.total;
  772. else if (to_i915(dev)->mm.aliasing_ppgtt)
  773. args->value = to_i915(dev)->mm.aliasing_ppgtt->base.total;
  774. else
  775. args->value = to_i915(dev)->ggtt.base.total;
  776. break;
  777. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  778. args->value = i915_gem_context_no_error_capture(ctx);
  779. break;
  780. case I915_CONTEXT_PARAM_BANNABLE:
  781. args->value = i915_gem_context_is_bannable(ctx);
  782. break;
  783. default:
  784. ret = -EINVAL;
  785. break;
  786. }
  787. mutex_unlock(&dev->struct_mutex);
  788. return ret;
  789. }
  790. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  791. struct drm_file *file)
  792. {
  793. struct drm_i915_file_private *file_priv = file->driver_priv;
  794. struct drm_i915_gem_context_param *args = data;
  795. struct i915_gem_context *ctx;
  796. int ret;
  797. ret = i915_mutex_lock_interruptible(dev);
  798. if (ret)
  799. return ret;
  800. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  801. if (IS_ERR(ctx)) {
  802. mutex_unlock(&dev->struct_mutex);
  803. return PTR_ERR(ctx);
  804. }
  805. switch (args->param) {
  806. case I915_CONTEXT_PARAM_BAN_PERIOD:
  807. ret = -EINVAL;
  808. break;
  809. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  810. if (args->size) {
  811. ret = -EINVAL;
  812. } else {
  813. ctx->flags &= ~CONTEXT_NO_ZEROMAP;
  814. ctx->flags |= args->value ? CONTEXT_NO_ZEROMAP : 0;
  815. }
  816. break;
  817. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  818. if (args->size)
  819. ret = -EINVAL;
  820. else if (args->value)
  821. i915_gem_context_set_no_error_capture(ctx);
  822. else
  823. i915_gem_context_clear_no_error_capture(ctx);
  824. break;
  825. case I915_CONTEXT_PARAM_BANNABLE:
  826. if (args->size)
  827. ret = -EINVAL;
  828. else if (!capable(CAP_SYS_ADMIN) && !args->value)
  829. ret = -EPERM;
  830. else if (args->value)
  831. i915_gem_context_set_bannable(ctx);
  832. else
  833. i915_gem_context_clear_bannable(ctx);
  834. break;
  835. default:
  836. ret = -EINVAL;
  837. break;
  838. }
  839. mutex_unlock(&dev->struct_mutex);
  840. return ret;
  841. }
  842. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
  843. void *data, struct drm_file *file)
  844. {
  845. struct drm_i915_private *dev_priv = to_i915(dev);
  846. struct drm_i915_reset_stats *args = data;
  847. struct i915_gem_context *ctx;
  848. int ret;
  849. if (args->flags || args->pad)
  850. return -EINVAL;
  851. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE && !capable(CAP_SYS_ADMIN))
  852. return -EPERM;
  853. ret = i915_mutex_lock_interruptible(dev);
  854. if (ret)
  855. return ret;
  856. ctx = i915_gem_context_lookup(file->driver_priv, args->ctx_id);
  857. if (IS_ERR(ctx)) {
  858. mutex_unlock(&dev->struct_mutex);
  859. return PTR_ERR(ctx);
  860. }
  861. if (capable(CAP_SYS_ADMIN))
  862. args->reset_count = i915_reset_count(&dev_priv->gpu_error);
  863. else
  864. args->reset_count = 0;
  865. args->batch_active = ctx->guilty_count;
  866. args->batch_pending = ctx->active_count;
  867. mutex_unlock(&dev->struct_mutex);
  868. return 0;
  869. }
  870. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  871. #include "selftests/mock_context.c"
  872. #include "selftests/i915_gem_context.c"
  873. #endif