intel_gt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include <drm/drm_managed.h>
  6. #include <drm/intel/intel-gtt.h>
  7. #include "gem/i915_gem_internal.h"
  8. #include "gem/i915_gem_lmem.h"
  9. #include "i915_drv.h"
  10. #include "i915_perf_oa_regs.h"
  11. #include "i915_reg.h"
  12. #include "intel_context.h"
  13. #include "intel_engine_pm.h"
  14. #include "intel_engine_regs.h"
  15. #include "intel_ggtt_gmch.h"
  16. #include "intel_gt.h"
  17. #include "intel_gt_buffer_pool.h"
  18. #include "intel_gt_clock_utils.h"
  19. #include "intel_gt_debugfs.h"
  20. #include "intel_gt_mcr.h"
  21. #include "intel_gt_pm.h"
  22. #include "intel_gt_print.h"
  23. #include "intel_gt_regs.h"
  24. #include "intel_gt_requests.h"
  25. #include "intel_migrate.h"
  26. #include "intel_mocs.h"
  27. #include "intel_pci_config.h"
  28. #include "intel_rc6.h"
  29. #include "intel_renderstate.h"
  30. #include "intel_rps.h"
  31. #include "intel_sa_media.h"
  32. #include "intel_gt_sysfs.h"
  33. #include "intel_tlb.h"
  34. #include "intel_uncore.h"
  35. #include "shmem_utils.h"
  36. void intel_gt_common_init_early(struct intel_gt *gt)
  37. {
  38. spin_lock_init(gt->irq_lock);
  39. INIT_LIST_HEAD(&gt->closed_vma);
  40. spin_lock_init(&gt->closed_lock);
  41. init_llist_head(&gt->watchdog.list);
  42. INIT_WORK(&gt->watchdog.work, intel_gt_watchdog_work);
  43. intel_gt_init_buffer_pool(gt);
  44. intel_gt_init_reset(gt);
  45. intel_gt_init_requests(gt);
  46. intel_gt_init_timelines(gt);
  47. intel_gt_init_tlb(gt);
  48. intel_gt_pm_init_early(gt);
  49. intel_wopcm_init_early(&gt->wopcm);
  50. intel_uc_init_early(&gt->uc);
  51. intel_rps_init_early(&gt->rps);
  52. }
  53. /* Preliminary initialization of Tile 0 */
  54. int intel_root_gt_init_early(struct drm_i915_private *i915)
  55. {
  56. struct intel_gt *gt;
  57. gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL);
  58. if (!gt)
  59. return -ENOMEM;
  60. i915->gt[0] = gt;
  61. gt->i915 = i915;
  62. gt->uncore = &i915->uncore;
  63. gt->irq_lock = drmm_kzalloc(&i915->drm, sizeof(*gt->irq_lock), GFP_KERNEL);
  64. if (!gt->irq_lock)
  65. return -ENOMEM;
  66. intel_gt_common_init_early(gt);
  67. return 0;
  68. }
  69. static int intel_gt_probe_lmem(struct intel_gt *gt)
  70. {
  71. struct drm_i915_private *i915 = gt->i915;
  72. unsigned int instance = gt->info.id;
  73. int id = INTEL_REGION_LMEM_0 + instance;
  74. struct intel_memory_region *mem;
  75. int err;
  76. mem = intel_gt_setup_lmem(gt);
  77. if (IS_ERR(mem)) {
  78. err = PTR_ERR(mem);
  79. if (err == -ENODEV)
  80. return 0;
  81. gt_err(gt, "Failed to setup region(%d) type=%d\n",
  82. err, INTEL_MEMORY_LOCAL);
  83. return err;
  84. }
  85. mem->id = id;
  86. mem->instance = instance;
  87. intel_memory_region_set_name(mem, "local%u", mem->instance);
  88. GEM_BUG_ON(!HAS_REGION(i915, id));
  89. GEM_BUG_ON(i915->mm.regions[id]);
  90. i915->mm.regions[id] = mem;
  91. return 0;
  92. }
  93. int intel_gt_assign_ggtt(struct intel_gt *gt)
  94. {
  95. /* Media GT shares primary GT's GGTT */
  96. if (gt->type == GT_MEDIA) {
  97. gt->ggtt = to_gt(gt->i915)->ggtt;
  98. } else {
  99. gt->ggtt = i915_ggtt_create(gt->i915);
  100. if (IS_ERR(gt->ggtt))
  101. return PTR_ERR(gt->ggtt);
  102. }
  103. list_add_tail(&gt->ggtt_link, &gt->ggtt->gt_list);
  104. return 0;
  105. }
  106. int intel_gt_init_mmio(struct intel_gt *gt)
  107. {
  108. intel_gt_init_clock_frequency(gt);
  109. intel_uc_init_mmio(&gt->uc);
  110. intel_sseu_info_init(gt);
  111. intel_gt_mcr_init(gt);
  112. return intel_engines_init_mmio(gt);
  113. }
  114. static void init_unused_ring(struct intel_gt *gt, u32 base)
  115. {
  116. struct intel_uncore *uncore = gt->uncore;
  117. intel_uncore_write(uncore, RING_CTL(base), 0);
  118. intel_uncore_write(uncore, RING_HEAD(base), 0);
  119. intel_uncore_write(uncore, RING_TAIL(base), 0);
  120. intel_uncore_write(uncore, RING_START(base), 0);
  121. }
  122. static void init_unused_rings(struct intel_gt *gt)
  123. {
  124. struct drm_i915_private *i915 = gt->i915;
  125. if (IS_I830(i915)) {
  126. init_unused_ring(gt, PRB1_BASE);
  127. init_unused_ring(gt, SRB0_BASE);
  128. init_unused_ring(gt, SRB1_BASE);
  129. init_unused_ring(gt, SRB2_BASE);
  130. init_unused_ring(gt, SRB3_BASE);
  131. } else if (GRAPHICS_VER(i915) == 2) {
  132. init_unused_ring(gt, SRB0_BASE);
  133. init_unused_ring(gt, SRB1_BASE);
  134. } else if (GRAPHICS_VER(i915) == 3) {
  135. init_unused_ring(gt, PRB1_BASE);
  136. init_unused_ring(gt, PRB2_BASE);
  137. }
  138. }
  139. int intel_gt_init_hw(struct intel_gt *gt)
  140. {
  141. struct drm_i915_private *i915 = gt->i915;
  142. struct intel_uncore *uncore = gt->uncore;
  143. int ret;
  144. gt->last_init_time = ktime_get();
  145. /* Double layer security blanket, see i915_gem_init() */
  146. intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
  147. if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9)
  148. intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
  149. if (IS_HASWELL(i915))
  150. intel_uncore_write(uncore,
  151. HSW_MI_PREDICATE_RESULT_2,
  152. INTEL_INFO(i915)->gt == 3 ?
  153. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  154. /* Apply the GT workarounds... */
  155. intel_gt_apply_workarounds(gt);
  156. /* ...and determine whether they are sticking. */
  157. intel_gt_verify_workarounds(gt, "init");
  158. intel_gt_init_swizzling(gt);
  159. /*
  160. * At least 830 can leave some of the unused rings
  161. * "active" (ie. head != tail) after resume which
  162. * will prevent c3 entry. Makes sure all unused rings
  163. * are totally idle.
  164. */
  165. init_unused_rings(gt);
  166. ret = i915_ppgtt_init_hw(gt);
  167. if (ret) {
  168. gt_err(gt, "Enabling PPGTT failed (%d)\n", ret);
  169. goto out;
  170. }
  171. /* We can't enable contexts until all firmware is loaded */
  172. ret = intel_uc_init_hw(&gt->uc);
  173. if (ret) {
  174. gt_probe_error(gt, "Enabling uc failed (%d)\n", ret);
  175. goto out;
  176. }
  177. intel_mocs_init(gt);
  178. out:
  179. intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
  180. return ret;
  181. }
  182. static void gen6_clear_engine_error_register(struct intel_engine_cs *engine)
  183. {
  184. GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
  185. GEN6_RING_FAULT_REG_POSTING_READ(engine);
  186. }
  187. i915_reg_t intel_gt_perf_limit_reasons_reg(struct intel_gt *gt)
  188. {
  189. /* GT0_PERF_LIMIT_REASONS is available only for Gen11+ */
  190. if (GRAPHICS_VER(gt->i915) < 11)
  191. return INVALID_MMIO_REG;
  192. return gt->type == GT_MEDIA ?
  193. MTL_MEDIA_PERF_LIMIT_REASONS : GT0_PERF_LIMIT_REASONS;
  194. }
  195. void
  196. intel_gt_clear_error_registers(struct intel_gt *gt,
  197. intel_engine_mask_t engine_mask)
  198. {
  199. struct drm_i915_private *i915 = gt->i915;
  200. struct intel_uncore *uncore = gt->uncore;
  201. u32 eir;
  202. if (GRAPHICS_VER(i915) != 2)
  203. intel_uncore_write(uncore, PGTBL_ER, 0);
  204. if (GRAPHICS_VER(i915) < 4)
  205. intel_uncore_write(uncore, IPEIR(RENDER_RING_BASE), 0);
  206. else
  207. intel_uncore_write(uncore, IPEIR_I965, 0);
  208. intel_uncore_write(uncore, EIR, 0);
  209. eir = intel_uncore_read(uncore, EIR);
  210. if (eir) {
  211. /*
  212. * some errors might have become stuck,
  213. * mask them.
  214. */
  215. gt_dbg(gt, "EIR stuck: 0x%08x, masking\n", eir);
  216. intel_uncore_rmw(uncore, EMR, 0, eir);
  217. intel_uncore_write(uncore, GEN2_IIR,
  218. I915_MASTER_ERROR_INTERRUPT);
  219. }
  220. /*
  221. * For the media GT, this ring fault register is not replicated,
  222. * so don't do multicast/replicated register read/write operation on it.
  223. */
  224. if (MEDIA_VER(i915) >= 13 && gt->type == GT_MEDIA) {
  225. intel_uncore_rmw(uncore, XELPMP_RING_FAULT_REG,
  226. RING_FAULT_VALID, 0);
  227. intel_uncore_posting_read(uncore,
  228. XELPMP_RING_FAULT_REG);
  229. } else if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55)) {
  230. intel_gt_mcr_multicast_rmw(gt, XEHP_RING_FAULT_REG,
  231. RING_FAULT_VALID, 0);
  232. intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG);
  233. } else if (GRAPHICS_VER(i915) >= 12) {
  234. intel_uncore_rmw(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID, 0);
  235. intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG);
  236. } else if (GRAPHICS_VER(i915) >= 8) {
  237. intel_uncore_rmw(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID, 0);
  238. intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
  239. } else if (GRAPHICS_VER(i915) >= 6) {
  240. struct intel_engine_cs *engine;
  241. enum intel_engine_id id;
  242. for_each_engine_masked(engine, gt, engine_mask, id)
  243. gen6_clear_engine_error_register(engine);
  244. }
  245. }
  246. static void gen6_check_faults(struct intel_gt *gt)
  247. {
  248. struct intel_engine_cs *engine;
  249. enum intel_engine_id id;
  250. for_each_engine(engine, gt, id) {
  251. u32 fault;
  252. fault = GEN6_RING_FAULT_REG_READ(engine);
  253. if (fault & RING_FAULT_VALID) {
  254. gt_dbg(gt, "Unexpected fault\n"
  255. "\tAddr: 0x%08x\n"
  256. "\tAddress space: %s\n"
  257. "\tSource ID: %d\n"
  258. "\tType: %d\n",
  259. fault & RING_FAULT_VADDR_MASK,
  260. fault & RING_FAULT_GTTSEL_MASK ?
  261. "GGTT" : "PPGTT",
  262. REG_FIELD_GET(RING_FAULT_SRCID_MASK, fault),
  263. REG_FIELD_GET(RING_FAULT_FAULT_TYPE_MASK, fault));
  264. }
  265. }
  266. }
  267. static void gen8_report_fault(struct intel_gt *gt, u32 fault,
  268. u32 fault_data0, u32 fault_data1)
  269. {
  270. u64 fault_addr;
  271. fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
  272. ((u64)fault_data0 << 12);
  273. gt_dbg(gt, "Unexpected fault\n"
  274. "\tAddr: 0x%08x_%08x\n"
  275. "\tAddress space: %s\n"
  276. "\tEngine ID: %d\n"
  277. "\tSource ID: %d\n"
  278. "\tType: %d\n",
  279. upper_32_bits(fault_addr), lower_32_bits(fault_addr),
  280. fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
  281. REG_FIELD_GET(RING_FAULT_ENGINE_ID_MASK, fault),
  282. REG_FIELD_GET(RING_FAULT_SRCID_MASK, fault),
  283. REG_FIELD_GET(RING_FAULT_FAULT_TYPE_MASK, fault));
  284. }
  285. static void xehp_check_faults(struct intel_gt *gt)
  286. {
  287. u32 fault;
  288. /*
  289. * Although the fault register now lives in an MCR register range,
  290. * the GAM registers are special and we only truly need to read
  291. * the "primary" GAM instance rather than handling each instance
  292. * individually. intel_gt_mcr_read_any() will automatically steer
  293. * toward the primary instance.
  294. */
  295. fault = intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG);
  296. if (fault & RING_FAULT_VALID)
  297. gen8_report_fault(gt, fault,
  298. intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA0),
  299. intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA1));
  300. }
  301. static void gen8_check_faults(struct intel_gt *gt)
  302. {
  303. struct intel_uncore *uncore = gt->uncore;
  304. i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg;
  305. u32 fault;
  306. if (GRAPHICS_VER(gt->i915) >= 12) {
  307. fault_reg = GEN12_RING_FAULT_REG;
  308. fault_data0_reg = GEN12_FAULT_TLB_DATA0;
  309. fault_data1_reg = GEN12_FAULT_TLB_DATA1;
  310. } else {
  311. fault_reg = GEN8_RING_FAULT_REG;
  312. fault_data0_reg = GEN8_FAULT_TLB_DATA0;
  313. fault_data1_reg = GEN8_FAULT_TLB_DATA1;
  314. }
  315. fault = intel_uncore_read(uncore, fault_reg);
  316. if (fault & RING_FAULT_VALID)
  317. gen8_report_fault(gt, fault,
  318. intel_uncore_read(uncore, fault_data0_reg),
  319. intel_uncore_read(uncore, fault_data1_reg));
  320. }
  321. void intel_gt_check_and_clear_faults(struct intel_gt *gt)
  322. {
  323. struct drm_i915_private *i915 = gt->i915;
  324. /* From GEN8 onwards we only have one 'All Engine Fault Register' */
  325. if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55))
  326. xehp_check_faults(gt);
  327. else if (GRAPHICS_VER(i915) >= 8)
  328. gen8_check_faults(gt);
  329. else if (GRAPHICS_VER(i915) >= 6)
  330. gen6_check_faults(gt);
  331. else
  332. return;
  333. intel_gt_clear_error_registers(gt, ALL_ENGINES);
  334. }
  335. void intel_gt_flush_ggtt_writes(struct intel_gt *gt)
  336. {
  337. struct intel_uncore *uncore = gt->uncore;
  338. intel_wakeref_t wakeref;
  339. /*
  340. * No actual flushing is required for the GTT write domain for reads
  341. * from the GTT domain. Writes to it "immediately" go to main memory
  342. * as far as we know, so there's no chipset flush. It also doesn't
  343. * land in the GPU render cache.
  344. *
  345. * However, we do have to enforce the order so that all writes through
  346. * the GTT land before any writes to the device, such as updates to
  347. * the GATT itself.
  348. *
  349. * We also have to wait a bit for the writes to land from the GTT.
  350. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  351. * timing. This issue has only been observed when switching quickly
  352. * between GTT writes and CPU reads from inside the kernel on recent hw,
  353. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  354. * system agents we cannot reproduce this behaviour, until Cannonlake
  355. * that was!).
  356. */
  357. wmb();
  358. if (INTEL_INFO(gt->i915)->has_coherent_ggtt)
  359. return;
  360. intel_gt_chipset_flush(gt);
  361. with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) {
  362. unsigned long flags;
  363. spin_lock_irqsave(&uncore->lock, flags);
  364. intel_uncore_posting_read_fw(uncore,
  365. RING_TAIL(RENDER_RING_BASE));
  366. spin_unlock_irqrestore(&uncore->lock, flags);
  367. }
  368. }
  369. void intel_gt_chipset_flush(struct intel_gt *gt)
  370. {
  371. wmb();
  372. if (GRAPHICS_VER(gt->i915) < 6)
  373. intel_ggtt_gmch_flush();
  374. }
  375. void intel_gt_driver_register(struct intel_gt *gt)
  376. {
  377. intel_gsc_init(&gt->gsc, gt->i915);
  378. intel_rps_driver_register(&gt->rps);
  379. intel_gt_debugfs_register(gt);
  380. intel_gt_sysfs_register(gt);
  381. }
  382. static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size)
  383. {
  384. struct drm_i915_private *i915 = gt->i915;
  385. struct drm_i915_gem_object *obj;
  386. struct i915_vma *vma;
  387. int ret;
  388. obj = i915_gem_object_create_lmem(i915, size,
  389. I915_BO_ALLOC_VOLATILE |
  390. I915_BO_ALLOC_GPU_ONLY);
  391. if (IS_ERR(obj) && !IS_METEORLAKE(i915)) /* Wa_22018444074 */
  392. obj = i915_gem_object_create_stolen(i915, size);
  393. if (IS_ERR(obj))
  394. obj = i915_gem_object_create_internal(i915, size);
  395. if (IS_ERR(obj)) {
  396. gt_err(gt, "Failed to allocate scratch page\n");
  397. return PTR_ERR(obj);
  398. }
  399. vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
  400. if (IS_ERR(vma)) {
  401. ret = PTR_ERR(vma);
  402. goto err_unref;
  403. }
  404. ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
  405. if (ret)
  406. goto err_unref;
  407. gt->scratch = i915_vma_make_unshrinkable(vma);
  408. return 0;
  409. err_unref:
  410. i915_gem_object_put(obj);
  411. return ret;
  412. }
  413. static void intel_gt_fini_scratch(struct intel_gt *gt)
  414. {
  415. i915_vma_unpin_and_release(&gt->scratch, 0);
  416. }
  417. static struct i915_address_space *kernel_vm(struct intel_gt *gt)
  418. {
  419. if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING)
  420. return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm;
  421. else
  422. return i915_vm_get(&gt->ggtt->vm);
  423. }
  424. static int __engines_record_defaults(struct intel_gt *gt)
  425. {
  426. struct i915_request *requests[I915_NUM_ENGINES] = {};
  427. struct intel_engine_cs *engine;
  428. enum intel_engine_id id;
  429. int err = 0;
  430. /*
  431. * As we reset the gpu during very early sanitisation, the current
  432. * register state on the GPU should reflect its defaults values.
  433. * We load a context onto the hw (with restore-inhibit), then switch
  434. * over to a second context to save that default register state. We
  435. * can then prime every new context with that state so they all start
  436. * from the same default HW values.
  437. */
  438. for_each_engine(engine, gt, id) {
  439. struct intel_renderstate so;
  440. struct intel_context *ce;
  441. struct i915_request *rq;
  442. /* We must be able to switch to something! */
  443. GEM_BUG_ON(!engine->kernel_context);
  444. ce = intel_context_create(engine);
  445. if (IS_ERR(ce)) {
  446. err = PTR_ERR(ce);
  447. goto out;
  448. }
  449. err = intel_renderstate_init(&so, ce);
  450. if (err)
  451. goto err;
  452. rq = i915_request_create(ce);
  453. if (IS_ERR(rq)) {
  454. err = PTR_ERR(rq);
  455. goto err_fini;
  456. }
  457. err = intel_engine_emit_ctx_wa(rq);
  458. if (err)
  459. goto err_rq;
  460. err = intel_renderstate_emit(&so, rq);
  461. if (err)
  462. goto err_rq;
  463. err_rq:
  464. requests[id] = i915_request_get(rq);
  465. i915_request_add(rq);
  466. err_fini:
  467. intel_renderstate_fini(&so, ce);
  468. err:
  469. if (err) {
  470. intel_context_put(ce);
  471. goto out;
  472. }
  473. }
  474. /* Flush the default context image to memory, and enable powersaving. */
  475. if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
  476. err = -EIO;
  477. goto out;
  478. }
  479. for (id = 0; id < ARRAY_SIZE(requests); id++) {
  480. struct i915_request *rq;
  481. struct file *state;
  482. rq = requests[id];
  483. if (!rq)
  484. continue;
  485. if (rq->fence.error) {
  486. err = -EIO;
  487. goto out;
  488. }
  489. GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags));
  490. if (!rq->context->state)
  491. continue;
  492. /* Keep a copy of the state's backing pages; free the obj */
  493. state = shmem_create_from_object(rq->context->state->obj);
  494. if (IS_ERR(state)) {
  495. err = PTR_ERR(state);
  496. goto out;
  497. }
  498. rq->engine->default_state = state;
  499. }
  500. out:
  501. /*
  502. * If we have to abandon now, we expect the engines to be idle
  503. * and ready to be torn-down. The quickest way we can accomplish
  504. * this is by declaring ourselves wedged.
  505. */
  506. if (err)
  507. intel_gt_set_wedged(gt);
  508. for (id = 0; id < ARRAY_SIZE(requests); id++) {
  509. struct intel_context *ce;
  510. struct i915_request *rq;
  511. rq = requests[id];
  512. if (!rq)
  513. continue;
  514. ce = rq->context;
  515. i915_request_put(rq);
  516. intel_context_put(ce);
  517. }
  518. return err;
  519. }
  520. static int __engines_verify_workarounds(struct intel_gt *gt)
  521. {
  522. struct intel_engine_cs *engine;
  523. enum intel_engine_id id;
  524. int err = 0;
  525. if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
  526. return 0;
  527. for_each_engine(engine, gt, id) {
  528. if (intel_engine_verify_workarounds(engine, "load"))
  529. err = -EIO;
  530. }
  531. /* Flush and restore the kernel context for safety */
  532. if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME)
  533. err = -EIO;
  534. return err;
  535. }
  536. static void __intel_gt_disable(struct intel_gt *gt)
  537. {
  538. intel_gt_set_wedged_on_fini(gt);
  539. intel_gt_suspend_prepare(gt);
  540. intel_gt_suspend_late(gt);
  541. GEM_BUG_ON(intel_gt_pm_is_awake(gt));
  542. }
  543. int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
  544. {
  545. long remaining_timeout;
  546. /* If the device is asleep, we have no requests outstanding */
  547. if (!intel_gt_pm_is_awake(gt))
  548. return 0;
  549. while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
  550. &remaining_timeout)) > 0) {
  551. cond_resched();
  552. if (signal_pending(current))
  553. return -EINTR;
  554. }
  555. if (timeout)
  556. return timeout;
  557. if (remaining_timeout < 0)
  558. remaining_timeout = 0;
  559. return intel_uc_wait_for_idle(&gt->uc, remaining_timeout);
  560. }
  561. int intel_gt_init(struct intel_gt *gt)
  562. {
  563. int err;
  564. intel_gt_init_workarounds(gt);
  565. /*
  566. * This is just a security blanket to placate dragons.
  567. * On some systems, we very sporadically observe that the first TLBs
  568. * used by the CS may be stale, despite us poking the TLB reset. If
  569. * we hold the forcewake during initialisation these problems
  570. * just magically go away.
  571. */
  572. intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
  573. err = intel_gt_init_scratch(gt,
  574. GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K);
  575. if (err)
  576. goto out_fw;
  577. intel_gt_pm_init(gt);
  578. gt->vm = kernel_vm(gt);
  579. if (!gt->vm) {
  580. err = -ENOMEM;
  581. goto err_pm;
  582. }
  583. intel_set_mocs_index(gt);
  584. err = intel_engines_init(gt);
  585. if (err)
  586. goto err_engines;
  587. err = intel_uc_init(&gt->uc);
  588. if (err)
  589. goto err_engines;
  590. err = intel_gt_resume(gt);
  591. if (err)
  592. goto err_uc_init;
  593. err = intel_gt_init_hwconfig(gt);
  594. if (err)
  595. gt_err(gt, "Failed to retrieve hwconfig table: %pe\n", ERR_PTR(err));
  596. err = __engines_record_defaults(gt);
  597. if (err)
  598. goto err_gt;
  599. err = __engines_verify_workarounds(gt);
  600. if (err)
  601. goto err_gt;
  602. intel_uc_init_late(&gt->uc);
  603. intel_migrate_init(&gt->migrate, gt);
  604. goto out_fw;
  605. err_gt:
  606. __intel_gt_disable(gt);
  607. intel_uc_fini_hw(&gt->uc);
  608. err_uc_init:
  609. intel_uc_fini(&gt->uc);
  610. err_engines:
  611. intel_engines_release(gt);
  612. i915_vm_put(fetch_and_zero(&gt->vm));
  613. err_pm:
  614. intel_gt_pm_fini(gt);
  615. intel_gt_fini_scratch(gt);
  616. out_fw:
  617. if (err)
  618. intel_gt_set_wedged_on_init(gt);
  619. intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
  620. return err;
  621. }
  622. ALLOW_ERROR_INJECTION(intel_gt_init, ERRNO);
  623. void intel_gt_driver_remove(struct intel_gt *gt)
  624. {
  625. __intel_gt_disable(gt);
  626. intel_migrate_fini(&gt->migrate);
  627. intel_uc_driver_remove(&gt->uc);
  628. intel_engines_release(gt);
  629. intel_gt_flush_buffer_pool(gt);
  630. }
  631. void intel_gt_driver_unregister(struct intel_gt *gt)
  632. {
  633. intel_wakeref_t wakeref;
  634. intel_gt_sysfs_unregister(gt);
  635. intel_rps_driver_unregister(&gt->rps);
  636. intel_gsc_fini(&gt->gsc);
  637. /*
  638. * If we unload the driver and wedge before the GSC worker is complete,
  639. * the worker will hit an error on its submission to the GSC engine and
  640. * then exit. This is hard to hit for a user, but it is reproducible
  641. * with skipping selftests. The error is handled gracefully by the
  642. * worker, so there are no functional issues, but we still end up with
  643. * an error message in dmesg, which is something we want to avoid as
  644. * this is a supported scenario. We could modify the worker to better
  645. * handle a wedging occurring during its execution, but that gets
  646. * complicated for a couple of reasons:
  647. * - We do want the error on runtime wedging, because there are
  648. * implications for subsystems outside of GT (i.e., PXP, HDCP), it's
  649. * only the error on driver unload that we want to silence.
  650. * - The worker is responsible for multiple submissions (GSC FW load,
  651. * HuC auth, SW proxy), so all of those will have to be adapted to
  652. * handle the wedged_on_fini scenario.
  653. * Therefore, it's much simpler to just wait for the worker to be done
  654. * before wedging on driver removal, also considering that the worker
  655. * will likely already be idle in the great majority of non-selftest
  656. * scenarios.
  657. */
  658. intel_gsc_uc_flush_work(&gt->uc.gsc);
  659. /*
  660. * Upon unregistering the device to prevent any new users, cancel
  661. * all in-flight requests so that we can quickly unbind the active
  662. * resources.
  663. */
  664. intel_gt_set_wedged_on_fini(gt);
  665. /* Scrub all HW state upon release */
  666. with_intel_runtime_pm(gt->uncore->rpm, wakeref)
  667. intel_gt_reset_all_engines(gt);
  668. }
  669. void intel_gt_driver_release(struct intel_gt *gt)
  670. {
  671. struct i915_address_space *vm;
  672. vm = fetch_and_zero(&gt->vm);
  673. if (vm) /* FIXME being called twice on error paths :( */
  674. i915_vm_put(vm);
  675. intel_wa_list_free(&gt->wa_list);
  676. intel_gt_pm_fini(gt);
  677. intel_gt_fini_scratch(gt);
  678. intel_gt_fini_buffer_pool(gt);
  679. intel_gt_fini_hwconfig(gt);
  680. }
  681. void intel_gt_driver_late_release_all(struct drm_i915_private *i915)
  682. {
  683. struct intel_gt *gt;
  684. unsigned int id;
  685. /* We need to wait for inflight RCU frees to release their grip */
  686. rcu_barrier();
  687. for_each_gt(gt, i915, id) {
  688. intel_uc_driver_late_release(&gt->uc);
  689. intel_gt_fini_requests(gt);
  690. intel_gt_fini_reset(gt);
  691. intel_gt_fini_timelines(gt);
  692. intel_gt_fini_tlb(gt);
  693. intel_engines_free(gt);
  694. }
  695. }
  696. static int intel_gt_tile_setup(struct intel_gt *gt, phys_addr_t phys_addr)
  697. {
  698. int ret;
  699. if (!gt_is_root(gt)) {
  700. struct intel_uncore *uncore;
  701. spinlock_t *irq_lock;
  702. uncore = drmm_kzalloc(&gt->i915->drm, sizeof(*uncore), GFP_KERNEL);
  703. if (!uncore)
  704. return -ENOMEM;
  705. irq_lock = drmm_kzalloc(&gt->i915->drm, sizeof(*irq_lock), GFP_KERNEL);
  706. if (!irq_lock)
  707. return -ENOMEM;
  708. gt->uncore = uncore;
  709. gt->irq_lock = irq_lock;
  710. intel_gt_common_init_early(gt);
  711. }
  712. intel_uncore_init_early(gt->uncore, gt);
  713. ret = intel_uncore_setup_mmio(gt->uncore, phys_addr);
  714. if (ret)
  715. return ret;
  716. gt->phys_addr = phys_addr;
  717. return 0;
  718. }
  719. int intel_gt_probe_all(struct drm_i915_private *i915)
  720. {
  721. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  722. struct intel_gt *gt = to_gt(i915);
  723. const struct intel_gt_definition *gtdef;
  724. phys_addr_t phys_addr;
  725. unsigned int mmio_bar;
  726. unsigned int i;
  727. int ret;
  728. mmio_bar = intel_mmio_bar(GRAPHICS_VER(i915));
  729. phys_addr = pci_resource_start(pdev, mmio_bar);
  730. /*
  731. * We always have at least one primary GT on any device
  732. * and it has been already initialized early during probe
  733. * in i915_driver_probe()
  734. */
  735. gt->i915 = i915;
  736. gt->name = "Primary GT";
  737. gt->info.engine_mask = INTEL_INFO(i915)->platform_engine_mask;
  738. gt_dbg(gt, "Setting up %s\n", gt->name);
  739. ret = intel_gt_tile_setup(gt, phys_addr);
  740. if (ret)
  741. return ret;
  742. if (!HAS_EXTRA_GT_LIST(i915))
  743. return 0;
  744. for (i = 1, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1];
  745. gtdef->name != NULL;
  746. i++, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]) {
  747. gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL);
  748. if (!gt) {
  749. ret = -ENOMEM;
  750. goto err;
  751. }
  752. gt->i915 = i915;
  753. gt->name = gtdef->name;
  754. gt->type = gtdef->type;
  755. gt->info.engine_mask = gtdef->engine_mask;
  756. gt->info.id = i;
  757. gt_dbg(gt, "Setting up %s\n", gt->name);
  758. if (GEM_WARN_ON(range_overflows_t(resource_size_t,
  759. gtdef->mapping_base,
  760. SZ_16M,
  761. pci_resource_len(pdev, mmio_bar)))) {
  762. ret = -ENODEV;
  763. goto err;
  764. }
  765. switch (gtdef->type) {
  766. case GT_TILE:
  767. ret = intel_gt_tile_setup(gt, phys_addr + gtdef->mapping_base);
  768. break;
  769. case GT_MEDIA:
  770. ret = intel_sa_mediagt_setup(gt, phys_addr + gtdef->mapping_base,
  771. gtdef->gsi_offset);
  772. break;
  773. case GT_PRIMARY:
  774. /* Primary GT should not appear in extra GT list */
  775. default:
  776. MISSING_CASE(gtdef->type);
  777. ret = -ENODEV;
  778. }
  779. if (ret)
  780. goto err;
  781. i915->gt[i] = gt;
  782. }
  783. return 0;
  784. err:
  785. i915_probe_error(i915, "Failed to initialize %s! (%d)\n", gtdef->name, ret);
  786. return ret;
  787. }
  788. int intel_gt_tiles_init(struct drm_i915_private *i915)
  789. {
  790. struct intel_gt *gt;
  791. unsigned int id;
  792. int ret;
  793. for_each_gt(gt, i915, id) {
  794. ret = intel_gt_probe_lmem(gt);
  795. if (ret)
  796. return ret;
  797. }
  798. return 0;
  799. }
  800. void intel_gt_info_print(const struct intel_gt_info *info,
  801. struct drm_printer *p)
  802. {
  803. drm_printf(p, "available engines: %x\n", info->engine_mask);
  804. intel_sseu_dump(&info->sseu, p);
  805. }
  806. enum i915_map_type intel_gt_coherent_map_type(struct intel_gt *gt,
  807. struct drm_i915_gem_object *obj,
  808. bool always_coherent)
  809. {
  810. /*
  811. * Wa_22016122933: always return I915_MAP_WC for Media
  812. * version 13.0 when the object is on the Media GT
  813. */
  814. if (i915_gem_object_is_lmem(obj) || intel_gt_needs_wa_22016122933(gt))
  815. return I915_MAP_WC;
  816. if (HAS_LLC(gt->i915) || always_coherent)
  817. return I915_MAP_WB;
  818. else
  819. return I915_MAP_WC;
  820. }
  821. bool intel_gt_needs_wa_16018031267(struct intel_gt *gt)
  822. {
  823. /* Wa_16018031267, Wa_16018063123 */
  824. return IS_GFX_GT_IP_RANGE(gt, IP_VER(12, 55), IP_VER(12, 71));
  825. }
  826. bool intel_gt_needs_wa_22016122933(struct intel_gt *gt)
  827. {
  828. return MEDIA_VER_FULL(gt->i915) == IP_VER(13, 0) && gt->type == GT_MEDIA;
  829. }
  830. static void __intel_gt_bind_context_set_ready(struct intel_gt *gt, bool ready)
  831. {
  832. struct intel_engine_cs *engine = gt->engine[BCS0];
  833. if (engine && engine->bind_context)
  834. engine->bind_context_ready = ready;
  835. }
  836. /**
  837. * intel_gt_bind_context_set_ready - Set the context binding as ready
  838. *
  839. * @gt: GT structure
  840. *
  841. * This function marks the binder context as ready.
  842. */
  843. void intel_gt_bind_context_set_ready(struct intel_gt *gt)
  844. {
  845. __intel_gt_bind_context_set_ready(gt, true);
  846. }
  847. /**
  848. * intel_gt_bind_context_set_unready - Set the context binding as ready
  849. * @gt: GT structure
  850. *
  851. * This function marks the binder context as not ready.
  852. */
  853. void intel_gt_bind_context_set_unready(struct intel_gt *gt)
  854. {
  855. __intel_gt_bind_context_set_ready(gt, false);
  856. }
  857. /**
  858. * intel_gt_is_bind_context_ready - Check if context binding is ready
  859. *
  860. * @gt: GT structure
  861. *
  862. * This function returns binder context's ready status.
  863. */
  864. bool intel_gt_is_bind_context_ready(struct intel_gt *gt)
  865. {
  866. struct intel_engine_cs *engine = gt->engine[BCS0];
  867. if (engine)
  868. return engine->bind_context_ready;
  869. return false;
  870. }