intel_engine_cs.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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. struct engine_info {
  76. unsigned int hw_id;
  77. unsigned int uabi_id;
  78. u8 class;
  79. u8 instance;
  80. u32 mmio_base;
  81. unsigned irq_shift;
  82. };
  83. static const struct engine_info intel_engines[] = {
  84. [RCS] = {
  85. .hw_id = RCS_HW,
  86. .uabi_id = I915_EXEC_RENDER,
  87. .class = RENDER_CLASS,
  88. .instance = 0,
  89. .mmio_base = RENDER_RING_BASE,
  90. .irq_shift = GEN8_RCS_IRQ_SHIFT,
  91. },
  92. [BCS] = {
  93. .hw_id = BCS_HW,
  94. .uabi_id = I915_EXEC_BLT,
  95. .class = COPY_ENGINE_CLASS,
  96. .instance = 0,
  97. .mmio_base = BLT_RING_BASE,
  98. .irq_shift = GEN8_BCS_IRQ_SHIFT,
  99. },
  100. [VCS] = {
  101. .hw_id = VCS_HW,
  102. .uabi_id = I915_EXEC_BSD,
  103. .class = VIDEO_DECODE_CLASS,
  104. .instance = 0,
  105. .mmio_base = GEN6_BSD_RING_BASE,
  106. .irq_shift = GEN8_VCS1_IRQ_SHIFT,
  107. },
  108. [VCS2] = {
  109. .hw_id = VCS2_HW,
  110. .uabi_id = I915_EXEC_BSD,
  111. .class = VIDEO_DECODE_CLASS,
  112. .instance = 1,
  113. .mmio_base = GEN8_BSD2_RING_BASE,
  114. .irq_shift = GEN8_VCS2_IRQ_SHIFT,
  115. },
  116. [VECS] = {
  117. .hw_id = VECS_HW,
  118. .uabi_id = I915_EXEC_VEBOX,
  119. .class = VIDEO_ENHANCEMENT_CLASS,
  120. .instance = 0,
  121. .mmio_base = VEBOX_RING_BASE,
  122. .irq_shift = GEN8_VECS_IRQ_SHIFT,
  123. },
  124. };
  125. /**
  126. * ___intel_engine_context_size() - return the size of the context for an engine
  127. * @dev_priv: i915 device private
  128. * @class: engine class
  129. *
  130. * Each engine class may require a different amount of space for a context
  131. * image.
  132. *
  133. * Return: size (in bytes) of an engine class specific context image
  134. *
  135. * Note: this size includes the HWSP, which is part of the context image
  136. * in LRC mode, but does not include the "shared data page" used with
  137. * GuC submission. The caller should account for this if using the GuC.
  138. */
  139. static u32
  140. __intel_engine_context_size(struct drm_i915_private *dev_priv, u8 class)
  141. {
  142. u32 cxt_size;
  143. BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
  144. switch (class) {
  145. case RENDER_CLASS:
  146. switch (INTEL_GEN(dev_priv)) {
  147. default:
  148. MISSING_CASE(INTEL_GEN(dev_priv));
  149. return DEFAULT_LR_CONTEXT_RENDER_SIZE;
  150. case 11:
  151. return GEN11_LR_CONTEXT_RENDER_SIZE;
  152. case 10:
  153. return GEN10_LR_CONTEXT_RENDER_SIZE;
  154. case 9:
  155. return GEN9_LR_CONTEXT_RENDER_SIZE;
  156. case 8:
  157. return GEN8_LR_CONTEXT_RENDER_SIZE;
  158. case 7:
  159. if (IS_HASWELL(dev_priv))
  160. return HSW_CXT_TOTAL_SIZE;
  161. cxt_size = I915_READ(GEN7_CXT_SIZE);
  162. return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
  163. PAGE_SIZE);
  164. case 6:
  165. cxt_size = I915_READ(CXT_SIZE);
  166. return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
  167. PAGE_SIZE);
  168. case 5:
  169. case 4:
  170. case 3:
  171. case 2:
  172. /* For the special day when i810 gets merged. */
  173. case 1:
  174. return 0;
  175. }
  176. break;
  177. default:
  178. MISSING_CASE(class);
  179. case VIDEO_DECODE_CLASS:
  180. case VIDEO_ENHANCEMENT_CLASS:
  181. case COPY_ENGINE_CLASS:
  182. if (INTEL_GEN(dev_priv) < 8)
  183. return 0;
  184. return GEN8_LR_CONTEXT_OTHER_SIZE;
  185. }
  186. }
  187. static int
  188. intel_engine_setup(struct drm_i915_private *dev_priv,
  189. enum intel_engine_id id)
  190. {
  191. const struct engine_info *info = &intel_engines[id];
  192. const struct engine_class_info *class_info;
  193. struct intel_engine_cs *engine;
  194. GEM_BUG_ON(info->class >= ARRAY_SIZE(intel_engine_classes));
  195. class_info = &intel_engine_classes[info->class];
  196. if (GEM_WARN_ON(info->class > MAX_ENGINE_CLASS))
  197. return -EINVAL;
  198. if (GEM_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
  199. return -EINVAL;
  200. if (GEM_WARN_ON(dev_priv->engine_class[info->class][info->instance]))
  201. return -EINVAL;
  202. GEM_BUG_ON(dev_priv->engine[id]);
  203. engine = kzalloc(sizeof(*engine), GFP_KERNEL);
  204. if (!engine)
  205. return -ENOMEM;
  206. engine->id = id;
  207. engine->i915 = dev_priv;
  208. WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s%u",
  209. class_info->name, info->instance) >=
  210. sizeof(engine->name));
  211. engine->hw_id = engine->guc_id = info->hw_id;
  212. engine->mmio_base = info->mmio_base;
  213. engine->irq_shift = info->irq_shift;
  214. engine->class = info->class;
  215. engine->instance = info->instance;
  216. engine->uabi_id = info->uabi_id;
  217. engine->uabi_class = class_info->uabi_class;
  218. engine->context_size = __intel_engine_context_size(dev_priv,
  219. engine->class);
  220. if (WARN_ON(engine->context_size > BIT(20)))
  221. engine->context_size = 0;
  222. /* Nothing to do here, execute in order of dependencies */
  223. engine->schedule = NULL;
  224. spin_lock_init(&engine->stats.lock);
  225. ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
  226. dev_priv->engine_class[info->class][info->instance] = engine;
  227. dev_priv->engine[id] = engine;
  228. return 0;
  229. }
  230. /**
  231. * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
  232. * @dev_priv: i915 device private
  233. *
  234. * Return: non-zero if the initialization failed.
  235. */
  236. int intel_engines_init_mmio(struct drm_i915_private *dev_priv)
  237. {
  238. struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
  239. const unsigned int ring_mask = INTEL_INFO(dev_priv)->ring_mask;
  240. struct intel_engine_cs *engine;
  241. enum intel_engine_id id;
  242. unsigned int mask = 0;
  243. unsigned int i;
  244. int err;
  245. WARN_ON(ring_mask == 0);
  246. WARN_ON(ring_mask &
  247. GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
  248. for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
  249. if (!HAS_ENGINE(dev_priv, i))
  250. continue;
  251. err = intel_engine_setup(dev_priv, i);
  252. if (err)
  253. goto cleanup;
  254. mask |= ENGINE_MASK(i);
  255. }
  256. /*
  257. * Catch failures to update intel_engines table when the new engines
  258. * are added to the driver by a warning and disabling the forgotten
  259. * engines.
  260. */
  261. if (WARN_ON(mask != ring_mask))
  262. device_info->ring_mask = mask;
  263. /* We always presume we have at least RCS available for later probing */
  264. if (WARN_ON(!HAS_ENGINE(dev_priv, RCS))) {
  265. err = -ENODEV;
  266. goto cleanup;
  267. }
  268. device_info->num_rings = hweight32(mask);
  269. i915_check_and_clear_faults(dev_priv);
  270. return 0;
  271. cleanup:
  272. for_each_engine(engine, dev_priv, id)
  273. kfree(engine);
  274. return err;
  275. }
  276. /**
  277. * intel_engines_init() - init the Engine Command Streamers
  278. * @dev_priv: i915 device private
  279. *
  280. * Return: non-zero if the initialization failed.
  281. */
  282. int intel_engines_init(struct drm_i915_private *dev_priv)
  283. {
  284. struct intel_engine_cs *engine;
  285. enum intel_engine_id id, err_id;
  286. int err;
  287. for_each_engine(engine, dev_priv, id) {
  288. const struct engine_class_info *class_info =
  289. &intel_engine_classes[engine->class];
  290. int (*init)(struct intel_engine_cs *engine);
  291. if (HAS_EXECLISTS(dev_priv))
  292. init = class_info->init_execlists;
  293. else
  294. init = class_info->init_legacy;
  295. err = -EINVAL;
  296. err_id = id;
  297. if (GEM_WARN_ON(!init))
  298. goto cleanup;
  299. err = init(engine);
  300. if (err)
  301. goto cleanup;
  302. GEM_BUG_ON(!engine->submit_request);
  303. }
  304. return 0;
  305. cleanup:
  306. for_each_engine(engine, dev_priv, id) {
  307. if (id >= err_id) {
  308. kfree(engine);
  309. dev_priv->engine[id] = NULL;
  310. } else {
  311. dev_priv->gt.cleanup_engine(engine);
  312. }
  313. }
  314. return err;
  315. }
  316. void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno)
  317. {
  318. struct drm_i915_private *dev_priv = engine->i915;
  319. /* Our semaphore implementation is strictly monotonic (i.e. we proceed
  320. * so long as the semaphore value in the register/page is greater
  321. * than the sync value), so whenever we reset the seqno,
  322. * so long as we reset the tracking semaphore value to 0, it will
  323. * always be before the next request's seqno. If we don't reset
  324. * the semaphore value, then when the seqno moves backwards all
  325. * future waits will complete instantly (causing rendering corruption).
  326. */
  327. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  328. I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
  329. I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
  330. if (HAS_VEBOX(dev_priv))
  331. I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
  332. }
  333. intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
  334. clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  335. /* After manually advancing the seqno, fake the interrupt in case
  336. * there are any waiters for that seqno.
  337. */
  338. intel_engine_wakeup(engine);
  339. GEM_BUG_ON(intel_engine_get_seqno(engine) != seqno);
  340. }
  341. static void intel_engine_init_timeline(struct intel_engine_cs *engine)
  342. {
  343. engine->timeline = &engine->i915->gt.global_timeline.engine[engine->id];
  344. }
  345. static bool csb_force_mmio(struct drm_i915_private *i915)
  346. {
  347. /*
  348. * IOMMU adds unpredictable latency causing the CSB write (from the
  349. * GPU into the HWSP) to only be visible some time after the interrupt
  350. * (missed breadcrumb syndrome).
  351. */
  352. if (intel_vtd_active())
  353. return true;
  354. /* Older GVT emulation depends upon intercepting CSB mmio */
  355. if (intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915))
  356. return true;
  357. return false;
  358. }
  359. static void intel_engine_init_execlist(struct intel_engine_cs *engine)
  360. {
  361. struct intel_engine_execlists * const execlists = &engine->execlists;
  362. execlists->csb_use_mmio = csb_force_mmio(engine->i915);
  363. execlists->port_mask = 1;
  364. BUILD_BUG_ON_NOT_POWER_OF_2(execlists_num_ports(execlists));
  365. GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
  366. execlists->queue = RB_ROOT;
  367. execlists->first = NULL;
  368. }
  369. /**
  370. * intel_engines_setup_common - setup engine state not requiring hw access
  371. * @engine: Engine to setup.
  372. *
  373. * Initializes @engine@ structure members shared between legacy and execlists
  374. * submission modes which do not require hardware access.
  375. *
  376. * Typically done early in the submission mode specific engine setup stage.
  377. */
  378. void intel_engine_setup_common(struct intel_engine_cs *engine)
  379. {
  380. intel_engine_init_execlist(engine);
  381. intel_engine_init_timeline(engine);
  382. intel_engine_init_hangcheck(engine);
  383. i915_gem_batch_pool_init(engine, &engine->batch_pool);
  384. intel_engine_init_cmd_parser(engine);
  385. }
  386. int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
  387. {
  388. struct drm_i915_gem_object *obj;
  389. struct i915_vma *vma;
  390. int ret;
  391. WARN_ON(engine->scratch);
  392. obj = i915_gem_object_create_stolen(engine->i915, size);
  393. if (!obj)
  394. obj = i915_gem_object_create_internal(engine->i915, size);
  395. if (IS_ERR(obj)) {
  396. DRM_ERROR("Failed to allocate scratch page\n");
  397. return PTR_ERR(obj);
  398. }
  399. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  400. if (IS_ERR(vma)) {
  401. ret = PTR_ERR(vma);
  402. goto err_unref;
  403. }
  404. ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
  405. if (ret)
  406. goto err_unref;
  407. engine->scratch = vma;
  408. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  409. engine->name, i915_ggtt_offset(vma));
  410. return 0;
  411. err_unref:
  412. i915_gem_object_put(obj);
  413. return ret;
  414. }
  415. static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
  416. {
  417. i915_vma_unpin_and_release(&engine->scratch);
  418. }
  419. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  420. {
  421. struct drm_i915_private *dev_priv = engine->i915;
  422. if (!dev_priv->status_page_dmah)
  423. return;
  424. drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
  425. engine->status_page.page_addr = NULL;
  426. }
  427. static void cleanup_status_page(struct intel_engine_cs *engine)
  428. {
  429. struct i915_vma *vma;
  430. struct drm_i915_gem_object *obj;
  431. vma = fetch_and_zero(&engine->status_page.vma);
  432. if (!vma)
  433. return;
  434. obj = vma->obj;
  435. i915_vma_unpin(vma);
  436. i915_vma_close(vma);
  437. i915_gem_object_unpin_map(obj);
  438. __i915_gem_object_release_unless_active(obj);
  439. }
  440. static int init_status_page(struct intel_engine_cs *engine)
  441. {
  442. struct drm_i915_gem_object *obj;
  443. struct i915_vma *vma;
  444. unsigned int flags;
  445. void *vaddr;
  446. int ret;
  447. obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  448. if (IS_ERR(obj)) {
  449. DRM_ERROR("Failed to allocate status page\n");
  450. return PTR_ERR(obj);
  451. }
  452. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  453. if (ret)
  454. goto err;
  455. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  456. if (IS_ERR(vma)) {
  457. ret = PTR_ERR(vma);
  458. goto err;
  459. }
  460. flags = PIN_GLOBAL;
  461. if (!HAS_LLC(engine->i915))
  462. /* On g33, we cannot place HWS above 256MiB, so
  463. * restrict its pinning to the low mappable arena.
  464. * Though this restriction is not documented for
  465. * gen4, gen5, or byt, they also behave similarly
  466. * and hang if the HWS is placed at the top of the
  467. * GTT. To generalise, it appears that all !llc
  468. * platforms have issues with us placing the HWS
  469. * above the mappable region (even though we never
  470. * actually map it).
  471. */
  472. flags |= PIN_MAPPABLE;
  473. else
  474. flags |= PIN_HIGH;
  475. ret = i915_vma_pin(vma, 0, 4096, flags);
  476. if (ret)
  477. goto err;
  478. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  479. if (IS_ERR(vaddr)) {
  480. ret = PTR_ERR(vaddr);
  481. goto err_unpin;
  482. }
  483. engine->status_page.vma = vma;
  484. engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
  485. engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
  486. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  487. engine->name, i915_ggtt_offset(vma));
  488. return 0;
  489. err_unpin:
  490. i915_vma_unpin(vma);
  491. err:
  492. i915_gem_object_put(obj);
  493. return ret;
  494. }
  495. static int init_phys_status_page(struct intel_engine_cs *engine)
  496. {
  497. struct drm_i915_private *dev_priv = engine->i915;
  498. GEM_BUG_ON(engine->id != RCS);
  499. dev_priv->status_page_dmah =
  500. drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
  501. if (!dev_priv->status_page_dmah)
  502. return -ENOMEM;
  503. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  504. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  505. return 0;
  506. }
  507. /**
  508. * intel_engines_init_common - initialize cengine state which might require hw access
  509. * @engine: Engine to initialize.
  510. *
  511. * Initializes @engine@ structure members shared between legacy and execlists
  512. * submission modes which do require hardware access.
  513. *
  514. * Typcally done at later stages of submission mode specific engine setup.
  515. *
  516. * Returns zero on success or an error code on failure.
  517. */
  518. int intel_engine_init_common(struct intel_engine_cs *engine)
  519. {
  520. struct intel_ring *ring;
  521. int ret;
  522. engine->set_default_submission(engine);
  523. /* We may need to do things with the shrinker which
  524. * require us to immediately switch back to the default
  525. * context. This can cause a problem as pinning the
  526. * default context also requires GTT space which may not
  527. * be available. To avoid this we always pin the default
  528. * context.
  529. */
  530. ring = engine->context_pin(engine, engine->i915->kernel_context);
  531. if (IS_ERR(ring))
  532. return PTR_ERR(ring);
  533. /*
  534. * Similarly the preempt context must always be available so that
  535. * we can interrupt the engine at any time.
  536. */
  537. if (HAS_LOGICAL_RING_PREEMPTION(engine->i915)) {
  538. ring = engine->context_pin(engine,
  539. engine->i915->preempt_context);
  540. if (IS_ERR(ring)) {
  541. ret = PTR_ERR(ring);
  542. goto err_unpin_kernel;
  543. }
  544. }
  545. ret = intel_engine_init_breadcrumbs(engine);
  546. if (ret)
  547. goto err_unpin_preempt;
  548. if (HWS_NEEDS_PHYSICAL(engine->i915))
  549. ret = init_phys_status_page(engine);
  550. else
  551. ret = init_status_page(engine);
  552. if (ret)
  553. goto err_breadcrumbs;
  554. return 0;
  555. err_breadcrumbs:
  556. intel_engine_fini_breadcrumbs(engine);
  557. err_unpin_preempt:
  558. if (HAS_LOGICAL_RING_PREEMPTION(engine->i915))
  559. engine->context_unpin(engine, engine->i915->preempt_context);
  560. err_unpin_kernel:
  561. engine->context_unpin(engine, engine->i915->kernel_context);
  562. return ret;
  563. }
  564. /**
  565. * intel_engines_cleanup_common - cleans up the engine state created by
  566. * the common initiailizers.
  567. * @engine: Engine to cleanup.
  568. *
  569. * This cleans up everything created by the common helpers.
  570. */
  571. void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  572. {
  573. intel_engine_cleanup_scratch(engine);
  574. if (HWS_NEEDS_PHYSICAL(engine->i915))
  575. cleanup_phys_status_page(engine);
  576. else
  577. cleanup_status_page(engine);
  578. intel_engine_fini_breadcrumbs(engine);
  579. intel_engine_cleanup_cmd_parser(engine);
  580. i915_gem_batch_pool_fini(&engine->batch_pool);
  581. if (engine->default_state)
  582. i915_gem_object_put(engine->default_state);
  583. if (HAS_LOGICAL_RING_PREEMPTION(engine->i915))
  584. engine->context_unpin(engine, engine->i915->preempt_context);
  585. engine->context_unpin(engine, engine->i915->kernel_context);
  586. }
  587. u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
  588. {
  589. struct drm_i915_private *dev_priv = engine->i915;
  590. u64 acthd;
  591. if (INTEL_GEN(dev_priv) >= 8)
  592. acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
  593. RING_ACTHD_UDW(engine->mmio_base));
  594. else if (INTEL_GEN(dev_priv) >= 4)
  595. acthd = I915_READ(RING_ACTHD(engine->mmio_base));
  596. else
  597. acthd = I915_READ(ACTHD);
  598. return acthd;
  599. }
  600. u64 intel_engine_get_last_batch_head(struct intel_engine_cs *engine)
  601. {
  602. struct drm_i915_private *dev_priv = engine->i915;
  603. u64 bbaddr;
  604. if (INTEL_GEN(dev_priv) >= 8)
  605. bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
  606. RING_BBADDR_UDW(engine->mmio_base));
  607. else
  608. bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
  609. return bbaddr;
  610. }
  611. const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
  612. {
  613. switch (type) {
  614. case I915_CACHE_NONE: return " uncached";
  615. case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
  616. case I915_CACHE_L3_LLC: return " L3+LLC";
  617. case I915_CACHE_WT: return " WT";
  618. default: return "";
  619. }
  620. }
  621. static inline uint32_t
  622. read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
  623. int subslice, i915_reg_t reg)
  624. {
  625. uint32_t mcr;
  626. uint32_t ret;
  627. enum forcewake_domains fw_domains;
  628. fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
  629. FW_REG_READ);
  630. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  631. GEN8_MCR_SELECTOR,
  632. FW_REG_READ | FW_REG_WRITE);
  633. spin_lock_irq(&dev_priv->uncore.lock);
  634. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  635. mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
  636. /*
  637. * The HW expects the slice and sublice selectors to be reset to 0
  638. * after reading out the registers.
  639. */
  640. WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
  641. mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
  642. mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
  643. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  644. ret = I915_READ_FW(reg);
  645. mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
  646. I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
  647. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  648. spin_unlock_irq(&dev_priv->uncore.lock);
  649. return ret;
  650. }
  651. /* NB: please notice the memset */
  652. void intel_engine_get_instdone(struct intel_engine_cs *engine,
  653. struct intel_instdone *instdone)
  654. {
  655. struct drm_i915_private *dev_priv = engine->i915;
  656. u32 mmio_base = engine->mmio_base;
  657. int slice;
  658. int subslice;
  659. memset(instdone, 0, sizeof(*instdone));
  660. switch (INTEL_GEN(dev_priv)) {
  661. default:
  662. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  663. if (engine->id != RCS)
  664. break;
  665. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  666. for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
  667. instdone->sampler[slice][subslice] =
  668. read_subslice_reg(dev_priv, slice, subslice,
  669. GEN7_SAMPLER_INSTDONE);
  670. instdone->row[slice][subslice] =
  671. read_subslice_reg(dev_priv, slice, subslice,
  672. GEN7_ROW_INSTDONE);
  673. }
  674. break;
  675. case 7:
  676. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  677. if (engine->id != RCS)
  678. break;
  679. instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
  680. instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
  681. instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
  682. break;
  683. case 6:
  684. case 5:
  685. case 4:
  686. instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
  687. if (engine->id == RCS)
  688. /* HACK: Using the wrong struct member */
  689. instdone->slice_common = I915_READ(GEN4_INSTDONE1);
  690. break;
  691. case 3:
  692. case 2:
  693. instdone->instdone = I915_READ(GEN2_INSTDONE);
  694. break;
  695. }
  696. }
  697. static int wa_add(struct drm_i915_private *dev_priv,
  698. i915_reg_t addr,
  699. const u32 mask, const u32 val)
  700. {
  701. const u32 idx = dev_priv->workarounds.count;
  702. if (WARN_ON(idx >= I915_MAX_WA_REGS))
  703. return -ENOSPC;
  704. dev_priv->workarounds.reg[idx].addr = addr;
  705. dev_priv->workarounds.reg[idx].value = val;
  706. dev_priv->workarounds.reg[idx].mask = mask;
  707. dev_priv->workarounds.count++;
  708. return 0;
  709. }
  710. #define WA_REG(addr, mask, val) do { \
  711. const int r = wa_add(dev_priv, (addr), (mask), (val)); \
  712. if (r) \
  713. return r; \
  714. } while (0)
  715. #define WA_SET_BIT_MASKED(addr, mask) \
  716. WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
  717. #define WA_CLR_BIT_MASKED(addr, mask) \
  718. WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
  719. #define WA_SET_FIELD_MASKED(addr, mask, value) \
  720. WA_REG(addr, mask, _MASKED_FIELD(mask, value))
  721. static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
  722. i915_reg_t reg)
  723. {
  724. struct drm_i915_private *dev_priv = engine->i915;
  725. struct i915_workarounds *wa = &dev_priv->workarounds;
  726. const uint32_t index = wa->hw_whitelist_count[engine->id];
  727. if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
  728. return -EINVAL;
  729. I915_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
  730. i915_mmio_reg_offset(reg));
  731. wa->hw_whitelist_count[engine->id]++;
  732. return 0;
  733. }
  734. static int gen8_init_workarounds(struct intel_engine_cs *engine)
  735. {
  736. struct drm_i915_private *dev_priv = engine->i915;
  737. WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
  738. /* WaDisableAsyncFlipPerfMode:bdw,chv */
  739. WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
  740. /* WaDisablePartialInstShootdown:bdw,chv */
  741. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  742. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  743. /* Use Force Non-Coherent whenever executing a 3D context. This is a
  744. * workaround for for a possible hang in the unlikely event a TLB
  745. * invalidation occurs during a PSD flush.
  746. */
  747. /* WaForceEnableNonCoherent:bdw,chv */
  748. /* WaHdcDisableFetchWhenMasked:bdw,chv */
  749. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  750. HDC_DONOT_FETCH_MEM_WHEN_MASKED |
  751. HDC_FORCE_NON_COHERENT);
  752. /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
  753. * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
  754. * polygons in the same 8x4 pixel/sample area to be processed without
  755. * stalling waiting for the earlier ones to write to Hierarchical Z
  756. * buffer."
  757. *
  758. * This optimization is off by default for BDW and CHV; turn it on.
  759. */
  760. WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
  761. /* Wa4x4STCOptimizationDisable:bdw,chv */
  762. WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
  763. /*
  764. * BSpec recommends 8x4 when MSAA is used,
  765. * however in practice 16x4 seems fastest.
  766. *
  767. * Note that PS/WM thread counts depend on the WIZ hashing
  768. * disable bit, which we don't touch here, but it's good
  769. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  770. */
  771. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  772. GEN6_WIZ_HASHING_MASK,
  773. GEN6_WIZ_HASHING_16x4);
  774. return 0;
  775. }
  776. static int bdw_init_workarounds(struct intel_engine_cs *engine)
  777. {
  778. struct drm_i915_private *dev_priv = engine->i915;
  779. int ret;
  780. ret = gen8_init_workarounds(engine);
  781. if (ret)
  782. return ret;
  783. /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
  784. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  785. /* WaDisableDopClockGating:bdw
  786. *
  787. * Also see the related UCGTCL1 write in broadwell_init_clock_gating()
  788. * to disable EUTC clock gating.
  789. */
  790. WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
  791. DOP_CLOCK_GATING_DISABLE);
  792. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  793. GEN8_SAMPLER_POWER_BYPASS_DIS);
  794. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  795. /* WaForceContextSaveRestoreNonCoherent:bdw */
  796. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  797. /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
  798. (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
  799. return 0;
  800. }
  801. static int chv_init_workarounds(struct intel_engine_cs *engine)
  802. {
  803. struct drm_i915_private *dev_priv = engine->i915;
  804. int ret;
  805. ret = gen8_init_workarounds(engine);
  806. if (ret)
  807. return ret;
  808. /* WaDisableThreadStallDopClockGating:chv */
  809. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  810. /* Improve HiZ throughput on CHV. */
  811. WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
  812. return 0;
  813. }
  814. static int gen9_init_workarounds(struct intel_engine_cs *engine)
  815. {
  816. struct drm_i915_private *dev_priv = engine->i915;
  817. int ret;
  818. /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk,cfl */
  819. I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
  820. /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk,cfl */
  821. I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
  822. GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
  823. /* WaDisableKillLogic:bxt,skl,kbl */
  824. if (!IS_COFFEELAKE(dev_priv))
  825. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  826. ECOCHK_DIS_TLB);
  827. if (HAS_LLC(dev_priv)) {
  828. /* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
  829. *
  830. * Must match Display Engine. See
  831. * WaCompressedResourceDisplayNewHashMode.
  832. */
  833. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  834. GEN9_PBE_COMPRESSED_HASH_SELECTION);
  835. WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
  836. GEN9_SAMPLER_HASH_COMPRESSED_READ_ADDR);
  837. I915_WRITE(MMCD_MISC_CTRL,
  838. I915_READ(MMCD_MISC_CTRL) |
  839. MMCD_PCLA |
  840. MMCD_HOTSPOT_EN);
  841. }
  842. /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk,cfl */
  843. /* WaDisablePartialInstShootdown:skl,bxt,kbl,glk,cfl */
  844. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  845. FLOW_CONTROL_ENABLE |
  846. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  847. /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
  848. if (!IS_COFFEELAKE(dev_priv))
  849. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  850. GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
  851. /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl,glk,cfl */
  852. /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl,cfl */
  853. WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
  854. GEN9_ENABLE_YV12_BUGFIX |
  855. GEN9_ENABLE_GPGPU_PREEMPTION);
  856. /* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk,cfl */
  857. /* WaDisablePartialResolveInVc:skl,bxt,kbl,cfl */
  858. WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
  859. GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
  860. /* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk,cfl */
  861. WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
  862. GEN9_CCS_TLB_PREFETCH_ENABLE);
  863. /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl,cfl */
  864. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  865. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  866. HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
  867. /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
  868. * both tied to WaForceContextSaveRestoreNonCoherent
  869. * in some hsds for skl. We keep the tie for all gen9. The
  870. * documentation is a bit hazy and so we want to get common behaviour,
  871. * even though there is no clear evidence we would need both on kbl/bxt.
  872. * This area has been source of system hangs so we play it safe
  873. * and mimic the skl regardless of what bspec says.
  874. *
  875. * Use Force Non-Coherent whenever executing a 3D context. This
  876. * is a workaround for a possible hang in the unlikely event
  877. * a TLB invalidation occurs during a PSD flush.
  878. */
  879. /* WaForceEnableNonCoherent:skl,bxt,kbl,cfl */
  880. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  881. HDC_FORCE_NON_COHERENT);
  882. /* WaDisableHDCInvalidation:skl,bxt,kbl,cfl */
  883. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  884. BDW_DISABLE_HDC_INVALIDATION);
  885. /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl,cfl */
  886. if (IS_SKYLAKE(dev_priv) ||
  887. IS_KABYLAKE(dev_priv) ||
  888. IS_COFFEELAKE(dev_priv))
  889. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  890. GEN8_SAMPLER_POWER_BYPASS_DIS);
  891. /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk,cfl */
  892. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
  893. /* WaProgramL3SqcReg1DefaultForPerf:bxt,glk */
  894. if (IS_GEN9_LP(dev_priv)) {
  895. u32 val = I915_READ(GEN8_L3SQCREG1);
  896. val &= ~L3_PRIO_CREDITS_MASK;
  897. val |= L3_GENERAL_PRIO_CREDITS(62) | L3_HIGH_PRIO_CREDITS(2);
  898. I915_WRITE(GEN8_L3SQCREG1, val);
  899. }
  900. /* WaOCLCoherentLineFlush:skl,bxt,kbl,cfl */
  901. I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
  902. GEN8_LQSC_FLUSH_COHERENT_LINES));
  903. /*
  904. * Supporting preemption with fine-granularity requires changes in the
  905. * batch buffer programming. Since we can't break old userspace, we
  906. * need to set our default preemption level to safe value. Userspace is
  907. * still able to use more fine-grained preemption levels, since in
  908. * WaEnablePreemptionGranularityControlByUMD we're whitelisting the
  909. * per-ctx register. As such, WaDisable{3D,GPGPU}MidCmdPreemption are
  910. * not real HW workarounds, but merely a way to start using preemption
  911. * while maintaining old contract with userspace.
  912. */
  913. /* WaDisable3DMidCmdPreemption:skl,bxt,glk,cfl,[cnl] */
  914. WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
  915. /* WaDisableGPGPUMidCmdPreemption:skl,bxt,blk,cfl,[cnl] */
  916. WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
  917. GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
  918. /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk,cfl */
  919. ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
  920. if (ret)
  921. return ret;
  922. /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl,cfl,[cnl] */
  923. I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
  924. _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
  925. ret = wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
  926. if (ret)
  927. return ret;
  928. /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk,cfl */
  929. ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
  930. if (ret)
  931. return ret;
  932. return 0;
  933. }
  934. static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
  935. {
  936. struct drm_i915_private *dev_priv = engine->i915;
  937. u8 vals[3] = { 0, 0, 0 };
  938. unsigned int i;
  939. for (i = 0; i < 3; i++) {
  940. u8 ss;
  941. /*
  942. * Only consider slices where one, and only one, subslice has 7
  943. * EUs
  944. */
  945. if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
  946. continue;
  947. /*
  948. * subslice_7eu[i] != 0 (because of the check above) and
  949. * ss_max == 4 (maximum number of subslices possible per slice)
  950. *
  951. * -> 0 <= ss <= 3;
  952. */
  953. ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
  954. vals[i] = 3 - ss;
  955. }
  956. if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
  957. return 0;
  958. /* Tune IZ hashing. See intel_device_info_runtime_init() */
  959. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  960. GEN9_IZ_HASHING_MASK(2) |
  961. GEN9_IZ_HASHING_MASK(1) |
  962. GEN9_IZ_HASHING_MASK(0),
  963. GEN9_IZ_HASHING(2, vals[2]) |
  964. GEN9_IZ_HASHING(1, vals[1]) |
  965. GEN9_IZ_HASHING(0, vals[0]));
  966. return 0;
  967. }
  968. static int skl_init_workarounds(struct intel_engine_cs *engine)
  969. {
  970. struct drm_i915_private *dev_priv = engine->i915;
  971. int ret;
  972. ret = gen9_init_workarounds(engine);
  973. if (ret)
  974. return ret;
  975. /* WaEnableGapsTsvCreditFix:skl */
  976. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  977. GEN9_GAPS_TSV_CREDIT_DISABLE));
  978. /* WaDisableGafsUnitClkGating:skl */
  979. I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
  980. GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
  981. /* WaInPlaceDecompressionHang:skl */
  982. if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
  983. I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
  984. (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
  985. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
  986. /* WaDisableLSQCROPERFforOCL:skl */
  987. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  988. if (ret)
  989. return ret;
  990. return skl_tune_iz_hashing(engine);
  991. }
  992. static int bxt_init_workarounds(struct intel_engine_cs *engine)
  993. {
  994. struct drm_i915_private *dev_priv = engine->i915;
  995. int ret;
  996. ret = gen9_init_workarounds(engine);
  997. if (ret)
  998. return ret;
  999. /* WaDisableThreadStallDopClockGating:bxt */
  1000. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  1001. STALL_DOP_GATING_DISABLE);
  1002. /* WaDisablePooledEuLoadBalancingFix:bxt */
  1003. I915_WRITE(FF_SLICE_CS_CHICKEN2,
  1004. _MASKED_BIT_ENABLE(GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE));
  1005. /* WaToEnableHwFixForPushConstHWBug:bxt */
  1006. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1007. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1008. /* WaInPlaceDecompressionHang:bxt */
  1009. I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
  1010. (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
  1011. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
  1012. return 0;
  1013. }
  1014. static int cnl_init_workarounds(struct intel_engine_cs *engine)
  1015. {
  1016. struct drm_i915_private *dev_priv = engine->i915;
  1017. int ret;
  1018. /* WaDisableI2mCycleOnWRPort:cnl (pre-prod) */
  1019. if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
  1020. I915_WRITE(GAMT_CHKN_BIT_REG,
  1021. (I915_READ(GAMT_CHKN_BIT_REG) |
  1022. GAMT_CHKN_DISABLE_I2M_CYCLE_ON_WR_PORT));
  1023. /* WaForceContextSaveRestoreNonCoherent:cnl */
  1024. WA_SET_BIT_MASKED(CNL_HDC_CHICKEN0,
  1025. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT);
  1026. /* WaThrottleEUPerfToAvoidTDBackPressure:cnl(pre-prod) */
  1027. if (IS_CNL_REVID(dev_priv, CNL_REVID_B0, CNL_REVID_B0))
  1028. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, THROTTLE_12_5);
  1029. /* WaDisableReplayBufferBankArbitrationOptimization:cnl */
  1030. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1031. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1032. /* WaDisableEnhancedSBEVertexCaching:cnl (pre-prod) */
  1033. if (IS_CNL_REVID(dev_priv, 0, CNL_REVID_B0))
  1034. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1035. GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE);
  1036. /* WaInPlaceDecompressionHang:cnl */
  1037. I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
  1038. (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
  1039. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
  1040. /* WaPushConstantDereferenceHoldDisable:cnl */
  1041. WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2, PUSH_CONSTANT_DEREF_DISABLE);
  1042. /* FtrEnableFastAnisoL1BankingFix: cnl */
  1043. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3, CNL_FAST_ANISO_L1_BANKING_FIX);
  1044. /* WaDisable3DMidCmdPreemption:cnl */
  1045. WA_CLR_BIT_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
  1046. /* WaDisableGPGPUMidCmdPreemption:cnl */
  1047. WA_SET_FIELD_MASKED(GEN8_CS_CHICKEN1, GEN9_PREEMPT_GPGPU_LEVEL_MASK,
  1048. GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
  1049. /* WaEnablePreemptionGranularityControlByUMD:cnl */
  1050. I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
  1051. _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
  1052. ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
  1053. if (ret)
  1054. return ret;
  1055. /* WaDisableEarlyEOT:cnl */
  1056. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, DISABLE_EARLY_EOT);
  1057. return 0;
  1058. }
  1059. static int kbl_init_workarounds(struct intel_engine_cs *engine)
  1060. {
  1061. struct drm_i915_private *dev_priv = engine->i915;
  1062. int ret;
  1063. ret = gen9_init_workarounds(engine);
  1064. if (ret)
  1065. return ret;
  1066. /* WaEnableGapsTsvCreditFix:kbl */
  1067. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  1068. GEN9_GAPS_TSV_CREDIT_DISABLE));
  1069. /* WaDisableDynamicCreditSharing:kbl */
  1070. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  1071. I915_WRITE(GAMT_CHKN_BIT_REG,
  1072. (I915_READ(GAMT_CHKN_BIT_REG) |
  1073. GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING));
  1074. /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
  1075. if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
  1076. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  1077. HDC_FENCE_DEST_SLM_DISABLE);
  1078. /* WaToEnableHwFixForPushConstHWBug:kbl */
  1079. if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
  1080. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1081. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1082. /* WaDisableGafsUnitClkGating:kbl */
  1083. I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
  1084. GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
  1085. /* WaDisableSbeCacheDispatchPortSharing:kbl */
  1086. WA_SET_BIT_MASKED(
  1087. GEN7_HALF_SLICE_CHICKEN1,
  1088. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  1089. /* WaInPlaceDecompressionHang:kbl */
  1090. I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
  1091. (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
  1092. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
  1093. /* WaDisableLSQCROPERFforOCL:kbl */
  1094. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  1095. if (ret)
  1096. return ret;
  1097. return 0;
  1098. }
  1099. static int glk_init_workarounds(struct intel_engine_cs *engine)
  1100. {
  1101. struct drm_i915_private *dev_priv = engine->i915;
  1102. int ret;
  1103. ret = gen9_init_workarounds(engine);
  1104. if (ret)
  1105. return ret;
  1106. /* WA #0862: Userspace has to set "Barrier Mode" to avoid hangs. */
  1107. ret = wa_ring_whitelist_reg(engine, GEN9_SLICE_COMMON_ECO_CHICKEN1);
  1108. if (ret)
  1109. return ret;
  1110. /* WaToEnableHwFixForPushConstHWBug:glk */
  1111. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1112. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1113. return 0;
  1114. }
  1115. static int cfl_init_workarounds(struct intel_engine_cs *engine)
  1116. {
  1117. struct drm_i915_private *dev_priv = engine->i915;
  1118. int ret;
  1119. ret = gen9_init_workarounds(engine);
  1120. if (ret)
  1121. return ret;
  1122. /* WaEnableGapsTsvCreditFix:cfl */
  1123. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  1124. GEN9_GAPS_TSV_CREDIT_DISABLE));
  1125. /* WaToEnableHwFixForPushConstHWBug:cfl */
  1126. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1127. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1128. /* WaDisableGafsUnitClkGating:cfl */
  1129. I915_WRITE(GEN7_UCGCTL4, (I915_READ(GEN7_UCGCTL4) |
  1130. GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE));
  1131. /* WaDisableSbeCacheDispatchPortSharing:cfl */
  1132. WA_SET_BIT_MASKED(
  1133. GEN7_HALF_SLICE_CHICKEN1,
  1134. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  1135. /* WaInPlaceDecompressionHang:cfl */
  1136. I915_WRITE(GEN9_GAMT_ECO_REG_RW_IA,
  1137. (I915_READ(GEN9_GAMT_ECO_REG_RW_IA) |
  1138. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS));
  1139. return 0;
  1140. }
  1141. int init_workarounds_ring(struct intel_engine_cs *engine)
  1142. {
  1143. struct drm_i915_private *dev_priv = engine->i915;
  1144. int err;
  1145. WARN_ON(engine->id != RCS);
  1146. dev_priv->workarounds.count = 0;
  1147. dev_priv->workarounds.hw_whitelist_count[engine->id] = 0;
  1148. if (IS_BROADWELL(dev_priv))
  1149. err = bdw_init_workarounds(engine);
  1150. else if (IS_CHERRYVIEW(dev_priv))
  1151. err = chv_init_workarounds(engine);
  1152. else if (IS_SKYLAKE(dev_priv))
  1153. err = skl_init_workarounds(engine);
  1154. else if (IS_BROXTON(dev_priv))
  1155. err = bxt_init_workarounds(engine);
  1156. else if (IS_KABYLAKE(dev_priv))
  1157. err = kbl_init_workarounds(engine);
  1158. else if (IS_GEMINILAKE(dev_priv))
  1159. err = glk_init_workarounds(engine);
  1160. else if (IS_COFFEELAKE(dev_priv))
  1161. err = cfl_init_workarounds(engine);
  1162. else if (IS_CANNONLAKE(dev_priv))
  1163. err = cnl_init_workarounds(engine);
  1164. else
  1165. err = 0;
  1166. if (err)
  1167. return err;
  1168. DRM_DEBUG_DRIVER("%s: Number of context specific w/a: %d\n",
  1169. engine->name, dev_priv->workarounds.count);
  1170. return 0;
  1171. }
  1172. int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
  1173. {
  1174. struct i915_workarounds *w = &req->i915->workarounds;
  1175. u32 *cs;
  1176. int ret, i;
  1177. if (w->count == 0)
  1178. return 0;
  1179. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  1180. if (ret)
  1181. return ret;
  1182. cs = intel_ring_begin(req, (w->count * 2 + 2));
  1183. if (IS_ERR(cs))
  1184. return PTR_ERR(cs);
  1185. *cs++ = MI_LOAD_REGISTER_IMM(w->count);
  1186. for (i = 0; i < w->count; i++) {
  1187. *cs++ = i915_mmio_reg_offset(w->reg[i].addr);
  1188. *cs++ = w->reg[i].value;
  1189. }
  1190. *cs++ = MI_NOOP;
  1191. intel_ring_advance(req, cs);
  1192. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  1193. if (ret)
  1194. return ret;
  1195. return 0;
  1196. }
  1197. static bool ring_is_idle(struct intel_engine_cs *engine)
  1198. {
  1199. struct drm_i915_private *dev_priv = engine->i915;
  1200. bool idle = true;
  1201. intel_runtime_pm_get(dev_priv);
  1202. /* First check that no commands are left in the ring */
  1203. if ((I915_READ_HEAD(engine) & HEAD_ADDR) !=
  1204. (I915_READ_TAIL(engine) & TAIL_ADDR))
  1205. idle = false;
  1206. /* No bit for gen2, so assume the CS parser is idle */
  1207. if (INTEL_GEN(dev_priv) > 2 && !(I915_READ_MODE(engine) & MODE_IDLE))
  1208. idle = false;
  1209. intel_runtime_pm_put(dev_priv);
  1210. return idle;
  1211. }
  1212. /**
  1213. * intel_engine_is_idle() - Report if the engine has finished process all work
  1214. * @engine: the intel_engine_cs
  1215. *
  1216. * Return true if there are no requests pending, nothing left to be submitted
  1217. * to hardware, and that the engine is idle.
  1218. */
  1219. bool intel_engine_is_idle(struct intel_engine_cs *engine)
  1220. {
  1221. struct drm_i915_private *dev_priv = engine->i915;
  1222. /* More white lies, if wedged, hw state is inconsistent */
  1223. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1224. return true;
  1225. /* Any inflight/incomplete requests? */
  1226. if (!i915_seqno_passed(intel_engine_get_seqno(engine),
  1227. intel_engine_last_submit(engine)))
  1228. return false;
  1229. if (I915_SELFTEST_ONLY(engine->breadcrumbs.mock))
  1230. return true;
  1231. /* Interrupt/tasklet pending? */
  1232. if (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted))
  1233. return false;
  1234. /* Waiting to drain ELSP? */
  1235. if (READ_ONCE(engine->execlists.active))
  1236. return false;
  1237. /* ELSP is empty, but there are ready requests? */
  1238. if (READ_ONCE(engine->execlists.first))
  1239. return false;
  1240. /* Ring stopped? */
  1241. if (!ring_is_idle(engine))
  1242. return false;
  1243. return true;
  1244. }
  1245. bool intel_engines_are_idle(struct drm_i915_private *dev_priv)
  1246. {
  1247. struct intel_engine_cs *engine;
  1248. enum intel_engine_id id;
  1249. /*
  1250. * If the driver is wedged, HW state may be very inconsistent and
  1251. * report that it is still busy, even though we have stopped using it.
  1252. */
  1253. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1254. return true;
  1255. for_each_engine(engine, dev_priv, id) {
  1256. if (!intel_engine_is_idle(engine))
  1257. return false;
  1258. }
  1259. return true;
  1260. }
  1261. /**
  1262. * intel_engine_has_kernel_context:
  1263. * @engine: the engine
  1264. *
  1265. * Returns true if the last context to be executed on this engine, or has been
  1266. * executed if the engine is already idle, is the kernel context
  1267. * (#i915.kernel_context).
  1268. */
  1269. bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine)
  1270. {
  1271. const struct i915_gem_context * const kernel_context =
  1272. engine->i915->kernel_context;
  1273. struct drm_i915_gem_request *rq;
  1274. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  1275. /*
  1276. * Check the last context seen by the engine. If active, it will be
  1277. * the last request that remains in the timeline. When idle, it is
  1278. * the last executed context as tracked by retirement.
  1279. */
  1280. rq = __i915_gem_active_peek(&engine->timeline->last_request);
  1281. if (rq)
  1282. return rq->ctx == kernel_context;
  1283. else
  1284. return engine->last_retired_context == kernel_context;
  1285. }
  1286. void intel_engines_reset_default_submission(struct drm_i915_private *i915)
  1287. {
  1288. struct intel_engine_cs *engine;
  1289. enum intel_engine_id id;
  1290. for_each_engine(engine, i915, id)
  1291. engine->set_default_submission(engine);
  1292. }
  1293. /**
  1294. * intel_engines_park: called when the GT is transitioning from busy->idle
  1295. * @i915: the i915 device
  1296. *
  1297. * The GT is now idle and about to go to sleep (maybe never to wake again?).
  1298. * Time for us to tidy and put away our toys (release resources back to the
  1299. * system).
  1300. */
  1301. void intel_engines_park(struct drm_i915_private *i915)
  1302. {
  1303. struct intel_engine_cs *engine;
  1304. enum intel_engine_id id;
  1305. for_each_engine(engine, i915, id) {
  1306. /* Flush the residual irq tasklets first. */
  1307. intel_engine_disarm_breadcrumbs(engine);
  1308. tasklet_kill(&engine->execlists.tasklet);
  1309. /*
  1310. * We are committed now to parking the engines, make sure there
  1311. * will be no more interrupts arriving later and the engines
  1312. * are truly idle.
  1313. */
  1314. if (wait_for(intel_engine_is_idle(engine), 10)) {
  1315. struct drm_printer p = drm_debug_printer(__func__);
  1316. dev_err(i915->drm.dev,
  1317. "%s is not idle before parking\n",
  1318. engine->name);
  1319. intel_engine_dump(engine, &p, NULL);
  1320. }
  1321. if (engine->park)
  1322. engine->park(engine);
  1323. i915_gem_batch_pool_fini(&engine->batch_pool);
  1324. engine->execlists.no_priolist = false;
  1325. }
  1326. }
  1327. /**
  1328. * intel_engines_unpark: called when the GT is transitioning from idle->busy
  1329. * @i915: the i915 device
  1330. *
  1331. * The GT was idle and now about to fire up with some new user requests.
  1332. */
  1333. void intel_engines_unpark(struct drm_i915_private *i915)
  1334. {
  1335. struct intel_engine_cs *engine;
  1336. enum intel_engine_id id;
  1337. for_each_engine(engine, i915, id) {
  1338. if (engine->unpark)
  1339. engine->unpark(engine);
  1340. }
  1341. }
  1342. bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
  1343. {
  1344. switch (INTEL_GEN(engine->i915)) {
  1345. case 2:
  1346. return false; /* uses physical not virtual addresses */
  1347. case 3:
  1348. /* maybe only uses physical not virtual addresses */
  1349. return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
  1350. case 6:
  1351. return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
  1352. default:
  1353. return true;
  1354. }
  1355. }
  1356. unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
  1357. {
  1358. struct intel_engine_cs *engine;
  1359. enum intel_engine_id id;
  1360. unsigned int which;
  1361. which = 0;
  1362. for_each_engine(engine, i915, id)
  1363. if (engine->default_state)
  1364. which |= BIT(engine->uabi_class);
  1365. return which;
  1366. }
  1367. static void print_request(struct drm_printer *m,
  1368. struct drm_i915_gem_request *rq,
  1369. const char *prefix)
  1370. {
  1371. drm_printf(m, "%s%x%s [%x:%x] prio=%d @ %dms: %s\n", prefix,
  1372. rq->global_seqno,
  1373. i915_gem_request_completed(rq) ? "!" : "",
  1374. rq->ctx->hw_id, rq->fence.seqno,
  1375. rq->priotree.priority,
  1376. jiffies_to_msecs(jiffies - rq->emitted_jiffies),
  1377. rq->timeline->common->name);
  1378. }
  1379. static void hexdump(struct drm_printer *m, const void *buf, size_t len)
  1380. {
  1381. const size_t rowsize = 8 * sizeof(u32);
  1382. const void *prev = NULL;
  1383. bool skip = false;
  1384. size_t pos;
  1385. for (pos = 0; pos < len; pos += rowsize) {
  1386. char line[128];
  1387. if (prev && !memcmp(prev, buf + pos, rowsize)) {
  1388. if (!skip) {
  1389. drm_printf(m, "*\n");
  1390. skip = true;
  1391. }
  1392. continue;
  1393. }
  1394. WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
  1395. rowsize, sizeof(u32),
  1396. line, sizeof(line),
  1397. false) >= sizeof(line));
  1398. drm_printf(m, "%08zx %s\n", pos, line);
  1399. prev = buf + pos;
  1400. skip = false;
  1401. }
  1402. }
  1403. void intel_engine_dump(struct intel_engine_cs *engine,
  1404. struct drm_printer *m,
  1405. const char *header, ...)
  1406. {
  1407. struct intel_breadcrumbs * const b = &engine->breadcrumbs;
  1408. const struct intel_engine_execlists * const execlists = &engine->execlists;
  1409. struct i915_gpu_error * const error = &engine->i915->gpu_error;
  1410. struct drm_i915_private *dev_priv = engine->i915;
  1411. struct drm_i915_gem_request *rq;
  1412. struct rb_node *rb;
  1413. char hdr[80];
  1414. u64 addr;
  1415. if (header) {
  1416. va_list ap;
  1417. va_start(ap, header);
  1418. drm_vprintf(m, header, &ap);
  1419. va_end(ap);
  1420. }
  1421. if (i915_terminally_wedged(&engine->i915->gpu_error))
  1422. drm_printf(m, "*** WEDGED ***\n");
  1423. drm_printf(m, "\tcurrent seqno %x, last %x, hangcheck %x [%d ms], inflight %d\n",
  1424. intel_engine_get_seqno(engine),
  1425. intel_engine_last_submit(engine),
  1426. engine->hangcheck.seqno,
  1427. jiffies_to_msecs(jiffies - engine->hangcheck.action_timestamp),
  1428. engine->timeline->inflight_seqnos);
  1429. drm_printf(m, "\tReset count: %d (global %d)\n",
  1430. i915_reset_engine_count(error, engine),
  1431. i915_reset_count(error));
  1432. rcu_read_lock();
  1433. drm_printf(m, "\tRequests:\n");
  1434. rq = list_first_entry(&engine->timeline->requests,
  1435. struct drm_i915_gem_request, link);
  1436. if (&rq->link != &engine->timeline->requests)
  1437. print_request(m, rq, "\t\tfirst ");
  1438. rq = list_last_entry(&engine->timeline->requests,
  1439. struct drm_i915_gem_request, link);
  1440. if (&rq->link != &engine->timeline->requests)
  1441. print_request(m, rq, "\t\tlast ");
  1442. rq = i915_gem_find_active_request(engine);
  1443. if (rq) {
  1444. print_request(m, rq, "\t\tactive ");
  1445. drm_printf(m,
  1446. "\t\t[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]\n",
  1447. rq->head, rq->postfix, rq->tail,
  1448. rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
  1449. rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
  1450. }
  1451. drm_printf(m, "\tRING_START: 0x%08x [0x%08x]\n",
  1452. I915_READ(RING_START(engine->mmio_base)),
  1453. rq ? i915_ggtt_offset(rq->ring->vma) : 0);
  1454. drm_printf(m, "\tRING_HEAD: 0x%08x [0x%08x]\n",
  1455. I915_READ(RING_HEAD(engine->mmio_base)) & HEAD_ADDR,
  1456. rq ? rq->ring->head : 0);
  1457. drm_printf(m, "\tRING_TAIL: 0x%08x [0x%08x]\n",
  1458. I915_READ(RING_TAIL(engine->mmio_base)) & TAIL_ADDR,
  1459. rq ? rq->ring->tail : 0);
  1460. drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
  1461. I915_READ(RING_CTL(engine->mmio_base)),
  1462. I915_READ(RING_CTL(engine->mmio_base)) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
  1463. if (INTEL_GEN(engine->i915) > 2) {
  1464. drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
  1465. I915_READ(RING_MI_MODE(engine->mmio_base)),
  1466. I915_READ(RING_MI_MODE(engine->mmio_base)) & (MODE_IDLE) ? " [idle]" : "");
  1467. }
  1468. if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
  1469. drm_printf(m, "\tSYNC_0: 0x%08x\n",
  1470. I915_READ(RING_SYNC_0(engine->mmio_base)));
  1471. drm_printf(m, "\tSYNC_1: 0x%08x\n",
  1472. I915_READ(RING_SYNC_1(engine->mmio_base)));
  1473. if (HAS_VEBOX(dev_priv))
  1474. drm_printf(m, "\tSYNC_2: 0x%08x\n",
  1475. I915_READ(RING_SYNC_2(engine->mmio_base)));
  1476. }
  1477. rcu_read_unlock();
  1478. addr = intel_engine_get_active_head(engine);
  1479. drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
  1480. upper_32_bits(addr), lower_32_bits(addr));
  1481. addr = intel_engine_get_last_batch_head(engine);
  1482. drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
  1483. upper_32_bits(addr), lower_32_bits(addr));
  1484. if (INTEL_GEN(dev_priv) >= 8)
  1485. addr = I915_READ64_2x32(RING_DMA_FADD(engine->mmio_base),
  1486. RING_DMA_FADD_UDW(engine->mmio_base));
  1487. else if (INTEL_GEN(dev_priv) >= 4)
  1488. addr = I915_READ(RING_DMA_FADD(engine->mmio_base));
  1489. else
  1490. addr = I915_READ(DMA_FADD_I8XX);
  1491. drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
  1492. upper_32_bits(addr), lower_32_bits(addr));
  1493. if (INTEL_GEN(dev_priv) >= 4) {
  1494. drm_printf(m, "\tIPEIR: 0x%08x\n",
  1495. I915_READ(RING_IPEIR(engine->mmio_base)));
  1496. drm_printf(m, "\tIPEHR: 0x%08x\n",
  1497. I915_READ(RING_IPEHR(engine->mmio_base)));
  1498. } else {
  1499. drm_printf(m, "\tIPEIR: 0x%08x\n", I915_READ(IPEIR));
  1500. drm_printf(m, "\tIPEHR: 0x%08x\n", I915_READ(IPEHR));
  1501. }
  1502. if (HAS_EXECLISTS(dev_priv)) {
  1503. const u32 *hws = &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
  1504. u32 ptr, read, write;
  1505. unsigned int idx;
  1506. drm_printf(m, "\tExeclist status: 0x%08x %08x\n",
  1507. I915_READ(RING_EXECLIST_STATUS_LO(engine)),
  1508. I915_READ(RING_EXECLIST_STATUS_HI(engine)));
  1509. ptr = I915_READ(RING_CONTEXT_STATUS_PTR(engine));
  1510. read = GEN8_CSB_READ_PTR(ptr);
  1511. write = GEN8_CSB_WRITE_PTR(ptr);
  1512. drm_printf(m, "\tExeclist CSB read %d [%d cached], write %d [%d from hws], interrupt posted? %s\n",
  1513. read, execlists->csb_head,
  1514. write,
  1515. intel_read_status_page(engine, intel_hws_csb_write_index(engine->i915)),
  1516. yesno(test_bit(ENGINE_IRQ_EXECLIST,
  1517. &engine->irq_posted)));
  1518. if (read >= GEN8_CSB_ENTRIES)
  1519. read = 0;
  1520. if (write >= GEN8_CSB_ENTRIES)
  1521. write = 0;
  1522. if (read > write)
  1523. write += GEN8_CSB_ENTRIES;
  1524. while (read < write) {
  1525. idx = ++read % GEN8_CSB_ENTRIES;
  1526. drm_printf(m, "\tExeclist CSB[%d]: 0x%08x [0x%08x in hwsp], context: %d [%d in hwsp]\n",
  1527. idx,
  1528. I915_READ(RING_CONTEXT_STATUS_BUF_LO(engine, idx)),
  1529. hws[idx * 2],
  1530. I915_READ(RING_CONTEXT_STATUS_BUF_HI(engine, idx)),
  1531. hws[idx * 2 + 1]);
  1532. }
  1533. rcu_read_lock();
  1534. for (idx = 0; idx < execlists_num_ports(execlists); idx++) {
  1535. unsigned int count;
  1536. rq = port_unpack(&execlists->port[idx], &count);
  1537. if (rq) {
  1538. snprintf(hdr, sizeof(hdr),
  1539. "\t\tELSP[%d] count=%d, rq: ",
  1540. idx, count);
  1541. print_request(m, rq, hdr);
  1542. } else {
  1543. drm_printf(m, "\t\tELSP[%d] idle\n", idx);
  1544. }
  1545. }
  1546. drm_printf(m, "\t\tHW active? 0x%x\n", execlists->active);
  1547. rcu_read_unlock();
  1548. } else if (INTEL_GEN(dev_priv) > 6) {
  1549. drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
  1550. I915_READ(RING_PP_DIR_BASE(engine)));
  1551. drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
  1552. I915_READ(RING_PP_DIR_BASE_READ(engine)));
  1553. drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
  1554. I915_READ(RING_PP_DIR_DCLV(engine)));
  1555. }
  1556. spin_lock_irq(&engine->timeline->lock);
  1557. list_for_each_entry(rq, &engine->timeline->requests, link)
  1558. print_request(m, rq, "\t\tE ");
  1559. for (rb = execlists->first; rb; rb = rb_next(rb)) {
  1560. struct i915_priolist *p =
  1561. rb_entry(rb, typeof(*p), node);
  1562. list_for_each_entry(rq, &p->requests, priotree.link)
  1563. print_request(m, rq, "\t\tQ ");
  1564. }
  1565. spin_unlock_irq(&engine->timeline->lock);
  1566. spin_lock_irq(&b->rb_lock);
  1567. for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
  1568. struct intel_wait *w = rb_entry(rb, typeof(*w), node);
  1569. drm_printf(m, "\t%s [%d] waiting for %x\n",
  1570. w->tsk->comm, w->tsk->pid, w->seqno);
  1571. }
  1572. spin_unlock_irq(&b->rb_lock);
  1573. if (INTEL_GEN(dev_priv) >= 6) {
  1574. drm_printf(m, "\tRING_IMR: %08x\n", I915_READ_IMR(engine));
  1575. }
  1576. drm_printf(m, "IRQ? 0x%lx (breadcrumbs? %s) (execlists? %s)\n",
  1577. engine->irq_posted,
  1578. yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
  1579. &engine->irq_posted)),
  1580. yesno(test_bit(ENGINE_IRQ_EXECLIST,
  1581. &engine->irq_posted)));
  1582. drm_printf(m, "HWSP:\n");
  1583. hexdump(m, engine->status_page.page_addr, PAGE_SIZE);
  1584. drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
  1585. }
  1586. static u8 user_class_map[] = {
  1587. [I915_ENGINE_CLASS_RENDER] = RENDER_CLASS,
  1588. [I915_ENGINE_CLASS_COPY] = COPY_ENGINE_CLASS,
  1589. [I915_ENGINE_CLASS_VIDEO] = VIDEO_DECODE_CLASS,
  1590. [I915_ENGINE_CLASS_VIDEO_ENHANCE] = VIDEO_ENHANCEMENT_CLASS,
  1591. };
  1592. struct intel_engine_cs *
  1593. intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
  1594. {
  1595. if (class >= ARRAY_SIZE(user_class_map))
  1596. return NULL;
  1597. class = user_class_map[class];
  1598. GEM_BUG_ON(class > MAX_ENGINE_CLASS);
  1599. if (instance > MAX_ENGINE_INSTANCE)
  1600. return NULL;
  1601. return i915->engine_class[class][instance];
  1602. }
  1603. /**
  1604. * intel_enable_engine_stats() - Enable engine busy tracking on engine
  1605. * @engine: engine to enable stats collection
  1606. *
  1607. * Start collecting the engine busyness data for @engine.
  1608. *
  1609. * Returns 0 on success or a negative error code.
  1610. */
  1611. int intel_enable_engine_stats(struct intel_engine_cs *engine)
  1612. {
  1613. struct intel_engine_execlists *execlists = &engine->execlists;
  1614. unsigned long flags;
  1615. int err = 0;
  1616. if (!intel_engine_supports_stats(engine))
  1617. return -ENODEV;
  1618. tasklet_disable(&execlists->tasklet);
  1619. spin_lock_irqsave(&engine->stats.lock, flags);
  1620. if (unlikely(engine->stats.enabled == ~0)) {
  1621. err = -EBUSY;
  1622. goto unlock;
  1623. }
  1624. if (engine->stats.enabled++ == 0) {
  1625. const struct execlist_port *port = execlists->port;
  1626. unsigned int num_ports = execlists_num_ports(execlists);
  1627. engine->stats.enabled_at = ktime_get();
  1628. /* XXX submission method oblivious? */
  1629. while (num_ports-- && port_isset(port)) {
  1630. engine->stats.active++;
  1631. port++;
  1632. }
  1633. if (engine->stats.active)
  1634. engine->stats.start = engine->stats.enabled_at;
  1635. }
  1636. unlock:
  1637. spin_unlock_irqrestore(&engine->stats.lock, flags);
  1638. tasklet_enable(&execlists->tasklet);
  1639. return err;
  1640. }
  1641. static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1642. {
  1643. ktime_t total = engine->stats.total;
  1644. /*
  1645. * If the engine is executing something at the moment
  1646. * add it to the total.
  1647. */
  1648. if (engine->stats.active)
  1649. total = ktime_add(total,
  1650. ktime_sub(ktime_get(), engine->stats.start));
  1651. return total;
  1652. }
  1653. /**
  1654. * intel_engine_get_busy_time() - Return current accumulated engine busyness
  1655. * @engine: engine to report on
  1656. *
  1657. * Returns accumulated time @engine was busy since engine stats were enabled.
  1658. */
  1659. ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine)
  1660. {
  1661. ktime_t total;
  1662. unsigned long flags;
  1663. spin_lock_irqsave(&engine->stats.lock, flags);
  1664. total = __intel_engine_get_busy_time(engine);
  1665. spin_unlock_irqrestore(&engine->stats.lock, flags);
  1666. return total;
  1667. }
  1668. /**
  1669. * intel_disable_engine_stats() - Disable engine busy tracking on engine
  1670. * @engine: engine to disable stats collection
  1671. *
  1672. * Stops collecting the engine busyness data for @engine.
  1673. */
  1674. void intel_disable_engine_stats(struct intel_engine_cs *engine)
  1675. {
  1676. unsigned long flags;
  1677. if (!intel_engine_supports_stats(engine))
  1678. return;
  1679. spin_lock_irqsave(&engine->stats.lock, flags);
  1680. WARN_ON_ONCE(engine->stats.enabled == 0);
  1681. if (--engine->stats.enabled == 0) {
  1682. engine->stats.total = __intel_engine_get_busy_time(engine);
  1683. engine->stats.active = 0;
  1684. }
  1685. spin_unlock_irqrestore(&engine->stats.lock, flags);
  1686. }
  1687. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1688. #include "selftests/mock_engine.c"
  1689. #endif