Home | History | Annotate | Line # | Download | only in gt
intel_rps.c revision 1.1.1.1
      1 /*	$NetBSD: intel_rps.c,v 1.1.1.1 2021/12/18 20:15:33 riastradh Exp $	*/
      2 
      3 /*
      4  * SPDX-License-Identifier: MIT
      5  *
      6  * Copyright  2019 Intel Corporation
      7  */
      8 
      9 #include <sys/cdefs.h>
     10 __KERNEL_RCSID(0, "$NetBSD: intel_rps.c,v 1.1.1.1 2021/12/18 20:15:33 riastradh Exp $");
     11 
     12 #include "i915_drv.h"
     13 #include "intel_gt.h"
     14 #include "intel_gt_irq.h"
     15 #include "intel_gt_pm_irq.h"
     16 #include "intel_rps.h"
     17 #include "intel_sideband.h"
     18 #include "../../../platform/x86/intel_ips.h"
     19 
     20 /*
     21  * Lock protecting IPS related data structures
     22  */
     23 static DEFINE_SPINLOCK(mchdev_lock);
     24 
     25 static struct intel_gt *rps_to_gt(struct intel_rps *rps)
     26 {
     27 	return container_of(rps, struct intel_gt, rps);
     28 }
     29 
     30 static struct drm_i915_private *rps_to_i915(struct intel_rps *rps)
     31 {
     32 	return rps_to_gt(rps)->i915;
     33 }
     34 
     35 static struct intel_uncore *rps_to_uncore(struct intel_rps *rps)
     36 {
     37 	return rps_to_gt(rps)->uncore;
     38 }
     39 
     40 static u32 rps_pm_sanitize_mask(struct intel_rps *rps, u32 mask)
     41 {
     42 	return mask & ~rps->pm_intrmsk_mbz;
     43 }
     44 
     45 static inline void set(struct intel_uncore *uncore, i915_reg_t reg, u32 val)
     46 {
     47 	intel_uncore_write_fw(uncore, reg, val);
     48 }
     49 
     50 static u32 rps_pm_mask(struct intel_rps *rps, u8 val)
     51 {
     52 	u32 mask = 0;
     53 
     54 	/* We use UP_EI_EXPIRED interrupts for both up/down in manual mode */
     55 	if (val > rps->min_freq_softlimit)
     56 		mask |= (GEN6_PM_RP_UP_EI_EXPIRED |
     57 			 GEN6_PM_RP_DOWN_THRESHOLD |
     58 			 GEN6_PM_RP_DOWN_TIMEOUT);
     59 
     60 	if (val < rps->max_freq_softlimit)
     61 		mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
     62 
     63 	mask &= rps->pm_events;
     64 
     65 	return rps_pm_sanitize_mask(rps, ~mask);
     66 }
     67 
     68 static void rps_reset_ei(struct intel_rps *rps)
     69 {
     70 	memset(&rps->ei, 0, sizeof(rps->ei));
     71 }
     72 
     73 static void rps_enable_interrupts(struct intel_rps *rps)
     74 {
     75 	struct intel_gt *gt = rps_to_gt(rps);
     76 
     77 	rps_reset_ei(rps);
     78 
     79 	if (IS_VALLEYVIEW(gt->i915))
     80 		/* WaGsvRC0ResidencyMethod:vlv */
     81 		rps->pm_events = GEN6_PM_RP_UP_EI_EXPIRED;
     82 	else
     83 		rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
     84 				  GEN6_PM_RP_DOWN_THRESHOLD |
     85 				  GEN6_PM_RP_DOWN_TIMEOUT);
     86 
     87 	spin_lock_irq(&gt->irq_lock);
     88 	gen6_gt_pm_enable_irq(gt, rps->pm_events);
     89 	spin_unlock_irq(&gt->irq_lock);
     90 
     91 	set(gt->uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, rps->cur_freq));
     92 }
     93 
     94 static void gen6_rps_reset_interrupts(struct intel_rps *rps)
     95 {
     96 	gen6_gt_pm_reset_iir(rps_to_gt(rps), GEN6_PM_RPS_EVENTS);
     97 }
     98 
     99 static void gen11_rps_reset_interrupts(struct intel_rps *rps)
    100 {
    101 	while (gen11_gt_reset_one_iir(rps_to_gt(rps), 0, GEN11_GTPM))
    102 		;
    103 }
    104 
    105 static void rps_reset_interrupts(struct intel_rps *rps)
    106 {
    107 	struct intel_gt *gt = rps_to_gt(rps);
    108 
    109 	spin_lock_irq(&gt->irq_lock);
    110 	if (INTEL_GEN(gt->i915) >= 11)
    111 		gen11_rps_reset_interrupts(rps);
    112 	else
    113 		gen6_rps_reset_interrupts(rps);
    114 
    115 	rps->pm_iir = 0;
    116 	spin_unlock_irq(&gt->irq_lock);
    117 }
    118 
    119 static void rps_disable_interrupts(struct intel_rps *rps)
    120 {
    121 	struct intel_gt *gt = rps_to_gt(rps);
    122 
    123 	rps->pm_events = 0;
    124 
    125 	set(gt->uncore, GEN6_PMINTRMSK, rps_pm_sanitize_mask(rps, ~0u));
    126 
    127 	spin_lock_irq(&gt->irq_lock);
    128 	gen6_gt_pm_disable_irq(gt, GEN6_PM_RPS_EVENTS);
    129 	spin_unlock_irq(&gt->irq_lock);
    130 
    131 	intel_synchronize_irq(gt->i915);
    132 
    133 	/*
    134 	 * Now that we will not be generating any more work, flush any
    135 	 * outstanding tasks. As we are called on the RPS idle path,
    136 	 * we will reset the GPU to minimum frequencies, so the current
    137 	 * state of the worker can be discarded.
    138 	 */
    139 	cancel_work_sync(&rps->work);
    140 
    141 	rps_reset_interrupts(rps);
    142 }
    143 
    144 static const struct cparams {
    145 	u16 i;
    146 	u16 t;
    147 	u16 m;
    148 	u16 c;
    149 } cparams[] = {
    150 	{ 1, 1333, 301, 28664 },
    151 	{ 1, 1066, 294, 24460 },
    152 	{ 1, 800, 294, 25192 },
    153 	{ 0, 1333, 276, 27605 },
    154 	{ 0, 1066, 276, 27605 },
    155 	{ 0, 800, 231, 23784 },
    156 };
    157 
    158 static void gen5_rps_init(struct intel_rps *rps)
    159 {
    160 	struct drm_i915_private *i915 = rps_to_i915(rps);
    161 	struct intel_uncore *uncore = rps_to_uncore(rps);
    162 	u8 fmax, fmin, fstart;
    163 	u32 rgvmodectl;
    164 	int c_m, i;
    165 
    166 	if (i915->fsb_freq <= 3200)
    167 		c_m = 0;
    168 	else if (i915->fsb_freq <= 4800)
    169 		c_m = 1;
    170 	else
    171 		c_m = 2;
    172 
    173 	for (i = 0; i < ARRAY_SIZE(cparams); i++) {
    174 		if (cparams[i].i == c_m && cparams[i].t == i915->mem_freq) {
    175 			rps->ips.m = cparams[i].m;
    176 			rps->ips.c = cparams[i].c;
    177 			break;
    178 		}
    179 	}
    180 
    181 	rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
    182 
    183 	/* Set up min, max, and cur for interrupt handling */
    184 	fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
    185 	fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
    186 	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
    187 		MEMMODE_FSTART_SHIFT;
    188 	DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
    189 			 fmax, fmin, fstart);
    190 
    191 	rps->min_freq = fmax;
    192 	rps->max_freq = fmin;
    193 
    194 	rps->idle_freq = rps->min_freq;
    195 	rps->cur_freq = rps->idle_freq;
    196 }
    197 
    198 static unsigned long
    199 __ips_chipset_val(struct intel_ips *ips)
    200 {
    201 	struct intel_uncore *uncore =
    202 		rps_to_uncore(container_of(ips, struct intel_rps, ips));
    203 	unsigned long now = jiffies_to_msecs(jiffies), dt;
    204 	unsigned long result;
    205 	u64 total, delta;
    206 
    207 	lockdep_assert_held(&mchdev_lock);
    208 
    209 	/*
    210 	 * Prevent division-by-zero if we are asking too fast.
    211 	 * Also, we don't get interesting results if we are polling
    212 	 * faster than once in 10ms, so just return the saved value
    213 	 * in such cases.
    214 	 */
    215 	dt = now - ips->last_time1;
    216 	if (dt <= 10)
    217 		return ips->chipset_power;
    218 
    219 	/* FIXME: handle per-counter overflow */
    220 	total = intel_uncore_read(uncore, DMIEC);
    221 	total += intel_uncore_read(uncore, DDREC);
    222 	total += intel_uncore_read(uncore, CSIEC);
    223 
    224 	delta = total - ips->last_count1;
    225 
    226 	result = div_u64(div_u64(ips->m * delta, dt) + ips->c, 10);
    227 
    228 	ips->last_count1 = total;
    229 	ips->last_time1 = now;
    230 
    231 	ips->chipset_power = result;
    232 
    233 	return result;
    234 }
    235 
    236 static unsigned long ips_mch_val(struct intel_uncore *uncore)
    237 {
    238 	unsigned int m, x, b;
    239 	u32 tsfs;
    240 
    241 	tsfs = intel_uncore_read(uncore, TSFS);
    242 	x = intel_uncore_read8(uncore, TR1);
    243 
    244 	b = tsfs & TSFS_INTR_MASK;
    245 	m = (tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT;
    246 
    247 	return m * x / 127 - b;
    248 }
    249 
    250 static int _pxvid_to_vd(u8 pxvid)
    251 {
    252 	if (pxvid == 0)
    253 		return 0;
    254 
    255 	if (pxvid >= 8 && pxvid < 31)
    256 		pxvid = 31;
    257 
    258 	return (pxvid + 2) * 125;
    259 }
    260 
    261 static u32 pvid_to_extvid(struct drm_i915_private *i915, u8 pxvid)
    262 {
    263 	const int vd = _pxvid_to_vd(pxvid);
    264 
    265 	if (INTEL_INFO(i915)->is_mobile)
    266 		return max(vd - 1125, 0);
    267 
    268 	return vd;
    269 }
    270 
    271 static void __gen5_ips_update(struct intel_ips *ips)
    272 {
    273 	struct intel_uncore *uncore =
    274 		rps_to_uncore(container_of(ips, struct intel_rps, ips));
    275 	u64 now, delta, dt;
    276 	u32 count;
    277 
    278 	lockdep_assert_held(&mchdev_lock);
    279 
    280 	now = ktime_get_raw_ns();
    281 	dt = now - ips->last_time2;
    282 	do_div(dt, NSEC_PER_MSEC);
    283 
    284 	/* Don't divide by 0 */
    285 	if (dt <= 10)
    286 		return;
    287 
    288 	count = intel_uncore_read(uncore, GFXEC);
    289 	delta = count - ips->last_count2;
    290 
    291 	ips->last_count2 = count;
    292 	ips->last_time2 = now;
    293 
    294 	/* More magic constants... */
    295 	ips->gfx_power = div_u64(delta * 1181, dt * 10);
    296 }
    297 
    298 static void gen5_rps_update(struct intel_rps *rps)
    299 {
    300 	spin_lock_irq(&mchdev_lock);
    301 	__gen5_ips_update(&rps->ips);
    302 	spin_unlock_irq(&mchdev_lock);
    303 }
    304 
    305 static bool gen5_rps_set(struct intel_rps *rps, u8 val)
    306 {
    307 	struct intel_uncore *uncore = rps_to_uncore(rps);
    308 	u16 rgvswctl;
    309 
    310 	lockdep_assert_held(&mchdev_lock);
    311 
    312 	rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
    313 	if (rgvswctl & MEMCTL_CMD_STS) {
    314 		DRM_DEBUG("gpu busy, RCS change rejected\n");
    315 		return false; /* still busy with another command */
    316 	}
    317 
    318 	/* Invert the frequency bin into an ips delay */
    319 	val = rps->max_freq - val;
    320 	val = rps->min_freq + val;
    321 
    322 	rgvswctl =
    323 		(MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
    324 		(val << MEMCTL_FREQ_SHIFT) |
    325 		MEMCTL_SFCAVM;
    326 	intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
    327 	intel_uncore_posting_read16(uncore, MEMSWCTL);
    328 
    329 	rgvswctl |= MEMCTL_CMD_STS;
    330 	intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
    331 
    332 	return true;
    333 }
    334 
    335 static unsigned long intel_pxfreq(u32 vidfreq)
    336 {
    337 	int div = (vidfreq & 0x3f0000) >> 16;
    338 	int post = (vidfreq & 0x3000) >> 12;
    339 	int pre = (vidfreq & 0x7);
    340 
    341 	if (!pre)
    342 		return 0;
    343 
    344 	return div * 133333 / (pre << post);
    345 }
    346 
    347 static unsigned int init_emon(struct intel_uncore *uncore)
    348 {
    349 	u8 pxw[16];
    350 	int i;
    351 
    352 	/* Disable to program */
    353 	intel_uncore_write(uncore, ECR, 0);
    354 	intel_uncore_posting_read(uncore, ECR);
    355 
    356 	/* Program energy weights for various events */
    357 	intel_uncore_write(uncore, SDEW, 0x15040d00);
    358 	intel_uncore_write(uncore, CSIEW0, 0x007f0000);
    359 	intel_uncore_write(uncore, CSIEW1, 0x1e220004);
    360 	intel_uncore_write(uncore, CSIEW2, 0x04000004);
    361 
    362 	for (i = 0; i < 5; i++)
    363 		intel_uncore_write(uncore, PEW(i), 0);
    364 	for (i = 0; i < 3; i++)
    365 		intel_uncore_write(uncore, DEW(i), 0);
    366 
    367 	/* Program P-state weights to account for frequency power adjustment */
    368 	for (i = 0; i < 16; i++) {
    369 		u32 pxvidfreq = intel_uncore_read(uncore, PXVFREQ(i));
    370 		unsigned int freq = intel_pxfreq(pxvidfreq);
    371 		unsigned int vid =
    372 			(pxvidfreq & PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
    373 		unsigned int val;
    374 
    375 		val = vid * vid * freq / 1000 * 255;
    376 		val /= 127 * 127 * 900;
    377 
    378 		pxw[i] = val;
    379 	}
    380 	/* Render standby states get 0 weight */
    381 	pxw[14] = 0;
    382 	pxw[15] = 0;
    383 
    384 	for (i = 0; i < 4; i++) {
    385 		intel_uncore_write(uncore, PXW(i),
    386 				   pxw[i * 4 + 0] << 24 |
    387 				   pxw[i * 4 + 1] << 16 |
    388 				   pxw[i * 4 + 2] <<  8 |
    389 				   pxw[i * 4 + 3] <<  0);
    390 	}
    391 
    392 	/* Adjust magic regs to magic values (more experimental results) */
    393 	intel_uncore_write(uncore, OGW0, 0);
    394 	intel_uncore_write(uncore, OGW1, 0);
    395 	intel_uncore_write(uncore, EG0, 0x00007f00);
    396 	intel_uncore_write(uncore, EG1, 0x0000000e);
    397 	intel_uncore_write(uncore, EG2, 0x000e0000);
    398 	intel_uncore_write(uncore, EG3, 0x68000300);
    399 	intel_uncore_write(uncore, EG4, 0x42000000);
    400 	intel_uncore_write(uncore, EG5, 0x00140031);
    401 	intel_uncore_write(uncore, EG6, 0);
    402 	intel_uncore_write(uncore, EG7, 0);
    403 
    404 	for (i = 0; i < 8; i++)
    405 		intel_uncore_write(uncore, PXWL(i), 0);
    406 
    407 	/* Enable PMON + select events */
    408 	intel_uncore_write(uncore, ECR, 0x80000019);
    409 
    410 	return intel_uncore_read(uncore, LCFUSE02) & LCFUSE_HIV_MASK;
    411 }
    412 
    413 static bool gen5_rps_enable(struct intel_rps *rps)
    414 {
    415 	struct intel_uncore *uncore = rps_to_uncore(rps);
    416 	u8 fstart, vstart;
    417 	u32 rgvmodectl;
    418 
    419 	spin_lock_irq(&mchdev_lock);
    420 
    421 	rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
    422 
    423 	/* Enable temp reporting */
    424 	intel_uncore_write16(uncore, PMMISC,
    425 			     intel_uncore_read16(uncore, PMMISC) | MCPPCE_EN);
    426 	intel_uncore_write16(uncore, TSC1,
    427 			     intel_uncore_read16(uncore, TSC1) | TSE);
    428 
    429 	/* 100ms RC evaluation intervals */
    430 	intel_uncore_write(uncore, RCUPEI, 100000);
    431 	intel_uncore_write(uncore, RCDNEI, 100000);
    432 
    433 	/* Set max/min thresholds to 90ms and 80ms respectively */
    434 	intel_uncore_write(uncore, RCBMAXAVG, 90000);
    435 	intel_uncore_write(uncore, RCBMINAVG, 80000);
    436 
    437 	intel_uncore_write(uncore, MEMIHYST, 1);
    438 
    439 	/* Set up min, max, and cur for interrupt handling */
    440 	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
    441 		MEMMODE_FSTART_SHIFT;
    442 
    443 	vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
    444 		  PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
    445 
    446 	intel_uncore_write(uncore,
    447 			   MEMINTREN,
    448 			   MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
    449 
    450 	intel_uncore_write(uncore, VIDSTART, vstart);
    451 	intel_uncore_posting_read(uncore, VIDSTART);
    452 
    453 	rgvmodectl |= MEMMODE_SWMODE_EN;
    454 	intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
    455 
    456 	if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
    457 			     MEMCTL_CMD_STS) == 0, 10))
    458 		DRM_ERROR("stuck trying to change perf mode\n");
    459 	mdelay(1);
    460 
    461 	gen5_rps_set(rps, rps->cur_freq);
    462 
    463 	rps->ips.last_count1 = intel_uncore_read(uncore, DMIEC);
    464 	rps->ips.last_count1 += intel_uncore_read(uncore, DDREC);
    465 	rps->ips.last_count1 += intel_uncore_read(uncore, CSIEC);
    466 	rps->ips.last_time1 = jiffies_to_msecs(jiffies);
    467 
    468 	rps->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
    469 	rps->ips.last_time2 = ktime_get_raw_ns();
    470 
    471 	spin_unlock_irq(&mchdev_lock);
    472 
    473 	rps->ips.corr = init_emon(uncore);
    474 
    475 	return true;
    476 }
    477 
    478 static void gen5_rps_disable(struct intel_rps *rps)
    479 {
    480 	struct intel_uncore *uncore = rps_to_uncore(rps);
    481 	u16 rgvswctl;
    482 
    483 	spin_lock_irq(&mchdev_lock);
    484 
    485 	rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
    486 
    487 	/* Ack interrupts, disable EFC interrupt */
    488 	intel_uncore_write(uncore, MEMINTREN,
    489 			   intel_uncore_read(uncore, MEMINTREN) &
    490 			   ~MEMINT_EVAL_CHG_EN);
    491 	intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
    492 	intel_uncore_write(uncore, DEIER,
    493 			   intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
    494 	intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
    495 	intel_uncore_write(uncore, DEIMR,
    496 			   intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
    497 
    498 	/* Go back to the starting frequency */
    499 	gen5_rps_set(rps, rps->idle_freq);
    500 	mdelay(1);
    501 	rgvswctl |= MEMCTL_CMD_STS;
    502 	intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
    503 	mdelay(1);
    504 
    505 	spin_unlock_irq(&mchdev_lock);
    506 }
    507 
    508 static u32 rps_limits(struct intel_rps *rps, u8 val)
    509 {
    510 	u32 limits;
    511 
    512 	/*
    513 	 * Only set the down limit when we've reached the lowest level to avoid
    514 	 * getting more interrupts, otherwise leave this clear. This prevents a
    515 	 * race in the hw when coming out of rc6: There's a tiny window where
    516 	 * the hw runs at the minimal clock before selecting the desired
    517 	 * frequency, if the down threshold expires in that window we will not
    518 	 * receive a down interrupt.
    519 	 */
    520 	if (INTEL_GEN(rps_to_i915(rps)) >= 9) {
    521 		limits = rps->max_freq_softlimit << 23;
    522 		if (val <= rps->min_freq_softlimit)
    523 			limits |= rps->min_freq_softlimit << 14;
    524 	} else {
    525 		limits = rps->max_freq_softlimit << 24;
    526 		if (val <= rps->min_freq_softlimit)
    527 			limits |= rps->min_freq_softlimit << 16;
    528 	}
    529 
    530 	return limits;
    531 }
    532 
    533 static void rps_set_power(struct intel_rps *rps, int new_power)
    534 {
    535 	struct intel_uncore *uncore = rps_to_uncore(rps);
    536 	struct drm_i915_private *i915 = rps_to_i915(rps);
    537 	u32 threshold_up = 0, threshold_down = 0; /* in % */
    538 	u32 ei_up = 0, ei_down = 0;
    539 
    540 	lockdep_assert_held(&rps->power.mutex);
    541 
    542 	if (new_power == rps->power.mode)
    543 		return;
    544 
    545 	/* Note the units here are not exactly 1us, but 1280ns. */
    546 	switch (new_power) {
    547 	case LOW_POWER:
    548 		/* Upclock if more than 95% busy over 16ms */
    549 		ei_up = 16000;
    550 		threshold_up = 95;
    551 
    552 		/* Downclock if less than 85% busy over 32ms */
    553 		ei_down = 32000;
    554 		threshold_down = 85;
    555 		break;
    556 
    557 	case BETWEEN:
    558 		/* Upclock if more than 90% busy over 13ms */
    559 		ei_up = 13000;
    560 		threshold_up = 90;
    561 
    562 		/* Downclock if less than 75% busy over 32ms */
    563 		ei_down = 32000;
    564 		threshold_down = 75;
    565 		break;
    566 
    567 	case HIGH_POWER:
    568 		/* Upclock if more than 85% busy over 10ms */
    569 		ei_up = 10000;
    570 		threshold_up = 85;
    571 
    572 		/* Downclock if less than 60% busy over 32ms */
    573 		ei_down = 32000;
    574 		threshold_down = 60;
    575 		break;
    576 	}
    577 
    578 	/* When byt can survive without system hang with dynamic
    579 	 * sw freq adjustments, this restriction can be lifted.
    580 	 */
    581 	if (IS_VALLEYVIEW(i915))
    582 		goto skip_hw_write;
    583 
    584 	set(uncore, GEN6_RP_UP_EI, GT_INTERVAL_FROM_US(i915, ei_up));
    585 	set(uncore, GEN6_RP_UP_THRESHOLD,
    586 	    GT_INTERVAL_FROM_US(i915, ei_up * threshold_up / 100));
    587 
    588 	set(uncore, GEN6_RP_DOWN_EI, GT_INTERVAL_FROM_US(i915, ei_down));
    589 	set(uncore, GEN6_RP_DOWN_THRESHOLD,
    590 	    GT_INTERVAL_FROM_US(i915, ei_down * threshold_down / 100));
    591 
    592 	set(uncore, GEN6_RP_CONTROL,
    593 	    (INTEL_GEN(i915) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
    594 	    GEN6_RP_MEDIA_HW_NORMAL_MODE |
    595 	    GEN6_RP_MEDIA_IS_GFX |
    596 	    GEN6_RP_ENABLE |
    597 	    GEN6_RP_UP_BUSY_AVG |
    598 	    GEN6_RP_DOWN_IDLE_AVG);
    599 
    600 skip_hw_write:
    601 	rps->power.mode = new_power;
    602 	rps->power.up_threshold = threshold_up;
    603 	rps->power.down_threshold = threshold_down;
    604 }
    605 
    606 static void gen6_rps_set_thresholds(struct intel_rps *rps, u8 val)
    607 {
    608 	int new_power;
    609 
    610 	new_power = rps->power.mode;
    611 	switch (rps->power.mode) {
    612 	case LOW_POWER:
    613 		if (val > rps->efficient_freq + 1 &&
    614 		    val > rps->cur_freq)
    615 			new_power = BETWEEN;
    616 		break;
    617 
    618 	case BETWEEN:
    619 		if (val <= rps->efficient_freq &&
    620 		    val < rps->cur_freq)
    621 			new_power = LOW_POWER;
    622 		else if (val >= rps->rp0_freq &&
    623 			 val > rps->cur_freq)
    624 			new_power = HIGH_POWER;
    625 		break;
    626 
    627 	case HIGH_POWER:
    628 		if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
    629 		    val < rps->cur_freq)
    630 			new_power = BETWEEN;
    631 		break;
    632 	}
    633 	/* Max/min bins are special */
    634 	if (val <= rps->min_freq_softlimit)
    635 		new_power = LOW_POWER;
    636 	if (val >= rps->max_freq_softlimit)
    637 		new_power = HIGH_POWER;
    638 
    639 	mutex_lock(&rps->power.mutex);
    640 	if (rps->power.interactive)
    641 		new_power = HIGH_POWER;
    642 	rps_set_power(rps, new_power);
    643 	mutex_unlock(&rps->power.mutex);
    644 }
    645 
    646 void intel_rps_mark_interactive(struct intel_rps *rps, bool interactive)
    647 {
    648 	mutex_lock(&rps->power.mutex);
    649 	if (interactive) {
    650 		if (!rps->power.interactive++ && rps->active)
    651 			rps_set_power(rps, HIGH_POWER);
    652 	} else {
    653 		GEM_BUG_ON(!rps->power.interactive);
    654 		rps->power.interactive--;
    655 	}
    656 	mutex_unlock(&rps->power.mutex);
    657 }
    658 
    659 static int gen6_rps_set(struct intel_rps *rps, u8 val)
    660 {
    661 	struct intel_uncore *uncore = rps_to_uncore(rps);
    662 	struct drm_i915_private *i915 = rps_to_i915(rps);
    663 	u32 swreq;
    664 
    665 	if (INTEL_GEN(i915) >= 9)
    666 		swreq = GEN9_FREQUENCY(val);
    667 	else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
    668 		swreq = HSW_FREQUENCY(val);
    669 	else
    670 		swreq = (GEN6_FREQUENCY(val) |
    671 			 GEN6_OFFSET(0) |
    672 			 GEN6_AGGRESSIVE_TURBO);
    673 	set(uncore, GEN6_RPNSWREQ, swreq);
    674 
    675 	return 0;
    676 }
    677 
    678 static int vlv_rps_set(struct intel_rps *rps, u8 val)
    679 {
    680 	struct drm_i915_private *i915 = rps_to_i915(rps);
    681 	int err;
    682 
    683 	vlv_punit_get(i915);
    684 	err = vlv_punit_write(i915, PUNIT_REG_GPU_FREQ_REQ, val);
    685 	vlv_punit_put(i915);
    686 
    687 	return err;
    688 }
    689 
    690 static int rps_set(struct intel_rps *rps, u8 val, bool update)
    691 {
    692 	struct drm_i915_private *i915 = rps_to_i915(rps);
    693 	int err;
    694 
    695 	if (INTEL_GEN(i915) < 6)
    696 		return 0;
    697 
    698 	if (val == rps->last_freq)
    699 		return 0;
    700 
    701 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
    702 		err = vlv_rps_set(rps, val);
    703 	else
    704 		err = gen6_rps_set(rps, val);
    705 	if (err)
    706 		return err;
    707 
    708 	if (update)
    709 		gen6_rps_set_thresholds(rps, val);
    710 	rps->last_freq = val;
    711 
    712 	return 0;
    713 }
    714 
    715 void intel_rps_unpark(struct intel_rps *rps)
    716 {
    717 	u8 freq;
    718 
    719 	if (!rps->enabled)
    720 		return;
    721 
    722 	/*
    723 	 * Use the user's desired frequency as a guide, but for better
    724 	 * performance, jump directly to RPe as our starting frequency.
    725 	 */
    726 	mutex_lock(&rps->lock);
    727 	rps->active = true;
    728 	freq = max(rps->cur_freq, rps->efficient_freq),
    729 	freq = clamp(freq, rps->min_freq_softlimit, rps->max_freq_softlimit);
    730 	intel_rps_set(rps, freq);
    731 	rps->last_adj = 0;
    732 	mutex_unlock(&rps->lock);
    733 
    734 	if (INTEL_GEN(rps_to_i915(rps)) >= 6)
    735 		rps_enable_interrupts(rps);
    736 
    737 	if (IS_GEN(rps_to_i915(rps), 5))
    738 		gen5_rps_update(rps);
    739 }
    740 
    741 void intel_rps_park(struct intel_rps *rps)
    742 {
    743 	struct drm_i915_private *i915 = rps_to_i915(rps);
    744 
    745 	if (!rps->enabled)
    746 		return;
    747 
    748 	if (INTEL_GEN(i915) >= 6)
    749 		rps_disable_interrupts(rps);
    750 
    751 	rps->active = false;
    752 	if (rps->last_freq <= rps->idle_freq)
    753 		return;
    754 
    755 	/*
    756 	 * The punit delays the write of the frequency and voltage until it
    757 	 * determines the GPU is awake. During normal usage we don't want to
    758 	 * waste power changing the frequency if the GPU is sleeping (rc6).
    759 	 * However, the GPU and driver is now idle and we do not want to delay
    760 	 * switching to minimum voltage (reducing power whilst idle) as we do
    761 	 * not expect to be woken in the near future and so must flush the
    762 	 * change by waking the device.
    763 	 *
    764 	 * We choose to take the media powerwell (either would do to trick the
    765 	 * punit into committing the voltage change) as that takes a lot less
    766 	 * power than the render powerwell.
    767 	 */
    768 	intel_uncore_forcewake_get(rps_to_uncore(rps), FORCEWAKE_MEDIA);
    769 	rps_set(rps, rps->idle_freq, false);
    770 	intel_uncore_forcewake_put(rps_to_uncore(rps), FORCEWAKE_MEDIA);
    771 }
    772 
    773 void intel_rps_boost(struct i915_request *rq)
    774 {
    775 	struct intel_rps *rps = &rq->engine->gt->rps;
    776 	unsigned long flags;
    777 
    778 	if (i915_request_signaled(rq) || !rps->active)
    779 		return;
    780 
    781 	/* Serializes with i915_request_retire() */
    782 	spin_lock_irqsave(&rq->lock, flags);
    783 	if (!i915_request_has_waitboost(rq) &&
    784 	    !dma_fence_is_signaled_locked(&rq->fence)) {
    785 		set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags);
    786 
    787 		if (!atomic_fetch_inc(&rps->num_waiters) &&
    788 		    READ_ONCE(rps->cur_freq) < rps->boost_freq)
    789 			schedule_work(&rps->work);
    790 
    791 		atomic_inc(&rps->boosts);
    792 	}
    793 	spin_unlock_irqrestore(&rq->lock, flags);
    794 }
    795 
    796 int intel_rps_set(struct intel_rps *rps, u8 val)
    797 {
    798 	int err;
    799 
    800 	lockdep_assert_held(&rps->lock);
    801 	GEM_BUG_ON(val > rps->max_freq);
    802 	GEM_BUG_ON(val < rps->min_freq);
    803 
    804 	if (rps->active) {
    805 		err = rps_set(rps, val, true);
    806 		if (err)
    807 			return err;
    808 
    809 		/*
    810 		 * Make sure we continue to get interrupts
    811 		 * until we hit the minimum or maximum frequencies.
    812 		 */
    813 		if (INTEL_GEN(rps_to_i915(rps)) >= 6) {
    814 			struct intel_uncore *uncore = rps_to_uncore(rps);
    815 
    816 			set(uncore,
    817 			    GEN6_RP_INTERRUPT_LIMITS, rps_limits(rps, val));
    818 
    819 			set(uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, val));
    820 		}
    821 	}
    822 
    823 	rps->cur_freq = val;
    824 	return 0;
    825 }
    826 
    827 static void gen6_rps_init(struct intel_rps *rps)
    828 {
    829 	struct drm_i915_private *i915 = rps_to_i915(rps);
    830 	struct intel_uncore *uncore = rps_to_uncore(rps);
    831 
    832 	/* All of these values are in units of 50MHz */
    833 
    834 	/* static values from HW: RP0 > RP1 > RPn (min_freq) */
    835 	if (IS_GEN9_LP(i915)) {
    836 		u32 rp_state_cap = intel_uncore_read(uncore, BXT_RP_STATE_CAP);
    837 
    838 		rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
    839 		rps->rp1_freq = (rp_state_cap >>  8) & 0xff;
    840 		rps->min_freq = (rp_state_cap >>  0) & 0xff;
    841 	} else {
    842 		u32 rp_state_cap = intel_uncore_read(uncore, GEN6_RP_STATE_CAP);
    843 
    844 		rps->rp0_freq = (rp_state_cap >>  0) & 0xff;
    845 		rps->rp1_freq = (rp_state_cap >>  8) & 0xff;
    846 		rps->min_freq = (rp_state_cap >> 16) & 0xff;
    847 	}
    848 
    849 	/* hw_max = RP0 until we check for overclocking */
    850 	rps->max_freq = rps->rp0_freq;
    851 
    852 	rps->efficient_freq = rps->rp1_freq;
    853 	if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
    854 	    IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
    855 		u32 ddcc_status = 0;
    856 
    857 		if (sandybridge_pcode_read(i915,
    858 					   HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
    859 					   &ddcc_status, NULL) == 0)
    860 			rps->efficient_freq =
    861 				clamp_t(u8,
    862 					(ddcc_status >> 8) & 0xff,
    863 					rps->min_freq,
    864 					rps->max_freq);
    865 	}
    866 
    867 	if (IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
    868 		/* Store the frequency values in 16.66 MHZ units, which is
    869 		 * the natural hardware unit for SKL
    870 		 */
    871 		rps->rp0_freq *= GEN9_FREQ_SCALER;
    872 		rps->rp1_freq *= GEN9_FREQ_SCALER;
    873 		rps->min_freq *= GEN9_FREQ_SCALER;
    874 		rps->max_freq *= GEN9_FREQ_SCALER;
    875 		rps->efficient_freq *= GEN9_FREQ_SCALER;
    876 	}
    877 }
    878 
    879 static bool rps_reset(struct intel_rps *rps)
    880 {
    881 	/* force a reset */
    882 	rps->power.mode = -1;
    883 	rps->last_freq = -1;
    884 
    885 	if (rps_set(rps, rps->min_freq, true)) {
    886 		DRM_ERROR("Failed to reset RPS to initial values\n");
    887 		return false;
    888 	}
    889 
    890 	rps->cur_freq = rps->min_freq;
    891 	return true;
    892 }
    893 
    894 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
    895 static bool gen9_rps_enable(struct intel_rps *rps)
    896 {
    897 	struct drm_i915_private *i915 = rps_to_i915(rps);
    898 	struct intel_uncore *uncore = rps_to_uncore(rps);
    899 
    900 	/* Program defaults and thresholds for RPS */
    901 	if (IS_GEN(i915, 9))
    902 		intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
    903 				      GEN9_FREQUENCY(rps->rp1_freq));
    904 
    905 	/* 1 second timeout */
    906 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT,
    907 			      GT_INTERVAL_FROM_US(i915, 1000000));
    908 
    909 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 0xa);
    910 
    911 	return rps_reset(rps);
    912 }
    913 
    914 static bool gen8_rps_enable(struct intel_rps *rps)
    915 {
    916 	struct intel_uncore *uncore = rps_to_uncore(rps);
    917 
    918 	intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
    919 			      HSW_FREQUENCY(rps->rp1_freq));
    920 
    921 	/* NB: Docs say 1s, and 1000000 - which aren't equivalent */
    922 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT,
    923 			      100000000 / 128); /* 1 second timeout */
    924 
    925 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
    926 
    927 	return rps_reset(rps);
    928 }
    929 
    930 static bool gen6_rps_enable(struct intel_rps *rps)
    931 {
    932 	struct intel_uncore *uncore = rps_to_uncore(rps);
    933 
    934 	/* Power down if completely idle for over 50ms */
    935 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 50000);
    936 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
    937 
    938 	return rps_reset(rps);
    939 }
    940 
    941 static int chv_rps_max_freq(struct intel_rps *rps)
    942 {
    943 	struct drm_i915_private *i915 = rps_to_i915(rps);
    944 	u32 val;
    945 
    946 	val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
    947 
    948 	switch (RUNTIME_INFO(i915)->sseu.eu_total) {
    949 	case 8:
    950 		/* (2 * 4) config */
    951 		val >>= FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT;
    952 		break;
    953 	case 12:
    954 		/* (2 * 6) config */
    955 		val >>= FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT;
    956 		break;
    957 	case 16:
    958 		/* (2 * 8) config */
    959 	default:
    960 		/* Setting (2 * 8) Min RP0 for any other combination */
    961 		val >>= FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT;
    962 		break;
    963 	}
    964 
    965 	return val & FB_GFX_FREQ_FUSE_MASK;
    966 }
    967 
    968 static int chv_rps_rpe_freq(struct intel_rps *rps)
    969 {
    970 	struct drm_i915_private *i915 = rps_to_i915(rps);
    971 	u32 val;
    972 
    973 	val = vlv_punit_read(i915, PUNIT_GPU_DUTYCYCLE_REG);
    974 	val >>= PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT;
    975 
    976 	return val & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
    977 }
    978 
    979 static int chv_rps_guar_freq(struct intel_rps *rps)
    980 {
    981 	struct drm_i915_private *i915 = rps_to_i915(rps);
    982 	u32 val;
    983 
    984 	val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
    985 
    986 	return val & FB_GFX_FREQ_FUSE_MASK;
    987 }
    988 
    989 static u32 chv_rps_min_freq(struct intel_rps *rps)
    990 {
    991 	struct drm_i915_private *i915 = rps_to_i915(rps);
    992 	u32 val;
    993 
    994 	val = vlv_punit_read(i915, FB_GFX_FMIN_AT_VMIN_FUSE);
    995 	val >>= FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT;
    996 
    997 	return val & FB_GFX_FREQ_FUSE_MASK;
    998 }
    999 
   1000 static bool chv_rps_enable(struct intel_rps *rps)
   1001 {
   1002 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1003 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1004 	u32 val;
   1005 
   1006 	/* 1: Program defaults and thresholds for RPS*/
   1007 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
   1008 	intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
   1009 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
   1010 	intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
   1011 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
   1012 
   1013 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
   1014 
   1015 	/* 2: Enable RPS */
   1016 	intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
   1017 			      GEN6_RP_MEDIA_HW_NORMAL_MODE |
   1018 			      GEN6_RP_MEDIA_IS_GFX |
   1019 			      GEN6_RP_ENABLE |
   1020 			      GEN6_RP_UP_BUSY_AVG |
   1021 			      GEN6_RP_DOWN_IDLE_AVG);
   1022 
   1023 	/* Setting Fixed Bias */
   1024 	vlv_punit_get(i915);
   1025 
   1026 	val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
   1027 	vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
   1028 
   1029 	val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
   1030 
   1031 	vlv_punit_put(i915);
   1032 
   1033 	/* RPS code assumes GPLL is used */
   1034 	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
   1035 
   1036 	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
   1037 	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
   1038 
   1039 	return rps_reset(rps);
   1040 }
   1041 
   1042 static int vlv_rps_guar_freq(struct intel_rps *rps)
   1043 {
   1044 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1045 	u32 val, rp1;
   1046 
   1047 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
   1048 
   1049 	rp1 = val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK;
   1050 	rp1 >>= FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
   1051 
   1052 	return rp1;
   1053 }
   1054 
   1055 static int vlv_rps_max_freq(struct intel_rps *rps)
   1056 {
   1057 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1058 	u32 val, rp0;
   1059 
   1060 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
   1061 
   1062 	rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
   1063 	/* Clamp to max */
   1064 	rp0 = min_t(u32, rp0, 0xea);
   1065 
   1066 	return rp0;
   1067 }
   1068 
   1069 static int vlv_rps_rpe_freq(struct intel_rps *rps)
   1070 {
   1071 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1072 	u32 val, rpe;
   1073 
   1074 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
   1075 	rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
   1076 	val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
   1077 	rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
   1078 
   1079 	return rpe;
   1080 }
   1081 
   1082 static int vlv_rps_min_freq(struct intel_rps *rps)
   1083 {
   1084 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1085 	u32 val;
   1086 
   1087 	val = vlv_punit_read(i915, PUNIT_REG_GPU_LFM) & 0xff;
   1088 	/*
   1089 	 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
   1090 	 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
   1091 	 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
   1092 	 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
   1093 	 * to make sure it matches what Punit accepts.
   1094 	 */
   1095 	return max_t(u32, val, 0xc0);
   1096 }
   1097 
   1098 static bool vlv_rps_enable(struct intel_rps *rps)
   1099 {
   1100 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1101 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1102 	u32 val;
   1103 
   1104 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
   1105 	intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
   1106 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
   1107 	intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
   1108 	intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
   1109 
   1110 	intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
   1111 
   1112 	intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
   1113 			      GEN6_RP_MEDIA_TURBO |
   1114 			      GEN6_RP_MEDIA_HW_NORMAL_MODE |
   1115 			      GEN6_RP_MEDIA_IS_GFX |
   1116 			      GEN6_RP_ENABLE |
   1117 			      GEN6_RP_UP_BUSY_AVG |
   1118 			      GEN6_RP_DOWN_IDLE_CONT);
   1119 
   1120 	vlv_punit_get(i915);
   1121 
   1122 	/* Setting Fixed Bias */
   1123 	val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
   1124 	vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
   1125 
   1126 	val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
   1127 
   1128 	vlv_punit_put(i915);
   1129 
   1130 	/* RPS code assumes GPLL is used */
   1131 	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
   1132 
   1133 	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
   1134 	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
   1135 
   1136 	return rps_reset(rps);
   1137 }
   1138 
   1139 static unsigned long __ips_gfx_val(struct intel_ips *ips)
   1140 {
   1141 	struct intel_rps *rps = container_of(ips, typeof(*rps), ips);
   1142 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1143 	unsigned long t, corr, state1, corr2, state2;
   1144 	u32 pxvid, ext_v;
   1145 
   1146 	lockdep_assert_held(&mchdev_lock);
   1147 
   1148 	pxvid = intel_uncore_read(uncore, PXVFREQ(rps->cur_freq));
   1149 	pxvid = (pxvid >> 24) & 0x7f;
   1150 	ext_v = pvid_to_extvid(rps_to_i915(rps), pxvid);
   1151 
   1152 	state1 = ext_v;
   1153 
   1154 	/* Revel in the empirically derived constants */
   1155 
   1156 	/* Correction factor in 1/100000 units */
   1157 	t = ips_mch_val(uncore);
   1158 	if (t > 80)
   1159 		corr = t * 2349 + 135940;
   1160 	else if (t >= 50)
   1161 		corr = t * 964 + 29317;
   1162 	else /* < 50 */
   1163 		corr = t * 301 + 1004;
   1164 
   1165 	corr = corr * 150142 * state1 / 10000 - 78642;
   1166 	corr /= 100000;
   1167 	corr2 = corr * ips->corr;
   1168 
   1169 	state2 = corr2 * state1 / 10000;
   1170 	state2 /= 100; /* convert to mW */
   1171 
   1172 	__gen5_ips_update(ips);
   1173 
   1174 	return ips->gfx_power + state2;
   1175 }
   1176 
   1177 void intel_rps_enable(struct intel_rps *rps)
   1178 {
   1179 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1180 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1181 
   1182 	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
   1183 	if (IS_CHERRYVIEW(i915))
   1184 		rps->enabled = chv_rps_enable(rps);
   1185 	else if (IS_VALLEYVIEW(i915))
   1186 		rps->enabled = vlv_rps_enable(rps);
   1187 	else if (INTEL_GEN(i915) >= 9)
   1188 		rps->enabled = gen9_rps_enable(rps);
   1189 	else if (INTEL_GEN(i915) >= 8)
   1190 		rps->enabled = gen8_rps_enable(rps);
   1191 	else if (INTEL_GEN(i915) >= 6)
   1192 		rps->enabled = gen6_rps_enable(rps);
   1193 	else if (IS_IRONLAKE_M(i915))
   1194 		rps->enabled = gen5_rps_enable(rps);
   1195 	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
   1196 	if (!rps->enabled)
   1197 		return;
   1198 
   1199 	WARN_ON(rps->max_freq < rps->min_freq);
   1200 	WARN_ON(rps->idle_freq > rps->max_freq);
   1201 
   1202 	WARN_ON(rps->efficient_freq < rps->min_freq);
   1203 	WARN_ON(rps->efficient_freq > rps->max_freq);
   1204 }
   1205 
   1206 static void gen6_rps_disable(struct intel_rps *rps)
   1207 {
   1208 	set(rps_to_uncore(rps), GEN6_RP_CONTROL, 0);
   1209 }
   1210 
   1211 void intel_rps_disable(struct intel_rps *rps)
   1212 {
   1213 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1214 
   1215 	rps->enabled = false;
   1216 
   1217 	if (INTEL_GEN(i915) >= 6)
   1218 		gen6_rps_disable(rps);
   1219 	else if (IS_IRONLAKE_M(i915))
   1220 		gen5_rps_disable(rps);
   1221 }
   1222 
   1223 static int byt_gpu_freq(struct intel_rps *rps, int val)
   1224 {
   1225 	/*
   1226 	 * N = val - 0xb7
   1227 	 * Slow = Fast = GPLL ref * N
   1228 	 */
   1229 	return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
   1230 }
   1231 
   1232 static int byt_freq_opcode(struct intel_rps *rps, int val)
   1233 {
   1234 	return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
   1235 }
   1236 
   1237 static int chv_gpu_freq(struct intel_rps *rps, int val)
   1238 {
   1239 	/*
   1240 	 * N = val / 2
   1241 	 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
   1242 	 */
   1243 	return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
   1244 }
   1245 
   1246 static int chv_freq_opcode(struct intel_rps *rps, int val)
   1247 {
   1248 	/* CHV needs even values */
   1249 	return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
   1250 }
   1251 
   1252 int intel_gpu_freq(struct intel_rps *rps, int val)
   1253 {
   1254 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1255 
   1256 	if (INTEL_GEN(i915) >= 9)
   1257 		return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
   1258 					 GEN9_FREQ_SCALER);
   1259 	else if (IS_CHERRYVIEW(i915))
   1260 		return chv_gpu_freq(rps, val);
   1261 	else if (IS_VALLEYVIEW(i915))
   1262 		return byt_gpu_freq(rps, val);
   1263 	else
   1264 		return val * GT_FREQUENCY_MULTIPLIER;
   1265 }
   1266 
   1267 int intel_freq_opcode(struct intel_rps *rps, int val)
   1268 {
   1269 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1270 
   1271 	if (INTEL_GEN(i915) >= 9)
   1272 		return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
   1273 					 GT_FREQUENCY_MULTIPLIER);
   1274 	else if (IS_CHERRYVIEW(i915))
   1275 		return chv_freq_opcode(rps, val);
   1276 	else if (IS_VALLEYVIEW(i915))
   1277 		return byt_freq_opcode(rps, val);
   1278 	else
   1279 		return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
   1280 }
   1281 
   1282 static void vlv_init_gpll_ref_freq(struct intel_rps *rps)
   1283 {
   1284 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1285 
   1286 	rps->gpll_ref_freq =
   1287 		vlv_get_cck_clock(i915, "GPLL ref",
   1288 				  CCK_GPLL_CLOCK_CONTROL,
   1289 				  i915->czclk_freq);
   1290 
   1291 	DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n", rps->gpll_ref_freq);
   1292 }
   1293 
   1294 static void vlv_rps_init(struct intel_rps *rps)
   1295 {
   1296 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1297 	u32 val;
   1298 
   1299 	vlv_iosf_sb_get(i915,
   1300 			BIT(VLV_IOSF_SB_PUNIT) |
   1301 			BIT(VLV_IOSF_SB_NC) |
   1302 			BIT(VLV_IOSF_SB_CCK));
   1303 
   1304 	vlv_init_gpll_ref_freq(rps);
   1305 
   1306 	val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
   1307 	switch ((val >> 6) & 3) {
   1308 	case 0:
   1309 	case 1:
   1310 		i915->mem_freq = 800;
   1311 		break;
   1312 	case 2:
   1313 		i915->mem_freq = 1066;
   1314 		break;
   1315 	case 3:
   1316 		i915->mem_freq = 1333;
   1317 		break;
   1318 	}
   1319 	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", i915->mem_freq);
   1320 
   1321 	rps->max_freq = vlv_rps_max_freq(rps);
   1322 	rps->rp0_freq = rps->max_freq;
   1323 	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
   1324 			 intel_gpu_freq(rps, rps->max_freq),
   1325 			 rps->max_freq);
   1326 
   1327 	rps->efficient_freq = vlv_rps_rpe_freq(rps);
   1328 	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
   1329 			 intel_gpu_freq(rps, rps->efficient_freq),
   1330 			 rps->efficient_freq);
   1331 
   1332 	rps->rp1_freq = vlv_rps_guar_freq(rps);
   1333 	DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
   1334 			 intel_gpu_freq(rps, rps->rp1_freq),
   1335 			 rps->rp1_freq);
   1336 
   1337 	rps->min_freq = vlv_rps_min_freq(rps);
   1338 	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
   1339 			 intel_gpu_freq(rps, rps->min_freq),
   1340 			 rps->min_freq);
   1341 
   1342 	vlv_iosf_sb_put(i915,
   1343 			BIT(VLV_IOSF_SB_PUNIT) |
   1344 			BIT(VLV_IOSF_SB_NC) |
   1345 			BIT(VLV_IOSF_SB_CCK));
   1346 }
   1347 
   1348 static void chv_rps_init(struct intel_rps *rps)
   1349 {
   1350 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1351 	u32 val;
   1352 
   1353 	vlv_iosf_sb_get(i915,
   1354 			BIT(VLV_IOSF_SB_PUNIT) |
   1355 			BIT(VLV_IOSF_SB_NC) |
   1356 			BIT(VLV_IOSF_SB_CCK));
   1357 
   1358 	vlv_init_gpll_ref_freq(rps);
   1359 
   1360 	val = vlv_cck_read(i915, CCK_FUSE_REG);
   1361 
   1362 	switch ((val >> 2) & 0x7) {
   1363 	case 3:
   1364 		i915->mem_freq = 2000;
   1365 		break;
   1366 	default:
   1367 		i915->mem_freq = 1600;
   1368 		break;
   1369 	}
   1370 	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", i915->mem_freq);
   1371 
   1372 	rps->max_freq = chv_rps_max_freq(rps);
   1373 	rps->rp0_freq = rps->max_freq;
   1374 	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
   1375 			 intel_gpu_freq(rps, rps->max_freq),
   1376 			 rps->max_freq);
   1377 
   1378 	rps->efficient_freq = chv_rps_rpe_freq(rps);
   1379 	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
   1380 			 intel_gpu_freq(rps, rps->efficient_freq),
   1381 			 rps->efficient_freq);
   1382 
   1383 	rps->rp1_freq = chv_rps_guar_freq(rps);
   1384 	DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
   1385 			 intel_gpu_freq(rps, rps->rp1_freq),
   1386 			 rps->rp1_freq);
   1387 
   1388 	rps->min_freq = chv_rps_min_freq(rps);
   1389 	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
   1390 			 intel_gpu_freq(rps, rps->min_freq),
   1391 			 rps->min_freq);
   1392 
   1393 	vlv_iosf_sb_put(i915,
   1394 			BIT(VLV_IOSF_SB_PUNIT) |
   1395 			BIT(VLV_IOSF_SB_NC) |
   1396 			BIT(VLV_IOSF_SB_CCK));
   1397 
   1398 	WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
   1399 		   rps->min_freq) & 1,
   1400 		  "Odd GPU freq values\n");
   1401 }
   1402 
   1403 static void vlv_c0_read(struct intel_uncore *uncore, struct intel_rps_ei *ei)
   1404 {
   1405 	ei->ktime = ktime_get_raw();
   1406 	ei->render_c0 = intel_uncore_read(uncore, VLV_RENDER_C0_COUNT);
   1407 	ei->media_c0 = intel_uncore_read(uncore, VLV_MEDIA_C0_COUNT);
   1408 }
   1409 
   1410 static u32 vlv_wa_c0_ei(struct intel_rps *rps, u32 pm_iir)
   1411 {
   1412 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1413 	const struct intel_rps_ei *prev = &rps->ei;
   1414 	struct intel_rps_ei now;
   1415 	u32 events = 0;
   1416 
   1417 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
   1418 		return 0;
   1419 
   1420 	vlv_c0_read(uncore, &now);
   1421 
   1422 	if (prev->ktime) {
   1423 		u64 time, c0;
   1424 		u32 render, media;
   1425 
   1426 		time = ktime_us_delta(now.ktime, prev->ktime);
   1427 
   1428 		time *= rps_to_i915(rps)->czclk_freq;
   1429 
   1430 		/* Workload can be split between render + media,
   1431 		 * e.g. SwapBuffers being blitted in X after being rendered in
   1432 		 * mesa. To account for this we need to combine both engines
   1433 		 * into our activity counter.
   1434 		 */
   1435 		render = now.render_c0 - prev->render_c0;
   1436 		media = now.media_c0 - prev->media_c0;
   1437 		c0 = max(render, media);
   1438 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
   1439 
   1440 		if (c0 > time * rps->power.up_threshold)
   1441 			events = GEN6_PM_RP_UP_THRESHOLD;
   1442 		else if (c0 < time * rps->power.down_threshold)
   1443 			events = GEN6_PM_RP_DOWN_THRESHOLD;
   1444 	}
   1445 
   1446 	rps->ei = now;
   1447 	return events;
   1448 }
   1449 
   1450 static void rps_work(struct work_struct *work)
   1451 {
   1452 	struct intel_rps *rps = container_of(work, typeof(*rps), work);
   1453 	struct intel_gt *gt = rps_to_gt(rps);
   1454 	bool client_boost = false;
   1455 	int new_freq, adj, min, max;
   1456 	u32 pm_iir = 0;
   1457 
   1458 	spin_lock_irq(&gt->irq_lock);
   1459 	pm_iir = fetch_and_zero(&rps->pm_iir);
   1460 	client_boost = atomic_read(&rps->num_waiters);
   1461 	spin_unlock_irq(&gt->irq_lock);
   1462 
   1463 	/* Make sure we didn't queue anything we're not going to process. */
   1464 	if ((pm_iir & rps->pm_events) == 0 && !client_boost)
   1465 		goto out;
   1466 
   1467 	mutex_lock(&rps->lock);
   1468 
   1469 	pm_iir |= vlv_wa_c0_ei(rps, pm_iir);
   1470 
   1471 	adj = rps->last_adj;
   1472 	new_freq = rps->cur_freq;
   1473 	min = rps->min_freq_softlimit;
   1474 	max = rps->max_freq_softlimit;
   1475 	if (client_boost)
   1476 		max = rps->max_freq;
   1477 	if (client_boost && new_freq < rps->boost_freq) {
   1478 		new_freq = rps->boost_freq;
   1479 		adj = 0;
   1480 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
   1481 		if (adj > 0)
   1482 			adj *= 2;
   1483 		else /* CHV needs even encode values */
   1484 			adj = IS_CHERRYVIEW(gt->i915) ? 2 : 1;
   1485 
   1486 		if (new_freq >= rps->max_freq_softlimit)
   1487 			adj = 0;
   1488 	} else if (client_boost) {
   1489 		adj = 0;
   1490 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
   1491 		if (rps->cur_freq > rps->efficient_freq)
   1492 			new_freq = rps->efficient_freq;
   1493 		else if (rps->cur_freq > rps->min_freq_softlimit)
   1494 			new_freq = rps->min_freq_softlimit;
   1495 		adj = 0;
   1496 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
   1497 		if (adj < 0)
   1498 			adj *= 2;
   1499 		else /* CHV needs even encode values */
   1500 			adj = IS_CHERRYVIEW(gt->i915) ? -2 : -1;
   1501 
   1502 		if (new_freq <= rps->min_freq_softlimit)
   1503 			adj = 0;
   1504 	} else { /* unknown event */
   1505 		adj = 0;
   1506 	}
   1507 
   1508 	rps->last_adj = adj;
   1509 
   1510 	/*
   1511 	 * Limit deboosting and boosting to keep ourselves at the extremes
   1512 	 * when in the respective power modes (i.e. slowly decrease frequencies
   1513 	 * while in the HIGH_POWER zone and slowly increase frequencies while
   1514 	 * in the LOW_POWER zone). On idle, we will hit the timeout and drop
   1515 	 * to the next level quickly, and conversely if busy we expect to
   1516 	 * hit a waitboost and rapidly switch into max power.
   1517 	 */
   1518 	if ((adj < 0 && rps->power.mode == HIGH_POWER) ||
   1519 	    (adj > 0 && rps->power.mode == LOW_POWER))
   1520 		rps->last_adj = 0;
   1521 
   1522 	/* sysfs frequency interfaces may have snuck in while servicing the
   1523 	 * interrupt
   1524 	 */
   1525 	new_freq += adj;
   1526 	new_freq = clamp_t(int, new_freq, min, max);
   1527 
   1528 	if (intel_rps_set(rps, new_freq)) {
   1529 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
   1530 		rps->last_adj = 0;
   1531 	}
   1532 
   1533 	mutex_unlock(&rps->lock);
   1534 
   1535 out:
   1536 	spin_lock_irq(&gt->irq_lock);
   1537 	gen6_gt_pm_unmask_irq(gt, rps->pm_events);
   1538 	spin_unlock_irq(&gt->irq_lock);
   1539 }
   1540 
   1541 void gen11_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
   1542 {
   1543 	struct intel_gt *gt = rps_to_gt(rps);
   1544 	const u32 events = rps->pm_events & pm_iir;
   1545 
   1546 	lockdep_assert_held(&gt->irq_lock);
   1547 
   1548 	if (unlikely(!events))
   1549 		return;
   1550 
   1551 	gen6_gt_pm_mask_irq(gt, events);
   1552 
   1553 	rps->pm_iir |= events;
   1554 	schedule_work(&rps->work);
   1555 }
   1556 
   1557 void gen6_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
   1558 {
   1559 	struct intel_gt *gt = rps_to_gt(rps);
   1560 
   1561 	if (pm_iir & rps->pm_events) {
   1562 		spin_lock(&gt->irq_lock);
   1563 		gen6_gt_pm_mask_irq(gt, pm_iir & rps->pm_events);
   1564 		rps->pm_iir |= pm_iir & rps->pm_events;
   1565 		schedule_work(&rps->work);
   1566 		spin_unlock(&gt->irq_lock);
   1567 	}
   1568 
   1569 	if (INTEL_GEN(gt->i915) >= 8)
   1570 		return;
   1571 
   1572 	if (pm_iir & PM_VEBOX_USER_INTERRUPT)
   1573 		intel_engine_signal_breadcrumbs(gt->engine[VECS0]);
   1574 
   1575 	if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
   1576 		DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
   1577 }
   1578 
   1579 void gen5_rps_irq_handler(struct intel_rps *rps)
   1580 {
   1581 	struct intel_uncore *uncore = rps_to_uncore(rps);
   1582 	u32 busy_up, busy_down, max_avg, min_avg;
   1583 	u8 new_freq;
   1584 
   1585 	spin_lock(&mchdev_lock);
   1586 
   1587 	intel_uncore_write16(uncore,
   1588 			     MEMINTRSTS,
   1589 			     intel_uncore_read(uncore, MEMINTRSTS));
   1590 
   1591 	intel_uncore_write16(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
   1592 	busy_up = intel_uncore_read(uncore, RCPREVBSYTUPAVG);
   1593 	busy_down = intel_uncore_read(uncore, RCPREVBSYTDNAVG);
   1594 	max_avg = intel_uncore_read(uncore, RCBMAXAVG);
   1595 	min_avg = intel_uncore_read(uncore, RCBMINAVG);
   1596 
   1597 	/* Handle RCS change request from hw */
   1598 	new_freq = rps->cur_freq;
   1599 	if (busy_up > max_avg)
   1600 		new_freq++;
   1601 	else if (busy_down < min_avg)
   1602 		new_freq--;
   1603 	new_freq = clamp(new_freq,
   1604 			 rps->min_freq_softlimit,
   1605 			 rps->max_freq_softlimit);
   1606 
   1607 	if (new_freq != rps->cur_freq && gen5_rps_set(rps, new_freq))
   1608 		rps->cur_freq = new_freq;
   1609 
   1610 	spin_unlock(&mchdev_lock);
   1611 }
   1612 
   1613 void intel_rps_init_early(struct intel_rps *rps)
   1614 {
   1615 	mutex_init(&rps->lock);
   1616 	mutex_init(&rps->power.mutex);
   1617 
   1618 	INIT_WORK(&rps->work, rps_work);
   1619 
   1620 	atomic_set(&rps->num_waiters, 0);
   1621 }
   1622 
   1623 void intel_rps_init(struct intel_rps *rps)
   1624 {
   1625 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1626 
   1627 	if (IS_CHERRYVIEW(i915))
   1628 		chv_rps_init(rps);
   1629 	else if (IS_VALLEYVIEW(i915))
   1630 		vlv_rps_init(rps);
   1631 	else if (INTEL_GEN(i915) >= 6)
   1632 		gen6_rps_init(rps);
   1633 	else if (IS_IRONLAKE_M(i915))
   1634 		gen5_rps_init(rps);
   1635 
   1636 	/* Derive initial user preferences/limits from the hardware limits */
   1637 	rps->max_freq_softlimit = rps->max_freq;
   1638 	rps->min_freq_softlimit = rps->min_freq;
   1639 
   1640 	/* After setting max-softlimit, find the overclock max freq */
   1641 	if (IS_GEN(i915, 6) || IS_IVYBRIDGE(i915) || IS_HASWELL(i915)) {
   1642 		u32 params = 0;
   1643 
   1644 		sandybridge_pcode_read(i915, GEN6_READ_OC_PARAMS,
   1645 				       &params, NULL);
   1646 		if (params & BIT(31)) { /* OC supported */
   1647 			DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
   1648 					 (rps->max_freq & 0xff) * 50,
   1649 					 (params & 0xff) * 50);
   1650 			rps->max_freq = params & 0xff;
   1651 		}
   1652 	}
   1653 
   1654 	/* Finally allow us to boost to max by default */
   1655 	rps->boost_freq = rps->max_freq;
   1656 	rps->idle_freq = rps->min_freq;
   1657 	rps->cur_freq = rps->idle_freq;
   1658 
   1659 	rps->pm_intrmsk_mbz = 0;
   1660 
   1661 	/*
   1662 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
   1663 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
   1664 	 *
   1665 	 * TODO: verify if this can be reproduced on VLV,CHV.
   1666 	 */
   1667 	if (INTEL_GEN(i915) <= 7)
   1668 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
   1669 
   1670 	if (INTEL_GEN(i915) >= 8 && INTEL_GEN(i915) < 11)
   1671 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
   1672 }
   1673 
   1674 u32 intel_rps_get_cagf(struct intel_rps *rps, u32 rpstat)
   1675 {
   1676 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1677 	u32 cagf;
   1678 
   1679 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
   1680 		cagf = (rpstat >> 8) & 0xff;
   1681 	else if (INTEL_GEN(i915) >= 9)
   1682 		cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
   1683 	else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
   1684 		cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
   1685 	else
   1686 		cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
   1687 
   1688 	return cagf;
   1689 }
   1690 
   1691 static u32 read_cagf(struct intel_rps *rps)
   1692 {
   1693 	struct drm_i915_private *i915 = rps_to_i915(rps);
   1694 	u32 freq;
   1695 
   1696 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
   1697 		vlv_punit_get(i915);
   1698 		freq = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
   1699 		vlv_punit_put(i915);
   1700 	} else {
   1701 		freq = intel_uncore_read(rps_to_gt(rps)->uncore, GEN6_RPSTAT1);
   1702 	}
   1703 
   1704 	return intel_rps_get_cagf(rps, freq);
   1705 }
   1706 
   1707 u32 intel_rps_read_actual_frequency(struct intel_rps *rps)
   1708 {
   1709 	struct intel_runtime_pm *rpm = rps_to_gt(rps)->uncore->rpm;
   1710 	intel_wakeref_t wakeref;
   1711 	u32 freq = 0;
   1712 
   1713 	with_intel_runtime_pm_if_in_use(rpm, wakeref)
   1714 		freq = intel_gpu_freq(rps, read_cagf(rps));
   1715 
   1716 	return freq;
   1717 }
   1718 
   1719 /* External interface for intel_ips.ko */
   1720 
   1721 static struct drm_i915_private __rcu *ips_mchdev;
   1722 
   1723 /**
   1724  * Tells the intel_ips driver that the i915 driver is now loaded, if
   1725  * IPS got loaded first.
   1726  *
   1727  * This awkward dance is so that neither module has to depend on the
   1728  * other in order for IPS to do the appropriate communication of
   1729  * GPU turbo limits to i915.
   1730  */
   1731 static void
   1732 ips_ping_for_i915_load(void)
   1733 {
   1734 	void (*link)(void);
   1735 
   1736 	link = symbol_get(ips_link_to_i915_driver);
   1737 	if (link) {
   1738 		link();
   1739 		symbol_put(ips_link_to_i915_driver);
   1740 	}
   1741 }
   1742 
   1743 void intel_rps_driver_register(struct intel_rps *rps)
   1744 {
   1745 	struct intel_gt *gt = rps_to_gt(rps);
   1746 
   1747 	/*
   1748 	 * We only register the i915 ips part with intel-ips once everything is
   1749 	 * set up, to avoid intel-ips sneaking in and reading bogus values.
   1750 	 */
   1751 	if (IS_GEN(gt->i915, 5)) {
   1752 		GEM_BUG_ON(ips_mchdev);
   1753 		rcu_assign_pointer(ips_mchdev, gt->i915);
   1754 		ips_ping_for_i915_load();
   1755 	}
   1756 }
   1757 
   1758 void intel_rps_driver_unregister(struct intel_rps *rps)
   1759 {
   1760 	if (rcu_access_pointer(ips_mchdev) == rps_to_i915(rps))
   1761 		rcu_assign_pointer(ips_mchdev, NULL);
   1762 }
   1763 
   1764 static struct drm_i915_private *mchdev_get(void)
   1765 {
   1766 	struct drm_i915_private *i915;
   1767 
   1768 	rcu_read_lock();
   1769 	i915 = rcu_dereference(ips_mchdev);
   1770 	if (!kref_get_unless_zero(&i915->drm.ref))
   1771 		i915 = NULL;
   1772 	rcu_read_unlock();
   1773 
   1774 	return i915;
   1775 }
   1776 
   1777 /**
   1778  * i915_read_mch_val - return value for IPS use
   1779  *
   1780  * Calculate and return a value for the IPS driver to use when deciding whether
   1781  * we have thermal and power headroom to increase CPU or GPU power budget.
   1782  */
   1783 unsigned long i915_read_mch_val(void)
   1784 {
   1785 	struct drm_i915_private *i915;
   1786 	unsigned long chipset_val = 0;
   1787 	unsigned long graphics_val = 0;
   1788 	intel_wakeref_t wakeref;
   1789 
   1790 	i915 = mchdev_get();
   1791 	if (!i915)
   1792 		return 0;
   1793 
   1794 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
   1795 		struct intel_ips *ips = &i915->gt.rps.ips;
   1796 
   1797 		spin_lock_irq(&mchdev_lock);
   1798 		chipset_val = __ips_chipset_val(ips);
   1799 		graphics_val = __ips_gfx_val(ips);
   1800 		spin_unlock_irq(&mchdev_lock);
   1801 	}
   1802 
   1803 	drm_dev_put(&i915->drm);
   1804 	return chipset_val + graphics_val;
   1805 }
   1806 EXPORT_SYMBOL_GPL(i915_read_mch_val);
   1807 
   1808 /**
   1809  * i915_gpu_raise - raise GPU frequency limit
   1810  *
   1811  * Raise the limit; IPS indicates we have thermal headroom.
   1812  */
   1813 bool i915_gpu_raise(void)
   1814 {
   1815 	struct drm_i915_private *i915;
   1816 	struct intel_rps *rps;
   1817 
   1818 	i915 = mchdev_get();
   1819 	if (!i915)
   1820 		return false;
   1821 
   1822 	rps = &i915->gt.rps;
   1823 
   1824 	spin_lock_irq(&mchdev_lock);
   1825 	if (rps->max_freq_softlimit < rps->max_freq)
   1826 		rps->max_freq_softlimit++;
   1827 	spin_unlock_irq(&mchdev_lock);
   1828 
   1829 	drm_dev_put(&i915->drm);
   1830 	return true;
   1831 }
   1832 EXPORT_SYMBOL_GPL(i915_gpu_raise);
   1833 
   1834 /**
   1835  * i915_gpu_lower - lower GPU frequency limit
   1836  *
   1837  * IPS indicates we're close to a thermal limit, so throttle back the GPU
   1838  * frequency maximum.
   1839  */
   1840 bool i915_gpu_lower(void)
   1841 {
   1842 	struct drm_i915_private *i915;
   1843 	struct intel_rps *rps;
   1844 
   1845 	i915 = mchdev_get();
   1846 	if (!i915)
   1847 		return false;
   1848 
   1849 	rps = &i915->gt.rps;
   1850 
   1851 	spin_lock_irq(&mchdev_lock);
   1852 	if (rps->max_freq_softlimit > rps->min_freq)
   1853 		rps->max_freq_softlimit--;
   1854 	spin_unlock_irq(&mchdev_lock);
   1855 
   1856 	drm_dev_put(&i915->drm);
   1857 	return true;
   1858 }
   1859 EXPORT_SYMBOL_GPL(i915_gpu_lower);
   1860 
   1861 /**
   1862  * i915_gpu_busy - indicate GPU business to IPS
   1863  *
   1864  * Tell the IPS driver whether or not the GPU is busy.
   1865  */
   1866 bool i915_gpu_busy(void)
   1867 {
   1868 	struct drm_i915_private *i915;
   1869 	bool ret;
   1870 
   1871 	i915 = mchdev_get();
   1872 	if (!i915)
   1873 		return false;
   1874 
   1875 	ret = i915->gt.awake;
   1876 
   1877 	drm_dev_put(&i915->drm);
   1878 	return ret;
   1879 }
   1880 EXPORT_SYMBOL_GPL(i915_gpu_busy);
   1881 
   1882 /**
   1883  * i915_gpu_turbo_disable - disable graphics turbo
   1884  *
   1885  * Disable graphics turbo by resetting the max frequency and setting the
   1886  * current frequency to the default.
   1887  */
   1888 bool i915_gpu_turbo_disable(void)
   1889 {
   1890 	struct drm_i915_private *i915;
   1891 	struct intel_rps *rps;
   1892 	bool ret;
   1893 
   1894 	i915 = mchdev_get();
   1895 	if (!i915)
   1896 		return false;
   1897 
   1898 	rps = &i915->gt.rps;
   1899 
   1900 	spin_lock_irq(&mchdev_lock);
   1901 	rps->max_freq_softlimit = rps->min_freq;
   1902 	ret = gen5_rps_set(&i915->gt.rps, rps->min_freq);
   1903 	spin_unlock_irq(&mchdev_lock);
   1904 
   1905 	drm_dev_put(&i915->drm);
   1906 	return ret;
   1907 }
   1908 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
   1909