intel_engine_cs.c 42 KB

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