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