intel_pm.c revision 1.22 1 /* $NetBSD: intel_pm.c,v 1.22 2020/02/14 04:36:12 riastradh Exp $ */
2
3 /*
4 * Copyright 2012 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eugeni Dodonov <eugeni.dodonov (at) intel.com>
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: intel_pm.c,v 1.22 2020/02/14 04:36:12 riastradh Exp $");
32
33 #include <linux/bitops.h>
34 #include <linux/cpufreq.h>
35 #include <linux/export.h>
36 #include "i915_drv.h"
37 #include "i915_trace.h"
38 #include "intel_drv.h"
39 #ifndef __NetBSD__
40 #include "../../../platform/x86/intel_ips.h"
41 #endif
42 #include <linux/module.h>
43 #include <linux/log2.h>
44 #include <linux/math64.h>
45 #include <linux/time.h>
46
47 #include <linux/nbsd-namespace.h>
48
49 /**
50 * RC6 is a special power stage which allows the GPU to enter an very
51 * low-voltage mode when idle, using down to 0V while at this stage. This
52 * stage is entered automatically when the GPU is idle when RC6 support is
53 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
54 *
55 * There are different RC6 modes available in Intel GPU, which differentiate
56 * among each other with the latency required to enter and leave RC6 and
57 * voltage consumed by the GPU in different states.
58 *
59 * The combination of the following flags define which states GPU is allowed
60 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
61 * RC6pp is deepest RC6. Their support by hardware varies according to the
62 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
63 * which brings the most power savings; deeper states save more power, but
64 * require higher latency to switch to and wake up.
65 */
66 #define INTEL_RC6_ENABLE (1<<0)
67 #define INTEL_RC6p_ENABLE (1<<1)
68 #define INTEL_RC6pp_ENABLE (1<<2)
69
70 static void bxt_init_clock_gating(struct drm_device *dev)
71 {
72 struct drm_i915_private *dev_priv = dev->dev_private;
73
74 /* WaDisableSDEUnitClockGating:bxt */
75 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
76 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
77
78 /*
79 * FIXME:
80 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
81 */
82 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
83 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
84
85 /*
86 * Lower the display internal timeout.
87 * This is needed to avoid any hard hangs when DSI port PLL
88 * is off and a MMIO access is attempted by any privilege
89 * application, using batch buffers or any other means.
90 */
91 I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
92 }
93
94 static void i915_pineview_get_mem_freq(struct drm_device *dev)
95 {
96 struct drm_i915_private *dev_priv = dev->dev_private;
97 u32 tmp;
98
99 tmp = I915_READ(CLKCFG);
100
101 switch (tmp & CLKCFG_FSB_MASK) {
102 case CLKCFG_FSB_533:
103 dev_priv->fsb_freq = 533; /* 133*4 */
104 break;
105 case CLKCFG_FSB_800:
106 dev_priv->fsb_freq = 800; /* 200*4 */
107 break;
108 case CLKCFG_FSB_667:
109 dev_priv->fsb_freq = 667; /* 167*4 */
110 break;
111 case CLKCFG_FSB_400:
112 dev_priv->fsb_freq = 400; /* 100*4 */
113 break;
114 }
115
116 switch (tmp & CLKCFG_MEM_MASK) {
117 case CLKCFG_MEM_533:
118 dev_priv->mem_freq = 533;
119 break;
120 case CLKCFG_MEM_667:
121 dev_priv->mem_freq = 667;
122 break;
123 case CLKCFG_MEM_800:
124 dev_priv->mem_freq = 800;
125 break;
126 }
127
128 /* detect pineview DDR3 setting */
129 tmp = I915_READ(CSHRDDR3CTL);
130 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
131 }
132
133 static void i915_ironlake_get_mem_freq(struct drm_device *dev)
134 {
135 struct drm_i915_private *dev_priv = dev->dev_private;
136 u16 ddrpll, csipll;
137
138 ddrpll = I915_READ16(DDRMPLL1);
139 csipll = I915_READ16(CSIPLL0);
140
141 switch (ddrpll & 0xff) {
142 case 0xc:
143 dev_priv->mem_freq = 800;
144 break;
145 case 0x10:
146 dev_priv->mem_freq = 1066;
147 break;
148 case 0x14:
149 dev_priv->mem_freq = 1333;
150 break;
151 case 0x18:
152 dev_priv->mem_freq = 1600;
153 break;
154 default:
155 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
156 ddrpll & 0xff);
157 dev_priv->mem_freq = 0;
158 break;
159 }
160
161 dev_priv->ips.r_t = dev_priv->mem_freq;
162
163 switch (csipll & 0x3ff) {
164 case 0x00c:
165 dev_priv->fsb_freq = 3200;
166 break;
167 case 0x00e:
168 dev_priv->fsb_freq = 3733;
169 break;
170 case 0x010:
171 dev_priv->fsb_freq = 4266;
172 break;
173 case 0x012:
174 dev_priv->fsb_freq = 4800;
175 break;
176 case 0x014:
177 dev_priv->fsb_freq = 5333;
178 break;
179 case 0x016:
180 dev_priv->fsb_freq = 5866;
181 break;
182 case 0x018:
183 dev_priv->fsb_freq = 6400;
184 break;
185 default:
186 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
187 csipll & 0x3ff);
188 dev_priv->fsb_freq = 0;
189 break;
190 }
191
192 if (dev_priv->fsb_freq == 3200) {
193 dev_priv->ips.c_m = 0;
194 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
195 dev_priv->ips.c_m = 1;
196 } else {
197 dev_priv->ips.c_m = 2;
198 }
199 }
200
201 static const struct cxsr_latency cxsr_latency_table[] = {
202 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
203 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
204 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
205 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
206 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
207
208 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
209 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
210 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
211 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
212 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
213
214 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
215 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
216 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
217 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
218 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
219
220 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
221 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
222 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
223 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
224 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
225
226 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
227 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
228 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
229 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
230 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
231
232 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
233 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
234 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
235 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
236 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
237 };
238
239 static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
240 int is_ddr3,
241 int fsb,
242 int mem)
243 {
244 const struct cxsr_latency *latency;
245 int i;
246
247 if (fsb == 0 || mem == 0)
248 return NULL;
249
250 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
251 latency = &cxsr_latency_table[i];
252 if (is_desktop == latency->is_desktop &&
253 is_ddr3 == latency->is_ddr3 &&
254 fsb == latency->fsb_freq && mem == latency->mem_freq)
255 return latency;
256 }
257
258 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
259
260 return NULL;
261 }
262
263 static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
264 {
265 u32 val;
266
267 mutex_lock(&dev_priv->rps.hw_lock);
268
269 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
270 if (enable)
271 val &= ~FORCE_DDR_HIGH_FREQ;
272 else
273 val |= FORCE_DDR_HIGH_FREQ;
274 val &= ~FORCE_DDR_LOW_FREQ;
275 val |= FORCE_DDR_FREQ_REQ_ACK;
276 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
277
278 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
279 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
280 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
281
282 mutex_unlock(&dev_priv->rps.hw_lock);
283 }
284
285 static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
286 {
287 u32 val;
288
289 mutex_lock(&dev_priv->rps.hw_lock);
290
291 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
292 if (enable)
293 val |= DSP_MAXFIFO_PM5_ENABLE;
294 else
295 val &= ~DSP_MAXFIFO_PM5_ENABLE;
296 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
297
298 mutex_unlock(&dev_priv->rps.hw_lock);
299 }
300
301 #define FW_WM(value, plane) \
302 (((u32)(value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
303
304 void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
305 {
306 struct drm_device *dev = dev_priv->dev;
307 u32 val;
308
309 if (IS_VALLEYVIEW(dev)) {
310 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
311 POSTING_READ(FW_BLC_SELF_VLV);
312 dev_priv->wm.vlv.cxsr = enable;
313 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
314 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
315 POSTING_READ(FW_BLC_SELF);
316 } else if (IS_PINEVIEW(dev)) {
317 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
318 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
319 I915_WRITE(DSPFW3, val);
320 POSTING_READ(DSPFW3);
321 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
322 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
323 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
324 I915_WRITE(FW_BLC_SELF, val);
325 POSTING_READ(FW_BLC_SELF);
326 } else if (IS_I915GM(dev)) {
327 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
328 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
329 I915_WRITE(INSTPM, val);
330 POSTING_READ(INSTPM);
331 } else {
332 return;
333 }
334
335 DRM_DEBUG_KMS("memory self-refresh is %s\n",
336 enable ? "enabled" : "disabled");
337 }
338
339
340 /*
341 * Latency for FIFO fetches is dependent on several factors:
342 * - memory configuration (speed, channels)
343 * - chipset
344 * - current MCH state
345 * It can be fairly high in some situations, so here we assume a fairly
346 * pessimal value. It's a tradeoff between extra memory fetches (if we
347 * set this value too high, the FIFO will fetch frequently to stay full)
348 * and power consumption (set it too low to save power and we might see
349 * FIFO underruns and display "flicker").
350 *
351 * A value of 5us seems to be a good balance; safe for very low end
352 * platforms but not overly aggressive on lower latency configs.
353 */
354 static const int pessimal_latency_ns = 5000;
355
356 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
357 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
358
359 static int vlv_get_fifo_size(struct drm_device *dev,
360 enum pipe pipe, int plane)
361 {
362 struct drm_i915_private *dev_priv = dev->dev_private;
363 int sprite0_start, sprite1_start, size;
364
365 switch (pipe) {
366 uint32_t dsparb, dsparb2, dsparb3;
367 case PIPE_A:
368 dsparb = I915_READ(DSPARB);
369 dsparb2 = I915_READ(DSPARB2);
370 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
371 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
372 break;
373 case PIPE_B:
374 dsparb = I915_READ(DSPARB);
375 dsparb2 = I915_READ(DSPARB2);
376 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
377 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
378 break;
379 case PIPE_C:
380 dsparb2 = I915_READ(DSPARB2);
381 dsparb3 = I915_READ(DSPARB3);
382 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
383 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
384 break;
385 default:
386 return 0;
387 }
388
389 switch (plane) {
390 case 0:
391 size = sprite0_start;
392 break;
393 case 1:
394 size = sprite1_start - sprite0_start;
395 break;
396 case 2:
397 size = 512 - 1 - sprite1_start;
398 break;
399 default:
400 return 0;
401 }
402
403 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
404 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
405 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
406 size);
407
408 return size;
409 }
410
411 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
412 {
413 struct drm_i915_private *dev_priv = dev->dev_private;
414 uint32_t dsparb = I915_READ(DSPARB);
415 int size;
416
417 size = dsparb & 0x7f;
418 if (plane)
419 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
420
421 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
422 plane ? "B" : "A", size);
423
424 return size;
425 }
426
427 static int i830_get_fifo_size(struct drm_device *dev, int plane)
428 {
429 struct drm_i915_private *dev_priv = dev->dev_private;
430 uint32_t dsparb = I915_READ(DSPARB);
431 int size;
432
433 size = dsparb & 0x1ff;
434 if (plane)
435 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
436 size >>= 1; /* Convert to cachelines */
437
438 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
439 plane ? "B" : "A", size);
440
441 return size;
442 }
443
444 static int i845_get_fifo_size(struct drm_device *dev, int plane)
445 {
446 struct drm_i915_private *dev_priv = dev->dev_private;
447 uint32_t dsparb = I915_READ(DSPARB);
448 int size;
449
450 size = dsparb & 0x7f;
451 size >>= 2; /* Convert to cachelines */
452
453 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
454 plane ? "B" : "A",
455 size);
456
457 return size;
458 }
459
460 /* Pineview has different values for various configs */
461 static const struct intel_watermark_params pineview_display_wm = {
462 .fifo_size = PINEVIEW_DISPLAY_FIFO,
463 .max_wm = PINEVIEW_MAX_WM,
464 .default_wm = PINEVIEW_DFT_WM,
465 .guard_size = PINEVIEW_GUARD_WM,
466 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
467 };
468 static const struct intel_watermark_params pineview_display_hplloff_wm = {
469 .fifo_size = PINEVIEW_DISPLAY_FIFO,
470 .max_wm = PINEVIEW_MAX_WM,
471 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
472 .guard_size = PINEVIEW_GUARD_WM,
473 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
474 };
475 static const struct intel_watermark_params pineview_cursor_wm = {
476 .fifo_size = PINEVIEW_CURSOR_FIFO,
477 .max_wm = PINEVIEW_CURSOR_MAX_WM,
478 .default_wm = PINEVIEW_CURSOR_DFT_WM,
479 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
480 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
481 };
482 static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
483 .fifo_size = PINEVIEW_CURSOR_FIFO,
484 .max_wm = PINEVIEW_CURSOR_MAX_WM,
485 .default_wm = PINEVIEW_CURSOR_DFT_WM,
486 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
487 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
488 };
489 static const struct intel_watermark_params g4x_wm_info = {
490 .fifo_size = G4X_FIFO_SIZE,
491 .max_wm = G4X_MAX_WM,
492 .default_wm = G4X_MAX_WM,
493 .guard_size = 2,
494 .cacheline_size = G4X_FIFO_LINE_SIZE,
495 };
496 static const struct intel_watermark_params g4x_cursor_wm_info = {
497 .fifo_size = I965_CURSOR_FIFO,
498 .max_wm = I965_CURSOR_MAX_WM,
499 .default_wm = I965_CURSOR_DFT_WM,
500 .guard_size = 2,
501 .cacheline_size = G4X_FIFO_LINE_SIZE,
502 };
503 static const struct intel_watermark_params valleyview_wm_info __unused = {
504 .fifo_size = VALLEYVIEW_FIFO_SIZE,
505 .max_wm = VALLEYVIEW_MAX_WM,
506 .default_wm = VALLEYVIEW_MAX_WM,
507 .guard_size = 2,
508 .cacheline_size = G4X_FIFO_LINE_SIZE,
509 };
510 static const struct intel_watermark_params valleyview_cursor_wm_info __unused = {
511 .fifo_size = I965_CURSOR_FIFO,
512 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
513 .default_wm = I965_CURSOR_DFT_WM,
514 .guard_size = 2,
515 .cacheline_size = G4X_FIFO_LINE_SIZE,
516 };
517 static const struct intel_watermark_params i965_cursor_wm_info = {
518 .fifo_size = I965_CURSOR_FIFO,
519 .max_wm = I965_CURSOR_MAX_WM,
520 .default_wm = I965_CURSOR_DFT_WM,
521 .guard_size = 2,
522 .cacheline_size = I915_FIFO_LINE_SIZE,
523 };
524 static const struct intel_watermark_params i945_wm_info = {
525 .fifo_size = I945_FIFO_SIZE,
526 .max_wm = I915_MAX_WM,
527 .default_wm = 1,
528 .guard_size = 2,
529 .cacheline_size = I915_FIFO_LINE_SIZE,
530 };
531 static const struct intel_watermark_params i915_wm_info = {
532 .fifo_size = I915_FIFO_SIZE,
533 .max_wm = I915_MAX_WM,
534 .default_wm = 1,
535 .guard_size = 2,
536 .cacheline_size = I915_FIFO_LINE_SIZE,
537 };
538 static const struct intel_watermark_params i830_a_wm_info = {
539 .fifo_size = I855GM_FIFO_SIZE,
540 .max_wm = I915_MAX_WM,
541 .default_wm = 1,
542 .guard_size = 2,
543 .cacheline_size = I830_FIFO_LINE_SIZE,
544 };
545 static const struct intel_watermark_params i830_bc_wm_info = {
546 .fifo_size = I855GM_FIFO_SIZE,
547 .max_wm = I915_MAX_WM/2,
548 .default_wm = 1,
549 .guard_size = 2,
550 .cacheline_size = I830_FIFO_LINE_SIZE,
551 };
552 static const struct intel_watermark_params i845_wm_info = {
553 .fifo_size = I830_FIFO_SIZE,
554 .max_wm = I915_MAX_WM,
555 .default_wm = 1,
556 .guard_size = 2,
557 .cacheline_size = I830_FIFO_LINE_SIZE,
558 };
559
560 /**
561 * intel_calculate_wm - calculate watermark level
562 * @clock_in_khz: pixel clock
563 * @wm: chip FIFO params
564 * @pixel_size: display pixel size
565 * @latency_ns: memory latency for the platform
566 *
567 * Calculate the watermark level (the level at which the display plane will
568 * start fetching from memory again). Each chip has a different display
569 * FIFO size and allocation, so the caller needs to figure that out and pass
570 * in the correct intel_watermark_params structure.
571 *
572 * As the pixel clock runs, the FIFO will be drained at a rate that depends
573 * on the pixel size. When it reaches the watermark level, it'll start
574 * fetching FIFO line sized based chunks from memory until the FIFO fills
575 * past the watermark point. If the FIFO drains completely, a FIFO underrun
576 * will occur, and a display engine hang could result.
577 */
578 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
579 const struct intel_watermark_params *wm,
580 int fifo_size,
581 int pixel_size,
582 unsigned long latency_ns)
583 {
584 long entries_required, wm_size;
585
586 /*
587 * Note: we need to make sure we don't overflow for various clock &
588 * latency values.
589 * clocks go from a few thousand to several hundred thousand.
590 * latency is usually a few thousand
591 */
592 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
593 1000;
594 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
595
596 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
597
598 wm_size = fifo_size - (entries_required + wm->guard_size);
599
600 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
601
602 /* Don't promote wm_size to unsigned... */
603 if (wm_size > (long)wm->max_wm)
604 wm_size = wm->max_wm;
605 if (wm_size <= 0)
606 wm_size = wm->default_wm;
607
608 /*
609 * Bspec seems to indicate that the value shouldn't be lower than
610 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
611 * Lets go for 8 which is the burst size since certain platforms
612 * already use a hardcoded 8 (which is what the spec says should be
613 * done).
614 */
615 if (wm_size <= 8)
616 wm_size = 8;
617
618 return wm_size;
619 }
620
621 static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
622 {
623 struct drm_crtc *crtc, *enabled = NULL;
624
625 for_each_crtc(dev, crtc) {
626 if (intel_crtc_active(crtc)) {
627 if (enabled)
628 return NULL;
629 enabled = crtc;
630 }
631 }
632
633 return enabled;
634 }
635
636 static void pineview_update_wm(struct drm_crtc *unused_crtc)
637 {
638 struct drm_device *dev = unused_crtc->dev;
639 struct drm_i915_private *dev_priv = dev->dev_private;
640 struct drm_crtc *crtc;
641 const struct cxsr_latency *latency;
642 u32 reg;
643 unsigned long wm;
644
645 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
646 dev_priv->fsb_freq, dev_priv->mem_freq);
647 if (!latency) {
648 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
649 intel_set_memory_cxsr(dev_priv, false);
650 return;
651 }
652
653 crtc = single_enabled_crtc(dev);
654 if (crtc) {
655 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
656 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
657 int clock = adjusted_mode->crtc_clock;
658
659 /* Display SR */
660 wm = intel_calculate_wm(clock, &pineview_display_wm,
661 pineview_display_wm.fifo_size,
662 pixel_size, latency->display_sr);
663 reg = I915_READ(DSPFW1);
664 reg &= ~DSPFW_SR_MASK;
665 reg |= FW_WM(wm, SR);
666 I915_WRITE(DSPFW1, reg);
667 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
668
669 /* cursor SR */
670 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
671 pineview_display_wm.fifo_size,
672 pixel_size, latency->cursor_sr);
673 reg = I915_READ(DSPFW3);
674 reg &= ~DSPFW_CURSOR_SR_MASK;
675 reg |= FW_WM(wm, CURSOR_SR);
676 I915_WRITE(DSPFW3, reg);
677
678 /* Display HPLL off SR */
679 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
680 pineview_display_hplloff_wm.fifo_size,
681 pixel_size, latency->display_hpll_disable);
682 reg = I915_READ(DSPFW3);
683 reg &= ~DSPFW_HPLL_SR_MASK;
684 reg |= FW_WM(wm, HPLL_SR);
685 I915_WRITE(DSPFW3, reg);
686
687 /* cursor HPLL off SR */
688 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
689 pineview_display_hplloff_wm.fifo_size,
690 pixel_size, latency->cursor_hpll_disable);
691 reg = I915_READ(DSPFW3);
692 reg &= ~DSPFW_HPLL_CURSOR_MASK;
693 reg |= FW_WM(wm, HPLL_CURSOR);
694 I915_WRITE(DSPFW3, reg);
695 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
696
697 intel_set_memory_cxsr(dev_priv, true);
698 } else {
699 intel_set_memory_cxsr(dev_priv, false);
700 }
701 }
702
703 static bool g4x_compute_wm0(struct drm_device *dev,
704 int plane,
705 const struct intel_watermark_params *display,
706 int display_latency_ns,
707 const struct intel_watermark_params *cursor,
708 int cursor_latency_ns,
709 int *plane_wm,
710 int *cursor_wm)
711 {
712 struct drm_crtc *crtc;
713 const struct drm_display_mode *adjusted_mode;
714 int htotal, hdisplay, clock, pixel_size;
715 int line_time_us, line_count;
716 int entries, tlb_miss;
717
718 crtc = intel_get_crtc_for_plane(dev, plane);
719 if (!intel_crtc_active(crtc)) {
720 *cursor_wm = cursor->guard_size;
721 *plane_wm = display->guard_size;
722 return false;
723 }
724
725 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
726 clock = adjusted_mode->crtc_clock;
727 htotal = adjusted_mode->crtc_htotal;
728 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
729 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
730
731 /* Use the small buffer method to calculate plane watermark */
732 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
733 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
734 if (tlb_miss > 0)
735 entries += tlb_miss;
736 entries = DIV_ROUND_UP(entries, display->cacheline_size);
737 *plane_wm = entries + display->guard_size;
738 if (*plane_wm > (int)display->max_wm)
739 *plane_wm = display->max_wm;
740
741 /* Use the large buffer method to calculate cursor watermark */
742 line_time_us = max(htotal * 1000 / clock, 1);
743 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
744 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
745 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
746 if (tlb_miss > 0)
747 entries += tlb_miss;
748 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
749 *cursor_wm = entries + cursor->guard_size;
750 if (*cursor_wm > (int)cursor->max_wm)
751 *cursor_wm = (int)cursor->max_wm;
752
753 return true;
754 }
755
756 /*
757 * Check the wm result.
758 *
759 * If any calculated watermark values is larger than the maximum value that
760 * can be programmed into the associated watermark register, that watermark
761 * must be disabled.
762 */
763 static bool g4x_check_srwm(struct drm_device *dev,
764 int display_wm, int cursor_wm,
765 const struct intel_watermark_params *display,
766 const struct intel_watermark_params *cursor)
767 {
768 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
769 display_wm, cursor_wm);
770
771 if (display_wm > display->max_wm) {
772 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
773 display_wm, display->max_wm);
774 return false;
775 }
776
777 if (cursor_wm > cursor->max_wm) {
778 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
779 cursor_wm, cursor->max_wm);
780 return false;
781 }
782
783 if (!(display_wm || cursor_wm)) {
784 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
785 return false;
786 }
787
788 return true;
789 }
790
791 static bool g4x_compute_srwm(struct drm_device *dev,
792 int plane,
793 int latency_ns,
794 const struct intel_watermark_params *display,
795 const struct intel_watermark_params *cursor,
796 int *display_wm, int *cursor_wm)
797 {
798 struct drm_crtc *crtc;
799 const struct drm_display_mode *adjusted_mode;
800 int hdisplay, htotal, pixel_size, clock;
801 unsigned long line_time_us;
802 int line_count, line_size;
803 int small, large;
804 int entries;
805
806 if (!latency_ns) {
807 *display_wm = *cursor_wm = 0;
808 return false;
809 }
810
811 crtc = intel_get_crtc_for_plane(dev, plane);
812 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
813 clock = adjusted_mode->crtc_clock;
814 htotal = adjusted_mode->crtc_htotal;
815 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
816 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
817
818 line_time_us = max(htotal * 1000 / clock, 1);
819 line_count = (latency_ns / line_time_us + 1000) / 1000;
820 line_size = hdisplay * pixel_size;
821
822 /* Use the minimum of the small and large buffer method for primary */
823 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
824 large = line_count * line_size;
825
826 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
827 *display_wm = entries + display->guard_size;
828
829 /* calculate the self-refresh watermark for display cursor */
830 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
831 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
832 *cursor_wm = entries + cursor->guard_size;
833
834 return g4x_check_srwm(dev,
835 *display_wm, *cursor_wm,
836 display, cursor);
837 }
838
839 #define FW_WM_VLV(value, plane) \
840 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
841
842 static void vlv_write_wm_values(struct intel_crtc *crtc,
843 const struct vlv_wm_values *wm)
844 {
845 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
846 enum pipe pipe = crtc->pipe;
847
848 I915_WRITE(VLV_DDL(pipe),
849 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
850 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
851 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
852 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
853
854 I915_WRITE(DSPFW1,
855 FW_WM(wm->sr.plane, SR) |
856 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
857 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
858 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
859 I915_WRITE(DSPFW2,
860 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
861 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
862 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
863 I915_WRITE(DSPFW3,
864 FW_WM(wm->sr.cursor, CURSOR_SR));
865
866 if (IS_CHERRYVIEW(dev_priv)) {
867 I915_WRITE(DSPFW7_CHV,
868 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
869 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
870 I915_WRITE(DSPFW8_CHV,
871 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
872 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
873 I915_WRITE(DSPFW9_CHV,
874 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
875 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
876 I915_WRITE(DSPHOWM,
877 FW_WM(wm->sr.plane >> 9, SR_HI) |
878 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
879 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
880 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
881 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
882 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
883 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
884 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
885 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
886 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
887 } else {
888 I915_WRITE(DSPFW7,
889 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
890 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
891 I915_WRITE(DSPHOWM,
892 FW_WM(wm->sr.plane >> 9, SR_HI) |
893 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
894 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
895 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
896 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
897 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
898 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
899 }
900
901 /* zero (unused) WM1 watermarks */
902 I915_WRITE(DSPFW4, 0);
903 I915_WRITE(DSPFW5, 0);
904 I915_WRITE(DSPFW6, 0);
905 I915_WRITE(DSPHOWM1, 0);
906
907 POSTING_READ(DSPFW1);
908 }
909
910 #undef FW_WM_VLV
911
912 enum vlv_wm_level {
913 VLV_WM_LEVEL_PM2,
914 VLV_WM_LEVEL_PM5,
915 VLV_WM_LEVEL_DDR_DVFS,
916 };
917
918 /* latency must be in 0.1us units. */
919 static unsigned int vlv_wm_method2(unsigned int pixel_rate,
920 unsigned int pipe_htotal,
921 unsigned int horiz_pixels,
922 unsigned int bytes_per_pixel,
923 unsigned int latency)
924 {
925 unsigned int ret;
926
927 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
928 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
929 ret = DIV_ROUND_UP(ret, 64);
930
931 return ret;
932 }
933
934 static void vlv_setup_wm_latency(struct drm_device *dev)
935 {
936 struct drm_i915_private *dev_priv = dev->dev_private;
937
938 /* all latencies in usec */
939 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
940
941 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
942
943 if (IS_CHERRYVIEW(dev_priv)) {
944 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
945 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
946
947 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
948 }
949 }
950
951 static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
952 struct intel_crtc *crtc,
953 const struct intel_plane_state *state,
954 int level)
955 {
956 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
957 int clock, htotal, pixel_size, width, wm;
958
959 if (dev_priv->wm.pri_latency[level] == 0)
960 return USHRT_MAX;
961
962 if (!state->visible)
963 return 0;
964
965 pixel_size = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
966 clock = crtc->config->base.adjusted_mode.crtc_clock;
967 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
968 width = crtc->config->pipe_src_w;
969 if (WARN_ON(htotal == 0))
970 htotal = 1;
971
972 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
973 /*
974 * FIXME the formula gives values that are
975 * too big for the cursor FIFO, and hence we
976 * would never be able to use cursors. For
977 * now just hardcode the watermark.
978 */
979 wm = 63;
980 } else {
981 wm = vlv_wm_method2(clock, htotal, width, pixel_size,
982 dev_priv->wm.pri_latency[level] * 10);
983 }
984
985 return min_t(int, wm, USHRT_MAX);
986 }
987
988 static void vlv_compute_fifo(struct intel_crtc *crtc)
989 {
990 struct drm_device *dev = crtc->base.dev;
991 struct vlv_wm_state *wm_state = &crtc->wm_state;
992 struct intel_plane *plane;
993 unsigned int total_rate = 0;
994 const int fifo_size = 512 - 1;
995 int fifo_extra, fifo_left = fifo_size;
996
997 for_each_intel_plane_on_crtc(dev, crtc, plane) {
998 struct intel_plane_state *state =
999 to_intel_plane_state(plane->base.state);
1000
1001 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1002 continue;
1003
1004 if (state->visible) {
1005 wm_state->num_active_planes++;
1006 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1007 }
1008 }
1009
1010 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1011 struct intel_plane_state *state =
1012 to_intel_plane_state(plane->base.state);
1013 unsigned int rate;
1014
1015 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1016 plane->wm.fifo_size = 63;
1017 continue;
1018 }
1019
1020 if (!state->visible) {
1021 plane->wm.fifo_size = 0;
1022 continue;
1023 }
1024
1025 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1026 plane->wm.fifo_size = fifo_size * rate / total_rate;
1027 fifo_left -= plane->wm.fifo_size;
1028 }
1029
1030 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1031
1032 /* spread the remainder evenly */
1033 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1034 int plane_extra;
1035
1036 if (fifo_left == 0)
1037 break;
1038
1039 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1040 continue;
1041
1042 /* give it all to the first plane if none are active */
1043 if (plane->wm.fifo_size == 0 &&
1044 wm_state->num_active_planes)
1045 continue;
1046
1047 plane_extra = min(fifo_extra, fifo_left);
1048 plane->wm.fifo_size += plane_extra;
1049 fifo_left -= plane_extra;
1050 }
1051
1052 WARN_ON(fifo_left != 0);
1053 }
1054
1055 static void vlv_invert_wms(struct intel_crtc *crtc)
1056 {
1057 struct vlv_wm_state *wm_state = &crtc->wm_state;
1058 int level;
1059
1060 for (level = 0; level < wm_state->num_levels; level++) {
1061 struct drm_device *dev = crtc->base.dev;
1062 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1063 struct intel_plane *plane;
1064
1065 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1066 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1067
1068 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1069 switch (plane->base.type) {
1070 int sprite;
1071 case DRM_PLANE_TYPE_CURSOR:
1072 wm_state->wm[level].cursor = plane->wm.fifo_size -
1073 wm_state->wm[level].cursor;
1074 break;
1075 case DRM_PLANE_TYPE_PRIMARY:
1076 wm_state->wm[level].primary = plane->wm.fifo_size -
1077 wm_state->wm[level].primary;
1078 break;
1079 case DRM_PLANE_TYPE_OVERLAY:
1080 sprite = plane->plane;
1081 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1082 wm_state->wm[level].sprite[sprite];
1083 break;
1084 }
1085 }
1086 }
1087 }
1088
1089 static void vlv_compute_wm(struct intel_crtc *crtc)
1090 {
1091 struct drm_device *dev = crtc->base.dev;
1092 struct vlv_wm_state *wm_state = &crtc->wm_state;
1093 struct intel_plane *plane;
1094 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1095 int level;
1096
1097 memset(wm_state, 0, sizeof(*wm_state));
1098
1099 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
1100 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
1101
1102 wm_state->num_active_planes = 0;
1103
1104 vlv_compute_fifo(crtc);
1105
1106 if (wm_state->num_active_planes != 1)
1107 wm_state->cxsr = false;
1108
1109 if (wm_state->cxsr) {
1110 for (level = 0; level < wm_state->num_levels; level++) {
1111 wm_state->sr[level].plane = sr_fifo_size;
1112 wm_state->sr[level].cursor = 63;
1113 }
1114 }
1115
1116 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1117 struct intel_plane_state *state =
1118 to_intel_plane_state(plane->base.state);
1119
1120 if (!state->visible)
1121 continue;
1122
1123 /* normal watermarks */
1124 for (level = 0; level < wm_state->num_levels; level++) {
1125 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1126 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1127
1128 /* hack */
1129 if (WARN_ON(level == 0 && wm > max_wm))
1130 wm = max_wm;
1131
1132 if (wm > plane->wm.fifo_size)
1133 break;
1134
1135 switch (plane->base.type) {
1136 int sprite;
1137 case DRM_PLANE_TYPE_CURSOR:
1138 wm_state->wm[level].cursor = wm;
1139 break;
1140 case DRM_PLANE_TYPE_PRIMARY:
1141 wm_state->wm[level].primary = wm;
1142 break;
1143 case DRM_PLANE_TYPE_OVERLAY:
1144 sprite = plane->plane;
1145 wm_state->wm[level].sprite[sprite] = wm;
1146 break;
1147 }
1148 }
1149
1150 wm_state->num_levels = level;
1151
1152 if (!wm_state->cxsr)
1153 continue;
1154
1155 /* maxfifo watermarks */
1156 switch (plane->base.type) {
1157 int sprite, level;
1158 case DRM_PLANE_TYPE_CURSOR:
1159 for (level = 0; level < wm_state->num_levels; level++)
1160 wm_state->sr[level].cursor =
1161 wm_state->wm[level].cursor;
1162 break;
1163 case DRM_PLANE_TYPE_PRIMARY:
1164 for (level = 0; level < wm_state->num_levels; level++)
1165 wm_state->sr[level].plane =
1166 min(wm_state->sr[level].plane,
1167 wm_state->wm[level].primary);
1168 break;
1169 case DRM_PLANE_TYPE_OVERLAY:
1170 sprite = plane->plane;
1171 for (level = 0; level < wm_state->num_levels; level++)
1172 wm_state->sr[level].plane =
1173 min(wm_state->sr[level].plane,
1174 wm_state->wm[level].sprite[sprite]);
1175 break;
1176 }
1177 }
1178
1179 /* clear any (partially) filled invalid levels */
1180 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
1181 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1182 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1183 }
1184
1185 vlv_invert_wms(crtc);
1186 }
1187
1188 #define VLV_FIFO(plane, value) \
1189 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1190
1191 static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1192 {
1193 struct drm_device *dev = crtc->base.dev;
1194 struct drm_i915_private *dev_priv = to_i915(dev);
1195 struct intel_plane *plane;
1196 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1197
1198 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1199 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1200 WARN_ON(plane->wm.fifo_size != 63);
1201 continue;
1202 }
1203
1204 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1205 sprite0_start = plane->wm.fifo_size;
1206 else if (plane->plane == 0)
1207 sprite1_start = sprite0_start + plane->wm.fifo_size;
1208 else
1209 fifo_size = sprite1_start + plane->wm.fifo_size;
1210 }
1211
1212 WARN_ON(fifo_size != 512 - 1);
1213
1214 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1215 pipe_name(crtc->pipe), sprite0_start,
1216 sprite1_start, fifo_size);
1217
1218 switch (crtc->pipe) {
1219 uint32_t dsparb, dsparb2, dsparb3;
1220 case PIPE_A:
1221 dsparb = I915_READ(DSPARB);
1222 dsparb2 = I915_READ(DSPARB2);
1223
1224 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1225 VLV_FIFO(SPRITEB, 0xff));
1226 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1227 VLV_FIFO(SPRITEB, sprite1_start));
1228
1229 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1230 VLV_FIFO(SPRITEB_HI, 0x1));
1231 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1232 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1233
1234 I915_WRITE(DSPARB, dsparb);
1235 I915_WRITE(DSPARB2, dsparb2);
1236 break;
1237 case PIPE_B:
1238 dsparb = I915_READ(DSPARB);
1239 dsparb2 = I915_READ(DSPARB2);
1240
1241 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1242 VLV_FIFO(SPRITED, 0xff));
1243 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1244 VLV_FIFO(SPRITED, sprite1_start));
1245
1246 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1247 VLV_FIFO(SPRITED_HI, 0xff));
1248 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1249 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1250
1251 I915_WRITE(DSPARB, dsparb);
1252 I915_WRITE(DSPARB2, dsparb2);
1253 break;
1254 case PIPE_C:
1255 dsparb3 = I915_READ(DSPARB3);
1256 dsparb2 = I915_READ(DSPARB2);
1257
1258 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1259 VLV_FIFO(SPRITEF, 0xff));
1260 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1261 VLV_FIFO(SPRITEF, sprite1_start));
1262
1263 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1264 VLV_FIFO(SPRITEF_HI, 0xff));
1265 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1266 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1267
1268 I915_WRITE(DSPARB3, dsparb3);
1269 I915_WRITE(DSPARB2, dsparb2);
1270 break;
1271 default:
1272 break;
1273 }
1274 }
1275
1276 #undef VLV_FIFO
1277
1278 static void vlv_merge_wm(struct drm_device *dev,
1279 struct vlv_wm_values *wm)
1280 {
1281 struct intel_crtc *crtc;
1282 int num_active_crtcs = 0;
1283
1284 wm->level = to_i915(dev)->wm.max_level;
1285 wm->cxsr = true;
1286
1287 for_each_intel_crtc(dev, crtc) {
1288 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1289
1290 if (!crtc->active)
1291 continue;
1292
1293 if (!wm_state->cxsr)
1294 wm->cxsr = false;
1295
1296 num_active_crtcs++;
1297 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1298 }
1299
1300 if (num_active_crtcs != 1)
1301 wm->cxsr = false;
1302
1303 if (num_active_crtcs > 1)
1304 wm->level = VLV_WM_LEVEL_PM2;
1305
1306 for_each_intel_crtc(dev, crtc) {
1307 struct vlv_wm_state *wm_state = &crtc->wm_state;
1308 enum pipe pipe = crtc->pipe;
1309
1310 if (!crtc->active)
1311 continue;
1312
1313 wm->pipe[pipe] = wm_state->wm[wm->level];
1314 if (wm->cxsr)
1315 wm->sr = wm_state->sr[wm->level];
1316
1317 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1318 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1319 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1320 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1321 }
1322 }
1323
1324 static void vlv_update_wm(struct drm_crtc *crtc)
1325 {
1326 struct drm_device *dev = crtc->dev;
1327 struct drm_i915_private *dev_priv = dev->dev_private;
1328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1329 enum pipe pipe = intel_crtc->pipe;
1330 struct vlv_wm_values wm = {};
1331
1332 vlv_compute_wm(intel_crtc);
1333 vlv_merge_wm(dev, &wm);
1334
1335 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1336 /* FIXME should be part of crtc atomic commit */
1337 vlv_pipe_set_fifo_size(intel_crtc);
1338 return;
1339 }
1340
1341 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1342 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1343 chv_set_memory_dvfs(dev_priv, false);
1344
1345 if (wm.level < VLV_WM_LEVEL_PM5 &&
1346 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1347 chv_set_memory_pm5(dev_priv, false);
1348
1349 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
1350 intel_set_memory_cxsr(dev_priv, false);
1351
1352 /* FIXME should be part of crtc atomic commit */
1353 vlv_pipe_set_fifo_size(intel_crtc);
1354
1355 vlv_write_wm_values(intel_crtc, &wm);
1356
1357 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1358 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1359 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1360 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1361 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1362
1363 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
1364 intel_set_memory_cxsr(dev_priv, true);
1365
1366 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1367 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1368 chv_set_memory_pm5(dev_priv, true);
1369
1370 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1371 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1372 chv_set_memory_dvfs(dev_priv, true);
1373
1374 dev_priv->wm.vlv = wm;
1375 }
1376
1377 #define single_plane_enabled(mask) is_power_of_2(mask)
1378
1379 static void g4x_update_wm(struct drm_crtc *crtc)
1380 {
1381 struct drm_device *dev = crtc->dev;
1382 static const int sr_latency_ns = 12000;
1383 struct drm_i915_private *dev_priv = dev->dev_private;
1384 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1385 int plane_sr, cursor_sr;
1386 unsigned int enabled = 0;
1387 bool cxsr_enabled;
1388
1389 if (g4x_compute_wm0(dev, PIPE_A,
1390 &g4x_wm_info, pessimal_latency_ns,
1391 &g4x_cursor_wm_info, pessimal_latency_ns,
1392 &planea_wm, &cursora_wm))
1393 enabled |= 1 << PIPE_A;
1394
1395 if (g4x_compute_wm0(dev, PIPE_B,
1396 &g4x_wm_info, pessimal_latency_ns,
1397 &g4x_cursor_wm_info, pessimal_latency_ns,
1398 &planeb_wm, &cursorb_wm))
1399 enabled |= 1 << PIPE_B;
1400
1401 if (single_plane_enabled(enabled) &&
1402 g4x_compute_srwm(dev, ffs(enabled) - 1,
1403 sr_latency_ns,
1404 &g4x_wm_info,
1405 &g4x_cursor_wm_info,
1406 &plane_sr, &cursor_sr)) {
1407 cxsr_enabled = true;
1408 } else {
1409 cxsr_enabled = false;
1410 intel_set_memory_cxsr(dev_priv, false);
1411 plane_sr = cursor_sr = 0;
1412 }
1413
1414 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1415 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1416 planea_wm, cursora_wm,
1417 planeb_wm, cursorb_wm,
1418 plane_sr, cursor_sr);
1419
1420 I915_WRITE(DSPFW1,
1421 FW_WM(plane_sr, SR) |
1422 FW_WM(cursorb_wm, CURSORB) |
1423 FW_WM(planeb_wm, PLANEB) |
1424 FW_WM(planea_wm, PLANEA));
1425 I915_WRITE(DSPFW2,
1426 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1427 FW_WM(cursora_wm, CURSORA));
1428 /* HPLL off in SR has some issues on G4x... disable it */
1429 I915_WRITE(DSPFW3,
1430 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1431 FW_WM(cursor_sr, CURSOR_SR));
1432
1433 if (cxsr_enabled)
1434 intel_set_memory_cxsr(dev_priv, true);
1435 }
1436
1437 static void i965_update_wm(struct drm_crtc *unused_crtc)
1438 {
1439 struct drm_device *dev = unused_crtc->dev;
1440 struct drm_i915_private *dev_priv = dev->dev_private;
1441 struct drm_crtc *crtc;
1442 int srwm = 1;
1443 int cursor_sr = 16;
1444 bool cxsr_enabled;
1445
1446 /* Calc sr entries for one plane configs */
1447 crtc = single_enabled_crtc(dev);
1448 if (crtc) {
1449 /* self-refresh has much higher latency */
1450 static const int sr_latency_ns = 12000;
1451 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1452 int clock = adjusted_mode->crtc_clock;
1453 int htotal = adjusted_mode->crtc_htotal;
1454 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1455 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1456 unsigned long line_time_us;
1457 int entries;
1458
1459 line_time_us = max(htotal * 1000 / clock, 1);
1460
1461 /* Use ns/us then divide to preserve precision */
1462 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1463 pixel_size * hdisplay;
1464 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1465 srwm = I965_FIFO_SIZE - entries;
1466 if (srwm < 0)
1467 srwm = 1;
1468 srwm &= 0x1ff;
1469 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1470 entries, srwm);
1471
1472 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1473 pixel_size * crtc->cursor->state->crtc_w;
1474 entries = DIV_ROUND_UP(entries,
1475 i965_cursor_wm_info.cacheline_size);
1476 cursor_sr = i965_cursor_wm_info.fifo_size -
1477 (entries + i965_cursor_wm_info.guard_size);
1478
1479 if (cursor_sr > i965_cursor_wm_info.max_wm)
1480 cursor_sr = i965_cursor_wm_info.max_wm;
1481
1482 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1483 "cursor %d\n", srwm, cursor_sr);
1484
1485 cxsr_enabled = true;
1486 } else {
1487 cxsr_enabled = false;
1488 /* Turn off self refresh if both pipes are enabled */
1489 intel_set_memory_cxsr(dev_priv, false);
1490 }
1491
1492 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1493 srwm);
1494
1495 /* 965 has limitations... */
1496 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1497 FW_WM(8, CURSORB) |
1498 FW_WM(8, PLANEB) |
1499 FW_WM(8, PLANEA));
1500 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1501 FW_WM(8, PLANEC_OLD));
1502 /* update cursor SR watermark */
1503 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
1504
1505 if (cxsr_enabled)
1506 intel_set_memory_cxsr(dev_priv, true);
1507 }
1508
1509 #undef FW_WM
1510
1511 static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1512 {
1513 struct drm_device *dev = unused_crtc->dev;
1514 struct drm_i915_private *dev_priv = dev->dev_private;
1515 const struct intel_watermark_params *wm_info;
1516 uint32_t fwater_lo;
1517 uint32_t fwater_hi;
1518 int cwm, srwm = 1;
1519 int fifo_size;
1520 int planea_wm, planeb_wm;
1521 struct drm_crtc *crtc, *enabled = NULL;
1522
1523 if (IS_I945GM(dev))
1524 wm_info = &i945_wm_info;
1525 else if (!IS_GEN2(dev))
1526 wm_info = &i915_wm_info;
1527 else
1528 wm_info = &i830_a_wm_info;
1529
1530 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1531 crtc = intel_get_crtc_for_plane(dev, 0);
1532 if (intel_crtc_active(crtc)) {
1533 const struct drm_display_mode *adjusted_mode;
1534 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1535 if (IS_GEN2(dev))
1536 cpp = 4;
1537
1538 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1539 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1540 wm_info, fifo_size, cpp,
1541 pessimal_latency_ns);
1542 enabled = crtc;
1543 } else {
1544 planea_wm = fifo_size - wm_info->guard_size;
1545 if (planea_wm > (long)wm_info->max_wm)
1546 planea_wm = wm_info->max_wm;
1547 }
1548
1549 if (IS_GEN2(dev))
1550 wm_info = &i830_bc_wm_info;
1551
1552 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1553 crtc = intel_get_crtc_for_plane(dev, 1);
1554 if (intel_crtc_active(crtc)) {
1555 const struct drm_display_mode *adjusted_mode;
1556 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1557 if (IS_GEN2(dev))
1558 cpp = 4;
1559
1560 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1561 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1562 wm_info, fifo_size, cpp,
1563 pessimal_latency_ns);
1564 if (enabled == NULL)
1565 enabled = crtc;
1566 else
1567 enabled = NULL;
1568 } else {
1569 planeb_wm = fifo_size - wm_info->guard_size;
1570 if (planeb_wm > (long)wm_info->max_wm)
1571 planeb_wm = wm_info->max_wm;
1572 }
1573
1574 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1575
1576 if (IS_I915GM(dev) && enabled) {
1577 struct drm_i915_gem_object *obj;
1578
1579 obj = intel_fb_obj(enabled->primary->state->fb);
1580
1581 /* self-refresh seems busted with untiled */
1582 if (obj->tiling_mode == I915_TILING_NONE)
1583 enabled = NULL;
1584 }
1585
1586 /*
1587 * Overlay gets an aggressive default since video jitter is bad.
1588 */
1589 cwm = 2;
1590
1591 /* Play safe and disable self-refresh before adjusting watermarks. */
1592 intel_set_memory_cxsr(dev_priv, false);
1593
1594 /* Calc sr entries for one plane configs */
1595 if (HAS_FW_BLC(dev) && enabled) {
1596 /* self-refresh has much higher latency */
1597 static const int sr_latency_ns = 6000;
1598 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
1599 int clock = adjusted_mode->crtc_clock;
1600 int htotal = adjusted_mode->crtc_htotal;
1601 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
1602 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
1603 unsigned long line_time_us;
1604 int entries;
1605
1606 line_time_us = max(htotal * 1000 / clock, 1);
1607
1608 /* Use ns/us then divide to preserve precision */
1609 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1610 pixel_size * hdisplay;
1611 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1612 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1613 srwm = wm_info->fifo_size - entries;
1614 if (srwm < 0)
1615 srwm = 1;
1616
1617 if (IS_I945G(dev) || IS_I945GM(dev))
1618 I915_WRITE(FW_BLC_SELF,
1619 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1620 else if (IS_I915GM(dev))
1621 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1622 }
1623
1624 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1625 planea_wm, planeb_wm, cwm, srwm);
1626
1627 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1628 fwater_hi = (cwm & 0x1f);
1629
1630 /* Set request length to 8 cachelines per fetch */
1631 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1632 fwater_hi = fwater_hi | (1 << 8);
1633
1634 I915_WRITE(FW_BLC, fwater_lo);
1635 I915_WRITE(FW_BLC2, fwater_hi);
1636
1637 if (enabled)
1638 intel_set_memory_cxsr(dev_priv, true);
1639 }
1640
1641 static void i845_update_wm(struct drm_crtc *unused_crtc)
1642 {
1643 struct drm_device *dev = unused_crtc->dev;
1644 struct drm_i915_private *dev_priv = dev->dev_private;
1645 struct drm_crtc *crtc;
1646 const struct drm_display_mode *adjusted_mode;
1647 uint32_t fwater_lo;
1648 int planea_wm;
1649
1650 crtc = single_enabled_crtc(dev);
1651 if (crtc == NULL)
1652 return;
1653
1654 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1655 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1656 &i845_wm_info,
1657 dev_priv->display.get_fifo_size(dev, 0),
1658 4, pessimal_latency_ns);
1659 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1660 fwater_lo |= (3<<8) | planea_wm;
1661
1662 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1663
1664 I915_WRITE(FW_BLC, fwater_lo);
1665 }
1666
1667 uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
1668 {
1669 uint32_t pixel_rate;
1670
1671 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
1672
1673 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1674 * adjust the pixel_rate here. */
1675
1676 if (pipe_config->pch_pfit.enabled) {
1677 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1678 uint32_t pfit_size = pipe_config->pch_pfit.size;
1679
1680 pipe_w = pipe_config->pipe_src_w;
1681 pipe_h = pipe_config->pipe_src_h;
1682
1683 pfit_w = (pfit_size >> 16) & 0xFFFF;
1684 pfit_h = pfit_size & 0xFFFF;
1685 if (pipe_w < pfit_w)
1686 pipe_w = pfit_w;
1687 if (pipe_h < pfit_h)
1688 pipe_h = pfit_h;
1689
1690 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1691 pfit_w * pfit_h);
1692 }
1693
1694 return pixel_rate;
1695 }
1696
1697 /* latency must be in 0.1us units. */
1698 static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1699 uint32_t latency)
1700 {
1701 uint64_t ret;
1702
1703 if (WARN(latency == 0, "Latency value missing\n"))
1704 return UINT_MAX;
1705
1706 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1707 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1708
1709 return ret;
1710 }
1711
1712 /* latency must be in 0.1us units. */
1713 static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1714 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1715 uint32_t latency)
1716 {
1717 uint32_t ret;
1718
1719 if (WARN(latency == 0, "Latency value missing\n"))
1720 return UINT_MAX;
1721
1722 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1723 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1724 ret = DIV_ROUND_UP(ret, 64) + 2;
1725 return ret;
1726 }
1727
1728 static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1729 uint8_t bytes_per_pixel)
1730 {
1731 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1732 }
1733
1734 struct skl_pipe_wm_parameters {
1735 bool active;
1736 uint32_t pipe_htotal;
1737 uint32_t pixel_rate; /* in KHz */
1738 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1739 };
1740
1741 struct ilk_wm_maximums {
1742 uint16_t pri;
1743 uint16_t spr;
1744 uint16_t cur;
1745 uint16_t fbc;
1746 };
1747
1748 /* used in computing the new watermarks state */
1749 struct intel_wm_config {
1750 unsigned int num_pipes_active;
1751 bool sprites_enabled;
1752 bool sprites_scaled;
1753 };
1754
1755 /*
1756 * For both WM_PIPE and WM_LP.
1757 * mem_value must be in 0.1us units.
1758 */
1759 static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
1760 const struct intel_plane_state *pstate,
1761 uint32_t mem_value,
1762 bool is_lp)
1763 {
1764 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1765 uint32_t method1, method2;
1766
1767 if (!cstate->base.active || !pstate->visible)
1768 return 0;
1769
1770 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
1771
1772 if (!is_lp)
1773 return method1;
1774
1775 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1776 cstate->base.adjusted_mode.crtc_htotal,
1777 drm_rect_width(&pstate->dst),
1778 bpp,
1779 mem_value);
1780
1781 return min(method1, method2);
1782 }
1783
1784 /*
1785 * For both WM_PIPE and WM_LP.
1786 * mem_value must be in 0.1us units.
1787 */
1788 static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
1789 const struct intel_plane_state *pstate,
1790 uint32_t mem_value)
1791 {
1792 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1793 uint32_t method1, method2;
1794
1795 if (!cstate->base.active || !pstate->visible)
1796 return 0;
1797
1798 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
1799 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1800 cstate->base.adjusted_mode.crtc_htotal,
1801 drm_rect_width(&pstate->dst),
1802 bpp,
1803 mem_value);
1804 return min(method1, method2);
1805 }
1806
1807 /*
1808 * For both WM_PIPE and WM_LP.
1809 * mem_value must be in 0.1us units.
1810 */
1811 static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
1812 const struct intel_plane_state *pstate,
1813 uint32_t mem_value)
1814 {
1815 /*
1816 * We treat the cursor plane as always-on for the purposes of watermark
1817 * calculation. Until we have two-stage watermark programming merged,
1818 * this is necessary to avoid flickering.
1819 */
1820 int cpp = 4;
1821 int width = pstate->visible ? pstate->base.crtc_w : 64;
1822
1823 if (!cstate->base.active)
1824 return 0;
1825
1826 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1827 cstate->base.adjusted_mode.crtc_htotal,
1828 width, cpp, mem_value);
1829 }
1830
1831 /* Only for WM_LP. */
1832 static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1833 const struct intel_plane_state *pstate,
1834 uint32_t pri_val)
1835 {
1836 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1837
1838 if (!cstate->base.active || !pstate->visible)
1839 return 0;
1840
1841 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), bpp);
1842 }
1843
1844 static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1845 {
1846 if (INTEL_INFO(dev)->gen >= 8)
1847 return 3072;
1848 else if (INTEL_INFO(dev)->gen >= 7)
1849 return 768;
1850 else
1851 return 512;
1852 }
1853
1854 static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1855 int level, bool is_sprite)
1856 {
1857 if (INTEL_INFO(dev)->gen >= 8)
1858 /* BDW primary/sprite plane watermarks */
1859 return level == 0 ? 255 : 2047;
1860 else if (INTEL_INFO(dev)->gen >= 7)
1861 /* IVB/HSW primary/sprite plane watermarks */
1862 return level == 0 ? 127 : 1023;
1863 else if (!is_sprite)
1864 /* ILK/SNB primary plane watermarks */
1865 return level == 0 ? 127 : 511;
1866 else
1867 /* ILK/SNB sprite plane watermarks */
1868 return level == 0 ? 63 : 255;
1869 }
1870
1871 static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1872 int level)
1873 {
1874 if (INTEL_INFO(dev)->gen >= 7)
1875 return level == 0 ? 63 : 255;
1876 else
1877 return level == 0 ? 31 : 63;
1878 }
1879
1880 static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1881 {
1882 if (INTEL_INFO(dev)->gen >= 8)
1883 return 31;
1884 else
1885 return 15;
1886 }
1887
1888 /* Calculate the maximum primary/sprite plane watermark */
1889 static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1890 int level,
1891 const struct intel_wm_config *config,
1892 enum intel_ddb_partitioning ddb_partitioning,
1893 bool is_sprite)
1894 {
1895 unsigned int fifo_size = ilk_display_fifo_size(dev);
1896
1897 /* if sprites aren't enabled, sprites get nothing */
1898 if (is_sprite && !config->sprites_enabled)
1899 return 0;
1900
1901 /* HSW allows LP1+ watermarks even with multiple pipes */
1902 if (level == 0 || config->num_pipes_active > 1) {
1903 fifo_size /= INTEL_INFO(dev)->num_pipes;
1904
1905 /*
1906 * For some reason the non self refresh
1907 * FIFO size is only half of the self
1908 * refresh FIFO size on ILK/SNB.
1909 */
1910 if (INTEL_INFO(dev)->gen <= 6)
1911 fifo_size /= 2;
1912 }
1913
1914 if (config->sprites_enabled) {
1915 /* level 0 is always calculated with 1:1 split */
1916 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1917 if (is_sprite)
1918 fifo_size *= 5;
1919 fifo_size /= 6;
1920 } else {
1921 fifo_size /= 2;
1922 }
1923 }
1924
1925 /* clamp to max that the registers can hold */
1926 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
1927 }
1928
1929 /* Calculate the maximum cursor plane watermark */
1930 static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
1931 int level,
1932 const struct intel_wm_config *config)
1933 {
1934 /* HSW LP1+ watermarks w/ multiple pipes */
1935 if (level > 0 && config->num_pipes_active > 1)
1936 return 64;
1937
1938 /* otherwise just report max that registers can hold */
1939 return ilk_cursor_wm_reg_max(dev, level);
1940 }
1941
1942 static void ilk_compute_wm_maximums(const struct drm_device *dev,
1943 int level,
1944 const struct intel_wm_config *config,
1945 enum intel_ddb_partitioning ddb_partitioning,
1946 struct ilk_wm_maximums *max)
1947 {
1948 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1949 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1950 max->cur = ilk_cursor_wm_max(dev, level, config);
1951 max->fbc = ilk_fbc_wm_reg_max(dev);
1952 }
1953
1954 static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1955 int level,
1956 struct ilk_wm_maximums *max)
1957 {
1958 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1959 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1960 max->cur = ilk_cursor_wm_reg_max(dev, level);
1961 max->fbc = ilk_fbc_wm_reg_max(dev);
1962 }
1963
1964 static bool ilk_validate_wm_level(int level,
1965 const struct ilk_wm_maximums *max,
1966 struct intel_wm_level *result)
1967 {
1968 bool ret;
1969
1970 /* already determined to be invalid? */
1971 if (!result->enable)
1972 return false;
1973
1974 result->enable = result->pri_val <= max->pri &&
1975 result->spr_val <= max->spr &&
1976 result->cur_val <= max->cur;
1977
1978 ret = result->enable;
1979
1980 /*
1981 * HACK until we can pre-compute everything,
1982 * and thus fail gracefully if LP0 watermarks
1983 * are exceeded...
1984 */
1985 if (level == 0 && !result->enable) {
1986 if (result->pri_val > max->pri)
1987 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1988 level, result->pri_val, max->pri);
1989 if (result->spr_val > max->spr)
1990 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1991 level, result->spr_val, max->spr);
1992 if (result->cur_val > max->cur)
1993 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1994 level, result->cur_val, max->cur);
1995
1996 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1997 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1998 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1999 result->enable = true;
2000 }
2001
2002 return ret;
2003 }
2004
2005 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2006 const struct intel_crtc *intel_crtc,
2007 int level,
2008 struct intel_crtc_state *cstate,
2009 struct intel_wm_level *result)
2010 {
2011 struct intel_plane *intel_plane;
2012 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2013 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2014 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2015
2016 /* WM1+ latency values stored in 0.5us units */
2017 if (level > 0) {
2018 pri_latency *= 5;
2019 spr_latency *= 5;
2020 cur_latency *= 5;
2021 }
2022
2023 for_each_intel_plane_on_crtc(dev_priv->dev, intel_crtc, intel_plane) {
2024 struct intel_plane_state *pstate =
2025 to_intel_plane_state(intel_plane->base.state);
2026
2027 switch (intel_plane->base.type) {
2028 case DRM_PLANE_TYPE_PRIMARY:
2029 result->pri_val = ilk_compute_pri_wm(cstate, pstate,
2030 pri_latency,
2031 level);
2032 result->fbc_val = ilk_compute_fbc_wm(cstate, pstate,
2033 result->pri_val);
2034 break;
2035 case DRM_PLANE_TYPE_OVERLAY:
2036 result->spr_val = ilk_compute_spr_wm(cstate, pstate,
2037 spr_latency);
2038 break;
2039 case DRM_PLANE_TYPE_CURSOR:
2040 result->cur_val = ilk_compute_cur_wm(cstate, pstate,
2041 cur_latency);
2042 break;
2043 }
2044 }
2045
2046 result->enable = true;
2047 }
2048
2049 static uint32_t
2050 hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
2051 {
2052 struct drm_i915_private *dev_priv = dev->dev_private;
2053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2054 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
2055 u32 linetime, ips_linetime;
2056
2057 if (!intel_crtc->active)
2058 return 0;
2059
2060 /* The WM are computed with base on how long it takes to fill a single
2061 * row at the given clock rate, multiplied by 8.
2062 * */
2063 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2064 adjusted_mode->crtc_clock);
2065 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2066 dev_priv->cdclk_freq);
2067
2068 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2069 PIPE_WM_LINETIME_TIME(linetime);
2070 }
2071
2072 static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
2073 {
2074 struct drm_i915_private *dev_priv = dev->dev_private;
2075
2076 if (IS_GEN9(dev)) {
2077 uint32_t val;
2078 int ret, i;
2079 int level, max_level = ilk_wm_max_level(dev);
2080
2081 /* read the first set of memory latencies[0:3] */
2082 val = 0; /* data0 to be programmed to 0 for first set */
2083 mutex_lock(&dev_priv->rps.hw_lock);
2084 ret = sandybridge_pcode_read(dev_priv,
2085 GEN9_PCODE_READ_MEM_LATENCY,
2086 &val);
2087 mutex_unlock(&dev_priv->rps.hw_lock);
2088
2089 if (ret) {
2090 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2091 return;
2092 }
2093
2094 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2095 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2096 GEN9_MEM_LATENCY_LEVEL_MASK;
2097 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2098 GEN9_MEM_LATENCY_LEVEL_MASK;
2099 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2100 GEN9_MEM_LATENCY_LEVEL_MASK;
2101
2102 /* read the second set of memory latencies[4:7] */
2103 val = 1; /* data0 to be programmed to 1 for second set */
2104 mutex_lock(&dev_priv->rps.hw_lock);
2105 ret = sandybridge_pcode_read(dev_priv,
2106 GEN9_PCODE_READ_MEM_LATENCY,
2107 &val);
2108 mutex_unlock(&dev_priv->rps.hw_lock);
2109 if (ret) {
2110 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2111 return;
2112 }
2113
2114 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2115 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2116 GEN9_MEM_LATENCY_LEVEL_MASK;
2117 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2118 GEN9_MEM_LATENCY_LEVEL_MASK;
2119 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2120 GEN9_MEM_LATENCY_LEVEL_MASK;
2121
2122 /*
2123 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2124 * need to be disabled. We make sure to sanitize the values out
2125 * of the punit to satisfy this requirement.
2126 */
2127 for (level = 1; level <= max_level; level++) {
2128 if (wm[level] == 0) {
2129 for (i = level + 1; i <= max_level; i++)
2130 wm[i] = 0;
2131 break;
2132 }
2133 }
2134
2135 /*
2136 * WaWmMemoryReadLatency:skl
2137 *
2138 * punit doesn't take into account the read latency so we need
2139 * to add 2us to the various latency levels we retrieve from the
2140 * punit when level 0 response data us 0us.
2141 */
2142 if (wm[0] == 0) {
2143 wm[0] += 2;
2144 for (level = 1; level <= max_level; level++) {
2145 if (wm[level] == 0)
2146 break;
2147 wm[level] += 2;
2148 }
2149 }
2150
2151 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2152 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2153
2154 wm[0] = (sskpd >> 56) & 0xFF;
2155 if (wm[0] == 0)
2156 wm[0] = sskpd & 0xF;
2157 wm[1] = (sskpd >> 4) & 0xFF;
2158 wm[2] = (sskpd >> 12) & 0xFF;
2159 wm[3] = (sskpd >> 20) & 0x1FF;
2160 wm[4] = (sskpd >> 32) & 0x1FF;
2161 } else if (INTEL_INFO(dev)->gen >= 6) {
2162 uint32_t sskpd = I915_READ(MCH_SSKPD);
2163
2164 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2165 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2166 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2167 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2168 } else if (INTEL_INFO(dev)->gen >= 5) {
2169 uint32_t mltr = I915_READ(MLTR_ILK);
2170
2171 /* ILK primary LP0 latency is 700 ns */
2172 wm[0] = 7;
2173 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2174 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2175 }
2176 }
2177
2178 static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2179 {
2180 /* ILK sprite LP0 latency is 1300 ns */
2181 if (INTEL_INFO(dev)->gen == 5)
2182 wm[0] = 13;
2183 }
2184
2185 static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2186 {
2187 /* ILK cursor LP0 latency is 1300 ns */
2188 if (INTEL_INFO(dev)->gen == 5)
2189 wm[0] = 13;
2190
2191 /* WaDoubleCursorLP3Latency:ivb */
2192 if (IS_IVYBRIDGE(dev))
2193 wm[3] *= 2;
2194 }
2195
2196 int ilk_wm_max_level(const struct drm_device *dev)
2197 {
2198 /* how many WM levels are we expecting */
2199 if (INTEL_INFO(dev)->gen >= 9)
2200 return 7;
2201 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2202 return 4;
2203 else if (INTEL_INFO(dev)->gen >= 6)
2204 return 3;
2205 else
2206 return 2;
2207 }
2208
2209 static void intel_print_wm_latency(struct drm_device *dev,
2210 const char *name,
2211 const uint16_t wm[8])
2212 {
2213 int level, max_level = ilk_wm_max_level(dev);
2214
2215 for (level = 0; level <= max_level; level++) {
2216 unsigned int latency = wm[level];
2217
2218 if (latency == 0) {
2219 DRM_ERROR("%s WM%d latency not provided\n",
2220 name, level);
2221 continue;
2222 }
2223
2224 /*
2225 * - latencies are in us on gen9.
2226 * - before then, WM1+ latency values are in 0.5us units
2227 */
2228 if (IS_GEN9(dev))
2229 latency *= 10;
2230 else if (level > 0)
2231 latency *= 5;
2232
2233 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2234 name, level, wm[level],
2235 latency / 10, latency % 10);
2236 }
2237 }
2238
2239 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2240 uint16_t wm[5], uint16_t min)
2241 {
2242 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2243
2244 if (wm[0] >= min)
2245 return false;
2246
2247 wm[0] = max(wm[0], min);
2248 for (level = 1; level <= max_level; level++)
2249 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2250
2251 return true;
2252 }
2253
2254 static void snb_wm_latency_quirk(struct drm_device *dev)
2255 {
2256 struct drm_i915_private *dev_priv = dev->dev_private;
2257 bool changed;
2258
2259 /*
2260 * The BIOS provided WM memory latency values are often
2261 * inadequate for high resolution displays. Adjust them.
2262 */
2263 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2264 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2265 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2266
2267 if (!changed)
2268 return;
2269
2270 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2271 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2272 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2273 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2274 }
2275
2276 static void ilk_setup_wm_latency(struct drm_device *dev)
2277 {
2278 struct drm_i915_private *dev_priv = dev->dev_private;
2279
2280 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2281
2282 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2283 sizeof(dev_priv->wm.pri_latency));
2284 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2285 sizeof(dev_priv->wm.pri_latency));
2286
2287 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2288 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2289
2290 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2291 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2292 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2293
2294 if (IS_GEN6(dev))
2295 snb_wm_latency_quirk(dev);
2296 }
2297
2298 static void skl_setup_wm_latency(struct drm_device *dev)
2299 {
2300 struct drm_i915_private *dev_priv = dev->dev_private;
2301
2302 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2303 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2304 }
2305
2306 static void ilk_compute_wm_config(struct drm_device *dev,
2307 struct intel_wm_config *config)
2308 {
2309 struct intel_crtc *intel_crtc;
2310
2311 /* Compute the currently _active_ config */
2312 for_each_intel_crtc(dev, intel_crtc) {
2313 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2314
2315 if (!wm->pipe_enabled)
2316 continue;
2317
2318 config->sprites_enabled |= wm->sprites_enabled;
2319 config->sprites_scaled |= wm->sprites_scaled;
2320 config->num_pipes_active++;
2321 }
2322 }
2323
2324 /* Compute new watermarks for the pipe */
2325 static bool intel_compute_pipe_wm(struct intel_crtc_state *cstate,
2326 struct intel_pipe_wm *pipe_wm)
2327 {
2328 struct drm_crtc *crtc = cstate->base.crtc;
2329 struct drm_device *dev = crtc->dev;
2330 const struct drm_i915_private *dev_priv = dev->dev_private;
2331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2332 struct intel_plane *intel_plane;
2333 struct intel_plane_state *sprstate = NULL;
2334 int level, max_level = ilk_wm_max_level(dev);
2335 /* LP0 watermark maximums depend on this pipe alone */
2336 struct intel_wm_config config = {
2337 .num_pipes_active = 1,
2338 };
2339 struct ilk_wm_maximums max;
2340
2341 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2342 if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY) {
2343 sprstate = to_intel_plane_state(intel_plane->base.state);
2344 break;
2345 }
2346 }
2347
2348 config.sprites_enabled = sprstate->visible;
2349 config.sprites_scaled = sprstate->visible &&
2350 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2351 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2352
2353 pipe_wm->pipe_enabled = cstate->base.active;
2354 pipe_wm->sprites_enabled = sprstate->visible;
2355 pipe_wm->sprites_scaled = config.sprites_scaled;
2356
2357 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2358 if (INTEL_INFO(dev)->gen <= 6 && sprstate->visible)
2359 max_level = 1;
2360
2361 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2362 if (config.sprites_scaled)
2363 max_level = 0;
2364
2365 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate, &pipe_wm->wm[0]);
2366
2367 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2368 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2369
2370 /* LP0 watermarks always use 1/2 DDB partitioning */
2371 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2372
2373 /* At least LP0 must be valid */
2374 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2375 return false;
2376
2377 ilk_compute_wm_reg_maximums(dev, 1, &max);
2378
2379 for (level = 1; level <= max_level; level++) {
2380 struct intel_wm_level wm = {};
2381
2382 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate, &wm);
2383
2384 /*
2385 * Disable any watermark level that exceeds the
2386 * register maximums since such watermarks are
2387 * always invalid.
2388 */
2389 if (!ilk_validate_wm_level(level, &max, &wm))
2390 break;
2391
2392 pipe_wm->wm[level] = wm;
2393 }
2394
2395 return true;
2396 }
2397
2398 /*
2399 * Merge the watermarks from all active pipes for a specific level.
2400 */
2401 static void ilk_merge_wm_level(struct drm_device *dev,
2402 int level,
2403 struct intel_wm_level *ret_wm)
2404 {
2405 const struct intel_crtc *intel_crtc;
2406
2407 ret_wm->enable = true;
2408
2409 for_each_intel_crtc(dev, intel_crtc) {
2410 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2411 const struct intel_wm_level *wm = &active->wm[level];
2412
2413 if (!active->pipe_enabled)
2414 continue;
2415
2416 /*
2417 * The watermark values may have been used in the past,
2418 * so we must maintain them in the registers for some
2419 * time even if the level is now disabled.
2420 */
2421 if (!wm->enable)
2422 ret_wm->enable = false;
2423
2424 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2425 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2426 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2427 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2428 }
2429 }
2430
2431 /*
2432 * Merge all low power watermarks for all active pipes.
2433 */
2434 static void ilk_wm_merge(struct drm_device *dev,
2435 const struct intel_wm_config *config,
2436 const struct ilk_wm_maximums *max,
2437 struct intel_pipe_wm *merged)
2438 {
2439 struct drm_i915_private *dev_priv = dev->dev_private;
2440 int level, max_level = ilk_wm_max_level(dev);
2441 int last_enabled_level = max_level;
2442
2443 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2444 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2445 config->num_pipes_active > 1)
2446 return;
2447
2448 /* ILK: FBC WM must be disabled always */
2449 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2450
2451 /* merge each WM1+ level */
2452 for (level = 1; level <= max_level; level++) {
2453 struct intel_wm_level *wm = &merged->wm[level];
2454
2455 ilk_merge_wm_level(dev, level, wm);
2456
2457 if (level > last_enabled_level)
2458 wm->enable = false;
2459 else if (!ilk_validate_wm_level(level, max, wm))
2460 /* make sure all following levels get disabled */
2461 last_enabled_level = level - 1;
2462
2463 /*
2464 * The spec says it is preferred to disable
2465 * FBC WMs instead of disabling a WM level.
2466 */
2467 if (wm->fbc_val > max->fbc) {
2468 if (wm->enable)
2469 merged->fbc_wm_enabled = false;
2470 wm->fbc_val = 0;
2471 }
2472 }
2473
2474 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2475 /*
2476 * FIXME this is racy. FBC might get enabled later.
2477 * What we should check here is whether FBC can be
2478 * enabled sometime later.
2479 */
2480 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
2481 intel_fbc_enabled(dev_priv)) {
2482 for (level = 2; level <= max_level; level++) {
2483 struct intel_wm_level *wm = &merged->wm[level];
2484
2485 wm->enable = false;
2486 }
2487 }
2488 }
2489
2490 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2491 {
2492 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2493 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2494 }
2495
2496 /* The value we need to program into the WM_LPx latency field */
2497 static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2498 {
2499 struct drm_i915_private *dev_priv = dev->dev_private;
2500
2501 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2502 return 2 * level;
2503 else
2504 return dev_priv->wm.pri_latency[level];
2505 }
2506
2507 static void ilk_compute_wm_results(struct drm_device *dev,
2508 const struct intel_pipe_wm *merged,
2509 enum intel_ddb_partitioning partitioning,
2510 struct ilk_wm_values *results)
2511 {
2512 struct intel_crtc *intel_crtc;
2513 int level, wm_lp;
2514
2515 results->enable_fbc_wm = merged->fbc_wm_enabled;
2516 results->partitioning = partitioning;
2517
2518 /* LP1+ register values */
2519 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2520 const struct intel_wm_level *r;
2521
2522 level = ilk_wm_lp_to_level(wm_lp, merged);
2523
2524 r = &merged->wm[level];
2525
2526 /*
2527 * Maintain the watermark values even if the level is
2528 * disabled. Doing otherwise could cause underruns.
2529 */
2530 results->wm_lp[wm_lp - 1] =
2531 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2532 (r->pri_val << WM1_LP_SR_SHIFT) |
2533 r->cur_val;
2534
2535 if (r->enable)
2536 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2537
2538 if (INTEL_INFO(dev)->gen >= 8)
2539 results->wm_lp[wm_lp - 1] |=
2540 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2541 else
2542 results->wm_lp[wm_lp - 1] |=
2543 r->fbc_val << WM1_LP_FBC_SHIFT;
2544
2545 /*
2546 * Always set WM1S_LP_EN when spr_val != 0, even if the
2547 * level is disabled. Doing otherwise could cause underruns.
2548 */
2549 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2550 WARN_ON(wm_lp != 1);
2551 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2552 } else
2553 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2554 }
2555
2556 /* LP0 register values */
2557 for_each_intel_crtc(dev, intel_crtc) {
2558 enum pipe pipe = intel_crtc->pipe;
2559 const struct intel_wm_level *r =
2560 &intel_crtc->wm.active.wm[0];
2561
2562 if (WARN_ON(!r->enable))
2563 continue;
2564
2565 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2566
2567 results->wm_pipe[pipe] =
2568 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2569 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2570 r->cur_val;
2571 }
2572 }
2573
2574 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
2575 * case both are at the same level. Prefer r1 in case they're the same. */
2576 static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2577 struct intel_pipe_wm *r1,
2578 struct intel_pipe_wm *r2)
2579 {
2580 int level, max_level = ilk_wm_max_level(dev);
2581 int level1 = 0, level2 = 0;
2582
2583 for (level = 1; level <= max_level; level++) {
2584 if (r1->wm[level].enable)
2585 level1 = level;
2586 if (r2->wm[level].enable)
2587 level2 = level;
2588 }
2589
2590 if (level1 == level2) {
2591 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2592 return r2;
2593 else
2594 return r1;
2595 } else if (level1 > level2) {
2596 return r1;
2597 } else {
2598 return r2;
2599 }
2600 }
2601
2602 /* dirty bits used to track which watermarks need changes */
2603 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2604 #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2605 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2606 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2607 #define WM_DIRTY_FBC (1 << 24)
2608 #define WM_DIRTY_DDB (1 << 25)
2609
2610 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2611 const struct ilk_wm_values *old,
2612 const struct ilk_wm_values *new)
2613 {
2614 unsigned int dirty = 0;
2615 enum pipe pipe;
2616 int wm_lp;
2617
2618 for_each_pipe(dev_priv, pipe) {
2619 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2620 dirty |= WM_DIRTY_LINETIME(pipe);
2621 /* Must disable LP1+ watermarks too */
2622 dirty |= WM_DIRTY_LP_ALL;
2623 }
2624
2625 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2626 dirty |= WM_DIRTY_PIPE(pipe);
2627 /* Must disable LP1+ watermarks too */
2628 dirty |= WM_DIRTY_LP_ALL;
2629 }
2630 }
2631
2632 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2633 dirty |= WM_DIRTY_FBC;
2634 /* Must disable LP1+ watermarks too */
2635 dirty |= WM_DIRTY_LP_ALL;
2636 }
2637
2638 if (old->partitioning != new->partitioning) {
2639 dirty |= WM_DIRTY_DDB;
2640 /* Must disable LP1+ watermarks too */
2641 dirty |= WM_DIRTY_LP_ALL;
2642 }
2643
2644 /* LP1+ watermarks already deemed dirty, no need to continue */
2645 if (dirty & WM_DIRTY_LP_ALL)
2646 return dirty;
2647
2648 /* Find the lowest numbered LP1+ watermark in need of an update... */
2649 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2650 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2651 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2652 break;
2653 }
2654
2655 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2656 for (; wm_lp <= 3; wm_lp++)
2657 dirty |= WM_DIRTY_LP(wm_lp);
2658
2659 return dirty;
2660 }
2661
2662 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2663 unsigned int dirty)
2664 {
2665 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2666 bool changed = false;
2667
2668 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2669 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2670 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2671 changed = true;
2672 }
2673 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2674 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2675 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2676 changed = true;
2677 }
2678 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2679 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2680 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2681 changed = true;
2682 }
2683
2684 /*
2685 * Don't touch WM1S_LP_EN here.
2686 * Doing so could cause underruns.
2687 */
2688
2689 return changed;
2690 }
2691
2692 /*
2693 * The spec says we shouldn't write when we don't need, because every write
2694 * causes WMs to be re-evaluated, expending some power.
2695 */
2696 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2697 struct ilk_wm_values *results)
2698 {
2699 struct drm_device *dev = dev_priv->dev;
2700 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2701 unsigned int dirty;
2702 uint32_t val;
2703
2704 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2705 if (!dirty)
2706 return;
2707
2708 _ilk_disable_lp_wm(dev_priv, dirty);
2709
2710 if (dirty & WM_DIRTY_PIPE(PIPE_A))
2711 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2712 if (dirty & WM_DIRTY_PIPE(PIPE_B))
2713 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2714 if (dirty & WM_DIRTY_PIPE(PIPE_C))
2715 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2716
2717 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2718 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2719 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2720 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2721 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2722 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2723
2724 if (dirty & WM_DIRTY_DDB) {
2725 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2726 val = I915_READ(WM_MISC);
2727 if (results->partitioning == INTEL_DDB_PART_1_2)
2728 val &= ~WM_MISC_DATA_PARTITION_5_6;
2729 else
2730 val |= WM_MISC_DATA_PARTITION_5_6;
2731 I915_WRITE(WM_MISC, val);
2732 } else {
2733 val = I915_READ(DISP_ARB_CTL2);
2734 if (results->partitioning == INTEL_DDB_PART_1_2)
2735 val &= ~DISP_DATA_PARTITION_5_6;
2736 else
2737 val |= DISP_DATA_PARTITION_5_6;
2738 I915_WRITE(DISP_ARB_CTL2, val);
2739 }
2740 }
2741
2742 if (dirty & WM_DIRTY_FBC) {
2743 val = I915_READ(DISP_ARB_CTL);
2744 if (results->enable_fbc_wm)
2745 val &= ~DISP_FBC_WM_DIS;
2746 else
2747 val |= DISP_FBC_WM_DIS;
2748 I915_WRITE(DISP_ARB_CTL, val);
2749 }
2750
2751 if (dirty & WM_DIRTY_LP(1) &&
2752 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2753 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2754
2755 if (INTEL_INFO(dev)->gen >= 7) {
2756 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2757 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2758 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2759 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2760 }
2761
2762 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2763 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2764 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2765 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2766 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2767 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2768
2769 dev_priv->wm.hw = *results;
2770 }
2771
2772 static bool ilk_disable_lp_wm(struct drm_device *dev)
2773 {
2774 struct drm_i915_private *dev_priv = dev->dev_private;
2775
2776 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2777 }
2778
2779 /*
2780 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2781 * different active planes.
2782 */
2783
2784 #define SKL_DDB_SIZE 896 /* in blocks */
2785 #define BXT_DDB_SIZE 512
2786
2787 static void
2788 skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2789 struct drm_crtc *for_crtc,
2790 const struct intel_wm_config *config,
2791 const struct skl_pipe_wm_parameters *params,
2792 struct skl_ddb_entry *alloc /* out */)
2793 {
2794 struct drm_crtc *crtc;
2795 unsigned int pipe_size, ddb_size;
2796 int nth_active_pipe;
2797
2798 if (!params->active) {
2799 alloc->start = 0;
2800 alloc->end = 0;
2801 return;
2802 }
2803
2804 if (IS_BROXTON(dev))
2805 ddb_size = BXT_DDB_SIZE;
2806 else
2807 ddb_size = SKL_DDB_SIZE;
2808
2809 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2810
2811 nth_active_pipe = 0;
2812 for_each_crtc(dev, crtc) {
2813 if (!to_intel_crtc(crtc)->active)
2814 continue;
2815
2816 if (crtc == for_crtc)
2817 break;
2818
2819 nth_active_pipe++;
2820 }
2821
2822 pipe_size = ddb_size / config->num_pipes_active;
2823 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
2824 alloc->end = alloc->start + pipe_size;
2825 }
2826
2827 static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2828 {
2829 if (config->num_pipes_active == 1)
2830 return 32;
2831
2832 return 8;
2833 }
2834
2835 static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2836 {
2837 entry->start = reg & 0x3ff;
2838 entry->end = (reg >> 16) & 0x3ff;
2839 if (entry->end)
2840 entry->end += 1;
2841 }
2842
2843 void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2844 struct skl_ddb_allocation *ddb /* out */)
2845 {
2846 enum pipe pipe;
2847 int plane;
2848 u32 val;
2849
2850 memset(ddb, 0, sizeof(*ddb));
2851
2852 for_each_pipe(dev_priv, pipe) {
2853 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PIPE(pipe)))
2854 continue;
2855
2856 for_each_plane(dev_priv, pipe, plane) {
2857 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2858 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2859 val);
2860 }
2861
2862 val = I915_READ(CUR_BUF_CFG(pipe));
2863 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2864 val);
2865 }
2866 }
2867
2868 static unsigned int
2869 skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p, int y)
2870 {
2871
2872 /* for planar format */
2873 if (p->y_bytes_per_pixel) {
2874 if (y) /* y-plane data rate */
2875 return p->horiz_pixels * p->vert_pixels * p->y_bytes_per_pixel;
2876 else /* uv-plane data rate */
2877 return (p->horiz_pixels/2) * (p->vert_pixels/2) * p->bytes_per_pixel;
2878 }
2879
2880 /* for packed formats */
2881 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2882 }
2883
2884 /*
2885 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2886 * a 8192x4096@32bpp framebuffer:
2887 * 3 * 4096 * 8192 * 4 < 2^32
2888 */
2889 static unsigned int
2890 skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2891 const struct skl_pipe_wm_parameters *params)
2892 {
2893 unsigned int total_data_rate = 0;
2894 int plane;
2895
2896 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2897 const struct intel_plane_wm_parameters *p;
2898
2899 p = ¶ms->plane[plane];
2900 if (!p->enabled)
2901 continue;
2902
2903 total_data_rate += skl_plane_relative_data_rate(p, 0); /* packed/uv */
2904 if (p->y_bytes_per_pixel) {
2905 total_data_rate += skl_plane_relative_data_rate(p, 1); /* y-plane */
2906 }
2907 }
2908
2909 return total_data_rate;
2910 }
2911
2912 static void
2913 skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2914 const struct intel_wm_config *config,
2915 const struct skl_pipe_wm_parameters *params,
2916 struct skl_ddb_allocation *ddb /* out */)
2917 {
2918 struct drm_device *dev = crtc->dev;
2919 struct drm_i915_private *dev_priv = dev->dev_private;
2920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2921 enum pipe pipe = intel_crtc->pipe;
2922 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
2923 uint16_t alloc_size, start, cursor_blocks;
2924 uint16_t minimum[I915_MAX_PLANES];
2925 uint16_t y_minimum[I915_MAX_PLANES];
2926 unsigned int total_data_rate;
2927 int plane;
2928
2929 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2930 alloc_size = skl_ddb_entry_size(alloc);
2931 if (alloc_size == 0) {
2932 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2933 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
2934 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
2935 return;
2936 }
2937
2938 cursor_blocks = skl_cursor_allocation(config);
2939 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
2940 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
2941
2942 alloc_size -= cursor_blocks;
2943 alloc->end -= cursor_blocks;
2944
2945 /* 1. Allocate the mininum required blocks for each active plane */
2946 for_each_plane(dev_priv, pipe, plane) {
2947 const struct intel_plane_wm_parameters *p;
2948
2949 p = ¶ms->plane[plane];
2950 if (!p->enabled)
2951 continue;
2952
2953 minimum[plane] = 8;
2954 alloc_size -= minimum[plane];
2955 y_minimum[plane] = p->y_bytes_per_pixel ? 8 : 0;
2956 alloc_size -= y_minimum[plane];
2957 }
2958
2959 /*
2960 * 2. Distribute the remaining space in proportion to the amount of
2961 * data each plane needs to fetch from memory.
2962 *
2963 * FIXME: we may not allocate every single block here.
2964 */
2965 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2966
2967 start = alloc->start;
2968 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2969 const struct intel_plane_wm_parameters *p;
2970 unsigned int data_rate, y_data_rate;
2971 uint16_t plane_blocks, y_plane_blocks = 0;
2972
2973 p = ¶ms->plane[plane];
2974 if (!p->enabled)
2975 continue;
2976
2977 data_rate = skl_plane_relative_data_rate(p, 0);
2978
2979 /*
2980 * allocation for (packed formats) or (uv-plane part of planar format):
2981 * promote the expression to 64 bits to avoid overflowing, the
2982 * result is < available as data_rate / total_data_rate < 1
2983 */
2984 plane_blocks = minimum[plane];
2985 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2986 total_data_rate);
2987
2988 ddb->plane[pipe][plane].start = start;
2989 ddb->plane[pipe][plane].end = start + plane_blocks;
2990
2991 start += plane_blocks;
2992
2993 /*
2994 * allocation for y_plane part of planar format:
2995 */
2996 if (p->y_bytes_per_pixel) {
2997 y_data_rate = skl_plane_relative_data_rate(p, 1);
2998 y_plane_blocks = y_minimum[plane];
2999 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3000 total_data_rate);
3001
3002 ddb->y_plane[pipe][plane].start = start;
3003 ddb->y_plane[pipe][plane].end = start + y_plane_blocks;
3004
3005 start += y_plane_blocks;
3006 }
3007
3008 }
3009
3010 }
3011
3012 static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
3013 {
3014 /* TODO: Take into account the scalers once we support them */
3015 return config->base.adjusted_mode.crtc_clock;
3016 }
3017
3018 /*
3019 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3020 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3021 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3022 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3023 */
3024 static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3025 uint32_t latency)
3026 {
3027 uint32_t wm_intermediate_val, ret;
3028
3029 if (latency == 0)
3030 return UINT_MAX;
3031
3032 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
3033 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3034
3035 return ret;
3036 }
3037
3038 static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3039 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
3040 uint64_t tiling, uint32_t latency)
3041 {
3042 uint32_t ret;
3043 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3044 uint32_t wm_intermediate_val;
3045
3046 if (latency == 0)
3047 return UINT_MAX;
3048
3049 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
3050
3051 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3052 tiling == I915_FORMAT_MOD_Yf_TILED) {
3053 plane_bytes_per_line *= 4;
3054 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3055 plane_blocks_per_line /= 4;
3056 } else {
3057 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3058 }
3059
3060 wm_intermediate_val = latency * pixel_rate;
3061 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3062 plane_blocks_per_line;
3063
3064 return ret;
3065 }
3066
3067 static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3068 const struct intel_crtc *intel_crtc)
3069 {
3070 struct drm_device *dev = intel_crtc->base.dev;
3071 struct drm_i915_private *dev_priv = dev->dev_private;
3072 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3073 enum pipe pipe = intel_crtc->pipe;
3074
3075 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3076 sizeof(new_ddb->plane[pipe])))
3077 return true;
3078
3079 if (memcmp(&new_ddb->plane[pipe][PLANE_CURSOR], &cur_ddb->plane[pipe][PLANE_CURSOR],
3080 sizeof(new_ddb->plane[pipe][PLANE_CURSOR])))
3081 return true;
3082
3083 return false;
3084 }
3085
3086 static void skl_compute_wm_global_parameters(struct drm_device *dev,
3087 struct intel_wm_config *config)
3088 {
3089 struct drm_crtc *crtc;
3090 struct drm_plane *plane;
3091
3092 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3093 config->num_pipes_active += to_intel_crtc(crtc)->active;
3094
3095 /* FIXME: I don't think we need those two global parameters on SKL */
3096 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3097 struct intel_plane *intel_plane = to_intel_plane(plane);
3098
3099 config->sprites_enabled |= intel_plane->wm.enabled;
3100 config->sprites_scaled |= intel_plane->wm.scaled;
3101 }
3102 }
3103
3104 static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
3105 struct skl_pipe_wm_parameters *p)
3106 {
3107 struct drm_device *dev = crtc->dev;
3108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3109 enum pipe pipe = intel_crtc->pipe;
3110 struct drm_plane *plane;
3111 struct drm_framebuffer *fb;
3112 int i = 1; /* Index for sprite planes start */
3113
3114 p->active = intel_crtc->active;
3115 if (p->active) {
3116 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
3117 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
3118
3119 fb = crtc->primary->state->fb;
3120 /* For planar: Bpp is for uv plane, y_Bpp is for y plane */
3121 if (fb) {
3122 p->plane[0].enabled = true;
3123 p->plane[0].bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
3124 drm_format_plane_cpp(fb->pixel_format, 1) :
3125 drm_format_plane_cpp(fb->pixel_format, 0);
3126 p->plane[0].y_bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
3127 drm_format_plane_cpp(fb->pixel_format, 0) : 0;
3128 p->plane[0].tiling = fb->modifier[0];
3129 } else {
3130 p->plane[0].enabled = false;
3131 p->plane[0].bytes_per_pixel = 0;
3132 p->plane[0].y_bytes_per_pixel = 0;
3133 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
3134 }
3135 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
3136 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
3137 p->plane[0].rotation = crtc->primary->state->rotation;
3138
3139 fb = crtc->cursor->state->fb;
3140 p->plane[PLANE_CURSOR].y_bytes_per_pixel = 0;
3141 if (fb) {
3142 p->plane[PLANE_CURSOR].enabled = true;
3143 p->plane[PLANE_CURSOR].bytes_per_pixel = fb->bits_per_pixel / 8;
3144 p->plane[PLANE_CURSOR].horiz_pixels = crtc->cursor->state->crtc_w;
3145 p->plane[PLANE_CURSOR].vert_pixels = crtc->cursor->state->crtc_h;
3146 } else {
3147 p->plane[PLANE_CURSOR].enabled = false;
3148 p->plane[PLANE_CURSOR].bytes_per_pixel = 0;
3149 p->plane[PLANE_CURSOR].horiz_pixels = 64;
3150 p->plane[PLANE_CURSOR].vert_pixels = 64;
3151 }
3152 }
3153
3154 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3155 struct intel_plane *intel_plane = to_intel_plane(plane);
3156
3157 if (intel_plane->pipe == pipe &&
3158 plane->type == DRM_PLANE_TYPE_OVERLAY)
3159 p->plane[i++] = intel_plane->wm;
3160 }
3161 }
3162
3163 static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3164 struct skl_pipe_wm_parameters *p,
3165 struct intel_plane_wm_parameters *p_params,
3166 uint16_t ddb_allocation,
3167 int level,
3168 uint16_t *out_blocks, /* out */
3169 uint8_t *out_lines /* out */)
3170 {
3171 uint32_t latency = dev_priv->wm.skl_latency[level];
3172 uint32_t method1, method2;
3173 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3174 uint32_t res_blocks, res_lines;
3175 uint32_t selected_result;
3176 uint8_t bytes_per_pixel;
3177
3178 if (latency == 0 || !p->active || !p_params->enabled)
3179 return false;
3180
3181 bytes_per_pixel = p_params->y_bytes_per_pixel ?
3182 p_params->y_bytes_per_pixel :
3183 p_params->bytes_per_pixel;
3184 method1 = skl_wm_method1(p->pixel_rate,
3185 bytes_per_pixel,
3186 latency);
3187 method2 = skl_wm_method2(p->pixel_rate,
3188 p->pipe_htotal,
3189 p_params->horiz_pixels,
3190 bytes_per_pixel,
3191 p_params->tiling,
3192 latency);
3193
3194 plane_bytes_per_line = p_params->horiz_pixels * bytes_per_pixel;
3195 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3196
3197 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
3198 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
3199 uint32_t min_scanlines = 4;
3200 uint32_t y_tile_minimum;
3201 if (intel_rotation_90_or_270(p_params->rotation)) {
3202 switch (p_params->bytes_per_pixel) {
3203 case 1:
3204 min_scanlines = 16;
3205 break;
3206 case 2:
3207 min_scanlines = 8;
3208 break;
3209 case 8:
3210 WARN(1, "Unsupported pixel depth for rotation");
3211 }
3212 }
3213 y_tile_minimum = plane_blocks_per_line * min_scanlines;
3214 selected_result = max(method2, y_tile_minimum);
3215 } else {
3216 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3217 selected_result = min(method1, method2);
3218 else
3219 selected_result = method1;
3220 }
3221
3222 res_blocks = selected_result + 1;
3223 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
3224
3225 if (level >= 1 && level <= 7) {
3226 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
3227 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
3228 res_lines += 4;
3229 else
3230 res_blocks++;
3231 }
3232
3233 if (res_blocks >= ddb_allocation || res_lines > 31)
3234 return false;
3235
3236 *out_blocks = res_blocks;
3237 *out_lines = res_lines;
3238
3239 return true;
3240 }
3241
3242 static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3243 struct skl_ddb_allocation *ddb,
3244 struct skl_pipe_wm_parameters *p,
3245 enum pipe pipe,
3246 int level,
3247 int num_planes,
3248 struct skl_wm_level *result)
3249 {
3250 uint16_t ddb_blocks;
3251 int i;
3252
3253 for (i = 0; i < num_planes; i++) {
3254 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3255
3256 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
3257 p, &p->plane[i],
3258 ddb_blocks,
3259 level,
3260 &result->plane_res_b[i],
3261 &result->plane_res_l[i]);
3262 }
3263
3264 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][PLANE_CURSOR]);
3265 result->plane_en[PLANE_CURSOR] = skl_compute_plane_wm(dev_priv, p,
3266 &p->plane[PLANE_CURSOR],
3267 ddb_blocks, level,
3268 &result->plane_res_b[PLANE_CURSOR],
3269 &result->plane_res_l[PLANE_CURSOR]);
3270 }
3271
3272 static uint32_t
3273 skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
3274 {
3275 if (!to_intel_crtc(crtc)->active)
3276 return 0;
3277
3278 if (WARN_ON(p->pixel_rate == 0))
3279 return 0;
3280
3281 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
3282 }
3283
3284 static void skl_compute_transition_wm(struct drm_crtc *crtc,
3285 struct skl_pipe_wm_parameters *params,
3286 struct skl_wm_level *trans_wm /* out */)
3287 {
3288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3289 int i;
3290
3291 if (!params->active)
3292 return;
3293
3294 /* Until we know more, just disable transition WMs */
3295 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3296 trans_wm->plane_en[i] = false;
3297 trans_wm->plane_en[PLANE_CURSOR] = false;
3298 }
3299
3300 static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3301 struct skl_ddb_allocation *ddb,
3302 struct skl_pipe_wm_parameters *params,
3303 struct skl_pipe_wm *pipe_wm)
3304 {
3305 struct drm_device *dev = crtc->dev;
3306 const struct drm_i915_private *dev_priv = dev->dev_private;
3307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3308 int level, max_level = ilk_wm_max_level(dev);
3309
3310 for (level = 0; level <= max_level; level++) {
3311 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3312 level, intel_num_planes(intel_crtc),
3313 &pipe_wm->wm[level]);
3314 }
3315 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3316
3317 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
3318 }
3319
3320 static void skl_compute_wm_results(struct drm_device *dev,
3321 struct skl_pipe_wm_parameters *p,
3322 struct skl_pipe_wm *p_wm,
3323 struct skl_wm_values *r,
3324 struct intel_crtc *intel_crtc)
3325 {
3326 int level, max_level = ilk_wm_max_level(dev);
3327 enum pipe pipe = intel_crtc->pipe;
3328 uint32_t temp;
3329 int i;
3330
3331 for (level = 0; level <= max_level; level++) {
3332 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3333 temp = 0;
3334
3335 temp |= p_wm->wm[level].plane_res_l[i] <<
3336 PLANE_WM_LINES_SHIFT;
3337 temp |= p_wm->wm[level].plane_res_b[i];
3338 if (p_wm->wm[level].plane_en[i])
3339 temp |= PLANE_WM_EN;
3340
3341 r->plane[pipe][i][level] = temp;
3342 }
3343
3344 temp = 0;
3345
3346 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3347 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
3348
3349 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
3350 temp |= PLANE_WM_EN;
3351
3352 r->plane[pipe][PLANE_CURSOR][level] = temp;
3353
3354 }
3355
3356 /* transition WMs */
3357 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3358 temp = 0;
3359 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3360 temp |= p_wm->trans_wm.plane_res_b[i];
3361 if (p_wm->trans_wm.plane_en[i])
3362 temp |= PLANE_WM_EN;
3363
3364 r->plane_trans[pipe][i] = temp;
3365 }
3366
3367 temp = 0;
3368 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3369 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3370 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
3371 temp |= PLANE_WM_EN;
3372
3373 r->plane_trans[pipe][PLANE_CURSOR] = temp;
3374
3375 r->wm_linetime[pipe] = p_wm->linetime;
3376 }
3377
3378 static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3379 const struct skl_ddb_entry *entry)
3380 {
3381 if (entry->end)
3382 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3383 else
3384 I915_WRITE(reg, 0);
3385 }
3386
3387 static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3388 const struct skl_wm_values *new)
3389 {
3390 struct drm_device *dev = dev_priv->dev;
3391 struct intel_crtc *crtc;
3392
3393 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3394 int i, level, max_level = ilk_wm_max_level(dev);
3395 enum pipe pipe = crtc->pipe;
3396
3397 if (!new->dirty[pipe])
3398 continue;
3399
3400 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3401
3402 for (level = 0; level <= max_level; level++) {
3403 for (i = 0; i < intel_num_planes(crtc); i++)
3404 I915_WRITE(PLANE_WM(pipe, i, level),
3405 new->plane[pipe][i][level]);
3406 I915_WRITE(CUR_WM(pipe, level),
3407 new->plane[pipe][PLANE_CURSOR][level]);
3408 }
3409 for (i = 0; i < intel_num_planes(crtc); i++)
3410 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3411 new->plane_trans[pipe][i]);
3412 I915_WRITE(CUR_WM_TRANS(pipe),
3413 new->plane_trans[pipe][PLANE_CURSOR]);
3414
3415 for (i = 0; i < intel_num_planes(crtc); i++) {
3416 skl_ddb_entry_write(dev_priv,
3417 PLANE_BUF_CFG(pipe, i),
3418 &new->ddb.plane[pipe][i]);
3419 skl_ddb_entry_write(dev_priv,
3420 PLANE_NV12_BUF_CFG(pipe, i),
3421 &new->ddb.y_plane[pipe][i]);
3422 }
3423
3424 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3425 &new->ddb.plane[pipe][PLANE_CURSOR]);
3426 }
3427 }
3428
3429 /*
3430 * When setting up a new DDB allocation arrangement, we need to correctly
3431 * sequence the times at which the new allocations for the pipes are taken into
3432 * account or we'll have pipes fetching from space previously allocated to
3433 * another pipe.
3434 *
3435 * Roughly the sequence looks like:
3436 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3437 * overlapping with a previous light-up pipe (another way to put it is:
3438 * pipes with their new allocation strickly included into their old ones).
3439 * 2. re-allocate the other pipes that get their allocation reduced
3440 * 3. allocate the pipes having their allocation increased
3441 *
3442 * Steps 1. and 2. are here to take care of the following case:
3443 * - Initially DDB looks like this:
3444 * | B | C |
3445 * - enable pipe A.
3446 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3447 * allocation
3448 * | A | B | C |
3449 *
3450 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3451 */
3452
3453 static void
3454 skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3455 {
3456 int plane;
3457
3458 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3459
3460 for_each_plane(dev_priv, pipe, plane) {
3461 I915_WRITE(PLANE_SURF(pipe, plane),
3462 I915_READ(PLANE_SURF(pipe, plane)));
3463 }
3464 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3465 }
3466
3467 static bool
3468 skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3469 const struct skl_ddb_allocation *new,
3470 enum pipe pipe)
3471 {
3472 uint16_t old_size, new_size;
3473
3474 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3475 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3476
3477 return old_size != new_size &&
3478 new->pipe[pipe].start >= old->pipe[pipe].start &&
3479 new->pipe[pipe].end <= old->pipe[pipe].end;
3480 }
3481
3482 static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3483 struct skl_wm_values *new_values)
3484 {
3485 struct drm_device *dev = dev_priv->dev;
3486 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3487 bool reallocated[I915_MAX_PIPES] = {};
3488 struct intel_crtc *crtc;
3489 enum pipe pipe;
3490
3491 new_ddb = &new_values->ddb;
3492 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3493
3494 /*
3495 * First pass: flush the pipes with the new allocation contained into
3496 * the old space.
3497 *
3498 * We'll wait for the vblank on those pipes to ensure we can safely
3499 * re-allocate the freed space without this pipe fetching from it.
3500 */
3501 for_each_intel_crtc(dev, crtc) {
3502 if (!crtc->active)
3503 continue;
3504
3505 pipe = crtc->pipe;
3506
3507 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3508 continue;
3509
3510 skl_wm_flush_pipe(dev_priv, pipe, 1);
3511 intel_wait_for_vblank(dev, pipe);
3512
3513 reallocated[pipe] = true;
3514 }
3515
3516
3517 /*
3518 * Second pass: flush the pipes that are having their allocation
3519 * reduced, but overlapping with a previous allocation.
3520 *
3521 * Here as well we need to wait for the vblank to make sure the freed
3522 * space is not used anymore.
3523 */
3524 for_each_intel_crtc(dev, crtc) {
3525 if (!crtc->active)
3526 continue;
3527
3528 pipe = crtc->pipe;
3529
3530 if (reallocated[pipe])
3531 continue;
3532
3533 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3534 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3535 skl_wm_flush_pipe(dev_priv, pipe, 2);
3536 intel_wait_for_vblank(dev, pipe);
3537 reallocated[pipe] = true;
3538 }
3539 }
3540
3541 /*
3542 * Third pass: flush the pipes that got more space allocated.
3543 *
3544 * We don't need to actively wait for the update here, next vblank
3545 * will just get more DDB space with the correct WM values.
3546 */
3547 for_each_intel_crtc(dev, crtc) {
3548 if (!crtc->active)
3549 continue;
3550
3551 pipe = crtc->pipe;
3552
3553 /*
3554 * At this point, only the pipes more space than before are
3555 * left to re-allocate.
3556 */
3557 if (reallocated[pipe])
3558 continue;
3559
3560 skl_wm_flush_pipe(dev_priv, pipe, 3);
3561 }
3562 }
3563
3564 static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3565 struct skl_pipe_wm_parameters *params,
3566 struct intel_wm_config *config,
3567 struct skl_ddb_allocation *ddb, /* out */
3568 struct skl_pipe_wm *pipe_wm /* out */)
3569 {
3570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3571
3572 skl_compute_wm_pipe_parameters(crtc, params);
3573 skl_allocate_pipe_ddb(crtc, config, params, ddb);
3574 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3575
3576 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3577 return false;
3578
3579 intel_crtc->wm.skl_active = *pipe_wm;
3580
3581 return true;
3582 }
3583
3584 static void skl_update_other_pipe_wm(struct drm_device *dev,
3585 struct drm_crtc *crtc,
3586 struct intel_wm_config *config,
3587 struct skl_wm_values *r)
3588 {
3589 struct intel_crtc *intel_crtc;
3590 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3591
3592 /*
3593 * If the WM update hasn't changed the allocation for this_crtc (the
3594 * crtc we are currently computing the new WM values for), other
3595 * enabled crtcs will keep the same allocation and we don't need to
3596 * recompute anything for them.
3597 */
3598 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3599 return;
3600
3601 /*
3602 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3603 * other active pipes need new DDB allocation and WM values.
3604 */
3605 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3606 base.head) {
3607 struct skl_pipe_wm_parameters params = {};
3608 struct skl_pipe_wm pipe_wm = {};
3609 bool wm_changed;
3610
3611 if (this_crtc->pipe == intel_crtc->pipe)
3612 continue;
3613
3614 if (!intel_crtc->active)
3615 continue;
3616
3617 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3618 ¶ms, config,
3619 &r->ddb, &pipe_wm);
3620
3621 /*
3622 * If we end up re-computing the other pipe WM values, it's
3623 * because it was really needed, so we expect the WM values to
3624 * be different.
3625 */
3626 WARN_ON(!wm_changed);
3627
3628 skl_compute_wm_results(dev, ¶ms, &pipe_wm, r, intel_crtc);
3629 r->dirty[intel_crtc->pipe] = true;
3630 }
3631 }
3632
3633 static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3634 {
3635 watermarks->wm_linetime[pipe] = 0;
3636 memset(watermarks->plane[pipe], 0,
3637 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
3638 memset(watermarks->plane_trans[pipe],
3639 0, sizeof(uint32_t) * I915_MAX_PLANES);
3640 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
3641
3642 /* Clear ddb entries for pipe */
3643 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3644 memset(&watermarks->ddb.plane[pipe], 0,
3645 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3646 memset(&watermarks->ddb.y_plane[pipe], 0,
3647 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3648 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3649 sizeof(struct skl_ddb_entry));
3650
3651 }
3652
3653 static void skl_update_wm(struct drm_crtc *crtc)
3654 {
3655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3656 struct drm_device *dev = crtc->dev;
3657 struct drm_i915_private *dev_priv = dev->dev_private;
3658 struct skl_pipe_wm_parameters params = {};
3659 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3660 struct skl_pipe_wm pipe_wm = {};
3661 struct intel_wm_config config = {};
3662
3663
3664 /* Clear all dirty flags */
3665 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3666
3667 skl_clear_wm(results, intel_crtc->pipe);
3668
3669 skl_compute_wm_global_parameters(dev, &config);
3670
3671 if (!skl_update_pipe_wm(crtc, ¶ms, &config,
3672 &results->ddb, &pipe_wm))
3673 return;
3674
3675 skl_compute_wm_results(dev, ¶ms, &pipe_wm, results, intel_crtc);
3676 results->dirty[intel_crtc->pipe] = true;
3677
3678 skl_update_other_pipe_wm(dev, crtc, &config, results);
3679 skl_write_wm_values(dev_priv, results);
3680 skl_flush_wm_values(dev_priv, results);
3681
3682 /* store the new configuration */
3683 dev_priv->wm.skl_hw = *results;
3684 }
3685
3686 static void
3687 skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3688 uint32_t sprite_width, uint32_t sprite_height,
3689 int pixel_size, bool enabled, bool scaled)
3690 {
3691 struct intel_plane *intel_plane = to_intel_plane(plane);
3692 struct drm_framebuffer *fb = plane->state->fb;
3693
3694 intel_plane->wm.enabled = enabled;
3695 intel_plane->wm.scaled = scaled;
3696 intel_plane->wm.horiz_pixels = sprite_width;
3697 intel_plane->wm.vert_pixels = sprite_height;
3698 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3699
3700 /* For planar: Bpp is for UV plane, y_Bpp is for Y plane */
3701 intel_plane->wm.bytes_per_pixel =
3702 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3703 drm_format_plane_cpp(plane->state->fb->pixel_format, 1) : pixel_size;
3704 intel_plane->wm.y_bytes_per_pixel =
3705 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3706 drm_format_plane_cpp(plane->state->fb->pixel_format, 0) : 0;
3707
3708 /*
3709 * Framebuffer can be NULL on plane disable, but it does not
3710 * matter for watermarks if we assume no tiling in that case.
3711 */
3712 if (fb)
3713 intel_plane->wm.tiling = fb->modifier[0];
3714 intel_plane->wm.rotation = plane->state->rotation;
3715
3716 skl_update_wm(crtc);
3717 }
3718
3719 static void ilk_update_wm(struct drm_crtc *crtc)
3720 {
3721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3722 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3723 struct drm_device *dev = crtc->dev;
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725 struct ilk_wm_maximums max;
3726 static const struct ilk_wm_values zero_values;
3727 struct ilk_wm_values results = zero_values;
3728 enum intel_ddb_partitioning partitioning;
3729 static const struct intel_pipe_wm zero_wm;
3730 struct intel_pipe_wm pipe_wm = zero_wm;
3731 struct intel_pipe_wm lp_wm_1_2 = zero_wm, lp_wm_5_6 = zero_wm,
3732 *best_lp_wm;
3733 static const struct intel_wm_config zero_config;
3734 struct intel_wm_config config = zero_config;
3735
3736 WARN_ON(cstate->base.active != intel_crtc->active);
3737
3738 intel_compute_pipe_wm(cstate, &pipe_wm);
3739
3740 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3741 return;
3742
3743 intel_crtc->wm.active = pipe_wm;
3744
3745 ilk_compute_wm_config(dev, &config);
3746
3747 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3748 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3749
3750 /* 5/6 split only in single pipe config on IVB+ */
3751 if (INTEL_INFO(dev)->gen >= 7 &&
3752 config.num_pipes_active == 1 && config.sprites_enabled) {
3753 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3754 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3755
3756 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3757 } else {
3758 best_lp_wm = &lp_wm_1_2;
3759 }
3760
3761 partitioning = (best_lp_wm == &lp_wm_1_2) ?
3762 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3763
3764 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3765
3766 ilk_write_wm_values(dev_priv, &results);
3767 }
3768
3769 static void
3770 ilk_update_sprite_wm(struct drm_plane *plane,
3771 struct drm_crtc *crtc,
3772 uint32_t sprite_width, uint32_t sprite_height,
3773 int pixel_size, bool enabled, bool scaled)
3774 {
3775 struct drm_device *dev = plane->dev;
3776 struct intel_plane *intel_plane = to_intel_plane(plane);
3777
3778 /*
3779 * IVB workaround: must disable low power watermarks for at least
3780 * one frame before enabling scaling. LP watermarks can be re-enabled
3781 * when scaling is disabled.
3782 *
3783 * WaCxSRDisabledForSpriteScaling:ivb
3784 */
3785 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3786 intel_wait_for_vblank(dev, intel_plane->pipe);
3787
3788 ilk_update_wm(crtc);
3789 }
3790
3791 static void skl_pipe_wm_active_state(uint32_t val,
3792 struct skl_pipe_wm *active,
3793 bool is_transwm,
3794 bool is_cursor,
3795 int i,
3796 int level)
3797 {
3798 bool is_enabled = (val & PLANE_WM_EN) != 0;
3799
3800 if (!is_transwm) {
3801 if (!is_cursor) {
3802 active->wm[level].plane_en[i] = is_enabled;
3803 active->wm[level].plane_res_b[i] =
3804 val & PLANE_WM_BLOCKS_MASK;
3805 active->wm[level].plane_res_l[i] =
3806 (val >> PLANE_WM_LINES_SHIFT) &
3807 PLANE_WM_LINES_MASK;
3808 } else {
3809 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3810 active->wm[level].plane_res_b[PLANE_CURSOR] =
3811 val & PLANE_WM_BLOCKS_MASK;
3812 active->wm[level].plane_res_l[PLANE_CURSOR] =
3813 (val >> PLANE_WM_LINES_SHIFT) &
3814 PLANE_WM_LINES_MASK;
3815 }
3816 } else {
3817 if (!is_cursor) {
3818 active->trans_wm.plane_en[i] = is_enabled;
3819 active->trans_wm.plane_res_b[i] =
3820 val & PLANE_WM_BLOCKS_MASK;
3821 active->trans_wm.plane_res_l[i] =
3822 (val >> PLANE_WM_LINES_SHIFT) &
3823 PLANE_WM_LINES_MASK;
3824 } else {
3825 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3826 active->trans_wm.plane_res_b[PLANE_CURSOR] =
3827 val & PLANE_WM_BLOCKS_MASK;
3828 active->trans_wm.plane_res_l[PLANE_CURSOR] =
3829 (val >> PLANE_WM_LINES_SHIFT) &
3830 PLANE_WM_LINES_MASK;
3831 }
3832 }
3833 }
3834
3835 static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3836 {
3837 struct drm_device *dev = crtc->dev;
3838 struct drm_i915_private *dev_priv = dev->dev_private;
3839 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3841 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3842 enum pipe pipe = intel_crtc->pipe;
3843 int level, i, max_level;
3844 uint32_t temp;
3845
3846 max_level = ilk_wm_max_level(dev);
3847
3848 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3849
3850 for (level = 0; level <= max_level; level++) {
3851 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3852 hw->plane[pipe][i][level] =
3853 I915_READ(PLANE_WM(pipe, i, level));
3854 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
3855 }
3856
3857 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3858 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3859 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
3860
3861 if (!intel_crtc->active)
3862 return;
3863
3864 hw->dirty[pipe] = true;
3865
3866 active->linetime = hw->wm_linetime[pipe];
3867
3868 for (level = 0; level <= max_level; level++) {
3869 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3870 temp = hw->plane[pipe][i][level];
3871 skl_pipe_wm_active_state(temp, active, false,
3872 false, i, level);
3873 }
3874 temp = hw->plane[pipe][PLANE_CURSOR][level];
3875 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3876 }
3877
3878 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3879 temp = hw->plane_trans[pipe][i];
3880 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3881 }
3882
3883 temp = hw->plane_trans[pipe][PLANE_CURSOR];
3884 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3885 }
3886
3887 void skl_wm_get_hw_state(struct drm_device *dev)
3888 {
3889 struct drm_i915_private *dev_priv = dev->dev_private;
3890 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3891 struct drm_crtc *crtc;
3892
3893 skl_ddb_get_hw_state(dev_priv, ddb);
3894 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3895 skl_pipe_wm_get_hw_state(crtc);
3896 }
3897
3898 static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3899 {
3900 struct drm_device *dev = crtc->dev;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
3902 struct ilk_wm_values *hw = &dev_priv->wm.hw;
3903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3904 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3905 enum pipe pipe = intel_crtc->pipe;
3906 static const unsigned int wm0_pipe_reg[] = {
3907 [PIPE_A] = WM0_PIPEA_ILK,
3908 [PIPE_B] = WM0_PIPEB_ILK,
3909 [PIPE_C] = WM0_PIPEC_IVB,
3910 };
3911
3912 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3913 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3914 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3915
3916 memset(active, 0, sizeof(*active));
3917
3918 active->pipe_enabled = intel_crtc->active;
3919
3920 if (active->pipe_enabled) {
3921 u32 tmp = hw->wm_pipe[pipe];
3922
3923 /*
3924 * For active pipes LP0 watermark is marked as
3925 * enabled, and LP1+ watermaks as disabled since
3926 * we can't really reverse compute them in case
3927 * multiple pipes are active.
3928 */
3929 active->wm[0].enable = true;
3930 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3931 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3932 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3933 active->linetime = hw->wm_linetime[pipe];
3934 } else {
3935 int level, max_level = ilk_wm_max_level(dev);
3936
3937 /*
3938 * For inactive pipes, all watermark levels
3939 * should be marked as enabled but zeroed,
3940 * which is what we'd compute them to.
3941 */
3942 for (level = 0; level <= max_level; level++)
3943 active->wm[level].enable = true;
3944 }
3945 }
3946
3947 #define _FW_WM(value, plane) \
3948 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3949 #define _FW_WM_VLV(value, plane) \
3950 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3951
3952 static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3953 struct vlv_wm_values *wm)
3954 {
3955 enum pipe pipe;
3956 uint32_t tmp;
3957
3958 for_each_pipe(dev_priv, pipe) {
3959 tmp = I915_READ(VLV_DDL(pipe));
3960
3961 wm->ddl[pipe].primary =
3962 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3963 wm->ddl[pipe].cursor =
3964 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3965 wm->ddl[pipe].sprite[0] =
3966 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3967 wm->ddl[pipe].sprite[1] =
3968 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3969 }
3970
3971 tmp = I915_READ(DSPFW1);
3972 wm->sr.plane = _FW_WM(tmp, SR);
3973 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3974 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3975 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3976
3977 tmp = I915_READ(DSPFW2);
3978 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3979 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3980 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3981
3982 tmp = I915_READ(DSPFW3);
3983 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3984
3985 if (IS_CHERRYVIEW(dev_priv)) {
3986 tmp = I915_READ(DSPFW7_CHV);
3987 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3988 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3989
3990 tmp = I915_READ(DSPFW8_CHV);
3991 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3992 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
3993
3994 tmp = I915_READ(DSPFW9_CHV);
3995 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
3996 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
3997
3998 tmp = I915_READ(DSPHOWM);
3999 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4000 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4001 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4002 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4003 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4004 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4005 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4006 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4007 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4008 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4009 } else {
4010 tmp = I915_READ(DSPFW7);
4011 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4012 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4013
4014 tmp = I915_READ(DSPHOWM);
4015 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4016 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4017 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4018 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4019 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4020 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4021 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4022 }
4023 }
4024
4025 #undef _FW_WM
4026 #undef _FW_WM_VLV
4027
4028 void vlv_wm_get_hw_state(struct drm_device *dev)
4029 {
4030 struct drm_i915_private *dev_priv = to_i915(dev);
4031 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4032 struct intel_plane *plane;
4033 enum pipe pipe;
4034 u32 val;
4035
4036 vlv_read_wm_values(dev_priv, wm);
4037
4038 for_each_intel_plane(dev, plane) {
4039 switch (plane->base.type) {
4040 int sprite;
4041 case DRM_PLANE_TYPE_CURSOR:
4042 plane->wm.fifo_size = 63;
4043 break;
4044 case DRM_PLANE_TYPE_PRIMARY:
4045 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4046 break;
4047 case DRM_PLANE_TYPE_OVERLAY:
4048 sprite = plane->plane;
4049 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4050 break;
4051 }
4052 }
4053
4054 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4055 wm->level = VLV_WM_LEVEL_PM2;
4056
4057 if (IS_CHERRYVIEW(dev_priv)) {
4058 mutex_lock(&dev_priv->rps.hw_lock);
4059
4060 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4061 if (val & DSP_MAXFIFO_PM5_ENABLE)
4062 wm->level = VLV_WM_LEVEL_PM5;
4063
4064 /*
4065 * If DDR DVFS is disabled in the BIOS, Punit
4066 * will never ack the request. So if that happens
4067 * assume we don't have to enable/disable DDR DVFS
4068 * dynamically. To test that just set the REQ_ACK
4069 * bit to poke the Punit, but don't change the
4070 * HIGH/LOW bits so that we don't actually change
4071 * the current state.
4072 */
4073 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4074 val |= FORCE_DDR_FREQ_REQ_ACK;
4075 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4076
4077 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4078 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4079 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4080 "assuming DDR DVFS is disabled\n");
4081 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4082 } else {
4083 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4084 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4085 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4086 }
4087
4088 mutex_unlock(&dev_priv->rps.hw_lock);
4089 }
4090
4091 for_each_pipe(dev_priv, pipe)
4092 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4093 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4094 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4095
4096 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4097 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4098 }
4099
4100 void ilk_wm_get_hw_state(struct drm_device *dev)
4101 {
4102 struct drm_i915_private *dev_priv = dev->dev_private;
4103 struct ilk_wm_values *hw = &dev_priv->wm.hw;
4104 struct drm_crtc *crtc;
4105
4106 for_each_crtc(dev, crtc)
4107 ilk_pipe_wm_get_hw_state(crtc);
4108
4109 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4110 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4111 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4112
4113 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
4114 if (INTEL_INFO(dev)->gen >= 7) {
4115 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4116 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4117 }
4118
4119 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4120 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4121 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4122 else if (IS_IVYBRIDGE(dev))
4123 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4124 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4125
4126 hw->enable_fbc_wm =
4127 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4128 }
4129
4130 /**
4131 * intel_update_watermarks - update FIFO watermark values based on current modes
4132 *
4133 * Calculate watermark values for the various WM regs based on current mode
4134 * and plane configuration.
4135 *
4136 * There are several cases to deal with here:
4137 * - normal (i.e. non-self-refresh)
4138 * - self-refresh (SR) mode
4139 * - lines are large relative to FIFO size (buffer can hold up to 2)
4140 * - lines are small relative to FIFO size (buffer can hold more than 2
4141 * lines), so need to account for TLB latency
4142 *
4143 * The normal calculation is:
4144 * watermark = dotclock * bytes per pixel * latency
4145 * where latency is platform & configuration dependent (we assume pessimal
4146 * values here).
4147 *
4148 * The SR calculation is:
4149 * watermark = (trunc(latency/line time)+1) * surface width *
4150 * bytes per pixel
4151 * where
4152 * line time = htotal / dotclock
4153 * surface width = hdisplay for normal plane and 64 for cursor
4154 * and latency is assumed to be high, as above.
4155 *
4156 * The final value programmed to the register should always be rounded up,
4157 * and include an extra 2 entries to account for clock crossings.
4158 *
4159 * We don't use the sprite, so we can ignore that. And on Crestline we have
4160 * to set the non-SR watermarks to 8.
4161 */
4162 void intel_update_watermarks(struct drm_crtc *crtc)
4163 {
4164 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4165
4166 if (dev_priv->display.update_wm)
4167 dev_priv->display.update_wm(crtc);
4168 }
4169
4170 void intel_update_sprite_watermarks(struct drm_plane *plane,
4171 struct drm_crtc *crtc,
4172 uint32_t sprite_width,
4173 uint32_t sprite_height,
4174 int pixel_size,
4175 bool enabled, bool scaled)
4176 {
4177 struct drm_i915_private *dev_priv = plane->dev->dev_private;
4178
4179 if (dev_priv->display.update_sprite_wm)
4180 dev_priv->display.update_sprite_wm(plane, crtc,
4181 sprite_width, sprite_height,
4182 pixel_size, enabled, scaled);
4183 }
4184
4185 /**
4186 * Lock protecting IPS related data structures
4187 */
4188 #ifdef __NetBSD__
4189 spinlock_t mchdev_lock;
4190 #else
4191 DEFINE_SPINLOCK(mchdev_lock);
4192 #endif
4193
4194 /* Global for IPS driver to get at the current i915 device. Protected by
4195 * mchdev_lock. */
4196 static struct drm_i915_private *i915_mch_dev;
4197
4198 bool ironlake_set_drps(struct drm_device *dev, u8 val)
4199 {
4200 struct drm_i915_private *dev_priv = dev->dev_private;
4201 u16 rgvswctl;
4202
4203 assert_spin_locked(&mchdev_lock);
4204
4205 rgvswctl = I915_READ16(MEMSWCTL);
4206 if (rgvswctl & MEMCTL_CMD_STS) {
4207 DRM_DEBUG("gpu busy, RCS change rejected\n");
4208 return false; /* still busy with another command */
4209 }
4210
4211 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4212 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4213 I915_WRITE16(MEMSWCTL, rgvswctl);
4214 POSTING_READ16(MEMSWCTL);
4215
4216 rgvswctl |= MEMCTL_CMD_STS;
4217 I915_WRITE16(MEMSWCTL, rgvswctl);
4218
4219 return true;
4220 }
4221
4222 static void ironlake_enable_drps(struct drm_device *dev)
4223 {
4224 struct drm_i915_private *dev_priv = dev->dev_private;
4225 u32 rgvmodectl = I915_READ(MEMMODECTL);
4226 u8 fmax, fmin, fstart, vstart;
4227
4228 spin_lock_irq(&mchdev_lock);
4229
4230 /* Enable temp reporting */
4231 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4232 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4233
4234 /* 100ms RC evaluation intervals */
4235 I915_WRITE(RCUPEI, 100000);
4236 I915_WRITE(RCDNEI, 100000);
4237
4238 /* Set max/min thresholds to 90ms and 80ms respectively */
4239 I915_WRITE(RCBMAXAVG, 90000);
4240 I915_WRITE(RCBMINAVG, 80000);
4241
4242 I915_WRITE(MEMIHYST, 1);
4243
4244 /* Set up min, max, and cur for interrupt handling */
4245 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4246 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4247 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4248 MEMMODE_FSTART_SHIFT;
4249
4250 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
4251 PXVFREQ_PX_SHIFT;
4252
4253 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4254 dev_priv->ips.fstart = fstart;
4255
4256 dev_priv->ips.max_delay = fstart;
4257 dev_priv->ips.min_delay = fmin;
4258 dev_priv->ips.cur_delay = fstart;
4259
4260 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4261 fmax, fmin, fstart);
4262
4263 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4264
4265 /*
4266 * Interrupts will be enabled in ironlake_irq_postinstall
4267 */
4268
4269 I915_WRITE(VIDSTART, vstart);
4270 POSTING_READ(VIDSTART);
4271
4272 rgvmodectl |= MEMMODE_SWMODE_EN;
4273 I915_WRITE(MEMMODECTL, rgvmodectl);
4274
4275 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
4276 DRM_ERROR("stuck trying to change perf mode\n");
4277 mdelay(1);
4278
4279 ironlake_set_drps(dev, fstart);
4280
4281 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4282 I915_READ(DDREC) + I915_READ(CSIEC);
4283 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4284 dev_priv->ips.last_count2 = I915_READ(GFXEC);
4285 dev_priv->ips.last_time2 = ktime_get_raw_ns();
4286
4287 spin_unlock_irq(&mchdev_lock);
4288 }
4289
4290 static void ironlake_disable_drps(struct drm_device *dev)
4291 {
4292 struct drm_i915_private *dev_priv = dev->dev_private;
4293 u16 rgvswctl;
4294
4295 spin_lock_irq(&mchdev_lock);
4296
4297 rgvswctl = I915_READ16(MEMSWCTL);
4298
4299 /* Ack interrupts, disable EFC interrupt */
4300 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4301 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4302 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4303 I915_WRITE(DEIIR, DE_PCU_EVENT);
4304 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4305
4306 /* Go back to the starting frequency */
4307 ironlake_set_drps(dev, dev_priv->ips.fstart);
4308 mdelay(1);
4309 rgvswctl |= MEMCTL_CMD_STS;
4310 I915_WRITE(MEMSWCTL, rgvswctl);
4311 mdelay(1);
4312
4313 spin_unlock_irq(&mchdev_lock);
4314 }
4315
4316 /* There's a funny hw issue where the hw returns all 0 when reading from
4317 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4318 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4319 * all limits and the gpu stuck at whatever frequency it is at atm).
4320 */
4321 static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
4322 {
4323 u32 limits;
4324
4325 /* Only set the down limit when we've reached the lowest level to avoid
4326 * getting more interrupts, otherwise leave this clear. This prevents a
4327 * race in the hw when coming out of rc6: There's a tiny window where
4328 * the hw runs at the minimal clock before selecting the desired
4329 * frequency, if the down threshold expires in that window we will not
4330 * receive a down interrupt. */
4331 if (IS_GEN9(dev_priv->dev)) {
4332 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4333 if (val <= dev_priv->rps.min_freq_softlimit)
4334 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4335 } else {
4336 limits = dev_priv->rps.max_freq_softlimit << 24;
4337 if (val <= dev_priv->rps.min_freq_softlimit)
4338 limits |= dev_priv->rps.min_freq_softlimit << 16;
4339 }
4340
4341 return limits;
4342 }
4343
4344 static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4345 {
4346 int new_power;
4347 u32 threshold_up = 0, threshold_down = 0; /* in % */
4348 u32 ei_up = 0, ei_down = 0;
4349
4350 new_power = dev_priv->rps.power;
4351 switch (dev_priv->rps.power) {
4352 case LOW_POWER:
4353 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
4354 new_power = BETWEEN;
4355 break;
4356
4357 case BETWEEN:
4358 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
4359 new_power = LOW_POWER;
4360 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
4361 new_power = HIGH_POWER;
4362 break;
4363
4364 case HIGH_POWER:
4365 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
4366 new_power = BETWEEN;
4367 break;
4368 }
4369 /* Max/min bins are special */
4370 if (val <= dev_priv->rps.min_freq_softlimit)
4371 new_power = LOW_POWER;
4372 if (val >= dev_priv->rps.max_freq_softlimit)
4373 new_power = HIGH_POWER;
4374 if (new_power == dev_priv->rps.power)
4375 return;
4376
4377 /* Note the units here are not exactly 1us, but 1280ns. */
4378 switch (new_power) {
4379 case LOW_POWER:
4380 /* Upclock if more than 95% busy over 16ms */
4381 ei_up = 16000;
4382 threshold_up = 95;
4383
4384 /* Downclock if less than 85% busy over 32ms */
4385 ei_down = 32000;
4386 threshold_down = 85;
4387 break;
4388
4389 case BETWEEN:
4390 /* Upclock if more than 90% busy over 13ms */
4391 ei_up = 13000;
4392 threshold_up = 90;
4393
4394 /* Downclock if less than 75% busy over 32ms */
4395 ei_down = 32000;
4396 threshold_down = 75;
4397 break;
4398
4399 case HIGH_POWER:
4400 /* Upclock if more than 85% busy over 10ms */
4401 ei_up = 10000;
4402 threshold_up = 85;
4403
4404 /* Downclock if less than 60% busy over 32ms */
4405 ei_down = 32000;
4406 threshold_down = 60;
4407 break;
4408 }
4409
4410 /* When byt can survive without system hang with dynamic
4411 * sw freq adjustments, this restriction can be lifted.
4412 */
4413 if (IS_VALLEYVIEW(dev_priv))
4414 goto skip_hw_write;
4415
4416 I915_WRITE(GEN6_RP_UP_EI,
4417 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4418 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4419 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4420
4421 I915_WRITE(GEN6_RP_DOWN_EI,
4422 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4423 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4424 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4425
4426 I915_WRITE(GEN6_RP_CONTROL,
4427 GEN6_RP_MEDIA_TURBO |
4428 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4429 GEN6_RP_MEDIA_IS_GFX |
4430 GEN6_RP_ENABLE |
4431 GEN6_RP_UP_BUSY_AVG |
4432 GEN6_RP_DOWN_IDLE_AVG);
4433
4434 skip_hw_write:
4435 dev_priv->rps.power = new_power;
4436 dev_priv->rps.up_threshold = threshold_up;
4437 dev_priv->rps.down_threshold = threshold_down;
4438 dev_priv->rps.last_adj = 0;
4439 }
4440
4441 static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4442 {
4443 u32 mask = 0;
4444
4445 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
4446 if (val > dev_priv->rps.min_freq_softlimit)
4447 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4448 if (val < dev_priv->rps.max_freq_softlimit)
4449 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
4450
4451 mask &= dev_priv->pm_rps_events;
4452
4453 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
4454 }
4455
4456 /* gen6_set_rps is called to update the frequency request, but should also be
4457 * called when the range (min_delay and max_delay) is modified so that we can
4458 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
4459 static void gen6_set_rps(struct drm_device *dev, u8 val)
4460 {
4461 struct drm_i915_private *dev_priv = dev->dev_private;
4462
4463 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4464 if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0))
4465 return;
4466
4467 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4468 WARN_ON(val > dev_priv->rps.max_freq);
4469 WARN_ON(val < dev_priv->rps.min_freq);
4470
4471 /* min/max delay may still have been modified so be sure to
4472 * write the limits value.
4473 */
4474 if (val != dev_priv->rps.cur_freq) {
4475 gen6_set_rps_thresholds(dev_priv, val);
4476
4477 if (IS_GEN9(dev))
4478 I915_WRITE(GEN6_RPNSWREQ,
4479 GEN9_FREQUENCY(val));
4480 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4481 I915_WRITE(GEN6_RPNSWREQ,
4482 HSW_FREQUENCY(val));
4483 else
4484 I915_WRITE(GEN6_RPNSWREQ,
4485 GEN6_FREQUENCY(val) |
4486 GEN6_OFFSET(0) |
4487 GEN6_AGGRESSIVE_TURBO);
4488 }
4489
4490 /* Make sure we continue to get interrupts
4491 * until we hit the minimum or maximum frequencies.
4492 */
4493 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
4494 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4495
4496 POSTING_READ(GEN6_RPNSWREQ);
4497
4498 dev_priv->rps.cur_freq = val;
4499 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4500 }
4501
4502 static void valleyview_set_rps(struct drm_device *dev, u8 val)
4503 {
4504 struct drm_i915_private *dev_priv = dev->dev_private;
4505
4506 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4507 WARN_ON(val > dev_priv->rps.max_freq);
4508 WARN_ON(val < dev_priv->rps.min_freq);
4509
4510 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4511 "Odd GPU freq value\n"))
4512 val &= ~1;
4513
4514 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4515
4516 if (val != dev_priv->rps.cur_freq) {
4517 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4518 if (!IS_CHERRYVIEW(dev_priv))
4519 gen6_set_rps_thresholds(dev_priv, val);
4520 }
4521
4522 dev_priv->rps.cur_freq = val;
4523 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4524 }
4525
4526 /* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
4527 *
4528 * * If Gfx is Idle, then
4529 * 1. Forcewake Media well.
4530 * 2. Request idle freq.
4531 * 3. Release Forcewake of Media well.
4532 */
4533 static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4534 {
4535 u32 val = dev_priv->rps.idle_freq;
4536
4537 if (dev_priv->rps.cur_freq <= val)
4538 return;
4539
4540 /* Wake up the media well, as that takes a lot less
4541 * power than the Render well. */
4542 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4543 valleyview_set_rps(dev_priv->dev, val);
4544 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
4545 }
4546
4547 void gen6_rps_busy(struct drm_i915_private *dev_priv)
4548 {
4549 mutex_lock(&dev_priv->rps.hw_lock);
4550 if (dev_priv->rps.enabled) {
4551 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
4552 gen6_rps_reset_ei(dev_priv);
4553 I915_WRITE(GEN6_PMINTRMSK,
4554 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4555 }
4556 mutex_unlock(&dev_priv->rps.hw_lock);
4557 }
4558
4559 void gen6_rps_idle(struct drm_i915_private *dev_priv)
4560 {
4561 struct drm_device *dev = dev_priv->dev;
4562
4563 mutex_lock(&dev_priv->rps.hw_lock);
4564 if (dev_priv->rps.enabled) {
4565 if (IS_VALLEYVIEW(dev))
4566 vlv_set_rps_idle(dev_priv);
4567 else
4568 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4569 dev_priv->rps.last_adj = 0;
4570 I915_WRITE(GEN6_PMINTRMSK,
4571 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
4572 }
4573 mutex_unlock(&dev_priv->rps.hw_lock);
4574
4575 spin_lock(&dev_priv->rps.client_lock);
4576 while (!list_empty(&dev_priv->rps.clients))
4577 list_del_init(dev_priv->rps.clients.next);
4578 spin_unlock(&dev_priv->rps.client_lock);
4579 }
4580
4581 void gen6_rps_boost(struct drm_i915_private *dev_priv,
4582 struct intel_rps_client *rps,
4583 unsigned long submitted)
4584 {
4585 /* This is intentionally racy! We peek at the state here, then
4586 * validate inside the RPS worker.
4587 */
4588 if (!(dev_priv->mm.busy &&
4589 dev_priv->rps.enabled &&
4590 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4591 return;
4592
4593 /* Force a RPS boost (and don't count it against the client) if
4594 * the GPU is severely congested.
4595 */
4596 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
4597 rps = NULL;
4598
4599 spin_lock(&dev_priv->rps.client_lock);
4600 if (rps == NULL || list_empty(&rps->link)) {
4601 spin_lock_irq(&dev_priv->irq_lock);
4602 if (dev_priv->rps.interrupts_enabled) {
4603 dev_priv->rps.client_boost = true;
4604 queue_work(dev_priv->wq, &dev_priv->rps.work);
4605 }
4606 spin_unlock_irq(&dev_priv->irq_lock);
4607
4608 if (rps != NULL) {
4609 list_add(&rps->link, &dev_priv->rps.clients);
4610 rps->boosts++;
4611 } else
4612 dev_priv->rps.boosts++;
4613 }
4614 spin_unlock(&dev_priv->rps.client_lock);
4615 }
4616
4617 void intel_set_rps(struct drm_device *dev, u8 val)
4618 {
4619 if (IS_VALLEYVIEW(dev))
4620 valleyview_set_rps(dev, val);
4621 else
4622 gen6_set_rps(dev, val);
4623 }
4624
4625 static void gen9_disable_rc6(struct drm_device *dev)
4626 {
4627 struct drm_i915_private *dev_priv = dev->dev_private;
4628
4629 I915_WRITE(GEN6_RC_CONTROL, 0);
4630 }
4631
4632 static void gen9_disable_rps(struct drm_device *dev)
4633 {
4634 struct drm_i915_private *dev_priv = dev->dev_private;
4635
4636 I915_WRITE(GEN9_PG_ENABLE, 0);
4637 }
4638
4639 static void gen6_disable_rc6(struct drm_device *dev)
4640 {
4641 struct drm_i915_private *dev_priv = dev->dev_private;
4642
4643 I915_WRITE(GEN6_RC_CONTROL, 0);
4644 }
4645
4646 static void gen6_disable_rps(struct drm_device *dev)
4647 {
4648 struct drm_i915_private *dev_priv = dev->dev_private;
4649
4650 I915_WRITE(GEN6_RPNSWREQ, 1UL << 31);
4651 }
4652
4653 static void cherryview_disable_rc6(struct drm_device *dev)
4654 {
4655 struct drm_i915_private *dev_priv = dev->dev_private;
4656
4657 I915_WRITE(GEN6_RC_CONTROL, 0);
4658 }
4659
4660 static void valleyview_disable_rc6(struct drm_device *dev)
4661 {
4662 struct drm_i915_private *dev_priv = dev->dev_private;
4663
4664 /* we're doing forcewake before Disabling RC6,
4665 * This what the BIOS expects when going into suspend */
4666 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4667
4668 I915_WRITE(GEN6_RC_CONTROL, 0);
4669
4670 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4671 }
4672
4673 static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4674 {
4675 if (IS_VALLEYVIEW(dev)) {
4676 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4677 mode = GEN6_RC_CTL_RC6_ENABLE;
4678 else
4679 mode = 0;
4680 }
4681 if (HAS_RC6p(dev))
4682 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4683 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4684 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4685 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4686
4687 else
4688 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4689 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
4690 }
4691
4692 static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4693 {
4694 /* No RC6 before Ironlake and code is gone for ilk. */
4695 if (INTEL_INFO(dev)->gen < 6)
4696 return 0;
4697
4698 /* Respect the kernel parameter if it is set */
4699 if (enable_rc6 >= 0) {
4700 int mask;
4701
4702 if (HAS_RC6p(dev))
4703 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4704 INTEL_RC6pp_ENABLE;
4705 else
4706 mask = INTEL_RC6_ENABLE;
4707
4708 if ((enable_rc6 & mask) != enable_rc6)
4709 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4710 enable_rc6 & mask, enable_rc6, mask);
4711
4712 return enable_rc6 & mask;
4713 }
4714
4715 if (IS_IVYBRIDGE(dev))
4716 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4717
4718 return INTEL_RC6_ENABLE;
4719 }
4720
4721 int intel_enable_rc6(const struct drm_device *dev)
4722 {
4723 return i915.enable_rc6;
4724 }
4725
4726 static void gen6_init_rps_frequencies(struct drm_device *dev)
4727 {
4728 struct drm_i915_private *dev_priv = dev->dev_private;
4729 uint32_t rp_state_cap;
4730 u32 ddcc_status = 0;
4731 int ret;
4732
4733 /* All of these values are in units of 50MHz */
4734 dev_priv->rps.cur_freq = 0;
4735 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
4736 if (IS_BROXTON(dev)) {
4737 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4738 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4739 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4740 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4741 } else {
4742 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4743 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4744 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4745 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4746 }
4747
4748 /* hw_max = RP0 until we check for overclocking */
4749 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4750
4751 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4752 if (IS_HASWELL(dev) || IS_BROADWELL(dev) ||
4753 IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4754 ret = sandybridge_pcode_read(dev_priv,
4755 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4756 &ddcc_status);
4757 if (0 == ret)
4758 dev_priv->rps.efficient_freq =
4759 clamp_t(u8,
4760 ((ddcc_status >> 8) & 0xff),
4761 dev_priv->rps.min_freq,
4762 dev_priv->rps.max_freq);
4763 }
4764
4765 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4766 /* Store the frequency values in 16.66 MHZ units, which is
4767 the natural hardware unit for SKL */
4768 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4769 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4770 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4771 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4772 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4773 }
4774
4775 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4776
4777 /* Preserve min/max settings in case of re-init */
4778 if (dev_priv->rps.max_freq_softlimit == 0)
4779 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4780
4781 if (dev_priv->rps.min_freq_softlimit == 0) {
4782 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4783 dev_priv->rps.min_freq_softlimit =
4784 max_t(int, dev_priv->rps.efficient_freq,
4785 intel_freq_opcode(dev_priv, 450));
4786 else
4787 dev_priv->rps.min_freq_softlimit =
4788 dev_priv->rps.min_freq;
4789 }
4790 }
4791
4792 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
4793 static void gen9_enable_rps(struct drm_device *dev)
4794 {
4795 struct drm_i915_private *dev_priv = dev->dev_private;
4796
4797 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4798
4799 gen6_init_rps_frequencies(dev);
4800
4801 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4802 if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) {
4803 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4804 return;
4805 }
4806
4807 /* Program defaults and thresholds for RPS*/
4808 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4809 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
4810
4811 /* 1 second timeout*/
4812 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4813 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4814
4815 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4816
4817 /* Leaning on the below call to gen6_set_rps to program/setup the
4818 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4819 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4820 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4821 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4822
4823 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4824 }
4825
4826 static void gen9_enable_rc6(struct drm_device *dev)
4827 {
4828 struct drm_i915_private *dev_priv = dev->dev_private;
4829 struct intel_engine_cs *ring;
4830 uint32_t rc6_mask = 0;
4831 int unused;
4832
4833 /* 1a: Software RC state - RC0 */
4834 I915_WRITE(GEN6_RC_STATE, 0);
4835
4836 /* 1b: Get forcewake during program sequence. Although the driver
4837 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4838 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4839
4840 /* 2a: Disable RC states. */
4841 I915_WRITE(GEN6_RC_CONTROL, 0);
4842
4843 /* 2b: Program RC6 thresholds.*/
4844
4845 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
4846 if (IS_SKYLAKE(dev))
4847 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4848 else
4849 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4850 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4851 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4852 for_each_ring(ring, dev_priv, unused)
4853 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4854
4855 if (HAS_GUC_UCODE(dev))
4856 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4857
4858 I915_WRITE(GEN6_RC_SLEEP, 0);
4859
4860 /* 2c: Program Coarse Power Gating Policies. */
4861 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4862 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4863
4864 /* 3a: Enable RC6 */
4865 if (!dev_priv->rps.ctx_corrupted &&
4866 intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4867 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4868 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4869 "on" : "off");
4870 /* WaRsUseTimeoutMode */
4871 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_D0) ||
4872 (IS_BROXTON(dev) && INTEL_REVID(dev) <= BXT_REVID_A0)) {
4873 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
4874 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4875 GEN7_RC_CTL_TO_MODE |
4876 rc6_mask);
4877 } else {
4878 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4879 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4880 GEN6_RC_CTL_EI_MODE(1) |
4881 rc6_mask);
4882 }
4883
4884 /*
4885 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
4886 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
4887 */
4888 if ((IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) ||
4889 INTEL_INFO(dev)->gen == 9)
4890 I915_WRITE(GEN9_PG_ENABLE, 0);
4891 else
4892 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4893 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
4894
4895 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4896
4897 }
4898
4899 static void gen8_enable_rps(struct drm_device *dev)
4900 {
4901 struct drm_i915_private *dev_priv = dev->dev_private;
4902 struct intel_engine_cs *ring;
4903 uint32_t rc6_mask = 0;
4904 int unused;
4905
4906 /* 1a: Software RC state - RC0 */
4907 I915_WRITE(GEN6_RC_STATE, 0);
4908
4909 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4910 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4911 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4912
4913 /* 2a: Disable RC states. */
4914 I915_WRITE(GEN6_RC_CONTROL, 0);
4915
4916 /* Initialize rps frequencies */
4917 gen6_init_rps_frequencies(dev);
4918
4919 /* 2b: Program RC6 thresholds.*/
4920 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4921 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4922 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4923 for_each_ring(ring, dev_priv, unused)
4924 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4925 I915_WRITE(GEN6_RC_SLEEP, 0);
4926 if (IS_BROADWELL(dev))
4927 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4928 else
4929 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4930
4931 /* 3: Enable RC6 */
4932 if (!dev_priv->rps.ctx_corrupted &&
4933 intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4934 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4935 intel_print_rc6_info(dev, rc6_mask);
4936 if (IS_BROADWELL(dev))
4937 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4938 GEN7_RC_CTL_TO_MODE |
4939 rc6_mask);
4940 else
4941 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4942 GEN6_RC_CTL_EI_MODE(1) |
4943 rc6_mask);
4944
4945 /* 4 Program defaults and thresholds for RPS*/
4946 I915_WRITE(GEN6_RPNSWREQ,
4947 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4948 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4949 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4950 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4951 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4952
4953 /* Docs recommend 900MHz, and 300 MHz respectively */
4954 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4955 dev_priv->rps.max_freq_softlimit << 24 |
4956 dev_priv->rps.min_freq_softlimit << 16);
4957
4958 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4959 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4960 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4961 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4962
4963 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4964
4965 /* 5: Enable RPS */
4966 I915_WRITE(GEN6_RP_CONTROL,
4967 GEN6_RP_MEDIA_TURBO |
4968 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4969 GEN6_RP_MEDIA_IS_GFX |
4970 GEN6_RP_ENABLE |
4971 GEN6_RP_UP_BUSY_AVG |
4972 GEN6_RP_DOWN_IDLE_AVG);
4973
4974 /* 6: Ring frequency + overclocking (our driver does this later */
4975
4976 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4977 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4978
4979 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4980 }
4981
4982 static void gen6_enable_rps(struct drm_device *dev)
4983 {
4984 struct drm_i915_private *dev_priv = dev->dev_private;
4985 struct intel_engine_cs *ring;
4986 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4987 u32 gtfifodbg;
4988 int rc6_mode;
4989 int i, ret;
4990
4991 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4992
4993 /* Here begins a magic sequence of register writes to enable
4994 * auto-downclocking.
4995 *
4996 * Perhaps there might be some value in exposing these to
4997 * userspace...
4998 */
4999 I915_WRITE(GEN6_RC_STATE, 0);
5000
5001 /* Clear the DBG now so we don't confuse earlier errors */
5002 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5003 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5004 I915_WRITE(GTFIFODBG, gtfifodbg);
5005 }
5006
5007 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5008
5009 /* Initialize rps frequencies */
5010 gen6_init_rps_frequencies(dev);
5011
5012 /* disable the counters and set deterministic thresholds */
5013 I915_WRITE(GEN6_RC_CONTROL, 0);
5014
5015 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5016 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5017 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5018 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5019 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5020
5021 for_each_ring(ring, dev_priv, i)
5022 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5023
5024 I915_WRITE(GEN6_RC_SLEEP, 0);
5025 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
5026 if (IS_IVYBRIDGE(dev))
5027 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5028 else
5029 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
5030 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
5031 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5032
5033 /* Check if we are enabling RC6 */
5034 rc6_mode = intel_enable_rc6(dev_priv->dev);
5035 if (rc6_mode & INTEL_RC6_ENABLE)
5036 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5037
5038 /* We don't use those on Haswell */
5039 if (!IS_HASWELL(dev)) {
5040 if (rc6_mode & INTEL_RC6p_ENABLE)
5041 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
5042
5043 if (rc6_mode & INTEL_RC6pp_ENABLE)
5044 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5045 }
5046
5047 intel_print_rc6_info(dev, rc6_mask);
5048
5049 I915_WRITE(GEN6_RC_CONTROL,
5050 rc6_mask |
5051 GEN6_RC_CTL_EI_MODE(1) |
5052 GEN6_RC_CTL_HW_ENABLE);
5053
5054 /* Power down if completely idle for over 50ms */
5055 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
5056 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5057
5058 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
5059 if (ret)
5060 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
5061
5062 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5063 if (!ret && (pcu_mbox & __BIT(31))) { /* OC supported */
5064 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
5065 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
5066 (pcu_mbox & 0xff) * 50);
5067 dev_priv->rps.max_freq = pcu_mbox & 0xff;
5068 }
5069
5070 dev_priv->rps.power = HIGH_POWER; /* force a reset */
5071 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
5072
5073 rc6vids = 0;
5074 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5075 if (IS_GEN6(dev) && ret) {
5076 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5077 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5078 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5079 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5080 rc6vids &= 0xffff00;
5081 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5082 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5083 if (ret)
5084 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5085 }
5086
5087 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5088 }
5089
5090 static void __gen6_update_ring_freq(struct drm_device *dev)
5091 {
5092 struct drm_i915_private *dev_priv = dev->dev_private;
5093 int min_freq = 15;
5094 unsigned int gpu_freq;
5095 unsigned int max_ia_freq, min_ring_freq;
5096 unsigned int max_gpu_freq, min_gpu_freq;
5097 int scaling_factor = 180;
5098 #ifndef __NetBSD__
5099 struct cpufreq_policy *policy;
5100 #endif
5101
5102 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5103
5104 #ifdef __NetBSD__
5105 {
5106 extern uint64_t tsc_freq; /* x86 TSC frequency in Hz */
5107 max_ia_freq = (tsc_freq / 1000);
5108 }
5109 #else
5110 policy = cpufreq_cpu_get(0);
5111 if (policy) {
5112 max_ia_freq = policy->cpuinfo.max_freq;
5113 cpufreq_cpu_put(policy);
5114 } else {
5115 /*
5116 * Default to measured freq if none found, PCU will ensure we
5117 * don't go over
5118 */
5119 max_ia_freq = tsc_khz;
5120 }
5121 #endif
5122
5123 /* Convert from kHz to MHz */
5124 max_ia_freq /= 1000;
5125
5126 min_ring_freq = I915_READ(DCLK) & 0xf;
5127 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5128 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
5129
5130 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
5131 /* Convert GT frequency to 50 HZ units */
5132 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5133 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5134 } else {
5135 min_gpu_freq = dev_priv->rps.min_freq;
5136 max_gpu_freq = dev_priv->rps.max_freq;
5137 }
5138
5139 /*
5140 * For each potential GPU frequency, load a ring frequency we'd like
5141 * to use for memory access. We do this by specifying the IA frequency
5142 * the PCU should use as a reference to determine the ring frequency.
5143 */
5144 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5145 int diff = max_gpu_freq - gpu_freq;
5146 unsigned int ia_freq = 0, ring_freq = 0;
5147
5148 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
5149 /*
5150 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5151 * No floor required for ring frequency on SKL.
5152 */
5153 ring_freq = gpu_freq;
5154 } else if (INTEL_INFO(dev)->gen >= 8) {
5155 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5156 ring_freq = max(min_ring_freq, gpu_freq);
5157 } else if (IS_HASWELL(dev)) {
5158 ring_freq = mult_frac(gpu_freq, 5, 4);
5159 ring_freq = max(min_ring_freq, ring_freq);
5160 /* leave ia_freq as the default, chosen by cpufreq */
5161 } else {
5162 /* On older processors, there is no separate ring
5163 * clock domain, so in order to boost the bandwidth
5164 * of the ring, we need to upclock the CPU (ia_freq).
5165 *
5166 * For GPU frequencies less than 750MHz,
5167 * just use the lowest ring freq.
5168 */
5169 if (gpu_freq < min_freq)
5170 ia_freq = 800;
5171 else
5172 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5173 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5174 }
5175
5176 sandybridge_pcode_write(dev_priv,
5177 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
5178 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5179 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5180 gpu_freq);
5181 }
5182 }
5183
5184 void gen6_update_ring_freq(struct drm_device *dev)
5185 {
5186 struct drm_i915_private *dev_priv = dev->dev_private;
5187
5188 if (!HAS_CORE_RING_FREQ(dev))
5189 return;
5190
5191 mutex_lock(&dev_priv->rps.hw_lock);
5192 __gen6_update_ring_freq(dev);
5193 mutex_unlock(&dev_priv->rps.hw_lock);
5194 }
5195
5196 static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
5197 {
5198 struct drm_device *dev = dev_priv->dev;
5199 u32 val, rp0;
5200
5201 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5202
5203 switch (INTEL_INFO(dev)->eu_total) {
5204 case 8:
5205 /* (2 * 4) config */
5206 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5207 break;
5208 case 12:
5209 /* (2 * 6) config */
5210 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5211 break;
5212 case 16:
5213 /* (2 * 8) config */
5214 default:
5215 /* Setting (2 * 8) Min RP0 for any other combination */
5216 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5217 break;
5218 }
5219
5220 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5221
5222 return rp0;
5223 }
5224
5225 static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5226 {
5227 u32 val, rpe;
5228
5229 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5230 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5231
5232 return rpe;
5233 }
5234
5235 static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5236 {
5237 u32 val, rp1;
5238
5239 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5240 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5241
5242 return rp1;
5243 }
5244
5245 static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5246 {
5247 u32 val, rp1;
5248
5249 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5250
5251 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5252
5253 return rp1;
5254 }
5255
5256 static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
5257 {
5258 u32 val, rp0;
5259
5260 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5261
5262 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5263 /* Clamp to max */
5264 rp0 = min_t(u32, rp0, 0xea);
5265
5266 return rp0;
5267 }
5268
5269 static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5270 {
5271 u32 val, rpe;
5272
5273 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
5274 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
5275 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
5276 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5277
5278 return rpe;
5279 }
5280
5281 static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
5282 {
5283 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5284 }
5285
5286 /* Check that the pctx buffer wasn't move under us. */
5287 static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5288 {
5289 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5290
5291 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5292 dev_priv->vlv_pctx->stolen->start);
5293 }
5294
5295
5296 /* Check that the pcbr address is not empty. */
5297 static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5298 {
5299 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5300
5301 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5302 }
5303
5304 static void cherryview_setup_pctx(struct drm_device *dev)
5305 {
5306 struct drm_i915_private *dev_priv = dev->dev_private;
5307 unsigned long pctx_paddr, paddr;
5308 struct i915_gtt *gtt = &dev_priv->gtt;
5309 u32 pcbr;
5310 int pctx_size = 32*1024;
5311
5312 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5313
5314 pcbr = I915_READ(VLV_PCBR);
5315 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5316 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5317 paddr = (dev_priv->mm.stolen_base +
5318 (gtt->stolen_size - pctx_size));
5319
5320 pctx_paddr = (paddr & (~4095));
5321 I915_WRITE(VLV_PCBR, pctx_paddr);
5322 }
5323
5324 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5325 }
5326
5327 static void valleyview_setup_pctx(struct drm_device *dev)
5328 {
5329 struct drm_i915_private *dev_priv = dev->dev_private;
5330 struct drm_i915_gem_object *pctx;
5331 unsigned long pctx_paddr;
5332 u32 pcbr;
5333 int pctx_size = 24*1024;
5334
5335 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5336
5337 pcbr = I915_READ(VLV_PCBR);
5338 if (pcbr) {
5339 /* BIOS set it up already, grab the pre-alloc'd space */
5340 int pcbr_offset;
5341
5342 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5343 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5344 pcbr_offset,
5345 I915_GTT_OFFSET_NONE,
5346 pctx_size);
5347 goto out;
5348 }
5349
5350 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5351
5352 /*
5353 * From the Gunit register HAS:
5354 * The Gfx driver is expected to program this register and ensure
5355 * proper allocation within Gfx stolen memory. For example, this
5356 * register should be programmed such than the PCBR range does not
5357 * overlap with other ranges, such as the frame buffer, protected
5358 * memory, or any other relevant ranges.
5359 */
5360 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5361 if (!pctx) {
5362 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5363 return;
5364 }
5365
5366 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5367 I915_WRITE(VLV_PCBR, pctx_paddr);
5368
5369 out:
5370 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5371 dev_priv->vlv_pctx = pctx;
5372 }
5373
5374 static void valleyview_cleanup_pctx(struct drm_device *dev)
5375 {
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377
5378 if (WARN_ON(!dev_priv->vlv_pctx))
5379 return;
5380
5381 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5382 dev_priv->vlv_pctx = NULL;
5383 }
5384
5385 static void valleyview_init_gt_powersave(struct drm_device *dev)
5386 {
5387 struct drm_i915_private *dev_priv = dev->dev_private;
5388 u32 val;
5389
5390 valleyview_setup_pctx(dev);
5391
5392 mutex_lock(&dev_priv->rps.hw_lock);
5393
5394 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5395 switch ((val >> 6) & 3) {
5396 case 0:
5397 case 1:
5398 dev_priv->mem_freq = 800;
5399 break;
5400 case 2:
5401 dev_priv->mem_freq = 1066;
5402 break;
5403 case 3:
5404 dev_priv->mem_freq = 1333;
5405 break;
5406 }
5407 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5408
5409 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5410 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5411 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5412 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5413 dev_priv->rps.max_freq);
5414
5415 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5416 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5417 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5418 dev_priv->rps.efficient_freq);
5419
5420 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5421 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5422 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5423 dev_priv->rps.rp1_freq);
5424
5425 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5426 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5427 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5428 dev_priv->rps.min_freq);
5429
5430 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5431
5432 /* Preserve min/max settings in case of re-init */
5433 if (dev_priv->rps.max_freq_softlimit == 0)
5434 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5435
5436 if (dev_priv->rps.min_freq_softlimit == 0)
5437 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5438
5439 mutex_unlock(&dev_priv->rps.hw_lock);
5440 }
5441
5442 static void cherryview_init_gt_powersave(struct drm_device *dev)
5443 {
5444 struct drm_i915_private *dev_priv = dev->dev_private;
5445 u32 val;
5446
5447 cherryview_setup_pctx(dev);
5448
5449 mutex_lock(&dev_priv->rps.hw_lock);
5450
5451 mutex_lock(&dev_priv->sb_lock);
5452 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
5453 mutex_unlock(&dev_priv->sb_lock);
5454
5455 switch ((val >> 2) & 0x7) {
5456 case 3:
5457 dev_priv->mem_freq = 2000;
5458 break;
5459 default:
5460 dev_priv->mem_freq = 1600;
5461 break;
5462 }
5463 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5464
5465 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5466 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5467 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5468 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5469 dev_priv->rps.max_freq);
5470
5471 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5472 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5473 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5474 dev_priv->rps.efficient_freq);
5475
5476 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5477 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5478 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5479 dev_priv->rps.rp1_freq);
5480
5481 /* PUnit validated range is only [RPe, RP0] */
5482 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
5483 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5484 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5485 dev_priv->rps.min_freq);
5486
5487 WARN_ONCE((dev_priv->rps.max_freq |
5488 dev_priv->rps.efficient_freq |
5489 dev_priv->rps.rp1_freq |
5490 dev_priv->rps.min_freq) & 1,
5491 "Odd GPU freq values\n");
5492
5493 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5494
5495 /* Preserve min/max settings in case of re-init */
5496 if (dev_priv->rps.max_freq_softlimit == 0)
5497 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5498
5499 if (dev_priv->rps.min_freq_softlimit == 0)
5500 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5501
5502 mutex_unlock(&dev_priv->rps.hw_lock);
5503 }
5504
5505 static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5506 {
5507 valleyview_cleanup_pctx(dev);
5508 }
5509
5510 static void cherryview_enable_rps(struct drm_device *dev)
5511 {
5512 struct drm_i915_private *dev_priv = dev->dev_private;
5513 struct intel_engine_cs *ring;
5514 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
5515 int i;
5516
5517 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5518
5519 gtfifodbg = I915_READ(GTFIFODBG);
5520 if (gtfifodbg) {
5521 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5522 gtfifodbg);
5523 I915_WRITE(GTFIFODBG, gtfifodbg);
5524 }
5525
5526 cherryview_check_pctx(dev_priv);
5527
5528 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5529 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5530 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5531
5532 /* Disable RC states. */
5533 I915_WRITE(GEN6_RC_CONTROL, 0);
5534
5535 /* 2a: Program RC6 thresholds.*/
5536 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5537 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5538 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5539
5540 for_each_ring(ring, dev_priv, i)
5541 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5542 I915_WRITE(GEN6_RC_SLEEP, 0);
5543
5544 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5545 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
5546
5547 /* allows RC6 residency counter to work */
5548 I915_WRITE(VLV_COUNTER_CONTROL,
5549 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5550 VLV_MEDIA_RC6_COUNT_EN |
5551 VLV_RENDER_RC6_COUNT_EN));
5552
5553 /* For now we assume BIOS is allocating and populating the PCBR */
5554 pcbr = I915_READ(VLV_PCBR);
5555
5556 /* 3: Enable RC6 */
5557 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5558 (pcbr >> VLV_PCBR_ADDR_SHIFT))
5559 rc6_mode = GEN7_RC_CTL_TO_MODE;
5560
5561 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5562
5563 /* 4 Program defaults and thresholds for RPS*/
5564 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5565 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5566 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5567 I915_WRITE(GEN6_RP_UP_EI, 66000);
5568 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5569
5570 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5571
5572 /* 5: Enable RPS */
5573 I915_WRITE(GEN6_RP_CONTROL,
5574 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5575 GEN6_RP_MEDIA_IS_GFX |
5576 GEN6_RP_ENABLE |
5577 GEN6_RP_UP_BUSY_AVG |
5578 GEN6_RP_DOWN_IDLE_AVG);
5579
5580 /* Setting Fixed Bias */
5581 val = VLV_OVERRIDE_EN |
5582 VLV_SOC_TDP_EN |
5583 CHV_BIAS_CPU_50_SOC_50;
5584 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5585
5586 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5587
5588 /* RPS code assumes GPLL is used */
5589 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5590
5591 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5592 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5593
5594 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5595 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5596 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5597 dev_priv->rps.cur_freq);
5598
5599 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5600 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5601 dev_priv->rps.efficient_freq);
5602
5603 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5604
5605 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5606 }
5607
5608 static void valleyview_enable_rps(struct drm_device *dev)
5609 {
5610 struct drm_i915_private *dev_priv = dev->dev_private;
5611 struct intel_engine_cs *ring;
5612 u32 gtfifodbg, val, rc6_mode = 0;
5613 int i;
5614
5615 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5616
5617 valleyview_check_pctx(dev_priv);
5618
5619 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5620 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5621 gtfifodbg);
5622 I915_WRITE(GTFIFODBG, gtfifodbg);
5623 }
5624
5625 /* If VLV, Forcewake all wells, else re-direct to regular path */
5626 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5627
5628 /* Disable RC states. */
5629 I915_WRITE(GEN6_RC_CONTROL, 0);
5630
5631 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5632 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5633 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5634 I915_WRITE(GEN6_RP_UP_EI, 66000);
5635 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5636
5637 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5638
5639 I915_WRITE(GEN6_RP_CONTROL,
5640 GEN6_RP_MEDIA_TURBO |
5641 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5642 GEN6_RP_MEDIA_IS_GFX |
5643 GEN6_RP_ENABLE |
5644 GEN6_RP_UP_BUSY_AVG |
5645 GEN6_RP_DOWN_IDLE_CONT);
5646
5647 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5648 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5649 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5650
5651 for_each_ring(ring, dev_priv, i)
5652 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5653
5654 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
5655
5656 /* allows RC6 residency counter to work */
5657 I915_WRITE(VLV_COUNTER_CONTROL,
5658 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5659 VLV_RENDER_RC0_COUNT_EN |
5660 VLV_MEDIA_RC6_COUNT_EN |
5661 VLV_RENDER_RC6_COUNT_EN));
5662
5663 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
5664 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
5665
5666 intel_print_rc6_info(dev, rc6_mode);
5667
5668 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5669
5670 /* Setting Fixed Bias */
5671 val = VLV_OVERRIDE_EN |
5672 VLV_SOC_TDP_EN |
5673 VLV_BIAS_CPU_125_SOC_875;
5674 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5675
5676 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5677
5678 /* RPS code assumes GPLL is used */
5679 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5680
5681 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5682 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5683
5684 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5685 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5686 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5687 dev_priv->rps.cur_freq);
5688
5689 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5690 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5691 dev_priv->rps.efficient_freq);
5692
5693 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5694
5695 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5696 }
5697
5698 static unsigned long intel_pxfreq(u32 vidfreq)
5699 {
5700 unsigned long freq;
5701 int div = (vidfreq & 0x3f0000) >> 16;
5702 int post = (vidfreq & 0x3000) >> 12;
5703 int pre = (vidfreq & 0x7);
5704
5705 if (!pre)
5706 return 0;
5707
5708 freq = ((div * 133333) / ((1<<post) * pre));
5709
5710 return freq;
5711 }
5712
5713 static const struct cparams {
5714 u16 i;
5715 u16 t;
5716 u16 m;
5717 u16 c;
5718 } cparams[] = {
5719 { 1, 1333, 301, 28664 },
5720 { 1, 1066, 294, 24460 },
5721 { 1, 800, 294, 25192 },
5722 { 0, 1333, 276, 27605 },
5723 { 0, 1066, 276, 27605 },
5724 { 0, 800, 231, 23784 },
5725 };
5726
5727 static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5728 {
5729 u64 total_count, diff, ret;
5730 u32 count1, count2, count3, m = 0, c = 0;
5731 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5732 int i;
5733
5734 assert_spin_locked(&mchdev_lock);
5735
5736 diff1 = now - dev_priv->ips.last_time1;
5737
5738 /* Prevent division-by-zero if we are asking too fast.
5739 * Also, we don't get interesting results if we are polling
5740 * faster than once in 10ms, so just return the saved value
5741 * in such cases.
5742 */
5743 if (diff1 <= 10)
5744 return dev_priv->ips.chipset_power;
5745
5746 count1 = I915_READ(DMIEC);
5747 count2 = I915_READ(DDREC);
5748 count3 = I915_READ(CSIEC);
5749
5750 total_count = count1 + count2 + count3;
5751
5752 /* FIXME: handle per-counter overflow */
5753 if (total_count < dev_priv->ips.last_count1) {
5754 diff = ~0UL - dev_priv->ips.last_count1;
5755 diff += total_count;
5756 } else {
5757 diff = total_count - dev_priv->ips.last_count1;
5758 }
5759
5760 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5761 if (cparams[i].i == dev_priv->ips.c_m &&
5762 cparams[i].t == dev_priv->ips.r_t) {
5763 m = cparams[i].m;
5764 c = cparams[i].c;
5765 break;
5766 }
5767 }
5768
5769 diff = div_u64(diff, diff1);
5770 ret = ((m * diff) + c);
5771 ret = div_u64(ret, 10);
5772
5773 dev_priv->ips.last_count1 = total_count;
5774 dev_priv->ips.last_time1 = now;
5775
5776 dev_priv->ips.chipset_power = ret;
5777
5778 return ret;
5779 }
5780
5781 unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5782 {
5783 struct drm_device *dev = dev_priv->dev;
5784 unsigned long val;
5785
5786 if (INTEL_INFO(dev)->gen != 5)
5787 return 0;
5788
5789 spin_lock_irq(&mchdev_lock);
5790
5791 val = __i915_chipset_val(dev_priv);
5792
5793 spin_unlock_irq(&mchdev_lock);
5794
5795 return val;
5796 }
5797
5798 unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5799 {
5800 unsigned long m, x, b;
5801 u32 tsfs;
5802
5803 tsfs = I915_READ(TSFS);
5804
5805 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5806 x = I915_READ8(TR1);
5807
5808 b = tsfs & TSFS_INTR_MASK;
5809
5810 return ((m * x) / 127) - b;
5811 }
5812
5813 static int _pxvid_to_vd(u8 pxvid)
5814 {
5815 if (pxvid == 0)
5816 return 0;
5817
5818 if (pxvid >= 8 && pxvid < 31)
5819 pxvid = 31;
5820
5821 return (pxvid + 2) * 125;
5822 }
5823
5824 static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5825 {
5826 struct drm_device *dev = dev_priv->dev;
5827 const int vd = _pxvid_to_vd(pxvid);
5828 const int vm = vd - 1125;
5829
5830 if (INTEL_INFO(dev)->is_mobile)
5831 return vm > 0 ? vm : 0;
5832
5833 return vd;
5834 }
5835
5836 static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5837 {
5838 u64 now, diff, diffms;
5839 u32 count;
5840
5841 assert_spin_locked(&mchdev_lock);
5842
5843 now = ktime_get_raw_ns();
5844 diffms = now - dev_priv->ips.last_time2;
5845 do_div(diffms, NSEC_PER_MSEC);
5846
5847 /* Don't divide by 0 */
5848 if (!diffms)
5849 return;
5850
5851 count = I915_READ(GFXEC);
5852
5853 if (count < dev_priv->ips.last_count2) {
5854 diff = ~0UL - dev_priv->ips.last_count2;
5855 diff += count;
5856 } else {
5857 diff = count - dev_priv->ips.last_count2;
5858 }
5859
5860 dev_priv->ips.last_count2 = count;
5861 dev_priv->ips.last_time2 = now;
5862
5863 /* More magic constants... */
5864 diff = diff * 1181;
5865 diff = div_u64(diff, diffms * 10);
5866 dev_priv->ips.gfx_power = diff;
5867 }
5868
5869 void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5870 {
5871 struct drm_device *dev = dev_priv->dev;
5872
5873 if (INTEL_INFO(dev)->gen != 5)
5874 return;
5875
5876 spin_lock_irq(&mchdev_lock);
5877
5878 __i915_update_gfx_val(dev_priv);
5879
5880 spin_unlock_irq(&mchdev_lock);
5881 }
5882
5883 static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5884 {
5885 unsigned long t, corr, state1, corr2, state2;
5886 u32 pxvid, ext_v;
5887
5888 assert_spin_locked(&mchdev_lock);
5889
5890 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
5891 pxvid = (pxvid >> 24) & 0x7f;
5892 ext_v = pvid_to_extvid(dev_priv, pxvid);
5893
5894 state1 = ext_v;
5895
5896 t = i915_mch_val(dev_priv);
5897
5898 /* Revel in the empirically derived constants */
5899
5900 /* Correction factor in 1/100000 units */
5901 if (t > 80)
5902 corr = ((t * 2349) + 135940);
5903 else if (t >= 50)
5904 corr = ((t * 964) + 29317);
5905 else /* < 50 */
5906 corr = ((t * 301) + 1004);
5907
5908 corr = corr * ((150142 * state1) / 10000 - 78642);
5909 corr /= 100000;
5910 corr2 = (corr * dev_priv->ips.corr);
5911
5912 state2 = (corr2 * state1) / 10000;
5913 state2 /= 100; /* convert to mW */
5914
5915 __i915_update_gfx_val(dev_priv);
5916
5917 return dev_priv->ips.gfx_power + state2;
5918 }
5919
5920 unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5921 {
5922 struct drm_device *dev = dev_priv->dev;
5923 unsigned long val;
5924
5925 if (INTEL_INFO(dev)->gen != 5)
5926 return 0;
5927
5928 spin_lock_irq(&mchdev_lock);
5929
5930 val = __i915_gfx_val(dev_priv);
5931
5932 spin_unlock_irq(&mchdev_lock);
5933
5934 return val;
5935 }
5936
5937 /**
5938 * i915_read_mch_val - return value for IPS use
5939 *
5940 * Calculate and return a value for the IPS driver to use when deciding whether
5941 * we have thermal and power headroom to increase CPU or GPU power budget.
5942 */
5943 unsigned long i915_read_mch_val(void)
5944 {
5945 struct drm_i915_private *dev_priv;
5946 unsigned long chipset_val, graphics_val, ret = 0;
5947
5948 spin_lock_irq(&mchdev_lock);
5949 if (!i915_mch_dev)
5950 goto out_unlock;
5951 dev_priv = i915_mch_dev;
5952
5953 chipset_val = __i915_chipset_val(dev_priv);
5954 graphics_val = __i915_gfx_val(dev_priv);
5955
5956 ret = chipset_val + graphics_val;
5957
5958 out_unlock:
5959 spin_unlock_irq(&mchdev_lock);
5960
5961 return ret;
5962 }
5963 EXPORT_SYMBOL_GPL(i915_read_mch_val);
5964
5965 /**
5966 * i915_gpu_raise - raise GPU frequency limit
5967 *
5968 * Raise the limit; IPS indicates we have thermal headroom.
5969 */
5970 bool i915_gpu_raise(void)
5971 {
5972 struct drm_i915_private *dev_priv;
5973 bool ret = true;
5974
5975 spin_lock_irq(&mchdev_lock);
5976 if (!i915_mch_dev) {
5977 ret = false;
5978 goto out_unlock;
5979 }
5980 dev_priv = i915_mch_dev;
5981
5982 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5983 dev_priv->ips.max_delay--;
5984
5985 out_unlock:
5986 spin_unlock_irq(&mchdev_lock);
5987
5988 return ret;
5989 }
5990 EXPORT_SYMBOL_GPL(i915_gpu_raise);
5991
5992 /**
5993 * i915_gpu_lower - lower GPU frequency limit
5994 *
5995 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5996 * frequency maximum.
5997 */
5998 bool i915_gpu_lower(void)
5999 {
6000 struct drm_i915_private *dev_priv;
6001 bool ret = true;
6002
6003 spin_lock_irq(&mchdev_lock);
6004 if (!i915_mch_dev) {
6005 ret = false;
6006 goto out_unlock;
6007 }
6008 dev_priv = i915_mch_dev;
6009
6010 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6011 dev_priv->ips.max_delay++;
6012
6013 out_unlock:
6014 spin_unlock_irq(&mchdev_lock);
6015
6016 return ret;
6017 }
6018 EXPORT_SYMBOL_GPL(i915_gpu_lower);
6019
6020 /**
6021 * i915_gpu_busy - indicate GPU business to IPS
6022 *
6023 * Tell the IPS driver whether or not the GPU is busy.
6024 */
6025 bool i915_gpu_busy(void)
6026 {
6027 struct drm_i915_private *dev_priv;
6028 struct intel_engine_cs *ring;
6029 bool ret = false;
6030 int i;
6031
6032 spin_lock_irq(&mchdev_lock);
6033 if (!i915_mch_dev)
6034 goto out_unlock;
6035 dev_priv = i915_mch_dev;
6036
6037 for_each_ring(ring, dev_priv, i)
6038 ret |= !list_empty(&ring->request_list);
6039
6040 out_unlock:
6041 spin_unlock_irq(&mchdev_lock);
6042
6043 return ret;
6044 }
6045 EXPORT_SYMBOL_GPL(i915_gpu_busy);
6046
6047 /**
6048 * i915_gpu_turbo_disable - disable graphics turbo
6049 *
6050 * Disable graphics turbo by resetting the max frequency and setting the
6051 * current frequency to the default.
6052 */
6053 bool i915_gpu_turbo_disable(void)
6054 {
6055 struct drm_i915_private *dev_priv;
6056 bool ret = true;
6057
6058 spin_lock_irq(&mchdev_lock);
6059 if (!i915_mch_dev) {
6060 ret = false;
6061 goto out_unlock;
6062 }
6063 dev_priv = i915_mch_dev;
6064
6065 dev_priv->ips.max_delay = dev_priv->ips.fstart;
6066
6067 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
6068 ret = false;
6069
6070 out_unlock:
6071 spin_unlock_irq(&mchdev_lock);
6072
6073 return ret;
6074 }
6075 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6076
6077 /**
6078 * Tells the intel_ips driver that the i915 driver is now loaded, if
6079 * IPS got loaded first.
6080 *
6081 * This awkward dance is so that neither module has to depend on the
6082 * other in order for IPS to do the appropriate communication of
6083 * GPU turbo limits to i915.
6084 */
6085 static void
6086 ips_ping_for_i915_load(void)
6087 {
6088 #ifndef __NetBSD__ /* XXX IPS GPU turbo limits what? */
6089 void (*link)(void);
6090
6091 link = symbol_get(ips_link_to_i915_driver);
6092 if (link) {
6093 link();
6094 symbol_put(ips_link_to_i915_driver);
6095 }
6096 #endif
6097 }
6098
6099 void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6100 {
6101 /* We only register the i915 ips part with intel-ips once everything is
6102 * set up, to avoid intel-ips sneaking in and reading bogus values. */
6103 spin_lock_irq(&mchdev_lock);
6104 i915_mch_dev = dev_priv;
6105 spin_unlock_irq(&mchdev_lock);
6106
6107 ips_ping_for_i915_load();
6108 }
6109
6110 void intel_gpu_ips_teardown(void)
6111 {
6112 spin_lock_irq(&mchdev_lock);
6113 i915_mch_dev = NULL;
6114 spin_unlock_irq(&mchdev_lock);
6115 }
6116
6117 static void intel_init_emon(struct drm_device *dev)
6118 {
6119 struct drm_i915_private *dev_priv = dev->dev_private;
6120 u32 lcfuse;
6121 u8 pxw[16];
6122 int i;
6123
6124 /* Disable to program */
6125 I915_WRITE(ECR, 0);
6126 POSTING_READ(ECR);
6127
6128 /* Program energy weights for various events */
6129 I915_WRITE(SDEW, 0x15040d00);
6130 I915_WRITE(CSIEW0, 0x007f0000);
6131 I915_WRITE(CSIEW1, 0x1e220004);
6132 I915_WRITE(CSIEW2, 0x04000004);
6133
6134 for (i = 0; i < 5; i++)
6135 I915_WRITE(PEW(i), 0);
6136 for (i = 0; i < 3; i++)
6137 I915_WRITE(DEW(i), 0);
6138
6139 /* Program P-state weights to account for frequency power adjustment */
6140 for (i = 0; i < 16; i++) {
6141 u32 pxvidfreq = I915_READ(PXVFREQ(i));
6142 unsigned long freq = intel_pxfreq(pxvidfreq);
6143 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6144 PXVFREQ_PX_SHIFT;
6145 unsigned long val;
6146
6147 val = vid * vid;
6148 val *= (freq / 1000);
6149 val *= 255;
6150 val /= (127*127*900);
6151 if (val > 0xff)
6152 DRM_ERROR("bad pxval: %ld\n", val);
6153 pxw[i] = val;
6154 }
6155 /* Render standby states get 0 weight */
6156 pxw[14] = 0;
6157 pxw[15] = 0;
6158
6159 for (i = 0; i < 4; i++) {
6160 u32 val = ((u32)pxw[i*4] << 24) | ((u32)pxw[(i*4)+1] << 16) |
6161 ((u32)pxw[(i*4)+2] << 8) | ((u32)pxw[(i*4)+3]);
6162 I915_WRITE(PXW(i), val);
6163 }
6164
6165 /* Adjust magic regs to magic values (more experimental results) */
6166 I915_WRITE(OGW0, 0);
6167 I915_WRITE(OGW1, 0);
6168 I915_WRITE(EG0, 0x00007f00);
6169 I915_WRITE(EG1, 0x0000000e);
6170 I915_WRITE(EG2, 0x000e0000);
6171 I915_WRITE(EG3, 0x68000300);
6172 I915_WRITE(EG4, 0x42000000);
6173 I915_WRITE(EG5, 0x00140031);
6174 I915_WRITE(EG6, 0);
6175 I915_WRITE(EG7, 0);
6176
6177 for (i = 0; i < 8; i++)
6178 I915_WRITE(PXWL(i), 0);
6179
6180 /* Enable PMON + select events */
6181 I915_WRITE(ECR, 0x80000019);
6182
6183 lcfuse = I915_READ(LCFUSE02);
6184
6185 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
6186 }
6187
6188 static bool i915_rc6_ctx_corrupted(struct drm_i915_private *dev_priv)
6189 {
6190 return !I915_READ(GEN8_RC6_CTX_INFO);
6191 }
6192
6193 static void i915_rc6_ctx_wa_init(struct drm_i915_private *i915)
6194 {
6195 if (!NEEDS_RC6_CTX_CORRUPTION_WA(i915))
6196 return;
6197
6198 if (i915_rc6_ctx_corrupted(i915)) {
6199 DRM_INFO("RC6 context corrupted, disabling runtime power management\n");
6200 i915->rps.ctx_corrupted = true;
6201 intel_runtime_pm_get(i915);
6202 }
6203 }
6204
6205 static void i915_rc6_ctx_wa_cleanup(struct drm_i915_private *i915)
6206 {
6207 if (i915->rps.ctx_corrupted) {
6208 intel_runtime_pm_put(i915);
6209 i915->rps.ctx_corrupted = false;
6210 }
6211 }
6212
6213 /**
6214 * i915_rc6_ctx_wa_suspend - system suspend sequence for the RC6 CTX WA
6215 * @i915: i915 device
6216 *
6217 * Perform any steps needed to clean up the RC6 CTX WA before system suspend.
6218 */
6219 void i915_rc6_ctx_wa_suspend(struct drm_i915_private *i915)
6220 {
6221 if (i915->rps.ctx_corrupted)
6222 intel_runtime_pm_put(i915);
6223 }
6224
6225 /**
6226 * i915_rc6_ctx_wa_resume - system resume sequence for the RC6 CTX WA
6227 * @i915: i915 device
6228 *
6229 * Perform any steps needed to re-init the RC6 CTX WA after system resume.
6230 */
6231 void i915_rc6_ctx_wa_resume(struct drm_i915_private *i915)
6232 {
6233 if (!i915->rps.ctx_corrupted)
6234 return;
6235
6236 if (i915_rc6_ctx_corrupted(i915)) {
6237 intel_runtime_pm_get(i915);
6238 return;
6239 }
6240
6241 DRM_INFO("RC6 context restored, re-enabling runtime power management\n");
6242 i915->rps.ctx_corrupted = false;
6243 }
6244
6245 static void intel_disable_rc6(struct drm_device *dev);
6246
6247 /**
6248 * i915_rc6_ctx_wa_check - check for a new RC6 CTX corruption
6249 * @i915: i915 device
6250 *
6251 * Check if an RC6 CTX corruption has happened since the last check and if so
6252 * disable RC6 and runtime power management.
6253 *
6254 * Return false if no context corruption has happened since the last call of
6255 * this function, true otherwise.
6256 */
6257 bool i915_rc6_ctx_wa_check(struct drm_i915_private *i915)
6258 {
6259 if (!NEEDS_RC6_CTX_CORRUPTION_WA(i915))
6260 return false;
6261
6262 if (i915->rps.ctx_corrupted)
6263 return false;
6264
6265 if (!i915_rc6_ctx_corrupted(i915))
6266 return false;
6267
6268 DRM_NOTE("RC6 context corruption, disabling runtime power management\n");
6269
6270 intel_disable_rc6(i915->dev);
6271 i915->rps.ctx_corrupted = true;
6272 intel_runtime_pm_get_noresume(i915);
6273
6274 return true;
6275 }
6276
6277 void intel_init_gt_powersave(struct drm_device *dev)
6278 {
6279 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6280
6281 i915_rc6_ctx_wa_init(to_i915(dev));
6282
6283 if (IS_CHERRYVIEW(dev))
6284 cherryview_init_gt_powersave(dev);
6285 else if (IS_VALLEYVIEW(dev))
6286 valleyview_init_gt_powersave(dev);
6287 }
6288
6289 void intel_cleanup_gt_powersave(struct drm_device *dev)
6290 {
6291 if (IS_CHERRYVIEW(dev))
6292 return;
6293 else if (IS_VALLEYVIEW(dev))
6294 valleyview_cleanup_gt_powersave(dev);
6295
6296 i915_rc6_ctx_wa_cleanup(to_i915(dev));
6297 }
6298
6299 static void gen6_suspend_rps(struct drm_device *dev)
6300 {
6301 struct drm_i915_private *dev_priv = dev->dev_private;
6302
6303 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6304
6305 gen6_disable_rps_interrupts(dev);
6306 }
6307
6308 /**
6309 * intel_suspend_gt_powersave - suspend PM work and helper threads
6310 * @dev: drm device
6311 *
6312 * We don't want to disable RC6 or other features here, we just want
6313 * to make sure any work we've queued has finished and won't bother
6314 * us while we're suspended.
6315 */
6316 void intel_suspend_gt_powersave(struct drm_device *dev)
6317 {
6318 struct drm_i915_private *dev_priv = dev->dev_private;
6319
6320 if (INTEL_INFO(dev)->gen < 6)
6321 return;
6322
6323 gen6_suspend_rps(dev);
6324
6325 /* Force GPU to min freq during suspend */
6326 gen6_rps_idle(dev_priv);
6327 }
6328
6329 static void __intel_disable_rc6(struct drm_device *dev)
6330 {
6331 if (INTEL_INFO(dev)->gen >= 9)
6332 gen9_disable_rc6(dev);
6333 else if (IS_CHERRYVIEW(dev))
6334 cherryview_disable_rc6(dev);
6335 else if (IS_VALLEYVIEW(dev))
6336 valleyview_disable_rc6(dev);
6337 else
6338 gen6_disable_rc6(dev);
6339 }
6340
6341 static void intel_disable_rc6(struct drm_device *dev)
6342 {
6343 struct drm_i915_private *dev_priv = to_i915(dev);
6344
6345 mutex_lock(&dev_priv->rps.hw_lock);
6346 __intel_disable_rc6(dev);
6347 mutex_unlock(&dev_priv->rps.hw_lock);
6348 }
6349
6350 static void intel_disable_rps(struct drm_device *dev)
6351 {
6352 if (IS_CHERRYVIEW(dev) || IS_VALLEYVIEW(dev))
6353 return;
6354
6355 if (INTEL_INFO(dev)->gen >= 9)
6356 gen9_disable_rps(dev);
6357 else
6358 gen6_disable_rps(dev);
6359 }
6360
6361 void intel_disable_gt_powersave(struct drm_device *dev)
6362 {
6363 struct drm_i915_private *dev_priv = dev->dev_private;
6364
6365 if (IS_IRONLAKE_M(dev)) {
6366 ironlake_disable_drps(dev);
6367 } else if (INTEL_INFO(dev)->gen >= 6) {
6368 intel_suspend_gt_powersave(dev);
6369
6370 mutex_lock(&dev_priv->rps.hw_lock);
6371
6372 __intel_disable_rc6(dev);
6373 intel_disable_rps(dev);
6374
6375 dev_priv->rps.enabled = false;
6376
6377 mutex_unlock(&dev_priv->rps.hw_lock);
6378 }
6379 }
6380
6381 static void intel_gen6_powersave_work(struct work_struct *work)
6382 {
6383 struct drm_i915_private *dev_priv =
6384 container_of(work, struct drm_i915_private,
6385 rps.delayed_resume_work.work);
6386 struct drm_device *dev = dev_priv->dev;
6387
6388 mutex_lock(&dev_priv->rps.hw_lock);
6389
6390 gen6_reset_rps_interrupts(dev);
6391
6392 if (IS_CHERRYVIEW(dev)) {
6393 cherryview_enable_rps(dev);
6394 } else if (IS_VALLEYVIEW(dev)) {
6395 valleyview_enable_rps(dev);
6396 } else if (INTEL_INFO(dev)->gen >= 9) {
6397 gen9_enable_rc6(dev);
6398 gen9_enable_rps(dev);
6399 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
6400 __gen6_update_ring_freq(dev);
6401 } else if (IS_BROADWELL(dev)) {
6402 gen8_enable_rps(dev);
6403 __gen6_update_ring_freq(dev);
6404 } else {
6405 gen6_enable_rps(dev);
6406 __gen6_update_ring_freq(dev);
6407 }
6408
6409 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6410 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6411
6412 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6413 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6414
6415 dev_priv->rps.enabled = true;
6416
6417 gen6_enable_rps_interrupts(dev);
6418
6419 mutex_unlock(&dev_priv->rps.hw_lock);
6420
6421 intel_runtime_pm_put(dev_priv);
6422 }
6423
6424 void intel_enable_gt_powersave(struct drm_device *dev)
6425 {
6426 struct drm_i915_private *dev_priv = dev->dev_private;
6427
6428 /* Powersaving is controlled by the host when inside a VM */
6429 if (intel_vgpu_active(dev))
6430 return;
6431
6432 if (IS_IRONLAKE_M(dev)) {
6433 mutex_lock(&dev->struct_mutex);
6434 ironlake_enable_drps(dev);
6435 intel_init_emon(dev);
6436 mutex_unlock(&dev->struct_mutex);
6437 } else if (INTEL_INFO(dev)->gen >= 6) {
6438 /*
6439 * PCU communication is slow and this doesn't need to be
6440 * done at any specific time, so do this out of our fast path
6441 * to make resume and init faster.
6442 *
6443 * We depend on the HW RC6 power context save/restore
6444 * mechanism when entering D3 through runtime PM suspend. So
6445 * disable RPM until RPS/RC6 is properly setup. We can only
6446 * get here via the driver load/system resume/runtime resume
6447 * paths, so the _noresume version is enough (and in case of
6448 * runtime resume it's necessary).
6449 */
6450 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6451 round_jiffies_up_relative(HZ)))
6452 intel_runtime_pm_get_noresume(dev_priv);
6453 }
6454 }
6455
6456 void intel_reset_gt_powersave(struct drm_device *dev)
6457 {
6458 struct drm_i915_private *dev_priv = dev->dev_private;
6459
6460 if (INTEL_INFO(dev)->gen < 6)
6461 return;
6462
6463 gen6_suspend_rps(dev);
6464 dev_priv->rps.enabled = false;
6465 }
6466
6467 static void ibx_init_clock_gating(struct drm_device *dev)
6468 {
6469 struct drm_i915_private *dev_priv = dev->dev_private;
6470
6471 /*
6472 * On Ibex Peak and Cougar Point, we need to disable clock
6473 * gating for the panel power sequencer or it will fail to
6474 * start up when no ports are active.
6475 */
6476 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6477 }
6478
6479 static void g4x_disable_trickle_feed(struct drm_device *dev)
6480 {
6481 struct drm_i915_private *dev_priv = dev->dev_private;
6482 enum pipe pipe;
6483
6484 for_each_pipe(dev_priv, pipe) {
6485 I915_WRITE(DSPCNTR(pipe),
6486 I915_READ(DSPCNTR(pipe)) |
6487 DISPPLANE_TRICKLE_FEED_DISABLE);
6488
6489 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6490 POSTING_READ(DSPSURF(pipe));
6491 }
6492 }
6493
6494 static void ilk_init_lp_watermarks(struct drm_device *dev)
6495 {
6496 struct drm_i915_private *dev_priv = dev->dev_private;
6497
6498 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6499 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6500 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6501
6502 /*
6503 * Don't touch WM1S_LP_EN here.
6504 * Doing so could cause underruns.
6505 */
6506 }
6507
6508 static void ironlake_init_clock_gating(struct drm_device *dev)
6509 {
6510 struct drm_i915_private *dev_priv = dev->dev_private;
6511 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6512
6513 /*
6514 * Required for FBC
6515 * WaFbcDisableDpfcClockGating:ilk
6516 */
6517 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6518 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6519 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6520
6521 I915_WRITE(PCH_3DCGDIS0,
6522 MARIUNIT_CLOCK_GATE_DISABLE |
6523 SVSMUNIT_CLOCK_GATE_DISABLE);
6524 I915_WRITE(PCH_3DCGDIS1,
6525 VFMUNIT_CLOCK_GATE_DISABLE);
6526
6527 /*
6528 * According to the spec the following bits should be set in
6529 * order to enable memory self-refresh
6530 * The bit 22/21 of 0x42004
6531 * The bit 5 of 0x42020
6532 * The bit 15 of 0x45000
6533 */
6534 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6535 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6536 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6537 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6538 I915_WRITE(DISP_ARB_CTL,
6539 (I915_READ(DISP_ARB_CTL) |
6540 DISP_FBC_WM_DIS));
6541
6542 ilk_init_lp_watermarks(dev);
6543
6544 /*
6545 * Based on the document from hardware guys the following bits
6546 * should be set unconditionally in order to enable FBC.
6547 * The bit 22 of 0x42000
6548 * The bit 22 of 0x42004
6549 * The bit 7,8,9 of 0x42020.
6550 */
6551 if (IS_IRONLAKE_M(dev)) {
6552 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
6553 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6554 I915_READ(ILK_DISPLAY_CHICKEN1) |
6555 ILK_FBCQ_DIS);
6556 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6557 I915_READ(ILK_DISPLAY_CHICKEN2) |
6558 ILK_DPARB_GATE);
6559 }
6560
6561 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6562
6563 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6564 I915_READ(ILK_DISPLAY_CHICKEN2) |
6565 ILK_ELPIN_409_SELECT);
6566 I915_WRITE(_3D_CHICKEN2,
6567 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6568 _3D_CHICKEN2_WM_READ_PIPELINED);
6569
6570 /* WaDisableRenderCachePipelinedFlush:ilk */
6571 I915_WRITE(CACHE_MODE_0,
6572 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6573
6574 /* WaDisable_RenderCache_OperationalFlush:ilk */
6575 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6576
6577 g4x_disable_trickle_feed(dev);
6578
6579 ibx_init_clock_gating(dev);
6580 }
6581
6582 static void cpt_init_clock_gating(struct drm_device *dev)
6583 {
6584 struct drm_i915_private *dev_priv = dev->dev_private;
6585 int pipe;
6586 uint32_t val;
6587
6588 /*
6589 * On Ibex Peak and Cougar Point, we need to disable clock
6590 * gating for the panel power sequencer or it will fail to
6591 * start up when no ports are active.
6592 */
6593 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6594 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6595 PCH_CPUNIT_CLOCK_GATE_DISABLE);
6596 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6597 DPLS_EDP_PPS_FIX_DIS);
6598 /* The below fixes the weird display corruption, a few pixels shifted
6599 * downward, on (only) LVDS of some HP laptops with IVY.
6600 */
6601 for_each_pipe(dev_priv, pipe) {
6602 val = I915_READ(TRANS_CHICKEN2(pipe));
6603 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6604 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6605 if (dev_priv->vbt.fdi_rx_polarity_inverted)
6606 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6607 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6608 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6609 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
6610 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6611 }
6612 /* WADP0ClockGatingDisable */
6613 for_each_pipe(dev_priv, pipe) {
6614 I915_WRITE(TRANS_CHICKEN1(pipe),
6615 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6616 }
6617 }
6618
6619 static void gen6_check_mch_setup(struct drm_device *dev)
6620 {
6621 struct drm_i915_private *dev_priv = dev->dev_private;
6622 uint32_t tmp;
6623
6624 tmp = I915_READ(MCH_SSKPD);
6625 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6626 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6627 tmp);
6628 }
6629
6630 static void gen6_init_clock_gating(struct drm_device *dev)
6631 {
6632 struct drm_i915_private *dev_priv = dev->dev_private;
6633 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6634
6635 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6636
6637 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6638 I915_READ(ILK_DISPLAY_CHICKEN2) |
6639 ILK_ELPIN_409_SELECT);
6640
6641 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
6642 I915_WRITE(_3D_CHICKEN,
6643 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6644
6645 /* WaDisable_RenderCache_OperationalFlush:snb */
6646 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6647
6648 /*
6649 * BSpec recoomends 8x4 when MSAA is used,
6650 * however in practice 16x4 seems fastest.
6651 *
6652 * Note that PS/WM thread counts depend on the WIZ hashing
6653 * disable bit, which we don't touch here, but it's good
6654 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6655 */
6656 I915_WRITE(GEN6_GT_MODE,
6657 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6658
6659 ilk_init_lp_watermarks(dev);
6660
6661 I915_WRITE(CACHE_MODE_0,
6662 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6663
6664 I915_WRITE(GEN6_UCGCTL1,
6665 I915_READ(GEN6_UCGCTL1) |
6666 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6667 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6668
6669 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6670 * gating disable must be set. Failure to set it results in
6671 * flickering pixels due to Z write ordering failures after
6672 * some amount of runtime in the Mesa "fire" demo, and Unigine
6673 * Sanctuary and Tropics, and apparently anything else with
6674 * alpha test or pixel discard.
6675 *
6676 * According to the spec, bit 11 (RCCUNIT) must also be set,
6677 * but we didn't debug actual testcases to find it out.
6678 *
6679 * WaDisableRCCUnitClockGating:snb
6680 * WaDisableRCPBUnitClockGating:snb
6681 */
6682 I915_WRITE(GEN6_UCGCTL2,
6683 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6684 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6685
6686 /* WaStripsFansDisableFastClipPerformanceFix:snb */
6687 I915_WRITE(_3D_CHICKEN3,
6688 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6689
6690 /*
6691 * Bspec says:
6692 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6693 * 3DSTATE_SF number of SF output attributes is more than 16."
6694 */
6695 I915_WRITE(_3D_CHICKEN3,
6696 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6697
6698 /*
6699 * According to the spec the following bits should be
6700 * set in order to enable memory self-refresh and fbc:
6701 * The bit21 and bit22 of 0x42000
6702 * The bit21 and bit22 of 0x42004
6703 * The bit5 and bit7 of 0x42020
6704 * The bit14 of 0x70180
6705 * The bit14 of 0x71180
6706 *
6707 * WaFbcAsynchFlipDisableFbcQueue:snb
6708 */
6709 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6710 I915_READ(ILK_DISPLAY_CHICKEN1) |
6711 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6712 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6713 I915_READ(ILK_DISPLAY_CHICKEN2) |
6714 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6715 I915_WRITE(ILK_DSPCLK_GATE_D,
6716 I915_READ(ILK_DSPCLK_GATE_D) |
6717 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6718 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6719
6720 g4x_disable_trickle_feed(dev);
6721
6722 cpt_init_clock_gating(dev);
6723
6724 gen6_check_mch_setup(dev);
6725 }
6726
6727 static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6728 {
6729 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6730
6731 /*
6732 * WaVSThreadDispatchOverride:ivb,vlv
6733 *
6734 * This actually overrides the dispatch
6735 * mode for all thread types.
6736 */
6737 reg &= ~GEN7_FF_SCHED_MASK;
6738 reg |= GEN7_FF_TS_SCHED_HW;
6739 reg |= GEN7_FF_VS_SCHED_HW;
6740 reg |= GEN7_FF_DS_SCHED_HW;
6741
6742 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6743 }
6744
6745 static void lpt_init_clock_gating(struct drm_device *dev)
6746 {
6747 struct drm_i915_private *dev_priv = dev->dev_private;
6748
6749 /*
6750 * TODO: this bit should only be enabled when really needed, then
6751 * disabled when not needed anymore in order to save power.
6752 */
6753 if (HAS_PCH_LPT_LP(dev))
6754 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6755 I915_READ(SOUTH_DSPCLK_GATE_D) |
6756 PCH_LP_PARTITION_LEVEL_DISABLE);
6757
6758 /* WADPOClockGatingDisable:hsw */
6759 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6760 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
6761 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6762 }
6763
6764 static void lpt_suspend_hw(struct drm_device *dev)
6765 {
6766 struct drm_i915_private *dev_priv = dev->dev_private;
6767
6768 if (HAS_PCH_LPT_LP(dev)) {
6769 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6770
6771 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6772 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6773 }
6774 }
6775
6776 static void broadwell_init_clock_gating(struct drm_device *dev)
6777 {
6778 struct drm_i915_private *dev_priv = dev->dev_private;
6779 enum pipe pipe;
6780 uint32_t misccpctl;
6781
6782 ilk_init_lp_watermarks(dev);
6783
6784 /* WaSwitchSolVfFArbitrationPriority:bdw */
6785 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6786
6787 /* WaPsrDPAMaskVBlankInSRD:bdw */
6788 I915_WRITE(CHICKEN_PAR1_1,
6789 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6790
6791 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
6792 for_each_pipe(dev_priv, pipe) {
6793 I915_WRITE(CHICKEN_PIPESL_1(pipe),
6794 I915_READ(CHICKEN_PIPESL_1(pipe)) |
6795 BDW_DPRS_MASK_VBLANK_SRD);
6796 }
6797
6798 /* WaVSRefCountFullforceMissDisable:bdw */
6799 /* WaDSRefCountFullforceMissDisable:bdw */
6800 I915_WRITE(GEN7_FF_THREAD_MODE,
6801 I915_READ(GEN7_FF_THREAD_MODE) &
6802 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6803
6804 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6805 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6806
6807 /* WaDisableSDEUnitClockGating:bdw */
6808 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6809 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6810
6811 /*
6812 * WaProgramL3SqcReg1Default:bdw
6813 * WaTempDisableDOPClkGating:bdw
6814 */
6815 misccpctl = I915_READ(GEN7_MISCCPCTL);
6816 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6817 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
6818 /*
6819 * Wait at least 100 clocks before re-enabling clock gating. See
6820 * the definition of L3SQCREG1 in BSpec.
6821 */
6822 POSTING_READ(GEN8_L3SQCREG1);
6823 udelay(1);
6824 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6825
6826 /*
6827 * WaGttCachingOffByDefault:bdw
6828 * GTT cache may not work with big pages, so if those
6829 * are ever enabled GTT cache may need to be disabled.
6830 */
6831 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6832
6833 lpt_init_clock_gating(dev);
6834 }
6835
6836 static void haswell_init_clock_gating(struct drm_device *dev)
6837 {
6838 struct drm_i915_private *dev_priv = dev->dev_private;
6839
6840 ilk_init_lp_watermarks(dev);
6841
6842 /* L3 caching of data atomics doesn't work -- disable it. */
6843 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6844 I915_WRITE(HSW_ROW_CHICKEN3,
6845 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6846
6847 /* This is required by WaCatErrorRejectionIssue:hsw */
6848 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6849 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6850 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6851
6852 /* WaVSRefCountFullforceMissDisable:hsw */
6853 I915_WRITE(GEN7_FF_THREAD_MODE,
6854 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6855
6856 /* WaDisable_RenderCache_OperationalFlush:hsw */
6857 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6858
6859 /* enable HiZ Raw Stall Optimization */
6860 I915_WRITE(CACHE_MODE_0_GEN7,
6861 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6862
6863 /* WaDisable4x2SubspanOptimization:hsw */
6864 I915_WRITE(CACHE_MODE_1,
6865 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6866
6867 /*
6868 * BSpec recommends 8x4 when MSAA is used,
6869 * however in practice 16x4 seems fastest.
6870 *
6871 * Note that PS/WM thread counts depend on the WIZ hashing
6872 * disable bit, which we don't touch here, but it's good
6873 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6874 */
6875 I915_WRITE(GEN7_GT_MODE,
6876 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6877
6878 /* WaSampleCChickenBitEnable:hsw */
6879 I915_WRITE(HALF_SLICE_CHICKEN3,
6880 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6881
6882 /* WaSwitchSolVfFArbitrationPriority:hsw */
6883 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6884
6885 /* WaRsPkgCStateDisplayPMReq:hsw */
6886 I915_WRITE(CHICKEN_PAR1_1,
6887 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6888
6889 lpt_init_clock_gating(dev);
6890 }
6891
6892 static void ivybridge_init_clock_gating(struct drm_device *dev)
6893 {
6894 struct drm_i915_private *dev_priv = dev->dev_private;
6895 uint32_t snpcr;
6896
6897 ilk_init_lp_watermarks(dev);
6898
6899 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6900
6901 /* WaDisableEarlyCull:ivb */
6902 I915_WRITE(_3D_CHICKEN3,
6903 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6904
6905 /* WaDisableBackToBackFlipFix:ivb */
6906 I915_WRITE(IVB_CHICKEN3,
6907 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6908 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6909
6910 /* WaDisablePSDDualDispatchEnable:ivb */
6911 if (IS_IVB_GT1(dev))
6912 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6913 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6914
6915 /* WaDisable_RenderCache_OperationalFlush:ivb */
6916 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6917
6918 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6919 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6920 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6921
6922 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6923 I915_WRITE(GEN7_L3CNTLREG1,
6924 GEN7_WA_FOR_GEN7_L3_CONTROL);
6925 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6926 GEN7_WA_L3_CHICKEN_MODE);
6927 if (IS_IVB_GT1(dev))
6928 I915_WRITE(GEN7_ROW_CHICKEN2,
6929 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6930 else {
6931 /* must write both registers */
6932 I915_WRITE(GEN7_ROW_CHICKEN2,
6933 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6934 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6935 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6936 }
6937
6938 /* WaForceL3Serialization:ivb */
6939 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6940 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6941
6942 /*
6943 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6944 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6945 */
6946 I915_WRITE(GEN6_UCGCTL2,
6947 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6948
6949 /* This is required by WaCatErrorRejectionIssue:ivb */
6950 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6951 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6952 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6953
6954 g4x_disable_trickle_feed(dev);
6955
6956 gen7_setup_fixed_func_scheduler(dev_priv);
6957
6958 if (0) { /* causes HiZ corruption on ivb:gt1 */
6959 /* enable HiZ Raw Stall Optimization */
6960 I915_WRITE(CACHE_MODE_0_GEN7,
6961 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6962 }
6963
6964 /* WaDisable4x2SubspanOptimization:ivb */
6965 I915_WRITE(CACHE_MODE_1,
6966 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6967
6968 /*
6969 * BSpec recommends 8x4 when MSAA is used,
6970 * however in practice 16x4 seems fastest.
6971 *
6972 * Note that PS/WM thread counts depend on the WIZ hashing
6973 * disable bit, which we don't touch here, but it's good
6974 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6975 */
6976 I915_WRITE(GEN7_GT_MODE,
6977 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6978
6979 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6980 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6981 snpcr |= GEN6_MBC_SNPCR_MED;
6982 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6983
6984 if (!HAS_PCH_NOP(dev))
6985 cpt_init_clock_gating(dev);
6986
6987 gen6_check_mch_setup(dev);
6988 }
6989
6990 static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6991 {
6992 u32 val;
6993
6994 /*
6995 * On driver load, a pipe may be active and driving a DSI display.
6996 * Preserve DPOUNIT_CLOCK_GATE_DISABLE to avoid the pipe getting stuck
6997 * (and never recovering) in this case. intel_dsi_post_disable() will
6998 * clear it when we turn off the display.
6999 */
7000 val = I915_READ(DSPCLK_GATE_D);
7001 val &= DPOUNIT_CLOCK_GATE_DISABLE;
7002 val |= VRHUNIT_CLOCK_GATE_DISABLE;
7003 I915_WRITE(DSPCLK_GATE_D, val);
7004
7005 /*
7006 * Disable trickle feed and enable pnd deadline calculation
7007 */
7008 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
7009 I915_WRITE(CBR1_VLV, 0);
7010 }
7011
7012 static void valleyview_init_clock_gating(struct drm_device *dev)
7013 {
7014 struct drm_i915_private *dev_priv = dev->dev_private;
7015
7016 vlv_init_display_clock_gating(dev_priv);
7017
7018 /* WaDisableEarlyCull:vlv */
7019 I915_WRITE(_3D_CHICKEN3,
7020 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7021
7022 /* WaDisableBackToBackFlipFix:vlv */
7023 I915_WRITE(IVB_CHICKEN3,
7024 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7025 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7026
7027 /* WaPsdDispatchEnable:vlv */
7028 /* WaDisablePSDDualDispatchEnable:vlv */
7029 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7030 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7031 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
7032
7033 /* WaDisable_RenderCache_OperationalFlush:vlv */
7034 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7035
7036 /* WaForceL3Serialization:vlv */
7037 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7038 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7039
7040 /* WaDisableDopClockGating:vlv */
7041 I915_WRITE(GEN7_ROW_CHICKEN2,
7042 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7043
7044 /* This is required by WaCatErrorRejectionIssue:vlv */
7045 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7046 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7047 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7048
7049 gen7_setup_fixed_func_scheduler(dev_priv);
7050
7051 /*
7052 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
7053 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
7054 */
7055 I915_WRITE(GEN6_UCGCTL2,
7056 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
7057
7058 /* WaDisableL3Bank2xClockGate:vlv
7059 * Disabling L3 clock gating- MMIO 940c[25] = 1
7060 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7061 I915_WRITE(GEN7_UCGCTL4,
7062 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
7063
7064 /*
7065 * BSpec says this must be set, even though
7066 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7067 */
7068 I915_WRITE(CACHE_MODE_1,
7069 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
7070
7071 /*
7072 * BSpec recommends 8x4 when MSAA is used,
7073 * however in practice 16x4 seems fastest.
7074 *
7075 * Note that PS/WM thread counts depend on the WIZ hashing
7076 * disable bit, which we don't touch here, but it's good
7077 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7078 */
7079 I915_WRITE(GEN7_GT_MODE,
7080 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7081
7082 /*
7083 * WaIncreaseL3CreditsForVLVB0:vlv
7084 * This is the hardware default actually.
7085 */
7086 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7087
7088 /*
7089 * WaDisableVLVClockGating_VBIIssue:vlv
7090 * Disable clock gating on th GCFG unit to prevent a delay
7091 * in the reporting of vblank events.
7092 */
7093 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
7094 }
7095
7096 static void cherryview_init_clock_gating(struct drm_device *dev)
7097 {
7098 struct drm_i915_private *dev_priv = dev->dev_private;
7099
7100 vlv_init_display_clock_gating(dev_priv);
7101
7102 /* WaVSRefCountFullforceMissDisable:chv */
7103 /* WaDSRefCountFullforceMissDisable:chv */
7104 I915_WRITE(GEN7_FF_THREAD_MODE,
7105 I915_READ(GEN7_FF_THREAD_MODE) &
7106 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
7107
7108 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7109 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7110 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
7111
7112 /* WaDisableCSUnitClockGating:chv */
7113 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7114 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7115
7116 /* WaDisableSDEUnitClockGating:chv */
7117 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7118 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7119
7120 /*
7121 * GTT cache may not work with big pages, so if those
7122 * are ever enabled GTT cache may need to be disabled.
7123 */
7124 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7125 }
7126
7127 static void g4x_init_clock_gating(struct drm_device *dev)
7128 {
7129 struct drm_i915_private *dev_priv = dev->dev_private;
7130 uint32_t dspclk_gate;
7131
7132 I915_WRITE(RENCLK_GATE_D1, 0);
7133 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7134 GS_UNIT_CLOCK_GATE_DISABLE |
7135 CL_UNIT_CLOCK_GATE_DISABLE);
7136 I915_WRITE(RAMCLK_GATE_D, 0);
7137 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7138 OVRUNIT_CLOCK_GATE_DISABLE |
7139 OVCUNIT_CLOCK_GATE_DISABLE;
7140 if (IS_GM45(dev))
7141 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7142 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
7143
7144 /* WaDisableRenderCachePipelinedFlush */
7145 I915_WRITE(CACHE_MODE_0,
7146 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
7147
7148 /* WaDisable_RenderCache_OperationalFlush:g4x */
7149 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7150
7151 g4x_disable_trickle_feed(dev);
7152 }
7153
7154 static void crestline_init_clock_gating(struct drm_device *dev)
7155 {
7156 struct drm_i915_private *dev_priv = dev->dev_private;
7157
7158 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7159 I915_WRITE(RENCLK_GATE_D2, 0);
7160 I915_WRITE(DSPCLK_GATE_D, 0);
7161 I915_WRITE(RAMCLK_GATE_D, 0);
7162 I915_WRITE16(DEUC, 0);
7163 I915_WRITE(MI_ARB_STATE,
7164 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7165
7166 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7167 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7168 }
7169
7170 static void broadwater_init_clock_gating(struct drm_device *dev)
7171 {
7172 struct drm_i915_private *dev_priv = dev->dev_private;
7173
7174 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7175 I965_RCC_CLOCK_GATE_DISABLE |
7176 I965_RCPB_CLOCK_GATE_DISABLE |
7177 I965_ISC_CLOCK_GATE_DISABLE |
7178 I965_FBC_CLOCK_GATE_DISABLE);
7179 I915_WRITE(RENCLK_GATE_D2, 0);
7180 I915_WRITE(MI_ARB_STATE,
7181 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7182
7183 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7184 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7185 }
7186
7187 static void gen3_init_clock_gating(struct drm_device *dev)
7188 {
7189 struct drm_i915_private *dev_priv = dev->dev_private;
7190 u32 dstate = I915_READ(D_STATE);
7191
7192 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7193 DSTATE_DOT_CLOCK_GATING;
7194 I915_WRITE(D_STATE, dstate);
7195
7196 if (IS_PINEVIEW(dev))
7197 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
7198
7199 /* IIR "flip pending" means done if this bit is set */
7200 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
7201
7202 /* interrupts should cause a wake up from C3 */
7203 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
7204
7205 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7206 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
7207
7208 I915_WRITE(MI_ARB_STATE,
7209 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7210 }
7211
7212 static void i85x_init_clock_gating(struct drm_device *dev)
7213 {
7214 struct drm_i915_private *dev_priv = dev->dev_private;
7215
7216 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7217
7218 /* interrupts should cause a wake up from C3 */
7219 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7220 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7221
7222 I915_WRITE(MEM_MODE,
7223 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7224 }
7225
7226 static void i830_init_clock_gating(struct drm_device *dev)
7227 {
7228 struct drm_i915_private *dev_priv = dev->dev_private;
7229
7230 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
7231
7232 I915_WRITE(MEM_MODE,
7233 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7234 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7235 }
7236
7237 void intel_init_clock_gating(struct drm_device *dev)
7238 {
7239 struct drm_i915_private *dev_priv = dev->dev_private;
7240
7241 if (dev_priv->display.init_clock_gating)
7242 dev_priv->display.init_clock_gating(dev);
7243 }
7244
7245 void intel_suspend_hw(struct drm_device *dev)
7246 {
7247 if (HAS_PCH_LPT(dev))
7248 lpt_suspend_hw(dev);
7249 }
7250
7251 /* Set up chip specific power management-related functions */
7252 void intel_init_pm(struct drm_device *dev)
7253 {
7254 struct drm_i915_private *dev_priv = dev->dev_private;
7255
7256 intel_fbc_init(dev_priv);
7257
7258 /* For cxsr */
7259 if (IS_PINEVIEW(dev))
7260 i915_pineview_get_mem_freq(dev);
7261 else if (IS_GEN5(dev))
7262 i915_ironlake_get_mem_freq(dev);
7263
7264 /* For FIFO watermark updates */
7265 if (INTEL_INFO(dev)->gen >= 9) {
7266 skl_setup_wm_latency(dev);
7267
7268 if (IS_BROXTON(dev))
7269 dev_priv->display.init_clock_gating =
7270 bxt_init_clock_gating;
7271 dev_priv->display.update_wm = skl_update_wm;
7272 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
7273 } else if (HAS_PCH_SPLIT(dev)) {
7274 ilk_setup_wm_latency(dev);
7275
7276 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7277 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7278 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7279 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7280 dev_priv->display.update_wm = ilk_update_wm;
7281 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7282 } else {
7283 DRM_DEBUG_KMS("Failed to read display plane latency. "
7284 "Disable CxSR\n");
7285 }
7286
7287 if (IS_GEN5(dev))
7288 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7289 else if (IS_GEN6(dev))
7290 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7291 else if (IS_IVYBRIDGE(dev))
7292 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7293 else if (IS_HASWELL(dev))
7294 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7295 else if (INTEL_INFO(dev)->gen == 8)
7296 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7297 } else if (IS_CHERRYVIEW(dev)) {
7298 vlv_setup_wm_latency(dev);
7299
7300 dev_priv->display.update_wm = vlv_update_wm;
7301 dev_priv->display.init_clock_gating =
7302 cherryview_init_clock_gating;
7303 } else if (IS_VALLEYVIEW(dev)) {
7304 vlv_setup_wm_latency(dev);
7305
7306 dev_priv->display.update_wm = vlv_update_wm;
7307 dev_priv->display.init_clock_gating =
7308 valleyview_init_clock_gating;
7309 } else if (IS_PINEVIEW(dev)) {
7310 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7311 dev_priv->is_ddr3,
7312 dev_priv->fsb_freq,
7313 dev_priv->mem_freq)) {
7314 DRM_INFO("failed to find known CxSR latency "
7315 "(found ddr%s fsb freq %d, mem freq %d), "
7316 "disabling CxSR\n",
7317 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7318 dev_priv->fsb_freq, dev_priv->mem_freq);
7319 /* Disable CxSR and never update its watermark again */
7320 intel_set_memory_cxsr(dev_priv, false);
7321 dev_priv->display.update_wm = NULL;
7322 } else
7323 dev_priv->display.update_wm = pineview_update_wm;
7324 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7325 } else if (IS_G4X(dev)) {
7326 dev_priv->display.update_wm = g4x_update_wm;
7327 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7328 } else if (IS_GEN4(dev)) {
7329 dev_priv->display.update_wm = i965_update_wm;
7330 if (IS_CRESTLINE(dev))
7331 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7332 else if (IS_BROADWATER(dev))
7333 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7334 } else if (IS_GEN3(dev)) {
7335 dev_priv->display.update_wm = i9xx_update_wm;
7336 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7337 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7338 } else if (IS_GEN2(dev)) {
7339 if (INTEL_INFO(dev)->num_pipes == 1) {
7340 dev_priv->display.update_wm = i845_update_wm;
7341 dev_priv->display.get_fifo_size = i845_get_fifo_size;
7342 } else {
7343 dev_priv->display.update_wm = i9xx_update_wm;
7344 dev_priv->display.get_fifo_size = i830_get_fifo_size;
7345 }
7346
7347 if (IS_I85X(dev) || IS_I865G(dev))
7348 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7349 else
7350 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7351 } else {
7352 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7353 }
7354 }
7355
7356 int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
7357 {
7358 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7359
7360 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7361 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7362 return -EAGAIN;
7363 }
7364
7365 I915_WRITE(GEN6_PCODE_DATA, *val);
7366 I915_WRITE(GEN6_PCODE_DATA1, 0);
7367 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7368
7369 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7370 500)) {
7371 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7372 return -ETIMEDOUT;
7373 }
7374
7375 *val = I915_READ(GEN6_PCODE_DATA);
7376 I915_WRITE(GEN6_PCODE_DATA, 0);
7377
7378 return 0;
7379 }
7380
7381 int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
7382 {
7383 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7384
7385 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7386 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7387 return -EAGAIN;
7388 }
7389
7390 I915_WRITE(GEN6_PCODE_DATA, val);
7391 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7392
7393 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7394 500)) {
7395 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7396 return -ETIMEDOUT;
7397 }
7398
7399 I915_WRITE(GEN6_PCODE_DATA, 0);
7400
7401 return 0;
7402 }
7403
7404 static int vlv_gpu_freq_div(unsigned int czclk_freq)
7405 {
7406 switch (czclk_freq) {
7407 case 200:
7408 return 10;
7409 case 267:
7410 return 12;
7411 case 320:
7412 case 333:
7413 return 16;
7414 case 400:
7415 return 20;
7416 default:
7417 return -1;
7418 }
7419 }
7420
7421 static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7422 {
7423 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7424
7425 div = vlv_gpu_freq_div(czclk_freq);
7426 if (div < 0)
7427 return div;
7428
7429 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
7430 }
7431
7432 static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7433 {
7434 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7435
7436 mul = vlv_gpu_freq_div(czclk_freq);
7437 if (mul < 0)
7438 return mul;
7439
7440 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
7441 }
7442
7443 static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7444 {
7445 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7446
7447 div = vlv_gpu_freq_div(czclk_freq) / 2;
7448 if (div < 0)
7449 return div;
7450
7451 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
7452 }
7453
7454 static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7455 {
7456 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7457
7458 mul = vlv_gpu_freq_div(czclk_freq) / 2;
7459 if (mul < 0)
7460 return mul;
7461
7462 /* CHV needs even values */
7463 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
7464 }
7465
7466 int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7467 {
7468 if (IS_GEN9(dev_priv->dev))
7469 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7470 GEN9_FREQ_SCALER);
7471 else if (IS_CHERRYVIEW(dev_priv->dev))
7472 return chv_gpu_freq(dev_priv, val);
7473 else if (IS_VALLEYVIEW(dev_priv->dev))
7474 return byt_gpu_freq(dev_priv, val);
7475 else
7476 return val * GT_FREQUENCY_MULTIPLIER;
7477 }
7478
7479 int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7480 {
7481 if (IS_GEN9(dev_priv->dev))
7482 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7483 GT_FREQUENCY_MULTIPLIER);
7484 else if (IS_CHERRYVIEW(dev_priv->dev))
7485 return chv_freq_opcode(dev_priv, val);
7486 else if (IS_VALLEYVIEW(dev_priv->dev))
7487 return byt_freq_opcode(dev_priv, val);
7488 else
7489 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
7490 }
7491
7492 struct request_boost {
7493 struct work_struct work;
7494 struct drm_i915_gem_request *req;
7495 };
7496
7497 static void __intel_rps_boost_work(struct work_struct *work)
7498 {
7499 struct request_boost *boost = container_of(work, struct request_boost, work);
7500 struct drm_i915_gem_request *req = boost->req;
7501
7502 if (!i915_gem_request_completed(req, true))
7503 gen6_rps_boost(to_i915(req->ring->dev), NULL,
7504 req->emitted_jiffies);
7505
7506 i915_gem_request_unreference__unlocked(req);
7507 kfree(boost);
7508 }
7509
7510 void intel_queue_rps_boost_for_request(struct drm_device *dev,
7511 struct drm_i915_gem_request *req)
7512 {
7513 struct request_boost *boost;
7514
7515 if (req == NULL || INTEL_INFO(dev)->gen < 6)
7516 return;
7517
7518 if (i915_gem_request_completed(req, true))
7519 return;
7520
7521 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7522 if (boost == NULL)
7523 return;
7524
7525 i915_gem_request_reference(req);
7526 boost->req = req;
7527
7528 INIT_WORK(&boost->work, __intel_rps_boost_work);
7529 queue_work(to_i915(dev)->wq, &boost->work);
7530 }
7531
7532 void intel_pm_setup(struct drm_device *dev)
7533 {
7534 struct drm_i915_private *dev_priv = dev->dev_private;
7535
7536 mutex_init(&dev_priv->rps.hw_lock);
7537 spin_lock_init(&dev_priv->rps.client_lock);
7538
7539 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7540 intel_gen6_powersave_work);
7541 INIT_LIST_HEAD(&dev_priv->rps.clients);
7542 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7543 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
7544
7545 dev_priv->pm.suspended = false;
7546 }
7547