xref: /openbsd-src/sys/dev/pci/drm/i915/intel_uncore.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
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 "i915_drv.h"
28 #include "i915_trace.h"
29 #include "i915_vgpu.h"
30 #include "intel_pm.h"
31 
32 #define FORCEWAKE_ACK_TIMEOUT_MS 50
33 #define GT_FIFO_TIMEOUT_MS	 10
34 
35 #define __raw_posting_read(...) ((void)__raw_uncore_read32(__VA_ARGS__))
36 
37 void
38 intel_uncore_mmio_debug_init_early(struct intel_uncore_mmio_debug *mmio_debug)
39 {
40 	mtx_init(&mmio_debug->lock, IPL_TTY);
41 	mmio_debug->unclaimed_mmio_check = 1;
42 }
43 
44 static void mmio_debug_suspend(struct intel_uncore_mmio_debug *mmio_debug)
45 {
46 	lockdep_assert_held(&mmio_debug->lock);
47 
48 	/* Save and disable mmio debugging for the user bypass */
49 	if (!mmio_debug->suspend_count++) {
50 		mmio_debug->saved_mmio_check = mmio_debug->unclaimed_mmio_check;
51 		mmio_debug->unclaimed_mmio_check = 0;
52 	}
53 }
54 
55 static void mmio_debug_resume(struct intel_uncore_mmio_debug *mmio_debug)
56 {
57 	lockdep_assert_held(&mmio_debug->lock);
58 
59 	if (!--mmio_debug->suspend_count)
60 		mmio_debug->unclaimed_mmio_check = mmio_debug->saved_mmio_check;
61 }
62 
63 static const char * const forcewake_domain_names[] = {
64 	"render",
65 	"blitter",
66 	"media",
67 	"vdbox0",
68 	"vdbox1",
69 	"vdbox2",
70 	"vdbox3",
71 	"vebox0",
72 	"vebox1",
73 };
74 
75 const char *
76 intel_uncore_forcewake_domain_to_str(const enum forcewake_domain_id id)
77 {
78 	BUILD_BUG_ON(ARRAY_SIZE(forcewake_domain_names) != FW_DOMAIN_ID_COUNT);
79 
80 	if (id >= 0 && id < FW_DOMAIN_ID_COUNT)
81 		return forcewake_domain_names[id];
82 
83 	WARN_ON(id);
84 
85 	return "unknown";
86 }
87 
88 #define fw_ack(d) readl((d)->reg_ack)
89 #define fw_set(d, val) writel(_MASKED_BIT_ENABLE((val)), (d)->reg_set)
90 #define fw_clear(d, val) writel(_MASKED_BIT_DISABLE((val)), (d)->reg_set)
91 
92 static inline void
93 fw_domain_reset(const struct intel_uncore_forcewake_domain *d)
94 {
95 	/*
96 	 * We don't really know if the powerwell for the forcewake domain we are
97 	 * trying to reset here does exist at this point (engines could be fused
98 	 * off in ICL+), so no waiting for acks
99 	 */
100 	/* WaRsClearFWBitsAtReset:bdw,skl */
101 	fw_clear(d, 0xffff);
102 }
103 
104 static inline void
105 fw_domain_arm_timer(struct intel_uncore_forcewake_domain *d)
106 {
107 	GEM_BUG_ON(d->uncore->fw_domains_timer & d->mask);
108 	d->uncore->fw_domains_timer |= d->mask;
109 	d->wake_count++;
110 #ifdef __linux__
111 	hrtimer_start_range_ns(&d->timer,
112 			       NSEC_PER_MSEC,
113 			       NSEC_PER_MSEC,
114 			       HRTIMER_MODE_REL);
115 #else
116 	timeout_add_msec(&d->timer, 1);
117 #endif
118 }
119 
120 static inline int
121 __wait_for_ack(const struct intel_uncore_forcewake_domain *d,
122 	       const u32 ack,
123 	       const u32 value)
124 {
125 	return wait_for_atomic((fw_ack(d) & ack) == value,
126 			       FORCEWAKE_ACK_TIMEOUT_MS);
127 }
128 
129 static inline int
130 wait_ack_clear(const struct intel_uncore_forcewake_domain *d,
131 	       const u32 ack)
132 {
133 	return __wait_for_ack(d, ack, 0);
134 }
135 
136 static inline int
137 wait_ack_set(const struct intel_uncore_forcewake_domain *d,
138 	     const u32 ack)
139 {
140 	return __wait_for_ack(d, ack, ack);
141 }
142 
143 static inline void
144 fw_domain_wait_ack_clear(const struct intel_uncore_forcewake_domain *d)
145 {
146 	if (wait_ack_clear(d, FORCEWAKE_KERNEL)) {
147 		DRM_ERROR("%s: timed out waiting for forcewake ack to clear.\n",
148 			  intel_uncore_forcewake_domain_to_str(d->id));
149 		add_taint_for_CI(TAINT_WARN); /* CI now unreliable */
150 	}
151 }
152 
153 enum ack_type {
154 	ACK_CLEAR = 0,
155 	ACK_SET
156 };
157 
158 static int
159 fw_domain_wait_ack_with_fallback(const struct intel_uncore_forcewake_domain *d,
160 				 const enum ack_type type)
161 {
162 	const u32 ack_bit = FORCEWAKE_KERNEL;
163 	const u32 value = type == ACK_SET ? ack_bit : 0;
164 	unsigned int pass;
165 	bool ack_detected;
166 
167 	/*
168 	 * There is a possibility of driver's wake request colliding
169 	 * with hardware's own wake requests and that can cause
170 	 * hardware to not deliver the driver's ack message.
171 	 *
172 	 * Use a fallback bit toggle to kick the gpu state machine
173 	 * in the hope that the original ack will be delivered along with
174 	 * the fallback ack.
175 	 *
176 	 * This workaround is described in HSDES #1604254524 and it's known as:
177 	 * WaRsForcewakeAddDelayForAck:skl,bxt,kbl,glk,cfl,cnl,icl
178 	 * although the name is a bit misleading.
179 	 */
180 
181 	pass = 1;
182 	do {
183 		wait_ack_clear(d, FORCEWAKE_KERNEL_FALLBACK);
184 
185 		fw_set(d, FORCEWAKE_KERNEL_FALLBACK);
186 		/* Give gt some time to relax before the polling frenzy */
187 		udelay(10 * pass);
188 		wait_ack_set(d, FORCEWAKE_KERNEL_FALLBACK);
189 
190 		ack_detected = (fw_ack(d) & ack_bit) == value;
191 
192 		fw_clear(d, FORCEWAKE_KERNEL_FALLBACK);
193 	} while (!ack_detected && pass++ < 10);
194 
195 	DRM_DEBUG_DRIVER("%s had to use fallback to %s ack, 0x%x (passes %u)\n",
196 			 intel_uncore_forcewake_domain_to_str(d->id),
197 			 type == ACK_SET ? "set" : "clear",
198 			 fw_ack(d),
199 			 pass);
200 
201 	return ack_detected ? 0 : -ETIMEDOUT;
202 }
203 
204 static inline void
205 fw_domain_wait_ack_clear_fallback(const struct intel_uncore_forcewake_domain *d)
206 {
207 	if (likely(!wait_ack_clear(d, FORCEWAKE_KERNEL)))
208 		return;
209 
210 	if (fw_domain_wait_ack_with_fallback(d, ACK_CLEAR))
211 		fw_domain_wait_ack_clear(d);
212 }
213 
214 static inline void
215 fw_domain_get(const struct intel_uncore_forcewake_domain *d)
216 {
217 	fw_set(d, FORCEWAKE_KERNEL);
218 }
219 
220 static inline void
221 fw_domain_wait_ack_set(const struct intel_uncore_forcewake_domain *d)
222 {
223 	if (wait_ack_set(d, FORCEWAKE_KERNEL)) {
224 		DRM_ERROR("%s: timed out waiting for forcewake ack request.\n",
225 			  intel_uncore_forcewake_domain_to_str(d->id));
226 		add_taint_for_CI(TAINT_WARN); /* CI now unreliable */
227 	}
228 }
229 
230 static inline void
231 fw_domain_wait_ack_set_fallback(const struct intel_uncore_forcewake_domain *d)
232 {
233 	if (likely(!wait_ack_set(d, FORCEWAKE_KERNEL)))
234 		return;
235 
236 	if (fw_domain_wait_ack_with_fallback(d, ACK_SET))
237 		fw_domain_wait_ack_set(d);
238 }
239 
240 static inline void
241 fw_domain_put(const struct intel_uncore_forcewake_domain *d)
242 {
243 	fw_clear(d, FORCEWAKE_KERNEL);
244 }
245 
246 static void
247 fw_domains_get(struct intel_uncore *uncore, enum forcewake_domains fw_domains)
248 {
249 	struct intel_uncore_forcewake_domain *d;
250 	unsigned int tmp;
251 
252 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
253 
254 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp) {
255 		fw_domain_wait_ack_clear(d);
256 		fw_domain_get(d);
257 	}
258 
259 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
260 		fw_domain_wait_ack_set(d);
261 
262 	uncore->fw_domains_active |= fw_domains;
263 }
264 
265 static void
266 fw_domains_get_with_fallback(struct intel_uncore *uncore,
267 			     enum forcewake_domains fw_domains)
268 {
269 	struct intel_uncore_forcewake_domain *d;
270 	unsigned int tmp;
271 
272 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
273 
274 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp) {
275 		fw_domain_wait_ack_clear_fallback(d);
276 		fw_domain_get(d);
277 	}
278 
279 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
280 		fw_domain_wait_ack_set_fallback(d);
281 
282 	uncore->fw_domains_active |= fw_domains;
283 }
284 
285 static void
286 fw_domains_put(struct intel_uncore *uncore, enum forcewake_domains fw_domains)
287 {
288 	struct intel_uncore_forcewake_domain *d;
289 	unsigned int tmp;
290 
291 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
292 
293 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
294 		fw_domain_put(d);
295 
296 	uncore->fw_domains_active &= ~fw_domains;
297 }
298 
299 static void
300 fw_domains_reset(struct intel_uncore *uncore,
301 		 enum forcewake_domains fw_domains)
302 {
303 	struct intel_uncore_forcewake_domain *d;
304 	unsigned int tmp;
305 
306 	if (!fw_domains)
307 		return;
308 
309 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
310 
311 	for_each_fw_domain_masked(d, fw_domains, uncore, tmp)
312 		fw_domain_reset(d);
313 }
314 
315 static inline u32 gt_thread_status(struct intel_uncore *uncore)
316 {
317 	u32 val;
318 
319 	val = __raw_uncore_read32(uncore, GEN6_GT_THREAD_STATUS_REG);
320 	val &= GEN6_GT_THREAD_STATUS_CORE_MASK;
321 
322 	return val;
323 }
324 
325 static void __gen6_gt_wait_for_thread_c0(struct intel_uncore *uncore)
326 {
327 	/*
328 	 * w/a for a sporadic read returning 0 by waiting for the GT
329 	 * thread to wake up.
330 	 */
331 	drm_WARN_ONCE(&uncore->i915->drm,
332 		      wait_for_atomic_us(gt_thread_status(uncore) == 0, 5000),
333 		      "GT thread status wait timed out\n");
334 }
335 
336 static void fw_domains_get_with_thread_status(struct intel_uncore *uncore,
337 					      enum forcewake_domains fw_domains)
338 {
339 	fw_domains_get(uncore, fw_domains);
340 
341 	/* WaRsForcewakeWaitTC0:snb,ivb,hsw,bdw,vlv */
342 	__gen6_gt_wait_for_thread_c0(uncore);
343 }
344 
345 static inline u32 fifo_free_entries(struct intel_uncore *uncore)
346 {
347 	u32 count = __raw_uncore_read32(uncore, GTFIFOCTL);
348 
349 	return count & GT_FIFO_FREE_ENTRIES_MASK;
350 }
351 
352 static void __gen6_gt_wait_for_fifo(struct intel_uncore *uncore)
353 {
354 	u32 n;
355 
356 	/* On VLV, FIFO will be shared by both SW and HW.
357 	 * So, we need to read the FREE_ENTRIES everytime */
358 	if (IS_VALLEYVIEW(uncore->i915))
359 		n = fifo_free_entries(uncore);
360 	else
361 		n = uncore->fifo_count;
362 
363 	if (n <= GT_FIFO_NUM_RESERVED_ENTRIES) {
364 		if (wait_for_atomic((n = fifo_free_entries(uncore)) >
365 				    GT_FIFO_NUM_RESERVED_ENTRIES,
366 				    GT_FIFO_TIMEOUT_MS)) {
367 			drm_dbg(&uncore->i915->drm,
368 				"GT_FIFO timeout, entries: %u\n", n);
369 			return;
370 		}
371 	}
372 
373 	uncore->fifo_count = n - 1;
374 }
375 
376 #ifdef __linux__
377 
378 static enum hrtimer_restart
379 intel_uncore_fw_release_timer(struct hrtimer *timer)
380 {
381 	struct intel_uncore_forcewake_domain *domain =
382 	       container_of(timer, struct intel_uncore_forcewake_domain, timer);
383 	struct intel_uncore *uncore = domain->uncore;
384 	unsigned long irqflags;
385 
386 	assert_rpm_device_not_suspended(uncore->rpm);
387 
388 	if (xchg(&domain->active, false))
389 		return HRTIMER_RESTART;
390 
391 	spin_lock_irqsave(&uncore->lock, irqflags);
392 
393 	uncore->fw_domains_timer &= ~domain->mask;
394 
395 	GEM_BUG_ON(!domain->wake_count);
396 	if (--domain->wake_count == 0)
397 		uncore->funcs.force_wake_put(uncore, domain->mask);
398 
399 	spin_unlock_irqrestore(&uncore->lock, irqflags);
400 
401 	return HRTIMER_NORESTART;
402 }
403 
404 #else
405 
406 void
407 intel_uncore_fw_release_timer(void *arg)
408 {
409 	struct intel_uncore_forcewake_domain *domain = arg;
410 	struct intel_uncore *uncore = domain->uncore;
411 	unsigned long irqflags;
412 
413 	assert_rpm_device_not_suspended(uncore->rpm);
414 
415 	if (xchg(&domain->active, false))
416 		return;
417 
418 	spin_lock_irqsave(&uncore->lock, irqflags);
419 
420 	uncore->fw_domains_timer &= ~domain->mask;
421 
422 	GEM_BUG_ON(!domain->wake_count);
423 	if (--domain->wake_count == 0)
424 		uncore->funcs.force_wake_put(uncore, domain->mask);
425 
426 	spin_unlock_irqrestore(&uncore->lock, irqflags);
427 }
428 
429 #endif
430 
431 /* Note callers must have acquired the PUNIT->PMIC bus, before calling this. */
432 static unsigned int
433 intel_uncore_forcewake_reset(struct intel_uncore *uncore)
434 {
435 	unsigned long irqflags;
436 	struct intel_uncore_forcewake_domain *domain;
437 	int retry_count = 100;
438 	enum forcewake_domains fw, active_domains;
439 
440 	iosf_mbi_assert_punit_acquired();
441 
442 	/* Hold uncore.lock across reset to prevent any register access
443 	 * with forcewake not set correctly. Wait until all pending
444 	 * timers are run before holding.
445 	 */
446 	while (1) {
447 		unsigned int tmp;
448 
449 		active_domains = 0;
450 
451 		for_each_fw_domain(domain, uncore, tmp) {
452 			smp_store_mb(domain->active, false);
453 			if (hrtimer_cancel(&domain->timer) == 0)
454 				continue;
455 
456 			intel_uncore_fw_release_timer(&domain->timer);
457 		}
458 
459 		spin_lock_irqsave(&uncore->lock, irqflags);
460 
461 		for_each_fw_domain(domain, uncore, tmp) {
462 			if (hrtimer_active(&domain->timer))
463 				active_domains |= domain->mask;
464 		}
465 
466 		if (active_domains == 0)
467 			break;
468 
469 		if (--retry_count == 0) {
470 			drm_err(&uncore->i915->drm, "Timed out waiting for forcewake timers to finish\n");
471 			break;
472 		}
473 
474 		spin_unlock_irqrestore(&uncore->lock, irqflags);
475 		cond_resched();
476 	}
477 
478 	drm_WARN_ON(&uncore->i915->drm, active_domains);
479 
480 	fw = uncore->fw_domains_active;
481 	if (fw)
482 		uncore->funcs.force_wake_put(uncore, fw);
483 
484 	fw_domains_reset(uncore, uncore->fw_domains);
485 	assert_forcewakes_inactive(uncore);
486 
487 	spin_unlock_irqrestore(&uncore->lock, irqflags);
488 
489 	return fw; /* track the lost user forcewake domains */
490 }
491 
492 static bool
493 fpga_check_for_unclaimed_mmio(struct intel_uncore *uncore)
494 {
495 	u32 dbg;
496 
497 	dbg = __raw_uncore_read32(uncore, FPGA_DBG);
498 	if (likely(!(dbg & FPGA_DBG_RM_NOCLAIM)))
499 		return false;
500 
501 	__raw_uncore_write32(uncore, FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
502 
503 	return true;
504 }
505 
506 static bool
507 vlv_check_for_unclaimed_mmio(struct intel_uncore *uncore)
508 {
509 	u32 cer;
510 
511 	cer = __raw_uncore_read32(uncore, CLAIM_ER);
512 	if (likely(!(cer & (CLAIM_ER_OVERFLOW | CLAIM_ER_CTR_MASK))))
513 		return false;
514 
515 	__raw_uncore_write32(uncore, CLAIM_ER, CLAIM_ER_CLR);
516 
517 	return true;
518 }
519 
520 static bool
521 gen6_check_for_fifo_debug(struct intel_uncore *uncore)
522 {
523 	u32 fifodbg;
524 
525 	fifodbg = __raw_uncore_read32(uncore, GTFIFODBG);
526 
527 	if (unlikely(fifodbg)) {
528 		drm_dbg(&uncore->i915->drm, "GTFIFODBG = 0x08%x\n", fifodbg);
529 		__raw_uncore_write32(uncore, GTFIFODBG, fifodbg);
530 	}
531 
532 	return fifodbg;
533 }
534 
535 static bool
536 check_for_unclaimed_mmio(struct intel_uncore *uncore)
537 {
538 	bool ret = false;
539 
540 	lockdep_assert_held(&uncore->debug->lock);
541 
542 	if (uncore->debug->suspend_count)
543 		return false;
544 
545 	if (intel_uncore_has_fpga_dbg_unclaimed(uncore))
546 		ret |= fpga_check_for_unclaimed_mmio(uncore);
547 
548 	if (intel_uncore_has_dbg_unclaimed(uncore))
549 		ret |= vlv_check_for_unclaimed_mmio(uncore);
550 
551 	if (intel_uncore_has_fifo(uncore))
552 		ret |= gen6_check_for_fifo_debug(uncore);
553 
554 	return ret;
555 }
556 
557 static void forcewake_early_sanitize(struct intel_uncore *uncore,
558 				     unsigned int restore_forcewake)
559 {
560 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
561 
562 	/* WaDisableShadowRegForCpd:chv */
563 	if (IS_CHERRYVIEW(uncore->i915)) {
564 		__raw_uncore_write32(uncore, GTFIFOCTL,
565 				     __raw_uncore_read32(uncore, GTFIFOCTL) |
566 				     GT_FIFO_CTL_BLOCK_ALL_POLICY_STALL |
567 				     GT_FIFO_CTL_RC6_POLICY_STALL);
568 	}
569 
570 	iosf_mbi_punit_acquire();
571 	intel_uncore_forcewake_reset(uncore);
572 	if (restore_forcewake) {
573 		spin_lock_irq(&uncore->lock);
574 		uncore->funcs.force_wake_get(uncore, restore_forcewake);
575 
576 		if (intel_uncore_has_fifo(uncore))
577 			uncore->fifo_count = fifo_free_entries(uncore);
578 		spin_unlock_irq(&uncore->lock);
579 	}
580 	iosf_mbi_punit_release();
581 }
582 
583 void intel_uncore_suspend(struct intel_uncore *uncore)
584 {
585 	if (!intel_uncore_has_forcewake(uncore))
586 		return;
587 
588 	iosf_mbi_punit_acquire();
589 	iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
590 		&uncore->pmic_bus_access_nb);
591 	uncore->fw_domains_saved = intel_uncore_forcewake_reset(uncore);
592 	iosf_mbi_punit_release();
593 }
594 
595 void intel_uncore_resume_early(struct intel_uncore *uncore)
596 {
597 	unsigned int restore_forcewake;
598 
599 	if (intel_uncore_unclaimed_mmio(uncore))
600 		drm_dbg(&uncore->i915->drm, "unclaimed mmio detected on resume, clearing\n");
601 
602 	if (!intel_uncore_has_forcewake(uncore))
603 		return;
604 
605 	restore_forcewake = fetch_and_zero(&uncore->fw_domains_saved);
606 	forcewake_early_sanitize(uncore, restore_forcewake);
607 
608 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
609 }
610 
611 void intel_uncore_runtime_resume(struct intel_uncore *uncore)
612 {
613 	if (!intel_uncore_has_forcewake(uncore))
614 		return;
615 
616 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
617 }
618 
619 static void __intel_uncore_forcewake_get(struct intel_uncore *uncore,
620 					 enum forcewake_domains fw_domains)
621 {
622 	struct intel_uncore_forcewake_domain *domain;
623 	unsigned int tmp;
624 
625 	fw_domains &= uncore->fw_domains;
626 
627 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
628 		if (domain->wake_count++) {
629 			fw_domains &= ~domain->mask;
630 			domain->active = true;
631 		}
632 	}
633 
634 	if (fw_domains)
635 		uncore->funcs.force_wake_get(uncore, fw_domains);
636 }
637 
638 /**
639  * intel_uncore_forcewake_get - grab forcewake domain references
640  * @uncore: the intel_uncore structure
641  * @fw_domains: forcewake domains to get reference on
642  *
643  * This function can be used get GT's forcewake domain references.
644  * Normal register access will handle the forcewake domains automatically.
645  * However if some sequence requires the GT to not power down a particular
646  * forcewake domains this function should be called at the beginning of the
647  * sequence. And subsequently the reference should be dropped by symmetric
648  * call to intel_unforce_forcewake_put(). Usually caller wants all the domains
649  * to be kept awake so the @fw_domains would be then FORCEWAKE_ALL.
650  */
651 void intel_uncore_forcewake_get(struct intel_uncore *uncore,
652 				enum forcewake_domains fw_domains)
653 {
654 	unsigned long irqflags;
655 
656 	if (!uncore->funcs.force_wake_get)
657 		return;
658 
659 	assert_rpm_wakelock_held(uncore->rpm);
660 
661 	spin_lock_irqsave(&uncore->lock, irqflags);
662 	__intel_uncore_forcewake_get(uncore, fw_domains);
663 	spin_unlock_irqrestore(&uncore->lock, irqflags);
664 }
665 
666 /**
667  * intel_uncore_forcewake_user_get - claim forcewake on behalf of userspace
668  * @uncore: the intel_uncore structure
669  *
670  * This function is a wrapper around intel_uncore_forcewake_get() to acquire
671  * the GT powerwell and in the process disable our debugging for the
672  * duration of userspace's bypass.
673  */
674 void intel_uncore_forcewake_user_get(struct intel_uncore *uncore)
675 {
676 	spin_lock_irq(&uncore->lock);
677 	if (!uncore->user_forcewake_count++) {
678 		intel_uncore_forcewake_get__locked(uncore, FORCEWAKE_ALL);
679 		spin_lock(&uncore->debug->lock);
680 		mmio_debug_suspend(uncore->debug);
681 		spin_unlock(&uncore->debug->lock);
682 	}
683 	spin_unlock_irq(&uncore->lock);
684 }
685 
686 /**
687  * intel_uncore_forcewake_user_put - release forcewake on behalf of userspace
688  * @uncore: the intel_uncore structure
689  *
690  * This function complements intel_uncore_forcewake_user_get() and releases
691  * the GT powerwell taken on behalf of the userspace bypass.
692  */
693 void intel_uncore_forcewake_user_put(struct intel_uncore *uncore)
694 {
695 	spin_lock_irq(&uncore->lock);
696 	if (!--uncore->user_forcewake_count) {
697 		spin_lock(&uncore->debug->lock);
698 		mmio_debug_resume(uncore->debug);
699 
700 		if (check_for_unclaimed_mmio(uncore))
701 			dev_info(uncore->i915->drm.dev,
702 				 "Invalid mmio detected during user access\n");
703 		spin_unlock(&uncore->debug->lock);
704 
705 		intel_uncore_forcewake_put__locked(uncore, FORCEWAKE_ALL);
706 	}
707 	spin_unlock_irq(&uncore->lock);
708 }
709 
710 /**
711  * intel_uncore_forcewake_get__locked - grab forcewake domain references
712  * @uncore: the intel_uncore structure
713  * @fw_domains: forcewake domains to get reference on
714  *
715  * See intel_uncore_forcewake_get(). This variant places the onus
716  * on the caller to explicitly handle the dev_priv->uncore.lock spinlock.
717  */
718 void intel_uncore_forcewake_get__locked(struct intel_uncore *uncore,
719 					enum forcewake_domains fw_domains)
720 {
721 	lockdep_assert_held(&uncore->lock);
722 
723 	if (!uncore->funcs.force_wake_get)
724 		return;
725 
726 	__intel_uncore_forcewake_get(uncore, fw_domains);
727 }
728 
729 static void __intel_uncore_forcewake_put(struct intel_uncore *uncore,
730 					 enum forcewake_domains fw_domains)
731 {
732 	struct intel_uncore_forcewake_domain *domain;
733 	unsigned int tmp;
734 
735 	fw_domains &= uncore->fw_domains;
736 
737 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
738 		GEM_BUG_ON(!domain->wake_count);
739 
740 		if (--domain->wake_count) {
741 			domain->active = true;
742 			continue;
743 		}
744 
745 		fw_domain_arm_timer(domain);
746 	}
747 }
748 
749 /**
750  * intel_uncore_forcewake_put - release a forcewake domain reference
751  * @uncore: the intel_uncore structure
752  * @fw_domains: forcewake domains to put references
753  *
754  * This function drops the device-level forcewakes for specified
755  * domains obtained by intel_uncore_forcewake_get().
756  */
757 void intel_uncore_forcewake_put(struct intel_uncore *uncore,
758 				enum forcewake_domains fw_domains)
759 {
760 	unsigned long irqflags;
761 
762 	if (!uncore->funcs.force_wake_put)
763 		return;
764 
765 	spin_lock_irqsave(&uncore->lock, irqflags);
766 	__intel_uncore_forcewake_put(uncore, fw_domains);
767 	spin_unlock_irqrestore(&uncore->lock, irqflags);
768 }
769 
770 /**
771  * intel_uncore_forcewake_put__locked - grab forcewake domain references
772  * @uncore: the intel_uncore structure
773  * @fw_domains: forcewake domains to get reference on
774  *
775  * See intel_uncore_forcewake_put(). This variant places the onus
776  * on the caller to explicitly handle the dev_priv->uncore.lock spinlock.
777  */
778 void intel_uncore_forcewake_put__locked(struct intel_uncore *uncore,
779 					enum forcewake_domains fw_domains)
780 {
781 	lockdep_assert_held(&uncore->lock);
782 
783 	if (!uncore->funcs.force_wake_put)
784 		return;
785 
786 	__intel_uncore_forcewake_put(uncore, fw_domains);
787 }
788 
789 void assert_forcewakes_inactive(struct intel_uncore *uncore)
790 {
791 	if (!uncore->funcs.force_wake_get)
792 		return;
793 
794 	drm_WARN(&uncore->i915->drm, uncore->fw_domains_active,
795 		 "Expected all fw_domains to be inactive, but %08x are still on\n",
796 		 uncore->fw_domains_active);
797 }
798 
799 void assert_forcewakes_active(struct intel_uncore *uncore,
800 			      enum forcewake_domains fw_domains)
801 {
802 	struct intel_uncore_forcewake_domain *domain;
803 	unsigned int tmp;
804 
805 	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
806 		return;
807 
808 	if (!uncore->funcs.force_wake_get)
809 		return;
810 
811 	spin_lock_irq(&uncore->lock);
812 
813 	assert_rpm_wakelock_held(uncore->rpm);
814 
815 	fw_domains &= uncore->fw_domains;
816 	drm_WARN(&uncore->i915->drm, fw_domains & ~uncore->fw_domains_active,
817 		 "Expected %08x fw_domains to be active, but %08x are off\n",
818 		 fw_domains, fw_domains & ~uncore->fw_domains_active);
819 
820 	/*
821 	 * Check that the caller has an explicit wakeref and we don't mistake
822 	 * it for the auto wakeref.
823 	 */
824 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp) {
825 		unsigned int actual = READ_ONCE(domain->wake_count);
826 		unsigned int expect = 1;
827 
828 		if (uncore->fw_domains_timer & domain->mask)
829 			expect++; /* pending automatic release */
830 
831 		if (drm_WARN(&uncore->i915->drm, actual < expect,
832 			     "Expected domain %d to be held awake by caller, count=%d\n",
833 			     domain->id, actual))
834 			break;
835 	}
836 
837 	spin_unlock_irq(&uncore->lock);
838 }
839 
840 /* We give fast paths for the really cool registers */
841 #define NEEDS_FORCE_WAKE(reg) ((reg) < 0x40000)
842 
843 #define __gen6_reg_read_fw_domains(uncore, offset) \
844 ({ \
845 	enum forcewake_domains __fwd; \
846 	if (NEEDS_FORCE_WAKE(offset)) \
847 		__fwd = FORCEWAKE_RENDER; \
848 	else \
849 		__fwd = 0; \
850 	__fwd; \
851 })
852 
853 static int fw_range_cmp(u32 offset, const struct intel_forcewake_range *entry)
854 {
855 	if (offset < entry->start)
856 		return -1;
857 	else if (offset > entry->end)
858 		return 1;
859 	else
860 		return 0;
861 }
862 
863 /* Copied and "macroized" from lib/bsearch.c */
864 #define BSEARCH(key, base, num, cmp) ({                                 \
865 	unsigned int start__ = 0, end__ = (num);                        \
866 	typeof(base) result__ = NULL;                                   \
867 	while (start__ < end__) {                                       \
868 		unsigned int mid__ = start__ + (end__ - start__) / 2;   \
869 		int ret__ = (cmp)((key), (base) + mid__);               \
870 		if (ret__ < 0) {                                        \
871 			end__ = mid__;                                  \
872 		} else if (ret__ > 0) {                                 \
873 			start__ = mid__ + 1;                            \
874 		} else {                                                \
875 			result__ = (base) + mid__;                      \
876 			break;                                          \
877 		}                                                       \
878 	}                                                               \
879 	result__;                                                       \
880 })
881 
882 static enum forcewake_domains
883 find_fw_domain(struct intel_uncore *uncore, u32 offset)
884 {
885 	const struct intel_forcewake_range *entry;
886 
887 	entry = BSEARCH(offset,
888 			uncore->fw_domains_table,
889 			uncore->fw_domains_table_entries,
890 			fw_range_cmp);
891 
892 	if (!entry)
893 		return 0;
894 
895 	/*
896 	 * The list of FW domains depends on the SKU in gen11+ so we
897 	 * can't determine it statically. We use FORCEWAKE_ALL and
898 	 * translate it here to the list of available domains.
899 	 */
900 	if (entry->domains == FORCEWAKE_ALL)
901 		return uncore->fw_domains;
902 
903 	drm_WARN(&uncore->i915->drm, entry->domains & ~uncore->fw_domains,
904 		 "Uninitialized forcewake domain(s) 0x%x accessed at 0x%x\n",
905 		 entry->domains & ~uncore->fw_domains, offset);
906 
907 	return entry->domains;
908 }
909 
910 #define GEN_FW_RANGE(s, e, d) \
911 	{ .start = (s), .end = (e), .domains = (d) }
912 
913 #define HAS_FWTABLE(dev_priv) \
914 	(INTEL_GEN(dev_priv) >= 9 || \
915 	 IS_CHERRYVIEW(dev_priv) || \
916 	 IS_VALLEYVIEW(dev_priv))
917 
918 /* *Must* be sorted by offset ranges! See intel_fw_table_check(). */
919 static const struct intel_forcewake_range __vlv_fw_ranges[] = {
920 	GEN_FW_RANGE(0x2000, 0x3fff, FORCEWAKE_RENDER),
921 	GEN_FW_RANGE(0x5000, 0x7fff, FORCEWAKE_RENDER),
922 	GEN_FW_RANGE(0xb000, 0x11fff, FORCEWAKE_RENDER),
923 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
924 	GEN_FW_RANGE(0x22000, 0x23fff, FORCEWAKE_MEDIA),
925 	GEN_FW_RANGE(0x2e000, 0x2ffff, FORCEWAKE_RENDER),
926 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_MEDIA),
927 };
928 
929 #define __fwtable_reg_read_fw_domains(uncore, offset) \
930 ({ \
931 	enum forcewake_domains __fwd = 0; \
932 	if (NEEDS_FORCE_WAKE((offset))) \
933 		__fwd = find_fw_domain(uncore, offset); \
934 	__fwd; \
935 })
936 
937 #define __gen11_fwtable_reg_read_fw_domains(uncore, offset) \
938 	find_fw_domain(uncore, offset)
939 
940 #define __gen12_fwtable_reg_read_fw_domains(uncore, offset) \
941 	find_fw_domain(uncore, offset)
942 
943 /* *Must* be sorted by offset! See intel_shadow_table_check(). */
944 static const i915_reg_t gen8_shadowed_regs[] = {
945 	RING_TAIL(RENDER_RING_BASE),	/* 0x2000 (base) */
946 	GEN6_RPNSWREQ,			/* 0xA008 */
947 	GEN6_RC_VIDEO_FREQ,		/* 0xA00C */
948 	RING_TAIL(GEN6_BSD_RING_BASE),	/* 0x12000 (base) */
949 	RING_TAIL(VEBOX_RING_BASE),	/* 0x1a000 (base) */
950 	RING_TAIL(BLT_RING_BASE),	/* 0x22000 (base) */
951 	/* TODO: Other registers are not yet used */
952 };
953 
954 static const i915_reg_t gen11_shadowed_regs[] = {
955 	RING_TAIL(RENDER_RING_BASE),		/* 0x2000 (base) */
956 	GEN6_RPNSWREQ,				/* 0xA008 */
957 	GEN6_RC_VIDEO_FREQ,			/* 0xA00C */
958 	RING_TAIL(BLT_RING_BASE),		/* 0x22000 (base) */
959 	RING_TAIL(GEN11_BSD_RING_BASE),		/* 0x1C0000 (base) */
960 	RING_TAIL(GEN11_BSD2_RING_BASE),	/* 0x1C4000 (base) */
961 	RING_TAIL(GEN11_VEBOX_RING_BASE),	/* 0x1C8000 (base) */
962 	RING_TAIL(GEN11_BSD3_RING_BASE),	/* 0x1D0000 (base) */
963 	RING_TAIL(GEN11_BSD4_RING_BASE),	/* 0x1D4000 (base) */
964 	RING_TAIL(GEN11_VEBOX2_RING_BASE),	/* 0x1D8000 (base) */
965 	/* TODO: Other registers are not yet used */
966 };
967 
968 static const i915_reg_t gen12_shadowed_regs[] = {
969 	RING_TAIL(RENDER_RING_BASE),		/* 0x2000 (base) */
970 	GEN6_RPNSWREQ,				/* 0xA008 */
971 	GEN6_RC_VIDEO_FREQ,			/* 0xA00C */
972 	RING_TAIL(BLT_RING_BASE),		/* 0x22000 (base) */
973 	RING_TAIL(GEN11_BSD_RING_BASE),		/* 0x1C0000 (base) */
974 	RING_TAIL(GEN11_BSD2_RING_BASE),	/* 0x1C4000 (base) */
975 	RING_TAIL(GEN11_VEBOX_RING_BASE),	/* 0x1C8000 (base) */
976 	RING_TAIL(GEN11_BSD3_RING_BASE),	/* 0x1D0000 (base) */
977 	RING_TAIL(GEN11_BSD4_RING_BASE),	/* 0x1D4000 (base) */
978 	RING_TAIL(GEN11_VEBOX2_RING_BASE),	/* 0x1D8000 (base) */
979 	/* TODO: Other registers are not yet used */
980 };
981 
982 static int mmio_reg_cmp(u32 key, const i915_reg_t *reg)
983 {
984 	u32 offset = i915_mmio_reg_offset(*reg);
985 
986 	if (key < offset)
987 		return -1;
988 	else if (key > offset)
989 		return 1;
990 	else
991 		return 0;
992 }
993 
994 #define __is_genX_shadowed(x) \
995 static bool is_gen##x##_shadowed(u32 offset) \
996 { \
997 	const i915_reg_t *regs = gen##x##_shadowed_regs; \
998 	return BSEARCH(offset, regs, ARRAY_SIZE(gen##x##_shadowed_regs), \
999 		       mmio_reg_cmp); \
1000 }
1001 
1002 __is_genX_shadowed(8)
1003 __is_genX_shadowed(11)
1004 __is_genX_shadowed(12)
1005 
1006 static enum forcewake_domains
1007 gen6_reg_write_fw_domains(struct intel_uncore *uncore, i915_reg_t reg)
1008 {
1009 	return FORCEWAKE_RENDER;
1010 }
1011 
1012 #define __gen8_reg_write_fw_domains(uncore, offset) \
1013 ({ \
1014 	enum forcewake_domains __fwd; \
1015 	if (NEEDS_FORCE_WAKE(offset) && !is_gen8_shadowed(offset)) \
1016 		__fwd = FORCEWAKE_RENDER; \
1017 	else \
1018 		__fwd = 0; \
1019 	__fwd; \
1020 })
1021 
1022 /* *Must* be sorted by offset ranges! See intel_fw_table_check(). */
1023 static const struct intel_forcewake_range __chv_fw_ranges[] = {
1024 	GEN_FW_RANGE(0x2000, 0x3fff, FORCEWAKE_RENDER),
1025 	GEN_FW_RANGE(0x4000, 0x4fff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1026 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1027 	GEN_FW_RANGE(0x8000, 0x82ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1028 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1029 	GEN_FW_RANGE(0x8500, 0x85ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1030 	GEN_FW_RANGE(0x8800, 0x88ff, FORCEWAKE_MEDIA),
1031 	GEN_FW_RANGE(0x9000, 0xafff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1032 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1033 	GEN_FW_RANGE(0xd000, 0xd7ff, FORCEWAKE_MEDIA),
1034 	GEN_FW_RANGE(0xe000, 0xe7ff, FORCEWAKE_RENDER),
1035 	GEN_FW_RANGE(0xf000, 0xffff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1036 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
1037 	GEN_FW_RANGE(0x1a000, 0x1bfff, FORCEWAKE_MEDIA),
1038 	GEN_FW_RANGE(0x1e800, 0x1e9ff, FORCEWAKE_MEDIA),
1039 	GEN_FW_RANGE(0x30000, 0x37fff, FORCEWAKE_MEDIA),
1040 };
1041 
1042 #define __fwtable_reg_write_fw_domains(uncore, offset) \
1043 ({ \
1044 	enum forcewake_domains __fwd = 0; \
1045 	if (NEEDS_FORCE_WAKE((offset)) && !is_gen8_shadowed(offset)) \
1046 		__fwd = find_fw_domain(uncore, offset); \
1047 	__fwd; \
1048 })
1049 
1050 #define __gen11_fwtable_reg_write_fw_domains(uncore, offset) \
1051 ({ \
1052 	enum forcewake_domains __fwd = 0; \
1053 	const u32 __offset = (offset); \
1054 	if (!is_gen11_shadowed(__offset)) \
1055 		__fwd = find_fw_domain(uncore, __offset); \
1056 	__fwd; \
1057 })
1058 
1059 #define __gen12_fwtable_reg_write_fw_domains(uncore, offset) \
1060 ({ \
1061 	enum forcewake_domains __fwd = 0; \
1062 	const u32 __offset = (offset); \
1063 	if (!is_gen12_shadowed(__offset)) \
1064 		__fwd = find_fw_domain(uncore, __offset); \
1065 	__fwd; \
1066 })
1067 
1068 /* *Must* be sorted by offset ranges! See intel_fw_table_check(). */
1069 static const struct intel_forcewake_range __gen9_fw_ranges[] = {
1070 	GEN_FW_RANGE(0x0, 0xaff, FORCEWAKE_BLITTER),
1071 	GEN_FW_RANGE(0xb00, 0x1fff, 0), /* uncore range */
1072 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1073 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_BLITTER),
1074 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1075 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_BLITTER),
1076 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1077 	GEN_FW_RANGE(0x8000, 0x812f, FORCEWAKE_BLITTER),
1078 	GEN_FW_RANGE(0x8130, 0x813f, FORCEWAKE_MEDIA),
1079 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1080 	GEN_FW_RANGE(0x8160, 0x82ff, FORCEWAKE_BLITTER),
1081 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1082 	GEN_FW_RANGE(0x8500, 0x87ff, FORCEWAKE_BLITTER),
1083 	GEN_FW_RANGE(0x8800, 0x89ff, FORCEWAKE_MEDIA),
1084 	GEN_FW_RANGE(0x8a00, 0x8bff, FORCEWAKE_BLITTER),
1085 	GEN_FW_RANGE(0x8c00, 0x8cff, FORCEWAKE_RENDER),
1086 	GEN_FW_RANGE(0x8d00, 0x93ff, FORCEWAKE_BLITTER),
1087 	GEN_FW_RANGE(0x9400, 0x97ff, FORCEWAKE_RENDER | FORCEWAKE_MEDIA),
1088 	GEN_FW_RANGE(0x9800, 0xafff, FORCEWAKE_BLITTER),
1089 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1090 	GEN_FW_RANGE(0xb480, 0xcfff, FORCEWAKE_BLITTER),
1091 	GEN_FW_RANGE(0xd000, 0xd7ff, FORCEWAKE_MEDIA),
1092 	GEN_FW_RANGE(0xd800, 0xdfff, FORCEWAKE_BLITTER),
1093 	GEN_FW_RANGE(0xe000, 0xe8ff, FORCEWAKE_RENDER),
1094 	GEN_FW_RANGE(0xe900, 0x11fff, FORCEWAKE_BLITTER),
1095 	GEN_FW_RANGE(0x12000, 0x13fff, FORCEWAKE_MEDIA),
1096 	GEN_FW_RANGE(0x14000, 0x19fff, FORCEWAKE_BLITTER),
1097 	GEN_FW_RANGE(0x1a000, 0x1e9ff, FORCEWAKE_MEDIA),
1098 	GEN_FW_RANGE(0x1ea00, 0x243ff, FORCEWAKE_BLITTER),
1099 	GEN_FW_RANGE(0x24400, 0x247ff, FORCEWAKE_RENDER),
1100 	GEN_FW_RANGE(0x24800, 0x2ffff, FORCEWAKE_BLITTER),
1101 	GEN_FW_RANGE(0x30000, 0x3ffff, FORCEWAKE_MEDIA),
1102 };
1103 
1104 /* *Must* be sorted by offset ranges! See intel_fw_table_check(). */
1105 static const struct intel_forcewake_range __gen11_fw_ranges[] = {
1106 	GEN_FW_RANGE(0x0, 0xaff, FORCEWAKE_BLITTER),
1107 	GEN_FW_RANGE(0xb00, 0x1fff, 0), /* uncore range */
1108 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1109 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_BLITTER),
1110 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1111 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_BLITTER),
1112 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1113 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_BLITTER),
1114 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1115 	GEN_FW_RANGE(0x8160, 0x82ff, FORCEWAKE_BLITTER),
1116 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1117 	GEN_FW_RANGE(0x8500, 0x8bff, FORCEWAKE_BLITTER),
1118 	GEN_FW_RANGE(0x8c00, 0x8cff, FORCEWAKE_RENDER),
1119 	GEN_FW_RANGE(0x8d00, 0x93ff, FORCEWAKE_BLITTER),
1120 	GEN_FW_RANGE(0x9400, 0x97ff, FORCEWAKE_ALL),
1121 	GEN_FW_RANGE(0x9800, 0xafff, FORCEWAKE_BLITTER),
1122 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1123 	GEN_FW_RANGE(0xb480, 0xdeff, FORCEWAKE_BLITTER),
1124 	GEN_FW_RANGE(0xdf00, 0xe8ff, FORCEWAKE_RENDER),
1125 	GEN_FW_RANGE(0xe900, 0x16dff, FORCEWAKE_BLITTER),
1126 	GEN_FW_RANGE(0x16e00, 0x19fff, FORCEWAKE_RENDER),
1127 	GEN_FW_RANGE(0x1a000, 0x243ff, FORCEWAKE_BLITTER),
1128 	GEN_FW_RANGE(0x24400, 0x247ff, FORCEWAKE_RENDER),
1129 	GEN_FW_RANGE(0x24800, 0x3ffff, FORCEWAKE_BLITTER),
1130 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),
1131 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0),
1132 	GEN_FW_RANGE(0x1c4000, 0x1c7fff, FORCEWAKE_MEDIA_VDBOX1),
1133 	GEN_FW_RANGE(0x1c8000, 0x1cbfff, FORCEWAKE_MEDIA_VEBOX0),
1134 	GEN_FW_RANGE(0x1cc000, 0x1cffff, FORCEWAKE_BLITTER),
1135 	GEN_FW_RANGE(0x1d0000, 0x1d3fff, FORCEWAKE_MEDIA_VDBOX2),
1136 	GEN_FW_RANGE(0x1d4000, 0x1d7fff, FORCEWAKE_MEDIA_VDBOX3),
1137 	GEN_FW_RANGE(0x1d8000, 0x1dbfff, FORCEWAKE_MEDIA_VEBOX1)
1138 };
1139 
1140 /* *Must* be sorted by offset ranges! See intel_fw_table_check(). */
1141 static const struct intel_forcewake_range __gen12_fw_ranges[] = {
1142 	GEN_FW_RANGE(0x0, 0xaff, FORCEWAKE_BLITTER),
1143 	GEN_FW_RANGE(0xb00, 0x1fff, 0), /* uncore range */
1144 	GEN_FW_RANGE(0x2000, 0x26ff, FORCEWAKE_RENDER),
1145 	GEN_FW_RANGE(0x2700, 0x2fff, FORCEWAKE_BLITTER),
1146 	GEN_FW_RANGE(0x3000, 0x3fff, FORCEWAKE_RENDER),
1147 	GEN_FW_RANGE(0x4000, 0x51ff, FORCEWAKE_BLITTER),
1148 	GEN_FW_RANGE(0x5200, 0x7fff, FORCEWAKE_RENDER),
1149 	GEN_FW_RANGE(0x8000, 0x813f, FORCEWAKE_BLITTER),
1150 	GEN_FW_RANGE(0x8140, 0x815f, FORCEWAKE_RENDER),
1151 	GEN_FW_RANGE(0x8160, 0x82ff, FORCEWAKE_BLITTER),
1152 	GEN_FW_RANGE(0x8300, 0x84ff, FORCEWAKE_RENDER),
1153 	GEN_FW_RANGE(0x8500, 0x8bff, FORCEWAKE_BLITTER),
1154 	GEN_FW_RANGE(0x8c00, 0x8cff, FORCEWAKE_RENDER),
1155 	GEN_FW_RANGE(0x8d00, 0x93ff, FORCEWAKE_BLITTER),
1156 	GEN_FW_RANGE(0x9400, 0x97ff, FORCEWAKE_ALL),
1157 	GEN_FW_RANGE(0x9800, 0xafff, FORCEWAKE_BLITTER),
1158 	GEN_FW_RANGE(0xb000, 0xb47f, FORCEWAKE_RENDER),
1159 	GEN_FW_RANGE(0xb480, 0xdfff, FORCEWAKE_BLITTER),
1160 	GEN_FW_RANGE(0xe000, 0xe8ff, FORCEWAKE_RENDER),
1161 	GEN_FW_RANGE(0xe900, 0x147ff, FORCEWAKE_BLITTER),
1162 	GEN_FW_RANGE(0x14800, 0x148ff, FORCEWAKE_RENDER),
1163 	GEN_FW_RANGE(0x14900, 0x19fff, FORCEWAKE_BLITTER),
1164 	GEN_FW_RANGE(0x1a000, 0x1a7ff, FORCEWAKE_RENDER),
1165 	GEN_FW_RANGE(0x1a800, 0x1afff, FORCEWAKE_BLITTER),
1166 	GEN_FW_RANGE(0x1b000, 0x1bfff, FORCEWAKE_RENDER),
1167 	GEN_FW_RANGE(0x1c000, 0x243ff, FORCEWAKE_BLITTER),
1168 	GEN_FW_RANGE(0x24400, 0x247ff, FORCEWAKE_RENDER),
1169 	GEN_FW_RANGE(0x24800, 0x3ffff, FORCEWAKE_BLITTER),
1170 	GEN_FW_RANGE(0x40000, 0x1bffff, 0),
1171 	GEN_FW_RANGE(0x1c0000, 0x1c3fff, FORCEWAKE_MEDIA_VDBOX0),
1172 	GEN_FW_RANGE(0x1c4000, 0x1c7fff, FORCEWAKE_MEDIA_VDBOX1),
1173 	GEN_FW_RANGE(0x1c8000, 0x1cbfff, FORCEWAKE_MEDIA_VEBOX0),
1174 	GEN_FW_RANGE(0x1cc000, 0x1cffff, FORCEWAKE_BLITTER),
1175 	GEN_FW_RANGE(0x1d0000, 0x1d3fff, FORCEWAKE_MEDIA_VDBOX2),
1176 	GEN_FW_RANGE(0x1d4000, 0x1d7fff, FORCEWAKE_MEDIA_VDBOX3),
1177 	GEN_FW_RANGE(0x1d8000, 0x1dbfff, FORCEWAKE_MEDIA_VEBOX1)
1178 };
1179 
1180 static void
1181 ilk_dummy_write(struct intel_uncore *uncore)
1182 {
1183 	/* WaIssueDummyWriteToWakeupFromRC6:ilk Issue a dummy write to wake up
1184 	 * the chip from rc6 before touching it for real. MI_MODE is masked,
1185 	 * hence harmless to write 0 into. */
1186 	__raw_uncore_write32(uncore, MI_MODE, 0);
1187 }
1188 
1189 static void
1190 __unclaimed_reg_debug(struct intel_uncore *uncore,
1191 		      const i915_reg_t reg,
1192 		      const bool read,
1193 		      const bool before)
1194 {
1195 	if (drm_WARN(&uncore->i915->drm,
1196 		     check_for_unclaimed_mmio(uncore) && !before,
1197 		     "Unclaimed %s register 0x%x\n",
1198 		     read ? "read from" : "write to",
1199 		     i915_mmio_reg_offset(reg)))
1200 		/* Only report the first N failures */
1201 		i915_modparams.mmio_debug--;
1202 }
1203 
1204 static inline void
1205 unclaimed_reg_debug(struct intel_uncore *uncore,
1206 		    const i915_reg_t reg,
1207 		    const bool read,
1208 		    const bool before)
1209 {
1210 	if (likely(!i915_modparams.mmio_debug))
1211 		return;
1212 
1213 	/* interrupts are disabled and re-enabled around uncore->lock usage */
1214 	lockdep_assert_held(&uncore->lock);
1215 
1216 	if (before)
1217 		spin_lock(&uncore->debug->lock);
1218 
1219 	__unclaimed_reg_debug(uncore, reg, read, before);
1220 
1221 	if (!before)
1222 		spin_unlock(&uncore->debug->lock);
1223 }
1224 
1225 #define GEN2_READ_HEADER(x) \
1226 	u##x val = 0; \
1227 	assert_rpm_wakelock_held(uncore->rpm);
1228 
1229 #define GEN2_READ_FOOTER \
1230 	trace_i915_reg_rw(false, reg, val, sizeof(val), trace); \
1231 	return val
1232 
1233 #define __gen2_read(x) \
1234 static u##x \
1235 gen2_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1236 	GEN2_READ_HEADER(x); \
1237 	val = __raw_uncore_read##x(uncore, reg); \
1238 	GEN2_READ_FOOTER; \
1239 }
1240 
1241 #define __gen5_read(x) \
1242 static u##x \
1243 gen5_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1244 	GEN2_READ_HEADER(x); \
1245 	ilk_dummy_write(uncore); \
1246 	val = __raw_uncore_read##x(uncore, reg); \
1247 	GEN2_READ_FOOTER; \
1248 }
1249 
1250 __gen5_read(8)
1251 __gen5_read(16)
1252 __gen5_read(32)
1253 __gen5_read(64)
1254 __gen2_read(8)
1255 __gen2_read(16)
1256 __gen2_read(32)
1257 __gen2_read(64)
1258 
1259 #undef __gen5_read
1260 #undef __gen2_read
1261 
1262 #undef GEN2_READ_FOOTER
1263 #undef GEN2_READ_HEADER
1264 
1265 #define GEN6_READ_HEADER(x) \
1266 	u32 offset = i915_mmio_reg_offset(reg); \
1267 	unsigned long irqflags; \
1268 	u##x val = 0; \
1269 	assert_rpm_wakelock_held(uncore->rpm); \
1270 	spin_lock_irqsave(&uncore->lock, irqflags); \
1271 	unclaimed_reg_debug(uncore, reg, true, true)
1272 
1273 #define GEN6_READ_FOOTER \
1274 	unclaimed_reg_debug(uncore, reg, true, false); \
1275 	spin_unlock_irqrestore(&uncore->lock, irqflags); \
1276 	trace_i915_reg_rw(false, reg, val, sizeof(val), trace); \
1277 	return val
1278 
1279 static noinline void ___force_wake_auto(struct intel_uncore *uncore,
1280 					enum forcewake_domains fw_domains)
1281 {
1282 	struct intel_uncore_forcewake_domain *domain;
1283 	unsigned int tmp;
1284 
1285 	GEM_BUG_ON(fw_domains & ~uncore->fw_domains);
1286 
1287 	for_each_fw_domain_masked(domain, fw_domains, uncore, tmp)
1288 		fw_domain_arm_timer(domain);
1289 
1290 	uncore->funcs.force_wake_get(uncore, fw_domains);
1291 }
1292 
1293 static inline void __force_wake_auto(struct intel_uncore *uncore,
1294 				     enum forcewake_domains fw_domains)
1295 {
1296 	GEM_BUG_ON(!fw_domains);
1297 
1298 	/* Turn on all requested but inactive supported forcewake domains. */
1299 	fw_domains &= uncore->fw_domains;
1300 	fw_domains &= ~uncore->fw_domains_active;
1301 
1302 	if (fw_domains)
1303 		___force_wake_auto(uncore, fw_domains);
1304 }
1305 
1306 #define __gen_read(func, x) \
1307 static u##x \
1308 func##_read##x(struct intel_uncore *uncore, i915_reg_t reg, bool trace) { \
1309 	enum forcewake_domains fw_engine; \
1310 	GEN6_READ_HEADER(x); \
1311 	fw_engine = __##func##_reg_read_fw_domains(uncore, offset); \
1312 	if (fw_engine) \
1313 		__force_wake_auto(uncore, fw_engine); \
1314 	val = __raw_uncore_read##x(uncore, reg); \
1315 	GEN6_READ_FOOTER; \
1316 }
1317 
1318 #define __gen_reg_read_funcs(func) \
1319 static enum forcewake_domains \
1320 func##_reg_read_fw_domains(struct intel_uncore *uncore, i915_reg_t reg) { \
1321 	return __##func##_reg_read_fw_domains(uncore, i915_mmio_reg_offset(reg)); \
1322 } \
1323 \
1324 __gen_read(func, 8) \
1325 __gen_read(func, 16) \
1326 __gen_read(func, 32) \
1327 __gen_read(func, 64)
1328 
1329 __gen_reg_read_funcs(gen12_fwtable);
1330 __gen_reg_read_funcs(gen11_fwtable);
1331 __gen_reg_read_funcs(fwtable);
1332 __gen_reg_read_funcs(gen6);
1333 
1334 #undef __gen_reg_read_funcs
1335 #undef GEN6_READ_FOOTER
1336 #undef GEN6_READ_HEADER
1337 
1338 #define GEN2_WRITE_HEADER \
1339 	trace_i915_reg_rw(true, reg, val, sizeof(val), trace); \
1340 	assert_rpm_wakelock_held(uncore->rpm); \
1341 
1342 #define GEN2_WRITE_FOOTER
1343 
1344 #define __gen2_write(x) \
1345 static void \
1346 gen2_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
1347 	GEN2_WRITE_HEADER; \
1348 	__raw_uncore_write##x(uncore, reg, val); \
1349 	GEN2_WRITE_FOOTER; \
1350 }
1351 
1352 #define __gen5_write(x) \
1353 static void \
1354 gen5_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
1355 	GEN2_WRITE_HEADER; \
1356 	ilk_dummy_write(uncore); \
1357 	__raw_uncore_write##x(uncore, reg, val); \
1358 	GEN2_WRITE_FOOTER; \
1359 }
1360 
1361 __gen5_write(8)
1362 __gen5_write(16)
1363 __gen5_write(32)
1364 __gen2_write(8)
1365 __gen2_write(16)
1366 __gen2_write(32)
1367 
1368 #undef __gen5_write
1369 #undef __gen2_write
1370 
1371 #undef GEN2_WRITE_FOOTER
1372 #undef GEN2_WRITE_HEADER
1373 
1374 #define GEN6_WRITE_HEADER \
1375 	u32 offset = i915_mmio_reg_offset(reg); \
1376 	unsigned long irqflags; \
1377 	trace_i915_reg_rw(true, reg, val, sizeof(val), trace); \
1378 	assert_rpm_wakelock_held(uncore->rpm); \
1379 	spin_lock_irqsave(&uncore->lock, irqflags); \
1380 	unclaimed_reg_debug(uncore, reg, false, true)
1381 
1382 #define GEN6_WRITE_FOOTER \
1383 	unclaimed_reg_debug(uncore, reg, false, false); \
1384 	spin_unlock_irqrestore(&uncore->lock, irqflags)
1385 
1386 #define __gen6_write(x) \
1387 static void \
1388 gen6_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
1389 	GEN6_WRITE_HEADER; \
1390 	if (NEEDS_FORCE_WAKE(offset)) \
1391 		__gen6_gt_wait_for_fifo(uncore); \
1392 	__raw_uncore_write##x(uncore, reg, val); \
1393 	GEN6_WRITE_FOOTER; \
1394 }
1395 __gen6_write(8)
1396 __gen6_write(16)
1397 __gen6_write(32)
1398 
1399 #define __gen_write(func, x) \
1400 static void \
1401 func##_write##x(struct intel_uncore *uncore, i915_reg_t reg, u##x val, bool trace) { \
1402 	enum forcewake_domains fw_engine; \
1403 	GEN6_WRITE_HEADER; \
1404 	fw_engine = __##func##_reg_write_fw_domains(uncore, offset); \
1405 	if (fw_engine) \
1406 		__force_wake_auto(uncore, fw_engine); \
1407 	__raw_uncore_write##x(uncore, reg, val); \
1408 	GEN6_WRITE_FOOTER; \
1409 }
1410 
1411 #define __gen_reg_write_funcs(func) \
1412 static enum forcewake_domains \
1413 func##_reg_write_fw_domains(struct intel_uncore *uncore, i915_reg_t reg) { \
1414 	return __##func##_reg_write_fw_domains(uncore, i915_mmio_reg_offset(reg)); \
1415 } \
1416 \
1417 __gen_write(func, 8) \
1418 __gen_write(func, 16) \
1419 __gen_write(func, 32)
1420 
1421 __gen_reg_write_funcs(gen12_fwtable);
1422 __gen_reg_write_funcs(gen11_fwtable);
1423 __gen_reg_write_funcs(fwtable);
1424 __gen_reg_write_funcs(gen8);
1425 
1426 #undef __gen_reg_write_funcs
1427 #undef GEN6_WRITE_FOOTER
1428 #undef GEN6_WRITE_HEADER
1429 
1430 #define ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, x) \
1431 do { \
1432 	(uncore)->funcs.mmio_writeb = x##_write8; \
1433 	(uncore)->funcs.mmio_writew = x##_write16; \
1434 	(uncore)->funcs.mmio_writel = x##_write32; \
1435 } while (0)
1436 
1437 #define ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, x) \
1438 do { \
1439 	(uncore)->funcs.mmio_readb = x##_read8; \
1440 	(uncore)->funcs.mmio_readw = x##_read16; \
1441 	(uncore)->funcs.mmio_readl = x##_read32; \
1442 	(uncore)->funcs.mmio_readq = x##_read64; \
1443 } while (0)
1444 
1445 #define ASSIGN_WRITE_MMIO_VFUNCS(uncore, x) \
1446 do { \
1447 	ASSIGN_RAW_WRITE_MMIO_VFUNCS((uncore), x); \
1448 	(uncore)->funcs.write_fw_domains = x##_reg_write_fw_domains; \
1449 } while (0)
1450 
1451 #define ASSIGN_READ_MMIO_VFUNCS(uncore, x) \
1452 do { \
1453 	ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, x); \
1454 	(uncore)->funcs.read_fw_domains = x##_reg_read_fw_domains; \
1455 } while (0)
1456 
1457 static int __fw_domain_init(struct intel_uncore *uncore,
1458 			    enum forcewake_domain_id domain_id,
1459 			    i915_reg_t reg_set,
1460 			    i915_reg_t reg_ack)
1461 {
1462 	struct intel_uncore_forcewake_domain *d;
1463 
1464 	GEM_BUG_ON(domain_id >= FW_DOMAIN_ID_COUNT);
1465 	GEM_BUG_ON(uncore->fw_domain[domain_id]);
1466 
1467 	if (i915_inject_probe_failure(uncore->i915))
1468 		return -ENOMEM;
1469 
1470 	d = kzalloc(sizeof(*d), GFP_KERNEL);
1471 	if (!d)
1472 		return -ENOMEM;
1473 
1474 	drm_WARN_ON(&uncore->i915->drm, !i915_mmio_reg_valid(reg_set));
1475 	drm_WARN_ON(&uncore->i915->drm, !i915_mmio_reg_valid(reg_ack));
1476 
1477 	d->uncore = uncore;
1478 	d->wake_count = 0;
1479 	d->reg_set = uncore->regs + i915_mmio_reg_offset(reg_set);
1480 	d->reg_ack = uncore->regs + i915_mmio_reg_offset(reg_ack);
1481 
1482 	d->id = domain_id;
1483 
1484 	BUILD_BUG_ON(FORCEWAKE_RENDER != (1 << FW_DOMAIN_ID_RENDER));
1485 	BUILD_BUG_ON(FORCEWAKE_BLITTER != (1 << FW_DOMAIN_ID_BLITTER));
1486 	BUILD_BUG_ON(FORCEWAKE_MEDIA != (1 << FW_DOMAIN_ID_MEDIA));
1487 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX0 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX0));
1488 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX1 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX1));
1489 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX2 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX2));
1490 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VDBOX3 != (1 << FW_DOMAIN_ID_MEDIA_VDBOX3));
1491 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX0 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX0));
1492 	BUILD_BUG_ON(FORCEWAKE_MEDIA_VEBOX1 != (1 << FW_DOMAIN_ID_MEDIA_VEBOX1));
1493 
1494 	d->mask = BIT(domain_id);
1495 
1496 #ifdef __linux__
1497 	hrtimer_init(&d->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1498 	d->timer.function = intel_uncore_fw_release_timer;
1499 #else
1500 	timeout_set(&d->timer, intel_uncore_fw_release_timer, d);
1501 #endif
1502 
1503 	uncore->fw_domains |= BIT(domain_id);
1504 
1505 	fw_domain_reset(d);
1506 
1507 	uncore->fw_domain[domain_id] = d;
1508 
1509 	return 0;
1510 }
1511 
1512 static void fw_domain_fini(struct intel_uncore *uncore,
1513 			   enum forcewake_domain_id domain_id)
1514 {
1515 	struct intel_uncore_forcewake_domain *d;
1516 
1517 	GEM_BUG_ON(domain_id >= FW_DOMAIN_ID_COUNT);
1518 
1519 	d = fetch_and_zero(&uncore->fw_domain[domain_id]);
1520 	if (!d)
1521 		return;
1522 
1523 	uncore->fw_domains &= ~BIT(domain_id);
1524 	drm_WARN_ON(&uncore->i915->drm, d->wake_count);
1525 	drm_WARN_ON(&uncore->i915->drm, hrtimer_cancel(&d->timer));
1526 	kfree(d);
1527 }
1528 
1529 static void intel_uncore_fw_domains_fini(struct intel_uncore *uncore)
1530 {
1531 	struct intel_uncore_forcewake_domain *d;
1532 	int tmp;
1533 
1534 	for_each_fw_domain(d, uncore, tmp)
1535 		fw_domain_fini(uncore, d->id);
1536 }
1537 
1538 static int intel_uncore_fw_domains_init(struct intel_uncore *uncore)
1539 {
1540 	struct drm_i915_private *i915 = uncore->i915;
1541 	int ret = 0;
1542 
1543 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
1544 
1545 #define fw_domain_init(uncore__, id__, set__, ack__) \
1546 	(ret ?: (ret = __fw_domain_init((uncore__), (id__), (set__), (ack__))))
1547 
1548 	if (INTEL_GEN(i915) >= 11) {
1549 		int i;
1550 
1551 		uncore->funcs.force_wake_get = fw_domains_get_with_fallback;
1552 		uncore->funcs.force_wake_put = fw_domains_put;
1553 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1554 			       FORCEWAKE_RENDER_GEN9,
1555 			       FORCEWAKE_ACK_RENDER_GEN9);
1556 		fw_domain_init(uncore, FW_DOMAIN_ID_BLITTER,
1557 			       FORCEWAKE_BLITTER_GEN9,
1558 			       FORCEWAKE_ACK_BLITTER_GEN9);
1559 
1560 		for (i = 0; i < I915_MAX_VCS; i++) {
1561 			if (!HAS_ENGINE(i915, _VCS(i)))
1562 				continue;
1563 
1564 			fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA_VDBOX0 + i,
1565 				       FORCEWAKE_MEDIA_VDBOX_GEN11(i),
1566 				       FORCEWAKE_ACK_MEDIA_VDBOX_GEN11(i));
1567 		}
1568 		for (i = 0; i < I915_MAX_VECS; i++) {
1569 			if (!HAS_ENGINE(i915, _VECS(i)))
1570 				continue;
1571 
1572 			fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA_VEBOX0 + i,
1573 				       FORCEWAKE_MEDIA_VEBOX_GEN11(i),
1574 				       FORCEWAKE_ACK_MEDIA_VEBOX_GEN11(i));
1575 		}
1576 	} else if (IS_GEN_RANGE(i915, 9, 10)) {
1577 		uncore->funcs.force_wake_get = fw_domains_get_with_fallback;
1578 		uncore->funcs.force_wake_put = fw_domains_put;
1579 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1580 			       FORCEWAKE_RENDER_GEN9,
1581 			       FORCEWAKE_ACK_RENDER_GEN9);
1582 		fw_domain_init(uncore, FW_DOMAIN_ID_BLITTER,
1583 			       FORCEWAKE_BLITTER_GEN9,
1584 			       FORCEWAKE_ACK_BLITTER_GEN9);
1585 		fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA,
1586 			       FORCEWAKE_MEDIA_GEN9, FORCEWAKE_ACK_MEDIA_GEN9);
1587 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
1588 		uncore->funcs.force_wake_get = fw_domains_get;
1589 		uncore->funcs.force_wake_put = fw_domains_put;
1590 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1591 			       FORCEWAKE_VLV, FORCEWAKE_ACK_VLV);
1592 		fw_domain_init(uncore, FW_DOMAIN_ID_MEDIA,
1593 			       FORCEWAKE_MEDIA_VLV, FORCEWAKE_ACK_MEDIA_VLV);
1594 	} else if (IS_HASWELL(i915) || IS_BROADWELL(i915)) {
1595 		uncore->funcs.force_wake_get =
1596 			fw_domains_get_with_thread_status;
1597 		uncore->funcs.force_wake_put = fw_domains_put;
1598 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1599 			       FORCEWAKE_MT, FORCEWAKE_ACK_HSW);
1600 	} else if (IS_IVYBRIDGE(i915)) {
1601 		u32 ecobus;
1602 
1603 		/* IVB configs may use multi-threaded forcewake */
1604 
1605 		/* A small trick here - if the bios hasn't configured
1606 		 * MT forcewake, and if the device is in RC6, then
1607 		 * force_wake_mt_get will not wake the device and the
1608 		 * ECOBUS read will return zero. Which will be
1609 		 * (correctly) interpreted by the test below as MT
1610 		 * forcewake being disabled.
1611 		 */
1612 		uncore->funcs.force_wake_get =
1613 			fw_domains_get_with_thread_status;
1614 		uncore->funcs.force_wake_put = fw_domains_put;
1615 
1616 		/* We need to init first for ECOBUS access and then
1617 		 * determine later if we want to reinit, in case of MT access is
1618 		 * not working. In this stage we don't know which flavour this
1619 		 * ivb is, so it is better to reset also the gen6 fw registers
1620 		 * before the ecobus check.
1621 		 */
1622 
1623 		__raw_uncore_write32(uncore, FORCEWAKE, 0);
1624 		__raw_posting_read(uncore, ECOBUS);
1625 
1626 		ret = __fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1627 				       FORCEWAKE_MT, FORCEWAKE_MT_ACK);
1628 		if (ret)
1629 			goto out;
1630 
1631 		spin_lock_irq(&uncore->lock);
1632 		fw_domains_get_with_thread_status(uncore, FORCEWAKE_RENDER);
1633 		ecobus = __raw_uncore_read32(uncore, ECOBUS);
1634 		fw_domains_put(uncore, FORCEWAKE_RENDER);
1635 		spin_unlock_irq(&uncore->lock);
1636 
1637 		if (!(ecobus & FORCEWAKE_MT_ENABLE)) {
1638 			drm_info(&i915->drm, "No MT forcewake available on Ivybridge, this can result in issues\n");
1639 			drm_info(&i915->drm, "when using vblank-synced partial screen updates.\n");
1640 			fw_domain_fini(uncore, FW_DOMAIN_ID_RENDER);
1641 			fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1642 				       FORCEWAKE, FORCEWAKE_ACK);
1643 		}
1644 	} else if (IS_GEN(i915, 6)) {
1645 		uncore->funcs.force_wake_get =
1646 			fw_domains_get_with_thread_status;
1647 		uncore->funcs.force_wake_put = fw_domains_put;
1648 		fw_domain_init(uncore, FW_DOMAIN_ID_RENDER,
1649 			       FORCEWAKE, FORCEWAKE_ACK);
1650 	}
1651 
1652 #undef fw_domain_init
1653 
1654 	/* All future platforms are expected to require complex power gating */
1655 	drm_WARN_ON(&i915->drm, !ret && uncore->fw_domains == 0);
1656 
1657 out:
1658 	if (ret)
1659 		intel_uncore_fw_domains_fini(uncore);
1660 
1661 	return ret;
1662 }
1663 
1664 #define ASSIGN_FW_DOMAINS_TABLE(uncore, d) \
1665 { \
1666 	(uncore)->fw_domains_table = \
1667 			(struct intel_forcewake_range *)(d); \
1668 	(uncore)->fw_domains_table_entries = ARRAY_SIZE((d)); \
1669 }
1670 
1671 static int i915_pmic_bus_access_notifier(struct notifier_block *nb,
1672 					 unsigned long action, void *data)
1673 {
1674 	struct intel_uncore *uncore = container_of(nb,
1675 			struct intel_uncore, pmic_bus_access_nb);
1676 
1677 	switch (action) {
1678 	case MBI_PMIC_BUS_ACCESS_BEGIN:
1679 		/*
1680 		 * forcewake all now to make sure that we don't need to do a
1681 		 * forcewake later which on systems where this notifier gets
1682 		 * called requires the punit to access to the shared pmic i2c
1683 		 * bus, which will be busy after this notification, leading to:
1684 		 * "render: timed out waiting for forcewake ack request."
1685 		 * errors.
1686 		 *
1687 		 * The notifier is unregistered during intel_runtime_suspend(),
1688 		 * so it's ok to access the HW here without holding a RPM
1689 		 * wake reference -> disable wakeref asserts for the time of
1690 		 * the access.
1691 		 */
1692 		disable_rpm_wakeref_asserts(uncore->rpm);
1693 		intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1694 		enable_rpm_wakeref_asserts(uncore->rpm);
1695 		break;
1696 	case MBI_PMIC_BUS_ACCESS_END:
1697 		intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
1698 		break;
1699 	}
1700 
1701 	return NOTIFY_OK;
1702 }
1703 
1704 static int uncore_mmio_setup(struct intel_uncore *uncore)
1705 {
1706 	struct drm_i915_private *i915 = uncore->i915;
1707 	struct pci_dev *pdev = i915->drm.pdev;
1708 	int mmio_bar;
1709 	int mmio_size;
1710 
1711 	mmio_bar = IS_GEN(i915, 2) ? 1 : 0;
1712 	/*
1713 	 * Before gen4, the registers and the GTT are behind different BARs.
1714 	 * However, from gen4 onwards, the registers and the GTT are shared
1715 	 * in the same BAR, so we want to restrict this ioremap from
1716 	 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1717 	 * the register BAR remains the same size for all the earlier
1718 	 * generations up to Ironlake.
1719 	 */
1720 	if (INTEL_GEN(i915) < 5)
1721 		mmio_size = 512 * 1024;
1722 	else
1723 		mmio_size = 2 * 1024 * 1024;
1724 #ifdef __linux__
1725 	uncore->regs = pci_iomap(pdev, mmio_bar, mmio_size);
1726 	if (uncore->regs == NULL) {
1727 		drm_err(&i915->drm, "failed to map registers\n");
1728 		return -EIO;
1729 	}
1730 #endif
1731 
1732 	return 0;
1733 }
1734 
1735 static void uncore_mmio_cleanup(struct intel_uncore *uncore)
1736 {
1737 #ifdef __linux__
1738 	struct pci_dev *pdev = uncore->i915->drm.pdev;
1739 
1740 	pci_iounmap(pdev, uncore->regs);
1741 #endif
1742 }
1743 
1744 void intel_uncore_init_early(struct intel_uncore *uncore,
1745 			     struct drm_i915_private *i915)
1746 {
1747 	mtx_init(&uncore->lock, IPL_TTY);
1748 	uncore->i915 = i915;
1749 	uncore->rpm = &i915->runtime_pm;
1750 	uncore->debug = &i915->mmio_debug;
1751 }
1752 
1753 static void uncore_raw_init(struct intel_uncore *uncore)
1754 {
1755 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore));
1756 
1757 	if (IS_GEN(uncore->i915, 5)) {
1758 		ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, gen5);
1759 		ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, gen5);
1760 	} else {
1761 		ASSIGN_RAW_WRITE_MMIO_VFUNCS(uncore, gen2);
1762 		ASSIGN_RAW_READ_MMIO_VFUNCS(uncore, gen2);
1763 	}
1764 }
1765 
1766 static int uncore_forcewake_init(struct intel_uncore *uncore)
1767 {
1768 	struct drm_i915_private *i915 = uncore->i915;
1769 	int ret;
1770 
1771 	GEM_BUG_ON(!intel_uncore_has_forcewake(uncore));
1772 
1773 	ret = intel_uncore_fw_domains_init(uncore);
1774 	if (ret)
1775 		return ret;
1776 	forcewake_early_sanitize(uncore, 0);
1777 
1778 	if (IS_GEN_RANGE(i915, 6, 7)) {
1779 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen6);
1780 
1781 		if (IS_VALLEYVIEW(i915)) {
1782 			ASSIGN_FW_DOMAINS_TABLE(uncore, __vlv_fw_ranges);
1783 			ASSIGN_READ_MMIO_VFUNCS(uncore, fwtable);
1784 		} else {
1785 			ASSIGN_READ_MMIO_VFUNCS(uncore, gen6);
1786 		}
1787 	} else if (IS_GEN(i915, 8)) {
1788 		if (IS_CHERRYVIEW(i915)) {
1789 			ASSIGN_FW_DOMAINS_TABLE(uncore, __chv_fw_ranges);
1790 			ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
1791 			ASSIGN_READ_MMIO_VFUNCS(uncore, fwtable);
1792 		} else {
1793 			ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen8);
1794 			ASSIGN_READ_MMIO_VFUNCS(uncore, gen6);
1795 		}
1796 	} else if (IS_GEN_RANGE(i915, 9, 10)) {
1797 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen9_fw_ranges);
1798 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, fwtable);
1799 		ASSIGN_READ_MMIO_VFUNCS(uncore, fwtable);
1800 	} else if (IS_GEN(i915, 11)) {
1801 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen11_fw_ranges);
1802 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen11_fwtable);
1803 		ASSIGN_READ_MMIO_VFUNCS(uncore, gen11_fwtable);
1804 	} else {
1805 		ASSIGN_FW_DOMAINS_TABLE(uncore, __gen12_fw_ranges);
1806 		ASSIGN_WRITE_MMIO_VFUNCS(uncore, gen12_fwtable);
1807 		ASSIGN_READ_MMIO_VFUNCS(uncore, gen12_fwtable);
1808 	}
1809 
1810 	uncore->pmic_bus_access_nb.notifier_call = i915_pmic_bus_access_notifier;
1811 	iosf_mbi_register_pmic_bus_access_notifier(&uncore->pmic_bus_access_nb);
1812 
1813 	return 0;
1814 }
1815 
1816 int intel_uncore_init_mmio(struct intel_uncore *uncore)
1817 {
1818 	struct drm_i915_private *i915 = uncore->i915;
1819 	int ret;
1820 
1821 	ret = uncore_mmio_setup(uncore);
1822 	if (ret)
1823 		return ret;
1824 
1825 	if (INTEL_GEN(i915) > 5 && !intel_vgpu_active(i915))
1826 		uncore->flags |= UNCORE_HAS_FORCEWAKE;
1827 
1828 	if (!intel_uncore_has_forcewake(uncore)) {
1829 		uncore_raw_init(uncore);
1830 	} else {
1831 		ret = uncore_forcewake_init(uncore);
1832 		if (ret)
1833 			goto out_mmio_cleanup;
1834 	}
1835 
1836 	/* make sure fw funcs are set if and only if we have fw*/
1837 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.force_wake_get);
1838 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.force_wake_put);
1839 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.read_fw_domains);
1840 	GEM_BUG_ON(intel_uncore_has_forcewake(uncore) != !!uncore->funcs.write_fw_domains);
1841 
1842 	if (HAS_FPGA_DBG_UNCLAIMED(i915))
1843 		uncore->flags |= UNCORE_HAS_FPGA_DBG_UNCLAIMED;
1844 
1845 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1846 		uncore->flags |= UNCORE_HAS_DBG_UNCLAIMED;
1847 
1848 	if (IS_GEN_RANGE(i915, 6, 7))
1849 		uncore->flags |= UNCORE_HAS_FIFO;
1850 
1851 	/* clear out unclaimed reg detection bit */
1852 	if (intel_uncore_unclaimed_mmio(uncore))
1853 		drm_dbg(&i915->drm, "unclaimed mmio detected on uncore init, clearing\n");
1854 
1855 	return 0;
1856 
1857 out_mmio_cleanup:
1858 	uncore_mmio_cleanup(uncore);
1859 
1860 	return ret;
1861 }
1862 
1863 /*
1864  * We might have detected that some engines are fused off after we initialized
1865  * the forcewake domains. Prune them, to make sure they only reference existing
1866  * engines.
1867  */
1868 void intel_uncore_prune_mmio_domains(struct intel_uncore *uncore)
1869 {
1870 	struct drm_i915_private *i915 = uncore->i915;
1871 	enum forcewake_domains fw_domains = uncore->fw_domains;
1872 	enum forcewake_domain_id domain_id;
1873 	int i;
1874 
1875 	if (!intel_uncore_has_forcewake(uncore) || INTEL_GEN(i915) < 11)
1876 		return;
1877 
1878 	for (i = 0; i < I915_MAX_VCS; i++) {
1879 		domain_id = FW_DOMAIN_ID_MEDIA_VDBOX0 + i;
1880 
1881 		if (HAS_ENGINE(i915, _VCS(i)))
1882 			continue;
1883 
1884 		if (fw_domains & BIT(domain_id))
1885 			fw_domain_fini(uncore, domain_id);
1886 	}
1887 
1888 	for (i = 0; i < I915_MAX_VECS; i++) {
1889 		domain_id = FW_DOMAIN_ID_MEDIA_VEBOX0 + i;
1890 
1891 		if (HAS_ENGINE(i915, _VECS(i)))
1892 			continue;
1893 
1894 		if (fw_domains & BIT(domain_id))
1895 			fw_domain_fini(uncore, domain_id);
1896 	}
1897 }
1898 
1899 void intel_uncore_fini_mmio(struct intel_uncore *uncore)
1900 {
1901 	if (intel_uncore_has_forcewake(uncore)) {
1902 		iosf_mbi_punit_acquire();
1903 		iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
1904 			&uncore->pmic_bus_access_nb);
1905 		intel_uncore_forcewake_reset(uncore);
1906 		intel_uncore_fw_domains_fini(uncore);
1907 		iosf_mbi_punit_release();
1908 	}
1909 
1910 	uncore_mmio_cleanup(uncore);
1911 }
1912 
1913 static const struct reg_whitelist {
1914 	i915_reg_t offset_ldw;
1915 	i915_reg_t offset_udw;
1916 	u16 gen_mask;
1917 	u8 size;
1918 } reg_read_whitelist[] = { {
1919 	.offset_ldw = RING_TIMESTAMP(RENDER_RING_BASE),
1920 	.offset_udw = RING_TIMESTAMP_UDW(RENDER_RING_BASE),
1921 	.gen_mask = INTEL_GEN_MASK(4, 12),
1922 	.size = 8
1923 } };
1924 
1925 int i915_reg_read_ioctl(struct drm_device *dev,
1926 			void *data, struct drm_file *file)
1927 {
1928 	struct drm_i915_private *i915 = to_i915(dev);
1929 	struct intel_uncore *uncore = &i915->uncore;
1930 	struct drm_i915_reg_read *reg = data;
1931 	struct reg_whitelist const *entry;
1932 	intel_wakeref_t wakeref;
1933 	unsigned int flags;
1934 	int remain;
1935 	int ret = 0;
1936 
1937 	entry = reg_read_whitelist;
1938 	remain = ARRAY_SIZE(reg_read_whitelist);
1939 	while (remain) {
1940 		u32 entry_offset = i915_mmio_reg_offset(entry->offset_ldw);
1941 
1942 		GEM_BUG_ON(!is_power_of_2(entry->size));
1943 		GEM_BUG_ON(entry->size > 8);
1944 		GEM_BUG_ON(entry_offset & (entry->size - 1));
1945 
1946 		if (INTEL_INFO(i915)->gen_mask & entry->gen_mask &&
1947 		    entry_offset == (reg->offset & -entry->size))
1948 			break;
1949 		entry++;
1950 		remain--;
1951 	}
1952 
1953 	if (!remain)
1954 		return -EINVAL;
1955 
1956 	flags = reg->offset & (entry->size - 1);
1957 
1958 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
1959 		if (entry->size == 8 && flags == I915_REG_READ_8B_WA)
1960 			reg->val = intel_uncore_read64_2x32(uncore,
1961 							    entry->offset_ldw,
1962 							    entry->offset_udw);
1963 		else if (entry->size == 8 && flags == 0)
1964 			reg->val = intel_uncore_read64(uncore,
1965 						       entry->offset_ldw);
1966 		else if (entry->size == 4 && flags == 0)
1967 			reg->val = intel_uncore_read(uncore, entry->offset_ldw);
1968 		else if (entry->size == 2 && flags == 0)
1969 			reg->val = intel_uncore_read16(uncore,
1970 						       entry->offset_ldw);
1971 		else if (entry->size == 1 && flags == 0)
1972 			reg->val = intel_uncore_read8(uncore,
1973 						      entry->offset_ldw);
1974 		else
1975 			ret = -EINVAL;
1976 	}
1977 
1978 	return ret;
1979 }
1980 
1981 /**
1982  * __intel_wait_for_register_fw - wait until register matches expected state
1983  * @uncore: the struct intel_uncore
1984  * @reg: the register to read
1985  * @mask: mask to apply to register value
1986  * @value: expected value
1987  * @fast_timeout_us: fast timeout in microsecond for atomic/tight wait
1988  * @slow_timeout_ms: slow timeout in millisecond
1989  * @out_value: optional placeholder to hold registry value
1990  *
1991  * This routine waits until the target register @reg contains the expected
1992  * @value after applying the @mask, i.e. it waits until ::
1993  *
1994  *     (I915_READ_FW(reg) & mask) == value
1995  *
1996  * Otherwise, the wait will timeout after @slow_timeout_ms milliseconds.
1997  * For atomic context @slow_timeout_ms must be zero and @fast_timeout_us
1998  * must be not larger than 20,0000 microseconds.
1999  *
2000  * Note that this routine assumes the caller holds forcewake asserted, it is
2001  * not suitable for very long waits. See intel_wait_for_register() if you
2002  * wish to wait without holding forcewake for the duration (i.e. you expect
2003  * the wait to be slow).
2004  *
2005  * Return: 0 if the register matches the desired condition, or -ETIMEDOUT.
2006  */
2007 int __intel_wait_for_register_fw(struct intel_uncore *uncore,
2008 				 i915_reg_t reg,
2009 				 u32 mask,
2010 				 u32 value,
2011 				 unsigned int fast_timeout_us,
2012 				 unsigned int slow_timeout_ms,
2013 				 u32 *out_value)
2014 {
2015 	u32 uninitialized_var(reg_value);
2016 #define done (((reg_value = intel_uncore_read_fw(uncore, reg)) & mask) == value)
2017 	int ret;
2018 
2019 	/* Catch any overuse of this function */
2020 	might_sleep_if(slow_timeout_ms);
2021 	GEM_BUG_ON(fast_timeout_us > 20000);
2022 
2023 	ret = -ETIMEDOUT;
2024 	if (fast_timeout_us && fast_timeout_us <= 20000)
2025 		ret = _wait_for_atomic(done, fast_timeout_us, 0);
2026 	if (ret && slow_timeout_ms)
2027 		ret = wait_for(done, slow_timeout_ms);
2028 
2029 	if (out_value)
2030 		*out_value = reg_value;
2031 
2032 	return ret;
2033 #undef done
2034 }
2035 
2036 /**
2037  * __intel_wait_for_register - wait until register matches expected state
2038  * @uncore: the struct intel_uncore
2039  * @reg: the register to read
2040  * @mask: mask to apply to register value
2041  * @value: expected value
2042  * @fast_timeout_us: fast timeout in microsecond for atomic/tight wait
2043  * @slow_timeout_ms: slow timeout in millisecond
2044  * @out_value: optional placeholder to hold registry value
2045  *
2046  * This routine waits until the target register @reg contains the expected
2047  * @value after applying the @mask, i.e. it waits until ::
2048  *
2049  *     (I915_READ(reg) & mask) == value
2050  *
2051  * Otherwise, the wait will timeout after @timeout_ms milliseconds.
2052  *
2053  * Return: 0 if the register matches the desired condition, or -ETIMEDOUT.
2054  */
2055 int __intel_wait_for_register(struct intel_uncore *uncore,
2056 			      i915_reg_t reg,
2057 			      u32 mask,
2058 			      u32 value,
2059 			      unsigned int fast_timeout_us,
2060 			      unsigned int slow_timeout_ms,
2061 			      u32 *out_value)
2062 {
2063 	unsigned fw =
2064 		intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
2065 	u32 reg_value;
2066 	int ret;
2067 
2068 	might_sleep_if(slow_timeout_ms);
2069 
2070 	spin_lock_irq(&uncore->lock);
2071 	intel_uncore_forcewake_get__locked(uncore, fw);
2072 
2073 	ret = __intel_wait_for_register_fw(uncore,
2074 					   reg, mask, value,
2075 					   fast_timeout_us, 0, &reg_value);
2076 
2077 	intel_uncore_forcewake_put__locked(uncore, fw);
2078 	spin_unlock_irq(&uncore->lock);
2079 
2080 	if (ret && slow_timeout_ms)
2081 		ret = __wait_for(reg_value = intel_uncore_read_notrace(uncore,
2082 								       reg),
2083 				 (reg_value & mask) == value,
2084 				 slow_timeout_ms * 1000, 10, 1000);
2085 
2086 	/* just trace the final value */
2087 	trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
2088 
2089 	if (out_value)
2090 		*out_value = reg_value;
2091 
2092 	return ret;
2093 }
2094 
2095 bool intel_uncore_unclaimed_mmio(struct intel_uncore *uncore)
2096 {
2097 	bool ret;
2098 
2099 	spin_lock_irq(&uncore->debug->lock);
2100 	ret = check_for_unclaimed_mmio(uncore);
2101 	spin_unlock_irq(&uncore->debug->lock);
2102 
2103 	return ret;
2104 }
2105 
2106 bool
2107 intel_uncore_arm_unclaimed_mmio_detection(struct intel_uncore *uncore)
2108 {
2109 	bool ret = false;
2110 
2111 	spin_lock_irq(&uncore->debug->lock);
2112 
2113 	if (unlikely(uncore->debug->unclaimed_mmio_check <= 0))
2114 		goto out;
2115 
2116 	if (unlikely(check_for_unclaimed_mmio(uncore))) {
2117 		if (!i915_modparams.mmio_debug) {
2118 			drm_dbg(&uncore->i915->drm,
2119 				"Unclaimed register detected, "
2120 				"enabling oneshot unclaimed register reporting. "
2121 				"Please use i915.mmio_debug=N for more information.\n");
2122 			i915_modparams.mmio_debug++;
2123 		}
2124 		uncore->debug->unclaimed_mmio_check--;
2125 		ret = true;
2126 	}
2127 
2128 out:
2129 	spin_unlock_irq(&uncore->debug->lock);
2130 
2131 	return ret;
2132 }
2133 
2134 /**
2135  * intel_uncore_forcewake_for_reg - which forcewake domains are needed to access
2136  * 				    a register
2137  * @uncore: pointer to struct intel_uncore
2138  * @reg: register in question
2139  * @op: operation bitmask of FW_REG_READ and/or FW_REG_WRITE
2140  *
2141  * Returns a set of forcewake domains required to be taken with for example
2142  * intel_uncore_forcewake_get for the specified register to be accessible in the
2143  * specified mode (read, write or read/write) with raw mmio accessors.
2144  *
2145  * NOTE: On Gen6 and Gen7 write forcewake domain (FORCEWAKE_RENDER) requires the
2146  * callers to do FIFO management on their own or risk losing writes.
2147  */
2148 enum forcewake_domains
2149 intel_uncore_forcewake_for_reg(struct intel_uncore *uncore,
2150 			       i915_reg_t reg, unsigned int op)
2151 {
2152 	enum forcewake_domains fw_domains = 0;
2153 
2154 	drm_WARN_ON(&uncore->i915->drm, !op);
2155 
2156 	if (!intel_uncore_has_forcewake(uncore))
2157 		return 0;
2158 
2159 	if (op & FW_REG_READ)
2160 		fw_domains = uncore->funcs.read_fw_domains(uncore, reg);
2161 
2162 	if (op & FW_REG_WRITE)
2163 		fw_domains |= uncore->funcs.write_fw_domains(uncore, reg);
2164 
2165 	drm_WARN_ON(&uncore->i915->drm, fw_domains & ~uncore->fw_domains);
2166 
2167 	return fw_domains;
2168 }
2169 
2170 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2171 #include "selftests/mock_uncore.c"
2172 #include "selftests/intel_uncore.c"
2173 #endif
2174