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