i915_gem_context.c 31 KB

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