intel_engine_cs.c 42 KB

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