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