2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
9 #include "intel_gt_irq.h"
10 #include "intel_gt_pm_irq.h"
11 #include "intel_rps.h"
12 #include "intel_sideband.h"
13 #include "../../../platform/x86/intel_ips.h"
16 * Lock protecting IPS related data structures
18 static DEFINE_SPINLOCK(mchdev_lock);
20 static struct intel_gt *rps_to_gt(struct intel_rps *rps)
22 return container_of(rps, struct intel_gt, rps);
25 static struct drm_i915_private *rps_to_i915(struct intel_rps *rps)
27 return rps_to_gt(rps)->i915;
30 static struct intel_uncore *rps_to_uncore(struct intel_rps *rps)
32 return rps_to_gt(rps)->uncore;
35 static u32 rps_pm_sanitize_mask(struct intel_rps *rps, u32 mask)
37 return mask & ~rps->pm_intrmsk_mbz;
40 static inline void set(struct intel_uncore *uncore, i915_reg_t reg, u32 val)
42 intel_uncore_write_fw(uncore, reg, val);
45 static u32 rps_pm_mask(struct intel_rps *rps, u8 val)
49 /* We use UP_EI_EXPIRED interrupts for both up/down in manual mode */
50 if (val > rps->min_freq_softlimit)
51 mask |= (GEN6_PM_RP_UP_EI_EXPIRED |
52 GEN6_PM_RP_DOWN_THRESHOLD |
53 GEN6_PM_RP_DOWN_TIMEOUT);
55 if (val < rps->max_freq_softlimit)
56 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
58 mask &= rps->pm_events;
60 return rps_pm_sanitize_mask(rps, ~mask);
63 static void rps_reset_ei(struct intel_rps *rps)
65 memset(&rps->ei, 0, sizeof(rps->ei));
68 static void rps_enable_interrupts(struct intel_rps *rps)
70 struct intel_gt *gt = rps_to_gt(rps);
74 if (IS_VALLEYVIEW(gt->i915))
75 /* WaGsvRC0ResidencyMethod:vlv */
76 rps->pm_events = GEN6_PM_RP_UP_EI_EXPIRED;
78 rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
79 GEN6_PM_RP_DOWN_THRESHOLD |
80 GEN6_PM_RP_DOWN_TIMEOUT);
82 spin_lock_irq(>->irq_lock);
83 gen6_gt_pm_enable_irq(gt, rps->pm_events);
84 spin_unlock_irq(>->irq_lock);
86 set(gt->uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, rps->cur_freq));
89 static void gen6_rps_reset_interrupts(struct intel_rps *rps)
91 gen6_gt_pm_reset_iir(rps_to_gt(rps), GEN6_PM_RPS_EVENTS);
94 static void gen11_rps_reset_interrupts(struct intel_rps *rps)
96 while (gen11_gt_reset_one_iir(rps_to_gt(rps), 0, GEN11_GTPM))
100 static void rps_reset_interrupts(struct intel_rps *rps)
102 struct intel_gt *gt = rps_to_gt(rps);
104 spin_lock_irq(>->irq_lock);
105 if (INTEL_GEN(gt->i915) >= 11)
106 gen11_rps_reset_interrupts(rps);
108 gen6_rps_reset_interrupts(rps);
111 spin_unlock_irq(>->irq_lock);
114 static void rps_disable_interrupts(struct intel_rps *rps)
116 struct intel_gt *gt = rps_to_gt(rps);
120 set(gt->uncore, GEN6_PMINTRMSK, rps_pm_sanitize_mask(rps, ~0u));
122 spin_lock_irq(>->irq_lock);
123 gen6_gt_pm_disable_irq(gt, GEN6_PM_RPS_EVENTS);
124 spin_unlock_irq(>->irq_lock);
126 intel_synchronize_irq(gt->i915);
129 * Now that we will not be generating any more work, flush any
130 * outstanding tasks. As we are called on the RPS idle path,
131 * we will reset the GPU to minimum frequencies, so the current
132 * state of the worker can be discarded.
134 cancel_work_sync(&rps->work);
136 rps_reset_interrupts(rps);
139 static const struct cparams {
145 { 1, 1333, 301, 28664 },
146 { 1, 1066, 294, 24460 },
147 { 1, 800, 294, 25192 },
148 { 0, 1333, 276, 27605 },
149 { 0, 1066, 276, 27605 },
150 { 0, 800, 231, 23784 },
153 static void gen5_rps_init(struct intel_rps *rps)
155 struct drm_i915_private *i915 = rps_to_i915(rps);
156 struct intel_uncore *uncore = rps_to_uncore(rps);
157 u8 fmax, fmin, fstart;
161 if (i915->fsb_freq <= 3200)
163 else if (i915->fsb_freq <= 4800)
168 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
169 if (cparams[i].i == c_m && cparams[i].t == i915->mem_freq) {
170 rps->ips.m = cparams[i].m;
171 rps->ips.c = cparams[i].c;
176 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
178 /* Set up min, max, and cur for interrupt handling */
179 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
180 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
181 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
182 MEMMODE_FSTART_SHIFT;
183 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
186 rps->min_freq = fmax;
187 rps->max_freq = fmin;
189 rps->idle_freq = rps->min_freq;
190 rps->cur_freq = rps->idle_freq;
194 __ips_chipset_val(struct intel_ips *ips)
196 struct intel_uncore *uncore =
197 rps_to_uncore(container_of(ips, struct intel_rps, ips));
198 unsigned long now = jiffies_to_msecs(jiffies), dt;
199 unsigned long result;
202 lockdep_assert_held(&mchdev_lock);
205 * Prevent division-by-zero if we are asking too fast.
206 * Also, we don't get interesting results if we are polling
207 * faster than once in 10ms, so just return the saved value
210 dt = now - ips->last_time1;
212 return ips->chipset_power;
214 /* FIXME: handle per-counter overflow */
215 total = intel_uncore_read(uncore, DMIEC);
216 total += intel_uncore_read(uncore, DDREC);
217 total += intel_uncore_read(uncore, CSIEC);
219 delta = total - ips->last_count1;
221 result = div_u64(div_u64(ips->m * delta, dt) + ips->c, 10);
223 ips->last_count1 = total;
224 ips->last_time1 = now;
226 ips->chipset_power = result;
231 static unsigned long ips_mch_val(struct intel_uncore *uncore)
233 unsigned int m, x, b;
236 tsfs = intel_uncore_read(uncore, TSFS);
237 x = intel_uncore_read8(uncore, TR1);
239 b = tsfs & TSFS_INTR_MASK;
240 m = (tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT;
242 return m * x / 127 - b;
245 static int _pxvid_to_vd(u8 pxvid)
250 if (pxvid >= 8 && pxvid < 31)
253 return (pxvid + 2) * 125;
256 static u32 pvid_to_extvid(struct drm_i915_private *i915, u8 pxvid)
258 const int vd = _pxvid_to_vd(pxvid);
260 if (INTEL_INFO(i915)->is_mobile)
261 return max(vd - 1125, 0);
266 static void __gen5_ips_update(struct intel_ips *ips)
268 struct intel_uncore *uncore =
269 rps_to_uncore(container_of(ips, struct intel_rps, ips));
273 lockdep_assert_held(&mchdev_lock);
275 now = ktime_get_raw_ns();
276 dt = now - ips->last_time2;
277 do_div(dt, NSEC_PER_MSEC);
279 /* Don't divide by 0 */
283 count = intel_uncore_read(uncore, GFXEC);
284 delta = count - ips->last_count2;
286 ips->last_count2 = count;
287 ips->last_time2 = now;
289 /* More magic constants... */
290 ips->gfx_power = div_u64(delta * 1181, dt * 10);
293 static void gen5_rps_update(struct intel_rps *rps)
295 spin_lock_irq(&mchdev_lock);
296 __gen5_ips_update(&rps->ips);
297 spin_unlock_irq(&mchdev_lock);
300 static bool gen5_rps_set(struct intel_rps *rps, u8 val)
302 struct intel_uncore *uncore = rps_to_uncore(rps);
305 lockdep_assert_held(&mchdev_lock);
307 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
308 if (rgvswctl & MEMCTL_CMD_STS) {
309 DRM_DEBUG("gpu busy, RCS change rejected\n");
310 return false; /* still busy with another command */
313 /* Invert the frequency bin into an ips delay */
314 val = rps->max_freq - val;
315 val = rps->min_freq + val;
318 (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
319 (val << MEMCTL_FREQ_SHIFT) |
321 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
322 intel_uncore_posting_read16(uncore, MEMSWCTL);
324 rgvswctl |= MEMCTL_CMD_STS;
325 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
330 static unsigned long intel_pxfreq(u32 vidfreq)
332 int div = (vidfreq & 0x3f0000) >> 16;
333 int post = (vidfreq & 0x3000) >> 12;
334 int pre = (vidfreq & 0x7);
339 return div * 133333 / (pre << post);
342 static unsigned int init_emon(struct intel_uncore *uncore)
347 /* Disable to program */
348 intel_uncore_write(uncore, ECR, 0);
349 intel_uncore_posting_read(uncore, ECR);
351 /* Program energy weights for various events */
352 intel_uncore_write(uncore, SDEW, 0x15040d00);
353 intel_uncore_write(uncore, CSIEW0, 0x007f0000);
354 intel_uncore_write(uncore, CSIEW1, 0x1e220004);
355 intel_uncore_write(uncore, CSIEW2, 0x04000004);
357 for (i = 0; i < 5; i++)
358 intel_uncore_write(uncore, PEW(i), 0);
359 for (i = 0; i < 3; i++)
360 intel_uncore_write(uncore, DEW(i), 0);
362 /* Program P-state weights to account for frequency power adjustment */
363 for (i = 0; i < 16; i++) {
364 u32 pxvidfreq = intel_uncore_read(uncore, PXVFREQ(i));
365 unsigned int freq = intel_pxfreq(pxvidfreq);
367 (pxvidfreq & PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
370 val = vid * vid * freq / 1000 * 255;
371 val /= 127 * 127 * 900;
375 /* Render standby states get 0 weight */
379 for (i = 0; i < 4; i++) {
380 intel_uncore_write(uncore, PXW(i),
381 pxw[i * 4 + 0] << 24 |
382 pxw[i * 4 + 1] << 16 |
383 pxw[i * 4 + 2] << 8 |
384 pxw[i * 4 + 3] << 0);
387 /* Adjust magic regs to magic values (more experimental results) */
388 intel_uncore_write(uncore, OGW0, 0);
389 intel_uncore_write(uncore, OGW1, 0);
390 intel_uncore_write(uncore, EG0, 0x00007f00);
391 intel_uncore_write(uncore, EG1, 0x0000000e);
392 intel_uncore_write(uncore, EG2, 0x000e0000);
393 intel_uncore_write(uncore, EG3, 0x68000300);
394 intel_uncore_write(uncore, EG4, 0x42000000);
395 intel_uncore_write(uncore, EG5, 0x00140031);
396 intel_uncore_write(uncore, EG6, 0);
397 intel_uncore_write(uncore, EG7, 0);
399 for (i = 0; i < 8; i++)
400 intel_uncore_write(uncore, PXWL(i), 0);
402 /* Enable PMON + select events */
403 intel_uncore_write(uncore, ECR, 0x80000019);
405 return intel_uncore_read(uncore, LCFUSE02) & LCFUSE_HIV_MASK;
408 static bool gen5_rps_enable(struct intel_rps *rps)
410 struct intel_uncore *uncore = rps_to_uncore(rps);
414 spin_lock_irq(&mchdev_lock);
416 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
418 /* Enable temp reporting */
419 intel_uncore_write16(uncore, PMMISC,
420 intel_uncore_read16(uncore, PMMISC) | MCPPCE_EN);
421 intel_uncore_write16(uncore, TSC1,
422 intel_uncore_read16(uncore, TSC1) | TSE);
424 /* 100ms RC evaluation intervals */
425 intel_uncore_write(uncore, RCUPEI, 100000);
426 intel_uncore_write(uncore, RCDNEI, 100000);
428 /* Set max/min thresholds to 90ms and 80ms respectively */
429 intel_uncore_write(uncore, RCBMAXAVG, 90000);
430 intel_uncore_write(uncore, RCBMINAVG, 80000);
432 intel_uncore_write(uncore, MEMIHYST, 1);
434 /* Set up min, max, and cur for interrupt handling */
435 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
436 MEMMODE_FSTART_SHIFT;
438 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
439 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
441 intel_uncore_write(uncore,
443 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
445 intel_uncore_write(uncore, VIDSTART, vstart);
446 intel_uncore_posting_read(uncore, VIDSTART);
448 rgvmodectl |= MEMMODE_SWMODE_EN;
449 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
451 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
452 MEMCTL_CMD_STS) == 0, 10))
453 DRM_ERROR("stuck trying to change perf mode\n");
456 gen5_rps_set(rps, rps->cur_freq);
458 rps->ips.last_count1 = intel_uncore_read(uncore, DMIEC);
459 rps->ips.last_count1 += intel_uncore_read(uncore, DDREC);
460 rps->ips.last_count1 += intel_uncore_read(uncore, CSIEC);
461 rps->ips.last_time1 = jiffies_to_msecs(jiffies);
463 rps->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
464 rps->ips.last_time2 = ktime_get_raw_ns();
466 spin_unlock_irq(&mchdev_lock);
468 rps->ips.corr = init_emon(uncore);
473 static void gen5_rps_disable(struct intel_rps *rps)
475 struct intel_uncore *uncore = rps_to_uncore(rps);
478 spin_lock_irq(&mchdev_lock);
480 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
482 /* Ack interrupts, disable EFC interrupt */
483 intel_uncore_write(uncore, MEMINTREN,
484 intel_uncore_read(uncore, MEMINTREN) &
485 ~MEMINT_EVAL_CHG_EN);
486 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
487 intel_uncore_write(uncore, DEIER,
488 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
489 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
490 intel_uncore_write(uncore, DEIMR,
491 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
493 /* Go back to the starting frequency */
494 gen5_rps_set(rps, rps->idle_freq);
496 rgvswctl |= MEMCTL_CMD_STS;
497 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
500 spin_unlock_irq(&mchdev_lock);
503 static u32 rps_limits(struct intel_rps *rps, u8 val)
508 * Only set the down limit when we've reached the lowest level to avoid
509 * getting more interrupts, otherwise leave this clear. This prevents a
510 * race in the hw when coming out of rc6: There's a tiny window where
511 * the hw runs at the minimal clock before selecting the desired
512 * frequency, if the down threshold expires in that window we will not
513 * receive a down interrupt.
515 if (INTEL_GEN(rps_to_i915(rps)) >= 9) {
516 limits = rps->max_freq_softlimit << 23;
517 if (val <= rps->min_freq_softlimit)
518 limits |= rps->min_freq_softlimit << 14;
520 limits = rps->max_freq_softlimit << 24;
521 if (val <= rps->min_freq_softlimit)
522 limits |= rps->min_freq_softlimit << 16;
528 static void rps_set_power(struct intel_rps *rps, int new_power)
530 struct intel_uncore *uncore = rps_to_uncore(rps);
531 struct drm_i915_private *i915 = rps_to_i915(rps);
532 u32 threshold_up = 0, threshold_down = 0; /* in % */
533 u32 ei_up = 0, ei_down = 0;
535 lockdep_assert_held(&rps->power.mutex);
537 if (new_power == rps->power.mode)
540 /* Note the units here are not exactly 1us, but 1280ns. */
543 /* Upclock if more than 95% busy over 16ms */
547 /* Downclock if less than 85% busy over 32ms */
553 /* Upclock if more than 90% busy over 13ms */
557 /* Downclock if less than 75% busy over 32ms */
563 /* Upclock if more than 85% busy over 10ms */
567 /* Downclock if less than 60% busy over 32ms */
573 /* When byt can survive without system hang with dynamic
574 * sw freq adjustments, this restriction can be lifted.
576 if (IS_VALLEYVIEW(i915))
579 set(uncore, GEN6_RP_UP_EI, GT_INTERVAL_FROM_US(i915, ei_up));
580 set(uncore, GEN6_RP_UP_THRESHOLD,
581 GT_INTERVAL_FROM_US(i915, ei_up * threshold_up / 100));
583 set(uncore, GEN6_RP_DOWN_EI, GT_INTERVAL_FROM_US(i915, ei_down));
584 set(uncore, GEN6_RP_DOWN_THRESHOLD,
585 GT_INTERVAL_FROM_US(i915, ei_down * threshold_down / 100));
587 set(uncore, GEN6_RP_CONTROL,
588 (INTEL_GEN(i915) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
589 GEN6_RP_MEDIA_HW_NORMAL_MODE |
590 GEN6_RP_MEDIA_IS_GFX |
592 GEN6_RP_UP_BUSY_AVG |
593 GEN6_RP_DOWN_IDLE_AVG);
596 rps->power.mode = new_power;
597 rps->power.up_threshold = threshold_up;
598 rps->power.down_threshold = threshold_down;
601 static void gen6_rps_set_thresholds(struct intel_rps *rps, u8 val)
605 new_power = rps->power.mode;
606 switch (rps->power.mode) {
608 if (val > rps->efficient_freq + 1 &&
614 if (val <= rps->efficient_freq &&
616 new_power = LOW_POWER;
617 else if (val >= rps->rp0_freq &&
619 new_power = HIGH_POWER;
623 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
628 /* Max/min bins are special */
629 if (val <= rps->min_freq_softlimit)
630 new_power = LOW_POWER;
631 if (val >= rps->max_freq_softlimit)
632 new_power = HIGH_POWER;
634 mutex_lock(&rps->power.mutex);
635 if (rps->power.interactive)
636 new_power = HIGH_POWER;
637 rps_set_power(rps, new_power);
638 mutex_unlock(&rps->power.mutex);
641 void intel_rps_mark_interactive(struct intel_rps *rps, bool interactive)
643 mutex_lock(&rps->power.mutex);
645 if (!rps->power.interactive++ && rps->active)
646 rps_set_power(rps, HIGH_POWER);
648 GEM_BUG_ON(!rps->power.interactive);
649 rps->power.interactive--;
651 mutex_unlock(&rps->power.mutex);
654 static int gen6_rps_set(struct intel_rps *rps, u8 val)
656 struct intel_uncore *uncore = rps_to_uncore(rps);
657 struct drm_i915_private *i915 = rps_to_i915(rps);
660 if (INTEL_GEN(i915) >= 9)
661 swreq = GEN9_FREQUENCY(val);
662 else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
663 swreq = HSW_FREQUENCY(val);
665 swreq = (GEN6_FREQUENCY(val) |
667 GEN6_AGGRESSIVE_TURBO);
668 set(uncore, GEN6_RPNSWREQ, swreq);
673 static int vlv_rps_set(struct intel_rps *rps, u8 val)
675 struct drm_i915_private *i915 = rps_to_i915(rps);
679 err = vlv_punit_write(i915, PUNIT_REG_GPU_FREQ_REQ, val);
685 static int rps_set(struct intel_rps *rps, u8 val, bool update)
687 struct drm_i915_private *i915 = rps_to_i915(rps);
690 if (INTEL_GEN(i915) < 6)
693 if (val == rps->last_freq)
696 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
697 err = vlv_rps_set(rps, val);
699 err = gen6_rps_set(rps, val);
704 gen6_rps_set_thresholds(rps, val);
705 rps->last_freq = val;
710 void intel_rps_unpark(struct intel_rps *rps)
718 * Use the user's desired frequency as a guide, but for better
719 * performance, jump directly to RPe as our starting frequency.
721 mutex_lock(&rps->lock);
723 freq = max(rps->cur_freq, rps->efficient_freq),
724 freq = clamp(freq, rps->min_freq_softlimit, rps->max_freq_softlimit);
725 intel_rps_set(rps, freq);
727 mutex_unlock(&rps->lock);
729 if (INTEL_GEN(rps_to_i915(rps)) >= 6)
730 rps_enable_interrupts(rps);
732 if (IS_GEN(rps_to_i915(rps), 5))
733 gen5_rps_update(rps);
736 void intel_rps_park(struct intel_rps *rps)
738 struct drm_i915_private *i915 = rps_to_i915(rps);
743 if (INTEL_GEN(i915) >= 6)
744 rps_disable_interrupts(rps);
747 if (rps->last_freq <= rps->idle_freq)
751 * The punit delays the write of the frequency and voltage until it
752 * determines the GPU is awake. During normal usage we don't want to
753 * waste power changing the frequency if the GPU is sleeping (rc6).
754 * However, the GPU and driver is now idle and we do not want to delay
755 * switching to minimum voltage (reducing power whilst idle) as we do
756 * not expect to be woken in the near future and so must flush the
757 * change by waking the device.
759 * We choose to take the media powerwell (either would do to trick the
760 * punit into committing the voltage change) as that takes a lot less
761 * power than the render powerwell.
763 intel_uncore_forcewake_get(rps_to_uncore(rps), FORCEWAKE_MEDIA);
764 rps_set(rps, rps->idle_freq, false);
765 intel_uncore_forcewake_put(rps_to_uncore(rps), FORCEWAKE_MEDIA);
768 void intel_rps_boost(struct i915_request *rq)
770 struct intel_rps *rps = &rq->engine->gt->rps;
773 if (i915_request_signaled(rq) || !rps->active)
776 /* Serializes with i915_request_retire() */
777 spin_lock_irqsave(&rq->lock, flags);
778 if (!i915_request_has_waitboost(rq) &&
779 !dma_fence_is_signaled_locked(&rq->fence)) {
780 set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags);
782 if (!atomic_fetch_inc(&rps->num_waiters) &&
783 READ_ONCE(rps->cur_freq) < rps->boost_freq)
784 schedule_work(&rps->work);
786 atomic_inc(&rps->boosts);
788 spin_unlock_irqrestore(&rq->lock, flags);
791 int intel_rps_set(struct intel_rps *rps, u8 val)
795 lockdep_assert_held(&rps->lock);
796 GEM_BUG_ON(val > rps->max_freq);
797 GEM_BUG_ON(val < rps->min_freq);
800 err = rps_set(rps, val, true);
805 * Make sure we continue to get interrupts
806 * until we hit the minimum or maximum frequencies.
808 if (INTEL_GEN(rps_to_i915(rps)) >= 6) {
809 struct intel_uncore *uncore = rps_to_uncore(rps);
812 GEN6_RP_INTERRUPT_LIMITS, rps_limits(rps, val));
814 set(uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, val));
822 static void gen6_rps_init(struct intel_rps *rps)
824 struct drm_i915_private *i915 = rps_to_i915(rps);
825 struct intel_uncore *uncore = rps_to_uncore(rps);
827 /* All of these values are in units of 50MHz */
829 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
830 if (IS_GEN9_LP(i915)) {
831 u32 rp_state_cap = intel_uncore_read(uncore, BXT_RP_STATE_CAP);
833 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
834 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
835 rps->min_freq = (rp_state_cap >> 0) & 0xff;
837 u32 rp_state_cap = intel_uncore_read(uncore, GEN6_RP_STATE_CAP);
839 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
840 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
841 rps->min_freq = (rp_state_cap >> 16) & 0xff;
844 /* hw_max = RP0 until we check for overclocking */
845 rps->max_freq = rps->rp0_freq;
847 rps->efficient_freq = rps->rp1_freq;
848 if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
849 IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
852 if (sandybridge_pcode_read(i915,
853 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
854 &ddcc_status, NULL) == 0)
855 rps->efficient_freq =
857 (ddcc_status >> 8) & 0xff,
862 if (IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
863 /* Store the frequency values in 16.66 MHZ units, which is
864 * the natural hardware unit for SKL
866 rps->rp0_freq *= GEN9_FREQ_SCALER;
867 rps->rp1_freq *= GEN9_FREQ_SCALER;
868 rps->min_freq *= GEN9_FREQ_SCALER;
869 rps->max_freq *= GEN9_FREQ_SCALER;
870 rps->efficient_freq *= GEN9_FREQ_SCALER;
874 static bool rps_reset(struct intel_rps *rps)
877 rps->power.mode = -1;
880 if (rps_set(rps, rps->min_freq, true)) {
881 DRM_ERROR("Failed to reset RPS to initial values\n");
885 rps->cur_freq = rps->min_freq;
889 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
890 static bool gen9_rps_enable(struct intel_rps *rps)
892 struct drm_i915_private *i915 = rps_to_i915(rps);
893 struct intel_uncore *uncore = rps_to_uncore(rps);
895 /* Program defaults and thresholds for RPS */
897 intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
898 GEN9_FREQUENCY(rps->rp1_freq));
900 /* 1 second timeout */
901 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT,
902 GT_INTERVAL_FROM_US(i915, 1000000));
904 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 0xa);
906 return rps_reset(rps);
909 static bool gen8_rps_enable(struct intel_rps *rps)
911 struct intel_uncore *uncore = rps_to_uncore(rps);
913 intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
914 HSW_FREQUENCY(rps->rp1_freq));
916 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
917 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT,
918 100000000 / 128); /* 1 second timeout */
920 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
922 return rps_reset(rps);
925 static bool gen6_rps_enable(struct intel_rps *rps)
927 struct intel_uncore *uncore = rps_to_uncore(rps);
929 /* Power down if completely idle for over 50ms */
930 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 50000);
931 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
933 return rps_reset(rps);
936 static int chv_rps_max_freq(struct intel_rps *rps)
938 struct drm_i915_private *i915 = rps_to_i915(rps);
941 val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
943 switch (RUNTIME_INFO(i915)->sseu.eu_total) {
946 val >>= FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT;
950 val >>= FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT;
955 /* Setting (2 * 8) Min RP0 for any other combination */
956 val >>= FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT;
960 return val & FB_GFX_FREQ_FUSE_MASK;
963 static int chv_rps_rpe_freq(struct intel_rps *rps)
965 struct drm_i915_private *i915 = rps_to_i915(rps);
968 val = vlv_punit_read(i915, PUNIT_GPU_DUTYCYCLE_REG);
969 val >>= PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT;
971 return val & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
974 static int chv_rps_guar_freq(struct intel_rps *rps)
976 struct drm_i915_private *i915 = rps_to_i915(rps);
979 val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
981 return val & FB_GFX_FREQ_FUSE_MASK;
984 static u32 chv_rps_min_freq(struct intel_rps *rps)
986 struct drm_i915_private *i915 = rps_to_i915(rps);
989 val = vlv_punit_read(i915, FB_GFX_FMIN_AT_VMIN_FUSE);
990 val >>= FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT;
992 return val & FB_GFX_FREQ_FUSE_MASK;
995 static bool chv_rps_enable(struct intel_rps *rps)
997 struct intel_uncore *uncore = rps_to_uncore(rps);
998 struct drm_i915_private *i915 = rps_to_i915(rps);
1001 /* 1: Program defaults and thresholds for RPS*/
1002 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1003 intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1004 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1005 intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1006 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1008 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1011 intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1012 GEN6_RP_MEDIA_HW_NORMAL_MODE |
1013 GEN6_RP_MEDIA_IS_GFX |
1015 GEN6_RP_UP_BUSY_AVG |
1016 GEN6_RP_DOWN_IDLE_AVG);
1018 /* Setting Fixed Bias */
1019 vlv_punit_get(i915);
1021 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
1022 vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1024 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1026 vlv_punit_put(i915);
1028 /* RPS code assumes GPLL is used */
1029 drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1030 "GPLL not enabled\n");
1032 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
1033 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
1035 return rps_reset(rps);
1038 static int vlv_rps_guar_freq(struct intel_rps *rps)
1040 struct drm_i915_private *i915 = rps_to_i915(rps);
1043 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1045 rp1 = val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK;
1046 rp1 >>= FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
1051 static int vlv_rps_max_freq(struct intel_rps *rps)
1053 struct drm_i915_private *i915 = rps_to_i915(rps);
1056 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1058 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
1060 rp0 = min_t(u32, rp0, 0xea);
1065 static int vlv_rps_rpe_freq(struct intel_rps *rps)
1067 struct drm_i915_private *i915 = rps_to_i915(rps);
1070 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
1071 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
1072 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
1073 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
1078 static int vlv_rps_min_freq(struct intel_rps *rps)
1080 struct drm_i915_private *i915 = rps_to_i915(rps);
1083 val = vlv_punit_read(i915, PUNIT_REG_GPU_LFM) & 0xff;
1085 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
1086 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
1087 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
1088 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
1089 * to make sure it matches what Punit accepts.
1091 return max_t(u32, val, 0xc0);
1094 static bool vlv_rps_enable(struct intel_rps *rps)
1096 struct intel_uncore *uncore = rps_to_uncore(rps);
1097 struct drm_i915_private *i915 = rps_to_i915(rps);
1100 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1101 intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1102 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1103 intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1104 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1106 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1108 intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1109 GEN6_RP_MEDIA_TURBO |
1110 GEN6_RP_MEDIA_HW_NORMAL_MODE |
1111 GEN6_RP_MEDIA_IS_GFX |
1113 GEN6_RP_UP_BUSY_AVG |
1114 GEN6_RP_DOWN_IDLE_CONT);
1116 vlv_punit_get(i915);
1118 /* Setting Fixed Bias */
1119 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
1120 vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1122 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1124 vlv_punit_put(i915);
1126 /* RPS code assumes GPLL is used */
1127 drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1128 "GPLL not enabled\n");
1130 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
1131 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
1133 return rps_reset(rps);
1136 static unsigned long __ips_gfx_val(struct intel_ips *ips)
1138 struct intel_rps *rps = container_of(ips, typeof(*rps), ips);
1139 struct intel_uncore *uncore = rps_to_uncore(rps);
1140 unsigned long t, corr, state1, corr2, state2;
1143 lockdep_assert_held(&mchdev_lock);
1145 pxvid = intel_uncore_read(uncore, PXVFREQ(rps->cur_freq));
1146 pxvid = (pxvid >> 24) & 0x7f;
1147 ext_v = pvid_to_extvid(rps_to_i915(rps), pxvid);
1151 /* Revel in the empirically derived constants */
1153 /* Correction factor in 1/100000 units */
1154 t = ips_mch_val(uncore);
1156 corr = t * 2349 + 135940;
1158 corr = t * 964 + 29317;
1160 corr = t * 301 + 1004;
1162 corr = corr * 150142 * state1 / 10000 - 78642;
1164 corr2 = corr * ips->corr;
1166 state2 = corr2 * state1 / 10000;
1167 state2 /= 100; /* convert to mW */
1169 __gen5_ips_update(ips);
1171 return ips->gfx_power + state2;
1174 void intel_rps_enable(struct intel_rps *rps)
1176 struct drm_i915_private *i915 = rps_to_i915(rps);
1177 struct intel_uncore *uncore = rps_to_uncore(rps);
1179 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1180 if (IS_CHERRYVIEW(i915))
1181 rps->enabled = chv_rps_enable(rps);
1182 else if (IS_VALLEYVIEW(i915))
1183 rps->enabled = vlv_rps_enable(rps);
1184 else if (INTEL_GEN(i915) >= 9)
1185 rps->enabled = gen9_rps_enable(rps);
1186 else if (INTEL_GEN(i915) >= 8)
1187 rps->enabled = gen8_rps_enable(rps);
1188 else if (INTEL_GEN(i915) >= 6)
1189 rps->enabled = gen6_rps_enable(rps);
1190 else if (IS_IRONLAKE_M(i915))
1191 rps->enabled = gen5_rps_enable(rps);
1192 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
1196 drm_WARN_ON(&i915->drm, rps->max_freq < rps->min_freq);
1197 drm_WARN_ON(&i915->drm, rps->idle_freq > rps->max_freq);
1199 drm_WARN_ON(&i915->drm, rps->efficient_freq < rps->min_freq);
1200 drm_WARN_ON(&i915->drm, rps->efficient_freq > rps->max_freq);
1203 static void gen6_rps_disable(struct intel_rps *rps)
1205 set(rps_to_uncore(rps), GEN6_RP_CONTROL, 0);
1208 void intel_rps_disable(struct intel_rps *rps)
1210 struct drm_i915_private *i915 = rps_to_i915(rps);
1212 rps->enabled = false;
1214 if (INTEL_GEN(i915) >= 6)
1215 gen6_rps_disable(rps);
1216 else if (IS_IRONLAKE_M(i915))
1217 gen5_rps_disable(rps);
1220 static int byt_gpu_freq(struct intel_rps *rps, int val)
1224 * Slow = Fast = GPLL ref * N
1226 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
1229 static int byt_freq_opcode(struct intel_rps *rps, int val)
1231 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
1234 static int chv_gpu_freq(struct intel_rps *rps, int val)
1238 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
1240 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
1243 static int chv_freq_opcode(struct intel_rps *rps, int val)
1245 /* CHV needs even values */
1246 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
1249 int intel_gpu_freq(struct intel_rps *rps, int val)
1251 struct drm_i915_private *i915 = rps_to_i915(rps);
1253 if (INTEL_GEN(i915) >= 9)
1254 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
1256 else if (IS_CHERRYVIEW(i915))
1257 return chv_gpu_freq(rps, val);
1258 else if (IS_VALLEYVIEW(i915))
1259 return byt_gpu_freq(rps, val);
1261 return val * GT_FREQUENCY_MULTIPLIER;
1264 int intel_freq_opcode(struct intel_rps *rps, int val)
1266 struct drm_i915_private *i915 = rps_to_i915(rps);
1268 if (INTEL_GEN(i915) >= 9)
1269 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
1270 GT_FREQUENCY_MULTIPLIER);
1271 else if (IS_CHERRYVIEW(i915))
1272 return chv_freq_opcode(rps, val);
1273 else if (IS_VALLEYVIEW(i915))
1274 return byt_freq_opcode(rps, val);
1276 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
1279 static void vlv_init_gpll_ref_freq(struct intel_rps *rps)
1281 struct drm_i915_private *i915 = rps_to_i915(rps);
1283 rps->gpll_ref_freq =
1284 vlv_get_cck_clock(i915, "GPLL ref",
1285 CCK_GPLL_CLOCK_CONTROL,
1288 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n", rps->gpll_ref_freq);
1291 static void vlv_rps_init(struct intel_rps *rps)
1293 struct drm_i915_private *i915 = rps_to_i915(rps);
1296 vlv_iosf_sb_get(i915,
1297 BIT(VLV_IOSF_SB_PUNIT) |
1298 BIT(VLV_IOSF_SB_NC) |
1299 BIT(VLV_IOSF_SB_CCK));
1301 vlv_init_gpll_ref_freq(rps);
1303 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1304 switch ((val >> 6) & 3) {
1307 i915->mem_freq = 800;
1310 i915->mem_freq = 1066;
1313 i915->mem_freq = 1333;
1316 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", i915->mem_freq);
1318 rps->max_freq = vlv_rps_max_freq(rps);
1319 rps->rp0_freq = rps->max_freq;
1320 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
1321 intel_gpu_freq(rps, rps->max_freq),
1324 rps->efficient_freq = vlv_rps_rpe_freq(rps);
1325 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
1326 intel_gpu_freq(rps, rps->efficient_freq),
1327 rps->efficient_freq);
1329 rps->rp1_freq = vlv_rps_guar_freq(rps);
1330 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
1331 intel_gpu_freq(rps, rps->rp1_freq),
1334 rps->min_freq = vlv_rps_min_freq(rps);
1335 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
1336 intel_gpu_freq(rps, rps->min_freq),
1339 vlv_iosf_sb_put(i915,
1340 BIT(VLV_IOSF_SB_PUNIT) |
1341 BIT(VLV_IOSF_SB_NC) |
1342 BIT(VLV_IOSF_SB_CCK));
1345 static void chv_rps_init(struct intel_rps *rps)
1347 struct drm_i915_private *i915 = rps_to_i915(rps);
1350 vlv_iosf_sb_get(i915,
1351 BIT(VLV_IOSF_SB_PUNIT) |
1352 BIT(VLV_IOSF_SB_NC) |
1353 BIT(VLV_IOSF_SB_CCK));
1355 vlv_init_gpll_ref_freq(rps);
1357 val = vlv_cck_read(i915, CCK_FUSE_REG);
1359 switch ((val >> 2) & 0x7) {
1361 i915->mem_freq = 2000;
1364 i915->mem_freq = 1600;
1367 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", i915->mem_freq);
1369 rps->max_freq = chv_rps_max_freq(rps);
1370 rps->rp0_freq = rps->max_freq;
1371 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
1372 intel_gpu_freq(rps, rps->max_freq),
1375 rps->efficient_freq = chv_rps_rpe_freq(rps);
1376 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
1377 intel_gpu_freq(rps, rps->efficient_freq),
1378 rps->efficient_freq);
1380 rps->rp1_freq = chv_rps_guar_freq(rps);
1381 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
1382 intel_gpu_freq(rps, rps->rp1_freq),
1385 rps->min_freq = chv_rps_min_freq(rps);
1386 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
1387 intel_gpu_freq(rps, rps->min_freq),
1390 vlv_iosf_sb_put(i915,
1391 BIT(VLV_IOSF_SB_PUNIT) |
1392 BIT(VLV_IOSF_SB_NC) |
1393 BIT(VLV_IOSF_SB_CCK));
1395 drm_WARN_ONCE(&i915->drm, (rps->max_freq | rps->efficient_freq |
1396 rps->rp1_freq | rps->min_freq) & 1,
1397 "Odd GPU freq values\n");
1400 static void vlv_c0_read(struct intel_uncore *uncore, struct intel_rps_ei *ei)
1402 ei->ktime = ktime_get_raw();
1403 ei->render_c0 = intel_uncore_read(uncore, VLV_RENDER_C0_COUNT);
1404 ei->media_c0 = intel_uncore_read(uncore, VLV_MEDIA_C0_COUNT);
1407 static u32 vlv_wa_c0_ei(struct intel_rps *rps, u32 pm_iir)
1409 struct intel_uncore *uncore = rps_to_uncore(rps);
1410 const struct intel_rps_ei *prev = &rps->ei;
1411 struct intel_rps_ei now;
1414 if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
1417 vlv_c0_read(uncore, &now);
1423 time = ktime_us_delta(now.ktime, prev->ktime);
1425 time *= rps_to_i915(rps)->czclk_freq;
1427 /* Workload can be split between render + media,
1428 * e.g. SwapBuffers being blitted in X after being rendered in
1429 * mesa. To account for this we need to combine both engines
1430 * into our activity counter.
1432 render = now.render_c0 - prev->render_c0;
1433 media = now.media_c0 - prev->media_c0;
1434 c0 = max(render, media);
1435 c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1437 if (c0 > time * rps->power.up_threshold)
1438 events = GEN6_PM_RP_UP_THRESHOLD;
1439 else if (c0 < time * rps->power.down_threshold)
1440 events = GEN6_PM_RP_DOWN_THRESHOLD;
1447 static void rps_work(struct work_struct *work)
1449 struct intel_rps *rps = container_of(work, typeof(*rps), work);
1450 struct intel_gt *gt = rps_to_gt(rps);
1451 bool client_boost = false;
1452 int new_freq, adj, min, max;
1455 spin_lock_irq(>->irq_lock);
1456 pm_iir = fetch_and_zero(&rps->pm_iir);
1457 client_boost = atomic_read(&rps->num_waiters);
1458 spin_unlock_irq(>->irq_lock);
1460 /* Make sure we didn't queue anything we're not going to process. */
1461 if ((pm_iir & rps->pm_events) == 0 && !client_boost)
1464 mutex_lock(&rps->lock);
1466 pm_iir |= vlv_wa_c0_ei(rps, pm_iir);
1468 adj = rps->last_adj;
1469 new_freq = rps->cur_freq;
1470 min = rps->min_freq_softlimit;
1471 max = rps->max_freq_softlimit;
1473 max = rps->max_freq;
1474 if (client_boost && new_freq < rps->boost_freq) {
1475 new_freq = rps->boost_freq;
1477 } else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1480 else /* CHV needs even encode values */
1481 adj = IS_CHERRYVIEW(gt->i915) ? 2 : 1;
1483 if (new_freq >= rps->max_freq_softlimit)
1485 } else if (client_boost) {
1487 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1488 if (rps->cur_freq > rps->efficient_freq)
1489 new_freq = rps->efficient_freq;
1490 else if (rps->cur_freq > rps->min_freq_softlimit)
1491 new_freq = rps->min_freq_softlimit;
1493 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1496 else /* CHV needs even encode values */
1497 adj = IS_CHERRYVIEW(gt->i915) ? -2 : -1;
1499 if (new_freq <= rps->min_freq_softlimit)
1501 } else { /* unknown event */
1505 rps->last_adj = adj;
1508 * Limit deboosting and boosting to keep ourselves at the extremes
1509 * when in the respective power modes (i.e. slowly decrease frequencies
1510 * while in the HIGH_POWER zone and slowly increase frequencies while
1511 * in the LOW_POWER zone). On idle, we will hit the timeout and drop
1512 * to the next level quickly, and conversely if busy we expect to
1513 * hit a waitboost and rapidly switch into max power.
1515 if ((adj < 0 && rps->power.mode == HIGH_POWER) ||
1516 (adj > 0 && rps->power.mode == LOW_POWER))
1519 /* sysfs frequency interfaces may have snuck in while servicing the
1523 new_freq = clamp_t(int, new_freq, min, max);
1525 if (intel_rps_set(rps, new_freq)) {
1526 DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
1530 mutex_unlock(&rps->lock);
1533 spin_lock_irq(>->irq_lock);
1534 gen6_gt_pm_unmask_irq(gt, rps->pm_events);
1535 spin_unlock_irq(>->irq_lock);
1538 void gen11_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1540 struct intel_gt *gt = rps_to_gt(rps);
1541 const u32 events = rps->pm_events & pm_iir;
1543 lockdep_assert_held(>->irq_lock);
1545 if (unlikely(!events))
1548 gen6_gt_pm_mask_irq(gt, events);
1550 rps->pm_iir |= events;
1551 schedule_work(&rps->work);
1554 void gen6_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1556 struct intel_gt *gt = rps_to_gt(rps);
1558 if (pm_iir & rps->pm_events) {
1559 spin_lock(>->irq_lock);
1560 gen6_gt_pm_mask_irq(gt, pm_iir & rps->pm_events);
1561 rps->pm_iir |= pm_iir & rps->pm_events;
1562 schedule_work(&rps->work);
1563 spin_unlock(>->irq_lock);
1566 if (INTEL_GEN(gt->i915) >= 8)
1569 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1570 intel_engine_signal_breadcrumbs(gt->engine[VECS0]);
1572 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1573 DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
1576 void gen5_rps_irq_handler(struct intel_rps *rps)
1578 struct intel_uncore *uncore = rps_to_uncore(rps);
1579 u32 busy_up, busy_down, max_avg, min_avg;
1582 spin_lock(&mchdev_lock);
1584 intel_uncore_write16(uncore,
1586 intel_uncore_read(uncore, MEMINTRSTS));
1588 intel_uncore_write16(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
1589 busy_up = intel_uncore_read(uncore, RCPREVBSYTUPAVG);
1590 busy_down = intel_uncore_read(uncore, RCPREVBSYTDNAVG);
1591 max_avg = intel_uncore_read(uncore, RCBMAXAVG);
1592 min_avg = intel_uncore_read(uncore, RCBMINAVG);
1594 /* Handle RCS change request from hw */
1595 new_freq = rps->cur_freq;
1596 if (busy_up > max_avg)
1598 else if (busy_down < min_avg)
1600 new_freq = clamp(new_freq,
1601 rps->min_freq_softlimit,
1602 rps->max_freq_softlimit);
1604 if (new_freq != rps->cur_freq && gen5_rps_set(rps, new_freq))
1605 rps->cur_freq = new_freq;
1607 spin_unlock(&mchdev_lock);
1610 void intel_rps_init_early(struct intel_rps *rps)
1612 mutex_init(&rps->lock);
1613 mutex_init(&rps->power.mutex);
1615 INIT_WORK(&rps->work, rps_work);
1617 atomic_set(&rps->num_waiters, 0);
1620 void intel_rps_init(struct intel_rps *rps)
1622 struct drm_i915_private *i915 = rps_to_i915(rps);
1624 if (IS_CHERRYVIEW(i915))
1626 else if (IS_VALLEYVIEW(i915))
1628 else if (INTEL_GEN(i915) >= 6)
1630 else if (IS_IRONLAKE_M(i915))
1633 /* Derive initial user preferences/limits from the hardware limits */
1634 rps->max_freq_softlimit = rps->max_freq;
1635 rps->min_freq_softlimit = rps->min_freq;
1637 /* After setting max-softlimit, find the overclock max freq */
1638 if (IS_GEN(i915, 6) || IS_IVYBRIDGE(i915) || IS_HASWELL(i915)) {
1641 sandybridge_pcode_read(i915, GEN6_READ_OC_PARAMS,
1643 if (params & BIT(31)) { /* OC supported */
1644 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
1645 (rps->max_freq & 0xff) * 50,
1646 (params & 0xff) * 50);
1647 rps->max_freq = params & 0xff;
1651 /* Finally allow us to boost to max by default */
1652 rps->boost_freq = rps->max_freq;
1653 rps->idle_freq = rps->min_freq;
1654 rps->cur_freq = rps->idle_freq;
1656 rps->pm_intrmsk_mbz = 0;
1659 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
1660 * if GEN6_PM_UP_EI_EXPIRED is masked.
1662 * TODO: verify if this can be reproduced on VLV,CHV.
1664 if (INTEL_GEN(i915) <= 7)
1665 rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
1667 if (INTEL_GEN(i915) >= 8 && INTEL_GEN(i915) < 11)
1668 rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1671 u32 intel_rps_get_cagf(struct intel_rps *rps, u32 rpstat)
1673 struct drm_i915_private *i915 = rps_to_i915(rps);
1676 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1677 cagf = (rpstat >> 8) & 0xff;
1678 else if (INTEL_GEN(i915) >= 9)
1679 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
1680 else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
1681 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
1683 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
1688 static u32 read_cagf(struct intel_rps *rps)
1690 struct drm_i915_private *i915 = rps_to_i915(rps);
1693 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
1694 vlv_punit_get(i915);
1695 freq = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1696 vlv_punit_put(i915);
1698 freq = intel_uncore_read(rps_to_gt(rps)->uncore, GEN6_RPSTAT1);
1701 return intel_rps_get_cagf(rps, freq);
1704 u32 intel_rps_read_actual_frequency(struct intel_rps *rps)
1706 struct intel_runtime_pm *rpm = rps_to_gt(rps)->uncore->rpm;
1707 intel_wakeref_t wakeref;
1710 with_intel_runtime_pm_if_in_use(rpm, wakeref)
1711 freq = intel_gpu_freq(rps, read_cagf(rps));
1716 /* External interface for intel_ips.ko */
1718 static struct drm_i915_private __rcu *ips_mchdev;
1721 * Tells the intel_ips driver that the i915 driver is now loaded, if
1722 * IPS got loaded first.
1724 * This awkward dance is so that neither module has to depend on the
1725 * other in order for IPS to do the appropriate communication of
1726 * GPU turbo limits to i915.
1729 ips_ping_for_i915_load(void)
1733 link = symbol_get(ips_link_to_i915_driver);
1736 symbol_put(ips_link_to_i915_driver);
1740 void intel_rps_driver_register(struct intel_rps *rps)
1742 struct intel_gt *gt = rps_to_gt(rps);
1745 * We only register the i915 ips part with intel-ips once everything is
1746 * set up, to avoid intel-ips sneaking in and reading bogus values.
1748 if (IS_GEN(gt->i915, 5)) {
1749 GEM_BUG_ON(ips_mchdev);
1750 rcu_assign_pointer(ips_mchdev, gt->i915);
1751 ips_ping_for_i915_load();
1755 void intel_rps_driver_unregister(struct intel_rps *rps)
1757 if (rcu_access_pointer(ips_mchdev) == rps_to_i915(rps))
1758 rcu_assign_pointer(ips_mchdev, NULL);
1761 static struct drm_i915_private *mchdev_get(void)
1763 struct drm_i915_private *i915;
1766 i915 = rcu_dereference(ips_mchdev);
1767 if (!kref_get_unless_zero(&i915->drm.ref))
1775 * i915_read_mch_val - return value for IPS use
1777 * Calculate and return a value for the IPS driver to use when deciding whether
1778 * we have thermal and power headroom to increase CPU or GPU power budget.
1780 unsigned long i915_read_mch_val(void)
1782 struct drm_i915_private *i915;
1783 unsigned long chipset_val = 0;
1784 unsigned long graphics_val = 0;
1785 intel_wakeref_t wakeref;
1787 i915 = mchdev_get();
1791 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
1792 struct intel_ips *ips = &i915->gt.rps.ips;
1794 spin_lock_irq(&mchdev_lock);
1795 chipset_val = __ips_chipset_val(ips);
1796 graphics_val = __ips_gfx_val(ips);
1797 spin_unlock_irq(&mchdev_lock);
1800 drm_dev_put(&i915->drm);
1801 return chipset_val + graphics_val;
1803 EXPORT_SYMBOL_GPL(i915_read_mch_val);
1806 * i915_gpu_raise - raise GPU frequency limit
1808 * Raise the limit; IPS indicates we have thermal headroom.
1810 bool i915_gpu_raise(void)
1812 struct drm_i915_private *i915;
1813 struct intel_rps *rps;
1815 i915 = mchdev_get();
1819 rps = &i915->gt.rps;
1821 spin_lock_irq(&mchdev_lock);
1822 if (rps->max_freq_softlimit < rps->max_freq)
1823 rps->max_freq_softlimit++;
1824 spin_unlock_irq(&mchdev_lock);
1826 drm_dev_put(&i915->drm);
1829 EXPORT_SYMBOL_GPL(i915_gpu_raise);
1832 * i915_gpu_lower - lower GPU frequency limit
1834 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1835 * frequency maximum.
1837 bool i915_gpu_lower(void)
1839 struct drm_i915_private *i915;
1840 struct intel_rps *rps;
1842 i915 = mchdev_get();
1846 rps = &i915->gt.rps;
1848 spin_lock_irq(&mchdev_lock);
1849 if (rps->max_freq_softlimit > rps->min_freq)
1850 rps->max_freq_softlimit--;
1851 spin_unlock_irq(&mchdev_lock);
1853 drm_dev_put(&i915->drm);
1856 EXPORT_SYMBOL_GPL(i915_gpu_lower);
1859 * i915_gpu_busy - indicate GPU business to IPS
1861 * Tell the IPS driver whether or not the GPU is busy.
1863 bool i915_gpu_busy(void)
1865 struct drm_i915_private *i915;
1868 i915 = mchdev_get();
1872 ret = i915->gt.awake;
1874 drm_dev_put(&i915->drm);
1877 EXPORT_SYMBOL_GPL(i915_gpu_busy);
1880 * i915_gpu_turbo_disable - disable graphics turbo
1882 * Disable graphics turbo by resetting the max frequency and setting the
1883 * current frequency to the default.
1885 bool i915_gpu_turbo_disable(void)
1887 struct drm_i915_private *i915;
1888 struct intel_rps *rps;
1891 i915 = mchdev_get();
1895 rps = &i915->gt.rps;
1897 spin_lock_irq(&mchdev_lock);
1898 rps->max_freq_softlimit = rps->min_freq;
1899 ret = gen5_rps_set(&i915->gt.rps, rps->min_freq);
1900 spin_unlock_irq(&mchdev_lock);
1902 drm_dev_put(&i915->drm);
1905 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);