drm/i915: don't keep reassigning FBC_UNSUPPORTED
[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)) {
fb9981aa
DL
2442 struct drm_plane *primary = intel_crtc->base.primary;
2443
2444 primary->fb = &plane_config->fb->base;
2445 primary->state->crtc = &intel_crtc->base;
2446 update_state_fb(primary);
2447
484b41dd 2448 return;
f55548b5 2449 }
484b41dd 2450
2d14030b 2451 kfree(plane_config->fb);
484b41dd
JB
2452
2453 /*
2454 * Failed to alloc the obj, check to see if we should share
2455 * an fb with another CRTC instead
2456 */
70e1e0ec 2457 for_each_crtc(dev, c) {
484b41dd
JB
2458 i = to_intel_crtc(c);
2459
2460 if (c == &intel_crtc->base)
2461 continue;
2462
2ff8fde1
MR
2463 if (!i->active)
2464 continue;
2465
2466 obj = intel_fb_obj(c->primary->fb);
2467 if (obj == NULL)
484b41dd
JB
2468 continue;
2469
2ff8fde1 2470 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
fb9981aa
DL
2471 struct drm_plane *primary = intel_crtc->base.primary;
2472
d9ceb816
JB
2473 if (obj->tiling_mode != I915_TILING_NONE)
2474 dev_priv->preserve_bios_swizzle = true;
2475
66e514c1 2476 drm_framebuffer_reference(c->primary->fb);
fb9981aa
DL
2477 primary->fb = c->primary->fb;
2478 primary->state->crtc = &intel_crtc->base;
5ba76c41 2479 update_state_fb(intel_crtc->base.primary);
2ff8fde1 2480 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2481 break;
2482 }
2483 }
afd65eb4 2484
46f297fb
JB
2485}
2486
29b9bde6
DV
2487static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2488 struct drm_framebuffer *fb,
2489 int x, int y)
81255565
JB
2490{
2491 struct drm_device *dev = crtc->dev;
2492 struct drm_i915_private *dev_priv = dev->dev_private;
2493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2494 struct drm_i915_gem_object *obj;
81255565 2495 int plane = intel_crtc->plane;
e506a0c6 2496 unsigned long linear_offset;
81255565 2497 u32 dspcntr;
f45651ba 2498 u32 reg = DSPCNTR(plane);
48404c1e 2499 int pixel_size;
f45651ba 2500
fdd508a6
VS
2501 if (!intel_crtc->primary_enabled) {
2502 I915_WRITE(reg, 0);
2503 if (INTEL_INFO(dev)->gen >= 4)
2504 I915_WRITE(DSPSURF(plane), 0);
2505 else
2506 I915_WRITE(DSPADDR(plane), 0);
2507 POSTING_READ(reg);
2508 return;
2509 }
2510
c9ba6fad
VS
2511 obj = intel_fb_obj(fb);
2512 if (WARN_ON(obj == NULL))
2513 return;
2514
2515 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2516
f45651ba
VS
2517 dspcntr = DISPPLANE_GAMMA_ENABLE;
2518
fdd508a6 2519 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2520
2521 if (INTEL_INFO(dev)->gen < 4) {
2522 if (intel_crtc->pipe == PIPE_B)
2523 dspcntr |= DISPPLANE_SEL_PIPE_B;
2524
2525 /* pipesrc and dspsize control the size that is scaled from,
2526 * which should always be the user's requested size.
2527 */
2528 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2529 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2530 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2531 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2532 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2533 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2534 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2535 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2536 I915_WRITE(PRIMPOS(plane), 0);
2537 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2538 }
81255565 2539
57779d06
VS
2540 switch (fb->pixel_format) {
2541 case DRM_FORMAT_C8:
81255565
JB
2542 dspcntr |= DISPPLANE_8BPP;
2543 break;
57779d06
VS
2544 case DRM_FORMAT_XRGB1555:
2545 case DRM_FORMAT_ARGB1555:
2546 dspcntr |= DISPPLANE_BGRX555;
81255565 2547 break;
57779d06
VS
2548 case DRM_FORMAT_RGB565:
2549 dspcntr |= DISPPLANE_BGRX565;
2550 break;
2551 case DRM_FORMAT_XRGB8888:
2552 case DRM_FORMAT_ARGB8888:
2553 dspcntr |= DISPPLANE_BGRX888;
2554 break;
2555 case DRM_FORMAT_XBGR8888:
2556 case DRM_FORMAT_ABGR8888:
2557 dspcntr |= DISPPLANE_RGBX888;
2558 break;
2559 case DRM_FORMAT_XRGB2101010:
2560 case DRM_FORMAT_ARGB2101010:
2561 dspcntr |= DISPPLANE_BGRX101010;
2562 break;
2563 case DRM_FORMAT_XBGR2101010:
2564 case DRM_FORMAT_ABGR2101010:
2565 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2566 break;
2567 default:
baba133a 2568 BUG();
81255565 2569 }
57779d06 2570
f45651ba
VS
2571 if (INTEL_INFO(dev)->gen >= 4 &&
2572 obj->tiling_mode != I915_TILING_NONE)
2573 dspcntr |= DISPPLANE_TILED;
81255565 2574
de1aa629
VS
2575 if (IS_G4X(dev))
2576 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2577
b9897127 2578 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2579
c2c75131
DV
2580 if (INTEL_INFO(dev)->gen >= 4) {
2581 intel_crtc->dspaddr_offset =
bc752862 2582 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2583 pixel_size,
bc752862 2584 fb->pitches[0]);
c2c75131
DV
2585 linear_offset -= intel_crtc->dspaddr_offset;
2586 } else {
e506a0c6 2587 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2588 }
e506a0c6 2589
8e7d688b 2590 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2591 dspcntr |= DISPPLANE_ROTATE_180;
2592
6e3c9717
ACO
2593 x += (intel_crtc->config->pipe_src_w - 1);
2594 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2595
2596 /* Finding the last pixel of the last line of the display
2597 data and adding to linear_offset*/
2598 linear_offset +=
6e3c9717
ACO
2599 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2600 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2601 }
2602
2603 I915_WRITE(reg, dspcntr);
2604
f343c5f6
BW
2605 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2606 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2607 fb->pitches[0]);
01f2c773 2608 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2609 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2610 I915_WRITE(DSPSURF(plane),
2611 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2612 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2613 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2614 } else
f343c5f6 2615 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2616 POSTING_READ(reg);
17638cd6
JB
2617}
2618
29b9bde6
DV
2619static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2620 struct drm_framebuffer *fb,
2621 int x, int y)
17638cd6
JB
2622{
2623 struct drm_device *dev = crtc->dev;
2624 struct drm_i915_private *dev_priv = dev->dev_private;
2625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2626 struct drm_i915_gem_object *obj;
17638cd6 2627 int plane = intel_crtc->plane;
e506a0c6 2628 unsigned long linear_offset;
17638cd6 2629 u32 dspcntr;
f45651ba 2630 u32 reg = DSPCNTR(plane);
48404c1e 2631 int pixel_size;
f45651ba 2632
fdd508a6
VS
2633 if (!intel_crtc->primary_enabled) {
2634 I915_WRITE(reg, 0);
2635 I915_WRITE(DSPSURF(plane), 0);
2636 POSTING_READ(reg);
2637 return;
2638 }
2639
c9ba6fad
VS
2640 obj = intel_fb_obj(fb);
2641 if (WARN_ON(obj == NULL))
2642 return;
2643
2644 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2645
f45651ba
VS
2646 dspcntr = DISPPLANE_GAMMA_ENABLE;
2647
fdd508a6 2648 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2649
2650 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2651 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2652
57779d06
VS
2653 switch (fb->pixel_format) {
2654 case DRM_FORMAT_C8:
17638cd6
JB
2655 dspcntr |= DISPPLANE_8BPP;
2656 break;
57779d06
VS
2657 case DRM_FORMAT_RGB565:
2658 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2659 break;
57779d06
VS
2660 case DRM_FORMAT_XRGB8888:
2661 case DRM_FORMAT_ARGB8888:
2662 dspcntr |= DISPPLANE_BGRX888;
2663 break;
2664 case DRM_FORMAT_XBGR8888:
2665 case DRM_FORMAT_ABGR8888:
2666 dspcntr |= DISPPLANE_RGBX888;
2667 break;
2668 case DRM_FORMAT_XRGB2101010:
2669 case DRM_FORMAT_ARGB2101010:
2670 dspcntr |= DISPPLANE_BGRX101010;
2671 break;
2672 case DRM_FORMAT_XBGR2101010:
2673 case DRM_FORMAT_ABGR2101010:
2674 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2675 break;
2676 default:
baba133a 2677 BUG();
17638cd6
JB
2678 }
2679
2680 if (obj->tiling_mode != I915_TILING_NONE)
2681 dspcntr |= DISPPLANE_TILED;
17638cd6 2682
f45651ba 2683 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2684 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2685
b9897127 2686 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2687 intel_crtc->dspaddr_offset =
bc752862 2688 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2689 pixel_size,
bc752862 2690 fb->pitches[0]);
c2c75131 2691 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2692 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2693 dspcntr |= DISPPLANE_ROTATE_180;
2694
2695 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2696 x += (intel_crtc->config->pipe_src_w - 1);
2697 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2698
2699 /* Finding the last pixel of the last line of the display
2700 data and adding to linear_offset*/
2701 linear_offset +=
6e3c9717
ACO
2702 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2703 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2704 }
2705 }
2706
2707 I915_WRITE(reg, dspcntr);
17638cd6 2708
f343c5f6
BW
2709 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2710 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2711 fb->pitches[0]);
01f2c773 2712 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2713 I915_WRITE(DSPSURF(plane),
2714 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2715 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2716 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2717 } else {
2718 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2719 I915_WRITE(DSPLINOFF(plane), linear_offset);
2720 }
17638cd6 2721 POSTING_READ(reg);
17638cd6
JB
2722}
2723
70d21f0e
DL
2724static void skylake_update_primary_plane(struct drm_crtc *crtc,
2725 struct drm_framebuffer *fb,
2726 int x, int y)
2727{
2728 struct drm_device *dev = crtc->dev;
2729 struct drm_i915_private *dev_priv = dev->dev_private;
2730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2731 struct intel_framebuffer *intel_fb;
2732 struct drm_i915_gem_object *obj;
2733 int pipe = intel_crtc->pipe;
2734 u32 plane_ctl, stride;
2735
2736 if (!intel_crtc->primary_enabled) {
2737 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2738 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2739 POSTING_READ(PLANE_CTL(pipe, 0));
2740 return;
2741 }
2742
2743 plane_ctl = PLANE_CTL_ENABLE |
2744 PLANE_CTL_PIPE_GAMMA_ENABLE |
2745 PLANE_CTL_PIPE_CSC_ENABLE;
2746
2747 switch (fb->pixel_format) {
2748 case DRM_FORMAT_RGB565:
2749 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2750 break;
2751 case DRM_FORMAT_XRGB8888:
2752 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2753 break;
2754 case DRM_FORMAT_XBGR8888:
2755 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2756 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2757 break;
2758 case DRM_FORMAT_XRGB2101010:
2759 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2760 break;
2761 case DRM_FORMAT_XBGR2101010:
2762 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2763 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2764 break;
2765 default:
2766 BUG();
2767 }
2768
2769 intel_fb = to_intel_framebuffer(fb);
2770 obj = intel_fb->obj;
2771
2772 /*
2773 * The stride is either expressed as a multiple of 64 bytes chunks for
2774 * linear buffers or in number of tiles for tiled buffers.
2775 */
2776 switch (obj->tiling_mode) {
2777 case I915_TILING_NONE:
2778 stride = fb->pitches[0] >> 6;
2779 break;
2780 case I915_TILING_X:
2781 plane_ctl |= PLANE_CTL_TILED_X;
2782 stride = fb->pitches[0] >> 9;
2783 break;
2784 default:
2785 BUG();
2786 }
2787
2788 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
8e7d688b 2789 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
1447dde0 2790 plane_ctl |= PLANE_CTL_ROTATE_180;
70d21f0e
DL
2791
2792 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2793
2794 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2795 i915_gem_obj_ggtt_offset(obj),
2796 x, y, fb->width, fb->height,
2797 fb->pitches[0]);
2798
2799 I915_WRITE(PLANE_POS(pipe, 0), 0);
2800 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2801 I915_WRITE(PLANE_SIZE(pipe, 0),
6e3c9717
ACO
2802 (intel_crtc->config->pipe_src_h - 1) << 16 |
2803 (intel_crtc->config->pipe_src_w - 1));
70d21f0e
DL
2804 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2805 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2806
2807 POSTING_READ(PLANE_SURF(pipe, 0));
2808}
2809
17638cd6
JB
2810/* Assume fb object is pinned & idle & fenced and just update base pointers */
2811static int
2812intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2813 int x, int y, enum mode_set_atomic state)
2814{
2815 struct drm_device *dev = crtc->dev;
2816 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2817
6b8e6ed0
CW
2818 if (dev_priv->display.disable_fbc)
2819 dev_priv->display.disable_fbc(dev);
81255565 2820
29b9bde6
DV
2821 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2822
2823 return 0;
81255565
JB
2824}
2825
7514747d 2826static void intel_complete_page_flips(struct drm_device *dev)
96a02917 2827{
96a02917
VS
2828 struct drm_crtc *crtc;
2829
70e1e0ec 2830 for_each_crtc(dev, crtc) {
96a02917
VS
2831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2832 enum plane plane = intel_crtc->plane;
2833
2834 intel_prepare_page_flip(dev, plane);
2835 intel_finish_page_flip_plane(dev, plane);
2836 }
7514747d
VS
2837}
2838
2839static void intel_update_primary_planes(struct drm_device *dev)
2840{
2841 struct drm_i915_private *dev_priv = dev->dev_private;
2842 struct drm_crtc *crtc;
96a02917 2843
70e1e0ec 2844 for_each_crtc(dev, crtc) {
96a02917
VS
2845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2846
51fd371b 2847 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2848 /*
2849 * FIXME: Once we have proper support for primary planes (and
2850 * disabling them without disabling the entire crtc) allow again
66e514c1 2851 * a NULL crtc->primary->fb.
947fdaad 2852 */
f4510a27 2853 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2854 dev_priv->display.update_primary_plane(crtc,
66e514c1 2855 crtc->primary->fb,
262ca2b0
MR
2856 crtc->x,
2857 crtc->y);
51fd371b 2858 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2859 }
2860}
2861
7514747d
VS
2862void intel_prepare_reset(struct drm_device *dev)
2863{
f98ce92f
VS
2864 struct drm_i915_private *dev_priv = to_i915(dev);
2865 struct intel_crtc *crtc;
2866
7514747d
VS
2867 /* no reset support for gen2 */
2868 if (IS_GEN2(dev))
2869 return;
2870
2871 /* reset doesn't touch the display */
2872 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
2873 return;
2874
2875 drm_modeset_lock_all(dev);
f98ce92f
VS
2876
2877 /*
2878 * Disabling the crtcs gracefully seems nicer. Also the
2879 * g33 docs say we should at least disable all the planes.
2880 */
2881 for_each_intel_crtc(dev, crtc) {
2882 if (crtc->active)
2883 dev_priv->display.crtc_disable(&crtc->base);
2884 }
7514747d
VS
2885}
2886
2887void intel_finish_reset(struct drm_device *dev)
2888{
2889 struct drm_i915_private *dev_priv = to_i915(dev);
2890
2891 /*
2892 * Flips in the rings will be nuked by the reset,
2893 * so complete all pending flips so that user space
2894 * will get its events and not get stuck.
2895 */
2896 intel_complete_page_flips(dev);
2897
2898 /* no reset support for gen2 */
2899 if (IS_GEN2(dev))
2900 return;
2901
2902 /* reset doesn't touch the display */
2903 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
2904 /*
2905 * Flips in the rings have been nuked by the reset,
2906 * so update the base address of all primary
2907 * planes to the the last fb to make sure we're
2908 * showing the correct fb after a reset.
2909 */
2910 intel_update_primary_planes(dev);
2911 return;
2912 }
2913
2914 /*
2915 * The display has been reset as well,
2916 * so need a full re-initialization.
2917 */
2918 intel_runtime_pm_disable_interrupts(dev_priv);
2919 intel_runtime_pm_enable_interrupts(dev_priv);
2920
2921 intel_modeset_init_hw(dev);
2922
2923 spin_lock_irq(&dev_priv->irq_lock);
2924 if (dev_priv->display.hpd_irq_setup)
2925 dev_priv->display.hpd_irq_setup(dev);
2926 spin_unlock_irq(&dev_priv->irq_lock);
2927
2928 intel_modeset_setup_hw_state(dev, true);
2929
2930 intel_hpd_init(dev_priv);
2931
2932 drm_modeset_unlock_all(dev);
2933}
2934
14667a4b
CW
2935static int
2936intel_finish_fb(struct drm_framebuffer *old_fb)
2937{
2ff8fde1 2938 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2939 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2940 bool was_interruptible = dev_priv->mm.interruptible;
2941 int ret;
2942
14667a4b
CW
2943 /* Big Hammer, we also need to ensure that any pending
2944 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2945 * current scanout is retired before unpinning the old
2946 * framebuffer.
2947 *
2948 * This should only fail upon a hung GPU, in which case we
2949 * can safely continue.
2950 */
2951 dev_priv->mm.interruptible = false;
2952 ret = i915_gem_object_finish_gpu(obj);
2953 dev_priv->mm.interruptible = was_interruptible;
2954
2955 return ret;
2956}
2957
7d5e3799
CW
2958static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2959{
2960 struct drm_device *dev = crtc->dev;
2961 struct drm_i915_private *dev_priv = dev->dev_private;
2962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
2963 bool pending;
2964
2965 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2966 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2967 return false;
2968
5e2d7afc 2969 spin_lock_irq(&dev->event_lock);
7d5e3799 2970 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 2971 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
2972
2973 return pending;
2974}
2975
e30e8f75
GP
2976static void intel_update_pipe_size(struct intel_crtc *crtc)
2977{
2978 struct drm_device *dev = crtc->base.dev;
2979 struct drm_i915_private *dev_priv = dev->dev_private;
2980 const struct drm_display_mode *adjusted_mode;
2981
2982 if (!i915.fastboot)
2983 return;
2984
2985 /*
2986 * Update pipe size and adjust fitter if needed: the reason for this is
2987 * that in compute_mode_changes we check the native mode (not the pfit
2988 * mode) to see if we can flip rather than do a full mode set. In the
2989 * fastboot case, we'll flip, but if we don't update the pipesrc and
2990 * pfit state, we'll end up with a big fb scanned out into the wrong
2991 * sized surface.
2992 *
2993 * To fix this properly, we need to hoist the checks up into
2994 * compute_mode_changes (or above), check the actual pfit state and
2995 * whether the platform allows pfit disable with pipe active, and only
2996 * then update the pipesrc and pfit state, even on the flip path.
2997 */
2998
6e3c9717 2999 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3000
3001 I915_WRITE(PIPESRC(crtc->pipe),
3002 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3003 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3004 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3005 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3006 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3007 I915_WRITE(PF_CTL(crtc->pipe), 0);
3008 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3009 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3010 }
6e3c9717
ACO
3011 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3012 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3013}
3014
5e84e1a4
ZW
3015static void intel_fdi_normal_train(struct drm_crtc *crtc)
3016{
3017 struct drm_device *dev = crtc->dev;
3018 struct drm_i915_private *dev_priv = dev->dev_private;
3019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3020 int pipe = intel_crtc->pipe;
3021 u32 reg, temp;
3022
3023 /* enable normal train */
3024 reg = FDI_TX_CTL(pipe);
3025 temp = I915_READ(reg);
61e499bf 3026 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3027 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3028 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3029 } else {
3030 temp &= ~FDI_LINK_TRAIN_NONE;
3031 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3032 }
5e84e1a4
ZW
3033 I915_WRITE(reg, temp);
3034
3035 reg = FDI_RX_CTL(pipe);
3036 temp = I915_READ(reg);
3037 if (HAS_PCH_CPT(dev)) {
3038 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3039 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3040 } else {
3041 temp &= ~FDI_LINK_TRAIN_NONE;
3042 temp |= FDI_LINK_TRAIN_NONE;
3043 }
3044 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3045
3046 /* wait one idle pattern time */
3047 POSTING_READ(reg);
3048 udelay(1000);
357555c0
JB
3049
3050 /* IVB wants error correction enabled */
3051 if (IS_IVYBRIDGE(dev))
3052 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3053 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3054}
3055
1fbc0d78 3056static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 3057{
1fbc0d78 3058 return crtc->base.enabled && crtc->active &&
6e3c9717 3059 crtc->config->has_pch_encoder;
1e833f40
DV
3060}
3061
01a415fd
DV
3062static void ivb_modeset_global_resources(struct drm_device *dev)
3063{
3064 struct drm_i915_private *dev_priv = dev->dev_private;
3065 struct intel_crtc *pipe_B_crtc =
3066 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3067 struct intel_crtc *pipe_C_crtc =
3068 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
3069 uint32_t temp;
3070
1e833f40
DV
3071 /*
3072 * When everything is off disable fdi C so that we could enable fdi B
3073 * with all lanes. Note that we don't care about enabled pipes without
3074 * an enabled pch encoder.
3075 */
3076 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
3077 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
3078 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3079 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3080
3081 temp = I915_READ(SOUTH_CHICKEN1);
3082 temp &= ~FDI_BC_BIFURCATION_SELECT;
3083 DRM_DEBUG_KMS("disabling fdi C rx\n");
3084 I915_WRITE(SOUTH_CHICKEN1, temp);
3085 }
3086}
3087
8db9d77b
ZW
3088/* The FDI link training functions for ILK/Ibexpeak. */
3089static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3090{
3091 struct drm_device *dev = crtc->dev;
3092 struct drm_i915_private *dev_priv = dev->dev_private;
3093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3094 int pipe = intel_crtc->pipe;
5eddb70b 3095 u32 reg, temp, tries;
8db9d77b 3096
1c8562f6 3097 /* FDI needs bits from pipe first */
0fc932b8 3098 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3099
e1a44743
AJ
3100 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3101 for train result */
5eddb70b
CW
3102 reg = FDI_RX_IMR(pipe);
3103 temp = I915_READ(reg);
e1a44743
AJ
3104 temp &= ~FDI_RX_SYMBOL_LOCK;
3105 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3106 I915_WRITE(reg, temp);
3107 I915_READ(reg);
e1a44743
AJ
3108 udelay(150);
3109
8db9d77b 3110 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3111 reg = FDI_TX_CTL(pipe);
3112 temp = I915_READ(reg);
627eb5a3 3113 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3114 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3115 temp &= ~FDI_LINK_TRAIN_NONE;
3116 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3117 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3118
5eddb70b
CW
3119 reg = FDI_RX_CTL(pipe);
3120 temp = I915_READ(reg);
8db9d77b
ZW
3121 temp &= ~FDI_LINK_TRAIN_NONE;
3122 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3123 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3124
3125 POSTING_READ(reg);
8db9d77b
ZW
3126 udelay(150);
3127
5b2adf89 3128 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3129 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3130 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3131 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3132
5eddb70b 3133 reg = FDI_RX_IIR(pipe);
e1a44743 3134 for (tries = 0; tries < 5; tries++) {
5eddb70b 3135 temp = I915_READ(reg);
8db9d77b
ZW
3136 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3137
3138 if ((temp & FDI_RX_BIT_LOCK)) {
3139 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3140 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3141 break;
3142 }
8db9d77b 3143 }
e1a44743 3144 if (tries == 5)
5eddb70b 3145 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3146
3147 /* Train 2 */
5eddb70b
CW
3148 reg = FDI_TX_CTL(pipe);
3149 temp = I915_READ(reg);
8db9d77b
ZW
3150 temp &= ~FDI_LINK_TRAIN_NONE;
3151 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3152 I915_WRITE(reg, temp);
8db9d77b 3153
5eddb70b
CW
3154 reg = FDI_RX_CTL(pipe);
3155 temp = I915_READ(reg);
8db9d77b
ZW
3156 temp &= ~FDI_LINK_TRAIN_NONE;
3157 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3158 I915_WRITE(reg, temp);
8db9d77b 3159
5eddb70b
CW
3160 POSTING_READ(reg);
3161 udelay(150);
8db9d77b 3162
5eddb70b 3163 reg = FDI_RX_IIR(pipe);
e1a44743 3164 for (tries = 0; tries < 5; tries++) {
5eddb70b 3165 temp = I915_READ(reg);
8db9d77b
ZW
3166 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3167
3168 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3169 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3170 DRM_DEBUG_KMS("FDI train 2 done.\n");
3171 break;
3172 }
8db9d77b 3173 }
e1a44743 3174 if (tries == 5)
5eddb70b 3175 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3176
3177 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3178
8db9d77b
ZW
3179}
3180
0206e353 3181static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3182 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3183 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3184 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3185 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3186};
3187
3188/* The FDI link training functions for SNB/Cougarpoint. */
3189static void gen6_fdi_link_train(struct drm_crtc *crtc)
3190{
3191 struct drm_device *dev = crtc->dev;
3192 struct drm_i915_private *dev_priv = dev->dev_private;
3193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3194 int pipe = intel_crtc->pipe;
fa37d39e 3195 u32 reg, temp, i, retry;
8db9d77b 3196
e1a44743
AJ
3197 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3198 for train result */
5eddb70b
CW
3199 reg = FDI_RX_IMR(pipe);
3200 temp = I915_READ(reg);
e1a44743
AJ
3201 temp &= ~FDI_RX_SYMBOL_LOCK;
3202 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3203 I915_WRITE(reg, temp);
3204
3205 POSTING_READ(reg);
e1a44743
AJ
3206 udelay(150);
3207
8db9d77b 3208 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3209 reg = FDI_TX_CTL(pipe);
3210 temp = I915_READ(reg);
627eb5a3 3211 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3212 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3213 temp &= ~FDI_LINK_TRAIN_NONE;
3214 temp |= FDI_LINK_TRAIN_PATTERN_1;
3215 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3216 /* SNB-B */
3217 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3218 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3219
d74cf324
DV
3220 I915_WRITE(FDI_RX_MISC(pipe),
3221 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3222
5eddb70b
CW
3223 reg = FDI_RX_CTL(pipe);
3224 temp = I915_READ(reg);
8db9d77b
ZW
3225 if (HAS_PCH_CPT(dev)) {
3226 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3227 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3228 } else {
3229 temp &= ~FDI_LINK_TRAIN_NONE;
3230 temp |= FDI_LINK_TRAIN_PATTERN_1;
3231 }
5eddb70b
CW
3232 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3233
3234 POSTING_READ(reg);
8db9d77b
ZW
3235 udelay(150);
3236
0206e353 3237 for (i = 0; i < 4; i++) {
5eddb70b
CW
3238 reg = FDI_TX_CTL(pipe);
3239 temp = I915_READ(reg);
8db9d77b
ZW
3240 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3241 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3242 I915_WRITE(reg, temp);
3243
3244 POSTING_READ(reg);
8db9d77b
ZW
3245 udelay(500);
3246
fa37d39e
SP
3247 for (retry = 0; retry < 5; retry++) {
3248 reg = FDI_RX_IIR(pipe);
3249 temp = I915_READ(reg);
3250 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3251 if (temp & FDI_RX_BIT_LOCK) {
3252 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3253 DRM_DEBUG_KMS("FDI train 1 done.\n");
3254 break;
3255 }
3256 udelay(50);
8db9d77b 3257 }
fa37d39e
SP
3258 if (retry < 5)
3259 break;
8db9d77b
ZW
3260 }
3261 if (i == 4)
5eddb70b 3262 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3263
3264 /* Train 2 */
5eddb70b
CW
3265 reg = FDI_TX_CTL(pipe);
3266 temp = I915_READ(reg);
8db9d77b
ZW
3267 temp &= ~FDI_LINK_TRAIN_NONE;
3268 temp |= FDI_LINK_TRAIN_PATTERN_2;
3269 if (IS_GEN6(dev)) {
3270 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3271 /* SNB-B */
3272 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3273 }
5eddb70b 3274 I915_WRITE(reg, temp);
8db9d77b 3275
5eddb70b
CW
3276 reg = FDI_RX_CTL(pipe);
3277 temp = I915_READ(reg);
8db9d77b
ZW
3278 if (HAS_PCH_CPT(dev)) {
3279 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3280 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3281 } else {
3282 temp &= ~FDI_LINK_TRAIN_NONE;
3283 temp |= FDI_LINK_TRAIN_PATTERN_2;
3284 }
5eddb70b
CW
3285 I915_WRITE(reg, temp);
3286
3287 POSTING_READ(reg);
8db9d77b
ZW
3288 udelay(150);
3289
0206e353 3290 for (i = 0; i < 4; i++) {
5eddb70b
CW
3291 reg = FDI_TX_CTL(pipe);
3292 temp = I915_READ(reg);
8db9d77b
ZW
3293 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3294 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3295 I915_WRITE(reg, temp);
3296
3297 POSTING_READ(reg);
8db9d77b
ZW
3298 udelay(500);
3299
fa37d39e
SP
3300 for (retry = 0; retry < 5; retry++) {
3301 reg = FDI_RX_IIR(pipe);
3302 temp = I915_READ(reg);
3303 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3304 if (temp & FDI_RX_SYMBOL_LOCK) {
3305 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3306 DRM_DEBUG_KMS("FDI train 2 done.\n");
3307 break;
3308 }
3309 udelay(50);
8db9d77b 3310 }
fa37d39e
SP
3311 if (retry < 5)
3312 break;
8db9d77b
ZW
3313 }
3314 if (i == 4)
5eddb70b 3315 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3316
3317 DRM_DEBUG_KMS("FDI train done.\n");
3318}
3319
357555c0
JB
3320/* Manual link training for Ivy Bridge A0 parts */
3321static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3322{
3323 struct drm_device *dev = crtc->dev;
3324 struct drm_i915_private *dev_priv = dev->dev_private;
3325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3326 int pipe = intel_crtc->pipe;
139ccd3f 3327 u32 reg, temp, i, j;
357555c0
JB
3328
3329 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3330 for train result */
3331 reg = FDI_RX_IMR(pipe);
3332 temp = I915_READ(reg);
3333 temp &= ~FDI_RX_SYMBOL_LOCK;
3334 temp &= ~FDI_RX_BIT_LOCK;
3335 I915_WRITE(reg, temp);
3336
3337 POSTING_READ(reg);
3338 udelay(150);
3339
01a415fd
DV
3340 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3341 I915_READ(FDI_RX_IIR(pipe)));
3342
139ccd3f
JB
3343 /* Try each vswing and preemphasis setting twice before moving on */
3344 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3345 /* disable first in case we need to retry */
3346 reg = FDI_TX_CTL(pipe);
3347 temp = I915_READ(reg);
3348 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3349 temp &= ~FDI_TX_ENABLE;
3350 I915_WRITE(reg, temp);
357555c0 3351
139ccd3f
JB
3352 reg = FDI_RX_CTL(pipe);
3353 temp = I915_READ(reg);
3354 temp &= ~FDI_LINK_TRAIN_AUTO;
3355 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3356 temp &= ~FDI_RX_ENABLE;
3357 I915_WRITE(reg, temp);
357555c0 3358
139ccd3f 3359 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3360 reg = FDI_TX_CTL(pipe);
3361 temp = I915_READ(reg);
139ccd3f 3362 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3363 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3364 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3365 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3366 temp |= snb_b_fdi_train_param[j/2];
3367 temp |= FDI_COMPOSITE_SYNC;
3368 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3369
139ccd3f
JB
3370 I915_WRITE(FDI_RX_MISC(pipe),
3371 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3372
139ccd3f 3373 reg = FDI_RX_CTL(pipe);
357555c0 3374 temp = I915_READ(reg);
139ccd3f
JB
3375 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3376 temp |= FDI_COMPOSITE_SYNC;
3377 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3378
139ccd3f
JB
3379 POSTING_READ(reg);
3380 udelay(1); /* should be 0.5us */
357555c0 3381
139ccd3f
JB
3382 for (i = 0; i < 4; i++) {
3383 reg = FDI_RX_IIR(pipe);
3384 temp = I915_READ(reg);
3385 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3386
139ccd3f
JB
3387 if (temp & FDI_RX_BIT_LOCK ||
3388 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3389 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3390 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3391 i);
3392 break;
3393 }
3394 udelay(1); /* should be 0.5us */
3395 }
3396 if (i == 4) {
3397 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3398 continue;
3399 }
357555c0 3400
139ccd3f 3401 /* Train 2 */
357555c0
JB
3402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
139ccd3f
JB
3404 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3405 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3406 I915_WRITE(reg, temp);
3407
3408 reg = FDI_RX_CTL(pipe);
3409 temp = I915_READ(reg);
3410 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3411 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3412 I915_WRITE(reg, temp);
3413
3414 POSTING_READ(reg);
139ccd3f 3415 udelay(2); /* should be 1.5us */
357555c0 3416
139ccd3f
JB
3417 for (i = 0; i < 4; i++) {
3418 reg = FDI_RX_IIR(pipe);
3419 temp = I915_READ(reg);
3420 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3421
139ccd3f
JB
3422 if (temp & FDI_RX_SYMBOL_LOCK ||
3423 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3424 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3425 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3426 i);
3427 goto train_done;
3428 }
3429 udelay(2); /* should be 1.5us */
357555c0 3430 }
139ccd3f
JB
3431 if (i == 4)
3432 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3433 }
357555c0 3434
139ccd3f 3435train_done:
357555c0
JB
3436 DRM_DEBUG_KMS("FDI train done.\n");
3437}
3438
88cefb6c 3439static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3440{
88cefb6c 3441 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3442 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3443 int pipe = intel_crtc->pipe;
5eddb70b 3444 u32 reg, temp;
79e53945 3445
c64e311e 3446
c98e9dcf 3447 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3448 reg = FDI_RX_CTL(pipe);
3449 temp = I915_READ(reg);
627eb5a3 3450 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3451 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3452 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3453 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3454
3455 POSTING_READ(reg);
c98e9dcf
JB
3456 udelay(200);
3457
3458 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3459 temp = I915_READ(reg);
3460 I915_WRITE(reg, temp | FDI_PCDCLK);
3461
3462 POSTING_READ(reg);
c98e9dcf
JB
3463 udelay(200);
3464
20749730
PZ
3465 /* Enable CPU FDI TX PLL, always on for Ironlake */
3466 reg = FDI_TX_CTL(pipe);
3467 temp = I915_READ(reg);
3468 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3469 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3470
20749730
PZ
3471 POSTING_READ(reg);
3472 udelay(100);
6be4a607 3473 }
0e23b99d
JB
3474}
3475
88cefb6c
DV
3476static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3477{
3478 struct drm_device *dev = intel_crtc->base.dev;
3479 struct drm_i915_private *dev_priv = dev->dev_private;
3480 int pipe = intel_crtc->pipe;
3481 u32 reg, temp;
3482
3483 /* Switch from PCDclk to Rawclk */
3484 reg = FDI_RX_CTL(pipe);
3485 temp = I915_READ(reg);
3486 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3487
3488 /* Disable CPU FDI TX PLL */
3489 reg = FDI_TX_CTL(pipe);
3490 temp = I915_READ(reg);
3491 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3492
3493 POSTING_READ(reg);
3494 udelay(100);
3495
3496 reg = FDI_RX_CTL(pipe);
3497 temp = I915_READ(reg);
3498 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3499
3500 /* Wait for the clocks to turn off. */
3501 POSTING_READ(reg);
3502 udelay(100);
3503}
3504
0fc932b8
JB
3505static void ironlake_fdi_disable(struct drm_crtc *crtc)
3506{
3507 struct drm_device *dev = crtc->dev;
3508 struct drm_i915_private *dev_priv = dev->dev_private;
3509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3510 int pipe = intel_crtc->pipe;
3511 u32 reg, temp;
3512
3513 /* disable CPU FDI tx and PCH FDI rx */
3514 reg = FDI_TX_CTL(pipe);
3515 temp = I915_READ(reg);
3516 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3517 POSTING_READ(reg);
3518
3519 reg = FDI_RX_CTL(pipe);
3520 temp = I915_READ(reg);
3521 temp &= ~(0x7 << 16);
dfd07d72 3522 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3523 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3524
3525 POSTING_READ(reg);
3526 udelay(100);
3527
3528 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3529 if (HAS_PCH_IBX(dev))
6f06ce18 3530 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3531
3532 /* still set train pattern 1 */
3533 reg = FDI_TX_CTL(pipe);
3534 temp = I915_READ(reg);
3535 temp &= ~FDI_LINK_TRAIN_NONE;
3536 temp |= FDI_LINK_TRAIN_PATTERN_1;
3537 I915_WRITE(reg, temp);
3538
3539 reg = FDI_RX_CTL(pipe);
3540 temp = I915_READ(reg);
3541 if (HAS_PCH_CPT(dev)) {
3542 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3543 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3544 } else {
3545 temp &= ~FDI_LINK_TRAIN_NONE;
3546 temp |= FDI_LINK_TRAIN_PATTERN_1;
3547 }
3548 /* BPC in FDI rx is consistent with that in PIPECONF */
3549 temp &= ~(0x07 << 16);
dfd07d72 3550 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3551 I915_WRITE(reg, temp);
3552
3553 POSTING_READ(reg);
3554 udelay(100);
3555}
3556
5dce5b93
CW
3557bool intel_has_pending_fb_unpin(struct drm_device *dev)
3558{
3559 struct intel_crtc *crtc;
3560
3561 /* Note that we don't need to be called with mode_config.lock here
3562 * as our list of CRTC objects is static for the lifetime of the
3563 * device and so cannot disappear as we iterate. Similarly, we can
3564 * happily treat the predicates as racy, atomic checks as userspace
3565 * cannot claim and pin a new fb without at least acquring the
3566 * struct_mutex and so serialising with us.
3567 */
d3fcc808 3568 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3569 if (atomic_read(&crtc->unpin_work_count) == 0)
3570 continue;
3571
3572 if (crtc->unpin_work)
3573 intel_wait_for_vblank(dev, crtc->pipe);
3574
3575 return true;
3576 }
3577
3578 return false;
3579}
3580
d6bbafa1
CW
3581static void page_flip_completed(struct intel_crtc *intel_crtc)
3582{
3583 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3584 struct intel_unpin_work *work = intel_crtc->unpin_work;
3585
3586 /* ensure that the unpin work is consistent wrt ->pending. */
3587 smp_rmb();
3588 intel_crtc->unpin_work = NULL;
3589
3590 if (work->event)
3591 drm_send_vblank_event(intel_crtc->base.dev,
3592 intel_crtc->pipe,
3593 work->event);
3594
3595 drm_crtc_vblank_put(&intel_crtc->base);
3596
3597 wake_up_all(&dev_priv->pending_flip_queue);
3598 queue_work(dev_priv->wq, &work->work);
3599
3600 trace_i915_flip_complete(intel_crtc->plane,
3601 work->pending_flip_obj);
3602}
3603
46a55d30 3604void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3605{
0f91128d 3606 struct drm_device *dev = crtc->dev;
5bb61643 3607 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3608
2c10d571 3609 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3610 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3611 !intel_crtc_has_pending_flip(crtc),
3612 60*HZ) == 0)) {
3613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3614
5e2d7afc 3615 spin_lock_irq(&dev->event_lock);
9c787942
CW
3616 if (intel_crtc->unpin_work) {
3617 WARN_ONCE(1, "Removing stuck page flip\n");
3618 page_flip_completed(intel_crtc);
3619 }
5e2d7afc 3620 spin_unlock_irq(&dev->event_lock);
9c787942 3621 }
5bb61643 3622
975d568a
CW
3623 if (crtc->primary->fb) {
3624 mutex_lock(&dev->struct_mutex);
3625 intel_finish_fb(crtc->primary->fb);
3626 mutex_unlock(&dev->struct_mutex);
3627 }
e6c3a2a6
CW
3628}
3629
e615efe4
ED
3630/* Program iCLKIP clock to the desired frequency */
3631static void lpt_program_iclkip(struct drm_crtc *crtc)
3632{
3633 struct drm_device *dev = crtc->dev;
3634 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3635 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3636 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3637 u32 temp;
3638
09153000
DV
3639 mutex_lock(&dev_priv->dpio_lock);
3640
e615efe4
ED
3641 /* It is necessary to ungate the pixclk gate prior to programming
3642 * the divisors, and gate it back when it is done.
3643 */
3644 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3645
3646 /* Disable SSCCTL */
3647 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3648 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3649 SBI_SSCCTL_DISABLE,
3650 SBI_ICLK);
e615efe4
ED
3651
3652 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3653 if (clock == 20000) {
e615efe4
ED
3654 auxdiv = 1;
3655 divsel = 0x41;
3656 phaseinc = 0x20;
3657 } else {
3658 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3659 * but the adjusted_mode->crtc_clock in in KHz. To get the
3660 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3661 * convert the virtual clock precision to KHz here for higher
3662 * precision.
3663 */
3664 u32 iclk_virtual_root_freq = 172800 * 1000;
3665 u32 iclk_pi_range = 64;
3666 u32 desired_divisor, msb_divisor_value, pi_value;
3667
12d7ceed 3668 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3669 msb_divisor_value = desired_divisor / iclk_pi_range;
3670 pi_value = desired_divisor % iclk_pi_range;
3671
3672 auxdiv = 0;
3673 divsel = msb_divisor_value - 2;
3674 phaseinc = pi_value;
3675 }
3676
3677 /* This should not happen with any sane values */
3678 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3679 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3680 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3681 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3682
3683 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3684 clock,
e615efe4
ED
3685 auxdiv,
3686 divsel,
3687 phasedir,
3688 phaseinc);
3689
3690 /* Program SSCDIVINTPHASE6 */
988d6ee8 3691 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3692 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3693 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3694 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3695 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3696 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3697 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3698 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3699
3700 /* Program SSCAUXDIV */
988d6ee8 3701 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3702 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3703 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3704 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3705
3706 /* Enable modulator and associated divider */
988d6ee8 3707 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3708 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3709 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3710
3711 /* Wait for initialization time */
3712 udelay(24);
3713
3714 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3715
3716 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3717}
3718
275f01b2
DV
3719static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3720 enum pipe pch_transcoder)
3721{
3722 struct drm_device *dev = crtc->base.dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3724 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3725
3726 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3727 I915_READ(HTOTAL(cpu_transcoder)));
3728 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3729 I915_READ(HBLANK(cpu_transcoder)));
3730 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3731 I915_READ(HSYNC(cpu_transcoder)));
3732
3733 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3734 I915_READ(VTOTAL(cpu_transcoder)));
3735 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3736 I915_READ(VBLANK(cpu_transcoder)));
3737 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3738 I915_READ(VSYNC(cpu_transcoder)));
3739 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3740 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3741}
3742
1fbc0d78
DV
3743static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3744{
3745 struct drm_i915_private *dev_priv = dev->dev_private;
3746 uint32_t temp;
3747
3748 temp = I915_READ(SOUTH_CHICKEN1);
3749 if (temp & FDI_BC_BIFURCATION_SELECT)
3750 return;
3751
3752 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3753 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3754
3755 temp |= FDI_BC_BIFURCATION_SELECT;
3756 DRM_DEBUG_KMS("enabling fdi C rx\n");
3757 I915_WRITE(SOUTH_CHICKEN1, temp);
3758 POSTING_READ(SOUTH_CHICKEN1);
3759}
3760
3761static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3762{
3763 struct drm_device *dev = intel_crtc->base.dev;
3764 struct drm_i915_private *dev_priv = dev->dev_private;
3765
3766 switch (intel_crtc->pipe) {
3767 case PIPE_A:
3768 break;
3769 case PIPE_B:
6e3c9717 3770 if (intel_crtc->config->fdi_lanes > 2)
1fbc0d78
DV
3771 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3772 else
3773 cpt_enable_fdi_bc_bifurcation(dev);
3774
3775 break;
3776 case PIPE_C:
3777 cpt_enable_fdi_bc_bifurcation(dev);
3778
3779 break;
3780 default:
3781 BUG();
3782 }
3783}
3784
f67a559d
JB
3785/*
3786 * Enable PCH resources required for PCH ports:
3787 * - PCH PLLs
3788 * - FDI training & RX/TX
3789 * - update transcoder timings
3790 * - DP transcoding bits
3791 * - transcoder
3792 */
3793static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3794{
3795 struct drm_device *dev = crtc->dev;
3796 struct drm_i915_private *dev_priv = dev->dev_private;
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
ee7b9f93 3799 u32 reg, temp;
2c07245f 3800
ab9412ba 3801 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3802
1fbc0d78
DV
3803 if (IS_IVYBRIDGE(dev))
3804 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3805
cd986abb
DV
3806 /* Write the TU size bits before fdi link training, so that error
3807 * detection works. */
3808 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3809 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3810
c98e9dcf 3811 /* For PCH output, training FDI link */
674cf967 3812 dev_priv->display.fdi_link_train(crtc);
2c07245f 3813
3ad8a208
DV
3814 /* We need to program the right clock selection before writing the pixel
3815 * mutliplier into the DPLL. */
303b81e0 3816 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3817 u32 sel;
4b645f14 3818
c98e9dcf 3819 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3820 temp |= TRANS_DPLL_ENABLE(pipe);
3821 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 3822 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3823 temp |= sel;
3824 else
3825 temp &= ~sel;
c98e9dcf 3826 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3827 }
5eddb70b 3828
3ad8a208
DV
3829 /* XXX: pch pll's can be enabled any time before we enable the PCH
3830 * transcoder, and we actually should do this to not upset any PCH
3831 * transcoder that already use the clock when we share it.
3832 *
3833 * Note that enable_shared_dpll tries to do the right thing, but
3834 * get_shared_dpll unconditionally resets the pll - we need that to have
3835 * the right LVDS enable sequence. */
85b3894f 3836 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3837
d9b6cb56
JB
3838 /* set transcoder timing, panel must allow it */
3839 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3840 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3841
303b81e0 3842 intel_fdi_normal_train(crtc);
5e84e1a4 3843
c98e9dcf 3844 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 3845 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 3846 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3847 reg = TRANS_DP_CTL(pipe);
3848 temp = I915_READ(reg);
3849 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3850 TRANS_DP_SYNC_MASK |
3851 TRANS_DP_BPC_MASK);
5eddb70b
CW
3852 temp |= (TRANS_DP_OUTPUT_ENABLE |
3853 TRANS_DP_ENH_FRAMING);
9325c9f0 3854 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3855
3856 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3857 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3858 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3859 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3860
3861 switch (intel_trans_dp_port_sel(crtc)) {
3862 case PCH_DP_B:
5eddb70b 3863 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3864 break;
3865 case PCH_DP_C:
5eddb70b 3866 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3867 break;
3868 case PCH_DP_D:
5eddb70b 3869 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3870 break;
3871 default:
e95d41e1 3872 BUG();
32f9d658 3873 }
2c07245f 3874
5eddb70b 3875 I915_WRITE(reg, temp);
6be4a607 3876 }
b52eb4dc 3877
b8a4f404 3878 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3879}
3880
1507e5bd
PZ
3881static void lpt_pch_enable(struct drm_crtc *crtc)
3882{
3883 struct drm_device *dev = crtc->dev;
3884 struct drm_i915_private *dev_priv = dev->dev_private;
3885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 3886 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 3887
ab9412ba 3888 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3889
8c52b5e8 3890 lpt_program_iclkip(crtc);
1507e5bd 3891
0540e488 3892 /* Set transcoder timing. */
275f01b2 3893 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3894
937bb610 3895 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3896}
3897
716c2e55 3898void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3899{
e2b78267 3900 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3901
3902 if (pll == NULL)
3903 return;
3904
3e369b76 3905 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 3906 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
3907 return;
3908 }
3909
3e369b76
ACO
3910 pll->config.crtc_mask &= ~(1 << crtc->pipe);
3911 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
3912 WARN_ON(pll->on);
3913 WARN_ON(pll->active);
3914 }
3915
6e3c9717 3916 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3917}
3918
190f68c5
ACO
3919struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
3920 struct intel_crtc_state *crtc_state)
ee7b9f93 3921{
e2b78267 3922 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 3923 struct intel_shared_dpll *pll;
e2b78267 3924 enum intel_dpll_id i;
ee7b9f93 3925
98b6bd99
DV
3926 if (HAS_PCH_IBX(dev_priv->dev)) {
3927 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3928 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3929 pll = &dev_priv->shared_dplls[i];
98b6bd99 3930
46edb027
DV
3931 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3932 crtc->base.base.id, pll->name);
98b6bd99 3933
8bd31e67 3934 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 3935
98b6bd99
DV
3936 goto found;
3937 }
3938
e72f9fbf
DV
3939 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3940 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3941
3942 /* Only want to check enabled timings first */
8bd31e67 3943 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
3944 continue;
3945
190f68c5 3946 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
3947 &pll->new_config->hw_state,
3948 sizeof(pll->new_config->hw_state)) == 0) {
3949 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 3950 crtc->base.base.id, pll->name,
8bd31e67
ACO
3951 pll->new_config->crtc_mask,
3952 pll->active);
ee7b9f93
JB
3953 goto found;
3954 }
3955 }
3956
3957 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3958 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3959 pll = &dev_priv->shared_dplls[i];
8bd31e67 3960 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
3961 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3962 crtc->base.base.id, pll->name);
ee7b9f93
JB
3963 goto found;
3964 }
3965 }
3966
3967 return NULL;
3968
3969found:
8bd31e67 3970 if (pll->new_config->crtc_mask == 0)
190f68c5 3971 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 3972
190f68c5 3973 crtc_state->shared_dpll = i;
46edb027
DV
3974 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3975 pipe_name(crtc->pipe));
ee7b9f93 3976
8bd31e67 3977 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 3978
ee7b9f93
JB
3979 return pll;
3980}
3981
8bd31e67
ACO
3982/**
3983 * intel_shared_dpll_start_config - start a new PLL staged config
3984 * @dev_priv: DRM device
3985 * @clear_pipes: mask of pipes that will have their PLLs freed
3986 *
3987 * Starts a new PLL staged config, copying the current config but
3988 * releasing the references of pipes specified in clear_pipes.
3989 */
3990static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
3991 unsigned clear_pipes)
3992{
3993 struct intel_shared_dpll *pll;
3994 enum intel_dpll_id i;
3995
3996 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3997 pll = &dev_priv->shared_dplls[i];
3998
3999 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4000 GFP_KERNEL);
4001 if (!pll->new_config)
4002 goto cleanup;
4003
4004 pll->new_config->crtc_mask &= ~clear_pipes;
4005 }
4006
4007 return 0;
4008
4009cleanup:
4010 while (--i >= 0) {
4011 pll = &dev_priv->shared_dplls[i];
f354d733 4012 kfree(pll->new_config);
8bd31e67
ACO
4013 pll->new_config = NULL;
4014 }
4015
4016 return -ENOMEM;
4017}
4018
4019static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4020{
4021 struct intel_shared_dpll *pll;
4022 enum intel_dpll_id i;
4023
4024 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4025 pll = &dev_priv->shared_dplls[i];
4026
4027 WARN_ON(pll->new_config == &pll->config);
4028
4029 pll->config = *pll->new_config;
4030 kfree(pll->new_config);
4031 pll->new_config = NULL;
4032 }
4033}
4034
4035static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4036{
4037 struct intel_shared_dpll *pll;
4038 enum intel_dpll_id i;
4039
4040 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4041 pll = &dev_priv->shared_dplls[i];
4042
4043 WARN_ON(pll->new_config == &pll->config);
4044
4045 kfree(pll->new_config);
4046 pll->new_config = NULL;
4047 }
4048}
4049
a1520318 4050static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4051{
4052 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4053 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4054 u32 temp;
4055
4056 temp = I915_READ(dslreg);
4057 udelay(500);
4058 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4059 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4060 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4061 }
4062}
4063
bd2e244f
JB
4064static void skylake_pfit_enable(struct intel_crtc *crtc)
4065{
4066 struct drm_device *dev = crtc->base.dev;
4067 struct drm_i915_private *dev_priv = dev->dev_private;
4068 int pipe = crtc->pipe;
4069
6e3c9717 4070 if (crtc->config->pch_pfit.enabled) {
bd2e244f 4071 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
6e3c9717
ACO
4072 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4073 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
bd2e244f
JB
4074 }
4075}
4076
b074cec8
JB
4077static void ironlake_pfit_enable(struct intel_crtc *crtc)
4078{
4079 struct drm_device *dev = crtc->base.dev;
4080 struct drm_i915_private *dev_priv = dev->dev_private;
4081 int pipe = crtc->pipe;
4082
6e3c9717 4083 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4084 /* Force use of hard-coded filter coefficients
4085 * as some pre-programmed values are broken,
4086 * e.g. x201.
4087 */
4088 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4089 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4090 PF_PIPE_SEL_IVB(pipe));
4091 else
4092 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4093 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4094 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4095 }
4096}
4097
4a3b8769 4098static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4099{
4100 struct drm_device *dev = crtc->dev;
4101 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4102 struct drm_plane *plane;
bb53d4ae
VS
4103 struct intel_plane *intel_plane;
4104
af2b653b
MR
4105 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4106 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4107 if (intel_plane->pipe == pipe)
4108 intel_plane_restore(&intel_plane->base);
af2b653b 4109 }
bb53d4ae
VS
4110}
4111
4a3b8769 4112static void intel_disable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4113{
4114 struct drm_device *dev = crtc->dev;
4115 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4116 struct drm_plane *plane;
bb53d4ae
VS
4117 struct intel_plane *intel_plane;
4118
af2b653b
MR
4119 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4120 intel_plane = to_intel_plane(plane);
bb53d4ae 4121 if (intel_plane->pipe == pipe)
cf4c7c12 4122 plane->funcs->disable_plane(plane);
af2b653b 4123 }
bb53d4ae
VS
4124}
4125
20bc8673 4126void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4127{
cea165c3
VS
4128 struct drm_device *dev = crtc->base.dev;
4129 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4130
6e3c9717 4131 if (!crtc->config->ips_enabled)
d77e4531
PZ
4132 return;
4133
cea165c3
VS
4134 /* We can only enable IPS after we enable a plane and wait for a vblank */
4135 intel_wait_for_vblank(dev, crtc->pipe);
4136
d77e4531 4137 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4138 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4139 mutex_lock(&dev_priv->rps.hw_lock);
4140 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4141 mutex_unlock(&dev_priv->rps.hw_lock);
4142 /* Quoting Art Runyan: "its not safe to expect any particular
4143 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4144 * mailbox." Moreover, the mailbox may return a bogus state,
4145 * so we need to just enable it and continue on.
2a114cc1
BW
4146 */
4147 } else {
4148 I915_WRITE(IPS_CTL, IPS_ENABLE);
4149 /* The bit only becomes 1 in the next vblank, so this wait here
4150 * is essentially intel_wait_for_vblank. If we don't have this
4151 * and don't wait for vblanks until the end of crtc_enable, then
4152 * the HW state readout code will complain that the expected
4153 * IPS_CTL value is not the one we read. */
4154 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4155 DRM_ERROR("Timed out waiting for IPS enable\n");
4156 }
d77e4531
PZ
4157}
4158
20bc8673 4159void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4160{
4161 struct drm_device *dev = crtc->base.dev;
4162 struct drm_i915_private *dev_priv = dev->dev_private;
4163
6e3c9717 4164 if (!crtc->config->ips_enabled)
d77e4531
PZ
4165 return;
4166
4167 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4168 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4169 mutex_lock(&dev_priv->rps.hw_lock);
4170 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4171 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4172 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4173 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4174 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4175 } else {
2a114cc1 4176 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4177 POSTING_READ(IPS_CTL);
4178 }
d77e4531
PZ
4179
4180 /* We need to wait for a vblank before we can disable the plane. */
4181 intel_wait_for_vblank(dev, crtc->pipe);
4182}
4183
4184/** Loads the palette/gamma unit for the CRTC with the prepared values */
4185static void intel_crtc_load_lut(struct drm_crtc *crtc)
4186{
4187 struct drm_device *dev = crtc->dev;
4188 struct drm_i915_private *dev_priv = dev->dev_private;
4189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4190 enum pipe pipe = intel_crtc->pipe;
4191 int palreg = PALETTE(pipe);
4192 int i;
4193 bool reenable_ips = false;
4194
4195 /* The clocks have to be on to load the palette. */
4196 if (!crtc->enabled || !intel_crtc->active)
4197 return;
4198
4199 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
409ee761 4200 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4201 assert_dsi_pll_enabled(dev_priv);
4202 else
4203 assert_pll_enabled(dev_priv, pipe);
4204 }
4205
4206 /* use legacy palette for Ironlake */
7a1db49a 4207 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4208 palreg = LGC_PALETTE(pipe);
4209
4210 /* Workaround : Do not read or write the pipe palette/gamma data while
4211 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4212 */
6e3c9717 4213 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4214 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4215 GAMMA_MODE_MODE_SPLIT)) {
4216 hsw_disable_ips(intel_crtc);
4217 reenable_ips = true;
4218 }
4219
4220 for (i = 0; i < 256; i++) {
4221 I915_WRITE(palreg + 4 * i,
4222 (intel_crtc->lut_r[i] << 16) |
4223 (intel_crtc->lut_g[i] << 8) |
4224 intel_crtc->lut_b[i]);
4225 }
4226
4227 if (reenable_ips)
4228 hsw_enable_ips(intel_crtc);
4229}
4230
d3eedb1a
VS
4231static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4232{
4233 if (!enable && intel_crtc->overlay) {
4234 struct drm_device *dev = intel_crtc->base.dev;
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236
4237 mutex_lock(&dev->struct_mutex);
4238 dev_priv->mm.interruptible = false;
4239 (void) intel_overlay_switch_off(intel_crtc->overlay);
4240 dev_priv->mm.interruptible = true;
4241 mutex_unlock(&dev->struct_mutex);
4242 }
4243
4244 /* Let userspace switch the overlay on again. In most cases userspace
4245 * has to recompute where to put it anyway.
4246 */
4247}
4248
d3eedb1a 4249static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4250{
4251 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4253 int pipe = intel_crtc->pipe;
a5c4d7bc 4254
fdd508a6 4255 intel_enable_primary_hw_plane(crtc->primary, crtc);
4a3b8769 4256 intel_enable_sprite_planes(crtc);
a5c4d7bc 4257 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4258 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4259
4260 hsw_enable_ips(intel_crtc);
4261
4262 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4263 intel_fbc_update(dev);
a5c4d7bc 4264 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4265
4266 /*
4267 * FIXME: Once we grow proper nuclear flip support out of this we need
4268 * to compute the mask of flip planes precisely. For the time being
4269 * consider this a flip from a NULL plane.
4270 */
4271 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4272}
4273
d3eedb1a 4274static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4275{
4276 struct drm_device *dev = crtc->dev;
4277 struct drm_i915_private *dev_priv = dev->dev_private;
4278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4279 int pipe = intel_crtc->pipe;
4280 int plane = intel_crtc->plane;
4281
4282 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4283
4284 if (dev_priv->fbc.plane == plane)
7ff0ebcc 4285 intel_fbc_disable(dev);
a5c4d7bc
VS
4286
4287 hsw_disable_ips(intel_crtc);
4288
d3eedb1a 4289 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc 4290 intel_crtc_update_cursor(crtc, false);
4a3b8769 4291 intel_disable_sprite_planes(crtc);
fdd508a6 4292 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4293
f99d7069
DV
4294 /*
4295 * FIXME: Once we grow proper nuclear flip support out of this we need
4296 * to compute the mask of flip planes precisely. For the time being
4297 * consider this a flip to a NULL plane.
4298 */
4299 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4300}
4301
f67a559d
JB
4302static void ironlake_crtc_enable(struct drm_crtc *crtc)
4303{
4304 struct drm_device *dev = crtc->dev;
4305 struct drm_i915_private *dev_priv = dev->dev_private;
4306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4307 struct intel_encoder *encoder;
f67a559d 4308 int pipe = intel_crtc->pipe;
f67a559d 4309
08a48469
DV
4310 WARN_ON(!crtc->enabled);
4311
f67a559d
JB
4312 if (intel_crtc->active)
4313 return;
4314
6e3c9717 4315 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4316 intel_prepare_shared_dpll(intel_crtc);
4317
6e3c9717 4318 if (intel_crtc->config->has_dp_encoder)
29407aab
DV
4319 intel_dp_set_m_n(intel_crtc);
4320
4321 intel_set_pipe_timings(intel_crtc);
4322
6e3c9717 4323 if (intel_crtc->config->has_pch_encoder) {
29407aab 4324 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4325 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4326 }
4327
4328 ironlake_set_pipeconf(crtc);
4329
f67a559d 4330 intel_crtc->active = true;
8664281b 4331
a72e4c9f
DV
4332 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4333 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4334
f6736a1a 4335 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4336 if (encoder->pre_enable)
4337 encoder->pre_enable(encoder);
f67a559d 4338
6e3c9717 4339 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4340 /* Note: FDI PLL enabling _must_ be done before we enable the
4341 * cpu pipes, hence this is separate from all the other fdi/pch
4342 * enabling. */
88cefb6c 4343 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4344 } else {
4345 assert_fdi_tx_disabled(dev_priv, pipe);
4346 assert_fdi_rx_disabled(dev_priv, pipe);
4347 }
f67a559d 4348
b074cec8 4349 ironlake_pfit_enable(intel_crtc);
f67a559d 4350
9c54c0dd
JB
4351 /*
4352 * On ILK+ LUT must be loaded before the pipe is running but with
4353 * clocks enabled
4354 */
4355 intel_crtc_load_lut(crtc);
4356
f37fcc2a 4357 intel_update_watermarks(crtc);
e1fdc473 4358 intel_enable_pipe(intel_crtc);
f67a559d 4359
6e3c9717 4360 if (intel_crtc->config->has_pch_encoder)
f67a559d 4361 ironlake_pch_enable(crtc);
c98e9dcf 4362
f9b61ff6
DV
4363 assert_vblank_disabled(crtc);
4364 drm_crtc_vblank_on(crtc);
4365
fa5c73b1
DV
4366 for_each_encoder_on_crtc(dev, crtc, encoder)
4367 encoder->enable(encoder);
61b77ddd
DV
4368
4369 if (HAS_PCH_CPT(dev))
a1520318 4370 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4371
d3eedb1a 4372 intel_crtc_enable_planes(crtc);
6be4a607
JB
4373}
4374
42db64ef
PZ
4375/* IPS only exists on ULT machines and is tied to pipe A. */
4376static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4377{
f5adf94e 4378 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4379}
4380
e4916946
PZ
4381/*
4382 * This implements the workaround described in the "notes" section of the mode
4383 * set sequence documentation. When going from no pipes or single pipe to
4384 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4385 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4386 */
4387static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4388{
4389 struct drm_device *dev = crtc->base.dev;
4390 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4391
4392 /* We want to get the other_active_crtc only if there's only 1 other
4393 * active crtc. */
d3fcc808 4394 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4395 if (!crtc_it->active || crtc_it == crtc)
4396 continue;
4397
4398 if (other_active_crtc)
4399 return;
4400
4401 other_active_crtc = crtc_it;
4402 }
4403 if (!other_active_crtc)
4404 return;
4405
4406 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4407 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4408}
4409
4f771f10
PZ
4410static void haswell_crtc_enable(struct drm_crtc *crtc)
4411{
4412 struct drm_device *dev = crtc->dev;
4413 struct drm_i915_private *dev_priv = dev->dev_private;
4414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4415 struct intel_encoder *encoder;
4416 int pipe = intel_crtc->pipe;
4f771f10
PZ
4417
4418 WARN_ON(!crtc->enabled);
4419
4420 if (intel_crtc->active)
4421 return;
4422
df8ad70c
DV
4423 if (intel_crtc_to_shared_dpll(intel_crtc))
4424 intel_enable_shared_dpll(intel_crtc);
4425
6e3c9717 4426 if (intel_crtc->config->has_dp_encoder)
229fca97
DV
4427 intel_dp_set_m_n(intel_crtc);
4428
4429 intel_set_pipe_timings(intel_crtc);
4430
6e3c9717
ACO
4431 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4432 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4433 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4434 }
4435
6e3c9717 4436 if (intel_crtc->config->has_pch_encoder) {
229fca97 4437 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4438 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4439 }
4440
4441 haswell_set_pipeconf(crtc);
4442
4443 intel_set_pipe_csc(crtc);
4444
4f771f10 4445 intel_crtc->active = true;
8664281b 4446
a72e4c9f 4447 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4448 for_each_encoder_on_crtc(dev, crtc, encoder)
4449 if (encoder->pre_enable)
4450 encoder->pre_enable(encoder);
4451
6e3c9717 4452 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4453 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4454 true);
4fe9467d
ID
4455 dev_priv->display.fdi_link_train(crtc);
4456 }
4457
1f544388 4458 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4459
bd2e244f
JB
4460 if (IS_SKYLAKE(dev))
4461 skylake_pfit_enable(intel_crtc);
4462 else
4463 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4464
4465 /*
4466 * On ILK+ LUT must be loaded before the pipe is running but with
4467 * clocks enabled
4468 */
4469 intel_crtc_load_lut(crtc);
4470
1f544388 4471 intel_ddi_set_pipe_settings(crtc);
8228c251 4472 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4473
f37fcc2a 4474 intel_update_watermarks(crtc);
e1fdc473 4475 intel_enable_pipe(intel_crtc);
42db64ef 4476
6e3c9717 4477 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4478 lpt_pch_enable(crtc);
4f771f10 4479
6e3c9717 4480 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4481 intel_ddi_set_vc_payload_alloc(crtc, true);
4482
f9b61ff6
DV
4483 assert_vblank_disabled(crtc);
4484 drm_crtc_vblank_on(crtc);
4485
8807e55b 4486 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4487 encoder->enable(encoder);
8807e55b
JN
4488 intel_opregion_notify_encoder(encoder, true);
4489 }
4f771f10 4490
e4916946
PZ
4491 /* If we change the relative order between pipe/planes enabling, we need
4492 * to change the workaround. */
4493 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4494 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4495}
4496
bd2e244f
JB
4497static void skylake_pfit_disable(struct intel_crtc *crtc)
4498{
4499 struct drm_device *dev = crtc->base.dev;
4500 struct drm_i915_private *dev_priv = dev->dev_private;
4501 int pipe = crtc->pipe;
4502
4503 /* To avoid upsetting the power well on haswell only disable the pfit if
4504 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4505 if (crtc->config->pch_pfit.enabled) {
bd2e244f
JB
4506 I915_WRITE(PS_CTL(pipe), 0);
4507 I915_WRITE(PS_WIN_POS(pipe), 0);
4508 I915_WRITE(PS_WIN_SZ(pipe), 0);
4509 }
4510}
4511
3f8dce3a
DV
4512static void ironlake_pfit_disable(struct intel_crtc *crtc)
4513{
4514 struct drm_device *dev = crtc->base.dev;
4515 struct drm_i915_private *dev_priv = dev->dev_private;
4516 int pipe = crtc->pipe;
4517
4518 /* To avoid upsetting the power well on haswell only disable the pfit if
4519 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4520 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4521 I915_WRITE(PF_CTL(pipe), 0);
4522 I915_WRITE(PF_WIN_POS(pipe), 0);
4523 I915_WRITE(PF_WIN_SZ(pipe), 0);
4524 }
4525}
4526
6be4a607
JB
4527static void ironlake_crtc_disable(struct drm_crtc *crtc)
4528{
4529 struct drm_device *dev = crtc->dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4532 struct intel_encoder *encoder;
6be4a607 4533 int pipe = intel_crtc->pipe;
5eddb70b 4534 u32 reg, temp;
b52eb4dc 4535
f7abfe8b
CW
4536 if (!intel_crtc->active)
4537 return;
4538
d3eedb1a 4539 intel_crtc_disable_planes(crtc);
a5c4d7bc 4540
ea9d758d
DV
4541 for_each_encoder_on_crtc(dev, crtc, encoder)
4542 encoder->disable(encoder);
4543
f9b61ff6
DV
4544 drm_crtc_vblank_off(crtc);
4545 assert_vblank_disabled(crtc);
4546
6e3c9717 4547 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 4548 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4549
575f7ab7 4550 intel_disable_pipe(intel_crtc);
32f9d658 4551
3f8dce3a 4552 ironlake_pfit_disable(intel_crtc);
2c07245f 4553
bf49ec8c
DV
4554 for_each_encoder_on_crtc(dev, crtc, encoder)
4555 if (encoder->post_disable)
4556 encoder->post_disable(encoder);
2c07245f 4557
6e3c9717 4558 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4559 ironlake_fdi_disable(crtc);
913d8d11 4560
d925c59a 4561 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4562
d925c59a
DV
4563 if (HAS_PCH_CPT(dev)) {
4564 /* disable TRANS_DP_CTL */
4565 reg = TRANS_DP_CTL(pipe);
4566 temp = I915_READ(reg);
4567 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4568 TRANS_DP_PORT_SEL_MASK);
4569 temp |= TRANS_DP_PORT_SEL_NONE;
4570 I915_WRITE(reg, temp);
4571
4572 /* disable DPLL_SEL */
4573 temp = I915_READ(PCH_DPLL_SEL);
11887397 4574 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4575 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4576 }
e3421a18 4577
d925c59a 4578 /* disable PCH DPLL */
e72f9fbf 4579 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4580
d925c59a
DV
4581 ironlake_fdi_pll_disable(intel_crtc);
4582 }
6b383a7f 4583
f7abfe8b 4584 intel_crtc->active = false;
46ba614c 4585 intel_update_watermarks(crtc);
d1ebd816
BW
4586
4587 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4588 intel_fbc_update(dev);
d1ebd816 4589 mutex_unlock(&dev->struct_mutex);
6be4a607 4590}
1b3c7a47 4591
4f771f10 4592static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4593{
4f771f10
PZ
4594 struct drm_device *dev = crtc->dev;
4595 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4597 struct intel_encoder *encoder;
6e3c9717 4598 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 4599
4f771f10
PZ
4600 if (!intel_crtc->active)
4601 return;
4602
d3eedb1a 4603 intel_crtc_disable_planes(crtc);
dda9a66a 4604
8807e55b
JN
4605 for_each_encoder_on_crtc(dev, crtc, encoder) {
4606 intel_opregion_notify_encoder(encoder, false);
4f771f10 4607 encoder->disable(encoder);
8807e55b 4608 }
4f771f10 4609
f9b61ff6
DV
4610 drm_crtc_vblank_off(crtc);
4611 assert_vblank_disabled(crtc);
4612
6e3c9717 4613 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
4614 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4615 false);
575f7ab7 4616 intel_disable_pipe(intel_crtc);
4f771f10 4617
6e3c9717 4618 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
4619 intel_ddi_set_vc_payload_alloc(crtc, false);
4620
ad80a810 4621 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4622
bd2e244f
JB
4623 if (IS_SKYLAKE(dev))
4624 skylake_pfit_disable(intel_crtc);
4625 else
4626 ironlake_pfit_disable(intel_crtc);
4f771f10 4627
1f544388 4628 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4629
6e3c9717 4630 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 4631 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 4632 intel_ddi_fdi_disable(crtc);
83616634 4633 }
4f771f10 4634
97b040aa
ID
4635 for_each_encoder_on_crtc(dev, crtc, encoder)
4636 if (encoder->post_disable)
4637 encoder->post_disable(encoder);
4638
4f771f10 4639 intel_crtc->active = false;
46ba614c 4640 intel_update_watermarks(crtc);
4f771f10
PZ
4641
4642 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4643 intel_fbc_update(dev);
4f771f10 4644 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4645
4646 if (intel_crtc_to_shared_dpll(intel_crtc))
4647 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4648}
4649
ee7b9f93
JB
4650static void ironlake_crtc_off(struct drm_crtc *crtc)
4651{
4652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4653 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4654}
4655
6441ab5f 4656
2dd24552
JB
4657static void i9xx_pfit_enable(struct intel_crtc *crtc)
4658{
4659 struct drm_device *dev = crtc->base.dev;
4660 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4661 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 4662
681a8504 4663 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
4664 return;
4665
2dd24552 4666 /*
c0b03411
DV
4667 * The panel fitter should only be adjusted whilst the pipe is disabled,
4668 * according to register description and PRM.
2dd24552 4669 */
c0b03411
DV
4670 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4671 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4672
b074cec8
JB
4673 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4674 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4675
4676 /* Border color in case we don't scale up to the full screen. Black by
4677 * default, change to something else for debugging. */
4678 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4679}
4680
d05410f9
DA
4681static enum intel_display_power_domain port_to_power_domain(enum port port)
4682{
4683 switch (port) {
4684 case PORT_A:
4685 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4686 case PORT_B:
4687 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4688 case PORT_C:
4689 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4690 case PORT_D:
4691 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4692 default:
4693 WARN_ON_ONCE(1);
4694 return POWER_DOMAIN_PORT_OTHER;
4695 }
4696}
4697
77d22dca
ID
4698#define for_each_power_domain(domain, mask) \
4699 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4700 if ((1 << (domain)) & (mask))
4701
319be8ae
ID
4702enum intel_display_power_domain
4703intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4704{
4705 struct drm_device *dev = intel_encoder->base.dev;
4706 struct intel_digital_port *intel_dig_port;
4707
4708 switch (intel_encoder->type) {
4709 case INTEL_OUTPUT_UNKNOWN:
4710 /* Only DDI platforms should ever use this output type */
4711 WARN_ON_ONCE(!HAS_DDI(dev));
4712 case INTEL_OUTPUT_DISPLAYPORT:
4713 case INTEL_OUTPUT_HDMI:
4714 case INTEL_OUTPUT_EDP:
4715 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4716 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4717 case INTEL_OUTPUT_DP_MST:
4718 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4719 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4720 case INTEL_OUTPUT_ANALOG:
4721 return POWER_DOMAIN_PORT_CRT;
4722 case INTEL_OUTPUT_DSI:
4723 return POWER_DOMAIN_PORT_DSI;
4724 default:
4725 return POWER_DOMAIN_PORT_OTHER;
4726 }
4727}
4728
4729static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4730{
319be8ae
ID
4731 struct drm_device *dev = crtc->dev;
4732 struct intel_encoder *intel_encoder;
4733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4734 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4735 unsigned long mask;
4736 enum transcoder transcoder;
4737
4738 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4739
4740 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4741 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
4742 if (intel_crtc->config->pch_pfit.enabled ||
4743 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
4744 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4745
319be8ae
ID
4746 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4747 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4748
77d22dca
ID
4749 return mask;
4750}
4751
77d22dca
ID
4752static void modeset_update_crtc_power_domains(struct drm_device *dev)
4753{
4754 struct drm_i915_private *dev_priv = dev->dev_private;
4755 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4756 struct intel_crtc *crtc;
4757
4758 /*
4759 * First get all needed power domains, then put all unneeded, to avoid
4760 * any unnecessary toggling of the power wells.
4761 */
d3fcc808 4762 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4763 enum intel_display_power_domain domain;
4764
4765 if (!crtc->base.enabled)
4766 continue;
4767
319be8ae 4768 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4769
4770 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4771 intel_display_power_get(dev_priv, domain);
4772 }
4773
50f6e502
VS
4774 if (dev_priv->display.modeset_global_resources)
4775 dev_priv->display.modeset_global_resources(dev);
4776
d3fcc808 4777 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4778 enum intel_display_power_domain domain;
4779
4780 for_each_power_domain(domain, crtc->enabled_power_domains)
4781 intel_display_power_put(dev_priv, domain);
4782
4783 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4784 }
4785
4786 intel_display_set_init_power(dev_priv, false);
4787}
4788
dfcab17e 4789/* returns HPLL frequency in kHz */
f8bf63fd 4790static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4791{
586f49dc 4792 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4793
586f49dc
JB
4794 /* Obtain SKU information */
4795 mutex_lock(&dev_priv->dpio_lock);
4796 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4797 CCK_FUSE_HPLL_FREQ_MASK;
4798 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4799
dfcab17e 4800 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4801}
4802
f8bf63fd
VS
4803static void vlv_update_cdclk(struct drm_device *dev)
4804{
4805 struct drm_i915_private *dev_priv = dev->dev_private;
4806
4807 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 4808 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
f8bf63fd
VS
4809 dev_priv->vlv_cdclk_freq);
4810
4811 /*
4812 * Program the gmbus_freq based on the cdclk frequency.
4813 * BSpec erroneously claims we should aim for 4MHz, but
4814 * in fact 1MHz is the correct frequency.
4815 */
6be1e3d3 4816 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
f8bf63fd
VS
4817}
4818
30a970c6
JB
4819/* Adjust CDclk dividers to allow high res or save power if possible */
4820static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4821{
4822 struct drm_i915_private *dev_priv = dev->dev_private;
4823 u32 val, cmd;
4824
d197b7d3 4825 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4826
dfcab17e 4827 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4828 cmd = 2;
dfcab17e 4829 else if (cdclk == 266667)
30a970c6
JB
4830 cmd = 1;
4831 else
4832 cmd = 0;
4833
4834 mutex_lock(&dev_priv->rps.hw_lock);
4835 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4836 val &= ~DSPFREQGUAR_MASK;
4837 val |= (cmd << DSPFREQGUAR_SHIFT);
4838 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4839 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4840 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4841 50)) {
4842 DRM_ERROR("timed out waiting for CDclk change\n");
4843 }
4844 mutex_unlock(&dev_priv->rps.hw_lock);
4845
dfcab17e 4846 if (cdclk == 400000) {
6bcda4f0 4847 u32 divider;
30a970c6 4848
6bcda4f0 4849 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
4850
4851 mutex_lock(&dev_priv->dpio_lock);
4852 /* adjust cdclk divider */
4853 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4854 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4855 val |= divider;
4856 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4857
4858 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4859 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4860 50))
4861 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4862 mutex_unlock(&dev_priv->dpio_lock);
4863 }
4864
4865 mutex_lock(&dev_priv->dpio_lock);
4866 /* adjust self-refresh exit latency value */
4867 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4868 val &= ~0x7f;
4869
4870 /*
4871 * For high bandwidth configs, we set a higher latency in the bunit
4872 * so that the core display fetch happens in time to avoid underruns.
4873 */
dfcab17e 4874 if (cdclk == 400000)
30a970c6
JB
4875 val |= 4500 / 250; /* 4.5 usec */
4876 else
4877 val |= 3000 / 250; /* 3.0 usec */
4878 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4879 mutex_unlock(&dev_priv->dpio_lock);
4880
f8bf63fd 4881 vlv_update_cdclk(dev);
30a970c6
JB
4882}
4883
383c5a6a
VS
4884static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4885{
4886 struct drm_i915_private *dev_priv = dev->dev_private;
4887 u32 val, cmd;
4888
4889 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4890
4891 switch (cdclk) {
4892 case 400000:
4893 cmd = 3;
4894 break;
4895 case 333333:
4896 case 320000:
4897 cmd = 2;
4898 break;
4899 case 266667:
4900 cmd = 1;
4901 break;
4902 case 200000:
4903 cmd = 0;
4904 break;
4905 default:
5f77eeb0 4906 MISSING_CASE(cdclk);
383c5a6a
VS
4907 return;
4908 }
4909
4910 mutex_lock(&dev_priv->rps.hw_lock);
4911 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4912 val &= ~DSPFREQGUAR_MASK_CHV;
4913 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4914 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4915 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4916 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4917 50)) {
4918 DRM_ERROR("timed out waiting for CDclk change\n");
4919 }
4920 mutex_unlock(&dev_priv->rps.hw_lock);
4921
4922 vlv_update_cdclk(dev);
4923}
4924
30a970c6
JB
4925static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4926 int max_pixclk)
4927{
6bcda4f0 4928 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
29dc7ef3 4929
d49a340d
VS
4930 /* FIXME: Punit isn't quite ready yet */
4931 if (IS_CHERRYVIEW(dev_priv->dev))
4932 return 400000;
4933
30a970c6
JB
4934 /*
4935 * Really only a few cases to deal with, as only 4 CDclks are supported:
4936 * 200MHz
4937 * 267MHz
29dc7ef3 4938 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4939 * 400MHz
4940 * So we check to see whether we're above 90% of the lower bin and
4941 * adjust if needed.
e37c67a1
VS
4942 *
4943 * We seem to get an unstable or solid color picture at 200MHz.
4944 * Not sure what's wrong. For now use 200MHz only when all pipes
4945 * are off.
30a970c6 4946 */
29dc7ef3 4947 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4948 return 400000;
4949 else if (max_pixclk > 266667*9/10)
29dc7ef3 4950 return freq_320;
e37c67a1 4951 else if (max_pixclk > 0)
dfcab17e 4952 return 266667;
e37c67a1
VS
4953 else
4954 return 200000;
30a970c6
JB
4955}
4956
2f2d7aa1
VS
4957/* compute the max pixel clock for new configuration */
4958static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4959{
4960 struct drm_device *dev = dev_priv->dev;
4961 struct intel_crtc *intel_crtc;
4962 int max_pixclk = 0;
4963
d3fcc808 4964 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4965 if (intel_crtc->new_enabled)
30a970c6 4966 max_pixclk = max(max_pixclk,
2d112de7 4967 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
30a970c6
JB
4968 }
4969
4970 return max_pixclk;
4971}
4972
4973static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4974 unsigned *prepare_pipes)
30a970c6
JB
4975{
4976 struct drm_i915_private *dev_priv = dev->dev_private;
4977 struct intel_crtc *intel_crtc;
2f2d7aa1 4978 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4979
d60c4473
ID
4980 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4981 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4982 return;
4983
2f2d7aa1 4984 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4985 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4986 if (intel_crtc->base.enabled)
4987 *prepare_pipes |= (1 << intel_crtc->pipe);
4988}
4989
4990static void valleyview_modeset_global_resources(struct drm_device *dev)
4991{
4992 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4993 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4994 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4995
383c5a6a 4996 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
738c05c0
ID
4997 /*
4998 * FIXME: We can end up here with all power domains off, yet
4999 * with a CDCLK frequency other than the minimum. To account
5000 * for this take the PIPE-A power domain, which covers the HW
5001 * blocks needed for the following programming. This can be
5002 * removed once it's guaranteed that we get here either with
5003 * the minimum CDCLK set, or the required power domains
5004 * enabled.
5005 */
5006 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5007
383c5a6a
VS
5008 if (IS_CHERRYVIEW(dev))
5009 cherryview_set_cdclk(dev, req_cdclk);
5010 else
5011 valleyview_set_cdclk(dev, req_cdclk);
738c05c0
ID
5012
5013 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5014 }
30a970c6
JB
5015}
5016
89b667f8
JB
5017static void valleyview_crtc_enable(struct drm_crtc *crtc)
5018{
5019 struct drm_device *dev = crtc->dev;
a72e4c9f 5020 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5022 struct intel_encoder *encoder;
5023 int pipe = intel_crtc->pipe;
23538ef1 5024 bool is_dsi;
89b667f8
JB
5025
5026 WARN_ON(!crtc->enabled);
5027
5028 if (intel_crtc->active)
5029 return;
5030
409ee761 5031 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5032
1ae0d137
VS
5033 if (!is_dsi) {
5034 if (IS_CHERRYVIEW(dev))
6e3c9717 5035 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5036 else
6e3c9717 5037 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5038 }
5b18e57c 5039
6e3c9717 5040 if (intel_crtc->config->has_dp_encoder)
5b18e57c
DV
5041 intel_dp_set_m_n(intel_crtc);
5042
5043 intel_set_pipe_timings(intel_crtc);
5044
c14b0485
VS
5045 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5046 struct drm_i915_private *dev_priv = dev->dev_private;
5047
5048 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5049 I915_WRITE(CHV_CANVAS(pipe), 0);
5050 }
5051
5b18e57c
DV
5052 i9xx_set_pipeconf(intel_crtc);
5053
89b667f8 5054 intel_crtc->active = true;
89b667f8 5055
a72e4c9f 5056 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5057
89b667f8
JB
5058 for_each_encoder_on_crtc(dev, crtc, encoder)
5059 if (encoder->pre_pll_enable)
5060 encoder->pre_pll_enable(encoder);
5061
9d556c99
CML
5062 if (!is_dsi) {
5063 if (IS_CHERRYVIEW(dev))
6e3c9717 5064 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5065 else
6e3c9717 5066 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5067 }
89b667f8
JB
5068
5069 for_each_encoder_on_crtc(dev, crtc, encoder)
5070 if (encoder->pre_enable)
5071 encoder->pre_enable(encoder);
5072
2dd24552
JB
5073 i9xx_pfit_enable(intel_crtc);
5074
63cbb074
VS
5075 intel_crtc_load_lut(crtc);
5076
f37fcc2a 5077 intel_update_watermarks(crtc);
e1fdc473 5078 intel_enable_pipe(intel_crtc);
be6a6f8e 5079
4b3a9526
VS
5080 assert_vblank_disabled(crtc);
5081 drm_crtc_vblank_on(crtc);
5082
f9b61ff6
DV
5083 for_each_encoder_on_crtc(dev, crtc, encoder)
5084 encoder->enable(encoder);
5085
9ab0460b 5086 intel_crtc_enable_planes(crtc);
d40d9187 5087
56b80e1f 5088 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5089 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
5090}
5091
f13c2ef3
DV
5092static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5093{
5094 struct drm_device *dev = crtc->base.dev;
5095 struct drm_i915_private *dev_priv = dev->dev_private;
5096
6e3c9717
ACO
5097 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5098 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5099}
5100
0b8765c6 5101static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5102{
5103 struct drm_device *dev = crtc->dev;
a72e4c9f 5104 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5106 struct intel_encoder *encoder;
79e53945 5107 int pipe = intel_crtc->pipe;
79e53945 5108
08a48469
DV
5109 WARN_ON(!crtc->enabled);
5110
f7abfe8b
CW
5111 if (intel_crtc->active)
5112 return;
5113
f13c2ef3
DV
5114 i9xx_set_pll_dividers(intel_crtc);
5115
6e3c9717 5116 if (intel_crtc->config->has_dp_encoder)
5b18e57c
DV
5117 intel_dp_set_m_n(intel_crtc);
5118
5119 intel_set_pipe_timings(intel_crtc);
5120
5b18e57c
DV
5121 i9xx_set_pipeconf(intel_crtc);
5122
f7abfe8b 5123 intel_crtc->active = true;
6b383a7f 5124
4a3436e8 5125 if (!IS_GEN2(dev))
a72e4c9f 5126 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5127
9d6d9f19
MK
5128 for_each_encoder_on_crtc(dev, crtc, encoder)
5129 if (encoder->pre_enable)
5130 encoder->pre_enable(encoder);
5131
f6736a1a
DV
5132 i9xx_enable_pll(intel_crtc);
5133
2dd24552
JB
5134 i9xx_pfit_enable(intel_crtc);
5135
63cbb074
VS
5136 intel_crtc_load_lut(crtc);
5137
f37fcc2a 5138 intel_update_watermarks(crtc);
e1fdc473 5139 intel_enable_pipe(intel_crtc);
be6a6f8e 5140
4b3a9526
VS
5141 assert_vblank_disabled(crtc);
5142 drm_crtc_vblank_on(crtc);
5143
f9b61ff6
DV
5144 for_each_encoder_on_crtc(dev, crtc, encoder)
5145 encoder->enable(encoder);
5146
9ab0460b 5147 intel_crtc_enable_planes(crtc);
d40d9187 5148
4a3436e8
VS
5149 /*
5150 * Gen2 reports pipe underruns whenever all planes are disabled.
5151 * So don't enable underrun reporting before at least some planes
5152 * are enabled.
5153 * FIXME: Need to fix the logic to work when we turn off all planes
5154 * but leave the pipe running.
5155 */
5156 if (IS_GEN2(dev))
a72e4c9f 5157 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5158
56b80e1f 5159 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5160 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 5161}
79e53945 5162
87476d63
DV
5163static void i9xx_pfit_disable(struct intel_crtc *crtc)
5164{
5165 struct drm_device *dev = crtc->base.dev;
5166 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5167
6e3c9717 5168 if (!crtc->config->gmch_pfit.control)
328d8e82 5169 return;
87476d63 5170
328d8e82 5171 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5172
328d8e82
DV
5173 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5174 I915_READ(PFIT_CONTROL));
5175 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5176}
5177
0b8765c6
JB
5178static void i9xx_crtc_disable(struct drm_crtc *crtc)
5179{
5180 struct drm_device *dev = crtc->dev;
5181 struct drm_i915_private *dev_priv = dev->dev_private;
5182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5183 struct intel_encoder *encoder;
0b8765c6 5184 int pipe = intel_crtc->pipe;
ef9c3aee 5185
f7abfe8b
CW
5186 if (!intel_crtc->active)
5187 return;
5188
4a3436e8
VS
5189 /*
5190 * Gen2 reports pipe underruns whenever all planes are disabled.
5191 * So diasble underrun reporting before all the planes get disabled.
5192 * FIXME: Need to fix the logic to work when we turn off all planes
5193 * but leave the pipe running.
5194 */
5195 if (IS_GEN2(dev))
a72e4c9f 5196 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5197
564ed191
ID
5198 /*
5199 * Vblank time updates from the shadow to live plane control register
5200 * are blocked if the memory self-refresh mode is active at that
5201 * moment. So to make sure the plane gets truly disabled, disable
5202 * first the self-refresh mode. The self-refresh enable bit in turn
5203 * will be checked/applied by the HW only at the next frame start
5204 * event which is after the vblank start event, so we need to have a
5205 * wait-for-vblank between disabling the plane and the pipe.
5206 */
5207 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
5208 intel_crtc_disable_planes(crtc);
5209
6304cd91
VS
5210 /*
5211 * On gen2 planes are double buffered but the pipe isn't, so we must
5212 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5213 * We also need to wait on all gmch platforms because of the
5214 * self-refresh mode constraint explained above.
6304cd91 5215 */
564ed191 5216 intel_wait_for_vblank(dev, pipe);
6304cd91 5217
4b3a9526
VS
5218 for_each_encoder_on_crtc(dev, crtc, encoder)
5219 encoder->disable(encoder);
5220
f9b61ff6
DV
5221 drm_crtc_vblank_off(crtc);
5222 assert_vblank_disabled(crtc);
5223
575f7ab7 5224 intel_disable_pipe(intel_crtc);
24a1f16d 5225
87476d63 5226 i9xx_pfit_disable(intel_crtc);
24a1f16d 5227
89b667f8
JB
5228 for_each_encoder_on_crtc(dev, crtc, encoder)
5229 if (encoder->post_disable)
5230 encoder->post_disable(encoder);
5231
409ee761 5232 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
5233 if (IS_CHERRYVIEW(dev))
5234 chv_disable_pll(dev_priv, pipe);
5235 else if (IS_VALLEYVIEW(dev))
5236 vlv_disable_pll(dev_priv, pipe);
5237 else
1c4e0274 5238 i9xx_disable_pll(intel_crtc);
076ed3b2 5239 }
0b8765c6 5240
4a3436e8 5241 if (!IS_GEN2(dev))
a72e4c9f 5242 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5243
f7abfe8b 5244 intel_crtc->active = false;
46ba614c 5245 intel_update_watermarks(crtc);
f37fcc2a 5246
efa9624e 5247 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5248 intel_fbc_update(dev);
efa9624e 5249 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5250}
5251
ee7b9f93
JB
5252static void i9xx_crtc_off(struct drm_crtc *crtc)
5253{
5254}
5255
b04c5bd6
BF
5256/* Master function to enable/disable CRTC and corresponding power wells */
5257void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5258{
5259 struct drm_device *dev = crtc->dev;
5260 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5262 enum intel_display_power_domain domain;
5263 unsigned long domains;
976f8a20 5264
0e572fe7
DV
5265 if (enable) {
5266 if (!intel_crtc->active) {
e1e9fb84
DV
5267 domains = get_crtc_power_domains(crtc);
5268 for_each_power_domain(domain, domains)
5269 intel_display_power_get(dev_priv, domain);
5270 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5271
5272 dev_priv->display.crtc_enable(crtc);
5273 }
5274 } else {
5275 if (intel_crtc->active) {
5276 dev_priv->display.crtc_disable(crtc);
5277
e1e9fb84
DV
5278 domains = intel_crtc->enabled_power_domains;
5279 for_each_power_domain(domain, domains)
5280 intel_display_power_put(dev_priv, domain);
5281 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5282 }
5283 }
b04c5bd6
BF
5284}
5285
5286/**
5287 * Sets the power management mode of the pipe and plane.
5288 */
5289void intel_crtc_update_dpms(struct drm_crtc *crtc)
5290{
5291 struct drm_device *dev = crtc->dev;
5292 struct intel_encoder *intel_encoder;
5293 bool enable = false;
5294
5295 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5296 enable |= intel_encoder->connectors_active;
5297
5298 intel_crtc_control(crtc, enable);
976f8a20
DV
5299}
5300
cdd59983
CW
5301static void intel_crtc_disable(struct drm_crtc *crtc)
5302{
cdd59983 5303 struct drm_device *dev = crtc->dev;
976f8a20 5304 struct drm_connector *connector;
ee7b9f93 5305 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 5306
976f8a20
DV
5307 /* crtc should still be enabled when we disable it. */
5308 WARN_ON(!crtc->enabled);
5309
5310 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
5311 dev_priv->display.off(crtc);
5312
455a6808 5313 crtc->primary->funcs->disable_plane(crtc->primary);
976f8a20
DV
5314
5315 /* Update computed state. */
5316 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5317 if (!connector->encoder || !connector->encoder->crtc)
5318 continue;
5319
5320 if (connector->encoder->crtc != crtc)
5321 continue;
5322
5323 connector->dpms = DRM_MODE_DPMS_OFF;
5324 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5325 }
5326}
5327
ea5b213a 5328void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5329{
4ef69c7a 5330 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5331
ea5b213a
CW
5332 drm_encoder_cleanup(encoder);
5333 kfree(intel_encoder);
7e7d76c3
JB
5334}
5335
9237329d 5336/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5337 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5338 * state of the entire output pipe. */
9237329d 5339static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5340{
5ab432ef
DV
5341 if (mode == DRM_MODE_DPMS_ON) {
5342 encoder->connectors_active = true;
5343
b2cabb0e 5344 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5345 } else {
5346 encoder->connectors_active = false;
5347
b2cabb0e 5348 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5349 }
79e53945
JB
5350}
5351
0a91ca29
DV
5352/* Cross check the actual hw state with our own modeset state tracking (and it's
5353 * internal consistency). */
b980514c 5354static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5355{
0a91ca29
DV
5356 if (connector->get_hw_state(connector)) {
5357 struct intel_encoder *encoder = connector->encoder;
5358 struct drm_crtc *crtc;
5359 bool encoder_enabled;
5360 enum pipe pipe;
5361
5362 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5363 connector->base.base.id,
c23cc417 5364 connector->base.name);
0a91ca29 5365
0e32b39c
DA
5366 /* there is no real hw state for MST connectors */
5367 if (connector->mst_port)
5368 return;
5369
e2c719b7 5370 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 5371 "wrong connector dpms state\n");
e2c719b7 5372 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 5373 "active connector not linked to encoder\n");
0a91ca29 5374
36cd7444 5375 if (encoder) {
e2c719b7 5376 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
5377 "encoder->connectors_active not set\n");
5378
5379 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
5380 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5381 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 5382 return;
0a91ca29 5383
36cd7444 5384 crtc = encoder->base.crtc;
0a91ca29 5385
e2c719b7
RC
5386 I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
5387 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5388 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
5389 "encoder active on the wrong pipe\n");
5390 }
0a91ca29 5391 }
79e53945
JB
5392}
5393
5ab432ef
DV
5394/* Even simpler default implementation, if there's really no special case to
5395 * consider. */
5396void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5397{
5ab432ef
DV
5398 /* All the simple cases only support two dpms states. */
5399 if (mode != DRM_MODE_DPMS_ON)
5400 mode = DRM_MODE_DPMS_OFF;
d4270e57 5401
5ab432ef
DV
5402 if (mode == connector->dpms)
5403 return;
5404
5405 connector->dpms = mode;
5406
5407 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5408 if (connector->encoder)
5409 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5410
b980514c 5411 intel_modeset_check_state(connector->dev);
79e53945
JB
5412}
5413
f0947c37
DV
5414/* Simple connector->get_hw_state implementation for encoders that support only
5415 * one connector and no cloning and hence the encoder state determines the state
5416 * of the connector. */
5417bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5418{
24929352 5419 enum pipe pipe = 0;
f0947c37 5420 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5421
f0947c37 5422 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5423}
5424
1857e1da 5425static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 5426 struct intel_crtc_state *pipe_config)
1857e1da
DV
5427{
5428 struct drm_i915_private *dev_priv = dev->dev_private;
5429 struct intel_crtc *pipe_B_crtc =
5430 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5431
5432 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5433 pipe_name(pipe), pipe_config->fdi_lanes);
5434 if (pipe_config->fdi_lanes > 4) {
5435 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5436 pipe_name(pipe), pipe_config->fdi_lanes);
5437 return false;
5438 }
5439
bafb6553 5440 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5441 if (pipe_config->fdi_lanes > 2) {
5442 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5443 pipe_config->fdi_lanes);
5444 return false;
5445 } else {
5446 return true;
5447 }
5448 }
5449
5450 if (INTEL_INFO(dev)->num_pipes == 2)
5451 return true;
5452
5453 /* Ivybridge 3 pipe is really complicated */
5454 switch (pipe) {
5455 case PIPE_A:
5456 return true;
5457 case PIPE_B:
5458 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5459 pipe_config->fdi_lanes > 2) {
5460 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5461 pipe_name(pipe), pipe_config->fdi_lanes);
5462 return false;
5463 }
5464 return true;
5465 case PIPE_C:
1e833f40 5466 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
6e3c9717 5467 pipe_B_crtc->config->fdi_lanes <= 2) {
1857e1da
DV
5468 if (pipe_config->fdi_lanes > 2) {
5469 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5470 pipe_name(pipe), pipe_config->fdi_lanes);
5471 return false;
5472 }
5473 } else {
5474 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5475 return false;
5476 }
5477 return true;
5478 default:
5479 BUG();
5480 }
5481}
5482
e29c22c0
DV
5483#define RETRY 1
5484static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 5485 struct intel_crtc_state *pipe_config)
877d48d5 5486{
1857e1da 5487 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 5488 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
ff9a6750 5489 int lane, link_bw, fdi_dotclock;
e29c22c0 5490 bool setup_ok, needs_recompute = false;
877d48d5 5491
e29c22c0 5492retry:
877d48d5
DV
5493 /* FDI is a binary signal running at ~2.7GHz, encoding
5494 * each output octet as 10 bits. The actual frequency
5495 * is stored as a divider into a 100MHz clock, and the
5496 * mode pixel clock is stored in units of 1KHz.
5497 * Hence the bw of each lane in terms of the mode signal
5498 * is:
5499 */
5500 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5501
241bfc38 5502 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5503
2bd89a07 5504 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5505 pipe_config->pipe_bpp);
5506
5507 pipe_config->fdi_lanes = lane;
5508
2bd89a07 5509 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5510 link_bw, &pipe_config->fdi_m_n);
1857e1da 5511
e29c22c0
DV
5512 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5513 intel_crtc->pipe, pipe_config);
5514 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5515 pipe_config->pipe_bpp -= 2*3;
5516 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5517 pipe_config->pipe_bpp);
5518 needs_recompute = true;
5519 pipe_config->bw_constrained = true;
5520
5521 goto retry;
5522 }
5523
5524 if (needs_recompute)
5525 return RETRY;
5526
5527 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5528}
5529
42db64ef 5530static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 5531 struct intel_crtc_state *pipe_config)
42db64ef 5532{
d330a953 5533 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5534 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5535 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5536}
5537
a43f6e0f 5538static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 5539 struct intel_crtc_state *pipe_config)
79e53945 5540{
a43f6e0f 5541 struct drm_device *dev = crtc->base.dev;
8bd31e67 5542 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 5543 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 5544
ad3a4479 5545 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 5546 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
5547 int clock_limit =
5548 dev_priv->display.get_display_clock_speed(dev);
5549
5550 /*
5551 * Enable pixel doubling when the dot clock
5552 * is > 90% of the (display) core speed.
5553 *
b397c96b
VS
5554 * GDG double wide on either pipe,
5555 * otherwise pipe A only.
cf532bb2 5556 */
b397c96b 5557 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5558 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5559 clock_limit *= 2;
cf532bb2 5560 pipe_config->double_wide = true;
ad3a4479
VS
5561 }
5562
241bfc38 5563 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5564 return -EINVAL;
2c07245f 5565 }
89749350 5566
1d1d0e27
VS
5567 /*
5568 * Pipe horizontal size must be even in:
5569 * - DVO ganged mode
5570 * - LVDS dual channel mode
5571 * - Double wide pipe
5572 */
409ee761 5573 if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
5574 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5575 pipe_config->pipe_src_w &= ~1;
5576
8693a824
DL
5577 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5578 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5579 */
5580 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5581 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5582 return -EINVAL;
44f46b42 5583
bd080ee5 5584 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5585 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5586 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5587 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5588 * for lvds. */
5589 pipe_config->pipe_bpp = 8*3;
5590 }
5591
f5adf94e 5592 if (HAS_IPS(dev))
a43f6e0f
DV
5593 hsw_compute_ips_config(crtc, pipe_config);
5594
877d48d5 5595 if (pipe_config->has_pch_encoder)
a43f6e0f 5596 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5597
e29c22c0 5598 return 0;
79e53945
JB
5599}
5600
25eb05fc
JB
5601static int valleyview_get_display_clock_speed(struct drm_device *dev)
5602{
d197b7d3 5603 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
5604 u32 val;
5605 int divider;
5606
d49a340d
VS
5607 /* FIXME: Punit isn't quite ready yet */
5608 if (IS_CHERRYVIEW(dev))
5609 return 400000;
5610
6bcda4f0
VS
5611 if (dev_priv->hpll_freq == 0)
5612 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5613
d197b7d3
VS
5614 mutex_lock(&dev_priv->dpio_lock);
5615 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5616 mutex_unlock(&dev_priv->dpio_lock);
5617
5618 divider = val & DISPLAY_FREQUENCY_VALUES;
5619
7d007f40
VS
5620 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5621 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5622 "cdclk change in progress\n");
5623
6bcda4f0 5624 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
5625}
5626
e70236a8
JB
5627static int i945_get_display_clock_speed(struct drm_device *dev)
5628{
5629 return 400000;
5630}
79e53945 5631
e70236a8 5632static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5633{
e70236a8
JB
5634 return 333000;
5635}
79e53945 5636
e70236a8
JB
5637static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5638{
5639 return 200000;
5640}
79e53945 5641
257a7ffc
DV
5642static int pnv_get_display_clock_speed(struct drm_device *dev)
5643{
5644 u16 gcfgc = 0;
5645
5646 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5647
5648 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5649 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5650 return 267000;
5651 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5652 return 333000;
5653 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5654 return 444000;
5655 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5656 return 200000;
5657 default:
5658 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5659 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5660 return 133000;
5661 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5662 return 167000;
5663 }
5664}
5665
e70236a8
JB
5666static int i915gm_get_display_clock_speed(struct drm_device *dev)
5667{
5668 u16 gcfgc = 0;
79e53945 5669
e70236a8
JB
5670 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5671
5672 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5673 return 133000;
5674 else {
5675 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5676 case GC_DISPLAY_CLOCK_333_MHZ:
5677 return 333000;
5678 default:
5679 case GC_DISPLAY_CLOCK_190_200_MHZ:
5680 return 190000;
79e53945 5681 }
e70236a8
JB
5682 }
5683}
5684
5685static int i865_get_display_clock_speed(struct drm_device *dev)
5686{
5687 return 266000;
5688}
5689
5690static int i855_get_display_clock_speed(struct drm_device *dev)
5691{
5692 u16 hpllcc = 0;
5693 /* Assume that the hardware is in the high speed state. This
5694 * should be the default.
5695 */
5696 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5697 case GC_CLOCK_133_200:
5698 case GC_CLOCK_100_200:
5699 return 200000;
5700 case GC_CLOCK_166_250:
5701 return 250000;
5702 case GC_CLOCK_100_133:
79e53945 5703 return 133000;
e70236a8 5704 }
79e53945 5705
e70236a8
JB
5706 /* Shouldn't happen */
5707 return 0;
5708}
79e53945 5709
e70236a8
JB
5710static int i830_get_display_clock_speed(struct drm_device *dev)
5711{
5712 return 133000;
79e53945
JB
5713}
5714
2c07245f 5715static void
a65851af 5716intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5717{
a65851af
VS
5718 while (*num > DATA_LINK_M_N_MASK ||
5719 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5720 *num >>= 1;
5721 *den >>= 1;
5722 }
5723}
5724
a65851af
VS
5725static void compute_m_n(unsigned int m, unsigned int n,
5726 uint32_t *ret_m, uint32_t *ret_n)
5727{
5728 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5729 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5730 intel_reduce_m_n_ratio(ret_m, ret_n);
5731}
5732
e69d0bc1
DV
5733void
5734intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5735 int pixel_clock, int link_clock,
5736 struct intel_link_m_n *m_n)
2c07245f 5737{
e69d0bc1 5738 m_n->tu = 64;
a65851af
VS
5739
5740 compute_m_n(bits_per_pixel * pixel_clock,
5741 link_clock * nlanes * 8,
5742 &m_n->gmch_m, &m_n->gmch_n);
5743
5744 compute_m_n(pixel_clock, link_clock,
5745 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5746}
5747
a7615030
CW
5748static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5749{
d330a953
JN
5750 if (i915.panel_use_ssc >= 0)
5751 return i915.panel_use_ssc != 0;
41aa3448 5752 return dev_priv->vbt.lvds_use_ssc
435793df 5753 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5754}
5755
409ee761 5756static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
c65d77d8 5757{
409ee761 5758 struct drm_device *dev = crtc->base.dev;
c65d77d8
JB
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760 int refclk;
5761
a0c4da24 5762 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5763 refclk = 100000;
d0737e1d 5764 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5765 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5766 refclk = dev_priv->vbt.lvds_ssc_freq;
5767 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5768 } else if (!IS_GEN2(dev)) {
5769 refclk = 96000;
5770 } else {
5771 refclk = 48000;
5772 }
5773
5774 return refclk;
5775}
5776
7429e9d4 5777static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5778{
7df00d7a 5779 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5780}
f47709a9 5781
7429e9d4
DV
5782static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5783{
5784 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5785}
5786
f47709a9 5787static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 5788 struct intel_crtc_state *crtc_state,
a7516a05
JB
5789 intel_clock_t *reduced_clock)
5790{
f47709a9 5791 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5792 u32 fp, fp2 = 0;
5793
5794 if (IS_PINEVIEW(dev)) {
190f68c5 5795 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5796 if (reduced_clock)
7429e9d4 5797 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5798 } else {
190f68c5 5799 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5800 if (reduced_clock)
7429e9d4 5801 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5802 }
5803
190f68c5 5804 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 5805
f47709a9 5806 crtc->lowfreq_avail = false;
e1f234bd 5807 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
d330a953 5808 reduced_clock && i915.powersave) {
190f68c5 5809 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 5810 crtc->lowfreq_avail = true;
a7516a05 5811 } else {
190f68c5 5812 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
5813 }
5814}
5815
5e69f97f
CML
5816static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5817 pipe)
89b667f8
JB
5818{
5819 u32 reg_val;
5820
5821 /*
5822 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5823 * and set it to a reasonable value instead.
5824 */
ab3c759a 5825 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5826 reg_val &= 0xffffff00;
5827 reg_val |= 0x00000030;
ab3c759a 5828 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5829
ab3c759a 5830 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5831 reg_val &= 0x8cffffff;
5832 reg_val = 0x8c000000;
ab3c759a 5833 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5834
ab3c759a 5835 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5836 reg_val &= 0xffffff00;
ab3c759a 5837 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5838
ab3c759a 5839 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5840 reg_val &= 0x00ffffff;
5841 reg_val |= 0xb0000000;
ab3c759a 5842 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5843}
5844
b551842d
DV
5845static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5846 struct intel_link_m_n *m_n)
5847{
5848 struct drm_device *dev = crtc->base.dev;
5849 struct drm_i915_private *dev_priv = dev->dev_private;
5850 int pipe = crtc->pipe;
5851
e3b95f1e
DV
5852 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5853 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5854 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5855 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5856}
5857
5858static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5859 struct intel_link_m_n *m_n,
5860 struct intel_link_m_n *m2_n2)
b551842d
DV
5861{
5862 struct drm_device *dev = crtc->base.dev;
5863 struct drm_i915_private *dev_priv = dev->dev_private;
5864 int pipe = crtc->pipe;
6e3c9717 5865 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
5866
5867 if (INTEL_INFO(dev)->gen >= 5) {
5868 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5869 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5870 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5871 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5872 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5873 * for gen < 8) and if DRRS is supported (to make sure the
5874 * registers are not unnecessarily accessed).
5875 */
5876 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 5877 crtc->config->has_drrs) {
f769cd24
VK
5878 I915_WRITE(PIPE_DATA_M2(transcoder),
5879 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5880 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5881 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5882 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5883 }
b551842d 5884 } else {
e3b95f1e
DV
5885 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5886 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5887 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5888 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5889 }
5890}
5891
f769cd24 5892void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2 5893{
6e3c9717
ACO
5894 if (crtc->config->has_pch_encoder)
5895 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 5896 else
6e3c9717
ACO
5897 intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
5898 &crtc->config->dp_m2_n2);
03afc4a2
DV
5899}
5900
d288f65f 5901static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 5902 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
5903{
5904 u32 dpll, dpll_md;
5905
5906 /*
5907 * Enable DPIO clock input. We should never disable the reference
5908 * clock for pipe B, since VGA hotplug / manual detection depends
5909 * on it.
5910 */
5911 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5912 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5913 /* We should never disable this, set it here for state tracking */
5914 if (crtc->pipe == PIPE_B)
5915 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5916 dpll |= DPLL_VCO_ENABLE;
d288f65f 5917 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 5918
d288f65f 5919 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 5920 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 5921 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
5922}
5923
d288f65f 5924static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 5925 const struct intel_crtc_state *pipe_config)
a0c4da24 5926{
f47709a9 5927 struct drm_device *dev = crtc->base.dev;
a0c4da24 5928 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5929 int pipe = crtc->pipe;
bdd4b6a6 5930 u32 mdiv;
a0c4da24 5931 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5932 u32 coreclk, reg_val;
a0c4da24 5933
09153000
DV
5934 mutex_lock(&dev_priv->dpio_lock);
5935
d288f65f
VS
5936 bestn = pipe_config->dpll.n;
5937 bestm1 = pipe_config->dpll.m1;
5938 bestm2 = pipe_config->dpll.m2;
5939 bestp1 = pipe_config->dpll.p1;
5940 bestp2 = pipe_config->dpll.p2;
a0c4da24 5941
89b667f8
JB
5942 /* See eDP HDMI DPIO driver vbios notes doc */
5943
5944 /* PLL B needs special handling */
bdd4b6a6 5945 if (pipe == PIPE_B)
5e69f97f 5946 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5947
5948 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5950
5951 /* Disable target IRef on PLL */
ab3c759a 5952 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5953 reg_val &= 0x00ffffff;
ab3c759a 5954 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5955
5956 /* Disable fast lock */
ab3c759a 5957 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5958
5959 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5960 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5961 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5962 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5963 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5964
5965 /*
5966 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5967 * but we don't support that).
5968 * Note: don't use the DAC post divider as it seems unstable.
5969 */
5970 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5972
a0c4da24 5973 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5974 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5975
89b667f8 5976 /* Set HBR and RBR LPF coefficients */
d288f65f 5977 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
5978 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
5979 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 5980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5981 0x009f0003);
89b667f8 5982 else
ab3c759a 5983 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5984 0x00d0000f);
5985
681a8504 5986 if (pipe_config->has_dp_encoder) {
89b667f8 5987 /* Use SSC source */
bdd4b6a6 5988 if (pipe == PIPE_A)
ab3c759a 5989 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5990 0x0df40000);
5991 else
ab3c759a 5992 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5993 0x0df70000);
5994 } else { /* HDMI or VGA */
5995 /* Use bend source */
bdd4b6a6 5996 if (pipe == PIPE_A)
ab3c759a 5997 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5998 0x0df70000);
5999 else
ab3c759a 6000 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6001 0x0df40000);
6002 }
a0c4da24 6003
ab3c759a 6004 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6005 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6006 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6007 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6008 coreclk |= 0x01000000;
ab3c759a 6009 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6010
ab3c759a 6011 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6012 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6013}
6014
d288f65f 6015static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6016 struct intel_crtc_state *pipe_config)
1ae0d137 6017{
d288f65f 6018 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6019 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6020 DPLL_VCO_ENABLE;
6021 if (crtc->pipe != PIPE_A)
d288f65f 6022 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6023
d288f65f
VS
6024 pipe_config->dpll_hw_state.dpll_md =
6025 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6026}
6027
d288f65f 6028static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6029 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6030{
6031 struct drm_device *dev = crtc->base.dev;
6032 struct drm_i915_private *dev_priv = dev->dev_private;
6033 int pipe = crtc->pipe;
6034 int dpll_reg = DPLL(crtc->pipe);
6035 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 6036 u32 loopfilter, intcoeff;
9d556c99
CML
6037 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
6038 int refclk;
6039
d288f65f
VS
6040 bestn = pipe_config->dpll.n;
6041 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6042 bestm1 = pipe_config->dpll.m1;
6043 bestm2 = pipe_config->dpll.m2 >> 22;
6044 bestp1 = pipe_config->dpll.p1;
6045 bestp2 = pipe_config->dpll.p2;
9d556c99
CML
6046
6047 /*
6048 * Enable Refclk and SSC
6049 */
a11b0703 6050 I915_WRITE(dpll_reg,
d288f65f 6051 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
6052
6053 mutex_lock(&dev_priv->dpio_lock);
9d556c99 6054
9d556c99
CML
6055 /* p1 and p2 divider */
6056 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6057 5 << DPIO_CHV_S1_DIV_SHIFT |
6058 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6059 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6060 1 << DPIO_CHV_K_DIV_SHIFT);
6061
6062 /* Feedback post-divider - m2 */
6063 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6064
6065 /* Feedback refclk divider - n and m1 */
6066 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6067 DPIO_CHV_M1_DIV_BY_2 |
6068 1 << DPIO_CHV_N_DIV_SHIFT);
6069
6070 /* M2 fraction division */
6071 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
6072
6073 /* M2 fraction division enable */
6074 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
6075 DPIO_CHV_FRAC_DIV_EN |
6076 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
6077
6078 /* Loop filter */
409ee761 6079 refclk = i9xx_get_refclk(crtc, 0);
9d556c99
CML
6080 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
6081 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
6082 if (refclk == 100000)
6083 intcoeff = 11;
6084 else if (refclk == 38400)
6085 intcoeff = 10;
6086 else
6087 intcoeff = 9;
6088 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
6089 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6090
6091 /* AFC Recal */
6092 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6093 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6094 DPIO_AFC_RECAL);
6095
6096 mutex_unlock(&dev_priv->dpio_lock);
6097}
6098
d288f65f
VS
6099/**
6100 * vlv_force_pll_on - forcibly enable just the PLL
6101 * @dev_priv: i915 private structure
6102 * @pipe: pipe PLL to enable
6103 * @dpll: PLL configuration
6104 *
6105 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6106 * in cases where we need the PLL enabled even when @pipe is not going to
6107 * be enabled.
6108 */
6109void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6110 const struct dpll *dpll)
6111{
6112 struct intel_crtc *crtc =
6113 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 6114 struct intel_crtc_state pipe_config = {
d288f65f
VS
6115 .pixel_multiplier = 1,
6116 .dpll = *dpll,
6117 };
6118
6119 if (IS_CHERRYVIEW(dev)) {
6120 chv_update_pll(crtc, &pipe_config);
6121 chv_prepare_pll(crtc, &pipe_config);
6122 chv_enable_pll(crtc, &pipe_config);
6123 } else {
6124 vlv_update_pll(crtc, &pipe_config);
6125 vlv_prepare_pll(crtc, &pipe_config);
6126 vlv_enable_pll(crtc, &pipe_config);
6127 }
6128}
6129
6130/**
6131 * vlv_force_pll_off - forcibly disable just the PLL
6132 * @dev_priv: i915 private structure
6133 * @pipe: pipe PLL to disable
6134 *
6135 * Disable the PLL for @pipe. To be used in cases where we need
6136 * the PLL enabled even when @pipe is not going to be enabled.
6137 */
6138void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6139{
6140 if (IS_CHERRYVIEW(dev))
6141 chv_disable_pll(to_i915(dev), pipe);
6142 else
6143 vlv_disable_pll(to_i915(dev), pipe);
6144}
6145
f47709a9 6146static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 6147 struct intel_crtc_state *crtc_state,
f47709a9 6148 intel_clock_t *reduced_clock,
eb1cbe48
DV
6149 int num_connectors)
6150{
f47709a9 6151 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6152 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
6153 u32 dpll;
6154 bool is_sdvo;
190f68c5 6155 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6156
190f68c5 6157 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6158
d0737e1d
ACO
6159 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6160 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
6161
6162 dpll = DPLL_VGA_MODE_DIS;
6163
d0737e1d 6164 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6165 dpll |= DPLLB_MODE_LVDS;
6166 else
6167 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6168
ef1b460d 6169 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 6170 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6171 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6172 }
198a037f
DV
6173
6174 if (is_sdvo)
4a33e48d 6175 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6176
190f68c5 6177 if (crtc_state->has_dp_encoder)
4a33e48d 6178 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6179
6180 /* compute bitmask from p1 value */
6181 if (IS_PINEVIEW(dev))
6182 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6183 else {
6184 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6185 if (IS_G4X(dev) && reduced_clock)
6186 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6187 }
6188 switch (clock->p2) {
6189 case 5:
6190 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6191 break;
6192 case 7:
6193 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6194 break;
6195 case 10:
6196 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6197 break;
6198 case 14:
6199 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6200 break;
6201 }
6202 if (INTEL_INFO(dev)->gen >= 4)
6203 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6204
190f68c5 6205 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6206 dpll |= PLL_REF_INPUT_TVCLKINBC;
d0737e1d 6207 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6208 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6209 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6210 else
6211 dpll |= PLL_REF_INPUT_DREFCLK;
6212
6213 dpll |= DPLL_VCO_ENABLE;
190f68c5 6214 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6215
eb1cbe48 6216 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 6217 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6218 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6219 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6220 }
6221}
6222
f47709a9 6223static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 6224 struct intel_crtc_state *crtc_state,
f47709a9 6225 intel_clock_t *reduced_clock,
eb1cbe48
DV
6226 int num_connectors)
6227{
f47709a9 6228 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6229 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6230 u32 dpll;
190f68c5 6231 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6232
190f68c5 6233 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6234
eb1cbe48
DV
6235 dpll = DPLL_VGA_MODE_DIS;
6236
d0737e1d 6237 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6238 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6239 } else {
6240 if (clock->p1 == 2)
6241 dpll |= PLL_P1_DIVIDE_BY_TWO;
6242 else
6243 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6244 if (clock->p2 == 4)
6245 dpll |= PLL_P2_DIVIDE_BY_4;
6246 }
6247
d0737e1d 6248 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
4a33e48d
DV
6249 dpll |= DPLL_DVO_2X_MODE;
6250
d0737e1d 6251 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6252 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6253 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6254 else
6255 dpll |= PLL_REF_INPUT_DREFCLK;
6256
6257 dpll |= DPLL_VCO_ENABLE;
190f68c5 6258 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6259}
6260
8a654f3b 6261static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6262{
6263 struct drm_device *dev = intel_crtc->base.dev;
6264 struct drm_i915_private *dev_priv = dev->dev_private;
6265 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6266 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 6267 struct drm_display_mode *adjusted_mode =
6e3c9717 6268 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6269 uint32_t crtc_vtotal, crtc_vblank_end;
6270 int vsyncshift = 0;
4d8a62ea
DV
6271
6272 /* We need to be careful not to changed the adjusted mode, for otherwise
6273 * the hw state checker will get angry at the mismatch. */
6274 crtc_vtotal = adjusted_mode->crtc_vtotal;
6275 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6276
609aeaca 6277 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6278 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6279 crtc_vtotal -= 1;
6280 crtc_vblank_end -= 1;
609aeaca 6281
409ee761 6282 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
6283 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6284 else
6285 vsyncshift = adjusted_mode->crtc_hsync_start -
6286 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6287 if (vsyncshift < 0)
6288 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6289 }
6290
6291 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6292 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6293
fe2b8f9d 6294 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6295 (adjusted_mode->crtc_hdisplay - 1) |
6296 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6297 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6298 (adjusted_mode->crtc_hblank_start - 1) |
6299 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6300 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6301 (adjusted_mode->crtc_hsync_start - 1) |
6302 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6303
fe2b8f9d 6304 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6305 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6306 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6307 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6308 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6309 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6310 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6311 (adjusted_mode->crtc_vsync_start - 1) |
6312 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6313
b5e508d4
PZ
6314 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6315 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6316 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6317 * bits. */
6318 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6319 (pipe == PIPE_B || pipe == PIPE_C))
6320 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6321
b0e77b9c
PZ
6322 /* pipesrc controls the size that is scaled from, which should
6323 * always be the user's requested size.
6324 */
6325 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6326 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6327 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6328}
6329
1bd1bd80 6330static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6331 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6332{
6333 struct drm_device *dev = crtc->base.dev;
6334 struct drm_i915_private *dev_priv = dev->dev_private;
6335 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6336 uint32_t tmp;
6337
6338 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6339 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6340 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6341 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6342 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6343 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6344 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6345 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6346 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6347
6348 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6349 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6350 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6351 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6352 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6353 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6354 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6355 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6356 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6357
6358 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6359 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6360 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6361 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
6362 }
6363
6364 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6365 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6366 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6367
2d112de7
ACO
6368 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6369 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6370}
6371
f6a83288 6372void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 6373 struct intel_crtc_state *pipe_config)
babea61d 6374{
2d112de7
ACO
6375 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6376 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6377 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6378 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 6379
2d112de7
ACO
6380 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6381 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6382 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6383 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 6384
2d112de7 6385 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 6386
2d112de7
ACO
6387 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6388 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
6389}
6390
84b046f3
DV
6391static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6392{
6393 struct drm_device *dev = intel_crtc->base.dev;
6394 struct drm_i915_private *dev_priv = dev->dev_private;
6395 uint32_t pipeconf;
6396
9f11a9e4 6397 pipeconf = 0;
84b046f3 6398
b6b5d049
VS
6399 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6400 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6401 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6402
6e3c9717 6403 if (intel_crtc->config->double_wide)
cf532bb2 6404 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6405
ff9ce46e
DV
6406 /* only g4x and later have fancy bpc/dither controls */
6407 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 6408 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 6409 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 6410 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6411 PIPECONF_DITHER_TYPE_SP;
84b046f3 6412
6e3c9717 6413 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
6414 case 18:
6415 pipeconf |= PIPECONF_6BPC;
6416 break;
6417 case 24:
6418 pipeconf |= PIPECONF_8BPC;
6419 break;
6420 case 30:
6421 pipeconf |= PIPECONF_10BPC;
6422 break;
6423 default:
6424 /* Case prevented by intel_choose_pipe_bpp_dither. */
6425 BUG();
84b046f3
DV
6426 }
6427 }
6428
6429 if (HAS_PIPE_CXSR(dev)) {
6430 if (intel_crtc->lowfreq_avail) {
6431 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6432 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6433 } else {
6434 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6435 }
6436 }
6437
6e3c9717 6438 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 6439 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 6440 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
6441 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6442 else
6443 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6444 } else
84b046f3
DV
6445 pipeconf |= PIPECONF_PROGRESSIVE;
6446
6e3c9717 6447 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 6448 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6449
84b046f3
DV
6450 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6451 POSTING_READ(PIPECONF(intel_crtc->pipe));
6452}
6453
190f68c5
ACO
6454static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6455 struct intel_crtc_state *crtc_state)
79e53945 6456{
c7653199 6457 struct drm_device *dev = crtc->base.dev;
79e53945 6458 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 6459 int refclk, num_connectors = 0;
652c393a 6460 intel_clock_t clock, reduced_clock;
a16af721 6461 bool ok, has_reduced_clock = false;
e9fd1c02 6462 bool is_lvds = false, is_dsi = false;
5eddb70b 6463 struct intel_encoder *encoder;
d4906093 6464 const intel_limit_t *limit;
79e53945 6465
d0737e1d
ACO
6466 for_each_intel_encoder(dev, encoder) {
6467 if (encoder->new_crtc != crtc)
6468 continue;
6469
5eddb70b 6470 switch (encoder->type) {
79e53945
JB
6471 case INTEL_OUTPUT_LVDS:
6472 is_lvds = true;
6473 break;
e9fd1c02
JN
6474 case INTEL_OUTPUT_DSI:
6475 is_dsi = true;
6476 break;
6847d71b
PZ
6477 default:
6478 break;
79e53945 6479 }
43565a06 6480
c751ce4f 6481 num_connectors++;
79e53945
JB
6482 }
6483
f2335330 6484 if (is_dsi)
5b18e57c 6485 return 0;
f2335330 6486
190f68c5 6487 if (!crtc_state->clock_set) {
409ee761 6488 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6489
e9fd1c02
JN
6490 /*
6491 * Returns a set of divisors for the desired target clock with
6492 * the given refclk, or FALSE. The returned values represent
6493 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6494 * 2) / p1 / p2.
6495 */
409ee761 6496 limit = intel_limit(crtc, refclk);
c7653199 6497 ok = dev_priv->display.find_dpll(limit, crtc,
190f68c5 6498 crtc_state->port_clock,
e9fd1c02 6499 refclk, NULL, &clock);
f2335330 6500 if (!ok) {
e9fd1c02
JN
6501 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6502 return -EINVAL;
6503 }
79e53945 6504
f2335330
JN
6505 if (is_lvds && dev_priv->lvds_downclock_avail) {
6506 /*
6507 * Ensure we match the reduced clock's P to the target
6508 * clock. If the clocks don't match, we can't switch
6509 * the display clock by using the FP0/FP1. In such case
6510 * we will disable the LVDS downclock feature.
6511 */
6512 has_reduced_clock =
c7653199 6513 dev_priv->display.find_dpll(limit, crtc,
f2335330
JN
6514 dev_priv->lvds_downclock,
6515 refclk, &clock,
6516 &reduced_clock);
6517 }
6518 /* Compat-code for transition, will disappear. */
190f68c5
ACO
6519 crtc_state->dpll.n = clock.n;
6520 crtc_state->dpll.m1 = clock.m1;
6521 crtc_state->dpll.m2 = clock.m2;
6522 crtc_state->dpll.p1 = clock.p1;
6523 crtc_state->dpll.p2 = clock.p2;
f47709a9 6524 }
7026d4ac 6525
e9fd1c02 6526 if (IS_GEN2(dev)) {
190f68c5 6527 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
6528 has_reduced_clock ? &reduced_clock : NULL,
6529 num_connectors);
9d556c99 6530 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 6531 chv_update_pll(crtc, crtc_state);
e9fd1c02 6532 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 6533 vlv_update_pll(crtc, crtc_state);
e9fd1c02 6534 } else {
190f68c5 6535 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 6536 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6537 num_connectors);
e9fd1c02 6538 }
79e53945 6539
c8f7a0db 6540 return 0;
f564048e
EA
6541}
6542
2fa2fe9a 6543static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 6544 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
6545{
6546 struct drm_device *dev = crtc->base.dev;
6547 struct drm_i915_private *dev_priv = dev->dev_private;
6548 uint32_t tmp;
6549
dc9e7dec
VS
6550 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6551 return;
6552
2fa2fe9a 6553 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6554 if (!(tmp & PFIT_ENABLE))
6555 return;
2fa2fe9a 6556
06922821 6557 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6558 if (INTEL_INFO(dev)->gen < 4) {
6559 if (crtc->pipe != PIPE_B)
6560 return;
2fa2fe9a
DV
6561 } else {
6562 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6563 return;
6564 }
6565
06922821 6566 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6567 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6568 if (INTEL_INFO(dev)->gen < 5)
6569 pipe_config->gmch_pfit.lvds_border_bits =
6570 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6571}
6572
acbec814 6573static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6574 struct intel_crtc_state *pipe_config)
acbec814
JB
6575{
6576 struct drm_device *dev = crtc->base.dev;
6577 struct drm_i915_private *dev_priv = dev->dev_private;
6578 int pipe = pipe_config->cpu_transcoder;
6579 intel_clock_t clock;
6580 u32 mdiv;
662c6ecb 6581 int refclk = 100000;
acbec814 6582
f573de5a
SK
6583 /* In case of MIPI DPLL will not even be used */
6584 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6585 return;
6586
acbec814 6587 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6588 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6589 mutex_unlock(&dev_priv->dpio_lock);
6590
6591 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6592 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6593 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6594 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6595 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6596
f646628b 6597 vlv_clock(refclk, &clock);
acbec814 6598
f646628b
VS
6599 /* clock.dot is the fast clock */
6600 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6601}
6602
5724dbd1
DL
6603static void
6604i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6605 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
6606{
6607 struct drm_device *dev = crtc->base.dev;
6608 struct drm_i915_private *dev_priv = dev->dev_private;
6609 u32 val, base, offset;
6610 int pipe = crtc->pipe, plane = crtc->plane;
6611 int fourcc, pixel_format;
6612 int aligned_height;
b113d5ee 6613 struct drm_framebuffer *fb;
1b842c89 6614 struct intel_framebuffer *intel_fb;
1ad292b5 6615
42a7b088
DL
6616 val = I915_READ(DSPCNTR(plane));
6617 if (!(val & DISPLAY_PLANE_ENABLE))
6618 return;
6619
d9806c9f 6620 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 6621 if (!intel_fb) {
1ad292b5
JB
6622 DRM_DEBUG_KMS("failed to alloc fb\n");
6623 return;
6624 }
6625
1b842c89
DL
6626 fb = &intel_fb->base;
6627
1ad292b5
JB
6628 if (INTEL_INFO(dev)->gen >= 4)
6629 if (val & DISPPLANE_TILED)
49af449b 6630 plane_config->tiling = I915_TILING_X;
1ad292b5
JB
6631
6632 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 6633 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
6634 fb->pixel_format = fourcc;
6635 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
6636
6637 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 6638 if (plane_config->tiling)
1ad292b5
JB
6639 offset = I915_READ(DSPTILEOFF(plane));
6640 else
6641 offset = I915_READ(DSPLINOFF(plane));
6642 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6643 } else {
6644 base = I915_READ(DSPADDR(plane));
6645 }
6646 plane_config->base = base;
6647
6648 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
6649 fb->width = ((val >> 16) & 0xfff) + 1;
6650 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6651
6652 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 6653 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6654
b113d5ee 6655 aligned_height = intel_fb_align_height(dev, fb->height,
ec2c981e 6656 plane_config->tiling);
1ad292b5 6657
b113d5ee 6658 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
1ad292b5 6659
2844a921
DL
6660 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6661 pipe_name(pipe), plane, fb->width, fb->height,
6662 fb->bits_per_pixel, base, fb->pitches[0],
6663 plane_config->size);
1ad292b5 6664
2d14030b 6665 plane_config->fb = intel_fb;
1ad292b5
JB
6666}
6667
70b23a98 6668static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6669 struct intel_crtc_state *pipe_config)
70b23a98
VS
6670{
6671 struct drm_device *dev = crtc->base.dev;
6672 struct drm_i915_private *dev_priv = dev->dev_private;
6673 int pipe = pipe_config->cpu_transcoder;
6674 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6675 intel_clock_t clock;
6676 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6677 int refclk = 100000;
6678
6679 mutex_lock(&dev_priv->dpio_lock);
6680 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6681 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6682 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6683 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6684 mutex_unlock(&dev_priv->dpio_lock);
6685
6686 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6687 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6688 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6689 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6690 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6691
6692 chv_clock(refclk, &clock);
6693
6694 /* clock.dot is the fast clock */
6695 pipe_config->port_clock = clock.dot / 5;
6696}
6697
0e8ffe1b 6698static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 6699 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
6700{
6701 struct drm_device *dev = crtc->base.dev;
6702 struct drm_i915_private *dev_priv = dev->dev_private;
6703 uint32_t tmp;
6704
f458ebbc
DV
6705 if (!intel_display_power_is_enabled(dev_priv,
6706 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
6707 return false;
6708
e143a21c 6709 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6710 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6711
0e8ffe1b
DV
6712 tmp = I915_READ(PIPECONF(crtc->pipe));
6713 if (!(tmp & PIPECONF_ENABLE))
6714 return false;
6715
42571aef
VS
6716 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6717 switch (tmp & PIPECONF_BPC_MASK) {
6718 case PIPECONF_6BPC:
6719 pipe_config->pipe_bpp = 18;
6720 break;
6721 case PIPECONF_8BPC:
6722 pipe_config->pipe_bpp = 24;
6723 break;
6724 case PIPECONF_10BPC:
6725 pipe_config->pipe_bpp = 30;
6726 break;
6727 default:
6728 break;
6729 }
6730 }
6731
b5a9fa09
DV
6732 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6733 pipe_config->limited_color_range = true;
6734
282740f7
VS
6735 if (INTEL_INFO(dev)->gen < 4)
6736 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6737
1bd1bd80
DV
6738 intel_get_pipe_timings(crtc, pipe_config);
6739
2fa2fe9a
DV
6740 i9xx_get_pfit_config(crtc, pipe_config);
6741
6c49f241
DV
6742 if (INTEL_INFO(dev)->gen >= 4) {
6743 tmp = I915_READ(DPLL_MD(crtc->pipe));
6744 pipe_config->pixel_multiplier =
6745 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6746 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6747 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6748 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6749 tmp = I915_READ(DPLL(crtc->pipe));
6750 pipe_config->pixel_multiplier =
6751 ((tmp & SDVO_MULTIPLIER_MASK)
6752 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6753 } else {
6754 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6755 * port and will be fixed up in the encoder->get_config
6756 * function. */
6757 pipe_config->pixel_multiplier = 1;
6758 }
8bcc2795
DV
6759 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6760 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6761 /*
6762 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6763 * on 830. Filter it out here so that we don't
6764 * report errors due to that.
6765 */
6766 if (IS_I830(dev))
6767 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6768
8bcc2795
DV
6769 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6770 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6771 } else {
6772 /* Mask out read-only status bits. */
6773 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6774 DPLL_PORTC_READY_MASK |
6775 DPLL_PORTB_READY_MASK);
8bcc2795 6776 }
6c49f241 6777
70b23a98
VS
6778 if (IS_CHERRYVIEW(dev))
6779 chv_crtc_clock_get(crtc, pipe_config);
6780 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6781 vlv_crtc_clock_get(crtc, pipe_config);
6782 else
6783 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6784
0e8ffe1b
DV
6785 return true;
6786}
6787
dde86e2d 6788static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6789{
6790 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6791 struct intel_encoder *encoder;
74cfd7ac 6792 u32 val, final;
13d83a67 6793 bool has_lvds = false;
199e5d79 6794 bool has_cpu_edp = false;
199e5d79 6795 bool has_panel = false;
99eb6a01
KP
6796 bool has_ck505 = false;
6797 bool can_ssc = false;
13d83a67
JB
6798
6799 /* We need to take the global config into account */
b2784e15 6800 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6801 switch (encoder->type) {
6802 case INTEL_OUTPUT_LVDS:
6803 has_panel = true;
6804 has_lvds = true;
6805 break;
6806 case INTEL_OUTPUT_EDP:
6807 has_panel = true;
2de6905f 6808 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6809 has_cpu_edp = true;
6810 break;
6847d71b
PZ
6811 default:
6812 break;
13d83a67
JB
6813 }
6814 }
6815
99eb6a01 6816 if (HAS_PCH_IBX(dev)) {
41aa3448 6817 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6818 can_ssc = has_ck505;
6819 } else {
6820 has_ck505 = false;
6821 can_ssc = true;
6822 }
6823
2de6905f
ID
6824 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6825 has_panel, has_lvds, has_ck505);
13d83a67
JB
6826
6827 /* Ironlake: try to setup display ref clock before DPLL
6828 * enabling. This is only under driver's control after
6829 * PCH B stepping, previous chipset stepping should be
6830 * ignoring this setting.
6831 */
74cfd7ac
CW
6832 val = I915_READ(PCH_DREF_CONTROL);
6833
6834 /* As we must carefully and slowly disable/enable each source in turn,
6835 * compute the final state we want first and check if we need to
6836 * make any changes at all.
6837 */
6838 final = val;
6839 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6840 if (has_ck505)
6841 final |= DREF_NONSPREAD_CK505_ENABLE;
6842 else
6843 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6844
6845 final &= ~DREF_SSC_SOURCE_MASK;
6846 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6847 final &= ~DREF_SSC1_ENABLE;
6848
6849 if (has_panel) {
6850 final |= DREF_SSC_SOURCE_ENABLE;
6851
6852 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6853 final |= DREF_SSC1_ENABLE;
6854
6855 if (has_cpu_edp) {
6856 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6857 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6858 else
6859 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6860 } else
6861 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6862 } else {
6863 final |= DREF_SSC_SOURCE_DISABLE;
6864 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6865 }
6866
6867 if (final == val)
6868 return;
6869
13d83a67 6870 /* Always enable nonspread source */
74cfd7ac 6871 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6872
99eb6a01 6873 if (has_ck505)
74cfd7ac 6874 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6875 else
74cfd7ac 6876 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6877
199e5d79 6878 if (has_panel) {
74cfd7ac
CW
6879 val &= ~DREF_SSC_SOURCE_MASK;
6880 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6881
199e5d79 6882 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6883 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6884 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6885 val |= DREF_SSC1_ENABLE;
e77166b5 6886 } else
74cfd7ac 6887 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6888
6889 /* Get SSC going before enabling the outputs */
74cfd7ac 6890 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6891 POSTING_READ(PCH_DREF_CONTROL);
6892 udelay(200);
6893
74cfd7ac 6894 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6895
6896 /* Enable CPU source on CPU attached eDP */
199e5d79 6897 if (has_cpu_edp) {
99eb6a01 6898 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6899 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6900 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6901 } else
74cfd7ac 6902 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6903 } else
74cfd7ac 6904 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6905
74cfd7ac 6906 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6907 POSTING_READ(PCH_DREF_CONTROL);
6908 udelay(200);
6909 } else {
6910 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6911
74cfd7ac 6912 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6913
6914 /* Turn off CPU output */
74cfd7ac 6915 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6916
74cfd7ac 6917 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6918 POSTING_READ(PCH_DREF_CONTROL);
6919 udelay(200);
6920
6921 /* Turn off the SSC source */
74cfd7ac
CW
6922 val &= ~DREF_SSC_SOURCE_MASK;
6923 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6924
6925 /* Turn off SSC1 */
74cfd7ac 6926 val &= ~DREF_SSC1_ENABLE;
199e5d79 6927
74cfd7ac 6928 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6929 POSTING_READ(PCH_DREF_CONTROL);
6930 udelay(200);
6931 }
74cfd7ac
CW
6932
6933 BUG_ON(val != final);
13d83a67
JB
6934}
6935
f31f2d55 6936static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6937{
f31f2d55 6938 uint32_t tmp;
dde86e2d 6939
0ff066a9
PZ
6940 tmp = I915_READ(SOUTH_CHICKEN2);
6941 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6942 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6943
0ff066a9
PZ
6944 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6945 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6946 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6947
0ff066a9
PZ
6948 tmp = I915_READ(SOUTH_CHICKEN2);
6949 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6950 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6951
0ff066a9
PZ
6952 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6953 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6954 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6955}
6956
6957/* WaMPhyProgramming:hsw */
6958static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6959{
6960 uint32_t tmp;
dde86e2d
PZ
6961
6962 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6963 tmp &= ~(0xFF << 24);
6964 tmp |= (0x12 << 24);
6965 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6966
dde86e2d
PZ
6967 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6968 tmp |= (1 << 11);
6969 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6970
6971 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6972 tmp |= (1 << 11);
6973 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6974
dde86e2d
PZ
6975 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6976 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6977 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6978
6979 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6980 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6981 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6982
0ff066a9
PZ
6983 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6984 tmp &= ~(7 << 13);
6985 tmp |= (5 << 13);
6986 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6987
0ff066a9
PZ
6988 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6989 tmp &= ~(7 << 13);
6990 tmp |= (5 << 13);
6991 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6992
6993 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6994 tmp &= ~0xFF;
6995 tmp |= 0x1C;
6996 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6997
6998 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6999 tmp &= ~0xFF;
7000 tmp |= 0x1C;
7001 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7002
7003 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7004 tmp &= ~(0xFF << 16);
7005 tmp |= (0x1C << 16);
7006 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7007
7008 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7009 tmp &= ~(0xFF << 16);
7010 tmp |= (0x1C << 16);
7011 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7012
0ff066a9
PZ
7013 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7014 tmp |= (1 << 27);
7015 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7016
0ff066a9
PZ
7017 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7018 tmp |= (1 << 27);
7019 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7020
0ff066a9
PZ
7021 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7022 tmp &= ~(0xF << 28);
7023 tmp |= (4 << 28);
7024 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7025
0ff066a9
PZ
7026 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7027 tmp &= ~(0xF << 28);
7028 tmp |= (4 << 28);
7029 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7030}
7031
2fa86a1f
PZ
7032/* Implements 3 different sequences from BSpec chapter "Display iCLK
7033 * Programming" based on the parameters passed:
7034 * - Sequence to enable CLKOUT_DP
7035 * - Sequence to enable CLKOUT_DP without spread
7036 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7037 */
7038static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7039 bool with_fdi)
f31f2d55
PZ
7040{
7041 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
7042 uint32_t reg, tmp;
7043
7044 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7045 with_spread = true;
7046 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7047 with_fdi, "LP PCH doesn't have FDI\n"))
7048 with_fdi = false;
f31f2d55
PZ
7049
7050 mutex_lock(&dev_priv->dpio_lock);
7051
7052 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7053 tmp &= ~SBI_SSCCTL_DISABLE;
7054 tmp |= SBI_SSCCTL_PATHALT;
7055 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7056
7057 udelay(24);
7058
2fa86a1f
PZ
7059 if (with_spread) {
7060 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7061 tmp &= ~SBI_SSCCTL_PATHALT;
7062 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7063
2fa86a1f
PZ
7064 if (with_fdi) {
7065 lpt_reset_fdi_mphy(dev_priv);
7066 lpt_program_fdi_mphy(dev_priv);
7067 }
7068 }
dde86e2d 7069
2fa86a1f
PZ
7070 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7071 SBI_GEN0 : SBI_DBUFF0;
7072 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7073 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7074 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
7075
7076 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
7077}
7078
47701c3b
PZ
7079/* Sequence to disable CLKOUT_DP */
7080static void lpt_disable_clkout_dp(struct drm_device *dev)
7081{
7082 struct drm_i915_private *dev_priv = dev->dev_private;
7083 uint32_t reg, tmp;
7084
7085 mutex_lock(&dev_priv->dpio_lock);
7086
7087 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7088 SBI_GEN0 : SBI_DBUFF0;
7089 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7090 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7091 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7092
7093 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7094 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7095 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7096 tmp |= SBI_SSCCTL_PATHALT;
7097 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7098 udelay(32);
7099 }
7100 tmp |= SBI_SSCCTL_DISABLE;
7101 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7102 }
7103
7104 mutex_unlock(&dev_priv->dpio_lock);
7105}
7106
bf8fa3d3
PZ
7107static void lpt_init_pch_refclk(struct drm_device *dev)
7108{
bf8fa3d3
PZ
7109 struct intel_encoder *encoder;
7110 bool has_vga = false;
7111
b2784e15 7112 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
7113 switch (encoder->type) {
7114 case INTEL_OUTPUT_ANALOG:
7115 has_vga = true;
7116 break;
6847d71b
PZ
7117 default:
7118 break;
bf8fa3d3
PZ
7119 }
7120 }
7121
47701c3b
PZ
7122 if (has_vga)
7123 lpt_enable_clkout_dp(dev, true, true);
7124 else
7125 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
7126}
7127
dde86e2d
PZ
7128/*
7129 * Initialize reference clocks when the driver loads
7130 */
7131void intel_init_pch_refclk(struct drm_device *dev)
7132{
7133 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7134 ironlake_init_pch_refclk(dev);
7135 else if (HAS_PCH_LPT(dev))
7136 lpt_init_pch_refclk(dev);
7137}
7138
d9d444cb
JB
7139static int ironlake_get_refclk(struct drm_crtc *crtc)
7140{
7141 struct drm_device *dev = crtc->dev;
7142 struct drm_i915_private *dev_priv = dev->dev_private;
7143 struct intel_encoder *encoder;
d9d444cb
JB
7144 int num_connectors = 0;
7145 bool is_lvds = false;
7146
d0737e1d
ACO
7147 for_each_intel_encoder(dev, encoder) {
7148 if (encoder->new_crtc != to_intel_crtc(crtc))
7149 continue;
7150
d9d444cb
JB
7151 switch (encoder->type) {
7152 case INTEL_OUTPUT_LVDS:
7153 is_lvds = true;
7154 break;
6847d71b
PZ
7155 default:
7156 break;
d9d444cb
JB
7157 }
7158 num_connectors++;
7159 }
7160
7161 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 7162 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 7163 dev_priv->vbt.lvds_ssc_freq);
e91e941b 7164 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
7165 }
7166
7167 return 120000;
7168}
7169
6ff93609 7170static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7171{
c8203565 7172 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
7173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7174 int pipe = intel_crtc->pipe;
c8203565
PZ
7175 uint32_t val;
7176
78114071 7177 val = 0;
c8203565 7178
6e3c9717 7179 switch (intel_crtc->config->pipe_bpp) {
c8203565 7180 case 18:
dfd07d72 7181 val |= PIPECONF_6BPC;
c8203565
PZ
7182 break;
7183 case 24:
dfd07d72 7184 val |= PIPECONF_8BPC;
c8203565
PZ
7185 break;
7186 case 30:
dfd07d72 7187 val |= PIPECONF_10BPC;
c8203565
PZ
7188 break;
7189 case 36:
dfd07d72 7190 val |= PIPECONF_12BPC;
c8203565
PZ
7191 break;
7192 default:
cc769b62
PZ
7193 /* Case prevented by intel_choose_pipe_bpp_dither. */
7194 BUG();
c8203565
PZ
7195 }
7196
6e3c9717 7197 if (intel_crtc->config->dither)
c8203565
PZ
7198 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7199
6e3c9717 7200 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
7201 val |= PIPECONF_INTERLACED_ILK;
7202 else
7203 val |= PIPECONF_PROGRESSIVE;
7204
6e3c9717 7205 if (intel_crtc->config->limited_color_range)
3685a8f3 7206 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 7207
c8203565
PZ
7208 I915_WRITE(PIPECONF(pipe), val);
7209 POSTING_READ(PIPECONF(pipe));
7210}
7211
86d3efce
VS
7212/*
7213 * Set up the pipe CSC unit.
7214 *
7215 * Currently only full range RGB to limited range RGB conversion
7216 * is supported, but eventually this should handle various
7217 * RGB<->YCbCr scenarios as well.
7218 */
50f3b016 7219static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
7220{
7221 struct drm_device *dev = crtc->dev;
7222 struct drm_i915_private *dev_priv = dev->dev_private;
7223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7224 int pipe = intel_crtc->pipe;
7225 uint16_t coeff = 0x7800; /* 1.0 */
7226
7227 /*
7228 * TODO: Check what kind of values actually come out of the pipe
7229 * with these coeff/postoff values and adjust to get the best
7230 * accuracy. Perhaps we even need to take the bpc value into
7231 * consideration.
7232 */
7233
6e3c9717 7234 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7235 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7236
7237 /*
7238 * GY/GU and RY/RU should be the other way around according
7239 * to BSpec, but reality doesn't agree. Just set them up in
7240 * a way that results in the correct picture.
7241 */
7242 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7243 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7244
7245 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7246 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7247
7248 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7249 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7250
7251 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7252 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7253 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7254
7255 if (INTEL_INFO(dev)->gen > 6) {
7256 uint16_t postoff = 0;
7257
6e3c9717 7258 if (intel_crtc->config->limited_color_range)
32cf0cb0 7259 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7260
7261 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7262 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7263 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7264
7265 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7266 } else {
7267 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7268
6e3c9717 7269 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7270 mode |= CSC_BLACK_SCREEN_OFFSET;
7271
7272 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7273 }
7274}
7275
6ff93609 7276static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7277{
756f85cf
PZ
7278 struct drm_device *dev = crtc->dev;
7279 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7281 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7282 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
7283 uint32_t val;
7284
3eff4faa 7285 val = 0;
ee2b0b38 7286
6e3c9717 7287 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
7288 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7289
6e3c9717 7290 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7291 val |= PIPECONF_INTERLACED_ILK;
7292 else
7293 val |= PIPECONF_PROGRESSIVE;
7294
702e7a56
PZ
7295 I915_WRITE(PIPECONF(cpu_transcoder), val);
7296 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7297
7298 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7299 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7300
3cdf122c 7301 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7302 val = 0;
7303
6e3c9717 7304 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
7305 case 18:
7306 val |= PIPEMISC_DITHER_6_BPC;
7307 break;
7308 case 24:
7309 val |= PIPEMISC_DITHER_8_BPC;
7310 break;
7311 case 30:
7312 val |= PIPEMISC_DITHER_10_BPC;
7313 break;
7314 case 36:
7315 val |= PIPEMISC_DITHER_12_BPC;
7316 break;
7317 default:
7318 /* Case prevented by pipe_config_set_bpp. */
7319 BUG();
7320 }
7321
6e3c9717 7322 if (intel_crtc->config->dither)
756f85cf
PZ
7323 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7324
7325 I915_WRITE(PIPEMISC(pipe), val);
7326 }
ee2b0b38
PZ
7327}
7328
6591c6e4 7329static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 7330 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
7331 intel_clock_t *clock,
7332 bool *has_reduced_clock,
7333 intel_clock_t *reduced_clock)
7334{
7335 struct drm_device *dev = crtc->dev;
7336 struct drm_i915_private *dev_priv = dev->dev_private;
a919ff14 7337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6591c6e4 7338 int refclk;
d4906093 7339 const intel_limit_t *limit;
a16af721 7340 bool ret, is_lvds = false;
79e53945 7341
d0737e1d 7342 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
79e53945 7343
d9d444cb 7344 refclk = ironlake_get_refclk(crtc);
79e53945 7345
d4906093
ML
7346 /*
7347 * Returns a set of divisors for the desired target clock with the given
7348 * refclk, or FALSE. The returned values represent the clock equation:
7349 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7350 */
409ee761 7351 limit = intel_limit(intel_crtc, refclk);
a919ff14 7352 ret = dev_priv->display.find_dpll(limit, intel_crtc,
190f68c5 7353 crtc_state->port_clock,
ee9300bb 7354 refclk, NULL, clock);
6591c6e4
PZ
7355 if (!ret)
7356 return false;
cda4b7d3 7357
ddc9003c 7358 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7359 /*
7360 * Ensure we match the reduced clock's P to the target clock.
7361 * If the clocks don't match, we can't switch the display clock
7362 * by using the FP0/FP1. In such case we will disable the LVDS
7363 * downclock feature.
7364 */
ee9300bb 7365 *has_reduced_clock =
a919ff14 7366 dev_priv->display.find_dpll(limit, intel_crtc,
ee9300bb
DV
7367 dev_priv->lvds_downclock,
7368 refclk, clock,
7369 reduced_clock);
652c393a 7370 }
61e9653f 7371
6591c6e4
PZ
7372 return true;
7373}
7374
d4b1931c
PZ
7375int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7376{
7377 /*
7378 * Account for spread spectrum to avoid
7379 * oversubscribing the link. Max center spread
7380 * is 2.5%; use 5% for safety's sake.
7381 */
7382 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7383 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7384}
7385
7429e9d4 7386static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7387{
7429e9d4 7388 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7389}
7390
de13a2e3 7391static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 7392 struct intel_crtc_state *crtc_state,
7429e9d4 7393 u32 *fp,
9a7c7890 7394 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7395{
de13a2e3 7396 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7397 struct drm_device *dev = crtc->dev;
7398 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7399 struct intel_encoder *intel_encoder;
7400 uint32_t dpll;
6cc5f341 7401 int factor, num_connectors = 0;
09ede541 7402 bool is_lvds = false, is_sdvo = false;
79e53945 7403
d0737e1d
ACO
7404 for_each_intel_encoder(dev, intel_encoder) {
7405 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7406 continue;
7407
de13a2e3 7408 switch (intel_encoder->type) {
79e53945
JB
7409 case INTEL_OUTPUT_LVDS:
7410 is_lvds = true;
7411 break;
7412 case INTEL_OUTPUT_SDVO:
7d57382e 7413 case INTEL_OUTPUT_HDMI:
79e53945 7414 is_sdvo = true;
79e53945 7415 break;
6847d71b
PZ
7416 default:
7417 break;
79e53945 7418 }
43565a06 7419
c751ce4f 7420 num_connectors++;
79e53945 7421 }
79e53945 7422
c1858123 7423 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7424 factor = 21;
7425 if (is_lvds) {
7426 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7427 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7428 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7429 factor = 25;
190f68c5 7430 } else if (crtc_state->sdvo_tv_clock)
8febb297 7431 factor = 20;
c1858123 7432
190f68c5 7433 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 7434 *fp |= FP_CB_TUNE;
2c07245f 7435
9a7c7890
DV
7436 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7437 *fp2 |= FP_CB_TUNE;
7438
5eddb70b 7439 dpll = 0;
2c07245f 7440
a07d6787
EA
7441 if (is_lvds)
7442 dpll |= DPLLB_MODE_LVDS;
7443 else
7444 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7445
190f68c5 7446 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 7447 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7448
7449 if (is_sdvo)
4a33e48d 7450 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 7451 if (crtc_state->has_dp_encoder)
4a33e48d 7452 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7453
a07d6787 7454 /* compute bitmask from p1 value */
190f68c5 7455 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7456 /* also FPA1 */
190f68c5 7457 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7458
190f68c5 7459 switch (crtc_state->dpll.p2) {
a07d6787
EA
7460 case 5:
7461 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7462 break;
7463 case 7:
7464 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7465 break;
7466 case 10:
7467 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7468 break;
7469 case 14:
7470 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7471 break;
79e53945
JB
7472 }
7473
b4c09f3b 7474 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7475 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7476 else
7477 dpll |= PLL_REF_INPUT_DREFCLK;
7478
959e16d6 7479 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7480}
7481
190f68c5
ACO
7482static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7483 struct intel_crtc_state *crtc_state)
de13a2e3 7484{
c7653199 7485 struct drm_device *dev = crtc->base.dev;
de13a2e3 7486 intel_clock_t clock, reduced_clock;
cbbab5bd 7487 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7488 bool ok, has_reduced_clock = false;
8b47047b 7489 bool is_lvds = false;
e2b78267 7490 struct intel_shared_dpll *pll;
de13a2e3 7491
409ee761 7492 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 7493
5dc5298b
PZ
7494 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7495 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7496
190f68c5 7497 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 7498 &has_reduced_clock, &reduced_clock);
190f68c5 7499 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
7500 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7501 return -EINVAL;
79e53945 7502 }
f47709a9 7503 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7504 if (!crtc_state->clock_set) {
7505 crtc_state->dpll.n = clock.n;
7506 crtc_state->dpll.m1 = clock.m1;
7507 crtc_state->dpll.m2 = clock.m2;
7508 crtc_state->dpll.p1 = clock.p1;
7509 crtc_state->dpll.p2 = clock.p2;
f47709a9 7510 }
79e53945 7511
5dc5298b 7512 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
7513 if (crtc_state->has_pch_encoder) {
7514 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 7515 if (has_reduced_clock)
7429e9d4 7516 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7517
190f68c5 7518 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
7519 &fp, &reduced_clock,
7520 has_reduced_clock ? &fp2 : NULL);
7521
190f68c5
ACO
7522 crtc_state->dpll_hw_state.dpll = dpll;
7523 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 7524 if (has_reduced_clock)
190f68c5 7525 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 7526 else
190f68c5 7527 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 7528
190f68c5 7529 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 7530 if (pll == NULL) {
84f44ce7 7531 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 7532 pipe_name(crtc->pipe));
4b645f14
JB
7533 return -EINVAL;
7534 }
3fb37703 7535 }
79e53945 7536
d330a953 7537 if (is_lvds && has_reduced_clock && i915.powersave)
c7653199 7538 crtc->lowfreq_avail = true;
bcd644e0 7539 else
c7653199 7540 crtc->lowfreq_avail = false;
e2b78267 7541
c8f7a0db 7542 return 0;
79e53945
JB
7543}
7544
eb14cb74
VS
7545static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7546 struct intel_link_m_n *m_n)
7547{
7548 struct drm_device *dev = crtc->base.dev;
7549 struct drm_i915_private *dev_priv = dev->dev_private;
7550 enum pipe pipe = crtc->pipe;
7551
7552 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7553 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7554 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7555 & ~TU_SIZE_MASK;
7556 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7557 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7558 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7559}
7560
7561static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7562 enum transcoder transcoder,
b95af8be
VK
7563 struct intel_link_m_n *m_n,
7564 struct intel_link_m_n *m2_n2)
72419203
DV
7565{
7566 struct drm_device *dev = crtc->base.dev;
7567 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7568 enum pipe pipe = crtc->pipe;
72419203 7569
eb14cb74
VS
7570 if (INTEL_INFO(dev)->gen >= 5) {
7571 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7572 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7573 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7574 & ~TU_SIZE_MASK;
7575 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7576 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7577 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7578 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7579 * gen < 8) and if DRRS is supported (to make sure the
7580 * registers are not unnecessarily read).
7581 */
7582 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 7583 crtc->config->has_drrs) {
b95af8be
VK
7584 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7585 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7586 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7587 & ~TU_SIZE_MASK;
7588 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7589 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7590 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7591 }
eb14cb74
VS
7592 } else {
7593 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7594 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7595 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7596 & ~TU_SIZE_MASK;
7597 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7598 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7599 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7600 }
7601}
7602
7603void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 7604 struct intel_crtc_state *pipe_config)
eb14cb74 7605{
681a8504 7606 if (pipe_config->has_pch_encoder)
eb14cb74
VS
7607 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7608 else
7609 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7610 &pipe_config->dp_m_n,
7611 &pipe_config->dp_m2_n2);
eb14cb74 7612}
72419203 7613
eb14cb74 7614static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 7615 struct intel_crtc_state *pipe_config)
eb14cb74
VS
7616{
7617 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7618 &pipe_config->fdi_m_n, NULL);
72419203
DV
7619}
7620
bd2e244f 7621static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7622 struct intel_crtc_state *pipe_config)
bd2e244f
JB
7623{
7624 struct drm_device *dev = crtc->base.dev;
7625 struct drm_i915_private *dev_priv = dev->dev_private;
7626 uint32_t tmp;
7627
7628 tmp = I915_READ(PS_CTL(crtc->pipe));
7629
7630 if (tmp & PS_ENABLE) {
7631 pipe_config->pch_pfit.enabled = true;
7632 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7633 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7634 }
7635}
7636
5724dbd1
DL
7637static void
7638skylake_get_initial_plane_config(struct intel_crtc *crtc,
7639 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
7640{
7641 struct drm_device *dev = crtc->base.dev;
7642 struct drm_i915_private *dev_priv = dev->dev_private;
7643 u32 val, base, offset, stride_mult;
7644 int pipe = crtc->pipe;
7645 int fourcc, pixel_format;
7646 int aligned_height;
7647 struct drm_framebuffer *fb;
1b842c89 7648 struct intel_framebuffer *intel_fb;
bc8d7dff 7649
d9806c9f 7650 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7651 if (!intel_fb) {
bc8d7dff
DL
7652 DRM_DEBUG_KMS("failed to alloc fb\n");
7653 return;
7654 }
7655
1b842c89
DL
7656 fb = &intel_fb->base;
7657
bc8d7dff 7658 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
7659 if (!(val & PLANE_CTL_ENABLE))
7660 goto error;
7661
bc8d7dff
DL
7662 if (val & PLANE_CTL_TILED_MASK)
7663 plane_config->tiling = I915_TILING_X;
7664
7665 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7666 fourcc = skl_format_to_fourcc(pixel_format,
7667 val & PLANE_CTL_ORDER_RGBX,
7668 val & PLANE_CTL_ALPHA_MASK);
7669 fb->pixel_format = fourcc;
7670 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7671
7672 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7673 plane_config->base = base;
7674
7675 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7676
7677 val = I915_READ(PLANE_SIZE(pipe, 0));
7678 fb->height = ((val >> 16) & 0xfff) + 1;
7679 fb->width = ((val >> 0) & 0x1fff) + 1;
7680
7681 val = I915_READ(PLANE_STRIDE(pipe, 0));
7682 switch (plane_config->tiling) {
7683 case I915_TILING_NONE:
7684 stride_mult = 64;
7685 break;
7686 case I915_TILING_X:
7687 stride_mult = 512;
7688 break;
7689 default:
7690 MISSING_CASE(plane_config->tiling);
7691 goto error;
7692 }
7693 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7694
7695 aligned_height = intel_fb_align_height(dev, fb->height,
7696 plane_config->tiling);
7697
7698 plane_config->size = ALIGN(fb->pitches[0] * aligned_height, PAGE_SIZE);
7699
7700 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7701 pipe_name(pipe), fb->width, fb->height,
7702 fb->bits_per_pixel, base, fb->pitches[0],
7703 plane_config->size);
7704
2d14030b 7705 plane_config->fb = intel_fb;
bc8d7dff
DL
7706 return;
7707
7708error:
7709 kfree(fb);
7710}
7711
2fa2fe9a 7712static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7713 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7714{
7715 struct drm_device *dev = crtc->base.dev;
7716 struct drm_i915_private *dev_priv = dev->dev_private;
7717 uint32_t tmp;
7718
7719 tmp = I915_READ(PF_CTL(crtc->pipe));
7720
7721 if (tmp & PF_ENABLE) {
fd4daa9c 7722 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7723 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7724 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7725
7726 /* We currently do not free assignements of panel fitters on
7727 * ivb/hsw (since we don't use the higher upscaling modes which
7728 * differentiates them) so just WARN about this case for now. */
7729 if (IS_GEN7(dev)) {
7730 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7731 PF_PIPE_SEL_IVB(crtc->pipe));
7732 }
2fa2fe9a 7733 }
79e53945
JB
7734}
7735
5724dbd1
DL
7736static void
7737ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7738 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
7739{
7740 struct drm_device *dev = crtc->base.dev;
7741 struct drm_i915_private *dev_priv = dev->dev_private;
7742 u32 val, base, offset;
aeee5a49 7743 int pipe = crtc->pipe;
4c6baa59
JB
7744 int fourcc, pixel_format;
7745 int aligned_height;
b113d5ee 7746 struct drm_framebuffer *fb;
1b842c89 7747 struct intel_framebuffer *intel_fb;
4c6baa59 7748
42a7b088
DL
7749 val = I915_READ(DSPCNTR(pipe));
7750 if (!(val & DISPLAY_PLANE_ENABLE))
7751 return;
7752
d9806c9f 7753 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7754 if (!intel_fb) {
4c6baa59
JB
7755 DRM_DEBUG_KMS("failed to alloc fb\n");
7756 return;
7757 }
7758
1b842c89
DL
7759 fb = &intel_fb->base;
7760
4c6baa59
JB
7761 if (INTEL_INFO(dev)->gen >= 4)
7762 if (val & DISPPLANE_TILED)
49af449b 7763 plane_config->tiling = I915_TILING_X;
4c6baa59
JB
7764
7765 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7766 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7767 fb->pixel_format = fourcc;
7768 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 7769
aeee5a49 7770 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 7771 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 7772 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 7773 } else {
49af449b 7774 if (plane_config->tiling)
aeee5a49 7775 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 7776 else
aeee5a49 7777 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
7778 }
7779 plane_config->base = base;
7780
7781 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7782 fb->width = ((val >> 16) & 0xfff) + 1;
7783 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7784
7785 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7786 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7787
b113d5ee 7788 aligned_height = intel_fb_align_height(dev, fb->height,
ec2c981e 7789 plane_config->tiling);
4c6baa59 7790
b113d5ee 7791 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
4c6baa59 7792
2844a921
DL
7793 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7794 pipe_name(pipe), fb->width, fb->height,
7795 fb->bits_per_pixel, base, fb->pitches[0],
7796 plane_config->size);
b113d5ee 7797
2d14030b 7798 plane_config->fb = intel_fb;
4c6baa59
JB
7799}
7800
0e8ffe1b 7801static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7802 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7803{
7804 struct drm_device *dev = crtc->base.dev;
7805 struct drm_i915_private *dev_priv = dev->dev_private;
7806 uint32_t tmp;
7807
f458ebbc
DV
7808 if (!intel_display_power_is_enabled(dev_priv,
7809 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
7810 return false;
7811
e143a21c 7812 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7813 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7814
0e8ffe1b
DV
7815 tmp = I915_READ(PIPECONF(crtc->pipe));
7816 if (!(tmp & PIPECONF_ENABLE))
7817 return false;
7818
42571aef
VS
7819 switch (tmp & PIPECONF_BPC_MASK) {
7820 case PIPECONF_6BPC:
7821 pipe_config->pipe_bpp = 18;
7822 break;
7823 case PIPECONF_8BPC:
7824 pipe_config->pipe_bpp = 24;
7825 break;
7826 case PIPECONF_10BPC:
7827 pipe_config->pipe_bpp = 30;
7828 break;
7829 case PIPECONF_12BPC:
7830 pipe_config->pipe_bpp = 36;
7831 break;
7832 default:
7833 break;
7834 }
7835
b5a9fa09
DV
7836 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7837 pipe_config->limited_color_range = true;
7838
ab9412ba 7839 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7840 struct intel_shared_dpll *pll;
7841
88adfff1
DV
7842 pipe_config->has_pch_encoder = true;
7843
627eb5a3
DV
7844 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7845 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7846 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7847
7848 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7849
c0d43d62 7850 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7851 pipe_config->shared_dpll =
7852 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7853 } else {
7854 tmp = I915_READ(PCH_DPLL_SEL);
7855 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7856 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7857 else
7858 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7859 }
66e985c0
DV
7860
7861 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7862
7863 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7864 &pipe_config->dpll_hw_state));
c93f54cf
DV
7865
7866 tmp = pipe_config->dpll_hw_state.dpll;
7867 pipe_config->pixel_multiplier =
7868 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7869 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7870
7871 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7872 } else {
7873 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7874 }
7875
1bd1bd80
DV
7876 intel_get_pipe_timings(crtc, pipe_config);
7877
2fa2fe9a
DV
7878 ironlake_get_pfit_config(crtc, pipe_config);
7879
0e8ffe1b
DV
7880 return true;
7881}
7882
be256dc7
PZ
7883static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7884{
7885 struct drm_device *dev = dev_priv->dev;
be256dc7 7886 struct intel_crtc *crtc;
be256dc7 7887
d3fcc808 7888 for_each_intel_crtc(dev, crtc)
e2c719b7 7889 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7890 pipe_name(crtc->pipe));
7891
e2c719b7
RC
7892 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7893 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7894 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7895 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
7896 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7897 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 7898 "CPU PWM1 enabled\n");
c5107b87 7899 if (IS_HASWELL(dev))
e2c719b7 7900 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 7901 "CPU PWM2 enabled\n");
e2c719b7 7902 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 7903 "PCH PWM1 enabled\n");
e2c719b7 7904 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 7905 "Utility pin enabled\n");
e2c719b7 7906 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 7907
9926ada1
PZ
7908 /*
7909 * In theory we can still leave IRQs enabled, as long as only the HPD
7910 * interrupts remain enabled. We used to check for that, but since it's
7911 * gen-specific and since we only disable LCPLL after we fully disable
7912 * the interrupts, the check below should be enough.
7913 */
e2c719b7 7914 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7915}
7916
9ccd5aeb
PZ
7917static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7918{
7919 struct drm_device *dev = dev_priv->dev;
7920
7921 if (IS_HASWELL(dev))
7922 return I915_READ(D_COMP_HSW);
7923 else
7924 return I915_READ(D_COMP_BDW);
7925}
7926
3c4c9b81
PZ
7927static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7928{
7929 struct drm_device *dev = dev_priv->dev;
7930
7931 if (IS_HASWELL(dev)) {
7932 mutex_lock(&dev_priv->rps.hw_lock);
7933 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7934 val))
f475dadf 7935 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7936 mutex_unlock(&dev_priv->rps.hw_lock);
7937 } else {
9ccd5aeb
PZ
7938 I915_WRITE(D_COMP_BDW, val);
7939 POSTING_READ(D_COMP_BDW);
3c4c9b81 7940 }
be256dc7
PZ
7941}
7942
7943/*
7944 * This function implements pieces of two sequences from BSpec:
7945 * - Sequence for display software to disable LCPLL
7946 * - Sequence for display software to allow package C8+
7947 * The steps implemented here are just the steps that actually touch the LCPLL
7948 * register. Callers should take care of disabling all the display engine
7949 * functions, doing the mode unset, fixing interrupts, etc.
7950 */
6ff58d53
PZ
7951static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7952 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7953{
7954 uint32_t val;
7955
7956 assert_can_disable_lcpll(dev_priv);
7957
7958 val = I915_READ(LCPLL_CTL);
7959
7960 if (switch_to_fclk) {
7961 val |= LCPLL_CD_SOURCE_FCLK;
7962 I915_WRITE(LCPLL_CTL, val);
7963
7964 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7965 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7966 DRM_ERROR("Switching to FCLK failed\n");
7967
7968 val = I915_READ(LCPLL_CTL);
7969 }
7970
7971 val |= LCPLL_PLL_DISABLE;
7972 I915_WRITE(LCPLL_CTL, val);
7973 POSTING_READ(LCPLL_CTL);
7974
7975 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7976 DRM_ERROR("LCPLL still locked\n");
7977
9ccd5aeb 7978 val = hsw_read_dcomp(dev_priv);
be256dc7 7979 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7980 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7981 ndelay(100);
7982
9ccd5aeb
PZ
7983 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7984 1))
be256dc7
PZ
7985 DRM_ERROR("D_COMP RCOMP still in progress\n");
7986
7987 if (allow_power_down) {
7988 val = I915_READ(LCPLL_CTL);
7989 val |= LCPLL_POWER_DOWN_ALLOW;
7990 I915_WRITE(LCPLL_CTL, val);
7991 POSTING_READ(LCPLL_CTL);
7992 }
7993}
7994
7995/*
7996 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7997 * source.
7998 */
6ff58d53 7999static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8000{
8001 uint32_t val;
8002
8003 val = I915_READ(LCPLL_CTL);
8004
8005 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8006 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8007 return;
8008
a8a8bd54
PZ
8009 /*
8010 * Make sure we're not on PC8 state before disabling PC8, otherwise
8011 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8012 */
59bad947 8013 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8014
be256dc7
PZ
8015 if (val & LCPLL_POWER_DOWN_ALLOW) {
8016 val &= ~LCPLL_POWER_DOWN_ALLOW;
8017 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8018 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8019 }
8020
9ccd5aeb 8021 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8022 val |= D_COMP_COMP_FORCE;
8023 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8024 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8025
8026 val = I915_READ(LCPLL_CTL);
8027 val &= ~LCPLL_PLL_DISABLE;
8028 I915_WRITE(LCPLL_CTL, val);
8029
8030 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8031 DRM_ERROR("LCPLL not locked yet\n");
8032
8033 if (val & LCPLL_CD_SOURCE_FCLK) {
8034 val = I915_READ(LCPLL_CTL);
8035 val &= ~LCPLL_CD_SOURCE_FCLK;
8036 I915_WRITE(LCPLL_CTL, val);
8037
8038 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8039 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8040 DRM_ERROR("Switching back to LCPLL failed\n");
8041 }
215733fa 8042
59bad947 8043 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
8044}
8045
765dab67
PZ
8046/*
8047 * Package states C8 and deeper are really deep PC states that can only be
8048 * reached when all the devices on the system allow it, so even if the graphics
8049 * device allows PC8+, it doesn't mean the system will actually get to these
8050 * states. Our driver only allows PC8+ when going into runtime PM.
8051 *
8052 * The requirements for PC8+ are that all the outputs are disabled, the power
8053 * well is disabled and most interrupts are disabled, and these are also
8054 * requirements for runtime PM. When these conditions are met, we manually do
8055 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8056 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8057 * hang the machine.
8058 *
8059 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8060 * the state of some registers, so when we come back from PC8+ we need to
8061 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8062 * need to take care of the registers kept by RC6. Notice that this happens even
8063 * if we don't put the device in PCI D3 state (which is what currently happens
8064 * because of the runtime PM support).
8065 *
8066 * For more, read "Display Sequences for Package C8" on the hardware
8067 * documentation.
8068 */
a14cb6fc 8069void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8070{
c67a470b
PZ
8071 struct drm_device *dev = dev_priv->dev;
8072 uint32_t val;
8073
c67a470b
PZ
8074 DRM_DEBUG_KMS("Enabling package C8+\n");
8075
c67a470b
PZ
8076 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8077 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8078 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8079 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8080 }
8081
8082 lpt_disable_clkout_dp(dev);
c67a470b
PZ
8083 hsw_disable_lcpll(dev_priv, true, true);
8084}
8085
a14cb6fc 8086void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
8087{
8088 struct drm_device *dev = dev_priv->dev;
8089 uint32_t val;
8090
c67a470b
PZ
8091 DRM_DEBUG_KMS("Disabling package C8+\n");
8092
8093 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
8094 lpt_init_pch_refclk(dev);
8095
8096 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8097 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8098 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8099 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8100 }
8101
8102 intel_prepare_ddi(dev);
c67a470b
PZ
8103}
8104
190f68c5
ACO
8105static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8106 struct intel_crtc_state *crtc_state)
09b4ddf9 8107{
190f68c5 8108 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 8109 return -EINVAL;
716c2e55 8110
c7653199 8111 crtc->lowfreq_avail = false;
644cef34 8112
c8f7a0db 8113 return 0;
79e53945
JB
8114}
8115
96b7dfb7
S
8116static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8117 enum port port,
5cec258b 8118 struct intel_crtc_state *pipe_config)
96b7dfb7 8119{
3148ade7 8120 u32 temp, dpll_ctl1;
96b7dfb7
S
8121
8122 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8123 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8124
8125 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
8126 case SKL_DPLL0:
8127 /*
8128 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8129 * of the shared DPLL framework and thus needs to be read out
8130 * separately
8131 */
8132 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8133 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8134 break;
96b7dfb7
S
8135 case SKL_DPLL1:
8136 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8137 break;
8138 case SKL_DPLL2:
8139 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8140 break;
8141 case SKL_DPLL3:
8142 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8143 break;
96b7dfb7
S
8144 }
8145}
8146
7d2c8175
DL
8147static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8148 enum port port,
5cec258b 8149 struct intel_crtc_state *pipe_config)
7d2c8175
DL
8150{
8151 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8152
8153 switch (pipe_config->ddi_pll_sel) {
8154 case PORT_CLK_SEL_WRPLL1:
8155 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8156 break;
8157 case PORT_CLK_SEL_WRPLL2:
8158 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8159 break;
8160 }
8161}
8162
26804afd 8163static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 8164 struct intel_crtc_state *pipe_config)
26804afd
DV
8165{
8166 struct drm_device *dev = crtc->base.dev;
8167 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 8168 struct intel_shared_dpll *pll;
26804afd
DV
8169 enum port port;
8170 uint32_t tmp;
8171
8172 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8173
8174 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8175
96b7dfb7
S
8176 if (IS_SKYLAKE(dev))
8177 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8178 else
8179 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 8180
d452c5b6
DV
8181 if (pipe_config->shared_dpll >= 0) {
8182 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8183
8184 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8185 &pipe_config->dpll_hw_state));
8186 }
8187
26804afd
DV
8188 /*
8189 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8190 * DDI E. So just check whether this pipe is wired to DDI E and whether
8191 * the PCH transcoder is on.
8192 */
ca370455
DL
8193 if (INTEL_INFO(dev)->gen < 9 &&
8194 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
8195 pipe_config->has_pch_encoder = true;
8196
8197 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8198 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8199 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8200
8201 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8202 }
8203}
8204
0e8ffe1b 8205static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8206 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8207{
8208 struct drm_device *dev = crtc->base.dev;
8209 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 8210 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
8211 uint32_t tmp;
8212
f458ebbc 8213 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
8214 POWER_DOMAIN_PIPE(crtc->pipe)))
8215 return false;
8216
e143a21c 8217 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
8218 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8219
eccb140b
DV
8220 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8221 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8222 enum pipe trans_edp_pipe;
8223 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8224 default:
8225 WARN(1, "unknown pipe linked to edp transcoder\n");
8226 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8227 case TRANS_DDI_EDP_INPUT_A_ON:
8228 trans_edp_pipe = PIPE_A;
8229 break;
8230 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8231 trans_edp_pipe = PIPE_B;
8232 break;
8233 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8234 trans_edp_pipe = PIPE_C;
8235 break;
8236 }
8237
8238 if (trans_edp_pipe == crtc->pipe)
8239 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8240 }
8241
f458ebbc 8242 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 8243 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
8244 return false;
8245
eccb140b 8246 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
8247 if (!(tmp & PIPECONF_ENABLE))
8248 return false;
8249
26804afd 8250 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 8251
1bd1bd80
DV
8252 intel_get_pipe_timings(crtc, pipe_config);
8253
2fa2fe9a 8254 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f
JB
8255 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8256 if (IS_SKYLAKE(dev))
8257 skylake_get_pfit_config(crtc, pipe_config);
8258 else
8259 ironlake_get_pfit_config(crtc, pipe_config);
8260 }
88adfff1 8261
e59150dc
JB
8262 if (IS_HASWELL(dev))
8263 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8264 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 8265
ebb69c95
CT
8266 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8267 pipe_config->pixel_multiplier =
8268 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8269 } else {
8270 pipe_config->pixel_multiplier = 1;
8271 }
6c49f241 8272
0e8ffe1b
DV
8273 return true;
8274}
8275
560b85bb
CW
8276static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8277{
8278 struct drm_device *dev = crtc->dev;
8279 struct drm_i915_private *dev_priv = dev->dev_private;
8280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8281 uint32_t cntl = 0, size = 0;
560b85bb 8282
dc41c154
VS
8283 if (base) {
8284 unsigned int width = intel_crtc->cursor_width;
8285 unsigned int height = intel_crtc->cursor_height;
8286 unsigned int stride = roundup_pow_of_two(width) * 4;
8287
8288 switch (stride) {
8289 default:
8290 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8291 width, stride);
8292 stride = 256;
8293 /* fallthrough */
8294 case 256:
8295 case 512:
8296 case 1024:
8297 case 2048:
8298 break;
4b0e333e
CW
8299 }
8300
dc41c154
VS
8301 cntl |= CURSOR_ENABLE |
8302 CURSOR_GAMMA_ENABLE |
8303 CURSOR_FORMAT_ARGB |
8304 CURSOR_STRIDE(stride);
8305
8306 size = (height << 12) | width;
4b0e333e 8307 }
560b85bb 8308
dc41c154
VS
8309 if (intel_crtc->cursor_cntl != 0 &&
8310 (intel_crtc->cursor_base != base ||
8311 intel_crtc->cursor_size != size ||
8312 intel_crtc->cursor_cntl != cntl)) {
8313 /* On these chipsets we can only modify the base/size/stride
8314 * whilst the cursor is disabled.
8315 */
8316 I915_WRITE(_CURACNTR, 0);
4b0e333e 8317 POSTING_READ(_CURACNTR);
dc41c154 8318 intel_crtc->cursor_cntl = 0;
4b0e333e 8319 }
560b85bb 8320
99d1f387 8321 if (intel_crtc->cursor_base != base) {
9db4a9c7 8322 I915_WRITE(_CURABASE, base);
99d1f387
VS
8323 intel_crtc->cursor_base = base;
8324 }
4726e0b0 8325
dc41c154
VS
8326 if (intel_crtc->cursor_size != size) {
8327 I915_WRITE(CURSIZE, size);
8328 intel_crtc->cursor_size = size;
4b0e333e 8329 }
560b85bb 8330
4b0e333e 8331 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8332 I915_WRITE(_CURACNTR, cntl);
8333 POSTING_READ(_CURACNTR);
4b0e333e 8334 intel_crtc->cursor_cntl = cntl;
560b85bb 8335 }
560b85bb
CW
8336}
8337
560b85bb 8338static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8339{
8340 struct drm_device *dev = crtc->dev;
8341 struct drm_i915_private *dev_priv = dev->dev_private;
8342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8343 int pipe = intel_crtc->pipe;
4b0e333e
CW
8344 uint32_t cntl;
8345
8346 cntl = 0;
8347 if (base) {
8348 cntl = MCURSOR_GAMMA_ENABLE;
8349 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8350 case 64:
8351 cntl |= CURSOR_MODE_64_ARGB_AX;
8352 break;
8353 case 128:
8354 cntl |= CURSOR_MODE_128_ARGB_AX;
8355 break;
8356 case 256:
8357 cntl |= CURSOR_MODE_256_ARGB_AX;
8358 break;
8359 default:
5f77eeb0 8360 MISSING_CASE(intel_crtc->cursor_width);
4726e0b0 8361 return;
65a21cd6 8362 }
4b0e333e 8363 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8364
8365 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8366 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8367 }
65a21cd6 8368
8e7d688b 8369 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
8370 cntl |= CURSOR_ROTATE_180;
8371
4b0e333e
CW
8372 if (intel_crtc->cursor_cntl != cntl) {
8373 I915_WRITE(CURCNTR(pipe), cntl);
8374 POSTING_READ(CURCNTR(pipe));
8375 intel_crtc->cursor_cntl = cntl;
65a21cd6 8376 }
4b0e333e 8377
65a21cd6 8378 /* and commit changes on next vblank */
5efb3e28
VS
8379 I915_WRITE(CURBASE(pipe), base);
8380 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8381
8382 intel_crtc->cursor_base = base;
65a21cd6
JB
8383}
8384
cda4b7d3 8385/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8386static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8387 bool on)
cda4b7d3
CW
8388{
8389 struct drm_device *dev = crtc->dev;
8390 struct drm_i915_private *dev_priv = dev->dev_private;
8391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8392 int pipe = intel_crtc->pipe;
3d7d6510
MR
8393 int x = crtc->cursor_x;
8394 int y = crtc->cursor_y;
d6e4db15 8395 u32 base = 0, pos = 0;
cda4b7d3 8396
d6e4db15 8397 if (on)
cda4b7d3 8398 base = intel_crtc->cursor_addr;
cda4b7d3 8399
6e3c9717 8400 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
8401 base = 0;
8402
6e3c9717 8403 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
8404 base = 0;
8405
8406 if (x < 0) {
efc9064e 8407 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8408 base = 0;
8409
8410 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8411 x = -x;
8412 }
8413 pos |= x << CURSOR_X_SHIFT;
8414
8415 if (y < 0) {
efc9064e 8416 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8417 base = 0;
8418
8419 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8420 y = -y;
8421 }
8422 pos |= y << CURSOR_Y_SHIFT;
8423
4b0e333e 8424 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8425 return;
8426
5efb3e28
VS
8427 I915_WRITE(CURPOS(pipe), pos);
8428
4398ad45
VS
8429 /* ILK+ do this automagically */
8430 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 8431 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
4398ad45
VS
8432 base += (intel_crtc->cursor_height *
8433 intel_crtc->cursor_width - 1) * 4;
8434 }
8435
8ac54669 8436 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8437 i845_update_cursor(crtc, base);
8438 else
8439 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8440}
8441
dc41c154
VS
8442static bool cursor_size_ok(struct drm_device *dev,
8443 uint32_t width, uint32_t height)
8444{
8445 if (width == 0 || height == 0)
8446 return false;
8447
8448 /*
8449 * 845g/865g are special in that they are only limited by
8450 * the width of their cursors, the height is arbitrary up to
8451 * the precision of the register. Everything else requires
8452 * square cursors, limited to a few power-of-two sizes.
8453 */
8454 if (IS_845G(dev) || IS_I865G(dev)) {
8455 if ((width & 63) != 0)
8456 return false;
8457
8458 if (width > (IS_845G(dev) ? 64 : 512))
8459 return false;
8460
8461 if (height > 1023)
8462 return false;
8463 } else {
8464 switch (width | height) {
8465 case 256:
8466 case 128:
8467 if (IS_GEN2(dev))
8468 return false;
8469 case 64:
8470 break;
8471 default:
8472 return false;
8473 }
8474 }
8475
8476 return true;
8477}
8478
79e53945 8479static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8480 u16 *blue, uint32_t start, uint32_t size)
79e53945 8481{
7203425a 8482 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8484
7203425a 8485 for (i = start; i < end; i++) {
79e53945
JB
8486 intel_crtc->lut_r[i] = red[i] >> 8;
8487 intel_crtc->lut_g[i] = green[i] >> 8;
8488 intel_crtc->lut_b[i] = blue[i] >> 8;
8489 }
8490
8491 intel_crtc_load_lut(crtc);
8492}
8493
79e53945
JB
8494/* VESA 640x480x72Hz mode to set on the pipe */
8495static struct drm_display_mode load_detect_mode = {
8496 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8497 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8498};
8499
a8bb6818
DV
8500struct drm_framebuffer *
8501__intel_framebuffer_create(struct drm_device *dev,
8502 struct drm_mode_fb_cmd2 *mode_cmd,
8503 struct drm_i915_gem_object *obj)
d2dff872
CW
8504{
8505 struct intel_framebuffer *intel_fb;
8506 int ret;
8507
8508 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8509 if (!intel_fb) {
6ccb81f2 8510 drm_gem_object_unreference(&obj->base);
d2dff872
CW
8511 return ERR_PTR(-ENOMEM);
8512 }
8513
8514 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8515 if (ret)
8516 goto err;
d2dff872
CW
8517
8518 return &intel_fb->base;
dd4916c5 8519err:
6ccb81f2 8520 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
8521 kfree(intel_fb);
8522
8523 return ERR_PTR(ret);
d2dff872
CW
8524}
8525
b5ea642a 8526static struct drm_framebuffer *
a8bb6818
DV
8527intel_framebuffer_create(struct drm_device *dev,
8528 struct drm_mode_fb_cmd2 *mode_cmd,
8529 struct drm_i915_gem_object *obj)
8530{
8531 struct drm_framebuffer *fb;
8532 int ret;
8533
8534 ret = i915_mutex_lock_interruptible(dev);
8535 if (ret)
8536 return ERR_PTR(ret);
8537 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8538 mutex_unlock(&dev->struct_mutex);
8539
8540 return fb;
8541}
8542
d2dff872
CW
8543static u32
8544intel_framebuffer_pitch_for_width(int width, int bpp)
8545{
8546 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8547 return ALIGN(pitch, 64);
8548}
8549
8550static u32
8551intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8552{
8553 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8554 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8555}
8556
8557static struct drm_framebuffer *
8558intel_framebuffer_create_for_mode(struct drm_device *dev,
8559 struct drm_display_mode *mode,
8560 int depth, int bpp)
8561{
8562 struct drm_i915_gem_object *obj;
0fed39bd 8563 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8564
8565 obj = i915_gem_alloc_object(dev,
8566 intel_framebuffer_size_for_mode(mode, bpp));
8567 if (obj == NULL)
8568 return ERR_PTR(-ENOMEM);
8569
8570 mode_cmd.width = mode->hdisplay;
8571 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8572 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8573 bpp);
5ca0c34a 8574 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8575
8576 return intel_framebuffer_create(dev, &mode_cmd, obj);
8577}
8578
8579static struct drm_framebuffer *
8580mode_fits_in_fbdev(struct drm_device *dev,
8581 struct drm_display_mode *mode)
8582{
4520f53a 8583#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8584 struct drm_i915_private *dev_priv = dev->dev_private;
8585 struct drm_i915_gem_object *obj;
8586 struct drm_framebuffer *fb;
8587
4c0e5528 8588 if (!dev_priv->fbdev)
d2dff872
CW
8589 return NULL;
8590
4c0e5528 8591 if (!dev_priv->fbdev->fb)
d2dff872
CW
8592 return NULL;
8593
4c0e5528
DV
8594 obj = dev_priv->fbdev->fb->obj;
8595 BUG_ON(!obj);
8596
8bcd4553 8597 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8598 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8599 fb->bits_per_pixel))
d2dff872
CW
8600 return NULL;
8601
01f2c773 8602 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8603 return NULL;
8604
8605 return fb;
4520f53a
DV
8606#else
8607 return NULL;
8608#endif
d2dff872
CW
8609}
8610
d2434ab7 8611bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8612 struct drm_display_mode *mode,
51fd371b
RC
8613 struct intel_load_detect_pipe *old,
8614 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8615{
8616 struct intel_crtc *intel_crtc;
d2434ab7
DV
8617 struct intel_encoder *intel_encoder =
8618 intel_attached_encoder(connector);
79e53945 8619 struct drm_crtc *possible_crtc;
4ef69c7a 8620 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8621 struct drm_crtc *crtc = NULL;
8622 struct drm_device *dev = encoder->dev;
94352cf9 8623 struct drm_framebuffer *fb;
51fd371b
RC
8624 struct drm_mode_config *config = &dev->mode_config;
8625 int ret, i = -1;
79e53945 8626
d2dff872 8627 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8628 connector->base.id, connector->name,
8e329a03 8629 encoder->base.id, encoder->name);
d2dff872 8630
51fd371b
RC
8631retry:
8632 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8633 if (ret)
8634 goto fail_unlock;
6e9f798d 8635
79e53945
JB
8636 /*
8637 * Algorithm gets a little messy:
7a5e4805 8638 *
79e53945
JB
8639 * - if the connector already has an assigned crtc, use it (but make
8640 * sure it's on first)
7a5e4805 8641 *
79e53945
JB
8642 * - try to find the first unused crtc that can drive this connector,
8643 * and use that if we find one
79e53945
JB
8644 */
8645
8646 /* See if we already have a CRTC for this connector */
8647 if (encoder->crtc) {
8648 crtc = encoder->crtc;
8261b191 8649
51fd371b 8650 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
8651 if (ret)
8652 goto fail_unlock;
8653 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
8654 if (ret)
8655 goto fail_unlock;
7b24056b 8656
24218aac 8657 old->dpms_mode = connector->dpms;
8261b191
CW
8658 old->load_detect_temp = false;
8659
8660 /* Make sure the crtc and connector are running */
24218aac
DV
8661 if (connector->dpms != DRM_MODE_DPMS_ON)
8662 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8663
7173188d 8664 return true;
79e53945
JB
8665 }
8666
8667 /* Find an unused one (if possible) */
70e1e0ec 8668 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8669 i++;
8670 if (!(encoder->possible_crtcs & (1 << i)))
8671 continue;
a459249c
VS
8672 if (possible_crtc->enabled)
8673 continue;
8674 /* This can occur when applying the pipe A quirk on resume. */
8675 if (to_intel_crtc(possible_crtc)->new_enabled)
8676 continue;
8677
8678 crtc = possible_crtc;
8679 break;
79e53945
JB
8680 }
8681
8682 /*
8683 * If we didn't find an unused CRTC, don't use any.
8684 */
8685 if (!crtc) {
7173188d 8686 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8687 goto fail_unlock;
79e53945
JB
8688 }
8689
51fd371b
RC
8690 ret = drm_modeset_lock(&crtc->mutex, ctx);
8691 if (ret)
4d02e2de
DV
8692 goto fail_unlock;
8693 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8694 if (ret)
51fd371b 8695 goto fail_unlock;
fc303101
DV
8696 intel_encoder->new_crtc = to_intel_crtc(crtc);
8697 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8698
8699 intel_crtc = to_intel_crtc(crtc);
412b61d8 8700 intel_crtc->new_enabled = true;
6e3c9717 8701 intel_crtc->new_config = intel_crtc->config;
24218aac 8702 old->dpms_mode = connector->dpms;
8261b191 8703 old->load_detect_temp = true;
d2dff872 8704 old->release_fb = NULL;
79e53945 8705
6492711d
CW
8706 if (!mode)
8707 mode = &load_detect_mode;
79e53945 8708
d2dff872
CW
8709 /* We need a framebuffer large enough to accommodate all accesses
8710 * that the plane may generate whilst we perform load detection.
8711 * We can not rely on the fbcon either being present (we get called
8712 * during its initialisation to detect all boot displays, or it may
8713 * not even exist) or that it is large enough to satisfy the
8714 * requested mode.
8715 */
94352cf9
DV
8716 fb = mode_fits_in_fbdev(dev, mode);
8717 if (fb == NULL) {
d2dff872 8718 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8719 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8720 old->release_fb = fb;
d2dff872
CW
8721 } else
8722 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8723 if (IS_ERR(fb)) {
d2dff872 8724 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8725 goto fail;
79e53945 8726 }
79e53945 8727
c0c36b94 8728 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8729 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8730 if (old->release_fb)
8731 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8732 goto fail;
79e53945 8733 }
7173188d 8734
79e53945 8735 /* let the connector get through one full cycle before testing */
9d0498a2 8736 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8737 return true;
412b61d8
VS
8738
8739 fail:
8740 intel_crtc->new_enabled = crtc->enabled;
8741 if (intel_crtc->new_enabled)
6e3c9717 8742 intel_crtc->new_config = intel_crtc->config;
412b61d8
VS
8743 else
8744 intel_crtc->new_config = NULL;
51fd371b
RC
8745fail_unlock:
8746 if (ret == -EDEADLK) {
8747 drm_modeset_backoff(ctx);
8748 goto retry;
8749 }
8750
412b61d8 8751 return false;
79e53945
JB
8752}
8753
d2434ab7 8754void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8755 struct intel_load_detect_pipe *old)
79e53945 8756{
d2434ab7
DV
8757 struct intel_encoder *intel_encoder =
8758 intel_attached_encoder(connector);
4ef69c7a 8759 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8760 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8762
d2dff872 8763 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8764 connector->base.id, connector->name,
8e329a03 8765 encoder->base.id, encoder->name);
d2dff872 8766
8261b191 8767 if (old->load_detect_temp) {
fc303101
DV
8768 to_intel_connector(connector)->new_encoder = NULL;
8769 intel_encoder->new_crtc = NULL;
412b61d8
VS
8770 intel_crtc->new_enabled = false;
8771 intel_crtc->new_config = NULL;
fc303101 8772 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8773
36206361
DV
8774 if (old->release_fb) {
8775 drm_framebuffer_unregister_private(old->release_fb);
8776 drm_framebuffer_unreference(old->release_fb);
8777 }
d2dff872 8778
0622a53c 8779 return;
79e53945
JB
8780 }
8781
c751ce4f 8782 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8783 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8784 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8785}
8786
da4a1efa 8787static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 8788 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
8789{
8790 struct drm_i915_private *dev_priv = dev->dev_private;
8791 u32 dpll = pipe_config->dpll_hw_state.dpll;
8792
8793 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8794 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8795 else if (HAS_PCH_SPLIT(dev))
8796 return 120000;
8797 else if (!IS_GEN2(dev))
8798 return 96000;
8799 else
8800 return 48000;
8801}
8802
79e53945 8803/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 8804static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8805 struct intel_crtc_state *pipe_config)
79e53945 8806{
f1f644dc 8807 struct drm_device *dev = crtc->base.dev;
79e53945 8808 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8809 int pipe = pipe_config->cpu_transcoder;
293623f7 8810 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8811 u32 fp;
8812 intel_clock_t clock;
da4a1efa 8813 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8814
8815 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8816 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8817 else
293623f7 8818 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8819
8820 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8821 if (IS_PINEVIEW(dev)) {
8822 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8823 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8824 } else {
8825 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8826 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8827 }
8828
a6c45cf0 8829 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8830 if (IS_PINEVIEW(dev))
8831 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8832 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8833 else
8834 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8835 DPLL_FPA01_P1_POST_DIV_SHIFT);
8836
8837 switch (dpll & DPLL_MODE_MASK) {
8838 case DPLLB_MODE_DAC_SERIAL:
8839 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8840 5 : 10;
8841 break;
8842 case DPLLB_MODE_LVDS:
8843 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8844 7 : 14;
8845 break;
8846 default:
28c97730 8847 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8848 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8849 return;
79e53945
JB
8850 }
8851
ac58c3f0 8852 if (IS_PINEVIEW(dev))
da4a1efa 8853 pineview_clock(refclk, &clock);
ac58c3f0 8854 else
da4a1efa 8855 i9xx_clock(refclk, &clock);
79e53945 8856 } else {
0fb58223 8857 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8858 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8859
8860 if (is_lvds) {
8861 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8862 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8863
8864 if (lvds & LVDS_CLKB_POWER_UP)
8865 clock.p2 = 7;
8866 else
8867 clock.p2 = 14;
79e53945
JB
8868 } else {
8869 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8870 clock.p1 = 2;
8871 else {
8872 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8873 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8874 }
8875 if (dpll & PLL_P2_DIVIDE_BY_4)
8876 clock.p2 = 4;
8877 else
8878 clock.p2 = 2;
79e53945 8879 }
da4a1efa
VS
8880
8881 i9xx_clock(refclk, &clock);
79e53945
JB
8882 }
8883
18442d08
VS
8884 /*
8885 * This value includes pixel_multiplier. We will use
241bfc38 8886 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8887 * encoder's get_config() function.
8888 */
8889 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8890}
8891
6878da05
VS
8892int intel_dotclock_calculate(int link_freq,
8893 const struct intel_link_m_n *m_n)
f1f644dc 8894{
f1f644dc
JB
8895 /*
8896 * The calculation for the data clock is:
1041a02f 8897 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8898 * But we want to avoid losing precison if possible, so:
1041a02f 8899 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8900 *
8901 * and the link clock is simpler:
1041a02f 8902 * link_clock = (m * link_clock) / n
f1f644dc
JB
8903 */
8904
6878da05
VS
8905 if (!m_n->link_n)
8906 return 0;
f1f644dc 8907
6878da05
VS
8908 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8909}
f1f644dc 8910
18442d08 8911static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 8912 struct intel_crtc_state *pipe_config)
6878da05
VS
8913{
8914 struct drm_device *dev = crtc->base.dev;
79e53945 8915
18442d08
VS
8916 /* read out port_clock from the DPLL */
8917 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8918
f1f644dc 8919 /*
18442d08 8920 * This value does not include pixel_multiplier.
241bfc38 8921 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8922 * agree once we know their relationship in the encoder's
8923 * get_config() function.
79e53945 8924 */
2d112de7 8925 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
8926 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8927 &pipe_config->fdi_m_n);
79e53945
JB
8928}
8929
8930/** Returns the currently programmed mode of the given pipe. */
8931struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8932 struct drm_crtc *crtc)
8933{
548f245b 8934 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8936 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 8937 struct drm_display_mode *mode;
5cec258b 8938 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
8939 int htot = I915_READ(HTOTAL(cpu_transcoder));
8940 int hsync = I915_READ(HSYNC(cpu_transcoder));
8941 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8942 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8943 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8944
8945 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8946 if (!mode)
8947 return NULL;
8948
f1f644dc
JB
8949 /*
8950 * Construct a pipe_config sufficient for getting the clock info
8951 * back out of crtc_clock_get.
8952 *
8953 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8954 * to use a real value here instead.
8955 */
293623f7 8956 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8957 pipe_config.pixel_multiplier = 1;
293623f7
VS
8958 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8959 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8960 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8961 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8962
773ae034 8963 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8964 mode->hdisplay = (htot & 0xffff) + 1;
8965 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8966 mode->hsync_start = (hsync & 0xffff) + 1;
8967 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8968 mode->vdisplay = (vtot & 0xffff) + 1;
8969 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8970 mode->vsync_start = (vsync & 0xffff) + 1;
8971 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8972
8973 drm_mode_set_name(mode);
79e53945
JB
8974
8975 return mode;
8976}
8977
652c393a
JB
8978static void intel_decrease_pllclock(struct drm_crtc *crtc)
8979{
8980 struct drm_device *dev = crtc->dev;
fbee40df 8981 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8983
baff296c 8984 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8985 return;
8986
8987 if (!dev_priv->lvds_downclock_avail)
8988 return;
8989
8990 /*
8991 * Since this is called by a timer, we should never get here in
8992 * the manual case.
8993 */
8994 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8995 int pipe = intel_crtc->pipe;
8996 int dpll_reg = DPLL(pipe);
8997 int dpll;
f6e5b160 8998
44d98a61 8999 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9000
8ac5a6d5 9001 assert_panel_unlocked(dev_priv, pipe);
652c393a 9002
dc257cf1 9003 dpll = I915_READ(dpll_reg);
652c393a
JB
9004 dpll |= DISPLAY_RATE_SELECT_FPA1;
9005 I915_WRITE(dpll_reg, dpll);
9d0498a2 9006 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9007 dpll = I915_READ(dpll_reg);
9008 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9009 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9010 }
9011
9012}
9013
f047e395
CW
9014void intel_mark_busy(struct drm_device *dev)
9015{
c67a470b
PZ
9016 struct drm_i915_private *dev_priv = dev->dev_private;
9017
f62a0076
CW
9018 if (dev_priv->mm.busy)
9019 return;
9020
43694d69 9021 intel_runtime_pm_get(dev_priv);
c67a470b 9022 i915_update_gfx_val(dev_priv);
f62a0076 9023 dev_priv->mm.busy = true;
f047e395
CW
9024}
9025
9026void intel_mark_idle(struct drm_device *dev)
652c393a 9027{
c67a470b 9028 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9029 struct drm_crtc *crtc;
652c393a 9030
f62a0076
CW
9031 if (!dev_priv->mm.busy)
9032 return;
9033
9034 dev_priv->mm.busy = false;
9035
d330a953 9036 if (!i915.powersave)
bb4cdd53 9037 goto out;
652c393a 9038
70e1e0ec 9039 for_each_crtc(dev, crtc) {
f4510a27 9040 if (!crtc->primary->fb)
652c393a
JB
9041 continue;
9042
725a5b54 9043 intel_decrease_pllclock(crtc);
652c393a 9044 }
b29c19b6 9045
3d13ef2e 9046 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9047 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9048
9049out:
43694d69 9050 intel_runtime_pm_put(dev_priv);
652c393a
JB
9051}
9052
f5de6e07
ACO
9053static void intel_crtc_set_state(struct intel_crtc *crtc,
9054 struct intel_crtc_state *crtc_state)
9055{
9056 kfree(crtc->config);
9057 crtc->config = crtc_state;
16f3f658 9058 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
9059}
9060
79e53945
JB
9061static void intel_crtc_destroy(struct drm_crtc *crtc)
9062{
9063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9064 struct drm_device *dev = crtc->dev;
9065 struct intel_unpin_work *work;
67e77c5a 9066
5e2d7afc 9067 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
9068 work = intel_crtc->unpin_work;
9069 intel_crtc->unpin_work = NULL;
5e2d7afc 9070 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
9071
9072 if (work) {
9073 cancel_work_sync(&work->work);
9074 kfree(work);
9075 }
79e53945 9076
f5de6e07 9077 intel_crtc_set_state(intel_crtc, NULL);
79e53945 9078 drm_crtc_cleanup(crtc);
67e77c5a 9079
79e53945
JB
9080 kfree(intel_crtc);
9081}
9082
6b95a207
KH
9083static void intel_unpin_work_fn(struct work_struct *__work)
9084{
9085 struct intel_unpin_work *work =
9086 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9087 struct drm_device *dev = work->crtc->dev;
f99d7069 9088 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9089
b4a98e57 9090 mutex_lock(&dev->struct_mutex);
ab8d6675 9091 intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
05394f39 9092 drm_gem_object_unreference(&work->pending_flip_obj->base);
ab8d6675 9093 drm_framebuffer_unreference(work->old_fb);
d9e86c0e 9094
7ff0ebcc 9095 intel_fbc_update(dev);
f06cc1b9
JH
9096
9097 if (work->flip_queued_req)
146d84f0 9098 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
9099 mutex_unlock(&dev->struct_mutex);
9100
f99d7069
DV
9101 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9102
b4a98e57
CW
9103 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9104 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9105
6b95a207
KH
9106 kfree(work);
9107}
9108
1afe3e9d 9109static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9110 struct drm_crtc *crtc)
6b95a207 9111{
6b95a207
KH
9112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9113 struct intel_unpin_work *work;
6b95a207
KH
9114 unsigned long flags;
9115
9116 /* Ignore early vblank irqs */
9117 if (intel_crtc == NULL)
9118 return;
9119
f326038a
DV
9120 /*
9121 * This is called both by irq handlers and the reset code (to complete
9122 * lost pageflips) so needs the full irqsave spinlocks.
9123 */
6b95a207
KH
9124 spin_lock_irqsave(&dev->event_lock, flags);
9125 work = intel_crtc->unpin_work;
e7d841ca
CW
9126
9127 /* Ensure we don't miss a work->pending update ... */
9128 smp_rmb();
9129
9130 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9131 spin_unlock_irqrestore(&dev->event_lock, flags);
9132 return;
9133 }
9134
d6bbafa1 9135 page_flip_completed(intel_crtc);
0af7e4df 9136
6b95a207 9137 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9138}
9139
1afe3e9d
JB
9140void intel_finish_page_flip(struct drm_device *dev, int pipe)
9141{
fbee40df 9142 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9143 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9144
49b14a5c 9145 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9146}
9147
9148void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9149{
fbee40df 9150 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9151 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9152
49b14a5c 9153 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9154}
9155
75f7f3ec
VS
9156/* Is 'a' after or equal to 'b'? */
9157static bool g4x_flip_count_after_eq(u32 a, u32 b)
9158{
9159 return !((a - b) & 0x80000000);
9160}
9161
9162static bool page_flip_finished(struct intel_crtc *crtc)
9163{
9164 struct drm_device *dev = crtc->base.dev;
9165 struct drm_i915_private *dev_priv = dev->dev_private;
9166
bdfa7542
VS
9167 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9168 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9169 return true;
9170
75f7f3ec
VS
9171 /*
9172 * The relevant registers doen't exist on pre-ctg.
9173 * As the flip done interrupt doesn't trigger for mmio
9174 * flips on gmch platforms, a flip count check isn't
9175 * really needed there. But since ctg has the registers,
9176 * include it in the check anyway.
9177 */
9178 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9179 return true;
9180
9181 /*
9182 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9183 * used the same base address. In that case the mmio flip might
9184 * have completed, but the CS hasn't even executed the flip yet.
9185 *
9186 * A flip count check isn't enough as the CS might have updated
9187 * the base address just after start of vblank, but before we
9188 * managed to process the interrupt. This means we'd complete the
9189 * CS flip too soon.
9190 *
9191 * Combining both checks should get us a good enough result. It may
9192 * still happen that the CS flip has been executed, but has not
9193 * yet actually completed. But in case the base address is the same
9194 * anyway, we don't really care.
9195 */
9196 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9197 crtc->unpin_work->gtt_offset &&
9198 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9199 crtc->unpin_work->flip_count);
9200}
9201
6b95a207
KH
9202void intel_prepare_page_flip(struct drm_device *dev, int plane)
9203{
fbee40df 9204 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9205 struct intel_crtc *intel_crtc =
9206 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9207 unsigned long flags;
9208
f326038a
DV
9209
9210 /*
9211 * This is called both by irq handlers and the reset code (to complete
9212 * lost pageflips) so needs the full irqsave spinlocks.
9213 *
9214 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
9215 * generate a page-flip completion irq, i.e. every modeset
9216 * is also accompanied by a spurious intel_prepare_page_flip().
9217 */
6b95a207 9218 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9219 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9220 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9221 spin_unlock_irqrestore(&dev->event_lock, flags);
9222}
9223
eba905b2 9224static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9225{
9226 /* Ensure that the work item is consistent when activating it ... */
9227 smp_wmb();
9228 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9229 /* and that it is marked active as soon as the irq could fire. */
9230 smp_wmb();
9231}
9232
8c9f3aaf
JB
9233static int intel_gen2_queue_flip(struct drm_device *dev,
9234 struct drm_crtc *crtc,
9235 struct drm_framebuffer *fb,
ed8d1975 9236 struct drm_i915_gem_object *obj,
a4872ba6 9237 struct intel_engine_cs *ring,
ed8d1975 9238 uint32_t flags)
8c9f3aaf 9239{
8c9f3aaf 9240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9241 u32 flip_mask;
9242 int ret;
9243
6d90c952 9244 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9245 if (ret)
4fa62c89 9246 return ret;
8c9f3aaf
JB
9247
9248 /* Can't queue multiple flips, so wait for the previous
9249 * one to finish before executing the next.
9250 */
9251 if (intel_crtc->plane)
9252 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9253 else
9254 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9255 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9256 intel_ring_emit(ring, MI_NOOP);
9257 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9258 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9259 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9260 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9261 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9262
9263 intel_mark_page_flip_active(intel_crtc);
09246732 9264 __intel_ring_advance(ring);
83d4092b 9265 return 0;
8c9f3aaf
JB
9266}
9267
9268static int intel_gen3_queue_flip(struct drm_device *dev,
9269 struct drm_crtc *crtc,
9270 struct drm_framebuffer *fb,
ed8d1975 9271 struct drm_i915_gem_object *obj,
a4872ba6 9272 struct intel_engine_cs *ring,
ed8d1975 9273 uint32_t flags)
8c9f3aaf 9274{
8c9f3aaf 9275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9276 u32 flip_mask;
9277 int ret;
9278
6d90c952 9279 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9280 if (ret)
4fa62c89 9281 return ret;
8c9f3aaf
JB
9282
9283 if (intel_crtc->plane)
9284 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9285 else
9286 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9287 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9288 intel_ring_emit(ring, MI_NOOP);
9289 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9290 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9291 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9292 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9293 intel_ring_emit(ring, MI_NOOP);
9294
e7d841ca 9295 intel_mark_page_flip_active(intel_crtc);
09246732 9296 __intel_ring_advance(ring);
83d4092b 9297 return 0;
8c9f3aaf
JB
9298}
9299
9300static int intel_gen4_queue_flip(struct drm_device *dev,
9301 struct drm_crtc *crtc,
9302 struct drm_framebuffer *fb,
ed8d1975 9303 struct drm_i915_gem_object *obj,
a4872ba6 9304 struct intel_engine_cs *ring,
ed8d1975 9305 uint32_t flags)
8c9f3aaf
JB
9306{
9307 struct drm_i915_private *dev_priv = dev->dev_private;
9308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9309 uint32_t pf, pipesrc;
9310 int ret;
9311
6d90c952 9312 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9313 if (ret)
4fa62c89 9314 return ret;
8c9f3aaf
JB
9315
9316 /* i965+ uses the linear or tiled offsets from the
9317 * Display Registers (which do not change across a page-flip)
9318 * so we need only reprogram the base address.
9319 */
6d90c952
DV
9320 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9321 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9322 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9323 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9324 obj->tiling_mode);
8c9f3aaf
JB
9325
9326 /* XXX Enabling the panel-fitter across page-flip is so far
9327 * untested on non-native modes, so ignore it for now.
9328 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9329 */
9330 pf = 0;
9331 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9332 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9333
9334 intel_mark_page_flip_active(intel_crtc);
09246732 9335 __intel_ring_advance(ring);
83d4092b 9336 return 0;
8c9f3aaf
JB
9337}
9338
9339static int intel_gen6_queue_flip(struct drm_device *dev,
9340 struct drm_crtc *crtc,
9341 struct drm_framebuffer *fb,
ed8d1975 9342 struct drm_i915_gem_object *obj,
a4872ba6 9343 struct intel_engine_cs *ring,
ed8d1975 9344 uint32_t flags)
8c9f3aaf
JB
9345{
9346 struct drm_i915_private *dev_priv = dev->dev_private;
9347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9348 uint32_t pf, pipesrc;
9349 int ret;
9350
6d90c952 9351 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9352 if (ret)
4fa62c89 9353 return ret;
8c9f3aaf 9354
6d90c952
DV
9355 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9356 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9357 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9358 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9359
dc257cf1
DV
9360 /* Contrary to the suggestions in the documentation,
9361 * "Enable Panel Fitter" does not seem to be required when page
9362 * flipping with a non-native mode, and worse causes a normal
9363 * modeset to fail.
9364 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9365 */
9366 pf = 0;
8c9f3aaf 9367 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9368 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9369
9370 intel_mark_page_flip_active(intel_crtc);
09246732 9371 __intel_ring_advance(ring);
83d4092b 9372 return 0;
8c9f3aaf
JB
9373}
9374
7c9017e5
JB
9375static int intel_gen7_queue_flip(struct drm_device *dev,
9376 struct drm_crtc *crtc,
9377 struct drm_framebuffer *fb,
ed8d1975 9378 struct drm_i915_gem_object *obj,
a4872ba6 9379 struct intel_engine_cs *ring,
ed8d1975 9380 uint32_t flags)
7c9017e5 9381{
7c9017e5 9382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9383 uint32_t plane_bit = 0;
ffe74d75
CW
9384 int len, ret;
9385
eba905b2 9386 switch (intel_crtc->plane) {
cb05d8de
DV
9387 case PLANE_A:
9388 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9389 break;
9390 case PLANE_B:
9391 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9392 break;
9393 case PLANE_C:
9394 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9395 break;
9396 default:
9397 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9398 return -ENODEV;
cb05d8de
DV
9399 }
9400
ffe74d75 9401 len = 4;
f476828a 9402 if (ring->id == RCS) {
ffe74d75 9403 len += 6;
f476828a
DL
9404 /*
9405 * On Gen 8, SRM is now taking an extra dword to accommodate
9406 * 48bits addresses, and we need a NOOP for the batch size to
9407 * stay even.
9408 */
9409 if (IS_GEN8(dev))
9410 len += 2;
9411 }
ffe74d75 9412
f66fab8e
VS
9413 /*
9414 * BSpec MI_DISPLAY_FLIP for IVB:
9415 * "The full packet must be contained within the same cache line."
9416 *
9417 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9418 * cacheline, if we ever start emitting more commands before
9419 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9420 * then do the cacheline alignment, and finally emit the
9421 * MI_DISPLAY_FLIP.
9422 */
9423 ret = intel_ring_cacheline_align(ring);
9424 if (ret)
4fa62c89 9425 return ret;
f66fab8e 9426
ffe74d75 9427 ret = intel_ring_begin(ring, len);
7c9017e5 9428 if (ret)
4fa62c89 9429 return ret;
7c9017e5 9430
ffe74d75
CW
9431 /* Unmask the flip-done completion message. Note that the bspec says that
9432 * we should do this for both the BCS and RCS, and that we must not unmask
9433 * more than one flip event at any time (or ensure that one flip message
9434 * can be sent by waiting for flip-done prior to queueing new flips).
9435 * Experimentation says that BCS works despite DERRMR masking all
9436 * flip-done completion events and that unmasking all planes at once
9437 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9438 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9439 */
9440 if (ring->id == RCS) {
9441 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9442 intel_ring_emit(ring, DERRMR);
9443 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9444 DERRMR_PIPEB_PRI_FLIP_DONE |
9445 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9446 if (IS_GEN8(dev))
9447 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9448 MI_SRM_LRM_GLOBAL_GTT);
9449 else
9450 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9451 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9452 intel_ring_emit(ring, DERRMR);
9453 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9454 if (IS_GEN8(dev)) {
9455 intel_ring_emit(ring, 0);
9456 intel_ring_emit(ring, MI_NOOP);
9457 }
ffe74d75
CW
9458 }
9459
cb05d8de 9460 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9461 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9462 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9463 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9464
9465 intel_mark_page_flip_active(intel_crtc);
09246732 9466 __intel_ring_advance(ring);
83d4092b 9467 return 0;
7c9017e5
JB
9468}
9469
84c33a64
SG
9470static bool use_mmio_flip(struct intel_engine_cs *ring,
9471 struct drm_i915_gem_object *obj)
9472{
9473 /*
9474 * This is not being used for older platforms, because
9475 * non-availability of flip done interrupt forces us to use
9476 * CS flips. Older platforms derive flip done using some clever
9477 * tricks involving the flip_pending status bits and vblank irqs.
9478 * So using MMIO flips there would disrupt this mechanism.
9479 */
9480
8e09bf83
CW
9481 if (ring == NULL)
9482 return true;
9483
84c33a64
SG
9484 if (INTEL_INFO(ring->dev)->gen < 5)
9485 return false;
9486
9487 if (i915.use_mmio_flip < 0)
9488 return false;
9489 else if (i915.use_mmio_flip > 0)
9490 return true;
14bf993e
OM
9491 else if (i915.enable_execlists)
9492 return true;
84c33a64 9493 else
41c52415 9494 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
9495}
9496
ff944564
DL
9497static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9498{
9499 struct drm_device *dev = intel_crtc->base.dev;
9500 struct drm_i915_private *dev_priv = dev->dev_private;
9501 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9502 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9503 struct drm_i915_gem_object *obj = intel_fb->obj;
9504 const enum pipe pipe = intel_crtc->pipe;
9505 u32 ctl, stride;
9506
9507 ctl = I915_READ(PLANE_CTL(pipe, 0));
9508 ctl &= ~PLANE_CTL_TILED_MASK;
9509 if (obj->tiling_mode == I915_TILING_X)
9510 ctl |= PLANE_CTL_TILED_X;
9511
9512 /*
9513 * The stride is either expressed as a multiple of 64 bytes chunks for
9514 * linear buffers or in number of tiles for tiled buffers.
9515 */
9516 stride = fb->pitches[0] >> 6;
9517 if (obj->tiling_mode == I915_TILING_X)
9518 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9519
9520 /*
9521 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9522 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9523 */
9524 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9525 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9526
9527 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9528 POSTING_READ(PLANE_SURF(pipe, 0));
9529}
9530
9531static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
9532{
9533 struct drm_device *dev = intel_crtc->base.dev;
9534 struct drm_i915_private *dev_priv = dev->dev_private;
9535 struct intel_framebuffer *intel_fb =
9536 to_intel_framebuffer(intel_crtc->base.primary->fb);
9537 struct drm_i915_gem_object *obj = intel_fb->obj;
9538 u32 dspcntr;
9539 u32 reg;
9540
84c33a64
SG
9541 reg = DSPCNTR(intel_crtc->plane);
9542 dspcntr = I915_READ(reg);
9543
c5d97472
DL
9544 if (obj->tiling_mode != I915_TILING_NONE)
9545 dspcntr |= DISPPLANE_TILED;
9546 else
9547 dspcntr &= ~DISPPLANE_TILED;
9548
84c33a64
SG
9549 I915_WRITE(reg, dspcntr);
9550
9551 I915_WRITE(DSPSURF(intel_crtc->plane),
9552 intel_crtc->unpin_work->gtt_offset);
9553 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 9554
ff944564
DL
9555}
9556
9557/*
9558 * XXX: This is the temporary way to update the plane registers until we get
9559 * around to using the usual plane update functions for MMIO flips
9560 */
9561static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9562{
9563 struct drm_device *dev = intel_crtc->base.dev;
9564 bool atomic_update;
9565 u32 start_vbl_count;
9566
9567 intel_mark_page_flip_active(intel_crtc);
9568
9569 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9570
9571 if (INTEL_INFO(dev)->gen >= 9)
9572 skl_do_mmio_flip(intel_crtc);
9573 else
9574 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9575 ilk_do_mmio_flip(intel_crtc);
9576
9362c7c5
ACO
9577 if (atomic_update)
9578 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
9579}
9580
9362c7c5 9581static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 9582{
cc8c4cc2 9583 struct intel_crtc *crtc =
9362c7c5 9584 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 9585 struct intel_mmio_flip *mmio_flip;
84c33a64 9586
cc8c4cc2
JH
9587 mmio_flip = &crtc->mmio_flip;
9588 if (mmio_flip->req)
9c654818
JH
9589 WARN_ON(__i915_wait_request(mmio_flip->req,
9590 crtc->reset_counter,
9591 false, NULL, NULL) != 0);
84c33a64 9592
cc8c4cc2
JH
9593 intel_do_mmio_flip(crtc);
9594 if (mmio_flip->req) {
9595 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 9596 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
9597 mutex_unlock(&crtc->base.dev->struct_mutex);
9598 }
84c33a64
SG
9599}
9600
9601static int intel_queue_mmio_flip(struct drm_device *dev,
9602 struct drm_crtc *crtc,
9603 struct drm_framebuffer *fb,
9604 struct drm_i915_gem_object *obj,
9605 struct intel_engine_cs *ring,
9606 uint32_t flags)
9607{
84c33a64 9608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 9609
cc8c4cc2
JH
9610 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9611 obj->last_write_req);
536f5b5e
ACO
9612
9613 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 9614
84c33a64
SG
9615 return 0;
9616}
9617
830c81db
DL
9618static int intel_gen9_queue_flip(struct drm_device *dev,
9619 struct drm_crtc *crtc,
9620 struct drm_framebuffer *fb,
9621 struct drm_i915_gem_object *obj,
9622 struct intel_engine_cs *ring,
9623 uint32_t flags)
9624{
9625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9626 uint32_t plane = 0, stride;
9627 int ret;
9628
9629 switch(intel_crtc->pipe) {
9630 case PIPE_A:
9631 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
9632 break;
9633 case PIPE_B:
9634 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
9635 break;
9636 case PIPE_C:
9637 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
9638 break;
9639 default:
9640 WARN_ONCE(1, "unknown plane in flip command\n");
9641 return -ENODEV;
9642 }
9643
9644 switch (obj->tiling_mode) {
9645 case I915_TILING_NONE:
9646 stride = fb->pitches[0] >> 6;
9647 break;
9648 case I915_TILING_X:
9649 stride = fb->pitches[0] >> 9;
9650 break;
9651 default:
9652 WARN_ONCE(1, "unknown tiling in flip command\n");
9653 return -ENODEV;
9654 }
9655
9656 ret = intel_ring_begin(ring, 10);
9657 if (ret)
9658 return ret;
9659
9660 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9661 intel_ring_emit(ring, DERRMR);
9662 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9663 DERRMR_PIPEB_PRI_FLIP_DONE |
9664 DERRMR_PIPEC_PRI_FLIP_DONE));
9665 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9666 MI_SRM_LRM_GLOBAL_GTT);
9667 intel_ring_emit(ring, DERRMR);
9668 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9669 intel_ring_emit(ring, 0);
9670
9671 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
9672 intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
9673 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9674
9675 intel_mark_page_flip_active(intel_crtc);
9676 __intel_ring_advance(ring);
9677
9678 return 0;
9679}
9680
8c9f3aaf
JB
9681static int intel_default_queue_flip(struct drm_device *dev,
9682 struct drm_crtc *crtc,
9683 struct drm_framebuffer *fb,
ed8d1975 9684 struct drm_i915_gem_object *obj,
a4872ba6 9685 struct intel_engine_cs *ring,
ed8d1975 9686 uint32_t flags)
8c9f3aaf
JB
9687{
9688 return -ENODEV;
9689}
9690
d6bbafa1
CW
9691static bool __intel_pageflip_stall_check(struct drm_device *dev,
9692 struct drm_crtc *crtc)
9693{
9694 struct drm_i915_private *dev_priv = dev->dev_private;
9695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9696 struct intel_unpin_work *work = intel_crtc->unpin_work;
9697 u32 addr;
9698
9699 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9700 return true;
9701
9702 if (!work->enable_stall_check)
9703 return false;
9704
9705 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
9706 if (work->flip_queued_req &&
9707 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
9708 return false;
9709
9710 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9711 }
9712
9713 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9714 return false;
9715
9716 /* Potential stall - if we see that the flip has happened,
9717 * assume a missed interrupt. */
9718 if (INTEL_INFO(dev)->gen >= 4)
9719 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9720 else
9721 addr = I915_READ(DSPADDR(intel_crtc->plane));
9722
9723 /* There is a potential issue here with a false positive after a flip
9724 * to the same address. We could address this by checking for a
9725 * non-incrementing frame counter.
9726 */
9727 return addr == work->gtt_offset;
9728}
9729
9730void intel_check_page_flip(struct drm_device *dev, int pipe)
9731{
9732 struct drm_i915_private *dev_priv = dev->dev_private;
9733 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f326038a
DV
9735
9736 WARN_ON(!in_irq());
d6bbafa1
CW
9737
9738 if (crtc == NULL)
9739 return;
9740
f326038a 9741 spin_lock(&dev->event_lock);
d6bbafa1
CW
9742 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9743 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9744 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9745 page_flip_completed(intel_crtc);
9746 }
f326038a 9747 spin_unlock(&dev->event_lock);
d6bbafa1
CW
9748}
9749
6b95a207
KH
9750static int intel_crtc_page_flip(struct drm_crtc *crtc,
9751 struct drm_framebuffer *fb,
ed8d1975
KP
9752 struct drm_pending_vblank_event *event,
9753 uint32_t page_flip_flags)
6b95a207
KH
9754{
9755 struct drm_device *dev = crtc->dev;
9756 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9757 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9758 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 9760 struct drm_plane *primary = crtc->primary;
a071fa00 9761 enum pipe pipe = intel_crtc->pipe;
6b95a207 9762 struct intel_unpin_work *work;
a4872ba6 9763 struct intel_engine_cs *ring;
52e68630 9764 int ret;
6b95a207 9765
2ff8fde1
MR
9766 /*
9767 * drm_mode_page_flip_ioctl() should already catch this, but double
9768 * check to be safe. In the future we may enable pageflipping from
9769 * a disabled primary plane.
9770 */
9771 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9772 return -EBUSY;
9773
e6a595d2 9774 /* Can't change pixel format via MI display flips. */
f4510a27 9775 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9776 return -EINVAL;
9777
9778 /*
9779 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9780 * Note that pitch changes could also affect these register.
9781 */
9782 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9783 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9784 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9785 return -EINVAL;
9786
f900db47
CW
9787 if (i915_terminally_wedged(&dev_priv->gpu_error))
9788 goto out_hang;
9789
b14c5679 9790 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9791 if (work == NULL)
9792 return -ENOMEM;
9793
6b95a207 9794 work->event = event;
b4a98e57 9795 work->crtc = crtc;
ab8d6675 9796 work->old_fb = old_fb;
6b95a207
KH
9797 INIT_WORK(&work->work, intel_unpin_work_fn);
9798
87b6b101 9799 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9800 if (ret)
9801 goto free_work;
9802
6b95a207 9803 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 9804 spin_lock_irq(&dev->event_lock);
6b95a207 9805 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9806 /* Before declaring the flip queue wedged, check if
9807 * the hardware completed the operation behind our backs.
9808 */
9809 if (__intel_pageflip_stall_check(dev, crtc)) {
9810 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9811 page_flip_completed(intel_crtc);
9812 } else {
9813 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 9814 spin_unlock_irq(&dev->event_lock);
468f0b44 9815
d6bbafa1
CW
9816 drm_crtc_vblank_put(crtc);
9817 kfree(work);
9818 return -EBUSY;
9819 }
6b95a207
KH
9820 }
9821 intel_crtc->unpin_work = work;
5e2d7afc 9822 spin_unlock_irq(&dev->event_lock);
6b95a207 9823
b4a98e57
CW
9824 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9825 flush_workqueue(dev_priv->wq);
9826
79158103
CW
9827 ret = i915_mutex_lock_interruptible(dev);
9828 if (ret)
9829 goto cleanup;
6b95a207 9830
75dfca80 9831 /* Reference the objects for the scheduled work. */
ab8d6675 9832 drm_framebuffer_reference(work->old_fb);
05394f39 9833 drm_gem_object_reference(&obj->base);
6b95a207 9834
f4510a27 9835 crtc->primary->fb = fb;
afd65eb4 9836 update_state_fb(crtc->primary);
1ed1f968 9837
e1f99ce6 9838 work->pending_flip_obj = obj;
e1f99ce6 9839
b4a98e57 9840 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9841 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9842
75f7f3ec 9843 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9844 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9845
4fa62c89
VS
9846 if (IS_VALLEYVIEW(dev)) {
9847 ring = &dev_priv->ring[BCS];
ab8d6675 9848 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
9849 /* vlv: DISPLAY_FLIP fails to change tiling */
9850 ring = NULL;
48bf5b2d 9851 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 9852 ring = &dev_priv->ring[BCS];
4fa62c89 9853 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 9854 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
9855 if (ring == NULL || ring->id != RCS)
9856 ring = &dev_priv->ring[BCS];
9857 } else {
9858 ring = &dev_priv->ring[RCS];
9859 }
9860
850c4cdc 9861 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
8c9f3aaf
JB
9862 if (ret)
9863 goto cleanup_pending;
6b95a207 9864
4fa62c89
VS
9865 work->gtt_offset =
9866 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9867
d6bbafa1 9868 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
9869 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9870 page_flip_flags);
d6bbafa1
CW
9871 if (ret)
9872 goto cleanup_unpin;
9873
f06cc1b9
JH
9874 i915_gem_request_assign(&work->flip_queued_req,
9875 obj->last_write_req);
d6bbafa1 9876 } else {
84c33a64 9877 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
9878 page_flip_flags);
9879 if (ret)
9880 goto cleanup_unpin;
9881
f06cc1b9
JH
9882 i915_gem_request_assign(&work->flip_queued_req,
9883 intel_ring_get_request(ring));
d6bbafa1
CW
9884 }
9885
9886 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9887 work->enable_stall_check = true;
4fa62c89 9888
ab8d6675 9889 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
9890 INTEL_FRONTBUFFER_PRIMARY(pipe));
9891
7ff0ebcc 9892 intel_fbc_disable(dev);
f99d7069 9893 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9894 mutex_unlock(&dev->struct_mutex);
9895
e5510fac
JB
9896 trace_i915_flip_request(intel_crtc->plane, obj);
9897
6b95a207 9898 return 0;
96b099fd 9899
4fa62c89
VS
9900cleanup_unpin:
9901 intel_unpin_fb_obj(obj);
8c9f3aaf 9902cleanup_pending:
b4a98e57 9903 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9904 crtc->primary->fb = old_fb;
afd65eb4 9905 update_state_fb(crtc->primary);
ab8d6675 9906 drm_framebuffer_unreference(work->old_fb);
05394f39 9907 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9908 mutex_unlock(&dev->struct_mutex);
9909
79158103 9910cleanup:
5e2d7afc 9911 spin_lock_irq(&dev->event_lock);
96b099fd 9912 intel_crtc->unpin_work = NULL;
5e2d7afc 9913 spin_unlock_irq(&dev->event_lock);
96b099fd 9914
87b6b101 9915 drm_crtc_vblank_put(crtc);
7317c75e 9916free_work:
96b099fd
CW
9917 kfree(work);
9918
f900db47
CW
9919 if (ret == -EIO) {
9920out_hang:
53a366b9 9921 ret = intel_plane_restore(primary);
f0d3dad3 9922 if (ret == 0 && event) {
5e2d7afc 9923 spin_lock_irq(&dev->event_lock);
a071fa00 9924 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 9925 spin_unlock_irq(&dev->event_lock);
f0d3dad3 9926 }
f900db47 9927 }
96b099fd 9928 return ret;
6b95a207
KH
9929}
9930
f6e5b160 9931static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9932 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9933 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
9934 .atomic_begin = intel_begin_crtc_commit,
9935 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
9936};
9937
9a935856
DV
9938/**
9939 * intel_modeset_update_staged_output_state
9940 *
9941 * Updates the staged output configuration state, e.g. after we've read out the
9942 * current hw state.
9943 */
9944static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9945{
7668851f 9946 struct intel_crtc *crtc;
9a935856
DV
9947 struct intel_encoder *encoder;
9948 struct intel_connector *connector;
f6e5b160 9949
9a935856
DV
9950 list_for_each_entry(connector, &dev->mode_config.connector_list,
9951 base.head) {
9952 connector->new_encoder =
9953 to_intel_encoder(connector->base.encoder);
9954 }
f6e5b160 9955
b2784e15 9956 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9957 encoder->new_crtc =
9958 to_intel_crtc(encoder->base.crtc);
9959 }
7668851f 9960
d3fcc808 9961 for_each_intel_crtc(dev, crtc) {
7668851f 9962 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9963
9964 if (crtc->new_enabled)
6e3c9717 9965 crtc->new_config = crtc->config;
7bd0a8e7
VS
9966 else
9967 crtc->new_config = NULL;
7668851f 9968 }
f6e5b160
CW
9969}
9970
9a935856
DV
9971/**
9972 * intel_modeset_commit_output_state
9973 *
9974 * This function copies the stage display pipe configuration to the real one.
9975 */
9976static void intel_modeset_commit_output_state(struct drm_device *dev)
9977{
7668851f 9978 struct intel_crtc *crtc;
9a935856
DV
9979 struct intel_encoder *encoder;
9980 struct intel_connector *connector;
f6e5b160 9981
9a935856
DV
9982 list_for_each_entry(connector, &dev->mode_config.connector_list,
9983 base.head) {
9984 connector->base.encoder = &connector->new_encoder->base;
9985 }
f6e5b160 9986
b2784e15 9987 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9988 encoder->base.crtc = &encoder->new_crtc->base;
9989 }
7668851f 9990
d3fcc808 9991 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9992 crtc->base.enabled = crtc->new_enabled;
9993 }
9a935856
DV
9994}
9995
050f7aeb 9996static void
eba905b2 9997connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 9998 struct intel_crtc_state *pipe_config)
050f7aeb
DV
9999{
10000 int bpp = pipe_config->pipe_bpp;
10001
10002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10003 connector->base.base.id,
c23cc417 10004 connector->base.name);
050f7aeb
DV
10005
10006 /* Don't use an invalid EDID bpc value */
10007 if (connector->base.display_info.bpc &&
10008 connector->base.display_info.bpc * 3 < bpp) {
10009 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10010 bpp, connector->base.display_info.bpc*3);
10011 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10012 }
10013
10014 /* Clamp bpp to 8 on screens without EDID 1.4 */
10015 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10016 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10017 bpp);
10018 pipe_config->pipe_bpp = 24;
10019 }
10020}
10021
4e53c2e0 10022static int
050f7aeb
DV
10023compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10024 struct drm_framebuffer *fb,
5cec258b 10025 struct intel_crtc_state *pipe_config)
4e53c2e0 10026{
050f7aeb
DV
10027 struct drm_device *dev = crtc->base.dev;
10028 struct intel_connector *connector;
4e53c2e0
DV
10029 int bpp;
10030
d42264b1
DV
10031 switch (fb->pixel_format) {
10032 case DRM_FORMAT_C8:
4e53c2e0
DV
10033 bpp = 8*3; /* since we go through a colormap */
10034 break;
d42264b1
DV
10035 case DRM_FORMAT_XRGB1555:
10036 case DRM_FORMAT_ARGB1555:
10037 /* checked in intel_framebuffer_init already */
10038 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10039 return -EINVAL;
10040 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10041 bpp = 6*3; /* min is 18bpp */
10042 break;
d42264b1
DV
10043 case DRM_FORMAT_XBGR8888:
10044 case DRM_FORMAT_ABGR8888:
10045 /* checked in intel_framebuffer_init already */
10046 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10047 return -EINVAL;
10048 case DRM_FORMAT_XRGB8888:
10049 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10050 bpp = 8*3;
10051 break;
d42264b1
DV
10052 case DRM_FORMAT_XRGB2101010:
10053 case DRM_FORMAT_ARGB2101010:
10054 case DRM_FORMAT_XBGR2101010:
10055 case DRM_FORMAT_ABGR2101010:
10056 /* checked in intel_framebuffer_init already */
10057 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10058 return -EINVAL;
4e53c2e0
DV
10059 bpp = 10*3;
10060 break;
baba133a 10061 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10062 default:
10063 DRM_DEBUG_KMS("unsupported depth\n");
10064 return -EINVAL;
10065 }
10066
4e53c2e0
DV
10067 pipe_config->pipe_bpp = bpp;
10068
10069 /* Clamp display bpp to EDID value */
10070 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10071 base.head) {
1b829e05
DV
10072 if (!connector->new_encoder ||
10073 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10074 continue;
10075
050f7aeb 10076 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10077 }
10078
10079 return bpp;
10080}
10081
644db711
DV
10082static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10083{
10084 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10085 "type: 0x%x flags: 0x%x\n",
1342830c 10086 mode->crtc_clock,
644db711
DV
10087 mode->crtc_hdisplay, mode->crtc_hsync_start,
10088 mode->crtc_hsync_end, mode->crtc_htotal,
10089 mode->crtc_vdisplay, mode->crtc_vsync_start,
10090 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10091}
10092
c0b03411 10093static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 10094 struct intel_crtc_state *pipe_config,
c0b03411
DV
10095 const char *context)
10096{
10097 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10098 context, pipe_name(crtc->pipe));
10099
10100 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10101 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10102 pipe_config->pipe_bpp, pipe_config->dither);
10103 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10104 pipe_config->has_pch_encoder,
10105 pipe_config->fdi_lanes,
10106 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10107 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10108 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10109 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10110 pipe_config->has_dp_encoder,
10111 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10112 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10113 pipe_config->dp_m_n.tu);
b95af8be
VK
10114
10115 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10116 pipe_config->has_dp_encoder,
10117 pipe_config->dp_m2_n2.gmch_m,
10118 pipe_config->dp_m2_n2.gmch_n,
10119 pipe_config->dp_m2_n2.link_m,
10120 pipe_config->dp_m2_n2.link_n,
10121 pipe_config->dp_m2_n2.tu);
10122
55072d19
DV
10123 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10124 pipe_config->has_audio,
10125 pipe_config->has_infoframe);
10126
c0b03411 10127 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 10128 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 10129 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
10130 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10131 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 10132 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10133 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10134 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10135 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10136 pipe_config->gmch_pfit.control,
10137 pipe_config->gmch_pfit.pgm_ratios,
10138 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10139 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10140 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10141 pipe_config->pch_pfit.size,
10142 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10143 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10144 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10145}
10146
bc079e8b
VS
10147static bool encoders_cloneable(const struct intel_encoder *a,
10148 const struct intel_encoder *b)
accfc0c5 10149{
bc079e8b
VS
10150 /* masks could be asymmetric, so check both ways */
10151 return a == b || (a->cloneable & (1 << b->type) &&
10152 b->cloneable & (1 << a->type));
10153}
10154
10155static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10156 struct intel_encoder *encoder)
10157{
10158 struct drm_device *dev = crtc->base.dev;
10159 struct intel_encoder *source_encoder;
10160
b2784e15 10161 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10162 if (source_encoder->new_crtc != crtc)
10163 continue;
10164
10165 if (!encoders_cloneable(encoder, source_encoder))
10166 return false;
10167 }
10168
10169 return true;
10170}
10171
10172static bool check_encoder_cloning(struct intel_crtc *crtc)
10173{
10174 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10175 struct intel_encoder *encoder;
10176
b2784e15 10177 for_each_intel_encoder(dev, encoder) {
bc079e8b 10178 if (encoder->new_crtc != crtc)
accfc0c5
DV
10179 continue;
10180
bc079e8b
VS
10181 if (!check_single_encoder_cloning(crtc, encoder))
10182 return false;
accfc0c5
DV
10183 }
10184
bc079e8b 10185 return true;
accfc0c5
DV
10186}
10187
00f0b378
VS
10188static bool check_digital_port_conflicts(struct drm_device *dev)
10189{
10190 struct intel_connector *connector;
10191 unsigned int used_ports = 0;
10192
10193 /*
10194 * Walk the connector list instead of the encoder
10195 * list to detect the problem on ddi platforms
10196 * where there's just one encoder per digital port.
10197 */
10198 list_for_each_entry(connector,
10199 &dev->mode_config.connector_list, base.head) {
10200 struct intel_encoder *encoder = connector->new_encoder;
10201
10202 if (!encoder)
10203 continue;
10204
10205 WARN_ON(!encoder->new_crtc);
10206
10207 switch (encoder->type) {
10208 unsigned int port_mask;
10209 case INTEL_OUTPUT_UNKNOWN:
10210 if (WARN_ON(!HAS_DDI(dev)))
10211 break;
10212 case INTEL_OUTPUT_DISPLAYPORT:
10213 case INTEL_OUTPUT_HDMI:
10214 case INTEL_OUTPUT_EDP:
10215 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10216
10217 /* the same port mustn't appear more than once */
10218 if (used_ports & port_mask)
10219 return false;
10220
10221 used_ports |= port_mask;
10222 default:
10223 break;
10224 }
10225 }
10226
10227 return true;
10228}
10229
5cec258b 10230static struct intel_crtc_state *
b8cecdf5 10231intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10232 struct drm_framebuffer *fb,
b8cecdf5 10233 struct drm_display_mode *mode)
ee7b9f93 10234{
7758a113 10235 struct drm_device *dev = crtc->dev;
7758a113 10236 struct intel_encoder *encoder;
5cec258b 10237 struct intel_crtc_state *pipe_config;
e29c22c0
DV
10238 int plane_bpp, ret = -EINVAL;
10239 bool retry = true;
ee7b9f93 10240
bc079e8b 10241 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10242 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10243 return ERR_PTR(-EINVAL);
10244 }
10245
00f0b378
VS
10246 if (!check_digital_port_conflicts(dev)) {
10247 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10248 return ERR_PTR(-EINVAL);
10249 }
10250
b8cecdf5
DV
10251 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10252 if (!pipe_config)
7758a113
DV
10253 return ERR_PTR(-ENOMEM);
10254
2d112de7
ACO
10255 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10256 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 10257
e143a21c
DV
10258 pipe_config->cpu_transcoder =
10259 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10260 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10261
2960bc9c
ID
10262 /*
10263 * Sanitize sync polarity flags based on requested ones. If neither
10264 * positive or negative polarity is requested, treat this as meaning
10265 * negative polarity.
10266 */
2d112de7 10267 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10268 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 10269 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 10270
2d112de7 10271 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10272 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 10273 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 10274
050f7aeb
DV
10275 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10276 * plane pixel format and any sink constraints into account. Returns the
10277 * source plane bpp so that dithering can be selected on mismatches
10278 * after encoders and crtc also have had their say. */
10279 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10280 fb, pipe_config);
4e53c2e0
DV
10281 if (plane_bpp < 0)
10282 goto fail;
10283
e41a56be
VS
10284 /*
10285 * Determine the real pipe dimensions. Note that stereo modes can
10286 * increase the actual pipe size due to the frame doubling and
10287 * insertion of additional space for blanks between the frame. This
10288 * is stored in the crtc timings. We use the requested mode to do this
10289 * computation to clearly distinguish it from the adjusted mode, which
10290 * can be changed by the connectors in the below retry loop.
10291 */
2d112de7 10292 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
10293 &pipe_config->pipe_src_w,
10294 &pipe_config->pipe_src_h);
e41a56be 10295
e29c22c0 10296encoder_retry:
ef1b460d 10297 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10298 pipe_config->port_clock = 0;
ef1b460d 10299 pipe_config->pixel_multiplier = 1;
ff9a6750 10300
135c81b8 10301 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
10302 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10303 CRTC_STEREO_DOUBLE);
135c81b8 10304
7758a113
DV
10305 /* Pass our mode to the connectors and the CRTC to give them a chance to
10306 * adjust it according to limitations or connector properties, and also
10307 * a chance to reject the mode entirely.
47f1c6c9 10308 */
b2784e15 10309 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10310
7758a113
DV
10311 if (&encoder->new_crtc->base != crtc)
10312 continue;
7ae89233 10313
efea6e8e
DV
10314 if (!(encoder->compute_config(encoder, pipe_config))) {
10315 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10316 goto fail;
10317 }
ee7b9f93 10318 }
47f1c6c9 10319
ff9a6750
DV
10320 /* Set default port clock if not overwritten by the encoder. Needs to be
10321 * done afterwards in case the encoder adjusts the mode. */
10322 if (!pipe_config->port_clock)
2d112de7 10323 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 10324 * pipe_config->pixel_multiplier;
ff9a6750 10325
a43f6e0f 10326 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10327 if (ret < 0) {
7758a113
DV
10328 DRM_DEBUG_KMS("CRTC fixup failed\n");
10329 goto fail;
ee7b9f93 10330 }
e29c22c0
DV
10331
10332 if (ret == RETRY) {
10333 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10334 ret = -EINVAL;
10335 goto fail;
10336 }
10337
10338 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10339 retry = false;
10340 goto encoder_retry;
10341 }
10342
4e53c2e0
DV
10343 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10344 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10345 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10346
b8cecdf5 10347 return pipe_config;
7758a113 10348fail:
b8cecdf5 10349 kfree(pipe_config);
e29c22c0 10350 return ERR_PTR(ret);
ee7b9f93 10351}
47f1c6c9 10352
e2e1ed41
DV
10353/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10354 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10355static void
10356intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10357 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10358{
10359 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10360 struct drm_device *dev = crtc->dev;
10361 struct intel_encoder *encoder;
10362 struct intel_connector *connector;
10363 struct drm_crtc *tmp_crtc;
79e53945 10364
e2e1ed41 10365 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10366
e2e1ed41
DV
10367 /* Check which crtcs have changed outputs connected to them, these need
10368 * to be part of the prepare_pipes mask. We don't (yet) support global
10369 * modeset across multiple crtcs, so modeset_pipes will only have one
10370 * bit set at most. */
10371 list_for_each_entry(connector, &dev->mode_config.connector_list,
10372 base.head) {
10373 if (connector->base.encoder == &connector->new_encoder->base)
10374 continue;
79e53945 10375
e2e1ed41
DV
10376 if (connector->base.encoder) {
10377 tmp_crtc = connector->base.encoder->crtc;
10378
10379 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10380 }
10381
10382 if (connector->new_encoder)
10383 *prepare_pipes |=
10384 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10385 }
10386
b2784e15 10387 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10388 if (encoder->base.crtc == &encoder->new_crtc->base)
10389 continue;
10390
10391 if (encoder->base.crtc) {
10392 tmp_crtc = encoder->base.crtc;
10393
10394 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10395 }
10396
10397 if (encoder->new_crtc)
10398 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10399 }
10400
7668851f 10401 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10402 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10403 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10404 continue;
7e7d76c3 10405
7668851f 10406 if (!intel_crtc->new_enabled)
e2e1ed41 10407 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10408 else
10409 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10410 }
10411
e2e1ed41
DV
10412
10413 /* set_mode is also used to update properties on life display pipes. */
10414 intel_crtc = to_intel_crtc(crtc);
7668851f 10415 if (intel_crtc->new_enabled)
e2e1ed41
DV
10416 *prepare_pipes |= 1 << intel_crtc->pipe;
10417
b6c5164d
DV
10418 /*
10419 * For simplicity do a full modeset on any pipe where the output routing
10420 * changed. We could be more clever, but that would require us to be
10421 * more careful with calling the relevant encoder->mode_set functions.
10422 */
e2e1ed41
DV
10423 if (*prepare_pipes)
10424 *modeset_pipes = *prepare_pipes;
10425
10426 /* ... and mask these out. */
10427 *modeset_pipes &= ~(*disable_pipes);
10428 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10429
10430 /*
10431 * HACK: We don't (yet) fully support global modesets. intel_set_config
10432 * obies this rule, but the modeset restore mode of
10433 * intel_modeset_setup_hw_state does not.
10434 */
10435 *modeset_pipes &= 1 << intel_crtc->pipe;
10436 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10437
10438 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10439 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10440}
79e53945 10441
ea9d758d 10442static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10443{
ea9d758d 10444 struct drm_encoder *encoder;
f6e5b160 10445 struct drm_device *dev = crtc->dev;
f6e5b160 10446
ea9d758d
DV
10447 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10448 if (encoder->crtc == crtc)
10449 return true;
10450
10451 return false;
10452}
10453
10454static void
10455intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10456{
ba41c0de 10457 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
10458 struct intel_encoder *intel_encoder;
10459 struct intel_crtc *intel_crtc;
10460 struct drm_connector *connector;
10461
ba41c0de
DV
10462 intel_shared_dpll_commit(dev_priv);
10463
b2784e15 10464 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10465 if (!intel_encoder->base.crtc)
10466 continue;
10467
10468 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10469
10470 if (prepare_pipes & (1 << intel_crtc->pipe))
10471 intel_encoder->connectors_active = false;
10472 }
10473
10474 intel_modeset_commit_output_state(dev);
10475
7668851f 10476 /* Double check state. */
d3fcc808 10477 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10478 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7 10479 WARN_ON(intel_crtc->new_config &&
6e3c9717 10480 intel_crtc->new_config != intel_crtc->config);
7bd0a8e7 10481 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10482 }
10483
10484 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10485 if (!connector->encoder || !connector->encoder->crtc)
10486 continue;
10487
10488 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10489
10490 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10491 struct drm_property *dpms_property =
10492 dev->mode_config.dpms_property;
10493
ea9d758d 10494 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10495 drm_object_property_set_value(&connector->base,
68d34720
DV
10496 dpms_property,
10497 DRM_MODE_DPMS_ON);
ea9d758d
DV
10498
10499 intel_encoder = to_intel_encoder(connector->encoder);
10500 intel_encoder->connectors_active = true;
10501 }
10502 }
10503
10504}
10505
3bd26263 10506static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10507{
3bd26263 10508 int diff;
f1f644dc
JB
10509
10510 if (clock1 == clock2)
10511 return true;
10512
10513 if (!clock1 || !clock2)
10514 return false;
10515
10516 diff = abs(clock1 - clock2);
10517
10518 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10519 return true;
10520
10521 return false;
10522}
10523
25c5b266
DV
10524#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10525 list_for_each_entry((intel_crtc), \
10526 &(dev)->mode_config.crtc_list, \
10527 base.head) \
0973f18f 10528 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10529
0e8ffe1b 10530static bool
2fa2fe9a 10531intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
10532 struct intel_crtc_state *current_config,
10533 struct intel_crtc_state *pipe_config)
0e8ffe1b 10534{
66e985c0
DV
10535#define PIPE_CONF_CHECK_X(name) \
10536 if (current_config->name != pipe_config->name) { \
10537 DRM_ERROR("mismatch in " #name " " \
10538 "(expected 0x%08x, found 0x%08x)\n", \
10539 current_config->name, \
10540 pipe_config->name); \
10541 return false; \
10542 }
10543
08a24034
DV
10544#define PIPE_CONF_CHECK_I(name) \
10545 if (current_config->name != pipe_config->name) { \
10546 DRM_ERROR("mismatch in " #name " " \
10547 "(expected %i, found %i)\n", \
10548 current_config->name, \
10549 pipe_config->name); \
10550 return false; \
88adfff1
DV
10551 }
10552
b95af8be
VK
10553/* This is required for BDW+ where there is only one set of registers for
10554 * switching between high and low RR.
10555 * This macro can be used whenever a comparison has to be made between one
10556 * hw state and multiple sw state variables.
10557 */
10558#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10559 if ((current_config->name != pipe_config->name) && \
10560 (current_config->alt_name != pipe_config->name)) { \
10561 DRM_ERROR("mismatch in " #name " " \
10562 "(expected %i or %i, found %i)\n", \
10563 current_config->name, \
10564 current_config->alt_name, \
10565 pipe_config->name); \
10566 return false; \
10567 }
10568
1bd1bd80
DV
10569#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10570 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10571 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10572 "(expected %i, found %i)\n", \
10573 current_config->name & (mask), \
10574 pipe_config->name & (mask)); \
10575 return false; \
10576 }
10577
5e550656
VS
10578#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10579 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10580 DRM_ERROR("mismatch in " #name " " \
10581 "(expected %i, found %i)\n", \
10582 current_config->name, \
10583 pipe_config->name); \
10584 return false; \
10585 }
10586
bb760063
DV
10587#define PIPE_CONF_QUIRK(quirk) \
10588 ((current_config->quirks | pipe_config->quirks) & (quirk))
10589
eccb140b
DV
10590 PIPE_CONF_CHECK_I(cpu_transcoder);
10591
08a24034
DV
10592 PIPE_CONF_CHECK_I(has_pch_encoder);
10593 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10594 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10595 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10596 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10597 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10598 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10599
eb14cb74 10600 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10601
10602 if (INTEL_INFO(dev)->gen < 8) {
10603 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10604 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10605 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10606 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10607 PIPE_CONF_CHECK_I(dp_m_n.tu);
10608
10609 if (current_config->has_drrs) {
10610 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10611 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10612 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10613 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10614 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10615 }
10616 } else {
10617 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10618 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10619 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10620 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10621 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10622 }
eb14cb74 10623
2d112de7
ACO
10624 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10625 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10626 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10627 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10628 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10629 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 10630
2d112de7
ACO
10631 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10632 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10633 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10634 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10635 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10636 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 10637
c93f54cf 10638 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10639 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10640 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10641 IS_VALLEYVIEW(dev))
10642 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 10643 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 10644
9ed109a7
DV
10645 PIPE_CONF_CHECK_I(has_audio);
10646
2d112de7 10647 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
10648 DRM_MODE_FLAG_INTERLACE);
10649
bb760063 10650 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 10651 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10652 DRM_MODE_FLAG_PHSYNC);
2d112de7 10653 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10654 DRM_MODE_FLAG_NHSYNC);
2d112de7 10655 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10656 DRM_MODE_FLAG_PVSYNC);
2d112de7 10657 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
10658 DRM_MODE_FLAG_NVSYNC);
10659 }
045ac3b5 10660
37327abd
VS
10661 PIPE_CONF_CHECK_I(pipe_src_w);
10662 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10663
9953599b
DV
10664 /*
10665 * FIXME: BIOS likes to set up a cloned config with lvds+external
10666 * screen. Since we don't yet re-compute the pipe config when moving
10667 * just the lvds port away to another pipe the sw tracking won't match.
10668 *
10669 * Proper atomic modesets with recomputed global state will fix this.
10670 * Until then just don't check gmch state for inherited modes.
10671 */
10672 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10673 PIPE_CONF_CHECK_I(gmch_pfit.control);
10674 /* pfit ratios are autocomputed by the hw on gen4+ */
10675 if (INTEL_INFO(dev)->gen < 4)
10676 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10677 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10678 }
10679
fd4daa9c
CW
10680 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10681 if (current_config->pch_pfit.enabled) {
10682 PIPE_CONF_CHECK_I(pch_pfit.pos);
10683 PIPE_CONF_CHECK_I(pch_pfit.size);
10684 }
2fa2fe9a 10685
e59150dc
JB
10686 /* BDW+ don't expose a synchronous way to read the state */
10687 if (IS_HASWELL(dev))
10688 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10689
282740f7
VS
10690 PIPE_CONF_CHECK_I(double_wide);
10691
26804afd
DV
10692 PIPE_CONF_CHECK_X(ddi_pll_sel);
10693
c0d43d62 10694 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10695 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10696 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10697 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10698 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10699 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
10700 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10701 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10702 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 10703
42571aef
VS
10704 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10705 PIPE_CONF_CHECK_I(pipe_bpp);
10706
2d112de7 10707 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 10708 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10709
66e985c0 10710#undef PIPE_CONF_CHECK_X
08a24034 10711#undef PIPE_CONF_CHECK_I
b95af8be 10712#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10713#undef PIPE_CONF_CHECK_FLAGS
5e550656 10714#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10715#undef PIPE_CONF_QUIRK
88adfff1 10716
0e8ffe1b
DV
10717 return true;
10718}
10719
08db6652
DL
10720static void check_wm_state(struct drm_device *dev)
10721{
10722 struct drm_i915_private *dev_priv = dev->dev_private;
10723 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10724 struct intel_crtc *intel_crtc;
10725 int plane;
10726
10727 if (INTEL_INFO(dev)->gen < 9)
10728 return;
10729
10730 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10731 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10732
10733 for_each_intel_crtc(dev, intel_crtc) {
10734 struct skl_ddb_entry *hw_entry, *sw_entry;
10735 const enum pipe pipe = intel_crtc->pipe;
10736
10737 if (!intel_crtc->active)
10738 continue;
10739
10740 /* planes */
10741 for_each_plane(pipe, plane) {
10742 hw_entry = &hw_ddb.plane[pipe][plane];
10743 sw_entry = &sw_ddb->plane[pipe][plane];
10744
10745 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10746 continue;
10747
10748 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10749 "(expected (%u,%u), found (%u,%u))\n",
10750 pipe_name(pipe), plane + 1,
10751 sw_entry->start, sw_entry->end,
10752 hw_entry->start, hw_entry->end);
10753 }
10754
10755 /* cursor */
10756 hw_entry = &hw_ddb.cursor[pipe];
10757 sw_entry = &sw_ddb->cursor[pipe];
10758
10759 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10760 continue;
10761
10762 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10763 "(expected (%u,%u), found (%u,%u))\n",
10764 pipe_name(pipe),
10765 sw_entry->start, sw_entry->end,
10766 hw_entry->start, hw_entry->end);
10767 }
10768}
10769
91d1b4bd
DV
10770static void
10771check_connector_state(struct drm_device *dev)
8af6cf88 10772{
8af6cf88
DV
10773 struct intel_connector *connector;
10774
10775 list_for_each_entry(connector, &dev->mode_config.connector_list,
10776 base.head) {
10777 /* This also checks the encoder/connector hw state with the
10778 * ->get_hw_state callbacks. */
10779 intel_connector_check_state(connector);
10780
e2c719b7 10781 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
10782 "connector's staged encoder doesn't match current encoder\n");
10783 }
91d1b4bd
DV
10784}
10785
10786static void
10787check_encoder_state(struct drm_device *dev)
10788{
10789 struct intel_encoder *encoder;
10790 struct intel_connector *connector;
8af6cf88 10791
b2784e15 10792 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10793 bool enabled = false;
10794 bool active = false;
10795 enum pipe pipe, tracked_pipe;
10796
10797 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10798 encoder->base.base.id,
8e329a03 10799 encoder->base.name);
8af6cf88 10800
e2c719b7 10801 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 10802 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 10803 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
10804 "encoder's active_connectors set, but no crtc\n");
10805
10806 list_for_each_entry(connector, &dev->mode_config.connector_list,
10807 base.head) {
10808 if (connector->base.encoder != &encoder->base)
10809 continue;
10810 enabled = true;
10811 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10812 active = true;
10813 }
0e32b39c
DA
10814 /*
10815 * for MST connectors if we unplug the connector is gone
10816 * away but the encoder is still connected to a crtc
10817 * until a modeset happens in response to the hotplug.
10818 */
10819 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10820 continue;
10821
e2c719b7 10822 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
10823 "encoder's enabled state mismatch "
10824 "(expected %i, found %i)\n",
10825 !!encoder->base.crtc, enabled);
e2c719b7 10826 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
10827 "active encoder with no crtc\n");
10828
e2c719b7 10829 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
10830 "encoder's computed active state doesn't match tracked active state "
10831 "(expected %i, found %i)\n", active, encoder->connectors_active);
10832
10833 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 10834 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
10835 "encoder's hw state doesn't match sw tracking "
10836 "(expected %i, found %i)\n",
10837 encoder->connectors_active, active);
10838
10839 if (!encoder->base.crtc)
10840 continue;
10841
10842 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 10843 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
10844 "active encoder's pipe doesn't match"
10845 "(expected %i, found %i)\n",
10846 tracked_pipe, pipe);
10847
10848 }
91d1b4bd
DV
10849}
10850
10851static void
10852check_crtc_state(struct drm_device *dev)
10853{
fbee40df 10854 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10855 struct intel_crtc *crtc;
10856 struct intel_encoder *encoder;
5cec258b 10857 struct intel_crtc_state pipe_config;
8af6cf88 10858
d3fcc808 10859 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10860 bool enabled = false;
10861 bool active = false;
10862
045ac3b5
JB
10863 memset(&pipe_config, 0, sizeof(pipe_config));
10864
8af6cf88
DV
10865 DRM_DEBUG_KMS("[CRTC:%d]\n",
10866 crtc->base.base.id);
10867
e2c719b7 10868 I915_STATE_WARN(crtc->active && !crtc->base.enabled,
8af6cf88
DV
10869 "active crtc, but not enabled in sw tracking\n");
10870
b2784e15 10871 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10872 if (encoder->base.crtc != &crtc->base)
10873 continue;
10874 enabled = true;
10875 if (encoder->connectors_active)
10876 active = true;
10877 }
6c49f241 10878
e2c719b7 10879 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
10880 "crtc's computed active state doesn't match tracked active state "
10881 "(expected %i, found %i)\n", active, crtc->active);
e2c719b7 10882 I915_STATE_WARN(enabled != crtc->base.enabled,
8af6cf88
DV
10883 "crtc's computed enabled state doesn't match tracked enabled state "
10884 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10885
0e8ffe1b
DV
10886 active = dev_priv->display.get_pipe_config(crtc,
10887 &pipe_config);
d62cf62a 10888
b6b5d049
VS
10889 /* hw state is inconsistent with the pipe quirk */
10890 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10891 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10892 active = crtc->active;
10893
b2784e15 10894 for_each_intel_encoder(dev, encoder) {
3eaba51c 10895 enum pipe pipe;
6c49f241
DV
10896 if (encoder->base.crtc != &crtc->base)
10897 continue;
1d37b689 10898 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10899 encoder->get_config(encoder, &pipe_config);
10900 }
10901
e2c719b7 10902 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
10903 "crtc active state doesn't match with hw state "
10904 "(expected %i, found %i)\n", crtc->active, active);
10905
c0b03411 10906 if (active &&
6e3c9717 10907 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 10908 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
10909 intel_dump_pipe_config(crtc, &pipe_config,
10910 "[hw state]");
6e3c9717 10911 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
10912 "[sw state]");
10913 }
8af6cf88
DV
10914 }
10915}
10916
91d1b4bd
DV
10917static void
10918check_shared_dpll_state(struct drm_device *dev)
10919{
fbee40df 10920 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10921 struct intel_crtc *crtc;
10922 struct intel_dpll_hw_state dpll_hw_state;
10923 int i;
5358901f
DV
10924
10925 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10926 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10927 int enabled_crtcs = 0, active_crtcs = 0;
10928 bool active;
10929
10930 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10931
10932 DRM_DEBUG_KMS("%s\n", pll->name);
10933
10934 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10935
e2c719b7 10936 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 10937 "more active pll users than references: %i vs %i\n",
3e369b76 10938 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 10939 I915_STATE_WARN(pll->active && !pll->on,
5358901f 10940 "pll in active use but not on in sw tracking\n");
e2c719b7 10941 I915_STATE_WARN(pll->on && !pll->active,
35c95375 10942 "pll in on but not on in use in sw tracking\n");
e2c719b7 10943 I915_STATE_WARN(pll->on != active,
5358901f
DV
10944 "pll on state mismatch (expected %i, found %i)\n",
10945 pll->on, active);
10946
d3fcc808 10947 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10948 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10949 enabled_crtcs++;
10950 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10951 active_crtcs++;
10952 }
e2c719b7 10953 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
10954 "pll active crtcs mismatch (expected %i, found %i)\n",
10955 pll->active, active_crtcs);
e2c719b7 10956 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 10957 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 10958 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 10959
e2c719b7 10960 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
10961 sizeof(dpll_hw_state)),
10962 "pll hw state mismatch\n");
5358901f 10963 }
8af6cf88
DV
10964}
10965
91d1b4bd
DV
10966void
10967intel_modeset_check_state(struct drm_device *dev)
10968{
08db6652 10969 check_wm_state(dev);
91d1b4bd
DV
10970 check_connector_state(dev);
10971 check_encoder_state(dev);
10972 check_crtc_state(dev);
10973 check_shared_dpll_state(dev);
10974}
10975
5cec258b 10976void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
10977 int dotclock)
10978{
10979 /*
10980 * FDI already provided one idea for the dotclock.
10981 * Yell if the encoder disagrees.
10982 */
2d112de7 10983 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 10984 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 10985 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10986}
10987
80715b2f
VS
10988static void update_scanline_offset(struct intel_crtc *crtc)
10989{
10990 struct drm_device *dev = crtc->base.dev;
10991
10992 /*
10993 * The scanline counter increments at the leading edge of hsync.
10994 *
10995 * On most platforms it starts counting from vtotal-1 on the
10996 * first active line. That means the scanline counter value is
10997 * always one less than what we would expect. Ie. just after
10998 * start of vblank, which also occurs at start of hsync (on the
10999 * last active line), the scanline counter will read vblank_start-1.
11000 *
11001 * On gen2 the scanline counter starts counting from 1 instead
11002 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11003 * to keep the value positive), instead of adding one.
11004 *
11005 * On HSW+ the behaviour of the scanline counter depends on the output
11006 * type. For DP ports it behaves like most other platforms, but on HDMI
11007 * there's an extra 1 line difference. So we need to add two instead of
11008 * one to the value.
11009 */
11010 if (IS_GEN2(dev)) {
6e3c9717 11011 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
11012 int vtotal;
11013
11014 vtotal = mode->crtc_vtotal;
11015 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11016 vtotal /= 2;
11017
11018 crtc->scanline_offset = vtotal - 1;
11019 } else if (HAS_DDI(dev) &&
409ee761 11020 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
11021 crtc->scanline_offset = 2;
11022 } else
11023 crtc->scanline_offset = 1;
11024}
11025
5cec258b 11026static struct intel_crtc_state *
7f27126e
JB
11027intel_modeset_compute_config(struct drm_crtc *crtc,
11028 struct drm_display_mode *mode,
11029 struct drm_framebuffer *fb,
11030 unsigned *modeset_pipes,
11031 unsigned *prepare_pipes,
11032 unsigned *disable_pipes)
11033{
5cec258b 11034 struct intel_crtc_state *pipe_config = NULL;
7f27126e
JB
11035
11036 intel_modeset_affected_pipes(crtc, modeset_pipes,
11037 prepare_pipes, disable_pipes);
11038
11039 if ((*modeset_pipes) == 0)
11040 goto out;
11041
11042 /*
11043 * Note this needs changes when we start tracking multiple modes
11044 * and crtcs. At that point we'll need to compute the whole config
11045 * (i.e. one pipe_config for each crtc) rather than just the one
11046 * for this crtc.
11047 */
11048 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11049 if (IS_ERR(pipe_config)) {
11050 goto out;
11051 }
11052 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11053 "[modeset]");
7f27126e
JB
11054
11055out:
11056 return pipe_config;
11057}
11058
ed6739ef
ACO
11059static int __intel_set_mode_setup_plls(struct drm_device *dev,
11060 unsigned modeset_pipes,
11061 unsigned disable_pipes)
11062{
11063 struct drm_i915_private *dev_priv = to_i915(dev);
11064 unsigned clear_pipes = modeset_pipes | disable_pipes;
11065 struct intel_crtc *intel_crtc;
11066 int ret = 0;
11067
11068 if (!dev_priv->display.crtc_compute_clock)
11069 return 0;
11070
11071 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11072 if (ret)
11073 goto done;
11074
11075 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11076 struct intel_crtc_state *state = intel_crtc->new_config;
11077 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11078 state);
11079 if (ret) {
11080 intel_shared_dpll_abort_config(dev_priv);
11081 goto done;
11082 }
11083 }
11084
11085done:
11086 return ret;
11087}
11088
f30da187
DV
11089static int __intel_set_mode(struct drm_crtc *crtc,
11090 struct drm_display_mode *mode,
7f27126e 11091 int x, int y, struct drm_framebuffer *fb,
5cec258b 11092 struct intel_crtc_state *pipe_config,
7f27126e
JB
11093 unsigned modeset_pipes,
11094 unsigned prepare_pipes,
11095 unsigned disable_pipes)
a6778b3c
DV
11096{
11097 struct drm_device *dev = crtc->dev;
fbee40df 11098 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 11099 struct drm_display_mode *saved_mode;
25c5b266 11100 struct intel_crtc *intel_crtc;
c0c36b94 11101 int ret = 0;
a6778b3c 11102
4b4b9238 11103 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
11104 if (!saved_mode)
11105 return -ENOMEM;
a6778b3c 11106
3ac18232 11107 *saved_mode = crtc->mode;
a6778b3c 11108
b9950a13
VS
11109 if (modeset_pipes)
11110 to_intel_crtc(crtc)->new_config = pipe_config;
11111
30a970c6
JB
11112 /*
11113 * See if the config requires any additional preparation, e.g.
11114 * to adjust global state with pipes off. We need to do this
11115 * here so we can get the modeset_pipe updated config for the new
11116 * mode set on this crtc. For other crtcs we need to use the
11117 * adjusted_mode bits in the crtc directly.
11118 */
c164f833 11119 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 11120 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 11121
c164f833
VS
11122 /* may have added more to prepare_pipes than we should */
11123 prepare_pipes &= ~disable_pipes;
11124 }
11125
ed6739ef
ACO
11126 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11127 if (ret)
11128 goto done;
8bd31e67 11129
460da916
DV
11130 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11131 intel_crtc_disable(&intel_crtc->base);
11132
ea9d758d
DV
11133 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11134 if (intel_crtc->base.enabled)
11135 dev_priv->display.crtc_disable(&intel_crtc->base);
11136 }
a6778b3c 11137
6c4c86f5
DV
11138 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11139 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
11140 *
11141 * Note we'll need to fix this up when we start tracking multiple
11142 * pipes; here we assume a single modeset_pipe and only track the
11143 * single crtc and mode.
f6e5b160 11144 */
b8cecdf5 11145 if (modeset_pipes) {
25c5b266 11146 crtc->mode = *mode;
b8cecdf5
DV
11147 /* mode_set/enable/disable functions rely on a correct pipe
11148 * config. */
f5de6e07 11149 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
11150
11151 /*
11152 * Calculate and store various constants which
11153 * are later needed by vblank and swap-completion
11154 * timestamping. They are derived from true hwmode.
11155 */
11156 drm_calc_timestamping_constants(crtc,
2d112de7 11157 &pipe_config->base.adjusted_mode);
b8cecdf5 11158 }
7758a113 11159
ea9d758d
DV
11160 /* Only after disabling all output pipelines that will be changed can we
11161 * update the the output configuration. */
11162 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11163
50f6e502 11164 modeset_update_crtc_power_domains(dev);
47fab737 11165
a6778b3c
DV
11166 /* Set up the DPLL and any encoders state that needs to adjust or depend
11167 * on the DPLL.
f6e5b160 11168 */
25c5b266 11169 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
11170 struct drm_plane *primary = intel_crtc->base.primary;
11171 int vdisplay, hdisplay;
4c10794f 11172
455a6808
GP
11173 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11174 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11175 fb, 0, 0,
11176 hdisplay, vdisplay,
11177 x << 16, y << 16,
11178 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
11179 }
11180
11181 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11182 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11183 update_scanline_offset(intel_crtc);
11184
25c5b266 11185 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11186 }
a6778b3c 11187
a6778b3c
DV
11188 /* FIXME: add subpixel order */
11189done:
4b4b9238 11190 if (ret && crtc->enabled)
3ac18232 11191 crtc->mode = *saved_mode;
a6778b3c 11192
3ac18232 11193 kfree(saved_mode);
a6778b3c 11194 return ret;
f6e5b160
CW
11195}
11196
7f27126e
JB
11197static int intel_set_mode_pipes(struct drm_crtc *crtc,
11198 struct drm_display_mode *mode,
11199 int x, int y, struct drm_framebuffer *fb,
5cec258b 11200 struct intel_crtc_state *pipe_config,
7f27126e
JB
11201 unsigned modeset_pipes,
11202 unsigned prepare_pipes,
11203 unsigned disable_pipes)
f30da187
DV
11204{
11205 int ret;
11206
7f27126e
JB
11207 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11208 prepare_pipes, disable_pipes);
f30da187
DV
11209
11210 if (ret == 0)
11211 intel_modeset_check_state(crtc->dev);
11212
11213 return ret;
11214}
11215
7f27126e
JB
11216static int intel_set_mode(struct drm_crtc *crtc,
11217 struct drm_display_mode *mode,
11218 int x, int y, struct drm_framebuffer *fb)
11219{
5cec258b 11220 struct intel_crtc_state *pipe_config;
7f27126e
JB
11221 unsigned modeset_pipes, prepare_pipes, disable_pipes;
11222
11223 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11224 &modeset_pipes,
11225 &prepare_pipes,
11226 &disable_pipes);
11227
11228 if (IS_ERR(pipe_config))
11229 return PTR_ERR(pipe_config);
11230
11231 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11232 modeset_pipes, prepare_pipes,
11233 disable_pipes);
11234}
11235
c0c36b94
CW
11236void intel_crtc_restore_mode(struct drm_crtc *crtc)
11237{
f4510a27 11238 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11239}
11240
25c5b266
DV
11241#undef for_each_intel_crtc_masked
11242
d9e55608
DV
11243static void intel_set_config_free(struct intel_set_config *config)
11244{
11245 if (!config)
11246 return;
11247
1aa4b628
DV
11248 kfree(config->save_connector_encoders);
11249 kfree(config->save_encoder_crtcs);
7668851f 11250 kfree(config->save_crtc_enabled);
d9e55608
DV
11251 kfree(config);
11252}
11253
85f9eb71
DV
11254static int intel_set_config_save_state(struct drm_device *dev,
11255 struct intel_set_config *config)
11256{
7668851f 11257 struct drm_crtc *crtc;
85f9eb71
DV
11258 struct drm_encoder *encoder;
11259 struct drm_connector *connector;
11260 int count;
11261
7668851f
VS
11262 config->save_crtc_enabled =
11263 kcalloc(dev->mode_config.num_crtc,
11264 sizeof(bool), GFP_KERNEL);
11265 if (!config->save_crtc_enabled)
11266 return -ENOMEM;
11267
1aa4b628
DV
11268 config->save_encoder_crtcs =
11269 kcalloc(dev->mode_config.num_encoder,
11270 sizeof(struct drm_crtc *), GFP_KERNEL);
11271 if (!config->save_encoder_crtcs)
85f9eb71
DV
11272 return -ENOMEM;
11273
1aa4b628
DV
11274 config->save_connector_encoders =
11275 kcalloc(dev->mode_config.num_connector,
11276 sizeof(struct drm_encoder *), GFP_KERNEL);
11277 if (!config->save_connector_encoders)
85f9eb71
DV
11278 return -ENOMEM;
11279
11280 /* Copy data. Note that driver private data is not affected.
11281 * Should anything bad happen only the expected state is
11282 * restored, not the drivers personal bookkeeping.
11283 */
7668851f 11284 count = 0;
70e1e0ec 11285 for_each_crtc(dev, crtc) {
7668851f
VS
11286 config->save_crtc_enabled[count++] = crtc->enabled;
11287 }
11288
85f9eb71
DV
11289 count = 0;
11290 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11291 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11292 }
11293
11294 count = 0;
11295 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11296 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11297 }
11298
11299 return 0;
11300}
11301
11302static void intel_set_config_restore_state(struct drm_device *dev,
11303 struct intel_set_config *config)
11304{
7668851f 11305 struct intel_crtc *crtc;
9a935856
DV
11306 struct intel_encoder *encoder;
11307 struct intel_connector *connector;
85f9eb71
DV
11308 int count;
11309
7668851f 11310 count = 0;
d3fcc808 11311 for_each_intel_crtc(dev, crtc) {
7668851f 11312 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11313
11314 if (crtc->new_enabled)
6e3c9717 11315 crtc->new_config = crtc->config;
7bd0a8e7
VS
11316 else
11317 crtc->new_config = NULL;
7668851f
VS
11318 }
11319
85f9eb71 11320 count = 0;
b2784e15 11321 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11322 encoder->new_crtc =
11323 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11324 }
11325
11326 count = 0;
9a935856
DV
11327 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11328 connector->new_encoder =
11329 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11330 }
11331}
11332
e3de42b6 11333static bool
2e57f47d 11334is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11335{
11336 int i;
11337
2e57f47d
CW
11338 if (set->num_connectors == 0)
11339 return false;
11340
11341 if (WARN_ON(set->connectors == NULL))
11342 return false;
11343
11344 for (i = 0; i < set->num_connectors; i++)
11345 if (set->connectors[i]->encoder &&
11346 set->connectors[i]->encoder->crtc == set->crtc &&
11347 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11348 return true;
11349
11350 return false;
11351}
11352
5e2b584e
DV
11353static void
11354intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11355 struct intel_set_config *config)
11356{
11357
11358 /* We should be able to check here if the fb has the same properties
11359 * and then just flip_or_move it */
2e57f47d
CW
11360 if (is_crtc_connector_off(set)) {
11361 config->mode_changed = true;
f4510a27 11362 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11363 /*
11364 * If we have no fb, we can only flip as long as the crtc is
11365 * active, otherwise we need a full mode set. The crtc may
11366 * be active if we've only disabled the primary plane, or
11367 * in fastboot situations.
11368 */
f4510a27 11369 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11370 struct intel_crtc *intel_crtc =
11371 to_intel_crtc(set->crtc);
11372
3b150f08 11373 if (intel_crtc->active) {
319d9827
JB
11374 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11375 config->fb_changed = true;
11376 } else {
11377 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11378 config->mode_changed = true;
11379 }
5e2b584e
DV
11380 } else if (set->fb == NULL) {
11381 config->mode_changed = true;
72f4901e 11382 } else if (set->fb->pixel_format !=
f4510a27 11383 set->crtc->primary->fb->pixel_format) {
5e2b584e 11384 config->mode_changed = true;
e3de42b6 11385 } else {
5e2b584e 11386 config->fb_changed = true;
e3de42b6 11387 }
5e2b584e
DV
11388 }
11389
835c5873 11390 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11391 config->fb_changed = true;
11392
11393 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11394 DRM_DEBUG_KMS("modes are different, full mode set\n");
11395 drm_mode_debug_printmodeline(&set->crtc->mode);
11396 drm_mode_debug_printmodeline(set->mode);
11397 config->mode_changed = true;
11398 }
a1d95703
CW
11399
11400 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11401 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11402}
11403
2e431051 11404static int
9a935856
DV
11405intel_modeset_stage_output_state(struct drm_device *dev,
11406 struct drm_mode_set *set,
11407 struct intel_set_config *config)
50f56119 11408{
9a935856
DV
11409 struct intel_connector *connector;
11410 struct intel_encoder *encoder;
7668851f 11411 struct intel_crtc *crtc;
f3f08572 11412 int ro;
50f56119 11413
9abdda74 11414 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11415 * of connectors. For paranoia, double-check this. */
11416 WARN_ON(!set->fb && (set->num_connectors != 0));
11417 WARN_ON(set->fb && (set->num_connectors == 0));
11418
9a935856
DV
11419 list_for_each_entry(connector, &dev->mode_config.connector_list,
11420 base.head) {
11421 /* Otherwise traverse passed in connector list and get encoders
11422 * for them. */
50f56119 11423 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11424 if (set->connectors[ro] == &connector->base) {
0e32b39c 11425 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11426 break;
11427 }
11428 }
11429
9a935856
DV
11430 /* If we disable the crtc, disable all its connectors. Also, if
11431 * the connector is on the changing crtc but not on the new
11432 * connector list, disable it. */
11433 if ((!set->fb || ro == set->num_connectors) &&
11434 connector->base.encoder &&
11435 connector->base.encoder->crtc == set->crtc) {
11436 connector->new_encoder = NULL;
11437
11438 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11439 connector->base.base.id,
c23cc417 11440 connector->base.name);
9a935856
DV
11441 }
11442
11443
11444 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11445 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11446 config->mode_changed = true;
50f56119
DV
11447 }
11448 }
9a935856 11449 /* connector->new_encoder is now updated for all connectors. */
50f56119 11450
9a935856 11451 /* Update crtc of enabled connectors. */
9a935856
DV
11452 list_for_each_entry(connector, &dev->mode_config.connector_list,
11453 base.head) {
7668851f
VS
11454 struct drm_crtc *new_crtc;
11455
9a935856 11456 if (!connector->new_encoder)
50f56119
DV
11457 continue;
11458
9a935856 11459 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11460
11461 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11462 if (set->connectors[ro] == &connector->base)
50f56119
DV
11463 new_crtc = set->crtc;
11464 }
11465
11466 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11467 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11468 new_crtc)) {
5e2b584e 11469 return -EINVAL;
50f56119 11470 }
0e32b39c 11471 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11472
11473 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11474 connector->base.base.id,
c23cc417 11475 connector->base.name,
9a935856
DV
11476 new_crtc->base.id);
11477 }
11478
11479 /* Check for any encoders that needs to be disabled. */
b2784e15 11480 for_each_intel_encoder(dev, encoder) {
5a65f358 11481 int num_connectors = 0;
9a935856
DV
11482 list_for_each_entry(connector,
11483 &dev->mode_config.connector_list,
11484 base.head) {
11485 if (connector->new_encoder == encoder) {
11486 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11487 num_connectors++;
9a935856
DV
11488 }
11489 }
5a65f358
PZ
11490
11491 if (num_connectors == 0)
11492 encoder->new_crtc = NULL;
11493 else if (num_connectors > 1)
11494 return -EINVAL;
11495
9a935856
DV
11496 /* Only now check for crtc changes so we don't miss encoders
11497 * that will be disabled. */
11498 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11499 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11500 config->mode_changed = true;
50f56119
DV
11501 }
11502 }
9a935856 11503 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11504 list_for_each_entry(connector, &dev->mode_config.connector_list,
11505 base.head) {
11506 if (connector->new_encoder)
11507 if (connector->new_encoder != connector->encoder)
11508 connector->encoder = connector->new_encoder;
11509 }
d3fcc808 11510 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11511 crtc->new_enabled = false;
11512
b2784e15 11513 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11514 if (encoder->new_crtc == crtc) {
11515 crtc->new_enabled = true;
11516 break;
11517 }
11518 }
11519
11520 if (crtc->new_enabled != crtc->base.enabled) {
11521 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11522 crtc->new_enabled ? "en" : "dis");
11523 config->mode_changed = true;
11524 }
7bd0a8e7
VS
11525
11526 if (crtc->new_enabled)
6e3c9717 11527 crtc->new_config = crtc->config;
7bd0a8e7
VS
11528 else
11529 crtc->new_config = NULL;
7668851f
VS
11530 }
11531
2e431051
DV
11532 return 0;
11533}
11534
7d00a1f5
VS
11535static void disable_crtc_nofb(struct intel_crtc *crtc)
11536{
11537 struct drm_device *dev = crtc->base.dev;
11538 struct intel_encoder *encoder;
11539 struct intel_connector *connector;
11540
11541 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11542 pipe_name(crtc->pipe));
11543
11544 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11545 if (connector->new_encoder &&
11546 connector->new_encoder->new_crtc == crtc)
11547 connector->new_encoder = NULL;
11548 }
11549
b2784e15 11550 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11551 if (encoder->new_crtc == crtc)
11552 encoder->new_crtc = NULL;
11553 }
11554
11555 crtc->new_enabled = false;
7bd0a8e7 11556 crtc->new_config = NULL;
7d00a1f5
VS
11557}
11558
2e431051
DV
11559static int intel_crtc_set_config(struct drm_mode_set *set)
11560{
11561 struct drm_device *dev;
2e431051
DV
11562 struct drm_mode_set save_set;
11563 struct intel_set_config *config;
5cec258b 11564 struct intel_crtc_state *pipe_config;
50f52756 11565 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 11566 int ret;
2e431051 11567
8d3e375e
DV
11568 BUG_ON(!set);
11569 BUG_ON(!set->crtc);
11570 BUG_ON(!set->crtc->helper_private);
2e431051 11571
7e53f3a4
DV
11572 /* Enforce sane interface api - has been abused by the fb helper. */
11573 BUG_ON(!set->mode && set->fb);
11574 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11575
2e431051
DV
11576 if (set->fb) {
11577 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11578 set->crtc->base.id, set->fb->base.id,
11579 (int)set->num_connectors, set->x, set->y);
11580 } else {
11581 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11582 }
11583
11584 dev = set->crtc->dev;
11585
11586 ret = -ENOMEM;
11587 config = kzalloc(sizeof(*config), GFP_KERNEL);
11588 if (!config)
11589 goto out_config;
11590
11591 ret = intel_set_config_save_state(dev, config);
11592 if (ret)
11593 goto out_config;
11594
11595 save_set.crtc = set->crtc;
11596 save_set.mode = &set->crtc->mode;
11597 save_set.x = set->crtc->x;
11598 save_set.y = set->crtc->y;
f4510a27 11599 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11600
11601 /* Compute whether we need a full modeset, only an fb base update or no
11602 * change at all. In the future we might also check whether only the
11603 * mode changed, e.g. for LVDS where we only change the panel fitter in
11604 * such cases. */
11605 intel_set_config_compute_mode_changes(set, config);
11606
9a935856 11607 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11608 if (ret)
11609 goto fail;
11610
50f52756
JB
11611 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11612 set->fb,
11613 &modeset_pipes,
11614 &prepare_pipes,
11615 &disable_pipes);
20664591 11616 if (IS_ERR(pipe_config)) {
6ac0483b 11617 ret = PTR_ERR(pipe_config);
50f52756 11618 goto fail;
20664591 11619 } else if (pipe_config) {
b9950a13 11620 if (pipe_config->has_audio !=
6e3c9717 11621 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
11622 config->mode_changed = true;
11623
af15d2ce
JB
11624 /*
11625 * Note we have an issue here with infoframes: current code
11626 * only updates them on the full mode set path per hw
11627 * requirements. So here we should be checking for any
11628 * required changes and forcing a mode set.
11629 */
20664591 11630 }
50f52756
JB
11631
11632 /* set_mode will free it in the mode_changed case */
11633 if (!config->mode_changed)
11634 kfree(pipe_config);
11635
1f9954d0
JB
11636 intel_update_pipe_size(to_intel_crtc(set->crtc));
11637
5e2b584e 11638 if (config->mode_changed) {
50f52756
JB
11639 ret = intel_set_mode_pipes(set->crtc, set->mode,
11640 set->x, set->y, set->fb, pipe_config,
11641 modeset_pipes, prepare_pipes,
11642 disable_pipes);
5e2b584e 11643 } else if (config->fb_changed) {
3b150f08 11644 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808
GP
11645 struct drm_plane *primary = set->crtc->primary;
11646 int vdisplay, hdisplay;
3b150f08 11647
455a6808
GP
11648 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11649 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11650 0, 0, hdisplay, vdisplay,
11651 set->x << 16, set->y << 16,
11652 hdisplay << 16, vdisplay << 16);
3b150f08
MR
11653
11654 /*
11655 * We need to make sure the primary plane is re-enabled if it
11656 * has previously been turned off.
11657 */
11658 if (!intel_crtc->primary_enabled && ret == 0) {
11659 WARN_ON(!intel_crtc->active);
fdd508a6 11660 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11661 }
11662
7ca51a3a
JB
11663 /*
11664 * In the fastboot case this may be our only check of the
11665 * state after boot. It would be better to only do it on
11666 * the first update, but we don't have a nice way of doing that
11667 * (and really, set_config isn't used much for high freq page
11668 * flipping, so increasing its cost here shouldn't be a big
11669 * deal).
11670 */
d330a953 11671 if (i915.fastboot && ret == 0)
7ca51a3a 11672 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11673 }
11674
2d05eae1 11675 if (ret) {
bf67dfeb
DV
11676 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11677 set->crtc->base.id, ret);
50f56119 11678fail:
2d05eae1 11679 intel_set_config_restore_state(dev, config);
50f56119 11680
7d00a1f5
VS
11681 /*
11682 * HACK: if the pipe was on, but we didn't have a framebuffer,
11683 * force the pipe off to avoid oopsing in the modeset code
11684 * due to fb==NULL. This should only happen during boot since
11685 * we don't yet reconstruct the FB from the hardware state.
11686 */
11687 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11688 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11689
2d05eae1
CW
11690 /* Try to restore the config */
11691 if (config->mode_changed &&
11692 intel_set_mode(save_set.crtc, save_set.mode,
11693 save_set.x, save_set.y, save_set.fb))
11694 DRM_ERROR("failed to restore config after modeset failure\n");
11695 }
50f56119 11696
d9e55608
DV
11697out_config:
11698 intel_set_config_free(config);
50f56119
DV
11699 return ret;
11700}
f6e5b160
CW
11701
11702static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11703 .gamma_set = intel_crtc_gamma_set,
50f56119 11704 .set_config = intel_crtc_set_config,
f6e5b160
CW
11705 .destroy = intel_crtc_destroy,
11706 .page_flip = intel_crtc_page_flip,
1356837e
MR
11707 .atomic_duplicate_state = intel_crtc_duplicate_state,
11708 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
11709};
11710
5358901f
DV
11711static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11712 struct intel_shared_dpll *pll,
11713 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11714{
5358901f 11715 uint32_t val;
ee7b9f93 11716
f458ebbc 11717 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
11718 return false;
11719
5358901f 11720 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11721 hw_state->dpll = val;
11722 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11723 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11724
11725 return val & DPLL_VCO_ENABLE;
11726}
11727
15bdd4cf
DV
11728static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11729 struct intel_shared_dpll *pll)
11730{
3e369b76
ACO
11731 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11732 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
11733}
11734
e7b903d2
DV
11735static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11736 struct intel_shared_dpll *pll)
11737{
e7b903d2 11738 /* PCH refclock must be enabled first */
89eff4be 11739 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11740
3e369b76 11741 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
11742
11743 /* Wait for the clocks to stabilize. */
11744 POSTING_READ(PCH_DPLL(pll->id));
11745 udelay(150);
11746
11747 /* The pixel multiplier can only be updated once the
11748 * DPLL is enabled and the clocks are stable.
11749 *
11750 * So write it again.
11751 */
3e369b76 11752 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 11753 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11754 udelay(200);
11755}
11756
11757static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11758 struct intel_shared_dpll *pll)
11759{
11760 struct drm_device *dev = dev_priv->dev;
11761 struct intel_crtc *crtc;
e7b903d2
DV
11762
11763 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11764 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11765 if (intel_crtc_to_shared_dpll(crtc) == pll)
11766 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11767 }
11768
15bdd4cf
DV
11769 I915_WRITE(PCH_DPLL(pll->id), 0);
11770 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11771 udelay(200);
11772}
11773
46edb027
DV
11774static char *ibx_pch_dpll_names[] = {
11775 "PCH DPLL A",
11776 "PCH DPLL B",
11777};
11778
7c74ade1 11779static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11780{
e7b903d2 11781 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11782 int i;
11783
7c74ade1 11784 dev_priv->num_shared_dpll = 2;
ee7b9f93 11785
e72f9fbf 11786 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11787 dev_priv->shared_dplls[i].id = i;
11788 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11789 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11790 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11791 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11792 dev_priv->shared_dplls[i].get_hw_state =
11793 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11794 }
11795}
11796
7c74ade1
DV
11797static void intel_shared_dpll_init(struct drm_device *dev)
11798{
e7b903d2 11799 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11800
9cd86933
DV
11801 if (HAS_DDI(dev))
11802 intel_ddi_pll_init(dev);
11803 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11804 ibx_pch_dpll_init(dev);
11805 else
11806 dev_priv->num_shared_dpll = 0;
11807
11808 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11809}
11810
6beb8c23
MR
11811/**
11812 * intel_prepare_plane_fb - Prepare fb for usage on plane
11813 * @plane: drm plane to prepare for
11814 * @fb: framebuffer to prepare for presentation
11815 *
11816 * Prepares a framebuffer for usage on a display plane. Generally this
11817 * involves pinning the underlying object and updating the frontbuffer tracking
11818 * bits. Some older platforms need special physical address handling for
11819 * cursor planes.
11820 *
11821 * Returns 0 on success, negative error code on failure.
11822 */
11823int
11824intel_prepare_plane_fb(struct drm_plane *plane,
11825 struct drm_framebuffer *fb)
465c120c
MR
11826{
11827 struct drm_device *dev = plane->dev;
6beb8c23
MR
11828 struct intel_plane *intel_plane = to_intel_plane(plane);
11829 enum pipe pipe = intel_plane->pipe;
11830 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11831 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11832 unsigned frontbuffer_bits = 0;
11833 int ret = 0;
465c120c 11834
ea2c67bb 11835 if (!obj)
465c120c
MR
11836 return 0;
11837
6beb8c23
MR
11838 switch (plane->type) {
11839 case DRM_PLANE_TYPE_PRIMARY:
11840 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
11841 break;
11842 case DRM_PLANE_TYPE_CURSOR:
11843 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
11844 break;
11845 case DRM_PLANE_TYPE_OVERLAY:
11846 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
11847 break;
11848 }
465c120c 11849
6beb8c23 11850 mutex_lock(&dev->struct_mutex);
465c120c 11851
6beb8c23
MR
11852 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11853 INTEL_INFO(dev)->cursor_needs_physical) {
11854 int align = IS_I830(dev) ? 16 * 1024 : 256;
11855 ret = i915_gem_object_attach_phys(obj, align);
11856 if (ret)
11857 DRM_DEBUG_KMS("failed to attach phys object\n");
11858 } else {
11859 ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
11860 }
465c120c 11861
6beb8c23
MR
11862 if (ret == 0)
11863 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 11864
4c34574f 11865 mutex_unlock(&dev->struct_mutex);
465c120c 11866
6beb8c23
MR
11867 return ret;
11868}
11869
38f3ce3a
MR
11870/**
11871 * intel_cleanup_plane_fb - Cleans up an fb after plane use
11872 * @plane: drm plane to clean up for
11873 * @fb: old framebuffer that was on plane
11874 *
11875 * Cleans up a framebuffer that has just been removed from a plane.
11876 */
11877void
11878intel_cleanup_plane_fb(struct drm_plane *plane,
11879 struct drm_framebuffer *fb)
11880{
11881 struct drm_device *dev = plane->dev;
11882 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11883
11884 if (WARN_ON(!obj))
11885 return;
11886
11887 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
11888 !INTEL_INFO(dev)->cursor_needs_physical) {
11889 mutex_lock(&dev->struct_mutex);
11890 intel_unpin_fb_obj(obj);
11891 mutex_unlock(&dev->struct_mutex);
11892 }
465c120c
MR
11893}
11894
11895static int
3c692a41
GP
11896intel_check_primary_plane(struct drm_plane *plane,
11897 struct intel_plane_state *state)
11898{
32b7eeec
MR
11899 struct drm_device *dev = plane->dev;
11900 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 11901 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 11902 struct intel_crtc *intel_crtc;
2b875c22 11903 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
11904 struct drm_rect *dest = &state->dst;
11905 struct drm_rect *src = &state->src;
11906 const struct drm_rect *clip = &state->clip;
465c120c
MR
11907 int ret;
11908
ea2c67bb
MR
11909 crtc = crtc ? crtc : plane->crtc;
11910 intel_crtc = to_intel_crtc(crtc);
11911
c59cb179
MR
11912 ret = drm_plane_helper_check_update(plane, crtc, fb,
11913 src, dest, clip,
11914 DRM_PLANE_HELPER_NO_SCALING,
11915 DRM_PLANE_HELPER_NO_SCALING,
11916 false, true, &state->visible);
11917 if (ret)
11918 return ret;
465c120c 11919
32b7eeec
MR
11920 if (intel_crtc->active) {
11921 intel_crtc->atomic.wait_for_flips = true;
11922
11923 /*
11924 * FBC does not work on some platforms for rotated
11925 * planes, so disable it when rotation is not 0 and
11926 * update it when rotation is set back to 0.
11927 *
11928 * FIXME: This is redundant with the fbc update done in
11929 * the primary plane enable function except that that
11930 * one is done too late. We eventually need to unify
11931 * this.
11932 */
11933 if (intel_crtc->primary_enabled &&
11934 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11935 dev_priv->fbc.plane == intel_crtc->plane &&
8e7d688b 11936 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
11937 intel_crtc->atomic.disable_fbc = true;
11938 }
11939
11940 if (state->visible) {
11941 /*
11942 * BDW signals flip done immediately if the plane
11943 * is disabled, even if the plane enable is already
11944 * armed to occur at the next vblank :(
11945 */
11946 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
11947 intel_crtc->atomic.wait_vblank = true;
11948 }
11949
11950 intel_crtc->atomic.fb_bits |=
11951 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11952
11953 intel_crtc->atomic.update_fbc = true;
ccc759dc
GP
11954 }
11955
14af293f
GP
11956 return 0;
11957}
11958
11959static void
11960intel_commit_primary_plane(struct drm_plane *plane,
11961 struct intel_plane_state *state)
11962{
2b875c22
MR
11963 struct drm_crtc *crtc = state->base.crtc;
11964 struct drm_framebuffer *fb = state->base.fb;
11965 struct drm_device *dev = plane->dev;
14af293f 11966 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 11967 struct intel_crtc *intel_crtc;
14af293f 11968 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
14af293f
GP
11969 struct intel_plane *intel_plane = to_intel_plane(plane);
11970 struct drm_rect *src = &state->src;
11971
ea2c67bb
MR
11972 crtc = crtc ? crtc : plane->crtc;
11973 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
11974
11975 plane->fb = fb;
9dc806fc
MR
11976 crtc->x = src->x1 >> 16;
11977 crtc->y = src->y1 >> 16;
ccc759dc 11978
ccc759dc 11979 intel_plane->obj = obj;
4c34574f 11980
ccc759dc 11981 if (intel_crtc->active) {
ccc759dc 11982 if (state->visible) {
ccc759dc
GP
11983 /* FIXME: kill this fastboot hack */
11984 intel_update_pipe_size(intel_crtc);
465c120c 11985
ccc759dc 11986 intel_crtc->primary_enabled = true;
465c120c 11987
ccc759dc
GP
11988 dev_priv->display.update_primary_plane(crtc, plane->fb,
11989 crtc->x, crtc->y);
ccc759dc
GP
11990 } else {
11991 /*
11992 * If clipping results in a non-visible primary plane,
11993 * we'll disable the primary plane. Note that this is
11994 * a bit different than what happens if userspace
11995 * explicitly disables the plane by passing fb=0
11996 * because plane->fb still gets set and pinned.
11997 */
11998 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 11999 }
ccc759dc 12000 }
465c120c
MR
12001}
12002
32b7eeec 12003static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 12004{
32b7eeec 12005 struct drm_device *dev = crtc->dev;
140fd38d 12006 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 12007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
12008 struct intel_plane *intel_plane;
12009 struct drm_plane *p;
12010 unsigned fb_bits = 0;
12011
12012 /* Track fb's for any planes being disabled */
12013 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12014 intel_plane = to_intel_plane(p);
12015
12016 if (intel_crtc->atomic.disabled_planes &
12017 (1 << drm_plane_index(p))) {
12018 switch (p->type) {
12019 case DRM_PLANE_TYPE_PRIMARY:
12020 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12021 break;
12022 case DRM_PLANE_TYPE_CURSOR:
12023 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12024 break;
12025 case DRM_PLANE_TYPE_OVERLAY:
12026 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12027 break;
12028 }
3c692a41 12029
ea2c67bb
MR
12030 mutex_lock(&dev->struct_mutex);
12031 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12032 mutex_unlock(&dev->struct_mutex);
12033 }
12034 }
3c692a41 12035
32b7eeec
MR
12036 if (intel_crtc->atomic.wait_for_flips)
12037 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 12038
32b7eeec
MR
12039 if (intel_crtc->atomic.disable_fbc)
12040 intel_fbc_disable(dev);
3c692a41 12041
32b7eeec
MR
12042 if (intel_crtc->atomic.pre_disable_primary)
12043 intel_pre_disable_primary(crtc);
3c692a41 12044
32b7eeec
MR
12045 if (intel_crtc->atomic.update_wm)
12046 intel_update_watermarks(crtc);
3c692a41 12047
32b7eeec 12048 intel_runtime_pm_get(dev_priv);
3c692a41 12049
c34c9ee4
MR
12050 /* Perform vblank evasion around commit operation */
12051 if (intel_crtc->active)
12052 intel_crtc->atomic.evade =
12053 intel_pipe_update_start(intel_crtc,
12054 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
12055}
12056
12057static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12058{
12059 struct drm_device *dev = crtc->dev;
12060 struct drm_i915_private *dev_priv = dev->dev_private;
12061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12062 struct drm_plane *p;
12063
c34c9ee4
MR
12064 if (intel_crtc->atomic.evade)
12065 intel_pipe_update_end(intel_crtc,
12066 intel_crtc->atomic.start_vbl_count);
3c692a41 12067
140fd38d 12068 intel_runtime_pm_put(dev_priv);
3c692a41 12069
32b7eeec
MR
12070 if (intel_crtc->atomic.wait_vblank)
12071 intel_wait_for_vblank(dev, intel_crtc->pipe);
12072
12073 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12074
12075 if (intel_crtc->atomic.update_fbc) {
ccc759dc 12076 mutex_lock(&dev->struct_mutex);
7ff0ebcc 12077 intel_fbc_update(dev);
ccc759dc 12078 mutex_unlock(&dev->struct_mutex);
38f3ce3a 12079 }
3c692a41 12080
32b7eeec
MR
12081 if (intel_crtc->atomic.post_enable_primary)
12082 intel_post_enable_primary(crtc);
3c692a41 12083
32b7eeec
MR
12084 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12085 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12086 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12087 false, false);
12088
12089 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
12090}
12091
cf4c7c12 12092/**
4a3b8769
MR
12093 * intel_plane_destroy - destroy a plane
12094 * @plane: plane to destroy
cf4c7c12 12095 *
4a3b8769
MR
12096 * Common destruction function for all types of planes (primary, cursor,
12097 * sprite).
cf4c7c12 12098 */
4a3b8769 12099void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
12100{
12101 struct intel_plane *intel_plane = to_intel_plane(plane);
12102 drm_plane_cleanup(plane);
12103 kfree(intel_plane);
12104}
12105
65a3fea0 12106const struct drm_plane_funcs intel_plane_funcs = {
3f678c96
MR
12107 .update_plane = drm_atomic_helper_update_plane,
12108 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 12109 .destroy = intel_plane_destroy,
c196e1d6 12110 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
12111 .atomic_get_property = intel_plane_atomic_get_property,
12112 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
12113 .atomic_duplicate_state = intel_plane_duplicate_state,
12114 .atomic_destroy_state = intel_plane_destroy_state,
12115
465c120c
MR
12116};
12117
12118static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12119 int pipe)
12120{
12121 struct intel_plane *primary;
8e7d688b 12122 struct intel_plane_state *state;
465c120c
MR
12123 const uint32_t *intel_primary_formats;
12124 int num_formats;
12125
12126 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12127 if (primary == NULL)
12128 return NULL;
12129
8e7d688b
MR
12130 state = intel_create_plane_state(&primary->base);
12131 if (!state) {
ea2c67bb
MR
12132 kfree(primary);
12133 return NULL;
12134 }
8e7d688b 12135 primary->base.state = &state->base;
ea2c67bb 12136
465c120c
MR
12137 primary->can_scale = false;
12138 primary->max_downscale = 1;
12139 primary->pipe = pipe;
12140 primary->plane = pipe;
c59cb179
MR
12141 primary->check_plane = intel_check_primary_plane;
12142 primary->commit_plane = intel_commit_primary_plane;
465c120c
MR
12143 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12144 primary->plane = !pipe;
12145
12146 if (INTEL_INFO(dev)->gen <= 3) {
12147 intel_primary_formats = intel_primary_formats_gen2;
12148 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12149 } else {
12150 intel_primary_formats = intel_primary_formats_gen4;
12151 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12152 }
12153
12154 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 12155 &intel_plane_funcs,
465c120c
MR
12156 intel_primary_formats, num_formats,
12157 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
12158
12159 if (INTEL_INFO(dev)->gen >= 4) {
12160 if (!dev->mode_config.rotation_property)
12161 dev->mode_config.rotation_property =
12162 drm_mode_create_rotation_property(dev,
12163 BIT(DRM_ROTATE_0) |
12164 BIT(DRM_ROTATE_180));
12165 if (dev->mode_config.rotation_property)
12166 drm_object_attach_property(&primary->base.base,
12167 dev->mode_config.rotation_property,
8e7d688b 12168 state->base.rotation);
48404c1e
SJ
12169 }
12170
ea2c67bb
MR
12171 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12172
465c120c
MR
12173 return &primary->base;
12174}
12175
3d7d6510 12176static int
852e787c
GP
12177intel_check_cursor_plane(struct drm_plane *plane,
12178 struct intel_plane_state *state)
3d7d6510 12179{
2b875c22 12180 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 12181 struct drm_device *dev = plane->dev;
2b875c22 12182 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
12183 struct drm_rect *dest = &state->dst;
12184 struct drm_rect *src = &state->src;
12185 const struct drm_rect *clip = &state->clip;
757f9a3e 12186 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 12187 struct intel_crtc *intel_crtc;
757f9a3e
GP
12188 unsigned stride;
12189 int ret;
3d7d6510 12190
ea2c67bb
MR
12191 crtc = crtc ? crtc : plane->crtc;
12192 intel_crtc = to_intel_crtc(crtc);
12193
757f9a3e 12194 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 12195 src, dest, clip,
3d7d6510
MR
12196 DRM_PLANE_HELPER_NO_SCALING,
12197 DRM_PLANE_HELPER_NO_SCALING,
852e787c 12198 true, true, &state->visible);
757f9a3e
GP
12199 if (ret)
12200 return ret;
12201
12202
12203 /* if we want to turn off the cursor ignore width and height */
12204 if (!obj)
32b7eeec 12205 goto finish;
757f9a3e 12206
757f9a3e 12207 /* Check for which cursor types we support */
ea2c67bb
MR
12208 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12209 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12210 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
12211 return -EINVAL;
12212 }
12213
ea2c67bb
MR
12214 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12215 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
12216 DRM_DEBUG_KMS("buffer is too small\n");
12217 return -ENOMEM;
12218 }
12219
e391ea88
GP
12220 if (fb == crtc->cursor->fb)
12221 return 0;
12222
757f9a3e
GP
12223 /* we only need to pin inside GTT if cursor is non-phy */
12224 mutex_lock(&dev->struct_mutex);
12225 if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
12226 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12227 ret = -EINVAL;
12228 }
12229 mutex_unlock(&dev->struct_mutex);
12230
32b7eeec
MR
12231finish:
12232 if (intel_crtc->active) {
ea2c67bb 12233 if (intel_crtc->cursor_width != state->base.crtc_w)
32b7eeec
MR
12234 intel_crtc->atomic.update_wm = true;
12235
12236 intel_crtc->atomic.fb_bits |=
12237 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12238 }
12239
757f9a3e 12240 return ret;
852e787c 12241}
3d7d6510 12242
f4a2cf29 12243static void
852e787c
GP
12244intel_commit_cursor_plane(struct drm_plane *plane,
12245 struct intel_plane_state *state)
12246{
2b875c22 12247 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
12248 struct drm_device *dev = plane->dev;
12249 struct intel_crtc *intel_crtc;
a919db90 12250 struct intel_plane *intel_plane = to_intel_plane(plane);
2b875c22 12251 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 12252 uint32_t addr;
852e787c 12253
ea2c67bb
MR
12254 crtc = crtc ? crtc : plane->crtc;
12255 intel_crtc = to_intel_crtc(crtc);
12256
2b875c22 12257 plane->fb = state->base.fb;
ea2c67bb
MR
12258 crtc->cursor_x = state->base.crtc_x;
12259 crtc->cursor_y = state->base.crtc_y;
12260
a919db90
SJ
12261 intel_plane->obj = obj;
12262
a912f12f
GP
12263 if (intel_crtc->cursor_bo == obj)
12264 goto update;
4ed91096 12265
f4a2cf29 12266 if (!obj)
a912f12f 12267 addr = 0;
f4a2cf29 12268 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 12269 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 12270 else
a912f12f 12271 addr = obj->phys_handle->busaddr;
852e787c 12272
a912f12f
GP
12273 intel_crtc->cursor_addr = addr;
12274 intel_crtc->cursor_bo = obj;
12275update:
ea2c67bb
MR
12276 intel_crtc->cursor_width = state->base.crtc_w;
12277 intel_crtc->cursor_height = state->base.crtc_h;
852e787c 12278
32b7eeec 12279 if (intel_crtc->active)
a912f12f 12280 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
12281}
12282
3d7d6510
MR
12283static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12284 int pipe)
12285{
12286 struct intel_plane *cursor;
8e7d688b 12287 struct intel_plane_state *state;
3d7d6510
MR
12288
12289 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12290 if (cursor == NULL)
12291 return NULL;
12292
8e7d688b
MR
12293 state = intel_create_plane_state(&cursor->base);
12294 if (!state) {
ea2c67bb
MR
12295 kfree(cursor);
12296 return NULL;
12297 }
8e7d688b 12298 cursor->base.state = &state->base;
ea2c67bb 12299
3d7d6510
MR
12300 cursor->can_scale = false;
12301 cursor->max_downscale = 1;
12302 cursor->pipe = pipe;
12303 cursor->plane = pipe;
c59cb179
MR
12304 cursor->check_plane = intel_check_cursor_plane;
12305 cursor->commit_plane = intel_commit_cursor_plane;
3d7d6510
MR
12306
12307 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 12308 &intel_plane_funcs,
3d7d6510
MR
12309 intel_cursor_formats,
12310 ARRAY_SIZE(intel_cursor_formats),
12311 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
12312
12313 if (INTEL_INFO(dev)->gen >= 4) {
12314 if (!dev->mode_config.rotation_property)
12315 dev->mode_config.rotation_property =
12316 drm_mode_create_rotation_property(dev,
12317 BIT(DRM_ROTATE_0) |
12318 BIT(DRM_ROTATE_180));
12319 if (dev->mode_config.rotation_property)
12320 drm_object_attach_property(&cursor->base.base,
12321 dev->mode_config.rotation_property,
8e7d688b 12322 state->base.rotation);
4398ad45
VS
12323 }
12324
ea2c67bb
MR
12325 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12326
3d7d6510
MR
12327 return &cursor->base;
12328}
12329
b358d0a6 12330static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12331{
fbee40df 12332 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12333 struct intel_crtc *intel_crtc;
f5de6e07 12334 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
12335 struct drm_plane *primary = NULL;
12336 struct drm_plane *cursor = NULL;
465c120c 12337 int i, ret;
79e53945 12338
955382f3 12339 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12340 if (intel_crtc == NULL)
12341 return;
12342
f5de6e07
ACO
12343 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12344 if (!crtc_state)
12345 goto fail;
12346 intel_crtc_set_state(intel_crtc, crtc_state);
12347
465c120c 12348 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12349 if (!primary)
12350 goto fail;
12351
12352 cursor = intel_cursor_plane_create(dev, pipe);
12353 if (!cursor)
12354 goto fail;
12355
465c120c 12356 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12357 cursor, &intel_crtc_funcs);
12358 if (ret)
12359 goto fail;
79e53945
JB
12360
12361 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12362 for (i = 0; i < 256; i++) {
12363 intel_crtc->lut_r[i] = i;
12364 intel_crtc->lut_g[i] = i;
12365 intel_crtc->lut_b[i] = i;
12366 }
12367
1f1c2e24
VS
12368 /*
12369 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12370 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12371 */
80824003
JB
12372 intel_crtc->pipe = pipe;
12373 intel_crtc->plane = pipe;
3a77c4c4 12374 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12375 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12376 intel_crtc->plane = !pipe;
80824003
JB
12377 }
12378
4b0e333e
CW
12379 intel_crtc->cursor_base = ~0;
12380 intel_crtc->cursor_cntl = ~0;
dc41c154 12381 intel_crtc->cursor_size = ~0;
8d7849db 12382
22fd0fab
JB
12383 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12384 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12385 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12386 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12387
9362c7c5
ACO
12388 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12389
79e53945 12390 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12391
12392 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12393 return;
12394
12395fail:
12396 if (primary)
12397 drm_plane_cleanup(primary);
12398 if (cursor)
12399 drm_plane_cleanup(cursor);
f5de6e07 12400 kfree(crtc_state);
3d7d6510 12401 kfree(intel_crtc);
79e53945
JB
12402}
12403
752aa88a
JB
12404enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12405{
12406 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12407 struct drm_device *dev = connector->base.dev;
752aa88a 12408
51fd371b 12409 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 12410
d3babd3f 12411 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
12412 return INVALID_PIPE;
12413
12414 return to_intel_crtc(encoder->crtc)->pipe;
12415}
12416
08d7b3d1 12417int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12418 struct drm_file *file)
08d7b3d1 12419{
08d7b3d1 12420 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12421 struct drm_crtc *drmmode_crtc;
c05422d5 12422 struct intel_crtc *crtc;
08d7b3d1 12423
1cff8f6b
DV
12424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12425 return -ENODEV;
08d7b3d1 12426
7707e653 12427 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12428
7707e653 12429 if (!drmmode_crtc) {
08d7b3d1 12430 DRM_ERROR("no such CRTC id\n");
3f2c2057 12431 return -ENOENT;
08d7b3d1
CW
12432 }
12433
7707e653 12434 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12435 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12436
c05422d5 12437 return 0;
08d7b3d1
CW
12438}
12439
66a9278e 12440static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12441{
66a9278e
DV
12442 struct drm_device *dev = encoder->base.dev;
12443 struct intel_encoder *source_encoder;
79e53945 12444 int index_mask = 0;
79e53945
JB
12445 int entry = 0;
12446
b2784e15 12447 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12448 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12449 index_mask |= (1 << entry);
12450
79e53945
JB
12451 entry++;
12452 }
4ef69c7a 12453
79e53945
JB
12454 return index_mask;
12455}
12456
4d302442
CW
12457static bool has_edp_a(struct drm_device *dev)
12458{
12459 struct drm_i915_private *dev_priv = dev->dev_private;
12460
12461 if (!IS_MOBILE(dev))
12462 return false;
12463
12464 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12465 return false;
12466
e3589908 12467 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12468 return false;
12469
12470 return true;
12471}
12472
84b4e042
JB
12473static bool intel_crt_present(struct drm_device *dev)
12474{
12475 struct drm_i915_private *dev_priv = dev->dev_private;
12476
884497ed
DL
12477 if (INTEL_INFO(dev)->gen >= 9)
12478 return false;
12479
cf404ce4 12480 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
12481 return false;
12482
12483 if (IS_CHERRYVIEW(dev))
12484 return false;
12485
12486 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12487 return false;
12488
12489 return true;
12490}
12491
79e53945
JB
12492static void intel_setup_outputs(struct drm_device *dev)
12493{
725e30ad 12494 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12495 struct intel_encoder *encoder;
c6f95f27 12496 struct drm_connector *connector;
cb0953d7 12497 bool dpd_is_edp = false;
79e53945 12498
c9093354 12499 intel_lvds_init(dev);
79e53945 12500
84b4e042 12501 if (intel_crt_present(dev))
79935fca 12502 intel_crt_init(dev);
cb0953d7 12503
affa9354 12504 if (HAS_DDI(dev)) {
0e72a5b5
ED
12505 int found;
12506
12507 /* Haswell uses DDI functions to detect digital outputs */
12508 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12509 /* DDI A only supports eDP */
12510 if (found)
12511 intel_ddi_init(dev, PORT_A);
12512
12513 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12514 * register */
12515 found = I915_READ(SFUSE_STRAP);
12516
12517 if (found & SFUSE_STRAP_DDIB_DETECTED)
12518 intel_ddi_init(dev, PORT_B);
12519 if (found & SFUSE_STRAP_DDIC_DETECTED)
12520 intel_ddi_init(dev, PORT_C);
12521 if (found & SFUSE_STRAP_DDID_DETECTED)
12522 intel_ddi_init(dev, PORT_D);
12523 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12524 int found;
5d8a7752 12525 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12526
12527 if (has_edp_a(dev))
12528 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12529
dc0fa718 12530 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12531 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12532 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12533 if (!found)
e2debe91 12534 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12535 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12536 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12537 }
12538
dc0fa718 12539 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12540 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12541
dc0fa718 12542 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12543 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12544
5eb08b69 12545 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12546 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12547
270b3042 12548 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12549 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12550 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
12551 /*
12552 * The DP_DETECTED bit is the latched state of the DDC
12553 * SDA pin at boot. However since eDP doesn't require DDC
12554 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12555 * eDP ports may have been muxed to an alternate function.
12556 * Thus we can't rely on the DP_DETECTED bit alone to detect
12557 * eDP ports. Consult the VBT as well as DP_DETECTED to
12558 * detect eDP ports.
12559 */
d2182a66
VS
12560 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12561 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
12562 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12563 PORT_B);
e17ac6db
VS
12564 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12565 intel_dp_is_edp(dev, PORT_B))
12566 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 12567
d2182a66
VS
12568 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12569 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
12570 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12571 PORT_C);
e17ac6db
VS
12572 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12573 intel_dp_is_edp(dev, PORT_C))
12574 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 12575
9418c1f1 12576 if (IS_CHERRYVIEW(dev)) {
e17ac6db 12577 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
12578 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12579 PORT_D);
e17ac6db
VS
12580 /* eDP not supported on port D, so don't check VBT */
12581 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12582 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
12583 }
12584
3cfca973 12585 intel_dsi_init(dev);
103a196f 12586 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12587 bool found = false;
7d57382e 12588
e2debe91 12589 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12590 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12591 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12592 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12593 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12594 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12595 }
27185ae1 12596
e7281eab 12597 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12598 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12599 }
13520b05
KH
12600
12601 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12602
e2debe91 12603 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12604 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12605 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12606 }
27185ae1 12607
e2debe91 12608 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12609
b01f2c3a
JB
12610 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12611 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12612 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12613 }
e7281eab 12614 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12615 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12616 }
27185ae1 12617
b01f2c3a 12618 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12619 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12620 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12621 } else if (IS_GEN2(dev))
79e53945
JB
12622 intel_dvo_init(dev);
12623
103a196f 12624 if (SUPPORTS_TV(dev))
79e53945
JB
12625 intel_tv_init(dev);
12626
c6f95f27
MR
12627 /*
12628 * FIXME: We don't have full atomic support yet, but we want to be
12629 * able to enable/test plane updates via the atomic interface in the
12630 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12631 * will take some atomic codepaths to lookup properties during
12632 * drmModeGetConnector() that unconditionally dereference
12633 * connector->state.
12634 *
12635 * We create a dummy connector state here for each connector to ensure
12636 * the DRM core doesn't try to dereference a NULL connector->state.
12637 * The actual connector properties will never be updated or contain
12638 * useful information, but since we're doing this specifically for
12639 * testing/debug of the plane operations (and only when a specific
12640 * kernel module option is given), that shouldn't really matter.
12641 *
12642 * Once atomic support for crtc's + connectors lands, this loop should
12643 * be removed since we'll be setting up real connector state, which
12644 * will contain Intel-specific properties.
12645 */
12646 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12647 list_for_each_entry(connector,
12648 &dev->mode_config.connector_list,
12649 head) {
12650 if (!WARN_ON(connector->state)) {
12651 connector->state =
12652 kzalloc(sizeof(*connector->state),
12653 GFP_KERNEL);
12654 }
12655 }
12656 }
12657
0bc12bcb 12658 intel_psr_init(dev);
7c8f8a70 12659
b2784e15 12660 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12661 encoder->base.possible_crtcs = encoder->crtc_mask;
12662 encoder->base.possible_clones =
66a9278e 12663 intel_encoder_clones(encoder);
79e53945 12664 }
47356eb6 12665
dde86e2d 12666 intel_init_pch_refclk(dev);
270b3042
DV
12667
12668 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12669}
12670
12671static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12672{
60a5ca01 12673 struct drm_device *dev = fb->dev;
79e53945 12674 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12675
ef2d633e 12676 drm_framebuffer_cleanup(fb);
60a5ca01 12677 mutex_lock(&dev->struct_mutex);
ef2d633e 12678 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12679 drm_gem_object_unreference(&intel_fb->obj->base);
12680 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12681 kfree(intel_fb);
12682}
12683
12684static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12685 struct drm_file *file,
79e53945
JB
12686 unsigned int *handle)
12687{
12688 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12689 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12690
05394f39 12691 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12692}
12693
12694static const struct drm_framebuffer_funcs intel_fb_funcs = {
12695 .destroy = intel_user_framebuffer_destroy,
12696 .create_handle = intel_user_framebuffer_create_handle,
12697};
12698
b5ea642a
DV
12699static int intel_framebuffer_init(struct drm_device *dev,
12700 struct intel_framebuffer *intel_fb,
12701 struct drm_mode_fb_cmd2 *mode_cmd,
12702 struct drm_i915_gem_object *obj)
79e53945 12703{
a57ce0b2 12704 int aligned_height;
a35cdaa0 12705 int pitch_limit;
79e53945
JB
12706 int ret;
12707
dd4916c5
DV
12708 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12709
c16ed4be
CW
12710 if (obj->tiling_mode == I915_TILING_Y) {
12711 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12712 return -EINVAL;
c16ed4be 12713 }
57cd6508 12714
c16ed4be
CW
12715 if (mode_cmd->pitches[0] & 63) {
12716 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12717 mode_cmd->pitches[0]);
57cd6508 12718 return -EINVAL;
c16ed4be 12719 }
57cd6508 12720
a35cdaa0
CW
12721 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12722 pitch_limit = 32*1024;
12723 } else if (INTEL_INFO(dev)->gen >= 4) {
12724 if (obj->tiling_mode)
12725 pitch_limit = 16*1024;
12726 else
12727 pitch_limit = 32*1024;
12728 } else if (INTEL_INFO(dev)->gen >= 3) {
12729 if (obj->tiling_mode)
12730 pitch_limit = 8*1024;
12731 else
12732 pitch_limit = 16*1024;
12733 } else
12734 /* XXX DSPC is limited to 4k tiled */
12735 pitch_limit = 8*1024;
12736
12737 if (mode_cmd->pitches[0] > pitch_limit) {
12738 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12739 obj->tiling_mode ? "tiled" : "linear",
12740 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12741 return -EINVAL;
c16ed4be 12742 }
5d7bd705
VS
12743
12744 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12745 mode_cmd->pitches[0] != obj->stride) {
12746 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12747 mode_cmd->pitches[0], obj->stride);
5d7bd705 12748 return -EINVAL;
c16ed4be 12749 }
5d7bd705 12750
57779d06 12751 /* Reject formats not supported by any plane early. */
308e5bcb 12752 switch (mode_cmd->pixel_format) {
57779d06 12753 case DRM_FORMAT_C8:
04b3924d
VS
12754 case DRM_FORMAT_RGB565:
12755 case DRM_FORMAT_XRGB8888:
12756 case DRM_FORMAT_ARGB8888:
57779d06
VS
12757 break;
12758 case DRM_FORMAT_XRGB1555:
12759 case DRM_FORMAT_ARGB1555:
c16ed4be 12760 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12761 DRM_DEBUG("unsupported pixel format: %s\n",
12762 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12763 return -EINVAL;
c16ed4be 12764 }
57779d06
VS
12765 break;
12766 case DRM_FORMAT_XBGR8888:
12767 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12768 case DRM_FORMAT_XRGB2101010:
12769 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12770 case DRM_FORMAT_XBGR2101010:
12771 case DRM_FORMAT_ABGR2101010:
c16ed4be 12772 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12773 DRM_DEBUG("unsupported pixel format: %s\n",
12774 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12775 return -EINVAL;
c16ed4be 12776 }
b5626747 12777 break;
04b3924d
VS
12778 case DRM_FORMAT_YUYV:
12779 case DRM_FORMAT_UYVY:
12780 case DRM_FORMAT_YVYU:
12781 case DRM_FORMAT_VYUY:
c16ed4be 12782 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12783 DRM_DEBUG("unsupported pixel format: %s\n",
12784 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12785 return -EINVAL;
c16ed4be 12786 }
57cd6508
CW
12787 break;
12788 default:
4ee62c76
VS
12789 DRM_DEBUG("unsupported pixel format: %s\n",
12790 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12791 return -EINVAL;
12792 }
12793
90f9a336
VS
12794 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12795 if (mode_cmd->offsets[0] != 0)
12796 return -EINVAL;
12797
ec2c981e
DL
12798 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
12799 obj->tiling_mode);
53155c0a
DV
12800 /* FIXME drm helper for size checks (especially planar formats)? */
12801 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12802 return -EINVAL;
12803
c7d73f6a
DV
12804 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12805 intel_fb->obj = obj;
80075d49 12806 intel_fb->obj->framebuffer_references++;
c7d73f6a 12807
79e53945
JB
12808 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12809 if (ret) {
12810 DRM_ERROR("framebuffer init failed %d\n", ret);
12811 return ret;
12812 }
12813
79e53945
JB
12814 return 0;
12815}
12816
79e53945
JB
12817static struct drm_framebuffer *
12818intel_user_framebuffer_create(struct drm_device *dev,
12819 struct drm_file *filp,
308e5bcb 12820 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12821{
05394f39 12822 struct drm_i915_gem_object *obj;
79e53945 12823
308e5bcb
JB
12824 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12825 mode_cmd->handles[0]));
c8725226 12826 if (&obj->base == NULL)
cce13ff7 12827 return ERR_PTR(-ENOENT);
79e53945 12828
d2dff872 12829 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12830}
12831
4520f53a 12832#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12833static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12834{
12835}
12836#endif
12837
79e53945 12838static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12839 .fb_create = intel_user_framebuffer_create,
0632fef6 12840 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
12841 .atomic_check = intel_atomic_check,
12842 .atomic_commit = intel_atomic_commit,
79e53945
JB
12843};
12844
e70236a8
JB
12845/* Set up chip specific display functions */
12846static void intel_init_display(struct drm_device *dev)
12847{
12848 struct drm_i915_private *dev_priv = dev->dev_private;
12849
ee9300bb
DV
12850 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12851 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12852 else if (IS_CHERRYVIEW(dev))
12853 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12854 else if (IS_VALLEYVIEW(dev))
12855 dev_priv->display.find_dpll = vlv_find_best_dpll;
12856 else if (IS_PINEVIEW(dev))
12857 dev_priv->display.find_dpll = pnv_find_best_dpll;
12858 else
12859 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12860
bc8d7dff
DL
12861 if (INTEL_INFO(dev)->gen >= 9) {
12862 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
12863 dev_priv->display.get_initial_plane_config =
12864 skylake_get_initial_plane_config;
bc8d7dff
DL
12865 dev_priv->display.crtc_compute_clock =
12866 haswell_crtc_compute_clock;
12867 dev_priv->display.crtc_enable = haswell_crtc_enable;
12868 dev_priv->display.crtc_disable = haswell_crtc_disable;
12869 dev_priv->display.off = ironlake_crtc_off;
12870 dev_priv->display.update_primary_plane =
12871 skylake_update_primary_plane;
12872 } else if (HAS_DDI(dev)) {
0e8ffe1b 12873 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
12874 dev_priv->display.get_initial_plane_config =
12875 ironlake_get_initial_plane_config;
797d0259
ACO
12876 dev_priv->display.crtc_compute_clock =
12877 haswell_crtc_compute_clock;
4f771f10
PZ
12878 dev_priv->display.crtc_enable = haswell_crtc_enable;
12879 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12880 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
12881 dev_priv->display.update_primary_plane =
12882 ironlake_update_primary_plane;
09b4ddf9 12883 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12884 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
12885 dev_priv->display.get_initial_plane_config =
12886 ironlake_get_initial_plane_config;
3fb37703
ACO
12887 dev_priv->display.crtc_compute_clock =
12888 ironlake_crtc_compute_clock;
76e5a89c
DV
12889 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12890 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12891 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12892 dev_priv->display.update_primary_plane =
12893 ironlake_update_primary_plane;
89b667f8
JB
12894 } else if (IS_VALLEYVIEW(dev)) {
12895 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
12896 dev_priv->display.get_initial_plane_config =
12897 i9xx_get_initial_plane_config;
d6dfee7a 12898 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
12899 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12900 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12901 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12902 dev_priv->display.update_primary_plane =
12903 i9xx_update_primary_plane;
f564048e 12904 } else {
0e8ffe1b 12905 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
12906 dev_priv->display.get_initial_plane_config =
12907 i9xx_get_initial_plane_config;
d6dfee7a 12908 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
12909 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12910 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12911 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12912 dev_priv->display.update_primary_plane =
12913 i9xx_update_primary_plane;
f564048e 12914 }
e70236a8 12915
e70236a8 12916 /* Returns the core display clock speed */
25eb05fc
JB
12917 if (IS_VALLEYVIEW(dev))
12918 dev_priv->display.get_display_clock_speed =
12919 valleyview_get_display_clock_speed;
12920 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12921 dev_priv->display.get_display_clock_speed =
12922 i945_get_display_clock_speed;
12923 else if (IS_I915G(dev))
12924 dev_priv->display.get_display_clock_speed =
12925 i915_get_display_clock_speed;
257a7ffc 12926 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12927 dev_priv->display.get_display_clock_speed =
12928 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12929 else if (IS_PINEVIEW(dev))
12930 dev_priv->display.get_display_clock_speed =
12931 pnv_get_display_clock_speed;
e70236a8
JB
12932 else if (IS_I915GM(dev))
12933 dev_priv->display.get_display_clock_speed =
12934 i915gm_get_display_clock_speed;
12935 else if (IS_I865G(dev))
12936 dev_priv->display.get_display_clock_speed =
12937 i865_get_display_clock_speed;
f0f8a9ce 12938 else if (IS_I85X(dev))
e70236a8
JB
12939 dev_priv->display.get_display_clock_speed =
12940 i855_get_display_clock_speed;
12941 else /* 852, 830 */
12942 dev_priv->display.get_display_clock_speed =
12943 i830_get_display_clock_speed;
12944
7c10a2b5 12945 if (IS_GEN5(dev)) {
3bb11b53 12946 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
12947 } else if (IS_GEN6(dev)) {
12948 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
12949 } else if (IS_IVYBRIDGE(dev)) {
12950 /* FIXME: detect B0+ stepping and use auto training */
12951 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
3bb11b53
SJ
12952 dev_priv->display.modeset_global_resources =
12953 ivb_modeset_global_resources;
059b2fe9 12954 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 12955 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
12956 } else if (IS_VALLEYVIEW(dev)) {
12957 dev_priv->display.modeset_global_resources =
12958 valleyview_modeset_global_resources;
e70236a8 12959 }
8c9f3aaf
JB
12960
12961 /* Default just returns -ENODEV to indicate unsupported */
12962 dev_priv->display.queue_flip = intel_default_queue_flip;
12963
12964 switch (INTEL_INFO(dev)->gen) {
12965 case 2:
12966 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12967 break;
12968
12969 case 3:
12970 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12971 break;
12972
12973 case 4:
12974 case 5:
12975 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12976 break;
12977
12978 case 6:
12979 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12980 break;
7c9017e5 12981 case 7:
4e0bbc31 12982 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12983 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12984 break;
830c81db
DL
12985 case 9:
12986 dev_priv->display.queue_flip = intel_gen9_queue_flip;
12987 break;
8c9f3aaf 12988 }
7bd688cd
JN
12989
12990 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12991
12992 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12993}
12994
b690e96c
JB
12995/*
12996 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12997 * resume, or other times. This quirk makes sure that's the case for
12998 * affected systems.
12999 */
0206e353 13000static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
13001{
13002 struct drm_i915_private *dev_priv = dev->dev_private;
13003
13004 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 13005 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
13006}
13007
b6b5d049
VS
13008static void quirk_pipeb_force(struct drm_device *dev)
13009{
13010 struct drm_i915_private *dev_priv = dev->dev_private;
13011
13012 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13013 DRM_INFO("applying pipe b force quirk\n");
13014}
13015
435793df
KP
13016/*
13017 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13018 */
13019static void quirk_ssc_force_disable(struct drm_device *dev)
13020{
13021 struct drm_i915_private *dev_priv = dev->dev_private;
13022 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 13023 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
13024}
13025
4dca20ef 13026/*
5a15ab5b
CE
13027 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13028 * brightness value
4dca20ef
CE
13029 */
13030static void quirk_invert_brightness(struct drm_device *dev)
13031{
13032 struct drm_i915_private *dev_priv = dev->dev_private;
13033 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 13034 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
13035}
13036
9c72cc6f
SD
13037/* Some VBT's incorrectly indicate no backlight is present */
13038static void quirk_backlight_present(struct drm_device *dev)
13039{
13040 struct drm_i915_private *dev_priv = dev->dev_private;
13041 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13042 DRM_INFO("applying backlight present quirk\n");
13043}
13044
b690e96c
JB
13045struct intel_quirk {
13046 int device;
13047 int subsystem_vendor;
13048 int subsystem_device;
13049 void (*hook)(struct drm_device *dev);
13050};
13051
5f85f176
EE
13052/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13053struct intel_dmi_quirk {
13054 void (*hook)(struct drm_device *dev);
13055 const struct dmi_system_id (*dmi_id_list)[];
13056};
13057
13058static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13059{
13060 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13061 return 1;
13062}
13063
13064static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13065 {
13066 .dmi_id_list = &(const struct dmi_system_id[]) {
13067 {
13068 .callback = intel_dmi_reverse_brightness,
13069 .ident = "NCR Corporation",
13070 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13071 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13072 },
13073 },
13074 { } /* terminating entry */
13075 },
13076 .hook = quirk_invert_brightness,
13077 },
13078};
13079
c43b5634 13080static struct intel_quirk intel_quirks[] = {
b690e96c 13081 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 13082 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 13083
b690e96c
JB
13084 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13085 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13086
b690e96c
JB
13087 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13088 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13089
5f080c0f
VS
13090 /* 830 needs to leave pipe A & dpll A up */
13091 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13092
b6b5d049
VS
13093 /* 830 needs to leave pipe B & dpll B up */
13094 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13095
435793df
KP
13096 /* Lenovo U160 cannot use SSC on LVDS */
13097 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
13098
13099 /* Sony Vaio Y cannot use SSC on LVDS */
13100 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 13101
be505f64
AH
13102 /* Acer Aspire 5734Z must invert backlight brightness */
13103 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13104
13105 /* Acer/eMachines G725 */
13106 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13107
13108 /* Acer/eMachines e725 */
13109 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13110
13111 /* Acer/Packard Bell NCL20 */
13112 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13113
13114 /* Acer Aspire 4736Z */
13115 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
13116
13117 /* Acer Aspire 5336 */
13118 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
13119
13120 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13121 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 13122
dfb3d47b
SD
13123 /* Acer C720 Chromebook (Core i3 4005U) */
13124 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13125
b2a9601c 13126 /* Apple Macbook 2,1 (Core 2 T7400) */
13127 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13128
d4967d8c
SD
13129 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13130 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
13131
13132 /* HP Chromebook 14 (Celeron 2955U) */
13133 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
13134};
13135
13136static void intel_init_quirks(struct drm_device *dev)
13137{
13138 struct pci_dev *d = dev->pdev;
13139 int i;
13140
13141 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13142 struct intel_quirk *q = &intel_quirks[i];
13143
13144 if (d->device == q->device &&
13145 (d->subsystem_vendor == q->subsystem_vendor ||
13146 q->subsystem_vendor == PCI_ANY_ID) &&
13147 (d->subsystem_device == q->subsystem_device ||
13148 q->subsystem_device == PCI_ANY_ID))
13149 q->hook(dev);
13150 }
5f85f176
EE
13151 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13152 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13153 intel_dmi_quirks[i].hook(dev);
13154 }
b690e96c
JB
13155}
13156
9cce37f4
JB
13157/* Disable the VGA plane that we never use */
13158static void i915_disable_vga(struct drm_device *dev)
13159{
13160 struct drm_i915_private *dev_priv = dev->dev_private;
13161 u8 sr1;
766aa1c4 13162 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 13163
2b37c616 13164 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 13165 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 13166 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
13167 sr1 = inb(VGA_SR_DATA);
13168 outb(sr1 | 1<<5, VGA_SR_DATA);
13169 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13170 udelay(300);
13171
01f5a626 13172 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
13173 POSTING_READ(vga_reg);
13174}
13175
f817586c
DV
13176void intel_modeset_init_hw(struct drm_device *dev)
13177{
a8f78b58
ED
13178 intel_prepare_ddi(dev);
13179
f8bf63fd
VS
13180 if (IS_VALLEYVIEW(dev))
13181 vlv_update_cdclk(dev);
13182
f817586c
DV
13183 intel_init_clock_gating(dev);
13184
8090c6b9 13185 intel_enable_gt_powersave(dev);
f817586c
DV
13186}
13187
79e53945
JB
13188void intel_modeset_init(struct drm_device *dev)
13189{
652c393a 13190 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 13191 int sprite, ret;
8cc87b75 13192 enum pipe pipe;
46f297fb 13193 struct intel_crtc *crtc;
79e53945
JB
13194
13195 drm_mode_config_init(dev);
13196
13197 dev->mode_config.min_width = 0;
13198 dev->mode_config.min_height = 0;
13199
019d96cb
DA
13200 dev->mode_config.preferred_depth = 24;
13201 dev->mode_config.prefer_shadow = 1;
13202
e6ecefaa 13203 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 13204
b690e96c
JB
13205 intel_init_quirks(dev);
13206
1fa61106
ED
13207 intel_init_pm(dev);
13208
e3c74757
BW
13209 if (INTEL_INFO(dev)->num_pipes == 0)
13210 return;
13211
e70236a8 13212 intel_init_display(dev);
7c10a2b5 13213 intel_init_audio(dev);
e70236a8 13214
a6c45cf0
CW
13215 if (IS_GEN2(dev)) {
13216 dev->mode_config.max_width = 2048;
13217 dev->mode_config.max_height = 2048;
13218 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
13219 dev->mode_config.max_width = 4096;
13220 dev->mode_config.max_height = 4096;
79e53945 13221 } else {
a6c45cf0
CW
13222 dev->mode_config.max_width = 8192;
13223 dev->mode_config.max_height = 8192;
79e53945 13224 }
068be561 13225
dc41c154
VS
13226 if (IS_845G(dev) || IS_I865G(dev)) {
13227 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13228 dev->mode_config.cursor_height = 1023;
13229 } else if (IS_GEN2(dev)) {
068be561
DL
13230 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13231 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13232 } else {
13233 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13234 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13235 }
13236
5d4545ae 13237 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 13238
28c97730 13239 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
13240 INTEL_INFO(dev)->num_pipes,
13241 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 13242
055e393f 13243 for_each_pipe(dev_priv, pipe) {
8cc87b75 13244 intel_crtc_init(dev, pipe);
1fe47785
DL
13245 for_each_sprite(pipe, sprite) {
13246 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 13247 if (ret)
06da8da2 13248 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 13249 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 13250 }
79e53945
JB
13251 }
13252
f42bb70d
JB
13253 intel_init_dpio(dev);
13254
e72f9fbf 13255 intel_shared_dpll_init(dev);
ee7b9f93 13256
9cce37f4
JB
13257 /* Just disable it once at startup */
13258 i915_disable_vga(dev);
79e53945 13259 intel_setup_outputs(dev);
11be49eb
CW
13260
13261 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 13262 intel_fbc_disable(dev);
fa9fa083 13263
6e9f798d 13264 drm_modeset_lock_all(dev);
fa9fa083 13265 intel_modeset_setup_hw_state(dev, false);
6e9f798d 13266 drm_modeset_unlock_all(dev);
46f297fb 13267
d3fcc808 13268 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
13269 if (!crtc->active)
13270 continue;
13271
46f297fb 13272 /*
46f297fb
JB
13273 * Note that reserving the BIOS fb up front prevents us
13274 * from stuffing other stolen allocations like the ring
13275 * on top. This prevents some ugliness at boot time, and
13276 * can even allow for smooth boot transitions if the BIOS
13277 * fb is large enough for the active pipe configuration.
13278 */
5724dbd1
DL
13279 if (dev_priv->display.get_initial_plane_config) {
13280 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
13281 &crtc->plane_config);
13282 /*
13283 * If the fb is shared between multiple heads, we'll
13284 * just get the first one.
13285 */
484b41dd 13286 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 13287 }
46f297fb 13288 }
2c7111db
CW
13289}
13290
7fad798e
DV
13291static void intel_enable_pipe_a(struct drm_device *dev)
13292{
13293 struct intel_connector *connector;
13294 struct drm_connector *crt = NULL;
13295 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 13296 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
13297
13298 /* We can't just switch on the pipe A, we need to set things up with a
13299 * proper mode and output configuration. As a gross hack, enable pipe A
13300 * by enabling the load detect pipe once. */
13301 list_for_each_entry(connector,
13302 &dev->mode_config.connector_list,
13303 base.head) {
13304 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13305 crt = &connector->base;
13306 break;
13307 }
13308 }
13309
13310 if (!crt)
13311 return;
13312
208bf9fd
VS
13313 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13314 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
13315}
13316
fa555837
DV
13317static bool
13318intel_check_plane_mapping(struct intel_crtc *crtc)
13319{
7eb552ae
BW
13320 struct drm_device *dev = crtc->base.dev;
13321 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
13322 u32 reg, val;
13323
7eb552ae 13324 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13325 return true;
13326
13327 reg = DSPCNTR(!crtc->plane);
13328 val = I915_READ(reg);
13329
13330 if ((val & DISPLAY_PLANE_ENABLE) &&
13331 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13332 return false;
13333
13334 return true;
13335}
13336
24929352
DV
13337static void intel_sanitize_crtc(struct intel_crtc *crtc)
13338{
13339 struct drm_device *dev = crtc->base.dev;
13340 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13341 u32 reg;
24929352 13342
24929352 13343 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 13344 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
13345 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13346
d3eaf884 13347 /* restore vblank interrupts to correct state */
d297e103
VS
13348 if (crtc->active) {
13349 update_scanline_offset(crtc);
d3eaf884 13350 drm_vblank_on(dev, crtc->pipe);
d297e103 13351 } else
d3eaf884
VS
13352 drm_vblank_off(dev, crtc->pipe);
13353
24929352 13354 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13355 * disable the crtc (and hence change the state) if it is wrong. Note
13356 * that gen4+ has a fixed plane -> pipe mapping. */
13357 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13358 struct intel_connector *connector;
13359 bool plane;
13360
24929352
DV
13361 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13362 crtc->base.base.id);
13363
13364 /* Pipe has the wrong plane attached and the plane is active.
13365 * Temporarily change the plane mapping and disable everything
13366 * ... */
13367 plane = crtc->plane;
13368 crtc->plane = !plane;
9c8958bc 13369 crtc->primary_enabled = true;
24929352
DV
13370 dev_priv->display.crtc_disable(&crtc->base);
13371 crtc->plane = plane;
13372
13373 /* ... and break all links. */
13374 list_for_each_entry(connector, &dev->mode_config.connector_list,
13375 base.head) {
13376 if (connector->encoder->base.crtc != &crtc->base)
13377 continue;
13378
7f1950fb
EE
13379 connector->base.dpms = DRM_MODE_DPMS_OFF;
13380 connector->base.encoder = NULL;
24929352 13381 }
7f1950fb
EE
13382 /* multiple connectors may have the same encoder:
13383 * handle them and break crtc link separately */
13384 list_for_each_entry(connector, &dev->mode_config.connector_list,
13385 base.head)
13386 if (connector->encoder->base.crtc == &crtc->base) {
13387 connector->encoder->base.crtc = NULL;
13388 connector->encoder->connectors_active = false;
13389 }
24929352
DV
13390
13391 WARN_ON(crtc->active);
13392 crtc->base.enabled = false;
13393 }
24929352 13394
7fad798e
DV
13395 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13396 crtc->pipe == PIPE_A && !crtc->active) {
13397 /* BIOS forgot to enable pipe A, this mostly happens after
13398 * resume. Force-enable the pipe to fix this, the update_dpms
13399 * call below we restore the pipe to the right state, but leave
13400 * the required bits on. */
13401 intel_enable_pipe_a(dev);
13402 }
13403
24929352
DV
13404 /* Adjust the state of the output pipe according to whether we
13405 * have active connectors/encoders. */
13406 intel_crtc_update_dpms(&crtc->base);
13407
13408 if (crtc->active != crtc->base.enabled) {
13409 struct intel_encoder *encoder;
13410
13411 /* This can happen either due to bugs in the get_hw_state
13412 * functions or because the pipe is force-enabled due to the
13413 * pipe A quirk. */
13414 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13415 crtc->base.base.id,
13416 crtc->base.enabled ? "enabled" : "disabled",
13417 crtc->active ? "enabled" : "disabled");
13418
13419 crtc->base.enabled = crtc->active;
13420
13421 /* Because we only establish the connector -> encoder ->
13422 * crtc links if something is active, this means the
13423 * crtc is now deactivated. Break the links. connector
13424 * -> encoder links are only establish when things are
13425 * actually up, hence no need to break them. */
13426 WARN_ON(crtc->active);
13427
13428 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13429 WARN_ON(encoder->connectors_active);
13430 encoder->base.crtc = NULL;
13431 }
13432 }
c5ab3bc0 13433
a3ed6aad 13434 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13435 /*
13436 * We start out with underrun reporting disabled to avoid races.
13437 * For correct bookkeeping mark this on active crtcs.
13438 *
c5ab3bc0
DV
13439 * Also on gmch platforms we dont have any hardware bits to
13440 * disable the underrun reporting. Which means we need to start
13441 * out with underrun reporting disabled also on inactive pipes,
13442 * since otherwise we'll complain about the garbage we read when
13443 * e.g. coming up after runtime pm.
13444 *
4cc31489
DV
13445 * No protection against concurrent access is required - at
13446 * worst a fifo underrun happens which also sets this to false.
13447 */
13448 crtc->cpu_fifo_underrun_disabled = true;
13449 crtc->pch_fifo_underrun_disabled = true;
13450 }
24929352
DV
13451}
13452
13453static void intel_sanitize_encoder(struct intel_encoder *encoder)
13454{
13455 struct intel_connector *connector;
13456 struct drm_device *dev = encoder->base.dev;
13457
13458 /* We need to check both for a crtc link (meaning that the
13459 * encoder is active and trying to read from a pipe) and the
13460 * pipe itself being active. */
13461 bool has_active_crtc = encoder->base.crtc &&
13462 to_intel_crtc(encoder->base.crtc)->active;
13463
13464 if (encoder->connectors_active && !has_active_crtc) {
13465 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13466 encoder->base.base.id,
8e329a03 13467 encoder->base.name);
24929352
DV
13468
13469 /* Connector is active, but has no active pipe. This is
13470 * fallout from our resume register restoring. Disable
13471 * the encoder manually again. */
13472 if (encoder->base.crtc) {
13473 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13474 encoder->base.base.id,
8e329a03 13475 encoder->base.name);
24929352 13476 encoder->disable(encoder);
a62d1497
VS
13477 if (encoder->post_disable)
13478 encoder->post_disable(encoder);
24929352 13479 }
7f1950fb
EE
13480 encoder->base.crtc = NULL;
13481 encoder->connectors_active = false;
24929352
DV
13482
13483 /* Inconsistent output/port/pipe state happens presumably due to
13484 * a bug in one of the get_hw_state functions. Or someplace else
13485 * in our code, like the register restore mess on resume. Clamp
13486 * things to off as a safer default. */
13487 list_for_each_entry(connector,
13488 &dev->mode_config.connector_list,
13489 base.head) {
13490 if (connector->encoder != encoder)
13491 continue;
7f1950fb
EE
13492 connector->base.dpms = DRM_MODE_DPMS_OFF;
13493 connector->base.encoder = NULL;
24929352
DV
13494 }
13495 }
13496 /* Enabled encoders without active connectors will be fixed in
13497 * the crtc fixup. */
13498}
13499
04098753 13500void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13501{
13502 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13503 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13504
04098753
ID
13505 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13506 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13507 i915_disable_vga(dev);
13508 }
13509}
13510
13511void i915_redisable_vga(struct drm_device *dev)
13512{
13513 struct drm_i915_private *dev_priv = dev->dev_private;
13514
8dc8a27c
PZ
13515 /* This function can be called both from intel_modeset_setup_hw_state or
13516 * at a very early point in our resume sequence, where the power well
13517 * structures are not yet restored. Since this function is at a very
13518 * paranoid "someone might have enabled VGA while we were not looking"
13519 * level, just check if the power well is enabled instead of trying to
13520 * follow the "don't touch the power well if we don't need it" policy
13521 * the rest of the driver uses. */
f458ebbc 13522 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13523 return;
13524
04098753 13525 i915_redisable_vga_power_on(dev);
0fde901f
KM
13526}
13527
98ec7739
VS
13528static bool primary_get_hw_state(struct intel_crtc *crtc)
13529{
13530 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13531
13532 if (!crtc->active)
13533 return false;
13534
13535 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13536}
13537
30e984df 13538static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13539{
13540 struct drm_i915_private *dev_priv = dev->dev_private;
13541 enum pipe pipe;
24929352
DV
13542 struct intel_crtc *crtc;
13543 struct intel_encoder *encoder;
13544 struct intel_connector *connector;
5358901f 13545 int i;
24929352 13546
d3fcc808 13547 for_each_intel_crtc(dev, crtc) {
6e3c9717 13548 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 13549
6e3c9717 13550 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 13551
0e8ffe1b 13552 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 13553 crtc->config);
24929352
DV
13554
13555 crtc->base.enabled = crtc->active;
98ec7739 13556 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13557
13558 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13559 crtc->base.base.id,
13560 crtc->active ? "enabled" : "disabled");
13561 }
13562
5358901f
DV
13563 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13564 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13565
3e369b76
ACO
13566 pll->on = pll->get_hw_state(dev_priv, pll,
13567 &pll->config.hw_state);
5358901f 13568 pll->active = 0;
3e369b76 13569 pll->config.crtc_mask = 0;
d3fcc808 13570 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 13571 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 13572 pll->active++;
3e369b76 13573 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 13574 }
5358901f 13575 }
5358901f 13576
1e6f2ddc 13577 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 13578 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 13579
3e369b76 13580 if (pll->config.crtc_mask)
bd2bb1b9 13581 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13582 }
13583
b2784e15 13584 for_each_intel_encoder(dev, encoder) {
24929352
DV
13585 pipe = 0;
13586
13587 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13588 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13589 encoder->base.crtc = &crtc->base;
6e3c9717 13590 encoder->get_config(encoder, crtc->config);
24929352
DV
13591 } else {
13592 encoder->base.crtc = NULL;
13593 }
13594
13595 encoder->connectors_active = false;
6f2bcceb 13596 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13597 encoder->base.base.id,
8e329a03 13598 encoder->base.name,
24929352 13599 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13600 pipe_name(pipe));
24929352
DV
13601 }
13602
13603 list_for_each_entry(connector, &dev->mode_config.connector_list,
13604 base.head) {
13605 if (connector->get_hw_state(connector)) {
13606 connector->base.dpms = DRM_MODE_DPMS_ON;
13607 connector->encoder->connectors_active = true;
13608 connector->base.encoder = &connector->encoder->base;
13609 } else {
13610 connector->base.dpms = DRM_MODE_DPMS_OFF;
13611 connector->base.encoder = NULL;
13612 }
13613 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13614 connector->base.base.id,
c23cc417 13615 connector->base.name,
24929352
DV
13616 connector->base.encoder ? "enabled" : "disabled");
13617 }
30e984df
DV
13618}
13619
13620/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13621 * and i915 state tracking structures. */
13622void intel_modeset_setup_hw_state(struct drm_device *dev,
13623 bool force_restore)
13624{
13625 struct drm_i915_private *dev_priv = dev->dev_private;
13626 enum pipe pipe;
30e984df
DV
13627 struct intel_crtc *crtc;
13628 struct intel_encoder *encoder;
35c95375 13629 int i;
30e984df
DV
13630
13631 intel_modeset_readout_hw_state(dev);
24929352 13632
babea61d
JB
13633 /*
13634 * Now that we have the config, copy it to each CRTC struct
13635 * Note that this could go away if we move to using crtc_config
13636 * checking everywhere.
13637 */
d3fcc808 13638 for_each_intel_crtc(dev, crtc) {
d330a953 13639 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
13640 intel_mode_from_pipe_config(&crtc->base.mode,
13641 crtc->config);
babea61d
JB
13642 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13643 crtc->base.base.id);
13644 drm_mode_debug_printmodeline(&crtc->base.mode);
13645 }
13646 }
13647
24929352 13648 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13649 for_each_intel_encoder(dev, encoder) {
24929352
DV
13650 intel_sanitize_encoder(encoder);
13651 }
13652
055e393f 13653 for_each_pipe(dev_priv, pipe) {
24929352
DV
13654 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13655 intel_sanitize_crtc(crtc);
6e3c9717
ACO
13656 intel_dump_pipe_config(crtc, crtc->config,
13657 "[setup_hw_state]");
24929352 13658 }
9a935856 13659
35c95375
DV
13660 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13661 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13662
13663 if (!pll->on || pll->active)
13664 continue;
13665
13666 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13667
13668 pll->disable(dev_priv, pll);
13669 pll->on = false;
13670 }
13671
3078999f
PB
13672 if (IS_GEN9(dev))
13673 skl_wm_get_hw_state(dev);
13674 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13675 ilk_wm_get_hw_state(dev);
13676
45e2b5f6 13677 if (force_restore) {
7d0bc1ea
VS
13678 i915_redisable_vga(dev);
13679
f30da187
DV
13680 /*
13681 * We need to use raw interfaces for restoring state to avoid
13682 * checking (bogus) intermediate states.
13683 */
055e393f 13684 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13685 struct drm_crtc *crtc =
13686 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 13687
7f27126e
JB
13688 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13689 crtc->primary->fb);
45e2b5f6
DV
13690 }
13691 } else {
13692 intel_modeset_update_staged_output_state(dev);
13693 }
8af6cf88
DV
13694
13695 intel_modeset_check_state(dev);
2c7111db
CW
13696}
13697
13698void intel_modeset_gem_init(struct drm_device *dev)
13699{
92122789 13700 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 13701 struct drm_crtc *c;
2ff8fde1 13702 struct drm_i915_gem_object *obj;
484b41dd 13703
ae48434c
ID
13704 mutex_lock(&dev->struct_mutex);
13705 intel_init_gt_powersave(dev);
13706 mutex_unlock(&dev->struct_mutex);
13707
92122789
JB
13708 /*
13709 * There may be no VBT; and if the BIOS enabled SSC we can
13710 * just keep using it to avoid unnecessary flicker. Whereas if the
13711 * BIOS isn't using it, don't assume it will work even if the VBT
13712 * indicates as much.
13713 */
13714 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13715 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13716 DREF_SSC1_ENABLE);
13717
1833b134 13718 intel_modeset_init_hw(dev);
02e792fb
DV
13719
13720 intel_setup_overlay(dev);
484b41dd
JB
13721
13722 /*
13723 * Make sure any fbs we allocated at startup are properly
13724 * pinned & fenced. When we do the allocation it's too early
13725 * for this.
13726 */
13727 mutex_lock(&dev->struct_mutex);
70e1e0ec 13728 for_each_crtc(dev, c) {
2ff8fde1
MR
13729 obj = intel_fb_obj(c->primary->fb);
13730 if (obj == NULL)
484b41dd
JB
13731 continue;
13732
850c4cdc
TU
13733 if (intel_pin_and_fence_fb_obj(c->primary,
13734 c->primary->fb,
13735 NULL)) {
484b41dd
JB
13736 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13737 to_intel_crtc(c)->pipe);
66e514c1
DA
13738 drm_framebuffer_unreference(c->primary->fb);
13739 c->primary->fb = NULL;
afd65eb4 13740 update_state_fb(c->primary);
484b41dd
JB
13741 }
13742 }
13743 mutex_unlock(&dev->struct_mutex);
0962c3c9
VS
13744
13745 intel_backlight_register(dev);
79e53945
JB
13746}
13747
4932e2c3
ID
13748void intel_connector_unregister(struct intel_connector *intel_connector)
13749{
13750 struct drm_connector *connector = &intel_connector->base;
13751
13752 intel_panel_destroy_backlight(connector);
34ea3d38 13753 drm_connector_unregister(connector);
4932e2c3
ID
13754}
13755
79e53945
JB
13756void intel_modeset_cleanup(struct drm_device *dev)
13757{
652c393a 13758 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13759 struct drm_connector *connector;
652c393a 13760
2eb5252e
ID
13761 intel_disable_gt_powersave(dev);
13762
0962c3c9
VS
13763 intel_backlight_unregister(dev);
13764
fd0c0642
DV
13765 /*
13766 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 13767 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
13768 * experience fancy races otherwise.
13769 */
2aeb7d3a 13770 intel_irq_uninstall(dev_priv);
eb21b92b 13771
fd0c0642
DV
13772 /*
13773 * Due to the hpd irq storm handling the hotplug work can re-arm the
13774 * poll handlers. Hence disable polling after hpd handling is shut down.
13775 */
f87ea761 13776 drm_kms_helper_poll_fini(dev);
fd0c0642 13777
652c393a
JB
13778 mutex_lock(&dev->struct_mutex);
13779
723bfd70
JB
13780 intel_unregister_dsm_handler();
13781
7ff0ebcc 13782 intel_fbc_disable(dev);
e70236a8 13783
930ebb46
DV
13784 ironlake_teardown_rc6(dev);
13785
69341a5e
KH
13786 mutex_unlock(&dev->struct_mutex);
13787
1630fe75
CW
13788 /* flush any delayed tasks or pending work */
13789 flush_scheduled_work();
13790
db31af1d
JN
13791 /* destroy the backlight and sysfs files before encoders/connectors */
13792 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13793 struct intel_connector *intel_connector;
13794
13795 intel_connector = to_intel_connector(connector);
13796 intel_connector->unregister(intel_connector);
db31af1d 13797 }
d9255d57 13798
79e53945 13799 drm_mode_config_cleanup(dev);
4d7bb011
DV
13800
13801 intel_cleanup_overlay(dev);
ae48434c
ID
13802
13803 mutex_lock(&dev->struct_mutex);
13804 intel_cleanup_gt_powersave(dev);
13805 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13806}
13807
f1c79df3
ZW
13808/*
13809 * Return which encoder is currently attached for connector.
13810 */
df0e9248 13811struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13812{
df0e9248
CW
13813 return &intel_attached_encoder(connector)->base;
13814}
f1c79df3 13815
df0e9248
CW
13816void intel_connector_attach_encoder(struct intel_connector *connector,
13817 struct intel_encoder *encoder)
13818{
13819 connector->encoder = encoder;
13820 drm_mode_connector_attach_encoder(&connector->base,
13821 &encoder->base);
79e53945 13822}
28d52043
DA
13823
13824/*
13825 * set vga decode state - true == enable VGA decode
13826 */
13827int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13828{
13829 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13830 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13831 u16 gmch_ctrl;
13832
75fa041d
CW
13833 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13834 DRM_ERROR("failed to read control word\n");
13835 return -EIO;
13836 }
13837
c0cc8a55
CW
13838 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13839 return 0;
13840
28d52043
DA
13841 if (state)
13842 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13843 else
13844 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13845
13846 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13847 DRM_ERROR("failed to write control word\n");
13848 return -EIO;
13849 }
13850
28d52043
DA
13851 return 0;
13852}
c4a1d9e4 13853
c4a1d9e4 13854struct intel_display_error_state {
ff57f1b0
PZ
13855
13856 u32 power_well_driver;
13857
63b66e5b
CW
13858 int num_transcoders;
13859
c4a1d9e4
CW
13860 struct intel_cursor_error_state {
13861 u32 control;
13862 u32 position;
13863 u32 base;
13864 u32 size;
52331309 13865 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13866
13867 struct intel_pipe_error_state {
ddf9c536 13868 bool power_domain_on;
c4a1d9e4 13869 u32 source;
f301b1e1 13870 u32 stat;
52331309 13871 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13872
13873 struct intel_plane_error_state {
13874 u32 control;
13875 u32 stride;
13876 u32 size;
13877 u32 pos;
13878 u32 addr;
13879 u32 surface;
13880 u32 tile_offset;
52331309 13881 } plane[I915_MAX_PIPES];
63b66e5b
CW
13882
13883 struct intel_transcoder_error_state {
ddf9c536 13884 bool power_domain_on;
63b66e5b
CW
13885 enum transcoder cpu_transcoder;
13886
13887 u32 conf;
13888
13889 u32 htotal;
13890 u32 hblank;
13891 u32 hsync;
13892 u32 vtotal;
13893 u32 vblank;
13894 u32 vsync;
13895 } transcoder[4];
c4a1d9e4
CW
13896};
13897
13898struct intel_display_error_state *
13899intel_display_capture_error_state(struct drm_device *dev)
13900{
fbee40df 13901 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13902 struct intel_display_error_state *error;
63b66e5b
CW
13903 int transcoders[] = {
13904 TRANSCODER_A,
13905 TRANSCODER_B,
13906 TRANSCODER_C,
13907 TRANSCODER_EDP,
13908 };
c4a1d9e4
CW
13909 int i;
13910
63b66e5b
CW
13911 if (INTEL_INFO(dev)->num_pipes == 0)
13912 return NULL;
13913
9d1cb914 13914 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13915 if (error == NULL)
13916 return NULL;
13917
190be112 13918 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13919 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13920
055e393f 13921 for_each_pipe(dev_priv, i) {
ddf9c536 13922 error->pipe[i].power_domain_on =
f458ebbc
DV
13923 __intel_display_power_is_enabled(dev_priv,
13924 POWER_DOMAIN_PIPE(i));
ddf9c536 13925 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13926 continue;
13927
5efb3e28
VS
13928 error->cursor[i].control = I915_READ(CURCNTR(i));
13929 error->cursor[i].position = I915_READ(CURPOS(i));
13930 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13931
13932 error->plane[i].control = I915_READ(DSPCNTR(i));
13933 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13934 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13935 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13936 error->plane[i].pos = I915_READ(DSPPOS(i));
13937 }
ca291363
PZ
13938 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13939 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13940 if (INTEL_INFO(dev)->gen >= 4) {
13941 error->plane[i].surface = I915_READ(DSPSURF(i));
13942 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13943 }
13944
c4a1d9e4 13945 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13946
3abfce77 13947 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13948 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13949 }
13950
13951 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13952 if (HAS_DDI(dev_priv->dev))
13953 error->num_transcoders++; /* Account for eDP. */
13954
13955 for (i = 0; i < error->num_transcoders; i++) {
13956 enum transcoder cpu_transcoder = transcoders[i];
13957
ddf9c536 13958 error->transcoder[i].power_domain_on =
f458ebbc 13959 __intel_display_power_is_enabled(dev_priv,
38cc1daf 13960 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13961 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13962 continue;
13963
63b66e5b
CW
13964 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13965
13966 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13967 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13968 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13969 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13970 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13971 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13972 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13973 }
13974
13975 return error;
13976}
13977
edc3d884
MK
13978#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13979
c4a1d9e4 13980void
edc3d884 13981intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13982 struct drm_device *dev,
13983 struct intel_display_error_state *error)
13984{
055e393f 13985 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13986 int i;
13987
63b66e5b
CW
13988 if (!error)
13989 return;
13990
edc3d884 13991 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13992 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13993 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13994 error->power_well_driver);
055e393f 13995 for_each_pipe(dev_priv, i) {
edc3d884 13996 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13997 err_printf(m, " Power: %s\n",
13998 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13999 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 14000 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
14001
14002 err_printf(m, "Plane [%d]:\n", i);
14003 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14004 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 14005 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
14006 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14007 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 14008 }
4b71a570 14009 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 14010 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 14011 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
14012 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14013 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
14014 }
14015
edc3d884
MK
14016 err_printf(m, "Cursor [%d]:\n", i);
14017 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14018 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14019 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 14020 }
63b66e5b
CW
14021
14022 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 14023 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 14024 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
14025 err_printf(m, " Power: %s\n",
14026 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
14027 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14028 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14029 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14030 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14031 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14032 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14033 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14034 }
c4a1d9e4 14035}
e2fcdaa9
VS
14036
14037void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14038{
14039 struct intel_crtc *crtc;
14040
14041 for_each_intel_crtc(dev, crtc) {
14042 struct intel_unpin_work *work;
e2fcdaa9 14043
5e2d7afc 14044 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
14045
14046 work = crtc->unpin_work;
14047
14048 if (work && work->event &&
14049 work->event->base.file_priv == file) {
14050 kfree(work->event);
14051 work->event = NULL;
14052 }
14053
5e2d7afc 14054 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
14055 }
14056}
This page took 2.602361 seconds and 5 git commands to generate.