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