xref: /linux/drivers/gpu/drm/i915/gt/intel_rps.c (revision 3027ce13e04eee76539ca65c2cb1028a01c8c508)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include <linux/string_helpers.h>
7 
8 #include <drm/i915_drm.h>
9 
10 #include "display/intel_display.h"
11 #include "display/intel_display_irq.h"
12 #include "i915_drv.h"
13 #include "i915_irq.h"
14 #include "i915_reg.h"
15 #include "intel_breadcrumbs.h"
16 #include "intel_gt.h"
17 #include "intel_gt_clock_utils.h"
18 #include "intel_gt_irq.h"
19 #include "intel_gt_pm.h"
20 #include "intel_gt_pm_irq.h"
21 #include "intel_gt_print.h"
22 #include "intel_gt_regs.h"
23 #include "intel_mchbar_regs.h"
24 #include "intel_pcode.h"
25 #include "intel_rps.h"
26 #include "vlv_sideband.h"
27 #include "../../../platform/x86/intel_ips.h"
28 
29 #define BUSY_MAX_EI	20u /* ms */
30 
31 /*
32  * Lock protecting IPS related data structures
33  */
34 static DEFINE_SPINLOCK(mchdev_lock);
35 
36 static struct intel_gt *rps_to_gt(struct intel_rps *rps)
37 {
38 	return container_of(rps, struct intel_gt, rps);
39 }
40 
41 static struct drm_i915_private *rps_to_i915(struct intel_rps *rps)
42 {
43 	return rps_to_gt(rps)->i915;
44 }
45 
46 static struct intel_uncore *rps_to_uncore(struct intel_rps *rps)
47 {
48 	return rps_to_gt(rps)->uncore;
49 }
50 
51 static struct intel_guc_slpc *rps_to_slpc(struct intel_rps *rps)
52 {
53 	struct intel_gt *gt = rps_to_gt(rps);
54 
55 	return &gt->uc.guc.slpc;
56 }
57 
58 static bool rps_uses_slpc(struct intel_rps *rps)
59 {
60 	struct intel_gt *gt = rps_to_gt(rps);
61 
62 	return intel_uc_uses_guc_slpc(&gt->uc);
63 }
64 
65 static u32 rps_pm_sanitize_mask(struct intel_rps *rps, u32 mask)
66 {
67 	return mask & ~rps->pm_intrmsk_mbz;
68 }
69 
70 static void set(struct intel_uncore *uncore, i915_reg_t reg, u32 val)
71 {
72 	intel_uncore_write_fw(uncore, reg, val);
73 }
74 
75 static void rps_timer(struct timer_list *t)
76 {
77 	struct intel_rps *rps = from_timer(rps, t, timer);
78 	struct intel_gt *gt = rps_to_gt(rps);
79 	struct intel_engine_cs *engine;
80 	ktime_t dt, last, timestamp;
81 	enum intel_engine_id id;
82 	s64 max_busy[3] = {};
83 
84 	timestamp = 0;
85 	for_each_engine(engine, gt, id) {
86 		s64 busy;
87 		int i;
88 
89 		dt = intel_engine_get_busy_time(engine, &timestamp);
90 		last = engine->stats.rps;
91 		engine->stats.rps = dt;
92 
93 		busy = ktime_to_ns(ktime_sub(dt, last));
94 		for (i = 0; i < ARRAY_SIZE(max_busy); i++) {
95 			if (busy > max_busy[i])
96 				swap(busy, max_busy[i]);
97 		}
98 	}
99 	last = rps->pm_timestamp;
100 	rps->pm_timestamp = timestamp;
101 
102 	if (intel_rps_is_active(rps)) {
103 		s64 busy;
104 		int i;
105 
106 		dt = ktime_sub(timestamp, last);
107 
108 		/*
109 		 * Our goal is to evaluate each engine independently, so we run
110 		 * at the lowest clocks required to sustain the heaviest
111 		 * workload. However, a task may be split into sequential
112 		 * dependent operations across a set of engines, such that
113 		 * the independent contributions do not account for high load,
114 		 * but overall the task is GPU bound. For example, consider
115 		 * video decode on vcs followed by colour post-processing
116 		 * on vecs, followed by general post-processing on rcs.
117 		 * Since multi-engines being active does imply a single
118 		 * continuous workload across all engines, we hedge our
119 		 * bets by only contributing a factor of the distributed
120 		 * load into our busyness calculation.
121 		 */
122 		busy = max_busy[0];
123 		for (i = 1; i < ARRAY_SIZE(max_busy); i++) {
124 			if (!max_busy[i])
125 				break;
126 
127 			busy += div_u64(max_busy[i], 1 << i);
128 		}
129 		GT_TRACE(gt,
130 			 "busy:%lld [%d%%], max:[%lld, %lld, %lld], interval:%d\n",
131 			 busy, (int)div64_u64(100 * busy, dt),
132 			 max_busy[0], max_busy[1], max_busy[2],
133 			 rps->pm_interval);
134 
135 		if (100 * busy > rps->power.up_threshold * dt &&
136 		    rps->cur_freq < rps->max_freq_softlimit) {
137 			rps->pm_iir |= GEN6_PM_RP_UP_THRESHOLD;
138 			rps->pm_interval = 1;
139 			queue_work(gt->i915->unordered_wq, &rps->work);
140 		} else if (100 * busy < rps->power.down_threshold * dt &&
141 			   rps->cur_freq > rps->min_freq_softlimit) {
142 			rps->pm_iir |= GEN6_PM_RP_DOWN_THRESHOLD;
143 			rps->pm_interval = 1;
144 			queue_work(gt->i915->unordered_wq, &rps->work);
145 		} else {
146 			rps->last_adj = 0;
147 		}
148 
149 		mod_timer(&rps->timer,
150 			  jiffies + msecs_to_jiffies(rps->pm_interval));
151 		rps->pm_interval = min(rps->pm_interval * 2, BUSY_MAX_EI);
152 	}
153 }
154 
155 static void rps_start_timer(struct intel_rps *rps)
156 {
157 	rps->pm_timestamp = ktime_sub(ktime_get(), rps->pm_timestamp);
158 	rps->pm_interval = 1;
159 	mod_timer(&rps->timer, jiffies + 1);
160 }
161 
162 static void rps_stop_timer(struct intel_rps *rps)
163 {
164 	del_timer_sync(&rps->timer);
165 	rps->pm_timestamp = ktime_sub(ktime_get(), rps->pm_timestamp);
166 	cancel_work_sync(&rps->work);
167 }
168 
169 static u32 rps_pm_mask(struct intel_rps *rps, u8 val)
170 {
171 	u32 mask = 0;
172 
173 	/* We use UP_EI_EXPIRED interrupts for both up/down in manual mode */
174 	if (val > rps->min_freq_softlimit)
175 		mask |= (GEN6_PM_RP_UP_EI_EXPIRED |
176 			 GEN6_PM_RP_DOWN_THRESHOLD |
177 			 GEN6_PM_RP_DOWN_TIMEOUT);
178 
179 	if (val < rps->max_freq_softlimit)
180 		mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
181 
182 	mask &= rps->pm_events;
183 
184 	return rps_pm_sanitize_mask(rps, ~mask);
185 }
186 
187 static void rps_reset_ei(struct intel_rps *rps)
188 {
189 	memset(&rps->ei, 0, sizeof(rps->ei));
190 }
191 
192 static void rps_enable_interrupts(struct intel_rps *rps)
193 {
194 	struct intel_gt *gt = rps_to_gt(rps);
195 
196 	GEM_BUG_ON(rps_uses_slpc(rps));
197 
198 	GT_TRACE(gt, "interrupts:on rps->pm_events: %x, rps_pm_mask:%x\n",
199 		 rps->pm_events, rps_pm_mask(rps, rps->last_freq));
200 
201 	rps_reset_ei(rps);
202 
203 	spin_lock_irq(gt->irq_lock);
204 	gen6_gt_pm_enable_irq(gt, rps->pm_events);
205 	spin_unlock_irq(gt->irq_lock);
206 
207 	intel_uncore_write(gt->uncore,
208 			   GEN6_PMINTRMSK, rps_pm_mask(rps, rps->last_freq));
209 }
210 
211 static void gen6_rps_reset_interrupts(struct intel_rps *rps)
212 {
213 	gen6_gt_pm_reset_iir(rps_to_gt(rps), GEN6_PM_RPS_EVENTS);
214 }
215 
216 static void gen11_rps_reset_interrupts(struct intel_rps *rps)
217 {
218 	while (gen11_gt_reset_one_iir(rps_to_gt(rps), 0, GEN11_GTPM))
219 		;
220 }
221 
222 static void rps_reset_interrupts(struct intel_rps *rps)
223 {
224 	struct intel_gt *gt = rps_to_gt(rps);
225 
226 	spin_lock_irq(gt->irq_lock);
227 	if (GRAPHICS_VER(gt->i915) >= 11)
228 		gen11_rps_reset_interrupts(rps);
229 	else
230 		gen6_rps_reset_interrupts(rps);
231 
232 	rps->pm_iir = 0;
233 	spin_unlock_irq(gt->irq_lock);
234 }
235 
236 static void rps_disable_interrupts(struct intel_rps *rps)
237 {
238 	struct intel_gt *gt = rps_to_gt(rps);
239 
240 	intel_uncore_write(gt->uncore,
241 			   GEN6_PMINTRMSK, rps_pm_sanitize_mask(rps, ~0u));
242 
243 	spin_lock_irq(gt->irq_lock);
244 	gen6_gt_pm_disable_irq(gt, GEN6_PM_RPS_EVENTS);
245 	spin_unlock_irq(gt->irq_lock);
246 
247 	intel_synchronize_irq(gt->i915);
248 
249 	/*
250 	 * Now that we will not be generating any more work, flush any
251 	 * outstanding tasks. As we are called on the RPS idle path,
252 	 * we will reset the GPU to minimum frequencies, so the current
253 	 * state of the worker can be discarded.
254 	 */
255 	cancel_work_sync(&rps->work);
256 
257 	rps_reset_interrupts(rps);
258 	GT_TRACE(gt, "interrupts:off\n");
259 }
260 
261 static const struct cparams {
262 	u16 i;
263 	u16 t;
264 	u16 m;
265 	u16 c;
266 } cparams[] = {
267 	{ 1, 1333, 301, 28664 },
268 	{ 1, 1066, 294, 24460 },
269 	{ 1, 800, 294, 25192 },
270 	{ 0, 1333, 276, 27605 },
271 	{ 0, 1066, 276, 27605 },
272 	{ 0, 800, 231, 23784 },
273 };
274 
275 static void gen5_rps_init(struct intel_rps *rps)
276 {
277 	struct drm_i915_private *i915 = rps_to_i915(rps);
278 	struct intel_uncore *uncore = rps_to_uncore(rps);
279 	u8 fmax, fmin, fstart;
280 	u32 rgvmodectl;
281 	int c_m, i;
282 
283 	if (i915->fsb_freq <= 3200)
284 		c_m = 0;
285 	else if (i915->fsb_freq <= 4800)
286 		c_m = 1;
287 	else
288 		c_m = 2;
289 
290 	for (i = 0; i < ARRAY_SIZE(cparams); i++) {
291 		if (cparams[i].i == c_m && cparams[i].t == i915->mem_freq) {
292 			rps->ips.m = cparams[i].m;
293 			rps->ips.c = cparams[i].c;
294 			break;
295 		}
296 	}
297 
298 	rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
299 
300 	/* Set up min, max, and cur for interrupt handling */
301 	fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
302 	fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
303 	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
304 		MEMMODE_FSTART_SHIFT;
305 	drm_dbg(&i915->drm, "fmax: %d, fmin: %d, fstart: %d\n",
306 		fmax, fmin, fstart);
307 
308 	rps->min_freq = fmax;
309 	rps->efficient_freq = fstart;
310 	rps->max_freq = fmin;
311 }
312 
313 static unsigned long
314 __ips_chipset_val(struct intel_ips *ips)
315 {
316 	struct intel_uncore *uncore =
317 		rps_to_uncore(container_of(ips, struct intel_rps, ips));
318 	unsigned long now = jiffies_to_msecs(jiffies), dt;
319 	unsigned long result;
320 	u64 total, delta;
321 
322 	lockdep_assert_held(&mchdev_lock);
323 
324 	/*
325 	 * Prevent division-by-zero if we are asking too fast.
326 	 * Also, we don't get interesting results if we are polling
327 	 * faster than once in 10ms, so just return the saved value
328 	 * in such cases.
329 	 */
330 	dt = now - ips->last_time1;
331 	if (dt <= 10)
332 		return ips->chipset_power;
333 
334 	/* FIXME: handle per-counter overflow */
335 	total = intel_uncore_read(uncore, DMIEC);
336 	total += intel_uncore_read(uncore, DDREC);
337 	total += intel_uncore_read(uncore, CSIEC);
338 
339 	delta = total - ips->last_count1;
340 
341 	result = div_u64(div_u64(ips->m * delta, dt) + ips->c, 10);
342 
343 	ips->last_count1 = total;
344 	ips->last_time1 = now;
345 
346 	ips->chipset_power = result;
347 
348 	return result;
349 }
350 
351 static unsigned long ips_mch_val(struct intel_uncore *uncore)
352 {
353 	unsigned int m, x, b;
354 	u32 tsfs;
355 
356 	tsfs = intel_uncore_read(uncore, TSFS);
357 	x = intel_uncore_read8(uncore, TR1);
358 
359 	b = tsfs & TSFS_INTR_MASK;
360 	m = (tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT;
361 
362 	return m * x / 127 - b;
363 }
364 
365 static int _pxvid_to_vd(u8 pxvid)
366 {
367 	if (pxvid == 0)
368 		return 0;
369 
370 	if (pxvid >= 8 && pxvid < 31)
371 		pxvid = 31;
372 
373 	return (pxvid + 2) * 125;
374 }
375 
376 static u32 pvid_to_extvid(struct drm_i915_private *i915, u8 pxvid)
377 {
378 	const int vd = _pxvid_to_vd(pxvid);
379 
380 	if (INTEL_INFO(i915)->is_mobile)
381 		return max(vd - 1125, 0);
382 
383 	return vd;
384 }
385 
386 static void __gen5_ips_update(struct intel_ips *ips)
387 {
388 	struct intel_uncore *uncore =
389 		rps_to_uncore(container_of(ips, struct intel_rps, ips));
390 	u64 now, delta, dt;
391 	u32 count;
392 
393 	lockdep_assert_held(&mchdev_lock);
394 
395 	now = ktime_get_raw_ns();
396 	dt = now - ips->last_time2;
397 	do_div(dt, NSEC_PER_MSEC);
398 
399 	/* Don't divide by 0 */
400 	if (dt <= 10)
401 		return;
402 
403 	count = intel_uncore_read(uncore, GFXEC);
404 	delta = count - ips->last_count2;
405 
406 	ips->last_count2 = count;
407 	ips->last_time2 = now;
408 
409 	/* More magic constants... */
410 	ips->gfx_power = div_u64(delta * 1181, dt * 10);
411 }
412 
413 static void gen5_rps_update(struct intel_rps *rps)
414 {
415 	spin_lock_irq(&mchdev_lock);
416 	__gen5_ips_update(&rps->ips);
417 	spin_unlock_irq(&mchdev_lock);
418 }
419 
420 static unsigned int gen5_invert_freq(struct intel_rps *rps,
421 				     unsigned int val)
422 {
423 	/* Invert the frequency bin into an ips delay */
424 	val = rps->max_freq - val;
425 	val = rps->min_freq + val;
426 
427 	return val;
428 }
429 
430 static int __gen5_rps_set(struct intel_rps *rps, u8 val)
431 {
432 	struct intel_uncore *uncore = rps_to_uncore(rps);
433 	u16 rgvswctl;
434 
435 	lockdep_assert_held(&mchdev_lock);
436 
437 	rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
438 	if (rgvswctl & MEMCTL_CMD_STS) {
439 		drm_dbg(&rps_to_i915(rps)->drm,
440 			"gpu busy, RCS change rejected\n");
441 		return -EBUSY; /* still busy with another command */
442 	}
443 
444 	/* Invert the frequency bin into an ips delay */
445 	val = gen5_invert_freq(rps, val);
446 
447 	rgvswctl =
448 		(MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
449 		(val << MEMCTL_FREQ_SHIFT) |
450 		MEMCTL_SFCAVM;
451 	intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
452 	intel_uncore_posting_read16(uncore, MEMSWCTL);
453 
454 	rgvswctl |= MEMCTL_CMD_STS;
455 	intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
456 
457 	return 0;
458 }
459 
460 static int gen5_rps_set(struct intel_rps *rps, u8 val)
461 {
462 	int err;
463 
464 	spin_lock_irq(&mchdev_lock);
465 	err = __gen5_rps_set(rps, val);
466 	spin_unlock_irq(&mchdev_lock);
467 
468 	return err;
469 }
470 
471 static unsigned long intel_pxfreq(u32 vidfreq)
472 {
473 	int div = (vidfreq & 0x3f0000) >> 16;
474 	int post = (vidfreq & 0x3000) >> 12;
475 	int pre = (vidfreq & 0x7);
476 
477 	if (!pre)
478 		return 0;
479 
480 	return div * 133333 / (pre << post);
481 }
482 
483 static unsigned int init_emon(struct intel_uncore *uncore)
484 {
485 	u8 pxw[16];
486 	int i;
487 
488 	/* Disable to program */
489 	intel_uncore_write(uncore, ECR, 0);
490 	intel_uncore_posting_read(uncore, ECR);
491 
492 	/* Program energy weights for various events */
493 	intel_uncore_write(uncore, SDEW, 0x15040d00);
494 	intel_uncore_write(uncore, CSIEW0, 0x007f0000);
495 	intel_uncore_write(uncore, CSIEW1, 0x1e220004);
496 	intel_uncore_write(uncore, CSIEW2, 0x04000004);
497 
498 	for (i = 0; i < 5; i++)
499 		intel_uncore_write(uncore, PEW(i), 0);
500 	for (i = 0; i < 3; i++)
501 		intel_uncore_write(uncore, DEW(i), 0);
502 
503 	/* Program P-state weights to account for frequency power adjustment */
504 	for (i = 0; i < 16; i++) {
505 		u32 pxvidfreq = intel_uncore_read(uncore, PXVFREQ(i));
506 		unsigned int freq = intel_pxfreq(pxvidfreq);
507 		unsigned int vid =
508 			(pxvidfreq & PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
509 		unsigned int val;
510 
511 		val = vid * vid * freq / 1000 * 255;
512 		val /= 127 * 127 * 900;
513 
514 		pxw[i] = val;
515 	}
516 	/* Render standby states get 0 weight */
517 	pxw[14] = 0;
518 	pxw[15] = 0;
519 
520 	for (i = 0; i < 4; i++) {
521 		intel_uncore_write(uncore, PXW(i),
522 				   pxw[i * 4 + 0] << 24 |
523 				   pxw[i * 4 + 1] << 16 |
524 				   pxw[i * 4 + 2] <<  8 |
525 				   pxw[i * 4 + 3] <<  0);
526 	}
527 
528 	/* Adjust magic regs to magic values (more experimental results) */
529 	intel_uncore_write(uncore, OGW0, 0);
530 	intel_uncore_write(uncore, OGW1, 0);
531 	intel_uncore_write(uncore, EG0, 0x00007f00);
532 	intel_uncore_write(uncore, EG1, 0x0000000e);
533 	intel_uncore_write(uncore, EG2, 0x000e0000);
534 	intel_uncore_write(uncore, EG3, 0x68000300);
535 	intel_uncore_write(uncore, EG4, 0x42000000);
536 	intel_uncore_write(uncore, EG5, 0x00140031);
537 	intel_uncore_write(uncore, EG6, 0);
538 	intel_uncore_write(uncore, EG7, 0);
539 
540 	for (i = 0; i < 8; i++)
541 		intel_uncore_write(uncore, PXWL(i), 0);
542 
543 	/* Enable PMON + select events */
544 	intel_uncore_write(uncore, ECR, 0x80000019);
545 
546 	return intel_uncore_read(uncore, LCFUSE02) & LCFUSE_HIV_MASK;
547 }
548 
549 static bool gen5_rps_enable(struct intel_rps *rps)
550 {
551 	struct drm_i915_private *i915 = rps_to_i915(rps);
552 	struct intel_uncore *uncore = rps_to_uncore(rps);
553 	u8 fstart, vstart;
554 	u32 rgvmodectl;
555 
556 	spin_lock_irq(&mchdev_lock);
557 
558 	rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
559 
560 	/* Enable temp reporting */
561 	intel_uncore_write16(uncore, PMMISC,
562 			     intel_uncore_read16(uncore, PMMISC) | MCPPCE_EN);
563 	intel_uncore_write16(uncore, TSC1,
564 			     intel_uncore_read16(uncore, TSC1) | TSE);
565 
566 	/* 100ms RC evaluation intervals */
567 	intel_uncore_write(uncore, RCUPEI, 100000);
568 	intel_uncore_write(uncore, RCDNEI, 100000);
569 
570 	/* Set max/min thresholds to 90ms and 80ms respectively */
571 	intel_uncore_write(uncore, RCBMAXAVG, 90000);
572 	intel_uncore_write(uncore, RCBMINAVG, 80000);
573 
574 	intel_uncore_write(uncore, MEMIHYST, 1);
575 
576 	/* Set up min, max, and cur for interrupt handling */
577 	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
578 		MEMMODE_FSTART_SHIFT;
579 
580 	vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
581 		  PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
582 
583 	intel_uncore_write(uncore,
584 			   MEMINTREN,
585 			   MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
586 
587 	intel_uncore_write(uncore, VIDSTART, vstart);
588 	intel_uncore_posting_read(uncore, VIDSTART);
589 
590 	rgvmodectl |= MEMMODE_SWMODE_EN;
591 	intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
592 
593 	if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
594 			     MEMCTL_CMD_STS) == 0, 10))
595 		drm_err(&uncore->i915->drm,
596 			"stuck trying to change perf mode\n");
597 	mdelay(1);
598 
599 	__gen5_rps_set(rps, rps->cur_freq);
600 
601 	rps->ips.last_count1 = intel_uncore_read(uncore, DMIEC);
602 	rps->ips.last_count1 += intel_uncore_read(uncore, DDREC);
603 	rps->ips.last_count1 += intel_uncore_read(uncore, CSIEC);
604 	rps->ips.last_time1 = jiffies_to_msecs(jiffies);
605 
606 	rps->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
607 	rps->ips.last_time2 = ktime_get_raw_ns();
608 
609 	spin_lock(&i915->irq_lock);
610 	ilk_enable_display_irq(i915, DE_PCU_EVENT);
611 	spin_unlock(&i915->irq_lock);
612 
613 	spin_unlock_irq(&mchdev_lock);
614 
615 	rps->ips.corr = init_emon(uncore);
616 
617 	return true;
618 }
619 
620 static void gen5_rps_disable(struct intel_rps *rps)
621 {
622 	struct drm_i915_private *i915 = rps_to_i915(rps);
623 	struct intel_uncore *uncore = rps_to_uncore(rps);
624 	u16 rgvswctl;
625 
626 	spin_lock_irq(&mchdev_lock);
627 
628 	spin_lock(&i915->irq_lock);
629 	ilk_disable_display_irq(i915, DE_PCU_EVENT);
630 	spin_unlock(&i915->irq_lock);
631 
632 	rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
633 
634 	/* Ack interrupts, disable EFC interrupt */
635 	intel_uncore_rmw(uncore, MEMINTREN, MEMINT_EVAL_CHG_EN, 0);
636 	intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
637 
638 	/* Go back to the starting frequency */
639 	__gen5_rps_set(rps, rps->idle_freq);
640 	mdelay(1);
641 	rgvswctl |= MEMCTL_CMD_STS;
642 	intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
643 	mdelay(1);
644 
645 	spin_unlock_irq(&mchdev_lock);
646 }
647 
648 static u32 rps_limits(struct intel_rps *rps, u8 val)
649 {
650 	u32 limits;
651 
652 	/*
653 	 * Only set the down limit when we've reached the lowest level to avoid
654 	 * getting more interrupts, otherwise leave this clear. This prevents a
655 	 * race in the hw when coming out of rc6: There's a tiny window where
656 	 * the hw runs at the minimal clock before selecting the desired
657 	 * frequency, if the down threshold expires in that window we will not
658 	 * receive a down interrupt.
659 	 */
660 	if (GRAPHICS_VER(rps_to_i915(rps)) >= 9) {
661 		limits = rps->max_freq_softlimit << 23;
662 		if (val <= rps->min_freq_softlimit)
663 			limits |= rps->min_freq_softlimit << 14;
664 	} else {
665 		limits = rps->max_freq_softlimit << 24;
666 		if (val <= rps->min_freq_softlimit)
667 			limits |= rps->min_freq_softlimit << 16;
668 	}
669 
670 	return limits;
671 }
672 
673 static void rps_set_power(struct intel_rps *rps, int new_power)
674 {
675 	struct intel_gt *gt = rps_to_gt(rps);
676 	struct intel_uncore *uncore = gt->uncore;
677 	u32 ei_up = 0, ei_down = 0;
678 
679 	lockdep_assert_held(&rps->power.mutex);
680 
681 	if (new_power == rps->power.mode)
682 		return;
683 
684 	/* Note the units here are not exactly 1us, but 1280ns. */
685 	switch (new_power) {
686 	case LOW_POWER:
687 		ei_up = 16000;
688 		ei_down = 32000;
689 		break;
690 
691 	case BETWEEN:
692 		ei_up = 13000;
693 		ei_down = 32000;
694 		break;
695 
696 	case HIGH_POWER:
697 		ei_up = 10000;
698 		ei_down = 32000;
699 		break;
700 	}
701 
702 	/* When byt can survive without system hang with dynamic
703 	 * sw freq adjustments, this restriction can be lifted.
704 	 */
705 	if (IS_VALLEYVIEW(gt->i915))
706 		goto skip_hw_write;
707 
708 	GT_TRACE(gt,
709 		 "changing power mode [%d], up %d%% @ %dus, down %d%% @ %dus\n",
710 		 new_power,
711 		 rps->power.up_threshold, ei_up,
712 		 rps->power.down_threshold, ei_down);
713 
714 	set(uncore, GEN6_RP_UP_EI,
715 	    intel_gt_ns_to_pm_interval(gt, ei_up * 1000));
716 	set(uncore, GEN6_RP_UP_THRESHOLD,
717 	    intel_gt_ns_to_pm_interval(gt,
718 				       ei_up * rps->power.up_threshold * 10));
719 
720 	set(uncore, GEN6_RP_DOWN_EI,
721 	    intel_gt_ns_to_pm_interval(gt, ei_down * 1000));
722 	set(uncore, GEN6_RP_DOWN_THRESHOLD,
723 	    intel_gt_ns_to_pm_interval(gt,
724 				       ei_down *
725 				       rps->power.down_threshold * 10));
726 
727 	set(uncore, GEN6_RP_CONTROL,
728 	    (GRAPHICS_VER(gt->i915) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
729 	    GEN6_RP_MEDIA_HW_NORMAL_MODE |
730 	    GEN6_RP_MEDIA_IS_GFX |
731 	    GEN6_RP_ENABLE |
732 	    GEN6_RP_UP_BUSY_AVG |
733 	    GEN6_RP_DOWN_IDLE_AVG);
734 
735 skip_hw_write:
736 	rps->power.mode = new_power;
737 }
738 
739 static void gen6_rps_set_thresholds(struct intel_rps *rps, u8 val)
740 {
741 	int new_power;
742 
743 	new_power = rps->power.mode;
744 	switch (rps->power.mode) {
745 	case LOW_POWER:
746 		if (val > rps->efficient_freq + 1 &&
747 		    val > rps->cur_freq)
748 			new_power = BETWEEN;
749 		break;
750 
751 	case BETWEEN:
752 		if (val <= rps->efficient_freq &&
753 		    val < rps->cur_freq)
754 			new_power = LOW_POWER;
755 		else if (val >= rps->rp0_freq &&
756 			 val > rps->cur_freq)
757 			new_power = HIGH_POWER;
758 		break;
759 
760 	case HIGH_POWER:
761 		if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
762 		    val < rps->cur_freq)
763 			new_power = BETWEEN;
764 		break;
765 	}
766 	/* Max/min bins are special */
767 	if (val <= rps->min_freq_softlimit)
768 		new_power = LOW_POWER;
769 	if (val >= rps->max_freq_softlimit)
770 		new_power = HIGH_POWER;
771 
772 	mutex_lock(&rps->power.mutex);
773 	if (rps->power.interactive)
774 		new_power = HIGH_POWER;
775 	rps_set_power(rps, new_power);
776 	mutex_unlock(&rps->power.mutex);
777 }
778 
779 void intel_rps_mark_interactive(struct intel_rps *rps, bool interactive)
780 {
781 	GT_TRACE(rps_to_gt(rps), "mark interactive: %s\n",
782 		 str_yes_no(interactive));
783 
784 	mutex_lock(&rps->power.mutex);
785 	if (interactive) {
786 		if (!rps->power.interactive++ && intel_rps_is_active(rps))
787 			rps_set_power(rps, HIGH_POWER);
788 	} else {
789 		GEM_BUG_ON(!rps->power.interactive);
790 		rps->power.interactive--;
791 	}
792 	mutex_unlock(&rps->power.mutex);
793 }
794 
795 static int gen6_rps_set(struct intel_rps *rps, u8 val)
796 {
797 	struct intel_uncore *uncore = rps_to_uncore(rps);
798 	struct drm_i915_private *i915 = rps_to_i915(rps);
799 	u32 swreq;
800 
801 	GEM_BUG_ON(rps_uses_slpc(rps));
802 
803 	if (GRAPHICS_VER(i915) >= 9)
804 		swreq = GEN9_FREQUENCY(val);
805 	else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
806 		swreq = HSW_FREQUENCY(val);
807 	else
808 		swreq = (GEN6_FREQUENCY(val) |
809 			 GEN6_OFFSET(0) |
810 			 GEN6_AGGRESSIVE_TURBO);
811 	set(uncore, GEN6_RPNSWREQ, swreq);
812 
813 	GT_TRACE(rps_to_gt(rps), "set val:%x, freq:%d, swreq:%x\n",
814 		 val, intel_gpu_freq(rps, val), swreq);
815 
816 	return 0;
817 }
818 
819 static int vlv_rps_set(struct intel_rps *rps, u8 val)
820 {
821 	struct drm_i915_private *i915 = rps_to_i915(rps);
822 	int err;
823 
824 	vlv_punit_get(i915);
825 	err = vlv_punit_write(i915, PUNIT_REG_GPU_FREQ_REQ, val);
826 	vlv_punit_put(i915);
827 
828 	GT_TRACE(rps_to_gt(rps), "set val:%x, freq:%d\n",
829 		 val, intel_gpu_freq(rps, val));
830 
831 	return err;
832 }
833 
834 static int rps_set(struct intel_rps *rps, u8 val, bool update)
835 {
836 	struct drm_i915_private *i915 = rps_to_i915(rps);
837 	int err;
838 
839 	if (val == rps->last_freq)
840 		return 0;
841 
842 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
843 		err = vlv_rps_set(rps, val);
844 	else if (GRAPHICS_VER(i915) >= 6)
845 		err = gen6_rps_set(rps, val);
846 	else
847 		err = gen5_rps_set(rps, val);
848 	if (err)
849 		return err;
850 
851 	if (update && GRAPHICS_VER(i915) >= 6)
852 		gen6_rps_set_thresholds(rps, val);
853 	rps->last_freq = val;
854 
855 	return 0;
856 }
857 
858 void intel_rps_unpark(struct intel_rps *rps)
859 {
860 	if (!intel_rps_is_enabled(rps))
861 		return;
862 
863 	GT_TRACE(rps_to_gt(rps), "unpark:%x\n", rps->cur_freq);
864 
865 	/*
866 	 * Use the user's desired frequency as a guide, but for better
867 	 * performance, jump directly to RPe as our starting frequency.
868 	 */
869 	mutex_lock(&rps->lock);
870 
871 	intel_rps_set_active(rps);
872 	intel_rps_set(rps,
873 		      clamp(rps->cur_freq,
874 			    rps->min_freq_softlimit,
875 			    rps->max_freq_softlimit));
876 
877 	mutex_unlock(&rps->lock);
878 
879 	rps->pm_iir = 0;
880 	if (intel_rps_has_interrupts(rps))
881 		rps_enable_interrupts(rps);
882 	if (intel_rps_uses_timer(rps))
883 		rps_start_timer(rps);
884 
885 	if (GRAPHICS_VER(rps_to_i915(rps)) == 5)
886 		gen5_rps_update(rps);
887 }
888 
889 void intel_rps_park(struct intel_rps *rps)
890 {
891 	int adj;
892 
893 	if (!intel_rps_is_enabled(rps))
894 		return;
895 
896 	if (!intel_rps_clear_active(rps))
897 		return;
898 
899 	if (intel_rps_uses_timer(rps))
900 		rps_stop_timer(rps);
901 	if (intel_rps_has_interrupts(rps))
902 		rps_disable_interrupts(rps);
903 
904 	if (rps->last_freq <= rps->idle_freq)
905 		return;
906 
907 	/*
908 	 * The punit delays the write of the frequency and voltage until it
909 	 * determines the GPU is awake. During normal usage we don't want to
910 	 * waste power changing the frequency if the GPU is sleeping (rc6).
911 	 * However, the GPU and driver is now idle and we do not want to delay
912 	 * switching to minimum voltage (reducing power whilst idle) as we do
913 	 * not expect to be woken in the near future and so must flush the
914 	 * change by waking the device.
915 	 *
916 	 * We choose to take the media powerwell (either would do to trick the
917 	 * punit into committing the voltage change) as that takes a lot less
918 	 * power than the render powerwell.
919 	 */
920 	intel_uncore_forcewake_get(rps_to_uncore(rps), FORCEWAKE_MEDIA);
921 	rps_set(rps, rps->idle_freq, false);
922 	intel_uncore_forcewake_put(rps_to_uncore(rps), FORCEWAKE_MEDIA);
923 
924 	/*
925 	 * Since we will try and restart from the previously requested
926 	 * frequency on unparking, treat this idle point as a downclock
927 	 * interrupt and reduce the frequency for resume. If we park/unpark
928 	 * more frequently than the rps worker can run, we will not respond
929 	 * to any EI and never see a change in frequency.
930 	 *
931 	 * (Note we accommodate Cherryview's limitation of only using an
932 	 * even bin by applying it to all.)
933 	 */
934 	adj = rps->last_adj;
935 	if (adj < 0)
936 		adj *= 2;
937 	else /* CHV needs even encode values */
938 		adj = -2;
939 	rps->last_adj = adj;
940 	rps->cur_freq = max_t(int, rps->cur_freq + adj, rps->min_freq);
941 	if (rps->cur_freq < rps->efficient_freq) {
942 		rps->cur_freq = rps->efficient_freq;
943 		rps->last_adj = 0;
944 	}
945 
946 	GT_TRACE(rps_to_gt(rps), "park:%x\n", rps->cur_freq);
947 }
948 
949 u32 intel_rps_get_boost_frequency(struct intel_rps *rps)
950 {
951 	struct intel_guc_slpc *slpc;
952 
953 	if (rps_uses_slpc(rps)) {
954 		slpc = rps_to_slpc(rps);
955 
956 		return slpc->boost_freq;
957 	} else {
958 		return intel_gpu_freq(rps, rps->boost_freq);
959 	}
960 }
961 
962 static int rps_set_boost_freq(struct intel_rps *rps, u32 val)
963 {
964 	bool boost = false;
965 
966 	/* Validate against (static) hardware limits */
967 	val = intel_freq_opcode(rps, val);
968 	if (val < rps->min_freq || val > rps->max_freq)
969 		return -EINVAL;
970 
971 	mutex_lock(&rps->lock);
972 	if (val != rps->boost_freq) {
973 		rps->boost_freq = val;
974 		boost = atomic_read(&rps->num_waiters);
975 	}
976 	mutex_unlock(&rps->lock);
977 	if (boost)
978 		queue_work(rps_to_gt(rps)->i915->unordered_wq, &rps->work);
979 
980 	return 0;
981 }
982 
983 int intel_rps_set_boost_frequency(struct intel_rps *rps, u32 freq)
984 {
985 	struct intel_guc_slpc *slpc;
986 
987 	if (rps_uses_slpc(rps)) {
988 		slpc = rps_to_slpc(rps);
989 
990 		return intel_guc_slpc_set_boost_freq(slpc, freq);
991 	} else {
992 		return rps_set_boost_freq(rps, freq);
993 	}
994 }
995 
996 void intel_rps_dec_waiters(struct intel_rps *rps)
997 {
998 	struct intel_guc_slpc *slpc;
999 
1000 	if (rps_uses_slpc(rps)) {
1001 		slpc = rps_to_slpc(rps);
1002 
1003 		intel_guc_slpc_dec_waiters(slpc);
1004 	} else {
1005 		atomic_dec(&rps->num_waiters);
1006 	}
1007 }
1008 
1009 void intel_rps_boost(struct i915_request *rq)
1010 {
1011 	struct intel_guc_slpc *slpc;
1012 
1013 	if (i915_request_signaled(rq) || i915_request_has_waitboost(rq))
1014 		return;
1015 
1016 	/* Serializes with i915_request_retire() */
1017 	if (!test_and_set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags)) {
1018 		struct intel_rps *rps = &READ_ONCE(rq->engine)->gt->rps;
1019 
1020 		if (rps_uses_slpc(rps)) {
1021 			slpc = rps_to_slpc(rps);
1022 
1023 			if (slpc->min_freq_softlimit >= slpc->boost_freq)
1024 				return;
1025 
1026 			/* Return if old value is non zero */
1027 			if (!atomic_fetch_inc(&slpc->num_waiters)) {
1028 				GT_TRACE(rps_to_gt(rps), "boost fence:%llx:%llx\n",
1029 					 rq->fence.context, rq->fence.seqno);
1030 				queue_work(rps_to_gt(rps)->i915->unordered_wq,
1031 					   &slpc->boost_work);
1032 			}
1033 
1034 			return;
1035 		}
1036 
1037 		if (atomic_fetch_inc(&rps->num_waiters))
1038 			return;
1039 
1040 		if (!intel_rps_is_active(rps))
1041 			return;
1042 
1043 		GT_TRACE(rps_to_gt(rps), "boost fence:%llx:%llx\n",
1044 			 rq->fence.context, rq->fence.seqno);
1045 
1046 		if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
1047 			queue_work(rps_to_gt(rps)->i915->unordered_wq, &rps->work);
1048 
1049 		WRITE_ONCE(rps->boosts, rps->boosts + 1); /* debug only */
1050 	}
1051 }
1052 
1053 int intel_rps_set(struct intel_rps *rps, u8 val)
1054 {
1055 	int err;
1056 
1057 	lockdep_assert_held(&rps->lock);
1058 	GEM_BUG_ON(val > rps->max_freq);
1059 	GEM_BUG_ON(val < rps->min_freq);
1060 
1061 	if (intel_rps_is_active(rps)) {
1062 		err = rps_set(rps, val, true);
1063 		if (err)
1064 			return err;
1065 
1066 		/*
1067 		 * Make sure we continue to get interrupts
1068 		 * until we hit the minimum or maximum frequencies.
1069 		 */
1070 		if (intel_rps_has_interrupts(rps)) {
1071 			struct intel_uncore *uncore = rps_to_uncore(rps);
1072 
1073 			set(uncore,
1074 			    GEN6_RP_INTERRUPT_LIMITS, rps_limits(rps, val));
1075 
1076 			set(uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, val));
1077 		}
1078 	}
1079 
1080 	rps->cur_freq = val;
1081 	return 0;
1082 }
1083 
1084 static u32 intel_rps_read_state_cap(struct intel_rps *rps)
1085 {
1086 	struct drm_i915_private *i915 = rps_to_i915(rps);
1087 	struct intel_uncore *uncore = rps_to_uncore(rps);
1088 
1089 	if (IS_GEN9_LP(i915))
1090 		return intel_uncore_read(uncore, BXT_RP_STATE_CAP);
1091 	else
1092 		return intel_uncore_read(uncore, GEN6_RP_STATE_CAP);
1093 }
1094 
1095 static void
1096 mtl_get_freq_caps(struct intel_rps *rps, struct intel_rps_freq_caps *caps)
1097 {
1098 	struct intel_uncore *uncore = rps_to_uncore(rps);
1099 	u32 rp_state_cap = rps_to_gt(rps)->type == GT_MEDIA ?
1100 				intel_uncore_read(uncore, MTL_MEDIAP_STATE_CAP) :
1101 				intel_uncore_read(uncore, MTL_RP_STATE_CAP);
1102 	u32 rpe = rps_to_gt(rps)->type == GT_MEDIA ?
1103 			intel_uncore_read(uncore, MTL_MPE_FREQUENCY) :
1104 			intel_uncore_read(uncore, MTL_GT_RPE_FREQUENCY);
1105 
1106 	/* MTL values are in units of 16.67 MHz */
1107 	caps->rp0_freq = REG_FIELD_GET(MTL_RP0_CAP_MASK, rp_state_cap);
1108 	caps->min_freq = REG_FIELD_GET(MTL_RPN_CAP_MASK, rp_state_cap);
1109 	caps->rp1_freq = REG_FIELD_GET(MTL_RPE_MASK, rpe);
1110 }
1111 
1112 static void
1113 __gen6_rps_get_freq_caps(struct intel_rps *rps, struct intel_rps_freq_caps *caps)
1114 {
1115 	struct drm_i915_private *i915 = rps_to_i915(rps);
1116 	u32 rp_state_cap;
1117 
1118 	rp_state_cap = intel_rps_read_state_cap(rps);
1119 
1120 	/* static values from HW: RP0 > RP1 > RPn (min_freq) */
1121 	if (IS_GEN9_LP(i915)) {
1122 		caps->rp0_freq = (rp_state_cap >> 16) & 0xff;
1123 		caps->rp1_freq = (rp_state_cap >>  8) & 0xff;
1124 		caps->min_freq = (rp_state_cap >>  0) & 0xff;
1125 	} else {
1126 		caps->rp0_freq = (rp_state_cap >>  0) & 0xff;
1127 		if (GRAPHICS_VER(i915) >= 10)
1128 			caps->rp1_freq = REG_FIELD_GET(RPE_MASK,
1129 						       intel_uncore_read(to_gt(i915)->uncore,
1130 						       GEN10_FREQ_INFO_REC));
1131 		else
1132 			caps->rp1_freq = (rp_state_cap >>  8) & 0xff;
1133 		caps->min_freq = (rp_state_cap >> 16) & 0xff;
1134 	}
1135 
1136 	if (IS_GEN9_BC(i915) || GRAPHICS_VER(i915) >= 11) {
1137 		/*
1138 		 * In this case rp_state_cap register reports frequencies in
1139 		 * units of 50 MHz. Convert these to the actual "hw unit", i.e.
1140 		 * units of 16.67 MHz
1141 		 */
1142 		caps->rp0_freq *= GEN9_FREQ_SCALER;
1143 		caps->rp1_freq *= GEN9_FREQ_SCALER;
1144 		caps->min_freq *= GEN9_FREQ_SCALER;
1145 	}
1146 }
1147 
1148 /**
1149  * gen6_rps_get_freq_caps - Get freq caps exposed by HW
1150  * @rps: the intel_rps structure
1151  * @caps: returned freq caps
1152  *
1153  * Returned "caps" frequencies should be converted to MHz using
1154  * intel_gpu_freq()
1155  */
1156 void gen6_rps_get_freq_caps(struct intel_rps *rps, struct intel_rps_freq_caps *caps)
1157 {
1158 	struct drm_i915_private *i915 = rps_to_i915(rps);
1159 
1160 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
1161 		return mtl_get_freq_caps(rps, caps);
1162 	else
1163 		return __gen6_rps_get_freq_caps(rps, caps);
1164 }
1165 
1166 static void gen6_rps_init(struct intel_rps *rps)
1167 {
1168 	struct drm_i915_private *i915 = rps_to_i915(rps);
1169 	struct intel_rps_freq_caps caps;
1170 
1171 	gen6_rps_get_freq_caps(rps, &caps);
1172 	rps->rp0_freq = caps.rp0_freq;
1173 	rps->rp1_freq = caps.rp1_freq;
1174 	rps->min_freq = caps.min_freq;
1175 
1176 	/* hw_max = RP0 until we check for overclocking */
1177 	rps->max_freq = rps->rp0_freq;
1178 
1179 	rps->efficient_freq = rps->rp1_freq;
1180 	if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
1181 	    IS_GEN9_BC(i915) || GRAPHICS_VER(i915) >= 11) {
1182 		u32 ddcc_status = 0;
1183 		u32 mult = 1;
1184 
1185 		if (IS_GEN9_BC(i915) || GRAPHICS_VER(i915) >= 11)
1186 			mult = GEN9_FREQ_SCALER;
1187 		if (snb_pcode_read(rps_to_gt(rps)->uncore,
1188 				   HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
1189 				   &ddcc_status, NULL) == 0)
1190 			rps->efficient_freq =
1191 				clamp_t(u32,
1192 					((ddcc_status >> 8) & 0xff) * mult,
1193 					rps->min_freq,
1194 					rps->max_freq);
1195 	}
1196 }
1197 
1198 static bool rps_reset(struct intel_rps *rps)
1199 {
1200 	struct drm_i915_private *i915 = rps_to_i915(rps);
1201 
1202 	/* force a reset */
1203 	rps->power.mode = -1;
1204 	rps->last_freq = -1;
1205 
1206 	if (rps_set(rps, rps->min_freq, true)) {
1207 		drm_err(&i915->drm, "Failed to reset RPS to initial values\n");
1208 		return false;
1209 	}
1210 
1211 	rps->cur_freq = rps->min_freq;
1212 	return true;
1213 }
1214 
1215 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
1216 static bool gen9_rps_enable(struct intel_rps *rps)
1217 {
1218 	struct intel_gt *gt = rps_to_gt(rps);
1219 	struct intel_uncore *uncore = gt->uncore;
1220 
1221 	/* Program defaults and thresholds for RPS */
1222 	if (GRAPHICS_VER(gt->i915) == 9)
1223 		intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
1224 				      GEN9_FREQUENCY(rps->rp1_freq));
1225 
1226 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 0xa);
1227 
1228 	rps->pm_events = GEN6_PM_RP_UP_THRESHOLD | GEN6_PM_RP_DOWN_THRESHOLD;
1229 
1230 	return rps_reset(rps);
1231 }
1232 
1233 static bool gen8_rps_enable(struct intel_rps *rps)
1234 {
1235 	struct intel_uncore *uncore = rps_to_uncore(rps);
1236 
1237 	intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
1238 			      HSW_FREQUENCY(rps->rp1_freq));
1239 
1240 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1241 
1242 	rps->pm_events = GEN6_PM_RP_UP_THRESHOLD | GEN6_PM_RP_DOWN_THRESHOLD;
1243 
1244 	return rps_reset(rps);
1245 }
1246 
1247 static bool gen6_rps_enable(struct intel_rps *rps)
1248 {
1249 	struct intel_uncore *uncore = rps_to_uncore(rps);
1250 
1251 	/* Power down if completely idle for over 50ms */
1252 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 50000);
1253 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1254 
1255 	rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
1256 			  GEN6_PM_RP_DOWN_THRESHOLD |
1257 			  GEN6_PM_RP_DOWN_TIMEOUT);
1258 
1259 	return rps_reset(rps);
1260 }
1261 
1262 static int chv_rps_max_freq(struct intel_rps *rps)
1263 {
1264 	struct drm_i915_private *i915 = rps_to_i915(rps);
1265 	struct intel_gt *gt = rps_to_gt(rps);
1266 	u32 val;
1267 
1268 	val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
1269 
1270 	switch (gt->info.sseu.eu_total) {
1271 	case 8:
1272 		/* (2 * 4) config */
1273 		val >>= FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT;
1274 		break;
1275 	case 12:
1276 		/* (2 * 6) config */
1277 		val >>= FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT;
1278 		break;
1279 	case 16:
1280 		/* (2 * 8) config */
1281 	default:
1282 		/* Setting (2 * 8) Min RP0 for any other combination */
1283 		val >>= FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT;
1284 		break;
1285 	}
1286 
1287 	return val & FB_GFX_FREQ_FUSE_MASK;
1288 }
1289 
1290 static int chv_rps_rpe_freq(struct intel_rps *rps)
1291 {
1292 	struct drm_i915_private *i915 = rps_to_i915(rps);
1293 	u32 val;
1294 
1295 	val = vlv_punit_read(i915, PUNIT_GPU_DUTYCYCLE_REG);
1296 	val >>= PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT;
1297 
1298 	return val & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
1299 }
1300 
1301 static int chv_rps_guar_freq(struct intel_rps *rps)
1302 {
1303 	struct drm_i915_private *i915 = rps_to_i915(rps);
1304 	u32 val;
1305 
1306 	val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
1307 
1308 	return val & FB_GFX_FREQ_FUSE_MASK;
1309 }
1310 
1311 static u32 chv_rps_min_freq(struct intel_rps *rps)
1312 {
1313 	struct drm_i915_private *i915 = rps_to_i915(rps);
1314 	u32 val;
1315 
1316 	val = vlv_punit_read(i915, FB_GFX_FMIN_AT_VMIN_FUSE);
1317 	val >>= FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT;
1318 
1319 	return val & FB_GFX_FREQ_FUSE_MASK;
1320 }
1321 
1322 static bool chv_rps_enable(struct intel_rps *rps)
1323 {
1324 	struct intel_uncore *uncore = rps_to_uncore(rps);
1325 	struct drm_i915_private *i915 = rps_to_i915(rps);
1326 	u32 val;
1327 
1328 	/* 1: Program defaults and thresholds for RPS*/
1329 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1330 	intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1331 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1332 	intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1333 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1334 
1335 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1336 
1337 	/* 2: Enable RPS */
1338 	intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1339 			      GEN6_RP_MEDIA_HW_NORMAL_MODE |
1340 			      GEN6_RP_MEDIA_IS_GFX |
1341 			      GEN6_RP_ENABLE |
1342 			      GEN6_RP_UP_BUSY_AVG |
1343 			      GEN6_RP_DOWN_IDLE_AVG);
1344 
1345 	rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
1346 			  GEN6_PM_RP_DOWN_THRESHOLD |
1347 			  GEN6_PM_RP_DOWN_TIMEOUT);
1348 
1349 	/* Setting Fixed Bias */
1350 	vlv_punit_get(i915);
1351 
1352 	val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
1353 	vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1354 
1355 	val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1356 
1357 	vlv_punit_put(i915);
1358 
1359 	/* RPS code assumes GPLL is used */
1360 	drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1361 		      "GPLL not enabled\n");
1362 
1363 	drm_dbg(&i915->drm, "GPLL enabled? %s\n",
1364 		str_yes_no(val & GPLLENABLE));
1365 	drm_dbg(&i915->drm, "GPU status: 0x%08x\n", val);
1366 
1367 	return rps_reset(rps);
1368 }
1369 
1370 static int vlv_rps_guar_freq(struct intel_rps *rps)
1371 {
1372 	struct drm_i915_private *i915 = rps_to_i915(rps);
1373 	u32 val, rp1;
1374 
1375 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1376 
1377 	rp1 = val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK;
1378 	rp1 >>= FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
1379 
1380 	return rp1;
1381 }
1382 
1383 static int vlv_rps_max_freq(struct intel_rps *rps)
1384 {
1385 	struct drm_i915_private *i915 = rps_to_i915(rps);
1386 	u32 val, rp0;
1387 
1388 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1389 
1390 	rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
1391 	/* Clamp to max */
1392 	rp0 = min_t(u32, rp0, 0xea);
1393 
1394 	return rp0;
1395 }
1396 
1397 static int vlv_rps_rpe_freq(struct intel_rps *rps)
1398 {
1399 	struct drm_i915_private *i915 = rps_to_i915(rps);
1400 	u32 val, rpe;
1401 
1402 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
1403 	rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
1404 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
1405 	rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
1406 
1407 	return rpe;
1408 }
1409 
1410 static int vlv_rps_min_freq(struct intel_rps *rps)
1411 {
1412 	struct drm_i915_private *i915 = rps_to_i915(rps);
1413 	u32 val;
1414 
1415 	val = vlv_punit_read(i915, PUNIT_REG_GPU_LFM) & 0xff;
1416 	/*
1417 	 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
1418 	 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
1419 	 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
1420 	 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
1421 	 * to make sure it matches what Punit accepts.
1422 	 */
1423 	return max_t(u32, val, 0xc0);
1424 }
1425 
1426 static bool vlv_rps_enable(struct intel_rps *rps)
1427 {
1428 	struct intel_uncore *uncore = rps_to_uncore(rps);
1429 	struct drm_i915_private *i915 = rps_to_i915(rps);
1430 	u32 val;
1431 
1432 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1433 	intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1434 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1435 	intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1436 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1437 
1438 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1439 
1440 	intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1441 			      GEN6_RP_MEDIA_TURBO |
1442 			      GEN6_RP_MEDIA_HW_NORMAL_MODE |
1443 			      GEN6_RP_MEDIA_IS_GFX |
1444 			      GEN6_RP_ENABLE |
1445 			      GEN6_RP_UP_BUSY_AVG |
1446 			      GEN6_RP_DOWN_IDLE_CONT);
1447 
1448 	/* WaGsvRC0ResidencyMethod:vlv */
1449 	rps->pm_events = GEN6_PM_RP_UP_EI_EXPIRED;
1450 
1451 	vlv_punit_get(i915);
1452 
1453 	/* Setting Fixed Bias */
1454 	val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
1455 	vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1456 
1457 	val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1458 
1459 	vlv_punit_put(i915);
1460 
1461 	/* RPS code assumes GPLL is used */
1462 	drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1463 		      "GPLL not enabled\n");
1464 
1465 	drm_dbg(&i915->drm, "GPLL enabled? %s\n",
1466 		str_yes_no(val & GPLLENABLE));
1467 	drm_dbg(&i915->drm, "GPU status: 0x%08x\n", val);
1468 
1469 	return rps_reset(rps);
1470 }
1471 
1472 static unsigned long __ips_gfx_val(struct intel_ips *ips)
1473 {
1474 	struct intel_rps *rps = container_of(ips, typeof(*rps), ips);
1475 	struct intel_uncore *uncore = rps_to_uncore(rps);
1476 	unsigned int t, state1, state2;
1477 	u32 pxvid, ext_v;
1478 	u64 corr, corr2;
1479 
1480 	lockdep_assert_held(&mchdev_lock);
1481 
1482 	pxvid = intel_uncore_read(uncore, PXVFREQ(rps->cur_freq));
1483 	pxvid = (pxvid >> 24) & 0x7f;
1484 	ext_v = pvid_to_extvid(rps_to_i915(rps), pxvid);
1485 
1486 	state1 = ext_v;
1487 
1488 	/* Revel in the empirically derived constants */
1489 
1490 	/* Correction factor in 1/100000 units */
1491 	t = ips_mch_val(uncore);
1492 	if (t > 80)
1493 		corr = t * 2349 + 135940;
1494 	else if (t >= 50)
1495 		corr = t * 964 + 29317;
1496 	else /* < 50 */
1497 		corr = t * 301 + 1004;
1498 
1499 	corr = div_u64(corr * 150142 * state1, 10000) - 78642;
1500 	corr2 = div_u64(corr, 100000) * ips->corr;
1501 
1502 	state2 = div_u64(corr2 * state1, 10000);
1503 	state2 /= 100; /* convert to mW */
1504 
1505 	__gen5_ips_update(ips);
1506 
1507 	return ips->gfx_power + state2;
1508 }
1509 
1510 static bool has_busy_stats(struct intel_rps *rps)
1511 {
1512 	struct intel_engine_cs *engine;
1513 	enum intel_engine_id id;
1514 
1515 	for_each_engine(engine, rps_to_gt(rps), id) {
1516 		if (!intel_engine_supports_stats(engine))
1517 			return false;
1518 	}
1519 
1520 	return true;
1521 }
1522 
1523 void intel_rps_enable(struct intel_rps *rps)
1524 {
1525 	struct drm_i915_private *i915 = rps_to_i915(rps);
1526 	struct intel_uncore *uncore = rps_to_uncore(rps);
1527 	bool enabled = false;
1528 
1529 	if (!HAS_RPS(i915))
1530 		return;
1531 
1532 	if (rps_uses_slpc(rps))
1533 		return;
1534 
1535 	intel_gt_check_clock_frequency(rps_to_gt(rps));
1536 
1537 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1538 	if (rps->max_freq <= rps->min_freq)
1539 		/* leave disabled, no room for dynamic reclocking */;
1540 	else if (IS_CHERRYVIEW(i915))
1541 		enabled = chv_rps_enable(rps);
1542 	else if (IS_VALLEYVIEW(i915))
1543 		enabled = vlv_rps_enable(rps);
1544 	else if (GRAPHICS_VER(i915) >= 9)
1545 		enabled = gen9_rps_enable(rps);
1546 	else if (GRAPHICS_VER(i915) >= 8)
1547 		enabled = gen8_rps_enable(rps);
1548 	else if (GRAPHICS_VER(i915) >= 6)
1549 		enabled = gen6_rps_enable(rps);
1550 	else if (IS_IRONLAKE_M(i915))
1551 		enabled = gen5_rps_enable(rps);
1552 	else
1553 		MISSING_CASE(GRAPHICS_VER(i915));
1554 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
1555 	if (!enabled)
1556 		return;
1557 
1558 	GT_TRACE(rps_to_gt(rps),
1559 		 "min:%x, max:%x, freq:[%d, %d], thresholds:[%u, %u]\n",
1560 		 rps->min_freq, rps->max_freq,
1561 		 intel_gpu_freq(rps, rps->min_freq),
1562 		 intel_gpu_freq(rps, rps->max_freq),
1563 		 rps->power.up_threshold,
1564 		 rps->power.down_threshold);
1565 
1566 	GEM_BUG_ON(rps->max_freq < rps->min_freq);
1567 	GEM_BUG_ON(rps->idle_freq > rps->max_freq);
1568 
1569 	GEM_BUG_ON(rps->efficient_freq < rps->min_freq);
1570 	GEM_BUG_ON(rps->efficient_freq > rps->max_freq);
1571 
1572 	if (has_busy_stats(rps))
1573 		intel_rps_set_timer(rps);
1574 	else if (GRAPHICS_VER(i915) >= 6 && GRAPHICS_VER(i915) <= 11)
1575 		intel_rps_set_interrupts(rps);
1576 	else
1577 		/* Ironlake currently uses intel_ips.ko */ {}
1578 
1579 	intel_rps_set_enabled(rps);
1580 }
1581 
1582 static void gen6_rps_disable(struct intel_rps *rps)
1583 {
1584 	set(rps_to_uncore(rps), GEN6_RP_CONTROL, 0);
1585 }
1586 
1587 void intel_rps_disable(struct intel_rps *rps)
1588 {
1589 	struct drm_i915_private *i915 = rps_to_i915(rps);
1590 
1591 	if (!intel_rps_is_enabled(rps))
1592 		return;
1593 
1594 	intel_rps_clear_enabled(rps);
1595 	intel_rps_clear_interrupts(rps);
1596 	intel_rps_clear_timer(rps);
1597 
1598 	if (GRAPHICS_VER(i915) >= 6)
1599 		gen6_rps_disable(rps);
1600 	else if (IS_IRONLAKE_M(i915))
1601 		gen5_rps_disable(rps);
1602 }
1603 
1604 static int byt_gpu_freq(struct intel_rps *rps, int val)
1605 {
1606 	/*
1607 	 * N = val - 0xb7
1608 	 * Slow = Fast = GPLL ref * N
1609 	 */
1610 	return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
1611 }
1612 
1613 static int byt_freq_opcode(struct intel_rps *rps, int val)
1614 {
1615 	return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
1616 }
1617 
1618 static int chv_gpu_freq(struct intel_rps *rps, int val)
1619 {
1620 	/*
1621 	 * N = val / 2
1622 	 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
1623 	 */
1624 	return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
1625 }
1626 
1627 static int chv_freq_opcode(struct intel_rps *rps, int val)
1628 {
1629 	/* CHV needs even values */
1630 	return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
1631 }
1632 
1633 int intel_gpu_freq(struct intel_rps *rps, int val)
1634 {
1635 	struct drm_i915_private *i915 = rps_to_i915(rps);
1636 
1637 	if (GRAPHICS_VER(i915) >= 9)
1638 		return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
1639 					 GEN9_FREQ_SCALER);
1640 	else if (IS_CHERRYVIEW(i915))
1641 		return chv_gpu_freq(rps, val);
1642 	else if (IS_VALLEYVIEW(i915))
1643 		return byt_gpu_freq(rps, val);
1644 	else if (GRAPHICS_VER(i915) >= 6)
1645 		return val * GT_FREQUENCY_MULTIPLIER;
1646 	else
1647 		return val;
1648 }
1649 
1650 int intel_freq_opcode(struct intel_rps *rps, int val)
1651 {
1652 	struct drm_i915_private *i915 = rps_to_i915(rps);
1653 
1654 	if (GRAPHICS_VER(i915) >= 9)
1655 		return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
1656 					 GT_FREQUENCY_MULTIPLIER);
1657 	else if (IS_CHERRYVIEW(i915))
1658 		return chv_freq_opcode(rps, val);
1659 	else if (IS_VALLEYVIEW(i915))
1660 		return byt_freq_opcode(rps, val);
1661 	else if (GRAPHICS_VER(i915) >= 6)
1662 		return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
1663 	else
1664 		return val;
1665 }
1666 
1667 static void vlv_init_gpll_ref_freq(struct intel_rps *rps)
1668 {
1669 	struct drm_i915_private *i915 = rps_to_i915(rps);
1670 
1671 	rps->gpll_ref_freq =
1672 		vlv_get_cck_clock(i915, "GPLL ref",
1673 				  CCK_GPLL_CLOCK_CONTROL,
1674 				  i915->czclk_freq);
1675 
1676 	drm_dbg(&i915->drm, "GPLL reference freq: %d kHz\n",
1677 		rps->gpll_ref_freq);
1678 }
1679 
1680 static void vlv_rps_init(struct intel_rps *rps)
1681 {
1682 	struct drm_i915_private *i915 = rps_to_i915(rps);
1683 
1684 	vlv_iosf_sb_get(i915,
1685 			BIT(VLV_IOSF_SB_PUNIT) |
1686 			BIT(VLV_IOSF_SB_NC) |
1687 			BIT(VLV_IOSF_SB_CCK));
1688 
1689 	vlv_init_gpll_ref_freq(rps);
1690 
1691 	rps->max_freq = vlv_rps_max_freq(rps);
1692 	rps->rp0_freq = rps->max_freq;
1693 	drm_dbg(&i915->drm, "max GPU freq: %d MHz (%u)\n",
1694 		intel_gpu_freq(rps, rps->max_freq), rps->max_freq);
1695 
1696 	rps->efficient_freq = vlv_rps_rpe_freq(rps);
1697 	drm_dbg(&i915->drm, "RPe GPU freq: %d MHz (%u)\n",
1698 		intel_gpu_freq(rps, rps->efficient_freq), rps->efficient_freq);
1699 
1700 	rps->rp1_freq = vlv_rps_guar_freq(rps);
1701 	drm_dbg(&i915->drm, "RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
1702 		intel_gpu_freq(rps, rps->rp1_freq), rps->rp1_freq);
1703 
1704 	rps->min_freq = vlv_rps_min_freq(rps);
1705 	drm_dbg(&i915->drm, "min GPU freq: %d MHz (%u)\n",
1706 		intel_gpu_freq(rps, rps->min_freq), rps->min_freq);
1707 
1708 	vlv_iosf_sb_put(i915,
1709 			BIT(VLV_IOSF_SB_PUNIT) |
1710 			BIT(VLV_IOSF_SB_NC) |
1711 			BIT(VLV_IOSF_SB_CCK));
1712 }
1713 
1714 static void chv_rps_init(struct intel_rps *rps)
1715 {
1716 	struct drm_i915_private *i915 = rps_to_i915(rps);
1717 
1718 	vlv_iosf_sb_get(i915,
1719 			BIT(VLV_IOSF_SB_PUNIT) |
1720 			BIT(VLV_IOSF_SB_NC) |
1721 			BIT(VLV_IOSF_SB_CCK));
1722 
1723 	vlv_init_gpll_ref_freq(rps);
1724 
1725 	rps->max_freq = chv_rps_max_freq(rps);
1726 	rps->rp0_freq = rps->max_freq;
1727 	drm_dbg(&i915->drm, "max GPU freq: %d MHz (%u)\n",
1728 		intel_gpu_freq(rps, rps->max_freq), rps->max_freq);
1729 
1730 	rps->efficient_freq = chv_rps_rpe_freq(rps);
1731 	drm_dbg(&i915->drm, "RPe GPU freq: %d MHz (%u)\n",
1732 		intel_gpu_freq(rps, rps->efficient_freq), rps->efficient_freq);
1733 
1734 	rps->rp1_freq = chv_rps_guar_freq(rps);
1735 	drm_dbg(&i915->drm, "RP1(Guar) GPU freq: %d MHz (%u)\n",
1736 		intel_gpu_freq(rps, rps->rp1_freq), rps->rp1_freq);
1737 
1738 	rps->min_freq = chv_rps_min_freq(rps);
1739 	drm_dbg(&i915->drm, "min GPU freq: %d MHz (%u)\n",
1740 		intel_gpu_freq(rps, rps->min_freq), rps->min_freq);
1741 
1742 	vlv_iosf_sb_put(i915,
1743 			BIT(VLV_IOSF_SB_PUNIT) |
1744 			BIT(VLV_IOSF_SB_NC) |
1745 			BIT(VLV_IOSF_SB_CCK));
1746 
1747 	drm_WARN_ONCE(&i915->drm, (rps->max_freq | rps->efficient_freq |
1748 				   rps->rp1_freq | rps->min_freq) & 1,
1749 		      "Odd GPU freq values\n");
1750 }
1751 
1752 static void vlv_c0_read(struct intel_uncore *uncore, struct intel_rps_ei *ei)
1753 {
1754 	ei->ktime = ktime_get_raw();
1755 	ei->render_c0 = intel_uncore_read(uncore, VLV_RENDER_C0_COUNT);
1756 	ei->media_c0 = intel_uncore_read(uncore, VLV_MEDIA_C0_COUNT);
1757 }
1758 
1759 static u32 vlv_wa_c0_ei(struct intel_rps *rps, u32 pm_iir)
1760 {
1761 	struct intel_uncore *uncore = rps_to_uncore(rps);
1762 	const struct intel_rps_ei *prev = &rps->ei;
1763 	struct intel_rps_ei now;
1764 	u32 events = 0;
1765 
1766 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
1767 		return 0;
1768 
1769 	vlv_c0_read(uncore, &now);
1770 
1771 	if (prev->ktime) {
1772 		u64 time, c0;
1773 		u32 render, media;
1774 
1775 		time = ktime_us_delta(now.ktime, prev->ktime);
1776 
1777 		time *= rps_to_i915(rps)->czclk_freq;
1778 
1779 		/* Workload can be split between render + media,
1780 		 * e.g. SwapBuffers being blitted in X after being rendered in
1781 		 * mesa. To account for this we need to combine both engines
1782 		 * into our activity counter.
1783 		 */
1784 		render = now.render_c0 - prev->render_c0;
1785 		media = now.media_c0 - prev->media_c0;
1786 		c0 = max(render, media);
1787 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1788 
1789 		if (c0 > time * rps->power.up_threshold)
1790 			events = GEN6_PM_RP_UP_THRESHOLD;
1791 		else if (c0 < time * rps->power.down_threshold)
1792 			events = GEN6_PM_RP_DOWN_THRESHOLD;
1793 	}
1794 
1795 	rps->ei = now;
1796 	return events;
1797 }
1798 
1799 static void rps_work(struct work_struct *work)
1800 {
1801 	struct intel_rps *rps = container_of(work, typeof(*rps), work);
1802 	struct intel_gt *gt = rps_to_gt(rps);
1803 	struct drm_i915_private *i915 = rps_to_i915(rps);
1804 	bool client_boost = false;
1805 	int new_freq, adj, min, max;
1806 	u32 pm_iir = 0;
1807 
1808 	spin_lock_irq(gt->irq_lock);
1809 	pm_iir = fetch_and_zero(&rps->pm_iir) & rps->pm_events;
1810 	client_boost = atomic_read(&rps->num_waiters);
1811 	spin_unlock_irq(gt->irq_lock);
1812 
1813 	/* Make sure we didn't queue anything we're not going to process. */
1814 	if (!pm_iir && !client_boost)
1815 		goto out;
1816 
1817 	mutex_lock(&rps->lock);
1818 	if (!intel_rps_is_active(rps)) {
1819 		mutex_unlock(&rps->lock);
1820 		return;
1821 	}
1822 
1823 	pm_iir |= vlv_wa_c0_ei(rps, pm_iir);
1824 
1825 	adj = rps->last_adj;
1826 	new_freq = rps->cur_freq;
1827 	min = rps->min_freq_softlimit;
1828 	max = rps->max_freq_softlimit;
1829 	if (client_boost)
1830 		max = rps->max_freq;
1831 
1832 	GT_TRACE(gt,
1833 		 "pm_iir:%x, client_boost:%s, last:%d, cur:%x, min:%x, max:%x\n",
1834 		 pm_iir, str_yes_no(client_boost),
1835 		 adj, new_freq, min, max);
1836 
1837 	if (client_boost && new_freq < rps->boost_freq) {
1838 		new_freq = rps->boost_freq;
1839 		adj = 0;
1840 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1841 		if (adj > 0)
1842 			adj *= 2;
1843 		else /* CHV needs even encode values */
1844 			adj = IS_CHERRYVIEW(gt->i915) ? 2 : 1;
1845 
1846 		if (new_freq >= rps->max_freq_softlimit)
1847 			adj = 0;
1848 	} else if (client_boost) {
1849 		adj = 0;
1850 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1851 		if (rps->cur_freq > rps->efficient_freq)
1852 			new_freq = rps->efficient_freq;
1853 		else if (rps->cur_freq > rps->min_freq_softlimit)
1854 			new_freq = rps->min_freq_softlimit;
1855 		adj = 0;
1856 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1857 		if (adj < 0)
1858 			adj *= 2;
1859 		else /* CHV needs even encode values */
1860 			adj = IS_CHERRYVIEW(gt->i915) ? -2 : -1;
1861 
1862 		if (new_freq <= rps->min_freq_softlimit)
1863 			adj = 0;
1864 	} else { /* unknown event */
1865 		adj = 0;
1866 	}
1867 
1868 	/*
1869 	 * sysfs frequency limits may have snuck in while
1870 	 * servicing the interrupt
1871 	 */
1872 	new_freq += adj;
1873 	new_freq = clamp_t(int, new_freq, min, max);
1874 
1875 	if (intel_rps_set(rps, new_freq)) {
1876 		drm_dbg(&i915->drm, "Failed to set new GPU frequency\n");
1877 		adj = 0;
1878 	}
1879 	rps->last_adj = adj;
1880 
1881 	mutex_unlock(&rps->lock);
1882 
1883 out:
1884 	spin_lock_irq(gt->irq_lock);
1885 	gen6_gt_pm_unmask_irq(gt, rps->pm_events);
1886 	spin_unlock_irq(gt->irq_lock);
1887 }
1888 
1889 void gen11_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1890 {
1891 	struct intel_gt *gt = rps_to_gt(rps);
1892 	const u32 events = rps->pm_events & pm_iir;
1893 
1894 	lockdep_assert_held(gt->irq_lock);
1895 
1896 	if (unlikely(!events))
1897 		return;
1898 
1899 	GT_TRACE(gt, "irq events:%x\n", events);
1900 
1901 	gen6_gt_pm_mask_irq(gt, events);
1902 
1903 	rps->pm_iir |= events;
1904 	queue_work(gt->i915->unordered_wq, &rps->work);
1905 }
1906 
1907 void gen6_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1908 {
1909 	struct intel_gt *gt = rps_to_gt(rps);
1910 	u32 events;
1911 
1912 	events = pm_iir & rps->pm_events;
1913 	if (events) {
1914 		spin_lock(gt->irq_lock);
1915 
1916 		GT_TRACE(gt, "irq events:%x\n", events);
1917 
1918 		gen6_gt_pm_mask_irq(gt, events);
1919 		rps->pm_iir |= events;
1920 
1921 		queue_work(gt->i915->unordered_wq, &rps->work);
1922 		spin_unlock(gt->irq_lock);
1923 	}
1924 
1925 	if (GRAPHICS_VER(gt->i915) >= 8)
1926 		return;
1927 
1928 	if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1929 		intel_engine_cs_irq(gt->engine[VECS0], pm_iir >> 10);
1930 
1931 	if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1932 		drm_dbg(&rps_to_i915(rps)->drm,
1933 			"Command parser error, pm_iir 0x%08x\n", pm_iir);
1934 }
1935 
1936 void gen5_rps_irq_handler(struct intel_rps *rps)
1937 {
1938 	struct intel_uncore *uncore = rps_to_uncore(rps);
1939 	u32 busy_up, busy_down, max_avg, min_avg;
1940 	u8 new_freq;
1941 
1942 	spin_lock(&mchdev_lock);
1943 
1944 	intel_uncore_write16(uncore,
1945 			     MEMINTRSTS,
1946 			     intel_uncore_read(uncore, MEMINTRSTS));
1947 
1948 	intel_uncore_write16(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
1949 	busy_up = intel_uncore_read(uncore, RCPREVBSYTUPAVG);
1950 	busy_down = intel_uncore_read(uncore, RCPREVBSYTDNAVG);
1951 	max_avg = intel_uncore_read(uncore, RCBMAXAVG);
1952 	min_avg = intel_uncore_read(uncore, RCBMINAVG);
1953 
1954 	/* Handle RCS change request from hw */
1955 	new_freq = rps->cur_freq;
1956 	if (busy_up > max_avg)
1957 		new_freq++;
1958 	else if (busy_down < min_avg)
1959 		new_freq--;
1960 	new_freq = clamp(new_freq,
1961 			 rps->min_freq_softlimit,
1962 			 rps->max_freq_softlimit);
1963 
1964 	if (new_freq != rps->cur_freq && !__gen5_rps_set(rps, new_freq))
1965 		rps->cur_freq = new_freq;
1966 
1967 	spin_unlock(&mchdev_lock);
1968 }
1969 
1970 void intel_rps_init_early(struct intel_rps *rps)
1971 {
1972 	mutex_init(&rps->lock);
1973 	mutex_init(&rps->power.mutex);
1974 
1975 	INIT_WORK(&rps->work, rps_work);
1976 	timer_setup(&rps->timer, rps_timer, 0);
1977 
1978 	atomic_set(&rps->num_waiters, 0);
1979 }
1980 
1981 void intel_rps_init(struct intel_rps *rps)
1982 {
1983 	struct drm_i915_private *i915 = rps_to_i915(rps);
1984 
1985 	if (rps_uses_slpc(rps))
1986 		return;
1987 
1988 	if (IS_CHERRYVIEW(i915))
1989 		chv_rps_init(rps);
1990 	else if (IS_VALLEYVIEW(i915))
1991 		vlv_rps_init(rps);
1992 	else if (GRAPHICS_VER(i915) >= 6)
1993 		gen6_rps_init(rps);
1994 	else if (IS_IRONLAKE_M(i915))
1995 		gen5_rps_init(rps);
1996 
1997 	/* Derive initial user preferences/limits from the hardware limits */
1998 	rps->max_freq_softlimit = rps->max_freq;
1999 	rps_to_gt(rps)->defaults.max_freq = rps->max_freq_softlimit;
2000 	rps->min_freq_softlimit = rps->min_freq;
2001 	rps_to_gt(rps)->defaults.min_freq = rps->min_freq_softlimit;
2002 
2003 	/* After setting max-softlimit, find the overclock max freq */
2004 	if (GRAPHICS_VER(i915) == 6 || IS_IVYBRIDGE(i915) || IS_HASWELL(i915)) {
2005 		u32 params = 0;
2006 
2007 		snb_pcode_read(rps_to_gt(rps)->uncore, GEN6_READ_OC_PARAMS, &params, NULL);
2008 		if (params & BIT(31)) { /* OC supported */
2009 			drm_dbg(&i915->drm,
2010 				"Overclocking supported, max: %dMHz, overclock: %dMHz\n",
2011 				(rps->max_freq & 0xff) * 50,
2012 				(params & 0xff) * 50);
2013 			rps->max_freq = params & 0xff;
2014 		}
2015 	}
2016 
2017 	/* Set default thresholds in % */
2018 	rps->power.up_threshold = 95;
2019 	rps_to_gt(rps)->defaults.rps_up_threshold = rps->power.up_threshold;
2020 	rps->power.down_threshold = 85;
2021 	rps_to_gt(rps)->defaults.rps_down_threshold = rps->power.down_threshold;
2022 
2023 	/* Finally allow us to boost to max by default */
2024 	rps->boost_freq = rps->max_freq;
2025 	rps->idle_freq = rps->min_freq;
2026 
2027 	/* Start in the middle, from here we will autotune based on workload */
2028 	rps->cur_freq = rps->efficient_freq;
2029 
2030 	rps->pm_intrmsk_mbz = 0;
2031 
2032 	/*
2033 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
2034 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
2035 	 *
2036 	 * TODO: verify if this can be reproduced on VLV,CHV.
2037 	 */
2038 	if (GRAPHICS_VER(i915) <= 7)
2039 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
2040 
2041 	if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) < 11)
2042 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
2043 
2044 	/* GuC needs ARAT expired interrupt unmasked */
2045 	if (intel_uc_uses_guc_submission(&rps_to_gt(rps)->uc))
2046 		rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
2047 }
2048 
2049 void intel_rps_sanitize(struct intel_rps *rps)
2050 {
2051 	if (rps_uses_slpc(rps))
2052 		return;
2053 
2054 	if (GRAPHICS_VER(rps_to_i915(rps)) >= 6)
2055 		rps_disable_interrupts(rps);
2056 }
2057 
2058 u32 intel_rps_read_rpstat(struct intel_rps *rps)
2059 {
2060 	struct drm_i915_private *i915 = rps_to_i915(rps);
2061 	i915_reg_t rpstat;
2062 
2063 	rpstat = (GRAPHICS_VER(i915) >= 12) ? GEN12_RPSTAT1 : GEN6_RPSTAT1;
2064 
2065 	return intel_uncore_read(rps_to_gt(rps)->uncore, rpstat);
2066 }
2067 
2068 static u32 intel_rps_get_cagf(struct intel_rps *rps, u32 rpstat)
2069 {
2070 	struct drm_i915_private *i915 = rps_to_i915(rps);
2071 	u32 cagf;
2072 
2073 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
2074 		cagf = REG_FIELD_GET(MTL_CAGF_MASK, rpstat);
2075 	else if (GRAPHICS_VER(i915) >= 12)
2076 		cagf = REG_FIELD_GET(GEN12_CAGF_MASK, rpstat);
2077 	else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
2078 		cagf = REG_FIELD_GET(RPE_MASK, rpstat);
2079 	else if (GRAPHICS_VER(i915) >= 9)
2080 		cagf = REG_FIELD_GET(GEN9_CAGF_MASK, rpstat);
2081 	else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
2082 		cagf = REG_FIELD_GET(HSW_CAGF_MASK, rpstat);
2083 	else if (GRAPHICS_VER(i915) >= 6)
2084 		cagf = REG_FIELD_GET(GEN6_CAGF_MASK, rpstat);
2085 	else
2086 		cagf = gen5_invert_freq(rps, REG_FIELD_GET(MEMSTAT_PSTATE_MASK, rpstat));
2087 
2088 	return cagf;
2089 }
2090 
2091 static u32 __read_cagf(struct intel_rps *rps, bool take_fw)
2092 {
2093 	struct drm_i915_private *i915 = rps_to_i915(rps);
2094 	struct intel_uncore *uncore = rps_to_uncore(rps);
2095 	i915_reg_t r = INVALID_MMIO_REG;
2096 	u32 freq;
2097 
2098 	/*
2099 	 * For Gen12+ reading freq from HW does not need a forcewake and
2100 	 * registers will return 0 freq when GT is in RC6
2101 	 */
2102 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70)) {
2103 		r = MTL_MIRROR_TARGET_WP1;
2104 	} else if (GRAPHICS_VER(i915) >= 12) {
2105 		r = GEN12_RPSTAT1;
2106 	} else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
2107 		vlv_punit_get(i915);
2108 		freq = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
2109 		vlv_punit_put(i915);
2110 	} else if (GRAPHICS_VER(i915) >= 6) {
2111 		r = GEN6_RPSTAT1;
2112 	} else {
2113 		r = MEMSTAT_ILK;
2114 	}
2115 
2116 	if (i915_mmio_reg_valid(r))
2117 		freq = take_fw ? intel_uncore_read(uncore, r) : intel_uncore_read_fw(uncore, r);
2118 
2119 	return intel_rps_get_cagf(rps, freq);
2120 }
2121 
2122 static u32 read_cagf(struct intel_rps *rps)
2123 {
2124 	return __read_cagf(rps, true);
2125 }
2126 
2127 u32 intel_rps_read_actual_frequency(struct intel_rps *rps)
2128 {
2129 	struct intel_runtime_pm *rpm = rps_to_uncore(rps)->rpm;
2130 	intel_wakeref_t wakeref;
2131 	u32 freq = 0;
2132 
2133 	with_intel_runtime_pm_if_in_use(rpm, wakeref)
2134 		freq = intel_gpu_freq(rps, read_cagf(rps));
2135 
2136 	return freq;
2137 }
2138 
2139 u32 intel_rps_read_actual_frequency_fw(struct intel_rps *rps)
2140 {
2141 	return intel_gpu_freq(rps, __read_cagf(rps, false));
2142 }
2143 
2144 static u32 intel_rps_read_punit_req(struct intel_rps *rps)
2145 {
2146 	struct intel_uncore *uncore = rps_to_uncore(rps);
2147 	struct intel_runtime_pm *rpm = rps_to_uncore(rps)->rpm;
2148 	intel_wakeref_t wakeref;
2149 	u32 freq = 0;
2150 
2151 	with_intel_runtime_pm_if_in_use(rpm, wakeref)
2152 		freq = intel_uncore_read(uncore, GEN6_RPNSWREQ);
2153 
2154 	return freq;
2155 }
2156 
2157 static u32 intel_rps_get_req(u32 pureq)
2158 {
2159 	u32 req = pureq >> GEN9_SW_REQ_UNSLICE_RATIO_SHIFT;
2160 
2161 	return req;
2162 }
2163 
2164 u32 intel_rps_read_punit_req_frequency(struct intel_rps *rps)
2165 {
2166 	u32 freq = intel_rps_get_req(intel_rps_read_punit_req(rps));
2167 
2168 	return intel_gpu_freq(rps, freq);
2169 }
2170 
2171 u32 intel_rps_get_requested_frequency(struct intel_rps *rps)
2172 {
2173 	if (rps_uses_slpc(rps))
2174 		return intel_rps_read_punit_req_frequency(rps);
2175 	else
2176 		return intel_gpu_freq(rps, rps->cur_freq);
2177 }
2178 
2179 u32 intel_rps_get_max_frequency(struct intel_rps *rps)
2180 {
2181 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2182 
2183 	if (rps_uses_slpc(rps))
2184 		return slpc->max_freq_softlimit;
2185 	else
2186 		return intel_gpu_freq(rps, rps->max_freq_softlimit);
2187 }
2188 
2189 /**
2190  * intel_rps_get_max_raw_freq - returns the max frequency in some raw format.
2191  * @rps: the intel_rps structure
2192  *
2193  * Returns the max frequency in a raw format. In newer platforms raw is in
2194  * units of 50 MHz.
2195  */
2196 u32 intel_rps_get_max_raw_freq(struct intel_rps *rps)
2197 {
2198 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2199 	u32 freq;
2200 
2201 	if (rps_uses_slpc(rps)) {
2202 		return DIV_ROUND_CLOSEST(slpc->rp0_freq,
2203 					 GT_FREQUENCY_MULTIPLIER);
2204 	} else {
2205 		freq = rps->max_freq;
2206 		if (GRAPHICS_VER(rps_to_i915(rps)) >= 9) {
2207 			/* Convert GT frequency to 50 MHz units */
2208 			freq /= GEN9_FREQ_SCALER;
2209 		}
2210 		return freq;
2211 	}
2212 }
2213 
2214 u32 intel_rps_get_rp0_frequency(struct intel_rps *rps)
2215 {
2216 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2217 
2218 	if (rps_uses_slpc(rps))
2219 		return slpc->rp0_freq;
2220 	else
2221 		return intel_gpu_freq(rps, rps->rp0_freq);
2222 }
2223 
2224 u32 intel_rps_get_rp1_frequency(struct intel_rps *rps)
2225 {
2226 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2227 
2228 	if (rps_uses_slpc(rps))
2229 		return slpc->rp1_freq;
2230 	else
2231 		return intel_gpu_freq(rps, rps->rp1_freq);
2232 }
2233 
2234 u32 intel_rps_get_rpn_frequency(struct intel_rps *rps)
2235 {
2236 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2237 
2238 	if (rps_uses_slpc(rps))
2239 		return slpc->min_freq;
2240 	else
2241 		return intel_gpu_freq(rps, rps->min_freq);
2242 }
2243 
2244 static void rps_frequency_dump(struct intel_rps *rps, struct drm_printer *p)
2245 {
2246 	struct intel_gt *gt = rps_to_gt(rps);
2247 	struct drm_i915_private *i915 = gt->i915;
2248 	struct intel_uncore *uncore = gt->uncore;
2249 	struct intel_rps_freq_caps caps;
2250 	u32 rp_state_limits;
2251 	u32 gt_perf_status;
2252 	u32 rpmodectl, rpinclimit, rpdeclimit;
2253 	u32 rpstat, cagf, reqf;
2254 	u32 rpcurupei, rpcurup, rpprevup;
2255 	u32 rpcurdownei, rpcurdown, rpprevdown;
2256 	u32 rpupei, rpupt, rpdownei, rpdownt;
2257 	u32 pm_ier, pm_imr, pm_isr, pm_iir, pm_mask;
2258 
2259 	rp_state_limits = intel_uncore_read(uncore, GEN6_RP_STATE_LIMITS);
2260 	gen6_rps_get_freq_caps(rps, &caps);
2261 	if (IS_GEN9_LP(i915))
2262 		gt_perf_status = intel_uncore_read(uncore, BXT_GT_PERF_STATUS);
2263 	else
2264 		gt_perf_status = intel_uncore_read(uncore, GEN6_GT_PERF_STATUS);
2265 
2266 	/* RPSTAT1 is in the GT power well */
2267 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
2268 
2269 	reqf = intel_uncore_read(uncore, GEN6_RPNSWREQ);
2270 	if (GRAPHICS_VER(i915) >= 9) {
2271 		reqf >>= 23;
2272 	} else {
2273 		reqf &= ~GEN6_TURBO_DISABLE;
2274 		if (IS_HASWELL(i915) || IS_BROADWELL(i915))
2275 			reqf >>= 24;
2276 		else
2277 			reqf >>= 25;
2278 	}
2279 	reqf = intel_gpu_freq(rps, reqf);
2280 
2281 	rpmodectl = intel_uncore_read(uncore, GEN6_RP_CONTROL);
2282 	rpinclimit = intel_uncore_read(uncore, GEN6_RP_UP_THRESHOLD);
2283 	rpdeclimit = intel_uncore_read(uncore, GEN6_RP_DOWN_THRESHOLD);
2284 
2285 	rpstat = intel_rps_read_rpstat(rps);
2286 	rpcurupei = intel_uncore_read(uncore, GEN6_RP_CUR_UP_EI) & GEN6_CURICONT_MASK;
2287 	rpcurup = intel_uncore_read(uncore, GEN6_RP_CUR_UP) & GEN6_CURBSYTAVG_MASK;
2288 	rpprevup = intel_uncore_read(uncore, GEN6_RP_PREV_UP) & GEN6_CURBSYTAVG_MASK;
2289 	rpcurdownei = intel_uncore_read(uncore, GEN6_RP_CUR_DOWN_EI) & GEN6_CURIAVG_MASK;
2290 	rpcurdown = intel_uncore_read(uncore, GEN6_RP_CUR_DOWN) & GEN6_CURBSYTAVG_MASK;
2291 	rpprevdown = intel_uncore_read(uncore, GEN6_RP_PREV_DOWN) & GEN6_CURBSYTAVG_MASK;
2292 
2293 	rpupei = intel_uncore_read(uncore, GEN6_RP_UP_EI);
2294 	rpupt = intel_uncore_read(uncore, GEN6_RP_UP_THRESHOLD);
2295 
2296 	rpdownei = intel_uncore_read(uncore, GEN6_RP_DOWN_EI);
2297 	rpdownt = intel_uncore_read(uncore, GEN6_RP_DOWN_THRESHOLD);
2298 
2299 	cagf = intel_rps_read_actual_frequency(rps);
2300 
2301 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
2302 
2303 	if (GRAPHICS_VER(i915) >= 11) {
2304 		pm_ier = intel_uncore_read(uncore, GEN11_GPM_WGBOXPERF_INTR_ENABLE);
2305 		pm_imr = intel_uncore_read(uncore, GEN11_GPM_WGBOXPERF_INTR_MASK);
2306 		/*
2307 		 * The equivalent to the PM ISR & IIR cannot be read
2308 		 * without affecting the current state of the system
2309 		 */
2310 		pm_isr = 0;
2311 		pm_iir = 0;
2312 	} else if (GRAPHICS_VER(i915) >= 8) {
2313 		pm_ier = intel_uncore_read(uncore, GEN8_GT_IER(2));
2314 		pm_imr = intel_uncore_read(uncore, GEN8_GT_IMR(2));
2315 		pm_isr = intel_uncore_read(uncore, GEN8_GT_ISR(2));
2316 		pm_iir = intel_uncore_read(uncore, GEN8_GT_IIR(2));
2317 	} else {
2318 		pm_ier = intel_uncore_read(uncore, GEN6_PMIER);
2319 		pm_imr = intel_uncore_read(uncore, GEN6_PMIMR);
2320 		pm_isr = intel_uncore_read(uncore, GEN6_PMISR);
2321 		pm_iir = intel_uncore_read(uncore, GEN6_PMIIR);
2322 	}
2323 	pm_mask = intel_uncore_read(uncore, GEN6_PMINTRMSK);
2324 
2325 	drm_printf(p, "Video Turbo Mode: %s\n",
2326 		   str_yes_no(rpmodectl & GEN6_RP_MEDIA_TURBO));
2327 	drm_printf(p, "HW control enabled: %s\n",
2328 		   str_yes_no(rpmodectl & GEN6_RP_ENABLE));
2329 	drm_printf(p, "SW control enabled: %s\n",
2330 		   str_yes_no((rpmodectl & GEN6_RP_MEDIA_MODE_MASK) == GEN6_RP_MEDIA_SW_MODE));
2331 
2332 	drm_printf(p, "PM IER=0x%08x IMR=0x%08x, MASK=0x%08x\n",
2333 		   pm_ier, pm_imr, pm_mask);
2334 	if (GRAPHICS_VER(i915) <= 10)
2335 		drm_printf(p, "PM ISR=0x%08x IIR=0x%08x\n",
2336 			   pm_isr, pm_iir);
2337 	drm_printf(p, "pm_intrmsk_mbz: 0x%08x\n",
2338 		   rps->pm_intrmsk_mbz);
2339 	drm_printf(p, "GT_PERF_STATUS: 0x%08x\n", gt_perf_status);
2340 	drm_printf(p, "Render p-state ratio: %d\n",
2341 		   (gt_perf_status & (GRAPHICS_VER(i915) >= 9 ? 0x1ff00 : 0xff00)) >> 8);
2342 	drm_printf(p, "Render p-state VID: %d\n",
2343 		   gt_perf_status & 0xff);
2344 	drm_printf(p, "Render p-state limit: %d\n",
2345 		   rp_state_limits & 0xff);
2346 	drm_printf(p, "RPSTAT1: 0x%08x\n", rpstat);
2347 	drm_printf(p, "RPMODECTL: 0x%08x\n", rpmodectl);
2348 	drm_printf(p, "RPINCLIMIT: 0x%08x\n", rpinclimit);
2349 	drm_printf(p, "RPDECLIMIT: 0x%08x\n", rpdeclimit);
2350 	drm_printf(p, "RPNSWREQ: %dMHz\n", reqf);
2351 	drm_printf(p, "CAGF: %dMHz\n", cagf);
2352 	drm_printf(p, "RP CUR UP EI: %d (%lldns)\n",
2353 		   rpcurupei,
2354 		   intel_gt_pm_interval_to_ns(gt, rpcurupei));
2355 	drm_printf(p, "RP CUR UP: %d (%lldns)\n",
2356 		   rpcurup, intel_gt_pm_interval_to_ns(gt, rpcurup));
2357 	drm_printf(p, "RP PREV UP: %d (%lldns)\n",
2358 		   rpprevup, intel_gt_pm_interval_to_ns(gt, rpprevup));
2359 	drm_printf(p, "Up threshold: %d%%\n",
2360 		   rps->power.up_threshold);
2361 	drm_printf(p, "RP UP EI: %d (%lldns)\n",
2362 		   rpupei, intel_gt_pm_interval_to_ns(gt, rpupei));
2363 	drm_printf(p, "RP UP THRESHOLD: %d (%lldns)\n",
2364 		   rpupt, intel_gt_pm_interval_to_ns(gt, rpupt));
2365 
2366 	drm_printf(p, "RP CUR DOWN EI: %d (%lldns)\n",
2367 		   rpcurdownei,
2368 		   intel_gt_pm_interval_to_ns(gt, rpcurdownei));
2369 	drm_printf(p, "RP CUR DOWN: %d (%lldns)\n",
2370 		   rpcurdown,
2371 		   intel_gt_pm_interval_to_ns(gt, rpcurdown));
2372 	drm_printf(p, "RP PREV DOWN: %d (%lldns)\n",
2373 		   rpprevdown,
2374 		   intel_gt_pm_interval_to_ns(gt, rpprevdown));
2375 	drm_printf(p, "Down threshold: %d%%\n",
2376 		   rps->power.down_threshold);
2377 	drm_printf(p, "RP DOWN EI: %d (%lldns)\n",
2378 		   rpdownei, intel_gt_pm_interval_to_ns(gt, rpdownei));
2379 	drm_printf(p, "RP DOWN THRESHOLD: %d (%lldns)\n",
2380 		   rpdownt, intel_gt_pm_interval_to_ns(gt, rpdownt));
2381 
2382 	drm_printf(p, "Lowest (RPN) frequency: %dMHz\n",
2383 		   intel_gpu_freq(rps, caps.min_freq));
2384 	drm_printf(p, "Nominal (RP1) frequency: %dMHz\n",
2385 		   intel_gpu_freq(rps, caps.rp1_freq));
2386 	drm_printf(p, "Max non-overclocked (RP0) frequency: %dMHz\n",
2387 		   intel_gpu_freq(rps, caps.rp0_freq));
2388 	drm_printf(p, "Max overclocked frequency: %dMHz\n",
2389 		   intel_gpu_freq(rps, rps->max_freq));
2390 
2391 	drm_printf(p, "Current freq: %d MHz\n",
2392 		   intel_gpu_freq(rps, rps->cur_freq));
2393 	drm_printf(p, "Actual freq: %d MHz\n", cagf);
2394 	drm_printf(p, "Idle freq: %d MHz\n",
2395 		   intel_gpu_freq(rps, rps->idle_freq));
2396 	drm_printf(p, "Min freq: %d MHz\n",
2397 		   intel_gpu_freq(rps, rps->min_freq));
2398 	drm_printf(p, "Boost freq: %d MHz\n",
2399 		   intel_gpu_freq(rps, rps->boost_freq));
2400 	drm_printf(p, "Max freq: %d MHz\n",
2401 		   intel_gpu_freq(rps, rps->max_freq));
2402 	drm_printf(p,
2403 		   "efficient (RPe) frequency: %d MHz\n",
2404 		   intel_gpu_freq(rps, rps->efficient_freq));
2405 }
2406 
2407 static void slpc_frequency_dump(struct intel_rps *rps, struct drm_printer *p)
2408 {
2409 	struct intel_gt *gt = rps_to_gt(rps);
2410 	struct intel_uncore *uncore = gt->uncore;
2411 	struct intel_rps_freq_caps caps;
2412 	u32 pm_mask;
2413 
2414 	gen6_rps_get_freq_caps(rps, &caps);
2415 	pm_mask = intel_uncore_read(uncore, GEN6_PMINTRMSK);
2416 
2417 	drm_printf(p, "PM MASK=0x%08x\n", pm_mask);
2418 	drm_printf(p, "pm_intrmsk_mbz: 0x%08x\n",
2419 		   rps->pm_intrmsk_mbz);
2420 	drm_printf(p, "RPSTAT1: 0x%08x\n", intel_rps_read_rpstat(rps));
2421 	drm_printf(p, "RPNSWREQ: %dMHz\n", intel_rps_get_requested_frequency(rps));
2422 	drm_printf(p, "Lowest (RPN) frequency: %dMHz\n",
2423 		   intel_gpu_freq(rps, caps.min_freq));
2424 	drm_printf(p, "Nominal (RP1) frequency: %dMHz\n",
2425 		   intel_gpu_freq(rps, caps.rp1_freq));
2426 	drm_printf(p, "Max non-overclocked (RP0) frequency: %dMHz\n",
2427 		   intel_gpu_freq(rps, caps.rp0_freq));
2428 	drm_printf(p, "Current freq: %d MHz\n",
2429 		   intel_rps_get_requested_frequency(rps));
2430 	drm_printf(p, "Actual freq: %d MHz\n",
2431 		   intel_rps_read_actual_frequency(rps));
2432 	drm_printf(p, "Min freq: %d MHz\n",
2433 		   intel_rps_get_min_frequency(rps));
2434 	drm_printf(p, "Boost freq: %d MHz\n",
2435 		   intel_rps_get_boost_frequency(rps));
2436 	drm_printf(p, "Max freq: %d MHz\n",
2437 		   intel_rps_get_max_frequency(rps));
2438 	drm_printf(p,
2439 		   "efficient (RPe) frequency: %d MHz\n",
2440 		   intel_gpu_freq(rps, caps.rp1_freq));
2441 }
2442 
2443 void gen6_rps_frequency_dump(struct intel_rps *rps, struct drm_printer *p)
2444 {
2445 	if (rps_uses_slpc(rps))
2446 		return slpc_frequency_dump(rps, p);
2447 	else
2448 		return rps_frequency_dump(rps, p);
2449 }
2450 
2451 static int set_max_freq(struct intel_rps *rps, u32 val)
2452 {
2453 	struct drm_i915_private *i915 = rps_to_i915(rps);
2454 	int ret = 0;
2455 
2456 	mutex_lock(&rps->lock);
2457 
2458 	val = intel_freq_opcode(rps, val);
2459 	if (val < rps->min_freq ||
2460 	    val > rps->max_freq ||
2461 	    val < rps->min_freq_softlimit) {
2462 		ret = -EINVAL;
2463 		goto unlock;
2464 	}
2465 
2466 	if (val > rps->rp0_freq)
2467 		drm_dbg(&i915->drm, "User requested overclocking to %d\n",
2468 			intel_gpu_freq(rps, val));
2469 
2470 	rps->max_freq_softlimit = val;
2471 
2472 	val = clamp_t(int, rps->cur_freq,
2473 		      rps->min_freq_softlimit,
2474 		      rps->max_freq_softlimit);
2475 
2476 	/*
2477 	 * We still need *_set_rps to process the new max_delay and
2478 	 * update the interrupt limits and PMINTRMSK even though
2479 	 * frequency request may be unchanged.
2480 	 */
2481 	intel_rps_set(rps, val);
2482 
2483 unlock:
2484 	mutex_unlock(&rps->lock);
2485 
2486 	return ret;
2487 }
2488 
2489 int intel_rps_set_max_frequency(struct intel_rps *rps, u32 val)
2490 {
2491 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2492 
2493 	if (rps_uses_slpc(rps))
2494 		return intel_guc_slpc_set_max_freq(slpc, val);
2495 	else
2496 		return set_max_freq(rps, val);
2497 }
2498 
2499 u32 intel_rps_get_min_frequency(struct intel_rps *rps)
2500 {
2501 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2502 
2503 	if (rps_uses_slpc(rps))
2504 		return slpc->min_freq_softlimit;
2505 	else
2506 		return intel_gpu_freq(rps, rps->min_freq_softlimit);
2507 }
2508 
2509 /**
2510  * intel_rps_get_min_raw_freq - returns the min frequency in some raw format.
2511  * @rps: the intel_rps structure
2512  *
2513  * Returns the min frequency in a raw format. In newer platforms raw is in
2514  * units of 50 MHz.
2515  */
2516 u32 intel_rps_get_min_raw_freq(struct intel_rps *rps)
2517 {
2518 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2519 	u32 freq;
2520 
2521 	if (rps_uses_slpc(rps)) {
2522 		return DIV_ROUND_CLOSEST(slpc->min_freq,
2523 					 GT_FREQUENCY_MULTIPLIER);
2524 	} else {
2525 		freq = rps->min_freq;
2526 		if (GRAPHICS_VER(rps_to_i915(rps)) >= 9) {
2527 			/* Convert GT frequency to 50 MHz units */
2528 			freq /= GEN9_FREQ_SCALER;
2529 		}
2530 		return freq;
2531 	}
2532 }
2533 
2534 static int set_min_freq(struct intel_rps *rps, u32 val)
2535 {
2536 	int ret = 0;
2537 
2538 	mutex_lock(&rps->lock);
2539 
2540 	val = intel_freq_opcode(rps, val);
2541 	if (val < rps->min_freq ||
2542 	    val > rps->max_freq ||
2543 	    val > rps->max_freq_softlimit) {
2544 		ret = -EINVAL;
2545 		goto unlock;
2546 	}
2547 
2548 	rps->min_freq_softlimit = val;
2549 
2550 	val = clamp_t(int, rps->cur_freq,
2551 		      rps->min_freq_softlimit,
2552 		      rps->max_freq_softlimit);
2553 
2554 	/*
2555 	 * We still need *_set_rps to process the new min_delay and
2556 	 * update the interrupt limits and PMINTRMSK even though
2557 	 * frequency request may be unchanged.
2558 	 */
2559 	intel_rps_set(rps, val);
2560 
2561 unlock:
2562 	mutex_unlock(&rps->lock);
2563 
2564 	return ret;
2565 }
2566 
2567 int intel_rps_set_min_frequency(struct intel_rps *rps, u32 val)
2568 {
2569 	struct intel_guc_slpc *slpc = rps_to_slpc(rps);
2570 
2571 	if (rps_uses_slpc(rps))
2572 		return intel_guc_slpc_set_min_freq(slpc, val);
2573 	else
2574 		return set_min_freq(rps, val);
2575 }
2576 
2577 u8 intel_rps_get_up_threshold(struct intel_rps *rps)
2578 {
2579 	return rps->power.up_threshold;
2580 }
2581 
2582 static int rps_set_threshold(struct intel_rps *rps, u8 *threshold, u8 val)
2583 {
2584 	int ret;
2585 
2586 	if (val > 100)
2587 		return -EINVAL;
2588 
2589 	ret = mutex_lock_interruptible(&rps->lock);
2590 	if (ret)
2591 		return ret;
2592 
2593 	if (*threshold == val)
2594 		goto out_unlock;
2595 
2596 	*threshold = val;
2597 
2598 	/* Force reset. */
2599 	rps->last_freq = -1;
2600 	mutex_lock(&rps->power.mutex);
2601 	rps->power.mode = -1;
2602 	mutex_unlock(&rps->power.mutex);
2603 
2604 	intel_rps_set(rps, clamp(rps->cur_freq,
2605 				 rps->min_freq_softlimit,
2606 				 rps->max_freq_softlimit));
2607 
2608 out_unlock:
2609 	mutex_unlock(&rps->lock);
2610 
2611 	return ret;
2612 }
2613 
2614 int intel_rps_set_up_threshold(struct intel_rps *rps, u8 threshold)
2615 {
2616 	return rps_set_threshold(rps, &rps->power.up_threshold, threshold);
2617 }
2618 
2619 u8 intel_rps_get_down_threshold(struct intel_rps *rps)
2620 {
2621 	return rps->power.down_threshold;
2622 }
2623 
2624 int intel_rps_set_down_threshold(struct intel_rps *rps, u8 threshold)
2625 {
2626 	return rps_set_threshold(rps, &rps->power.down_threshold, threshold);
2627 }
2628 
2629 static void intel_rps_set_manual(struct intel_rps *rps, bool enable)
2630 {
2631 	struct intel_uncore *uncore = rps_to_uncore(rps);
2632 	u32 state = enable ? GEN9_RPSWCTL_ENABLE : GEN9_RPSWCTL_DISABLE;
2633 
2634 	/* Allow punit to process software requests */
2635 	intel_uncore_write(uncore, GEN6_RP_CONTROL, state);
2636 }
2637 
2638 void intel_rps_raise_unslice(struct intel_rps *rps)
2639 {
2640 	struct intel_uncore *uncore = rps_to_uncore(rps);
2641 
2642 	mutex_lock(&rps->lock);
2643 
2644 	if (rps_uses_slpc(rps)) {
2645 		/* RP limits have not been initialized yet for SLPC path */
2646 		struct intel_rps_freq_caps caps;
2647 
2648 		gen6_rps_get_freq_caps(rps, &caps);
2649 
2650 		intel_rps_set_manual(rps, true);
2651 		intel_uncore_write(uncore, GEN6_RPNSWREQ,
2652 				   ((caps.rp0_freq <<
2653 				   GEN9_SW_REQ_UNSLICE_RATIO_SHIFT) |
2654 				   GEN9_IGNORE_SLICE_RATIO));
2655 		intel_rps_set_manual(rps, false);
2656 	} else {
2657 		intel_rps_set(rps, rps->rp0_freq);
2658 	}
2659 
2660 	mutex_unlock(&rps->lock);
2661 }
2662 
2663 void intel_rps_lower_unslice(struct intel_rps *rps)
2664 {
2665 	struct intel_uncore *uncore = rps_to_uncore(rps);
2666 
2667 	mutex_lock(&rps->lock);
2668 
2669 	if (rps_uses_slpc(rps)) {
2670 		/* RP limits have not been initialized yet for SLPC path */
2671 		struct intel_rps_freq_caps caps;
2672 
2673 		gen6_rps_get_freq_caps(rps, &caps);
2674 
2675 		intel_rps_set_manual(rps, true);
2676 		intel_uncore_write(uncore, GEN6_RPNSWREQ,
2677 				   ((caps.min_freq <<
2678 				   GEN9_SW_REQ_UNSLICE_RATIO_SHIFT) |
2679 				   GEN9_IGNORE_SLICE_RATIO));
2680 		intel_rps_set_manual(rps, false);
2681 	} else {
2682 		intel_rps_set(rps, rps->min_freq);
2683 	}
2684 
2685 	mutex_unlock(&rps->lock);
2686 }
2687 
2688 static u32 rps_read_mmio(struct intel_rps *rps, i915_reg_t reg32)
2689 {
2690 	struct intel_gt *gt = rps_to_gt(rps);
2691 	intel_wakeref_t wakeref;
2692 	u32 val;
2693 
2694 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
2695 		val = intel_uncore_read(gt->uncore, reg32);
2696 
2697 	return val;
2698 }
2699 
2700 bool rps_read_mask_mmio(struct intel_rps *rps,
2701 			i915_reg_t reg32, u32 mask)
2702 {
2703 	return rps_read_mmio(rps, reg32) & mask;
2704 }
2705 
2706 /* External interface for intel_ips.ko */
2707 
2708 static struct drm_i915_private __rcu *ips_mchdev;
2709 
2710 /*
2711  * Tells the intel_ips driver that the i915 driver is now loaded, if
2712  * IPS got loaded first.
2713  *
2714  * This awkward dance is so that neither module has to depend on the
2715  * other in order for IPS to do the appropriate communication of
2716  * GPU turbo limits to i915.
2717  */
2718 static void
2719 ips_ping_for_i915_load(void)
2720 {
2721 	void (*link)(void);
2722 
2723 	link = symbol_get(ips_link_to_i915_driver);
2724 	if (link) {
2725 		link();
2726 		symbol_put(ips_link_to_i915_driver);
2727 	}
2728 }
2729 
2730 void intel_rps_driver_register(struct intel_rps *rps)
2731 {
2732 	struct intel_gt *gt = rps_to_gt(rps);
2733 
2734 	/*
2735 	 * We only register the i915 ips part with intel-ips once everything is
2736 	 * set up, to avoid intel-ips sneaking in and reading bogus values.
2737 	 */
2738 	if (GRAPHICS_VER(gt->i915) == 5) {
2739 		GEM_BUG_ON(ips_mchdev);
2740 		rcu_assign_pointer(ips_mchdev, gt->i915);
2741 		ips_ping_for_i915_load();
2742 	}
2743 }
2744 
2745 void intel_rps_driver_unregister(struct intel_rps *rps)
2746 {
2747 	if (rcu_access_pointer(ips_mchdev) == rps_to_i915(rps))
2748 		rcu_assign_pointer(ips_mchdev, NULL);
2749 }
2750 
2751 static struct drm_i915_private *mchdev_get(void)
2752 {
2753 	struct drm_i915_private *i915;
2754 
2755 	rcu_read_lock();
2756 	i915 = rcu_dereference(ips_mchdev);
2757 	if (i915 && !kref_get_unless_zero(&i915->drm.ref))
2758 		i915 = NULL;
2759 	rcu_read_unlock();
2760 
2761 	return i915;
2762 }
2763 
2764 /**
2765  * i915_read_mch_val - return value for IPS use
2766  *
2767  * Calculate and return a value for the IPS driver to use when deciding whether
2768  * we have thermal and power headroom to increase CPU or GPU power budget.
2769  */
2770 unsigned long i915_read_mch_val(void)
2771 {
2772 	struct drm_i915_private *i915;
2773 	unsigned long chipset_val = 0;
2774 	unsigned long graphics_val = 0;
2775 	intel_wakeref_t wakeref;
2776 
2777 	i915 = mchdev_get();
2778 	if (!i915)
2779 		return 0;
2780 
2781 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
2782 		struct intel_ips *ips = &to_gt(i915)->rps.ips;
2783 
2784 		spin_lock_irq(&mchdev_lock);
2785 		chipset_val = __ips_chipset_val(ips);
2786 		graphics_val = __ips_gfx_val(ips);
2787 		spin_unlock_irq(&mchdev_lock);
2788 	}
2789 
2790 	drm_dev_put(&i915->drm);
2791 	return chipset_val + graphics_val;
2792 }
2793 EXPORT_SYMBOL_GPL(i915_read_mch_val);
2794 
2795 /**
2796  * i915_gpu_raise - raise GPU frequency limit
2797  *
2798  * Raise the limit; IPS indicates we have thermal headroom.
2799  */
2800 bool i915_gpu_raise(void)
2801 {
2802 	struct drm_i915_private *i915;
2803 	struct intel_rps *rps;
2804 
2805 	i915 = mchdev_get();
2806 	if (!i915)
2807 		return false;
2808 
2809 	rps = &to_gt(i915)->rps;
2810 
2811 	spin_lock_irq(&mchdev_lock);
2812 	if (rps->max_freq_softlimit < rps->max_freq)
2813 		rps->max_freq_softlimit++;
2814 	spin_unlock_irq(&mchdev_lock);
2815 
2816 	drm_dev_put(&i915->drm);
2817 	return true;
2818 }
2819 EXPORT_SYMBOL_GPL(i915_gpu_raise);
2820 
2821 /**
2822  * i915_gpu_lower - lower GPU frequency limit
2823  *
2824  * IPS indicates we're close to a thermal limit, so throttle back the GPU
2825  * frequency maximum.
2826  */
2827 bool i915_gpu_lower(void)
2828 {
2829 	struct drm_i915_private *i915;
2830 	struct intel_rps *rps;
2831 
2832 	i915 = mchdev_get();
2833 	if (!i915)
2834 		return false;
2835 
2836 	rps = &to_gt(i915)->rps;
2837 
2838 	spin_lock_irq(&mchdev_lock);
2839 	if (rps->max_freq_softlimit > rps->min_freq)
2840 		rps->max_freq_softlimit--;
2841 	spin_unlock_irq(&mchdev_lock);
2842 
2843 	drm_dev_put(&i915->drm);
2844 	return true;
2845 }
2846 EXPORT_SYMBOL_GPL(i915_gpu_lower);
2847 
2848 /**
2849  * i915_gpu_busy - indicate GPU business to IPS
2850  *
2851  * Tell the IPS driver whether or not the GPU is busy.
2852  */
2853 bool i915_gpu_busy(void)
2854 {
2855 	struct drm_i915_private *i915;
2856 	bool ret;
2857 
2858 	i915 = mchdev_get();
2859 	if (!i915)
2860 		return false;
2861 
2862 	ret = to_gt(i915)->awake;
2863 
2864 	drm_dev_put(&i915->drm);
2865 	return ret;
2866 }
2867 EXPORT_SYMBOL_GPL(i915_gpu_busy);
2868 
2869 /**
2870  * i915_gpu_turbo_disable - disable graphics turbo
2871  *
2872  * Disable graphics turbo by resetting the max frequency and setting the
2873  * current frequency to the default.
2874  */
2875 bool i915_gpu_turbo_disable(void)
2876 {
2877 	struct drm_i915_private *i915;
2878 	struct intel_rps *rps;
2879 	bool ret;
2880 
2881 	i915 = mchdev_get();
2882 	if (!i915)
2883 		return false;
2884 
2885 	rps = &to_gt(i915)->rps;
2886 
2887 	spin_lock_irq(&mchdev_lock);
2888 	rps->max_freq_softlimit = rps->min_freq;
2889 	ret = !__gen5_rps_set(&to_gt(i915)->rps, rps->min_freq);
2890 	spin_unlock_irq(&mchdev_lock);
2891 
2892 	drm_dev_put(&i915->drm);
2893 	return ret;
2894 }
2895 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
2896 
2897 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2898 #include "selftest_rps.c"
2899 #include "selftest_slpc.c"
2900 #endif
2901