xref: /linux/drivers/gpu/drm/i915/intel_uncore.c (revision 6b7cbdbe)
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <drm/drm_managed.h>
25 #include <linux/pm_runtime.h>
26 
27 #include "gt/intel_engine_regs.h"
28 #include "gt/intel_gt_regs.h"
29 
30 #include "i915_drv.h"
31 #include "i915_iosf_mbi.h"
32 #include "i915_trace.h"
33 #include "i915_vgpu.h"
34 #include "intel_pm.h"
35 
36 #define FORCEWAKE_ACK_TIMEOUT_MS 50
37 #define GT_FIFO_TIMEOUT_MS	 10
38 
39 #define __raw_posting_read(...) ((void)__raw_uncore_read32(__VA_ARGS__))
40 
41 static void
42 fw_domains_get(struct intel_uncore *uncore, enum forcewake_domains fw_domains)
43 {
44 	uncore->fw_get_funcs->force_wake_get(uncore, fw_domains);
45 }
46 
47 void
48 intel_uncore_mmio_debug_init_early(struct drm_i915_private *i915)
49 {
50 	spin_lock_init(&i915->mmio_debug.lock);
51 	i915->mmio_debug.unclaimed_mmio_check = 1;
52 
53 	i915->uncore.debug = &i915->mmio_debug;
54 }
55 
56 static void mmio_debug_suspend(struct intel_uncore *uncore)
57 {
58 	if (!uncore->debug)
59 		return;
60 
61 	spin_lock(&uncore->debug->lock);
62 
63 	/* Save and disable mmio debugging for the user bypass */
64 	if (!uncore->debug->suspend_count++) {
65 		uncore->debug->saved_mmio_check = uncore->debug->unclaimed_mmio_check;
66 		uncore->debug->unclaimed_mmio_check = 0;
67 	}
68 
69 	spin_unlock(&uncore->debug->lock);
70 }
71 
72 static bool check_for_unclaimed_mmio(struct intel_uncore *uncore);
73 
74 static void mmio_debug_resume(struct intel_uncore *uncore)
75 {
76 	if (!uncore->debug)
77 		return;
78 
79 	spin_lock(&uncore->debug->lock);
80 
81 	if (!--uncore->debug->suspend_count)
82 		uncore->debug->unclaimed_mmio_check = uncore->debug->saved_mmio_check;
83 
84 	if (check_for_unclaimed_mmio(uncore))
85 		drm_info(&uncore->i915->drm,
86 			 "Invalid mmio detected during user access\n");
87 
88 	spin_unlock(&uncore->debug->lock);
89 }
90 
91 static const char * const forcewake_domain_names[] = {
92 	"render",
93 	"gt",
94 	"media",
95 	"vdbox0",
96 	"vdbox1",
97 	"vdbox2",
98 	"vdbox3",
99 	"vdbox4",
100 	"vdbox5",
101 	"vdbox6",
102 	"vdbox7",
103 	"vebox0",
104 	"vebox1",
105 	"vebox2",
106 	"vebox3",
107 	"gsc",
108 };
109 
110 const char *
111 intel_uncore_forcewake_domain_to_str(const enum forcewake_domain_id id)
112 {
113 	BUILD_BUG_ON(ARRAY_SIZE(forcewake_domain_names) != FW_DOMAIN_ID_COUNT);
114 
115 	if (id >= 0 && id < FW_DOMAIN_ID_COUNT)
116 		return forcewake_domain_names[id];
117 
118 	WARN_ON(id);
119 
120 	return "unknown";
121 }
122 
123 #define fw_ack(d) readl((d)->reg_ack)
124 #define fw_set(d, val) writel(_MASKED_BIT_ENABLE((val)), (d)->reg_set)
125 #define fw_clear(d, val) writel(_MASKED_BIT_DISABLE((val)), (d)->reg_set)
126 
127 static inline void
128 fw_domain_reset(const struct intel_uncore_forcewake_domain *d)
129 {
130 	/*
131 	 * We don't really know if the powerwell for the forcewake domain we are
132 	 * trying to reset here does exist at this point (engines could be fused
133 	 * off in ICL+), so no waiting for acks
134 	 */
135 	/* WaRsClearFWBitsAtReset */
136 	if (GRAPHICS_VER(d->uncore->i915) >= 12)
137 		fw_clear(d, 0xefff);
138 	else
139 		fw_clear(d, 0xffff);
140 }
141 
142 static inline void
143 fw_domain_arm_timer(struct intel_uncore_forcewake_domain *d)
144 {
145 	GEM_BUG_ON(d->uncore->fw_domains_timer & d->mask);
146 	d->uncore->fw_domains_timer |= d->mask;
147 	d->wake_count++;
148 	hrtimer_start_range_ns(&d->timer,
149 			       NSEC_PER_MSEC,
150 			       NSEC_PER_MSEC,
151 			       HRTIMER_MODE_REL);
152 }
153 
154 static inline int
155 __wait_for_ack(const struct intel_uncore_forcewake_domain *d,
156 	       const u32 ack,
157 	       const u32 value)
158 {
159 	return wait_for_atomic((fw_ack(d) & ack) == value,
160 			       FORCEWAKE_ACK_TIMEOUT_MS);
161 }
162 
163 static inline int
164 wait_ack_clear(const struct intel_uncore_forcewake_domain *d,
165 	       const u32 ack)
166 {
167 	return __wait_for_ack(d, ack, 0);
168 }
169 
170 static inline int
171 wait_ack_set(const struct intel_uncore_forcewake_domain *d,
172 	     const u32 ack)
173 {
174 	return __wait_for_ack(d, ack, ack);
175 }
176 
177 static inline void
178 fw_domain_wait_ack_clear(const struct intel_uncore_forcewake_domain *d)
179 {
180 	if (wait_ack_clear(d, FORCEWAKE_KERNEL)) {
181 		drm_err(&d->uncore->i915->drm,
182 			"%s: timed out waiting for forcewake ack to clear.\n",
183 			intel_uncore_forcewake_domain_to_str(d->id));
184 		add_taint_for_CI(d->uncore->i915, TAINT_WARN); /* CI now unreliable */
185 	}
186 }
187 
188 enum ack_type {
189 	ACK_CLEAR = 0,
190 	ACK_SET
191 };
192 
193 static int
194 fw_domain_wait_ack_with_fallback(const struct intel_uncore_forcewake_domain *d,
195 				 const enum ack_type type)
196 {
197 	const u32 ack_bit = FORCEWAKE_KERNEL;
198 	const u32 value = type == ACK_SET ? ack_bit : 0;
199 	unsigned int pass;
200 	bool ack_detected;
201 
202 	/*
203 	 * There is a possibility of driver's wake request colliding
204 	 * with hardware's own wake requests and that can cause
205 	 * hardware to not deliver the driver's ack message.
206 	 *
207 	 * Use a fallback bit toggle to kick the gpu state machine
208 	 * in the hope that the original ack will be delivered along with
209 	 * the fallback ack.
210 	 *
211 	 * This workaround is described in HSDES #1604254524 and it's known as:
212 	 * WaRsForcewakeAddDelayForAck:skl,bxt,kbl,glk,cfl,cnl,icl
213 	 * although the name is a bit misleading.
214 	 */
215 
216 	pass = 1;
217 	do {
218 		wait_ack_clear(d, FORCEWAKE_KERNEL_FALLBACK);
219 
220 		fw_set(d, FORCEWAKE_KERNEL_FALLBACK);
221 		/* Give gt some time to relax before the polling frenzy */
222 		udelay(10 * pass);
223 		wait_ack_set(d, FORCEWAKE_KERNEL_FALLBACK);
224 
225 		ack_detected = (fw_ack(d) & ack_bit) == value;
226 
227 		fw_clear(d, FORCEWAKE_KERNEL_FALLBACK);
228 	} while (!ack_detected && pass++ < 10);
229 
230 	drm_dbg(&d->uncore->i915->drm,
231 		"%s had to use fallback to %s ack, 0x%x (passes %u)\n",
232 		intel_uncore_forcewake_domain_to_str(d->id),
233 		type == ACK_SET ? "set" : "clear",
234 		fw_ack(d),
235 		pass);
236 
237 	return ack_detected ? 0 : -ETIMEDOUT;
238 }
239 
240 static inline void
241 fw_domain_wait_ack_clear_fallback(const struct intel_uncore_forcewake_domain *d)
242 {
243 	if (likely(!wait_ack_clear(d, FORCEWAKE_KERNEL)))
244 		return;
245 
246 	if (fw_domain_wait_ack_with_fallback(d, ACK_CLEAR))
247 		fw_domain_wait_ack_clear(d);
248 }
249 
250 static inline void
251 fw_domain_get(const struct intel_uncore_forcewake_domain *d)
252 {
253 	fw_set(d, FORCEWAKE_KERNEL);
254 }
255 
256 static inline void
257 fw_domain_wait_ack_set(const struct intel_uncore_forcewake_domain *d)
258 {
259 	if (wait_ack_set(d, FORCEWAKE_KERNEL)) {
260 		drm_err(&d->uncore->i915->drm,
261 			"%s: timed out waiting for forcewake ack request.\n",
262 			intel_uncore_forcewake_domain_to_str(d->id));
263 		add_taint_for_CI(d->uncore->i915, TAINT_WARN); /* CI now unreliable */
264 	}
265 }
266 
267 static inline void
268 fw_domain_wait_ack_set_fallback(const struct intel_uncore_forcewake_domain *d)
269 {
270 	if (likely(!wait_ack_set(d, FORCEWAKE_KERNEL)))
271 		return;
272 
273 	if (fw_domain_wait_ack_with_fallback(d, ACK_SET))
274 		fw_domain_wait_ack_set(d);
275 }
276 
277 static inline void
278 fw_domain_put(const struct intel_uncore_forcewake_domain *d)
279 {
280 	fw_clear(d, FORCEWAKE_KERNEL);
281 }
282 
283 static void
284 fw_domains_get_normal(struct intel_uncore *uncore, enum forcewake_domains fw_domains)
285 {
286 	struct intel_uncore_forcewake_domain *d;
287 	unsigned int tmp;
288 
289 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
290 
291 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp) {
292 		fw_domain_wait_ack_clear(d);
293 		fw_domain_get(d);
294 	}
295 
296 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
297 		fw_domain_wait_ack_set(d);
298 
299 	uncore->fw_domains_active |= fw_domains;
300 }
301 
302 static void
303 fw_domains_get_with_fallback(struct intel_uncore *uncore,
304 			     enum forcewake_domains fw_domains)
305 {
306 	struct intel_uncore_forcewake_domain *d;
307 	unsigned int tmp;
308 
309 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
310 
311 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp) {
312 		fw_domain_wait_ack_clear_fallback(d);
313 		fw_domain_get(d);
314 	}
315 
316 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
317 		fw_domain_wait_ack_set_fallback(d);
318 
319 	uncore->fw_domains_active |= fw_domains;
320 }
321 
322 static void
323 fw_domains_put(struct intel_uncore *uncore, enum forcewake_domains fw_domains)
324 {
325 	struct intel_uncore_forcewake_domain *d;
326 	unsigned int tmp;
327 
328 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
329 
330 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
331 		fw_domain_put(d);
332 
333 	uncore->fw_domains_active &= ~fw_domains;
334 }
335 
336 static void
337 fw_domains_reset(struct intel_uncore *uncore,
338 		 enum forcewake_domains fw_domains)
339 {
340 	struct intel_uncore_forcewake_domain *d;
341 	unsigned int tmp;
342 
343 	if (!fw_domains)
344 		return;
345 
346 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
347 
348 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
349 		fw_domain_reset(d);
350 }
351 
352 static inline u32 gt_thread_status(struct intel_uncore *uncore)
353 {
354 	u32 val;
355 
356 	val = __raw_uncore_read32(uncore, GEN6_GT_THREAD_STATUS_REG);
357 	val &= GEN6_GT_THREAD_STATUS_CORE_MASK;
358 
359 	return val;
360 }
361 
362 static void __gen6_gt_wait_for_thread_c0(struct intel_uncore *uncore)
363 {
364 	/*
365 	 * w/a for a sporadic read returning 0 by waiting for the GT
366 	 * thread to wake up.
367 	 */
368 	drm_WARN_ONCE(&uncore->i915->drm,
369 		      wait_for_atomic_us(gt_thread_status(uncore) == 0, 5000),
370 		      "GT thread status wait timed out\n");
371 }
372 
373 static void fw_domains_get_with_thread_status(struct intel_uncore *uncore,
374 					      enum forcewake_domains fw_domains)
375 {
376 	fw_domains_get_normal(uncore, fw_domains);
377 
378 	/* WaRsForcewakeWaitTC0:snb,ivb,hsw,bdw,vlv */
379 	__gen6_gt_wait_for_thread_c0(uncore);
380 }
381 
382 static inline u32 fifo_free_entries(struct intel_uncore *uncore)
383 {
384 	u32 count = __raw_uncore_read32(uncore, GTFIFOCTL);
385 
386 	return count & GT_FIFO_FREE_ENTRIES_MASK;
387 }
388 
389 static void __gen6_gt_wait_for_fifo(struct intel_uncore *uncore)
390 {
391 	u32 n;
392 
393 	/* On VLV, FIFO will be shared by both SW and HW.
394 	 * So, we need to read the FREE_ENTRIES everytime */
395 	if (IS_VALLEYVIEW(uncore->i915))
396 		n = fifo_free_entries(uncore);
397 	else
398 		n = uncore->fifo_count;
399 
400 	if (n <= GT_FIFO_NUM_RESERVED_ENTRIES) {
401 		if (wait_for_atomic((n = fifo_free_entries(uncore)) >
402 				    GT_FIFO_NUM_RESERVED_ENTRIES,
403 				    GT_FIFO_TIMEOUT_MS)) {
404 			drm_dbg(&uncore->i915->drm,
405 				"GT_FIFO timeout, entries: %u\n", n);
406 			return;
407 		}
408 	}
409 
410 	uncore->fifo_count = n - 1;
411 }
412 
413 static enum hrtimer_restart
414 intel_uncore_fw_release_timer(struct hrtimer *timer)
415 {
416 	struct intel_uncore_forcewake_domain *domain =
417 	       container_of(timer, struct intel_uncore_forcewake_domain, timer);
418 	struct intel_uncore *uncore = domain->uncore;
419 	unsigned long irqflags;
420 
421 	assert_rpm_device_not_suspended(uncore->rpm);
422 
423 	if (xchg(&domain->active, false))
424 		return HRTIMER_RESTART;
425 
426 	spin_lock_irqsave(&uncore->lock, irqflags);
427 
428 	uncore->fw_domains_timer &= ~domain->mask;
429 
430 	GEM_BUG_ON(!domain->wake_count);
431 	if (--domain->wake_count == 0)
432 		fw_domains_put(uncore, domain->mask);
433 
434 	spin_unlock_irqrestore(&uncore->lock, irqflags);
435 
436 	return HRTIMER_NORESTART;
437 }
438 
439 /* Note callers must have acquired the PUNIT->PMIC bus, before calling this. */
440 static unsigned int
441 intel_uncore_forcewake_reset(struct intel_uncore *uncore)
442 {
443 	unsigned long irqflags;
444 	struct intel_uncore_forcewake_domain *domain;
445 	int retry_count = 100;
446 	enum forcewake_domains fw, active_domains;
447 
448 	iosf_mbi_assert_punit_acquired();
449 
450 	/* Hold uncore.lock across reset to prevent any register access
451 	 * with forcewake not set correctly. Wait until all pending
452 	 * timers are run before holding.
453 	 */
454 	while (1) {
455 		unsigned int tmp;
456 
457 		active_domains = 0;
458 
459 		for_each_fw_domain(domain, uncore, tmp) {
460 			smp_store_mb(domain->active, false);
461 			if (hrtimer_cancel(&domain->timer) == 0)
462 				continue;
463 
464 			intel_uncore_fw_release_timer(&domain->timer);
465 		}
466 
467 		spin_lock_irqsave(&uncore->lock, irqflags);
468 
469 		for_each_fw_domain(domain, uncore, tmp) {
470 			if (hrtimer_active(&domain->timer))
471 				active_domains |= domain->mask;
472 		}
473 
474 		if (active_domains == 0)
475 			break;
476 
477 		if (--retry_count == 0) {
478 			drm_err(&uncore->i915->drm, "Timed out waiting for forcewake timers to finish\n");
479 			break;
480 		}
481 
482 		spin_unlock_irqrestore(&uncore->lock, irqflags);
483 		cond_resched();
484 	}
485 
486 	drm_WARN_ON(&uncore->i915->drm, active_domains);
487 
488 	fw = uncore->fw_domains_active;
489 	if (fw)
490 		fw_domains_put(uncore, fw);
491 
492 	fw_domains_reset(uncore, uncore->fw_domains);
493 	assert_forcewakes_inactive(uncore);
494 
495 	spin_unlock_irqrestore(&uncore->lock, irqflags);
496 
497 	return fw; /* track the lost user forcewake domains */
498 }
499 
500 static bool
501 fpga_check_for_unclaimed_mmio(struct intel_uncore *uncore)
502 {
503 	u32 dbg;
504 
505 	dbg = __raw_uncore_read32(uncore, FPGA_DBG);
506 	if (likely(!(dbg & FPGA_DBG_RM_NOCLAIM)))
507 		return false;
508 
509 	/*
510 	 * Bugs in PCI programming (or failing hardware) can occasionally cause
511 	 * us to lose access to the MMIO BAR.  When this happens, register
512 	 * reads will come back with 0xFFFFFFFF for every register and things
513 	 * go bad very quickly.  Let's try to detect that special case and at
514 	 * least try to print a more informative message about what has
515 	 * happened.
516 	 *
517 	 * During normal operation the FPGA_DBG register has several unused
518 	 * bits that will always read back as 0's so we can use them as canaries
519 	 * to recognize when MMIO accesses are just busted.
520 	 */
521 	if (unlikely(dbg == ~0))
522 		drm_err(&uncore->i915->drm,
523 			"Lost access to MMIO BAR; all registers now read back as 0xFFFFFFFF!\n");
524 
525 	__raw_uncore_write32(uncore, FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
526 
527 	return true;
528 }
529 
530 static bool
531 vlv_check_for_unclaimed_mmio(struct intel_uncore *uncore)
532 {
533 	u32 cer;
534 
535 	cer = __raw_uncore_read32(uncore, CLAIM_ER);
536 	if (likely(!(cer & (CLAIM_ER_OVERFLOW | CLAIM_ER_CTR_MASK))))
537 		return false;
538 
539 	__raw_uncore_write32(uncore, CLAIM_ER, CLAIM_ER_CLR);
540 
541 	return true;
542 }
543 
544 static bool
545 gen6_check_for_fifo_debug(struct intel_uncore *uncore)
546 {
547 	u32 fifodbg;
548 
549 	fifodbg = __raw_uncore_read32(uncore, GTFIFODBG);
550 
551 	if (unlikely(fifodbg)) {
552 		drm_dbg(&uncore->i915->drm, "GTFIFODBG = 0x08%x\n", fifodbg);
553 		__raw_uncore_write32(uncore, GTFIFODBG, fifodbg);
554 	}
555 
556 	return fifodbg;
557 }
558 
559 static bool
560 check_for_unclaimed_mmio(struct intel_uncore *uncore)
561 {
562 	bool ret = false;
563 
564 	lockdep_assert_held(&uncore->debug->lock);
565 
566 	if (uncore->debug->suspend_count)
567 		return false;
568 
569 	if (intel_uncore_has_fpga_dbg_unclaimed(uncore))
570 		ret |= fpga_check_for_unclaimed_mmio(uncore);
571 
572 	if (intel_uncore_has_dbg_unclaimed(uncore))
573 		ret |= vlv_check_for_unclaimed_mmio(uncore);
574 
575 	if (intel_uncore_has_fifo(uncore))
576 		ret |= gen6_check_for_fifo_debug(uncore);
577 
578 	return ret;
579 }
580 
581 static void forcewake_early_sanitize(struct intel_uncore *uncore,
582 				     unsigned int restore_forcewake)
583 {
584 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
585 
586 	/* WaDisableShadowRegForCpd:chv */
587 	if (IS_CHERRYVIEW(uncore->i915)) {
588 		__raw_uncore_write32(uncore, GTFIFOCTL,
589 				     __raw_uncore_read32(uncore, GTFIFOCTL) |
590 				     GT_FIFO_CTL_BLOCK_ALL_POLICY_STALL |
591 				     GT_FIFO_CTL_RC6_POLICY_STALL);
592 	}
593 
594 	iosf_mbi_punit_acquire();
595 	intel_uncore_forcewake_reset(uncore);
596 	if (restore_forcewake) {
597 		spin_lock_irq(&uncore->lock);
598 		fw_domains_get(uncore, restore_forcewake);
599 
600 		if (intel_uncore_has_fifo(uncore))
601 			uncore->fifo_count = fifo_free_entries(uncore);
602 		spin_unlock_irq(&uncore->lock);
603 	}
604 	iosf_mbi_punit_release();
605 }
606 
607 void intel_uncore_suspend(struct intel_uncore *uncore)
608 {
609 	if (!intel_uncore_has_forcewake(uncore))
610 		return;
611 
612 	iosf_mbi_punit_acquire();
613 	iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
614 		&uncore->pmic_bus_access_nb);
615 	uncore->fw_domains_saved = intel_uncore_forcewake_reset(uncore);
616 	iosf_mbi_punit_release();
617 }
618 
619 void intel_uncore_resume_early(struct intel_uncore *uncore)
620 {
621 	unsigned int restore_forcewake;
622 
623 	if (intel_uncore_unclaimed_mmio(uncore))
624 		drm_dbg(&uncore->i915->drm, "unclaimed mmio detected on resume, clearing\n");
625 
626 	if (!intel_uncore_has_forcewake(uncore))
627 		return;
628 
629 	restore_forcewake = fetch_and_zero(&uncore->fw_domains_saved);
630 	forcewake_early_sanitize(uncore, restore_forcewake);
631 
632 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
633 }
634 
635 void intel_uncore_runtime_resume(struct intel_uncore *uncore)
636 {
637 	if (!intel_uncore_has_forcewake(uncore))
638 		return;
639 
640 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
641 }
642 
643 static void __intel_uncore_forcewake_get(struct intel_uncore *uncore,
644 					 enum forcewake_domains fw_domains)
645 {
646 	struct intel_uncore_forcewake_domain *domain;
647 	unsigned int tmp;
648 
649 	fw_domains &= uncore->fw_domains;
650 
651 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
652 		if (domain->wake_count++) {
653 			fw_domains &= ~domain->mask;
654 			domain->active = true;
655 		}
656 	}
657 
658 	if (fw_domains)
659 		fw_domains_get(uncore, fw_domains);
660 }
661 
662 /**
663  * intel_uncore_forcewake_get - grab forcewake domain references
664  * @uncore: the intel_uncore structure
665  * @fw_domains: forcewake domains to get reference on
666  *
667  * This function can be used get GT's forcewake domain references.
668  * Normal register access will handle the forcewake domains automatically.
669  * However if some sequence requires the GT to not power down a particular
670  * forcewake domains this function should be called at the beginning of the
671  * sequence. And subsequently the reference should be dropped by symmetric
672  * call to intel_unforce_forcewake_put(). Usually caller wants all the domains
673  * to be kept awake so the @fw_domains would be then FORCEWAKE_ALL.
674  */
675 void intel_uncore_forcewake_get(struct intel_uncore *uncore,
676 				enum forcewake_domains fw_domains)
677 {
678 	unsigned long irqflags;
679 
680 	if (!uncore->fw_get_funcs)
681 		return;
682 
683 	assert_rpm_wakelock_held(uncore->rpm);
684 
685 	spin_lock_irqsave(&uncore->lock, irqflags);
686 	__intel_uncore_forcewake_get(uncore, fw_domains);
687 	spin_unlock_irqrestore(&uncore->lock, irqflags);
688 }
689 
690 /**
691  * intel_uncore_forcewake_user_get - claim forcewake on behalf of userspace
692  * @uncore: the intel_uncore structure
693  *
694  * This function is a wrapper around intel_uncore_forcewake_get() to acquire
695  * the GT powerwell and in the process disable our debugging for the
696  * duration of userspace's bypass.
697  */
698 void intel_uncore_forcewake_user_get(struct intel_uncore *uncore)
699 {
700 	spin_lock_irq(&uncore->lock);
701 	if (!uncore->user_forcewake_count++) {
702 		intel_uncore_forcewake_get__locked(uncore, FORCEWAKE_ALL);
703 		mmio_debug_suspend(uncore);
704 	}
705 	spin_unlock_irq(&uncore->lock);
706 }
707 
708 /**
709  * intel_uncore_forcewake_user_put - release forcewake on behalf of userspace
710  * @uncore: the intel_uncore structure
711  *
712  * This function complements intel_uncore_forcewake_user_get() and releases
713  * the GT powerwell taken on behalf of the userspace bypass.
714  */
715 void intel_uncore_forcewake_user_put(struct intel_uncore *uncore)
716 {
717 	spin_lock_irq(&uncore->lock);
718 	if (!--uncore->user_forcewake_count) {
719 		mmio_debug_resume(uncore);
720 		intel_uncore_forcewake_put__locked(uncore, FORCEWAKE_ALL);
721 	}
722 	spin_unlock_irq(&uncore->lock);
723 }
724 
725 /**
726  * intel_uncore_forcewake_get__locked - grab forcewake domain references
727  * @uncore: the intel_uncore structure
728  * @fw_domains: forcewake domains to get reference on
729  *
730  * See intel_uncore_forcewake_get(). This variant places the onus
731  * on the caller to explicitly handle the dev_priv->uncore.lock spinlock.
732  */
733 void intel_uncore_forcewake_get__locked(struct intel_uncore *uncore,
734 					enum forcewake_domains fw_domains)
735 {
736 	lockdep_assert_held(&uncore->lock);
737 
738 	if (!uncore->fw_get_funcs)
739 		return;
740 
741 	__intel_uncore_forcewake_get(uncore, fw_domains);
742 }
743 
744 static void __intel_uncore_forcewake_put(struct intel_uncore *uncore,
745 					 enum forcewake_domains fw_domains,
746 					 bool delayed)
747 {
748 	struct intel_uncore_forcewake_domain *domain;
749 	unsigned int tmp;
750 
751 	fw_domains &= uncore->fw_domains;
752 
753 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
754 		GEM_BUG_ON(!domain->wake_count);
755 
756 		if (--domain->wake_count) {
757 			domain->active = true;
758 			continue;
759 		}
760 
761 		if (delayed &&
762 		    !(domain->uncore->fw_domains_timer & domain->mask))
763 			fw_domain_arm_timer(domain);
764 		else
765 			fw_domains_put(uncore, domain->mask);
766 	}
767 }
768 
769 /**
770  * intel_uncore_forcewake_put - release a forcewake domain reference
771  * @uncore: the intel_uncore structure
772  * @fw_domains: forcewake domains to put references
773  *
774  * This function drops the device-level forcewakes for specified
775  * domains obtained by intel_uncore_forcewake_get().
776  */
777 void intel_uncore_forcewake_put(struct intel_uncore *uncore,
778 				enum forcewake_domains fw_domains)
779 {
780 	unsigned long irqflags;
781 
782 	if (!uncore->fw_get_funcs)
783 		return;
784 
785 	spin_lock_irqsave(&uncore->lock, irqflags);
786 	__intel_uncore_forcewake_put(uncore, fw_domains, false);
787 	spin_unlock_irqrestore(&uncore->lock, irqflags);
788 }
789 
790 void intel_uncore_forcewake_put_delayed(struct intel_uncore *uncore,
791 					enum forcewake_domains fw_domains)
792 {
793 	unsigned long irqflags;
794 
795 	if (!uncore->fw_get_funcs)
796 		return;
797 
798 	spin_lock_irqsave(&uncore->lock, irqflags);
799 	__intel_uncore_forcewake_put(uncore, fw_domains, true);
800 	spin_unlock_irqrestore(&uncore->lock, irqflags);
801 }
802 
803 /**
804  * intel_uncore_forcewake_flush - flush the delayed release
805  * @uncore: the intel_uncore structure
806  * @fw_domains: forcewake domains to flush
807  */
808 void intel_uncore_forcewake_flush(struct intel_uncore *uncore,
809 				  enum forcewake_domains fw_domains)
810 {
811 	struct intel_uncore_forcewake_domain *domain;
812 	unsigned int tmp;
813 
814 	if (!uncore->fw_get_funcs)
815 		return;
816 
817 	fw_domains &= uncore->fw_domains;
818 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
819 		WRITE_ONCE(domain->active, false);
820 		if (hrtimer_cancel(&domain->timer))
821 			intel_uncore_fw_release_timer(&domain->timer);
822 	}
823 }
824 
825 /**
826  * intel_uncore_forcewake_put__locked - grab forcewake domain references
827  * @uncore: the intel_uncore structure
828  * @fw_domains: forcewake domains to get reference on
829  *
830  * See intel_uncore_forcewake_put(). This variant places the onus
831  * on the caller to explicitly handle the dev_priv->uncore.lock spinlock.
832  */
833 void intel_uncore_forcewake_put__locked(struct intel_uncore *uncore,
834 					enum forcewake_domains fw_domains)
835 {
836 	lockdep_assert_held(&uncore->lock);
837 
838 	if (!uncore->fw_get_funcs)
839 		return;
840 
841 	__intel_uncore_forcewake_put(uncore, fw_domains, false);
842 }
843 
844 void assert_forcewakes_inactive(struct intel_uncore *uncore)
845 {
846 	if (!uncore->fw_get_funcs)
847 		return;
848 
849 	drm_WARN(&uncore->i915->drm, uncore->fw_domains_active,
850 		 "Expected all fw_domains to be inactive, but %08x are still on\n",
851 		 uncore->fw_domains_active);
852 }
853 
854 void assert_forcewakes_active(struct intel_uncore *uncore,
855 			      enum forcewake_domains fw_domains)
856 {
857 	struct intel_uncore_forcewake_domain *domain;
858 	unsigned int tmp;
859 
860 	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
861 		return;
862 
863 	if (!uncore->fw_get_funcs)
864 		return;
865 
866 	spin_lock_irq(&uncore->lock);
867 
868 	assert_rpm_wakelock_held(uncore->rpm);
869 
870 	fw_domains &= uncore->fw_domains;
871 	drm_WARN(&uncore->i915->drm, fw_domains & ~uncore->fw_domains_active,
872 		 "Expected %08x fw_domains to be active, but %08x are off\n",
873 		 fw_domains, fw_domains & ~uncore->fw_domains_active);
874 
875 	/*
876 	 * Check that the caller has an explicit wakeref and we don't mistake
877 	 * it for the auto wakeref.
878 	 */
879 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
880 		unsigned int actual = READ_ONCE(domain->wake_count);
881 		unsigned int expect = 1;
882 
883 		if (uncore->fw_domains_timer & domain->mask)
884 			expect++; /* pending automatic release */
885 
886 		if (drm_WARN(&uncore->i915->drm, actual < expect,
887 			     "Expected domain %d to be held awake by caller, count=%d\n",
888 			     domain->id, actual))
889 			break;
890 	}
891 
892 	spin_unlock_irq(&uncore->lock);
893 }
894 
895 /*
896  * We give fast paths for the really cool registers.  The second range includes
897  * media domains (and the GSC starting from Xe_LPM+)
898  */
899 #define NEEDS_FORCE_WAKE(reg) ({ \
900 	u32 __reg = (reg); \
901 	__reg < 0x40000 || __reg >= 0x116000; \
902 })
903 
904 static int fw_range_cmp(u32 offset, const struct intel_forcewake_range *entry)
905 {
906 	if (offset < entry->start)
907 		return -1;
908 	else if (offset > entry->end)
909 		return 1;
910 	else
911 		return 0;
912 }
913 
914 /* Copied and "macroized" from lib/bsearch.c */
915 #define BSEARCH(key, base, num, cmp) ({                                 \
916 	unsigned int start__ = 0, end__ = (num);                        \
917 	typeof(base) result__ = NULL;                                   \
918 	while (start__ < end__) {                                       \
919 		unsigned int mid__ = start__ + (end__ - start__) / 2;   \
920 		int ret__ = (cmp)((key), (base) + mid__);               \
921 		if (ret__ < 0) {                                        \
922 			end__ = mid__;                                  \
923 		} else if (ret__ > 0) {                                 \
924 			start__ = mid__ + 1;                            \
925 		} else {                                                \
926 			result__ = (base) + mid__;                      \
927 			break;                                          \
928 		}                                                       \
929 	}                                                               \
930 	result__;                                                       \
931 })
932 
933 static enum forcewake_domains
934 find_fw_domain(struct intel_uncore *uncore, u32 offset)
935 {
936 	const struct intel_forcewake_range *entry;
937 
938 	if (IS_GSI_REG(offset))
939 		offset += uncore->gsi_offset;
940 
941 	entry = BSEARCH(offset,
942 			uncore->fw_domains_table,
943 			uncore->fw_domains_table_entries,
944 			fw_range_cmp);
945 
946 	if (!entry)
947 		return 0;
948 
949 	/*
950 	 * The list of FW domains depends on the SKU in gen11+ so we
951 	 * can't determine it statically. We use FORCEWAKE_ALL and
952 	 * translate it here to the list of available domains.
953 	 */
954 	if (entry->domains == FORCEWAKE_ALL)
955 		return uncore->fw_domains;
956 
957 	drm_WARN(&uncore->i915->drm, entry->domains & ~uncore->fw_domains,
958 		 "Uninitialized forcewake domain(s) 0x%x accessed at 0x%x\n",
959 		 entry->domains & ~uncore->fw_domains, offset);
960 
961 	return entry->domains;
962 }
963 
964 /*
965  * Shadowed register tables describe special register ranges that i915 is
966  * allowed to write to without acquiring forcewake.  If these registers' power
967  * wells are down, the hardware will save values written by i915 to a shadow
968  * copy and automatically transfer them into the real register the next time
969  * the power well is woken up.  Shadowing only applies to writes; forcewake
970  * must still be acquired when reading from registers in these ranges.
971  *
972  * The documentation for shadowed registers is somewhat spotty on older
973  * platforms.  However missing registers from these lists is non-fatal; it just
974  * means we'll wake up the hardware for some register accesses where we didn't
975  * really need to.
976  *
977  * The ranges listed in these tables must be sorted by offset.
978  *
979  * When adding new tables here, please also add them to
980  * intel_shadow_table_check() in selftests/intel_uncore.c so that they will be
981  * scanned for obvious mistakes or typos by the selftests.
982  */
983 
984 static const struct i915_range gen8_shadowed_regs[] = {
985 	{ .start =  0x2030, .end =  0x2030 },
986 	{ .start =  0xA008, .end =  0xA00C },
987 	{ .start = 0x12030, .end = 0x12030 },
988 	{ .start = 0x1a030, .end = 0x1a030 },
989 	{ .start = 0x22030, .end = 0x22030 },
990 };
991 
992 static const struct i915_range gen11_shadowed_regs[] = {
993 	{ .start =   0x2030, .end =   0x2030 },
994 	{ .start =   0x2550, .end =   0x2550 },
995 	{ .start =   0xA008, .end =   0xA00C },
996 	{ .start =  0x22030, .end =  0x22030 },
997 	{ .start =  0x22230, .end =  0x22230 },
998 	{ .start =  0x22510, .end =  0x22550 },
999 	{ .start = 0x1C0030, .end = 0x1C0030 },
1000 	{ .start = 0x1C0230, .end = 0x1C0230 },
1001 	{ .start = 0x1C0510, .end = 0x1C0550 },
1002 	{ .start = 0x1C4030, .end = 0x1C4030 },
1003 	{ .start = 0x1C4230, .end = 0x1C4230 },
1004 	{ .start = 0x1C4510, .end = 0x1C4550 },
1005 	{ .start = 0x1C8030, .end = 0x1C8030 },
1006 	{ .start = 0x1C8230, .end = 0x1C8230 },
1007 	{ .start = 0x1C8510, .end = 0x1C8550 },
1008 	{ .start = 0x1D0030, .end = 0x1D0030 },
1009 	{ .start = 0x1D0230, .end = 0x1D0230 },
1010 	{ .start = 0x1D0510, .end = 0x1D0550 },
1011 	{ .start = 0x1D4030, .end = 0x1D4030 },
1012 	{ .start = 0x1D4230, .end = 0x1D4230 },
1013 	{ .start = 0x1D4510, .end = 0x1D4550 },
1014 	{ .start = 0x1D8030, .end = 0x1D8030 },
1015 	{ .start = 0x1D8230, .end = 0x1D8230 },
1016 	{ .start = 0x1D8510, .end = 0x1D8550 },
1017 };
1018 
1019 static const struct i915_range gen12_shadowed_regs[] = {
1020 	{ .start =   0x2030, .end =   0x2030 },
1021 	{ .start =   0x2510, .end =   0x2550 },
1022 	{ .start =   0xA008, .end =   0xA00C },
1023 	{ .start =   0xA188, .end =   0xA188 },
1024 	{ .start =   0xA278, .end =   0xA278 },
1025 	{ .start =   0xA540, .end =   0xA56C },
1026 	{ .start =   0xC4C8, .end =   0xC4C8 },
1027 	{ .start =   0xC4D4, .end =   0xC4D4 },
1028 	{ .start =   0xC600, .end =   0xC600 },
1029 	{ .start =  0x22030, .end =  0x22030 },
1030 	{ .start =  0x22510, .end =  0x22550 },
1031 	{ .start = 0x1C0030, .end = 0x1C0030 },
1032 	{ .start = 0x1C0510, .end = 0x1C0550 },
1033 	{ .start = 0x1C4030, .end = 0x1C4030 },
1034 	{ .start = 0x1C4510, .end = 0x1C4550 },
1035 	{ .start = 0x1C8030, .end = 0x1C8030 },
1036 	{ .start = 0x1C8510, .end = 0x1C8550 },
1037 	{ .start = 0x1D0030, .end = 0x1D0030 },
1038 	{ .start = 0x1D0510, .end = 0x1D0550 },
1039 	{ .start = 0x1D4030, .end = 0x1D4030 },
1040 	{ .start = 0x1D4510, .end = 0x1D4550 },
1041 	{ .start = 0x1D8030, .end = 0x1D8030 },
1042 	{ .start = 0x1D8510, .end = 0x1D8550 },
1043 
1044 	/*
1045 	 * The rest of these ranges are specific to Xe_HP and beyond, but
1046 	 * are reserved/unused ranges on earlier gen12 platforms, so they can
1047 	 * be safely added to the gen12 table.
1048 	 */
1049 	{ .start = 0x1E0030, .end = 0x1E0030 },
1050 	{ .start = 0x1E0510, .end = 0x1E0550 },
1051 	{ .start = 0x1E4030, .end = 0x1E4030 },
1052 	{ .start = 0x1E4510, .end = 0x1E4550 },
1053 	{ .start = 0x1E8030, .end = 0x1E8030 },
1054 	{ .start = 0x1E8510, .end = 0x1E8550 },
1055 	{ .start = 0x1F0030, .end = 0x1F0030 },
1056 	{ .start = 0x1F0510, .end = 0x1F0550 },
1057 	{ .start = 0x1F4030, .end = 0x1F4030 },
1058 	{ .start = 0x1F4510, .end = 0x1F4550 },
1059 	{ .start = 0x1F8030, .end = 0x1F8030 },
1060 	{ .start = 0x1F8510, .end = 0x1F8550 },
1061 };
1062 
1063 static const struct i915_range dg2_shadowed_regs[] = {
1064 	{ .start =   0x2030, .end =   0x2030 },
1065 	{ .start =   0x2510, .end =   0x2550 },
1066 	{ .start =   0xA008, .end =   0xA00C },
1067 	{ .start =   0xA188, .end =   0xA188 },
1068 	{ .start =   0xA278, .end =   0xA278 },
1069 	{ .start =   0xA540, .end =   0xA56C },
1070 	{ .start =   0xC4C8, .end =   0xC4C8 },
1071 	{ .start =   0xC4E0, .end =   0xC4E0 },
1072 	{ .start =   0xC600, .end =   0xC600 },
1073 	{ .start =   0xC658, .end =   0xC658 },
1074 	{ .start =  0x22030, .end =  0x22030 },
1075 	{ .start =  0x22510, .end =  0x22550 },
1076 	{ .start = 0x1C0030, .end = 0x1C0030 },
1077 	{ .start = 0x1C0510, .end = 0x1C0550 },
1078 	{ .start = 0x1C4030, .end = 0x1C4030 },
1079 	{ .start = 0x1C4510, .end = 0x1C4550 },
1080 	{ .start = 0x1C8030, .end = 0x1C8030 },
1081 	{ .start = 0x1C8510, .end = 0x1C8550 },
1082 	{ .start = 0x1D0030, .end = 0x1D0030 },
1083 	{ .start = 0x1D0510, .end = 0x1D0550 },
1084 	{ .start = 0x1D4030, .end = 0x1D4030 },
1085 	{ .start = 0x1D4510, .end = 0x1D4550 },
1086 	{ .start = 0x1D8030, .end = 0x1D8030 },
1087 	{ .start = 0x1D8510, .end = 0x1D8550 },
1088 	{ .start = 0x1E0030, .end = 0x1E0030 },
1089 	{ .start = 0x1E0510, .end = 0x1E0550 },
1090 	{ .start = 0x1E4030, .end = 0x1E4030 },
1091 	{ .start = 0x1E4510, .end = 0x1E4550 },
1092 	{ .start = 0x1E8030, .end = 0x1E8030 },
1093 	{ .start = 0x1E8510, .end = 0x1E8550 },
1094 	{ .start = 0x1F0030, .end = 0x1F0030 },
1095 	{ .start = 0x1F0510, .end = 0x1F0550 },
1096 	{ .start = 0x1F4030, .end = 0x1F4030 },
1097 	{ .start = 0x1F4510, .end = 0x1F4550 },
1098 	{ .start = 0x1F8030, .end = 0x1F8030 },
1099 	{ .start = 0x1F8510, .end = 0x1F8550 },
1100 };
1101 
1102 static const struct i915_range pvc_shadowed_regs[] = {
1103 	{ .start =   0x2030, .end =   0x2030 },
1104 	{ .start =   0x2510, .end =   0x2550 },
1105 	{ .start =   0xA008, .end =   0xA00C },
1106 	{ .start =   0xA188, .end =   0xA188 },
1107 	{ .start =   0xA278, .end =   0xA278 },
1108 	{ .start =   0xA540, .end =   0xA56C },
1109 	{ .start =   0xC4C8, .end =   0xC4C8 },
1110 	{ .start =   0xC4E0, .end =   0xC4E0 },
1111 	{ .start =   0xC600, .end =   0xC600 },
1112 	{ .start =   0xC658, .end =   0xC658 },
1113 	{ .start =  0x22030, .end =  0x22030 },
1114 	{ .start =  0x22510, .end =  0x22550 },
1115 	{ .start = 0x1C0030, .end = 0x1C0030 },
1116 	{ .start = 0x1C0510, .end = 0x1C0550 },
1117 	{ .start = 0x1C4030, .end = 0x1C4030 },
1118 	{ .start = 0x1C4510, .end = 0x1C4550 },
1119 	{ .start = 0x1C8030, .end = 0x1C8030 },
1120 	{ .start = 0x1C8510, .end = 0x1C8550 },
1121 	{ .start = 0x1D0030, .end = 0x1D0030 },
1122 	{ .start = 0x1D0510, .end = 0x1D0550 },
1123 	{ .start = 0x1D4030, .end = 0x1D4030 },
1124 	{ .start = 0x1D4510, .end = 0x1D4550 },
1125 	{ .start = 0x1D8030, .end = 0x1D8030 },
1126 	{ .start = 0x1D8510, .end = 0x1D8550 },
1127 	{ .start = 0x1E0030, .end = 0x1E0030 },
1128 	{ .start = 0x1E0510, .end = 0x1E0550 },
1129 	{ .start = 0x1E4030, .end = 0x1E4030 },
1130 	{ .start = 0x1E4510, .end = 0x1E4550 },
1131 	{ .start = 0x1E8030, .end = 0x1E8030 },
1132 	{ .start = 0x1E8510, .end = 0x1E8550 },
1133 	{ .start = 0x1F0030, .end = 0x1F0030 },
1134 	{ .start = 0x1F0510, .end = 0x1F0550 },
1135 	{ .start = 0x1F4030, .end = 0x1F4030 },
1136 	{ .start = 0x1F4510, .end = 0x1F4550 },
1137 	{ .start = 0x1F8030, .end = 0x1F8030 },
1138 	{ .start = 0x1F8510, .end = 0x1F8550 },
1139 };
1140 
1141 static const struct i915_range mtl_shadowed_regs[] = {
1142 	{ .start =   0x2030, .end =   0x2030 },
1143 	{ .start =   0x2510, .end =   0x2550 },
1144 	{ .start =   0xA008, .end =   0xA00C },
1145 	{ .start =   0xA188, .end =   0xA188 },
1146 	{ .start =   0xA278, .end =   0xA278 },
1147 	{ .start =   0xA540, .end =   0xA56C },
1148 	{ .start =   0xC050, .end =   0xC050 },
1149 	{ .start =   0xC340, .end =   0xC340 },
1150 	{ .start =   0xC4C8, .end =   0xC4C8 },
1151 	{ .start =   0xC4E0, .end =   0xC4E0 },
1152 	{ .start =   0xC600, .end =   0xC600 },
1153 	{ .start =   0xC658, .end =   0xC658 },
1154 	{ .start =   0xCFD4, .end =   0xCFDC },
1155 	{ .start =  0x22030, .end =  0x22030 },
1156 	{ .start =  0x22510, .end =  0x22550 },
1157 };
1158 
1159 static const struct i915_range xelpmp_shadowed_regs[] = {
1160 	{ .start = 0x1C0030, .end = 0x1C0030 },
1161 	{ .start = 0x1C0510, .end = 0x1C0550 },
1162 	{ .start = 0x1C8030, .end = 0x1C8030 },
1163 	{ .start = 0x1C8510, .end = 0x1C8550 },
1164 	{ .start = 0x1D0030, .end = 0x1D0030 },
1165 	{ .start = 0x1D0510, .end = 0x1D0550 },
1166 	{ .start = 0x38A008, .end = 0x38A00C },
1167 	{ .start = 0x38A188, .end = 0x38A188 },
1168 	{ .start = 0x38A278, .end = 0x38A278 },
1169 	{ .start = 0x38A540, .end = 0x38A56C },
1170 	{ .start = 0x38A618, .end = 0x38A618 },
1171 	{ .start = 0x38C050, .end = 0x38C050 },
1172 	{ .start = 0x38C340, .end = 0x38C340 },
1173 	{ .start = 0x38C4C8, .end = 0x38C4C8 },
1174 	{ .start = 0x38C4E0, .end = 0x38C4E4 },
1175 	{ .start = 0x38C600, .end = 0x38C600 },
1176 	{ .start = 0x38C658, .end = 0x38C658 },
1177 	{ .start = 0x38CFD4, .end = 0x38CFDC },
1178 };
1179 
1180 static int mmio_range_cmp(u32 key, const struct i915_range *range)
1181 {
1182 	if (key < range->start)
1183 		return -1;
1184 	else if (key > range->end)
1185 		return 1;
1186 	else
1187 		return 0;
1188 }
1189 
1190 static bool is_shadowed(struct intel_uncore *uncore, u32 offset)
1191 {
1192 	if (drm_WARN_ON(&uncore->i915->drm, !uncore->shadowed_reg_table))
1193 		return false;
1194 
1195 	if (IS_GSI_REG(offset))
1196 		offset += uncore->gsi_offset;
1197 
1198 	return BSEARCH(offset,
1199 		       uncore->shadowed_reg_table,
1200 		       uncore->shadowed_reg_table_entries,
1201 		       mmio_range_cmp);
1202 }
1203 
1204 static enum forcewake_domains
1205 gen6_reg_write_fw_domains(struct intel_uncore *uncore, i915_reg_t reg)
1206 {
1207 	return FORCEWAKE_RENDER;
1208 }
1209 
1210 #define __fwtable_reg_read_fw_domains(uncore, offset) \
1211 ({ \
1212 	enum forcewake_domains __fwd = 0; \
1213 	if (NEEDS_FORCE_WAKE((offset))) \
1214 		__fwd = find_fw_domain(uncore, offset); \
1215 	__fwd; \
1216 })
1217 
1218 #define __fwtable_reg_write_fw_domains(uncore, offset) \
1219 ({ \
1220 	enum forcewake_domains __fwd = 0; \
1221 	const u32 __offset = (offset); \
1222 	if (NEEDS_FORCE_WAKE((__offset)) && !is_shadowed(uncore, __offset)) \
1223 		__fwd = find_fw_domain(uncore, __offset); \
1224 	__fwd; \
1225 })
1226 
1227 #define GEN_FW_RANGE(s, e, d) \
1228 	{ .start = (s), .end = (e), .domains = (d) }
1229 
1230 /*
1231  * All platforms' forcewake tables below must be sorted by offset ranges.
1232  * Furthermore, new forcewake tables added should be "watertight" and have
1233  * no gaps between ranges.
1234  *
1235  * When there are multiple consecutive ranges listed in the bspec with
1236  * the same forcewake domain, it is customary to combine them into a single
1237  * row in the tables below to keep the tables small and lookups fast.
1238  * Likewise, reserved/unused ranges may be combined with the preceding and/or
1239  * following ranges since the driver will never be making MMIO accesses in
1240  * those ranges.
1241  *
1242  * For example, if the bspec were to list:
1243  *
1244  *    ...
1245  *    0x1000 - 0x1fff:  GT
1246  *    0x2000 - 0x2cff:  GT
1247  *    0x2d00 - 0x2fff:  unused/reserved
1248  *    0x3000 - 0xffff:  GT
1249  *    ...
1250  *
1251  * these could all be represented by a single line in the code:
1252  *
1253  *   GEN_FW_RANGE(0x1000, 0xffff, FORCEWAKE_GT)
1254  *
1255  * When adding new forcewake tables here, please also add them to
1256  * intel_uncore_mock_selftests in selftests/intel_uncore.c so that they will be
1257  * scanned for obvious mistakes or typos by the selftests.
1258  */
1259 
1260 static const struct intel_forcewake_range __gen6_fw_ranges[] = {
1261 	GEN_FW_RANGE(0x0, 0x3ffff, FORCEWAKE_RENDER),
1262 };
1263 
1264 static const struct intel_forcewake_range __vlv_fw_ranges[] = {
1265 	GEN_FW_RANGE(0x2000, 0x3fff, FORCEWAKE_RENDER),
1266 	GEN_FW_RANGE(0x5000, 0x7fff, FORCEWAKE_RENDER),
1267 	GEN_FW_RANGE(0xb000, 0x11fff, FORCEWAKE_RENDER),
1268 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
1269 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_MEDIA),
1270 	GEN_FW_RANGE(0x2e000, 0x2ffff, FORCEWAKE_RENDER),
1271 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_MEDIA),
1272 };
1273 
1274 static const struct intel_forcewake_range __chv_fw_ranges[] = {
1275 	GEN_FW_RANGE(0x2000, 0x3fff, FORCEWAKE_RENDER),
1276 	GEN_FW_RANGE(0x4000, 0x4fff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1277 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1278 	GEN_FW_RANGE(0x8000, 0x82ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1279 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1280 	GEN_FW_RANGE(0x8500, 0x85ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1281 	GEN_FW_RANGE(0x8800, 0x88ff, FORCEWAKE_MEDIA),
1282 	GEN_FW_RANGE(0x9000, 0xafff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1283 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1284 	GEN_FW_RANGE(0xd000, 0xd7ff, FORCEWAKE_MEDIA),
1285 	GEN_FW_RANGE(0xe000, 0xe7ff, FORCEWAKE_RENDER),
1286 	GEN_FW_RANGE(0xf000, 0xffff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1287 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
1288 	GEN_FW_RANGE(0x1a000, 0x1bfff, FORCEWAKE_MEDIA),
1289 	GEN_FW_RANGE(0x1e800, 0x1e9ff, FORCEWAKE_MEDIA),
1290 	GEN_FW_RANGE(0x30000, 0x37fff, FORCEWAKE_MEDIA),
1291 };
1292 
1293 static const struct intel_forcewake_range __gen9_fw_ranges[] = {
1294 	GEN_FW_RANGE(0x0, 0xaff, FORCEWAKE_GT),
1295 	GEN_FW_RANGE(0xb00, 0x1fff, 0), /* uncore range */
1296 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1297 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_GT),
1298 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1299 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_GT),
1300 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1301 	GEN_FW_RANGE(0x8000, 0x812f, FORCEWAKE_GT),
1302 	GEN_FW_RANGE(0x8130, 0x813f, FORCEWAKE_MEDIA),
1303 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1304 	GEN_FW_RANGE(0x8160, 0x82ff, FORCEWAKE_GT),
1305 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1306 	GEN_FW_RANGE(0x8500, 0x87ff, FORCEWAKE_GT),
1307 	GEN_FW_RANGE(0x8800, 0x89ff, FORCEWAKE_MEDIA),
1308 	GEN_FW_RANGE(0x8a00, 0x8bff, FORCEWAKE_GT),
1309 	GEN_FW_RANGE(0x8c00, 0x8cff, FORCEWAKE_RENDER),
1310 	GEN_FW_RANGE(0x8d00, 0x93ff, FORCEWAKE_GT),
1311 	GEN_FW_RANGE(0x9400, 0x97ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1312 	GEN_FW_RANGE(0x9800, 0xafff, FORCEWAKE_GT),
1313 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1314 	GEN_FW_RANGE(0xb480, 0xcfff, FORCEWAKE_GT),
1315 	GEN_FW_RANGE(0xd000, 0xd7ff, FORCEWAKE_MEDIA),
1316 	GEN_FW_RANGE(0xd800, 0xdfff, FORCEWAKE_GT),
1317 	GEN_FW_RANGE(0xe000, 0xe8ff, FORCEWAKE_RENDER),
1318 	GEN_FW_RANGE(0xe900, 0x11fff, FORCEWAKE_GT),
1319 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
1320 	GEN_FW_RANGE(0x14000, 0x19fff, FORCEWAKE_GT),
1321 	GEN_FW_RANGE(0x1a000, 0x1e9ff, FORCEWAKE_MEDIA),
1322 	GEN_FW_RANGE(0x1ea00, 0x243ff, FORCEWAKE_GT),
1323 	GEN_FW_RANGE(0x24400, 0x247ff, FORCEWAKE_RENDER),
1324 	GEN_FW_RANGE(0x24800, 0x2ffff, FORCEWAKE_GT),
1325 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_MEDIA),
1326 };
1327 
1328 static const struct intel_forcewake_range __gen11_fw_ranges[] = {
1329 	GEN_FW_RANGE(0x0, 0x1fff, 0), /* uncore range */
1330 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1331 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_GT),
1332 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1333 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_GT),
1334 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1335 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_GT),
1336 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1337 	GEN_FW_RANGE(0x8160, 0x82ff, FORCEWAKE_GT),
1338 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1339 	GEN_FW_RANGE(0x8500, 0x87ff, FORCEWAKE_GT),
1340 	GEN_FW_RANGE(0x8800, 0x8bff, 0),
1341 	GEN_FW_RANGE(0x8c00, 0x8cff, FORCEWAKE_RENDER),
1342 	GEN_FW_RANGE(0x8d00, 0x94cf, FORCEWAKE_GT),
1343 	GEN_FW_RANGE(0x94d0, 0x955f, FORCEWAKE_RENDER),
1344 	GEN_FW_RANGE(0x9560, 0x95ff, 0),
1345 	GEN_FW_RANGE(0x9600, 0xafff, FORCEWAKE_GT),
1346 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1347 	GEN_FW_RANGE(0xb480, 0xdeff, FORCEWAKE_GT),
1348 	GEN_FW_RANGE(0xdf00, 0xe8ff, FORCEWAKE_RENDER),
1349 	GEN_FW_RANGE(0xe900, 0x16dff, FORCEWAKE_GT),
1350 	GEN_FW_RANGE(0x16e00, 0x19fff, FORCEWAKE_RENDER),
1351 	GEN_FW_RANGE(0x1a000, 0x23fff, FORCEWAKE_GT),
1352 	GEN_FW_RANGE(0x24000, 0x2407f, 0),
1353 	GEN_FW_RANGE(0x24080, 0x2417f, FORCEWAKE_GT),
1354 	GEN_FW_RANGE(0x24180, 0x242ff, FORCEWAKE_RENDER),
1355 	GEN_FW_RANGE(0x24300, 0x243ff, FORCEWAKE_GT),
1356 	GEN_FW_RANGE(0x24400, 0x24fff, FORCEWAKE_RENDER),
1357 	GEN_FW_RANGE(0x25000, 0x3ffff, FORCEWAKE_GT),
1358 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),
1359 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0),
1360 	GEN_FW_RANGE(0x1c4000, 0x1c7fff, 0),
1361 	GEN_FW_RANGE(0x1c8000, 0x1cffff, FORCEWAKE_MEDIA_VEBOX0),
1362 	GEN_FW_RANGE(0x1d0000, 0x1d3fff, FORCEWAKE_MEDIA_VDBOX2),
1363 	GEN_FW_RANGE(0x1d4000, 0x1dbfff, 0)
1364 };
1365 
1366 static const struct intel_forcewake_range __gen12_fw_ranges[] = {
1367 	GEN_FW_RANGE(0x0, 0x1fff, 0), /*
1368 		0x0   -  0xaff: reserved
1369 		0xb00 - 0x1fff: always on */
1370 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1371 	GEN_FW_RANGE(0x2700, 0x27ff, FORCEWAKE_GT),
1372 	GEN_FW_RANGE(0x2800, 0x2aff, FORCEWAKE_RENDER),
1373 	GEN_FW_RANGE(0x2b00, 0x2fff, FORCEWAKE_GT),
1374 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1375 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_GT), /*
1376 		0x4000 - 0x48ff: gt
1377 		0x4900 - 0x51ff: reserved */
1378 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER), /*
1379 		0x5200 - 0x53ff: render
1380 		0x5400 - 0x54ff: reserved
1381 		0x5500 - 0x7fff: render */
1382 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_GT),
1383 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1384 	GEN_FW_RANGE(0x8160, 0x81ff, 0), /*
1385 		0x8160 - 0x817f: reserved
1386 		0x8180 - 0x81ff: always on */
1387 	GEN_FW_RANGE(0x8200, 0x82ff, FORCEWAKE_GT),
1388 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1389 	GEN_FW_RANGE(0x8500, 0x94cf, FORCEWAKE_GT), /*
1390 		0x8500 - 0x87ff: gt
1391 		0x8800 - 0x8fff: reserved
1392 		0x9000 - 0x947f: gt
1393 		0x9480 - 0x94cf: reserved */
1394 	GEN_FW_RANGE(0x94d0, 0x955f, FORCEWAKE_RENDER),
1395 	GEN_FW_RANGE(0x9560, 0x97ff, 0), /*
1396 		0x9560 - 0x95ff: always on
1397 		0x9600 - 0x97ff: reserved */
1398 	GEN_FW_RANGE(0x9800, 0xafff, FORCEWAKE_GT),
1399 	GEN_FW_RANGE(0xb000, 0xb3ff, FORCEWAKE_RENDER),
1400 	GEN_FW_RANGE(0xb400, 0xcfff, FORCEWAKE_GT), /*
1401 		0xb400 - 0xbf7f: gt
1402 		0xb480 - 0xbfff: reserved
1403 		0xc000 - 0xcfff: gt */
1404 	GEN_FW_RANGE(0xd000, 0xd7ff, 0),
1405 	GEN_FW_RANGE(0xd800, 0xd8ff, FORCEWAKE_RENDER),
1406 	GEN_FW_RANGE(0xd900, 0xdbff, FORCEWAKE_GT),
1407 	GEN_FW_RANGE(0xdc00, 0xefff, FORCEWAKE_RENDER), /*
1408 		0xdc00 - 0xddff: render
1409 		0xde00 - 0xde7f: reserved
1410 		0xde80 - 0xe8ff: render
1411 		0xe900 - 0xefff: reserved */
1412 	GEN_FW_RANGE(0xf000, 0x147ff, FORCEWAKE_GT), /*
1413 		 0xf000 - 0xffff: gt
1414 		0x10000 - 0x147ff: reserved */
1415 	GEN_FW_RANGE(0x14800, 0x1ffff, FORCEWAKE_RENDER), /*
1416 		0x14800 - 0x14fff: render
1417 		0x15000 - 0x16dff: reserved
1418 		0x16e00 - 0x1bfff: render
1419 		0x1c000 - 0x1ffff: reserved */
1420 	GEN_FW_RANGE(0x20000, 0x20fff, FORCEWAKE_MEDIA_VDBOX0),
1421 	GEN_FW_RANGE(0x21000, 0x21fff, FORCEWAKE_MEDIA_VDBOX2),
1422 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_GT),
1423 	GEN_FW_RANGE(0x24000, 0x2417f, 0), /*
1424 		0x24000 - 0x2407f: always on
1425 		0x24080 - 0x2417f: reserved */
1426 	GEN_FW_RANGE(0x24180, 0x249ff, FORCEWAKE_GT), /*
1427 		0x24180 - 0x241ff: gt
1428 		0x24200 - 0x249ff: reserved */
1429 	GEN_FW_RANGE(0x24a00, 0x251ff, FORCEWAKE_RENDER), /*
1430 		0x24a00 - 0x24a7f: render
1431 		0x24a80 - 0x251ff: reserved */
1432 	GEN_FW_RANGE(0x25200, 0x255ff, FORCEWAKE_GT), /*
1433 		0x25200 - 0x252ff: gt
1434 		0x25300 - 0x255ff: reserved */
1435 	GEN_FW_RANGE(0x25600, 0x2567f, FORCEWAKE_MEDIA_VDBOX0),
1436 	GEN_FW_RANGE(0x25680, 0x259ff, FORCEWAKE_MEDIA_VDBOX2), /*
1437 		0x25680 - 0x256ff: VD2
1438 		0x25700 - 0x259ff: reserved */
1439 	GEN_FW_RANGE(0x25a00, 0x25a7f, FORCEWAKE_MEDIA_VDBOX0),
1440 	GEN_FW_RANGE(0x25a80, 0x2ffff, FORCEWAKE_MEDIA_VDBOX2), /*
1441 		0x25a80 - 0x25aff: VD2
1442 		0x25b00 - 0x2ffff: reserved */
1443 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_GT),
1444 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),
1445 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0), /*
1446 		0x1c0000 - 0x1c2bff: VD0
1447 		0x1c2c00 - 0x1c2cff: reserved
1448 		0x1c2d00 - 0x1c2dff: VD0
1449 		0x1c2e00 - 0x1c3eff: reserved
1450 		0x1c3f00 - 0x1c3fff: VD0 */
1451 	GEN_FW_RANGE(0x1c4000, 0x1c7fff, 0),
1452 	GEN_FW_RANGE(0x1c8000, 0x1cbfff, FORCEWAKE_MEDIA_VEBOX0), /*
1453 		0x1c8000 - 0x1ca0ff: VE0
1454 		0x1ca100 - 0x1cbeff: reserved
1455 		0x1cbf00 - 0x1cbfff: VE0 */
1456 	GEN_FW_RANGE(0x1cc000, 0x1cffff, FORCEWAKE_MEDIA_VDBOX0), /*
1457 		0x1cc000 - 0x1ccfff: VD0
1458 		0x1cd000 - 0x1cffff: reserved */
1459 	GEN_FW_RANGE(0x1d0000, 0x1d3fff, FORCEWAKE_MEDIA_VDBOX2), /*
1460 		0x1d0000 - 0x1d2bff: VD2
1461 		0x1d2c00 - 0x1d2cff: reserved
1462 		0x1d2d00 - 0x1d2dff: VD2
1463 		0x1d2e00 - 0x1d3eff: reserved
1464 		0x1d3f00 - 0x1d3fff: VD2 */
1465 };
1466 
1467 /*
1468  * Graphics IP version 12.55 brings a slight change to the 0xd800 range,
1469  * switching it from the GT domain to the render domain.
1470  */
1471 #define XEHP_FWRANGES(FW_RANGE_D800)					\
1472 	GEN_FW_RANGE(0x0, 0x1fff, 0), /*					\
1473 		  0x0 -  0xaff: reserved					\
1474 		0xb00 - 0x1fff: always on */					\
1475 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),				\
1476 	GEN_FW_RANGE(0x2700, 0x4aff, FORCEWAKE_GT),				\
1477 	GEN_FW_RANGE(0x4b00, 0x51ff, 0), /*					\
1478 		0x4b00 - 0x4fff: reserved					\
1479 		0x5000 - 0x51ff: always on */					\
1480 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),				\
1481 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_GT),				\
1482 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),				\
1483 	GEN_FW_RANGE(0x8160, 0x81ff, 0), /*					\
1484 		0x8160 - 0x817f: reserved					\
1485 		0x8180 - 0x81ff: always on */					\
1486 	GEN_FW_RANGE(0x8200, 0x82ff, FORCEWAKE_GT),				\
1487 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),				\
1488 	GEN_FW_RANGE(0x8500, 0x8cff, FORCEWAKE_GT), /*				\
1489 		0x8500 - 0x87ff: gt						\
1490 		0x8800 - 0x8c7f: reserved					\
1491 		0x8c80 - 0x8cff: gt (DG2 only) */				\
1492 	GEN_FW_RANGE(0x8d00, 0x8fff, FORCEWAKE_RENDER), /*			\
1493 		0x8d00 - 0x8dff: render (DG2 only)				\
1494 		0x8e00 - 0x8fff: reserved */					\
1495 	GEN_FW_RANGE(0x9000, 0x94cf, FORCEWAKE_GT), /*				\
1496 		0x9000 - 0x947f: gt						\
1497 		0x9480 - 0x94cf: reserved */					\
1498 	GEN_FW_RANGE(0x94d0, 0x955f, FORCEWAKE_RENDER),				\
1499 	GEN_FW_RANGE(0x9560, 0x967f, 0), /*					\
1500 		0x9560 - 0x95ff: always on					\
1501 		0x9600 - 0x967f: reserved */					\
1502 	GEN_FW_RANGE(0x9680, 0x97ff, FORCEWAKE_RENDER), /*			\
1503 		0x9680 - 0x96ff: render (DG2 only)				\
1504 		0x9700 - 0x97ff: reserved */					\
1505 	GEN_FW_RANGE(0x9800, 0xcfff, FORCEWAKE_GT), /*				\
1506 		0x9800 - 0xb4ff: gt						\
1507 		0xb500 - 0xbfff: reserved					\
1508 		0xc000 - 0xcfff: gt */						\
1509 	GEN_FW_RANGE(0xd000, 0xd7ff, 0),					\
1510 	GEN_FW_RANGE(0xd800, 0xd87f, FW_RANGE_D800),			\
1511 	GEN_FW_RANGE(0xd880, 0xdbff, FORCEWAKE_GT),				\
1512 	GEN_FW_RANGE(0xdc00, 0xdcff, FORCEWAKE_RENDER),				\
1513 	GEN_FW_RANGE(0xdd00, 0xde7f, FORCEWAKE_GT), /*				\
1514 		0xdd00 - 0xddff: gt						\
1515 		0xde00 - 0xde7f: reserved */					\
1516 	GEN_FW_RANGE(0xde80, 0xe8ff, FORCEWAKE_RENDER), /*			\
1517 		0xde80 - 0xdfff: render						\
1518 		0xe000 - 0xe0ff: reserved					\
1519 		0xe100 - 0xe8ff: render */					\
1520 	GEN_FW_RANGE(0xe900, 0xffff, FORCEWAKE_GT), /*				\
1521 		0xe900 - 0xe9ff: gt						\
1522 		0xea00 - 0xefff: reserved					\
1523 		0xf000 - 0xffff: gt */						\
1524 	GEN_FW_RANGE(0x10000, 0x12fff, 0), /*					\
1525 		0x10000 - 0x11fff: reserved					\
1526 		0x12000 - 0x127ff: always on					\
1527 		0x12800 - 0x12fff: reserved */					\
1528 	GEN_FW_RANGE(0x13000, 0x131ff, FORCEWAKE_MEDIA_VDBOX0), /* DG2 only */	\
1529 	GEN_FW_RANGE(0x13200, 0x13fff, FORCEWAKE_MEDIA_VDBOX2), /*		\
1530 		0x13200 - 0x133ff: VD2 (DG2 only)				\
1531 		0x13400 - 0x13fff: reserved */					\
1532 	GEN_FW_RANGE(0x14000, 0x141ff, FORCEWAKE_MEDIA_VDBOX0), /* XEHPSDV only */	\
1533 	GEN_FW_RANGE(0x14200, 0x143ff, FORCEWAKE_MEDIA_VDBOX2), /* XEHPSDV only */	\
1534 	GEN_FW_RANGE(0x14400, 0x145ff, FORCEWAKE_MEDIA_VDBOX4), /* XEHPSDV only */	\
1535 	GEN_FW_RANGE(0x14600, 0x147ff, FORCEWAKE_MEDIA_VDBOX6), /* XEHPSDV only */	\
1536 	GEN_FW_RANGE(0x14800, 0x14fff, FORCEWAKE_RENDER),			\
1537 	GEN_FW_RANGE(0x15000, 0x16dff, FORCEWAKE_GT), /*			\
1538 		0x15000 - 0x15fff: gt (DG2 only)				\
1539 		0x16000 - 0x16dff: reserved */					\
1540 	GEN_FW_RANGE(0x16e00, 0x1ffff, FORCEWAKE_RENDER),			\
1541 	GEN_FW_RANGE(0x20000, 0x21fff, FORCEWAKE_MEDIA_VDBOX0), /*		\
1542 		0x20000 - 0x20fff: VD0 (XEHPSDV only)				\
1543 		0x21000 - 0x21fff: reserved */					\
1544 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_GT),				\
1545 	GEN_FW_RANGE(0x24000, 0x2417f, 0), /*					\
1546 		0x24000 - 0x2407f: always on					\
1547 		0x24080 - 0x2417f: reserved */					\
1548 	GEN_FW_RANGE(0x24180, 0x249ff, FORCEWAKE_GT), /*			\
1549 		0x24180 - 0x241ff: gt						\
1550 		0x24200 - 0x249ff: reserved */					\
1551 	GEN_FW_RANGE(0x24a00, 0x251ff, FORCEWAKE_RENDER), /*			\
1552 		0x24a00 - 0x24a7f: render					\
1553 		0x24a80 - 0x251ff: reserved */					\
1554 	GEN_FW_RANGE(0x25200, 0x25fff, FORCEWAKE_GT), /*			\
1555 		0x25200 - 0x252ff: gt						\
1556 		0x25300 - 0x25fff: reserved */					\
1557 	GEN_FW_RANGE(0x26000, 0x2ffff, FORCEWAKE_RENDER), /*			\
1558 		0x26000 - 0x27fff: render					\
1559 		0x28000 - 0x29fff: reserved					\
1560 		0x2a000 - 0x2ffff: undocumented */				\
1561 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_GT),				\
1562 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),					\
1563 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0), /*		\
1564 		0x1c0000 - 0x1c2bff: VD0					\
1565 		0x1c2c00 - 0x1c2cff: reserved					\
1566 		0x1c2d00 - 0x1c2dff: VD0					\
1567 		0x1c2e00 - 0x1c3eff: VD0 (DG2 only)				\
1568 		0x1c3f00 - 0x1c3fff: VD0 */					\
1569 	GEN_FW_RANGE(0x1c4000, 0x1c7fff, FORCEWAKE_MEDIA_VDBOX1), /*		\
1570 		0x1c4000 - 0x1c6bff: VD1					\
1571 		0x1c6c00 - 0x1c6cff: reserved					\
1572 		0x1c6d00 - 0x1c6dff: VD1					\
1573 		0x1c6e00 - 0x1c7fff: reserved */				\
1574 	GEN_FW_RANGE(0x1c8000, 0x1cbfff, FORCEWAKE_MEDIA_VEBOX0), /*		\
1575 		0x1c8000 - 0x1ca0ff: VE0					\
1576 		0x1ca100 - 0x1cbfff: reserved */				\
1577 	GEN_FW_RANGE(0x1cc000, 0x1ccfff, FORCEWAKE_MEDIA_VDBOX0),		\
1578 	GEN_FW_RANGE(0x1cd000, 0x1cdfff, FORCEWAKE_MEDIA_VDBOX2),		\
1579 	GEN_FW_RANGE(0x1ce000, 0x1cefff, FORCEWAKE_MEDIA_VDBOX4),		\
1580 	GEN_FW_RANGE(0x1cf000, 0x1cffff, FORCEWAKE_MEDIA_VDBOX6),		\
1581 	GEN_FW_RANGE(0x1d0000, 0x1d3fff, FORCEWAKE_MEDIA_VDBOX2), /*		\
1582 		0x1d0000 - 0x1d2bff: VD2					\
1583 		0x1d2c00 - 0x1d2cff: reserved					\
1584 		0x1d2d00 - 0x1d2dff: VD2					\
1585 		0x1d2e00 - 0x1d3dff: VD2 (DG2 only)				\
1586 		0x1d3e00 - 0x1d3eff: reserved					\
1587 		0x1d3f00 - 0x1d3fff: VD2 */					\
1588 	GEN_FW_RANGE(0x1d4000, 0x1d7fff, FORCEWAKE_MEDIA_VDBOX3), /*		\
1589 		0x1d4000 - 0x1d6bff: VD3					\
1590 		0x1d6c00 - 0x1d6cff: reserved					\
1591 		0x1d6d00 - 0x1d6dff: VD3					\
1592 		0x1d6e00 - 0x1d7fff: reserved */				\
1593 	GEN_FW_RANGE(0x1d8000, 0x1dffff, FORCEWAKE_MEDIA_VEBOX1), /*		\
1594 		0x1d8000 - 0x1da0ff: VE1					\
1595 		0x1da100 - 0x1dffff: reserved */				\
1596 	GEN_FW_RANGE(0x1e0000, 0x1e3fff, FORCEWAKE_MEDIA_VDBOX4), /*		\
1597 		0x1e0000 - 0x1e2bff: VD4					\
1598 		0x1e2c00 - 0x1e2cff: reserved					\
1599 		0x1e2d00 - 0x1e2dff: VD4					\
1600 		0x1e2e00 - 0x1e3eff: reserved					\
1601 		0x1e3f00 - 0x1e3fff: VD4 */					\
1602 	GEN_FW_RANGE(0x1e4000, 0x1e7fff, FORCEWAKE_MEDIA_VDBOX5), /*		\
1603 		0x1e4000 - 0x1e6bff: VD5					\
1604 		0x1e6c00 - 0x1e6cff: reserved					\
1605 		0x1e6d00 - 0x1e6dff: VD5					\
1606 		0x1e6e00 - 0x1e7fff: reserved */				\
1607 	GEN_FW_RANGE(0x1e8000, 0x1effff, FORCEWAKE_MEDIA_VEBOX2), /*		\
1608 		0x1e8000 - 0x1ea0ff: VE2					\
1609 		0x1ea100 - 0x1effff: reserved */				\
1610 	GEN_FW_RANGE(0x1f0000, 0x1f3fff, FORCEWAKE_MEDIA_VDBOX6), /*		\
1611 		0x1f0000 - 0x1f2bff: VD6					\
1612 		0x1f2c00 - 0x1f2cff: reserved					\
1613 		0x1f2d00 - 0x1f2dff: VD6					\
1614 		0x1f2e00 - 0x1f3eff: reserved					\
1615 		0x1f3f00 - 0x1f3fff: VD6 */					\
1616 	GEN_FW_RANGE(0x1f4000, 0x1f7fff, FORCEWAKE_MEDIA_VDBOX7), /*		\
1617 		0x1f4000 - 0x1f6bff: VD7					\
1618 		0x1f6c00 - 0x1f6cff: reserved					\
1619 		0x1f6d00 - 0x1f6dff: VD7					\
1620 		0x1f6e00 - 0x1f7fff: reserved */				\
1621 	GEN_FW_RANGE(0x1f8000, 0x1fa0ff, FORCEWAKE_MEDIA_VEBOX3),
1622 
1623 static const struct intel_forcewake_range __xehp_fw_ranges[] = {
1624 	XEHP_FWRANGES(FORCEWAKE_GT)
1625 };
1626 
1627 static const struct intel_forcewake_range __dg2_fw_ranges[] = {
1628 	XEHP_FWRANGES(FORCEWAKE_RENDER)
1629 };
1630 
1631 static const struct intel_forcewake_range __pvc_fw_ranges[] = {
1632 	GEN_FW_RANGE(0x0, 0xaff, 0),
1633 	GEN_FW_RANGE(0xb00, 0xbff, FORCEWAKE_GT),
1634 	GEN_FW_RANGE(0xc00, 0xfff, 0),
1635 	GEN_FW_RANGE(0x1000, 0x1fff, FORCEWAKE_GT),
1636 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1637 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_GT),
1638 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1639 	GEN_FW_RANGE(0x4000, 0x813f, FORCEWAKE_GT), /*
1640 		0x4000 - 0x4aff: gt
1641 		0x4b00 - 0x4fff: reserved
1642 		0x5000 - 0x51ff: gt
1643 		0x5200 - 0x52ff: reserved
1644 		0x5300 - 0x53ff: gt
1645 		0x5400 - 0x7fff: reserved
1646 		0x8000 - 0x813f: gt */
1647 	GEN_FW_RANGE(0x8140, 0x817f, FORCEWAKE_RENDER),
1648 	GEN_FW_RANGE(0x8180, 0x81ff, 0),
1649 	GEN_FW_RANGE(0x8200, 0x94cf, FORCEWAKE_GT), /*
1650 		0x8200 - 0x82ff: gt
1651 		0x8300 - 0x84ff: reserved
1652 		0x8500 - 0x887f: gt
1653 		0x8880 - 0x8a7f: reserved
1654 		0x8a80 - 0x8aff: gt
1655 		0x8b00 - 0x8fff: reserved
1656 		0x9000 - 0x947f: gt
1657 		0x9480 - 0x94cf: reserved */
1658 	GEN_FW_RANGE(0x94d0, 0x955f, FORCEWAKE_RENDER),
1659 	GEN_FW_RANGE(0x9560, 0x967f, 0), /*
1660 		0x9560 - 0x95ff: always on
1661 		0x9600 - 0x967f: reserved */
1662 	GEN_FW_RANGE(0x9680, 0x97ff, FORCEWAKE_RENDER), /*
1663 		0x9680 - 0x96ff: render
1664 		0x9700 - 0x97ff: reserved */
1665 	GEN_FW_RANGE(0x9800, 0xcfff, FORCEWAKE_GT), /*
1666 		0x9800 - 0xb4ff: gt
1667 		0xb500 - 0xbfff: reserved
1668 		0xc000 - 0xcfff: gt */
1669 	GEN_FW_RANGE(0xd000, 0xd3ff, 0),
1670 	GEN_FW_RANGE(0xd400, 0xdbff, FORCEWAKE_GT),
1671 	GEN_FW_RANGE(0xdc00, 0xdcff, FORCEWAKE_RENDER),
1672 	GEN_FW_RANGE(0xdd00, 0xde7f, FORCEWAKE_GT), /*
1673 		0xdd00 - 0xddff: gt
1674 		0xde00 - 0xde7f: reserved */
1675 	GEN_FW_RANGE(0xde80, 0xe8ff, FORCEWAKE_RENDER), /*
1676 		0xde80 - 0xdeff: render
1677 		0xdf00 - 0xe1ff: reserved
1678 		0xe200 - 0xe7ff: render
1679 		0xe800 - 0xe8ff: reserved */
1680 	GEN_FW_RANGE(0xe900, 0x11fff, FORCEWAKE_GT), /*
1681 		 0xe900 -  0xe9ff: gt
1682 		 0xea00 -  0xebff: reserved
1683 		 0xec00 -  0xffff: gt
1684 		0x10000 - 0x11fff: reserved */
1685 	GEN_FW_RANGE(0x12000, 0x12fff, 0), /*
1686 		0x12000 - 0x127ff: always on
1687 		0x12800 - 0x12fff: reserved */
1688 	GEN_FW_RANGE(0x13000, 0x19fff, FORCEWAKE_GT), /*
1689 		0x13000 - 0x135ff: gt
1690 		0x13600 - 0x147ff: reserved
1691 		0x14800 - 0x153ff: gt
1692 		0x15400 - 0x19fff: reserved */
1693 	GEN_FW_RANGE(0x1a000, 0x21fff, FORCEWAKE_RENDER), /*
1694 		0x1a000 - 0x1ffff: render
1695 		0x20000 - 0x21fff: reserved */
1696 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_GT),
1697 	GEN_FW_RANGE(0x24000, 0x2417f, 0), /*
1698 		24000 - 0x2407f: always on
1699 		24080 - 0x2417f: reserved */
1700 	GEN_FW_RANGE(0x24180, 0x25fff, FORCEWAKE_GT), /*
1701 		0x24180 - 0x241ff: gt
1702 		0x24200 - 0x251ff: reserved
1703 		0x25200 - 0x252ff: gt
1704 		0x25300 - 0x25fff: reserved */
1705 	GEN_FW_RANGE(0x26000, 0x2ffff, FORCEWAKE_RENDER), /*
1706 		0x26000 - 0x27fff: render
1707 		0x28000 - 0x2ffff: reserved */
1708 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_GT),
1709 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),
1710 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0), /*
1711 		0x1c0000 - 0x1c2bff: VD0
1712 		0x1c2c00 - 0x1c2cff: reserved
1713 		0x1c2d00 - 0x1c2dff: VD0
1714 		0x1c2e00 - 0x1c3eff: reserved
1715 		0x1c3f00 - 0x1c3fff: VD0 */
1716 	GEN_FW_RANGE(0x1c4000, 0x1cffff, FORCEWAKE_MEDIA_VDBOX1), /*
1717 		0x1c4000 - 0x1c6aff: VD1
1718 		0x1c6b00 - 0x1c7eff: reserved
1719 		0x1c7f00 - 0x1c7fff: VD1
1720 		0x1c8000 - 0x1cffff: reserved */
1721 	GEN_FW_RANGE(0x1d0000, 0x23ffff, FORCEWAKE_MEDIA_VDBOX2), /*
1722 		0x1d0000 - 0x1d2aff: VD2
1723 		0x1d2b00 - 0x1d3eff: reserved
1724 		0x1d3f00 - 0x1d3fff: VD2
1725 		0x1d4000 - 0x23ffff: reserved */
1726 	GEN_FW_RANGE(0x240000, 0x3dffff, 0),
1727 	GEN_FW_RANGE(0x3e0000, 0x3effff, FORCEWAKE_GT),
1728 };
1729 
1730 static const struct intel_forcewake_range __mtl_fw_ranges[] = {
1731 	GEN_FW_RANGE(0x0, 0xaff, 0),
1732 	GEN_FW_RANGE(0xb00, 0xbff, FORCEWAKE_GT),
1733 	GEN_FW_RANGE(0xc00, 0xfff, 0),
1734 	GEN_FW_RANGE(0x1000, 0x1fff, FORCEWAKE_GT),
1735 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1736 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_GT),
1737 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1738 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_GT), /*
1739 		0x4000 - 0x48ff: render
1740 		0x4900 - 0x51ff: reserved */
1741 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER), /*
1742 		0x5200 - 0x53ff: render
1743 		0x5400 - 0x54ff: reserved
1744 		0x5500 - 0x7fff: render */
1745 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_GT),
1746 	GEN_FW_RANGE(0x8140, 0x817f, FORCEWAKE_RENDER), /*
1747 		0x8140 - 0x815f: render
1748 		0x8160 - 0x817f: reserved */
1749 	GEN_FW_RANGE(0x8180, 0x81ff, 0),
1750 	GEN_FW_RANGE(0x8200, 0x94cf, FORCEWAKE_GT), /*
1751 		0x8200 - 0x87ff: gt
1752 		0x8800 - 0x8dff: reserved
1753 		0x8e00 - 0x8f7f: gt
1754 		0x8f80 - 0x8fff: reserved
1755 		0x9000 - 0x947f: gt
1756 		0x9480 - 0x94cf: reserved */
1757 	GEN_FW_RANGE(0x94d0, 0x955f, FORCEWAKE_RENDER),
1758 	GEN_FW_RANGE(0x9560, 0x967f, 0), /*
1759 		0x9560 - 0x95ff: always on
1760 		0x9600 - 0x967f: reserved */
1761 	GEN_FW_RANGE(0x9680, 0x97ff, FORCEWAKE_RENDER), /*
1762 		0x9680 - 0x96ff: render
1763 		0x9700 - 0x97ff: reserved */
1764 	GEN_FW_RANGE(0x9800, 0xcfff, FORCEWAKE_GT), /*
1765 		0x9800 - 0xb4ff: gt
1766 		0xb500 - 0xbfff: reserved
1767 		0xc000 - 0xcfff: gt */
1768 	GEN_FW_RANGE(0xd000, 0xd7ff, 0), /*
1769 		0xd000 - 0xd3ff: always on
1770 		0xd400 - 0xd7ff: reserved */
1771 	GEN_FW_RANGE(0xd800, 0xd87f, FORCEWAKE_RENDER),
1772 	GEN_FW_RANGE(0xd880, 0xdbff, FORCEWAKE_GT),
1773 	GEN_FW_RANGE(0xdc00, 0xdcff, FORCEWAKE_RENDER),
1774 	GEN_FW_RANGE(0xdd00, 0xde7f, FORCEWAKE_GT), /*
1775 		0xdd00 - 0xddff: gt
1776 		0xde00 - 0xde7f: reserved */
1777 	GEN_FW_RANGE(0xde80, 0xe8ff, FORCEWAKE_RENDER), /*
1778 		0xde80 - 0xdfff: render
1779 		0xe000 - 0xe0ff: reserved
1780 		0xe100 - 0xe8ff: render */
1781 	GEN_FW_RANGE(0xe900, 0xe9ff, FORCEWAKE_GT),
1782 	GEN_FW_RANGE(0xea00, 0x147ff, 0), /*
1783 		 0xea00 - 0x11fff: reserved
1784 		0x12000 - 0x127ff: always on
1785 		0x12800 - 0x147ff: reserved */
1786 	GEN_FW_RANGE(0x14800, 0x19fff, FORCEWAKE_GT), /*
1787 		0x14800 - 0x153ff: gt
1788 		0x15400 - 0x19fff: reserved */
1789 	GEN_FW_RANGE(0x1a000, 0x21fff, FORCEWAKE_RENDER), /*
1790 		0x1a000 - 0x1bfff: render
1791 		0x1c000 - 0x21fff: reserved */
1792 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_GT),
1793 	GEN_FW_RANGE(0x24000, 0x2ffff, 0), /*
1794 		0x24000 - 0x2407f: always on
1795 		0x24080 - 0x2ffff: reserved */
1796 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_GT)
1797 };
1798 
1799 /*
1800  * Note that the register ranges here are the final offsets after
1801  * translation of the GSI block to the 0x380000 offset.
1802  *
1803  * NOTE:  There are a couple MCR ranges near the bottom of this table
1804  * that need to power up either VD0 or VD2 depending on which replicated
1805  * instance of the register we're trying to access.  Our forcewake logic
1806  * at the moment doesn't have a good way to take steering into consideration,
1807  * and the driver doesn't even access any registers in those ranges today,
1808  * so for now we just mark those ranges as FORCEWAKE_ALL.  That will ensure
1809  * proper operation if we do start using the ranges in the future, and we
1810  * can determine at that time whether it's worth adding extra complexity to
1811  * the forcewake handling to take steering into consideration.
1812  */
1813 static const struct intel_forcewake_range __xelpmp_fw_ranges[] = {
1814 	GEN_FW_RANGE(0x0, 0x115fff, 0), /* render GT range */
1815 	GEN_FW_RANGE(0x116000, 0x11ffff, FORCEWAKE_GSC), /*
1816 		0x116000 - 0x117fff: gsc
1817 		0x118000 - 0x119fff: reserved
1818 		0x11a000 - 0x11efff: gsc
1819 		0x11f000 - 0x11ffff: reserved */
1820 	GEN_FW_RANGE(0x120000, 0x1bffff, 0), /* non-GT range */
1821 	GEN_FW_RANGE(0x1c0000, 0x1c7fff, FORCEWAKE_MEDIA_VDBOX0), /*
1822 		0x1c0000 - 0x1c3dff: VD0
1823 		0x1c3e00 - 0x1c3eff: reserved
1824 		0x1c3f00 - 0x1c3fff: VD0
1825 		0x1c4000 - 0x1c7fff: reserved */
1826 	GEN_FW_RANGE(0x1c8000, 0x1cbfff, FORCEWAKE_MEDIA_VEBOX0), /*
1827 		0x1c8000 - 0x1ca0ff: VE0
1828 		0x1ca100 - 0x1cbfff: reserved */
1829 	GEN_FW_RANGE(0x1cc000, 0x1cffff, FORCEWAKE_MEDIA_VDBOX0), /*
1830 		0x1cc000 - 0x1cdfff: VD0
1831 		0x1ce000 - 0x1cffff: reserved */
1832 	GEN_FW_RANGE(0x1d0000, 0x1d7fff, FORCEWAKE_MEDIA_VDBOX2), /*
1833 		0x1d0000 - 0x1d3dff: VD2
1834 		0x1d3e00 - 0x1d3eff: reserved
1835 		0x1d4000 - 0x1d7fff: VD2 */
1836 	GEN_FW_RANGE(0x1d8000, 0x1da0ff, FORCEWAKE_MEDIA_VEBOX1),
1837 	GEN_FW_RANGE(0x1da100, 0x380aff, 0), /*
1838 		0x1da100 - 0x23ffff: reserved
1839 		0x240000 - 0x37ffff: non-GT range
1840 		0x380000 - 0x380aff: reserved */
1841 	GEN_FW_RANGE(0x380b00, 0x380bff, FORCEWAKE_GT),
1842 	GEN_FW_RANGE(0x380c00, 0x380fff, 0),
1843 	GEN_FW_RANGE(0x381000, 0x38817f, FORCEWAKE_GT), /*
1844 		0x381000 - 0x381fff: gt
1845 		0x382000 - 0x383fff: reserved
1846 		0x384000 - 0x384aff: gt
1847 		0x384b00 - 0x3851ff: reserved
1848 		0x385200 - 0x3871ff: gt
1849 		0x387200 - 0x387fff: reserved
1850 		0x388000 - 0x38813f: gt
1851 		0x388140 - 0x38817f: reserved */
1852 	GEN_FW_RANGE(0x388180, 0x3882ff, 0), /*
1853 		0x388180 - 0x3881ff: always on
1854 		0x388200 - 0x3882ff: reserved */
1855 	GEN_FW_RANGE(0x388300, 0x38955f, FORCEWAKE_GT), /*
1856 		0x388300 - 0x38887f: gt
1857 		0x388880 - 0x388fff: reserved
1858 		0x389000 - 0x38947f: gt
1859 		0x389480 - 0x38955f: reserved */
1860 	GEN_FW_RANGE(0x389560, 0x389fff, 0), /*
1861 		0x389560 - 0x3895ff: always on
1862 		0x389600 - 0x389fff: reserved */
1863 	GEN_FW_RANGE(0x38a000, 0x38cfff, FORCEWAKE_GT), /*
1864 		0x38a000 - 0x38afff: gt
1865 		0x38b000 - 0x38bfff: reserved
1866 		0x38c000 - 0x38cfff: gt */
1867 	GEN_FW_RANGE(0x38d000, 0x38d11f, 0),
1868 	GEN_FW_RANGE(0x38d120, 0x391fff, FORCEWAKE_GT), /*
1869 		0x38d120 - 0x38dfff: gt
1870 		0x38e000 - 0x38efff: reserved
1871 		0x38f000 - 0x38ffff: gt
1872 		0x389000 - 0x391fff: reserved */
1873 	GEN_FW_RANGE(0x392000, 0x392fff, 0), /*
1874 		0x392000 - 0x3927ff: always on
1875 		0x392800 - 0x292fff: reserved */
1876 	GEN_FW_RANGE(0x393000, 0x3931ff, FORCEWAKE_GT),
1877 	GEN_FW_RANGE(0x393200, 0x39323f, FORCEWAKE_ALL), /* instance-based, see note above */
1878 	GEN_FW_RANGE(0x393240, 0x3933ff, FORCEWAKE_GT),
1879 	GEN_FW_RANGE(0x393400, 0x3934ff, FORCEWAKE_ALL), /* instance-based, see note above */
1880 	GEN_FW_RANGE(0x393500, 0x393c7f, 0), /*
1881 		0x393500 - 0x393bff: reserved
1882 		0x393c00 - 0x393c7f: always on */
1883 	GEN_FW_RANGE(0x393c80, 0x393dff, FORCEWAKE_GT),
1884 };
1885 
1886 static void
1887 ilk_dummy_write(struct intel_uncore *uncore)
1888 {
1889 	/* WaIssueDummyWriteToWakeupFromRC6:ilk Issue a dummy write to wake up
1890 	 * the chip from rc6 before touching it for real. MI_MODE is masked,
1891 	 * hence harmless to write 0 into. */
1892 	__raw_uncore_write32(uncore, RING_MI_MODE(RENDER_RING_BASE), 0);
1893 }
1894 
1895 static void
1896 __unclaimed_reg_debug(struct intel_uncore *uncore,
1897 		      const i915_reg_t reg,
1898 		      const bool read)
1899 {
1900 	if (drm_WARN(&uncore->i915->drm,
1901 		     check_for_unclaimed_mmio(uncore),
1902 		     "Unclaimed %s register 0x%x\n",
1903 		     read ? "read from" : "write to",
1904 		     i915_mmio_reg_offset(reg)))
1905 		/* Only report the first N failures */
1906 		uncore->i915->params.mmio_debug--;
1907 }
1908 
1909 static void
1910 __unclaimed_previous_reg_debug(struct intel_uncore *uncore,
1911 			       const i915_reg_t reg,
1912 			       const bool read)
1913 {
1914 	if (check_for_unclaimed_mmio(uncore))
1915 		drm_dbg(&uncore->i915->drm,
1916 			"Unclaimed access detected before %s register 0x%x\n",
1917 			read ? "read from" : "write to",
1918 			i915_mmio_reg_offset(reg));
1919 }
1920 
1921 static inline void
1922 unclaimed_reg_debug(struct intel_uncore *uncore,
1923 		    const i915_reg_t reg,
1924 		    const bool read,
1925 		    const bool before)
1926 {
1927 	if (likely(!uncore->i915->params.mmio_debug) || !uncore->debug)
1928 		return;
1929 
1930 	/* interrupts are disabled and re-enabled around uncore->lock usage */
1931 	lockdep_assert_held(&uncore->lock);
1932 
1933 	if (before) {
1934 		spin_lock(&uncore->debug->lock);
1935 		__unclaimed_previous_reg_debug(uncore, reg, read);
1936 	} else {
1937 		__unclaimed_reg_debug(uncore, reg, read);
1938 		spin_unlock(&uncore->debug->lock);
1939 	}
1940 }
1941 
1942 #define __vgpu_read(x) \
1943 static u##x \
1944 vgpu_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1945 	u##x val = __raw_uncore_read##x(uncore, reg); \
1946 	trace_i915_reg_rw(false, reg, val, sizeof(val), trace); \
1947 	return val; \
1948 }
1949 __vgpu_read(8)
1950 __vgpu_read(16)
1951 __vgpu_read(32)
1952 __vgpu_read(64)
1953 
1954 #define GEN2_READ_HEADER(x) \
1955 	u##x val = 0; \
1956 	assert_rpm_wakelock_held(uncore->rpm);
1957 
1958 #define GEN2_READ_FOOTER \
1959 	trace_i915_reg_rw(false, reg, val, sizeof(val), trace); \
1960 	return val
1961 
1962 #define __gen2_read(x) \
1963 static u##x \
1964 gen2_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1965 	GEN2_READ_HEADER(x); \
1966 	val = __raw_uncore_read##x(uncore, reg); \
1967 	GEN2_READ_FOOTER; \
1968 }
1969 
1970 #define __gen5_read(x) \
1971 static u##x \
1972 gen5_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1973 	GEN2_READ_HEADER(x); \
1974 	ilk_dummy_write(uncore); \
1975 	val = __raw_uncore_read##x(uncore, reg); \
1976 	GEN2_READ_FOOTER; \
1977 }
1978 
1979 __gen5_read(8)
1980 __gen5_read(16)
1981 __gen5_read(32)
1982 __gen5_read(64)
1983 __gen2_read(8)
1984 __gen2_read(16)
1985 __gen2_read(32)
1986 __gen2_read(64)
1987 
1988 #undef __gen5_read
1989 #undef __gen2_read
1990 
1991 #undef GEN2_READ_FOOTER
1992 #undef GEN2_READ_HEADER
1993 
1994 #define GEN6_READ_HEADER(x) \
1995 	u32 offset = i915_mmio_reg_offset(reg); \
1996 	unsigned long irqflags; \
1997 	u##x val = 0; \
1998 	assert_rpm_wakelock_held(uncore->rpm); \
1999 	spin_lock_irqsave(&uncore->lock, irqflags); \
2000 	unclaimed_reg_debug(uncore, reg, true, true)
2001 
2002 #define GEN6_READ_FOOTER \
2003 	unclaimed_reg_debug(uncore, reg, true, false); \
2004 	spin_unlock_irqrestore(&uncore->lock, irqflags); \
2005 	trace_i915_reg_rw(false, reg, val, sizeof(val), trace); \
2006 	return val
2007 
2008 static noinline void ___force_wake_auto(struct intel_uncore *uncore,
2009 					enum forcewake_domains fw_domains)
2010 {
2011 	struct intel_uncore_forcewake_domain *domain;
2012 	unsigned int tmp;
2013 
2014 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
2015 
2016 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp)
2017 		fw_domain_arm_timer(domain);
2018 
2019 	fw_domains_get(uncore, fw_domains);
2020 }
2021 
2022 static inline void __force_wake_auto(struct intel_uncore *uncore,
2023 				     enum forcewake_domains fw_domains)
2024 {
2025 	GEM_BUG_ON(!fw_domains);
2026 
2027 	/* Turn on all requested but inactive supported forcewake domains. */
2028 	fw_domains &= uncore->fw_domains;
2029 	fw_domains &= ~uncore->fw_domains_active;
2030 
2031 	if (fw_domains)
2032 		___force_wake_auto(uncore, fw_domains);
2033 }
2034 
2035 #define __gen_fwtable_read(x) \
2036 static u##x \
2037 fwtable_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) \
2038 { \
2039 	enum forcewake_domains fw_engine; \
2040 	GEN6_READ_HEADER(x); \
2041 	fw_engine = __fwtable_reg_read_fw_domains(uncore, offset); \
2042 	if (fw_engine) \
2043 		__force_wake_auto(uncore, fw_engine); \
2044 	val = __raw_uncore_read##x(uncore, reg); \
2045 	GEN6_READ_FOOTER; \
2046 }
2047 
2048 static enum forcewake_domains
2049 fwtable_reg_read_fw_domains(struct intel_uncore *uncore, i915_reg_t reg) {
2050 	return __fwtable_reg_read_fw_domains(uncore, i915_mmio_reg_offset(reg));
2051 }
2052 
2053 __gen_fwtable_read(8)
2054 __gen_fwtable_read(16)
2055 __gen_fwtable_read(32)
2056 __gen_fwtable_read(64)
2057 
2058 #undef __gen_fwtable_read
2059 #undef GEN6_READ_FOOTER
2060 #undef GEN6_READ_HEADER
2061 
2062 #define GEN2_WRITE_HEADER \
2063 	trace_i915_reg_rw(true, reg, val, sizeof(val), trace); \
2064 	assert_rpm_wakelock_held(uncore->rpm); \
2065 
2066 #define GEN2_WRITE_FOOTER
2067 
2068 #define __gen2_write(x) \
2069 static void \
2070 gen2_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
2071 	GEN2_WRITE_HEADER; \
2072 	__raw_uncore_write##x(uncore, reg, val); \
2073 	GEN2_WRITE_FOOTER; \
2074 }
2075 
2076 #define __gen5_write(x) \
2077 static void \
2078 gen5_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
2079 	GEN2_WRITE_HEADER; \
2080 	ilk_dummy_write(uncore); \
2081 	__raw_uncore_write##x(uncore, reg, val); \
2082 	GEN2_WRITE_FOOTER; \
2083 }
2084 
2085 __gen5_write(8)
2086 __gen5_write(16)
2087 __gen5_write(32)
2088 __gen2_write(8)
2089 __gen2_write(16)
2090 __gen2_write(32)
2091 
2092 #undef __gen5_write
2093 #undef __gen2_write
2094 
2095 #undef GEN2_WRITE_FOOTER
2096 #undef GEN2_WRITE_HEADER
2097 
2098 #define GEN6_WRITE_HEADER \
2099 	u32 offset = i915_mmio_reg_offset(reg); \
2100 	unsigned long irqflags; \
2101 	trace_i915_reg_rw(true, reg, val, sizeof(val), trace); \
2102 	assert_rpm_wakelock_held(uncore->rpm); \
2103 	spin_lock_irqsave(&uncore->lock, irqflags); \
2104 	unclaimed_reg_debug(uncore, reg, false, true)
2105 
2106 #define GEN6_WRITE_FOOTER \
2107 	unclaimed_reg_debug(uncore, reg, false, false); \
2108 	spin_unlock_irqrestore(&uncore->lock, irqflags)
2109 
2110 #define __gen6_write(x) \
2111 static void \
2112 gen6_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
2113 	GEN6_WRITE_HEADER; \
2114 	if (NEEDS_FORCE_WAKE(offset)) \
2115 		__gen6_gt_wait_for_fifo(uncore); \
2116 	__raw_uncore_write##x(uncore, reg, val); \
2117 	GEN6_WRITE_FOOTER; \
2118 }
2119 __gen6_write(8)
2120 __gen6_write(16)
2121 __gen6_write(32)
2122 
2123 #define __gen_fwtable_write(x) \
2124 static void \
2125 fwtable_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
2126 	enum forcewake_domains fw_engine; \
2127 	GEN6_WRITE_HEADER; \
2128 	fw_engine = __fwtable_reg_write_fw_domains(uncore, offset); \
2129 	if (fw_engine) \
2130 		__force_wake_auto(uncore, fw_engine); \
2131 	__raw_uncore_write##x(uncore, reg, val); \
2132 	GEN6_WRITE_FOOTER; \
2133 }
2134 
2135 static enum forcewake_domains
2136 fwtable_reg_write_fw_domains(struct intel_uncore *uncore, i915_reg_t reg)
2137 {
2138 	return __fwtable_reg_write_fw_domains(uncore, i915_mmio_reg_offset(reg));
2139 }
2140 
2141 __gen_fwtable_write(8)
2142 __gen_fwtable_write(16)
2143 __gen_fwtable_write(32)
2144 
2145 #undef __gen_fwtable_write
2146 #undef GEN6_WRITE_FOOTER
2147 #undef GEN6_WRITE_HEADER
2148 
2149 #define __vgpu_write(x) \
2150 static void \
2151 vgpu_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
2152 	trace_i915_reg_rw(true, reg, val, sizeof(val), trace); \
2153 	__raw_uncore_write##x(uncore, reg, val); \
2154 }
2155 __vgpu_write(8)
2156 __vgpu_write(16)
2157 __vgpu_write(32)
2158 
2159 #define ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, x) \
2160 do { \
2161 	(uncore)->funcs.mmio_writeb = x##_write8; \
2162 	(uncore)->funcs.mmio_writew = x##_write16; \
2163 	(uncore)->funcs.mmio_writel = x##_write32; \
2164 } while (0)
2165 
2166 #define ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, x) \
2167 do { \
2168 	(uncore)->funcs.mmio_readb = x##_read8; \
2169 	(uncore)->funcs.mmio_readw = x##_read16; \
2170 	(uncore)->funcs.mmio_readl = x##_read32; \
2171 	(uncore)->funcs.mmio_readq = x##_read64; \
2172 } while (0)
2173 
2174 #define ASSIGN_WRITE_MMIO_VFUNCS(uncore, x) \
2175 do { \
2176 	ASSIGN_RAW_WRITE_MMIO_VFUNCS((uncore), x); \
2177 	(uncore)->funcs.write_fw_domains = x##_reg_write_fw_domains; \
2178 } while (0)
2179 
2180 #define ASSIGN_READ_MMIO_VFUNCS(uncore, x) \
2181 do { \
2182 	ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, x); \
2183 	(uncore)->funcs.read_fw_domains = x##_reg_read_fw_domains; \
2184 } while (0)
2185 
2186 static int __fw_domain_init(struct intel_uncore *uncore,
2187 			    enum forcewake_domain_id domain_id,
2188 			    i915_reg_t reg_set,
2189 			    i915_reg_t reg_ack)
2190 {
2191 	struct intel_uncore_forcewake_domain *d;
2192 
2193 	GEM_BUG_ON(domain_id >= FW_DOMAIN_ID_COUNT);
2194 	GEM_BUG_ON(uncore->fw_domain[domain_id]);
2195 
2196 	if (i915_inject_probe_failure(uncore->i915))
2197 		return -ENOMEM;
2198 
2199 	d = kzalloc(sizeof(*d), GFP_KERNEL);
2200 	if (!d)
2201 		return -ENOMEM;
2202 
2203 	drm_WARN_ON(&uncore->i915->drm, !i915_mmio_reg_valid(reg_set));
2204 	drm_WARN_ON(&uncore->i915->drm, !i915_mmio_reg_valid(reg_ack));
2205 
2206 	d->uncore = uncore;
2207 	d->wake_count = 0;
2208 	d->reg_set = uncore->regs + i915_mmio_reg_offset(reg_set) + uncore->gsi_offset;
2209 	d->reg_ack = uncore->regs + i915_mmio_reg_offset(reg_ack) + uncore->gsi_offset;
2210 
2211 	d->id = domain_id;
2212 
2213 	BUILD_BUG_ON(FORCEWAKE_RENDER != (1 << FW_DOMAIN_ID_RENDER));
2214 	BUILD_BUG_ON(FORCEWAKE_GT != (1 << FW_DOMAIN_ID_GT));
2215 	BUILD_BUG_ON(FORCEWAKE_MEDIA != (1 << FW_DOMAIN_ID_MEDIA));
2216 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX0 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX0));
2217 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX1 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX1));
2218 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX2 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX2));
2219 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX3 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX3));
2220 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX4 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX4));
2221 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX5 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX5));
2222 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX6 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX6));
2223 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX7 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX7));
2224 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX0 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX0));
2225 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX1 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX1));
2226 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX2 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX2));
2227 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX3 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX3));
2228 	BUILD_BUG_ON(FORCEWAKE_GSC != (1 << FW_DOMAIN_ID_GSC));
2229 
2230 	d->mask = BIT(domain_id);
2231 
2232 	hrtimer_init(&d->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2233 	d->timer.function = intel_uncore_fw_release_timer;
2234 
2235 	uncore->fw_domains |= BIT(domain_id);
2236 
2237 	fw_domain_reset(d);
2238 
2239 	uncore->fw_domain[domain_id] = d;
2240 
2241 	return 0;
2242 }
2243 
2244 static void fw_domain_fini(struct intel_uncore *uncore,
2245 			   enum forcewake_domain_id domain_id)
2246 {
2247 	struct intel_uncore_forcewake_domain *d;
2248 
2249 	GEM_BUG_ON(domain_id >= FW_DOMAIN_ID_COUNT);
2250 
2251 	d = fetch_and_zero(&uncore->fw_domain[domain_id]);
2252 	if (!d)
2253 		return;
2254 
2255 	uncore->fw_domains &= ~BIT(domain_id);
2256 	drm_WARN_ON(&uncore->i915->drm, d->wake_count);
2257 	drm_WARN_ON(&uncore->i915->drm, hrtimer_cancel(&d->timer));
2258 	kfree(d);
2259 }
2260 
2261 static void intel_uncore_fw_domains_fini(struct intel_uncore *uncore)
2262 {
2263 	struct intel_uncore_forcewake_domain *d;
2264 	int tmp;
2265 
2266 	for_each_fw_domain(d, uncore, tmp)
2267 		fw_domain_fini(uncore, d->id);
2268 }
2269 
2270 static const struct intel_uncore_fw_get uncore_get_fallback = {
2271 	.force_wake_get = fw_domains_get_with_fallback
2272 };
2273 
2274 static const struct intel_uncore_fw_get uncore_get_normal = {
2275 	.force_wake_get = fw_domains_get_normal,
2276 };
2277 
2278 static const struct intel_uncore_fw_get uncore_get_thread_status = {
2279 	.force_wake_get = fw_domains_get_with_thread_status
2280 };
2281 
2282 static int intel_uncore_fw_domains_init(struct intel_uncore *uncore)
2283 {
2284 	struct drm_i915_private *i915 = uncore->i915;
2285 	int ret = 0;
2286 
2287 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
2288 
2289 #define fw_domain_init(uncore__, id__, set__, ack__) \
2290 	(ret ?: (ret = __fw_domain_init((uncore__), (id__), (set__), (ack__))))
2291 
2292 	if (GRAPHICS_VER(i915) >= 11) {
2293 		intel_engine_mask_t emask;
2294 		int i;
2295 
2296 		/* we'll prune the domains of missing engines later */
2297 		emask = uncore->gt->info.engine_mask;
2298 
2299 		uncore->fw_get_funcs = &uncore_get_fallback;
2300 		if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
2301 			fw_domain_init(uncore, FW_DOMAIN_ID_GT,
2302 				       FORCEWAKE_GT_GEN9,
2303 				       FORCEWAKE_ACK_GT_MTL);
2304 		else
2305 			fw_domain_init(uncore, FW_DOMAIN_ID_GT,
2306 				       FORCEWAKE_GT_GEN9,
2307 				       FORCEWAKE_ACK_GT_GEN9);
2308 
2309 		if (RCS_MASK(uncore->gt) || CCS_MASK(uncore->gt))
2310 			fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2311 				       FORCEWAKE_RENDER_GEN9,
2312 				       FORCEWAKE_ACK_RENDER_GEN9);
2313 
2314 		for (i = 0; i < I915_MAX_VCS; i++) {
2315 			if (!__HAS_ENGINE(emask, _VCS(i)))
2316 				continue;
2317 
2318 			fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA_VDBOX0 + i,
2319 				       FORCEWAKE_MEDIA_VDBOX_GEN11(i),
2320 				       FORCEWAKE_ACK_MEDIA_VDBOX_GEN11(i));
2321 		}
2322 		for (i = 0; i < I915_MAX_VECS; i++) {
2323 			if (!__HAS_ENGINE(emask, _VECS(i)))
2324 				continue;
2325 
2326 			fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA_VEBOX0 + i,
2327 				       FORCEWAKE_MEDIA_VEBOX_GEN11(i),
2328 				       FORCEWAKE_ACK_MEDIA_VEBOX_GEN11(i));
2329 		}
2330 
2331 		if (uncore->gt->type == GT_MEDIA)
2332 			fw_domain_init(uncore, FW_DOMAIN_ID_GSC,
2333 				       FORCEWAKE_REQ_GSC, FORCEWAKE_ACK_GSC);
2334 	} else if (IS_GRAPHICS_VER(i915, 9, 10)) {
2335 		uncore->fw_get_funcs = &uncore_get_fallback;
2336 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2337 			       FORCEWAKE_RENDER_GEN9,
2338 			       FORCEWAKE_ACK_RENDER_GEN9);
2339 		fw_domain_init(uncore, FW_DOMAIN_ID_GT,
2340 			       FORCEWAKE_GT_GEN9,
2341 			       FORCEWAKE_ACK_GT_GEN9);
2342 		fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA,
2343 			       FORCEWAKE_MEDIA_GEN9, FORCEWAKE_ACK_MEDIA_GEN9);
2344 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
2345 		uncore->fw_get_funcs = &uncore_get_normal;
2346 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2347 			       FORCEWAKE_VLV, FORCEWAKE_ACK_VLV);
2348 		fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA,
2349 			       FORCEWAKE_MEDIA_VLV, FORCEWAKE_ACK_MEDIA_VLV);
2350 	} else if (IS_HASWELL(i915) || IS_BROADWELL(i915)) {
2351 		uncore->fw_get_funcs = &uncore_get_thread_status;
2352 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2353 			       FORCEWAKE_MT, FORCEWAKE_ACK_HSW);
2354 	} else if (IS_IVYBRIDGE(i915)) {
2355 		u32 ecobus;
2356 
2357 		/* IVB configs may use multi-threaded forcewake */
2358 
2359 		/* A small trick here - if the bios hasn't configured
2360 		 * MT forcewake, and if the device is in RC6, then
2361 		 * force_wake_mt_get will not wake the device and the
2362 		 * ECOBUS read will return zero. Which will be
2363 		 * (correctly) interpreted by the test below as MT
2364 		 * forcewake being disabled.
2365 		 */
2366 		uncore->fw_get_funcs = &uncore_get_thread_status;
2367 
2368 		/* We need to init first for ECOBUS access and then
2369 		 * determine later if we want to reinit, in case of MT access is
2370 		 * not working. In this stage we don't know which flavour this
2371 		 * ivb is, so it is better to reset also the gen6 fw registers
2372 		 * before the ecobus check.
2373 		 */
2374 
2375 		__raw_uncore_write32(uncore, FORCEWAKE, 0);
2376 		__raw_posting_read(uncore, ECOBUS);
2377 
2378 		ret = __fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2379 				       FORCEWAKE_MT, FORCEWAKE_MT_ACK);
2380 		if (ret)
2381 			goto out;
2382 
2383 		spin_lock_irq(&uncore->lock);
2384 		fw_domains_get_with_thread_status(uncore, FORCEWAKE_RENDER);
2385 		ecobus = __raw_uncore_read32(uncore, ECOBUS);
2386 		fw_domains_put(uncore, FORCEWAKE_RENDER);
2387 		spin_unlock_irq(&uncore->lock);
2388 
2389 		if (!(ecobus & FORCEWAKE_MT_ENABLE)) {
2390 			drm_info(&i915->drm, "No MT forcewake available on Ivybridge, this can result in issues\n");
2391 			drm_info(&i915->drm, "when using vblank-synced partial screen updates.\n");
2392 			fw_domain_fini(uncore, FW_DOMAIN_ID_RENDER);
2393 			fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2394 				       FORCEWAKE, FORCEWAKE_ACK);
2395 		}
2396 	} else if (GRAPHICS_VER(i915) == 6) {
2397 		uncore->fw_get_funcs = &uncore_get_thread_status;
2398 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
2399 			       FORCEWAKE, FORCEWAKE_ACK);
2400 	}
2401 
2402 #undef fw_domain_init
2403 
2404 	/* All future platforms are expected to require complex power gating */
2405 	drm_WARN_ON(&i915->drm, !ret && uncore->fw_domains == 0);
2406 
2407 out:
2408 	if (ret)
2409 		intel_uncore_fw_domains_fini(uncore);
2410 
2411 	return ret;
2412 }
2413 
2414 #define ASSIGN_FW_DOMAINS_TABLE(uncore, d) \
2415 { \
2416 	(uncore)->fw_domains_table = \
2417 			(struct intel_forcewake_range *)(d); \
2418 	(uncore)->fw_domains_table_entries = ARRAY_SIZE((d)); \
2419 }
2420 
2421 #define ASSIGN_SHADOW_TABLE(uncore, d) \
2422 { \
2423 	(uncore)->shadowed_reg_table = d; \
2424 	(uncore)->shadowed_reg_table_entries = ARRAY_SIZE((d)); \
2425 }
2426 
2427 static int i915_pmic_bus_access_notifier(struct notifier_block *nb,
2428 					 unsigned long action, void *data)
2429 {
2430 	struct intel_uncore *uncore = container_of(nb,
2431 			struct intel_uncore, pmic_bus_access_nb);
2432 
2433 	switch (action) {
2434 	case MBI_PMIC_BUS_ACCESS_BEGIN:
2435 		/*
2436 		 * forcewake all now to make sure that we don't need to do a
2437 		 * forcewake later which on systems where this notifier gets
2438 		 * called requires the punit to access to the shared pmic i2c
2439 		 * bus, which will be busy after this notification, leading to:
2440 		 * "render: timed out waiting for forcewake ack request."
2441 		 * errors.
2442 		 *
2443 		 * The notifier is unregistered during intel_runtime_suspend(),
2444 		 * so it's ok to access the HW here without holding a RPM
2445 		 * wake reference -> disable wakeref asserts for the time of
2446 		 * the access.
2447 		 */
2448 		disable_rpm_wakeref_asserts(uncore->rpm);
2449 		intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
2450 		enable_rpm_wakeref_asserts(uncore->rpm);
2451 		break;
2452 	case MBI_PMIC_BUS_ACCESS_END:
2453 		intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
2454 		break;
2455 	}
2456 
2457 	return NOTIFY_OK;
2458 }
2459 
2460 static void uncore_unmap_mmio(struct drm_device *drm, void *regs)
2461 {
2462 	iounmap(regs);
2463 }
2464 
2465 int intel_uncore_setup_mmio(struct intel_uncore *uncore, phys_addr_t phys_addr)
2466 {
2467 	struct drm_i915_private *i915 = uncore->i915;
2468 	int mmio_size;
2469 
2470 	/*
2471 	 * Before gen4, the registers and the GTT are behind different BARs.
2472 	 * However, from gen4 onwards, the registers and the GTT are shared
2473 	 * in the same BAR, so we want to restrict this ioremap from
2474 	 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
2475 	 * the register BAR remains the same size for all the earlier
2476 	 * generations up to Ironlake.
2477 	 * For dgfx chips register range is expanded to 4MB, and this larger
2478 	 * range is also used for integrated gpus beginning with Meteor Lake.
2479 	 */
2480 	if (IS_DGFX(i915) || GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
2481 		mmio_size = 4 * 1024 * 1024;
2482 	else if (GRAPHICS_VER(i915) >= 5)
2483 		mmio_size = 2 * 1024 * 1024;
2484 	else
2485 		mmio_size = 512 * 1024;
2486 
2487 	uncore->regs = ioremap(phys_addr, mmio_size);
2488 	if (uncore->regs == NULL) {
2489 		drm_err(&i915->drm, "failed to map registers\n");
2490 		return -EIO;
2491 	}
2492 
2493 	return drmm_add_action_or_reset(&i915->drm, uncore_unmap_mmio, uncore->regs);
2494 }
2495 
2496 void intel_uncore_init_early(struct intel_uncore *uncore,
2497 			     struct intel_gt *gt)
2498 {
2499 	spin_lock_init(&uncore->lock);
2500 	uncore->i915 = gt->i915;
2501 	uncore->gt = gt;
2502 	uncore->rpm = &gt->i915->runtime_pm;
2503 }
2504 
2505 static void uncore_raw_init(struct intel_uncore *uncore)
2506 {
2507 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore));
2508 
2509 	if (intel_vgpu_active(uncore->i915)) {
2510 		ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, vgpu);
2511 		ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, vgpu);
2512 	} else if (GRAPHICS_VER(uncore->i915) == 5) {
2513 		ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, gen5);
2514 		ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, gen5);
2515 	} else {
2516 		ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, gen2);
2517 		ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, gen2);
2518 	}
2519 }
2520 
2521 static int uncore_media_forcewake_init(struct intel_uncore *uncore)
2522 {
2523 	struct drm_i915_private *i915 = uncore->i915;
2524 
2525 	if (MEDIA_VER(i915) >= 13) {
2526 		ASSIGN_FW_DOMAINS_TABLE(uncore, __xelpmp_fw_ranges);
2527 		ASSIGN_SHADOW_TABLE(uncore, xelpmp_shadowed_regs);
2528 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2529 	} else {
2530 		MISSING_CASE(MEDIA_VER(i915));
2531 		return -ENODEV;
2532 	}
2533 
2534 	return 0;
2535 }
2536 
2537 static int uncore_forcewake_init(struct intel_uncore *uncore)
2538 {
2539 	struct drm_i915_private *i915 = uncore->i915;
2540 	int ret;
2541 
2542 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
2543 
2544 	ret = intel_uncore_fw_domains_init(uncore);
2545 	if (ret)
2546 		return ret;
2547 	forcewake_early_sanitize(uncore, 0);
2548 
2549 	ASSIGN_READ_MMIO_VFUNCS(uncore, fwtable);
2550 
2551 	if (uncore->gt->type == GT_MEDIA)
2552 		return uncore_media_forcewake_init(uncore);
2553 
2554 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70)) {
2555 		ASSIGN_FW_DOMAINS_TABLE(uncore, __mtl_fw_ranges);
2556 		ASSIGN_SHADOW_TABLE(uncore, mtl_shadowed_regs);
2557 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2558 	} else if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 60)) {
2559 		ASSIGN_FW_DOMAINS_TABLE(uncore, __pvc_fw_ranges);
2560 		ASSIGN_SHADOW_TABLE(uncore, pvc_shadowed_regs);
2561 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2562 	} else if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55)) {
2563 		ASSIGN_FW_DOMAINS_TABLE(uncore, __dg2_fw_ranges);
2564 		ASSIGN_SHADOW_TABLE(uncore, dg2_shadowed_regs);
2565 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2566 	} else if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) {
2567 		ASSIGN_FW_DOMAINS_TABLE(uncore, __xehp_fw_ranges);
2568 		ASSIGN_SHADOW_TABLE(uncore, gen12_shadowed_regs);
2569 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2570 	} else if (GRAPHICS_VER(i915) >= 12) {
2571 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen12_fw_ranges);
2572 		ASSIGN_SHADOW_TABLE(uncore, gen12_shadowed_regs);
2573 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2574 	} else if (GRAPHICS_VER(i915) == 11) {
2575 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen11_fw_ranges);
2576 		ASSIGN_SHADOW_TABLE(uncore, gen11_shadowed_regs);
2577 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2578 	} else if (IS_GRAPHICS_VER(i915, 9, 10)) {
2579 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen9_fw_ranges);
2580 		ASSIGN_SHADOW_TABLE(uncore, gen8_shadowed_regs);
2581 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2582 	} else if (IS_CHERRYVIEW(i915)) {
2583 		ASSIGN_FW_DOMAINS_TABLE(uncore, __chv_fw_ranges);
2584 		ASSIGN_SHADOW_TABLE(uncore, gen8_shadowed_regs);
2585 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2586 	} else if (GRAPHICS_VER(i915) == 8) {
2587 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen6_fw_ranges);
2588 		ASSIGN_SHADOW_TABLE(uncore, gen8_shadowed_regs);
2589 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
2590 	} else if (IS_VALLEYVIEW(i915)) {
2591 		ASSIGN_FW_DOMAINS_TABLE(uncore, __vlv_fw_ranges);
2592 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen6);
2593 	} else if (IS_GRAPHICS_VER(i915, 6, 7)) {
2594 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen6_fw_ranges);
2595 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen6);
2596 	}
2597 
2598 	uncore->pmic_bus_access_nb.notifier_call = i915_pmic_bus_access_notifier;
2599 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
2600 
2601 	return 0;
2602 }
2603 
2604 int intel_uncore_init_mmio(struct intel_uncore *uncore)
2605 {
2606 	struct drm_i915_private *i915 = uncore->i915;
2607 	int ret;
2608 
2609 	/*
2610 	 * The boot firmware initializes local memory and assesses its health.
2611 	 * If memory training fails, the punit will have been instructed to
2612 	 * keep the GT powered down; we won't be able to communicate with it
2613 	 * and we should not continue with driver initialization.
2614 	 */
2615 	if (IS_DGFX(i915) &&
2616 	    !(__raw_uncore_read32(uncore, GU_CNTL) & LMEM_INIT)) {
2617 		drm_err(&i915->drm, "LMEM not initialized by firmware\n");
2618 		return -ENODEV;
2619 	}
2620 
2621 	if (GRAPHICS_VER(i915) > 5 && !intel_vgpu_active(i915))
2622 		uncore->flags |= UNCORE_HAS_FORCEWAKE;
2623 
2624 	if (!intel_uncore_has_forcewake(uncore)) {
2625 		uncore_raw_init(uncore);
2626 	} else {
2627 		ret = uncore_forcewake_init(uncore);
2628 		if (ret)
2629 			return ret;
2630 	}
2631 
2632 	/* make sure fw funcs are set if and only if we have fw*/
2633 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->fw_get_funcs);
2634 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.read_fw_domains);
2635 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.write_fw_domains);
2636 
2637 	if (HAS_FPGA_DBG_UNCLAIMED(i915))
2638 		uncore->flags |= UNCORE_HAS_FPGA_DBG_UNCLAIMED;
2639 
2640 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
2641 		uncore->flags |= UNCORE_HAS_DBG_UNCLAIMED;
2642 
2643 	if (IS_GRAPHICS_VER(i915, 6, 7))
2644 		uncore->flags |= UNCORE_HAS_FIFO;
2645 
2646 	/* clear out unclaimed reg detection bit */
2647 	if (intel_uncore_unclaimed_mmio(uncore))
2648 		drm_dbg(&i915->drm, "unclaimed mmio detected on uncore init, clearing\n");
2649 
2650 	return 0;
2651 }
2652 
2653 /*
2654  * We might have detected that some engines are fused off after we initialized
2655  * the forcewake domains. Prune them, to make sure they only reference existing
2656  * engines.
2657  */
2658 void intel_uncore_prune_engine_fw_domains(struct intel_uncore *uncore,
2659 					  struct intel_gt *gt)
2660 {
2661 	enum forcewake_domains fw_domains = uncore->fw_domains;
2662 	enum forcewake_domain_id domain_id;
2663 	int i;
2664 
2665 	if (!intel_uncore_has_forcewake(uncore) || GRAPHICS_VER(uncore->i915) < 11)
2666 		return;
2667 
2668 	for (i = 0; i < I915_MAX_VCS; i++) {
2669 		domain_id = FW_DOMAIN_ID_MEDIA_VDBOX0 + i;
2670 
2671 		if (HAS_ENGINE(gt, _VCS(i)))
2672 			continue;
2673 
2674 		/*
2675 		 * Starting with XeHP, the power well for an even-numbered
2676 		 * VDBOX is also used for shared units within the
2677 		 * media slice such as SFC.  So even if the engine
2678 		 * itself is fused off, we still need to initialize
2679 		 * the forcewake domain if any of the other engines
2680 		 * in the same media slice are present.
2681 		 */
2682 		if (GRAPHICS_VER_FULL(uncore->i915) >= IP_VER(12, 50) && i % 2 == 0) {
2683 			if ((i + 1 < I915_MAX_VCS) && HAS_ENGINE(gt, _VCS(i + 1)))
2684 				continue;
2685 
2686 			if (HAS_ENGINE(gt, _VECS(i / 2)))
2687 				continue;
2688 		}
2689 
2690 		if (fw_domains & BIT(domain_id))
2691 			fw_domain_fini(uncore, domain_id);
2692 	}
2693 
2694 	for (i = 0; i < I915_MAX_VECS; i++) {
2695 		domain_id = FW_DOMAIN_ID_MEDIA_VEBOX0 + i;
2696 
2697 		if (HAS_ENGINE(gt, _VECS(i)))
2698 			continue;
2699 
2700 		if (fw_domains & BIT(domain_id))
2701 			fw_domain_fini(uncore, domain_id);
2702 	}
2703 
2704 	if ((fw_domains & BIT(FW_DOMAIN_ID_GSC)) && !HAS_ENGINE(gt, GSC0))
2705 		fw_domain_fini(uncore, FW_DOMAIN_ID_GSC);
2706 }
2707 
2708 /*
2709  * The driver-initiated FLR is the highest level of reset that we can trigger
2710  * from within the driver. It is different from the PCI FLR in that it doesn't
2711  * fully reset the SGUnit and doesn't modify the PCI config space and therefore
2712  * it doesn't require a re-enumeration of the PCI BARs. However, the
2713  * driver-initiated FLR does still cause a reset of both GT and display and a
2714  * memory wipe of local and stolen memory, so recovery would require a full HW
2715  * re-init and saving/restoring (or re-populating) the wiped memory. Since we
2716  * perform the FLR as the very last action before releasing access to the HW
2717  * during the driver release flow, we don't attempt recovery at all, because
2718  * if/when a new instance of i915 is bound to the device it will do a full
2719  * re-init anyway.
2720  */
2721 static void driver_initiated_flr(struct intel_uncore *uncore)
2722 {
2723 	struct drm_i915_private *i915 = uncore->i915;
2724 	const unsigned int flr_timeout_ms = 3000; /* specs recommend a 3s wait */
2725 	int ret;
2726 
2727 	drm_dbg(&i915->drm, "Triggering Driver-FLR\n");
2728 
2729 	/*
2730 	 * Make sure any pending FLR requests have cleared by waiting for the
2731 	 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS
2732 	 * to make sure it's not still set from a prior attempt (it's a write to
2733 	 * clear bit).
2734 	 * Note that we should never be in a situation where a previous attempt
2735 	 * is still pending (unless the HW is totally dead), but better to be
2736 	 * safe in case something unexpected happens
2737 	 */
2738 	ret = intel_wait_for_register_fw(uncore, GU_CNTL, DRIVERFLR, 0, flr_timeout_ms);
2739 	if (ret) {
2740 		drm_err(&i915->drm,
2741 			"Failed to wait for Driver-FLR bit to clear! %d\n",
2742 			ret);
2743 		return;
2744 	}
2745 	intel_uncore_write_fw(uncore, GU_DEBUG, DRIVERFLR_STATUS);
2746 
2747 	/* Trigger the actual Driver-FLR */
2748 	intel_uncore_rmw_fw(uncore, GU_CNTL, 0, DRIVERFLR);
2749 
2750 	ret = intel_wait_for_register_fw(uncore, GU_DEBUG,
2751 					 DRIVERFLR_STATUS, DRIVERFLR_STATUS,
2752 					 flr_timeout_ms);
2753 	if (ret) {
2754 		drm_err(&i915->drm, "wait for Driver-FLR completion failed! %d\n", ret);
2755 		return;
2756 	}
2757 
2758 	intel_uncore_write_fw(uncore, GU_DEBUG, DRIVERFLR_STATUS);
2759 }
2760 
2761 /* Called via drm-managed action */
2762 void intel_uncore_fini_mmio(struct drm_device *dev, void *data)
2763 {
2764 	struct intel_uncore *uncore = data;
2765 
2766 	if (intel_uncore_has_forcewake(uncore)) {
2767 		iosf_mbi_punit_acquire();
2768 		iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
2769 			&uncore->pmic_bus_access_nb);
2770 		intel_uncore_forcewake_reset(uncore);
2771 		intel_uncore_fw_domains_fini(uncore);
2772 		iosf_mbi_punit_release();
2773 	}
2774 
2775 	if (intel_uncore_needs_flr_on_fini(uncore))
2776 		driver_initiated_flr(uncore);
2777 }
2778 
2779 /**
2780  * __intel_wait_for_register_fw - wait until register matches expected state
2781  * @uncore: the struct intel_uncore
2782  * @reg: the register to read
2783  * @mask: mask to apply to register value
2784  * @value: expected value
2785  * @fast_timeout_us: fast timeout in microsecond for atomic/tight wait
2786  * @slow_timeout_ms: slow timeout in millisecond
2787  * @out_value: optional placeholder to hold registry value
2788  *
2789  * This routine waits until the target register @reg contains the expected
2790  * @value after applying the @mask, i.e. it waits until ::
2791  *
2792  *     (intel_uncore_read_fw(uncore, reg) & mask) == value
2793  *
2794  * Otherwise, the wait will timeout after @slow_timeout_ms milliseconds.
2795  * For atomic context @slow_timeout_ms must be zero and @fast_timeout_us
2796  * must be not larger than 20,0000 microseconds.
2797  *
2798  * Note that this routine assumes the caller holds forcewake asserted, it is
2799  * not suitable for very long waits. See intel_wait_for_register() if you
2800  * wish to wait without holding forcewake for the duration (i.e. you expect
2801  * the wait to be slow).
2802  *
2803  * Return: 0 if the register matches the desired condition, or -ETIMEDOUT.
2804  */
2805 int __intel_wait_for_register_fw(struct intel_uncore *uncore,
2806 				 i915_reg_t reg,
2807 				 u32 mask,
2808 				 u32 value,
2809 				 unsigned int fast_timeout_us,
2810 				 unsigned int slow_timeout_ms,
2811 				 u32 *out_value)
2812 {
2813 	u32 reg_value = 0;
2814 #define done (((reg_value = intel_uncore_read_fw(uncore, reg)) & mask) == value)
2815 	int ret;
2816 
2817 	/* Catch any overuse of this function */
2818 	might_sleep_if(slow_timeout_ms);
2819 	GEM_BUG_ON(fast_timeout_us > 20000);
2820 	GEM_BUG_ON(!fast_timeout_us && !slow_timeout_ms);
2821 
2822 	ret = -ETIMEDOUT;
2823 	if (fast_timeout_us && fast_timeout_us <= 20000)
2824 		ret = _wait_for_atomic(done, fast_timeout_us, 0);
2825 	if (ret && slow_timeout_ms)
2826 		ret = wait_for(done, slow_timeout_ms);
2827 
2828 	if (out_value)
2829 		*out_value = reg_value;
2830 
2831 	return ret;
2832 #undef done
2833 }
2834 
2835 /**
2836  * __intel_wait_for_register - wait until register matches expected state
2837  * @uncore: the struct intel_uncore
2838  * @reg: the register to read
2839  * @mask: mask to apply to register value
2840  * @value: expected value
2841  * @fast_timeout_us: fast timeout in microsecond for atomic/tight wait
2842  * @slow_timeout_ms: slow timeout in millisecond
2843  * @out_value: optional placeholder to hold registry value
2844  *
2845  * This routine waits until the target register @reg contains the expected
2846  * @value after applying the @mask, i.e. it waits until ::
2847  *
2848  *     (intel_uncore_read(uncore, reg) & mask) == value
2849  *
2850  * Otherwise, the wait will timeout after @timeout_ms milliseconds.
2851  *
2852  * Return: 0 if the register matches the desired condition, or -ETIMEDOUT.
2853  */
2854 int __intel_wait_for_register(struct intel_uncore *uncore,
2855 			      i915_reg_t reg,
2856 			      u32 mask,
2857 			      u32 value,
2858 			      unsigned int fast_timeout_us,
2859 			      unsigned int slow_timeout_ms,
2860 			      u32 *out_value)
2861 {
2862 	unsigned fw =
2863 		intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
2864 	u32 reg_value;
2865 	int ret;
2866 
2867 	might_sleep_if(slow_timeout_ms);
2868 
2869 	spin_lock_irq(&uncore->lock);
2870 	intel_uncore_forcewake_get__locked(uncore, fw);
2871 
2872 	ret = __intel_wait_for_register_fw(uncore,
2873 					   reg, mask, value,
2874 					   fast_timeout_us, 0, &reg_value);
2875 
2876 	intel_uncore_forcewake_put__locked(uncore, fw);
2877 	spin_unlock_irq(&uncore->lock);
2878 
2879 	if (ret && slow_timeout_ms)
2880 		ret = __wait_for(reg_value = intel_uncore_read_notrace(uncore,
2881 								       reg),
2882 				 (reg_value & mask) == value,
2883 				 slow_timeout_ms * 1000, 10, 1000);
2884 
2885 	/* just trace the final value */
2886 	trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
2887 
2888 	if (out_value)
2889 		*out_value = reg_value;
2890 
2891 	return ret;
2892 }
2893 
2894 bool intel_uncore_unclaimed_mmio(struct intel_uncore *uncore)
2895 {
2896 	bool ret;
2897 
2898 	if (!uncore->debug)
2899 		return false;
2900 
2901 	spin_lock_irq(&uncore->debug->lock);
2902 	ret = check_for_unclaimed_mmio(uncore);
2903 	spin_unlock_irq(&uncore->debug->lock);
2904 
2905 	return ret;
2906 }
2907 
2908 bool
2909 intel_uncore_arm_unclaimed_mmio_detection(struct intel_uncore *uncore)
2910 {
2911 	bool ret = false;
2912 
2913 	if (drm_WARN_ON(&uncore->i915->drm, !uncore->debug))
2914 		return false;
2915 
2916 	spin_lock_irq(&uncore->debug->lock);
2917 
2918 	if (unlikely(uncore->debug->unclaimed_mmio_check <= 0))
2919 		goto out;
2920 
2921 	if (unlikely(check_for_unclaimed_mmio(uncore))) {
2922 		if (!uncore->i915->params.mmio_debug) {
2923 			drm_dbg(&uncore->i915->drm,
2924 				"Unclaimed register detected, "
2925 				"enabling oneshot unclaimed register reporting. "
2926 				"Please use i915.mmio_debug=N for more information.\n");
2927 			uncore->i915->params.mmio_debug++;
2928 		}
2929 		uncore->debug->unclaimed_mmio_check--;
2930 		ret = true;
2931 	}
2932 
2933 out:
2934 	spin_unlock_irq(&uncore->debug->lock);
2935 
2936 	return ret;
2937 }
2938 
2939 /**
2940  * intel_uncore_forcewake_for_reg - which forcewake domains are needed to access
2941  * 				    a register
2942  * @uncore: pointer to struct intel_uncore
2943  * @reg: register in question
2944  * @op: operation bitmask of FW_REG_READ and/or FW_REG_WRITE
2945  *
2946  * Returns a set of forcewake domains required to be taken with for example
2947  * intel_uncore_forcewake_get for the specified register to be accessible in the
2948  * specified mode (read, write or read/write) with raw mmio accessors.
2949  *
2950  * NOTE: On Gen6 and Gen7 write forcewake domain (FORCEWAKE_RENDER) requires the
2951  * callers to do FIFO management on their own or risk losing writes.
2952  */
2953 enum forcewake_domains
2954 intel_uncore_forcewake_for_reg(struct intel_uncore *uncore,
2955 			       i915_reg_t reg, unsigned int op)
2956 {
2957 	enum forcewake_domains fw_domains = 0;
2958 
2959 	drm_WARN_ON(&uncore->i915->drm, !op);
2960 
2961 	if (!intel_uncore_has_forcewake(uncore))
2962 		return 0;
2963 
2964 	if (op & FW_REG_READ)
2965 		fw_domains = uncore->funcs.read_fw_domains(uncore, reg);
2966 
2967 	if (op & FW_REG_WRITE)
2968 		fw_domains |= uncore->funcs.write_fw_domains(uncore, reg);
2969 
2970 	drm_WARN_ON(&uncore->i915->drm, fw_domains & ~uncore->fw_domains);
2971 
2972 	return fw_domains;
2973 }
2974 
2975 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2976 #include "selftests/mock_uncore.c"
2977 #include "selftests/intel_uncore.c"
2978 #endif
2979