intel_engine_cs.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. int intel_engine_create_scratch(struct intel_engine_cs *engine,
  429. unsigned int size)
  430. {
  431. struct drm_i915_gem_object *obj;
  432. struct i915_vma *vma;
  433. int ret;
  434. WARN_ON(engine->scratch);
  435. obj = i915_gem_object_create_stolen(engine->i915, size);
  436. if (!obj)
  437. obj = i915_gem_object_create_internal(engine->i915, size);
  438. if (IS_ERR(obj)) {
  439. DRM_ERROR("Failed to allocate scratch page\n");
  440. return PTR_ERR(obj);
  441. }
  442. vma = i915_vma_instance(obj, &engine->i915->ggtt.vm, NULL);
  443. if (IS_ERR(vma)) {
  444. ret = PTR_ERR(vma);
  445. goto err_unref;
  446. }
  447. ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
  448. if (ret)
  449. goto err_unref;
  450. engine->scratch = vma;
  451. return 0;
  452. err_unref:
  453. i915_gem_object_put(obj);
  454. return ret;
  455. }
  456. void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
  457. {
  458. i915_vma_unpin_and_release(&engine->scratch);
  459. }
  460. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  461. {
  462. struct drm_i915_private *dev_priv = engine->i915;
  463. if (!dev_priv->status_page_dmah)
  464. return;
  465. drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
  466. engine->status_page.page_addr = NULL;
  467. }
  468. static void cleanup_status_page(struct intel_engine_cs *engine)
  469. {
  470. struct i915_vma *vma;
  471. struct drm_i915_gem_object *obj;
  472. vma = fetch_and_zero(&engine->status_page.vma);
  473. if (!vma)
  474. return;
  475. obj = vma->obj;
  476. i915_vma_unpin(vma);
  477. i915_vma_close(vma);
  478. i915_gem_object_unpin_map(obj);
  479. __i915_gem_object_release_unless_active(obj);
  480. }
  481. static int init_status_page(struct intel_engine_cs *engine)
  482. {
  483. struct drm_i915_gem_object *obj;
  484. struct i915_vma *vma;
  485. unsigned int flags;
  486. void *vaddr;
  487. int ret;
  488. obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  489. if (IS_ERR(obj)) {
  490. DRM_ERROR("Failed to allocate status page\n");
  491. return PTR_ERR(obj);
  492. }
  493. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  494. if (ret)
  495. goto err;
  496. vma = i915_vma_instance(obj, &engine->i915->ggtt.vm, NULL);
  497. if (IS_ERR(vma)) {
  498. ret = PTR_ERR(vma);
  499. goto err;
  500. }
  501. flags = PIN_GLOBAL;
  502. if (!HAS_LLC(engine->i915))
  503. /* On g33, we cannot place HWS above 256MiB, so
  504. * restrict its pinning to the low mappable arena.
  505. * Though this restriction is not documented for
  506. * gen4, gen5, or byt, they also behave similarly
  507. * and hang if the HWS is placed at the top of the
  508. * GTT. To generalise, it appears that all !llc
  509. * platforms have issues with us placing the HWS
  510. * above the mappable region (even though we never
  511. * actually map it).
  512. */
  513. flags |= PIN_MAPPABLE;
  514. else
  515. flags |= PIN_HIGH;
  516. ret = i915_vma_pin(vma, 0, 4096, flags);
  517. if (ret)
  518. goto err;
  519. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  520. if (IS_ERR(vaddr)) {
  521. ret = PTR_ERR(vaddr);
  522. goto err_unpin;
  523. }
  524. engine->status_page.vma = vma;
  525. engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
  526. engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
  527. return 0;
  528. err_unpin:
  529. i915_vma_unpin(vma);
  530. err:
  531. i915_gem_object_put(obj);
  532. return ret;
  533. }
  534. static int init_phys_status_page(struct intel_engine_cs *engine)
  535. {
  536. struct drm_i915_private *dev_priv = engine->i915;
  537. GEM_BUG_ON(engine->id != RCS);
  538. dev_priv->status_page_dmah =
  539. drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
  540. if (!dev_priv->status_page_dmah)
  541. return -ENOMEM;
  542. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  543. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  544. return 0;
  545. }
  546. static void __intel_context_unpin(struct i915_gem_context *ctx,
  547. struct intel_engine_cs *engine)
  548. {
  549. intel_context_unpin(to_intel_context(ctx, engine));
  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 drm_i915_private *i915 = engine->i915;
  565. struct intel_context *ce;
  566. int ret;
  567. engine->set_default_submission(engine);
  568. /* We may need to do things with the shrinker which
  569. * require us to immediately switch back to the default
  570. * context. This can cause a problem as pinning the
  571. * default context also requires GTT space which may not
  572. * be available. To avoid this we always pin the default
  573. * context.
  574. */
  575. ce = intel_context_pin(i915->kernel_context, engine);
  576. if (IS_ERR(ce))
  577. return PTR_ERR(ce);
  578. /*
  579. * Similarly the preempt context must always be available so that
  580. * we can interrupt the engine at any time.
  581. */
  582. if (i915->preempt_context) {
  583. ce = intel_context_pin(i915->preempt_context, engine);
  584. if (IS_ERR(ce)) {
  585. ret = PTR_ERR(ce);
  586. goto err_unpin_kernel;
  587. }
  588. }
  589. ret = intel_engine_init_breadcrumbs(engine);
  590. if (ret)
  591. goto err_unpin_preempt;
  592. if (HWS_NEEDS_PHYSICAL(i915))
  593. ret = init_phys_status_page(engine);
  594. else
  595. ret = init_status_page(engine);
  596. if (ret)
  597. goto err_breadcrumbs;
  598. return 0;
  599. err_breadcrumbs:
  600. intel_engine_fini_breadcrumbs(engine);
  601. err_unpin_preempt:
  602. if (i915->preempt_context)
  603. __intel_context_unpin(i915->preempt_context, engine);
  604. err_unpin_kernel:
  605. __intel_context_unpin(i915->kernel_context, engine);
  606. return ret;
  607. }
  608. /**
  609. * intel_engines_cleanup_common - cleans up the engine state created by
  610. * the common initiailizers.
  611. * @engine: Engine to cleanup.
  612. *
  613. * This cleans up everything created by the common helpers.
  614. */
  615. void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  616. {
  617. struct drm_i915_private *i915 = engine->i915;
  618. intel_engine_cleanup_scratch(engine);
  619. if (HWS_NEEDS_PHYSICAL(engine->i915))
  620. cleanup_phys_status_page(engine);
  621. else
  622. cleanup_status_page(engine);
  623. intel_engine_fini_breadcrumbs(engine);
  624. intel_engine_cleanup_cmd_parser(engine);
  625. i915_gem_batch_pool_fini(&engine->batch_pool);
  626. if (engine->default_state)
  627. i915_gem_object_put(engine->default_state);
  628. if (i915->preempt_context)
  629. __intel_context_unpin(i915->preempt_context, engine);
  630. __intel_context_unpin(i915->kernel_context, engine);
  631. i915_timeline_fini(&engine->timeline);
  632. }
  633. u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
  634. {
  635. struct drm_i915_private *dev_priv = engine->i915;
  636. u64 acthd;
  637. if (INTEL_GEN(dev_priv) >= 8)
  638. acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
  639. RING_ACTHD_UDW(engine->mmio_base));
  640. else if (INTEL_GEN(dev_priv) >= 4)
  641. acthd = I915_READ(RING_ACTHD(engine->mmio_base));
  642. else
  643. acthd = I915_READ(ACTHD);
  644. return acthd;
  645. }
  646. u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
  647. {
  648. struct drm_i915_private *dev_priv = engine->i915;
  649. u64 bbaddr;
  650. if (INTEL_GEN(dev_priv) >= 8)
  651. bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
  652. RING_BBADDR_UDW(engine->mmio_base));
  653. else
  654. bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
  655. return bbaddr;
  656. }
  657. int intel_engine_stop_cs(struct intel_engine_cs *engine)
  658. {
  659. struct drm_i915_private *dev_priv = engine->i915;
  660. const u32 base = engine->mmio_base;
  661. const i915_reg_t mode = RING_MI_MODE(base);
  662. int err;
  663. if (INTEL_GEN(dev_priv) < 3)
  664. return -ENODEV;
  665. GEM_TRACE("%s\n", engine->name);
  666. I915_WRITE_FW(mode, _MASKED_BIT_ENABLE(STOP_RING));
  667. err = 0;
  668. if (__intel_wait_for_register_fw(dev_priv,
  669. mode, MODE_IDLE, MODE_IDLE,
  670. 1000, 0,
  671. NULL)) {
  672. GEM_TRACE("%s: timed out on STOP_RING -> IDLE\n", engine->name);
  673. err = -ETIMEDOUT;
  674. }
  675. /* A final mmio read to let GPU writes be hopefully flushed to memory */
  676. POSTING_READ_FW(mode);
  677. return err;
  678. }
  679. const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
  680. {
  681. switch (type) {
  682. case I915_CACHE_NONE: return " uncached";
  683. case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
  684. case I915_CACHE_L3_LLC: return " L3+LLC";
  685. case I915_CACHE_WT: return " WT";
  686. default: return "";
  687. }
  688. }
  689. u32 intel_calculate_mcr_s_ss_select(struct drm_i915_private *dev_priv)
  690. {
  691. const struct sseu_dev_info *sseu = &(INTEL_INFO(dev_priv)->sseu);
  692. u32 mcr_s_ss_select;
  693. u32 slice = fls(sseu->slice_mask);
  694. u32 subslice = fls(sseu->subslice_mask[slice]);
  695. if (INTEL_GEN(dev_priv) == 10)
  696. mcr_s_ss_select = GEN8_MCR_SLICE(slice) |
  697. GEN8_MCR_SUBSLICE(subslice);
  698. else if (INTEL_GEN(dev_priv) >= 11)
  699. mcr_s_ss_select = GEN11_MCR_SLICE(slice) |
  700. GEN11_MCR_SUBSLICE(subslice);
  701. else
  702. mcr_s_ss_select = 0;
  703. return mcr_s_ss_select;
  704. }
  705. static inline uint32_t
  706. read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
  707. int subslice, i915_reg_t reg)
  708. {
  709. uint32_t mcr_slice_subslice_mask;
  710. uint32_t mcr_slice_subslice_select;
  711. uint32_t default_mcr_s_ss_select;
  712. uint32_t mcr;
  713. uint32_t ret;
  714. enum forcewake_domains fw_domains;
  715. if (INTEL_GEN(dev_priv) >= 11) {
  716. mcr_slice_subslice_mask = GEN11_MCR_SLICE_MASK |
  717. GEN11_MCR_SUBSLICE_MASK;
  718. mcr_slice_subslice_select = GEN11_MCR_SLICE(slice) |
  719. GEN11_MCR_SUBSLICE(subslice);
  720. } else {
  721. mcr_slice_subslice_mask = GEN8_MCR_SLICE_MASK |
  722. GEN8_MCR_SUBSLICE_MASK;
  723. mcr_slice_subslice_select = GEN8_MCR_SLICE(slice) |
  724. GEN8_MCR_SUBSLICE(subslice);
  725. }
  726. default_mcr_s_ss_select = intel_calculate_mcr_s_ss_select(dev_priv);
  727. fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
  728. FW_REG_READ);
  729. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  730. GEN8_MCR_SELECTOR,
  731. FW_REG_READ | FW_REG_WRITE);
  732. spin_lock_irq(&dev_priv->uncore.lock);
  733. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  734. mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
  735. WARN_ON_ONCE((mcr & mcr_slice_subslice_mask) !=
  736. default_mcr_s_ss_select);
  737. mcr &= ~mcr_slice_subslice_mask;
  738. mcr |= mcr_slice_subslice_select;
  739. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  740. ret = I915_READ_FW(reg);
  741. mcr &= ~mcr_slice_subslice_mask;
  742. mcr |= default_mcr_s_ss_select;
  743. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  744. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  745. spin_unlock_irq(&dev_priv->uncore.lock);
  746. return ret;
  747. }
  748. /* NB: please notice the memset */
  749. void intel_engine_get_instdone(struct intel_engine_cs *engine,
  750. struct intel_instdone *instdone)
  751. {
  752. struct drm_i915_private *dev_priv = engine->i915;
  753. u32 mmio_base = engine->mmio_base;
  754. int slice;
  755. int subslice;
  756. memset(instdone, 0, sizeof(*instdone));
  757. switch (INTEL_GEN(dev_priv)) {
  758. default:
  759. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  760. if (engine->id != RCS)
  761. break;
  762. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  763. for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
  764. instdone->sampler[slice][subslice] =
  765. read_subslice_reg(dev_priv, slice, subslice,
  766. GEN7_SAMPLER_INSTDONE);
  767. instdone->row[slice][subslice] =
  768. read_subslice_reg(dev_priv, slice, subslice,
  769. GEN7_ROW_INSTDONE);
  770. }
  771. break;
  772. case 7:
  773. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  774. if (engine->id != RCS)
  775. break;
  776. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  777. instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
  778. instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
  779. break;
  780. case 6:
  781. case 5:
  782. case 4:
  783. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  784. if (engine->id == RCS)
  785. /* HACK: Using the wrong struct member */
  786. instdone->slice_common = I915_READ(GEN4_INSTDONE1);
  787. break;
  788. case 3:
  789. case 2:
  790. instdone->instdone = I915_READ(GEN2_INSTDONE);
  791. break;
  792. }
  793. }
  794. static bool ring_is_idle(struct intel_engine_cs *engine)
  795. {
  796. struct drm_i915_private *dev_priv = engine->i915;
  797. bool idle = true;
  798. /* If the whole device is asleep, the engine must be idle */
  799. if (!intel_runtime_pm_get_if_in_use(dev_priv))
  800. return true;
  801. /* First check that no commands are left in the ring */
  802. if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
  803. (I915_READ_TAIL(engine) & TAIL_ADDR))
  804. idle = false;
  805. /* No bit for gen2, so assume the CS parser is idle */
  806. if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
  807. idle = false;
  808. intel_runtime_pm_put(dev_priv);
  809. return idle;
  810. }
  811. /**
  812. * intel_engine_is_idle() - Report if the engine has finished process all work
  813. * @engine: the intel_engine_cs
  814. *
  815. * Return true if there are no requests pending, nothing left to be submitted
  816. * to hardware, and that the engine is idle.
  817. */
  818. bool intel_engine_is_idle(struct intel_engine_cs *engine)
  819. {
  820. struct drm_i915_private *dev_priv = engine->i915;
  821. /* More white lies, if wedged, hw state is inconsistent */
  822. if (i915_terminally_wedged(&dev_priv->gpu_error))
  823. return true;
  824. /* Any inflight/incomplete requests? */
  825. if (!i915_seqno_passed(intel_engine_get_seqno(engine),
  826. intel_engine_last_submit(engine)))
  827. return false;
  828. if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
  829. return true;
  830. /* Waiting to drain ELSP? */
  831. if (READ_ONCE(engine->execlists.active)) {
  832. struct tasklet_struct *t = &engine->execlists.tasklet;
  833. local_bh_disable();
  834. if (tasklet_trylock(t)) {
  835. /* Must wait for any GPU reset in progress. */
  836. if (__tasklet_is_enabled(t))
  837. t->func(t->data);
  838. tasklet_unlock(t);
  839. }
  840. local_bh_enable();
  841. if (READ_ONCE(engine->execlists.active))
  842. return false;
  843. }
  844. /* ELSP is empty, but there are ready requests? E.g. after reset */
  845. if (!RB_EMPTY_ROOT(&engine->execlists.queue.rb_root))
  846. return false;
  847. /* Ring stopped? */
  848. if (!ring_is_idle(engine))
  849. return false;
  850. return true;
  851. }
  852. bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
  853. {
  854. struct intel_engine_cs *engine;
  855. enum intel_engine_id id;
  856. /*
  857. * If the driver is wedged, HW state may be very inconsistent and
  858. * report that it is still busy, even though we have stopped using it.
  859. */
  860. if (i915_terminally_wedged(&dev_priv->gpu_error))
  861. return true;
  862. for_each_engine(engine, dev_priv, id) {
  863. if (!intel_engine_is_idle(engine))
  864. return false;
  865. }
  866. return true;
  867. }
  868. /**
  869. * intel_engine_has_kernel_context:
  870. * @engine: the engine
  871. *
  872. * Returns true if the last context to be executed on this engine, or has been
  873. * executed if the engine is already idle, is the kernel context
  874. * (#i915.kernel_context).
  875. */
  876. bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
  877. {
  878. const struct intel_context *kernel_context =
  879. to_intel_context(engine->i915->kernel_context, engine);
  880. struct i915_request *rq;
  881. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  882. /*
  883. * Check the last context seen by the engine. If active, it will be
  884. * the last request that remains in the timeline. When idle, it is
  885. * the last executed context as tracked by retirement.
  886. */
  887. rq = __i915_gem_active_peek(&engine->timeline.last_request);
  888. if (rq)
  889. return rq->hw_context == kernel_context;
  890. else
  891. return engine->last_retired_context == kernel_context;
  892. }
  893. void intel_engines_reset_default_submission(struct drm_i915_private *i915)
  894. {
  895. struct intel_engine_cs *engine;
  896. enum intel_engine_id id;
  897. for_each_engine(engine, i915, id)
  898. engine->set_default_submission(engine);
  899. }
  900. /**
  901. * intel_engines_sanitize: called after the GPU has lost power
  902. * @i915: the i915 device
  903. *
  904. * Anytime we reset the GPU, either with an explicit GPU reset or through a
  905. * PCI power cycle, the GPU loses state and we must reset our state tracking
  906. * to match. Note that calling intel_engines_sanitize() if the GPU has not
  907. * been reset results in much confusion!
  908. */
  909. void intel_engines_sanitize(struct drm_i915_private *i915)
  910. {
  911. struct intel_engine_cs *engine;
  912. enum intel_engine_id id;
  913. GEM_TRACE("\n");
  914. for_each_engine(engine, i915, id) {
  915. if (engine->reset.reset)
  916. engine->reset.reset(engine, NULL);
  917. }
  918. }
  919. /**
  920. * intel_engines_park: called when the GT is transitioning from busy->idle
  921. * @i915: the i915 device
  922. *
  923. * The GT is now idle and about to go to sleep (maybe never to wake again?).
  924. * Time for us to tidy and put away our toys (release resources back to the
  925. * system).
  926. */
  927. void intel_engines_park(struct drm_i915_private *i915)
  928. {
  929. struct intel_engine_cs *engine;
  930. enum intel_engine_id id;
  931. for_each_engine(engine, i915, id) {
  932. /* Flush the residual irq tasklets first. */
  933. intel_engine_disarm_breadcrumbs(engine);
  934. tasklet_kill(&engine->execlists.tasklet);
  935. /*
  936. * We are committed now to parking the engines, make sure there
  937. * will be no more interrupts arriving later and the engines
  938. * are truly idle.
  939. */
  940. if (wait_for(intel_engine_is_idle(engine), 10)) {
  941. struct drm_printer p = drm_debug_printer(__func__);
  942. dev_err(i915->drm.dev,
  943. "%s is not idle before parking\n",
  944. engine->name);
  945. intel_engine_dump(engine, &p, NULL);
  946. }
  947. /* Must be reset upon idling, or we may miss the busy wakeup. */
  948. GEM_BUG_ON(engine->execlists.queue_priority != INT_MIN);
  949. if (engine->park)
  950. engine->park(engine);
  951. if (engine->pinned_default_state) {
  952. i915_gem_object_unpin_map(engine->default_state);
  953. engine->pinned_default_state = NULL;
  954. }
  955. i915_gem_batch_pool_fini(&engine->batch_pool);
  956. engine->execlists.no_priolist = false;
  957. }
  958. }
  959. /**
  960. * intel_engines_unpark: called when the GT is transitioning from idle->busy
  961. * @i915: the i915 device
  962. *
  963. * The GT was idle and now about to fire up with some new user requests.
  964. */
  965. void intel_engines_unpark(struct drm_i915_private *i915)
  966. {
  967. struct intel_engine_cs *engine;
  968. enum intel_engine_id id;
  969. for_each_engine(engine, i915, id) {
  970. void *map;
  971. /* Pin the default state for fast resets from atomic context. */
  972. map = NULL;
  973. if (engine->default_state)
  974. map = i915_gem_object_pin_map(engine->default_state,
  975. I915_MAP_WB);
  976. if (!IS_ERR_OR_NULL(map))
  977. engine->pinned_default_state = map;
  978. if (engine->unpark)
  979. engine->unpark(engine);
  980. intel_engine_init_hangcheck(engine);
  981. }
  982. }
  983. /**
  984. * intel_engine_lost_context: called when the GPU is reset into unknown state
  985. * @engine: the engine
  986. *
  987. * We have either reset the GPU or otherwise about to lose state tracking of
  988. * the current GPU logical state (e.g. suspend). On next use, it is therefore
  989. * imperative that we make no presumptions about the current state and load
  990. * from scratch.
  991. */
  992. void intel_engine_lost_context(struct intel_engine_cs *engine)
  993. {
  994. struct intel_context *ce;
  995. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  996. ce = fetch_and_zero(&engine->last_retired_context);
  997. if (ce)
  998. intel_context_unpin(ce);
  999. }
  1000. bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
  1001. {
  1002. switch (INTEL_GEN(engine->i915)) {
  1003. case 2:
  1004. return false; /* uses physical not virtual addresses */
  1005. case 3:
  1006. /* maybe only uses physical not virtual addresses */
  1007. return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
  1008. case 6:
  1009. return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
  1010. default:
  1011. return true;
  1012. }
  1013. }
  1014. unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
  1015. {
  1016. struct intel_engine_cs *engine;
  1017. enum intel_engine_id id;
  1018. unsigned int which;
  1019. which = 0;
  1020. for_each_engine(engine, i915, id)
  1021. if (engine->default_state)
  1022. which |= BIT(engine->uabi_class);
  1023. return which;
  1024. }
  1025. static int print_sched_attr(struct drm_i915_private *i915,
  1026. const struct i915_sched_attr *attr,
  1027. char *buf, int x, int len)
  1028. {
  1029. if (attr->priority == I915_PRIORITY_INVALID)
  1030. return x;
  1031. x += snprintf(buf + x, len - x,
  1032. " prio=%d", attr->priority);
  1033. return x;
  1034. }
  1035. static void print_request(struct drm_printer *m,
  1036. struct i915_request *rq,
  1037. const char *prefix)
  1038. {
  1039. const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
  1040. char buf[80] = "";
  1041. int x = 0;
  1042. x = print_sched_attr(rq->i915, &rq->sched.attr, buf, x, sizeof(buf));
  1043. drm_printf(m, "%s%x%s [%llx:%x]%s @ %dms: %s\n",
  1044. prefix,
  1045. rq->global_seqno,
  1046. i915_request_completed(rq) ? "!" : "",
  1047. rq->fence.context, rq->fence.seqno,
  1048. buf,
  1049. jiffies_to_msecs(jiffies - rq->emitted_jiffies),
  1050. name);
  1051. }
  1052. static void hexdump(struct drm_printer *m, const void *buf, size_t len)
  1053. {
  1054. const size_t rowsize = 8 * sizeof(u32);
  1055. const void *prev = NULL;
  1056. bool skip = false;
  1057. size_t pos;
  1058. for (pos = 0; pos < len; pos += rowsize) {
  1059. char line[128];
  1060. if (prev && !memcmp(prev, buf + pos, rowsize)) {
  1061. if (!skip) {
  1062. drm_printf(m, "*\n");
  1063. skip = true;
  1064. }
  1065. continue;
  1066. }
  1067. WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
  1068. rowsize, sizeof(u32),
  1069. line, sizeof(line),
  1070. false) >= sizeof(line));
  1071. drm_printf(m, "[%04zx] %s\n", pos, line);
  1072. prev = buf + pos;
  1073. skip = false;
  1074. }
  1075. }
  1076. static void intel_engine_print_registers(const struct intel_engine_cs *engine,
  1077. struct drm_printer *m)
  1078. {
  1079. struct drm_i915_private *dev_priv = engine->i915;
  1080. const struct intel_engine_execlists * const execlists =
  1081. &engine->execlists;
  1082. u64 addr;
  1083. if (engine->id == RCS && IS_GEN(dev_priv, 4, 7))
  1084. drm_printf(m, "\tCCID: 0x%08x\n", I915_READ(CCID));
  1085. drm_printf(m, "\tRING_START: 0x%08x\n",
  1086. I915_READ(RING_START(engine->mmio_base)));
  1087. drm_printf(m, "\tRING_HEAD: 0x%08x\n",
  1088. I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR);
  1089. drm_printf(m, "\tRING_TAIL: 0x%08x\n",
  1090. I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR);
  1091. drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
  1092. I915_READ(RING_CTL(engine->mmio_base)),
  1093. I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
  1094. if (INTEL_GEN(engine->i915) > 2) {
  1095. drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
  1096. I915_READ(RING_MI_MODE(engine->mmio_base)),
  1097. I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
  1098. }
  1099. if (INTEL_GEN(dev_priv) >= 6) {
  1100. drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
  1101. }
  1102. if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
  1103. drm_printf(m, "\tSYNC_0: 0x%08x\n",
  1104. I915_READ(RING_SYNC_0(engine->mmio_base)));
  1105. drm_printf(m, "\tSYNC_1: 0x%08x\n",
  1106. I915_READ(RING_SYNC_1(engine->mmio_base)));
  1107. if (HAS_VEBOX(dev_priv))
  1108. drm_printf(m, "\tSYNC_2: 0x%08x\n",
  1109. I915_READ(RING_SYNC_2(engine->mmio_base)));
  1110. }
  1111. addr = intel_engine_get_active_head(engine);
  1112. drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
  1113. upper_32_bits(addr), lower_32_bits(addr));
  1114. addr = intel_engine_get_last_batch_head(engine);
  1115. drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
  1116. upper_32_bits(addr), lower_32_bits(addr));
  1117. if (INTEL_GEN(dev_priv) >= 8)
  1118. addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
  1119. RING_DMA_FADD_UDW(engine->mmio_base));
  1120. else if (INTEL_GEN(dev_priv) >= 4)
  1121. addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
  1122. else
  1123. addr = I915_READ(DMA_FADD_I8XX);
  1124. drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
  1125. upper_32_bits(addr), lower_32_bits(addr));
  1126. if (INTEL_GEN(dev_priv) >= 4) {
  1127. drm_printf(m, "\tIPEIR: 0x%08x\n",
  1128. I915_READ(RING_IPEIR(engine->mmio_base)));
  1129. drm_printf(m, "\tIPEHR: 0x%08x\n",
  1130. I915_READ(RING_IPEHR(engine->mmio_base)));
  1131. } else {
  1132. drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
  1133. drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
  1134. }
  1135. if (HAS_EXECLISTS(dev_priv)) {
  1136. const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
  1137. u32 ptr, read, write;
  1138. unsigned int idx;
  1139. drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
  1140. I915_READ(RING_EXECLIST_STATUS_LO(engine)),
  1141. I915_READ(RING_EXECLIST_STATUS_HI(engine)));
  1142. ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
  1143. read = GEN8_CSB_READ_PTR(ptr);
  1144. write = GEN8_CSB_WRITE_PTR(ptr);
  1145. drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], tasklet queued? %s (%s)\n",
  1146. read, execlists->csb_head,
  1147. write,
  1148. intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
  1149. yesno(test_bit(TASKLET_STATE_SCHED,
  1150. &engine->execlists.tasklet.state)),
  1151. enableddisabled(!atomic_read(&engine->execlists.tasklet.count)));
  1152. if (read >= GEN8_CSB_ENTRIES)
  1153. read = 0;
  1154. if (write >= GEN8_CSB_ENTRIES)
  1155. write = 0;
  1156. if (read > write)
  1157. write += GEN8_CSB_ENTRIES;
  1158. while (read < write) {
  1159. idx = ++read % GEN8_CSB_ENTRIES;
  1160. drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
  1161. idx,
  1162. I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
  1163. hws[idx * 2],
  1164. I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
  1165. hws[idx * 2 + 1]);
  1166. }
  1167. rcu_read_lock();
  1168. for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
  1169. struct i915_request *rq;
  1170. unsigned int count;
  1171. rq = port_unpack(&execlists->port[idx], &count);
  1172. if (rq) {
  1173. char hdr[80];
  1174. snprintf(hdr, sizeof(hdr),
  1175. "\t\tELSP[%d] count=%d, ring->start=%08x, rq: ",
  1176. idx, count,
  1177. i915_ggtt_offset(rq->ring->vma));
  1178. print_request(m, rq, hdr);
  1179. } else {
  1180. drm_printf(m, "\t\tELSP[%d] idle\n", idx);
  1181. }
  1182. }
  1183. drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
  1184. rcu_read_unlock();
  1185. } else if (INTEL_GEN(dev_priv) > 6) {
  1186. drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
  1187. I915_READ(RING_PP_DIR_BASE(engine)));
  1188. drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
  1189. I915_READ(RING_PP_DIR_BASE_READ(engine)));
  1190. drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
  1191. I915_READ(RING_PP_DIR_DCLV(engine)));
  1192. }
  1193. }
  1194. static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
  1195. {
  1196. void *ring;
  1197. int size;
  1198. drm_printf(m,
  1199. "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n",
  1200. rq->head, rq->postfix, rq->tail,
  1201. rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
  1202. rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
  1203. size = rq->tail - rq->head;
  1204. if (rq->tail < rq->head)
  1205. size += rq->ring->size;
  1206. ring = kmalloc(size, GFP_ATOMIC);
  1207. if (ring) {
  1208. const void *vaddr = rq->ring->vaddr;
  1209. unsigned int head = rq->head;
  1210. unsigned int len = 0;
  1211. if (rq->tail < head) {
  1212. len = rq->ring->size - head;
  1213. memcpy(ring, vaddr + head, len);
  1214. head = 0;
  1215. }
  1216. memcpy(ring + len, vaddr + head, size - len);
  1217. hexdump(m, ring, size);
  1218. kfree(ring);
  1219. }
  1220. }
  1221. void intel_engine_dump(struct intel_engine_cs *engine,
  1222. struct drm_printer *m,
  1223. const char *header, ...)
  1224. {
  1225. const int MAX_REQUESTS_TO_SHOW = 8;
  1226. struct intel_breadcrumbs * const b = &engine->breadcrumbs;
  1227. const struct intel_engine_execlists * const execlists = &engine->execlists;
  1228. struct i915_gpu_error * const error = &engine->i915->gpu_error;
  1229. struct i915_request *rq, *last;
  1230. unsigned long flags;
  1231. struct rb_node *rb;
  1232. int count;
  1233. if (header) {
  1234. va_list ap;
  1235. va_start(ap, header);
  1236. drm_vprintf(m, header, &ap);
  1237. va_end(ap);
  1238. }
  1239. if (i915_terminally_wedged(&engine->i915->gpu_error))
  1240. drm_printf(m, "*** WEDGED ***\n");
  1241. drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms]\n",
  1242. intel_engine_get_seqno(engine),
  1243. intel_engine_last_submit(engine),
  1244. engine->hangcheck.seqno,
  1245. jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp));
  1246. drm_printf(m, "\tReset count: %d (global %d)\n",
  1247. i915_reset_engine_count(error, engine),
  1248. i915_reset_count(error));
  1249. rcu_read_lock();
  1250. drm_printf(m, "\tRequests:\n");
  1251. rq = list_first_entry(&engine->timeline.requests,
  1252. struct i915_request, link);
  1253. if (&rq->link != &engine->timeline.requests)
  1254. print_request(m, rq, "\t\tfirst ");
  1255. rq = list_last_entry(&engine->timeline.requests,
  1256. struct i915_request, link);
  1257. if (&rq->link != &engine->timeline.requests)
  1258. print_request(m, rq, "\t\tlast ");
  1259. rq = i915_gem_find_active_request(engine);
  1260. if (rq) {
  1261. print_request(m, rq, "\t\tactive ");
  1262. drm_printf(m, "\t\tring->start: 0x%08x\n",
  1263. i915_ggtt_offset(rq->ring->vma));
  1264. drm_printf(m, "\t\tring->head: 0x%08x\n",
  1265. rq->ring->head);
  1266. drm_printf(m, "\t\tring->tail: 0x%08x\n",
  1267. rq->ring->tail);
  1268. drm_printf(m, "\t\tring->emit: 0x%08x\n",
  1269. rq->ring->emit);
  1270. drm_printf(m, "\t\tring->space: 0x%08x\n",
  1271. rq->ring->space);
  1272. print_request_ring(m, rq);
  1273. }
  1274. rcu_read_unlock();
  1275. if (intel_runtime_pm_get_if_in_use(engine->i915)) {
  1276. intel_engine_print_registers(engine, m);
  1277. intel_runtime_pm_put(engine->i915);
  1278. } else {
  1279. drm_printf(m, "\tDevice is asleep; skipping register dump\n");
  1280. }
  1281. local_irq_save(flags);
  1282. spin_lock(&engine->timeline.lock);
  1283. last = NULL;
  1284. count = 0;
  1285. list_for_each_entry(rq, &engine->timeline.requests, link) {
  1286. if (count++ < MAX_REQUESTS_TO_SHOW - 1)
  1287. print_request(m, rq, "\t\tE ");
  1288. else
  1289. last = rq;
  1290. }
  1291. if (last) {
  1292. if (count > MAX_REQUESTS_TO_SHOW) {
  1293. drm_printf(m,
  1294. "\t\t...skipping %d executing requests...\n",
  1295. count - MAX_REQUESTS_TO_SHOW);
  1296. }
  1297. print_request(m, last, "\t\tE ");
  1298. }
  1299. last = NULL;
  1300. count = 0;
  1301. drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
  1302. for (rb = rb_first_cached(&execlists->queue); rb; rb = rb_next(rb)) {
  1303. struct i915_priolist *p =
  1304. rb_entry(rb, typeof(*p), node);
  1305. list_for_each_entry(rq, &p->requests, sched.link) {
  1306. if (count++ < MAX_REQUESTS_TO_SHOW - 1)
  1307. print_request(m, rq, "\t\tQ ");
  1308. else
  1309. last = rq;
  1310. }
  1311. }
  1312. if (last) {
  1313. if (count > MAX_REQUESTS_TO_SHOW) {
  1314. drm_printf(m,
  1315. "\t\t...skipping %d queued requests...\n",
  1316. count - MAX_REQUESTS_TO_SHOW);
  1317. }
  1318. print_request(m, last, "\t\tQ ");
  1319. }
  1320. spin_unlock(&engine->timeline.lock);
  1321. spin_lock(&b->rb_lock);
  1322. for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
  1323. struct intel_wait *w = rb_entry(rb, typeof(*w), node);
  1324. drm_printf(m, "\t%s [%d] waiting for %x\n",
  1325. w->tsk->comm, w->tsk->pid, w->seqno);
  1326. }
  1327. spin_unlock(&b->rb_lock);
  1328. local_irq_restore(flags);
  1329. drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s)\n",
  1330. engine->irq_posted,
  1331. yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
  1332. &engine->irq_posted)));
  1333. drm_printf(m, "HWSP:\n");
  1334. hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
  1335. drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
  1336. }
  1337. static u8 user_class_map[] = {
  1338. [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
  1339. [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
  1340. [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
  1341. [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
  1342. };
  1343. struct intel_engine_cs *
  1344. intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
  1345. {
  1346. if (class >= ARRAY_SIZE(user_class_map))
  1347. return NULL;
  1348. class = user_class_map[class];
  1349. GEM_BUG_ON(class > MAX_ENGINE_CLASS);
  1350. if (instance > MAX_ENGINE_INSTANCE)
  1351. return NULL;
  1352. return i915->engine_class[class][instance];
  1353. }
  1354. /**
  1355. * intel_enable_engine_stats() - Enable engine busy tracking on engine
  1356. * @engine: engine to enable stats collection
  1357. *
  1358. * Start collecting the engine busyness data for @engine.
  1359. *
  1360. * Returns 0 on success or a negative error code.
  1361. */
  1362. int intel_enable_engine_stats(struct intel_engine_cs *engine)
  1363. {
  1364. struct intel_engine_execlists *execlists = &engine->execlists;
  1365. unsigned long flags;
  1366. int err = 0;
  1367. if (!intel_engine_supports_stats(engine))
  1368. return -ENODEV;
  1369. spin_lock_irqsave(&engine->timeline.lock, flags);
  1370. write_seqlock(&engine->stats.lock);
  1371. if (unlikely(engine->stats.enabled == ~0)) {
  1372. err = -EBUSY;
  1373. goto unlock;
  1374. }
  1375. if (engine->stats.enabled++ == 0) {
  1376. const struct execlist_port *port = execlists->port;
  1377. unsigned int num_ports = execlists_num_ports(execlists);
  1378. engine->stats.enabled_at = ktime_get();
  1379. /* XXX submission method oblivious? */
  1380. while (num_ports-- && port_isset(port)) {
  1381. engine->stats.active++;
  1382. port++;
  1383. }
  1384. if (engine->stats.active)
  1385. engine->stats.start = engine->stats.enabled_at;
  1386. }
  1387. unlock:
  1388. write_sequnlock(&engine->stats.lock);
  1389. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  1390. return err;
  1391. }
  1392. static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1393. {
  1394. ktime_t total = engine->stats.total;
  1395. /*
  1396. * If the engine is executing something at the moment
  1397. * add it to the total.
  1398. */
  1399. if (engine->stats.active)
  1400. total = ktime_add(total,
  1401. ktime_sub(ktime_get(), engine->stats.start));
  1402. return total;
  1403. }
  1404. /**
  1405. * intel_engine_get_busy_time() - Return current accumulated engine busyness
  1406. * @engine: engine to report on
  1407. *
  1408. * Returns accumulated time @engine was busy since engine stats were enabled.
  1409. */
  1410. ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1411. {
  1412. unsigned int seq;
  1413. ktime_t total;
  1414. do {
  1415. seq = read_seqbegin(&engine->stats.lock);
  1416. total = __intel_engine_get_busy_time(engine);
  1417. } while (read_seqretry(&engine->stats.lock, seq));
  1418. return total;
  1419. }
  1420. /**
  1421. * intel_disable_engine_stats() - Disable engine busy tracking on engine
  1422. * @engine: engine to disable stats collection
  1423. *
  1424. * Stops collecting the engine busyness data for @engine.
  1425. */
  1426. void intel_disable_engine_stats(struct intel_engine_cs *engine)
  1427. {
  1428. unsigned long flags;
  1429. if (!intel_engine_supports_stats(engine))
  1430. return;
  1431. write_seqlock_irqsave(&engine->stats.lock, flags);
  1432. WARN_ON_ONCE(engine->stats.enabled == 0);
  1433. if (--engine->stats.enabled == 0) {
  1434. engine->stats.total = __intel_engine_get_busy_time(engine);
  1435. engine->stats.active = 0;
  1436. }
  1437. write_sequnlock_irqrestore(&engine->stats.lock, flags);
  1438. }
  1439. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1440. #include "selftests/mock_engine.c"
  1441. #include "selftests/intel_engine_cs.c"
  1442. #endif