i915_gem_context.c 31 KB

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