drm/i915: Store the initial framebuffer in initial_plane_config
[deliverable/linux.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"
c196e1d6 40#include <drm/drm_atomic_helper.h>
760285e7
DH
41#include <drm/drm_dp_helper.h>
42#include <drm/drm_crtc_helper.h>
465c120c
MR
43#include <drm/drm_plane_helper.h>
44#include <drm/drm_rect.h>
c0f372b3 45#include <linux/dma_remapping.h>
79e53945 46
465c120c
MR
47/* Primary plane formats supported by all gen */
48#define COMMON_PRIMARY_FORMATS \
49 DRM_FORMAT_C8, \
50 DRM_FORMAT_RGB565, \
51 DRM_FORMAT_XRGB8888, \
52 DRM_FORMAT_ARGB8888
53
54/* Primary plane formats for gen <= 3 */
55static const uint32_t intel_primary_formats_gen2[] = {
56 COMMON_PRIMARY_FORMATS,
57 DRM_FORMAT_XRGB1555,
58 DRM_FORMAT_ARGB1555,
59};
60
61/* Primary plane formats for gen >= 4 */
62static const uint32_t intel_primary_formats_gen4[] = {
63 COMMON_PRIMARY_FORMATS, \
64 DRM_FORMAT_XBGR8888,
65 DRM_FORMAT_ABGR8888,
66 DRM_FORMAT_XRGB2101010,
67 DRM_FORMAT_ARGB2101010,
68 DRM_FORMAT_XBGR2101010,
69 DRM_FORMAT_ABGR2101010,
70};
71
3d7d6510
MR
72/* Cursor formats */
73static const uint32_t intel_cursor_formats[] = {
74 DRM_FORMAT_ARGB8888,
75};
76
6b383a7f 77static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 78
f1f644dc 79static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 80 struct intel_crtc_state *pipe_config);
18442d08 81static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 82 struct intel_crtc_state *pipe_config);
f1f644dc 83
e7457a9a
DL
84static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
85 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
86static int intel_framebuffer_init(struct drm_device *dev,
87 struct intel_framebuffer *ifb,
88 struct drm_mode_fb_cmd2 *mode_cmd,
89 struct drm_i915_gem_object *obj);
5b18e57c
DV
90static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
91static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 92static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
93 struct intel_link_m_n *m_n,
94 struct intel_link_m_n *m2_n2);
29407aab 95static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
96static void haswell_set_pipeconf(struct drm_crtc *crtc);
97static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 98static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 99 const struct intel_crtc_state *pipe_config);
d288f65f 100static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 101 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
102static void intel_begin_crtc_commit(struct drm_crtc *crtc);
103static void intel_finish_crtc_commit(struct drm_crtc *crtc);
e7457a9a 104
0e32b39c
DA
105static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
106{
107 if (!connector->mst_port)
108 return connector->encoder;
109 else
110 return &connector->mst_port->mst_encoders[pipe]->base;
111}
112
79e53945 113typedef struct {
0206e353 114 int min, max;
79e53945
JB
115} intel_range_t;
116
117typedef struct {
0206e353
AJ
118 int dot_limit;
119 int p2_slow, p2_fast;
79e53945
JB
120} intel_p2_t;
121
d4906093
ML
122typedef struct intel_limit intel_limit_t;
123struct intel_limit {
0206e353
AJ
124 intel_range_t dot, vco, n, m, m1, m2, p, p1;
125 intel_p2_t p2;
d4906093 126};
79e53945 127
d2acd215
DV
128int
129intel_pch_rawclk(struct drm_device *dev)
130{
131 struct drm_i915_private *dev_priv = dev->dev_private;
132
133 WARN_ON(!HAS_PCH_SPLIT(dev));
134
135 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
136}
137
021357ac
CW
138static inline u32 /* units of 100MHz */
139intel_fdi_link_freq(struct drm_device *dev)
140{
8b99e68c
CW
141 if (IS_GEN5(dev)) {
142 struct drm_i915_private *dev_priv = dev->dev_private;
143 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
144 } else
145 return 27;
021357ac
CW
146}
147
5d536e28 148static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 149 .dot = { .min = 25000, .max = 350000 },
9c333719 150 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 151 .n = { .min = 2, .max = 16 },
0206e353
AJ
152 .m = { .min = 96, .max = 140 },
153 .m1 = { .min = 18, .max = 26 },
154 .m2 = { .min = 6, .max = 16 },
155 .p = { .min = 4, .max = 128 },
156 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
157 .p2 = { .dot_limit = 165000,
158 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
159};
160
5d536e28
DV
161static const intel_limit_t intel_limits_i8xx_dvo = {
162 .dot = { .min = 25000, .max = 350000 },
9c333719 163 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 164 .n = { .min = 2, .max = 16 },
5d536e28
DV
165 .m = { .min = 96, .max = 140 },
166 .m1 = { .min = 18, .max = 26 },
167 .m2 = { .min = 6, .max = 16 },
168 .p = { .min = 4, .max = 128 },
169 .p1 = { .min = 2, .max = 33 },
170 .p2 = { .dot_limit = 165000,
171 .p2_slow = 4, .p2_fast = 4 },
172};
173
e4b36699 174static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 175 .dot = { .min = 25000, .max = 350000 },
9c333719 176 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 177 .n = { .min = 2, .max = 16 },
0206e353
AJ
178 .m = { .min = 96, .max = 140 },
179 .m1 = { .min = 18, .max = 26 },
180 .m2 = { .min = 6, .max = 16 },
181 .p = { .min = 4, .max = 128 },
182 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
183 .p2 = { .dot_limit = 165000,
184 .p2_slow = 14, .p2_fast = 7 },
e4b36699 185};
273e27ca 186
e4b36699 187static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
188 .dot = { .min = 20000, .max = 400000 },
189 .vco = { .min = 1400000, .max = 2800000 },
190 .n = { .min = 1, .max = 6 },
191 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
192 .m1 = { .min = 8, .max = 18 },
193 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
194 .p = { .min = 5, .max = 80 },
195 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
196 .p2 = { .dot_limit = 200000,
197 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
198};
199
200static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
201 .dot = { .min = 20000, .max = 400000 },
202 .vco = { .min = 1400000, .max = 2800000 },
203 .n = { .min = 1, .max = 6 },
204 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
205 .m1 = { .min = 8, .max = 18 },
206 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
207 .p = { .min = 7, .max = 98 },
208 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
209 .p2 = { .dot_limit = 112000,
210 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
211};
212
273e27ca 213
e4b36699 214static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
215 .dot = { .min = 25000, .max = 270000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 10, .max = 30 },
222 .p1 = { .min = 1, .max = 3},
223 .p2 = { .dot_limit = 270000,
224 .p2_slow = 10,
225 .p2_fast = 10
044c7c41 226 },
e4b36699
KP
227};
228
229static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
230 .dot = { .min = 22000, .max = 400000 },
231 .vco = { .min = 1750000, .max = 3500000},
232 .n = { .min = 1, .max = 4 },
233 .m = { .min = 104, .max = 138 },
234 .m1 = { .min = 16, .max = 23 },
235 .m2 = { .min = 5, .max = 11 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8},
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
240};
241
242static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
243 .dot = { .min = 20000, .max = 115000 },
244 .vco = { .min = 1750000, .max = 3500000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 104, .max = 138 },
247 .m1 = { .min = 17, .max = 23 },
248 .m2 = { .min = 5, .max = 11 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 0,
252 .p2_slow = 14, .p2_fast = 14
044c7c41 253 },
e4b36699
KP
254};
255
256static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
257 .dot = { .min = 80000, .max = 224000 },
258 .vco = { .min = 1750000, .max = 3500000 },
259 .n = { .min = 1, .max = 3 },
260 .m = { .min = 104, .max = 138 },
261 .m1 = { .min = 17, .max = 23 },
262 .m2 = { .min = 5, .max = 11 },
263 .p = { .min = 14, .max = 42 },
264 .p1 = { .min = 2, .max = 6 },
265 .p2 = { .dot_limit = 0,
266 .p2_slow = 7, .p2_fast = 7
044c7c41 267 },
e4b36699
KP
268};
269
f2b115e6 270static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
271 .dot = { .min = 20000, .max = 400000},
272 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 273 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
273e27ca 276 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
277 .m1 = { .min = 0, .max = 0 },
278 .m2 = { .min = 0, .max = 254 },
279 .p = { .min = 5, .max = 80 },
280 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
281 .p2 = { .dot_limit = 200000,
282 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
283};
284
f2b115e6 285static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
286 .dot = { .min = 20000, .max = 400000 },
287 .vco = { .min = 1700000, .max = 3500000 },
288 .n = { .min = 3, .max = 6 },
289 .m = { .min = 2, .max = 256 },
290 .m1 = { .min = 0, .max = 0 },
291 .m2 = { .min = 0, .max = 254 },
292 .p = { .min = 7, .max = 112 },
293 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
294 .p2 = { .dot_limit = 112000,
295 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
296};
297
273e27ca
EA
298/* Ironlake / Sandybridge
299 *
300 * We calculate clock using (register_value + 2) for N/M1/M2, so here
301 * the range value for them is (actual_value - 2).
302 */
b91ad0ec 303static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
304 .dot = { .min = 25000, .max = 350000 },
305 .vco = { .min = 1760000, .max = 3510000 },
306 .n = { .min = 1, .max = 5 },
307 .m = { .min = 79, .max = 127 },
308 .m1 = { .min = 12, .max = 22 },
309 .m2 = { .min = 5, .max = 9 },
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
312 .p2 = { .dot_limit = 225000,
313 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
314};
315
b91ad0ec 316static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
317 .dot = { .min = 25000, .max = 350000 },
318 .vco = { .min = 1760000, .max = 3510000 },
319 .n = { .min = 1, .max = 3 },
320 .m = { .min = 79, .max = 118 },
321 .m1 = { .min = 12, .max = 22 },
322 .m2 = { .min = 5, .max = 9 },
323 .p = { .min = 28, .max = 112 },
324 .p1 = { .min = 2, .max = 8 },
325 .p2 = { .dot_limit = 225000,
326 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
327};
328
329static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 350000 },
331 .vco = { .min = 1760000, .max = 3510000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 79, .max = 127 },
334 .m1 = { .min = 12, .max = 22 },
335 .m2 = { .min = 5, .max = 9 },
336 .p = { .min = 14, .max = 56 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 225000,
339 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
340};
341
273e27ca 342/* LVDS 100mhz refclk limits. */
b91ad0ec 343static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
344 .dot = { .min = 25000, .max = 350000 },
345 .vco = { .min = 1760000, .max = 3510000 },
346 .n = { .min = 1, .max = 2 },
347 .m = { .min = 79, .max = 126 },
348 .m1 = { .min = 12, .max = 22 },
349 .m2 = { .min = 5, .max = 9 },
350 .p = { .min = 28, .max = 112 },
0206e353 351 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
352 .p2 = { .dot_limit = 225000,
353 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
354};
355
356static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
357 .dot = { .min = 25000, .max = 350000 },
358 .vco = { .min = 1760000, .max = 3510000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 79, .max = 126 },
361 .m1 = { .min = 12, .max = 22 },
362 .m2 = { .min = 5, .max = 9 },
363 .p = { .min = 14, .max = 42 },
0206e353 364 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
365 .p2 = { .dot_limit = 225000,
366 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
367};
368
dc730512 369static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
370 /*
371 * These are the data rate limits (measured in fast clocks)
372 * since those are the strictest limits we have. The fast
373 * clock and actual rate limits are more relaxed, so checking
374 * them would make no difference.
375 */
376 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 377 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 378 .n = { .min = 1, .max = 7 },
a0c4da24
JB
379 .m1 = { .min = 2, .max = 3 },
380 .m2 = { .min = 11, .max = 156 },
b99ab663 381 .p1 = { .min = 2, .max = 3 },
5fdc9c49 382 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
383};
384
ef9348c8
CML
385static const intel_limit_t intel_limits_chv = {
386 /*
387 * These are the data rate limits (measured in fast clocks)
388 * since those are the strictest limits we have. The fast
389 * clock and actual rate limits are more relaxed, so checking
390 * them would make no difference.
391 */
392 .dot = { .min = 25000 * 5, .max = 540000 * 5},
393 .vco = { .min = 4860000, .max = 6700000 },
394 .n = { .min = 1, .max = 1 },
395 .m1 = { .min = 2, .max = 2 },
396 .m2 = { .min = 24 << 22, .max = 175 << 22 },
397 .p1 = { .min = 2, .max = 4 },
398 .p2 = { .p2_slow = 1, .p2_fast = 14 },
399};
400
6b4bf1c4
VS
401static void vlv_clock(int refclk, intel_clock_t *clock)
402{
403 clock->m = clock->m1 * clock->m2;
404 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
405 if (WARN_ON(clock->n == 0 || clock->p == 0))
406 return;
fb03ac01
VS
407 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
408 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
409}
410
e0638cdf
PZ
411/**
412 * Returns whether any output on the specified pipe is of the specified type
413 */
4093561b 414bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 415{
409ee761 416 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
417 struct intel_encoder *encoder;
418
409ee761 419 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
d0737e1d
ACO
426/**
427 * Returns whether any output on the specified pipe will have the specified
428 * type after a staged modeset is complete, i.e., the same as
429 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
430 * encoder->crtc.
431 */
432static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
433{
434 struct drm_device *dev = crtc->base.dev;
435 struct intel_encoder *encoder;
436
437 for_each_intel_encoder(dev, encoder)
438 if (encoder->new_crtc == crtc && encoder->type == type)
439 return true;
440
441 return false;
442}
443
409ee761 444static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
1b894b59 445 int refclk)
2c07245f 446{
409ee761 447 struct drm_device *dev = crtc->base.dev;
2c07245f 448 const intel_limit_t *limit;
b91ad0ec 449
d0737e1d 450 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 451 if (intel_is_dual_link_lvds(dev)) {
1b894b59 452 if (refclk == 100000)
b91ad0ec
ZW
453 limit = &intel_limits_ironlake_dual_lvds_100m;
454 else
455 limit = &intel_limits_ironlake_dual_lvds;
456 } else {
1b894b59 457 if (refclk == 100000)
b91ad0ec
ZW
458 limit = &intel_limits_ironlake_single_lvds_100m;
459 else
460 limit = &intel_limits_ironlake_single_lvds;
461 }
c6bb3538 462 } else
b91ad0ec 463 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
464
465 return limit;
466}
467
409ee761 468static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
044c7c41 469{
409ee761 470 struct drm_device *dev = crtc->base.dev;
044c7c41
ML
471 const intel_limit_t *limit;
472
d0737e1d 473 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 474 if (intel_is_dual_link_lvds(dev))
e4b36699 475 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 476 else
e4b36699 477 limit = &intel_limits_g4x_single_channel_lvds;
d0737e1d
ACO
478 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
479 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 480 limit = &intel_limits_g4x_hdmi;
d0737e1d 481 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 482 limit = &intel_limits_g4x_sdvo;
044c7c41 483 } else /* The option is for other outputs */
e4b36699 484 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
485
486 return limit;
487}
488
409ee761 489static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
79e53945 490{
409ee761 491 struct drm_device *dev = crtc->base.dev;
79e53945
JB
492 const intel_limit_t *limit;
493
bad720ff 494 if (HAS_PCH_SPLIT(dev))
1b894b59 495 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 496 else if (IS_G4X(dev)) {
044c7c41 497 limit = intel_g4x_limit(crtc);
f2b115e6 498 } else if (IS_PINEVIEW(dev)) {
d0737e1d 499 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 500 limit = &intel_limits_pineview_lvds;
2177832f 501 else
f2b115e6 502 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
503 } else if (IS_CHERRYVIEW(dev)) {
504 limit = &intel_limits_chv;
a0c4da24 505 } else if (IS_VALLEYVIEW(dev)) {
dc730512 506 limit = &intel_limits_vlv;
a6c45cf0 507 } else if (!IS_GEN2(dev)) {
d0737e1d 508 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
509 limit = &intel_limits_i9xx_lvds;
510 else
511 limit = &intel_limits_i9xx_sdvo;
79e53945 512 } else {
d0737e1d 513 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 514 limit = &intel_limits_i8xx_lvds;
d0737e1d 515 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 516 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
517 else
518 limit = &intel_limits_i8xx_dac;
79e53945
JB
519 }
520 return limit;
521}
522
f2b115e6
AJ
523/* m1 is reserved as 0 in Pineview, n is a ring counter */
524static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 525{
2177832f
SL
526 clock->m = clock->m2 + 2;
527 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
528 if (WARN_ON(clock->n == 0 || clock->p == 0))
529 return;
fb03ac01
VS
530 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
531 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
532}
533
7429e9d4
DV
534static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
535{
536 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
537}
538
ac58c3f0 539static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 540{
7429e9d4 541 clock->m = i9xx_dpll_compute_m(clock);
79e53945 542 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
543 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
544 return;
fb03ac01
VS
545 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
546 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
547}
548
ef9348c8
CML
549static void chv_clock(int refclk, intel_clock_t *clock)
550{
551 clock->m = clock->m1 * clock->m2;
552 clock->p = clock->p1 * clock->p2;
553 if (WARN_ON(clock->n == 0 || clock->p == 0))
554 return;
555 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
556 clock->n << 22);
557 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
558}
559
7c04d1d9 560#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
561/**
562 * Returns whether the given set of divisors are valid for a given refclk with
563 * the given connectors.
564 */
565
1b894b59
CW
566static bool intel_PLL_is_valid(struct drm_device *dev,
567 const intel_limit_t *limit,
568 const intel_clock_t *clock)
79e53945 569{
f01b7962
VS
570 if (clock->n < limit->n.min || limit->n.max < clock->n)
571 INTELPllInvalid("n out of range\n");
79e53945 572 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 573 INTELPllInvalid("p1 out of range\n");
79e53945 574 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 575 INTELPllInvalid("m2 out of range\n");
79e53945 576 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 577 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
578
579 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
580 if (clock->m1 <= clock->m2)
581 INTELPllInvalid("m1 <= m2\n");
582
583 if (!IS_VALLEYVIEW(dev)) {
584 if (clock->p < limit->p.min || limit->p.max < clock->p)
585 INTELPllInvalid("p out of range\n");
586 if (clock->m < limit->m.min || limit->m.max < clock->m)
587 INTELPllInvalid("m out of range\n");
588 }
589
79e53945 590 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 591 INTELPllInvalid("vco out of range\n");
79e53945
JB
592 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
593 * connector, etc., rather than just a single range.
594 */
595 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 596 INTELPllInvalid("dot out of range\n");
79e53945
JB
597
598 return true;
599}
600
d4906093 601static bool
a919ff14 602i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
cec2f356
SP
603 int target, int refclk, intel_clock_t *match_clock,
604 intel_clock_t *best_clock)
79e53945 605{
a919ff14 606 struct drm_device *dev = crtc->base.dev;
79e53945 607 intel_clock_t clock;
79e53945
JB
608 int err = target;
609
d0737e1d 610 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 611 /*
a210b028
DV
612 * For LVDS just rely on its current settings for dual-channel.
613 * We haven't figured out how to reliably set up different
614 * single/dual channel state, if we even can.
79e53945 615 */
1974cad0 616 if (intel_is_dual_link_lvds(dev))
79e53945
JB
617 clock.p2 = limit->p2.p2_fast;
618 else
619 clock.p2 = limit->p2.p2_slow;
620 } else {
621 if (target < limit->p2.dot_limit)
622 clock.p2 = limit->p2.p2_slow;
623 else
624 clock.p2 = limit->p2.p2_fast;
625 }
626
0206e353 627 memset(best_clock, 0, sizeof(*best_clock));
79e53945 628
42158660
ZY
629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 633 if (clock.m2 >= clock.m1)
42158660
ZY
634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
639 int this_err;
640
ac58c3f0
DV
641 i9xx_clock(refclk, &clock);
642 if (!intel_PLL_is_valid(dev, limit,
643 &clock))
644 continue;
645 if (match_clock &&
646 clock.p != match_clock->p)
647 continue;
648
649 this_err = abs(clock.dot - target);
650 if (this_err < err) {
651 *best_clock = clock;
652 err = this_err;
653 }
654 }
655 }
656 }
657 }
658
659 return (err != target);
660}
661
662static bool
a919ff14 663pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
664 int target, int refclk, intel_clock_t *match_clock,
665 intel_clock_t *best_clock)
79e53945 666{
a919ff14 667 struct drm_device *dev = crtc->base.dev;
79e53945 668 intel_clock_t clock;
79e53945
JB
669 int err = target;
670
d0737e1d 671 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 672 /*
a210b028
DV
673 * For LVDS just rely on its current settings for dual-channel.
674 * We haven't figured out how to reliably set up different
675 * single/dual channel state, if we even can.
79e53945 676 */
1974cad0 677 if (intel_is_dual_link_lvds(dev))
79e53945
JB
678 clock.p2 = limit->p2.p2_fast;
679 else
680 clock.p2 = limit->p2.p2_slow;
681 } else {
682 if (target < limit->p2.dot_limit)
683 clock.p2 = limit->p2.p2_slow;
684 else
685 clock.p2 = limit->p2.p2_fast;
686 }
687
0206e353 688 memset(best_clock, 0, sizeof(*best_clock));
79e53945 689
42158660
ZY
690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
691 clock.m1++) {
692 for (clock.m2 = limit->m2.min;
693 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
698 int this_err;
699
ac58c3f0 700 pineview_clock(refclk, &clock);
1b894b59
CW
701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
79e53945 703 continue;
cec2f356
SP
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
79e53945
JB
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
d4906093 721static bool
a919ff14 722g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
723 int target, int refclk, intel_clock_t *match_clock,
724 intel_clock_t *best_clock)
d4906093 725{
a919ff14 726 struct drm_device *dev = crtc->base.dev;
d4906093
ML
727 intel_clock_t clock;
728 int max_n;
729 bool found;
6ba770dc
AJ
730 /* approximately equals target * 0.00585 */
731 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
732 found = false;
733
d0737e1d 734 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 735 if (intel_is_dual_link_lvds(dev))
d4906093
ML
736 clock.p2 = limit->p2.p2_fast;
737 else
738 clock.p2 = limit->p2.p2_slow;
739 } else {
740 if (target < limit->p2.dot_limit)
741 clock.p2 = limit->p2.p2_slow;
742 else
743 clock.p2 = limit->p2.p2_fast;
744 }
745
746 memset(best_clock, 0, sizeof(*best_clock));
747 max_n = limit->n.max;
f77f13e2 748 /* based on hardware requirement, prefer smaller n to precision */
d4906093 749 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 750 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
751 for (clock.m1 = limit->m1.max;
752 clock.m1 >= limit->m1.min; clock.m1--) {
753 for (clock.m2 = limit->m2.max;
754 clock.m2 >= limit->m2.min; clock.m2--) {
755 for (clock.p1 = limit->p1.max;
756 clock.p1 >= limit->p1.min; clock.p1--) {
757 int this_err;
758
ac58c3f0 759 i9xx_clock(refclk, &clock);
1b894b59
CW
760 if (!intel_PLL_is_valid(dev, limit,
761 &clock))
d4906093 762 continue;
1b894b59
CW
763
764 this_err = abs(clock.dot - target);
d4906093
ML
765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
2c07245f
ZW
775 return found;
776}
777
a0c4da24 778static bool
a919ff14 779vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
780 int target, int refclk, intel_clock_t *match_clock,
781 intel_clock_t *best_clock)
a0c4da24 782{
a919ff14 783 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 784 intel_clock_t clock;
69e4f900 785 unsigned int bestppm = 1000000;
27e639bf
VS
786 /* min update 19.2 MHz */
787 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 788 bool found = false;
a0c4da24 789
6b4bf1c4
VS
790 target *= 5; /* fast clock */
791
792 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
793
794 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 796 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 797 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 798 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 799 clock.p = clock.p1 * clock.p2;
a0c4da24 800 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 801 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
802 unsigned int ppm, diff;
803
6b4bf1c4
VS
804 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
805 refclk * clock.m1);
806
807 vlv_clock(refclk, &clock);
43b0ac53 808
f01b7962
VS
809 if (!intel_PLL_is_valid(dev, limit,
810 &clock))
43b0ac53
VS
811 continue;
812
6b4bf1c4
VS
813 diff = abs(clock.dot - target);
814 ppm = div_u64(1000000ULL * diff, target);
815
816 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 817 bestppm = 0;
6b4bf1c4 818 *best_clock = clock;
49e497ef 819 found = true;
43b0ac53 820 }
6b4bf1c4 821
c686122c 822 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 823 bestppm = ppm;
6b4bf1c4 824 *best_clock = clock;
49e497ef 825 found = true;
a0c4da24
JB
826 }
827 }
828 }
829 }
830 }
a0c4da24 831
49e497ef 832 return found;
a0c4da24 833}
a4fc5ed6 834
ef9348c8 835static bool
a919ff14 836chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ef9348c8
CML
837 int target, int refclk, intel_clock_t *match_clock,
838 intel_clock_t *best_clock)
839{
a919ff14 840 struct drm_device *dev = crtc->base.dev;
ef9348c8
CML
841 intel_clock_t clock;
842 uint64_t m2;
843 int found = false;
844
845 memset(best_clock, 0, sizeof(*best_clock));
846
847 /*
848 * Based on hardware doc, the n always set to 1, and m1 always
849 * set to 2. If requires to support 200Mhz refclk, we need to
850 * revisit this because n may not 1 anymore.
851 */
852 clock.n = 1, clock.m1 = 2;
853 target *= 5; /* fast clock */
854
855 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
856 for (clock.p2 = limit->p2.p2_fast;
857 clock.p2 >= limit->p2.p2_slow;
858 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
859
860 clock.p = clock.p1 * clock.p2;
861
862 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
863 clock.n) << 22, refclk * clock.m1);
864
865 if (m2 > INT_MAX/clock.m1)
866 continue;
867
868 clock.m2 = m2;
869
870 chv_clock(refclk, &clock);
871
872 if (!intel_PLL_is_valid(dev, limit, &clock))
873 continue;
874
875 /* based on hardware requirement, prefer bigger p
876 */
877 if (clock.p > best_clock->p) {
878 *best_clock = clock;
879 found = true;
880 }
881 }
882 }
883
884 return found;
885}
886
20ddf665
VS
887bool intel_crtc_active(struct drm_crtc *crtc)
888{
889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
890
891 /* Be paranoid as we can arrive here with only partial
892 * state retrieved from the hardware during setup.
893 *
241bfc38 894 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
895 * as Haswell has gained clock readout/fastboot support.
896 *
66e514c1 897 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
898 * properly reconstruct framebuffers.
899 */
f4510a27 900 return intel_crtc->active && crtc->primary->fb &&
6e3c9717 901 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
902}
903
a5c961d1
PZ
904enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
905 enum pipe pipe)
906{
907 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
909
6e3c9717 910 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
911}
912
fbf49ea2
VS
913static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
914{
915 struct drm_i915_private *dev_priv = dev->dev_private;
916 u32 reg = PIPEDSL(pipe);
917 u32 line1, line2;
918 u32 line_mask;
919
920 if (IS_GEN2(dev))
921 line_mask = DSL_LINEMASK_GEN2;
922 else
923 line_mask = DSL_LINEMASK_GEN3;
924
925 line1 = I915_READ(reg) & line_mask;
926 mdelay(5);
927 line2 = I915_READ(reg) & line_mask;
928
929 return line1 == line2;
930}
931
ab7ad7f6
KP
932/*
933 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 934 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
935 *
936 * After disabling a pipe, we can't wait for vblank in the usual way,
937 * spinning on the vblank interrupt status bit, since we won't actually
938 * see an interrupt when the pipe is disabled.
939 *
ab7ad7f6
KP
940 * On Gen4 and above:
941 * wait for the pipe register state bit to turn off
942 *
943 * Otherwise:
944 * wait for the display line value to settle (it usually
945 * ends up stopping at the start of the next frame).
58e10eb9 946 *
9d0498a2 947 */
575f7ab7 948static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 949{
575f7ab7 950 struct drm_device *dev = crtc->base.dev;
9d0498a2 951 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 952 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 953 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
954
955 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 956 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
957
958 /* Wait for the Pipe State to go off */
58e10eb9
CW
959 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
960 100))
284637d9 961 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 962 } else {
ab7ad7f6 963 /* Wait for the display line to settle */
fbf49ea2 964 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 965 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 966 }
79e53945
JB
967}
968
b0ea7d37
DL
969/*
970 * ibx_digital_port_connected - is the specified port connected?
971 * @dev_priv: i915 private structure
972 * @port: the port to test
973 *
974 * Returns true if @port is connected, false otherwise.
975 */
976bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
977 struct intel_digital_port *port)
978{
979 u32 bit;
980
c36346e3 981 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 982 switch (port->port) {
c36346e3
DL
983 case PORT_B:
984 bit = SDE_PORTB_HOTPLUG;
985 break;
986 case PORT_C:
987 bit = SDE_PORTC_HOTPLUG;
988 break;
989 case PORT_D:
990 bit = SDE_PORTD_HOTPLUG;
991 break;
992 default:
993 return true;
994 }
995 } else {
eba905b2 996 switch (port->port) {
c36346e3
DL
997 case PORT_B:
998 bit = SDE_PORTB_HOTPLUG_CPT;
999 break;
1000 case PORT_C:
1001 bit = SDE_PORTC_HOTPLUG_CPT;
1002 break;
1003 case PORT_D:
1004 bit = SDE_PORTD_HOTPLUG_CPT;
1005 break;
1006 default:
1007 return true;
1008 }
b0ea7d37
DL
1009 }
1010
1011 return I915_READ(SDEISR) & bit;
1012}
1013
b24e7179
JB
1014static const char *state_string(bool enabled)
1015{
1016 return enabled ? "on" : "off";
1017}
1018
1019/* Only for pre-ILK configs */
55607e8a
DV
1020void assert_pll(struct drm_i915_private *dev_priv,
1021 enum pipe pipe, bool state)
b24e7179
JB
1022{
1023 int reg;
1024 u32 val;
1025 bool cur_state;
1026
1027 reg = DPLL(pipe);
1028 val = I915_READ(reg);
1029 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1030 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1031 "PLL state assertion failure (expected %s, current %s)\n",
1032 state_string(state), state_string(cur_state));
1033}
b24e7179 1034
23538ef1
JN
1035/* XXX: the dsi pll is shared between MIPI DSI ports */
1036static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1037{
1038 u32 val;
1039 bool cur_state;
1040
1041 mutex_lock(&dev_priv->dpio_lock);
1042 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1043 mutex_unlock(&dev_priv->dpio_lock);
1044
1045 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1046 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1047 "DSI PLL state assertion failure (expected %s, current %s)\n",
1048 state_string(state), state_string(cur_state));
1049}
1050#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1051#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1052
55607e8a 1053struct intel_shared_dpll *
e2b78267
DV
1054intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1055{
1056 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1057
6e3c9717 1058 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1059 return NULL;
1060
6e3c9717 1061 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1062}
1063
040484af 1064/* For ILK+ */
55607e8a
DV
1065void assert_shared_dpll(struct drm_i915_private *dev_priv,
1066 struct intel_shared_dpll *pll,
1067 bool state)
040484af 1068{
040484af 1069 bool cur_state;
5358901f 1070 struct intel_dpll_hw_state hw_state;
040484af 1071
92b27b08 1072 if (WARN (!pll,
46edb027 1073 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1074 return;
ee7b9f93 1075
5358901f 1076 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1077 I915_STATE_WARN(cur_state != state,
5358901f
DV
1078 "%s assertion failure (expected %s, current %s)\n",
1079 pll->name, state_string(state), state_string(cur_state));
040484af 1080}
040484af
JB
1081
1082static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1083 enum pipe pipe, bool state)
1084{
1085 int reg;
1086 u32 val;
1087 bool cur_state;
ad80a810
PZ
1088 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1089 pipe);
040484af 1090
affa9354
PZ
1091 if (HAS_DDI(dev_priv->dev)) {
1092 /* DDI does not have a specific FDI_TX register */
ad80a810 1093 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1094 val = I915_READ(reg);
ad80a810 1095 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1096 } else {
1097 reg = FDI_TX_CTL(pipe);
1098 val = I915_READ(reg);
1099 cur_state = !!(val & FDI_TX_ENABLE);
1100 }
e2c719b7 1101 I915_STATE_WARN(cur_state != state,
040484af
JB
1102 "FDI TX state assertion failure (expected %s, current %s)\n",
1103 state_string(state), state_string(cur_state));
1104}
1105#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1106#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1107
1108static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1109 enum pipe pipe, bool state)
1110{
1111 int reg;
1112 u32 val;
1113 bool cur_state;
1114
d63fa0dc
PZ
1115 reg = FDI_RX_CTL(pipe);
1116 val = I915_READ(reg);
1117 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1118 I915_STATE_WARN(cur_state != state,
040484af
JB
1119 "FDI RX state assertion failure (expected %s, current %s)\n",
1120 state_string(state), state_string(cur_state));
1121}
1122#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1123#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1124
1125static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1126 enum pipe pipe)
1127{
1128 int reg;
1129 u32 val;
1130
1131 /* ILK FDI PLL is always enabled */
3d13ef2e 1132 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1133 return;
1134
bf507ef7 1135 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1136 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1137 return;
1138
040484af
JB
1139 reg = FDI_TX_CTL(pipe);
1140 val = I915_READ(reg);
e2c719b7 1141 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1142}
1143
55607e8a
DV
1144void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
040484af
JB
1146{
1147 int reg;
1148 u32 val;
55607e8a 1149 bool cur_state;
040484af
JB
1150
1151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
55607e8a 1153 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1154 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1155 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
040484af
JB
1157}
1158
b680c37a
DV
1159void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
ea0760cf 1161{
bedd4dba
JN
1162 struct drm_device *dev = dev_priv->dev;
1163 int pp_reg;
ea0760cf
JB
1164 u32 val;
1165 enum pipe panel_pipe = PIPE_A;
0de3b485 1166 bool locked = true;
ea0760cf 1167
bedd4dba
JN
1168 if (WARN_ON(HAS_DDI(dev)))
1169 return;
1170
1171 if (HAS_PCH_SPLIT(dev)) {
1172 u32 port_sel;
1173
ea0760cf 1174 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1175 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1176
1177 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1178 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1179 panel_pipe = PIPE_B;
1180 /* XXX: else fix for eDP */
1181 } else if (IS_VALLEYVIEW(dev)) {
1182 /* presumably write lock depends on pipe, not port select */
1183 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1184 panel_pipe = pipe;
ea0760cf
JB
1185 } else {
1186 pp_reg = PP_CONTROL;
bedd4dba
JN
1187 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1188 panel_pipe = PIPE_B;
ea0760cf
JB
1189 }
1190
1191 val = I915_READ(pp_reg);
1192 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1193 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1194 locked = false;
1195
e2c719b7 1196 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1197 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1198 pipe_name(pipe));
ea0760cf
JB
1199}
1200
93ce0ba6
JN
1201static void assert_cursor(struct drm_i915_private *dev_priv,
1202 enum pipe pipe, bool state)
1203{
1204 struct drm_device *dev = dev_priv->dev;
1205 bool cur_state;
1206
d9d82081 1207 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1208 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1209 else
5efb3e28 1210 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1211
e2c719b7 1212 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1213 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1214 pipe_name(pipe), state_string(state), state_string(cur_state));
1215}
1216#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1217#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1218
b840d907
JB
1219void assert_pipe(struct drm_i915_private *dev_priv,
1220 enum pipe pipe, bool state)
b24e7179
JB
1221{
1222 int reg;
1223 u32 val;
63d7bbe9 1224 bool cur_state;
702e7a56
PZ
1225 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1226 pipe);
b24e7179 1227
b6b5d049
VS
1228 /* if we need the pipe quirk it must be always on */
1229 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1230 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1231 state = true;
1232
f458ebbc 1233 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1234 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1235 cur_state = false;
1236 } else {
1237 reg = PIPECONF(cpu_transcoder);
1238 val = I915_READ(reg);
1239 cur_state = !!(val & PIPECONF_ENABLE);
1240 }
1241
e2c719b7 1242 I915_STATE_WARN(cur_state != state,
63d7bbe9 1243 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1244 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1245}
1246
931872fc
CW
1247static void assert_plane(struct drm_i915_private *dev_priv,
1248 enum plane plane, bool state)
b24e7179
JB
1249{
1250 int reg;
1251 u32 val;
931872fc 1252 bool cur_state;
b24e7179
JB
1253
1254 reg = DSPCNTR(plane);
1255 val = I915_READ(reg);
931872fc 1256 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1257 I915_STATE_WARN(cur_state != state,
931872fc
CW
1258 "plane %c assertion failure (expected %s, current %s)\n",
1259 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1260}
1261
931872fc
CW
1262#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1263#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1264
b24e7179
JB
1265static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1266 enum pipe pipe)
1267{
653e1026 1268 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1269 int reg, i;
1270 u32 val;
1271 int cur_pipe;
1272
653e1026
VS
1273 /* Primary planes are fixed to pipes on gen4+ */
1274 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1275 reg = DSPCNTR(pipe);
1276 val = I915_READ(reg);
e2c719b7 1277 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1278 "plane %c assertion failure, should be disabled but not\n",
1279 plane_name(pipe));
19ec1358 1280 return;
28c05794 1281 }
19ec1358 1282
b24e7179 1283 /* Need to check both planes against the pipe */
055e393f 1284 for_each_pipe(dev_priv, i) {
b24e7179
JB
1285 reg = DSPCNTR(i);
1286 val = I915_READ(reg);
1287 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1288 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1289 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1290 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1291 plane_name(i), pipe_name(pipe));
b24e7179
JB
1292 }
1293}
1294
19332d7a
JB
1295static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe)
1297{
20674eef 1298 struct drm_device *dev = dev_priv->dev;
1fe47785 1299 int reg, sprite;
19332d7a
JB
1300 u32 val;
1301
7feb8b88
DL
1302 if (INTEL_INFO(dev)->gen >= 9) {
1303 for_each_sprite(pipe, sprite) {
1304 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1305 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1306 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1307 sprite, pipe_name(pipe));
1308 }
1309 } else if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1310 for_each_sprite(pipe, sprite) {
1311 reg = SPCNTR(pipe, sprite);
20674eef 1312 val = I915_READ(reg);
e2c719b7 1313 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1314 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1315 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1316 }
1317 } else if (INTEL_INFO(dev)->gen >= 7) {
1318 reg = SPRCTL(pipe);
19332d7a 1319 val = I915_READ(reg);
e2c719b7 1320 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1321 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1322 plane_name(pipe), pipe_name(pipe));
1323 } else if (INTEL_INFO(dev)->gen >= 5) {
1324 reg = DVSCNTR(pipe);
19332d7a 1325 val = I915_READ(reg);
e2c719b7 1326 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1327 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1328 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1329 }
1330}
1331
08c71e5e
VS
1332static void assert_vblank_disabled(struct drm_crtc *crtc)
1333{
e2c719b7 1334 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1335 drm_crtc_vblank_put(crtc);
1336}
1337
89eff4be 1338static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1339{
1340 u32 val;
1341 bool enabled;
1342
e2c719b7 1343 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1344
92f2584a
JB
1345 val = I915_READ(PCH_DREF_CONTROL);
1346 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1347 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1348 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1349}
1350
ab9412ba
DV
1351static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1352 enum pipe pipe)
92f2584a
JB
1353{
1354 int reg;
1355 u32 val;
1356 bool enabled;
1357
ab9412ba 1358 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1359 val = I915_READ(reg);
1360 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1361 I915_STATE_WARN(enabled,
9db4a9c7
JB
1362 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1363 pipe_name(pipe));
92f2584a
JB
1364}
1365
4e634389
KP
1366static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1367 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1368{
1369 if ((val & DP_PORT_EN) == 0)
1370 return false;
1371
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
1373 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1374 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1375 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1376 return false;
44f37d1f
CML
1377 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1378 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1379 return false;
f0575e92
KP
1380 } else {
1381 if ((val & DP_PIPE_MASK) != (pipe << 30))
1382 return false;
1383 }
1384 return true;
1385}
1386
1519b995
KP
1387static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe, u32 val)
1389{
dc0fa718 1390 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1391 return false;
1392
1393 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1394 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1395 return false;
44f37d1f
CML
1396 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1397 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1398 return false;
1519b995 1399 } else {
dc0fa718 1400 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1401 return false;
1402 }
1403 return true;
1404}
1405
1406static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1407 enum pipe pipe, u32 val)
1408{
1409 if ((val & LVDS_PORT_EN) == 0)
1410 return false;
1411
1412 if (HAS_PCH_CPT(dev_priv->dev)) {
1413 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1414 return false;
1415 } else {
1416 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1417 return false;
1418 }
1419 return true;
1420}
1421
1422static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1423 enum pipe pipe, u32 val)
1424{
1425 if ((val & ADPA_DAC_ENABLE) == 0)
1426 return false;
1427 if (HAS_PCH_CPT(dev_priv->dev)) {
1428 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1429 return false;
1430 } else {
1431 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1432 return false;
1433 }
1434 return true;
1435}
1436
291906f1 1437static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1438 enum pipe pipe, int reg, u32 port_sel)
291906f1 1439{
47a05eca 1440 u32 val = I915_READ(reg);
e2c719b7 1441 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1442 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1443 reg, pipe_name(pipe));
de9a35ab 1444
e2c719b7 1445 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1446 && (val & DP_PIPEB_SELECT),
de9a35ab 1447 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1448}
1449
1450static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, int reg)
1452{
47a05eca 1453 u32 val = I915_READ(reg);
e2c719b7 1454 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1455 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1456 reg, pipe_name(pipe));
de9a35ab 1457
e2c719b7 1458 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1459 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1460 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1461}
1462
1463static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1464 enum pipe pipe)
1465{
1466 int reg;
1467 u32 val;
291906f1 1468
f0575e92
KP
1469 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1472
1473 reg = PCH_ADPA;
1474 val = I915_READ(reg);
e2c719b7 1475 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1476 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1477 pipe_name(pipe));
291906f1
JB
1478
1479 reg = PCH_LVDS;
1480 val = I915_READ(reg);
e2c719b7 1481 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1482 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1483 pipe_name(pipe));
291906f1 1484
e2debe91
PZ
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1488}
1489
40e9cf64
JB
1490static void intel_init_dpio(struct drm_device *dev)
1491{
1492 struct drm_i915_private *dev_priv = dev->dev_private;
1493
1494 if (!IS_VALLEYVIEW(dev))
1495 return;
1496
a09caddd
CML
1497 /*
1498 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1499 * CHV x1 PHY (DP/HDMI D)
1500 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1501 */
1502 if (IS_CHERRYVIEW(dev)) {
1503 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1504 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1505 } else {
1506 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1507 }
5382f5f3
JB
1508}
1509
d288f65f 1510static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1511 const struct intel_crtc_state *pipe_config)
87442f73 1512{
426115cf
DV
1513 struct drm_device *dev = crtc->base.dev;
1514 struct drm_i915_private *dev_priv = dev->dev_private;
1515 int reg = DPLL(crtc->pipe);
d288f65f 1516 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1517
426115cf 1518 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1519
1520 /* No really, not for ILK+ */
1521 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1522
1523 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1524 if (IS_MOBILE(dev_priv->dev))
426115cf 1525 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1526
426115cf
DV
1527 I915_WRITE(reg, dpll);
1528 POSTING_READ(reg);
1529 udelay(150);
1530
1531 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1532 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1533
d288f65f 1534 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1535 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1536
1537 /* We do this three times for luck */
426115cf 1538 I915_WRITE(reg, dpll);
87442f73
DV
1539 POSTING_READ(reg);
1540 udelay(150); /* wait for warmup */
426115cf 1541 I915_WRITE(reg, dpll);
87442f73
DV
1542 POSTING_READ(reg);
1543 udelay(150); /* wait for warmup */
426115cf 1544 I915_WRITE(reg, dpll);
87442f73
DV
1545 POSTING_READ(reg);
1546 udelay(150); /* wait for warmup */
1547}
1548
d288f65f 1549static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1550 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1551{
1552 struct drm_device *dev = crtc->base.dev;
1553 struct drm_i915_private *dev_priv = dev->dev_private;
1554 int pipe = crtc->pipe;
1555 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1556 u32 tmp;
1557
1558 assert_pipe_disabled(dev_priv, crtc->pipe);
1559
1560 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1561
1562 mutex_lock(&dev_priv->dpio_lock);
1563
1564 /* Enable back the 10bit clock to display controller */
1565 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1566 tmp |= DPIO_DCLKP_EN;
1567 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1568
1569 /*
1570 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1571 */
1572 udelay(1);
1573
1574 /* Enable PLL */
d288f65f 1575 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1576
1577 /* Check PLL is locked */
a11b0703 1578 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1579 DRM_ERROR("PLL %d failed to lock\n", pipe);
1580
a11b0703 1581 /* not sure when this should be written */
d288f65f 1582 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1583 POSTING_READ(DPLL_MD(pipe));
1584
9d556c99
CML
1585 mutex_unlock(&dev_priv->dpio_lock);
1586}
1587
1c4e0274
VS
1588static int intel_num_dvo_pipes(struct drm_device *dev)
1589{
1590 struct intel_crtc *crtc;
1591 int count = 0;
1592
1593 for_each_intel_crtc(dev, crtc)
1594 count += crtc->active &&
409ee761 1595 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1596
1597 return count;
1598}
1599
66e3d5c0 1600static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1601{
66e3d5c0
DV
1602 struct drm_device *dev = crtc->base.dev;
1603 struct drm_i915_private *dev_priv = dev->dev_private;
1604 int reg = DPLL(crtc->pipe);
6e3c9717 1605 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1606
66e3d5c0 1607 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1608
63d7bbe9 1609 /* No really, not for ILK+ */
3d13ef2e 1610 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1611
1612 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1613 if (IS_MOBILE(dev) && !IS_I830(dev))
1614 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1615
1c4e0274
VS
1616 /* Enable DVO 2x clock on both PLLs if necessary */
1617 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1618 /*
1619 * It appears to be important that we don't enable this
1620 * for the current pipe before otherwise configuring the
1621 * PLL. No idea how this should be handled if multiple
1622 * DVO outputs are enabled simultaneosly.
1623 */
1624 dpll |= DPLL_DVO_2X_MODE;
1625 I915_WRITE(DPLL(!crtc->pipe),
1626 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1627 }
66e3d5c0
DV
1628
1629 /* Wait for the clocks to stabilize. */
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (INTEL_INFO(dev)->gen >= 4) {
1634 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1635 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1636 } else {
1637 /* The pixel multiplier can only be updated once the
1638 * DPLL is enabled and the clocks are stable.
1639 *
1640 * So write it again.
1641 */
1642 I915_WRITE(reg, dpll);
1643 }
63d7bbe9
JB
1644
1645 /* We do this three times for luck */
66e3d5c0 1646 I915_WRITE(reg, dpll);
63d7bbe9
JB
1647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
66e3d5c0 1649 I915_WRITE(reg, dpll);
63d7bbe9
JB
1650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
66e3d5c0 1652 I915_WRITE(reg, dpll);
63d7bbe9
JB
1653 POSTING_READ(reg);
1654 udelay(150); /* wait for warmup */
1655}
1656
1657/**
50b44a44 1658 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1659 * @dev_priv: i915 private structure
1660 * @pipe: pipe PLL to disable
1661 *
1662 * Disable the PLL for @pipe, making sure the pipe is off first.
1663 *
1664 * Note! This is for pre-ILK only.
1665 */
1c4e0274 1666static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1667{
1c4e0274
VS
1668 struct drm_device *dev = crtc->base.dev;
1669 struct drm_i915_private *dev_priv = dev->dev_private;
1670 enum pipe pipe = crtc->pipe;
1671
1672 /* Disable DVO 2x clock on both PLLs if necessary */
1673 if (IS_I830(dev) &&
409ee761 1674 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1675 intel_num_dvo_pipes(dev) == 1) {
1676 I915_WRITE(DPLL(PIPE_B),
1677 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1678 I915_WRITE(DPLL(PIPE_A),
1679 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1680 }
1681
b6b5d049
VS
1682 /* Don't disable pipe or pipe PLLs if needed */
1683 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1684 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1685 return;
1686
1687 /* Make sure the pipe isn't still relying on us */
1688 assert_pipe_disabled(dev_priv, pipe);
1689
50b44a44
DV
1690 I915_WRITE(DPLL(pipe), 0);
1691 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1692}
1693
f6071166
JB
1694static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1695{
1696 u32 val = 0;
1697
1698 /* Make sure the pipe isn't still relying on us */
1699 assert_pipe_disabled(dev_priv, pipe);
1700
e5cbfbfb
ID
1701 /*
1702 * Leave integrated clock source and reference clock enabled for pipe B.
1703 * The latter is needed for VGA hotplug / manual detection.
1704 */
f6071166 1705 if (pipe == PIPE_B)
e5cbfbfb 1706 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1707 I915_WRITE(DPLL(pipe), val);
1708 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1709
1710}
1711
1712static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
d752048d 1714 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1715 u32 val;
1716
a11b0703
VS
1717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1719
a11b0703 1720 /* Set PLL en = 0 */
d17ec4ce 1721 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1722 if (pipe != PIPE_A)
1723 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
d752048d
VS
1726
1727 mutex_lock(&dev_priv->dpio_lock);
1728
1729 /* Disable 10bit clock to display controller */
1730 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1731 val &= ~DPIO_DCLKP_EN;
1732 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1733
61407f6d
VS
1734 /* disable left/right clock distribution */
1735 if (pipe != PIPE_B) {
1736 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1737 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1738 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1739 } else {
1740 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1741 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1742 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1743 }
1744
d752048d 1745 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1746}
1747
e4607fcf
CML
1748void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1749 struct intel_digital_port *dport)
89b667f8
JB
1750{
1751 u32 port_mask;
00fc31b7 1752 int dpll_reg;
89b667f8 1753
e4607fcf
CML
1754 switch (dport->port) {
1755 case PORT_B:
89b667f8 1756 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1757 dpll_reg = DPLL(0);
e4607fcf
CML
1758 break;
1759 case PORT_C:
89b667f8 1760 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1761 dpll_reg = DPLL(0);
1762 break;
1763 case PORT_D:
1764 port_mask = DPLL_PORTD_READY_MASK;
1765 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1766 break;
1767 default:
1768 BUG();
1769 }
89b667f8 1770
00fc31b7 1771 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1772 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1773 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1774}
1775
b14b1055
DV
1776static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1777{
1778 struct drm_device *dev = crtc->base.dev;
1779 struct drm_i915_private *dev_priv = dev->dev_private;
1780 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1781
be19f0ff
CW
1782 if (WARN_ON(pll == NULL))
1783 return;
1784
3e369b76 1785 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1786 if (pll->active == 0) {
1787 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1788 WARN_ON(pll->on);
1789 assert_shared_dpll_disabled(dev_priv, pll);
1790
1791 pll->mode_set(dev_priv, pll);
1792 }
1793}
1794
92f2584a 1795/**
85b3894f 1796 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1797 * @dev_priv: i915 private structure
1798 * @pipe: pipe PLL to enable
1799 *
1800 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1801 * drives the transcoder clock.
1802 */
85b3894f 1803static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1804{
3d13ef2e
DL
1805 struct drm_device *dev = crtc->base.dev;
1806 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1807 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1808
87a875bb 1809 if (WARN_ON(pll == NULL))
48da64a8
CW
1810 return;
1811
3e369b76 1812 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1813 return;
ee7b9f93 1814
74dd6928 1815 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1816 pll->name, pll->active, pll->on,
e2b78267 1817 crtc->base.base.id);
92f2584a 1818
cdbd2316
DV
1819 if (pll->active++) {
1820 WARN_ON(!pll->on);
e9d6944e 1821 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1822 return;
1823 }
f4a091c7 1824 WARN_ON(pll->on);
ee7b9f93 1825
bd2bb1b9
PZ
1826 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1827
46edb027 1828 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1829 pll->enable(dev_priv, pll);
ee7b9f93 1830 pll->on = true;
92f2584a
JB
1831}
1832
f6daaec2 1833static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1834{
3d13ef2e
DL
1835 struct drm_device *dev = crtc->base.dev;
1836 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1837 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1838
92f2584a 1839 /* PCH only available on ILK+ */
3d13ef2e 1840 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1841 if (WARN_ON(pll == NULL))
ee7b9f93 1842 return;
92f2584a 1843
3e369b76 1844 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1845 return;
7a419866 1846
46edb027
DV
1847 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1848 pll->name, pll->active, pll->on,
e2b78267 1849 crtc->base.base.id);
7a419866 1850
48da64a8 1851 if (WARN_ON(pll->active == 0)) {
e9d6944e 1852 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1853 return;
1854 }
1855
e9d6944e 1856 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1857 WARN_ON(!pll->on);
cdbd2316 1858 if (--pll->active)
7a419866 1859 return;
ee7b9f93 1860
46edb027 1861 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1862 pll->disable(dev_priv, pll);
ee7b9f93 1863 pll->on = false;
bd2bb1b9
PZ
1864
1865 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1866}
1867
b8a4f404
PZ
1868static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1869 enum pipe pipe)
040484af 1870{
23670b32 1871 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1872 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1874 uint32_t reg, val, pipeconf_val;
040484af
JB
1875
1876 /* PCH only available on ILK+ */
55522f37 1877 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1878
1879 /* Make sure PCH DPLL is enabled */
e72f9fbf 1880 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1881 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1882
1883 /* FDI must be feeding us bits for PCH ports */
1884 assert_fdi_tx_enabled(dev_priv, pipe);
1885 assert_fdi_rx_enabled(dev_priv, pipe);
1886
23670b32
DV
1887 if (HAS_PCH_CPT(dev)) {
1888 /* Workaround: Set the timing override bit before enabling the
1889 * pch transcoder. */
1890 reg = TRANS_CHICKEN2(pipe);
1891 val = I915_READ(reg);
1892 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1893 I915_WRITE(reg, val);
59c859d6 1894 }
23670b32 1895
ab9412ba 1896 reg = PCH_TRANSCONF(pipe);
040484af 1897 val = I915_READ(reg);
5f7f726d 1898 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1899
1900 if (HAS_PCH_IBX(dev_priv->dev)) {
1901 /*
1902 * make the BPC in transcoder be consistent with
1903 * that in pipeconf reg.
1904 */
dfd07d72
DV
1905 val &= ~PIPECONF_BPC_MASK;
1906 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1907 }
5f7f726d
PZ
1908
1909 val &= ~TRANS_INTERLACE_MASK;
1910 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1911 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1912 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1913 val |= TRANS_LEGACY_INTERLACED_ILK;
1914 else
1915 val |= TRANS_INTERLACED;
5f7f726d
PZ
1916 else
1917 val |= TRANS_PROGRESSIVE;
1918
040484af
JB
1919 I915_WRITE(reg, val | TRANS_ENABLE);
1920 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1921 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1922}
1923
8fb033d7 1924static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1925 enum transcoder cpu_transcoder)
040484af 1926{
8fb033d7 1927 u32 val, pipeconf_val;
8fb033d7
PZ
1928
1929 /* PCH only available on ILK+ */
55522f37 1930 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1931
8fb033d7 1932 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1933 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1934 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1935
223a6fdf
PZ
1936 /* Workaround: set timing override bit. */
1937 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1938 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1939 I915_WRITE(_TRANSA_CHICKEN2, val);
1940
25f3ef11 1941 val = TRANS_ENABLE;
937bb610 1942 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1943
9a76b1c6
PZ
1944 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1945 PIPECONF_INTERLACED_ILK)
a35f2679 1946 val |= TRANS_INTERLACED;
8fb033d7
PZ
1947 else
1948 val |= TRANS_PROGRESSIVE;
1949
ab9412ba
DV
1950 I915_WRITE(LPT_TRANSCONF, val);
1951 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1952 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1953}
1954
b8a4f404
PZ
1955static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1956 enum pipe pipe)
040484af 1957{
23670b32
DV
1958 struct drm_device *dev = dev_priv->dev;
1959 uint32_t reg, val;
040484af
JB
1960
1961 /* FDI relies on the transcoder */
1962 assert_fdi_tx_disabled(dev_priv, pipe);
1963 assert_fdi_rx_disabled(dev_priv, pipe);
1964
291906f1
JB
1965 /* Ports must be off as well */
1966 assert_pch_ports_disabled(dev_priv, pipe);
1967
ab9412ba 1968 reg = PCH_TRANSCONF(pipe);
040484af
JB
1969 val = I915_READ(reg);
1970 val &= ~TRANS_ENABLE;
1971 I915_WRITE(reg, val);
1972 /* wait for PCH transcoder off, transcoder state */
1973 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1974 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1975
1976 if (!HAS_PCH_IBX(dev)) {
1977 /* Workaround: Clear the timing override chicken bit again. */
1978 reg = TRANS_CHICKEN2(pipe);
1979 val = I915_READ(reg);
1980 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1981 I915_WRITE(reg, val);
1982 }
040484af
JB
1983}
1984
ab4d966c 1985static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1986{
8fb033d7
PZ
1987 u32 val;
1988
ab9412ba 1989 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1990 val &= ~TRANS_ENABLE;
ab9412ba 1991 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1992 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1993 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1994 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1995
1996 /* Workaround: clear timing override bit. */
1997 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1998 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1999 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2000}
2001
b24e7179 2002/**
309cfea8 2003 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2004 * @crtc: crtc responsible for the pipe
b24e7179 2005 *
0372264a 2006 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2007 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2008 */
e1fdc473 2009static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2010{
0372264a
PZ
2011 struct drm_device *dev = crtc->base.dev;
2012 struct drm_i915_private *dev_priv = dev->dev_private;
2013 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2014 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2015 pipe);
1a240d4d 2016 enum pipe pch_transcoder;
b24e7179
JB
2017 int reg;
2018 u32 val;
2019
58c6eaa2 2020 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2021 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2022 assert_sprites_disabled(dev_priv, pipe);
2023
681e5811 2024 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2025 pch_transcoder = TRANSCODER_A;
2026 else
2027 pch_transcoder = pipe;
2028
b24e7179
JB
2029 /*
2030 * A pipe without a PLL won't actually be able to drive bits from
2031 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2032 * need the check.
2033 */
2034 if (!HAS_PCH_SPLIT(dev_priv->dev))
409ee761 2035 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2036 assert_dsi_pll_enabled(dev_priv);
2037 else
2038 assert_pll_enabled(dev_priv, pipe);
040484af 2039 else {
6e3c9717 2040 if (crtc->config->has_pch_encoder) {
040484af 2041 /* if driving the PCH, we need FDI enabled */
cc391bbb 2042 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2043 assert_fdi_tx_pll_enabled(dev_priv,
2044 (enum pipe) cpu_transcoder);
040484af
JB
2045 }
2046 /* FIXME: assert CPU port conditions for SNB+ */
2047 }
b24e7179 2048
702e7a56 2049 reg = PIPECONF(cpu_transcoder);
b24e7179 2050 val = I915_READ(reg);
7ad25d48 2051 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2052 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2053 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2054 return;
7ad25d48 2055 }
00d70b15
CW
2056
2057 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2058 POSTING_READ(reg);
b24e7179
JB
2059}
2060
2061/**
309cfea8 2062 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2063 * @crtc: crtc whose pipes is to be disabled
b24e7179 2064 *
575f7ab7
VS
2065 * Disable the pipe of @crtc, making sure that various hardware
2066 * specific requirements are met, if applicable, e.g. plane
2067 * disabled, panel fitter off, etc.
b24e7179
JB
2068 *
2069 * Will wait until the pipe has shut down before returning.
2070 */
575f7ab7 2071static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2072{
575f7ab7 2073 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2075 enum pipe pipe = crtc->pipe;
b24e7179
JB
2076 int reg;
2077 u32 val;
2078
2079 /*
2080 * Make sure planes won't keep trying to pump pixels to us,
2081 * or we might hang the display.
2082 */
2083 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2084 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2085 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2086
702e7a56 2087 reg = PIPECONF(cpu_transcoder);
b24e7179 2088 val = I915_READ(reg);
00d70b15
CW
2089 if ((val & PIPECONF_ENABLE) == 0)
2090 return;
2091
67adc644
VS
2092 /*
2093 * Double wide has implications for planes
2094 * so best keep it disabled when not needed.
2095 */
6e3c9717 2096 if (crtc->config->double_wide)
67adc644
VS
2097 val &= ~PIPECONF_DOUBLE_WIDE;
2098
2099 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2100 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2101 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2102 val &= ~PIPECONF_ENABLE;
2103
2104 I915_WRITE(reg, val);
2105 if ((val & PIPECONF_ENABLE) == 0)
2106 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2107}
2108
d74362c9
KP
2109/*
2110 * Plane regs are double buffered, going from enabled->disabled needs a
2111 * trigger in order to latch. The display address reg provides this.
2112 */
1dba99f4
VS
2113void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2114 enum plane plane)
d74362c9 2115{
3d13ef2e
DL
2116 struct drm_device *dev = dev_priv->dev;
2117 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2118
2119 I915_WRITE(reg, I915_READ(reg));
2120 POSTING_READ(reg);
d74362c9
KP
2121}
2122
b24e7179 2123/**
262ca2b0 2124 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2125 * @plane: plane to be enabled
2126 * @crtc: crtc for the plane
b24e7179 2127 *
fdd508a6 2128 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2129 */
fdd508a6
VS
2130static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2131 struct drm_crtc *crtc)
b24e7179 2132{
fdd508a6
VS
2133 struct drm_device *dev = plane->dev;
2134 struct drm_i915_private *dev_priv = dev->dev_private;
2135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2136
2137 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2138 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2139
98ec7739
VS
2140 if (intel_crtc->primary_enabled)
2141 return;
0037f71c 2142
4c445e0e 2143 intel_crtc->primary_enabled = true;
939c2fe8 2144
fdd508a6
VS
2145 dev_priv->display.update_primary_plane(crtc, plane->fb,
2146 crtc->x, crtc->y);
33c3b0d1
VS
2147
2148 /*
2149 * BDW signals flip done immediately if the plane
2150 * is disabled, even if the plane enable is already
2151 * armed to occur at the next vblank :(
2152 */
2153 if (IS_BROADWELL(dev))
2154 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2155}
2156
b24e7179 2157/**
262ca2b0 2158 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2159 * @plane: plane to be disabled
2160 * @crtc: crtc for the plane
b24e7179 2161 *
fdd508a6 2162 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2163 */
fdd508a6
VS
2164static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2165 struct drm_crtc *crtc)
b24e7179 2166{
fdd508a6
VS
2167 struct drm_device *dev = plane->dev;
2168 struct drm_i915_private *dev_priv = dev->dev_private;
2169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2170
32b7eeec
MR
2171 if (WARN_ON(!intel_crtc->active))
2172 return;
b24e7179 2173
98ec7739
VS
2174 if (!intel_crtc->primary_enabled)
2175 return;
0037f71c 2176
4c445e0e 2177 intel_crtc->primary_enabled = false;
939c2fe8 2178
fdd508a6
VS
2179 dev_priv->display.update_primary_plane(crtc, plane->fb,
2180 crtc->x, crtc->y);
b24e7179
JB
2181}
2182
693db184
CW
2183static bool need_vtd_wa(struct drm_device *dev)
2184{
2185#ifdef CONFIG_INTEL_IOMMU
2186 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2187 return true;
2188#endif
2189 return false;
2190}
2191
ec2c981e
DL
2192int
2193intel_fb_align_height(struct drm_device *dev, int height, unsigned int tiling)
a57ce0b2
JB
2194{
2195 int tile_height;
2196
ec2c981e 2197 tile_height = tiling ? (IS_GEN2(dev) ? 16 : 8) : 1;
a57ce0b2
JB
2198 return ALIGN(height, tile_height);
2199}
2200
127bd2ac 2201int
850c4cdc
TU
2202intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2203 struct drm_framebuffer *fb,
a4872ba6 2204 struct intel_engine_cs *pipelined)
6b95a207 2205{
850c4cdc 2206 struct drm_device *dev = fb->dev;
ce453d81 2207 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2208 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207
KH
2209 u32 alignment;
2210 int ret;
2211
ebcdd39e
MR
2212 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2213
05394f39 2214 switch (obj->tiling_mode) {
6b95a207 2215 case I915_TILING_NONE:
1fada4cc
DL
2216 if (INTEL_INFO(dev)->gen >= 9)
2217 alignment = 256 * 1024;
2218 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2219 alignment = 128 * 1024;
a6c45cf0 2220 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2221 alignment = 4 * 1024;
2222 else
2223 alignment = 64 * 1024;
6b95a207
KH
2224 break;
2225 case I915_TILING_X:
1fada4cc
DL
2226 if (INTEL_INFO(dev)->gen >= 9)
2227 alignment = 256 * 1024;
2228 else {
2229 /* pin() will align the object as required by fence */
2230 alignment = 0;
2231 }
6b95a207
KH
2232 break;
2233 case I915_TILING_Y:
80075d49 2234 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2235 return -EINVAL;
2236 default:
2237 BUG();
2238 }
2239
693db184
CW
2240 /* Note that the w/a also requires 64 PTE of padding following the
2241 * bo. We currently fill all unused PTE with the shadow page and so
2242 * we should always have valid PTE following the scanout preventing
2243 * the VT-d warning.
2244 */
2245 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2246 alignment = 256 * 1024;
2247
d6dd6843
PZ
2248 /*
2249 * Global gtt pte registers are special registers which actually forward
2250 * writes to a chunk of system memory. Which means that there is no risk
2251 * that the register values disappear as soon as we call
2252 * intel_runtime_pm_put(), so it is correct to wrap only the
2253 * pin/unpin/fence and not more.
2254 */
2255 intel_runtime_pm_get(dev_priv);
2256
ce453d81 2257 dev_priv->mm.interruptible = false;
2da3b9b9 2258 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2259 if (ret)
ce453d81 2260 goto err_interruptible;
6b95a207
KH
2261
2262 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2263 * fence, whereas 965+ only requires a fence if using
2264 * framebuffer compression. For simplicity, we always install
2265 * a fence as the cost is not that onerous.
2266 */
06d98131 2267 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2268 if (ret)
2269 goto err_unpin;
1690e1eb 2270
9a5a53b3 2271 i915_gem_object_pin_fence(obj);
6b95a207 2272
ce453d81 2273 dev_priv->mm.interruptible = true;
d6dd6843 2274 intel_runtime_pm_put(dev_priv);
6b95a207 2275 return 0;
48b956c5
CW
2276
2277err_unpin:
cc98b413 2278 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2279err_interruptible:
2280 dev_priv->mm.interruptible = true;
d6dd6843 2281 intel_runtime_pm_put(dev_priv);
48b956c5 2282 return ret;
6b95a207
KH
2283}
2284
1690e1eb
CW
2285void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2286{
ebcdd39e
MR
2287 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2288
1690e1eb 2289 i915_gem_object_unpin_fence(obj);
cc98b413 2290 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2291}
2292
c2c75131
DV
2293/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2294 * is assumed to be a power-of-two. */
bc752862
CW
2295unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2296 unsigned int tiling_mode,
2297 unsigned int cpp,
2298 unsigned int pitch)
c2c75131 2299{
bc752862
CW
2300 if (tiling_mode != I915_TILING_NONE) {
2301 unsigned int tile_rows, tiles;
c2c75131 2302
bc752862
CW
2303 tile_rows = *y / 8;
2304 *y %= 8;
c2c75131 2305
bc752862
CW
2306 tiles = *x / (512/cpp);
2307 *x %= 512/cpp;
2308
2309 return tile_rows * pitch * 8 + tiles * 4096;
2310 } else {
2311 unsigned int offset;
2312
2313 offset = *y * pitch + *x * cpp;
2314 *y = 0;
2315 *x = (offset & 4095) / cpp;
2316 return offset & -4096;
2317 }
c2c75131
DV
2318}
2319
b35d63fa 2320static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2321{
2322 switch (format) {
2323 case DISPPLANE_8BPP:
2324 return DRM_FORMAT_C8;
2325 case DISPPLANE_BGRX555:
2326 return DRM_FORMAT_XRGB1555;
2327 case DISPPLANE_BGRX565:
2328 return DRM_FORMAT_RGB565;
2329 default:
2330 case DISPPLANE_BGRX888:
2331 return DRM_FORMAT_XRGB8888;
2332 case DISPPLANE_RGBX888:
2333 return DRM_FORMAT_XBGR8888;
2334 case DISPPLANE_BGRX101010:
2335 return DRM_FORMAT_XRGB2101010;
2336 case DISPPLANE_RGBX101010:
2337 return DRM_FORMAT_XBGR2101010;
2338 }
2339}
2340
bc8d7dff
DL
2341static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2342{
2343 switch (format) {
2344 case PLANE_CTL_FORMAT_RGB_565:
2345 return DRM_FORMAT_RGB565;
2346 default:
2347 case PLANE_CTL_FORMAT_XRGB_8888:
2348 if (rgb_order) {
2349 if (alpha)
2350 return DRM_FORMAT_ABGR8888;
2351 else
2352 return DRM_FORMAT_XBGR8888;
2353 } else {
2354 if (alpha)
2355 return DRM_FORMAT_ARGB8888;
2356 else
2357 return DRM_FORMAT_XRGB8888;
2358 }
2359 case PLANE_CTL_FORMAT_XRGB_2101010:
2360 if (rgb_order)
2361 return DRM_FORMAT_XBGR2101010;
2362 else
2363 return DRM_FORMAT_XRGB2101010;
2364 }
2365}
2366
5724dbd1
DL
2367static bool
2368intel_alloc_plane_obj(struct intel_crtc *crtc,
2369 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2370{
2371 struct drm_device *dev = crtc->base.dev;
2372 struct drm_i915_gem_object *obj = NULL;
2373 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2374 struct drm_framebuffer *fb = &plane_config->fb->base;
46f297fb
JB
2375 u32 base = plane_config->base;
2376
ff2652ea
CW
2377 if (plane_config->size == 0)
2378 return false;
2379
46f297fb
JB
2380 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2381 plane_config->size);
2382 if (!obj)
484b41dd 2383 return false;
46f297fb 2384
49af449b
DL
2385 obj->tiling_mode = plane_config->tiling;
2386 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2387 obj->stride = fb->pitches[0];
46f297fb 2388
6bf129df
DL
2389 mode_cmd.pixel_format = fb->pixel_format;
2390 mode_cmd.width = fb->width;
2391 mode_cmd.height = fb->height;
2392 mode_cmd.pitches[0] = fb->pitches[0];
46f297fb
JB
2393
2394 mutex_lock(&dev->struct_mutex);
2395
6bf129df 2396 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2397 &mode_cmd, obj)) {
46f297fb
JB
2398 DRM_DEBUG_KMS("intel fb init failed\n");
2399 goto out_unref_obj;
2400 }
2401
a071fa00 2402 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2403 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2404
2405 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2406 return true;
46f297fb
JB
2407
2408out_unref_obj:
2409 drm_gem_object_unreference(&obj->base);
2410 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2411 return false;
2412}
2413
afd65eb4
MR
2414/* Update plane->state->fb to match plane->fb after driver-internal updates */
2415static void
2416update_state_fb(struct drm_plane *plane)
2417{
2418 if (plane->fb == plane->state->fb)
2419 return;
2420
2421 if (plane->state->fb)
2422 drm_framebuffer_unreference(plane->state->fb);
2423 plane->state->fb = plane->fb;
2424 if (plane->state->fb)
2425 drm_framebuffer_reference(plane->state->fb);
2426}
2427
5724dbd1
DL
2428static void
2429intel_find_plane_obj(struct intel_crtc *intel_crtc,
2430 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2431{
2432 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2433 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2434 struct drm_crtc *c;
2435 struct intel_crtc *i;
2ff8fde1 2436 struct drm_i915_gem_object *obj;
484b41dd 2437
2d14030b 2438 if (!plane_config->fb)
484b41dd
JB
2439 return;
2440
f55548b5 2441 if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
2d14030b 2442 intel_crtc->base.primary->fb = &plane_config->fb->base;
f55548b5 2443 update_state_fb(intel_crtc->base.primary);
484b41dd 2444 return;
f55548b5 2445 }
484b41dd 2446
2d14030b 2447 kfree(plane_config->fb);
484b41dd
JB
2448
2449 /*
2450 * Failed to alloc the obj, check to see if we should share
2451 * an fb with another CRTC instead
2452 */
70e1e0ec 2453 for_each_crtc(dev, c) {
484b41dd
JB
2454 i = to_intel_crtc(c);
2455
2456 if (c == &intel_crtc->base)
2457 continue;
2458
2ff8fde1
MR
2459 if (!i->active)
2460 continue;
2461
2462 obj = intel_fb_obj(c->primary->fb);
2463 if (obj == NULL)
484b41dd
JB
2464 continue;
2465
2ff8fde1 2466 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
d9ceb816
JB
2467 if (obj->tiling_mode != I915_TILING_NONE)
2468 dev_priv->preserve_bios_swizzle = true;
2469
66e514c1
DA
2470 drm_framebuffer_reference(c->primary->fb);
2471 intel_crtc->base.primary->fb = c->primary->fb;
5ba76c41 2472 update_state_fb(intel_crtc->base.primary);
2ff8fde1 2473 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2474 break;
2475 }
2476 }
afd65eb4 2477
46f297fb
JB
2478}
2479
29b9bde6
DV
2480static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2481 struct drm_framebuffer *fb,
2482 int x, int y)
81255565
JB
2483{
2484 struct drm_device *dev = crtc->dev;
2485 struct drm_i915_private *dev_priv = dev->dev_private;
2486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2487 struct drm_i915_gem_object *obj;
81255565 2488 int plane = intel_crtc->plane;
e506a0c6 2489 unsigned long linear_offset;
81255565 2490 u32 dspcntr;
f45651ba 2491 u32 reg = DSPCNTR(plane);
48404c1e 2492 int pixel_size;
f45651ba 2493
fdd508a6
VS
2494 if (!intel_crtc->primary_enabled) {
2495 I915_WRITE(reg, 0);
2496 if (INTEL_INFO(dev)->gen >= 4)
2497 I915_WRITE(DSPSURF(plane), 0);
2498 else
2499 I915_WRITE(DSPADDR(plane), 0);
2500 POSTING_READ(reg);
2501 return;
2502 }
2503
c9ba6fad
VS
2504 obj = intel_fb_obj(fb);
2505 if (WARN_ON(obj == NULL))
2506 return;
2507
2508 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2509
f45651ba
VS
2510 dspcntr = DISPPLANE_GAMMA_ENABLE;
2511
fdd508a6 2512 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2513
2514 if (INTEL_INFO(dev)->gen < 4) {
2515 if (intel_crtc->pipe == PIPE_B)
2516 dspcntr |= DISPPLANE_SEL_PIPE_B;
2517
2518 /* pipesrc and dspsize control the size that is scaled from,
2519 * which should always be the user's requested size.
2520 */
2521 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2522 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2523 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2524 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2525 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2526 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2527 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2528 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2529 I915_WRITE(PRIMPOS(plane), 0);
2530 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2531 }
81255565 2532
57779d06
VS
2533 switch (fb->pixel_format) {
2534 case DRM_FORMAT_C8:
81255565
JB
2535 dspcntr |= DISPPLANE_8BPP;
2536 break;
57779d06
VS
2537 case DRM_FORMAT_XRGB1555:
2538 case DRM_FORMAT_ARGB1555:
2539 dspcntr |= DISPPLANE_BGRX555;
81255565 2540 break;
57779d06
VS
2541 case DRM_FORMAT_RGB565:
2542 dspcntr |= DISPPLANE_BGRX565;
2543 break;
2544 case DRM_FORMAT_XRGB8888:
2545 case DRM_FORMAT_ARGB8888:
2546 dspcntr |= DISPPLANE_BGRX888;
2547 break;
2548 case DRM_FORMAT_XBGR8888:
2549 case DRM_FORMAT_ABGR8888:
2550 dspcntr |= DISPPLANE_RGBX888;
2551 break;
2552 case DRM_FORMAT_XRGB2101010:
2553 case DRM_FORMAT_ARGB2101010:
2554 dspcntr |= DISPPLANE_BGRX101010;
2555 break;
2556 case DRM_FORMAT_XBGR2101010:
2557 case DRM_FORMAT_ABGR2101010:
2558 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2559 break;
2560 default:
baba133a 2561 BUG();
81255565 2562 }
57779d06 2563
f45651ba
VS
2564 if (INTEL_INFO(dev)->gen >= 4 &&
2565 obj->tiling_mode != I915_TILING_NONE)
2566 dspcntr |= DISPPLANE_TILED;
81255565 2567
de1aa629
VS
2568 if (IS_G4X(dev))
2569 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2570
b9897127 2571 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2572
c2c75131
DV
2573 if (INTEL_INFO(dev)->gen >= 4) {
2574 intel_crtc->dspaddr_offset =
bc752862 2575 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2576 pixel_size,
bc752862 2577 fb->pitches[0]);
c2c75131
DV
2578 linear_offset -= intel_crtc->dspaddr_offset;
2579 } else {
e506a0c6 2580 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2581 }
e506a0c6 2582
8e7d688b 2583 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2584 dspcntr |= DISPPLANE_ROTATE_180;
2585
6e3c9717
ACO
2586 x += (intel_crtc->config->pipe_src_w - 1);
2587 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2588
2589 /* Finding the last pixel of the last line of the display
2590 data and adding to linear_offset*/
2591 linear_offset +=
6e3c9717
ACO
2592 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2593 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2594 }
2595
2596 I915_WRITE(reg, dspcntr);
2597
f343c5f6
BW
2598 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2599 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2600 fb->pitches[0]);
01f2c773 2601 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2602 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2603 I915_WRITE(DSPSURF(plane),
2604 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2605 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2606 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2607 } else
f343c5f6 2608 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2609 POSTING_READ(reg);
17638cd6
JB
2610}
2611
29b9bde6
DV
2612static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2613 struct drm_framebuffer *fb,
2614 int x, int y)
17638cd6
JB
2615{
2616 struct drm_device *dev = crtc->dev;
2617 struct drm_i915_private *dev_priv = dev->dev_private;
2618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2619 struct drm_i915_gem_object *obj;
17638cd6 2620 int plane = intel_crtc->plane;
e506a0c6 2621 unsigned long linear_offset;
17638cd6 2622 u32 dspcntr;
f45651ba 2623 u32 reg = DSPCNTR(plane);
48404c1e 2624 int pixel_size;
f45651ba 2625
fdd508a6
VS
2626 if (!intel_crtc->primary_enabled) {
2627 I915_WRITE(reg, 0);
2628 I915_WRITE(DSPSURF(plane), 0);
2629 POSTING_READ(reg);
2630 return;
2631 }
2632
c9ba6fad
VS
2633 obj = intel_fb_obj(fb);
2634 if (WARN_ON(obj == NULL))
2635 return;
2636
2637 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2638
f45651ba
VS
2639 dspcntr = DISPPLANE_GAMMA_ENABLE;
2640
fdd508a6 2641 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2642
2643 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2644 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2645
57779d06
VS
2646 switch (fb->pixel_format) {
2647 case DRM_FORMAT_C8:
17638cd6
JB
2648 dspcntr |= DISPPLANE_8BPP;
2649 break;
57779d06
VS
2650 case DRM_FORMAT_RGB565:
2651 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2652 break;
57779d06
VS
2653 case DRM_FORMAT_XRGB8888:
2654 case DRM_FORMAT_ARGB8888:
2655 dspcntr |= DISPPLANE_BGRX888;
2656 break;
2657 case DRM_FORMAT_XBGR8888:
2658 case DRM_FORMAT_ABGR8888:
2659 dspcntr |= DISPPLANE_RGBX888;
2660 break;
2661 case DRM_FORMAT_XRGB2101010:
2662 case DRM_FORMAT_ARGB2101010:
2663 dspcntr |= DISPPLANE_BGRX101010;
2664 break;
2665 case DRM_FORMAT_XBGR2101010:
2666 case DRM_FORMAT_ABGR2101010:
2667 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2668 break;
2669 default:
baba133a 2670 BUG();
17638cd6
JB
2671 }
2672
2673 if (obj->tiling_mode != I915_TILING_NONE)
2674 dspcntr |= DISPPLANE_TILED;
17638cd6 2675
f45651ba 2676 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2677 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2678
b9897127 2679 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2680 intel_crtc->dspaddr_offset =
bc752862 2681 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2682 pixel_size,
bc752862 2683 fb->pitches[0]);
c2c75131 2684 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2685 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2686 dspcntr |= DISPPLANE_ROTATE_180;
2687
2688 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2689 x += (intel_crtc->config->pipe_src_w - 1);
2690 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2691
2692 /* Finding the last pixel of the last line of the display
2693 data and adding to linear_offset*/
2694 linear_offset +=
6e3c9717
ACO
2695 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2696 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2697 }
2698 }
2699
2700 I915_WRITE(reg, dspcntr);
17638cd6 2701
f343c5f6
BW
2702 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2703 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2704 fb->pitches[0]);
01f2c773 2705 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2706 I915_WRITE(DSPSURF(plane),
2707 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2708 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2709 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2710 } else {
2711 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2712 I915_WRITE(DSPLINOFF(plane), linear_offset);
2713 }
17638cd6 2714 POSTING_READ(reg);
17638cd6
JB
2715}
2716
70d21f0e
DL
2717static void skylake_update_primary_plane(struct drm_crtc *crtc,
2718 struct drm_framebuffer *fb,
2719 int x, int y)
2720{
2721 struct drm_device *dev = crtc->dev;
2722 struct drm_i915_private *dev_priv = dev->dev_private;
2723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2724 struct intel_framebuffer *intel_fb;
2725 struct drm_i915_gem_object *obj;
2726 int pipe = intel_crtc->pipe;
2727 u32 plane_ctl, stride;
2728
2729 if (!intel_crtc->primary_enabled) {
2730 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2731 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2732 POSTING_READ(PLANE_CTL(pipe, 0));
2733 return;
2734 }
2735
2736 plane_ctl = PLANE_CTL_ENABLE |
2737 PLANE_CTL_PIPE_GAMMA_ENABLE |
2738 PLANE_CTL_PIPE_CSC_ENABLE;
2739
2740 switch (fb->pixel_format) {
2741 case DRM_FORMAT_RGB565:
2742 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2743 break;
2744 case DRM_FORMAT_XRGB8888:
2745 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2746 break;
2747 case DRM_FORMAT_XBGR8888:
2748 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2749 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2750 break;
2751 case DRM_FORMAT_XRGB2101010:
2752 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2753 break;
2754 case DRM_FORMAT_XBGR2101010:
2755 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2756 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2757 break;
2758 default:
2759 BUG();
2760 }
2761
2762 intel_fb = to_intel_framebuffer(fb);
2763 obj = intel_fb->obj;
2764
2765 /*
2766 * The stride is either expressed as a multiple of 64 bytes chunks for
2767 * linear buffers or in number of tiles for tiled buffers.
2768 */
2769 switch (obj->tiling_mode) {
2770 case I915_TILING_NONE:
2771 stride = fb->pitches[0] >> 6;
2772 break;
2773 case I915_TILING_X:
2774 plane_ctl |= PLANE_CTL_TILED_X;
2775 stride = fb->pitches[0] >> 9;
2776 break;
2777 default:
2778 BUG();
2779 }
2780
2781 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
8e7d688b 2782 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
1447dde0 2783 plane_ctl |= PLANE_CTL_ROTATE_180;
70d21f0e
DL
2784
2785 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2786
2787 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2788 i915_gem_obj_ggtt_offset(obj),
2789 x, y, fb->width, fb->height,
2790 fb->pitches[0]);
2791
2792 I915_WRITE(PLANE_POS(pipe, 0), 0);
2793 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2794 I915_WRITE(PLANE_SIZE(pipe, 0),
6e3c9717
ACO
2795 (intel_crtc->config->pipe_src_h - 1) << 16 |
2796 (intel_crtc->config->pipe_src_w - 1));
70d21f0e
DL
2797 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2798 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2799
2800 POSTING_READ(PLANE_SURF(pipe, 0));
2801}
2802
17638cd6
JB
2803/* Assume fb object is pinned & idle & fenced and just update base pointers */
2804static int
2805intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2806 int x, int y, enum mode_set_atomic state)
2807{
2808 struct drm_device *dev = crtc->dev;
2809 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2810
6b8e6ed0
CW
2811 if (dev_priv->display.disable_fbc)
2812 dev_priv->display.disable_fbc(dev);
81255565 2813
29b9bde6
DV
2814 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2815
2816 return 0;
81255565
JB
2817}
2818
7514747d 2819static void intel_complete_page_flips(struct drm_device *dev)
96a02917 2820{
96a02917
VS
2821 struct drm_crtc *crtc;
2822
70e1e0ec 2823 for_each_crtc(dev, crtc) {
96a02917
VS
2824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2825 enum plane plane = intel_crtc->plane;
2826
2827 intel_prepare_page_flip(dev, plane);
2828 intel_finish_page_flip_plane(dev, plane);
2829 }
7514747d
VS
2830}
2831
2832static void intel_update_primary_planes(struct drm_device *dev)
2833{
2834 struct drm_i915_private *dev_priv = dev->dev_private;
2835 struct drm_crtc *crtc;
96a02917 2836
70e1e0ec 2837 for_each_crtc(dev, crtc) {
96a02917
VS
2838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2839
51fd371b 2840 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2841 /*
2842 * FIXME: Once we have proper support for primary planes (and
2843 * disabling them without disabling the entire crtc) allow again
66e514c1 2844 * a NULL crtc->primary->fb.
947fdaad 2845 */
f4510a27 2846 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2847 dev_priv->display.update_primary_plane(crtc,
66e514c1 2848 crtc->primary->fb,
262ca2b0
MR
2849 crtc->x,
2850 crtc->y);
51fd371b 2851 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2852 }
2853}
2854
7514747d
VS
2855void intel_prepare_reset(struct drm_device *dev)
2856{
f98ce92f
VS
2857 struct drm_i915_private *dev_priv = to_i915(dev);
2858 struct intel_crtc *crtc;
2859
7514747d
VS
2860 /* no reset support for gen2 */
2861 if (IS_GEN2(dev))
2862 return;
2863
2864 /* reset doesn't touch the display */
2865 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
2866 return;
2867
2868 drm_modeset_lock_all(dev);
f98ce92f
VS
2869
2870 /*
2871 * Disabling the crtcs gracefully seems nicer. Also the
2872 * g33 docs say we should at least disable all the planes.
2873 */
2874 for_each_intel_crtc(dev, crtc) {
2875 if (crtc->active)
2876 dev_priv->display.crtc_disable(&crtc->base);
2877 }
7514747d
VS
2878}
2879
2880void intel_finish_reset(struct drm_device *dev)
2881{
2882 struct drm_i915_private *dev_priv = to_i915(dev);
2883
2884 /*
2885 * Flips in the rings will be nuked by the reset,
2886 * so complete all pending flips so that user space
2887 * will get its events and not get stuck.
2888 */
2889 intel_complete_page_flips(dev);
2890
2891 /* no reset support for gen2 */
2892 if (IS_GEN2(dev))
2893 return;
2894
2895 /* reset doesn't touch the display */
2896 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
2897 /*
2898 * Flips in the rings have been nuked by the reset,
2899 * so update the base address of all primary
2900 * planes to the the last fb to make sure we're
2901 * showing the correct fb after a reset.
2902 */
2903 intel_update_primary_planes(dev);
2904 return;
2905 }
2906
2907 /*
2908 * The display has been reset as well,
2909 * so need a full re-initialization.
2910 */
2911 intel_runtime_pm_disable_interrupts(dev_priv);
2912 intel_runtime_pm_enable_interrupts(dev_priv);
2913
2914 intel_modeset_init_hw(dev);
2915
2916 spin_lock_irq(&dev_priv->irq_lock);
2917 if (dev_priv->display.hpd_irq_setup)
2918 dev_priv->display.hpd_irq_setup(dev);
2919 spin_unlock_irq(&dev_priv->irq_lock);
2920
2921 intel_modeset_setup_hw_state(dev, true);
2922
2923 intel_hpd_init(dev_priv);
2924
2925 drm_modeset_unlock_all(dev);
2926}
2927
14667a4b
CW
2928static int
2929intel_finish_fb(struct drm_framebuffer *old_fb)
2930{
2ff8fde1 2931 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2932 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2933 bool was_interruptible = dev_priv->mm.interruptible;
2934 int ret;
2935
14667a4b
CW
2936 /* Big Hammer, we also need to ensure that any pending
2937 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2938 * current scanout is retired before unpinning the old
2939 * framebuffer.
2940 *
2941 * This should only fail upon a hung GPU, in which case we
2942 * can safely continue.
2943 */
2944 dev_priv->mm.interruptible = false;
2945 ret = i915_gem_object_finish_gpu(obj);
2946 dev_priv->mm.interruptible = was_interruptible;
2947
2948 return ret;
2949}
2950
7d5e3799
CW
2951static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2952{
2953 struct drm_device *dev = crtc->dev;
2954 struct drm_i915_private *dev_priv = dev->dev_private;
2955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
2956 bool pending;
2957
2958 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2959 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2960 return false;
2961
5e2d7afc 2962 spin_lock_irq(&dev->event_lock);
7d5e3799 2963 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 2964 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
2965
2966 return pending;
2967}
2968
e30e8f75
GP
2969static void intel_update_pipe_size(struct intel_crtc *crtc)
2970{
2971 struct drm_device *dev = crtc->base.dev;
2972 struct drm_i915_private *dev_priv = dev->dev_private;
2973 const struct drm_display_mode *adjusted_mode;
2974
2975 if (!i915.fastboot)
2976 return;
2977
2978 /*
2979 * Update pipe size and adjust fitter if needed: the reason for this is
2980 * that in compute_mode_changes we check the native mode (not the pfit
2981 * mode) to see if we can flip rather than do a full mode set. In the
2982 * fastboot case, we'll flip, but if we don't update the pipesrc and
2983 * pfit state, we'll end up with a big fb scanned out into the wrong
2984 * sized surface.
2985 *
2986 * To fix this properly, we need to hoist the checks up into
2987 * compute_mode_changes (or above), check the actual pfit state and
2988 * whether the platform allows pfit disable with pipe active, and only
2989 * then update the pipesrc and pfit state, even on the flip path.
2990 */
2991
6e3c9717 2992 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
2993
2994 I915_WRITE(PIPESRC(crtc->pipe),
2995 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2996 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 2997 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
2998 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2999 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3000 I915_WRITE(PF_CTL(crtc->pipe), 0);
3001 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3002 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3003 }
6e3c9717
ACO
3004 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3005 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3006}
3007
5e84e1a4
ZW
3008static void intel_fdi_normal_train(struct drm_crtc *crtc)
3009{
3010 struct drm_device *dev = crtc->dev;
3011 struct drm_i915_private *dev_priv = dev->dev_private;
3012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3013 int pipe = intel_crtc->pipe;
3014 u32 reg, temp;
3015
3016 /* enable normal train */
3017 reg = FDI_TX_CTL(pipe);
3018 temp = I915_READ(reg);
61e499bf 3019 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3020 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3021 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3022 } else {
3023 temp &= ~FDI_LINK_TRAIN_NONE;
3024 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3025 }
5e84e1a4
ZW
3026 I915_WRITE(reg, temp);
3027
3028 reg = FDI_RX_CTL(pipe);
3029 temp = I915_READ(reg);
3030 if (HAS_PCH_CPT(dev)) {
3031 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3032 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3033 } else {
3034 temp &= ~FDI_LINK_TRAIN_NONE;
3035 temp |= FDI_LINK_TRAIN_NONE;
3036 }
3037 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3038
3039 /* wait one idle pattern time */
3040 POSTING_READ(reg);
3041 udelay(1000);
357555c0
JB
3042
3043 /* IVB wants error correction enabled */
3044 if (IS_IVYBRIDGE(dev))
3045 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3046 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3047}
3048
1fbc0d78 3049static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 3050{
1fbc0d78 3051 return crtc->base.enabled && crtc->active &&
6e3c9717 3052 crtc->config->has_pch_encoder;
1e833f40
DV
3053}
3054
01a415fd
DV
3055static void ivb_modeset_global_resources(struct drm_device *dev)
3056{
3057 struct drm_i915_private *dev_priv = dev->dev_private;
3058 struct intel_crtc *pipe_B_crtc =
3059 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3060 struct intel_crtc *pipe_C_crtc =
3061 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
3062 uint32_t temp;
3063
1e833f40
DV
3064 /*
3065 * When everything is off disable fdi C so that we could enable fdi B
3066 * with all lanes. Note that we don't care about enabled pipes without
3067 * an enabled pch encoder.
3068 */
3069 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
3070 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
3071 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3072 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3073
3074 temp = I915_READ(SOUTH_CHICKEN1);
3075 temp &= ~FDI_BC_BIFURCATION_SELECT;
3076 DRM_DEBUG_KMS("disabling fdi C rx\n");
3077 I915_WRITE(SOUTH_CHICKEN1, temp);
3078 }
3079}
3080
8db9d77b
ZW
3081/* The FDI link training functions for ILK/Ibexpeak. */
3082static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3083{
3084 struct drm_device *dev = crtc->dev;
3085 struct drm_i915_private *dev_priv = dev->dev_private;
3086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3087 int pipe = intel_crtc->pipe;
5eddb70b 3088 u32 reg, temp, tries;
8db9d77b 3089
1c8562f6 3090 /* FDI needs bits from pipe first */
0fc932b8 3091 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3092
e1a44743
AJ
3093 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3094 for train result */
5eddb70b
CW
3095 reg = FDI_RX_IMR(pipe);
3096 temp = I915_READ(reg);
e1a44743
AJ
3097 temp &= ~FDI_RX_SYMBOL_LOCK;
3098 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3099 I915_WRITE(reg, temp);
3100 I915_READ(reg);
e1a44743
AJ
3101 udelay(150);
3102
8db9d77b 3103 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3104 reg = FDI_TX_CTL(pipe);
3105 temp = I915_READ(reg);
627eb5a3 3106 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3107 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3108 temp &= ~FDI_LINK_TRAIN_NONE;
3109 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3110 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3111
5eddb70b
CW
3112 reg = FDI_RX_CTL(pipe);
3113 temp = I915_READ(reg);
8db9d77b
ZW
3114 temp &= ~FDI_LINK_TRAIN_NONE;
3115 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3116 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3117
3118 POSTING_READ(reg);
8db9d77b
ZW
3119 udelay(150);
3120
5b2adf89 3121 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3122 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3123 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3124 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3125
5eddb70b 3126 reg = FDI_RX_IIR(pipe);
e1a44743 3127 for (tries = 0; tries < 5; tries++) {
5eddb70b 3128 temp = I915_READ(reg);
8db9d77b
ZW
3129 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3130
3131 if ((temp & FDI_RX_BIT_LOCK)) {
3132 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3133 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3134 break;
3135 }
8db9d77b 3136 }
e1a44743 3137 if (tries == 5)
5eddb70b 3138 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3139
3140 /* Train 2 */
5eddb70b
CW
3141 reg = FDI_TX_CTL(pipe);
3142 temp = I915_READ(reg);
8db9d77b
ZW
3143 temp &= ~FDI_LINK_TRAIN_NONE;
3144 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3145 I915_WRITE(reg, temp);
8db9d77b 3146
5eddb70b
CW
3147 reg = FDI_RX_CTL(pipe);
3148 temp = I915_READ(reg);
8db9d77b
ZW
3149 temp &= ~FDI_LINK_TRAIN_NONE;
3150 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3151 I915_WRITE(reg, temp);
8db9d77b 3152
5eddb70b
CW
3153 POSTING_READ(reg);
3154 udelay(150);
8db9d77b 3155
5eddb70b 3156 reg = FDI_RX_IIR(pipe);
e1a44743 3157 for (tries = 0; tries < 5; tries++) {
5eddb70b 3158 temp = I915_READ(reg);
8db9d77b
ZW
3159 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3160
3161 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3162 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3163 DRM_DEBUG_KMS("FDI train 2 done.\n");
3164 break;
3165 }
8db9d77b 3166 }
e1a44743 3167 if (tries == 5)
5eddb70b 3168 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3169
3170 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3171
8db9d77b
ZW
3172}
3173
0206e353 3174static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3175 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3176 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3177 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3178 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3179};
3180
3181/* The FDI link training functions for SNB/Cougarpoint. */
3182static void gen6_fdi_link_train(struct drm_crtc *crtc)
3183{
3184 struct drm_device *dev = crtc->dev;
3185 struct drm_i915_private *dev_priv = dev->dev_private;
3186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3187 int pipe = intel_crtc->pipe;
fa37d39e 3188 u32 reg, temp, i, retry;
8db9d77b 3189
e1a44743
AJ
3190 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3191 for train result */
5eddb70b
CW
3192 reg = FDI_RX_IMR(pipe);
3193 temp = I915_READ(reg);
e1a44743
AJ
3194 temp &= ~FDI_RX_SYMBOL_LOCK;
3195 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3196 I915_WRITE(reg, temp);
3197
3198 POSTING_READ(reg);
e1a44743
AJ
3199 udelay(150);
3200
8db9d77b 3201 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3202 reg = FDI_TX_CTL(pipe);
3203 temp = I915_READ(reg);
627eb5a3 3204 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3205 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3206 temp &= ~FDI_LINK_TRAIN_NONE;
3207 temp |= FDI_LINK_TRAIN_PATTERN_1;
3208 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3209 /* SNB-B */
3210 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3211 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3212
d74cf324
DV
3213 I915_WRITE(FDI_RX_MISC(pipe),
3214 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3215
5eddb70b
CW
3216 reg = FDI_RX_CTL(pipe);
3217 temp = I915_READ(reg);
8db9d77b
ZW
3218 if (HAS_PCH_CPT(dev)) {
3219 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3220 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3221 } else {
3222 temp &= ~FDI_LINK_TRAIN_NONE;
3223 temp |= FDI_LINK_TRAIN_PATTERN_1;
3224 }
5eddb70b
CW
3225 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3226
3227 POSTING_READ(reg);
8db9d77b
ZW
3228 udelay(150);
3229
0206e353 3230 for (i = 0; i < 4; i++) {
5eddb70b
CW
3231 reg = FDI_TX_CTL(pipe);
3232 temp = I915_READ(reg);
8db9d77b
ZW
3233 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3234 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3235 I915_WRITE(reg, temp);
3236
3237 POSTING_READ(reg);
8db9d77b
ZW
3238 udelay(500);
3239
fa37d39e
SP
3240 for (retry = 0; retry < 5; retry++) {
3241 reg = FDI_RX_IIR(pipe);
3242 temp = I915_READ(reg);
3243 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3244 if (temp & FDI_RX_BIT_LOCK) {
3245 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3246 DRM_DEBUG_KMS("FDI train 1 done.\n");
3247 break;
3248 }
3249 udelay(50);
8db9d77b 3250 }
fa37d39e
SP
3251 if (retry < 5)
3252 break;
8db9d77b
ZW
3253 }
3254 if (i == 4)
5eddb70b 3255 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3256
3257 /* Train 2 */
5eddb70b
CW
3258 reg = FDI_TX_CTL(pipe);
3259 temp = I915_READ(reg);
8db9d77b
ZW
3260 temp &= ~FDI_LINK_TRAIN_NONE;
3261 temp |= FDI_LINK_TRAIN_PATTERN_2;
3262 if (IS_GEN6(dev)) {
3263 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3264 /* SNB-B */
3265 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3266 }
5eddb70b 3267 I915_WRITE(reg, temp);
8db9d77b 3268
5eddb70b
CW
3269 reg = FDI_RX_CTL(pipe);
3270 temp = I915_READ(reg);
8db9d77b
ZW
3271 if (HAS_PCH_CPT(dev)) {
3272 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3273 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3274 } else {
3275 temp &= ~FDI_LINK_TRAIN_NONE;
3276 temp |= FDI_LINK_TRAIN_PATTERN_2;
3277 }
5eddb70b
CW
3278 I915_WRITE(reg, temp);
3279
3280 POSTING_READ(reg);
8db9d77b
ZW
3281 udelay(150);
3282
0206e353 3283 for (i = 0; i < 4; i++) {
5eddb70b
CW
3284 reg = FDI_TX_CTL(pipe);
3285 temp = I915_READ(reg);
8db9d77b
ZW
3286 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3287 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3288 I915_WRITE(reg, temp);
3289
3290 POSTING_READ(reg);
8db9d77b
ZW
3291 udelay(500);
3292
fa37d39e
SP
3293 for (retry = 0; retry < 5; retry++) {
3294 reg = FDI_RX_IIR(pipe);
3295 temp = I915_READ(reg);
3296 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3297 if (temp & FDI_RX_SYMBOL_LOCK) {
3298 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3299 DRM_DEBUG_KMS("FDI train 2 done.\n");
3300 break;
3301 }
3302 udelay(50);
8db9d77b 3303 }
fa37d39e
SP
3304 if (retry < 5)
3305 break;
8db9d77b
ZW
3306 }
3307 if (i == 4)
5eddb70b 3308 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3309
3310 DRM_DEBUG_KMS("FDI train done.\n");
3311}
3312
357555c0
JB
3313/* Manual link training for Ivy Bridge A0 parts */
3314static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3315{
3316 struct drm_device *dev = crtc->dev;
3317 struct drm_i915_private *dev_priv = dev->dev_private;
3318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3319 int pipe = intel_crtc->pipe;
139ccd3f 3320 u32 reg, temp, i, j;
357555c0
JB
3321
3322 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3323 for train result */
3324 reg = FDI_RX_IMR(pipe);
3325 temp = I915_READ(reg);
3326 temp &= ~FDI_RX_SYMBOL_LOCK;
3327 temp &= ~FDI_RX_BIT_LOCK;
3328 I915_WRITE(reg, temp);
3329
3330 POSTING_READ(reg);
3331 udelay(150);
3332
01a415fd
DV
3333 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3334 I915_READ(FDI_RX_IIR(pipe)));
3335
139ccd3f
JB
3336 /* Try each vswing and preemphasis setting twice before moving on */
3337 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3338 /* disable first in case we need to retry */
3339 reg = FDI_TX_CTL(pipe);
3340 temp = I915_READ(reg);
3341 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3342 temp &= ~FDI_TX_ENABLE;
3343 I915_WRITE(reg, temp);
357555c0 3344
139ccd3f
JB
3345 reg = FDI_RX_CTL(pipe);
3346 temp = I915_READ(reg);
3347 temp &= ~FDI_LINK_TRAIN_AUTO;
3348 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3349 temp &= ~FDI_RX_ENABLE;
3350 I915_WRITE(reg, temp);
357555c0 3351
139ccd3f 3352 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3353 reg = FDI_TX_CTL(pipe);
3354 temp = I915_READ(reg);
139ccd3f 3355 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3356 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3357 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3358 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3359 temp |= snb_b_fdi_train_param[j/2];
3360 temp |= FDI_COMPOSITE_SYNC;
3361 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3362
139ccd3f
JB
3363 I915_WRITE(FDI_RX_MISC(pipe),
3364 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3365
139ccd3f 3366 reg = FDI_RX_CTL(pipe);
357555c0 3367 temp = I915_READ(reg);
139ccd3f
JB
3368 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3369 temp |= FDI_COMPOSITE_SYNC;
3370 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3371
139ccd3f
JB
3372 POSTING_READ(reg);
3373 udelay(1); /* should be 0.5us */
357555c0 3374
139ccd3f
JB
3375 for (i = 0; i < 4; i++) {
3376 reg = FDI_RX_IIR(pipe);
3377 temp = I915_READ(reg);
3378 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3379
139ccd3f
JB
3380 if (temp & FDI_RX_BIT_LOCK ||
3381 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3382 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3383 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3384 i);
3385 break;
3386 }
3387 udelay(1); /* should be 0.5us */
3388 }
3389 if (i == 4) {
3390 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3391 continue;
3392 }
357555c0 3393
139ccd3f 3394 /* Train 2 */
357555c0
JB
3395 reg = FDI_TX_CTL(pipe);
3396 temp = I915_READ(reg);
139ccd3f
JB
3397 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3398 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3399 I915_WRITE(reg, temp);
3400
3401 reg = FDI_RX_CTL(pipe);
3402 temp = I915_READ(reg);
3403 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3404 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3405 I915_WRITE(reg, temp);
3406
3407 POSTING_READ(reg);
139ccd3f 3408 udelay(2); /* should be 1.5us */
357555c0 3409
139ccd3f
JB
3410 for (i = 0; i < 4; i++) {
3411 reg = FDI_RX_IIR(pipe);
3412 temp = I915_READ(reg);
3413 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3414
139ccd3f
JB
3415 if (temp & FDI_RX_SYMBOL_LOCK ||
3416 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3417 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3418 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3419 i);
3420 goto train_done;
3421 }
3422 udelay(2); /* should be 1.5us */
357555c0 3423 }
139ccd3f
JB
3424 if (i == 4)
3425 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3426 }
357555c0 3427
139ccd3f 3428train_done:
357555c0
JB
3429 DRM_DEBUG_KMS("FDI train done.\n");
3430}
3431
88cefb6c 3432static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3433{
88cefb6c 3434 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3435 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3436 int pipe = intel_crtc->pipe;
5eddb70b 3437 u32 reg, temp;
79e53945 3438
c64e311e 3439
c98e9dcf 3440 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3441 reg = FDI_RX_CTL(pipe);
3442 temp = I915_READ(reg);
627eb5a3 3443 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3444 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3445 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3446 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3447
3448 POSTING_READ(reg);
c98e9dcf
JB
3449 udelay(200);
3450
3451 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3452 temp = I915_READ(reg);
3453 I915_WRITE(reg, temp | FDI_PCDCLK);
3454
3455 POSTING_READ(reg);
c98e9dcf
JB
3456 udelay(200);
3457
20749730
PZ
3458 /* Enable CPU FDI TX PLL, always on for Ironlake */
3459 reg = FDI_TX_CTL(pipe);
3460 temp = I915_READ(reg);
3461 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3462 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3463
20749730
PZ
3464 POSTING_READ(reg);
3465 udelay(100);
6be4a607 3466 }
0e23b99d
JB
3467}
3468
88cefb6c
DV
3469static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3470{
3471 struct drm_device *dev = intel_crtc->base.dev;
3472 struct drm_i915_private *dev_priv = dev->dev_private;
3473 int pipe = intel_crtc->pipe;
3474 u32 reg, temp;
3475
3476 /* Switch from PCDclk to Rawclk */
3477 reg = FDI_RX_CTL(pipe);
3478 temp = I915_READ(reg);
3479 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3480
3481 /* Disable CPU FDI TX PLL */
3482 reg = FDI_TX_CTL(pipe);
3483 temp = I915_READ(reg);
3484 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3485
3486 POSTING_READ(reg);
3487 udelay(100);
3488
3489 reg = FDI_RX_CTL(pipe);
3490 temp = I915_READ(reg);
3491 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3492
3493 /* Wait for the clocks to turn off. */
3494 POSTING_READ(reg);
3495 udelay(100);
3496}
3497
0fc932b8
JB
3498static void ironlake_fdi_disable(struct drm_crtc *crtc)
3499{
3500 struct drm_device *dev = crtc->dev;
3501 struct drm_i915_private *dev_priv = dev->dev_private;
3502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3503 int pipe = intel_crtc->pipe;
3504 u32 reg, temp;
3505
3506 /* disable CPU FDI tx and PCH FDI rx */
3507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
3509 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3510 POSTING_READ(reg);
3511
3512 reg = FDI_RX_CTL(pipe);
3513 temp = I915_READ(reg);
3514 temp &= ~(0x7 << 16);
dfd07d72 3515 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3516 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3517
3518 POSTING_READ(reg);
3519 udelay(100);
3520
3521 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3522 if (HAS_PCH_IBX(dev))
6f06ce18 3523 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3524
3525 /* still set train pattern 1 */
3526 reg = FDI_TX_CTL(pipe);
3527 temp = I915_READ(reg);
3528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_1;
3530 I915_WRITE(reg, temp);
3531
3532 reg = FDI_RX_CTL(pipe);
3533 temp = I915_READ(reg);
3534 if (HAS_PCH_CPT(dev)) {
3535 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3536 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3537 } else {
3538 temp &= ~FDI_LINK_TRAIN_NONE;
3539 temp |= FDI_LINK_TRAIN_PATTERN_1;
3540 }
3541 /* BPC in FDI rx is consistent with that in PIPECONF */
3542 temp &= ~(0x07 << 16);
dfd07d72 3543 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3544 I915_WRITE(reg, temp);
3545
3546 POSTING_READ(reg);
3547 udelay(100);
3548}
3549
5dce5b93
CW
3550bool intel_has_pending_fb_unpin(struct drm_device *dev)
3551{
3552 struct intel_crtc *crtc;
3553
3554 /* Note that we don't need to be called with mode_config.lock here
3555 * as our list of CRTC objects is static for the lifetime of the
3556 * device and so cannot disappear as we iterate. Similarly, we can
3557 * happily treat the predicates as racy, atomic checks as userspace
3558 * cannot claim and pin a new fb without at least acquring the
3559 * struct_mutex and so serialising with us.
3560 */
d3fcc808 3561 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3562 if (atomic_read(&crtc->unpin_work_count) == 0)
3563 continue;
3564
3565 if (crtc->unpin_work)
3566 intel_wait_for_vblank(dev, crtc->pipe);
3567
3568 return true;
3569 }
3570
3571 return false;
3572}
3573
d6bbafa1
CW
3574static void page_flip_completed(struct intel_crtc *intel_crtc)
3575{
3576 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3577 struct intel_unpin_work *work = intel_crtc->unpin_work;
3578
3579 /* ensure that the unpin work is consistent wrt ->pending. */
3580 smp_rmb();
3581 intel_crtc->unpin_work = NULL;
3582
3583 if (work->event)
3584 drm_send_vblank_event(intel_crtc->base.dev,
3585 intel_crtc->pipe,
3586 work->event);
3587
3588 drm_crtc_vblank_put(&intel_crtc->base);
3589
3590 wake_up_all(&dev_priv->pending_flip_queue);
3591 queue_work(dev_priv->wq, &work->work);
3592
3593 trace_i915_flip_complete(intel_crtc->plane,
3594 work->pending_flip_obj);
3595}
3596
46a55d30 3597void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3598{
0f91128d 3599 struct drm_device *dev = crtc->dev;
5bb61643 3600 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3601
2c10d571 3602 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3603 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3604 !intel_crtc_has_pending_flip(crtc),
3605 60*HZ) == 0)) {
3606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3607
5e2d7afc 3608 spin_lock_irq(&dev->event_lock);
9c787942
CW
3609 if (intel_crtc->unpin_work) {
3610 WARN_ONCE(1, "Removing stuck page flip\n");
3611 page_flip_completed(intel_crtc);
3612 }
5e2d7afc 3613 spin_unlock_irq(&dev->event_lock);
9c787942 3614 }
5bb61643 3615
975d568a
CW
3616 if (crtc->primary->fb) {
3617 mutex_lock(&dev->struct_mutex);
3618 intel_finish_fb(crtc->primary->fb);
3619 mutex_unlock(&dev->struct_mutex);
3620 }
e6c3a2a6
CW
3621}
3622
e615efe4
ED
3623/* Program iCLKIP clock to the desired frequency */
3624static void lpt_program_iclkip(struct drm_crtc *crtc)
3625{
3626 struct drm_device *dev = crtc->dev;
3627 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3628 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3629 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3630 u32 temp;
3631
09153000
DV
3632 mutex_lock(&dev_priv->dpio_lock);
3633
e615efe4
ED
3634 /* It is necessary to ungate the pixclk gate prior to programming
3635 * the divisors, and gate it back when it is done.
3636 */
3637 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3638
3639 /* Disable SSCCTL */
3640 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3641 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3642 SBI_SSCCTL_DISABLE,
3643 SBI_ICLK);
e615efe4
ED
3644
3645 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3646 if (clock == 20000) {
e615efe4
ED
3647 auxdiv = 1;
3648 divsel = 0x41;
3649 phaseinc = 0x20;
3650 } else {
3651 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3652 * but the adjusted_mode->crtc_clock in in KHz. To get the
3653 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3654 * convert the virtual clock precision to KHz here for higher
3655 * precision.
3656 */
3657 u32 iclk_virtual_root_freq = 172800 * 1000;
3658 u32 iclk_pi_range = 64;
3659 u32 desired_divisor, msb_divisor_value, pi_value;
3660
12d7ceed 3661 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3662 msb_divisor_value = desired_divisor / iclk_pi_range;
3663 pi_value = desired_divisor % iclk_pi_range;
3664
3665 auxdiv = 0;
3666 divsel = msb_divisor_value - 2;
3667 phaseinc = pi_value;
3668 }
3669
3670 /* This should not happen with any sane values */
3671 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3672 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3673 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3674 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3675
3676 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3677 clock,
e615efe4
ED
3678 auxdiv,
3679 divsel,
3680 phasedir,
3681 phaseinc);
3682
3683 /* Program SSCDIVINTPHASE6 */
988d6ee8 3684 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3685 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3686 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3687 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3688 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3689 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3690 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3691 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3692
3693 /* Program SSCAUXDIV */
988d6ee8 3694 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3695 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3696 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3697 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3698
3699 /* Enable modulator and associated divider */
988d6ee8 3700 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3701 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3702 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3703
3704 /* Wait for initialization time */
3705 udelay(24);
3706
3707 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3708
3709 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3710}
3711
275f01b2
DV
3712static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3713 enum pipe pch_transcoder)
3714{
3715 struct drm_device *dev = crtc->base.dev;
3716 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3717 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3718
3719 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3720 I915_READ(HTOTAL(cpu_transcoder)));
3721 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3722 I915_READ(HBLANK(cpu_transcoder)));
3723 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3724 I915_READ(HSYNC(cpu_transcoder)));
3725
3726 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3727 I915_READ(VTOTAL(cpu_transcoder)));
3728 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3729 I915_READ(VBLANK(cpu_transcoder)));
3730 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3731 I915_READ(VSYNC(cpu_transcoder)));
3732 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3733 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3734}
3735
1fbc0d78
DV
3736static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3737{
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 uint32_t temp;
3740
3741 temp = I915_READ(SOUTH_CHICKEN1);
3742 if (temp & FDI_BC_BIFURCATION_SELECT)
3743 return;
3744
3745 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3746 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3747
3748 temp |= FDI_BC_BIFURCATION_SELECT;
3749 DRM_DEBUG_KMS("enabling fdi C rx\n");
3750 I915_WRITE(SOUTH_CHICKEN1, temp);
3751 POSTING_READ(SOUTH_CHICKEN1);
3752}
3753
3754static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3755{
3756 struct drm_device *dev = intel_crtc->base.dev;
3757 struct drm_i915_private *dev_priv = dev->dev_private;
3758
3759 switch (intel_crtc->pipe) {
3760 case PIPE_A:
3761 break;
3762 case PIPE_B:
6e3c9717 3763 if (intel_crtc->config->fdi_lanes > 2)
1fbc0d78
DV
3764 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3765 else
3766 cpt_enable_fdi_bc_bifurcation(dev);
3767
3768 break;
3769 case PIPE_C:
3770 cpt_enable_fdi_bc_bifurcation(dev);
3771
3772 break;
3773 default:
3774 BUG();
3775 }
3776}
3777
f67a559d
JB
3778/*
3779 * Enable PCH resources required for PCH ports:
3780 * - PCH PLLs
3781 * - FDI training & RX/TX
3782 * - update transcoder timings
3783 * - DP transcoding bits
3784 * - transcoder
3785 */
3786static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3787{
3788 struct drm_device *dev = crtc->dev;
3789 struct drm_i915_private *dev_priv = dev->dev_private;
3790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3791 int pipe = intel_crtc->pipe;
ee7b9f93 3792 u32 reg, temp;
2c07245f 3793
ab9412ba 3794 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3795
1fbc0d78
DV
3796 if (IS_IVYBRIDGE(dev))
3797 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3798
cd986abb
DV
3799 /* Write the TU size bits before fdi link training, so that error
3800 * detection works. */
3801 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3802 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3803
c98e9dcf 3804 /* For PCH output, training FDI link */
674cf967 3805 dev_priv->display.fdi_link_train(crtc);
2c07245f 3806
3ad8a208
DV
3807 /* We need to program the right clock selection before writing the pixel
3808 * mutliplier into the DPLL. */
303b81e0 3809 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3810 u32 sel;
4b645f14 3811
c98e9dcf 3812 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3813 temp |= TRANS_DPLL_ENABLE(pipe);
3814 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 3815 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3816 temp |= sel;
3817 else
3818 temp &= ~sel;
c98e9dcf 3819 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3820 }
5eddb70b 3821
3ad8a208
DV
3822 /* XXX: pch pll's can be enabled any time before we enable the PCH
3823 * transcoder, and we actually should do this to not upset any PCH
3824 * transcoder that already use the clock when we share it.
3825 *
3826 * Note that enable_shared_dpll tries to do the right thing, but
3827 * get_shared_dpll unconditionally resets the pll - we need that to have
3828 * the right LVDS enable sequence. */
85b3894f 3829 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3830
d9b6cb56
JB
3831 /* set transcoder timing, panel must allow it */
3832 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3833 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3834
303b81e0 3835 intel_fdi_normal_train(crtc);
5e84e1a4 3836
c98e9dcf 3837 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 3838 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 3839 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3840 reg = TRANS_DP_CTL(pipe);
3841 temp = I915_READ(reg);
3842 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3843 TRANS_DP_SYNC_MASK |
3844 TRANS_DP_BPC_MASK);
5eddb70b
CW
3845 temp |= (TRANS_DP_OUTPUT_ENABLE |
3846 TRANS_DP_ENH_FRAMING);
9325c9f0 3847 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3848
3849 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3850 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3851 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3852 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3853
3854 switch (intel_trans_dp_port_sel(crtc)) {
3855 case PCH_DP_B:
5eddb70b 3856 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3857 break;
3858 case PCH_DP_C:
5eddb70b 3859 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3860 break;
3861 case PCH_DP_D:
5eddb70b 3862 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3863 break;
3864 default:
e95d41e1 3865 BUG();
32f9d658 3866 }
2c07245f 3867
5eddb70b 3868 I915_WRITE(reg, temp);
6be4a607 3869 }
b52eb4dc 3870
b8a4f404 3871 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3872}
3873
1507e5bd
PZ
3874static void lpt_pch_enable(struct drm_crtc *crtc)
3875{
3876 struct drm_device *dev = crtc->dev;
3877 struct drm_i915_private *dev_priv = dev->dev_private;
3878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 3879 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 3880
ab9412ba 3881 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3882
8c52b5e8 3883 lpt_program_iclkip(crtc);
1507e5bd 3884
0540e488 3885 /* Set transcoder timing. */
275f01b2 3886 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3887
937bb610 3888 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3889}
3890
716c2e55 3891void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3892{
e2b78267 3893 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3894
3895 if (pll == NULL)
3896 return;
3897
3e369b76 3898 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 3899 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
3900 return;
3901 }
3902
3e369b76
ACO
3903 pll->config.crtc_mask &= ~(1 << crtc->pipe);
3904 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
3905 WARN_ON(pll->on);
3906 WARN_ON(pll->active);
3907 }
3908
6e3c9717 3909 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3910}
3911
190f68c5
ACO
3912struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
3913 struct intel_crtc_state *crtc_state)
ee7b9f93 3914{
e2b78267 3915 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 3916 struct intel_shared_dpll *pll;
e2b78267 3917 enum intel_dpll_id i;
ee7b9f93 3918
98b6bd99
DV
3919 if (HAS_PCH_IBX(dev_priv->dev)) {
3920 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3921 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3922 pll = &dev_priv->shared_dplls[i];
98b6bd99 3923
46edb027
DV
3924 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3925 crtc->base.base.id, pll->name);
98b6bd99 3926
8bd31e67 3927 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 3928
98b6bd99
DV
3929 goto found;
3930 }
3931
e72f9fbf
DV
3932 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3933 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3934
3935 /* Only want to check enabled timings first */
8bd31e67 3936 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
3937 continue;
3938
190f68c5 3939 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
3940 &pll->new_config->hw_state,
3941 sizeof(pll->new_config->hw_state)) == 0) {
3942 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 3943 crtc->base.base.id, pll->name,
8bd31e67
ACO
3944 pll->new_config->crtc_mask,
3945 pll->active);
ee7b9f93
JB
3946 goto found;
3947 }
3948 }
3949
3950 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3951 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3952 pll = &dev_priv->shared_dplls[i];
8bd31e67 3953 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
3954 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3955 crtc->base.base.id, pll->name);
ee7b9f93
JB
3956 goto found;
3957 }
3958 }
3959
3960 return NULL;
3961
3962found:
8bd31e67 3963 if (pll->new_config->crtc_mask == 0)
190f68c5 3964 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 3965
190f68c5 3966 crtc_state->shared_dpll = i;
46edb027
DV
3967 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3968 pipe_name(crtc->pipe));
ee7b9f93 3969
8bd31e67 3970 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 3971
ee7b9f93
JB
3972 return pll;
3973}
3974
8bd31e67
ACO
3975/**
3976 * intel_shared_dpll_start_config - start a new PLL staged config
3977 * @dev_priv: DRM device
3978 * @clear_pipes: mask of pipes that will have their PLLs freed
3979 *
3980 * Starts a new PLL staged config, copying the current config but
3981 * releasing the references of pipes specified in clear_pipes.
3982 */
3983static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
3984 unsigned clear_pipes)
3985{
3986 struct intel_shared_dpll *pll;
3987 enum intel_dpll_id i;
3988
3989 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3990 pll = &dev_priv->shared_dplls[i];
3991
3992 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
3993 GFP_KERNEL);
3994 if (!pll->new_config)
3995 goto cleanup;
3996
3997 pll->new_config->crtc_mask &= ~clear_pipes;
3998 }
3999
4000 return 0;
4001
4002cleanup:
4003 while (--i >= 0) {
4004 pll = &dev_priv->shared_dplls[i];
f354d733 4005 kfree(pll->new_config);
8bd31e67
ACO
4006 pll->new_config = NULL;
4007 }
4008
4009 return -ENOMEM;
4010}
4011
4012static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4013{
4014 struct intel_shared_dpll *pll;
4015 enum intel_dpll_id i;
4016
4017 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4018 pll = &dev_priv->shared_dplls[i];
4019
4020 WARN_ON(pll->new_config == &pll->config);
4021
4022 pll->config = *pll->new_config;
4023 kfree(pll->new_config);
4024 pll->new_config = NULL;
4025 }
4026}
4027
4028static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4029{
4030 struct intel_shared_dpll *pll;
4031 enum intel_dpll_id i;
4032
4033 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4034 pll = &dev_priv->shared_dplls[i];
4035
4036 WARN_ON(pll->new_config == &pll->config);
4037
4038 kfree(pll->new_config);
4039 pll->new_config = NULL;
4040 }
4041}
4042
a1520318 4043static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4044{
4045 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4046 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4047 u32 temp;
4048
4049 temp = I915_READ(dslreg);
4050 udelay(500);
4051 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4052 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4053 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4054 }
4055}
4056
bd2e244f
JB
4057static void skylake_pfit_enable(struct intel_crtc *crtc)
4058{
4059 struct drm_device *dev = crtc->base.dev;
4060 struct drm_i915_private *dev_priv = dev->dev_private;
4061 int pipe = crtc->pipe;
4062
6e3c9717 4063 if (crtc->config->pch_pfit.enabled) {
bd2e244f 4064 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
6e3c9717
ACO
4065 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4066 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
bd2e244f
JB
4067 }
4068}
4069
b074cec8
JB
4070static void ironlake_pfit_enable(struct intel_crtc *crtc)
4071{
4072 struct drm_device *dev = crtc->base.dev;
4073 struct drm_i915_private *dev_priv = dev->dev_private;
4074 int pipe = crtc->pipe;
4075
6e3c9717 4076 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4077 /* Force use of hard-coded filter coefficients
4078 * as some pre-programmed values are broken,
4079 * e.g. x201.
4080 */
4081 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4082 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4083 PF_PIPE_SEL_IVB(pipe));
4084 else
4085 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4086 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4087 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4088 }
4089}
4090
4a3b8769 4091static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4092{
4093 struct drm_device *dev = crtc->dev;
4094 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4095 struct drm_plane *plane;
bb53d4ae
VS
4096 struct intel_plane *intel_plane;
4097
af2b653b
MR
4098 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4099 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4100 if (intel_plane->pipe == pipe)
4101 intel_plane_restore(&intel_plane->base);
af2b653b 4102 }
bb53d4ae
VS
4103}
4104
4a3b8769 4105static void intel_disable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4106{
4107 struct drm_device *dev = crtc->dev;
4108 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4109 struct drm_plane *plane;
bb53d4ae
VS
4110 struct intel_plane *intel_plane;
4111
af2b653b
MR
4112 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4113 intel_plane = to_intel_plane(plane);
bb53d4ae 4114 if (intel_plane->pipe == pipe)
cf4c7c12 4115 plane->funcs->disable_plane(plane);
af2b653b 4116 }
bb53d4ae
VS
4117}
4118
20bc8673 4119void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4120{
cea165c3
VS
4121 struct drm_device *dev = crtc->base.dev;
4122 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4123
6e3c9717 4124 if (!crtc->config->ips_enabled)
d77e4531
PZ
4125 return;
4126
cea165c3
VS
4127 /* We can only enable IPS after we enable a plane and wait for a vblank */
4128 intel_wait_for_vblank(dev, crtc->pipe);
4129
d77e4531 4130 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4131 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4132 mutex_lock(&dev_priv->rps.hw_lock);
4133 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4134 mutex_unlock(&dev_priv->rps.hw_lock);
4135 /* Quoting Art Runyan: "its not safe to expect any particular
4136 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4137 * mailbox." Moreover, the mailbox may return a bogus state,
4138 * so we need to just enable it and continue on.
2a114cc1
BW
4139 */
4140 } else {
4141 I915_WRITE(IPS_CTL, IPS_ENABLE);
4142 /* The bit only becomes 1 in the next vblank, so this wait here
4143 * is essentially intel_wait_for_vblank. If we don't have this
4144 * and don't wait for vblanks until the end of crtc_enable, then
4145 * the HW state readout code will complain that the expected
4146 * IPS_CTL value is not the one we read. */
4147 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4148 DRM_ERROR("Timed out waiting for IPS enable\n");
4149 }
d77e4531
PZ
4150}
4151
20bc8673 4152void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4153{
4154 struct drm_device *dev = crtc->base.dev;
4155 struct drm_i915_private *dev_priv = dev->dev_private;
4156
6e3c9717 4157 if (!crtc->config->ips_enabled)
d77e4531
PZ
4158 return;
4159
4160 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4161 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4162 mutex_lock(&dev_priv->rps.hw_lock);
4163 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4164 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4165 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4166 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4167 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4168 } else {
2a114cc1 4169 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4170 POSTING_READ(IPS_CTL);
4171 }
d77e4531
PZ
4172
4173 /* We need to wait for a vblank before we can disable the plane. */
4174 intel_wait_for_vblank(dev, crtc->pipe);
4175}
4176
4177/** Loads the palette/gamma unit for the CRTC with the prepared values */
4178static void intel_crtc_load_lut(struct drm_crtc *crtc)
4179{
4180 struct drm_device *dev = crtc->dev;
4181 struct drm_i915_private *dev_priv = dev->dev_private;
4182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4183 enum pipe pipe = intel_crtc->pipe;
4184 int palreg = PALETTE(pipe);
4185 int i;
4186 bool reenable_ips = false;
4187
4188 /* The clocks have to be on to load the palette. */
4189 if (!crtc->enabled || !intel_crtc->active)
4190 return;
4191
4192 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
409ee761 4193 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4194 assert_dsi_pll_enabled(dev_priv);
4195 else
4196 assert_pll_enabled(dev_priv, pipe);
4197 }
4198
4199 /* use legacy palette for Ironlake */
7a1db49a 4200 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4201 palreg = LGC_PALETTE(pipe);
4202
4203 /* Workaround : Do not read or write the pipe palette/gamma data while
4204 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4205 */
6e3c9717 4206 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4207 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4208 GAMMA_MODE_MODE_SPLIT)) {
4209 hsw_disable_ips(intel_crtc);
4210 reenable_ips = true;
4211 }
4212
4213 for (i = 0; i < 256; i++) {
4214 I915_WRITE(palreg + 4 * i,
4215 (intel_crtc->lut_r[i] << 16) |
4216 (intel_crtc->lut_g[i] << 8) |
4217 intel_crtc->lut_b[i]);
4218 }
4219
4220 if (reenable_ips)
4221 hsw_enable_ips(intel_crtc);
4222}
4223
d3eedb1a
VS
4224static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4225{
4226 if (!enable && intel_crtc->overlay) {
4227 struct drm_device *dev = intel_crtc->base.dev;
4228 struct drm_i915_private *dev_priv = dev->dev_private;
4229
4230 mutex_lock(&dev->struct_mutex);
4231 dev_priv->mm.interruptible = false;
4232 (void) intel_overlay_switch_off(intel_crtc->overlay);
4233 dev_priv->mm.interruptible = true;
4234 mutex_unlock(&dev->struct_mutex);
4235 }
4236
4237 /* Let userspace switch the overlay on again. In most cases userspace
4238 * has to recompute where to put it anyway.
4239 */
4240}
4241
d3eedb1a 4242static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4243{
4244 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4246 int pipe = intel_crtc->pipe;
a5c4d7bc 4247
fdd508a6 4248 intel_enable_primary_hw_plane(crtc->primary, crtc);
4a3b8769 4249 intel_enable_sprite_planes(crtc);
a5c4d7bc 4250 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4251 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4252
4253 hsw_enable_ips(intel_crtc);
4254
4255 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4256 intel_fbc_update(dev);
a5c4d7bc 4257 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4258
4259 /*
4260 * FIXME: Once we grow proper nuclear flip support out of this we need
4261 * to compute the mask of flip planes precisely. For the time being
4262 * consider this a flip from a NULL plane.
4263 */
4264 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4265}
4266
d3eedb1a 4267static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4268{
4269 struct drm_device *dev = crtc->dev;
4270 struct drm_i915_private *dev_priv = dev->dev_private;
4271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4272 int pipe = intel_crtc->pipe;
4273 int plane = intel_crtc->plane;
4274
4275 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4276
4277 if (dev_priv->fbc.plane == plane)
7ff0ebcc 4278 intel_fbc_disable(dev);
a5c4d7bc
VS
4279
4280 hsw_disable_ips(intel_crtc);
4281
d3eedb1a 4282 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc 4283 intel_crtc_update_cursor(crtc, false);
4a3b8769 4284 intel_disable_sprite_planes(crtc);
fdd508a6 4285 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4286
f99d7069
DV
4287 /*
4288 * FIXME: Once we grow proper nuclear flip support out of this we need
4289 * to compute the mask of flip planes precisely. For the time being
4290 * consider this a flip to a NULL plane.
4291 */
4292 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4293}
4294
f67a559d
JB
4295static void ironlake_crtc_enable(struct drm_crtc *crtc)
4296{
4297 struct drm_device *dev = crtc->dev;
4298 struct drm_i915_private *dev_priv = dev->dev_private;
4299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4300 struct intel_encoder *encoder;
f67a559d 4301 int pipe = intel_crtc->pipe;
f67a559d 4302
08a48469
DV
4303 WARN_ON(!crtc->enabled);
4304
f67a559d
JB
4305 if (intel_crtc->active)
4306 return;
4307
6e3c9717 4308 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4309 intel_prepare_shared_dpll(intel_crtc);
4310
6e3c9717 4311 if (intel_crtc->config->has_dp_encoder)
29407aab
DV
4312 intel_dp_set_m_n(intel_crtc);
4313
4314 intel_set_pipe_timings(intel_crtc);
4315
6e3c9717 4316 if (intel_crtc->config->has_pch_encoder) {
29407aab 4317 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4318 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4319 }
4320
4321 ironlake_set_pipeconf(crtc);
4322
f67a559d 4323 intel_crtc->active = true;
8664281b 4324
a72e4c9f
DV
4325 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4326 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4327
f6736a1a 4328 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4329 if (encoder->pre_enable)
4330 encoder->pre_enable(encoder);
f67a559d 4331
6e3c9717 4332 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4333 /* Note: FDI PLL enabling _must_ be done before we enable the
4334 * cpu pipes, hence this is separate from all the other fdi/pch
4335 * enabling. */
88cefb6c 4336 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4337 } else {
4338 assert_fdi_tx_disabled(dev_priv, pipe);
4339 assert_fdi_rx_disabled(dev_priv, pipe);
4340 }
f67a559d 4341
b074cec8 4342 ironlake_pfit_enable(intel_crtc);
f67a559d 4343
9c54c0dd
JB
4344 /*
4345 * On ILK+ LUT must be loaded before the pipe is running but with
4346 * clocks enabled
4347 */
4348 intel_crtc_load_lut(crtc);
4349
f37fcc2a 4350 intel_update_watermarks(crtc);
e1fdc473 4351 intel_enable_pipe(intel_crtc);
f67a559d 4352
6e3c9717 4353 if (intel_crtc->config->has_pch_encoder)
f67a559d 4354 ironlake_pch_enable(crtc);
c98e9dcf 4355
f9b61ff6
DV
4356 assert_vblank_disabled(crtc);
4357 drm_crtc_vblank_on(crtc);
4358
fa5c73b1
DV
4359 for_each_encoder_on_crtc(dev, crtc, encoder)
4360 encoder->enable(encoder);
61b77ddd
DV
4361
4362 if (HAS_PCH_CPT(dev))
a1520318 4363 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4364
d3eedb1a 4365 intel_crtc_enable_planes(crtc);
6be4a607
JB
4366}
4367
42db64ef
PZ
4368/* IPS only exists on ULT machines and is tied to pipe A. */
4369static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4370{
f5adf94e 4371 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4372}
4373
e4916946
PZ
4374/*
4375 * This implements the workaround described in the "notes" section of the mode
4376 * set sequence documentation. When going from no pipes or single pipe to
4377 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4378 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4379 */
4380static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4381{
4382 struct drm_device *dev = crtc->base.dev;
4383 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4384
4385 /* We want to get the other_active_crtc only if there's only 1 other
4386 * active crtc. */
d3fcc808 4387 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4388 if (!crtc_it->active || crtc_it == crtc)
4389 continue;
4390
4391 if (other_active_crtc)
4392 return;
4393
4394 other_active_crtc = crtc_it;
4395 }
4396 if (!other_active_crtc)
4397 return;
4398
4399 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4400 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4401}
4402
4f771f10
PZ
4403static void haswell_crtc_enable(struct drm_crtc *crtc)
4404{
4405 struct drm_device *dev = crtc->dev;
4406 struct drm_i915_private *dev_priv = dev->dev_private;
4407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4408 struct intel_encoder *encoder;
4409 int pipe = intel_crtc->pipe;
4f771f10
PZ
4410
4411 WARN_ON(!crtc->enabled);
4412
4413 if (intel_crtc->active)
4414 return;
4415
df8ad70c
DV
4416 if (intel_crtc_to_shared_dpll(intel_crtc))
4417 intel_enable_shared_dpll(intel_crtc);
4418
6e3c9717 4419 if (intel_crtc->config->has_dp_encoder)
229fca97
DV
4420 intel_dp_set_m_n(intel_crtc);
4421
4422 intel_set_pipe_timings(intel_crtc);
4423
6e3c9717
ACO
4424 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4425 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4426 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4427 }
4428
6e3c9717 4429 if (intel_crtc->config->has_pch_encoder) {
229fca97 4430 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4431 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4432 }
4433
4434 haswell_set_pipeconf(crtc);
4435
4436 intel_set_pipe_csc(crtc);
4437
4f771f10 4438 intel_crtc->active = true;
8664281b 4439
a72e4c9f 4440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4441 for_each_encoder_on_crtc(dev, crtc, encoder)
4442 if (encoder->pre_enable)
4443 encoder->pre_enable(encoder);
4444
6e3c9717 4445 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4446 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4447 true);
4fe9467d
ID
4448 dev_priv->display.fdi_link_train(crtc);
4449 }
4450
1f544388 4451 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4452
bd2e244f
JB
4453 if (IS_SKYLAKE(dev))
4454 skylake_pfit_enable(intel_crtc);
4455 else
4456 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4457
4458 /*
4459 * On ILK+ LUT must be loaded before the pipe is running but with
4460 * clocks enabled
4461 */
4462 intel_crtc_load_lut(crtc);
4463
1f544388 4464 intel_ddi_set_pipe_settings(crtc);
8228c251 4465 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4466
f37fcc2a 4467 intel_update_watermarks(crtc);
e1fdc473 4468 intel_enable_pipe(intel_crtc);
42db64ef 4469
6e3c9717 4470 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4471 lpt_pch_enable(crtc);
4f771f10 4472
6e3c9717 4473 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4474 intel_ddi_set_vc_payload_alloc(crtc, true);
4475
f9b61ff6
DV
4476 assert_vblank_disabled(crtc);
4477 drm_crtc_vblank_on(crtc);
4478
8807e55b 4479 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4480 encoder->enable(encoder);
8807e55b
JN
4481 intel_opregion_notify_encoder(encoder, true);
4482 }
4f771f10 4483
e4916946
PZ
4484 /* If we change the relative order between pipe/planes enabling, we need
4485 * to change the workaround. */
4486 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4487 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4488}
4489
bd2e244f
JB
4490static void skylake_pfit_disable(struct intel_crtc *crtc)
4491{
4492 struct drm_device *dev = crtc->base.dev;
4493 struct drm_i915_private *dev_priv = dev->dev_private;
4494 int pipe = crtc->pipe;
4495
4496 /* To avoid upsetting the power well on haswell only disable the pfit if
4497 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4498 if (crtc->config->pch_pfit.enabled) {
bd2e244f
JB
4499 I915_WRITE(PS_CTL(pipe), 0);
4500 I915_WRITE(PS_WIN_POS(pipe), 0);
4501 I915_WRITE(PS_WIN_SZ(pipe), 0);
4502 }
4503}
4504
3f8dce3a
DV
4505static void ironlake_pfit_disable(struct intel_crtc *crtc)
4506{
4507 struct drm_device *dev = crtc->base.dev;
4508 struct drm_i915_private *dev_priv = dev->dev_private;
4509 int pipe = crtc->pipe;
4510
4511 /* To avoid upsetting the power well on haswell only disable the pfit if
4512 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4513 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4514 I915_WRITE(PF_CTL(pipe), 0);
4515 I915_WRITE(PF_WIN_POS(pipe), 0);
4516 I915_WRITE(PF_WIN_SZ(pipe), 0);
4517 }
4518}
4519
6be4a607
JB
4520static void ironlake_crtc_disable(struct drm_crtc *crtc)
4521{
4522 struct drm_device *dev = crtc->dev;
4523 struct drm_i915_private *dev_priv = dev->dev_private;
4524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4525 struct intel_encoder *encoder;
6be4a607 4526 int pipe = intel_crtc->pipe;
5eddb70b 4527 u32 reg, temp;
b52eb4dc 4528
f7abfe8b
CW
4529 if (!intel_crtc->active)
4530 return;
4531
d3eedb1a 4532 intel_crtc_disable_planes(crtc);
a5c4d7bc 4533
ea9d758d
DV
4534 for_each_encoder_on_crtc(dev, crtc, encoder)
4535 encoder->disable(encoder);
4536
f9b61ff6
DV
4537 drm_crtc_vblank_off(crtc);
4538 assert_vblank_disabled(crtc);
4539
6e3c9717 4540 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 4541 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4542
575f7ab7 4543 intel_disable_pipe(intel_crtc);
32f9d658 4544
3f8dce3a 4545 ironlake_pfit_disable(intel_crtc);
2c07245f 4546
bf49ec8c
DV
4547 for_each_encoder_on_crtc(dev, crtc, encoder)
4548 if (encoder->post_disable)
4549 encoder->post_disable(encoder);
2c07245f 4550
6e3c9717 4551 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4552 ironlake_fdi_disable(crtc);
913d8d11 4553
d925c59a 4554 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4555
d925c59a
DV
4556 if (HAS_PCH_CPT(dev)) {
4557 /* disable TRANS_DP_CTL */
4558 reg = TRANS_DP_CTL(pipe);
4559 temp = I915_READ(reg);
4560 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4561 TRANS_DP_PORT_SEL_MASK);
4562 temp |= TRANS_DP_PORT_SEL_NONE;
4563 I915_WRITE(reg, temp);
4564
4565 /* disable DPLL_SEL */
4566 temp = I915_READ(PCH_DPLL_SEL);
11887397 4567 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4568 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4569 }
e3421a18 4570
d925c59a 4571 /* disable PCH DPLL */
e72f9fbf 4572 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4573
d925c59a
DV
4574 ironlake_fdi_pll_disable(intel_crtc);
4575 }
6b383a7f 4576
f7abfe8b 4577 intel_crtc->active = false;
46ba614c 4578 intel_update_watermarks(crtc);
d1ebd816
BW
4579
4580 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4581 intel_fbc_update(dev);
d1ebd816 4582 mutex_unlock(&dev->struct_mutex);
6be4a607 4583}
1b3c7a47 4584
4f771f10 4585static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4586{
4f771f10
PZ
4587 struct drm_device *dev = crtc->dev;
4588 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4590 struct intel_encoder *encoder;
6e3c9717 4591 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 4592
4f771f10
PZ
4593 if (!intel_crtc->active)
4594 return;
4595
d3eedb1a 4596 intel_crtc_disable_planes(crtc);
dda9a66a 4597
8807e55b
JN
4598 for_each_encoder_on_crtc(dev, crtc, encoder) {
4599 intel_opregion_notify_encoder(encoder, false);
4f771f10 4600 encoder->disable(encoder);
8807e55b 4601 }
4f771f10 4602
f9b61ff6
DV
4603 drm_crtc_vblank_off(crtc);
4604 assert_vblank_disabled(crtc);
4605
6e3c9717 4606 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
4607 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4608 false);
575f7ab7 4609 intel_disable_pipe(intel_crtc);
4f771f10 4610
6e3c9717 4611 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
4612 intel_ddi_set_vc_payload_alloc(crtc, false);
4613
ad80a810 4614 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4615
bd2e244f
JB
4616 if (IS_SKYLAKE(dev))
4617 skylake_pfit_disable(intel_crtc);
4618 else
4619 ironlake_pfit_disable(intel_crtc);
4f771f10 4620
1f544388 4621 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4622
6e3c9717 4623 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 4624 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 4625 intel_ddi_fdi_disable(crtc);
83616634 4626 }
4f771f10 4627
97b040aa
ID
4628 for_each_encoder_on_crtc(dev, crtc, encoder)
4629 if (encoder->post_disable)
4630 encoder->post_disable(encoder);
4631
4f771f10 4632 intel_crtc->active = false;
46ba614c 4633 intel_update_watermarks(crtc);
4f771f10
PZ
4634
4635 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4636 intel_fbc_update(dev);
4f771f10 4637 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4638
4639 if (intel_crtc_to_shared_dpll(intel_crtc))
4640 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4641}
4642
ee7b9f93
JB
4643static void ironlake_crtc_off(struct drm_crtc *crtc)
4644{
4645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4646 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4647}
4648
6441ab5f 4649
2dd24552
JB
4650static void i9xx_pfit_enable(struct intel_crtc *crtc)
4651{
4652 struct drm_device *dev = crtc->base.dev;
4653 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4654 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 4655
681a8504 4656 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
4657 return;
4658
2dd24552 4659 /*
c0b03411
DV
4660 * The panel fitter should only be adjusted whilst the pipe is disabled,
4661 * according to register description and PRM.
2dd24552 4662 */
c0b03411
DV
4663 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4664 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4665
b074cec8
JB
4666 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4667 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4668
4669 /* Border color in case we don't scale up to the full screen. Black by
4670 * default, change to something else for debugging. */
4671 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4672}
4673
d05410f9
DA
4674static enum intel_display_power_domain port_to_power_domain(enum port port)
4675{
4676 switch (port) {
4677 case PORT_A:
4678 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4679 case PORT_B:
4680 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4681 case PORT_C:
4682 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4683 case PORT_D:
4684 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4685 default:
4686 WARN_ON_ONCE(1);
4687 return POWER_DOMAIN_PORT_OTHER;
4688 }
4689}
4690
77d22dca
ID
4691#define for_each_power_domain(domain, mask) \
4692 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4693 if ((1 << (domain)) & (mask))
4694
319be8ae
ID
4695enum intel_display_power_domain
4696intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4697{
4698 struct drm_device *dev = intel_encoder->base.dev;
4699 struct intel_digital_port *intel_dig_port;
4700
4701 switch (intel_encoder->type) {
4702 case INTEL_OUTPUT_UNKNOWN:
4703 /* Only DDI platforms should ever use this output type */
4704 WARN_ON_ONCE(!HAS_DDI(dev));
4705 case INTEL_OUTPUT_DISPLAYPORT:
4706 case INTEL_OUTPUT_HDMI:
4707 case INTEL_OUTPUT_EDP:
4708 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4709 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4710 case INTEL_OUTPUT_DP_MST:
4711 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4712 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4713 case INTEL_OUTPUT_ANALOG:
4714 return POWER_DOMAIN_PORT_CRT;
4715 case INTEL_OUTPUT_DSI:
4716 return POWER_DOMAIN_PORT_DSI;
4717 default:
4718 return POWER_DOMAIN_PORT_OTHER;
4719 }
4720}
4721
4722static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4723{
319be8ae
ID
4724 struct drm_device *dev = crtc->dev;
4725 struct intel_encoder *intel_encoder;
4726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4727 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4728 unsigned long mask;
4729 enum transcoder transcoder;
4730
4731 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4732
4733 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4734 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
4735 if (intel_crtc->config->pch_pfit.enabled ||
4736 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
4737 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4738
319be8ae
ID
4739 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4740 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4741
77d22dca
ID
4742 return mask;
4743}
4744
77d22dca
ID
4745static void modeset_update_crtc_power_domains(struct drm_device *dev)
4746{
4747 struct drm_i915_private *dev_priv = dev->dev_private;
4748 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4749 struct intel_crtc *crtc;
4750
4751 /*
4752 * First get all needed power domains, then put all unneeded, to avoid
4753 * any unnecessary toggling of the power wells.
4754 */
d3fcc808 4755 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4756 enum intel_display_power_domain domain;
4757
4758 if (!crtc->base.enabled)
4759 continue;
4760
319be8ae 4761 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4762
4763 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4764 intel_display_power_get(dev_priv, domain);
4765 }
4766
50f6e502
VS
4767 if (dev_priv->display.modeset_global_resources)
4768 dev_priv->display.modeset_global_resources(dev);
4769
d3fcc808 4770 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4771 enum intel_display_power_domain domain;
4772
4773 for_each_power_domain(domain, crtc->enabled_power_domains)
4774 intel_display_power_put(dev_priv, domain);
4775
4776 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4777 }
4778
4779 intel_display_set_init_power(dev_priv, false);
4780}
4781
dfcab17e 4782/* returns HPLL frequency in kHz */
f8bf63fd 4783static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4784{
586f49dc 4785 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4786
586f49dc
JB
4787 /* Obtain SKU information */
4788 mutex_lock(&dev_priv->dpio_lock);
4789 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4790 CCK_FUSE_HPLL_FREQ_MASK;
4791 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4792
dfcab17e 4793 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4794}
4795
f8bf63fd
VS
4796static void vlv_update_cdclk(struct drm_device *dev)
4797{
4798 struct drm_i915_private *dev_priv = dev->dev_private;
4799
4800 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 4801 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
f8bf63fd
VS
4802 dev_priv->vlv_cdclk_freq);
4803
4804 /*
4805 * Program the gmbus_freq based on the cdclk frequency.
4806 * BSpec erroneously claims we should aim for 4MHz, but
4807 * in fact 1MHz is the correct frequency.
4808 */
6be1e3d3 4809 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
f8bf63fd
VS
4810}
4811
30a970c6
JB
4812/* Adjust CDclk dividers to allow high res or save power if possible */
4813static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4814{
4815 struct drm_i915_private *dev_priv = dev->dev_private;
4816 u32 val, cmd;
4817
d197b7d3 4818 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4819
dfcab17e 4820 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4821 cmd = 2;
dfcab17e 4822 else if (cdclk == 266667)
30a970c6
JB
4823 cmd = 1;
4824 else
4825 cmd = 0;
4826
4827 mutex_lock(&dev_priv->rps.hw_lock);
4828 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4829 val &= ~DSPFREQGUAR_MASK;
4830 val |= (cmd << DSPFREQGUAR_SHIFT);
4831 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4832 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4833 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4834 50)) {
4835 DRM_ERROR("timed out waiting for CDclk change\n");
4836 }
4837 mutex_unlock(&dev_priv->rps.hw_lock);
4838
dfcab17e 4839 if (cdclk == 400000) {
6bcda4f0 4840 u32 divider;
30a970c6 4841
6bcda4f0 4842 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
4843
4844 mutex_lock(&dev_priv->dpio_lock);
4845 /* adjust cdclk divider */
4846 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4847 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4848 val |= divider;
4849 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4850
4851 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4852 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4853 50))
4854 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4855 mutex_unlock(&dev_priv->dpio_lock);
4856 }
4857
4858 mutex_lock(&dev_priv->dpio_lock);
4859 /* adjust self-refresh exit latency value */
4860 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4861 val &= ~0x7f;
4862
4863 /*
4864 * For high bandwidth configs, we set a higher latency in the bunit
4865 * so that the core display fetch happens in time to avoid underruns.
4866 */
dfcab17e 4867 if (cdclk == 400000)
30a970c6
JB
4868 val |= 4500 / 250; /* 4.5 usec */
4869 else
4870 val |= 3000 / 250; /* 3.0 usec */
4871 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4872 mutex_unlock(&dev_priv->dpio_lock);
4873
f8bf63fd 4874 vlv_update_cdclk(dev);
30a970c6
JB
4875}
4876
383c5a6a
VS
4877static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4878{
4879 struct drm_i915_private *dev_priv = dev->dev_private;
4880 u32 val, cmd;
4881
4882 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4883
4884 switch (cdclk) {
4885 case 400000:
4886 cmd = 3;
4887 break;
4888 case 333333:
4889 case 320000:
4890 cmd = 2;
4891 break;
4892 case 266667:
4893 cmd = 1;
4894 break;
4895 case 200000:
4896 cmd = 0;
4897 break;
4898 default:
5f77eeb0 4899 MISSING_CASE(cdclk);
383c5a6a
VS
4900 return;
4901 }
4902
4903 mutex_lock(&dev_priv->rps.hw_lock);
4904 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4905 val &= ~DSPFREQGUAR_MASK_CHV;
4906 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4907 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4908 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4909 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4910 50)) {
4911 DRM_ERROR("timed out waiting for CDclk change\n");
4912 }
4913 mutex_unlock(&dev_priv->rps.hw_lock);
4914
4915 vlv_update_cdclk(dev);
4916}
4917
30a970c6
JB
4918static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4919 int max_pixclk)
4920{
6bcda4f0 4921 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
29dc7ef3 4922
d49a340d
VS
4923 /* FIXME: Punit isn't quite ready yet */
4924 if (IS_CHERRYVIEW(dev_priv->dev))
4925 return 400000;
4926
30a970c6
JB
4927 /*
4928 * Really only a few cases to deal with, as only 4 CDclks are supported:
4929 * 200MHz
4930 * 267MHz
29dc7ef3 4931 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4932 * 400MHz
4933 * So we check to see whether we're above 90% of the lower bin and
4934 * adjust if needed.
e37c67a1
VS
4935 *
4936 * We seem to get an unstable or solid color picture at 200MHz.
4937 * Not sure what's wrong. For now use 200MHz only when all pipes
4938 * are off.
30a970c6 4939 */
29dc7ef3 4940 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4941 return 400000;
4942 else if (max_pixclk > 266667*9/10)
29dc7ef3 4943 return freq_320;
e37c67a1 4944 else if (max_pixclk > 0)
dfcab17e 4945 return 266667;
e37c67a1
VS
4946 else
4947 return 200000;
30a970c6
JB
4948}
4949
2f2d7aa1
VS
4950/* compute the max pixel clock for new configuration */
4951static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4952{
4953 struct drm_device *dev = dev_priv->dev;
4954 struct intel_crtc *intel_crtc;
4955 int max_pixclk = 0;
4956
d3fcc808 4957 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4958 if (intel_crtc->new_enabled)
30a970c6 4959 max_pixclk = max(max_pixclk,
2d112de7 4960 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
30a970c6
JB
4961 }
4962
4963 return max_pixclk;
4964}
4965
4966static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4967 unsigned *prepare_pipes)
30a970c6
JB
4968{
4969 struct drm_i915_private *dev_priv = dev->dev_private;
4970 struct intel_crtc *intel_crtc;
2f2d7aa1 4971 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4972
d60c4473
ID
4973 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4974 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4975 return;
4976
2f2d7aa1 4977 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4978 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4979 if (intel_crtc->base.enabled)
4980 *prepare_pipes |= (1 << intel_crtc->pipe);
4981}
4982
4983static void valleyview_modeset_global_resources(struct drm_device *dev)
4984{
4985 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4986 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4987 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4988
383c5a6a 4989 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
738c05c0
ID
4990 /*
4991 * FIXME: We can end up here with all power domains off, yet
4992 * with a CDCLK frequency other than the minimum. To account
4993 * for this take the PIPE-A power domain, which covers the HW
4994 * blocks needed for the following programming. This can be
4995 * removed once it's guaranteed that we get here either with
4996 * the minimum CDCLK set, or the required power domains
4997 * enabled.
4998 */
4999 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5000
383c5a6a
VS
5001 if (IS_CHERRYVIEW(dev))
5002 cherryview_set_cdclk(dev, req_cdclk);
5003 else
5004 valleyview_set_cdclk(dev, req_cdclk);
738c05c0
ID
5005
5006 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5007 }
30a970c6
JB
5008}
5009
89b667f8
JB
5010static void valleyview_crtc_enable(struct drm_crtc *crtc)
5011{
5012 struct drm_device *dev = crtc->dev;
a72e4c9f 5013 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5015 struct intel_encoder *encoder;
5016 int pipe = intel_crtc->pipe;
23538ef1 5017 bool is_dsi;
89b667f8
JB
5018
5019 WARN_ON(!crtc->enabled);
5020
5021 if (intel_crtc->active)
5022 return;
5023
409ee761 5024 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5025
1ae0d137
VS
5026 if (!is_dsi) {
5027 if (IS_CHERRYVIEW(dev))
6e3c9717 5028 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5029 else
6e3c9717 5030 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5031 }
5b18e57c 5032
6e3c9717 5033 if (intel_crtc->config->has_dp_encoder)
5b18e57c
DV
5034 intel_dp_set_m_n(intel_crtc);
5035
5036 intel_set_pipe_timings(intel_crtc);
5037
c14b0485
VS
5038 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5039 struct drm_i915_private *dev_priv = dev->dev_private;
5040
5041 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5042 I915_WRITE(CHV_CANVAS(pipe), 0);
5043 }
5044
5b18e57c
DV
5045 i9xx_set_pipeconf(intel_crtc);
5046
89b667f8 5047 intel_crtc->active = true;
89b667f8 5048
a72e4c9f 5049 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5050
89b667f8
JB
5051 for_each_encoder_on_crtc(dev, crtc, encoder)
5052 if (encoder->pre_pll_enable)
5053 encoder->pre_pll_enable(encoder);
5054
9d556c99
CML
5055 if (!is_dsi) {
5056 if (IS_CHERRYVIEW(dev))
6e3c9717 5057 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5058 else
6e3c9717 5059 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5060 }
89b667f8
JB
5061
5062 for_each_encoder_on_crtc(dev, crtc, encoder)
5063 if (encoder->pre_enable)
5064 encoder->pre_enable(encoder);
5065
2dd24552
JB
5066 i9xx_pfit_enable(intel_crtc);
5067
63cbb074
VS
5068 intel_crtc_load_lut(crtc);
5069
f37fcc2a 5070 intel_update_watermarks(crtc);
e1fdc473 5071 intel_enable_pipe(intel_crtc);
be6a6f8e 5072
4b3a9526
VS
5073 assert_vblank_disabled(crtc);
5074 drm_crtc_vblank_on(crtc);
5075
f9b61ff6
DV
5076 for_each_encoder_on_crtc(dev, crtc, encoder)
5077 encoder->enable(encoder);
5078
9ab0460b 5079 intel_crtc_enable_planes(crtc);
d40d9187 5080
56b80e1f 5081 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5082 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
5083}
5084
f13c2ef3
DV
5085static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5086{
5087 struct drm_device *dev = crtc->base.dev;
5088 struct drm_i915_private *dev_priv = dev->dev_private;
5089
6e3c9717
ACO
5090 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5091 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5092}
5093
0b8765c6 5094static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5095{
5096 struct drm_device *dev = crtc->dev;
a72e4c9f 5097 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5099 struct intel_encoder *encoder;
79e53945 5100 int pipe = intel_crtc->pipe;
79e53945 5101
08a48469
DV
5102 WARN_ON(!crtc->enabled);
5103
f7abfe8b
CW
5104 if (intel_crtc->active)
5105 return;
5106
f13c2ef3
DV
5107 i9xx_set_pll_dividers(intel_crtc);
5108
6e3c9717 5109 if (intel_crtc->config->has_dp_encoder)
5b18e57c
DV
5110 intel_dp_set_m_n(intel_crtc);
5111
5112 intel_set_pipe_timings(intel_crtc);
5113
5b18e57c
DV
5114 i9xx_set_pipeconf(intel_crtc);
5115
f7abfe8b 5116 intel_crtc->active = true;
6b383a7f 5117
4a3436e8 5118 if (!IS_GEN2(dev))
a72e4c9f 5119 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5120
9d6d9f19
MK
5121 for_each_encoder_on_crtc(dev, crtc, encoder)
5122 if (encoder->pre_enable)
5123 encoder->pre_enable(encoder);
5124
f6736a1a
DV
5125 i9xx_enable_pll(intel_crtc);
5126
2dd24552
JB
5127 i9xx_pfit_enable(intel_crtc);
5128
63cbb074
VS
5129 intel_crtc_load_lut(crtc);
5130
f37fcc2a 5131 intel_update_watermarks(crtc);
e1fdc473 5132 intel_enable_pipe(intel_crtc);
be6a6f8e 5133
4b3a9526
VS
5134 assert_vblank_disabled(crtc);
5135 drm_crtc_vblank_on(crtc);
5136
f9b61ff6
DV
5137 for_each_encoder_on_crtc(dev, crtc, encoder)
5138 encoder->enable(encoder);
5139
9ab0460b 5140 intel_crtc_enable_planes(crtc);
d40d9187 5141
4a3436e8
VS
5142 /*
5143 * Gen2 reports pipe underruns whenever all planes are disabled.
5144 * So don't enable underrun reporting before at least some planes
5145 * are enabled.
5146 * FIXME: Need to fix the logic to work when we turn off all planes
5147 * but leave the pipe running.
5148 */
5149 if (IS_GEN2(dev))
a72e4c9f 5150 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5151
56b80e1f 5152 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5153 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 5154}
79e53945 5155
87476d63
DV
5156static void i9xx_pfit_disable(struct intel_crtc *crtc)
5157{
5158 struct drm_device *dev = crtc->base.dev;
5159 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5160
6e3c9717 5161 if (!crtc->config->gmch_pfit.control)
328d8e82 5162 return;
87476d63 5163
328d8e82 5164 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5165
328d8e82
DV
5166 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5167 I915_READ(PFIT_CONTROL));
5168 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5169}
5170
0b8765c6
JB
5171static void i9xx_crtc_disable(struct drm_crtc *crtc)
5172{
5173 struct drm_device *dev = crtc->dev;
5174 struct drm_i915_private *dev_priv = dev->dev_private;
5175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5176 struct intel_encoder *encoder;
0b8765c6 5177 int pipe = intel_crtc->pipe;
ef9c3aee 5178
f7abfe8b
CW
5179 if (!intel_crtc->active)
5180 return;
5181
4a3436e8
VS
5182 /*
5183 * Gen2 reports pipe underruns whenever all planes are disabled.
5184 * So diasble underrun reporting before all the planes get disabled.
5185 * FIXME: Need to fix the logic to work when we turn off all planes
5186 * but leave the pipe running.
5187 */
5188 if (IS_GEN2(dev))
a72e4c9f 5189 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5190
564ed191
ID
5191 /*
5192 * Vblank time updates from the shadow to live plane control register
5193 * are blocked if the memory self-refresh mode is active at that
5194 * moment. So to make sure the plane gets truly disabled, disable
5195 * first the self-refresh mode. The self-refresh enable bit in turn
5196 * will be checked/applied by the HW only at the next frame start
5197 * event which is after the vblank start event, so we need to have a
5198 * wait-for-vblank between disabling the plane and the pipe.
5199 */
5200 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
5201 intel_crtc_disable_planes(crtc);
5202
6304cd91
VS
5203 /*
5204 * On gen2 planes are double buffered but the pipe isn't, so we must
5205 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5206 * We also need to wait on all gmch platforms because of the
5207 * self-refresh mode constraint explained above.
6304cd91 5208 */
564ed191 5209 intel_wait_for_vblank(dev, pipe);
6304cd91 5210
4b3a9526
VS
5211 for_each_encoder_on_crtc(dev, crtc, encoder)
5212 encoder->disable(encoder);
5213
f9b61ff6
DV
5214 drm_crtc_vblank_off(crtc);
5215 assert_vblank_disabled(crtc);
5216
575f7ab7 5217 intel_disable_pipe(intel_crtc);
24a1f16d 5218
87476d63 5219 i9xx_pfit_disable(intel_crtc);
24a1f16d 5220
89b667f8
JB
5221 for_each_encoder_on_crtc(dev, crtc, encoder)
5222 if (encoder->post_disable)
5223 encoder->post_disable(encoder);
5224
409ee761 5225 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
5226 if (IS_CHERRYVIEW(dev))
5227 chv_disable_pll(dev_priv, pipe);
5228 else if (IS_VALLEYVIEW(dev))
5229 vlv_disable_pll(dev_priv, pipe);
5230 else
1c4e0274 5231 i9xx_disable_pll(intel_crtc);
076ed3b2 5232 }
0b8765c6 5233
4a3436e8 5234 if (!IS_GEN2(dev))
a72e4c9f 5235 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5236
f7abfe8b 5237 intel_crtc->active = false;
46ba614c 5238 intel_update_watermarks(crtc);
f37fcc2a 5239
efa9624e 5240 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5241 intel_fbc_update(dev);
efa9624e 5242 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5243}
5244
ee7b9f93
JB
5245static void i9xx_crtc_off(struct drm_crtc *crtc)
5246{
5247}
5248
b04c5bd6
BF
5249/* Master function to enable/disable CRTC and corresponding power wells */
5250void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5251{
5252 struct drm_device *dev = crtc->dev;
5253 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5255 enum intel_display_power_domain domain;
5256 unsigned long domains;
976f8a20 5257
0e572fe7
DV
5258 if (enable) {
5259 if (!intel_crtc->active) {
e1e9fb84
DV
5260 domains = get_crtc_power_domains(crtc);
5261 for_each_power_domain(domain, domains)
5262 intel_display_power_get(dev_priv, domain);
5263 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5264
5265 dev_priv->display.crtc_enable(crtc);
5266 }
5267 } else {
5268 if (intel_crtc->active) {
5269 dev_priv->display.crtc_disable(crtc);
5270
e1e9fb84
DV
5271 domains = intel_crtc->enabled_power_domains;
5272 for_each_power_domain(domain, domains)
5273 intel_display_power_put(dev_priv, domain);
5274 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5275 }
5276 }
b04c5bd6
BF
5277}
5278
5279/**
5280 * Sets the power management mode of the pipe and plane.
5281 */
5282void intel_crtc_update_dpms(struct drm_crtc *crtc)
5283{
5284 struct drm_device *dev = crtc->dev;
5285 struct intel_encoder *intel_encoder;
5286 bool enable = false;
5287
5288 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5289 enable |= intel_encoder->connectors_active;
5290
5291 intel_crtc_control(crtc, enable);
976f8a20
DV
5292}
5293
cdd59983
CW
5294static void intel_crtc_disable(struct drm_crtc *crtc)
5295{
cdd59983 5296 struct drm_device *dev = crtc->dev;
976f8a20 5297 struct drm_connector *connector;
ee7b9f93 5298 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 5299
976f8a20
DV
5300 /* crtc should still be enabled when we disable it. */
5301 WARN_ON(!crtc->enabled);
5302
5303 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
5304 dev_priv->display.off(crtc);
5305
455a6808 5306 crtc->primary->funcs->disable_plane(crtc->primary);
976f8a20
DV
5307
5308 /* Update computed state. */
5309 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5310 if (!connector->encoder || !connector->encoder->crtc)
5311 continue;
5312
5313 if (connector->encoder->crtc != crtc)
5314 continue;
5315
5316 connector->dpms = DRM_MODE_DPMS_OFF;
5317 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5318 }
5319}
5320
ea5b213a 5321void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5322{
4ef69c7a 5323 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5324
ea5b213a
CW
5325 drm_encoder_cleanup(encoder);
5326 kfree(intel_encoder);
7e7d76c3
JB
5327}
5328
9237329d 5329/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5330 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5331 * state of the entire output pipe. */
9237329d 5332static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5333{
5ab432ef
DV
5334 if (mode == DRM_MODE_DPMS_ON) {
5335 encoder->connectors_active = true;
5336
b2cabb0e 5337 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5338 } else {
5339 encoder->connectors_active = false;
5340
b2cabb0e 5341 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5342 }
79e53945
JB
5343}
5344
0a91ca29
DV
5345/* Cross check the actual hw state with our own modeset state tracking (and it's
5346 * internal consistency). */
b980514c 5347static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5348{
0a91ca29
DV
5349 if (connector->get_hw_state(connector)) {
5350 struct intel_encoder *encoder = connector->encoder;
5351 struct drm_crtc *crtc;
5352 bool encoder_enabled;
5353 enum pipe pipe;
5354
5355 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5356 connector->base.base.id,
c23cc417 5357 connector->base.name);
0a91ca29 5358
0e32b39c
DA
5359 /* there is no real hw state for MST connectors */
5360 if (connector->mst_port)
5361 return;
5362
e2c719b7 5363 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 5364 "wrong connector dpms state\n");
e2c719b7 5365 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 5366 "active connector not linked to encoder\n");
0a91ca29 5367
36cd7444 5368 if (encoder) {
e2c719b7 5369 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
5370 "encoder->connectors_active not set\n");
5371
5372 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
5373 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5374 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 5375 return;
0a91ca29 5376
36cd7444 5377 crtc = encoder->base.crtc;
0a91ca29 5378
e2c719b7
RC
5379 I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
5380 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5381 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
5382 "encoder active on the wrong pipe\n");
5383 }
0a91ca29 5384 }
79e53945
JB
5385}
5386
5ab432ef
DV
5387/* Even simpler default implementation, if there's really no special case to
5388 * consider. */
5389void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5390{
5ab432ef
DV
5391 /* All the simple cases only support two dpms states. */
5392 if (mode != DRM_MODE_DPMS_ON)
5393 mode = DRM_MODE_DPMS_OFF;
d4270e57 5394
5ab432ef
DV
5395 if (mode == connector->dpms)
5396 return;
5397
5398 connector->dpms = mode;
5399
5400 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5401 if (connector->encoder)
5402 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5403
b980514c 5404 intel_modeset_check_state(connector->dev);
79e53945
JB
5405}
5406
f0947c37
DV
5407/* Simple connector->get_hw_state implementation for encoders that support only
5408 * one connector and no cloning and hence the encoder state determines the state
5409 * of the connector. */
5410bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5411{
24929352 5412 enum pipe pipe = 0;
f0947c37 5413 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5414
f0947c37 5415 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5416}
5417
1857e1da 5418static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 5419 struct intel_crtc_state *pipe_config)
1857e1da
DV
5420{
5421 struct drm_i915_private *dev_priv = dev->dev_private;
5422 struct intel_crtc *pipe_B_crtc =
5423 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5424
5425 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5426 pipe_name(pipe), pipe_config->fdi_lanes);
5427 if (pipe_config->fdi_lanes > 4) {
5428 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5429 pipe_name(pipe), pipe_config->fdi_lanes);
5430 return false;
5431 }
5432
bafb6553 5433 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5434 if (pipe_config->fdi_lanes > 2) {
5435 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5436 pipe_config->fdi_lanes);
5437 return false;
5438 } else {
5439 return true;
5440 }
5441 }
5442
5443 if (INTEL_INFO(dev)->num_pipes == 2)
5444 return true;
5445
5446 /* Ivybridge 3 pipe is really complicated */
5447 switch (pipe) {
5448 case PIPE_A:
5449 return true;
5450 case PIPE_B:
5451 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5452 pipe_config->fdi_lanes > 2) {
5453 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5454 pipe_name(pipe), pipe_config->fdi_lanes);
5455 return false;
5456 }
5457 return true;
5458 case PIPE_C:
1e833f40 5459 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
6e3c9717 5460 pipe_B_crtc->config->fdi_lanes <= 2) {
1857e1da
DV
5461 if (pipe_config->fdi_lanes > 2) {
5462 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5463 pipe_name(pipe), pipe_config->fdi_lanes);
5464 return false;
5465 }
5466 } else {
5467 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5468 return false;
5469 }
5470 return true;
5471 default:
5472 BUG();
5473 }
5474}
5475
e29c22c0
DV
5476#define RETRY 1
5477static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 5478 struct intel_crtc_state *pipe_config)
877d48d5 5479{
1857e1da 5480 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 5481 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
ff9a6750 5482 int lane, link_bw, fdi_dotclock;
e29c22c0 5483 bool setup_ok, needs_recompute = false;
877d48d5 5484
e29c22c0 5485retry:
877d48d5
DV
5486 /* FDI is a binary signal running at ~2.7GHz, encoding
5487 * each output octet as 10 bits. The actual frequency
5488 * is stored as a divider into a 100MHz clock, and the
5489 * mode pixel clock is stored in units of 1KHz.
5490 * Hence the bw of each lane in terms of the mode signal
5491 * is:
5492 */
5493 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5494
241bfc38 5495 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5496
2bd89a07 5497 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5498 pipe_config->pipe_bpp);
5499
5500 pipe_config->fdi_lanes = lane;
5501
2bd89a07 5502 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5503 link_bw, &pipe_config->fdi_m_n);
1857e1da 5504
e29c22c0
DV
5505 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5506 intel_crtc->pipe, pipe_config);
5507 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5508 pipe_config->pipe_bpp -= 2*3;
5509 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5510 pipe_config->pipe_bpp);
5511 needs_recompute = true;
5512 pipe_config->bw_constrained = true;
5513
5514 goto retry;
5515 }
5516
5517 if (needs_recompute)
5518 return RETRY;
5519
5520 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5521}
5522
42db64ef 5523static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 5524 struct intel_crtc_state *pipe_config)
42db64ef 5525{
d330a953 5526 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5527 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5528 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5529}
5530
a43f6e0f 5531static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 5532 struct intel_crtc_state *pipe_config)
79e53945 5533{
a43f6e0f 5534 struct drm_device *dev = crtc->base.dev;
8bd31e67 5535 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 5536 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 5537
ad3a4479 5538 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 5539 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
5540 int clock_limit =
5541 dev_priv->display.get_display_clock_speed(dev);
5542
5543 /*
5544 * Enable pixel doubling when the dot clock
5545 * is > 90% of the (display) core speed.
5546 *
b397c96b
VS
5547 * GDG double wide on either pipe,
5548 * otherwise pipe A only.
cf532bb2 5549 */
b397c96b 5550 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5551 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5552 clock_limit *= 2;
cf532bb2 5553 pipe_config->double_wide = true;
ad3a4479
VS
5554 }
5555
241bfc38 5556 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5557 return -EINVAL;
2c07245f 5558 }
89749350 5559
1d1d0e27
VS
5560 /*
5561 * Pipe horizontal size must be even in:
5562 * - DVO ganged mode
5563 * - LVDS dual channel mode
5564 * - Double wide pipe
5565 */
409ee761 5566 if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
5567 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5568 pipe_config->pipe_src_w &= ~1;
5569
8693a824
DL
5570 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5571 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5572 */
5573 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5574 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5575 return -EINVAL;
44f46b42 5576
bd080ee5 5577 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5578 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5579 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5580 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5581 * for lvds. */
5582 pipe_config->pipe_bpp = 8*3;
5583 }
5584
f5adf94e 5585 if (HAS_IPS(dev))
a43f6e0f
DV
5586 hsw_compute_ips_config(crtc, pipe_config);
5587
877d48d5 5588 if (pipe_config->has_pch_encoder)
a43f6e0f 5589 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5590
e29c22c0 5591 return 0;
79e53945
JB
5592}
5593
25eb05fc
JB
5594static int valleyview_get_display_clock_speed(struct drm_device *dev)
5595{
d197b7d3 5596 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
5597 u32 val;
5598 int divider;
5599
d49a340d
VS
5600 /* FIXME: Punit isn't quite ready yet */
5601 if (IS_CHERRYVIEW(dev))
5602 return 400000;
5603
6bcda4f0
VS
5604 if (dev_priv->hpll_freq == 0)
5605 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5606
d197b7d3
VS
5607 mutex_lock(&dev_priv->dpio_lock);
5608 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5609 mutex_unlock(&dev_priv->dpio_lock);
5610
5611 divider = val & DISPLAY_FREQUENCY_VALUES;
5612
7d007f40
VS
5613 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5614 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5615 "cdclk change in progress\n");
5616
6bcda4f0 5617 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
5618}
5619
e70236a8
JB
5620static int i945_get_display_clock_speed(struct drm_device *dev)
5621{
5622 return 400000;
5623}
79e53945 5624
e70236a8 5625static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5626{
e70236a8
JB
5627 return 333000;
5628}
79e53945 5629
e70236a8
JB
5630static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5631{
5632 return 200000;
5633}
79e53945 5634
257a7ffc
DV
5635static int pnv_get_display_clock_speed(struct drm_device *dev)
5636{
5637 u16 gcfgc = 0;
5638
5639 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5640
5641 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5642 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5643 return 267000;
5644 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5645 return 333000;
5646 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5647 return 444000;
5648 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5649 return 200000;
5650 default:
5651 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5652 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5653 return 133000;
5654 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5655 return 167000;
5656 }
5657}
5658
e70236a8
JB
5659static int i915gm_get_display_clock_speed(struct drm_device *dev)
5660{
5661 u16 gcfgc = 0;
79e53945 5662
e70236a8
JB
5663 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5664
5665 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5666 return 133000;
5667 else {
5668 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5669 case GC_DISPLAY_CLOCK_333_MHZ:
5670 return 333000;
5671 default:
5672 case GC_DISPLAY_CLOCK_190_200_MHZ:
5673 return 190000;
79e53945 5674 }
e70236a8
JB
5675 }
5676}
5677
5678static int i865_get_display_clock_speed(struct drm_device *dev)
5679{
5680 return 266000;
5681}
5682
5683static int i855_get_display_clock_speed(struct drm_device *dev)
5684{
5685 u16 hpllcc = 0;
5686 /* Assume that the hardware is in the high speed state. This
5687 * should be the default.
5688 */
5689 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5690 case GC_CLOCK_133_200:
5691 case GC_CLOCK_100_200:
5692 return 200000;
5693 case GC_CLOCK_166_250:
5694 return 250000;
5695 case GC_CLOCK_100_133:
79e53945 5696 return 133000;
e70236a8 5697 }
79e53945 5698
e70236a8
JB
5699 /* Shouldn't happen */
5700 return 0;
5701}
79e53945 5702
e70236a8
JB
5703static int i830_get_display_clock_speed(struct drm_device *dev)
5704{
5705 return 133000;
79e53945
JB
5706}
5707
2c07245f 5708static void
a65851af 5709intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5710{
a65851af
VS
5711 while (*num > DATA_LINK_M_N_MASK ||
5712 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5713 *num >>= 1;
5714 *den >>= 1;
5715 }
5716}
5717
a65851af
VS
5718static void compute_m_n(unsigned int m, unsigned int n,
5719 uint32_t *ret_m, uint32_t *ret_n)
5720{
5721 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5722 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5723 intel_reduce_m_n_ratio(ret_m, ret_n);
5724}
5725
e69d0bc1
DV
5726void
5727intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5728 int pixel_clock, int link_clock,
5729 struct intel_link_m_n *m_n)
2c07245f 5730{
e69d0bc1 5731 m_n->tu = 64;
a65851af
VS
5732
5733 compute_m_n(bits_per_pixel * pixel_clock,
5734 link_clock * nlanes * 8,
5735 &m_n->gmch_m, &m_n->gmch_n);
5736
5737 compute_m_n(pixel_clock, link_clock,
5738 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5739}
5740
a7615030
CW
5741static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5742{
d330a953
JN
5743 if (i915.panel_use_ssc >= 0)
5744 return i915.panel_use_ssc != 0;
41aa3448 5745 return dev_priv->vbt.lvds_use_ssc
435793df 5746 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5747}
5748
409ee761 5749static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
c65d77d8 5750{
409ee761 5751 struct drm_device *dev = crtc->base.dev;
c65d77d8
JB
5752 struct drm_i915_private *dev_priv = dev->dev_private;
5753 int refclk;
5754
a0c4da24 5755 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5756 refclk = 100000;
d0737e1d 5757 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5758 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5759 refclk = dev_priv->vbt.lvds_ssc_freq;
5760 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5761 } else if (!IS_GEN2(dev)) {
5762 refclk = 96000;
5763 } else {
5764 refclk = 48000;
5765 }
5766
5767 return refclk;
5768}
5769
7429e9d4 5770static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5771{
7df00d7a 5772 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5773}
f47709a9 5774
7429e9d4
DV
5775static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5776{
5777 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5778}
5779
f47709a9 5780static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 5781 struct intel_crtc_state *crtc_state,
a7516a05
JB
5782 intel_clock_t *reduced_clock)
5783{
f47709a9 5784 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5785 u32 fp, fp2 = 0;
5786
5787 if (IS_PINEVIEW(dev)) {
190f68c5 5788 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5789 if (reduced_clock)
7429e9d4 5790 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5791 } else {
190f68c5 5792 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5793 if (reduced_clock)
7429e9d4 5794 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5795 }
5796
190f68c5 5797 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 5798
f47709a9 5799 crtc->lowfreq_avail = false;
e1f234bd 5800 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
d330a953 5801 reduced_clock && i915.powersave) {
190f68c5 5802 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 5803 crtc->lowfreq_avail = true;
a7516a05 5804 } else {
190f68c5 5805 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
5806 }
5807}
5808
5e69f97f
CML
5809static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5810 pipe)
89b667f8
JB
5811{
5812 u32 reg_val;
5813
5814 /*
5815 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5816 * and set it to a reasonable value instead.
5817 */
ab3c759a 5818 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5819 reg_val &= 0xffffff00;
5820 reg_val |= 0x00000030;
ab3c759a 5821 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5822
ab3c759a 5823 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5824 reg_val &= 0x8cffffff;
5825 reg_val = 0x8c000000;
ab3c759a 5826 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5827
ab3c759a 5828 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5829 reg_val &= 0xffffff00;
ab3c759a 5830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5831
ab3c759a 5832 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5833 reg_val &= 0x00ffffff;
5834 reg_val |= 0xb0000000;
ab3c759a 5835 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5836}
5837
b551842d
DV
5838static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5839 struct intel_link_m_n *m_n)
5840{
5841 struct drm_device *dev = crtc->base.dev;
5842 struct drm_i915_private *dev_priv = dev->dev_private;
5843 int pipe = crtc->pipe;
5844
e3b95f1e
DV
5845 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5846 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5847 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5848 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5849}
5850
5851static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5852 struct intel_link_m_n *m_n,
5853 struct intel_link_m_n *m2_n2)
b551842d
DV
5854{
5855 struct drm_device *dev = crtc->base.dev;
5856 struct drm_i915_private *dev_priv = dev->dev_private;
5857 int pipe = crtc->pipe;
6e3c9717 5858 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
5859
5860 if (INTEL_INFO(dev)->gen >= 5) {
5861 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5862 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5863 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5864 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5865 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5866 * for gen < 8) and if DRRS is supported (to make sure the
5867 * registers are not unnecessarily accessed).
5868 */
5869 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 5870 crtc->config->has_drrs) {
f769cd24
VK
5871 I915_WRITE(PIPE_DATA_M2(transcoder),
5872 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5873 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5874 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5875 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5876 }
b551842d 5877 } else {
e3b95f1e
DV
5878 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5879 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5880 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5881 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5882 }
5883}
5884
f769cd24 5885void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2 5886{
6e3c9717
ACO
5887 if (crtc->config->has_pch_encoder)
5888 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 5889 else
6e3c9717
ACO
5890 intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
5891 &crtc->config->dp_m2_n2);
03afc4a2
DV
5892}
5893
d288f65f 5894static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 5895 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
5896{
5897 u32 dpll, dpll_md;
5898
5899 /*
5900 * Enable DPIO clock input. We should never disable the reference
5901 * clock for pipe B, since VGA hotplug / manual detection depends
5902 * on it.
5903 */
5904 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5905 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5906 /* We should never disable this, set it here for state tracking */
5907 if (crtc->pipe == PIPE_B)
5908 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5909 dpll |= DPLL_VCO_ENABLE;
d288f65f 5910 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 5911
d288f65f 5912 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 5913 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 5914 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
5915}
5916
d288f65f 5917static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 5918 const struct intel_crtc_state *pipe_config)
a0c4da24 5919{
f47709a9 5920 struct drm_device *dev = crtc->base.dev;
a0c4da24 5921 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5922 int pipe = crtc->pipe;
bdd4b6a6 5923 u32 mdiv;
a0c4da24 5924 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5925 u32 coreclk, reg_val;
a0c4da24 5926
09153000
DV
5927 mutex_lock(&dev_priv->dpio_lock);
5928
d288f65f
VS
5929 bestn = pipe_config->dpll.n;
5930 bestm1 = pipe_config->dpll.m1;
5931 bestm2 = pipe_config->dpll.m2;
5932 bestp1 = pipe_config->dpll.p1;
5933 bestp2 = pipe_config->dpll.p2;
a0c4da24 5934
89b667f8
JB
5935 /* See eDP HDMI DPIO driver vbios notes doc */
5936
5937 /* PLL B needs special handling */
bdd4b6a6 5938 if (pipe == PIPE_B)
5e69f97f 5939 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5940
5941 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5942 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5943
5944 /* Disable target IRef on PLL */
ab3c759a 5945 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5946 reg_val &= 0x00ffffff;
ab3c759a 5947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5948
5949 /* Disable fast lock */
ab3c759a 5950 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5951
5952 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5953 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5954 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5955 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5956 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5957
5958 /*
5959 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5960 * but we don't support that).
5961 * Note: don't use the DAC post divider as it seems unstable.
5962 */
5963 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5965
a0c4da24 5966 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5967 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5968
89b667f8 5969 /* Set HBR and RBR LPF coefficients */
d288f65f 5970 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
5971 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
5972 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 5973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5974 0x009f0003);
89b667f8 5975 else
ab3c759a 5976 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5977 0x00d0000f);
5978
681a8504 5979 if (pipe_config->has_dp_encoder) {
89b667f8 5980 /* Use SSC source */
bdd4b6a6 5981 if (pipe == PIPE_A)
ab3c759a 5982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5983 0x0df40000);
5984 else
ab3c759a 5985 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5986 0x0df70000);
5987 } else { /* HDMI or VGA */
5988 /* Use bend source */
bdd4b6a6 5989 if (pipe == PIPE_A)
ab3c759a 5990 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5991 0x0df70000);
5992 else
ab3c759a 5993 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5994 0x0df40000);
5995 }
a0c4da24 5996
ab3c759a 5997 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 5998 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
5999 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6000 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6001 coreclk |= 0x01000000;
ab3c759a 6002 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6003
ab3c759a 6004 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6005 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6006}
6007
d288f65f 6008static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6009 struct intel_crtc_state *pipe_config)
1ae0d137 6010{
d288f65f 6011 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6012 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6013 DPLL_VCO_ENABLE;
6014 if (crtc->pipe != PIPE_A)
d288f65f 6015 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6016
d288f65f
VS
6017 pipe_config->dpll_hw_state.dpll_md =
6018 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6019}
6020
d288f65f 6021static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6022 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6023{
6024 struct drm_device *dev = crtc->base.dev;
6025 struct drm_i915_private *dev_priv = dev->dev_private;
6026 int pipe = crtc->pipe;
6027 int dpll_reg = DPLL(crtc->pipe);
6028 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 6029 u32 loopfilter, intcoeff;
9d556c99
CML
6030 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
6031 int refclk;
6032
d288f65f
VS
6033 bestn = pipe_config->dpll.n;
6034 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6035 bestm1 = pipe_config->dpll.m1;
6036 bestm2 = pipe_config->dpll.m2 >> 22;
6037 bestp1 = pipe_config->dpll.p1;
6038 bestp2 = pipe_config->dpll.p2;
9d556c99
CML
6039
6040 /*
6041 * Enable Refclk and SSC
6042 */
a11b0703 6043 I915_WRITE(dpll_reg,
d288f65f 6044 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
6045
6046 mutex_lock(&dev_priv->dpio_lock);
9d556c99 6047
9d556c99
CML
6048 /* p1 and p2 divider */
6049 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6050 5 << DPIO_CHV_S1_DIV_SHIFT |
6051 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6052 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6053 1 << DPIO_CHV_K_DIV_SHIFT);
6054
6055 /* Feedback post-divider - m2 */
6056 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6057
6058 /* Feedback refclk divider - n and m1 */
6059 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6060 DPIO_CHV_M1_DIV_BY_2 |
6061 1 << DPIO_CHV_N_DIV_SHIFT);
6062
6063 /* M2 fraction division */
6064 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
6065
6066 /* M2 fraction division enable */
6067 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
6068 DPIO_CHV_FRAC_DIV_EN |
6069 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
6070
6071 /* Loop filter */
409ee761 6072 refclk = i9xx_get_refclk(crtc, 0);
9d556c99
CML
6073 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
6074 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
6075 if (refclk == 100000)
6076 intcoeff = 11;
6077 else if (refclk == 38400)
6078 intcoeff = 10;
6079 else
6080 intcoeff = 9;
6081 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
6082 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6083
6084 /* AFC Recal */
6085 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6086 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6087 DPIO_AFC_RECAL);
6088
6089 mutex_unlock(&dev_priv->dpio_lock);
6090}
6091
d288f65f
VS
6092/**
6093 * vlv_force_pll_on - forcibly enable just the PLL
6094 * @dev_priv: i915 private structure
6095 * @pipe: pipe PLL to enable
6096 * @dpll: PLL configuration
6097 *
6098 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6099 * in cases where we need the PLL enabled even when @pipe is not going to
6100 * be enabled.
6101 */
6102void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6103 const struct dpll *dpll)
6104{
6105 struct intel_crtc *crtc =
6106 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 6107 struct intel_crtc_state pipe_config = {
d288f65f
VS
6108 .pixel_multiplier = 1,
6109 .dpll = *dpll,
6110 };
6111
6112 if (IS_CHERRYVIEW(dev)) {
6113 chv_update_pll(crtc, &pipe_config);
6114 chv_prepare_pll(crtc, &pipe_config);
6115 chv_enable_pll(crtc, &pipe_config);
6116 } else {
6117 vlv_update_pll(crtc, &pipe_config);
6118 vlv_prepare_pll(crtc, &pipe_config);
6119 vlv_enable_pll(crtc, &pipe_config);
6120 }
6121}
6122
6123/**
6124 * vlv_force_pll_off - forcibly disable just the PLL
6125 * @dev_priv: i915 private structure
6126 * @pipe: pipe PLL to disable
6127 *
6128 * Disable the PLL for @pipe. To be used in cases where we need
6129 * the PLL enabled even when @pipe is not going to be enabled.
6130 */
6131void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6132{
6133 if (IS_CHERRYVIEW(dev))
6134 chv_disable_pll(to_i915(dev), pipe);
6135 else
6136 vlv_disable_pll(to_i915(dev), pipe);
6137}
6138
f47709a9 6139static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 6140 struct intel_crtc_state *crtc_state,
f47709a9 6141 intel_clock_t *reduced_clock,
eb1cbe48
DV
6142 int num_connectors)
6143{
f47709a9 6144 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6145 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
6146 u32 dpll;
6147 bool is_sdvo;
190f68c5 6148 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6149
190f68c5 6150 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6151
d0737e1d
ACO
6152 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6153 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
6154
6155 dpll = DPLL_VGA_MODE_DIS;
6156
d0737e1d 6157 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6158 dpll |= DPLLB_MODE_LVDS;
6159 else
6160 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6161
ef1b460d 6162 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 6163 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6164 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6165 }
198a037f
DV
6166
6167 if (is_sdvo)
4a33e48d 6168 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6169
190f68c5 6170 if (crtc_state->has_dp_encoder)
4a33e48d 6171 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6172
6173 /* compute bitmask from p1 value */
6174 if (IS_PINEVIEW(dev))
6175 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6176 else {
6177 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6178 if (IS_G4X(dev) && reduced_clock)
6179 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6180 }
6181 switch (clock->p2) {
6182 case 5:
6183 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6184 break;
6185 case 7:
6186 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6187 break;
6188 case 10:
6189 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6190 break;
6191 case 14:
6192 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6193 break;
6194 }
6195 if (INTEL_INFO(dev)->gen >= 4)
6196 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6197
190f68c5 6198 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6199 dpll |= PLL_REF_INPUT_TVCLKINBC;
d0737e1d 6200 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6201 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6202 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6203 else
6204 dpll |= PLL_REF_INPUT_DREFCLK;
6205
6206 dpll |= DPLL_VCO_ENABLE;
190f68c5 6207 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6208
eb1cbe48 6209 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 6210 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6211 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6212 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6213 }
6214}
6215
f47709a9 6216static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 6217 struct intel_crtc_state *crtc_state,
f47709a9 6218 intel_clock_t *reduced_clock,
eb1cbe48
DV
6219 int num_connectors)
6220{
f47709a9 6221 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6222 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6223 u32 dpll;
190f68c5 6224 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6225
190f68c5 6226 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6227
eb1cbe48
DV
6228 dpll = DPLL_VGA_MODE_DIS;
6229
d0737e1d 6230 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6231 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6232 } else {
6233 if (clock->p1 == 2)
6234 dpll |= PLL_P1_DIVIDE_BY_TWO;
6235 else
6236 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6237 if (clock->p2 == 4)
6238 dpll |= PLL_P2_DIVIDE_BY_4;
6239 }
6240
d0737e1d 6241 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
4a33e48d
DV
6242 dpll |= DPLL_DVO_2X_MODE;
6243
d0737e1d 6244 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6245 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6246 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6247 else
6248 dpll |= PLL_REF_INPUT_DREFCLK;
6249
6250 dpll |= DPLL_VCO_ENABLE;
190f68c5 6251 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6252}
6253
8a654f3b 6254static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6255{
6256 struct drm_device *dev = intel_crtc->base.dev;
6257 struct drm_i915_private *dev_priv = dev->dev_private;
6258 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6259 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 6260 struct drm_display_mode *adjusted_mode =
6e3c9717 6261 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6262 uint32_t crtc_vtotal, crtc_vblank_end;
6263 int vsyncshift = 0;
4d8a62ea
DV
6264
6265 /* We need to be careful not to changed the adjusted mode, for otherwise
6266 * the hw state checker will get angry at the mismatch. */
6267 crtc_vtotal = adjusted_mode->crtc_vtotal;
6268 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6269
609aeaca 6270 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6271 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6272 crtc_vtotal -= 1;
6273 crtc_vblank_end -= 1;
609aeaca 6274
409ee761 6275 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
6276 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6277 else
6278 vsyncshift = adjusted_mode->crtc_hsync_start -
6279 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6280 if (vsyncshift < 0)
6281 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6282 }
6283
6284 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6285 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6286
fe2b8f9d 6287 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6288 (adjusted_mode->crtc_hdisplay - 1) |
6289 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6290 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6291 (adjusted_mode->crtc_hblank_start - 1) |
6292 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6293 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6294 (adjusted_mode->crtc_hsync_start - 1) |
6295 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6296
fe2b8f9d 6297 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6298 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6299 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6300 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6301 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6302 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6303 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6304 (adjusted_mode->crtc_vsync_start - 1) |
6305 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6306
b5e508d4
PZ
6307 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6308 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6309 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6310 * bits. */
6311 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6312 (pipe == PIPE_B || pipe == PIPE_C))
6313 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6314
b0e77b9c
PZ
6315 /* pipesrc controls the size that is scaled from, which should
6316 * always be the user's requested size.
6317 */
6318 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6319 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6320 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6321}
6322
1bd1bd80 6323static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6324 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6325{
6326 struct drm_device *dev = crtc->base.dev;
6327 struct drm_i915_private *dev_priv = dev->dev_private;
6328 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6329 uint32_t tmp;
6330
6331 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6332 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6333 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6334 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6335 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6336 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6337 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6338 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6339 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6340
6341 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6342 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6343 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6344 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6345 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6346 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6347 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6348 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6349 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6350
6351 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6352 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6353 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6354 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
6355 }
6356
6357 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6358 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6359 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6360
2d112de7
ACO
6361 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6362 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6363}
6364
f6a83288 6365void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 6366 struct intel_crtc_state *pipe_config)
babea61d 6367{
2d112de7
ACO
6368 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6369 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6370 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6371 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 6372
2d112de7
ACO
6373 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6374 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6375 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6376 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 6377
2d112de7 6378 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 6379
2d112de7
ACO
6380 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6381 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
6382}
6383
84b046f3
DV
6384static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6385{
6386 struct drm_device *dev = intel_crtc->base.dev;
6387 struct drm_i915_private *dev_priv = dev->dev_private;
6388 uint32_t pipeconf;
6389
9f11a9e4 6390 pipeconf = 0;
84b046f3 6391
b6b5d049
VS
6392 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6393 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6394 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6395
6e3c9717 6396 if (intel_crtc->config->double_wide)
cf532bb2 6397 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6398
ff9ce46e
DV
6399 /* only g4x and later have fancy bpc/dither controls */
6400 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 6401 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 6402 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 6403 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6404 PIPECONF_DITHER_TYPE_SP;
84b046f3 6405
6e3c9717 6406 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
6407 case 18:
6408 pipeconf |= PIPECONF_6BPC;
6409 break;
6410 case 24:
6411 pipeconf |= PIPECONF_8BPC;
6412 break;
6413 case 30:
6414 pipeconf |= PIPECONF_10BPC;
6415 break;
6416 default:
6417 /* Case prevented by intel_choose_pipe_bpp_dither. */
6418 BUG();
84b046f3
DV
6419 }
6420 }
6421
6422 if (HAS_PIPE_CXSR(dev)) {
6423 if (intel_crtc->lowfreq_avail) {
6424 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6425 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6426 } else {
6427 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6428 }
6429 }
6430
6e3c9717 6431 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 6432 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 6433 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
6434 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6435 else
6436 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6437 } else
84b046f3
DV
6438 pipeconf |= PIPECONF_PROGRESSIVE;
6439
6e3c9717 6440 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 6441 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6442
84b046f3
DV
6443 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6444 POSTING_READ(PIPECONF(intel_crtc->pipe));
6445}
6446
190f68c5
ACO
6447static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6448 struct intel_crtc_state *crtc_state)
79e53945 6449{
c7653199 6450 struct drm_device *dev = crtc->base.dev;
79e53945 6451 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 6452 int refclk, num_connectors = 0;
652c393a 6453 intel_clock_t clock, reduced_clock;
a16af721 6454 bool ok, has_reduced_clock = false;
e9fd1c02 6455 bool is_lvds = false, is_dsi = false;
5eddb70b 6456 struct intel_encoder *encoder;
d4906093 6457 const intel_limit_t *limit;
79e53945 6458
d0737e1d
ACO
6459 for_each_intel_encoder(dev, encoder) {
6460 if (encoder->new_crtc != crtc)
6461 continue;
6462
5eddb70b 6463 switch (encoder->type) {
79e53945
JB
6464 case INTEL_OUTPUT_LVDS:
6465 is_lvds = true;
6466 break;
e9fd1c02
JN
6467 case INTEL_OUTPUT_DSI:
6468 is_dsi = true;
6469 break;
6847d71b
PZ
6470 default:
6471 break;
79e53945 6472 }
43565a06 6473
c751ce4f 6474 num_connectors++;
79e53945
JB
6475 }
6476
f2335330 6477 if (is_dsi)
5b18e57c 6478 return 0;
f2335330 6479
190f68c5 6480 if (!crtc_state->clock_set) {
409ee761 6481 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6482
e9fd1c02
JN
6483 /*
6484 * Returns a set of divisors for the desired target clock with
6485 * the given refclk, or FALSE. The returned values represent
6486 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6487 * 2) / p1 / p2.
6488 */
409ee761 6489 limit = intel_limit(crtc, refclk);
c7653199 6490 ok = dev_priv->display.find_dpll(limit, crtc,
190f68c5 6491 crtc_state->port_clock,
e9fd1c02 6492 refclk, NULL, &clock);
f2335330 6493 if (!ok) {
e9fd1c02
JN
6494 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6495 return -EINVAL;
6496 }
79e53945 6497
f2335330
JN
6498 if (is_lvds && dev_priv->lvds_downclock_avail) {
6499 /*
6500 * Ensure we match the reduced clock's P to the target
6501 * clock. If the clocks don't match, we can't switch
6502 * the display clock by using the FP0/FP1. In such case
6503 * we will disable the LVDS downclock feature.
6504 */
6505 has_reduced_clock =
c7653199 6506 dev_priv->display.find_dpll(limit, crtc,
f2335330
JN
6507 dev_priv->lvds_downclock,
6508 refclk, &clock,
6509 &reduced_clock);
6510 }
6511 /* Compat-code for transition, will disappear. */
190f68c5
ACO
6512 crtc_state->dpll.n = clock.n;
6513 crtc_state->dpll.m1 = clock.m1;
6514 crtc_state->dpll.m2 = clock.m2;
6515 crtc_state->dpll.p1 = clock.p1;
6516 crtc_state->dpll.p2 = clock.p2;
f47709a9 6517 }
7026d4ac 6518
e9fd1c02 6519 if (IS_GEN2(dev)) {
190f68c5 6520 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
6521 has_reduced_clock ? &reduced_clock : NULL,
6522 num_connectors);
9d556c99 6523 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 6524 chv_update_pll(crtc, crtc_state);
e9fd1c02 6525 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 6526 vlv_update_pll(crtc, crtc_state);
e9fd1c02 6527 } else {
190f68c5 6528 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 6529 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6530 num_connectors);
e9fd1c02 6531 }
79e53945 6532
c8f7a0db 6533 return 0;
f564048e
EA
6534}
6535
2fa2fe9a 6536static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 6537 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
6538{
6539 struct drm_device *dev = crtc->base.dev;
6540 struct drm_i915_private *dev_priv = dev->dev_private;
6541 uint32_t tmp;
6542
dc9e7dec
VS
6543 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6544 return;
6545
2fa2fe9a 6546 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6547 if (!(tmp & PFIT_ENABLE))
6548 return;
2fa2fe9a 6549
06922821 6550 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6551 if (INTEL_INFO(dev)->gen < 4) {
6552 if (crtc->pipe != PIPE_B)
6553 return;
2fa2fe9a
DV
6554 } else {
6555 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6556 return;
6557 }
6558
06922821 6559 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6560 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6561 if (INTEL_INFO(dev)->gen < 5)
6562 pipe_config->gmch_pfit.lvds_border_bits =
6563 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6564}
6565
acbec814 6566static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6567 struct intel_crtc_state *pipe_config)
acbec814
JB
6568{
6569 struct drm_device *dev = crtc->base.dev;
6570 struct drm_i915_private *dev_priv = dev->dev_private;
6571 int pipe = pipe_config->cpu_transcoder;
6572 intel_clock_t clock;
6573 u32 mdiv;
662c6ecb 6574 int refclk = 100000;
acbec814 6575
f573de5a
SK
6576 /* In case of MIPI DPLL will not even be used */
6577 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6578 return;
6579
acbec814 6580 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6581 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6582 mutex_unlock(&dev_priv->dpio_lock);
6583
6584 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6585 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6586 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6587 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6588 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6589
f646628b 6590 vlv_clock(refclk, &clock);
acbec814 6591
f646628b
VS
6592 /* clock.dot is the fast clock */
6593 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6594}
6595
5724dbd1
DL
6596static void
6597i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6598 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
6599{
6600 struct drm_device *dev = crtc->base.dev;
6601 struct drm_i915_private *dev_priv = dev->dev_private;
6602 u32 val, base, offset;
6603 int pipe = crtc->pipe, plane = crtc->plane;
6604 int fourcc, pixel_format;
6605 int aligned_height;
b113d5ee 6606 struct drm_framebuffer *fb;
1b842c89 6607 struct intel_framebuffer *intel_fb;
1ad292b5 6608
d9806c9f 6609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 6610 if (!intel_fb) {
1ad292b5
JB
6611 DRM_DEBUG_KMS("failed to alloc fb\n");
6612 return;
6613 }
6614
1b842c89
DL
6615 fb = &intel_fb->base;
6616
1ad292b5
JB
6617 val = I915_READ(DSPCNTR(plane));
6618
6619 if (INTEL_INFO(dev)->gen >= 4)
6620 if (val & DISPPLANE_TILED)
49af449b 6621 plane_config->tiling = I915_TILING_X;
1ad292b5
JB
6622
6623 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 6624 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
6625 fb->pixel_format = fourcc;
6626 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
6627
6628 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 6629 if (plane_config->tiling)
1ad292b5
JB
6630 offset = I915_READ(DSPTILEOFF(plane));
6631 else
6632 offset = I915_READ(DSPLINOFF(plane));
6633 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6634 } else {
6635 base = I915_READ(DSPADDR(plane));
6636 }
6637 plane_config->base = base;
6638
6639 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
6640 fb->width = ((val >> 16) & 0xfff) + 1;
6641 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6642
6643 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 6644 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6645
b113d5ee 6646 aligned_height = intel_fb_align_height(dev, fb->height,
ec2c981e 6647 plane_config->tiling);
1ad292b5 6648
b113d5ee 6649 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
1ad292b5 6650
2844a921
DL
6651 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6652 pipe_name(pipe), plane, fb->width, fb->height,
6653 fb->bits_per_pixel, base, fb->pitches[0],
6654 plane_config->size);
1ad292b5 6655
2d14030b 6656 plane_config->fb = intel_fb;
1ad292b5
JB
6657}
6658
70b23a98 6659static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6660 struct intel_crtc_state *pipe_config)
70b23a98
VS
6661{
6662 struct drm_device *dev = crtc->base.dev;
6663 struct drm_i915_private *dev_priv = dev->dev_private;
6664 int pipe = pipe_config->cpu_transcoder;
6665 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6666 intel_clock_t clock;
6667 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6668 int refclk = 100000;
6669
6670 mutex_lock(&dev_priv->dpio_lock);
6671 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6672 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6673 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6674 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6675 mutex_unlock(&dev_priv->dpio_lock);
6676
6677 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6678 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6679 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6680 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6681 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6682
6683 chv_clock(refclk, &clock);
6684
6685 /* clock.dot is the fast clock */
6686 pipe_config->port_clock = clock.dot / 5;
6687}
6688
0e8ffe1b 6689static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 6690 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
6691{
6692 struct drm_device *dev = crtc->base.dev;
6693 struct drm_i915_private *dev_priv = dev->dev_private;
6694 uint32_t tmp;
6695
f458ebbc
DV
6696 if (!intel_display_power_is_enabled(dev_priv,
6697 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
6698 return false;
6699
e143a21c 6700 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6701 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6702
0e8ffe1b
DV
6703 tmp = I915_READ(PIPECONF(crtc->pipe));
6704 if (!(tmp & PIPECONF_ENABLE))
6705 return false;
6706
42571aef
VS
6707 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6708 switch (tmp & PIPECONF_BPC_MASK) {
6709 case PIPECONF_6BPC:
6710 pipe_config->pipe_bpp = 18;
6711 break;
6712 case PIPECONF_8BPC:
6713 pipe_config->pipe_bpp = 24;
6714 break;
6715 case PIPECONF_10BPC:
6716 pipe_config->pipe_bpp = 30;
6717 break;
6718 default:
6719 break;
6720 }
6721 }
6722
b5a9fa09
DV
6723 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6724 pipe_config->limited_color_range = true;
6725
282740f7
VS
6726 if (INTEL_INFO(dev)->gen < 4)
6727 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6728
1bd1bd80
DV
6729 intel_get_pipe_timings(crtc, pipe_config);
6730
2fa2fe9a
DV
6731 i9xx_get_pfit_config(crtc, pipe_config);
6732
6c49f241
DV
6733 if (INTEL_INFO(dev)->gen >= 4) {
6734 tmp = I915_READ(DPLL_MD(crtc->pipe));
6735 pipe_config->pixel_multiplier =
6736 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6737 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6738 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6739 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6740 tmp = I915_READ(DPLL(crtc->pipe));
6741 pipe_config->pixel_multiplier =
6742 ((tmp & SDVO_MULTIPLIER_MASK)
6743 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6744 } else {
6745 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6746 * port and will be fixed up in the encoder->get_config
6747 * function. */
6748 pipe_config->pixel_multiplier = 1;
6749 }
8bcc2795
DV
6750 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6751 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6752 /*
6753 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6754 * on 830. Filter it out here so that we don't
6755 * report errors due to that.
6756 */
6757 if (IS_I830(dev))
6758 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6759
8bcc2795
DV
6760 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6761 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6762 } else {
6763 /* Mask out read-only status bits. */
6764 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6765 DPLL_PORTC_READY_MASK |
6766 DPLL_PORTB_READY_MASK);
8bcc2795 6767 }
6c49f241 6768
70b23a98
VS
6769 if (IS_CHERRYVIEW(dev))
6770 chv_crtc_clock_get(crtc, pipe_config);
6771 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6772 vlv_crtc_clock_get(crtc, pipe_config);
6773 else
6774 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6775
0e8ffe1b
DV
6776 return true;
6777}
6778
dde86e2d 6779static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6780{
6781 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6782 struct intel_encoder *encoder;
74cfd7ac 6783 u32 val, final;
13d83a67 6784 bool has_lvds = false;
199e5d79 6785 bool has_cpu_edp = false;
199e5d79 6786 bool has_panel = false;
99eb6a01
KP
6787 bool has_ck505 = false;
6788 bool can_ssc = false;
13d83a67
JB
6789
6790 /* We need to take the global config into account */
b2784e15 6791 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6792 switch (encoder->type) {
6793 case INTEL_OUTPUT_LVDS:
6794 has_panel = true;
6795 has_lvds = true;
6796 break;
6797 case INTEL_OUTPUT_EDP:
6798 has_panel = true;
2de6905f 6799 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6800 has_cpu_edp = true;
6801 break;
6847d71b
PZ
6802 default:
6803 break;
13d83a67
JB
6804 }
6805 }
6806
99eb6a01 6807 if (HAS_PCH_IBX(dev)) {
41aa3448 6808 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6809 can_ssc = has_ck505;
6810 } else {
6811 has_ck505 = false;
6812 can_ssc = true;
6813 }
6814
2de6905f
ID
6815 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6816 has_panel, has_lvds, has_ck505);
13d83a67
JB
6817
6818 /* Ironlake: try to setup display ref clock before DPLL
6819 * enabling. This is only under driver's control after
6820 * PCH B stepping, previous chipset stepping should be
6821 * ignoring this setting.
6822 */
74cfd7ac
CW
6823 val = I915_READ(PCH_DREF_CONTROL);
6824
6825 /* As we must carefully and slowly disable/enable each source in turn,
6826 * compute the final state we want first and check if we need to
6827 * make any changes at all.
6828 */
6829 final = val;
6830 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6831 if (has_ck505)
6832 final |= DREF_NONSPREAD_CK505_ENABLE;
6833 else
6834 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6835
6836 final &= ~DREF_SSC_SOURCE_MASK;
6837 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6838 final &= ~DREF_SSC1_ENABLE;
6839
6840 if (has_panel) {
6841 final |= DREF_SSC_SOURCE_ENABLE;
6842
6843 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6844 final |= DREF_SSC1_ENABLE;
6845
6846 if (has_cpu_edp) {
6847 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6848 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6849 else
6850 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6851 } else
6852 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6853 } else {
6854 final |= DREF_SSC_SOURCE_DISABLE;
6855 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6856 }
6857
6858 if (final == val)
6859 return;
6860
13d83a67 6861 /* Always enable nonspread source */
74cfd7ac 6862 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6863
99eb6a01 6864 if (has_ck505)
74cfd7ac 6865 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6866 else
74cfd7ac 6867 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6868
199e5d79 6869 if (has_panel) {
74cfd7ac
CW
6870 val &= ~DREF_SSC_SOURCE_MASK;
6871 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6872
199e5d79 6873 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6874 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6875 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6876 val |= DREF_SSC1_ENABLE;
e77166b5 6877 } else
74cfd7ac 6878 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6879
6880 /* Get SSC going before enabling the outputs */
74cfd7ac 6881 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6882 POSTING_READ(PCH_DREF_CONTROL);
6883 udelay(200);
6884
74cfd7ac 6885 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6886
6887 /* Enable CPU source on CPU attached eDP */
199e5d79 6888 if (has_cpu_edp) {
99eb6a01 6889 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6890 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6891 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6892 } else
74cfd7ac 6893 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6894 } else
74cfd7ac 6895 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6896
74cfd7ac 6897 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6898 POSTING_READ(PCH_DREF_CONTROL);
6899 udelay(200);
6900 } else {
6901 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6902
74cfd7ac 6903 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6904
6905 /* Turn off CPU output */
74cfd7ac 6906 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6907
74cfd7ac 6908 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6909 POSTING_READ(PCH_DREF_CONTROL);
6910 udelay(200);
6911
6912 /* Turn off the SSC source */
74cfd7ac
CW
6913 val &= ~DREF_SSC_SOURCE_MASK;
6914 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6915
6916 /* Turn off SSC1 */
74cfd7ac 6917 val &= ~DREF_SSC1_ENABLE;
199e5d79 6918
74cfd7ac 6919 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6920 POSTING_READ(PCH_DREF_CONTROL);
6921 udelay(200);
6922 }
74cfd7ac
CW
6923
6924 BUG_ON(val != final);
13d83a67
JB
6925}
6926
f31f2d55 6927static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6928{
f31f2d55 6929 uint32_t tmp;
dde86e2d 6930
0ff066a9
PZ
6931 tmp = I915_READ(SOUTH_CHICKEN2);
6932 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6933 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6934
0ff066a9
PZ
6935 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6936 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6937 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6938
0ff066a9
PZ
6939 tmp = I915_READ(SOUTH_CHICKEN2);
6940 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6941 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6942
0ff066a9
PZ
6943 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6944 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6945 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6946}
6947
6948/* WaMPhyProgramming:hsw */
6949static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6950{
6951 uint32_t tmp;
dde86e2d
PZ
6952
6953 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6954 tmp &= ~(0xFF << 24);
6955 tmp |= (0x12 << 24);
6956 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6957
dde86e2d
PZ
6958 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6959 tmp |= (1 << 11);
6960 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6961
6962 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6963 tmp |= (1 << 11);
6964 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6965
dde86e2d
PZ
6966 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6967 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6968 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6969
6970 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6971 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6972 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6973
0ff066a9
PZ
6974 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6975 tmp &= ~(7 << 13);
6976 tmp |= (5 << 13);
6977 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6978
0ff066a9
PZ
6979 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6980 tmp &= ~(7 << 13);
6981 tmp |= (5 << 13);
6982 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6983
6984 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6985 tmp &= ~0xFF;
6986 tmp |= 0x1C;
6987 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6988
6989 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6990 tmp &= ~0xFF;
6991 tmp |= 0x1C;
6992 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6993
6994 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6995 tmp &= ~(0xFF << 16);
6996 tmp |= (0x1C << 16);
6997 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6998
6999 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7000 tmp &= ~(0xFF << 16);
7001 tmp |= (0x1C << 16);
7002 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7003
0ff066a9
PZ
7004 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7005 tmp |= (1 << 27);
7006 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7007
0ff066a9
PZ
7008 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7009 tmp |= (1 << 27);
7010 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7011
0ff066a9
PZ
7012 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7013 tmp &= ~(0xF << 28);
7014 tmp |= (4 << 28);
7015 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7016
0ff066a9
PZ
7017 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7018 tmp &= ~(0xF << 28);
7019 tmp |= (4 << 28);
7020 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7021}
7022
2fa86a1f
PZ
7023/* Implements 3 different sequences from BSpec chapter "Display iCLK
7024 * Programming" based on the parameters passed:
7025 * - Sequence to enable CLKOUT_DP
7026 * - Sequence to enable CLKOUT_DP without spread
7027 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7028 */
7029static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7030 bool with_fdi)
f31f2d55
PZ
7031{
7032 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
7033 uint32_t reg, tmp;
7034
7035 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7036 with_spread = true;
7037 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7038 with_fdi, "LP PCH doesn't have FDI\n"))
7039 with_fdi = false;
f31f2d55
PZ
7040
7041 mutex_lock(&dev_priv->dpio_lock);
7042
7043 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7044 tmp &= ~SBI_SSCCTL_DISABLE;
7045 tmp |= SBI_SSCCTL_PATHALT;
7046 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7047
7048 udelay(24);
7049
2fa86a1f
PZ
7050 if (with_spread) {
7051 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7052 tmp &= ~SBI_SSCCTL_PATHALT;
7053 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7054
2fa86a1f
PZ
7055 if (with_fdi) {
7056 lpt_reset_fdi_mphy(dev_priv);
7057 lpt_program_fdi_mphy(dev_priv);
7058 }
7059 }
dde86e2d 7060
2fa86a1f
PZ
7061 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7062 SBI_GEN0 : SBI_DBUFF0;
7063 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7064 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7065 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
7066
7067 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
7068}
7069
47701c3b
PZ
7070/* Sequence to disable CLKOUT_DP */
7071static void lpt_disable_clkout_dp(struct drm_device *dev)
7072{
7073 struct drm_i915_private *dev_priv = dev->dev_private;
7074 uint32_t reg, tmp;
7075
7076 mutex_lock(&dev_priv->dpio_lock);
7077
7078 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7079 SBI_GEN0 : SBI_DBUFF0;
7080 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7081 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7082 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7083
7084 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7085 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7086 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7087 tmp |= SBI_SSCCTL_PATHALT;
7088 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7089 udelay(32);
7090 }
7091 tmp |= SBI_SSCCTL_DISABLE;
7092 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7093 }
7094
7095 mutex_unlock(&dev_priv->dpio_lock);
7096}
7097
bf8fa3d3
PZ
7098static void lpt_init_pch_refclk(struct drm_device *dev)
7099{
bf8fa3d3
PZ
7100 struct intel_encoder *encoder;
7101 bool has_vga = false;
7102
b2784e15 7103 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
7104 switch (encoder->type) {
7105 case INTEL_OUTPUT_ANALOG:
7106 has_vga = true;
7107 break;
6847d71b
PZ
7108 default:
7109 break;
bf8fa3d3
PZ
7110 }
7111 }
7112
47701c3b
PZ
7113 if (has_vga)
7114 lpt_enable_clkout_dp(dev, true, true);
7115 else
7116 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
7117}
7118
dde86e2d
PZ
7119/*
7120 * Initialize reference clocks when the driver loads
7121 */
7122void intel_init_pch_refclk(struct drm_device *dev)
7123{
7124 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7125 ironlake_init_pch_refclk(dev);
7126 else if (HAS_PCH_LPT(dev))
7127 lpt_init_pch_refclk(dev);
7128}
7129
d9d444cb
JB
7130static int ironlake_get_refclk(struct drm_crtc *crtc)
7131{
7132 struct drm_device *dev = crtc->dev;
7133 struct drm_i915_private *dev_priv = dev->dev_private;
7134 struct intel_encoder *encoder;
d9d444cb
JB
7135 int num_connectors = 0;
7136 bool is_lvds = false;
7137
d0737e1d
ACO
7138 for_each_intel_encoder(dev, encoder) {
7139 if (encoder->new_crtc != to_intel_crtc(crtc))
7140 continue;
7141
d9d444cb
JB
7142 switch (encoder->type) {
7143 case INTEL_OUTPUT_LVDS:
7144 is_lvds = true;
7145 break;
6847d71b
PZ
7146 default:
7147 break;
d9d444cb
JB
7148 }
7149 num_connectors++;
7150 }
7151
7152 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 7153 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 7154 dev_priv->vbt.lvds_ssc_freq);
e91e941b 7155 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
7156 }
7157
7158 return 120000;
7159}
7160
6ff93609 7161static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7162{
c8203565 7163 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
7164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7165 int pipe = intel_crtc->pipe;
c8203565
PZ
7166 uint32_t val;
7167
78114071 7168 val = 0;
c8203565 7169
6e3c9717 7170 switch (intel_crtc->config->pipe_bpp) {
c8203565 7171 case 18:
dfd07d72 7172 val |= PIPECONF_6BPC;
c8203565
PZ
7173 break;
7174 case 24:
dfd07d72 7175 val |= PIPECONF_8BPC;
c8203565
PZ
7176 break;
7177 case 30:
dfd07d72 7178 val |= PIPECONF_10BPC;
c8203565
PZ
7179 break;
7180 case 36:
dfd07d72 7181 val |= PIPECONF_12BPC;
c8203565
PZ
7182 break;
7183 default:
cc769b62
PZ
7184 /* Case prevented by intel_choose_pipe_bpp_dither. */
7185 BUG();
c8203565
PZ
7186 }
7187
6e3c9717 7188 if (intel_crtc->config->dither)
c8203565
PZ
7189 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7190
6e3c9717 7191 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
7192 val |= PIPECONF_INTERLACED_ILK;
7193 else
7194 val |= PIPECONF_PROGRESSIVE;
7195
6e3c9717 7196 if (intel_crtc->config->limited_color_range)
3685a8f3 7197 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 7198
c8203565
PZ
7199 I915_WRITE(PIPECONF(pipe), val);
7200 POSTING_READ(PIPECONF(pipe));
7201}
7202
86d3efce
VS
7203/*
7204 * Set up the pipe CSC unit.
7205 *
7206 * Currently only full range RGB to limited range RGB conversion
7207 * is supported, but eventually this should handle various
7208 * RGB<->YCbCr scenarios as well.
7209 */
50f3b016 7210static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
7211{
7212 struct drm_device *dev = crtc->dev;
7213 struct drm_i915_private *dev_priv = dev->dev_private;
7214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7215 int pipe = intel_crtc->pipe;
7216 uint16_t coeff = 0x7800; /* 1.0 */
7217
7218 /*
7219 * TODO: Check what kind of values actually come out of the pipe
7220 * with these coeff/postoff values and adjust to get the best
7221 * accuracy. Perhaps we even need to take the bpc value into
7222 * consideration.
7223 */
7224
6e3c9717 7225 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7226 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7227
7228 /*
7229 * GY/GU and RY/RU should be the other way around according
7230 * to BSpec, but reality doesn't agree. Just set them up in
7231 * a way that results in the correct picture.
7232 */
7233 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7234 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7235
7236 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7237 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7238
7239 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7240 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7241
7242 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7243 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7244 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7245
7246 if (INTEL_INFO(dev)->gen > 6) {
7247 uint16_t postoff = 0;
7248
6e3c9717 7249 if (intel_crtc->config->limited_color_range)
32cf0cb0 7250 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7251
7252 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7253 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7254 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7255
7256 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7257 } else {
7258 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7259
6e3c9717 7260 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7261 mode |= CSC_BLACK_SCREEN_OFFSET;
7262
7263 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7264 }
7265}
7266
6ff93609 7267static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7268{
756f85cf
PZ
7269 struct drm_device *dev = crtc->dev;
7270 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7272 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7273 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
7274 uint32_t val;
7275
3eff4faa 7276 val = 0;
ee2b0b38 7277
6e3c9717 7278 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
7279 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7280
6e3c9717 7281 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7282 val |= PIPECONF_INTERLACED_ILK;
7283 else
7284 val |= PIPECONF_PROGRESSIVE;
7285
702e7a56
PZ
7286 I915_WRITE(PIPECONF(cpu_transcoder), val);
7287 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7288
7289 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7290 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7291
3cdf122c 7292 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7293 val = 0;
7294
6e3c9717 7295 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
7296 case 18:
7297 val |= PIPEMISC_DITHER_6_BPC;
7298 break;
7299 case 24:
7300 val |= PIPEMISC_DITHER_8_BPC;
7301 break;
7302 case 30:
7303 val |= PIPEMISC_DITHER_10_BPC;
7304 break;
7305 case 36:
7306 val |= PIPEMISC_DITHER_12_BPC;
7307 break;
7308 default:
7309 /* Case prevented by pipe_config_set_bpp. */
7310 BUG();
7311 }
7312
6e3c9717 7313 if (intel_crtc->config->dither)
756f85cf
PZ
7314 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7315
7316 I915_WRITE(PIPEMISC(pipe), val);
7317 }
ee2b0b38
PZ
7318}
7319
6591c6e4 7320static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 7321 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
7322 intel_clock_t *clock,
7323 bool *has_reduced_clock,
7324 intel_clock_t *reduced_clock)
7325{
7326 struct drm_device *dev = crtc->dev;
7327 struct drm_i915_private *dev_priv = dev->dev_private;
a919ff14 7328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6591c6e4 7329 int refclk;
d4906093 7330 const intel_limit_t *limit;
a16af721 7331 bool ret, is_lvds = false;
79e53945 7332
d0737e1d 7333 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
79e53945 7334
d9d444cb 7335 refclk = ironlake_get_refclk(crtc);
79e53945 7336
d4906093
ML
7337 /*
7338 * Returns a set of divisors for the desired target clock with the given
7339 * refclk, or FALSE. The returned values represent the clock equation:
7340 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7341 */
409ee761 7342 limit = intel_limit(intel_crtc, refclk);
a919ff14 7343 ret = dev_priv->display.find_dpll(limit, intel_crtc,
190f68c5 7344 crtc_state->port_clock,
ee9300bb 7345 refclk, NULL, clock);
6591c6e4
PZ
7346 if (!ret)
7347 return false;
cda4b7d3 7348
ddc9003c 7349 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7350 /*
7351 * Ensure we match the reduced clock's P to the target clock.
7352 * If the clocks don't match, we can't switch the display clock
7353 * by using the FP0/FP1. In such case we will disable the LVDS
7354 * downclock feature.
7355 */
ee9300bb 7356 *has_reduced_clock =
a919ff14 7357 dev_priv->display.find_dpll(limit, intel_crtc,
ee9300bb
DV
7358 dev_priv->lvds_downclock,
7359 refclk, clock,
7360 reduced_clock);
652c393a 7361 }
61e9653f 7362
6591c6e4
PZ
7363 return true;
7364}
7365
d4b1931c
PZ
7366int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7367{
7368 /*
7369 * Account for spread spectrum to avoid
7370 * oversubscribing the link. Max center spread
7371 * is 2.5%; use 5% for safety's sake.
7372 */
7373 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7374 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7375}
7376
7429e9d4 7377static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7378{
7429e9d4 7379 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7380}
7381
de13a2e3 7382static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 7383 struct intel_crtc_state *crtc_state,
7429e9d4 7384 u32 *fp,
9a7c7890 7385 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7386{
de13a2e3 7387 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7388 struct drm_device *dev = crtc->dev;
7389 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7390 struct intel_encoder *intel_encoder;
7391 uint32_t dpll;
6cc5f341 7392 int factor, num_connectors = 0;
09ede541 7393 bool is_lvds = false, is_sdvo = false;
79e53945 7394
d0737e1d
ACO
7395 for_each_intel_encoder(dev, intel_encoder) {
7396 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7397 continue;
7398
de13a2e3 7399 switch (intel_encoder->type) {
79e53945
JB
7400 case INTEL_OUTPUT_LVDS:
7401 is_lvds = true;
7402 break;
7403 case INTEL_OUTPUT_SDVO:
7d57382e 7404 case INTEL_OUTPUT_HDMI:
79e53945 7405 is_sdvo = true;
79e53945 7406 break;
6847d71b
PZ
7407 default:
7408 break;
79e53945 7409 }
43565a06 7410
c751ce4f 7411 num_connectors++;
79e53945 7412 }
79e53945 7413
c1858123 7414 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7415 factor = 21;
7416 if (is_lvds) {
7417 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7418 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7419 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7420 factor = 25;
190f68c5 7421 } else if (crtc_state->sdvo_tv_clock)
8febb297 7422 factor = 20;
c1858123 7423
190f68c5 7424 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 7425 *fp |= FP_CB_TUNE;
2c07245f 7426
9a7c7890
DV
7427 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7428 *fp2 |= FP_CB_TUNE;
7429
5eddb70b 7430 dpll = 0;
2c07245f 7431
a07d6787
EA
7432 if (is_lvds)
7433 dpll |= DPLLB_MODE_LVDS;
7434 else
7435 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7436
190f68c5 7437 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 7438 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7439
7440 if (is_sdvo)
4a33e48d 7441 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 7442 if (crtc_state->has_dp_encoder)
4a33e48d 7443 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7444
a07d6787 7445 /* compute bitmask from p1 value */
190f68c5 7446 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7447 /* also FPA1 */
190f68c5 7448 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7449
190f68c5 7450 switch (crtc_state->dpll.p2) {
a07d6787
EA
7451 case 5:
7452 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7453 break;
7454 case 7:
7455 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7456 break;
7457 case 10:
7458 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7459 break;
7460 case 14:
7461 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7462 break;
79e53945
JB
7463 }
7464
b4c09f3b 7465 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7466 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7467 else
7468 dpll |= PLL_REF_INPUT_DREFCLK;
7469
959e16d6 7470 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7471}
7472
190f68c5
ACO
7473static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7474 struct intel_crtc_state *crtc_state)
de13a2e3 7475{
c7653199 7476 struct drm_device *dev = crtc->base.dev;
de13a2e3 7477 intel_clock_t clock, reduced_clock;
cbbab5bd 7478 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7479 bool ok, has_reduced_clock = false;
8b47047b 7480 bool is_lvds = false;
e2b78267 7481 struct intel_shared_dpll *pll;
de13a2e3 7482
409ee761 7483 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 7484
5dc5298b
PZ
7485 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7486 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7487
190f68c5 7488 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 7489 &has_reduced_clock, &reduced_clock);
190f68c5 7490 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
7491 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7492 return -EINVAL;
79e53945 7493 }
f47709a9 7494 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7495 if (!crtc_state->clock_set) {
7496 crtc_state->dpll.n = clock.n;
7497 crtc_state->dpll.m1 = clock.m1;
7498 crtc_state->dpll.m2 = clock.m2;
7499 crtc_state->dpll.p1 = clock.p1;
7500 crtc_state->dpll.p2 = clock.p2;
f47709a9 7501 }
79e53945 7502
5dc5298b 7503 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
7504 if (crtc_state->has_pch_encoder) {
7505 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 7506 if (has_reduced_clock)
7429e9d4 7507 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7508
190f68c5 7509 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
7510 &fp, &reduced_clock,
7511 has_reduced_clock ? &fp2 : NULL);
7512
190f68c5
ACO
7513 crtc_state->dpll_hw_state.dpll = dpll;
7514 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 7515 if (has_reduced_clock)
190f68c5 7516 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 7517 else
190f68c5 7518 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 7519
190f68c5 7520 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 7521 if (pll == NULL) {
84f44ce7 7522 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 7523 pipe_name(crtc->pipe));
4b645f14
JB
7524 return -EINVAL;
7525 }
3fb37703 7526 }
79e53945 7527
d330a953 7528 if (is_lvds && has_reduced_clock && i915.powersave)
c7653199 7529 crtc->lowfreq_avail = true;
bcd644e0 7530 else
c7653199 7531 crtc->lowfreq_avail = false;
e2b78267 7532
c8f7a0db 7533 return 0;
79e53945
JB
7534}
7535
eb14cb74
VS
7536static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7537 struct intel_link_m_n *m_n)
7538{
7539 struct drm_device *dev = crtc->base.dev;
7540 struct drm_i915_private *dev_priv = dev->dev_private;
7541 enum pipe pipe = crtc->pipe;
7542
7543 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7544 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7545 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7546 & ~TU_SIZE_MASK;
7547 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7548 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7549 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7550}
7551
7552static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7553 enum transcoder transcoder,
b95af8be
VK
7554 struct intel_link_m_n *m_n,
7555 struct intel_link_m_n *m2_n2)
72419203
DV
7556{
7557 struct drm_device *dev = crtc->base.dev;
7558 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7559 enum pipe pipe = crtc->pipe;
72419203 7560
eb14cb74
VS
7561 if (INTEL_INFO(dev)->gen >= 5) {
7562 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7563 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7564 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7565 & ~TU_SIZE_MASK;
7566 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7567 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7568 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7569 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7570 * gen < 8) and if DRRS is supported (to make sure the
7571 * registers are not unnecessarily read).
7572 */
7573 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 7574 crtc->config->has_drrs) {
b95af8be
VK
7575 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7576 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7577 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7578 & ~TU_SIZE_MASK;
7579 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7580 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7581 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7582 }
eb14cb74
VS
7583 } else {
7584 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7585 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7586 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7587 & ~TU_SIZE_MASK;
7588 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7589 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7590 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7591 }
7592}
7593
7594void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 7595 struct intel_crtc_state *pipe_config)
eb14cb74 7596{
681a8504 7597 if (pipe_config->has_pch_encoder)
eb14cb74
VS
7598 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7599 else
7600 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7601 &pipe_config->dp_m_n,
7602 &pipe_config->dp_m2_n2);
eb14cb74 7603}
72419203 7604
eb14cb74 7605static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 7606 struct intel_crtc_state *pipe_config)
eb14cb74
VS
7607{
7608 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7609 &pipe_config->fdi_m_n, NULL);
72419203
DV
7610}
7611
bd2e244f 7612static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7613 struct intel_crtc_state *pipe_config)
bd2e244f
JB
7614{
7615 struct drm_device *dev = crtc->base.dev;
7616 struct drm_i915_private *dev_priv = dev->dev_private;
7617 uint32_t tmp;
7618
7619 tmp = I915_READ(PS_CTL(crtc->pipe));
7620
7621 if (tmp & PS_ENABLE) {
7622 pipe_config->pch_pfit.enabled = true;
7623 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7624 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7625 }
7626}
7627
5724dbd1
DL
7628static void
7629skylake_get_initial_plane_config(struct intel_crtc *crtc,
7630 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
7631{
7632 struct drm_device *dev = crtc->base.dev;
7633 struct drm_i915_private *dev_priv = dev->dev_private;
7634 u32 val, base, offset, stride_mult;
7635 int pipe = crtc->pipe;
7636 int fourcc, pixel_format;
7637 int aligned_height;
7638 struct drm_framebuffer *fb;
1b842c89 7639 struct intel_framebuffer *intel_fb;
bc8d7dff 7640
d9806c9f 7641 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7642 if (!intel_fb) {
bc8d7dff
DL
7643 DRM_DEBUG_KMS("failed to alloc fb\n");
7644 return;
7645 }
7646
1b842c89
DL
7647 fb = &intel_fb->base;
7648
bc8d7dff
DL
7649 val = I915_READ(PLANE_CTL(pipe, 0));
7650 if (val & PLANE_CTL_TILED_MASK)
7651 plane_config->tiling = I915_TILING_X;
7652
7653 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7654 fourcc = skl_format_to_fourcc(pixel_format,
7655 val & PLANE_CTL_ORDER_RGBX,
7656 val & PLANE_CTL_ALPHA_MASK);
7657 fb->pixel_format = fourcc;
7658 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7659
7660 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7661 plane_config->base = base;
7662
7663 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7664
7665 val = I915_READ(PLANE_SIZE(pipe, 0));
7666 fb->height = ((val >> 16) & 0xfff) + 1;
7667 fb->width = ((val >> 0) & 0x1fff) + 1;
7668
7669 val = I915_READ(PLANE_STRIDE(pipe, 0));
7670 switch (plane_config->tiling) {
7671 case I915_TILING_NONE:
7672 stride_mult = 64;
7673 break;
7674 case I915_TILING_X:
7675 stride_mult = 512;
7676 break;
7677 default:
7678 MISSING_CASE(plane_config->tiling);
7679 goto error;
7680 }
7681 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7682
7683 aligned_height = intel_fb_align_height(dev, fb->height,
7684 plane_config->tiling);
7685
7686 plane_config->size = ALIGN(fb->pitches[0] * aligned_height, PAGE_SIZE);
7687
7688 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7689 pipe_name(pipe), fb->width, fb->height,
7690 fb->bits_per_pixel, base, fb->pitches[0],
7691 plane_config->size);
7692
2d14030b 7693 plane_config->fb = intel_fb;
bc8d7dff
DL
7694 return;
7695
7696error:
7697 kfree(fb);
7698}
7699
2fa2fe9a 7700static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7701 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7702{
7703 struct drm_device *dev = crtc->base.dev;
7704 struct drm_i915_private *dev_priv = dev->dev_private;
7705 uint32_t tmp;
7706
7707 tmp = I915_READ(PF_CTL(crtc->pipe));
7708
7709 if (tmp & PF_ENABLE) {
fd4daa9c 7710 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7711 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7712 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7713
7714 /* We currently do not free assignements of panel fitters on
7715 * ivb/hsw (since we don't use the higher upscaling modes which
7716 * differentiates them) so just WARN about this case for now. */
7717 if (IS_GEN7(dev)) {
7718 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7719 PF_PIPE_SEL_IVB(crtc->pipe));
7720 }
2fa2fe9a 7721 }
79e53945
JB
7722}
7723
5724dbd1
DL
7724static void
7725ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7726 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
7727{
7728 struct drm_device *dev = crtc->base.dev;
7729 struct drm_i915_private *dev_priv = dev->dev_private;
7730 u32 val, base, offset;
aeee5a49 7731 int pipe = crtc->pipe;
4c6baa59
JB
7732 int fourcc, pixel_format;
7733 int aligned_height;
b113d5ee 7734 struct drm_framebuffer *fb;
1b842c89 7735 struct intel_framebuffer *intel_fb;
4c6baa59 7736
d9806c9f 7737 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7738 if (!intel_fb) {
4c6baa59
JB
7739 DRM_DEBUG_KMS("failed to alloc fb\n");
7740 return;
7741 }
7742
1b842c89
DL
7743 fb = &intel_fb->base;
7744
aeee5a49 7745 val = I915_READ(DSPCNTR(pipe));
4c6baa59
JB
7746
7747 if (INTEL_INFO(dev)->gen >= 4)
7748 if (val & DISPPLANE_TILED)
49af449b 7749 plane_config->tiling = I915_TILING_X;
4c6baa59
JB
7750
7751 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7752 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7753 fb->pixel_format = fourcc;
7754 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 7755
aeee5a49 7756 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 7757 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 7758 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 7759 } else {
49af449b 7760 if (plane_config->tiling)
aeee5a49 7761 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 7762 else
aeee5a49 7763 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
7764 }
7765 plane_config->base = base;
7766
7767 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7768 fb->width = ((val >> 16) & 0xfff) + 1;
7769 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7770
7771 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7772 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7773
b113d5ee 7774 aligned_height = intel_fb_align_height(dev, fb->height,
ec2c981e 7775 plane_config->tiling);
4c6baa59 7776
b113d5ee 7777 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
4c6baa59 7778
2844a921
DL
7779 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7780 pipe_name(pipe), fb->width, fb->height,
7781 fb->bits_per_pixel, base, fb->pitches[0],
7782 plane_config->size);
b113d5ee 7783
2d14030b 7784 plane_config->fb = intel_fb;
4c6baa59
JB
7785}
7786
0e8ffe1b 7787static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7788 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7789{
7790 struct drm_device *dev = crtc->base.dev;
7791 struct drm_i915_private *dev_priv = dev->dev_private;
7792 uint32_t tmp;
7793
f458ebbc
DV
7794 if (!intel_display_power_is_enabled(dev_priv,
7795 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
7796 return false;
7797
e143a21c 7798 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7799 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7800
0e8ffe1b
DV
7801 tmp = I915_READ(PIPECONF(crtc->pipe));
7802 if (!(tmp & PIPECONF_ENABLE))
7803 return false;
7804
42571aef
VS
7805 switch (tmp & PIPECONF_BPC_MASK) {
7806 case PIPECONF_6BPC:
7807 pipe_config->pipe_bpp = 18;
7808 break;
7809 case PIPECONF_8BPC:
7810 pipe_config->pipe_bpp = 24;
7811 break;
7812 case PIPECONF_10BPC:
7813 pipe_config->pipe_bpp = 30;
7814 break;
7815 case PIPECONF_12BPC:
7816 pipe_config->pipe_bpp = 36;
7817 break;
7818 default:
7819 break;
7820 }
7821
b5a9fa09
DV
7822 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7823 pipe_config->limited_color_range = true;
7824
ab9412ba 7825 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7826 struct intel_shared_dpll *pll;
7827
88adfff1
DV
7828 pipe_config->has_pch_encoder = true;
7829
627eb5a3
DV
7830 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7831 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7832 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7833
7834 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7835
c0d43d62 7836 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7837 pipe_config->shared_dpll =
7838 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7839 } else {
7840 tmp = I915_READ(PCH_DPLL_SEL);
7841 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7842 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7843 else
7844 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7845 }
66e985c0
DV
7846
7847 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7848
7849 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7850 &pipe_config->dpll_hw_state));
c93f54cf
DV
7851
7852 tmp = pipe_config->dpll_hw_state.dpll;
7853 pipe_config->pixel_multiplier =
7854 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7855 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7856
7857 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7858 } else {
7859 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7860 }
7861
1bd1bd80
DV
7862 intel_get_pipe_timings(crtc, pipe_config);
7863
2fa2fe9a
DV
7864 ironlake_get_pfit_config(crtc, pipe_config);
7865
0e8ffe1b
DV
7866 return true;
7867}
7868
be256dc7
PZ
7869static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7870{
7871 struct drm_device *dev = dev_priv->dev;
be256dc7 7872 struct intel_crtc *crtc;
be256dc7 7873
d3fcc808 7874 for_each_intel_crtc(dev, crtc)
e2c719b7 7875 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7876 pipe_name(crtc->pipe));
7877
e2c719b7
RC
7878 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7879 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7880 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7881 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
7882 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7883 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 7884 "CPU PWM1 enabled\n");
c5107b87 7885 if (IS_HASWELL(dev))
e2c719b7 7886 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 7887 "CPU PWM2 enabled\n");
e2c719b7 7888 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 7889 "PCH PWM1 enabled\n");
e2c719b7 7890 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 7891 "Utility pin enabled\n");
e2c719b7 7892 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 7893
9926ada1
PZ
7894 /*
7895 * In theory we can still leave IRQs enabled, as long as only the HPD
7896 * interrupts remain enabled. We used to check for that, but since it's
7897 * gen-specific and since we only disable LCPLL after we fully disable
7898 * the interrupts, the check below should be enough.
7899 */
e2c719b7 7900 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7901}
7902
9ccd5aeb
PZ
7903static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7904{
7905 struct drm_device *dev = dev_priv->dev;
7906
7907 if (IS_HASWELL(dev))
7908 return I915_READ(D_COMP_HSW);
7909 else
7910 return I915_READ(D_COMP_BDW);
7911}
7912
3c4c9b81
PZ
7913static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7914{
7915 struct drm_device *dev = dev_priv->dev;
7916
7917 if (IS_HASWELL(dev)) {
7918 mutex_lock(&dev_priv->rps.hw_lock);
7919 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7920 val))
f475dadf 7921 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7922 mutex_unlock(&dev_priv->rps.hw_lock);
7923 } else {
9ccd5aeb
PZ
7924 I915_WRITE(D_COMP_BDW, val);
7925 POSTING_READ(D_COMP_BDW);
3c4c9b81 7926 }
be256dc7
PZ
7927}
7928
7929/*
7930 * This function implements pieces of two sequences from BSpec:
7931 * - Sequence for display software to disable LCPLL
7932 * - Sequence for display software to allow package C8+
7933 * The steps implemented here are just the steps that actually touch the LCPLL
7934 * register. Callers should take care of disabling all the display engine
7935 * functions, doing the mode unset, fixing interrupts, etc.
7936 */
6ff58d53
PZ
7937static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7938 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7939{
7940 uint32_t val;
7941
7942 assert_can_disable_lcpll(dev_priv);
7943
7944 val = I915_READ(LCPLL_CTL);
7945
7946 if (switch_to_fclk) {
7947 val |= LCPLL_CD_SOURCE_FCLK;
7948 I915_WRITE(LCPLL_CTL, val);
7949
7950 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7951 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7952 DRM_ERROR("Switching to FCLK failed\n");
7953
7954 val = I915_READ(LCPLL_CTL);
7955 }
7956
7957 val |= LCPLL_PLL_DISABLE;
7958 I915_WRITE(LCPLL_CTL, val);
7959 POSTING_READ(LCPLL_CTL);
7960
7961 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7962 DRM_ERROR("LCPLL still locked\n");
7963
9ccd5aeb 7964 val = hsw_read_dcomp(dev_priv);
be256dc7 7965 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7966 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7967 ndelay(100);
7968
9ccd5aeb
PZ
7969 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7970 1))
be256dc7
PZ
7971 DRM_ERROR("D_COMP RCOMP still in progress\n");
7972
7973 if (allow_power_down) {
7974 val = I915_READ(LCPLL_CTL);
7975 val |= LCPLL_POWER_DOWN_ALLOW;
7976 I915_WRITE(LCPLL_CTL, val);
7977 POSTING_READ(LCPLL_CTL);
7978 }
7979}
7980
7981/*
7982 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7983 * source.
7984 */
6ff58d53 7985static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7986{
7987 uint32_t val;
7988
7989 val = I915_READ(LCPLL_CTL);
7990
7991 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7992 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7993 return;
7994
a8a8bd54
PZ
7995 /*
7996 * Make sure we're not on PC8 state before disabling PC8, otherwise
7997 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 7998 */
59bad947 7999 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8000
be256dc7
PZ
8001 if (val & LCPLL_POWER_DOWN_ALLOW) {
8002 val &= ~LCPLL_POWER_DOWN_ALLOW;
8003 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8004 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8005 }
8006
9ccd5aeb 8007 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8008 val |= D_COMP_COMP_FORCE;
8009 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8010 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8011
8012 val = I915_READ(LCPLL_CTL);
8013 val &= ~LCPLL_PLL_DISABLE;
8014 I915_WRITE(LCPLL_CTL, val);
8015
8016 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8017 DRM_ERROR("LCPLL not locked yet\n");
8018
8019 if (val & LCPLL_CD_SOURCE_FCLK) {
8020 val = I915_READ(LCPLL_CTL);
8021 val &= ~LCPLL_CD_SOURCE_FCLK;
8022 I915_WRITE(LCPLL_CTL, val);
8023
8024 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8025 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8026 DRM_ERROR("Switching back to LCPLL failed\n");
8027 }
215733fa 8028
59bad947 8029 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
8030}
8031
765dab67
PZ
8032/*
8033 * Package states C8 and deeper are really deep PC states that can only be
8034 * reached when all the devices on the system allow it, so even if the graphics
8035 * device allows PC8+, it doesn't mean the system will actually get to these
8036 * states. Our driver only allows PC8+ when going into runtime PM.
8037 *
8038 * The requirements for PC8+ are that all the outputs are disabled, the power
8039 * well is disabled and most interrupts are disabled, and these are also
8040 * requirements for runtime PM. When these conditions are met, we manually do
8041 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8042 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8043 * hang the machine.
8044 *
8045 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8046 * the state of some registers, so when we come back from PC8+ we need to
8047 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8048 * need to take care of the registers kept by RC6. Notice that this happens even
8049 * if we don't put the device in PCI D3 state (which is what currently happens
8050 * because of the runtime PM support).
8051 *
8052 * For more, read "Display Sequences for Package C8" on the hardware
8053 * documentation.
8054 */
a14cb6fc 8055void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8056{
c67a470b
PZ
8057 struct drm_device *dev = dev_priv->dev;
8058 uint32_t val;
8059
c67a470b
PZ
8060 DRM_DEBUG_KMS("Enabling package C8+\n");
8061
c67a470b
PZ
8062 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8063 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8064 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8065 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8066 }
8067
8068 lpt_disable_clkout_dp(dev);
c67a470b
PZ
8069 hsw_disable_lcpll(dev_priv, true, true);
8070}
8071
a14cb6fc 8072void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
8073{
8074 struct drm_device *dev = dev_priv->dev;
8075 uint32_t val;
8076
c67a470b
PZ
8077 DRM_DEBUG_KMS("Disabling package C8+\n");
8078
8079 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
8080 lpt_init_pch_refclk(dev);
8081
8082 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8083 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8084 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8085 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8086 }
8087
8088 intel_prepare_ddi(dev);
c67a470b
PZ
8089}
8090
190f68c5
ACO
8091static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8092 struct intel_crtc_state *crtc_state)
09b4ddf9 8093{
190f68c5 8094 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 8095 return -EINVAL;
716c2e55 8096
c7653199 8097 crtc->lowfreq_avail = false;
644cef34 8098
c8f7a0db 8099 return 0;
79e53945
JB
8100}
8101
96b7dfb7
S
8102static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8103 enum port port,
5cec258b 8104 struct intel_crtc_state *pipe_config)
96b7dfb7 8105{
3148ade7 8106 u32 temp, dpll_ctl1;
96b7dfb7
S
8107
8108 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8109 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8110
8111 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
8112 case SKL_DPLL0:
8113 /*
8114 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8115 * of the shared DPLL framework and thus needs to be read out
8116 * separately
8117 */
8118 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8119 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8120 break;
96b7dfb7
S
8121 case SKL_DPLL1:
8122 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8123 break;
8124 case SKL_DPLL2:
8125 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8126 break;
8127 case SKL_DPLL3:
8128 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8129 break;
96b7dfb7
S
8130 }
8131}
8132
7d2c8175
DL
8133static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8134 enum port port,
5cec258b 8135 struct intel_crtc_state *pipe_config)
7d2c8175
DL
8136{
8137 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8138
8139 switch (pipe_config->ddi_pll_sel) {
8140 case PORT_CLK_SEL_WRPLL1:
8141 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8142 break;
8143 case PORT_CLK_SEL_WRPLL2:
8144 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8145 break;
8146 }
8147}
8148
26804afd 8149static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 8150 struct intel_crtc_state *pipe_config)
26804afd
DV
8151{
8152 struct drm_device *dev = crtc->base.dev;
8153 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 8154 struct intel_shared_dpll *pll;
26804afd
DV
8155 enum port port;
8156 uint32_t tmp;
8157
8158 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8159
8160 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8161
96b7dfb7
S
8162 if (IS_SKYLAKE(dev))
8163 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8164 else
8165 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 8166
d452c5b6
DV
8167 if (pipe_config->shared_dpll >= 0) {
8168 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8169
8170 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8171 &pipe_config->dpll_hw_state));
8172 }
8173
26804afd
DV
8174 /*
8175 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8176 * DDI E. So just check whether this pipe is wired to DDI E and whether
8177 * the PCH transcoder is on.
8178 */
ca370455
DL
8179 if (INTEL_INFO(dev)->gen < 9 &&
8180 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
8181 pipe_config->has_pch_encoder = true;
8182
8183 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8184 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8185 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8186
8187 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8188 }
8189}
8190
0e8ffe1b 8191static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8192 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8193{
8194 struct drm_device *dev = crtc->base.dev;
8195 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 8196 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
8197 uint32_t tmp;
8198
f458ebbc 8199 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
8200 POWER_DOMAIN_PIPE(crtc->pipe)))
8201 return false;
8202
e143a21c 8203 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
8204 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8205
eccb140b
DV
8206 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8207 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8208 enum pipe trans_edp_pipe;
8209 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8210 default:
8211 WARN(1, "unknown pipe linked to edp transcoder\n");
8212 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8213 case TRANS_DDI_EDP_INPUT_A_ON:
8214 trans_edp_pipe = PIPE_A;
8215 break;
8216 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8217 trans_edp_pipe = PIPE_B;
8218 break;
8219 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8220 trans_edp_pipe = PIPE_C;
8221 break;
8222 }
8223
8224 if (trans_edp_pipe == crtc->pipe)
8225 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8226 }
8227
f458ebbc 8228 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 8229 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
8230 return false;
8231
eccb140b 8232 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
8233 if (!(tmp & PIPECONF_ENABLE))
8234 return false;
8235
26804afd 8236 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 8237
1bd1bd80
DV
8238 intel_get_pipe_timings(crtc, pipe_config);
8239
2fa2fe9a 8240 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f
JB
8241 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8242 if (IS_SKYLAKE(dev))
8243 skylake_get_pfit_config(crtc, pipe_config);
8244 else
8245 ironlake_get_pfit_config(crtc, pipe_config);
8246 }
88adfff1 8247
e59150dc
JB
8248 if (IS_HASWELL(dev))
8249 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8250 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 8251
ebb69c95
CT
8252 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8253 pipe_config->pixel_multiplier =
8254 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8255 } else {
8256 pipe_config->pixel_multiplier = 1;
8257 }
6c49f241 8258
0e8ffe1b
DV
8259 return true;
8260}
8261
560b85bb
CW
8262static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8263{
8264 struct drm_device *dev = crtc->dev;
8265 struct drm_i915_private *dev_priv = dev->dev_private;
8266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8267 uint32_t cntl = 0, size = 0;
560b85bb 8268
dc41c154
VS
8269 if (base) {
8270 unsigned int width = intel_crtc->cursor_width;
8271 unsigned int height = intel_crtc->cursor_height;
8272 unsigned int stride = roundup_pow_of_two(width) * 4;
8273
8274 switch (stride) {
8275 default:
8276 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8277 width, stride);
8278 stride = 256;
8279 /* fallthrough */
8280 case 256:
8281 case 512:
8282 case 1024:
8283 case 2048:
8284 break;
4b0e333e
CW
8285 }
8286
dc41c154
VS
8287 cntl |= CURSOR_ENABLE |
8288 CURSOR_GAMMA_ENABLE |
8289 CURSOR_FORMAT_ARGB |
8290 CURSOR_STRIDE(stride);
8291
8292 size = (height << 12) | width;
4b0e333e 8293 }
560b85bb 8294
dc41c154
VS
8295 if (intel_crtc->cursor_cntl != 0 &&
8296 (intel_crtc->cursor_base != base ||
8297 intel_crtc->cursor_size != size ||
8298 intel_crtc->cursor_cntl != cntl)) {
8299 /* On these chipsets we can only modify the base/size/stride
8300 * whilst the cursor is disabled.
8301 */
8302 I915_WRITE(_CURACNTR, 0);
4b0e333e 8303 POSTING_READ(_CURACNTR);
dc41c154 8304 intel_crtc->cursor_cntl = 0;
4b0e333e 8305 }
560b85bb 8306
99d1f387 8307 if (intel_crtc->cursor_base != base) {
9db4a9c7 8308 I915_WRITE(_CURABASE, base);
99d1f387
VS
8309 intel_crtc->cursor_base = base;
8310 }
4726e0b0 8311
dc41c154
VS
8312 if (intel_crtc->cursor_size != size) {
8313 I915_WRITE(CURSIZE, size);
8314 intel_crtc->cursor_size = size;
4b0e333e 8315 }
560b85bb 8316
4b0e333e 8317 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8318 I915_WRITE(_CURACNTR, cntl);
8319 POSTING_READ(_CURACNTR);
4b0e333e 8320 intel_crtc->cursor_cntl = cntl;
560b85bb 8321 }
560b85bb
CW
8322}
8323
560b85bb 8324static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8325{
8326 struct drm_device *dev = crtc->dev;
8327 struct drm_i915_private *dev_priv = dev->dev_private;
8328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8329 int pipe = intel_crtc->pipe;
4b0e333e
CW
8330 uint32_t cntl;
8331
8332 cntl = 0;
8333 if (base) {
8334 cntl = MCURSOR_GAMMA_ENABLE;
8335 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8336 case 64:
8337 cntl |= CURSOR_MODE_64_ARGB_AX;
8338 break;
8339 case 128:
8340 cntl |= CURSOR_MODE_128_ARGB_AX;
8341 break;
8342 case 256:
8343 cntl |= CURSOR_MODE_256_ARGB_AX;
8344 break;
8345 default:
5f77eeb0 8346 MISSING_CASE(intel_crtc->cursor_width);
4726e0b0 8347 return;
65a21cd6 8348 }
4b0e333e 8349 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8350
8351 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8352 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8353 }
65a21cd6 8354
8e7d688b 8355 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
8356 cntl |= CURSOR_ROTATE_180;
8357
4b0e333e
CW
8358 if (intel_crtc->cursor_cntl != cntl) {
8359 I915_WRITE(CURCNTR(pipe), cntl);
8360 POSTING_READ(CURCNTR(pipe));
8361 intel_crtc->cursor_cntl = cntl;
65a21cd6 8362 }
4b0e333e 8363
65a21cd6 8364 /* and commit changes on next vblank */
5efb3e28
VS
8365 I915_WRITE(CURBASE(pipe), base);
8366 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8367
8368 intel_crtc->cursor_base = base;
65a21cd6
JB
8369}
8370
cda4b7d3 8371/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8372static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8373 bool on)
cda4b7d3
CW
8374{
8375 struct drm_device *dev = crtc->dev;
8376 struct drm_i915_private *dev_priv = dev->dev_private;
8377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8378 int pipe = intel_crtc->pipe;
3d7d6510
MR
8379 int x = crtc->cursor_x;
8380 int y = crtc->cursor_y;
d6e4db15 8381 u32 base = 0, pos = 0;
cda4b7d3 8382
d6e4db15 8383 if (on)
cda4b7d3 8384 base = intel_crtc->cursor_addr;
cda4b7d3 8385
6e3c9717 8386 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
8387 base = 0;
8388
6e3c9717 8389 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
8390 base = 0;
8391
8392 if (x < 0) {
efc9064e 8393 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8394 base = 0;
8395
8396 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8397 x = -x;
8398 }
8399 pos |= x << CURSOR_X_SHIFT;
8400
8401 if (y < 0) {
efc9064e 8402 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8403 base = 0;
8404
8405 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8406 y = -y;
8407 }
8408 pos |= y << CURSOR_Y_SHIFT;
8409
4b0e333e 8410 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8411 return;
8412
5efb3e28
VS
8413 I915_WRITE(CURPOS(pipe), pos);
8414
4398ad45
VS
8415 /* ILK+ do this automagically */
8416 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 8417 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
4398ad45
VS
8418 base += (intel_crtc->cursor_height *
8419 intel_crtc->cursor_width - 1) * 4;
8420 }
8421
8ac54669 8422 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8423 i845_update_cursor(crtc, base);
8424 else
8425 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8426}
8427
dc41c154
VS
8428static bool cursor_size_ok(struct drm_device *dev,
8429 uint32_t width, uint32_t height)
8430{
8431 if (width == 0 || height == 0)
8432 return false;
8433
8434 /*
8435 * 845g/865g are special in that they are only limited by
8436 * the width of their cursors, the height is arbitrary up to
8437 * the precision of the register. Everything else requires
8438 * square cursors, limited to a few power-of-two sizes.
8439 */
8440 if (IS_845G(dev) || IS_I865G(dev)) {
8441 if ((width & 63) != 0)
8442 return false;
8443
8444 if (width > (IS_845G(dev) ? 64 : 512))
8445 return false;
8446
8447 if (height > 1023)
8448 return false;
8449 } else {
8450 switch (width | height) {
8451 case 256:
8452 case 128:
8453 if (IS_GEN2(dev))
8454 return false;
8455 case 64:
8456 break;
8457 default:
8458 return false;
8459 }
8460 }
8461
8462 return true;
8463}
8464
79e53945 8465static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8466 u16 *blue, uint32_t start, uint32_t size)
79e53945 8467{
7203425a 8468 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8470
7203425a 8471 for (i = start; i < end; i++) {
79e53945
JB
8472 intel_crtc->lut_r[i] = red[i] >> 8;
8473 intel_crtc->lut_g[i] = green[i] >> 8;
8474 intel_crtc->lut_b[i] = blue[i] >> 8;
8475 }
8476
8477 intel_crtc_load_lut(crtc);
8478}
8479
79e53945
JB
8480/* VESA 640x480x72Hz mode to set on the pipe */
8481static struct drm_display_mode load_detect_mode = {
8482 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8483 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8484};
8485
a8bb6818
DV
8486struct drm_framebuffer *
8487__intel_framebuffer_create(struct drm_device *dev,
8488 struct drm_mode_fb_cmd2 *mode_cmd,
8489 struct drm_i915_gem_object *obj)
d2dff872
CW
8490{
8491 struct intel_framebuffer *intel_fb;
8492 int ret;
8493
8494 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8495 if (!intel_fb) {
6ccb81f2 8496 drm_gem_object_unreference(&obj->base);
d2dff872
CW
8497 return ERR_PTR(-ENOMEM);
8498 }
8499
8500 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8501 if (ret)
8502 goto err;
d2dff872
CW
8503
8504 return &intel_fb->base;
dd4916c5 8505err:
6ccb81f2 8506 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
8507 kfree(intel_fb);
8508
8509 return ERR_PTR(ret);
d2dff872
CW
8510}
8511
b5ea642a 8512static struct drm_framebuffer *
a8bb6818
DV
8513intel_framebuffer_create(struct drm_device *dev,
8514 struct drm_mode_fb_cmd2 *mode_cmd,
8515 struct drm_i915_gem_object *obj)
8516{
8517 struct drm_framebuffer *fb;
8518 int ret;
8519
8520 ret = i915_mutex_lock_interruptible(dev);
8521 if (ret)
8522 return ERR_PTR(ret);
8523 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8524 mutex_unlock(&dev->struct_mutex);
8525
8526 return fb;
8527}
8528
d2dff872
CW
8529static u32
8530intel_framebuffer_pitch_for_width(int width, int bpp)
8531{
8532 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8533 return ALIGN(pitch, 64);
8534}
8535
8536static u32
8537intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8538{
8539 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8540 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8541}
8542
8543static struct drm_framebuffer *
8544intel_framebuffer_create_for_mode(struct drm_device *dev,
8545 struct drm_display_mode *mode,
8546 int depth, int bpp)
8547{
8548 struct drm_i915_gem_object *obj;
0fed39bd 8549 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8550
8551 obj = i915_gem_alloc_object(dev,
8552 intel_framebuffer_size_for_mode(mode, bpp));
8553 if (obj == NULL)
8554 return ERR_PTR(-ENOMEM);
8555
8556 mode_cmd.width = mode->hdisplay;
8557 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8558 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8559 bpp);
5ca0c34a 8560 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8561
8562 return intel_framebuffer_create(dev, &mode_cmd, obj);
8563}
8564
8565static struct drm_framebuffer *
8566mode_fits_in_fbdev(struct drm_device *dev,
8567 struct drm_display_mode *mode)
8568{
4520f53a 8569#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8570 struct drm_i915_private *dev_priv = dev->dev_private;
8571 struct drm_i915_gem_object *obj;
8572 struct drm_framebuffer *fb;
8573
4c0e5528 8574 if (!dev_priv->fbdev)
d2dff872
CW
8575 return NULL;
8576
4c0e5528 8577 if (!dev_priv->fbdev->fb)
d2dff872
CW
8578 return NULL;
8579
4c0e5528
DV
8580 obj = dev_priv->fbdev->fb->obj;
8581 BUG_ON(!obj);
8582
8bcd4553 8583 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8584 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8585 fb->bits_per_pixel))
d2dff872
CW
8586 return NULL;
8587
01f2c773 8588 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8589 return NULL;
8590
8591 return fb;
4520f53a
DV
8592#else
8593 return NULL;
8594#endif
d2dff872
CW
8595}
8596
d2434ab7 8597bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8598 struct drm_display_mode *mode,
51fd371b
RC
8599 struct intel_load_detect_pipe *old,
8600 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8601{
8602 struct intel_crtc *intel_crtc;
d2434ab7
DV
8603 struct intel_encoder *intel_encoder =
8604 intel_attached_encoder(connector);
79e53945 8605 struct drm_crtc *possible_crtc;
4ef69c7a 8606 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8607 struct drm_crtc *crtc = NULL;
8608 struct drm_device *dev = encoder->dev;
94352cf9 8609 struct drm_framebuffer *fb;
51fd371b
RC
8610 struct drm_mode_config *config = &dev->mode_config;
8611 int ret, i = -1;
79e53945 8612
d2dff872 8613 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8614 connector->base.id, connector->name,
8e329a03 8615 encoder->base.id, encoder->name);
d2dff872 8616
51fd371b
RC
8617retry:
8618 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8619 if (ret)
8620 goto fail_unlock;
6e9f798d 8621
79e53945
JB
8622 /*
8623 * Algorithm gets a little messy:
7a5e4805 8624 *
79e53945
JB
8625 * - if the connector already has an assigned crtc, use it (but make
8626 * sure it's on first)
7a5e4805 8627 *
79e53945
JB
8628 * - try to find the first unused crtc that can drive this connector,
8629 * and use that if we find one
79e53945
JB
8630 */
8631
8632 /* See if we already have a CRTC for this connector */
8633 if (encoder->crtc) {
8634 crtc = encoder->crtc;
8261b191 8635
51fd371b 8636 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
8637 if (ret)
8638 goto fail_unlock;
8639 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
8640 if (ret)
8641 goto fail_unlock;
7b24056b 8642
24218aac 8643 old->dpms_mode = connector->dpms;
8261b191
CW
8644 old->load_detect_temp = false;
8645
8646 /* Make sure the crtc and connector are running */
24218aac
DV
8647 if (connector->dpms != DRM_MODE_DPMS_ON)
8648 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8649
7173188d 8650 return true;
79e53945
JB
8651 }
8652
8653 /* Find an unused one (if possible) */
70e1e0ec 8654 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8655 i++;
8656 if (!(encoder->possible_crtcs & (1 << i)))
8657 continue;
a459249c
VS
8658 if (possible_crtc->enabled)
8659 continue;
8660 /* This can occur when applying the pipe A quirk on resume. */
8661 if (to_intel_crtc(possible_crtc)->new_enabled)
8662 continue;
8663
8664 crtc = possible_crtc;
8665 break;
79e53945
JB
8666 }
8667
8668 /*
8669 * If we didn't find an unused CRTC, don't use any.
8670 */
8671 if (!crtc) {
7173188d 8672 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8673 goto fail_unlock;
79e53945
JB
8674 }
8675
51fd371b
RC
8676 ret = drm_modeset_lock(&crtc->mutex, ctx);
8677 if (ret)
4d02e2de
DV
8678 goto fail_unlock;
8679 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8680 if (ret)
51fd371b 8681 goto fail_unlock;
fc303101
DV
8682 intel_encoder->new_crtc = to_intel_crtc(crtc);
8683 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8684
8685 intel_crtc = to_intel_crtc(crtc);
412b61d8 8686 intel_crtc->new_enabled = true;
6e3c9717 8687 intel_crtc->new_config = intel_crtc->config;
24218aac 8688 old->dpms_mode = connector->dpms;
8261b191 8689 old->load_detect_temp = true;
d2dff872 8690 old->release_fb = NULL;
79e53945 8691
6492711d
CW
8692 if (!mode)
8693 mode = &load_detect_mode;
79e53945 8694
d2dff872
CW
8695 /* We need a framebuffer large enough to accommodate all accesses
8696 * that the plane may generate whilst we perform load detection.
8697 * We can not rely on the fbcon either being present (we get called
8698 * during its initialisation to detect all boot displays, or it may
8699 * not even exist) or that it is large enough to satisfy the
8700 * requested mode.
8701 */
94352cf9
DV
8702 fb = mode_fits_in_fbdev(dev, mode);
8703 if (fb == NULL) {
d2dff872 8704 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8705 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8706 old->release_fb = fb;
d2dff872
CW
8707 } else
8708 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8709 if (IS_ERR(fb)) {
d2dff872 8710 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8711 goto fail;
79e53945 8712 }
79e53945 8713
c0c36b94 8714 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8715 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8716 if (old->release_fb)
8717 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8718 goto fail;
79e53945 8719 }
7173188d 8720
79e53945 8721 /* let the connector get through one full cycle before testing */
9d0498a2 8722 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8723 return true;
412b61d8
VS
8724
8725 fail:
8726 intel_crtc->new_enabled = crtc->enabled;
8727 if (intel_crtc->new_enabled)
6e3c9717 8728 intel_crtc->new_config = intel_crtc->config;
412b61d8
VS
8729 else
8730 intel_crtc->new_config = NULL;
51fd371b
RC
8731fail_unlock:
8732 if (ret == -EDEADLK) {
8733 drm_modeset_backoff(ctx);
8734 goto retry;
8735 }
8736
412b61d8 8737 return false;
79e53945
JB
8738}
8739
d2434ab7 8740void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8741 struct intel_load_detect_pipe *old)
79e53945 8742{
d2434ab7
DV
8743 struct intel_encoder *intel_encoder =
8744 intel_attached_encoder(connector);
4ef69c7a 8745 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8746 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8748
d2dff872 8749 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8750 connector->base.id, connector->name,
8e329a03 8751 encoder->base.id, encoder->name);
d2dff872 8752
8261b191 8753 if (old->load_detect_temp) {
fc303101
DV
8754 to_intel_connector(connector)->new_encoder = NULL;
8755 intel_encoder->new_crtc = NULL;
412b61d8
VS
8756 intel_crtc->new_enabled = false;
8757 intel_crtc->new_config = NULL;
fc303101 8758 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8759
36206361
DV
8760 if (old->release_fb) {
8761 drm_framebuffer_unregister_private(old->release_fb);
8762 drm_framebuffer_unreference(old->release_fb);
8763 }
d2dff872 8764
0622a53c 8765 return;
79e53945
JB
8766 }
8767
c751ce4f 8768 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8769 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8770 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8771}
8772
da4a1efa 8773static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 8774 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
8775{
8776 struct drm_i915_private *dev_priv = dev->dev_private;
8777 u32 dpll = pipe_config->dpll_hw_state.dpll;
8778
8779 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8780 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8781 else if (HAS_PCH_SPLIT(dev))
8782 return 120000;
8783 else if (!IS_GEN2(dev))
8784 return 96000;
8785 else
8786 return 48000;
8787}
8788
79e53945 8789/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 8790static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8791 struct intel_crtc_state *pipe_config)
79e53945 8792{
f1f644dc 8793 struct drm_device *dev = crtc->base.dev;
79e53945 8794 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8795 int pipe = pipe_config->cpu_transcoder;
293623f7 8796 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8797 u32 fp;
8798 intel_clock_t clock;
da4a1efa 8799 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8800
8801 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8802 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8803 else
293623f7 8804 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8805
8806 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8807 if (IS_PINEVIEW(dev)) {
8808 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8809 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8810 } else {
8811 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8812 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8813 }
8814
a6c45cf0 8815 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8816 if (IS_PINEVIEW(dev))
8817 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8818 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8819 else
8820 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8821 DPLL_FPA01_P1_POST_DIV_SHIFT);
8822
8823 switch (dpll & DPLL_MODE_MASK) {
8824 case DPLLB_MODE_DAC_SERIAL:
8825 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8826 5 : 10;
8827 break;
8828 case DPLLB_MODE_LVDS:
8829 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8830 7 : 14;
8831 break;
8832 default:
28c97730 8833 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8834 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8835 return;
79e53945
JB
8836 }
8837
ac58c3f0 8838 if (IS_PINEVIEW(dev))
da4a1efa 8839 pineview_clock(refclk, &clock);
ac58c3f0 8840 else
da4a1efa 8841 i9xx_clock(refclk, &clock);
79e53945 8842 } else {
0fb58223 8843 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8844 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8845
8846 if (is_lvds) {
8847 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8848 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8849
8850 if (lvds & LVDS_CLKB_POWER_UP)
8851 clock.p2 = 7;
8852 else
8853 clock.p2 = 14;
79e53945
JB
8854 } else {
8855 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8856 clock.p1 = 2;
8857 else {
8858 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8859 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8860 }
8861 if (dpll & PLL_P2_DIVIDE_BY_4)
8862 clock.p2 = 4;
8863 else
8864 clock.p2 = 2;
79e53945 8865 }
da4a1efa
VS
8866
8867 i9xx_clock(refclk, &clock);
79e53945
JB
8868 }
8869
18442d08
VS
8870 /*
8871 * This value includes pixel_multiplier. We will use
241bfc38 8872 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8873 * encoder's get_config() function.
8874 */
8875 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8876}
8877
6878da05
VS
8878int intel_dotclock_calculate(int link_freq,
8879 const struct intel_link_m_n *m_n)
f1f644dc 8880{
f1f644dc
JB
8881 /*
8882 * The calculation for the data clock is:
1041a02f 8883 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8884 * But we want to avoid losing precison if possible, so:
1041a02f 8885 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8886 *
8887 * and the link clock is simpler:
1041a02f 8888 * link_clock = (m * link_clock) / n
f1f644dc
JB
8889 */
8890
6878da05
VS
8891 if (!m_n->link_n)
8892 return 0;
f1f644dc 8893
6878da05
VS
8894 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8895}
f1f644dc 8896
18442d08 8897static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 8898 struct intel_crtc_state *pipe_config)
6878da05
VS
8899{
8900 struct drm_device *dev = crtc->base.dev;
79e53945 8901
18442d08
VS
8902 /* read out port_clock from the DPLL */
8903 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8904
f1f644dc 8905 /*
18442d08 8906 * This value does not include pixel_multiplier.
241bfc38 8907 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8908 * agree once we know their relationship in the encoder's
8909 * get_config() function.
79e53945 8910 */
2d112de7 8911 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
8912 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8913 &pipe_config->fdi_m_n);
79e53945
JB
8914}
8915
8916/** Returns the currently programmed mode of the given pipe. */
8917struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8918 struct drm_crtc *crtc)
8919{
548f245b 8920 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8921 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8922 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 8923 struct drm_display_mode *mode;
5cec258b 8924 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
8925 int htot = I915_READ(HTOTAL(cpu_transcoder));
8926 int hsync = I915_READ(HSYNC(cpu_transcoder));
8927 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8928 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8929 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8930
8931 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8932 if (!mode)
8933 return NULL;
8934
f1f644dc
JB
8935 /*
8936 * Construct a pipe_config sufficient for getting the clock info
8937 * back out of crtc_clock_get.
8938 *
8939 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8940 * to use a real value here instead.
8941 */
293623f7 8942 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8943 pipe_config.pixel_multiplier = 1;
293623f7
VS
8944 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8945 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8946 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8947 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8948
773ae034 8949 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8950 mode->hdisplay = (htot & 0xffff) + 1;
8951 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8952 mode->hsync_start = (hsync & 0xffff) + 1;
8953 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8954 mode->vdisplay = (vtot & 0xffff) + 1;
8955 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8956 mode->vsync_start = (vsync & 0xffff) + 1;
8957 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8958
8959 drm_mode_set_name(mode);
79e53945
JB
8960
8961 return mode;
8962}
8963
652c393a
JB
8964static void intel_decrease_pllclock(struct drm_crtc *crtc)
8965{
8966 struct drm_device *dev = crtc->dev;
fbee40df 8967 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8969
baff296c 8970 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8971 return;
8972
8973 if (!dev_priv->lvds_downclock_avail)
8974 return;
8975
8976 /*
8977 * Since this is called by a timer, we should never get here in
8978 * the manual case.
8979 */
8980 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8981 int pipe = intel_crtc->pipe;
8982 int dpll_reg = DPLL(pipe);
8983 int dpll;
f6e5b160 8984
44d98a61 8985 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8986
8ac5a6d5 8987 assert_panel_unlocked(dev_priv, pipe);
652c393a 8988
dc257cf1 8989 dpll = I915_READ(dpll_reg);
652c393a
JB
8990 dpll |= DISPLAY_RATE_SELECT_FPA1;
8991 I915_WRITE(dpll_reg, dpll);
9d0498a2 8992 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8993 dpll = I915_READ(dpll_reg);
8994 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8995 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8996 }
8997
8998}
8999
f047e395
CW
9000void intel_mark_busy(struct drm_device *dev)
9001{
c67a470b
PZ
9002 struct drm_i915_private *dev_priv = dev->dev_private;
9003
f62a0076
CW
9004 if (dev_priv->mm.busy)
9005 return;
9006
43694d69 9007 intel_runtime_pm_get(dev_priv);
c67a470b 9008 i915_update_gfx_val(dev_priv);
f62a0076 9009 dev_priv->mm.busy = true;
f047e395
CW
9010}
9011
9012void intel_mark_idle(struct drm_device *dev)
652c393a 9013{
c67a470b 9014 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9015 struct drm_crtc *crtc;
652c393a 9016
f62a0076
CW
9017 if (!dev_priv->mm.busy)
9018 return;
9019
9020 dev_priv->mm.busy = false;
9021
d330a953 9022 if (!i915.powersave)
bb4cdd53 9023 goto out;
652c393a 9024
70e1e0ec 9025 for_each_crtc(dev, crtc) {
f4510a27 9026 if (!crtc->primary->fb)
652c393a
JB
9027 continue;
9028
725a5b54 9029 intel_decrease_pllclock(crtc);
652c393a 9030 }
b29c19b6 9031
3d13ef2e 9032 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9033 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9034
9035out:
43694d69 9036 intel_runtime_pm_put(dev_priv);
652c393a
JB
9037}
9038
f5de6e07
ACO
9039static void intel_crtc_set_state(struct intel_crtc *crtc,
9040 struct intel_crtc_state *crtc_state)
9041{
9042 kfree(crtc->config);
9043 crtc->config = crtc_state;
16f3f658 9044 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
9045}
9046
79e53945
JB
9047static void intel_crtc_destroy(struct drm_crtc *crtc)
9048{
9049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9050 struct drm_device *dev = crtc->dev;
9051 struct intel_unpin_work *work;
67e77c5a 9052
5e2d7afc 9053 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
9054 work = intel_crtc->unpin_work;
9055 intel_crtc->unpin_work = NULL;
5e2d7afc 9056 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
9057
9058 if (work) {
9059 cancel_work_sync(&work->work);
9060 kfree(work);
9061 }
79e53945 9062
f5de6e07 9063 intel_crtc_set_state(intel_crtc, NULL);
79e53945 9064 drm_crtc_cleanup(crtc);
67e77c5a 9065
79e53945
JB
9066 kfree(intel_crtc);
9067}
9068
6b95a207
KH
9069static void intel_unpin_work_fn(struct work_struct *__work)
9070{
9071 struct intel_unpin_work *work =
9072 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9073 struct drm_device *dev = work->crtc->dev;
f99d7069 9074 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9075
b4a98e57 9076 mutex_lock(&dev->struct_mutex);
ab8d6675 9077 intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
05394f39 9078 drm_gem_object_unreference(&work->pending_flip_obj->base);
ab8d6675 9079 drm_framebuffer_unreference(work->old_fb);
d9e86c0e 9080
7ff0ebcc 9081 intel_fbc_update(dev);
f06cc1b9
JH
9082
9083 if (work->flip_queued_req)
146d84f0 9084 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
9085 mutex_unlock(&dev->struct_mutex);
9086
f99d7069
DV
9087 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9088
b4a98e57
CW
9089 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9090 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9091
6b95a207
KH
9092 kfree(work);
9093}
9094
1afe3e9d 9095static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9096 struct drm_crtc *crtc)
6b95a207 9097{
6b95a207
KH
9098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9099 struct intel_unpin_work *work;
6b95a207
KH
9100 unsigned long flags;
9101
9102 /* Ignore early vblank irqs */
9103 if (intel_crtc == NULL)
9104 return;
9105
f326038a
DV
9106 /*
9107 * This is called both by irq handlers and the reset code (to complete
9108 * lost pageflips) so needs the full irqsave spinlocks.
9109 */
6b95a207
KH
9110 spin_lock_irqsave(&dev->event_lock, flags);
9111 work = intel_crtc->unpin_work;
e7d841ca
CW
9112
9113 /* Ensure we don't miss a work->pending update ... */
9114 smp_rmb();
9115
9116 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9117 spin_unlock_irqrestore(&dev->event_lock, flags);
9118 return;
9119 }
9120
d6bbafa1 9121 page_flip_completed(intel_crtc);
0af7e4df 9122
6b95a207 9123 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9124}
9125
1afe3e9d
JB
9126void intel_finish_page_flip(struct drm_device *dev, int pipe)
9127{
fbee40df 9128 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9129 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9130
49b14a5c 9131 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9132}
9133
9134void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9135{
fbee40df 9136 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9137 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9138
49b14a5c 9139 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9140}
9141
75f7f3ec
VS
9142/* Is 'a' after or equal to 'b'? */
9143static bool g4x_flip_count_after_eq(u32 a, u32 b)
9144{
9145 return !((a - b) & 0x80000000);
9146}
9147
9148static bool page_flip_finished(struct intel_crtc *crtc)
9149{
9150 struct drm_device *dev = crtc->base.dev;
9151 struct drm_i915_private *dev_priv = dev->dev_private;
9152
bdfa7542
VS
9153 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9154 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9155 return true;
9156
75f7f3ec
VS
9157 /*
9158 * The relevant registers doen't exist on pre-ctg.
9159 * As the flip done interrupt doesn't trigger for mmio
9160 * flips on gmch platforms, a flip count check isn't
9161 * really needed there. But since ctg has the registers,
9162 * include it in the check anyway.
9163 */
9164 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9165 return true;
9166
9167 /*
9168 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9169 * used the same base address. In that case the mmio flip might
9170 * have completed, but the CS hasn't even executed the flip yet.
9171 *
9172 * A flip count check isn't enough as the CS might have updated
9173 * the base address just after start of vblank, but before we
9174 * managed to process the interrupt. This means we'd complete the
9175 * CS flip too soon.
9176 *
9177 * Combining both checks should get us a good enough result. It may
9178 * still happen that the CS flip has been executed, but has not
9179 * yet actually completed. But in case the base address is the same
9180 * anyway, we don't really care.
9181 */
9182 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9183 crtc->unpin_work->gtt_offset &&
9184 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9185 crtc->unpin_work->flip_count);
9186}
9187
6b95a207
KH
9188void intel_prepare_page_flip(struct drm_device *dev, int plane)
9189{
fbee40df 9190 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9191 struct intel_crtc *intel_crtc =
9192 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9193 unsigned long flags;
9194
f326038a
DV
9195
9196 /*
9197 * This is called both by irq handlers and the reset code (to complete
9198 * lost pageflips) so needs the full irqsave spinlocks.
9199 *
9200 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
9201 * generate a page-flip completion irq, i.e. every modeset
9202 * is also accompanied by a spurious intel_prepare_page_flip().
9203 */
6b95a207 9204 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9205 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9206 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9207 spin_unlock_irqrestore(&dev->event_lock, flags);
9208}
9209
eba905b2 9210static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9211{
9212 /* Ensure that the work item is consistent when activating it ... */
9213 smp_wmb();
9214 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9215 /* and that it is marked active as soon as the irq could fire. */
9216 smp_wmb();
9217}
9218
8c9f3aaf
JB
9219static int intel_gen2_queue_flip(struct drm_device *dev,
9220 struct drm_crtc *crtc,
9221 struct drm_framebuffer *fb,
ed8d1975 9222 struct drm_i915_gem_object *obj,
a4872ba6 9223 struct intel_engine_cs *ring,
ed8d1975 9224 uint32_t flags)
8c9f3aaf 9225{
8c9f3aaf 9226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9227 u32 flip_mask;
9228 int ret;
9229
6d90c952 9230 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9231 if (ret)
4fa62c89 9232 return ret;
8c9f3aaf
JB
9233
9234 /* Can't queue multiple flips, so wait for the previous
9235 * one to finish before executing the next.
9236 */
9237 if (intel_crtc->plane)
9238 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9239 else
9240 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9241 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9242 intel_ring_emit(ring, MI_NOOP);
9243 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9244 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9245 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9246 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9247 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9248
9249 intel_mark_page_flip_active(intel_crtc);
09246732 9250 __intel_ring_advance(ring);
83d4092b 9251 return 0;
8c9f3aaf
JB
9252}
9253
9254static int intel_gen3_queue_flip(struct drm_device *dev,
9255 struct drm_crtc *crtc,
9256 struct drm_framebuffer *fb,
ed8d1975 9257 struct drm_i915_gem_object *obj,
a4872ba6 9258 struct intel_engine_cs *ring,
ed8d1975 9259 uint32_t flags)
8c9f3aaf 9260{
8c9f3aaf 9261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9262 u32 flip_mask;
9263 int ret;
9264
6d90c952 9265 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9266 if (ret)
4fa62c89 9267 return ret;
8c9f3aaf
JB
9268
9269 if (intel_crtc->plane)
9270 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9271 else
9272 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9273 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9274 intel_ring_emit(ring, MI_NOOP);
9275 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9276 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9277 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9278 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9279 intel_ring_emit(ring, MI_NOOP);
9280
e7d841ca 9281 intel_mark_page_flip_active(intel_crtc);
09246732 9282 __intel_ring_advance(ring);
83d4092b 9283 return 0;
8c9f3aaf
JB
9284}
9285
9286static int intel_gen4_queue_flip(struct drm_device *dev,
9287 struct drm_crtc *crtc,
9288 struct drm_framebuffer *fb,
ed8d1975 9289 struct drm_i915_gem_object *obj,
a4872ba6 9290 struct intel_engine_cs *ring,
ed8d1975 9291 uint32_t flags)
8c9f3aaf
JB
9292{
9293 struct drm_i915_private *dev_priv = dev->dev_private;
9294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9295 uint32_t pf, pipesrc;
9296 int ret;
9297
6d90c952 9298 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9299 if (ret)
4fa62c89 9300 return ret;
8c9f3aaf
JB
9301
9302 /* i965+ uses the linear or tiled offsets from the
9303 * Display Registers (which do not change across a page-flip)
9304 * so we need only reprogram the base address.
9305 */
6d90c952
DV
9306 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9307 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9308 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9309 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9310 obj->tiling_mode);
8c9f3aaf
JB
9311
9312 /* XXX Enabling the panel-fitter across page-flip is so far
9313 * untested on non-native modes, so ignore it for now.
9314 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9315 */
9316 pf = 0;
9317 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9318 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9319
9320 intel_mark_page_flip_active(intel_crtc);
09246732 9321 __intel_ring_advance(ring);
83d4092b 9322 return 0;
8c9f3aaf
JB
9323}
9324
9325static int intel_gen6_queue_flip(struct drm_device *dev,
9326 struct drm_crtc *crtc,
9327 struct drm_framebuffer *fb,
ed8d1975 9328 struct drm_i915_gem_object *obj,
a4872ba6 9329 struct intel_engine_cs *ring,
ed8d1975 9330 uint32_t flags)
8c9f3aaf
JB
9331{
9332 struct drm_i915_private *dev_priv = dev->dev_private;
9333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9334 uint32_t pf, pipesrc;
9335 int ret;
9336
6d90c952 9337 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9338 if (ret)
4fa62c89 9339 return ret;
8c9f3aaf 9340
6d90c952
DV
9341 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9342 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9343 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9344 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9345
dc257cf1
DV
9346 /* Contrary to the suggestions in the documentation,
9347 * "Enable Panel Fitter" does not seem to be required when page
9348 * flipping with a non-native mode, and worse causes a normal
9349 * modeset to fail.
9350 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9351 */
9352 pf = 0;
8c9f3aaf 9353 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9354 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9355
9356 intel_mark_page_flip_active(intel_crtc);
09246732 9357 __intel_ring_advance(ring);
83d4092b 9358 return 0;
8c9f3aaf
JB
9359}
9360
7c9017e5
JB
9361static int intel_gen7_queue_flip(struct drm_device *dev,
9362 struct drm_crtc *crtc,
9363 struct drm_framebuffer *fb,
ed8d1975 9364 struct drm_i915_gem_object *obj,
a4872ba6 9365 struct intel_engine_cs *ring,
ed8d1975 9366 uint32_t flags)
7c9017e5 9367{
7c9017e5 9368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9369 uint32_t plane_bit = 0;
ffe74d75
CW
9370 int len, ret;
9371
eba905b2 9372 switch (intel_crtc->plane) {
cb05d8de
DV
9373 case PLANE_A:
9374 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9375 break;
9376 case PLANE_B:
9377 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9378 break;
9379 case PLANE_C:
9380 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9381 break;
9382 default:
9383 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9384 return -ENODEV;
cb05d8de
DV
9385 }
9386
ffe74d75 9387 len = 4;
f476828a 9388 if (ring->id == RCS) {
ffe74d75 9389 len += 6;
f476828a
DL
9390 /*
9391 * On Gen 8, SRM is now taking an extra dword to accommodate
9392 * 48bits addresses, and we need a NOOP for the batch size to
9393 * stay even.
9394 */
9395 if (IS_GEN8(dev))
9396 len += 2;
9397 }
ffe74d75 9398
f66fab8e
VS
9399 /*
9400 * BSpec MI_DISPLAY_FLIP for IVB:
9401 * "The full packet must be contained within the same cache line."
9402 *
9403 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9404 * cacheline, if we ever start emitting more commands before
9405 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9406 * then do the cacheline alignment, and finally emit the
9407 * MI_DISPLAY_FLIP.
9408 */
9409 ret = intel_ring_cacheline_align(ring);
9410 if (ret)
4fa62c89 9411 return ret;
f66fab8e 9412
ffe74d75 9413 ret = intel_ring_begin(ring, len);
7c9017e5 9414 if (ret)
4fa62c89 9415 return ret;
7c9017e5 9416
ffe74d75
CW
9417 /* Unmask the flip-done completion message. Note that the bspec says that
9418 * we should do this for both the BCS and RCS, and that we must not unmask
9419 * more than one flip event at any time (or ensure that one flip message
9420 * can be sent by waiting for flip-done prior to queueing new flips).
9421 * Experimentation says that BCS works despite DERRMR masking all
9422 * flip-done completion events and that unmasking all planes at once
9423 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9424 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9425 */
9426 if (ring->id == RCS) {
9427 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9428 intel_ring_emit(ring, DERRMR);
9429 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9430 DERRMR_PIPEB_PRI_FLIP_DONE |
9431 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9432 if (IS_GEN8(dev))
9433 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9434 MI_SRM_LRM_GLOBAL_GTT);
9435 else
9436 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9437 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9438 intel_ring_emit(ring, DERRMR);
9439 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9440 if (IS_GEN8(dev)) {
9441 intel_ring_emit(ring, 0);
9442 intel_ring_emit(ring, MI_NOOP);
9443 }
ffe74d75
CW
9444 }
9445
cb05d8de 9446 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9447 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9448 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9449 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9450
9451 intel_mark_page_flip_active(intel_crtc);
09246732 9452 __intel_ring_advance(ring);
83d4092b 9453 return 0;
7c9017e5
JB
9454}
9455
84c33a64
SG
9456static bool use_mmio_flip(struct intel_engine_cs *ring,
9457 struct drm_i915_gem_object *obj)
9458{
9459 /*
9460 * This is not being used for older platforms, because
9461 * non-availability of flip done interrupt forces us to use
9462 * CS flips. Older platforms derive flip done using some clever
9463 * tricks involving the flip_pending status bits and vblank irqs.
9464 * So using MMIO flips there would disrupt this mechanism.
9465 */
9466
8e09bf83
CW
9467 if (ring == NULL)
9468 return true;
9469
84c33a64
SG
9470 if (INTEL_INFO(ring->dev)->gen < 5)
9471 return false;
9472
9473 if (i915.use_mmio_flip < 0)
9474 return false;
9475 else if (i915.use_mmio_flip > 0)
9476 return true;
14bf993e
OM
9477 else if (i915.enable_execlists)
9478 return true;
84c33a64 9479 else
41c52415 9480 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
9481}
9482
ff944564
DL
9483static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9484{
9485 struct drm_device *dev = intel_crtc->base.dev;
9486 struct drm_i915_private *dev_priv = dev->dev_private;
9487 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9488 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9489 struct drm_i915_gem_object *obj = intel_fb->obj;
9490 const enum pipe pipe = intel_crtc->pipe;
9491 u32 ctl, stride;
9492
9493 ctl = I915_READ(PLANE_CTL(pipe, 0));
9494 ctl &= ~PLANE_CTL_TILED_MASK;
9495 if (obj->tiling_mode == I915_TILING_X)
9496 ctl |= PLANE_CTL_TILED_X;
9497
9498 /*
9499 * The stride is either expressed as a multiple of 64 bytes chunks for
9500 * linear buffers or in number of tiles for tiled buffers.
9501 */
9502 stride = fb->pitches[0] >> 6;
9503 if (obj->tiling_mode == I915_TILING_X)
9504 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9505
9506 /*
9507 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9508 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9509 */
9510 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9511 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9512
9513 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9514 POSTING_READ(PLANE_SURF(pipe, 0));
9515}
9516
9517static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
9518{
9519 struct drm_device *dev = intel_crtc->base.dev;
9520 struct drm_i915_private *dev_priv = dev->dev_private;
9521 struct intel_framebuffer *intel_fb =
9522 to_intel_framebuffer(intel_crtc->base.primary->fb);
9523 struct drm_i915_gem_object *obj = intel_fb->obj;
9524 u32 dspcntr;
9525 u32 reg;
9526
84c33a64
SG
9527 reg = DSPCNTR(intel_crtc->plane);
9528 dspcntr = I915_READ(reg);
9529
c5d97472
DL
9530 if (obj->tiling_mode != I915_TILING_NONE)
9531 dspcntr |= DISPPLANE_TILED;
9532 else
9533 dspcntr &= ~DISPPLANE_TILED;
9534
84c33a64
SG
9535 I915_WRITE(reg, dspcntr);
9536
9537 I915_WRITE(DSPSURF(intel_crtc->plane),
9538 intel_crtc->unpin_work->gtt_offset);
9539 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 9540
ff944564
DL
9541}
9542
9543/*
9544 * XXX: This is the temporary way to update the plane registers until we get
9545 * around to using the usual plane update functions for MMIO flips
9546 */
9547static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9548{
9549 struct drm_device *dev = intel_crtc->base.dev;
9550 bool atomic_update;
9551 u32 start_vbl_count;
9552
9553 intel_mark_page_flip_active(intel_crtc);
9554
9555 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9556
9557 if (INTEL_INFO(dev)->gen >= 9)
9558 skl_do_mmio_flip(intel_crtc);
9559 else
9560 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9561 ilk_do_mmio_flip(intel_crtc);
9562
9362c7c5
ACO
9563 if (atomic_update)
9564 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
9565}
9566
9362c7c5 9567static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 9568{
cc8c4cc2 9569 struct intel_crtc *crtc =
9362c7c5 9570 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 9571 struct intel_mmio_flip *mmio_flip;
84c33a64 9572
cc8c4cc2
JH
9573 mmio_flip = &crtc->mmio_flip;
9574 if (mmio_flip->req)
9c654818
JH
9575 WARN_ON(__i915_wait_request(mmio_flip->req,
9576 crtc->reset_counter,
9577 false, NULL, NULL) != 0);
84c33a64 9578
cc8c4cc2
JH
9579 intel_do_mmio_flip(crtc);
9580 if (mmio_flip->req) {
9581 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 9582 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
9583 mutex_unlock(&crtc->base.dev->struct_mutex);
9584 }
84c33a64
SG
9585}
9586
9587static int intel_queue_mmio_flip(struct drm_device *dev,
9588 struct drm_crtc *crtc,
9589 struct drm_framebuffer *fb,
9590 struct drm_i915_gem_object *obj,
9591 struct intel_engine_cs *ring,
9592 uint32_t flags)
9593{
84c33a64 9594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 9595
cc8c4cc2
JH
9596 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9597 obj->last_write_req);
536f5b5e
ACO
9598
9599 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 9600
84c33a64
SG
9601 return 0;
9602}
9603
830c81db
DL
9604static int intel_gen9_queue_flip(struct drm_device *dev,
9605 struct drm_crtc *crtc,
9606 struct drm_framebuffer *fb,
9607 struct drm_i915_gem_object *obj,
9608 struct intel_engine_cs *ring,
9609 uint32_t flags)
9610{
9611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9612 uint32_t plane = 0, stride;
9613 int ret;
9614
9615 switch(intel_crtc->pipe) {
9616 case PIPE_A:
9617 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
9618 break;
9619 case PIPE_B:
9620 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
9621 break;
9622 case PIPE_C:
9623 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
9624 break;
9625 default:
9626 WARN_ONCE(1, "unknown plane in flip command\n");
9627 return -ENODEV;
9628 }
9629
9630 switch (obj->tiling_mode) {
9631 case I915_TILING_NONE:
9632 stride = fb->pitches[0] >> 6;
9633 break;
9634 case I915_TILING_X:
9635 stride = fb->pitches[0] >> 9;
9636 break;
9637 default:
9638 WARN_ONCE(1, "unknown tiling in flip command\n");
9639 return -ENODEV;
9640 }
9641
9642 ret = intel_ring_begin(ring, 10);
9643 if (ret)
9644 return ret;
9645
9646 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9647 intel_ring_emit(ring, DERRMR);
9648 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9649 DERRMR_PIPEB_PRI_FLIP_DONE |
9650 DERRMR_PIPEC_PRI_FLIP_DONE));
9651 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9652 MI_SRM_LRM_GLOBAL_GTT);
9653 intel_ring_emit(ring, DERRMR);
9654 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9655 intel_ring_emit(ring, 0);
9656
9657 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
9658 intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
9659 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9660
9661 intel_mark_page_flip_active(intel_crtc);
9662 __intel_ring_advance(ring);
9663
9664 return 0;
9665}
9666
8c9f3aaf
JB
9667static int intel_default_queue_flip(struct drm_device *dev,
9668 struct drm_crtc *crtc,
9669 struct drm_framebuffer *fb,
ed8d1975 9670 struct drm_i915_gem_object *obj,
a4872ba6 9671 struct intel_engine_cs *ring,
ed8d1975 9672 uint32_t flags)
8c9f3aaf
JB
9673{
9674 return -ENODEV;
9675}
9676
d6bbafa1
CW
9677static bool __intel_pageflip_stall_check(struct drm_device *dev,
9678 struct drm_crtc *crtc)
9679{
9680 struct drm_i915_private *dev_priv = dev->dev_private;
9681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9682 struct intel_unpin_work *work = intel_crtc->unpin_work;
9683 u32 addr;
9684
9685 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9686 return true;
9687
9688 if (!work->enable_stall_check)
9689 return false;
9690
9691 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
9692 if (work->flip_queued_req &&
9693 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
9694 return false;
9695
9696 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9697 }
9698
9699 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9700 return false;
9701
9702 /* Potential stall - if we see that the flip has happened,
9703 * assume a missed interrupt. */
9704 if (INTEL_INFO(dev)->gen >= 4)
9705 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9706 else
9707 addr = I915_READ(DSPADDR(intel_crtc->plane));
9708
9709 /* There is a potential issue here with a false positive after a flip
9710 * to the same address. We could address this by checking for a
9711 * non-incrementing frame counter.
9712 */
9713 return addr == work->gtt_offset;
9714}
9715
9716void intel_check_page_flip(struct drm_device *dev, int pipe)
9717{
9718 struct drm_i915_private *dev_priv = dev->dev_private;
9719 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f326038a
DV
9721
9722 WARN_ON(!in_irq());
d6bbafa1
CW
9723
9724 if (crtc == NULL)
9725 return;
9726
f326038a 9727 spin_lock(&dev->event_lock);
d6bbafa1
CW
9728 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9729 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9730 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9731 page_flip_completed(intel_crtc);
9732 }
f326038a 9733 spin_unlock(&dev->event_lock);
d6bbafa1
CW
9734}
9735
6b95a207
KH
9736static int intel_crtc_page_flip(struct drm_crtc *crtc,
9737 struct drm_framebuffer *fb,
ed8d1975
KP
9738 struct drm_pending_vblank_event *event,
9739 uint32_t page_flip_flags)
6b95a207
KH
9740{
9741 struct drm_device *dev = crtc->dev;
9742 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9743 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 9746 struct drm_plane *primary = crtc->primary;
a071fa00 9747 enum pipe pipe = intel_crtc->pipe;
6b95a207 9748 struct intel_unpin_work *work;
a4872ba6 9749 struct intel_engine_cs *ring;
52e68630 9750 int ret;
6b95a207 9751
2ff8fde1
MR
9752 /*
9753 * drm_mode_page_flip_ioctl() should already catch this, but double
9754 * check to be safe. In the future we may enable pageflipping from
9755 * a disabled primary plane.
9756 */
9757 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9758 return -EBUSY;
9759
e6a595d2 9760 /* Can't change pixel format via MI display flips. */
f4510a27 9761 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9762 return -EINVAL;
9763
9764 /*
9765 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9766 * Note that pitch changes could also affect these register.
9767 */
9768 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9769 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9770 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9771 return -EINVAL;
9772
f900db47
CW
9773 if (i915_terminally_wedged(&dev_priv->gpu_error))
9774 goto out_hang;
9775
b14c5679 9776 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9777 if (work == NULL)
9778 return -ENOMEM;
9779
6b95a207 9780 work->event = event;
b4a98e57 9781 work->crtc = crtc;
ab8d6675 9782 work->old_fb = old_fb;
6b95a207
KH
9783 INIT_WORK(&work->work, intel_unpin_work_fn);
9784
87b6b101 9785 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9786 if (ret)
9787 goto free_work;
9788
6b95a207 9789 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 9790 spin_lock_irq(&dev->event_lock);
6b95a207 9791 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9792 /* Before declaring the flip queue wedged, check if
9793 * the hardware completed the operation behind our backs.
9794 */
9795 if (__intel_pageflip_stall_check(dev, crtc)) {
9796 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9797 page_flip_completed(intel_crtc);
9798 } else {
9799 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 9800 spin_unlock_irq(&dev->event_lock);
468f0b44 9801
d6bbafa1
CW
9802 drm_crtc_vblank_put(crtc);
9803 kfree(work);
9804 return -EBUSY;
9805 }
6b95a207
KH
9806 }
9807 intel_crtc->unpin_work = work;
5e2d7afc 9808 spin_unlock_irq(&dev->event_lock);
6b95a207 9809
b4a98e57
CW
9810 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9811 flush_workqueue(dev_priv->wq);
9812
79158103
CW
9813 ret = i915_mutex_lock_interruptible(dev);
9814 if (ret)
9815 goto cleanup;
6b95a207 9816
75dfca80 9817 /* Reference the objects for the scheduled work. */
ab8d6675 9818 drm_framebuffer_reference(work->old_fb);
05394f39 9819 drm_gem_object_reference(&obj->base);
6b95a207 9820
f4510a27 9821 crtc->primary->fb = fb;
afd65eb4 9822 update_state_fb(crtc->primary);
1ed1f968 9823
e1f99ce6 9824 work->pending_flip_obj = obj;
e1f99ce6 9825
b4a98e57 9826 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9827 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9828
75f7f3ec 9829 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9830 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9831
4fa62c89
VS
9832 if (IS_VALLEYVIEW(dev)) {
9833 ring = &dev_priv->ring[BCS];
ab8d6675 9834 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
9835 /* vlv: DISPLAY_FLIP fails to change tiling */
9836 ring = NULL;
48bf5b2d 9837 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 9838 ring = &dev_priv->ring[BCS];
4fa62c89 9839 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 9840 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
9841 if (ring == NULL || ring->id != RCS)
9842 ring = &dev_priv->ring[BCS];
9843 } else {
9844 ring = &dev_priv->ring[RCS];
9845 }
9846
850c4cdc 9847 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
8c9f3aaf
JB
9848 if (ret)
9849 goto cleanup_pending;
6b95a207 9850
4fa62c89
VS
9851 work->gtt_offset =
9852 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9853
d6bbafa1 9854 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
9855 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9856 page_flip_flags);
d6bbafa1
CW
9857 if (ret)
9858 goto cleanup_unpin;
9859
f06cc1b9
JH
9860 i915_gem_request_assign(&work->flip_queued_req,
9861 obj->last_write_req);
d6bbafa1 9862 } else {
84c33a64 9863 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
9864 page_flip_flags);
9865 if (ret)
9866 goto cleanup_unpin;
9867
f06cc1b9
JH
9868 i915_gem_request_assign(&work->flip_queued_req,
9869 intel_ring_get_request(ring));
d6bbafa1
CW
9870 }
9871
9872 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9873 work->enable_stall_check = true;
4fa62c89 9874
ab8d6675 9875 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
9876 INTEL_FRONTBUFFER_PRIMARY(pipe));
9877
7ff0ebcc 9878 intel_fbc_disable(dev);
f99d7069 9879 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9880 mutex_unlock(&dev->struct_mutex);
9881
e5510fac
JB
9882 trace_i915_flip_request(intel_crtc->plane, obj);
9883
6b95a207 9884 return 0;
96b099fd 9885
4fa62c89
VS
9886cleanup_unpin:
9887 intel_unpin_fb_obj(obj);
8c9f3aaf 9888cleanup_pending:
b4a98e57 9889 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9890 crtc->primary->fb = old_fb;
afd65eb4 9891 update_state_fb(crtc->primary);
ab8d6675 9892 drm_framebuffer_unreference(work->old_fb);
05394f39 9893 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9894 mutex_unlock(&dev->struct_mutex);
9895
79158103 9896cleanup:
5e2d7afc 9897 spin_lock_irq(&dev->event_lock);
96b099fd 9898 intel_crtc->unpin_work = NULL;
5e2d7afc 9899 spin_unlock_irq(&dev->event_lock);
96b099fd 9900
87b6b101 9901 drm_crtc_vblank_put(crtc);
7317c75e 9902free_work:
96b099fd
CW
9903 kfree(work);
9904
f900db47
CW
9905 if (ret == -EIO) {
9906out_hang:
53a366b9 9907 ret = intel_plane_restore(primary);
f0d3dad3 9908 if (ret == 0 && event) {
5e2d7afc 9909 spin_lock_irq(&dev->event_lock);
a071fa00 9910 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 9911 spin_unlock_irq(&dev->event_lock);
f0d3dad3 9912 }
f900db47 9913 }
96b099fd 9914 return ret;
6b95a207
KH
9915}
9916
f6e5b160 9917static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9918 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9919 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
9920 .atomic_begin = intel_begin_crtc_commit,
9921 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
9922};
9923
9a935856
DV
9924/**
9925 * intel_modeset_update_staged_output_state
9926 *
9927 * Updates the staged output configuration state, e.g. after we've read out the
9928 * current hw state.
9929 */
9930static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9931{
7668851f 9932 struct intel_crtc *crtc;
9a935856
DV
9933 struct intel_encoder *encoder;
9934 struct intel_connector *connector;
f6e5b160 9935
9a935856
DV
9936 list_for_each_entry(connector, &dev->mode_config.connector_list,
9937 base.head) {
9938 connector->new_encoder =
9939 to_intel_encoder(connector->base.encoder);
9940 }
f6e5b160 9941
b2784e15 9942 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9943 encoder->new_crtc =
9944 to_intel_crtc(encoder->base.crtc);
9945 }
7668851f 9946
d3fcc808 9947 for_each_intel_crtc(dev, crtc) {
7668851f 9948 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9949
9950 if (crtc->new_enabled)
6e3c9717 9951 crtc->new_config = crtc->config;
7bd0a8e7
VS
9952 else
9953 crtc->new_config = NULL;
7668851f 9954 }
f6e5b160
CW
9955}
9956
9a935856
DV
9957/**
9958 * intel_modeset_commit_output_state
9959 *
9960 * This function copies the stage display pipe configuration to the real one.
9961 */
9962static void intel_modeset_commit_output_state(struct drm_device *dev)
9963{
7668851f 9964 struct intel_crtc *crtc;
9a935856
DV
9965 struct intel_encoder *encoder;
9966 struct intel_connector *connector;
f6e5b160 9967
9a935856
DV
9968 list_for_each_entry(connector, &dev->mode_config.connector_list,
9969 base.head) {
9970 connector->base.encoder = &connector->new_encoder->base;
9971 }
f6e5b160 9972
b2784e15 9973 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9974 encoder->base.crtc = &encoder->new_crtc->base;
9975 }
7668851f 9976
d3fcc808 9977 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9978 crtc->base.enabled = crtc->new_enabled;
9979 }
9a935856
DV
9980}
9981
050f7aeb 9982static void
eba905b2 9983connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 9984 struct intel_crtc_state *pipe_config)
050f7aeb
DV
9985{
9986 int bpp = pipe_config->pipe_bpp;
9987
9988 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9989 connector->base.base.id,
c23cc417 9990 connector->base.name);
050f7aeb
DV
9991
9992 /* Don't use an invalid EDID bpc value */
9993 if (connector->base.display_info.bpc &&
9994 connector->base.display_info.bpc * 3 < bpp) {
9995 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9996 bpp, connector->base.display_info.bpc*3);
9997 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9998 }
9999
10000 /* Clamp bpp to 8 on screens without EDID 1.4 */
10001 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10002 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10003 bpp);
10004 pipe_config->pipe_bpp = 24;
10005 }
10006}
10007
4e53c2e0 10008static int
050f7aeb
DV
10009compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10010 struct drm_framebuffer *fb,
5cec258b 10011 struct intel_crtc_state *pipe_config)
4e53c2e0 10012{
050f7aeb
DV
10013 struct drm_device *dev = crtc->base.dev;
10014 struct intel_connector *connector;
4e53c2e0
DV
10015 int bpp;
10016
d42264b1
DV
10017 switch (fb->pixel_format) {
10018 case DRM_FORMAT_C8:
4e53c2e0
DV
10019 bpp = 8*3; /* since we go through a colormap */
10020 break;
d42264b1
DV
10021 case DRM_FORMAT_XRGB1555:
10022 case DRM_FORMAT_ARGB1555:
10023 /* checked in intel_framebuffer_init already */
10024 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10025 return -EINVAL;
10026 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10027 bpp = 6*3; /* min is 18bpp */
10028 break;
d42264b1
DV
10029 case DRM_FORMAT_XBGR8888:
10030 case DRM_FORMAT_ABGR8888:
10031 /* checked in intel_framebuffer_init already */
10032 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10033 return -EINVAL;
10034 case DRM_FORMAT_XRGB8888:
10035 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10036 bpp = 8*3;
10037 break;
d42264b1
DV
10038 case DRM_FORMAT_XRGB2101010:
10039 case DRM_FORMAT_ARGB2101010:
10040 case DRM_FORMAT_XBGR2101010:
10041 case DRM_FORMAT_ABGR2101010:
10042 /* checked in intel_framebuffer_init already */
10043 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10044 return -EINVAL;
4e53c2e0
DV
10045 bpp = 10*3;
10046 break;
baba133a 10047 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10048 default:
10049 DRM_DEBUG_KMS("unsupported depth\n");
10050 return -EINVAL;
10051 }
10052
4e53c2e0
DV
10053 pipe_config->pipe_bpp = bpp;
10054
10055 /* Clamp display bpp to EDID value */
10056 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10057 base.head) {
1b829e05
DV
10058 if (!connector->new_encoder ||
10059 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10060 continue;
10061
050f7aeb 10062 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10063 }
10064
10065 return bpp;
10066}
10067
644db711
DV
10068static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10069{
10070 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10071 "type: 0x%x flags: 0x%x\n",
1342830c 10072 mode->crtc_clock,
644db711
DV
10073 mode->crtc_hdisplay, mode->crtc_hsync_start,
10074 mode->crtc_hsync_end, mode->crtc_htotal,
10075 mode->crtc_vdisplay, mode->crtc_vsync_start,
10076 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10077}
10078
c0b03411 10079static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 10080 struct intel_crtc_state *pipe_config,
c0b03411
DV
10081 const char *context)
10082{
10083 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10084 context, pipe_name(crtc->pipe));
10085
10086 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10087 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10088 pipe_config->pipe_bpp, pipe_config->dither);
10089 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10090 pipe_config->has_pch_encoder,
10091 pipe_config->fdi_lanes,
10092 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10093 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10094 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10095 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10096 pipe_config->has_dp_encoder,
10097 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10098 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10099 pipe_config->dp_m_n.tu);
b95af8be
VK
10100
10101 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10102 pipe_config->has_dp_encoder,
10103 pipe_config->dp_m2_n2.gmch_m,
10104 pipe_config->dp_m2_n2.gmch_n,
10105 pipe_config->dp_m2_n2.link_m,
10106 pipe_config->dp_m2_n2.link_n,
10107 pipe_config->dp_m2_n2.tu);
10108
55072d19
DV
10109 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10110 pipe_config->has_audio,
10111 pipe_config->has_infoframe);
10112
c0b03411 10113 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 10114 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 10115 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
10116 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10117 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 10118 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10119 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10120 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10121 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10122 pipe_config->gmch_pfit.control,
10123 pipe_config->gmch_pfit.pgm_ratios,
10124 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10125 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10126 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10127 pipe_config->pch_pfit.size,
10128 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10129 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10130 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10131}
10132
bc079e8b
VS
10133static bool encoders_cloneable(const struct intel_encoder *a,
10134 const struct intel_encoder *b)
accfc0c5 10135{
bc079e8b
VS
10136 /* masks could be asymmetric, so check both ways */
10137 return a == b || (a->cloneable & (1 << b->type) &&
10138 b->cloneable & (1 << a->type));
10139}
10140
10141static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10142 struct intel_encoder *encoder)
10143{
10144 struct drm_device *dev = crtc->base.dev;
10145 struct intel_encoder *source_encoder;
10146
b2784e15 10147 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10148 if (source_encoder->new_crtc != crtc)
10149 continue;
10150
10151 if (!encoders_cloneable(encoder, source_encoder))
10152 return false;
10153 }
10154
10155 return true;
10156}
10157
10158static bool check_encoder_cloning(struct intel_crtc *crtc)
10159{
10160 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10161 struct intel_encoder *encoder;
10162
b2784e15 10163 for_each_intel_encoder(dev, encoder) {
bc079e8b 10164 if (encoder->new_crtc != crtc)
accfc0c5
DV
10165 continue;
10166
bc079e8b
VS
10167 if (!check_single_encoder_cloning(crtc, encoder))
10168 return false;
accfc0c5
DV
10169 }
10170
bc079e8b 10171 return true;
accfc0c5
DV
10172}
10173
00f0b378
VS
10174static bool check_digital_port_conflicts(struct drm_device *dev)
10175{
10176 struct intel_connector *connector;
10177 unsigned int used_ports = 0;
10178
10179 /*
10180 * Walk the connector list instead of the encoder
10181 * list to detect the problem on ddi platforms
10182 * where there's just one encoder per digital port.
10183 */
10184 list_for_each_entry(connector,
10185 &dev->mode_config.connector_list, base.head) {
10186 struct intel_encoder *encoder = connector->new_encoder;
10187
10188 if (!encoder)
10189 continue;
10190
10191 WARN_ON(!encoder->new_crtc);
10192
10193 switch (encoder->type) {
10194 unsigned int port_mask;
10195 case INTEL_OUTPUT_UNKNOWN:
10196 if (WARN_ON(!HAS_DDI(dev)))
10197 break;
10198 case INTEL_OUTPUT_DISPLAYPORT:
10199 case INTEL_OUTPUT_HDMI:
10200 case INTEL_OUTPUT_EDP:
10201 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10202
10203 /* the same port mustn't appear more than once */
10204 if (used_ports & port_mask)
10205 return false;
10206
10207 used_ports |= port_mask;
10208 default:
10209 break;
10210 }
10211 }
10212
10213 return true;
10214}
10215
5cec258b 10216static struct intel_crtc_state *
b8cecdf5 10217intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10218 struct drm_framebuffer *fb,
b8cecdf5 10219 struct drm_display_mode *mode)
ee7b9f93 10220{
7758a113 10221 struct drm_device *dev = crtc->dev;
7758a113 10222 struct intel_encoder *encoder;
5cec258b 10223 struct intel_crtc_state *pipe_config;
e29c22c0
DV
10224 int plane_bpp, ret = -EINVAL;
10225 bool retry = true;
ee7b9f93 10226
bc079e8b 10227 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10228 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10229 return ERR_PTR(-EINVAL);
10230 }
10231
00f0b378
VS
10232 if (!check_digital_port_conflicts(dev)) {
10233 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10234 return ERR_PTR(-EINVAL);
10235 }
10236
b8cecdf5
DV
10237 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10238 if (!pipe_config)
7758a113
DV
10239 return ERR_PTR(-ENOMEM);
10240
2d112de7
ACO
10241 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10242 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 10243
e143a21c
DV
10244 pipe_config->cpu_transcoder =
10245 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10246 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10247
2960bc9c
ID
10248 /*
10249 * Sanitize sync polarity flags based on requested ones. If neither
10250 * positive or negative polarity is requested, treat this as meaning
10251 * negative polarity.
10252 */
2d112de7 10253 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10254 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 10255 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 10256
2d112de7 10257 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10258 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 10259 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 10260
050f7aeb
DV
10261 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10262 * plane pixel format and any sink constraints into account. Returns the
10263 * source plane bpp so that dithering can be selected on mismatches
10264 * after encoders and crtc also have had their say. */
10265 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10266 fb, pipe_config);
4e53c2e0
DV
10267 if (plane_bpp < 0)
10268 goto fail;
10269
e41a56be
VS
10270 /*
10271 * Determine the real pipe dimensions. Note that stereo modes can
10272 * increase the actual pipe size due to the frame doubling and
10273 * insertion of additional space for blanks between the frame. This
10274 * is stored in the crtc timings. We use the requested mode to do this
10275 * computation to clearly distinguish it from the adjusted mode, which
10276 * can be changed by the connectors in the below retry loop.
10277 */
2d112de7 10278 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
10279 &pipe_config->pipe_src_w,
10280 &pipe_config->pipe_src_h);
e41a56be 10281
e29c22c0 10282encoder_retry:
ef1b460d 10283 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10284 pipe_config->port_clock = 0;
ef1b460d 10285 pipe_config->pixel_multiplier = 1;
ff9a6750 10286
135c81b8 10287 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
10288 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10289 CRTC_STEREO_DOUBLE);
135c81b8 10290
7758a113
DV
10291 /* Pass our mode to the connectors and the CRTC to give them a chance to
10292 * adjust it according to limitations or connector properties, and also
10293 * a chance to reject the mode entirely.
47f1c6c9 10294 */
b2784e15 10295 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10296
7758a113
DV
10297 if (&encoder->new_crtc->base != crtc)
10298 continue;
7ae89233 10299
efea6e8e
DV
10300 if (!(encoder->compute_config(encoder, pipe_config))) {
10301 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10302 goto fail;
10303 }
ee7b9f93 10304 }
47f1c6c9 10305
ff9a6750
DV
10306 /* Set default port clock if not overwritten by the encoder. Needs to be
10307 * done afterwards in case the encoder adjusts the mode. */
10308 if (!pipe_config->port_clock)
2d112de7 10309 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 10310 * pipe_config->pixel_multiplier;
ff9a6750 10311
a43f6e0f 10312 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10313 if (ret < 0) {
7758a113
DV
10314 DRM_DEBUG_KMS("CRTC fixup failed\n");
10315 goto fail;
ee7b9f93 10316 }
e29c22c0
DV
10317
10318 if (ret == RETRY) {
10319 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10320 ret = -EINVAL;
10321 goto fail;
10322 }
10323
10324 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10325 retry = false;
10326 goto encoder_retry;
10327 }
10328
4e53c2e0
DV
10329 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10330 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10331 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10332
b8cecdf5 10333 return pipe_config;
7758a113 10334fail:
b8cecdf5 10335 kfree(pipe_config);
e29c22c0 10336 return ERR_PTR(ret);
ee7b9f93 10337}
47f1c6c9 10338
e2e1ed41
DV
10339/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10340 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10341static void
10342intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10343 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10344{
10345 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10346 struct drm_device *dev = crtc->dev;
10347 struct intel_encoder *encoder;
10348 struct intel_connector *connector;
10349 struct drm_crtc *tmp_crtc;
79e53945 10350
e2e1ed41 10351 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10352
e2e1ed41
DV
10353 /* Check which crtcs have changed outputs connected to them, these need
10354 * to be part of the prepare_pipes mask. We don't (yet) support global
10355 * modeset across multiple crtcs, so modeset_pipes will only have one
10356 * bit set at most. */
10357 list_for_each_entry(connector, &dev->mode_config.connector_list,
10358 base.head) {
10359 if (connector->base.encoder == &connector->new_encoder->base)
10360 continue;
79e53945 10361
e2e1ed41
DV
10362 if (connector->base.encoder) {
10363 tmp_crtc = connector->base.encoder->crtc;
10364
10365 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10366 }
10367
10368 if (connector->new_encoder)
10369 *prepare_pipes |=
10370 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10371 }
10372
b2784e15 10373 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10374 if (encoder->base.crtc == &encoder->new_crtc->base)
10375 continue;
10376
10377 if (encoder->base.crtc) {
10378 tmp_crtc = encoder->base.crtc;
10379
10380 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10381 }
10382
10383 if (encoder->new_crtc)
10384 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10385 }
10386
7668851f 10387 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10388 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10389 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10390 continue;
7e7d76c3 10391
7668851f 10392 if (!intel_crtc->new_enabled)
e2e1ed41 10393 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10394 else
10395 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10396 }
10397
e2e1ed41
DV
10398
10399 /* set_mode is also used to update properties on life display pipes. */
10400 intel_crtc = to_intel_crtc(crtc);
7668851f 10401 if (intel_crtc->new_enabled)
e2e1ed41
DV
10402 *prepare_pipes |= 1 << intel_crtc->pipe;
10403
b6c5164d
DV
10404 /*
10405 * For simplicity do a full modeset on any pipe where the output routing
10406 * changed. We could be more clever, but that would require us to be
10407 * more careful with calling the relevant encoder->mode_set functions.
10408 */
e2e1ed41
DV
10409 if (*prepare_pipes)
10410 *modeset_pipes = *prepare_pipes;
10411
10412 /* ... and mask these out. */
10413 *modeset_pipes &= ~(*disable_pipes);
10414 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10415
10416 /*
10417 * HACK: We don't (yet) fully support global modesets. intel_set_config
10418 * obies this rule, but the modeset restore mode of
10419 * intel_modeset_setup_hw_state does not.
10420 */
10421 *modeset_pipes &= 1 << intel_crtc->pipe;
10422 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10423
10424 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10425 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10426}
79e53945 10427
ea9d758d 10428static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10429{
ea9d758d 10430 struct drm_encoder *encoder;
f6e5b160 10431 struct drm_device *dev = crtc->dev;
f6e5b160 10432
ea9d758d
DV
10433 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10434 if (encoder->crtc == crtc)
10435 return true;
10436
10437 return false;
10438}
10439
10440static void
10441intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10442{
ba41c0de 10443 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
10444 struct intel_encoder *intel_encoder;
10445 struct intel_crtc *intel_crtc;
10446 struct drm_connector *connector;
10447
ba41c0de
DV
10448 intel_shared_dpll_commit(dev_priv);
10449
b2784e15 10450 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10451 if (!intel_encoder->base.crtc)
10452 continue;
10453
10454 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10455
10456 if (prepare_pipes & (1 << intel_crtc->pipe))
10457 intel_encoder->connectors_active = false;
10458 }
10459
10460 intel_modeset_commit_output_state(dev);
10461
7668851f 10462 /* Double check state. */
d3fcc808 10463 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10464 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7 10465 WARN_ON(intel_crtc->new_config &&
6e3c9717 10466 intel_crtc->new_config != intel_crtc->config);
7bd0a8e7 10467 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10468 }
10469
10470 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10471 if (!connector->encoder || !connector->encoder->crtc)
10472 continue;
10473
10474 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10475
10476 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10477 struct drm_property *dpms_property =
10478 dev->mode_config.dpms_property;
10479
ea9d758d 10480 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10481 drm_object_property_set_value(&connector->base,
68d34720
DV
10482 dpms_property,
10483 DRM_MODE_DPMS_ON);
ea9d758d
DV
10484
10485 intel_encoder = to_intel_encoder(connector->encoder);
10486 intel_encoder->connectors_active = true;
10487 }
10488 }
10489
10490}
10491
3bd26263 10492static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10493{
3bd26263 10494 int diff;
f1f644dc
JB
10495
10496 if (clock1 == clock2)
10497 return true;
10498
10499 if (!clock1 || !clock2)
10500 return false;
10501
10502 diff = abs(clock1 - clock2);
10503
10504 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10505 return true;
10506
10507 return false;
10508}
10509
25c5b266
DV
10510#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10511 list_for_each_entry((intel_crtc), \
10512 &(dev)->mode_config.crtc_list, \
10513 base.head) \
0973f18f 10514 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10515
0e8ffe1b 10516static bool
2fa2fe9a 10517intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
10518 struct intel_crtc_state *current_config,
10519 struct intel_crtc_state *pipe_config)
0e8ffe1b 10520{
66e985c0
DV
10521#define PIPE_CONF_CHECK_X(name) \
10522 if (current_config->name != pipe_config->name) { \
10523 DRM_ERROR("mismatch in " #name " " \
10524 "(expected 0x%08x, found 0x%08x)\n", \
10525 current_config->name, \
10526 pipe_config->name); \
10527 return false; \
10528 }
10529
08a24034
DV
10530#define PIPE_CONF_CHECK_I(name) \
10531 if (current_config->name != pipe_config->name) { \
10532 DRM_ERROR("mismatch in " #name " " \
10533 "(expected %i, found %i)\n", \
10534 current_config->name, \
10535 pipe_config->name); \
10536 return false; \
88adfff1
DV
10537 }
10538
b95af8be
VK
10539/* This is required for BDW+ where there is only one set of registers for
10540 * switching between high and low RR.
10541 * This macro can be used whenever a comparison has to be made between one
10542 * hw state and multiple sw state variables.
10543 */
10544#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10545 if ((current_config->name != pipe_config->name) && \
10546 (current_config->alt_name != pipe_config->name)) { \
10547 DRM_ERROR("mismatch in " #name " " \
10548 "(expected %i or %i, found %i)\n", \
10549 current_config->name, \
10550 current_config->alt_name, \
10551 pipe_config->name); \
10552 return false; \
10553 }
10554
1bd1bd80
DV
10555#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10556 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10557 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10558 "(expected %i, found %i)\n", \
10559 current_config->name & (mask), \
10560 pipe_config->name & (mask)); \
10561 return false; \
10562 }
10563
5e550656
VS
10564#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10565 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10566 DRM_ERROR("mismatch in " #name " " \
10567 "(expected %i, found %i)\n", \
10568 current_config->name, \
10569 pipe_config->name); \
10570 return false; \
10571 }
10572
bb760063
DV
10573#define PIPE_CONF_QUIRK(quirk) \
10574 ((current_config->quirks | pipe_config->quirks) & (quirk))
10575
eccb140b
DV
10576 PIPE_CONF_CHECK_I(cpu_transcoder);
10577
08a24034
DV
10578 PIPE_CONF_CHECK_I(has_pch_encoder);
10579 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10580 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10581 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10582 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10583 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10584 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10585
eb14cb74 10586 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10587
10588 if (INTEL_INFO(dev)->gen < 8) {
10589 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10590 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10591 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10592 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10593 PIPE_CONF_CHECK_I(dp_m_n.tu);
10594
10595 if (current_config->has_drrs) {
10596 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10597 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10598 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10599 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10600 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10601 }
10602 } else {
10603 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10604 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10605 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10606 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10607 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10608 }
eb14cb74 10609
2d112de7
ACO
10610 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10611 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10612 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10613 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10614 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10615 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 10616
2d112de7
ACO
10617 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10618 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10619 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10620 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10621 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10622 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 10623
c93f54cf 10624 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10625 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10626 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10627 IS_VALLEYVIEW(dev))
10628 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 10629 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 10630
9ed109a7
DV
10631 PIPE_CONF_CHECK_I(has_audio);
10632
2d112de7 10633 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
10634 DRM_MODE_FLAG_INTERLACE);
10635
bb760063 10636 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 10637 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10638 DRM_MODE_FLAG_PHSYNC);
2d112de7 10639 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10640 DRM_MODE_FLAG_NHSYNC);
2d112de7 10641 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10642 DRM_MODE_FLAG_PVSYNC);
2d112de7 10643 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
10644 DRM_MODE_FLAG_NVSYNC);
10645 }
045ac3b5 10646
37327abd
VS
10647 PIPE_CONF_CHECK_I(pipe_src_w);
10648 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10649
9953599b
DV
10650 /*
10651 * FIXME: BIOS likes to set up a cloned config with lvds+external
10652 * screen. Since we don't yet re-compute the pipe config when moving
10653 * just the lvds port away to another pipe the sw tracking won't match.
10654 *
10655 * Proper atomic modesets with recomputed global state will fix this.
10656 * Until then just don't check gmch state for inherited modes.
10657 */
10658 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10659 PIPE_CONF_CHECK_I(gmch_pfit.control);
10660 /* pfit ratios are autocomputed by the hw on gen4+ */
10661 if (INTEL_INFO(dev)->gen < 4)
10662 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10663 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10664 }
10665
fd4daa9c
CW
10666 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10667 if (current_config->pch_pfit.enabled) {
10668 PIPE_CONF_CHECK_I(pch_pfit.pos);
10669 PIPE_CONF_CHECK_I(pch_pfit.size);
10670 }
2fa2fe9a 10671
e59150dc
JB
10672 /* BDW+ don't expose a synchronous way to read the state */
10673 if (IS_HASWELL(dev))
10674 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10675
282740f7
VS
10676 PIPE_CONF_CHECK_I(double_wide);
10677
26804afd
DV
10678 PIPE_CONF_CHECK_X(ddi_pll_sel);
10679
c0d43d62 10680 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10681 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10682 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10683 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10684 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10685 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
10686 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10687 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10688 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 10689
42571aef
VS
10690 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10691 PIPE_CONF_CHECK_I(pipe_bpp);
10692
2d112de7 10693 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 10694 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10695
66e985c0 10696#undef PIPE_CONF_CHECK_X
08a24034 10697#undef PIPE_CONF_CHECK_I
b95af8be 10698#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10699#undef PIPE_CONF_CHECK_FLAGS
5e550656 10700#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10701#undef PIPE_CONF_QUIRK
88adfff1 10702
0e8ffe1b
DV
10703 return true;
10704}
10705
08db6652
DL
10706static void check_wm_state(struct drm_device *dev)
10707{
10708 struct drm_i915_private *dev_priv = dev->dev_private;
10709 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10710 struct intel_crtc *intel_crtc;
10711 int plane;
10712
10713 if (INTEL_INFO(dev)->gen < 9)
10714 return;
10715
10716 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10717 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10718
10719 for_each_intel_crtc(dev, intel_crtc) {
10720 struct skl_ddb_entry *hw_entry, *sw_entry;
10721 const enum pipe pipe = intel_crtc->pipe;
10722
10723 if (!intel_crtc->active)
10724 continue;
10725
10726 /* planes */
10727 for_each_plane(pipe, plane) {
10728 hw_entry = &hw_ddb.plane[pipe][plane];
10729 sw_entry = &sw_ddb->plane[pipe][plane];
10730
10731 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10732 continue;
10733
10734 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10735 "(expected (%u,%u), found (%u,%u))\n",
10736 pipe_name(pipe), plane + 1,
10737 sw_entry->start, sw_entry->end,
10738 hw_entry->start, hw_entry->end);
10739 }
10740
10741 /* cursor */
10742 hw_entry = &hw_ddb.cursor[pipe];
10743 sw_entry = &sw_ddb->cursor[pipe];
10744
10745 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10746 continue;
10747
10748 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10749 "(expected (%u,%u), found (%u,%u))\n",
10750 pipe_name(pipe),
10751 sw_entry->start, sw_entry->end,
10752 hw_entry->start, hw_entry->end);
10753 }
10754}
10755
91d1b4bd
DV
10756static void
10757check_connector_state(struct drm_device *dev)
8af6cf88 10758{
8af6cf88
DV
10759 struct intel_connector *connector;
10760
10761 list_for_each_entry(connector, &dev->mode_config.connector_list,
10762 base.head) {
10763 /* This also checks the encoder/connector hw state with the
10764 * ->get_hw_state callbacks. */
10765 intel_connector_check_state(connector);
10766
e2c719b7 10767 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
10768 "connector's staged encoder doesn't match current encoder\n");
10769 }
91d1b4bd
DV
10770}
10771
10772static void
10773check_encoder_state(struct drm_device *dev)
10774{
10775 struct intel_encoder *encoder;
10776 struct intel_connector *connector;
8af6cf88 10777
b2784e15 10778 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10779 bool enabled = false;
10780 bool active = false;
10781 enum pipe pipe, tracked_pipe;
10782
10783 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10784 encoder->base.base.id,
8e329a03 10785 encoder->base.name);
8af6cf88 10786
e2c719b7 10787 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 10788 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 10789 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
10790 "encoder's active_connectors set, but no crtc\n");
10791
10792 list_for_each_entry(connector, &dev->mode_config.connector_list,
10793 base.head) {
10794 if (connector->base.encoder != &encoder->base)
10795 continue;
10796 enabled = true;
10797 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10798 active = true;
10799 }
0e32b39c
DA
10800 /*
10801 * for MST connectors if we unplug the connector is gone
10802 * away but the encoder is still connected to a crtc
10803 * until a modeset happens in response to the hotplug.
10804 */
10805 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10806 continue;
10807
e2c719b7 10808 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
10809 "encoder's enabled state mismatch "
10810 "(expected %i, found %i)\n",
10811 !!encoder->base.crtc, enabled);
e2c719b7 10812 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
10813 "active encoder with no crtc\n");
10814
e2c719b7 10815 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
10816 "encoder's computed active state doesn't match tracked active state "
10817 "(expected %i, found %i)\n", active, encoder->connectors_active);
10818
10819 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 10820 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
10821 "encoder's hw state doesn't match sw tracking "
10822 "(expected %i, found %i)\n",
10823 encoder->connectors_active, active);
10824
10825 if (!encoder->base.crtc)
10826 continue;
10827
10828 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 10829 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
10830 "active encoder's pipe doesn't match"
10831 "(expected %i, found %i)\n",
10832 tracked_pipe, pipe);
10833
10834 }
91d1b4bd
DV
10835}
10836
10837static void
10838check_crtc_state(struct drm_device *dev)
10839{
fbee40df 10840 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10841 struct intel_crtc *crtc;
10842 struct intel_encoder *encoder;
5cec258b 10843 struct intel_crtc_state pipe_config;
8af6cf88 10844
d3fcc808 10845 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10846 bool enabled = false;
10847 bool active = false;
10848
045ac3b5
JB
10849 memset(&pipe_config, 0, sizeof(pipe_config));
10850
8af6cf88
DV
10851 DRM_DEBUG_KMS("[CRTC:%d]\n",
10852 crtc->base.base.id);
10853
e2c719b7 10854 I915_STATE_WARN(crtc->active && !crtc->base.enabled,
8af6cf88
DV
10855 "active crtc, but not enabled in sw tracking\n");
10856
b2784e15 10857 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10858 if (encoder->base.crtc != &crtc->base)
10859 continue;
10860 enabled = true;
10861 if (encoder->connectors_active)
10862 active = true;
10863 }
6c49f241 10864
e2c719b7 10865 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
10866 "crtc's computed active state doesn't match tracked active state "
10867 "(expected %i, found %i)\n", active, crtc->active);
e2c719b7 10868 I915_STATE_WARN(enabled != crtc->base.enabled,
8af6cf88
DV
10869 "crtc's computed enabled state doesn't match tracked enabled state "
10870 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10871
0e8ffe1b
DV
10872 active = dev_priv->display.get_pipe_config(crtc,
10873 &pipe_config);
d62cf62a 10874
b6b5d049
VS
10875 /* hw state is inconsistent with the pipe quirk */
10876 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10877 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10878 active = crtc->active;
10879
b2784e15 10880 for_each_intel_encoder(dev, encoder) {
3eaba51c 10881 enum pipe pipe;
6c49f241
DV
10882 if (encoder->base.crtc != &crtc->base)
10883 continue;
1d37b689 10884 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10885 encoder->get_config(encoder, &pipe_config);
10886 }
10887
e2c719b7 10888 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
10889 "crtc active state doesn't match with hw state "
10890 "(expected %i, found %i)\n", crtc->active, active);
10891
c0b03411 10892 if (active &&
6e3c9717 10893 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 10894 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
10895 intel_dump_pipe_config(crtc, &pipe_config,
10896 "[hw state]");
6e3c9717 10897 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
10898 "[sw state]");
10899 }
8af6cf88
DV
10900 }
10901}
10902
91d1b4bd
DV
10903static void
10904check_shared_dpll_state(struct drm_device *dev)
10905{
fbee40df 10906 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10907 struct intel_crtc *crtc;
10908 struct intel_dpll_hw_state dpll_hw_state;
10909 int i;
5358901f
DV
10910
10911 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10912 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10913 int enabled_crtcs = 0, active_crtcs = 0;
10914 bool active;
10915
10916 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10917
10918 DRM_DEBUG_KMS("%s\n", pll->name);
10919
10920 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10921
e2c719b7 10922 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 10923 "more active pll users than references: %i vs %i\n",
3e369b76 10924 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 10925 I915_STATE_WARN(pll->active && !pll->on,
5358901f 10926 "pll in active use but not on in sw tracking\n");
e2c719b7 10927 I915_STATE_WARN(pll->on && !pll->active,
35c95375 10928 "pll in on but not on in use in sw tracking\n");
e2c719b7 10929 I915_STATE_WARN(pll->on != active,
5358901f
DV
10930 "pll on state mismatch (expected %i, found %i)\n",
10931 pll->on, active);
10932
d3fcc808 10933 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10934 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10935 enabled_crtcs++;
10936 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10937 active_crtcs++;
10938 }
e2c719b7 10939 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
10940 "pll active crtcs mismatch (expected %i, found %i)\n",
10941 pll->active, active_crtcs);
e2c719b7 10942 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 10943 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 10944 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 10945
e2c719b7 10946 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
10947 sizeof(dpll_hw_state)),
10948 "pll hw state mismatch\n");
5358901f 10949 }
8af6cf88
DV
10950}
10951
91d1b4bd
DV
10952void
10953intel_modeset_check_state(struct drm_device *dev)
10954{
08db6652 10955 check_wm_state(dev);
91d1b4bd
DV
10956 check_connector_state(dev);
10957 check_encoder_state(dev);
10958 check_crtc_state(dev);
10959 check_shared_dpll_state(dev);
10960}
10961
5cec258b 10962void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
10963 int dotclock)
10964{
10965 /*
10966 * FDI already provided one idea for the dotclock.
10967 * Yell if the encoder disagrees.
10968 */
2d112de7 10969 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 10970 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 10971 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10972}
10973
80715b2f
VS
10974static void update_scanline_offset(struct intel_crtc *crtc)
10975{
10976 struct drm_device *dev = crtc->base.dev;
10977
10978 /*
10979 * The scanline counter increments at the leading edge of hsync.
10980 *
10981 * On most platforms it starts counting from vtotal-1 on the
10982 * first active line. That means the scanline counter value is
10983 * always one less than what we would expect. Ie. just after
10984 * start of vblank, which also occurs at start of hsync (on the
10985 * last active line), the scanline counter will read vblank_start-1.
10986 *
10987 * On gen2 the scanline counter starts counting from 1 instead
10988 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10989 * to keep the value positive), instead of adding one.
10990 *
10991 * On HSW+ the behaviour of the scanline counter depends on the output
10992 * type. For DP ports it behaves like most other platforms, but on HDMI
10993 * there's an extra 1 line difference. So we need to add two instead of
10994 * one to the value.
10995 */
10996 if (IS_GEN2(dev)) {
6e3c9717 10997 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
10998 int vtotal;
10999
11000 vtotal = mode->crtc_vtotal;
11001 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11002 vtotal /= 2;
11003
11004 crtc->scanline_offset = vtotal - 1;
11005 } else if (HAS_DDI(dev) &&
409ee761 11006 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
11007 crtc->scanline_offset = 2;
11008 } else
11009 crtc->scanline_offset = 1;
11010}
11011
5cec258b 11012static struct intel_crtc_state *
7f27126e
JB
11013intel_modeset_compute_config(struct drm_crtc *crtc,
11014 struct drm_display_mode *mode,
11015 struct drm_framebuffer *fb,
11016 unsigned *modeset_pipes,
11017 unsigned *prepare_pipes,
11018 unsigned *disable_pipes)
11019{
5cec258b 11020 struct intel_crtc_state *pipe_config = NULL;
7f27126e
JB
11021
11022 intel_modeset_affected_pipes(crtc, modeset_pipes,
11023 prepare_pipes, disable_pipes);
11024
11025 if ((*modeset_pipes) == 0)
11026 goto out;
11027
11028 /*
11029 * Note this needs changes when we start tracking multiple modes
11030 * and crtcs. At that point we'll need to compute the whole config
11031 * (i.e. one pipe_config for each crtc) rather than just the one
11032 * for this crtc.
11033 */
11034 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11035 if (IS_ERR(pipe_config)) {
11036 goto out;
11037 }
11038 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11039 "[modeset]");
7f27126e
JB
11040
11041out:
11042 return pipe_config;
11043}
11044
ed6739ef
ACO
11045static int __intel_set_mode_setup_plls(struct drm_device *dev,
11046 unsigned modeset_pipes,
11047 unsigned disable_pipes)
11048{
11049 struct drm_i915_private *dev_priv = to_i915(dev);
11050 unsigned clear_pipes = modeset_pipes | disable_pipes;
11051 struct intel_crtc *intel_crtc;
11052 int ret = 0;
11053
11054 if (!dev_priv->display.crtc_compute_clock)
11055 return 0;
11056
11057 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11058 if (ret)
11059 goto done;
11060
11061 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11062 struct intel_crtc_state *state = intel_crtc->new_config;
11063 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11064 state);
11065 if (ret) {
11066 intel_shared_dpll_abort_config(dev_priv);
11067 goto done;
11068 }
11069 }
11070
11071done:
11072 return ret;
11073}
11074
f30da187
DV
11075static int __intel_set_mode(struct drm_crtc *crtc,
11076 struct drm_display_mode *mode,
7f27126e 11077 int x, int y, struct drm_framebuffer *fb,
5cec258b 11078 struct intel_crtc_state *pipe_config,
7f27126e
JB
11079 unsigned modeset_pipes,
11080 unsigned prepare_pipes,
11081 unsigned disable_pipes)
a6778b3c
DV
11082{
11083 struct drm_device *dev = crtc->dev;
fbee40df 11084 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 11085 struct drm_display_mode *saved_mode;
25c5b266 11086 struct intel_crtc *intel_crtc;
c0c36b94 11087 int ret = 0;
a6778b3c 11088
4b4b9238 11089 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
11090 if (!saved_mode)
11091 return -ENOMEM;
a6778b3c 11092
3ac18232 11093 *saved_mode = crtc->mode;
a6778b3c 11094
b9950a13
VS
11095 if (modeset_pipes)
11096 to_intel_crtc(crtc)->new_config = pipe_config;
11097
30a970c6
JB
11098 /*
11099 * See if the config requires any additional preparation, e.g.
11100 * to adjust global state with pipes off. We need to do this
11101 * here so we can get the modeset_pipe updated config for the new
11102 * mode set on this crtc. For other crtcs we need to use the
11103 * adjusted_mode bits in the crtc directly.
11104 */
c164f833 11105 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 11106 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 11107
c164f833
VS
11108 /* may have added more to prepare_pipes than we should */
11109 prepare_pipes &= ~disable_pipes;
11110 }
11111
ed6739ef
ACO
11112 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11113 if (ret)
11114 goto done;
8bd31e67 11115
460da916
DV
11116 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11117 intel_crtc_disable(&intel_crtc->base);
11118
ea9d758d
DV
11119 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11120 if (intel_crtc->base.enabled)
11121 dev_priv->display.crtc_disable(&intel_crtc->base);
11122 }
a6778b3c 11123
6c4c86f5
DV
11124 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11125 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
11126 *
11127 * Note we'll need to fix this up when we start tracking multiple
11128 * pipes; here we assume a single modeset_pipe and only track the
11129 * single crtc and mode.
f6e5b160 11130 */
b8cecdf5 11131 if (modeset_pipes) {
25c5b266 11132 crtc->mode = *mode;
b8cecdf5
DV
11133 /* mode_set/enable/disable functions rely on a correct pipe
11134 * config. */
f5de6e07 11135 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
11136
11137 /*
11138 * Calculate and store various constants which
11139 * are later needed by vblank and swap-completion
11140 * timestamping. They are derived from true hwmode.
11141 */
11142 drm_calc_timestamping_constants(crtc,
2d112de7 11143 &pipe_config->base.adjusted_mode);
b8cecdf5 11144 }
7758a113 11145
ea9d758d
DV
11146 /* Only after disabling all output pipelines that will be changed can we
11147 * update the the output configuration. */
11148 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11149
50f6e502 11150 modeset_update_crtc_power_domains(dev);
47fab737 11151
a6778b3c
DV
11152 /* Set up the DPLL and any encoders state that needs to adjust or depend
11153 * on the DPLL.
f6e5b160 11154 */
25c5b266 11155 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
11156 struct drm_plane *primary = intel_crtc->base.primary;
11157 int vdisplay, hdisplay;
4c10794f 11158
455a6808
GP
11159 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11160 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11161 fb, 0, 0,
11162 hdisplay, vdisplay,
11163 x << 16, y << 16,
11164 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
11165 }
11166
11167 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11168 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11169 update_scanline_offset(intel_crtc);
11170
25c5b266 11171 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11172 }
a6778b3c 11173
a6778b3c
DV
11174 /* FIXME: add subpixel order */
11175done:
4b4b9238 11176 if (ret && crtc->enabled)
3ac18232 11177 crtc->mode = *saved_mode;
a6778b3c 11178
3ac18232 11179 kfree(saved_mode);
a6778b3c 11180 return ret;
f6e5b160
CW
11181}
11182
7f27126e
JB
11183static int intel_set_mode_pipes(struct drm_crtc *crtc,
11184 struct drm_display_mode *mode,
11185 int x, int y, struct drm_framebuffer *fb,
5cec258b 11186 struct intel_crtc_state *pipe_config,
7f27126e
JB
11187 unsigned modeset_pipes,
11188 unsigned prepare_pipes,
11189 unsigned disable_pipes)
f30da187
DV
11190{
11191 int ret;
11192
7f27126e
JB
11193 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11194 prepare_pipes, disable_pipes);
f30da187
DV
11195
11196 if (ret == 0)
11197 intel_modeset_check_state(crtc->dev);
11198
11199 return ret;
11200}
11201
7f27126e
JB
11202static int intel_set_mode(struct drm_crtc *crtc,
11203 struct drm_display_mode *mode,
11204 int x, int y, struct drm_framebuffer *fb)
11205{
5cec258b 11206 struct intel_crtc_state *pipe_config;
7f27126e
JB
11207 unsigned modeset_pipes, prepare_pipes, disable_pipes;
11208
11209 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11210 &modeset_pipes,
11211 &prepare_pipes,
11212 &disable_pipes);
11213
11214 if (IS_ERR(pipe_config))
11215 return PTR_ERR(pipe_config);
11216
11217 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11218 modeset_pipes, prepare_pipes,
11219 disable_pipes);
11220}
11221
c0c36b94
CW
11222void intel_crtc_restore_mode(struct drm_crtc *crtc)
11223{
f4510a27 11224 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11225}
11226
25c5b266
DV
11227#undef for_each_intel_crtc_masked
11228
d9e55608
DV
11229static void intel_set_config_free(struct intel_set_config *config)
11230{
11231 if (!config)
11232 return;
11233
1aa4b628
DV
11234 kfree(config->save_connector_encoders);
11235 kfree(config->save_encoder_crtcs);
7668851f 11236 kfree(config->save_crtc_enabled);
d9e55608
DV
11237 kfree(config);
11238}
11239
85f9eb71
DV
11240static int intel_set_config_save_state(struct drm_device *dev,
11241 struct intel_set_config *config)
11242{
7668851f 11243 struct drm_crtc *crtc;
85f9eb71
DV
11244 struct drm_encoder *encoder;
11245 struct drm_connector *connector;
11246 int count;
11247
7668851f
VS
11248 config->save_crtc_enabled =
11249 kcalloc(dev->mode_config.num_crtc,
11250 sizeof(bool), GFP_KERNEL);
11251 if (!config->save_crtc_enabled)
11252 return -ENOMEM;
11253
1aa4b628
DV
11254 config->save_encoder_crtcs =
11255 kcalloc(dev->mode_config.num_encoder,
11256 sizeof(struct drm_crtc *), GFP_KERNEL);
11257 if (!config->save_encoder_crtcs)
85f9eb71
DV
11258 return -ENOMEM;
11259
1aa4b628
DV
11260 config->save_connector_encoders =
11261 kcalloc(dev->mode_config.num_connector,
11262 sizeof(struct drm_encoder *), GFP_KERNEL);
11263 if (!config->save_connector_encoders)
85f9eb71
DV
11264 return -ENOMEM;
11265
11266 /* Copy data. Note that driver private data is not affected.
11267 * Should anything bad happen only the expected state is
11268 * restored, not the drivers personal bookkeeping.
11269 */
7668851f 11270 count = 0;
70e1e0ec 11271 for_each_crtc(dev, crtc) {
7668851f
VS
11272 config->save_crtc_enabled[count++] = crtc->enabled;
11273 }
11274
85f9eb71
DV
11275 count = 0;
11276 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11277 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11278 }
11279
11280 count = 0;
11281 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11282 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11283 }
11284
11285 return 0;
11286}
11287
11288static void intel_set_config_restore_state(struct drm_device *dev,
11289 struct intel_set_config *config)
11290{
7668851f 11291 struct intel_crtc *crtc;
9a935856
DV
11292 struct intel_encoder *encoder;
11293 struct intel_connector *connector;
85f9eb71
DV
11294 int count;
11295
7668851f 11296 count = 0;
d3fcc808 11297 for_each_intel_crtc(dev, crtc) {
7668851f 11298 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11299
11300 if (crtc->new_enabled)
6e3c9717 11301 crtc->new_config = crtc->config;
7bd0a8e7
VS
11302 else
11303 crtc->new_config = NULL;
7668851f
VS
11304 }
11305
85f9eb71 11306 count = 0;
b2784e15 11307 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11308 encoder->new_crtc =
11309 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11310 }
11311
11312 count = 0;
9a935856
DV
11313 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11314 connector->new_encoder =
11315 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11316 }
11317}
11318
e3de42b6 11319static bool
2e57f47d 11320is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11321{
11322 int i;
11323
2e57f47d
CW
11324 if (set->num_connectors == 0)
11325 return false;
11326
11327 if (WARN_ON(set->connectors == NULL))
11328 return false;
11329
11330 for (i = 0; i < set->num_connectors; i++)
11331 if (set->connectors[i]->encoder &&
11332 set->connectors[i]->encoder->crtc == set->crtc &&
11333 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11334 return true;
11335
11336 return false;
11337}
11338
5e2b584e
DV
11339static void
11340intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11341 struct intel_set_config *config)
11342{
11343
11344 /* We should be able to check here if the fb has the same properties
11345 * and then just flip_or_move it */
2e57f47d
CW
11346 if (is_crtc_connector_off(set)) {
11347 config->mode_changed = true;
f4510a27 11348 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11349 /*
11350 * If we have no fb, we can only flip as long as the crtc is
11351 * active, otherwise we need a full mode set. The crtc may
11352 * be active if we've only disabled the primary plane, or
11353 * in fastboot situations.
11354 */
f4510a27 11355 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11356 struct intel_crtc *intel_crtc =
11357 to_intel_crtc(set->crtc);
11358
3b150f08 11359 if (intel_crtc->active) {
319d9827
JB
11360 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11361 config->fb_changed = true;
11362 } else {
11363 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11364 config->mode_changed = true;
11365 }
5e2b584e
DV
11366 } else if (set->fb == NULL) {
11367 config->mode_changed = true;
72f4901e 11368 } else if (set->fb->pixel_format !=
f4510a27 11369 set->crtc->primary->fb->pixel_format) {
5e2b584e 11370 config->mode_changed = true;
e3de42b6 11371 } else {
5e2b584e 11372 config->fb_changed = true;
e3de42b6 11373 }
5e2b584e
DV
11374 }
11375
835c5873 11376 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11377 config->fb_changed = true;
11378
11379 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11380 DRM_DEBUG_KMS("modes are different, full mode set\n");
11381 drm_mode_debug_printmodeline(&set->crtc->mode);
11382 drm_mode_debug_printmodeline(set->mode);
11383 config->mode_changed = true;
11384 }
a1d95703
CW
11385
11386 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11387 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11388}
11389
2e431051 11390static int
9a935856
DV
11391intel_modeset_stage_output_state(struct drm_device *dev,
11392 struct drm_mode_set *set,
11393 struct intel_set_config *config)
50f56119 11394{
9a935856
DV
11395 struct intel_connector *connector;
11396 struct intel_encoder *encoder;
7668851f 11397 struct intel_crtc *crtc;
f3f08572 11398 int ro;
50f56119 11399
9abdda74 11400 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11401 * of connectors. For paranoia, double-check this. */
11402 WARN_ON(!set->fb && (set->num_connectors != 0));
11403 WARN_ON(set->fb && (set->num_connectors == 0));
11404
9a935856
DV
11405 list_for_each_entry(connector, &dev->mode_config.connector_list,
11406 base.head) {
11407 /* Otherwise traverse passed in connector list and get encoders
11408 * for them. */
50f56119 11409 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11410 if (set->connectors[ro] == &connector->base) {
0e32b39c 11411 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11412 break;
11413 }
11414 }
11415
9a935856
DV
11416 /* If we disable the crtc, disable all its connectors. Also, if
11417 * the connector is on the changing crtc but not on the new
11418 * connector list, disable it. */
11419 if ((!set->fb || ro == set->num_connectors) &&
11420 connector->base.encoder &&
11421 connector->base.encoder->crtc == set->crtc) {
11422 connector->new_encoder = NULL;
11423
11424 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11425 connector->base.base.id,
c23cc417 11426 connector->base.name);
9a935856
DV
11427 }
11428
11429
11430 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11431 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11432 config->mode_changed = true;
50f56119
DV
11433 }
11434 }
9a935856 11435 /* connector->new_encoder is now updated for all connectors. */
50f56119 11436
9a935856 11437 /* Update crtc of enabled connectors. */
9a935856
DV
11438 list_for_each_entry(connector, &dev->mode_config.connector_list,
11439 base.head) {
7668851f
VS
11440 struct drm_crtc *new_crtc;
11441
9a935856 11442 if (!connector->new_encoder)
50f56119
DV
11443 continue;
11444
9a935856 11445 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11446
11447 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11448 if (set->connectors[ro] == &connector->base)
50f56119
DV
11449 new_crtc = set->crtc;
11450 }
11451
11452 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11453 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11454 new_crtc)) {
5e2b584e 11455 return -EINVAL;
50f56119 11456 }
0e32b39c 11457 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11458
11459 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11460 connector->base.base.id,
c23cc417 11461 connector->base.name,
9a935856
DV
11462 new_crtc->base.id);
11463 }
11464
11465 /* Check for any encoders that needs to be disabled. */
b2784e15 11466 for_each_intel_encoder(dev, encoder) {
5a65f358 11467 int num_connectors = 0;
9a935856
DV
11468 list_for_each_entry(connector,
11469 &dev->mode_config.connector_list,
11470 base.head) {
11471 if (connector->new_encoder == encoder) {
11472 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11473 num_connectors++;
9a935856
DV
11474 }
11475 }
5a65f358
PZ
11476
11477 if (num_connectors == 0)
11478 encoder->new_crtc = NULL;
11479 else if (num_connectors > 1)
11480 return -EINVAL;
11481
9a935856
DV
11482 /* Only now check for crtc changes so we don't miss encoders
11483 * that will be disabled. */
11484 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11485 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11486 config->mode_changed = true;
50f56119
DV
11487 }
11488 }
9a935856 11489 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11490 list_for_each_entry(connector, &dev->mode_config.connector_list,
11491 base.head) {
11492 if (connector->new_encoder)
11493 if (connector->new_encoder != connector->encoder)
11494 connector->encoder = connector->new_encoder;
11495 }
d3fcc808 11496 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11497 crtc->new_enabled = false;
11498
b2784e15 11499 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11500 if (encoder->new_crtc == crtc) {
11501 crtc->new_enabled = true;
11502 break;
11503 }
11504 }
11505
11506 if (crtc->new_enabled != crtc->base.enabled) {
11507 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11508 crtc->new_enabled ? "en" : "dis");
11509 config->mode_changed = true;
11510 }
7bd0a8e7
VS
11511
11512 if (crtc->new_enabled)
6e3c9717 11513 crtc->new_config = crtc->config;
7bd0a8e7
VS
11514 else
11515 crtc->new_config = NULL;
7668851f
VS
11516 }
11517
2e431051
DV
11518 return 0;
11519}
11520
7d00a1f5
VS
11521static void disable_crtc_nofb(struct intel_crtc *crtc)
11522{
11523 struct drm_device *dev = crtc->base.dev;
11524 struct intel_encoder *encoder;
11525 struct intel_connector *connector;
11526
11527 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11528 pipe_name(crtc->pipe));
11529
11530 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11531 if (connector->new_encoder &&
11532 connector->new_encoder->new_crtc == crtc)
11533 connector->new_encoder = NULL;
11534 }
11535
b2784e15 11536 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11537 if (encoder->new_crtc == crtc)
11538 encoder->new_crtc = NULL;
11539 }
11540
11541 crtc->new_enabled = false;
7bd0a8e7 11542 crtc->new_config = NULL;
7d00a1f5
VS
11543}
11544
2e431051
DV
11545static int intel_crtc_set_config(struct drm_mode_set *set)
11546{
11547 struct drm_device *dev;
2e431051
DV
11548 struct drm_mode_set save_set;
11549 struct intel_set_config *config;
5cec258b 11550 struct intel_crtc_state *pipe_config;
50f52756 11551 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 11552 int ret;
2e431051 11553
8d3e375e
DV
11554 BUG_ON(!set);
11555 BUG_ON(!set->crtc);
11556 BUG_ON(!set->crtc->helper_private);
2e431051 11557
7e53f3a4
DV
11558 /* Enforce sane interface api - has been abused by the fb helper. */
11559 BUG_ON(!set->mode && set->fb);
11560 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11561
2e431051
DV
11562 if (set->fb) {
11563 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11564 set->crtc->base.id, set->fb->base.id,
11565 (int)set->num_connectors, set->x, set->y);
11566 } else {
11567 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11568 }
11569
11570 dev = set->crtc->dev;
11571
11572 ret = -ENOMEM;
11573 config = kzalloc(sizeof(*config), GFP_KERNEL);
11574 if (!config)
11575 goto out_config;
11576
11577 ret = intel_set_config_save_state(dev, config);
11578 if (ret)
11579 goto out_config;
11580
11581 save_set.crtc = set->crtc;
11582 save_set.mode = &set->crtc->mode;
11583 save_set.x = set->crtc->x;
11584 save_set.y = set->crtc->y;
f4510a27 11585 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11586
11587 /* Compute whether we need a full modeset, only an fb base update or no
11588 * change at all. In the future we might also check whether only the
11589 * mode changed, e.g. for LVDS where we only change the panel fitter in
11590 * such cases. */
11591 intel_set_config_compute_mode_changes(set, config);
11592
9a935856 11593 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11594 if (ret)
11595 goto fail;
11596
50f52756
JB
11597 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11598 set->fb,
11599 &modeset_pipes,
11600 &prepare_pipes,
11601 &disable_pipes);
20664591 11602 if (IS_ERR(pipe_config)) {
6ac0483b 11603 ret = PTR_ERR(pipe_config);
50f52756 11604 goto fail;
20664591 11605 } else if (pipe_config) {
b9950a13 11606 if (pipe_config->has_audio !=
6e3c9717 11607 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
11608 config->mode_changed = true;
11609
af15d2ce
JB
11610 /*
11611 * Note we have an issue here with infoframes: current code
11612 * only updates them on the full mode set path per hw
11613 * requirements. So here we should be checking for any
11614 * required changes and forcing a mode set.
11615 */
20664591 11616 }
50f52756
JB
11617
11618 /* set_mode will free it in the mode_changed case */
11619 if (!config->mode_changed)
11620 kfree(pipe_config);
11621
1f9954d0
JB
11622 intel_update_pipe_size(to_intel_crtc(set->crtc));
11623
5e2b584e 11624 if (config->mode_changed) {
50f52756
JB
11625 ret = intel_set_mode_pipes(set->crtc, set->mode,
11626 set->x, set->y, set->fb, pipe_config,
11627 modeset_pipes, prepare_pipes,
11628 disable_pipes);
5e2b584e 11629 } else if (config->fb_changed) {
3b150f08 11630 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808
GP
11631 struct drm_plane *primary = set->crtc->primary;
11632 int vdisplay, hdisplay;
3b150f08 11633
455a6808
GP
11634 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11635 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11636 0, 0, hdisplay, vdisplay,
11637 set->x << 16, set->y << 16,
11638 hdisplay << 16, vdisplay << 16);
3b150f08
MR
11639
11640 /*
11641 * We need to make sure the primary plane is re-enabled if it
11642 * has previously been turned off.
11643 */
11644 if (!intel_crtc->primary_enabled && ret == 0) {
11645 WARN_ON(!intel_crtc->active);
fdd508a6 11646 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11647 }
11648
7ca51a3a
JB
11649 /*
11650 * In the fastboot case this may be our only check of the
11651 * state after boot. It would be better to only do it on
11652 * the first update, but we don't have a nice way of doing that
11653 * (and really, set_config isn't used much for high freq page
11654 * flipping, so increasing its cost here shouldn't be a big
11655 * deal).
11656 */
d330a953 11657 if (i915.fastboot && ret == 0)
7ca51a3a 11658 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11659 }
11660
2d05eae1 11661 if (ret) {
bf67dfeb
DV
11662 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11663 set->crtc->base.id, ret);
50f56119 11664fail:
2d05eae1 11665 intel_set_config_restore_state(dev, config);
50f56119 11666
7d00a1f5
VS
11667 /*
11668 * HACK: if the pipe was on, but we didn't have a framebuffer,
11669 * force the pipe off to avoid oopsing in the modeset code
11670 * due to fb==NULL. This should only happen during boot since
11671 * we don't yet reconstruct the FB from the hardware state.
11672 */
11673 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11674 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11675
2d05eae1
CW
11676 /* Try to restore the config */
11677 if (config->mode_changed &&
11678 intel_set_mode(save_set.crtc, save_set.mode,
11679 save_set.x, save_set.y, save_set.fb))
11680 DRM_ERROR("failed to restore config after modeset failure\n");
11681 }
50f56119 11682
d9e55608
DV
11683out_config:
11684 intel_set_config_free(config);
50f56119
DV
11685 return ret;
11686}
f6e5b160
CW
11687
11688static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11689 .gamma_set = intel_crtc_gamma_set,
50f56119 11690 .set_config = intel_crtc_set_config,
f6e5b160
CW
11691 .destroy = intel_crtc_destroy,
11692 .page_flip = intel_crtc_page_flip,
1356837e
MR
11693 .atomic_duplicate_state = intel_crtc_duplicate_state,
11694 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
11695};
11696
5358901f
DV
11697static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11698 struct intel_shared_dpll *pll,
11699 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11700{
5358901f 11701 uint32_t val;
ee7b9f93 11702
f458ebbc 11703 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
11704 return false;
11705
5358901f 11706 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11707 hw_state->dpll = val;
11708 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11709 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11710
11711 return val & DPLL_VCO_ENABLE;
11712}
11713
15bdd4cf
DV
11714static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11715 struct intel_shared_dpll *pll)
11716{
3e369b76
ACO
11717 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11718 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
11719}
11720
e7b903d2
DV
11721static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11722 struct intel_shared_dpll *pll)
11723{
e7b903d2 11724 /* PCH refclock must be enabled first */
89eff4be 11725 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11726
3e369b76 11727 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
11728
11729 /* Wait for the clocks to stabilize. */
11730 POSTING_READ(PCH_DPLL(pll->id));
11731 udelay(150);
11732
11733 /* The pixel multiplier can only be updated once the
11734 * DPLL is enabled and the clocks are stable.
11735 *
11736 * So write it again.
11737 */
3e369b76 11738 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 11739 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11740 udelay(200);
11741}
11742
11743static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11744 struct intel_shared_dpll *pll)
11745{
11746 struct drm_device *dev = dev_priv->dev;
11747 struct intel_crtc *crtc;
e7b903d2
DV
11748
11749 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11750 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11751 if (intel_crtc_to_shared_dpll(crtc) == pll)
11752 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11753 }
11754
15bdd4cf
DV
11755 I915_WRITE(PCH_DPLL(pll->id), 0);
11756 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11757 udelay(200);
11758}
11759
46edb027
DV
11760static char *ibx_pch_dpll_names[] = {
11761 "PCH DPLL A",
11762 "PCH DPLL B",
11763};
11764
7c74ade1 11765static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11766{
e7b903d2 11767 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11768 int i;
11769
7c74ade1 11770 dev_priv->num_shared_dpll = 2;
ee7b9f93 11771
e72f9fbf 11772 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11773 dev_priv->shared_dplls[i].id = i;
11774 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11775 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11776 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11777 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11778 dev_priv->shared_dplls[i].get_hw_state =
11779 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11780 }
11781}
11782
7c74ade1
DV
11783static void intel_shared_dpll_init(struct drm_device *dev)
11784{
e7b903d2 11785 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11786
9cd86933
DV
11787 if (HAS_DDI(dev))
11788 intel_ddi_pll_init(dev);
11789 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11790 ibx_pch_dpll_init(dev);
11791 else
11792 dev_priv->num_shared_dpll = 0;
11793
11794 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11795}
11796
6beb8c23
MR
11797/**
11798 * intel_prepare_plane_fb - Prepare fb for usage on plane
11799 * @plane: drm plane to prepare for
11800 * @fb: framebuffer to prepare for presentation
11801 *
11802 * Prepares a framebuffer for usage on a display plane. Generally this
11803 * involves pinning the underlying object and updating the frontbuffer tracking
11804 * bits. Some older platforms need special physical address handling for
11805 * cursor planes.
11806 *
11807 * Returns 0 on success, negative error code on failure.
11808 */
11809int
11810intel_prepare_plane_fb(struct drm_plane *plane,
11811 struct drm_framebuffer *fb)
465c120c
MR
11812{
11813 struct drm_device *dev = plane->dev;
6beb8c23
MR
11814 struct intel_plane *intel_plane = to_intel_plane(plane);
11815 enum pipe pipe = intel_plane->pipe;
11816 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11817 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11818 unsigned frontbuffer_bits = 0;
11819 int ret = 0;
465c120c 11820
ea2c67bb 11821 if (!obj)
465c120c
MR
11822 return 0;
11823
6beb8c23
MR
11824 switch (plane->type) {
11825 case DRM_PLANE_TYPE_PRIMARY:
11826 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
11827 break;
11828 case DRM_PLANE_TYPE_CURSOR:
11829 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
11830 break;
11831 case DRM_PLANE_TYPE_OVERLAY:
11832 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
11833 break;
11834 }
465c120c 11835
6beb8c23 11836 mutex_lock(&dev->struct_mutex);
465c120c 11837
6beb8c23
MR
11838 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11839 INTEL_INFO(dev)->cursor_needs_physical) {
11840 int align = IS_I830(dev) ? 16 * 1024 : 256;
11841 ret = i915_gem_object_attach_phys(obj, align);
11842 if (ret)
11843 DRM_DEBUG_KMS("failed to attach phys object\n");
11844 } else {
11845 ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
11846 }
465c120c 11847
6beb8c23
MR
11848 if (ret == 0)
11849 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 11850
4c34574f 11851 mutex_unlock(&dev->struct_mutex);
465c120c 11852
6beb8c23
MR
11853 return ret;
11854}
11855
38f3ce3a
MR
11856/**
11857 * intel_cleanup_plane_fb - Cleans up an fb after plane use
11858 * @plane: drm plane to clean up for
11859 * @fb: old framebuffer that was on plane
11860 *
11861 * Cleans up a framebuffer that has just been removed from a plane.
11862 */
11863void
11864intel_cleanup_plane_fb(struct drm_plane *plane,
11865 struct drm_framebuffer *fb)
11866{
11867 struct drm_device *dev = plane->dev;
11868 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11869
11870 if (WARN_ON(!obj))
11871 return;
11872
11873 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
11874 !INTEL_INFO(dev)->cursor_needs_physical) {
11875 mutex_lock(&dev->struct_mutex);
11876 intel_unpin_fb_obj(obj);
11877 mutex_unlock(&dev->struct_mutex);
11878 }
465c120c
MR
11879}
11880
11881static int
3c692a41
GP
11882intel_check_primary_plane(struct drm_plane *plane,
11883 struct intel_plane_state *state)
11884{
32b7eeec
MR
11885 struct drm_device *dev = plane->dev;
11886 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 11887 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 11888 struct intel_crtc *intel_crtc;
2b875c22 11889 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
11890 struct drm_rect *dest = &state->dst;
11891 struct drm_rect *src = &state->src;
11892 const struct drm_rect *clip = &state->clip;
465c120c
MR
11893 int ret;
11894
ea2c67bb
MR
11895 crtc = crtc ? crtc : plane->crtc;
11896 intel_crtc = to_intel_crtc(crtc);
11897
c59cb179
MR
11898 ret = drm_plane_helper_check_update(plane, crtc, fb,
11899 src, dest, clip,
11900 DRM_PLANE_HELPER_NO_SCALING,
11901 DRM_PLANE_HELPER_NO_SCALING,
11902 false, true, &state->visible);
11903 if (ret)
11904 return ret;
465c120c 11905
32b7eeec
MR
11906 if (intel_crtc->active) {
11907 intel_crtc->atomic.wait_for_flips = true;
11908
11909 /*
11910 * FBC does not work on some platforms for rotated
11911 * planes, so disable it when rotation is not 0 and
11912 * update it when rotation is set back to 0.
11913 *
11914 * FIXME: This is redundant with the fbc update done in
11915 * the primary plane enable function except that that
11916 * one is done too late. We eventually need to unify
11917 * this.
11918 */
11919 if (intel_crtc->primary_enabled &&
11920 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11921 dev_priv->fbc.plane == intel_crtc->plane &&
8e7d688b 11922 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
11923 intel_crtc->atomic.disable_fbc = true;
11924 }
11925
11926 if (state->visible) {
11927 /*
11928 * BDW signals flip done immediately if the plane
11929 * is disabled, even if the plane enable is already
11930 * armed to occur at the next vblank :(
11931 */
11932 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
11933 intel_crtc->atomic.wait_vblank = true;
11934 }
11935
11936 intel_crtc->atomic.fb_bits |=
11937 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11938
11939 intel_crtc->atomic.update_fbc = true;
ccc759dc
GP
11940 }
11941
14af293f
GP
11942 return 0;
11943}
11944
11945static void
11946intel_commit_primary_plane(struct drm_plane *plane,
11947 struct intel_plane_state *state)
11948{
2b875c22
MR
11949 struct drm_crtc *crtc = state->base.crtc;
11950 struct drm_framebuffer *fb = state->base.fb;
11951 struct drm_device *dev = plane->dev;
14af293f 11952 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 11953 struct intel_crtc *intel_crtc;
14af293f 11954 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
14af293f
GP
11955 struct intel_plane *intel_plane = to_intel_plane(plane);
11956 struct drm_rect *src = &state->src;
11957
ea2c67bb
MR
11958 crtc = crtc ? crtc : plane->crtc;
11959 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
11960
11961 plane->fb = fb;
9dc806fc
MR
11962 crtc->x = src->x1 >> 16;
11963 crtc->y = src->y1 >> 16;
ccc759dc 11964
ccc759dc 11965 intel_plane->obj = obj;
4c34574f 11966
ccc759dc 11967 if (intel_crtc->active) {
ccc759dc 11968 if (state->visible) {
ccc759dc
GP
11969 /* FIXME: kill this fastboot hack */
11970 intel_update_pipe_size(intel_crtc);
465c120c 11971
ccc759dc 11972 intel_crtc->primary_enabled = true;
465c120c 11973
ccc759dc
GP
11974 dev_priv->display.update_primary_plane(crtc, plane->fb,
11975 crtc->x, crtc->y);
ccc759dc
GP
11976 } else {
11977 /*
11978 * If clipping results in a non-visible primary plane,
11979 * we'll disable the primary plane. Note that this is
11980 * a bit different than what happens if userspace
11981 * explicitly disables the plane by passing fb=0
11982 * because plane->fb still gets set and pinned.
11983 */
11984 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 11985 }
ccc759dc 11986 }
465c120c
MR
11987}
11988
32b7eeec 11989static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 11990{
32b7eeec 11991 struct drm_device *dev = crtc->dev;
140fd38d 11992 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 11993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
11994 struct intel_plane *intel_plane;
11995 struct drm_plane *p;
11996 unsigned fb_bits = 0;
11997
11998 /* Track fb's for any planes being disabled */
11999 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12000 intel_plane = to_intel_plane(p);
12001
12002 if (intel_crtc->atomic.disabled_planes &
12003 (1 << drm_plane_index(p))) {
12004 switch (p->type) {
12005 case DRM_PLANE_TYPE_PRIMARY:
12006 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12007 break;
12008 case DRM_PLANE_TYPE_CURSOR:
12009 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12010 break;
12011 case DRM_PLANE_TYPE_OVERLAY:
12012 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12013 break;
12014 }
3c692a41 12015
ea2c67bb
MR
12016 mutex_lock(&dev->struct_mutex);
12017 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12018 mutex_unlock(&dev->struct_mutex);
12019 }
12020 }
3c692a41 12021
32b7eeec
MR
12022 if (intel_crtc->atomic.wait_for_flips)
12023 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 12024
32b7eeec
MR
12025 if (intel_crtc->atomic.disable_fbc)
12026 intel_fbc_disable(dev);
3c692a41 12027
32b7eeec
MR
12028 if (intel_crtc->atomic.pre_disable_primary)
12029 intel_pre_disable_primary(crtc);
3c692a41 12030
32b7eeec
MR
12031 if (intel_crtc->atomic.update_wm)
12032 intel_update_watermarks(crtc);
3c692a41 12033
32b7eeec 12034 intel_runtime_pm_get(dev_priv);
3c692a41 12035
c34c9ee4
MR
12036 /* Perform vblank evasion around commit operation */
12037 if (intel_crtc->active)
12038 intel_crtc->atomic.evade =
12039 intel_pipe_update_start(intel_crtc,
12040 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
12041}
12042
12043static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12044{
12045 struct drm_device *dev = crtc->dev;
12046 struct drm_i915_private *dev_priv = dev->dev_private;
12047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12048 struct drm_plane *p;
12049
c34c9ee4
MR
12050 if (intel_crtc->atomic.evade)
12051 intel_pipe_update_end(intel_crtc,
12052 intel_crtc->atomic.start_vbl_count);
3c692a41 12053
140fd38d 12054 intel_runtime_pm_put(dev_priv);
3c692a41 12055
32b7eeec
MR
12056 if (intel_crtc->atomic.wait_vblank)
12057 intel_wait_for_vblank(dev, intel_crtc->pipe);
12058
12059 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12060
12061 if (intel_crtc->atomic.update_fbc) {
ccc759dc 12062 mutex_lock(&dev->struct_mutex);
7ff0ebcc 12063 intel_fbc_update(dev);
ccc759dc 12064 mutex_unlock(&dev->struct_mutex);
38f3ce3a 12065 }
3c692a41 12066
32b7eeec
MR
12067 if (intel_crtc->atomic.post_enable_primary)
12068 intel_post_enable_primary(crtc);
3c692a41 12069
32b7eeec
MR
12070 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12071 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12072 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12073 false, false);
12074
12075 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
12076}
12077
cf4c7c12 12078/**
4a3b8769
MR
12079 * intel_plane_destroy - destroy a plane
12080 * @plane: plane to destroy
cf4c7c12 12081 *
4a3b8769
MR
12082 * Common destruction function for all types of planes (primary, cursor,
12083 * sprite).
cf4c7c12 12084 */
4a3b8769 12085void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
12086{
12087 struct intel_plane *intel_plane = to_intel_plane(plane);
12088 drm_plane_cleanup(plane);
12089 kfree(intel_plane);
12090}
12091
65a3fea0 12092const struct drm_plane_funcs intel_plane_funcs = {
3f678c96
MR
12093 .update_plane = drm_atomic_helper_update_plane,
12094 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 12095 .destroy = intel_plane_destroy,
c196e1d6 12096 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
12097 .atomic_get_property = intel_plane_atomic_get_property,
12098 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
12099 .atomic_duplicate_state = intel_plane_duplicate_state,
12100 .atomic_destroy_state = intel_plane_destroy_state,
12101
465c120c
MR
12102};
12103
12104static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12105 int pipe)
12106{
12107 struct intel_plane *primary;
8e7d688b 12108 struct intel_plane_state *state;
465c120c
MR
12109 const uint32_t *intel_primary_formats;
12110 int num_formats;
12111
12112 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12113 if (primary == NULL)
12114 return NULL;
12115
8e7d688b
MR
12116 state = intel_create_plane_state(&primary->base);
12117 if (!state) {
ea2c67bb
MR
12118 kfree(primary);
12119 return NULL;
12120 }
8e7d688b 12121 primary->base.state = &state->base;
ea2c67bb 12122
465c120c
MR
12123 primary->can_scale = false;
12124 primary->max_downscale = 1;
12125 primary->pipe = pipe;
12126 primary->plane = pipe;
c59cb179
MR
12127 primary->check_plane = intel_check_primary_plane;
12128 primary->commit_plane = intel_commit_primary_plane;
465c120c
MR
12129 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12130 primary->plane = !pipe;
12131
12132 if (INTEL_INFO(dev)->gen <= 3) {
12133 intel_primary_formats = intel_primary_formats_gen2;
12134 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12135 } else {
12136 intel_primary_formats = intel_primary_formats_gen4;
12137 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12138 }
12139
12140 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 12141 &intel_plane_funcs,
465c120c
MR
12142 intel_primary_formats, num_formats,
12143 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
12144
12145 if (INTEL_INFO(dev)->gen >= 4) {
12146 if (!dev->mode_config.rotation_property)
12147 dev->mode_config.rotation_property =
12148 drm_mode_create_rotation_property(dev,
12149 BIT(DRM_ROTATE_0) |
12150 BIT(DRM_ROTATE_180));
12151 if (dev->mode_config.rotation_property)
12152 drm_object_attach_property(&primary->base.base,
12153 dev->mode_config.rotation_property,
8e7d688b 12154 state->base.rotation);
48404c1e
SJ
12155 }
12156
ea2c67bb
MR
12157 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12158
465c120c
MR
12159 return &primary->base;
12160}
12161
3d7d6510 12162static int
852e787c
GP
12163intel_check_cursor_plane(struct drm_plane *plane,
12164 struct intel_plane_state *state)
3d7d6510 12165{
2b875c22 12166 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 12167 struct drm_device *dev = plane->dev;
2b875c22 12168 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
12169 struct drm_rect *dest = &state->dst;
12170 struct drm_rect *src = &state->src;
12171 const struct drm_rect *clip = &state->clip;
757f9a3e 12172 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 12173 struct intel_crtc *intel_crtc;
757f9a3e
GP
12174 unsigned stride;
12175 int ret;
3d7d6510 12176
ea2c67bb
MR
12177 crtc = crtc ? crtc : plane->crtc;
12178 intel_crtc = to_intel_crtc(crtc);
12179
757f9a3e 12180 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 12181 src, dest, clip,
3d7d6510
MR
12182 DRM_PLANE_HELPER_NO_SCALING,
12183 DRM_PLANE_HELPER_NO_SCALING,
852e787c 12184 true, true, &state->visible);
757f9a3e
GP
12185 if (ret)
12186 return ret;
12187
12188
12189 /* if we want to turn off the cursor ignore width and height */
12190 if (!obj)
32b7eeec 12191 goto finish;
757f9a3e 12192
757f9a3e 12193 /* Check for which cursor types we support */
ea2c67bb
MR
12194 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12195 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12196 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
12197 return -EINVAL;
12198 }
12199
ea2c67bb
MR
12200 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12201 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
12202 DRM_DEBUG_KMS("buffer is too small\n");
12203 return -ENOMEM;
12204 }
12205
e391ea88
GP
12206 if (fb == crtc->cursor->fb)
12207 return 0;
12208
757f9a3e
GP
12209 /* we only need to pin inside GTT if cursor is non-phy */
12210 mutex_lock(&dev->struct_mutex);
12211 if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
12212 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12213 ret = -EINVAL;
12214 }
12215 mutex_unlock(&dev->struct_mutex);
12216
32b7eeec
MR
12217finish:
12218 if (intel_crtc->active) {
ea2c67bb 12219 if (intel_crtc->cursor_width != state->base.crtc_w)
32b7eeec
MR
12220 intel_crtc->atomic.update_wm = true;
12221
12222 intel_crtc->atomic.fb_bits |=
12223 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12224 }
12225
757f9a3e 12226 return ret;
852e787c 12227}
3d7d6510 12228
f4a2cf29 12229static void
852e787c
GP
12230intel_commit_cursor_plane(struct drm_plane *plane,
12231 struct intel_plane_state *state)
12232{
2b875c22 12233 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
12234 struct drm_device *dev = plane->dev;
12235 struct intel_crtc *intel_crtc;
a919db90 12236 struct intel_plane *intel_plane = to_intel_plane(plane);
2b875c22 12237 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 12238 uint32_t addr;
852e787c 12239
ea2c67bb
MR
12240 crtc = crtc ? crtc : plane->crtc;
12241 intel_crtc = to_intel_crtc(crtc);
12242
2b875c22 12243 plane->fb = state->base.fb;
ea2c67bb
MR
12244 crtc->cursor_x = state->base.crtc_x;
12245 crtc->cursor_y = state->base.crtc_y;
12246
a919db90
SJ
12247 intel_plane->obj = obj;
12248
a912f12f
GP
12249 if (intel_crtc->cursor_bo == obj)
12250 goto update;
4ed91096 12251
f4a2cf29 12252 if (!obj)
a912f12f 12253 addr = 0;
f4a2cf29 12254 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 12255 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 12256 else
a912f12f 12257 addr = obj->phys_handle->busaddr;
852e787c 12258
a912f12f
GP
12259 intel_crtc->cursor_addr = addr;
12260 intel_crtc->cursor_bo = obj;
12261update:
ea2c67bb
MR
12262 intel_crtc->cursor_width = state->base.crtc_w;
12263 intel_crtc->cursor_height = state->base.crtc_h;
852e787c 12264
32b7eeec 12265 if (intel_crtc->active)
a912f12f 12266 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
12267}
12268
3d7d6510
MR
12269static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12270 int pipe)
12271{
12272 struct intel_plane *cursor;
8e7d688b 12273 struct intel_plane_state *state;
3d7d6510
MR
12274
12275 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12276 if (cursor == NULL)
12277 return NULL;
12278
8e7d688b
MR
12279 state = intel_create_plane_state(&cursor->base);
12280 if (!state) {
ea2c67bb
MR
12281 kfree(cursor);
12282 return NULL;
12283 }
8e7d688b 12284 cursor->base.state = &state->base;
ea2c67bb 12285
3d7d6510
MR
12286 cursor->can_scale = false;
12287 cursor->max_downscale = 1;
12288 cursor->pipe = pipe;
12289 cursor->plane = pipe;
c59cb179
MR
12290 cursor->check_plane = intel_check_cursor_plane;
12291 cursor->commit_plane = intel_commit_cursor_plane;
3d7d6510
MR
12292
12293 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 12294 &intel_plane_funcs,
3d7d6510
MR
12295 intel_cursor_formats,
12296 ARRAY_SIZE(intel_cursor_formats),
12297 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
12298
12299 if (INTEL_INFO(dev)->gen >= 4) {
12300 if (!dev->mode_config.rotation_property)
12301 dev->mode_config.rotation_property =
12302 drm_mode_create_rotation_property(dev,
12303 BIT(DRM_ROTATE_0) |
12304 BIT(DRM_ROTATE_180));
12305 if (dev->mode_config.rotation_property)
12306 drm_object_attach_property(&cursor->base.base,
12307 dev->mode_config.rotation_property,
8e7d688b 12308 state->base.rotation);
4398ad45
VS
12309 }
12310
ea2c67bb
MR
12311 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12312
3d7d6510
MR
12313 return &cursor->base;
12314}
12315
b358d0a6 12316static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12317{
fbee40df 12318 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12319 struct intel_crtc *intel_crtc;
f5de6e07 12320 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
12321 struct drm_plane *primary = NULL;
12322 struct drm_plane *cursor = NULL;
465c120c 12323 int i, ret;
79e53945 12324
955382f3 12325 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12326 if (intel_crtc == NULL)
12327 return;
12328
f5de6e07
ACO
12329 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12330 if (!crtc_state)
12331 goto fail;
12332 intel_crtc_set_state(intel_crtc, crtc_state);
12333
465c120c 12334 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12335 if (!primary)
12336 goto fail;
12337
12338 cursor = intel_cursor_plane_create(dev, pipe);
12339 if (!cursor)
12340 goto fail;
12341
465c120c 12342 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12343 cursor, &intel_crtc_funcs);
12344 if (ret)
12345 goto fail;
79e53945
JB
12346
12347 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12348 for (i = 0; i < 256; i++) {
12349 intel_crtc->lut_r[i] = i;
12350 intel_crtc->lut_g[i] = i;
12351 intel_crtc->lut_b[i] = i;
12352 }
12353
1f1c2e24
VS
12354 /*
12355 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12356 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12357 */
80824003
JB
12358 intel_crtc->pipe = pipe;
12359 intel_crtc->plane = pipe;
3a77c4c4 12360 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12361 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12362 intel_crtc->plane = !pipe;
80824003
JB
12363 }
12364
4b0e333e
CW
12365 intel_crtc->cursor_base = ~0;
12366 intel_crtc->cursor_cntl = ~0;
dc41c154 12367 intel_crtc->cursor_size = ~0;
8d7849db 12368
22fd0fab
JB
12369 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12370 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12371 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12372 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12373
9362c7c5
ACO
12374 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12375
79e53945 12376 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12377
12378 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12379 return;
12380
12381fail:
12382 if (primary)
12383 drm_plane_cleanup(primary);
12384 if (cursor)
12385 drm_plane_cleanup(cursor);
f5de6e07 12386 kfree(crtc_state);
3d7d6510 12387 kfree(intel_crtc);
79e53945
JB
12388}
12389
752aa88a
JB
12390enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12391{
12392 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12393 struct drm_device *dev = connector->base.dev;
752aa88a 12394
51fd371b 12395 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 12396
d3babd3f 12397 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
12398 return INVALID_PIPE;
12399
12400 return to_intel_crtc(encoder->crtc)->pipe;
12401}
12402
08d7b3d1 12403int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12404 struct drm_file *file)
08d7b3d1 12405{
08d7b3d1 12406 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12407 struct drm_crtc *drmmode_crtc;
c05422d5 12408 struct intel_crtc *crtc;
08d7b3d1 12409
1cff8f6b
DV
12410 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12411 return -ENODEV;
08d7b3d1 12412
7707e653 12413 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12414
7707e653 12415 if (!drmmode_crtc) {
08d7b3d1 12416 DRM_ERROR("no such CRTC id\n");
3f2c2057 12417 return -ENOENT;
08d7b3d1
CW
12418 }
12419
7707e653 12420 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12421 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12422
c05422d5 12423 return 0;
08d7b3d1
CW
12424}
12425
66a9278e 12426static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12427{
66a9278e
DV
12428 struct drm_device *dev = encoder->base.dev;
12429 struct intel_encoder *source_encoder;
79e53945 12430 int index_mask = 0;
79e53945
JB
12431 int entry = 0;
12432
b2784e15 12433 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12434 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12435 index_mask |= (1 << entry);
12436
79e53945
JB
12437 entry++;
12438 }
4ef69c7a 12439
79e53945
JB
12440 return index_mask;
12441}
12442
4d302442
CW
12443static bool has_edp_a(struct drm_device *dev)
12444{
12445 struct drm_i915_private *dev_priv = dev->dev_private;
12446
12447 if (!IS_MOBILE(dev))
12448 return false;
12449
12450 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12451 return false;
12452
e3589908 12453 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12454 return false;
12455
12456 return true;
12457}
12458
84b4e042
JB
12459static bool intel_crt_present(struct drm_device *dev)
12460{
12461 struct drm_i915_private *dev_priv = dev->dev_private;
12462
884497ed
DL
12463 if (INTEL_INFO(dev)->gen >= 9)
12464 return false;
12465
cf404ce4 12466 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
12467 return false;
12468
12469 if (IS_CHERRYVIEW(dev))
12470 return false;
12471
12472 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12473 return false;
12474
12475 return true;
12476}
12477
79e53945
JB
12478static void intel_setup_outputs(struct drm_device *dev)
12479{
725e30ad 12480 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12481 struct intel_encoder *encoder;
c6f95f27 12482 struct drm_connector *connector;
cb0953d7 12483 bool dpd_is_edp = false;
79e53945 12484
c9093354 12485 intel_lvds_init(dev);
79e53945 12486
84b4e042 12487 if (intel_crt_present(dev))
79935fca 12488 intel_crt_init(dev);
cb0953d7 12489
affa9354 12490 if (HAS_DDI(dev)) {
0e72a5b5
ED
12491 int found;
12492
12493 /* Haswell uses DDI functions to detect digital outputs */
12494 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12495 /* DDI A only supports eDP */
12496 if (found)
12497 intel_ddi_init(dev, PORT_A);
12498
12499 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12500 * register */
12501 found = I915_READ(SFUSE_STRAP);
12502
12503 if (found & SFUSE_STRAP_DDIB_DETECTED)
12504 intel_ddi_init(dev, PORT_B);
12505 if (found & SFUSE_STRAP_DDIC_DETECTED)
12506 intel_ddi_init(dev, PORT_C);
12507 if (found & SFUSE_STRAP_DDID_DETECTED)
12508 intel_ddi_init(dev, PORT_D);
12509 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12510 int found;
5d8a7752 12511 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12512
12513 if (has_edp_a(dev))
12514 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12515
dc0fa718 12516 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12517 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12518 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12519 if (!found)
e2debe91 12520 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12521 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12522 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12523 }
12524
dc0fa718 12525 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12526 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12527
dc0fa718 12528 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12529 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12530
5eb08b69 12531 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12532 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12533
270b3042 12534 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12535 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12536 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
12537 /*
12538 * The DP_DETECTED bit is the latched state of the DDC
12539 * SDA pin at boot. However since eDP doesn't require DDC
12540 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12541 * eDP ports may have been muxed to an alternate function.
12542 * Thus we can't rely on the DP_DETECTED bit alone to detect
12543 * eDP ports. Consult the VBT as well as DP_DETECTED to
12544 * detect eDP ports.
12545 */
d2182a66
VS
12546 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12547 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
12548 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12549 PORT_B);
e17ac6db
VS
12550 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12551 intel_dp_is_edp(dev, PORT_B))
12552 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 12553
d2182a66
VS
12554 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12555 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
12556 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12557 PORT_C);
e17ac6db
VS
12558 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12559 intel_dp_is_edp(dev, PORT_C))
12560 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 12561
9418c1f1 12562 if (IS_CHERRYVIEW(dev)) {
e17ac6db 12563 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
12564 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12565 PORT_D);
e17ac6db
VS
12566 /* eDP not supported on port D, so don't check VBT */
12567 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12568 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
12569 }
12570
3cfca973 12571 intel_dsi_init(dev);
103a196f 12572 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12573 bool found = false;
7d57382e 12574
e2debe91 12575 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12576 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12577 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12578 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12579 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12580 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12581 }
27185ae1 12582
e7281eab 12583 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12584 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12585 }
13520b05
KH
12586
12587 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12588
e2debe91 12589 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12590 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12591 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12592 }
27185ae1 12593
e2debe91 12594 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12595
b01f2c3a
JB
12596 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12597 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12598 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12599 }
e7281eab 12600 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12601 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12602 }
27185ae1 12603
b01f2c3a 12604 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12605 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12606 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12607 } else if (IS_GEN2(dev))
79e53945
JB
12608 intel_dvo_init(dev);
12609
103a196f 12610 if (SUPPORTS_TV(dev))
79e53945
JB
12611 intel_tv_init(dev);
12612
c6f95f27
MR
12613 /*
12614 * FIXME: We don't have full atomic support yet, but we want to be
12615 * able to enable/test plane updates via the atomic interface in the
12616 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12617 * will take some atomic codepaths to lookup properties during
12618 * drmModeGetConnector() that unconditionally dereference
12619 * connector->state.
12620 *
12621 * We create a dummy connector state here for each connector to ensure
12622 * the DRM core doesn't try to dereference a NULL connector->state.
12623 * The actual connector properties will never be updated or contain
12624 * useful information, but since we're doing this specifically for
12625 * testing/debug of the plane operations (and only when a specific
12626 * kernel module option is given), that shouldn't really matter.
12627 *
12628 * Once atomic support for crtc's + connectors lands, this loop should
12629 * be removed since we'll be setting up real connector state, which
12630 * will contain Intel-specific properties.
12631 */
12632 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12633 list_for_each_entry(connector,
12634 &dev->mode_config.connector_list,
12635 head) {
12636 if (!WARN_ON(connector->state)) {
12637 connector->state =
12638 kzalloc(sizeof(*connector->state),
12639 GFP_KERNEL);
12640 }
12641 }
12642 }
12643
0bc12bcb 12644 intel_psr_init(dev);
7c8f8a70 12645
b2784e15 12646 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12647 encoder->base.possible_crtcs = encoder->crtc_mask;
12648 encoder->base.possible_clones =
66a9278e 12649 intel_encoder_clones(encoder);
79e53945 12650 }
47356eb6 12651
dde86e2d 12652 intel_init_pch_refclk(dev);
270b3042
DV
12653
12654 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12655}
12656
12657static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12658{
60a5ca01 12659 struct drm_device *dev = fb->dev;
79e53945 12660 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12661
ef2d633e 12662 drm_framebuffer_cleanup(fb);
60a5ca01 12663 mutex_lock(&dev->struct_mutex);
ef2d633e 12664 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12665 drm_gem_object_unreference(&intel_fb->obj->base);
12666 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12667 kfree(intel_fb);
12668}
12669
12670static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12671 struct drm_file *file,
79e53945
JB
12672 unsigned int *handle)
12673{
12674 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12675 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12676
05394f39 12677 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12678}
12679
12680static const struct drm_framebuffer_funcs intel_fb_funcs = {
12681 .destroy = intel_user_framebuffer_destroy,
12682 .create_handle = intel_user_framebuffer_create_handle,
12683};
12684
b5ea642a
DV
12685static int intel_framebuffer_init(struct drm_device *dev,
12686 struct intel_framebuffer *intel_fb,
12687 struct drm_mode_fb_cmd2 *mode_cmd,
12688 struct drm_i915_gem_object *obj)
79e53945 12689{
a57ce0b2 12690 int aligned_height;
a35cdaa0 12691 int pitch_limit;
79e53945
JB
12692 int ret;
12693
dd4916c5
DV
12694 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12695
c16ed4be
CW
12696 if (obj->tiling_mode == I915_TILING_Y) {
12697 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12698 return -EINVAL;
c16ed4be 12699 }
57cd6508 12700
c16ed4be
CW
12701 if (mode_cmd->pitches[0] & 63) {
12702 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12703 mode_cmd->pitches[0]);
57cd6508 12704 return -EINVAL;
c16ed4be 12705 }
57cd6508 12706
a35cdaa0
CW
12707 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12708 pitch_limit = 32*1024;
12709 } else if (INTEL_INFO(dev)->gen >= 4) {
12710 if (obj->tiling_mode)
12711 pitch_limit = 16*1024;
12712 else
12713 pitch_limit = 32*1024;
12714 } else if (INTEL_INFO(dev)->gen >= 3) {
12715 if (obj->tiling_mode)
12716 pitch_limit = 8*1024;
12717 else
12718 pitch_limit = 16*1024;
12719 } else
12720 /* XXX DSPC is limited to 4k tiled */
12721 pitch_limit = 8*1024;
12722
12723 if (mode_cmd->pitches[0] > pitch_limit) {
12724 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12725 obj->tiling_mode ? "tiled" : "linear",
12726 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12727 return -EINVAL;
c16ed4be 12728 }
5d7bd705
VS
12729
12730 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12731 mode_cmd->pitches[0] != obj->stride) {
12732 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12733 mode_cmd->pitches[0], obj->stride);
5d7bd705 12734 return -EINVAL;
c16ed4be 12735 }
5d7bd705 12736
57779d06 12737 /* Reject formats not supported by any plane early. */
308e5bcb 12738 switch (mode_cmd->pixel_format) {
57779d06 12739 case DRM_FORMAT_C8:
04b3924d
VS
12740 case DRM_FORMAT_RGB565:
12741 case DRM_FORMAT_XRGB8888:
12742 case DRM_FORMAT_ARGB8888:
57779d06
VS
12743 break;
12744 case DRM_FORMAT_XRGB1555:
12745 case DRM_FORMAT_ARGB1555:
c16ed4be 12746 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12747 DRM_DEBUG("unsupported pixel format: %s\n",
12748 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12749 return -EINVAL;
c16ed4be 12750 }
57779d06
VS
12751 break;
12752 case DRM_FORMAT_XBGR8888:
12753 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12754 case DRM_FORMAT_XRGB2101010:
12755 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12756 case DRM_FORMAT_XBGR2101010:
12757 case DRM_FORMAT_ABGR2101010:
c16ed4be 12758 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12759 DRM_DEBUG("unsupported pixel format: %s\n",
12760 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12761 return -EINVAL;
c16ed4be 12762 }
b5626747 12763 break;
04b3924d
VS
12764 case DRM_FORMAT_YUYV:
12765 case DRM_FORMAT_UYVY:
12766 case DRM_FORMAT_YVYU:
12767 case DRM_FORMAT_VYUY:
c16ed4be 12768 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12769 DRM_DEBUG("unsupported pixel format: %s\n",
12770 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12771 return -EINVAL;
c16ed4be 12772 }
57cd6508
CW
12773 break;
12774 default:
4ee62c76
VS
12775 DRM_DEBUG("unsupported pixel format: %s\n",
12776 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12777 return -EINVAL;
12778 }
12779
90f9a336
VS
12780 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12781 if (mode_cmd->offsets[0] != 0)
12782 return -EINVAL;
12783
ec2c981e
DL
12784 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
12785 obj->tiling_mode);
53155c0a
DV
12786 /* FIXME drm helper for size checks (especially planar formats)? */
12787 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12788 return -EINVAL;
12789
c7d73f6a
DV
12790 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12791 intel_fb->obj = obj;
80075d49 12792 intel_fb->obj->framebuffer_references++;
c7d73f6a 12793
79e53945
JB
12794 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12795 if (ret) {
12796 DRM_ERROR("framebuffer init failed %d\n", ret);
12797 return ret;
12798 }
12799
79e53945
JB
12800 return 0;
12801}
12802
79e53945
JB
12803static struct drm_framebuffer *
12804intel_user_framebuffer_create(struct drm_device *dev,
12805 struct drm_file *filp,
308e5bcb 12806 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12807{
05394f39 12808 struct drm_i915_gem_object *obj;
79e53945 12809
308e5bcb
JB
12810 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12811 mode_cmd->handles[0]));
c8725226 12812 if (&obj->base == NULL)
cce13ff7 12813 return ERR_PTR(-ENOENT);
79e53945 12814
d2dff872 12815 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12816}
12817
4520f53a 12818#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12819static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12820{
12821}
12822#endif
12823
79e53945 12824static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12825 .fb_create = intel_user_framebuffer_create,
0632fef6 12826 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
12827 .atomic_check = intel_atomic_check,
12828 .atomic_commit = intel_atomic_commit,
79e53945
JB
12829};
12830
e70236a8
JB
12831/* Set up chip specific display functions */
12832static void intel_init_display(struct drm_device *dev)
12833{
12834 struct drm_i915_private *dev_priv = dev->dev_private;
12835
ee9300bb
DV
12836 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12837 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12838 else if (IS_CHERRYVIEW(dev))
12839 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12840 else if (IS_VALLEYVIEW(dev))
12841 dev_priv->display.find_dpll = vlv_find_best_dpll;
12842 else if (IS_PINEVIEW(dev))
12843 dev_priv->display.find_dpll = pnv_find_best_dpll;
12844 else
12845 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12846
bc8d7dff
DL
12847 if (INTEL_INFO(dev)->gen >= 9) {
12848 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
12849 dev_priv->display.get_initial_plane_config =
12850 skylake_get_initial_plane_config;
bc8d7dff
DL
12851 dev_priv->display.crtc_compute_clock =
12852 haswell_crtc_compute_clock;
12853 dev_priv->display.crtc_enable = haswell_crtc_enable;
12854 dev_priv->display.crtc_disable = haswell_crtc_disable;
12855 dev_priv->display.off = ironlake_crtc_off;
12856 dev_priv->display.update_primary_plane =
12857 skylake_update_primary_plane;
12858 } else if (HAS_DDI(dev)) {
0e8ffe1b 12859 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
12860 dev_priv->display.get_initial_plane_config =
12861 ironlake_get_initial_plane_config;
797d0259
ACO
12862 dev_priv->display.crtc_compute_clock =
12863 haswell_crtc_compute_clock;
4f771f10
PZ
12864 dev_priv->display.crtc_enable = haswell_crtc_enable;
12865 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12866 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
12867 dev_priv->display.update_primary_plane =
12868 ironlake_update_primary_plane;
09b4ddf9 12869 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12870 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
12871 dev_priv->display.get_initial_plane_config =
12872 ironlake_get_initial_plane_config;
3fb37703
ACO
12873 dev_priv->display.crtc_compute_clock =
12874 ironlake_crtc_compute_clock;
76e5a89c
DV
12875 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12876 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12877 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12878 dev_priv->display.update_primary_plane =
12879 ironlake_update_primary_plane;
89b667f8
JB
12880 } else if (IS_VALLEYVIEW(dev)) {
12881 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
12882 dev_priv->display.get_initial_plane_config =
12883 i9xx_get_initial_plane_config;
d6dfee7a 12884 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
12885 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12886 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12887 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12888 dev_priv->display.update_primary_plane =
12889 i9xx_update_primary_plane;
f564048e 12890 } else {
0e8ffe1b 12891 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
12892 dev_priv->display.get_initial_plane_config =
12893 i9xx_get_initial_plane_config;
d6dfee7a 12894 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
12895 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12896 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12897 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12898 dev_priv->display.update_primary_plane =
12899 i9xx_update_primary_plane;
f564048e 12900 }
e70236a8 12901
e70236a8 12902 /* Returns the core display clock speed */
25eb05fc
JB
12903 if (IS_VALLEYVIEW(dev))
12904 dev_priv->display.get_display_clock_speed =
12905 valleyview_get_display_clock_speed;
12906 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12907 dev_priv->display.get_display_clock_speed =
12908 i945_get_display_clock_speed;
12909 else if (IS_I915G(dev))
12910 dev_priv->display.get_display_clock_speed =
12911 i915_get_display_clock_speed;
257a7ffc 12912 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12913 dev_priv->display.get_display_clock_speed =
12914 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12915 else if (IS_PINEVIEW(dev))
12916 dev_priv->display.get_display_clock_speed =
12917 pnv_get_display_clock_speed;
e70236a8
JB
12918 else if (IS_I915GM(dev))
12919 dev_priv->display.get_display_clock_speed =
12920 i915gm_get_display_clock_speed;
12921 else if (IS_I865G(dev))
12922 dev_priv->display.get_display_clock_speed =
12923 i865_get_display_clock_speed;
f0f8a9ce 12924 else if (IS_I85X(dev))
e70236a8
JB
12925 dev_priv->display.get_display_clock_speed =
12926 i855_get_display_clock_speed;
12927 else /* 852, 830 */
12928 dev_priv->display.get_display_clock_speed =
12929 i830_get_display_clock_speed;
12930
7c10a2b5 12931 if (IS_GEN5(dev)) {
3bb11b53 12932 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
12933 } else if (IS_GEN6(dev)) {
12934 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
12935 } else if (IS_IVYBRIDGE(dev)) {
12936 /* FIXME: detect B0+ stepping and use auto training */
12937 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
3bb11b53
SJ
12938 dev_priv->display.modeset_global_resources =
12939 ivb_modeset_global_resources;
059b2fe9 12940 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 12941 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
12942 } else if (IS_VALLEYVIEW(dev)) {
12943 dev_priv->display.modeset_global_resources =
12944 valleyview_modeset_global_resources;
e70236a8 12945 }
8c9f3aaf
JB
12946
12947 /* Default just returns -ENODEV to indicate unsupported */
12948 dev_priv->display.queue_flip = intel_default_queue_flip;
12949
12950 switch (INTEL_INFO(dev)->gen) {
12951 case 2:
12952 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12953 break;
12954
12955 case 3:
12956 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12957 break;
12958
12959 case 4:
12960 case 5:
12961 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12962 break;
12963
12964 case 6:
12965 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12966 break;
7c9017e5 12967 case 7:
4e0bbc31 12968 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12969 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12970 break;
830c81db
DL
12971 case 9:
12972 dev_priv->display.queue_flip = intel_gen9_queue_flip;
12973 break;
8c9f3aaf 12974 }
7bd688cd
JN
12975
12976 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12977
12978 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12979}
12980
b690e96c
JB
12981/*
12982 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12983 * resume, or other times. This quirk makes sure that's the case for
12984 * affected systems.
12985 */
0206e353 12986static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12987{
12988 struct drm_i915_private *dev_priv = dev->dev_private;
12989
12990 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12991 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12992}
12993
b6b5d049
VS
12994static void quirk_pipeb_force(struct drm_device *dev)
12995{
12996 struct drm_i915_private *dev_priv = dev->dev_private;
12997
12998 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12999 DRM_INFO("applying pipe b force quirk\n");
13000}
13001
435793df
KP
13002/*
13003 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13004 */
13005static void quirk_ssc_force_disable(struct drm_device *dev)
13006{
13007 struct drm_i915_private *dev_priv = dev->dev_private;
13008 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 13009 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
13010}
13011
4dca20ef 13012/*
5a15ab5b
CE
13013 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13014 * brightness value
4dca20ef
CE
13015 */
13016static void quirk_invert_brightness(struct drm_device *dev)
13017{
13018 struct drm_i915_private *dev_priv = dev->dev_private;
13019 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 13020 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
13021}
13022
9c72cc6f
SD
13023/* Some VBT's incorrectly indicate no backlight is present */
13024static void quirk_backlight_present(struct drm_device *dev)
13025{
13026 struct drm_i915_private *dev_priv = dev->dev_private;
13027 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13028 DRM_INFO("applying backlight present quirk\n");
13029}
13030
b690e96c
JB
13031struct intel_quirk {
13032 int device;
13033 int subsystem_vendor;
13034 int subsystem_device;
13035 void (*hook)(struct drm_device *dev);
13036};
13037
5f85f176
EE
13038/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13039struct intel_dmi_quirk {
13040 void (*hook)(struct drm_device *dev);
13041 const struct dmi_system_id (*dmi_id_list)[];
13042};
13043
13044static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13045{
13046 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13047 return 1;
13048}
13049
13050static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13051 {
13052 .dmi_id_list = &(const struct dmi_system_id[]) {
13053 {
13054 .callback = intel_dmi_reverse_brightness,
13055 .ident = "NCR Corporation",
13056 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13057 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13058 },
13059 },
13060 { } /* terminating entry */
13061 },
13062 .hook = quirk_invert_brightness,
13063 },
13064};
13065
c43b5634 13066static struct intel_quirk intel_quirks[] = {
b690e96c 13067 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 13068 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 13069
b690e96c
JB
13070 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13071 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13072
b690e96c
JB
13073 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13074 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13075
5f080c0f
VS
13076 /* 830 needs to leave pipe A & dpll A up */
13077 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13078
b6b5d049
VS
13079 /* 830 needs to leave pipe B & dpll B up */
13080 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13081
435793df
KP
13082 /* Lenovo U160 cannot use SSC on LVDS */
13083 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
13084
13085 /* Sony Vaio Y cannot use SSC on LVDS */
13086 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 13087
be505f64
AH
13088 /* Acer Aspire 5734Z must invert backlight brightness */
13089 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13090
13091 /* Acer/eMachines G725 */
13092 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13093
13094 /* Acer/eMachines e725 */
13095 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13096
13097 /* Acer/Packard Bell NCL20 */
13098 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13099
13100 /* Acer Aspire 4736Z */
13101 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
13102
13103 /* Acer Aspire 5336 */
13104 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
13105
13106 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13107 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 13108
dfb3d47b
SD
13109 /* Acer C720 Chromebook (Core i3 4005U) */
13110 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13111
b2a9601c 13112 /* Apple Macbook 2,1 (Core 2 T7400) */
13113 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13114
d4967d8c
SD
13115 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13116 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
13117
13118 /* HP Chromebook 14 (Celeron 2955U) */
13119 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
13120};
13121
13122static void intel_init_quirks(struct drm_device *dev)
13123{
13124 struct pci_dev *d = dev->pdev;
13125 int i;
13126
13127 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13128 struct intel_quirk *q = &intel_quirks[i];
13129
13130 if (d->device == q->device &&
13131 (d->subsystem_vendor == q->subsystem_vendor ||
13132 q->subsystem_vendor == PCI_ANY_ID) &&
13133 (d->subsystem_device == q->subsystem_device ||
13134 q->subsystem_device == PCI_ANY_ID))
13135 q->hook(dev);
13136 }
5f85f176
EE
13137 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13138 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13139 intel_dmi_quirks[i].hook(dev);
13140 }
b690e96c
JB
13141}
13142
9cce37f4
JB
13143/* Disable the VGA plane that we never use */
13144static void i915_disable_vga(struct drm_device *dev)
13145{
13146 struct drm_i915_private *dev_priv = dev->dev_private;
13147 u8 sr1;
766aa1c4 13148 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 13149
2b37c616 13150 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 13151 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 13152 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
13153 sr1 = inb(VGA_SR_DATA);
13154 outb(sr1 | 1<<5, VGA_SR_DATA);
13155 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13156 udelay(300);
13157
01f5a626 13158 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
13159 POSTING_READ(vga_reg);
13160}
13161
f817586c
DV
13162void intel_modeset_init_hw(struct drm_device *dev)
13163{
a8f78b58
ED
13164 intel_prepare_ddi(dev);
13165
f8bf63fd
VS
13166 if (IS_VALLEYVIEW(dev))
13167 vlv_update_cdclk(dev);
13168
f817586c
DV
13169 intel_init_clock_gating(dev);
13170
8090c6b9 13171 intel_enable_gt_powersave(dev);
f817586c
DV
13172}
13173
79e53945
JB
13174void intel_modeset_init(struct drm_device *dev)
13175{
652c393a 13176 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 13177 int sprite, ret;
8cc87b75 13178 enum pipe pipe;
46f297fb 13179 struct intel_crtc *crtc;
79e53945
JB
13180
13181 drm_mode_config_init(dev);
13182
13183 dev->mode_config.min_width = 0;
13184 dev->mode_config.min_height = 0;
13185
019d96cb
DA
13186 dev->mode_config.preferred_depth = 24;
13187 dev->mode_config.prefer_shadow = 1;
13188
e6ecefaa 13189 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 13190
b690e96c
JB
13191 intel_init_quirks(dev);
13192
1fa61106
ED
13193 intel_init_pm(dev);
13194
e3c74757
BW
13195 if (INTEL_INFO(dev)->num_pipes == 0)
13196 return;
13197
e70236a8 13198 intel_init_display(dev);
7c10a2b5 13199 intel_init_audio(dev);
e70236a8 13200
a6c45cf0
CW
13201 if (IS_GEN2(dev)) {
13202 dev->mode_config.max_width = 2048;
13203 dev->mode_config.max_height = 2048;
13204 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
13205 dev->mode_config.max_width = 4096;
13206 dev->mode_config.max_height = 4096;
79e53945 13207 } else {
a6c45cf0
CW
13208 dev->mode_config.max_width = 8192;
13209 dev->mode_config.max_height = 8192;
79e53945 13210 }
068be561 13211
dc41c154
VS
13212 if (IS_845G(dev) || IS_I865G(dev)) {
13213 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13214 dev->mode_config.cursor_height = 1023;
13215 } else if (IS_GEN2(dev)) {
068be561
DL
13216 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13217 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13218 } else {
13219 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13220 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13221 }
13222
5d4545ae 13223 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 13224
28c97730 13225 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
13226 INTEL_INFO(dev)->num_pipes,
13227 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 13228
055e393f 13229 for_each_pipe(dev_priv, pipe) {
8cc87b75 13230 intel_crtc_init(dev, pipe);
1fe47785
DL
13231 for_each_sprite(pipe, sprite) {
13232 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 13233 if (ret)
06da8da2 13234 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 13235 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 13236 }
79e53945
JB
13237 }
13238
f42bb70d
JB
13239 intel_init_dpio(dev);
13240
e72f9fbf 13241 intel_shared_dpll_init(dev);
ee7b9f93 13242
9cce37f4
JB
13243 /* Just disable it once at startup */
13244 i915_disable_vga(dev);
79e53945 13245 intel_setup_outputs(dev);
11be49eb
CW
13246
13247 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 13248 intel_fbc_disable(dev);
fa9fa083 13249
6e9f798d 13250 drm_modeset_lock_all(dev);
fa9fa083 13251 intel_modeset_setup_hw_state(dev, false);
6e9f798d 13252 drm_modeset_unlock_all(dev);
46f297fb 13253
d3fcc808 13254 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
13255 if (!crtc->active)
13256 continue;
13257
46f297fb 13258 /*
46f297fb
JB
13259 * Note that reserving the BIOS fb up front prevents us
13260 * from stuffing other stolen allocations like the ring
13261 * on top. This prevents some ugliness at boot time, and
13262 * can even allow for smooth boot transitions if the BIOS
13263 * fb is large enough for the active pipe configuration.
13264 */
5724dbd1
DL
13265 if (dev_priv->display.get_initial_plane_config) {
13266 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
13267 &crtc->plane_config);
13268 /*
13269 * If the fb is shared between multiple heads, we'll
13270 * just get the first one.
13271 */
484b41dd 13272 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 13273 }
46f297fb 13274 }
2c7111db
CW
13275}
13276
7fad798e
DV
13277static void intel_enable_pipe_a(struct drm_device *dev)
13278{
13279 struct intel_connector *connector;
13280 struct drm_connector *crt = NULL;
13281 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 13282 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
13283
13284 /* We can't just switch on the pipe A, we need to set things up with a
13285 * proper mode and output configuration. As a gross hack, enable pipe A
13286 * by enabling the load detect pipe once. */
13287 list_for_each_entry(connector,
13288 &dev->mode_config.connector_list,
13289 base.head) {
13290 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13291 crt = &connector->base;
13292 break;
13293 }
13294 }
13295
13296 if (!crt)
13297 return;
13298
208bf9fd
VS
13299 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13300 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
13301}
13302
fa555837
DV
13303static bool
13304intel_check_plane_mapping(struct intel_crtc *crtc)
13305{
7eb552ae
BW
13306 struct drm_device *dev = crtc->base.dev;
13307 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
13308 u32 reg, val;
13309
7eb552ae 13310 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13311 return true;
13312
13313 reg = DSPCNTR(!crtc->plane);
13314 val = I915_READ(reg);
13315
13316 if ((val & DISPLAY_PLANE_ENABLE) &&
13317 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13318 return false;
13319
13320 return true;
13321}
13322
24929352
DV
13323static void intel_sanitize_crtc(struct intel_crtc *crtc)
13324{
13325 struct drm_device *dev = crtc->base.dev;
13326 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13327 u32 reg;
24929352 13328
24929352 13329 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 13330 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
13331 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13332
d3eaf884 13333 /* restore vblank interrupts to correct state */
d297e103
VS
13334 if (crtc->active) {
13335 update_scanline_offset(crtc);
d3eaf884 13336 drm_vblank_on(dev, crtc->pipe);
d297e103 13337 } else
d3eaf884
VS
13338 drm_vblank_off(dev, crtc->pipe);
13339
24929352 13340 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13341 * disable the crtc (and hence change the state) if it is wrong. Note
13342 * that gen4+ has a fixed plane -> pipe mapping. */
13343 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13344 struct intel_connector *connector;
13345 bool plane;
13346
24929352
DV
13347 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13348 crtc->base.base.id);
13349
13350 /* Pipe has the wrong plane attached and the plane is active.
13351 * Temporarily change the plane mapping and disable everything
13352 * ... */
13353 plane = crtc->plane;
13354 crtc->plane = !plane;
9c8958bc 13355 crtc->primary_enabled = true;
24929352
DV
13356 dev_priv->display.crtc_disable(&crtc->base);
13357 crtc->plane = plane;
13358
13359 /* ... and break all links. */
13360 list_for_each_entry(connector, &dev->mode_config.connector_list,
13361 base.head) {
13362 if (connector->encoder->base.crtc != &crtc->base)
13363 continue;
13364
7f1950fb
EE
13365 connector->base.dpms = DRM_MODE_DPMS_OFF;
13366 connector->base.encoder = NULL;
24929352 13367 }
7f1950fb
EE
13368 /* multiple connectors may have the same encoder:
13369 * handle them and break crtc link separately */
13370 list_for_each_entry(connector, &dev->mode_config.connector_list,
13371 base.head)
13372 if (connector->encoder->base.crtc == &crtc->base) {
13373 connector->encoder->base.crtc = NULL;
13374 connector->encoder->connectors_active = false;
13375 }
24929352
DV
13376
13377 WARN_ON(crtc->active);
13378 crtc->base.enabled = false;
13379 }
24929352 13380
7fad798e
DV
13381 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13382 crtc->pipe == PIPE_A && !crtc->active) {
13383 /* BIOS forgot to enable pipe A, this mostly happens after
13384 * resume. Force-enable the pipe to fix this, the update_dpms
13385 * call below we restore the pipe to the right state, but leave
13386 * the required bits on. */
13387 intel_enable_pipe_a(dev);
13388 }
13389
24929352
DV
13390 /* Adjust the state of the output pipe according to whether we
13391 * have active connectors/encoders. */
13392 intel_crtc_update_dpms(&crtc->base);
13393
13394 if (crtc->active != crtc->base.enabled) {
13395 struct intel_encoder *encoder;
13396
13397 /* This can happen either due to bugs in the get_hw_state
13398 * functions or because the pipe is force-enabled due to the
13399 * pipe A quirk. */
13400 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13401 crtc->base.base.id,
13402 crtc->base.enabled ? "enabled" : "disabled",
13403 crtc->active ? "enabled" : "disabled");
13404
13405 crtc->base.enabled = crtc->active;
13406
13407 /* Because we only establish the connector -> encoder ->
13408 * crtc links if something is active, this means the
13409 * crtc is now deactivated. Break the links. connector
13410 * -> encoder links are only establish when things are
13411 * actually up, hence no need to break them. */
13412 WARN_ON(crtc->active);
13413
13414 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13415 WARN_ON(encoder->connectors_active);
13416 encoder->base.crtc = NULL;
13417 }
13418 }
c5ab3bc0 13419
a3ed6aad 13420 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13421 /*
13422 * We start out with underrun reporting disabled to avoid races.
13423 * For correct bookkeeping mark this on active crtcs.
13424 *
c5ab3bc0
DV
13425 * Also on gmch platforms we dont have any hardware bits to
13426 * disable the underrun reporting. Which means we need to start
13427 * out with underrun reporting disabled also on inactive pipes,
13428 * since otherwise we'll complain about the garbage we read when
13429 * e.g. coming up after runtime pm.
13430 *
4cc31489
DV
13431 * No protection against concurrent access is required - at
13432 * worst a fifo underrun happens which also sets this to false.
13433 */
13434 crtc->cpu_fifo_underrun_disabled = true;
13435 crtc->pch_fifo_underrun_disabled = true;
13436 }
24929352
DV
13437}
13438
13439static void intel_sanitize_encoder(struct intel_encoder *encoder)
13440{
13441 struct intel_connector *connector;
13442 struct drm_device *dev = encoder->base.dev;
13443
13444 /* We need to check both for a crtc link (meaning that the
13445 * encoder is active and trying to read from a pipe) and the
13446 * pipe itself being active. */
13447 bool has_active_crtc = encoder->base.crtc &&
13448 to_intel_crtc(encoder->base.crtc)->active;
13449
13450 if (encoder->connectors_active && !has_active_crtc) {
13451 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13452 encoder->base.base.id,
8e329a03 13453 encoder->base.name);
24929352
DV
13454
13455 /* Connector is active, but has no active pipe. This is
13456 * fallout from our resume register restoring. Disable
13457 * the encoder manually again. */
13458 if (encoder->base.crtc) {
13459 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13460 encoder->base.base.id,
8e329a03 13461 encoder->base.name);
24929352 13462 encoder->disable(encoder);
a62d1497
VS
13463 if (encoder->post_disable)
13464 encoder->post_disable(encoder);
24929352 13465 }
7f1950fb
EE
13466 encoder->base.crtc = NULL;
13467 encoder->connectors_active = false;
24929352
DV
13468
13469 /* Inconsistent output/port/pipe state happens presumably due to
13470 * a bug in one of the get_hw_state functions. Or someplace else
13471 * in our code, like the register restore mess on resume. Clamp
13472 * things to off as a safer default. */
13473 list_for_each_entry(connector,
13474 &dev->mode_config.connector_list,
13475 base.head) {
13476 if (connector->encoder != encoder)
13477 continue;
7f1950fb
EE
13478 connector->base.dpms = DRM_MODE_DPMS_OFF;
13479 connector->base.encoder = NULL;
24929352
DV
13480 }
13481 }
13482 /* Enabled encoders without active connectors will be fixed in
13483 * the crtc fixup. */
13484}
13485
04098753 13486void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13487{
13488 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13489 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13490
04098753
ID
13491 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13492 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13493 i915_disable_vga(dev);
13494 }
13495}
13496
13497void i915_redisable_vga(struct drm_device *dev)
13498{
13499 struct drm_i915_private *dev_priv = dev->dev_private;
13500
8dc8a27c
PZ
13501 /* This function can be called both from intel_modeset_setup_hw_state or
13502 * at a very early point in our resume sequence, where the power well
13503 * structures are not yet restored. Since this function is at a very
13504 * paranoid "someone might have enabled VGA while we were not looking"
13505 * level, just check if the power well is enabled instead of trying to
13506 * follow the "don't touch the power well if we don't need it" policy
13507 * the rest of the driver uses. */
f458ebbc 13508 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13509 return;
13510
04098753 13511 i915_redisable_vga_power_on(dev);
0fde901f
KM
13512}
13513
98ec7739
VS
13514static bool primary_get_hw_state(struct intel_crtc *crtc)
13515{
13516 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13517
13518 if (!crtc->active)
13519 return false;
13520
13521 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13522}
13523
30e984df 13524static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13525{
13526 struct drm_i915_private *dev_priv = dev->dev_private;
13527 enum pipe pipe;
24929352
DV
13528 struct intel_crtc *crtc;
13529 struct intel_encoder *encoder;
13530 struct intel_connector *connector;
5358901f 13531 int i;
24929352 13532
d3fcc808 13533 for_each_intel_crtc(dev, crtc) {
6e3c9717 13534 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 13535
6e3c9717 13536 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 13537
0e8ffe1b 13538 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 13539 crtc->config);
24929352
DV
13540
13541 crtc->base.enabled = crtc->active;
98ec7739 13542 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13543
13544 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13545 crtc->base.base.id,
13546 crtc->active ? "enabled" : "disabled");
13547 }
13548
5358901f
DV
13549 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13550 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13551
3e369b76
ACO
13552 pll->on = pll->get_hw_state(dev_priv, pll,
13553 &pll->config.hw_state);
5358901f 13554 pll->active = 0;
3e369b76 13555 pll->config.crtc_mask = 0;
d3fcc808 13556 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 13557 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 13558 pll->active++;
3e369b76 13559 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 13560 }
5358901f 13561 }
5358901f 13562
1e6f2ddc 13563 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 13564 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 13565
3e369b76 13566 if (pll->config.crtc_mask)
bd2bb1b9 13567 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13568 }
13569
b2784e15 13570 for_each_intel_encoder(dev, encoder) {
24929352
DV
13571 pipe = 0;
13572
13573 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13574 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13575 encoder->base.crtc = &crtc->base;
6e3c9717 13576 encoder->get_config(encoder, crtc->config);
24929352
DV
13577 } else {
13578 encoder->base.crtc = NULL;
13579 }
13580
13581 encoder->connectors_active = false;
6f2bcceb 13582 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13583 encoder->base.base.id,
8e329a03 13584 encoder->base.name,
24929352 13585 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13586 pipe_name(pipe));
24929352
DV
13587 }
13588
13589 list_for_each_entry(connector, &dev->mode_config.connector_list,
13590 base.head) {
13591 if (connector->get_hw_state(connector)) {
13592 connector->base.dpms = DRM_MODE_DPMS_ON;
13593 connector->encoder->connectors_active = true;
13594 connector->base.encoder = &connector->encoder->base;
13595 } else {
13596 connector->base.dpms = DRM_MODE_DPMS_OFF;
13597 connector->base.encoder = NULL;
13598 }
13599 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13600 connector->base.base.id,
c23cc417 13601 connector->base.name,
24929352
DV
13602 connector->base.encoder ? "enabled" : "disabled");
13603 }
30e984df
DV
13604}
13605
13606/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13607 * and i915 state tracking structures. */
13608void intel_modeset_setup_hw_state(struct drm_device *dev,
13609 bool force_restore)
13610{
13611 struct drm_i915_private *dev_priv = dev->dev_private;
13612 enum pipe pipe;
30e984df
DV
13613 struct intel_crtc *crtc;
13614 struct intel_encoder *encoder;
35c95375 13615 int i;
30e984df
DV
13616
13617 intel_modeset_readout_hw_state(dev);
24929352 13618
babea61d
JB
13619 /*
13620 * Now that we have the config, copy it to each CRTC struct
13621 * Note that this could go away if we move to using crtc_config
13622 * checking everywhere.
13623 */
d3fcc808 13624 for_each_intel_crtc(dev, crtc) {
d330a953 13625 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
13626 intel_mode_from_pipe_config(&crtc->base.mode,
13627 crtc->config);
babea61d
JB
13628 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13629 crtc->base.base.id);
13630 drm_mode_debug_printmodeline(&crtc->base.mode);
13631 }
13632 }
13633
24929352 13634 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13635 for_each_intel_encoder(dev, encoder) {
24929352
DV
13636 intel_sanitize_encoder(encoder);
13637 }
13638
055e393f 13639 for_each_pipe(dev_priv, pipe) {
24929352
DV
13640 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13641 intel_sanitize_crtc(crtc);
6e3c9717
ACO
13642 intel_dump_pipe_config(crtc, crtc->config,
13643 "[setup_hw_state]");
24929352 13644 }
9a935856 13645
35c95375
DV
13646 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13647 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13648
13649 if (!pll->on || pll->active)
13650 continue;
13651
13652 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13653
13654 pll->disable(dev_priv, pll);
13655 pll->on = false;
13656 }
13657
3078999f
PB
13658 if (IS_GEN9(dev))
13659 skl_wm_get_hw_state(dev);
13660 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13661 ilk_wm_get_hw_state(dev);
13662
45e2b5f6 13663 if (force_restore) {
7d0bc1ea
VS
13664 i915_redisable_vga(dev);
13665
f30da187
DV
13666 /*
13667 * We need to use raw interfaces for restoring state to avoid
13668 * checking (bogus) intermediate states.
13669 */
055e393f 13670 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13671 struct drm_crtc *crtc =
13672 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 13673
7f27126e
JB
13674 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13675 crtc->primary->fb);
45e2b5f6
DV
13676 }
13677 } else {
13678 intel_modeset_update_staged_output_state(dev);
13679 }
8af6cf88
DV
13680
13681 intel_modeset_check_state(dev);
2c7111db
CW
13682}
13683
13684void intel_modeset_gem_init(struct drm_device *dev)
13685{
92122789 13686 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 13687 struct drm_crtc *c;
2ff8fde1 13688 struct drm_i915_gem_object *obj;
484b41dd 13689
ae48434c
ID
13690 mutex_lock(&dev->struct_mutex);
13691 intel_init_gt_powersave(dev);
13692 mutex_unlock(&dev->struct_mutex);
13693
92122789
JB
13694 /*
13695 * There may be no VBT; and if the BIOS enabled SSC we can
13696 * just keep using it to avoid unnecessary flicker. Whereas if the
13697 * BIOS isn't using it, don't assume it will work even if the VBT
13698 * indicates as much.
13699 */
13700 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13701 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13702 DREF_SSC1_ENABLE);
13703
1833b134 13704 intel_modeset_init_hw(dev);
02e792fb
DV
13705
13706 intel_setup_overlay(dev);
484b41dd
JB
13707
13708 /*
13709 * Make sure any fbs we allocated at startup are properly
13710 * pinned & fenced. When we do the allocation it's too early
13711 * for this.
13712 */
13713 mutex_lock(&dev->struct_mutex);
70e1e0ec 13714 for_each_crtc(dev, c) {
2ff8fde1
MR
13715 obj = intel_fb_obj(c->primary->fb);
13716 if (obj == NULL)
484b41dd
JB
13717 continue;
13718
850c4cdc
TU
13719 if (intel_pin_and_fence_fb_obj(c->primary,
13720 c->primary->fb,
13721 NULL)) {
484b41dd
JB
13722 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13723 to_intel_crtc(c)->pipe);
66e514c1
DA
13724 drm_framebuffer_unreference(c->primary->fb);
13725 c->primary->fb = NULL;
afd65eb4 13726 update_state_fb(c->primary);
484b41dd
JB
13727 }
13728 }
13729 mutex_unlock(&dev->struct_mutex);
0962c3c9
VS
13730
13731 intel_backlight_register(dev);
79e53945
JB
13732}
13733
4932e2c3
ID
13734void intel_connector_unregister(struct intel_connector *intel_connector)
13735{
13736 struct drm_connector *connector = &intel_connector->base;
13737
13738 intel_panel_destroy_backlight(connector);
34ea3d38 13739 drm_connector_unregister(connector);
4932e2c3
ID
13740}
13741
79e53945
JB
13742void intel_modeset_cleanup(struct drm_device *dev)
13743{
652c393a 13744 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13745 struct drm_connector *connector;
652c393a 13746
2eb5252e
ID
13747 intel_disable_gt_powersave(dev);
13748
0962c3c9
VS
13749 intel_backlight_unregister(dev);
13750
fd0c0642
DV
13751 /*
13752 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 13753 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
13754 * experience fancy races otherwise.
13755 */
2aeb7d3a 13756 intel_irq_uninstall(dev_priv);
eb21b92b 13757
fd0c0642
DV
13758 /*
13759 * Due to the hpd irq storm handling the hotplug work can re-arm the
13760 * poll handlers. Hence disable polling after hpd handling is shut down.
13761 */
f87ea761 13762 drm_kms_helper_poll_fini(dev);
fd0c0642 13763
652c393a
JB
13764 mutex_lock(&dev->struct_mutex);
13765
723bfd70
JB
13766 intel_unregister_dsm_handler();
13767
7ff0ebcc 13768 intel_fbc_disable(dev);
e70236a8 13769
930ebb46
DV
13770 ironlake_teardown_rc6(dev);
13771
69341a5e
KH
13772 mutex_unlock(&dev->struct_mutex);
13773
1630fe75
CW
13774 /* flush any delayed tasks or pending work */
13775 flush_scheduled_work();
13776
db31af1d
JN
13777 /* destroy the backlight and sysfs files before encoders/connectors */
13778 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13779 struct intel_connector *intel_connector;
13780
13781 intel_connector = to_intel_connector(connector);
13782 intel_connector->unregister(intel_connector);
db31af1d 13783 }
d9255d57 13784
79e53945 13785 drm_mode_config_cleanup(dev);
4d7bb011
DV
13786
13787 intel_cleanup_overlay(dev);
ae48434c
ID
13788
13789 mutex_lock(&dev->struct_mutex);
13790 intel_cleanup_gt_powersave(dev);
13791 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13792}
13793
f1c79df3
ZW
13794/*
13795 * Return which encoder is currently attached for connector.
13796 */
df0e9248 13797struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13798{
df0e9248
CW
13799 return &intel_attached_encoder(connector)->base;
13800}
f1c79df3 13801
df0e9248
CW
13802void intel_connector_attach_encoder(struct intel_connector *connector,
13803 struct intel_encoder *encoder)
13804{
13805 connector->encoder = encoder;
13806 drm_mode_connector_attach_encoder(&connector->base,
13807 &encoder->base);
79e53945 13808}
28d52043
DA
13809
13810/*
13811 * set vga decode state - true == enable VGA decode
13812 */
13813int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13814{
13815 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13816 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13817 u16 gmch_ctrl;
13818
75fa041d
CW
13819 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13820 DRM_ERROR("failed to read control word\n");
13821 return -EIO;
13822 }
13823
c0cc8a55
CW
13824 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13825 return 0;
13826
28d52043
DA
13827 if (state)
13828 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13829 else
13830 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13831
13832 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13833 DRM_ERROR("failed to write control word\n");
13834 return -EIO;
13835 }
13836
28d52043
DA
13837 return 0;
13838}
c4a1d9e4 13839
c4a1d9e4 13840struct intel_display_error_state {
ff57f1b0
PZ
13841
13842 u32 power_well_driver;
13843
63b66e5b
CW
13844 int num_transcoders;
13845
c4a1d9e4
CW
13846 struct intel_cursor_error_state {
13847 u32 control;
13848 u32 position;
13849 u32 base;
13850 u32 size;
52331309 13851 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13852
13853 struct intel_pipe_error_state {
ddf9c536 13854 bool power_domain_on;
c4a1d9e4 13855 u32 source;
f301b1e1 13856 u32 stat;
52331309 13857 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13858
13859 struct intel_plane_error_state {
13860 u32 control;
13861 u32 stride;
13862 u32 size;
13863 u32 pos;
13864 u32 addr;
13865 u32 surface;
13866 u32 tile_offset;
52331309 13867 } plane[I915_MAX_PIPES];
63b66e5b
CW
13868
13869 struct intel_transcoder_error_state {
ddf9c536 13870 bool power_domain_on;
63b66e5b
CW
13871 enum transcoder cpu_transcoder;
13872
13873 u32 conf;
13874
13875 u32 htotal;
13876 u32 hblank;
13877 u32 hsync;
13878 u32 vtotal;
13879 u32 vblank;
13880 u32 vsync;
13881 } transcoder[4];
c4a1d9e4
CW
13882};
13883
13884struct intel_display_error_state *
13885intel_display_capture_error_state(struct drm_device *dev)
13886{
fbee40df 13887 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13888 struct intel_display_error_state *error;
63b66e5b
CW
13889 int transcoders[] = {
13890 TRANSCODER_A,
13891 TRANSCODER_B,
13892 TRANSCODER_C,
13893 TRANSCODER_EDP,
13894 };
c4a1d9e4
CW
13895 int i;
13896
63b66e5b
CW
13897 if (INTEL_INFO(dev)->num_pipes == 0)
13898 return NULL;
13899
9d1cb914 13900 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13901 if (error == NULL)
13902 return NULL;
13903
190be112 13904 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13905 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13906
055e393f 13907 for_each_pipe(dev_priv, i) {
ddf9c536 13908 error->pipe[i].power_domain_on =
f458ebbc
DV
13909 __intel_display_power_is_enabled(dev_priv,
13910 POWER_DOMAIN_PIPE(i));
ddf9c536 13911 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13912 continue;
13913
5efb3e28
VS
13914 error->cursor[i].control = I915_READ(CURCNTR(i));
13915 error->cursor[i].position = I915_READ(CURPOS(i));
13916 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13917
13918 error->plane[i].control = I915_READ(DSPCNTR(i));
13919 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13920 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13921 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13922 error->plane[i].pos = I915_READ(DSPPOS(i));
13923 }
ca291363
PZ
13924 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13925 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13926 if (INTEL_INFO(dev)->gen >= 4) {
13927 error->plane[i].surface = I915_READ(DSPSURF(i));
13928 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13929 }
13930
c4a1d9e4 13931 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13932
3abfce77 13933 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13934 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13935 }
13936
13937 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13938 if (HAS_DDI(dev_priv->dev))
13939 error->num_transcoders++; /* Account for eDP. */
13940
13941 for (i = 0; i < error->num_transcoders; i++) {
13942 enum transcoder cpu_transcoder = transcoders[i];
13943
ddf9c536 13944 error->transcoder[i].power_domain_on =
f458ebbc 13945 __intel_display_power_is_enabled(dev_priv,
38cc1daf 13946 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13947 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13948 continue;
13949
63b66e5b
CW
13950 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13951
13952 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13953 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13954 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13955 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13956 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13957 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13958 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13959 }
13960
13961 return error;
13962}
13963
edc3d884
MK
13964#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13965
c4a1d9e4 13966void
edc3d884 13967intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13968 struct drm_device *dev,
13969 struct intel_display_error_state *error)
13970{
055e393f 13971 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13972 int i;
13973
63b66e5b
CW
13974 if (!error)
13975 return;
13976
edc3d884 13977 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13978 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13979 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13980 error->power_well_driver);
055e393f 13981 for_each_pipe(dev_priv, i) {
edc3d884 13982 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13983 err_printf(m, " Power: %s\n",
13984 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13985 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13986 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13987
13988 err_printf(m, "Plane [%d]:\n", i);
13989 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13990 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13991 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13992 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13993 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13994 }
4b71a570 13995 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13996 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13997 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13998 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13999 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
14000 }
14001
edc3d884
MK
14002 err_printf(m, "Cursor [%d]:\n", i);
14003 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14004 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14005 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 14006 }
63b66e5b
CW
14007
14008 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 14009 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 14010 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
14011 err_printf(m, " Power: %s\n",
14012 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
14013 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14014 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14015 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14016 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14017 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14018 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14019 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14020 }
c4a1d9e4 14021}
e2fcdaa9
VS
14022
14023void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14024{
14025 struct intel_crtc *crtc;
14026
14027 for_each_intel_crtc(dev, crtc) {
14028 struct intel_unpin_work *work;
e2fcdaa9 14029
5e2d7afc 14030 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
14031
14032 work = crtc->unpin_work;
14033
14034 if (work && work->event &&
14035 work->event->base.file_priv == file) {
14036 kfree(work->event);
14037 work->event = NULL;
14038 }
14039
5e2d7afc 14040 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
14041 }
14042}
This page took 2.827105 seconds and 5 git commands to generate.