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