]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Reorganize FBC function pointer initializaition
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
760285e7
DH
40#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
c0f372b3 42#include <linux/dma_remapping.h>
79e53945 43
3dec0095 44static void intel_increase_pllclock(struct drm_crtc *crtc);
6b383a7f 45static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 46
f1f644dc
JB
47static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
18442d08
VS
49static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
f1f644dc 51
e7457a9a
DL
52static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
54
55
79e53945 56typedef struct {
0206e353 57 int min, max;
79e53945
JB
58} intel_range_t;
59
60typedef struct {
0206e353
AJ
61 int dot_limit;
62 int p2_slow, p2_fast;
79e53945
JB
63} intel_p2_t;
64
d4906093
ML
65typedef struct intel_limit intel_limit_t;
66struct intel_limit {
0206e353
AJ
67 intel_range_t dot, vco, n, m, m1, m2, p, p1;
68 intel_p2_t p2;
d4906093 69};
79e53945 70
d2acd215
DV
71int
72intel_pch_rawclk(struct drm_device *dev)
73{
74 struct drm_i915_private *dev_priv = dev->dev_private;
75
76 WARN_ON(!HAS_PCH_SPLIT(dev));
77
78 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
79}
80
021357ac
CW
81static inline u32 /* units of 100MHz */
82intel_fdi_link_freq(struct drm_device *dev)
83{
8b99e68c
CW
84 if (IS_GEN5(dev)) {
85 struct drm_i915_private *dev_priv = dev->dev_private;
86 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 } else
88 return 27;
021357ac
CW
89}
90
5d536e28 91static const intel_limit_t intel_limits_i8xx_dac = {
0206e353
AJ
92 .dot = { .min = 25000, .max = 350000 },
93 .vco = { .min = 930000, .max = 1400000 },
94 .n = { .min = 3, .max = 16 },
95 .m = { .min = 96, .max = 140 },
96 .m1 = { .min = 18, .max = 26 },
97 .m2 = { .min = 6, .max = 16 },
98 .p = { .min = 4, .max = 128 },
99 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
100 .p2 = { .dot_limit = 165000,
101 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
102};
103
5d536e28
DV
104static const intel_limit_t intel_limits_i8xx_dvo = {
105 .dot = { .min = 25000, .max = 350000 },
106 .vco = { .min = 930000, .max = 1400000 },
107 .n = { .min = 3, .max = 16 },
108 .m = { .min = 96, .max = 140 },
109 .m1 = { .min = 18, .max = 26 },
110 .m2 = { .min = 6, .max = 16 },
111 .p = { .min = 4, .max = 128 },
112 .p1 = { .min = 2, .max = 33 },
113 .p2 = { .dot_limit = 165000,
114 .p2_slow = 4, .p2_fast = 4 },
115};
116
e4b36699 117static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353
AJ
118 .dot = { .min = 25000, .max = 350000 },
119 .vco = { .min = 930000, .max = 1400000 },
120 .n = { .min = 3, .max = 16 },
121 .m = { .min = 96, .max = 140 },
122 .m1 = { .min = 18, .max = 26 },
123 .m2 = { .min = 6, .max = 16 },
124 .p = { .min = 4, .max = 128 },
125 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
126 .p2 = { .dot_limit = 165000,
127 .p2_slow = 14, .p2_fast = 7 },
e4b36699 128};
273e27ca 129
e4b36699 130static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
131 .dot = { .min = 20000, .max = 400000 },
132 .vco = { .min = 1400000, .max = 2800000 },
133 .n = { .min = 1, .max = 6 },
134 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
135 .m1 = { .min = 8, .max = 18 },
136 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
137 .p = { .min = 5, .max = 80 },
138 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
139 .p2 = { .dot_limit = 200000,
140 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
141};
142
143static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
144 .dot = { .min = 20000, .max = 400000 },
145 .vco = { .min = 1400000, .max = 2800000 },
146 .n = { .min = 1, .max = 6 },
147 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
148 .m1 = { .min = 8, .max = 18 },
149 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
150 .p = { .min = 7, .max = 98 },
151 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
152 .p2 = { .dot_limit = 112000,
153 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
154};
155
273e27ca 156
e4b36699 157static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
158 .dot = { .min = 25000, .max = 270000 },
159 .vco = { .min = 1750000, .max = 3500000},
160 .n = { .min = 1, .max = 4 },
161 .m = { .min = 104, .max = 138 },
162 .m1 = { .min = 17, .max = 23 },
163 .m2 = { .min = 5, .max = 11 },
164 .p = { .min = 10, .max = 30 },
165 .p1 = { .min = 1, .max = 3},
166 .p2 = { .dot_limit = 270000,
167 .p2_slow = 10,
168 .p2_fast = 10
044c7c41 169 },
e4b36699
KP
170};
171
172static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
173 .dot = { .min = 22000, .max = 400000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 16, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8},
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
183};
184
185static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
186 .dot = { .min = 20000, .max = 115000 },
187 .vco = { .min = 1750000, .max = 3500000 },
188 .n = { .min = 1, .max = 3 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 28, .max = 112 },
193 .p1 = { .min = 2, .max = 8 },
194 .p2 = { .dot_limit = 0,
195 .p2_slow = 14, .p2_fast = 14
044c7c41 196 },
e4b36699
KP
197};
198
199static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
200 .dot = { .min = 80000, .max = 224000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 14, .max = 42 },
207 .p1 = { .min = 2, .max = 6 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 7, .p2_fast = 7
044c7c41 210 },
e4b36699
KP
211};
212
f2b115e6 213static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
214 .dot = { .min = 20000, .max = 400000},
215 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 216 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
217 .n = { .min = 3, .max = 6 },
218 .m = { .min = 2, .max = 256 },
273e27ca 219 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
220 .m1 = { .min = 0, .max = 0 },
221 .m2 = { .min = 0, .max = 254 },
222 .p = { .min = 5, .max = 80 },
223 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
224 .p2 = { .dot_limit = 200000,
225 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
226};
227
f2b115e6 228static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
229 .dot = { .min = 20000, .max = 400000 },
230 .vco = { .min = 1700000, .max = 3500000 },
231 .n = { .min = 3, .max = 6 },
232 .m = { .min = 2, .max = 256 },
233 .m1 = { .min = 0, .max = 0 },
234 .m2 = { .min = 0, .max = 254 },
235 .p = { .min = 7, .max = 112 },
236 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
237 .p2 = { .dot_limit = 112000,
238 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
239};
240
273e27ca
EA
241/* Ironlake / Sandybridge
242 *
243 * We calculate clock using (register_value + 2) for N/M1/M2, so here
244 * the range value for them is (actual_value - 2).
245 */
b91ad0ec 246static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
247 .dot = { .min = 25000, .max = 350000 },
248 .vco = { .min = 1760000, .max = 3510000 },
249 .n = { .min = 1, .max = 5 },
250 .m = { .min = 79, .max = 127 },
251 .m1 = { .min = 12, .max = 22 },
252 .m2 = { .min = 5, .max = 9 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
255 .p2 = { .dot_limit = 225000,
256 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
257};
258
b91ad0ec 259static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
260 .dot = { .min = 25000, .max = 350000 },
261 .vco = { .min = 1760000, .max = 3510000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 79, .max = 118 },
264 .m1 = { .min = 12, .max = 22 },
265 .m2 = { .min = 5, .max = 9 },
266 .p = { .min = 28, .max = 112 },
267 .p1 = { .min = 2, .max = 8 },
268 .p2 = { .dot_limit = 225000,
269 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
270};
271
272static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
273 .dot = { .min = 25000, .max = 350000 },
274 .vco = { .min = 1760000, .max = 3510000 },
275 .n = { .min = 1, .max = 3 },
276 .m = { .min = 79, .max = 127 },
277 .m1 = { .min = 12, .max = 22 },
278 .m2 = { .min = 5, .max = 9 },
279 .p = { .min = 14, .max = 56 },
280 .p1 = { .min = 2, .max = 8 },
281 .p2 = { .dot_limit = 225000,
282 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
283};
284
273e27ca 285/* LVDS 100mhz refclk limits. */
b91ad0ec 286static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
287 .dot = { .min = 25000, .max = 350000 },
288 .vco = { .min = 1760000, .max = 3510000 },
289 .n = { .min = 1, .max = 2 },
290 .m = { .min = 79, .max = 126 },
291 .m1 = { .min = 12, .max = 22 },
292 .m2 = { .min = 5, .max = 9 },
293 .p = { .min = 28, .max = 112 },
0206e353 294 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
295 .p2 = { .dot_limit = 225000,
296 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 126 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 42 },
0206e353 307 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
310};
311
dc730512 312static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
313 /*
314 * These are the data rate limits (measured in fast clocks)
315 * since those are the strictest limits we have. The fast
316 * clock and actual rate limits are more relaxed, so checking
317 * them would make no difference.
318 */
319 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 320 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 321 .n = { .min = 1, .max = 7 },
a0c4da24
JB
322 .m1 = { .min = 2, .max = 3 },
323 .m2 = { .min = 11, .max = 156 },
b99ab663 324 .p1 = { .min = 2, .max = 3 },
5fdc9c49 325 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
326};
327
6b4bf1c4
VS
328static void vlv_clock(int refclk, intel_clock_t *clock)
329{
330 clock->m = clock->m1 * clock->m2;
331 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
332 if (WARN_ON(clock->n == 0 || clock->p == 0))
333 return;
fb03ac01
VS
334 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
335 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
336}
337
e0638cdf
PZ
338/**
339 * Returns whether any output on the specified pipe is of the specified type
340 */
341static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
342{
343 struct drm_device *dev = crtc->dev;
344 struct intel_encoder *encoder;
345
346 for_each_encoder_on_crtc(dev, crtc, encoder)
347 if (encoder->type == type)
348 return true;
349
350 return false;
351}
352
1b894b59
CW
353static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
354 int refclk)
2c07245f 355{
b91ad0ec 356 struct drm_device *dev = crtc->dev;
2c07245f 357 const intel_limit_t *limit;
b91ad0ec
ZW
358
359 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 360 if (intel_is_dual_link_lvds(dev)) {
1b894b59 361 if (refclk == 100000)
b91ad0ec
ZW
362 limit = &intel_limits_ironlake_dual_lvds_100m;
363 else
364 limit = &intel_limits_ironlake_dual_lvds;
365 } else {
1b894b59 366 if (refclk == 100000)
b91ad0ec
ZW
367 limit = &intel_limits_ironlake_single_lvds_100m;
368 else
369 limit = &intel_limits_ironlake_single_lvds;
370 }
c6bb3538 371 } else
b91ad0ec 372 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
373
374 return limit;
375}
376
044c7c41
ML
377static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
378{
379 struct drm_device *dev = crtc->dev;
044c7c41
ML
380 const intel_limit_t *limit;
381
382 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 383 if (intel_is_dual_link_lvds(dev))
e4b36699 384 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 385 else
e4b36699 386 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
387 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
388 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 389 limit = &intel_limits_g4x_hdmi;
044c7c41 390 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 391 limit = &intel_limits_g4x_sdvo;
044c7c41 392 } else /* The option is for other outputs */
e4b36699 393 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
394
395 return limit;
396}
397
1b894b59 398static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
399{
400 struct drm_device *dev = crtc->dev;
401 const intel_limit_t *limit;
402
bad720ff 403 if (HAS_PCH_SPLIT(dev))
1b894b59 404 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 405 else if (IS_G4X(dev)) {
044c7c41 406 limit = intel_g4x_limit(crtc);
f2b115e6 407 } else if (IS_PINEVIEW(dev)) {
2177832f 408 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 409 limit = &intel_limits_pineview_lvds;
2177832f 410 else
f2b115e6 411 limit = &intel_limits_pineview_sdvo;
a0c4da24 412 } else if (IS_VALLEYVIEW(dev)) {
dc730512 413 limit = &intel_limits_vlv;
a6c45cf0
CW
414 } else if (!IS_GEN2(dev)) {
415 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
416 limit = &intel_limits_i9xx_lvds;
417 else
418 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
419 } else {
420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 421 limit = &intel_limits_i8xx_lvds;
5d536e28 422 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 423 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
424 else
425 limit = &intel_limits_i8xx_dac;
79e53945
JB
426 }
427 return limit;
428}
429
f2b115e6
AJ
430/* m1 is reserved as 0 in Pineview, n is a ring counter */
431static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 432{
2177832f
SL
433 clock->m = clock->m2 + 2;
434 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
435 if (WARN_ON(clock->n == 0 || clock->p == 0))
436 return;
fb03ac01
VS
437 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
438 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
439}
440
7429e9d4
DV
441static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
442{
443 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
444}
445
ac58c3f0 446static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 447{
7429e9d4 448 clock->m = i9xx_dpll_compute_m(clock);
79e53945 449 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
450 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
451 return;
fb03ac01
VS
452 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
453 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
454}
455
7c04d1d9 456#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
457/**
458 * Returns whether the given set of divisors are valid for a given refclk with
459 * the given connectors.
460 */
461
1b894b59
CW
462static bool intel_PLL_is_valid(struct drm_device *dev,
463 const intel_limit_t *limit,
464 const intel_clock_t *clock)
79e53945 465{
f01b7962
VS
466 if (clock->n < limit->n.min || limit->n.max < clock->n)
467 INTELPllInvalid("n out of range\n");
79e53945 468 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 469 INTELPllInvalid("p1 out of range\n");
79e53945 470 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 471 INTELPllInvalid("m2 out of range\n");
79e53945 472 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 473 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
474
475 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
476 if (clock->m1 <= clock->m2)
477 INTELPllInvalid("m1 <= m2\n");
478
479 if (!IS_VALLEYVIEW(dev)) {
480 if (clock->p < limit->p.min || limit->p.max < clock->p)
481 INTELPllInvalid("p out of range\n");
482 if (clock->m < limit->m.min || limit->m.max < clock->m)
483 INTELPllInvalid("m out of range\n");
484 }
485
79e53945 486 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 487 INTELPllInvalid("vco out of range\n");
79e53945
JB
488 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
489 * connector, etc., rather than just a single range.
490 */
491 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 492 INTELPllInvalid("dot out of range\n");
79e53945
JB
493
494 return true;
495}
496
d4906093 497static bool
ee9300bb 498i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
499 int target, int refclk, intel_clock_t *match_clock,
500 intel_clock_t *best_clock)
79e53945
JB
501{
502 struct drm_device *dev = crtc->dev;
79e53945 503 intel_clock_t clock;
79e53945
JB
504 int err = target;
505
a210b028 506 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 507 /*
a210b028
DV
508 * For LVDS just rely on its current settings for dual-channel.
509 * We haven't figured out how to reliably set up different
510 * single/dual channel state, if we even can.
79e53945 511 */
1974cad0 512 if (intel_is_dual_link_lvds(dev))
79e53945
JB
513 clock.p2 = limit->p2.p2_fast;
514 else
515 clock.p2 = limit->p2.p2_slow;
516 } else {
517 if (target < limit->p2.dot_limit)
518 clock.p2 = limit->p2.p2_slow;
519 else
520 clock.p2 = limit->p2.p2_fast;
521 }
522
0206e353 523 memset(best_clock, 0, sizeof(*best_clock));
79e53945 524
42158660
ZY
525 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
526 clock.m1++) {
527 for (clock.m2 = limit->m2.min;
528 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 529 if (clock.m2 >= clock.m1)
42158660
ZY
530 break;
531 for (clock.n = limit->n.min;
532 clock.n <= limit->n.max; clock.n++) {
533 for (clock.p1 = limit->p1.min;
534 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
535 int this_err;
536
ac58c3f0
DV
537 i9xx_clock(refclk, &clock);
538 if (!intel_PLL_is_valid(dev, limit,
539 &clock))
540 continue;
541 if (match_clock &&
542 clock.p != match_clock->p)
543 continue;
544
545 this_err = abs(clock.dot - target);
546 if (this_err < err) {
547 *best_clock = clock;
548 err = this_err;
549 }
550 }
551 }
552 }
553 }
554
555 return (err != target);
556}
557
558static bool
ee9300bb
DV
559pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
560 int target, int refclk, intel_clock_t *match_clock,
561 intel_clock_t *best_clock)
79e53945
JB
562{
563 struct drm_device *dev = crtc->dev;
79e53945 564 intel_clock_t clock;
79e53945
JB
565 int err = target;
566
a210b028 567 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 568 /*
a210b028
DV
569 * For LVDS just rely on its current settings for dual-channel.
570 * We haven't figured out how to reliably set up different
571 * single/dual channel state, if we even can.
79e53945 572 */
1974cad0 573 if (intel_is_dual_link_lvds(dev))
79e53945
JB
574 clock.p2 = limit->p2.p2_fast;
575 else
576 clock.p2 = limit->p2.p2_slow;
577 } else {
578 if (target < limit->p2.dot_limit)
579 clock.p2 = limit->p2.p2_slow;
580 else
581 clock.p2 = limit->p2.p2_fast;
582 }
583
0206e353 584 memset(best_clock, 0, sizeof(*best_clock));
79e53945 585
42158660
ZY
586 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
587 clock.m1++) {
588 for (clock.m2 = limit->m2.min;
589 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
590 for (clock.n = limit->n.min;
591 clock.n <= limit->n.max; clock.n++) {
592 for (clock.p1 = limit->p1.min;
593 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
594 int this_err;
595
ac58c3f0 596 pineview_clock(refclk, &clock);
1b894b59
CW
597 if (!intel_PLL_is_valid(dev, limit,
598 &clock))
79e53945 599 continue;
cec2f356
SP
600 if (match_clock &&
601 clock.p != match_clock->p)
602 continue;
79e53945
JB
603
604 this_err = abs(clock.dot - target);
605 if (this_err < err) {
606 *best_clock = clock;
607 err = this_err;
608 }
609 }
610 }
611 }
612 }
613
614 return (err != target);
615}
616
d4906093 617static bool
ee9300bb
DV
618g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
619 int target, int refclk, intel_clock_t *match_clock,
620 intel_clock_t *best_clock)
d4906093
ML
621{
622 struct drm_device *dev = crtc->dev;
d4906093
ML
623 intel_clock_t clock;
624 int max_n;
625 bool found;
6ba770dc
AJ
626 /* approximately equals target * 0.00585 */
627 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
628 found = false;
629
630 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 631 if (intel_is_dual_link_lvds(dev))
d4906093
ML
632 clock.p2 = limit->p2.p2_fast;
633 else
634 clock.p2 = limit->p2.p2_slow;
635 } else {
636 if (target < limit->p2.dot_limit)
637 clock.p2 = limit->p2.p2_slow;
638 else
639 clock.p2 = limit->p2.p2_fast;
640 }
641
642 memset(best_clock, 0, sizeof(*best_clock));
643 max_n = limit->n.max;
f77f13e2 644 /* based on hardware requirement, prefer smaller n to precision */
d4906093 645 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 646 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
647 for (clock.m1 = limit->m1.max;
648 clock.m1 >= limit->m1.min; clock.m1--) {
649 for (clock.m2 = limit->m2.max;
650 clock.m2 >= limit->m2.min; clock.m2--) {
651 for (clock.p1 = limit->p1.max;
652 clock.p1 >= limit->p1.min; clock.p1--) {
653 int this_err;
654
ac58c3f0 655 i9xx_clock(refclk, &clock);
1b894b59
CW
656 if (!intel_PLL_is_valid(dev, limit,
657 &clock))
d4906093 658 continue;
1b894b59
CW
659
660 this_err = abs(clock.dot - target);
d4906093
ML
661 if (this_err < err_most) {
662 *best_clock = clock;
663 err_most = this_err;
664 max_n = clock.n;
665 found = true;
666 }
667 }
668 }
669 }
670 }
2c07245f
ZW
671 return found;
672}
673
a0c4da24 674static bool
ee9300bb
DV
675vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
676 int target, int refclk, intel_clock_t *match_clock,
677 intel_clock_t *best_clock)
a0c4da24 678{
f01b7962 679 struct drm_device *dev = crtc->dev;
6b4bf1c4 680 intel_clock_t clock;
69e4f900 681 unsigned int bestppm = 1000000;
27e639bf
VS
682 /* min update 19.2 MHz */
683 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 684 bool found = false;
a0c4da24 685
6b4bf1c4
VS
686 target *= 5; /* fast clock */
687
688 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
689
690 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 691 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 692 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 693 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 694 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 695 clock.p = clock.p1 * clock.p2;
a0c4da24 696 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 697 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
698 unsigned int ppm, diff;
699
6b4bf1c4
VS
700 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
701 refclk * clock.m1);
702
703 vlv_clock(refclk, &clock);
43b0ac53 704
f01b7962
VS
705 if (!intel_PLL_is_valid(dev, limit,
706 &clock))
43b0ac53
VS
707 continue;
708
6b4bf1c4
VS
709 diff = abs(clock.dot - target);
710 ppm = div_u64(1000000ULL * diff, target);
711
712 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 713 bestppm = 0;
6b4bf1c4 714 *best_clock = clock;
49e497ef 715 found = true;
43b0ac53 716 }
6b4bf1c4 717
c686122c 718 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 719 bestppm = ppm;
6b4bf1c4 720 *best_clock = clock;
49e497ef 721 found = true;
a0c4da24
JB
722 }
723 }
724 }
725 }
726 }
a0c4da24 727
49e497ef 728 return found;
a0c4da24 729}
a4fc5ed6 730
20ddf665
VS
731bool intel_crtc_active(struct drm_crtc *crtc)
732{
733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
734
735 /* Be paranoid as we can arrive here with only partial
736 * state retrieved from the hardware during setup.
737 *
241bfc38 738 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
739 * as Haswell has gained clock readout/fastboot support.
740 *
741 * We can ditch the crtc->fb check as soon as we can
742 * properly reconstruct framebuffers.
743 */
744 return intel_crtc->active && crtc->fb &&
241bfc38 745 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
746}
747
a5c961d1
PZ
748enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
749 enum pipe pipe)
750{
751 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
753
3b117c8f 754 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
755}
756
57e22f4a 757static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
758{
759 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 760 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
761
762 frame = I915_READ(frame_reg);
763
764 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
765 DRM_DEBUG_KMS("vblank wait timed out\n");
766}
767
9d0498a2
JB
768/**
769 * intel_wait_for_vblank - wait for vblank on a given pipe
770 * @dev: drm device
771 * @pipe: pipe to wait for
772 *
773 * Wait for vblank to occur on a given pipe. Needed for various bits of
774 * mode setting code.
775 */
776void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 777{
9d0498a2 778 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 779 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 780
57e22f4a
VS
781 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
782 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
783 return;
784 }
785
300387c0
CW
786 /* Clear existing vblank status. Note this will clear any other
787 * sticky status fields as well.
788 *
789 * This races with i915_driver_irq_handler() with the result
790 * that either function could miss a vblank event. Here it is not
791 * fatal, as we will either wait upon the next vblank interrupt or
792 * timeout. Generally speaking intel_wait_for_vblank() is only
793 * called during modeset at which time the GPU should be idle and
794 * should *not* be performing page flips and thus not waiting on
795 * vblanks...
796 * Currently, the result of us stealing a vblank from the irq
797 * handler is that a single frame will be skipped during swapbuffers.
798 */
799 I915_WRITE(pipestat_reg,
800 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
801
9d0498a2 802 /* Wait for vblank interrupt bit to set */
481b6af3
CW
803 if (wait_for(I915_READ(pipestat_reg) &
804 PIPE_VBLANK_INTERRUPT_STATUS,
805 50))
9d0498a2
JB
806 DRM_DEBUG_KMS("vblank wait timed out\n");
807}
808
fbf49ea2
VS
809static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
810{
811 struct drm_i915_private *dev_priv = dev->dev_private;
812 u32 reg = PIPEDSL(pipe);
813 u32 line1, line2;
814 u32 line_mask;
815
816 if (IS_GEN2(dev))
817 line_mask = DSL_LINEMASK_GEN2;
818 else
819 line_mask = DSL_LINEMASK_GEN3;
820
821 line1 = I915_READ(reg) & line_mask;
822 mdelay(5);
823 line2 = I915_READ(reg) & line_mask;
824
825 return line1 == line2;
826}
827
ab7ad7f6
KP
828/*
829 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
830 * @dev: drm device
831 * @pipe: pipe to wait for
832 *
833 * After disabling a pipe, we can't wait for vblank in the usual way,
834 * spinning on the vblank interrupt status bit, since we won't actually
835 * see an interrupt when the pipe is disabled.
836 *
ab7ad7f6
KP
837 * On Gen4 and above:
838 * wait for the pipe register state bit to turn off
839 *
840 * Otherwise:
841 * wait for the display line value to settle (it usually
842 * ends up stopping at the start of the next frame).
58e10eb9 843 *
9d0498a2 844 */
58e10eb9 845void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
846{
847 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
848 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
849 pipe);
ab7ad7f6
KP
850
851 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 852 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
853
854 /* Wait for the Pipe State to go off */
58e10eb9
CW
855 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
856 100))
284637d9 857 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 858 } else {
ab7ad7f6 859 /* Wait for the display line to settle */
fbf49ea2 860 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 861 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 862 }
79e53945
JB
863}
864
b0ea7d37
DL
865/*
866 * ibx_digital_port_connected - is the specified port connected?
867 * @dev_priv: i915 private structure
868 * @port: the port to test
869 *
870 * Returns true if @port is connected, false otherwise.
871 */
872bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
873 struct intel_digital_port *port)
874{
875 u32 bit;
876
c36346e3
DL
877 if (HAS_PCH_IBX(dev_priv->dev)) {
878 switch(port->port) {
879 case PORT_B:
880 bit = SDE_PORTB_HOTPLUG;
881 break;
882 case PORT_C:
883 bit = SDE_PORTC_HOTPLUG;
884 break;
885 case PORT_D:
886 bit = SDE_PORTD_HOTPLUG;
887 break;
888 default:
889 return true;
890 }
891 } else {
892 switch(port->port) {
893 case PORT_B:
894 bit = SDE_PORTB_HOTPLUG_CPT;
895 break;
896 case PORT_C:
897 bit = SDE_PORTC_HOTPLUG_CPT;
898 break;
899 case PORT_D:
900 bit = SDE_PORTD_HOTPLUG_CPT;
901 break;
902 default:
903 return true;
904 }
b0ea7d37
DL
905 }
906
907 return I915_READ(SDEISR) & bit;
908}
909
b24e7179
JB
910static const char *state_string(bool enabled)
911{
912 return enabled ? "on" : "off";
913}
914
915/* Only for pre-ILK configs */
55607e8a
DV
916void assert_pll(struct drm_i915_private *dev_priv,
917 enum pipe pipe, bool state)
b24e7179
JB
918{
919 int reg;
920 u32 val;
921 bool cur_state;
922
923 reg = DPLL(pipe);
924 val = I915_READ(reg);
925 cur_state = !!(val & DPLL_VCO_ENABLE);
926 WARN(cur_state != state,
927 "PLL state assertion failure (expected %s, current %s)\n",
928 state_string(state), state_string(cur_state));
929}
b24e7179 930
23538ef1
JN
931/* XXX: the dsi pll is shared between MIPI DSI ports */
932static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
933{
934 u32 val;
935 bool cur_state;
936
937 mutex_lock(&dev_priv->dpio_lock);
938 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
939 mutex_unlock(&dev_priv->dpio_lock);
940
941 cur_state = val & DSI_PLL_VCO_EN;
942 WARN(cur_state != state,
943 "DSI PLL state assertion failure (expected %s, current %s)\n",
944 state_string(state), state_string(cur_state));
945}
946#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
947#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
948
55607e8a 949struct intel_shared_dpll *
e2b78267
DV
950intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
951{
952 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
953
a43f6e0f 954 if (crtc->config.shared_dpll < 0)
e2b78267
DV
955 return NULL;
956
a43f6e0f 957 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
958}
959
040484af 960/* For ILK+ */
55607e8a
DV
961void assert_shared_dpll(struct drm_i915_private *dev_priv,
962 struct intel_shared_dpll *pll,
963 bool state)
040484af 964{
040484af 965 bool cur_state;
5358901f 966 struct intel_dpll_hw_state hw_state;
040484af 967
9d82aa17
ED
968 if (HAS_PCH_LPT(dev_priv->dev)) {
969 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
970 return;
971 }
972
92b27b08 973 if (WARN (!pll,
46edb027 974 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 975 return;
ee7b9f93 976
5358901f 977 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 978 WARN(cur_state != state,
5358901f
DV
979 "%s assertion failure (expected %s, current %s)\n",
980 pll->name, state_string(state), state_string(cur_state));
040484af 981}
040484af
JB
982
983static void assert_fdi_tx(struct drm_i915_private *dev_priv,
984 enum pipe pipe, bool state)
985{
986 int reg;
987 u32 val;
988 bool cur_state;
ad80a810
PZ
989 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
990 pipe);
040484af 991
affa9354
PZ
992 if (HAS_DDI(dev_priv->dev)) {
993 /* DDI does not have a specific FDI_TX register */
ad80a810 994 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 995 val = I915_READ(reg);
ad80a810 996 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
997 } else {
998 reg = FDI_TX_CTL(pipe);
999 val = I915_READ(reg);
1000 cur_state = !!(val & FDI_TX_ENABLE);
1001 }
040484af
JB
1002 WARN(cur_state != state,
1003 "FDI TX state assertion failure (expected %s, current %s)\n",
1004 state_string(state), state_string(cur_state));
1005}
1006#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1007#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1008
1009static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1010 enum pipe pipe, bool state)
1011{
1012 int reg;
1013 u32 val;
1014 bool cur_state;
1015
d63fa0dc
PZ
1016 reg = FDI_RX_CTL(pipe);
1017 val = I915_READ(reg);
1018 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1019 WARN(cur_state != state,
1020 "FDI RX state assertion failure (expected %s, current %s)\n",
1021 state_string(state), state_string(cur_state));
1022}
1023#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1024#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1025
1026static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 int reg;
1030 u32 val;
1031
1032 /* ILK FDI PLL is always enabled */
1033 if (dev_priv->info->gen == 5)
1034 return;
1035
bf507ef7 1036 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1037 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1038 return;
1039
040484af
JB
1040 reg = FDI_TX_CTL(pipe);
1041 val = I915_READ(reg);
1042 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1043}
1044
55607e8a
DV
1045void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1046 enum pipe pipe, bool state)
040484af
JB
1047{
1048 int reg;
1049 u32 val;
55607e8a 1050 bool cur_state;
040484af
JB
1051
1052 reg = FDI_RX_CTL(pipe);
1053 val = I915_READ(reg);
55607e8a
DV
1054 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1055 WARN(cur_state != state,
1056 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1057 state_string(state), state_string(cur_state));
040484af
JB
1058}
1059
ea0760cf
JB
1060static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1061 enum pipe pipe)
1062{
1063 int pp_reg, lvds_reg;
1064 u32 val;
1065 enum pipe panel_pipe = PIPE_A;
0de3b485 1066 bool locked = true;
ea0760cf
JB
1067
1068 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1069 pp_reg = PCH_PP_CONTROL;
1070 lvds_reg = PCH_LVDS;
1071 } else {
1072 pp_reg = PP_CONTROL;
1073 lvds_reg = LVDS;
1074 }
1075
1076 val = I915_READ(pp_reg);
1077 if (!(val & PANEL_POWER_ON) ||
1078 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1079 locked = false;
1080
1081 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1082 panel_pipe = PIPE_B;
1083
1084 WARN(panel_pipe == pipe && locked,
1085 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1086 pipe_name(pipe));
ea0760cf
JB
1087}
1088
93ce0ba6
JN
1089static void assert_cursor(struct drm_i915_private *dev_priv,
1090 enum pipe pipe, bool state)
1091{
1092 struct drm_device *dev = dev_priv->dev;
1093 bool cur_state;
1094
1095 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
1096 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
1097 else if (IS_845G(dev) || IS_I865G(dev))
1098 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1099 else
1100 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1101
1102 WARN(cur_state != state,
1103 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1104 pipe_name(pipe), state_string(state), state_string(cur_state));
1105}
1106#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1107#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1108
b840d907
JB
1109void assert_pipe(struct drm_i915_private *dev_priv,
1110 enum pipe pipe, bool state)
b24e7179
JB
1111{
1112 int reg;
1113 u32 val;
63d7bbe9 1114 bool cur_state;
702e7a56
PZ
1115 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1116 pipe);
b24e7179 1117
8e636784
DV
1118 /* if we need the pipe A quirk it must be always on */
1119 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1120 state = true;
1121
b97186f0
PZ
1122 if (!intel_display_power_enabled(dev_priv->dev,
1123 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1124 cur_state = false;
1125 } else {
1126 reg = PIPECONF(cpu_transcoder);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & PIPECONF_ENABLE);
1129 }
1130
63d7bbe9
JB
1131 WARN(cur_state != state,
1132 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1133 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1134}
1135
931872fc
CW
1136static void assert_plane(struct drm_i915_private *dev_priv,
1137 enum plane plane, bool state)
b24e7179
JB
1138{
1139 int reg;
1140 u32 val;
931872fc 1141 bool cur_state;
b24e7179
JB
1142
1143 reg = DSPCNTR(plane);
1144 val = I915_READ(reg);
931872fc
CW
1145 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1146 WARN(cur_state != state,
1147 "plane %c assertion failure (expected %s, current %s)\n",
1148 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1149}
1150
931872fc
CW
1151#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1152#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1153
b24e7179
JB
1154static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1155 enum pipe pipe)
1156{
653e1026 1157 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1158 int reg, i;
1159 u32 val;
1160 int cur_pipe;
1161
653e1026
VS
1162 /* Primary planes are fixed to pipes on gen4+ */
1163 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1164 reg = DSPCNTR(pipe);
1165 val = I915_READ(reg);
1166 WARN((val & DISPLAY_PLANE_ENABLE),
1167 "plane %c assertion failure, should be disabled but not\n",
1168 plane_name(pipe));
19ec1358 1169 return;
28c05794 1170 }
19ec1358 1171
b24e7179 1172 /* Need to check both planes against the pipe */
08e2a7de 1173 for_each_pipe(i) {
b24e7179
JB
1174 reg = DSPCNTR(i);
1175 val = I915_READ(reg);
1176 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1177 DISPPLANE_SEL_PIPE_SHIFT;
1178 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1179 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1180 plane_name(i), pipe_name(pipe));
b24e7179
JB
1181 }
1182}
1183
19332d7a
JB
1184static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1185 enum pipe pipe)
1186{
20674eef 1187 struct drm_device *dev = dev_priv->dev;
19332d7a
JB
1188 int reg, i;
1189 u32 val;
1190
20674eef
VS
1191 if (IS_VALLEYVIEW(dev)) {
1192 for (i = 0; i < dev_priv->num_plane; i++) {
1193 reg = SPCNTR(pipe, i);
1194 val = I915_READ(reg);
1195 WARN((val & SP_ENABLE),
1196 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1197 sprite_name(pipe, i), pipe_name(pipe));
1198 }
1199 } else if (INTEL_INFO(dev)->gen >= 7) {
1200 reg = SPRCTL(pipe);
19332d7a 1201 val = I915_READ(reg);
20674eef 1202 WARN((val & SPRITE_ENABLE),
06da8da2 1203 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1204 plane_name(pipe), pipe_name(pipe));
1205 } else if (INTEL_INFO(dev)->gen >= 5) {
1206 reg = DVSCNTR(pipe);
19332d7a 1207 val = I915_READ(reg);
20674eef 1208 WARN((val & DVS_ENABLE),
06da8da2 1209 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1210 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1211 }
1212}
1213
92f2584a
JB
1214static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1215{
1216 u32 val;
1217 bool enabled;
1218
9d82aa17
ED
1219 if (HAS_PCH_LPT(dev_priv->dev)) {
1220 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1221 return;
1222 }
1223
92f2584a
JB
1224 val = I915_READ(PCH_DREF_CONTROL);
1225 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1226 DREF_SUPERSPREAD_SOURCE_MASK));
1227 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1228}
1229
ab9412ba
DV
1230static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
92f2584a
JB
1232{
1233 int reg;
1234 u32 val;
1235 bool enabled;
1236
ab9412ba 1237 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1238 val = I915_READ(reg);
1239 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1240 WARN(enabled,
1241 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1242 pipe_name(pipe));
92f2584a
JB
1243}
1244
4e634389
KP
1245static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1247{
1248 if ((val & DP_PORT_EN) == 0)
1249 return false;
1250
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
1252 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1253 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1254 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1255 return false;
1256 } else {
1257 if ((val & DP_PIPE_MASK) != (pipe << 30))
1258 return false;
1259 }
1260 return true;
1261}
1262
1519b995
KP
1263static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1264 enum pipe pipe, u32 val)
1265{
dc0fa718 1266 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1267 return false;
1268
1269 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1270 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995
KP
1271 return false;
1272 } else {
dc0fa718 1273 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1274 return false;
1275 }
1276 return true;
1277}
1278
1279static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1280 enum pipe pipe, u32 val)
1281{
1282 if ((val & LVDS_PORT_EN) == 0)
1283 return false;
1284
1285 if (HAS_PCH_CPT(dev_priv->dev)) {
1286 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1287 return false;
1288 } else {
1289 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1290 return false;
1291 }
1292 return true;
1293}
1294
1295static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, u32 val)
1297{
1298 if ((val & ADPA_DAC_ENABLE) == 0)
1299 return false;
1300 if (HAS_PCH_CPT(dev_priv->dev)) {
1301 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1302 return false;
1303 } else {
1304 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1305 return false;
1306 }
1307 return true;
1308}
1309
291906f1 1310static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1311 enum pipe pipe, int reg, u32 port_sel)
291906f1 1312{
47a05eca 1313 u32 val = I915_READ(reg);
4e634389 1314 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1315 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1316 reg, pipe_name(pipe));
de9a35ab 1317
75c5da27
DV
1318 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1319 && (val & DP_PIPEB_SELECT),
de9a35ab 1320 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1321}
1322
1323static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1324 enum pipe pipe, int reg)
1325{
47a05eca 1326 u32 val = I915_READ(reg);
b70ad586 1327 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1328 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1329 reg, pipe_name(pipe));
de9a35ab 1330
dc0fa718 1331 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1332 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1333 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1334}
1335
1336static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe)
1338{
1339 int reg;
1340 u32 val;
291906f1 1341
f0575e92
KP
1342 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1343 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1344 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1345
1346 reg = PCH_ADPA;
1347 val = I915_READ(reg);
b70ad586 1348 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1349 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1350 pipe_name(pipe));
291906f1
JB
1351
1352 reg = PCH_LVDS;
1353 val = I915_READ(reg);
b70ad586 1354 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1355 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1356 pipe_name(pipe));
291906f1 1357
e2debe91
PZ
1358 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1359 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1360 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1361}
1362
40e9cf64
JB
1363static void intel_init_dpio(struct drm_device *dev)
1364{
1365 struct drm_i915_private *dev_priv = dev->dev_private;
1366
1367 if (!IS_VALLEYVIEW(dev))
1368 return;
1369
e4607fcf 1370 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
40e9cf64
JB
1371 /*
1372 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1373 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1374 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1375 * b. The other bits such as sfr settings / modesel may all be set
1376 * to 0.
1377 *
1378 * This should only be done on init and resume from S3 with both
1379 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1380 */
1381 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1382}
1383
426115cf 1384static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1385{
426115cf
DV
1386 struct drm_device *dev = crtc->base.dev;
1387 struct drm_i915_private *dev_priv = dev->dev_private;
1388 int reg = DPLL(crtc->pipe);
1389 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1390
426115cf 1391 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1392
1393 /* No really, not for ILK+ */
1394 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1395
1396 /* PLL is protected by panel, make sure we can write it */
1397 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
426115cf 1398 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1399
426115cf
DV
1400 I915_WRITE(reg, dpll);
1401 POSTING_READ(reg);
1402 udelay(150);
1403
1404 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1405 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1406
1407 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1408 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1409
1410 /* We do this three times for luck */
426115cf 1411 I915_WRITE(reg, dpll);
87442f73
DV
1412 POSTING_READ(reg);
1413 udelay(150); /* wait for warmup */
426115cf 1414 I915_WRITE(reg, dpll);
87442f73
DV
1415 POSTING_READ(reg);
1416 udelay(150); /* wait for warmup */
426115cf 1417 I915_WRITE(reg, dpll);
87442f73
DV
1418 POSTING_READ(reg);
1419 udelay(150); /* wait for warmup */
1420}
1421
66e3d5c0 1422static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1423{
66e3d5c0
DV
1424 struct drm_device *dev = crtc->base.dev;
1425 struct drm_i915_private *dev_priv = dev->dev_private;
1426 int reg = DPLL(crtc->pipe);
1427 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1428
66e3d5c0 1429 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1430
63d7bbe9 1431 /* No really, not for ILK+ */
87442f73 1432 BUG_ON(dev_priv->info->gen >= 5);
63d7bbe9
JB
1433
1434 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1435 if (IS_MOBILE(dev) && !IS_I830(dev))
1436 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1437
66e3d5c0
DV
1438 I915_WRITE(reg, dpll);
1439
1440 /* Wait for the clocks to stabilize. */
1441 POSTING_READ(reg);
1442 udelay(150);
1443
1444 if (INTEL_INFO(dev)->gen >= 4) {
1445 I915_WRITE(DPLL_MD(crtc->pipe),
1446 crtc->config.dpll_hw_state.dpll_md);
1447 } else {
1448 /* The pixel multiplier can only be updated once the
1449 * DPLL is enabled and the clocks are stable.
1450 *
1451 * So write it again.
1452 */
1453 I915_WRITE(reg, dpll);
1454 }
63d7bbe9
JB
1455
1456 /* We do this three times for luck */
66e3d5c0 1457 I915_WRITE(reg, dpll);
63d7bbe9
JB
1458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
66e3d5c0 1460 I915_WRITE(reg, dpll);
63d7bbe9
JB
1461 POSTING_READ(reg);
1462 udelay(150); /* wait for warmup */
66e3d5c0 1463 I915_WRITE(reg, dpll);
63d7bbe9
JB
1464 POSTING_READ(reg);
1465 udelay(150); /* wait for warmup */
1466}
1467
1468/**
50b44a44 1469 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1470 * @dev_priv: i915 private structure
1471 * @pipe: pipe PLL to disable
1472 *
1473 * Disable the PLL for @pipe, making sure the pipe is off first.
1474 *
1475 * Note! This is for pre-ILK only.
1476 */
50b44a44 1477static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1478{
63d7bbe9
JB
1479 /* Don't disable pipe A or pipe A PLLs if needed */
1480 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1481 return;
1482
1483 /* Make sure the pipe isn't still relying on us */
1484 assert_pipe_disabled(dev_priv, pipe);
1485
50b44a44
DV
1486 I915_WRITE(DPLL(pipe), 0);
1487 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1488}
1489
f6071166
JB
1490static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1491{
1492 u32 val = 0;
1493
1494 /* Make sure the pipe isn't still relying on us */
1495 assert_pipe_disabled(dev_priv, pipe);
1496
1497 /* Leave integrated clock source enabled */
1498 if (pipe == PIPE_B)
1499 val = DPLL_INTEGRATED_CRI_CLK_VLV;
1500 I915_WRITE(DPLL(pipe), val);
1501 POSTING_READ(DPLL(pipe));
1502}
1503
e4607fcf
CML
1504void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1505 struct intel_digital_port *dport)
89b667f8
JB
1506{
1507 u32 port_mask;
1508
e4607fcf
CML
1509 switch (dport->port) {
1510 case PORT_B:
89b667f8 1511 port_mask = DPLL_PORTB_READY_MASK;
e4607fcf
CML
1512 break;
1513 case PORT_C:
89b667f8 1514 port_mask = DPLL_PORTC_READY_MASK;
e4607fcf
CML
1515 break;
1516 default:
1517 BUG();
1518 }
89b667f8
JB
1519
1520 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1521 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
be46ffd4 1522 port_name(dport->port), I915_READ(DPLL(0)));
89b667f8
JB
1523}
1524
92f2584a 1525/**
e72f9fbf 1526 * ironlake_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1527 * @dev_priv: i915 private structure
1528 * @pipe: pipe PLL to enable
1529 *
1530 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1531 * drives the transcoder clock.
1532 */
e2b78267 1533static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1534{
e2b78267
DV
1535 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1536 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1537
48da64a8 1538 /* PCH PLLs only available on ILK, SNB and IVB */
92f2584a 1539 BUG_ON(dev_priv->info->gen < 5);
87a875bb 1540 if (WARN_ON(pll == NULL))
48da64a8
CW
1541 return;
1542
1543 if (WARN_ON(pll->refcount == 0))
1544 return;
ee7b9f93 1545
46edb027
DV
1546 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1547 pll->name, pll->active, pll->on,
e2b78267 1548 crtc->base.base.id);
92f2584a 1549
cdbd2316
DV
1550 if (pll->active++) {
1551 WARN_ON(!pll->on);
e9d6944e 1552 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1553 return;
1554 }
f4a091c7 1555 WARN_ON(pll->on);
ee7b9f93 1556
46edb027 1557 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1558 pll->enable(dev_priv, pll);
ee7b9f93 1559 pll->on = true;
92f2584a
JB
1560}
1561
e2b78267 1562static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1563{
e2b78267
DV
1564 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1565 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1566
92f2584a
JB
1567 /* PCH only available on ILK+ */
1568 BUG_ON(dev_priv->info->gen < 5);
87a875bb 1569 if (WARN_ON(pll == NULL))
ee7b9f93 1570 return;
92f2584a 1571
48da64a8
CW
1572 if (WARN_ON(pll->refcount == 0))
1573 return;
7a419866 1574
46edb027
DV
1575 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1576 pll->name, pll->active, pll->on,
e2b78267 1577 crtc->base.base.id);
7a419866 1578
48da64a8 1579 if (WARN_ON(pll->active == 0)) {
e9d6944e 1580 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1581 return;
1582 }
1583
e9d6944e 1584 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1585 WARN_ON(!pll->on);
cdbd2316 1586 if (--pll->active)
7a419866 1587 return;
ee7b9f93 1588
46edb027 1589 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1590 pll->disable(dev_priv, pll);
ee7b9f93 1591 pll->on = false;
92f2584a
JB
1592}
1593
b8a4f404
PZ
1594static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1595 enum pipe pipe)
040484af 1596{
23670b32 1597 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1598 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1599 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1600 uint32_t reg, val, pipeconf_val;
040484af
JB
1601
1602 /* PCH only available on ILK+ */
1603 BUG_ON(dev_priv->info->gen < 5);
1604
1605 /* Make sure PCH DPLL is enabled */
e72f9fbf 1606 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1607 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1608
1609 /* FDI must be feeding us bits for PCH ports */
1610 assert_fdi_tx_enabled(dev_priv, pipe);
1611 assert_fdi_rx_enabled(dev_priv, pipe);
1612
23670b32
DV
1613 if (HAS_PCH_CPT(dev)) {
1614 /* Workaround: Set the timing override bit before enabling the
1615 * pch transcoder. */
1616 reg = TRANS_CHICKEN2(pipe);
1617 val = I915_READ(reg);
1618 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1619 I915_WRITE(reg, val);
59c859d6 1620 }
23670b32 1621
ab9412ba 1622 reg = PCH_TRANSCONF(pipe);
040484af 1623 val = I915_READ(reg);
5f7f726d 1624 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1625
1626 if (HAS_PCH_IBX(dev_priv->dev)) {
1627 /*
1628 * make the BPC in transcoder be consistent with
1629 * that in pipeconf reg.
1630 */
dfd07d72
DV
1631 val &= ~PIPECONF_BPC_MASK;
1632 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1633 }
5f7f726d
PZ
1634
1635 val &= ~TRANS_INTERLACE_MASK;
1636 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1637 if (HAS_PCH_IBX(dev_priv->dev) &&
1638 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1639 val |= TRANS_LEGACY_INTERLACED_ILK;
1640 else
1641 val |= TRANS_INTERLACED;
5f7f726d
PZ
1642 else
1643 val |= TRANS_PROGRESSIVE;
1644
040484af
JB
1645 I915_WRITE(reg, val | TRANS_ENABLE);
1646 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1647 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1648}
1649
8fb033d7 1650static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1651 enum transcoder cpu_transcoder)
040484af 1652{
8fb033d7 1653 u32 val, pipeconf_val;
8fb033d7
PZ
1654
1655 /* PCH only available on ILK+ */
1656 BUG_ON(dev_priv->info->gen < 5);
1657
8fb033d7 1658 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1659 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1660 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1661
223a6fdf
PZ
1662 /* Workaround: set timing override bit. */
1663 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1664 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1665 I915_WRITE(_TRANSA_CHICKEN2, val);
1666
25f3ef11 1667 val = TRANS_ENABLE;
937bb610 1668 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1669
9a76b1c6
PZ
1670 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1671 PIPECONF_INTERLACED_ILK)
a35f2679 1672 val |= TRANS_INTERLACED;
8fb033d7
PZ
1673 else
1674 val |= TRANS_PROGRESSIVE;
1675
ab9412ba
DV
1676 I915_WRITE(LPT_TRANSCONF, val);
1677 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1678 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1679}
1680
b8a4f404
PZ
1681static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1682 enum pipe pipe)
040484af 1683{
23670b32
DV
1684 struct drm_device *dev = dev_priv->dev;
1685 uint32_t reg, val;
040484af
JB
1686
1687 /* FDI relies on the transcoder */
1688 assert_fdi_tx_disabled(dev_priv, pipe);
1689 assert_fdi_rx_disabled(dev_priv, pipe);
1690
291906f1
JB
1691 /* Ports must be off as well */
1692 assert_pch_ports_disabled(dev_priv, pipe);
1693
ab9412ba 1694 reg = PCH_TRANSCONF(pipe);
040484af
JB
1695 val = I915_READ(reg);
1696 val &= ~TRANS_ENABLE;
1697 I915_WRITE(reg, val);
1698 /* wait for PCH transcoder off, transcoder state */
1699 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1700 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1701
1702 if (!HAS_PCH_IBX(dev)) {
1703 /* Workaround: Clear the timing override chicken bit again. */
1704 reg = TRANS_CHICKEN2(pipe);
1705 val = I915_READ(reg);
1706 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1707 I915_WRITE(reg, val);
1708 }
040484af
JB
1709}
1710
ab4d966c 1711static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1712{
8fb033d7
PZ
1713 u32 val;
1714
ab9412ba 1715 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1716 val &= ~TRANS_ENABLE;
ab9412ba 1717 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1718 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1719 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1720 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1721
1722 /* Workaround: clear timing override bit. */
1723 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1724 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1725 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1726}
1727
b24e7179 1728/**
309cfea8 1729 * intel_enable_pipe - enable a pipe, asserting requirements
b24e7179
JB
1730 * @dev_priv: i915 private structure
1731 * @pipe: pipe to enable
040484af 1732 * @pch_port: on ILK+, is this pipe driving a PCH port or not
b24e7179
JB
1733 *
1734 * Enable @pipe, making sure that various hardware specific requirements
1735 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1736 *
1737 * @pipe should be %PIPE_A or %PIPE_B.
1738 *
1739 * Will wait until the pipe is actually running (i.e. first vblank) before
1740 * returning.
1741 */
040484af 1742static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
23538ef1 1743 bool pch_port, bool dsi)
b24e7179 1744{
702e7a56
PZ
1745 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1746 pipe);
1a240d4d 1747 enum pipe pch_transcoder;
b24e7179
JB
1748 int reg;
1749 u32 val;
1750
58c6eaa2 1751 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1752 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1753 assert_sprites_disabled(dev_priv, pipe);
1754
681e5811 1755 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1756 pch_transcoder = TRANSCODER_A;
1757 else
1758 pch_transcoder = pipe;
1759
b24e7179
JB
1760 /*
1761 * A pipe without a PLL won't actually be able to drive bits from
1762 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1763 * need the check.
1764 */
1765 if (!HAS_PCH_SPLIT(dev_priv->dev))
23538ef1
JN
1766 if (dsi)
1767 assert_dsi_pll_enabled(dev_priv);
1768 else
1769 assert_pll_enabled(dev_priv, pipe);
040484af
JB
1770 else {
1771 if (pch_port) {
1772 /* if driving the PCH, we need FDI enabled */
cc391bbb 1773 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1774 assert_fdi_tx_pll_enabled(dev_priv,
1775 (enum pipe) cpu_transcoder);
040484af
JB
1776 }
1777 /* FIXME: assert CPU port conditions for SNB+ */
1778 }
b24e7179 1779
702e7a56 1780 reg = PIPECONF(cpu_transcoder);
b24e7179 1781 val = I915_READ(reg);
00d70b15
CW
1782 if (val & PIPECONF_ENABLE)
1783 return;
1784
1785 I915_WRITE(reg, val | PIPECONF_ENABLE);
b24e7179
JB
1786 intel_wait_for_vblank(dev_priv->dev, pipe);
1787}
1788
1789/**
309cfea8 1790 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
1791 * @dev_priv: i915 private structure
1792 * @pipe: pipe to disable
1793 *
1794 * Disable @pipe, making sure that various hardware specific requirements
1795 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1796 *
1797 * @pipe should be %PIPE_A or %PIPE_B.
1798 *
1799 * Will wait until the pipe has shut down before returning.
1800 */
1801static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1802 enum pipe pipe)
1803{
702e7a56
PZ
1804 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1805 pipe);
b24e7179
JB
1806 int reg;
1807 u32 val;
1808
1809 /*
1810 * Make sure planes won't keep trying to pump pixels to us,
1811 * or we might hang the display.
1812 */
1813 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1814 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1815 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
1816
1817 /* Don't disable pipe A or pipe A PLLs if needed */
1818 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1819 return;
1820
702e7a56 1821 reg = PIPECONF(cpu_transcoder);
b24e7179 1822 val = I915_READ(reg);
00d70b15
CW
1823 if ((val & PIPECONF_ENABLE) == 0)
1824 return;
1825
1826 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
1827 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1828}
1829
d74362c9
KP
1830/*
1831 * Plane regs are double buffered, going from enabled->disabled needs a
1832 * trigger in order to latch. The display address reg provides this.
1833 */
1dba99f4
VS
1834void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1835 enum plane plane)
d74362c9 1836{
1dba99f4
VS
1837 u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1838
1839 I915_WRITE(reg, I915_READ(reg));
1840 POSTING_READ(reg);
d74362c9
KP
1841}
1842
b24e7179 1843/**
d1de00ef 1844 * intel_enable_primary_plane - enable the primary plane on a given pipe
b24e7179
JB
1845 * @dev_priv: i915 private structure
1846 * @plane: plane to enable
1847 * @pipe: pipe being fed
1848 *
1849 * Enable @plane on @pipe, making sure that @pipe is running first.
1850 */
d1de00ef
VS
1851static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1852 enum plane plane, enum pipe pipe)
b24e7179 1853{
939c2fe8
VS
1854 struct intel_crtc *intel_crtc =
1855 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
1856 int reg;
1857 u32 val;
1858
1859 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1860 assert_pipe_enabled(dev_priv, pipe);
1861
4c445e0e 1862 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
0037f71c 1863
4c445e0e 1864 intel_crtc->primary_enabled = true;
939c2fe8 1865
b24e7179
JB
1866 reg = DSPCNTR(plane);
1867 val = I915_READ(reg);
00d70b15
CW
1868 if (val & DISPLAY_PLANE_ENABLE)
1869 return;
1870
1871 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1dba99f4 1872 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
1873 intel_wait_for_vblank(dev_priv->dev, pipe);
1874}
1875
b24e7179 1876/**
d1de00ef 1877 * intel_disable_primary_plane - disable the primary plane
b24e7179
JB
1878 * @dev_priv: i915 private structure
1879 * @plane: plane to disable
1880 * @pipe: pipe consuming the data
1881 *
1882 * Disable @plane; should be an independent operation.
1883 */
d1de00ef
VS
1884static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1885 enum plane plane, enum pipe pipe)
b24e7179 1886{
939c2fe8
VS
1887 struct intel_crtc *intel_crtc =
1888 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
1889 int reg;
1890 u32 val;
1891
4c445e0e 1892 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
0037f71c 1893
4c445e0e 1894 intel_crtc->primary_enabled = false;
939c2fe8 1895
b24e7179
JB
1896 reg = DSPCNTR(plane);
1897 val = I915_READ(reg);
00d70b15
CW
1898 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1899 return;
1900
1901 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1dba99f4 1902 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
1903 intel_wait_for_vblank(dev_priv->dev, pipe);
1904}
1905
693db184
CW
1906static bool need_vtd_wa(struct drm_device *dev)
1907{
1908#ifdef CONFIG_INTEL_IOMMU
1909 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1910 return true;
1911#endif
1912 return false;
1913}
1914
127bd2ac 1915int
48b956c5 1916intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 1917 struct drm_i915_gem_object *obj,
919926ae 1918 struct intel_ring_buffer *pipelined)
6b95a207 1919{
ce453d81 1920 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
1921 u32 alignment;
1922 int ret;
1923
05394f39 1924 switch (obj->tiling_mode) {
6b95a207 1925 case I915_TILING_NONE:
534843da
CW
1926 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1927 alignment = 128 * 1024;
a6c45cf0 1928 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
1929 alignment = 4 * 1024;
1930 else
1931 alignment = 64 * 1024;
6b95a207
KH
1932 break;
1933 case I915_TILING_X:
1934 /* pin() will align the object as required by fence */
1935 alignment = 0;
1936 break;
1937 case I915_TILING_Y:
80075d49 1938 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
1939 return -EINVAL;
1940 default:
1941 BUG();
1942 }
1943
693db184
CW
1944 /* Note that the w/a also requires 64 PTE of padding following the
1945 * bo. We currently fill all unused PTE with the shadow page and so
1946 * we should always have valid PTE following the scanout preventing
1947 * the VT-d warning.
1948 */
1949 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1950 alignment = 256 * 1024;
1951
ce453d81 1952 dev_priv->mm.interruptible = false;
2da3b9b9 1953 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 1954 if (ret)
ce453d81 1955 goto err_interruptible;
6b95a207
KH
1956
1957 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1958 * fence, whereas 965+ only requires a fence if using
1959 * framebuffer compression. For simplicity, we always install
1960 * a fence as the cost is not that onerous.
1961 */
06d98131 1962 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
1963 if (ret)
1964 goto err_unpin;
1690e1eb 1965
9a5a53b3 1966 i915_gem_object_pin_fence(obj);
6b95a207 1967
ce453d81 1968 dev_priv->mm.interruptible = true;
6b95a207 1969 return 0;
48b956c5
CW
1970
1971err_unpin:
cc98b413 1972 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
1973err_interruptible:
1974 dev_priv->mm.interruptible = true;
48b956c5 1975 return ret;
6b95a207
KH
1976}
1977
1690e1eb
CW
1978void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1979{
1980 i915_gem_object_unpin_fence(obj);
cc98b413 1981 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
1982}
1983
c2c75131
DV
1984/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1985 * is assumed to be a power-of-two. */
bc752862
CW
1986unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1987 unsigned int tiling_mode,
1988 unsigned int cpp,
1989 unsigned int pitch)
c2c75131 1990{
bc752862
CW
1991 if (tiling_mode != I915_TILING_NONE) {
1992 unsigned int tile_rows, tiles;
c2c75131 1993
bc752862
CW
1994 tile_rows = *y / 8;
1995 *y %= 8;
c2c75131 1996
bc752862
CW
1997 tiles = *x / (512/cpp);
1998 *x %= 512/cpp;
1999
2000 return tile_rows * pitch * 8 + tiles * 4096;
2001 } else {
2002 unsigned int offset;
2003
2004 offset = *y * pitch + *x * cpp;
2005 *y = 0;
2006 *x = (offset & 4095) / cpp;
2007 return offset & -4096;
2008 }
c2c75131
DV
2009}
2010
17638cd6
JB
2011static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2012 int x, int y)
81255565
JB
2013{
2014 struct drm_device *dev = crtc->dev;
2015 struct drm_i915_private *dev_priv = dev->dev_private;
2016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2017 struct intel_framebuffer *intel_fb;
05394f39 2018 struct drm_i915_gem_object *obj;
81255565 2019 int plane = intel_crtc->plane;
e506a0c6 2020 unsigned long linear_offset;
81255565 2021 u32 dspcntr;
5eddb70b 2022 u32 reg;
81255565
JB
2023
2024 switch (plane) {
2025 case 0:
2026 case 1:
2027 break;
2028 default:
84f44ce7 2029 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
81255565
JB
2030 return -EINVAL;
2031 }
2032
2033 intel_fb = to_intel_framebuffer(fb);
2034 obj = intel_fb->obj;
81255565 2035
5eddb70b
CW
2036 reg = DSPCNTR(plane);
2037 dspcntr = I915_READ(reg);
81255565
JB
2038 /* Mask out pixel format bits in case we change it */
2039 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2040 switch (fb->pixel_format) {
2041 case DRM_FORMAT_C8:
81255565
JB
2042 dspcntr |= DISPPLANE_8BPP;
2043 break;
57779d06
VS
2044 case DRM_FORMAT_XRGB1555:
2045 case DRM_FORMAT_ARGB1555:
2046 dspcntr |= DISPPLANE_BGRX555;
81255565 2047 break;
57779d06
VS
2048 case DRM_FORMAT_RGB565:
2049 dspcntr |= DISPPLANE_BGRX565;
2050 break;
2051 case DRM_FORMAT_XRGB8888:
2052 case DRM_FORMAT_ARGB8888:
2053 dspcntr |= DISPPLANE_BGRX888;
2054 break;
2055 case DRM_FORMAT_XBGR8888:
2056 case DRM_FORMAT_ABGR8888:
2057 dspcntr |= DISPPLANE_RGBX888;
2058 break;
2059 case DRM_FORMAT_XRGB2101010:
2060 case DRM_FORMAT_ARGB2101010:
2061 dspcntr |= DISPPLANE_BGRX101010;
2062 break;
2063 case DRM_FORMAT_XBGR2101010:
2064 case DRM_FORMAT_ABGR2101010:
2065 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2066 break;
2067 default:
baba133a 2068 BUG();
81255565 2069 }
57779d06 2070
a6c45cf0 2071 if (INTEL_INFO(dev)->gen >= 4) {
05394f39 2072 if (obj->tiling_mode != I915_TILING_NONE)
81255565
JB
2073 dspcntr |= DISPPLANE_TILED;
2074 else
2075 dspcntr &= ~DISPPLANE_TILED;
2076 }
2077
de1aa629
VS
2078 if (IS_G4X(dev))
2079 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2080
5eddb70b 2081 I915_WRITE(reg, dspcntr);
81255565 2082
e506a0c6 2083 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
81255565 2084
c2c75131
DV
2085 if (INTEL_INFO(dev)->gen >= 4) {
2086 intel_crtc->dspaddr_offset =
bc752862
CW
2087 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2088 fb->bits_per_pixel / 8,
2089 fb->pitches[0]);
c2c75131
DV
2090 linear_offset -= intel_crtc->dspaddr_offset;
2091 } else {
e506a0c6 2092 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2093 }
e506a0c6 2094
f343c5f6
BW
2095 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2096 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2097 fb->pitches[0]);
01f2c773 2098 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2099 if (INTEL_INFO(dev)->gen >= 4) {
c2c75131 2100 I915_MODIFY_DISPBASE(DSPSURF(plane),
f343c5f6 2101 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2102 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2103 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2104 } else
f343c5f6 2105 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2106 POSTING_READ(reg);
81255565 2107
17638cd6
JB
2108 return 0;
2109}
2110
2111static int ironlake_update_plane(struct drm_crtc *crtc,
2112 struct drm_framebuffer *fb, int x, int y)
2113{
2114 struct drm_device *dev = crtc->dev;
2115 struct drm_i915_private *dev_priv = dev->dev_private;
2116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2117 struct intel_framebuffer *intel_fb;
2118 struct drm_i915_gem_object *obj;
2119 int plane = intel_crtc->plane;
e506a0c6 2120 unsigned long linear_offset;
17638cd6
JB
2121 u32 dspcntr;
2122 u32 reg;
2123
2124 switch (plane) {
2125 case 0:
2126 case 1:
27f8227b 2127 case 2:
17638cd6
JB
2128 break;
2129 default:
84f44ce7 2130 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
17638cd6
JB
2131 return -EINVAL;
2132 }
2133
2134 intel_fb = to_intel_framebuffer(fb);
2135 obj = intel_fb->obj;
2136
2137 reg = DSPCNTR(plane);
2138 dspcntr = I915_READ(reg);
2139 /* Mask out pixel format bits in case we change it */
2140 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2141 switch (fb->pixel_format) {
2142 case DRM_FORMAT_C8:
17638cd6
JB
2143 dspcntr |= DISPPLANE_8BPP;
2144 break;
57779d06
VS
2145 case DRM_FORMAT_RGB565:
2146 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2147 break;
57779d06
VS
2148 case DRM_FORMAT_XRGB8888:
2149 case DRM_FORMAT_ARGB8888:
2150 dspcntr |= DISPPLANE_BGRX888;
2151 break;
2152 case DRM_FORMAT_XBGR8888:
2153 case DRM_FORMAT_ABGR8888:
2154 dspcntr |= DISPPLANE_RGBX888;
2155 break;
2156 case DRM_FORMAT_XRGB2101010:
2157 case DRM_FORMAT_ARGB2101010:
2158 dspcntr |= DISPPLANE_BGRX101010;
2159 break;
2160 case DRM_FORMAT_XBGR2101010:
2161 case DRM_FORMAT_ABGR2101010:
2162 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2163 break;
2164 default:
baba133a 2165 BUG();
17638cd6
JB
2166 }
2167
2168 if (obj->tiling_mode != I915_TILING_NONE)
2169 dspcntr |= DISPPLANE_TILED;
2170 else
2171 dspcntr &= ~DISPPLANE_TILED;
2172
b42c6009 2173 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1f5d76db
PZ
2174 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2175 else
2176 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6
JB
2177
2178 I915_WRITE(reg, dspcntr);
2179
e506a0c6 2180 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
c2c75131 2181 intel_crtc->dspaddr_offset =
bc752862
CW
2182 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2183 fb->bits_per_pixel / 8,
2184 fb->pitches[0]);
c2c75131 2185 linear_offset -= intel_crtc->dspaddr_offset;
17638cd6 2186
f343c5f6
BW
2187 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2188 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2189 fb->pitches[0]);
01f2c773 2190 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
c2c75131 2191 I915_MODIFY_DISPBASE(DSPSURF(plane),
f343c5f6 2192 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2193 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2194 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2195 } else {
2196 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2197 I915_WRITE(DSPLINOFF(plane), linear_offset);
2198 }
17638cd6
JB
2199 POSTING_READ(reg);
2200
2201 return 0;
2202}
2203
2204/* Assume fb object is pinned & idle & fenced and just update base pointers */
2205static int
2206intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2207 int x, int y, enum mode_set_atomic state)
2208{
2209 struct drm_device *dev = crtc->dev;
2210 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2211
6b8e6ed0
CW
2212 if (dev_priv->display.disable_fbc)
2213 dev_priv->display.disable_fbc(dev);
3dec0095 2214 intel_increase_pllclock(crtc);
81255565 2215
6b8e6ed0 2216 return dev_priv->display.update_plane(crtc, fb, x, y);
81255565
JB
2217}
2218
96a02917
VS
2219void intel_display_handle_reset(struct drm_device *dev)
2220{
2221 struct drm_i915_private *dev_priv = dev->dev_private;
2222 struct drm_crtc *crtc;
2223
2224 /*
2225 * Flips in the rings have been nuked by the reset,
2226 * so complete all pending flips so that user space
2227 * will get its events and not get stuck.
2228 *
2229 * Also update the base address of all primary
2230 * planes to the the last fb to make sure we're
2231 * showing the correct fb after a reset.
2232 *
2233 * Need to make two loops over the crtcs so that we
2234 * don't try to grab a crtc mutex before the
2235 * pending_flip_queue really got woken up.
2236 */
2237
2238 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2240 enum plane plane = intel_crtc->plane;
2241
2242 intel_prepare_page_flip(dev, plane);
2243 intel_finish_page_flip_plane(dev, plane);
2244 }
2245
2246 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2248
2249 mutex_lock(&crtc->mutex);
947fdaad
CW
2250 /*
2251 * FIXME: Once we have proper support for primary planes (and
2252 * disabling them without disabling the entire crtc) allow again
2253 * a NULL crtc->fb.
2254 */
2255 if (intel_crtc->active && crtc->fb)
96a02917
VS
2256 dev_priv->display.update_plane(crtc, crtc->fb,
2257 crtc->x, crtc->y);
2258 mutex_unlock(&crtc->mutex);
2259 }
2260}
2261
14667a4b
CW
2262static int
2263intel_finish_fb(struct drm_framebuffer *old_fb)
2264{
2265 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2266 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2267 bool was_interruptible = dev_priv->mm.interruptible;
2268 int ret;
2269
14667a4b
CW
2270 /* Big Hammer, we also need to ensure that any pending
2271 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2272 * current scanout is retired before unpinning the old
2273 * framebuffer.
2274 *
2275 * This should only fail upon a hung GPU, in which case we
2276 * can safely continue.
2277 */
2278 dev_priv->mm.interruptible = false;
2279 ret = i915_gem_object_finish_gpu(obj);
2280 dev_priv->mm.interruptible = was_interruptible;
2281
2282 return ret;
2283}
2284
198598d0
VS
2285static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2286{
2287 struct drm_device *dev = crtc->dev;
2288 struct drm_i915_master_private *master_priv;
2289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2290
2291 if (!dev->primary->master)
2292 return;
2293
2294 master_priv = dev->primary->master->driver_priv;
2295 if (!master_priv->sarea_priv)
2296 return;
2297
2298 switch (intel_crtc->pipe) {
2299 case 0:
2300 master_priv->sarea_priv->pipeA_x = x;
2301 master_priv->sarea_priv->pipeA_y = y;
2302 break;
2303 case 1:
2304 master_priv->sarea_priv->pipeB_x = x;
2305 master_priv->sarea_priv->pipeB_y = y;
2306 break;
2307 default:
2308 break;
2309 }
2310}
2311
5c3b82e2 2312static int
3c4fdcfb 2313intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2314 struct drm_framebuffer *fb)
79e53945
JB
2315{
2316 struct drm_device *dev = crtc->dev;
6b8e6ed0 2317 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
94352cf9 2319 struct drm_framebuffer *old_fb;
5c3b82e2 2320 int ret;
79e53945
JB
2321
2322 /* no fb bound */
94352cf9 2323 if (!fb) {
a5071c2f 2324 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2325 return 0;
2326 }
2327
7eb552ae 2328 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2329 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2330 plane_name(intel_crtc->plane),
2331 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2332 return -EINVAL;
79e53945
JB
2333 }
2334
5c3b82e2 2335 mutex_lock(&dev->struct_mutex);
265db958 2336 ret = intel_pin_and_fence_fb_obj(dev,
94352cf9 2337 to_intel_framebuffer(fb)->obj,
919926ae 2338 NULL);
5c3b82e2
CW
2339 if (ret != 0) {
2340 mutex_unlock(&dev->struct_mutex);
a5071c2f 2341 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2342 return ret;
2343 }
79e53945 2344
bb2043de
DL
2345 /*
2346 * Update pipe size and adjust fitter if needed: the reason for this is
2347 * that in compute_mode_changes we check the native mode (not the pfit
2348 * mode) to see if we can flip rather than do a full mode set. In the
2349 * fastboot case, we'll flip, but if we don't update the pipesrc and
2350 * pfit state, we'll end up with a big fb scanned out into the wrong
2351 * sized surface.
2352 *
2353 * To fix this properly, we need to hoist the checks up into
2354 * compute_mode_changes (or above), check the actual pfit state and
2355 * whether the platform allows pfit disable with pipe active, and only
2356 * then update the pipesrc and pfit state, even on the flip path.
2357 */
4d6a3e63 2358 if (i915_fastboot) {
d7bf63f2
DL
2359 const struct drm_display_mode *adjusted_mode =
2360 &intel_crtc->config.adjusted_mode;
2361
4d6a3e63 2362 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2363 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2364 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2365 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2368 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2369 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2370 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2371 }
2372 }
2373
94352cf9 2374 ret = dev_priv->display.update_plane(crtc, fb, x, y);
4e6cfefc 2375 if (ret) {
94352cf9 2376 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
5c3b82e2 2377 mutex_unlock(&dev->struct_mutex);
a5071c2f 2378 DRM_ERROR("failed to update base address\n");
4e6cfefc 2379 return ret;
79e53945 2380 }
3c4fdcfb 2381
94352cf9
DV
2382 old_fb = crtc->fb;
2383 crtc->fb = fb;
6c4c86f5
DV
2384 crtc->x = x;
2385 crtc->y = y;
94352cf9 2386
b7f1de28 2387 if (old_fb) {
d7697eea
DV
2388 if (intel_crtc->active && old_fb != fb)
2389 intel_wait_for_vblank(dev, intel_crtc->pipe);
1690e1eb 2390 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
b7f1de28 2391 }
652c393a 2392
6b8e6ed0 2393 intel_update_fbc(dev);
4906557e 2394 intel_edp_psr_update(dev);
5c3b82e2 2395 mutex_unlock(&dev->struct_mutex);
79e53945 2396
198598d0 2397 intel_crtc_update_sarea_pos(crtc, x, y);
5c3b82e2
CW
2398
2399 return 0;
79e53945
JB
2400}
2401
5e84e1a4
ZW
2402static void intel_fdi_normal_train(struct drm_crtc *crtc)
2403{
2404 struct drm_device *dev = crtc->dev;
2405 struct drm_i915_private *dev_priv = dev->dev_private;
2406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2407 int pipe = intel_crtc->pipe;
2408 u32 reg, temp;
2409
2410 /* enable normal train */
2411 reg = FDI_TX_CTL(pipe);
2412 temp = I915_READ(reg);
61e499bf 2413 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2414 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2415 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2416 } else {
2417 temp &= ~FDI_LINK_TRAIN_NONE;
2418 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2419 }
5e84e1a4
ZW
2420 I915_WRITE(reg, temp);
2421
2422 reg = FDI_RX_CTL(pipe);
2423 temp = I915_READ(reg);
2424 if (HAS_PCH_CPT(dev)) {
2425 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2426 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2427 } else {
2428 temp &= ~FDI_LINK_TRAIN_NONE;
2429 temp |= FDI_LINK_TRAIN_NONE;
2430 }
2431 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2432
2433 /* wait one idle pattern time */
2434 POSTING_READ(reg);
2435 udelay(1000);
357555c0
JB
2436
2437 /* IVB wants error correction enabled */
2438 if (IS_IVYBRIDGE(dev))
2439 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2440 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2441}
2442
1fbc0d78 2443static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2444{
1fbc0d78
DV
2445 return crtc->base.enabled && crtc->active &&
2446 crtc->config.has_pch_encoder;
1e833f40
DV
2447}
2448
01a415fd
DV
2449static void ivb_modeset_global_resources(struct drm_device *dev)
2450{
2451 struct drm_i915_private *dev_priv = dev->dev_private;
2452 struct intel_crtc *pipe_B_crtc =
2453 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2454 struct intel_crtc *pipe_C_crtc =
2455 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2456 uint32_t temp;
2457
1e833f40
DV
2458 /*
2459 * When everything is off disable fdi C so that we could enable fdi B
2460 * with all lanes. Note that we don't care about enabled pipes without
2461 * an enabled pch encoder.
2462 */
2463 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2464 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2465 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2466 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2467
2468 temp = I915_READ(SOUTH_CHICKEN1);
2469 temp &= ~FDI_BC_BIFURCATION_SELECT;
2470 DRM_DEBUG_KMS("disabling fdi C rx\n");
2471 I915_WRITE(SOUTH_CHICKEN1, temp);
2472 }
2473}
2474
8db9d77b
ZW
2475/* The FDI link training functions for ILK/Ibexpeak. */
2476static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2477{
2478 struct drm_device *dev = crtc->dev;
2479 struct drm_i915_private *dev_priv = dev->dev_private;
2480 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2481 int pipe = intel_crtc->pipe;
0fc932b8 2482 int plane = intel_crtc->plane;
5eddb70b 2483 u32 reg, temp, tries;
8db9d77b 2484
0fc932b8
JB
2485 /* FDI needs bits from pipe & plane first */
2486 assert_pipe_enabled(dev_priv, pipe);
2487 assert_plane_enabled(dev_priv, plane);
2488
e1a44743
AJ
2489 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2490 for train result */
5eddb70b
CW
2491 reg = FDI_RX_IMR(pipe);
2492 temp = I915_READ(reg);
e1a44743
AJ
2493 temp &= ~FDI_RX_SYMBOL_LOCK;
2494 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2495 I915_WRITE(reg, temp);
2496 I915_READ(reg);
e1a44743
AJ
2497 udelay(150);
2498
8db9d77b 2499 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2500 reg = FDI_TX_CTL(pipe);
2501 temp = I915_READ(reg);
627eb5a3
DV
2502 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2503 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2504 temp &= ~FDI_LINK_TRAIN_NONE;
2505 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2506 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2507
5eddb70b
CW
2508 reg = FDI_RX_CTL(pipe);
2509 temp = I915_READ(reg);
8db9d77b
ZW
2510 temp &= ~FDI_LINK_TRAIN_NONE;
2511 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2512 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2513
2514 POSTING_READ(reg);
8db9d77b
ZW
2515 udelay(150);
2516
5b2adf89 2517 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2518 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2519 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2520 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2521
5eddb70b 2522 reg = FDI_RX_IIR(pipe);
e1a44743 2523 for (tries = 0; tries < 5; tries++) {
5eddb70b 2524 temp = I915_READ(reg);
8db9d77b
ZW
2525 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2526
2527 if ((temp & FDI_RX_BIT_LOCK)) {
2528 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2529 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2530 break;
2531 }
8db9d77b 2532 }
e1a44743 2533 if (tries == 5)
5eddb70b 2534 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2535
2536 /* Train 2 */
5eddb70b
CW
2537 reg = FDI_TX_CTL(pipe);
2538 temp = I915_READ(reg);
8db9d77b
ZW
2539 temp &= ~FDI_LINK_TRAIN_NONE;
2540 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2541 I915_WRITE(reg, temp);
8db9d77b 2542
5eddb70b
CW
2543 reg = FDI_RX_CTL(pipe);
2544 temp = I915_READ(reg);
8db9d77b
ZW
2545 temp &= ~FDI_LINK_TRAIN_NONE;
2546 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2547 I915_WRITE(reg, temp);
8db9d77b 2548
5eddb70b
CW
2549 POSTING_READ(reg);
2550 udelay(150);
8db9d77b 2551
5eddb70b 2552 reg = FDI_RX_IIR(pipe);
e1a44743 2553 for (tries = 0; tries < 5; tries++) {
5eddb70b 2554 temp = I915_READ(reg);
8db9d77b
ZW
2555 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2556
2557 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2558 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2559 DRM_DEBUG_KMS("FDI train 2 done.\n");
2560 break;
2561 }
8db9d77b 2562 }
e1a44743 2563 if (tries == 5)
5eddb70b 2564 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2565
2566 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2567
8db9d77b
ZW
2568}
2569
0206e353 2570static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2571 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2572 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2573 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2574 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2575};
2576
2577/* The FDI link training functions for SNB/Cougarpoint. */
2578static void gen6_fdi_link_train(struct drm_crtc *crtc)
2579{
2580 struct drm_device *dev = crtc->dev;
2581 struct drm_i915_private *dev_priv = dev->dev_private;
2582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2583 int pipe = intel_crtc->pipe;
fa37d39e 2584 u32 reg, temp, i, retry;
8db9d77b 2585
e1a44743
AJ
2586 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2587 for train result */
5eddb70b
CW
2588 reg = FDI_RX_IMR(pipe);
2589 temp = I915_READ(reg);
e1a44743
AJ
2590 temp &= ~FDI_RX_SYMBOL_LOCK;
2591 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2592 I915_WRITE(reg, temp);
2593
2594 POSTING_READ(reg);
e1a44743
AJ
2595 udelay(150);
2596
8db9d77b 2597 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2598 reg = FDI_TX_CTL(pipe);
2599 temp = I915_READ(reg);
627eb5a3
DV
2600 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2601 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2602 temp &= ~FDI_LINK_TRAIN_NONE;
2603 temp |= FDI_LINK_TRAIN_PATTERN_1;
2604 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2605 /* SNB-B */
2606 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 2607 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2608
d74cf324
DV
2609 I915_WRITE(FDI_RX_MISC(pipe),
2610 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2611
5eddb70b
CW
2612 reg = FDI_RX_CTL(pipe);
2613 temp = I915_READ(reg);
8db9d77b
ZW
2614 if (HAS_PCH_CPT(dev)) {
2615 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2616 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2617 } else {
2618 temp &= ~FDI_LINK_TRAIN_NONE;
2619 temp |= FDI_LINK_TRAIN_PATTERN_1;
2620 }
5eddb70b
CW
2621 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2622
2623 POSTING_READ(reg);
8db9d77b
ZW
2624 udelay(150);
2625
0206e353 2626 for (i = 0; i < 4; i++) {
5eddb70b
CW
2627 reg = FDI_TX_CTL(pipe);
2628 temp = I915_READ(reg);
8db9d77b
ZW
2629 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2630 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2631 I915_WRITE(reg, temp);
2632
2633 POSTING_READ(reg);
8db9d77b
ZW
2634 udelay(500);
2635
fa37d39e
SP
2636 for (retry = 0; retry < 5; retry++) {
2637 reg = FDI_RX_IIR(pipe);
2638 temp = I915_READ(reg);
2639 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2640 if (temp & FDI_RX_BIT_LOCK) {
2641 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2642 DRM_DEBUG_KMS("FDI train 1 done.\n");
2643 break;
2644 }
2645 udelay(50);
8db9d77b 2646 }
fa37d39e
SP
2647 if (retry < 5)
2648 break;
8db9d77b
ZW
2649 }
2650 if (i == 4)
5eddb70b 2651 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2652
2653 /* Train 2 */
5eddb70b
CW
2654 reg = FDI_TX_CTL(pipe);
2655 temp = I915_READ(reg);
8db9d77b
ZW
2656 temp &= ~FDI_LINK_TRAIN_NONE;
2657 temp |= FDI_LINK_TRAIN_PATTERN_2;
2658 if (IS_GEN6(dev)) {
2659 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2660 /* SNB-B */
2661 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2662 }
5eddb70b 2663 I915_WRITE(reg, temp);
8db9d77b 2664
5eddb70b
CW
2665 reg = FDI_RX_CTL(pipe);
2666 temp = I915_READ(reg);
8db9d77b
ZW
2667 if (HAS_PCH_CPT(dev)) {
2668 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2669 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2670 } else {
2671 temp &= ~FDI_LINK_TRAIN_NONE;
2672 temp |= FDI_LINK_TRAIN_PATTERN_2;
2673 }
5eddb70b
CW
2674 I915_WRITE(reg, temp);
2675
2676 POSTING_READ(reg);
8db9d77b
ZW
2677 udelay(150);
2678
0206e353 2679 for (i = 0; i < 4; i++) {
5eddb70b
CW
2680 reg = FDI_TX_CTL(pipe);
2681 temp = I915_READ(reg);
8db9d77b
ZW
2682 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2683 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2684 I915_WRITE(reg, temp);
2685
2686 POSTING_READ(reg);
8db9d77b
ZW
2687 udelay(500);
2688
fa37d39e
SP
2689 for (retry = 0; retry < 5; retry++) {
2690 reg = FDI_RX_IIR(pipe);
2691 temp = I915_READ(reg);
2692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2693 if (temp & FDI_RX_SYMBOL_LOCK) {
2694 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2695 DRM_DEBUG_KMS("FDI train 2 done.\n");
2696 break;
2697 }
2698 udelay(50);
8db9d77b 2699 }
fa37d39e
SP
2700 if (retry < 5)
2701 break;
8db9d77b
ZW
2702 }
2703 if (i == 4)
5eddb70b 2704 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2705
2706 DRM_DEBUG_KMS("FDI train done.\n");
2707}
2708
357555c0
JB
2709/* Manual link training for Ivy Bridge A0 parts */
2710static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2711{
2712 struct drm_device *dev = crtc->dev;
2713 struct drm_i915_private *dev_priv = dev->dev_private;
2714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2715 int pipe = intel_crtc->pipe;
139ccd3f 2716 u32 reg, temp, i, j;
357555c0
JB
2717
2718 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2719 for train result */
2720 reg = FDI_RX_IMR(pipe);
2721 temp = I915_READ(reg);
2722 temp &= ~FDI_RX_SYMBOL_LOCK;
2723 temp &= ~FDI_RX_BIT_LOCK;
2724 I915_WRITE(reg, temp);
2725
2726 POSTING_READ(reg);
2727 udelay(150);
2728
01a415fd
DV
2729 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2730 I915_READ(FDI_RX_IIR(pipe)));
2731
139ccd3f
JB
2732 /* Try each vswing and preemphasis setting twice before moving on */
2733 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2734 /* disable first in case we need to retry */
2735 reg = FDI_TX_CTL(pipe);
2736 temp = I915_READ(reg);
2737 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2738 temp &= ~FDI_TX_ENABLE;
2739 I915_WRITE(reg, temp);
357555c0 2740
139ccd3f
JB
2741 reg = FDI_RX_CTL(pipe);
2742 temp = I915_READ(reg);
2743 temp &= ~FDI_LINK_TRAIN_AUTO;
2744 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2745 temp &= ~FDI_RX_ENABLE;
2746 I915_WRITE(reg, temp);
357555c0 2747
139ccd3f 2748 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
2749 reg = FDI_TX_CTL(pipe);
2750 temp = I915_READ(reg);
139ccd3f
JB
2751 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2752 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2753 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 2754 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
2755 temp |= snb_b_fdi_train_param[j/2];
2756 temp |= FDI_COMPOSITE_SYNC;
2757 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 2758
139ccd3f
JB
2759 I915_WRITE(FDI_RX_MISC(pipe),
2760 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 2761
139ccd3f 2762 reg = FDI_RX_CTL(pipe);
357555c0 2763 temp = I915_READ(reg);
139ccd3f
JB
2764 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2765 temp |= FDI_COMPOSITE_SYNC;
2766 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 2767
139ccd3f
JB
2768 POSTING_READ(reg);
2769 udelay(1); /* should be 0.5us */
357555c0 2770
139ccd3f
JB
2771 for (i = 0; i < 4; i++) {
2772 reg = FDI_RX_IIR(pipe);
2773 temp = I915_READ(reg);
2774 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 2775
139ccd3f
JB
2776 if (temp & FDI_RX_BIT_LOCK ||
2777 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2778 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2779 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2780 i);
2781 break;
2782 }
2783 udelay(1); /* should be 0.5us */
2784 }
2785 if (i == 4) {
2786 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2787 continue;
2788 }
357555c0 2789
139ccd3f 2790 /* Train 2 */
357555c0
JB
2791 reg = FDI_TX_CTL(pipe);
2792 temp = I915_READ(reg);
139ccd3f
JB
2793 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2794 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2795 I915_WRITE(reg, temp);
2796
2797 reg = FDI_RX_CTL(pipe);
2798 temp = I915_READ(reg);
2799 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2800 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
2801 I915_WRITE(reg, temp);
2802
2803 POSTING_READ(reg);
139ccd3f 2804 udelay(2); /* should be 1.5us */
357555c0 2805
139ccd3f
JB
2806 for (i = 0; i < 4; i++) {
2807 reg = FDI_RX_IIR(pipe);
2808 temp = I915_READ(reg);
2809 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 2810
139ccd3f
JB
2811 if (temp & FDI_RX_SYMBOL_LOCK ||
2812 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2813 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2814 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2815 i);
2816 goto train_done;
2817 }
2818 udelay(2); /* should be 1.5us */
357555c0 2819 }
139ccd3f
JB
2820 if (i == 4)
2821 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 2822 }
357555c0 2823
139ccd3f 2824train_done:
357555c0
JB
2825 DRM_DEBUG_KMS("FDI train done.\n");
2826}
2827
88cefb6c 2828static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 2829{
88cefb6c 2830 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 2831 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 2832 int pipe = intel_crtc->pipe;
5eddb70b 2833 u32 reg, temp;
79e53945 2834
c64e311e 2835
c98e9dcf 2836 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
2837 reg = FDI_RX_CTL(pipe);
2838 temp = I915_READ(reg);
627eb5a3
DV
2839 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2840 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 2841 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
2842 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2843
2844 POSTING_READ(reg);
c98e9dcf
JB
2845 udelay(200);
2846
2847 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
2848 temp = I915_READ(reg);
2849 I915_WRITE(reg, temp | FDI_PCDCLK);
2850
2851 POSTING_READ(reg);
c98e9dcf
JB
2852 udelay(200);
2853
20749730
PZ
2854 /* Enable CPU FDI TX PLL, always on for Ironlake */
2855 reg = FDI_TX_CTL(pipe);
2856 temp = I915_READ(reg);
2857 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2858 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 2859
20749730
PZ
2860 POSTING_READ(reg);
2861 udelay(100);
6be4a607 2862 }
0e23b99d
JB
2863}
2864
88cefb6c
DV
2865static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2866{
2867 struct drm_device *dev = intel_crtc->base.dev;
2868 struct drm_i915_private *dev_priv = dev->dev_private;
2869 int pipe = intel_crtc->pipe;
2870 u32 reg, temp;
2871
2872 /* Switch from PCDclk to Rawclk */
2873 reg = FDI_RX_CTL(pipe);
2874 temp = I915_READ(reg);
2875 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2876
2877 /* Disable CPU FDI TX PLL */
2878 reg = FDI_TX_CTL(pipe);
2879 temp = I915_READ(reg);
2880 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2881
2882 POSTING_READ(reg);
2883 udelay(100);
2884
2885 reg = FDI_RX_CTL(pipe);
2886 temp = I915_READ(reg);
2887 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2888
2889 /* Wait for the clocks to turn off. */
2890 POSTING_READ(reg);
2891 udelay(100);
2892}
2893
0fc932b8
JB
2894static void ironlake_fdi_disable(struct drm_crtc *crtc)
2895{
2896 struct drm_device *dev = crtc->dev;
2897 struct drm_i915_private *dev_priv = dev->dev_private;
2898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2899 int pipe = intel_crtc->pipe;
2900 u32 reg, temp;
2901
2902 /* disable CPU FDI tx and PCH FDI rx */
2903 reg = FDI_TX_CTL(pipe);
2904 temp = I915_READ(reg);
2905 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2906 POSTING_READ(reg);
2907
2908 reg = FDI_RX_CTL(pipe);
2909 temp = I915_READ(reg);
2910 temp &= ~(0x7 << 16);
dfd07d72 2911 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
2912 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2913
2914 POSTING_READ(reg);
2915 udelay(100);
2916
2917 /* Ironlake workaround, disable clock pointer after downing FDI */
6f06ce18
JB
2918 if (HAS_PCH_IBX(dev)) {
2919 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
6f06ce18 2920 }
0fc932b8
JB
2921
2922 /* still set train pattern 1 */
2923 reg = FDI_TX_CTL(pipe);
2924 temp = I915_READ(reg);
2925 temp &= ~FDI_LINK_TRAIN_NONE;
2926 temp |= FDI_LINK_TRAIN_PATTERN_1;
2927 I915_WRITE(reg, temp);
2928
2929 reg = FDI_RX_CTL(pipe);
2930 temp = I915_READ(reg);
2931 if (HAS_PCH_CPT(dev)) {
2932 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2933 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2934 } else {
2935 temp &= ~FDI_LINK_TRAIN_NONE;
2936 temp |= FDI_LINK_TRAIN_PATTERN_1;
2937 }
2938 /* BPC in FDI rx is consistent with that in PIPECONF */
2939 temp &= ~(0x07 << 16);
dfd07d72 2940 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
2941 I915_WRITE(reg, temp);
2942
2943 POSTING_READ(reg);
2944 udelay(100);
2945}
2946
5bb61643
CW
2947static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2948{
2949 struct drm_device *dev = crtc->dev;
2950 struct drm_i915_private *dev_priv = dev->dev_private;
10d83730 2951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5bb61643
CW
2952 unsigned long flags;
2953 bool pending;
2954
10d83730
VS
2955 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2956 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
5bb61643
CW
2957 return false;
2958
2959 spin_lock_irqsave(&dev->event_lock, flags);
2960 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2961 spin_unlock_irqrestore(&dev->event_lock, flags);
2962
2963 return pending;
2964}
2965
e6c3a2a6
CW
2966static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2967{
0f91128d 2968 struct drm_device *dev = crtc->dev;
5bb61643 2969 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6
CW
2970
2971 if (crtc->fb == NULL)
2972 return;
2973
2c10d571
DV
2974 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2975
5bb61643
CW
2976 wait_event(dev_priv->pending_flip_queue,
2977 !intel_crtc_has_pending_flip(crtc));
2978
0f91128d
CW
2979 mutex_lock(&dev->struct_mutex);
2980 intel_finish_fb(crtc->fb);
2981 mutex_unlock(&dev->struct_mutex);
e6c3a2a6
CW
2982}
2983
e615efe4
ED
2984/* Program iCLKIP clock to the desired frequency */
2985static void lpt_program_iclkip(struct drm_crtc *crtc)
2986{
2987 struct drm_device *dev = crtc->dev;
2988 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 2989 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
2990 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2991 u32 temp;
2992
09153000
DV
2993 mutex_lock(&dev_priv->dpio_lock);
2994
e615efe4
ED
2995 /* It is necessary to ungate the pixclk gate prior to programming
2996 * the divisors, and gate it back when it is done.
2997 */
2998 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2999
3000 /* Disable SSCCTL */
3001 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3002 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3003 SBI_SSCCTL_DISABLE,
3004 SBI_ICLK);
e615efe4
ED
3005
3006 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3007 if (clock == 20000) {
e615efe4
ED
3008 auxdiv = 1;
3009 divsel = 0x41;
3010 phaseinc = 0x20;
3011 } else {
3012 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3013 * but the adjusted_mode->crtc_clock in in KHz. To get the
3014 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3015 * convert the virtual clock precision to KHz here for higher
3016 * precision.
3017 */
3018 u32 iclk_virtual_root_freq = 172800 * 1000;
3019 u32 iclk_pi_range = 64;
3020 u32 desired_divisor, msb_divisor_value, pi_value;
3021
12d7ceed 3022 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3023 msb_divisor_value = desired_divisor / iclk_pi_range;
3024 pi_value = desired_divisor % iclk_pi_range;
3025
3026 auxdiv = 0;
3027 divsel = msb_divisor_value - 2;
3028 phaseinc = pi_value;
3029 }
3030
3031 /* This should not happen with any sane values */
3032 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3033 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3034 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3035 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3036
3037 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3038 clock,
e615efe4
ED
3039 auxdiv,
3040 divsel,
3041 phasedir,
3042 phaseinc);
3043
3044 /* Program SSCDIVINTPHASE6 */
988d6ee8 3045 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3046 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3047 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3048 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3049 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3050 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3051 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3052 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3053
3054 /* Program SSCAUXDIV */
988d6ee8 3055 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3056 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3057 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3058 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3059
3060 /* Enable modulator and associated divider */
988d6ee8 3061 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3062 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3063 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3064
3065 /* Wait for initialization time */
3066 udelay(24);
3067
3068 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3069
3070 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3071}
3072
275f01b2
DV
3073static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3074 enum pipe pch_transcoder)
3075{
3076 struct drm_device *dev = crtc->base.dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3079
3080 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3081 I915_READ(HTOTAL(cpu_transcoder)));
3082 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3083 I915_READ(HBLANK(cpu_transcoder)));
3084 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3085 I915_READ(HSYNC(cpu_transcoder)));
3086
3087 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3088 I915_READ(VTOTAL(cpu_transcoder)));
3089 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3090 I915_READ(VBLANK(cpu_transcoder)));
3091 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3092 I915_READ(VSYNC(cpu_transcoder)));
3093 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3094 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3095}
3096
1fbc0d78
DV
3097static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3098{
3099 struct drm_i915_private *dev_priv = dev->dev_private;
3100 uint32_t temp;
3101
3102 temp = I915_READ(SOUTH_CHICKEN1);
3103 if (temp & FDI_BC_BIFURCATION_SELECT)
3104 return;
3105
3106 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3107 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3108
3109 temp |= FDI_BC_BIFURCATION_SELECT;
3110 DRM_DEBUG_KMS("enabling fdi C rx\n");
3111 I915_WRITE(SOUTH_CHICKEN1, temp);
3112 POSTING_READ(SOUTH_CHICKEN1);
3113}
3114
3115static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3116{
3117 struct drm_device *dev = intel_crtc->base.dev;
3118 struct drm_i915_private *dev_priv = dev->dev_private;
3119
3120 switch (intel_crtc->pipe) {
3121 case PIPE_A:
3122 break;
3123 case PIPE_B:
3124 if (intel_crtc->config.fdi_lanes > 2)
3125 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3126 else
3127 cpt_enable_fdi_bc_bifurcation(dev);
3128
3129 break;
3130 case PIPE_C:
3131 cpt_enable_fdi_bc_bifurcation(dev);
3132
3133 break;
3134 default:
3135 BUG();
3136 }
3137}
3138
f67a559d
JB
3139/*
3140 * Enable PCH resources required for PCH ports:
3141 * - PCH PLLs
3142 * - FDI training & RX/TX
3143 * - update transcoder timings
3144 * - DP transcoding bits
3145 * - transcoder
3146 */
3147static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3148{
3149 struct drm_device *dev = crtc->dev;
3150 struct drm_i915_private *dev_priv = dev->dev_private;
3151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3152 int pipe = intel_crtc->pipe;
ee7b9f93 3153 u32 reg, temp;
2c07245f 3154
ab9412ba 3155 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3156
1fbc0d78
DV
3157 if (IS_IVYBRIDGE(dev))
3158 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3159
cd986abb
DV
3160 /* Write the TU size bits before fdi link training, so that error
3161 * detection works. */
3162 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3163 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3164
c98e9dcf 3165 /* For PCH output, training FDI link */
674cf967 3166 dev_priv->display.fdi_link_train(crtc);
2c07245f 3167
3ad8a208
DV
3168 /* We need to program the right clock selection before writing the pixel
3169 * mutliplier into the DPLL. */
303b81e0 3170 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3171 u32 sel;
4b645f14 3172
c98e9dcf 3173 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3174 temp |= TRANS_DPLL_ENABLE(pipe);
3175 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3176 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3177 temp |= sel;
3178 else
3179 temp &= ~sel;
c98e9dcf 3180 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3181 }
5eddb70b 3182
3ad8a208
DV
3183 /* XXX: pch pll's can be enabled any time before we enable the PCH
3184 * transcoder, and we actually should do this to not upset any PCH
3185 * transcoder that already use the clock when we share it.
3186 *
3187 * Note that enable_shared_dpll tries to do the right thing, but
3188 * get_shared_dpll unconditionally resets the pll - we need that to have
3189 * the right LVDS enable sequence. */
3190 ironlake_enable_shared_dpll(intel_crtc);
3191
d9b6cb56
JB
3192 /* set transcoder timing, panel must allow it */
3193 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3194 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3195
303b81e0 3196 intel_fdi_normal_train(crtc);
5e84e1a4 3197
c98e9dcf
JB
3198 /* For PCH DP, enable TRANS_DP_CTL */
3199 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3200 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3201 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3202 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3203 reg = TRANS_DP_CTL(pipe);
3204 temp = I915_READ(reg);
3205 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3206 TRANS_DP_SYNC_MASK |
3207 TRANS_DP_BPC_MASK);
5eddb70b
CW
3208 temp |= (TRANS_DP_OUTPUT_ENABLE |
3209 TRANS_DP_ENH_FRAMING);
9325c9f0 3210 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3211
3212 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3213 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3214 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3215 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3216
3217 switch (intel_trans_dp_port_sel(crtc)) {
3218 case PCH_DP_B:
5eddb70b 3219 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3220 break;
3221 case PCH_DP_C:
5eddb70b 3222 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3223 break;
3224 case PCH_DP_D:
5eddb70b 3225 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3226 break;
3227 default:
e95d41e1 3228 BUG();
32f9d658 3229 }
2c07245f 3230
5eddb70b 3231 I915_WRITE(reg, temp);
6be4a607 3232 }
b52eb4dc 3233
b8a4f404 3234 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3235}
3236
1507e5bd
PZ
3237static void lpt_pch_enable(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
3240 struct drm_i915_private *dev_priv = dev->dev_private;
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3242 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3243
ab9412ba 3244 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3245
8c52b5e8 3246 lpt_program_iclkip(crtc);
1507e5bd 3247
0540e488 3248 /* Set transcoder timing. */
275f01b2 3249 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3250
937bb610 3251 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3252}
3253
e2b78267 3254static void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3255{
e2b78267 3256 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3257
3258 if (pll == NULL)
3259 return;
3260
3261 if (pll->refcount == 0) {
46edb027 3262 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3263 return;
3264 }
3265
f4a091c7
DV
3266 if (--pll->refcount == 0) {
3267 WARN_ON(pll->on);
3268 WARN_ON(pll->active);
3269 }
3270
a43f6e0f 3271 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3272}
3273
b89a1d39 3274static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3275{
e2b78267
DV
3276 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3277 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3278 enum intel_dpll_id i;
ee7b9f93 3279
ee7b9f93 3280 if (pll) {
46edb027
DV
3281 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3282 crtc->base.base.id, pll->name);
e2b78267 3283 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3284 }
3285
98b6bd99
DV
3286 if (HAS_PCH_IBX(dev_priv->dev)) {
3287 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3288 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3289 pll = &dev_priv->shared_dplls[i];
98b6bd99 3290
46edb027
DV
3291 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3292 crtc->base.base.id, pll->name);
98b6bd99
DV
3293
3294 goto found;
3295 }
3296
e72f9fbf
DV
3297 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3298 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3299
3300 /* Only want to check enabled timings first */
3301 if (pll->refcount == 0)
3302 continue;
3303
b89a1d39
DV
3304 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3305 sizeof(pll->hw_state)) == 0) {
46edb027 3306 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3307 crtc->base.base.id,
46edb027 3308 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3309
3310 goto found;
3311 }
3312 }
3313
3314 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3315 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3316 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3317 if (pll->refcount == 0) {
46edb027
DV
3318 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3319 crtc->base.base.id, pll->name);
ee7b9f93
JB
3320 goto found;
3321 }
3322 }
3323
3324 return NULL;
3325
3326found:
a43f6e0f 3327 crtc->config.shared_dpll = i;
46edb027
DV
3328 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3329 pipe_name(crtc->pipe));
ee7b9f93 3330
cdbd2316 3331 if (pll->active == 0) {
66e985c0
DV
3332 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3333 sizeof(pll->hw_state));
3334
46edb027 3335 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
cdbd2316 3336 WARN_ON(pll->on);
e9d6944e 3337 assert_shared_dpll_disabled(dev_priv, pll);
ee7b9f93 3338
15bdd4cf 3339 pll->mode_set(dev_priv, pll);
cdbd2316
DV
3340 }
3341 pll->refcount++;
e04c7350 3342
ee7b9f93
JB
3343 return pll;
3344}
3345
a1520318 3346static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3347{
3348 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3349 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3350 u32 temp;
3351
3352 temp = I915_READ(dslreg);
3353 udelay(500);
3354 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3355 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3356 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3357 }
3358}
3359
b074cec8
JB
3360static void ironlake_pfit_enable(struct intel_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->base.dev;
3363 struct drm_i915_private *dev_priv = dev->dev_private;
3364 int pipe = crtc->pipe;
3365
fd4daa9c 3366 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3367 /* Force use of hard-coded filter coefficients
3368 * as some pre-programmed values are broken,
3369 * e.g. x201.
3370 */
3371 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3372 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3373 PF_PIPE_SEL_IVB(pipe));
3374 else
3375 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3376 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3377 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3378 }
3379}
3380
bb53d4ae
VS
3381static void intel_enable_planes(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
3384 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3385 struct intel_plane *intel_plane;
3386
3387 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3388 if (intel_plane->pipe == pipe)
3389 intel_plane_restore(&intel_plane->base);
3390}
3391
3392static void intel_disable_planes(struct drm_crtc *crtc)
3393{
3394 struct drm_device *dev = crtc->dev;
3395 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3396 struct intel_plane *intel_plane;
3397
3398 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3399 if (intel_plane->pipe == pipe)
3400 intel_plane_disable(&intel_plane->base);
3401}
3402
20bc8673 3403void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3404{
3405 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3406
3407 if (!crtc->config.ips_enabled)
3408 return;
3409
3410 /* We can only enable IPS after we enable a plane and wait for a vblank.
3411 * We guarantee that the plane is enabled by calling intel_enable_ips
3412 * only after intel_enable_plane. And intel_enable_plane already waits
3413 * for a vblank, so all we need to do here is to enable the IPS bit. */
3414 assert_plane_enabled(dev_priv, crtc->plane);
2a114cc1
BW
3415 if (IS_BROADWELL(crtc->base.dev)) {
3416 mutex_lock(&dev_priv->rps.hw_lock);
3417 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3418 mutex_unlock(&dev_priv->rps.hw_lock);
3419 /* Quoting Art Runyan: "its not safe to expect any particular
3420 * value in IPS_CTL bit 31 after enabling IPS through the
3421 * mailbox." Therefore we need to defer waiting on the state
3422 * change.
3423 * TODO: need to fix this for state checker
3424 */
3425 } else {
3426 I915_WRITE(IPS_CTL, IPS_ENABLE);
3427 /* The bit only becomes 1 in the next vblank, so this wait here
3428 * is essentially intel_wait_for_vblank. If we don't have this
3429 * and don't wait for vblanks until the end of crtc_enable, then
3430 * the HW state readout code will complain that the expected
3431 * IPS_CTL value is not the one we read. */
3432 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3433 DRM_ERROR("Timed out waiting for IPS enable\n");
3434 }
d77e4531
PZ
3435}
3436
20bc8673 3437void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3438{
3439 struct drm_device *dev = crtc->base.dev;
3440 struct drm_i915_private *dev_priv = dev->dev_private;
3441
3442 if (!crtc->config.ips_enabled)
3443 return;
3444
3445 assert_plane_enabled(dev_priv, crtc->plane);
2a114cc1
BW
3446 if (IS_BROADWELL(crtc->base.dev)) {
3447 mutex_lock(&dev_priv->rps.hw_lock);
3448 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3449 mutex_unlock(&dev_priv->rps.hw_lock);
3450 } else
3451 I915_WRITE(IPS_CTL, 0);
d77e4531
PZ
3452 POSTING_READ(IPS_CTL);
3453
3454 /* We need to wait for a vblank before we can disable the plane. */
3455 intel_wait_for_vblank(dev, crtc->pipe);
3456}
3457
3458/** Loads the palette/gamma unit for the CRTC with the prepared values */
3459static void intel_crtc_load_lut(struct drm_crtc *crtc)
3460{
3461 struct drm_device *dev = crtc->dev;
3462 struct drm_i915_private *dev_priv = dev->dev_private;
3463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3464 enum pipe pipe = intel_crtc->pipe;
3465 int palreg = PALETTE(pipe);
3466 int i;
3467 bool reenable_ips = false;
3468
3469 /* The clocks have to be on to load the palette. */
3470 if (!crtc->enabled || !intel_crtc->active)
3471 return;
3472
3473 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3474 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3475 assert_dsi_pll_enabled(dev_priv);
3476 else
3477 assert_pll_enabled(dev_priv, pipe);
3478 }
3479
3480 /* use legacy palette for Ironlake */
3481 if (HAS_PCH_SPLIT(dev))
3482 palreg = LGC_PALETTE(pipe);
3483
3484 /* Workaround : Do not read or write the pipe palette/gamma data while
3485 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3486 */
3487 if (intel_crtc->config.ips_enabled &&
3488 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3489 GAMMA_MODE_MODE_SPLIT)) {
3490 hsw_disable_ips(intel_crtc);
3491 reenable_ips = true;
3492 }
3493
3494 for (i = 0; i < 256; i++) {
3495 I915_WRITE(palreg + 4 * i,
3496 (intel_crtc->lut_r[i] << 16) |
3497 (intel_crtc->lut_g[i] << 8) |
3498 intel_crtc->lut_b[i]);
3499 }
3500
3501 if (reenable_ips)
3502 hsw_enable_ips(intel_crtc);
3503}
3504
f67a559d
JB
3505static void ironlake_crtc_enable(struct drm_crtc *crtc)
3506{
3507 struct drm_device *dev = crtc->dev;
3508 struct drm_i915_private *dev_priv = dev->dev_private;
3509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3510 struct intel_encoder *encoder;
f67a559d
JB
3511 int pipe = intel_crtc->pipe;
3512 int plane = intel_crtc->plane;
f67a559d 3513
08a48469
DV
3514 WARN_ON(!crtc->enabled);
3515
f67a559d
JB
3516 if (intel_crtc->active)
3517 return;
3518
3519 intel_crtc->active = true;
8664281b
PZ
3520
3521 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3522 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3523
f6736a1a 3524 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
3525 if (encoder->pre_enable)
3526 encoder->pre_enable(encoder);
f67a559d 3527
5bfe2ac0 3528 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
3529 /* Note: FDI PLL enabling _must_ be done before we enable the
3530 * cpu pipes, hence this is separate from all the other fdi/pch
3531 * enabling. */
88cefb6c 3532 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
3533 } else {
3534 assert_fdi_tx_disabled(dev_priv, pipe);
3535 assert_fdi_rx_disabled(dev_priv, pipe);
3536 }
f67a559d 3537
b074cec8 3538 ironlake_pfit_enable(intel_crtc);
f67a559d 3539
9c54c0dd
JB
3540 /*
3541 * On ILK+ LUT must be loaded before the pipe is running but with
3542 * clocks enabled
3543 */
3544 intel_crtc_load_lut(crtc);
3545
f37fcc2a 3546 intel_update_watermarks(crtc);
5bfe2ac0 3547 intel_enable_pipe(dev_priv, pipe,
23538ef1 3548 intel_crtc->config.has_pch_encoder, false);
d1de00ef 3549 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 3550 intel_enable_planes(crtc);
5c38d48c 3551 intel_crtc_update_cursor(crtc, true);
f67a559d 3552
5bfe2ac0 3553 if (intel_crtc->config.has_pch_encoder)
f67a559d 3554 ironlake_pch_enable(crtc);
c98e9dcf 3555
d1ebd816 3556 mutex_lock(&dev->struct_mutex);
bed4a673 3557 intel_update_fbc(dev);
d1ebd816
BW
3558 mutex_unlock(&dev->struct_mutex);
3559
fa5c73b1
DV
3560 for_each_encoder_on_crtc(dev, crtc, encoder)
3561 encoder->enable(encoder);
61b77ddd
DV
3562
3563 if (HAS_PCH_CPT(dev))
a1520318 3564 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100
DV
3565
3566 /*
3567 * There seems to be a race in PCH platform hw (at least on some
3568 * outputs) where an enabled pipe still completes any pageflip right
3569 * away (as if the pipe is off) instead of waiting for vblank. As soon
3570 * as the first vblank happend, everything works as expected. Hence just
3571 * wait for one vblank before returning to avoid strange things
3572 * happening.
3573 */
3574 intel_wait_for_vblank(dev, intel_crtc->pipe);
6be4a607
JB
3575}
3576
42db64ef
PZ
3577/* IPS only exists on ULT machines and is tied to pipe A. */
3578static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3579{
f5adf94e 3580 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
3581}
3582
dda9a66a
VS
3583static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3584{
3585 struct drm_device *dev = crtc->dev;
3586 struct drm_i915_private *dev_priv = dev->dev_private;
3587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3588 int pipe = intel_crtc->pipe;
3589 int plane = intel_crtc->plane;
3590
d1de00ef 3591 intel_enable_primary_plane(dev_priv, plane, pipe);
dda9a66a
VS
3592 intel_enable_planes(crtc);
3593 intel_crtc_update_cursor(crtc, true);
3594
3595 hsw_enable_ips(intel_crtc);
3596
3597 mutex_lock(&dev->struct_mutex);
3598 intel_update_fbc(dev);
3599 mutex_unlock(&dev->struct_mutex);
3600}
3601
3602static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3603{
3604 struct drm_device *dev = crtc->dev;
3605 struct drm_i915_private *dev_priv = dev->dev_private;
3606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3607 int pipe = intel_crtc->pipe;
3608 int plane = intel_crtc->plane;
3609
3610 intel_crtc_wait_for_pending_flips(crtc);
3611 drm_vblank_off(dev, pipe);
3612
3613 /* FBC must be disabled before disabling the plane on HSW. */
3614 if (dev_priv->fbc.plane == plane)
3615 intel_disable_fbc(dev);
3616
3617 hsw_disable_ips(intel_crtc);
3618
3619 intel_crtc_update_cursor(crtc, false);
3620 intel_disable_planes(crtc);
d1de00ef 3621 intel_disable_primary_plane(dev_priv, plane, pipe);
dda9a66a
VS
3622}
3623
e4916946
PZ
3624/*
3625 * This implements the workaround described in the "notes" section of the mode
3626 * set sequence documentation. When going from no pipes or single pipe to
3627 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3628 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3629 */
3630static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3631{
3632 struct drm_device *dev = crtc->base.dev;
3633 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3634
3635 /* We want to get the other_active_crtc only if there's only 1 other
3636 * active crtc. */
3637 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3638 if (!crtc_it->active || crtc_it == crtc)
3639 continue;
3640
3641 if (other_active_crtc)
3642 return;
3643
3644 other_active_crtc = crtc_it;
3645 }
3646 if (!other_active_crtc)
3647 return;
3648
3649 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3650 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3651}
3652
4f771f10
PZ
3653static void haswell_crtc_enable(struct drm_crtc *crtc)
3654{
3655 struct drm_device *dev = crtc->dev;
3656 struct drm_i915_private *dev_priv = dev->dev_private;
3657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3658 struct intel_encoder *encoder;
3659 int pipe = intel_crtc->pipe;
4f771f10
PZ
3660
3661 WARN_ON(!crtc->enabled);
3662
3663 if (intel_crtc->active)
3664 return;
3665
3666 intel_crtc->active = true;
8664281b
PZ
3667
3668 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3669 if (intel_crtc->config.has_pch_encoder)
3670 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3671
5bfe2ac0 3672 if (intel_crtc->config.has_pch_encoder)
04945641 3673 dev_priv->display.fdi_link_train(crtc);
4f771f10
PZ
3674
3675 for_each_encoder_on_crtc(dev, crtc, encoder)
3676 if (encoder->pre_enable)
3677 encoder->pre_enable(encoder);
3678
1f544388 3679 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 3680
b074cec8 3681 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
3682
3683 /*
3684 * On ILK+ LUT must be loaded before the pipe is running but with
3685 * clocks enabled
3686 */
3687 intel_crtc_load_lut(crtc);
3688
1f544388 3689 intel_ddi_set_pipe_settings(crtc);
8228c251 3690 intel_ddi_enable_transcoder_func(crtc);
4f771f10 3691
f37fcc2a 3692 intel_update_watermarks(crtc);
5bfe2ac0 3693 intel_enable_pipe(dev_priv, pipe,
23538ef1 3694 intel_crtc->config.has_pch_encoder, false);
42db64ef 3695
5bfe2ac0 3696 if (intel_crtc->config.has_pch_encoder)
1507e5bd 3697 lpt_pch_enable(crtc);
4f771f10 3698
8807e55b 3699 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 3700 encoder->enable(encoder);
8807e55b
JN
3701 intel_opregion_notify_encoder(encoder, true);
3702 }
4f771f10 3703
e4916946
PZ
3704 /* If we change the relative order between pipe/planes enabling, we need
3705 * to change the workaround. */
3706 haswell_mode_set_planes_workaround(intel_crtc);
dda9a66a
VS
3707 haswell_crtc_enable_planes(crtc);
3708
4f771f10
PZ
3709 /*
3710 * There seems to be a race in PCH platform hw (at least on some
3711 * outputs) where an enabled pipe still completes any pageflip right
3712 * away (as if the pipe is off) instead of waiting for vblank. As soon
3713 * as the first vblank happend, everything works as expected. Hence just
3714 * wait for one vblank before returning to avoid strange things
3715 * happening.
3716 */
3717 intel_wait_for_vblank(dev, intel_crtc->pipe);
3718}
3719
3f8dce3a
DV
3720static void ironlake_pfit_disable(struct intel_crtc *crtc)
3721{
3722 struct drm_device *dev = crtc->base.dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 int pipe = crtc->pipe;
3725
3726 /* To avoid upsetting the power well on haswell only disable the pfit if
3727 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 3728 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
3729 I915_WRITE(PF_CTL(pipe), 0);
3730 I915_WRITE(PF_WIN_POS(pipe), 0);
3731 I915_WRITE(PF_WIN_SZ(pipe), 0);
3732 }
3733}
3734
6be4a607
JB
3735static void ironlake_crtc_disable(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3740 struct intel_encoder *encoder;
6be4a607
JB
3741 int pipe = intel_crtc->pipe;
3742 int plane = intel_crtc->plane;
5eddb70b 3743 u32 reg, temp;
b52eb4dc 3744
ef9c3aee 3745
f7abfe8b
CW
3746 if (!intel_crtc->active)
3747 return;
3748
ea9d758d
DV
3749 for_each_encoder_on_crtc(dev, crtc, encoder)
3750 encoder->disable(encoder);
3751
e6c3a2a6 3752 intel_crtc_wait_for_pending_flips(crtc);
6be4a607 3753 drm_vblank_off(dev, pipe);
913d8d11 3754
5c3fe8b0 3755 if (dev_priv->fbc.plane == plane)
973d04f9 3756 intel_disable_fbc(dev);
2c07245f 3757
0d5b8c61 3758 intel_crtc_update_cursor(crtc, false);
bb53d4ae 3759 intel_disable_planes(crtc);
d1de00ef 3760 intel_disable_primary_plane(dev_priv, plane, pipe);
0d5b8c61 3761
d925c59a
DV
3762 if (intel_crtc->config.has_pch_encoder)
3763 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3764
b24e7179 3765 intel_disable_pipe(dev_priv, pipe);
32f9d658 3766
3f8dce3a 3767 ironlake_pfit_disable(intel_crtc);
2c07245f 3768
bf49ec8c
DV
3769 for_each_encoder_on_crtc(dev, crtc, encoder)
3770 if (encoder->post_disable)
3771 encoder->post_disable(encoder);
2c07245f 3772
d925c59a
DV
3773 if (intel_crtc->config.has_pch_encoder) {
3774 ironlake_fdi_disable(crtc);
913d8d11 3775
d925c59a
DV
3776 ironlake_disable_pch_transcoder(dev_priv, pipe);
3777 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 3778
d925c59a
DV
3779 if (HAS_PCH_CPT(dev)) {
3780 /* disable TRANS_DP_CTL */
3781 reg = TRANS_DP_CTL(pipe);
3782 temp = I915_READ(reg);
3783 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3784 TRANS_DP_PORT_SEL_MASK);
3785 temp |= TRANS_DP_PORT_SEL_NONE;
3786 I915_WRITE(reg, temp);
3787
3788 /* disable DPLL_SEL */
3789 temp = I915_READ(PCH_DPLL_SEL);
11887397 3790 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 3791 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 3792 }
e3421a18 3793
d925c59a 3794 /* disable PCH DPLL */
e72f9fbf 3795 intel_disable_shared_dpll(intel_crtc);
8db9d77b 3796
d925c59a
DV
3797 ironlake_fdi_pll_disable(intel_crtc);
3798 }
6b383a7f 3799
f7abfe8b 3800 intel_crtc->active = false;
46ba614c 3801 intel_update_watermarks(crtc);
d1ebd816
BW
3802
3803 mutex_lock(&dev->struct_mutex);
6b383a7f 3804 intel_update_fbc(dev);
d1ebd816 3805 mutex_unlock(&dev->struct_mutex);
6be4a607 3806}
1b3c7a47 3807
4f771f10 3808static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 3809{
4f771f10
PZ
3810 struct drm_device *dev = crtc->dev;
3811 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 3812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
3813 struct intel_encoder *encoder;
3814 int pipe = intel_crtc->pipe;
3b117c8f 3815 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 3816
4f771f10
PZ
3817 if (!intel_crtc->active)
3818 return;
3819
dda9a66a
VS
3820 haswell_crtc_disable_planes(crtc);
3821
8807e55b
JN
3822 for_each_encoder_on_crtc(dev, crtc, encoder) {
3823 intel_opregion_notify_encoder(encoder, false);
4f771f10 3824 encoder->disable(encoder);
8807e55b 3825 }
4f771f10 3826
8664281b
PZ
3827 if (intel_crtc->config.has_pch_encoder)
3828 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
3829 intel_disable_pipe(dev_priv, pipe);
3830
ad80a810 3831 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 3832
3f8dce3a 3833 ironlake_pfit_disable(intel_crtc);
4f771f10 3834
1f544388 3835 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10
PZ
3836
3837 for_each_encoder_on_crtc(dev, crtc, encoder)
3838 if (encoder->post_disable)
3839 encoder->post_disable(encoder);
3840
88adfff1 3841 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 3842 lpt_disable_pch_transcoder(dev_priv);
8664281b 3843 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 3844 intel_ddi_fdi_disable(crtc);
83616634 3845 }
4f771f10
PZ
3846
3847 intel_crtc->active = false;
46ba614c 3848 intel_update_watermarks(crtc);
4f771f10
PZ
3849
3850 mutex_lock(&dev->struct_mutex);
3851 intel_update_fbc(dev);
3852 mutex_unlock(&dev->struct_mutex);
3853}
3854
ee7b9f93
JB
3855static void ironlake_crtc_off(struct drm_crtc *crtc)
3856{
3857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 3858 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
3859}
3860
6441ab5f
PZ
3861static void haswell_crtc_off(struct drm_crtc *crtc)
3862{
3863 intel_ddi_put_crtc_pll(crtc);
3864}
3865
02e792fb
DV
3866static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3867{
02e792fb 3868 if (!enable && intel_crtc->overlay) {
23f09ce3 3869 struct drm_device *dev = intel_crtc->base.dev;
ce453d81 3870 struct drm_i915_private *dev_priv = dev->dev_private;
03f77ea5 3871
23f09ce3 3872 mutex_lock(&dev->struct_mutex);
ce453d81
CW
3873 dev_priv->mm.interruptible = false;
3874 (void) intel_overlay_switch_off(intel_crtc->overlay);
3875 dev_priv->mm.interruptible = true;
23f09ce3 3876 mutex_unlock(&dev->struct_mutex);
02e792fb 3877 }
02e792fb 3878
5dcdbcb0
CW
3879 /* Let userspace switch the overlay on again. In most cases userspace
3880 * has to recompute where to put it anyway.
3881 */
02e792fb
DV
3882}
3883
61bc95c1
EE
3884/**
3885 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3886 * cursor plane briefly if not already running after enabling the display
3887 * plane.
3888 * This workaround avoids occasional blank screens when self refresh is
3889 * enabled.
3890 */
3891static void
3892g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3893{
3894 u32 cntl = I915_READ(CURCNTR(pipe));
3895
3896 if ((cntl & CURSOR_MODE) == 0) {
3897 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3898
3899 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3900 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3901 intel_wait_for_vblank(dev_priv->dev, pipe);
3902 I915_WRITE(CURCNTR(pipe), cntl);
3903 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3904 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3905 }
3906}
3907
2dd24552
JB
3908static void i9xx_pfit_enable(struct intel_crtc *crtc)
3909{
3910 struct drm_device *dev = crtc->base.dev;
3911 struct drm_i915_private *dev_priv = dev->dev_private;
3912 struct intel_crtc_config *pipe_config = &crtc->config;
3913
328d8e82 3914 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
3915 return;
3916
2dd24552 3917 /*
c0b03411
DV
3918 * The panel fitter should only be adjusted whilst the pipe is disabled,
3919 * according to register description and PRM.
2dd24552 3920 */
c0b03411
DV
3921 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3922 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 3923
b074cec8
JB
3924 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3925 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
3926
3927 /* Border color in case we don't scale up to the full screen. Black by
3928 * default, change to something else for debugging. */
3929 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
3930}
3931
586f49dc 3932int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 3933{
586f49dc 3934 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 3935
586f49dc
JB
3936 /* Obtain SKU information */
3937 mutex_lock(&dev_priv->dpio_lock);
3938 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
3939 CCK_FUSE_HPLL_FREQ_MASK;
3940 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 3941
586f49dc 3942 return vco_freq[hpll_freq];
30a970c6
JB
3943}
3944
3945/* Adjust CDclk dividers to allow high res or save power if possible */
3946static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
3947{
3948 struct drm_i915_private *dev_priv = dev->dev_private;
3949 u32 val, cmd;
3950
3951 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
3952 cmd = 2;
3953 else if (cdclk == 266)
3954 cmd = 1;
3955 else
3956 cmd = 0;
3957
3958 mutex_lock(&dev_priv->rps.hw_lock);
3959 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3960 val &= ~DSPFREQGUAR_MASK;
3961 val |= (cmd << DSPFREQGUAR_SHIFT);
3962 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
3963 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
3964 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
3965 50)) {
3966 DRM_ERROR("timed out waiting for CDclk change\n");
3967 }
3968 mutex_unlock(&dev_priv->rps.hw_lock);
3969
3970 if (cdclk == 400) {
3971 u32 divider, vco;
3972
3973 vco = valleyview_get_vco(dev_priv);
3974 divider = ((vco << 1) / cdclk) - 1;
3975
3976 mutex_lock(&dev_priv->dpio_lock);
3977 /* adjust cdclk divider */
3978 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
3979 val &= ~0xf;
3980 val |= divider;
3981 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
3982 mutex_unlock(&dev_priv->dpio_lock);
3983 }
3984
3985 mutex_lock(&dev_priv->dpio_lock);
3986 /* adjust self-refresh exit latency value */
3987 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
3988 val &= ~0x7f;
3989
3990 /*
3991 * For high bandwidth configs, we set a higher latency in the bunit
3992 * so that the core display fetch happens in time to avoid underruns.
3993 */
3994 if (cdclk == 400)
3995 val |= 4500 / 250; /* 4.5 usec */
3996 else
3997 val |= 3000 / 250; /* 3.0 usec */
3998 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
3999 mutex_unlock(&dev_priv->dpio_lock);
4000
4001 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4002 intel_i2c_reset(dev);
4003}
4004
4005static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4006{
4007 int cur_cdclk, vco;
4008 int divider;
4009
4010 vco = valleyview_get_vco(dev_priv);
4011
4012 mutex_lock(&dev_priv->dpio_lock);
4013 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4014 mutex_unlock(&dev_priv->dpio_lock);
4015
4016 divider &= 0xf;
4017
4018 cur_cdclk = (vco << 1) / (divider + 1);
4019
4020 return cur_cdclk;
4021}
4022
4023static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4024 int max_pixclk)
4025{
4026 int cur_cdclk;
4027
4028 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4029
4030 /*
4031 * Really only a few cases to deal with, as only 4 CDclks are supported:
4032 * 200MHz
4033 * 267MHz
4034 * 320MHz
4035 * 400MHz
4036 * So we check to see whether we're above 90% of the lower bin and
4037 * adjust if needed.
4038 */
4039 if (max_pixclk > 288000) {
4040 return 400;
4041 } else if (max_pixclk > 240000) {
4042 return 320;
4043 } else
4044 return 266;
4045 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4046}
4047
4048static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv,
4049 unsigned modeset_pipes,
4050 struct intel_crtc_config *pipe_config)
4051{
4052 struct drm_device *dev = dev_priv->dev;
4053 struct intel_crtc *intel_crtc;
4054 int max_pixclk = 0;
4055
4056 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4057 base.head) {
4058 if (modeset_pipes & (1 << intel_crtc->pipe))
4059 max_pixclk = max(max_pixclk,
4060 pipe_config->adjusted_mode.crtc_clock);
4061 else if (intel_crtc->base.enabled)
4062 max_pixclk = max(max_pixclk,
4063 intel_crtc->config.adjusted_mode.crtc_clock);
4064 }
4065
4066 return max_pixclk;
4067}
4068
4069static void valleyview_modeset_global_pipes(struct drm_device *dev,
4070 unsigned *prepare_pipes,
4071 unsigned modeset_pipes,
4072 struct intel_crtc_config *pipe_config)
4073{
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 struct intel_crtc *intel_crtc;
4076 int max_pixclk = intel_mode_max_pixclk(dev_priv, modeset_pipes,
4077 pipe_config);
4078 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4079
4080 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4081 return;
4082
4083 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4084 base.head)
4085 if (intel_crtc->base.enabled)
4086 *prepare_pipes |= (1 << intel_crtc->pipe);
4087}
4088
4089static void valleyview_modeset_global_resources(struct drm_device *dev)
4090{
4091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 int max_pixclk = intel_mode_max_pixclk(dev_priv, 0, NULL);
4093 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4094 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4095
4096 if (req_cdclk != cur_cdclk)
4097 valleyview_set_cdclk(dev, req_cdclk);
4098}
4099
89b667f8
JB
4100static void valleyview_crtc_enable(struct drm_crtc *crtc)
4101{
4102 struct drm_device *dev = crtc->dev;
4103 struct drm_i915_private *dev_priv = dev->dev_private;
4104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4105 struct intel_encoder *encoder;
4106 int pipe = intel_crtc->pipe;
4107 int plane = intel_crtc->plane;
23538ef1 4108 bool is_dsi;
89b667f8
JB
4109
4110 WARN_ON(!crtc->enabled);
4111
4112 if (intel_crtc->active)
4113 return;
4114
4115 intel_crtc->active = true;
89b667f8 4116
89b667f8
JB
4117 for_each_encoder_on_crtc(dev, crtc, encoder)
4118 if (encoder->pre_pll_enable)
4119 encoder->pre_pll_enable(encoder);
4120
23538ef1
JN
4121 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4122
e9fd1c02
JN
4123 if (!is_dsi)
4124 vlv_enable_pll(intel_crtc);
89b667f8
JB
4125
4126 for_each_encoder_on_crtc(dev, crtc, encoder)
4127 if (encoder->pre_enable)
4128 encoder->pre_enable(encoder);
4129
2dd24552
JB
4130 i9xx_pfit_enable(intel_crtc);
4131
63cbb074
VS
4132 intel_crtc_load_lut(crtc);
4133
f37fcc2a 4134 intel_update_watermarks(crtc);
23538ef1 4135 intel_enable_pipe(dev_priv, pipe, false, is_dsi);
d1de00ef 4136 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 4137 intel_enable_planes(crtc);
5c38d48c 4138 intel_crtc_update_cursor(crtc, true);
89b667f8 4139
89b667f8 4140 intel_update_fbc(dev);
5004945f
JN
4141
4142 for_each_encoder_on_crtc(dev, crtc, encoder)
4143 encoder->enable(encoder);
89b667f8
JB
4144}
4145
0b8765c6 4146static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4147{
4148 struct drm_device *dev = crtc->dev;
79e53945
JB
4149 struct drm_i915_private *dev_priv = dev->dev_private;
4150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4151 struct intel_encoder *encoder;
79e53945 4152 int pipe = intel_crtc->pipe;
80824003 4153 int plane = intel_crtc->plane;
79e53945 4154
08a48469
DV
4155 WARN_ON(!crtc->enabled);
4156
f7abfe8b
CW
4157 if (intel_crtc->active)
4158 return;
4159
4160 intel_crtc->active = true;
6b383a7f 4161
9d6d9f19
MK
4162 for_each_encoder_on_crtc(dev, crtc, encoder)
4163 if (encoder->pre_enable)
4164 encoder->pre_enable(encoder);
4165
f6736a1a
DV
4166 i9xx_enable_pll(intel_crtc);
4167
2dd24552
JB
4168 i9xx_pfit_enable(intel_crtc);
4169
63cbb074
VS
4170 intel_crtc_load_lut(crtc);
4171
f37fcc2a 4172 intel_update_watermarks(crtc);
23538ef1 4173 intel_enable_pipe(dev_priv, pipe, false, false);
d1de00ef 4174 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 4175 intel_enable_planes(crtc);
22e407d7 4176 /* The fixup needs to happen before cursor is enabled */
61bc95c1
EE
4177 if (IS_G4X(dev))
4178 g4x_fixup_plane(dev_priv, pipe);
22e407d7 4179 intel_crtc_update_cursor(crtc, true);
79e53945 4180
0b8765c6
JB
4181 /* Give the overlay scaler a chance to enable if it's on this pipe */
4182 intel_crtc_dpms_overlay(intel_crtc, true);
ef9c3aee 4183
f440eb13 4184 intel_update_fbc(dev);
ef9c3aee 4185
fa5c73b1
DV
4186 for_each_encoder_on_crtc(dev, crtc, encoder)
4187 encoder->enable(encoder);
0b8765c6 4188}
79e53945 4189
87476d63
DV
4190static void i9xx_pfit_disable(struct intel_crtc *crtc)
4191{
4192 struct drm_device *dev = crtc->base.dev;
4193 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4194
328d8e82
DV
4195 if (!crtc->config.gmch_pfit.control)
4196 return;
87476d63 4197
328d8e82 4198 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4199
328d8e82
DV
4200 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4201 I915_READ(PFIT_CONTROL));
4202 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4203}
4204
0b8765c6
JB
4205static void i9xx_crtc_disable(struct drm_crtc *crtc)
4206{
4207 struct drm_device *dev = crtc->dev;
4208 struct drm_i915_private *dev_priv = dev->dev_private;
4209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4210 struct intel_encoder *encoder;
0b8765c6
JB
4211 int pipe = intel_crtc->pipe;
4212 int plane = intel_crtc->plane;
ef9c3aee 4213
f7abfe8b
CW
4214 if (!intel_crtc->active)
4215 return;
4216
ea9d758d
DV
4217 for_each_encoder_on_crtc(dev, crtc, encoder)
4218 encoder->disable(encoder);
4219
0b8765c6 4220 /* Give the overlay scaler a chance to disable if it's on this pipe */
e6c3a2a6
CW
4221 intel_crtc_wait_for_pending_flips(crtc);
4222 drm_vblank_off(dev, pipe);
0b8765c6 4223
5c3fe8b0 4224 if (dev_priv->fbc.plane == plane)
973d04f9 4225 intel_disable_fbc(dev);
79e53945 4226
0d5b8c61
VS
4227 intel_crtc_dpms_overlay(intel_crtc, false);
4228 intel_crtc_update_cursor(crtc, false);
bb53d4ae 4229 intel_disable_planes(crtc);
d1de00ef 4230 intel_disable_primary_plane(dev_priv, plane, pipe);
0d5b8c61 4231
b24e7179 4232 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4233
87476d63 4234 i9xx_pfit_disable(intel_crtc);
24a1f16d 4235
89b667f8
JB
4236 for_each_encoder_on_crtc(dev, crtc, encoder)
4237 if (encoder->post_disable)
4238 encoder->post_disable(encoder);
4239
f6071166
JB
4240 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4241 vlv_disable_pll(dev_priv, pipe);
4242 else if (!IS_VALLEYVIEW(dev))
e9fd1c02 4243 i9xx_disable_pll(dev_priv, pipe);
0b8765c6 4244
f7abfe8b 4245 intel_crtc->active = false;
46ba614c 4246 intel_update_watermarks(crtc);
f37fcc2a 4247
6b383a7f 4248 intel_update_fbc(dev);
0b8765c6
JB
4249}
4250
ee7b9f93
JB
4251static void i9xx_crtc_off(struct drm_crtc *crtc)
4252{
4253}
4254
976f8a20
DV
4255static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4256 bool enabled)
2c07245f
ZW
4257{
4258 struct drm_device *dev = crtc->dev;
4259 struct drm_i915_master_private *master_priv;
4260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4261 int pipe = intel_crtc->pipe;
79e53945
JB
4262
4263 if (!dev->primary->master)
4264 return;
4265
4266 master_priv = dev->primary->master->driver_priv;
4267 if (!master_priv->sarea_priv)
4268 return;
4269
79e53945
JB
4270 switch (pipe) {
4271 case 0:
4272 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4273 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4274 break;
4275 case 1:
4276 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4277 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4278 break;
4279 default:
9db4a9c7 4280 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4281 break;
4282 }
79e53945
JB
4283}
4284
976f8a20
DV
4285/**
4286 * Sets the power management mode of the pipe and plane.
4287 */
4288void intel_crtc_update_dpms(struct drm_crtc *crtc)
4289{
4290 struct drm_device *dev = crtc->dev;
4291 struct drm_i915_private *dev_priv = dev->dev_private;
4292 struct intel_encoder *intel_encoder;
4293 bool enable = false;
4294
4295 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4296 enable |= intel_encoder->connectors_active;
4297
4298 if (enable)
4299 dev_priv->display.crtc_enable(crtc);
4300 else
4301 dev_priv->display.crtc_disable(crtc);
4302
4303 intel_crtc_update_sarea(crtc, enable);
4304}
4305
cdd59983
CW
4306static void intel_crtc_disable(struct drm_crtc *crtc)
4307{
cdd59983 4308 struct drm_device *dev = crtc->dev;
976f8a20 4309 struct drm_connector *connector;
ee7b9f93 4310 struct drm_i915_private *dev_priv = dev->dev_private;
7b9f35a6 4311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cdd59983 4312
976f8a20
DV
4313 /* crtc should still be enabled when we disable it. */
4314 WARN_ON(!crtc->enabled);
4315
4316 dev_priv->display.crtc_disable(crtc);
c77bf565 4317 intel_crtc->eld_vld = false;
976f8a20 4318 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4319 dev_priv->display.off(crtc);
4320
931872fc 4321 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
93ce0ba6 4322 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
931872fc 4323 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
cdd59983
CW
4324
4325 if (crtc->fb) {
4326 mutex_lock(&dev->struct_mutex);
1690e1eb 4327 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
cdd59983 4328 mutex_unlock(&dev->struct_mutex);
976f8a20
DV
4329 crtc->fb = NULL;
4330 }
4331
4332 /* Update computed state. */
4333 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4334 if (!connector->encoder || !connector->encoder->crtc)
4335 continue;
4336
4337 if (connector->encoder->crtc != crtc)
4338 continue;
4339
4340 connector->dpms = DRM_MODE_DPMS_OFF;
4341 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
4342 }
4343}
4344
ea5b213a 4345void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 4346{
4ef69c7a 4347 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 4348
ea5b213a
CW
4349 drm_encoder_cleanup(encoder);
4350 kfree(intel_encoder);
7e7d76c3
JB
4351}
4352
9237329d 4353/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
4354 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4355 * state of the entire output pipe. */
9237329d 4356static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 4357{
5ab432ef
DV
4358 if (mode == DRM_MODE_DPMS_ON) {
4359 encoder->connectors_active = true;
4360
b2cabb0e 4361 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
4362 } else {
4363 encoder->connectors_active = false;
4364
b2cabb0e 4365 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 4366 }
79e53945
JB
4367}
4368
0a91ca29
DV
4369/* Cross check the actual hw state with our own modeset state tracking (and it's
4370 * internal consistency). */
b980514c 4371static void intel_connector_check_state(struct intel_connector *connector)
79e53945 4372{
0a91ca29
DV
4373 if (connector->get_hw_state(connector)) {
4374 struct intel_encoder *encoder = connector->encoder;
4375 struct drm_crtc *crtc;
4376 bool encoder_enabled;
4377 enum pipe pipe;
4378
4379 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4380 connector->base.base.id,
4381 drm_get_connector_name(&connector->base));
4382
4383 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4384 "wrong connector dpms state\n");
4385 WARN(connector->base.encoder != &encoder->base,
4386 "active connector not linked to encoder\n");
4387 WARN(!encoder->connectors_active,
4388 "encoder->connectors_active not set\n");
4389
4390 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4391 WARN(!encoder_enabled, "encoder not enabled\n");
4392 if (WARN_ON(!encoder->base.crtc))
4393 return;
4394
4395 crtc = encoder->base.crtc;
4396
4397 WARN(!crtc->enabled, "crtc not enabled\n");
4398 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4399 WARN(pipe != to_intel_crtc(crtc)->pipe,
4400 "encoder active on the wrong pipe\n");
4401 }
79e53945
JB
4402}
4403
5ab432ef
DV
4404/* Even simpler default implementation, if there's really no special case to
4405 * consider. */
4406void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 4407{
5ab432ef
DV
4408 /* All the simple cases only support two dpms states. */
4409 if (mode != DRM_MODE_DPMS_ON)
4410 mode = DRM_MODE_DPMS_OFF;
d4270e57 4411
5ab432ef
DV
4412 if (mode == connector->dpms)
4413 return;
4414
4415 connector->dpms = mode;
4416
4417 /* Only need to change hw state when actually enabled */
c9976dcf
CW
4418 if (connector->encoder)
4419 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 4420
b980514c 4421 intel_modeset_check_state(connector->dev);
79e53945
JB
4422}
4423
f0947c37
DV
4424/* Simple connector->get_hw_state implementation for encoders that support only
4425 * one connector and no cloning and hence the encoder state determines the state
4426 * of the connector. */
4427bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 4428{
24929352 4429 enum pipe pipe = 0;
f0947c37 4430 struct intel_encoder *encoder = connector->encoder;
ea5b213a 4431
f0947c37 4432 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
4433}
4434
1857e1da
DV
4435static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4436 struct intel_crtc_config *pipe_config)
4437{
4438 struct drm_i915_private *dev_priv = dev->dev_private;
4439 struct intel_crtc *pipe_B_crtc =
4440 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4441
4442 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4443 pipe_name(pipe), pipe_config->fdi_lanes);
4444 if (pipe_config->fdi_lanes > 4) {
4445 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4446 pipe_name(pipe), pipe_config->fdi_lanes);
4447 return false;
4448 }
4449
bafb6553 4450 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
4451 if (pipe_config->fdi_lanes > 2) {
4452 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4453 pipe_config->fdi_lanes);
4454 return false;
4455 } else {
4456 return true;
4457 }
4458 }
4459
4460 if (INTEL_INFO(dev)->num_pipes == 2)
4461 return true;
4462
4463 /* Ivybridge 3 pipe is really complicated */
4464 switch (pipe) {
4465 case PIPE_A:
4466 return true;
4467 case PIPE_B:
4468 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4469 pipe_config->fdi_lanes > 2) {
4470 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4471 pipe_name(pipe), pipe_config->fdi_lanes);
4472 return false;
4473 }
4474 return true;
4475 case PIPE_C:
1e833f40 4476 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
4477 pipe_B_crtc->config.fdi_lanes <= 2) {
4478 if (pipe_config->fdi_lanes > 2) {
4479 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4480 pipe_name(pipe), pipe_config->fdi_lanes);
4481 return false;
4482 }
4483 } else {
4484 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4485 return false;
4486 }
4487 return true;
4488 default:
4489 BUG();
4490 }
4491}
4492
e29c22c0
DV
4493#define RETRY 1
4494static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4495 struct intel_crtc_config *pipe_config)
877d48d5 4496{
1857e1da 4497 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 4498 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 4499 int lane, link_bw, fdi_dotclock;
e29c22c0 4500 bool setup_ok, needs_recompute = false;
877d48d5 4501
e29c22c0 4502retry:
877d48d5
DV
4503 /* FDI is a binary signal running at ~2.7GHz, encoding
4504 * each output octet as 10 bits. The actual frequency
4505 * is stored as a divider into a 100MHz clock, and the
4506 * mode pixel clock is stored in units of 1KHz.
4507 * Hence the bw of each lane in terms of the mode signal
4508 * is:
4509 */
4510 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4511
241bfc38 4512 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 4513
2bd89a07 4514 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
4515 pipe_config->pipe_bpp);
4516
4517 pipe_config->fdi_lanes = lane;
4518
2bd89a07 4519 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 4520 link_bw, &pipe_config->fdi_m_n);
1857e1da 4521
e29c22c0
DV
4522 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4523 intel_crtc->pipe, pipe_config);
4524 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4525 pipe_config->pipe_bpp -= 2*3;
4526 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4527 pipe_config->pipe_bpp);
4528 needs_recompute = true;
4529 pipe_config->bw_constrained = true;
4530
4531 goto retry;
4532 }
4533
4534 if (needs_recompute)
4535 return RETRY;
4536
4537 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
4538}
4539
42db64ef
PZ
4540static void hsw_compute_ips_config(struct intel_crtc *crtc,
4541 struct intel_crtc_config *pipe_config)
4542{
3c4ca58c
PZ
4543 pipe_config->ips_enabled = i915_enable_ips &&
4544 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 4545 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
4546}
4547
a43f6e0f 4548static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 4549 struct intel_crtc_config *pipe_config)
79e53945 4550{
a43f6e0f 4551 struct drm_device *dev = crtc->base.dev;
b8cecdf5 4552 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 4553
ad3a4479 4554 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
4555 if (INTEL_INFO(dev)->gen < 4) {
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557 int clock_limit =
4558 dev_priv->display.get_display_clock_speed(dev);
4559
4560 /*
4561 * Enable pixel doubling when the dot clock
4562 * is > 90% of the (display) core speed.
4563 *
b397c96b
VS
4564 * GDG double wide on either pipe,
4565 * otherwise pipe A only.
cf532bb2 4566 */
b397c96b 4567 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 4568 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 4569 clock_limit *= 2;
cf532bb2 4570 pipe_config->double_wide = true;
ad3a4479
VS
4571 }
4572
241bfc38 4573 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 4574 return -EINVAL;
2c07245f 4575 }
89749350 4576
1d1d0e27
VS
4577 /*
4578 * Pipe horizontal size must be even in:
4579 * - DVO ganged mode
4580 * - LVDS dual channel mode
4581 * - Double wide pipe
4582 */
4583 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4584 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4585 pipe_config->pipe_src_w &= ~1;
4586
8693a824
DL
4587 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4588 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
4589 */
4590 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4591 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 4592 return -EINVAL;
44f46b42 4593
bd080ee5 4594 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 4595 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 4596 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
4597 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4598 * for lvds. */
4599 pipe_config->pipe_bpp = 8*3;
4600 }
4601
f5adf94e 4602 if (HAS_IPS(dev))
a43f6e0f
DV
4603 hsw_compute_ips_config(crtc, pipe_config);
4604
4605 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4606 * clock survives for now. */
4607 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4608 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 4609
877d48d5 4610 if (pipe_config->has_pch_encoder)
a43f6e0f 4611 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 4612
e29c22c0 4613 return 0;
79e53945
JB
4614}
4615
25eb05fc
JB
4616static int valleyview_get_display_clock_speed(struct drm_device *dev)
4617{
4618 return 400000; /* FIXME */
4619}
4620
e70236a8
JB
4621static int i945_get_display_clock_speed(struct drm_device *dev)
4622{
4623 return 400000;
4624}
79e53945 4625
e70236a8 4626static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 4627{
e70236a8
JB
4628 return 333000;
4629}
79e53945 4630
e70236a8
JB
4631static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4632{
4633 return 200000;
4634}
79e53945 4635
257a7ffc
DV
4636static int pnv_get_display_clock_speed(struct drm_device *dev)
4637{
4638 u16 gcfgc = 0;
4639
4640 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4641
4642 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4643 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4644 return 267000;
4645 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4646 return 333000;
4647 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4648 return 444000;
4649 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4650 return 200000;
4651 default:
4652 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4653 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4654 return 133000;
4655 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4656 return 167000;
4657 }
4658}
4659
e70236a8
JB
4660static int i915gm_get_display_clock_speed(struct drm_device *dev)
4661{
4662 u16 gcfgc = 0;
79e53945 4663
e70236a8
JB
4664 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4665
4666 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4667 return 133000;
4668 else {
4669 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4670 case GC_DISPLAY_CLOCK_333_MHZ:
4671 return 333000;
4672 default:
4673 case GC_DISPLAY_CLOCK_190_200_MHZ:
4674 return 190000;
79e53945 4675 }
e70236a8
JB
4676 }
4677}
4678
4679static int i865_get_display_clock_speed(struct drm_device *dev)
4680{
4681 return 266000;
4682}
4683
4684static int i855_get_display_clock_speed(struct drm_device *dev)
4685{
4686 u16 hpllcc = 0;
4687 /* Assume that the hardware is in the high speed state. This
4688 * should be the default.
4689 */
4690 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4691 case GC_CLOCK_133_200:
4692 case GC_CLOCK_100_200:
4693 return 200000;
4694 case GC_CLOCK_166_250:
4695 return 250000;
4696 case GC_CLOCK_100_133:
79e53945 4697 return 133000;
e70236a8 4698 }
79e53945 4699
e70236a8
JB
4700 /* Shouldn't happen */
4701 return 0;
4702}
79e53945 4703
e70236a8
JB
4704static int i830_get_display_clock_speed(struct drm_device *dev)
4705{
4706 return 133000;
79e53945
JB
4707}
4708
2c07245f 4709static void
a65851af 4710intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 4711{
a65851af
VS
4712 while (*num > DATA_LINK_M_N_MASK ||
4713 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
4714 *num >>= 1;
4715 *den >>= 1;
4716 }
4717}
4718
a65851af
VS
4719static void compute_m_n(unsigned int m, unsigned int n,
4720 uint32_t *ret_m, uint32_t *ret_n)
4721{
4722 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4723 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4724 intel_reduce_m_n_ratio(ret_m, ret_n);
4725}
4726
e69d0bc1
DV
4727void
4728intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4729 int pixel_clock, int link_clock,
4730 struct intel_link_m_n *m_n)
2c07245f 4731{
e69d0bc1 4732 m_n->tu = 64;
a65851af
VS
4733
4734 compute_m_n(bits_per_pixel * pixel_clock,
4735 link_clock * nlanes * 8,
4736 &m_n->gmch_m, &m_n->gmch_n);
4737
4738 compute_m_n(pixel_clock, link_clock,
4739 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
4740}
4741
a7615030
CW
4742static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4743{
72bbe58c
KP
4744 if (i915_panel_use_ssc >= 0)
4745 return i915_panel_use_ssc != 0;
41aa3448 4746 return dev_priv->vbt.lvds_use_ssc
435793df 4747 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
4748}
4749
c65d77d8
JB
4750static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4751{
4752 struct drm_device *dev = crtc->dev;
4753 struct drm_i915_private *dev_priv = dev->dev_private;
4754 int refclk;
4755
a0c4da24 4756 if (IS_VALLEYVIEW(dev)) {
9a0ea498 4757 refclk = 100000;
a0c4da24 4758 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 4759 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
41aa3448 4760 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
c65d77d8
JB
4761 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4762 refclk / 1000);
4763 } else if (!IS_GEN2(dev)) {
4764 refclk = 96000;
4765 } else {
4766 refclk = 48000;
4767 }
4768
4769 return refclk;
4770}
4771
7429e9d4 4772static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 4773{
7df00d7a 4774 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 4775}
f47709a9 4776
7429e9d4
DV
4777static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4778{
4779 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
4780}
4781
f47709a9 4782static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
4783 intel_clock_t *reduced_clock)
4784{
f47709a9 4785 struct drm_device *dev = crtc->base.dev;
a7516a05 4786 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 4787 int pipe = crtc->pipe;
a7516a05
JB
4788 u32 fp, fp2 = 0;
4789
4790 if (IS_PINEVIEW(dev)) {
7429e9d4 4791 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 4792 if (reduced_clock)
7429e9d4 4793 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 4794 } else {
7429e9d4 4795 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 4796 if (reduced_clock)
7429e9d4 4797 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
4798 }
4799
4800 I915_WRITE(FP0(pipe), fp);
8bcc2795 4801 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 4802
f47709a9
DV
4803 crtc->lowfreq_avail = false;
4804 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
a7516a05
JB
4805 reduced_clock && i915_powersave) {
4806 I915_WRITE(FP1(pipe), fp2);
8bcc2795 4807 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 4808 crtc->lowfreq_avail = true;
a7516a05
JB
4809 } else {
4810 I915_WRITE(FP1(pipe), fp);
8bcc2795 4811 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
4812 }
4813}
4814
5e69f97f
CML
4815static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
4816 pipe)
89b667f8
JB
4817{
4818 u32 reg_val;
4819
4820 /*
4821 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4822 * and set it to a reasonable value instead.
4823 */
ab3c759a 4824 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
4825 reg_val &= 0xffffff00;
4826 reg_val |= 0x00000030;
ab3c759a 4827 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 4828
ab3c759a 4829 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
4830 reg_val &= 0x8cffffff;
4831 reg_val = 0x8c000000;
ab3c759a 4832 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 4833
ab3c759a 4834 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 4835 reg_val &= 0xffffff00;
ab3c759a 4836 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 4837
ab3c759a 4838 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
4839 reg_val &= 0x00ffffff;
4840 reg_val |= 0xb0000000;
ab3c759a 4841 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
4842}
4843
b551842d
DV
4844static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4845 struct intel_link_m_n *m_n)
4846{
4847 struct drm_device *dev = crtc->base.dev;
4848 struct drm_i915_private *dev_priv = dev->dev_private;
4849 int pipe = crtc->pipe;
4850
e3b95f1e
DV
4851 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4852 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4853 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4854 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
4855}
4856
4857static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4858 struct intel_link_m_n *m_n)
4859{
4860 struct drm_device *dev = crtc->base.dev;
4861 struct drm_i915_private *dev_priv = dev->dev_private;
4862 int pipe = crtc->pipe;
4863 enum transcoder transcoder = crtc->config.cpu_transcoder;
4864
4865 if (INTEL_INFO(dev)->gen >= 5) {
4866 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4867 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4868 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4869 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4870 } else {
e3b95f1e
DV
4871 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4872 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4873 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4874 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
4875 }
4876}
4877
03afc4a2
DV
4878static void intel_dp_set_m_n(struct intel_crtc *crtc)
4879{
4880 if (crtc->config.has_pch_encoder)
4881 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4882 else
4883 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4884}
4885
f47709a9 4886static void vlv_update_pll(struct intel_crtc *crtc)
a0c4da24 4887{
f47709a9 4888 struct drm_device *dev = crtc->base.dev;
a0c4da24 4889 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 4890 int pipe = crtc->pipe;
89b667f8 4891 u32 dpll, mdiv;
a0c4da24 4892 u32 bestn, bestm1, bestm2, bestp1, bestp2;
198a037f 4893 u32 coreclk, reg_val, dpll_md;
a0c4da24 4894
09153000
DV
4895 mutex_lock(&dev_priv->dpio_lock);
4896
f47709a9
DV
4897 bestn = crtc->config.dpll.n;
4898 bestm1 = crtc->config.dpll.m1;
4899 bestm2 = crtc->config.dpll.m2;
4900 bestp1 = crtc->config.dpll.p1;
4901 bestp2 = crtc->config.dpll.p2;
a0c4da24 4902
89b667f8
JB
4903 /* See eDP HDMI DPIO driver vbios notes doc */
4904
4905 /* PLL B needs special handling */
4906 if (pipe)
5e69f97f 4907 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
4908
4909 /* Set up Tx target for periodic Rcomp update */
ab3c759a 4910 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
4911
4912 /* Disable target IRef on PLL */
ab3c759a 4913 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 4914 reg_val &= 0x00ffffff;
ab3c759a 4915 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
4916
4917 /* Disable fast lock */
ab3c759a 4918 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
4919
4920 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
4921 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4922 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4923 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 4924 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
4925
4926 /*
4927 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4928 * but we don't support that).
4929 * Note: don't use the DAC post divider as it seems unstable.
4930 */
4931 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 4932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 4933
a0c4da24 4934 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 4935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 4936
89b667f8 4937 /* Set HBR and RBR LPF coefficients */
ff9a6750 4938 if (crtc->config.port_clock == 162000 ||
99750bd4 4939 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 4940 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 4941 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 4942 0x009f0003);
89b667f8 4943 else
ab3c759a 4944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
4945 0x00d0000f);
4946
4947 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4948 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4949 /* Use SSC source */
4950 if (!pipe)
ab3c759a 4951 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
4952 0x0df40000);
4953 else
ab3c759a 4954 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
4955 0x0df70000);
4956 } else { /* HDMI or VGA */
4957 /* Use bend source */
4958 if (!pipe)
ab3c759a 4959 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
4960 0x0df70000);
4961 else
ab3c759a 4962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
4963 0x0df40000);
4964 }
a0c4da24 4965
ab3c759a 4966 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
4967 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4968 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4969 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4970 coreclk |= 0x01000000;
ab3c759a 4971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 4972
ab3c759a 4973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a0c4da24 4974
89b667f8
JB
4975 /* Enable DPIO clock input */
4976 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4977 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
f6071166
JB
4978 /* We should never disable this, set it here for state tracking */
4979 if (pipe == PIPE_B)
89b667f8 4980 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
a0c4da24 4981 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
4982 crtc->config.dpll_hw_state.dpll = dpll;
4983
ef1b460d
DV
4984 dpll_md = (crtc->config.pixel_multiplier - 1)
4985 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795
DV
4986 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4987
89b667f8
JB
4988 if (crtc->config.has_dp_encoder)
4989 intel_dp_set_m_n(crtc);
09153000
DV
4990
4991 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
4992}
4993
f47709a9
DV
4994static void i9xx_update_pll(struct intel_crtc *crtc,
4995 intel_clock_t *reduced_clock,
eb1cbe48
DV
4996 int num_connectors)
4997{
f47709a9 4998 struct drm_device *dev = crtc->base.dev;
eb1cbe48 4999 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5000 u32 dpll;
5001 bool is_sdvo;
f47709a9 5002 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5003
f47709a9 5004 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5005
f47709a9
DV
5006 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5007 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5008
5009 dpll = DPLL_VGA_MODE_DIS;
5010
f47709a9 5011 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5012 dpll |= DPLLB_MODE_LVDS;
5013 else
5014 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5015
ef1b460d 5016 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5017 dpll |= (crtc->config.pixel_multiplier - 1)
5018 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5019 }
198a037f
DV
5020
5021 if (is_sdvo)
4a33e48d 5022 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5023
f47709a9 5024 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5025 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5026
5027 /* compute bitmask from p1 value */
5028 if (IS_PINEVIEW(dev))
5029 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5030 else {
5031 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5032 if (IS_G4X(dev) && reduced_clock)
5033 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5034 }
5035 switch (clock->p2) {
5036 case 5:
5037 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5038 break;
5039 case 7:
5040 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5041 break;
5042 case 10:
5043 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5044 break;
5045 case 14:
5046 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5047 break;
5048 }
5049 if (INTEL_INFO(dev)->gen >= 4)
5050 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5051
09ede541 5052 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5053 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5054 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5055 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5056 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5057 else
5058 dpll |= PLL_REF_INPUT_DREFCLK;
5059
5060 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5061 crtc->config.dpll_hw_state.dpll = dpll;
5062
eb1cbe48 5063 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5064 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5065 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5066 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48 5067 }
66e3d5c0
DV
5068
5069 if (crtc->config.has_dp_encoder)
5070 intel_dp_set_m_n(crtc);
eb1cbe48
DV
5071}
5072
f47709a9 5073static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5074 intel_clock_t *reduced_clock,
eb1cbe48
DV
5075 int num_connectors)
5076{
f47709a9 5077 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5078 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5079 u32 dpll;
f47709a9 5080 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5081
f47709a9 5082 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5083
eb1cbe48
DV
5084 dpll = DPLL_VGA_MODE_DIS;
5085
f47709a9 5086 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5087 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5088 } else {
5089 if (clock->p1 == 2)
5090 dpll |= PLL_P1_DIVIDE_BY_TWO;
5091 else
5092 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5093 if (clock->p2 == 4)
5094 dpll |= PLL_P2_DIVIDE_BY_4;
5095 }
5096
4a33e48d
DV
5097 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5098 dpll |= DPLL_DVO_2X_MODE;
5099
f47709a9 5100 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5101 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5102 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5103 else
5104 dpll |= PLL_REF_INPUT_DREFCLK;
5105
5106 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5107 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5108}
5109
8a654f3b 5110static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5111{
5112 struct drm_device *dev = intel_crtc->base.dev;
5113 struct drm_i915_private *dev_priv = dev->dev_private;
5114 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5115 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5116 struct drm_display_mode *adjusted_mode =
5117 &intel_crtc->config.adjusted_mode;
4d8a62ea
DV
5118 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5119
5120 /* We need to be careful not to changed the adjusted mode, for otherwise
5121 * the hw state checker will get angry at the mismatch. */
5122 crtc_vtotal = adjusted_mode->crtc_vtotal;
5123 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c
PZ
5124
5125 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5126 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5127 crtc_vtotal -= 1;
5128 crtc_vblank_end -= 1;
b0e77b9c
PZ
5129 vsyncshift = adjusted_mode->crtc_hsync_start
5130 - adjusted_mode->crtc_htotal / 2;
5131 } else {
5132 vsyncshift = 0;
5133 }
5134
5135 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5136 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5137
fe2b8f9d 5138 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5139 (adjusted_mode->crtc_hdisplay - 1) |
5140 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5141 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5142 (adjusted_mode->crtc_hblank_start - 1) |
5143 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5144 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5145 (adjusted_mode->crtc_hsync_start - 1) |
5146 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5147
fe2b8f9d 5148 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5149 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5150 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5151 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5152 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5153 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5154 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5155 (adjusted_mode->crtc_vsync_start - 1) |
5156 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5157
b5e508d4
PZ
5158 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5159 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5160 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5161 * bits. */
5162 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5163 (pipe == PIPE_B || pipe == PIPE_C))
5164 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5165
b0e77b9c
PZ
5166 /* pipesrc controls the size that is scaled from, which should
5167 * always be the user's requested size.
5168 */
5169 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5170 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5171 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5172}
5173
1bd1bd80
DV
5174static void intel_get_pipe_timings(struct intel_crtc *crtc,
5175 struct intel_crtc_config *pipe_config)
5176{
5177 struct drm_device *dev = crtc->base.dev;
5178 struct drm_i915_private *dev_priv = dev->dev_private;
5179 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5180 uint32_t tmp;
5181
5182 tmp = I915_READ(HTOTAL(cpu_transcoder));
5183 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5184 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5185 tmp = I915_READ(HBLANK(cpu_transcoder));
5186 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5187 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5188 tmp = I915_READ(HSYNC(cpu_transcoder));
5189 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5190 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5191
5192 tmp = I915_READ(VTOTAL(cpu_transcoder));
5193 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5194 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5195 tmp = I915_READ(VBLANK(cpu_transcoder));
5196 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5197 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5198 tmp = I915_READ(VSYNC(cpu_transcoder));
5199 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5200 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5201
5202 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5203 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5204 pipe_config->adjusted_mode.crtc_vtotal += 1;
5205 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5206 }
5207
5208 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
5209 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5210 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5211
5212 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5213 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
5214}
5215
babea61d
JB
5216static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
5217 struct intel_crtc_config *pipe_config)
5218{
5219 struct drm_crtc *crtc = &intel_crtc->base;
5220
5221 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5222 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
5223 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5224 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5225
5226 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5227 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5228 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5229 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5230
5231 crtc->mode.flags = pipe_config->adjusted_mode.flags;
5232
241bfc38 5233 crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
babea61d
JB
5234 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
5235}
5236
84b046f3
DV
5237static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5238{
5239 struct drm_device *dev = intel_crtc->base.dev;
5240 struct drm_i915_private *dev_priv = dev->dev_private;
5241 uint32_t pipeconf;
5242
9f11a9e4 5243 pipeconf = 0;
84b046f3 5244
67c72a12
DV
5245 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5246 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5247 pipeconf |= PIPECONF_ENABLE;
5248
cf532bb2
VS
5249 if (intel_crtc->config.double_wide)
5250 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 5251
ff9ce46e
DV
5252 /* only g4x and later have fancy bpc/dither controls */
5253 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
5254 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5255 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5256 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 5257 PIPECONF_DITHER_TYPE_SP;
84b046f3 5258
ff9ce46e
DV
5259 switch (intel_crtc->config.pipe_bpp) {
5260 case 18:
5261 pipeconf |= PIPECONF_6BPC;
5262 break;
5263 case 24:
5264 pipeconf |= PIPECONF_8BPC;
5265 break;
5266 case 30:
5267 pipeconf |= PIPECONF_10BPC;
5268 break;
5269 default:
5270 /* Case prevented by intel_choose_pipe_bpp_dither. */
5271 BUG();
84b046f3
DV
5272 }
5273 }
5274
5275 if (HAS_PIPE_CXSR(dev)) {
5276 if (intel_crtc->lowfreq_avail) {
5277 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5278 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5279 } else {
5280 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
5281 }
5282 }
5283
84b046f3
DV
5284 if (!IS_GEN2(dev) &&
5285 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5286 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5287 else
5288 pipeconf |= PIPECONF_PROGRESSIVE;
5289
9f11a9e4
DV
5290 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5291 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 5292
84b046f3
DV
5293 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5294 POSTING_READ(PIPECONF(intel_crtc->pipe));
5295}
5296
f564048e 5297static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 5298 int x, int y,
94352cf9 5299 struct drm_framebuffer *fb)
79e53945
JB
5300{
5301 struct drm_device *dev = crtc->dev;
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5304 int pipe = intel_crtc->pipe;
80824003 5305 int plane = intel_crtc->plane;
c751ce4f 5306 int refclk, num_connectors = 0;
652c393a 5307 intel_clock_t clock, reduced_clock;
84b046f3 5308 u32 dspcntr;
a16af721 5309 bool ok, has_reduced_clock = false;
e9fd1c02 5310 bool is_lvds = false, is_dsi = false;
5eddb70b 5311 struct intel_encoder *encoder;
d4906093 5312 const intel_limit_t *limit;
5c3b82e2 5313 int ret;
79e53945 5314
6c2b7c12 5315 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 5316 switch (encoder->type) {
79e53945
JB
5317 case INTEL_OUTPUT_LVDS:
5318 is_lvds = true;
5319 break;
e9fd1c02
JN
5320 case INTEL_OUTPUT_DSI:
5321 is_dsi = true;
5322 break;
79e53945 5323 }
43565a06 5324
c751ce4f 5325 num_connectors++;
79e53945
JB
5326 }
5327
f2335330
JN
5328 if (is_dsi)
5329 goto skip_dpll;
5330
5331 if (!intel_crtc->config.clock_set) {
5332 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 5333
e9fd1c02
JN
5334 /*
5335 * Returns a set of divisors for the desired target clock with
5336 * the given refclk, or FALSE. The returned values represent
5337 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5338 * 2) / p1 / p2.
5339 */
5340 limit = intel_limit(crtc, refclk);
5341 ok = dev_priv->display.find_dpll(limit, crtc,
5342 intel_crtc->config.port_clock,
5343 refclk, NULL, &clock);
f2335330 5344 if (!ok) {
e9fd1c02
JN
5345 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5346 return -EINVAL;
5347 }
79e53945 5348
f2335330
JN
5349 if (is_lvds && dev_priv->lvds_downclock_avail) {
5350 /*
5351 * Ensure we match the reduced clock's P to the target
5352 * clock. If the clocks don't match, we can't switch
5353 * the display clock by using the FP0/FP1. In such case
5354 * we will disable the LVDS downclock feature.
5355 */
5356 has_reduced_clock =
5357 dev_priv->display.find_dpll(limit, crtc,
5358 dev_priv->lvds_downclock,
5359 refclk, &clock,
5360 &reduced_clock);
5361 }
5362 /* Compat-code for transition, will disappear. */
f47709a9
DV
5363 intel_crtc->config.dpll.n = clock.n;
5364 intel_crtc->config.dpll.m1 = clock.m1;
5365 intel_crtc->config.dpll.m2 = clock.m2;
5366 intel_crtc->config.dpll.p1 = clock.p1;
5367 intel_crtc->config.dpll.p2 = clock.p2;
5368 }
7026d4ac 5369
e9fd1c02 5370 if (IS_GEN2(dev)) {
8a654f3b 5371 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
5372 has_reduced_clock ? &reduced_clock : NULL,
5373 num_connectors);
e9fd1c02 5374 } else if (IS_VALLEYVIEW(dev)) {
f2335330 5375 vlv_update_pll(intel_crtc);
e9fd1c02 5376 } else {
f47709a9 5377 i9xx_update_pll(intel_crtc,
eb1cbe48 5378 has_reduced_clock ? &reduced_clock : NULL,
89b667f8 5379 num_connectors);
e9fd1c02 5380 }
79e53945 5381
f2335330 5382skip_dpll:
79e53945
JB
5383 /* Set up the display plane register */
5384 dspcntr = DISPPLANE_GAMMA_ENABLE;
5385
da6ecc5d
JB
5386 if (!IS_VALLEYVIEW(dev)) {
5387 if (pipe == 0)
5388 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5389 else
5390 dspcntr |= DISPPLANE_SEL_PIPE_B;
5391 }
79e53945 5392
8a654f3b 5393 intel_set_pipe_timings(intel_crtc);
5eddb70b
CW
5394
5395 /* pipesrc and dspsize control the size that is scaled from,
5396 * which should always be the user's requested size.
79e53945 5397 */
929c77fb 5398 I915_WRITE(DSPSIZE(plane),
37327abd
VS
5399 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5400 (intel_crtc->config.pipe_src_w - 1));
929c77fb 5401 I915_WRITE(DSPPOS(plane), 0);
2c07245f 5402
84b046f3
DV
5403 i9xx_set_pipeconf(intel_crtc);
5404
f564048e
EA
5405 I915_WRITE(DSPCNTR(plane), dspcntr);
5406 POSTING_READ(DSPCNTR(plane));
5407
94352cf9 5408 ret = intel_pipe_set_base(crtc, x, y, fb);
f564048e 5409
f564048e
EA
5410 return ret;
5411}
5412
2fa2fe9a
DV
5413static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5414 struct intel_crtc_config *pipe_config)
5415{
5416 struct drm_device *dev = crtc->base.dev;
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 uint32_t tmp;
5419
5420 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
5421 if (!(tmp & PFIT_ENABLE))
5422 return;
2fa2fe9a 5423
06922821 5424 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
5425 if (INTEL_INFO(dev)->gen < 4) {
5426 if (crtc->pipe != PIPE_B)
5427 return;
2fa2fe9a
DV
5428 } else {
5429 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5430 return;
5431 }
5432
06922821 5433 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
5434 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5435 if (INTEL_INFO(dev)->gen < 5)
5436 pipe_config->gmch_pfit.lvds_border_bits =
5437 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5438}
5439
acbec814
JB
5440static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5441 struct intel_crtc_config *pipe_config)
5442{
5443 struct drm_device *dev = crtc->base.dev;
5444 struct drm_i915_private *dev_priv = dev->dev_private;
5445 int pipe = pipe_config->cpu_transcoder;
5446 intel_clock_t clock;
5447 u32 mdiv;
662c6ecb 5448 int refclk = 100000;
acbec814
JB
5449
5450 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 5451 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
5452 mutex_unlock(&dev_priv->dpio_lock);
5453
5454 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5455 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5456 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5457 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5458 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5459
f646628b 5460 vlv_clock(refclk, &clock);
acbec814 5461
f646628b
VS
5462 /* clock.dot is the fast clock */
5463 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
5464}
5465
0e8ffe1b
DV
5466static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5467 struct intel_crtc_config *pipe_config)
5468{
5469 struct drm_device *dev = crtc->base.dev;
5470 struct drm_i915_private *dev_priv = dev->dev_private;
5471 uint32_t tmp;
5472
e143a21c 5473 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 5474 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 5475
0e8ffe1b
DV
5476 tmp = I915_READ(PIPECONF(crtc->pipe));
5477 if (!(tmp & PIPECONF_ENABLE))
5478 return false;
5479
42571aef
VS
5480 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5481 switch (tmp & PIPECONF_BPC_MASK) {
5482 case PIPECONF_6BPC:
5483 pipe_config->pipe_bpp = 18;
5484 break;
5485 case PIPECONF_8BPC:
5486 pipe_config->pipe_bpp = 24;
5487 break;
5488 case PIPECONF_10BPC:
5489 pipe_config->pipe_bpp = 30;
5490 break;
5491 default:
5492 break;
5493 }
5494 }
5495
282740f7
VS
5496 if (INTEL_INFO(dev)->gen < 4)
5497 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5498
1bd1bd80
DV
5499 intel_get_pipe_timings(crtc, pipe_config);
5500
2fa2fe9a
DV
5501 i9xx_get_pfit_config(crtc, pipe_config);
5502
6c49f241
DV
5503 if (INTEL_INFO(dev)->gen >= 4) {
5504 tmp = I915_READ(DPLL_MD(crtc->pipe));
5505 pipe_config->pixel_multiplier =
5506 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5507 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 5508 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
5509 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5510 tmp = I915_READ(DPLL(crtc->pipe));
5511 pipe_config->pixel_multiplier =
5512 ((tmp & SDVO_MULTIPLIER_MASK)
5513 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5514 } else {
5515 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5516 * port and will be fixed up in the encoder->get_config
5517 * function. */
5518 pipe_config->pixel_multiplier = 1;
5519 }
8bcc2795
DV
5520 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5521 if (!IS_VALLEYVIEW(dev)) {
5522 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5523 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
5524 } else {
5525 /* Mask out read-only status bits. */
5526 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5527 DPLL_PORTC_READY_MASK |
5528 DPLL_PORTB_READY_MASK);
8bcc2795 5529 }
6c49f241 5530
acbec814
JB
5531 if (IS_VALLEYVIEW(dev))
5532 vlv_crtc_clock_get(crtc, pipe_config);
5533 else
5534 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 5535
0e8ffe1b
DV
5536 return true;
5537}
5538
dde86e2d 5539static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
5540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
5542 struct drm_mode_config *mode_config = &dev->mode_config;
13d83a67 5543 struct intel_encoder *encoder;
74cfd7ac 5544 u32 val, final;
13d83a67 5545 bool has_lvds = false;
199e5d79 5546 bool has_cpu_edp = false;
199e5d79 5547 bool has_panel = false;
99eb6a01
KP
5548 bool has_ck505 = false;
5549 bool can_ssc = false;
13d83a67
JB
5550
5551 /* We need to take the global config into account */
199e5d79
KP
5552 list_for_each_entry(encoder, &mode_config->encoder_list,
5553 base.head) {
5554 switch (encoder->type) {
5555 case INTEL_OUTPUT_LVDS:
5556 has_panel = true;
5557 has_lvds = true;
5558 break;
5559 case INTEL_OUTPUT_EDP:
5560 has_panel = true;
2de6905f 5561 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
5562 has_cpu_edp = true;
5563 break;
13d83a67
JB
5564 }
5565 }
5566
99eb6a01 5567 if (HAS_PCH_IBX(dev)) {
41aa3448 5568 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
5569 can_ssc = has_ck505;
5570 } else {
5571 has_ck505 = false;
5572 can_ssc = true;
5573 }
5574
2de6905f
ID
5575 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5576 has_panel, has_lvds, has_ck505);
13d83a67
JB
5577
5578 /* Ironlake: try to setup display ref clock before DPLL
5579 * enabling. This is only under driver's control after
5580 * PCH B stepping, previous chipset stepping should be
5581 * ignoring this setting.
5582 */
74cfd7ac
CW
5583 val = I915_READ(PCH_DREF_CONTROL);
5584
5585 /* As we must carefully and slowly disable/enable each source in turn,
5586 * compute the final state we want first and check if we need to
5587 * make any changes at all.
5588 */
5589 final = val;
5590 final &= ~DREF_NONSPREAD_SOURCE_MASK;
5591 if (has_ck505)
5592 final |= DREF_NONSPREAD_CK505_ENABLE;
5593 else
5594 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5595
5596 final &= ~DREF_SSC_SOURCE_MASK;
5597 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5598 final &= ~DREF_SSC1_ENABLE;
5599
5600 if (has_panel) {
5601 final |= DREF_SSC_SOURCE_ENABLE;
5602
5603 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5604 final |= DREF_SSC1_ENABLE;
5605
5606 if (has_cpu_edp) {
5607 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5608 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5609 else
5610 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5611 } else
5612 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5613 } else {
5614 final |= DREF_SSC_SOURCE_DISABLE;
5615 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5616 }
5617
5618 if (final == val)
5619 return;
5620
13d83a67 5621 /* Always enable nonspread source */
74cfd7ac 5622 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 5623
99eb6a01 5624 if (has_ck505)
74cfd7ac 5625 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 5626 else
74cfd7ac 5627 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 5628
199e5d79 5629 if (has_panel) {
74cfd7ac
CW
5630 val &= ~DREF_SSC_SOURCE_MASK;
5631 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 5632
199e5d79 5633 /* SSC must be turned on before enabling the CPU output */
99eb6a01 5634 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 5635 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 5636 val |= DREF_SSC1_ENABLE;
e77166b5 5637 } else
74cfd7ac 5638 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
5639
5640 /* Get SSC going before enabling the outputs */
74cfd7ac 5641 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5642 POSTING_READ(PCH_DREF_CONTROL);
5643 udelay(200);
5644
74cfd7ac 5645 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
5646
5647 /* Enable CPU source on CPU attached eDP */
199e5d79 5648 if (has_cpu_edp) {
99eb6a01 5649 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 5650 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 5651 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
199e5d79 5652 }
13d83a67 5653 else
74cfd7ac 5654 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 5655 } else
74cfd7ac 5656 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 5657
74cfd7ac 5658 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5659 POSTING_READ(PCH_DREF_CONTROL);
5660 udelay(200);
5661 } else {
5662 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5663
74cfd7ac 5664 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
5665
5666 /* Turn off CPU output */
74cfd7ac 5667 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 5668
74cfd7ac 5669 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5670 POSTING_READ(PCH_DREF_CONTROL);
5671 udelay(200);
5672
5673 /* Turn off the SSC source */
74cfd7ac
CW
5674 val &= ~DREF_SSC_SOURCE_MASK;
5675 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
5676
5677 /* Turn off SSC1 */
74cfd7ac 5678 val &= ~DREF_SSC1_ENABLE;
199e5d79 5679
74cfd7ac 5680 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
5681 POSTING_READ(PCH_DREF_CONTROL);
5682 udelay(200);
5683 }
74cfd7ac
CW
5684
5685 BUG_ON(val != final);
13d83a67
JB
5686}
5687
f31f2d55 5688static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 5689{
f31f2d55 5690 uint32_t tmp;
dde86e2d 5691
0ff066a9
PZ
5692 tmp = I915_READ(SOUTH_CHICKEN2);
5693 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5694 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 5695
0ff066a9
PZ
5696 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5697 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5698 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 5699
0ff066a9
PZ
5700 tmp = I915_READ(SOUTH_CHICKEN2);
5701 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5702 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 5703
0ff066a9
PZ
5704 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5705 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5706 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
5707}
5708
5709/* WaMPhyProgramming:hsw */
5710static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5711{
5712 uint32_t tmp;
dde86e2d
PZ
5713
5714 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5715 tmp &= ~(0xFF << 24);
5716 tmp |= (0x12 << 24);
5717 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5718
dde86e2d
PZ
5719 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5720 tmp |= (1 << 11);
5721 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5722
5723 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5724 tmp |= (1 << 11);
5725 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5726
dde86e2d
PZ
5727 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5728 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5729 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5730
5731 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5732 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5733 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5734
0ff066a9
PZ
5735 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5736 tmp &= ~(7 << 13);
5737 tmp |= (5 << 13);
5738 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 5739
0ff066a9
PZ
5740 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5741 tmp &= ~(7 << 13);
5742 tmp |= (5 << 13);
5743 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
5744
5745 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5746 tmp &= ~0xFF;
5747 tmp |= 0x1C;
5748 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5749
5750 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5751 tmp &= ~0xFF;
5752 tmp |= 0x1C;
5753 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5754
5755 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5756 tmp &= ~(0xFF << 16);
5757 tmp |= (0x1C << 16);
5758 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5759
5760 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5761 tmp &= ~(0xFF << 16);
5762 tmp |= (0x1C << 16);
5763 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5764
0ff066a9
PZ
5765 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5766 tmp |= (1 << 27);
5767 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 5768
0ff066a9
PZ
5769 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5770 tmp |= (1 << 27);
5771 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 5772
0ff066a9
PZ
5773 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5774 tmp &= ~(0xF << 28);
5775 tmp |= (4 << 28);
5776 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 5777
0ff066a9
PZ
5778 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5779 tmp &= ~(0xF << 28);
5780 tmp |= (4 << 28);
5781 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
5782}
5783
2fa86a1f
PZ
5784/* Implements 3 different sequences from BSpec chapter "Display iCLK
5785 * Programming" based on the parameters passed:
5786 * - Sequence to enable CLKOUT_DP
5787 * - Sequence to enable CLKOUT_DP without spread
5788 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5789 */
5790static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5791 bool with_fdi)
f31f2d55
PZ
5792{
5793 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
5794 uint32_t reg, tmp;
5795
5796 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5797 with_spread = true;
5798 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5799 with_fdi, "LP PCH doesn't have FDI\n"))
5800 with_fdi = false;
f31f2d55
PZ
5801
5802 mutex_lock(&dev_priv->dpio_lock);
5803
5804 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5805 tmp &= ~SBI_SSCCTL_DISABLE;
5806 tmp |= SBI_SSCCTL_PATHALT;
5807 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5808
5809 udelay(24);
5810
2fa86a1f
PZ
5811 if (with_spread) {
5812 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5813 tmp &= ~SBI_SSCCTL_PATHALT;
5814 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 5815
2fa86a1f
PZ
5816 if (with_fdi) {
5817 lpt_reset_fdi_mphy(dev_priv);
5818 lpt_program_fdi_mphy(dev_priv);
5819 }
5820 }
dde86e2d 5821
2fa86a1f
PZ
5822 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5823 SBI_GEN0 : SBI_DBUFF0;
5824 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5825 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5826 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
5827
5828 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
5829}
5830
47701c3b
PZ
5831/* Sequence to disable CLKOUT_DP */
5832static void lpt_disable_clkout_dp(struct drm_device *dev)
5833{
5834 struct drm_i915_private *dev_priv = dev->dev_private;
5835 uint32_t reg, tmp;
5836
5837 mutex_lock(&dev_priv->dpio_lock);
5838
5839 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5840 SBI_GEN0 : SBI_DBUFF0;
5841 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5842 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5843 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5844
5845 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5846 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5847 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5848 tmp |= SBI_SSCCTL_PATHALT;
5849 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5850 udelay(32);
5851 }
5852 tmp |= SBI_SSCCTL_DISABLE;
5853 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5854 }
5855
5856 mutex_unlock(&dev_priv->dpio_lock);
5857}
5858
bf8fa3d3
PZ
5859static void lpt_init_pch_refclk(struct drm_device *dev)
5860{
5861 struct drm_mode_config *mode_config = &dev->mode_config;
5862 struct intel_encoder *encoder;
5863 bool has_vga = false;
5864
5865 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5866 switch (encoder->type) {
5867 case INTEL_OUTPUT_ANALOG:
5868 has_vga = true;
5869 break;
5870 }
5871 }
5872
47701c3b
PZ
5873 if (has_vga)
5874 lpt_enable_clkout_dp(dev, true, true);
5875 else
5876 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
5877}
5878
dde86e2d
PZ
5879/*
5880 * Initialize reference clocks when the driver loads
5881 */
5882void intel_init_pch_refclk(struct drm_device *dev)
5883{
5884 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5885 ironlake_init_pch_refclk(dev);
5886 else if (HAS_PCH_LPT(dev))
5887 lpt_init_pch_refclk(dev);
5888}
5889
d9d444cb
JB
5890static int ironlake_get_refclk(struct drm_crtc *crtc)
5891{
5892 struct drm_device *dev = crtc->dev;
5893 struct drm_i915_private *dev_priv = dev->dev_private;
5894 struct intel_encoder *encoder;
d9d444cb
JB
5895 int num_connectors = 0;
5896 bool is_lvds = false;
5897
6c2b7c12 5898 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
5899 switch (encoder->type) {
5900 case INTEL_OUTPUT_LVDS:
5901 is_lvds = true;
5902 break;
d9d444cb
JB
5903 }
5904 num_connectors++;
5905 }
5906
5907 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5908 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
41aa3448
RV
5909 dev_priv->vbt.lvds_ssc_freq);
5910 return dev_priv->vbt.lvds_ssc_freq * 1000;
d9d444cb
JB
5911 }
5912
5913 return 120000;
5914}
5915
6ff93609 5916static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 5917{
c8203565 5918 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
5919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5920 int pipe = intel_crtc->pipe;
c8203565
PZ
5921 uint32_t val;
5922
78114071 5923 val = 0;
c8203565 5924
965e0c48 5925 switch (intel_crtc->config.pipe_bpp) {
c8203565 5926 case 18:
dfd07d72 5927 val |= PIPECONF_6BPC;
c8203565
PZ
5928 break;
5929 case 24:
dfd07d72 5930 val |= PIPECONF_8BPC;
c8203565
PZ
5931 break;
5932 case 30:
dfd07d72 5933 val |= PIPECONF_10BPC;
c8203565
PZ
5934 break;
5935 case 36:
dfd07d72 5936 val |= PIPECONF_12BPC;
c8203565
PZ
5937 break;
5938 default:
cc769b62
PZ
5939 /* Case prevented by intel_choose_pipe_bpp_dither. */
5940 BUG();
c8203565
PZ
5941 }
5942
d8b32247 5943 if (intel_crtc->config.dither)
c8203565
PZ
5944 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5945
6ff93609 5946 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
5947 val |= PIPECONF_INTERLACED_ILK;
5948 else
5949 val |= PIPECONF_PROGRESSIVE;
5950
50f3b016 5951 if (intel_crtc->config.limited_color_range)
3685a8f3 5952 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 5953
c8203565
PZ
5954 I915_WRITE(PIPECONF(pipe), val);
5955 POSTING_READ(PIPECONF(pipe));
5956}
5957
86d3efce
VS
5958/*
5959 * Set up the pipe CSC unit.
5960 *
5961 * Currently only full range RGB to limited range RGB conversion
5962 * is supported, but eventually this should handle various
5963 * RGB<->YCbCr scenarios as well.
5964 */
50f3b016 5965static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
5966{
5967 struct drm_device *dev = crtc->dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
5969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5970 int pipe = intel_crtc->pipe;
5971 uint16_t coeff = 0x7800; /* 1.0 */
5972
5973 /*
5974 * TODO: Check what kind of values actually come out of the pipe
5975 * with these coeff/postoff values and adjust to get the best
5976 * accuracy. Perhaps we even need to take the bpc value into
5977 * consideration.
5978 */
5979
50f3b016 5980 if (intel_crtc->config.limited_color_range)
86d3efce
VS
5981 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5982
5983 /*
5984 * GY/GU and RY/RU should be the other way around according
5985 * to BSpec, but reality doesn't agree. Just set them up in
5986 * a way that results in the correct picture.
5987 */
5988 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5989 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5990
5991 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5992 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5993
5994 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5995 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5996
5997 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5998 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5999 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6000
6001 if (INTEL_INFO(dev)->gen > 6) {
6002 uint16_t postoff = 0;
6003
50f3b016 6004 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6005 postoff = (16 * (1 << 13) / 255) & 0x1fff;
6006
6007 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6008 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6009 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6010
6011 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6012 } else {
6013 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6014
50f3b016 6015 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6016 mode |= CSC_BLACK_SCREEN_OFFSET;
6017
6018 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6019 }
6020}
6021
6ff93609 6022static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6023{
756f85cf
PZ
6024 struct drm_device *dev = crtc->dev;
6025 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6027 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6028 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6029 uint32_t val;
6030
3eff4faa 6031 val = 0;
ee2b0b38 6032
756f85cf 6033 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6034 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6035
6ff93609 6036 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6037 val |= PIPECONF_INTERLACED_ILK;
6038 else
6039 val |= PIPECONF_PROGRESSIVE;
6040
702e7a56
PZ
6041 I915_WRITE(PIPECONF(cpu_transcoder), val);
6042 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6043
6044 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6045 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6046
6047 if (IS_BROADWELL(dev)) {
6048 val = 0;
6049
6050 switch (intel_crtc->config.pipe_bpp) {
6051 case 18:
6052 val |= PIPEMISC_DITHER_6_BPC;
6053 break;
6054 case 24:
6055 val |= PIPEMISC_DITHER_8_BPC;
6056 break;
6057 case 30:
6058 val |= PIPEMISC_DITHER_10_BPC;
6059 break;
6060 case 36:
6061 val |= PIPEMISC_DITHER_12_BPC;
6062 break;
6063 default:
6064 /* Case prevented by pipe_config_set_bpp. */
6065 BUG();
6066 }
6067
6068 if (intel_crtc->config.dither)
6069 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6070
6071 I915_WRITE(PIPEMISC(pipe), val);
6072 }
ee2b0b38
PZ
6073}
6074
6591c6e4 6075static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6076 intel_clock_t *clock,
6077 bool *has_reduced_clock,
6078 intel_clock_t *reduced_clock)
6079{
6080 struct drm_device *dev = crtc->dev;
6081 struct drm_i915_private *dev_priv = dev->dev_private;
6082 struct intel_encoder *intel_encoder;
6083 int refclk;
d4906093 6084 const intel_limit_t *limit;
a16af721 6085 bool ret, is_lvds = false;
79e53945 6086
6591c6e4
PZ
6087 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6088 switch (intel_encoder->type) {
79e53945
JB
6089 case INTEL_OUTPUT_LVDS:
6090 is_lvds = true;
6091 break;
79e53945
JB
6092 }
6093 }
6094
d9d444cb 6095 refclk = ironlake_get_refclk(crtc);
79e53945 6096
d4906093
ML
6097 /*
6098 * Returns a set of divisors for the desired target clock with the given
6099 * refclk, or FALSE. The returned values represent the clock equation:
6100 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6101 */
1b894b59 6102 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6103 ret = dev_priv->display.find_dpll(limit, crtc,
6104 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6105 refclk, NULL, clock);
6591c6e4
PZ
6106 if (!ret)
6107 return false;
cda4b7d3 6108
ddc9003c 6109 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6110 /*
6111 * Ensure we match the reduced clock's P to the target clock.
6112 * If the clocks don't match, we can't switch the display clock
6113 * by using the FP0/FP1. In such case we will disable the LVDS
6114 * downclock feature.
6115 */
ee9300bb
DV
6116 *has_reduced_clock =
6117 dev_priv->display.find_dpll(limit, crtc,
6118 dev_priv->lvds_downclock,
6119 refclk, clock,
6120 reduced_clock);
652c393a 6121 }
61e9653f 6122
6591c6e4
PZ
6123 return true;
6124}
6125
d4b1931c
PZ
6126int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6127{
6128 /*
6129 * Account for spread spectrum to avoid
6130 * oversubscribing the link. Max center spread
6131 * is 2.5%; use 5% for safety's sake.
6132 */
6133 u32 bps = target_clock * bpp * 21 / 20;
6134 return bps / (link_bw * 8) + 1;
6135}
6136
7429e9d4 6137static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 6138{
7429e9d4 6139 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
6140}
6141
de13a2e3 6142static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 6143 u32 *fp,
9a7c7890 6144 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 6145{
de13a2e3 6146 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
6147 struct drm_device *dev = crtc->dev;
6148 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
6149 struct intel_encoder *intel_encoder;
6150 uint32_t dpll;
6cc5f341 6151 int factor, num_connectors = 0;
09ede541 6152 bool is_lvds = false, is_sdvo = false;
79e53945 6153
de13a2e3
PZ
6154 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6155 switch (intel_encoder->type) {
79e53945
JB
6156 case INTEL_OUTPUT_LVDS:
6157 is_lvds = true;
6158 break;
6159 case INTEL_OUTPUT_SDVO:
7d57382e 6160 case INTEL_OUTPUT_HDMI:
79e53945 6161 is_sdvo = true;
79e53945 6162 break;
79e53945 6163 }
43565a06 6164
c751ce4f 6165 num_connectors++;
79e53945 6166 }
79e53945 6167
c1858123 6168 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
6169 factor = 21;
6170 if (is_lvds) {
6171 if ((intel_panel_use_ssc(dev_priv) &&
41aa3448 6172 dev_priv->vbt.lvds_ssc_freq == 100) ||
f0b44056 6173 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 6174 factor = 25;
09ede541 6175 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 6176 factor = 20;
c1858123 6177
7429e9d4 6178 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 6179 *fp |= FP_CB_TUNE;
2c07245f 6180
9a7c7890
DV
6181 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6182 *fp2 |= FP_CB_TUNE;
6183
5eddb70b 6184 dpll = 0;
2c07245f 6185
a07d6787
EA
6186 if (is_lvds)
6187 dpll |= DPLLB_MODE_LVDS;
6188 else
6189 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 6190
ef1b460d
DV
6191 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6192 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
6193
6194 if (is_sdvo)
4a33e48d 6195 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 6196 if (intel_crtc->config.has_dp_encoder)
4a33e48d 6197 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 6198
a07d6787 6199 /* compute bitmask from p1 value */
7429e9d4 6200 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 6201 /* also FPA1 */
7429e9d4 6202 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 6203
7429e9d4 6204 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
6205 case 5:
6206 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6207 break;
6208 case 7:
6209 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6210 break;
6211 case 10:
6212 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6213 break;
6214 case 14:
6215 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6216 break;
79e53945
JB
6217 }
6218
b4c09f3b 6219 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 6220 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
6221 else
6222 dpll |= PLL_REF_INPUT_DREFCLK;
6223
959e16d6 6224 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
6225}
6226
6227static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
6228 int x, int y,
6229 struct drm_framebuffer *fb)
6230{
6231 struct drm_device *dev = crtc->dev;
6232 struct drm_i915_private *dev_priv = dev->dev_private;
6233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6234 int pipe = intel_crtc->pipe;
6235 int plane = intel_crtc->plane;
6236 int num_connectors = 0;
6237 intel_clock_t clock, reduced_clock;
cbbab5bd 6238 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 6239 bool ok, has_reduced_clock = false;
8b47047b 6240 bool is_lvds = false;
de13a2e3 6241 struct intel_encoder *encoder;
e2b78267 6242 struct intel_shared_dpll *pll;
de13a2e3 6243 int ret;
de13a2e3
PZ
6244
6245 for_each_encoder_on_crtc(dev, crtc, encoder) {
6246 switch (encoder->type) {
6247 case INTEL_OUTPUT_LVDS:
6248 is_lvds = true;
6249 break;
de13a2e3
PZ
6250 }
6251
6252 num_connectors++;
a07d6787 6253 }
79e53945 6254
5dc5298b
PZ
6255 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6256 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 6257
ff9a6750 6258 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 6259 &has_reduced_clock, &reduced_clock);
ee9300bb 6260 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
6261 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6262 return -EINVAL;
79e53945 6263 }
f47709a9
DV
6264 /* Compat-code for transition, will disappear. */
6265 if (!intel_crtc->config.clock_set) {
6266 intel_crtc->config.dpll.n = clock.n;
6267 intel_crtc->config.dpll.m1 = clock.m1;
6268 intel_crtc->config.dpll.m2 = clock.m2;
6269 intel_crtc->config.dpll.p1 = clock.p1;
6270 intel_crtc->config.dpll.p2 = clock.p2;
6271 }
79e53945 6272
5dc5298b 6273 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 6274 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 6275 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 6276 if (has_reduced_clock)
7429e9d4 6277 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 6278
7429e9d4 6279 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
6280 &fp, &reduced_clock,
6281 has_reduced_clock ? &fp2 : NULL);
6282
959e16d6 6283 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
6284 intel_crtc->config.dpll_hw_state.fp0 = fp;
6285 if (has_reduced_clock)
6286 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6287 else
6288 intel_crtc->config.dpll_hw_state.fp1 = fp;
6289
b89a1d39 6290 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 6291 if (pll == NULL) {
84f44ce7
VS
6292 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6293 pipe_name(pipe));
4b645f14
JB
6294 return -EINVAL;
6295 }
ee7b9f93 6296 } else
e72f9fbf 6297 intel_put_shared_dpll(intel_crtc);
79e53945 6298
03afc4a2
DV
6299 if (intel_crtc->config.has_dp_encoder)
6300 intel_dp_set_m_n(intel_crtc);
79e53945 6301
bcd644e0
DV
6302 if (is_lvds && has_reduced_clock && i915_powersave)
6303 intel_crtc->lowfreq_avail = true;
6304 else
6305 intel_crtc->lowfreq_avail = false;
e2b78267 6306
8a654f3b 6307 intel_set_pipe_timings(intel_crtc);
5eddb70b 6308
ca3a0ff8 6309 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
6310 intel_cpu_transcoder_set_m_n(intel_crtc,
6311 &intel_crtc->config.fdi_m_n);
6312 }
2c07245f 6313
6ff93609 6314 ironlake_set_pipeconf(crtc);
79e53945 6315
a1f9e77e
PZ
6316 /* Set up the display plane register */
6317 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
b24e7179 6318 POSTING_READ(DSPCNTR(plane));
79e53945 6319
94352cf9 6320 ret = intel_pipe_set_base(crtc, x, y, fb);
7662c8bd 6321
1857e1da 6322 return ret;
79e53945
JB
6323}
6324
eb14cb74
VS
6325static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6326 struct intel_link_m_n *m_n)
6327{
6328 struct drm_device *dev = crtc->base.dev;
6329 struct drm_i915_private *dev_priv = dev->dev_private;
6330 enum pipe pipe = crtc->pipe;
6331
6332 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6333 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6334 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6335 & ~TU_SIZE_MASK;
6336 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6337 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6338 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6339}
6340
6341static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6342 enum transcoder transcoder,
6343 struct intel_link_m_n *m_n)
72419203
DV
6344{
6345 struct drm_device *dev = crtc->base.dev;
6346 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 6347 enum pipe pipe = crtc->pipe;
72419203 6348
eb14cb74
VS
6349 if (INTEL_INFO(dev)->gen >= 5) {
6350 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6351 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6352 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6353 & ~TU_SIZE_MASK;
6354 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6355 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6356 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6357 } else {
6358 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6359 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6360 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6361 & ~TU_SIZE_MASK;
6362 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6363 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6364 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6365 }
6366}
6367
6368void intel_dp_get_m_n(struct intel_crtc *crtc,
6369 struct intel_crtc_config *pipe_config)
6370{
6371 if (crtc->config.has_pch_encoder)
6372 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6373 else
6374 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6375 &pipe_config->dp_m_n);
6376}
72419203 6377
eb14cb74
VS
6378static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6379 struct intel_crtc_config *pipe_config)
6380{
6381 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6382 &pipe_config->fdi_m_n);
72419203
DV
6383}
6384
2fa2fe9a
DV
6385static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6386 struct intel_crtc_config *pipe_config)
6387{
6388 struct drm_device *dev = crtc->base.dev;
6389 struct drm_i915_private *dev_priv = dev->dev_private;
6390 uint32_t tmp;
6391
6392 tmp = I915_READ(PF_CTL(crtc->pipe));
6393
6394 if (tmp & PF_ENABLE) {
fd4daa9c 6395 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
6396 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6397 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
6398
6399 /* We currently do not free assignements of panel fitters on
6400 * ivb/hsw (since we don't use the higher upscaling modes which
6401 * differentiates them) so just WARN about this case for now. */
6402 if (IS_GEN7(dev)) {
6403 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6404 PF_PIPE_SEL_IVB(crtc->pipe));
6405 }
2fa2fe9a 6406 }
79e53945
JB
6407}
6408
0e8ffe1b
DV
6409static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6410 struct intel_crtc_config *pipe_config)
6411{
6412 struct drm_device *dev = crtc->base.dev;
6413 struct drm_i915_private *dev_priv = dev->dev_private;
6414 uint32_t tmp;
6415
e143a21c 6416 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6417 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6418
0e8ffe1b
DV
6419 tmp = I915_READ(PIPECONF(crtc->pipe));
6420 if (!(tmp & PIPECONF_ENABLE))
6421 return false;
6422
42571aef
VS
6423 switch (tmp & PIPECONF_BPC_MASK) {
6424 case PIPECONF_6BPC:
6425 pipe_config->pipe_bpp = 18;
6426 break;
6427 case PIPECONF_8BPC:
6428 pipe_config->pipe_bpp = 24;
6429 break;
6430 case PIPECONF_10BPC:
6431 pipe_config->pipe_bpp = 30;
6432 break;
6433 case PIPECONF_12BPC:
6434 pipe_config->pipe_bpp = 36;
6435 break;
6436 default:
6437 break;
6438 }
6439
ab9412ba 6440 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
6441 struct intel_shared_dpll *pll;
6442
88adfff1
DV
6443 pipe_config->has_pch_encoder = true;
6444
627eb5a3
DV
6445 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6446 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6447 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
6448
6449 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 6450
c0d43d62 6451 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
6452 pipe_config->shared_dpll =
6453 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
6454 } else {
6455 tmp = I915_READ(PCH_DPLL_SEL);
6456 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6457 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6458 else
6459 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6460 }
66e985c0
DV
6461
6462 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6463
6464 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6465 &pipe_config->dpll_hw_state));
c93f54cf
DV
6466
6467 tmp = pipe_config->dpll_hw_state.dpll;
6468 pipe_config->pixel_multiplier =
6469 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6470 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
6471
6472 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
6473 } else {
6474 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
6475 }
6476
1bd1bd80
DV
6477 intel_get_pipe_timings(crtc, pipe_config);
6478
2fa2fe9a
DV
6479 ironlake_get_pfit_config(crtc, pipe_config);
6480
0e8ffe1b
DV
6481 return true;
6482}
6483
be256dc7
PZ
6484static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6485{
6486 struct drm_device *dev = dev_priv->dev;
6487 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6488 struct intel_crtc *crtc;
6489 unsigned long irqflags;
bd633a7c 6490 uint32_t val;
be256dc7
PZ
6491
6492 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6493 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6494 pipe_name(crtc->pipe));
6495
6496 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6497 WARN(plls->spll_refcount, "SPLL enabled\n");
6498 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6499 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6500 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6501 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6502 "CPU PWM1 enabled\n");
6503 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6504 "CPU PWM2 enabled\n");
6505 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6506 "PCH PWM1 enabled\n");
6507 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6508 "Utility pin enabled\n");
6509 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6510
6511 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6512 val = I915_READ(DEIMR);
6513 WARN((val & ~DE_PCH_EVENT_IVB) != val,
6514 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6515 val = I915_READ(SDEIMR);
bd633a7c 6516 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
be256dc7
PZ
6517 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6518 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6519}
6520
6521/*
6522 * This function implements pieces of two sequences from BSpec:
6523 * - Sequence for display software to disable LCPLL
6524 * - Sequence for display software to allow package C8+
6525 * The steps implemented here are just the steps that actually touch the LCPLL
6526 * register. Callers should take care of disabling all the display engine
6527 * functions, doing the mode unset, fixing interrupts, etc.
6528 */
6ff58d53
PZ
6529static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6530 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
6531{
6532 uint32_t val;
6533
6534 assert_can_disable_lcpll(dev_priv);
6535
6536 val = I915_READ(LCPLL_CTL);
6537
6538 if (switch_to_fclk) {
6539 val |= LCPLL_CD_SOURCE_FCLK;
6540 I915_WRITE(LCPLL_CTL, val);
6541
6542 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6543 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6544 DRM_ERROR("Switching to FCLK failed\n");
6545
6546 val = I915_READ(LCPLL_CTL);
6547 }
6548
6549 val |= LCPLL_PLL_DISABLE;
6550 I915_WRITE(LCPLL_CTL, val);
6551 POSTING_READ(LCPLL_CTL);
6552
6553 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6554 DRM_ERROR("LCPLL still locked\n");
6555
6556 val = I915_READ(D_COMP);
6557 val |= D_COMP_COMP_DISABLE;
515b2392
PZ
6558 mutex_lock(&dev_priv->rps.hw_lock);
6559 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6560 DRM_ERROR("Failed to disable D_COMP\n");
6561 mutex_unlock(&dev_priv->rps.hw_lock);
be256dc7
PZ
6562 POSTING_READ(D_COMP);
6563 ndelay(100);
6564
6565 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6566 DRM_ERROR("D_COMP RCOMP still in progress\n");
6567
6568 if (allow_power_down) {
6569 val = I915_READ(LCPLL_CTL);
6570 val |= LCPLL_POWER_DOWN_ALLOW;
6571 I915_WRITE(LCPLL_CTL, val);
6572 POSTING_READ(LCPLL_CTL);
6573 }
6574}
6575
6576/*
6577 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6578 * source.
6579 */
6ff58d53 6580static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
6581{
6582 uint32_t val;
6583
6584 val = I915_READ(LCPLL_CTL);
6585
6586 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6587 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6588 return;
6589
215733fa
PZ
6590 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6591 * we'll hang the machine! */
c8d9a590 6592 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
215733fa 6593
be256dc7
PZ
6594 if (val & LCPLL_POWER_DOWN_ALLOW) {
6595 val &= ~LCPLL_POWER_DOWN_ALLOW;
6596 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 6597 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
6598 }
6599
6600 val = I915_READ(D_COMP);
6601 val |= D_COMP_COMP_FORCE;
6602 val &= ~D_COMP_COMP_DISABLE;
515b2392
PZ
6603 mutex_lock(&dev_priv->rps.hw_lock);
6604 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6605 DRM_ERROR("Failed to enable D_COMP\n");
6606 mutex_unlock(&dev_priv->rps.hw_lock);
35d8f2eb 6607 POSTING_READ(D_COMP);
be256dc7
PZ
6608
6609 val = I915_READ(LCPLL_CTL);
6610 val &= ~LCPLL_PLL_DISABLE;
6611 I915_WRITE(LCPLL_CTL, val);
6612
6613 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6614 DRM_ERROR("LCPLL not locked yet\n");
6615
6616 if (val & LCPLL_CD_SOURCE_FCLK) {
6617 val = I915_READ(LCPLL_CTL);
6618 val &= ~LCPLL_CD_SOURCE_FCLK;
6619 I915_WRITE(LCPLL_CTL, val);
6620
6621 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6622 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6623 DRM_ERROR("Switching back to LCPLL failed\n");
6624 }
215733fa 6625
c8d9a590 6626 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
6627}
6628
c67a470b
PZ
6629void hsw_enable_pc8_work(struct work_struct *__work)
6630{
6631 struct drm_i915_private *dev_priv =
6632 container_of(to_delayed_work(__work), struct drm_i915_private,
6633 pc8.enable_work);
6634 struct drm_device *dev = dev_priv->dev;
6635 uint32_t val;
6636
6637 if (dev_priv->pc8.enabled)
6638 return;
6639
6640 DRM_DEBUG_KMS("Enabling package C8+\n");
6641
6642 dev_priv->pc8.enabled = true;
6643
6644 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6645 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6646 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6647 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6648 }
6649
6650 lpt_disable_clkout_dp(dev);
6651 hsw_pc8_disable_interrupts(dev);
6652 hsw_disable_lcpll(dev_priv, true, true);
6653}
6654
6655static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6656{
6657 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6658 WARN(dev_priv->pc8.disable_count < 1,
6659 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6660
6661 dev_priv->pc8.disable_count--;
6662 if (dev_priv->pc8.disable_count != 0)
6663 return;
6664
6665 schedule_delayed_work(&dev_priv->pc8.enable_work,
90058745 6666 msecs_to_jiffies(i915_pc8_timeout));
c67a470b
PZ
6667}
6668
6669static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6670{
6671 struct drm_device *dev = dev_priv->dev;
6672 uint32_t val;
6673
6674 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6675 WARN(dev_priv->pc8.disable_count < 0,
6676 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6677
6678 dev_priv->pc8.disable_count++;
6679 if (dev_priv->pc8.disable_count != 1)
6680 return;
6681
6682 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6683 if (!dev_priv->pc8.enabled)
6684 return;
6685
6686 DRM_DEBUG_KMS("Disabling package C8+\n");
6687
6688 hsw_restore_lcpll(dev_priv);
6689 hsw_pc8_restore_interrupts(dev);
6690 lpt_init_pch_refclk(dev);
6691
6692 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6693 val = I915_READ(SOUTH_DSPCLK_GATE_D);
6694 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6695 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6696 }
6697
6698 intel_prepare_ddi(dev);
6699 i915_gem_init_swizzling(dev);
6700 mutex_lock(&dev_priv->rps.hw_lock);
6701 gen6_update_ring_freq(dev);
6702 mutex_unlock(&dev_priv->rps.hw_lock);
6703 dev_priv->pc8.enabled = false;
6704}
6705
6706void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6707{
6708 mutex_lock(&dev_priv->pc8.lock);
6709 __hsw_enable_package_c8(dev_priv);
6710 mutex_unlock(&dev_priv->pc8.lock);
6711}
6712
6713void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6714{
6715 mutex_lock(&dev_priv->pc8.lock);
6716 __hsw_disable_package_c8(dev_priv);
6717 mutex_unlock(&dev_priv->pc8.lock);
6718}
6719
6720static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6721{
6722 struct drm_device *dev = dev_priv->dev;
6723 struct intel_crtc *crtc;
6724 uint32_t val;
6725
6726 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6727 if (crtc->base.enabled)
6728 return false;
6729
6730 /* This case is still possible since we have the i915.disable_power_well
6731 * parameter and also the KVMr or something else might be requesting the
6732 * power well. */
6733 val = I915_READ(HSW_PWR_WELL_DRIVER);
6734 if (val != 0) {
6735 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6736 return false;
6737 }
6738
6739 return true;
6740}
6741
6742/* Since we're called from modeset_global_resources there's no way to
6743 * symmetrically increase and decrease the refcount, so we use
6744 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6745 * or not.
6746 */
6747static void hsw_update_package_c8(struct drm_device *dev)
6748{
6749 struct drm_i915_private *dev_priv = dev->dev_private;
6750 bool allow;
6751
6752 if (!i915_enable_pc8)
6753 return;
6754
6755 mutex_lock(&dev_priv->pc8.lock);
6756
6757 allow = hsw_can_enable_package_c8(dev_priv);
6758
6759 if (allow == dev_priv->pc8.requirements_met)
6760 goto done;
6761
6762 dev_priv->pc8.requirements_met = allow;
6763
6764 if (allow)
6765 __hsw_enable_package_c8(dev_priv);
6766 else
6767 __hsw_disable_package_c8(dev_priv);
6768
6769done:
6770 mutex_unlock(&dev_priv->pc8.lock);
6771}
6772
6773static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6774{
3458122e 6775 mutex_lock(&dev_priv->pc8.lock);
c67a470b
PZ
6776 if (!dev_priv->pc8.gpu_idle) {
6777 dev_priv->pc8.gpu_idle = true;
3458122e 6778 __hsw_enable_package_c8(dev_priv);
c67a470b 6779 }
3458122e 6780 mutex_unlock(&dev_priv->pc8.lock);
c67a470b
PZ
6781}
6782
6783static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6784{
3458122e 6785 mutex_lock(&dev_priv->pc8.lock);
c67a470b
PZ
6786 if (dev_priv->pc8.gpu_idle) {
6787 dev_priv->pc8.gpu_idle = false;
3458122e 6788 __hsw_disable_package_c8(dev_priv);
c67a470b 6789 }
3458122e 6790 mutex_unlock(&dev_priv->pc8.lock);
be256dc7
PZ
6791}
6792
6efdf354
ID
6793#define for_each_power_domain(domain, mask) \
6794 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
6795 if ((1 << (domain)) & (mask))
6796
6797static unsigned long get_pipe_power_domains(struct drm_device *dev,
6798 enum pipe pipe, bool pfit_enabled)
6799{
6800 unsigned long mask;
6801 enum transcoder transcoder;
6802
6803 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
6804
6805 mask = BIT(POWER_DOMAIN_PIPE(pipe));
6806 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6807 if (pfit_enabled)
6808 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6809
6810 return mask;
6811}
6812
baa70707
ID
6813void intel_display_set_init_power(struct drm_device *dev, bool enable)
6814{
6815 struct drm_i915_private *dev_priv = dev->dev_private;
6816
6817 if (dev_priv->power_domains.init_power_on == enable)
6818 return;
6819
6820 if (enable)
6821 intel_display_power_get(dev, POWER_DOMAIN_INIT);
6822 else
6823 intel_display_power_put(dev, POWER_DOMAIN_INIT);
6824
6825 dev_priv->power_domains.init_power_on = enable;
6826}
6827
4f074129 6828static void modeset_update_power_wells(struct drm_device *dev)
d6dd9eb1 6829{
6efdf354 6830 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
d6dd9eb1 6831 struct intel_crtc *crtc;
d6dd9eb1 6832
6efdf354
ID
6833 /*
6834 * First get all needed power domains, then put all unneeded, to avoid
6835 * any unnecessary toggling of the power wells.
6836 */
d6dd9eb1 6837 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6efdf354
ID
6838 enum intel_display_power_domain domain;
6839
e7a639c4
DV
6840 if (!crtc->base.enabled)
6841 continue;
d6dd9eb1 6842
6efdf354
ID
6843 pipe_domains[crtc->pipe] = get_pipe_power_domains(dev,
6844 crtc->pipe,
6845 crtc->config.pch_pfit.enabled);
6846
6847 for_each_power_domain(domain, pipe_domains[crtc->pipe])
6848 intel_display_power_get(dev, domain);
d6dd9eb1
DV
6849 }
6850
6efdf354
ID
6851 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6852 enum intel_display_power_domain domain;
6853
6854 for_each_power_domain(domain, crtc->enabled_power_domains)
6855 intel_display_power_put(dev, domain);
6856
6857 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
6858 }
baa70707
ID
6859
6860 intel_display_set_init_power(dev, false);
4f074129 6861}
c67a470b 6862
4f074129
ID
6863static void haswell_modeset_global_resources(struct drm_device *dev)
6864{
6865 modeset_update_power_wells(dev);
c67a470b 6866 hsw_update_package_c8(dev);
d6dd9eb1
DV
6867}
6868
09b4ddf9 6869static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
6870 int x, int y,
6871 struct drm_framebuffer *fb)
6872{
6873 struct drm_device *dev = crtc->dev;
6874 struct drm_i915_private *dev_priv = dev->dev_private;
6875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 6876 int plane = intel_crtc->plane;
09b4ddf9 6877 int ret;
09b4ddf9 6878
ff9a6750 6879 if (!intel_ddi_pll_mode_set(crtc))
6441ab5f
PZ
6880 return -EINVAL;
6881
03afc4a2
DV
6882 if (intel_crtc->config.has_dp_encoder)
6883 intel_dp_set_m_n(intel_crtc);
09b4ddf9
PZ
6884
6885 intel_crtc->lowfreq_avail = false;
09b4ddf9 6886
8a654f3b 6887 intel_set_pipe_timings(intel_crtc);
09b4ddf9 6888
ca3a0ff8 6889 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
6890 intel_cpu_transcoder_set_m_n(intel_crtc,
6891 &intel_crtc->config.fdi_m_n);
6892 }
09b4ddf9 6893
6ff93609 6894 haswell_set_pipeconf(crtc);
09b4ddf9 6895
50f3b016 6896 intel_set_pipe_csc(crtc);
86d3efce 6897
09b4ddf9 6898 /* Set up the display plane register */
86d3efce 6899 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
09b4ddf9
PZ
6900 POSTING_READ(DSPCNTR(plane));
6901
6902 ret = intel_pipe_set_base(crtc, x, y, fb);
6903
1f803ee5 6904 return ret;
79e53945
JB
6905}
6906
0e8ffe1b
DV
6907static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6908 struct intel_crtc_config *pipe_config)
6909{
6910 struct drm_device *dev = crtc->base.dev;
6911 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 6912 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
6913 uint32_t tmp;
6914
e143a21c 6915 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
6916 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6917
eccb140b
DV
6918 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6919 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6920 enum pipe trans_edp_pipe;
6921 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6922 default:
6923 WARN(1, "unknown pipe linked to edp transcoder\n");
6924 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6925 case TRANS_DDI_EDP_INPUT_A_ON:
6926 trans_edp_pipe = PIPE_A;
6927 break;
6928 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6929 trans_edp_pipe = PIPE_B;
6930 break;
6931 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6932 trans_edp_pipe = PIPE_C;
6933 break;
6934 }
6935
6936 if (trans_edp_pipe == crtc->pipe)
6937 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6938 }
6939
b97186f0 6940 if (!intel_display_power_enabled(dev,
eccb140b 6941 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
6942 return false;
6943
eccb140b 6944 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
6945 if (!(tmp & PIPECONF_ENABLE))
6946 return false;
6947
88adfff1 6948 /*
f196e6be 6949 * Haswell has only FDI/PCH transcoder A. It is which is connected to
88adfff1
DV
6950 * DDI E. So just check whether this pipe is wired to DDI E and whether
6951 * the PCH transcoder is on.
6952 */
eccb140b 6953 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
88adfff1 6954 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
ab9412ba 6955 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
88adfff1
DV
6956 pipe_config->has_pch_encoder = true;
6957
627eb5a3
DV
6958 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6959 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6960 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
6961
6962 ironlake_get_fdi_m_n_config(crtc, pipe_config);
627eb5a3
DV
6963 }
6964
1bd1bd80
DV
6965 intel_get_pipe_timings(crtc, pipe_config);
6966
2fa2fe9a
DV
6967 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6968 if (intel_display_power_enabled(dev, pfit_domain))
6969 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 6970
42db64ef
PZ
6971 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6972 (I915_READ(IPS_CTL) & IPS_ENABLE);
6973
6c49f241
DV
6974 pipe_config->pixel_multiplier = 1;
6975
0e8ffe1b
DV
6976 return true;
6977}
6978
f564048e 6979static int intel_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6980 int x, int y,
94352cf9 6981 struct drm_framebuffer *fb)
f564048e
EA
6982{
6983 struct drm_device *dev = crtc->dev;
6984 struct drm_i915_private *dev_priv = dev->dev_private;
9256aa19 6985 struct intel_encoder *encoder;
0b701d27 6986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b8cecdf5 6987 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
0b701d27 6988 int pipe = intel_crtc->pipe;
f564048e
EA
6989 int ret;
6990
0b701d27 6991 drm_vblank_pre_modeset(dev, pipe);
7662c8bd 6992
b8cecdf5
DV
6993 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6994
79e53945 6995 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 6996
9256aa19
DV
6997 if (ret != 0)
6998 return ret;
6999
7000 for_each_encoder_on_crtc(dev, crtc, encoder) {
7001 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7002 encoder->base.base.id,
7003 drm_get_encoder_name(&encoder->base),
7004 mode->base.id, mode->name);
36f2d1f1 7005 encoder->mode_set(encoder);
9256aa19
DV
7006 }
7007
7008 return 0;
79e53945
JB
7009}
7010
1a91510d
JN
7011static struct {
7012 int clock;
7013 u32 config;
7014} hdmi_audio_clock[] = {
7015 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7016 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7017 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7018 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7019 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7020 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7021 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7022 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7023 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7024 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7025};
7026
7027/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7028static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7029{
7030 int i;
7031
7032 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7033 if (mode->clock == hdmi_audio_clock[i].clock)
7034 break;
7035 }
7036
7037 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7038 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7039 i = 1;
7040 }
7041
7042 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7043 hdmi_audio_clock[i].clock,
7044 hdmi_audio_clock[i].config);
7045
7046 return hdmi_audio_clock[i].config;
7047}
7048
3a9627f4
WF
7049static bool intel_eld_uptodate(struct drm_connector *connector,
7050 int reg_eldv, uint32_t bits_eldv,
7051 int reg_elda, uint32_t bits_elda,
7052 int reg_edid)
7053{
7054 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7055 uint8_t *eld = connector->eld;
7056 uint32_t i;
7057
7058 i = I915_READ(reg_eldv);
7059 i &= bits_eldv;
7060
7061 if (!eld[0])
7062 return !i;
7063
7064 if (!i)
7065 return false;
7066
7067 i = I915_READ(reg_elda);
7068 i &= ~bits_elda;
7069 I915_WRITE(reg_elda, i);
7070
7071 for (i = 0; i < eld[2]; i++)
7072 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7073 return false;
7074
7075 return true;
7076}
7077
e0dac65e 7078static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7079 struct drm_crtc *crtc,
7080 struct drm_display_mode *mode)
e0dac65e
WF
7081{
7082 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7083 uint8_t *eld = connector->eld;
7084 uint32_t eldv;
7085 uint32_t len;
7086 uint32_t i;
7087
7088 i = I915_READ(G4X_AUD_VID_DID);
7089
7090 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7091 eldv = G4X_ELDV_DEVCL_DEVBLC;
7092 else
7093 eldv = G4X_ELDV_DEVCTG;
7094
3a9627f4
WF
7095 if (intel_eld_uptodate(connector,
7096 G4X_AUD_CNTL_ST, eldv,
7097 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7098 G4X_HDMIW_HDMIEDID))
7099 return;
7100
e0dac65e
WF
7101 i = I915_READ(G4X_AUD_CNTL_ST);
7102 i &= ~(eldv | G4X_ELD_ADDR);
7103 len = (i >> 9) & 0x1f; /* ELD buffer size */
7104 I915_WRITE(G4X_AUD_CNTL_ST, i);
7105
7106 if (!eld[0])
7107 return;
7108
7109 len = min_t(uint8_t, eld[2], len);
7110 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7111 for (i = 0; i < len; i++)
7112 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7113
7114 i = I915_READ(G4X_AUD_CNTL_ST);
7115 i |= eldv;
7116 I915_WRITE(G4X_AUD_CNTL_ST, i);
7117}
7118
83358c85 7119static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7120 struct drm_crtc *crtc,
7121 struct drm_display_mode *mode)
83358c85
WX
7122{
7123 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7124 uint8_t *eld = connector->eld;
7125 struct drm_device *dev = crtc->dev;
7b9f35a6 7126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83358c85
WX
7127 uint32_t eldv;
7128 uint32_t i;
7129 int len;
7130 int pipe = to_intel_crtc(crtc)->pipe;
7131 int tmp;
7132
7133 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7134 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7135 int aud_config = HSW_AUD_CFG(pipe);
7136 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7137
7138
7139 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7140
7141 /* Audio output enable */
7142 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7143 tmp = I915_READ(aud_cntrl_st2);
7144 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7145 I915_WRITE(aud_cntrl_st2, tmp);
7146
7147 /* Wait for 1 vertical blank */
7148 intel_wait_for_vblank(dev, pipe);
7149
7150 /* Set ELD valid state */
7151 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7152 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7153 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7154 I915_WRITE(aud_cntrl_st2, tmp);
7155 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7156 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7157
7158 /* Enable HDMI mode */
7159 tmp = I915_READ(aud_config);
7e7cb34f 7160 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7161 /* clear N_programing_enable and N_value_index */
7162 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7163 I915_WRITE(aud_config, tmp);
7164
7165 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7166
7167 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7b9f35a6 7168 intel_crtc->eld_vld = true;
83358c85
WX
7169
7170 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7171 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7172 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7173 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7174 } else {
7175 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7176 }
83358c85
WX
7177
7178 if (intel_eld_uptodate(connector,
7179 aud_cntrl_st2, eldv,
7180 aud_cntl_st, IBX_ELD_ADDRESS,
7181 hdmiw_hdmiedid))
7182 return;
7183
7184 i = I915_READ(aud_cntrl_st2);
7185 i &= ~eldv;
7186 I915_WRITE(aud_cntrl_st2, i);
7187
7188 if (!eld[0])
7189 return;
7190
7191 i = I915_READ(aud_cntl_st);
7192 i &= ~IBX_ELD_ADDRESS;
7193 I915_WRITE(aud_cntl_st, i);
7194 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7195 DRM_DEBUG_DRIVER("port num:%d\n", i);
7196
7197 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7198 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7199 for (i = 0; i < len; i++)
7200 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7201
7202 i = I915_READ(aud_cntrl_st2);
7203 i |= eldv;
7204 I915_WRITE(aud_cntrl_st2, i);
7205
7206}
7207
e0dac65e 7208static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7209 struct drm_crtc *crtc,
7210 struct drm_display_mode *mode)
e0dac65e
WF
7211{
7212 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7213 uint8_t *eld = connector->eld;
7214 uint32_t eldv;
7215 uint32_t i;
7216 int len;
7217 int hdmiw_hdmiedid;
b6daa025 7218 int aud_config;
e0dac65e
WF
7219 int aud_cntl_st;
7220 int aud_cntrl_st2;
9b138a83 7221 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7222
b3f33cbf 7223 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7224 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7225 aud_config = IBX_AUD_CFG(pipe);
7226 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 7227 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
7228 } else if (IS_VALLEYVIEW(connector->dev)) {
7229 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7230 aud_config = VLV_AUD_CFG(pipe);
7231 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7232 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 7233 } else {
9b138a83
WX
7234 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7235 aud_config = CPT_AUD_CFG(pipe);
7236 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 7237 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
7238 }
7239
9b138a83 7240 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 7241
9ca2fe73
ML
7242 if (IS_VALLEYVIEW(connector->dev)) {
7243 struct intel_encoder *intel_encoder;
7244 struct intel_digital_port *intel_dig_port;
7245
7246 intel_encoder = intel_attached_encoder(connector);
7247 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7248 i = intel_dig_port->port;
7249 } else {
7250 i = I915_READ(aud_cntl_st);
7251 i = (i >> 29) & DIP_PORT_SEL_MASK;
7252 /* DIP_Port_Select, 0x1 = PortB */
7253 }
7254
e0dac65e
WF
7255 if (!i) {
7256 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7257 /* operate blindly on all ports */
1202b4c6
WF
7258 eldv = IBX_ELD_VALIDB;
7259 eldv |= IBX_ELD_VALIDB << 4;
7260 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 7261 } else {
2582a850 7262 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 7263 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
7264 }
7265
3a9627f4
WF
7266 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7267 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7268 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 7269 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7270 } else {
7271 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7272 }
e0dac65e 7273
3a9627f4
WF
7274 if (intel_eld_uptodate(connector,
7275 aud_cntrl_st2, eldv,
7276 aud_cntl_st, IBX_ELD_ADDRESS,
7277 hdmiw_hdmiedid))
7278 return;
7279
e0dac65e
WF
7280 i = I915_READ(aud_cntrl_st2);
7281 i &= ~eldv;
7282 I915_WRITE(aud_cntrl_st2, i);
7283
7284 if (!eld[0])
7285 return;
7286
e0dac65e 7287 i = I915_READ(aud_cntl_st);
1202b4c6 7288 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
7289 I915_WRITE(aud_cntl_st, i);
7290
7291 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7292 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7293 for (i = 0; i < len; i++)
7294 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7295
7296 i = I915_READ(aud_cntrl_st2);
7297 i |= eldv;
7298 I915_WRITE(aud_cntrl_st2, i);
7299}
7300
7301void intel_write_eld(struct drm_encoder *encoder,
7302 struct drm_display_mode *mode)
7303{
7304 struct drm_crtc *crtc = encoder->crtc;
7305 struct drm_connector *connector;
7306 struct drm_device *dev = encoder->dev;
7307 struct drm_i915_private *dev_priv = dev->dev_private;
7308
7309 connector = drm_select_eld(encoder, mode);
7310 if (!connector)
7311 return;
7312
7313 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7314 connector->base.id,
7315 drm_get_connector_name(connector),
7316 connector->encoder->base.id,
7317 drm_get_encoder_name(connector->encoder));
7318
7319 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7320
7321 if (dev_priv->display.write_eld)
34427052 7322 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
7323}
7324
560b85bb
CW
7325static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7326{
7327 struct drm_device *dev = crtc->dev;
7328 struct drm_i915_private *dev_priv = dev->dev_private;
7329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7330 bool visible = base != 0;
7331 u32 cntl;
7332
7333 if (intel_crtc->cursor_visible == visible)
7334 return;
7335
9db4a9c7 7336 cntl = I915_READ(_CURACNTR);
560b85bb
CW
7337 if (visible) {
7338 /* On these chipsets we can only modify the base whilst
7339 * the cursor is disabled.
7340 */
9db4a9c7 7341 I915_WRITE(_CURABASE, base);
560b85bb
CW
7342
7343 cntl &= ~(CURSOR_FORMAT_MASK);
7344 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7345 cntl |= CURSOR_ENABLE |
7346 CURSOR_GAMMA_ENABLE |
7347 CURSOR_FORMAT_ARGB;
7348 } else
7349 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
9db4a9c7 7350 I915_WRITE(_CURACNTR, cntl);
560b85bb
CW
7351
7352 intel_crtc->cursor_visible = visible;
7353}
7354
7355static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7356{
7357 struct drm_device *dev = crtc->dev;
7358 struct drm_i915_private *dev_priv = dev->dev_private;
7359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7360 int pipe = intel_crtc->pipe;
7361 bool visible = base != 0;
7362
7363 if (intel_crtc->cursor_visible != visible) {
548f245b 7364 uint32_t cntl = I915_READ(CURCNTR(pipe));
560b85bb
CW
7365 if (base) {
7366 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7367 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7368 cntl |= pipe << 28; /* Connect to correct pipe */
7369 } else {
7370 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7371 cntl |= CURSOR_MODE_DISABLE;
7372 }
9db4a9c7 7373 I915_WRITE(CURCNTR(pipe), cntl);
560b85bb
CW
7374
7375 intel_crtc->cursor_visible = visible;
7376 }
7377 /* and commit changes on next vblank */
9db4a9c7 7378 I915_WRITE(CURBASE(pipe), base);
560b85bb
CW
7379}
7380
65a21cd6
JB
7381static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7382{
7383 struct drm_device *dev = crtc->dev;
7384 struct drm_i915_private *dev_priv = dev->dev_private;
7385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7386 int pipe = intel_crtc->pipe;
7387 bool visible = base != 0;
7388
7389 if (intel_crtc->cursor_visible != visible) {
7390 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7391 if (base) {
7392 cntl &= ~CURSOR_MODE;
7393 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7394 } else {
7395 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7396 cntl |= CURSOR_MODE_DISABLE;
7397 }
6bbfa1c5 7398 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
86d3efce 7399 cntl |= CURSOR_PIPE_CSC_ENABLE;
1f5d76db
PZ
7400 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7401 }
65a21cd6
JB
7402 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7403
7404 intel_crtc->cursor_visible = visible;
7405 }
7406 /* and commit changes on next vblank */
7407 I915_WRITE(CURBASE_IVB(pipe), base);
7408}
7409
cda4b7d3 7410/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
7411static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7412 bool on)
cda4b7d3
CW
7413{
7414 struct drm_device *dev = crtc->dev;
7415 struct drm_i915_private *dev_priv = dev->dev_private;
7416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7417 int pipe = intel_crtc->pipe;
7418 int x = intel_crtc->cursor_x;
7419 int y = intel_crtc->cursor_y;
d6e4db15 7420 u32 base = 0, pos = 0;
cda4b7d3
CW
7421 bool visible;
7422
d6e4db15 7423 if (on)
cda4b7d3 7424 base = intel_crtc->cursor_addr;
cda4b7d3 7425
d6e4db15
VS
7426 if (x >= intel_crtc->config.pipe_src_w)
7427 base = 0;
7428
7429 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
7430 base = 0;
7431
7432 if (x < 0) {
efc9064e 7433 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
7434 base = 0;
7435
7436 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7437 x = -x;
7438 }
7439 pos |= x << CURSOR_X_SHIFT;
7440
7441 if (y < 0) {
efc9064e 7442 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
7443 base = 0;
7444
7445 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7446 y = -y;
7447 }
7448 pos |= y << CURSOR_Y_SHIFT;
7449
7450 visible = base != 0;
560b85bb 7451 if (!visible && !intel_crtc->cursor_visible)
cda4b7d3
CW
7452 return;
7453
b3dc685e 7454 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
65a21cd6
JB
7455 I915_WRITE(CURPOS_IVB(pipe), pos);
7456 ivb_update_cursor(crtc, base);
7457 } else {
7458 I915_WRITE(CURPOS(pipe), pos);
7459 if (IS_845G(dev) || IS_I865G(dev))
7460 i845_update_cursor(crtc, base);
7461 else
7462 i9xx_update_cursor(crtc, base);
7463 }
cda4b7d3
CW
7464}
7465
79e53945 7466static int intel_crtc_cursor_set(struct drm_crtc *crtc,
05394f39 7467 struct drm_file *file,
79e53945
JB
7468 uint32_t handle,
7469 uint32_t width, uint32_t height)
7470{
7471 struct drm_device *dev = crtc->dev;
7472 struct drm_i915_private *dev_priv = dev->dev_private;
7473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
05394f39 7474 struct drm_i915_gem_object *obj;
cda4b7d3 7475 uint32_t addr;
3f8bc370 7476 int ret;
79e53945 7477
79e53945
JB
7478 /* if we want to turn off the cursor ignore width and height */
7479 if (!handle) {
28c97730 7480 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 7481 addr = 0;
05394f39 7482 obj = NULL;
5004417d 7483 mutex_lock(&dev->struct_mutex);
3f8bc370 7484 goto finish;
79e53945
JB
7485 }
7486
7487 /* Currently we only support 64x64 cursors */
7488 if (width != 64 || height != 64) {
7489 DRM_ERROR("we currently only support 64x64 cursors\n");
7490 return -EINVAL;
7491 }
7492
05394f39 7493 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
c8725226 7494 if (&obj->base == NULL)
79e53945
JB
7495 return -ENOENT;
7496
05394f39 7497 if (obj->base.size < width * height * 4) {
79e53945 7498 DRM_ERROR("buffer is to small\n");
34b8686e
DA
7499 ret = -ENOMEM;
7500 goto fail;
79e53945
JB
7501 }
7502
71acb5eb 7503 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 7504 mutex_lock(&dev->struct_mutex);
b295d1b6 7505 if (!dev_priv->info->cursor_needs_physical) {
693db184
CW
7506 unsigned alignment;
7507
d9e86c0e
CW
7508 if (obj->tiling_mode) {
7509 DRM_ERROR("cursor cannot be tiled\n");
7510 ret = -EINVAL;
7511 goto fail_locked;
7512 }
7513
693db184
CW
7514 /* Note that the w/a also requires 2 PTE of padding following
7515 * the bo. We currently fill all unused PTE with the shadow
7516 * page and so we should always have valid PTE following the
7517 * cursor preventing the VT-d warning.
7518 */
7519 alignment = 0;
7520 if (need_vtd_wa(dev))
7521 alignment = 64*1024;
7522
7523 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb
CW
7524 if (ret) {
7525 DRM_ERROR("failed to move cursor bo into the GTT\n");
2da3b9b9 7526 goto fail_locked;
e7b526bb
CW
7527 }
7528
d9e86c0e
CW
7529 ret = i915_gem_object_put_fence(obj);
7530 if (ret) {
2da3b9b9 7531 DRM_ERROR("failed to release fence for cursor");
d9e86c0e
CW
7532 goto fail_unpin;
7533 }
7534
f343c5f6 7535 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 7536 } else {
6eeefaf3 7537 int align = IS_I830(dev) ? 16 * 1024 : 256;
05394f39 7538 ret = i915_gem_attach_phys_object(dev, obj,
6eeefaf3
CW
7539 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7540 align);
71acb5eb
DA
7541 if (ret) {
7542 DRM_ERROR("failed to attach phys object\n");
7f9872e0 7543 goto fail_locked;
71acb5eb 7544 }
05394f39 7545 addr = obj->phys_obj->handle->busaddr;
3f8bc370
KH
7546 }
7547
a6c45cf0 7548 if (IS_GEN2(dev))
14b60391
JB
7549 I915_WRITE(CURSIZE, (height << 12) | width);
7550
3f8bc370 7551 finish:
3f8bc370 7552 if (intel_crtc->cursor_bo) {
b295d1b6 7553 if (dev_priv->info->cursor_needs_physical) {
05394f39 7554 if (intel_crtc->cursor_bo != obj)
71acb5eb
DA
7555 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7556 } else
cc98b413 7557 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
05394f39 7558 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
3f8bc370 7559 }
80824003 7560
7f9872e0 7561 mutex_unlock(&dev->struct_mutex);
3f8bc370
KH
7562
7563 intel_crtc->cursor_addr = addr;
05394f39 7564 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
7565 intel_crtc->cursor_width = width;
7566 intel_crtc->cursor_height = height;
7567
f2f5f771
VS
7568 if (intel_crtc->active)
7569 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
3f8bc370 7570
79e53945 7571 return 0;
e7b526bb 7572fail_unpin:
cc98b413 7573 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 7574fail_locked:
34b8686e 7575 mutex_unlock(&dev->struct_mutex);
bc9025bd 7576fail:
05394f39 7577 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 7578 return ret;
79e53945
JB
7579}
7580
7581static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7582{
79e53945 7583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7584
92e76c8c
VS
7585 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7586 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
652c393a 7587
f2f5f771
VS
7588 if (intel_crtc->active)
7589 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
79e53945
JB
7590
7591 return 0;
b8c00ac5
DA
7592}
7593
79e53945 7594static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 7595 u16 *blue, uint32_t start, uint32_t size)
79e53945 7596{
7203425a 7597 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 7598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7599
7203425a 7600 for (i = start; i < end; i++) {
79e53945
JB
7601 intel_crtc->lut_r[i] = red[i] >> 8;
7602 intel_crtc->lut_g[i] = green[i] >> 8;
7603 intel_crtc->lut_b[i] = blue[i] >> 8;
7604 }
7605
7606 intel_crtc_load_lut(crtc);
7607}
7608
79e53945
JB
7609/* VESA 640x480x72Hz mode to set on the pipe */
7610static struct drm_display_mode load_detect_mode = {
7611 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7612 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7613};
7614
d2dff872
CW
7615static struct drm_framebuffer *
7616intel_framebuffer_create(struct drm_device *dev,
308e5bcb 7617 struct drm_mode_fb_cmd2 *mode_cmd,
d2dff872
CW
7618 struct drm_i915_gem_object *obj)
7619{
7620 struct intel_framebuffer *intel_fb;
7621 int ret;
7622
7623 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7624 if (!intel_fb) {
7625 drm_gem_object_unreference_unlocked(&obj->base);
7626 return ERR_PTR(-ENOMEM);
7627 }
7628
dd4916c5
DV
7629 ret = i915_mutex_lock_interruptible(dev);
7630 if (ret)
7631 goto err;
7632
d2dff872 7633 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
7634 mutex_unlock(&dev->struct_mutex);
7635 if (ret)
7636 goto err;
d2dff872
CW
7637
7638 return &intel_fb->base;
dd4916c5
DV
7639err:
7640 drm_gem_object_unreference_unlocked(&obj->base);
7641 kfree(intel_fb);
7642
7643 return ERR_PTR(ret);
d2dff872
CW
7644}
7645
7646static u32
7647intel_framebuffer_pitch_for_width(int width, int bpp)
7648{
7649 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7650 return ALIGN(pitch, 64);
7651}
7652
7653static u32
7654intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7655{
7656 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7657 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7658}
7659
7660static struct drm_framebuffer *
7661intel_framebuffer_create_for_mode(struct drm_device *dev,
7662 struct drm_display_mode *mode,
7663 int depth, int bpp)
7664{
7665 struct drm_i915_gem_object *obj;
0fed39bd 7666 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
7667
7668 obj = i915_gem_alloc_object(dev,
7669 intel_framebuffer_size_for_mode(mode, bpp));
7670 if (obj == NULL)
7671 return ERR_PTR(-ENOMEM);
7672
7673 mode_cmd.width = mode->hdisplay;
7674 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
7675 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
7676 bpp);
5ca0c34a 7677 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
7678
7679 return intel_framebuffer_create(dev, &mode_cmd, obj);
7680}
7681
7682static struct drm_framebuffer *
7683mode_fits_in_fbdev(struct drm_device *dev,
7684 struct drm_display_mode *mode)
7685{
4520f53a 7686#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
7687 struct drm_i915_private *dev_priv = dev->dev_private;
7688 struct drm_i915_gem_object *obj;
7689 struct drm_framebuffer *fb;
7690
7691 if (dev_priv->fbdev == NULL)
7692 return NULL;
7693
7694 obj = dev_priv->fbdev->ifb.obj;
7695 if (obj == NULL)
7696 return NULL;
7697
7698 fb = &dev_priv->fbdev->ifb.base;
01f2c773
VS
7699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7700 fb->bits_per_pixel))
d2dff872
CW
7701 return NULL;
7702
01f2c773 7703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
7704 return NULL;
7705
7706 return fb;
4520f53a
DV
7707#else
7708 return NULL;
7709#endif
d2dff872
CW
7710}
7711
d2434ab7 7712bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 7713 struct drm_display_mode *mode,
8261b191 7714 struct intel_load_detect_pipe *old)
79e53945
JB
7715{
7716 struct intel_crtc *intel_crtc;
d2434ab7
DV
7717 struct intel_encoder *intel_encoder =
7718 intel_attached_encoder(connector);
79e53945 7719 struct drm_crtc *possible_crtc;
4ef69c7a 7720 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
7721 struct drm_crtc *crtc = NULL;
7722 struct drm_device *dev = encoder->dev;
94352cf9 7723 struct drm_framebuffer *fb;
79e53945
JB
7724 int i = -1;
7725
d2dff872
CW
7726 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7727 connector->base.id, drm_get_connector_name(connector),
7728 encoder->base.id, drm_get_encoder_name(encoder));
7729
79e53945
JB
7730 /*
7731 * Algorithm gets a little messy:
7a5e4805 7732 *
79e53945
JB
7733 * - if the connector already has an assigned crtc, use it (but make
7734 * sure it's on first)
7a5e4805 7735 *
79e53945
JB
7736 * - try to find the first unused crtc that can drive this connector,
7737 * and use that if we find one
79e53945
JB
7738 */
7739
7740 /* See if we already have a CRTC for this connector */
7741 if (encoder->crtc) {
7742 crtc = encoder->crtc;
8261b191 7743
7b24056b
DV
7744 mutex_lock(&crtc->mutex);
7745
24218aac 7746 old->dpms_mode = connector->dpms;
8261b191
CW
7747 old->load_detect_temp = false;
7748
7749 /* Make sure the crtc and connector are running */
24218aac
DV
7750 if (connector->dpms != DRM_MODE_DPMS_ON)
7751 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 7752
7173188d 7753 return true;
79e53945
JB
7754 }
7755
7756 /* Find an unused one (if possible) */
7757 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7758 i++;
7759 if (!(encoder->possible_crtcs & (1 << i)))
7760 continue;
7761 if (!possible_crtc->enabled) {
7762 crtc = possible_crtc;
7763 break;
7764 }
79e53945
JB
7765 }
7766
7767 /*
7768 * If we didn't find an unused CRTC, don't use any.
7769 */
7770 if (!crtc) {
7173188d
CW
7771 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7772 return false;
79e53945
JB
7773 }
7774
7b24056b 7775 mutex_lock(&crtc->mutex);
fc303101
DV
7776 intel_encoder->new_crtc = to_intel_crtc(crtc);
7777 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
7778
7779 intel_crtc = to_intel_crtc(crtc);
24218aac 7780 old->dpms_mode = connector->dpms;
8261b191 7781 old->load_detect_temp = true;
d2dff872 7782 old->release_fb = NULL;
79e53945 7783
6492711d
CW
7784 if (!mode)
7785 mode = &load_detect_mode;
79e53945 7786
d2dff872
CW
7787 /* We need a framebuffer large enough to accommodate all accesses
7788 * that the plane may generate whilst we perform load detection.
7789 * We can not rely on the fbcon either being present (we get called
7790 * during its initialisation to detect all boot displays, or it may
7791 * not even exist) or that it is large enough to satisfy the
7792 * requested mode.
7793 */
94352cf9
DV
7794 fb = mode_fits_in_fbdev(dev, mode);
7795 if (fb == NULL) {
d2dff872 7796 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
7797 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7798 old->release_fb = fb;
d2dff872
CW
7799 } else
7800 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 7801 if (IS_ERR(fb)) {
d2dff872 7802 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
7b24056b 7803 mutex_unlock(&crtc->mutex);
0e8b3d3e 7804 return false;
79e53945 7805 }
79e53945 7806
c0c36b94 7807 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 7808 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
7809 if (old->release_fb)
7810 old->release_fb->funcs->destroy(old->release_fb);
7b24056b 7811 mutex_unlock(&crtc->mutex);
0e8b3d3e 7812 return false;
79e53945 7813 }
7173188d 7814
79e53945 7815 /* let the connector get through one full cycle before testing */
9d0498a2 7816 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 7817 return true;
79e53945
JB
7818}
7819
d2434ab7 7820void intel_release_load_detect_pipe(struct drm_connector *connector,
8261b191 7821 struct intel_load_detect_pipe *old)
79e53945 7822{
d2434ab7
DV
7823 struct intel_encoder *intel_encoder =
7824 intel_attached_encoder(connector);
4ef69c7a 7825 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 7826 struct drm_crtc *crtc = encoder->crtc;
79e53945 7827
d2dff872
CW
7828 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7829 connector->base.id, drm_get_connector_name(connector),
7830 encoder->base.id, drm_get_encoder_name(encoder));
7831
8261b191 7832 if (old->load_detect_temp) {
fc303101
DV
7833 to_intel_connector(connector)->new_encoder = NULL;
7834 intel_encoder->new_crtc = NULL;
7835 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 7836
36206361
DV
7837 if (old->release_fb) {
7838 drm_framebuffer_unregister_private(old->release_fb);
7839 drm_framebuffer_unreference(old->release_fb);
7840 }
d2dff872 7841
67c96400 7842 mutex_unlock(&crtc->mutex);
0622a53c 7843 return;
79e53945
JB
7844 }
7845
c751ce4f 7846 /* Switch crtc and encoder back off if necessary */
24218aac
DV
7847 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7848 connector->funcs->dpms(connector, old->dpms_mode);
7b24056b
DV
7849
7850 mutex_unlock(&crtc->mutex);
79e53945
JB
7851}
7852
da4a1efa
VS
7853static int i9xx_pll_refclk(struct drm_device *dev,
7854 const struct intel_crtc_config *pipe_config)
7855{
7856 struct drm_i915_private *dev_priv = dev->dev_private;
7857 u32 dpll = pipe_config->dpll_hw_state.dpll;
7858
7859 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7860 return dev_priv->vbt.lvds_ssc_freq * 1000;
7861 else if (HAS_PCH_SPLIT(dev))
7862 return 120000;
7863 else if (!IS_GEN2(dev))
7864 return 96000;
7865 else
7866 return 48000;
7867}
7868
79e53945 7869/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
7870static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7871 struct intel_crtc_config *pipe_config)
79e53945 7872{
f1f644dc 7873 struct drm_device *dev = crtc->base.dev;
79e53945 7874 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 7875 int pipe = pipe_config->cpu_transcoder;
293623f7 7876 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
7877 u32 fp;
7878 intel_clock_t clock;
da4a1efa 7879 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
7880
7881 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 7882 fp = pipe_config->dpll_hw_state.fp0;
79e53945 7883 else
293623f7 7884 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
7885
7886 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
7887 if (IS_PINEVIEW(dev)) {
7888 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7889 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
7890 } else {
7891 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7892 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7893 }
7894
a6c45cf0 7895 if (!IS_GEN2(dev)) {
f2b115e6
AJ
7896 if (IS_PINEVIEW(dev))
7897 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7898 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
7899 else
7900 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
7901 DPLL_FPA01_P1_POST_DIV_SHIFT);
7902
7903 switch (dpll & DPLL_MODE_MASK) {
7904 case DPLLB_MODE_DAC_SERIAL:
7905 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7906 5 : 10;
7907 break;
7908 case DPLLB_MODE_LVDS:
7909 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7910 7 : 14;
7911 break;
7912 default:
28c97730 7913 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 7914 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 7915 return;
79e53945
JB
7916 }
7917
ac58c3f0 7918 if (IS_PINEVIEW(dev))
da4a1efa 7919 pineview_clock(refclk, &clock);
ac58c3f0 7920 else
da4a1efa 7921 i9xx_clock(refclk, &clock);
79e53945
JB
7922 } else {
7923 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7924
7925 if (is_lvds) {
7926 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7927 DPLL_FPA01_P1_POST_DIV_SHIFT);
7928 clock.p2 = 14;
79e53945
JB
7929 } else {
7930 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7931 clock.p1 = 2;
7932 else {
7933 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7934 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7935 }
7936 if (dpll & PLL_P2_DIVIDE_BY_4)
7937 clock.p2 = 4;
7938 else
7939 clock.p2 = 2;
79e53945 7940 }
da4a1efa
VS
7941
7942 i9xx_clock(refclk, &clock);
79e53945
JB
7943 }
7944
18442d08
VS
7945 /*
7946 * This value includes pixel_multiplier. We will use
241bfc38 7947 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
7948 * encoder's get_config() function.
7949 */
7950 pipe_config->port_clock = clock.dot;
f1f644dc
JB
7951}
7952
6878da05
VS
7953int intel_dotclock_calculate(int link_freq,
7954 const struct intel_link_m_n *m_n)
f1f644dc 7955{
f1f644dc
JB
7956 /*
7957 * The calculation for the data clock is:
1041a02f 7958 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 7959 * But we want to avoid losing precison if possible, so:
1041a02f 7960 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
7961 *
7962 * and the link clock is simpler:
1041a02f 7963 * link_clock = (m * link_clock) / n
f1f644dc
JB
7964 */
7965
6878da05
VS
7966 if (!m_n->link_n)
7967 return 0;
f1f644dc 7968
6878da05
VS
7969 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
7970}
f1f644dc 7971
18442d08
VS
7972static void ironlake_pch_clock_get(struct intel_crtc *crtc,
7973 struct intel_crtc_config *pipe_config)
6878da05
VS
7974{
7975 struct drm_device *dev = crtc->base.dev;
79e53945 7976
18442d08
VS
7977 /* read out port_clock from the DPLL */
7978 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 7979
f1f644dc 7980 /*
18442d08 7981 * This value does not include pixel_multiplier.
241bfc38 7982 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
7983 * agree once we know their relationship in the encoder's
7984 * get_config() function.
79e53945 7985 */
241bfc38 7986 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
7987 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
7988 &pipe_config->fdi_m_n);
79e53945
JB
7989}
7990
7991/** Returns the currently programmed mode of the given pipe. */
7992struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7993 struct drm_crtc *crtc)
7994{
548f245b 7995 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 7996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 7997 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 7998 struct drm_display_mode *mode;
f1f644dc 7999 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8000 int htot = I915_READ(HTOTAL(cpu_transcoder));
8001 int hsync = I915_READ(HSYNC(cpu_transcoder));
8002 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8003 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8004 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8005
8006 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8007 if (!mode)
8008 return NULL;
8009
f1f644dc
JB
8010 /*
8011 * Construct a pipe_config sufficient for getting the clock info
8012 * back out of crtc_clock_get.
8013 *
8014 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8015 * to use a real value here instead.
8016 */
293623f7 8017 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8018 pipe_config.pixel_multiplier = 1;
293623f7
VS
8019 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8020 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8021 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8022 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8023
773ae034 8024 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8025 mode->hdisplay = (htot & 0xffff) + 1;
8026 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8027 mode->hsync_start = (hsync & 0xffff) + 1;
8028 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8029 mode->vdisplay = (vtot & 0xffff) + 1;
8030 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8031 mode->vsync_start = (vsync & 0xffff) + 1;
8032 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8033
8034 drm_mode_set_name(mode);
79e53945
JB
8035
8036 return mode;
8037}
8038
3dec0095 8039static void intel_increase_pllclock(struct drm_crtc *crtc)
652c393a
JB
8040{
8041 struct drm_device *dev = crtc->dev;
8042 drm_i915_private_t *dev_priv = dev->dev_private;
8043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8044 int pipe = intel_crtc->pipe;
dbdc6479
JB
8045 int dpll_reg = DPLL(pipe);
8046 int dpll;
652c393a 8047
bad720ff 8048 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8049 return;
8050
8051 if (!dev_priv->lvds_downclock_avail)
8052 return;
8053
dbdc6479 8054 dpll = I915_READ(dpll_reg);
652c393a 8055 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8056 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8057
8ac5a6d5 8058 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8059
8060 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8061 I915_WRITE(dpll_reg, dpll);
9d0498a2 8062 intel_wait_for_vblank(dev, pipe);
dbdc6479 8063
652c393a
JB
8064 dpll = I915_READ(dpll_reg);
8065 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8066 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8067 }
652c393a
JB
8068}
8069
8070static void intel_decrease_pllclock(struct drm_crtc *crtc)
8071{
8072 struct drm_device *dev = crtc->dev;
8073 drm_i915_private_t *dev_priv = dev->dev_private;
8074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8075
bad720ff 8076 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8077 return;
8078
8079 if (!dev_priv->lvds_downclock_avail)
8080 return;
8081
8082 /*
8083 * Since this is called by a timer, we should never get here in
8084 * the manual case.
8085 */
8086 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8087 int pipe = intel_crtc->pipe;
8088 int dpll_reg = DPLL(pipe);
8089 int dpll;
f6e5b160 8090
44d98a61 8091 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8092
8ac5a6d5 8093 assert_panel_unlocked(dev_priv, pipe);
652c393a 8094
dc257cf1 8095 dpll = I915_READ(dpll_reg);
652c393a
JB
8096 dpll |= DISPLAY_RATE_SELECT_FPA1;
8097 I915_WRITE(dpll_reg, dpll);
9d0498a2 8098 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8099 dpll = I915_READ(dpll_reg);
8100 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8101 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8102 }
8103
8104}
8105
f047e395
CW
8106void intel_mark_busy(struct drm_device *dev)
8107{
c67a470b
PZ
8108 struct drm_i915_private *dev_priv = dev->dev_private;
8109
8110 hsw_package_c8_gpu_busy(dev_priv);
8111 i915_update_gfx_val(dev_priv);
f047e395
CW
8112}
8113
8114void intel_mark_idle(struct drm_device *dev)
652c393a 8115{
c67a470b 8116 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8117 struct drm_crtc *crtc;
652c393a 8118
c67a470b
PZ
8119 hsw_package_c8_gpu_idle(dev_priv);
8120
652c393a
JB
8121 if (!i915_powersave)
8122 return;
8123
652c393a 8124 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
652c393a
JB
8125 if (!crtc->fb)
8126 continue;
8127
725a5b54 8128 intel_decrease_pllclock(crtc);
652c393a 8129 }
b29c19b6
CW
8130
8131 if (dev_priv->info->gen >= 6)
8132 gen6_rps_idle(dev->dev_private);
652c393a
JB
8133}
8134
c65355bb
CW
8135void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8136 struct intel_ring_buffer *ring)
652c393a 8137{
f047e395
CW
8138 struct drm_device *dev = obj->base.dev;
8139 struct drm_crtc *crtc;
652c393a 8140
f047e395 8141 if (!i915_powersave)
acb87dfb
CW
8142 return;
8143
652c393a
JB
8144 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8145 if (!crtc->fb)
8146 continue;
8147
c65355bb
CW
8148 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8149 continue;
8150
8151 intel_increase_pllclock(crtc);
8152 if (ring && intel_fbc_enabled(dev))
8153 ring->fbc_dirty = true;
652c393a
JB
8154 }
8155}
8156
79e53945
JB
8157static void intel_crtc_destroy(struct drm_crtc *crtc)
8158{
8159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
8160 struct drm_device *dev = crtc->dev;
8161 struct intel_unpin_work *work;
8162 unsigned long flags;
8163
8164 spin_lock_irqsave(&dev->event_lock, flags);
8165 work = intel_crtc->unpin_work;
8166 intel_crtc->unpin_work = NULL;
8167 spin_unlock_irqrestore(&dev->event_lock, flags);
8168
8169 if (work) {
8170 cancel_work_sync(&work->work);
8171 kfree(work);
8172 }
79e53945 8173
40ccc72b
MK
8174 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8175
79e53945 8176 drm_crtc_cleanup(crtc);
67e77c5a 8177
79e53945
JB
8178 kfree(intel_crtc);
8179}
8180
6b95a207
KH
8181static void intel_unpin_work_fn(struct work_struct *__work)
8182{
8183 struct intel_unpin_work *work =
8184 container_of(__work, struct intel_unpin_work, work);
b4a98e57 8185 struct drm_device *dev = work->crtc->dev;
6b95a207 8186
b4a98e57 8187 mutex_lock(&dev->struct_mutex);
1690e1eb 8188 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
8189 drm_gem_object_unreference(&work->pending_flip_obj->base);
8190 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 8191
b4a98e57
CW
8192 intel_update_fbc(dev);
8193 mutex_unlock(&dev->struct_mutex);
8194
8195 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8196 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8197
6b95a207
KH
8198 kfree(work);
8199}
8200
1afe3e9d 8201static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 8202 struct drm_crtc *crtc)
6b95a207
KH
8203{
8204 drm_i915_private_t *dev_priv = dev->dev_private;
6b95a207
KH
8205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8206 struct intel_unpin_work *work;
6b95a207
KH
8207 unsigned long flags;
8208
8209 /* Ignore early vblank irqs */
8210 if (intel_crtc == NULL)
8211 return;
8212
8213 spin_lock_irqsave(&dev->event_lock, flags);
8214 work = intel_crtc->unpin_work;
e7d841ca
CW
8215
8216 /* Ensure we don't miss a work->pending update ... */
8217 smp_rmb();
8218
8219 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
8220 spin_unlock_irqrestore(&dev->event_lock, flags);
8221 return;
8222 }
8223
e7d841ca
CW
8224 /* and that the unpin work is consistent wrt ->pending. */
8225 smp_rmb();
8226
6b95a207 8227 intel_crtc->unpin_work = NULL;
6b95a207 8228
45a066eb
RC
8229 if (work->event)
8230 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 8231
0af7e4df
MK
8232 drm_vblank_put(dev, intel_crtc->pipe);
8233
6b95a207
KH
8234 spin_unlock_irqrestore(&dev->event_lock, flags);
8235
2c10d571 8236 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
8237
8238 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
8239
8240 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
8241}
8242
1afe3e9d
JB
8243void intel_finish_page_flip(struct drm_device *dev, int pipe)
8244{
8245 drm_i915_private_t *dev_priv = dev->dev_private;
8246 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8247
49b14a5c 8248 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8249}
8250
8251void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8252{
8253 drm_i915_private_t *dev_priv = dev->dev_private;
8254 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8255
49b14a5c 8256 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8257}
8258
6b95a207
KH
8259void intel_prepare_page_flip(struct drm_device *dev, int plane)
8260{
8261 drm_i915_private_t *dev_priv = dev->dev_private;
8262 struct intel_crtc *intel_crtc =
8263 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8264 unsigned long flags;
8265
e7d841ca
CW
8266 /* NB: An MMIO update of the plane base pointer will also
8267 * generate a page-flip completion irq, i.e. every modeset
8268 * is also accompanied by a spurious intel_prepare_page_flip().
8269 */
6b95a207 8270 spin_lock_irqsave(&dev->event_lock, flags);
e7d841ca
CW
8271 if (intel_crtc->unpin_work)
8272 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
8273 spin_unlock_irqrestore(&dev->event_lock, flags);
8274}
8275
e7d841ca
CW
8276inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8277{
8278 /* Ensure that the work item is consistent when activating it ... */
8279 smp_wmb();
8280 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8281 /* and that it is marked active as soon as the irq could fire. */
8282 smp_wmb();
8283}
8284
8c9f3aaf
JB
8285static int intel_gen2_queue_flip(struct drm_device *dev,
8286 struct drm_crtc *crtc,
8287 struct drm_framebuffer *fb,
ed8d1975
KP
8288 struct drm_i915_gem_object *obj,
8289 uint32_t flags)
8c9f3aaf
JB
8290{
8291 struct drm_i915_private *dev_priv = dev->dev_private;
8292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8293 u32 flip_mask;
6d90c952 8294 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8295 int ret;
8296
6d90c952 8297 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8298 if (ret)
83d4092b 8299 goto err;
8c9f3aaf 8300
6d90c952 8301 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8302 if (ret)
83d4092b 8303 goto err_unpin;
8c9f3aaf
JB
8304
8305 /* Can't queue multiple flips, so wait for the previous
8306 * one to finish before executing the next.
8307 */
8308 if (intel_crtc->plane)
8309 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8310 else
8311 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8312 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8313 intel_ring_emit(ring, MI_NOOP);
8314 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8315 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8316 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8317 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952 8318 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
8319
8320 intel_mark_page_flip_active(intel_crtc);
09246732 8321 __intel_ring_advance(ring);
83d4092b
CW
8322 return 0;
8323
8324err_unpin:
8325 intel_unpin_fb_obj(obj);
8326err:
8c9f3aaf
JB
8327 return ret;
8328}
8329
8330static int intel_gen3_queue_flip(struct drm_device *dev,
8331 struct drm_crtc *crtc,
8332 struct drm_framebuffer *fb,
ed8d1975
KP
8333 struct drm_i915_gem_object *obj,
8334 uint32_t flags)
8c9f3aaf
JB
8335{
8336 struct drm_i915_private *dev_priv = dev->dev_private;
8337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8338 u32 flip_mask;
6d90c952 8339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8340 int ret;
8341
6d90c952 8342 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8343 if (ret)
83d4092b 8344 goto err;
8c9f3aaf 8345
6d90c952 8346 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8347 if (ret)
83d4092b 8348 goto err_unpin;
8c9f3aaf
JB
8349
8350 if (intel_crtc->plane)
8351 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8352 else
8353 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8354 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8355 intel_ring_emit(ring, MI_NOOP);
8356 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8357 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8358 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8359 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952
DV
8360 intel_ring_emit(ring, MI_NOOP);
8361
e7d841ca 8362 intel_mark_page_flip_active(intel_crtc);
09246732 8363 __intel_ring_advance(ring);
83d4092b
CW
8364 return 0;
8365
8366err_unpin:
8367 intel_unpin_fb_obj(obj);
8368err:
8c9f3aaf
JB
8369 return ret;
8370}
8371
8372static int intel_gen4_queue_flip(struct drm_device *dev,
8373 struct drm_crtc *crtc,
8374 struct drm_framebuffer *fb,
ed8d1975
KP
8375 struct drm_i915_gem_object *obj,
8376 uint32_t flags)
8c9f3aaf
JB
8377{
8378 struct drm_i915_private *dev_priv = dev->dev_private;
8379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8380 uint32_t pf, pipesrc;
6d90c952 8381 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8382 int ret;
8383
6d90c952 8384 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8385 if (ret)
83d4092b 8386 goto err;
8c9f3aaf 8387
6d90c952 8388 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8389 if (ret)
83d4092b 8390 goto err_unpin;
8c9f3aaf
JB
8391
8392 /* i965+ uses the linear or tiled offsets from the
8393 * Display Registers (which do not change across a page-flip)
8394 * so we need only reprogram the base address.
8395 */
6d90c952
DV
8396 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8397 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8398 intel_ring_emit(ring, fb->pitches[0]);
c2c75131 8399 intel_ring_emit(ring,
f343c5f6 8400 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
c2c75131 8401 obj->tiling_mode);
8c9f3aaf
JB
8402
8403 /* XXX Enabling the panel-fitter across page-flip is so far
8404 * untested on non-native modes, so ignore it for now.
8405 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8406 */
8407 pf = 0;
8408 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8409 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8410
8411 intel_mark_page_flip_active(intel_crtc);
09246732 8412 __intel_ring_advance(ring);
83d4092b
CW
8413 return 0;
8414
8415err_unpin:
8416 intel_unpin_fb_obj(obj);
8417err:
8c9f3aaf
JB
8418 return ret;
8419}
8420
8421static int intel_gen6_queue_flip(struct drm_device *dev,
8422 struct drm_crtc *crtc,
8423 struct drm_framebuffer *fb,
ed8d1975
KP
8424 struct drm_i915_gem_object *obj,
8425 uint32_t flags)
8c9f3aaf
JB
8426{
8427 struct drm_i915_private *dev_priv = dev->dev_private;
8428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6d90c952 8429 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8430 uint32_t pf, pipesrc;
8431 int ret;
8432
6d90c952 8433 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8434 if (ret)
83d4092b 8435 goto err;
8c9f3aaf 8436
6d90c952 8437 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8438 if (ret)
83d4092b 8439 goto err_unpin;
8c9f3aaf 8440
6d90c952
DV
8441 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8442 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8443 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
f343c5f6 8444 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
8c9f3aaf 8445
dc257cf1
DV
8446 /* Contrary to the suggestions in the documentation,
8447 * "Enable Panel Fitter" does not seem to be required when page
8448 * flipping with a non-native mode, and worse causes a normal
8449 * modeset to fail.
8450 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8451 */
8452 pf = 0;
8c9f3aaf 8453 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8454 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8455
8456 intel_mark_page_flip_active(intel_crtc);
09246732 8457 __intel_ring_advance(ring);
83d4092b
CW
8458 return 0;
8459
8460err_unpin:
8461 intel_unpin_fb_obj(obj);
8462err:
8c9f3aaf
JB
8463 return ret;
8464}
8465
7c9017e5
JB
8466static int intel_gen7_queue_flip(struct drm_device *dev,
8467 struct drm_crtc *crtc,
8468 struct drm_framebuffer *fb,
ed8d1975
KP
8469 struct drm_i915_gem_object *obj,
8470 uint32_t flags)
7c9017e5
JB
8471{
8472 struct drm_i915_private *dev_priv = dev->dev_private;
8473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ffe74d75 8474 struct intel_ring_buffer *ring;
cb05d8de 8475 uint32_t plane_bit = 0;
ffe74d75
CW
8476 int len, ret;
8477
8478 ring = obj->ring;
1c5fd085 8479 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
ffe74d75 8480 ring = &dev_priv->ring[BCS];
7c9017e5
JB
8481
8482 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8483 if (ret)
83d4092b 8484 goto err;
7c9017e5 8485
cb05d8de
DV
8486 switch(intel_crtc->plane) {
8487 case PLANE_A:
8488 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8489 break;
8490 case PLANE_B:
8491 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8492 break;
8493 case PLANE_C:
8494 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8495 break;
8496 default:
8497 WARN_ONCE(1, "unknown plane in flip command\n");
8498 ret = -ENODEV;
ab3951eb 8499 goto err_unpin;
cb05d8de
DV
8500 }
8501
ffe74d75
CW
8502 len = 4;
8503 if (ring->id == RCS)
8504 len += 6;
8505
8506 ret = intel_ring_begin(ring, len);
7c9017e5 8507 if (ret)
83d4092b 8508 goto err_unpin;
7c9017e5 8509
ffe74d75
CW
8510 /* Unmask the flip-done completion message. Note that the bspec says that
8511 * we should do this for both the BCS and RCS, and that we must not unmask
8512 * more than one flip event at any time (or ensure that one flip message
8513 * can be sent by waiting for flip-done prior to queueing new flips).
8514 * Experimentation says that BCS works despite DERRMR masking all
8515 * flip-done completion events and that unmasking all planes at once
8516 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8517 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8518 */
8519 if (ring->id == RCS) {
8520 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8521 intel_ring_emit(ring, DERRMR);
8522 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8523 DERRMR_PIPEB_PRI_FLIP_DONE |
8524 DERRMR_PIPEC_PRI_FLIP_DONE));
8525 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
8526 intel_ring_emit(ring, DERRMR);
8527 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8528 }
8529
cb05d8de 8530 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 8531 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
f343c5f6 8532 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7c9017e5 8533 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
8534
8535 intel_mark_page_flip_active(intel_crtc);
09246732 8536 __intel_ring_advance(ring);
83d4092b
CW
8537 return 0;
8538
8539err_unpin:
8540 intel_unpin_fb_obj(obj);
8541err:
7c9017e5
JB
8542 return ret;
8543}
8544
8c9f3aaf
JB
8545static int intel_default_queue_flip(struct drm_device *dev,
8546 struct drm_crtc *crtc,
8547 struct drm_framebuffer *fb,
ed8d1975
KP
8548 struct drm_i915_gem_object *obj,
8549 uint32_t flags)
8c9f3aaf
JB
8550{
8551 return -ENODEV;
8552}
8553
6b95a207
KH
8554static int intel_crtc_page_flip(struct drm_crtc *crtc,
8555 struct drm_framebuffer *fb,
ed8d1975
KP
8556 struct drm_pending_vblank_event *event,
8557 uint32_t page_flip_flags)
6b95a207
KH
8558{
8559 struct drm_device *dev = crtc->dev;
8560 struct drm_i915_private *dev_priv = dev->dev_private;
4a35f83b
VS
8561 struct drm_framebuffer *old_fb = crtc->fb;
8562 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
6b95a207
KH
8563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8564 struct intel_unpin_work *work;
8c9f3aaf 8565 unsigned long flags;
52e68630 8566 int ret;
6b95a207 8567
e6a595d2
VS
8568 /* Can't change pixel format via MI display flips. */
8569 if (fb->pixel_format != crtc->fb->pixel_format)
8570 return -EINVAL;
8571
8572 /*
8573 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8574 * Note that pitch changes could also affect these register.
8575 */
8576 if (INTEL_INFO(dev)->gen > 3 &&
8577 (fb->offsets[0] != crtc->fb->offsets[0] ||
8578 fb->pitches[0] != crtc->fb->pitches[0]))
8579 return -EINVAL;
8580
b14c5679 8581 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
8582 if (work == NULL)
8583 return -ENOMEM;
8584
6b95a207 8585 work->event = event;
b4a98e57 8586 work->crtc = crtc;
4a35f83b 8587 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
6b95a207
KH
8588 INIT_WORK(&work->work, intel_unpin_work_fn);
8589
7317c75e
JB
8590 ret = drm_vblank_get(dev, intel_crtc->pipe);
8591 if (ret)
8592 goto free_work;
8593
6b95a207
KH
8594 /* We borrow the event spin lock for protecting unpin_work */
8595 spin_lock_irqsave(&dev->event_lock, flags);
8596 if (intel_crtc->unpin_work) {
8597 spin_unlock_irqrestore(&dev->event_lock, flags);
8598 kfree(work);
7317c75e 8599 drm_vblank_put(dev, intel_crtc->pipe);
468f0b44
CW
8600
8601 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
8602 return -EBUSY;
8603 }
8604 intel_crtc->unpin_work = work;
8605 spin_unlock_irqrestore(&dev->event_lock, flags);
8606
b4a98e57
CW
8607 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8608 flush_workqueue(dev_priv->wq);
8609
79158103
CW
8610 ret = i915_mutex_lock_interruptible(dev);
8611 if (ret)
8612 goto cleanup;
6b95a207 8613
75dfca80 8614 /* Reference the objects for the scheduled work. */
05394f39
CW
8615 drm_gem_object_reference(&work->old_fb_obj->base);
8616 drm_gem_object_reference(&obj->base);
6b95a207
KH
8617
8618 crtc->fb = fb;
96b099fd 8619
e1f99ce6 8620 work->pending_flip_obj = obj;
e1f99ce6 8621
4e5359cd
SF
8622 work->enable_stall_check = true;
8623
b4a98e57 8624 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 8625 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 8626
ed8d1975 8627 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
8c9f3aaf
JB
8628 if (ret)
8629 goto cleanup_pending;
6b95a207 8630
7782de3b 8631 intel_disable_fbc(dev);
c65355bb 8632 intel_mark_fb_busy(obj, NULL);
6b95a207
KH
8633 mutex_unlock(&dev->struct_mutex);
8634
e5510fac
JB
8635 trace_i915_flip_request(intel_crtc->plane, obj);
8636
6b95a207 8637 return 0;
96b099fd 8638
8c9f3aaf 8639cleanup_pending:
b4a98e57 8640 atomic_dec(&intel_crtc->unpin_work_count);
4a35f83b 8641 crtc->fb = old_fb;
05394f39
CW
8642 drm_gem_object_unreference(&work->old_fb_obj->base);
8643 drm_gem_object_unreference(&obj->base);
96b099fd
CW
8644 mutex_unlock(&dev->struct_mutex);
8645
79158103 8646cleanup:
96b099fd
CW
8647 spin_lock_irqsave(&dev->event_lock, flags);
8648 intel_crtc->unpin_work = NULL;
8649 spin_unlock_irqrestore(&dev->event_lock, flags);
8650
7317c75e
JB
8651 drm_vblank_put(dev, intel_crtc->pipe);
8652free_work:
96b099fd
CW
8653 kfree(work);
8654
8655 return ret;
6b95a207
KH
8656}
8657
f6e5b160 8658static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
8659 .mode_set_base_atomic = intel_pipe_set_base_atomic,
8660 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
8661};
8662
50f56119
DV
8663static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8664 struct drm_crtc *crtc)
8665{
8666 struct drm_device *dev;
8667 struct drm_crtc *tmp;
8668 int crtc_mask = 1;
47f1c6c9 8669
50f56119 8670 WARN(!crtc, "checking null crtc?\n");
47f1c6c9 8671
50f56119 8672 dev = crtc->dev;
47f1c6c9 8673
50f56119
DV
8674 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8675 if (tmp == crtc)
8676 break;
8677 crtc_mask <<= 1;
8678 }
47f1c6c9 8679
50f56119
DV
8680 if (encoder->possible_crtcs & crtc_mask)
8681 return true;
8682 return false;
47f1c6c9 8683}
79e53945 8684
9a935856
DV
8685/**
8686 * intel_modeset_update_staged_output_state
8687 *
8688 * Updates the staged output configuration state, e.g. after we've read out the
8689 * current hw state.
8690 */
8691static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 8692{
9a935856
DV
8693 struct intel_encoder *encoder;
8694 struct intel_connector *connector;
f6e5b160 8695
9a935856
DV
8696 list_for_each_entry(connector, &dev->mode_config.connector_list,
8697 base.head) {
8698 connector->new_encoder =
8699 to_intel_encoder(connector->base.encoder);
8700 }
f6e5b160 8701
9a935856
DV
8702 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8703 base.head) {
8704 encoder->new_crtc =
8705 to_intel_crtc(encoder->base.crtc);
8706 }
f6e5b160
CW
8707}
8708
9a935856
DV
8709/**
8710 * intel_modeset_commit_output_state
8711 *
8712 * This function copies the stage display pipe configuration to the real one.
8713 */
8714static void intel_modeset_commit_output_state(struct drm_device *dev)
8715{
8716 struct intel_encoder *encoder;
8717 struct intel_connector *connector;
f6e5b160 8718
9a935856
DV
8719 list_for_each_entry(connector, &dev->mode_config.connector_list,
8720 base.head) {
8721 connector->base.encoder = &connector->new_encoder->base;
8722 }
f6e5b160 8723
9a935856
DV
8724 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8725 base.head) {
8726 encoder->base.crtc = &encoder->new_crtc->base;
8727 }
8728}
8729
050f7aeb
DV
8730static void
8731connected_sink_compute_bpp(struct intel_connector * connector,
8732 struct intel_crtc_config *pipe_config)
8733{
8734 int bpp = pipe_config->pipe_bpp;
8735
8736 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8737 connector->base.base.id,
8738 drm_get_connector_name(&connector->base));
8739
8740 /* Don't use an invalid EDID bpc value */
8741 if (connector->base.display_info.bpc &&
8742 connector->base.display_info.bpc * 3 < bpp) {
8743 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8744 bpp, connector->base.display_info.bpc*3);
8745 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8746 }
8747
8748 /* Clamp bpp to 8 on screens without EDID 1.4 */
8749 if (connector->base.display_info.bpc == 0 && bpp > 24) {
8750 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8751 bpp);
8752 pipe_config->pipe_bpp = 24;
8753 }
8754}
8755
4e53c2e0 8756static int
050f7aeb
DV
8757compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8758 struct drm_framebuffer *fb,
8759 struct intel_crtc_config *pipe_config)
4e53c2e0 8760{
050f7aeb
DV
8761 struct drm_device *dev = crtc->base.dev;
8762 struct intel_connector *connector;
4e53c2e0
DV
8763 int bpp;
8764
d42264b1
DV
8765 switch (fb->pixel_format) {
8766 case DRM_FORMAT_C8:
4e53c2e0
DV
8767 bpp = 8*3; /* since we go through a colormap */
8768 break;
d42264b1
DV
8769 case DRM_FORMAT_XRGB1555:
8770 case DRM_FORMAT_ARGB1555:
8771 /* checked in intel_framebuffer_init already */
8772 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8773 return -EINVAL;
8774 case DRM_FORMAT_RGB565:
4e53c2e0
DV
8775 bpp = 6*3; /* min is 18bpp */
8776 break;
d42264b1
DV
8777 case DRM_FORMAT_XBGR8888:
8778 case DRM_FORMAT_ABGR8888:
8779 /* checked in intel_framebuffer_init already */
8780 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8781 return -EINVAL;
8782 case DRM_FORMAT_XRGB8888:
8783 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
8784 bpp = 8*3;
8785 break;
d42264b1
DV
8786 case DRM_FORMAT_XRGB2101010:
8787 case DRM_FORMAT_ARGB2101010:
8788 case DRM_FORMAT_XBGR2101010:
8789 case DRM_FORMAT_ABGR2101010:
8790 /* checked in intel_framebuffer_init already */
8791 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 8792 return -EINVAL;
4e53c2e0
DV
8793 bpp = 10*3;
8794 break;
baba133a 8795 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
8796 default:
8797 DRM_DEBUG_KMS("unsupported depth\n");
8798 return -EINVAL;
8799 }
8800
4e53c2e0
DV
8801 pipe_config->pipe_bpp = bpp;
8802
8803 /* Clamp display bpp to EDID value */
8804 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 8805 base.head) {
1b829e05
DV
8806 if (!connector->new_encoder ||
8807 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
8808 continue;
8809
050f7aeb 8810 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
8811 }
8812
8813 return bpp;
8814}
8815
644db711
DV
8816static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
8817{
8818 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
8819 "type: 0x%x flags: 0x%x\n",
1342830c 8820 mode->crtc_clock,
644db711
DV
8821 mode->crtc_hdisplay, mode->crtc_hsync_start,
8822 mode->crtc_hsync_end, mode->crtc_htotal,
8823 mode->crtc_vdisplay, mode->crtc_vsync_start,
8824 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
8825}
8826
c0b03411
DV
8827static void intel_dump_pipe_config(struct intel_crtc *crtc,
8828 struct intel_crtc_config *pipe_config,
8829 const char *context)
8830{
8831 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8832 context, pipe_name(crtc->pipe));
8833
8834 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8835 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8836 pipe_config->pipe_bpp, pipe_config->dither);
8837 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8838 pipe_config->has_pch_encoder,
8839 pipe_config->fdi_lanes,
8840 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8841 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8842 pipe_config->fdi_m_n.tu);
eb14cb74
VS
8843 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8844 pipe_config->has_dp_encoder,
8845 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
8846 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
8847 pipe_config->dp_m_n.tu);
c0b03411
DV
8848 DRM_DEBUG_KMS("requested mode:\n");
8849 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8850 DRM_DEBUG_KMS("adjusted mode:\n");
8851 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 8852 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 8853 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
8854 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
8855 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
8856 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8857 pipe_config->gmch_pfit.control,
8858 pipe_config->gmch_pfit.pgm_ratios,
8859 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 8860 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 8861 pipe_config->pch_pfit.pos,
fd4daa9c
CW
8862 pipe_config->pch_pfit.size,
8863 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 8864 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 8865 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
8866}
8867
accfc0c5
DV
8868static bool check_encoder_cloning(struct drm_crtc *crtc)
8869{
8870 int num_encoders = 0;
8871 bool uncloneable_encoders = false;
8872 struct intel_encoder *encoder;
8873
8874 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8875 base.head) {
8876 if (&encoder->new_crtc->base != crtc)
8877 continue;
8878
8879 num_encoders++;
8880 if (!encoder->cloneable)
8881 uncloneable_encoders = true;
8882 }
8883
8884 return !(num_encoders > 1 && uncloneable_encoders);
8885}
8886
b8cecdf5
DV
8887static struct intel_crtc_config *
8888intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 8889 struct drm_framebuffer *fb,
b8cecdf5 8890 struct drm_display_mode *mode)
ee7b9f93 8891{
7758a113 8892 struct drm_device *dev = crtc->dev;
7758a113 8893 struct intel_encoder *encoder;
b8cecdf5 8894 struct intel_crtc_config *pipe_config;
e29c22c0
DV
8895 int plane_bpp, ret = -EINVAL;
8896 bool retry = true;
ee7b9f93 8897
accfc0c5
DV
8898 if (!check_encoder_cloning(crtc)) {
8899 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8900 return ERR_PTR(-EINVAL);
8901 }
8902
b8cecdf5
DV
8903 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8904 if (!pipe_config)
7758a113
DV
8905 return ERR_PTR(-ENOMEM);
8906
b8cecdf5
DV
8907 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8908 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 8909
e143a21c
DV
8910 pipe_config->cpu_transcoder =
8911 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 8912 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 8913
2960bc9c
ID
8914 /*
8915 * Sanitize sync polarity flags based on requested ones. If neither
8916 * positive or negative polarity is requested, treat this as meaning
8917 * negative polarity.
8918 */
8919 if (!(pipe_config->adjusted_mode.flags &
8920 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8921 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8922
8923 if (!(pipe_config->adjusted_mode.flags &
8924 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8925 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8926
050f7aeb
DV
8927 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8928 * plane pixel format and any sink constraints into account. Returns the
8929 * source plane bpp so that dithering can be selected on mismatches
8930 * after encoders and crtc also have had their say. */
8931 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8932 fb, pipe_config);
4e53c2e0
DV
8933 if (plane_bpp < 0)
8934 goto fail;
8935
e41a56be
VS
8936 /*
8937 * Determine the real pipe dimensions. Note that stereo modes can
8938 * increase the actual pipe size due to the frame doubling and
8939 * insertion of additional space for blanks between the frame. This
8940 * is stored in the crtc timings. We use the requested mode to do this
8941 * computation to clearly distinguish it from the adjusted mode, which
8942 * can be changed by the connectors in the below retry loop.
8943 */
8944 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
8945 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
8946 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
8947
e29c22c0 8948encoder_retry:
ef1b460d 8949 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 8950 pipe_config->port_clock = 0;
ef1b460d 8951 pipe_config->pixel_multiplier = 1;
ff9a6750 8952
135c81b8 8953 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 8954 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 8955
7758a113
DV
8956 /* Pass our mode to the connectors and the CRTC to give them a chance to
8957 * adjust it according to limitations or connector properties, and also
8958 * a chance to reject the mode entirely.
47f1c6c9 8959 */
7758a113
DV
8960 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8961 base.head) {
47f1c6c9 8962
7758a113
DV
8963 if (&encoder->new_crtc->base != crtc)
8964 continue;
7ae89233 8965
efea6e8e
DV
8966 if (!(encoder->compute_config(encoder, pipe_config))) {
8967 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
8968 goto fail;
8969 }
ee7b9f93 8970 }
47f1c6c9 8971
ff9a6750
DV
8972 /* Set default port clock if not overwritten by the encoder. Needs to be
8973 * done afterwards in case the encoder adjusts the mode. */
8974 if (!pipe_config->port_clock)
241bfc38
DL
8975 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
8976 * pipe_config->pixel_multiplier;
ff9a6750 8977
a43f6e0f 8978 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 8979 if (ret < 0) {
7758a113
DV
8980 DRM_DEBUG_KMS("CRTC fixup failed\n");
8981 goto fail;
ee7b9f93 8982 }
e29c22c0
DV
8983
8984 if (ret == RETRY) {
8985 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8986 ret = -EINVAL;
8987 goto fail;
8988 }
8989
8990 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8991 retry = false;
8992 goto encoder_retry;
8993 }
8994
4e53c2e0
DV
8995 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8996 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8997 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8998
b8cecdf5 8999 return pipe_config;
7758a113 9000fail:
b8cecdf5 9001 kfree(pipe_config);
e29c22c0 9002 return ERR_PTR(ret);
ee7b9f93 9003}
47f1c6c9 9004
e2e1ed41
DV
9005/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9006 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9007static void
9008intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9009 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
9010{
9011 struct intel_crtc *intel_crtc;
e2e1ed41
DV
9012 struct drm_device *dev = crtc->dev;
9013 struct intel_encoder *encoder;
9014 struct intel_connector *connector;
9015 struct drm_crtc *tmp_crtc;
79e53945 9016
e2e1ed41 9017 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 9018
e2e1ed41
DV
9019 /* Check which crtcs have changed outputs connected to them, these need
9020 * to be part of the prepare_pipes mask. We don't (yet) support global
9021 * modeset across multiple crtcs, so modeset_pipes will only have one
9022 * bit set at most. */
9023 list_for_each_entry(connector, &dev->mode_config.connector_list,
9024 base.head) {
9025 if (connector->base.encoder == &connector->new_encoder->base)
9026 continue;
79e53945 9027
e2e1ed41
DV
9028 if (connector->base.encoder) {
9029 tmp_crtc = connector->base.encoder->crtc;
9030
9031 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9032 }
9033
9034 if (connector->new_encoder)
9035 *prepare_pipes |=
9036 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
9037 }
9038
e2e1ed41
DV
9039 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9040 base.head) {
9041 if (encoder->base.crtc == &encoder->new_crtc->base)
9042 continue;
9043
9044 if (encoder->base.crtc) {
9045 tmp_crtc = encoder->base.crtc;
9046
9047 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9048 }
9049
9050 if (encoder->new_crtc)
9051 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
9052 }
9053
e2e1ed41
DV
9054 /* Check for any pipes that will be fully disabled ... */
9055 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9056 base.head) {
9057 bool used = false;
22fd0fab 9058
e2e1ed41
DV
9059 /* Don't try to disable disabled crtcs. */
9060 if (!intel_crtc->base.enabled)
9061 continue;
7e7d76c3 9062
e2e1ed41
DV
9063 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9064 base.head) {
9065 if (encoder->new_crtc == intel_crtc)
9066 used = true;
9067 }
9068
9069 if (!used)
9070 *disable_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
9071 }
9072
e2e1ed41
DV
9073
9074 /* set_mode is also used to update properties on life display pipes. */
9075 intel_crtc = to_intel_crtc(crtc);
9076 if (crtc->enabled)
9077 *prepare_pipes |= 1 << intel_crtc->pipe;
9078
b6c5164d
DV
9079 /*
9080 * For simplicity do a full modeset on any pipe where the output routing
9081 * changed. We could be more clever, but that would require us to be
9082 * more careful with calling the relevant encoder->mode_set functions.
9083 */
e2e1ed41
DV
9084 if (*prepare_pipes)
9085 *modeset_pipes = *prepare_pipes;
9086
9087 /* ... and mask these out. */
9088 *modeset_pipes &= ~(*disable_pipes);
9089 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
9090
9091 /*
9092 * HACK: We don't (yet) fully support global modesets. intel_set_config
9093 * obies this rule, but the modeset restore mode of
9094 * intel_modeset_setup_hw_state does not.
9095 */
9096 *modeset_pipes &= 1 << intel_crtc->pipe;
9097 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
9098
9099 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9100 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 9101}
79e53945 9102
ea9d758d 9103static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 9104{
ea9d758d 9105 struct drm_encoder *encoder;
f6e5b160 9106 struct drm_device *dev = crtc->dev;
f6e5b160 9107
ea9d758d
DV
9108 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9109 if (encoder->crtc == crtc)
9110 return true;
9111
9112 return false;
9113}
9114
9115static void
9116intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9117{
9118 struct intel_encoder *intel_encoder;
9119 struct intel_crtc *intel_crtc;
9120 struct drm_connector *connector;
9121
9122 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9123 base.head) {
9124 if (!intel_encoder->base.crtc)
9125 continue;
9126
9127 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9128
9129 if (prepare_pipes & (1 << intel_crtc->pipe))
9130 intel_encoder->connectors_active = false;
9131 }
9132
9133 intel_modeset_commit_output_state(dev);
9134
9135 /* Update computed state. */
9136 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9137 base.head) {
9138 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
9139 }
9140
9141 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9142 if (!connector->encoder || !connector->encoder->crtc)
9143 continue;
9144
9145 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9146
9147 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
9148 struct drm_property *dpms_property =
9149 dev->mode_config.dpms_property;
9150
ea9d758d 9151 connector->dpms = DRM_MODE_DPMS_ON;
662595df 9152 drm_object_property_set_value(&connector->base,
68d34720
DV
9153 dpms_property,
9154 DRM_MODE_DPMS_ON);
ea9d758d
DV
9155
9156 intel_encoder = to_intel_encoder(connector->encoder);
9157 intel_encoder->connectors_active = true;
9158 }
9159 }
9160
9161}
9162
3bd26263 9163static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 9164{
3bd26263 9165 int diff;
f1f644dc
JB
9166
9167 if (clock1 == clock2)
9168 return true;
9169
9170 if (!clock1 || !clock2)
9171 return false;
9172
9173 diff = abs(clock1 - clock2);
9174
9175 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9176 return true;
9177
9178 return false;
9179}
9180
25c5b266
DV
9181#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9182 list_for_each_entry((intel_crtc), \
9183 &(dev)->mode_config.crtc_list, \
9184 base.head) \
0973f18f 9185 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 9186
0e8ffe1b 9187static bool
2fa2fe9a
DV
9188intel_pipe_config_compare(struct drm_device *dev,
9189 struct intel_crtc_config *current_config,
0e8ffe1b
DV
9190 struct intel_crtc_config *pipe_config)
9191{
66e985c0
DV
9192#define PIPE_CONF_CHECK_X(name) \
9193 if (current_config->name != pipe_config->name) { \
9194 DRM_ERROR("mismatch in " #name " " \
9195 "(expected 0x%08x, found 0x%08x)\n", \
9196 current_config->name, \
9197 pipe_config->name); \
9198 return false; \
9199 }
9200
08a24034
DV
9201#define PIPE_CONF_CHECK_I(name) \
9202 if (current_config->name != pipe_config->name) { \
9203 DRM_ERROR("mismatch in " #name " " \
9204 "(expected %i, found %i)\n", \
9205 current_config->name, \
9206 pipe_config->name); \
9207 return false; \
88adfff1
DV
9208 }
9209
1bd1bd80
DV
9210#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9211 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 9212 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
9213 "(expected %i, found %i)\n", \
9214 current_config->name & (mask), \
9215 pipe_config->name & (mask)); \
9216 return false; \
9217 }
9218
5e550656
VS
9219#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9220 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9221 DRM_ERROR("mismatch in " #name " " \
9222 "(expected %i, found %i)\n", \
9223 current_config->name, \
9224 pipe_config->name); \
9225 return false; \
9226 }
9227
bb760063
DV
9228#define PIPE_CONF_QUIRK(quirk) \
9229 ((current_config->quirks | pipe_config->quirks) & (quirk))
9230
eccb140b
DV
9231 PIPE_CONF_CHECK_I(cpu_transcoder);
9232
08a24034
DV
9233 PIPE_CONF_CHECK_I(has_pch_encoder);
9234 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
9235 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9236 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9237 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9238 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9239 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 9240
eb14cb74
VS
9241 PIPE_CONF_CHECK_I(has_dp_encoder);
9242 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9243 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9244 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9245 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9246 PIPE_CONF_CHECK_I(dp_m_n.tu);
9247
1bd1bd80
DV
9248 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9249 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9250 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9251 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9252 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9253 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9254
9255 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9256 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9257 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9258 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9259 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9260 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9261
c93f54cf 9262 PIPE_CONF_CHECK_I(pixel_multiplier);
6c49f241 9263
1bd1bd80
DV
9264 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9265 DRM_MODE_FLAG_INTERLACE);
9266
bb760063
DV
9267 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9268 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9269 DRM_MODE_FLAG_PHSYNC);
9270 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9271 DRM_MODE_FLAG_NHSYNC);
9272 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9273 DRM_MODE_FLAG_PVSYNC);
9274 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9275 DRM_MODE_FLAG_NVSYNC);
9276 }
045ac3b5 9277
37327abd
VS
9278 PIPE_CONF_CHECK_I(pipe_src_w);
9279 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 9280
2fa2fe9a
DV
9281 PIPE_CONF_CHECK_I(gmch_pfit.control);
9282 /* pfit ratios are autocomputed by the hw on gen4+ */
9283 if (INTEL_INFO(dev)->gen < 4)
9284 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9285 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
fd4daa9c
CW
9286 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9287 if (current_config->pch_pfit.enabled) {
9288 PIPE_CONF_CHECK_I(pch_pfit.pos);
9289 PIPE_CONF_CHECK_I(pch_pfit.size);
9290 }
2fa2fe9a 9291
42db64ef
PZ
9292 PIPE_CONF_CHECK_I(ips_enabled);
9293
282740f7
VS
9294 PIPE_CONF_CHECK_I(double_wide);
9295
c0d43d62 9296 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 9297 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 9298 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
9299 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9300 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
c0d43d62 9301
42571aef
VS
9302 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9303 PIPE_CONF_CHECK_I(pipe_bpp);
9304
d71b8d4a 9305 if (!IS_HASWELL(dev)) {
241bfc38 9306 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
d71b8d4a
VS
9307 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9308 }
5e550656 9309
66e985c0 9310#undef PIPE_CONF_CHECK_X
08a24034 9311#undef PIPE_CONF_CHECK_I
1bd1bd80 9312#undef PIPE_CONF_CHECK_FLAGS
5e550656 9313#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 9314#undef PIPE_CONF_QUIRK
88adfff1 9315
0e8ffe1b
DV
9316 return true;
9317}
9318
91d1b4bd
DV
9319static void
9320check_connector_state(struct drm_device *dev)
8af6cf88 9321{
8af6cf88
DV
9322 struct intel_connector *connector;
9323
9324 list_for_each_entry(connector, &dev->mode_config.connector_list,
9325 base.head) {
9326 /* This also checks the encoder/connector hw state with the
9327 * ->get_hw_state callbacks. */
9328 intel_connector_check_state(connector);
9329
9330 WARN(&connector->new_encoder->base != connector->base.encoder,
9331 "connector's staged encoder doesn't match current encoder\n");
9332 }
91d1b4bd
DV
9333}
9334
9335static void
9336check_encoder_state(struct drm_device *dev)
9337{
9338 struct intel_encoder *encoder;
9339 struct intel_connector *connector;
8af6cf88
DV
9340
9341 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9342 base.head) {
9343 bool enabled = false;
9344 bool active = false;
9345 enum pipe pipe, tracked_pipe;
9346
9347 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9348 encoder->base.base.id,
9349 drm_get_encoder_name(&encoder->base));
9350
9351 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9352 "encoder's stage crtc doesn't match current crtc\n");
9353 WARN(encoder->connectors_active && !encoder->base.crtc,
9354 "encoder's active_connectors set, but no crtc\n");
9355
9356 list_for_each_entry(connector, &dev->mode_config.connector_list,
9357 base.head) {
9358 if (connector->base.encoder != &encoder->base)
9359 continue;
9360 enabled = true;
9361 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9362 active = true;
9363 }
9364 WARN(!!encoder->base.crtc != enabled,
9365 "encoder's enabled state mismatch "
9366 "(expected %i, found %i)\n",
9367 !!encoder->base.crtc, enabled);
9368 WARN(active && !encoder->base.crtc,
9369 "active encoder with no crtc\n");
9370
9371 WARN(encoder->connectors_active != active,
9372 "encoder's computed active state doesn't match tracked active state "
9373 "(expected %i, found %i)\n", active, encoder->connectors_active);
9374
9375 active = encoder->get_hw_state(encoder, &pipe);
9376 WARN(active != encoder->connectors_active,
9377 "encoder's hw state doesn't match sw tracking "
9378 "(expected %i, found %i)\n",
9379 encoder->connectors_active, active);
9380
9381 if (!encoder->base.crtc)
9382 continue;
9383
9384 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9385 WARN(active && pipe != tracked_pipe,
9386 "active encoder's pipe doesn't match"
9387 "(expected %i, found %i)\n",
9388 tracked_pipe, pipe);
9389
9390 }
91d1b4bd
DV
9391}
9392
9393static void
9394check_crtc_state(struct drm_device *dev)
9395{
9396 drm_i915_private_t *dev_priv = dev->dev_private;
9397 struct intel_crtc *crtc;
9398 struct intel_encoder *encoder;
9399 struct intel_crtc_config pipe_config;
8af6cf88
DV
9400
9401 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9402 base.head) {
9403 bool enabled = false;
9404 bool active = false;
9405
045ac3b5
JB
9406 memset(&pipe_config, 0, sizeof(pipe_config));
9407
8af6cf88
DV
9408 DRM_DEBUG_KMS("[CRTC:%d]\n",
9409 crtc->base.base.id);
9410
9411 WARN(crtc->active && !crtc->base.enabled,
9412 "active crtc, but not enabled in sw tracking\n");
9413
9414 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9415 base.head) {
9416 if (encoder->base.crtc != &crtc->base)
9417 continue;
9418 enabled = true;
9419 if (encoder->connectors_active)
9420 active = true;
9421 }
6c49f241 9422
8af6cf88
DV
9423 WARN(active != crtc->active,
9424 "crtc's computed active state doesn't match tracked active state "
9425 "(expected %i, found %i)\n", active, crtc->active);
9426 WARN(enabled != crtc->base.enabled,
9427 "crtc's computed enabled state doesn't match tracked enabled state "
9428 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9429
0e8ffe1b
DV
9430 active = dev_priv->display.get_pipe_config(crtc,
9431 &pipe_config);
d62cf62a
DV
9432
9433 /* hw state is inconsistent with the pipe A quirk */
9434 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9435 active = crtc->active;
9436
6c49f241
DV
9437 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9438 base.head) {
3eaba51c 9439 enum pipe pipe;
6c49f241
DV
9440 if (encoder->base.crtc != &crtc->base)
9441 continue;
3eaba51c
VS
9442 if (encoder->get_config &&
9443 encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
9444 encoder->get_config(encoder, &pipe_config);
9445 }
9446
0e8ffe1b
DV
9447 WARN(crtc->active != active,
9448 "crtc active state doesn't match with hw state "
9449 "(expected %i, found %i)\n", crtc->active, active);
9450
c0b03411
DV
9451 if (active &&
9452 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9453 WARN(1, "pipe state doesn't match!\n");
9454 intel_dump_pipe_config(crtc, &pipe_config,
9455 "[hw state]");
9456 intel_dump_pipe_config(crtc, &crtc->config,
9457 "[sw state]");
9458 }
8af6cf88
DV
9459 }
9460}
9461
91d1b4bd
DV
9462static void
9463check_shared_dpll_state(struct drm_device *dev)
9464{
9465 drm_i915_private_t *dev_priv = dev->dev_private;
9466 struct intel_crtc *crtc;
9467 struct intel_dpll_hw_state dpll_hw_state;
9468 int i;
5358901f
DV
9469
9470 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9471 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9472 int enabled_crtcs = 0, active_crtcs = 0;
9473 bool active;
9474
9475 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9476
9477 DRM_DEBUG_KMS("%s\n", pll->name);
9478
9479 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9480
9481 WARN(pll->active > pll->refcount,
9482 "more active pll users than references: %i vs %i\n",
9483 pll->active, pll->refcount);
9484 WARN(pll->active && !pll->on,
9485 "pll in active use but not on in sw tracking\n");
35c95375
DV
9486 WARN(pll->on && !pll->active,
9487 "pll in on but not on in use in sw tracking\n");
5358901f
DV
9488 WARN(pll->on != active,
9489 "pll on state mismatch (expected %i, found %i)\n",
9490 pll->on, active);
9491
9492 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9493 base.head) {
9494 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9495 enabled_crtcs++;
9496 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9497 active_crtcs++;
9498 }
9499 WARN(pll->active != active_crtcs,
9500 "pll active crtcs mismatch (expected %i, found %i)\n",
9501 pll->active, active_crtcs);
9502 WARN(pll->refcount != enabled_crtcs,
9503 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9504 pll->refcount, enabled_crtcs);
66e985c0
DV
9505
9506 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9507 sizeof(dpll_hw_state)),
9508 "pll hw state mismatch\n");
5358901f 9509 }
8af6cf88
DV
9510}
9511
91d1b4bd
DV
9512void
9513intel_modeset_check_state(struct drm_device *dev)
9514{
9515 check_connector_state(dev);
9516 check_encoder_state(dev);
9517 check_crtc_state(dev);
9518 check_shared_dpll_state(dev);
9519}
9520
18442d08
VS
9521void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9522 int dotclock)
9523{
9524 /*
9525 * FDI already provided one idea for the dotclock.
9526 * Yell if the encoder disagrees.
9527 */
241bfc38 9528 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 9529 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 9530 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
9531}
9532
f30da187
DV
9533static int __intel_set_mode(struct drm_crtc *crtc,
9534 struct drm_display_mode *mode,
9535 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
9536{
9537 struct drm_device *dev = crtc->dev;
dbf2b54e 9538 drm_i915_private_t *dev_priv = dev->dev_private;
b8cecdf5
DV
9539 struct drm_display_mode *saved_mode, *saved_hwmode;
9540 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
9541 struct intel_crtc *intel_crtc;
9542 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 9543 int ret = 0;
a6778b3c 9544
a1e22653 9545 saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
9546 if (!saved_mode)
9547 return -ENOMEM;
3ac18232 9548 saved_hwmode = saved_mode + 1;
a6778b3c 9549
e2e1ed41 9550 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
9551 &prepare_pipes, &disable_pipes);
9552
3ac18232
TG
9553 *saved_hwmode = crtc->hwmode;
9554 *saved_mode = crtc->mode;
a6778b3c 9555
25c5b266
DV
9556 /* Hack: Because we don't (yet) support global modeset on multiple
9557 * crtcs, we don't keep track of the new mode for more than one crtc.
9558 * Hence simply check whether any bit is set in modeset_pipes in all the
9559 * pieces of code that are not yet converted to deal with mutliple crtcs
9560 * changing their mode at the same time. */
25c5b266 9561 if (modeset_pipes) {
4e53c2e0 9562 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
9563 if (IS_ERR(pipe_config)) {
9564 ret = PTR_ERR(pipe_config);
9565 pipe_config = NULL;
9566
3ac18232 9567 goto out;
25c5b266 9568 }
c0b03411
DV
9569 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9570 "[modeset]");
25c5b266 9571 }
a6778b3c 9572
30a970c6
JB
9573 /*
9574 * See if the config requires any additional preparation, e.g.
9575 * to adjust global state with pipes off. We need to do this
9576 * here so we can get the modeset_pipe updated config for the new
9577 * mode set on this crtc. For other crtcs we need to use the
9578 * adjusted_mode bits in the crtc directly.
9579 */
c164f833 9580 if (IS_VALLEYVIEW(dev)) {
30a970c6
JB
9581 valleyview_modeset_global_pipes(dev, &prepare_pipes,
9582 modeset_pipes, pipe_config);
9583
c164f833
VS
9584 /* may have added more to prepare_pipes than we should */
9585 prepare_pipes &= ~disable_pipes;
9586 }
9587
460da916
DV
9588 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9589 intel_crtc_disable(&intel_crtc->base);
9590
ea9d758d
DV
9591 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9592 if (intel_crtc->base.enabled)
9593 dev_priv->display.crtc_disable(&intel_crtc->base);
9594 }
a6778b3c 9595
6c4c86f5
DV
9596 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9597 * to set it here already despite that we pass it down the callchain.
f6e5b160 9598 */
b8cecdf5 9599 if (modeset_pipes) {
25c5b266 9600 crtc->mode = *mode;
b8cecdf5
DV
9601 /* mode_set/enable/disable functions rely on a correct pipe
9602 * config. */
9603 to_intel_crtc(crtc)->config = *pipe_config;
9604 }
7758a113 9605
ea9d758d
DV
9606 /* Only after disabling all output pipelines that will be changed can we
9607 * update the the output configuration. */
9608 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 9609
47fab737
DV
9610 if (dev_priv->display.modeset_global_resources)
9611 dev_priv->display.modeset_global_resources(dev);
9612
a6778b3c
DV
9613 /* Set up the DPLL and any encoders state that needs to adjust or depend
9614 * on the DPLL.
f6e5b160 9615 */
25c5b266 9616 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
c0c36b94 9617 ret = intel_crtc_mode_set(&intel_crtc->base,
c0c36b94
CW
9618 x, y, fb);
9619 if (ret)
9620 goto done;
a6778b3c
DV
9621 }
9622
9623 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
25c5b266
DV
9624 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
9625 dev_priv->display.crtc_enable(&intel_crtc->base);
a6778b3c 9626
25c5b266
DV
9627 if (modeset_pipes) {
9628 /* Store real post-adjustment hardware mode. */
b8cecdf5 9629 crtc->hwmode = pipe_config->adjusted_mode;
a6778b3c 9630
25c5b266
DV
9631 /* Calculate and store various constants which
9632 * are later needed by vblank and swap-completion
9633 * timestamping. They are derived from true hwmode.
9634 */
9635 drm_calc_timestamping_constants(crtc);
9636 }
a6778b3c
DV
9637
9638 /* FIXME: add subpixel order */
9639done:
c0c36b94 9640 if (ret && crtc->enabled) {
3ac18232
TG
9641 crtc->hwmode = *saved_hwmode;
9642 crtc->mode = *saved_mode;
a6778b3c
DV
9643 }
9644
3ac18232 9645out:
b8cecdf5 9646 kfree(pipe_config);
3ac18232 9647 kfree(saved_mode);
a6778b3c 9648 return ret;
f6e5b160
CW
9649}
9650
e7457a9a
DL
9651static int intel_set_mode(struct drm_crtc *crtc,
9652 struct drm_display_mode *mode,
9653 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
9654{
9655 int ret;
9656
9657 ret = __intel_set_mode(crtc, mode, x, y, fb);
9658
9659 if (ret == 0)
9660 intel_modeset_check_state(crtc->dev);
9661
9662 return ret;
9663}
9664
c0c36b94
CW
9665void intel_crtc_restore_mode(struct drm_crtc *crtc)
9666{
9667 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9668}
9669
25c5b266
DV
9670#undef for_each_intel_crtc_masked
9671
d9e55608
DV
9672static void intel_set_config_free(struct intel_set_config *config)
9673{
9674 if (!config)
9675 return;
9676
1aa4b628
DV
9677 kfree(config->save_connector_encoders);
9678 kfree(config->save_encoder_crtcs);
d9e55608
DV
9679 kfree(config);
9680}
9681
85f9eb71
DV
9682static int intel_set_config_save_state(struct drm_device *dev,
9683 struct intel_set_config *config)
9684{
85f9eb71
DV
9685 struct drm_encoder *encoder;
9686 struct drm_connector *connector;
9687 int count;
9688
1aa4b628
DV
9689 config->save_encoder_crtcs =
9690 kcalloc(dev->mode_config.num_encoder,
9691 sizeof(struct drm_crtc *), GFP_KERNEL);
9692 if (!config->save_encoder_crtcs)
85f9eb71
DV
9693 return -ENOMEM;
9694
1aa4b628
DV
9695 config->save_connector_encoders =
9696 kcalloc(dev->mode_config.num_connector,
9697 sizeof(struct drm_encoder *), GFP_KERNEL);
9698 if (!config->save_connector_encoders)
85f9eb71
DV
9699 return -ENOMEM;
9700
9701 /* Copy data. Note that driver private data is not affected.
9702 * Should anything bad happen only the expected state is
9703 * restored, not the drivers personal bookkeeping.
9704 */
85f9eb71
DV
9705 count = 0;
9706 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 9707 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
9708 }
9709
9710 count = 0;
9711 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 9712 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
9713 }
9714
9715 return 0;
9716}
9717
9718static void intel_set_config_restore_state(struct drm_device *dev,
9719 struct intel_set_config *config)
9720{
9a935856
DV
9721 struct intel_encoder *encoder;
9722 struct intel_connector *connector;
85f9eb71
DV
9723 int count;
9724
85f9eb71 9725 count = 0;
9a935856
DV
9726 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9727 encoder->new_crtc =
9728 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
9729 }
9730
9731 count = 0;
9a935856
DV
9732 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9733 connector->new_encoder =
9734 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
9735 }
9736}
9737
e3de42b6 9738static bool
2e57f47d 9739is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
9740{
9741 int i;
9742
2e57f47d
CW
9743 if (set->num_connectors == 0)
9744 return false;
9745
9746 if (WARN_ON(set->connectors == NULL))
9747 return false;
9748
9749 for (i = 0; i < set->num_connectors; i++)
9750 if (set->connectors[i]->encoder &&
9751 set->connectors[i]->encoder->crtc == set->crtc &&
9752 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
9753 return true;
9754
9755 return false;
9756}
9757
5e2b584e
DV
9758static void
9759intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9760 struct intel_set_config *config)
9761{
9762
9763 /* We should be able to check here if the fb has the same properties
9764 * and then just flip_or_move it */
2e57f47d
CW
9765 if (is_crtc_connector_off(set)) {
9766 config->mode_changed = true;
e3de42b6 9767 } else if (set->crtc->fb != set->fb) {
5e2b584e
DV
9768 /* If we have no fb then treat it as a full mode set */
9769 if (set->crtc->fb == NULL) {
319d9827
JB
9770 struct intel_crtc *intel_crtc =
9771 to_intel_crtc(set->crtc);
9772
9773 if (intel_crtc->active && i915_fastboot) {
9774 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9775 config->fb_changed = true;
9776 } else {
9777 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9778 config->mode_changed = true;
9779 }
5e2b584e
DV
9780 } else if (set->fb == NULL) {
9781 config->mode_changed = true;
72f4901e
DV
9782 } else if (set->fb->pixel_format !=
9783 set->crtc->fb->pixel_format) {
5e2b584e 9784 config->mode_changed = true;
e3de42b6 9785 } else {
5e2b584e 9786 config->fb_changed = true;
e3de42b6 9787 }
5e2b584e
DV
9788 }
9789
835c5873 9790 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
9791 config->fb_changed = true;
9792
9793 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9794 DRM_DEBUG_KMS("modes are different, full mode set\n");
9795 drm_mode_debug_printmodeline(&set->crtc->mode);
9796 drm_mode_debug_printmodeline(set->mode);
9797 config->mode_changed = true;
9798 }
a1d95703
CW
9799
9800 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9801 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
9802}
9803
2e431051 9804static int
9a935856
DV
9805intel_modeset_stage_output_state(struct drm_device *dev,
9806 struct drm_mode_set *set,
9807 struct intel_set_config *config)
50f56119 9808{
85f9eb71 9809 struct drm_crtc *new_crtc;
9a935856
DV
9810 struct intel_connector *connector;
9811 struct intel_encoder *encoder;
f3f08572 9812 int ro;
50f56119 9813
9abdda74 9814 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
9815 * of connectors. For paranoia, double-check this. */
9816 WARN_ON(!set->fb && (set->num_connectors != 0));
9817 WARN_ON(set->fb && (set->num_connectors == 0));
9818
9a935856
DV
9819 list_for_each_entry(connector, &dev->mode_config.connector_list,
9820 base.head) {
9821 /* Otherwise traverse passed in connector list and get encoders
9822 * for them. */
50f56119 9823 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856
DV
9824 if (set->connectors[ro] == &connector->base) {
9825 connector->new_encoder = connector->encoder;
50f56119
DV
9826 break;
9827 }
9828 }
9829
9a935856
DV
9830 /* If we disable the crtc, disable all its connectors. Also, if
9831 * the connector is on the changing crtc but not on the new
9832 * connector list, disable it. */
9833 if ((!set->fb || ro == set->num_connectors) &&
9834 connector->base.encoder &&
9835 connector->base.encoder->crtc == set->crtc) {
9836 connector->new_encoder = NULL;
9837
9838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9839 connector->base.base.id,
9840 drm_get_connector_name(&connector->base));
9841 }
9842
9843
9844 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 9845 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 9846 config->mode_changed = true;
50f56119
DV
9847 }
9848 }
9a935856 9849 /* connector->new_encoder is now updated for all connectors. */
50f56119 9850
9a935856 9851 /* Update crtc of enabled connectors. */
9a935856
DV
9852 list_for_each_entry(connector, &dev->mode_config.connector_list,
9853 base.head) {
9854 if (!connector->new_encoder)
50f56119
DV
9855 continue;
9856
9a935856 9857 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
9858
9859 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 9860 if (set->connectors[ro] == &connector->base)
50f56119
DV
9861 new_crtc = set->crtc;
9862 }
9863
9864 /* Make sure the new CRTC will work with the encoder */
9a935856
DV
9865 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9866 new_crtc)) {
5e2b584e 9867 return -EINVAL;
50f56119 9868 }
9a935856
DV
9869 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9870
9871 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9872 connector->base.base.id,
9873 drm_get_connector_name(&connector->base),
9874 new_crtc->base.id);
9875 }
9876
9877 /* Check for any encoders that needs to be disabled. */
9878 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9879 base.head) {
9880 list_for_each_entry(connector,
9881 &dev->mode_config.connector_list,
9882 base.head) {
9883 if (connector->new_encoder == encoder) {
9884 WARN_ON(!connector->new_encoder->new_crtc);
9885
9886 goto next_encoder;
9887 }
9888 }
9889 encoder->new_crtc = NULL;
9890next_encoder:
9891 /* Only now check for crtc changes so we don't miss encoders
9892 * that will be disabled. */
9893 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 9894 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 9895 config->mode_changed = true;
50f56119
DV
9896 }
9897 }
9a935856 9898 /* Now we've also updated encoder->new_crtc for all encoders. */
50f56119 9899
2e431051
DV
9900 return 0;
9901}
9902
9903static int intel_crtc_set_config(struct drm_mode_set *set)
9904{
9905 struct drm_device *dev;
2e431051
DV
9906 struct drm_mode_set save_set;
9907 struct intel_set_config *config;
9908 int ret;
2e431051 9909
8d3e375e
DV
9910 BUG_ON(!set);
9911 BUG_ON(!set->crtc);
9912 BUG_ON(!set->crtc->helper_private);
2e431051 9913
7e53f3a4
DV
9914 /* Enforce sane interface api - has been abused by the fb helper. */
9915 BUG_ON(!set->mode && set->fb);
9916 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 9917
2e431051
DV
9918 if (set->fb) {
9919 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9920 set->crtc->base.id, set->fb->base.id,
9921 (int)set->num_connectors, set->x, set->y);
9922 } else {
9923 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
9924 }
9925
9926 dev = set->crtc->dev;
9927
9928 ret = -ENOMEM;
9929 config = kzalloc(sizeof(*config), GFP_KERNEL);
9930 if (!config)
9931 goto out_config;
9932
9933 ret = intel_set_config_save_state(dev, config);
9934 if (ret)
9935 goto out_config;
9936
9937 save_set.crtc = set->crtc;
9938 save_set.mode = &set->crtc->mode;
9939 save_set.x = set->crtc->x;
9940 save_set.y = set->crtc->y;
9941 save_set.fb = set->crtc->fb;
9942
9943 /* Compute whether we need a full modeset, only an fb base update or no
9944 * change at all. In the future we might also check whether only the
9945 * mode changed, e.g. for LVDS where we only change the panel fitter in
9946 * such cases. */
9947 intel_set_config_compute_mode_changes(set, config);
9948
9a935856 9949 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
9950 if (ret)
9951 goto fail;
9952
5e2b584e 9953 if (config->mode_changed) {
c0c36b94
CW
9954 ret = intel_set_mode(set->crtc, set->mode,
9955 set->x, set->y, set->fb);
5e2b584e 9956 } else if (config->fb_changed) {
4878cae2
VS
9957 intel_crtc_wait_for_pending_flips(set->crtc);
9958
4f660f49 9959 ret = intel_pipe_set_base(set->crtc,
94352cf9 9960 set->x, set->y, set->fb);
50f56119
DV
9961 }
9962
2d05eae1 9963 if (ret) {
bf67dfeb
DV
9964 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9965 set->crtc->base.id, ret);
50f56119 9966fail:
2d05eae1 9967 intel_set_config_restore_state(dev, config);
50f56119 9968
2d05eae1
CW
9969 /* Try to restore the config */
9970 if (config->mode_changed &&
9971 intel_set_mode(save_set.crtc, save_set.mode,
9972 save_set.x, save_set.y, save_set.fb))
9973 DRM_ERROR("failed to restore config after modeset failure\n");
9974 }
50f56119 9975
d9e55608
DV
9976out_config:
9977 intel_set_config_free(config);
50f56119
DV
9978 return ret;
9979}
f6e5b160
CW
9980
9981static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160
CW
9982 .cursor_set = intel_crtc_cursor_set,
9983 .cursor_move = intel_crtc_cursor_move,
9984 .gamma_set = intel_crtc_gamma_set,
50f56119 9985 .set_config = intel_crtc_set_config,
f6e5b160
CW
9986 .destroy = intel_crtc_destroy,
9987 .page_flip = intel_crtc_page_flip,
9988};
9989
79f689aa
PZ
9990static void intel_cpu_pll_init(struct drm_device *dev)
9991{
affa9354 9992 if (HAS_DDI(dev))
79f689aa
PZ
9993 intel_ddi_pll_init(dev);
9994}
9995
5358901f
DV
9996static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9997 struct intel_shared_dpll *pll,
9998 struct intel_dpll_hw_state *hw_state)
ee7b9f93 9999{
5358901f 10000 uint32_t val;
ee7b9f93 10001
5358901f 10002 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
10003 hw_state->dpll = val;
10004 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10005 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
10006
10007 return val & DPLL_VCO_ENABLE;
10008}
10009
15bdd4cf
DV
10010static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10011 struct intel_shared_dpll *pll)
10012{
10013 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10014 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10015}
10016
e7b903d2
DV
10017static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10018 struct intel_shared_dpll *pll)
10019{
e7b903d2
DV
10020 /* PCH refclock must be enabled first */
10021 assert_pch_refclk_enabled(dev_priv);
10022
15bdd4cf
DV
10023 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10024
10025 /* Wait for the clocks to stabilize. */
10026 POSTING_READ(PCH_DPLL(pll->id));
10027 udelay(150);
10028
10029 /* The pixel multiplier can only be updated once the
10030 * DPLL is enabled and the clocks are stable.
10031 *
10032 * So write it again.
10033 */
10034 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10035 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10036 udelay(200);
10037}
10038
10039static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10040 struct intel_shared_dpll *pll)
10041{
10042 struct drm_device *dev = dev_priv->dev;
10043 struct intel_crtc *crtc;
e7b903d2
DV
10044
10045 /* Make sure no transcoder isn't still depending on us. */
10046 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10047 if (intel_crtc_to_shared_dpll(crtc) == pll)
10048 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
10049 }
10050
15bdd4cf
DV
10051 I915_WRITE(PCH_DPLL(pll->id), 0);
10052 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10053 udelay(200);
10054}
10055
46edb027
DV
10056static char *ibx_pch_dpll_names[] = {
10057 "PCH DPLL A",
10058 "PCH DPLL B",
10059};
10060
7c74ade1 10061static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 10062{
e7b903d2 10063 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
10064 int i;
10065
7c74ade1 10066 dev_priv->num_shared_dpll = 2;
ee7b9f93 10067
e72f9fbf 10068 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
10069 dev_priv->shared_dplls[i].id = i;
10070 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 10071 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
10072 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10073 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
10074 dev_priv->shared_dplls[i].get_hw_state =
10075 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
10076 }
10077}
10078
7c74ade1
DV
10079static void intel_shared_dpll_init(struct drm_device *dev)
10080{
e7b903d2 10081 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1
DV
10082
10083 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10084 ibx_pch_dpll_init(dev);
10085 else
10086 dev_priv->num_shared_dpll = 0;
10087
10088 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10089 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
10090 dev_priv->num_shared_dpll);
10091}
10092
b358d0a6 10093static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 10094{
22fd0fab 10095 drm_i915_private_t *dev_priv = dev->dev_private;
79e53945
JB
10096 struct intel_crtc *intel_crtc;
10097 int i;
10098
955382f3 10099 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
10100 if (intel_crtc == NULL)
10101 return;
10102
10103 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10104
10105 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
10106 for (i = 0; i < 256; i++) {
10107 intel_crtc->lut_r[i] = i;
10108 intel_crtc->lut_g[i] = i;
10109 intel_crtc->lut_b[i] = i;
10110 }
10111
80824003
JB
10112 /* Swap pipes & planes for FBC on pre-965 */
10113 intel_crtc->pipe = pipe;
10114 intel_crtc->plane = pipe;
e2e767ab 10115 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
28c97730 10116 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 10117 intel_crtc->plane = !pipe;
80824003
JB
10118 }
10119
22fd0fab
JB
10120 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10121 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10122 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10123 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10124
79e53945 10125 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
79e53945
JB
10126}
10127
752aa88a
JB
10128enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10129{
10130 struct drm_encoder *encoder = connector->base.encoder;
10131
10132 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10133
10134 if (!encoder)
10135 return INVALID_PIPE;
10136
10137 return to_intel_crtc(encoder->crtc)->pipe;
10138}
10139
08d7b3d1 10140int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 10141 struct drm_file *file)
08d7b3d1 10142{
08d7b3d1 10143 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
10144 struct drm_mode_object *drmmode_obj;
10145 struct intel_crtc *crtc;
08d7b3d1 10146
1cff8f6b
DV
10147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10148 return -ENODEV;
08d7b3d1 10149
c05422d5
DV
10150 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10151 DRM_MODE_OBJECT_CRTC);
08d7b3d1 10152
c05422d5 10153 if (!drmmode_obj) {
08d7b3d1 10154 DRM_ERROR("no such CRTC id\n");
3f2c2057 10155 return -ENOENT;
08d7b3d1
CW
10156 }
10157
c05422d5
DV
10158 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10159 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 10160
c05422d5 10161 return 0;
08d7b3d1
CW
10162}
10163
66a9278e 10164static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 10165{
66a9278e
DV
10166 struct drm_device *dev = encoder->base.dev;
10167 struct intel_encoder *source_encoder;
79e53945 10168 int index_mask = 0;
79e53945
JB
10169 int entry = 0;
10170
66a9278e
DV
10171 list_for_each_entry(source_encoder,
10172 &dev->mode_config.encoder_list, base.head) {
10173
10174 if (encoder == source_encoder)
79e53945 10175 index_mask |= (1 << entry);
66a9278e
DV
10176
10177 /* Intel hw has only one MUX where enocoders could be cloned. */
10178 if (encoder->cloneable && source_encoder->cloneable)
10179 index_mask |= (1 << entry);
10180
79e53945
JB
10181 entry++;
10182 }
4ef69c7a 10183
79e53945
JB
10184 return index_mask;
10185}
10186
4d302442
CW
10187static bool has_edp_a(struct drm_device *dev)
10188{
10189 struct drm_i915_private *dev_priv = dev->dev_private;
10190
10191 if (!IS_MOBILE(dev))
10192 return false;
10193
10194 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10195 return false;
10196
10197 if (IS_GEN5(dev) &&
10198 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
10199 return false;
10200
10201 return true;
10202}
10203
79e53945
JB
10204static void intel_setup_outputs(struct drm_device *dev)
10205{
725e30ad 10206 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 10207 struct intel_encoder *encoder;
cb0953d7 10208 bool dpd_is_edp = false;
79e53945 10209
c9093354 10210 intel_lvds_init(dev);
79e53945 10211
c40c0f5b 10212 if (!IS_ULT(dev))
79935fca 10213 intel_crt_init(dev);
cb0953d7 10214
affa9354 10215 if (HAS_DDI(dev)) {
0e72a5b5
ED
10216 int found;
10217
10218 /* Haswell uses DDI functions to detect digital outputs */
10219 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10220 /* DDI A only supports eDP */
10221 if (found)
10222 intel_ddi_init(dev, PORT_A);
10223
10224 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10225 * register */
10226 found = I915_READ(SFUSE_STRAP);
10227
10228 if (found & SFUSE_STRAP_DDIB_DETECTED)
10229 intel_ddi_init(dev, PORT_B);
10230 if (found & SFUSE_STRAP_DDIC_DETECTED)
10231 intel_ddi_init(dev, PORT_C);
10232 if (found & SFUSE_STRAP_DDID_DETECTED)
10233 intel_ddi_init(dev, PORT_D);
10234 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 10235 int found;
270b3042
DV
10236 dpd_is_edp = intel_dpd_is_edp(dev);
10237
10238 if (has_edp_a(dev))
10239 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 10240
dc0fa718 10241 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 10242 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 10243 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 10244 if (!found)
e2debe91 10245 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 10246 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 10247 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
10248 }
10249
dc0fa718 10250 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 10251 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 10252
dc0fa718 10253 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 10254 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 10255
5eb08b69 10256 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 10257 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 10258
270b3042 10259 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 10260 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 10261 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
10262 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10263 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10264 PORT_B);
10265 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10266 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10267 }
10268
6f6005a5
JB
10269 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10270 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10271 PORT_C);
10272 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
10273 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
10274 PORT_C);
10275 }
19c03924 10276
3cfca973 10277 intel_dsi_init(dev);
103a196f 10278 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 10279 bool found = false;
7d57382e 10280
e2debe91 10281 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10282 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 10283 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
10284 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10285 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 10286 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 10287 }
27185ae1 10288
e7281eab 10289 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10290 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 10291 }
13520b05
KH
10292
10293 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 10294
e2debe91 10295 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10296 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 10297 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 10298 }
27185ae1 10299
e2debe91 10300 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 10301
b01f2c3a
JB
10302 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10303 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 10304 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 10305 }
e7281eab 10306 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10307 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 10308 }
27185ae1 10309
b01f2c3a 10310 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 10311 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 10312 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 10313 } else if (IS_GEN2(dev))
79e53945
JB
10314 intel_dvo_init(dev);
10315
103a196f 10316 if (SUPPORTS_TV(dev))
79e53945
JB
10317 intel_tv_init(dev);
10318
4ef69c7a
CW
10319 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10320 encoder->base.possible_crtcs = encoder->crtc_mask;
10321 encoder->base.possible_clones =
66a9278e 10322 intel_encoder_clones(encoder);
79e53945 10323 }
47356eb6 10324
dde86e2d 10325 intel_init_pch_refclk(dev);
270b3042
DV
10326
10327 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
10328}
10329
ddfe1567
CW
10330void intel_framebuffer_fini(struct intel_framebuffer *fb)
10331{
10332 drm_framebuffer_cleanup(&fb->base);
80075d49 10333 WARN_ON(!fb->obj->framebuffer_references--);
ddfe1567
CW
10334 drm_gem_object_unreference_unlocked(&fb->obj->base);
10335}
10336
79e53945
JB
10337static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10338{
10339 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 10340
ddfe1567 10341 intel_framebuffer_fini(intel_fb);
79e53945
JB
10342 kfree(intel_fb);
10343}
10344
10345static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 10346 struct drm_file *file,
79e53945
JB
10347 unsigned int *handle)
10348{
10349 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 10350 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 10351
05394f39 10352 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
10353}
10354
10355static const struct drm_framebuffer_funcs intel_fb_funcs = {
10356 .destroy = intel_user_framebuffer_destroy,
10357 .create_handle = intel_user_framebuffer_create_handle,
10358};
10359
38651674
DA
10360int intel_framebuffer_init(struct drm_device *dev,
10361 struct intel_framebuffer *intel_fb,
308e5bcb 10362 struct drm_mode_fb_cmd2 *mode_cmd,
05394f39 10363 struct drm_i915_gem_object *obj)
79e53945 10364{
53155c0a 10365 int aligned_height, tile_height;
a35cdaa0 10366 int pitch_limit;
79e53945
JB
10367 int ret;
10368
dd4916c5
DV
10369 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10370
c16ed4be
CW
10371 if (obj->tiling_mode == I915_TILING_Y) {
10372 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 10373 return -EINVAL;
c16ed4be 10374 }
57cd6508 10375
c16ed4be
CW
10376 if (mode_cmd->pitches[0] & 63) {
10377 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10378 mode_cmd->pitches[0]);
57cd6508 10379 return -EINVAL;
c16ed4be 10380 }
57cd6508 10381
a35cdaa0
CW
10382 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10383 pitch_limit = 32*1024;
10384 } else if (INTEL_INFO(dev)->gen >= 4) {
10385 if (obj->tiling_mode)
10386 pitch_limit = 16*1024;
10387 else
10388 pitch_limit = 32*1024;
10389 } else if (INTEL_INFO(dev)->gen >= 3) {
10390 if (obj->tiling_mode)
10391 pitch_limit = 8*1024;
10392 else
10393 pitch_limit = 16*1024;
10394 } else
10395 /* XXX DSPC is limited to 4k tiled */
10396 pitch_limit = 8*1024;
10397
10398 if (mode_cmd->pitches[0] > pitch_limit) {
10399 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10400 obj->tiling_mode ? "tiled" : "linear",
10401 mode_cmd->pitches[0], pitch_limit);
5d7bd705 10402 return -EINVAL;
c16ed4be 10403 }
5d7bd705
VS
10404
10405 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
10406 mode_cmd->pitches[0] != obj->stride) {
10407 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10408 mode_cmd->pitches[0], obj->stride);
5d7bd705 10409 return -EINVAL;
c16ed4be 10410 }
5d7bd705 10411
57779d06 10412 /* Reject formats not supported by any plane early. */
308e5bcb 10413 switch (mode_cmd->pixel_format) {
57779d06 10414 case DRM_FORMAT_C8:
04b3924d
VS
10415 case DRM_FORMAT_RGB565:
10416 case DRM_FORMAT_XRGB8888:
10417 case DRM_FORMAT_ARGB8888:
57779d06
VS
10418 break;
10419 case DRM_FORMAT_XRGB1555:
10420 case DRM_FORMAT_ARGB1555:
c16ed4be 10421 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
10422 DRM_DEBUG("unsupported pixel format: %s\n",
10423 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10424 return -EINVAL;
c16ed4be 10425 }
57779d06
VS
10426 break;
10427 case DRM_FORMAT_XBGR8888:
10428 case DRM_FORMAT_ABGR8888:
04b3924d
VS
10429 case DRM_FORMAT_XRGB2101010:
10430 case DRM_FORMAT_ARGB2101010:
57779d06
VS
10431 case DRM_FORMAT_XBGR2101010:
10432 case DRM_FORMAT_ABGR2101010:
c16ed4be 10433 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
10434 DRM_DEBUG("unsupported pixel format: %s\n",
10435 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10436 return -EINVAL;
c16ed4be 10437 }
b5626747 10438 break;
04b3924d
VS
10439 case DRM_FORMAT_YUYV:
10440 case DRM_FORMAT_UYVY:
10441 case DRM_FORMAT_YVYU:
10442 case DRM_FORMAT_VYUY:
c16ed4be 10443 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
10444 DRM_DEBUG("unsupported pixel format: %s\n",
10445 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10446 return -EINVAL;
c16ed4be 10447 }
57cd6508
CW
10448 break;
10449 default:
4ee62c76
VS
10450 DRM_DEBUG("unsupported pixel format: %s\n",
10451 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
10452 return -EINVAL;
10453 }
10454
90f9a336
VS
10455 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10456 if (mode_cmd->offsets[0] != 0)
10457 return -EINVAL;
10458
53155c0a
DV
10459 tile_height = IS_GEN2(dev) ? 16 : 8;
10460 aligned_height = ALIGN(mode_cmd->height,
10461 obj->tiling_mode ? tile_height : 1);
10462 /* FIXME drm helper for size checks (especially planar formats)? */
10463 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10464 return -EINVAL;
10465
c7d73f6a
DV
10466 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10467 intel_fb->obj = obj;
80075d49 10468 intel_fb->obj->framebuffer_references++;
c7d73f6a 10469
79e53945
JB
10470 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10471 if (ret) {
10472 DRM_ERROR("framebuffer init failed %d\n", ret);
10473 return ret;
10474 }
10475
79e53945
JB
10476 return 0;
10477}
10478
79e53945
JB
10479static struct drm_framebuffer *
10480intel_user_framebuffer_create(struct drm_device *dev,
10481 struct drm_file *filp,
308e5bcb 10482 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 10483{
05394f39 10484 struct drm_i915_gem_object *obj;
79e53945 10485
308e5bcb
JB
10486 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10487 mode_cmd->handles[0]));
c8725226 10488 if (&obj->base == NULL)
cce13ff7 10489 return ERR_PTR(-ENOENT);
79e53945 10490
d2dff872 10491 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
10492}
10493
4520f53a 10494#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 10495static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
10496{
10497}
10498#endif
10499
79e53945 10500static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 10501 .fb_create = intel_user_framebuffer_create,
0632fef6 10502 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
10503};
10504
e70236a8
JB
10505/* Set up chip specific display functions */
10506static void intel_init_display(struct drm_device *dev)
10507{
10508 struct drm_i915_private *dev_priv = dev->dev_private;
10509
ee9300bb
DV
10510 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
10511 dev_priv->display.find_dpll = g4x_find_best_dpll;
10512 else if (IS_VALLEYVIEW(dev))
10513 dev_priv->display.find_dpll = vlv_find_best_dpll;
10514 else if (IS_PINEVIEW(dev))
10515 dev_priv->display.find_dpll = pnv_find_best_dpll;
10516 else
10517 dev_priv->display.find_dpll = i9xx_find_best_dpll;
10518
affa9354 10519 if (HAS_DDI(dev)) {
0e8ffe1b 10520 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
09b4ddf9 10521 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
10522 dev_priv->display.crtc_enable = haswell_crtc_enable;
10523 dev_priv->display.crtc_disable = haswell_crtc_disable;
6441ab5f 10524 dev_priv->display.off = haswell_crtc_off;
09b4ddf9
PZ
10525 dev_priv->display.update_plane = ironlake_update_plane;
10526 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 10527 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
f564048e 10528 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
10529 dev_priv->display.crtc_enable = ironlake_crtc_enable;
10530 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 10531 dev_priv->display.off = ironlake_crtc_off;
17638cd6 10532 dev_priv->display.update_plane = ironlake_update_plane;
89b667f8
JB
10533 } else if (IS_VALLEYVIEW(dev)) {
10534 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
10535 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
10536 dev_priv->display.crtc_enable = valleyview_crtc_enable;
10537 dev_priv->display.crtc_disable = i9xx_crtc_disable;
10538 dev_priv->display.off = i9xx_crtc_off;
10539 dev_priv->display.update_plane = i9xx_update_plane;
f564048e 10540 } else {
0e8ffe1b 10541 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
f564048e 10542 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
10543 dev_priv->display.crtc_enable = i9xx_crtc_enable;
10544 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 10545 dev_priv->display.off = i9xx_crtc_off;
17638cd6 10546 dev_priv->display.update_plane = i9xx_update_plane;
f564048e 10547 }
e70236a8 10548
e70236a8 10549 /* Returns the core display clock speed */
25eb05fc
JB
10550 if (IS_VALLEYVIEW(dev))
10551 dev_priv->display.get_display_clock_speed =
10552 valleyview_get_display_clock_speed;
10553 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
10554 dev_priv->display.get_display_clock_speed =
10555 i945_get_display_clock_speed;
10556 else if (IS_I915G(dev))
10557 dev_priv->display.get_display_clock_speed =
10558 i915_get_display_clock_speed;
257a7ffc 10559 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
10560 dev_priv->display.get_display_clock_speed =
10561 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
10562 else if (IS_PINEVIEW(dev))
10563 dev_priv->display.get_display_clock_speed =
10564 pnv_get_display_clock_speed;
e70236a8
JB
10565 else if (IS_I915GM(dev))
10566 dev_priv->display.get_display_clock_speed =
10567 i915gm_get_display_clock_speed;
10568 else if (IS_I865G(dev))
10569 dev_priv->display.get_display_clock_speed =
10570 i865_get_display_clock_speed;
f0f8a9ce 10571 else if (IS_I85X(dev))
e70236a8
JB
10572 dev_priv->display.get_display_clock_speed =
10573 i855_get_display_clock_speed;
10574 else /* 852, 830 */
10575 dev_priv->display.get_display_clock_speed =
10576 i830_get_display_clock_speed;
10577
7f8a8569 10578 if (HAS_PCH_SPLIT(dev)) {
f00a3ddf 10579 if (IS_GEN5(dev)) {
674cf967 10580 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
e0dac65e 10581 dev_priv->display.write_eld = ironlake_write_eld;
1398261a 10582 } else if (IS_GEN6(dev)) {
674cf967 10583 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
e0dac65e 10584 dev_priv->display.write_eld = ironlake_write_eld;
357555c0
JB
10585 } else if (IS_IVYBRIDGE(dev)) {
10586 /* FIXME: detect B0+ stepping and use auto training */
10587 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
e0dac65e 10588 dev_priv->display.write_eld = ironlake_write_eld;
01a415fd
DV
10589 dev_priv->display.modeset_global_resources =
10590 ivb_modeset_global_resources;
4e0bbc31 10591 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
c82e4d26 10592 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
83358c85 10593 dev_priv->display.write_eld = haswell_write_eld;
d6dd9eb1
DV
10594 dev_priv->display.modeset_global_resources =
10595 haswell_modeset_global_resources;
a0e63c22 10596 }
6067aaea 10597 } else if (IS_G4X(dev)) {
e0dac65e 10598 dev_priv->display.write_eld = g4x_write_eld;
30a970c6
JB
10599 } else if (IS_VALLEYVIEW(dev)) {
10600 dev_priv->display.modeset_global_resources =
10601 valleyview_modeset_global_resources;
9ca2fe73 10602 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 10603 }
8c9f3aaf
JB
10604
10605 /* Default just returns -ENODEV to indicate unsupported */
10606 dev_priv->display.queue_flip = intel_default_queue_flip;
10607
10608 switch (INTEL_INFO(dev)->gen) {
10609 case 2:
10610 dev_priv->display.queue_flip = intel_gen2_queue_flip;
10611 break;
10612
10613 case 3:
10614 dev_priv->display.queue_flip = intel_gen3_queue_flip;
10615 break;
10616
10617 case 4:
10618 case 5:
10619 dev_priv->display.queue_flip = intel_gen4_queue_flip;
10620 break;
10621
10622 case 6:
10623 dev_priv->display.queue_flip = intel_gen6_queue_flip;
10624 break;
7c9017e5 10625 case 7:
4e0bbc31 10626 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
10627 dev_priv->display.queue_flip = intel_gen7_queue_flip;
10628 break;
8c9f3aaf 10629 }
7bd688cd
JN
10630
10631 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
10632}
10633
b690e96c
JB
10634/*
10635 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
10636 * resume, or other times. This quirk makes sure that's the case for
10637 * affected systems.
10638 */
0206e353 10639static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
10640{
10641 struct drm_i915_private *dev_priv = dev->dev_private;
10642
10643 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 10644 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
10645}
10646
435793df
KP
10647/*
10648 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
10649 */
10650static void quirk_ssc_force_disable(struct drm_device *dev)
10651{
10652 struct drm_i915_private *dev_priv = dev->dev_private;
10653 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 10654 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
10655}
10656
4dca20ef 10657/*
5a15ab5b
CE
10658 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
10659 * brightness value
4dca20ef
CE
10660 */
10661static void quirk_invert_brightness(struct drm_device *dev)
10662{
10663 struct drm_i915_private *dev_priv = dev->dev_private;
10664 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 10665 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
10666}
10667
b690e96c
JB
10668struct intel_quirk {
10669 int device;
10670 int subsystem_vendor;
10671 int subsystem_device;
10672 void (*hook)(struct drm_device *dev);
10673};
10674
5f85f176
EE
10675/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
10676struct intel_dmi_quirk {
10677 void (*hook)(struct drm_device *dev);
10678 const struct dmi_system_id (*dmi_id_list)[];
10679};
10680
10681static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
10682{
10683 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
10684 return 1;
10685}
10686
10687static const struct intel_dmi_quirk intel_dmi_quirks[] = {
10688 {
10689 .dmi_id_list = &(const struct dmi_system_id[]) {
10690 {
10691 .callback = intel_dmi_reverse_brightness,
10692 .ident = "NCR Corporation",
10693 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
10694 DMI_MATCH(DMI_PRODUCT_NAME, ""),
10695 },
10696 },
10697 { } /* terminating entry */
10698 },
10699 .hook = quirk_invert_brightness,
10700 },
10701};
10702
c43b5634 10703static struct intel_quirk intel_quirks[] = {
b690e96c 10704 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 10705 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 10706
b690e96c
JB
10707 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
10708 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
10709
b690e96c
JB
10710 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
10711 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
10712
a4945f95 10713 /* 830 needs to leave pipe A & dpll A up */
dcdaed6e 10714 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
435793df
KP
10715
10716 /* Lenovo U160 cannot use SSC on LVDS */
10717 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
10718
10719 /* Sony Vaio Y cannot use SSC on LVDS */
10720 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 10721
ee1452d7
JN
10722 /*
10723 * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
10724 * seem to use inverted backlight PWM.
10725 */
10726 { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
b690e96c
JB
10727};
10728
10729static void intel_init_quirks(struct drm_device *dev)
10730{
10731 struct pci_dev *d = dev->pdev;
10732 int i;
10733
10734 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10735 struct intel_quirk *q = &intel_quirks[i];
10736
10737 if (d->device == q->device &&
10738 (d->subsystem_vendor == q->subsystem_vendor ||
10739 q->subsystem_vendor == PCI_ANY_ID) &&
10740 (d->subsystem_device == q->subsystem_device ||
10741 q->subsystem_device == PCI_ANY_ID))
10742 q->hook(dev);
10743 }
5f85f176
EE
10744 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
10745 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
10746 intel_dmi_quirks[i].hook(dev);
10747 }
b690e96c
JB
10748}
10749
9cce37f4
JB
10750/* Disable the VGA plane that we never use */
10751static void i915_disable_vga(struct drm_device *dev)
10752{
10753 struct drm_i915_private *dev_priv = dev->dev_private;
10754 u8 sr1;
766aa1c4 10755 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4
JB
10756
10757 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 10758 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
10759 sr1 = inb(VGA_SR_DATA);
10760 outb(sr1 | 1<<5, VGA_SR_DATA);
10761 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10762 udelay(300);
10763
10764 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10765 POSTING_READ(vga_reg);
10766}
10767
f817586c
DV
10768void intel_modeset_init_hw(struct drm_device *dev)
10769{
f6071166
JB
10770 struct drm_i915_private *dev_priv = dev->dev_private;
10771
a8f78b58
ED
10772 intel_prepare_ddi(dev);
10773
f817586c
DV
10774 intel_init_clock_gating(dev);
10775
f6071166
JB
10776 /* Enable the CRI clock source so we can get at the display */
10777 if (IS_VALLEYVIEW(dev))
10778 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
10779 DPLL_INTEGRATED_CRI_CLK_VLV);
10780
40e9cf64
JB
10781 intel_init_dpio(dev);
10782
79f5b2c7 10783 mutex_lock(&dev->struct_mutex);
8090c6b9 10784 intel_enable_gt_powersave(dev);
79f5b2c7 10785 mutex_unlock(&dev->struct_mutex);
f817586c
DV
10786}
10787
7d708ee4
ID
10788void intel_modeset_suspend_hw(struct drm_device *dev)
10789{
10790 intel_suspend_hw(dev);
10791}
10792
79e53945
JB
10793void intel_modeset_init(struct drm_device *dev)
10794{
652c393a 10795 struct drm_i915_private *dev_priv = dev->dev_private;
7f1f3851 10796 int i, j, ret;
79e53945
JB
10797
10798 drm_mode_config_init(dev);
10799
10800 dev->mode_config.min_width = 0;
10801 dev->mode_config.min_height = 0;
10802
019d96cb
DA
10803 dev->mode_config.preferred_depth = 24;
10804 dev->mode_config.prefer_shadow = 1;
10805
e6ecefaa 10806 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 10807
b690e96c
JB
10808 intel_init_quirks(dev);
10809
1fa61106
ED
10810 intel_init_pm(dev);
10811
e3c74757
BW
10812 if (INTEL_INFO(dev)->num_pipes == 0)
10813 return;
10814
e70236a8
JB
10815 intel_init_display(dev);
10816
a6c45cf0
CW
10817 if (IS_GEN2(dev)) {
10818 dev->mode_config.max_width = 2048;
10819 dev->mode_config.max_height = 2048;
10820 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
10821 dev->mode_config.max_width = 4096;
10822 dev->mode_config.max_height = 4096;
79e53945 10823 } else {
a6c45cf0
CW
10824 dev->mode_config.max_width = 8192;
10825 dev->mode_config.max_height = 8192;
79e53945 10826 }
5d4545ae 10827 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 10828
28c97730 10829 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
10830 INTEL_INFO(dev)->num_pipes,
10831 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 10832
08e2a7de 10833 for_each_pipe(i) {
79e53945 10834 intel_crtc_init(dev, i);
7f1f3851
JB
10835 for (j = 0; j < dev_priv->num_plane; j++) {
10836 ret = intel_plane_init(dev, i, j);
10837 if (ret)
06da8da2
VS
10838 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10839 pipe_name(i), sprite_name(i, j), ret);
7f1f3851 10840 }
79e53945
JB
10841 }
10842
79f689aa 10843 intel_cpu_pll_init(dev);
e72f9fbf 10844 intel_shared_dpll_init(dev);
ee7b9f93 10845
9cce37f4
JB
10846 /* Just disable it once at startup */
10847 i915_disable_vga(dev);
79e53945 10848 intel_setup_outputs(dev);
11be49eb
CW
10849
10850 /* Just in case the BIOS is doing something questionable. */
10851 intel_disable_fbc(dev);
2c7111db
CW
10852}
10853
24929352
DV
10854static void
10855intel_connector_break_all_links(struct intel_connector *connector)
10856{
10857 connector->base.dpms = DRM_MODE_DPMS_OFF;
10858 connector->base.encoder = NULL;
10859 connector->encoder->connectors_active = false;
10860 connector->encoder->base.crtc = NULL;
10861}
10862
7fad798e
DV
10863static void intel_enable_pipe_a(struct drm_device *dev)
10864{
10865 struct intel_connector *connector;
10866 struct drm_connector *crt = NULL;
10867 struct intel_load_detect_pipe load_detect_temp;
10868
10869 /* We can't just switch on the pipe A, we need to set things up with a
10870 * proper mode and output configuration. As a gross hack, enable pipe A
10871 * by enabling the load detect pipe once. */
10872 list_for_each_entry(connector,
10873 &dev->mode_config.connector_list,
10874 base.head) {
10875 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10876 crt = &connector->base;
10877 break;
10878 }
10879 }
10880
10881 if (!crt)
10882 return;
10883
10884 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10885 intel_release_load_detect_pipe(crt, &load_detect_temp);
10886
652c393a 10887
7fad798e
DV
10888}
10889
fa555837
DV
10890static bool
10891intel_check_plane_mapping(struct intel_crtc *crtc)
10892{
7eb552ae
BW
10893 struct drm_device *dev = crtc->base.dev;
10894 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
10895 u32 reg, val;
10896
7eb552ae 10897 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
10898 return true;
10899
10900 reg = DSPCNTR(!crtc->plane);
10901 val = I915_READ(reg);
10902
10903 if ((val & DISPLAY_PLANE_ENABLE) &&
10904 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10905 return false;
10906
10907 return true;
10908}
10909
24929352
DV
10910static void intel_sanitize_crtc(struct intel_crtc *crtc)
10911{
10912 struct drm_device *dev = crtc->base.dev;
10913 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 10914 u32 reg;
24929352 10915
24929352 10916 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 10917 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
10918 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10919
10920 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
10921 * disable the crtc (and hence change the state) if it is wrong. Note
10922 * that gen4+ has a fixed plane -> pipe mapping. */
10923 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
10924 struct intel_connector *connector;
10925 bool plane;
10926
24929352
DV
10927 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10928 crtc->base.base.id);
10929
10930 /* Pipe has the wrong plane attached and the plane is active.
10931 * Temporarily change the plane mapping and disable everything
10932 * ... */
10933 plane = crtc->plane;
10934 crtc->plane = !plane;
10935 dev_priv->display.crtc_disable(&crtc->base);
10936 crtc->plane = plane;
10937
10938 /* ... and break all links. */
10939 list_for_each_entry(connector, &dev->mode_config.connector_list,
10940 base.head) {
10941 if (connector->encoder->base.crtc != &crtc->base)
10942 continue;
10943
10944 intel_connector_break_all_links(connector);
10945 }
10946
10947 WARN_ON(crtc->active);
10948 crtc->base.enabled = false;
10949 }
24929352 10950
7fad798e
DV
10951 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10952 crtc->pipe == PIPE_A && !crtc->active) {
10953 /* BIOS forgot to enable pipe A, this mostly happens after
10954 * resume. Force-enable the pipe to fix this, the update_dpms
10955 * call below we restore the pipe to the right state, but leave
10956 * the required bits on. */
10957 intel_enable_pipe_a(dev);
10958 }
10959
24929352
DV
10960 /* Adjust the state of the output pipe according to whether we
10961 * have active connectors/encoders. */
10962 intel_crtc_update_dpms(&crtc->base);
10963
10964 if (crtc->active != crtc->base.enabled) {
10965 struct intel_encoder *encoder;
10966
10967 /* This can happen either due to bugs in the get_hw_state
10968 * functions or because the pipe is force-enabled due to the
10969 * pipe A quirk. */
10970 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10971 crtc->base.base.id,
10972 crtc->base.enabled ? "enabled" : "disabled",
10973 crtc->active ? "enabled" : "disabled");
10974
10975 crtc->base.enabled = crtc->active;
10976
10977 /* Because we only establish the connector -> encoder ->
10978 * crtc links if something is active, this means the
10979 * crtc is now deactivated. Break the links. connector
10980 * -> encoder links are only establish when things are
10981 * actually up, hence no need to break them. */
10982 WARN_ON(crtc->active);
10983
10984 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10985 WARN_ON(encoder->connectors_active);
10986 encoder->base.crtc = NULL;
10987 }
10988 }
10989}
10990
10991static void intel_sanitize_encoder(struct intel_encoder *encoder)
10992{
10993 struct intel_connector *connector;
10994 struct drm_device *dev = encoder->base.dev;
10995
10996 /* We need to check both for a crtc link (meaning that the
10997 * encoder is active and trying to read from a pipe) and the
10998 * pipe itself being active. */
10999 bool has_active_crtc = encoder->base.crtc &&
11000 to_intel_crtc(encoder->base.crtc)->active;
11001
11002 if (encoder->connectors_active && !has_active_crtc) {
11003 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11004 encoder->base.base.id,
11005 drm_get_encoder_name(&encoder->base));
11006
11007 /* Connector is active, but has no active pipe. This is
11008 * fallout from our resume register restoring. Disable
11009 * the encoder manually again. */
11010 if (encoder->base.crtc) {
11011 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11012 encoder->base.base.id,
11013 drm_get_encoder_name(&encoder->base));
11014 encoder->disable(encoder);
11015 }
11016
11017 /* Inconsistent output/port/pipe state happens presumably due to
11018 * a bug in one of the get_hw_state functions. Or someplace else
11019 * in our code, like the register restore mess on resume. Clamp
11020 * things to off as a safer default. */
11021 list_for_each_entry(connector,
11022 &dev->mode_config.connector_list,
11023 base.head) {
11024 if (connector->encoder != encoder)
11025 continue;
11026
11027 intel_connector_break_all_links(connector);
11028 }
11029 }
11030 /* Enabled encoders without active connectors will be fixed in
11031 * the crtc fixup. */
11032}
11033
44cec740 11034void i915_redisable_vga(struct drm_device *dev)
0fde901f
KM
11035{
11036 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 11037 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 11038
8dc8a27c
PZ
11039 /* This function can be called both from intel_modeset_setup_hw_state or
11040 * at a very early point in our resume sequence, where the power well
11041 * structures are not yet restored. Since this function is at a very
11042 * paranoid "someone might have enabled VGA while we were not looking"
11043 * level, just check if the power well is enabled instead of trying to
11044 * follow the "don't touch the power well if we don't need it" policy
11045 * the rest of the driver uses. */
f9e711e9 11046 if ((IS_HASWELL(dev) || IS_BROADWELL(dev)) &&
6aedd1f5 11047 (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
8dc8a27c
PZ
11048 return;
11049
e1553faa 11050 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
0fde901f 11051 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
209d5211 11052 i915_disable_vga(dev);
0fde901f
KM
11053 }
11054}
11055
30e984df 11056static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
11057{
11058 struct drm_i915_private *dev_priv = dev->dev_private;
11059 enum pipe pipe;
24929352
DV
11060 struct intel_crtc *crtc;
11061 struct intel_encoder *encoder;
11062 struct intel_connector *connector;
5358901f 11063 int i;
24929352 11064
0e8ffe1b
DV
11065 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11066 base.head) {
88adfff1 11067 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 11068
0e8ffe1b
DV
11069 crtc->active = dev_priv->display.get_pipe_config(crtc,
11070 &crtc->config);
24929352
DV
11071
11072 crtc->base.enabled = crtc->active;
4c445e0e 11073 crtc->primary_enabled = crtc->active;
24929352
DV
11074
11075 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11076 crtc->base.base.id,
11077 crtc->active ? "enabled" : "disabled");
11078 }
11079
5358901f 11080 /* FIXME: Smash this into the new shared dpll infrastructure. */
affa9354 11081 if (HAS_DDI(dev))
6441ab5f
PZ
11082 intel_ddi_setup_hw_pll_state(dev);
11083
5358901f
DV
11084 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11085 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11086
11087 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11088 pll->active = 0;
11089 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11090 base.head) {
11091 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11092 pll->active++;
11093 }
11094 pll->refcount = pll->active;
11095
35c95375
DV
11096 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11097 pll->name, pll->refcount, pll->on);
5358901f
DV
11098 }
11099
24929352
DV
11100 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11101 base.head) {
11102 pipe = 0;
11103
11104 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
11105 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11106 encoder->base.crtc = &crtc->base;
510d5f2f 11107 if (encoder->get_config)
045ac3b5 11108 encoder->get_config(encoder, &crtc->config);
24929352
DV
11109 } else {
11110 encoder->base.crtc = NULL;
11111 }
11112
11113 encoder->connectors_active = false;
6f2bcceb 11114 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352
DV
11115 encoder->base.base.id,
11116 drm_get_encoder_name(&encoder->base),
11117 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 11118 pipe_name(pipe));
24929352
DV
11119 }
11120
11121 list_for_each_entry(connector, &dev->mode_config.connector_list,
11122 base.head) {
11123 if (connector->get_hw_state(connector)) {
11124 connector->base.dpms = DRM_MODE_DPMS_ON;
11125 connector->encoder->connectors_active = true;
11126 connector->base.encoder = &connector->encoder->base;
11127 } else {
11128 connector->base.dpms = DRM_MODE_DPMS_OFF;
11129 connector->base.encoder = NULL;
11130 }
11131 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11132 connector->base.base.id,
11133 drm_get_connector_name(&connector->base),
11134 connector->base.encoder ? "enabled" : "disabled");
11135 }
30e984df
DV
11136}
11137
11138/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11139 * and i915 state tracking structures. */
11140void intel_modeset_setup_hw_state(struct drm_device *dev,
11141 bool force_restore)
11142{
11143 struct drm_i915_private *dev_priv = dev->dev_private;
11144 enum pipe pipe;
30e984df
DV
11145 struct intel_crtc *crtc;
11146 struct intel_encoder *encoder;
35c95375 11147 int i;
30e984df
DV
11148
11149 intel_modeset_readout_hw_state(dev);
24929352 11150
babea61d
JB
11151 /*
11152 * Now that we have the config, copy it to each CRTC struct
11153 * Note that this could go away if we move to using crtc_config
11154 * checking everywhere.
11155 */
11156 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11157 base.head) {
11158 if (crtc->active && i915_fastboot) {
11159 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
11160
11161 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11162 crtc->base.base.id);
11163 drm_mode_debug_printmodeline(&crtc->base.mode);
11164 }
11165 }
11166
24929352
DV
11167 /* HW state is read out, now we need to sanitize this mess. */
11168 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11169 base.head) {
11170 intel_sanitize_encoder(encoder);
11171 }
11172
11173 for_each_pipe(pipe) {
11174 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11175 intel_sanitize_crtc(crtc);
c0b03411 11176 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 11177 }
9a935856 11178
35c95375
DV
11179 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11180 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11181
11182 if (!pll->on || pll->active)
11183 continue;
11184
11185 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11186
11187 pll->disable(dev_priv, pll);
11188 pll->on = false;
11189 }
11190
243e6a44
VS
11191 if (IS_HASWELL(dev))
11192 ilk_wm_get_hw_state(dev);
11193
45e2b5f6 11194 if (force_restore) {
7d0bc1ea
VS
11195 i915_redisable_vga(dev);
11196
f30da187
DV
11197 /*
11198 * We need to use raw interfaces for restoring state to avoid
11199 * checking (bogus) intermediate states.
11200 */
45e2b5f6 11201 for_each_pipe(pipe) {
b5644d05
JB
11202 struct drm_crtc *crtc =
11203 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
11204
11205 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11206 crtc->fb);
45e2b5f6
DV
11207 }
11208 } else {
11209 intel_modeset_update_staged_output_state(dev);
11210 }
8af6cf88
DV
11211
11212 intel_modeset_check_state(dev);
2e938892
DV
11213
11214 drm_mode_config_reset(dev);
2c7111db
CW
11215}
11216
11217void intel_modeset_gem_init(struct drm_device *dev)
11218{
1833b134 11219 intel_modeset_init_hw(dev);
02e792fb
DV
11220
11221 intel_setup_overlay(dev);
24929352 11222
45e2b5f6 11223 intel_modeset_setup_hw_state(dev, false);
79e53945
JB
11224}
11225
11226void intel_modeset_cleanup(struct drm_device *dev)
11227{
652c393a
JB
11228 struct drm_i915_private *dev_priv = dev->dev_private;
11229 struct drm_crtc *crtc;
d9255d57 11230 struct drm_connector *connector;
652c393a 11231
fd0c0642
DV
11232 /*
11233 * Interrupts and polling as the first thing to avoid creating havoc.
11234 * Too much stuff here (turning of rps, connectors, ...) would
11235 * experience fancy races otherwise.
11236 */
11237 drm_irq_uninstall(dev);
11238 cancel_work_sync(&dev_priv->hotplug_work);
11239 /*
11240 * Due to the hpd irq storm handling the hotplug work can re-arm the
11241 * poll handlers. Hence disable polling after hpd handling is shut down.
11242 */
f87ea761 11243 drm_kms_helper_poll_fini(dev);
fd0c0642 11244
652c393a
JB
11245 mutex_lock(&dev->struct_mutex);
11246
723bfd70
JB
11247 intel_unregister_dsm_handler();
11248
652c393a
JB
11249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11250 /* Skip inactive CRTCs */
11251 if (!crtc->fb)
11252 continue;
11253
3dec0095 11254 intel_increase_pllclock(crtc);
652c393a
JB
11255 }
11256
973d04f9 11257 intel_disable_fbc(dev);
e70236a8 11258
8090c6b9 11259 intel_disable_gt_powersave(dev);
0cdab21f 11260
930ebb46
DV
11261 ironlake_teardown_rc6(dev);
11262
69341a5e
KH
11263 mutex_unlock(&dev->struct_mutex);
11264
1630fe75
CW
11265 /* flush any delayed tasks or pending work */
11266 flush_scheduled_work();
11267
db31af1d
JN
11268 /* destroy the backlight and sysfs files before encoders/connectors */
11269 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11270 intel_panel_destroy_backlight(connector);
d9255d57 11271 drm_sysfs_connector_remove(connector);
db31af1d 11272 }
d9255d57 11273
79e53945 11274 drm_mode_config_cleanup(dev);
4d7bb011
DV
11275
11276 intel_cleanup_overlay(dev);
79e53945
JB
11277}
11278
f1c79df3
ZW
11279/*
11280 * Return which encoder is currently attached for connector.
11281 */
df0e9248 11282struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 11283{
df0e9248
CW
11284 return &intel_attached_encoder(connector)->base;
11285}
f1c79df3 11286
df0e9248
CW
11287void intel_connector_attach_encoder(struct intel_connector *connector,
11288 struct intel_encoder *encoder)
11289{
11290 connector->encoder = encoder;
11291 drm_mode_connector_attach_encoder(&connector->base,
11292 &encoder->base);
79e53945 11293}
28d52043
DA
11294
11295/*
11296 * set vga decode state - true == enable VGA decode
11297 */
11298int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11299{
11300 struct drm_i915_private *dev_priv = dev->dev_private;
11301 u16 gmch_ctrl;
11302
11303 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
11304 if (state)
11305 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11306 else
11307 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
11308 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
11309 return 0;
11310}
c4a1d9e4 11311
c4a1d9e4 11312struct intel_display_error_state {
ff57f1b0
PZ
11313
11314 u32 power_well_driver;
11315
63b66e5b
CW
11316 int num_transcoders;
11317
c4a1d9e4
CW
11318 struct intel_cursor_error_state {
11319 u32 control;
11320 u32 position;
11321 u32 base;
11322 u32 size;
52331309 11323 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
11324
11325 struct intel_pipe_error_state {
ddf9c536 11326 bool power_domain_on;
c4a1d9e4 11327 u32 source;
52331309 11328 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
11329
11330 struct intel_plane_error_state {
11331 u32 control;
11332 u32 stride;
11333 u32 size;
11334 u32 pos;
11335 u32 addr;
11336 u32 surface;
11337 u32 tile_offset;
52331309 11338 } plane[I915_MAX_PIPES];
63b66e5b
CW
11339
11340 struct intel_transcoder_error_state {
ddf9c536 11341 bool power_domain_on;
63b66e5b
CW
11342 enum transcoder cpu_transcoder;
11343
11344 u32 conf;
11345
11346 u32 htotal;
11347 u32 hblank;
11348 u32 hsync;
11349 u32 vtotal;
11350 u32 vblank;
11351 u32 vsync;
11352 } transcoder[4];
c4a1d9e4
CW
11353};
11354
11355struct intel_display_error_state *
11356intel_display_capture_error_state(struct drm_device *dev)
11357{
0206e353 11358 drm_i915_private_t *dev_priv = dev->dev_private;
c4a1d9e4 11359 struct intel_display_error_state *error;
63b66e5b
CW
11360 int transcoders[] = {
11361 TRANSCODER_A,
11362 TRANSCODER_B,
11363 TRANSCODER_C,
11364 TRANSCODER_EDP,
11365 };
c4a1d9e4
CW
11366 int i;
11367
63b66e5b
CW
11368 if (INTEL_INFO(dev)->num_pipes == 0)
11369 return NULL;
11370
9d1cb914 11371 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
11372 if (error == NULL)
11373 return NULL;
11374
190be112 11375 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
11376 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11377
52331309 11378 for_each_pipe(i) {
ddf9c536
ID
11379 error->pipe[i].power_domain_on =
11380 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11381 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
11382 continue;
11383
a18c4c3d
PZ
11384 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11385 error->cursor[i].control = I915_READ(CURCNTR(i));
11386 error->cursor[i].position = I915_READ(CURPOS(i));
11387 error->cursor[i].base = I915_READ(CURBASE(i));
11388 } else {
11389 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11390 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11391 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11392 }
c4a1d9e4
CW
11393
11394 error->plane[i].control = I915_READ(DSPCNTR(i));
11395 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 11396 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 11397 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
11398 error->plane[i].pos = I915_READ(DSPPOS(i));
11399 }
ca291363
PZ
11400 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11401 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
11402 if (INTEL_INFO(dev)->gen >= 4) {
11403 error->plane[i].surface = I915_READ(DSPSURF(i));
11404 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11405 }
11406
c4a1d9e4 11407 error->pipe[i].source = I915_READ(PIPESRC(i));
63b66e5b
CW
11408 }
11409
11410 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11411 if (HAS_DDI(dev_priv->dev))
11412 error->num_transcoders++; /* Account for eDP. */
11413
11414 for (i = 0; i < error->num_transcoders; i++) {
11415 enum transcoder cpu_transcoder = transcoders[i];
11416
ddf9c536
ID
11417 error->transcoder[i].power_domain_on =
11418 intel_display_power_enabled_sw(dev, POWER_DOMAIN_PIPE(i));
11419 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
11420 continue;
11421
63b66e5b
CW
11422 error->transcoder[i].cpu_transcoder = cpu_transcoder;
11423
11424 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
11425 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
11426 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
11427 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
11428 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
11429 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
11430 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
11431 }
11432
11433 return error;
11434}
11435
edc3d884
MK
11436#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
11437
c4a1d9e4 11438void
edc3d884 11439intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
11440 struct drm_device *dev,
11441 struct intel_display_error_state *error)
11442{
11443 int i;
11444
63b66e5b
CW
11445 if (!error)
11446 return;
11447
edc3d884 11448 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 11449 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 11450 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 11451 error->power_well_driver);
52331309 11452 for_each_pipe(i) {
edc3d884 11453 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
11454 err_printf(m, " Power: %s\n",
11455 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 11456 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
edc3d884
MK
11457
11458 err_printf(m, "Plane [%d]:\n", i);
11459 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
11460 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 11461 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
11462 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
11463 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 11464 }
4b71a570 11465 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 11466 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 11467 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
11468 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
11469 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
11470 }
11471
edc3d884
MK
11472 err_printf(m, "Cursor [%d]:\n", i);
11473 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
11474 err_printf(m, " POS: %08x\n", error->cursor[i].position);
11475 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 11476 }
63b66e5b
CW
11477
11478 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 11479 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 11480 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
11481 err_printf(m, " Power: %s\n",
11482 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
11483 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
11484 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
11485 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
11486 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
11487 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
11488 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
11489 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
11490 }
c4a1d9e4 11491}