intel_engine_cs.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * Copyright © 2016 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. */
  24. #include <drm/drm_print.h>
  25. #include "i915_drv.h"
  26. #include "i915_vgpu.h"
  27. #include "intel_ringbuffer.h"
  28. #include "intel_lrc.h"
  29. /* Haswell does have the CXT_SIZE register however it does not appear to be
  30. * valid. Now, docs explain in dwords what is in the context object. The full
  31. * size is 70720 bytes, however, the power context and execlist context will
  32. * never be saved (power context is stored elsewhere, and execlists don't work
  33. * on HSW) - so the final size, including the extra state required for the
  34. * Resource Streamer, is 66944 bytes, which rounds to 17 pages.
  35. */
  36. #define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE)
  37. #define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  38. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  39. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  40. #define GEN10_LR_CONTEXT_RENDER_SIZE (18 * PAGE_SIZE)
  41. #define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE)
  42. #define GEN8_LR_CONTEXT_OTHER_SIZE ( 2 * PAGE_SIZE)
  43. struct engine_class_info {
  44. const char *name;
  45. int (*init_legacy)(struct intel_engine_cs *engine);
  46. int (*init_execlists)(struct intel_engine_cs *engine);
  47. u8 uabi_class;
  48. };
  49. static const struct engine_class_info intel_engine_classes[] = {
  50. [RENDER_CLASS] = {
  51. .name = "rcs",
  52. .init_execlists = logical_render_ring_init,
  53. .init_legacy = intel_init_render_ring_buffer,
  54. .uabi_class = I915_ENGINE_CLASS_RENDER,
  55. },
  56. [COPY_ENGINE_CLASS] = {
  57. .name = "bcs",
  58. .init_execlists = logical_xcs_ring_init,
  59. .init_legacy = intel_init_blt_ring_buffer,
  60. .uabi_class = I915_ENGINE_CLASS_COPY,
  61. },
  62. [VIDEO_DECODE_CLASS] = {
  63. .name = "vcs",
  64. .init_execlists = logical_xcs_ring_init,
  65. .init_legacy = intel_init_bsd_ring_buffer,
  66. .uabi_class = I915_ENGINE_CLASS_VIDEO,
  67. },
  68. [VIDEO_ENHANCEMENT_CLASS] = {
  69. .name = "vecs",
  70. .init_execlists = logical_xcs_ring_init,
  71. .init_legacy = intel_init_vebox_ring_buffer,
  72. .uabi_class = I915_ENGINE_CLASS_VIDEO_ENHANCE,
  73. },
  74. };
  75. #define MAX_MMIO_BASES 3
  76. struct engine_info {
  77. unsigned int hw_id;
  78. unsigned int uabi_id;
  79. u8 class;
  80. u8 instance;
  81. /* mmio bases table *must* be sorted in reverse gen order */
  82. struct engine_mmio_base {
  83. u32 gen : 8;
  84. u32 base : 24;
  85. } mmio_bases[MAX_MMIO_BASES];
  86. };
  87. static const struct engine_info intel_engines[] = {
  88. [RCS] = {
  89. .hw_id = RCS_HW,
  90. .uabi_id = I915_EXEC_RENDER,
  91. .class = RENDER_CLASS,
  92. .instance = 0,
  93. .mmio_bases = {
  94. { .gen = 1, .base = RENDER_RING_BASE }
  95. },
  96. },
  97. [BCS] = {
  98. .hw_id = BCS_HW,
  99. .uabi_id = I915_EXEC_BLT,
  100. .class = COPY_ENGINE_CLASS,
  101. .instance = 0,
  102. .mmio_bases = {
  103. { .gen = 6, .base = BLT_RING_BASE }
  104. },
  105. },
  106. [VCS] = {
  107. .hw_id = VCS_HW,
  108. .uabi_id = I915_EXEC_BSD,
  109. .class = VIDEO_DECODE_CLASS,
  110. .instance = 0,
  111. .mmio_bases = {
  112. { .gen = 11, .base = GEN11_BSD_RING_BASE },
  113. { .gen = 6, .base = GEN6_BSD_RING_BASE },
  114. { .gen = 4, .base = BSD_RING_BASE }
  115. },
  116. },
  117. [VCS2] = {
  118. .hw_id = VCS2_HW,
  119. .uabi_id = I915_EXEC_BSD,
  120. .class = VIDEO_DECODE_CLASS,
  121. .instance = 1,
  122. .mmio_bases = {
  123. { .gen = 11, .base = GEN11_BSD2_RING_BASE },
  124. { .gen = 8, .base = GEN8_BSD2_RING_BASE }
  125. },
  126. },
  127. [VCS3] = {
  128. .hw_id = VCS3_HW,
  129. .uabi_id = I915_EXEC_BSD,
  130. .class = VIDEO_DECODE_CLASS,
  131. .instance = 2,
  132. .mmio_bases = {
  133. { .gen = 11, .base = GEN11_BSD3_RING_BASE }
  134. },
  135. },
  136. [VCS4] = {
  137. .hw_id = VCS4_HW,
  138. .uabi_id = I915_EXEC_BSD,
  139. .class = VIDEO_DECODE_CLASS,
  140. .instance = 3,
  141. .mmio_bases = {
  142. { .gen = 11, .base = GEN11_BSD4_RING_BASE }
  143. },
  144. },
  145. [VECS] = {
  146. .hw_id = VECS_HW,
  147. .uabi_id = I915_EXEC_VEBOX,
  148. .class = VIDEO_ENHANCEMENT_CLASS,
  149. .instance = 0,
  150. .mmio_bases = {
  151. { .gen = 11, .base = GEN11_VEBOX_RING_BASE },
  152. { .gen = 7, .base = VEBOX_RING_BASE }
  153. },
  154. },
  155. [VECS2] = {
  156. .hw_id = VECS2_HW,
  157. .uabi_id = I915_EXEC_VEBOX,
  158. .class = VIDEO_ENHANCEMENT_CLASS,
  159. .instance = 1,
  160. .mmio_bases = {
  161. { .gen = 11, .base = GEN11_VEBOX2_RING_BASE }
  162. },
  163. },
  164. };
  165. /**
  166. * ___intel_engine_context_size() - return the size of the context for an engine
  167. * @dev_priv: i915 device private
  168. * @class: engine class
  169. *
  170. * Each engine class may require a different amount of space for a context
  171. * image.
  172. *
  173. * Return: size (in bytes) of an engine class specific context image
  174. *
  175. * Note: this size includes the HWSP, which is part of the context image
  176. * in LRC mode, but does not include the "shared data page" used with
  177. * GuC submission. The caller should account for this if using the GuC.
  178. */
  179. static u32
  180. __intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
  181. {
  182. u32 cxt_size;
  183. BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
  184. switch (class) {
  185. case RENDER_CLASS:
  186. switch (INTEL_GEN(dev_priv)) {
  187. default:
  188. MISSING_CASE(INTEL_GEN(dev_priv));
  189. return DEFAULT_LR_CONTEXT_RENDER_SIZE;
  190. case 11:
  191. return GEN11_LR_CONTEXT_RENDER_SIZE;
  192. case 10:
  193. return GEN10_LR_CONTEXT_RENDER_SIZE;
  194. case 9:
  195. return GEN9_LR_CONTEXT_RENDER_SIZE;
  196. case 8:
  197. return GEN8_LR_CONTEXT_RENDER_SIZE;
  198. case 7:
  199. if (IS_HASWELL(dev_priv))
  200. return HSW_CXT_TOTAL_SIZE;
  201. cxt_size = I915_READ(GEN7_CXT_SIZE);
  202. return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
  203. PAGE_SIZE);
  204. case 6:
  205. cxt_size = I915_READ(CXT_SIZE);
  206. return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
  207. PAGE_SIZE);
  208. case 5:
  209. case 4:
  210. case 3:
  211. case 2:
  212. /* For the special day when i810 gets merged. */
  213. case 1:
  214. return 0;
  215. }
  216. break;
  217. default:
  218. MISSING_CASE(class);
  219. case VIDEO_DECODE_CLASS:
  220. case VIDEO_ENHANCEMENT_CLASS:
  221. case COPY_ENGINE_CLASS:
  222. if (INTEL_GEN(dev_priv) < 8)
  223. return 0;
  224. return GEN8_LR_CONTEXT_OTHER_SIZE;
  225. }
  226. }
  227. static u32 __engine_mmio_base(struct drm_i915_private *i915,
  228. const struct engine_mmio_base *bases)
  229. {
  230. int i;
  231. for (i = 0; i < MAX_MMIO_BASES; i++)
  232. if (INTEL_GEN(i915) >= bases[i].gen)
  233. break;
  234. GEM_BUG_ON(i == MAX_MMIO_BASES);
  235. GEM_BUG_ON(!bases[i].base);
  236. return bases[i].base;
  237. }
  238. static void __sprint_engine_name(char *name, const struct engine_info *info)
  239. {
  240. WARN_ON(snprintf(name, INTEL_ENGINE_CS_MAX_NAME, "%s%u",
  241. intel_engine_classes[info->class].name,
  242. info->instance) >= INTEL_ENGINE_CS_MAX_NAME);
  243. }
  244. static int
  245. intel_engine_setup(struct drm_i915_private *dev_priv,
  246. enum intel_engine_id id)
  247. {
  248. const struct engine_info *info = &intel_engines[id];
  249. struct intel_engine_cs *engine;
  250. GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
  251. BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
  252. BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
  253. if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
  254. return -EINVAL;
  255. if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
  256. return -EINVAL;
  257. if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
  258. return -EINVAL;
  259. GEM_BUG_ON(dev_priv->engine[id]);
  260. engine = kzalloc(sizeof(*engine), GFP_KERNEL);
  261. if (!engine)
  262. return -ENOMEM;
  263. engine->id = id;
  264. engine->i915 = dev_priv;
  265. __sprint_engine_name(engine->name, info);
  266. engine->hw_id = engine->guc_id = info->hw_id;
  267. engine->mmio_base = __engine_mmio_base(dev_priv, info->mmio_bases);
  268. engine->class = info->class;
  269. engine->instance = info->instance;
  270. engine->uabi_id = info->uabi_id;
  271. engine->uabi_class = intel_engine_classes[info->class].uabi_class;
  272. engine->context_size = __intel_engine_context_size(dev_priv,
  273. engine->class);
  274. if (WARN_ON(engine->context_size > BIT(20)))
  275. engine->context_size = 0;
  276. /* Nothing to do here, execute in order of dependencies */
  277. engine->schedule = NULL;
  278. seqlock_init(&engine->stats.lock);
  279. ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
  280. dev_priv->engine_class[info->class][info->instance] = engine;
  281. dev_priv->engine[id] = engine;
  282. return 0;
  283. }
  284. /**
  285. * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
  286. * @dev_priv: i915 device private
  287. *
  288. * Return: non-zero if the initialization failed.
  289. */
  290. int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
  291. {
  292. struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
  293. const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
  294. struct intel_engine_cs *engine;
  295. enum intel_engine_id id;
  296. unsigned int mask = 0;
  297. unsigned int i;
  298. int err;
  299. WARN_ON(ring_mask == 0);
  300. WARN_ON(ring_mask &
  301. GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
  302. for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
  303. if (!HAS_ENGINE(dev_priv, i))
  304. continue;
  305. err = intel_engine_setup(dev_priv, i);
  306. if (err)
  307. goto cleanup;
  308. mask |= ENGINE_MASK(i);
  309. }
  310. /*
  311. * Catch failures to update intel_engines table when the new engines
  312. * are added to the driver by a warning and disabling the forgotten
  313. * engines.
  314. */
  315. if (WARN_ON(mask != ring_mask))
  316. device_info->ring_mask = mask;
  317. /* We always presume we have at least RCS available for later probing */
  318. if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
  319. err = -ENODEV;
  320. goto cleanup;
  321. }
  322. device_info->num_rings = hweight32(mask);
  323. i915_check_and_clear_faults(dev_priv);
  324. return 0;
  325. cleanup:
  326. for_each_engine(engine, dev_priv, id)
  327. kfree(engine);
  328. return err;
  329. }
  330. /**
  331. * intel_engines_init() - init the Engine Command Streamers
  332. * @dev_priv: i915 device private
  333. *
  334. * Return: non-zero if the initialization failed.
  335. */
  336. int intel_engines_init(struct drm_i915_private *dev_priv)
  337. {
  338. struct intel_engine_cs *engine;
  339. enum intel_engine_id id, err_id;
  340. int err;
  341. for_each_engine(engine, dev_priv, id) {
  342. const struct engine_class_info *class_info =
  343. &intel_engine_classes[engine->class];
  344. int (*init)(struct intel_engine_cs *engine);
  345. if (HAS_EXECLISTS(dev_priv))
  346. init = class_info->init_execlists;
  347. else
  348. init = class_info->init_legacy;
  349. err = -EINVAL;
  350. err_id = id;
  351. if (GEM_WARN_ON(!init))
  352. goto cleanup;
  353. err = init(engine);
  354. if (err)
  355. goto cleanup;
  356. GEM_BUG_ON(!engine->submit_request);
  357. }
  358. return 0;
  359. cleanup:
  360. for_each_engine(engine, dev_priv, id) {
  361. if (id >= err_id) {
  362. kfree(engine);
  363. dev_priv->engine[id] = NULL;
  364. } else {
  365. dev_priv->gt.cleanup_engine(engine);
  366. }
  367. }
  368. return err;
  369. }
  370. void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
  371. {
  372. struct drm_i915_private *dev_priv = engine->i915;
  373. /* Our semaphore implementation is strictly monotonic (i.e. we proceed
  374. * so long as the semaphore value in the register/page is greater
  375. * than the sync value), so whenever we reset the seqno,
  376. * so long as we reset the tracking semaphore value to 0, it will
  377. * always be before the next request's seqno. If we don't reset
  378. * the semaphore value, then when the seqno moves backwards all
  379. * future waits will complete instantly (causing rendering corruption).
  380. */
  381. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  382. I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
  383. I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
  384. if (HAS_VEBOX(dev_priv))
  385. I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
  386. }
  387. intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
  388. clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  389. /* After manually advancing the seqno, fake the interrupt in case
  390. * there are any waiters for that seqno.
  391. */
  392. intel_engine_wakeup(engine);
  393. GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
  394. }
  395. static void intel_engine_init_batch_pool(struct intel_engine_cs *engine)
  396. {
  397. i915_gem_batch_pool_init(&engine->batch_pool, engine);
  398. }
  399. static bool csb_force_mmio(struct drm_i915_private *i915)
  400. {
  401. /* Older GVT emulation depends upon intercepting CSB mmio */
  402. if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
  403. return true;
  404. return false;
  405. }
  406. static void intel_engine_init_execlist(struct intel_engine_cs *engine)
  407. {
  408. struct intel_engine_execlists * const execlists = &engine->execlists;
  409. execlists->csb_use_mmio = csb_force_mmio(engine->i915);
  410. execlists->port_mask = 1;
  411. BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
  412. GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
  413. execlists->queue_priority = INT_MIN;
  414. execlists->queue = RB_ROOT;
  415. execlists->first = NULL;
  416. }
  417. /**
  418. * intel_engines_setup_common - setup engine state not requiring hw access
  419. * @engine: Engine to setup.
  420. *
  421. * Initializes @engine@ structure members shared between legacy and execlists
  422. * submission modes which do not require hardware access.
  423. *
  424. * Typically done early in the submission mode specific engine setup stage.
  425. */
  426. void intel_engine_setup_common(struct intel_engine_cs *engine)
  427. {
  428. i915_timeline_init(engine->i915, &engine->timeline, engine->name);
  429. intel_engine_init_execlist(engine);
  430. intel_engine_init_hangcheck(engine);
  431. intel_engine_init_batch_pool(engine);
  432. intel_engine_init_cmd_parser(engine);
  433. }
  434. int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
  435. {
  436. struct drm_i915_gem_object *obj;
  437. struct i915_vma *vma;
  438. int ret;
  439. WARN_ON(engine->scratch);
  440. obj = i915_gem_object_create_stolen(engine->i915, size);
  441. if (!obj)
  442. obj = i915_gem_object_create_internal(engine->i915, size);
  443. if (IS_ERR(obj)) {
  444. DRM_ERROR("Failed to allocate scratch page\n");
  445. return PTR_ERR(obj);
  446. }
  447. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  448. if (IS_ERR(vma)) {
  449. ret = PTR_ERR(vma);
  450. goto err_unref;
  451. }
  452. ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
  453. if (ret)
  454. goto err_unref;
  455. engine->scratch = vma;
  456. return 0;
  457. err_unref:
  458. i915_gem_object_put(obj);
  459. return ret;
  460. }
  461. static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
  462. {
  463. i915_vma_unpin_and_release(&engine->scratch);
  464. }
  465. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  466. {
  467. struct drm_i915_private *dev_priv = engine->i915;
  468. if (!dev_priv->status_page_dmah)
  469. return;
  470. drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
  471. engine->status_page.page_addr = NULL;
  472. }
  473. static void cleanup_status_page(struct intel_engine_cs *engine)
  474. {
  475. struct i915_vma *vma;
  476. struct drm_i915_gem_object *obj;
  477. vma = fetch_and_zero(&engine->status_page.vma);
  478. if (!vma)
  479. return;
  480. obj = vma->obj;
  481. i915_vma_unpin(vma);
  482. i915_vma_close(vma);
  483. i915_gem_object_unpin_map(obj);
  484. __i915_gem_object_release_unless_active(obj);
  485. }
  486. static int init_status_page(struct intel_engine_cs *engine)
  487. {
  488. struct drm_i915_gem_object *obj;
  489. struct i915_vma *vma;
  490. unsigned int flags;
  491. void *vaddr;
  492. int ret;
  493. obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  494. if (IS_ERR(obj)) {
  495. DRM_ERROR("Failed to allocate status page\n");
  496. return PTR_ERR(obj);
  497. }
  498. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  499. if (ret)
  500. goto err;
  501. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  502. if (IS_ERR(vma)) {
  503. ret = PTR_ERR(vma);
  504. goto err;
  505. }
  506. flags = PIN_GLOBAL;
  507. if (!HAS_LLC(engine->i915))
  508. /* On g33, we cannot place HWS above 256MiB, so
  509. * restrict its pinning to the low mappable arena.
  510. * Though this restriction is not documented for
  511. * gen4, gen5, or byt, they also behave similarly
  512. * and hang if the HWS is placed at the top of the
  513. * GTT. To generalise, it appears that all !llc
  514. * platforms have issues with us placing the HWS
  515. * above the mappable region (even though we never
  516. * actually map it).
  517. */
  518. flags |= PIN_MAPPABLE;
  519. else
  520. flags |= PIN_HIGH;
  521. ret = i915_vma_pin(vma, 0, 4096, flags);
  522. if (ret)
  523. goto err;
  524. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  525. if (IS_ERR(vaddr)) {
  526. ret = PTR_ERR(vaddr);
  527. goto err_unpin;
  528. }
  529. engine->status_page.vma = vma;
  530. engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
  531. engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
  532. return 0;
  533. err_unpin:
  534. i915_vma_unpin(vma);
  535. err:
  536. i915_gem_object_put(obj);
  537. return ret;
  538. }
  539. static int init_phys_status_page(struct intel_engine_cs *engine)
  540. {
  541. struct drm_i915_private *dev_priv = engine->i915;
  542. GEM_BUG_ON(engine->id != RCS);
  543. dev_priv->status_page_dmah =
  544. drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
  545. if (!dev_priv->status_page_dmah)
  546. return -ENOMEM;
  547. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  548. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  549. return 0;
  550. }
  551. /**
  552. * intel_engines_init_common - initialize cengine state which might require hw access
  553. * @engine: Engine to initialize.
  554. *
  555. * Initializes @engine@ structure members shared between legacy and execlists
  556. * submission modes which do require hardware access.
  557. *
  558. * Typcally done at later stages of submission mode specific engine setup.
  559. *
  560. * Returns zero on success or an error code on failure.
  561. */
  562. int intel_engine_init_common(struct intel_engine_cs *engine)
  563. {
  564. struct intel_ring *ring;
  565. int ret;
  566. engine->set_default_submission(engine);
  567. /* We may need to do things with the shrinker which
  568. * require us to immediately switch back to the default
  569. * context. This can cause a problem as pinning the
  570. * default context also requires GTT space which may not
  571. * be available. To avoid this we always pin the default
  572. * context.
  573. */
  574. ring = intel_context_pin(engine->i915->kernel_context, engine);
  575. if (IS_ERR(ring))
  576. return PTR_ERR(ring);
  577. /*
  578. * Similarly the preempt context must always be available so that
  579. * we can interrupt the engine at any time.
  580. */
  581. if (engine->i915->preempt_context) {
  582. ring = intel_context_pin(engine->i915->preempt_context, engine);
  583. if (IS_ERR(ring)) {
  584. ret = PTR_ERR(ring);
  585. goto err_unpin_kernel;
  586. }
  587. }
  588. ret = intel_engine_init_breadcrumbs(engine);
  589. if (ret)
  590. goto err_unpin_preempt;
  591. if (HWS_NEEDS_PHYSICAL(engine->i915))
  592. ret = init_phys_status_page(engine);
  593. else
  594. ret = init_status_page(engine);
  595. if (ret)
  596. goto err_breadcrumbs;
  597. return 0;
  598. err_breadcrumbs:
  599. intel_engine_fini_breadcrumbs(engine);
  600. err_unpin_preempt:
  601. if (engine->i915->preempt_context)
  602. intel_context_unpin(engine->i915->preempt_context, engine);
  603. err_unpin_kernel:
  604. intel_context_unpin(engine->i915->kernel_context, engine);
  605. return ret;
  606. }
  607. /**
  608. * intel_engines_cleanup_common - cleans up the engine state created by
  609. * the common initiailizers.
  610. * @engine: Engine to cleanup.
  611. *
  612. * This cleans up everything created by the common helpers.
  613. */
  614. void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  615. {
  616. intel_engine_cleanup_scratch(engine);
  617. if (HWS_NEEDS_PHYSICAL(engine->i915))
  618. cleanup_phys_status_page(engine);
  619. else
  620. cleanup_status_page(engine);
  621. intel_engine_fini_breadcrumbs(engine);
  622. intel_engine_cleanup_cmd_parser(engine);
  623. i915_gem_batch_pool_fini(&engine->batch_pool);
  624. if (engine->default_state)
  625. i915_gem_object_put(engine->default_state);
  626. if (engine->i915->preempt_context)
  627. intel_context_unpin(engine->i915->preempt_context, engine);
  628. intel_context_unpin(engine->i915->kernel_context, engine);
  629. i915_timeline_fini(&engine->timeline);
  630. }
  631. u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
  632. {
  633. struct drm_i915_private *dev_priv = engine->i915;
  634. u64 acthd;
  635. if (INTEL_GEN(dev_priv) >= 8)
  636. acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
  637. RING_ACTHD_UDW(engine->mmio_base));
  638. else if (INTEL_GEN(dev_priv) >= 4)
  639. acthd = I915_READ(RING_ACTHD(engine->mmio_base));
  640. else
  641. acthd = I915_READ(ACTHD);
  642. return acthd;
  643. }
  644. u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
  645. {
  646. struct drm_i915_private *dev_priv = engine->i915;
  647. u64 bbaddr;
  648. if (INTEL_GEN(dev_priv) >= 8)
  649. bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
  650. RING_BBADDR_UDW(engine->mmio_base));
  651. else
  652. bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
  653. return bbaddr;
  654. }
  655. const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
  656. {
  657. switch (type) {
  658. case I915_CACHE_NONE: return " uncached";
  659. case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
  660. case I915_CACHE_L3_LLC: return " L3+LLC";
  661. case I915_CACHE_WT: return " WT";
  662. default: return "";
  663. }
  664. }
  665. static inline uint32_t
  666. read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
  667. int subslice, i915_reg_t reg)
  668. {
  669. uint32_t mcr_slice_subslice_mask;
  670. uint32_t mcr_slice_subslice_select;
  671. uint32_t mcr;
  672. uint32_t ret;
  673. enum forcewake_domains fw_domains;
  674. if (INTEL_GEN(dev_priv) >= 11) {
  675. mcr_slice_subslice_mask = GEN11_MCR_SLICE_MASK |
  676. GEN11_MCR_SUBSLICE_MASK;
  677. mcr_slice_subslice_select = GEN11_MCR_SLICE(slice) |
  678. GEN11_MCR_SUBSLICE(subslice);
  679. } else {
  680. mcr_slice_subslice_mask = GEN8_MCR_SLICE_MASK |
  681. GEN8_MCR_SUBSLICE_MASK;
  682. mcr_slice_subslice_select = GEN8_MCR_SLICE(slice) |
  683. GEN8_MCR_SUBSLICE(subslice);
  684. }
  685. fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
  686. FW_REG_READ);
  687. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  688. GEN8_MCR_SELECTOR,
  689. FW_REG_READ | FW_REG_WRITE);
  690. spin_lock_irq(&dev_priv->uncore.lock);
  691. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  692. mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
  693. /*
  694. * The HW expects the slice and sublice selectors to be reset to 0
  695. * after reading out the registers.
  696. */
  697. WARN_ON_ONCE(mcr & mcr_slice_subslice_mask);
  698. mcr &= ~mcr_slice_subslice_mask;
  699. mcr |= mcr_slice_subslice_select;
  700. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  701. ret = I915_READ_FW(reg);
  702. mcr &= ~mcr_slice_subslice_mask;
  703. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  704. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  705. spin_unlock_irq(&dev_priv->uncore.lock);
  706. return ret;
  707. }
  708. /* NB: please notice the memset */
  709. void intel_engine_get_instdone(struct intel_engine_cs *engine,
  710. struct intel_instdone *instdone)
  711. {
  712. struct drm_i915_private *dev_priv = engine->i915;
  713. u32 mmio_base = engine->mmio_base;
  714. int slice;
  715. int subslice;
  716. memset(instdone, 0, sizeof(*instdone));
  717. switch (INTEL_GEN(dev_priv)) {
  718. default:
  719. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  720. if (engine->id != RCS)
  721. break;
  722. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  723. for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
  724. instdone->sampler[slice][subslice] =
  725. read_subslice_reg(dev_priv, slice, subslice,
  726. GEN7_SAMPLER_INSTDONE);
  727. instdone->row[slice][subslice] =
  728. read_subslice_reg(dev_priv, slice, subslice,
  729. GEN7_ROW_INSTDONE);
  730. }
  731. break;
  732. case 7:
  733. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  734. if (engine->id != RCS)
  735. break;
  736. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  737. instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
  738. instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
  739. break;
  740. case 6:
  741. case 5:
  742. case 4:
  743. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  744. if (engine->id == RCS)
  745. /* HACK: Using the wrong struct member */
  746. instdone->slice_common = I915_READ(GEN4_INSTDONE1);
  747. break;
  748. case 3:
  749. case 2:
  750. instdone->instdone = I915_READ(GEN2_INSTDONE);
  751. break;
  752. }
  753. }
  754. static bool ring_is_idle(struct intel_engine_cs *engine)
  755. {
  756. struct drm_i915_private *dev_priv = engine->i915;
  757. bool idle = true;
  758. /* If the whole device is asleep, the engine must be idle */
  759. if (!intel_runtime_pm_get_if_in_use(dev_priv))
  760. return true;
  761. /* First check that no commands are left in the ring */
  762. if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
  763. (I915_READ_TAIL(engine) & TAIL_ADDR))
  764. idle = false;
  765. /* No bit for gen2, so assume the CS parser is idle */
  766. if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
  767. idle = false;
  768. intel_runtime_pm_put(dev_priv);
  769. return idle;
  770. }
  771. /**
  772. * intel_engine_is_idle() - Report if the engine has finished process all work
  773. * @engine: the intel_engine_cs
  774. *
  775. * Return true if there are no requests pending, nothing left to be submitted
  776. * to hardware, and that the engine is idle.
  777. */
  778. bool intel_engine_is_idle(struct intel_engine_cs *engine)
  779. {
  780. struct drm_i915_private *dev_priv = engine->i915;
  781. /* More white lies, if wedged, hw state is inconsistent */
  782. if (i915_terminally_wedged(&dev_priv->gpu_error))
  783. return true;
  784. /* Any inflight/incomplete requests? */
  785. if (!i915_seqno_passed(intel_engine_get_seqno(engine),
  786. intel_engine_last_submit(engine)))
  787. return false;
  788. if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
  789. return true;
  790. /* Waiting to drain ELSP? */
  791. if (READ_ONCE(engine->execlists.active))
  792. return false;
  793. /* ELSP is empty, but there are ready requests? */
  794. if (READ_ONCE(engine->execlists.first))
  795. return false;
  796. /* Ring stopped? */
  797. if (!ring_is_idle(engine))
  798. return false;
  799. return true;
  800. }
  801. bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
  802. {
  803. struct intel_engine_cs *engine;
  804. enum intel_engine_id id;
  805. /*
  806. * If the driver is wedged, HW state may be very inconsistent and
  807. * report that it is still busy, even though we have stopped using it.
  808. */
  809. if (i915_terminally_wedged(&dev_priv->gpu_error))
  810. return true;
  811. for_each_engine(engine, dev_priv, id) {
  812. if (!intel_engine_is_idle(engine))
  813. return false;
  814. }
  815. return true;
  816. }
  817. /**
  818. * intel_engine_has_kernel_context:
  819. * @engine: the engine
  820. *
  821. * Returns true if the last context to be executed on this engine, or has been
  822. * executed if the engine is already idle, is the kernel context
  823. * (#i915.kernel_context).
  824. */
  825. bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
  826. {
  827. const struct i915_gem_context * const kernel_context =
  828. engine->i915->kernel_context;
  829. struct i915_request *rq;
  830. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  831. /*
  832. * Check the last context seen by the engine. If active, it will be
  833. * the last request that remains in the timeline. When idle, it is
  834. * the last executed context as tracked by retirement.
  835. */
  836. rq = __i915_gem_active_peek(&engine->timeline.last_request);
  837. if (rq)
  838. return rq->ctx == kernel_context;
  839. else
  840. return engine->last_retired_context == kernel_context;
  841. }
  842. void intel_engines_reset_default_submission(struct drm_i915_private *i915)
  843. {
  844. struct intel_engine_cs *engine;
  845. enum intel_engine_id id;
  846. for_each_engine(engine, i915, id)
  847. engine->set_default_submission(engine);
  848. }
  849. /**
  850. * intel_engines_park: called when the GT is transitioning from busy->idle
  851. * @i915: the i915 device
  852. *
  853. * The GT is now idle and about to go to sleep (maybe never to wake again?).
  854. * Time for us to tidy and put away our toys (release resources back to the
  855. * system).
  856. */
  857. void intel_engines_park(struct drm_i915_private *i915)
  858. {
  859. struct intel_engine_cs *engine;
  860. enum intel_engine_id id;
  861. for_each_engine(engine, i915, id) {
  862. /* Flush the residual irq tasklets first. */
  863. intel_engine_disarm_breadcrumbs(engine);
  864. tasklet_kill(&engine->execlists.tasklet);
  865. /*
  866. * We are committed now to parking the engines, make sure there
  867. * will be no more interrupts arriving later and the engines
  868. * are truly idle.
  869. */
  870. if (wait_for(intel_engine_is_idle(engine), 10)) {
  871. struct drm_printer p = drm_debug_printer(__func__);
  872. dev_err(i915->drm.dev,
  873. "%s is not idle before parking\n",
  874. engine->name);
  875. intel_engine_dump(engine, &p, NULL);
  876. }
  877. /* Must be reset upon idling, or we may miss the busy wakeup. */
  878. GEM_BUG_ON(engine->execlists.queue_priority != INT_MIN);
  879. if (engine->park)
  880. engine->park(engine);
  881. i915_gem_batch_pool_fini(&engine->batch_pool);
  882. engine->execlists.no_priolist = false;
  883. }
  884. }
  885. /**
  886. * intel_engines_unpark: called when the GT is transitioning from idle->busy
  887. * @i915: the i915 device
  888. *
  889. * The GT was idle and now about to fire up with some new user requests.
  890. */
  891. void intel_engines_unpark(struct drm_i915_private *i915)
  892. {
  893. struct intel_engine_cs *engine;
  894. enum intel_engine_id id;
  895. for_each_engine(engine, i915, id) {
  896. if (engine->unpark)
  897. engine->unpark(engine);
  898. intel_engine_init_hangcheck(engine);
  899. }
  900. }
  901. bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
  902. {
  903. switch (INTEL_GEN(engine->i915)) {
  904. case 2:
  905. return false; /* uses physical not virtual addresses */
  906. case 3:
  907. /* maybe only uses physical not virtual addresses */
  908. return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
  909. case 6:
  910. return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
  911. default:
  912. return true;
  913. }
  914. }
  915. unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
  916. {
  917. struct intel_engine_cs *engine;
  918. enum intel_engine_id id;
  919. unsigned int which;
  920. which = 0;
  921. for_each_engine(engine, i915, id)
  922. if (engine->default_state)
  923. which |= BIT(engine->uabi_class);
  924. return which;
  925. }
  926. static int print_sched_attr(struct drm_i915_private *i915,
  927. const struct i915_sched_attr *attr,
  928. char *buf, int x, int len)
  929. {
  930. if (attr->priority == I915_PRIORITY_INVALID)
  931. return x;
  932. x += snprintf(buf + x, len - x,
  933. " prio=%d", attr->priority);
  934. return x;
  935. }
  936. static void print_request(struct drm_printer *m,
  937. struct i915_request *rq,
  938. const char *prefix)
  939. {
  940. const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
  941. char buf[80] = "";
  942. int x = 0;
  943. x = print_sched_attr(rq->i915, &rq->sched.attr, buf, x, sizeof(buf));
  944. drm_printf(m, "%s%x%s [%llx:%x]%s @ %dms: %s\n",
  945. prefix,
  946. rq->global_seqno,
  947. i915_request_completed(rq) ? "!" : "",
  948. rq->fence.context, rq->fence.seqno,
  949. buf,
  950. jiffies_to_msecs(jiffies - rq->emitted_jiffies),
  951. name);
  952. }
  953. static void hexdump(struct drm_printer *m, const void *buf, size_t len)
  954. {
  955. const size_t rowsize = 8 * sizeof(u32);
  956. const void *prev = NULL;
  957. bool skip = false;
  958. size_t pos;
  959. for (pos = 0; pos < len; pos += rowsize) {
  960. char line[128];
  961. if (prev && !memcmp(prev, buf + pos, rowsize)) {
  962. if (!skip) {
  963. drm_printf(m, "*\n");
  964. skip = true;
  965. }
  966. continue;
  967. }
  968. WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
  969. rowsize, sizeof(u32),
  970. line, sizeof(line),
  971. false) >= sizeof(line));
  972. drm_printf(m, "%08zx %s\n", pos, line);
  973. prev = buf + pos;
  974. skip = false;
  975. }
  976. }
  977. static void intel_engine_print_registers(const struct intel_engine_cs *engine,
  978. struct drm_printer *m)
  979. {
  980. struct drm_i915_private *dev_priv = engine->i915;
  981. const struct intel_engine_execlists * const execlists =
  982. &engine->execlists;
  983. u64 addr;
  984. drm_printf(m, "\tRING_START: 0x%08x\n",
  985. I915_READ(RING_START(engine->mmio_base)));
  986. drm_printf(m, "\tRING_HEAD: 0x%08x\n",
  987. I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
  988. drm_printf(m, "\tRING_TAIL: 0x%08x\n",
  989. I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
  990. drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
  991. I915_READ(RING_CTL(engine->mmio_base)),
  992. I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
  993. if (INTEL_GEN(engine->i915) > 2) {
  994. drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
  995. I915_READ(RING_MI_MODE(engine->mmio_base)),
  996. I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
  997. }
  998. if (INTEL_GEN(dev_priv) >= 6) {
  999. drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
  1000. }
  1001. if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
  1002. drm_printf(m, "\tSYNC_0: 0x%08x\n",
  1003. I915_READ(RING_SYNC_0(engine->mmio_base)));
  1004. drm_printf(m, "\tSYNC_1: 0x%08x\n",
  1005. I915_READ(RING_SYNC_1(engine->mmio_base)));
  1006. if (HAS_VEBOX(dev_priv))
  1007. drm_printf(m, "\tSYNC_2: 0x%08x\n",
  1008. I915_READ(RING_SYNC_2(engine->mmio_base)));
  1009. }
  1010. addr = intel_engine_get_active_head(engine);
  1011. drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
  1012. upper_32_bits(addr), lower_32_bits(addr));
  1013. addr = intel_engine_get_last_batch_head(engine);
  1014. drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
  1015. upper_32_bits(addr), lower_32_bits(addr));
  1016. if (INTEL_GEN(dev_priv) >= 8)
  1017. addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
  1018. RING_DMA_FADD_UDW(engine->mmio_base));
  1019. else if (INTEL_GEN(dev_priv) >= 4)
  1020. addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
  1021. else
  1022. addr = I915_READ(DMA_FADD_I8XX);
  1023. drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
  1024. upper_32_bits(addr), lower_32_bits(addr));
  1025. if (INTEL_GEN(dev_priv) >= 4) {
  1026. drm_printf(m, "\tIPEIR: 0x%08x\n",
  1027. I915_READ(RING_IPEIR(engine->mmio_base)));
  1028. drm_printf(m, "\tIPEHR: 0x%08x\n",
  1029. I915_READ(RING_IPEHR(engine->mmio_base)));
  1030. } else {
  1031. drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
  1032. drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
  1033. }
  1034. if (HAS_EXECLISTS(dev_priv)) {
  1035. const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
  1036. u32 ptr, read, write;
  1037. unsigned int idx;
  1038. drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
  1039. I915_READ(RING_EXECLIST_STATUS_LO(engine)),
  1040. I915_READ(RING_EXECLIST_STATUS_HI(engine)));
  1041. ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
  1042. read = GEN8_CSB_READ_PTR(ptr);
  1043. write = GEN8_CSB_WRITE_PTR(ptr);
  1044. drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s, tasklet queued? %s (%s)\n",
  1045. read, execlists->csb_head,
  1046. write,
  1047. intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
  1048. yesno(test_bit(ENGINE_IRQ_EXECLIST,
  1049. &engine->irq_posted)),
  1050. yesno(test_bit(TASKLET_STATE_SCHED,
  1051. &engine->execlists.tasklet.state)),
  1052. enableddisabled(!atomic_read(&engine->execlists.tasklet.count)));
  1053. if (read >= GEN8_CSB_ENTRIES)
  1054. read = 0;
  1055. if (write >= GEN8_CSB_ENTRIES)
  1056. write = 0;
  1057. if (read > write)
  1058. write += GEN8_CSB_ENTRIES;
  1059. while (read < write) {
  1060. idx = ++read % GEN8_CSB_ENTRIES;
  1061. drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
  1062. idx,
  1063. I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
  1064. hws[idx * 2],
  1065. I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
  1066. hws[idx * 2 + 1]);
  1067. }
  1068. rcu_read_lock();
  1069. for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
  1070. struct i915_request *rq;
  1071. unsigned int count;
  1072. rq = port_unpack(&execlists->port[idx], &count);
  1073. if (rq) {
  1074. char hdr[80];
  1075. snprintf(hdr, sizeof(hdr),
  1076. "\t\tELSP[%d] count=%d, ring->start=%08x, rq: ",
  1077. idx, count,
  1078. i915_ggtt_offset(rq->ring->vma));
  1079. print_request(m, rq, hdr);
  1080. } else {
  1081. drm_printf(m, "\t\tELSP[%d] idle\n", idx);
  1082. }
  1083. }
  1084. drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
  1085. rcu_read_unlock();
  1086. } else if (INTEL_GEN(dev_priv) > 6) {
  1087. drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
  1088. I915_READ(RING_PP_DIR_BASE(engine)));
  1089. drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
  1090. I915_READ(RING_PP_DIR_BASE_READ(engine)));
  1091. drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
  1092. I915_READ(RING_PP_DIR_DCLV(engine)));
  1093. }
  1094. }
  1095. void intel_engine_dump(struct intel_engine_cs *engine,
  1096. struct drm_printer *m,
  1097. const char *header, ...)
  1098. {
  1099. const int MAX_REQUESTS_TO_SHOW = 8;
  1100. struct intel_breadcrumbs * const b = &engine->breadcrumbs;
  1101. const struct intel_engine_execlists * const execlists = &engine->execlists;
  1102. struct i915_gpu_error * const error = &engine->i915->gpu_error;
  1103. struct i915_request *rq, *last;
  1104. struct rb_node *rb;
  1105. int count;
  1106. if (header) {
  1107. va_list ap;
  1108. va_start(ap, header);
  1109. drm_vprintf(m, header, &ap);
  1110. va_end(ap);
  1111. }
  1112. if (i915_terminally_wedged(&engine->i915->gpu_error))
  1113. drm_printf(m, "*** WEDGED ***\n");
  1114. drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms]\n",
  1115. intel_engine_get_seqno(engine),
  1116. intel_engine_last_submit(engine),
  1117. engine->hangcheck.seqno,
  1118. jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp));
  1119. drm_printf(m, "\tReset count: %d (global %d)\n",
  1120. i915_reset_engine_count(error, engine),
  1121. i915_reset_count(error));
  1122. rcu_read_lock();
  1123. drm_printf(m, "\tRequests:\n");
  1124. rq = list_first_entry(&engine->timeline.requests,
  1125. struct i915_request, link);
  1126. if (&rq->link != &engine->timeline.requests)
  1127. print_request(m, rq, "\t\tfirst ");
  1128. rq = list_last_entry(&engine->timeline.requests,
  1129. struct i915_request, link);
  1130. if (&rq->link != &engine->timeline.requests)
  1131. print_request(m, rq, "\t\tlast ");
  1132. rq = i915_gem_find_active_request(engine);
  1133. if (rq) {
  1134. print_request(m, rq, "\t\tactive ");
  1135. drm_printf(m,
  1136. "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
  1137. rq->head, rq->postfix, rq->tail,
  1138. rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
  1139. rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
  1140. drm_printf(m, "\t\tring->start: 0x%08x\n",
  1141. i915_ggtt_offset(rq->ring->vma));
  1142. drm_printf(m, "\t\tring->head: 0x%08x\n",
  1143. rq->ring->head);
  1144. drm_printf(m, "\t\tring->tail: 0x%08x\n",
  1145. rq->ring->tail);
  1146. drm_printf(m, "\t\tring->emit: 0x%08x\n",
  1147. rq->ring->emit);
  1148. drm_printf(m, "\t\tring->space: 0x%08x\n",
  1149. rq->ring->space);
  1150. }
  1151. rcu_read_unlock();
  1152. if (intel_runtime_pm_get_if_in_use(engine->i915)) {
  1153. intel_engine_print_registers(engine, m);
  1154. intel_runtime_pm_put(engine->i915);
  1155. } else {
  1156. drm_printf(m, "\tDevice is asleep; skipping register dump\n");
  1157. }
  1158. spin_lock_irq(&engine->timeline.lock);
  1159. last = NULL;
  1160. count = 0;
  1161. list_for_each_entry(rq, &engine->timeline.requests, link) {
  1162. if (count++ < MAX_REQUESTS_TO_SHOW - 1)
  1163. print_request(m, rq, "\t\tE ");
  1164. else
  1165. last = rq;
  1166. }
  1167. if (last) {
  1168. if (count > MAX_REQUESTS_TO_SHOW) {
  1169. drm_printf(m,
  1170. "\t\t...skipping %d executing requests...\n",
  1171. count - MAX_REQUESTS_TO_SHOW);
  1172. }
  1173. print_request(m, last, "\t\tE ");
  1174. }
  1175. last = NULL;
  1176. count = 0;
  1177. drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
  1178. for (rb = execlists->first; rb; rb = rb_next(rb)) {
  1179. struct i915_priolist *p =
  1180. rb_entry(rb, typeof(*p), node);
  1181. list_for_each_entry(rq, &p->requests, sched.link) {
  1182. if (count++ < MAX_REQUESTS_TO_SHOW - 1)
  1183. print_request(m, rq, "\t\tQ ");
  1184. else
  1185. last = rq;
  1186. }
  1187. }
  1188. if (last) {
  1189. if (count > MAX_REQUESTS_TO_SHOW) {
  1190. drm_printf(m,
  1191. "\t\t...skipping %d queued requests...\n",
  1192. count - MAX_REQUESTS_TO_SHOW);
  1193. }
  1194. print_request(m, last, "\t\tQ ");
  1195. }
  1196. spin_unlock_irq(&engine->timeline.lock);
  1197. spin_lock_irq(&b->rb_lock);
  1198. for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
  1199. struct intel_wait *w = rb_entry(rb, typeof(*w), node);
  1200. drm_printf(m, "\t%s [%d] waiting for %x\n",
  1201. w->tsk->comm, w->tsk->pid, w->seqno);
  1202. }
  1203. spin_unlock_irq(&b->rb_lock);
  1204. drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
  1205. engine->irq_posted,
  1206. yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
  1207. &engine->irq_posted)),
  1208. yesno(test_bit(ENGINE_IRQ_EXECLIST,
  1209. &engine->irq_posted)));
  1210. drm_printf(m, "HWSP:\n");
  1211. hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
  1212. drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
  1213. }
  1214. static u8 user_class_map[] = {
  1215. [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
  1216. [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
  1217. [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
  1218. [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
  1219. };
  1220. struct intel_engine_cs *
  1221. intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
  1222. {
  1223. if (class >= ARRAY_SIZE(user_class_map))
  1224. return NULL;
  1225. class = user_class_map[class];
  1226. GEM_BUG_ON(class > MAX_ENGINE_CLASS);
  1227. if (instance > MAX_ENGINE_INSTANCE)
  1228. return NULL;
  1229. return i915->engine_class[class][instance];
  1230. }
  1231. /**
  1232. * intel_enable_engine_stats() - Enable engine busy tracking on engine
  1233. * @engine: engine to enable stats collection
  1234. *
  1235. * Start collecting the engine busyness data for @engine.
  1236. *
  1237. * Returns 0 on success or a negative error code.
  1238. */
  1239. int intel_enable_engine_stats(struct intel_engine_cs *engine)
  1240. {
  1241. struct intel_engine_execlists *execlists = &engine->execlists;
  1242. unsigned long flags;
  1243. int err = 0;
  1244. if (!intel_engine_supports_stats(engine))
  1245. return -ENODEV;
  1246. tasklet_disable(&execlists->tasklet);
  1247. write_seqlock_irqsave(&engine->stats.lock, flags);
  1248. if (unlikely(engine->stats.enabled == ~0)) {
  1249. err = -EBUSY;
  1250. goto unlock;
  1251. }
  1252. if (engine->stats.enabled++ == 0) {
  1253. const struct execlist_port *port = execlists->port;
  1254. unsigned int num_ports = execlists_num_ports(execlists);
  1255. engine->stats.enabled_at = ktime_get();
  1256. /* XXX submission method oblivious? */
  1257. while (num_ports-- && port_isset(port)) {
  1258. engine->stats.active++;
  1259. port++;
  1260. }
  1261. if (engine->stats.active)
  1262. engine->stats.start = engine->stats.enabled_at;
  1263. }
  1264. unlock:
  1265. write_sequnlock_irqrestore(&engine->stats.lock, flags);
  1266. tasklet_enable(&execlists->tasklet);
  1267. return err;
  1268. }
  1269. static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1270. {
  1271. ktime_t total = engine->stats.total;
  1272. /*
  1273. * If the engine is executing something at the moment
  1274. * add it to the total.
  1275. */
  1276. if (engine->stats.active)
  1277. total = ktime_add(total,
  1278. ktime_sub(ktime_get(), engine->stats.start));
  1279. return total;
  1280. }
  1281. /**
  1282. * intel_engine_get_busy_time() - Return current accumulated engine busyness
  1283. * @engine: engine to report on
  1284. *
  1285. * Returns accumulated time @engine was busy since engine stats were enabled.
  1286. */
  1287. ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1288. {
  1289. unsigned int seq;
  1290. ktime_t total;
  1291. do {
  1292. seq = read_seqbegin(&engine->stats.lock);
  1293. total = __intel_engine_get_busy_time(engine);
  1294. } while (read_seqretry(&engine->stats.lock, seq));
  1295. return total;
  1296. }
  1297. /**
  1298. * intel_disable_engine_stats() - Disable engine busy tracking on engine
  1299. * @engine: engine to disable stats collection
  1300. *
  1301. * Stops collecting the engine busyness data for @engine.
  1302. */
  1303. void intel_disable_engine_stats(struct intel_engine_cs *engine)
  1304. {
  1305. unsigned long flags;
  1306. if (!intel_engine_supports_stats(engine))
  1307. return;
  1308. write_seqlock_irqsave(&engine->stats.lock, flags);
  1309. WARN_ON_ONCE(engine->stats.enabled == 0);
  1310. if (--engine->stats.enabled == 0) {
  1311. engine->stats.total = __intel_engine_get_busy_time(engine);
  1312. engine->stats.active = 0;
  1313. }
  1314. write_sequnlock_irqrestore(&engine->stats.lock, flags);
  1315. }
  1316. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1317. #include "selftests/mock_engine.c"
  1318. #include "selftests/intel_engine_cs.c"
  1319. #endif