1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2014-2018 Intel Corporation
5  */
6 
7 #include "i915_drv.h"
8 #include "intel_context.h"
9 #include "intel_engine_pm.h"
10 #include "intel_gpu_commands.h"
11 #include "intel_gt.h"
12 #include "intel_ring.h"
13 #include "intel_workarounds.h"
14 
15 /**
16  * DOC: Hardware workarounds
17  *
18  * This file is intended as a central place to implement most [1]_ of the
19  * required workarounds for hardware to work as originally intended. They fall
20  * in five basic categories depending on how/when they are applied:
21  *
22  * - Workarounds that touch registers that are saved/restored to/from the HW
23  *   context image. The list is emitted (via Load Register Immediate commands)
24  *   everytime a new context is created.
25  * - GT workarounds. The list of these WAs is applied whenever these registers
26  *   revert to default values (on GPU reset, suspend/resume [2]_, etc..).
27  * - Display workarounds. The list is applied during display clock-gating
28  *   initialization.
29  * - Workarounds that whitelist a privileged register, so that UMDs can manage
30  *   them directly. This is just a special case of a MMMIO workaround (as we
31  *   write the list of these to/be-whitelisted registers to some special HW
32  *   registers).
33  * - Workaround batchbuffers, that get executed automatically by the hardware
34  *   on every HW context restore.
35  *
36  * .. [1] Please notice that there are other WAs that, due to their nature,
37  *    cannot be applied from a central place. Those are peppered around the rest
38  *    of the code, as needed.
39  *
40  * .. [2] Technically, some registers are powercontext saved & restored, so they
41  *    survive a suspend/resume. In practice, writing them again is not too
42  *    costly and simplifies things. We can revisit this in the future.
43  *
44  * Layout
45  * ~~~~~~
46  *
47  * Keep things in this file ordered by WA type, as per the above (context, GT,
48  * display, register whitelist, batchbuffer). Then, inside each type, keep the
49  * following order:
50  *
51  * - Infrastructure functions and macros
52  * - WAs per platform in standard gen/chrono order
53  * - Public functions to init or apply the given workaround type.
54  */
55 
56 /*
57  * KBL revision ID ordering is bizarre; higher revision ID's map to lower
58  * steppings in some cases.  So rather than test against the revision ID
59  * directly, let's map that into our own range of increasing ID's that we
60  * can test against in a regular manner.
61  */
62 
63 const struct i915_rev_steppings kbl_revids[] = {
64 	[0] = { .gt_stepping = KBL_REVID_A0, .disp_stepping = KBL_REVID_A0 },
65 	[1] = { .gt_stepping = KBL_REVID_B0, .disp_stepping = KBL_REVID_B0 },
66 	[2] = { .gt_stepping = KBL_REVID_C0, .disp_stepping = KBL_REVID_B0 },
67 	[3] = { .gt_stepping = KBL_REVID_D0, .disp_stepping = KBL_REVID_B0 },
68 	[4] = { .gt_stepping = KBL_REVID_F0, .disp_stepping = KBL_REVID_C0 },
69 	[5] = { .gt_stepping = KBL_REVID_C0, .disp_stepping = KBL_REVID_B1 },
70 	[6] = { .gt_stepping = KBL_REVID_D1, .disp_stepping = KBL_REVID_B1 },
71 	[7] = { .gt_stepping = KBL_REVID_G0, .disp_stepping = KBL_REVID_C0 },
72 };
73 
74 const struct i915_rev_steppings tgl_uy_revids[] = {
75 	[0] = { .gt_stepping = TGL_REVID_A0, .disp_stepping = TGL_REVID_A0 },
76 	[1] = { .gt_stepping = TGL_REVID_B0, .disp_stepping = TGL_REVID_C0 },
77 	[2] = { .gt_stepping = TGL_REVID_B1, .disp_stepping = TGL_REVID_C0 },
78 	[3] = { .gt_stepping = TGL_REVID_C0, .disp_stepping = TGL_REVID_D0 },
79 };
80 
81 /* Same GT stepping between tgl_uy_revids and tgl_revids don't mean the same HW */
82 const struct i915_rev_steppings tgl_revids[] = {
83 	[0] = { .gt_stepping = TGL_REVID_A0, .disp_stepping = TGL_REVID_B0 },
84 	[1] = { .gt_stepping = TGL_REVID_B0, .disp_stepping = TGL_REVID_D0 },
85 };
86 
87 static void wa_init_start(struct i915_wa_list *wal, const char *name, const char *engine_name)
88 {
89 	wal->name = name;
90 	wal->engine_name = engine_name;
91 }
92 
93 #define WA_LIST_CHUNK (1 << 4)
94 
95 static void wa_init_finish(struct i915_wa_list *wal)
96 {
97 	/* Trim unused entries. */
98 	if (!IS_ALIGNED(wal->count, WA_LIST_CHUNK)) {
99 		struct i915_wa *list = kmemdup(wal->list,
100 					       wal->count * sizeof(*list),
101 					       GFP_KERNEL);
102 
103 		if (list) {
104 			kfree(wal->list);
105 			wal->list = list;
106 		}
107 	}
108 
109 	if (!wal->count)
110 		return;
111 
112 	DRM_DEBUG_DRIVER("Initialized %u %s workarounds on %s\n",
113 			 wal->wa_count, wal->name, wal->engine_name);
114 }
115 
116 static void _wa_add(struct i915_wa_list *wal, const struct i915_wa *wa)
117 {
118 	unsigned int addr = i915_mmio_reg_offset(wa->reg);
119 	unsigned int start = 0, end = wal->count;
120 	const unsigned int grow = WA_LIST_CHUNK;
121 	struct i915_wa *wa_;
122 
123 	GEM_BUG_ON(!is_power_of_2(grow));
124 
125 	if (IS_ALIGNED(wal->count, grow)) { /* Either uninitialized or full. */
126 		struct i915_wa *list;
127 
128 		list = kmalloc_array(ALIGN(wal->count + 1, grow), sizeof(*wa),
129 				     GFP_KERNEL);
130 		if (!list) {
131 			DRM_ERROR("No space for workaround init!\n");
132 			return;
133 		}
134 
135 		if (wal->list) {
136 			memcpy(list, wal->list, sizeof(*wa) * wal->count);
137 			kfree(wal->list);
138 		}
139 
140 		wal->list = list;
141 	}
142 
143 	while (start < end) {
144 		unsigned int mid = start + (end - start) / 2;
145 
146 		if (i915_mmio_reg_offset(wal->list[mid].reg) < addr) {
147 			start = mid + 1;
148 		} else if (i915_mmio_reg_offset(wal->list[mid].reg) > addr) {
149 			end = mid;
150 		} else {
151 			wa_ = &wal->list[mid];
152 
153 			if ((wa->clr | wa_->clr) && !(wa->clr & ~wa_->clr)) {
154 				DRM_ERROR("Discarding overwritten w/a for reg %04x (clear: %08x, set: %08x)\n",
155 					  i915_mmio_reg_offset(wa_->reg),
156 					  wa_->clr, wa_->set);
157 
158 				wa_->set &= ~wa->clr;
159 			}
160 
161 			wal->wa_count++;
162 			wa_->set |= wa->set;
163 			wa_->clr |= wa->clr;
164 			wa_->read |= wa->read;
165 			return;
166 		}
167 	}
168 
169 	wal->wa_count++;
170 	wa_ = &wal->list[wal->count++];
171 	*wa_ = *wa;
172 
173 	while (wa_-- > wal->list) {
174 		GEM_BUG_ON(i915_mmio_reg_offset(wa_[0].reg) ==
175 			   i915_mmio_reg_offset(wa_[1].reg));
176 		if (i915_mmio_reg_offset(wa_[1].reg) >
177 		    i915_mmio_reg_offset(wa_[0].reg))
178 			break;
179 
180 		swap(wa_[1], wa_[0]);
181 	}
182 }
183 
184 static void wa_add(struct i915_wa_list *wal, i915_reg_t reg,
185 		   u32 clear, u32 set, u32 read_mask)
186 {
187 	struct i915_wa wa = {
188 		.reg  = reg,
189 		.clr  = clear,
190 		.set  = set,
191 		.read = read_mask,
192 	};
193 
194 	_wa_add(wal, &wa);
195 }
196 
197 static void
198 wa_write_clr_set(struct i915_wa_list *wal, i915_reg_t reg, u32 clear, u32 set)
199 {
200 	wa_add(wal, reg, clear, set, clear);
201 }
202 
203 static void
204 wa_write(struct i915_wa_list *wal, i915_reg_t reg, u32 set)
205 {
206 	wa_write_clr_set(wal, reg, ~0, set);
207 }
208 
209 static void
210 wa_write_or(struct i915_wa_list *wal, i915_reg_t reg, u32 set)
211 {
212 	wa_write_clr_set(wal, reg, set, set);
213 }
214 
215 static void
216 wa_write_clr(struct i915_wa_list *wal, i915_reg_t reg, u32 clr)
217 {
218 	wa_write_clr_set(wal, reg, clr, 0);
219 }
220 
221 /*
222  * WA operations on "masked register". A masked register has the upper 16 bits
223  * documented as "masked" in b-spec. Its purpose is to allow writing to just a
224  * portion of the register without a rmw: you simply write in the upper 16 bits
225  * the mask of bits you are going to modify.
226  *
227  * The wa_masked_* family of functions already does the necessary operations to
228  * calculate the mask based on the parameters passed, so user only has to
229  * provide the lower 16 bits of that register.
230  */
231 
232 static void
233 wa_masked_en(struct i915_wa_list *wal, i915_reg_t reg, u32 val)
234 {
235 	wa_add(wal, reg, 0, _MASKED_BIT_ENABLE(val), val);
236 }
237 
238 static void
239 wa_masked_dis(struct i915_wa_list *wal, i915_reg_t reg, u32 val)
240 {
241 	wa_add(wal, reg, 0, _MASKED_BIT_DISABLE(val), val);
242 }
243 
244 static void
245 wa_masked_field_set(struct i915_wa_list *wal, i915_reg_t reg,
246 		    u32 mask, u32 val)
247 {
248 	wa_add(wal, reg, 0, _MASKED_FIELD(mask, val), mask);
249 }
250 
251 static void gen6_ctx_workarounds_init(struct intel_engine_cs *engine,
252 				      struct i915_wa_list *wal)
253 {
254 	wa_masked_en(wal, INSTPM, INSTPM_FORCE_ORDERING);
255 }
256 
257 static void gen7_ctx_workarounds_init(struct intel_engine_cs *engine,
258 				      struct i915_wa_list *wal)
259 {
260 	wa_masked_en(wal, INSTPM, INSTPM_FORCE_ORDERING);
261 }
262 
263 static void gen8_ctx_workarounds_init(struct intel_engine_cs *engine,
264 				      struct i915_wa_list *wal)
265 {
266 	wa_masked_en(wal, INSTPM, INSTPM_FORCE_ORDERING);
267 
268 	/* WaDisableAsyncFlipPerfMode:bdw,chv */
269 	wa_masked_en(wal, MI_MODE, ASYNC_FLIP_PERF_DISABLE);
270 
271 	/* WaDisablePartialInstShootdown:bdw,chv */
272 	wa_masked_en(wal, GEN8_ROW_CHICKEN,
273 		     PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
274 
275 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
276 	 * workaround for for a possible hang in the unlikely event a TLB
277 	 * invalidation occurs during a PSD flush.
278 	 */
279 	/* WaForceEnableNonCoherent:bdw,chv */
280 	/* WaHdcDisableFetchWhenMasked:bdw,chv */
281 	wa_masked_en(wal, HDC_CHICKEN0,
282 		     HDC_DONOT_FETCH_MEM_WHEN_MASKED |
283 		     HDC_FORCE_NON_COHERENT);
284 
285 	/* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
286 	 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
287 	 *  polygons in the same 8x4 pixel/sample area to be processed without
288 	 *  stalling waiting for the earlier ones to write to Hierarchical Z
289 	 *  buffer."
290 	 *
291 	 * This optimization is off by default for BDW and CHV; turn it on.
292 	 */
293 	wa_masked_dis(wal, CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
294 
295 	/* Wa4x4STCOptimizationDisable:bdw,chv */
296 	wa_masked_en(wal, CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
297 
298 	/*
299 	 * BSpec recommends 8x4 when MSAA is used,
300 	 * however in practice 16x4 seems fastest.
301 	 *
302 	 * Note that PS/WM thread counts depend on the WIZ hashing
303 	 * disable bit, which we don't touch here, but it's good
304 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
305 	 */
306 	wa_masked_field_set(wal, GEN7_GT_MODE,
307 			    GEN6_WIZ_HASHING_MASK,
308 			    GEN6_WIZ_HASHING_16x4);
309 }
310 
311 static void bdw_ctx_workarounds_init(struct intel_engine_cs *engine,
312 				     struct i915_wa_list *wal)
313 {
314 	struct drm_i915_private *i915 = engine->i915;
315 
316 	gen8_ctx_workarounds_init(engine, wal);
317 
318 	/* WaDisableThreadStallDopClockGating:bdw (pre-production) */
319 	wa_masked_en(wal, GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
320 
321 	/* WaDisableDopClockGating:bdw
322 	 *
323 	 * Also see the related UCGTCL1 write in bdw_init_clock_gating()
324 	 * to disable EUTC clock gating.
325 	 */
326 	wa_masked_en(wal, GEN7_ROW_CHICKEN2,
327 		     DOP_CLOCK_GATING_DISABLE);
328 
329 	wa_masked_en(wal, HALF_SLICE_CHICKEN3,
330 		     GEN8_SAMPLER_POWER_BYPASS_DIS);
331 
332 	wa_masked_en(wal, HDC_CHICKEN0,
333 		     /* WaForceContextSaveRestoreNonCoherent:bdw */
334 		     HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
335 		     /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
336 		     (IS_BDW_GT3(i915) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
337 }
338 
339 static void chv_ctx_workarounds_init(struct intel_engine_cs *engine,
340 				     struct i915_wa_list *wal)
341 {
342 	gen8_ctx_workarounds_init(engine, wal);
343 
344 	/* WaDisableThreadStallDopClockGating:chv */
345 	wa_masked_en(wal, GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
346 
347 	/* Improve HiZ throughput on CHV. */
348 	wa_masked_en(wal, HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
349 }
350 
351 static void gen9_ctx_workarounds_init(struct intel_engine_cs *engine,
352 				      struct i915_wa_list *wal)
353 {
354 	struct drm_i915_private *i915 = engine->i915;
355 
356 	if (HAS_LLC(i915)) {
357 		/* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
358 		 *
359 		 * Must match Display Engine. See
360 		 * WaCompressedResourceDisplayNewHashMode.
361 		 */
362 		wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
363 			     GEN9_PBE_COMPRESSED_HASH_SELECTION);
364 		wa_masked_en(wal, GEN9_HALF_SLICE_CHICKEN7,
365 			     GEN9_SAMPLER_HASH_COMPRESSED_READ_ADDR);
366 	}
367 
368 	/* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl,glk,cfl */
369 	/* WaDisablePartialInstShootdown:skl,bxt,kbl,glk,cfl */
370 	wa_masked_en(wal, GEN8_ROW_CHICKEN,
371 		     FLOW_CONTROL_ENABLE |
372 		     PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
373 
374 	/* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl,glk,cfl */
375 	/* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl,cfl */
376 	wa_masked_en(wal, GEN9_HALF_SLICE_CHICKEN7,
377 		     GEN9_ENABLE_YV12_BUGFIX |
378 		     GEN9_ENABLE_GPGPU_PREEMPTION);
379 
380 	/* Wa4x4STCOptimizationDisable:skl,bxt,kbl,glk,cfl */
381 	/* WaDisablePartialResolveInVc:skl,bxt,kbl,cfl */
382 	wa_masked_en(wal, CACHE_MODE_1,
383 		     GEN8_4x4_STC_OPTIMIZATION_DISABLE |
384 		     GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
385 
386 	/* WaCcsTlbPrefetchDisable:skl,bxt,kbl,glk,cfl */
387 	wa_masked_dis(wal, GEN9_HALF_SLICE_CHICKEN5,
388 		      GEN9_CCS_TLB_PREFETCH_ENABLE);
389 
390 	/* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl,cfl */
391 	wa_masked_en(wal, HDC_CHICKEN0,
392 		     HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
393 		     HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
394 
395 	/* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
396 	 * both tied to WaForceContextSaveRestoreNonCoherent
397 	 * in some hsds for skl. We keep the tie for all gen9. The
398 	 * documentation is a bit hazy and so we want to get common behaviour,
399 	 * even though there is no clear evidence we would need both on kbl/bxt.
400 	 * This area has been source of system hangs so we play it safe
401 	 * and mimic the skl regardless of what bspec says.
402 	 *
403 	 * Use Force Non-Coherent whenever executing a 3D context. This
404 	 * is a workaround for a possible hang in the unlikely event
405 	 * a TLB invalidation occurs during a PSD flush.
406 	 */
407 
408 	/* WaForceEnableNonCoherent:skl,bxt,kbl,cfl */
409 	wa_masked_en(wal, HDC_CHICKEN0,
410 		     HDC_FORCE_NON_COHERENT);
411 
412 	/* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl,cfl */
413 	if (IS_SKYLAKE(i915) ||
414 	    IS_KABYLAKE(i915) ||
415 	    IS_COFFEELAKE(i915) ||
416 	    IS_COMETLAKE(i915))
417 		wa_masked_en(wal, HALF_SLICE_CHICKEN3,
418 			     GEN8_SAMPLER_POWER_BYPASS_DIS);
419 
420 	/* WaDisableSTUnitPowerOptimization:skl,bxt,kbl,glk,cfl */
421 	wa_masked_en(wal, HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
422 
423 	/*
424 	 * Supporting preemption with fine-granularity requires changes in the
425 	 * batch buffer programming. Since we can't break old userspace, we
426 	 * need to set our default preemption level to safe value. Userspace is
427 	 * still able to use more fine-grained preemption levels, since in
428 	 * WaEnablePreemptionGranularityControlByUMD we're whitelisting the
429 	 * per-ctx register. As such, WaDisable{3D,GPGPU}MidCmdPreemption are
430 	 * not real HW workarounds, but merely a way to start using preemption
431 	 * while maintaining old contract with userspace.
432 	 */
433 
434 	/* WaDisable3DMidCmdPreemption:skl,bxt,glk,cfl,[cnl] */
435 	wa_masked_dis(wal, GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
436 
437 	/* WaDisableGPGPUMidCmdPreemption:skl,bxt,blk,cfl,[cnl] */
438 	wa_masked_field_set(wal, GEN8_CS_CHICKEN1,
439 			    GEN9_PREEMPT_GPGPU_LEVEL_MASK,
440 			    GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
441 
442 	/* WaClearHIZ_WM_CHICKEN3:bxt,glk */
443 	if (IS_GEN9_LP(i915))
444 		wa_masked_en(wal, GEN9_WM_CHICKEN3, GEN9_FACTOR_IN_CLR_VAL_HIZ);
445 }
446 
447 static void skl_tune_iz_hashing(struct intel_engine_cs *engine,
448 				struct i915_wa_list *wal)
449 {
450 	struct intel_gt *gt = engine->gt;
451 	u8 vals[3] = { 0, 0, 0 };
452 	unsigned int i;
453 
454 	for (i = 0; i < 3; i++) {
455 		u8 ss;
456 
457 		/*
458 		 * Only consider slices where one, and only one, subslice has 7
459 		 * EUs
460 		 */
461 		if (!is_power_of_2(gt->info.sseu.subslice_7eu[i]))
462 			continue;
463 
464 		/*
465 		 * subslice_7eu[i] != 0 (because of the check above) and
466 		 * ss_max == 4 (maximum number of subslices possible per slice)
467 		 *
468 		 * ->    0 <= ss <= 3;
469 		 */
470 		ss = ffs(gt->info.sseu.subslice_7eu[i]) - 1;
471 		vals[i] = 3 - ss;
472 	}
473 
474 	if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
475 		return;
476 
477 	/* Tune IZ hashing. See intel_device_info_runtime_init() */
478 	wa_masked_field_set(wal, GEN7_GT_MODE,
479 			    GEN9_IZ_HASHING_MASK(2) |
480 			    GEN9_IZ_HASHING_MASK(1) |
481 			    GEN9_IZ_HASHING_MASK(0),
482 			    GEN9_IZ_HASHING(2, vals[2]) |
483 			    GEN9_IZ_HASHING(1, vals[1]) |
484 			    GEN9_IZ_HASHING(0, vals[0]));
485 }
486 
487 static void skl_ctx_workarounds_init(struct intel_engine_cs *engine,
488 				     struct i915_wa_list *wal)
489 {
490 	gen9_ctx_workarounds_init(engine, wal);
491 	skl_tune_iz_hashing(engine, wal);
492 }
493 
494 static void bxt_ctx_workarounds_init(struct intel_engine_cs *engine,
495 				     struct i915_wa_list *wal)
496 {
497 	gen9_ctx_workarounds_init(engine, wal);
498 
499 	/* WaDisableThreadStallDopClockGating:bxt */
500 	wa_masked_en(wal, GEN8_ROW_CHICKEN,
501 		     STALL_DOP_GATING_DISABLE);
502 
503 	/* WaToEnableHwFixForPushConstHWBug:bxt */
504 	wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
505 		     GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
506 }
507 
508 static void kbl_ctx_workarounds_init(struct intel_engine_cs *engine,
509 				     struct i915_wa_list *wal)
510 {
511 	struct drm_i915_private *i915 = engine->i915;
512 
513 	gen9_ctx_workarounds_init(engine, wal);
514 
515 	/* WaToEnableHwFixForPushConstHWBug:kbl */
516 	if (IS_KBL_GT_REVID(i915, KBL_REVID_C0, REVID_FOREVER))
517 		wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
518 			     GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
519 
520 	/* WaDisableSbeCacheDispatchPortSharing:kbl */
521 	wa_masked_en(wal, GEN7_HALF_SLICE_CHICKEN1,
522 		     GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
523 }
524 
525 static void glk_ctx_workarounds_init(struct intel_engine_cs *engine,
526 				     struct i915_wa_list *wal)
527 {
528 	gen9_ctx_workarounds_init(engine, wal);
529 
530 	/* WaToEnableHwFixForPushConstHWBug:glk */
531 	wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
532 		     GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
533 }
534 
535 static void cfl_ctx_workarounds_init(struct intel_engine_cs *engine,
536 				     struct i915_wa_list *wal)
537 {
538 	gen9_ctx_workarounds_init(engine, wal);
539 
540 	/* WaToEnableHwFixForPushConstHWBug:cfl */
541 	wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
542 		     GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
543 
544 	/* WaDisableSbeCacheDispatchPortSharing:cfl */
545 	wa_masked_en(wal, GEN7_HALF_SLICE_CHICKEN1,
546 		     GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
547 }
548 
549 static void cnl_ctx_workarounds_init(struct intel_engine_cs *engine,
550 				     struct i915_wa_list *wal)
551 {
552 	/* WaForceContextSaveRestoreNonCoherent:cnl */
553 	wa_masked_en(wal, CNL_HDC_CHICKEN0,
554 		     HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT);
555 
556 	/* WaDisableReplayBufferBankArbitrationOptimization:cnl */
557 	wa_masked_en(wal, COMMON_SLICE_CHICKEN2,
558 		     GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
559 
560 	/* WaPushConstantDereferenceHoldDisable:cnl */
561 	wa_masked_en(wal, GEN7_ROW_CHICKEN2, PUSH_CONSTANT_DEREF_DISABLE);
562 
563 	/* FtrEnableFastAnisoL1BankingFix:cnl */
564 	wa_masked_en(wal, HALF_SLICE_CHICKEN3, CNL_FAST_ANISO_L1_BANKING_FIX);
565 
566 	/* WaDisable3DMidCmdPreemption:cnl */
567 	wa_masked_dis(wal, GEN8_CS_CHICKEN1, GEN9_PREEMPT_3D_OBJECT_LEVEL);
568 
569 	/* WaDisableGPGPUMidCmdPreemption:cnl */
570 	wa_masked_field_set(wal, GEN8_CS_CHICKEN1,
571 			    GEN9_PREEMPT_GPGPU_LEVEL_MASK,
572 			    GEN9_PREEMPT_GPGPU_COMMAND_LEVEL);
573 
574 	/* WaDisableEarlyEOT:cnl */
575 	wa_masked_en(wal, GEN8_ROW_CHICKEN, DISABLE_EARLY_EOT);
576 }
577 
578 static void icl_ctx_workarounds_init(struct intel_engine_cs *engine,
579 				     struct i915_wa_list *wal)
580 {
581 	struct drm_i915_private *i915 = engine->i915;
582 
583 	/* WaDisableBankHangMode:icl */
584 	wa_write(wal,
585 		 GEN8_L3CNTLREG,
586 		 intel_uncore_read(engine->uncore, GEN8_L3CNTLREG) |
587 		 GEN8_ERRDETBCTRL);
588 
589 	/* Wa_1604370585:icl (pre-prod)
590 	 * Formerly known as WaPushConstantDereferenceHoldDisable
591 	 */
592 	if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_B0))
593 		wa_masked_en(wal, GEN7_ROW_CHICKEN2,
594 			     PUSH_CONSTANT_DEREF_DISABLE);
595 
596 	/* WaForceEnableNonCoherent:icl
597 	 * This is not the same workaround as in early Gen9 platforms, where
598 	 * lacking this could cause system hangs, but coherency performance
599 	 * overhead is high and only a few compute workloads really need it
600 	 * (the register is whitelisted in hardware now, so UMDs can opt in
601 	 * for coherency if they have a good reason).
602 	 */
603 	wa_masked_en(wal, ICL_HDC_MODE, HDC_FORCE_NON_COHERENT);
604 
605 	/* Wa_2006611047:icl (pre-prod)
606 	 * Formerly known as WaDisableImprovedTdlClkGating
607 	 */
608 	if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_A0))
609 		wa_masked_en(wal, GEN7_ROW_CHICKEN2,
610 			     GEN11_TDL_CLOCK_GATING_FIX_DISABLE);
611 
612 	/* Wa_2006665173:icl (pre-prod) */
613 	if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_A0))
614 		wa_masked_en(wal, GEN11_COMMON_SLICE_CHICKEN3,
615 			     GEN11_BLEND_EMB_FIX_DISABLE_IN_RCC);
616 
617 	/* WaEnableFloatBlendOptimization:icl */
618 	wa_write_clr_set(wal,
619 			 GEN10_CACHE_MODE_SS,
620 			 0, /* write-only, so skip validation */
621 			 _MASKED_BIT_ENABLE(FLOAT_BLEND_OPTIMIZATION_ENABLE));
622 
623 	/* WaDisableGPGPUMidThreadPreemption:icl */
624 	wa_masked_field_set(wal, GEN8_CS_CHICKEN1,
625 			    GEN9_PREEMPT_GPGPU_LEVEL_MASK,
626 			    GEN9_PREEMPT_GPGPU_THREAD_GROUP_LEVEL);
627 
628 	/* allow headerless messages for preemptible GPGPU context */
629 	wa_masked_en(wal, GEN10_SAMPLER_MODE,
630 		     GEN11_SAMPLER_ENABLE_HEADLESS_MSG);
631 
632 	/* Wa_1604278689:icl,ehl */
633 	wa_write(wal, IVB_FBC_RT_BASE, 0xFFFFFFFF & ~ILK_FBC_RT_VALID);
634 	wa_write_clr_set(wal, IVB_FBC_RT_BASE_UPPER,
635 			 0, /* write-only register; skip validation */
636 			 0xFFFFFFFF);
637 
638 	/* Wa_1406306137:icl,ehl */
639 	wa_masked_en(wal, GEN9_ROW_CHICKEN4, GEN11_DIS_PICK_2ND_EU);
640 }
641 
642 static void gen12_ctx_workarounds_init(struct intel_engine_cs *engine,
643 				       struct i915_wa_list *wal)
644 {
645 	/*
646 	 * Wa_1409142259:tgl
647 	 * Wa_1409347922:tgl
648 	 * Wa_1409252684:tgl
649 	 * Wa_1409217633:tgl
650 	 * Wa_1409207793:tgl
651 	 * Wa_1409178076:tgl
652 	 * Wa_1408979724:tgl
653 	 * Wa_14010443199:rkl
654 	 * Wa_14010698770:rkl
655 	 */
656 	wa_masked_en(wal, GEN11_COMMON_SLICE_CHICKEN3,
657 		     GEN12_DISABLE_CPS_AWARE_COLOR_PIPE);
658 
659 	/* WaDisableGPGPUMidThreadPreemption:gen12 */
660 	wa_masked_field_set(wal, GEN8_CS_CHICKEN1,
661 			    GEN9_PREEMPT_GPGPU_LEVEL_MASK,
662 			    GEN9_PREEMPT_GPGPU_THREAD_GROUP_LEVEL);
663 }
664 
665 static void tgl_ctx_workarounds_init(struct intel_engine_cs *engine,
666 				     struct i915_wa_list *wal)
667 {
668 	gen12_ctx_workarounds_init(engine, wal);
669 
670 	/*
671 	 * Wa_1604555607:tgl,rkl
672 	 *
673 	 * Note that the implementation of this workaround is further modified
674 	 * according to the FF_MODE2 guidance given by Wa_1608008084:gen12.
675 	 * FF_MODE2 register will return the wrong value when read. The default
676 	 * value for this register is zero for all fields and there are no bit
677 	 * masks. So instead of doing a RMW we should just write the GS Timer
678 	 * and TDS timer values for Wa_1604555607 and Wa_16011163337.
679 	 */
680 	wa_add(wal,
681 	       FF_MODE2,
682 	       FF_MODE2_GS_TIMER_MASK | FF_MODE2_TDS_TIMER_MASK,
683 	       FF_MODE2_GS_TIMER_224  | FF_MODE2_TDS_TIMER_128,
684 	       0);
685 }
686 
687 static void dg1_ctx_workarounds_init(struct intel_engine_cs *engine,
688 				     struct i915_wa_list *wal)
689 {
690 	gen12_ctx_workarounds_init(engine, wal);
691 
692 	/* Wa_1409044764 */
693 	wa_masked_dis(wal, GEN11_COMMON_SLICE_CHICKEN3,
694 		      DG1_FLOAT_POINT_BLEND_OPT_STRICT_MODE_EN);
695 
696 	/* Wa_22010493298 */
697 	wa_masked_en(wal, HIZ_CHICKEN,
698 		     DG1_HZ_READ_SUPPRESSION_OPTIMIZATION_DISABLE);
699 
700 	/*
701 	 * Wa_16011163337
702 	 *
703 	 * Like in tgl_ctx_workarounds_init(), read verification is ignored due
704 	 * to Wa_1608008084.
705 	 */
706 	wa_add(wal,
707 	       FF_MODE2,
708 	       FF_MODE2_GS_TIMER_MASK, FF_MODE2_GS_TIMER_224, 0);
709 }
710 
711 static void
712 __intel_engine_init_ctx_wa(struct intel_engine_cs *engine,
713 			   struct i915_wa_list *wal,
714 			   const char *name)
715 {
716 	struct drm_i915_private *i915 = engine->i915;
717 
718 	if (engine->class != RENDER_CLASS)
719 		return;
720 
721 	wa_init_start(wal, name, engine->name);
722 
723 	if (IS_DG1(i915))
724 		dg1_ctx_workarounds_init(engine, wal);
725 	else if (IS_ROCKETLAKE(i915) || IS_TIGERLAKE(i915))
726 		tgl_ctx_workarounds_init(engine, wal);
727 	else if (IS_GEN(i915, 12))
728 		gen12_ctx_workarounds_init(engine, wal);
729 	else if (IS_GEN(i915, 11))
730 		icl_ctx_workarounds_init(engine, wal);
731 	else if (IS_CANNONLAKE(i915))
732 		cnl_ctx_workarounds_init(engine, wal);
733 	else if (IS_COFFEELAKE(i915) || IS_COMETLAKE(i915))
734 		cfl_ctx_workarounds_init(engine, wal);
735 	else if (IS_GEMINILAKE(i915))
736 		glk_ctx_workarounds_init(engine, wal);
737 	else if (IS_KABYLAKE(i915))
738 		kbl_ctx_workarounds_init(engine, wal);
739 	else if (IS_BROXTON(i915))
740 		bxt_ctx_workarounds_init(engine, wal);
741 	else if (IS_SKYLAKE(i915))
742 		skl_ctx_workarounds_init(engine, wal);
743 	else if (IS_CHERRYVIEW(i915))
744 		chv_ctx_workarounds_init(engine, wal);
745 	else if (IS_BROADWELL(i915))
746 		bdw_ctx_workarounds_init(engine, wal);
747 	else if (IS_GEN(i915, 7))
748 		gen7_ctx_workarounds_init(engine, wal);
749 	else if (IS_GEN(i915, 6))
750 		gen6_ctx_workarounds_init(engine, wal);
751 	else if (INTEL_GEN(i915) < 8)
752 		return;
753 	else
754 		MISSING_CASE(INTEL_GEN(i915));
755 
756 	wa_init_finish(wal);
757 }
758 
759 void intel_engine_init_ctx_wa(struct intel_engine_cs *engine)
760 {
761 	__intel_engine_init_ctx_wa(engine, &engine->ctx_wa_list, "context");
762 }
763 
764 int intel_engine_emit_ctx_wa(struct i915_request *rq)
765 {
766 	struct i915_wa_list *wal = &rq->engine->ctx_wa_list;
767 	struct i915_wa *wa;
768 	unsigned int i;
769 	u32 *cs;
770 	int ret;
771 
772 	if (wal->count == 0)
773 		return 0;
774 
775 	ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
776 	if (ret)
777 		return ret;
778 
779 	cs = intel_ring_begin(rq, (wal->count * 2 + 2));
780 	if (IS_ERR(cs))
781 		return PTR_ERR(cs);
782 
783 	*cs++ = MI_LOAD_REGISTER_IMM(wal->count);
784 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++) {
785 		*cs++ = i915_mmio_reg_offset(wa->reg);
786 		*cs++ = wa->set;
787 	}
788 	*cs++ = MI_NOOP;
789 
790 	intel_ring_advance(rq, cs);
791 
792 	ret = rq->engine->emit_flush(rq, EMIT_BARRIER);
793 	if (ret)
794 		return ret;
795 
796 	return 0;
797 }
798 
799 static void
800 gen4_gt_workarounds_init(struct drm_i915_private *i915,
801 			 struct i915_wa_list *wal)
802 {
803 	/* WaDisable_RenderCache_OperationalFlush:gen4,ilk */
804 	wa_masked_dis(wal, CACHE_MODE_0, RC_OP_FLUSH_ENABLE);
805 }
806 
807 static void
808 g4x_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
809 {
810 	gen4_gt_workarounds_init(i915, wal);
811 
812 	/* WaDisableRenderCachePipelinedFlush:g4x,ilk */
813 	wa_masked_en(wal, CACHE_MODE_0, CM0_PIPELINED_RENDER_FLUSH_DISABLE);
814 }
815 
816 static void
817 ilk_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
818 {
819 	g4x_gt_workarounds_init(i915, wal);
820 
821 	wa_masked_en(wal, _3D_CHICKEN2, _3D_CHICKEN2_WM_READ_PIPELINED);
822 }
823 
824 static void
825 snb_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
826 {
827 }
828 
829 static void
830 ivb_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
831 {
832 	/* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
833 	wa_masked_dis(wal,
834 		      GEN7_COMMON_SLICE_CHICKEN1,
835 		      GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
836 
837 	/* WaApplyL3ControlAndL3ChickenMode:ivb */
838 	wa_write(wal, GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
839 	wa_write(wal, GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
840 
841 	/* WaForceL3Serialization:ivb */
842 	wa_write_clr(wal, GEN7_L3SQCREG4, L3SQ_URB_READ_CAM_MATCH_DISABLE);
843 }
844 
845 static void
846 vlv_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
847 {
848 	/* WaForceL3Serialization:vlv */
849 	wa_write_clr(wal, GEN7_L3SQCREG4, L3SQ_URB_READ_CAM_MATCH_DISABLE);
850 
851 	/*
852 	 * WaIncreaseL3CreditsForVLVB0:vlv
853 	 * This is the hardware default actually.
854 	 */
855 	wa_write(wal, GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
856 }
857 
858 static void
859 hsw_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
860 {
861 	/* L3 caching of data atomics doesn't work -- disable it. */
862 	wa_write(wal, HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
863 
864 	wa_add(wal,
865 	       HSW_ROW_CHICKEN3, 0,
866 	       _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
867 		0 /* XXX does this reg exist? */);
868 
869 	/* WaVSRefCountFullforceMissDisable:hsw */
870 	wa_write_clr(wal, GEN7_FF_THREAD_MODE, GEN7_FF_VS_REF_CNT_FFME);
871 }
872 
873 static void
874 gen9_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
875 {
876 	/* WaDisableKillLogic:bxt,skl,kbl */
877 	if (!IS_COFFEELAKE(i915) && !IS_COMETLAKE(i915))
878 		wa_write_or(wal,
879 			    GAM_ECOCHK,
880 			    ECOCHK_DIS_TLB);
881 
882 	if (HAS_LLC(i915)) {
883 		/* WaCompressedResourceSamplerPbeMediaNewHashMode:skl,kbl
884 		 *
885 		 * Must match Display Engine. See
886 		 * WaCompressedResourceDisplayNewHashMode.
887 		 */
888 		wa_write_or(wal,
889 			    MMCD_MISC_CTRL,
890 			    MMCD_PCLA | MMCD_HOTSPOT_EN);
891 	}
892 
893 	/* WaDisableHDCInvalidation:skl,bxt,kbl,cfl */
894 	wa_write_or(wal,
895 		    GAM_ECOCHK,
896 		    BDW_DISABLE_HDC_INVALIDATION);
897 }
898 
899 static void
900 skl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
901 {
902 	gen9_gt_workarounds_init(i915, wal);
903 
904 	/* WaDisableGafsUnitClkGating:skl */
905 	wa_write_or(wal,
906 		    GEN7_UCGCTL4,
907 		    GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
908 
909 	/* WaInPlaceDecompressionHang:skl */
910 	if (IS_SKL_REVID(i915, SKL_REVID_H0, REVID_FOREVER))
911 		wa_write_or(wal,
912 			    GEN9_GAMT_ECO_REG_RW_IA,
913 			    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
914 }
915 
916 static void
917 bxt_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
918 {
919 	gen9_gt_workarounds_init(i915, wal);
920 
921 	/* WaInPlaceDecompressionHang:bxt */
922 	wa_write_or(wal,
923 		    GEN9_GAMT_ECO_REG_RW_IA,
924 		    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
925 }
926 
927 static void
928 kbl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
929 {
930 	gen9_gt_workarounds_init(i915, wal);
931 
932 	/* WaDisableDynamicCreditSharing:kbl */
933 	if (IS_KBL_GT_REVID(i915, 0, KBL_REVID_B0))
934 		wa_write_or(wal,
935 			    GAMT_CHKN_BIT_REG,
936 			    GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
937 
938 	/* WaDisableGafsUnitClkGating:kbl */
939 	wa_write_or(wal,
940 		    GEN7_UCGCTL4,
941 		    GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
942 
943 	/* WaInPlaceDecompressionHang:kbl */
944 	wa_write_or(wal,
945 		    GEN9_GAMT_ECO_REG_RW_IA,
946 		    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
947 }
948 
949 static void
950 glk_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
951 {
952 	gen9_gt_workarounds_init(i915, wal);
953 }
954 
955 static void
956 cfl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
957 {
958 	gen9_gt_workarounds_init(i915, wal);
959 
960 	/* WaDisableGafsUnitClkGating:cfl */
961 	wa_write_or(wal,
962 		    GEN7_UCGCTL4,
963 		    GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
964 
965 	/* WaInPlaceDecompressionHang:cfl */
966 	wa_write_or(wal,
967 		    GEN9_GAMT_ECO_REG_RW_IA,
968 		    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
969 }
970 
971 static void
972 wa_init_mcr(struct drm_i915_private *i915, struct i915_wa_list *wal)
973 {
974 	const struct sseu_dev_info *sseu = &i915->gt.info.sseu;
975 	unsigned int slice, subslice;
976 	u32 l3_en, mcr, mcr_mask;
977 
978 	GEM_BUG_ON(INTEL_GEN(i915) < 10);
979 
980 	/*
981 	 * WaProgramMgsrForL3BankSpecificMmioReads: cnl,icl
982 	 * L3Banks could be fused off in single slice scenario. If that is
983 	 * the case, we might need to program MCR select to a valid L3Bank
984 	 * by default, to make sure we correctly read certain registers
985 	 * later on (in the range 0xB100 - 0xB3FF).
986 	 *
987 	 * WaProgramMgsrForCorrectSliceSpecificMmioReads:cnl,icl
988 	 * Before any MMIO read into slice/subslice specific registers, MCR
989 	 * packet control register needs to be programmed to point to any
990 	 * enabled s/ss pair. Otherwise, incorrect values will be returned.
991 	 * This means each subsequent MMIO read will be forwarded to an
992 	 * specific s/ss combination, but this is OK since these registers
993 	 * are consistent across s/ss in almost all cases. In the rare
994 	 * occasions, such as INSTDONE, where this value is dependent
995 	 * on s/ss combo, the read should be done with read_subslice_reg.
996 	 *
997 	 * Since GEN8_MCR_SELECTOR contains dual-purpose bits which select both
998 	 * to which subslice, or to which L3 bank, the respective mmio reads
999 	 * will go, we have to find a common index which works for both
1000 	 * accesses.
1001 	 *
1002 	 * Case where we cannot find a common index fortunately should not
1003 	 * happen in production hardware, so we only emit a warning instead of
1004 	 * implementing something more complex that requires checking the range
1005 	 * of every MMIO read.
1006 	 */
1007 
1008 	if (INTEL_GEN(i915) >= 10 && is_power_of_2(sseu->slice_mask)) {
1009 		u32 l3_fuse =
1010 			intel_uncore_read(&i915->uncore, GEN10_MIRROR_FUSE3) &
1011 			GEN10_L3BANK_MASK;
1012 
1013 		drm_dbg(&i915->drm, "L3 fuse = %x\n", l3_fuse);
1014 		l3_en = ~(l3_fuse << GEN10_L3BANK_PAIR_COUNT | l3_fuse);
1015 	} else {
1016 		l3_en = ~0;
1017 	}
1018 
1019 	slice = fls(sseu->slice_mask) - 1;
1020 	subslice = fls(l3_en & intel_sseu_get_subslices(sseu, slice));
1021 	if (!subslice) {
1022 		drm_warn(&i915->drm,
1023 			 "No common index found between subslice mask %x and L3 bank mask %x!\n",
1024 			 intel_sseu_get_subslices(sseu, slice), l3_en);
1025 		subslice = fls(l3_en);
1026 		drm_WARN_ON(&i915->drm, !subslice);
1027 	}
1028 	subslice--;
1029 
1030 	if (INTEL_GEN(i915) >= 11) {
1031 		mcr = GEN11_MCR_SLICE(slice) | GEN11_MCR_SUBSLICE(subslice);
1032 		mcr_mask = GEN11_MCR_SLICE_MASK | GEN11_MCR_SUBSLICE_MASK;
1033 	} else {
1034 		mcr = GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
1035 		mcr_mask = GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK;
1036 	}
1037 
1038 	drm_dbg(&i915->drm, "MCR slice/subslice = %x\n", mcr);
1039 
1040 	wa_write_clr_set(wal, GEN8_MCR_SELECTOR, mcr_mask, mcr);
1041 }
1042 
1043 static void
1044 cnl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
1045 {
1046 	wa_init_mcr(i915, wal);
1047 
1048 	/* WaInPlaceDecompressionHang:cnl */
1049 	wa_write_or(wal,
1050 		    GEN9_GAMT_ECO_REG_RW_IA,
1051 		    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1052 }
1053 
1054 static void
1055 icl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
1056 {
1057 	wa_init_mcr(i915, wal);
1058 
1059 	/* WaInPlaceDecompressionHang:icl */
1060 	wa_write_or(wal,
1061 		    GEN9_GAMT_ECO_REG_RW_IA,
1062 		    GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1063 
1064 	/* WaModifyGamTlbPartitioning:icl */
1065 	wa_write_clr_set(wal,
1066 			 GEN11_GACB_PERF_CTRL,
1067 			 GEN11_HASH_CTRL_MASK,
1068 			 GEN11_HASH_CTRL_BIT0 | GEN11_HASH_CTRL_BIT4);
1069 
1070 	/* Wa_1405766107:icl
1071 	 * Formerly known as WaCL2SFHalfMaxAlloc
1072 	 */
1073 	wa_write_or(wal,
1074 		    GEN11_LSN_UNSLCVC,
1075 		    GEN11_LSN_UNSLCVC_GAFS_HALF_SF_MAXALLOC |
1076 		    GEN11_LSN_UNSLCVC_GAFS_HALF_CL2_MAXALLOC);
1077 
1078 	/* Wa_220166154:icl
1079 	 * Formerly known as WaDisCtxReload
1080 	 */
1081 	wa_write_or(wal,
1082 		    GEN8_GAMW_ECO_DEV_RW_IA,
1083 		    GAMW_ECO_DEV_CTX_RELOAD_DISABLE);
1084 
1085 	/* Wa_1405779004:icl (pre-prod) */
1086 	if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_A0))
1087 		wa_write_or(wal,
1088 			    SLICE_UNIT_LEVEL_CLKGATE,
1089 			    MSCUNIT_CLKGATE_DIS);
1090 
1091 	/* Wa_1406838659:icl (pre-prod) */
1092 	if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_B0))
1093 		wa_write_or(wal,
1094 			    INF_UNIT_LEVEL_CLKGATE,
1095 			    CGPSF_CLKGATE_DIS);
1096 
1097 	/* Wa_1406463099:icl
1098 	 * Formerly known as WaGamTlbPendError
1099 	 */
1100 	wa_write_or(wal,
1101 		    GAMT_CHKN_BIT_REG,
1102 		    GAMT_CHKN_DISABLE_L3_COH_PIPE);
1103 
1104 	/* Wa_1607087056:icl,ehl,jsl */
1105 	if (IS_ICELAKE(i915) ||
1106 		IS_JSL_EHL_REVID(i915, EHL_REVID_A0, EHL_REVID_A0)) {
1107 		wa_write_or(wal,
1108 			    SLICE_UNIT_LEVEL_CLKGATE,
1109 			    L3_CLKGATE_DIS | L3_CR2X_CLKGATE_DIS);
1110 	}
1111 }
1112 
1113 static void
1114 gen12_gt_workarounds_init(struct drm_i915_private *i915,
1115 			  struct i915_wa_list *wal)
1116 {
1117 	wa_init_mcr(i915, wal);
1118 }
1119 
1120 static void
1121 tgl_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
1122 {
1123 	gen12_gt_workarounds_init(i915, wal);
1124 
1125 	/* Wa_1409420604:tgl */
1126 	if (IS_TGL_UY_GT_REVID(i915, TGL_REVID_A0, TGL_REVID_A0))
1127 		wa_write_or(wal,
1128 			    SUBSLICE_UNIT_LEVEL_CLKGATE2,
1129 			    CPSSUNIT_CLKGATE_DIS);
1130 
1131 	/* Wa_1607087056:tgl also know as BUG:1409180338 */
1132 	if (IS_TGL_UY_GT_REVID(i915, TGL_REVID_A0, TGL_REVID_A0))
1133 		wa_write_or(wal,
1134 			    SLICE_UNIT_LEVEL_CLKGATE,
1135 			    L3_CLKGATE_DIS | L3_CR2X_CLKGATE_DIS);
1136 
1137 	/* Wa_1408615072:tgl[a0] */
1138 	if (IS_TGL_UY_GT_REVID(i915, TGL_REVID_A0, TGL_REVID_A0))
1139 		wa_write_or(wal, UNSLICE_UNIT_LEVEL_CLKGATE2,
1140 			    VSUNIT_CLKGATE_DIS_TGL);
1141 }
1142 
1143 static void
1144 dg1_gt_workarounds_init(struct drm_i915_private *i915, struct i915_wa_list *wal)
1145 {
1146 	gen12_gt_workarounds_init(i915, wal);
1147 
1148 	/* Wa_1607087056:dg1 */
1149 	if (IS_DG1_REVID(i915, DG1_REVID_A0, DG1_REVID_A0))
1150 		wa_write_or(wal,
1151 			    SLICE_UNIT_LEVEL_CLKGATE,
1152 			    L3_CLKGATE_DIS | L3_CR2X_CLKGATE_DIS);
1153 
1154 	/* Wa_1409420604:dg1 */
1155 	if (IS_DG1(i915))
1156 		wa_write_or(wal,
1157 			    SUBSLICE_UNIT_LEVEL_CLKGATE2,
1158 			    CPSSUNIT_CLKGATE_DIS);
1159 
1160 	/* Wa_1408615072:dg1 */
1161 	/* Empirical testing shows this register is unaffected by engine reset. */
1162 	if (IS_DG1(i915))
1163 		wa_write_or(wal, UNSLICE_UNIT_LEVEL_CLKGATE2,
1164 			    VSUNIT_CLKGATE_DIS_TGL);
1165 }
1166 
1167 static void
1168 gt_init_workarounds(struct drm_i915_private *i915, struct i915_wa_list *wal)
1169 {
1170 	if (IS_DG1(i915))
1171 		dg1_gt_workarounds_init(i915, wal);
1172 	else if (IS_TIGERLAKE(i915))
1173 		tgl_gt_workarounds_init(i915, wal);
1174 	else if (IS_GEN(i915, 12))
1175 		gen12_gt_workarounds_init(i915, wal);
1176 	else if (IS_GEN(i915, 11))
1177 		icl_gt_workarounds_init(i915, wal);
1178 	else if (IS_CANNONLAKE(i915))
1179 		cnl_gt_workarounds_init(i915, wal);
1180 	else if (IS_COFFEELAKE(i915) || IS_COMETLAKE(i915))
1181 		cfl_gt_workarounds_init(i915, wal);
1182 	else if (IS_GEMINILAKE(i915))
1183 		glk_gt_workarounds_init(i915, wal);
1184 	else if (IS_KABYLAKE(i915))
1185 		kbl_gt_workarounds_init(i915, wal);
1186 	else if (IS_BROXTON(i915))
1187 		bxt_gt_workarounds_init(i915, wal);
1188 	else if (IS_SKYLAKE(i915))
1189 		skl_gt_workarounds_init(i915, wal);
1190 	else if (IS_HASWELL(i915))
1191 		hsw_gt_workarounds_init(i915, wal);
1192 	else if (IS_VALLEYVIEW(i915))
1193 		vlv_gt_workarounds_init(i915, wal);
1194 	else if (IS_IVYBRIDGE(i915))
1195 		ivb_gt_workarounds_init(i915, wal);
1196 	else if (IS_GEN(i915, 6))
1197 		snb_gt_workarounds_init(i915, wal);
1198 	else if (IS_GEN(i915, 5))
1199 		ilk_gt_workarounds_init(i915, wal);
1200 	else if (IS_G4X(i915))
1201 		g4x_gt_workarounds_init(i915, wal);
1202 	else if (IS_GEN(i915, 4))
1203 		gen4_gt_workarounds_init(i915, wal);
1204 	else if (INTEL_GEN(i915) <= 8)
1205 		return;
1206 	else
1207 		MISSING_CASE(INTEL_GEN(i915));
1208 }
1209 
1210 void intel_gt_init_workarounds(struct drm_i915_private *i915)
1211 {
1212 	struct i915_wa_list *wal = &i915->gt_wa_list;
1213 
1214 	wa_init_start(wal, "GT", "global");
1215 	gt_init_workarounds(i915, wal);
1216 	wa_init_finish(wal);
1217 }
1218 
1219 static enum forcewake_domains
1220 wal_get_fw_for_rmw(struct intel_uncore *uncore, const struct i915_wa_list *wal)
1221 {
1222 	enum forcewake_domains fw = 0;
1223 	struct i915_wa *wa;
1224 	unsigned int i;
1225 
1226 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++)
1227 		fw |= intel_uncore_forcewake_for_reg(uncore,
1228 						     wa->reg,
1229 						     FW_REG_READ |
1230 						     FW_REG_WRITE);
1231 
1232 	return fw;
1233 }
1234 
1235 static bool
1236 wa_verify(const struct i915_wa *wa, u32 cur, const char *name, const char *from)
1237 {
1238 	if ((cur ^ wa->set) & wa->read) {
1239 		DRM_ERROR("%s workaround lost on %s! (reg[%x]=0x%x, relevant bits were 0x%x vs expected 0x%x)\n",
1240 			  name, from, i915_mmio_reg_offset(wa->reg),
1241 			  cur, cur & wa->read, wa->set & wa->read);
1242 
1243 		return false;
1244 	}
1245 
1246 	return true;
1247 }
1248 
1249 static void
1250 wa_list_apply(struct intel_uncore *uncore, const struct i915_wa_list *wal)
1251 {
1252 	enum forcewake_domains fw;
1253 	unsigned long flags;
1254 	struct i915_wa *wa;
1255 	unsigned int i;
1256 
1257 	if (!wal->count)
1258 		return;
1259 
1260 	fw = wal_get_fw_for_rmw(uncore, wal);
1261 
1262 	spin_lock_irqsave(&uncore->lock, flags);
1263 	intel_uncore_forcewake_get__locked(uncore, fw);
1264 
1265 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++) {
1266 		if (wa->clr)
1267 			intel_uncore_rmw_fw(uncore, wa->reg, wa->clr, wa->set);
1268 		else
1269 			intel_uncore_write_fw(uncore, wa->reg, wa->set);
1270 		if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1271 			wa_verify(wa,
1272 				  intel_uncore_read_fw(uncore, wa->reg),
1273 				  wal->name, "application");
1274 	}
1275 
1276 	intel_uncore_forcewake_put__locked(uncore, fw);
1277 	spin_unlock_irqrestore(&uncore->lock, flags);
1278 }
1279 
1280 void intel_gt_apply_workarounds(struct intel_gt *gt)
1281 {
1282 	wa_list_apply(gt->uncore, &gt->i915->gt_wa_list);
1283 }
1284 
1285 static bool wa_list_verify(struct intel_uncore *uncore,
1286 			   const struct i915_wa_list *wal,
1287 			   const char *from)
1288 {
1289 	struct i915_wa *wa;
1290 	unsigned int i;
1291 	bool ok = true;
1292 
1293 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++)
1294 		ok &= wa_verify(wa,
1295 				intel_uncore_read(uncore, wa->reg),
1296 				wal->name, from);
1297 
1298 	return ok;
1299 }
1300 
1301 bool intel_gt_verify_workarounds(struct intel_gt *gt, const char *from)
1302 {
1303 	return wa_list_verify(gt->uncore, &gt->i915->gt_wa_list, from);
1304 }
1305 
1306 __maybe_unused
1307 static bool is_nonpriv_flags_valid(u32 flags)
1308 {
1309 	/* Check only valid flag bits are set */
1310 	if (flags & ~RING_FORCE_TO_NONPRIV_MASK_VALID)
1311 		return false;
1312 
1313 	/* NB: Only 3 out of 4 enum values are valid for access field */
1314 	if ((flags & RING_FORCE_TO_NONPRIV_ACCESS_MASK) ==
1315 	    RING_FORCE_TO_NONPRIV_ACCESS_INVALID)
1316 		return false;
1317 
1318 	return true;
1319 }
1320 
1321 static void
1322 whitelist_reg_ext(struct i915_wa_list *wal, i915_reg_t reg, u32 flags)
1323 {
1324 	struct i915_wa wa = {
1325 		.reg = reg
1326 	};
1327 
1328 	if (GEM_DEBUG_WARN_ON(wal->count >= RING_MAX_NONPRIV_SLOTS))
1329 		return;
1330 
1331 	if (GEM_DEBUG_WARN_ON(!is_nonpriv_flags_valid(flags)))
1332 		return;
1333 
1334 	wa.reg.reg |= flags;
1335 	_wa_add(wal, &wa);
1336 }
1337 
1338 static void
1339 whitelist_reg(struct i915_wa_list *wal, i915_reg_t reg)
1340 {
1341 	whitelist_reg_ext(wal, reg, RING_FORCE_TO_NONPRIV_ACCESS_RW);
1342 }
1343 
1344 static void gen9_whitelist_build(struct i915_wa_list *w)
1345 {
1346 	/* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt,glk,cfl */
1347 	whitelist_reg(w, GEN9_CTX_PREEMPT_REG);
1348 
1349 	/* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl,cfl,[cnl] */
1350 	whitelist_reg(w, GEN8_CS_CHICKEN1);
1351 
1352 	/* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl,glk,cfl */
1353 	whitelist_reg(w, GEN8_HDC_CHICKEN1);
1354 
1355 	/* WaSendPushConstantsFromMMIO:skl,bxt */
1356 	whitelist_reg(w, COMMON_SLICE_CHICKEN2);
1357 }
1358 
1359 static void skl_whitelist_build(struct intel_engine_cs *engine)
1360 {
1361 	struct i915_wa_list *w = &engine->whitelist;
1362 
1363 	if (engine->class != RENDER_CLASS)
1364 		return;
1365 
1366 	gen9_whitelist_build(w);
1367 
1368 	/* WaDisableLSQCROPERFforOCL:skl */
1369 	whitelist_reg(w, GEN8_L3SQCREG4);
1370 }
1371 
1372 static void bxt_whitelist_build(struct intel_engine_cs *engine)
1373 {
1374 	if (engine->class != RENDER_CLASS)
1375 		return;
1376 
1377 	gen9_whitelist_build(&engine->whitelist);
1378 }
1379 
1380 static void kbl_whitelist_build(struct intel_engine_cs *engine)
1381 {
1382 	struct i915_wa_list *w = &engine->whitelist;
1383 
1384 	if (engine->class != RENDER_CLASS)
1385 		return;
1386 
1387 	gen9_whitelist_build(w);
1388 
1389 	/* WaDisableLSQCROPERFforOCL:kbl */
1390 	whitelist_reg(w, GEN8_L3SQCREG4);
1391 }
1392 
1393 static void glk_whitelist_build(struct intel_engine_cs *engine)
1394 {
1395 	struct i915_wa_list *w = &engine->whitelist;
1396 
1397 	if (engine->class != RENDER_CLASS)
1398 		return;
1399 
1400 	gen9_whitelist_build(w);
1401 
1402 	/* WA #0862: Userspace has to set "Barrier Mode" to avoid hangs. */
1403 	whitelist_reg(w, GEN9_SLICE_COMMON_ECO_CHICKEN1);
1404 }
1405 
1406 static void cfl_whitelist_build(struct intel_engine_cs *engine)
1407 {
1408 	struct i915_wa_list *w = &engine->whitelist;
1409 
1410 	if (engine->class != RENDER_CLASS)
1411 		return;
1412 
1413 	gen9_whitelist_build(w);
1414 
1415 	/*
1416 	 * WaAllowPMDepthAndInvocationCountAccessFromUMD:cfl,whl,cml,aml
1417 	 *
1418 	 * This covers 4 register which are next to one another :
1419 	 *   - PS_INVOCATION_COUNT
1420 	 *   - PS_INVOCATION_COUNT_UDW
1421 	 *   - PS_DEPTH_COUNT
1422 	 *   - PS_DEPTH_COUNT_UDW
1423 	 */
1424 	whitelist_reg_ext(w, PS_INVOCATION_COUNT,
1425 			  RING_FORCE_TO_NONPRIV_ACCESS_RD |
1426 			  RING_FORCE_TO_NONPRIV_RANGE_4);
1427 }
1428 
1429 static void cml_whitelist_build(struct intel_engine_cs *engine)
1430 {
1431 	struct i915_wa_list *w = &engine->whitelist;
1432 
1433 	if (engine->class != RENDER_CLASS)
1434 		whitelist_reg_ext(w,
1435 				  RING_CTX_TIMESTAMP(engine->mmio_base),
1436 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1437 
1438 	cfl_whitelist_build(engine);
1439 }
1440 
1441 static void cnl_whitelist_build(struct intel_engine_cs *engine)
1442 {
1443 	struct i915_wa_list *w = &engine->whitelist;
1444 
1445 	if (engine->class != RENDER_CLASS)
1446 		return;
1447 
1448 	/* WaEnablePreemptionGranularityControlByUMD:cnl */
1449 	whitelist_reg(w, GEN8_CS_CHICKEN1);
1450 }
1451 
1452 static void icl_whitelist_build(struct intel_engine_cs *engine)
1453 {
1454 	struct i915_wa_list *w = &engine->whitelist;
1455 
1456 	switch (engine->class) {
1457 	case RENDER_CLASS:
1458 		/* WaAllowUMDToModifyHalfSliceChicken7:icl */
1459 		whitelist_reg(w, GEN9_HALF_SLICE_CHICKEN7);
1460 
1461 		/* WaAllowUMDToModifySamplerMode:icl */
1462 		whitelist_reg(w, GEN10_SAMPLER_MODE);
1463 
1464 		/* WaEnableStateCacheRedirectToCS:icl */
1465 		whitelist_reg(w, GEN9_SLICE_COMMON_ECO_CHICKEN1);
1466 
1467 		/*
1468 		 * WaAllowPMDepthAndInvocationCountAccessFromUMD:icl
1469 		 *
1470 		 * This covers 4 register which are next to one another :
1471 		 *   - PS_INVOCATION_COUNT
1472 		 *   - PS_INVOCATION_COUNT_UDW
1473 		 *   - PS_DEPTH_COUNT
1474 		 *   - PS_DEPTH_COUNT_UDW
1475 		 */
1476 		whitelist_reg_ext(w, PS_INVOCATION_COUNT,
1477 				  RING_FORCE_TO_NONPRIV_ACCESS_RD |
1478 				  RING_FORCE_TO_NONPRIV_RANGE_4);
1479 		break;
1480 
1481 	case VIDEO_DECODE_CLASS:
1482 		/* hucStatusRegOffset */
1483 		whitelist_reg_ext(w, _MMIO(0x2000 + engine->mmio_base),
1484 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1485 		/* hucUKernelHdrInfoRegOffset */
1486 		whitelist_reg_ext(w, _MMIO(0x2014 + engine->mmio_base),
1487 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1488 		/* hucStatus2RegOffset */
1489 		whitelist_reg_ext(w, _MMIO(0x23B0 + engine->mmio_base),
1490 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1491 		whitelist_reg_ext(w,
1492 				  RING_CTX_TIMESTAMP(engine->mmio_base),
1493 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1494 		break;
1495 
1496 	default:
1497 		whitelist_reg_ext(w,
1498 				  RING_CTX_TIMESTAMP(engine->mmio_base),
1499 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1500 		break;
1501 	}
1502 }
1503 
1504 static void tgl_whitelist_build(struct intel_engine_cs *engine)
1505 {
1506 	struct i915_wa_list *w = &engine->whitelist;
1507 
1508 	switch (engine->class) {
1509 	case RENDER_CLASS:
1510 		/*
1511 		 * WaAllowPMDepthAndInvocationCountAccessFromUMD:tgl
1512 		 * Wa_1408556865:tgl
1513 		 *
1514 		 * This covers 4 registers which are next to one another :
1515 		 *   - PS_INVOCATION_COUNT
1516 		 *   - PS_INVOCATION_COUNT_UDW
1517 		 *   - PS_DEPTH_COUNT
1518 		 *   - PS_DEPTH_COUNT_UDW
1519 		 */
1520 		whitelist_reg_ext(w, PS_INVOCATION_COUNT,
1521 				  RING_FORCE_TO_NONPRIV_ACCESS_RD |
1522 				  RING_FORCE_TO_NONPRIV_RANGE_4);
1523 
1524 		/* Wa_1808121037:tgl */
1525 		whitelist_reg(w, GEN7_COMMON_SLICE_CHICKEN1);
1526 
1527 		/* Wa_1806527549:tgl */
1528 		whitelist_reg(w, HIZ_CHICKEN);
1529 		break;
1530 	default:
1531 		whitelist_reg_ext(w,
1532 				  RING_CTX_TIMESTAMP(engine->mmio_base),
1533 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1534 		break;
1535 	}
1536 }
1537 
1538 static void dg1_whitelist_build(struct intel_engine_cs *engine)
1539 {
1540 	struct i915_wa_list *w = &engine->whitelist;
1541 
1542 	tgl_whitelist_build(engine);
1543 
1544 	/* GEN:BUG:1409280441:dg1 */
1545 	if (IS_DG1_REVID(engine->i915, DG1_REVID_A0, DG1_REVID_A0) &&
1546 	    (engine->class == RENDER_CLASS ||
1547 	     engine->class == COPY_ENGINE_CLASS))
1548 		whitelist_reg_ext(w, RING_ID(engine->mmio_base),
1549 				  RING_FORCE_TO_NONPRIV_ACCESS_RD);
1550 }
1551 
1552 void intel_engine_init_whitelist(struct intel_engine_cs *engine)
1553 {
1554 	struct drm_i915_private *i915 = engine->i915;
1555 	struct i915_wa_list *w = &engine->whitelist;
1556 
1557 	wa_init_start(w, "whitelist", engine->name);
1558 
1559 	if (IS_DG1(i915))
1560 		dg1_whitelist_build(engine);
1561 	else if (IS_GEN(i915, 12))
1562 		tgl_whitelist_build(engine);
1563 	else if (IS_GEN(i915, 11))
1564 		icl_whitelist_build(engine);
1565 	else if (IS_CANNONLAKE(i915))
1566 		cnl_whitelist_build(engine);
1567 	else if (IS_COMETLAKE(i915))
1568 		cml_whitelist_build(engine);
1569 	else if (IS_COFFEELAKE(i915))
1570 		cfl_whitelist_build(engine);
1571 	else if (IS_GEMINILAKE(i915))
1572 		glk_whitelist_build(engine);
1573 	else if (IS_KABYLAKE(i915))
1574 		kbl_whitelist_build(engine);
1575 	else if (IS_BROXTON(i915))
1576 		bxt_whitelist_build(engine);
1577 	else if (IS_SKYLAKE(i915))
1578 		skl_whitelist_build(engine);
1579 	else if (INTEL_GEN(i915) <= 8)
1580 		return;
1581 	else
1582 		MISSING_CASE(INTEL_GEN(i915));
1583 
1584 	wa_init_finish(w);
1585 }
1586 
1587 void intel_engine_apply_whitelist(struct intel_engine_cs *engine)
1588 {
1589 	const struct i915_wa_list *wal = &engine->whitelist;
1590 	struct intel_uncore *uncore = engine->uncore;
1591 	const u32 base = engine->mmio_base;
1592 	struct i915_wa *wa;
1593 	unsigned int i;
1594 
1595 	if (!wal->count)
1596 		return;
1597 
1598 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++)
1599 		intel_uncore_write(uncore,
1600 				   RING_FORCE_TO_NONPRIV(base, i),
1601 				   i915_mmio_reg_offset(wa->reg));
1602 
1603 	/* And clear the rest just in case of garbage */
1604 	for (; i < RING_MAX_NONPRIV_SLOTS; i++)
1605 		intel_uncore_write(uncore,
1606 				   RING_FORCE_TO_NONPRIV(base, i),
1607 				   i915_mmio_reg_offset(RING_NOPID(base)));
1608 }
1609 
1610 static void
1611 rcs_engine_wa_init(struct intel_engine_cs *engine, struct i915_wa_list *wal)
1612 {
1613 	struct drm_i915_private *i915 = engine->i915;
1614 
1615 	if (IS_DG1_REVID(i915, DG1_REVID_A0, DG1_REVID_A0) ||
1616 	    IS_TGL_UY_GT_REVID(i915, TGL_REVID_A0, TGL_REVID_A0)) {
1617 		/*
1618 		 * Wa_1607138336:tgl[a0],dg1[a0]
1619 		 * Wa_1607063988:tgl[a0],dg1[a0]
1620 		 */
1621 		wa_write_or(wal,
1622 			    GEN9_CTX_PREEMPT_REG,
1623 			    GEN12_DISABLE_POSH_BUSY_FF_DOP_CG);
1624 	}
1625 
1626 	if (IS_TGL_UY_GT_REVID(i915, TGL_REVID_A0, TGL_REVID_A0)) {
1627 		/*
1628 		 * Wa_1606679103:tgl
1629 		 * (see also Wa_1606682166:icl)
1630 		 */
1631 		wa_write_or(wal,
1632 			    GEN7_SARCHKMD,
1633 			    GEN7_DISABLE_SAMPLER_PREFETCH);
1634 	}
1635 
1636 	if (IS_DG1(i915) || IS_ROCKETLAKE(i915) || IS_TIGERLAKE(i915)) {
1637 		/* Wa_1606931601:tgl,rkl,dg1 */
1638 		wa_masked_en(wal, GEN7_ROW_CHICKEN2, GEN12_DISABLE_EARLY_READ);
1639 
1640 		/*
1641 		 * Wa_1407928979:tgl A*
1642 		 * Wa_18011464164:tgl[B0+],dg1[B0+]
1643 		 * Wa_22010931296:tgl[B0+],dg1[B0+]
1644 		 * Wa_14010919138:rkl, dg1
1645 		 */
1646 		wa_write_or(wal, GEN7_FF_THREAD_MODE,
1647 			    GEN12_FF_TESSELATION_DOP_GATE_DISABLE);
1648 
1649 		/*
1650 		 * Wa_1606700617:tgl,dg1
1651 		 * Wa_22010271021:tgl,rkl,dg1
1652 		 */
1653 		wa_masked_en(wal,
1654 			     GEN9_CS_DEBUG_MODE1,
1655 			     FF_DOP_CLOCK_GATE_DISABLE);
1656 
1657 		/* Wa_1406941453:tgl,rkl,dg1 */
1658 		wa_masked_en(wal,
1659 			     GEN10_SAMPLER_MODE,
1660 			     ENABLE_SMALLPL);
1661 	}
1662 
1663 	if (IS_DG1_REVID(i915, DG1_REVID_A0, DG1_REVID_A0) ||
1664 	    IS_ROCKETLAKE(i915) || IS_TIGERLAKE(i915)) {
1665 		/* Wa_1409804808:tgl,rkl,dg1[a0] */
1666 		wa_masked_en(wal, GEN7_ROW_CHICKEN2,
1667 			     GEN12_PUSH_CONST_DEREF_HOLD_DIS);
1668 
1669 		/*
1670 		 * Wa_1409085225:tgl
1671 		 * Wa_14010229206:tgl,rkl,dg1[a0]
1672 		 */
1673 		wa_masked_en(wal, GEN9_ROW_CHICKEN4, GEN12_DISABLE_TDL_PUSH);
1674 
1675 		/*
1676 		 * Wa_1607030317:tgl
1677 		 * Wa_1607186500:tgl
1678 		 * Wa_1607297627:tgl,rkl,dg1[a0]
1679 		 *
1680 		 * On TGL and RKL there are multiple entries for this WA in the
1681 		 * BSpec; some indicate this is an A0-only WA, others indicate
1682 		 * it applies to all steppings so we trust the "all steppings."
1683 		 * For DG1 this only applies to A0.
1684 		 */
1685 		wa_masked_en(wal,
1686 			     GEN6_RC_SLEEP_PSMI_CONTROL,
1687 			     GEN12_WAIT_FOR_EVENT_POWER_DOWN_DISABLE |
1688 			     GEN8_RC_SEMA_IDLE_MSG_DISABLE);
1689 	}
1690 
1691 	if (IS_GEN(i915, 11)) {
1692 		/* This is not an Wa. Enable for better image quality */
1693 		wa_masked_en(wal,
1694 			     _3D_CHICKEN3,
1695 			     _3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE);
1696 
1697 		/* WaPipelineFlushCoherentLines:icl */
1698 		wa_write_or(wal,
1699 			    GEN8_L3SQCREG4,
1700 			    GEN8_LQSC_FLUSH_COHERENT_LINES);
1701 
1702 		/*
1703 		 * Wa_1405543622:icl
1704 		 * Formerly known as WaGAPZPriorityScheme
1705 		 */
1706 		wa_write_or(wal,
1707 			    GEN8_GARBCNTL,
1708 			    GEN11_ARBITRATION_PRIO_ORDER_MASK);
1709 
1710 		/*
1711 		 * Wa_1604223664:icl
1712 		 * Formerly known as WaL3BankAddressHashing
1713 		 */
1714 		wa_write_clr_set(wal,
1715 				 GEN8_GARBCNTL,
1716 				 GEN11_HASH_CTRL_EXCL_MASK,
1717 				 GEN11_HASH_CTRL_EXCL_BIT0);
1718 		wa_write_clr_set(wal,
1719 				 GEN11_GLBLINVL,
1720 				 GEN11_BANK_HASH_ADDR_EXCL_MASK,
1721 				 GEN11_BANK_HASH_ADDR_EXCL_BIT0);
1722 
1723 		/*
1724 		 * Wa_1405733216:icl
1725 		 * Formerly known as WaDisableCleanEvicts
1726 		 */
1727 		wa_write_or(wal,
1728 			    GEN8_L3SQCREG4,
1729 			    GEN11_LQSC_CLEAN_EVICT_DISABLE);
1730 
1731 		/* WaForwardProgressSoftReset:icl */
1732 		wa_write_or(wal,
1733 			    GEN10_SCRATCH_LNCF2,
1734 			    PMFLUSHDONE_LNICRSDROP |
1735 			    PMFLUSH_GAPL3UNBLOCK |
1736 			    PMFLUSHDONE_LNEBLK);
1737 
1738 		/* Wa_1406609255:icl (pre-prod) */
1739 		if (IS_ICL_REVID(i915, ICL_REVID_A0, ICL_REVID_B0))
1740 			wa_write_or(wal,
1741 				    GEN7_SARCHKMD,
1742 				    GEN7_DISABLE_DEMAND_PREFETCH);
1743 
1744 		/* Wa_1606682166:icl */
1745 		wa_write_or(wal,
1746 			    GEN7_SARCHKMD,
1747 			    GEN7_DISABLE_SAMPLER_PREFETCH);
1748 
1749 		/* Wa_1409178092:icl */
1750 		wa_write_clr_set(wal,
1751 				 GEN11_SCRATCH2,
1752 				 GEN11_COHERENT_PARTIAL_WRITE_MERGE_ENABLE,
1753 				 0);
1754 
1755 		/* WaEnable32PlaneMode:icl */
1756 		wa_masked_en(wal, GEN9_CSFE_CHICKEN1_RCS,
1757 			     GEN11_ENABLE_32_PLANE_MODE);
1758 
1759 		/*
1760 		 * Wa_1408615072:icl,ehl  (vsunit)
1761 		 * Wa_1407596294:icl,ehl  (hsunit)
1762 		 */
1763 		wa_write_or(wal, UNSLICE_UNIT_LEVEL_CLKGATE,
1764 			    VSUNIT_CLKGATE_DIS | HSUNIT_CLKGATE_DIS);
1765 
1766 		/* Wa_1407352427:icl,ehl */
1767 		wa_write_or(wal, UNSLICE_UNIT_LEVEL_CLKGATE2,
1768 			    PSDUNIT_CLKGATE_DIS);
1769 
1770 		/* Wa_1406680159:icl,ehl */
1771 		wa_write_or(wal,
1772 			    SUBSLICE_UNIT_LEVEL_CLKGATE,
1773 			    GWUNIT_CLKGATE_DIS);
1774 
1775 		/*
1776 		 * Wa_1408767742:icl[a2..forever],ehl[all]
1777 		 * Wa_1605460711:icl[a0..c0]
1778 		 */
1779 		wa_write_or(wal,
1780 			    GEN7_FF_THREAD_MODE,
1781 			    GEN12_FF_TESSELATION_DOP_GATE_DISABLE);
1782 
1783 		/* Wa_22010271021:ehl */
1784 		if (IS_JSL_EHL(i915))
1785 			wa_masked_en(wal,
1786 				     GEN9_CS_DEBUG_MODE1,
1787 				     FF_DOP_CLOCK_GATE_DISABLE);
1788 	}
1789 
1790 	if (IS_GEN_RANGE(i915, 9, 12)) {
1791 		/* FtrPerCtxtPreemptionGranularityControl:skl,bxt,kbl,cfl,cnl,icl,tgl */
1792 		wa_masked_en(wal,
1793 			     GEN7_FF_SLICE_CS_CHICKEN1,
1794 			     GEN9_FFSC_PERCTX_PREEMPT_CTRL);
1795 	}
1796 
1797 	if (IS_SKYLAKE(i915) ||
1798 	    IS_KABYLAKE(i915) ||
1799 	    IS_COFFEELAKE(i915) ||
1800 	    IS_COMETLAKE(i915)) {
1801 		/* WaEnableGapsTsvCreditFix:skl,kbl,cfl */
1802 		wa_write_or(wal,
1803 			    GEN8_GARBCNTL,
1804 			    GEN9_GAPS_TSV_CREDIT_DISABLE);
1805 	}
1806 
1807 	if (IS_BROXTON(i915)) {
1808 		/* WaDisablePooledEuLoadBalancingFix:bxt */
1809 		wa_masked_en(wal,
1810 			     FF_SLICE_CS_CHICKEN2,
1811 			     GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1812 	}
1813 
1814 	if (IS_GEN(i915, 9)) {
1815 		/* WaContextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl,glk,cfl */
1816 		wa_masked_en(wal,
1817 			     GEN9_CSFE_CHICKEN1_RCS,
1818 			     GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE);
1819 
1820 		/* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl,glk,cfl */
1821 		wa_write_or(wal,
1822 			    BDW_SCRATCH1,
1823 			    GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
1824 
1825 		/* WaProgramL3SqcReg1DefaultForPerf:bxt,glk */
1826 		if (IS_GEN9_LP(i915))
1827 			wa_write_clr_set(wal,
1828 					 GEN8_L3SQCREG1,
1829 					 L3_PRIO_CREDITS_MASK,
1830 					 L3_GENERAL_PRIO_CREDITS(62) |
1831 					 L3_HIGH_PRIO_CREDITS(2));
1832 
1833 		/* WaOCLCoherentLineFlush:skl,bxt,kbl,cfl */
1834 		wa_write_or(wal,
1835 			    GEN8_L3SQCREG4,
1836 			    GEN8_LQSC_FLUSH_COHERENT_LINES);
1837 
1838 		/* Disable atomics in L3 to prevent unrecoverable hangs */
1839 		wa_write_clr_set(wal, GEN9_SCRATCH_LNCF1,
1840 				 GEN9_LNCF_NONIA_COHERENT_ATOMICS_ENABLE, 0);
1841 		wa_write_clr_set(wal, GEN8_L3SQCREG4,
1842 				 GEN8_LQSQ_NONIA_COHERENT_ATOMICS_ENABLE, 0);
1843 		wa_write_clr_set(wal, GEN9_SCRATCH1,
1844 				 EVICTION_PERF_FIX_ENABLE, 0);
1845 	}
1846 
1847 	if (IS_HASWELL(i915)) {
1848 		/* WaSampleCChickenBitEnable:hsw */
1849 		wa_masked_en(wal,
1850 			     HALF_SLICE_CHICKEN3, HSW_SAMPLE_C_PERFORMANCE);
1851 
1852 		wa_masked_dis(wal,
1853 			      CACHE_MODE_0_GEN7,
1854 			      /* enable HiZ Raw Stall Optimization */
1855 			      HIZ_RAW_STALL_OPT_DISABLE);
1856 
1857 		/* WaDisable4x2SubspanOptimization:hsw */
1858 		wa_masked_en(wal, CACHE_MODE_1, PIXEL_SUBSPAN_COLLECT_OPT_DISABLE);
1859 	}
1860 
1861 	if (IS_VALLEYVIEW(i915)) {
1862 		/* WaDisableEarlyCull:vlv */
1863 		wa_masked_en(wal,
1864 			     _3D_CHICKEN3,
1865 			     _3D_CHICKEN_SF_DISABLE_OBJEND_CULL);
1866 
1867 		/*
1868 		 * WaVSThreadDispatchOverride:ivb,vlv
1869 		 *
1870 		 * This actually overrides the dispatch
1871 		 * mode for all thread types.
1872 		 */
1873 		wa_write_clr_set(wal,
1874 				 GEN7_FF_THREAD_MODE,
1875 				 GEN7_FF_SCHED_MASK,
1876 				 GEN7_FF_TS_SCHED_HW |
1877 				 GEN7_FF_VS_SCHED_HW |
1878 				 GEN7_FF_DS_SCHED_HW);
1879 
1880 		/* WaPsdDispatchEnable:vlv */
1881 		/* WaDisablePSDDualDispatchEnable:vlv */
1882 		wa_masked_en(wal,
1883 			     GEN7_HALF_SLICE_CHICKEN1,
1884 			     GEN7_MAX_PS_THREAD_DEP |
1885 			     GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE);
1886 	}
1887 
1888 	if (IS_IVYBRIDGE(i915)) {
1889 		/* WaDisableEarlyCull:ivb */
1890 		wa_masked_en(wal,
1891 			     _3D_CHICKEN3,
1892 			     _3D_CHICKEN_SF_DISABLE_OBJEND_CULL);
1893 
1894 		if (0) { /* causes HiZ corruption on ivb:gt1 */
1895 			/* enable HiZ Raw Stall Optimization */
1896 			wa_masked_dis(wal,
1897 				      CACHE_MODE_0_GEN7,
1898 				      HIZ_RAW_STALL_OPT_DISABLE);
1899 		}
1900 
1901 		/*
1902 		 * WaVSThreadDispatchOverride:ivb,vlv
1903 		 *
1904 		 * This actually overrides the dispatch
1905 		 * mode for all thread types.
1906 		 */
1907 		wa_write_clr_set(wal,
1908 				 GEN7_FF_THREAD_MODE,
1909 				 GEN7_FF_SCHED_MASK,
1910 				 GEN7_FF_TS_SCHED_HW |
1911 				 GEN7_FF_VS_SCHED_HW |
1912 				 GEN7_FF_DS_SCHED_HW);
1913 
1914 		/* WaDisablePSDDualDispatchEnable:ivb */
1915 		if (IS_IVB_GT1(i915))
1916 			wa_masked_en(wal,
1917 				     GEN7_HALF_SLICE_CHICKEN1,
1918 				     GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE);
1919 	}
1920 
1921 	if (IS_GEN(i915, 7)) {
1922 		/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1923 		wa_masked_en(wal,
1924 			     GFX_MODE_GEN7,
1925 			     GFX_TLB_INVALIDATE_EXPLICIT | GFX_REPLAY_MODE);
1926 
1927 		/* WaDisable_RenderCache_OperationalFlush:ivb,vlv,hsw */
1928 		wa_masked_dis(wal, CACHE_MODE_0_GEN7, RC_OP_FLUSH_ENABLE);
1929 
1930 		/*
1931 		 * BSpec says this must be set, even though
1932 		 * WaDisable4x2SubspanOptimization:ivb,hsw
1933 		 * WaDisable4x2SubspanOptimization isn't listed for VLV.
1934 		 */
1935 		wa_masked_en(wal,
1936 			     CACHE_MODE_1,
1937 			     PIXEL_SUBSPAN_COLLECT_OPT_DISABLE);
1938 
1939 		/*
1940 		 * BSpec recommends 8x4 when MSAA is used,
1941 		 * however in practice 16x4 seems fastest.
1942 		 *
1943 		 * Note that PS/WM thread counts depend on the WIZ hashing
1944 		 * disable bit, which we don't touch here, but it's good
1945 		 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
1946 		 */
1947 		wa_add(wal, GEN7_GT_MODE, 0,
1948 		       _MASKED_FIELD(GEN6_WIZ_HASHING_MASK,
1949 				     GEN6_WIZ_HASHING_16x4),
1950 		       GEN6_WIZ_HASHING_16x4);
1951 	}
1952 
1953 	if (IS_GEN_RANGE(i915, 6, 7))
1954 		/*
1955 		 * We need to disable the AsyncFlip performance optimisations in
1956 		 * order to use MI_WAIT_FOR_EVENT within the CS. It should
1957 		 * already be programmed to '1' on all products.
1958 		 *
1959 		 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1960 		 */
1961 		wa_masked_en(wal,
1962 			     MI_MODE,
1963 			     ASYNC_FLIP_PERF_DISABLE);
1964 
1965 	if (IS_GEN(i915, 6)) {
1966 		/*
1967 		 * Required for the hardware to program scanline values for
1968 		 * waiting
1969 		 * WaEnableFlushTlbInvalidationMode:snb
1970 		 */
1971 		wa_masked_en(wal,
1972 			     GFX_MODE,
1973 			     GFX_TLB_INVALIDATE_EXPLICIT);
1974 
1975 		/* WaDisableHiZPlanesWhenMSAAEnabled:snb */
1976 		wa_masked_en(wal,
1977 			     _3D_CHICKEN,
1978 			     _3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB);
1979 
1980 		wa_masked_en(wal,
1981 			     _3D_CHICKEN3,
1982 			     /* WaStripsFansDisableFastClipPerformanceFix:snb */
1983 			     _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL |
1984 			     /*
1985 			      * Bspec says:
1986 			      * "This bit must be set if 3DSTATE_CLIP clip mode is set
1987 			      * to normal and 3DSTATE_SF number of SF output attributes
1988 			      * is more than 16."
1989 			      */
1990 			     _3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH);
1991 
1992 		/*
1993 		 * BSpec recommends 8x4 when MSAA is used,
1994 		 * however in practice 16x4 seems fastest.
1995 		 *
1996 		 * Note that PS/WM thread counts depend on the WIZ hashing
1997 		 * disable bit, which we don't touch here, but it's good
1998 		 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
1999 		 */
2000 		wa_add(wal,
2001 		       GEN6_GT_MODE, 0,
2002 		       _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4),
2003 		       GEN6_WIZ_HASHING_16x4);
2004 
2005 		/* WaDisable_RenderCache_OperationalFlush:snb */
2006 		wa_masked_dis(wal, CACHE_MODE_0, RC_OP_FLUSH_ENABLE);
2007 
2008 		/*
2009 		 * From the Sandybridge PRM, volume 1 part 3, page 24:
2010 		 * "If this bit is set, STCunit will have LRA as replacement
2011 		 *  policy. [...] This bit must be reset. LRA replacement
2012 		 *  policy is not supported."
2013 		 */
2014 		wa_masked_dis(wal,
2015 			      CACHE_MODE_0,
2016 			      CM0_STC_EVICT_DISABLE_LRA_SNB);
2017 	}
2018 
2019 	if (IS_GEN_RANGE(i915, 4, 6))
2020 		/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
2021 		wa_add(wal, MI_MODE,
2022 		       0, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH),
2023 		       /* XXX bit doesn't stick on Broadwater */
2024 		       IS_I965G(i915) ? 0 : VS_TIMER_DISPATCH);
2025 
2026 	if (IS_GEN(i915, 4))
2027 		/*
2028 		 * Disable CONSTANT_BUFFER before it is loaded from the context
2029 		 * image. For as it is loaded, it is executed and the stored
2030 		 * address may no longer be valid, leading to a GPU hang.
2031 		 *
2032 		 * This imposes the requirement that userspace reload their
2033 		 * CONSTANT_BUFFER on every batch, fortunately a requirement
2034 		 * they are already accustomed to from before contexts were
2035 		 * enabled.
2036 		 */
2037 		wa_add(wal, ECOSKPD,
2038 		       0, _MASKED_BIT_ENABLE(ECO_CONSTANT_BUFFER_SR_DISABLE),
2039 		       0 /* XXX bit doesn't stick on Broadwater */);
2040 }
2041 
2042 static void
2043 xcs_engine_wa_init(struct intel_engine_cs *engine, struct i915_wa_list *wal)
2044 {
2045 	struct drm_i915_private *i915 = engine->i915;
2046 
2047 	/* WaKBLVECSSemaphoreWaitPoll:kbl */
2048 	if (IS_KBL_GT_REVID(i915, KBL_REVID_A0, KBL_REVID_E0)) {
2049 		wa_write(wal,
2050 			 RING_SEMA_WAIT_POLL(engine->mmio_base),
2051 			 1);
2052 	}
2053 }
2054 
2055 static void
2056 engine_init_workarounds(struct intel_engine_cs *engine, struct i915_wa_list *wal)
2057 {
2058 	if (I915_SELFTEST_ONLY(INTEL_GEN(engine->i915) < 4))
2059 		return;
2060 
2061 	if (engine->class == RENDER_CLASS)
2062 		rcs_engine_wa_init(engine, wal);
2063 	else
2064 		xcs_engine_wa_init(engine, wal);
2065 }
2066 
2067 void intel_engine_init_workarounds(struct intel_engine_cs *engine)
2068 {
2069 	struct i915_wa_list *wal = &engine->wa_list;
2070 
2071 	if (INTEL_GEN(engine->i915) < 4)
2072 		return;
2073 
2074 	wa_init_start(wal, "engine", engine->name);
2075 	engine_init_workarounds(engine, wal);
2076 	wa_init_finish(wal);
2077 }
2078 
2079 void intel_engine_apply_workarounds(struct intel_engine_cs *engine)
2080 {
2081 	wa_list_apply(engine->uncore, &engine->wa_list);
2082 }
2083 
2084 struct mcr_range {
2085 	u32 start;
2086 	u32 end;
2087 };
2088 
2089 static const struct mcr_range mcr_ranges_gen8[] = {
2090 	{ .start = 0x5500, .end = 0x55ff },
2091 	{ .start = 0x7000, .end = 0x7fff },
2092 	{ .start = 0x9400, .end = 0x97ff },
2093 	{ .start = 0xb000, .end = 0xb3ff },
2094 	{ .start = 0xe000, .end = 0xe7ff },
2095 	{},
2096 };
2097 
2098 static const struct mcr_range mcr_ranges_gen12[] = {
2099 	{ .start =  0x8150, .end =  0x815f },
2100 	{ .start =  0x9520, .end =  0x955f },
2101 	{ .start =  0xb100, .end =  0xb3ff },
2102 	{ .start =  0xde80, .end =  0xe8ff },
2103 	{ .start = 0x24a00, .end = 0x24a7f },
2104 	{},
2105 };
2106 
2107 static bool mcr_range(struct drm_i915_private *i915, u32 offset)
2108 {
2109 	const struct mcr_range *mcr_ranges;
2110 	int i;
2111 
2112 	if (INTEL_GEN(i915) >= 12)
2113 		mcr_ranges = mcr_ranges_gen12;
2114 	else if (INTEL_GEN(i915) >= 8)
2115 		mcr_ranges = mcr_ranges_gen8;
2116 	else
2117 		return false;
2118 
2119 	/*
2120 	 * Registers in these ranges are affected by the MCR selector
2121 	 * which only controls CPU initiated MMIO. Routing does not
2122 	 * work for CS access so we cannot verify them on this path.
2123 	 */
2124 	for (i = 0; mcr_ranges[i].start; i++)
2125 		if (offset >= mcr_ranges[i].start &&
2126 		    offset <= mcr_ranges[i].end)
2127 			return true;
2128 
2129 	return false;
2130 }
2131 
2132 static int
2133 wa_list_srm(struct i915_request *rq,
2134 	    const struct i915_wa_list *wal,
2135 	    struct i915_vma *vma)
2136 {
2137 	struct drm_i915_private *i915 = rq->engine->i915;
2138 	unsigned int i, count = 0;
2139 	const struct i915_wa *wa;
2140 	u32 srm, *cs;
2141 
2142 	srm = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
2143 	if (INTEL_GEN(i915) >= 8)
2144 		srm++;
2145 
2146 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++) {
2147 		if (!mcr_range(i915, i915_mmio_reg_offset(wa->reg)))
2148 			count++;
2149 	}
2150 
2151 	cs = intel_ring_begin(rq, 4 * count);
2152 	if (IS_ERR(cs))
2153 		return PTR_ERR(cs);
2154 
2155 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++) {
2156 		u32 offset = i915_mmio_reg_offset(wa->reg);
2157 
2158 		if (mcr_range(i915, offset))
2159 			continue;
2160 
2161 		*cs++ = srm;
2162 		*cs++ = offset;
2163 		*cs++ = i915_ggtt_offset(vma) + sizeof(u32) * i;
2164 		*cs++ = 0;
2165 	}
2166 	intel_ring_advance(rq, cs);
2167 
2168 	return 0;
2169 }
2170 
2171 static int engine_wa_list_verify(struct intel_context *ce,
2172 				 const struct i915_wa_list * const wal,
2173 				 const char *from)
2174 {
2175 	const struct i915_wa *wa;
2176 	struct i915_request *rq;
2177 	struct i915_vma *vma;
2178 	struct i915_gem_ww_ctx ww;
2179 	unsigned int i;
2180 	u32 *results;
2181 	int err;
2182 
2183 	if (!wal->count)
2184 		return 0;
2185 
2186 	vma = __vm_create_scratch_for_read(&ce->engine->gt->ggtt->vm,
2187 					   wal->count * sizeof(u32));
2188 	if (IS_ERR(vma))
2189 		return PTR_ERR(vma);
2190 
2191 	intel_engine_pm_get(ce->engine);
2192 	i915_gem_ww_ctx_init(&ww, false);
2193 retry:
2194 	err = i915_gem_object_lock(vma->obj, &ww);
2195 	if (err == 0)
2196 		err = intel_context_pin_ww(ce, &ww);
2197 	if (err)
2198 		goto err_pm;
2199 
2200 	rq = i915_request_create(ce);
2201 	if (IS_ERR(rq)) {
2202 		err = PTR_ERR(rq);
2203 		goto err_unpin;
2204 	}
2205 
2206 	err = i915_request_await_object(rq, vma->obj, true);
2207 	if (err == 0)
2208 		err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
2209 	if (err == 0)
2210 		err = wa_list_srm(rq, wal, vma);
2211 
2212 	i915_request_get(rq);
2213 	if (err)
2214 		i915_request_set_error_once(rq, err);
2215 	i915_request_add(rq);
2216 
2217 	if (err)
2218 		goto err_rq;
2219 
2220 	if (i915_request_wait(rq, 0, HZ / 5) < 0) {
2221 		err = -ETIME;
2222 		goto err_rq;
2223 	}
2224 
2225 	results = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
2226 	if (IS_ERR(results)) {
2227 		err = PTR_ERR(results);
2228 		goto err_rq;
2229 	}
2230 
2231 	err = 0;
2232 	for (i = 0, wa = wal->list; i < wal->count; i++, wa++) {
2233 		if (mcr_range(rq->engine->i915, i915_mmio_reg_offset(wa->reg)))
2234 			continue;
2235 
2236 		if (!wa_verify(wa, results[i], wal->name, from))
2237 			err = -ENXIO;
2238 	}
2239 
2240 	i915_gem_object_unpin_map(vma->obj);
2241 
2242 err_rq:
2243 	i915_request_put(rq);
2244 err_unpin:
2245 	intel_context_unpin(ce);
2246 err_pm:
2247 	if (err == -EDEADLK) {
2248 		err = i915_gem_ww_ctx_backoff(&ww);
2249 		if (!err)
2250 			goto retry;
2251 	}
2252 	i915_gem_ww_ctx_fini(&ww);
2253 	intel_engine_pm_put(ce->engine);
2254 	i915_vma_unpin(vma);
2255 	i915_vma_put(vma);
2256 	return err;
2257 }
2258 
2259 int intel_engine_verify_workarounds(struct intel_engine_cs *engine,
2260 				    const char *from)
2261 {
2262 	return engine_wa_list_verify(engine->kernel_context,
2263 				     &engine->wa_list,
2264 				     from);
2265 }
2266 
2267 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2268 #include "selftest_workarounds.c"
2269 #endif
2270