]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: enable PPGTT on VLV
[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>
465c120c
MR
42#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
c0f372b3 44#include <linux/dma_remapping.h>
79e53945 45
465c120c
MR
46/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
ef9348c8 71#define DIV_ROUND_CLOSEST_ULL(ll, d) \
465c120c 72({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
ef9348c8 73
3dec0095 74static void intel_increase_pllclock(struct drm_crtc *crtc);
6b383a7f 75static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 76
f1f644dc
JB
77static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
78 struct intel_crtc_config *pipe_config);
18442d08
VS
79static void ironlake_pch_clock_get(struct intel_crtc *crtc,
80 struct intel_crtc_config *pipe_config);
f1f644dc 81
e7457a9a
DL
82static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
83 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
84static int intel_framebuffer_init(struct drm_device *dev,
85 struct intel_framebuffer *ifb,
86 struct drm_mode_fb_cmd2 *mode_cmd,
87 struct drm_i915_gem_object *obj);
5b18e57c
DV
88static void intel_dp_set_m_n(struct intel_crtc *crtc);
89static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
90static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab
DV
91static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
92 struct intel_link_m_n *m_n);
93static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
94static void haswell_set_pipeconf(struct drm_crtc *crtc);
95static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 96static void vlv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 97
79e53945 98typedef struct {
0206e353 99 int min, max;
79e53945
JB
100} intel_range_t;
101
102typedef struct {
0206e353
AJ
103 int dot_limit;
104 int p2_slow, p2_fast;
79e53945
JB
105} intel_p2_t;
106
d4906093
ML
107typedef struct intel_limit intel_limit_t;
108struct intel_limit {
0206e353
AJ
109 intel_range_t dot, vco, n, m, m1, m2, p, p1;
110 intel_p2_t p2;
d4906093 111};
79e53945 112
d2acd215
DV
113int
114intel_pch_rawclk(struct drm_device *dev)
115{
116 struct drm_i915_private *dev_priv = dev->dev_private;
117
118 WARN_ON(!HAS_PCH_SPLIT(dev));
119
120 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
121}
122
021357ac
CW
123static inline u32 /* units of 100MHz */
124intel_fdi_link_freq(struct drm_device *dev)
125{
8b99e68c
CW
126 if (IS_GEN5(dev)) {
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
129 } else
130 return 27;
021357ac
CW
131}
132
5d536e28 133static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 134 .dot = { .min = 25000, .max = 350000 },
9c333719 135 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 136 .n = { .min = 2, .max = 16 },
0206e353
AJ
137 .m = { .min = 96, .max = 140 },
138 .m1 = { .min = 18, .max = 26 },
139 .m2 = { .min = 6, .max = 16 },
140 .p = { .min = 4, .max = 128 },
141 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
142 .p2 = { .dot_limit = 165000,
143 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
144};
145
5d536e28
DV
146static const intel_limit_t intel_limits_i8xx_dvo = {
147 .dot = { .min = 25000, .max = 350000 },
9c333719 148 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 149 .n = { .min = 2, .max = 16 },
5d536e28
DV
150 .m = { .min = 96, .max = 140 },
151 .m1 = { .min = 18, .max = 26 },
152 .m2 = { .min = 6, .max = 16 },
153 .p = { .min = 4, .max = 128 },
154 .p1 = { .min = 2, .max = 33 },
155 .p2 = { .dot_limit = 165000,
156 .p2_slow = 4, .p2_fast = 4 },
157};
158
e4b36699 159static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 160 .dot = { .min = 25000, .max = 350000 },
9c333719 161 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 162 .n = { .min = 2, .max = 16 },
0206e353
AJ
163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 14, .p2_fast = 7 },
e4b36699 170};
273e27ca 171
e4b36699 172static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
173 .dot = { .min = 20000, .max = 400000 },
174 .vco = { .min = 1400000, .max = 2800000 },
175 .n = { .min = 1, .max = 6 },
176 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
177 .m1 = { .min = 8, .max = 18 },
178 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
181 .p2 = { .dot_limit = 200000,
182 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
183};
184
185static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
186 .dot = { .min = 20000, .max = 400000 },
187 .vco = { .min = 1400000, .max = 2800000 },
188 .n = { .min = 1, .max = 6 },
189 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
190 .m1 = { .min = 8, .max = 18 },
191 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
192 .p = { .min = 7, .max = 98 },
193 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
194 .p2 = { .dot_limit = 112000,
195 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
196};
197
273e27ca 198
e4b36699 199static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
200 .dot = { .min = 25000, .max = 270000 },
201 .vco = { .min = 1750000, .max = 3500000},
202 .n = { .min = 1, .max = 4 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 10, .max = 30 },
207 .p1 = { .min = 1, .max = 3},
208 .p2 = { .dot_limit = 270000,
209 .p2_slow = 10,
210 .p2_fast = 10
044c7c41 211 },
e4b36699
KP
212};
213
214static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
215 .dot = { .min = 22000, .max = 400000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 16, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 5, .max = 80 },
222 .p1 = { .min = 1, .max = 8},
223 .p2 = { .dot_limit = 165000,
224 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
225};
226
227static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
228 .dot = { .min = 20000, .max = 115000 },
229 .vco = { .min = 1750000, .max = 3500000 },
230 .n = { .min = 1, .max = 3 },
231 .m = { .min = 104, .max = 138 },
232 .m1 = { .min = 17, .max = 23 },
233 .m2 = { .min = 5, .max = 11 },
234 .p = { .min = 28, .max = 112 },
235 .p1 = { .min = 2, .max = 8 },
236 .p2 = { .dot_limit = 0,
237 .p2_slow = 14, .p2_fast = 14
044c7c41 238 },
e4b36699
KP
239};
240
241static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
242 .dot = { .min = 80000, .max = 224000 },
243 .vco = { .min = 1750000, .max = 3500000 },
244 .n = { .min = 1, .max = 3 },
245 .m = { .min = 104, .max = 138 },
246 .m1 = { .min = 17, .max = 23 },
247 .m2 = { .min = 5, .max = 11 },
248 .p = { .min = 14, .max = 42 },
249 .p1 = { .min = 2, .max = 6 },
250 .p2 = { .dot_limit = 0,
251 .p2_slow = 7, .p2_fast = 7
044c7c41 252 },
e4b36699
KP
253};
254
f2b115e6 255static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 258 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
273e27ca 261 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
268};
269
f2b115e6 270static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
271 .dot = { .min = 20000, .max = 400000 },
272 .vco = { .min = 1700000, .max = 3500000 },
273 .n = { .min = 3, .max = 6 },
274 .m = { .min = 2, .max = 256 },
275 .m1 = { .min = 0, .max = 0 },
276 .m2 = { .min = 0, .max = 254 },
277 .p = { .min = 7, .max = 112 },
278 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
279 .p2 = { .dot_limit = 112000,
280 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
281};
282
273e27ca
EA
283/* Ironlake / Sandybridge
284 *
285 * We calculate clock using (register_value + 2) for N/M1/M2, so here
286 * the range value for them is (actual_value - 2).
287 */
b91ad0ec 288static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
289 .dot = { .min = 25000, .max = 350000 },
290 .vco = { .min = 1760000, .max = 3510000 },
291 .n = { .min = 1, .max = 5 },
292 .m = { .min = 79, .max = 127 },
293 .m1 = { .min = 12, .max = 22 },
294 .m2 = { .min = 5, .max = 9 },
295 .p = { .min = 5, .max = 80 },
296 .p1 = { .min = 1, .max = 8 },
297 .p2 = { .dot_limit = 225000,
298 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
299};
300
b91ad0ec 301static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 3 },
305 .m = { .min = 79, .max = 118 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 28, .max = 112 },
309 .p1 = { .min = 2, .max = 8 },
310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
312};
313
314static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 3 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 14, .max = 56 },
322 .p1 = { .min = 2, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
325};
326
273e27ca 327/* LVDS 100mhz refclk limits. */
b91ad0ec 328static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
329 .dot = { .min = 25000, .max = 350000 },
330 .vco = { .min = 1760000, .max = 3510000 },
331 .n = { .min = 1, .max = 2 },
332 .m = { .min = 79, .max = 126 },
333 .m1 = { .min = 12, .max = 22 },
334 .m2 = { .min = 5, .max = 9 },
335 .p = { .min = 28, .max = 112 },
0206e353 336 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
337 .p2 = { .dot_limit = 225000,
338 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
339};
340
341static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
342 .dot = { .min = 25000, .max = 350000 },
343 .vco = { .min = 1760000, .max = 3510000 },
344 .n = { .min = 1, .max = 3 },
345 .m = { .min = 79, .max = 126 },
346 .m1 = { .min = 12, .max = 22 },
347 .m2 = { .min = 5, .max = 9 },
348 .p = { .min = 14, .max = 42 },
0206e353 349 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
350 .p2 = { .dot_limit = 225000,
351 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
352};
353
dc730512 354static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
355 /*
356 * These are the data rate limits (measured in fast clocks)
357 * since those are the strictest limits we have. The fast
358 * clock and actual rate limits are more relaxed, so checking
359 * them would make no difference.
360 */
361 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 362 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 363 .n = { .min = 1, .max = 7 },
a0c4da24
JB
364 .m1 = { .min = 2, .max = 3 },
365 .m2 = { .min = 11, .max = 156 },
b99ab663 366 .p1 = { .min = 2, .max = 3 },
5fdc9c49 367 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
368};
369
ef9348c8
CML
370static const intel_limit_t intel_limits_chv = {
371 /*
372 * These are the data rate limits (measured in fast clocks)
373 * since those are the strictest limits we have. The fast
374 * clock and actual rate limits are more relaxed, so checking
375 * them would make no difference.
376 */
377 .dot = { .min = 25000 * 5, .max = 540000 * 5},
378 .vco = { .min = 4860000, .max = 6700000 },
379 .n = { .min = 1, .max = 1 },
380 .m1 = { .min = 2, .max = 2 },
381 .m2 = { .min = 24 << 22, .max = 175 << 22 },
382 .p1 = { .min = 2, .max = 4 },
383 .p2 = { .p2_slow = 1, .p2_fast = 14 },
384};
385
6b4bf1c4
VS
386static void vlv_clock(int refclk, intel_clock_t *clock)
387{
388 clock->m = clock->m1 * clock->m2;
389 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
390 if (WARN_ON(clock->n == 0 || clock->p == 0))
391 return;
fb03ac01
VS
392 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
393 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
394}
395
e0638cdf
PZ
396/**
397 * Returns whether any output on the specified pipe is of the specified type
398 */
399static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
400{
401 struct drm_device *dev = crtc->dev;
402 struct intel_encoder *encoder;
403
404 for_each_encoder_on_crtc(dev, crtc, encoder)
405 if (encoder->type == type)
406 return true;
407
408 return false;
409}
410
1b894b59
CW
411static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
412 int refclk)
2c07245f 413{
b91ad0ec 414 struct drm_device *dev = crtc->dev;
2c07245f 415 const intel_limit_t *limit;
b91ad0ec
ZW
416
417 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 418 if (intel_is_dual_link_lvds(dev)) {
1b894b59 419 if (refclk == 100000)
b91ad0ec
ZW
420 limit = &intel_limits_ironlake_dual_lvds_100m;
421 else
422 limit = &intel_limits_ironlake_dual_lvds;
423 } else {
1b894b59 424 if (refclk == 100000)
b91ad0ec
ZW
425 limit = &intel_limits_ironlake_single_lvds_100m;
426 else
427 limit = &intel_limits_ironlake_single_lvds;
428 }
c6bb3538 429 } else
b91ad0ec 430 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
431
432 return limit;
433}
434
044c7c41
ML
435static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
436{
437 struct drm_device *dev = crtc->dev;
044c7c41
ML
438 const intel_limit_t *limit;
439
440 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 441 if (intel_is_dual_link_lvds(dev))
e4b36699 442 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 443 else
e4b36699 444 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
445 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
446 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 447 limit = &intel_limits_g4x_hdmi;
044c7c41 448 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 449 limit = &intel_limits_g4x_sdvo;
044c7c41 450 } else /* The option is for other outputs */
e4b36699 451 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
452
453 return limit;
454}
455
1b894b59 456static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
457{
458 struct drm_device *dev = crtc->dev;
459 const intel_limit_t *limit;
460
bad720ff 461 if (HAS_PCH_SPLIT(dev))
1b894b59 462 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 463 else if (IS_G4X(dev)) {
044c7c41 464 limit = intel_g4x_limit(crtc);
f2b115e6 465 } else if (IS_PINEVIEW(dev)) {
2177832f 466 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 467 limit = &intel_limits_pineview_lvds;
2177832f 468 else
f2b115e6 469 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
470 } else if (IS_CHERRYVIEW(dev)) {
471 limit = &intel_limits_chv;
a0c4da24 472 } else if (IS_VALLEYVIEW(dev)) {
dc730512 473 limit = &intel_limits_vlv;
a6c45cf0
CW
474 } else if (!IS_GEN2(dev)) {
475 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
476 limit = &intel_limits_i9xx_lvds;
477 else
478 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
479 } else {
480 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 481 limit = &intel_limits_i8xx_lvds;
5d536e28 482 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 483 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
484 else
485 limit = &intel_limits_i8xx_dac;
79e53945
JB
486 }
487 return limit;
488}
489
f2b115e6
AJ
490/* m1 is reserved as 0 in Pineview, n is a ring counter */
491static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 492{
2177832f
SL
493 clock->m = clock->m2 + 2;
494 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
495 if (WARN_ON(clock->n == 0 || clock->p == 0))
496 return;
fb03ac01
VS
497 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
498 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
499}
500
7429e9d4
DV
501static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
502{
503 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
504}
505
ac58c3f0 506static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 507{
7429e9d4 508 clock->m = i9xx_dpll_compute_m(clock);
79e53945 509 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
510 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
511 return;
fb03ac01
VS
512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
514}
515
ef9348c8
CML
516static void chv_clock(int refclk, intel_clock_t *clock)
517{
518 clock->m = clock->m1 * clock->m2;
519 clock->p = clock->p1 * clock->p2;
520 if (WARN_ON(clock->n == 0 || clock->p == 0))
521 return;
522 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
523 clock->n << 22);
524 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
525}
526
7c04d1d9 527#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
528/**
529 * Returns whether the given set of divisors are valid for a given refclk with
530 * the given connectors.
531 */
532
1b894b59
CW
533static bool intel_PLL_is_valid(struct drm_device *dev,
534 const intel_limit_t *limit,
535 const intel_clock_t *clock)
79e53945 536{
f01b7962
VS
537 if (clock->n < limit->n.min || limit->n.max < clock->n)
538 INTELPllInvalid("n out of range\n");
79e53945 539 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 540 INTELPllInvalid("p1 out of range\n");
79e53945 541 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 542 INTELPllInvalid("m2 out of range\n");
79e53945 543 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 544 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
545
546 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
547 if (clock->m1 <= clock->m2)
548 INTELPllInvalid("m1 <= m2\n");
549
550 if (!IS_VALLEYVIEW(dev)) {
551 if (clock->p < limit->p.min || limit->p.max < clock->p)
552 INTELPllInvalid("p out of range\n");
553 if (clock->m < limit->m.min || limit->m.max < clock->m)
554 INTELPllInvalid("m out of range\n");
555 }
556
79e53945 557 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 558 INTELPllInvalid("vco out of range\n");
79e53945
JB
559 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
560 * connector, etc., rather than just a single range.
561 */
562 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 563 INTELPllInvalid("dot out of range\n");
79e53945
JB
564
565 return true;
566}
567
d4906093 568static bool
ee9300bb 569i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
570 int target, int refclk, intel_clock_t *match_clock,
571 intel_clock_t *best_clock)
79e53945
JB
572{
573 struct drm_device *dev = crtc->dev;
79e53945 574 intel_clock_t clock;
79e53945
JB
575 int err = target;
576
a210b028 577 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 578 /*
a210b028
DV
579 * For LVDS just rely on its current settings for dual-channel.
580 * We haven't figured out how to reliably set up different
581 * single/dual channel state, if we even can.
79e53945 582 */
1974cad0 583 if (intel_is_dual_link_lvds(dev))
79e53945
JB
584 clock.p2 = limit->p2.p2_fast;
585 else
586 clock.p2 = limit->p2.p2_slow;
587 } else {
588 if (target < limit->p2.dot_limit)
589 clock.p2 = limit->p2.p2_slow;
590 else
591 clock.p2 = limit->p2.p2_fast;
592 }
593
0206e353 594 memset(best_clock, 0, sizeof(*best_clock));
79e53945 595
42158660
ZY
596 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
597 clock.m1++) {
598 for (clock.m2 = limit->m2.min;
599 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 600 if (clock.m2 >= clock.m1)
42158660
ZY
601 break;
602 for (clock.n = limit->n.min;
603 clock.n <= limit->n.max; clock.n++) {
604 for (clock.p1 = limit->p1.min;
605 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
606 int this_err;
607
ac58c3f0
DV
608 i9xx_clock(refclk, &clock);
609 if (!intel_PLL_is_valid(dev, limit,
610 &clock))
611 continue;
612 if (match_clock &&
613 clock.p != match_clock->p)
614 continue;
615
616 this_err = abs(clock.dot - target);
617 if (this_err < err) {
618 *best_clock = clock;
619 err = this_err;
620 }
621 }
622 }
623 }
624 }
625
626 return (err != target);
627}
628
629static bool
ee9300bb
DV
630pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
631 int target, int refclk, intel_clock_t *match_clock,
632 intel_clock_t *best_clock)
79e53945
JB
633{
634 struct drm_device *dev = crtc->dev;
79e53945 635 intel_clock_t clock;
79e53945
JB
636 int err = target;
637
a210b028 638 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 639 /*
a210b028
DV
640 * For LVDS just rely on its current settings for dual-channel.
641 * We haven't figured out how to reliably set up different
642 * single/dual channel state, if we even can.
79e53945 643 */
1974cad0 644 if (intel_is_dual_link_lvds(dev))
79e53945
JB
645 clock.p2 = limit->p2.p2_fast;
646 else
647 clock.p2 = limit->p2.p2_slow;
648 } else {
649 if (target < limit->p2.dot_limit)
650 clock.p2 = limit->p2.p2_slow;
651 else
652 clock.p2 = limit->p2.p2_fast;
653 }
654
0206e353 655 memset(best_clock, 0, sizeof(*best_clock));
79e53945 656
42158660
ZY
657 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
658 clock.m1++) {
659 for (clock.m2 = limit->m2.min;
660 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
665 int this_err;
666
ac58c3f0 667 pineview_clock(refclk, &clock);
1b894b59
CW
668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
79e53945 670 continue;
cec2f356
SP
671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
79e53945
JB
674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
d4906093 688static bool
ee9300bb
DV
689g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
d4906093
ML
692{
693 struct drm_device *dev = crtc->dev;
d4906093
ML
694 intel_clock_t clock;
695 int max_n;
696 bool found;
6ba770dc
AJ
697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
699 found = false;
700
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 702 if (intel_is_dual_link_lvds(dev))
d4906093
ML
703 clock.p2 = limit->p2.p2_fast;
704 else
705 clock.p2 = limit->p2.p2_slow;
706 } else {
707 if (target < limit->p2.dot_limit)
708 clock.p2 = limit->p2.p2_slow;
709 else
710 clock.p2 = limit->p2.p2_fast;
711 }
712
713 memset(best_clock, 0, sizeof(*best_clock));
714 max_n = limit->n.max;
f77f13e2 715 /* based on hardware requirement, prefer smaller n to precision */
d4906093 716 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 717 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
718 for (clock.m1 = limit->m1.max;
719 clock.m1 >= limit->m1.min; clock.m1--) {
720 for (clock.m2 = limit->m2.max;
721 clock.m2 >= limit->m2.min; clock.m2--) {
722 for (clock.p1 = limit->p1.max;
723 clock.p1 >= limit->p1.min; clock.p1--) {
724 int this_err;
725
ac58c3f0 726 i9xx_clock(refclk, &clock);
1b894b59
CW
727 if (!intel_PLL_is_valid(dev, limit,
728 &clock))
d4906093 729 continue;
1b894b59
CW
730
731 this_err = abs(clock.dot - target);
d4906093
ML
732 if (this_err < err_most) {
733 *best_clock = clock;
734 err_most = this_err;
735 max_n = clock.n;
736 found = true;
737 }
738 }
739 }
740 }
741 }
2c07245f
ZW
742 return found;
743}
744
a0c4da24 745static bool
ee9300bb
DV
746vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
747 int target, int refclk, intel_clock_t *match_clock,
748 intel_clock_t *best_clock)
a0c4da24 749{
f01b7962 750 struct drm_device *dev = crtc->dev;
6b4bf1c4 751 intel_clock_t clock;
69e4f900 752 unsigned int bestppm = 1000000;
27e639bf
VS
753 /* min update 19.2 MHz */
754 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 755 bool found = false;
a0c4da24 756
6b4bf1c4
VS
757 target *= 5; /* fast clock */
758
759 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
760
761 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 762 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 763 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 764 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 765 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 766 clock.p = clock.p1 * clock.p2;
a0c4da24 767 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 768 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
769 unsigned int ppm, diff;
770
6b4bf1c4
VS
771 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
772 refclk * clock.m1);
773
774 vlv_clock(refclk, &clock);
43b0ac53 775
f01b7962
VS
776 if (!intel_PLL_is_valid(dev, limit,
777 &clock))
43b0ac53
VS
778 continue;
779
6b4bf1c4
VS
780 diff = abs(clock.dot - target);
781 ppm = div_u64(1000000ULL * diff, target);
782
783 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 784 bestppm = 0;
6b4bf1c4 785 *best_clock = clock;
49e497ef 786 found = true;
43b0ac53 787 }
6b4bf1c4 788
c686122c 789 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 790 bestppm = ppm;
6b4bf1c4 791 *best_clock = clock;
49e497ef 792 found = true;
a0c4da24
JB
793 }
794 }
795 }
796 }
797 }
a0c4da24 798
49e497ef 799 return found;
a0c4da24 800}
a4fc5ed6 801
ef9348c8
CML
802static bool
803chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
804 int target, int refclk, intel_clock_t *match_clock,
805 intel_clock_t *best_clock)
806{
807 struct drm_device *dev = crtc->dev;
808 intel_clock_t clock;
809 uint64_t m2;
810 int found = false;
811
812 memset(best_clock, 0, sizeof(*best_clock));
813
814 /*
815 * Based on hardware doc, the n always set to 1, and m1 always
816 * set to 2. If requires to support 200Mhz refclk, we need to
817 * revisit this because n may not 1 anymore.
818 */
819 clock.n = 1, clock.m1 = 2;
820 target *= 5; /* fast clock */
821
822 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
823 for (clock.p2 = limit->p2.p2_fast;
824 clock.p2 >= limit->p2.p2_slow;
825 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
826
827 clock.p = clock.p1 * clock.p2;
828
829 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
830 clock.n) << 22, refclk * clock.m1);
831
832 if (m2 > INT_MAX/clock.m1)
833 continue;
834
835 clock.m2 = m2;
836
837 chv_clock(refclk, &clock);
838
839 if (!intel_PLL_is_valid(dev, limit, &clock))
840 continue;
841
842 /* based on hardware requirement, prefer bigger p
843 */
844 if (clock.p > best_clock->p) {
845 *best_clock = clock;
846 found = true;
847 }
848 }
849 }
850
851 return found;
852}
853
20ddf665
VS
854bool intel_crtc_active(struct drm_crtc *crtc)
855{
856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
857
858 /* Be paranoid as we can arrive here with only partial
859 * state retrieved from the hardware during setup.
860 *
241bfc38 861 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
862 * as Haswell has gained clock readout/fastboot support.
863 *
66e514c1 864 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
865 * properly reconstruct framebuffers.
866 */
f4510a27 867 return intel_crtc->active && crtc->primary->fb &&
241bfc38 868 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
869}
870
a5c961d1
PZ
871enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
872 enum pipe pipe)
873{
874 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
876
3b117c8f 877 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
878}
879
57e22f4a 880static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
881{
882 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 883 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
884
885 frame = I915_READ(frame_reg);
886
887 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
93937071 888 WARN(1, "vblank wait timed out\n");
a928d536
PZ
889}
890
9d0498a2
JB
891/**
892 * intel_wait_for_vblank - wait for vblank on a given pipe
893 * @dev: drm device
894 * @pipe: pipe to wait for
895 *
896 * Wait for vblank to occur on a given pipe. Needed for various bits of
897 * mode setting code.
898 */
899void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 900{
9d0498a2 901 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 902 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 903
57e22f4a
VS
904 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
905 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
906 return;
907 }
908
300387c0
CW
909 /* Clear existing vblank status. Note this will clear any other
910 * sticky status fields as well.
911 *
912 * This races with i915_driver_irq_handler() with the result
913 * that either function could miss a vblank event. Here it is not
914 * fatal, as we will either wait upon the next vblank interrupt or
915 * timeout. Generally speaking intel_wait_for_vblank() is only
916 * called during modeset at which time the GPU should be idle and
917 * should *not* be performing page flips and thus not waiting on
918 * vblanks...
919 * Currently, the result of us stealing a vblank from the irq
920 * handler is that a single frame will be skipped during swapbuffers.
921 */
922 I915_WRITE(pipestat_reg,
923 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
924
9d0498a2 925 /* Wait for vblank interrupt bit to set */
481b6af3
CW
926 if (wait_for(I915_READ(pipestat_reg) &
927 PIPE_VBLANK_INTERRUPT_STATUS,
928 50))
9d0498a2
JB
929 DRM_DEBUG_KMS("vblank wait timed out\n");
930}
931
fbf49ea2
VS
932static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
933{
934 struct drm_i915_private *dev_priv = dev->dev_private;
935 u32 reg = PIPEDSL(pipe);
936 u32 line1, line2;
937 u32 line_mask;
938
939 if (IS_GEN2(dev))
940 line_mask = DSL_LINEMASK_GEN2;
941 else
942 line_mask = DSL_LINEMASK_GEN3;
943
944 line1 = I915_READ(reg) & line_mask;
945 mdelay(5);
946 line2 = I915_READ(reg) & line_mask;
947
948 return line1 == line2;
949}
950
ab7ad7f6
KP
951/*
952 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
953 * @dev: drm device
954 * @pipe: pipe to wait for
955 *
956 * After disabling a pipe, we can't wait for vblank in the usual way,
957 * spinning on the vblank interrupt status bit, since we won't actually
958 * see an interrupt when the pipe is disabled.
959 *
ab7ad7f6
KP
960 * On Gen4 and above:
961 * wait for the pipe register state bit to turn off
962 *
963 * Otherwise:
964 * wait for the display line value to settle (it usually
965 * ends up stopping at the start of the next frame).
58e10eb9 966 *
9d0498a2 967 */
58e10eb9 968void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
969{
970 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
971 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
972 pipe);
ab7ad7f6
KP
973
974 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 975 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
976
977 /* Wait for the Pipe State to go off */
58e10eb9
CW
978 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
979 100))
284637d9 980 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 981 } else {
ab7ad7f6 982 /* Wait for the display line to settle */
fbf49ea2 983 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 984 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 985 }
79e53945
JB
986}
987
b0ea7d37
DL
988/*
989 * ibx_digital_port_connected - is the specified port connected?
990 * @dev_priv: i915 private structure
991 * @port: the port to test
992 *
993 * Returns true if @port is connected, false otherwise.
994 */
995bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
996 struct intel_digital_port *port)
997{
998 u32 bit;
999
c36346e3 1000 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1001 switch (port->port) {
c36346e3
DL
1002 case PORT_B:
1003 bit = SDE_PORTB_HOTPLUG;
1004 break;
1005 case PORT_C:
1006 bit = SDE_PORTC_HOTPLUG;
1007 break;
1008 case PORT_D:
1009 bit = SDE_PORTD_HOTPLUG;
1010 break;
1011 default:
1012 return true;
1013 }
1014 } else {
eba905b2 1015 switch (port->port) {
c36346e3
DL
1016 case PORT_B:
1017 bit = SDE_PORTB_HOTPLUG_CPT;
1018 break;
1019 case PORT_C:
1020 bit = SDE_PORTC_HOTPLUG_CPT;
1021 break;
1022 case PORT_D:
1023 bit = SDE_PORTD_HOTPLUG_CPT;
1024 break;
1025 default:
1026 return true;
1027 }
b0ea7d37
DL
1028 }
1029
1030 return I915_READ(SDEISR) & bit;
1031}
1032
b24e7179
JB
1033static const char *state_string(bool enabled)
1034{
1035 return enabled ? "on" : "off";
1036}
1037
1038/* Only for pre-ILK configs */
55607e8a
DV
1039void assert_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
b24e7179
JB
1041{
1042 int reg;
1043 u32 val;
1044 bool cur_state;
1045
1046 reg = DPLL(pipe);
1047 val = I915_READ(reg);
1048 cur_state = !!(val & DPLL_VCO_ENABLE);
1049 WARN(cur_state != state,
1050 "PLL state assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
1052}
b24e7179 1053
23538ef1
JN
1054/* XXX: the dsi pll is shared between MIPI DSI ports */
1055static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1056{
1057 u32 val;
1058 bool cur_state;
1059
1060 mutex_lock(&dev_priv->dpio_lock);
1061 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1062 mutex_unlock(&dev_priv->dpio_lock);
1063
1064 cur_state = val & DSI_PLL_VCO_EN;
1065 WARN(cur_state != state,
1066 "DSI PLL state assertion failure (expected %s, current %s)\n",
1067 state_string(state), state_string(cur_state));
1068}
1069#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1070#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1071
55607e8a 1072struct intel_shared_dpll *
e2b78267
DV
1073intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1074{
1075 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1076
a43f6e0f 1077 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1078 return NULL;
1079
a43f6e0f 1080 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1081}
1082
040484af 1083/* For ILK+ */
55607e8a
DV
1084void assert_shared_dpll(struct drm_i915_private *dev_priv,
1085 struct intel_shared_dpll *pll,
1086 bool state)
040484af 1087{
040484af 1088 bool cur_state;
5358901f 1089 struct intel_dpll_hw_state hw_state;
040484af 1090
9d82aa17
ED
1091 if (HAS_PCH_LPT(dev_priv->dev)) {
1092 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1093 return;
1094 }
1095
92b27b08 1096 if (WARN (!pll,
46edb027 1097 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1098 return;
ee7b9f93 1099
5358901f 1100 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1101 WARN(cur_state != state,
5358901f
DV
1102 "%s assertion failure (expected %s, current %s)\n",
1103 pll->name, state_string(state), state_string(cur_state));
040484af 1104}
040484af
JB
1105
1106static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1107 enum pipe pipe, bool state)
1108{
1109 int reg;
1110 u32 val;
1111 bool cur_state;
ad80a810
PZ
1112 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1113 pipe);
040484af 1114
affa9354
PZ
1115 if (HAS_DDI(dev_priv->dev)) {
1116 /* DDI does not have a specific FDI_TX register */
ad80a810 1117 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1118 val = I915_READ(reg);
ad80a810 1119 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1120 } else {
1121 reg = FDI_TX_CTL(pipe);
1122 val = I915_READ(reg);
1123 cur_state = !!(val & FDI_TX_ENABLE);
1124 }
040484af
JB
1125 WARN(cur_state != state,
1126 "FDI TX state assertion failure (expected %s, current %s)\n",
1127 state_string(state), state_string(cur_state));
1128}
1129#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1130#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1131
1132static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
1134{
1135 int reg;
1136 u32 val;
1137 bool cur_state;
1138
d63fa0dc
PZ
1139 reg = FDI_RX_CTL(pipe);
1140 val = I915_READ(reg);
1141 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1142 WARN(cur_state != state,
1143 "FDI RX state assertion failure (expected %s, current %s)\n",
1144 state_string(state), state_string(cur_state));
1145}
1146#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1147#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1148
1149static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1150 enum pipe pipe)
1151{
1152 int reg;
1153 u32 val;
1154
1155 /* ILK FDI PLL is always enabled */
3d13ef2e 1156 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1157 return;
1158
bf507ef7 1159 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1160 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1161 return;
1162
040484af
JB
1163 reg = FDI_TX_CTL(pipe);
1164 val = I915_READ(reg);
1165 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1166}
1167
55607e8a
DV
1168void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
040484af
JB
1170{
1171 int reg;
1172 u32 val;
55607e8a 1173 bool cur_state;
040484af
JB
1174
1175 reg = FDI_RX_CTL(pipe);
1176 val = I915_READ(reg);
55607e8a
DV
1177 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1178 WARN(cur_state != state,
1179 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1180 state_string(state), state_string(cur_state));
040484af
JB
1181}
1182
ea0760cf
JB
1183static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1184 enum pipe pipe)
1185{
1186 int pp_reg, lvds_reg;
1187 u32 val;
1188 enum pipe panel_pipe = PIPE_A;
0de3b485 1189 bool locked = true;
ea0760cf
JB
1190
1191 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1192 pp_reg = PCH_PP_CONTROL;
1193 lvds_reg = PCH_LVDS;
1194 } else {
1195 pp_reg = PP_CONTROL;
1196 lvds_reg = LVDS;
1197 }
1198
1199 val = I915_READ(pp_reg);
1200 if (!(val & PANEL_POWER_ON) ||
1201 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1202 locked = false;
1203
1204 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1205 panel_pipe = PIPE_B;
1206
1207 WARN(panel_pipe == pipe && locked,
1208 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1209 pipe_name(pipe));
ea0760cf
JB
1210}
1211
93ce0ba6
JN
1212static void assert_cursor(struct drm_i915_private *dev_priv,
1213 enum pipe pipe, bool state)
1214{
1215 struct drm_device *dev = dev_priv->dev;
1216 bool cur_state;
1217
d9d82081 1218 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1219 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1220 else
5efb3e28 1221 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1222
1223 WARN(cur_state != state,
1224 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1225 pipe_name(pipe), state_string(state), state_string(cur_state));
1226}
1227#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1228#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1229
b840d907
JB
1230void assert_pipe(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, bool state)
b24e7179
JB
1232{
1233 int reg;
1234 u32 val;
63d7bbe9 1235 bool cur_state;
702e7a56
PZ
1236 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1237 pipe);
b24e7179 1238
8e636784
DV
1239 /* if we need the pipe A quirk it must be always on */
1240 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1241 state = true;
1242
da7e29bd 1243 if (!intel_display_power_enabled(dev_priv,
b97186f0 1244 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1245 cur_state = false;
1246 } else {
1247 reg = PIPECONF(cpu_transcoder);
1248 val = I915_READ(reg);
1249 cur_state = !!(val & PIPECONF_ENABLE);
1250 }
1251
63d7bbe9
JB
1252 WARN(cur_state != state,
1253 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1254 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1255}
1256
931872fc
CW
1257static void assert_plane(struct drm_i915_private *dev_priv,
1258 enum plane plane, bool state)
b24e7179
JB
1259{
1260 int reg;
1261 u32 val;
931872fc 1262 bool cur_state;
b24e7179
JB
1263
1264 reg = DSPCNTR(plane);
1265 val = I915_READ(reg);
931872fc
CW
1266 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1267 WARN(cur_state != state,
1268 "plane %c assertion failure (expected %s, current %s)\n",
1269 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1270}
1271
931872fc
CW
1272#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1273#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1274
b24e7179
JB
1275static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1276 enum pipe pipe)
1277{
653e1026 1278 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1279 int reg, i;
1280 u32 val;
1281 int cur_pipe;
1282
653e1026
VS
1283 /* Primary planes are fixed to pipes on gen4+ */
1284 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1285 reg = DSPCNTR(pipe);
1286 val = I915_READ(reg);
83f26f16 1287 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1288 "plane %c assertion failure, should be disabled but not\n",
1289 plane_name(pipe));
19ec1358 1290 return;
28c05794 1291 }
19ec1358 1292
b24e7179 1293 /* Need to check both planes against the pipe */
08e2a7de 1294 for_each_pipe(i) {
b24e7179
JB
1295 reg = DSPCNTR(i);
1296 val = I915_READ(reg);
1297 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1298 DISPPLANE_SEL_PIPE_SHIFT;
1299 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1300 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1301 plane_name(i), pipe_name(pipe));
b24e7179
JB
1302 }
1303}
1304
19332d7a
JB
1305static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1306 enum pipe pipe)
1307{
20674eef 1308 struct drm_device *dev = dev_priv->dev;
1fe47785 1309 int reg, sprite;
19332d7a
JB
1310 u32 val;
1311
20674eef 1312 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1313 for_each_sprite(pipe, sprite) {
1314 reg = SPCNTR(pipe, sprite);
20674eef 1315 val = I915_READ(reg);
83f26f16 1316 WARN(val & SP_ENABLE,
20674eef 1317 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1318 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1319 }
1320 } else if (INTEL_INFO(dev)->gen >= 7) {
1321 reg = SPRCTL(pipe);
19332d7a 1322 val = I915_READ(reg);
83f26f16 1323 WARN(val & SPRITE_ENABLE,
06da8da2 1324 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1325 plane_name(pipe), pipe_name(pipe));
1326 } else if (INTEL_INFO(dev)->gen >= 5) {
1327 reg = DVSCNTR(pipe);
19332d7a 1328 val = I915_READ(reg);
83f26f16 1329 WARN(val & DVS_ENABLE,
06da8da2 1330 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1331 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1332 }
1333}
1334
89eff4be 1335static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1336{
1337 u32 val;
1338 bool enabled;
1339
89eff4be 1340 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1341
92f2584a
JB
1342 val = I915_READ(PCH_DREF_CONTROL);
1343 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1344 DREF_SUPERSPREAD_SOURCE_MASK));
1345 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1346}
1347
ab9412ba
DV
1348static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1349 enum pipe pipe)
92f2584a
JB
1350{
1351 int reg;
1352 u32 val;
1353 bool enabled;
1354
ab9412ba 1355 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1356 val = I915_READ(reg);
1357 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1358 WARN(enabled,
1359 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1360 pipe_name(pipe));
92f2584a
JB
1361}
1362
4e634389
KP
1363static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1365{
1366 if ((val & DP_PORT_EN) == 0)
1367 return false;
1368
1369 if (HAS_PCH_CPT(dev_priv->dev)) {
1370 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1371 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1372 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1373 return false;
44f37d1f
CML
1374 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1375 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1376 return false;
f0575e92
KP
1377 } else {
1378 if ((val & DP_PIPE_MASK) != (pipe << 30))
1379 return false;
1380 }
1381 return true;
1382}
1383
1519b995
KP
1384static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1385 enum pipe pipe, u32 val)
1386{
dc0fa718 1387 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1388 return false;
1389
1390 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1391 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1392 return false;
44f37d1f
CML
1393 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1394 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1395 return false;
1519b995 1396 } else {
dc0fa718 1397 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1398 return false;
1399 }
1400 return true;
1401}
1402
1403static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe, u32 val)
1405{
1406 if ((val & LVDS_PORT_EN) == 0)
1407 return false;
1408
1409 if (HAS_PCH_CPT(dev_priv->dev)) {
1410 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1411 return false;
1412 } else {
1413 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1414 return false;
1415 }
1416 return true;
1417}
1418
1419static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1420 enum pipe pipe, u32 val)
1421{
1422 if ((val & ADPA_DAC_ENABLE) == 0)
1423 return false;
1424 if (HAS_PCH_CPT(dev_priv->dev)) {
1425 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1426 return false;
1427 } else {
1428 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1429 return false;
1430 }
1431 return true;
1432}
1433
291906f1 1434static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1435 enum pipe pipe, int reg, u32 port_sel)
291906f1 1436{
47a05eca 1437 u32 val = I915_READ(reg);
4e634389 1438 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1439 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1440 reg, pipe_name(pipe));
de9a35ab 1441
75c5da27
DV
1442 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1443 && (val & DP_PIPEB_SELECT),
de9a35ab 1444 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1445}
1446
1447static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1448 enum pipe pipe, int reg)
1449{
47a05eca 1450 u32 val = I915_READ(reg);
b70ad586 1451 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1452 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1453 reg, pipe_name(pipe));
de9a35ab 1454
dc0fa718 1455 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1456 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1457 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1458}
1459
1460static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe)
1462{
1463 int reg;
1464 u32 val;
291906f1 1465
f0575e92
KP
1466 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1467 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1468 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1469
1470 reg = PCH_ADPA;
1471 val = I915_READ(reg);
b70ad586 1472 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1473 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1474 pipe_name(pipe));
291906f1
JB
1475
1476 reg = PCH_LVDS;
1477 val = I915_READ(reg);
b70ad586 1478 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1479 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1480 pipe_name(pipe));
291906f1 1481
e2debe91
PZ
1482 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1483 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1485}
1486
40e9cf64
JB
1487static void intel_init_dpio(struct drm_device *dev)
1488{
1489 struct drm_i915_private *dev_priv = dev->dev_private;
1490
1491 if (!IS_VALLEYVIEW(dev))
1492 return;
1493
a09caddd
CML
1494 /*
1495 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1496 * CHV x1 PHY (DP/HDMI D)
1497 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1498 */
1499 if (IS_CHERRYVIEW(dev)) {
1500 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1501 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1502 } else {
1503 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1504 }
5382f5f3
JB
1505}
1506
1507static void intel_reset_dpio(struct drm_device *dev)
1508{
1509 struct drm_i915_private *dev_priv = dev->dev_private;
1510
1511 if (!IS_VALLEYVIEW(dev))
1512 return;
1513
076ed3b2
CML
1514 if (IS_CHERRYVIEW(dev)) {
1515 enum dpio_phy phy;
1516 u32 val;
1517
1518 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1519 /* Poll for phypwrgood signal */
1520 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1521 PHY_POWERGOOD(phy), 1))
1522 DRM_ERROR("Display PHY %d is not power up\n", phy);
1523
1524 /*
1525 * Deassert common lane reset for PHY.
1526 *
1527 * This should only be done on init and resume from S3
1528 * with both PLLs disabled, or we risk losing DPIO and
1529 * PLL synchronization.
1530 */
1531 val = I915_READ(DISPLAY_PHY_CONTROL);
1532 I915_WRITE(DISPLAY_PHY_CONTROL,
1533 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1534 }
1535
1536 } else {
1537 /*
57021059
JB
1538 * If DPIO has already been reset, e.g. by BIOS, just skip all
1539 * this.
076ed3b2 1540 */
57021059
JB
1541 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1542 return;
1543
1544 /*
1545 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1546 * Need to assert and de-assert PHY SB reset by gating the
1547 * common lane power, then un-gating it.
1548 * Simply ungating isn't enough to reset the PHY enough to get
1549 * ports and lanes running.
1550 */
1551 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1552 false);
1553 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1554 true);
076ed3b2 1555 }
40e9cf64
JB
1556}
1557
426115cf 1558static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1559{
426115cf
DV
1560 struct drm_device *dev = crtc->base.dev;
1561 struct drm_i915_private *dev_priv = dev->dev_private;
1562 int reg = DPLL(crtc->pipe);
1563 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1564
426115cf 1565 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1566
1567 /* No really, not for ILK+ */
1568 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
426115cf 1572 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1573
426115cf
DV
1574 I915_WRITE(reg, dpll);
1575 POSTING_READ(reg);
1576 udelay(150);
1577
1578 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1579 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1580
1581 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1582 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1583
1584 /* We do this three times for luck */
426115cf 1585 I915_WRITE(reg, dpll);
87442f73
DV
1586 POSTING_READ(reg);
1587 udelay(150); /* wait for warmup */
426115cf 1588 I915_WRITE(reg, dpll);
87442f73
DV
1589 POSTING_READ(reg);
1590 udelay(150); /* wait for warmup */
426115cf 1591 I915_WRITE(reg, dpll);
87442f73
DV
1592 POSTING_READ(reg);
1593 udelay(150); /* wait for warmup */
1594}
1595
9d556c99
CML
1596static void chv_enable_pll(struct intel_crtc *crtc)
1597{
1598 struct drm_device *dev = crtc->base.dev;
1599 struct drm_i915_private *dev_priv = dev->dev_private;
1600 int pipe = crtc->pipe;
1601 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1602 u32 tmp;
1603
1604 assert_pipe_disabled(dev_priv, crtc->pipe);
1605
1606 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1607
1608 mutex_lock(&dev_priv->dpio_lock);
1609
1610 /* Enable back the 10bit clock to display controller */
1611 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1612 tmp |= DPIO_DCLKP_EN;
1613 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1614
1615 /*
1616 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1617 */
1618 udelay(1);
1619
1620 /* Enable PLL */
a11b0703 1621 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1622
1623 /* Check PLL is locked */
a11b0703 1624 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1625 DRM_ERROR("PLL %d failed to lock\n", pipe);
1626
a11b0703
VS
1627 /* not sure when this should be written */
1628 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1629 POSTING_READ(DPLL_MD(pipe));
1630
9d556c99
CML
1631 mutex_unlock(&dev_priv->dpio_lock);
1632}
1633
66e3d5c0 1634static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1635{
66e3d5c0
DV
1636 struct drm_device *dev = crtc->base.dev;
1637 struct drm_i915_private *dev_priv = dev->dev_private;
1638 int reg = DPLL(crtc->pipe);
1639 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1640
66e3d5c0 1641 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1642
63d7bbe9 1643 /* No really, not for ILK+ */
3d13ef2e 1644 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1645
1646 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1647 if (IS_MOBILE(dev) && !IS_I830(dev))
1648 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1649
66e3d5c0
DV
1650 I915_WRITE(reg, dpll);
1651
1652 /* Wait for the clocks to stabilize. */
1653 POSTING_READ(reg);
1654 udelay(150);
1655
1656 if (INTEL_INFO(dev)->gen >= 4) {
1657 I915_WRITE(DPLL_MD(crtc->pipe),
1658 crtc->config.dpll_hw_state.dpll_md);
1659 } else {
1660 /* The pixel multiplier can only be updated once the
1661 * DPLL is enabled and the clocks are stable.
1662 *
1663 * So write it again.
1664 */
1665 I915_WRITE(reg, dpll);
1666 }
63d7bbe9
JB
1667
1668 /* We do this three times for luck */
66e3d5c0 1669 I915_WRITE(reg, dpll);
63d7bbe9
JB
1670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
66e3d5c0 1672 I915_WRITE(reg, dpll);
63d7bbe9
JB
1673 POSTING_READ(reg);
1674 udelay(150); /* wait for warmup */
66e3d5c0 1675 I915_WRITE(reg, dpll);
63d7bbe9
JB
1676 POSTING_READ(reg);
1677 udelay(150); /* wait for warmup */
1678}
1679
1680/**
50b44a44 1681 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1682 * @dev_priv: i915 private structure
1683 * @pipe: pipe PLL to disable
1684 *
1685 * Disable the PLL for @pipe, making sure the pipe is off first.
1686 *
1687 * Note! This is for pre-ILK only.
1688 */
50b44a44 1689static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1690{
63d7bbe9
JB
1691 /* Don't disable pipe A or pipe A PLLs if needed */
1692 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1693 return;
1694
1695 /* Make sure the pipe isn't still relying on us */
1696 assert_pipe_disabled(dev_priv, pipe);
1697
50b44a44
DV
1698 I915_WRITE(DPLL(pipe), 0);
1699 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1700}
1701
f6071166
JB
1702static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1703{
1704 u32 val = 0;
1705
1706 /* Make sure the pipe isn't still relying on us */
1707 assert_pipe_disabled(dev_priv, pipe);
1708
e5cbfbfb
ID
1709 /*
1710 * Leave integrated clock source and reference clock enabled for pipe B.
1711 * The latter is needed for VGA hotplug / manual detection.
1712 */
f6071166 1713 if (pipe == PIPE_B)
e5cbfbfb 1714 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1715 I915_WRITE(DPLL(pipe), val);
1716 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1717
1718}
1719
1720static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1721{
d752048d 1722 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1723 u32 val;
1724
a11b0703
VS
1725 /* Make sure the pipe isn't still relying on us */
1726 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1727
a11b0703
VS
1728 /* Set PLL en = 0 */
1729 val = DPLL_SSC_REF_CLOCK_CHV;
1730 if (pipe != PIPE_A)
1731 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1732 I915_WRITE(DPLL(pipe), val);
1733 POSTING_READ(DPLL(pipe));
d752048d
VS
1734
1735 mutex_lock(&dev_priv->dpio_lock);
1736
1737 /* Disable 10bit clock to display controller */
1738 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1739 val &= ~DPIO_DCLKP_EN;
1740 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1741
61407f6d
VS
1742 /* disable left/right clock distribution */
1743 if (pipe != PIPE_B) {
1744 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1745 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1746 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1747 } else {
1748 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1749 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1750 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1751 }
1752
d752048d 1753 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1754}
1755
e4607fcf
CML
1756void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1757 struct intel_digital_port *dport)
89b667f8
JB
1758{
1759 u32 port_mask;
00fc31b7 1760 int dpll_reg;
89b667f8 1761
e4607fcf
CML
1762 switch (dport->port) {
1763 case PORT_B:
89b667f8 1764 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1765 dpll_reg = DPLL(0);
e4607fcf
CML
1766 break;
1767 case PORT_C:
89b667f8 1768 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1769 dpll_reg = DPLL(0);
1770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1774 break;
1775 default:
1776 BUG();
1777 }
89b667f8 1778
00fc31b7 1779 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1780 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1781 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1782}
1783
b14b1055
DV
1784static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1785{
1786 struct drm_device *dev = crtc->base.dev;
1787 struct drm_i915_private *dev_priv = dev->dev_private;
1788 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1789
be19f0ff
CW
1790 if (WARN_ON(pll == NULL))
1791 return;
1792
b14b1055
DV
1793 WARN_ON(!pll->refcount);
1794 if (pll->active == 0) {
1795 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1796 WARN_ON(pll->on);
1797 assert_shared_dpll_disabled(dev_priv, pll);
1798
1799 pll->mode_set(dev_priv, pll);
1800 }
1801}
1802
92f2584a 1803/**
85b3894f 1804 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1805 * @dev_priv: i915 private structure
1806 * @pipe: pipe PLL to enable
1807 *
1808 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1809 * drives the transcoder clock.
1810 */
85b3894f 1811static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1812{
3d13ef2e
DL
1813 struct drm_device *dev = crtc->base.dev;
1814 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1815 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1816
87a875bb 1817 if (WARN_ON(pll == NULL))
48da64a8
CW
1818 return;
1819
1820 if (WARN_ON(pll->refcount == 0))
1821 return;
ee7b9f93 1822
46edb027
DV
1823 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1824 pll->name, pll->active, pll->on,
e2b78267 1825 crtc->base.base.id);
92f2584a 1826
cdbd2316
DV
1827 if (pll->active++) {
1828 WARN_ON(!pll->on);
e9d6944e 1829 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1830 return;
1831 }
f4a091c7 1832 WARN_ON(pll->on);
ee7b9f93 1833
46edb027 1834 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1835 pll->enable(dev_priv, pll);
ee7b9f93 1836 pll->on = true;
92f2584a
JB
1837}
1838
e2b78267 1839static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1840{
3d13ef2e
DL
1841 struct drm_device *dev = crtc->base.dev;
1842 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1843 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1844
92f2584a 1845 /* PCH only available on ILK+ */
3d13ef2e 1846 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1847 if (WARN_ON(pll == NULL))
ee7b9f93 1848 return;
92f2584a 1849
48da64a8
CW
1850 if (WARN_ON(pll->refcount == 0))
1851 return;
7a419866 1852
46edb027
DV
1853 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1854 pll->name, pll->active, pll->on,
e2b78267 1855 crtc->base.base.id);
7a419866 1856
48da64a8 1857 if (WARN_ON(pll->active == 0)) {
e9d6944e 1858 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1859 return;
1860 }
1861
e9d6944e 1862 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1863 WARN_ON(!pll->on);
cdbd2316 1864 if (--pll->active)
7a419866 1865 return;
ee7b9f93 1866
46edb027 1867 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1868 pll->disable(dev_priv, pll);
ee7b9f93 1869 pll->on = false;
92f2584a
JB
1870}
1871
b8a4f404
PZ
1872static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1873 enum pipe pipe)
040484af 1874{
23670b32 1875 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1876 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1878 uint32_t reg, val, pipeconf_val;
040484af
JB
1879
1880 /* PCH only available on ILK+ */
3d13ef2e 1881 BUG_ON(INTEL_INFO(dev)->gen < 5);
040484af
JB
1882
1883 /* Make sure PCH DPLL is enabled */
e72f9fbf 1884 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1885 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1886
1887 /* FDI must be feeding us bits for PCH ports */
1888 assert_fdi_tx_enabled(dev_priv, pipe);
1889 assert_fdi_rx_enabled(dev_priv, pipe);
1890
23670b32
DV
1891 if (HAS_PCH_CPT(dev)) {
1892 /* Workaround: Set the timing override bit before enabling the
1893 * pch transcoder. */
1894 reg = TRANS_CHICKEN2(pipe);
1895 val = I915_READ(reg);
1896 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1897 I915_WRITE(reg, val);
59c859d6 1898 }
23670b32 1899
ab9412ba 1900 reg = PCH_TRANSCONF(pipe);
040484af 1901 val = I915_READ(reg);
5f7f726d 1902 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1903
1904 if (HAS_PCH_IBX(dev_priv->dev)) {
1905 /*
1906 * make the BPC in transcoder be consistent with
1907 * that in pipeconf reg.
1908 */
dfd07d72
DV
1909 val &= ~PIPECONF_BPC_MASK;
1910 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1911 }
5f7f726d
PZ
1912
1913 val &= ~TRANS_INTERLACE_MASK;
1914 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1915 if (HAS_PCH_IBX(dev_priv->dev) &&
1916 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1917 val |= TRANS_LEGACY_INTERLACED_ILK;
1918 else
1919 val |= TRANS_INTERLACED;
5f7f726d
PZ
1920 else
1921 val |= TRANS_PROGRESSIVE;
1922
040484af
JB
1923 I915_WRITE(reg, val | TRANS_ENABLE);
1924 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1925 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1926}
1927
8fb033d7 1928static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1929 enum transcoder cpu_transcoder)
040484af 1930{
8fb033d7 1931 u32 val, pipeconf_val;
8fb033d7
PZ
1932
1933 /* PCH only available on ILK+ */
3d13ef2e 1934 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
8fb033d7 1935
8fb033d7 1936 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1937 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1938 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1939
223a6fdf
PZ
1940 /* Workaround: set timing override bit. */
1941 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1942 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1943 I915_WRITE(_TRANSA_CHICKEN2, val);
1944
25f3ef11 1945 val = TRANS_ENABLE;
937bb610 1946 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1947
9a76b1c6
PZ
1948 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1949 PIPECONF_INTERLACED_ILK)
a35f2679 1950 val |= TRANS_INTERLACED;
8fb033d7
PZ
1951 else
1952 val |= TRANS_PROGRESSIVE;
1953
ab9412ba
DV
1954 I915_WRITE(LPT_TRANSCONF, val);
1955 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1956 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1957}
1958
b8a4f404
PZ
1959static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1960 enum pipe pipe)
040484af 1961{
23670b32
DV
1962 struct drm_device *dev = dev_priv->dev;
1963 uint32_t reg, val;
040484af
JB
1964
1965 /* FDI relies on the transcoder */
1966 assert_fdi_tx_disabled(dev_priv, pipe);
1967 assert_fdi_rx_disabled(dev_priv, pipe);
1968
291906f1
JB
1969 /* Ports must be off as well */
1970 assert_pch_ports_disabled(dev_priv, pipe);
1971
ab9412ba 1972 reg = PCH_TRANSCONF(pipe);
040484af
JB
1973 val = I915_READ(reg);
1974 val &= ~TRANS_ENABLE;
1975 I915_WRITE(reg, val);
1976 /* wait for PCH transcoder off, transcoder state */
1977 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1978 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1979
1980 if (!HAS_PCH_IBX(dev)) {
1981 /* Workaround: Clear the timing override chicken bit again. */
1982 reg = TRANS_CHICKEN2(pipe);
1983 val = I915_READ(reg);
1984 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1985 I915_WRITE(reg, val);
1986 }
040484af
JB
1987}
1988
ab4d966c 1989static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1990{
8fb033d7
PZ
1991 u32 val;
1992
ab9412ba 1993 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1994 val &= ~TRANS_ENABLE;
ab9412ba 1995 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1996 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1997 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1998 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1999
2000 /* Workaround: clear timing override bit. */
2001 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2002 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2003 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2004}
2005
b24e7179 2006/**
309cfea8 2007 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2008 * @crtc: crtc responsible for the pipe
b24e7179 2009 *
0372264a 2010 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2011 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2012 */
e1fdc473 2013static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2014{
0372264a
PZ
2015 struct drm_device *dev = crtc->base.dev;
2016 struct drm_i915_private *dev_priv = dev->dev_private;
2017 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2018 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2019 pipe);
1a240d4d 2020 enum pipe pch_transcoder;
b24e7179
JB
2021 int reg;
2022 u32 val;
2023
58c6eaa2 2024 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2025 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2026 assert_sprites_disabled(dev_priv, pipe);
2027
681e5811 2028 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2029 pch_transcoder = TRANSCODER_A;
2030 else
2031 pch_transcoder = pipe;
2032
b24e7179
JB
2033 /*
2034 * A pipe without a PLL won't actually be able to drive bits from
2035 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2036 * need the check.
2037 */
2038 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2039 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2040 assert_dsi_pll_enabled(dev_priv);
2041 else
2042 assert_pll_enabled(dev_priv, pipe);
040484af 2043 else {
30421c4f 2044 if (crtc->config.has_pch_encoder) {
040484af 2045 /* if driving the PCH, we need FDI enabled */
cc391bbb 2046 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2047 assert_fdi_tx_pll_enabled(dev_priv,
2048 (enum pipe) cpu_transcoder);
040484af
JB
2049 }
2050 /* FIXME: assert CPU port conditions for SNB+ */
2051 }
b24e7179 2052
702e7a56 2053 reg = PIPECONF(cpu_transcoder);
b24e7179 2054 val = I915_READ(reg);
7ad25d48
PZ
2055 if (val & PIPECONF_ENABLE) {
2056 WARN_ON(!(pipe == PIPE_A &&
2057 dev_priv->quirks & QUIRK_PIPEA_FORCE));
00d70b15 2058 return;
7ad25d48 2059 }
00d70b15
CW
2060
2061 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2062 POSTING_READ(reg);
b24e7179
JB
2063}
2064
2065/**
309cfea8 2066 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
2067 * @dev_priv: i915 private structure
2068 * @pipe: pipe to disable
2069 *
2070 * Disable @pipe, making sure that various hardware specific requirements
2071 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2072 *
2073 * @pipe should be %PIPE_A or %PIPE_B.
2074 *
2075 * Will wait until the pipe has shut down before returning.
2076 */
2077static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2078 enum pipe pipe)
2079{
702e7a56
PZ
2080 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2081 pipe);
b24e7179
JB
2082 int reg;
2083 u32 val;
2084
2085 /*
2086 * Make sure planes won't keep trying to pump pixels to us,
2087 * or we might hang the display.
2088 */
2089 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2090 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2091 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
2092
2093 /* Don't disable pipe A or pipe A PLLs if needed */
2094 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2095 return;
2096
702e7a56 2097 reg = PIPECONF(cpu_transcoder);
b24e7179 2098 val = I915_READ(reg);
00d70b15
CW
2099 if ((val & PIPECONF_ENABLE) == 0)
2100 return;
2101
2102 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
2103 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2104}
2105
d74362c9
KP
2106/*
2107 * Plane regs are double buffered, going from enabled->disabled needs a
2108 * trigger in order to latch. The display address reg provides this.
2109 */
1dba99f4
VS
2110void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2111 enum plane plane)
d74362c9 2112{
3d13ef2e
DL
2113 struct drm_device *dev = dev_priv->dev;
2114 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2115
2116 I915_WRITE(reg, I915_READ(reg));
2117 POSTING_READ(reg);
d74362c9
KP
2118}
2119
b24e7179 2120/**
262ca2b0 2121 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
b24e7179
JB
2122 * @dev_priv: i915 private structure
2123 * @plane: plane to enable
2124 * @pipe: pipe being fed
2125 *
2126 * Enable @plane on @pipe, making sure that @pipe is running first.
2127 */
262ca2b0
MR
2128static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2129 enum plane plane, enum pipe pipe)
b24e7179 2130{
939c2fe8
VS
2131 struct intel_crtc *intel_crtc =
2132 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2133 int reg;
2134 u32 val;
2135
2136 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2137 assert_pipe_enabled(dev_priv, pipe);
2138
98ec7739
VS
2139 if (intel_crtc->primary_enabled)
2140 return;
0037f71c 2141
4c445e0e 2142 intel_crtc->primary_enabled = true;
939c2fe8 2143
b24e7179
JB
2144 reg = DSPCNTR(plane);
2145 val = I915_READ(reg);
10efa932 2146 WARN_ON(val & DISPLAY_PLANE_ENABLE);
00d70b15
CW
2147
2148 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1dba99f4 2149 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2150}
2151
b24e7179 2152/**
262ca2b0 2153 * intel_disable_primary_hw_plane - disable the primary hardware plane
b24e7179
JB
2154 * @dev_priv: i915 private structure
2155 * @plane: plane to disable
2156 * @pipe: pipe consuming the data
2157 *
2158 * Disable @plane; should be an independent operation.
2159 */
262ca2b0
MR
2160static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2161 enum plane plane, enum pipe pipe)
b24e7179 2162{
939c2fe8
VS
2163 struct intel_crtc *intel_crtc =
2164 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2165 int reg;
2166 u32 val;
2167
98ec7739
VS
2168 if (!intel_crtc->primary_enabled)
2169 return;
0037f71c 2170
4c445e0e 2171 intel_crtc->primary_enabled = false;
939c2fe8 2172
b24e7179
JB
2173 reg = DSPCNTR(plane);
2174 val = I915_READ(reg);
10efa932 2175 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
00d70b15
CW
2176
2177 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1dba99f4 2178 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2179}
2180
693db184
CW
2181static bool need_vtd_wa(struct drm_device *dev)
2182{
2183#ifdef CONFIG_INTEL_IOMMU
2184 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2185 return true;
2186#endif
2187 return false;
2188}
2189
a57ce0b2
JB
2190static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2191{
2192 int tile_height;
2193
2194 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2195 return ALIGN(height, tile_height);
2196}
2197
127bd2ac 2198int
48b956c5 2199intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2200 struct drm_i915_gem_object *obj,
a4872ba6 2201 struct intel_engine_cs *pipelined)
6b95a207 2202{
ce453d81 2203 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2204 u32 alignment;
2205 int ret;
2206
05394f39 2207 switch (obj->tiling_mode) {
6b95a207 2208 case I915_TILING_NONE:
534843da
CW
2209 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2210 alignment = 128 * 1024;
a6c45cf0 2211 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2212 alignment = 4 * 1024;
2213 else
2214 alignment = 64 * 1024;
6b95a207
KH
2215 break;
2216 case I915_TILING_X:
2217 /* pin() will align the object as required by fence */
2218 alignment = 0;
2219 break;
2220 case I915_TILING_Y:
80075d49 2221 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2222 return -EINVAL;
2223 default:
2224 BUG();
2225 }
2226
693db184
CW
2227 /* Note that the w/a also requires 64 PTE of padding following the
2228 * bo. We currently fill all unused PTE with the shadow page and so
2229 * we should always have valid PTE following the scanout preventing
2230 * the VT-d warning.
2231 */
2232 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2233 alignment = 256 * 1024;
2234
ce453d81 2235 dev_priv->mm.interruptible = false;
2da3b9b9 2236 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2237 if (ret)
ce453d81 2238 goto err_interruptible;
6b95a207
KH
2239
2240 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2241 * fence, whereas 965+ only requires a fence if using
2242 * framebuffer compression. For simplicity, we always install
2243 * a fence as the cost is not that onerous.
2244 */
06d98131 2245 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2246 if (ret)
2247 goto err_unpin;
1690e1eb 2248
9a5a53b3 2249 i915_gem_object_pin_fence(obj);
6b95a207 2250
ce453d81 2251 dev_priv->mm.interruptible = true;
6b95a207 2252 return 0;
48b956c5
CW
2253
2254err_unpin:
cc98b413 2255 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2256err_interruptible:
2257 dev_priv->mm.interruptible = true;
48b956c5 2258 return ret;
6b95a207
KH
2259}
2260
1690e1eb
CW
2261void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2262{
2263 i915_gem_object_unpin_fence(obj);
cc98b413 2264 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2265}
2266
c2c75131
DV
2267/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2268 * is assumed to be a power-of-two. */
bc752862
CW
2269unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2270 unsigned int tiling_mode,
2271 unsigned int cpp,
2272 unsigned int pitch)
c2c75131 2273{
bc752862
CW
2274 if (tiling_mode != I915_TILING_NONE) {
2275 unsigned int tile_rows, tiles;
c2c75131 2276
bc752862
CW
2277 tile_rows = *y / 8;
2278 *y %= 8;
c2c75131 2279
bc752862
CW
2280 tiles = *x / (512/cpp);
2281 *x %= 512/cpp;
2282
2283 return tile_rows * pitch * 8 + tiles * 4096;
2284 } else {
2285 unsigned int offset;
2286
2287 offset = *y * pitch + *x * cpp;
2288 *y = 0;
2289 *x = (offset & 4095) / cpp;
2290 return offset & -4096;
2291 }
c2c75131
DV
2292}
2293
46f297fb
JB
2294int intel_format_to_fourcc(int format)
2295{
2296 switch (format) {
2297 case DISPPLANE_8BPP:
2298 return DRM_FORMAT_C8;
2299 case DISPPLANE_BGRX555:
2300 return DRM_FORMAT_XRGB1555;
2301 case DISPPLANE_BGRX565:
2302 return DRM_FORMAT_RGB565;
2303 default:
2304 case DISPPLANE_BGRX888:
2305 return DRM_FORMAT_XRGB8888;
2306 case DISPPLANE_RGBX888:
2307 return DRM_FORMAT_XBGR8888;
2308 case DISPPLANE_BGRX101010:
2309 return DRM_FORMAT_XRGB2101010;
2310 case DISPPLANE_RGBX101010:
2311 return DRM_FORMAT_XBGR2101010;
2312 }
2313}
2314
484b41dd 2315static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2316 struct intel_plane_config *plane_config)
2317{
2318 struct drm_device *dev = crtc->base.dev;
2319 struct drm_i915_gem_object *obj = NULL;
2320 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2321 u32 base = plane_config->base;
2322
ff2652ea
CW
2323 if (plane_config->size == 0)
2324 return false;
2325
46f297fb
JB
2326 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2327 plane_config->size);
2328 if (!obj)
484b41dd 2329 return false;
46f297fb
JB
2330
2331 if (plane_config->tiled) {
2332 obj->tiling_mode = I915_TILING_X;
66e514c1 2333 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2334 }
2335
66e514c1
DA
2336 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2337 mode_cmd.width = crtc->base.primary->fb->width;
2338 mode_cmd.height = crtc->base.primary->fb->height;
2339 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2340
2341 mutex_lock(&dev->struct_mutex);
2342
66e514c1 2343 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2344 &mode_cmd, obj)) {
46f297fb
JB
2345 DRM_DEBUG_KMS("intel fb init failed\n");
2346 goto out_unref_obj;
2347 }
2348
2349 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2350
2351 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2352 return true;
46f297fb
JB
2353
2354out_unref_obj:
2355 drm_gem_object_unreference(&obj->base);
2356 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2357 return false;
2358}
2359
2360static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2361 struct intel_plane_config *plane_config)
2362{
2363 struct drm_device *dev = intel_crtc->base.dev;
2364 struct drm_crtc *c;
2365 struct intel_crtc *i;
2366 struct intel_framebuffer *fb;
2367
66e514c1 2368 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2369 return;
2370
2371 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2372 return;
2373
66e514c1
DA
2374 kfree(intel_crtc->base.primary->fb);
2375 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2376
2377 /*
2378 * Failed to alloc the obj, check to see if we should share
2379 * an fb with another CRTC instead
2380 */
70e1e0ec 2381 for_each_crtc(dev, c) {
484b41dd
JB
2382 i = to_intel_crtc(c);
2383
2384 if (c == &intel_crtc->base)
2385 continue;
2386
66e514c1 2387 if (!i->active || !c->primary->fb)
484b41dd
JB
2388 continue;
2389
66e514c1 2390 fb = to_intel_framebuffer(c->primary->fb);
484b41dd 2391 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
66e514c1
DA
2392 drm_framebuffer_reference(c->primary->fb);
2393 intel_crtc->base.primary->fb = c->primary->fb;
484b41dd
JB
2394 break;
2395 }
2396 }
46f297fb
JB
2397}
2398
29b9bde6
DV
2399static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2400 struct drm_framebuffer *fb,
2401 int x, int y)
81255565
JB
2402{
2403 struct drm_device *dev = crtc->dev;
2404 struct drm_i915_private *dev_priv = dev->dev_private;
2405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2406 struct intel_framebuffer *intel_fb;
05394f39 2407 struct drm_i915_gem_object *obj;
81255565 2408 int plane = intel_crtc->plane;
e506a0c6 2409 unsigned long linear_offset;
81255565 2410 u32 dspcntr;
5eddb70b 2411 u32 reg;
81255565 2412
81255565
JB
2413 intel_fb = to_intel_framebuffer(fb);
2414 obj = intel_fb->obj;
81255565 2415
5eddb70b
CW
2416 reg = DSPCNTR(plane);
2417 dspcntr = I915_READ(reg);
81255565
JB
2418 /* Mask out pixel format bits in case we change it */
2419 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2420 switch (fb->pixel_format) {
2421 case DRM_FORMAT_C8:
81255565
JB
2422 dspcntr |= DISPPLANE_8BPP;
2423 break;
57779d06
VS
2424 case DRM_FORMAT_XRGB1555:
2425 case DRM_FORMAT_ARGB1555:
2426 dspcntr |= DISPPLANE_BGRX555;
81255565 2427 break;
57779d06
VS
2428 case DRM_FORMAT_RGB565:
2429 dspcntr |= DISPPLANE_BGRX565;
2430 break;
2431 case DRM_FORMAT_XRGB8888:
2432 case DRM_FORMAT_ARGB8888:
2433 dspcntr |= DISPPLANE_BGRX888;
2434 break;
2435 case DRM_FORMAT_XBGR8888:
2436 case DRM_FORMAT_ABGR8888:
2437 dspcntr |= DISPPLANE_RGBX888;
2438 break;
2439 case DRM_FORMAT_XRGB2101010:
2440 case DRM_FORMAT_ARGB2101010:
2441 dspcntr |= DISPPLANE_BGRX101010;
2442 break;
2443 case DRM_FORMAT_XBGR2101010:
2444 case DRM_FORMAT_ABGR2101010:
2445 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2446 break;
2447 default:
baba133a 2448 BUG();
81255565 2449 }
57779d06 2450
a6c45cf0 2451 if (INTEL_INFO(dev)->gen >= 4) {
05394f39 2452 if (obj->tiling_mode != I915_TILING_NONE)
81255565
JB
2453 dspcntr |= DISPPLANE_TILED;
2454 else
2455 dspcntr &= ~DISPPLANE_TILED;
2456 }
2457
de1aa629
VS
2458 if (IS_G4X(dev))
2459 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2460
5eddb70b 2461 I915_WRITE(reg, dspcntr);
81255565 2462
e506a0c6 2463 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
81255565 2464
c2c75131
DV
2465 if (INTEL_INFO(dev)->gen >= 4) {
2466 intel_crtc->dspaddr_offset =
bc752862
CW
2467 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2468 fb->bits_per_pixel / 8,
2469 fb->pitches[0]);
c2c75131
DV
2470 linear_offset -= intel_crtc->dspaddr_offset;
2471 } else {
e506a0c6 2472 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2473 }
e506a0c6 2474
f343c5f6
BW
2475 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2476 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2477 fb->pitches[0]);
01f2c773 2478 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2479 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2480 I915_WRITE(DSPSURF(plane),
2481 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2482 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2483 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2484 } else
f343c5f6 2485 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2486 POSTING_READ(reg);
17638cd6
JB
2487}
2488
29b9bde6
DV
2489static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2490 struct drm_framebuffer *fb,
2491 int x, int y)
17638cd6
JB
2492{
2493 struct drm_device *dev = crtc->dev;
2494 struct drm_i915_private *dev_priv = dev->dev_private;
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 struct intel_framebuffer *intel_fb;
2497 struct drm_i915_gem_object *obj;
2498 int plane = intel_crtc->plane;
e506a0c6 2499 unsigned long linear_offset;
17638cd6
JB
2500 u32 dspcntr;
2501 u32 reg;
2502
17638cd6
JB
2503 intel_fb = to_intel_framebuffer(fb);
2504 obj = intel_fb->obj;
2505
2506 reg = DSPCNTR(plane);
2507 dspcntr = I915_READ(reg);
2508 /* Mask out pixel format bits in case we change it */
2509 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2510 switch (fb->pixel_format) {
2511 case DRM_FORMAT_C8:
17638cd6
JB
2512 dspcntr |= DISPPLANE_8BPP;
2513 break;
57779d06
VS
2514 case DRM_FORMAT_RGB565:
2515 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2516 break;
57779d06
VS
2517 case DRM_FORMAT_XRGB8888:
2518 case DRM_FORMAT_ARGB8888:
2519 dspcntr |= DISPPLANE_BGRX888;
2520 break;
2521 case DRM_FORMAT_XBGR8888:
2522 case DRM_FORMAT_ABGR8888:
2523 dspcntr |= DISPPLANE_RGBX888;
2524 break;
2525 case DRM_FORMAT_XRGB2101010:
2526 case DRM_FORMAT_ARGB2101010:
2527 dspcntr |= DISPPLANE_BGRX101010;
2528 break;
2529 case DRM_FORMAT_XBGR2101010:
2530 case DRM_FORMAT_ABGR2101010:
2531 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2532 break;
2533 default:
baba133a 2534 BUG();
17638cd6
JB
2535 }
2536
2537 if (obj->tiling_mode != I915_TILING_NONE)
2538 dspcntr |= DISPPLANE_TILED;
2539 else
2540 dspcntr &= ~DISPPLANE_TILED;
2541
b42c6009 2542 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1f5d76db
PZ
2543 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2544 else
2545 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6
JB
2546
2547 I915_WRITE(reg, dspcntr);
2548
e506a0c6 2549 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
c2c75131 2550 intel_crtc->dspaddr_offset =
bc752862
CW
2551 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2552 fb->bits_per_pixel / 8,
2553 fb->pitches[0]);
c2c75131 2554 linear_offset -= intel_crtc->dspaddr_offset;
17638cd6 2555
f343c5f6
BW
2556 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2557 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2558 fb->pitches[0]);
01f2c773 2559 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2560 I915_WRITE(DSPSURF(plane),
2561 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2562 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2563 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2564 } else {
2565 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2566 I915_WRITE(DSPLINOFF(plane), linear_offset);
2567 }
17638cd6 2568 POSTING_READ(reg);
17638cd6
JB
2569}
2570
2571/* Assume fb object is pinned & idle & fenced and just update base pointers */
2572static int
2573intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2574 int x, int y, enum mode_set_atomic state)
2575{
2576 struct drm_device *dev = crtc->dev;
2577 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2578
6b8e6ed0
CW
2579 if (dev_priv->display.disable_fbc)
2580 dev_priv->display.disable_fbc(dev);
3dec0095 2581 intel_increase_pllclock(crtc);
81255565 2582
29b9bde6
DV
2583 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2584
2585 return 0;
81255565
JB
2586}
2587
96a02917
VS
2588void intel_display_handle_reset(struct drm_device *dev)
2589{
2590 struct drm_i915_private *dev_priv = dev->dev_private;
2591 struct drm_crtc *crtc;
2592
2593 /*
2594 * Flips in the rings have been nuked by the reset,
2595 * so complete all pending flips so that user space
2596 * will get its events and not get stuck.
2597 *
2598 * Also update the base address of all primary
2599 * planes to the the last fb to make sure we're
2600 * showing the correct fb after a reset.
2601 *
2602 * Need to make two loops over the crtcs so that we
2603 * don't try to grab a crtc mutex before the
2604 * pending_flip_queue really got woken up.
2605 */
2606
70e1e0ec 2607 for_each_crtc(dev, crtc) {
96a02917
VS
2608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2609 enum plane plane = intel_crtc->plane;
2610
2611 intel_prepare_page_flip(dev, plane);
2612 intel_finish_page_flip_plane(dev, plane);
2613 }
2614
70e1e0ec 2615 for_each_crtc(dev, crtc) {
96a02917
VS
2616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2617
51fd371b 2618 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2619 /*
2620 * FIXME: Once we have proper support for primary planes (and
2621 * disabling them without disabling the entire crtc) allow again
66e514c1 2622 * a NULL crtc->primary->fb.
947fdaad 2623 */
f4510a27 2624 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2625 dev_priv->display.update_primary_plane(crtc,
66e514c1 2626 crtc->primary->fb,
262ca2b0
MR
2627 crtc->x,
2628 crtc->y);
51fd371b 2629 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2630 }
2631}
2632
14667a4b
CW
2633static int
2634intel_finish_fb(struct drm_framebuffer *old_fb)
2635{
2636 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2637 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2638 bool was_interruptible = dev_priv->mm.interruptible;
2639 int ret;
2640
14667a4b
CW
2641 /* Big Hammer, we also need to ensure that any pending
2642 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2643 * current scanout is retired before unpinning the old
2644 * framebuffer.
2645 *
2646 * This should only fail upon a hung GPU, in which case we
2647 * can safely continue.
2648 */
2649 dev_priv->mm.interruptible = false;
2650 ret = i915_gem_object_finish_gpu(obj);
2651 dev_priv->mm.interruptible = was_interruptible;
2652
2653 return ret;
2654}
2655
7d5e3799
CW
2656static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2657{
2658 struct drm_device *dev = crtc->dev;
2659 struct drm_i915_private *dev_priv = dev->dev_private;
2660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2661 unsigned long flags;
2662 bool pending;
2663
2664 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2665 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2666 return false;
2667
2668 spin_lock_irqsave(&dev->event_lock, flags);
2669 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2670 spin_unlock_irqrestore(&dev->event_lock, flags);
2671
2672 return pending;
2673}
2674
5c3b82e2 2675static int
3c4fdcfb 2676intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2677 struct drm_framebuffer *fb)
79e53945
JB
2678{
2679 struct drm_device *dev = crtc->dev;
6b8e6ed0 2680 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
94352cf9 2682 struct drm_framebuffer *old_fb;
5c3b82e2 2683 int ret;
79e53945 2684
7d5e3799
CW
2685 if (intel_crtc_has_pending_flip(crtc)) {
2686 DRM_ERROR("pipe is still busy with an old pageflip\n");
2687 return -EBUSY;
2688 }
2689
79e53945 2690 /* no fb bound */
94352cf9 2691 if (!fb) {
a5071c2f 2692 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2693 return 0;
2694 }
2695
7eb552ae 2696 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2697 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2698 plane_name(intel_crtc->plane),
2699 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2700 return -EINVAL;
79e53945
JB
2701 }
2702
5c3b82e2 2703 mutex_lock(&dev->struct_mutex);
265db958 2704 ret = intel_pin_and_fence_fb_obj(dev,
94352cf9 2705 to_intel_framebuffer(fb)->obj,
919926ae 2706 NULL);
8ac36ec1 2707 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2708 if (ret != 0) {
a5071c2f 2709 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2710 return ret;
2711 }
79e53945 2712
bb2043de
DL
2713 /*
2714 * Update pipe size and adjust fitter if needed: the reason for this is
2715 * that in compute_mode_changes we check the native mode (not the pfit
2716 * mode) to see if we can flip rather than do a full mode set. In the
2717 * fastboot case, we'll flip, but if we don't update the pipesrc and
2718 * pfit state, we'll end up with a big fb scanned out into the wrong
2719 * sized surface.
2720 *
2721 * To fix this properly, we need to hoist the checks up into
2722 * compute_mode_changes (or above), check the actual pfit state and
2723 * whether the platform allows pfit disable with pipe active, and only
2724 * then update the pipesrc and pfit state, even on the flip path.
2725 */
d330a953 2726 if (i915.fastboot) {
d7bf63f2
DL
2727 const struct drm_display_mode *adjusted_mode =
2728 &intel_crtc->config.adjusted_mode;
2729
4d6a3e63 2730 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2731 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2732 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2733 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2734 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2735 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2736 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2737 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2738 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2739 }
0637d60d
JB
2740 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2741 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2742 }
2743
29b9bde6 2744 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2745
f4510a27
MR
2746 old_fb = crtc->primary->fb;
2747 crtc->primary->fb = fb;
6c4c86f5
DV
2748 crtc->x = x;
2749 crtc->y = y;
94352cf9 2750
b7f1de28 2751 if (old_fb) {
d7697eea
DV
2752 if (intel_crtc->active && old_fb != fb)
2753 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2754 mutex_lock(&dev->struct_mutex);
1690e1eb 2755 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
8ac36ec1 2756 mutex_unlock(&dev->struct_mutex);
b7f1de28 2757 }
652c393a 2758
8ac36ec1 2759 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2760 intel_update_fbc(dev);
4906557e 2761 intel_edp_psr_update(dev);
5c3b82e2 2762 mutex_unlock(&dev->struct_mutex);
79e53945 2763
5c3b82e2 2764 return 0;
79e53945
JB
2765}
2766
5e84e1a4
ZW
2767static void intel_fdi_normal_train(struct drm_crtc *crtc)
2768{
2769 struct drm_device *dev = crtc->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2772 int pipe = intel_crtc->pipe;
2773 u32 reg, temp;
2774
2775 /* enable normal train */
2776 reg = FDI_TX_CTL(pipe);
2777 temp = I915_READ(reg);
61e499bf 2778 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2779 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2780 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2781 } else {
2782 temp &= ~FDI_LINK_TRAIN_NONE;
2783 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2784 }
5e84e1a4
ZW
2785 I915_WRITE(reg, temp);
2786
2787 reg = FDI_RX_CTL(pipe);
2788 temp = I915_READ(reg);
2789 if (HAS_PCH_CPT(dev)) {
2790 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2791 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2792 } else {
2793 temp &= ~FDI_LINK_TRAIN_NONE;
2794 temp |= FDI_LINK_TRAIN_NONE;
2795 }
2796 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2797
2798 /* wait one idle pattern time */
2799 POSTING_READ(reg);
2800 udelay(1000);
357555c0
JB
2801
2802 /* IVB wants error correction enabled */
2803 if (IS_IVYBRIDGE(dev))
2804 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2805 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2806}
2807
1fbc0d78 2808static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2809{
1fbc0d78
DV
2810 return crtc->base.enabled && crtc->active &&
2811 crtc->config.has_pch_encoder;
1e833f40
DV
2812}
2813
01a415fd
DV
2814static void ivb_modeset_global_resources(struct drm_device *dev)
2815{
2816 struct drm_i915_private *dev_priv = dev->dev_private;
2817 struct intel_crtc *pipe_B_crtc =
2818 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2819 struct intel_crtc *pipe_C_crtc =
2820 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2821 uint32_t temp;
2822
1e833f40
DV
2823 /*
2824 * When everything is off disable fdi C so that we could enable fdi B
2825 * with all lanes. Note that we don't care about enabled pipes without
2826 * an enabled pch encoder.
2827 */
2828 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2829 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2830 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2831 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2832
2833 temp = I915_READ(SOUTH_CHICKEN1);
2834 temp &= ~FDI_BC_BIFURCATION_SELECT;
2835 DRM_DEBUG_KMS("disabling fdi C rx\n");
2836 I915_WRITE(SOUTH_CHICKEN1, temp);
2837 }
2838}
2839
8db9d77b
ZW
2840/* The FDI link training functions for ILK/Ibexpeak. */
2841static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2842{
2843 struct drm_device *dev = crtc->dev;
2844 struct drm_i915_private *dev_priv = dev->dev_private;
2845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2846 int pipe = intel_crtc->pipe;
5eddb70b 2847 u32 reg, temp, tries;
8db9d77b 2848
1c8562f6 2849 /* FDI needs bits from pipe first */
0fc932b8 2850 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2851
e1a44743
AJ
2852 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2853 for train result */
5eddb70b
CW
2854 reg = FDI_RX_IMR(pipe);
2855 temp = I915_READ(reg);
e1a44743
AJ
2856 temp &= ~FDI_RX_SYMBOL_LOCK;
2857 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2858 I915_WRITE(reg, temp);
2859 I915_READ(reg);
e1a44743
AJ
2860 udelay(150);
2861
8db9d77b 2862 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2863 reg = FDI_TX_CTL(pipe);
2864 temp = I915_READ(reg);
627eb5a3
DV
2865 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2866 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2867 temp &= ~FDI_LINK_TRAIN_NONE;
2868 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2869 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2870
5eddb70b
CW
2871 reg = FDI_RX_CTL(pipe);
2872 temp = I915_READ(reg);
8db9d77b
ZW
2873 temp &= ~FDI_LINK_TRAIN_NONE;
2874 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2875 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2876
2877 POSTING_READ(reg);
8db9d77b
ZW
2878 udelay(150);
2879
5b2adf89 2880 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2881 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2882 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2883 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2884
5eddb70b 2885 reg = FDI_RX_IIR(pipe);
e1a44743 2886 for (tries = 0; tries < 5; tries++) {
5eddb70b 2887 temp = I915_READ(reg);
8db9d77b
ZW
2888 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2889
2890 if ((temp & FDI_RX_BIT_LOCK)) {
2891 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2892 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2893 break;
2894 }
8db9d77b 2895 }
e1a44743 2896 if (tries == 5)
5eddb70b 2897 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2898
2899 /* Train 2 */
5eddb70b
CW
2900 reg = FDI_TX_CTL(pipe);
2901 temp = I915_READ(reg);
8db9d77b
ZW
2902 temp &= ~FDI_LINK_TRAIN_NONE;
2903 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2904 I915_WRITE(reg, temp);
8db9d77b 2905
5eddb70b
CW
2906 reg = FDI_RX_CTL(pipe);
2907 temp = I915_READ(reg);
8db9d77b
ZW
2908 temp &= ~FDI_LINK_TRAIN_NONE;
2909 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2910 I915_WRITE(reg, temp);
8db9d77b 2911
5eddb70b
CW
2912 POSTING_READ(reg);
2913 udelay(150);
8db9d77b 2914
5eddb70b 2915 reg = FDI_RX_IIR(pipe);
e1a44743 2916 for (tries = 0; tries < 5; tries++) {
5eddb70b 2917 temp = I915_READ(reg);
8db9d77b
ZW
2918 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2919
2920 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2921 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2922 DRM_DEBUG_KMS("FDI train 2 done.\n");
2923 break;
2924 }
8db9d77b 2925 }
e1a44743 2926 if (tries == 5)
5eddb70b 2927 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2928
2929 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2930
8db9d77b
ZW
2931}
2932
0206e353 2933static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2934 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2935 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2936 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2937 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2938};
2939
2940/* The FDI link training functions for SNB/Cougarpoint. */
2941static void gen6_fdi_link_train(struct drm_crtc *crtc)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
2945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2946 int pipe = intel_crtc->pipe;
fa37d39e 2947 u32 reg, temp, i, retry;
8db9d77b 2948
e1a44743
AJ
2949 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2950 for train result */
5eddb70b
CW
2951 reg = FDI_RX_IMR(pipe);
2952 temp = I915_READ(reg);
e1a44743
AJ
2953 temp &= ~FDI_RX_SYMBOL_LOCK;
2954 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2955 I915_WRITE(reg, temp);
2956
2957 POSTING_READ(reg);
e1a44743
AJ
2958 udelay(150);
2959
8db9d77b 2960 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2961 reg = FDI_TX_CTL(pipe);
2962 temp = I915_READ(reg);
627eb5a3
DV
2963 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2964 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2965 temp &= ~FDI_LINK_TRAIN_NONE;
2966 temp |= FDI_LINK_TRAIN_PATTERN_1;
2967 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2968 /* SNB-B */
2969 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 2970 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2971
d74cf324
DV
2972 I915_WRITE(FDI_RX_MISC(pipe),
2973 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2974
5eddb70b
CW
2975 reg = FDI_RX_CTL(pipe);
2976 temp = I915_READ(reg);
8db9d77b
ZW
2977 if (HAS_PCH_CPT(dev)) {
2978 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2979 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2980 } else {
2981 temp &= ~FDI_LINK_TRAIN_NONE;
2982 temp |= FDI_LINK_TRAIN_PATTERN_1;
2983 }
5eddb70b
CW
2984 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2985
2986 POSTING_READ(reg);
8db9d77b
ZW
2987 udelay(150);
2988
0206e353 2989 for (i = 0; i < 4; i++) {
5eddb70b
CW
2990 reg = FDI_TX_CTL(pipe);
2991 temp = I915_READ(reg);
8db9d77b
ZW
2992 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2993 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2994 I915_WRITE(reg, temp);
2995
2996 POSTING_READ(reg);
8db9d77b
ZW
2997 udelay(500);
2998
fa37d39e
SP
2999 for (retry = 0; retry < 5; retry++) {
3000 reg = FDI_RX_IIR(pipe);
3001 temp = I915_READ(reg);
3002 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3003 if (temp & FDI_RX_BIT_LOCK) {
3004 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3005 DRM_DEBUG_KMS("FDI train 1 done.\n");
3006 break;
3007 }
3008 udelay(50);
8db9d77b 3009 }
fa37d39e
SP
3010 if (retry < 5)
3011 break;
8db9d77b
ZW
3012 }
3013 if (i == 4)
5eddb70b 3014 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3015
3016 /* Train 2 */
5eddb70b
CW
3017 reg = FDI_TX_CTL(pipe);
3018 temp = I915_READ(reg);
8db9d77b
ZW
3019 temp &= ~FDI_LINK_TRAIN_NONE;
3020 temp |= FDI_LINK_TRAIN_PATTERN_2;
3021 if (IS_GEN6(dev)) {
3022 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3023 /* SNB-B */
3024 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3025 }
5eddb70b 3026 I915_WRITE(reg, temp);
8db9d77b 3027
5eddb70b
CW
3028 reg = FDI_RX_CTL(pipe);
3029 temp = I915_READ(reg);
8db9d77b
ZW
3030 if (HAS_PCH_CPT(dev)) {
3031 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3032 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3033 } else {
3034 temp &= ~FDI_LINK_TRAIN_NONE;
3035 temp |= FDI_LINK_TRAIN_PATTERN_2;
3036 }
5eddb70b
CW
3037 I915_WRITE(reg, temp);
3038
3039 POSTING_READ(reg);
8db9d77b
ZW
3040 udelay(150);
3041
0206e353 3042 for (i = 0; i < 4; i++) {
5eddb70b
CW
3043 reg = FDI_TX_CTL(pipe);
3044 temp = I915_READ(reg);
8db9d77b
ZW
3045 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3046 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3047 I915_WRITE(reg, temp);
3048
3049 POSTING_READ(reg);
8db9d77b
ZW
3050 udelay(500);
3051
fa37d39e
SP
3052 for (retry = 0; retry < 5; retry++) {
3053 reg = FDI_RX_IIR(pipe);
3054 temp = I915_READ(reg);
3055 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3056 if (temp & FDI_RX_SYMBOL_LOCK) {
3057 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3058 DRM_DEBUG_KMS("FDI train 2 done.\n");
3059 break;
3060 }
3061 udelay(50);
8db9d77b 3062 }
fa37d39e
SP
3063 if (retry < 5)
3064 break;
8db9d77b
ZW
3065 }
3066 if (i == 4)
5eddb70b 3067 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3068
3069 DRM_DEBUG_KMS("FDI train done.\n");
3070}
3071
357555c0
JB
3072/* Manual link training for Ivy Bridge A0 parts */
3073static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3074{
3075 struct drm_device *dev = crtc->dev;
3076 struct drm_i915_private *dev_priv = dev->dev_private;
3077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3078 int pipe = intel_crtc->pipe;
139ccd3f 3079 u32 reg, temp, i, j;
357555c0
JB
3080
3081 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3082 for train result */
3083 reg = FDI_RX_IMR(pipe);
3084 temp = I915_READ(reg);
3085 temp &= ~FDI_RX_SYMBOL_LOCK;
3086 temp &= ~FDI_RX_BIT_LOCK;
3087 I915_WRITE(reg, temp);
3088
3089 POSTING_READ(reg);
3090 udelay(150);
3091
01a415fd
DV
3092 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3093 I915_READ(FDI_RX_IIR(pipe)));
3094
139ccd3f
JB
3095 /* Try each vswing and preemphasis setting twice before moving on */
3096 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3097 /* disable first in case we need to retry */
3098 reg = FDI_TX_CTL(pipe);
3099 temp = I915_READ(reg);
3100 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3101 temp &= ~FDI_TX_ENABLE;
3102 I915_WRITE(reg, temp);
357555c0 3103
139ccd3f
JB
3104 reg = FDI_RX_CTL(pipe);
3105 temp = I915_READ(reg);
3106 temp &= ~FDI_LINK_TRAIN_AUTO;
3107 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3108 temp &= ~FDI_RX_ENABLE;
3109 I915_WRITE(reg, temp);
357555c0 3110
139ccd3f 3111 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3112 reg = FDI_TX_CTL(pipe);
3113 temp = I915_READ(reg);
139ccd3f
JB
3114 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3115 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3116 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3117 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3118 temp |= snb_b_fdi_train_param[j/2];
3119 temp |= FDI_COMPOSITE_SYNC;
3120 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3121
139ccd3f
JB
3122 I915_WRITE(FDI_RX_MISC(pipe),
3123 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3124
139ccd3f 3125 reg = FDI_RX_CTL(pipe);
357555c0 3126 temp = I915_READ(reg);
139ccd3f
JB
3127 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3128 temp |= FDI_COMPOSITE_SYNC;
3129 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3130
139ccd3f
JB
3131 POSTING_READ(reg);
3132 udelay(1); /* should be 0.5us */
357555c0 3133
139ccd3f
JB
3134 for (i = 0; i < 4; i++) {
3135 reg = FDI_RX_IIR(pipe);
3136 temp = I915_READ(reg);
3137 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3138
139ccd3f
JB
3139 if (temp & FDI_RX_BIT_LOCK ||
3140 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3141 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3142 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3143 i);
3144 break;
3145 }
3146 udelay(1); /* should be 0.5us */
3147 }
3148 if (i == 4) {
3149 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3150 continue;
3151 }
357555c0 3152
139ccd3f 3153 /* Train 2 */
357555c0
JB
3154 reg = FDI_TX_CTL(pipe);
3155 temp = I915_READ(reg);
139ccd3f
JB
3156 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3157 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3158 I915_WRITE(reg, temp);
3159
3160 reg = FDI_RX_CTL(pipe);
3161 temp = I915_READ(reg);
3162 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3163 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3164 I915_WRITE(reg, temp);
3165
3166 POSTING_READ(reg);
139ccd3f 3167 udelay(2); /* should be 1.5us */
357555c0 3168
139ccd3f
JB
3169 for (i = 0; i < 4; i++) {
3170 reg = FDI_RX_IIR(pipe);
3171 temp = I915_READ(reg);
3172 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3173
139ccd3f
JB
3174 if (temp & FDI_RX_SYMBOL_LOCK ||
3175 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3176 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3177 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3178 i);
3179 goto train_done;
3180 }
3181 udelay(2); /* should be 1.5us */
357555c0 3182 }
139ccd3f
JB
3183 if (i == 4)
3184 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3185 }
357555c0 3186
139ccd3f 3187train_done:
357555c0
JB
3188 DRM_DEBUG_KMS("FDI train done.\n");
3189}
3190
88cefb6c 3191static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3192{
88cefb6c 3193 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3194 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3195 int pipe = intel_crtc->pipe;
5eddb70b 3196 u32 reg, temp;
79e53945 3197
c64e311e 3198
c98e9dcf 3199 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3200 reg = FDI_RX_CTL(pipe);
3201 temp = I915_READ(reg);
627eb5a3
DV
3202 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3203 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3204 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3205 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3206
3207 POSTING_READ(reg);
c98e9dcf
JB
3208 udelay(200);
3209
3210 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3211 temp = I915_READ(reg);
3212 I915_WRITE(reg, temp | FDI_PCDCLK);
3213
3214 POSTING_READ(reg);
c98e9dcf
JB
3215 udelay(200);
3216
20749730
PZ
3217 /* Enable CPU FDI TX PLL, always on for Ironlake */
3218 reg = FDI_TX_CTL(pipe);
3219 temp = I915_READ(reg);
3220 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3221 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3222
20749730
PZ
3223 POSTING_READ(reg);
3224 udelay(100);
6be4a607 3225 }
0e23b99d
JB
3226}
3227
88cefb6c
DV
3228static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3229{
3230 struct drm_device *dev = intel_crtc->base.dev;
3231 struct drm_i915_private *dev_priv = dev->dev_private;
3232 int pipe = intel_crtc->pipe;
3233 u32 reg, temp;
3234
3235 /* Switch from PCDclk to Rawclk */
3236 reg = FDI_RX_CTL(pipe);
3237 temp = I915_READ(reg);
3238 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3239
3240 /* Disable CPU FDI TX PLL */
3241 reg = FDI_TX_CTL(pipe);
3242 temp = I915_READ(reg);
3243 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3244
3245 POSTING_READ(reg);
3246 udelay(100);
3247
3248 reg = FDI_RX_CTL(pipe);
3249 temp = I915_READ(reg);
3250 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3251
3252 /* Wait for the clocks to turn off. */
3253 POSTING_READ(reg);
3254 udelay(100);
3255}
3256
0fc932b8
JB
3257static void ironlake_fdi_disable(struct drm_crtc *crtc)
3258{
3259 struct drm_device *dev = crtc->dev;
3260 struct drm_i915_private *dev_priv = dev->dev_private;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3262 int pipe = intel_crtc->pipe;
3263 u32 reg, temp;
3264
3265 /* disable CPU FDI tx and PCH FDI rx */
3266 reg = FDI_TX_CTL(pipe);
3267 temp = I915_READ(reg);
3268 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3269 POSTING_READ(reg);
3270
3271 reg = FDI_RX_CTL(pipe);
3272 temp = I915_READ(reg);
3273 temp &= ~(0x7 << 16);
dfd07d72 3274 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3275 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3276
3277 POSTING_READ(reg);
3278 udelay(100);
3279
3280 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3281 if (HAS_PCH_IBX(dev))
6f06ce18 3282 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3283
3284 /* still set train pattern 1 */
3285 reg = FDI_TX_CTL(pipe);
3286 temp = I915_READ(reg);
3287 temp &= ~FDI_LINK_TRAIN_NONE;
3288 temp |= FDI_LINK_TRAIN_PATTERN_1;
3289 I915_WRITE(reg, temp);
3290
3291 reg = FDI_RX_CTL(pipe);
3292 temp = I915_READ(reg);
3293 if (HAS_PCH_CPT(dev)) {
3294 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3295 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3296 } else {
3297 temp &= ~FDI_LINK_TRAIN_NONE;
3298 temp |= FDI_LINK_TRAIN_PATTERN_1;
3299 }
3300 /* BPC in FDI rx is consistent with that in PIPECONF */
3301 temp &= ~(0x07 << 16);
dfd07d72 3302 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3303 I915_WRITE(reg, temp);
3304
3305 POSTING_READ(reg);
3306 udelay(100);
3307}
3308
5dce5b93
CW
3309bool intel_has_pending_fb_unpin(struct drm_device *dev)
3310{
3311 struct intel_crtc *crtc;
3312
3313 /* Note that we don't need to be called with mode_config.lock here
3314 * as our list of CRTC objects is static for the lifetime of the
3315 * device and so cannot disappear as we iterate. Similarly, we can
3316 * happily treat the predicates as racy, atomic checks as userspace
3317 * cannot claim and pin a new fb without at least acquring the
3318 * struct_mutex and so serialising with us.
3319 */
d3fcc808 3320 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3321 if (atomic_read(&crtc->unpin_work_count) == 0)
3322 continue;
3323
3324 if (crtc->unpin_work)
3325 intel_wait_for_vblank(dev, crtc->pipe);
3326
3327 return true;
3328 }
3329
3330 return false;
3331}
3332
46a55d30 3333void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3334{
0f91128d 3335 struct drm_device *dev = crtc->dev;
5bb61643 3336 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3337
f4510a27 3338 if (crtc->primary->fb == NULL)
e6c3a2a6
CW
3339 return;
3340
2c10d571
DV
3341 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3342
eed6d67d
DV
3343 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3344 !intel_crtc_has_pending_flip(crtc),
3345 60*HZ) == 0);
5bb61643 3346
0f91128d 3347 mutex_lock(&dev->struct_mutex);
f4510a27 3348 intel_finish_fb(crtc->primary->fb);
0f91128d 3349 mutex_unlock(&dev->struct_mutex);
e6c3a2a6
CW
3350}
3351
e615efe4
ED
3352/* Program iCLKIP clock to the desired frequency */
3353static void lpt_program_iclkip(struct drm_crtc *crtc)
3354{
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3357 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3358 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3359 u32 temp;
3360
09153000
DV
3361 mutex_lock(&dev_priv->dpio_lock);
3362
e615efe4
ED
3363 /* It is necessary to ungate the pixclk gate prior to programming
3364 * the divisors, and gate it back when it is done.
3365 */
3366 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3367
3368 /* Disable SSCCTL */
3369 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3370 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3371 SBI_SSCCTL_DISABLE,
3372 SBI_ICLK);
e615efe4
ED
3373
3374 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3375 if (clock == 20000) {
e615efe4
ED
3376 auxdiv = 1;
3377 divsel = 0x41;
3378 phaseinc = 0x20;
3379 } else {
3380 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3381 * but the adjusted_mode->crtc_clock in in KHz. To get the
3382 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3383 * convert the virtual clock precision to KHz here for higher
3384 * precision.
3385 */
3386 u32 iclk_virtual_root_freq = 172800 * 1000;
3387 u32 iclk_pi_range = 64;
3388 u32 desired_divisor, msb_divisor_value, pi_value;
3389
12d7ceed 3390 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3391 msb_divisor_value = desired_divisor / iclk_pi_range;
3392 pi_value = desired_divisor % iclk_pi_range;
3393
3394 auxdiv = 0;
3395 divsel = msb_divisor_value - 2;
3396 phaseinc = pi_value;
3397 }
3398
3399 /* This should not happen with any sane values */
3400 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3401 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3402 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3403 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3404
3405 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3406 clock,
e615efe4
ED
3407 auxdiv,
3408 divsel,
3409 phasedir,
3410 phaseinc);
3411
3412 /* Program SSCDIVINTPHASE6 */
988d6ee8 3413 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3414 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3415 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3416 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3417 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3418 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3419 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3420 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3421
3422 /* Program SSCAUXDIV */
988d6ee8 3423 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3424 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3425 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3426 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3427
3428 /* Enable modulator and associated divider */
988d6ee8 3429 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3430 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3431 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3432
3433 /* Wait for initialization time */
3434 udelay(24);
3435
3436 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3437
3438 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3439}
3440
275f01b2
DV
3441static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3442 enum pipe pch_transcoder)
3443{
3444 struct drm_device *dev = crtc->base.dev;
3445 struct drm_i915_private *dev_priv = dev->dev_private;
3446 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3447
3448 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3449 I915_READ(HTOTAL(cpu_transcoder)));
3450 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3451 I915_READ(HBLANK(cpu_transcoder)));
3452 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3453 I915_READ(HSYNC(cpu_transcoder)));
3454
3455 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3456 I915_READ(VTOTAL(cpu_transcoder)));
3457 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3458 I915_READ(VBLANK(cpu_transcoder)));
3459 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3460 I915_READ(VSYNC(cpu_transcoder)));
3461 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3462 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3463}
3464
1fbc0d78
DV
3465static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3466{
3467 struct drm_i915_private *dev_priv = dev->dev_private;
3468 uint32_t temp;
3469
3470 temp = I915_READ(SOUTH_CHICKEN1);
3471 if (temp & FDI_BC_BIFURCATION_SELECT)
3472 return;
3473
3474 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3475 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3476
3477 temp |= FDI_BC_BIFURCATION_SELECT;
3478 DRM_DEBUG_KMS("enabling fdi C rx\n");
3479 I915_WRITE(SOUTH_CHICKEN1, temp);
3480 POSTING_READ(SOUTH_CHICKEN1);
3481}
3482
3483static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3484{
3485 struct drm_device *dev = intel_crtc->base.dev;
3486 struct drm_i915_private *dev_priv = dev->dev_private;
3487
3488 switch (intel_crtc->pipe) {
3489 case PIPE_A:
3490 break;
3491 case PIPE_B:
3492 if (intel_crtc->config.fdi_lanes > 2)
3493 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3494 else
3495 cpt_enable_fdi_bc_bifurcation(dev);
3496
3497 break;
3498 case PIPE_C:
3499 cpt_enable_fdi_bc_bifurcation(dev);
3500
3501 break;
3502 default:
3503 BUG();
3504 }
3505}
3506
f67a559d
JB
3507/*
3508 * Enable PCH resources required for PCH ports:
3509 * - PCH PLLs
3510 * - FDI training & RX/TX
3511 * - update transcoder timings
3512 * - DP transcoding bits
3513 * - transcoder
3514 */
3515static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3516{
3517 struct drm_device *dev = crtc->dev;
3518 struct drm_i915_private *dev_priv = dev->dev_private;
3519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3520 int pipe = intel_crtc->pipe;
ee7b9f93 3521 u32 reg, temp;
2c07245f 3522
ab9412ba 3523 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3524
1fbc0d78
DV
3525 if (IS_IVYBRIDGE(dev))
3526 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3527
cd986abb
DV
3528 /* Write the TU size bits before fdi link training, so that error
3529 * detection works. */
3530 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3531 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3532
c98e9dcf 3533 /* For PCH output, training FDI link */
674cf967 3534 dev_priv->display.fdi_link_train(crtc);
2c07245f 3535
3ad8a208
DV
3536 /* We need to program the right clock selection before writing the pixel
3537 * mutliplier into the DPLL. */
303b81e0 3538 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3539 u32 sel;
4b645f14 3540
c98e9dcf 3541 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3542 temp |= TRANS_DPLL_ENABLE(pipe);
3543 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3544 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3545 temp |= sel;
3546 else
3547 temp &= ~sel;
c98e9dcf 3548 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3549 }
5eddb70b 3550
3ad8a208
DV
3551 /* XXX: pch pll's can be enabled any time before we enable the PCH
3552 * transcoder, and we actually should do this to not upset any PCH
3553 * transcoder that already use the clock when we share it.
3554 *
3555 * Note that enable_shared_dpll tries to do the right thing, but
3556 * get_shared_dpll unconditionally resets the pll - we need that to have
3557 * the right LVDS enable sequence. */
85b3894f 3558 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3559
d9b6cb56
JB
3560 /* set transcoder timing, panel must allow it */
3561 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3562 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3563
303b81e0 3564 intel_fdi_normal_train(crtc);
5e84e1a4 3565
c98e9dcf
JB
3566 /* For PCH DP, enable TRANS_DP_CTL */
3567 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3568 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3569 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3570 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3571 reg = TRANS_DP_CTL(pipe);
3572 temp = I915_READ(reg);
3573 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3574 TRANS_DP_SYNC_MASK |
3575 TRANS_DP_BPC_MASK);
5eddb70b
CW
3576 temp |= (TRANS_DP_OUTPUT_ENABLE |
3577 TRANS_DP_ENH_FRAMING);
9325c9f0 3578 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3579
3580 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3581 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3582 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3583 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3584
3585 switch (intel_trans_dp_port_sel(crtc)) {
3586 case PCH_DP_B:
5eddb70b 3587 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3588 break;
3589 case PCH_DP_C:
5eddb70b 3590 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3591 break;
3592 case PCH_DP_D:
5eddb70b 3593 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3594 break;
3595 default:
e95d41e1 3596 BUG();
32f9d658 3597 }
2c07245f 3598
5eddb70b 3599 I915_WRITE(reg, temp);
6be4a607 3600 }
b52eb4dc 3601
b8a4f404 3602 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3603}
3604
1507e5bd
PZ
3605static void lpt_pch_enable(struct drm_crtc *crtc)
3606{
3607 struct drm_device *dev = crtc->dev;
3608 struct drm_i915_private *dev_priv = dev->dev_private;
3609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3610 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3611
ab9412ba 3612 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3613
8c52b5e8 3614 lpt_program_iclkip(crtc);
1507e5bd 3615
0540e488 3616 /* Set transcoder timing. */
275f01b2 3617 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3618
937bb610 3619 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3620}
3621
e2b78267 3622static void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3623{
e2b78267 3624 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3625
3626 if (pll == NULL)
3627 return;
3628
3629 if (pll->refcount == 0) {
46edb027 3630 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3631 return;
3632 }
3633
f4a091c7
DV
3634 if (--pll->refcount == 0) {
3635 WARN_ON(pll->on);
3636 WARN_ON(pll->active);
3637 }
3638
a43f6e0f 3639 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3640}
3641
b89a1d39 3642static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3643{
e2b78267
DV
3644 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3645 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3646 enum intel_dpll_id i;
ee7b9f93 3647
ee7b9f93 3648 if (pll) {
46edb027
DV
3649 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3650 crtc->base.base.id, pll->name);
e2b78267 3651 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3652 }
3653
98b6bd99
DV
3654 if (HAS_PCH_IBX(dev_priv->dev)) {
3655 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3656 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3657 pll = &dev_priv->shared_dplls[i];
98b6bd99 3658
46edb027
DV
3659 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3660 crtc->base.base.id, pll->name);
98b6bd99 3661
f2a69f44
DV
3662 WARN_ON(pll->refcount);
3663
98b6bd99
DV
3664 goto found;
3665 }
3666
e72f9fbf
DV
3667 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3668 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3669
3670 /* Only want to check enabled timings first */
3671 if (pll->refcount == 0)
3672 continue;
3673
b89a1d39
DV
3674 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3675 sizeof(pll->hw_state)) == 0) {
46edb027 3676 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3677 crtc->base.base.id,
46edb027 3678 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3679
3680 goto found;
3681 }
3682 }
3683
3684 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3685 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3686 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3687 if (pll->refcount == 0) {
46edb027
DV
3688 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3689 crtc->base.base.id, pll->name);
ee7b9f93
JB
3690 goto found;
3691 }
3692 }
3693
3694 return NULL;
3695
3696found:
f2a69f44
DV
3697 if (pll->refcount == 0)
3698 pll->hw_state = crtc->config.dpll_hw_state;
3699
a43f6e0f 3700 crtc->config.shared_dpll = i;
46edb027
DV
3701 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3702 pipe_name(crtc->pipe));
ee7b9f93 3703
cdbd2316 3704 pll->refcount++;
e04c7350 3705
ee7b9f93
JB
3706 return pll;
3707}
3708
a1520318 3709static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3710{
3711 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3712 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3713 u32 temp;
3714
3715 temp = I915_READ(dslreg);
3716 udelay(500);
3717 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3718 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3719 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3720 }
3721}
3722
b074cec8
JB
3723static void ironlake_pfit_enable(struct intel_crtc *crtc)
3724{
3725 struct drm_device *dev = crtc->base.dev;
3726 struct drm_i915_private *dev_priv = dev->dev_private;
3727 int pipe = crtc->pipe;
3728
fd4daa9c 3729 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3730 /* Force use of hard-coded filter coefficients
3731 * as some pre-programmed values are broken,
3732 * e.g. x201.
3733 */
3734 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3735 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3736 PF_PIPE_SEL_IVB(pipe));
3737 else
3738 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3739 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3740 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3741 }
3742}
3743
bb53d4ae
VS
3744static void intel_enable_planes(struct drm_crtc *crtc)
3745{
3746 struct drm_device *dev = crtc->dev;
3747 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3748 struct drm_plane *plane;
bb53d4ae
VS
3749 struct intel_plane *intel_plane;
3750
af2b653b
MR
3751 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3752 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3753 if (intel_plane->pipe == pipe)
3754 intel_plane_restore(&intel_plane->base);
af2b653b 3755 }
bb53d4ae
VS
3756}
3757
3758static void intel_disable_planes(struct drm_crtc *crtc)
3759{
3760 struct drm_device *dev = crtc->dev;
3761 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3762 struct drm_plane *plane;
bb53d4ae
VS
3763 struct intel_plane *intel_plane;
3764
af2b653b
MR
3765 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3766 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3767 if (intel_plane->pipe == pipe)
3768 intel_plane_disable(&intel_plane->base);
af2b653b 3769 }
bb53d4ae
VS
3770}
3771
20bc8673 3772void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3773{
cea165c3
VS
3774 struct drm_device *dev = crtc->base.dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3776
3777 if (!crtc->config.ips_enabled)
3778 return;
3779
cea165c3
VS
3780 /* We can only enable IPS after we enable a plane and wait for a vblank */
3781 intel_wait_for_vblank(dev, crtc->pipe);
3782
d77e4531 3783 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3784 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3785 mutex_lock(&dev_priv->rps.hw_lock);
3786 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3787 mutex_unlock(&dev_priv->rps.hw_lock);
3788 /* Quoting Art Runyan: "its not safe to expect any particular
3789 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3790 * mailbox." Moreover, the mailbox may return a bogus state,
3791 * so we need to just enable it and continue on.
2a114cc1
BW
3792 */
3793 } else {
3794 I915_WRITE(IPS_CTL, IPS_ENABLE);
3795 /* The bit only becomes 1 in the next vblank, so this wait here
3796 * is essentially intel_wait_for_vblank. If we don't have this
3797 * and don't wait for vblanks until the end of crtc_enable, then
3798 * the HW state readout code will complain that the expected
3799 * IPS_CTL value is not the one we read. */
3800 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3801 DRM_ERROR("Timed out waiting for IPS enable\n");
3802 }
d77e4531
PZ
3803}
3804
20bc8673 3805void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3806{
3807 struct drm_device *dev = crtc->base.dev;
3808 struct drm_i915_private *dev_priv = dev->dev_private;
3809
3810 if (!crtc->config.ips_enabled)
3811 return;
3812
3813 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3814 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3815 mutex_lock(&dev_priv->rps.hw_lock);
3816 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3817 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3818 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3819 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3820 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3821 } else {
2a114cc1 3822 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3823 POSTING_READ(IPS_CTL);
3824 }
d77e4531
PZ
3825
3826 /* We need to wait for a vblank before we can disable the plane. */
3827 intel_wait_for_vblank(dev, crtc->pipe);
3828}
3829
3830/** Loads the palette/gamma unit for the CRTC with the prepared values */
3831static void intel_crtc_load_lut(struct drm_crtc *crtc)
3832{
3833 struct drm_device *dev = crtc->dev;
3834 struct drm_i915_private *dev_priv = dev->dev_private;
3835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3836 enum pipe pipe = intel_crtc->pipe;
3837 int palreg = PALETTE(pipe);
3838 int i;
3839 bool reenable_ips = false;
3840
3841 /* The clocks have to be on to load the palette. */
3842 if (!crtc->enabled || !intel_crtc->active)
3843 return;
3844
3845 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3846 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3847 assert_dsi_pll_enabled(dev_priv);
3848 else
3849 assert_pll_enabled(dev_priv, pipe);
3850 }
3851
3852 /* use legacy palette for Ironlake */
3853 if (HAS_PCH_SPLIT(dev))
3854 palreg = LGC_PALETTE(pipe);
3855
3856 /* Workaround : Do not read or write the pipe palette/gamma data while
3857 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3858 */
41e6fc4c 3859 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3860 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3861 GAMMA_MODE_MODE_SPLIT)) {
3862 hsw_disable_ips(intel_crtc);
3863 reenable_ips = true;
3864 }
3865
3866 for (i = 0; i < 256; i++) {
3867 I915_WRITE(palreg + 4 * i,
3868 (intel_crtc->lut_r[i] << 16) |
3869 (intel_crtc->lut_g[i] << 8) |
3870 intel_crtc->lut_b[i]);
3871 }
3872
3873 if (reenable_ips)
3874 hsw_enable_ips(intel_crtc);
3875}
3876
d3eedb1a
VS
3877static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3878{
3879 if (!enable && intel_crtc->overlay) {
3880 struct drm_device *dev = intel_crtc->base.dev;
3881 struct drm_i915_private *dev_priv = dev->dev_private;
3882
3883 mutex_lock(&dev->struct_mutex);
3884 dev_priv->mm.interruptible = false;
3885 (void) intel_overlay_switch_off(intel_crtc->overlay);
3886 dev_priv->mm.interruptible = true;
3887 mutex_unlock(&dev->struct_mutex);
3888 }
3889
3890 /* Let userspace switch the overlay on again. In most cases userspace
3891 * has to recompute where to put it anyway.
3892 */
3893}
3894
3895/**
3896 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3897 * cursor plane briefly if not already running after enabling the display
3898 * plane.
3899 * This workaround avoids occasional blank screens when self refresh is
3900 * enabled.
3901 */
3902static void
3903g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3904{
3905 u32 cntl = I915_READ(CURCNTR(pipe));
3906
3907 if ((cntl & CURSOR_MODE) == 0) {
3908 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3909
3910 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3911 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3912 intel_wait_for_vblank(dev_priv->dev, pipe);
3913 I915_WRITE(CURCNTR(pipe), cntl);
3914 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3915 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3916 }
3917}
3918
3919static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3920{
3921 struct drm_device *dev = crtc->dev;
3922 struct drm_i915_private *dev_priv = dev->dev_private;
3923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3924 int pipe = intel_crtc->pipe;
3925 int plane = intel_crtc->plane;
3926
f98551ae
VS
3927 drm_vblank_on(dev, pipe);
3928
a5c4d7bc
VS
3929 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3930 intel_enable_planes(crtc);
d3eedb1a
VS
3931 /* The fixup needs to happen before cursor is enabled */
3932 if (IS_G4X(dev))
3933 g4x_fixup_plane(dev_priv, pipe);
a5c4d7bc 3934 intel_crtc_update_cursor(crtc, true);
d3eedb1a 3935 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
3936
3937 hsw_enable_ips(intel_crtc);
3938
3939 mutex_lock(&dev->struct_mutex);
3940 intel_update_fbc(dev);
71b1c373 3941 intel_edp_psr_update(dev);
a5c4d7bc
VS
3942 mutex_unlock(&dev->struct_mutex);
3943}
3944
d3eedb1a 3945static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3946{
3947 struct drm_device *dev = crtc->dev;
3948 struct drm_i915_private *dev_priv = dev->dev_private;
3949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3950 int pipe = intel_crtc->pipe;
3951 int plane = intel_crtc->plane;
3952
3953 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
3954
3955 if (dev_priv->fbc.plane == plane)
3956 intel_disable_fbc(dev);
3957
3958 hsw_disable_ips(intel_crtc);
3959
d3eedb1a 3960 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
3961 intel_crtc_update_cursor(crtc, false);
3962 intel_disable_planes(crtc);
3963 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
f98551ae
VS
3964
3965 drm_vblank_off(dev, pipe);
a5c4d7bc
VS
3966}
3967
f67a559d
JB
3968static void ironlake_crtc_enable(struct drm_crtc *crtc)
3969{
3970 struct drm_device *dev = crtc->dev;
3971 struct drm_i915_private *dev_priv = dev->dev_private;
3972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3973 struct intel_encoder *encoder;
f67a559d 3974 int pipe = intel_crtc->pipe;
29407aab 3975 enum plane plane = intel_crtc->plane;
f67a559d 3976
08a48469
DV
3977 WARN_ON(!crtc->enabled);
3978
f67a559d
JB
3979 if (intel_crtc->active)
3980 return;
3981
b14b1055
DV
3982 if (intel_crtc->config.has_pch_encoder)
3983 intel_prepare_shared_dpll(intel_crtc);
3984
29407aab
DV
3985 if (intel_crtc->config.has_dp_encoder)
3986 intel_dp_set_m_n(intel_crtc);
3987
3988 intel_set_pipe_timings(intel_crtc);
3989
3990 if (intel_crtc->config.has_pch_encoder) {
3991 intel_cpu_transcoder_set_m_n(intel_crtc,
3992 &intel_crtc->config.fdi_m_n);
3993 }
3994
3995 ironlake_set_pipeconf(crtc);
3996
3997 /* Set up the display plane register */
3998 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
3999 POSTING_READ(DSPCNTR(plane));
4000
4001 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4002 crtc->x, crtc->y);
4003
f67a559d 4004 intel_crtc->active = true;
8664281b
PZ
4005
4006 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4007 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4008
f6736a1a 4009 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4010 if (encoder->pre_enable)
4011 encoder->pre_enable(encoder);
f67a559d 4012
5bfe2ac0 4013 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4014 /* Note: FDI PLL enabling _must_ be done before we enable the
4015 * cpu pipes, hence this is separate from all the other fdi/pch
4016 * enabling. */
88cefb6c 4017 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4018 } else {
4019 assert_fdi_tx_disabled(dev_priv, pipe);
4020 assert_fdi_rx_disabled(dev_priv, pipe);
4021 }
f67a559d 4022
b074cec8 4023 ironlake_pfit_enable(intel_crtc);
f67a559d 4024
9c54c0dd
JB
4025 /*
4026 * On ILK+ LUT must be loaded before the pipe is running but with
4027 * clocks enabled
4028 */
4029 intel_crtc_load_lut(crtc);
4030
f37fcc2a 4031 intel_update_watermarks(crtc);
e1fdc473 4032 intel_enable_pipe(intel_crtc);
f67a559d 4033
5bfe2ac0 4034 if (intel_crtc->config.has_pch_encoder)
f67a559d 4035 ironlake_pch_enable(crtc);
c98e9dcf 4036
fa5c73b1
DV
4037 for_each_encoder_on_crtc(dev, crtc, encoder)
4038 encoder->enable(encoder);
61b77ddd
DV
4039
4040 if (HAS_PCH_CPT(dev))
a1520318 4041 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4042
d3eedb1a 4043 intel_crtc_enable_planes(crtc);
6be4a607
JB
4044}
4045
42db64ef
PZ
4046/* IPS only exists on ULT machines and is tied to pipe A. */
4047static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4048{
f5adf94e 4049 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4050}
4051
e4916946
PZ
4052/*
4053 * This implements the workaround described in the "notes" section of the mode
4054 * set sequence documentation. When going from no pipes or single pipe to
4055 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4056 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4057 */
4058static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4059{
4060 struct drm_device *dev = crtc->base.dev;
4061 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4062
4063 /* We want to get the other_active_crtc only if there's only 1 other
4064 * active crtc. */
d3fcc808 4065 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4066 if (!crtc_it->active || crtc_it == crtc)
4067 continue;
4068
4069 if (other_active_crtc)
4070 return;
4071
4072 other_active_crtc = crtc_it;
4073 }
4074 if (!other_active_crtc)
4075 return;
4076
4077 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4078 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4079}
4080
4f771f10
PZ
4081static void haswell_crtc_enable(struct drm_crtc *crtc)
4082{
4083 struct drm_device *dev = crtc->dev;
4084 struct drm_i915_private *dev_priv = dev->dev_private;
4085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4086 struct intel_encoder *encoder;
4087 int pipe = intel_crtc->pipe;
229fca97 4088 enum plane plane = intel_crtc->plane;
4f771f10
PZ
4089
4090 WARN_ON(!crtc->enabled);
4091
4092 if (intel_crtc->active)
4093 return;
4094
229fca97
DV
4095 if (intel_crtc->config.has_dp_encoder)
4096 intel_dp_set_m_n(intel_crtc);
4097
4098 intel_set_pipe_timings(intel_crtc);
4099
4100 if (intel_crtc->config.has_pch_encoder) {
4101 intel_cpu_transcoder_set_m_n(intel_crtc,
4102 &intel_crtc->config.fdi_m_n);
4103 }
4104
4105 haswell_set_pipeconf(crtc);
4106
4107 intel_set_pipe_csc(crtc);
4108
4109 /* Set up the display plane register */
4110 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4111 POSTING_READ(DSPCNTR(plane));
4112
4113 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4114 crtc->x, crtc->y);
4115
4f771f10 4116 intel_crtc->active = true;
8664281b
PZ
4117
4118 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4119 if (intel_crtc->config.has_pch_encoder)
4120 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4121
5bfe2ac0 4122 if (intel_crtc->config.has_pch_encoder)
04945641 4123 dev_priv->display.fdi_link_train(crtc);
4f771f10
PZ
4124
4125 for_each_encoder_on_crtc(dev, crtc, encoder)
4126 if (encoder->pre_enable)
4127 encoder->pre_enable(encoder);
4128
1f544388 4129 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4130
b074cec8 4131 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4132
4133 /*
4134 * On ILK+ LUT must be loaded before the pipe is running but with
4135 * clocks enabled
4136 */
4137 intel_crtc_load_lut(crtc);
4138
1f544388 4139 intel_ddi_set_pipe_settings(crtc);
8228c251 4140 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4141
f37fcc2a 4142 intel_update_watermarks(crtc);
e1fdc473 4143 intel_enable_pipe(intel_crtc);
42db64ef 4144
5bfe2ac0 4145 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4146 lpt_pch_enable(crtc);
4f771f10 4147
8807e55b 4148 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4149 encoder->enable(encoder);
8807e55b
JN
4150 intel_opregion_notify_encoder(encoder, true);
4151 }
4f771f10 4152
e4916946
PZ
4153 /* If we change the relative order between pipe/planes enabling, we need
4154 * to change the workaround. */
4155 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4156 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4157}
4158
3f8dce3a
DV
4159static void ironlake_pfit_disable(struct intel_crtc *crtc)
4160{
4161 struct drm_device *dev = crtc->base.dev;
4162 struct drm_i915_private *dev_priv = dev->dev_private;
4163 int pipe = crtc->pipe;
4164
4165 /* To avoid upsetting the power well on haswell only disable the pfit if
4166 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4167 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4168 I915_WRITE(PF_CTL(pipe), 0);
4169 I915_WRITE(PF_WIN_POS(pipe), 0);
4170 I915_WRITE(PF_WIN_SZ(pipe), 0);
4171 }
4172}
4173
6be4a607
JB
4174static void ironlake_crtc_disable(struct drm_crtc *crtc)
4175{
4176 struct drm_device *dev = crtc->dev;
4177 struct drm_i915_private *dev_priv = dev->dev_private;
4178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4179 struct intel_encoder *encoder;
6be4a607 4180 int pipe = intel_crtc->pipe;
5eddb70b 4181 u32 reg, temp;
b52eb4dc 4182
f7abfe8b
CW
4183 if (!intel_crtc->active)
4184 return;
4185
d3eedb1a 4186 intel_crtc_disable_planes(crtc);
a5c4d7bc 4187
ea9d758d
DV
4188 for_each_encoder_on_crtc(dev, crtc, encoder)
4189 encoder->disable(encoder);
4190
d925c59a
DV
4191 if (intel_crtc->config.has_pch_encoder)
4192 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4193
b24e7179 4194 intel_disable_pipe(dev_priv, pipe);
32f9d658 4195
3f8dce3a 4196 ironlake_pfit_disable(intel_crtc);
2c07245f 4197
bf49ec8c
DV
4198 for_each_encoder_on_crtc(dev, crtc, encoder)
4199 if (encoder->post_disable)
4200 encoder->post_disable(encoder);
2c07245f 4201
d925c59a
DV
4202 if (intel_crtc->config.has_pch_encoder) {
4203 ironlake_fdi_disable(crtc);
913d8d11 4204
d925c59a
DV
4205 ironlake_disable_pch_transcoder(dev_priv, pipe);
4206 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4207
d925c59a
DV
4208 if (HAS_PCH_CPT(dev)) {
4209 /* disable TRANS_DP_CTL */
4210 reg = TRANS_DP_CTL(pipe);
4211 temp = I915_READ(reg);
4212 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4213 TRANS_DP_PORT_SEL_MASK);
4214 temp |= TRANS_DP_PORT_SEL_NONE;
4215 I915_WRITE(reg, temp);
4216
4217 /* disable DPLL_SEL */
4218 temp = I915_READ(PCH_DPLL_SEL);
11887397 4219 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4220 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4221 }
e3421a18 4222
d925c59a 4223 /* disable PCH DPLL */
e72f9fbf 4224 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4225
d925c59a
DV
4226 ironlake_fdi_pll_disable(intel_crtc);
4227 }
6b383a7f 4228
f7abfe8b 4229 intel_crtc->active = false;
46ba614c 4230 intel_update_watermarks(crtc);
d1ebd816
BW
4231
4232 mutex_lock(&dev->struct_mutex);
6b383a7f 4233 intel_update_fbc(dev);
71b1c373 4234 intel_edp_psr_update(dev);
d1ebd816 4235 mutex_unlock(&dev->struct_mutex);
6be4a607 4236}
1b3c7a47 4237
4f771f10 4238static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4239{
4f771f10
PZ
4240 struct drm_device *dev = crtc->dev;
4241 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
4243 struct intel_encoder *encoder;
4244 int pipe = intel_crtc->pipe;
3b117c8f 4245 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4246
4f771f10
PZ
4247 if (!intel_crtc->active)
4248 return;
4249
d3eedb1a 4250 intel_crtc_disable_planes(crtc);
dda9a66a 4251
8807e55b
JN
4252 for_each_encoder_on_crtc(dev, crtc, encoder) {
4253 intel_opregion_notify_encoder(encoder, false);
4f771f10 4254 encoder->disable(encoder);
8807e55b 4255 }
4f771f10 4256
8664281b
PZ
4257 if (intel_crtc->config.has_pch_encoder)
4258 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
4259 intel_disable_pipe(dev_priv, pipe);
4260
ad80a810 4261 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4262
3f8dce3a 4263 ironlake_pfit_disable(intel_crtc);
4f771f10 4264
1f544388 4265 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10
PZ
4266
4267 for_each_encoder_on_crtc(dev, crtc, encoder)
4268 if (encoder->post_disable)
4269 encoder->post_disable(encoder);
4270
88adfff1 4271 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4272 lpt_disable_pch_transcoder(dev_priv);
8664281b 4273 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4274 intel_ddi_fdi_disable(crtc);
83616634 4275 }
4f771f10
PZ
4276
4277 intel_crtc->active = false;
46ba614c 4278 intel_update_watermarks(crtc);
4f771f10
PZ
4279
4280 mutex_lock(&dev->struct_mutex);
4281 intel_update_fbc(dev);
71b1c373 4282 intel_edp_psr_update(dev);
4f771f10
PZ
4283 mutex_unlock(&dev->struct_mutex);
4284}
4285
ee7b9f93
JB
4286static void ironlake_crtc_off(struct drm_crtc *crtc)
4287{
4288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4289 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4290}
4291
6441ab5f
PZ
4292static void haswell_crtc_off(struct drm_crtc *crtc)
4293{
4294 intel_ddi_put_crtc_pll(crtc);
4295}
4296
2dd24552
JB
4297static void i9xx_pfit_enable(struct intel_crtc *crtc)
4298{
4299 struct drm_device *dev = crtc->base.dev;
4300 struct drm_i915_private *dev_priv = dev->dev_private;
4301 struct intel_crtc_config *pipe_config = &crtc->config;
4302
328d8e82 4303 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4304 return;
4305
2dd24552 4306 /*
c0b03411
DV
4307 * The panel fitter should only be adjusted whilst the pipe is disabled,
4308 * according to register description and PRM.
2dd24552 4309 */
c0b03411
DV
4310 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4311 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4312
b074cec8
JB
4313 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4314 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4315
4316 /* Border color in case we don't scale up to the full screen. Black by
4317 * default, change to something else for debugging. */
4318 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4319}
4320
77d22dca
ID
4321#define for_each_power_domain(domain, mask) \
4322 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4323 if ((1 << (domain)) & (mask))
4324
319be8ae
ID
4325enum intel_display_power_domain
4326intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4327{
4328 struct drm_device *dev = intel_encoder->base.dev;
4329 struct intel_digital_port *intel_dig_port;
4330
4331 switch (intel_encoder->type) {
4332 case INTEL_OUTPUT_UNKNOWN:
4333 /* Only DDI platforms should ever use this output type */
4334 WARN_ON_ONCE(!HAS_DDI(dev));
4335 case INTEL_OUTPUT_DISPLAYPORT:
4336 case INTEL_OUTPUT_HDMI:
4337 case INTEL_OUTPUT_EDP:
4338 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4339 switch (intel_dig_port->port) {
4340 case PORT_A:
4341 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4342 case PORT_B:
4343 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4344 case PORT_C:
4345 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4346 case PORT_D:
4347 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4348 default:
4349 WARN_ON_ONCE(1);
4350 return POWER_DOMAIN_PORT_OTHER;
4351 }
4352 case INTEL_OUTPUT_ANALOG:
4353 return POWER_DOMAIN_PORT_CRT;
4354 case INTEL_OUTPUT_DSI:
4355 return POWER_DOMAIN_PORT_DSI;
4356 default:
4357 return POWER_DOMAIN_PORT_OTHER;
4358 }
4359}
4360
4361static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4362{
319be8ae
ID
4363 struct drm_device *dev = crtc->dev;
4364 struct intel_encoder *intel_encoder;
4365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4366 enum pipe pipe = intel_crtc->pipe;
4367 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
77d22dca
ID
4368 unsigned long mask;
4369 enum transcoder transcoder;
4370
4371 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4372
4373 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4374 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4375 if (pfit_enabled)
4376 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4377
319be8ae
ID
4378 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4379 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4380
77d22dca
ID
4381 return mask;
4382}
4383
4384void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4385 bool enable)
4386{
4387 if (dev_priv->power_domains.init_power_on == enable)
4388 return;
4389
4390 if (enable)
4391 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4392 else
4393 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4394
4395 dev_priv->power_domains.init_power_on = enable;
4396}
4397
4398static void modeset_update_crtc_power_domains(struct drm_device *dev)
4399{
4400 struct drm_i915_private *dev_priv = dev->dev_private;
4401 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4402 struct intel_crtc *crtc;
4403
4404 /*
4405 * First get all needed power domains, then put all unneeded, to avoid
4406 * any unnecessary toggling of the power wells.
4407 */
d3fcc808 4408 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4409 enum intel_display_power_domain domain;
4410
4411 if (!crtc->base.enabled)
4412 continue;
4413
319be8ae 4414 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4415
4416 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4417 intel_display_power_get(dev_priv, domain);
4418 }
4419
d3fcc808 4420 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4421 enum intel_display_power_domain domain;
4422
4423 for_each_power_domain(domain, crtc->enabled_power_domains)
4424 intel_display_power_put(dev_priv, domain);
4425
4426 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4427 }
4428
4429 intel_display_set_init_power(dev_priv, false);
4430}
4431
586f49dc 4432int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4433{
586f49dc 4434 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4435
586f49dc
JB
4436 /* Obtain SKU information */
4437 mutex_lock(&dev_priv->dpio_lock);
4438 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4439 CCK_FUSE_HPLL_FREQ_MASK;
4440 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4441
586f49dc 4442 return vco_freq[hpll_freq];
30a970c6
JB
4443}
4444
4445/* Adjust CDclk dividers to allow high res or save power if possible */
4446static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4447{
4448 struct drm_i915_private *dev_priv = dev->dev_private;
4449 u32 val, cmd;
4450
d60c4473
ID
4451 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4452 dev_priv->vlv_cdclk_freq = cdclk;
4453
30a970c6
JB
4454 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4455 cmd = 2;
4456 else if (cdclk == 266)
4457 cmd = 1;
4458 else
4459 cmd = 0;
4460
4461 mutex_lock(&dev_priv->rps.hw_lock);
4462 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4463 val &= ~DSPFREQGUAR_MASK;
4464 val |= (cmd << DSPFREQGUAR_SHIFT);
4465 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4466 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4467 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4468 50)) {
4469 DRM_ERROR("timed out waiting for CDclk change\n");
4470 }
4471 mutex_unlock(&dev_priv->rps.hw_lock);
4472
4473 if (cdclk == 400) {
4474 u32 divider, vco;
4475
4476 vco = valleyview_get_vco(dev_priv);
4477 divider = ((vco << 1) / cdclk) - 1;
4478
4479 mutex_lock(&dev_priv->dpio_lock);
4480 /* adjust cdclk divider */
4481 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4482 val &= ~0xf;
4483 val |= divider;
4484 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4485 mutex_unlock(&dev_priv->dpio_lock);
4486 }
4487
4488 mutex_lock(&dev_priv->dpio_lock);
4489 /* adjust self-refresh exit latency value */
4490 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4491 val &= ~0x7f;
4492
4493 /*
4494 * For high bandwidth configs, we set a higher latency in the bunit
4495 * so that the core display fetch happens in time to avoid underruns.
4496 */
4497 if (cdclk == 400)
4498 val |= 4500 / 250; /* 4.5 usec */
4499 else
4500 val |= 3000 / 250; /* 3.0 usec */
4501 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4502 mutex_unlock(&dev_priv->dpio_lock);
4503
4504 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4505 intel_i2c_reset(dev);
4506}
4507
d60c4473 4508int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4509{
4510 int cur_cdclk, vco;
4511 int divider;
4512
4513 vco = valleyview_get_vco(dev_priv);
4514
4515 mutex_lock(&dev_priv->dpio_lock);
4516 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4517 mutex_unlock(&dev_priv->dpio_lock);
4518
4519 divider &= 0xf;
4520
4521 cur_cdclk = (vco << 1) / (divider + 1);
4522
4523 return cur_cdclk;
4524}
4525
4526static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4527 int max_pixclk)
4528{
30a970c6
JB
4529 /*
4530 * Really only a few cases to deal with, as only 4 CDclks are supported:
4531 * 200MHz
4532 * 267MHz
4533 * 320MHz
4534 * 400MHz
4535 * So we check to see whether we're above 90% of the lower bin and
4536 * adjust if needed.
4537 */
4538 if (max_pixclk > 288000) {
4539 return 400;
4540 } else if (max_pixclk > 240000) {
4541 return 320;
4542 } else
4543 return 266;
4544 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4545}
4546
2f2d7aa1
VS
4547/* compute the max pixel clock for new configuration */
4548static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4549{
4550 struct drm_device *dev = dev_priv->dev;
4551 struct intel_crtc *intel_crtc;
4552 int max_pixclk = 0;
4553
d3fcc808 4554 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4555 if (intel_crtc->new_enabled)
30a970c6 4556 max_pixclk = max(max_pixclk,
2f2d7aa1 4557 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4558 }
4559
4560 return max_pixclk;
4561}
4562
4563static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4564 unsigned *prepare_pipes)
30a970c6
JB
4565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 struct intel_crtc *intel_crtc;
2f2d7aa1 4568 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4569
d60c4473
ID
4570 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4571 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4572 return;
4573
2f2d7aa1 4574 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4575 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4576 if (intel_crtc->base.enabled)
4577 *prepare_pipes |= (1 << intel_crtc->pipe);
4578}
4579
4580static void valleyview_modeset_global_resources(struct drm_device *dev)
4581{
4582 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4583 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4584 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4585
d60c4473 4586 if (req_cdclk != dev_priv->vlv_cdclk_freq)
30a970c6 4587 valleyview_set_cdclk(dev, req_cdclk);
77961eb9 4588 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4589}
4590
89b667f8
JB
4591static void valleyview_crtc_enable(struct drm_crtc *crtc)
4592{
4593 struct drm_device *dev = crtc->dev;
5b18e57c 4594 struct drm_i915_private *dev_priv = dev->dev_private;
89b667f8
JB
4595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 struct intel_encoder *encoder;
4597 int pipe = intel_crtc->pipe;
5b18e57c 4598 int plane = intel_crtc->plane;
23538ef1 4599 bool is_dsi;
5b18e57c 4600 u32 dspcntr;
89b667f8
JB
4601
4602 WARN_ON(!crtc->enabled);
4603
4604 if (intel_crtc->active)
4605 return;
4606
bdd4b6a6
DV
4607 vlv_prepare_pll(intel_crtc);
4608
5b18e57c
DV
4609 /* Set up the display plane register */
4610 dspcntr = DISPPLANE_GAMMA_ENABLE;
4611
4612 if (intel_crtc->config.has_dp_encoder)
4613 intel_dp_set_m_n(intel_crtc);
4614
4615 intel_set_pipe_timings(intel_crtc);
4616
4617 /* pipesrc and dspsize control the size that is scaled from,
4618 * which should always be the user's requested size.
4619 */
4620 I915_WRITE(DSPSIZE(plane),
4621 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4622 (intel_crtc->config.pipe_src_w - 1));
4623 I915_WRITE(DSPPOS(plane), 0);
4624
4625 i9xx_set_pipeconf(intel_crtc);
4626
4627 I915_WRITE(DSPCNTR(plane), dspcntr);
4628 POSTING_READ(DSPCNTR(plane));
4629
4630 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4631 crtc->x, crtc->y);
4632
89b667f8 4633 intel_crtc->active = true;
89b667f8 4634
4a3436e8
VS
4635 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4636
89b667f8
JB
4637 for_each_encoder_on_crtc(dev, crtc, encoder)
4638 if (encoder->pre_pll_enable)
4639 encoder->pre_pll_enable(encoder);
4640
23538ef1
JN
4641 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4642
9d556c99
CML
4643 if (!is_dsi) {
4644 if (IS_CHERRYVIEW(dev))
4645 chv_enable_pll(intel_crtc);
4646 else
4647 vlv_enable_pll(intel_crtc);
4648 }
89b667f8
JB
4649
4650 for_each_encoder_on_crtc(dev, crtc, encoder)
4651 if (encoder->pre_enable)
4652 encoder->pre_enable(encoder);
4653
2dd24552
JB
4654 i9xx_pfit_enable(intel_crtc);
4655
63cbb074
VS
4656 intel_crtc_load_lut(crtc);
4657
f37fcc2a 4658 intel_update_watermarks(crtc);
e1fdc473 4659 intel_enable_pipe(intel_crtc);
be6a6f8e 4660
5004945f
JN
4661 for_each_encoder_on_crtc(dev, crtc, encoder)
4662 encoder->enable(encoder);
9ab0460b
VS
4663
4664 intel_crtc_enable_planes(crtc);
d40d9187 4665
56b80e1f
VS
4666 /* Underruns don't raise interrupts, so check manually. */
4667 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4668}
4669
f13c2ef3
DV
4670static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4671{
4672 struct drm_device *dev = crtc->base.dev;
4673 struct drm_i915_private *dev_priv = dev->dev_private;
4674
4675 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4676 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4677}
4678
0b8765c6 4679static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4680{
4681 struct drm_device *dev = crtc->dev;
5b18e57c 4682 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 4683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4684 struct intel_encoder *encoder;
79e53945 4685 int pipe = intel_crtc->pipe;
5b18e57c
DV
4686 int plane = intel_crtc->plane;
4687 u32 dspcntr;
79e53945 4688
08a48469
DV
4689 WARN_ON(!crtc->enabled);
4690
f7abfe8b
CW
4691 if (intel_crtc->active)
4692 return;
4693
f13c2ef3
DV
4694 i9xx_set_pll_dividers(intel_crtc);
4695
5b18e57c
DV
4696 /* Set up the display plane register */
4697 dspcntr = DISPPLANE_GAMMA_ENABLE;
4698
4699 if (pipe == 0)
4700 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4701 else
4702 dspcntr |= DISPPLANE_SEL_PIPE_B;
4703
4704 if (intel_crtc->config.has_dp_encoder)
4705 intel_dp_set_m_n(intel_crtc);
4706
4707 intel_set_pipe_timings(intel_crtc);
4708
4709 /* pipesrc and dspsize control the size that is scaled from,
4710 * which should always be the user's requested size.
4711 */
4712 I915_WRITE(DSPSIZE(plane),
4713 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4714 (intel_crtc->config.pipe_src_w - 1));
4715 I915_WRITE(DSPPOS(plane), 0);
4716
4717 i9xx_set_pipeconf(intel_crtc);
4718
4719 I915_WRITE(DSPCNTR(plane), dspcntr);
4720 POSTING_READ(DSPCNTR(plane));
4721
4722 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4723 crtc->x, crtc->y);
4724
f7abfe8b 4725 intel_crtc->active = true;
6b383a7f 4726
4a3436e8
VS
4727 if (!IS_GEN2(dev))
4728 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4729
9d6d9f19
MK
4730 for_each_encoder_on_crtc(dev, crtc, encoder)
4731 if (encoder->pre_enable)
4732 encoder->pre_enable(encoder);
4733
f6736a1a
DV
4734 i9xx_enable_pll(intel_crtc);
4735
2dd24552
JB
4736 i9xx_pfit_enable(intel_crtc);
4737
63cbb074
VS
4738 intel_crtc_load_lut(crtc);
4739
f37fcc2a 4740 intel_update_watermarks(crtc);
e1fdc473 4741 intel_enable_pipe(intel_crtc);
be6a6f8e 4742
fa5c73b1
DV
4743 for_each_encoder_on_crtc(dev, crtc, encoder)
4744 encoder->enable(encoder);
9ab0460b
VS
4745
4746 intel_crtc_enable_planes(crtc);
d40d9187 4747
4a3436e8
VS
4748 /*
4749 * Gen2 reports pipe underruns whenever all planes are disabled.
4750 * So don't enable underrun reporting before at least some planes
4751 * are enabled.
4752 * FIXME: Need to fix the logic to work when we turn off all planes
4753 * but leave the pipe running.
4754 */
4755 if (IS_GEN2(dev))
4756 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4757
56b80e1f
VS
4758 /* Underruns don't raise interrupts, so check manually. */
4759 i9xx_check_fifo_underruns(dev);
0b8765c6 4760}
79e53945 4761
87476d63
DV
4762static void i9xx_pfit_disable(struct intel_crtc *crtc)
4763{
4764 struct drm_device *dev = crtc->base.dev;
4765 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4766
328d8e82
DV
4767 if (!crtc->config.gmch_pfit.control)
4768 return;
87476d63 4769
328d8e82 4770 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4771
328d8e82
DV
4772 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4773 I915_READ(PFIT_CONTROL));
4774 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4775}
4776
0b8765c6
JB
4777static void i9xx_crtc_disable(struct drm_crtc *crtc)
4778{
4779 struct drm_device *dev = crtc->dev;
4780 struct drm_i915_private *dev_priv = dev->dev_private;
4781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4782 struct intel_encoder *encoder;
0b8765c6 4783 int pipe = intel_crtc->pipe;
ef9c3aee 4784
f7abfe8b
CW
4785 if (!intel_crtc->active)
4786 return;
4787
4a3436e8
VS
4788 /*
4789 * Gen2 reports pipe underruns whenever all planes are disabled.
4790 * So diasble underrun reporting before all the planes get disabled.
4791 * FIXME: Need to fix the logic to work when we turn off all planes
4792 * but leave the pipe running.
4793 */
4794 if (IS_GEN2(dev))
4795 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4796
9ab0460b
VS
4797 intel_crtc_disable_planes(crtc);
4798
ea9d758d
DV
4799 for_each_encoder_on_crtc(dev, crtc, encoder)
4800 encoder->disable(encoder);
4801
6304cd91
VS
4802 /*
4803 * On gen2 planes are double buffered but the pipe isn't, so we must
4804 * wait for planes to fully turn off before disabling the pipe.
4805 */
4806 if (IS_GEN2(dev))
4807 intel_wait_for_vblank(dev, pipe);
4808
b24e7179 4809 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4810
87476d63 4811 i9xx_pfit_disable(intel_crtc);
24a1f16d 4812
89b667f8
JB
4813 for_each_encoder_on_crtc(dev, crtc, encoder)
4814 if (encoder->post_disable)
4815 encoder->post_disable(encoder);
4816
076ed3b2
CML
4817 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4818 if (IS_CHERRYVIEW(dev))
4819 chv_disable_pll(dev_priv, pipe);
4820 else if (IS_VALLEYVIEW(dev))
4821 vlv_disable_pll(dev_priv, pipe);
4822 else
4823 i9xx_disable_pll(dev_priv, pipe);
4824 }
0b8765c6 4825
4a3436e8
VS
4826 if (!IS_GEN2(dev))
4827 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4828
f7abfe8b 4829 intel_crtc->active = false;
46ba614c 4830 intel_update_watermarks(crtc);
f37fcc2a 4831
efa9624e 4832 mutex_lock(&dev->struct_mutex);
6b383a7f 4833 intel_update_fbc(dev);
71b1c373 4834 intel_edp_psr_update(dev);
efa9624e 4835 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
4836}
4837
ee7b9f93
JB
4838static void i9xx_crtc_off(struct drm_crtc *crtc)
4839{
4840}
4841
976f8a20
DV
4842static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4843 bool enabled)
2c07245f
ZW
4844{
4845 struct drm_device *dev = crtc->dev;
4846 struct drm_i915_master_private *master_priv;
4847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4848 int pipe = intel_crtc->pipe;
79e53945
JB
4849
4850 if (!dev->primary->master)
4851 return;
4852
4853 master_priv = dev->primary->master->driver_priv;
4854 if (!master_priv->sarea_priv)
4855 return;
4856
79e53945
JB
4857 switch (pipe) {
4858 case 0:
4859 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4860 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4861 break;
4862 case 1:
4863 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4864 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4865 break;
4866 default:
9db4a9c7 4867 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4868 break;
4869 }
79e53945
JB
4870}
4871
976f8a20
DV
4872/**
4873 * Sets the power management mode of the pipe and plane.
4874 */
4875void intel_crtc_update_dpms(struct drm_crtc *crtc)
4876{
4877 struct drm_device *dev = crtc->dev;
4878 struct drm_i915_private *dev_priv = dev->dev_private;
4879 struct intel_encoder *intel_encoder;
4880 bool enable = false;
4881
4882 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4883 enable |= intel_encoder->connectors_active;
4884
4885 if (enable)
4886 dev_priv->display.crtc_enable(crtc);
4887 else
4888 dev_priv->display.crtc_disable(crtc);
4889
4890 intel_crtc_update_sarea(crtc, enable);
4891}
4892
cdd59983
CW
4893static void intel_crtc_disable(struct drm_crtc *crtc)
4894{
cdd59983 4895 struct drm_device *dev = crtc->dev;
976f8a20 4896 struct drm_connector *connector;
ee7b9f93 4897 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 4898
976f8a20
DV
4899 /* crtc should still be enabled when we disable it. */
4900 WARN_ON(!crtc->enabled);
4901
4902 dev_priv->display.crtc_disable(crtc);
4903 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4904 dev_priv->display.off(crtc);
4905
931872fc 4906 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
93ce0ba6 4907 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
931872fc 4908 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
cdd59983 4909
f4510a27 4910 if (crtc->primary->fb) {
cdd59983 4911 mutex_lock(&dev->struct_mutex);
f4510a27 4912 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
cdd59983 4913 mutex_unlock(&dev->struct_mutex);
f4510a27 4914 crtc->primary->fb = NULL;
976f8a20
DV
4915 }
4916
4917 /* Update computed state. */
4918 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4919 if (!connector->encoder || !connector->encoder->crtc)
4920 continue;
4921
4922 if (connector->encoder->crtc != crtc)
4923 continue;
4924
4925 connector->dpms = DRM_MODE_DPMS_OFF;
4926 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
4927 }
4928}
4929
ea5b213a 4930void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 4931{
4ef69c7a 4932 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 4933
ea5b213a
CW
4934 drm_encoder_cleanup(encoder);
4935 kfree(intel_encoder);
7e7d76c3
JB
4936}
4937
9237329d 4938/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
4939 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4940 * state of the entire output pipe. */
9237329d 4941static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 4942{
5ab432ef
DV
4943 if (mode == DRM_MODE_DPMS_ON) {
4944 encoder->connectors_active = true;
4945
b2cabb0e 4946 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
4947 } else {
4948 encoder->connectors_active = false;
4949
b2cabb0e 4950 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 4951 }
79e53945
JB
4952}
4953
0a91ca29
DV
4954/* Cross check the actual hw state with our own modeset state tracking (and it's
4955 * internal consistency). */
b980514c 4956static void intel_connector_check_state(struct intel_connector *connector)
79e53945 4957{
0a91ca29
DV
4958 if (connector->get_hw_state(connector)) {
4959 struct intel_encoder *encoder = connector->encoder;
4960 struct drm_crtc *crtc;
4961 bool encoder_enabled;
4962 enum pipe pipe;
4963
4964 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4965 connector->base.base.id,
c23cc417 4966 connector->base.name);
0a91ca29
DV
4967
4968 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4969 "wrong connector dpms state\n");
4970 WARN(connector->base.encoder != &encoder->base,
4971 "active connector not linked to encoder\n");
4972 WARN(!encoder->connectors_active,
4973 "encoder->connectors_active not set\n");
4974
4975 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4976 WARN(!encoder_enabled, "encoder not enabled\n");
4977 if (WARN_ON(!encoder->base.crtc))
4978 return;
4979
4980 crtc = encoder->base.crtc;
4981
4982 WARN(!crtc->enabled, "crtc not enabled\n");
4983 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4984 WARN(pipe != to_intel_crtc(crtc)->pipe,
4985 "encoder active on the wrong pipe\n");
4986 }
79e53945
JB
4987}
4988
5ab432ef
DV
4989/* Even simpler default implementation, if there's really no special case to
4990 * consider. */
4991void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 4992{
5ab432ef
DV
4993 /* All the simple cases only support two dpms states. */
4994 if (mode != DRM_MODE_DPMS_ON)
4995 mode = DRM_MODE_DPMS_OFF;
d4270e57 4996
5ab432ef
DV
4997 if (mode == connector->dpms)
4998 return;
4999
5000 connector->dpms = mode;
5001
5002 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5003 if (connector->encoder)
5004 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5005
b980514c 5006 intel_modeset_check_state(connector->dev);
79e53945
JB
5007}
5008
f0947c37
DV
5009/* Simple connector->get_hw_state implementation for encoders that support only
5010 * one connector and no cloning and hence the encoder state determines the state
5011 * of the connector. */
5012bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5013{
24929352 5014 enum pipe pipe = 0;
f0947c37 5015 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5016
f0947c37 5017 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5018}
5019
1857e1da
DV
5020static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5021 struct intel_crtc_config *pipe_config)
5022{
5023 struct drm_i915_private *dev_priv = dev->dev_private;
5024 struct intel_crtc *pipe_B_crtc =
5025 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5026
5027 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5028 pipe_name(pipe), pipe_config->fdi_lanes);
5029 if (pipe_config->fdi_lanes > 4) {
5030 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5031 pipe_name(pipe), pipe_config->fdi_lanes);
5032 return false;
5033 }
5034
bafb6553 5035 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5036 if (pipe_config->fdi_lanes > 2) {
5037 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5038 pipe_config->fdi_lanes);
5039 return false;
5040 } else {
5041 return true;
5042 }
5043 }
5044
5045 if (INTEL_INFO(dev)->num_pipes == 2)
5046 return true;
5047
5048 /* Ivybridge 3 pipe is really complicated */
5049 switch (pipe) {
5050 case PIPE_A:
5051 return true;
5052 case PIPE_B:
5053 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5054 pipe_config->fdi_lanes > 2) {
5055 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5056 pipe_name(pipe), pipe_config->fdi_lanes);
5057 return false;
5058 }
5059 return true;
5060 case PIPE_C:
1e833f40 5061 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5062 pipe_B_crtc->config.fdi_lanes <= 2) {
5063 if (pipe_config->fdi_lanes > 2) {
5064 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5065 pipe_name(pipe), pipe_config->fdi_lanes);
5066 return false;
5067 }
5068 } else {
5069 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5070 return false;
5071 }
5072 return true;
5073 default:
5074 BUG();
5075 }
5076}
5077
e29c22c0
DV
5078#define RETRY 1
5079static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5080 struct intel_crtc_config *pipe_config)
877d48d5 5081{
1857e1da 5082 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5083 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5084 int lane, link_bw, fdi_dotclock;
e29c22c0 5085 bool setup_ok, needs_recompute = false;
877d48d5 5086
e29c22c0 5087retry:
877d48d5
DV
5088 /* FDI is a binary signal running at ~2.7GHz, encoding
5089 * each output octet as 10 bits. The actual frequency
5090 * is stored as a divider into a 100MHz clock, and the
5091 * mode pixel clock is stored in units of 1KHz.
5092 * Hence the bw of each lane in terms of the mode signal
5093 * is:
5094 */
5095 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5096
241bfc38 5097 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5098
2bd89a07 5099 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5100 pipe_config->pipe_bpp);
5101
5102 pipe_config->fdi_lanes = lane;
5103
2bd89a07 5104 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5105 link_bw, &pipe_config->fdi_m_n);
1857e1da 5106
e29c22c0
DV
5107 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5108 intel_crtc->pipe, pipe_config);
5109 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5110 pipe_config->pipe_bpp -= 2*3;
5111 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5112 pipe_config->pipe_bpp);
5113 needs_recompute = true;
5114 pipe_config->bw_constrained = true;
5115
5116 goto retry;
5117 }
5118
5119 if (needs_recompute)
5120 return RETRY;
5121
5122 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5123}
5124
42db64ef
PZ
5125static void hsw_compute_ips_config(struct intel_crtc *crtc,
5126 struct intel_crtc_config *pipe_config)
5127{
d330a953 5128 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5129 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5130 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5131}
5132
a43f6e0f 5133static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5134 struct intel_crtc_config *pipe_config)
79e53945 5135{
a43f6e0f 5136 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5137 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5138
ad3a4479 5139 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5140 if (INTEL_INFO(dev)->gen < 4) {
5141 struct drm_i915_private *dev_priv = dev->dev_private;
5142 int clock_limit =
5143 dev_priv->display.get_display_clock_speed(dev);
5144
5145 /*
5146 * Enable pixel doubling when the dot clock
5147 * is > 90% of the (display) core speed.
5148 *
b397c96b
VS
5149 * GDG double wide on either pipe,
5150 * otherwise pipe A only.
cf532bb2 5151 */
b397c96b 5152 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5153 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5154 clock_limit *= 2;
cf532bb2 5155 pipe_config->double_wide = true;
ad3a4479
VS
5156 }
5157
241bfc38 5158 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5159 return -EINVAL;
2c07245f 5160 }
89749350 5161
1d1d0e27
VS
5162 /*
5163 * Pipe horizontal size must be even in:
5164 * - DVO ganged mode
5165 * - LVDS dual channel mode
5166 * - Double wide pipe
5167 */
5168 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5169 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5170 pipe_config->pipe_src_w &= ~1;
5171
8693a824
DL
5172 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5173 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5174 */
5175 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5176 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5177 return -EINVAL;
44f46b42 5178
bd080ee5 5179 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5180 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5181 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5182 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5183 * for lvds. */
5184 pipe_config->pipe_bpp = 8*3;
5185 }
5186
f5adf94e 5187 if (HAS_IPS(dev))
a43f6e0f
DV
5188 hsw_compute_ips_config(crtc, pipe_config);
5189
5190 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5191 * clock survives for now. */
5192 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5193 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5194
877d48d5 5195 if (pipe_config->has_pch_encoder)
a43f6e0f 5196 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5197
e29c22c0 5198 return 0;
79e53945
JB
5199}
5200
25eb05fc
JB
5201static int valleyview_get_display_clock_speed(struct drm_device *dev)
5202{
5203 return 400000; /* FIXME */
5204}
5205
e70236a8
JB
5206static int i945_get_display_clock_speed(struct drm_device *dev)
5207{
5208 return 400000;
5209}
79e53945 5210
e70236a8 5211static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5212{
e70236a8
JB
5213 return 333000;
5214}
79e53945 5215
e70236a8
JB
5216static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5217{
5218 return 200000;
5219}
79e53945 5220
257a7ffc
DV
5221static int pnv_get_display_clock_speed(struct drm_device *dev)
5222{
5223 u16 gcfgc = 0;
5224
5225 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5226
5227 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5228 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5229 return 267000;
5230 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5231 return 333000;
5232 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5233 return 444000;
5234 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5235 return 200000;
5236 default:
5237 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5238 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5239 return 133000;
5240 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5241 return 167000;
5242 }
5243}
5244
e70236a8
JB
5245static int i915gm_get_display_clock_speed(struct drm_device *dev)
5246{
5247 u16 gcfgc = 0;
79e53945 5248
e70236a8
JB
5249 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5250
5251 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5252 return 133000;
5253 else {
5254 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5255 case GC_DISPLAY_CLOCK_333_MHZ:
5256 return 333000;
5257 default:
5258 case GC_DISPLAY_CLOCK_190_200_MHZ:
5259 return 190000;
79e53945 5260 }
e70236a8
JB
5261 }
5262}
5263
5264static int i865_get_display_clock_speed(struct drm_device *dev)
5265{
5266 return 266000;
5267}
5268
5269static int i855_get_display_clock_speed(struct drm_device *dev)
5270{
5271 u16 hpllcc = 0;
5272 /* Assume that the hardware is in the high speed state. This
5273 * should be the default.
5274 */
5275 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5276 case GC_CLOCK_133_200:
5277 case GC_CLOCK_100_200:
5278 return 200000;
5279 case GC_CLOCK_166_250:
5280 return 250000;
5281 case GC_CLOCK_100_133:
79e53945 5282 return 133000;
e70236a8 5283 }
79e53945 5284
e70236a8
JB
5285 /* Shouldn't happen */
5286 return 0;
5287}
79e53945 5288
e70236a8
JB
5289static int i830_get_display_clock_speed(struct drm_device *dev)
5290{
5291 return 133000;
79e53945
JB
5292}
5293
2c07245f 5294static void
a65851af 5295intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5296{
a65851af
VS
5297 while (*num > DATA_LINK_M_N_MASK ||
5298 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5299 *num >>= 1;
5300 *den >>= 1;
5301 }
5302}
5303
a65851af
VS
5304static void compute_m_n(unsigned int m, unsigned int n,
5305 uint32_t *ret_m, uint32_t *ret_n)
5306{
5307 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5308 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5309 intel_reduce_m_n_ratio(ret_m, ret_n);
5310}
5311
e69d0bc1
DV
5312void
5313intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5314 int pixel_clock, int link_clock,
5315 struct intel_link_m_n *m_n)
2c07245f 5316{
e69d0bc1 5317 m_n->tu = 64;
a65851af
VS
5318
5319 compute_m_n(bits_per_pixel * pixel_clock,
5320 link_clock * nlanes * 8,
5321 &m_n->gmch_m, &m_n->gmch_n);
5322
5323 compute_m_n(pixel_clock, link_clock,
5324 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5325}
5326
a7615030
CW
5327static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5328{
d330a953
JN
5329 if (i915.panel_use_ssc >= 0)
5330 return i915.panel_use_ssc != 0;
41aa3448 5331 return dev_priv->vbt.lvds_use_ssc
435793df 5332 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5333}
5334
c65d77d8
JB
5335static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5336{
5337 struct drm_device *dev = crtc->dev;
5338 struct drm_i915_private *dev_priv = dev->dev_private;
5339 int refclk;
5340
a0c4da24 5341 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5342 refclk = 100000;
a0c4da24 5343 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5344 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5345 refclk = dev_priv->vbt.lvds_ssc_freq;
5346 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5347 } else if (!IS_GEN2(dev)) {
5348 refclk = 96000;
5349 } else {
5350 refclk = 48000;
5351 }
5352
5353 return refclk;
5354}
5355
7429e9d4 5356static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5357{
7df00d7a 5358 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5359}
f47709a9 5360
7429e9d4
DV
5361static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5362{
5363 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5364}
5365
f47709a9 5366static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5367 intel_clock_t *reduced_clock)
5368{
f47709a9 5369 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5370 u32 fp, fp2 = 0;
5371
5372 if (IS_PINEVIEW(dev)) {
7429e9d4 5373 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5374 if (reduced_clock)
7429e9d4 5375 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5376 } else {
7429e9d4 5377 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5378 if (reduced_clock)
7429e9d4 5379 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5380 }
5381
8bcc2795 5382 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5383
f47709a9
DV
5384 crtc->lowfreq_avail = false;
5385 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5386 reduced_clock && i915.powersave) {
8bcc2795 5387 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5388 crtc->lowfreq_avail = true;
a7516a05 5389 } else {
8bcc2795 5390 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5391 }
5392}
5393
5e69f97f
CML
5394static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5395 pipe)
89b667f8
JB
5396{
5397 u32 reg_val;
5398
5399 /*
5400 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5401 * and set it to a reasonable value instead.
5402 */
ab3c759a 5403 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5404 reg_val &= 0xffffff00;
5405 reg_val |= 0x00000030;
ab3c759a 5406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5407
ab3c759a 5408 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5409 reg_val &= 0x8cffffff;
5410 reg_val = 0x8c000000;
ab3c759a 5411 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5412
ab3c759a 5413 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5414 reg_val &= 0xffffff00;
ab3c759a 5415 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5416
ab3c759a 5417 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5418 reg_val &= 0x00ffffff;
5419 reg_val |= 0xb0000000;
ab3c759a 5420 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5421}
5422
b551842d
DV
5423static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5424 struct intel_link_m_n *m_n)
5425{
5426 struct drm_device *dev = crtc->base.dev;
5427 struct drm_i915_private *dev_priv = dev->dev_private;
5428 int pipe = crtc->pipe;
5429
e3b95f1e
DV
5430 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5431 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5432 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5433 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5434}
5435
5436static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5437 struct intel_link_m_n *m_n)
5438{
5439 struct drm_device *dev = crtc->base.dev;
5440 struct drm_i915_private *dev_priv = dev->dev_private;
5441 int pipe = crtc->pipe;
5442 enum transcoder transcoder = crtc->config.cpu_transcoder;
5443
5444 if (INTEL_INFO(dev)->gen >= 5) {
5445 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5446 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5447 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5448 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5449 } else {
e3b95f1e
DV
5450 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5451 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5452 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5453 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5454 }
5455}
5456
03afc4a2
DV
5457static void intel_dp_set_m_n(struct intel_crtc *crtc)
5458{
5459 if (crtc->config.has_pch_encoder)
5460 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5461 else
5462 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5463}
5464
f47709a9 5465static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5466{
5467 u32 dpll, dpll_md;
5468
5469 /*
5470 * Enable DPIO clock input. We should never disable the reference
5471 * clock for pipe B, since VGA hotplug / manual detection depends
5472 * on it.
5473 */
5474 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5475 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5476 /* We should never disable this, set it here for state tracking */
5477 if (crtc->pipe == PIPE_B)
5478 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5479 dpll |= DPLL_VCO_ENABLE;
5480 crtc->config.dpll_hw_state.dpll = dpll;
5481
5482 dpll_md = (crtc->config.pixel_multiplier - 1)
5483 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5484 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5485}
5486
5487static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5488{
f47709a9 5489 struct drm_device *dev = crtc->base.dev;
a0c4da24 5490 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5491 int pipe = crtc->pipe;
bdd4b6a6 5492 u32 mdiv;
a0c4da24 5493 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5494 u32 coreclk, reg_val;
a0c4da24 5495
09153000
DV
5496 mutex_lock(&dev_priv->dpio_lock);
5497
f47709a9
DV
5498 bestn = crtc->config.dpll.n;
5499 bestm1 = crtc->config.dpll.m1;
5500 bestm2 = crtc->config.dpll.m2;
5501 bestp1 = crtc->config.dpll.p1;
5502 bestp2 = crtc->config.dpll.p2;
a0c4da24 5503
89b667f8
JB
5504 /* See eDP HDMI DPIO driver vbios notes doc */
5505
5506 /* PLL B needs special handling */
bdd4b6a6 5507 if (pipe == PIPE_B)
5e69f97f 5508 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5509
5510 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5511 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5512
5513 /* Disable target IRef on PLL */
ab3c759a 5514 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5515 reg_val &= 0x00ffffff;
ab3c759a 5516 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5517
5518 /* Disable fast lock */
ab3c759a 5519 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5520
5521 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5522 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5523 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5524 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5525 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5526
5527 /*
5528 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5529 * but we don't support that).
5530 * Note: don't use the DAC post divider as it seems unstable.
5531 */
5532 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5533 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5534
a0c4da24 5535 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5536 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5537
89b667f8 5538 /* Set HBR and RBR LPF coefficients */
ff9a6750 5539 if (crtc->config.port_clock == 162000 ||
99750bd4 5540 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5541 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5542 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5543 0x009f0003);
89b667f8 5544 else
ab3c759a 5545 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5546 0x00d0000f);
5547
5548 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5549 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5550 /* Use SSC source */
bdd4b6a6 5551 if (pipe == PIPE_A)
ab3c759a 5552 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5553 0x0df40000);
5554 else
ab3c759a 5555 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5556 0x0df70000);
5557 } else { /* HDMI or VGA */
5558 /* Use bend source */
bdd4b6a6 5559 if (pipe == PIPE_A)
ab3c759a 5560 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5561 0x0df70000);
5562 else
ab3c759a 5563 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5564 0x0df40000);
5565 }
a0c4da24 5566
ab3c759a 5567 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5568 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5569 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5570 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5571 coreclk |= 0x01000000;
ab3c759a 5572 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5573
ab3c759a 5574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5575 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5576}
5577
9d556c99
CML
5578static void chv_update_pll(struct intel_crtc *crtc)
5579{
5580 struct drm_device *dev = crtc->base.dev;
5581 struct drm_i915_private *dev_priv = dev->dev_private;
5582 int pipe = crtc->pipe;
5583 int dpll_reg = DPLL(crtc->pipe);
5584 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5585 u32 loopfilter, intcoeff;
9d556c99
CML
5586 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5587 int refclk;
5588
a11b0703
VS
5589 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5590 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5591 DPLL_VCO_ENABLE;
5592 if (pipe != PIPE_A)
5593 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5594
5595 crtc->config.dpll_hw_state.dpll_md =
5596 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
9d556c99
CML
5597
5598 bestn = crtc->config.dpll.n;
5599 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5600 bestm1 = crtc->config.dpll.m1;
5601 bestm2 = crtc->config.dpll.m2 >> 22;
5602 bestp1 = crtc->config.dpll.p1;
5603 bestp2 = crtc->config.dpll.p2;
5604
5605 /*
5606 * Enable Refclk and SSC
5607 */
a11b0703
VS
5608 I915_WRITE(dpll_reg,
5609 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5610
5611 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5612
9d556c99
CML
5613 /* p1 and p2 divider */
5614 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5615 5 << DPIO_CHV_S1_DIV_SHIFT |
5616 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5617 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5618 1 << DPIO_CHV_K_DIV_SHIFT);
5619
5620 /* Feedback post-divider - m2 */
5621 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5622
5623 /* Feedback refclk divider - n and m1 */
5624 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5625 DPIO_CHV_M1_DIV_BY_2 |
5626 1 << DPIO_CHV_N_DIV_SHIFT);
5627
5628 /* M2 fraction division */
5629 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5630
5631 /* M2 fraction division enable */
5632 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5633 DPIO_CHV_FRAC_DIV_EN |
5634 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5635
5636 /* Loop filter */
5637 refclk = i9xx_get_refclk(&crtc->base, 0);
5638 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5639 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5640 if (refclk == 100000)
5641 intcoeff = 11;
5642 else if (refclk == 38400)
5643 intcoeff = 10;
5644 else
5645 intcoeff = 9;
5646 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5647 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5648
5649 /* AFC Recal */
5650 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5651 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5652 DPIO_AFC_RECAL);
5653
5654 mutex_unlock(&dev_priv->dpio_lock);
5655}
5656
f47709a9
DV
5657static void i9xx_update_pll(struct intel_crtc *crtc,
5658 intel_clock_t *reduced_clock,
eb1cbe48
DV
5659 int num_connectors)
5660{
f47709a9 5661 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5662 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5663 u32 dpll;
5664 bool is_sdvo;
f47709a9 5665 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5666
f47709a9 5667 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5668
f47709a9
DV
5669 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5670 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5671
5672 dpll = DPLL_VGA_MODE_DIS;
5673
f47709a9 5674 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5675 dpll |= DPLLB_MODE_LVDS;
5676 else
5677 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5678
ef1b460d 5679 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5680 dpll |= (crtc->config.pixel_multiplier - 1)
5681 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5682 }
198a037f
DV
5683
5684 if (is_sdvo)
4a33e48d 5685 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5686
f47709a9 5687 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5688 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5689
5690 /* compute bitmask from p1 value */
5691 if (IS_PINEVIEW(dev))
5692 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5693 else {
5694 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5695 if (IS_G4X(dev) && reduced_clock)
5696 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5697 }
5698 switch (clock->p2) {
5699 case 5:
5700 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5701 break;
5702 case 7:
5703 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5704 break;
5705 case 10:
5706 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5707 break;
5708 case 14:
5709 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5710 break;
5711 }
5712 if (INTEL_INFO(dev)->gen >= 4)
5713 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5714
09ede541 5715 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5716 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5717 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5718 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5719 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5720 else
5721 dpll |= PLL_REF_INPUT_DREFCLK;
5722
5723 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5724 crtc->config.dpll_hw_state.dpll = dpll;
5725
eb1cbe48 5726 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5727 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5728 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5729 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5730 }
5731}
5732
f47709a9 5733static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5734 intel_clock_t *reduced_clock,
eb1cbe48
DV
5735 int num_connectors)
5736{
f47709a9 5737 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5738 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5739 u32 dpll;
f47709a9 5740 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5741
f47709a9 5742 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5743
eb1cbe48
DV
5744 dpll = DPLL_VGA_MODE_DIS;
5745
f47709a9 5746 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5747 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5748 } else {
5749 if (clock->p1 == 2)
5750 dpll |= PLL_P1_DIVIDE_BY_TWO;
5751 else
5752 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5753 if (clock->p2 == 4)
5754 dpll |= PLL_P2_DIVIDE_BY_4;
5755 }
5756
4a33e48d
DV
5757 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5758 dpll |= DPLL_DVO_2X_MODE;
5759
f47709a9 5760 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5761 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5762 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5763 else
5764 dpll |= PLL_REF_INPUT_DREFCLK;
5765
5766 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5767 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5768}
5769
8a654f3b 5770static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5771{
5772 struct drm_device *dev = intel_crtc->base.dev;
5773 struct drm_i915_private *dev_priv = dev->dev_private;
5774 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5775 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5776 struct drm_display_mode *adjusted_mode =
5777 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5778 uint32_t crtc_vtotal, crtc_vblank_end;
5779 int vsyncshift = 0;
4d8a62ea
DV
5780
5781 /* We need to be careful not to changed the adjusted mode, for otherwise
5782 * the hw state checker will get angry at the mismatch. */
5783 crtc_vtotal = adjusted_mode->crtc_vtotal;
5784 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5785
609aeaca 5786 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5787 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5788 crtc_vtotal -= 1;
5789 crtc_vblank_end -= 1;
609aeaca
VS
5790
5791 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5792 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5793 else
5794 vsyncshift = adjusted_mode->crtc_hsync_start -
5795 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5796 if (vsyncshift < 0)
5797 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5798 }
5799
5800 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5801 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5802
fe2b8f9d 5803 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5804 (adjusted_mode->crtc_hdisplay - 1) |
5805 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5806 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5807 (adjusted_mode->crtc_hblank_start - 1) |
5808 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5809 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5810 (adjusted_mode->crtc_hsync_start - 1) |
5811 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5812
fe2b8f9d 5813 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5814 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5815 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5816 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5817 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5818 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5819 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5820 (adjusted_mode->crtc_vsync_start - 1) |
5821 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5822
b5e508d4
PZ
5823 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5824 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5825 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5826 * bits. */
5827 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5828 (pipe == PIPE_B || pipe == PIPE_C))
5829 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5830
b0e77b9c
PZ
5831 /* pipesrc controls the size that is scaled from, which should
5832 * always be the user's requested size.
5833 */
5834 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5835 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5836 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5837}
5838
1bd1bd80
DV
5839static void intel_get_pipe_timings(struct intel_crtc *crtc,
5840 struct intel_crtc_config *pipe_config)
5841{
5842 struct drm_device *dev = crtc->base.dev;
5843 struct drm_i915_private *dev_priv = dev->dev_private;
5844 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5845 uint32_t tmp;
5846
5847 tmp = I915_READ(HTOTAL(cpu_transcoder));
5848 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5849 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5850 tmp = I915_READ(HBLANK(cpu_transcoder));
5851 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5852 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5853 tmp = I915_READ(HSYNC(cpu_transcoder));
5854 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5855 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5856
5857 tmp = I915_READ(VTOTAL(cpu_transcoder));
5858 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5859 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5860 tmp = I915_READ(VBLANK(cpu_transcoder));
5861 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5862 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5863 tmp = I915_READ(VSYNC(cpu_transcoder));
5864 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5865 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5866
5867 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5868 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5869 pipe_config->adjusted_mode.crtc_vtotal += 1;
5870 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5871 }
5872
5873 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
5874 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5875 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5876
5877 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5878 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
5879}
5880
f6a83288
DV
5881void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5882 struct intel_crtc_config *pipe_config)
babea61d 5883{
f6a83288
DV
5884 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5885 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5886 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5887 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 5888
f6a83288
DV
5889 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5890 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5891 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5892 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 5893
f6a83288 5894 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 5895
f6a83288
DV
5896 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5897 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
5898}
5899
84b046f3
DV
5900static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5901{
5902 struct drm_device *dev = intel_crtc->base.dev;
5903 struct drm_i915_private *dev_priv = dev->dev_private;
5904 uint32_t pipeconf;
5905
9f11a9e4 5906 pipeconf = 0;
84b046f3 5907
67c72a12
DV
5908 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5909 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5910 pipeconf |= PIPECONF_ENABLE;
5911
cf532bb2
VS
5912 if (intel_crtc->config.double_wide)
5913 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 5914
ff9ce46e
DV
5915 /* only g4x and later have fancy bpc/dither controls */
5916 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
5917 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5918 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5919 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 5920 PIPECONF_DITHER_TYPE_SP;
84b046f3 5921
ff9ce46e
DV
5922 switch (intel_crtc->config.pipe_bpp) {
5923 case 18:
5924 pipeconf |= PIPECONF_6BPC;
5925 break;
5926 case 24:
5927 pipeconf |= PIPECONF_8BPC;
5928 break;
5929 case 30:
5930 pipeconf |= PIPECONF_10BPC;
5931 break;
5932 default:
5933 /* Case prevented by intel_choose_pipe_bpp_dither. */
5934 BUG();
84b046f3
DV
5935 }
5936 }
5937
5938 if (HAS_PIPE_CXSR(dev)) {
5939 if (intel_crtc->lowfreq_avail) {
5940 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5941 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5942 } else {
5943 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
5944 }
5945 }
5946
efc2cfff
VS
5947 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5948 if (INTEL_INFO(dev)->gen < 4 ||
5949 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5950 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5951 else
5952 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5953 } else
84b046f3
DV
5954 pipeconf |= PIPECONF_PROGRESSIVE;
5955
9f11a9e4
DV
5956 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5957 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 5958
84b046f3
DV
5959 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5960 POSTING_READ(PIPECONF(intel_crtc->pipe));
5961}
5962
f564048e 5963static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 5964 int x, int y,
94352cf9 5965 struct drm_framebuffer *fb)
79e53945
JB
5966{
5967 struct drm_device *dev = crtc->dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
5969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 5970 int refclk, num_connectors = 0;
652c393a 5971 intel_clock_t clock, reduced_clock;
a16af721 5972 bool ok, has_reduced_clock = false;
e9fd1c02 5973 bool is_lvds = false, is_dsi = false;
5eddb70b 5974 struct intel_encoder *encoder;
d4906093 5975 const intel_limit_t *limit;
79e53945 5976
6c2b7c12 5977 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 5978 switch (encoder->type) {
79e53945
JB
5979 case INTEL_OUTPUT_LVDS:
5980 is_lvds = true;
5981 break;
e9fd1c02
JN
5982 case INTEL_OUTPUT_DSI:
5983 is_dsi = true;
5984 break;
79e53945 5985 }
43565a06 5986
c751ce4f 5987 num_connectors++;
79e53945
JB
5988 }
5989
f2335330 5990 if (is_dsi)
5b18e57c 5991 return 0;
f2335330
JN
5992
5993 if (!intel_crtc->config.clock_set) {
5994 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 5995
e9fd1c02
JN
5996 /*
5997 * Returns a set of divisors for the desired target clock with
5998 * the given refclk, or FALSE. The returned values represent
5999 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6000 * 2) / p1 / p2.
6001 */
6002 limit = intel_limit(crtc, refclk);
6003 ok = dev_priv->display.find_dpll(limit, crtc,
6004 intel_crtc->config.port_clock,
6005 refclk, NULL, &clock);
f2335330 6006 if (!ok) {
e9fd1c02
JN
6007 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6008 return -EINVAL;
6009 }
79e53945 6010
f2335330
JN
6011 if (is_lvds && dev_priv->lvds_downclock_avail) {
6012 /*
6013 * Ensure we match the reduced clock's P to the target
6014 * clock. If the clocks don't match, we can't switch
6015 * the display clock by using the FP0/FP1. In such case
6016 * we will disable the LVDS downclock feature.
6017 */
6018 has_reduced_clock =
6019 dev_priv->display.find_dpll(limit, crtc,
6020 dev_priv->lvds_downclock,
6021 refclk, &clock,
6022 &reduced_clock);
6023 }
6024 /* Compat-code for transition, will disappear. */
f47709a9
DV
6025 intel_crtc->config.dpll.n = clock.n;
6026 intel_crtc->config.dpll.m1 = clock.m1;
6027 intel_crtc->config.dpll.m2 = clock.m2;
6028 intel_crtc->config.dpll.p1 = clock.p1;
6029 intel_crtc->config.dpll.p2 = clock.p2;
6030 }
7026d4ac 6031
e9fd1c02 6032 if (IS_GEN2(dev)) {
8a654f3b 6033 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6034 has_reduced_clock ? &reduced_clock : NULL,
6035 num_connectors);
9d556c99
CML
6036 } else if (IS_CHERRYVIEW(dev)) {
6037 chv_update_pll(intel_crtc);
e9fd1c02 6038 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6039 vlv_update_pll(intel_crtc);
e9fd1c02 6040 } else {
f47709a9 6041 i9xx_update_pll(intel_crtc,
eb1cbe48 6042 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6043 num_connectors);
e9fd1c02 6044 }
79e53945 6045
c8f7a0db 6046 return 0;
f564048e
EA
6047}
6048
2fa2fe9a
DV
6049static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6050 struct intel_crtc_config *pipe_config)
6051{
6052 struct drm_device *dev = crtc->base.dev;
6053 struct drm_i915_private *dev_priv = dev->dev_private;
6054 uint32_t tmp;
6055
dc9e7dec
VS
6056 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6057 return;
6058
2fa2fe9a 6059 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6060 if (!(tmp & PFIT_ENABLE))
6061 return;
2fa2fe9a 6062
06922821 6063 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6064 if (INTEL_INFO(dev)->gen < 4) {
6065 if (crtc->pipe != PIPE_B)
6066 return;
2fa2fe9a
DV
6067 } else {
6068 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6069 return;
6070 }
6071
06922821 6072 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6073 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6074 if (INTEL_INFO(dev)->gen < 5)
6075 pipe_config->gmch_pfit.lvds_border_bits =
6076 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6077}
6078
acbec814
JB
6079static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6080 struct intel_crtc_config *pipe_config)
6081{
6082 struct drm_device *dev = crtc->base.dev;
6083 struct drm_i915_private *dev_priv = dev->dev_private;
6084 int pipe = pipe_config->cpu_transcoder;
6085 intel_clock_t clock;
6086 u32 mdiv;
662c6ecb 6087 int refclk = 100000;
acbec814
JB
6088
6089 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6090 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6091 mutex_unlock(&dev_priv->dpio_lock);
6092
6093 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6094 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6095 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6096 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6097 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6098
f646628b 6099 vlv_clock(refclk, &clock);
acbec814 6100
f646628b
VS
6101 /* clock.dot is the fast clock */
6102 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6103}
6104
1ad292b5
JB
6105static void i9xx_get_plane_config(struct intel_crtc *crtc,
6106 struct intel_plane_config *plane_config)
6107{
6108 struct drm_device *dev = crtc->base.dev;
6109 struct drm_i915_private *dev_priv = dev->dev_private;
6110 u32 val, base, offset;
6111 int pipe = crtc->pipe, plane = crtc->plane;
6112 int fourcc, pixel_format;
6113 int aligned_height;
6114
66e514c1
DA
6115 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6116 if (!crtc->base.primary->fb) {
1ad292b5
JB
6117 DRM_DEBUG_KMS("failed to alloc fb\n");
6118 return;
6119 }
6120
6121 val = I915_READ(DSPCNTR(plane));
6122
6123 if (INTEL_INFO(dev)->gen >= 4)
6124 if (val & DISPPLANE_TILED)
6125 plane_config->tiled = true;
6126
6127 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6128 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6129 crtc->base.primary->fb->pixel_format = fourcc;
6130 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6131 drm_format_plane_cpp(fourcc, 0) * 8;
6132
6133 if (INTEL_INFO(dev)->gen >= 4) {
6134 if (plane_config->tiled)
6135 offset = I915_READ(DSPTILEOFF(plane));
6136 else
6137 offset = I915_READ(DSPLINOFF(plane));
6138 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6139 } else {
6140 base = I915_READ(DSPADDR(plane));
6141 }
6142 plane_config->base = base;
6143
6144 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6145 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6146 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6147
6148 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 6149 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
1ad292b5 6150
66e514c1 6151 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6152 plane_config->tiled);
6153
66e514c1 6154 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
1ad292b5
JB
6155 aligned_height, PAGE_SIZE);
6156
6157 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6158 pipe, plane, crtc->base.primary->fb->width,
6159 crtc->base.primary->fb->height,
6160 crtc->base.primary->fb->bits_per_pixel, base,
6161 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6162 plane_config->size);
6163
6164}
6165
70b23a98
VS
6166static void chv_crtc_clock_get(struct intel_crtc *crtc,
6167 struct intel_crtc_config *pipe_config)
6168{
6169 struct drm_device *dev = crtc->base.dev;
6170 struct drm_i915_private *dev_priv = dev->dev_private;
6171 int pipe = pipe_config->cpu_transcoder;
6172 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6173 intel_clock_t clock;
6174 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6175 int refclk = 100000;
6176
6177 mutex_lock(&dev_priv->dpio_lock);
6178 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6179 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6180 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6181 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6182 mutex_unlock(&dev_priv->dpio_lock);
6183
6184 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6185 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6186 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6187 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6188 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6189
6190 chv_clock(refclk, &clock);
6191
6192 /* clock.dot is the fast clock */
6193 pipe_config->port_clock = clock.dot / 5;
6194}
6195
0e8ffe1b
DV
6196static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6197 struct intel_crtc_config *pipe_config)
6198{
6199 struct drm_device *dev = crtc->base.dev;
6200 struct drm_i915_private *dev_priv = dev->dev_private;
6201 uint32_t tmp;
6202
b5482bd0
ID
6203 if (!intel_display_power_enabled(dev_priv,
6204 POWER_DOMAIN_PIPE(crtc->pipe)))
6205 return false;
6206
e143a21c 6207 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6208 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6209
0e8ffe1b
DV
6210 tmp = I915_READ(PIPECONF(crtc->pipe));
6211 if (!(tmp & PIPECONF_ENABLE))
6212 return false;
6213
42571aef
VS
6214 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6215 switch (tmp & PIPECONF_BPC_MASK) {
6216 case PIPECONF_6BPC:
6217 pipe_config->pipe_bpp = 18;
6218 break;
6219 case PIPECONF_8BPC:
6220 pipe_config->pipe_bpp = 24;
6221 break;
6222 case PIPECONF_10BPC:
6223 pipe_config->pipe_bpp = 30;
6224 break;
6225 default:
6226 break;
6227 }
6228 }
6229
b5a9fa09
DV
6230 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6231 pipe_config->limited_color_range = true;
6232
282740f7
VS
6233 if (INTEL_INFO(dev)->gen < 4)
6234 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6235
1bd1bd80
DV
6236 intel_get_pipe_timings(crtc, pipe_config);
6237
2fa2fe9a
DV
6238 i9xx_get_pfit_config(crtc, pipe_config);
6239
6c49f241
DV
6240 if (INTEL_INFO(dev)->gen >= 4) {
6241 tmp = I915_READ(DPLL_MD(crtc->pipe));
6242 pipe_config->pixel_multiplier =
6243 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6244 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6245 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6246 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6247 tmp = I915_READ(DPLL(crtc->pipe));
6248 pipe_config->pixel_multiplier =
6249 ((tmp & SDVO_MULTIPLIER_MASK)
6250 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6251 } else {
6252 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6253 * port and will be fixed up in the encoder->get_config
6254 * function. */
6255 pipe_config->pixel_multiplier = 1;
6256 }
8bcc2795
DV
6257 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6258 if (!IS_VALLEYVIEW(dev)) {
6259 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6260 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6261 } else {
6262 /* Mask out read-only status bits. */
6263 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6264 DPLL_PORTC_READY_MASK |
6265 DPLL_PORTB_READY_MASK);
8bcc2795 6266 }
6c49f241 6267
70b23a98
VS
6268 if (IS_CHERRYVIEW(dev))
6269 chv_crtc_clock_get(crtc, pipe_config);
6270 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6271 vlv_crtc_clock_get(crtc, pipe_config);
6272 else
6273 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6274
0e8ffe1b
DV
6275 return true;
6276}
6277
dde86e2d 6278static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6279{
6280 struct drm_i915_private *dev_priv = dev->dev_private;
6281 struct drm_mode_config *mode_config = &dev->mode_config;
13d83a67 6282 struct intel_encoder *encoder;
74cfd7ac 6283 u32 val, final;
13d83a67 6284 bool has_lvds = false;
199e5d79 6285 bool has_cpu_edp = false;
199e5d79 6286 bool has_panel = false;
99eb6a01
KP
6287 bool has_ck505 = false;
6288 bool can_ssc = false;
13d83a67
JB
6289
6290 /* We need to take the global config into account */
199e5d79
KP
6291 list_for_each_entry(encoder, &mode_config->encoder_list,
6292 base.head) {
6293 switch (encoder->type) {
6294 case INTEL_OUTPUT_LVDS:
6295 has_panel = true;
6296 has_lvds = true;
6297 break;
6298 case INTEL_OUTPUT_EDP:
6299 has_panel = true;
2de6905f 6300 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6301 has_cpu_edp = true;
6302 break;
13d83a67
JB
6303 }
6304 }
6305
99eb6a01 6306 if (HAS_PCH_IBX(dev)) {
41aa3448 6307 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6308 can_ssc = has_ck505;
6309 } else {
6310 has_ck505 = false;
6311 can_ssc = true;
6312 }
6313
2de6905f
ID
6314 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6315 has_panel, has_lvds, has_ck505);
13d83a67
JB
6316
6317 /* Ironlake: try to setup display ref clock before DPLL
6318 * enabling. This is only under driver's control after
6319 * PCH B stepping, previous chipset stepping should be
6320 * ignoring this setting.
6321 */
74cfd7ac
CW
6322 val = I915_READ(PCH_DREF_CONTROL);
6323
6324 /* As we must carefully and slowly disable/enable each source in turn,
6325 * compute the final state we want first and check if we need to
6326 * make any changes at all.
6327 */
6328 final = val;
6329 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6330 if (has_ck505)
6331 final |= DREF_NONSPREAD_CK505_ENABLE;
6332 else
6333 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6334
6335 final &= ~DREF_SSC_SOURCE_MASK;
6336 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6337 final &= ~DREF_SSC1_ENABLE;
6338
6339 if (has_panel) {
6340 final |= DREF_SSC_SOURCE_ENABLE;
6341
6342 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6343 final |= DREF_SSC1_ENABLE;
6344
6345 if (has_cpu_edp) {
6346 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6347 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6348 else
6349 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6350 } else
6351 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6352 } else {
6353 final |= DREF_SSC_SOURCE_DISABLE;
6354 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6355 }
6356
6357 if (final == val)
6358 return;
6359
13d83a67 6360 /* Always enable nonspread source */
74cfd7ac 6361 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6362
99eb6a01 6363 if (has_ck505)
74cfd7ac 6364 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6365 else
74cfd7ac 6366 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6367
199e5d79 6368 if (has_panel) {
74cfd7ac
CW
6369 val &= ~DREF_SSC_SOURCE_MASK;
6370 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6371
199e5d79 6372 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6373 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6374 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6375 val |= DREF_SSC1_ENABLE;
e77166b5 6376 } else
74cfd7ac 6377 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6378
6379 /* Get SSC going before enabling the outputs */
74cfd7ac 6380 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6381 POSTING_READ(PCH_DREF_CONTROL);
6382 udelay(200);
6383
74cfd7ac 6384 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6385
6386 /* Enable CPU source on CPU attached eDP */
199e5d79 6387 if (has_cpu_edp) {
99eb6a01 6388 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6389 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6390 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6391 } else
74cfd7ac 6392 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6393 } else
74cfd7ac 6394 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6395
74cfd7ac 6396 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6397 POSTING_READ(PCH_DREF_CONTROL);
6398 udelay(200);
6399 } else {
6400 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6401
74cfd7ac 6402 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6403
6404 /* Turn off CPU output */
74cfd7ac 6405 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6406
74cfd7ac 6407 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6408 POSTING_READ(PCH_DREF_CONTROL);
6409 udelay(200);
6410
6411 /* Turn off the SSC source */
74cfd7ac
CW
6412 val &= ~DREF_SSC_SOURCE_MASK;
6413 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6414
6415 /* Turn off SSC1 */
74cfd7ac 6416 val &= ~DREF_SSC1_ENABLE;
199e5d79 6417
74cfd7ac 6418 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6419 POSTING_READ(PCH_DREF_CONTROL);
6420 udelay(200);
6421 }
74cfd7ac
CW
6422
6423 BUG_ON(val != final);
13d83a67
JB
6424}
6425
f31f2d55 6426static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6427{
f31f2d55 6428 uint32_t tmp;
dde86e2d 6429
0ff066a9
PZ
6430 tmp = I915_READ(SOUTH_CHICKEN2);
6431 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6432 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6433
0ff066a9
PZ
6434 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6435 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6436 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6437
0ff066a9
PZ
6438 tmp = I915_READ(SOUTH_CHICKEN2);
6439 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6440 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6441
0ff066a9
PZ
6442 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6443 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6444 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6445}
6446
6447/* WaMPhyProgramming:hsw */
6448static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6449{
6450 uint32_t tmp;
dde86e2d
PZ
6451
6452 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6453 tmp &= ~(0xFF << 24);
6454 tmp |= (0x12 << 24);
6455 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6456
dde86e2d
PZ
6457 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6458 tmp |= (1 << 11);
6459 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6460
6461 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6462 tmp |= (1 << 11);
6463 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6464
dde86e2d
PZ
6465 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6466 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6467 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6468
6469 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6470 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6471 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6472
0ff066a9
PZ
6473 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6474 tmp &= ~(7 << 13);
6475 tmp |= (5 << 13);
6476 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6477
0ff066a9
PZ
6478 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6479 tmp &= ~(7 << 13);
6480 tmp |= (5 << 13);
6481 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6482
6483 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6484 tmp &= ~0xFF;
6485 tmp |= 0x1C;
6486 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6487
6488 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6489 tmp &= ~0xFF;
6490 tmp |= 0x1C;
6491 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6492
6493 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6494 tmp &= ~(0xFF << 16);
6495 tmp |= (0x1C << 16);
6496 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6497
6498 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6499 tmp &= ~(0xFF << 16);
6500 tmp |= (0x1C << 16);
6501 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6502
0ff066a9
PZ
6503 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6504 tmp |= (1 << 27);
6505 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6506
0ff066a9
PZ
6507 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6508 tmp |= (1 << 27);
6509 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6510
0ff066a9
PZ
6511 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6512 tmp &= ~(0xF << 28);
6513 tmp |= (4 << 28);
6514 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6515
0ff066a9
PZ
6516 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6517 tmp &= ~(0xF << 28);
6518 tmp |= (4 << 28);
6519 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6520}
6521
2fa86a1f
PZ
6522/* Implements 3 different sequences from BSpec chapter "Display iCLK
6523 * Programming" based on the parameters passed:
6524 * - Sequence to enable CLKOUT_DP
6525 * - Sequence to enable CLKOUT_DP without spread
6526 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6527 */
6528static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6529 bool with_fdi)
f31f2d55
PZ
6530{
6531 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6532 uint32_t reg, tmp;
6533
6534 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6535 with_spread = true;
6536 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6537 with_fdi, "LP PCH doesn't have FDI\n"))
6538 with_fdi = false;
f31f2d55
PZ
6539
6540 mutex_lock(&dev_priv->dpio_lock);
6541
6542 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6543 tmp &= ~SBI_SSCCTL_DISABLE;
6544 tmp |= SBI_SSCCTL_PATHALT;
6545 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6546
6547 udelay(24);
6548
2fa86a1f
PZ
6549 if (with_spread) {
6550 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6551 tmp &= ~SBI_SSCCTL_PATHALT;
6552 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6553
2fa86a1f
PZ
6554 if (with_fdi) {
6555 lpt_reset_fdi_mphy(dev_priv);
6556 lpt_program_fdi_mphy(dev_priv);
6557 }
6558 }
dde86e2d 6559
2fa86a1f
PZ
6560 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6561 SBI_GEN0 : SBI_DBUFF0;
6562 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6563 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6564 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6565
6566 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6567}
6568
47701c3b
PZ
6569/* Sequence to disable CLKOUT_DP */
6570static void lpt_disable_clkout_dp(struct drm_device *dev)
6571{
6572 struct drm_i915_private *dev_priv = dev->dev_private;
6573 uint32_t reg, tmp;
6574
6575 mutex_lock(&dev_priv->dpio_lock);
6576
6577 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6578 SBI_GEN0 : SBI_DBUFF0;
6579 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6580 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6581 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6582
6583 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6584 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6585 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6586 tmp |= SBI_SSCCTL_PATHALT;
6587 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6588 udelay(32);
6589 }
6590 tmp |= SBI_SSCCTL_DISABLE;
6591 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6592 }
6593
6594 mutex_unlock(&dev_priv->dpio_lock);
6595}
6596
bf8fa3d3
PZ
6597static void lpt_init_pch_refclk(struct drm_device *dev)
6598{
6599 struct drm_mode_config *mode_config = &dev->mode_config;
6600 struct intel_encoder *encoder;
6601 bool has_vga = false;
6602
6603 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6604 switch (encoder->type) {
6605 case INTEL_OUTPUT_ANALOG:
6606 has_vga = true;
6607 break;
6608 }
6609 }
6610
47701c3b
PZ
6611 if (has_vga)
6612 lpt_enable_clkout_dp(dev, true, true);
6613 else
6614 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6615}
6616
dde86e2d
PZ
6617/*
6618 * Initialize reference clocks when the driver loads
6619 */
6620void intel_init_pch_refclk(struct drm_device *dev)
6621{
6622 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6623 ironlake_init_pch_refclk(dev);
6624 else if (HAS_PCH_LPT(dev))
6625 lpt_init_pch_refclk(dev);
6626}
6627
d9d444cb
JB
6628static int ironlake_get_refclk(struct drm_crtc *crtc)
6629{
6630 struct drm_device *dev = crtc->dev;
6631 struct drm_i915_private *dev_priv = dev->dev_private;
6632 struct intel_encoder *encoder;
d9d444cb
JB
6633 int num_connectors = 0;
6634 bool is_lvds = false;
6635
6c2b7c12 6636 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6637 switch (encoder->type) {
6638 case INTEL_OUTPUT_LVDS:
6639 is_lvds = true;
6640 break;
d9d444cb
JB
6641 }
6642 num_connectors++;
6643 }
6644
6645 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6646 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6647 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6648 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6649 }
6650
6651 return 120000;
6652}
6653
6ff93609 6654static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6655{
c8203565 6656 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6658 int pipe = intel_crtc->pipe;
c8203565
PZ
6659 uint32_t val;
6660
78114071 6661 val = 0;
c8203565 6662
965e0c48 6663 switch (intel_crtc->config.pipe_bpp) {
c8203565 6664 case 18:
dfd07d72 6665 val |= PIPECONF_6BPC;
c8203565
PZ
6666 break;
6667 case 24:
dfd07d72 6668 val |= PIPECONF_8BPC;
c8203565
PZ
6669 break;
6670 case 30:
dfd07d72 6671 val |= PIPECONF_10BPC;
c8203565
PZ
6672 break;
6673 case 36:
dfd07d72 6674 val |= PIPECONF_12BPC;
c8203565
PZ
6675 break;
6676 default:
cc769b62
PZ
6677 /* Case prevented by intel_choose_pipe_bpp_dither. */
6678 BUG();
c8203565
PZ
6679 }
6680
d8b32247 6681 if (intel_crtc->config.dither)
c8203565
PZ
6682 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6683
6ff93609 6684 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6685 val |= PIPECONF_INTERLACED_ILK;
6686 else
6687 val |= PIPECONF_PROGRESSIVE;
6688
50f3b016 6689 if (intel_crtc->config.limited_color_range)
3685a8f3 6690 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6691
c8203565
PZ
6692 I915_WRITE(PIPECONF(pipe), val);
6693 POSTING_READ(PIPECONF(pipe));
6694}
6695
86d3efce
VS
6696/*
6697 * Set up the pipe CSC unit.
6698 *
6699 * Currently only full range RGB to limited range RGB conversion
6700 * is supported, but eventually this should handle various
6701 * RGB<->YCbCr scenarios as well.
6702 */
50f3b016 6703static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6704{
6705 struct drm_device *dev = crtc->dev;
6706 struct drm_i915_private *dev_priv = dev->dev_private;
6707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6708 int pipe = intel_crtc->pipe;
6709 uint16_t coeff = 0x7800; /* 1.0 */
6710
6711 /*
6712 * TODO: Check what kind of values actually come out of the pipe
6713 * with these coeff/postoff values and adjust to get the best
6714 * accuracy. Perhaps we even need to take the bpc value into
6715 * consideration.
6716 */
6717
50f3b016 6718 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6719 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6720
6721 /*
6722 * GY/GU and RY/RU should be the other way around according
6723 * to BSpec, but reality doesn't agree. Just set them up in
6724 * a way that results in the correct picture.
6725 */
6726 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6727 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6728
6729 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6730 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6731
6732 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6733 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6734
6735 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6736 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6737 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6738
6739 if (INTEL_INFO(dev)->gen > 6) {
6740 uint16_t postoff = 0;
6741
50f3b016 6742 if (intel_crtc->config.limited_color_range)
32cf0cb0 6743 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6744
6745 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6746 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6747 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6748
6749 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6750 } else {
6751 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6752
50f3b016 6753 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6754 mode |= CSC_BLACK_SCREEN_OFFSET;
6755
6756 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6757 }
6758}
6759
6ff93609 6760static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6761{
756f85cf
PZ
6762 struct drm_device *dev = crtc->dev;
6763 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6765 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6766 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6767 uint32_t val;
6768
3eff4faa 6769 val = 0;
ee2b0b38 6770
756f85cf 6771 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6772 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6773
6ff93609 6774 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6775 val |= PIPECONF_INTERLACED_ILK;
6776 else
6777 val |= PIPECONF_PROGRESSIVE;
6778
702e7a56
PZ
6779 I915_WRITE(PIPECONF(cpu_transcoder), val);
6780 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6781
6782 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6783 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6784
6785 if (IS_BROADWELL(dev)) {
6786 val = 0;
6787
6788 switch (intel_crtc->config.pipe_bpp) {
6789 case 18:
6790 val |= PIPEMISC_DITHER_6_BPC;
6791 break;
6792 case 24:
6793 val |= PIPEMISC_DITHER_8_BPC;
6794 break;
6795 case 30:
6796 val |= PIPEMISC_DITHER_10_BPC;
6797 break;
6798 case 36:
6799 val |= PIPEMISC_DITHER_12_BPC;
6800 break;
6801 default:
6802 /* Case prevented by pipe_config_set_bpp. */
6803 BUG();
6804 }
6805
6806 if (intel_crtc->config.dither)
6807 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6808
6809 I915_WRITE(PIPEMISC(pipe), val);
6810 }
ee2b0b38
PZ
6811}
6812
6591c6e4 6813static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6814 intel_clock_t *clock,
6815 bool *has_reduced_clock,
6816 intel_clock_t *reduced_clock)
6817{
6818 struct drm_device *dev = crtc->dev;
6819 struct drm_i915_private *dev_priv = dev->dev_private;
6820 struct intel_encoder *intel_encoder;
6821 int refclk;
d4906093 6822 const intel_limit_t *limit;
a16af721 6823 bool ret, is_lvds = false;
79e53945 6824
6591c6e4
PZ
6825 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6826 switch (intel_encoder->type) {
79e53945
JB
6827 case INTEL_OUTPUT_LVDS:
6828 is_lvds = true;
6829 break;
79e53945
JB
6830 }
6831 }
6832
d9d444cb 6833 refclk = ironlake_get_refclk(crtc);
79e53945 6834
d4906093
ML
6835 /*
6836 * Returns a set of divisors for the desired target clock with the given
6837 * refclk, or FALSE. The returned values represent the clock equation:
6838 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6839 */
1b894b59 6840 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6841 ret = dev_priv->display.find_dpll(limit, crtc,
6842 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6843 refclk, NULL, clock);
6591c6e4
PZ
6844 if (!ret)
6845 return false;
cda4b7d3 6846
ddc9003c 6847 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6848 /*
6849 * Ensure we match the reduced clock's P to the target clock.
6850 * If the clocks don't match, we can't switch the display clock
6851 * by using the FP0/FP1. In such case we will disable the LVDS
6852 * downclock feature.
6853 */
ee9300bb
DV
6854 *has_reduced_clock =
6855 dev_priv->display.find_dpll(limit, crtc,
6856 dev_priv->lvds_downclock,
6857 refclk, clock,
6858 reduced_clock);
652c393a 6859 }
61e9653f 6860
6591c6e4
PZ
6861 return true;
6862}
6863
d4b1931c
PZ
6864int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6865{
6866 /*
6867 * Account for spread spectrum to avoid
6868 * oversubscribing the link. Max center spread
6869 * is 2.5%; use 5% for safety's sake.
6870 */
6871 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 6872 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
6873}
6874
7429e9d4 6875static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 6876{
7429e9d4 6877 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
6878}
6879
de13a2e3 6880static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 6881 u32 *fp,
9a7c7890 6882 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 6883{
de13a2e3 6884 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
6885 struct drm_device *dev = crtc->dev;
6886 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
6887 struct intel_encoder *intel_encoder;
6888 uint32_t dpll;
6cc5f341 6889 int factor, num_connectors = 0;
09ede541 6890 bool is_lvds = false, is_sdvo = false;
79e53945 6891
de13a2e3
PZ
6892 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6893 switch (intel_encoder->type) {
79e53945
JB
6894 case INTEL_OUTPUT_LVDS:
6895 is_lvds = true;
6896 break;
6897 case INTEL_OUTPUT_SDVO:
7d57382e 6898 case INTEL_OUTPUT_HDMI:
79e53945 6899 is_sdvo = true;
79e53945 6900 break;
79e53945 6901 }
43565a06 6902
c751ce4f 6903 num_connectors++;
79e53945 6904 }
79e53945 6905
c1858123 6906 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
6907 factor = 21;
6908 if (is_lvds) {
6909 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 6910 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 6911 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 6912 factor = 25;
09ede541 6913 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 6914 factor = 20;
c1858123 6915
7429e9d4 6916 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 6917 *fp |= FP_CB_TUNE;
2c07245f 6918
9a7c7890
DV
6919 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6920 *fp2 |= FP_CB_TUNE;
6921
5eddb70b 6922 dpll = 0;
2c07245f 6923
a07d6787
EA
6924 if (is_lvds)
6925 dpll |= DPLLB_MODE_LVDS;
6926 else
6927 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 6928
ef1b460d
DV
6929 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6930 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
6931
6932 if (is_sdvo)
4a33e48d 6933 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 6934 if (intel_crtc->config.has_dp_encoder)
4a33e48d 6935 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 6936
a07d6787 6937 /* compute bitmask from p1 value */
7429e9d4 6938 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 6939 /* also FPA1 */
7429e9d4 6940 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 6941
7429e9d4 6942 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
6943 case 5:
6944 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6945 break;
6946 case 7:
6947 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6948 break;
6949 case 10:
6950 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6951 break;
6952 case 14:
6953 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6954 break;
79e53945
JB
6955 }
6956
b4c09f3b 6957 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 6958 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
6959 else
6960 dpll |= PLL_REF_INPUT_DREFCLK;
6961
959e16d6 6962 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
6963}
6964
6965static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
6966 int x, int y,
6967 struct drm_framebuffer *fb)
6968{
6969 struct drm_device *dev = crtc->dev;
de13a2e3 6970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
6971 int num_connectors = 0;
6972 intel_clock_t clock, reduced_clock;
cbbab5bd 6973 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 6974 bool ok, has_reduced_clock = false;
8b47047b 6975 bool is_lvds = false;
de13a2e3 6976 struct intel_encoder *encoder;
e2b78267 6977 struct intel_shared_dpll *pll;
de13a2e3
PZ
6978
6979 for_each_encoder_on_crtc(dev, crtc, encoder) {
6980 switch (encoder->type) {
6981 case INTEL_OUTPUT_LVDS:
6982 is_lvds = true;
6983 break;
de13a2e3
PZ
6984 }
6985
6986 num_connectors++;
a07d6787 6987 }
79e53945 6988
5dc5298b
PZ
6989 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6990 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 6991
ff9a6750 6992 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 6993 &has_reduced_clock, &reduced_clock);
ee9300bb 6994 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
6995 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6996 return -EINVAL;
79e53945 6997 }
f47709a9
DV
6998 /* Compat-code for transition, will disappear. */
6999 if (!intel_crtc->config.clock_set) {
7000 intel_crtc->config.dpll.n = clock.n;
7001 intel_crtc->config.dpll.m1 = clock.m1;
7002 intel_crtc->config.dpll.m2 = clock.m2;
7003 intel_crtc->config.dpll.p1 = clock.p1;
7004 intel_crtc->config.dpll.p2 = clock.p2;
7005 }
79e53945 7006
5dc5298b 7007 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7008 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7009 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7010 if (has_reduced_clock)
7429e9d4 7011 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7012
7429e9d4 7013 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7014 &fp, &reduced_clock,
7015 has_reduced_clock ? &fp2 : NULL);
7016
959e16d6 7017 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7018 intel_crtc->config.dpll_hw_state.fp0 = fp;
7019 if (has_reduced_clock)
7020 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7021 else
7022 intel_crtc->config.dpll_hw_state.fp1 = fp;
7023
b89a1d39 7024 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7025 if (pll == NULL) {
84f44ce7 7026 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7027 pipe_name(intel_crtc->pipe));
4b645f14
JB
7028 return -EINVAL;
7029 }
ee7b9f93 7030 } else
e72f9fbf 7031 intel_put_shared_dpll(intel_crtc);
79e53945 7032
d330a953 7033 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7034 intel_crtc->lowfreq_avail = true;
7035 else
7036 intel_crtc->lowfreq_avail = false;
e2b78267 7037
c8f7a0db 7038 return 0;
79e53945
JB
7039}
7040
eb14cb74
VS
7041static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7042 struct intel_link_m_n *m_n)
7043{
7044 struct drm_device *dev = crtc->base.dev;
7045 struct drm_i915_private *dev_priv = dev->dev_private;
7046 enum pipe pipe = crtc->pipe;
7047
7048 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7049 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7050 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7051 & ~TU_SIZE_MASK;
7052 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7053 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7054 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7055}
7056
7057static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7058 enum transcoder transcoder,
7059 struct intel_link_m_n *m_n)
72419203
DV
7060{
7061 struct drm_device *dev = crtc->base.dev;
7062 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7063 enum pipe pipe = crtc->pipe;
72419203 7064
eb14cb74
VS
7065 if (INTEL_INFO(dev)->gen >= 5) {
7066 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7067 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7068 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7069 & ~TU_SIZE_MASK;
7070 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7071 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7072 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7073 } else {
7074 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7075 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7076 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7077 & ~TU_SIZE_MASK;
7078 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7079 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7080 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7081 }
7082}
7083
7084void intel_dp_get_m_n(struct intel_crtc *crtc,
7085 struct intel_crtc_config *pipe_config)
7086{
7087 if (crtc->config.has_pch_encoder)
7088 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7089 else
7090 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7091 &pipe_config->dp_m_n);
7092}
72419203 7093
eb14cb74
VS
7094static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7095 struct intel_crtc_config *pipe_config)
7096{
7097 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7098 &pipe_config->fdi_m_n);
72419203
DV
7099}
7100
2fa2fe9a
DV
7101static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7102 struct intel_crtc_config *pipe_config)
7103{
7104 struct drm_device *dev = crtc->base.dev;
7105 struct drm_i915_private *dev_priv = dev->dev_private;
7106 uint32_t tmp;
7107
7108 tmp = I915_READ(PF_CTL(crtc->pipe));
7109
7110 if (tmp & PF_ENABLE) {
fd4daa9c 7111 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7112 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7113 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7114
7115 /* We currently do not free assignements of panel fitters on
7116 * ivb/hsw (since we don't use the higher upscaling modes which
7117 * differentiates them) so just WARN about this case for now. */
7118 if (IS_GEN7(dev)) {
7119 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7120 PF_PIPE_SEL_IVB(crtc->pipe));
7121 }
2fa2fe9a 7122 }
79e53945
JB
7123}
7124
4c6baa59
JB
7125static void ironlake_get_plane_config(struct intel_crtc *crtc,
7126 struct intel_plane_config *plane_config)
7127{
7128 struct drm_device *dev = crtc->base.dev;
7129 struct drm_i915_private *dev_priv = dev->dev_private;
7130 u32 val, base, offset;
7131 int pipe = crtc->pipe, plane = crtc->plane;
7132 int fourcc, pixel_format;
7133 int aligned_height;
7134
66e514c1
DA
7135 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7136 if (!crtc->base.primary->fb) {
4c6baa59
JB
7137 DRM_DEBUG_KMS("failed to alloc fb\n");
7138 return;
7139 }
7140
7141 val = I915_READ(DSPCNTR(plane));
7142
7143 if (INTEL_INFO(dev)->gen >= 4)
7144 if (val & DISPPLANE_TILED)
7145 plane_config->tiled = true;
7146
7147 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7148 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7149 crtc->base.primary->fb->pixel_format = fourcc;
7150 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7151 drm_format_plane_cpp(fourcc, 0) * 8;
7152
7153 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7154 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7155 offset = I915_READ(DSPOFFSET(plane));
7156 } else {
7157 if (plane_config->tiled)
7158 offset = I915_READ(DSPTILEOFF(plane));
7159 else
7160 offset = I915_READ(DSPLINOFF(plane));
7161 }
7162 plane_config->base = base;
7163
7164 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7165 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7166 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7167
7168 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 7169 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
4c6baa59 7170
66e514c1 7171 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7172 plane_config->tiled);
7173
66e514c1 7174 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
4c6baa59
JB
7175 aligned_height, PAGE_SIZE);
7176
7177 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7178 pipe, plane, crtc->base.primary->fb->width,
7179 crtc->base.primary->fb->height,
7180 crtc->base.primary->fb->bits_per_pixel, base,
7181 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7182 plane_config->size);
7183}
7184
0e8ffe1b
DV
7185static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7186 struct intel_crtc_config *pipe_config)
7187{
7188 struct drm_device *dev = crtc->base.dev;
7189 struct drm_i915_private *dev_priv = dev->dev_private;
7190 uint32_t tmp;
7191
e143a21c 7192 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7193 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7194
0e8ffe1b
DV
7195 tmp = I915_READ(PIPECONF(crtc->pipe));
7196 if (!(tmp & PIPECONF_ENABLE))
7197 return false;
7198
42571aef
VS
7199 switch (tmp & PIPECONF_BPC_MASK) {
7200 case PIPECONF_6BPC:
7201 pipe_config->pipe_bpp = 18;
7202 break;
7203 case PIPECONF_8BPC:
7204 pipe_config->pipe_bpp = 24;
7205 break;
7206 case PIPECONF_10BPC:
7207 pipe_config->pipe_bpp = 30;
7208 break;
7209 case PIPECONF_12BPC:
7210 pipe_config->pipe_bpp = 36;
7211 break;
7212 default:
7213 break;
7214 }
7215
b5a9fa09
DV
7216 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7217 pipe_config->limited_color_range = true;
7218
ab9412ba 7219 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7220 struct intel_shared_dpll *pll;
7221
88adfff1
DV
7222 pipe_config->has_pch_encoder = true;
7223
627eb5a3
DV
7224 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7225 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7226 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7227
7228 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7229
c0d43d62 7230 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7231 pipe_config->shared_dpll =
7232 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7233 } else {
7234 tmp = I915_READ(PCH_DPLL_SEL);
7235 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7236 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7237 else
7238 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7239 }
66e985c0
DV
7240
7241 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7242
7243 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7244 &pipe_config->dpll_hw_state));
c93f54cf
DV
7245
7246 tmp = pipe_config->dpll_hw_state.dpll;
7247 pipe_config->pixel_multiplier =
7248 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7249 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7250
7251 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7252 } else {
7253 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7254 }
7255
1bd1bd80
DV
7256 intel_get_pipe_timings(crtc, pipe_config);
7257
2fa2fe9a
DV
7258 ironlake_get_pfit_config(crtc, pipe_config);
7259
0e8ffe1b
DV
7260 return true;
7261}
7262
be256dc7
PZ
7263static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7264{
7265 struct drm_device *dev = dev_priv->dev;
7266 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7267 struct intel_crtc *crtc;
be256dc7 7268
d3fcc808 7269 for_each_intel_crtc(dev, crtc)
798183c5 7270 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7271 pipe_name(crtc->pipe));
7272
7273 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7274 WARN(plls->spll_refcount, "SPLL enabled\n");
7275 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7276 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7277 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7278 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7279 "CPU PWM1 enabled\n");
7280 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7281 "CPU PWM2 enabled\n");
7282 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7283 "PCH PWM1 enabled\n");
7284 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7285 "Utility pin enabled\n");
7286 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7287
9926ada1
PZ
7288 /*
7289 * In theory we can still leave IRQs enabled, as long as only the HPD
7290 * interrupts remain enabled. We used to check for that, but since it's
7291 * gen-specific and since we only disable LCPLL after we fully disable
7292 * the interrupts, the check below should be enough.
7293 */
7294 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
be256dc7
PZ
7295}
7296
3c4c9b81
PZ
7297static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7298{
7299 struct drm_device *dev = dev_priv->dev;
7300
7301 if (IS_HASWELL(dev)) {
7302 mutex_lock(&dev_priv->rps.hw_lock);
7303 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7304 val))
7305 DRM_ERROR("Failed to disable D_COMP\n");
7306 mutex_unlock(&dev_priv->rps.hw_lock);
7307 } else {
7308 I915_WRITE(D_COMP, val);
7309 }
7310 POSTING_READ(D_COMP);
be256dc7
PZ
7311}
7312
7313/*
7314 * This function implements pieces of two sequences from BSpec:
7315 * - Sequence for display software to disable LCPLL
7316 * - Sequence for display software to allow package C8+
7317 * The steps implemented here are just the steps that actually touch the LCPLL
7318 * register. Callers should take care of disabling all the display engine
7319 * functions, doing the mode unset, fixing interrupts, etc.
7320 */
6ff58d53
PZ
7321static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7322 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7323{
7324 uint32_t val;
7325
7326 assert_can_disable_lcpll(dev_priv);
7327
7328 val = I915_READ(LCPLL_CTL);
7329
7330 if (switch_to_fclk) {
7331 val |= LCPLL_CD_SOURCE_FCLK;
7332 I915_WRITE(LCPLL_CTL, val);
7333
7334 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7335 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7336 DRM_ERROR("Switching to FCLK failed\n");
7337
7338 val = I915_READ(LCPLL_CTL);
7339 }
7340
7341 val |= LCPLL_PLL_DISABLE;
7342 I915_WRITE(LCPLL_CTL, val);
7343 POSTING_READ(LCPLL_CTL);
7344
7345 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7346 DRM_ERROR("LCPLL still locked\n");
7347
7348 val = I915_READ(D_COMP);
7349 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7350 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7351 ndelay(100);
7352
7353 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7354 DRM_ERROR("D_COMP RCOMP still in progress\n");
7355
7356 if (allow_power_down) {
7357 val = I915_READ(LCPLL_CTL);
7358 val |= LCPLL_POWER_DOWN_ALLOW;
7359 I915_WRITE(LCPLL_CTL, val);
7360 POSTING_READ(LCPLL_CTL);
7361 }
7362}
7363
7364/*
7365 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7366 * source.
7367 */
6ff58d53 7368static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7369{
7370 uint32_t val;
a8a8bd54 7371 unsigned long irqflags;
be256dc7
PZ
7372
7373 val = I915_READ(LCPLL_CTL);
7374
7375 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7376 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7377 return;
7378
a8a8bd54
PZ
7379 /*
7380 * Make sure we're not on PC8 state before disabling PC8, otherwise
7381 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7382 *
7383 * The other problem is that hsw_restore_lcpll() is called as part of
7384 * the runtime PM resume sequence, so we can't just call
7385 * gen6_gt_force_wake_get() because that function calls
7386 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7387 * while we are on the resume sequence. So to solve this problem we have
7388 * to call special forcewake code that doesn't touch runtime PM and
7389 * doesn't enable the forcewake delayed work.
7390 */
7391 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7392 if (dev_priv->uncore.forcewake_count++ == 0)
7393 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7394 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7395
be256dc7
PZ
7396 if (val & LCPLL_POWER_DOWN_ALLOW) {
7397 val &= ~LCPLL_POWER_DOWN_ALLOW;
7398 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7399 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7400 }
7401
7402 val = I915_READ(D_COMP);
7403 val |= D_COMP_COMP_FORCE;
7404 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7405 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7406
7407 val = I915_READ(LCPLL_CTL);
7408 val &= ~LCPLL_PLL_DISABLE;
7409 I915_WRITE(LCPLL_CTL, val);
7410
7411 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7412 DRM_ERROR("LCPLL not locked yet\n");
7413
7414 if (val & LCPLL_CD_SOURCE_FCLK) {
7415 val = I915_READ(LCPLL_CTL);
7416 val &= ~LCPLL_CD_SOURCE_FCLK;
7417 I915_WRITE(LCPLL_CTL, val);
7418
7419 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7420 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7421 DRM_ERROR("Switching back to LCPLL failed\n");
7422 }
215733fa 7423
a8a8bd54
PZ
7424 /* See the big comment above. */
7425 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7426 if (--dev_priv->uncore.forcewake_count == 0)
7427 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7428 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7429}
7430
765dab67
PZ
7431/*
7432 * Package states C8 and deeper are really deep PC states that can only be
7433 * reached when all the devices on the system allow it, so even if the graphics
7434 * device allows PC8+, it doesn't mean the system will actually get to these
7435 * states. Our driver only allows PC8+ when going into runtime PM.
7436 *
7437 * The requirements for PC8+ are that all the outputs are disabled, the power
7438 * well is disabled and most interrupts are disabled, and these are also
7439 * requirements for runtime PM. When these conditions are met, we manually do
7440 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7441 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7442 * hang the machine.
7443 *
7444 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7445 * the state of some registers, so when we come back from PC8+ we need to
7446 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7447 * need to take care of the registers kept by RC6. Notice that this happens even
7448 * if we don't put the device in PCI D3 state (which is what currently happens
7449 * because of the runtime PM support).
7450 *
7451 * For more, read "Display Sequences for Package C8" on the hardware
7452 * documentation.
7453 */
a14cb6fc 7454void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7455{
c67a470b
PZ
7456 struct drm_device *dev = dev_priv->dev;
7457 uint32_t val;
7458
c67a470b
PZ
7459 DRM_DEBUG_KMS("Enabling package C8+\n");
7460
c67a470b
PZ
7461 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7462 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7463 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7464 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7465 }
7466
7467 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7468 hsw_disable_lcpll(dev_priv, true, true);
7469}
7470
a14cb6fc 7471void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7472{
7473 struct drm_device *dev = dev_priv->dev;
7474 uint32_t val;
7475
c67a470b
PZ
7476 DRM_DEBUG_KMS("Disabling package C8+\n");
7477
7478 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7479 lpt_init_pch_refclk(dev);
7480
7481 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7482 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7483 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7484 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7485 }
7486
7487 intel_prepare_ddi(dev);
c67a470b
PZ
7488}
7489
9a952a0d
PZ
7490static void snb_modeset_global_resources(struct drm_device *dev)
7491{
7492 modeset_update_crtc_power_domains(dev);
7493}
7494
4f074129
ID
7495static void haswell_modeset_global_resources(struct drm_device *dev)
7496{
da723569 7497 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7498}
7499
09b4ddf9 7500static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7501 int x, int y,
7502 struct drm_framebuffer *fb)
7503{
09b4ddf9 7504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7505
566b734a 7506 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7507 return -EINVAL;
566b734a 7508 intel_ddi_pll_enable(intel_crtc);
6441ab5f 7509
644cef34
DV
7510 intel_crtc->lowfreq_avail = false;
7511
c8f7a0db 7512 return 0;
79e53945
JB
7513}
7514
0e8ffe1b
DV
7515static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7516 struct intel_crtc_config *pipe_config)
7517{
7518 struct drm_device *dev = crtc->base.dev;
7519 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7520 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7521 uint32_t tmp;
7522
b5482bd0
ID
7523 if (!intel_display_power_enabled(dev_priv,
7524 POWER_DOMAIN_PIPE(crtc->pipe)))
7525 return false;
7526
e143a21c 7527 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7528 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7529
eccb140b
DV
7530 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7531 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7532 enum pipe trans_edp_pipe;
7533 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7534 default:
7535 WARN(1, "unknown pipe linked to edp transcoder\n");
7536 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7537 case TRANS_DDI_EDP_INPUT_A_ON:
7538 trans_edp_pipe = PIPE_A;
7539 break;
7540 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7541 trans_edp_pipe = PIPE_B;
7542 break;
7543 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7544 trans_edp_pipe = PIPE_C;
7545 break;
7546 }
7547
7548 if (trans_edp_pipe == crtc->pipe)
7549 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7550 }
7551
da7e29bd 7552 if (!intel_display_power_enabled(dev_priv,
eccb140b 7553 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7554 return false;
7555
eccb140b 7556 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7557 if (!(tmp & PIPECONF_ENABLE))
7558 return false;
7559
88adfff1 7560 /*
f196e6be 7561 * Haswell has only FDI/PCH transcoder A. It is which is connected to
88adfff1
DV
7562 * DDI E. So just check whether this pipe is wired to DDI E and whether
7563 * the PCH transcoder is on.
7564 */
eccb140b 7565 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
88adfff1 7566 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
ab9412ba 7567 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
88adfff1
DV
7568 pipe_config->has_pch_encoder = true;
7569
627eb5a3
DV
7570 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7571 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7572 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7573
7574 ironlake_get_fdi_m_n_config(crtc, pipe_config);
627eb5a3
DV
7575 }
7576
1bd1bd80
DV
7577 intel_get_pipe_timings(crtc, pipe_config);
7578
2fa2fe9a 7579 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7580 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7581 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7582
e59150dc
JB
7583 if (IS_HASWELL(dev))
7584 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7585 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7586
6c49f241
DV
7587 pipe_config->pixel_multiplier = 1;
7588
0e8ffe1b
DV
7589 return true;
7590}
7591
1a91510d
JN
7592static struct {
7593 int clock;
7594 u32 config;
7595} hdmi_audio_clock[] = {
7596 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7597 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7598 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7599 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7600 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7601 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7602 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7603 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7604 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7605 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7606};
7607
7608/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7609static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7610{
7611 int i;
7612
7613 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7614 if (mode->clock == hdmi_audio_clock[i].clock)
7615 break;
7616 }
7617
7618 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7619 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7620 i = 1;
7621 }
7622
7623 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7624 hdmi_audio_clock[i].clock,
7625 hdmi_audio_clock[i].config);
7626
7627 return hdmi_audio_clock[i].config;
7628}
7629
3a9627f4
WF
7630static bool intel_eld_uptodate(struct drm_connector *connector,
7631 int reg_eldv, uint32_t bits_eldv,
7632 int reg_elda, uint32_t bits_elda,
7633 int reg_edid)
7634{
7635 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7636 uint8_t *eld = connector->eld;
7637 uint32_t i;
7638
7639 i = I915_READ(reg_eldv);
7640 i &= bits_eldv;
7641
7642 if (!eld[0])
7643 return !i;
7644
7645 if (!i)
7646 return false;
7647
7648 i = I915_READ(reg_elda);
7649 i &= ~bits_elda;
7650 I915_WRITE(reg_elda, i);
7651
7652 for (i = 0; i < eld[2]; i++)
7653 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7654 return false;
7655
7656 return true;
7657}
7658
e0dac65e 7659static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7660 struct drm_crtc *crtc,
7661 struct drm_display_mode *mode)
e0dac65e
WF
7662{
7663 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7664 uint8_t *eld = connector->eld;
7665 uint32_t eldv;
7666 uint32_t len;
7667 uint32_t i;
7668
7669 i = I915_READ(G4X_AUD_VID_DID);
7670
7671 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7672 eldv = G4X_ELDV_DEVCL_DEVBLC;
7673 else
7674 eldv = G4X_ELDV_DEVCTG;
7675
3a9627f4
WF
7676 if (intel_eld_uptodate(connector,
7677 G4X_AUD_CNTL_ST, eldv,
7678 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7679 G4X_HDMIW_HDMIEDID))
7680 return;
7681
e0dac65e
WF
7682 i = I915_READ(G4X_AUD_CNTL_ST);
7683 i &= ~(eldv | G4X_ELD_ADDR);
7684 len = (i >> 9) & 0x1f; /* ELD buffer size */
7685 I915_WRITE(G4X_AUD_CNTL_ST, i);
7686
7687 if (!eld[0])
7688 return;
7689
7690 len = min_t(uint8_t, eld[2], len);
7691 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7692 for (i = 0; i < len; i++)
7693 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7694
7695 i = I915_READ(G4X_AUD_CNTL_ST);
7696 i |= eldv;
7697 I915_WRITE(G4X_AUD_CNTL_ST, i);
7698}
7699
83358c85 7700static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7701 struct drm_crtc *crtc,
7702 struct drm_display_mode *mode)
83358c85
WX
7703{
7704 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7705 uint8_t *eld = connector->eld;
83358c85
WX
7706 uint32_t eldv;
7707 uint32_t i;
7708 int len;
7709 int pipe = to_intel_crtc(crtc)->pipe;
7710 int tmp;
7711
7712 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7713 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7714 int aud_config = HSW_AUD_CFG(pipe);
7715 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7716
83358c85
WX
7717 /* Audio output enable */
7718 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7719 tmp = I915_READ(aud_cntrl_st2);
7720 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7721 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7722 POSTING_READ(aud_cntrl_st2);
83358c85 7723
c7905792 7724 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7725
7726 /* Set ELD valid state */
7727 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7728 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7729 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7730 I915_WRITE(aud_cntrl_st2, tmp);
7731 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7732 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7733
7734 /* Enable HDMI mode */
7735 tmp = I915_READ(aud_config);
7e7cb34f 7736 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7737 /* clear N_programing_enable and N_value_index */
7738 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7739 I915_WRITE(aud_config, tmp);
7740
7741 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7742
7743 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7744
7745 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7746 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7747 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7748 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7749 } else {
7750 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7751 }
83358c85
WX
7752
7753 if (intel_eld_uptodate(connector,
7754 aud_cntrl_st2, eldv,
7755 aud_cntl_st, IBX_ELD_ADDRESS,
7756 hdmiw_hdmiedid))
7757 return;
7758
7759 i = I915_READ(aud_cntrl_st2);
7760 i &= ~eldv;
7761 I915_WRITE(aud_cntrl_st2, i);
7762
7763 if (!eld[0])
7764 return;
7765
7766 i = I915_READ(aud_cntl_st);
7767 i &= ~IBX_ELD_ADDRESS;
7768 I915_WRITE(aud_cntl_st, i);
7769 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7770 DRM_DEBUG_DRIVER("port num:%d\n", i);
7771
7772 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7773 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7774 for (i = 0; i < len; i++)
7775 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7776
7777 i = I915_READ(aud_cntrl_st2);
7778 i |= eldv;
7779 I915_WRITE(aud_cntrl_st2, i);
7780
7781}
7782
e0dac65e 7783static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7784 struct drm_crtc *crtc,
7785 struct drm_display_mode *mode)
e0dac65e
WF
7786{
7787 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7788 uint8_t *eld = connector->eld;
7789 uint32_t eldv;
7790 uint32_t i;
7791 int len;
7792 int hdmiw_hdmiedid;
b6daa025 7793 int aud_config;
e0dac65e
WF
7794 int aud_cntl_st;
7795 int aud_cntrl_st2;
9b138a83 7796 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7797
b3f33cbf 7798 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7799 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7800 aud_config = IBX_AUD_CFG(pipe);
7801 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 7802 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
7803 } else if (IS_VALLEYVIEW(connector->dev)) {
7804 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7805 aud_config = VLV_AUD_CFG(pipe);
7806 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7807 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 7808 } else {
9b138a83
WX
7809 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7810 aud_config = CPT_AUD_CFG(pipe);
7811 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 7812 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
7813 }
7814
9b138a83 7815 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 7816
9ca2fe73
ML
7817 if (IS_VALLEYVIEW(connector->dev)) {
7818 struct intel_encoder *intel_encoder;
7819 struct intel_digital_port *intel_dig_port;
7820
7821 intel_encoder = intel_attached_encoder(connector);
7822 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7823 i = intel_dig_port->port;
7824 } else {
7825 i = I915_READ(aud_cntl_st);
7826 i = (i >> 29) & DIP_PORT_SEL_MASK;
7827 /* DIP_Port_Select, 0x1 = PortB */
7828 }
7829
e0dac65e
WF
7830 if (!i) {
7831 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7832 /* operate blindly on all ports */
1202b4c6
WF
7833 eldv = IBX_ELD_VALIDB;
7834 eldv |= IBX_ELD_VALIDB << 4;
7835 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 7836 } else {
2582a850 7837 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 7838 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
7839 }
7840
3a9627f4
WF
7841 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7842 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7843 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 7844 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7845 } else {
7846 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7847 }
e0dac65e 7848
3a9627f4
WF
7849 if (intel_eld_uptodate(connector,
7850 aud_cntrl_st2, eldv,
7851 aud_cntl_st, IBX_ELD_ADDRESS,
7852 hdmiw_hdmiedid))
7853 return;
7854
e0dac65e
WF
7855 i = I915_READ(aud_cntrl_st2);
7856 i &= ~eldv;
7857 I915_WRITE(aud_cntrl_st2, i);
7858
7859 if (!eld[0])
7860 return;
7861
e0dac65e 7862 i = I915_READ(aud_cntl_st);
1202b4c6 7863 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
7864 I915_WRITE(aud_cntl_st, i);
7865
7866 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7867 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7868 for (i = 0; i < len; i++)
7869 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7870
7871 i = I915_READ(aud_cntrl_st2);
7872 i |= eldv;
7873 I915_WRITE(aud_cntrl_st2, i);
7874}
7875
7876void intel_write_eld(struct drm_encoder *encoder,
7877 struct drm_display_mode *mode)
7878{
7879 struct drm_crtc *crtc = encoder->crtc;
7880 struct drm_connector *connector;
7881 struct drm_device *dev = encoder->dev;
7882 struct drm_i915_private *dev_priv = dev->dev_private;
7883
7884 connector = drm_select_eld(encoder, mode);
7885 if (!connector)
7886 return;
7887
7888 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7889 connector->base.id,
c23cc417 7890 connector->name,
e0dac65e 7891 connector->encoder->base.id,
8e329a03 7892 connector->encoder->name);
e0dac65e
WF
7893
7894 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7895
7896 if (dev_priv->display.write_eld)
34427052 7897 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
7898}
7899
560b85bb
CW
7900static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7901{
7902 struct drm_device *dev = crtc->dev;
7903 struct drm_i915_private *dev_priv = dev->dev_private;
7904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4b0e333e 7905 uint32_t cntl;
560b85bb 7906
4b0e333e 7907 if (base != intel_crtc->cursor_base) {
560b85bb
CW
7908 /* On these chipsets we can only modify the base whilst
7909 * the cursor is disabled.
7910 */
4b0e333e
CW
7911 if (intel_crtc->cursor_cntl) {
7912 I915_WRITE(_CURACNTR, 0);
7913 POSTING_READ(_CURACNTR);
7914 intel_crtc->cursor_cntl = 0;
7915 }
7916
9db4a9c7 7917 I915_WRITE(_CURABASE, base);
4b0e333e
CW
7918 POSTING_READ(_CURABASE);
7919 }
560b85bb 7920
4b0e333e
CW
7921 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7922 cntl = 0;
7923 if (base)
7924 cntl = (CURSOR_ENABLE |
560b85bb 7925 CURSOR_GAMMA_ENABLE |
4b0e333e
CW
7926 CURSOR_FORMAT_ARGB);
7927 if (intel_crtc->cursor_cntl != cntl) {
7928 I915_WRITE(_CURACNTR, cntl);
7929 POSTING_READ(_CURACNTR);
7930 intel_crtc->cursor_cntl = cntl;
7931 }
560b85bb
CW
7932}
7933
7934static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7935{
7936 struct drm_device *dev = crtc->dev;
7937 struct drm_i915_private *dev_priv = dev->dev_private;
7938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7939 int pipe = intel_crtc->pipe;
4b0e333e 7940 uint32_t cntl;
4726e0b0 7941
4b0e333e
CW
7942 cntl = 0;
7943 if (base) {
7944 cntl = MCURSOR_GAMMA_ENABLE;
7945 switch (intel_crtc->cursor_width) {
4726e0b0
SK
7946 case 64:
7947 cntl |= CURSOR_MODE_64_ARGB_AX;
7948 break;
7949 case 128:
7950 cntl |= CURSOR_MODE_128_ARGB_AX;
7951 break;
7952 case 256:
7953 cntl |= CURSOR_MODE_256_ARGB_AX;
7954 break;
7955 default:
7956 WARN_ON(1);
7957 return;
560b85bb 7958 }
4b0e333e
CW
7959 cntl |= pipe << 28; /* Connect to correct pipe */
7960 }
7961 if (intel_crtc->cursor_cntl != cntl) {
9db4a9c7 7962 I915_WRITE(CURCNTR(pipe), cntl);
4b0e333e
CW
7963 POSTING_READ(CURCNTR(pipe));
7964 intel_crtc->cursor_cntl = cntl;
560b85bb 7965 }
4b0e333e 7966
560b85bb 7967 /* and commit changes on next vblank */
9db4a9c7 7968 I915_WRITE(CURBASE(pipe), base);
b2ea8ef5 7969 POSTING_READ(CURBASE(pipe));
560b85bb
CW
7970}
7971
65a21cd6
JB
7972static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7973{
7974 struct drm_device *dev = crtc->dev;
7975 struct drm_i915_private *dev_priv = dev->dev_private;
7976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7977 int pipe = intel_crtc->pipe;
4b0e333e
CW
7978 uint32_t cntl;
7979
7980 cntl = 0;
7981 if (base) {
7982 cntl = MCURSOR_GAMMA_ENABLE;
7983 switch (intel_crtc->cursor_width) {
4726e0b0
SK
7984 case 64:
7985 cntl |= CURSOR_MODE_64_ARGB_AX;
7986 break;
7987 case 128:
7988 cntl |= CURSOR_MODE_128_ARGB_AX;
7989 break;
7990 case 256:
7991 cntl |= CURSOR_MODE_256_ARGB_AX;
7992 break;
7993 default:
7994 WARN_ON(1);
7995 return;
65a21cd6 7996 }
4b0e333e
CW
7997 }
7998 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
7999 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 8000
4b0e333e
CW
8001 if (intel_crtc->cursor_cntl != cntl) {
8002 I915_WRITE(CURCNTR(pipe), cntl);
8003 POSTING_READ(CURCNTR(pipe));
8004 intel_crtc->cursor_cntl = cntl;
65a21cd6 8005 }
4b0e333e 8006
65a21cd6 8007 /* and commit changes on next vblank */
5efb3e28
VS
8008 I915_WRITE(CURBASE(pipe), base);
8009 POSTING_READ(CURBASE(pipe));
65a21cd6
JB
8010}
8011
cda4b7d3 8012/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8013static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8014 bool on)
cda4b7d3
CW
8015{
8016 struct drm_device *dev = crtc->dev;
8017 struct drm_i915_private *dev_priv = dev->dev_private;
8018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8019 int pipe = intel_crtc->pipe;
8020 int x = intel_crtc->cursor_x;
8021 int y = intel_crtc->cursor_y;
d6e4db15 8022 u32 base = 0, pos = 0;
cda4b7d3 8023
d6e4db15 8024 if (on)
cda4b7d3 8025 base = intel_crtc->cursor_addr;
cda4b7d3 8026
d6e4db15
VS
8027 if (x >= intel_crtc->config.pipe_src_w)
8028 base = 0;
8029
8030 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8031 base = 0;
8032
8033 if (x < 0) {
efc9064e 8034 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8035 base = 0;
8036
8037 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8038 x = -x;
8039 }
8040 pos |= x << CURSOR_X_SHIFT;
8041
8042 if (y < 0) {
efc9064e 8043 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8044 base = 0;
8045
8046 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8047 y = -y;
8048 }
8049 pos |= y << CURSOR_Y_SHIFT;
8050
4b0e333e 8051 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8052 return;
8053
5efb3e28
VS
8054 I915_WRITE(CURPOS(pipe), pos);
8055
8056 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
65a21cd6 8057 ivb_update_cursor(crtc, base);
5efb3e28
VS
8058 else if (IS_845G(dev) || IS_I865G(dev))
8059 i845_update_cursor(crtc, base);
8060 else
8061 i9xx_update_cursor(crtc, base);
4b0e333e 8062 intel_crtc->cursor_base = base;
cda4b7d3
CW
8063}
8064
79e53945 8065static int intel_crtc_cursor_set(struct drm_crtc *crtc,
05394f39 8066 struct drm_file *file,
79e53945
JB
8067 uint32_t handle,
8068 uint32_t width, uint32_t height)
8069{
8070 struct drm_device *dev = crtc->dev;
8071 struct drm_i915_private *dev_priv = dev->dev_private;
8072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
05394f39 8073 struct drm_i915_gem_object *obj;
64f962e3 8074 unsigned old_width;
cda4b7d3 8075 uint32_t addr;
3f8bc370 8076 int ret;
79e53945 8077
79e53945
JB
8078 /* if we want to turn off the cursor ignore width and height */
8079 if (!handle) {
28c97730 8080 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8081 addr = 0;
05394f39 8082 obj = NULL;
5004417d 8083 mutex_lock(&dev->struct_mutex);
3f8bc370 8084 goto finish;
79e53945
JB
8085 }
8086
4726e0b0
SK
8087 /* Check for which cursor types we support */
8088 if (!((width == 64 && height == 64) ||
8089 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8090 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8091 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8092 return -EINVAL;
8093 }
8094
05394f39 8095 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
c8725226 8096 if (&obj->base == NULL)
79e53945
JB
8097 return -ENOENT;
8098
05394f39 8099 if (obj->base.size < width * height * 4) {
3b25b31f 8100 DRM_DEBUG_KMS("buffer is to small\n");
34b8686e
DA
8101 ret = -ENOMEM;
8102 goto fail;
79e53945
JB
8103 }
8104
71acb5eb 8105 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8106 mutex_lock(&dev->struct_mutex);
3d13ef2e 8107 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8108 unsigned alignment;
8109
d9e86c0e 8110 if (obj->tiling_mode) {
3b25b31f 8111 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8112 ret = -EINVAL;
8113 goto fail_locked;
8114 }
8115
693db184
CW
8116 /* Note that the w/a also requires 2 PTE of padding following
8117 * the bo. We currently fill all unused PTE with the shadow
8118 * page and so we should always have valid PTE following the
8119 * cursor preventing the VT-d warning.
8120 */
8121 alignment = 0;
8122 if (need_vtd_wa(dev))
8123 alignment = 64*1024;
8124
8125 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8126 if (ret) {
3b25b31f 8127 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 8128 goto fail_locked;
e7b526bb
CW
8129 }
8130
d9e86c0e
CW
8131 ret = i915_gem_object_put_fence(obj);
8132 if (ret) {
3b25b31f 8133 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
8134 goto fail_unpin;
8135 }
8136
f343c5f6 8137 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 8138 } else {
6eeefaf3 8139 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8140 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8141 if (ret) {
3b25b31f 8142 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8143 goto fail_locked;
71acb5eb 8144 }
00731155 8145 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8146 }
8147
a6c45cf0 8148 if (IS_GEN2(dev))
14b60391
JB
8149 I915_WRITE(CURSIZE, (height << 12) | width);
8150
3f8bc370 8151 finish:
3f8bc370 8152 if (intel_crtc->cursor_bo) {
00731155 8153 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8154 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
05394f39 8155 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
3f8bc370 8156 }
80824003 8157
7f9872e0 8158 mutex_unlock(&dev->struct_mutex);
3f8bc370 8159
64f962e3
CW
8160 old_width = intel_crtc->cursor_width;
8161
3f8bc370 8162 intel_crtc->cursor_addr = addr;
05394f39 8163 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8164 intel_crtc->cursor_width = width;
8165 intel_crtc->cursor_height = height;
8166
64f962e3
CW
8167 if (intel_crtc->active) {
8168 if (old_width != width)
8169 intel_update_watermarks(crtc);
f2f5f771 8170 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8171 }
3f8bc370 8172
79e53945 8173 return 0;
e7b526bb 8174fail_unpin:
cc98b413 8175 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8176fail_locked:
34b8686e 8177 mutex_unlock(&dev->struct_mutex);
bc9025bd 8178fail:
05394f39 8179 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8180 return ret;
79e53945
JB
8181}
8182
8183static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
8184{
79e53945 8185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8186
92e76c8c
VS
8187 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
8188 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
652c393a 8189
f2f5f771
VS
8190 if (intel_crtc->active)
8191 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
79e53945
JB
8192
8193 return 0;
b8c00ac5
DA
8194}
8195
79e53945 8196static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8197 u16 *blue, uint32_t start, uint32_t size)
79e53945 8198{
7203425a 8199 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8201
7203425a 8202 for (i = start; i < end; i++) {
79e53945
JB
8203 intel_crtc->lut_r[i] = red[i] >> 8;
8204 intel_crtc->lut_g[i] = green[i] >> 8;
8205 intel_crtc->lut_b[i] = blue[i] >> 8;
8206 }
8207
8208 intel_crtc_load_lut(crtc);
8209}
8210
79e53945
JB
8211/* VESA 640x480x72Hz mode to set on the pipe */
8212static struct drm_display_mode load_detect_mode = {
8213 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8214 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8215};
8216
a8bb6818
DV
8217struct drm_framebuffer *
8218__intel_framebuffer_create(struct drm_device *dev,
8219 struct drm_mode_fb_cmd2 *mode_cmd,
8220 struct drm_i915_gem_object *obj)
d2dff872
CW
8221{
8222 struct intel_framebuffer *intel_fb;
8223 int ret;
8224
8225 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8226 if (!intel_fb) {
8227 drm_gem_object_unreference_unlocked(&obj->base);
8228 return ERR_PTR(-ENOMEM);
8229 }
8230
8231 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8232 if (ret)
8233 goto err;
d2dff872
CW
8234
8235 return &intel_fb->base;
dd4916c5
DV
8236err:
8237 drm_gem_object_unreference_unlocked(&obj->base);
8238 kfree(intel_fb);
8239
8240 return ERR_PTR(ret);
d2dff872
CW
8241}
8242
b5ea642a 8243static struct drm_framebuffer *
a8bb6818
DV
8244intel_framebuffer_create(struct drm_device *dev,
8245 struct drm_mode_fb_cmd2 *mode_cmd,
8246 struct drm_i915_gem_object *obj)
8247{
8248 struct drm_framebuffer *fb;
8249 int ret;
8250
8251 ret = i915_mutex_lock_interruptible(dev);
8252 if (ret)
8253 return ERR_PTR(ret);
8254 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8255 mutex_unlock(&dev->struct_mutex);
8256
8257 return fb;
8258}
8259
d2dff872
CW
8260static u32
8261intel_framebuffer_pitch_for_width(int width, int bpp)
8262{
8263 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8264 return ALIGN(pitch, 64);
8265}
8266
8267static u32
8268intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8269{
8270 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8271 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8272}
8273
8274static struct drm_framebuffer *
8275intel_framebuffer_create_for_mode(struct drm_device *dev,
8276 struct drm_display_mode *mode,
8277 int depth, int bpp)
8278{
8279 struct drm_i915_gem_object *obj;
0fed39bd 8280 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8281
8282 obj = i915_gem_alloc_object(dev,
8283 intel_framebuffer_size_for_mode(mode, bpp));
8284 if (obj == NULL)
8285 return ERR_PTR(-ENOMEM);
8286
8287 mode_cmd.width = mode->hdisplay;
8288 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8289 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8290 bpp);
5ca0c34a 8291 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8292
8293 return intel_framebuffer_create(dev, &mode_cmd, obj);
8294}
8295
8296static struct drm_framebuffer *
8297mode_fits_in_fbdev(struct drm_device *dev,
8298 struct drm_display_mode *mode)
8299{
4520f53a 8300#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8301 struct drm_i915_private *dev_priv = dev->dev_private;
8302 struct drm_i915_gem_object *obj;
8303 struct drm_framebuffer *fb;
8304
4c0e5528 8305 if (!dev_priv->fbdev)
d2dff872
CW
8306 return NULL;
8307
4c0e5528 8308 if (!dev_priv->fbdev->fb)
d2dff872
CW
8309 return NULL;
8310
4c0e5528
DV
8311 obj = dev_priv->fbdev->fb->obj;
8312 BUG_ON(!obj);
8313
8bcd4553 8314 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8315 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8316 fb->bits_per_pixel))
d2dff872
CW
8317 return NULL;
8318
01f2c773 8319 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8320 return NULL;
8321
8322 return fb;
4520f53a
DV
8323#else
8324 return NULL;
8325#endif
d2dff872
CW
8326}
8327
d2434ab7 8328bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8329 struct drm_display_mode *mode,
51fd371b
RC
8330 struct intel_load_detect_pipe *old,
8331 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8332{
8333 struct intel_crtc *intel_crtc;
d2434ab7
DV
8334 struct intel_encoder *intel_encoder =
8335 intel_attached_encoder(connector);
79e53945 8336 struct drm_crtc *possible_crtc;
4ef69c7a 8337 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8338 struct drm_crtc *crtc = NULL;
8339 struct drm_device *dev = encoder->dev;
94352cf9 8340 struct drm_framebuffer *fb;
51fd371b
RC
8341 struct drm_mode_config *config = &dev->mode_config;
8342 int ret, i = -1;
79e53945 8343
d2dff872 8344 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8345 connector->base.id, connector->name,
8e329a03 8346 encoder->base.id, encoder->name);
d2dff872 8347
51fd371b
RC
8348 drm_modeset_acquire_init(ctx, 0);
8349
8350retry:
8351 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8352 if (ret)
8353 goto fail_unlock;
6e9f798d 8354
79e53945
JB
8355 /*
8356 * Algorithm gets a little messy:
7a5e4805 8357 *
79e53945
JB
8358 * - if the connector already has an assigned crtc, use it (but make
8359 * sure it's on first)
7a5e4805 8360 *
79e53945
JB
8361 * - try to find the first unused crtc that can drive this connector,
8362 * and use that if we find one
79e53945
JB
8363 */
8364
8365 /* See if we already have a CRTC for this connector */
8366 if (encoder->crtc) {
8367 crtc = encoder->crtc;
8261b191 8368
51fd371b
RC
8369 ret = drm_modeset_lock(&crtc->mutex, ctx);
8370 if (ret)
8371 goto fail_unlock;
7b24056b 8372
24218aac 8373 old->dpms_mode = connector->dpms;
8261b191
CW
8374 old->load_detect_temp = false;
8375
8376 /* Make sure the crtc and connector are running */
24218aac
DV
8377 if (connector->dpms != DRM_MODE_DPMS_ON)
8378 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8379
7173188d 8380 return true;
79e53945
JB
8381 }
8382
8383 /* Find an unused one (if possible) */
70e1e0ec 8384 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8385 i++;
8386 if (!(encoder->possible_crtcs & (1 << i)))
8387 continue;
8388 if (!possible_crtc->enabled) {
8389 crtc = possible_crtc;
8390 break;
8391 }
79e53945
JB
8392 }
8393
8394 /*
8395 * If we didn't find an unused CRTC, don't use any.
8396 */
8397 if (!crtc) {
7173188d 8398 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8399 goto fail_unlock;
79e53945
JB
8400 }
8401
51fd371b
RC
8402 ret = drm_modeset_lock(&crtc->mutex, ctx);
8403 if (ret)
8404 goto fail_unlock;
fc303101
DV
8405 intel_encoder->new_crtc = to_intel_crtc(crtc);
8406 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8407
8408 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8409 intel_crtc->new_enabled = true;
8410 intel_crtc->new_config = &intel_crtc->config;
24218aac 8411 old->dpms_mode = connector->dpms;
8261b191 8412 old->load_detect_temp = true;
d2dff872 8413 old->release_fb = NULL;
79e53945 8414
6492711d
CW
8415 if (!mode)
8416 mode = &load_detect_mode;
79e53945 8417
d2dff872
CW
8418 /* We need a framebuffer large enough to accommodate all accesses
8419 * that the plane may generate whilst we perform load detection.
8420 * We can not rely on the fbcon either being present (we get called
8421 * during its initialisation to detect all boot displays, or it may
8422 * not even exist) or that it is large enough to satisfy the
8423 * requested mode.
8424 */
94352cf9
DV
8425 fb = mode_fits_in_fbdev(dev, mode);
8426 if (fb == NULL) {
d2dff872 8427 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8428 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8429 old->release_fb = fb;
d2dff872
CW
8430 } else
8431 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8432 if (IS_ERR(fb)) {
d2dff872 8433 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8434 goto fail;
79e53945 8435 }
79e53945 8436
c0c36b94 8437 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8438 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8439 if (old->release_fb)
8440 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8441 goto fail;
79e53945 8442 }
7173188d 8443
79e53945 8444 /* let the connector get through one full cycle before testing */
9d0498a2 8445 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8446 return true;
412b61d8
VS
8447
8448 fail:
8449 intel_crtc->new_enabled = crtc->enabled;
8450 if (intel_crtc->new_enabled)
8451 intel_crtc->new_config = &intel_crtc->config;
8452 else
8453 intel_crtc->new_config = NULL;
51fd371b
RC
8454fail_unlock:
8455 if (ret == -EDEADLK) {
8456 drm_modeset_backoff(ctx);
8457 goto retry;
8458 }
8459
8460 drm_modeset_drop_locks(ctx);
8461 drm_modeset_acquire_fini(ctx);
6e9f798d 8462
412b61d8 8463 return false;
79e53945
JB
8464}
8465
d2434ab7 8466void intel_release_load_detect_pipe(struct drm_connector *connector,
51fd371b
RC
8467 struct intel_load_detect_pipe *old,
8468 struct drm_modeset_acquire_ctx *ctx)
79e53945 8469{
d2434ab7
DV
8470 struct intel_encoder *intel_encoder =
8471 intel_attached_encoder(connector);
4ef69c7a 8472 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8473 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8475
d2dff872 8476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8477 connector->base.id, connector->name,
8e329a03 8478 encoder->base.id, encoder->name);
d2dff872 8479
8261b191 8480 if (old->load_detect_temp) {
fc303101
DV
8481 to_intel_connector(connector)->new_encoder = NULL;
8482 intel_encoder->new_crtc = NULL;
412b61d8
VS
8483 intel_crtc->new_enabled = false;
8484 intel_crtc->new_config = NULL;
fc303101 8485 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8486
36206361
DV
8487 if (old->release_fb) {
8488 drm_framebuffer_unregister_private(old->release_fb);
8489 drm_framebuffer_unreference(old->release_fb);
8490 }
d2dff872 8491
51fd371b 8492 goto unlock;
0622a53c 8493 return;
79e53945
JB
8494 }
8495
c751ce4f 8496 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8497 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8498 connector->funcs->dpms(connector, old->dpms_mode);
7b24056b 8499
51fd371b
RC
8500unlock:
8501 drm_modeset_drop_locks(ctx);
8502 drm_modeset_acquire_fini(ctx);
79e53945
JB
8503}
8504
da4a1efa
VS
8505static int i9xx_pll_refclk(struct drm_device *dev,
8506 const struct intel_crtc_config *pipe_config)
8507{
8508 struct drm_i915_private *dev_priv = dev->dev_private;
8509 u32 dpll = pipe_config->dpll_hw_state.dpll;
8510
8511 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8512 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8513 else if (HAS_PCH_SPLIT(dev))
8514 return 120000;
8515 else if (!IS_GEN2(dev))
8516 return 96000;
8517 else
8518 return 48000;
8519}
8520
79e53945 8521/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8522static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8523 struct intel_crtc_config *pipe_config)
79e53945 8524{
f1f644dc 8525 struct drm_device *dev = crtc->base.dev;
79e53945 8526 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8527 int pipe = pipe_config->cpu_transcoder;
293623f7 8528 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8529 u32 fp;
8530 intel_clock_t clock;
da4a1efa 8531 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8532
8533 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8534 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8535 else
293623f7 8536 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8537
8538 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8539 if (IS_PINEVIEW(dev)) {
8540 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8541 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8542 } else {
8543 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8544 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8545 }
8546
a6c45cf0 8547 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8548 if (IS_PINEVIEW(dev))
8549 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8550 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8551 else
8552 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8553 DPLL_FPA01_P1_POST_DIV_SHIFT);
8554
8555 switch (dpll & DPLL_MODE_MASK) {
8556 case DPLLB_MODE_DAC_SERIAL:
8557 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8558 5 : 10;
8559 break;
8560 case DPLLB_MODE_LVDS:
8561 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8562 7 : 14;
8563 break;
8564 default:
28c97730 8565 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8566 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8567 return;
79e53945
JB
8568 }
8569
ac58c3f0 8570 if (IS_PINEVIEW(dev))
da4a1efa 8571 pineview_clock(refclk, &clock);
ac58c3f0 8572 else
da4a1efa 8573 i9xx_clock(refclk, &clock);
79e53945 8574 } else {
0fb58223 8575 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8576 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8577
8578 if (is_lvds) {
8579 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8580 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8581
8582 if (lvds & LVDS_CLKB_POWER_UP)
8583 clock.p2 = 7;
8584 else
8585 clock.p2 = 14;
79e53945
JB
8586 } else {
8587 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8588 clock.p1 = 2;
8589 else {
8590 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8591 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8592 }
8593 if (dpll & PLL_P2_DIVIDE_BY_4)
8594 clock.p2 = 4;
8595 else
8596 clock.p2 = 2;
79e53945 8597 }
da4a1efa
VS
8598
8599 i9xx_clock(refclk, &clock);
79e53945
JB
8600 }
8601
18442d08
VS
8602 /*
8603 * This value includes pixel_multiplier. We will use
241bfc38 8604 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8605 * encoder's get_config() function.
8606 */
8607 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8608}
8609
6878da05
VS
8610int intel_dotclock_calculate(int link_freq,
8611 const struct intel_link_m_n *m_n)
f1f644dc 8612{
f1f644dc
JB
8613 /*
8614 * The calculation for the data clock is:
1041a02f 8615 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8616 * But we want to avoid losing precison if possible, so:
1041a02f 8617 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8618 *
8619 * and the link clock is simpler:
1041a02f 8620 * link_clock = (m * link_clock) / n
f1f644dc
JB
8621 */
8622
6878da05
VS
8623 if (!m_n->link_n)
8624 return 0;
f1f644dc 8625
6878da05
VS
8626 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8627}
f1f644dc 8628
18442d08
VS
8629static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8630 struct intel_crtc_config *pipe_config)
6878da05
VS
8631{
8632 struct drm_device *dev = crtc->base.dev;
79e53945 8633
18442d08
VS
8634 /* read out port_clock from the DPLL */
8635 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8636
f1f644dc 8637 /*
18442d08 8638 * This value does not include pixel_multiplier.
241bfc38 8639 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8640 * agree once we know their relationship in the encoder's
8641 * get_config() function.
79e53945 8642 */
241bfc38 8643 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8644 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8645 &pipe_config->fdi_m_n);
79e53945
JB
8646}
8647
8648/** Returns the currently programmed mode of the given pipe. */
8649struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8650 struct drm_crtc *crtc)
8651{
548f245b 8652 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8654 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8655 struct drm_display_mode *mode;
f1f644dc 8656 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8657 int htot = I915_READ(HTOTAL(cpu_transcoder));
8658 int hsync = I915_READ(HSYNC(cpu_transcoder));
8659 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8660 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8661 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8662
8663 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8664 if (!mode)
8665 return NULL;
8666
f1f644dc
JB
8667 /*
8668 * Construct a pipe_config sufficient for getting the clock info
8669 * back out of crtc_clock_get.
8670 *
8671 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8672 * to use a real value here instead.
8673 */
293623f7 8674 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8675 pipe_config.pixel_multiplier = 1;
293623f7
VS
8676 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8677 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8678 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8679 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8680
773ae034 8681 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8682 mode->hdisplay = (htot & 0xffff) + 1;
8683 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8684 mode->hsync_start = (hsync & 0xffff) + 1;
8685 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8686 mode->vdisplay = (vtot & 0xffff) + 1;
8687 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8688 mode->vsync_start = (vsync & 0xffff) + 1;
8689 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8690
8691 drm_mode_set_name(mode);
79e53945
JB
8692
8693 return mode;
8694}
8695
3dec0095 8696static void intel_increase_pllclock(struct drm_crtc *crtc)
652c393a
JB
8697{
8698 struct drm_device *dev = crtc->dev;
fbee40df 8699 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a
JB
8700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8701 int pipe = intel_crtc->pipe;
dbdc6479
JB
8702 int dpll_reg = DPLL(pipe);
8703 int dpll;
652c393a 8704
bad720ff 8705 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8706 return;
8707
8708 if (!dev_priv->lvds_downclock_avail)
8709 return;
8710
dbdc6479 8711 dpll = I915_READ(dpll_reg);
652c393a 8712 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8713 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8714
8ac5a6d5 8715 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8716
8717 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8718 I915_WRITE(dpll_reg, dpll);
9d0498a2 8719 intel_wait_for_vblank(dev, pipe);
dbdc6479 8720
652c393a
JB
8721 dpll = I915_READ(dpll_reg);
8722 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8723 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8724 }
652c393a
JB
8725}
8726
8727static void intel_decrease_pllclock(struct drm_crtc *crtc)
8728{
8729 struct drm_device *dev = crtc->dev;
fbee40df 8730 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8732
bad720ff 8733 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8734 return;
8735
8736 if (!dev_priv->lvds_downclock_avail)
8737 return;
8738
8739 /*
8740 * Since this is called by a timer, we should never get here in
8741 * the manual case.
8742 */
8743 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8744 int pipe = intel_crtc->pipe;
8745 int dpll_reg = DPLL(pipe);
8746 int dpll;
f6e5b160 8747
44d98a61 8748 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8749
8ac5a6d5 8750 assert_panel_unlocked(dev_priv, pipe);
652c393a 8751
dc257cf1 8752 dpll = I915_READ(dpll_reg);
652c393a
JB
8753 dpll |= DISPLAY_RATE_SELECT_FPA1;
8754 I915_WRITE(dpll_reg, dpll);
9d0498a2 8755 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8756 dpll = I915_READ(dpll_reg);
8757 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8758 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8759 }
8760
8761}
8762
f047e395
CW
8763void intel_mark_busy(struct drm_device *dev)
8764{
c67a470b
PZ
8765 struct drm_i915_private *dev_priv = dev->dev_private;
8766
f62a0076
CW
8767 if (dev_priv->mm.busy)
8768 return;
8769
43694d69 8770 intel_runtime_pm_get(dev_priv);
c67a470b 8771 i915_update_gfx_val(dev_priv);
f62a0076 8772 dev_priv->mm.busy = true;
f047e395
CW
8773}
8774
8775void intel_mark_idle(struct drm_device *dev)
652c393a 8776{
c67a470b 8777 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8778 struct drm_crtc *crtc;
652c393a 8779
f62a0076
CW
8780 if (!dev_priv->mm.busy)
8781 return;
8782
8783 dev_priv->mm.busy = false;
8784
d330a953 8785 if (!i915.powersave)
bb4cdd53 8786 goto out;
652c393a 8787
70e1e0ec 8788 for_each_crtc(dev, crtc) {
f4510a27 8789 if (!crtc->primary->fb)
652c393a
JB
8790 continue;
8791
725a5b54 8792 intel_decrease_pllclock(crtc);
652c393a 8793 }
b29c19b6 8794
3d13ef2e 8795 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 8796 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
8797
8798out:
43694d69 8799 intel_runtime_pm_put(dev_priv);
652c393a
JB
8800}
8801
c65355bb 8802void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
a4872ba6 8803 struct intel_engine_cs *ring)
652c393a 8804{
f047e395
CW
8805 struct drm_device *dev = obj->base.dev;
8806 struct drm_crtc *crtc;
652c393a 8807
d330a953 8808 if (!i915.powersave)
acb87dfb
CW
8809 return;
8810
70e1e0ec 8811 for_each_crtc(dev, crtc) {
f4510a27 8812 if (!crtc->primary->fb)
652c393a
JB
8813 continue;
8814
f4510a27 8815 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
c65355bb
CW
8816 continue;
8817
8818 intel_increase_pllclock(crtc);
8819 if (ring && intel_fbc_enabled(dev))
8820 ring->fbc_dirty = true;
652c393a
JB
8821 }
8822}
8823
79e53945
JB
8824static void intel_crtc_destroy(struct drm_crtc *crtc)
8825{
8826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
8827 struct drm_device *dev = crtc->dev;
8828 struct intel_unpin_work *work;
8829 unsigned long flags;
8830
8831 spin_lock_irqsave(&dev->event_lock, flags);
8832 work = intel_crtc->unpin_work;
8833 intel_crtc->unpin_work = NULL;
8834 spin_unlock_irqrestore(&dev->event_lock, flags);
8835
8836 if (work) {
8837 cancel_work_sync(&work->work);
8838 kfree(work);
8839 }
79e53945 8840
40ccc72b
MK
8841 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8842
79e53945 8843 drm_crtc_cleanup(crtc);
67e77c5a 8844
79e53945
JB
8845 kfree(intel_crtc);
8846}
8847
6b95a207
KH
8848static void intel_unpin_work_fn(struct work_struct *__work)
8849{
8850 struct intel_unpin_work *work =
8851 container_of(__work, struct intel_unpin_work, work);
b4a98e57 8852 struct drm_device *dev = work->crtc->dev;
6b95a207 8853
b4a98e57 8854 mutex_lock(&dev->struct_mutex);
1690e1eb 8855 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
8856 drm_gem_object_unreference(&work->pending_flip_obj->base);
8857 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 8858
b4a98e57
CW
8859 intel_update_fbc(dev);
8860 mutex_unlock(&dev->struct_mutex);
8861
8862 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8863 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8864
6b95a207
KH
8865 kfree(work);
8866}
8867
1afe3e9d 8868static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 8869 struct drm_crtc *crtc)
6b95a207 8870{
fbee40df 8871 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
8872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8873 struct intel_unpin_work *work;
6b95a207
KH
8874 unsigned long flags;
8875
8876 /* Ignore early vblank irqs */
8877 if (intel_crtc == NULL)
8878 return;
8879
8880 spin_lock_irqsave(&dev->event_lock, flags);
8881 work = intel_crtc->unpin_work;
e7d841ca
CW
8882
8883 /* Ensure we don't miss a work->pending update ... */
8884 smp_rmb();
8885
8886 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
8887 spin_unlock_irqrestore(&dev->event_lock, flags);
8888 return;
8889 }
8890
e7d841ca
CW
8891 /* and that the unpin work is consistent wrt ->pending. */
8892 smp_rmb();
8893
6b95a207 8894 intel_crtc->unpin_work = NULL;
6b95a207 8895
45a066eb
RC
8896 if (work->event)
8897 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 8898
87b6b101 8899 drm_crtc_vblank_put(crtc);
0af7e4df 8900
6b95a207
KH
8901 spin_unlock_irqrestore(&dev->event_lock, flags);
8902
2c10d571 8903 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
8904
8905 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
8906
8907 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
8908}
8909
1afe3e9d
JB
8910void intel_finish_page_flip(struct drm_device *dev, int pipe)
8911{
fbee40df 8912 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8913 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8914
49b14a5c 8915 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8916}
8917
8918void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8919{
fbee40df 8920 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8921 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8922
49b14a5c 8923 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8924}
8925
75f7f3ec
VS
8926/* Is 'a' after or equal to 'b'? */
8927static bool g4x_flip_count_after_eq(u32 a, u32 b)
8928{
8929 return !((a - b) & 0x80000000);
8930}
8931
8932static bool page_flip_finished(struct intel_crtc *crtc)
8933{
8934 struct drm_device *dev = crtc->base.dev;
8935 struct drm_i915_private *dev_priv = dev->dev_private;
8936
8937 /*
8938 * The relevant registers doen't exist on pre-ctg.
8939 * As the flip done interrupt doesn't trigger for mmio
8940 * flips on gmch platforms, a flip count check isn't
8941 * really needed there. But since ctg has the registers,
8942 * include it in the check anyway.
8943 */
8944 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8945 return true;
8946
8947 /*
8948 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8949 * used the same base address. In that case the mmio flip might
8950 * have completed, but the CS hasn't even executed the flip yet.
8951 *
8952 * A flip count check isn't enough as the CS might have updated
8953 * the base address just after start of vblank, but before we
8954 * managed to process the interrupt. This means we'd complete the
8955 * CS flip too soon.
8956 *
8957 * Combining both checks should get us a good enough result. It may
8958 * still happen that the CS flip has been executed, but has not
8959 * yet actually completed. But in case the base address is the same
8960 * anyway, we don't really care.
8961 */
8962 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8963 crtc->unpin_work->gtt_offset &&
8964 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8965 crtc->unpin_work->flip_count);
8966}
8967
6b95a207
KH
8968void intel_prepare_page_flip(struct drm_device *dev, int plane)
8969{
fbee40df 8970 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
8971 struct intel_crtc *intel_crtc =
8972 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8973 unsigned long flags;
8974
e7d841ca
CW
8975 /* NB: An MMIO update of the plane base pointer will also
8976 * generate a page-flip completion irq, i.e. every modeset
8977 * is also accompanied by a spurious intel_prepare_page_flip().
8978 */
6b95a207 8979 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 8980 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 8981 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
8982 spin_unlock_irqrestore(&dev->event_lock, flags);
8983}
8984
eba905b2 8985static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
8986{
8987 /* Ensure that the work item is consistent when activating it ... */
8988 smp_wmb();
8989 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8990 /* and that it is marked active as soon as the irq could fire. */
8991 smp_wmb();
8992}
8993
8c9f3aaf
JB
8994static int intel_gen2_queue_flip(struct drm_device *dev,
8995 struct drm_crtc *crtc,
8996 struct drm_framebuffer *fb,
ed8d1975 8997 struct drm_i915_gem_object *obj,
a4872ba6 8998 struct intel_engine_cs *ring,
ed8d1975 8999 uint32_t flags)
8c9f3aaf 9000{
8c9f3aaf 9001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9002 u32 flip_mask;
9003 int ret;
9004
6d90c952 9005 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9006 if (ret)
4fa62c89 9007 return ret;
8c9f3aaf
JB
9008
9009 /* Can't queue multiple flips, so wait for the previous
9010 * one to finish before executing the next.
9011 */
9012 if (intel_crtc->plane)
9013 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9014 else
9015 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9016 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9017 intel_ring_emit(ring, MI_NOOP);
9018 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9019 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9020 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9021 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9022 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9023
9024 intel_mark_page_flip_active(intel_crtc);
09246732 9025 __intel_ring_advance(ring);
83d4092b 9026 return 0;
8c9f3aaf
JB
9027}
9028
9029static int intel_gen3_queue_flip(struct drm_device *dev,
9030 struct drm_crtc *crtc,
9031 struct drm_framebuffer *fb,
ed8d1975 9032 struct drm_i915_gem_object *obj,
a4872ba6 9033 struct intel_engine_cs *ring,
ed8d1975 9034 uint32_t flags)
8c9f3aaf 9035{
8c9f3aaf 9036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9037 u32 flip_mask;
9038 int ret;
9039
6d90c952 9040 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9041 if (ret)
4fa62c89 9042 return ret;
8c9f3aaf
JB
9043
9044 if (intel_crtc->plane)
9045 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9046 else
9047 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9048 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9049 intel_ring_emit(ring, MI_NOOP);
9050 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9051 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9052 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9053 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9054 intel_ring_emit(ring, MI_NOOP);
9055
e7d841ca 9056 intel_mark_page_flip_active(intel_crtc);
09246732 9057 __intel_ring_advance(ring);
83d4092b 9058 return 0;
8c9f3aaf
JB
9059}
9060
9061static int intel_gen4_queue_flip(struct drm_device *dev,
9062 struct drm_crtc *crtc,
9063 struct drm_framebuffer *fb,
ed8d1975 9064 struct drm_i915_gem_object *obj,
a4872ba6 9065 struct intel_engine_cs *ring,
ed8d1975 9066 uint32_t flags)
8c9f3aaf
JB
9067{
9068 struct drm_i915_private *dev_priv = dev->dev_private;
9069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9070 uint32_t pf, pipesrc;
9071 int ret;
9072
6d90c952 9073 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9074 if (ret)
4fa62c89 9075 return ret;
8c9f3aaf
JB
9076
9077 /* i965+ uses the linear or tiled offsets from the
9078 * Display Registers (which do not change across a page-flip)
9079 * so we need only reprogram the base address.
9080 */
6d90c952
DV
9081 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9082 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9083 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9084 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9085 obj->tiling_mode);
8c9f3aaf
JB
9086
9087 /* XXX Enabling the panel-fitter across page-flip is so far
9088 * untested on non-native modes, so ignore it for now.
9089 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9090 */
9091 pf = 0;
9092 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9093 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9094
9095 intel_mark_page_flip_active(intel_crtc);
09246732 9096 __intel_ring_advance(ring);
83d4092b 9097 return 0;
8c9f3aaf
JB
9098}
9099
9100static int intel_gen6_queue_flip(struct drm_device *dev,
9101 struct drm_crtc *crtc,
9102 struct drm_framebuffer *fb,
ed8d1975 9103 struct drm_i915_gem_object *obj,
a4872ba6 9104 struct intel_engine_cs *ring,
ed8d1975 9105 uint32_t flags)
8c9f3aaf
JB
9106{
9107 struct drm_i915_private *dev_priv = dev->dev_private;
9108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9109 uint32_t pf, pipesrc;
9110 int ret;
9111
6d90c952 9112 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9113 if (ret)
4fa62c89 9114 return ret;
8c9f3aaf 9115
6d90c952
DV
9116 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9117 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9118 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9119 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9120
dc257cf1
DV
9121 /* Contrary to the suggestions in the documentation,
9122 * "Enable Panel Fitter" does not seem to be required when page
9123 * flipping with a non-native mode, and worse causes a normal
9124 * modeset to fail.
9125 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9126 */
9127 pf = 0;
8c9f3aaf 9128 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9129 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9130
9131 intel_mark_page_flip_active(intel_crtc);
09246732 9132 __intel_ring_advance(ring);
83d4092b 9133 return 0;
8c9f3aaf
JB
9134}
9135
7c9017e5
JB
9136static int intel_gen7_queue_flip(struct drm_device *dev,
9137 struct drm_crtc *crtc,
9138 struct drm_framebuffer *fb,
ed8d1975 9139 struct drm_i915_gem_object *obj,
a4872ba6 9140 struct intel_engine_cs *ring,
ed8d1975 9141 uint32_t flags)
7c9017e5 9142{
7c9017e5 9143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9144 uint32_t plane_bit = 0;
ffe74d75
CW
9145 int len, ret;
9146
eba905b2 9147 switch (intel_crtc->plane) {
cb05d8de
DV
9148 case PLANE_A:
9149 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9150 break;
9151 case PLANE_B:
9152 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9153 break;
9154 case PLANE_C:
9155 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9156 break;
9157 default:
9158 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9159 return -ENODEV;
cb05d8de
DV
9160 }
9161
ffe74d75 9162 len = 4;
f476828a 9163 if (ring->id == RCS) {
ffe74d75 9164 len += 6;
f476828a
DL
9165 /*
9166 * On Gen 8, SRM is now taking an extra dword to accommodate
9167 * 48bits addresses, and we need a NOOP for the batch size to
9168 * stay even.
9169 */
9170 if (IS_GEN8(dev))
9171 len += 2;
9172 }
ffe74d75 9173
f66fab8e
VS
9174 /*
9175 * BSpec MI_DISPLAY_FLIP for IVB:
9176 * "The full packet must be contained within the same cache line."
9177 *
9178 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9179 * cacheline, if we ever start emitting more commands before
9180 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9181 * then do the cacheline alignment, and finally emit the
9182 * MI_DISPLAY_FLIP.
9183 */
9184 ret = intel_ring_cacheline_align(ring);
9185 if (ret)
4fa62c89 9186 return ret;
f66fab8e 9187
ffe74d75 9188 ret = intel_ring_begin(ring, len);
7c9017e5 9189 if (ret)
4fa62c89 9190 return ret;
7c9017e5 9191
ffe74d75
CW
9192 /* Unmask the flip-done completion message. Note that the bspec says that
9193 * we should do this for both the BCS and RCS, and that we must not unmask
9194 * more than one flip event at any time (or ensure that one flip message
9195 * can be sent by waiting for flip-done prior to queueing new flips).
9196 * Experimentation says that BCS works despite DERRMR masking all
9197 * flip-done completion events and that unmasking all planes at once
9198 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9199 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9200 */
9201 if (ring->id == RCS) {
9202 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9203 intel_ring_emit(ring, DERRMR);
9204 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9205 DERRMR_PIPEB_PRI_FLIP_DONE |
9206 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9207 if (IS_GEN8(dev))
9208 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9209 MI_SRM_LRM_GLOBAL_GTT);
9210 else
9211 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9212 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9213 intel_ring_emit(ring, DERRMR);
9214 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9215 if (IS_GEN8(dev)) {
9216 intel_ring_emit(ring, 0);
9217 intel_ring_emit(ring, MI_NOOP);
9218 }
ffe74d75
CW
9219 }
9220
cb05d8de 9221 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9222 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9223 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9224 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9225
9226 intel_mark_page_flip_active(intel_crtc);
09246732 9227 __intel_ring_advance(ring);
83d4092b 9228 return 0;
7c9017e5
JB
9229}
9230
8c9f3aaf
JB
9231static int intel_default_queue_flip(struct drm_device *dev,
9232 struct drm_crtc *crtc,
9233 struct drm_framebuffer *fb,
ed8d1975 9234 struct drm_i915_gem_object *obj,
a4872ba6 9235 struct intel_engine_cs *ring,
ed8d1975 9236 uint32_t flags)
8c9f3aaf
JB
9237{
9238 return -ENODEV;
9239}
9240
6b95a207
KH
9241static int intel_crtc_page_flip(struct drm_crtc *crtc,
9242 struct drm_framebuffer *fb,
ed8d1975
KP
9243 struct drm_pending_vblank_event *event,
9244 uint32_t page_flip_flags)
6b95a207
KH
9245{
9246 struct drm_device *dev = crtc->dev;
9247 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9248 struct drm_framebuffer *old_fb = crtc->primary->fb;
4a35f83b 9249 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
6b95a207
KH
9250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9251 struct intel_unpin_work *work;
a4872ba6 9252 struct intel_engine_cs *ring;
8c9f3aaf 9253 unsigned long flags;
52e68630 9254 int ret;
6b95a207 9255
e6a595d2 9256 /* Can't change pixel format via MI display flips. */
f4510a27 9257 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9258 return -EINVAL;
9259
9260 /*
9261 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9262 * Note that pitch changes could also affect these register.
9263 */
9264 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9265 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9266 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9267 return -EINVAL;
9268
f900db47
CW
9269 if (i915_terminally_wedged(&dev_priv->gpu_error))
9270 goto out_hang;
9271
b14c5679 9272 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9273 if (work == NULL)
9274 return -ENOMEM;
9275
6b95a207 9276 work->event = event;
b4a98e57 9277 work->crtc = crtc;
4a35f83b 9278 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
6b95a207
KH
9279 INIT_WORK(&work->work, intel_unpin_work_fn);
9280
87b6b101 9281 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9282 if (ret)
9283 goto free_work;
9284
6b95a207
KH
9285 /* We borrow the event spin lock for protecting unpin_work */
9286 spin_lock_irqsave(&dev->event_lock, flags);
9287 if (intel_crtc->unpin_work) {
9288 spin_unlock_irqrestore(&dev->event_lock, flags);
9289 kfree(work);
87b6b101 9290 drm_crtc_vblank_put(crtc);
468f0b44
CW
9291
9292 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
9293 return -EBUSY;
9294 }
9295 intel_crtc->unpin_work = work;
9296 spin_unlock_irqrestore(&dev->event_lock, flags);
9297
b4a98e57
CW
9298 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9299 flush_workqueue(dev_priv->wq);
9300
79158103
CW
9301 ret = i915_mutex_lock_interruptible(dev);
9302 if (ret)
9303 goto cleanup;
6b95a207 9304
75dfca80 9305 /* Reference the objects for the scheduled work. */
05394f39
CW
9306 drm_gem_object_reference(&work->old_fb_obj->base);
9307 drm_gem_object_reference(&obj->base);
6b95a207 9308
f4510a27 9309 crtc->primary->fb = fb;
96b099fd 9310
e1f99ce6 9311 work->pending_flip_obj = obj;
e1f99ce6 9312
4e5359cd
SF
9313 work->enable_stall_check = true;
9314
b4a98e57 9315 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9316 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9317
75f7f3ec
VS
9318 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9319 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
9320
4fa62c89
VS
9321 if (IS_VALLEYVIEW(dev)) {
9322 ring = &dev_priv->ring[BCS];
9323 } else if (INTEL_INFO(dev)->gen >= 7) {
9324 ring = obj->ring;
9325 if (ring == NULL || ring->id != RCS)
9326 ring = &dev_priv->ring[BCS];
9327 } else {
9328 ring = &dev_priv->ring[RCS];
9329 }
9330
9331 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9332 if (ret)
9333 goto cleanup_pending;
6b95a207 9334
4fa62c89
VS
9335 work->gtt_offset =
9336 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9337
9338 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
9339 if (ret)
9340 goto cleanup_unpin;
9341
7782de3b 9342 intel_disable_fbc(dev);
c65355bb 9343 intel_mark_fb_busy(obj, NULL);
6b95a207
KH
9344 mutex_unlock(&dev->struct_mutex);
9345
e5510fac
JB
9346 trace_i915_flip_request(intel_crtc->plane, obj);
9347
6b95a207 9348 return 0;
96b099fd 9349
4fa62c89
VS
9350cleanup_unpin:
9351 intel_unpin_fb_obj(obj);
8c9f3aaf 9352cleanup_pending:
b4a98e57 9353 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9354 crtc->primary->fb = old_fb;
05394f39
CW
9355 drm_gem_object_unreference(&work->old_fb_obj->base);
9356 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9357 mutex_unlock(&dev->struct_mutex);
9358
79158103 9359cleanup:
96b099fd
CW
9360 spin_lock_irqsave(&dev->event_lock, flags);
9361 intel_crtc->unpin_work = NULL;
9362 spin_unlock_irqrestore(&dev->event_lock, flags);
9363
87b6b101 9364 drm_crtc_vblank_put(crtc);
7317c75e 9365free_work:
96b099fd
CW
9366 kfree(work);
9367
f900db47
CW
9368 if (ret == -EIO) {
9369out_hang:
9370 intel_crtc_wait_for_pending_flips(crtc);
9371 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9372 if (ret == 0 && event)
9373 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9374 }
96b099fd 9375 return ret;
6b95a207
KH
9376}
9377
f6e5b160 9378static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9379 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9380 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9381};
9382
9a935856
DV
9383/**
9384 * intel_modeset_update_staged_output_state
9385 *
9386 * Updates the staged output configuration state, e.g. after we've read out the
9387 * current hw state.
9388 */
9389static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9390{
7668851f 9391 struct intel_crtc *crtc;
9a935856
DV
9392 struct intel_encoder *encoder;
9393 struct intel_connector *connector;
f6e5b160 9394
9a935856
DV
9395 list_for_each_entry(connector, &dev->mode_config.connector_list,
9396 base.head) {
9397 connector->new_encoder =
9398 to_intel_encoder(connector->base.encoder);
9399 }
f6e5b160 9400
9a935856
DV
9401 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9402 base.head) {
9403 encoder->new_crtc =
9404 to_intel_crtc(encoder->base.crtc);
9405 }
7668851f 9406
d3fcc808 9407 for_each_intel_crtc(dev, crtc) {
7668851f 9408 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9409
9410 if (crtc->new_enabled)
9411 crtc->new_config = &crtc->config;
9412 else
9413 crtc->new_config = NULL;
7668851f 9414 }
f6e5b160
CW
9415}
9416
9a935856
DV
9417/**
9418 * intel_modeset_commit_output_state
9419 *
9420 * This function copies the stage display pipe configuration to the real one.
9421 */
9422static void intel_modeset_commit_output_state(struct drm_device *dev)
9423{
7668851f 9424 struct intel_crtc *crtc;
9a935856
DV
9425 struct intel_encoder *encoder;
9426 struct intel_connector *connector;
f6e5b160 9427
9a935856
DV
9428 list_for_each_entry(connector, &dev->mode_config.connector_list,
9429 base.head) {
9430 connector->base.encoder = &connector->new_encoder->base;
9431 }
f6e5b160 9432
9a935856
DV
9433 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9434 base.head) {
9435 encoder->base.crtc = &encoder->new_crtc->base;
9436 }
7668851f 9437
d3fcc808 9438 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9439 crtc->base.enabled = crtc->new_enabled;
9440 }
9a935856
DV
9441}
9442
050f7aeb 9443static void
eba905b2 9444connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
9445 struct intel_crtc_config *pipe_config)
9446{
9447 int bpp = pipe_config->pipe_bpp;
9448
9449 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9450 connector->base.base.id,
c23cc417 9451 connector->base.name);
050f7aeb
DV
9452
9453 /* Don't use an invalid EDID bpc value */
9454 if (connector->base.display_info.bpc &&
9455 connector->base.display_info.bpc * 3 < bpp) {
9456 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9457 bpp, connector->base.display_info.bpc*3);
9458 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9459 }
9460
9461 /* Clamp bpp to 8 on screens without EDID 1.4 */
9462 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9463 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9464 bpp);
9465 pipe_config->pipe_bpp = 24;
9466 }
9467}
9468
4e53c2e0 9469static int
050f7aeb
DV
9470compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9471 struct drm_framebuffer *fb,
9472 struct intel_crtc_config *pipe_config)
4e53c2e0 9473{
050f7aeb
DV
9474 struct drm_device *dev = crtc->base.dev;
9475 struct intel_connector *connector;
4e53c2e0
DV
9476 int bpp;
9477
d42264b1
DV
9478 switch (fb->pixel_format) {
9479 case DRM_FORMAT_C8:
4e53c2e0
DV
9480 bpp = 8*3; /* since we go through a colormap */
9481 break;
d42264b1
DV
9482 case DRM_FORMAT_XRGB1555:
9483 case DRM_FORMAT_ARGB1555:
9484 /* checked in intel_framebuffer_init already */
9485 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9486 return -EINVAL;
9487 case DRM_FORMAT_RGB565:
4e53c2e0
DV
9488 bpp = 6*3; /* min is 18bpp */
9489 break;
d42264b1
DV
9490 case DRM_FORMAT_XBGR8888:
9491 case DRM_FORMAT_ABGR8888:
9492 /* checked in intel_framebuffer_init already */
9493 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9494 return -EINVAL;
9495 case DRM_FORMAT_XRGB8888:
9496 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
9497 bpp = 8*3;
9498 break;
d42264b1
DV
9499 case DRM_FORMAT_XRGB2101010:
9500 case DRM_FORMAT_ARGB2101010:
9501 case DRM_FORMAT_XBGR2101010:
9502 case DRM_FORMAT_ABGR2101010:
9503 /* checked in intel_framebuffer_init already */
9504 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 9505 return -EINVAL;
4e53c2e0
DV
9506 bpp = 10*3;
9507 break;
baba133a 9508 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
9509 default:
9510 DRM_DEBUG_KMS("unsupported depth\n");
9511 return -EINVAL;
9512 }
9513
4e53c2e0
DV
9514 pipe_config->pipe_bpp = bpp;
9515
9516 /* Clamp display bpp to EDID value */
9517 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 9518 base.head) {
1b829e05
DV
9519 if (!connector->new_encoder ||
9520 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
9521 continue;
9522
050f7aeb 9523 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
9524 }
9525
9526 return bpp;
9527}
9528
644db711
DV
9529static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9530{
9531 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9532 "type: 0x%x flags: 0x%x\n",
1342830c 9533 mode->crtc_clock,
644db711
DV
9534 mode->crtc_hdisplay, mode->crtc_hsync_start,
9535 mode->crtc_hsync_end, mode->crtc_htotal,
9536 mode->crtc_vdisplay, mode->crtc_vsync_start,
9537 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9538}
9539
c0b03411
DV
9540static void intel_dump_pipe_config(struct intel_crtc *crtc,
9541 struct intel_crtc_config *pipe_config,
9542 const char *context)
9543{
9544 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9545 context, pipe_name(crtc->pipe));
9546
9547 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9548 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9549 pipe_config->pipe_bpp, pipe_config->dither);
9550 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9551 pipe_config->has_pch_encoder,
9552 pipe_config->fdi_lanes,
9553 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9554 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9555 pipe_config->fdi_m_n.tu);
eb14cb74
VS
9556 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9557 pipe_config->has_dp_encoder,
9558 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9559 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9560 pipe_config->dp_m_n.tu);
c0b03411
DV
9561 DRM_DEBUG_KMS("requested mode:\n");
9562 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9563 DRM_DEBUG_KMS("adjusted mode:\n");
9564 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 9565 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 9566 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
9567 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9568 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
9569 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9570 pipe_config->gmch_pfit.control,
9571 pipe_config->gmch_pfit.pgm_ratios,
9572 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 9573 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 9574 pipe_config->pch_pfit.pos,
fd4daa9c
CW
9575 pipe_config->pch_pfit.size,
9576 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 9577 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 9578 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
9579}
9580
bc079e8b
VS
9581static bool encoders_cloneable(const struct intel_encoder *a,
9582 const struct intel_encoder *b)
accfc0c5 9583{
bc079e8b
VS
9584 /* masks could be asymmetric, so check both ways */
9585 return a == b || (a->cloneable & (1 << b->type) &&
9586 b->cloneable & (1 << a->type));
9587}
9588
9589static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9590 struct intel_encoder *encoder)
9591{
9592 struct drm_device *dev = crtc->base.dev;
9593 struct intel_encoder *source_encoder;
9594
9595 list_for_each_entry(source_encoder,
9596 &dev->mode_config.encoder_list, base.head) {
9597 if (source_encoder->new_crtc != crtc)
9598 continue;
9599
9600 if (!encoders_cloneable(encoder, source_encoder))
9601 return false;
9602 }
9603
9604 return true;
9605}
9606
9607static bool check_encoder_cloning(struct intel_crtc *crtc)
9608{
9609 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
9610 struct intel_encoder *encoder;
9611
bc079e8b
VS
9612 list_for_each_entry(encoder,
9613 &dev->mode_config.encoder_list, base.head) {
9614 if (encoder->new_crtc != crtc)
accfc0c5
DV
9615 continue;
9616
bc079e8b
VS
9617 if (!check_single_encoder_cloning(crtc, encoder))
9618 return false;
accfc0c5
DV
9619 }
9620
bc079e8b 9621 return true;
accfc0c5
DV
9622}
9623
b8cecdf5
DV
9624static struct intel_crtc_config *
9625intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 9626 struct drm_framebuffer *fb,
b8cecdf5 9627 struct drm_display_mode *mode)
ee7b9f93 9628{
7758a113 9629 struct drm_device *dev = crtc->dev;
7758a113 9630 struct intel_encoder *encoder;
b8cecdf5 9631 struct intel_crtc_config *pipe_config;
e29c22c0
DV
9632 int plane_bpp, ret = -EINVAL;
9633 bool retry = true;
ee7b9f93 9634
bc079e8b 9635 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
9636 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9637 return ERR_PTR(-EINVAL);
9638 }
9639
b8cecdf5
DV
9640 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9641 if (!pipe_config)
7758a113
DV
9642 return ERR_PTR(-ENOMEM);
9643
b8cecdf5
DV
9644 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9645 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 9646
e143a21c
DV
9647 pipe_config->cpu_transcoder =
9648 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 9649 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 9650
2960bc9c
ID
9651 /*
9652 * Sanitize sync polarity flags based on requested ones. If neither
9653 * positive or negative polarity is requested, treat this as meaning
9654 * negative polarity.
9655 */
9656 if (!(pipe_config->adjusted_mode.flags &
9657 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9658 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9659
9660 if (!(pipe_config->adjusted_mode.flags &
9661 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9662 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9663
050f7aeb
DV
9664 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9665 * plane pixel format and any sink constraints into account. Returns the
9666 * source plane bpp so that dithering can be selected on mismatches
9667 * after encoders and crtc also have had their say. */
9668 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9669 fb, pipe_config);
4e53c2e0
DV
9670 if (plane_bpp < 0)
9671 goto fail;
9672
e41a56be
VS
9673 /*
9674 * Determine the real pipe dimensions. Note that stereo modes can
9675 * increase the actual pipe size due to the frame doubling and
9676 * insertion of additional space for blanks between the frame. This
9677 * is stored in the crtc timings. We use the requested mode to do this
9678 * computation to clearly distinguish it from the adjusted mode, which
9679 * can be changed by the connectors in the below retry loop.
9680 */
9681 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9682 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9683 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9684
e29c22c0 9685encoder_retry:
ef1b460d 9686 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 9687 pipe_config->port_clock = 0;
ef1b460d 9688 pipe_config->pixel_multiplier = 1;
ff9a6750 9689
135c81b8 9690 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 9691 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 9692
7758a113
DV
9693 /* Pass our mode to the connectors and the CRTC to give them a chance to
9694 * adjust it according to limitations or connector properties, and also
9695 * a chance to reject the mode entirely.
47f1c6c9 9696 */
7758a113
DV
9697 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9698 base.head) {
47f1c6c9 9699
7758a113
DV
9700 if (&encoder->new_crtc->base != crtc)
9701 continue;
7ae89233 9702
efea6e8e
DV
9703 if (!(encoder->compute_config(encoder, pipe_config))) {
9704 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
9705 goto fail;
9706 }
ee7b9f93 9707 }
47f1c6c9 9708
ff9a6750
DV
9709 /* Set default port clock if not overwritten by the encoder. Needs to be
9710 * done afterwards in case the encoder adjusts the mode. */
9711 if (!pipe_config->port_clock)
241bfc38
DL
9712 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9713 * pipe_config->pixel_multiplier;
ff9a6750 9714
a43f6e0f 9715 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 9716 if (ret < 0) {
7758a113
DV
9717 DRM_DEBUG_KMS("CRTC fixup failed\n");
9718 goto fail;
ee7b9f93 9719 }
e29c22c0
DV
9720
9721 if (ret == RETRY) {
9722 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9723 ret = -EINVAL;
9724 goto fail;
9725 }
9726
9727 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9728 retry = false;
9729 goto encoder_retry;
9730 }
9731
4e53c2e0
DV
9732 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9733 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9734 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9735
b8cecdf5 9736 return pipe_config;
7758a113 9737fail:
b8cecdf5 9738 kfree(pipe_config);
e29c22c0 9739 return ERR_PTR(ret);
ee7b9f93 9740}
47f1c6c9 9741
e2e1ed41
DV
9742/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9743 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9744static void
9745intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9746 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
9747{
9748 struct intel_crtc *intel_crtc;
e2e1ed41
DV
9749 struct drm_device *dev = crtc->dev;
9750 struct intel_encoder *encoder;
9751 struct intel_connector *connector;
9752 struct drm_crtc *tmp_crtc;
79e53945 9753
e2e1ed41 9754 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 9755
e2e1ed41
DV
9756 /* Check which crtcs have changed outputs connected to them, these need
9757 * to be part of the prepare_pipes mask. We don't (yet) support global
9758 * modeset across multiple crtcs, so modeset_pipes will only have one
9759 * bit set at most. */
9760 list_for_each_entry(connector, &dev->mode_config.connector_list,
9761 base.head) {
9762 if (connector->base.encoder == &connector->new_encoder->base)
9763 continue;
79e53945 9764
e2e1ed41
DV
9765 if (connector->base.encoder) {
9766 tmp_crtc = connector->base.encoder->crtc;
9767
9768 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9769 }
9770
9771 if (connector->new_encoder)
9772 *prepare_pipes |=
9773 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
9774 }
9775
e2e1ed41
DV
9776 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9777 base.head) {
9778 if (encoder->base.crtc == &encoder->new_crtc->base)
9779 continue;
9780
9781 if (encoder->base.crtc) {
9782 tmp_crtc = encoder->base.crtc;
9783
9784 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9785 }
9786
9787 if (encoder->new_crtc)
9788 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
9789 }
9790
7668851f 9791 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 9792 for_each_intel_crtc(dev, intel_crtc) {
7668851f 9793 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 9794 continue;
7e7d76c3 9795
7668851f 9796 if (!intel_crtc->new_enabled)
e2e1ed41 9797 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
9798 else
9799 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
9800 }
9801
e2e1ed41
DV
9802
9803 /* set_mode is also used to update properties on life display pipes. */
9804 intel_crtc = to_intel_crtc(crtc);
7668851f 9805 if (intel_crtc->new_enabled)
e2e1ed41
DV
9806 *prepare_pipes |= 1 << intel_crtc->pipe;
9807
b6c5164d
DV
9808 /*
9809 * For simplicity do a full modeset on any pipe where the output routing
9810 * changed. We could be more clever, but that would require us to be
9811 * more careful with calling the relevant encoder->mode_set functions.
9812 */
e2e1ed41
DV
9813 if (*prepare_pipes)
9814 *modeset_pipes = *prepare_pipes;
9815
9816 /* ... and mask these out. */
9817 *modeset_pipes &= ~(*disable_pipes);
9818 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
9819
9820 /*
9821 * HACK: We don't (yet) fully support global modesets. intel_set_config
9822 * obies this rule, but the modeset restore mode of
9823 * intel_modeset_setup_hw_state does not.
9824 */
9825 *modeset_pipes &= 1 << intel_crtc->pipe;
9826 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
9827
9828 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9829 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 9830}
79e53945 9831
ea9d758d 9832static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 9833{
ea9d758d 9834 struct drm_encoder *encoder;
f6e5b160 9835 struct drm_device *dev = crtc->dev;
f6e5b160 9836
ea9d758d
DV
9837 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9838 if (encoder->crtc == crtc)
9839 return true;
9840
9841 return false;
9842}
9843
9844static void
9845intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9846{
9847 struct intel_encoder *intel_encoder;
9848 struct intel_crtc *intel_crtc;
9849 struct drm_connector *connector;
9850
9851 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9852 base.head) {
9853 if (!intel_encoder->base.crtc)
9854 continue;
9855
9856 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9857
9858 if (prepare_pipes & (1 << intel_crtc->pipe))
9859 intel_encoder->connectors_active = false;
9860 }
9861
9862 intel_modeset_commit_output_state(dev);
9863
7668851f 9864 /* Double check state. */
d3fcc808 9865 for_each_intel_crtc(dev, intel_crtc) {
7668851f 9866 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
9867 WARN_ON(intel_crtc->new_config &&
9868 intel_crtc->new_config != &intel_crtc->config);
9869 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
9870 }
9871
9872 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9873 if (!connector->encoder || !connector->encoder->crtc)
9874 continue;
9875
9876 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9877
9878 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
9879 struct drm_property *dpms_property =
9880 dev->mode_config.dpms_property;
9881
ea9d758d 9882 connector->dpms = DRM_MODE_DPMS_ON;
662595df 9883 drm_object_property_set_value(&connector->base,
68d34720
DV
9884 dpms_property,
9885 DRM_MODE_DPMS_ON);
ea9d758d
DV
9886
9887 intel_encoder = to_intel_encoder(connector->encoder);
9888 intel_encoder->connectors_active = true;
9889 }
9890 }
9891
9892}
9893
3bd26263 9894static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 9895{
3bd26263 9896 int diff;
f1f644dc
JB
9897
9898 if (clock1 == clock2)
9899 return true;
9900
9901 if (!clock1 || !clock2)
9902 return false;
9903
9904 diff = abs(clock1 - clock2);
9905
9906 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9907 return true;
9908
9909 return false;
9910}
9911
25c5b266
DV
9912#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9913 list_for_each_entry((intel_crtc), \
9914 &(dev)->mode_config.crtc_list, \
9915 base.head) \
0973f18f 9916 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 9917
0e8ffe1b 9918static bool
2fa2fe9a
DV
9919intel_pipe_config_compare(struct drm_device *dev,
9920 struct intel_crtc_config *current_config,
0e8ffe1b
DV
9921 struct intel_crtc_config *pipe_config)
9922{
66e985c0
DV
9923#define PIPE_CONF_CHECK_X(name) \
9924 if (current_config->name != pipe_config->name) { \
9925 DRM_ERROR("mismatch in " #name " " \
9926 "(expected 0x%08x, found 0x%08x)\n", \
9927 current_config->name, \
9928 pipe_config->name); \
9929 return false; \
9930 }
9931
08a24034
DV
9932#define PIPE_CONF_CHECK_I(name) \
9933 if (current_config->name != pipe_config->name) { \
9934 DRM_ERROR("mismatch in " #name " " \
9935 "(expected %i, found %i)\n", \
9936 current_config->name, \
9937 pipe_config->name); \
9938 return false; \
88adfff1
DV
9939 }
9940
1bd1bd80
DV
9941#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9942 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 9943 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
9944 "(expected %i, found %i)\n", \
9945 current_config->name & (mask), \
9946 pipe_config->name & (mask)); \
9947 return false; \
9948 }
9949
5e550656
VS
9950#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9951 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9952 DRM_ERROR("mismatch in " #name " " \
9953 "(expected %i, found %i)\n", \
9954 current_config->name, \
9955 pipe_config->name); \
9956 return false; \
9957 }
9958
bb760063
DV
9959#define PIPE_CONF_QUIRK(quirk) \
9960 ((current_config->quirks | pipe_config->quirks) & (quirk))
9961
eccb140b
DV
9962 PIPE_CONF_CHECK_I(cpu_transcoder);
9963
08a24034
DV
9964 PIPE_CONF_CHECK_I(has_pch_encoder);
9965 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
9966 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9967 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9968 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9969 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9970 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 9971
eb14cb74
VS
9972 PIPE_CONF_CHECK_I(has_dp_encoder);
9973 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9974 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9975 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9976 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9977 PIPE_CONF_CHECK_I(dp_m_n.tu);
9978
1bd1bd80
DV
9979 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9980 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9981 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9982 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9983 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9984 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9985
9986 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9987 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9988 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9989 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9990 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9991 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9992
c93f54cf 9993 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 9994 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
9995 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
9996 IS_VALLEYVIEW(dev))
9997 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 9998
9ed109a7
DV
9999 PIPE_CONF_CHECK_I(has_audio);
10000
1bd1bd80
DV
10001 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10002 DRM_MODE_FLAG_INTERLACE);
10003
bb760063
DV
10004 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10005 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10006 DRM_MODE_FLAG_PHSYNC);
10007 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10008 DRM_MODE_FLAG_NHSYNC);
10009 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10010 DRM_MODE_FLAG_PVSYNC);
10011 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10012 DRM_MODE_FLAG_NVSYNC);
10013 }
045ac3b5 10014
37327abd
VS
10015 PIPE_CONF_CHECK_I(pipe_src_w);
10016 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10017
9953599b
DV
10018 /*
10019 * FIXME: BIOS likes to set up a cloned config with lvds+external
10020 * screen. Since we don't yet re-compute the pipe config when moving
10021 * just the lvds port away to another pipe the sw tracking won't match.
10022 *
10023 * Proper atomic modesets with recomputed global state will fix this.
10024 * Until then just don't check gmch state for inherited modes.
10025 */
10026 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10027 PIPE_CONF_CHECK_I(gmch_pfit.control);
10028 /* pfit ratios are autocomputed by the hw on gen4+ */
10029 if (INTEL_INFO(dev)->gen < 4)
10030 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10031 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10032 }
10033
fd4daa9c
CW
10034 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10035 if (current_config->pch_pfit.enabled) {
10036 PIPE_CONF_CHECK_I(pch_pfit.pos);
10037 PIPE_CONF_CHECK_I(pch_pfit.size);
10038 }
2fa2fe9a 10039
e59150dc
JB
10040 /* BDW+ don't expose a synchronous way to read the state */
10041 if (IS_HASWELL(dev))
10042 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10043
282740f7
VS
10044 PIPE_CONF_CHECK_I(double_wide);
10045
c0d43d62 10046 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10047 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10048 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10049 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10050 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
c0d43d62 10051
42571aef
VS
10052 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10053 PIPE_CONF_CHECK_I(pipe_bpp);
10054
a9a7e98a
JB
10055 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10056 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10057
66e985c0 10058#undef PIPE_CONF_CHECK_X
08a24034 10059#undef PIPE_CONF_CHECK_I
1bd1bd80 10060#undef PIPE_CONF_CHECK_FLAGS
5e550656 10061#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10062#undef PIPE_CONF_QUIRK
88adfff1 10063
0e8ffe1b
DV
10064 return true;
10065}
10066
91d1b4bd
DV
10067static void
10068check_connector_state(struct drm_device *dev)
8af6cf88 10069{
8af6cf88
DV
10070 struct intel_connector *connector;
10071
10072 list_for_each_entry(connector, &dev->mode_config.connector_list,
10073 base.head) {
10074 /* This also checks the encoder/connector hw state with the
10075 * ->get_hw_state callbacks. */
10076 intel_connector_check_state(connector);
10077
10078 WARN(&connector->new_encoder->base != connector->base.encoder,
10079 "connector's staged encoder doesn't match current encoder\n");
10080 }
91d1b4bd
DV
10081}
10082
10083static void
10084check_encoder_state(struct drm_device *dev)
10085{
10086 struct intel_encoder *encoder;
10087 struct intel_connector *connector;
8af6cf88
DV
10088
10089 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10090 base.head) {
10091 bool enabled = false;
10092 bool active = false;
10093 enum pipe pipe, tracked_pipe;
10094
10095 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10096 encoder->base.base.id,
8e329a03 10097 encoder->base.name);
8af6cf88
DV
10098
10099 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10100 "encoder's stage crtc doesn't match current crtc\n");
10101 WARN(encoder->connectors_active && !encoder->base.crtc,
10102 "encoder's active_connectors set, but no crtc\n");
10103
10104 list_for_each_entry(connector, &dev->mode_config.connector_list,
10105 base.head) {
10106 if (connector->base.encoder != &encoder->base)
10107 continue;
10108 enabled = true;
10109 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10110 active = true;
10111 }
10112 WARN(!!encoder->base.crtc != enabled,
10113 "encoder's enabled state mismatch "
10114 "(expected %i, found %i)\n",
10115 !!encoder->base.crtc, enabled);
10116 WARN(active && !encoder->base.crtc,
10117 "active encoder with no crtc\n");
10118
10119 WARN(encoder->connectors_active != active,
10120 "encoder's computed active state doesn't match tracked active state "
10121 "(expected %i, found %i)\n", active, encoder->connectors_active);
10122
10123 active = encoder->get_hw_state(encoder, &pipe);
10124 WARN(active != encoder->connectors_active,
10125 "encoder's hw state doesn't match sw tracking "
10126 "(expected %i, found %i)\n",
10127 encoder->connectors_active, active);
10128
10129 if (!encoder->base.crtc)
10130 continue;
10131
10132 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10133 WARN(active && pipe != tracked_pipe,
10134 "active encoder's pipe doesn't match"
10135 "(expected %i, found %i)\n",
10136 tracked_pipe, pipe);
10137
10138 }
91d1b4bd
DV
10139}
10140
10141static void
10142check_crtc_state(struct drm_device *dev)
10143{
fbee40df 10144 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10145 struct intel_crtc *crtc;
10146 struct intel_encoder *encoder;
10147 struct intel_crtc_config pipe_config;
8af6cf88 10148
d3fcc808 10149 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10150 bool enabled = false;
10151 bool active = false;
10152
045ac3b5
JB
10153 memset(&pipe_config, 0, sizeof(pipe_config));
10154
8af6cf88
DV
10155 DRM_DEBUG_KMS("[CRTC:%d]\n",
10156 crtc->base.base.id);
10157
10158 WARN(crtc->active && !crtc->base.enabled,
10159 "active crtc, but not enabled in sw tracking\n");
10160
10161 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10162 base.head) {
10163 if (encoder->base.crtc != &crtc->base)
10164 continue;
10165 enabled = true;
10166 if (encoder->connectors_active)
10167 active = true;
10168 }
6c49f241 10169
8af6cf88
DV
10170 WARN(active != crtc->active,
10171 "crtc's computed active state doesn't match tracked active state "
10172 "(expected %i, found %i)\n", active, crtc->active);
10173 WARN(enabled != crtc->base.enabled,
10174 "crtc's computed enabled state doesn't match tracked enabled state "
10175 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10176
0e8ffe1b
DV
10177 active = dev_priv->display.get_pipe_config(crtc,
10178 &pipe_config);
d62cf62a
DV
10179
10180 /* hw state is inconsistent with the pipe A quirk */
10181 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10182 active = crtc->active;
10183
6c49f241
DV
10184 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10185 base.head) {
3eaba51c 10186 enum pipe pipe;
6c49f241
DV
10187 if (encoder->base.crtc != &crtc->base)
10188 continue;
1d37b689 10189 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10190 encoder->get_config(encoder, &pipe_config);
10191 }
10192
0e8ffe1b
DV
10193 WARN(crtc->active != active,
10194 "crtc active state doesn't match with hw state "
10195 "(expected %i, found %i)\n", crtc->active, active);
10196
c0b03411
DV
10197 if (active &&
10198 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10199 WARN(1, "pipe state doesn't match!\n");
10200 intel_dump_pipe_config(crtc, &pipe_config,
10201 "[hw state]");
10202 intel_dump_pipe_config(crtc, &crtc->config,
10203 "[sw state]");
10204 }
8af6cf88
DV
10205 }
10206}
10207
91d1b4bd
DV
10208static void
10209check_shared_dpll_state(struct drm_device *dev)
10210{
fbee40df 10211 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10212 struct intel_crtc *crtc;
10213 struct intel_dpll_hw_state dpll_hw_state;
10214 int i;
5358901f
DV
10215
10216 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10217 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10218 int enabled_crtcs = 0, active_crtcs = 0;
10219 bool active;
10220
10221 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10222
10223 DRM_DEBUG_KMS("%s\n", pll->name);
10224
10225 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10226
10227 WARN(pll->active > pll->refcount,
10228 "more active pll users than references: %i vs %i\n",
10229 pll->active, pll->refcount);
10230 WARN(pll->active && !pll->on,
10231 "pll in active use but not on in sw tracking\n");
35c95375
DV
10232 WARN(pll->on && !pll->active,
10233 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10234 WARN(pll->on != active,
10235 "pll on state mismatch (expected %i, found %i)\n",
10236 pll->on, active);
10237
d3fcc808 10238 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10239 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10240 enabled_crtcs++;
10241 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10242 active_crtcs++;
10243 }
10244 WARN(pll->active != active_crtcs,
10245 "pll active crtcs mismatch (expected %i, found %i)\n",
10246 pll->active, active_crtcs);
10247 WARN(pll->refcount != enabled_crtcs,
10248 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10249 pll->refcount, enabled_crtcs);
66e985c0
DV
10250
10251 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10252 sizeof(dpll_hw_state)),
10253 "pll hw state mismatch\n");
5358901f 10254 }
8af6cf88
DV
10255}
10256
91d1b4bd
DV
10257void
10258intel_modeset_check_state(struct drm_device *dev)
10259{
10260 check_connector_state(dev);
10261 check_encoder_state(dev);
10262 check_crtc_state(dev);
10263 check_shared_dpll_state(dev);
10264}
10265
18442d08
VS
10266void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10267 int dotclock)
10268{
10269 /*
10270 * FDI already provided one idea for the dotclock.
10271 * Yell if the encoder disagrees.
10272 */
241bfc38 10273 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10274 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10275 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10276}
10277
80715b2f
VS
10278static void update_scanline_offset(struct intel_crtc *crtc)
10279{
10280 struct drm_device *dev = crtc->base.dev;
10281
10282 /*
10283 * The scanline counter increments at the leading edge of hsync.
10284 *
10285 * On most platforms it starts counting from vtotal-1 on the
10286 * first active line. That means the scanline counter value is
10287 * always one less than what we would expect. Ie. just after
10288 * start of vblank, which also occurs at start of hsync (on the
10289 * last active line), the scanline counter will read vblank_start-1.
10290 *
10291 * On gen2 the scanline counter starts counting from 1 instead
10292 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10293 * to keep the value positive), instead of adding one.
10294 *
10295 * On HSW+ the behaviour of the scanline counter depends on the output
10296 * type. For DP ports it behaves like most other platforms, but on HDMI
10297 * there's an extra 1 line difference. So we need to add two instead of
10298 * one to the value.
10299 */
10300 if (IS_GEN2(dev)) {
10301 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10302 int vtotal;
10303
10304 vtotal = mode->crtc_vtotal;
10305 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10306 vtotal /= 2;
10307
10308 crtc->scanline_offset = vtotal - 1;
10309 } else if (HAS_DDI(dev) &&
10310 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10311 crtc->scanline_offset = 2;
10312 } else
10313 crtc->scanline_offset = 1;
10314}
10315
f30da187
DV
10316static int __intel_set_mode(struct drm_crtc *crtc,
10317 struct drm_display_mode *mode,
10318 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10319{
10320 struct drm_device *dev = crtc->dev;
fbee40df 10321 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10322 struct drm_display_mode *saved_mode;
b8cecdf5 10323 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10324 struct intel_crtc *intel_crtc;
10325 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10326 int ret = 0;
a6778b3c 10327
4b4b9238 10328 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10329 if (!saved_mode)
10330 return -ENOMEM;
a6778b3c 10331
e2e1ed41 10332 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10333 &prepare_pipes, &disable_pipes);
10334
3ac18232 10335 *saved_mode = crtc->mode;
a6778b3c 10336
25c5b266
DV
10337 /* Hack: Because we don't (yet) support global modeset on multiple
10338 * crtcs, we don't keep track of the new mode for more than one crtc.
10339 * Hence simply check whether any bit is set in modeset_pipes in all the
10340 * pieces of code that are not yet converted to deal with mutliple crtcs
10341 * changing their mode at the same time. */
25c5b266 10342 if (modeset_pipes) {
4e53c2e0 10343 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10344 if (IS_ERR(pipe_config)) {
10345 ret = PTR_ERR(pipe_config);
10346 pipe_config = NULL;
10347
3ac18232 10348 goto out;
25c5b266 10349 }
c0b03411
DV
10350 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10351 "[modeset]");
50741abc 10352 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10353 }
a6778b3c 10354
30a970c6
JB
10355 /*
10356 * See if the config requires any additional preparation, e.g.
10357 * to adjust global state with pipes off. We need to do this
10358 * here so we can get the modeset_pipe updated config for the new
10359 * mode set on this crtc. For other crtcs we need to use the
10360 * adjusted_mode bits in the crtc directly.
10361 */
c164f833 10362 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10363 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10364
c164f833
VS
10365 /* may have added more to prepare_pipes than we should */
10366 prepare_pipes &= ~disable_pipes;
10367 }
10368
460da916
DV
10369 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10370 intel_crtc_disable(&intel_crtc->base);
10371
ea9d758d
DV
10372 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10373 if (intel_crtc->base.enabled)
10374 dev_priv->display.crtc_disable(&intel_crtc->base);
10375 }
a6778b3c 10376
6c4c86f5
DV
10377 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10378 * to set it here already despite that we pass it down the callchain.
f6e5b160 10379 */
b8cecdf5 10380 if (modeset_pipes) {
25c5b266 10381 crtc->mode = *mode;
b8cecdf5
DV
10382 /* mode_set/enable/disable functions rely on a correct pipe
10383 * config. */
10384 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10385 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10386
10387 /*
10388 * Calculate and store various constants which
10389 * are later needed by vblank and swap-completion
10390 * timestamping. They are derived from true hwmode.
10391 */
10392 drm_calc_timestamping_constants(crtc,
10393 &pipe_config->adjusted_mode);
b8cecdf5 10394 }
7758a113 10395
ea9d758d
DV
10396 /* Only after disabling all output pipelines that will be changed can we
10397 * update the the output configuration. */
10398 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10399
47fab737
DV
10400 if (dev_priv->display.modeset_global_resources)
10401 dev_priv->display.modeset_global_resources(dev);
10402
a6778b3c
DV
10403 /* Set up the DPLL and any encoders state that needs to adjust or depend
10404 * on the DPLL.
f6e5b160 10405 */
25c5b266 10406 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
4c10794f
DV
10407 struct drm_framebuffer *old_fb;
10408
10409 mutex_lock(&dev->struct_mutex);
10410 ret = intel_pin_and_fence_fb_obj(dev,
10411 to_intel_framebuffer(fb)->obj,
10412 NULL);
10413 if (ret != 0) {
10414 DRM_ERROR("pin & fence failed\n");
10415 mutex_unlock(&dev->struct_mutex);
10416 goto done;
10417 }
10418 old_fb = crtc->primary->fb;
10419 if (old_fb)
10420 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
10421 mutex_unlock(&dev->struct_mutex);
10422
10423 crtc->primary->fb = fb;
10424 crtc->x = x;
10425 crtc->y = y;
10426
4271b753
DV
10427 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10428 x, y, fb);
c0c36b94
CW
10429 if (ret)
10430 goto done;
a6778b3c
DV
10431 }
10432
10433 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
10434 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10435 update_scanline_offset(intel_crtc);
10436
25c5b266 10437 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 10438 }
a6778b3c 10439
a6778b3c
DV
10440 /* FIXME: add subpixel order */
10441done:
4b4b9238 10442 if (ret && crtc->enabled)
3ac18232 10443 crtc->mode = *saved_mode;
a6778b3c 10444
3ac18232 10445out:
b8cecdf5 10446 kfree(pipe_config);
3ac18232 10447 kfree(saved_mode);
a6778b3c 10448 return ret;
f6e5b160
CW
10449}
10450
e7457a9a
DL
10451static int intel_set_mode(struct drm_crtc *crtc,
10452 struct drm_display_mode *mode,
10453 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
10454{
10455 int ret;
10456
10457 ret = __intel_set_mode(crtc, mode, x, y, fb);
10458
10459 if (ret == 0)
10460 intel_modeset_check_state(crtc->dev);
10461
10462 return ret;
10463}
10464
c0c36b94
CW
10465void intel_crtc_restore_mode(struct drm_crtc *crtc)
10466{
f4510a27 10467 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
10468}
10469
25c5b266
DV
10470#undef for_each_intel_crtc_masked
10471
d9e55608
DV
10472static void intel_set_config_free(struct intel_set_config *config)
10473{
10474 if (!config)
10475 return;
10476
1aa4b628
DV
10477 kfree(config->save_connector_encoders);
10478 kfree(config->save_encoder_crtcs);
7668851f 10479 kfree(config->save_crtc_enabled);
d9e55608
DV
10480 kfree(config);
10481}
10482
85f9eb71
DV
10483static int intel_set_config_save_state(struct drm_device *dev,
10484 struct intel_set_config *config)
10485{
7668851f 10486 struct drm_crtc *crtc;
85f9eb71
DV
10487 struct drm_encoder *encoder;
10488 struct drm_connector *connector;
10489 int count;
10490
7668851f
VS
10491 config->save_crtc_enabled =
10492 kcalloc(dev->mode_config.num_crtc,
10493 sizeof(bool), GFP_KERNEL);
10494 if (!config->save_crtc_enabled)
10495 return -ENOMEM;
10496
1aa4b628
DV
10497 config->save_encoder_crtcs =
10498 kcalloc(dev->mode_config.num_encoder,
10499 sizeof(struct drm_crtc *), GFP_KERNEL);
10500 if (!config->save_encoder_crtcs)
85f9eb71
DV
10501 return -ENOMEM;
10502
1aa4b628
DV
10503 config->save_connector_encoders =
10504 kcalloc(dev->mode_config.num_connector,
10505 sizeof(struct drm_encoder *), GFP_KERNEL);
10506 if (!config->save_connector_encoders)
85f9eb71
DV
10507 return -ENOMEM;
10508
10509 /* Copy data. Note that driver private data is not affected.
10510 * Should anything bad happen only the expected state is
10511 * restored, not the drivers personal bookkeeping.
10512 */
7668851f 10513 count = 0;
70e1e0ec 10514 for_each_crtc(dev, crtc) {
7668851f
VS
10515 config->save_crtc_enabled[count++] = crtc->enabled;
10516 }
10517
85f9eb71
DV
10518 count = 0;
10519 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 10520 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
10521 }
10522
10523 count = 0;
10524 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 10525 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
10526 }
10527
10528 return 0;
10529}
10530
10531static void intel_set_config_restore_state(struct drm_device *dev,
10532 struct intel_set_config *config)
10533{
7668851f 10534 struct intel_crtc *crtc;
9a935856
DV
10535 struct intel_encoder *encoder;
10536 struct intel_connector *connector;
85f9eb71
DV
10537 int count;
10538
7668851f 10539 count = 0;
d3fcc808 10540 for_each_intel_crtc(dev, crtc) {
7668851f 10541 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
10542
10543 if (crtc->new_enabled)
10544 crtc->new_config = &crtc->config;
10545 else
10546 crtc->new_config = NULL;
7668851f
VS
10547 }
10548
85f9eb71 10549 count = 0;
9a935856
DV
10550 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10551 encoder->new_crtc =
10552 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
10553 }
10554
10555 count = 0;
9a935856
DV
10556 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10557 connector->new_encoder =
10558 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
10559 }
10560}
10561
e3de42b6 10562static bool
2e57f47d 10563is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
10564{
10565 int i;
10566
2e57f47d
CW
10567 if (set->num_connectors == 0)
10568 return false;
10569
10570 if (WARN_ON(set->connectors == NULL))
10571 return false;
10572
10573 for (i = 0; i < set->num_connectors; i++)
10574 if (set->connectors[i]->encoder &&
10575 set->connectors[i]->encoder->crtc == set->crtc &&
10576 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
10577 return true;
10578
10579 return false;
10580}
10581
5e2b584e
DV
10582static void
10583intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10584 struct intel_set_config *config)
10585{
10586
10587 /* We should be able to check here if the fb has the same properties
10588 * and then just flip_or_move it */
2e57f47d
CW
10589 if (is_crtc_connector_off(set)) {
10590 config->mode_changed = true;
f4510a27 10591 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
10592 /*
10593 * If we have no fb, we can only flip as long as the crtc is
10594 * active, otherwise we need a full mode set. The crtc may
10595 * be active if we've only disabled the primary plane, or
10596 * in fastboot situations.
10597 */
f4510a27 10598 if (set->crtc->primary->fb == NULL) {
319d9827
JB
10599 struct intel_crtc *intel_crtc =
10600 to_intel_crtc(set->crtc);
10601
3b150f08 10602 if (intel_crtc->active) {
319d9827
JB
10603 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10604 config->fb_changed = true;
10605 } else {
10606 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10607 config->mode_changed = true;
10608 }
5e2b584e
DV
10609 } else if (set->fb == NULL) {
10610 config->mode_changed = true;
72f4901e 10611 } else if (set->fb->pixel_format !=
f4510a27 10612 set->crtc->primary->fb->pixel_format) {
5e2b584e 10613 config->mode_changed = true;
e3de42b6 10614 } else {
5e2b584e 10615 config->fb_changed = true;
e3de42b6 10616 }
5e2b584e
DV
10617 }
10618
835c5873 10619 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
10620 config->fb_changed = true;
10621
10622 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10623 DRM_DEBUG_KMS("modes are different, full mode set\n");
10624 drm_mode_debug_printmodeline(&set->crtc->mode);
10625 drm_mode_debug_printmodeline(set->mode);
10626 config->mode_changed = true;
10627 }
a1d95703
CW
10628
10629 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10630 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
10631}
10632
2e431051 10633static int
9a935856
DV
10634intel_modeset_stage_output_state(struct drm_device *dev,
10635 struct drm_mode_set *set,
10636 struct intel_set_config *config)
50f56119 10637{
9a935856
DV
10638 struct intel_connector *connector;
10639 struct intel_encoder *encoder;
7668851f 10640 struct intel_crtc *crtc;
f3f08572 10641 int ro;
50f56119 10642
9abdda74 10643 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
10644 * of connectors. For paranoia, double-check this. */
10645 WARN_ON(!set->fb && (set->num_connectors != 0));
10646 WARN_ON(set->fb && (set->num_connectors == 0));
10647
9a935856
DV
10648 list_for_each_entry(connector, &dev->mode_config.connector_list,
10649 base.head) {
10650 /* Otherwise traverse passed in connector list and get encoders
10651 * for them. */
50f56119 10652 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856
DV
10653 if (set->connectors[ro] == &connector->base) {
10654 connector->new_encoder = connector->encoder;
50f56119
DV
10655 break;
10656 }
10657 }
10658
9a935856
DV
10659 /* If we disable the crtc, disable all its connectors. Also, if
10660 * the connector is on the changing crtc but not on the new
10661 * connector list, disable it. */
10662 if ((!set->fb || ro == set->num_connectors) &&
10663 connector->base.encoder &&
10664 connector->base.encoder->crtc == set->crtc) {
10665 connector->new_encoder = NULL;
10666
10667 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10668 connector->base.base.id,
c23cc417 10669 connector->base.name);
9a935856
DV
10670 }
10671
10672
10673 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 10674 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 10675 config->mode_changed = true;
50f56119
DV
10676 }
10677 }
9a935856 10678 /* connector->new_encoder is now updated for all connectors. */
50f56119 10679
9a935856 10680 /* Update crtc of enabled connectors. */
9a935856
DV
10681 list_for_each_entry(connector, &dev->mode_config.connector_list,
10682 base.head) {
7668851f
VS
10683 struct drm_crtc *new_crtc;
10684
9a935856 10685 if (!connector->new_encoder)
50f56119
DV
10686 continue;
10687
9a935856 10688 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
10689
10690 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 10691 if (set->connectors[ro] == &connector->base)
50f56119
DV
10692 new_crtc = set->crtc;
10693 }
10694
10695 /* Make sure the new CRTC will work with the encoder */
14509916
TR
10696 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10697 new_crtc)) {
5e2b584e 10698 return -EINVAL;
50f56119 10699 }
9a935856
DV
10700 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10701
10702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10703 connector->base.base.id,
c23cc417 10704 connector->base.name,
9a935856
DV
10705 new_crtc->base.id);
10706 }
10707
10708 /* Check for any encoders that needs to be disabled. */
10709 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10710 base.head) {
5a65f358 10711 int num_connectors = 0;
9a935856
DV
10712 list_for_each_entry(connector,
10713 &dev->mode_config.connector_list,
10714 base.head) {
10715 if (connector->new_encoder == encoder) {
10716 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 10717 num_connectors++;
9a935856
DV
10718 }
10719 }
5a65f358
PZ
10720
10721 if (num_connectors == 0)
10722 encoder->new_crtc = NULL;
10723 else if (num_connectors > 1)
10724 return -EINVAL;
10725
9a935856
DV
10726 /* Only now check for crtc changes so we don't miss encoders
10727 * that will be disabled. */
10728 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 10729 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 10730 config->mode_changed = true;
50f56119
DV
10731 }
10732 }
9a935856 10733 /* Now we've also updated encoder->new_crtc for all encoders. */
50f56119 10734
d3fcc808 10735 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10736 crtc->new_enabled = false;
10737
10738 list_for_each_entry(encoder,
10739 &dev->mode_config.encoder_list,
10740 base.head) {
10741 if (encoder->new_crtc == crtc) {
10742 crtc->new_enabled = true;
10743 break;
10744 }
10745 }
10746
10747 if (crtc->new_enabled != crtc->base.enabled) {
10748 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10749 crtc->new_enabled ? "en" : "dis");
10750 config->mode_changed = true;
10751 }
7bd0a8e7
VS
10752
10753 if (crtc->new_enabled)
10754 crtc->new_config = &crtc->config;
10755 else
10756 crtc->new_config = NULL;
7668851f
VS
10757 }
10758
2e431051
DV
10759 return 0;
10760}
10761
7d00a1f5
VS
10762static void disable_crtc_nofb(struct intel_crtc *crtc)
10763{
10764 struct drm_device *dev = crtc->base.dev;
10765 struct intel_encoder *encoder;
10766 struct intel_connector *connector;
10767
10768 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10769 pipe_name(crtc->pipe));
10770
10771 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10772 if (connector->new_encoder &&
10773 connector->new_encoder->new_crtc == crtc)
10774 connector->new_encoder = NULL;
10775 }
10776
10777 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10778 if (encoder->new_crtc == crtc)
10779 encoder->new_crtc = NULL;
10780 }
10781
10782 crtc->new_enabled = false;
7bd0a8e7 10783 crtc->new_config = NULL;
7d00a1f5
VS
10784}
10785
2e431051
DV
10786static int intel_crtc_set_config(struct drm_mode_set *set)
10787{
10788 struct drm_device *dev;
2e431051
DV
10789 struct drm_mode_set save_set;
10790 struct intel_set_config *config;
10791 int ret;
2e431051 10792
8d3e375e
DV
10793 BUG_ON(!set);
10794 BUG_ON(!set->crtc);
10795 BUG_ON(!set->crtc->helper_private);
2e431051 10796
7e53f3a4
DV
10797 /* Enforce sane interface api - has been abused by the fb helper. */
10798 BUG_ON(!set->mode && set->fb);
10799 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 10800
2e431051
DV
10801 if (set->fb) {
10802 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10803 set->crtc->base.id, set->fb->base.id,
10804 (int)set->num_connectors, set->x, set->y);
10805 } else {
10806 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
10807 }
10808
10809 dev = set->crtc->dev;
10810
10811 ret = -ENOMEM;
10812 config = kzalloc(sizeof(*config), GFP_KERNEL);
10813 if (!config)
10814 goto out_config;
10815
10816 ret = intel_set_config_save_state(dev, config);
10817 if (ret)
10818 goto out_config;
10819
10820 save_set.crtc = set->crtc;
10821 save_set.mode = &set->crtc->mode;
10822 save_set.x = set->crtc->x;
10823 save_set.y = set->crtc->y;
f4510a27 10824 save_set.fb = set->crtc->primary->fb;
2e431051
DV
10825
10826 /* Compute whether we need a full modeset, only an fb base update or no
10827 * change at all. In the future we might also check whether only the
10828 * mode changed, e.g. for LVDS where we only change the panel fitter in
10829 * such cases. */
10830 intel_set_config_compute_mode_changes(set, config);
10831
9a935856 10832 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
10833 if (ret)
10834 goto fail;
10835
5e2b584e 10836 if (config->mode_changed) {
c0c36b94
CW
10837 ret = intel_set_mode(set->crtc, set->mode,
10838 set->x, set->y, set->fb);
5e2b584e 10839 } else if (config->fb_changed) {
3b150f08
MR
10840 struct drm_i915_private *dev_priv = dev->dev_private;
10841 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
10842
4878cae2
VS
10843 intel_crtc_wait_for_pending_flips(set->crtc);
10844
4f660f49 10845 ret = intel_pipe_set_base(set->crtc,
94352cf9 10846 set->x, set->y, set->fb);
3b150f08
MR
10847
10848 /*
10849 * We need to make sure the primary plane is re-enabled if it
10850 * has previously been turned off.
10851 */
10852 if (!intel_crtc->primary_enabled && ret == 0) {
10853 WARN_ON(!intel_crtc->active);
10854 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
10855 intel_crtc->pipe);
10856 }
10857
7ca51a3a
JB
10858 /*
10859 * In the fastboot case this may be our only check of the
10860 * state after boot. It would be better to only do it on
10861 * the first update, but we don't have a nice way of doing that
10862 * (and really, set_config isn't used much for high freq page
10863 * flipping, so increasing its cost here shouldn't be a big
10864 * deal).
10865 */
d330a953 10866 if (i915.fastboot && ret == 0)
7ca51a3a 10867 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
10868 }
10869
2d05eae1 10870 if (ret) {
bf67dfeb
DV
10871 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10872 set->crtc->base.id, ret);
50f56119 10873fail:
2d05eae1 10874 intel_set_config_restore_state(dev, config);
50f56119 10875
7d00a1f5
VS
10876 /*
10877 * HACK: if the pipe was on, but we didn't have a framebuffer,
10878 * force the pipe off to avoid oopsing in the modeset code
10879 * due to fb==NULL. This should only happen during boot since
10880 * we don't yet reconstruct the FB from the hardware state.
10881 */
10882 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10883 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10884
2d05eae1
CW
10885 /* Try to restore the config */
10886 if (config->mode_changed &&
10887 intel_set_mode(save_set.crtc, save_set.mode,
10888 save_set.x, save_set.y, save_set.fb))
10889 DRM_ERROR("failed to restore config after modeset failure\n");
10890 }
50f56119 10891
d9e55608
DV
10892out_config:
10893 intel_set_config_free(config);
50f56119
DV
10894 return ret;
10895}
f6e5b160
CW
10896
10897static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160
CW
10898 .cursor_set = intel_crtc_cursor_set,
10899 .cursor_move = intel_crtc_cursor_move,
10900 .gamma_set = intel_crtc_gamma_set,
50f56119 10901 .set_config = intel_crtc_set_config,
f6e5b160
CW
10902 .destroy = intel_crtc_destroy,
10903 .page_flip = intel_crtc_page_flip,
10904};
10905
79f689aa
PZ
10906static void intel_cpu_pll_init(struct drm_device *dev)
10907{
affa9354 10908 if (HAS_DDI(dev))
79f689aa
PZ
10909 intel_ddi_pll_init(dev);
10910}
10911
5358901f
DV
10912static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10913 struct intel_shared_dpll *pll,
10914 struct intel_dpll_hw_state *hw_state)
ee7b9f93 10915{
5358901f 10916 uint32_t val;
ee7b9f93 10917
5358901f 10918 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
10919 hw_state->dpll = val;
10920 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10921 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
10922
10923 return val & DPLL_VCO_ENABLE;
10924}
10925
15bdd4cf
DV
10926static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10927 struct intel_shared_dpll *pll)
10928{
10929 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10930 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10931}
10932
e7b903d2
DV
10933static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10934 struct intel_shared_dpll *pll)
10935{
e7b903d2 10936 /* PCH refclock must be enabled first */
89eff4be 10937 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 10938
15bdd4cf
DV
10939 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10940
10941 /* Wait for the clocks to stabilize. */
10942 POSTING_READ(PCH_DPLL(pll->id));
10943 udelay(150);
10944
10945 /* The pixel multiplier can only be updated once the
10946 * DPLL is enabled and the clocks are stable.
10947 *
10948 * So write it again.
10949 */
10950 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10951 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10952 udelay(200);
10953}
10954
10955static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10956 struct intel_shared_dpll *pll)
10957{
10958 struct drm_device *dev = dev_priv->dev;
10959 struct intel_crtc *crtc;
e7b903d2
DV
10960
10961 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 10962 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
10963 if (intel_crtc_to_shared_dpll(crtc) == pll)
10964 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
10965 }
10966
15bdd4cf
DV
10967 I915_WRITE(PCH_DPLL(pll->id), 0);
10968 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10969 udelay(200);
10970}
10971
46edb027
DV
10972static char *ibx_pch_dpll_names[] = {
10973 "PCH DPLL A",
10974 "PCH DPLL B",
10975};
10976
7c74ade1 10977static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 10978{
e7b903d2 10979 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
10980 int i;
10981
7c74ade1 10982 dev_priv->num_shared_dpll = 2;
ee7b9f93 10983
e72f9fbf 10984 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
10985 dev_priv->shared_dplls[i].id = i;
10986 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 10987 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
10988 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10989 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
10990 dev_priv->shared_dplls[i].get_hw_state =
10991 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
10992 }
10993}
10994
7c74ade1
DV
10995static void intel_shared_dpll_init(struct drm_device *dev)
10996{
e7b903d2 10997 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1
DV
10998
10999 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11000 ibx_pch_dpll_init(dev);
11001 else
11002 dev_priv->num_shared_dpll = 0;
11003
11004 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11005}
11006
465c120c
MR
11007static int
11008intel_primary_plane_disable(struct drm_plane *plane)
11009{
11010 struct drm_device *dev = plane->dev;
11011 struct drm_i915_private *dev_priv = dev->dev_private;
11012 struct intel_plane *intel_plane = to_intel_plane(plane);
11013 struct intel_crtc *intel_crtc;
11014
11015 if (!plane->fb)
11016 return 0;
11017
11018 BUG_ON(!plane->crtc);
11019
11020 intel_crtc = to_intel_crtc(plane->crtc);
11021
11022 /*
11023 * Even though we checked plane->fb above, it's still possible that
11024 * the primary plane has been implicitly disabled because the crtc
11025 * coordinates given weren't visible, or because we detected
11026 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11027 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11028 * In either case, we need to unpin the FB and let the fb pointer get
11029 * updated, but otherwise we don't need to touch the hardware.
11030 */
11031 if (!intel_crtc->primary_enabled)
11032 goto disable_unpin;
11033
11034 intel_crtc_wait_for_pending_flips(plane->crtc);
11035 intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
11036 intel_plane->pipe);
11037
11038disable_unpin:
11039 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11040 plane->fb = NULL;
11041
11042 return 0;
11043}
11044
11045static int
11046intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11047 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11048 unsigned int crtc_w, unsigned int crtc_h,
11049 uint32_t src_x, uint32_t src_y,
11050 uint32_t src_w, uint32_t src_h)
11051{
11052 struct drm_device *dev = crtc->dev;
11053 struct drm_i915_private *dev_priv = dev->dev_private;
11054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11055 struct intel_plane *intel_plane = to_intel_plane(plane);
11056 struct drm_rect dest = {
11057 /* integer pixels */
11058 .x1 = crtc_x,
11059 .y1 = crtc_y,
11060 .x2 = crtc_x + crtc_w,
11061 .y2 = crtc_y + crtc_h,
11062 };
11063 struct drm_rect src = {
11064 /* 16.16 fixed point */
11065 .x1 = src_x,
11066 .y1 = src_y,
11067 .x2 = src_x + src_w,
11068 .y2 = src_y + src_h,
11069 };
11070 const struct drm_rect clip = {
11071 /* integer pixels */
11072 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11073 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11074 };
11075 bool visible;
11076 int ret;
11077
11078 ret = drm_plane_helper_check_update(plane, crtc, fb,
11079 &src, &dest, &clip,
11080 DRM_PLANE_HELPER_NO_SCALING,
11081 DRM_PLANE_HELPER_NO_SCALING,
11082 false, true, &visible);
11083
11084 if (ret)
11085 return ret;
11086
11087 /*
11088 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11089 * updating the fb pointer, and returning without touching the
11090 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11091 * turn on the display with all planes setup as desired.
11092 */
11093 if (!crtc->enabled) {
11094 /*
11095 * If we already called setplane while the crtc was disabled,
11096 * we may have an fb pinned; unpin it.
11097 */
11098 if (plane->fb)
11099 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11100
11101 /* Pin and return without programming hardware */
11102 return intel_pin_and_fence_fb_obj(dev,
11103 to_intel_framebuffer(fb)->obj,
11104 NULL);
11105 }
11106
11107 intel_crtc_wait_for_pending_flips(crtc);
11108
11109 /*
11110 * If clipping results in a non-visible primary plane, we'll disable
11111 * the primary plane. Note that this is a bit different than what
11112 * happens if userspace explicitly disables the plane by passing fb=0
11113 * because plane->fb still gets set and pinned.
11114 */
11115 if (!visible) {
11116 /*
11117 * Try to pin the new fb first so that we can bail out if we
11118 * fail.
11119 */
11120 if (plane->fb != fb) {
11121 ret = intel_pin_and_fence_fb_obj(dev,
11122 to_intel_framebuffer(fb)->obj,
11123 NULL);
11124 if (ret)
11125 return ret;
11126 }
11127
11128 if (intel_crtc->primary_enabled)
11129 intel_disable_primary_hw_plane(dev_priv,
11130 intel_plane->plane,
11131 intel_plane->pipe);
11132
11133
11134 if (plane->fb != fb)
11135 if (plane->fb)
11136 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11137
11138 return 0;
11139 }
11140
11141 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11142 if (ret)
11143 return ret;
11144
11145 if (!intel_crtc->primary_enabled)
11146 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11147 intel_crtc->pipe);
11148
11149 return 0;
11150}
11151
11152static void intel_primary_plane_destroy(struct drm_plane *plane)
11153{
11154 struct intel_plane *intel_plane = to_intel_plane(plane);
11155 drm_plane_cleanup(plane);
11156 kfree(intel_plane);
11157}
11158
11159static const struct drm_plane_funcs intel_primary_plane_funcs = {
11160 .update_plane = intel_primary_plane_setplane,
11161 .disable_plane = intel_primary_plane_disable,
11162 .destroy = intel_primary_plane_destroy,
11163};
11164
11165static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11166 int pipe)
11167{
11168 struct intel_plane *primary;
11169 const uint32_t *intel_primary_formats;
11170 int num_formats;
11171
11172 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11173 if (primary == NULL)
11174 return NULL;
11175
11176 primary->can_scale = false;
11177 primary->max_downscale = 1;
11178 primary->pipe = pipe;
11179 primary->plane = pipe;
11180 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11181 primary->plane = !pipe;
11182
11183 if (INTEL_INFO(dev)->gen <= 3) {
11184 intel_primary_formats = intel_primary_formats_gen2;
11185 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11186 } else {
11187 intel_primary_formats = intel_primary_formats_gen4;
11188 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11189 }
11190
11191 drm_universal_plane_init(dev, &primary->base, 0,
11192 &intel_primary_plane_funcs,
11193 intel_primary_formats, num_formats,
11194 DRM_PLANE_TYPE_PRIMARY);
11195 return &primary->base;
11196}
11197
b358d0a6 11198static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 11199{
fbee40df 11200 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 11201 struct intel_crtc *intel_crtc;
465c120c
MR
11202 struct drm_plane *primary;
11203 int i, ret;
79e53945 11204
955382f3 11205 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
11206 if (intel_crtc == NULL)
11207 return;
11208
465c120c
MR
11209 primary = intel_primary_plane_create(dev, pipe);
11210 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
11211 NULL, &intel_crtc_funcs);
11212 if (ret) {
11213 drm_plane_cleanup(primary);
11214 kfree(intel_crtc);
11215 return;
11216 }
79e53945
JB
11217
11218 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
11219 for (i = 0; i < 256; i++) {
11220 intel_crtc->lut_r[i] = i;
11221 intel_crtc->lut_g[i] = i;
11222 intel_crtc->lut_b[i] = i;
11223 }
11224
1f1c2e24
VS
11225 /*
11226 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
11227 * is hooked to plane B. Hence we want plane A feeding pipe B.
11228 */
80824003
JB
11229 intel_crtc->pipe = pipe;
11230 intel_crtc->plane = pipe;
3a77c4c4 11231 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 11232 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 11233 intel_crtc->plane = !pipe;
80824003
JB
11234 }
11235
4b0e333e
CW
11236 intel_crtc->cursor_base = ~0;
11237 intel_crtc->cursor_cntl = ~0;
11238
8d7849db
VS
11239 init_waitqueue_head(&intel_crtc->vbl_wait);
11240
22fd0fab
JB
11241 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11242 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11243 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11244 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11245
79e53945 11246 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
11247
11248 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
79e53945
JB
11249}
11250
752aa88a
JB
11251enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11252{
11253 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 11254 struct drm_device *dev = connector->base.dev;
752aa88a 11255
51fd371b 11256 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
11257
11258 if (!encoder)
11259 return INVALID_PIPE;
11260
11261 return to_intel_crtc(encoder->crtc)->pipe;
11262}
11263
08d7b3d1 11264int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 11265 struct drm_file *file)
08d7b3d1 11266{
08d7b3d1 11267 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
11268 struct drm_mode_object *drmmode_obj;
11269 struct intel_crtc *crtc;
08d7b3d1 11270
1cff8f6b
DV
11271 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11272 return -ENODEV;
08d7b3d1 11273
c05422d5
DV
11274 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11275 DRM_MODE_OBJECT_CRTC);
08d7b3d1 11276
c05422d5 11277 if (!drmmode_obj) {
08d7b3d1 11278 DRM_ERROR("no such CRTC id\n");
3f2c2057 11279 return -ENOENT;
08d7b3d1
CW
11280 }
11281
c05422d5
DV
11282 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11283 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 11284
c05422d5 11285 return 0;
08d7b3d1
CW
11286}
11287
66a9278e 11288static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 11289{
66a9278e
DV
11290 struct drm_device *dev = encoder->base.dev;
11291 struct intel_encoder *source_encoder;
79e53945 11292 int index_mask = 0;
79e53945
JB
11293 int entry = 0;
11294
66a9278e
DV
11295 list_for_each_entry(source_encoder,
11296 &dev->mode_config.encoder_list, base.head) {
bc079e8b 11297 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
11298 index_mask |= (1 << entry);
11299
79e53945
JB
11300 entry++;
11301 }
4ef69c7a 11302
79e53945
JB
11303 return index_mask;
11304}
11305
4d302442
CW
11306static bool has_edp_a(struct drm_device *dev)
11307{
11308 struct drm_i915_private *dev_priv = dev->dev_private;
11309
11310 if (!IS_MOBILE(dev))
11311 return false;
11312
11313 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11314 return false;
11315
e3589908 11316 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
11317 return false;
11318
11319 return true;
11320}
11321
ba0fbca4
DL
11322const char *intel_output_name(int output)
11323{
11324 static const char *names[] = {
11325 [INTEL_OUTPUT_UNUSED] = "Unused",
11326 [INTEL_OUTPUT_ANALOG] = "Analog",
11327 [INTEL_OUTPUT_DVO] = "DVO",
11328 [INTEL_OUTPUT_SDVO] = "SDVO",
11329 [INTEL_OUTPUT_LVDS] = "LVDS",
11330 [INTEL_OUTPUT_TVOUT] = "TV",
11331 [INTEL_OUTPUT_HDMI] = "HDMI",
11332 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11333 [INTEL_OUTPUT_EDP] = "eDP",
11334 [INTEL_OUTPUT_DSI] = "DSI",
11335 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11336 };
11337
11338 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11339 return "Invalid";
11340
11341 return names[output];
11342}
11343
79e53945
JB
11344static void intel_setup_outputs(struct drm_device *dev)
11345{
725e30ad 11346 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 11347 struct intel_encoder *encoder;
cb0953d7 11348 bool dpd_is_edp = false;
79e53945 11349
c9093354 11350 intel_lvds_init(dev);
79e53945 11351
27da3bdf 11352 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
79935fca 11353 intel_crt_init(dev);
cb0953d7 11354
affa9354 11355 if (HAS_DDI(dev)) {
0e72a5b5
ED
11356 int found;
11357
11358 /* Haswell uses DDI functions to detect digital outputs */
11359 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11360 /* DDI A only supports eDP */
11361 if (found)
11362 intel_ddi_init(dev, PORT_A);
11363
11364 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11365 * register */
11366 found = I915_READ(SFUSE_STRAP);
11367
11368 if (found & SFUSE_STRAP_DDIB_DETECTED)
11369 intel_ddi_init(dev, PORT_B);
11370 if (found & SFUSE_STRAP_DDIC_DETECTED)
11371 intel_ddi_init(dev, PORT_C);
11372 if (found & SFUSE_STRAP_DDID_DETECTED)
11373 intel_ddi_init(dev, PORT_D);
11374 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 11375 int found;
5d8a7752 11376 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
11377
11378 if (has_edp_a(dev))
11379 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 11380
dc0fa718 11381 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 11382 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 11383 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 11384 if (!found)
e2debe91 11385 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 11386 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 11387 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
11388 }
11389
dc0fa718 11390 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 11391 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 11392
dc0fa718 11393 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 11394 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 11395
5eb08b69 11396 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 11397 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 11398
270b3042 11399 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 11400 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 11401 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
11402 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11403 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11404 PORT_B);
11405 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11406 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11407 }
11408
6f6005a5
JB
11409 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11410 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11411 PORT_C);
11412 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 11413 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 11414 }
19c03924 11415
9418c1f1
VS
11416 if (IS_CHERRYVIEW(dev)) {
11417 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11418 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11419 PORT_D);
11420 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11421 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11422 }
11423 }
11424
3cfca973 11425 intel_dsi_init(dev);
103a196f 11426 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 11427 bool found = false;
7d57382e 11428
e2debe91 11429 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 11430 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 11431 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
11432 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11433 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 11434 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 11435 }
27185ae1 11436
e7281eab 11437 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 11438 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 11439 }
13520b05
KH
11440
11441 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 11442
e2debe91 11443 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 11444 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 11445 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 11446 }
27185ae1 11447
e2debe91 11448 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 11449
b01f2c3a
JB
11450 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11451 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 11452 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 11453 }
e7281eab 11454 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 11455 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 11456 }
27185ae1 11457
b01f2c3a 11458 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 11459 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 11460 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 11461 } else if (IS_GEN2(dev))
79e53945
JB
11462 intel_dvo_init(dev);
11463
103a196f 11464 if (SUPPORTS_TV(dev))
79e53945
JB
11465 intel_tv_init(dev);
11466
4ef69c7a
CW
11467 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11468 encoder->base.possible_crtcs = encoder->crtc_mask;
11469 encoder->base.possible_clones =
66a9278e 11470 intel_encoder_clones(encoder);
79e53945 11471 }
47356eb6 11472
dde86e2d 11473 intel_init_pch_refclk(dev);
270b3042
DV
11474
11475 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
11476}
11477
11478static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11479{
11480 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 11481
ef2d633e
DV
11482 drm_framebuffer_cleanup(fb);
11483 WARN_ON(!intel_fb->obj->framebuffer_references--);
11484 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
79e53945
JB
11485 kfree(intel_fb);
11486}
11487
11488static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 11489 struct drm_file *file,
79e53945
JB
11490 unsigned int *handle)
11491{
11492 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 11493 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 11494
05394f39 11495 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
11496}
11497
11498static const struct drm_framebuffer_funcs intel_fb_funcs = {
11499 .destroy = intel_user_framebuffer_destroy,
11500 .create_handle = intel_user_framebuffer_create_handle,
11501};
11502
b5ea642a
DV
11503static int intel_framebuffer_init(struct drm_device *dev,
11504 struct intel_framebuffer *intel_fb,
11505 struct drm_mode_fb_cmd2 *mode_cmd,
11506 struct drm_i915_gem_object *obj)
79e53945 11507{
a57ce0b2 11508 int aligned_height;
a35cdaa0 11509 int pitch_limit;
79e53945
JB
11510 int ret;
11511
dd4916c5
DV
11512 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11513
c16ed4be
CW
11514 if (obj->tiling_mode == I915_TILING_Y) {
11515 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 11516 return -EINVAL;
c16ed4be 11517 }
57cd6508 11518
c16ed4be
CW
11519 if (mode_cmd->pitches[0] & 63) {
11520 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11521 mode_cmd->pitches[0]);
57cd6508 11522 return -EINVAL;
c16ed4be 11523 }
57cd6508 11524
a35cdaa0
CW
11525 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11526 pitch_limit = 32*1024;
11527 } else if (INTEL_INFO(dev)->gen >= 4) {
11528 if (obj->tiling_mode)
11529 pitch_limit = 16*1024;
11530 else
11531 pitch_limit = 32*1024;
11532 } else if (INTEL_INFO(dev)->gen >= 3) {
11533 if (obj->tiling_mode)
11534 pitch_limit = 8*1024;
11535 else
11536 pitch_limit = 16*1024;
11537 } else
11538 /* XXX DSPC is limited to 4k tiled */
11539 pitch_limit = 8*1024;
11540
11541 if (mode_cmd->pitches[0] > pitch_limit) {
11542 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11543 obj->tiling_mode ? "tiled" : "linear",
11544 mode_cmd->pitches[0], pitch_limit);
5d7bd705 11545 return -EINVAL;
c16ed4be 11546 }
5d7bd705
VS
11547
11548 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
11549 mode_cmd->pitches[0] != obj->stride) {
11550 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11551 mode_cmd->pitches[0], obj->stride);
5d7bd705 11552 return -EINVAL;
c16ed4be 11553 }
5d7bd705 11554
57779d06 11555 /* Reject formats not supported by any plane early. */
308e5bcb 11556 switch (mode_cmd->pixel_format) {
57779d06 11557 case DRM_FORMAT_C8:
04b3924d
VS
11558 case DRM_FORMAT_RGB565:
11559 case DRM_FORMAT_XRGB8888:
11560 case DRM_FORMAT_ARGB8888:
57779d06
VS
11561 break;
11562 case DRM_FORMAT_XRGB1555:
11563 case DRM_FORMAT_ARGB1555:
c16ed4be 11564 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
11565 DRM_DEBUG("unsupported pixel format: %s\n",
11566 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11567 return -EINVAL;
c16ed4be 11568 }
57779d06
VS
11569 break;
11570 case DRM_FORMAT_XBGR8888:
11571 case DRM_FORMAT_ABGR8888:
04b3924d
VS
11572 case DRM_FORMAT_XRGB2101010:
11573 case DRM_FORMAT_ARGB2101010:
57779d06
VS
11574 case DRM_FORMAT_XBGR2101010:
11575 case DRM_FORMAT_ABGR2101010:
c16ed4be 11576 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
11577 DRM_DEBUG("unsupported pixel format: %s\n",
11578 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11579 return -EINVAL;
c16ed4be 11580 }
b5626747 11581 break;
04b3924d
VS
11582 case DRM_FORMAT_YUYV:
11583 case DRM_FORMAT_UYVY:
11584 case DRM_FORMAT_YVYU:
11585 case DRM_FORMAT_VYUY:
c16ed4be 11586 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
11587 DRM_DEBUG("unsupported pixel format: %s\n",
11588 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11589 return -EINVAL;
c16ed4be 11590 }
57cd6508
CW
11591 break;
11592 default:
4ee62c76
VS
11593 DRM_DEBUG("unsupported pixel format: %s\n",
11594 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
11595 return -EINVAL;
11596 }
11597
90f9a336
VS
11598 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11599 if (mode_cmd->offsets[0] != 0)
11600 return -EINVAL;
11601
a57ce0b2
JB
11602 aligned_height = intel_align_height(dev, mode_cmd->height,
11603 obj->tiling_mode);
53155c0a
DV
11604 /* FIXME drm helper for size checks (especially planar formats)? */
11605 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11606 return -EINVAL;
11607
c7d73f6a
DV
11608 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11609 intel_fb->obj = obj;
80075d49 11610 intel_fb->obj->framebuffer_references++;
c7d73f6a 11611
79e53945
JB
11612 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11613 if (ret) {
11614 DRM_ERROR("framebuffer init failed %d\n", ret);
11615 return ret;
11616 }
11617
79e53945
JB
11618 return 0;
11619}
11620
79e53945
JB
11621static struct drm_framebuffer *
11622intel_user_framebuffer_create(struct drm_device *dev,
11623 struct drm_file *filp,
308e5bcb 11624 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 11625{
05394f39 11626 struct drm_i915_gem_object *obj;
79e53945 11627
308e5bcb
JB
11628 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11629 mode_cmd->handles[0]));
c8725226 11630 if (&obj->base == NULL)
cce13ff7 11631 return ERR_PTR(-ENOENT);
79e53945 11632
d2dff872 11633 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
11634}
11635
4520f53a 11636#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 11637static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
11638{
11639}
11640#endif
11641
79e53945 11642static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 11643 .fb_create = intel_user_framebuffer_create,
0632fef6 11644 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
11645};
11646
e70236a8
JB
11647/* Set up chip specific display functions */
11648static void intel_init_display(struct drm_device *dev)
11649{
11650 struct drm_i915_private *dev_priv = dev->dev_private;
11651
ee9300bb
DV
11652 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11653 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
11654 else if (IS_CHERRYVIEW(dev))
11655 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
11656 else if (IS_VALLEYVIEW(dev))
11657 dev_priv->display.find_dpll = vlv_find_best_dpll;
11658 else if (IS_PINEVIEW(dev))
11659 dev_priv->display.find_dpll = pnv_find_best_dpll;
11660 else
11661 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11662
affa9354 11663 if (HAS_DDI(dev)) {
0e8ffe1b 11664 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 11665 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 11666 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
11667 dev_priv->display.crtc_enable = haswell_crtc_enable;
11668 dev_priv->display.crtc_disable = haswell_crtc_disable;
6441ab5f 11669 dev_priv->display.off = haswell_crtc_off;
262ca2b0
MR
11670 dev_priv->display.update_primary_plane =
11671 ironlake_update_primary_plane;
09b4ddf9 11672 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 11673 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 11674 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 11675 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
11676 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11677 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 11678 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
11679 dev_priv->display.update_primary_plane =
11680 ironlake_update_primary_plane;
89b667f8
JB
11681 } else if (IS_VALLEYVIEW(dev)) {
11682 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11683 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
11684 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11685 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11686 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11687 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11688 dev_priv->display.update_primary_plane =
11689 i9xx_update_primary_plane;
f564048e 11690 } else {
0e8ffe1b 11691 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11692 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 11693 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
11694 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11695 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 11696 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11697 dev_priv->display.update_primary_plane =
11698 i9xx_update_primary_plane;
f564048e 11699 }
e70236a8 11700
e70236a8 11701 /* Returns the core display clock speed */
25eb05fc
JB
11702 if (IS_VALLEYVIEW(dev))
11703 dev_priv->display.get_display_clock_speed =
11704 valleyview_get_display_clock_speed;
11705 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
11706 dev_priv->display.get_display_clock_speed =
11707 i945_get_display_clock_speed;
11708 else if (IS_I915G(dev))
11709 dev_priv->display.get_display_clock_speed =
11710 i915_get_display_clock_speed;
257a7ffc 11711 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
11712 dev_priv->display.get_display_clock_speed =
11713 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
11714 else if (IS_PINEVIEW(dev))
11715 dev_priv->display.get_display_clock_speed =
11716 pnv_get_display_clock_speed;
e70236a8
JB
11717 else if (IS_I915GM(dev))
11718 dev_priv->display.get_display_clock_speed =
11719 i915gm_get_display_clock_speed;
11720 else if (IS_I865G(dev))
11721 dev_priv->display.get_display_clock_speed =
11722 i865_get_display_clock_speed;
f0f8a9ce 11723 else if (IS_I85X(dev))
e70236a8
JB
11724 dev_priv->display.get_display_clock_speed =
11725 i855_get_display_clock_speed;
11726 else /* 852, 830 */
11727 dev_priv->display.get_display_clock_speed =
11728 i830_get_display_clock_speed;
11729
7f8a8569 11730 if (HAS_PCH_SPLIT(dev)) {
f00a3ddf 11731 if (IS_GEN5(dev)) {
674cf967 11732 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
e0dac65e 11733 dev_priv->display.write_eld = ironlake_write_eld;
1398261a 11734 } else if (IS_GEN6(dev)) {
674cf967 11735 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
e0dac65e 11736 dev_priv->display.write_eld = ironlake_write_eld;
9a952a0d
PZ
11737 dev_priv->display.modeset_global_resources =
11738 snb_modeset_global_resources;
357555c0
JB
11739 } else if (IS_IVYBRIDGE(dev)) {
11740 /* FIXME: detect B0+ stepping and use auto training */
11741 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
e0dac65e 11742 dev_priv->display.write_eld = ironlake_write_eld;
01a415fd
DV
11743 dev_priv->display.modeset_global_resources =
11744 ivb_modeset_global_resources;
4e0bbc31 11745 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
c82e4d26 11746 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
83358c85 11747 dev_priv->display.write_eld = haswell_write_eld;
d6dd9eb1
DV
11748 dev_priv->display.modeset_global_resources =
11749 haswell_modeset_global_resources;
a0e63c22 11750 }
6067aaea 11751 } else if (IS_G4X(dev)) {
e0dac65e 11752 dev_priv->display.write_eld = g4x_write_eld;
30a970c6
JB
11753 } else if (IS_VALLEYVIEW(dev)) {
11754 dev_priv->display.modeset_global_resources =
11755 valleyview_modeset_global_resources;
9ca2fe73 11756 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 11757 }
8c9f3aaf
JB
11758
11759 /* Default just returns -ENODEV to indicate unsupported */
11760 dev_priv->display.queue_flip = intel_default_queue_flip;
11761
11762 switch (INTEL_INFO(dev)->gen) {
11763 case 2:
11764 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11765 break;
11766
11767 case 3:
11768 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11769 break;
11770
11771 case 4:
11772 case 5:
11773 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11774 break;
11775
11776 case 6:
11777 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11778 break;
7c9017e5 11779 case 7:
4e0bbc31 11780 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
11781 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11782 break;
8c9f3aaf 11783 }
7bd688cd
JN
11784
11785 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
11786}
11787
b690e96c
JB
11788/*
11789 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11790 * resume, or other times. This quirk makes sure that's the case for
11791 * affected systems.
11792 */
0206e353 11793static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
11794{
11795 struct drm_i915_private *dev_priv = dev->dev_private;
11796
11797 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 11798 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
11799}
11800
435793df
KP
11801/*
11802 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11803 */
11804static void quirk_ssc_force_disable(struct drm_device *dev)
11805{
11806 struct drm_i915_private *dev_priv = dev->dev_private;
11807 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 11808 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
11809}
11810
4dca20ef 11811/*
5a15ab5b
CE
11812 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11813 * brightness value
4dca20ef
CE
11814 */
11815static void quirk_invert_brightness(struct drm_device *dev)
11816{
11817 struct drm_i915_private *dev_priv = dev->dev_private;
11818 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 11819 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
11820}
11821
b690e96c
JB
11822struct intel_quirk {
11823 int device;
11824 int subsystem_vendor;
11825 int subsystem_device;
11826 void (*hook)(struct drm_device *dev);
11827};
11828
5f85f176
EE
11829/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11830struct intel_dmi_quirk {
11831 void (*hook)(struct drm_device *dev);
11832 const struct dmi_system_id (*dmi_id_list)[];
11833};
11834
11835static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11836{
11837 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11838 return 1;
11839}
11840
11841static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11842 {
11843 .dmi_id_list = &(const struct dmi_system_id[]) {
11844 {
11845 .callback = intel_dmi_reverse_brightness,
11846 .ident = "NCR Corporation",
11847 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11848 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11849 },
11850 },
11851 { } /* terminating entry */
11852 },
11853 .hook = quirk_invert_brightness,
11854 },
11855};
11856
c43b5634 11857static struct intel_quirk intel_quirks[] = {
b690e96c 11858 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 11859 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 11860
b690e96c
JB
11861 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11862 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11863
b690e96c
JB
11864 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11865 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11866
435793df
KP
11867 /* Lenovo U160 cannot use SSC on LVDS */
11868 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
11869
11870 /* Sony Vaio Y cannot use SSC on LVDS */
11871 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 11872
be505f64
AH
11873 /* Acer Aspire 5734Z must invert backlight brightness */
11874 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11875
11876 /* Acer/eMachines G725 */
11877 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11878
11879 /* Acer/eMachines e725 */
11880 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11881
11882 /* Acer/Packard Bell NCL20 */
11883 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11884
11885 /* Acer Aspire 4736Z */
11886 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
11887
11888 /* Acer Aspire 5336 */
11889 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
b690e96c
JB
11890};
11891
11892static void intel_init_quirks(struct drm_device *dev)
11893{
11894 struct pci_dev *d = dev->pdev;
11895 int i;
11896
11897 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11898 struct intel_quirk *q = &intel_quirks[i];
11899
11900 if (d->device == q->device &&
11901 (d->subsystem_vendor == q->subsystem_vendor ||
11902 q->subsystem_vendor == PCI_ANY_ID) &&
11903 (d->subsystem_device == q->subsystem_device ||
11904 q->subsystem_device == PCI_ANY_ID))
11905 q->hook(dev);
11906 }
5f85f176
EE
11907 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11908 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11909 intel_dmi_quirks[i].hook(dev);
11910 }
b690e96c
JB
11911}
11912
9cce37f4
JB
11913/* Disable the VGA plane that we never use */
11914static void i915_disable_vga(struct drm_device *dev)
11915{
11916 struct drm_i915_private *dev_priv = dev->dev_private;
11917 u8 sr1;
766aa1c4 11918 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 11919
2b37c616 11920 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 11921 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 11922 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
11923 sr1 = inb(VGA_SR_DATA);
11924 outb(sr1 | 1<<5, VGA_SR_DATA);
11925 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11926 udelay(300);
11927
11928 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11929 POSTING_READ(vga_reg);
11930}
11931
f817586c
DV
11932void intel_modeset_init_hw(struct drm_device *dev)
11933{
a8f78b58
ED
11934 intel_prepare_ddi(dev);
11935
f817586c
DV
11936 intel_init_clock_gating(dev);
11937
5382f5f3 11938 intel_reset_dpio(dev);
40e9cf64 11939
8090c6b9 11940 intel_enable_gt_powersave(dev);
f817586c
DV
11941}
11942
7d708ee4
ID
11943void intel_modeset_suspend_hw(struct drm_device *dev)
11944{
11945 intel_suspend_hw(dev);
11946}
11947
79e53945
JB
11948void intel_modeset_init(struct drm_device *dev)
11949{
652c393a 11950 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 11951 int sprite, ret;
8cc87b75 11952 enum pipe pipe;
46f297fb 11953 struct intel_crtc *crtc;
79e53945
JB
11954
11955 drm_mode_config_init(dev);
11956
11957 dev->mode_config.min_width = 0;
11958 dev->mode_config.min_height = 0;
11959
019d96cb
DA
11960 dev->mode_config.preferred_depth = 24;
11961 dev->mode_config.prefer_shadow = 1;
11962
e6ecefaa 11963 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 11964
b690e96c
JB
11965 intel_init_quirks(dev);
11966
1fa61106
ED
11967 intel_init_pm(dev);
11968
e3c74757
BW
11969 if (INTEL_INFO(dev)->num_pipes == 0)
11970 return;
11971
e70236a8
JB
11972 intel_init_display(dev);
11973
a6c45cf0
CW
11974 if (IS_GEN2(dev)) {
11975 dev->mode_config.max_width = 2048;
11976 dev->mode_config.max_height = 2048;
11977 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
11978 dev->mode_config.max_width = 4096;
11979 dev->mode_config.max_height = 4096;
79e53945 11980 } else {
a6c45cf0
CW
11981 dev->mode_config.max_width = 8192;
11982 dev->mode_config.max_height = 8192;
79e53945 11983 }
068be561
DL
11984
11985 if (IS_GEN2(dev)) {
11986 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
11987 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
11988 } else {
11989 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
11990 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
11991 }
11992
5d4545ae 11993 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 11994
28c97730 11995 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
11996 INTEL_INFO(dev)->num_pipes,
11997 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 11998
8cc87b75
DL
11999 for_each_pipe(pipe) {
12000 intel_crtc_init(dev, pipe);
1fe47785
DL
12001 for_each_sprite(pipe, sprite) {
12002 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12003 if (ret)
06da8da2 12004 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12005 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12006 }
79e53945
JB
12007 }
12008
f42bb70d 12009 intel_init_dpio(dev);
5382f5f3 12010 intel_reset_dpio(dev);
f42bb70d 12011
79f689aa 12012 intel_cpu_pll_init(dev);
e72f9fbf 12013 intel_shared_dpll_init(dev);
ee7b9f93 12014
9cce37f4
JB
12015 /* Just disable it once at startup */
12016 i915_disable_vga(dev);
79e53945 12017 intel_setup_outputs(dev);
11be49eb
CW
12018
12019 /* Just in case the BIOS is doing something questionable. */
12020 intel_disable_fbc(dev);
fa9fa083 12021
6e9f798d 12022 drm_modeset_lock_all(dev);
fa9fa083 12023 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12024 drm_modeset_unlock_all(dev);
46f297fb 12025
d3fcc808 12026 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12027 if (!crtc->active)
12028 continue;
12029
46f297fb 12030 /*
46f297fb
JB
12031 * Note that reserving the BIOS fb up front prevents us
12032 * from stuffing other stolen allocations like the ring
12033 * on top. This prevents some ugliness at boot time, and
12034 * can even allow for smooth boot transitions if the BIOS
12035 * fb is large enough for the active pipe configuration.
12036 */
12037 if (dev_priv->display.get_plane_config) {
12038 dev_priv->display.get_plane_config(crtc,
12039 &crtc->plane_config);
12040 /*
12041 * If the fb is shared between multiple heads, we'll
12042 * just get the first one.
12043 */
484b41dd 12044 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12045 }
46f297fb 12046 }
2c7111db
CW
12047}
12048
7fad798e
DV
12049static void intel_enable_pipe_a(struct drm_device *dev)
12050{
12051 struct intel_connector *connector;
12052 struct drm_connector *crt = NULL;
12053 struct intel_load_detect_pipe load_detect_temp;
51fd371b 12054 struct drm_modeset_acquire_ctx ctx;
7fad798e
DV
12055
12056 /* We can't just switch on the pipe A, we need to set things up with a
12057 * proper mode and output configuration. As a gross hack, enable pipe A
12058 * by enabling the load detect pipe once. */
12059 list_for_each_entry(connector,
12060 &dev->mode_config.connector_list,
12061 base.head) {
12062 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12063 crt = &connector->base;
12064 break;
12065 }
12066 }
12067
12068 if (!crt)
12069 return;
12070
51fd371b
RC
12071 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
12072 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
7fad798e 12073
652c393a 12074
7fad798e
DV
12075}
12076
fa555837
DV
12077static bool
12078intel_check_plane_mapping(struct intel_crtc *crtc)
12079{
7eb552ae
BW
12080 struct drm_device *dev = crtc->base.dev;
12081 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12082 u32 reg, val;
12083
7eb552ae 12084 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
12085 return true;
12086
12087 reg = DSPCNTR(!crtc->plane);
12088 val = I915_READ(reg);
12089
12090 if ((val & DISPLAY_PLANE_ENABLE) &&
12091 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12092 return false;
12093
12094 return true;
12095}
12096
24929352
DV
12097static void intel_sanitize_crtc(struct intel_crtc *crtc)
12098{
12099 struct drm_device *dev = crtc->base.dev;
12100 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 12101 u32 reg;
24929352 12102
24929352 12103 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 12104 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
12105 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12106
d3eaf884
VS
12107 /* restore vblank interrupts to correct state */
12108 if (crtc->active)
12109 drm_vblank_on(dev, crtc->pipe);
12110 else
12111 drm_vblank_off(dev, crtc->pipe);
12112
24929352 12113 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
12114 * disable the crtc (and hence change the state) if it is wrong. Note
12115 * that gen4+ has a fixed plane -> pipe mapping. */
12116 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
12117 struct intel_connector *connector;
12118 bool plane;
12119
24929352
DV
12120 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12121 crtc->base.base.id);
12122
12123 /* Pipe has the wrong plane attached and the plane is active.
12124 * Temporarily change the plane mapping and disable everything
12125 * ... */
12126 plane = crtc->plane;
12127 crtc->plane = !plane;
12128 dev_priv->display.crtc_disable(&crtc->base);
12129 crtc->plane = plane;
12130
12131 /* ... and break all links. */
12132 list_for_each_entry(connector, &dev->mode_config.connector_list,
12133 base.head) {
12134 if (connector->encoder->base.crtc != &crtc->base)
12135 continue;
12136
7f1950fb
EE
12137 connector->base.dpms = DRM_MODE_DPMS_OFF;
12138 connector->base.encoder = NULL;
24929352 12139 }
7f1950fb
EE
12140 /* multiple connectors may have the same encoder:
12141 * handle them and break crtc link separately */
12142 list_for_each_entry(connector, &dev->mode_config.connector_list,
12143 base.head)
12144 if (connector->encoder->base.crtc == &crtc->base) {
12145 connector->encoder->base.crtc = NULL;
12146 connector->encoder->connectors_active = false;
12147 }
24929352
DV
12148
12149 WARN_ON(crtc->active);
12150 crtc->base.enabled = false;
12151 }
24929352 12152
7fad798e
DV
12153 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12154 crtc->pipe == PIPE_A && !crtc->active) {
12155 /* BIOS forgot to enable pipe A, this mostly happens after
12156 * resume. Force-enable the pipe to fix this, the update_dpms
12157 * call below we restore the pipe to the right state, but leave
12158 * the required bits on. */
12159 intel_enable_pipe_a(dev);
12160 }
12161
24929352
DV
12162 /* Adjust the state of the output pipe according to whether we
12163 * have active connectors/encoders. */
12164 intel_crtc_update_dpms(&crtc->base);
12165
12166 if (crtc->active != crtc->base.enabled) {
12167 struct intel_encoder *encoder;
12168
12169 /* This can happen either due to bugs in the get_hw_state
12170 * functions or because the pipe is force-enabled due to the
12171 * pipe A quirk. */
12172 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12173 crtc->base.base.id,
12174 crtc->base.enabled ? "enabled" : "disabled",
12175 crtc->active ? "enabled" : "disabled");
12176
12177 crtc->base.enabled = crtc->active;
12178
12179 /* Because we only establish the connector -> encoder ->
12180 * crtc links if something is active, this means the
12181 * crtc is now deactivated. Break the links. connector
12182 * -> encoder links are only establish when things are
12183 * actually up, hence no need to break them. */
12184 WARN_ON(crtc->active);
12185
12186 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12187 WARN_ON(encoder->connectors_active);
12188 encoder->base.crtc = NULL;
12189 }
12190 }
c5ab3bc0
DV
12191
12192 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
4cc31489
DV
12193 /*
12194 * We start out with underrun reporting disabled to avoid races.
12195 * For correct bookkeeping mark this on active crtcs.
12196 *
c5ab3bc0
DV
12197 * Also on gmch platforms we dont have any hardware bits to
12198 * disable the underrun reporting. Which means we need to start
12199 * out with underrun reporting disabled also on inactive pipes,
12200 * since otherwise we'll complain about the garbage we read when
12201 * e.g. coming up after runtime pm.
12202 *
4cc31489
DV
12203 * No protection against concurrent access is required - at
12204 * worst a fifo underrun happens which also sets this to false.
12205 */
12206 crtc->cpu_fifo_underrun_disabled = true;
12207 crtc->pch_fifo_underrun_disabled = true;
80715b2f
VS
12208
12209 update_scanline_offset(crtc);
4cc31489 12210 }
24929352
DV
12211}
12212
12213static void intel_sanitize_encoder(struct intel_encoder *encoder)
12214{
12215 struct intel_connector *connector;
12216 struct drm_device *dev = encoder->base.dev;
12217
12218 /* We need to check both for a crtc link (meaning that the
12219 * encoder is active and trying to read from a pipe) and the
12220 * pipe itself being active. */
12221 bool has_active_crtc = encoder->base.crtc &&
12222 to_intel_crtc(encoder->base.crtc)->active;
12223
12224 if (encoder->connectors_active && !has_active_crtc) {
12225 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12226 encoder->base.base.id,
8e329a03 12227 encoder->base.name);
24929352
DV
12228
12229 /* Connector is active, but has no active pipe. This is
12230 * fallout from our resume register restoring. Disable
12231 * the encoder manually again. */
12232 if (encoder->base.crtc) {
12233 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12234 encoder->base.base.id,
8e329a03 12235 encoder->base.name);
24929352
DV
12236 encoder->disable(encoder);
12237 }
7f1950fb
EE
12238 encoder->base.crtc = NULL;
12239 encoder->connectors_active = false;
24929352
DV
12240
12241 /* Inconsistent output/port/pipe state happens presumably due to
12242 * a bug in one of the get_hw_state functions. Or someplace else
12243 * in our code, like the register restore mess on resume. Clamp
12244 * things to off as a safer default. */
12245 list_for_each_entry(connector,
12246 &dev->mode_config.connector_list,
12247 base.head) {
12248 if (connector->encoder != encoder)
12249 continue;
7f1950fb
EE
12250 connector->base.dpms = DRM_MODE_DPMS_OFF;
12251 connector->base.encoder = NULL;
24929352
DV
12252 }
12253 }
12254 /* Enabled encoders without active connectors will be fixed in
12255 * the crtc fixup. */
12256}
12257
04098753 12258void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
12259{
12260 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 12261 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 12262
04098753
ID
12263 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12264 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12265 i915_disable_vga(dev);
12266 }
12267}
12268
12269void i915_redisable_vga(struct drm_device *dev)
12270{
12271 struct drm_i915_private *dev_priv = dev->dev_private;
12272
8dc8a27c
PZ
12273 /* This function can be called both from intel_modeset_setup_hw_state or
12274 * at a very early point in our resume sequence, where the power well
12275 * structures are not yet restored. Since this function is at a very
12276 * paranoid "someone might have enabled VGA while we were not looking"
12277 * level, just check if the power well is enabled instead of trying to
12278 * follow the "don't touch the power well if we don't need it" policy
12279 * the rest of the driver uses. */
04098753 12280 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
12281 return;
12282
04098753 12283 i915_redisable_vga_power_on(dev);
0fde901f
KM
12284}
12285
98ec7739
VS
12286static bool primary_get_hw_state(struct intel_crtc *crtc)
12287{
12288 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12289
12290 if (!crtc->active)
12291 return false;
12292
12293 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12294}
12295
30e984df 12296static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
12297{
12298 struct drm_i915_private *dev_priv = dev->dev_private;
12299 enum pipe pipe;
24929352
DV
12300 struct intel_crtc *crtc;
12301 struct intel_encoder *encoder;
12302 struct intel_connector *connector;
5358901f 12303 int i;
24929352 12304
d3fcc808 12305 for_each_intel_crtc(dev, crtc) {
88adfff1 12306 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 12307
9953599b
DV
12308 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12309
0e8ffe1b
DV
12310 crtc->active = dev_priv->display.get_pipe_config(crtc,
12311 &crtc->config);
24929352
DV
12312
12313 crtc->base.enabled = crtc->active;
98ec7739 12314 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
12315
12316 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12317 crtc->base.base.id,
12318 crtc->active ? "enabled" : "disabled");
12319 }
12320
5358901f 12321 /* FIXME: Smash this into the new shared dpll infrastructure. */
affa9354 12322 if (HAS_DDI(dev))
6441ab5f
PZ
12323 intel_ddi_setup_hw_pll_state(dev);
12324
5358901f
DV
12325 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12326 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12327
12328 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12329 pll->active = 0;
d3fcc808 12330 for_each_intel_crtc(dev, crtc) {
5358901f
DV
12331 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12332 pll->active++;
12333 }
12334 pll->refcount = pll->active;
12335
35c95375
DV
12336 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12337 pll->name, pll->refcount, pll->on);
5358901f
DV
12338 }
12339
24929352
DV
12340 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12341 base.head) {
12342 pipe = 0;
12343
12344 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
12345 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12346 encoder->base.crtc = &crtc->base;
1d37b689 12347 encoder->get_config(encoder, &crtc->config);
24929352
DV
12348 } else {
12349 encoder->base.crtc = NULL;
12350 }
12351
12352 encoder->connectors_active = false;
6f2bcceb 12353 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 12354 encoder->base.base.id,
8e329a03 12355 encoder->base.name,
24929352 12356 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 12357 pipe_name(pipe));
24929352
DV
12358 }
12359
12360 list_for_each_entry(connector, &dev->mode_config.connector_list,
12361 base.head) {
12362 if (connector->get_hw_state(connector)) {
12363 connector->base.dpms = DRM_MODE_DPMS_ON;
12364 connector->encoder->connectors_active = true;
12365 connector->base.encoder = &connector->encoder->base;
12366 } else {
12367 connector->base.dpms = DRM_MODE_DPMS_OFF;
12368 connector->base.encoder = NULL;
12369 }
12370 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12371 connector->base.base.id,
c23cc417 12372 connector->base.name,
24929352
DV
12373 connector->base.encoder ? "enabled" : "disabled");
12374 }
30e984df
DV
12375}
12376
12377/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12378 * and i915 state tracking structures. */
12379void intel_modeset_setup_hw_state(struct drm_device *dev,
12380 bool force_restore)
12381{
12382 struct drm_i915_private *dev_priv = dev->dev_private;
12383 enum pipe pipe;
30e984df
DV
12384 struct intel_crtc *crtc;
12385 struct intel_encoder *encoder;
35c95375 12386 int i;
30e984df
DV
12387
12388 intel_modeset_readout_hw_state(dev);
24929352 12389
babea61d
JB
12390 /*
12391 * Now that we have the config, copy it to each CRTC struct
12392 * Note that this could go away if we move to using crtc_config
12393 * checking everywhere.
12394 */
d3fcc808 12395 for_each_intel_crtc(dev, crtc) {
d330a953 12396 if (crtc->active && i915.fastboot) {
f6a83288 12397 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
12398 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12399 crtc->base.base.id);
12400 drm_mode_debug_printmodeline(&crtc->base.mode);
12401 }
12402 }
12403
24929352
DV
12404 /* HW state is read out, now we need to sanitize this mess. */
12405 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12406 base.head) {
12407 intel_sanitize_encoder(encoder);
12408 }
12409
12410 for_each_pipe(pipe) {
12411 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12412 intel_sanitize_crtc(crtc);
c0b03411 12413 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 12414 }
9a935856 12415
35c95375
DV
12416 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12417 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12418
12419 if (!pll->on || pll->active)
12420 continue;
12421
12422 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12423
12424 pll->disable(dev_priv, pll);
12425 pll->on = false;
12426 }
12427
96f90c54 12428 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
12429 ilk_wm_get_hw_state(dev);
12430
45e2b5f6 12431 if (force_restore) {
7d0bc1ea
VS
12432 i915_redisable_vga(dev);
12433
f30da187
DV
12434 /*
12435 * We need to use raw interfaces for restoring state to avoid
12436 * checking (bogus) intermediate states.
12437 */
45e2b5f6 12438 for_each_pipe(pipe) {
b5644d05
JB
12439 struct drm_crtc *crtc =
12440 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
12441
12442 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 12443 crtc->primary->fb);
45e2b5f6
DV
12444 }
12445 } else {
12446 intel_modeset_update_staged_output_state(dev);
12447 }
8af6cf88
DV
12448
12449 intel_modeset_check_state(dev);
2c7111db
CW
12450}
12451
12452void intel_modeset_gem_init(struct drm_device *dev)
12453{
484b41dd
JB
12454 struct drm_crtc *c;
12455 struct intel_framebuffer *fb;
12456
ae48434c
ID
12457 mutex_lock(&dev->struct_mutex);
12458 intel_init_gt_powersave(dev);
12459 mutex_unlock(&dev->struct_mutex);
12460
1833b134 12461 intel_modeset_init_hw(dev);
02e792fb
DV
12462
12463 intel_setup_overlay(dev);
484b41dd
JB
12464
12465 /*
12466 * Make sure any fbs we allocated at startup are properly
12467 * pinned & fenced. When we do the allocation it's too early
12468 * for this.
12469 */
12470 mutex_lock(&dev->struct_mutex);
70e1e0ec 12471 for_each_crtc(dev, c) {
66e514c1 12472 if (!c->primary->fb)
484b41dd
JB
12473 continue;
12474
66e514c1 12475 fb = to_intel_framebuffer(c->primary->fb);
484b41dd
JB
12476 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12477 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12478 to_intel_crtc(c)->pipe);
66e514c1
DA
12479 drm_framebuffer_unreference(c->primary->fb);
12480 c->primary->fb = NULL;
484b41dd
JB
12481 }
12482 }
12483 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12484}
12485
4932e2c3
ID
12486void intel_connector_unregister(struct intel_connector *intel_connector)
12487{
12488 struct drm_connector *connector = &intel_connector->base;
12489
12490 intel_panel_destroy_backlight(connector);
12491 drm_sysfs_connector_remove(connector);
12492}
12493
79e53945
JB
12494void intel_modeset_cleanup(struct drm_device *dev)
12495{
652c393a
JB
12496 struct drm_i915_private *dev_priv = dev->dev_private;
12497 struct drm_crtc *crtc;
d9255d57 12498 struct drm_connector *connector;
652c393a 12499
fd0c0642
DV
12500 /*
12501 * Interrupts and polling as the first thing to avoid creating havoc.
12502 * Too much stuff here (turning of rps, connectors, ...) would
12503 * experience fancy races otherwise.
12504 */
12505 drm_irq_uninstall(dev);
12506 cancel_work_sync(&dev_priv->hotplug_work);
12507 /*
12508 * Due to the hpd irq storm handling the hotplug work can re-arm the
12509 * poll handlers. Hence disable polling after hpd handling is shut down.
12510 */
f87ea761 12511 drm_kms_helper_poll_fini(dev);
fd0c0642 12512
652c393a
JB
12513 mutex_lock(&dev->struct_mutex);
12514
723bfd70
JB
12515 intel_unregister_dsm_handler();
12516
70e1e0ec 12517 for_each_crtc(dev, crtc) {
652c393a 12518 /* Skip inactive CRTCs */
f4510a27 12519 if (!crtc->primary->fb)
652c393a
JB
12520 continue;
12521
3dec0095 12522 intel_increase_pllclock(crtc);
652c393a
JB
12523 }
12524
973d04f9 12525 intel_disable_fbc(dev);
e70236a8 12526
8090c6b9 12527 intel_disable_gt_powersave(dev);
0cdab21f 12528
930ebb46
DV
12529 ironlake_teardown_rc6(dev);
12530
69341a5e
KH
12531 mutex_unlock(&dev->struct_mutex);
12532
1630fe75
CW
12533 /* flush any delayed tasks or pending work */
12534 flush_scheduled_work();
12535
db31af1d
JN
12536 /* destroy the backlight and sysfs files before encoders/connectors */
12537 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
12538 struct intel_connector *intel_connector;
12539
12540 intel_connector = to_intel_connector(connector);
12541 intel_connector->unregister(intel_connector);
db31af1d 12542 }
d9255d57 12543
79e53945 12544 drm_mode_config_cleanup(dev);
4d7bb011
DV
12545
12546 intel_cleanup_overlay(dev);
ae48434c
ID
12547
12548 mutex_lock(&dev->struct_mutex);
12549 intel_cleanup_gt_powersave(dev);
12550 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12551}
12552
f1c79df3
ZW
12553/*
12554 * Return which encoder is currently attached for connector.
12555 */
df0e9248 12556struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 12557{
df0e9248
CW
12558 return &intel_attached_encoder(connector)->base;
12559}
f1c79df3 12560
df0e9248
CW
12561void intel_connector_attach_encoder(struct intel_connector *connector,
12562 struct intel_encoder *encoder)
12563{
12564 connector->encoder = encoder;
12565 drm_mode_connector_attach_encoder(&connector->base,
12566 &encoder->base);
79e53945 12567}
28d52043
DA
12568
12569/*
12570 * set vga decode state - true == enable VGA decode
12571 */
12572int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12573{
12574 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 12575 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
12576 u16 gmch_ctrl;
12577
75fa041d
CW
12578 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12579 DRM_ERROR("failed to read control word\n");
12580 return -EIO;
12581 }
12582
c0cc8a55
CW
12583 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12584 return 0;
12585
28d52043
DA
12586 if (state)
12587 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12588 else
12589 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
12590
12591 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12592 DRM_ERROR("failed to write control word\n");
12593 return -EIO;
12594 }
12595
28d52043
DA
12596 return 0;
12597}
c4a1d9e4 12598
c4a1d9e4 12599struct intel_display_error_state {
ff57f1b0
PZ
12600
12601 u32 power_well_driver;
12602
63b66e5b
CW
12603 int num_transcoders;
12604
c4a1d9e4
CW
12605 struct intel_cursor_error_state {
12606 u32 control;
12607 u32 position;
12608 u32 base;
12609 u32 size;
52331309 12610 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
12611
12612 struct intel_pipe_error_state {
ddf9c536 12613 bool power_domain_on;
c4a1d9e4 12614 u32 source;
f301b1e1 12615 u32 stat;
52331309 12616 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
12617
12618 struct intel_plane_error_state {
12619 u32 control;
12620 u32 stride;
12621 u32 size;
12622 u32 pos;
12623 u32 addr;
12624 u32 surface;
12625 u32 tile_offset;
52331309 12626 } plane[I915_MAX_PIPES];
63b66e5b
CW
12627
12628 struct intel_transcoder_error_state {
ddf9c536 12629 bool power_domain_on;
63b66e5b
CW
12630 enum transcoder cpu_transcoder;
12631
12632 u32 conf;
12633
12634 u32 htotal;
12635 u32 hblank;
12636 u32 hsync;
12637 u32 vtotal;
12638 u32 vblank;
12639 u32 vsync;
12640 } transcoder[4];
c4a1d9e4
CW
12641};
12642
12643struct intel_display_error_state *
12644intel_display_capture_error_state(struct drm_device *dev)
12645{
fbee40df 12646 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 12647 struct intel_display_error_state *error;
63b66e5b
CW
12648 int transcoders[] = {
12649 TRANSCODER_A,
12650 TRANSCODER_B,
12651 TRANSCODER_C,
12652 TRANSCODER_EDP,
12653 };
c4a1d9e4
CW
12654 int i;
12655
63b66e5b
CW
12656 if (INTEL_INFO(dev)->num_pipes == 0)
12657 return NULL;
12658
9d1cb914 12659 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
12660 if (error == NULL)
12661 return NULL;
12662
190be112 12663 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
12664 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12665
52331309 12666 for_each_pipe(i) {
ddf9c536 12667 error->pipe[i].power_domain_on =
da7e29bd
ID
12668 intel_display_power_enabled_sw(dev_priv,
12669 POWER_DOMAIN_PIPE(i));
ddf9c536 12670 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
12671 continue;
12672
5efb3e28
VS
12673 error->cursor[i].control = I915_READ(CURCNTR(i));
12674 error->cursor[i].position = I915_READ(CURPOS(i));
12675 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
12676
12677 error->plane[i].control = I915_READ(DSPCNTR(i));
12678 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 12679 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 12680 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
12681 error->plane[i].pos = I915_READ(DSPPOS(i));
12682 }
ca291363
PZ
12683 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12684 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
12685 if (INTEL_INFO(dev)->gen >= 4) {
12686 error->plane[i].surface = I915_READ(DSPSURF(i));
12687 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12688 }
12689
c4a1d9e4 12690 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1
ID
12691
12692 if (!HAS_PCH_SPLIT(dev))
12693 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
12694 }
12695
12696 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12697 if (HAS_DDI(dev_priv->dev))
12698 error->num_transcoders++; /* Account for eDP. */
12699
12700 for (i = 0; i < error->num_transcoders; i++) {
12701 enum transcoder cpu_transcoder = transcoders[i];
12702
ddf9c536 12703 error->transcoder[i].power_domain_on =
da7e29bd 12704 intel_display_power_enabled_sw(dev_priv,
38cc1daf 12705 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 12706 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
12707 continue;
12708
63b66e5b
CW
12709 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12710
12711 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12712 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12713 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12714 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12715 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12716 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12717 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
12718 }
12719
12720 return error;
12721}
12722
edc3d884
MK
12723#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12724
c4a1d9e4 12725void
edc3d884 12726intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
12727 struct drm_device *dev,
12728 struct intel_display_error_state *error)
12729{
12730 int i;
12731
63b66e5b
CW
12732 if (!error)
12733 return;
12734
edc3d884 12735 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 12736 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 12737 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 12738 error->power_well_driver);
52331309 12739 for_each_pipe(i) {
edc3d884 12740 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
12741 err_printf(m, " Power: %s\n",
12742 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 12743 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 12744 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
12745
12746 err_printf(m, "Plane [%d]:\n", i);
12747 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12748 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 12749 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
12750 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12751 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 12752 }
4b71a570 12753 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 12754 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 12755 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
12756 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12757 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
12758 }
12759
edc3d884
MK
12760 err_printf(m, "Cursor [%d]:\n", i);
12761 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12762 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12763 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 12764 }
63b66e5b
CW
12765
12766 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 12767 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 12768 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
12769 err_printf(m, " Power: %s\n",
12770 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
12771 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12772 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12773 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12774 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12775 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12776 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12777 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12778 }
c4a1d9e4 12779}