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