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