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