2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
28 #include <linux/cpufreq.h>
30 #include "intel_drv.h"
31 #include "../../../platform/x86/intel_ips.h"
32 #include <linux/module.h>
35 * RC6 is a special power stage which allows the GPU to enter an very
36 * low-voltage mode when idle, using down to 0V while at this stage. This
37 * stage is entered automatically when the GPU is idle when RC6 support is
38 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
40 * There are different RC6 modes available in Intel GPU, which differentiate
41 * among each other with the latency required to enter and leave RC6 and
42 * voltage consumed by the GPU in different states.
44 * The combination of the following flags define which states GPU is allowed
45 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
46 * RC6pp is deepest RC6. Their support by hardware varies according to the
47 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
48 * which brings the most power savings; deeper states save more power, but
49 * require higher latency to switch to and wake up.
51 #define INTEL_RC6_ENABLE (1<<0)
52 #define INTEL_RC6p_ENABLE (1<<1)
53 #define INTEL_RC6pp_ENABLE (1<<2)
55 static void gen9_init_clock_gating(struct drm_device *dev)
57 struct drm_i915_private *dev_priv = dev->dev_private;
59 /* WaEnableLbsSlaRetryTimerDecrement:skl */
60 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
61 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
63 /* WaDisableKillLogic:bxt,skl */
64 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
68 static void skl_init_clock_gating(struct drm_device *dev)
70 struct drm_i915_private *dev_priv = dev->dev_private;
72 gen9_init_clock_gating(dev);
74 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
75 /* WaDisableHDCInvalidation:skl */
76 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
77 BDW_DISABLE_HDC_INVALIDATION);
79 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
80 I915_WRITE(FF_SLICE_CS_CHICKEN2,
81 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
84 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
85 * involving this register should also be added to WA batch as required.
87 if (INTEL_REVID(dev) <= SKL_REVID_E0)
88 /* WaDisableLSQCROPERFforOCL:skl */
89 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
90 GEN8_LQSC_RO_PERF_DIS);
92 /* WaEnableGapsTsvCreditFix:skl */
93 if (IS_SKYLAKE(dev) && (INTEL_REVID(dev) >= SKL_REVID_C0)) {
94 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
95 GEN9_GAPS_TSV_CREDIT_DISABLE));
99 static void bxt_init_clock_gating(struct drm_device *dev)
101 struct drm_i915_private *dev_priv = dev->dev_private;
103 gen9_init_clock_gating(dev);
105 /* WaDisableSDEUnitClockGating:bxt */
106 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
107 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
111 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
113 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
114 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
116 /* WaStoreMultiplePTEenable:bxt */
117 /* This is a requirement according to Hardware specification */
118 if (INTEL_REVID(dev) == BXT_REVID_A0)
119 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
121 /* WaSetClckGatingDisableMedia:bxt */
122 if (INTEL_REVID(dev) == BXT_REVID_A0) {
123 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
124 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
128 static void i915_pineview_get_mem_freq(struct drm_device *dev)
130 struct drm_i915_private *dev_priv = dev->dev_private;
133 tmp = I915_READ(CLKCFG);
135 switch (tmp & CLKCFG_FSB_MASK) {
137 dev_priv->fsb_freq = 533; /* 133*4 */
140 dev_priv->fsb_freq = 800; /* 200*4 */
143 dev_priv->fsb_freq = 667; /* 167*4 */
146 dev_priv->fsb_freq = 400; /* 100*4 */
150 switch (tmp & CLKCFG_MEM_MASK) {
152 dev_priv->mem_freq = 533;
155 dev_priv->mem_freq = 667;
158 dev_priv->mem_freq = 800;
162 /* detect pineview DDR3 setting */
163 tmp = I915_READ(CSHRDDR3CTL);
164 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
167 static void i915_ironlake_get_mem_freq(struct drm_device *dev)
169 struct drm_i915_private *dev_priv = dev->dev_private;
172 ddrpll = I915_READ16(DDRMPLL1);
173 csipll = I915_READ16(CSIPLL0);
175 switch (ddrpll & 0xff) {
177 dev_priv->mem_freq = 800;
180 dev_priv->mem_freq = 1066;
183 dev_priv->mem_freq = 1333;
186 dev_priv->mem_freq = 1600;
189 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
191 dev_priv->mem_freq = 0;
195 dev_priv->ips.r_t = dev_priv->mem_freq;
197 switch (csipll & 0x3ff) {
199 dev_priv->fsb_freq = 3200;
202 dev_priv->fsb_freq = 3733;
205 dev_priv->fsb_freq = 4266;
208 dev_priv->fsb_freq = 4800;
211 dev_priv->fsb_freq = 5333;
214 dev_priv->fsb_freq = 5866;
217 dev_priv->fsb_freq = 6400;
220 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
222 dev_priv->fsb_freq = 0;
226 if (dev_priv->fsb_freq == 3200) {
227 dev_priv->ips.c_m = 0;
228 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
229 dev_priv->ips.c_m = 1;
231 dev_priv->ips.c_m = 2;
235 static const struct cxsr_latency cxsr_latency_table[] = {
236 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
237 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
238 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
239 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
240 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
242 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
243 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
244 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
245 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
246 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
248 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
249 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
250 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
251 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
252 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
254 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
255 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
256 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
257 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
258 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
260 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
261 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
262 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
263 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
264 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
266 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
267 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
268 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
269 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
270 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
273 static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
278 const struct cxsr_latency *latency;
281 if (fsb == 0 || mem == 0)
284 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
285 latency = &cxsr_latency_table[i];
286 if (is_desktop == latency->is_desktop &&
287 is_ddr3 == latency->is_ddr3 &&
288 fsb == latency->fsb_freq && mem == latency->mem_freq)
292 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
297 static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
301 mutex_lock(&dev_priv->rps.hw_lock);
303 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
305 val &= ~FORCE_DDR_HIGH_FREQ;
307 val |= FORCE_DDR_HIGH_FREQ;
308 val &= ~FORCE_DDR_LOW_FREQ;
309 val |= FORCE_DDR_FREQ_REQ_ACK;
310 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
312 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
313 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
314 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
316 mutex_unlock(&dev_priv->rps.hw_lock);
319 static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
323 mutex_lock(&dev_priv->rps.hw_lock);
325 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
327 val |= DSP_MAXFIFO_PM5_ENABLE;
329 val &= ~DSP_MAXFIFO_PM5_ENABLE;
330 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
332 mutex_unlock(&dev_priv->rps.hw_lock);
335 #define FW_WM(value, plane) \
336 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
338 void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
340 struct drm_device *dev = dev_priv->dev;
343 if (IS_VALLEYVIEW(dev)) {
344 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
345 POSTING_READ(FW_BLC_SELF_VLV);
346 dev_priv->wm.vlv.cxsr = enable;
347 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
348 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
349 POSTING_READ(FW_BLC_SELF);
350 } else if (IS_PINEVIEW(dev)) {
351 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
352 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
353 I915_WRITE(DSPFW3, val);
354 POSTING_READ(DSPFW3);
355 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
356 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
357 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
358 I915_WRITE(FW_BLC_SELF, val);
359 POSTING_READ(FW_BLC_SELF);
360 } else if (IS_I915GM(dev)) {
361 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
362 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
363 I915_WRITE(INSTPM, val);
364 POSTING_READ(INSTPM);
369 DRM_DEBUG_KMS("memory self-refresh is %s\n",
370 enable ? "enabled" : "disabled");
375 * Latency for FIFO fetches is dependent on several factors:
376 * - memory configuration (speed, channels)
378 * - current MCH state
379 * It can be fairly high in some situations, so here we assume a fairly
380 * pessimal value. It's a tradeoff between extra memory fetches (if we
381 * set this value too high, the FIFO will fetch frequently to stay full)
382 * and power consumption (set it too low to save power and we might see
383 * FIFO underruns and display "flicker").
385 * A value of 5us seems to be a good balance; safe for very low end
386 * platforms but not overly aggressive on lower latency configs.
388 static const int pessimal_latency_ns = 5000;
390 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
391 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
393 static int vlv_get_fifo_size(struct drm_device *dev,
394 enum pipe pipe, int plane)
396 struct drm_i915_private *dev_priv = dev->dev_private;
397 int sprite0_start, sprite1_start, size;
400 uint32_t dsparb, dsparb2, dsparb3;
402 dsparb = I915_READ(DSPARB);
403 dsparb2 = I915_READ(DSPARB2);
404 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
405 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
408 dsparb = I915_READ(DSPARB);
409 dsparb2 = I915_READ(DSPARB2);
410 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
411 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
414 dsparb2 = I915_READ(DSPARB2);
415 dsparb3 = I915_READ(DSPARB3);
416 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
417 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
425 size = sprite0_start;
428 size = sprite1_start - sprite0_start;
431 size = 512 - 1 - sprite1_start;
437 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
438 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
439 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
445 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
447 struct drm_i915_private *dev_priv = dev->dev_private;
448 uint32_t dsparb = I915_READ(DSPARB);
451 size = dsparb & 0x7f;
453 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
455 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
456 plane ? "B" : "A", size);
461 static int i830_get_fifo_size(struct drm_device *dev, int plane)
463 struct drm_i915_private *dev_priv = dev->dev_private;
464 uint32_t dsparb = I915_READ(DSPARB);
467 size = dsparb & 0x1ff;
469 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
470 size >>= 1; /* Convert to cachelines */
472 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
473 plane ? "B" : "A", size);
478 static int i845_get_fifo_size(struct drm_device *dev, int plane)
480 struct drm_i915_private *dev_priv = dev->dev_private;
481 uint32_t dsparb = I915_READ(DSPARB);
484 size = dsparb & 0x7f;
485 size >>= 2; /* Convert to cachelines */
487 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
494 /* Pineview has different values for various configs */
495 static const struct intel_watermark_params pineview_display_wm = {
496 .fifo_size = PINEVIEW_DISPLAY_FIFO,
497 .max_wm = PINEVIEW_MAX_WM,
498 .default_wm = PINEVIEW_DFT_WM,
499 .guard_size = PINEVIEW_GUARD_WM,
500 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
502 static const struct intel_watermark_params pineview_display_hplloff_wm = {
503 .fifo_size = PINEVIEW_DISPLAY_FIFO,
504 .max_wm = PINEVIEW_MAX_WM,
505 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
506 .guard_size = PINEVIEW_GUARD_WM,
507 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
509 static const struct intel_watermark_params pineview_cursor_wm = {
510 .fifo_size = PINEVIEW_CURSOR_FIFO,
511 .max_wm = PINEVIEW_CURSOR_MAX_WM,
512 .default_wm = PINEVIEW_CURSOR_DFT_WM,
513 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
514 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
516 static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
517 .fifo_size = PINEVIEW_CURSOR_FIFO,
518 .max_wm = PINEVIEW_CURSOR_MAX_WM,
519 .default_wm = PINEVIEW_CURSOR_DFT_WM,
520 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
521 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
523 static const struct intel_watermark_params g4x_wm_info = {
524 .fifo_size = G4X_FIFO_SIZE,
525 .max_wm = G4X_MAX_WM,
526 .default_wm = G4X_MAX_WM,
528 .cacheline_size = G4X_FIFO_LINE_SIZE,
530 static const struct intel_watermark_params g4x_cursor_wm_info = {
531 .fifo_size = I965_CURSOR_FIFO,
532 .max_wm = I965_CURSOR_MAX_WM,
533 .default_wm = I965_CURSOR_DFT_WM,
535 .cacheline_size = G4X_FIFO_LINE_SIZE,
537 static const struct intel_watermark_params valleyview_wm_info = {
538 .fifo_size = VALLEYVIEW_FIFO_SIZE,
539 .max_wm = VALLEYVIEW_MAX_WM,
540 .default_wm = VALLEYVIEW_MAX_WM,
542 .cacheline_size = G4X_FIFO_LINE_SIZE,
544 static const struct intel_watermark_params valleyview_cursor_wm_info = {
545 .fifo_size = I965_CURSOR_FIFO,
546 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
547 .default_wm = I965_CURSOR_DFT_WM,
549 .cacheline_size = G4X_FIFO_LINE_SIZE,
551 static const struct intel_watermark_params i965_cursor_wm_info = {
552 .fifo_size = I965_CURSOR_FIFO,
553 .max_wm = I965_CURSOR_MAX_WM,
554 .default_wm = I965_CURSOR_DFT_WM,
556 .cacheline_size = I915_FIFO_LINE_SIZE,
558 static const struct intel_watermark_params i945_wm_info = {
559 .fifo_size = I945_FIFO_SIZE,
560 .max_wm = I915_MAX_WM,
563 .cacheline_size = I915_FIFO_LINE_SIZE,
565 static const struct intel_watermark_params i915_wm_info = {
566 .fifo_size = I915_FIFO_SIZE,
567 .max_wm = I915_MAX_WM,
570 .cacheline_size = I915_FIFO_LINE_SIZE,
572 static const struct intel_watermark_params i830_a_wm_info = {
573 .fifo_size = I855GM_FIFO_SIZE,
574 .max_wm = I915_MAX_WM,
577 .cacheline_size = I830_FIFO_LINE_SIZE,
579 static const struct intel_watermark_params i830_bc_wm_info = {
580 .fifo_size = I855GM_FIFO_SIZE,
581 .max_wm = I915_MAX_WM/2,
584 .cacheline_size = I830_FIFO_LINE_SIZE,
586 static const struct intel_watermark_params i845_wm_info = {
587 .fifo_size = I830_FIFO_SIZE,
588 .max_wm = I915_MAX_WM,
591 .cacheline_size = I830_FIFO_LINE_SIZE,
595 * intel_calculate_wm - calculate watermark level
596 * @clock_in_khz: pixel clock
597 * @wm: chip FIFO params
598 * @pixel_size: display pixel size
599 * @latency_ns: memory latency for the platform
601 * Calculate the watermark level (the level at which the display plane will
602 * start fetching from memory again). Each chip has a different display
603 * FIFO size and allocation, so the caller needs to figure that out and pass
604 * in the correct intel_watermark_params structure.
606 * As the pixel clock runs, the FIFO will be drained at a rate that depends
607 * on the pixel size. When it reaches the watermark level, it'll start
608 * fetching FIFO line sized based chunks from memory until the FIFO fills
609 * past the watermark point. If the FIFO drains completely, a FIFO underrun
610 * will occur, and a display engine hang could result.
612 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
613 const struct intel_watermark_params *wm,
616 unsigned long latency_ns)
618 long entries_required, wm_size;
621 * Note: we need to make sure we don't overflow for various clock &
623 * clocks go from a few thousand to several hundred thousand.
624 * latency is usually a few thousand
626 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
628 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
630 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
632 wm_size = fifo_size - (entries_required + wm->guard_size);
634 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
636 /* Don't promote wm_size to unsigned... */
637 if (wm_size > (long)wm->max_wm)
638 wm_size = wm->max_wm;
640 wm_size = wm->default_wm;
643 * Bspec seems to indicate that the value shouldn't be lower than
644 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
645 * Lets go for 8 which is the burst size since certain platforms
646 * already use a hardcoded 8 (which is what the spec says should be
655 static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
657 struct drm_crtc *crtc, *enabled = NULL;
659 for_each_crtc(dev, crtc) {
660 if (intel_crtc_active(crtc)) {
670 static void pineview_update_wm(struct drm_crtc *unused_crtc)
672 struct drm_device *dev = unused_crtc->dev;
673 struct drm_i915_private *dev_priv = dev->dev_private;
674 struct drm_crtc *crtc;
675 const struct cxsr_latency *latency;
679 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
680 dev_priv->fsb_freq, dev_priv->mem_freq);
682 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
683 intel_set_memory_cxsr(dev_priv, false);
687 crtc = single_enabled_crtc(dev);
689 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
690 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
691 int clock = adjusted_mode->crtc_clock;
694 wm = intel_calculate_wm(clock, &pineview_display_wm,
695 pineview_display_wm.fifo_size,
696 pixel_size, latency->display_sr);
697 reg = I915_READ(DSPFW1);
698 reg &= ~DSPFW_SR_MASK;
699 reg |= FW_WM(wm, SR);
700 I915_WRITE(DSPFW1, reg);
701 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
704 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
705 pineview_display_wm.fifo_size,
706 pixel_size, latency->cursor_sr);
707 reg = I915_READ(DSPFW3);
708 reg &= ~DSPFW_CURSOR_SR_MASK;
709 reg |= FW_WM(wm, CURSOR_SR);
710 I915_WRITE(DSPFW3, reg);
712 /* Display HPLL off SR */
713 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
714 pineview_display_hplloff_wm.fifo_size,
715 pixel_size, latency->display_hpll_disable);
716 reg = I915_READ(DSPFW3);
717 reg &= ~DSPFW_HPLL_SR_MASK;
718 reg |= FW_WM(wm, HPLL_SR);
719 I915_WRITE(DSPFW3, reg);
721 /* cursor HPLL off SR */
722 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
723 pineview_display_hplloff_wm.fifo_size,
724 pixel_size, latency->cursor_hpll_disable);
725 reg = I915_READ(DSPFW3);
726 reg &= ~DSPFW_HPLL_CURSOR_MASK;
727 reg |= FW_WM(wm, HPLL_CURSOR);
728 I915_WRITE(DSPFW3, reg);
729 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
731 intel_set_memory_cxsr(dev_priv, true);
733 intel_set_memory_cxsr(dev_priv, false);
737 static bool g4x_compute_wm0(struct drm_device *dev,
739 const struct intel_watermark_params *display,
740 int display_latency_ns,
741 const struct intel_watermark_params *cursor,
742 int cursor_latency_ns,
746 struct drm_crtc *crtc;
747 const struct drm_display_mode *adjusted_mode;
748 int htotal, hdisplay, clock, pixel_size;
749 int line_time_us, line_count;
750 int entries, tlb_miss;
752 crtc = intel_get_crtc_for_plane(dev, plane);
753 if (!intel_crtc_active(crtc)) {
754 *cursor_wm = cursor->guard_size;
755 *plane_wm = display->guard_size;
759 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
760 clock = adjusted_mode->crtc_clock;
761 htotal = adjusted_mode->crtc_htotal;
762 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
763 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
765 /* Use the small buffer method to calculate plane watermark */
766 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
767 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
770 entries = DIV_ROUND_UP(entries, display->cacheline_size);
771 *plane_wm = entries + display->guard_size;
772 if (*plane_wm > (int)display->max_wm)
773 *plane_wm = display->max_wm;
775 /* Use the large buffer method to calculate cursor watermark */
776 line_time_us = max(htotal * 1000 / clock, 1);
777 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
778 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
779 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
782 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
783 *cursor_wm = entries + cursor->guard_size;
784 if (*cursor_wm > (int)cursor->max_wm)
785 *cursor_wm = (int)cursor->max_wm;
791 * Check the wm result.
793 * If any calculated watermark values is larger than the maximum value that
794 * can be programmed into the associated watermark register, that watermark
797 static bool g4x_check_srwm(struct drm_device *dev,
798 int display_wm, int cursor_wm,
799 const struct intel_watermark_params *display,
800 const struct intel_watermark_params *cursor)
802 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
803 display_wm, cursor_wm);
805 if (display_wm > display->max_wm) {
806 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
807 display_wm, display->max_wm);
811 if (cursor_wm > cursor->max_wm) {
812 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
813 cursor_wm, cursor->max_wm);
817 if (!(display_wm || cursor_wm)) {
818 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
825 static bool g4x_compute_srwm(struct drm_device *dev,
828 const struct intel_watermark_params *display,
829 const struct intel_watermark_params *cursor,
830 int *display_wm, int *cursor_wm)
832 struct drm_crtc *crtc;
833 const struct drm_display_mode *adjusted_mode;
834 int hdisplay, htotal, pixel_size, clock;
835 unsigned long line_time_us;
836 int line_count, line_size;
841 *display_wm = *cursor_wm = 0;
845 crtc = intel_get_crtc_for_plane(dev, plane);
846 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
847 clock = adjusted_mode->crtc_clock;
848 htotal = adjusted_mode->crtc_htotal;
849 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
850 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
852 line_time_us = max(htotal * 1000 / clock, 1);
853 line_count = (latency_ns / line_time_us + 1000) / 1000;
854 line_size = hdisplay * pixel_size;
856 /* Use the minimum of the small and large buffer method for primary */
857 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
858 large = line_count * line_size;
860 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
861 *display_wm = entries + display->guard_size;
863 /* calculate the self-refresh watermark for display cursor */
864 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
865 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
866 *cursor_wm = entries + cursor->guard_size;
868 return g4x_check_srwm(dev,
869 *display_wm, *cursor_wm,
873 #define FW_WM_VLV(value, plane) \
874 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
876 static void vlv_write_wm_values(struct intel_crtc *crtc,
877 const struct vlv_wm_values *wm)
879 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
880 enum pipe pipe = crtc->pipe;
882 I915_WRITE(VLV_DDL(pipe),
883 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
884 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
885 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
886 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
889 FW_WM(wm->sr.plane, SR) |
890 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
891 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
892 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
894 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
895 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
896 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
898 FW_WM(wm->sr.cursor, CURSOR_SR));
900 if (IS_CHERRYVIEW(dev_priv)) {
901 I915_WRITE(DSPFW7_CHV,
902 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
903 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
904 I915_WRITE(DSPFW8_CHV,
905 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
906 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
907 I915_WRITE(DSPFW9_CHV,
908 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
909 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
911 FW_WM(wm->sr.plane >> 9, SR_HI) |
912 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
913 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
914 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
915 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
916 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
917 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
918 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
919 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
920 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
923 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
924 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
926 FW_WM(wm->sr.plane >> 9, SR_HI) |
927 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
928 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
929 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
930 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
931 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
932 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
935 /* zero (unused) WM1 watermarks */
936 I915_WRITE(DSPFW4, 0);
937 I915_WRITE(DSPFW5, 0);
938 I915_WRITE(DSPFW6, 0);
939 I915_WRITE(DSPHOWM1, 0);
941 POSTING_READ(DSPFW1);
949 VLV_WM_LEVEL_DDR_DVFS,
952 /* latency must be in 0.1us units. */
953 static unsigned int vlv_wm_method2(unsigned int pixel_rate,
954 unsigned int pipe_htotal,
955 unsigned int horiz_pixels,
956 unsigned int bytes_per_pixel,
957 unsigned int latency)
961 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
962 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
963 ret = DIV_ROUND_UP(ret, 64);
968 static void vlv_setup_wm_latency(struct drm_device *dev)
970 struct drm_i915_private *dev_priv = dev->dev_private;
972 /* all latencies in usec */
973 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
975 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
977 if (IS_CHERRYVIEW(dev_priv)) {
978 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
979 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
981 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
985 static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
986 struct intel_crtc *crtc,
987 const struct intel_plane_state *state,
990 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
991 int clock, htotal, pixel_size, width, wm;
993 if (dev_priv->wm.pri_latency[level] == 0)
999 pixel_size = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1000 clock = crtc->config->base.adjusted_mode.crtc_clock;
1001 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
1002 width = crtc->config->pipe_src_w;
1003 if (WARN_ON(htotal == 0))
1006 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1008 * FIXME the formula gives values that are
1009 * too big for the cursor FIFO, and hence we
1010 * would never be able to use cursors. For
1011 * now just hardcode the watermark.
1015 wm = vlv_wm_method2(clock, htotal, width, pixel_size,
1016 dev_priv->wm.pri_latency[level] * 10);
1019 return min_t(int, wm, USHRT_MAX);
1022 static void vlv_compute_fifo(struct intel_crtc *crtc)
1024 struct drm_device *dev = crtc->base.dev;
1025 struct vlv_wm_state *wm_state = &crtc->wm_state;
1026 struct intel_plane *plane;
1027 unsigned int total_rate = 0;
1028 const int fifo_size = 512 - 1;
1029 int fifo_extra, fifo_left = fifo_size;
1031 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1032 struct intel_plane_state *state =
1033 to_intel_plane_state(plane->base.state);
1035 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1038 if (state->visible) {
1039 wm_state->num_active_planes++;
1040 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1044 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1045 struct intel_plane_state *state =
1046 to_intel_plane_state(plane->base.state);
1049 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1050 plane->wm.fifo_size = 63;
1054 if (!state->visible) {
1055 plane->wm.fifo_size = 0;
1059 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1060 plane->wm.fifo_size = fifo_size * rate / total_rate;
1061 fifo_left -= plane->wm.fifo_size;
1064 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1066 /* spread the remainder evenly */
1067 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1073 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1076 /* give it all to the first plane if none are active */
1077 if (plane->wm.fifo_size == 0 &&
1078 wm_state->num_active_planes)
1081 plane_extra = min(fifo_extra, fifo_left);
1082 plane->wm.fifo_size += plane_extra;
1083 fifo_left -= plane_extra;
1086 WARN_ON(fifo_left != 0);
1089 static void vlv_invert_wms(struct intel_crtc *crtc)
1091 struct vlv_wm_state *wm_state = &crtc->wm_state;
1094 for (level = 0; level < wm_state->num_levels; level++) {
1095 struct drm_device *dev = crtc->base.dev;
1096 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1097 struct intel_plane *plane;
1099 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1100 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1102 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1103 switch (plane->base.type) {
1105 case DRM_PLANE_TYPE_CURSOR:
1106 wm_state->wm[level].cursor = plane->wm.fifo_size -
1107 wm_state->wm[level].cursor;
1109 case DRM_PLANE_TYPE_PRIMARY:
1110 wm_state->wm[level].primary = plane->wm.fifo_size -
1111 wm_state->wm[level].primary;
1113 case DRM_PLANE_TYPE_OVERLAY:
1114 sprite = plane->plane;
1115 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1116 wm_state->wm[level].sprite[sprite];
1123 static void vlv_compute_wm(struct intel_crtc *crtc)
1125 struct drm_device *dev = crtc->base.dev;
1126 struct vlv_wm_state *wm_state = &crtc->wm_state;
1127 struct intel_plane *plane;
1128 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1131 memset(wm_state, 0, sizeof(*wm_state));
1133 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
1134 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
1136 wm_state->num_active_planes = 0;
1138 vlv_compute_fifo(crtc);
1140 if (wm_state->num_active_planes != 1)
1141 wm_state->cxsr = false;
1143 if (wm_state->cxsr) {
1144 for (level = 0; level < wm_state->num_levels; level++) {
1145 wm_state->sr[level].plane = sr_fifo_size;
1146 wm_state->sr[level].cursor = 63;
1150 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1151 struct intel_plane_state *state =
1152 to_intel_plane_state(plane->base.state);
1154 if (!state->visible)
1157 /* normal watermarks */
1158 for (level = 0; level < wm_state->num_levels; level++) {
1159 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1160 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1163 if (WARN_ON(level == 0 && wm > max_wm))
1166 if (wm > plane->wm.fifo_size)
1169 switch (plane->base.type) {
1171 case DRM_PLANE_TYPE_CURSOR:
1172 wm_state->wm[level].cursor = wm;
1174 case DRM_PLANE_TYPE_PRIMARY:
1175 wm_state->wm[level].primary = wm;
1177 case DRM_PLANE_TYPE_OVERLAY:
1178 sprite = plane->plane;
1179 wm_state->wm[level].sprite[sprite] = wm;
1184 wm_state->num_levels = level;
1186 if (!wm_state->cxsr)
1189 /* maxfifo watermarks */
1190 switch (plane->base.type) {
1192 case DRM_PLANE_TYPE_CURSOR:
1193 for (level = 0; level < wm_state->num_levels; level++)
1194 wm_state->sr[level].cursor =
1195 wm_state->sr[level].cursor;
1197 case DRM_PLANE_TYPE_PRIMARY:
1198 for (level = 0; level < wm_state->num_levels; level++)
1199 wm_state->sr[level].plane =
1200 min(wm_state->sr[level].plane,
1201 wm_state->wm[level].primary);
1203 case DRM_PLANE_TYPE_OVERLAY:
1204 sprite = plane->plane;
1205 for (level = 0; level < wm_state->num_levels; level++)
1206 wm_state->sr[level].plane =
1207 min(wm_state->sr[level].plane,
1208 wm_state->wm[level].sprite[sprite]);
1213 /* clear any (partially) filled invalid levels */
1214 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
1215 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1216 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1219 vlv_invert_wms(crtc);
1222 #define VLV_FIFO(plane, value) \
1223 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1225 static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1227 struct drm_device *dev = crtc->base.dev;
1228 struct drm_i915_private *dev_priv = to_i915(dev);
1229 struct intel_plane *plane;
1230 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1232 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1233 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1234 WARN_ON(plane->wm.fifo_size != 63);
1238 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1239 sprite0_start = plane->wm.fifo_size;
1240 else if (plane->plane == 0)
1241 sprite1_start = sprite0_start + plane->wm.fifo_size;
1243 fifo_size = sprite1_start + plane->wm.fifo_size;
1246 WARN_ON(fifo_size != 512 - 1);
1248 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1249 pipe_name(crtc->pipe), sprite0_start,
1250 sprite1_start, fifo_size);
1252 switch (crtc->pipe) {
1253 uint32_t dsparb, dsparb2, dsparb3;
1255 dsparb = I915_READ(DSPARB);
1256 dsparb2 = I915_READ(DSPARB2);
1258 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1259 VLV_FIFO(SPRITEB, 0xff));
1260 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1261 VLV_FIFO(SPRITEB, sprite1_start));
1263 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1264 VLV_FIFO(SPRITEB_HI, 0x1));
1265 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1266 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1268 I915_WRITE(DSPARB, dsparb);
1269 I915_WRITE(DSPARB2, dsparb2);
1272 dsparb = I915_READ(DSPARB);
1273 dsparb2 = I915_READ(DSPARB2);
1275 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1276 VLV_FIFO(SPRITED, 0xff));
1277 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1278 VLV_FIFO(SPRITED, sprite1_start));
1280 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1281 VLV_FIFO(SPRITED_HI, 0xff));
1282 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1283 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1285 I915_WRITE(DSPARB, dsparb);
1286 I915_WRITE(DSPARB2, dsparb2);
1289 dsparb3 = I915_READ(DSPARB3);
1290 dsparb2 = I915_READ(DSPARB2);
1292 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1293 VLV_FIFO(SPRITEF, 0xff));
1294 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1295 VLV_FIFO(SPRITEF, sprite1_start));
1297 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1298 VLV_FIFO(SPRITEF_HI, 0xff));
1299 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1300 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1302 I915_WRITE(DSPARB3, dsparb3);
1303 I915_WRITE(DSPARB2, dsparb2);
1312 static void vlv_merge_wm(struct drm_device *dev,
1313 struct vlv_wm_values *wm)
1315 struct intel_crtc *crtc;
1316 int num_active_crtcs = 0;
1318 wm->level = to_i915(dev)->wm.max_level;
1321 for_each_intel_crtc(dev, crtc) {
1322 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1327 if (!wm_state->cxsr)
1331 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1334 if (num_active_crtcs != 1)
1337 if (num_active_crtcs > 1)
1338 wm->level = VLV_WM_LEVEL_PM2;
1340 for_each_intel_crtc(dev, crtc) {
1341 struct vlv_wm_state *wm_state = &crtc->wm_state;
1342 enum pipe pipe = crtc->pipe;
1347 wm->pipe[pipe] = wm_state->wm[wm->level];
1349 wm->sr = wm_state->sr[wm->level];
1351 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1352 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1353 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1354 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1358 static void vlv_update_wm(struct drm_crtc *crtc)
1360 struct drm_device *dev = crtc->dev;
1361 struct drm_i915_private *dev_priv = dev->dev_private;
1362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1363 enum pipe pipe = intel_crtc->pipe;
1364 struct vlv_wm_values wm = {};
1366 vlv_compute_wm(intel_crtc);
1367 vlv_merge_wm(dev, &wm);
1369 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1370 /* FIXME should be part of crtc atomic commit */
1371 vlv_pipe_set_fifo_size(intel_crtc);
1375 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1376 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1377 chv_set_memory_dvfs(dev_priv, false);
1379 if (wm.level < VLV_WM_LEVEL_PM5 &&
1380 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1381 chv_set_memory_pm5(dev_priv, false);
1383 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
1384 intel_set_memory_cxsr(dev_priv, false);
1386 /* FIXME should be part of crtc atomic commit */
1387 vlv_pipe_set_fifo_size(intel_crtc);
1389 vlv_write_wm_values(intel_crtc, &wm);
1391 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1392 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1393 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1394 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1395 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1397 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
1398 intel_set_memory_cxsr(dev_priv, true);
1400 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1401 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1402 chv_set_memory_pm5(dev_priv, true);
1404 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1405 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1406 chv_set_memory_dvfs(dev_priv, true);
1408 dev_priv->wm.vlv = wm;
1411 #define single_plane_enabled(mask) is_power_of_2(mask)
1413 static void g4x_update_wm(struct drm_crtc *crtc)
1415 struct drm_device *dev = crtc->dev;
1416 static const int sr_latency_ns = 12000;
1417 struct drm_i915_private *dev_priv = dev->dev_private;
1418 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1419 int plane_sr, cursor_sr;
1420 unsigned int enabled = 0;
1423 if (g4x_compute_wm0(dev, PIPE_A,
1424 &g4x_wm_info, pessimal_latency_ns,
1425 &g4x_cursor_wm_info, pessimal_latency_ns,
1426 &planea_wm, &cursora_wm))
1427 enabled |= 1 << PIPE_A;
1429 if (g4x_compute_wm0(dev, PIPE_B,
1430 &g4x_wm_info, pessimal_latency_ns,
1431 &g4x_cursor_wm_info, pessimal_latency_ns,
1432 &planeb_wm, &cursorb_wm))
1433 enabled |= 1 << PIPE_B;
1435 if (single_plane_enabled(enabled) &&
1436 g4x_compute_srwm(dev, ffs(enabled) - 1,
1439 &g4x_cursor_wm_info,
1440 &plane_sr, &cursor_sr)) {
1441 cxsr_enabled = true;
1443 cxsr_enabled = false;
1444 intel_set_memory_cxsr(dev_priv, false);
1445 plane_sr = cursor_sr = 0;
1448 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1449 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1450 planea_wm, cursora_wm,
1451 planeb_wm, cursorb_wm,
1452 plane_sr, cursor_sr);
1455 FW_WM(plane_sr, SR) |
1456 FW_WM(cursorb_wm, CURSORB) |
1457 FW_WM(planeb_wm, PLANEB) |
1458 FW_WM(planea_wm, PLANEA));
1460 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1461 FW_WM(cursora_wm, CURSORA));
1462 /* HPLL off in SR has some issues on G4x... disable it */
1464 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1465 FW_WM(cursor_sr, CURSOR_SR));
1468 intel_set_memory_cxsr(dev_priv, true);
1471 static void i965_update_wm(struct drm_crtc *unused_crtc)
1473 struct drm_device *dev = unused_crtc->dev;
1474 struct drm_i915_private *dev_priv = dev->dev_private;
1475 struct drm_crtc *crtc;
1480 /* Calc sr entries for one plane configs */
1481 crtc = single_enabled_crtc(dev);
1483 /* self-refresh has much higher latency */
1484 static const int sr_latency_ns = 12000;
1485 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1486 int clock = adjusted_mode->crtc_clock;
1487 int htotal = adjusted_mode->crtc_htotal;
1488 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1489 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1490 unsigned long line_time_us;
1493 line_time_us = max(htotal * 1000 / clock, 1);
1495 /* Use ns/us then divide to preserve precision */
1496 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1497 pixel_size * hdisplay;
1498 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1499 srwm = I965_FIFO_SIZE - entries;
1503 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1506 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1507 pixel_size * crtc->cursor->state->crtc_w;
1508 entries = DIV_ROUND_UP(entries,
1509 i965_cursor_wm_info.cacheline_size);
1510 cursor_sr = i965_cursor_wm_info.fifo_size -
1511 (entries + i965_cursor_wm_info.guard_size);
1513 if (cursor_sr > i965_cursor_wm_info.max_wm)
1514 cursor_sr = i965_cursor_wm_info.max_wm;
1516 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1517 "cursor %d\n", srwm, cursor_sr);
1519 cxsr_enabled = true;
1521 cxsr_enabled = false;
1522 /* Turn off self refresh if both pipes are enabled */
1523 intel_set_memory_cxsr(dev_priv, false);
1526 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1529 /* 965 has limitations... */
1530 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1534 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1535 FW_WM(8, PLANEC_OLD));
1536 /* update cursor SR watermark */
1537 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
1540 intel_set_memory_cxsr(dev_priv, true);
1545 static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1547 struct drm_device *dev = unused_crtc->dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
1549 const struct intel_watermark_params *wm_info;
1554 int planea_wm, planeb_wm;
1555 struct drm_crtc *crtc, *enabled = NULL;
1558 wm_info = &i945_wm_info;
1559 else if (!IS_GEN2(dev))
1560 wm_info = &i915_wm_info;
1562 wm_info = &i830_a_wm_info;
1564 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1565 crtc = intel_get_crtc_for_plane(dev, 0);
1566 if (intel_crtc_active(crtc)) {
1567 const struct drm_display_mode *adjusted_mode;
1568 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1572 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1573 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1574 wm_info, fifo_size, cpp,
1575 pessimal_latency_ns);
1578 planea_wm = fifo_size - wm_info->guard_size;
1579 if (planea_wm > (long)wm_info->max_wm)
1580 planea_wm = wm_info->max_wm;
1584 wm_info = &i830_bc_wm_info;
1586 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1587 crtc = intel_get_crtc_for_plane(dev, 1);
1588 if (intel_crtc_active(crtc)) {
1589 const struct drm_display_mode *adjusted_mode;
1590 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1594 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1595 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1596 wm_info, fifo_size, cpp,
1597 pessimal_latency_ns);
1598 if (enabled == NULL)
1603 planeb_wm = fifo_size - wm_info->guard_size;
1604 if (planeb_wm > (long)wm_info->max_wm)
1605 planeb_wm = wm_info->max_wm;
1608 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1610 if (IS_I915GM(dev) && enabled) {
1611 struct drm_i915_gem_object *obj;
1613 obj = intel_fb_obj(enabled->primary->state->fb);
1615 /* self-refresh seems busted with untiled */
1616 if (obj->tiling_mode == I915_TILING_NONE)
1621 * Overlay gets an aggressive default since video jitter is bad.
1625 /* Play safe and disable self-refresh before adjusting watermarks. */
1626 intel_set_memory_cxsr(dev_priv, false);
1628 /* Calc sr entries for one plane configs */
1629 if (HAS_FW_BLC(dev) && enabled) {
1630 /* self-refresh has much higher latency */
1631 static const int sr_latency_ns = 6000;
1632 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
1633 int clock = adjusted_mode->crtc_clock;
1634 int htotal = adjusted_mode->crtc_htotal;
1635 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
1636 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
1637 unsigned long line_time_us;
1640 line_time_us = max(htotal * 1000 / clock, 1);
1642 /* Use ns/us then divide to preserve precision */
1643 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1644 pixel_size * hdisplay;
1645 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1646 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1647 srwm = wm_info->fifo_size - entries;
1651 if (IS_I945G(dev) || IS_I945GM(dev))
1652 I915_WRITE(FW_BLC_SELF,
1653 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1654 else if (IS_I915GM(dev))
1655 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1658 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1659 planea_wm, planeb_wm, cwm, srwm);
1661 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1662 fwater_hi = (cwm & 0x1f);
1664 /* Set request length to 8 cachelines per fetch */
1665 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1666 fwater_hi = fwater_hi | (1 << 8);
1668 I915_WRITE(FW_BLC, fwater_lo);
1669 I915_WRITE(FW_BLC2, fwater_hi);
1672 intel_set_memory_cxsr(dev_priv, true);
1675 static void i845_update_wm(struct drm_crtc *unused_crtc)
1677 struct drm_device *dev = unused_crtc->dev;
1678 struct drm_i915_private *dev_priv = dev->dev_private;
1679 struct drm_crtc *crtc;
1680 const struct drm_display_mode *adjusted_mode;
1684 crtc = single_enabled_crtc(dev);
1688 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1689 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1691 dev_priv->display.get_fifo_size(dev, 0),
1692 4, pessimal_latency_ns);
1693 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1694 fwater_lo |= (3<<8) | planea_wm;
1696 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1698 I915_WRITE(FW_BLC, fwater_lo);
1701 uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
1703 uint32_t pixel_rate;
1705 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
1707 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1708 * adjust the pixel_rate here. */
1710 if (pipe_config->pch_pfit.enabled) {
1711 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1712 uint32_t pfit_size = pipe_config->pch_pfit.size;
1714 pipe_w = pipe_config->pipe_src_w;
1715 pipe_h = pipe_config->pipe_src_h;
1717 pfit_w = (pfit_size >> 16) & 0xFFFF;
1718 pfit_h = pfit_size & 0xFFFF;
1719 if (pipe_w < pfit_w)
1721 if (pipe_h < pfit_h)
1724 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1731 /* latency must be in 0.1us units. */
1732 static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1737 if (WARN(latency == 0, "Latency value missing\n"))
1740 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1741 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1746 /* latency must be in 0.1us units. */
1747 static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1748 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1753 if (WARN(latency == 0, "Latency value missing\n"))
1756 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1757 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1758 ret = DIV_ROUND_UP(ret, 64) + 2;
1762 static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1763 uint8_t bytes_per_pixel)
1765 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1768 struct ilk_wm_maximums {
1775 /* used in computing the new watermarks state */
1776 struct intel_wm_config {
1777 unsigned int num_pipes_active;
1778 bool sprites_enabled;
1779 bool sprites_scaled;
1783 * For both WM_PIPE and WM_LP.
1784 * mem_value must be in 0.1us units.
1786 static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
1787 const struct intel_plane_state *pstate,
1791 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1792 uint32_t method1, method2;
1794 if (!cstate->base.active || !pstate->visible)
1797 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
1802 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1803 cstate->base.adjusted_mode.crtc_htotal,
1804 drm_rect_width(&pstate->dst),
1808 return min(method1, method2);
1812 * For both WM_PIPE and WM_LP.
1813 * mem_value must be in 0.1us units.
1815 static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
1816 const struct intel_plane_state *pstate,
1819 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1820 uint32_t method1, method2;
1822 if (!cstate->base.active || !pstate->visible)
1825 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
1826 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1827 cstate->base.adjusted_mode.crtc_htotal,
1828 drm_rect_width(&pstate->dst),
1831 return min(method1, method2);
1835 * For both WM_PIPE and WM_LP.
1836 * mem_value must be in 0.1us units.
1838 static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
1839 const struct intel_plane_state *pstate,
1842 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1844 if (!cstate->base.active || !pstate->visible)
1847 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1848 cstate->base.adjusted_mode.crtc_htotal,
1849 drm_rect_width(&pstate->dst),
1854 /* Only for WM_LP. */
1855 static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1856 const struct intel_plane_state *pstate,
1859 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1861 if (!cstate->base.active || !pstate->visible)
1864 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), bpp);
1867 static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1869 if (INTEL_INFO(dev)->gen >= 8)
1871 else if (INTEL_INFO(dev)->gen >= 7)
1877 static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1878 int level, bool is_sprite)
1880 if (INTEL_INFO(dev)->gen >= 8)
1881 /* BDW primary/sprite plane watermarks */
1882 return level == 0 ? 255 : 2047;
1883 else if (INTEL_INFO(dev)->gen >= 7)
1884 /* IVB/HSW primary/sprite plane watermarks */
1885 return level == 0 ? 127 : 1023;
1886 else if (!is_sprite)
1887 /* ILK/SNB primary plane watermarks */
1888 return level == 0 ? 127 : 511;
1890 /* ILK/SNB sprite plane watermarks */
1891 return level == 0 ? 63 : 255;
1894 static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1897 if (INTEL_INFO(dev)->gen >= 7)
1898 return level == 0 ? 63 : 255;
1900 return level == 0 ? 31 : 63;
1903 static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1905 if (INTEL_INFO(dev)->gen >= 8)
1911 /* Calculate the maximum primary/sprite plane watermark */
1912 static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1914 const struct intel_wm_config *config,
1915 enum intel_ddb_partitioning ddb_partitioning,
1918 unsigned int fifo_size = ilk_display_fifo_size(dev);
1920 /* if sprites aren't enabled, sprites get nothing */
1921 if (is_sprite && !config->sprites_enabled)
1924 /* HSW allows LP1+ watermarks even with multiple pipes */
1925 if (level == 0 || config->num_pipes_active > 1) {
1926 fifo_size /= INTEL_INFO(dev)->num_pipes;
1929 * For some reason the non self refresh
1930 * FIFO size is only half of the self
1931 * refresh FIFO size on ILK/SNB.
1933 if (INTEL_INFO(dev)->gen <= 6)
1937 if (config->sprites_enabled) {
1938 /* level 0 is always calculated with 1:1 split */
1939 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1948 /* clamp to max that the registers can hold */
1949 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
1952 /* Calculate the maximum cursor plane watermark */
1953 static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
1955 const struct intel_wm_config *config)
1957 /* HSW LP1+ watermarks w/ multiple pipes */
1958 if (level > 0 && config->num_pipes_active > 1)
1961 /* otherwise just report max that registers can hold */
1962 return ilk_cursor_wm_reg_max(dev, level);
1965 static void ilk_compute_wm_maximums(const struct drm_device *dev,
1967 const struct intel_wm_config *config,
1968 enum intel_ddb_partitioning ddb_partitioning,
1969 struct ilk_wm_maximums *max)
1971 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1972 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1973 max->cur = ilk_cursor_wm_max(dev, level, config);
1974 max->fbc = ilk_fbc_wm_reg_max(dev);
1977 static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1979 struct ilk_wm_maximums *max)
1981 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1982 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1983 max->cur = ilk_cursor_wm_reg_max(dev, level);
1984 max->fbc = ilk_fbc_wm_reg_max(dev);
1987 static bool ilk_validate_wm_level(int level,
1988 const struct ilk_wm_maximums *max,
1989 struct intel_wm_level *result)
1993 /* already determined to be invalid? */
1994 if (!result->enable)
1997 result->enable = result->pri_val <= max->pri &&
1998 result->spr_val <= max->spr &&
1999 result->cur_val <= max->cur;
2001 ret = result->enable;
2004 * HACK until we can pre-compute everything,
2005 * and thus fail gracefully if LP0 watermarks
2008 if (level == 0 && !result->enable) {
2009 if (result->pri_val > max->pri)
2010 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2011 level, result->pri_val, max->pri);
2012 if (result->spr_val > max->spr)
2013 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2014 level, result->spr_val, max->spr);
2015 if (result->cur_val > max->cur)
2016 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2017 level, result->cur_val, max->cur);
2019 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2020 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2021 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2022 result->enable = true;
2028 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2029 const struct intel_crtc *intel_crtc,
2031 struct intel_crtc_state *cstate,
2032 struct intel_wm_level *result)
2034 struct intel_plane *intel_plane;
2035 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2036 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2037 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2039 /* WM1+ latency values stored in 0.5us units */
2046 for_each_intel_plane_on_crtc(dev_priv->dev, intel_crtc, intel_plane) {
2047 struct intel_plane_state *pstate =
2048 to_intel_plane_state(intel_plane->base.state);
2050 switch (intel_plane->base.type) {
2051 case DRM_PLANE_TYPE_PRIMARY:
2052 result->pri_val = ilk_compute_pri_wm(cstate, pstate,
2055 result->fbc_val = ilk_compute_fbc_wm(cstate, pstate,
2058 case DRM_PLANE_TYPE_OVERLAY:
2059 result->spr_val = ilk_compute_spr_wm(cstate, pstate,
2062 case DRM_PLANE_TYPE_CURSOR:
2063 result->cur_val = ilk_compute_cur_wm(cstate, pstate,
2069 result->enable = true;
2073 hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
2075 struct drm_i915_private *dev_priv = dev->dev_private;
2076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2077 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
2078 u32 linetime, ips_linetime;
2080 if (!intel_crtc->active)
2083 /* The WM are computed with base on how long it takes to fill a single
2084 * row at the given clock rate, multiplied by 8.
2086 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2087 adjusted_mode->crtc_clock);
2088 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2089 dev_priv->cdclk_freq);
2091 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2092 PIPE_WM_LINETIME_TIME(linetime);
2095 static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
2097 struct drm_i915_private *dev_priv = dev->dev_private;
2102 int level, max_level = ilk_wm_max_level(dev);
2104 /* read the first set of memory latencies[0:3] */
2105 val = 0; /* data0 to be programmed to 0 for first set */
2106 mutex_lock(&dev_priv->rps.hw_lock);
2107 ret = sandybridge_pcode_read(dev_priv,
2108 GEN9_PCODE_READ_MEM_LATENCY,
2110 mutex_unlock(&dev_priv->rps.hw_lock);
2113 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2117 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2118 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2119 GEN9_MEM_LATENCY_LEVEL_MASK;
2120 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2121 GEN9_MEM_LATENCY_LEVEL_MASK;
2122 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2123 GEN9_MEM_LATENCY_LEVEL_MASK;
2125 /* read the second set of memory latencies[4:7] */
2126 val = 1; /* data0 to be programmed to 1 for second set */
2127 mutex_lock(&dev_priv->rps.hw_lock);
2128 ret = sandybridge_pcode_read(dev_priv,
2129 GEN9_PCODE_READ_MEM_LATENCY,
2131 mutex_unlock(&dev_priv->rps.hw_lock);
2133 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2137 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2138 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2139 GEN9_MEM_LATENCY_LEVEL_MASK;
2140 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2141 GEN9_MEM_LATENCY_LEVEL_MASK;
2142 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2143 GEN9_MEM_LATENCY_LEVEL_MASK;
2146 * WaWmMemoryReadLatency:skl
2148 * punit doesn't take into account the read latency so we need
2149 * to add 2us to the various latency levels we retrieve from
2151 * - W0 is a bit special in that it's the only level that
2152 * can't be disabled if we want to have display working, so
2153 * we always add 2us there.
2154 * - For levels >=1, punit returns 0us latency when they are
2155 * disabled, so we respect that and don't add 2us then
2157 * Additionally, if a level n (n > 1) has a 0us latency, all
2158 * levels m (m >= n) need to be disabled. We make sure to
2159 * sanitize the values out of the punit to satisfy this
2163 for (level = 1; level <= max_level; level++)
2167 for (i = level + 1; i <= max_level; i++)
2172 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2173 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2175 wm[0] = (sskpd >> 56) & 0xFF;
2177 wm[0] = sskpd & 0xF;
2178 wm[1] = (sskpd >> 4) & 0xFF;
2179 wm[2] = (sskpd >> 12) & 0xFF;
2180 wm[3] = (sskpd >> 20) & 0x1FF;
2181 wm[4] = (sskpd >> 32) & 0x1FF;
2182 } else if (INTEL_INFO(dev)->gen >= 6) {
2183 uint32_t sskpd = I915_READ(MCH_SSKPD);
2185 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2186 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2187 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2188 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2189 } else if (INTEL_INFO(dev)->gen >= 5) {
2190 uint32_t mltr = I915_READ(MLTR_ILK);
2192 /* ILK primary LP0 latency is 700 ns */
2194 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2195 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2199 static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2201 /* ILK sprite LP0 latency is 1300 ns */
2202 if (INTEL_INFO(dev)->gen == 5)
2206 static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2208 /* ILK cursor LP0 latency is 1300 ns */
2209 if (INTEL_INFO(dev)->gen == 5)
2212 /* WaDoubleCursorLP3Latency:ivb */
2213 if (IS_IVYBRIDGE(dev))
2217 int ilk_wm_max_level(const struct drm_device *dev)
2219 /* how many WM levels are we expecting */
2220 if (INTEL_INFO(dev)->gen >= 9)
2222 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2224 else if (INTEL_INFO(dev)->gen >= 6)
2230 static void intel_print_wm_latency(struct drm_device *dev,
2232 const uint16_t wm[8])
2234 int level, max_level = ilk_wm_max_level(dev);
2236 for (level = 0; level <= max_level; level++) {
2237 unsigned int latency = wm[level];
2240 DRM_ERROR("%s WM%d latency not provided\n",
2246 * - latencies are in us on gen9.
2247 * - before then, WM1+ latency values are in 0.5us units
2254 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2255 name, level, wm[level],
2256 latency / 10, latency % 10);
2260 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2261 uint16_t wm[5], uint16_t min)
2263 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2268 wm[0] = max(wm[0], min);
2269 for (level = 1; level <= max_level; level++)
2270 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2275 static void snb_wm_latency_quirk(struct drm_device *dev)
2277 struct drm_i915_private *dev_priv = dev->dev_private;
2281 * The BIOS provided WM memory latency values are often
2282 * inadequate for high resolution displays. Adjust them.
2284 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2285 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2286 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2291 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2292 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2293 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2294 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2297 static void ilk_setup_wm_latency(struct drm_device *dev)
2299 struct drm_i915_private *dev_priv = dev->dev_private;
2301 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2303 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2304 sizeof(dev_priv->wm.pri_latency));
2305 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2306 sizeof(dev_priv->wm.pri_latency));
2308 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2309 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2311 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2312 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2313 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2316 snb_wm_latency_quirk(dev);
2319 static void skl_setup_wm_latency(struct drm_device *dev)
2321 struct drm_i915_private *dev_priv = dev->dev_private;
2323 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2324 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2327 static void ilk_compute_wm_config(struct drm_device *dev,
2328 struct intel_wm_config *config)
2330 struct intel_crtc *intel_crtc;
2332 /* Compute the currently _active_ config */
2333 for_each_intel_crtc(dev, intel_crtc) {
2334 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2336 if (!wm->pipe_enabled)
2339 config->sprites_enabled |= wm->sprites_enabled;
2340 config->sprites_scaled |= wm->sprites_scaled;
2341 config->num_pipes_active++;
2345 /* Compute new watermarks for the pipe */
2346 static bool intel_compute_pipe_wm(struct intel_crtc_state *cstate,
2347 struct intel_pipe_wm *pipe_wm)
2349 struct drm_crtc *crtc = cstate->base.crtc;
2350 struct drm_device *dev = crtc->dev;
2351 const struct drm_i915_private *dev_priv = dev->dev_private;
2352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2353 struct intel_plane *intel_plane;
2354 struct intel_plane_state *sprstate = NULL;
2355 int level, max_level = ilk_wm_max_level(dev);
2356 /* LP0 watermark maximums depend on this pipe alone */
2357 struct intel_wm_config config = {
2358 .num_pipes_active = 1,
2360 struct ilk_wm_maximums max;
2362 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2363 if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY) {
2364 sprstate = to_intel_plane_state(intel_plane->base.state);
2369 config.sprites_enabled = sprstate->visible;
2370 config.sprites_scaled = sprstate->visible &&
2371 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2372 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2374 pipe_wm->pipe_enabled = cstate->base.active;
2375 pipe_wm->sprites_enabled = sprstate->visible;
2376 pipe_wm->sprites_scaled = config.sprites_scaled;
2378 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2379 if (INTEL_INFO(dev)->gen <= 6 && sprstate->visible)
2382 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2383 if (config.sprites_scaled)
2386 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate, &pipe_wm->wm[0]);
2388 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2389 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2391 /* LP0 watermarks always use 1/2 DDB partitioning */
2392 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2394 /* At least LP0 must be valid */
2395 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2398 ilk_compute_wm_reg_maximums(dev, 1, &max);
2400 for (level = 1; level <= max_level; level++) {
2401 struct intel_wm_level wm = {};
2403 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate, &wm);
2406 * Disable any watermark level that exceeds the
2407 * register maximums since such watermarks are
2410 if (!ilk_validate_wm_level(level, &max, &wm))
2413 pipe_wm->wm[level] = wm;
2420 * Merge the watermarks from all active pipes for a specific level.
2422 static void ilk_merge_wm_level(struct drm_device *dev,
2424 struct intel_wm_level *ret_wm)
2426 const struct intel_crtc *intel_crtc;
2428 ret_wm->enable = true;
2430 for_each_intel_crtc(dev, intel_crtc) {
2431 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2432 const struct intel_wm_level *wm = &active->wm[level];
2434 if (!active->pipe_enabled)
2438 * The watermark values may have been used in the past,
2439 * so we must maintain them in the registers for some
2440 * time even if the level is now disabled.
2443 ret_wm->enable = false;
2445 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2446 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2447 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2448 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2453 * Merge all low power watermarks for all active pipes.
2455 static void ilk_wm_merge(struct drm_device *dev,
2456 const struct intel_wm_config *config,
2457 const struct ilk_wm_maximums *max,
2458 struct intel_pipe_wm *merged)
2460 struct drm_i915_private *dev_priv = dev->dev_private;
2461 int level, max_level = ilk_wm_max_level(dev);
2462 int last_enabled_level = max_level;
2464 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2465 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2466 config->num_pipes_active > 1)
2469 /* ILK: FBC WM must be disabled always */
2470 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2472 /* merge each WM1+ level */
2473 for (level = 1; level <= max_level; level++) {
2474 struct intel_wm_level *wm = &merged->wm[level];
2476 ilk_merge_wm_level(dev, level, wm);
2478 if (level > last_enabled_level)
2480 else if (!ilk_validate_wm_level(level, max, wm))
2481 /* make sure all following levels get disabled */
2482 last_enabled_level = level - 1;
2485 * The spec says it is preferred to disable
2486 * FBC WMs instead of disabling a WM level.
2488 if (wm->fbc_val > max->fbc) {
2490 merged->fbc_wm_enabled = false;
2495 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2497 * FIXME this is racy. FBC might get enabled later.
2498 * What we should check here is whether FBC can be
2499 * enabled sometime later.
2501 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
2502 intel_fbc_enabled(dev_priv)) {
2503 for (level = 2; level <= max_level; level++) {
2504 struct intel_wm_level *wm = &merged->wm[level];
2511 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2513 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2514 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2517 /* The value we need to program into the WM_LPx latency field */
2518 static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2520 struct drm_i915_private *dev_priv = dev->dev_private;
2522 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2525 return dev_priv->wm.pri_latency[level];
2528 static void ilk_compute_wm_results(struct drm_device *dev,
2529 const struct intel_pipe_wm *merged,
2530 enum intel_ddb_partitioning partitioning,
2531 struct ilk_wm_values *results)
2533 struct intel_crtc *intel_crtc;
2536 results->enable_fbc_wm = merged->fbc_wm_enabled;
2537 results->partitioning = partitioning;
2539 /* LP1+ register values */
2540 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2541 const struct intel_wm_level *r;
2543 level = ilk_wm_lp_to_level(wm_lp, merged);
2545 r = &merged->wm[level];
2548 * Maintain the watermark values even if the level is
2549 * disabled. Doing otherwise could cause underruns.
2551 results->wm_lp[wm_lp - 1] =
2552 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2553 (r->pri_val << WM1_LP_SR_SHIFT) |
2557 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2559 if (INTEL_INFO(dev)->gen >= 8)
2560 results->wm_lp[wm_lp - 1] |=
2561 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2563 results->wm_lp[wm_lp - 1] |=
2564 r->fbc_val << WM1_LP_FBC_SHIFT;
2567 * Always set WM1S_LP_EN when spr_val != 0, even if the
2568 * level is disabled. Doing otherwise could cause underruns.
2570 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2571 WARN_ON(wm_lp != 1);
2572 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2574 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2577 /* LP0 register values */
2578 for_each_intel_crtc(dev, intel_crtc) {
2579 enum pipe pipe = intel_crtc->pipe;
2580 const struct intel_wm_level *r =
2581 &intel_crtc->wm.active.wm[0];
2583 if (WARN_ON(!r->enable))
2586 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2588 results->wm_pipe[pipe] =
2589 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2590 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2595 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
2596 * case both are at the same level. Prefer r1 in case they're the same. */
2597 static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2598 struct intel_pipe_wm *r1,
2599 struct intel_pipe_wm *r2)
2601 int level, max_level = ilk_wm_max_level(dev);
2602 int level1 = 0, level2 = 0;
2604 for (level = 1; level <= max_level; level++) {
2605 if (r1->wm[level].enable)
2607 if (r2->wm[level].enable)
2611 if (level1 == level2) {
2612 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2616 } else if (level1 > level2) {
2623 /* dirty bits used to track which watermarks need changes */
2624 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2625 #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2626 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2627 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2628 #define WM_DIRTY_FBC (1 << 24)
2629 #define WM_DIRTY_DDB (1 << 25)
2631 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2632 const struct ilk_wm_values *old,
2633 const struct ilk_wm_values *new)
2635 unsigned int dirty = 0;
2639 for_each_pipe(dev_priv, pipe) {
2640 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2641 dirty |= WM_DIRTY_LINETIME(pipe);
2642 /* Must disable LP1+ watermarks too */
2643 dirty |= WM_DIRTY_LP_ALL;
2646 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2647 dirty |= WM_DIRTY_PIPE(pipe);
2648 /* Must disable LP1+ watermarks too */
2649 dirty |= WM_DIRTY_LP_ALL;
2653 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2654 dirty |= WM_DIRTY_FBC;
2655 /* Must disable LP1+ watermarks too */
2656 dirty |= WM_DIRTY_LP_ALL;
2659 if (old->partitioning != new->partitioning) {
2660 dirty |= WM_DIRTY_DDB;
2661 /* Must disable LP1+ watermarks too */
2662 dirty |= WM_DIRTY_LP_ALL;
2665 /* LP1+ watermarks already deemed dirty, no need to continue */
2666 if (dirty & WM_DIRTY_LP_ALL)
2669 /* Find the lowest numbered LP1+ watermark in need of an update... */
2670 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2671 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2672 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2676 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2677 for (; wm_lp <= 3; wm_lp++)
2678 dirty |= WM_DIRTY_LP(wm_lp);
2683 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2686 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2687 bool changed = false;
2689 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2690 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2691 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2694 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2695 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2696 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2699 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2700 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2701 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2706 * Don't touch WM1S_LP_EN here.
2707 * Doing so could cause underruns.
2714 * The spec says we shouldn't write when we don't need, because every write
2715 * causes WMs to be re-evaluated, expending some power.
2717 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2718 struct ilk_wm_values *results)
2720 struct drm_device *dev = dev_priv->dev;
2721 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2725 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2729 _ilk_disable_lp_wm(dev_priv, dirty);
2731 if (dirty & WM_DIRTY_PIPE(PIPE_A))
2732 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2733 if (dirty & WM_DIRTY_PIPE(PIPE_B))
2734 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2735 if (dirty & WM_DIRTY_PIPE(PIPE_C))
2736 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2738 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2739 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2740 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2741 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2742 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2743 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2745 if (dirty & WM_DIRTY_DDB) {
2746 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2747 val = I915_READ(WM_MISC);
2748 if (results->partitioning == INTEL_DDB_PART_1_2)
2749 val &= ~WM_MISC_DATA_PARTITION_5_6;
2751 val |= WM_MISC_DATA_PARTITION_5_6;
2752 I915_WRITE(WM_MISC, val);
2754 val = I915_READ(DISP_ARB_CTL2);
2755 if (results->partitioning == INTEL_DDB_PART_1_2)
2756 val &= ~DISP_DATA_PARTITION_5_6;
2758 val |= DISP_DATA_PARTITION_5_6;
2759 I915_WRITE(DISP_ARB_CTL2, val);
2763 if (dirty & WM_DIRTY_FBC) {
2764 val = I915_READ(DISP_ARB_CTL);
2765 if (results->enable_fbc_wm)
2766 val &= ~DISP_FBC_WM_DIS;
2768 val |= DISP_FBC_WM_DIS;
2769 I915_WRITE(DISP_ARB_CTL, val);
2772 if (dirty & WM_DIRTY_LP(1) &&
2773 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2774 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2776 if (INTEL_INFO(dev)->gen >= 7) {
2777 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2778 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2779 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2780 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2783 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2784 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2785 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2786 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2787 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2788 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2790 dev_priv->wm.hw = *results;
2793 static bool ilk_disable_lp_wm(struct drm_device *dev)
2795 struct drm_i915_private *dev_priv = dev->dev_private;
2797 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2801 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2802 * different active planes.
2805 #define SKL_DDB_SIZE 896 /* in blocks */
2806 #define BXT_DDB_SIZE 512
2809 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2810 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2811 * other universal planes are in indices 1..n. Note that this may leave unused
2812 * indices between the top "sprite" plane and the cursor.
2815 skl_wm_plane_id(const struct intel_plane *plane)
2817 switch (plane->base.type) {
2818 case DRM_PLANE_TYPE_PRIMARY:
2820 case DRM_PLANE_TYPE_CURSOR:
2821 return PLANE_CURSOR;
2822 case DRM_PLANE_TYPE_OVERLAY:
2823 return plane->plane + 1;
2825 MISSING_CASE(plane->base.type);
2826 return plane->plane;
2831 skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2832 const struct intel_crtc_state *cstate,
2833 const struct intel_wm_config *config,
2834 struct skl_ddb_entry *alloc /* out */)
2836 struct drm_crtc *for_crtc = cstate->base.crtc;
2837 struct drm_crtc *crtc;
2838 unsigned int pipe_size, ddb_size;
2839 int nth_active_pipe;
2841 if (!cstate->base.active) {
2847 if (IS_BROXTON(dev))
2848 ddb_size = BXT_DDB_SIZE;
2850 ddb_size = SKL_DDB_SIZE;
2852 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2854 nth_active_pipe = 0;
2855 for_each_crtc(dev, crtc) {
2856 if (!to_intel_crtc(crtc)->active)
2859 if (crtc == for_crtc)
2865 pipe_size = ddb_size / config->num_pipes_active;
2866 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
2867 alloc->end = alloc->start + pipe_size;
2870 static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2872 if (config->num_pipes_active == 1)
2878 static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2880 entry->start = reg & 0x3ff;
2881 entry->end = (reg >> 16) & 0x3ff;
2886 void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2887 struct skl_ddb_allocation *ddb /* out */)
2893 for_each_pipe(dev_priv, pipe) {
2894 for_each_plane(dev_priv, pipe, plane) {
2895 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2896 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2900 val = I915_READ(CUR_BUF_CFG(pipe));
2901 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2907 skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2908 const struct drm_plane_state *pstate,
2911 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2912 struct drm_framebuffer *fb = pstate->fb;
2914 /* for planar format */
2915 if (fb->pixel_format == DRM_FORMAT_NV12) {
2916 if (y) /* y-plane data rate */
2917 return intel_crtc->config->pipe_src_w *
2918 intel_crtc->config->pipe_src_h *
2919 drm_format_plane_cpp(fb->pixel_format, 0);
2920 else /* uv-plane data rate */
2921 return (intel_crtc->config->pipe_src_w/2) *
2922 (intel_crtc->config->pipe_src_h/2) *
2923 drm_format_plane_cpp(fb->pixel_format, 1);
2926 /* for packed formats */
2927 return intel_crtc->config->pipe_src_w *
2928 intel_crtc->config->pipe_src_h *
2929 drm_format_plane_cpp(fb->pixel_format, 0);
2933 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2934 * a 8192x4096@32bpp framebuffer:
2935 * 3 * 4096 * 8192 * 4 < 2^32
2938 skl_get_total_relative_data_rate(const struct intel_crtc_state *cstate)
2940 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2941 struct drm_device *dev = intel_crtc->base.dev;
2942 const struct intel_plane *intel_plane;
2943 unsigned int total_data_rate = 0;
2945 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2946 const struct drm_plane_state *pstate = intel_plane->base.state;
2948 if (pstate->fb == NULL)
2952 total_data_rate += skl_plane_relative_data_rate(cstate,
2956 if (pstate->fb->pixel_format == DRM_FORMAT_NV12)
2958 total_data_rate += skl_plane_relative_data_rate(cstate,
2963 return total_data_rate;
2967 skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
2968 const struct intel_wm_config *config,
2969 struct skl_ddb_allocation *ddb /* out */)
2971 struct drm_crtc *crtc = cstate->base.crtc;
2972 struct drm_device *dev = crtc->dev;
2973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2974 struct intel_plane *intel_plane;
2975 enum pipe pipe = intel_crtc->pipe;
2976 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
2977 uint16_t alloc_size, start, cursor_blocks;
2978 uint16_t minimum[I915_MAX_PLANES];
2979 uint16_t y_minimum[I915_MAX_PLANES];
2980 unsigned int total_data_rate;
2982 skl_ddb_get_pipe_allocation_limits(dev, cstate, config, alloc);
2983 alloc_size = skl_ddb_entry_size(alloc);
2984 if (alloc_size == 0) {
2985 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2986 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
2987 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
2991 cursor_blocks = skl_cursor_allocation(config);
2992 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
2993 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
2995 alloc_size -= cursor_blocks;
2996 alloc->end -= cursor_blocks;
2998 /* 1. Allocate the mininum required blocks for each active plane */
2999 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3000 struct drm_plane *plane = &intel_plane->base;
3001 struct drm_framebuffer *fb = plane->fb;
3002 int id = skl_wm_plane_id(intel_plane);
3006 if (plane->type == DRM_PLANE_TYPE_CURSOR)
3010 alloc_size -= minimum[id];
3011 y_minimum[id] = (fb->pixel_format == DRM_FORMAT_NV12) ? 8 : 0;
3012 alloc_size -= y_minimum[id];
3016 * 2. Distribute the remaining space in proportion to the amount of
3017 * data each plane needs to fetch from memory.
3019 * FIXME: we may not allocate every single block here.
3021 total_data_rate = skl_get_total_relative_data_rate(cstate);
3023 start = alloc->start;
3024 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3025 struct drm_plane *plane = &intel_plane->base;
3026 struct drm_plane_state *pstate = intel_plane->base.state;
3027 unsigned int data_rate, y_data_rate;
3028 uint16_t plane_blocks, y_plane_blocks = 0;
3029 int id = skl_wm_plane_id(intel_plane);
3031 if (pstate->fb == NULL)
3033 if (plane->type == DRM_PLANE_TYPE_CURSOR)
3036 data_rate = skl_plane_relative_data_rate(cstate, pstate, 0);
3039 * allocation for (packed formats) or (uv-plane part of planar format):
3040 * promote the expression to 64 bits to avoid overflowing, the
3041 * result is < available as data_rate / total_data_rate < 1
3043 plane_blocks = minimum[id];
3044 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3047 ddb->plane[pipe][id].start = start;
3048 ddb->plane[pipe][id].end = start + plane_blocks;
3050 start += plane_blocks;
3053 * allocation for y_plane part of planar format:
3055 if (pstate->fb->pixel_format == DRM_FORMAT_NV12) {
3056 y_data_rate = skl_plane_relative_data_rate(cstate,
3059 y_plane_blocks = y_minimum[id];
3060 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3063 ddb->y_plane[pipe][id].start = start;
3064 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3066 start += y_plane_blocks;
3073 static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
3075 /* TODO: Take into account the scalers once we support them */
3076 return config->base.adjusted_mode.crtc_clock;
3080 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3081 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3082 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3083 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3085 static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3088 uint32_t wm_intermediate_val, ret;
3093 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
3094 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3099 static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3100 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
3101 uint64_t tiling, uint32_t latency)
3104 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3105 uint32_t wm_intermediate_val;
3110 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
3112 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3113 tiling == I915_FORMAT_MOD_Yf_TILED) {
3114 plane_bytes_per_line *= 4;
3115 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3116 plane_blocks_per_line /= 4;
3118 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3121 wm_intermediate_val = latency * pixel_rate;
3122 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3123 plane_blocks_per_line;
3128 static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3129 const struct intel_crtc *intel_crtc)
3131 struct drm_device *dev = intel_crtc->base.dev;
3132 struct drm_i915_private *dev_priv = dev->dev_private;
3133 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3134 enum pipe pipe = intel_crtc->pipe;
3136 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3137 sizeof(new_ddb->plane[pipe])))
3140 if (memcmp(&new_ddb->plane[pipe][PLANE_CURSOR], &cur_ddb->plane[pipe][PLANE_CURSOR],
3141 sizeof(new_ddb->plane[pipe][PLANE_CURSOR])))
3147 static void skl_compute_wm_global_parameters(struct drm_device *dev,
3148 struct intel_wm_config *config)
3150 struct drm_crtc *crtc;
3151 struct drm_plane *plane;
3153 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3154 config->num_pipes_active += to_intel_crtc(crtc)->active;
3156 /* FIXME: I don't think we need those two global parameters on SKL */
3157 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3158 struct intel_plane *intel_plane = to_intel_plane(plane);
3160 config->sprites_enabled |= intel_plane->wm.enabled;
3161 config->sprites_scaled |= intel_plane->wm.scaled;
3165 static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3166 struct intel_crtc_state *cstate,
3167 struct intel_plane *intel_plane,
3168 uint16_t ddb_allocation,
3170 uint16_t *out_blocks, /* out */
3171 uint8_t *out_lines /* out */)
3173 struct drm_plane *plane = &intel_plane->base;
3174 struct drm_framebuffer *fb = plane->state->fb;
3175 uint32_t latency = dev_priv->wm.skl_latency[level];
3176 uint32_t method1, method2;
3177 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3178 uint32_t res_blocks, res_lines;
3179 uint32_t selected_result;
3180 uint8_t bytes_per_pixel;
3182 if (latency == 0 || !cstate->base.active || !fb)
3185 bytes_per_pixel = (fb->pixel_format == DRM_FORMAT_NV12) ?
3186 drm_format_plane_cpp(DRM_FORMAT_NV12, 0) :
3187 drm_format_plane_cpp(DRM_FORMAT_NV12, 1);
3188 method1 = skl_wm_method1(skl_pipe_pixel_rate(cstate),
3191 method2 = skl_wm_method2(skl_pipe_pixel_rate(cstate),
3192 cstate->base.adjusted_mode.crtc_htotal,
3198 plane_bytes_per_line = cstate->pipe_src_w * bytes_per_pixel;
3199 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3201 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3202 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3203 uint32_t min_scanlines = 4;
3204 uint32_t y_tile_minimum;
3205 if (intel_rotation_90_or_270(plane->state->rotation)) {
3206 int bpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3207 drm_format_plane_cpp(fb->pixel_format, 1) :
3208 drm_format_plane_cpp(fb->pixel_format, 0);
3218 WARN(1, "Unsupported pixel depth for rotation");
3221 y_tile_minimum = plane_blocks_per_line * min_scanlines;
3222 selected_result = max(method2, y_tile_minimum);
3224 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3225 selected_result = min(method1, method2);
3227 selected_result = method1;
3230 res_blocks = selected_result + 1;
3231 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
3233 if (level >= 1 && level <= 7) {
3234 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3235 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
3241 if (res_blocks >= ddb_allocation || res_lines > 31)
3244 *out_blocks = res_blocks;
3245 *out_lines = res_lines;
3250 static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3251 struct skl_ddb_allocation *ddb,
3252 struct intel_crtc_state *cstate,
3254 struct skl_wm_level *result)
3256 struct drm_device *dev = dev_priv->dev;
3257 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3258 struct intel_plane *intel_plane;
3259 uint16_t ddb_blocks;
3260 enum pipe pipe = intel_crtc->pipe;
3262 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3263 int i = skl_wm_plane_id(intel_plane);
3265 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3267 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
3272 &result->plane_res_b[i],
3273 &result->plane_res_l[i]);
3278 skl_compute_linetime_wm(struct intel_crtc_state *cstate)
3280 if (!cstate->base.active)
3283 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
3286 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3287 skl_pipe_pixel_rate(cstate));
3290 static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
3291 struct skl_wm_level *trans_wm /* out */)
3293 struct drm_crtc *crtc = cstate->base.crtc;
3294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3295 struct intel_plane *intel_plane;
3297 if (!cstate->base.active)
3300 /* Until we know more, just disable transition WMs */
3301 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3302 int i = skl_wm_plane_id(intel_plane);
3304 trans_wm->plane_en[i] = false;
3308 static void skl_compute_pipe_wm(struct intel_crtc_state *cstate,
3309 struct skl_ddb_allocation *ddb,
3310 struct skl_pipe_wm *pipe_wm)
3312 struct drm_device *dev = cstate->base.crtc->dev;
3313 const struct drm_i915_private *dev_priv = dev->dev_private;
3314 int level, max_level = ilk_wm_max_level(dev);
3316 for (level = 0; level <= max_level; level++) {
3317 skl_compute_wm_level(dev_priv, ddb, cstate,
3318 level, &pipe_wm->wm[level]);
3320 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
3322 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
3325 static void skl_compute_wm_results(struct drm_device *dev,
3326 struct skl_pipe_wm *p_wm,
3327 struct skl_wm_values *r,
3328 struct intel_crtc *intel_crtc)
3330 int level, max_level = ilk_wm_max_level(dev);
3331 enum pipe pipe = intel_crtc->pipe;
3335 for (level = 0; level <= max_level; level++) {
3336 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3339 temp |= p_wm->wm[level].plane_res_l[i] <<
3340 PLANE_WM_LINES_SHIFT;
3341 temp |= p_wm->wm[level].plane_res_b[i];
3342 if (p_wm->wm[level].plane_en[i])
3343 temp |= PLANE_WM_EN;
3345 r->plane[pipe][i][level] = temp;
3350 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3351 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
3353 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
3354 temp |= PLANE_WM_EN;
3356 r->plane[pipe][PLANE_CURSOR][level] = temp;
3360 /* transition WMs */
3361 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3363 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3364 temp |= p_wm->trans_wm.plane_res_b[i];
3365 if (p_wm->trans_wm.plane_en[i])
3366 temp |= PLANE_WM_EN;
3368 r->plane_trans[pipe][i] = temp;
3372 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3373 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3374 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
3375 temp |= PLANE_WM_EN;
3377 r->plane_trans[pipe][PLANE_CURSOR] = temp;
3379 r->wm_linetime[pipe] = p_wm->linetime;
3382 static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3383 const struct skl_ddb_entry *entry)
3386 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3391 static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3392 const struct skl_wm_values *new)
3394 struct drm_device *dev = dev_priv->dev;
3395 struct intel_crtc *crtc;
3397 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3398 int i, level, max_level = ilk_wm_max_level(dev);
3399 enum pipe pipe = crtc->pipe;
3401 if (!new->dirty[pipe])
3404 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3406 for (level = 0; level <= max_level; level++) {
3407 for (i = 0; i < intel_num_planes(crtc); i++)
3408 I915_WRITE(PLANE_WM(pipe, i, level),
3409 new->plane[pipe][i][level]);
3410 I915_WRITE(CUR_WM(pipe, level),
3411 new->plane[pipe][PLANE_CURSOR][level]);
3413 for (i = 0; i < intel_num_planes(crtc); i++)
3414 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3415 new->plane_trans[pipe][i]);
3416 I915_WRITE(CUR_WM_TRANS(pipe),
3417 new->plane_trans[pipe][PLANE_CURSOR]);
3419 for (i = 0; i < intel_num_planes(crtc); i++) {
3420 skl_ddb_entry_write(dev_priv,
3421 PLANE_BUF_CFG(pipe, i),
3422 &new->ddb.plane[pipe][i]);
3423 skl_ddb_entry_write(dev_priv,
3424 PLANE_NV12_BUF_CFG(pipe, i),
3425 &new->ddb.y_plane[pipe][i]);
3428 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3429 &new->ddb.plane[pipe][PLANE_CURSOR]);
3434 * When setting up a new DDB allocation arrangement, we need to correctly
3435 * sequence the times at which the new allocations for the pipes are taken into
3436 * account or we'll have pipes fetching from space previously allocated to
3439 * Roughly the sequence looks like:
3440 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3441 * overlapping with a previous light-up pipe (another way to put it is:
3442 * pipes with their new allocation strickly included into their old ones).
3443 * 2. re-allocate the other pipes that get their allocation reduced
3444 * 3. allocate the pipes having their allocation increased
3446 * Steps 1. and 2. are here to take care of the following case:
3447 * - Initially DDB looks like this:
3450 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3454 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3458 skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3462 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3464 for_each_plane(dev_priv, pipe, plane) {
3465 I915_WRITE(PLANE_SURF(pipe, plane),
3466 I915_READ(PLANE_SURF(pipe, plane)));
3468 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3472 skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3473 const struct skl_ddb_allocation *new,
3476 uint16_t old_size, new_size;
3478 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3479 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3481 return old_size != new_size &&
3482 new->pipe[pipe].start >= old->pipe[pipe].start &&
3483 new->pipe[pipe].end <= old->pipe[pipe].end;
3486 static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3487 struct skl_wm_values *new_values)
3489 struct drm_device *dev = dev_priv->dev;
3490 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3491 bool reallocated[I915_MAX_PIPES] = {};
3492 struct intel_crtc *crtc;
3495 new_ddb = &new_values->ddb;
3496 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3499 * First pass: flush the pipes with the new allocation contained into
3502 * We'll wait for the vblank on those pipes to ensure we can safely
3503 * re-allocate the freed space without this pipe fetching from it.
3505 for_each_intel_crtc(dev, crtc) {
3511 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3514 skl_wm_flush_pipe(dev_priv, pipe, 1);
3515 intel_wait_for_vblank(dev, pipe);
3517 reallocated[pipe] = true;
3522 * Second pass: flush the pipes that are having their allocation
3523 * reduced, but overlapping with a previous allocation.
3525 * Here as well we need to wait for the vblank to make sure the freed
3526 * space is not used anymore.
3528 for_each_intel_crtc(dev, crtc) {
3534 if (reallocated[pipe])
3537 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3538 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3539 skl_wm_flush_pipe(dev_priv, pipe, 2);
3540 intel_wait_for_vblank(dev, pipe);
3541 reallocated[pipe] = true;
3546 * Third pass: flush the pipes that got more space allocated.
3548 * We don't need to actively wait for the update here, next vblank
3549 * will just get more DDB space with the correct WM values.
3551 for_each_intel_crtc(dev, crtc) {
3558 * At this point, only the pipes more space than before are
3559 * left to re-allocate.
3561 if (reallocated[pipe])
3564 skl_wm_flush_pipe(dev_priv, pipe, 3);
3568 static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3569 struct intel_wm_config *config,
3570 struct skl_ddb_allocation *ddb, /* out */
3571 struct skl_pipe_wm *pipe_wm /* out */)
3573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3574 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3576 skl_allocate_pipe_ddb(cstate, config, ddb);
3577 skl_compute_pipe_wm(cstate, ddb, pipe_wm);
3579 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3582 intel_crtc->wm.skl_active = *pipe_wm;
3587 static void skl_update_other_pipe_wm(struct drm_device *dev,
3588 struct drm_crtc *crtc,
3589 struct intel_wm_config *config,
3590 struct skl_wm_values *r)
3592 struct intel_crtc *intel_crtc;
3593 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3596 * If the WM update hasn't changed the allocation for this_crtc (the
3597 * crtc we are currently computing the new WM values for), other
3598 * enabled crtcs will keep the same allocation and we don't need to
3599 * recompute anything for them.
3601 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3605 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3606 * other active pipes need new DDB allocation and WM values.
3608 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3610 struct skl_pipe_wm pipe_wm = {};
3613 if (this_crtc->pipe == intel_crtc->pipe)
3616 if (!intel_crtc->active)
3619 wm_changed = skl_update_pipe_wm(&intel_crtc->base, config,
3623 * If we end up re-computing the other pipe WM values, it's
3624 * because it was really needed, so we expect the WM values to
3627 WARN_ON(!wm_changed);
3629 skl_compute_wm_results(dev, &pipe_wm, r, intel_crtc);
3630 r->dirty[intel_crtc->pipe] = true;
3634 static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3636 watermarks->wm_linetime[pipe] = 0;
3637 memset(watermarks->plane[pipe], 0,
3638 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
3639 memset(watermarks->plane_trans[pipe],
3640 0, sizeof(uint32_t) * I915_MAX_PLANES);
3641 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
3643 /* Clear ddb entries for pipe */
3644 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3645 memset(&watermarks->ddb.plane[pipe], 0,
3646 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3647 memset(&watermarks->ddb.y_plane[pipe], 0,
3648 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3649 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3650 sizeof(struct skl_ddb_entry));
3654 static void skl_update_wm(struct drm_crtc *crtc)
3656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3657 struct drm_device *dev = crtc->dev;
3658 struct drm_i915_private *dev_priv = dev->dev_private;
3659 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3660 struct skl_pipe_wm pipe_wm = {};
3661 struct intel_wm_config config = {};
3664 /* Clear all dirty flags */
3665 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3667 skl_clear_wm(results, intel_crtc->pipe);
3669 skl_compute_wm_global_parameters(dev, &config);
3671 if (!skl_update_pipe_wm(crtc, &config, &results->ddb, &pipe_wm))
3674 skl_compute_wm_results(dev, &pipe_wm, results, intel_crtc);
3675 results->dirty[intel_crtc->pipe] = true;
3677 skl_update_other_pipe_wm(dev, crtc, &config, results);
3678 skl_write_wm_values(dev_priv, results);
3679 skl_flush_wm_values(dev_priv, results);
3681 /* store the new configuration */
3682 dev_priv->wm.skl_hw = *results;
3686 skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3687 uint32_t sprite_width, uint32_t sprite_height,
3688 int pixel_size, bool enabled, bool scaled)
3690 struct intel_plane *intel_plane = to_intel_plane(plane);
3691 struct drm_framebuffer *fb = plane->state->fb;
3693 intel_plane->wm.enabled = enabled;
3694 intel_plane->wm.scaled = scaled;
3695 intel_plane->wm.horiz_pixels = sprite_width;
3696 intel_plane->wm.vert_pixels = sprite_height;
3697 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3699 /* For planar: Bpp is for UV plane, y_Bpp is for Y plane */
3700 intel_plane->wm.bytes_per_pixel =
3701 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3702 drm_format_plane_cpp(plane->state->fb->pixel_format, 1) : pixel_size;
3703 intel_plane->wm.y_bytes_per_pixel =
3704 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3705 drm_format_plane_cpp(plane->state->fb->pixel_format, 0) : 0;
3708 * Framebuffer can be NULL on plane disable, but it does not
3709 * matter for watermarks if we assume no tiling in that case.
3712 intel_plane->wm.tiling = fb->modifier[0];
3713 intel_plane->wm.rotation = plane->state->rotation;
3715 skl_update_wm(crtc);
3718 static void ilk_update_wm(struct drm_crtc *crtc)
3720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3721 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3722 struct drm_device *dev = crtc->dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 struct ilk_wm_maximums max;
3725 struct ilk_wm_values results = {};
3726 enum intel_ddb_partitioning partitioning;
3727 struct intel_pipe_wm pipe_wm = {};
3728 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3729 struct intel_wm_config config = {};
3731 WARN_ON(cstate->base.active != intel_crtc->active);
3734 * IVB workaround: must disable low power watermarks for at least
3735 * one frame before enabling scaling. LP watermarks can be re-enabled
3736 * when scaling is disabled.
3738 * WaCxSRDisabledForSpriteScaling:ivb
3740 if (cstate->disable_lp_wm) {
3741 ilk_disable_lp_wm(dev);
3742 intel_wait_for_vblank(dev, intel_crtc->pipe);
3745 intel_compute_pipe_wm(cstate, &pipe_wm);
3747 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3750 intel_crtc->wm.active = pipe_wm;
3752 ilk_compute_wm_config(dev, &config);
3754 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3755 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3757 /* 5/6 split only in single pipe config on IVB+ */
3758 if (INTEL_INFO(dev)->gen >= 7 &&
3759 config.num_pipes_active == 1 && config.sprites_enabled) {
3760 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3761 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3763 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3765 best_lp_wm = &lp_wm_1_2;
3768 partitioning = (best_lp_wm == &lp_wm_1_2) ?
3769 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3771 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3773 ilk_write_wm_values(dev_priv, &results);
3776 static void skl_pipe_wm_active_state(uint32_t val,
3777 struct skl_pipe_wm *active,
3783 bool is_enabled = (val & PLANE_WM_EN) != 0;
3787 active->wm[level].plane_en[i] = is_enabled;
3788 active->wm[level].plane_res_b[i] =
3789 val & PLANE_WM_BLOCKS_MASK;
3790 active->wm[level].plane_res_l[i] =
3791 (val >> PLANE_WM_LINES_SHIFT) &
3792 PLANE_WM_LINES_MASK;
3794 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3795 active->wm[level].plane_res_b[PLANE_CURSOR] =
3796 val & PLANE_WM_BLOCKS_MASK;
3797 active->wm[level].plane_res_l[PLANE_CURSOR] =
3798 (val >> PLANE_WM_LINES_SHIFT) &
3799 PLANE_WM_LINES_MASK;
3803 active->trans_wm.plane_en[i] = is_enabled;
3804 active->trans_wm.plane_res_b[i] =
3805 val & PLANE_WM_BLOCKS_MASK;
3806 active->trans_wm.plane_res_l[i] =
3807 (val >> PLANE_WM_LINES_SHIFT) &
3808 PLANE_WM_LINES_MASK;
3810 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3811 active->trans_wm.plane_res_b[PLANE_CURSOR] =
3812 val & PLANE_WM_BLOCKS_MASK;
3813 active->trans_wm.plane_res_l[PLANE_CURSOR] =
3814 (val >> PLANE_WM_LINES_SHIFT) &
3815 PLANE_WM_LINES_MASK;
3820 static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3822 struct drm_device *dev = crtc->dev;
3823 struct drm_i915_private *dev_priv = dev->dev_private;
3824 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3826 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3827 enum pipe pipe = intel_crtc->pipe;
3828 int level, i, max_level;
3831 max_level = ilk_wm_max_level(dev);
3833 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3835 for (level = 0; level <= max_level; level++) {
3836 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3837 hw->plane[pipe][i][level] =
3838 I915_READ(PLANE_WM(pipe, i, level));
3839 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
3842 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3843 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3844 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
3846 if (!intel_crtc->active)
3849 hw->dirty[pipe] = true;
3851 active->linetime = hw->wm_linetime[pipe];
3853 for (level = 0; level <= max_level; level++) {
3854 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3855 temp = hw->plane[pipe][i][level];
3856 skl_pipe_wm_active_state(temp, active, false,
3859 temp = hw->plane[pipe][PLANE_CURSOR][level];
3860 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3863 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3864 temp = hw->plane_trans[pipe][i];
3865 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3868 temp = hw->plane_trans[pipe][PLANE_CURSOR];
3869 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3872 void skl_wm_get_hw_state(struct drm_device *dev)
3874 struct drm_i915_private *dev_priv = dev->dev_private;
3875 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3876 struct drm_crtc *crtc;
3878 skl_ddb_get_hw_state(dev_priv, ddb);
3879 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3880 skl_pipe_wm_get_hw_state(crtc);
3883 static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3885 struct drm_device *dev = crtc->dev;
3886 struct drm_i915_private *dev_priv = dev->dev_private;
3887 struct ilk_wm_values *hw = &dev_priv->wm.hw;
3888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3889 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3890 enum pipe pipe = intel_crtc->pipe;
3891 static const unsigned int wm0_pipe_reg[] = {
3892 [PIPE_A] = WM0_PIPEA_ILK,
3893 [PIPE_B] = WM0_PIPEB_ILK,
3894 [PIPE_C] = WM0_PIPEC_IVB,
3897 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3898 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3899 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3901 active->pipe_enabled = intel_crtc->active;
3903 if (active->pipe_enabled) {
3904 u32 tmp = hw->wm_pipe[pipe];
3907 * For active pipes LP0 watermark is marked as
3908 * enabled, and LP1+ watermaks as disabled since
3909 * we can't really reverse compute them in case
3910 * multiple pipes are active.
3912 active->wm[0].enable = true;
3913 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3914 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3915 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3916 active->linetime = hw->wm_linetime[pipe];
3918 int level, max_level = ilk_wm_max_level(dev);
3921 * For inactive pipes, all watermark levels
3922 * should be marked as enabled but zeroed,
3923 * which is what we'd compute them to.
3925 for (level = 0; level <= max_level; level++)
3926 active->wm[level].enable = true;
3930 #define _FW_WM(value, plane) \
3931 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3932 #define _FW_WM_VLV(value, plane) \
3933 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3935 static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3936 struct vlv_wm_values *wm)
3941 for_each_pipe(dev_priv, pipe) {
3942 tmp = I915_READ(VLV_DDL(pipe));
3944 wm->ddl[pipe].primary =
3945 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3946 wm->ddl[pipe].cursor =
3947 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3948 wm->ddl[pipe].sprite[0] =
3949 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3950 wm->ddl[pipe].sprite[1] =
3951 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3954 tmp = I915_READ(DSPFW1);
3955 wm->sr.plane = _FW_WM(tmp, SR);
3956 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3957 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3958 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3960 tmp = I915_READ(DSPFW2);
3961 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3962 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3963 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3965 tmp = I915_READ(DSPFW3);
3966 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3968 if (IS_CHERRYVIEW(dev_priv)) {
3969 tmp = I915_READ(DSPFW7_CHV);
3970 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3971 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3973 tmp = I915_READ(DSPFW8_CHV);
3974 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3975 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
3977 tmp = I915_READ(DSPFW9_CHV);
3978 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
3979 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
3981 tmp = I915_READ(DSPHOWM);
3982 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3983 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3984 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3985 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
3986 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3987 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3988 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3989 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3990 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3991 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
3993 tmp = I915_READ(DSPFW7);
3994 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3995 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3997 tmp = I915_READ(DSPHOWM);
3998 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3999 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4000 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4001 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4002 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4003 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4004 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4011 void vlv_wm_get_hw_state(struct drm_device *dev)
4013 struct drm_i915_private *dev_priv = to_i915(dev);
4014 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4015 struct intel_plane *plane;
4019 vlv_read_wm_values(dev_priv, wm);
4021 for_each_intel_plane(dev, plane) {
4022 switch (plane->base.type) {
4024 case DRM_PLANE_TYPE_CURSOR:
4025 plane->wm.fifo_size = 63;
4027 case DRM_PLANE_TYPE_PRIMARY:
4028 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4030 case DRM_PLANE_TYPE_OVERLAY:
4031 sprite = plane->plane;
4032 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4037 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4038 wm->level = VLV_WM_LEVEL_PM2;
4040 if (IS_CHERRYVIEW(dev_priv)) {
4041 mutex_lock(&dev_priv->rps.hw_lock);
4043 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4044 if (val & DSP_MAXFIFO_PM5_ENABLE)
4045 wm->level = VLV_WM_LEVEL_PM5;
4048 * If DDR DVFS is disabled in the BIOS, Punit
4049 * will never ack the request. So if that happens
4050 * assume we don't have to enable/disable DDR DVFS
4051 * dynamically. To test that just set the REQ_ACK
4052 * bit to poke the Punit, but don't change the
4053 * HIGH/LOW bits so that we don't actually change
4054 * the current state.
4056 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4057 val |= FORCE_DDR_FREQ_REQ_ACK;
4058 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4060 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4061 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4062 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4063 "assuming DDR DVFS is disabled\n");
4064 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4066 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4067 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4068 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4071 mutex_unlock(&dev_priv->rps.hw_lock);
4074 for_each_pipe(dev_priv, pipe)
4075 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4076 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4077 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4079 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4080 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4083 void ilk_wm_get_hw_state(struct drm_device *dev)
4085 struct drm_i915_private *dev_priv = dev->dev_private;
4086 struct ilk_wm_values *hw = &dev_priv->wm.hw;
4087 struct drm_crtc *crtc;
4089 for_each_crtc(dev, crtc)
4090 ilk_pipe_wm_get_hw_state(crtc);
4092 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4093 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4094 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4096 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
4097 if (INTEL_INFO(dev)->gen >= 7) {
4098 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4099 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4102 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4103 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4104 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4105 else if (IS_IVYBRIDGE(dev))
4106 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4107 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4110 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4114 * intel_update_watermarks - update FIFO watermark values based on current modes
4116 * Calculate watermark values for the various WM regs based on current mode
4117 * and plane configuration.
4119 * There are several cases to deal with here:
4120 * - normal (i.e. non-self-refresh)
4121 * - self-refresh (SR) mode
4122 * - lines are large relative to FIFO size (buffer can hold up to 2)
4123 * - lines are small relative to FIFO size (buffer can hold more than 2
4124 * lines), so need to account for TLB latency
4126 * The normal calculation is:
4127 * watermark = dotclock * bytes per pixel * latency
4128 * where latency is platform & configuration dependent (we assume pessimal
4131 * The SR calculation is:
4132 * watermark = (trunc(latency/line time)+1) * surface width *
4135 * line time = htotal / dotclock
4136 * surface width = hdisplay for normal plane and 64 for cursor
4137 * and latency is assumed to be high, as above.
4139 * The final value programmed to the register should always be rounded up,
4140 * and include an extra 2 entries to account for clock crossings.
4142 * We don't use the sprite, so we can ignore that. And on Crestline we have
4143 * to set the non-SR watermarks to 8.
4145 void intel_update_watermarks(struct drm_crtc *crtc)
4147 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4149 if (dev_priv->display.update_wm)
4150 dev_priv->display.update_wm(crtc);
4153 void intel_update_sprite_watermarks(struct drm_plane *plane,
4154 struct drm_crtc *crtc,
4155 uint32_t sprite_width,
4156 uint32_t sprite_height,
4158 bool enabled, bool scaled)
4160 struct drm_i915_private *dev_priv = plane->dev->dev_private;
4162 if (dev_priv->display.update_sprite_wm)
4163 dev_priv->display.update_sprite_wm(plane, crtc,
4164 sprite_width, sprite_height,
4165 pixel_size, enabled, scaled);
4169 * Lock protecting IPS related data structures
4171 DEFINE_SPINLOCK(mchdev_lock);
4173 /* Global for IPS driver to get at the current i915 device. Protected by
4175 static struct drm_i915_private *i915_mch_dev;
4177 bool ironlake_set_drps(struct drm_device *dev, u8 val)
4179 struct drm_i915_private *dev_priv = dev->dev_private;
4182 assert_spin_locked(&mchdev_lock);
4184 rgvswctl = I915_READ16(MEMSWCTL);
4185 if (rgvswctl & MEMCTL_CMD_STS) {
4186 DRM_DEBUG("gpu busy, RCS change rejected\n");
4187 return false; /* still busy with another command */
4190 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4191 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4192 I915_WRITE16(MEMSWCTL, rgvswctl);
4193 POSTING_READ16(MEMSWCTL);
4195 rgvswctl |= MEMCTL_CMD_STS;
4196 I915_WRITE16(MEMSWCTL, rgvswctl);
4201 static void ironlake_enable_drps(struct drm_device *dev)
4203 struct drm_i915_private *dev_priv = dev->dev_private;
4204 u32 rgvmodectl = I915_READ(MEMMODECTL);
4205 u8 fmax, fmin, fstart, vstart;
4207 spin_lock_irq(&mchdev_lock);
4209 /* Enable temp reporting */
4210 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4211 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4213 /* 100ms RC evaluation intervals */
4214 I915_WRITE(RCUPEI, 100000);
4215 I915_WRITE(RCDNEI, 100000);
4217 /* Set max/min thresholds to 90ms and 80ms respectively */
4218 I915_WRITE(RCBMAXAVG, 90000);
4219 I915_WRITE(RCBMINAVG, 80000);
4221 I915_WRITE(MEMIHYST, 1);
4223 /* Set up min, max, and cur for interrupt handling */
4224 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4225 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4226 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4227 MEMMODE_FSTART_SHIFT;
4229 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
4232 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4233 dev_priv->ips.fstart = fstart;
4235 dev_priv->ips.max_delay = fstart;
4236 dev_priv->ips.min_delay = fmin;
4237 dev_priv->ips.cur_delay = fstart;
4239 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4240 fmax, fmin, fstart);
4242 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4245 * Interrupts will be enabled in ironlake_irq_postinstall
4248 I915_WRITE(VIDSTART, vstart);
4249 POSTING_READ(VIDSTART);
4251 rgvmodectl |= MEMMODE_SWMODE_EN;
4252 I915_WRITE(MEMMODECTL, rgvmodectl);
4254 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
4255 DRM_ERROR("stuck trying to change perf mode\n");
4258 ironlake_set_drps(dev, fstart);
4260 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4261 I915_READ(DDREC) + I915_READ(CSIEC);
4262 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4263 dev_priv->ips.last_count2 = I915_READ(GFXEC);
4264 dev_priv->ips.last_time2 = ktime_get_raw_ns();
4266 spin_unlock_irq(&mchdev_lock);
4269 static void ironlake_disable_drps(struct drm_device *dev)
4271 struct drm_i915_private *dev_priv = dev->dev_private;
4274 spin_lock_irq(&mchdev_lock);
4276 rgvswctl = I915_READ16(MEMSWCTL);
4278 /* Ack interrupts, disable EFC interrupt */
4279 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4280 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4281 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4282 I915_WRITE(DEIIR, DE_PCU_EVENT);
4283 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4285 /* Go back to the starting frequency */
4286 ironlake_set_drps(dev, dev_priv->ips.fstart);
4288 rgvswctl |= MEMCTL_CMD_STS;
4289 I915_WRITE(MEMSWCTL, rgvswctl);
4292 spin_unlock_irq(&mchdev_lock);
4295 /* There's a funny hw issue where the hw returns all 0 when reading from
4296 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4297 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4298 * all limits and the gpu stuck at whatever frequency it is at atm).
4300 static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
4304 /* Only set the down limit when we've reached the lowest level to avoid
4305 * getting more interrupts, otherwise leave this clear. This prevents a
4306 * race in the hw when coming out of rc6: There's a tiny window where
4307 * the hw runs at the minimal clock before selecting the desired
4308 * frequency, if the down threshold expires in that window we will not
4309 * receive a down interrupt. */
4310 if (IS_GEN9(dev_priv->dev)) {
4311 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4312 if (val <= dev_priv->rps.min_freq_softlimit)
4313 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4315 limits = dev_priv->rps.max_freq_softlimit << 24;
4316 if (val <= dev_priv->rps.min_freq_softlimit)
4317 limits |= dev_priv->rps.min_freq_softlimit << 16;
4323 static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4326 u32 threshold_up = 0, threshold_down = 0; /* in % */
4327 u32 ei_up = 0, ei_down = 0;
4329 new_power = dev_priv->rps.power;
4330 switch (dev_priv->rps.power) {
4332 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
4333 new_power = BETWEEN;
4337 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
4338 new_power = LOW_POWER;
4339 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
4340 new_power = HIGH_POWER;
4344 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
4345 new_power = BETWEEN;
4348 /* Max/min bins are special */
4349 if (val <= dev_priv->rps.min_freq_softlimit)
4350 new_power = LOW_POWER;
4351 if (val >= dev_priv->rps.max_freq_softlimit)
4352 new_power = HIGH_POWER;
4353 if (new_power == dev_priv->rps.power)
4356 /* Note the units here are not exactly 1us, but 1280ns. */
4357 switch (new_power) {
4359 /* Upclock if more than 95% busy over 16ms */
4363 /* Downclock if less than 85% busy over 32ms */
4365 threshold_down = 85;
4369 /* Upclock if more than 90% busy over 13ms */
4373 /* Downclock if less than 75% busy over 32ms */
4375 threshold_down = 75;
4379 /* Upclock if more than 85% busy over 10ms */
4383 /* Downclock if less than 60% busy over 32ms */
4385 threshold_down = 60;
4389 I915_WRITE(GEN6_RP_UP_EI,
4390 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4391 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4392 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4394 I915_WRITE(GEN6_RP_DOWN_EI,
4395 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4396 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4397 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4399 I915_WRITE(GEN6_RP_CONTROL,
4400 GEN6_RP_MEDIA_TURBO |
4401 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4402 GEN6_RP_MEDIA_IS_GFX |
4404 GEN6_RP_UP_BUSY_AVG |
4405 GEN6_RP_DOWN_IDLE_AVG);
4407 dev_priv->rps.power = new_power;
4408 dev_priv->rps.up_threshold = threshold_up;
4409 dev_priv->rps.down_threshold = threshold_down;
4410 dev_priv->rps.last_adj = 0;
4413 static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4417 if (val > dev_priv->rps.min_freq_softlimit)
4418 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4419 if (val < dev_priv->rps.max_freq_softlimit)
4420 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
4422 mask &= dev_priv->pm_rps_events;
4424 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
4427 /* gen6_set_rps is called to update the frequency request, but should also be
4428 * called when the range (min_delay and max_delay) is modified so that we can
4429 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
4430 static void gen6_set_rps(struct drm_device *dev, u8 val)
4432 struct drm_i915_private *dev_priv = dev->dev_private;
4434 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4435 if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0))
4438 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4439 WARN_ON(val > dev_priv->rps.max_freq);
4440 WARN_ON(val < dev_priv->rps.min_freq);
4442 /* min/max delay may still have been modified so be sure to
4443 * write the limits value.
4445 if (val != dev_priv->rps.cur_freq) {
4446 gen6_set_rps_thresholds(dev_priv, val);
4449 I915_WRITE(GEN6_RPNSWREQ,
4450 GEN9_FREQUENCY(val));
4451 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4452 I915_WRITE(GEN6_RPNSWREQ,
4453 HSW_FREQUENCY(val));
4455 I915_WRITE(GEN6_RPNSWREQ,
4456 GEN6_FREQUENCY(val) |
4458 GEN6_AGGRESSIVE_TURBO);
4461 /* Make sure we continue to get interrupts
4462 * until we hit the minimum or maximum frequencies.
4464 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
4465 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4467 POSTING_READ(GEN6_RPNSWREQ);
4469 dev_priv->rps.cur_freq = val;
4470 trace_intel_gpu_freq_change(val * 50);
4473 static void valleyview_set_rps(struct drm_device *dev, u8 val)
4475 struct drm_i915_private *dev_priv = dev->dev_private;
4477 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4478 WARN_ON(val > dev_priv->rps.max_freq);
4479 WARN_ON(val < dev_priv->rps.min_freq);
4481 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4482 "Odd GPU freq value\n"))
4485 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4487 if (val != dev_priv->rps.cur_freq) {
4488 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4489 if (!IS_CHERRYVIEW(dev_priv))
4490 gen6_set_rps_thresholds(dev_priv, val);
4493 dev_priv->rps.cur_freq = val;
4494 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4497 /* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
4499 * * If Gfx is Idle, then
4500 * 1. Forcewake Media well.
4501 * 2. Request idle freq.
4502 * 3. Release Forcewake of Media well.
4504 static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4506 u32 val = dev_priv->rps.idle_freq;
4508 if (dev_priv->rps.cur_freq <= val)
4511 /* Wake up the media well, as that takes a lot less
4512 * power than the Render well. */
4513 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4514 valleyview_set_rps(dev_priv->dev, val);
4515 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
4518 void gen6_rps_busy(struct drm_i915_private *dev_priv)
4520 mutex_lock(&dev_priv->rps.hw_lock);
4521 if (dev_priv->rps.enabled) {
4522 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4523 gen6_rps_reset_ei(dev_priv);
4524 I915_WRITE(GEN6_PMINTRMSK,
4525 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4527 mutex_unlock(&dev_priv->rps.hw_lock);
4530 void gen6_rps_idle(struct drm_i915_private *dev_priv)
4532 struct drm_device *dev = dev_priv->dev;
4534 mutex_lock(&dev_priv->rps.hw_lock);
4535 if (dev_priv->rps.enabled) {
4536 if (IS_VALLEYVIEW(dev))
4537 vlv_set_rps_idle(dev_priv);
4539 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4540 dev_priv->rps.last_adj = 0;
4541 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
4543 mutex_unlock(&dev_priv->rps.hw_lock);
4545 spin_lock(&dev_priv->rps.client_lock);
4546 while (!list_empty(&dev_priv->rps.clients))
4547 list_del_init(dev_priv->rps.clients.next);
4548 spin_unlock(&dev_priv->rps.client_lock);
4551 void gen6_rps_boost(struct drm_i915_private *dev_priv,
4552 struct intel_rps_client *rps,
4553 unsigned long submitted)
4555 /* This is intentionally racy! We peek at the state here, then
4556 * validate inside the RPS worker.
4558 if (!(dev_priv->mm.busy &&
4559 dev_priv->rps.enabled &&
4560 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4563 /* Force a RPS boost (and don't count it against the client) if
4564 * the GPU is severely congested.
4566 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
4569 spin_lock(&dev_priv->rps.client_lock);
4570 if (rps == NULL || list_empty(&rps->link)) {
4571 spin_lock_irq(&dev_priv->irq_lock);
4572 if (dev_priv->rps.interrupts_enabled) {
4573 dev_priv->rps.client_boost = true;
4574 queue_work(dev_priv->wq, &dev_priv->rps.work);
4576 spin_unlock_irq(&dev_priv->irq_lock);
4579 list_add(&rps->link, &dev_priv->rps.clients);
4582 dev_priv->rps.boosts++;
4584 spin_unlock(&dev_priv->rps.client_lock);
4587 void intel_set_rps(struct drm_device *dev, u8 val)
4589 if (IS_VALLEYVIEW(dev))
4590 valleyview_set_rps(dev, val);
4592 gen6_set_rps(dev, val);
4595 static void gen9_disable_rps(struct drm_device *dev)
4597 struct drm_i915_private *dev_priv = dev->dev_private;
4599 I915_WRITE(GEN6_RC_CONTROL, 0);
4600 I915_WRITE(GEN9_PG_ENABLE, 0);
4603 static void gen6_disable_rps(struct drm_device *dev)
4605 struct drm_i915_private *dev_priv = dev->dev_private;
4607 I915_WRITE(GEN6_RC_CONTROL, 0);
4608 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
4611 static void cherryview_disable_rps(struct drm_device *dev)
4613 struct drm_i915_private *dev_priv = dev->dev_private;
4615 I915_WRITE(GEN6_RC_CONTROL, 0);
4618 static void valleyview_disable_rps(struct drm_device *dev)
4620 struct drm_i915_private *dev_priv = dev->dev_private;
4622 /* we're doing forcewake before Disabling RC6,
4623 * This what the BIOS expects when going into suspend */
4624 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4626 I915_WRITE(GEN6_RC_CONTROL, 0);
4628 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4631 static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4633 if (IS_VALLEYVIEW(dev)) {
4634 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4635 mode = GEN6_RC_CTL_RC6_ENABLE;
4640 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4641 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4642 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4643 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4646 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4647 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
4650 static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4652 /* No RC6 before Ironlake and code is gone for ilk. */
4653 if (INTEL_INFO(dev)->gen < 6)
4656 /* Respect the kernel parameter if it is set */
4657 if (enable_rc6 >= 0) {
4661 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4664 mask = INTEL_RC6_ENABLE;
4666 if ((enable_rc6 & mask) != enable_rc6)
4667 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4668 enable_rc6 & mask, enable_rc6, mask);
4670 return enable_rc6 & mask;
4673 if (IS_IVYBRIDGE(dev))
4674 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4676 return INTEL_RC6_ENABLE;
4679 int intel_enable_rc6(const struct drm_device *dev)
4681 return i915.enable_rc6;
4684 static void gen6_init_rps_frequencies(struct drm_device *dev)
4686 struct drm_i915_private *dev_priv = dev->dev_private;
4687 uint32_t rp_state_cap;
4688 u32 ddcc_status = 0;
4691 /* All of these values are in units of 50MHz */
4692 dev_priv->rps.cur_freq = 0;
4693 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
4694 if (IS_BROXTON(dev)) {
4695 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4696 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4697 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4698 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4700 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4701 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4702 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4703 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4706 /* hw_max = RP0 until we check for overclocking */
4707 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4709 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4710 if (IS_HASWELL(dev) || IS_BROADWELL(dev) || IS_SKYLAKE(dev)) {
4711 ret = sandybridge_pcode_read(dev_priv,
4712 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4715 dev_priv->rps.efficient_freq =
4717 ((ddcc_status >> 8) & 0xff),
4718 dev_priv->rps.min_freq,
4719 dev_priv->rps.max_freq);
4722 if (IS_SKYLAKE(dev)) {
4723 /* Store the frequency values in 16.66 MHZ units, which is
4724 the natural hardware unit for SKL */
4725 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4726 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4727 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4728 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4729 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4732 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4734 /* Preserve min/max settings in case of re-init */
4735 if (dev_priv->rps.max_freq_softlimit == 0)
4736 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4738 if (dev_priv->rps.min_freq_softlimit == 0) {
4739 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4740 dev_priv->rps.min_freq_softlimit =
4741 max_t(int, dev_priv->rps.efficient_freq,
4742 intel_freq_opcode(dev_priv, 450));
4744 dev_priv->rps.min_freq_softlimit =
4745 dev_priv->rps.min_freq;
4749 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
4750 static void gen9_enable_rps(struct drm_device *dev)
4752 struct drm_i915_private *dev_priv = dev->dev_private;
4754 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4756 gen6_init_rps_frequencies(dev);
4758 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4759 if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) {
4760 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4764 /* Program defaults and thresholds for RPS*/
4765 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4766 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
4768 /* 1 second timeout*/
4769 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4770 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4772 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4774 /* Leaning on the below call to gen6_set_rps to program/setup the
4775 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4776 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4777 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4778 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4780 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4783 static void gen9_enable_rc6(struct drm_device *dev)
4785 struct drm_i915_private *dev_priv = dev->dev_private;
4786 struct intel_engine_cs *ring;
4787 uint32_t rc6_mask = 0;
4790 /* 1a: Software RC state - RC0 */
4791 I915_WRITE(GEN6_RC_STATE, 0);
4793 /* 1b: Get forcewake during program sequence. Although the driver
4794 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4795 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4797 /* 2a: Disable RC states. */
4798 I915_WRITE(GEN6_RC_CONTROL, 0);
4800 /* 2b: Program RC6 thresholds.*/
4802 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
4803 if (IS_SKYLAKE(dev) && !((IS_SKL_GT3(dev) || IS_SKL_GT4(dev)) &&
4804 (INTEL_REVID(dev) <= SKL_REVID_E0)))
4805 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4807 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4808 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4809 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4810 for_each_ring(ring, dev_priv, unused)
4811 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4813 if (HAS_GUC_UCODE(dev))
4814 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4816 I915_WRITE(GEN6_RC_SLEEP, 0);
4817 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4819 /* 2c: Program Coarse Power Gating Policies. */
4820 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4821 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4823 /* 3a: Enable RC6 */
4824 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4825 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4826 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4829 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_D0) ||
4830 (IS_BROXTON(dev) && INTEL_REVID(dev) <= BXT_REVID_A0))
4831 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4832 GEN7_RC_CTL_TO_MODE |
4835 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4836 GEN6_RC_CTL_EI_MODE(1) |
4840 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
4841 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
4843 if ((IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) ||
4844 ((IS_SKL_GT3(dev) || IS_SKL_GT4(dev)) && (INTEL_REVID(dev) <= SKL_REVID_E0)))
4845 I915_WRITE(GEN9_PG_ENABLE, 0);
4847 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4848 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
4850 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4854 static void gen8_enable_rps(struct drm_device *dev)
4856 struct drm_i915_private *dev_priv = dev->dev_private;
4857 struct intel_engine_cs *ring;
4858 uint32_t rc6_mask = 0;
4861 /* 1a: Software RC state - RC0 */
4862 I915_WRITE(GEN6_RC_STATE, 0);
4864 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4865 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4866 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4868 /* 2a: Disable RC states. */
4869 I915_WRITE(GEN6_RC_CONTROL, 0);
4871 /* Initialize rps frequencies */
4872 gen6_init_rps_frequencies(dev);
4874 /* 2b: Program RC6 thresholds.*/
4875 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4876 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4877 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4878 for_each_ring(ring, dev_priv, unused)
4879 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4880 I915_WRITE(GEN6_RC_SLEEP, 0);
4881 if (IS_BROADWELL(dev))
4882 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4884 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4887 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4888 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4889 intel_print_rc6_info(dev, rc6_mask);
4890 if (IS_BROADWELL(dev))
4891 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4892 GEN7_RC_CTL_TO_MODE |
4895 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4896 GEN6_RC_CTL_EI_MODE(1) |
4899 /* 4 Program defaults and thresholds for RPS*/
4900 I915_WRITE(GEN6_RPNSWREQ,
4901 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4902 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4903 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4904 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4905 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4907 /* Docs recommend 900MHz, and 300 MHz respectively */
4908 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4909 dev_priv->rps.max_freq_softlimit << 24 |
4910 dev_priv->rps.min_freq_softlimit << 16);
4912 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4913 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4914 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4915 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4917 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4920 I915_WRITE(GEN6_RP_CONTROL,
4921 GEN6_RP_MEDIA_TURBO |
4922 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4923 GEN6_RP_MEDIA_IS_GFX |
4925 GEN6_RP_UP_BUSY_AVG |
4926 GEN6_RP_DOWN_IDLE_AVG);
4928 /* 6: Ring frequency + overclocking (our driver does this later */
4930 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4931 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4933 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4936 static void gen6_enable_rps(struct drm_device *dev)
4938 struct drm_i915_private *dev_priv = dev->dev_private;
4939 struct intel_engine_cs *ring;
4940 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4945 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4947 /* Here begins a magic sequence of register writes to enable
4948 * auto-downclocking.
4950 * Perhaps there might be some value in exposing these to
4953 I915_WRITE(GEN6_RC_STATE, 0);
4955 /* Clear the DBG now so we don't confuse earlier errors */
4956 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4957 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4958 I915_WRITE(GTFIFODBG, gtfifodbg);
4961 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4963 /* Initialize rps frequencies */
4964 gen6_init_rps_frequencies(dev);
4966 /* disable the counters and set deterministic thresholds */
4967 I915_WRITE(GEN6_RC_CONTROL, 0);
4969 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4970 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4971 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4972 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4973 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4975 for_each_ring(ring, dev_priv, i)
4976 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4978 I915_WRITE(GEN6_RC_SLEEP, 0);
4979 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
4980 if (IS_IVYBRIDGE(dev))
4981 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4983 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
4984 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
4985 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4987 /* Check if we are enabling RC6 */
4988 rc6_mode = intel_enable_rc6(dev_priv->dev);
4989 if (rc6_mode & INTEL_RC6_ENABLE)
4990 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4992 /* We don't use those on Haswell */
4993 if (!IS_HASWELL(dev)) {
4994 if (rc6_mode & INTEL_RC6p_ENABLE)
4995 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
4997 if (rc6_mode & INTEL_RC6pp_ENABLE)
4998 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5001 intel_print_rc6_info(dev, rc6_mask);
5003 I915_WRITE(GEN6_RC_CONTROL,
5005 GEN6_RC_CTL_EI_MODE(1) |
5006 GEN6_RC_CTL_HW_ENABLE);
5008 /* Power down if completely idle for over 50ms */
5009 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
5010 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5012 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
5014 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
5016 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5017 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5018 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
5019 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
5020 (pcu_mbox & 0xff) * 50);
5021 dev_priv->rps.max_freq = pcu_mbox & 0xff;
5024 dev_priv->rps.power = HIGH_POWER; /* force a reset */
5025 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
5028 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5029 if (IS_GEN6(dev) && ret) {
5030 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5031 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5032 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5033 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5034 rc6vids &= 0xffff00;
5035 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5036 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5038 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5041 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5044 static void __gen6_update_ring_freq(struct drm_device *dev)
5046 struct drm_i915_private *dev_priv = dev->dev_private;
5048 unsigned int gpu_freq;
5049 unsigned int max_ia_freq, min_ring_freq;
5050 unsigned int max_gpu_freq, min_gpu_freq;
5051 int scaling_factor = 180;
5052 struct cpufreq_policy *policy;
5054 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5056 policy = cpufreq_cpu_get(0);
5058 max_ia_freq = policy->cpuinfo.max_freq;
5059 cpufreq_cpu_put(policy);
5062 * Default to measured freq if none found, PCU will ensure we
5065 max_ia_freq = tsc_khz;
5068 /* Convert from kHz to MHz */
5069 max_ia_freq /= 1000;
5071 min_ring_freq = I915_READ(DCLK) & 0xf;
5072 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5073 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
5075 if (IS_SKYLAKE(dev)) {
5076 /* Convert GT frequency to 50 HZ units */
5077 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5078 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5080 min_gpu_freq = dev_priv->rps.min_freq;
5081 max_gpu_freq = dev_priv->rps.max_freq;
5085 * For each potential GPU frequency, load a ring frequency we'd like
5086 * to use for memory access. We do this by specifying the IA frequency
5087 * the PCU should use as a reference to determine the ring frequency.
5089 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5090 int diff = max_gpu_freq - gpu_freq;
5091 unsigned int ia_freq = 0, ring_freq = 0;
5093 if (IS_SKYLAKE(dev)) {
5095 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5096 * No floor required for ring frequency on SKL.
5098 ring_freq = gpu_freq;
5099 } else if (INTEL_INFO(dev)->gen >= 8) {
5100 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5101 ring_freq = max(min_ring_freq, gpu_freq);
5102 } else if (IS_HASWELL(dev)) {
5103 ring_freq = mult_frac(gpu_freq, 5, 4);
5104 ring_freq = max(min_ring_freq, ring_freq);
5105 /* leave ia_freq as the default, chosen by cpufreq */
5107 /* On older processors, there is no separate ring
5108 * clock domain, so in order to boost the bandwidth
5109 * of the ring, we need to upclock the CPU (ia_freq).
5111 * For GPU frequencies less than 750MHz,
5112 * just use the lowest ring freq.
5114 if (gpu_freq < min_freq)
5117 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5118 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5121 sandybridge_pcode_write(dev_priv,
5122 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
5123 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5124 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5129 void gen6_update_ring_freq(struct drm_device *dev)
5131 struct drm_i915_private *dev_priv = dev->dev_private;
5133 if (!HAS_CORE_RING_FREQ(dev))
5136 mutex_lock(&dev_priv->rps.hw_lock);
5137 __gen6_update_ring_freq(dev);
5138 mutex_unlock(&dev_priv->rps.hw_lock);
5141 static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
5143 struct drm_device *dev = dev_priv->dev;
5146 if (dev->pdev->revision >= 0x20) {
5147 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5149 switch (INTEL_INFO(dev)->eu_total) {
5151 /* (2 * 4) config */
5152 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5155 /* (2 * 6) config */
5156 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5159 /* (2 * 8) config */
5161 /* Setting (2 * 8) Min RP0 for any other combination */
5162 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5165 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5167 /* For pre-production hardware */
5168 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
5169 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
5170 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5175 static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5179 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5180 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5185 static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5187 struct drm_device *dev = dev_priv->dev;
5190 if (dev->pdev->revision >= 0x20) {
5191 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5192 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5194 /* For pre-production hardware */
5195 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5196 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
5197 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
5202 static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5206 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5208 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5213 static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
5217 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5219 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5221 rp0 = min_t(u32, rp0, 0xea);
5226 static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5230 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
5231 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
5232 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
5233 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5238 static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
5240 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5243 /* Check that the pctx buffer wasn't move under us. */
5244 static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5246 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5248 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5249 dev_priv->vlv_pctx->stolen->start);
5253 /* Check that the pcbr address is not empty. */
5254 static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5256 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5258 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5261 static void cherryview_setup_pctx(struct drm_device *dev)
5263 struct drm_i915_private *dev_priv = dev->dev_private;
5264 unsigned long pctx_paddr, paddr;
5265 struct i915_gtt *gtt = &dev_priv->gtt;
5267 int pctx_size = 32*1024;
5269 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5271 pcbr = I915_READ(VLV_PCBR);
5272 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5273 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5274 paddr = (dev_priv->mm.stolen_base +
5275 (gtt->stolen_size - pctx_size));
5277 pctx_paddr = (paddr & (~4095));
5278 I915_WRITE(VLV_PCBR, pctx_paddr);
5281 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5284 static void valleyview_setup_pctx(struct drm_device *dev)
5286 struct drm_i915_private *dev_priv = dev->dev_private;
5287 struct drm_i915_gem_object *pctx;
5288 unsigned long pctx_paddr;
5290 int pctx_size = 24*1024;
5292 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5294 pcbr = I915_READ(VLV_PCBR);
5296 /* BIOS set it up already, grab the pre-alloc'd space */
5299 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5300 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5302 I915_GTT_OFFSET_NONE,
5307 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5310 * From the Gunit register HAS:
5311 * The Gfx driver is expected to program this register and ensure
5312 * proper allocation within Gfx stolen memory. For example, this
5313 * register should be programmed such than the PCBR range does not
5314 * overlap with other ranges, such as the frame buffer, protected
5315 * memory, or any other relevant ranges.
5317 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5319 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5323 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5324 I915_WRITE(VLV_PCBR, pctx_paddr);
5327 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5328 dev_priv->vlv_pctx = pctx;
5331 static void valleyview_cleanup_pctx(struct drm_device *dev)
5333 struct drm_i915_private *dev_priv = dev->dev_private;
5335 if (WARN_ON(!dev_priv->vlv_pctx))
5338 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5339 dev_priv->vlv_pctx = NULL;
5342 static void valleyview_init_gt_powersave(struct drm_device *dev)
5344 struct drm_i915_private *dev_priv = dev->dev_private;
5347 valleyview_setup_pctx(dev);
5349 mutex_lock(&dev_priv->rps.hw_lock);
5351 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5352 switch ((val >> 6) & 3) {
5355 dev_priv->mem_freq = 800;
5358 dev_priv->mem_freq = 1066;
5361 dev_priv->mem_freq = 1333;
5364 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5366 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5367 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5368 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5369 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5370 dev_priv->rps.max_freq);
5372 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5373 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5374 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5375 dev_priv->rps.efficient_freq);
5377 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5378 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5379 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5380 dev_priv->rps.rp1_freq);
5382 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5383 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5384 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5385 dev_priv->rps.min_freq);
5387 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5389 /* Preserve min/max settings in case of re-init */
5390 if (dev_priv->rps.max_freq_softlimit == 0)
5391 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5393 if (dev_priv->rps.min_freq_softlimit == 0)
5394 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5396 mutex_unlock(&dev_priv->rps.hw_lock);
5399 static void cherryview_init_gt_powersave(struct drm_device *dev)
5401 struct drm_i915_private *dev_priv = dev->dev_private;
5404 cherryview_setup_pctx(dev);
5406 mutex_lock(&dev_priv->rps.hw_lock);
5408 mutex_lock(&dev_priv->sb_lock);
5409 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
5410 mutex_unlock(&dev_priv->sb_lock);
5412 switch ((val >> 2) & 0x7) {
5414 dev_priv->mem_freq = 2000;
5417 dev_priv->mem_freq = 1600;
5420 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5422 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5423 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5424 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5425 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5426 dev_priv->rps.max_freq);
5428 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5429 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5430 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5431 dev_priv->rps.efficient_freq);
5433 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5434 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5435 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5436 dev_priv->rps.rp1_freq);
5438 /* PUnit validated range is only [RPe, RP0] */
5439 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
5440 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5441 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5442 dev_priv->rps.min_freq);
5444 WARN_ONCE((dev_priv->rps.max_freq |
5445 dev_priv->rps.efficient_freq |
5446 dev_priv->rps.rp1_freq |
5447 dev_priv->rps.min_freq) & 1,
5448 "Odd GPU freq values\n");
5450 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5452 /* Preserve min/max settings in case of re-init */
5453 if (dev_priv->rps.max_freq_softlimit == 0)
5454 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5456 if (dev_priv->rps.min_freq_softlimit == 0)
5457 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5459 mutex_unlock(&dev_priv->rps.hw_lock);
5462 static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5464 valleyview_cleanup_pctx(dev);
5467 static void cherryview_enable_rps(struct drm_device *dev)
5469 struct drm_i915_private *dev_priv = dev->dev_private;
5470 struct intel_engine_cs *ring;
5471 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
5474 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5476 gtfifodbg = I915_READ(GTFIFODBG);
5478 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5480 I915_WRITE(GTFIFODBG, gtfifodbg);
5483 cherryview_check_pctx(dev_priv);
5485 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5486 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5487 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5489 /* Disable RC states. */
5490 I915_WRITE(GEN6_RC_CONTROL, 0);
5492 /* 2a: Program RC6 thresholds.*/
5493 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5494 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5495 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5497 for_each_ring(ring, dev_priv, i)
5498 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5499 I915_WRITE(GEN6_RC_SLEEP, 0);
5501 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5502 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
5504 /* allows RC6 residency counter to work */
5505 I915_WRITE(VLV_COUNTER_CONTROL,
5506 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5507 VLV_MEDIA_RC6_COUNT_EN |
5508 VLV_RENDER_RC6_COUNT_EN));
5510 /* For now we assume BIOS is allocating and populating the PCBR */
5511 pcbr = I915_READ(VLV_PCBR);
5514 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5515 (pcbr >> VLV_PCBR_ADDR_SHIFT))
5516 rc6_mode = GEN7_RC_CTL_TO_MODE;
5518 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5520 /* 4 Program defaults and thresholds for RPS*/
5521 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5522 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5523 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5524 I915_WRITE(GEN6_RP_UP_EI, 66000);
5525 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5527 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5530 I915_WRITE(GEN6_RP_CONTROL,
5531 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5532 GEN6_RP_MEDIA_IS_GFX |
5534 GEN6_RP_UP_BUSY_AVG |
5535 GEN6_RP_DOWN_IDLE_AVG);
5537 /* Setting Fixed Bias */
5538 val = VLV_OVERRIDE_EN |
5540 CHV_BIAS_CPU_50_SOC_50;
5541 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5543 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5545 /* RPS code assumes GPLL is used */
5546 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5548 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5549 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5551 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5552 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5553 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5554 dev_priv->rps.cur_freq);
5556 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5557 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5558 dev_priv->rps.efficient_freq);
5560 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5562 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5565 static void valleyview_enable_rps(struct drm_device *dev)
5567 struct drm_i915_private *dev_priv = dev->dev_private;
5568 struct intel_engine_cs *ring;
5569 u32 gtfifodbg, val, rc6_mode = 0;
5572 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5574 valleyview_check_pctx(dev_priv);
5576 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5577 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5579 I915_WRITE(GTFIFODBG, gtfifodbg);
5582 /* If VLV, Forcewake all wells, else re-direct to regular path */
5583 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5585 /* Disable RC states. */
5586 I915_WRITE(GEN6_RC_CONTROL, 0);
5588 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5589 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5590 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5591 I915_WRITE(GEN6_RP_UP_EI, 66000);
5592 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5594 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5596 I915_WRITE(GEN6_RP_CONTROL,
5597 GEN6_RP_MEDIA_TURBO |
5598 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5599 GEN6_RP_MEDIA_IS_GFX |
5601 GEN6_RP_UP_BUSY_AVG |
5602 GEN6_RP_DOWN_IDLE_CONT);
5604 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5605 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5606 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5608 for_each_ring(ring, dev_priv, i)
5609 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5611 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
5613 /* allows RC6 residency counter to work */
5614 I915_WRITE(VLV_COUNTER_CONTROL,
5615 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5616 VLV_RENDER_RC0_COUNT_EN |
5617 VLV_MEDIA_RC6_COUNT_EN |
5618 VLV_RENDER_RC6_COUNT_EN));
5620 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
5621 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
5623 intel_print_rc6_info(dev, rc6_mode);
5625 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5627 /* Setting Fixed Bias */
5628 val = VLV_OVERRIDE_EN |
5630 VLV_BIAS_CPU_125_SOC_875;
5631 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5633 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5635 /* RPS code assumes GPLL is used */
5636 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5638 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5639 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5641 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5642 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5643 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5644 dev_priv->rps.cur_freq);
5646 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5647 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5648 dev_priv->rps.efficient_freq);
5650 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5652 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5655 static unsigned long intel_pxfreq(u32 vidfreq)
5658 int div = (vidfreq & 0x3f0000) >> 16;
5659 int post = (vidfreq & 0x3000) >> 12;
5660 int pre = (vidfreq & 0x7);
5665 freq = ((div * 133333) / ((1<<post) * pre));
5670 static const struct cparams {
5676 { 1, 1333, 301, 28664 },
5677 { 1, 1066, 294, 24460 },
5678 { 1, 800, 294, 25192 },
5679 { 0, 1333, 276, 27605 },
5680 { 0, 1066, 276, 27605 },
5681 { 0, 800, 231, 23784 },
5684 static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5686 u64 total_count, diff, ret;
5687 u32 count1, count2, count3, m = 0, c = 0;
5688 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5691 assert_spin_locked(&mchdev_lock);
5693 diff1 = now - dev_priv->ips.last_time1;
5695 /* Prevent division-by-zero if we are asking too fast.
5696 * Also, we don't get interesting results if we are polling
5697 * faster than once in 10ms, so just return the saved value
5701 return dev_priv->ips.chipset_power;
5703 count1 = I915_READ(DMIEC);
5704 count2 = I915_READ(DDREC);
5705 count3 = I915_READ(CSIEC);
5707 total_count = count1 + count2 + count3;
5709 /* FIXME: handle per-counter overflow */
5710 if (total_count < dev_priv->ips.last_count1) {
5711 diff = ~0UL - dev_priv->ips.last_count1;
5712 diff += total_count;
5714 diff = total_count - dev_priv->ips.last_count1;
5717 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5718 if (cparams[i].i == dev_priv->ips.c_m &&
5719 cparams[i].t == dev_priv->ips.r_t) {
5726 diff = div_u64(diff, diff1);
5727 ret = ((m * diff) + c);
5728 ret = div_u64(ret, 10);
5730 dev_priv->ips.last_count1 = total_count;
5731 dev_priv->ips.last_time1 = now;
5733 dev_priv->ips.chipset_power = ret;
5738 unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5740 struct drm_device *dev = dev_priv->dev;
5743 if (INTEL_INFO(dev)->gen != 5)
5746 spin_lock_irq(&mchdev_lock);
5748 val = __i915_chipset_val(dev_priv);
5750 spin_unlock_irq(&mchdev_lock);
5755 unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5757 unsigned long m, x, b;
5760 tsfs = I915_READ(TSFS);
5762 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5763 x = I915_READ8(TR1);
5765 b = tsfs & TSFS_INTR_MASK;
5767 return ((m * x) / 127) - b;
5770 static int _pxvid_to_vd(u8 pxvid)
5775 if (pxvid >= 8 && pxvid < 31)
5778 return (pxvid + 2) * 125;
5781 static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5783 struct drm_device *dev = dev_priv->dev;
5784 const int vd = _pxvid_to_vd(pxvid);
5785 const int vm = vd - 1125;
5787 if (INTEL_INFO(dev)->is_mobile)
5788 return vm > 0 ? vm : 0;
5793 static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5795 u64 now, diff, diffms;
5798 assert_spin_locked(&mchdev_lock);
5800 now = ktime_get_raw_ns();
5801 diffms = now - dev_priv->ips.last_time2;
5802 do_div(diffms, NSEC_PER_MSEC);
5804 /* Don't divide by 0 */
5808 count = I915_READ(GFXEC);
5810 if (count < dev_priv->ips.last_count2) {
5811 diff = ~0UL - dev_priv->ips.last_count2;
5814 diff = count - dev_priv->ips.last_count2;
5817 dev_priv->ips.last_count2 = count;
5818 dev_priv->ips.last_time2 = now;
5820 /* More magic constants... */
5822 diff = div_u64(diff, diffms * 10);
5823 dev_priv->ips.gfx_power = diff;
5826 void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5828 struct drm_device *dev = dev_priv->dev;
5830 if (INTEL_INFO(dev)->gen != 5)
5833 spin_lock_irq(&mchdev_lock);
5835 __i915_update_gfx_val(dev_priv);
5837 spin_unlock_irq(&mchdev_lock);
5840 static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5842 unsigned long t, corr, state1, corr2, state2;
5845 assert_spin_locked(&mchdev_lock);
5847 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
5848 pxvid = (pxvid >> 24) & 0x7f;
5849 ext_v = pvid_to_extvid(dev_priv, pxvid);
5853 t = i915_mch_val(dev_priv);
5855 /* Revel in the empirically derived constants */
5857 /* Correction factor in 1/100000 units */
5859 corr = ((t * 2349) + 135940);
5861 corr = ((t * 964) + 29317);
5863 corr = ((t * 301) + 1004);
5865 corr = corr * ((150142 * state1) / 10000 - 78642);
5867 corr2 = (corr * dev_priv->ips.corr);
5869 state2 = (corr2 * state1) / 10000;
5870 state2 /= 100; /* convert to mW */
5872 __i915_update_gfx_val(dev_priv);
5874 return dev_priv->ips.gfx_power + state2;
5877 unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5879 struct drm_device *dev = dev_priv->dev;
5882 if (INTEL_INFO(dev)->gen != 5)
5885 spin_lock_irq(&mchdev_lock);
5887 val = __i915_gfx_val(dev_priv);
5889 spin_unlock_irq(&mchdev_lock);
5895 * i915_read_mch_val - return value for IPS use
5897 * Calculate and return a value for the IPS driver to use when deciding whether
5898 * we have thermal and power headroom to increase CPU or GPU power budget.
5900 unsigned long i915_read_mch_val(void)
5902 struct drm_i915_private *dev_priv;
5903 unsigned long chipset_val, graphics_val, ret = 0;
5905 spin_lock_irq(&mchdev_lock);
5908 dev_priv = i915_mch_dev;
5910 chipset_val = __i915_chipset_val(dev_priv);
5911 graphics_val = __i915_gfx_val(dev_priv);
5913 ret = chipset_val + graphics_val;
5916 spin_unlock_irq(&mchdev_lock);
5920 EXPORT_SYMBOL_GPL(i915_read_mch_val);
5923 * i915_gpu_raise - raise GPU frequency limit
5925 * Raise the limit; IPS indicates we have thermal headroom.
5927 bool i915_gpu_raise(void)
5929 struct drm_i915_private *dev_priv;
5932 spin_lock_irq(&mchdev_lock);
5933 if (!i915_mch_dev) {
5937 dev_priv = i915_mch_dev;
5939 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5940 dev_priv->ips.max_delay--;
5943 spin_unlock_irq(&mchdev_lock);
5947 EXPORT_SYMBOL_GPL(i915_gpu_raise);
5950 * i915_gpu_lower - lower GPU frequency limit
5952 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5953 * frequency maximum.
5955 bool i915_gpu_lower(void)
5957 struct drm_i915_private *dev_priv;
5960 spin_lock_irq(&mchdev_lock);
5961 if (!i915_mch_dev) {
5965 dev_priv = i915_mch_dev;
5967 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5968 dev_priv->ips.max_delay++;
5971 spin_unlock_irq(&mchdev_lock);
5975 EXPORT_SYMBOL_GPL(i915_gpu_lower);
5978 * i915_gpu_busy - indicate GPU business to IPS
5980 * Tell the IPS driver whether or not the GPU is busy.
5982 bool i915_gpu_busy(void)
5984 struct drm_i915_private *dev_priv;
5985 struct intel_engine_cs *ring;
5989 spin_lock_irq(&mchdev_lock);
5992 dev_priv = i915_mch_dev;
5994 for_each_ring(ring, dev_priv, i)
5995 ret |= !list_empty(&ring->request_list);
5998 spin_unlock_irq(&mchdev_lock);
6002 EXPORT_SYMBOL_GPL(i915_gpu_busy);
6005 * i915_gpu_turbo_disable - disable graphics turbo
6007 * Disable graphics turbo by resetting the max frequency and setting the
6008 * current frequency to the default.
6010 bool i915_gpu_turbo_disable(void)
6012 struct drm_i915_private *dev_priv;
6015 spin_lock_irq(&mchdev_lock);
6016 if (!i915_mch_dev) {
6020 dev_priv = i915_mch_dev;
6022 dev_priv->ips.max_delay = dev_priv->ips.fstart;
6024 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
6028 spin_unlock_irq(&mchdev_lock);
6032 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6035 * Tells the intel_ips driver that the i915 driver is now loaded, if
6036 * IPS got loaded first.
6038 * This awkward dance is so that neither module has to depend on the
6039 * other in order for IPS to do the appropriate communication of
6040 * GPU turbo limits to i915.
6043 ips_ping_for_i915_load(void)
6047 link = symbol_get(ips_link_to_i915_driver);
6050 symbol_put(ips_link_to_i915_driver);
6054 void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6056 /* We only register the i915 ips part with intel-ips once everything is
6057 * set up, to avoid intel-ips sneaking in and reading bogus values. */
6058 spin_lock_irq(&mchdev_lock);
6059 i915_mch_dev = dev_priv;
6060 spin_unlock_irq(&mchdev_lock);
6062 ips_ping_for_i915_load();
6065 void intel_gpu_ips_teardown(void)
6067 spin_lock_irq(&mchdev_lock);
6068 i915_mch_dev = NULL;
6069 spin_unlock_irq(&mchdev_lock);
6072 static void intel_init_emon(struct drm_device *dev)
6074 struct drm_i915_private *dev_priv = dev->dev_private;
6079 /* Disable to program */
6083 /* Program energy weights for various events */
6084 I915_WRITE(SDEW, 0x15040d00);
6085 I915_WRITE(CSIEW0, 0x007f0000);
6086 I915_WRITE(CSIEW1, 0x1e220004);
6087 I915_WRITE(CSIEW2, 0x04000004);
6089 for (i = 0; i < 5; i++)
6090 I915_WRITE(PEW(i), 0);
6091 for (i = 0; i < 3; i++)
6092 I915_WRITE(DEW(i), 0);
6094 /* Program P-state weights to account for frequency power adjustment */
6095 for (i = 0; i < 16; i++) {
6096 u32 pxvidfreq = I915_READ(PXVFREQ(i));
6097 unsigned long freq = intel_pxfreq(pxvidfreq);
6098 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6103 val *= (freq / 1000);
6105 val /= (127*127*900);
6107 DRM_ERROR("bad pxval: %ld\n", val);
6110 /* Render standby states get 0 weight */
6114 for (i = 0; i < 4; i++) {
6115 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6116 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6117 I915_WRITE(PXW(i), val);
6120 /* Adjust magic regs to magic values (more experimental results) */
6121 I915_WRITE(OGW0, 0);
6122 I915_WRITE(OGW1, 0);
6123 I915_WRITE(EG0, 0x00007f00);
6124 I915_WRITE(EG1, 0x0000000e);
6125 I915_WRITE(EG2, 0x000e0000);
6126 I915_WRITE(EG3, 0x68000300);
6127 I915_WRITE(EG4, 0x42000000);
6128 I915_WRITE(EG5, 0x00140031);
6132 for (i = 0; i < 8; i++)
6133 I915_WRITE(PXWL(i), 0);
6135 /* Enable PMON + select events */
6136 I915_WRITE(ECR, 0x80000019);
6138 lcfuse = I915_READ(LCFUSE02);
6140 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
6143 void intel_init_gt_powersave(struct drm_device *dev)
6145 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6147 if (IS_CHERRYVIEW(dev))
6148 cherryview_init_gt_powersave(dev);
6149 else if (IS_VALLEYVIEW(dev))
6150 valleyview_init_gt_powersave(dev);
6153 void intel_cleanup_gt_powersave(struct drm_device *dev)
6155 if (IS_CHERRYVIEW(dev))
6157 else if (IS_VALLEYVIEW(dev))
6158 valleyview_cleanup_gt_powersave(dev);
6161 static void gen6_suspend_rps(struct drm_device *dev)
6163 struct drm_i915_private *dev_priv = dev->dev_private;
6165 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6167 gen6_disable_rps_interrupts(dev);
6171 * intel_suspend_gt_powersave - suspend PM work and helper threads
6174 * We don't want to disable RC6 or other features here, we just want
6175 * to make sure any work we've queued has finished and won't bother
6176 * us while we're suspended.
6178 void intel_suspend_gt_powersave(struct drm_device *dev)
6180 struct drm_i915_private *dev_priv = dev->dev_private;
6182 if (INTEL_INFO(dev)->gen < 6)
6185 gen6_suspend_rps(dev);
6187 /* Force GPU to min freq during suspend */
6188 gen6_rps_idle(dev_priv);
6191 void intel_disable_gt_powersave(struct drm_device *dev)
6193 struct drm_i915_private *dev_priv = dev->dev_private;
6195 if (IS_IRONLAKE_M(dev)) {
6196 ironlake_disable_drps(dev);
6197 } else if (INTEL_INFO(dev)->gen >= 6) {
6198 intel_suspend_gt_powersave(dev);
6200 mutex_lock(&dev_priv->rps.hw_lock);
6201 if (INTEL_INFO(dev)->gen >= 9)
6202 gen9_disable_rps(dev);
6203 else if (IS_CHERRYVIEW(dev))
6204 cherryview_disable_rps(dev);
6205 else if (IS_VALLEYVIEW(dev))
6206 valleyview_disable_rps(dev);
6208 gen6_disable_rps(dev);
6210 dev_priv->rps.enabled = false;
6211 mutex_unlock(&dev_priv->rps.hw_lock);
6215 static void intel_gen6_powersave_work(struct work_struct *work)
6217 struct drm_i915_private *dev_priv =
6218 container_of(work, struct drm_i915_private,
6219 rps.delayed_resume_work.work);
6220 struct drm_device *dev = dev_priv->dev;
6222 mutex_lock(&dev_priv->rps.hw_lock);
6224 gen6_reset_rps_interrupts(dev);
6226 if (IS_CHERRYVIEW(dev)) {
6227 cherryview_enable_rps(dev);
6228 } else if (IS_VALLEYVIEW(dev)) {
6229 valleyview_enable_rps(dev);
6230 } else if (INTEL_INFO(dev)->gen >= 9) {
6231 gen9_enable_rc6(dev);
6232 gen9_enable_rps(dev);
6233 if (IS_SKYLAKE(dev))
6234 __gen6_update_ring_freq(dev);
6235 } else if (IS_BROADWELL(dev)) {
6236 gen8_enable_rps(dev);
6237 __gen6_update_ring_freq(dev);
6239 gen6_enable_rps(dev);
6240 __gen6_update_ring_freq(dev);
6243 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6244 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6246 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6247 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6249 dev_priv->rps.enabled = true;
6251 gen6_enable_rps_interrupts(dev);
6253 mutex_unlock(&dev_priv->rps.hw_lock);
6255 intel_runtime_pm_put(dev_priv);
6258 void intel_enable_gt_powersave(struct drm_device *dev)
6260 struct drm_i915_private *dev_priv = dev->dev_private;
6262 /* Powersaving is controlled by the host when inside a VM */
6263 if (intel_vgpu_active(dev))
6266 if (IS_IRONLAKE_M(dev)) {
6267 mutex_lock(&dev->struct_mutex);
6268 ironlake_enable_drps(dev);
6269 intel_init_emon(dev);
6270 mutex_unlock(&dev->struct_mutex);
6271 } else if (INTEL_INFO(dev)->gen >= 6) {
6273 * PCU communication is slow and this doesn't need to be
6274 * done at any specific time, so do this out of our fast path
6275 * to make resume and init faster.
6277 * We depend on the HW RC6 power context save/restore
6278 * mechanism when entering D3 through runtime PM suspend. So
6279 * disable RPM until RPS/RC6 is properly setup. We can only
6280 * get here via the driver load/system resume/runtime resume
6281 * paths, so the _noresume version is enough (and in case of
6282 * runtime resume it's necessary).
6284 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6285 round_jiffies_up_relative(HZ)))
6286 intel_runtime_pm_get_noresume(dev_priv);
6290 void intel_reset_gt_powersave(struct drm_device *dev)
6292 struct drm_i915_private *dev_priv = dev->dev_private;
6294 if (INTEL_INFO(dev)->gen < 6)
6297 gen6_suspend_rps(dev);
6298 dev_priv->rps.enabled = false;
6301 static void ibx_init_clock_gating(struct drm_device *dev)
6303 struct drm_i915_private *dev_priv = dev->dev_private;
6306 * On Ibex Peak and Cougar Point, we need to disable clock
6307 * gating for the panel power sequencer or it will fail to
6308 * start up when no ports are active.
6310 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6313 static void g4x_disable_trickle_feed(struct drm_device *dev)
6315 struct drm_i915_private *dev_priv = dev->dev_private;
6318 for_each_pipe(dev_priv, pipe) {
6319 I915_WRITE(DSPCNTR(pipe),
6320 I915_READ(DSPCNTR(pipe)) |
6321 DISPPLANE_TRICKLE_FEED_DISABLE);
6323 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6324 POSTING_READ(DSPSURF(pipe));
6328 static void ilk_init_lp_watermarks(struct drm_device *dev)
6330 struct drm_i915_private *dev_priv = dev->dev_private;
6332 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6333 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6334 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6337 * Don't touch WM1S_LP_EN here.
6338 * Doing so could cause underruns.
6342 static void ironlake_init_clock_gating(struct drm_device *dev)
6344 struct drm_i915_private *dev_priv = dev->dev_private;
6345 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6349 * WaFbcDisableDpfcClockGating:ilk
6351 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6352 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6353 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6355 I915_WRITE(PCH_3DCGDIS0,
6356 MARIUNIT_CLOCK_GATE_DISABLE |
6357 SVSMUNIT_CLOCK_GATE_DISABLE);
6358 I915_WRITE(PCH_3DCGDIS1,
6359 VFMUNIT_CLOCK_GATE_DISABLE);
6362 * According to the spec the following bits should be set in
6363 * order to enable memory self-refresh
6364 * The bit 22/21 of 0x42004
6365 * The bit 5 of 0x42020
6366 * The bit 15 of 0x45000
6368 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6369 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6370 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6371 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6372 I915_WRITE(DISP_ARB_CTL,
6373 (I915_READ(DISP_ARB_CTL) |
6376 ilk_init_lp_watermarks(dev);
6379 * Based on the document from hardware guys the following bits
6380 * should be set unconditionally in order to enable FBC.
6381 * The bit 22 of 0x42000
6382 * The bit 22 of 0x42004
6383 * The bit 7,8,9 of 0x42020.
6385 if (IS_IRONLAKE_M(dev)) {
6386 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
6387 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6388 I915_READ(ILK_DISPLAY_CHICKEN1) |
6390 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6391 I915_READ(ILK_DISPLAY_CHICKEN2) |
6395 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6397 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6398 I915_READ(ILK_DISPLAY_CHICKEN2) |
6399 ILK_ELPIN_409_SELECT);
6400 I915_WRITE(_3D_CHICKEN2,
6401 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6402 _3D_CHICKEN2_WM_READ_PIPELINED);
6404 /* WaDisableRenderCachePipelinedFlush:ilk */
6405 I915_WRITE(CACHE_MODE_0,
6406 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6408 /* WaDisable_RenderCache_OperationalFlush:ilk */
6409 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6411 g4x_disable_trickle_feed(dev);
6413 ibx_init_clock_gating(dev);
6416 static void cpt_init_clock_gating(struct drm_device *dev)
6418 struct drm_i915_private *dev_priv = dev->dev_private;
6423 * On Ibex Peak and Cougar Point, we need to disable clock
6424 * gating for the panel power sequencer or it will fail to
6425 * start up when no ports are active.
6427 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6428 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6429 PCH_CPUNIT_CLOCK_GATE_DISABLE);
6430 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6431 DPLS_EDP_PPS_FIX_DIS);
6432 /* The below fixes the weird display corruption, a few pixels shifted
6433 * downward, on (only) LVDS of some HP laptops with IVY.
6435 for_each_pipe(dev_priv, pipe) {
6436 val = I915_READ(TRANS_CHICKEN2(pipe));
6437 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6438 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6439 if (dev_priv->vbt.fdi_rx_polarity_inverted)
6440 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6441 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6442 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6443 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
6444 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6446 /* WADP0ClockGatingDisable */
6447 for_each_pipe(dev_priv, pipe) {
6448 I915_WRITE(TRANS_CHICKEN1(pipe),
6449 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6453 static void gen6_check_mch_setup(struct drm_device *dev)
6455 struct drm_i915_private *dev_priv = dev->dev_private;
6458 tmp = I915_READ(MCH_SSKPD);
6459 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6460 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6464 static void gen6_init_clock_gating(struct drm_device *dev)
6466 struct drm_i915_private *dev_priv = dev->dev_private;
6467 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6469 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6471 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6472 I915_READ(ILK_DISPLAY_CHICKEN2) |
6473 ILK_ELPIN_409_SELECT);
6475 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
6476 I915_WRITE(_3D_CHICKEN,
6477 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6479 /* WaDisable_RenderCache_OperationalFlush:snb */
6480 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6483 * BSpec recoomends 8x4 when MSAA is used,
6484 * however in practice 16x4 seems fastest.
6486 * Note that PS/WM thread counts depend on the WIZ hashing
6487 * disable bit, which we don't touch here, but it's good
6488 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6490 I915_WRITE(GEN6_GT_MODE,
6491 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6493 ilk_init_lp_watermarks(dev);
6495 I915_WRITE(CACHE_MODE_0,
6496 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6498 I915_WRITE(GEN6_UCGCTL1,
6499 I915_READ(GEN6_UCGCTL1) |
6500 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6501 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6503 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6504 * gating disable must be set. Failure to set it results in
6505 * flickering pixels due to Z write ordering failures after
6506 * some amount of runtime in the Mesa "fire" demo, and Unigine
6507 * Sanctuary and Tropics, and apparently anything else with
6508 * alpha test or pixel discard.
6510 * According to the spec, bit 11 (RCCUNIT) must also be set,
6511 * but we didn't debug actual testcases to find it out.
6513 * WaDisableRCCUnitClockGating:snb
6514 * WaDisableRCPBUnitClockGating:snb
6516 I915_WRITE(GEN6_UCGCTL2,
6517 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6518 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6520 /* WaStripsFansDisableFastClipPerformanceFix:snb */
6521 I915_WRITE(_3D_CHICKEN3,
6522 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6526 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6527 * 3DSTATE_SF number of SF output attributes is more than 16."
6529 I915_WRITE(_3D_CHICKEN3,
6530 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6533 * According to the spec the following bits should be
6534 * set in order to enable memory self-refresh and fbc:
6535 * The bit21 and bit22 of 0x42000
6536 * The bit21 and bit22 of 0x42004
6537 * The bit5 and bit7 of 0x42020
6538 * The bit14 of 0x70180
6539 * The bit14 of 0x71180
6541 * WaFbcAsynchFlipDisableFbcQueue:snb
6543 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6544 I915_READ(ILK_DISPLAY_CHICKEN1) |
6545 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6546 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6547 I915_READ(ILK_DISPLAY_CHICKEN2) |
6548 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6549 I915_WRITE(ILK_DSPCLK_GATE_D,
6550 I915_READ(ILK_DSPCLK_GATE_D) |
6551 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6552 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6554 g4x_disable_trickle_feed(dev);
6556 cpt_init_clock_gating(dev);
6558 gen6_check_mch_setup(dev);
6561 static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6563 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6566 * WaVSThreadDispatchOverride:ivb,vlv
6568 * This actually overrides the dispatch
6569 * mode for all thread types.
6571 reg &= ~GEN7_FF_SCHED_MASK;
6572 reg |= GEN7_FF_TS_SCHED_HW;
6573 reg |= GEN7_FF_VS_SCHED_HW;
6574 reg |= GEN7_FF_DS_SCHED_HW;
6576 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6579 static void lpt_init_clock_gating(struct drm_device *dev)
6581 struct drm_i915_private *dev_priv = dev->dev_private;
6584 * TODO: this bit should only be enabled when really needed, then
6585 * disabled when not needed anymore in order to save power.
6587 if (HAS_PCH_LPT_LP(dev))
6588 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6589 I915_READ(SOUTH_DSPCLK_GATE_D) |
6590 PCH_LP_PARTITION_LEVEL_DISABLE);
6592 /* WADPOClockGatingDisable:hsw */
6593 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6594 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
6595 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6598 static void lpt_suspend_hw(struct drm_device *dev)
6600 struct drm_i915_private *dev_priv = dev->dev_private;
6602 if (HAS_PCH_LPT_LP(dev)) {
6603 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6605 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6606 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6610 static void broadwell_init_clock_gating(struct drm_device *dev)
6612 struct drm_i915_private *dev_priv = dev->dev_private;
6616 ilk_init_lp_watermarks(dev);
6618 /* WaSwitchSolVfFArbitrationPriority:bdw */
6619 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6621 /* WaPsrDPAMaskVBlankInSRD:bdw */
6622 I915_WRITE(CHICKEN_PAR1_1,
6623 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6625 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
6626 for_each_pipe(dev_priv, pipe) {
6627 I915_WRITE(CHICKEN_PIPESL_1(pipe),
6628 I915_READ(CHICKEN_PIPESL_1(pipe)) |
6629 BDW_DPRS_MASK_VBLANK_SRD);
6632 /* WaVSRefCountFullforceMissDisable:bdw */
6633 /* WaDSRefCountFullforceMissDisable:bdw */
6634 I915_WRITE(GEN7_FF_THREAD_MODE,
6635 I915_READ(GEN7_FF_THREAD_MODE) &
6636 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6638 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6639 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6641 /* WaDisableSDEUnitClockGating:bdw */
6642 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6643 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6646 * WaProgramL3SqcReg1Default:bdw
6647 * WaTempDisableDOPClkGating:bdw
6649 misccpctl = I915_READ(GEN7_MISCCPCTL);
6650 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6651 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
6652 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6655 * WaGttCachingOffByDefault:bdw
6656 * GTT cache may not work with big pages, so if those
6657 * are ever enabled GTT cache may need to be disabled.
6659 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6661 lpt_init_clock_gating(dev);
6664 static void haswell_init_clock_gating(struct drm_device *dev)
6666 struct drm_i915_private *dev_priv = dev->dev_private;
6668 ilk_init_lp_watermarks(dev);
6670 /* L3 caching of data atomics doesn't work -- disable it. */
6671 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6672 I915_WRITE(HSW_ROW_CHICKEN3,
6673 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6675 /* This is required by WaCatErrorRejectionIssue:hsw */
6676 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6677 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6678 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6680 /* WaVSRefCountFullforceMissDisable:hsw */
6681 I915_WRITE(GEN7_FF_THREAD_MODE,
6682 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6684 /* WaDisable_RenderCache_OperationalFlush:hsw */
6685 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6687 /* enable HiZ Raw Stall Optimization */
6688 I915_WRITE(CACHE_MODE_0_GEN7,
6689 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6691 /* WaDisable4x2SubspanOptimization:hsw */
6692 I915_WRITE(CACHE_MODE_1,
6693 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6696 * BSpec recommends 8x4 when MSAA is used,
6697 * however in practice 16x4 seems fastest.
6699 * Note that PS/WM thread counts depend on the WIZ hashing
6700 * disable bit, which we don't touch here, but it's good
6701 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6703 I915_WRITE(GEN7_GT_MODE,
6704 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6706 /* WaSampleCChickenBitEnable:hsw */
6707 I915_WRITE(HALF_SLICE_CHICKEN3,
6708 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6710 /* WaSwitchSolVfFArbitrationPriority:hsw */
6711 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6713 /* WaRsPkgCStateDisplayPMReq:hsw */
6714 I915_WRITE(CHICKEN_PAR1_1,
6715 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6717 lpt_init_clock_gating(dev);
6720 static void ivybridge_init_clock_gating(struct drm_device *dev)
6722 struct drm_i915_private *dev_priv = dev->dev_private;
6725 ilk_init_lp_watermarks(dev);
6727 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6729 /* WaDisableEarlyCull:ivb */
6730 I915_WRITE(_3D_CHICKEN3,
6731 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6733 /* WaDisableBackToBackFlipFix:ivb */
6734 I915_WRITE(IVB_CHICKEN3,
6735 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6736 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6738 /* WaDisablePSDDualDispatchEnable:ivb */
6739 if (IS_IVB_GT1(dev))
6740 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6741 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6743 /* WaDisable_RenderCache_OperationalFlush:ivb */
6744 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6746 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6747 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6748 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6750 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6751 I915_WRITE(GEN7_L3CNTLREG1,
6752 GEN7_WA_FOR_GEN7_L3_CONTROL);
6753 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6754 GEN7_WA_L3_CHICKEN_MODE);
6755 if (IS_IVB_GT1(dev))
6756 I915_WRITE(GEN7_ROW_CHICKEN2,
6757 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6759 /* must write both registers */
6760 I915_WRITE(GEN7_ROW_CHICKEN2,
6761 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6762 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6763 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6766 /* WaForceL3Serialization:ivb */
6767 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6768 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6771 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6772 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6774 I915_WRITE(GEN6_UCGCTL2,
6775 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6777 /* This is required by WaCatErrorRejectionIssue:ivb */
6778 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6779 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6780 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6782 g4x_disable_trickle_feed(dev);
6784 gen7_setup_fixed_func_scheduler(dev_priv);
6786 if (0) { /* causes HiZ corruption on ivb:gt1 */
6787 /* enable HiZ Raw Stall Optimization */
6788 I915_WRITE(CACHE_MODE_0_GEN7,
6789 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6792 /* WaDisable4x2SubspanOptimization:ivb */
6793 I915_WRITE(CACHE_MODE_1,
6794 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6797 * BSpec recommends 8x4 when MSAA is used,
6798 * however in practice 16x4 seems fastest.
6800 * Note that PS/WM thread counts depend on the WIZ hashing
6801 * disable bit, which we don't touch here, but it's good
6802 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6804 I915_WRITE(GEN7_GT_MODE,
6805 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6807 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6808 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6809 snpcr |= GEN6_MBC_SNPCR_MED;
6810 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6812 if (!HAS_PCH_NOP(dev))
6813 cpt_init_clock_gating(dev);
6815 gen6_check_mch_setup(dev);
6818 static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6820 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6823 * Disable trickle feed and enable pnd deadline calculation
6825 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6826 I915_WRITE(CBR1_VLV, 0);
6829 static void valleyview_init_clock_gating(struct drm_device *dev)
6831 struct drm_i915_private *dev_priv = dev->dev_private;
6833 vlv_init_display_clock_gating(dev_priv);
6835 /* WaDisableEarlyCull:vlv */
6836 I915_WRITE(_3D_CHICKEN3,
6837 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6839 /* WaDisableBackToBackFlipFix:vlv */
6840 I915_WRITE(IVB_CHICKEN3,
6841 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6842 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6844 /* WaPsdDispatchEnable:vlv */
6845 /* WaDisablePSDDualDispatchEnable:vlv */
6846 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6847 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6848 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6850 /* WaDisable_RenderCache_OperationalFlush:vlv */
6851 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6853 /* WaForceL3Serialization:vlv */
6854 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6855 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6857 /* WaDisableDopClockGating:vlv */
6858 I915_WRITE(GEN7_ROW_CHICKEN2,
6859 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6861 /* This is required by WaCatErrorRejectionIssue:vlv */
6862 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6863 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6864 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6866 gen7_setup_fixed_func_scheduler(dev_priv);
6869 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6870 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
6872 I915_WRITE(GEN6_UCGCTL2,
6873 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6875 /* WaDisableL3Bank2xClockGate:vlv
6876 * Disabling L3 clock gating- MMIO 940c[25] = 1
6877 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6878 I915_WRITE(GEN7_UCGCTL4,
6879 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
6882 * BSpec says this must be set, even though
6883 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6885 I915_WRITE(CACHE_MODE_1,
6886 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6889 * BSpec recommends 8x4 when MSAA is used,
6890 * however in practice 16x4 seems fastest.
6892 * Note that PS/WM thread counts depend on the WIZ hashing
6893 * disable bit, which we don't touch here, but it's good
6894 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6896 I915_WRITE(GEN7_GT_MODE,
6897 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6900 * WaIncreaseL3CreditsForVLVB0:vlv
6901 * This is the hardware default actually.
6903 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6906 * WaDisableVLVClockGating_VBIIssue:vlv
6907 * Disable clock gating on th GCFG unit to prevent a delay
6908 * in the reporting of vblank events.
6910 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6913 static void cherryview_init_clock_gating(struct drm_device *dev)
6915 struct drm_i915_private *dev_priv = dev->dev_private;
6917 vlv_init_display_clock_gating(dev_priv);
6919 /* WaVSRefCountFullforceMissDisable:chv */
6920 /* WaDSRefCountFullforceMissDisable:chv */
6921 I915_WRITE(GEN7_FF_THREAD_MODE,
6922 I915_READ(GEN7_FF_THREAD_MODE) &
6923 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6925 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6926 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6927 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6929 /* WaDisableCSUnitClockGating:chv */
6930 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6931 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6933 /* WaDisableSDEUnitClockGating:chv */
6934 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6935 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6938 * GTT cache may not work with big pages, so if those
6939 * are ever enabled GTT cache may need to be disabled.
6941 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6944 static void g4x_init_clock_gating(struct drm_device *dev)
6946 struct drm_i915_private *dev_priv = dev->dev_private;
6947 uint32_t dspclk_gate;
6949 I915_WRITE(RENCLK_GATE_D1, 0);
6950 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6951 GS_UNIT_CLOCK_GATE_DISABLE |
6952 CL_UNIT_CLOCK_GATE_DISABLE);
6953 I915_WRITE(RAMCLK_GATE_D, 0);
6954 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6955 OVRUNIT_CLOCK_GATE_DISABLE |
6956 OVCUNIT_CLOCK_GATE_DISABLE;
6958 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6959 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
6961 /* WaDisableRenderCachePipelinedFlush */
6962 I915_WRITE(CACHE_MODE_0,
6963 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6965 /* WaDisable_RenderCache_OperationalFlush:g4x */
6966 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6968 g4x_disable_trickle_feed(dev);
6971 static void crestline_init_clock_gating(struct drm_device *dev)
6973 struct drm_i915_private *dev_priv = dev->dev_private;
6975 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6976 I915_WRITE(RENCLK_GATE_D2, 0);
6977 I915_WRITE(DSPCLK_GATE_D, 0);
6978 I915_WRITE(RAMCLK_GATE_D, 0);
6979 I915_WRITE16(DEUC, 0);
6980 I915_WRITE(MI_ARB_STATE,
6981 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6983 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6984 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6987 static void broadwater_init_clock_gating(struct drm_device *dev)
6989 struct drm_i915_private *dev_priv = dev->dev_private;
6991 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6992 I965_RCC_CLOCK_GATE_DISABLE |
6993 I965_RCPB_CLOCK_GATE_DISABLE |
6994 I965_ISC_CLOCK_GATE_DISABLE |
6995 I965_FBC_CLOCK_GATE_DISABLE);
6996 I915_WRITE(RENCLK_GATE_D2, 0);
6997 I915_WRITE(MI_ARB_STATE,
6998 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7000 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7001 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7004 static void gen3_init_clock_gating(struct drm_device *dev)
7006 struct drm_i915_private *dev_priv = dev->dev_private;
7007 u32 dstate = I915_READ(D_STATE);
7009 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7010 DSTATE_DOT_CLOCK_GATING;
7011 I915_WRITE(D_STATE, dstate);
7013 if (IS_PINEVIEW(dev))
7014 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
7016 /* IIR "flip pending" means done if this bit is set */
7017 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
7019 /* interrupts should cause a wake up from C3 */
7020 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
7022 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7023 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
7025 I915_WRITE(MI_ARB_STATE,
7026 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7029 static void i85x_init_clock_gating(struct drm_device *dev)
7031 struct drm_i915_private *dev_priv = dev->dev_private;
7033 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7035 /* interrupts should cause a wake up from C3 */
7036 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7037 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7039 I915_WRITE(MEM_MODE,
7040 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7043 static void i830_init_clock_gating(struct drm_device *dev)
7045 struct drm_i915_private *dev_priv = dev->dev_private;
7047 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
7049 I915_WRITE(MEM_MODE,
7050 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7051 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7054 void intel_init_clock_gating(struct drm_device *dev)
7056 struct drm_i915_private *dev_priv = dev->dev_private;
7058 if (dev_priv->display.init_clock_gating)
7059 dev_priv->display.init_clock_gating(dev);
7062 void intel_suspend_hw(struct drm_device *dev)
7064 if (HAS_PCH_LPT(dev))
7065 lpt_suspend_hw(dev);
7068 /* Set up chip specific power management-related functions */
7069 void intel_init_pm(struct drm_device *dev)
7071 struct drm_i915_private *dev_priv = dev->dev_private;
7073 intel_fbc_init(dev_priv);
7076 if (IS_PINEVIEW(dev))
7077 i915_pineview_get_mem_freq(dev);
7078 else if (IS_GEN5(dev))
7079 i915_ironlake_get_mem_freq(dev);
7081 /* For FIFO watermark updates */
7082 if (INTEL_INFO(dev)->gen >= 9) {
7083 skl_setup_wm_latency(dev);
7085 if (IS_BROXTON(dev))
7086 dev_priv->display.init_clock_gating =
7087 bxt_init_clock_gating;
7088 else if (IS_SKYLAKE(dev))
7089 dev_priv->display.init_clock_gating =
7090 skl_init_clock_gating;
7091 dev_priv->display.update_wm = skl_update_wm;
7092 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
7093 } else if (HAS_PCH_SPLIT(dev)) {
7094 ilk_setup_wm_latency(dev);
7096 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7097 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7098 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7099 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7100 dev_priv->display.update_wm = ilk_update_wm;
7102 DRM_DEBUG_KMS("Failed to read display plane latency. "
7107 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7108 else if (IS_GEN6(dev))
7109 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7110 else if (IS_IVYBRIDGE(dev))
7111 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7112 else if (IS_HASWELL(dev))
7113 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7114 else if (INTEL_INFO(dev)->gen == 8)
7115 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7116 } else if (IS_CHERRYVIEW(dev)) {
7117 vlv_setup_wm_latency(dev);
7119 dev_priv->display.update_wm = vlv_update_wm;
7120 dev_priv->display.init_clock_gating =
7121 cherryview_init_clock_gating;
7122 } else if (IS_VALLEYVIEW(dev)) {
7123 vlv_setup_wm_latency(dev);
7125 dev_priv->display.update_wm = vlv_update_wm;
7126 dev_priv->display.init_clock_gating =
7127 valleyview_init_clock_gating;
7128 } else if (IS_PINEVIEW(dev)) {
7129 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7132 dev_priv->mem_freq)) {
7133 DRM_INFO("failed to find known CxSR latency "
7134 "(found ddr%s fsb freq %d, mem freq %d), "
7136 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7137 dev_priv->fsb_freq, dev_priv->mem_freq);
7138 /* Disable CxSR and never update its watermark again */
7139 intel_set_memory_cxsr(dev_priv, false);
7140 dev_priv->display.update_wm = NULL;
7142 dev_priv->display.update_wm = pineview_update_wm;
7143 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7144 } else if (IS_G4X(dev)) {
7145 dev_priv->display.update_wm = g4x_update_wm;
7146 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7147 } else if (IS_GEN4(dev)) {
7148 dev_priv->display.update_wm = i965_update_wm;
7149 if (IS_CRESTLINE(dev))
7150 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7151 else if (IS_BROADWATER(dev))
7152 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7153 } else if (IS_GEN3(dev)) {
7154 dev_priv->display.update_wm = i9xx_update_wm;
7155 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7156 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7157 } else if (IS_GEN2(dev)) {
7158 if (INTEL_INFO(dev)->num_pipes == 1) {
7159 dev_priv->display.update_wm = i845_update_wm;
7160 dev_priv->display.get_fifo_size = i845_get_fifo_size;
7162 dev_priv->display.update_wm = i9xx_update_wm;
7163 dev_priv->display.get_fifo_size = i830_get_fifo_size;
7166 if (IS_I85X(dev) || IS_I865G(dev))
7167 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7169 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7171 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7175 int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
7177 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7179 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7180 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7184 I915_WRITE(GEN6_PCODE_DATA, *val);
7185 I915_WRITE(GEN6_PCODE_DATA1, 0);
7186 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7188 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7190 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7194 *val = I915_READ(GEN6_PCODE_DATA);
7195 I915_WRITE(GEN6_PCODE_DATA, 0);
7200 int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
7202 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7204 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7205 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7209 I915_WRITE(GEN6_PCODE_DATA, val);
7210 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7212 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7214 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7218 I915_WRITE(GEN6_PCODE_DATA, 0);
7223 static int vlv_gpu_freq_div(unsigned int czclk_freq)
7225 switch (czclk_freq) {
7240 static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7242 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7244 div = vlv_gpu_freq_div(czclk_freq);
7248 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
7251 static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7253 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7255 mul = vlv_gpu_freq_div(czclk_freq);
7259 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
7262 static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7264 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7266 div = vlv_gpu_freq_div(czclk_freq) / 2;
7270 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
7273 static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7275 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7277 mul = vlv_gpu_freq_div(czclk_freq) / 2;
7281 /* CHV needs even values */
7282 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
7285 int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7287 if (IS_GEN9(dev_priv->dev))
7288 return (val * GT_FREQUENCY_MULTIPLIER) / GEN9_FREQ_SCALER;
7289 else if (IS_CHERRYVIEW(dev_priv->dev))
7290 return chv_gpu_freq(dev_priv, val);
7291 else if (IS_VALLEYVIEW(dev_priv->dev))
7292 return byt_gpu_freq(dev_priv, val);
7294 return val * GT_FREQUENCY_MULTIPLIER;
7297 int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7299 if (IS_GEN9(dev_priv->dev))
7300 return (val * GEN9_FREQ_SCALER) / GT_FREQUENCY_MULTIPLIER;
7301 else if (IS_CHERRYVIEW(dev_priv->dev))
7302 return chv_freq_opcode(dev_priv, val);
7303 else if (IS_VALLEYVIEW(dev_priv->dev))
7304 return byt_freq_opcode(dev_priv, val);
7306 return val / GT_FREQUENCY_MULTIPLIER;
7309 struct request_boost {
7310 struct work_struct work;
7311 struct drm_i915_gem_request *req;
7314 static void __intel_rps_boost_work(struct work_struct *work)
7316 struct request_boost *boost = container_of(work, struct request_boost, work);
7317 struct drm_i915_gem_request *req = boost->req;
7319 if (!i915_gem_request_completed(req, true))
7320 gen6_rps_boost(to_i915(req->ring->dev), NULL,
7321 req->emitted_jiffies);
7323 i915_gem_request_unreference__unlocked(req);
7327 void intel_queue_rps_boost_for_request(struct drm_device *dev,
7328 struct drm_i915_gem_request *req)
7330 struct request_boost *boost;
7332 if (req == NULL || INTEL_INFO(dev)->gen < 6)
7335 if (i915_gem_request_completed(req, true))
7338 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7342 i915_gem_request_reference(req);
7345 INIT_WORK(&boost->work, __intel_rps_boost_work);
7346 queue_work(to_i915(dev)->wq, &boost->work);
7349 void intel_pm_setup(struct drm_device *dev)
7351 struct drm_i915_private *dev_priv = dev->dev_private;
7353 mutex_init(&dev_priv->rps.hw_lock);
7354 spin_lock_init(&dev_priv->rps.client_lock);
7356 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7357 intel_gen6_powersave_work);
7358 INIT_LIST_HEAD(&dev_priv->rps.clients);
7359 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7360 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
7362 dev_priv->pm.suspended = false;