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