i915_gem_context.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 4096
  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. static void i915_gem_context_clean(struct i915_gem_context *ctx)
  128. {
  129. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  130. struct i915_vma *vma, *next;
  131. if (!ppgtt)
  132. return;
  133. list_for_each_entry_safe(vma, next, &ppgtt->base.inactive_list,
  134. vm_link) {
  135. if (WARN_ON(__i915_vma_unbind_no_wait(vma)))
  136. break;
  137. }
  138. }
  139. void i915_gem_context_free(struct kref *ctx_ref)
  140. {
  141. struct i915_gem_context *ctx = container_of(ctx_ref, typeof(*ctx), ref);
  142. int i;
  143. lockdep_assert_held(&ctx->i915->dev->struct_mutex);
  144. trace_i915_context_free(ctx);
  145. /*
  146. * This context is going away and we need to remove all VMAs still
  147. * around. This is to handle imported shared objects for which
  148. * destructor did not run when their handles were closed.
  149. */
  150. i915_gem_context_clean(ctx);
  151. i915_ppgtt_put(ctx->ppgtt);
  152. for (i = 0; i < I915_NUM_ENGINES; i++) {
  153. struct intel_context *ce = &ctx->engine[i];
  154. if (!ce->state)
  155. continue;
  156. WARN_ON(ce->pin_count);
  157. if (ce->ringbuf)
  158. intel_ringbuffer_free(ce->ringbuf);
  159. drm_gem_object_unreference(&ce->state->base);
  160. }
  161. list_del(&ctx->link);
  162. ida_simple_remove(&ctx->i915->context_hw_ida, ctx->hw_id);
  163. kfree(ctx);
  164. }
  165. struct drm_i915_gem_object *
  166. i915_gem_alloc_context_obj(struct drm_device *dev, size_t size)
  167. {
  168. struct drm_i915_gem_object *obj;
  169. int ret;
  170. lockdep_assert_held(&dev->struct_mutex);
  171. obj = i915_gem_object_create(dev, size);
  172. if (IS_ERR(obj))
  173. return obj;
  174. /*
  175. * Try to make the context utilize L3 as well as LLC.
  176. *
  177. * On VLV we don't have L3 controls in the PTEs so we
  178. * shouldn't touch the cache level, especially as that
  179. * would make the object snooped which might have a
  180. * negative performance impact.
  181. *
  182. * Snooping is required on non-llc platforms in execlist
  183. * mode, but since all GGTT accesses use PAT entry 0 we
  184. * get snooping anyway regardless of cache_level.
  185. *
  186. * This is only applicable for Ivy Bridge devices since
  187. * later platforms don't have L3 control bits in the PTE.
  188. */
  189. if (IS_IVYBRIDGE(dev)) {
  190. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  191. /* Failure shouldn't ever happen this early */
  192. if (WARN_ON(ret)) {
  193. drm_gem_object_unreference(&obj->base);
  194. return ERR_PTR(ret);
  195. }
  196. }
  197. return obj;
  198. }
  199. static int assign_hw_id(struct drm_i915_private *dev_priv, unsigned *out)
  200. {
  201. int ret;
  202. ret = ida_simple_get(&dev_priv->context_hw_ida,
  203. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  204. if (ret < 0) {
  205. /* Contexts are only released when no longer active.
  206. * Flush any pending retires to hopefully release some
  207. * stale contexts and try again.
  208. */
  209. i915_gem_retire_requests(dev_priv);
  210. ret = ida_simple_get(&dev_priv->context_hw_ida,
  211. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. *out = ret;
  216. return 0;
  217. }
  218. static struct i915_gem_context *
  219. __create_hw_context(struct drm_device *dev,
  220. struct drm_i915_file_private *file_priv)
  221. {
  222. struct drm_i915_private *dev_priv = dev->dev_private;
  223. struct i915_gem_context *ctx;
  224. int ret;
  225. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  226. if (ctx == NULL)
  227. return ERR_PTR(-ENOMEM);
  228. ret = assign_hw_id(dev_priv, &ctx->hw_id);
  229. if (ret) {
  230. kfree(ctx);
  231. return ERR_PTR(ret);
  232. }
  233. kref_init(&ctx->ref);
  234. list_add_tail(&ctx->link, &dev_priv->context_list);
  235. ctx->i915 = dev_priv;
  236. if (dev_priv->hw_context_size) {
  237. struct drm_i915_gem_object *obj =
  238. i915_gem_alloc_context_obj(dev, dev_priv->hw_context_size);
  239. if (IS_ERR(obj)) {
  240. ret = PTR_ERR(obj);
  241. goto err_out;
  242. }
  243. ctx->engine[RCS].state = obj;
  244. }
  245. /* Default context will never have a file_priv */
  246. if (file_priv != NULL) {
  247. ret = idr_alloc(&file_priv->context_idr, ctx,
  248. DEFAULT_CONTEXT_HANDLE, 0, GFP_KERNEL);
  249. if (ret < 0)
  250. goto err_out;
  251. } else
  252. ret = DEFAULT_CONTEXT_HANDLE;
  253. ctx->file_priv = file_priv;
  254. ctx->user_handle = ret;
  255. /* NB: Mark all slices as needing a remap so that when the context first
  256. * loads it will restore whatever remap state already exists. If there
  257. * is no remap info, it will be a NOP. */
  258. ctx->remap_slice = ALL_L3_SLICES(dev_priv);
  259. ctx->hang_stats.ban_period_seconds = DRM_I915_CTX_BAN_PERIOD;
  260. return ctx;
  261. err_out:
  262. i915_gem_context_unreference(ctx);
  263. return ERR_PTR(ret);
  264. }
  265. /**
  266. * The default context needs to exist per ring that uses contexts. It stores the
  267. * context state of the GPU for applications that don't utilize HW contexts, as
  268. * well as an idle case.
  269. */
  270. static struct i915_gem_context *
  271. i915_gem_create_context(struct drm_device *dev,
  272. struct drm_i915_file_private *file_priv)
  273. {
  274. struct i915_gem_context *ctx;
  275. lockdep_assert_held(&dev->struct_mutex);
  276. ctx = __create_hw_context(dev, file_priv);
  277. if (IS_ERR(ctx))
  278. return ctx;
  279. if (USES_FULL_PPGTT(dev)) {
  280. struct i915_hw_ppgtt *ppgtt = i915_ppgtt_create(dev, file_priv);
  281. if (IS_ERR(ppgtt)) {
  282. DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n",
  283. PTR_ERR(ppgtt));
  284. idr_remove(&file_priv->context_idr, ctx->user_handle);
  285. i915_gem_context_unreference(ctx);
  286. return ERR_CAST(ppgtt);
  287. }
  288. ctx->ppgtt = ppgtt;
  289. }
  290. trace_i915_context_create(ctx);
  291. return ctx;
  292. }
  293. static void i915_gem_context_unpin(struct i915_gem_context *ctx,
  294. struct intel_engine_cs *engine)
  295. {
  296. if (i915.enable_execlists) {
  297. intel_lr_context_unpin(ctx, engine);
  298. } else {
  299. struct intel_context *ce = &ctx->engine[engine->id];
  300. if (ce->state)
  301. i915_gem_object_ggtt_unpin(ce->state);
  302. i915_gem_context_unreference(ctx);
  303. }
  304. }
  305. void i915_gem_context_reset(struct drm_device *dev)
  306. {
  307. struct drm_i915_private *dev_priv = dev->dev_private;
  308. lockdep_assert_held(&dev->struct_mutex);
  309. if (i915.enable_execlists) {
  310. struct i915_gem_context *ctx;
  311. list_for_each_entry(ctx, &dev_priv->context_list, link)
  312. intel_lr_context_reset(dev_priv, ctx);
  313. }
  314. i915_gem_context_lost(dev_priv);
  315. }
  316. int i915_gem_context_init(struct drm_device *dev)
  317. {
  318. struct drm_i915_private *dev_priv = dev->dev_private;
  319. struct i915_gem_context *ctx;
  320. /* Init should only be called once per module load. Eventually the
  321. * restriction on the context_disabled check can be loosened. */
  322. if (WARN_ON(dev_priv->kernel_context))
  323. return 0;
  324. if (intel_vgpu_active(dev_priv) &&
  325. HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  326. if (!i915.enable_execlists) {
  327. DRM_INFO("Only EXECLIST mode is supported in vgpu.\n");
  328. return -EINVAL;
  329. }
  330. }
  331. /* Using the simple ida interface, the max is limited by sizeof(int) */
  332. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > INT_MAX);
  333. ida_init(&dev_priv->context_hw_ida);
  334. if (i915.enable_execlists) {
  335. /* NB: intentionally left blank. We will allocate our own
  336. * backing objects as we need them, thank you very much */
  337. dev_priv->hw_context_size = 0;
  338. } else if (HAS_HW_CONTEXTS(dev_priv)) {
  339. dev_priv->hw_context_size =
  340. round_up(get_context_size(dev_priv), 4096);
  341. if (dev_priv->hw_context_size > (1<<20)) {
  342. DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n",
  343. dev_priv->hw_context_size);
  344. dev_priv->hw_context_size = 0;
  345. }
  346. }
  347. ctx = i915_gem_create_context(dev, NULL);
  348. if (IS_ERR(ctx)) {
  349. DRM_ERROR("Failed to create default global context (error %ld)\n",
  350. PTR_ERR(ctx));
  351. return PTR_ERR(ctx);
  352. }
  353. if (!i915.enable_execlists && ctx->engine[RCS].state) {
  354. int ret;
  355. /* We may need to do things with the shrinker which
  356. * require us to immediately switch back to the default
  357. * context. This can cause a problem as pinning the
  358. * default context also requires GTT space which may not
  359. * be available. To avoid this we always pin the default
  360. * context.
  361. */
  362. ret = i915_gem_obj_ggtt_pin(ctx->engine[RCS].state,
  363. get_context_alignment(dev_priv), 0);
  364. if (ret) {
  365. DRM_ERROR("Failed to pinned default global context (error %d)\n",
  366. ret);
  367. i915_gem_context_unreference(ctx);
  368. return ret;
  369. }
  370. }
  371. dev_priv->kernel_context = ctx;
  372. DRM_DEBUG_DRIVER("%s context support initialized\n",
  373. i915.enable_execlists ? "LR" :
  374. dev_priv->hw_context_size ? "HW" : "fake");
  375. return 0;
  376. }
  377. void i915_gem_context_lost(struct drm_i915_private *dev_priv)
  378. {
  379. struct intel_engine_cs *engine;
  380. lockdep_assert_held(&dev_priv->dev->struct_mutex);
  381. for_each_engine(engine, dev_priv) {
  382. if (engine->last_context) {
  383. i915_gem_context_unpin(engine->last_context, engine);
  384. engine->last_context = NULL;
  385. }
  386. /* Force the GPU state to be reinitialised on enabling */
  387. dev_priv->kernel_context->engine[engine->id].initialised =
  388. engine->init_context == NULL;
  389. }
  390. /* Force the GPU state to be reinitialised on enabling */
  391. dev_priv->kernel_context->remap_slice = ALL_L3_SLICES(dev_priv);
  392. }
  393. void i915_gem_context_fini(struct drm_device *dev)
  394. {
  395. struct drm_i915_private *dev_priv = dev->dev_private;
  396. struct i915_gem_context *dctx = dev_priv->kernel_context;
  397. lockdep_assert_held(&dev->struct_mutex);
  398. if (!i915.enable_execlists && dctx->engine[RCS].state)
  399. i915_gem_object_ggtt_unpin(dctx->engine[RCS].state);
  400. i915_gem_context_unreference(dctx);
  401. dev_priv->kernel_context = NULL;
  402. ida_destroy(&dev_priv->context_hw_ida);
  403. }
  404. static int context_idr_cleanup(int id, void *p, void *data)
  405. {
  406. struct i915_gem_context *ctx = p;
  407. ctx->file_priv = ERR_PTR(-EBADF);
  408. i915_gem_context_unreference(ctx);
  409. return 0;
  410. }
  411. int i915_gem_context_open(struct drm_device *dev, struct drm_file *file)
  412. {
  413. struct drm_i915_file_private *file_priv = file->driver_priv;
  414. struct i915_gem_context *ctx;
  415. idr_init(&file_priv->context_idr);
  416. mutex_lock(&dev->struct_mutex);
  417. ctx = i915_gem_create_context(dev, file_priv);
  418. mutex_unlock(&dev->struct_mutex);
  419. if (IS_ERR(ctx)) {
  420. idr_destroy(&file_priv->context_idr);
  421. return PTR_ERR(ctx);
  422. }
  423. return 0;
  424. }
  425. void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
  426. {
  427. struct drm_i915_file_private *file_priv = file->driver_priv;
  428. lockdep_assert_held(&dev->struct_mutex);
  429. idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL);
  430. idr_destroy(&file_priv->context_idr);
  431. }
  432. static inline int
  433. mi_set_context(struct drm_i915_gem_request *req, u32 hw_flags)
  434. {
  435. struct drm_i915_private *dev_priv = req->i915;
  436. struct intel_engine_cs *engine = req->engine;
  437. u32 flags = hw_flags | MI_MM_SPACE_GTT;
  438. const int num_rings =
  439. /* Use an extended w/a on ivb+ if signalling from other rings */
  440. i915_semaphore_is_enabled(dev_priv) ?
  441. hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1 :
  442. 0;
  443. int len, ret;
  444. /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB
  445. * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value
  446. * explicitly, so we rely on the value at ring init, stored in
  447. * itlb_before_ctx_switch.
  448. */
  449. if (IS_GEN6(dev_priv)) {
  450. ret = engine->flush(req, I915_GEM_GPU_DOMAINS, 0);
  451. if (ret)
  452. return ret;
  453. }
  454. /* These flags are for resource streamer on HSW+ */
  455. if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8)
  456. flags |= (HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN);
  457. else if (INTEL_GEN(dev_priv) < 8)
  458. flags |= (MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN);
  459. len = 4;
  460. if (INTEL_GEN(dev_priv) >= 7)
  461. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  462. ret = intel_ring_begin(req, len);
  463. if (ret)
  464. return ret;
  465. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  466. if (INTEL_GEN(dev_priv) >= 7) {
  467. intel_ring_emit(engine, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  468. if (num_rings) {
  469. struct intel_engine_cs *signaller;
  470. intel_ring_emit(engine,
  471. MI_LOAD_REGISTER_IMM(num_rings));
  472. for_each_engine(signaller, dev_priv) {
  473. if (signaller == engine)
  474. continue;
  475. intel_ring_emit_reg(engine,
  476. RING_PSMI_CTL(signaller->mmio_base));
  477. intel_ring_emit(engine,
  478. _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  479. }
  480. }
  481. }
  482. intel_ring_emit(engine, MI_NOOP);
  483. intel_ring_emit(engine, MI_SET_CONTEXT);
  484. intel_ring_emit(engine,
  485. i915_gem_obj_ggtt_offset(req->ctx->engine[RCS].state) |
  486. flags);
  487. /*
  488. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  489. * WaMiSetContext_Hang:snb,ivb,vlv
  490. */
  491. intel_ring_emit(engine, MI_NOOP);
  492. if (INTEL_GEN(dev_priv) >= 7) {
  493. if (num_rings) {
  494. struct intel_engine_cs *signaller;
  495. i915_reg_t last_reg = {}; /* keep gcc quiet */
  496. intel_ring_emit(engine,
  497. MI_LOAD_REGISTER_IMM(num_rings));
  498. for_each_engine(signaller, dev_priv) {
  499. if (signaller == engine)
  500. continue;
  501. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  502. intel_ring_emit_reg(engine, last_reg);
  503. intel_ring_emit(engine,
  504. _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE));
  505. }
  506. /* Insert a delay before the next switch! */
  507. intel_ring_emit(engine,
  508. MI_STORE_REGISTER_MEM |
  509. MI_SRM_LRM_GLOBAL_GTT);
  510. intel_ring_emit_reg(engine, last_reg);
  511. intel_ring_emit(engine, engine->scratch.gtt_offset);
  512. intel_ring_emit(engine, MI_NOOP);
  513. }
  514. intel_ring_emit(engine, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  515. }
  516. intel_ring_advance(engine);
  517. return ret;
  518. }
  519. static int remap_l3(struct drm_i915_gem_request *req, int slice)
  520. {
  521. u32 *remap_info = req->i915->l3_parity.remap_info[slice];
  522. struct intel_engine_cs *engine = req->engine;
  523. int i, ret;
  524. if (!remap_info)
  525. return 0;
  526. ret = intel_ring_begin(req, GEN7_L3LOG_SIZE/4 * 2 + 2);
  527. if (ret)
  528. return ret;
  529. /*
  530. * Note: We do not worry about the concurrent register cacheline hang
  531. * here because no other code should access these registers other than
  532. * at initialization time.
  533. */
  534. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4));
  535. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  536. intel_ring_emit_reg(engine, GEN7_L3LOG(slice, i));
  537. intel_ring_emit(engine, remap_info[i]);
  538. }
  539. intel_ring_emit(engine, MI_NOOP);
  540. intel_ring_advance(engine);
  541. return 0;
  542. }
  543. static inline bool skip_rcs_switch(struct i915_hw_ppgtt *ppgtt,
  544. struct intel_engine_cs *engine,
  545. struct i915_gem_context *to)
  546. {
  547. if (to->remap_slice)
  548. return false;
  549. if (!to->engine[RCS].initialised)
  550. return false;
  551. if (ppgtt && (intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  552. return false;
  553. return to == engine->last_context;
  554. }
  555. static bool
  556. needs_pd_load_pre(struct i915_hw_ppgtt *ppgtt,
  557. struct intel_engine_cs *engine,
  558. struct i915_gem_context *to)
  559. {
  560. if (!ppgtt)
  561. return false;
  562. /* Always load the ppgtt on first use */
  563. if (!engine->last_context)
  564. return true;
  565. /* Same context without new entries, skip */
  566. if (engine->last_context == to &&
  567. !(intel_engine_flag(engine) & ppgtt->pd_dirty_rings))
  568. return false;
  569. if (engine->id != RCS)
  570. return true;
  571. if (INTEL_GEN(engine->i915) < 8)
  572. return true;
  573. return false;
  574. }
  575. static bool
  576. needs_pd_load_post(struct i915_hw_ppgtt *ppgtt,
  577. struct i915_gem_context *to,
  578. u32 hw_flags)
  579. {
  580. if (!ppgtt)
  581. return false;
  582. if (!IS_GEN8(to->i915))
  583. return false;
  584. if (hw_flags & MI_RESTORE_INHIBIT)
  585. return true;
  586. return false;
  587. }
  588. static int do_rcs_switch(struct drm_i915_gem_request *req)
  589. {
  590. struct i915_gem_context *to = req->ctx;
  591. struct intel_engine_cs *engine = req->engine;
  592. struct i915_hw_ppgtt *ppgtt = to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  593. struct i915_gem_context *from;
  594. u32 hw_flags;
  595. int ret, i;
  596. if (skip_rcs_switch(ppgtt, engine, to))
  597. return 0;
  598. /* Trying to pin first makes error handling easier. */
  599. ret = i915_gem_obj_ggtt_pin(to->engine[RCS].state,
  600. get_context_alignment(engine->i915),
  601. 0);
  602. if (ret)
  603. return ret;
  604. /*
  605. * Pin can switch back to the default context if we end up calling into
  606. * evict_everything - as a last ditch gtt defrag effort that also
  607. * switches to the default context. Hence we need to reload from here.
  608. *
  609. * XXX: Doing so is painfully broken!
  610. */
  611. from = engine->last_context;
  612. /*
  613. * Clear this page out of any CPU caches for coherent swap-in/out. Note
  614. * that thanks to write = false in this call and us not setting any gpu
  615. * write domains when putting a context object onto the active list
  616. * (when switching away from it), this won't block.
  617. *
  618. * XXX: We need a real interface to do this instead of trickery.
  619. */
  620. ret = i915_gem_object_set_to_gtt_domain(to->engine[RCS].state, false);
  621. if (ret)
  622. goto unpin_out;
  623. if (needs_pd_load_pre(ppgtt, engine, to)) {
  624. /* Older GENs and non render rings still want the load first,
  625. * "PP_DCLV followed by PP_DIR_BASE register through Load
  626. * Register Immediate commands in Ring Buffer before submitting
  627. * a context."*/
  628. trace_switch_mm(engine, to);
  629. ret = ppgtt->switch_mm(ppgtt, req);
  630. if (ret)
  631. goto unpin_out;
  632. }
  633. if (!to->engine[RCS].initialised || i915_gem_context_is_default(to))
  634. /* NB: If we inhibit the restore, the context is not allowed to
  635. * die because future work may end up depending on valid address
  636. * space. This means we must enforce that a page table load
  637. * occur when this occurs. */
  638. hw_flags = MI_RESTORE_INHIBIT;
  639. else if (ppgtt && intel_engine_flag(engine) & ppgtt->pd_dirty_rings)
  640. hw_flags = MI_FORCE_RESTORE;
  641. else
  642. hw_flags = 0;
  643. if (to != from || (hw_flags & MI_FORCE_RESTORE)) {
  644. ret = mi_set_context(req, hw_flags);
  645. if (ret)
  646. goto unpin_out;
  647. }
  648. /* The backing object for the context is done after switching to the
  649. * *next* context. Therefore we cannot retire the previous context until
  650. * the next context has already started running. In fact, the below code
  651. * is a bit suboptimal because the retiring can occur simply after the
  652. * MI_SET_CONTEXT instead of when the next seqno has completed.
  653. */
  654. if (from != NULL) {
  655. from->engine[RCS].state->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
  656. i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->engine[RCS].state), req);
  657. /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the
  658. * whole damn pipeline, we don't need to explicitly mark the
  659. * object dirty. The only exception is that the context must be
  660. * correct in case the object gets swapped out. Ideally we'd be
  661. * able to defer doing this until we know the object would be
  662. * swapped, but there is no way to do that yet.
  663. */
  664. from->engine[RCS].state->dirty = 1;
  665. /* obj is kept alive until the next request by its active ref */
  666. i915_gem_object_ggtt_unpin(from->engine[RCS].state);
  667. i915_gem_context_unreference(from);
  668. }
  669. i915_gem_context_reference(to);
  670. engine->last_context = to;
  671. /* GEN8 does *not* require an explicit reload if the PDPs have been
  672. * setup, and we do not wish to move them.
  673. */
  674. if (needs_pd_load_post(ppgtt, to, hw_flags)) {
  675. trace_switch_mm(engine, to);
  676. ret = ppgtt->switch_mm(ppgtt, req);
  677. /* The hardware context switch is emitted, but we haven't
  678. * actually changed the state - so it's probably safe to bail
  679. * here. Still, let the user know something dangerous has
  680. * happened.
  681. */
  682. if (ret)
  683. return ret;
  684. }
  685. if (ppgtt)
  686. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  687. for (i = 0; i < MAX_L3_SLICES; i++) {
  688. if (!(to->remap_slice & (1<<i)))
  689. continue;
  690. ret = remap_l3(req, i);
  691. if (ret)
  692. return ret;
  693. to->remap_slice &= ~(1<<i);
  694. }
  695. if (!to->engine[RCS].initialised) {
  696. if (engine->init_context) {
  697. ret = engine->init_context(req);
  698. if (ret)
  699. return ret;
  700. }
  701. to->engine[RCS].initialised = true;
  702. }
  703. return 0;
  704. unpin_out:
  705. i915_gem_object_ggtt_unpin(to->engine[RCS].state);
  706. return ret;
  707. }
  708. /**
  709. * i915_switch_context() - perform a GPU context switch.
  710. * @req: request for which we'll execute the context switch
  711. *
  712. * The context life cycle is simple. The context refcount is incremented and
  713. * decremented by 1 and create and destroy. If the context is in use by the GPU,
  714. * it will have a refcount > 1. This allows us to destroy the context abstract
  715. * object while letting the normal object tracking destroy the backing BO.
  716. *
  717. * This function should not be used in execlists mode. Instead the context is
  718. * switched by writing to the ELSP and requests keep a reference to their
  719. * context.
  720. */
  721. int i915_switch_context(struct drm_i915_gem_request *req)
  722. {
  723. struct intel_engine_cs *engine = req->engine;
  724. WARN_ON(i915.enable_execlists);
  725. lockdep_assert_held(&req->i915->dev->struct_mutex);
  726. if (!req->ctx->engine[engine->id].state) {
  727. struct i915_gem_context *to = req->ctx;
  728. struct i915_hw_ppgtt *ppgtt =
  729. to->ppgtt ?: req->i915->mm.aliasing_ppgtt;
  730. if (needs_pd_load_pre(ppgtt, engine, to)) {
  731. int ret;
  732. trace_switch_mm(engine, to);
  733. ret = ppgtt->switch_mm(ppgtt, req);
  734. if (ret)
  735. return ret;
  736. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  737. }
  738. if (to != engine->last_context) {
  739. i915_gem_context_reference(to);
  740. if (engine->last_context)
  741. i915_gem_context_unreference(engine->last_context);
  742. engine->last_context = to;
  743. }
  744. return 0;
  745. }
  746. return do_rcs_switch(req);
  747. }
  748. static bool contexts_enabled(struct drm_device *dev)
  749. {
  750. return i915.enable_execlists || to_i915(dev)->hw_context_size;
  751. }
  752. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  753. struct drm_file *file)
  754. {
  755. struct drm_i915_gem_context_create *args = data;
  756. struct drm_i915_file_private *file_priv = file->driver_priv;
  757. struct i915_gem_context *ctx;
  758. int ret;
  759. if (!contexts_enabled(dev))
  760. return -ENODEV;
  761. if (args->pad != 0)
  762. return -EINVAL;
  763. ret = i915_mutex_lock_interruptible(dev);
  764. if (ret)
  765. return ret;
  766. ctx = i915_gem_create_context(dev, file_priv);
  767. mutex_unlock(&dev->struct_mutex);
  768. if (IS_ERR(ctx))
  769. return PTR_ERR(ctx);
  770. args->ctx_id = ctx->user_handle;
  771. DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id);
  772. return 0;
  773. }
  774. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  775. struct drm_file *file)
  776. {
  777. struct drm_i915_gem_context_destroy *args = data;
  778. struct drm_i915_file_private *file_priv = file->driver_priv;
  779. struct i915_gem_context *ctx;
  780. int ret;
  781. if (args->pad != 0)
  782. return -EINVAL;
  783. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE)
  784. return -ENOENT;
  785. ret = i915_mutex_lock_interruptible(dev);
  786. if (ret)
  787. return ret;
  788. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  789. if (IS_ERR(ctx)) {
  790. mutex_unlock(&dev->struct_mutex);
  791. return PTR_ERR(ctx);
  792. }
  793. idr_remove(&file_priv->context_idr, ctx->user_handle);
  794. i915_gem_context_unreference(ctx);
  795. mutex_unlock(&dev->struct_mutex);
  796. DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id);
  797. return 0;
  798. }
  799. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  800. struct drm_file *file)
  801. {
  802. struct drm_i915_file_private *file_priv = file->driver_priv;
  803. struct drm_i915_gem_context_param *args = data;
  804. struct i915_gem_context *ctx;
  805. int ret;
  806. ret = i915_mutex_lock_interruptible(dev);
  807. if (ret)
  808. return ret;
  809. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  810. if (IS_ERR(ctx)) {
  811. mutex_unlock(&dev->struct_mutex);
  812. return PTR_ERR(ctx);
  813. }
  814. args->size = 0;
  815. switch (args->param) {
  816. case I915_CONTEXT_PARAM_BAN_PERIOD:
  817. args->value = ctx->hang_stats.ban_period_seconds;
  818. break;
  819. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  820. args->value = ctx->flags & CONTEXT_NO_ZEROMAP;
  821. break;
  822. case I915_CONTEXT_PARAM_GTT_SIZE:
  823. if (ctx->ppgtt)
  824. args->value = ctx->ppgtt->base.total;
  825. else if (to_i915(dev)->mm.aliasing_ppgtt)
  826. args->value = to_i915(dev)->mm.aliasing_ppgtt->base.total;
  827. else
  828. args->value = to_i915(dev)->ggtt.base.total;
  829. break;
  830. default:
  831. ret = -EINVAL;
  832. break;
  833. }
  834. mutex_unlock(&dev->struct_mutex);
  835. return ret;
  836. }
  837. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  838. struct drm_file *file)
  839. {
  840. struct drm_i915_file_private *file_priv = file->driver_priv;
  841. struct drm_i915_gem_context_param *args = data;
  842. struct i915_gem_context *ctx;
  843. int ret;
  844. ret = i915_mutex_lock_interruptible(dev);
  845. if (ret)
  846. return ret;
  847. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  848. if (IS_ERR(ctx)) {
  849. mutex_unlock(&dev->struct_mutex);
  850. return PTR_ERR(ctx);
  851. }
  852. switch (args->param) {
  853. case I915_CONTEXT_PARAM_BAN_PERIOD:
  854. if (args->size)
  855. ret = -EINVAL;
  856. else if (args->value < ctx->hang_stats.ban_period_seconds &&
  857. !capable(CAP_SYS_ADMIN))
  858. ret = -EPERM;
  859. else
  860. ctx->hang_stats.ban_period_seconds = args->value;
  861. break;
  862. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  863. if (args->size) {
  864. ret = -EINVAL;
  865. } else {
  866. ctx->flags &= ~CONTEXT_NO_ZEROMAP;
  867. ctx->flags |= args->value ? CONTEXT_NO_ZEROMAP : 0;
  868. }
  869. break;
  870. default:
  871. ret = -EINVAL;
  872. break;
  873. }
  874. mutex_unlock(&dev->struct_mutex);
  875. return ret;
  876. }
  877. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
  878. void *data, struct drm_file *file)
  879. {
  880. struct drm_i915_private *dev_priv = dev->dev_private;
  881. struct drm_i915_reset_stats *args = data;
  882. struct i915_ctx_hang_stats *hs;
  883. struct i915_gem_context *ctx;
  884. int ret;
  885. if (args->flags || args->pad)
  886. return -EINVAL;
  887. if (args->ctx_id == DEFAULT_CONTEXT_HANDLE && !capable(CAP_SYS_ADMIN))
  888. return -EPERM;
  889. ret = i915_mutex_lock_interruptible(dev);
  890. if (ret)
  891. return ret;
  892. ctx = i915_gem_context_lookup(file->driver_priv, args->ctx_id);
  893. if (IS_ERR(ctx)) {
  894. mutex_unlock(&dev->struct_mutex);
  895. return PTR_ERR(ctx);
  896. }
  897. hs = &ctx->hang_stats;
  898. if (capable(CAP_SYS_ADMIN))
  899. args->reset_count = i915_reset_count(&dev_priv->gpu_error);
  900. else
  901. args->reset_count = 0;
  902. args->batch_active = hs->batch_active;
  903. args->batch_pending = hs->batch_pending;
  904. mutex_unlock(&dev->struct_mutex);
  905. return 0;
  906. }