intel_engine_cs.c 44 KB

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