2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include <drm/i915_drm.h>
10 #include "intel_breadcrumbs.h"
12 #include "intel_gt_clock_utils.h"
13 #include "intel_gt_irq.h"
14 #include "intel_gt_pm_irq.h"
15 #include "intel_rps.h"
16 #include "intel_sideband.h"
17 #include "../../../platform/x86/intel_ips.h"
19 #define BUSY_MAX_EI 20u /* ms */
22 * Lock protecting IPS related data structures
24 static DEFINE_SPINLOCK(mchdev_lock);
26 static struct intel_gt *rps_to_gt(struct intel_rps *rps)
28 return container_of(rps, struct intel_gt, rps);
31 static struct drm_i915_private *rps_to_i915(struct intel_rps *rps)
33 return rps_to_gt(rps)->i915;
36 static struct intel_uncore *rps_to_uncore(struct intel_rps *rps)
38 return rps_to_gt(rps)->uncore;
41 static u32 rps_pm_sanitize_mask(struct intel_rps *rps, u32 mask)
43 return mask & ~rps->pm_intrmsk_mbz;
46 static inline void set(struct intel_uncore *uncore, i915_reg_t reg, u32 val)
48 intel_uncore_write_fw(uncore, reg, val);
51 static void rps_timer(struct timer_list *t)
53 struct intel_rps *rps = from_timer(rps, t, timer);
54 struct intel_engine_cs *engine;
55 ktime_t dt, last, timestamp;
56 enum intel_engine_id id;
60 for_each_engine(engine, rps_to_gt(rps), id) {
64 dt = intel_engine_get_busy_time(engine, ×tamp);
65 last = engine->stats.rps;
66 engine->stats.rps = dt;
68 busy = ktime_to_ns(ktime_sub(dt, last));
69 for (i = 0; i < ARRAY_SIZE(max_busy); i++) {
70 if (busy > max_busy[i])
71 swap(busy, max_busy[i]);
74 last = rps->pm_timestamp;
75 rps->pm_timestamp = timestamp;
77 if (intel_rps_is_active(rps)) {
81 dt = ktime_sub(timestamp, last);
84 * Our goal is to evaluate each engine independently, so we run
85 * at the lowest clocks required to sustain the heaviest
86 * workload. However, a task may be split into sequential
87 * dependent operations across a set of engines, such that
88 * the independent contributions do not account for high load,
89 * but overall the task is GPU bound. For example, consider
90 * video decode on vcs followed by colour post-processing
91 * on vecs, followed by general post-processing on rcs.
92 * Since multi-engines being active does imply a single
93 * continuous workload across all engines, we hedge our
94 * bets by only contributing a factor of the distributed
95 * load into our busyness calculation.
98 for (i = 1; i < ARRAY_SIZE(max_busy); i++) {
102 busy += div_u64(max_busy[i], 1 << i);
104 GT_TRACE(rps_to_gt(rps),
105 "busy:%lld [%d%%], max:[%lld, %lld, %lld], interval:%d\n",
106 busy, (int)div64_u64(100 * busy, dt),
107 max_busy[0], max_busy[1], max_busy[2],
110 if (100 * busy > rps->power.up_threshold * dt &&
111 rps->cur_freq < rps->max_freq_softlimit) {
112 rps->pm_iir |= GEN6_PM_RP_UP_THRESHOLD;
113 rps->pm_interval = 1;
114 schedule_work(&rps->work);
115 } else if (100 * busy < rps->power.down_threshold * dt &&
116 rps->cur_freq > rps->min_freq_softlimit) {
117 rps->pm_iir |= GEN6_PM_RP_DOWN_THRESHOLD;
118 rps->pm_interval = 1;
119 schedule_work(&rps->work);
124 mod_timer(&rps->timer,
125 jiffies + msecs_to_jiffies(rps->pm_interval));
126 rps->pm_interval = min(rps->pm_interval * 2, BUSY_MAX_EI);
130 static void rps_start_timer(struct intel_rps *rps)
132 rps->pm_timestamp = ktime_sub(ktime_get(), rps->pm_timestamp);
133 rps->pm_interval = 1;
134 mod_timer(&rps->timer, jiffies + 1);
137 static void rps_stop_timer(struct intel_rps *rps)
139 del_timer_sync(&rps->timer);
140 rps->pm_timestamp = ktime_sub(ktime_get(), rps->pm_timestamp);
141 cancel_work_sync(&rps->work);
144 static u32 rps_pm_mask(struct intel_rps *rps, u8 val)
148 /* We use UP_EI_EXPIRED interrupts for both up/down in manual mode */
149 if (val > rps->min_freq_softlimit)
150 mask |= (GEN6_PM_RP_UP_EI_EXPIRED |
151 GEN6_PM_RP_DOWN_THRESHOLD |
152 GEN6_PM_RP_DOWN_TIMEOUT);
154 if (val < rps->max_freq_softlimit)
155 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
157 mask &= rps->pm_events;
159 return rps_pm_sanitize_mask(rps, ~mask);
162 static void rps_reset_ei(struct intel_rps *rps)
164 memset(&rps->ei, 0, sizeof(rps->ei));
167 static void rps_enable_interrupts(struct intel_rps *rps)
169 struct intel_gt *gt = rps_to_gt(rps);
171 GT_TRACE(gt, "interrupts:on rps->pm_events: %x, rps_pm_mask:%x\n",
172 rps->pm_events, rps_pm_mask(rps, rps->last_freq));
176 spin_lock_irq(>->irq_lock);
177 gen6_gt_pm_enable_irq(gt, rps->pm_events);
178 spin_unlock_irq(>->irq_lock);
180 intel_uncore_write(gt->uncore,
181 GEN6_PMINTRMSK, rps_pm_mask(rps, rps->last_freq));
184 static void gen6_rps_reset_interrupts(struct intel_rps *rps)
186 gen6_gt_pm_reset_iir(rps_to_gt(rps), GEN6_PM_RPS_EVENTS);
189 static void gen11_rps_reset_interrupts(struct intel_rps *rps)
191 while (gen11_gt_reset_one_iir(rps_to_gt(rps), 0, GEN11_GTPM))
195 static void rps_reset_interrupts(struct intel_rps *rps)
197 struct intel_gt *gt = rps_to_gt(rps);
199 spin_lock_irq(>->irq_lock);
200 if (INTEL_GEN(gt->i915) >= 11)
201 gen11_rps_reset_interrupts(rps);
203 gen6_rps_reset_interrupts(rps);
206 spin_unlock_irq(>->irq_lock);
209 static void rps_disable_interrupts(struct intel_rps *rps)
211 struct intel_gt *gt = rps_to_gt(rps);
213 intel_uncore_write(gt->uncore,
214 GEN6_PMINTRMSK, rps_pm_sanitize_mask(rps, ~0u));
216 spin_lock_irq(>->irq_lock);
217 gen6_gt_pm_disable_irq(gt, GEN6_PM_RPS_EVENTS);
218 spin_unlock_irq(>->irq_lock);
220 intel_synchronize_irq(gt->i915);
223 * Now that we will not be generating any more work, flush any
224 * outstanding tasks. As we are called on the RPS idle path,
225 * we will reset the GPU to minimum frequencies, so the current
226 * state of the worker can be discarded.
228 cancel_work_sync(&rps->work);
230 rps_reset_interrupts(rps);
231 GT_TRACE(gt, "interrupts:off\n");
234 static const struct cparams {
240 { 1, 1333, 301, 28664 },
241 { 1, 1066, 294, 24460 },
242 { 1, 800, 294, 25192 },
243 { 0, 1333, 276, 27605 },
244 { 0, 1066, 276, 27605 },
245 { 0, 800, 231, 23784 },
248 static void gen5_rps_init(struct intel_rps *rps)
250 struct drm_i915_private *i915 = rps_to_i915(rps);
251 struct intel_uncore *uncore = rps_to_uncore(rps);
252 u8 fmax, fmin, fstart;
256 if (i915->fsb_freq <= 3200)
258 else if (i915->fsb_freq <= 4800)
263 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
264 if (cparams[i].i == c_m && cparams[i].t == i915->mem_freq) {
265 rps->ips.m = cparams[i].m;
266 rps->ips.c = cparams[i].c;
271 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
273 /* Set up min, max, and cur for interrupt handling */
274 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
275 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
276 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
277 MEMMODE_FSTART_SHIFT;
278 drm_dbg(&i915->drm, "fmax: %d, fmin: %d, fstart: %d\n",
281 rps->min_freq = fmax;
282 rps->efficient_freq = fstart;
283 rps->max_freq = fmin;
287 __ips_chipset_val(struct intel_ips *ips)
289 struct intel_uncore *uncore =
290 rps_to_uncore(container_of(ips, struct intel_rps, ips));
291 unsigned long now = jiffies_to_msecs(jiffies), dt;
292 unsigned long result;
295 lockdep_assert_held(&mchdev_lock);
298 * Prevent division-by-zero if we are asking too fast.
299 * Also, we don't get interesting results if we are polling
300 * faster than once in 10ms, so just return the saved value
303 dt = now - ips->last_time1;
305 return ips->chipset_power;
307 /* FIXME: handle per-counter overflow */
308 total = intel_uncore_read(uncore, DMIEC);
309 total += intel_uncore_read(uncore, DDREC);
310 total += intel_uncore_read(uncore, CSIEC);
312 delta = total - ips->last_count1;
314 result = div_u64(div_u64(ips->m * delta, dt) + ips->c, 10);
316 ips->last_count1 = total;
317 ips->last_time1 = now;
319 ips->chipset_power = result;
324 static unsigned long ips_mch_val(struct intel_uncore *uncore)
326 unsigned int m, x, b;
329 tsfs = intel_uncore_read(uncore, TSFS);
330 x = intel_uncore_read8(uncore, TR1);
332 b = tsfs & TSFS_INTR_MASK;
333 m = (tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT;
335 return m * x / 127 - b;
338 static int _pxvid_to_vd(u8 pxvid)
343 if (pxvid >= 8 && pxvid < 31)
346 return (pxvid + 2) * 125;
349 static u32 pvid_to_extvid(struct drm_i915_private *i915, u8 pxvid)
351 const int vd = _pxvid_to_vd(pxvid);
353 if (INTEL_INFO(i915)->is_mobile)
354 return max(vd - 1125, 0);
359 static void __gen5_ips_update(struct intel_ips *ips)
361 struct intel_uncore *uncore =
362 rps_to_uncore(container_of(ips, struct intel_rps, ips));
366 lockdep_assert_held(&mchdev_lock);
368 now = ktime_get_raw_ns();
369 dt = now - ips->last_time2;
370 do_div(dt, NSEC_PER_MSEC);
372 /* Don't divide by 0 */
376 count = intel_uncore_read(uncore, GFXEC);
377 delta = count - ips->last_count2;
379 ips->last_count2 = count;
380 ips->last_time2 = now;
382 /* More magic constants... */
383 ips->gfx_power = div_u64(delta * 1181, dt * 10);
386 static void gen5_rps_update(struct intel_rps *rps)
388 spin_lock_irq(&mchdev_lock);
389 __gen5_ips_update(&rps->ips);
390 spin_unlock_irq(&mchdev_lock);
393 static bool gen5_rps_set(struct intel_rps *rps, u8 val)
395 struct intel_uncore *uncore = rps_to_uncore(rps);
398 lockdep_assert_held(&mchdev_lock);
400 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
401 if (rgvswctl & MEMCTL_CMD_STS) {
402 DRM_DEBUG("gpu busy, RCS change rejected\n");
403 return false; /* still busy with another command */
406 /* Invert the frequency bin into an ips delay */
407 val = rps->max_freq - val;
408 val = rps->min_freq + val;
411 (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
412 (val << MEMCTL_FREQ_SHIFT) |
414 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
415 intel_uncore_posting_read16(uncore, MEMSWCTL);
417 rgvswctl |= MEMCTL_CMD_STS;
418 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
423 static unsigned long intel_pxfreq(u32 vidfreq)
425 int div = (vidfreq & 0x3f0000) >> 16;
426 int post = (vidfreq & 0x3000) >> 12;
427 int pre = (vidfreq & 0x7);
432 return div * 133333 / (pre << post);
435 static unsigned int init_emon(struct intel_uncore *uncore)
440 /* Disable to program */
441 intel_uncore_write(uncore, ECR, 0);
442 intel_uncore_posting_read(uncore, ECR);
444 /* Program energy weights for various events */
445 intel_uncore_write(uncore, SDEW, 0x15040d00);
446 intel_uncore_write(uncore, CSIEW0, 0x007f0000);
447 intel_uncore_write(uncore, CSIEW1, 0x1e220004);
448 intel_uncore_write(uncore, CSIEW2, 0x04000004);
450 for (i = 0; i < 5; i++)
451 intel_uncore_write(uncore, PEW(i), 0);
452 for (i = 0; i < 3; i++)
453 intel_uncore_write(uncore, DEW(i), 0);
455 /* Program P-state weights to account for frequency power adjustment */
456 for (i = 0; i < 16; i++) {
457 u32 pxvidfreq = intel_uncore_read(uncore, PXVFREQ(i));
458 unsigned int freq = intel_pxfreq(pxvidfreq);
460 (pxvidfreq & PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
463 val = vid * vid * freq / 1000 * 255;
464 val /= 127 * 127 * 900;
468 /* Render standby states get 0 weight */
472 for (i = 0; i < 4; i++) {
473 intel_uncore_write(uncore, PXW(i),
474 pxw[i * 4 + 0] << 24 |
475 pxw[i * 4 + 1] << 16 |
476 pxw[i * 4 + 2] << 8 |
477 pxw[i * 4 + 3] << 0);
480 /* Adjust magic regs to magic values (more experimental results) */
481 intel_uncore_write(uncore, OGW0, 0);
482 intel_uncore_write(uncore, OGW1, 0);
483 intel_uncore_write(uncore, EG0, 0x00007f00);
484 intel_uncore_write(uncore, EG1, 0x0000000e);
485 intel_uncore_write(uncore, EG2, 0x000e0000);
486 intel_uncore_write(uncore, EG3, 0x68000300);
487 intel_uncore_write(uncore, EG4, 0x42000000);
488 intel_uncore_write(uncore, EG5, 0x00140031);
489 intel_uncore_write(uncore, EG6, 0);
490 intel_uncore_write(uncore, EG7, 0);
492 for (i = 0; i < 8; i++)
493 intel_uncore_write(uncore, PXWL(i), 0);
495 /* Enable PMON + select events */
496 intel_uncore_write(uncore, ECR, 0x80000019);
498 return intel_uncore_read(uncore, LCFUSE02) & LCFUSE_HIV_MASK;
501 static bool gen5_rps_enable(struct intel_rps *rps)
503 struct intel_uncore *uncore = rps_to_uncore(rps);
507 spin_lock_irq(&mchdev_lock);
509 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
511 /* Enable temp reporting */
512 intel_uncore_write16(uncore, PMMISC,
513 intel_uncore_read16(uncore, PMMISC) | MCPPCE_EN);
514 intel_uncore_write16(uncore, TSC1,
515 intel_uncore_read16(uncore, TSC1) | TSE);
517 /* 100ms RC evaluation intervals */
518 intel_uncore_write(uncore, RCUPEI, 100000);
519 intel_uncore_write(uncore, RCDNEI, 100000);
521 /* Set max/min thresholds to 90ms and 80ms respectively */
522 intel_uncore_write(uncore, RCBMAXAVG, 90000);
523 intel_uncore_write(uncore, RCBMINAVG, 80000);
525 intel_uncore_write(uncore, MEMIHYST, 1);
527 /* Set up min, max, and cur for interrupt handling */
528 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
529 MEMMODE_FSTART_SHIFT;
531 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
532 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
534 intel_uncore_write(uncore,
536 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
538 intel_uncore_write(uncore, VIDSTART, vstart);
539 intel_uncore_posting_read(uncore, VIDSTART);
541 rgvmodectl |= MEMMODE_SWMODE_EN;
542 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
544 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
545 MEMCTL_CMD_STS) == 0, 10))
546 drm_err(&uncore->i915->drm,
547 "stuck trying to change perf mode\n");
550 gen5_rps_set(rps, rps->cur_freq);
552 rps->ips.last_count1 = intel_uncore_read(uncore, DMIEC);
553 rps->ips.last_count1 += intel_uncore_read(uncore, DDREC);
554 rps->ips.last_count1 += intel_uncore_read(uncore, CSIEC);
555 rps->ips.last_time1 = jiffies_to_msecs(jiffies);
557 rps->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
558 rps->ips.last_time2 = ktime_get_raw_ns();
560 spin_unlock_irq(&mchdev_lock);
562 rps->ips.corr = init_emon(uncore);
567 static void gen5_rps_disable(struct intel_rps *rps)
569 struct intel_uncore *uncore = rps_to_uncore(rps);
572 spin_lock_irq(&mchdev_lock);
574 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
576 /* Ack interrupts, disable EFC interrupt */
577 intel_uncore_write(uncore, MEMINTREN,
578 intel_uncore_read(uncore, MEMINTREN) &
579 ~MEMINT_EVAL_CHG_EN);
580 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
581 intel_uncore_write(uncore, DEIER,
582 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
583 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
584 intel_uncore_write(uncore, DEIMR,
585 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
587 /* Go back to the starting frequency */
588 gen5_rps_set(rps, rps->idle_freq);
590 rgvswctl |= MEMCTL_CMD_STS;
591 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
594 spin_unlock_irq(&mchdev_lock);
597 static u32 rps_limits(struct intel_rps *rps, u8 val)
602 * Only set the down limit when we've reached the lowest level to avoid
603 * getting more interrupts, otherwise leave this clear. This prevents a
604 * race in the hw when coming out of rc6: There's a tiny window where
605 * the hw runs at the minimal clock before selecting the desired
606 * frequency, if the down threshold expires in that window we will not
607 * receive a down interrupt.
609 if (INTEL_GEN(rps_to_i915(rps)) >= 9) {
610 limits = rps->max_freq_softlimit << 23;
611 if (val <= rps->min_freq_softlimit)
612 limits |= rps->min_freq_softlimit << 14;
614 limits = rps->max_freq_softlimit << 24;
615 if (val <= rps->min_freq_softlimit)
616 limits |= rps->min_freq_softlimit << 16;
622 static void rps_set_power(struct intel_rps *rps, int new_power)
624 struct intel_gt *gt = rps_to_gt(rps);
625 struct intel_uncore *uncore = gt->uncore;
626 u32 threshold_up = 0, threshold_down = 0; /* in % */
627 u32 ei_up = 0, ei_down = 0;
629 lockdep_assert_held(&rps->power.mutex);
631 if (new_power == rps->power.mode)
637 /* Note the units here are not exactly 1us, but 1280ns. */
655 /* When byt can survive without system hang with dynamic
656 * sw freq adjustments, this restriction can be lifted.
658 if (IS_VALLEYVIEW(gt->i915))
662 "changing power mode [%d], up %d%% @ %dus, down %d%% @ %dus\n",
663 new_power, threshold_up, ei_up, threshold_down, ei_down);
665 set(uncore, GEN6_RP_UP_EI,
666 intel_gt_ns_to_pm_interval(gt, ei_up * 1000));
667 set(uncore, GEN6_RP_UP_THRESHOLD,
668 intel_gt_ns_to_pm_interval(gt, ei_up * threshold_up * 10));
670 set(uncore, GEN6_RP_DOWN_EI,
671 intel_gt_ns_to_pm_interval(gt, ei_down * 1000));
672 set(uncore, GEN6_RP_DOWN_THRESHOLD,
673 intel_gt_ns_to_pm_interval(gt, ei_down * threshold_down * 10));
675 set(uncore, GEN6_RP_CONTROL,
676 (INTEL_GEN(gt->i915) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
677 GEN6_RP_MEDIA_HW_NORMAL_MODE |
678 GEN6_RP_MEDIA_IS_GFX |
680 GEN6_RP_UP_BUSY_AVG |
681 GEN6_RP_DOWN_IDLE_AVG);
684 rps->power.mode = new_power;
685 rps->power.up_threshold = threshold_up;
686 rps->power.down_threshold = threshold_down;
689 static void gen6_rps_set_thresholds(struct intel_rps *rps, u8 val)
693 new_power = rps->power.mode;
694 switch (rps->power.mode) {
696 if (val > rps->efficient_freq + 1 &&
702 if (val <= rps->efficient_freq &&
704 new_power = LOW_POWER;
705 else if (val >= rps->rp0_freq &&
707 new_power = HIGH_POWER;
711 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
716 /* Max/min bins are special */
717 if (val <= rps->min_freq_softlimit)
718 new_power = LOW_POWER;
719 if (val >= rps->max_freq_softlimit)
720 new_power = HIGH_POWER;
722 mutex_lock(&rps->power.mutex);
723 if (rps->power.interactive)
724 new_power = HIGH_POWER;
725 rps_set_power(rps, new_power);
726 mutex_unlock(&rps->power.mutex);
729 void intel_rps_mark_interactive(struct intel_rps *rps, bool interactive)
731 GT_TRACE(rps_to_gt(rps), "mark interactive: %s\n", yesno(interactive));
733 mutex_lock(&rps->power.mutex);
735 if (!rps->power.interactive++ && intel_rps_is_active(rps))
736 rps_set_power(rps, HIGH_POWER);
738 GEM_BUG_ON(!rps->power.interactive);
739 rps->power.interactive--;
741 mutex_unlock(&rps->power.mutex);
744 static int gen6_rps_set(struct intel_rps *rps, u8 val)
746 struct intel_uncore *uncore = rps_to_uncore(rps);
747 struct drm_i915_private *i915 = rps_to_i915(rps);
750 if (INTEL_GEN(i915) >= 9)
751 swreq = GEN9_FREQUENCY(val);
752 else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
753 swreq = HSW_FREQUENCY(val);
755 swreq = (GEN6_FREQUENCY(val) |
757 GEN6_AGGRESSIVE_TURBO);
758 set(uncore, GEN6_RPNSWREQ, swreq);
760 GT_TRACE(rps_to_gt(rps), "set val:%x, freq:%d, swreq:%x\n",
761 val, intel_gpu_freq(rps, val), swreq);
766 static int vlv_rps_set(struct intel_rps *rps, u8 val)
768 struct drm_i915_private *i915 = rps_to_i915(rps);
772 err = vlv_punit_write(i915, PUNIT_REG_GPU_FREQ_REQ, val);
775 GT_TRACE(rps_to_gt(rps), "set val:%x, freq:%d\n",
776 val, intel_gpu_freq(rps, val));
781 static int rps_set(struct intel_rps *rps, u8 val, bool update)
783 struct drm_i915_private *i915 = rps_to_i915(rps);
786 if (INTEL_GEN(i915) < 6)
789 if (val == rps->last_freq)
792 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
793 err = vlv_rps_set(rps, val);
795 err = gen6_rps_set(rps, val);
800 gen6_rps_set_thresholds(rps, val);
801 rps->last_freq = val;
806 void intel_rps_unpark(struct intel_rps *rps)
808 if (!intel_rps_is_enabled(rps))
811 GT_TRACE(rps_to_gt(rps), "unpark:%x\n", rps->cur_freq);
814 * Use the user's desired frequency as a guide, but for better
815 * performance, jump directly to RPe as our starting frequency.
817 mutex_lock(&rps->lock);
819 intel_rps_set_active(rps);
822 rps->min_freq_softlimit,
823 rps->max_freq_softlimit));
825 mutex_unlock(&rps->lock);
828 if (intel_rps_has_interrupts(rps))
829 rps_enable_interrupts(rps);
830 if (intel_rps_uses_timer(rps))
831 rps_start_timer(rps);
833 if (IS_GEN(rps_to_i915(rps), 5))
834 gen5_rps_update(rps);
837 void intel_rps_park(struct intel_rps *rps)
841 if (!intel_rps_clear_active(rps))
844 if (intel_rps_uses_timer(rps))
846 if (intel_rps_has_interrupts(rps))
847 rps_disable_interrupts(rps);
849 if (rps->last_freq <= rps->idle_freq)
853 * The punit delays the write of the frequency and voltage until it
854 * determines the GPU is awake. During normal usage we don't want to
855 * waste power changing the frequency if the GPU is sleeping (rc6).
856 * However, the GPU and driver is now idle and we do not want to delay
857 * switching to minimum voltage (reducing power whilst idle) as we do
858 * not expect to be woken in the near future and so must flush the
859 * change by waking the device.
861 * We choose to take the media powerwell (either would do to trick the
862 * punit into committing the voltage change) as that takes a lot less
863 * power than the render powerwell.
865 intel_uncore_forcewake_get(rps_to_uncore(rps), FORCEWAKE_MEDIA);
866 rps_set(rps, rps->idle_freq, false);
867 intel_uncore_forcewake_put(rps_to_uncore(rps), FORCEWAKE_MEDIA);
870 * Since we will try and restart from the previously requested
871 * frequency on unparking, treat this idle point as a downclock
872 * interrupt and reduce the frequency for resume. If we park/unpark
873 * more frequently than the rps worker can run, we will not respond
874 * to any EI and never see a change in frequency.
876 * (Note we accommodate Cherryview's limitation of only using an
877 * even bin by applying it to all.)
882 else /* CHV needs even encode values */
885 rps->cur_freq = max_t(int, rps->cur_freq + adj, rps->min_freq);
887 GT_TRACE(rps_to_gt(rps), "park:%x\n", rps->cur_freq);
890 void intel_rps_boost(struct i915_request *rq)
892 struct intel_rps *rps = &READ_ONCE(rq->engine)->gt->rps;
895 if (i915_request_signaled(rq) || !intel_rps_is_active(rps))
898 /* Serializes with i915_request_retire() */
899 spin_lock_irqsave(&rq->lock, flags);
900 if (!i915_request_has_waitboost(rq) &&
901 !dma_fence_is_signaled_locked(&rq->fence)) {
902 set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags);
904 GT_TRACE(rps_to_gt(rps), "boost fence:%llx:%llx\n",
905 rq->fence.context, rq->fence.seqno);
907 if (!atomic_fetch_inc(&rps->num_waiters) &&
908 READ_ONCE(rps->cur_freq) < rps->boost_freq)
909 schedule_work(&rps->work);
911 atomic_inc(&rps->boosts);
913 spin_unlock_irqrestore(&rq->lock, flags);
916 int intel_rps_set(struct intel_rps *rps, u8 val)
920 lockdep_assert_held(&rps->lock);
921 GEM_BUG_ON(val > rps->max_freq);
922 GEM_BUG_ON(val < rps->min_freq);
924 if (intel_rps_is_active(rps)) {
925 err = rps_set(rps, val, true);
930 * Make sure we continue to get interrupts
931 * until we hit the minimum or maximum frequencies.
933 if (intel_rps_has_interrupts(rps)) {
934 struct intel_uncore *uncore = rps_to_uncore(rps);
937 GEN6_RP_INTERRUPT_LIMITS, rps_limits(rps, val));
939 set(uncore, GEN6_PMINTRMSK, rps_pm_mask(rps, val));
947 static void gen6_rps_init(struct intel_rps *rps)
949 struct drm_i915_private *i915 = rps_to_i915(rps);
950 struct intel_uncore *uncore = rps_to_uncore(rps);
952 /* All of these values are in units of 50MHz */
954 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
955 if (IS_GEN9_LP(i915)) {
956 u32 rp_state_cap = intel_uncore_read(uncore, BXT_RP_STATE_CAP);
958 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
959 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
960 rps->min_freq = (rp_state_cap >> 0) & 0xff;
962 u32 rp_state_cap = intel_uncore_read(uncore, GEN6_RP_STATE_CAP);
964 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
965 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
966 rps->min_freq = (rp_state_cap >> 16) & 0xff;
969 /* hw_max = RP0 until we check for overclocking */
970 rps->max_freq = rps->rp0_freq;
972 rps->efficient_freq = rps->rp1_freq;
973 if (IS_HASWELL(i915) || IS_BROADWELL(i915) ||
974 IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
977 if (sandybridge_pcode_read(i915,
978 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
979 &ddcc_status, NULL) == 0)
980 rps->efficient_freq =
982 (ddcc_status >> 8) & 0xff,
987 if (IS_GEN9_BC(i915) || INTEL_GEN(i915) >= 10) {
988 /* Store the frequency values in 16.66 MHZ units, which is
989 * the natural hardware unit for SKL
991 rps->rp0_freq *= GEN9_FREQ_SCALER;
992 rps->rp1_freq *= GEN9_FREQ_SCALER;
993 rps->min_freq *= GEN9_FREQ_SCALER;
994 rps->max_freq *= GEN9_FREQ_SCALER;
995 rps->efficient_freq *= GEN9_FREQ_SCALER;
999 static bool rps_reset(struct intel_rps *rps)
1001 struct drm_i915_private *i915 = rps_to_i915(rps);
1004 rps->power.mode = -1;
1005 rps->last_freq = -1;
1007 if (rps_set(rps, rps->min_freq, true)) {
1008 drm_err(&i915->drm, "Failed to reset RPS to initial values\n");
1012 rps->cur_freq = rps->min_freq;
1016 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
1017 static bool gen9_rps_enable(struct intel_rps *rps)
1019 struct intel_gt *gt = rps_to_gt(rps);
1020 struct intel_uncore *uncore = gt->uncore;
1022 /* Program defaults and thresholds for RPS */
1023 if (IS_GEN(gt->i915, 9))
1024 intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
1025 GEN9_FREQUENCY(rps->rp1_freq));
1027 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 0xa);
1029 rps->pm_events = GEN6_PM_RP_UP_THRESHOLD | GEN6_PM_RP_DOWN_THRESHOLD;
1031 return rps_reset(rps);
1034 static bool gen8_rps_enable(struct intel_rps *rps)
1036 struct intel_uncore *uncore = rps_to_uncore(rps);
1038 intel_uncore_write_fw(uncore, GEN6_RC_VIDEO_FREQ,
1039 HSW_FREQUENCY(rps->rp1_freq));
1041 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1043 rps->pm_events = GEN6_PM_RP_UP_THRESHOLD | GEN6_PM_RP_DOWN_THRESHOLD;
1045 return rps_reset(rps);
1048 static bool gen6_rps_enable(struct intel_rps *rps)
1050 struct intel_uncore *uncore = rps_to_uncore(rps);
1052 /* Power down if completely idle for over 50ms */
1053 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 50000);
1054 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1056 rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
1057 GEN6_PM_RP_DOWN_THRESHOLD |
1058 GEN6_PM_RP_DOWN_TIMEOUT);
1060 return rps_reset(rps);
1063 static int chv_rps_max_freq(struct intel_rps *rps)
1065 struct drm_i915_private *i915 = rps_to_i915(rps);
1066 struct intel_gt *gt = rps_to_gt(rps);
1069 val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
1071 switch (gt->info.sseu.eu_total) {
1073 /* (2 * 4) config */
1074 val >>= FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT;
1077 /* (2 * 6) config */
1078 val >>= FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT;
1081 /* (2 * 8) config */
1083 /* Setting (2 * 8) Min RP0 for any other combination */
1084 val >>= FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT;
1088 return val & FB_GFX_FREQ_FUSE_MASK;
1091 static int chv_rps_rpe_freq(struct intel_rps *rps)
1093 struct drm_i915_private *i915 = rps_to_i915(rps);
1096 val = vlv_punit_read(i915, PUNIT_GPU_DUTYCYCLE_REG);
1097 val >>= PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT;
1099 return val & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
1102 static int chv_rps_guar_freq(struct intel_rps *rps)
1104 struct drm_i915_private *i915 = rps_to_i915(rps);
1107 val = vlv_punit_read(i915, FB_GFX_FMAX_AT_VMAX_FUSE);
1109 return val & FB_GFX_FREQ_FUSE_MASK;
1112 static u32 chv_rps_min_freq(struct intel_rps *rps)
1114 struct drm_i915_private *i915 = rps_to_i915(rps);
1117 val = vlv_punit_read(i915, FB_GFX_FMIN_AT_VMIN_FUSE);
1118 val >>= FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT;
1120 return val & FB_GFX_FREQ_FUSE_MASK;
1123 static bool chv_rps_enable(struct intel_rps *rps)
1125 struct intel_uncore *uncore = rps_to_uncore(rps);
1126 struct drm_i915_private *i915 = rps_to_i915(rps);
1129 /* 1: Program defaults and thresholds for RPS*/
1130 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1131 intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1132 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1133 intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1134 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1136 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1139 intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1140 GEN6_RP_MEDIA_HW_NORMAL_MODE |
1141 GEN6_RP_MEDIA_IS_GFX |
1143 GEN6_RP_UP_BUSY_AVG |
1144 GEN6_RP_DOWN_IDLE_AVG);
1146 rps->pm_events = (GEN6_PM_RP_UP_THRESHOLD |
1147 GEN6_PM_RP_DOWN_THRESHOLD |
1148 GEN6_PM_RP_DOWN_TIMEOUT);
1150 /* Setting Fixed Bias */
1151 vlv_punit_get(i915);
1153 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
1154 vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1156 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1158 vlv_punit_put(i915);
1160 /* RPS code assumes GPLL is used */
1161 drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1162 "GPLL not enabled\n");
1164 drm_dbg(&i915->drm, "GPLL enabled? %s\n", yesno(val & GPLLENABLE));
1165 drm_dbg(&i915->drm, "GPU status: 0x%08x\n", val);
1167 return rps_reset(rps);
1170 static int vlv_rps_guar_freq(struct intel_rps *rps)
1172 struct drm_i915_private *i915 = rps_to_i915(rps);
1175 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1177 rp1 = val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK;
1178 rp1 >>= FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
1183 static int vlv_rps_max_freq(struct intel_rps *rps)
1185 struct drm_i915_private *i915 = rps_to_i915(rps);
1188 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FREQ_FUSE);
1190 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
1192 rp0 = min_t(u32, rp0, 0xea);
1197 static int vlv_rps_rpe_freq(struct intel_rps *rps)
1199 struct drm_i915_private *i915 = rps_to_i915(rps);
1202 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
1203 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
1204 val = vlv_nc_read(i915, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
1205 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
1210 static int vlv_rps_min_freq(struct intel_rps *rps)
1212 struct drm_i915_private *i915 = rps_to_i915(rps);
1215 val = vlv_punit_read(i915, PUNIT_REG_GPU_LFM) & 0xff;
1217 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
1218 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
1219 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
1220 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
1221 * to make sure it matches what Punit accepts.
1223 return max_t(u32, val, 0xc0);
1226 static bool vlv_rps_enable(struct intel_rps *rps)
1228 struct intel_uncore *uncore = rps_to_uncore(rps);
1229 struct drm_i915_private *i915 = rps_to_i915(rps);
1232 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_TIMEOUT, 1000000);
1233 intel_uncore_write_fw(uncore, GEN6_RP_UP_THRESHOLD, 59400);
1234 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_THRESHOLD, 245000);
1235 intel_uncore_write_fw(uncore, GEN6_RP_UP_EI, 66000);
1236 intel_uncore_write_fw(uncore, GEN6_RP_DOWN_EI, 350000);
1238 intel_uncore_write_fw(uncore, GEN6_RP_IDLE_HYSTERSIS, 10);
1240 intel_uncore_write_fw(uncore, GEN6_RP_CONTROL,
1241 GEN6_RP_MEDIA_TURBO |
1242 GEN6_RP_MEDIA_HW_NORMAL_MODE |
1243 GEN6_RP_MEDIA_IS_GFX |
1245 GEN6_RP_UP_BUSY_AVG |
1246 GEN6_RP_DOWN_IDLE_CONT);
1248 /* WaGsvRC0ResidencyMethod:vlv */
1249 rps->pm_events = GEN6_PM_RP_UP_EI_EXPIRED;
1251 vlv_punit_get(i915);
1253 /* Setting Fixed Bias */
1254 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
1255 vlv_punit_write(i915, VLV_TURBO_SOC_OVERRIDE, val);
1257 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1259 vlv_punit_put(i915);
1261 /* RPS code assumes GPLL is used */
1262 drm_WARN_ONCE(&i915->drm, (val & GPLLENABLE) == 0,
1263 "GPLL not enabled\n");
1265 drm_dbg(&i915->drm, "GPLL enabled? %s\n", yesno(val & GPLLENABLE));
1266 drm_dbg(&i915->drm, "GPU status: 0x%08x\n", val);
1268 return rps_reset(rps);
1271 static unsigned long __ips_gfx_val(struct intel_ips *ips)
1273 struct intel_rps *rps = container_of(ips, typeof(*rps), ips);
1274 struct intel_uncore *uncore = rps_to_uncore(rps);
1275 unsigned long t, corr, state1, corr2, state2;
1278 lockdep_assert_held(&mchdev_lock);
1280 pxvid = intel_uncore_read(uncore, PXVFREQ(rps->cur_freq));
1281 pxvid = (pxvid >> 24) & 0x7f;
1282 ext_v = pvid_to_extvid(rps_to_i915(rps), pxvid);
1286 /* Revel in the empirically derived constants */
1288 /* Correction factor in 1/100000 units */
1289 t = ips_mch_val(uncore);
1291 corr = t * 2349 + 135940;
1293 corr = t * 964 + 29317;
1295 corr = t * 301 + 1004;
1297 corr = corr * 150142 * state1 / 10000 - 78642;
1299 corr2 = corr * ips->corr;
1301 state2 = corr2 * state1 / 10000;
1302 state2 /= 100; /* convert to mW */
1304 __gen5_ips_update(ips);
1306 return ips->gfx_power + state2;
1309 static bool has_busy_stats(struct intel_rps *rps)
1311 struct intel_engine_cs *engine;
1312 enum intel_engine_id id;
1314 for_each_engine(engine, rps_to_gt(rps), id) {
1315 if (!intel_engine_supports_stats(engine))
1322 void intel_rps_enable(struct intel_rps *rps)
1324 struct drm_i915_private *i915 = rps_to_i915(rps);
1325 struct intel_uncore *uncore = rps_to_uncore(rps);
1326 bool enabled = false;
1331 intel_gt_check_clock_frequency(rps_to_gt(rps));
1333 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1334 if (rps->max_freq <= rps->min_freq)
1335 /* leave disabled, no room for dynamic reclocking */;
1336 else if (IS_CHERRYVIEW(i915))
1337 enabled = chv_rps_enable(rps);
1338 else if (IS_VALLEYVIEW(i915))
1339 enabled = vlv_rps_enable(rps);
1340 else if (INTEL_GEN(i915) >= 9)
1341 enabled = gen9_rps_enable(rps);
1342 else if (INTEL_GEN(i915) >= 8)
1343 enabled = gen8_rps_enable(rps);
1344 else if (INTEL_GEN(i915) >= 6)
1345 enabled = gen6_rps_enable(rps);
1346 else if (IS_IRONLAKE_M(i915))
1347 enabled = gen5_rps_enable(rps);
1349 MISSING_CASE(INTEL_GEN(i915));
1350 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
1354 GT_TRACE(rps_to_gt(rps),
1355 "min:%x, max:%x, freq:[%d, %d]\n",
1356 rps->min_freq, rps->max_freq,
1357 intel_gpu_freq(rps, rps->min_freq),
1358 intel_gpu_freq(rps, rps->max_freq));
1360 GEM_BUG_ON(rps->max_freq < rps->min_freq);
1361 GEM_BUG_ON(rps->idle_freq > rps->max_freq);
1363 GEM_BUG_ON(rps->efficient_freq < rps->min_freq);
1364 GEM_BUG_ON(rps->efficient_freq > rps->max_freq);
1366 if (has_busy_stats(rps))
1367 intel_rps_set_timer(rps);
1368 else if (INTEL_GEN(i915) >= 6)
1369 intel_rps_set_interrupts(rps);
1371 /* Ironlake currently uses intel_ips.ko */ {}
1373 intel_rps_set_enabled(rps);
1376 static void gen6_rps_disable(struct intel_rps *rps)
1378 set(rps_to_uncore(rps), GEN6_RP_CONTROL, 0);
1381 void intel_rps_disable(struct intel_rps *rps)
1383 struct drm_i915_private *i915 = rps_to_i915(rps);
1385 intel_rps_clear_enabled(rps);
1386 intel_rps_clear_interrupts(rps);
1387 intel_rps_clear_timer(rps);
1389 if (INTEL_GEN(i915) >= 6)
1390 gen6_rps_disable(rps);
1391 else if (IS_IRONLAKE_M(i915))
1392 gen5_rps_disable(rps);
1395 static int byt_gpu_freq(struct intel_rps *rps, int val)
1399 * Slow = Fast = GPLL ref * N
1401 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
1404 static int byt_freq_opcode(struct intel_rps *rps, int val)
1406 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
1409 static int chv_gpu_freq(struct intel_rps *rps, int val)
1413 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
1415 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
1418 static int chv_freq_opcode(struct intel_rps *rps, int val)
1420 /* CHV needs even values */
1421 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
1424 int intel_gpu_freq(struct intel_rps *rps, int val)
1426 struct drm_i915_private *i915 = rps_to_i915(rps);
1428 if (INTEL_GEN(i915) >= 9)
1429 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
1431 else if (IS_CHERRYVIEW(i915))
1432 return chv_gpu_freq(rps, val);
1433 else if (IS_VALLEYVIEW(i915))
1434 return byt_gpu_freq(rps, val);
1436 return val * GT_FREQUENCY_MULTIPLIER;
1439 int intel_freq_opcode(struct intel_rps *rps, int val)
1441 struct drm_i915_private *i915 = rps_to_i915(rps);
1443 if (INTEL_GEN(i915) >= 9)
1444 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
1445 GT_FREQUENCY_MULTIPLIER);
1446 else if (IS_CHERRYVIEW(i915))
1447 return chv_freq_opcode(rps, val);
1448 else if (IS_VALLEYVIEW(i915))
1449 return byt_freq_opcode(rps, val);
1451 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
1454 static void vlv_init_gpll_ref_freq(struct intel_rps *rps)
1456 struct drm_i915_private *i915 = rps_to_i915(rps);
1458 rps->gpll_ref_freq =
1459 vlv_get_cck_clock(i915, "GPLL ref",
1460 CCK_GPLL_CLOCK_CONTROL,
1463 drm_dbg(&i915->drm, "GPLL reference freq: %d kHz\n",
1464 rps->gpll_ref_freq);
1467 static void vlv_rps_init(struct intel_rps *rps)
1469 struct drm_i915_private *i915 = rps_to_i915(rps);
1472 vlv_iosf_sb_get(i915,
1473 BIT(VLV_IOSF_SB_PUNIT) |
1474 BIT(VLV_IOSF_SB_NC) |
1475 BIT(VLV_IOSF_SB_CCK));
1477 vlv_init_gpll_ref_freq(rps);
1479 val = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1480 switch ((val >> 6) & 3) {
1483 i915->mem_freq = 800;
1486 i915->mem_freq = 1066;
1489 i915->mem_freq = 1333;
1492 drm_dbg(&i915->drm, "DDR speed: %d MHz\n", i915->mem_freq);
1494 rps->max_freq = vlv_rps_max_freq(rps);
1495 rps->rp0_freq = rps->max_freq;
1496 drm_dbg(&i915->drm, "max GPU freq: %d MHz (%u)\n",
1497 intel_gpu_freq(rps, rps->max_freq), rps->max_freq);
1499 rps->efficient_freq = vlv_rps_rpe_freq(rps);
1500 drm_dbg(&i915->drm, "RPe GPU freq: %d MHz (%u)\n",
1501 intel_gpu_freq(rps, rps->efficient_freq), rps->efficient_freq);
1503 rps->rp1_freq = vlv_rps_guar_freq(rps);
1504 drm_dbg(&i915->drm, "RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
1505 intel_gpu_freq(rps, rps->rp1_freq), rps->rp1_freq);
1507 rps->min_freq = vlv_rps_min_freq(rps);
1508 drm_dbg(&i915->drm, "min GPU freq: %d MHz (%u)\n",
1509 intel_gpu_freq(rps, rps->min_freq), rps->min_freq);
1511 vlv_iosf_sb_put(i915,
1512 BIT(VLV_IOSF_SB_PUNIT) |
1513 BIT(VLV_IOSF_SB_NC) |
1514 BIT(VLV_IOSF_SB_CCK));
1517 static void chv_rps_init(struct intel_rps *rps)
1519 struct drm_i915_private *i915 = rps_to_i915(rps);
1522 vlv_iosf_sb_get(i915,
1523 BIT(VLV_IOSF_SB_PUNIT) |
1524 BIT(VLV_IOSF_SB_NC) |
1525 BIT(VLV_IOSF_SB_CCK));
1527 vlv_init_gpll_ref_freq(rps);
1529 val = vlv_cck_read(i915, CCK_FUSE_REG);
1531 switch ((val >> 2) & 0x7) {
1533 i915->mem_freq = 2000;
1536 i915->mem_freq = 1600;
1539 drm_dbg(&i915->drm, "DDR speed: %d MHz\n", i915->mem_freq);
1541 rps->max_freq = chv_rps_max_freq(rps);
1542 rps->rp0_freq = rps->max_freq;
1543 drm_dbg(&i915->drm, "max GPU freq: %d MHz (%u)\n",
1544 intel_gpu_freq(rps, rps->max_freq), rps->max_freq);
1546 rps->efficient_freq = chv_rps_rpe_freq(rps);
1547 drm_dbg(&i915->drm, "RPe GPU freq: %d MHz (%u)\n",
1548 intel_gpu_freq(rps, rps->efficient_freq), rps->efficient_freq);
1550 rps->rp1_freq = chv_rps_guar_freq(rps);
1551 drm_dbg(&i915->drm, "RP1(Guar) GPU freq: %d MHz (%u)\n",
1552 intel_gpu_freq(rps, rps->rp1_freq), rps->rp1_freq);
1554 rps->min_freq = chv_rps_min_freq(rps);
1555 drm_dbg(&i915->drm, "min GPU freq: %d MHz (%u)\n",
1556 intel_gpu_freq(rps, rps->min_freq), rps->min_freq);
1558 vlv_iosf_sb_put(i915,
1559 BIT(VLV_IOSF_SB_PUNIT) |
1560 BIT(VLV_IOSF_SB_NC) |
1561 BIT(VLV_IOSF_SB_CCK));
1563 drm_WARN_ONCE(&i915->drm, (rps->max_freq | rps->efficient_freq |
1564 rps->rp1_freq | rps->min_freq) & 1,
1565 "Odd GPU freq values\n");
1568 static void vlv_c0_read(struct intel_uncore *uncore, struct intel_rps_ei *ei)
1570 ei->ktime = ktime_get_raw();
1571 ei->render_c0 = intel_uncore_read(uncore, VLV_RENDER_C0_COUNT);
1572 ei->media_c0 = intel_uncore_read(uncore, VLV_MEDIA_C0_COUNT);
1575 static u32 vlv_wa_c0_ei(struct intel_rps *rps, u32 pm_iir)
1577 struct intel_uncore *uncore = rps_to_uncore(rps);
1578 const struct intel_rps_ei *prev = &rps->ei;
1579 struct intel_rps_ei now;
1582 if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
1585 vlv_c0_read(uncore, &now);
1591 time = ktime_us_delta(now.ktime, prev->ktime);
1593 time *= rps_to_i915(rps)->czclk_freq;
1595 /* Workload can be split between render + media,
1596 * e.g. SwapBuffers being blitted in X after being rendered in
1597 * mesa. To account for this we need to combine both engines
1598 * into our activity counter.
1600 render = now.render_c0 - prev->render_c0;
1601 media = now.media_c0 - prev->media_c0;
1602 c0 = max(render, media);
1603 c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1605 if (c0 > time * rps->power.up_threshold)
1606 events = GEN6_PM_RP_UP_THRESHOLD;
1607 else if (c0 < time * rps->power.down_threshold)
1608 events = GEN6_PM_RP_DOWN_THRESHOLD;
1615 static void rps_work(struct work_struct *work)
1617 struct intel_rps *rps = container_of(work, typeof(*rps), work);
1618 struct intel_gt *gt = rps_to_gt(rps);
1619 struct drm_i915_private *i915 = rps_to_i915(rps);
1620 bool client_boost = false;
1621 int new_freq, adj, min, max;
1624 spin_lock_irq(>->irq_lock);
1625 pm_iir = fetch_and_zero(&rps->pm_iir) & rps->pm_events;
1626 client_boost = atomic_read(&rps->num_waiters);
1627 spin_unlock_irq(>->irq_lock);
1629 /* Make sure we didn't queue anything we're not going to process. */
1630 if (!pm_iir && !client_boost)
1633 mutex_lock(&rps->lock);
1634 if (!intel_rps_is_active(rps)) {
1635 mutex_unlock(&rps->lock);
1639 pm_iir |= vlv_wa_c0_ei(rps, pm_iir);
1641 adj = rps->last_adj;
1642 new_freq = rps->cur_freq;
1643 min = rps->min_freq_softlimit;
1644 max = rps->max_freq_softlimit;
1646 max = rps->max_freq;
1649 "pm_iir:%x, client_boost:%s, last:%d, cur:%x, min:%x, max:%x\n",
1650 pm_iir, yesno(client_boost),
1651 adj, new_freq, min, max);
1653 if (client_boost && new_freq < rps->boost_freq) {
1654 new_freq = rps->boost_freq;
1656 } else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1659 else /* CHV needs even encode values */
1660 adj = IS_CHERRYVIEW(gt->i915) ? 2 : 1;
1662 if (new_freq >= rps->max_freq_softlimit)
1664 } else if (client_boost) {
1666 } else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1667 if (rps->cur_freq > rps->efficient_freq)
1668 new_freq = rps->efficient_freq;
1669 else if (rps->cur_freq > rps->min_freq_softlimit)
1670 new_freq = rps->min_freq_softlimit;
1672 } else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1675 else /* CHV needs even encode values */
1676 adj = IS_CHERRYVIEW(gt->i915) ? -2 : -1;
1678 if (new_freq <= rps->min_freq_softlimit)
1680 } else { /* unknown event */
1685 * sysfs frequency limits may have snuck in while
1686 * servicing the interrupt
1689 new_freq = clamp_t(int, new_freq, min, max);
1691 if (intel_rps_set(rps, new_freq)) {
1692 drm_dbg(&i915->drm, "Failed to set new GPU frequency\n");
1695 rps->last_adj = adj;
1697 mutex_unlock(&rps->lock);
1700 spin_lock_irq(>->irq_lock);
1701 gen6_gt_pm_unmask_irq(gt, rps->pm_events);
1702 spin_unlock_irq(>->irq_lock);
1705 void gen11_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1707 struct intel_gt *gt = rps_to_gt(rps);
1708 const u32 events = rps->pm_events & pm_iir;
1710 lockdep_assert_held(>->irq_lock);
1712 if (unlikely(!events))
1715 GT_TRACE(gt, "irq events:%x\n", events);
1717 gen6_gt_pm_mask_irq(gt, events);
1719 rps->pm_iir |= events;
1720 schedule_work(&rps->work);
1723 void gen6_rps_irq_handler(struct intel_rps *rps, u32 pm_iir)
1725 struct intel_gt *gt = rps_to_gt(rps);
1728 events = pm_iir & rps->pm_events;
1730 spin_lock(>->irq_lock);
1732 GT_TRACE(gt, "irq events:%x\n", events);
1734 gen6_gt_pm_mask_irq(gt, events);
1735 rps->pm_iir |= events;
1737 schedule_work(&rps->work);
1738 spin_unlock(>->irq_lock);
1741 if (INTEL_GEN(gt->i915) >= 8)
1744 if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1745 intel_engine_signal_breadcrumbs(gt->engine[VECS0]);
1747 if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1748 DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
1751 void gen5_rps_irq_handler(struct intel_rps *rps)
1753 struct intel_uncore *uncore = rps_to_uncore(rps);
1754 u32 busy_up, busy_down, max_avg, min_avg;
1757 spin_lock(&mchdev_lock);
1759 intel_uncore_write16(uncore,
1761 intel_uncore_read(uncore, MEMINTRSTS));
1763 intel_uncore_write16(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
1764 busy_up = intel_uncore_read(uncore, RCPREVBSYTUPAVG);
1765 busy_down = intel_uncore_read(uncore, RCPREVBSYTDNAVG);
1766 max_avg = intel_uncore_read(uncore, RCBMAXAVG);
1767 min_avg = intel_uncore_read(uncore, RCBMINAVG);
1769 /* Handle RCS change request from hw */
1770 new_freq = rps->cur_freq;
1771 if (busy_up > max_avg)
1773 else if (busy_down < min_avg)
1775 new_freq = clamp(new_freq,
1776 rps->min_freq_softlimit,
1777 rps->max_freq_softlimit);
1779 if (new_freq != rps->cur_freq && gen5_rps_set(rps, new_freq))
1780 rps->cur_freq = new_freq;
1782 spin_unlock(&mchdev_lock);
1785 void intel_rps_init_early(struct intel_rps *rps)
1787 mutex_init(&rps->lock);
1788 mutex_init(&rps->power.mutex);
1790 INIT_WORK(&rps->work, rps_work);
1791 timer_setup(&rps->timer, rps_timer, 0);
1793 atomic_set(&rps->num_waiters, 0);
1796 void intel_rps_init(struct intel_rps *rps)
1798 struct drm_i915_private *i915 = rps_to_i915(rps);
1800 if (IS_CHERRYVIEW(i915))
1802 else if (IS_VALLEYVIEW(i915))
1804 else if (INTEL_GEN(i915) >= 6)
1806 else if (IS_IRONLAKE_M(i915))
1809 /* Derive initial user preferences/limits from the hardware limits */
1810 rps->max_freq_softlimit = rps->max_freq;
1811 rps->min_freq_softlimit = rps->min_freq;
1813 /* After setting max-softlimit, find the overclock max freq */
1814 if (IS_GEN(i915, 6) || IS_IVYBRIDGE(i915) || IS_HASWELL(i915)) {
1817 sandybridge_pcode_read(i915, GEN6_READ_OC_PARAMS,
1819 if (params & BIT(31)) { /* OC supported */
1821 "Overclocking supported, max: %dMHz, overclock: %dMHz\n",
1822 (rps->max_freq & 0xff) * 50,
1823 (params & 0xff) * 50);
1824 rps->max_freq = params & 0xff;
1828 /* Finally allow us to boost to max by default */
1829 rps->boost_freq = rps->max_freq;
1830 rps->idle_freq = rps->min_freq;
1832 /* Start in the middle, from here we will autotune based on workload */
1833 rps->cur_freq = rps->efficient_freq;
1835 rps->pm_intrmsk_mbz = 0;
1838 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
1839 * if GEN6_PM_UP_EI_EXPIRED is masked.
1841 * TODO: verify if this can be reproduced on VLV,CHV.
1843 if (INTEL_GEN(i915) <= 7)
1844 rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
1846 if (INTEL_GEN(i915) >= 8 && INTEL_GEN(i915) < 11)
1847 rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1850 void intel_rps_sanitize(struct intel_rps *rps)
1852 if (INTEL_GEN(rps_to_i915(rps)) >= 6)
1853 rps_disable_interrupts(rps);
1856 u32 intel_rps_get_cagf(struct intel_rps *rps, u32 rpstat)
1858 struct drm_i915_private *i915 = rps_to_i915(rps);
1861 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1862 cagf = (rpstat >> 8) & 0xff;
1863 else if (INTEL_GEN(i915) >= 9)
1864 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
1865 else if (IS_HASWELL(i915) || IS_BROADWELL(i915))
1866 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
1868 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
1873 static u32 read_cagf(struct intel_rps *rps)
1875 struct drm_i915_private *i915 = rps_to_i915(rps);
1878 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
1879 vlv_punit_get(i915);
1880 freq = vlv_punit_read(i915, PUNIT_REG_GPU_FREQ_STS);
1881 vlv_punit_put(i915);
1883 freq = intel_uncore_read(rps_to_uncore(rps), GEN6_RPSTAT1);
1886 return intel_rps_get_cagf(rps, freq);
1889 u32 intel_rps_read_actual_frequency(struct intel_rps *rps)
1891 struct intel_runtime_pm *rpm = rps_to_uncore(rps)->rpm;
1892 intel_wakeref_t wakeref;
1895 with_intel_runtime_pm_if_in_use(rpm, wakeref)
1896 freq = intel_gpu_freq(rps, read_cagf(rps));
1901 /* External interface for intel_ips.ko */
1903 static struct drm_i915_private __rcu *ips_mchdev;
1906 * Tells the intel_ips driver that the i915 driver is now loaded, if
1907 * IPS got loaded first.
1909 * This awkward dance is so that neither module has to depend on the
1910 * other in order for IPS to do the appropriate communication of
1911 * GPU turbo limits to i915.
1914 ips_ping_for_i915_load(void)
1918 link = symbol_get(ips_link_to_i915_driver);
1921 symbol_put(ips_link_to_i915_driver);
1925 void intel_rps_driver_register(struct intel_rps *rps)
1927 struct intel_gt *gt = rps_to_gt(rps);
1930 * We only register the i915 ips part with intel-ips once everything is
1931 * set up, to avoid intel-ips sneaking in and reading bogus values.
1933 if (IS_GEN(gt->i915, 5)) {
1934 GEM_BUG_ON(ips_mchdev);
1935 rcu_assign_pointer(ips_mchdev, gt->i915);
1936 ips_ping_for_i915_load();
1940 void intel_rps_driver_unregister(struct intel_rps *rps)
1942 if (rcu_access_pointer(ips_mchdev) == rps_to_i915(rps))
1943 rcu_assign_pointer(ips_mchdev, NULL);
1946 static struct drm_i915_private *mchdev_get(void)
1948 struct drm_i915_private *i915;
1951 i915 = rcu_dereference(ips_mchdev);
1952 if (!kref_get_unless_zero(&i915->drm.ref))
1960 * i915_read_mch_val - return value for IPS use
1962 * Calculate and return a value for the IPS driver to use when deciding whether
1963 * we have thermal and power headroom to increase CPU or GPU power budget.
1965 unsigned long i915_read_mch_val(void)
1967 struct drm_i915_private *i915;
1968 unsigned long chipset_val = 0;
1969 unsigned long graphics_val = 0;
1970 intel_wakeref_t wakeref;
1972 i915 = mchdev_get();
1976 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
1977 struct intel_ips *ips = &i915->gt.rps.ips;
1979 spin_lock_irq(&mchdev_lock);
1980 chipset_val = __ips_chipset_val(ips);
1981 graphics_val = __ips_gfx_val(ips);
1982 spin_unlock_irq(&mchdev_lock);
1985 drm_dev_put(&i915->drm);
1986 return chipset_val + graphics_val;
1988 EXPORT_SYMBOL_GPL(i915_read_mch_val);
1991 * i915_gpu_raise - raise GPU frequency limit
1993 * Raise the limit; IPS indicates we have thermal headroom.
1995 bool i915_gpu_raise(void)
1997 struct drm_i915_private *i915;
1998 struct intel_rps *rps;
2000 i915 = mchdev_get();
2004 rps = &i915->gt.rps;
2006 spin_lock_irq(&mchdev_lock);
2007 if (rps->max_freq_softlimit < rps->max_freq)
2008 rps->max_freq_softlimit++;
2009 spin_unlock_irq(&mchdev_lock);
2011 drm_dev_put(&i915->drm);
2014 EXPORT_SYMBOL_GPL(i915_gpu_raise);
2017 * i915_gpu_lower - lower GPU frequency limit
2019 * IPS indicates we're close to a thermal limit, so throttle back the GPU
2020 * frequency maximum.
2022 bool i915_gpu_lower(void)
2024 struct drm_i915_private *i915;
2025 struct intel_rps *rps;
2027 i915 = mchdev_get();
2031 rps = &i915->gt.rps;
2033 spin_lock_irq(&mchdev_lock);
2034 if (rps->max_freq_softlimit > rps->min_freq)
2035 rps->max_freq_softlimit--;
2036 spin_unlock_irq(&mchdev_lock);
2038 drm_dev_put(&i915->drm);
2041 EXPORT_SYMBOL_GPL(i915_gpu_lower);
2044 * i915_gpu_busy - indicate GPU business to IPS
2046 * Tell the IPS driver whether or not the GPU is busy.
2048 bool i915_gpu_busy(void)
2050 struct drm_i915_private *i915;
2053 i915 = mchdev_get();
2057 ret = i915->gt.awake;
2059 drm_dev_put(&i915->drm);
2062 EXPORT_SYMBOL_GPL(i915_gpu_busy);
2065 * i915_gpu_turbo_disable - disable graphics turbo
2067 * Disable graphics turbo by resetting the max frequency and setting the
2068 * current frequency to the default.
2070 bool i915_gpu_turbo_disable(void)
2072 struct drm_i915_private *i915;
2073 struct intel_rps *rps;
2076 i915 = mchdev_get();
2080 rps = &i915->gt.rps;
2082 spin_lock_irq(&mchdev_lock);
2083 rps->max_freq_softlimit = rps->min_freq;
2084 ret = gen5_rps_set(&i915->gt.rps, rps->min_freq);
2085 spin_unlock_irq(&mchdev_lock);
2087 drm_dev_put(&i915->drm);
2090 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
2092 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2093 #include "selftest_rps.c"