]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Introduce a for_each_plane() macro
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
760285e7
DH
40#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
465c120c
MR
42#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
c0f372b3 44#include <linux/dma_remapping.h>
79e53945 45
465c120c
MR
46/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
3d7d6510
MR
71/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
ef9348c8 76#define DIV_ROUND_CLOSEST_ULL(ll, d) \
465c120c 77({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
ef9348c8 78
cc36513c
DV
79static void intel_increase_pllclock(struct drm_device *dev,
80 enum pipe pipe);
6b383a7f 81static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 82
f1f644dc
JB
83static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
84 struct intel_crtc_config *pipe_config);
18442d08
VS
85static void ironlake_pch_clock_get(struct intel_crtc *crtc,
86 struct intel_crtc_config *pipe_config);
f1f644dc 87
e7457a9a
DL
88static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
89 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 102static void vlv_prepare_pll(struct intel_crtc *crtc);
1ae0d137 103static void chv_prepare_pll(struct intel_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 */
414static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
415{
416 struct drm_device *dev = crtc->dev;
417 struct intel_encoder *encoder;
418
419 for_each_encoder_on_crtc(dev, crtc, encoder)
420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
1b894b59
CW
426static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
427 int refclk)
2c07245f 428{
b91ad0ec 429 struct drm_device *dev = crtc->dev;
2c07245f 430 const intel_limit_t *limit;
b91ad0ec
ZW
431
432 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 433 if (intel_is_dual_link_lvds(dev)) {
1b894b59 434 if (refclk == 100000)
b91ad0ec
ZW
435 limit = &intel_limits_ironlake_dual_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_dual_lvds;
438 } else {
1b894b59 439 if (refclk == 100000)
b91ad0ec
ZW
440 limit = &intel_limits_ironlake_single_lvds_100m;
441 else
442 limit = &intel_limits_ironlake_single_lvds;
443 }
c6bb3538 444 } else
b91ad0ec 445 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
446
447 return limit;
448}
449
044c7c41
ML
450static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
451{
452 struct drm_device *dev = crtc->dev;
044c7c41
ML
453 const intel_limit_t *limit;
454
455 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 456 if (intel_is_dual_link_lvds(dev))
e4b36699 457 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 458 else
e4b36699 459 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
461 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 462 limit = &intel_limits_g4x_hdmi;
044c7c41 463 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 464 limit = &intel_limits_g4x_sdvo;
044c7c41 465 } else /* The option is for other outputs */
e4b36699 466 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
467
468 return limit;
469}
470
1b894b59 471static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
472{
473 struct drm_device *dev = crtc->dev;
474 const intel_limit_t *limit;
475
bad720ff 476 if (HAS_PCH_SPLIT(dev))
1b894b59 477 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 478 else if (IS_G4X(dev)) {
044c7c41 479 limit = intel_g4x_limit(crtc);
f2b115e6 480 } else if (IS_PINEVIEW(dev)) {
2177832f 481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 482 limit = &intel_limits_pineview_lvds;
2177832f 483 else
f2b115e6 484 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
485 } else if (IS_CHERRYVIEW(dev)) {
486 limit = &intel_limits_chv;
a0c4da24 487 } else if (IS_VALLEYVIEW(dev)) {
dc730512 488 limit = &intel_limits_vlv;
a6c45cf0
CW
489 } else if (!IS_GEN2(dev)) {
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
491 limit = &intel_limits_i9xx_lvds;
492 else
493 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
494 } else {
495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 496 limit = &intel_limits_i8xx_lvds;
5d536e28 497 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 498 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
499 else
500 limit = &intel_limits_i8xx_dac;
79e53945
JB
501 }
502 return limit;
503}
504
f2b115e6
AJ
505/* m1 is reserved as 0 in Pineview, n is a ring counter */
506static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 507{
2177832f
SL
508 clock->m = clock->m2 + 2;
509 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
510 if (WARN_ON(clock->n == 0 || clock->p == 0))
511 return;
fb03ac01
VS
512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
514}
515
7429e9d4
DV
516static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
517{
518 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
519}
520
ac58c3f0 521static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 522{
7429e9d4 523 clock->m = i9xx_dpll_compute_m(clock);
79e53945 524 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
525 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
526 return;
fb03ac01
VS
527 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
528 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
529}
530
ef9348c8
CML
531static void chv_clock(int refclk, intel_clock_t *clock)
532{
533 clock->m = clock->m1 * clock->m2;
534 clock->p = clock->p1 * clock->p2;
535 if (WARN_ON(clock->n == 0 || clock->p == 0))
536 return;
537 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
538 clock->n << 22);
539 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
540}
541
7c04d1d9 542#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
543/**
544 * Returns whether the given set of divisors are valid for a given refclk with
545 * the given connectors.
546 */
547
1b894b59
CW
548static bool intel_PLL_is_valid(struct drm_device *dev,
549 const intel_limit_t *limit,
550 const intel_clock_t *clock)
79e53945 551{
f01b7962
VS
552 if (clock->n < limit->n.min || limit->n.max < clock->n)
553 INTELPllInvalid("n out of range\n");
79e53945 554 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 555 INTELPllInvalid("p1 out of range\n");
79e53945 556 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 557 INTELPllInvalid("m2 out of range\n");
79e53945 558 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 559 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
560
561 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
562 if (clock->m1 <= clock->m2)
563 INTELPllInvalid("m1 <= m2\n");
564
565 if (!IS_VALLEYVIEW(dev)) {
566 if (clock->p < limit->p.min || limit->p.max < clock->p)
567 INTELPllInvalid("p out of range\n");
568 if (clock->m < limit->m.min || limit->m.max < clock->m)
569 INTELPllInvalid("m out of range\n");
570 }
571
79e53945 572 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 573 INTELPllInvalid("vco out of range\n");
79e53945
JB
574 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
575 * connector, etc., rather than just a single range.
576 */
577 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 578 INTELPllInvalid("dot out of range\n");
79e53945
JB
579
580 return true;
581}
582
d4906093 583static bool
ee9300bb 584i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
585 int target, int refclk, intel_clock_t *match_clock,
586 intel_clock_t *best_clock)
79e53945
JB
587{
588 struct drm_device *dev = crtc->dev;
79e53945 589 intel_clock_t clock;
79e53945
JB
590 int err = target;
591
a210b028 592 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 593 /*
a210b028
DV
594 * For LVDS just rely on its current settings for dual-channel.
595 * We haven't figured out how to reliably set up different
596 * single/dual channel state, if we even can.
79e53945 597 */
1974cad0 598 if (intel_is_dual_link_lvds(dev))
79e53945
JB
599 clock.p2 = limit->p2.p2_fast;
600 else
601 clock.p2 = limit->p2.p2_slow;
602 } else {
603 if (target < limit->p2.dot_limit)
604 clock.p2 = limit->p2.p2_slow;
605 else
606 clock.p2 = limit->p2.p2_fast;
607 }
608
0206e353 609 memset(best_clock, 0, sizeof(*best_clock));
79e53945 610
42158660
ZY
611 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
612 clock.m1++) {
613 for (clock.m2 = limit->m2.min;
614 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 615 if (clock.m2 >= clock.m1)
42158660
ZY
616 break;
617 for (clock.n = limit->n.min;
618 clock.n <= limit->n.max; clock.n++) {
619 for (clock.p1 = limit->p1.min;
620 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
621 int this_err;
622
ac58c3f0
DV
623 i9xx_clock(refclk, &clock);
624 if (!intel_PLL_is_valid(dev, limit,
625 &clock))
626 continue;
627 if (match_clock &&
628 clock.p != match_clock->p)
629 continue;
630
631 this_err = abs(clock.dot - target);
632 if (this_err < err) {
633 *best_clock = clock;
634 err = this_err;
635 }
636 }
637 }
638 }
639 }
640
641 return (err != target);
642}
643
644static bool
ee9300bb
DV
645pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
646 int target, int refclk, intel_clock_t *match_clock,
647 intel_clock_t *best_clock)
79e53945
JB
648{
649 struct drm_device *dev = crtc->dev;
79e53945 650 intel_clock_t clock;
79e53945
JB
651 int err = target;
652
a210b028 653 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 654 /*
a210b028
DV
655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
79e53945 658 */
1974cad0 659 if (intel_is_dual_link_lvds(dev))
79e53945
JB
660 clock.p2 = limit->p2.p2_fast;
661 else
662 clock.p2 = limit->p2.p2_slow;
663 } else {
664 if (target < limit->p2.dot_limit)
665 clock.p2 = limit->p2.p2_slow;
666 else
667 clock.p2 = limit->p2.p2_fast;
668 }
669
0206e353 670 memset(best_clock, 0, sizeof(*best_clock));
79e53945 671
42158660
ZY
672 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
673 clock.m1++) {
674 for (clock.m2 = limit->m2.min;
675 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
676 for (clock.n = limit->n.min;
677 clock.n <= limit->n.max; clock.n++) {
678 for (clock.p1 = limit->p1.min;
679 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
680 int this_err;
681
ac58c3f0 682 pineview_clock(refclk, &clock);
1b894b59
CW
683 if (!intel_PLL_is_valid(dev, limit,
684 &clock))
79e53945 685 continue;
cec2f356
SP
686 if (match_clock &&
687 clock.p != match_clock->p)
688 continue;
79e53945
JB
689
690 this_err = abs(clock.dot - target);
691 if (this_err < err) {
692 *best_clock = clock;
693 err = this_err;
694 }
695 }
696 }
697 }
698 }
699
700 return (err != target);
701}
702
d4906093 703static bool
ee9300bb
DV
704g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
d4906093
ML
707{
708 struct drm_device *dev = crtc->dev;
d4906093
ML
709 intel_clock_t clock;
710 int max_n;
711 bool found;
6ba770dc
AJ
712 /* approximately equals target * 0.00585 */
713 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
714 found = false;
715
716 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 717 if (intel_is_dual_link_lvds(dev))
d4906093
ML
718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
728 memset(best_clock, 0, sizeof(*best_clock));
729 max_n = limit->n.max;
f77f13e2 730 /* based on hardware requirement, prefer smaller n to precision */
d4906093 731 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 732 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
733 for (clock.m1 = limit->m1.max;
734 clock.m1 >= limit->m1.min; clock.m1--) {
735 for (clock.m2 = limit->m2.max;
736 clock.m2 >= limit->m2.min; clock.m2--) {
737 for (clock.p1 = limit->p1.max;
738 clock.p1 >= limit->p1.min; clock.p1--) {
739 int this_err;
740
ac58c3f0 741 i9xx_clock(refclk, &clock);
1b894b59
CW
742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
d4906093 744 continue;
1b894b59
CW
745
746 this_err = abs(clock.dot - target);
d4906093
ML
747 if (this_err < err_most) {
748 *best_clock = clock;
749 err_most = this_err;
750 max_n = clock.n;
751 found = true;
752 }
753 }
754 }
755 }
756 }
2c07245f
ZW
757 return found;
758}
759
a0c4da24 760static bool
ee9300bb
DV
761vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
762 int target, int refclk, intel_clock_t *match_clock,
763 intel_clock_t *best_clock)
a0c4da24 764{
f01b7962 765 struct drm_device *dev = crtc->dev;
6b4bf1c4 766 intel_clock_t clock;
69e4f900 767 unsigned int bestppm = 1000000;
27e639bf
VS
768 /* min update 19.2 MHz */
769 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 770 bool found = false;
a0c4da24 771
6b4bf1c4
VS
772 target *= 5; /* fast clock */
773
774 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
775
776 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 777 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 778 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 779 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 780 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 781 clock.p = clock.p1 * clock.p2;
a0c4da24 782 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 783 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
784 unsigned int ppm, diff;
785
6b4bf1c4
VS
786 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
787 refclk * clock.m1);
788
789 vlv_clock(refclk, &clock);
43b0ac53 790
f01b7962
VS
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
43b0ac53
VS
793 continue;
794
6b4bf1c4
VS
795 diff = abs(clock.dot - target);
796 ppm = div_u64(1000000ULL * diff, target);
797
798 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 799 bestppm = 0;
6b4bf1c4 800 *best_clock = clock;
49e497ef 801 found = true;
43b0ac53 802 }
6b4bf1c4 803
c686122c 804 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 805 bestppm = ppm;
6b4bf1c4 806 *best_clock = clock;
49e497ef 807 found = true;
a0c4da24
JB
808 }
809 }
810 }
811 }
812 }
a0c4da24 813
49e497ef 814 return found;
a0c4da24 815}
a4fc5ed6 816
ef9348c8
CML
817static bool
818chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
819 int target, int refclk, intel_clock_t *match_clock,
820 intel_clock_t *best_clock)
821{
822 struct drm_device *dev = crtc->dev;
823 intel_clock_t clock;
824 uint64_t m2;
825 int found = false;
826
827 memset(best_clock, 0, sizeof(*best_clock));
828
829 /*
830 * Based on hardware doc, the n always set to 1, and m1 always
831 * set to 2. If requires to support 200Mhz refclk, we need to
832 * revisit this because n may not 1 anymore.
833 */
834 clock.n = 1, clock.m1 = 2;
835 target *= 5; /* fast clock */
836
837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
838 for (clock.p2 = limit->p2.p2_fast;
839 clock.p2 >= limit->p2.p2_slow;
840 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
841
842 clock.p = clock.p1 * clock.p2;
843
844 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
845 clock.n) << 22, refclk * clock.m1);
846
847 if (m2 > INT_MAX/clock.m1)
848 continue;
849
850 clock.m2 = m2;
851
852 chv_clock(refclk, &clock);
853
854 if (!intel_PLL_is_valid(dev, limit, &clock))
855 continue;
856
857 /* based on hardware requirement, prefer bigger p
858 */
859 if (clock.p > best_clock->p) {
860 *best_clock = clock;
861 found = true;
862 }
863 }
864 }
865
866 return found;
867}
868
20ddf665
VS
869bool intel_crtc_active(struct drm_crtc *crtc)
870{
871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
872
873 /* Be paranoid as we can arrive here with only partial
874 * state retrieved from the hardware during setup.
875 *
241bfc38 876 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
877 * as Haswell has gained clock readout/fastboot support.
878 *
66e514c1 879 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
880 * properly reconstruct framebuffers.
881 */
f4510a27 882 return intel_crtc->active && crtc->primary->fb &&
241bfc38 883 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
884}
885
a5c961d1
PZ
886enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
887 enum pipe pipe)
888{
889 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
891
3b117c8f 892 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
893}
894
57e22f4a 895static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 898 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
899
900 frame = I915_READ(frame_reg);
901
902 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
31e4b89a
DL
903 WARN(1, "vblank wait on pipe %c timed out\n",
904 pipe_name(pipe));
a928d536
PZ
905}
906
9d0498a2
JB
907/**
908 * intel_wait_for_vblank - wait for vblank on a given pipe
909 * @dev: drm device
910 * @pipe: pipe to wait for
911 *
912 * Wait for vblank to occur on a given pipe. Needed for various bits of
913 * mode setting code.
914 */
915void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 916{
9d0498a2 917 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 918 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 919
57e22f4a
VS
920 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
921 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
922 return;
923 }
924
300387c0
CW
925 /* Clear existing vblank status. Note this will clear any other
926 * sticky status fields as well.
927 *
928 * This races with i915_driver_irq_handler() with the result
929 * that either function could miss a vblank event. Here it is not
930 * fatal, as we will either wait upon the next vblank interrupt or
931 * timeout. Generally speaking intel_wait_for_vblank() is only
932 * called during modeset at which time the GPU should be idle and
933 * should *not* be performing page flips and thus not waiting on
934 * vblanks...
935 * Currently, the result of us stealing a vblank from the irq
936 * handler is that a single frame will be skipped during swapbuffers.
937 */
938 I915_WRITE(pipestat_reg,
939 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
940
9d0498a2 941 /* Wait for vblank interrupt bit to set */
481b6af3
CW
942 if (wait_for(I915_READ(pipestat_reg) &
943 PIPE_VBLANK_INTERRUPT_STATUS,
944 50))
31e4b89a
DL
945 DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
946 pipe_name(pipe));
9d0498a2
JB
947}
948
fbf49ea2
VS
949static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
950{
951 struct drm_i915_private *dev_priv = dev->dev_private;
952 u32 reg = PIPEDSL(pipe);
953 u32 line1, line2;
954 u32 line_mask;
955
956 if (IS_GEN2(dev))
957 line_mask = DSL_LINEMASK_GEN2;
958 else
959 line_mask = DSL_LINEMASK_GEN3;
960
961 line1 = I915_READ(reg) & line_mask;
962 mdelay(5);
963 line2 = I915_READ(reg) & line_mask;
964
965 return line1 == line2;
966}
967
ab7ad7f6
KP
968/*
969 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 970 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
971 *
972 * After disabling a pipe, we can't wait for vblank in the usual way,
973 * spinning on the vblank interrupt status bit, since we won't actually
974 * see an interrupt when the pipe is disabled.
975 *
ab7ad7f6
KP
976 * On Gen4 and above:
977 * wait for the pipe register state bit to turn off
978 *
979 * Otherwise:
980 * wait for the display line value to settle (it usually
981 * ends up stopping at the start of the next frame).
58e10eb9 982 *
9d0498a2 983 */
575f7ab7 984static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 985{
575f7ab7 986 struct drm_device *dev = crtc->base.dev;
9d0498a2 987 struct drm_i915_private *dev_priv = dev->dev_private;
575f7ab7
VS
988 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
989 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
990
991 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 992 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
993
994 /* Wait for the Pipe State to go off */
58e10eb9
CW
995 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
996 100))
284637d9 997 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 998 } else {
ab7ad7f6 999 /* Wait for the display line to settle */
fbf49ea2 1000 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1001 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1002 }
79e53945
JB
1003}
1004
b0ea7d37
DL
1005/*
1006 * ibx_digital_port_connected - is the specified port connected?
1007 * @dev_priv: i915 private structure
1008 * @port: the port to test
1009 *
1010 * Returns true if @port is connected, false otherwise.
1011 */
1012bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1013 struct intel_digital_port *port)
1014{
1015 u32 bit;
1016
c36346e3 1017 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1018 switch (port->port) {
c36346e3
DL
1019 case PORT_B:
1020 bit = SDE_PORTB_HOTPLUG;
1021 break;
1022 case PORT_C:
1023 bit = SDE_PORTC_HOTPLUG;
1024 break;
1025 case PORT_D:
1026 bit = SDE_PORTD_HOTPLUG;
1027 break;
1028 default:
1029 return true;
1030 }
1031 } else {
eba905b2 1032 switch (port->port) {
c36346e3
DL
1033 case PORT_B:
1034 bit = SDE_PORTB_HOTPLUG_CPT;
1035 break;
1036 case PORT_C:
1037 bit = SDE_PORTC_HOTPLUG_CPT;
1038 break;
1039 case PORT_D:
1040 bit = SDE_PORTD_HOTPLUG_CPT;
1041 break;
1042 default:
1043 return true;
1044 }
b0ea7d37
DL
1045 }
1046
1047 return I915_READ(SDEISR) & bit;
1048}
1049
b24e7179
JB
1050static const char *state_string(bool enabled)
1051{
1052 return enabled ? "on" : "off";
1053}
1054
1055/* Only for pre-ILK configs */
55607e8a
DV
1056void assert_pll(struct drm_i915_private *dev_priv,
1057 enum pipe pipe, bool state)
b24e7179
JB
1058{
1059 int reg;
1060 u32 val;
1061 bool cur_state;
1062
1063 reg = DPLL(pipe);
1064 val = I915_READ(reg);
1065 cur_state = !!(val & DPLL_VCO_ENABLE);
1066 WARN(cur_state != state,
1067 "PLL state assertion failure (expected %s, current %s)\n",
1068 state_string(state), state_string(cur_state));
1069}
b24e7179 1070
23538ef1
JN
1071/* XXX: the dsi pll is shared between MIPI DSI ports */
1072static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1073{
1074 u32 val;
1075 bool cur_state;
1076
1077 mutex_lock(&dev_priv->dpio_lock);
1078 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1079 mutex_unlock(&dev_priv->dpio_lock);
1080
1081 cur_state = val & DSI_PLL_VCO_EN;
1082 WARN(cur_state != state,
1083 "DSI PLL state assertion failure (expected %s, current %s)\n",
1084 state_string(state), state_string(cur_state));
1085}
1086#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1087#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1088
55607e8a 1089struct intel_shared_dpll *
e2b78267
DV
1090intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1091{
1092 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1093
a43f6e0f 1094 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1095 return NULL;
1096
a43f6e0f 1097 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1098}
1099
040484af 1100/* For ILK+ */
55607e8a
DV
1101void assert_shared_dpll(struct drm_i915_private *dev_priv,
1102 struct intel_shared_dpll *pll,
1103 bool state)
040484af 1104{
040484af 1105 bool cur_state;
5358901f 1106 struct intel_dpll_hw_state hw_state;
040484af 1107
92b27b08 1108 if (WARN (!pll,
46edb027 1109 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1110 return;
ee7b9f93 1111
5358901f 1112 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1113 WARN(cur_state != state,
5358901f
DV
1114 "%s assertion failure (expected %s, current %s)\n",
1115 pll->name, state_string(state), state_string(cur_state));
040484af 1116}
040484af
JB
1117
1118static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
ad80a810
PZ
1124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1125 pipe);
040484af 1126
affa9354
PZ
1127 if (HAS_DDI(dev_priv->dev)) {
1128 /* DDI does not have a specific FDI_TX register */
ad80a810 1129 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1130 val = I915_READ(reg);
ad80a810 1131 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1132 } else {
1133 reg = FDI_TX_CTL(pipe);
1134 val = I915_READ(reg);
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
040484af
JB
1137 WARN(cur_state != state,
1138 "FDI TX state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
1147 int reg;
1148 u32 val;
1149 bool cur_state;
1150
d63fa0dc
PZ
1151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
1153 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1154 WARN(cur_state != state,
1155 "FDI RX state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
1164 int reg;
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
3d13ef2e 1168 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1169 return;
1170
bf507ef7 1171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1172 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1173 return;
1174
040484af
JB
1175 reg = FDI_TX_CTL(pipe);
1176 val = I915_READ(reg);
1177 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1178}
1179
55607e8a
DV
1180void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
040484af
JB
1182{
1183 int reg;
1184 u32 val;
55607e8a 1185 bool cur_state;
040484af
JB
1186
1187 reg = FDI_RX_CTL(pipe);
1188 val = I915_READ(reg);
55607e8a
DV
1189 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1190 WARN(cur_state != state,
1191 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1192 state_string(state), state_string(cur_state));
040484af
JB
1193}
1194
ea0760cf
JB
1195static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1196 enum pipe pipe)
1197{
bedd4dba
JN
1198 struct drm_device *dev = dev_priv->dev;
1199 int pp_reg;
ea0760cf
JB
1200 u32 val;
1201 enum pipe panel_pipe = PIPE_A;
0de3b485 1202 bool locked = true;
ea0760cf 1203
bedd4dba
JN
1204 if (WARN_ON(HAS_DDI(dev)))
1205 return;
1206
1207 if (HAS_PCH_SPLIT(dev)) {
1208 u32 port_sel;
1209
ea0760cf 1210 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1211 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1212
1213 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1214 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1215 panel_pipe = PIPE_B;
1216 /* XXX: else fix for eDP */
1217 } else if (IS_VALLEYVIEW(dev)) {
1218 /* presumably write lock depends on pipe, not port select */
1219 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1220 panel_pipe = pipe;
ea0760cf
JB
1221 } else {
1222 pp_reg = PP_CONTROL;
bedd4dba
JN
1223 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1224 panel_pipe = PIPE_B;
ea0760cf
JB
1225 }
1226
1227 val = I915_READ(pp_reg);
1228 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1229 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1230 locked = false;
1231
ea0760cf
JB
1232 WARN(panel_pipe == pipe && locked,
1233 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1234 pipe_name(pipe));
ea0760cf
JB
1235}
1236
93ce0ba6
JN
1237static void assert_cursor(struct drm_i915_private *dev_priv,
1238 enum pipe pipe, bool state)
1239{
1240 struct drm_device *dev = dev_priv->dev;
1241 bool cur_state;
1242
d9d82081 1243 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1244 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1245 else
5efb3e28 1246 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1247
1248 WARN(cur_state != state,
1249 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1250 pipe_name(pipe), state_string(state), state_string(cur_state));
1251}
1252#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1253#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1254
b840d907
JB
1255void assert_pipe(struct drm_i915_private *dev_priv,
1256 enum pipe pipe, bool state)
b24e7179
JB
1257{
1258 int reg;
1259 u32 val;
63d7bbe9 1260 bool cur_state;
702e7a56
PZ
1261 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1262 pipe);
b24e7179 1263
b6b5d049
VS
1264 /* if we need the pipe quirk it must be always on */
1265 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1266 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1267 state = true;
1268
da7e29bd 1269 if (!intel_display_power_enabled(dev_priv,
b97186f0 1270 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1271 cur_state = false;
1272 } else {
1273 reg = PIPECONF(cpu_transcoder);
1274 val = I915_READ(reg);
1275 cur_state = !!(val & PIPECONF_ENABLE);
1276 }
1277
63d7bbe9
JB
1278 WARN(cur_state != state,
1279 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1280 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1281}
1282
931872fc
CW
1283static void assert_plane(struct drm_i915_private *dev_priv,
1284 enum plane plane, bool state)
b24e7179
JB
1285{
1286 int reg;
1287 u32 val;
931872fc 1288 bool cur_state;
b24e7179
JB
1289
1290 reg = DSPCNTR(plane);
1291 val = I915_READ(reg);
931872fc
CW
1292 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1293 WARN(cur_state != state,
1294 "plane %c assertion failure (expected %s, current %s)\n",
1295 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1296}
1297
931872fc
CW
1298#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1299#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1300
b24e7179
JB
1301static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1302 enum pipe pipe)
1303{
653e1026 1304 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1305 int reg, i;
1306 u32 val;
1307 int cur_pipe;
1308
653e1026
VS
1309 /* Primary planes are fixed to pipes on gen4+ */
1310 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1311 reg = DSPCNTR(pipe);
1312 val = I915_READ(reg);
83f26f16 1313 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1314 "plane %c assertion failure, should be disabled but not\n",
1315 plane_name(pipe));
19ec1358 1316 return;
28c05794 1317 }
19ec1358 1318
b24e7179 1319 /* Need to check both planes against the pipe */
055e393f 1320 for_each_pipe(dev_priv, i) {
b24e7179
JB
1321 reg = DSPCNTR(i);
1322 val = I915_READ(reg);
1323 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1324 DISPPLANE_SEL_PIPE_SHIFT;
1325 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1326 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1327 plane_name(i), pipe_name(pipe));
b24e7179
JB
1328 }
1329}
1330
19332d7a
JB
1331static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1332 enum pipe pipe)
1333{
20674eef 1334 struct drm_device *dev = dev_priv->dev;
1fe47785 1335 int reg, sprite;
19332d7a
JB
1336 u32 val;
1337
20674eef 1338 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1339 for_each_sprite(pipe, sprite) {
1340 reg = SPCNTR(pipe, sprite);
20674eef 1341 val = I915_READ(reg);
83f26f16 1342 WARN(val & SP_ENABLE,
20674eef 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1344 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1345 }
1346 } else if (INTEL_INFO(dev)->gen >= 7) {
1347 reg = SPRCTL(pipe);
19332d7a 1348 val = I915_READ(reg);
83f26f16 1349 WARN(val & SPRITE_ENABLE,
06da8da2 1350 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1351 plane_name(pipe), pipe_name(pipe));
1352 } else if (INTEL_INFO(dev)->gen >= 5) {
1353 reg = DVSCNTR(pipe);
19332d7a 1354 val = I915_READ(reg);
83f26f16 1355 WARN(val & DVS_ENABLE,
06da8da2 1356 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1357 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1358 }
1359}
1360
89eff4be 1361static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1362{
1363 u32 val;
1364 bool enabled;
1365
89eff4be 1366 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1367
92f2584a
JB
1368 val = I915_READ(PCH_DREF_CONTROL);
1369 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1370 DREF_SUPERSPREAD_SOURCE_MASK));
1371 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1372}
1373
ab9412ba
DV
1374static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1375 enum pipe pipe)
92f2584a
JB
1376{
1377 int reg;
1378 u32 val;
1379 bool enabled;
1380
ab9412ba 1381 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1382 val = I915_READ(reg);
1383 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1384 WARN(enabled,
1385 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1386 pipe_name(pipe));
92f2584a
JB
1387}
1388
4e634389
KP
1389static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1390 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1391{
1392 if ((val & DP_PORT_EN) == 0)
1393 return false;
1394
1395 if (HAS_PCH_CPT(dev_priv->dev)) {
1396 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1397 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1398 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1399 return false;
44f37d1f
CML
1400 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1401 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1402 return false;
f0575e92
KP
1403 } else {
1404 if ((val & DP_PIPE_MASK) != (pipe << 30))
1405 return false;
1406 }
1407 return true;
1408}
1409
1519b995
KP
1410static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
dc0fa718 1413 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1414 return false;
1415
1416 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1417 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1418 return false;
44f37d1f
CML
1419 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1420 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1421 return false;
1519b995 1422 } else {
dc0fa718 1423 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1424 return false;
1425 }
1426 return true;
1427}
1428
1429static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1430 enum pipe pipe, u32 val)
1431{
1432 if ((val & LVDS_PORT_EN) == 0)
1433 return false;
1434
1435 if (HAS_PCH_CPT(dev_priv->dev)) {
1436 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1437 return false;
1438 } else {
1439 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1440 return false;
1441 }
1442 return true;
1443}
1444
1445static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1446 enum pipe pipe, u32 val)
1447{
1448 if ((val & ADPA_DAC_ENABLE) == 0)
1449 return false;
1450 if (HAS_PCH_CPT(dev_priv->dev)) {
1451 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1452 return false;
1453 } else {
1454 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1455 return false;
1456 }
1457 return true;
1458}
1459
291906f1 1460static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1461 enum pipe pipe, int reg, u32 port_sel)
291906f1 1462{
47a05eca 1463 u32 val = I915_READ(reg);
4e634389 1464 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1465 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1466 reg, pipe_name(pipe));
de9a35ab 1467
75c5da27
DV
1468 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1469 && (val & DP_PIPEB_SELECT),
de9a35ab 1470 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1471}
1472
1473static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1474 enum pipe pipe, int reg)
1475{
47a05eca 1476 u32 val = I915_READ(reg);
b70ad586 1477 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1478 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1479 reg, pipe_name(pipe));
de9a35ab 1480
dc0fa718 1481 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1482 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1483 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1484}
1485
1486static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1487 enum pipe pipe)
1488{
1489 int reg;
1490 u32 val;
291906f1 1491
f0575e92
KP
1492 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1493 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1494 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1495
1496 reg = PCH_ADPA;
1497 val = I915_READ(reg);
b70ad586 1498 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1499 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1500 pipe_name(pipe));
291906f1
JB
1501
1502 reg = PCH_LVDS;
1503 val = I915_READ(reg);
b70ad586 1504 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1505 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1506 pipe_name(pipe));
291906f1 1507
e2debe91
PZ
1508 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1509 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1510 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1511}
1512
40e9cf64
JB
1513static void intel_init_dpio(struct drm_device *dev)
1514{
1515 struct drm_i915_private *dev_priv = dev->dev_private;
1516
1517 if (!IS_VALLEYVIEW(dev))
1518 return;
1519
a09caddd
CML
1520 /*
1521 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1522 * CHV x1 PHY (DP/HDMI D)
1523 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1524 */
1525 if (IS_CHERRYVIEW(dev)) {
1526 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1527 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1528 } else {
1529 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1530 }
5382f5f3
JB
1531}
1532
426115cf 1533static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1534{
426115cf
DV
1535 struct drm_device *dev = crtc->base.dev;
1536 struct drm_i915_private *dev_priv = dev->dev_private;
1537 int reg = DPLL(crtc->pipe);
1538 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1539
426115cf 1540 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1541
1542 /* No really, not for ILK+ */
1543 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1544
1545 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1546 if (IS_MOBILE(dev_priv->dev))
426115cf 1547 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1548
426115cf
DV
1549 I915_WRITE(reg, dpll);
1550 POSTING_READ(reg);
1551 udelay(150);
1552
1553 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1554 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1555
1556 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1557 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1558
1559 /* We do this three times for luck */
426115cf 1560 I915_WRITE(reg, dpll);
87442f73
DV
1561 POSTING_READ(reg);
1562 udelay(150); /* wait for warmup */
426115cf 1563 I915_WRITE(reg, dpll);
87442f73
DV
1564 POSTING_READ(reg);
1565 udelay(150); /* wait for warmup */
426115cf 1566 I915_WRITE(reg, dpll);
87442f73
DV
1567 POSTING_READ(reg);
1568 udelay(150); /* wait for warmup */
1569}
1570
9d556c99
CML
1571static void chv_enable_pll(struct intel_crtc *crtc)
1572{
1573 struct drm_device *dev = crtc->base.dev;
1574 struct drm_i915_private *dev_priv = dev->dev_private;
1575 int pipe = crtc->pipe;
1576 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1577 u32 tmp;
1578
1579 assert_pipe_disabled(dev_priv, crtc->pipe);
1580
1581 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1582
1583 mutex_lock(&dev_priv->dpio_lock);
1584
1585 /* Enable back the 10bit clock to display controller */
1586 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1587 tmp |= DPIO_DCLKP_EN;
1588 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1589
1590 /*
1591 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1592 */
1593 udelay(1);
1594
1595 /* Enable PLL */
a11b0703 1596 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1597
1598 /* Check PLL is locked */
a11b0703 1599 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1600 DRM_ERROR("PLL %d failed to lock\n", pipe);
1601
a11b0703
VS
1602 /* not sure when this should be written */
1603 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1604 POSTING_READ(DPLL_MD(pipe));
1605
9d556c99
CML
1606 mutex_unlock(&dev_priv->dpio_lock);
1607}
1608
66e3d5c0 1609static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1610{
66e3d5c0
DV
1611 struct drm_device *dev = crtc->base.dev;
1612 struct drm_i915_private *dev_priv = dev->dev_private;
1613 int reg = DPLL(crtc->pipe);
1614 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1615
66e3d5c0 1616 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1617
63d7bbe9 1618 /* No really, not for ILK+ */
3d13ef2e 1619 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1620
1621 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1624
66e3d5c0
DV
1625 I915_WRITE(reg, dpll);
1626
1627 /* Wait for the clocks to stabilize. */
1628 POSTING_READ(reg);
1629 udelay(150);
1630
1631 if (INTEL_INFO(dev)->gen >= 4) {
1632 I915_WRITE(DPLL_MD(crtc->pipe),
1633 crtc->config.dpll_hw_state.dpll_md);
1634 } else {
1635 /* The pixel multiplier can only be updated once the
1636 * DPLL is enabled and the clocks are stable.
1637 *
1638 * So write it again.
1639 */
1640 I915_WRITE(reg, dpll);
1641 }
63d7bbe9
JB
1642
1643 /* We do this three times for luck */
66e3d5c0 1644 I915_WRITE(reg, dpll);
63d7bbe9
JB
1645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
66e3d5c0 1647 I915_WRITE(reg, dpll);
63d7bbe9
JB
1648 POSTING_READ(reg);
1649 udelay(150); /* wait for warmup */
66e3d5c0 1650 I915_WRITE(reg, dpll);
63d7bbe9
JB
1651 POSTING_READ(reg);
1652 udelay(150); /* wait for warmup */
1653}
1654
1655/**
50b44a44 1656 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1657 * @dev_priv: i915 private structure
1658 * @pipe: pipe PLL to disable
1659 *
1660 * Disable the PLL for @pipe, making sure the pipe is off first.
1661 *
1662 * Note! This is for pre-ILK only.
1663 */
50b44a44 1664static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1665{
b6b5d049
VS
1666 /* Don't disable pipe or pipe PLLs if needed */
1667 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1668 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1669 return;
1670
1671 /* Make sure the pipe isn't still relying on us */
1672 assert_pipe_disabled(dev_priv, pipe);
1673
50b44a44
DV
1674 I915_WRITE(DPLL(pipe), 0);
1675 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1676}
1677
f6071166
JB
1678static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1679{
1680 u32 val = 0;
1681
1682 /* Make sure the pipe isn't still relying on us */
1683 assert_pipe_disabled(dev_priv, pipe);
1684
e5cbfbfb
ID
1685 /*
1686 * Leave integrated clock source and reference clock enabled for pipe B.
1687 * The latter is needed for VGA hotplug / manual detection.
1688 */
f6071166 1689 if (pipe == PIPE_B)
e5cbfbfb 1690 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1691 I915_WRITE(DPLL(pipe), val);
1692 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1693
1694}
1695
1696static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1697{
d752048d 1698 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1699 u32 val;
1700
a11b0703
VS
1701 /* Make sure the pipe isn't still relying on us */
1702 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1703
a11b0703 1704 /* Set PLL en = 0 */
d17ec4ce 1705 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1706 if (pipe != PIPE_A)
1707 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1708 I915_WRITE(DPLL(pipe), val);
1709 POSTING_READ(DPLL(pipe));
d752048d
VS
1710
1711 mutex_lock(&dev_priv->dpio_lock);
1712
1713 /* Disable 10bit clock to display controller */
1714 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1715 val &= ~DPIO_DCLKP_EN;
1716 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1717
61407f6d
VS
1718 /* disable left/right clock distribution */
1719 if (pipe != PIPE_B) {
1720 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1721 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1722 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1723 } else {
1724 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1725 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1726 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1727 }
1728
d752048d 1729 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1730}
1731
e4607fcf
CML
1732void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1733 struct intel_digital_port *dport)
89b667f8
JB
1734{
1735 u32 port_mask;
00fc31b7 1736 int dpll_reg;
89b667f8 1737
e4607fcf
CML
1738 switch (dport->port) {
1739 case PORT_B:
89b667f8 1740 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1741 dpll_reg = DPLL(0);
e4607fcf
CML
1742 break;
1743 case PORT_C:
89b667f8 1744 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1745 dpll_reg = DPLL(0);
1746 break;
1747 case PORT_D:
1748 port_mask = DPLL_PORTD_READY_MASK;
1749 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1750 break;
1751 default:
1752 BUG();
1753 }
89b667f8 1754
00fc31b7 1755 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1756 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1757 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1758}
1759
b14b1055
DV
1760static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1761{
1762 struct drm_device *dev = crtc->base.dev;
1763 struct drm_i915_private *dev_priv = dev->dev_private;
1764 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1765
be19f0ff
CW
1766 if (WARN_ON(pll == NULL))
1767 return;
1768
b14b1055
DV
1769 WARN_ON(!pll->refcount);
1770 if (pll->active == 0) {
1771 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1772 WARN_ON(pll->on);
1773 assert_shared_dpll_disabled(dev_priv, pll);
1774
1775 pll->mode_set(dev_priv, pll);
1776 }
1777}
1778
92f2584a 1779/**
85b3894f 1780 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1781 * @dev_priv: i915 private structure
1782 * @pipe: pipe PLL to enable
1783 *
1784 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1785 * drives the transcoder clock.
1786 */
85b3894f 1787static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1788{
3d13ef2e
DL
1789 struct drm_device *dev = crtc->base.dev;
1790 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1791 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1792
87a875bb 1793 if (WARN_ON(pll == NULL))
48da64a8
CW
1794 return;
1795
1796 if (WARN_ON(pll->refcount == 0))
1797 return;
ee7b9f93 1798
74dd6928 1799 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1800 pll->name, pll->active, pll->on,
e2b78267 1801 crtc->base.base.id);
92f2584a 1802
cdbd2316
DV
1803 if (pll->active++) {
1804 WARN_ON(!pll->on);
e9d6944e 1805 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1806 return;
1807 }
f4a091c7 1808 WARN_ON(pll->on);
ee7b9f93 1809
bd2bb1b9
PZ
1810 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1811
46edb027 1812 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1813 pll->enable(dev_priv, pll);
ee7b9f93 1814 pll->on = true;
92f2584a
JB
1815}
1816
f6daaec2 1817static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1818{
3d13ef2e
DL
1819 struct drm_device *dev = crtc->base.dev;
1820 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1821 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1822
92f2584a 1823 /* PCH only available on ILK+ */
3d13ef2e 1824 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1825 if (WARN_ON(pll == NULL))
ee7b9f93 1826 return;
92f2584a 1827
48da64a8
CW
1828 if (WARN_ON(pll->refcount == 0))
1829 return;
7a419866 1830
46edb027
DV
1831 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1832 pll->name, pll->active, pll->on,
e2b78267 1833 crtc->base.base.id);
7a419866 1834
48da64a8 1835 if (WARN_ON(pll->active == 0)) {
e9d6944e 1836 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1837 return;
1838 }
1839
e9d6944e 1840 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1841 WARN_ON(!pll->on);
cdbd2316 1842 if (--pll->active)
7a419866 1843 return;
ee7b9f93 1844
46edb027 1845 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1846 pll->disable(dev_priv, pll);
ee7b9f93 1847 pll->on = false;
bd2bb1b9
PZ
1848
1849 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1850}
1851
b8a4f404
PZ
1852static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1853 enum pipe pipe)
040484af 1854{
23670b32 1855 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1856 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1858 uint32_t reg, val, pipeconf_val;
040484af
JB
1859
1860 /* PCH only available on ILK+ */
55522f37 1861 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1862
1863 /* Make sure PCH DPLL is enabled */
e72f9fbf 1864 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1865 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1866
1867 /* FDI must be feeding us bits for PCH ports */
1868 assert_fdi_tx_enabled(dev_priv, pipe);
1869 assert_fdi_rx_enabled(dev_priv, pipe);
1870
23670b32
DV
1871 if (HAS_PCH_CPT(dev)) {
1872 /* Workaround: Set the timing override bit before enabling the
1873 * pch transcoder. */
1874 reg = TRANS_CHICKEN2(pipe);
1875 val = I915_READ(reg);
1876 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1877 I915_WRITE(reg, val);
59c859d6 1878 }
23670b32 1879
ab9412ba 1880 reg = PCH_TRANSCONF(pipe);
040484af 1881 val = I915_READ(reg);
5f7f726d 1882 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1883
1884 if (HAS_PCH_IBX(dev_priv->dev)) {
1885 /*
1886 * make the BPC in transcoder be consistent with
1887 * that in pipeconf reg.
1888 */
dfd07d72
DV
1889 val &= ~PIPECONF_BPC_MASK;
1890 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1891 }
5f7f726d
PZ
1892
1893 val &= ~TRANS_INTERLACE_MASK;
1894 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1895 if (HAS_PCH_IBX(dev_priv->dev) &&
1896 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1897 val |= TRANS_LEGACY_INTERLACED_ILK;
1898 else
1899 val |= TRANS_INTERLACED;
5f7f726d
PZ
1900 else
1901 val |= TRANS_PROGRESSIVE;
1902
040484af
JB
1903 I915_WRITE(reg, val | TRANS_ENABLE);
1904 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1905 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1906}
1907
8fb033d7 1908static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1909 enum transcoder cpu_transcoder)
040484af 1910{
8fb033d7 1911 u32 val, pipeconf_val;
8fb033d7
PZ
1912
1913 /* PCH only available on ILK+ */
55522f37 1914 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1915
8fb033d7 1916 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1917 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1918 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1919
223a6fdf
PZ
1920 /* Workaround: set timing override bit. */
1921 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1922 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1923 I915_WRITE(_TRANSA_CHICKEN2, val);
1924
25f3ef11 1925 val = TRANS_ENABLE;
937bb610 1926 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1927
9a76b1c6
PZ
1928 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1929 PIPECONF_INTERLACED_ILK)
a35f2679 1930 val |= TRANS_INTERLACED;
8fb033d7
PZ
1931 else
1932 val |= TRANS_PROGRESSIVE;
1933
ab9412ba
DV
1934 I915_WRITE(LPT_TRANSCONF, val);
1935 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1936 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1937}
1938
b8a4f404
PZ
1939static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1940 enum pipe pipe)
040484af 1941{
23670b32
DV
1942 struct drm_device *dev = dev_priv->dev;
1943 uint32_t reg, val;
040484af
JB
1944
1945 /* FDI relies on the transcoder */
1946 assert_fdi_tx_disabled(dev_priv, pipe);
1947 assert_fdi_rx_disabled(dev_priv, pipe);
1948
291906f1
JB
1949 /* Ports must be off as well */
1950 assert_pch_ports_disabled(dev_priv, pipe);
1951
ab9412ba 1952 reg = PCH_TRANSCONF(pipe);
040484af
JB
1953 val = I915_READ(reg);
1954 val &= ~TRANS_ENABLE;
1955 I915_WRITE(reg, val);
1956 /* wait for PCH transcoder off, transcoder state */
1957 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1958 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1959
1960 if (!HAS_PCH_IBX(dev)) {
1961 /* Workaround: Clear the timing override chicken bit again. */
1962 reg = TRANS_CHICKEN2(pipe);
1963 val = I915_READ(reg);
1964 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1965 I915_WRITE(reg, val);
1966 }
040484af
JB
1967}
1968
ab4d966c 1969static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1970{
8fb033d7
PZ
1971 u32 val;
1972
ab9412ba 1973 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1974 val &= ~TRANS_ENABLE;
ab9412ba 1975 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1976 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1977 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1978 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1979
1980 /* Workaround: clear timing override bit. */
1981 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1982 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1983 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1984}
1985
b24e7179 1986/**
309cfea8 1987 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1988 * @crtc: crtc responsible for the pipe
b24e7179 1989 *
0372264a 1990 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1991 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1992 */
e1fdc473 1993static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1994{
0372264a
PZ
1995 struct drm_device *dev = crtc->base.dev;
1996 struct drm_i915_private *dev_priv = dev->dev_private;
1997 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1998 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1999 pipe);
1a240d4d 2000 enum pipe pch_transcoder;
b24e7179
JB
2001 int reg;
2002 u32 val;
2003
58c6eaa2 2004 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2005 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2006 assert_sprites_disabled(dev_priv, pipe);
2007
681e5811 2008 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2009 pch_transcoder = TRANSCODER_A;
2010 else
2011 pch_transcoder = pipe;
2012
b24e7179
JB
2013 /*
2014 * A pipe without a PLL won't actually be able to drive bits from
2015 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2016 * need the check.
2017 */
2018 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2019 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2020 assert_dsi_pll_enabled(dev_priv);
2021 else
2022 assert_pll_enabled(dev_priv, pipe);
040484af 2023 else {
30421c4f 2024 if (crtc->config.has_pch_encoder) {
040484af 2025 /* if driving the PCH, we need FDI enabled */
cc391bbb 2026 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2027 assert_fdi_tx_pll_enabled(dev_priv,
2028 (enum pipe) cpu_transcoder);
040484af
JB
2029 }
2030 /* FIXME: assert CPU port conditions for SNB+ */
2031 }
b24e7179 2032
702e7a56 2033 reg = PIPECONF(cpu_transcoder);
b24e7179 2034 val = I915_READ(reg);
7ad25d48 2035 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2036 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2037 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2038 return;
7ad25d48 2039 }
00d70b15
CW
2040
2041 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2042 POSTING_READ(reg);
b24e7179
JB
2043}
2044
2045/**
309cfea8 2046 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2047 * @crtc: crtc whose pipes is to be disabled
b24e7179 2048 *
575f7ab7
VS
2049 * Disable the pipe of @crtc, making sure that various hardware
2050 * specific requirements are met, if applicable, e.g. plane
2051 * disabled, panel fitter off, etc.
b24e7179
JB
2052 *
2053 * Will wait until the pipe has shut down before returning.
2054 */
575f7ab7 2055static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2056{
575f7ab7
VS
2057 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2058 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2059 enum pipe pipe = crtc->pipe;
b24e7179
JB
2060 int reg;
2061 u32 val;
2062
2063 /*
2064 * Make sure planes won't keep trying to pump pixels to us,
2065 * or we might hang the display.
2066 */
2067 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2068 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2069 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2070
702e7a56 2071 reg = PIPECONF(cpu_transcoder);
b24e7179 2072 val = I915_READ(reg);
00d70b15
CW
2073 if ((val & PIPECONF_ENABLE) == 0)
2074 return;
2075
67adc644
VS
2076 /*
2077 * Double wide has implications for planes
2078 * so best keep it disabled when not needed.
2079 */
2080 if (crtc->config.double_wide)
2081 val &= ~PIPECONF_DOUBLE_WIDE;
2082
2083 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2084 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2085 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2086 val &= ~PIPECONF_ENABLE;
2087
2088 I915_WRITE(reg, val);
2089 if ((val & PIPECONF_ENABLE) == 0)
2090 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2091}
2092
d74362c9
KP
2093/*
2094 * Plane regs are double buffered, going from enabled->disabled needs a
2095 * trigger in order to latch. The display address reg provides this.
2096 */
1dba99f4
VS
2097void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2098 enum plane plane)
d74362c9 2099{
3d13ef2e
DL
2100 struct drm_device *dev = dev_priv->dev;
2101 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2102
2103 I915_WRITE(reg, I915_READ(reg));
2104 POSTING_READ(reg);
d74362c9
KP
2105}
2106
b24e7179 2107/**
262ca2b0 2108 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2109 * @plane: plane to be enabled
2110 * @crtc: crtc for the plane
b24e7179 2111 *
fdd508a6 2112 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2113 */
fdd508a6
VS
2114static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2115 struct drm_crtc *crtc)
b24e7179 2116{
fdd508a6
VS
2117 struct drm_device *dev = plane->dev;
2118 struct drm_i915_private *dev_priv = dev->dev_private;
2119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2120
2121 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2122 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2123
98ec7739
VS
2124 if (intel_crtc->primary_enabled)
2125 return;
0037f71c 2126
4c445e0e 2127 intel_crtc->primary_enabled = true;
939c2fe8 2128
fdd508a6
VS
2129 dev_priv->display.update_primary_plane(crtc, plane->fb,
2130 crtc->x, crtc->y);
33c3b0d1
VS
2131
2132 /*
2133 * BDW signals flip done immediately if the plane
2134 * is disabled, even if the plane enable is already
2135 * armed to occur at the next vblank :(
2136 */
2137 if (IS_BROADWELL(dev))
2138 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2139}
2140
b24e7179 2141/**
262ca2b0 2142 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2143 * @plane: plane to be disabled
2144 * @crtc: crtc for the plane
b24e7179 2145 *
fdd508a6 2146 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2147 */
fdd508a6
VS
2148static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2149 struct drm_crtc *crtc)
b24e7179 2150{
fdd508a6
VS
2151 struct drm_device *dev = plane->dev;
2152 struct drm_i915_private *dev_priv = dev->dev_private;
2153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2154
2155 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2156
98ec7739
VS
2157 if (!intel_crtc->primary_enabled)
2158 return;
0037f71c 2159
4c445e0e 2160 intel_crtc->primary_enabled = false;
939c2fe8 2161
fdd508a6
VS
2162 dev_priv->display.update_primary_plane(crtc, plane->fb,
2163 crtc->x, crtc->y);
b24e7179
JB
2164}
2165
693db184
CW
2166static bool need_vtd_wa(struct drm_device *dev)
2167{
2168#ifdef CONFIG_INTEL_IOMMU
2169 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2170 return true;
2171#endif
2172 return false;
2173}
2174
a57ce0b2
JB
2175static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2176{
2177 int tile_height;
2178
2179 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2180 return ALIGN(height, tile_height);
2181}
2182
127bd2ac 2183int
48b956c5 2184intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2185 struct drm_i915_gem_object *obj,
a4872ba6 2186 struct intel_engine_cs *pipelined)
6b95a207 2187{
ce453d81 2188 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2189 u32 alignment;
2190 int ret;
2191
ebcdd39e
MR
2192 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2193
05394f39 2194 switch (obj->tiling_mode) {
6b95a207 2195 case I915_TILING_NONE:
534843da
CW
2196 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2197 alignment = 128 * 1024;
a6c45cf0 2198 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2199 alignment = 4 * 1024;
2200 else
2201 alignment = 64 * 1024;
6b95a207
KH
2202 break;
2203 case I915_TILING_X:
2204 /* pin() will align the object as required by fence */
2205 alignment = 0;
2206 break;
2207 case I915_TILING_Y:
80075d49 2208 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2209 return -EINVAL;
2210 default:
2211 BUG();
2212 }
2213
693db184
CW
2214 /* Note that the w/a also requires 64 PTE of padding following the
2215 * bo. We currently fill all unused PTE with the shadow page and so
2216 * we should always have valid PTE following the scanout preventing
2217 * the VT-d warning.
2218 */
2219 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2220 alignment = 256 * 1024;
2221
ce453d81 2222 dev_priv->mm.interruptible = false;
2da3b9b9 2223 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2224 if (ret)
ce453d81 2225 goto err_interruptible;
6b95a207
KH
2226
2227 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2228 * fence, whereas 965+ only requires a fence if using
2229 * framebuffer compression. For simplicity, we always install
2230 * a fence as the cost is not that onerous.
2231 */
06d98131 2232 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2233 if (ret)
2234 goto err_unpin;
1690e1eb 2235
9a5a53b3 2236 i915_gem_object_pin_fence(obj);
6b95a207 2237
ce453d81 2238 dev_priv->mm.interruptible = true;
6b95a207 2239 return 0;
48b956c5
CW
2240
2241err_unpin:
cc98b413 2242 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2243err_interruptible:
2244 dev_priv->mm.interruptible = true;
48b956c5 2245 return ret;
6b95a207
KH
2246}
2247
1690e1eb
CW
2248void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2249{
ebcdd39e
MR
2250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
1690e1eb 2252 i915_gem_object_unpin_fence(obj);
cc98b413 2253 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2254}
2255
c2c75131
DV
2256/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2257 * is assumed to be a power-of-two. */
bc752862
CW
2258unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2259 unsigned int tiling_mode,
2260 unsigned int cpp,
2261 unsigned int pitch)
c2c75131 2262{
bc752862
CW
2263 if (tiling_mode != I915_TILING_NONE) {
2264 unsigned int tile_rows, tiles;
c2c75131 2265
bc752862
CW
2266 tile_rows = *y / 8;
2267 *y %= 8;
c2c75131 2268
bc752862
CW
2269 tiles = *x / (512/cpp);
2270 *x %= 512/cpp;
2271
2272 return tile_rows * pitch * 8 + tiles * 4096;
2273 } else {
2274 unsigned int offset;
2275
2276 offset = *y * pitch + *x * cpp;
2277 *y = 0;
2278 *x = (offset & 4095) / cpp;
2279 return offset & -4096;
2280 }
c2c75131
DV
2281}
2282
46f297fb
JB
2283int intel_format_to_fourcc(int format)
2284{
2285 switch (format) {
2286 case DISPPLANE_8BPP:
2287 return DRM_FORMAT_C8;
2288 case DISPPLANE_BGRX555:
2289 return DRM_FORMAT_XRGB1555;
2290 case DISPPLANE_BGRX565:
2291 return DRM_FORMAT_RGB565;
2292 default:
2293 case DISPPLANE_BGRX888:
2294 return DRM_FORMAT_XRGB8888;
2295 case DISPPLANE_RGBX888:
2296 return DRM_FORMAT_XBGR8888;
2297 case DISPPLANE_BGRX101010:
2298 return DRM_FORMAT_XRGB2101010;
2299 case DISPPLANE_RGBX101010:
2300 return DRM_FORMAT_XBGR2101010;
2301 }
2302}
2303
484b41dd 2304static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2305 struct intel_plane_config *plane_config)
2306{
2307 struct drm_device *dev = crtc->base.dev;
2308 struct drm_i915_gem_object *obj = NULL;
2309 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2310 u32 base = plane_config->base;
2311
ff2652ea
CW
2312 if (plane_config->size == 0)
2313 return false;
2314
46f297fb
JB
2315 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2316 plane_config->size);
2317 if (!obj)
484b41dd 2318 return false;
46f297fb
JB
2319
2320 if (plane_config->tiled) {
2321 obj->tiling_mode = I915_TILING_X;
66e514c1 2322 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2323 }
2324
66e514c1
DA
2325 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2326 mode_cmd.width = crtc->base.primary->fb->width;
2327 mode_cmd.height = crtc->base.primary->fb->height;
2328 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2329
2330 mutex_lock(&dev->struct_mutex);
2331
66e514c1 2332 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2333 &mode_cmd, obj)) {
46f297fb
JB
2334 DRM_DEBUG_KMS("intel fb init failed\n");
2335 goto out_unref_obj;
2336 }
2337
a071fa00 2338 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2339 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2340
2341 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2342 return true;
46f297fb
JB
2343
2344out_unref_obj:
2345 drm_gem_object_unreference(&obj->base);
2346 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2347 return false;
2348}
2349
2350static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2351 struct intel_plane_config *plane_config)
2352{
2353 struct drm_device *dev = intel_crtc->base.dev;
2354 struct drm_crtc *c;
2355 struct intel_crtc *i;
2ff8fde1 2356 struct drm_i915_gem_object *obj;
484b41dd 2357
66e514c1 2358 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2359 return;
2360
2361 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2362 return;
2363
66e514c1
DA
2364 kfree(intel_crtc->base.primary->fb);
2365 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2366
2367 /*
2368 * Failed to alloc the obj, check to see if we should share
2369 * an fb with another CRTC instead
2370 */
70e1e0ec 2371 for_each_crtc(dev, c) {
484b41dd
JB
2372 i = to_intel_crtc(c);
2373
2374 if (c == &intel_crtc->base)
2375 continue;
2376
2ff8fde1
MR
2377 if (!i->active)
2378 continue;
2379
2380 obj = intel_fb_obj(c->primary->fb);
2381 if (obj == NULL)
484b41dd
JB
2382 continue;
2383
2ff8fde1 2384 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2385 drm_framebuffer_reference(c->primary->fb);
2386 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2387 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2388 break;
2389 }
2390 }
46f297fb
JB
2391}
2392
29b9bde6
DV
2393static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2394 struct drm_framebuffer *fb,
2395 int x, int y)
81255565
JB
2396{
2397 struct drm_device *dev = crtc->dev;
2398 struct drm_i915_private *dev_priv = dev->dev_private;
2399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2400 struct drm_i915_gem_object *obj;
81255565 2401 int plane = intel_crtc->plane;
e506a0c6 2402 unsigned long linear_offset;
81255565 2403 u32 dspcntr;
f45651ba 2404 u32 reg = DSPCNTR(plane);
48404c1e
SJ
2405 int pixel_size;
2406
fdd508a6
VS
2407 if (!intel_crtc->primary_enabled) {
2408 I915_WRITE(reg, 0);
2409 if (INTEL_INFO(dev)->gen >= 4)
2410 I915_WRITE(DSPSURF(plane), 0);
2411 else
2412 I915_WRITE(DSPADDR(plane), 0);
2413 POSTING_READ(reg);
2414 return;
2415 }
2416
c9ba6fad
VS
2417 obj = intel_fb_obj(fb);
2418 if (WARN_ON(obj == NULL))
2419 return;
2420
2421 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2422
f45651ba
VS
2423 dspcntr = DISPPLANE_GAMMA_ENABLE;
2424
fdd508a6 2425 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2426
2427 if (INTEL_INFO(dev)->gen < 4) {
2428 if (intel_crtc->pipe == PIPE_B)
2429 dspcntr |= DISPPLANE_SEL_PIPE_B;
2430
2431 /* pipesrc and dspsize control the size that is scaled from,
2432 * which should always be the user's requested size.
2433 */
2434 I915_WRITE(DSPSIZE(plane),
2435 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2436 (intel_crtc->config.pipe_src_w - 1));
2437 I915_WRITE(DSPPOS(plane), 0);
2438 }
81255565 2439
57779d06
VS
2440 switch (fb->pixel_format) {
2441 case DRM_FORMAT_C8:
81255565
JB
2442 dspcntr |= DISPPLANE_8BPP;
2443 break;
57779d06
VS
2444 case DRM_FORMAT_XRGB1555:
2445 case DRM_FORMAT_ARGB1555:
2446 dspcntr |= DISPPLANE_BGRX555;
81255565 2447 break;
57779d06
VS
2448 case DRM_FORMAT_RGB565:
2449 dspcntr |= DISPPLANE_BGRX565;
2450 break;
2451 case DRM_FORMAT_XRGB8888:
2452 case DRM_FORMAT_ARGB8888:
2453 dspcntr |= DISPPLANE_BGRX888;
2454 break;
2455 case DRM_FORMAT_XBGR8888:
2456 case DRM_FORMAT_ABGR8888:
2457 dspcntr |= DISPPLANE_RGBX888;
2458 break;
2459 case DRM_FORMAT_XRGB2101010:
2460 case DRM_FORMAT_ARGB2101010:
2461 dspcntr |= DISPPLANE_BGRX101010;
2462 break;
2463 case DRM_FORMAT_XBGR2101010:
2464 case DRM_FORMAT_ABGR2101010:
2465 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2466 break;
2467 default:
baba133a 2468 BUG();
81255565 2469 }
57779d06 2470
f45651ba
VS
2471 if (INTEL_INFO(dev)->gen >= 4 &&
2472 obj->tiling_mode != I915_TILING_NONE)
2473 dspcntr |= DISPPLANE_TILED;
81255565 2474
de1aa629
VS
2475 if (IS_G4X(dev))
2476 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2477
b9897127 2478 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2479
c2c75131
DV
2480 if (INTEL_INFO(dev)->gen >= 4) {
2481 intel_crtc->dspaddr_offset =
bc752862 2482 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2483 pixel_size,
bc752862 2484 fb->pitches[0]);
c2c75131
DV
2485 linear_offset -= intel_crtc->dspaddr_offset;
2486 } else {
e506a0c6 2487 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2488 }
e506a0c6 2489
48404c1e
SJ
2490 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2491 dspcntr |= DISPPLANE_ROTATE_180;
2492
2493 x += (intel_crtc->config.pipe_src_w - 1);
2494 y += (intel_crtc->config.pipe_src_h - 1);
2495
2496 /* Finding the last pixel of the last line of the display
2497 data and adding to linear_offset*/
2498 linear_offset +=
2499 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2500 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2501 }
2502
2503 I915_WRITE(reg, dspcntr);
2504
f343c5f6
BW
2505 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2506 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2507 fb->pitches[0]);
01f2c773 2508 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2509 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2510 I915_WRITE(DSPSURF(plane),
2511 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2512 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2513 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2514 } else
f343c5f6 2515 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2516 POSTING_READ(reg);
17638cd6
JB
2517}
2518
29b9bde6
DV
2519static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2520 struct drm_framebuffer *fb,
2521 int x, int y)
17638cd6
JB
2522{
2523 struct drm_device *dev = crtc->dev;
2524 struct drm_i915_private *dev_priv = dev->dev_private;
2525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2526 struct drm_i915_gem_object *obj;
17638cd6 2527 int plane = intel_crtc->plane;
e506a0c6 2528 unsigned long linear_offset;
17638cd6 2529 u32 dspcntr;
f45651ba 2530 u32 reg = DSPCNTR(plane);
48404c1e
SJ
2531 int pixel_size;
2532
fdd508a6
VS
2533 if (!intel_crtc->primary_enabled) {
2534 I915_WRITE(reg, 0);
2535 I915_WRITE(DSPSURF(plane), 0);
2536 POSTING_READ(reg);
2537 return;
2538 }
2539
c9ba6fad
VS
2540 obj = intel_fb_obj(fb);
2541 if (WARN_ON(obj == NULL))
2542 return;
2543
2544 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2545
f45651ba
VS
2546 dspcntr = DISPPLANE_GAMMA_ENABLE;
2547
fdd508a6 2548 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2549
2550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2551 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2552
57779d06
VS
2553 switch (fb->pixel_format) {
2554 case DRM_FORMAT_C8:
17638cd6
JB
2555 dspcntr |= DISPPLANE_8BPP;
2556 break;
57779d06
VS
2557 case DRM_FORMAT_RGB565:
2558 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2559 break;
57779d06
VS
2560 case DRM_FORMAT_XRGB8888:
2561 case DRM_FORMAT_ARGB8888:
2562 dspcntr |= DISPPLANE_BGRX888;
2563 break;
2564 case DRM_FORMAT_XBGR8888:
2565 case DRM_FORMAT_ABGR8888:
2566 dspcntr |= DISPPLANE_RGBX888;
2567 break;
2568 case DRM_FORMAT_XRGB2101010:
2569 case DRM_FORMAT_ARGB2101010:
2570 dspcntr |= DISPPLANE_BGRX101010;
2571 break;
2572 case DRM_FORMAT_XBGR2101010:
2573 case DRM_FORMAT_ABGR2101010:
2574 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2575 break;
2576 default:
baba133a 2577 BUG();
17638cd6
JB
2578 }
2579
2580 if (obj->tiling_mode != I915_TILING_NONE)
2581 dspcntr |= DISPPLANE_TILED;
17638cd6 2582
f45651ba 2583 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2584 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2585
b9897127 2586 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2587 intel_crtc->dspaddr_offset =
bc752862 2588 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2589 pixel_size,
bc752862 2590 fb->pitches[0]);
c2c75131 2591 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2592 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2593 dspcntr |= DISPPLANE_ROTATE_180;
2594
2595 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2596 x += (intel_crtc->config.pipe_src_w - 1);
2597 y += (intel_crtc->config.pipe_src_h - 1);
2598
2599 /* Finding the last pixel of the last line of the display
2600 data and adding to linear_offset*/
2601 linear_offset +=
2602 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2603 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2604 }
2605 }
2606
2607 I915_WRITE(reg, dspcntr);
17638cd6 2608
f343c5f6
BW
2609 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2610 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2611 fb->pitches[0]);
01f2c773 2612 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2613 I915_WRITE(DSPSURF(plane),
2614 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2615 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2616 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2617 } else {
2618 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2619 I915_WRITE(DSPLINOFF(plane), linear_offset);
2620 }
17638cd6 2621 POSTING_READ(reg);
17638cd6
JB
2622}
2623
2624/* Assume fb object is pinned & idle & fenced and just update base pointers */
2625static int
2626intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2627 int x, int y, enum mode_set_atomic state)
2628{
2629 struct drm_device *dev = crtc->dev;
2630 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2631
6b8e6ed0
CW
2632 if (dev_priv->display.disable_fbc)
2633 dev_priv->display.disable_fbc(dev);
cc36513c 2634 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
81255565 2635
29b9bde6
DV
2636 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2637
2638 return 0;
81255565
JB
2639}
2640
96a02917
VS
2641void intel_display_handle_reset(struct drm_device *dev)
2642{
2643 struct drm_i915_private *dev_priv = dev->dev_private;
2644 struct drm_crtc *crtc;
2645
2646 /*
2647 * Flips in the rings have been nuked by the reset,
2648 * so complete all pending flips so that user space
2649 * will get its events and not get stuck.
2650 *
2651 * Also update the base address of all primary
2652 * planes to the the last fb to make sure we're
2653 * showing the correct fb after a reset.
2654 *
2655 * Need to make two loops over the crtcs so that we
2656 * don't try to grab a crtc mutex before the
2657 * pending_flip_queue really got woken up.
2658 */
2659
70e1e0ec 2660 for_each_crtc(dev, crtc) {
96a02917
VS
2661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2662 enum plane plane = intel_crtc->plane;
2663
2664 intel_prepare_page_flip(dev, plane);
2665 intel_finish_page_flip_plane(dev, plane);
2666 }
2667
70e1e0ec 2668 for_each_crtc(dev, crtc) {
96a02917
VS
2669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2670
51fd371b 2671 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2672 /*
2673 * FIXME: Once we have proper support for primary planes (and
2674 * disabling them without disabling the entire crtc) allow again
66e514c1 2675 * a NULL crtc->primary->fb.
947fdaad 2676 */
f4510a27 2677 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2678 dev_priv->display.update_primary_plane(crtc,
66e514c1 2679 crtc->primary->fb,
262ca2b0
MR
2680 crtc->x,
2681 crtc->y);
51fd371b 2682 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2683 }
2684}
2685
14667a4b
CW
2686static int
2687intel_finish_fb(struct drm_framebuffer *old_fb)
2688{
2ff8fde1 2689 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2690 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2691 bool was_interruptible = dev_priv->mm.interruptible;
2692 int ret;
2693
14667a4b
CW
2694 /* Big Hammer, we also need to ensure that any pending
2695 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2696 * current scanout is retired before unpinning the old
2697 * framebuffer.
2698 *
2699 * This should only fail upon a hung GPU, in which case we
2700 * can safely continue.
2701 */
2702 dev_priv->mm.interruptible = false;
2703 ret = i915_gem_object_finish_gpu(obj);
2704 dev_priv->mm.interruptible = was_interruptible;
2705
2706 return ret;
2707}
2708
7d5e3799
CW
2709static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2710{
2711 struct drm_device *dev = crtc->dev;
2712 struct drm_i915_private *dev_priv = dev->dev_private;
2713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2714 unsigned long flags;
2715 bool pending;
2716
2717 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2718 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2719 return false;
2720
2721 spin_lock_irqsave(&dev->event_lock, flags);
2722 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2723 spin_unlock_irqrestore(&dev->event_lock, flags);
2724
2725 return pending;
2726}
2727
5c3b82e2 2728static int
3c4fdcfb 2729intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2730 struct drm_framebuffer *fb)
79e53945
JB
2731{
2732 struct drm_device *dev = crtc->dev;
6b8e6ed0 2733 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2735 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2736 struct drm_framebuffer *old_fb = crtc->primary->fb;
2737 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2738 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2739 int ret;
79e53945 2740
7d5e3799
CW
2741 if (intel_crtc_has_pending_flip(crtc)) {
2742 DRM_ERROR("pipe is still busy with an old pageflip\n");
2743 return -EBUSY;
2744 }
2745
79e53945 2746 /* no fb bound */
94352cf9 2747 if (!fb) {
a5071c2f 2748 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2749 return 0;
2750 }
2751
7eb552ae 2752 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2753 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2754 plane_name(intel_crtc->plane),
2755 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2756 return -EINVAL;
79e53945
JB
2757 }
2758
5c3b82e2 2759 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2760 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2761 if (ret == 0)
91565c85 2762 i915_gem_track_fb(old_obj, obj,
a071fa00 2763 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2764 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2765 if (ret != 0) {
a5071c2f 2766 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2767 return ret;
2768 }
79e53945 2769
bb2043de
DL
2770 /*
2771 * Update pipe size and adjust fitter if needed: the reason for this is
2772 * that in compute_mode_changes we check the native mode (not the pfit
2773 * mode) to see if we can flip rather than do a full mode set. In the
2774 * fastboot case, we'll flip, but if we don't update the pipesrc and
2775 * pfit state, we'll end up with a big fb scanned out into the wrong
2776 * sized surface.
2777 *
2778 * To fix this properly, we need to hoist the checks up into
2779 * compute_mode_changes (or above), check the actual pfit state and
2780 * whether the platform allows pfit disable with pipe active, and only
2781 * then update the pipesrc and pfit state, even on the flip path.
2782 */
d330a953 2783 if (i915.fastboot) {
d7bf63f2
DL
2784 const struct drm_display_mode *adjusted_mode =
2785 &intel_crtc->config.adjusted_mode;
2786
4d6a3e63 2787 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2788 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2789 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2790 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2791 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2792 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2793 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2794 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2795 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2796 }
0637d60d
JB
2797 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2798 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2799 }
2800
29b9bde6 2801 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2802
f99d7069
DV
2803 if (intel_crtc->active)
2804 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2805
f4510a27 2806 crtc->primary->fb = fb;
6c4c86f5
DV
2807 crtc->x = x;
2808 crtc->y = y;
94352cf9 2809
b7f1de28 2810 if (old_fb) {
d7697eea
DV
2811 if (intel_crtc->active && old_fb != fb)
2812 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2813 mutex_lock(&dev->struct_mutex);
2ff8fde1 2814 intel_unpin_fb_obj(old_obj);
8ac36ec1 2815 mutex_unlock(&dev->struct_mutex);
b7f1de28 2816 }
652c393a 2817
8ac36ec1 2818 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2819 intel_update_fbc(dev);
5c3b82e2 2820 mutex_unlock(&dev->struct_mutex);
79e53945 2821
5c3b82e2 2822 return 0;
79e53945
JB
2823}
2824
5e84e1a4
ZW
2825static void intel_fdi_normal_train(struct drm_crtc *crtc)
2826{
2827 struct drm_device *dev = crtc->dev;
2828 struct drm_i915_private *dev_priv = dev->dev_private;
2829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2830 int pipe = intel_crtc->pipe;
2831 u32 reg, temp;
2832
2833 /* enable normal train */
2834 reg = FDI_TX_CTL(pipe);
2835 temp = I915_READ(reg);
61e499bf 2836 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2837 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2838 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2839 } else {
2840 temp &= ~FDI_LINK_TRAIN_NONE;
2841 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2842 }
5e84e1a4
ZW
2843 I915_WRITE(reg, temp);
2844
2845 reg = FDI_RX_CTL(pipe);
2846 temp = I915_READ(reg);
2847 if (HAS_PCH_CPT(dev)) {
2848 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2849 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2850 } else {
2851 temp &= ~FDI_LINK_TRAIN_NONE;
2852 temp |= FDI_LINK_TRAIN_NONE;
2853 }
2854 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2855
2856 /* wait one idle pattern time */
2857 POSTING_READ(reg);
2858 udelay(1000);
357555c0
JB
2859
2860 /* IVB wants error correction enabled */
2861 if (IS_IVYBRIDGE(dev))
2862 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2863 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2864}
2865
1fbc0d78 2866static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2867{
1fbc0d78
DV
2868 return crtc->base.enabled && crtc->active &&
2869 crtc->config.has_pch_encoder;
1e833f40
DV
2870}
2871
01a415fd
DV
2872static void ivb_modeset_global_resources(struct drm_device *dev)
2873{
2874 struct drm_i915_private *dev_priv = dev->dev_private;
2875 struct intel_crtc *pipe_B_crtc =
2876 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2877 struct intel_crtc *pipe_C_crtc =
2878 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2879 uint32_t temp;
2880
1e833f40
DV
2881 /*
2882 * When everything is off disable fdi C so that we could enable fdi B
2883 * with all lanes. Note that we don't care about enabled pipes without
2884 * an enabled pch encoder.
2885 */
2886 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2887 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2888 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2889 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2890
2891 temp = I915_READ(SOUTH_CHICKEN1);
2892 temp &= ~FDI_BC_BIFURCATION_SELECT;
2893 DRM_DEBUG_KMS("disabling fdi C rx\n");
2894 I915_WRITE(SOUTH_CHICKEN1, temp);
2895 }
2896}
2897
8db9d77b
ZW
2898/* The FDI link training functions for ILK/Ibexpeak. */
2899static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2900{
2901 struct drm_device *dev = crtc->dev;
2902 struct drm_i915_private *dev_priv = dev->dev_private;
2903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2904 int pipe = intel_crtc->pipe;
5eddb70b 2905 u32 reg, temp, tries;
8db9d77b 2906
1c8562f6 2907 /* FDI needs bits from pipe first */
0fc932b8 2908 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2909
e1a44743
AJ
2910 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2911 for train result */
5eddb70b
CW
2912 reg = FDI_RX_IMR(pipe);
2913 temp = I915_READ(reg);
e1a44743
AJ
2914 temp &= ~FDI_RX_SYMBOL_LOCK;
2915 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2916 I915_WRITE(reg, temp);
2917 I915_READ(reg);
e1a44743
AJ
2918 udelay(150);
2919
8db9d77b 2920 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2921 reg = FDI_TX_CTL(pipe);
2922 temp = I915_READ(reg);
627eb5a3
DV
2923 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2924 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2925 temp &= ~FDI_LINK_TRAIN_NONE;
2926 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2927 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2928
5eddb70b
CW
2929 reg = FDI_RX_CTL(pipe);
2930 temp = I915_READ(reg);
8db9d77b
ZW
2931 temp &= ~FDI_LINK_TRAIN_NONE;
2932 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2933 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2934
2935 POSTING_READ(reg);
8db9d77b
ZW
2936 udelay(150);
2937
5b2adf89 2938 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2939 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2940 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2941 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2942
5eddb70b 2943 reg = FDI_RX_IIR(pipe);
e1a44743 2944 for (tries = 0; tries < 5; tries++) {
5eddb70b 2945 temp = I915_READ(reg);
8db9d77b
ZW
2946 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2947
2948 if ((temp & FDI_RX_BIT_LOCK)) {
2949 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2950 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2951 break;
2952 }
8db9d77b 2953 }
e1a44743 2954 if (tries == 5)
5eddb70b 2955 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2956
2957 /* Train 2 */
5eddb70b
CW
2958 reg = FDI_TX_CTL(pipe);
2959 temp = I915_READ(reg);
8db9d77b
ZW
2960 temp &= ~FDI_LINK_TRAIN_NONE;
2961 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2962 I915_WRITE(reg, temp);
8db9d77b 2963
5eddb70b
CW
2964 reg = FDI_RX_CTL(pipe);
2965 temp = I915_READ(reg);
8db9d77b
ZW
2966 temp &= ~FDI_LINK_TRAIN_NONE;
2967 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2968 I915_WRITE(reg, temp);
8db9d77b 2969
5eddb70b
CW
2970 POSTING_READ(reg);
2971 udelay(150);
8db9d77b 2972
5eddb70b 2973 reg = FDI_RX_IIR(pipe);
e1a44743 2974 for (tries = 0; tries < 5; tries++) {
5eddb70b 2975 temp = I915_READ(reg);
8db9d77b
ZW
2976 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2977
2978 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2979 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2980 DRM_DEBUG_KMS("FDI train 2 done.\n");
2981 break;
2982 }
8db9d77b 2983 }
e1a44743 2984 if (tries == 5)
5eddb70b 2985 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2986
2987 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2988
8db9d77b
ZW
2989}
2990
0206e353 2991static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2992 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2993 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2994 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2995 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2996};
2997
2998/* The FDI link training functions for SNB/Cougarpoint. */
2999static void gen6_fdi_link_train(struct drm_crtc *crtc)
3000{
3001 struct drm_device *dev = crtc->dev;
3002 struct drm_i915_private *dev_priv = dev->dev_private;
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3004 int pipe = intel_crtc->pipe;
fa37d39e 3005 u32 reg, temp, i, retry;
8db9d77b 3006
e1a44743
AJ
3007 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3008 for train result */
5eddb70b
CW
3009 reg = FDI_RX_IMR(pipe);
3010 temp = I915_READ(reg);
e1a44743
AJ
3011 temp &= ~FDI_RX_SYMBOL_LOCK;
3012 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3013 I915_WRITE(reg, temp);
3014
3015 POSTING_READ(reg);
e1a44743
AJ
3016 udelay(150);
3017
8db9d77b 3018 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3019 reg = FDI_TX_CTL(pipe);
3020 temp = I915_READ(reg);
627eb5a3
DV
3021 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3022 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3023 temp &= ~FDI_LINK_TRAIN_NONE;
3024 temp |= FDI_LINK_TRAIN_PATTERN_1;
3025 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3026 /* SNB-B */
3027 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3028 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3029
d74cf324
DV
3030 I915_WRITE(FDI_RX_MISC(pipe),
3031 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3032
5eddb70b
CW
3033 reg = FDI_RX_CTL(pipe);
3034 temp = I915_READ(reg);
8db9d77b
ZW
3035 if (HAS_PCH_CPT(dev)) {
3036 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3037 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3038 } else {
3039 temp &= ~FDI_LINK_TRAIN_NONE;
3040 temp |= FDI_LINK_TRAIN_PATTERN_1;
3041 }
5eddb70b
CW
3042 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3043
3044 POSTING_READ(reg);
8db9d77b
ZW
3045 udelay(150);
3046
0206e353 3047 for (i = 0; i < 4; i++) {
5eddb70b
CW
3048 reg = FDI_TX_CTL(pipe);
3049 temp = I915_READ(reg);
8db9d77b
ZW
3050 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3051 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3052 I915_WRITE(reg, temp);
3053
3054 POSTING_READ(reg);
8db9d77b
ZW
3055 udelay(500);
3056
fa37d39e
SP
3057 for (retry = 0; retry < 5; retry++) {
3058 reg = FDI_RX_IIR(pipe);
3059 temp = I915_READ(reg);
3060 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3061 if (temp & FDI_RX_BIT_LOCK) {
3062 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3063 DRM_DEBUG_KMS("FDI train 1 done.\n");
3064 break;
3065 }
3066 udelay(50);
8db9d77b 3067 }
fa37d39e
SP
3068 if (retry < 5)
3069 break;
8db9d77b
ZW
3070 }
3071 if (i == 4)
5eddb70b 3072 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3073
3074 /* Train 2 */
5eddb70b
CW
3075 reg = FDI_TX_CTL(pipe);
3076 temp = I915_READ(reg);
8db9d77b
ZW
3077 temp &= ~FDI_LINK_TRAIN_NONE;
3078 temp |= FDI_LINK_TRAIN_PATTERN_2;
3079 if (IS_GEN6(dev)) {
3080 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3081 /* SNB-B */
3082 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3083 }
5eddb70b 3084 I915_WRITE(reg, temp);
8db9d77b 3085
5eddb70b
CW
3086 reg = FDI_RX_CTL(pipe);
3087 temp = I915_READ(reg);
8db9d77b
ZW
3088 if (HAS_PCH_CPT(dev)) {
3089 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3090 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3091 } else {
3092 temp &= ~FDI_LINK_TRAIN_NONE;
3093 temp |= FDI_LINK_TRAIN_PATTERN_2;
3094 }
5eddb70b
CW
3095 I915_WRITE(reg, temp);
3096
3097 POSTING_READ(reg);
8db9d77b
ZW
3098 udelay(150);
3099
0206e353 3100 for (i = 0; i < 4; i++) {
5eddb70b
CW
3101 reg = FDI_TX_CTL(pipe);
3102 temp = I915_READ(reg);
8db9d77b
ZW
3103 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3104 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3105 I915_WRITE(reg, temp);
3106
3107 POSTING_READ(reg);
8db9d77b
ZW
3108 udelay(500);
3109
fa37d39e
SP
3110 for (retry = 0; retry < 5; retry++) {
3111 reg = FDI_RX_IIR(pipe);
3112 temp = I915_READ(reg);
3113 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3114 if (temp & FDI_RX_SYMBOL_LOCK) {
3115 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3116 DRM_DEBUG_KMS("FDI train 2 done.\n");
3117 break;
3118 }
3119 udelay(50);
8db9d77b 3120 }
fa37d39e
SP
3121 if (retry < 5)
3122 break;
8db9d77b
ZW
3123 }
3124 if (i == 4)
5eddb70b 3125 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3126
3127 DRM_DEBUG_KMS("FDI train done.\n");
3128}
3129
357555c0
JB
3130/* Manual link training for Ivy Bridge A0 parts */
3131static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3132{
3133 struct drm_device *dev = crtc->dev;
3134 struct drm_i915_private *dev_priv = dev->dev_private;
3135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3136 int pipe = intel_crtc->pipe;
139ccd3f 3137 u32 reg, temp, i, j;
357555c0
JB
3138
3139 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3140 for train result */
3141 reg = FDI_RX_IMR(pipe);
3142 temp = I915_READ(reg);
3143 temp &= ~FDI_RX_SYMBOL_LOCK;
3144 temp &= ~FDI_RX_BIT_LOCK;
3145 I915_WRITE(reg, temp);
3146
3147 POSTING_READ(reg);
3148 udelay(150);
3149
01a415fd
DV
3150 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3151 I915_READ(FDI_RX_IIR(pipe)));
3152
139ccd3f
JB
3153 /* Try each vswing and preemphasis setting twice before moving on */
3154 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3155 /* disable first in case we need to retry */
3156 reg = FDI_TX_CTL(pipe);
3157 temp = I915_READ(reg);
3158 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3159 temp &= ~FDI_TX_ENABLE;
3160 I915_WRITE(reg, temp);
357555c0 3161
139ccd3f
JB
3162 reg = FDI_RX_CTL(pipe);
3163 temp = I915_READ(reg);
3164 temp &= ~FDI_LINK_TRAIN_AUTO;
3165 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3166 temp &= ~FDI_RX_ENABLE;
3167 I915_WRITE(reg, temp);
357555c0 3168
139ccd3f 3169 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3170 reg = FDI_TX_CTL(pipe);
3171 temp = I915_READ(reg);
139ccd3f
JB
3172 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3173 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3174 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3175 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3176 temp |= snb_b_fdi_train_param[j/2];
3177 temp |= FDI_COMPOSITE_SYNC;
3178 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3179
139ccd3f
JB
3180 I915_WRITE(FDI_RX_MISC(pipe),
3181 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3182
139ccd3f 3183 reg = FDI_RX_CTL(pipe);
357555c0 3184 temp = I915_READ(reg);
139ccd3f
JB
3185 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3186 temp |= FDI_COMPOSITE_SYNC;
3187 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3188
139ccd3f
JB
3189 POSTING_READ(reg);
3190 udelay(1); /* should be 0.5us */
357555c0 3191
139ccd3f
JB
3192 for (i = 0; i < 4; i++) {
3193 reg = FDI_RX_IIR(pipe);
3194 temp = I915_READ(reg);
3195 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3196
139ccd3f
JB
3197 if (temp & FDI_RX_BIT_LOCK ||
3198 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3199 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3200 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3201 i);
3202 break;
3203 }
3204 udelay(1); /* should be 0.5us */
3205 }
3206 if (i == 4) {
3207 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3208 continue;
3209 }
357555c0 3210
139ccd3f 3211 /* Train 2 */
357555c0
JB
3212 reg = FDI_TX_CTL(pipe);
3213 temp = I915_READ(reg);
139ccd3f
JB
3214 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3215 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3216 I915_WRITE(reg, temp);
3217
3218 reg = FDI_RX_CTL(pipe);
3219 temp = I915_READ(reg);
3220 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3221 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3222 I915_WRITE(reg, temp);
3223
3224 POSTING_READ(reg);
139ccd3f 3225 udelay(2); /* should be 1.5us */
357555c0 3226
139ccd3f
JB
3227 for (i = 0; i < 4; i++) {
3228 reg = FDI_RX_IIR(pipe);
3229 temp = I915_READ(reg);
3230 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3231
139ccd3f
JB
3232 if (temp & FDI_RX_SYMBOL_LOCK ||
3233 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3234 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3235 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3236 i);
3237 goto train_done;
3238 }
3239 udelay(2); /* should be 1.5us */
357555c0 3240 }
139ccd3f
JB
3241 if (i == 4)
3242 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3243 }
357555c0 3244
139ccd3f 3245train_done:
357555c0
JB
3246 DRM_DEBUG_KMS("FDI train done.\n");
3247}
3248
88cefb6c 3249static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3250{
88cefb6c 3251 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3252 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3253 int pipe = intel_crtc->pipe;
5eddb70b 3254 u32 reg, temp;
79e53945 3255
c64e311e 3256
c98e9dcf 3257 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
627eb5a3
DV
3260 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3261 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3262 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3263 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3264
3265 POSTING_READ(reg);
c98e9dcf
JB
3266 udelay(200);
3267
3268 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3269 temp = I915_READ(reg);
3270 I915_WRITE(reg, temp | FDI_PCDCLK);
3271
3272 POSTING_READ(reg);
c98e9dcf
JB
3273 udelay(200);
3274
20749730
PZ
3275 /* Enable CPU FDI TX PLL, always on for Ironlake */
3276 reg = FDI_TX_CTL(pipe);
3277 temp = I915_READ(reg);
3278 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3279 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3280
20749730
PZ
3281 POSTING_READ(reg);
3282 udelay(100);
6be4a607 3283 }
0e23b99d
JB
3284}
3285
88cefb6c
DV
3286static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3287{
3288 struct drm_device *dev = intel_crtc->base.dev;
3289 struct drm_i915_private *dev_priv = dev->dev_private;
3290 int pipe = intel_crtc->pipe;
3291 u32 reg, temp;
3292
3293 /* Switch from PCDclk to Rawclk */
3294 reg = FDI_RX_CTL(pipe);
3295 temp = I915_READ(reg);
3296 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3297
3298 /* Disable CPU FDI TX PLL */
3299 reg = FDI_TX_CTL(pipe);
3300 temp = I915_READ(reg);
3301 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3302
3303 POSTING_READ(reg);
3304 udelay(100);
3305
3306 reg = FDI_RX_CTL(pipe);
3307 temp = I915_READ(reg);
3308 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3309
3310 /* Wait for the clocks to turn off. */
3311 POSTING_READ(reg);
3312 udelay(100);
3313}
3314
0fc932b8
JB
3315static void ironlake_fdi_disable(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3320 int pipe = intel_crtc->pipe;
3321 u32 reg, temp;
3322
3323 /* disable CPU FDI tx and PCH FDI rx */
3324 reg = FDI_TX_CTL(pipe);
3325 temp = I915_READ(reg);
3326 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3327 POSTING_READ(reg);
3328
3329 reg = FDI_RX_CTL(pipe);
3330 temp = I915_READ(reg);
3331 temp &= ~(0x7 << 16);
dfd07d72 3332 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3333 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3334
3335 POSTING_READ(reg);
3336 udelay(100);
3337
3338 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3339 if (HAS_PCH_IBX(dev))
6f06ce18 3340 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3341
3342 /* still set train pattern 1 */
3343 reg = FDI_TX_CTL(pipe);
3344 temp = I915_READ(reg);
3345 temp &= ~FDI_LINK_TRAIN_NONE;
3346 temp |= FDI_LINK_TRAIN_PATTERN_1;
3347 I915_WRITE(reg, temp);
3348
3349 reg = FDI_RX_CTL(pipe);
3350 temp = I915_READ(reg);
3351 if (HAS_PCH_CPT(dev)) {
3352 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3353 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3354 } else {
3355 temp &= ~FDI_LINK_TRAIN_NONE;
3356 temp |= FDI_LINK_TRAIN_PATTERN_1;
3357 }
3358 /* BPC in FDI rx is consistent with that in PIPECONF */
3359 temp &= ~(0x07 << 16);
dfd07d72 3360 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3361 I915_WRITE(reg, temp);
3362
3363 POSTING_READ(reg);
3364 udelay(100);
3365}
3366
5dce5b93
CW
3367bool intel_has_pending_fb_unpin(struct drm_device *dev)
3368{
3369 struct intel_crtc *crtc;
3370
3371 /* Note that we don't need to be called with mode_config.lock here
3372 * as our list of CRTC objects is static for the lifetime of the
3373 * device and so cannot disappear as we iterate. Similarly, we can
3374 * happily treat the predicates as racy, atomic checks as userspace
3375 * cannot claim and pin a new fb without at least acquring the
3376 * struct_mutex and so serialising with us.
3377 */
d3fcc808 3378 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3379 if (atomic_read(&crtc->unpin_work_count) == 0)
3380 continue;
3381
3382 if (crtc->unpin_work)
3383 intel_wait_for_vblank(dev, crtc->pipe);
3384
3385 return true;
3386 }
3387
3388 return false;
3389}
3390
46a55d30 3391void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3392{
0f91128d 3393 struct drm_device *dev = crtc->dev;
5bb61643 3394 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3395
2c10d571 3396 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
eed6d67d
DV
3397 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3398 !intel_crtc_has_pending_flip(crtc),
3399 60*HZ) == 0);
5bb61643 3400
975d568a
CW
3401 if (crtc->primary->fb) {
3402 mutex_lock(&dev->struct_mutex);
3403 intel_finish_fb(crtc->primary->fb);
3404 mutex_unlock(&dev->struct_mutex);
3405 }
e6c3a2a6
CW
3406}
3407
e615efe4
ED
3408/* Program iCLKIP clock to the desired frequency */
3409static void lpt_program_iclkip(struct drm_crtc *crtc)
3410{
3411 struct drm_device *dev = crtc->dev;
3412 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3413 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3414 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3415 u32 temp;
3416
09153000
DV
3417 mutex_lock(&dev_priv->dpio_lock);
3418
e615efe4
ED
3419 /* It is necessary to ungate the pixclk gate prior to programming
3420 * the divisors, and gate it back when it is done.
3421 */
3422 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3423
3424 /* Disable SSCCTL */
3425 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3426 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3427 SBI_SSCCTL_DISABLE,
3428 SBI_ICLK);
e615efe4
ED
3429
3430 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3431 if (clock == 20000) {
e615efe4
ED
3432 auxdiv = 1;
3433 divsel = 0x41;
3434 phaseinc = 0x20;
3435 } else {
3436 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3437 * but the adjusted_mode->crtc_clock in in KHz. To get the
3438 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3439 * convert the virtual clock precision to KHz here for higher
3440 * precision.
3441 */
3442 u32 iclk_virtual_root_freq = 172800 * 1000;
3443 u32 iclk_pi_range = 64;
3444 u32 desired_divisor, msb_divisor_value, pi_value;
3445
12d7ceed 3446 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3447 msb_divisor_value = desired_divisor / iclk_pi_range;
3448 pi_value = desired_divisor % iclk_pi_range;
3449
3450 auxdiv = 0;
3451 divsel = msb_divisor_value - 2;
3452 phaseinc = pi_value;
3453 }
3454
3455 /* This should not happen with any sane values */
3456 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3457 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3458 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3459 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3460
3461 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3462 clock,
e615efe4
ED
3463 auxdiv,
3464 divsel,
3465 phasedir,
3466 phaseinc);
3467
3468 /* Program SSCDIVINTPHASE6 */
988d6ee8 3469 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3470 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3471 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3472 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3473 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3474 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3475 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3476 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3477
3478 /* Program SSCAUXDIV */
988d6ee8 3479 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3480 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3481 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3482 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3483
3484 /* Enable modulator and associated divider */
988d6ee8 3485 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3486 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3487 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3488
3489 /* Wait for initialization time */
3490 udelay(24);
3491
3492 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3493
3494 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3495}
3496
275f01b2
DV
3497static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3498 enum pipe pch_transcoder)
3499{
3500 struct drm_device *dev = crtc->base.dev;
3501 struct drm_i915_private *dev_priv = dev->dev_private;
3502 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3503
3504 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3505 I915_READ(HTOTAL(cpu_transcoder)));
3506 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3507 I915_READ(HBLANK(cpu_transcoder)));
3508 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3509 I915_READ(HSYNC(cpu_transcoder)));
3510
3511 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3512 I915_READ(VTOTAL(cpu_transcoder)));
3513 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3514 I915_READ(VBLANK(cpu_transcoder)));
3515 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3516 I915_READ(VSYNC(cpu_transcoder)));
3517 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3518 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3519}
3520
1fbc0d78
DV
3521static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3522{
3523 struct drm_i915_private *dev_priv = dev->dev_private;
3524 uint32_t temp;
3525
3526 temp = I915_READ(SOUTH_CHICKEN1);
3527 if (temp & FDI_BC_BIFURCATION_SELECT)
3528 return;
3529
3530 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3531 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3532
3533 temp |= FDI_BC_BIFURCATION_SELECT;
3534 DRM_DEBUG_KMS("enabling fdi C rx\n");
3535 I915_WRITE(SOUTH_CHICKEN1, temp);
3536 POSTING_READ(SOUTH_CHICKEN1);
3537}
3538
3539static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3540{
3541 struct drm_device *dev = intel_crtc->base.dev;
3542 struct drm_i915_private *dev_priv = dev->dev_private;
3543
3544 switch (intel_crtc->pipe) {
3545 case PIPE_A:
3546 break;
3547 case PIPE_B:
3548 if (intel_crtc->config.fdi_lanes > 2)
3549 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3550 else
3551 cpt_enable_fdi_bc_bifurcation(dev);
3552
3553 break;
3554 case PIPE_C:
3555 cpt_enable_fdi_bc_bifurcation(dev);
3556
3557 break;
3558 default:
3559 BUG();
3560 }
3561}
3562
f67a559d
JB
3563/*
3564 * Enable PCH resources required for PCH ports:
3565 * - PCH PLLs
3566 * - FDI training & RX/TX
3567 * - update transcoder timings
3568 * - DP transcoding bits
3569 * - transcoder
3570 */
3571static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3572{
3573 struct drm_device *dev = crtc->dev;
3574 struct drm_i915_private *dev_priv = dev->dev_private;
3575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3576 int pipe = intel_crtc->pipe;
ee7b9f93 3577 u32 reg, temp;
2c07245f 3578
ab9412ba 3579 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3580
1fbc0d78
DV
3581 if (IS_IVYBRIDGE(dev))
3582 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3583
cd986abb
DV
3584 /* Write the TU size bits before fdi link training, so that error
3585 * detection works. */
3586 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3587 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3588
c98e9dcf 3589 /* For PCH output, training FDI link */
674cf967 3590 dev_priv->display.fdi_link_train(crtc);
2c07245f 3591
3ad8a208
DV
3592 /* We need to program the right clock selection before writing the pixel
3593 * mutliplier into the DPLL. */
303b81e0 3594 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3595 u32 sel;
4b645f14 3596
c98e9dcf 3597 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3598 temp |= TRANS_DPLL_ENABLE(pipe);
3599 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3600 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3601 temp |= sel;
3602 else
3603 temp &= ~sel;
c98e9dcf 3604 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3605 }
5eddb70b 3606
3ad8a208
DV
3607 /* XXX: pch pll's can be enabled any time before we enable the PCH
3608 * transcoder, and we actually should do this to not upset any PCH
3609 * transcoder that already use the clock when we share it.
3610 *
3611 * Note that enable_shared_dpll tries to do the right thing, but
3612 * get_shared_dpll unconditionally resets the pll - we need that to have
3613 * the right LVDS enable sequence. */
85b3894f 3614 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3615
d9b6cb56
JB
3616 /* set transcoder timing, panel must allow it */
3617 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3618 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3619
303b81e0 3620 intel_fdi_normal_train(crtc);
5e84e1a4 3621
c98e9dcf
JB
3622 /* For PCH DP, enable TRANS_DP_CTL */
3623 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3624 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3625 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3626 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3627 reg = TRANS_DP_CTL(pipe);
3628 temp = I915_READ(reg);
3629 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3630 TRANS_DP_SYNC_MASK |
3631 TRANS_DP_BPC_MASK);
5eddb70b
CW
3632 temp |= (TRANS_DP_OUTPUT_ENABLE |
3633 TRANS_DP_ENH_FRAMING);
9325c9f0 3634 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3635
3636 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3637 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3638 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3639 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3640
3641 switch (intel_trans_dp_port_sel(crtc)) {
3642 case PCH_DP_B:
5eddb70b 3643 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3644 break;
3645 case PCH_DP_C:
5eddb70b 3646 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3647 break;
3648 case PCH_DP_D:
5eddb70b 3649 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3650 break;
3651 default:
e95d41e1 3652 BUG();
32f9d658 3653 }
2c07245f 3654
5eddb70b 3655 I915_WRITE(reg, temp);
6be4a607 3656 }
b52eb4dc 3657
b8a4f404 3658 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3659}
3660
1507e5bd
PZ
3661static void lpt_pch_enable(struct drm_crtc *crtc)
3662{
3663 struct drm_device *dev = crtc->dev;
3664 struct drm_i915_private *dev_priv = dev->dev_private;
3665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3666 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3667
ab9412ba 3668 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3669
8c52b5e8 3670 lpt_program_iclkip(crtc);
1507e5bd 3671
0540e488 3672 /* Set transcoder timing. */
275f01b2 3673 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3674
937bb610 3675 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3676}
3677
716c2e55 3678void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3679{
e2b78267 3680 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3681
3682 if (pll == NULL)
3683 return;
3684
3685 if (pll->refcount == 0) {
46edb027 3686 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3687 return;
3688 }
3689
f4a091c7
DV
3690 if (--pll->refcount == 0) {
3691 WARN_ON(pll->on);
3692 WARN_ON(pll->active);
3693 }
3694
a43f6e0f 3695 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3696}
3697
716c2e55 3698struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3699{
e2b78267
DV
3700 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3701 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3702 enum intel_dpll_id i;
ee7b9f93 3703
ee7b9f93 3704 if (pll) {
46edb027
DV
3705 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3706 crtc->base.base.id, pll->name);
e2b78267 3707 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3708 }
3709
98b6bd99
DV
3710 if (HAS_PCH_IBX(dev_priv->dev)) {
3711 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3712 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3713 pll = &dev_priv->shared_dplls[i];
98b6bd99 3714
46edb027
DV
3715 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3716 crtc->base.base.id, pll->name);
98b6bd99 3717
f2a69f44
DV
3718 WARN_ON(pll->refcount);
3719
98b6bd99
DV
3720 goto found;
3721 }
3722
e72f9fbf
DV
3723 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3724 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3725
3726 /* Only want to check enabled timings first */
3727 if (pll->refcount == 0)
3728 continue;
3729
b89a1d39
DV
3730 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3731 sizeof(pll->hw_state)) == 0) {
46edb027 3732 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3733 crtc->base.base.id,
46edb027 3734 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3735
3736 goto found;
3737 }
3738 }
3739
3740 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3741 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3742 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3743 if (pll->refcount == 0) {
46edb027
DV
3744 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3745 crtc->base.base.id, pll->name);
ee7b9f93
JB
3746 goto found;
3747 }
3748 }
3749
3750 return NULL;
3751
3752found:
f2a69f44
DV
3753 if (pll->refcount == 0)
3754 pll->hw_state = crtc->config.dpll_hw_state;
3755
a43f6e0f 3756 crtc->config.shared_dpll = i;
46edb027
DV
3757 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3758 pipe_name(crtc->pipe));
ee7b9f93 3759
cdbd2316 3760 pll->refcount++;
e04c7350 3761
ee7b9f93
JB
3762 return pll;
3763}
3764
a1520318 3765static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3766{
3767 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3768 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3769 u32 temp;
3770
3771 temp = I915_READ(dslreg);
3772 udelay(500);
3773 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3774 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3775 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3776 }
3777}
3778
b074cec8
JB
3779static void ironlake_pfit_enable(struct intel_crtc *crtc)
3780{
3781 struct drm_device *dev = crtc->base.dev;
3782 struct drm_i915_private *dev_priv = dev->dev_private;
3783 int pipe = crtc->pipe;
3784
fd4daa9c 3785 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3786 /* Force use of hard-coded filter coefficients
3787 * as some pre-programmed values are broken,
3788 * e.g. x201.
3789 */
3790 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3791 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3792 PF_PIPE_SEL_IVB(pipe));
3793 else
3794 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3795 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3796 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3797 }
3798}
3799
bb53d4ae
VS
3800static void intel_enable_planes(struct drm_crtc *crtc)
3801{
3802 struct drm_device *dev = crtc->dev;
3803 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3804 struct drm_plane *plane;
bb53d4ae
VS
3805 struct intel_plane *intel_plane;
3806
af2b653b
MR
3807 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3808 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3809 if (intel_plane->pipe == pipe)
3810 intel_plane_restore(&intel_plane->base);
af2b653b 3811 }
bb53d4ae
VS
3812}
3813
3814static void intel_disable_planes(struct drm_crtc *crtc)
3815{
3816 struct drm_device *dev = crtc->dev;
3817 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3818 struct drm_plane *plane;
bb53d4ae
VS
3819 struct intel_plane *intel_plane;
3820
af2b653b
MR
3821 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3822 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3823 if (intel_plane->pipe == pipe)
3824 intel_plane_disable(&intel_plane->base);
af2b653b 3825 }
bb53d4ae
VS
3826}
3827
20bc8673 3828void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3829{
cea165c3
VS
3830 struct drm_device *dev = crtc->base.dev;
3831 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3832
3833 if (!crtc->config.ips_enabled)
3834 return;
3835
cea165c3
VS
3836 /* We can only enable IPS after we enable a plane and wait for a vblank */
3837 intel_wait_for_vblank(dev, crtc->pipe);
3838
d77e4531 3839 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3840 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3841 mutex_lock(&dev_priv->rps.hw_lock);
3842 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3843 mutex_unlock(&dev_priv->rps.hw_lock);
3844 /* Quoting Art Runyan: "its not safe to expect any particular
3845 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3846 * mailbox." Moreover, the mailbox may return a bogus state,
3847 * so we need to just enable it and continue on.
2a114cc1
BW
3848 */
3849 } else {
3850 I915_WRITE(IPS_CTL, IPS_ENABLE);
3851 /* The bit only becomes 1 in the next vblank, so this wait here
3852 * is essentially intel_wait_for_vblank. If we don't have this
3853 * and don't wait for vblanks until the end of crtc_enable, then
3854 * the HW state readout code will complain that the expected
3855 * IPS_CTL value is not the one we read. */
3856 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3857 DRM_ERROR("Timed out waiting for IPS enable\n");
3858 }
d77e4531
PZ
3859}
3860
20bc8673 3861void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3862{
3863 struct drm_device *dev = crtc->base.dev;
3864 struct drm_i915_private *dev_priv = dev->dev_private;
3865
3866 if (!crtc->config.ips_enabled)
3867 return;
3868
3869 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3870 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3871 mutex_lock(&dev_priv->rps.hw_lock);
3872 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3873 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3874 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3875 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3876 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3877 } else {
2a114cc1 3878 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3879 POSTING_READ(IPS_CTL);
3880 }
d77e4531
PZ
3881
3882 /* We need to wait for a vblank before we can disable the plane. */
3883 intel_wait_for_vblank(dev, crtc->pipe);
3884}
3885
3886/** Loads the palette/gamma unit for the CRTC with the prepared values */
3887static void intel_crtc_load_lut(struct drm_crtc *crtc)
3888{
3889 struct drm_device *dev = crtc->dev;
3890 struct drm_i915_private *dev_priv = dev->dev_private;
3891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3892 enum pipe pipe = intel_crtc->pipe;
3893 int palreg = PALETTE(pipe);
3894 int i;
3895 bool reenable_ips = false;
3896
3897 /* The clocks have to be on to load the palette. */
3898 if (!crtc->enabled || !intel_crtc->active)
3899 return;
3900
3901 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3902 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3903 assert_dsi_pll_enabled(dev_priv);
3904 else
3905 assert_pll_enabled(dev_priv, pipe);
3906 }
3907
3908 /* use legacy palette for Ironlake */
7a1db49a 3909 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
3910 palreg = LGC_PALETTE(pipe);
3911
3912 /* Workaround : Do not read or write the pipe palette/gamma data while
3913 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3914 */
41e6fc4c 3915 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3916 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3917 GAMMA_MODE_MODE_SPLIT)) {
3918 hsw_disable_ips(intel_crtc);
3919 reenable_ips = true;
3920 }
3921
3922 for (i = 0; i < 256; i++) {
3923 I915_WRITE(palreg + 4 * i,
3924 (intel_crtc->lut_r[i] << 16) |
3925 (intel_crtc->lut_g[i] << 8) |
3926 intel_crtc->lut_b[i]);
3927 }
3928
3929 if (reenable_ips)
3930 hsw_enable_ips(intel_crtc);
3931}
3932
d3eedb1a
VS
3933static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3934{
3935 if (!enable && intel_crtc->overlay) {
3936 struct drm_device *dev = intel_crtc->base.dev;
3937 struct drm_i915_private *dev_priv = dev->dev_private;
3938
3939 mutex_lock(&dev->struct_mutex);
3940 dev_priv->mm.interruptible = false;
3941 (void) intel_overlay_switch_off(intel_crtc->overlay);
3942 dev_priv->mm.interruptible = true;
3943 mutex_unlock(&dev->struct_mutex);
3944 }
3945
3946 /* Let userspace switch the overlay on again. In most cases userspace
3947 * has to recompute where to put it anyway.
3948 */
3949}
3950
d3eedb1a 3951static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3952{
3953 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
3954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3955 int pipe = intel_crtc->pipe;
a5c4d7bc 3956
f98551ae
VS
3957 drm_vblank_on(dev, pipe);
3958
fdd508a6 3959 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
3960 intel_enable_planes(crtc);
3961 intel_crtc_update_cursor(crtc, true);
d3eedb1a 3962 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
3963
3964 hsw_enable_ips(intel_crtc);
3965
3966 mutex_lock(&dev->struct_mutex);
3967 intel_update_fbc(dev);
3968 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
3969
3970 /*
3971 * FIXME: Once we grow proper nuclear flip support out of this we need
3972 * to compute the mask of flip planes precisely. For the time being
3973 * consider this a flip from a NULL plane.
3974 */
3975 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
3976}
3977
d3eedb1a 3978static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3979{
3980 struct drm_device *dev = crtc->dev;
3981 struct drm_i915_private *dev_priv = dev->dev_private;
3982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3983 int pipe = intel_crtc->pipe;
3984 int plane = intel_crtc->plane;
3985
3986 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
3987
3988 if (dev_priv->fbc.plane == plane)
3989 intel_disable_fbc(dev);
3990
3991 hsw_disable_ips(intel_crtc);
3992
d3eedb1a 3993 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
3994 intel_crtc_update_cursor(crtc, false);
3995 intel_disable_planes(crtc);
fdd508a6 3996 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 3997
f99d7069
DV
3998 /*
3999 * FIXME: Once we grow proper nuclear flip support out of this we need
4000 * to compute the mask of flip planes precisely. For the time being
4001 * consider this a flip to a NULL plane.
4002 */
4003 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4004
f98551ae 4005 drm_vblank_off(dev, pipe);
a5c4d7bc
VS
4006}
4007
f67a559d
JB
4008static void ironlake_crtc_enable(struct drm_crtc *crtc)
4009{
4010 struct drm_device *dev = crtc->dev;
4011 struct drm_i915_private *dev_priv = dev->dev_private;
4012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4013 struct intel_encoder *encoder;
f67a559d 4014 int pipe = intel_crtc->pipe;
f67a559d 4015
08a48469
DV
4016 WARN_ON(!crtc->enabled);
4017
f67a559d
JB
4018 if (intel_crtc->active)
4019 return;
4020
b14b1055
DV
4021 if (intel_crtc->config.has_pch_encoder)
4022 intel_prepare_shared_dpll(intel_crtc);
4023
29407aab
DV
4024 if (intel_crtc->config.has_dp_encoder)
4025 intel_dp_set_m_n(intel_crtc);
4026
4027 intel_set_pipe_timings(intel_crtc);
4028
4029 if (intel_crtc->config.has_pch_encoder) {
4030 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4031 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4032 }
4033
4034 ironlake_set_pipeconf(crtc);
4035
f67a559d 4036 intel_crtc->active = true;
8664281b
PZ
4037
4038 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4039 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4040
f6736a1a 4041 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4042 if (encoder->pre_enable)
4043 encoder->pre_enable(encoder);
f67a559d 4044
5bfe2ac0 4045 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4046 /* Note: FDI PLL enabling _must_ be done before we enable the
4047 * cpu pipes, hence this is separate from all the other fdi/pch
4048 * enabling. */
88cefb6c 4049 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4050 } else {
4051 assert_fdi_tx_disabled(dev_priv, pipe);
4052 assert_fdi_rx_disabled(dev_priv, pipe);
4053 }
f67a559d 4054
b074cec8 4055 ironlake_pfit_enable(intel_crtc);
f67a559d 4056
9c54c0dd
JB
4057 /*
4058 * On ILK+ LUT must be loaded before the pipe is running but with
4059 * clocks enabled
4060 */
4061 intel_crtc_load_lut(crtc);
4062
f37fcc2a 4063 intel_update_watermarks(crtc);
e1fdc473 4064 intel_enable_pipe(intel_crtc);
f67a559d 4065
5bfe2ac0 4066 if (intel_crtc->config.has_pch_encoder)
f67a559d 4067 ironlake_pch_enable(crtc);
c98e9dcf 4068
fa5c73b1
DV
4069 for_each_encoder_on_crtc(dev, crtc, encoder)
4070 encoder->enable(encoder);
61b77ddd
DV
4071
4072 if (HAS_PCH_CPT(dev))
a1520318 4073 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4074
d3eedb1a 4075 intel_crtc_enable_planes(crtc);
6be4a607
JB
4076}
4077
42db64ef
PZ
4078/* IPS only exists on ULT machines and is tied to pipe A. */
4079static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4080{
f5adf94e 4081 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4082}
4083
e4916946
PZ
4084/*
4085 * This implements the workaround described in the "notes" section of the mode
4086 * set sequence documentation. When going from no pipes or single pipe to
4087 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4088 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4089 */
4090static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4091{
4092 struct drm_device *dev = crtc->base.dev;
4093 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4094
4095 /* We want to get the other_active_crtc only if there's only 1 other
4096 * active crtc. */
d3fcc808 4097 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4098 if (!crtc_it->active || crtc_it == crtc)
4099 continue;
4100
4101 if (other_active_crtc)
4102 return;
4103
4104 other_active_crtc = crtc_it;
4105 }
4106 if (!other_active_crtc)
4107 return;
4108
4109 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4110 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4111}
4112
4f771f10
PZ
4113static void haswell_crtc_enable(struct drm_crtc *crtc)
4114{
4115 struct drm_device *dev = crtc->dev;
4116 struct drm_i915_private *dev_priv = dev->dev_private;
4117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4118 struct intel_encoder *encoder;
4119 int pipe = intel_crtc->pipe;
4f771f10
PZ
4120
4121 WARN_ON(!crtc->enabled);
4122
4123 if (intel_crtc->active)
4124 return;
4125
df8ad70c
DV
4126 if (intel_crtc_to_shared_dpll(intel_crtc))
4127 intel_enable_shared_dpll(intel_crtc);
4128
229fca97
DV
4129 if (intel_crtc->config.has_dp_encoder)
4130 intel_dp_set_m_n(intel_crtc);
4131
4132 intel_set_pipe_timings(intel_crtc);
4133
4134 if (intel_crtc->config.has_pch_encoder) {
4135 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4136 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4137 }
4138
4139 haswell_set_pipeconf(crtc);
4140
4141 intel_set_pipe_csc(crtc);
4142
4f771f10 4143 intel_crtc->active = true;
8664281b
PZ
4144
4145 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4f771f10
PZ
4146 for_each_encoder_on_crtc(dev, crtc, encoder)
4147 if (encoder->pre_enable)
4148 encoder->pre_enable(encoder);
4149
4fe9467d
ID
4150 if (intel_crtc->config.has_pch_encoder) {
4151 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4152 dev_priv->display.fdi_link_train(crtc);
4153 }
4154
1f544388 4155 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4156
b074cec8 4157 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4158
4159 /*
4160 * On ILK+ LUT must be loaded before the pipe is running but with
4161 * clocks enabled
4162 */
4163 intel_crtc_load_lut(crtc);
4164
1f544388 4165 intel_ddi_set_pipe_settings(crtc);
8228c251 4166 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4167
f37fcc2a 4168 intel_update_watermarks(crtc);
e1fdc473 4169 intel_enable_pipe(intel_crtc);
42db64ef 4170
5bfe2ac0 4171 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4172 lpt_pch_enable(crtc);
4f771f10 4173
0e32b39c
DA
4174 if (intel_crtc->config.dp_encoder_is_mst)
4175 intel_ddi_set_vc_payload_alloc(crtc, true);
4176
8807e55b 4177 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4178 encoder->enable(encoder);
8807e55b
JN
4179 intel_opregion_notify_encoder(encoder, true);
4180 }
4f771f10 4181
e4916946
PZ
4182 /* If we change the relative order between pipe/planes enabling, we need
4183 * to change the workaround. */
4184 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4185 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4186}
4187
3f8dce3a
DV
4188static void ironlake_pfit_disable(struct intel_crtc *crtc)
4189{
4190 struct drm_device *dev = crtc->base.dev;
4191 struct drm_i915_private *dev_priv = dev->dev_private;
4192 int pipe = crtc->pipe;
4193
4194 /* To avoid upsetting the power well on haswell only disable the pfit if
4195 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4196 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4197 I915_WRITE(PF_CTL(pipe), 0);
4198 I915_WRITE(PF_WIN_POS(pipe), 0);
4199 I915_WRITE(PF_WIN_SZ(pipe), 0);
4200 }
4201}
4202
6be4a607
JB
4203static void ironlake_crtc_disable(struct drm_crtc *crtc)
4204{
4205 struct drm_device *dev = crtc->dev;
4206 struct drm_i915_private *dev_priv = dev->dev_private;
4207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4208 struct intel_encoder *encoder;
6be4a607 4209 int pipe = intel_crtc->pipe;
5eddb70b 4210 u32 reg, temp;
b52eb4dc 4211
f7abfe8b
CW
4212 if (!intel_crtc->active)
4213 return;
4214
d3eedb1a 4215 intel_crtc_disable_planes(crtc);
a5c4d7bc 4216
ea9d758d
DV
4217 for_each_encoder_on_crtc(dev, crtc, encoder)
4218 encoder->disable(encoder);
4219
d925c59a
DV
4220 if (intel_crtc->config.has_pch_encoder)
4221 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4222
575f7ab7 4223 intel_disable_pipe(intel_crtc);
32f9d658 4224
0e32b39c
DA
4225 if (intel_crtc->config.dp_encoder_is_mst)
4226 intel_ddi_set_vc_payload_alloc(crtc, false);
4227
3f8dce3a 4228 ironlake_pfit_disable(intel_crtc);
2c07245f 4229
bf49ec8c
DV
4230 for_each_encoder_on_crtc(dev, crtc, encoder)
4231 if (encoder->post_disable)
4232 encoder->post_disable(encoder);
2c07245f 4233
d925c59a
DV
4234 if (intel_crtc->config.has_pch_encoder) {
4235 ironlake_fdi_disable(crtc);
913d8d11 4236
d925c59a
DV
4237 ironlake_disable_pch_transcoder(dev_priv, pipe);
4238 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4239
d925c59a
DV
4240 if (HAS_PCH_CPT(dev)) {
4241 /* disable TRANS_DP_CTL */
4242 reg = TRANS_DP_CTL(pipe);
4243 temp = I915_READ(reg);
4244 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4245 TRANS_DP_PORT_SEL_MASK);
4246 temp |= TRANS_DP_PORT_SEL_NONE;
4247 I915_WRITE(reg, temp);
4248
4249 /* disable DPLL_SEL */
4250 temp = I915_READ(PCH_DPLL_SEL);
11887397 4251 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4252 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4253 }
e3421a18 4254
d925c59a 4255 /* disable PCH DPLL */
e72f9fbf 4256 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4257
d925c59a
DV
4258 ironlake_fdi_pll_disable(intel_crtc);
4259 }
6b383a7f 4260
f7abfe8b 4261 intel_crtc->active = false;
46ba614c 4262 intel_update_watermarks(crtc);
d1ebd816
BW
4263
4264 mutex_lock(&dev->struct_mutex);
6b383a7f 4265 intel_update_fbc(dev);
d1ebd816 4266 mutex_unlock(&dev->struct_mutex);
6be4a607 4267}
1b3c7a47 4268
4f771f10 4269static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4270{
4f771f10
PZ
4271 struct drm_device *dev = crtc->dev;
4272 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4274 struct intel_encoder *encoder;
3b117c8f 4275 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4276
4f771f10
PZ
4277 if (!intel_crtc->active)
4278 return;
4279
d3eedb1a 4280 intel_crtc_disable_planes(crtc);
dda9a66a 4281
8807e55b
JN
4282 for_each_encoder_on_crtc(dev, crtc, encoder) {
4283 intel_opregion_notify_encoder(encoder, false);
4f771f10 4284 encoder->disable(encoder);
8807e55b 4285 }
4f771f10 4286
8664281b
PZ
4287 if (intel_crtc->config.has_pch_encoder)
4288 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
575f7ab7 4289 intel_disable_pipe(intel_crtc);
4f771f10 4290
ad80a810 4291 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4292
3f8dce3a 4293 ironlake_pfit_disable(intel_crtc);
4f771f10 4294
1f544388 4295 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4296
88adfff1 4297 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4298 lpt_disable_pch_transcoder(dev_priv);
8664281b 4299 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4300 intel_ddi_fdi_disable(crtc);
83616634 4301 }
4f771f10 4302
97b040aa
ID
4303 for_each_encoder_on_crtc(dev, crtc, encoder)
4304 if (encoder->post_disable)
4305 encoder->post_disable(encoder);
4306
4f771f10 4307 intel_crtc->active = false;
46ba614c 4308 intel_update_watermarks(crtc);
4f771f10
PZ
4309
4310 mutex_lock(&dev->struct_mutex);
4311 intel_update_fbc(dev);
4312 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4313
4314 if (intel_crtc_to_shared_dpll(intel_crtc))
4315 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4316}
4317
ee7b9f93
JB
4318static void ironlake_crtc_off(struct drm_crtc *crtc)
4319{
4320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4321 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4322}
4323
6441ab5f 4324
2dd24552
JB
4325static void i9xx_pfit_enable(struct intel_crtc *crtc)
4326{
4327 struct drm_device *dev = crtc->base.dev;
4328 struct drm_i915_private *dev_priv = dev->dev_private;
4329 struct intel_crtc_config *pipe_config = &crtc->config;
4330
328d8e82 4331 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4332 return;
4333
2dd24552 4334 /*
c0b03411
DV
4335 * The panel fitter should only be adjusted whilst the pipe is disabled,
4336 * according to register description and PRM.
2dd24552 4337 */
c0b03411
DV
4338 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4339 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4340
b074cec8
JB
4341 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4342 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4343
4344 /* Border color in case we don't scale up to the full screen. Black by
4345 * default, change to something else for debugging. */
4346 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4347}
4348
d05410f9
DA
4349static enum intel_display_power_domain port_to_power_domain(enum port port)
4350{
4351 switch (port) {
4352 case PORT_A:
4353 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4354 case PORT_B:
4355 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4356 case PORT_C:
4357 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4358 case PORT_D:
4359 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4360 default:
4361 WARN_ON_ONCE(1);
4362 return POWER_DOMAIN_PORT_OTHER;
4363 }
4364}
4365
77d22dca
ID
4366#define for_each_power_domain(domain, mask) \
4367 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4368 if ((1 << (domain)) & (mask))
4369
319be8ae
ID
4370enum intel_display_power_domain
4371intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4372{
4373 struct drm_device *dev = intel_encoder->base.dev;
4374 struct intel_digital_port *intel_dig_port;
4375
4376 switch (intel_encoder->type) {
4377 case INTEL_OUTPUT_UNKNOWN:
4378 /* Only DDI platforms should ever use this output type */
4379 WARN_ON_ONCE(!HAS_DDI(dev));
4380 case INTEL_OUTPUT_DISPLAYPORT:
4381 case INTEL_OUTPUT_HDMI:
4382 case INTEL_OUTPUT_EDP:
4383 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4384 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4385 case INTEL_OUTPUT_DP_MST:
4386 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4387 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4388 case INTEL_OUTPUT_ANALOG:
4389 return POWER_DOMAIN_PORT_CRT;
4390 case INTEL_OUTPUT_DSI:
4391 return POWER_DOMAIN_PORT_DSI;
4392 default:
4393 return POWER_DOMAIN_PORT_OTHER;
4394 }
4395}
4396
4397static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4398{
319be8ae
ID
4399 struct drm_device *dev = crtc->dev;
4400 struct intel_encoder *intel_encoder;
4401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4402 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4403 unsigned long mask;
4404 enum transcoder transcoder;
4405
4406 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4407
4408 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4409 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4410 if (intel_crtc->config.pch_pfit.enabled ||
4411 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4412 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4413
319be8ae
ID
4414 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4415 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4416
77d22dca
ID
4417 return mask;
4418}
4419
4420void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4421 bool enable)
4422{
4423 if (dev_priv->power_domains.init_power_on == enable)
4424 return;
4425
4426 if (enable)
4427 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4428 else
4429 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4430
4431 dev_priv->power_domains.init_power_on = enable;
4432}
4433
4434static void modeset_update_crtc_power_domains(struct drm_device *dev)
4435{
4436 struct drm_i915_private *dev_priv = dev->dev_private;
4437 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4438 struct intel_crtc *crtc;
4439
4440 /*
4441 * First get all needed power domains, then put all unneeded, to avoid
4442 * any unnecessary toggling of the power wells.
4443 */
d3fcc808 4444 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4445 enum intel_display_power_domain domain;
4446
4447 if (!crtc->base.enabled)
4448 continue;
4449
319be8ae 4450 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4451
4452 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4453 intel_display_power_get(dev_priv, domain);
4454 }
4455
d3fcc808 4456 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4457 enum intel_display_power_domain domain;
4458
4459 for_each_power_domain(domain, crtc->enabled_power_domains)
4460 intel_display_power_put(dev_priv, domain);
4461
4462 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4463 }
4464
4465 intel_display_set_init_power(dev_priv, false);
4466}
4467
dfcab17e 4468/* returns HPLL frequency in kHz */
f8bf63fd 4469static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4470{
586f49dc 4471 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4472
586f49dc
JB
4473 /* Obtain SKU information */
4474 mutex_lock(&dev_priv->dpio_lock);
4475 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4476 CCK_FUSE_HPLL_FREQ_MASK;
4477 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4478
dfcab17e 4479 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4480}
4481
f8bf63fd
VS
4482static void vlv_update_cdclk(struct drm_device *dev)
4483{
4484 struct drm_i915_private *dev_priv = dev->dev_private;
4485
4486 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4487 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4488 dev_priv->vlv_cdclk_freq);
4489
4490 /*
4491 * Program the gmbus_freq based on the cdclk frequency.
4492 * BSpec erroneously claims we should aim for 4MHz, but
4493 * in fact 1MHz is the correct frequency.
4494 */
4495 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4496}
4497
30a970c6
JB
4498/* Adjust CDclk dividers to allow high res or save power if possible */
4499static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4500{
4501 struct drm_i915_private *dev_priv = dev->dev_private;
4502 u32 val, cmd;
4503
d197b7d3 4504 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4505
dfcab17e 4506 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4507 cmd = 2;
dfcab17e 4508 else if (cdclk == 266667)
30a970c6
JB
4509 cmd = 1;
4510 else
4511 cmd = 0;
4512
4513 mutex_lock(&dev_priv->rps.hw_lock);
4514 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4515 val &= ~DSPFREQGUAR_MASK;
4516 val |= (cmd << DSPFREQGUAR_SHIFT);
4517 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4518 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4519 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4520 50)) {
4521 DRM_ERROR("timed out waiting for CDclk change\n");
4522 }
4523 mutex_unlock(&dev_priv->rps.hw_lock);
4524
dfcab17e 4525 if (cdclk == 400000) {
30a970c6
JB
4526 u32 divider, vco;
4527
4528 vco = valleyview_get_vco(dev_priv);
dfcab17e 4529 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4530
4531 mutex_lock(&dev_priv->dpio_lock);
4532 /* adjust cdclk divider */
4533 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4534 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4535 val |= divider;
4536 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4537
4538 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4539 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4540 50))
4541 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4542 mutex_unlock(&dev_priv->dpio_lock);
4543 }
4544
4545 mutex_lock(&dev_priv->dpio_lock);
4546 /* adjust self-refresh exit latency value */
4547 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4548 val &= ~0x7f;
4549
4550 /*
4551 * For high bandwidth configs, we set a higher latency in the bunit
4552 * so that the core display fetch happens in time to avoid underruns.
4553 */
dfcab17e 4554 if (cdclk == 400000)
30a970c6
JB
4555 val |= 4500 / 250; /* 4.5 usec */
4556 else
4557 val |= 3000 / 250; /* 3.0 usec */
4558 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4559 mutex_unlock(&dev_priv->dpio_lock);
4560
f8bf63fd 4561 vlv_update_cdclk(dev);
30a970c6
JB
4562}
4563
383c5a6a
VS
4564static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 u32 val, cmd;
4568
4569 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4570
4571 switch (cdclk) {
4572 case 400000:
4573 cmd = 3;
4574 break;
4575 case 333333:
4576 case 320000:
4577 cmd = 2;
4578 break;
4579 case 266667:
4580 cmd = 1;
4581 break;
4582 case 200000:
4583 cmd = 0;
4584 break;
4585 default:
4586 WARN_ON(1);
4587 return;
4588 }
4589
4590 mutex_lock(&dev_priv->rps.hw_lock);
4591 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4592 val &= ~DSPFREQGUAR_MASK_CHV;
4593 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4594 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4595 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4596 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4597 50)) {
4598 DRM_ERROR("timed out waiting for CDclk change\n");
4599 }
4600 mutex_unlock(&dev_priv->rps.hw_lock);
4601
4602 vlv_update_cdclk(dev);
4603}
4604
30a970c6
JB
4605static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4606 int max_pixclk)
4607{
29dc7ef3
VS
4608 int vco = valleyview_get_vco(dev_priv);
4609 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4610
d49a340d
VS
4611 /* FIXME: Punit isn't quite ready yet */
4612 if (IS_CHERRYVIEW(dev_priv->dev))
4613 return 400000;
4614
30a970c6
JB
4615 /*
4616 * Really only a few cases to deal with, as only 4 CDclks are supported:
4617 * 200MHz
4618 * 267MHz
29dc7ef3 4619 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4620 * 400MHz
4621 * So we check to see whether we're above 90% of the lower bin and
4622 * adjust if needed.
e37c67a1
VS
4623 *
4624 * We seem to get an unstable or solid color picture at 200MHz.
4625 * Not sure what's wrong. For now use 200MHz only when all pipes
4626 * are off.
30a970c6 4627 */
29dc7ef3 4628 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4629 return 400000;
4630 else if (max_pixclk > 266667*9/10)
29dc7ef3 4631 return freq_320;
e37c67a1 4632 else if (max_pixclk > 0)
dfcab17e 4633 return 266667;
e37c67a1
VS
4634 else
4635 return 200000;
30a970c6
JB
4636}
4637
2f2d7aa1
VS
4638/* compute the max pixel clock for new configuration */
4639static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4640{
4641 struct drm_device *dev = dev_priv->dev;
4642 struct intel_crtc *intel_crtc;
4643 int max_pixclk = 0;
4644
d3fcc808 4645 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4646 if (intel_crtc->new_enabled)
30a970c6 4647 max_pixclk = max(max_pixclk,
2f2d7aa1 4648 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4649 }
4650
4651 return max_pixclk;
4652}
4653
4654static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4655 unsigned *prepare_pipes)
30a970c6
JB
4656{
4657 struct drm_i915_private *dev_priv = dev->dev_private;
4658 struct intel_crtc *intel_crtc;
2f2d7aa1 4659 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4660
d60c4473
ID
4661 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4662 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4663 return;
4664
2f2d7aa1 4665 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4666 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4667 if (intel_crtc->base.enabled)
4668 *prepare_pipes |= (1 << intel_crtc->pipe);
4669}
4670
4671static void valleyview_modeset_global_resources(struct drm_device *dev)
4672{
4673 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4674 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4675 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4676
383c5a6a
VS
4677 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4678 if (IS_CHERRYVIEW(dev))
4679 cherryview_set_cdclk(dev, req_cdclk);
4680 else
4681 valleyview_set_cdclk(dev, req_cdclk);
4682 }
4683
77961eb9 4684 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4685}
4686
89b667f8
JB
4687static void valleyview_crtc_enable(struct drm_crtc *crtc)
4688{
4689 struct drm_device *dev = crtc->dev;
89b667f8
JB
4690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4691 struct intel_encoder *encoder;
4692 int pipe = intel_crtc->pipe;
23538ef1 4693 bool is_dsi;
89b667f8
JB
4694
4695 WARN_ON(!crtc->enabled);
4696
4697 if (intel_crtc->active)
4698 return;
4699
8525a235
SK
4700 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4701
1ae0d137
VS
4702 if (!is_dsi) {
4703 if (IS_CHERRYVIEW(dev))
4704 chv_prepare_pll(intel_crtc);
4705 else
4706 vlv_prepare_pll(intel_crtc);
4707 }
bdd4b6a6 4708
5b18e57c
DV
4709 if (intel_crtc->config.has_dp_encoder)
4710 intel_dp_set_m_n(intel_crtc);
4711
4712 intel_set_pipe_timings(intel_crtc);
4713
5b18e57c
DV
4714 i9xx_set_pipeconf(intel_crtc);
4715
89b667f8 4716 intel_crtc->active = true;
89b667f8 4717
4a3436e8
VS
4718 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4719
89b667f8
JB
4720 for_each_encoder_on_crtc(dev, crtc, encoder)
4721 if (encoder->pre_pll_enable)
4722 encoder->pre_pll_enable(encoder);
4723
9d556c99
CML
4724 if (!is_dsi) {
4725 if (IS_CHERRYVIEW(dev))
4726 chv_enable_pll(intel_crtc);
4727 else
4728 vlv_enable_pll(intel_crtc);
4729 }
89b667f8
JB
4730
4731 for_each_encoder_on_crtc(dev, crtc, encoder)
4732 if (encoder->pre_enable)
4733 encoder->pre_enable(encoder);
4734
2dd24552
JB
4735 i9xx_pfit_enable(intel_crtc);
4736
63cbb074
VS
4737 intel_crtc_load_lut(crtc);
4738
f37fcc2a 4739 intel_update_watermarks(crtc);
e1fdc473 4740 intel_enable_pipe(intel_crtc);
be6a6f8e 4741
5004945f
JN
4742 for_each_encoder_on_crtc(dev, crtc, encoder)
4743 encoder->enable(encoder);
9ab0460b
VS
4744
4745 intel_crtc_enable_planes(crtc);
d40d9187 4746
56b80e1f
VS
4747 /* Underruns don't raise interrupts, so check manually. */
4748 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4749}
4750
f13c2ef3
DV
4751static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4752{
4753 struct drm_device *dev = crtc->base.dev;
4754 struct drm_i915_private *dev_priv = dev->dev_private;
4755
4756 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4757 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4758}
4759
0b8765c6 4760static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4761{
4762 struct drm_device *dev = crtc->dev;
79e53945 4763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4764 struct intel_encoder *encoder;
79e53945 4765 int pipe = intel_crtc->pipe;
79e53945 4766
08a48469
DV
4767 WARN_ON(!crtc->enabled);
4768
f7abfe8b
CW
4769 if (intel_crtc->active)
4770 return;
4771
f13c2ef3
DV
4772 i9xx_set_pll_dividers(intel_crtc);
4773
5b18e57c
DV
4774 if (intel_crtc->config.has_dp_encoder)
4775 intel_dp_set_m_n(intel_crtc);
4776
4777 intel_set_pipe_timings(intel_crtc);
4778
5b18e57c
DV
4779 i9xx_set_pipeconf(intel_crtc);
4780
f7abfe8b 4781 intel_crtc->active = true;
6b383a7f 4782
4a3436e8
VS
4783 if (!IS_GEN2(dev))
4784 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4785
9d6d9f19
MK
4786 for_each_encoder_on_crtc(dev, crtc, encoder)
4787 if (encoder->pre_enable)
4788 encoder->pre_enable(encoder);
4789
f6736a1a
DV
4790 i9xx_enable_pll(intel_crtc);
4791
2dd24552
JB
4792 i9xx_pfit_enable(intel_crtc);
4793
63cbb074
VS
4794 intel_crtc_load_lut(crtc);
4795
f37fcc2a 4796 intel_update_watermarks(crtc);
e1fdc473 4797 intel_enable_pipe(intel_crtc);
be6a6f8e 4798
fa5c73b1
DV
4799 for_each_encoder_on_crtc(dev, crtc, encoder)
4800 encoder->enable(encoder);
9ab0460b
VS
4801
4802 intel_crtc_enable_planes(crtc);
d40d9187 4803
4a3436e8
VS
4804 /*
4805 * Gen2 reports pipe underruns whenever all planes are disabled.
4806 * So don't enable underrun reporting before at least some planes
4807 * are enabled.
4808 * FIXME: Need to fix the logic to work when we turn off all planes
4809 * but leave the pipe running.
4810 */
4811 if (IS_GEN2(dev))
4812 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4813
56b80e1f
VS
4814 /* Underruns don't raise interrupts, so check manually. */
4815 i9xx_check_fifo_underruns(dev);
0b8765c6 4816}
79e53945 4817
87476d63
DV
4818static void i9xx_pfit_disable(struct intel_crtc *crtc)
4819{
4820 struct drm_device *dev = crtc->base.dev;
4821 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4822
328d8e82
DV
4823 if (!crtc->config.gmch_pfit.control)
4824 return;
87476d63 4825
328d8e82 4826 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4827
328d8e82
DV
4828 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4829 I915_READ(PFIT_CONTROL));
4830 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4831}
4832
0b8765c6
JB
4833static void i9xx_crtc_disable(struct drm_crtc *crtc)
4834{
4835 struct drm_device *dev = crtc->dev;
4836 struct drm_i915_private *dev_priv = dev->dev_private;
4837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4838 struct intel_encoder *encoder;
0b8765c6 4839 int pipe = intel_crtc->pipe;
ef9c3aee 4840
f7abfe8b
CW
4841 if (!intel_crtc->active)
4842 return;
4843
4a3436e8
VS
4844 /*
4845 * Gen2 reports pipe underruns whenever all planes are disabled.
4846 * So diasble underrun reporting before all the planes get disabled.
4847 * FIXME: Need to fix the logic to work when we turn off all planes
4848 * but leave the pipe running.
4849 */
4850 if (IS_GEN2(dev))
4851 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4852
564ed191
ID
4853 /*
4854 * Vblank time updates from the shadow to live plane control register
4855 * are blocked if the memory self-refresh mode is active at that
4856 * moment. So to make sure the plane gets truly disabled, disable
4857 * first the self-refresh mode. The self-refresh enable bit in turn
4858 * will be checked/applied by the HW only at the next frame start
4859 * event which is after the vblank start event, so we need to have a
4860 * wait-for-vblank between disabling the plane and the pipe.
4861 */
4862 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
4863 intel_crtc_disable_planes(crtc);
4864
ea9d758d
DV
4865 for_each_encoder_on_crtc(dev, crtc, encoder)
4866 encoder->disable(encoder);
4867
6304cd91
VS
4868 /*
4869 * On gen2 planes are double buffered but the pipe isn't, so we must
4870 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
4871 * We also need to wait on all gmch platforms because of the
4872 * self-refresh mode constraint explained above.
6304cd91 4873 */
564ed191 4874 intel_wait_for_vblank(dev, pipe);
6304cd91 4875
575f7ab7 4876 intel_disable_pipe(intel_crtc);
24a1f16d 4877
87476d63 4878 i9xx_pfit_disable(intel_crtc);
24a1f16d 4879
89b667f8
JB
4880 for_each_encoder_on_crtc(dev, crtc, encoder)
4881 if (encoder->post_disable)
4882 encoder->post_disable(encoder);
4883
076ed3b2
CML
4884 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4885 if (IS_CHERRYVIEW(dev))
4886 chv_disable_pll(dev_priv, pipe);
4887 else if (IS_VALLEYVIEW(dev))
4888 vlv_disable_pll(dev_priv, pipe);
4889 else
4890 i9xx_disable_pll(dev_priv, pipe);
4891 }
0b8765c6 4892
4a3436e8
VS
4893 if (!IS_GEN2(dev))
4894 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4895
f7abfe8b 4896 intel_crtc->active = false;
46ba614c 4897 intel_update_watermarks(crtc);
f37fcc2a 4898
efa9624e 4899 mutex_lock(&dev->struct_mutex);
6b383a7f 4900 intel_update_fbc(dev);
efa9624e 4901 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
4902}
4903
ee7b9f93
JB
4904static void i9xx_crtc_off(struct drm_crtc *crtc)
4905{
4906}
4907
976f8a20
DV
4908static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4909 bool enabled)
2c07245f
ZW
4910{
4911 struct drm_device *dev = crtc->dev;
4912 struct drm_i915_master_private *master_priv;
4913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4914 int pipe = intel_crtc->pipe;
79e53945
JB
4915
4916 if (!dev->primary->master)
4917 return;
4918
4919 master_priv = dev->primary->master->driver_priv;
4920 if (!master_priv->sarea_priv)
4921 return;
4922
79e53945
JB
4923 switch (pipe) {
4924 case 0:
4925 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4926 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4927 break;
4928 case 1:
4929 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4930 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4931 break;
4932 default:
9db4a9c7 4933 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4934 break;
4935 }
79e53945
JB
4936}
4937
b04c5bd6
BF
4938/* Master function to enable/disable CRTC and corresponding power wells */
4939void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
4940{
4941 struct drm_device *dev = crtc->dev;
4942 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 4943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
4944 enum intel_display_power_domain domain;
4945 unsigned long domains;
976f8a20 4946
0e572fe7
DV
4947 if (enable) {
4948 if (!intel_crtc->active) {
e1e9fb84
DV
4949 domains = get_crtc_power_domains(crtc);
4950 for_each_power_domain(domain, domains)
4951 intel_display_power_get(dev_priv, domain);
4952 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
4953
4954 dev_priv->display.crtc_enable(crtc);
4955 }
4956 } else {
4957 if (intel_crtc->active) {
4958 dev_priv->display.crtc_disable(crtc);
4959
e1e9fb84
DV
4960 domains = intel_crtc->enabled_power_domains;
4961 for_each_power_domain(domain, domains)
4962 intel_display_power_put(dev_priv, domain);
4963 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
4964 }
4965 }
b04c5bd6
BF
4966}
4967
4968/**
4969 * Sets the power management mode of the pipe and plane.
4970 */
4971void intel_crtc_update_dpms(struct drm_crtc *crtc)
4972{
4973 struct drm_device *dev = crtc->dev;
4974 struct intel_encoder *intel_encoder;
4975 bool enable = false;
4976
4977 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4978 enable |= intel_encoder->connectors_active;
4979
4980 intel_crtc_control(crtc, enable);
976f8a20
DV
4981
4982 intel_crtc_update_sarea(crtc, enable);
4983}
4984
cdd59983
CW
4985static void intel_crtc_disable(struct drm_crtc *crtc)
4986{
cdd59983 4987 struct drm_device *dev = crtc->dev;
976f8a20 4988 struct drm_connector *connector;
ee7b9f93 4989 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 4990 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 4991 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 4992
976f8a20
DV
4993 /* crtc should still be enabled when we disable it. */
4994 WARN_ON(!crtc->enabled);
4995
4996 dev_priv->display.crtc_disable(crtc);
4997 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4998 dev_priv->display.off(crtc);
4999
f4510a27 5000 if (crtc->primary->fb) {
cdd59983 5001 mutex_lock(&dev->struct_mutex);
a071fa00
DV
5002 intel_unpin_fb_obj(old_obj);
5003 i915_gem_track_fb(old_obj, NULL,
5004 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 5005 mutex_unlock(&dev->struct_mutex);
f4510a27 5006 crtc->primary->fb = NULL;
976f8a20
DV
5007 }
5008
5009 /* Update computed state. */
5010 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5011 if (!connector->encoder || !connector->encoder->crtc)
5012 continue;
5013
5014 if (connector->encoder->crtc != crtc)
5015 continue;
5016
5017 connector->dpms = DRM_MODE_DPMS_OFF;
5018 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5019 }
5020}
5021
ea5b213a 5022void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5023{
4ef69c7a 5024 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5025
ea5b213a
CW
5026 drm_encoder_cleanup(encoder);
5027 kfree(intel_encoder);
7e7d76c3
JB
5028}
5029
9237329d 5030/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5031 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5032 * state of the entire output pipe. */
9237329d 5033static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5034{
5ab432ef
DV
5035 if (mode == DRM_MODE_DPMS_ON) {
5036 encoder->connectors_active = true;
5037
b2cabb0e 5038 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5039 } else {
5040 encoder->connectors_active = false;
5041
b2cabb0e 5042 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5043 }
79e53945
JB
5044}
5045
0a91ca29
DV
5046/* Cross check the actual hw state with our own modeset state tracking (and it's
5047 * internal consistency). */
b980514c 5048static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5049{
0a91ca29
DV
5050 if (connector->get_hw_state(connector)) {
5051 struct intel_encoder *encoder = connector->encoder;
5052 struct drm_crtc *crtc;
5053 bool encoder_enabled;
5054 enum pipe pipe;
5055
5056 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5057 connector->base.base.id,
c23cc417 5058 connector->base.name);
0a91ca29 5059
0e32b39c
DA
5060 /* there is no real hw state for MST connectors */
5061 if (connector->mst_port)
5062 return;
5063
0a91ca29
DV
5064 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5065 "wrong connector dpms state\n");
5066 WARN(connector->base.encoder != &encoder->base,
5067 "active connector not linked to encoder\n");
0a91ca29 5068
36cd7444
DA
5069 if (encoder) {
5070 WARN(!encoder->connectors_active,
5071 "encoder->connectors_active not set\n");
5072
5073 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5074 WARN(!encoder_enabled, "encoder not enabled\n");
5075 if (WARN_ON(!encoder->base.crtc))
5076 return;
0a91ca29 5077
36cd7444 5078 crtc = encoder->base.crtc;
0a91ca29 5079
36cd7444
DA
5080 WARN(!crtc->enabled, "crtc not enabled\n");
5081 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5082 WARN(pipe != to_intel_crtc(crtc)->pipe,
5083 "encoder active on the wrong pipe\n");
5084 }
0a91ca29 5085 }
79e53945
JB
5086}
5087
5ab432ef
DV
5088/* Even simpler default implementation, if there's really no special case to
5089 * consider. */
5090void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5091{
5ab432ef
DV
5092 /* All the simple cases only support two dpms states. */
5093 if (mode != DRM_MODE_DPMS_ON)
5094 mode = DRM_MODE_DPMS_OFF;
d4270e57 5095
5ab432ef
DV
5096 if (mode == connector->dpms)
5097 return;
5098
5099 connector->dpms = mode;
5100
5101 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5102 if (connector->encoder)
5103 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5104
b980514c 5105 intel_modeset_check_state(connector->dev);
79e53945
JB
5106}
5107
f0947c37
DV
5108/* Simple connector->get_hw_state implementation for encoders that support only
5109 * one connector and no cloning and hence the encoder state determines the state
5110 * of the connector. */
5111bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5112{
24929352 5113 enum pipe pipe = 0;
f0947c37 5114 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5115
f0947c37 5116 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5117}
5118
1857e1da
DV
5119static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5120 struct intel_crtc_config *pipe_config)
5121{
5122 struct drm_i915_private *dev_priv = dev->dev_private;
5123 struct intel_crtc *pipe_B_crtc =
5124 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5125
5126 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5127 pipe_name(pipe), pipe_config->fdi_lanes);
5128 if (pipe_config->fdi_lanes > 4) {
5129 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5130 pipe_name(pipe), pipe_config->fdi_lanes);
5131 return false;
5132 }
5133
bafb6553 5134 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5135 if (pipe_config->fdi_lanes > 2) {
5136 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5137 pipe_config->fdi_lanes);
5138 return false;
5139 } else {
5140 return true;
5141 }
5142 }
5143
5144 if (INTEL_INFO(dev)->num_pipes == 2)
5145 return true;
5146
5147 /* Ivybridge 3 pipe is really complicated */
5148 switch (pipe) {
5149 case PIPE_A:
5150 return true;
5151 case PIPE_B:
5152 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5153 pipe_config->fdi_lanes > 2) {
5154 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5155 pipe_name(pipe), pipe_config->fdi_lanes);
5156 return false;
5157 }
5158 return true;
5159 case PIPE_C:
1e833f40 5160 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5161 pipe_B_crtc->config.fdi_lanes <= 2) {
5162 if (pipe_config->fdi_lanes > 2) {
5163 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5164 pipe_name(pipe), pipe_config->fdi_lanes);
5165 return false;
5166 }
5167 } else {
5168 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5169 return false;
5170 }
5171 return true;
5172 default:
5173 BUG();
5174 }
5175}
5176
e29c22c0
DV
5177#define RETRY 1
5178static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5179 struct intel_crtc_config *pipe_config)
877d48d5 5180{
1857e1da 5181 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5182 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5183 int lane, link_bw, fdi_dotclock;
e29c22c0 5184 bool setup_ok, needs_recompute = false;
877d48d5 5185
e29c22c0 5186retry:
877d48d5
DV
5187 /* FDI is a binary signal running at ~2.7GHz, encoding
5188 * each output octet as 10 bits. The actual frequency
5189 * is stored as a divider into a 100MHz clock, and the
5190 * mode pixel clock is stored in units of 1KHz.
5191 * Hence the bw of each lane in terms of the mode signal
5192 * is:
5193 */
5194 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5195
241bfc38 5196 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5197
2bd89a07 5198 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5199 pipe_config->pipe_bpp);
5200
5201 pipe_config->fdi_lanes = lane;
5202
2bd89a07 5203 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5204 link_bw, &pipe_config->fdi_m_n);
1857e1da 5205
e29c22c0
DV
5206 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5207 intel_crtc->pipe, pipe_config);
5208 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5209 pipe_config->pipe_bpp -= 2*3;
5210 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5211 pipe_config->pipe_bpp);
5212 needs_recompute = true;
5213 pipe_config->bw_constrained = true;
5214
5215 goto retry;
5216 }
5217
5218 if (needs_recompute)
5219 return RETRY;
5220
5221 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5222}
5223
42db64ef
PZ
5224static void hsw_compute_ips_config(struct intel_crtc *crtc,
5225 struct intel_crtc_config *pipe_config)
5226{
d330a953 5227 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5228 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5229 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5230}
5231
a43f6e0f 5232static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5233 struct intel_crtc_config *pipe_config)
79e53945 5234{
a43f6e0f 5235 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5236 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5237
ad3a4479 5238 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5239 if (INTEL_INFO(dev)->gen < 4) {
5240 struct drm_i915_private *dev_priv = dev->dev_private;
5241 int clock_limit =
5242 dev_priv->display.get_display_clock_speed(dev);
5243
5244 /*
5245 * Enable pixel doubling when the dot clock
5246 * is > 90% of the (display) core speed.
5247 *
b397c96b
VS
5248 * GDG double wide on either pipe,
5249 * otherwise pipe A only.
cf532bb2 5250 */
b397c96b 5251 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5252 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5253 clock_limit *= 2;
cf532bb2 5254 pipe_config->double_wide = true;
ad3a4479
VS
5255 }
5256
241bfc38 5257 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5258 return -EINVAL;
2c07245f 5259 }
89749350 5260
1d1d0e27
VS
5261 /*
5262 * Pipe horizontal size must be even in:
5263 * - DVO ganged mode
5264 * - LVDS dual channel mode
5265 * - Double wide pipe
5266 */
5267 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5268 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5269 pipe_config->pipe_src_w &= ~1;
5270
8693a824
DL
5271 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5272 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5273 */
5274 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5275 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5276 return -EINVAL;
44f46b42 5277
bd080ee5 5278 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5279 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5280 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5281 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5282 * for lvds. */
5283 pipe_config->pipe_bpp = 8*3;
5284 }
5285
f5adf94e 5286 if (HAS_IPS(dev))
a43f6e0f
DV
5287 hsw_compute_ips_config(crtc, pipe_config);
5288
12030431
DV
5289 /*
5290 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5291 * old clock survives for now.
5292 */
5293 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5294 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5295
877d48d5 5296 if (pipe_config->has_pch_encoder)
a43f6e0f 5297 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5298
e29c22c0 5299 return 0;
79e53945
JB
5300}
5301
25eb05fc
JB
5302static int valleyview_get_display_clock_speed(struct drm_device *dev)
5303{
d197b7d3
VS
5304 struct drm_i915_private *dev_priv = dev->dev_private;
5305 int vco = valleyview_get_vco(dev_priv);
5306 u32 val;
5307 int divider;
5308
d49a340d
VS
5309 /* FIXME: Punit isn't quite ready yet */
5310 if (IS_CHERRYVIEW(dev))
5311 return 400000;
5312
d197b7d3
VS
5313 mutex_lock(&dev_priv->dpio_lock);
5314 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5315 mutex_unlock(&dev_priv->dpio_lock);
5316
5317 divider = val & DISPLAY_FREQUENCY_VALUES;
5318
7d007f40
VS
5319 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5320 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5321 "cdclk change in progress\n");
5322
d197b7d3 5323 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5324}
5325
e70236a8
JB
5326static int i945_get_display_clock_speed(struct drm_device *dev)
5327{
5328 return 400000;
5329}
79e53945 5330
e70236a8 5331static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5332{
e70236a8
JB
5333 return 333000;
5334}
79e53945 5335
e70236a8
JB
5336static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5337{
5338 return 200000;
5339}
79e53945 5340
257a7ffc
DV
5341static int pnv_get_display_clock_speed(struct drm_device *dev)
5342{
5343 u16 gcfgc = 0;
5344
5345 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5346
5347 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5348 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5349 return 267000;
5350 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5351 return 333000;
5352 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5353 return 444000;
5354 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5355 return 200000;
5356 default:
5357 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5358 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5359 return 133000;
5360 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5361 return 167000;
5362 }
5363}
5364
e70236a8
JB
5365static int i915gm_get_display_clock_speed(struct drm_device *dev)
5366{
5367 u16 gcfgc = 0;
79e53945 5368
e70236a8
JB
5369 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5370
5371 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5372 return 133000;
5373 else {
5374 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5375 case GC_DISPLAY_CLOCK_333_MHZ:
5376 return 333000;
5377 default:
5378 case GC_DISPLAY_CLOCK_190_200_MHZ:
5379 return 190000;
79e53945 5380 }
e70236a8
JB
5381 }
5382}
5383
5384static int i865_get_display_clock_speed(struct drm_device *dev)
5385{
5386 return 266000;
5387}
5388
5389static int i855_get_display_clock_speed(struct drm_device *dev)
5390{
5391 u16 hpllcc = 0;
5392 /* Assume that the hardware is in the high speed state. This
5393 * should be the default.
5394 */
5395 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5396 case GC_CLOCK_133_200:
5397 case GC_CLOCK_100_200:
5398 return 200000;
5399 case GC_CLOCK_166_250:
5400 return 250000;
5401 case GC_CLOCK_100_133:
79e53945 5402 return 133000;
e70236a8 5403 }
79e53945 5404
e70236a8
JB
5405 /* Shouldn't happen */
5406 return 0;
5407}
79e53945 5408
e70236a8
JB
5409static int i830_get_display_clock_speed(struct drm_device *dev)
5410{
5411 return 133000;
79e53945
JB
5412}
5413
2c07245f 5414static void
a65851af 5415intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5416{
a65851af
VS
5417 while (*num > DATA_LINK_M_N_MASK ||
5418 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5419 *num >>= 1;
5420 *den >>= 1;
5421 }
5422}
5423
a65851af
VS
5424static void compute_m_n(unsigned int m, unsigned int n,
5425 uint32_t *ret_m, uint32_t *ret_n)
5426{
5427 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5428 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5429 intel_reduce_m_n_ratio(ret_m, ret_n);
5430}
5431
e69d0bc1
DV
5432void
5433intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5434 int pixel_clock, int link_clock,
5435 struct intel_link_m_n *m_n)
2c07245f 5436{
e69d0bc1 5437 m_n->tu = 64;
a65851af
VS
5438
5439 compute_m_n(bits_per_pixel * pixel_clock,
5440 link_clock * nlanes * 8,
5441 &m_n->gmch_m, &m_n->gmch_n);
5442
5443 compute_m_n(pixel_clock, link_clock,
5444 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5445}
5446
a7615030
CW
5447static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5448{
d330a953
JN
5449 if (i915.panel_use_ssc >= 0)
5450 return i915.panel_use_ssc != 0;
41aa3448 5451 return dev_priv->vbt.lvds_use_ssc
435793df 5452 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5453}
5454
c65d77d8
JB
5455static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5456{
5457 struct drm_device *dev = crtc->dev;
5458 struct drm_i915_private *dev_priv = dev->dev_private;
5459 int refclk;
5460
a0c4da24 5461 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5462 refclk = 100000;
a0c4da24 5463 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5464 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5465 refclk = dev_priv->vbt.lvds_ssc_freq;
5466 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5467 } else if (!IS_GEN2(dev)) {
5468 refclk = 96000;
5469 } else {
5470 refclk = 48000;
5471 }
5472
5473 return refclk;
5474}
5475
7429e9d4 5476static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5477{
7df00d7a 5478 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5479}
f47709a9 5480
7429e9d4
DV
5481static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5482{
5483 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5484}
5485
f47709a9 5486static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5487 intel_clock_t *reduced_clock)
5488{
f47709a9 5489 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5490 u32 fp, fp2 = 0;
5491
5492 if (IS_PINEVIEW(dev)) {
7429e9d4 5493 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5494 if (reduced_clock)
7429e9d4 5495 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5496 } else {
7429e9d4 5497 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5498 if (reduced_clock)
7429e9d4 5499 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5500 }
5501
8bcc2795 5502 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5503
f47709a9
DV
5504 crtc->lowfreq_avail = false;
5505 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5506 reduced_clock && i915.powersave) {
8bcc2795 5507 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5508 crtc->lowfreq_avail = true;
a7516a05 5509 } else {
8bcc2795 5510 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5511 }
5512}
5513
5e69f97f
CML
5514static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5515 pipe)
89b667f8
JB
5516{
5517 u32 reg_val;
5518
5519 /*
5520 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5521 * and set it to a reasonable value instead.
5522 */
ab3c759a 5523 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5524 reg_val &= 0xffffff00;
5525 reg_val |= 0x00000030;
ab3c759a 5526 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5527
ab3c759a 5528 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5529 reg_val &= 0x8cffffff;
5530 reg_val = 0x8c000000;
ab3c759a 5531 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5532
ab3c759a 5533 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5534 reg_val &= 0xffffff00;
ab3c759a 5535 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5536
ab3c759a 5537 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5538 reg_val &= 0x00ffffff;
5539 reg_val |= 0xb0000000;
ab3c759a 5540 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5541}
5542
b551842d
DV
5543static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5544 struct intel_link_m_n *m_n)
5545{
5546 struct drm_device *dev = crtc->base.dev;
5547 struct drm_i915_private *dev_priv = dev->dev_private;
5548 int pipe = crtc->pipe;
5549
e3b95f1e
DV
5550 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5551 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5552 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5553 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5554}
5555
5556static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5557 struct intel_link_m_n *m_n,
5558 struct intel_link_m_n *m2_n2)
b551842d
DV
5559{
5560 struct drm_device *dev = crtc->base.dev;
5561 struct drm_i915_private *dev_priv = dev->dev_private;
5562 int pipe = crtc->pipe;
5563 enum transcoder transcoder = crtc->config.cpu_transcoder;
5564
5565 if (INTEL_INFO(dev)->gen >= 5) {
5566 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5567 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5568 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5569 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5570 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5571 * for gen < 8) and if DRRS is supported (to make sure the
5572 * registers are not unnecessarily accessed).
5573 */
5574 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5575 crtc->config.has_drrs) {
5576 I915_WRITE(PIPE_DATA_M2(transcoder),
5577 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5578 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5579 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5580 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5581 }
b551842d 5582 } else {
e3b95f1e
DV
5583 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5584 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5585 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5586 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5587 }
5588}
5589
f769cd24 5590void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5591{
5592 if (crtc->config.has_pch_encoder)
5593 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5594 else
f769cd24
VK
5595 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5596 &crtc->config.dp_m2_n2);
03afc4a2
DV
5597}
5598
f47709a9 5599static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5600{
5601 u32 dpll, dpll_md;
5602
5603 /*
5604 * Enable DPIO clock input. We should never disable the reference
5605 * clock for pipe B, since VGA hotplug / manual detection depends
5606 * on it.
5607 */
5608 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5609 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5610 /* We should never disable this, set it here for state tracking */
5611 if (crtc->pipe == PIPE_B)
5612 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5613 dpll |= DPLL_VCO_ENABLE;
5614 crtc->config.dpll_hw_state.dpll = dpll;
5615
5616 dpll_md = (crtc->config.pixel_multiplier - 1)
5617 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5618 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5619}
5620
5621static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5622{
f47709a9 5623 struct drm_device *dev = crtc->base.dev;
a0c4da24 5624 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5625 int pipe = crtc->pipe;
bdd4b6a6 5626 u32 mdiv;
a0c4da24 5627 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5628 u32 coreclk, reg_val;
a0c4da24 5629
09153000
DV
5630 mutex_lock(&dev_priv->dpio_lock);
5631
f47709a9
DV
5632 bestn = crtc->config.dpll.n;
5633 bestm1 = crtc->config.dpll.m1;
5634 bestm2 = crtc->config.dpll.m2;
5635 bestp1 = crtc->config.dpll.p1;
5636 bestp2 = crtc->config.dpll.p2;
a0c4da24 5637
89b667f8
JB
5638 /* See eDP HDMI DPIO driver vbios notes doc */
5639
5640 /* PLL B needs special handling */
bdd4b6a6 5641 if (pipe == PIPE_B)
5e69f97f 5642 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5643
5644 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5645 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5646
5647 /* Disable target IRef on PLL */
ab3c759a 5648 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5649 reg_val &= 0x00ffffff;
ab3c759a 5650 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5651
5652 /* Disable fast lock */
ab3c759a 5653 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5654
5655 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5656 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5657 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5658 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5659 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5660
5661 /*
5662 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5663 * but we don't support that).
5664 * Note: don't use the DAC post divider as it seems unstable.
5665 */
5666 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5667 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5668
a0c4da24 5669 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5670 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5671
89b667f8 5672 /* Set HBR and RBR LPF coefficients */
ff9a6750 5673 if (crtc->config.port_clock == 162000 ||
99750bd4 5674 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5675 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5676 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5677 0x009f0003);
89b667f8 5678 else
ab3c759a 5679 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5680 0x00d0000f);
5681
5682 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5683 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5684 /* Use SSC source */
bdd4b6a6 5685 if (pipe == PIPE_A)
ab3c759a 5686 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5687 0x0df40000);
5688 else
ab3c759a 5689 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5690 0x0df70000);
5691 } else { /* HDMI or VGA */
5692 /* Use bend source */
bdd4b6a6 5693 if (pipe == PIPE_A)
ab3c759a 5694 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5695 0x0df70000);
5696 else
ab3c759a 5697 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5698 0x0df40000);
5699 }
a0c4da24 5700
ab3c759a 5701 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5702 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5703 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5704 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5705 coreclk |= 0x01000000;
ab3c759a 5706 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5707
ab3c759a 5708 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5709 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5710}
5711
9d556c99 5712static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5713{
5714 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5715 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5716 DPLL_VCO_ENABLE;
5717 if (crtc->pipe != PIPE_A)
5718 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5719
5720 crtc->config.dpll_hw_state.dpll_md =
5721 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5722}
5723
5724static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5725{
5726 struct drm_device *dev = crtc->base.dev;
5727 struct drm_i915_private *dev_priv = dev->dev_private;
5728 int pipe = crtc->pipe;
5729 int dpll_reg = DPLL(crtc->pipe);
5730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5731 u32 loopfilter, intcoeff;
9d556c99
CML
5732 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5733 int refclk;
5734
9d556c99
CML
5735 bestn = crtc->config.dpll.n;
5736 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5737 bestm1 = crtc->config.dpll.m1;
5738 bestm2 = crtc->config.dpll.m2 >> 22;
5739 bestp1 = crtc->config.dpll.p1;
5740 bestp2 = crtc->config.dpll.p2;
5741
5742 /*
5743 * Enable Refclk and SSC
5744 */
a11b0703
VS
5745 I915_WRITE(dpll_reg,
5746 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5747
5748 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5749
9d556c99
CML
5750 /* p1 and p2 divider */
5751 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5752 5 << DPIO_CHV_S1_DIV_SHIFT |
5753 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5754 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5755 1 << DPIO_CHV_K_DIV_SHIFT);
5756
5757 /* Feedback post-divider - m2 */
5758 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5759
5760 /* Feedback refclk divider - n and m1 */
5761 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5762 DPIO_CHV_M1_DIV_BY_2 |
5763 1 << DPIO_CHV_N_DIV_SHIFT);
5764
5765 /* M2 fraction division */
5766 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5767
5768 /* M2 fraction division enable */
5769 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5770 DPIO_CHV_FRAC_DIV_EN |
5771 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5772
5773 /* Loop filter */
5774 refclk = i9xx_get_refclk(&crtc->base, 0);
5775 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5776 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5777 if (refclk == 100000)
5778 intcoeff = 11;
5779 else if (refclk == 38400)
5780 intcoeff = 10;
5781 else
5782 intcoeff = 9;
5783 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5784 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5785
5786 /* AFC Recal */
5787 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5788 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5789 DPIO_AFC_RECAL);
5790
5791 mutex_unlock(&dev_priv->dpio_lock);
5792}
5793
f47709a9
DV
5794static void i9xx_update_pll(struct intel_crtc *crtc,
5795 intel_clock_t *reduced_clock,
eb1cbe48
DV
5796 int num_connectors)
5797{
f47709a9 5798 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5799 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5800 u32 dpll;
5801 bool is_sdvo;
f47709a9 5802 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5803
f47709a9 5804 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5805
f47709a9
DV
5806 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5807 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5808
5809 dpll = DPLL_VGA_MODE_DIS;
5810
f47709a9 5811 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5812 dpll |= DPLLB_MODE_LVDS;
5813 else
5814 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5815
ef1b460d 5816 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5817 dpll |= (crtc->config.pixel_multiplier - 1)
5818 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5819 }
198a037f
DV
5820
5821 if (is_sdvo)
4a33e48d 5822 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5823
f47709a9 5824 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5825 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5826
5827 /* compute bitmask from p1 value */
5828 if (IS_PINEVIEW(dev))
5829 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5830 else {
5831 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5832 if (IS_G4X(dev) && reduced_clock)
5833 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5834 }
5835 switch (clock->p2) {
5836 case 5:
5837 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5838 break;
5839 case 7:
5840 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5841 break;
5842 case 10:
5843 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5844 break;
5845 case 14:
5846 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5847 break;
5848 }
5849 if (INTEL_INFO(dev)->gen >= 4)
5850 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5851
09ede541 5852 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5853 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5854 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5855 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5856 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5857 else
5858 dpll |= PLL_REF_INPUT_DREFCLK;
5859
5860 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5861 crtc->config.dpll_hw_state.dpll = dpll;
5862
eb1cbe48 5863 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5864 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5865 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5866 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5867 }
5868}
5869
f47709a9 5870static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5871 intel_clock_t *reduced_clock,
eb1cbe48
DV
5872 int num_connectors)
5873{
f47709a9 5874 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5875 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5876 u32 dpll;
f47709a9 5877 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5878
f47709a9 5879 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5880
eb1cbe48
DV
5881 dpll = DPLL_VGA_MODE_DIS;
5882
f47709a9 5883 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5884 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5885 } else {
5886 if (clock->p1 == 2)
5887 dpll |= PLL_P1_DIVIDE_BY_TWO;
5888 else
5889 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5890 if (clock->p2 == 4)
5891 dpll |= PLL_P2_DIVIDE_BY_4;
5892 }
5893
4a33e48d
DV
5894 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5895 dpll |= DPLL_DVO_2X_MODE;
5896
f47709a9 5897 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5898 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5899 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5900 else
5901 dpll |= PLL_REF_INPUT_DREFCLK;
5902
5903 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5904 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5905}
5906
8a654f3b 5907static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5908{
5909 struct drm_device *dev = intel_crtc->base.dev;
5910 struct drm_i915_private *dev_priv = dev->dev_private;
5911 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5912 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5913 struct drm_display_mode *adjusted_mode =
5914 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5915 uint32_t crtc_vtotal, crtc_vblank_end;
5916 int vsyncshift = 0;
4d8a62ea
DV
5917
5918 /* We need to be careful not to changed the adjusted mode, for otherwise
5919 * the hw state checker will get angry at the mismatch. */
5920 crtc_vtotal = adjusted_mode->crtc_vtotal;
5921 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5922
609aeaca 5923 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5924 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5925 crtc_vtotal -= 1;
5926 crtc_vblank_end -= 1;
609aeaca
VS
5927
5928 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5929 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5930 else
5931 vsyncshift = adjusted_mode->crtc_hsync_start -
5932 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5933 if (vsyncshift < 0)
5934 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5935 }
5936
5937 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5938 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5939
fe2b8f9d 5940 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5941 (adjusted_mode->crtc_hdisplay - 1) |
5942 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5943 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5944 (adjusted_mode->crtc_hblank_start - 1) |
5945 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5946 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5947 (adjusted_mode->crtc_hsync_start - 1) |
5948 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5949
fe2b8f9d 5950 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5951 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5952 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5953 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5954 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5955 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5956 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5957 (adjusted_mode->crtc_vsync_start - 1) |
5958 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5959
b5e508d4
PZ
5960 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5961 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5962 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5963 * bits. */
5964 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5965 (pipe == PIPE_B || pipe == PIPE_C))
5966 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5967
b0e77b9c
PZ
5968 /* pipesrc controls the size that is scaled from, which should
5969 * always be the user's requested size.
5970 */
5971 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5972 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5973 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5974}
5975
1bd1bd80
DV
5976static void intel_get_pipe_timings(struct intel_crtc *crtc,
5977 struct intel_crtc_config *pipe_config)
5978{
5979 struct drm_device *dev = crtc->base.dev;
5980 struct drm_i915_private *dev_priv = dev->dev_private;
5981 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5982 uint32_t tmp;
5983
5984 tmp = I915_READ(HTOTAL(cpu_transcoder));
5985 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5986 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5987 tmp = I915_READ(HBLANK(cpu_transcoder));
5988 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5989 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5990 tmp = I915_READ(HSYNC(cpu_transcoder));
5991 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5992 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5993
5994 tmp = I915_READ(VTOTAL(cpu_transcoder));
5995 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5996 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5997 tmp = I915_READ(VBLANK(cpu_transcoder));
5998 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5999 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6000 tmp = I915_READ(VSYNC(cpu_transcoder));
6001 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6002 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6003
6004 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6005 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6006 pipe_config->adjusted_mode.crtc_vtotal += 1;
6007 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6008 }
6009
6010 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6011 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6012 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6013
6014 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6015 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6016}
6017
f6a83288
DV
6018void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6019 struct intel_crtc_config *pipe_config)
babea61d 6020{
f6a83288
DV
6021 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6022 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6023 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6024 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6025
f6a83288
DV
6026 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6027 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6028 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6029 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6030
f6a83288 6031 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6032
f6a83288
DV
6033 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6034 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6035}
6036
84b046f3
DV
6037static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6038{
6039 struct drm_device *dev = intel_crtc->base.dev;
6040 struct drm_i915_private *dev_priv = dev->dev_private;
6041 uint32_t pipeconf;
6042
9f11a9e4 6043 pipeconf = 0;
84b046f3 6044
b6b5d049
VS
6045 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6046 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6047 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6048
cf532bb2
VS
6049 if (intel_crtc->config.double_wide)
6050 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6051
ff9ce46e
DV
6052 /* only g4x and later have fancy bpc/dither controls */
6053 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6054 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6055 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6056 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6057 PIPECONF_DITHER_TYPE_SP;
84b046f3 6058
ff9ce46e
DV
6059 switch (intel_crtc->config.pipe_bpp) {
6060 case 18:
6061 pipeconf |= PIPECONF_6BPC;
6062 break;
6063 case 24:
6064 pipeconf |= PIPECONF_8BPC;
6065 break;
6066 case 30:
6067 pipeconf |= PIPECONF_10BPC;
6068 break;
6069 default:
6070 /* Case prevented by intel_choose_pipe_bpp_dither. */
6071 BUG();
84b046f3
DV
6072 }
6073 }
6074
6075 if (HAS_PIPE_CXSR(dev)) {
6076 if (intel_crtc->lowfreq_avail) {
6077 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6078 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6079 } else {
6080 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6081 }
6082 }
6083
efc2cfff
VS
6084 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6085 if (INTEL_INFO(dev)->gen < 4 ||
6086 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6087 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6088 else
6089 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6090 } else
84b046f3
DV
6091 pipeconf |= PIPECONF_PROGRESSIVE;
6092
9f11a9e4
DV
6093 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6094 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6095
84b046f3
DV
6096 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6097 POSTING_READ(PIPECONF(intel_crtc->pipe));
6098}
6099
f564048e 6100static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6101 int x, int y,
94352cf9 6102 struct drm_framebuffer *fb)
79e53945
JB
6103{
6104 struct drm_device *dev = crtc->dev;
6105 struct drm_i915_private *dev_priv = dev->dev_private;
6106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6107 int refclk, num_connectors = 0;
652c393a 6108 intel_clock_t clock, reduced_clock;
a16af721 6109 bool ok, has_reduced_clock = false;
e9fd1c02 6110 bool is_lvds = false, is_dsi = false;
5eddb70b 6111 struct intel_encoder *encoder;
d4906093 6112 const intel_limit_t *limit;
79e53945 6113
6c2b7c12 6114 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6115 switch (encoder->type) {
79e53945
JB
6116 case INTEL_OUTPUT_LVDS:
6117 is_lvds = true;
6118 break;
e9fd1c02
JN
6119 case INTEL_OUTPUT_DSI:
6120 is_dsi = true;
6121 break;
79e53945 6122 }
43565a06 6123
c751ce4f 6124 num_connectors++;
79e53945
JB
6125 }
6126
f2335330 6127 if (is_dsi)
5b18e57c 6128 return 0;
f2335330
JN
6129
6130 if (!intel_crtc->config.clock_set) {
6131 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6132
e9fd1c02
JN
6133 /*
6134 * Returns a set of divisors for the desired target clock with
6135 * the given refclk, or FALSE. The returned values represent
6136 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6137 * 2) / p1 / p2.
6138 */
6139 limit = intel_limit(crtc, refclk);
6140 ok = dev_priv->display.find_dpll(limit, crtc,
6141 intel_crtc->config.port_clock,
6142 refclk, NULL, &clock);
f2335330 6143 if (!ok) {
e9fd1c02
JN
6144 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6145 return -EINVAL;
6146 }
79e53945 6147
f2335330
JN
6148 if (is_lvds && dev_priv->lvds_downclock_avail) {
6149 /*
6150 * Ensure we match the reduced clock's P to the target
6151 * clock. If the clocks don't match, we can't switch
6152 * the display clock by using the FP0/FP1. In such case
6153 * we will disable the LVDS downclock feature.
6154 */
6155 has_reduced_clock =
6156 dev_priv->display.find_dpll(limit, crtc,
6157 dev_priv->lvds_downclock,
6158 refclk, &clock,
6159 &reduced_clock);
6160 }
6161 /* Compat-code for transition, will disappear. */
f47709a9
DV
6162 intel_crtc->config.dpll.n = clock.n;
6163 intel_crtc->config.dpll.m1 = clock.m1;
6164 intel_crtc->config.dpll.m2 = clock.m2;
6165 intel_crtc->config.dpll.p1 = clock.p1;
6166 intel_crtc->config.dpll.p2 = clock.p2;
6167 }
7026d4ac 6168
e9fd1c02 6169 if (IS_GEN2(dev)) {
8a654f3b 6170 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6171 has_reduced_clock ? &reduced_clock : NULL,
6172 num_connectors);
9d556c99
CML
6173 } else if (IS_CHERRYVIEW(dev)) {
6174 chv_update_pll(intel_crtc);
e9fd1c02 6175 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6176 vlv_update_pll(intel_crtc);
e9fd1c02 6177 } else {
f47709a9 6178 i9xx_update_pll(intel_crtc,
eb1cbe48 6179 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6180 num_connectors);
e9fd1c02 6181 }
79e53945 6182
c8f7a0db 6183 return 0;
f564048e
EA
6184}
6185
2fa2fe9a
DV
6186static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6187 struct intel_crtc_config *pipe_config)
6188{
6189 struct drm_device *dev = crtc->base.dev;
6190 struct drm_i915_private *dev_priv = dev->dev_private;
6191 uint32_t tmp;
6192
dc9e7dec
VS
6193 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6194 return;
6195
2fa2fe9a 6196 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6197 if (!(tmp & PFIT_ENABLE))
6198 return;
2fa2fe9a 6199
06922821 6200 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6201 if (INTEL_INFO(dev)->gen < 4) {
6202 if (crtc->pipe != PIPE_B)
6203 return;
2fa2fe9a
DV
6204 } else {
6205 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6206 return;
6207 }
6208
06922821 6209 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6210 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6211 if (INTEL_INFO(dev)->gen < 5)
6212 pipe_config->gmch_pfit.lvds_border_bits =
6213 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6214}
6215
acbec814
JB
6216static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6217 struct intel_crtc_config *pipe_config)
6218{
6219 struct drm_device *dev = crtc->base.dev;
6220 struct drm_i915_private *dev_priv = dev->dev_private;
6221 int pipe = pipe_config->cpu_transcoder;
6222 intel_clock_t clock;
6223 u32 mdiv;
662c6ecb 6224 int refclk = 100000;
acbec814 6225
f573de5a
SK
6226 /* In case of MIPI DPLL will not even be used */
6227 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6228 return;
6229
acbec814 6230 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6231 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6232 mutex_unlock(&dev_priv->dpio_lock);
6233
6234 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6235 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6236 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6237 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6238 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6239
f646628b 6240 vlv_clock(refclk, &clock);
acbec814 6241
f646628b
VS
6242 /* clock.dot is the fast clock */
6243 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6244}
6245
1ad292b5
JB
6246static void i9xx_get_plane_config(struct intel_crtc *crtc,
6247 struct intel_plane_config *plane_config)
6248{
6249 struct drm_device *dev = crtc->base.dev;
6250 struct drm_i915_private *dev_priv = dev->dev_private;
6251 u32 val, base, offset;
6252 int pipe = crtc->pipe, plane = crtc->plane;
6253 int fourcc, pixel_format;
6254 int aligned_height;
6255
66e514c1
DA
6256 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6257 if (!crtc->base.primary->fb) {
1ad292b5
JB
6258 DRM_DEBUG_KMS("failed to alloc fb\n");
6259 return;
6260 }
6261
6262 val = I915_READ(DSPCNTR(plane));
6263
6264 if (INTEL_INFO(dev)->gen >= 4)
6265 if (val & DISPPLANE_TILED)
6266 plane_config->tiled = true;
6267
6268 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6269 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6270 crtc->base.primary->fb->pixel_format = fourcc;
6271 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6272 drm_format_plane_cpp(fourcc, 0) * 8;
6273
6274 if (INTEL_INFO(dev)->gen >= 4) {
6275 if (plane_config->tiled)
6276 offset = I915_READ(DSPTILEOFF(plane));
6277 else
6278 offset = I915_READ(DSPLINOFF(plane));
6279 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6280 } else {
6281 base = I915_READ(DSPADDR(plane));
6282 }
6283 plane_config->base = base;
6284
6285 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6286 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6287 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6288
6289 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6290 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6291
66e514c1 6292 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6293 plane_config->tiled);
6294
1267a26b
FF
6295 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6296 aligned_height);
1ad292b5
JB
6297
6298 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6299 pipe, plane, crtc->base.primary->fb->width,
6300 crtc->base.primary->fb->height,
6301 crtc->base.primary->fb->bits_per_pixel, base,
6302 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6303 plane_config->size);
6304
6305}
6306
70b23a98
VS
6307static void chv_crtc_clock_get(struct intel_crtc *crtc,
6308 struct intel_crtc_config *pipe_config)
6309{
6310 struct drm_device *dev = crtc->base.dev;
6311 struct drm_i915_private *dev_priv = dev->dev_private;
6312 int pipe = pipe_config->cpu_transcoder;
6313 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6314 intel_clock_t clock;
6315 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6316 int refclk = 100000;
6317
6318 mutex_lock(&dev_priv->dpio_lock);
6319 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6320 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6321 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6322 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6323 mutex_unlock(&dev_priv->dpio_lock);
6324
6325 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6326 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6327 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6328 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6329 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6330
6331 chv_clock(refclk, &clock);
6332
6333 /* clock.dot is the fast clock */
6334 pipe_config->port_clock = clock.dot / 5;
6335}
6336
0e8ffe1b
DV
6337static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6338 struct intel_crtc_config *pipe_config)
6339{
6340 struct drm_device *dev = crtc->base.dev;
6341 struct drm_i915_private *dev_priv = dev->dev_private;
6342 uint32_t tmp;
6343
b5482bd0
ID
6344 if (!intel_display_power_enabled(dev_priv,
6345 POWER_DOMAIN_PIPE(crtc->pipe)))
6346 return false;
6347
e143a21c 6348 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6349 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6350
0e8ffe1b
DV
6351 tmp = I915_READ(PIPECONF(crtc->pipe));
6352 if (!(tmp & PIPECONF_ENABLE))
6353 return false;
6354
42571aef
VS
6355 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6356 switch (tmp & PIPECONF_BPC_MASK) {
6357 case PIPECONF_6BPC:
6358 pipe_config->pipe_bpp = 18;
6359 break;
6360 case PIPECONF_8BPC:
6361 pipe_config->pipe_bpp = 24;
6362 break;
6363 case PIPECONF_10BPC:
6364 pipe_config->pipe_bpp = 30;
6365 break;
6366 default:
6367 break;
6368 }
6369 }
6370
b5a9fa09
DV
6371 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6372 pipe_config->limited_color_range = true;
6373
282740f7
VS
6374 if (INTEL_INFO(dev)->gen < 4)
6375 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6376
1bd1bd80
DV
6377 intel_get_pipe_timings(crtc, pipe_config);
6378
2fa2fe9a
DV
6379 i9xx_get_pfit_config(crtc, pipe_config);
6380
6c49f241
DV
6381 if (INTEL_INFO(dev)->gen >= 4) {
6382 tmp = I915_READ(DPLL_MD(crtc->pipe));
6383 pipe_config->pixel_multiplier =
6384 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6385 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6386 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6387 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6388 tmp = I915_READ(DPLL(crtc->pipe));
6389 pipe_config->pixel_multiplier =
6390 ((tmp & SDVO_MULTIPLIER_MASK)
6391 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6392 } else {
6393 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6394 * port and will be fixed up in the encoder->get_config
6395 * function. */
6396 pipe_config->pixel_multiplier = 1;
6397 }
8bcc2795
DV
6398 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6399 if (!IS_VALLEYVIEW(dev)) {
6400 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6401 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6402 } else {
6403 /* Mask out read-only status bits. */
6404 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6405 DPLL_PORTC_READY_MASK |
6406 DPLL_PORTB_READY_MASK);
8bcc2795 6407 }
6c49f241 6408
70b23a98
VS
6409 if (IS_CHERRYVIEW(dev))
6410 chv_crtc_clock_get(crtc, pipe_config);
6411 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6412 vlv_crtc_clock_get(crtc, pipe_config);
6413 else
6414 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6415
0e8ffe1b
DV
6416 return true;
6417}
6418
dde86e2d 6419static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6420{
6421 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6422 struct intel_encoder *encoder;
74cfd7ac 6423 u32 val, final;
13d83a67 6424 bool has_lvds = false;
199e5d79 6425 bool has_cpu_edp = false;
199e5d79 6426 bool has_panel = false;
99eb6a01
KP
6427 bool has_ck505 = false;
6428 bool can_ssc = false;
13d83a67
JB
6429
6430 /* We need to take the global config into account */
b2784e15 6431 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6432 switch (encoder->type) {
6433 case INTEL_OUTPUT_LVDS:
6434 has_panel = true;
6435 has_lvds = true;
6436 break;
6437 case INTEL_OUTPUT_EDP:
6438 has_panel = true;
2de6905f 6439 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6440 has_cpu_edp = true;
6441 break;
13d83a67
JB
6442 }
6443 }
6444
99eb6a01 6445 if (HAS_PCH_IBX(dev)) {
41aa3448 6446 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6447 can_ssc = has_ck505;
6448 } else {
6449 has_ck505 = false;
6450 can_ssc = true;
6451 }
6452
2de6905f
ID
6453 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6454 has_panel, has_lvds, has_ck505);
13d83a67
JB
6455
6456 /* Ironlake: try to setup display ref clock before DPLL
6457 * enabling. This is only under driver's control after
6458 * PCH B stepping, previous chipset stepping should be
6459 * ignoring this setting.
6460 */
74cfd7ac
CW
6461 val = I915_READ(PCH_DREF_CONTROL);
6462
6463 /* As we must carefully and slowly disable/enable each source in turn,
6464 * compute the final state we want first and check if we need to
6465 * make any changes at all.
6466 */
6467 final = val;
6468 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6469 if (has_ck505)
6470 final |= DREF_NONSPREAD_CK505_ENABLE;
6471 else
6472 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6473
6474 final &= ~DREF_SSC_SOURCE_MASK;
6475 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6476 final &= ~DREF_SSC1_ENABLE;
6477
6478 if (has_panel) {
6479 final |= DREF_SSC_SOURCE_ENABLE;
6480
6481 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6482 final |= DREF_SSC1_ENABLE;
6483
6484 if (has_cpu_edp) {
6485 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6486 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6487 else
6488 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6489 } else
6490 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6491 } else {
6492 final |= DREF_SSC_SOURCE_DISABLE;
6493 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6494 }
6495
6496 if (final == val)
6497 return;
6498
13d83a67 6499 /* Always enable nonspread source */
74cfd7ac 6500 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6501
99eb6a01 6502 if (has_ck505)
74cfd7ac 6503 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6504 else
74cfd7ac 6505 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6506
199e5d79 6507 if (has_panel) {
74cfd7ac
CW
6508 val &= ~DREF_SSC_SOURCE_MASK;
6509 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6510
199e5d79 6511 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6512 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6513 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6514 val |= DREF_SSC1_ENABLE;
e77166b5 6515 } else
74cfd7ac 6516 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6517
6518 /* Get SSC going before enabling the outputs */
74cfd7ac 6519 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6520 POSTING_READ(PCH_DREF_CONTROL);
6521 udelay(200);
6522
74cfd7ac 6523 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6524
6525 /* Enable CPU source on CPU attached eDP */
199e5d79 6526 if (has_cpu_edp) {
99eb6a01 6527 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6528 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6529 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6530 } else
74cfd7ac 6531 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6532 } else
74cfd7ac 6533 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6534
74cfd7ac 6535 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6536 POSTING_READ(PCH_DREF_CONTROL);
6537 udelay(200);
6538 } else {
6539 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6540
74cfd7ac 6541 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6542
6543 /* Turn off CPU output */
74cfd7ac 6544 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6545
74cfd7ac 6546 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6547 POSTING_READ(PCH_DREF_CONTROL);
6548 udelay(200);
6549
6550 /* Turn off the SSC source */
74cfd7ac
CW
6551 val &= ~DREF_SSC_SOURCE_MASK;
6552 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6553
6554 /* Turn off SSC1 */
74cfd7ac 6555 val &= ~DREF_SSC1_ENABLE;
199e5d79 6556
74cfd7ac 6557 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6558 POSTING_READ(PCH_DREF_CONTROL);
6559 udelay(200);
6560 }
74cfd7ac
CW
6561
6562 BUG_ON(val != final);
13d83a67
JB
6563}
6564
f31f2d55 6565static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6566{
f31f2d55 6567 uint32_t tmp;
dde86e2d 6568
0ff066a9
PZ
6569 tmp = I915_READ(SOUTH_CHICKEN2);
6570 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6571 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6572
0ff066a9
PZ
6573 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6574 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6575 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6576
0ff066a9
PZ
6577 tmp = I915_READ(SOUTH_CHICKEN2);
6578 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6579 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6580
0ff066a9
PZ
6581 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6582 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6583 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6584}
6585
6586/* WaMPhyProgramming:hsw */
6587static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6588{
6589 uint32_t tmp;
dde86e2d
PZ
6590
6591 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6592 tmp &= ~(0xFF << 24);
6593 tmp |= (0x12 << 24);
6594 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6595
dde86e2d
PZ
6596 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6597 tmp |= (1 << 11);
6598 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6599
6600 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6601 tmp |= (1 << 11);
6602 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6603
dde86e2d
PZ
6604 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6605 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6606 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6607
6608 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6609 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6610 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6611
0ff066a9
PZ
6612 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6613 tmp &= ~(7 << 13);
6614 tmp |= (5 << 13);
6615 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6616
0ff066a9
PZ
6617 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6618 tmp &= ~(7 << 13);
6619 tmp |= (5 << 13);
6620 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6621
6622 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6623 tmp &= ~0xFF;
6624 tmp |= 0x1C;
6625 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6626
6627 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6628 tmp &= ~0xFF;
6629 tmp |= 0x1C;
6630 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6631
6632 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6633 tmp &= ~(0xFF << 16);
6634 tmp |= (0x1C << 16);
6635 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6636
6637 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6638 tmp &= ~(0xFF << 16);
6639 tmp |= (0x1C << 16);
6640 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6641
0ff066a9
PZ
6642 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6643 tmp |= (1 << 27);
6644 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6645
0ff066a9
PZ
6646 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6647 tmp |= (1 << 27);
6648 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6649
0ff066a9
PZ
6650 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6651 tmp &= ~(0xF << 28);
6652 tmp |= (4 << 28);
6653 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6654
0ff066a9
PZ
6655 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6656 tmp &= ~(0xF << 28);
6657 tmp |= (4 << 28);
6658 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6659}
6660
2fa86a1f
PZ
6661/* Implements 3 different sequences from BSpec chapter "Display iCLK
6662 * Programming" based on the parameters passed:
6663 * - Sequence to enable CLKOUT_DP
6664 * - Sequence to enable CLKOUT_DP without spread
6665 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6666 */
6667static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6668 bool with_fdi)
f31f2d55
PZ
6669{
6670 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6671 uint32_t reg, tmp;
6672
6673 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6674 with_spread = true;
6675 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6676 with_fdi, "LP PCH doesn't have FDI\n"))
6677 with_fdi = false;
f31f2d55
PZ
6678
6679 mutex_lock(&dev_priv->dpio_lock);
6680
6681 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6682 tmp &= ~SBI_SSCCTL_DISABLE;
6683 tmp |= SBI_SSCCTL_PATHALT;
6684 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6685
6686 udelay(24);
6687
2fa86a1f
PZ
6688 if (with_spread) {
6689 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6690 tmp &= ~SBI_SSCCTL_PATHALT;
6691 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6692
2fa86a1f
PZ
6693 if (with_fdi) {
6694 lpt_reset_fdi_mphy(dev_priv);
6695 lpt_program_fdi_mphy(dev_priv);
6696 }
6697 }
dde86e2d 6698
2fa86a1f
PZ
6699 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6700 SBI_GEN0 : SBI_DBUFF0;
6701 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6702 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6703 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6704
6705 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6706}
6707
47701c3b
PZ
6708/* Sequence to disable CLKOUT_DP */
6709static void lpt_disable_clkout_dp(struct drm_device *dev)
6710{
6711 struct drm_i915_private *dev_priv = dev->dev_private;
6712 uint32_t reg, tmp;
6713
6714 mutex_lock(&dev_priv->dpio_lock);
6715
6716 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6717 SBI_GEN0 : SBI_DBUFF0;
6718 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6719 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6720 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6721
6722 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6723 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6724 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6725 tmp |= SBI_SSCCTL_PATHALT;
6726 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6727 udelay(32);
6728 }
6729 tmp |= SBI_SSCCTL_DISABLE;
6730 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6731 }
6732
6733 mutex_unlock(&dev_priv->dpio_lock);
6734}
6735
bf8fa3d3
PZ
6736static void lpt_init_pch_refclk(struct drm_device *dev)
6737{
bf8fa3d3
PZ
6738 struct intel_encoder *encoder;
6739 bool has_vga = false;
6740
b2784e15 6741 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6742 switch (encoder->type) {
6743 case INTEL_OUTPUT_ANALOG:
6744 has_vga = true;
6745 break;
6746 }
6747 }
6748
47701c3b
PZ
6749 if (has_vga)
6750 lpt_enable_clkout_dp(dev, true, true);
6751 else
6752 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6753}
6754
dde86e2d
PZ
6755/*
6756 * Initialize reference clocks when the driver loads
6757 */
6758void intel_init_pch_refclk(struct drm_device *dev)
6759{
6760 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6761 ironlake_init_pch_refclk(dev);
6762 else if (HAS_PCH_LPT(dev))
6763 lpt_init_pch_refclk(dev);
6764}
6765
d9d444cb
JB
6766static int ironlake_get_refclk(struct drm_crtc *crtc)
6767{
6768 struct drm_device *dev = crtc->dev;
6769 struct drm_i915_private *dev_priv = dev->dev_private;
6770 struct intel_encoder *encoder;
d9d444cb
JB
6771 int num_connectors = 0;
6772 bool is_lvds = false;
6773
6c2b7c12 6774 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6775 switch (encoder->type) {
6776 case INTEL_OUTPUT_LVDS:
6777 is_lvds = true;
6778 break;
d9d444cb
JB
6779 }
6780 num_connectors++;
6781 }
6782
6783 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6784 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6785 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6786 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6787 }
6788
6789 return 120000;
6790}
6791
6ff93609 6792static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6793{
c8203565 6794 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6796 int pipe = intel_crtc->pipe;
c8203565
PZ
6797 uint32_t val;
6798
78114071 6799 val = 0;
c8203565 6800
965e0c48 6801 switch (intel_crtc->config.pipe_bpp) {
c8203565 6802 case 18:
dfd07d72 6803 val |= PIPECONF_6BPC;
c8203565
PZ
6804 break;
6805 case 24:
dfd07d72 6806 val |= PIPECONF_8BPC;
c8203565
PZ
6807 break;
6808 case 30:
dfd07d72 6809 val |= PIPECONF_10BPC;
c8203565
PZ
6810 break;
6811 case 36:
dfd07d72 6812 val |= PIPECONF_12BPC;
c8203565
PZ
6813 break;
6814 default:
cc769b62
PZ
6815 /* Case prevented by intel_choose_pipe_bpp_dither. */
6816 BUG();
c8203565
PZ
6817 }
6818
d8b32247 6819 if (intel_crtc->config.dither)
c8203565
PZ
6820 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6821
6ff93609 6822 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6823 val |= PIPECONF_INTERLACED_ILK;
6824 else
6825 val |= PIPECONF_PROGRESSIVE;
6826
50f3b016 6827 if (intel_crtc->config.limited_color_range)
3685a8f3 6828 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6829
c8203565
PZ
6830 I915_WRITE(PIPECONF(pipe), val);
6831 POSTING_READ(PIPECONF(pipe));
6832}
6833
86d3efce
VS
6834/*
6835 * Set up the pipe CSC unit.
6836 *
6837 * Currently only full range RGB to limited range RGB conversion
6838 * is supported, but eventually this should handle various
6839 * RGB<->YCbCr scenarios as well.
6840 */
50f3b016 6841static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6842{
6843 struct drm_device *dev = crtc->dev;
6844 struct drm_i915_private *dev_priv = dev->dev_private;
6845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6846 int pipe = intel_crtc->pipe;
6847 uint16_t coeff = 0x7800; /* 1.0 */
6848
6849 /*
6850 * TODO: Check what kind of values actually come out of the pipe
6851 * with these coeff/postoff values and adjust to get the best
6852 * accuracy. Perhaps we even need to take the bpc value into
6853 * consideration.
6854 */
6855
50f3b016 6856 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6857 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6858
6859 /*
6860 * GY/GU and RY/RU should be the other way around according
6861 * to BSpec, but reality doesn't agree. Just set them up in
6862 * a way that results in the correct picture.
6863 */
6864 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6865 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6866
6867 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6868 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6869
6870 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6871 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6872
6873 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6874 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6875 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6876
6877 if (INTEL_INFO(dev)->gen > 6) {
6878 uint16_t postoff = 0;
6879
50f3b016 6880 if (intel_crtc->config.limited_color_range)
32cf0cb0 6881 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6882
6883 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6884 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6885 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6886
6887 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6888 } else {
6889 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6890
50f3b016 6891 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6892 mode |= CSC_BLACK_SCREEN_OFFSET;
6893
6894 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6895 }
6896}
6897
6ff93609 6898static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6899{
756f85cf
PZ
6900 struct drm_device *dev = crtc->dev;
6901 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6903 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6904 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6905 uint32_t val;
6906
3eff4faa 6907 val = 0;
ee2b0b38 6908
756f85cf 6909 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6910 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6911
6ff93609 6912 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6913 val |= PIPECONF_INTERLACED_ILK;
6914 else
6915 val |= PIPECONF_PROGRESSIVE;
6916
702e7a56
PZ
6917 I915_WRITE(PIPECONF(cpu_transcoder), val);
6918 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6919
6920 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6921 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6922
6923 if (IS_BROADWELL(dev)) {
6924 val = 0;
6925
6926 switch (intel_crtc->config.pipe_bpp) {
6927 case 18:
6928 val |= PIPEMISC_DITHER_6_BPC;
6929 break;
6930 case 24:
6931 val |= PIPEMISC_DITHER_8_BPC;
6932 break;
6933 case 30:
6934 val |= PIPEMISC_DITHER_10_BPC;
6935 break;
6936 case 36:
6937 val |= PIPEMISC_DITHER_12_BPC;
6938 break;
6939 default:
6940 /* Case prevented by pipe_config_set_bpp. */
6941 BUG();
6942 }
6943
6944 if (intel_crtc->config.dither)
6945 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6946
6947 I915_WRITE(PIPEMISC(pipe), val);
6948 }
ee2b0b38
PZ
6949}
6950
6591c6e4 6951static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6952 intel_clock_t *clock,
6953 bool *has_reduced_clock,
6954 intel_clock_t *reduced_clock)
6955{
6956 struct drm_device *dev = crtc->dev;
6957 struct drm_i915_private *dev_priv = dev->dev_private;
6958 struct intel_encoder *intel_encoder;
6959 int refclk;
d4906093 6960 const intel_limit_t *limit;
a16af721 6961 bool ret, is_lvds = false;
79e53945 6962
6591c6e4
PZ
6963 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6964 switch (intel_encoder->type) {
79e53945
JB
6965 case INTEL_OUTPUT_LVDS:
6966 is_lvds = true;
6967 break;
79e53945
JB
6968 }
6969 }
6970
d9d444cb 6971 refclk = ironlake_get_refclk(crtc);
79e53945 6972
d4906093
ML
6973 /*
6974 * Returns a set of divisors for the desired target clock with the given
6975 * refclk, or FALSE. The returned values represent the clock equation:
6976 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6977 */
1b894b59 6978 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6979 ret = dev_priv->display.find_dpll(limit, crtc,
6980 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6981 refclk, NULL, clock);
6591c6e4
PZ
6982 if (!ret)
6983 return false;
cda4b7d3 6984
ddc9003c 6985 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6986 /*
6987 * Ensure we match the reduced clock's P to the target clock.
6988 * If the clocks don't match, we can't switch the display clock
6989 * by using the FP0/FP1. In such case we will disable the LVDS
6990 * downclock feature.
6991 */
ee9300bb
DV
6992 *has_reduced_clock =
6993 dev_priv->display.find_dpll(limit, crtc,
6994 dev_priv->lvds_downclock,
6995 refclk, clock,
6996 reduced_clock);
652c393a 6997 }
61e9653f 6998
6591c6e4
PZ
6999 return true;
7000}
7001
d4b1931c
PZ
7002int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7003{
7004 /*
7005 * Account for spread spectrum to avoid
7006 * oversubscribing the link. Max center spread
7007 * is 2.5%; use 5% for safety's sake.
7008 */
7009 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7010 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7011}
7012
7429e9d4 7013static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7014{
7429e9d4 7015 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7016}
7017
de13a2e3 7018static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7019 u32 *fp,
9a7c7890 7020 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7021{
de13a2e3 7022 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7023 struct drm_device *dev = crtc->dev;
7024 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7025 struct intel_encoder *intel_encoder;
7026 uint32_t dpll;
6cc5f341 7027 int factor, num_connectors = 0;
09ede541 7028 bool is_lvds = false, is_sdvo = false;
79e53945 7029
de13a2e3
PZ
7030 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7031 switch (intel_encoder->type) {
79e53945
JB
7032 case INTEL_OUTPUT_LVDS:
7033 is_lvds = true;
7034 break;
7035 case INTEL_OUTPUT_SDVO:
7d57382e 7036 case INTEL_OUTPUT_HDMI:
79e53945 7037 is_sdvo = true;
79e53945 7038 break;
79e53945 7039 }
43565a06 7040
c751ce4f 7041 num_connectors++;
79e53945 7042 }
79e53945 7043
c1858123 7044 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7045 factor = 21;
7046 if (is_lvds) {
7047 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7048 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7049 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7050 factor = 25;
09ede541 7051 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7052 factor = 20;
c1858123 7053
7429e9d4 7054 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7055 *fp |= FP_CB_TUNE;
2c07245f 7056
9a7c7890
DV
7057 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7058 *fp2 |= FP_CB_TUNE;
7059
5eddb70b 7060 dpll = 0;
2c07245f 7061
a07d6787
EA
7062 if (is_lvds)
7063 dpll |= DPLLB_MODE_LVDS;
7064 else
7065 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7066
ef1b460d
DV
7067 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7068 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7069
7070 if (is_sdvo)
4a33e48d 7071 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7072 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7073 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7074
a07d6787 7075 /* compute bitmask from p1 value */
7429e9d4 7076 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7077 /* also FPA1 */
7429e9d4 7078 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7079
7429e9d4 7080 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7081 case 5:
7082 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7083 break;
7084 case 7:
7085 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7086 break;
7087 case 10:
7088 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7089 break;
7090 case 14:
7091 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7092 break;
79e53945
JB
7093 }
7094
b4c09f3b 7095 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7096 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7097 else
7098 dpll |= PLL_REF_INPUT_DREFCLK;
7099
959e16d6 7100 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7101}
7102
7103static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7104 int x, int y,
7105 struct drm_framebuffer *fb)
7106{
7107 struct drm_device *dev = crtc->dev;
de13a2e3 7108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7109 int num_connectors = 0;
7110 intel_clock_t clock, reduced_clock;
cbbab5bd 7111 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7112 bool ok, has_reduced_clock = false;
8b47047b 7113 bool is_lvds = false;
de13a2e3 7114 struct intel_encoder *encoder;
e2b78267 7115 struct intel_shared_dpll *pll;
de13a2e3
PZ
7116
7117 for_each_encoder_on_crtc(dev, crtc, encoder) {
7118 switch (encoder->type) {
7119 case INTEL_OUTPUT_LVDS:
7120 is_lvds = true;
7121 break;
de13a2e3
PZ
7122 }
7123
7124 num_connectors++;
a07d6787 7125 }
79e53945 7126
5dc5298b
PZ
7127 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7128 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7129
ff9a6750 7130 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7131 &has_reduced_clock, &reduced_clock);
ee9300bb 7132 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7133 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7134 return -EINVAL;
79e53945 7135 }
f47709a9
DV
7136 /* Compat-code for transition, will disappear. */
7137 if (!intel_crtc->config.clock_set) {
7138 intel_crtc->config.dpll.n = clock.n;
7139 intel_crtc->config.dpll.m1 = clock.m1;
7140 intel_crtc->config.dpll.m2 = clock.m2;
7141 intel_crtc->config.dpll.p1 = clock.p1;
7142 intel_crtc->config.dpll.p2 = clock.p2;
7143 }
79e53945 7144
5dc5298b 7145 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7146 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7147 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7148 if (has_reduced_clock)
7429e9d4 7149 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7150
7429e9d4 7151 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7152 &fp, &reduced_clock,
7153 has_reduced_clock ? &fp2 : NULL);
7154
959e16d6 7155 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7156 intel_crtc->config.dpll_hw_state.fp0 = fp;
7157 if (has_reduced_clock)
7158 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7159 else
7160 intel_crtc->config.dpll_hw_state.fp1 = fp;
7161
b89a1d39 7162 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7163 if (pll == NULL) {
84f44ce7 7164 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7165 pipe_name(intel_crtc->pipe));
4b645f14
JB
7166 return -EINVAL;
7167 }
ee7b9f93 7168 } else
e72f9fbf 7169 intel_put_shared_dpll(intel_crtc);
79e53945 7170
d330a953 7171 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7172 intel_crtc->lowfreq_avail = true;
7173 else
7174 intel_crtc->lowfreq_avail = false;
e2b78267 7175
c8f7a0db 7176 return 0;
79e53945
JB
7177}
7178
eb14cb74
VS
7179static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7180 struct intel_link_m_n *m_n)
7181{
7182 struct drm_device *dev = crtc->base.dev;
7183 struct drm_i915_private *dev_priv = dev->dev_private;
7184 enum pipe pipe = crtc->pipe;
7185
7186 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7187 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7188 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7189 & ~TU_SIZE_MASK;
7190 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7191 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7192 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7193}
7194
7195static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7196 enum transcoder transcoder,
b95af8be
VK
7197 struct intel_link_m_n *m_n,
7198 struct intel_link_m_n *m2_n2)
72419203
DV
7199{
7200 struct drm_device *dev = crtc->base.dev;
7201 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7202 enum pipe pipe = crtc->pipe;
72419203 7203
eb14cb74
VS
7204 if (INTEL_INFO(dev)->gen >= 5) {
7205 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7206 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7207 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7208 & ~TU_SIZE_MASK;
7209 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7210 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7211 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7212 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7213 * gen < 8) and if DRRS is supported (to make sure the
7214 * registers are not unnecessarily read).
7215 */
7216 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7217 crtc->config.has_drrs) {
7218 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7219 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7220 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7221 & ~TU_SIZE_MASK;
7222 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7223 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7224 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7225 }
eb14cb74
VS
7226 } else {
7227 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7228 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7229 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7230 & ~TU_SIZE_MASK;
7231 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7232 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7233 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7234 }
7235}
7236
7237void intel_dp_get_m_n(struct intel_crtc *crtc,
7238 struct intel_crtc_config *pipe_config)
7239{
7240 if (crtc->config.has_pch_encoder)
7241 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7242 else
7243 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7244 &pipe_config->dp_m_n,
7245 &pipe_config->dp_m2_n2);
eb14cb74 7246}
72419203 7247
eb14cb74
VS
7248static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7249 struct intel_crtc_config *pipe_config)
7250{
7251 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7252 &pipe_config->fdi_m_n, NULL);
72419203
DV
7253}
7254
2fa2fe9a
DV
7255static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7256 struct intel_crtc_config *pipe_config)
7257{
7258 struct drm_device *dev = crtc->base.dev;
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 uint32_t tmp;
7261
7262 tmp = I915_READ(PF_CTL(crtc->pipe));
7263
7264 if (tmp & PF_ENABLE) {
fd4daa9c 7265 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7266 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7267 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7268
7269 /* We currently do not free assignements of panel fitters on
7270 * ivb/hsw (since we don't use the higher upscaling modes which
7271 * differentiates them) so just WARN about this case for now. */
7272 if (IS_GEN7(dev)) {
7273 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7274 PF_PIPE_SEL_IVB(crtc->pipe));
7275 }
2fa2fe9a 7276 }
79e53945
JB
7277}
7278
4c6baa59
JB
7279static void ironlake_get_plane_config(struct intel_crtc *crtc,
7280 struct intel_plane_config *plane_config)
7281{
7282 struct drm_device *dev = crtc->base.dev;
7283 struct drm_i915_private *dev_priv = dev->dev_private;
7284 u32 val, base, offset;
7285 int pipe = crtc->pipe, plane = crtc->plane;
7286 int fourcc, pixel_format;
7287 int aligned_height;
7288
66e514c1
DA
7289 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7290 if (!crtc->base.primary->fb) {
4c6baa59
JB
7291 DRM_DEBUG_KMS("failed to alloc fb\n");
7292 return;
7293 }
7294
7295 val = I915_READ(DSPCNTR(plane));
7296
7297 if (INTEL_INFO(dev)->gen >= 4)
7298 if (val & DISPPLANE_TILED)
7299 plane_config->tiled = true;
7300
7301 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7302 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7303 crtc->base.primary->fb->pixel_format = fourcc;
7304 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7305 drm_format_plane_cpp(fourcc, 0) * 8;
7306
7307 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7308 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7309 offset = I915_READ(DSPOFFSET(plane));
7310 } else {
7311 if (plane_config->tiled)
7312 offset = I915_READ(DSPTILEOFF(plane));
7313 else
7314 offset = I915_READ(DSPLINOFF(plane));
7315 }
7316 plane_config->base = base;
7317
7318 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7319 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7320 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7321
7322 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7323 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7324
66e514c1 7325 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7326 plane_config->tiled);
7327
1267a26b
FF
7328 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7329 aligned_height);
4c6baa59
JB
7330
7331 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7332 pipe, plane, crtc->base.primary->fb->width,
7333 crtc->base.primary->fb->height,
7334 crtc->base.primary->fb->bits_per_pixel, base,
7335 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7336 plane_config->size);
7337}
7338
0e8ffe1b
DV
7339static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7340 struct intel_crtc_config *pipe_config)
7341{
7342 struct drm_device *dev = crtc->base.dev;
7343 struct drm_i915_private *dev_priv = dev->dev_private;
7344 uint32_t tmp;
7345
930e8c9e
PZ
7346 if (!intel_display_power_enabled(dev_priv,
7347 POWER_DOMAIN_PIPE(crtc->pipe)))
7348 return false;
7349
e143a21c 7350 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7351 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7352
0e8ffe1b
DV
7353 tmp = I915_READ(PIPECONF(crtc->pipe));
7354 if (!(tmp & PIPECONF_ENABLE))
7355 return false;
7356
42571aef
VS
7357 switch (tmp & PIPECONF_BPC_MASK) {
7358 case PIPECONF_6BPC:
7359 pipe_config->pipe_bpp = 18;
7360 break;
7361 case PIPECONF_8BPC:
7362 pipe_config->pipe_bpp = 24;
7363 break;
7364 case PIPECONF_10BPC:
7365 pipe_config->pipe_bpp = 30;
7366 break;
7367 case PIPECONF_12BPC:
7368 pipe_config->pipe_bpp = 36;
7369 break;
7370 default:
7371 break;
7372 }
7373
b5a9fa09
DV
7374 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7375 pipe_config->limited_color_range = true;
7376
ab9412ba 7377 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7378 struct intel_shared_dpll *pll;
7379
88adfff1
DV
7380 pipe_config->has_pch_encoder = true;
7381
627eb5a3
DV
7382 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7383 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7384 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7385
7386 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7387
c0d43d62 7388 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7389 pipe_config->shared_dpll =
7390 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7391 } else {
7392 tmp = I915_READ(PCH_DPLL_SEL);
7393 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7394 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7395 else
7396 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7397 }
66e985c0
DV
7398
7399 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7400
7401 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7402 &pipe_config->dpll_hw_state));
c93f54cf
DV
7403
7404 tmp = pipe_config->dpll_hw_state.dpll;
7405 pipe_config->pixel_multiplier =
7406 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7407 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7408
7409 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7410 } else {
7411 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7412 }
7413
1bd1bd80
DV
7414 intel_get_pipe_timings(crtc, pipe_config);
7415
2fa2fe9a
DV
7416 ironlake_get_pfit_config(crtc, pipe_config);
7417
0e8ffe1b
DV
7418 return true;
7419}
7420
be256dc7
PZ
7421static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7422{
7423 struct drm_device *dev = dev_priv->dev;
be256dc7 7424 struct intel_crtc *crtc;
be256dc7 7425
d3fcc808 7426 for_each_intel_crtc(dev, crtc)
798183c5 7427 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7428 pipe_name(crtc->pipe));
7429
7430 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7431 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7432 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7433 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7434 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7435 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7436 "CPU PWM1 enabled\n");
c5107b87
PZ
7437 if (IS_HASWELL(dev))
7438 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7439 "CPU PWM2 enabled\n");
be256dc7
PZ
7440 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7441 "PCH PWM1 enabled\n");
7442 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7443 "Utility pin enabled\n");
7444 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7445
9926ada1
PZ
7446 /*
7447 * In theory we can still leave IRQs enabled, as long as only the HPD
7448 * interrupts remain enabled. We used to check for that, but since it's
7449 * gen-specific and since we only disable LCPLL after we fully disable
7450 * the interrupts, the check below should be enough.
7451 */
9df7575f 7452 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7453}
7454
9ccd5aeb
PZ
7455static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7456{
7457 struct drm_device *dev = dev_priv->dev;
7458
7459 if (IS_HASWELL(dev))
7460 return I915_READ(D_COMP_HSW);
7461 else
7462 return I915_READ(D_COMP_BDW);
7463}
7464
3c4c9b81
PZ
7465static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7466{
7467 struct drm_device *dev = dev_priv->dev;
7468
7469 if (IS_HASWELL(dev)) {
7470 mutex_lock(&dev_priv->rps.hw_lock);
7471 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7472 val))
f475dadf 7473 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7474 mutex_unlock(&dev_priv->rps.hw_lock);
7475 } else {
9ccd5aeb
PZ
7476 I915_WRITE(D_COMP_BDW, val);
7477 POSTING_READ(D_COMP_BDW);
3c4c9b81 7478 }
be256dc7
PZ
7479}
7480
7481/*
7482 * This function implements pieces of two sequences from BSpec:
7483 * - Sequence for display software to disable LCPLL
7484 * - Sequence for display software to allow package C8+
7485 * The steps implemented here are just the steps that actually touch the LCPLL
7486 * register. Callers should take care of disabling all the display engine
7487 * functions, doing the mode unset, fixing interrupts, etc.
7488 */
6ff58d53
PZ
7489static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7490 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7491{
7492 uint32_t val;
7493
7494 assert_can_disable_lcpll(dev_priv);
7495
7496 val = I915_READ(LCPLL_CTL);
7497
7498 if (switch_to_fclk) {
7499 val |= LCPLL_CD_SOURCE_FCLK;
7500 I915_WRITE(LCPLL_CTL, val);
7501
7502 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7503 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7504 DRM_ERROR("Switching to FCLK failed\n");
7505
7506 val = I915_READ(LCPLL_CTL);
7507 }
7508
7509 val |= LCPLL_PLL_DISABLE;
7510 I915_WRITE(LCPLL_CTL, val);
7511 POSTING_READ(LCPLL_CTL);
7512
7513 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7514 DRM_ERROR("LCPLL still locked\n");
7515
9ccd5aeb 7516 val = hsw_read_dcomp(dev_priv);
be256dc7 7517 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7518 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7519 ndelay(100);
7520
9ccd5aeb
PZ
7521 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7522 1))
be256dc7
PZ
7523 DRM_ERROR("D_COMP RCOMP still in progress\n");
7524
7525 if (allow_power_down) {
7526 val = I915_READ(LCPLL_CTL);
7527 val |= LCPLL_POWER_DOWN_ALLOW;
7528 I915_WRITE(LCPLL_CTL, val);
7529 POSTING_READ(LCPLL_CTL);
7530 }
7531}
7532
7533/*
7534 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7535 * source.
7536 */
6ff58d53 7537static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7538{
7539 uint32_t val;
a8a8bd54 7540 unsigned long irqflags;
be256dc7
PZ
7541
7542 val = I915_READ(LCPLL_CTL);
7543
7544 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7545 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7546 return;
7547
a8a8bd54
PZ
7548 /*
7549 * Make sure we're not on PC8 state before disabling PC8, otherwise
7550 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7551 *
7552 * The other problem is that hsw_restore_lcpll() is called as part of
7553 * the runtime PM resume sequence, so we can't just call
7554 * gen6_gt_force_wake_get() because that function calls
7555 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7556 * while we are on the resume sequence. So to solve this problem we have
7557 * to call special forcewake code that doesn't touch runtime PM and
7558 * doesn't enable the forcewake delayed work.
7559 */
7560 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7561 if (dev_priv->uncore.forcewake_count++ == 0)
7562 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7563 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7564
be256dc7
PZ
7565 if (val & LCPLL_POWER_DOWN_ALLOW) {
7566 val &= ~LCPLL_POWER_DOWN_ALLOW;
7567 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7568 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7569 }
7570
9ccd5aeb 7571 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7572 val |= D_COMP_COMP_FORCE;
7573 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7574 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7575
7576 val = I915_READ(LCPLL_CTL);
7577 val &= ~LCPLL_PLL_DISABLE;
7578 I915_WRITE(LCPLL_CTL, val);
7579
7580 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7581 DRM_ERROR("LCPLL not locked yet\n");
7582
7583 if (val & LCPLL_CD_SOURCE_FCLK) {
7584 val = I915_READ(LCPLL_CTL);
7585 val &= ~LCPLL_CD_SOURCE_FCLK;
7586 I915_WRITE(LCPLL_CTL, val);
7587
7588 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7589 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7590 DRM_ERROR("Switching back to LCPLL failed\n");
7591 }
215733fa 7592
a8a8bd54
PZ
7593 /* See the big comment above. */
7594 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7595 if (--dev_priv->uncore.forcewake_count == 0)
7596 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7597 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7598}
7599
765dab67
PZ
7600/*
7601 * Package states C8 and deeper are really deep PC states that can only be
7602 * reached when all the devices on the system allow it, so even if the graphics
7603 * device allows PC8+, it doesn't mean the system will actually get to these
7604 * states. Our driver only allows PC8+ when going into runtime PM.
7605 *
7606 * The requirements for PC8+ are that all the outputs are disabled, the power
7607 * well is disabled and most interrupts are disabled, and these are also
7608 * requirements for runtime PM. When these conditions are met, we manually do
7609 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7610 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7611 * hang the machine.
7612 *
7613 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7614 * the state of some registers, so when we come back from PC8+ we need to
7615 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7616 * need to take care of the registers kept by RC6. Notice that this happens even
7617 * if we don't put the device in PCI D3 state (which is what currently happens
7618 * because of the runtime PM support).
7619 *
7620 * For more, read "Display Sequences for Package C8" on the hardware
7621 * documentation.
7622 */
a14cb6fc 7623void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7624{
c67a470b
PZ
7625 struct drm_device *dev = dev_priv->dev;
7626 uint32_t val;
7627
c67a470b
PZ
7628 DRM_DEBUG_KMS("Enabling package C8+\n");
7629
c67a470b
PZ
7630 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7631 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7632 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7633 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7634 }
7635
7636 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7637 hsw_disable_lcpll(dev_priv, true, true);
7638}
7639
a14cb6fc 7640void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7641{
7642 struct drm_device *dev = dev_priv->dev;
7643 uint32_t val;
7644
c67a470b
PZ
7645 DRM_DEBUG_KMS("Disabling package C8+\n");
7646
7647 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7648 lpt_init_pch_refclk(dev);
7649
7650 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7651 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7652 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7653 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7654 }
7655
7656 intel_prepare_ddi(dev);
c67a470b
PZ
7657}
7658
9a952a0d
PZ
7659static void snb_modeset_global_resources(struct drm_device *dev)
7660{
7661 modeset_update_crtc_power_domains(dev);
7662}
7663
4f074129
ID
7664static void haswell_modeset_global_resources(struct drm_device *dev)
7665{
da723569 7666 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7667}
7668
09b4ddf9 7669static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7670 int x, int y,
7671 struct drm_framebuffer *fb)
7672{
09b4ddf9 7673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7674
566b734a 7675 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7676 return -EINVAL;
716c2e55 7677
644cef34
DV
7678 intel_crtc->lowfreq_avail = false;
7679
c8f7a0db 7680 return 0;
79e53945
JB
7681}
7682
7d2c8175
DL
7683static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7684 enum port port,
7685 struct intel_crtc_config *pipe_config)
7686{
7687 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7688
7689 switch (pipe_config->ddi_pll_sel) {
7690 case PORT_CLK_SEL_WRPLL1:
7691 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7692 break;
7693 case PORT_CLK_SEL_WRPLL2:
7694 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7695 break;
7696 }
7697}
7698
26804afd
DV
7699static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7700 struct intel_crtc_config *pipe_config)
7701{
7702 struct drm_device *dev = crtc->base.dev;
7703 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7704 struct intel_shared_dpll *pll;
26804afd
DV
7705 enum port port;
7706 uint32_t tmp;
7707
7708 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7709
7710 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7711
7d2c8175 7712 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7713
d452c5b6
DV
7714 if (pipe_config->shared_dpll >= 0) {
7715 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7716
7717 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7718 &pipe_config->dpll_hw_state));
7719 }
7720
26804afd
DV
7721 /*
7722 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7723 * DDI E. So just check whether this pipe is wired to DDI E and whether
7724 * the PCH transcoder is on.
7725 */
7726 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7727 pipe_config->has_pch_encoder = true;
7728
7729 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7730 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7731 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7732
7733 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7734 }
7735}
7736
0e8ffe1b
DV
7737static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7738 struct intel_crtc_config *pipe_config)
7739{
7740 struct drm_device *dev = crtc->base.dev;
7741 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7742 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7743 uint32_t tmp;
7744
b5482bd0
ID
7745 if (!intel_display_power_enabled(dev_priv,
7746 POWER_DOMAIN_PIPE(crtc->pipe)))
7747 return false;
7748
e143a21c 7749 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7750 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7751
eccb140b
DV
7752 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7753 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7754 enum pipe trans_edp_pipe;
7755 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7756 default:
7757 WARN(1, "unknown pipe linked to edp transcoder\n");
7758 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7759 case TRANS_DDI_EDP_INPUT_A_ON:
7760 trans_edp_pipe = PIPE_A;
7761 break;
7762 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7763 trans_edp_pipe = PIPE_B;
7764 break;
7765 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7766 trans_edp_pipe = PIPE_C;
7767 break;
7768 }
7769
7770 if (trans_edp_pipe == crtc->pipe)
7771 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7772 }
7773
da7e29bd 7774 if (!intel_display_power_enabled(dev_priv,
eccb140b 7775 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7776 return false;
7777
eccb140b 7778 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7779 if (!(tmp & PIPECONF_ENABLE))
7780 return false;
7781
26804afd 7782 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7783
1bd1bd80
DV
7784 intel_get_pipe_timings(crtc, pipe_config);
7785
2fa2fe9a 7786 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7787 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7788 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7789
e59150dc
JB
7790 if (IS_HASWELL(dev))
7791 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7792 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7793
6c49f241
DV
7794 pipe_config->pixel_multiplier = 1;
7795
0e8ffe1b
DV
7796 return true;
7797}
7798
1a91510d
JN
7799static struct {
7800 int clock;
7801 u32 config;
7802} hdmi_audio_clock[] = {
7803 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7804 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7805 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7806 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7807 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7808 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7809 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7810 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7811 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7812 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7813};
7814
7815/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7816static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7817{
7818 int i;
7819
7820 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7821 if (mode->clock == hdmi_audio_clock[i].clock)
7822 break;
7823 }
7824
7825 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7826 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7827 i = 1;
7828 }
7829
7830 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7831 hdmi_audio_clock[i].clock,
7832 hdmi_audio_clock[i].config);
7833
7834 return hdmi_audio_clock[i].config;
7835}
7836
3a9627f4
WF
7837static bool intel_eld_uptodate(struct drm_connector *connector,
7838 int reg_eldv, uint32_t bits_eldv,
7839 int reg_elda, uint32_t bits_elda,
7840 int reg_edid)
7841{
7842 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7843 uint8_t *eld = connector->eld;
7844 uint32_t i;
7845
7846 i = I915_READ(reg_eldv);
7847 i &= bits_eldv;
7848
7849 if (!eld[0])
7850 return !i;
7851
7852 if (!i)
7853 return false;
7854
7855 i = I915_READ(reg_elda);
7856 i &= ~bits_elda;
7857 I915_WRITE(reg_elda, i);
7858
7859 for (i = 0; i < eld[2]; i++)
7860 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7861 return false;
7862
7863 return true;
7864}
7865
e0dac65e 7866static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7867 struct drm_crtc *crtc,
7868 struct drm_display_mode *mode)
e0dac65e
WF
7869{
7870 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7871 uint8_t *eld = connector->eld;
7872 uint32_t eldv;
7873 uint32_t len;
7874 uint32_t i;
7875
7876 i = I915_READ(G4X_AUD_VID_DID);
7877
7878 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7879 eldv = G4X_ELDV_DEVCL_DEVBLC;
7880 else
7881 eldv = G4X_ELDV_DEVCTG;
7882
3a9627f4
WF
7883 if (intel_eld_uptodate(connector,
7884 G4X_AUD_CNTL_ST, eldv,
7885 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7886 G4X_HDMIW_HDMIEDID))
7887 return;
7888
e0dac65e
WF
7889 i = I915_READ(G4X_AUD_CNTL_ST);
7890 i &= ~(eldv | G4X_ELD_ADDR);
7891 len = (i >> 9) & 0x1f; /* ELD buffer size */
7892 I915_WRITE(G4X_AUD_CNTL_ST, i);
7893
7894 if (!eld[0])
7895 return;
7896
7897 len = min_t(uint8_t, eld[2], len);
7898 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7899 for (i = 0; i < len; i++)
7900 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7901
7902 i = I915_READ(G4X_AUD_CNTL_ST);
7903 i |= eldv;
7904 I915_WRITE(G4X_AUD_CNTL_ST, i);
7905}
7906
83358c85 7907static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7908 struct drm_crtc *crtc,
7909 struct drm_display_mode *mode)
83358c85
WX
7910{
7911 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7912 uint8_t *eld = connector->eld;
83358c85
WX
7913 uint32_t eldv;
7914 uint32_t i;
7915 int len;
7916 int pipe = to_intel_crtc(crtc)->pipe;
7917 int tmp;
7918
7919 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7920 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7921 int aud_config = HSW_AUD_CFG(pipe);
7922 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7923
83358c85
WX
7924 /* Audio output enable */
7925 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7926 tmp = I915_READ(aud_cntrl_st2);
7927 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7928 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7929 POSTING_READ(aud_cntrl_st2);
83358c85 7930
c7905792 7931 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7932
7933 /* Set ELD valid state */
7934 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7935 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7936 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7937 I915_WRITE(aud_cntrl_st2, tmp);
7938 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7939 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7940
7941 /* Enable HDMI mode */
7942 tmp = I915_READ(aud_config);
7e7cb34f 7943 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7944 /* clear N_programing_enable and N_value_index */
7945 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7946 I915_WRITE(aud_config, tmp);
7947
7948 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7949
7950 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7951
7952 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7953 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7954 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7955 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7956 } else {
7957 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7958 }
83358c85
WX
7959
7960 if (intel_eld_uptodate(connector,
7961 aud_cntrl_st2, eldv,
7962 aud_cntl_st, IBX_ELD_ADDRESS,
7963 hdmiw_hdmiedid))
7964 return;
7965
7966 i = I915_READ(aud_cntrl_st2);
7967 i &= ~eldv;
7968 I915_WRITE(aud_cntrl_st2, i);
7969
7970 if (!eld[0])
7971 return;
7972
7973 i = I915_READ(aud_cntl_st);
7974 i &= ~IBX_ELD_ADDRESS;
7975 I915_WRITE(aud_cntl_st, i);
7976 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7977 DRM_DEBUG_DRIVER("port num:%d\n", i);
7978
7979 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7980 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7981 for (i = 0; i < len; i++)
7982 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7983
7984 i = I915_READ(aud_cntrl_st2);
7985 i |= eldv;
7986 I915_WRITE(aud_cntrl_st2, i);
7987
7988}
7989
e0dac65e 7990static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7991 struct drm_crtc *crtc,
7992 struct drm_display_mode *mode)
e0dac65e
WF
7993{
7994 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7995 uint8_t *eld = connector->eld;
7996 uint32_t eldv;
7997 uint32_t i;
7998 int len;
7999 int hdmiw_hdmiedid;
b6daa025 8000 int aud_config;
e0dac65e
WF
8001 int aud_cntl_st;
8002 int aud_cntrl_st2;
9b138a83 8003 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 8004
b3f33cbf 8005 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
8006 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8007 aud_config = IBX_AUD_CFG(pipe);
8008 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8009 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8010 } else if (IS_VALLEYVIEW(connector->dev)) {
8011 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8012 aud_config = VLV_AUD_CFG(pipe);
8013 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8014 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8015 } else {
9b138a83
WX
8016 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8017 aud_config = CPT_AUD_CFG(pipe);
8018 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8019 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8020 }
8021
9b138a83 8022 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8023
9ca2fe73
ML
8024 if (IS_VALLEYVIEW(connector->dev)) {
8025 struct intel_encoder *intel_encoder;
8026 struct intel_digital_port *intel_dig_port;
8027
8028 intel_encoder = intel_attached_encoder(connector);
8029 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8030 i = intel_dig_port->port;
8031 } else {
8032 i = I915_READ(aud_cntl_st);
8033 i = (i >> 29) & DIP_PORT_SEL_MASK;
8034 /* DIP_Port_Select, 0x1 = PortB */
8035 }
8036
e0dac65e
WF
8037 if (!i) {
8038 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8039 /* operate blindly on all ports */
1202b4c6
WF
8040 eldv = IBX_ELD_VALIDB;
8041 eldv |= IBX_ELD_VALIDB << 4;
8042 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8043 } else {
2582a850 8044 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8045 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8046 }
8047
3a9627f4
WF
8048 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8049 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8050 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8051 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8052 } else {
8053 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8054 }
e0dac65e 8055
3a9627f4
WF
8056 if (intel_eld_uptodate(connector,
8057 aud_cntrl_st2, eldv,
8058 aud_cntl_st, IBX_ELD_ADDRESS,
8059 hdmiw_hdmiedid))
8060 return;
8061
e0dac65e
WF
8062 i = I915_READ(aud_cntrl_st2);
8063 i &= ~eldv;
8064 I915_WRITE(aud_cntrl_st2, i);
8065
8066 if (!eld[0])
8067 return;
8068
e0dac65e 8069 i = I915_READ(aud_cntl_st);
1202b4c6 8070 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8071 I915_WRITE(aud_cntl_st, i);
8072
8073 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8074 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8075 for (i = 0; i < len; i++)
8076 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8077
8078 i = I915_READ(aud_cntrl_st2);
8079 i |= eldv;
8080 I915_WRITE(aud_cntrl_st2, i);
8081}
8082
8083void intel_write_eld(struct drm_encoder *encoder,
8084 struct drm_display_mode *mode)
8085{
8086 struct drm_crtc *crtc = encoder->crtc;
8087 struct drm_connector *connector;
8088 struct drm_device *dev = encoder->dev;
8089 struct drm_i915_private *dev_priv = dev->dev_private;
8090
8091 connector = drm_select_eld(encoder, mode);
8092 if (!connector)
8093 return;
8094
8095 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8096 connector->base.id,
c23cc417 8097 connector->name,
e0dac65e 8098 connector->encoder->base.id,
8e329a03 8099 connector->encoder->name);
e0dac65e
WF
8100
8101 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8102
8103 if (dev_priv->display.write_eld)
34427052 8104 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8105}
8106
560b85bb
CW
8107static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8108{
8109 struct drm_device *dev = crtc->dev;
8110 struct drm_i915_private *dev_priv = dev->dev_private;
8111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8112 uint32_t cntl = 0, size = 0;
560b85bb 8113
dc41c154
VS
8114 if (base) {
8115 unsigned int width = intel_crtc->cursor_width;
8116 unsigned int height = intel_crtc->cursor_height;
8117 unsigned int stride = roundup_pow_of_two(width) * 4;
8118
8119 switch (stride) {
8120 default:
8121 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8122 width, stride);
8123 stride = 256;
8124 /* fallthrough */
8125 case 256:
8126 case 512:
8127 case 1024:
8128 case 2048:
8129 break;
4b0e333e
CW
8130 }
8131
dc41c154
VS
8132 cntl |= CURSOR_ENABLE |
8133 CURSOR_GAMMA_ENABLE |
8134 CURSOR_FORMAT_ARGB |
8135 CURSOR_STRIDE(stride);
8136
8137 size = (height << 12) | width;
4b0e333e 8138 }
560b85bb 8139
dc41c154
VS
8140 if (intel_crtc->cursor_cntl != 0 &&
8141 (intel_crtc->cursor_base != base ||
8142 intel_crtc->cursor_size != size ||
8143 intel_crtc->cursor_cntl != cntl)) {
8144 /* On these chipsets we can only modify the base/size/stride
8145 * whilst the cursor is disabled.
8146 */
8147 I915_WRITE(_CURACNTR, 0);
4b0e333e 8148 POSTING_READ(_CURACNTR);
dc41c154 8149 intel_crtc->cursor_cntl = 0;
4b0e333e 8150 }
560b85bb 8151
dc41c154 8152 if (intel_crtc->cursor_base != base)
9db4a9c7 8153 I915_WRITE(_CURABASE, base);
4726e0b0 8154
dc41c154
VS
8155 if (intel_crtc->cursor_size != size) {
8156 I915_WRITE(CURSIZE, size);
8157 intel_crtc->cursor_size = size;
4b0e333e 8158 }
560b85bb 8159
4b0e333e 8160 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8161 I915_WRITE(_CURACNTR, cntl);
8162 POSTING_READ(_CURACNTR);
4b0e333e 8163 intel_crtc->cursor_cntl = cntl;
560b85bb 8164 }
560b85bb
CW
8165}
8166
560b85bb 8167static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8168{
8169 struct drm_device *dev = crtc->dev;
8170 struct drm_i915_private *dev_priv = dev->dev_private;
8171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8172 int pipe = intel_crtc->pipe;
4b0e333e
CW
8173 uint32_t cntl;
8174
8175 cntl = 0;
8176 if (base) {
8177 cntl = MCURSOR_GAMMA_ENABLE;
8178 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8179 case 64:
8180 cntl |= CURSOR_MODE_64_ARGB_AX;
8181 break;
8182 case 128:
8183 cntl |= CURSOR_MODE_128_ARGB_AX;
8184 break;
8185 case 256:
8186 cntl |= CURSOR_MODE_256_ARGB_AX;
8187 break;
8188 default:
8189 WARN_ON(1);
8190 return;
65a21cd6 8191 }
4b0e333e 8192 cntl |= pipe << 28; /* Connect to correct pipe */
4b0e333e
CW
8193 }
8194 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8195 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 8196
4b0e333e
CW
8197 if (intel_crtc->cursor_cntl != cntl) {
8198 I915_WRITE(CURCNTR(pipe), cntl);
8199 POSTING_READ(CURCNTR(pipe));
8200 intel_crtc->cursor_cntl = cntl;
65a21cd6 8201 }
4b0e333e 8202
65a21cd6 8203 /* and commit changes on next vblank */
5efb3e28
VS
8204 I915_WRITE(CURBASE(pipe), base);
8205 POSTING_READ(CURBASE(pipe));
65a21cd6
JB
8206}
8207
cda4b7d3 8208/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8209static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8210 bool on)
cda4b7d3
CW
8211{
8212 struct drm_device *dev = crtc->dev;
8213 struct drm_i915_private *dev_priv = dev->dev_private;
8214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8215 int pipe = intel_crtc->pipe;
3d7d6510
MR
8216 int x = crtc->cursor_x;
8217 int y = crtc->cursor_y;
d6e4db15 8218 u32 base = 0, pos = 0;
cda4b7d3 8219
d6e4db15 8220 if (on)
cda4b7d3 8221 base = intel_crtc->cursor_addr;
cda4b7d3 8222
d6e4db15
VS
8223 if (x >= intel_crtc->config.pipe_src_w)
8224 base = 0;
8225
8226 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8227 base = 0;
8228
8229 if (x < 0) {
efc9064e 8230 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8231 base = 0;
8232
8233 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8234 x = -x;
8235 }
8236 pos |= x << CURSOR_X_SHIFT;
8237
8238 if (y < 0) {
efc9064e 8239 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8240 base = 0;
8241
8242 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8243 y = -y;
8244 }
8245 pos |= y << CURSOR_Y_SHIFT;
8246
4b0e333e 8247 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8248 return;
8249
5efb3e28
VS
8250 I915_WRITE(CURPOS(pipe), pos);
8251
8ac54669 8252 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8253 i845_update_cursor(crtc, base);
8254 else
8255 i9xx_update_cursor(crtc, base);
4b0e333e 8256 intel_crtc->cursor_base = base;
cda4b7d3
CW
8257}
8258
dc41c154
VS
8259static bool cursor_size_ok(struct drm_device *dev,
8260 uint32_t width, uint32_t height)
8261{
8262 if (width == 0 || height == 0)
8263 return false;
8264
8265 /*
8266 * 845g/865g are special in that they are only limited by
8267 * the width of their cursors, the height is arbitrary up to
8268 * the precision of the register. Everything else requires
8269 * square cursors, limited to a few power-of-two sizes.
8270 */
8271 if (IS_845G(dev) || IS_I865G(dev)) {
8272 if ((width & 63) != 0)
8273 return false;
8274
8275 if (width > (IS_845G(dev) ? 64 : 512))
8276 return false;
8277
8278 if (height > 1023)
8279 return false;
8280 } else {
8281 switch (width | height) {
8282 case 256:
8283 case 128:
8284 if (IS_GEN2(dev))
8285 return false;
8286 case 64:
8287 break;
8288 default:
8289 return false;
8290 }
8291 }
8292
8293 return true;
8294}
8295
e3287951
MR
8296/*
8297 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8298 *
8299 * Note that the object's reference will be consumed if the update fails. If
8300 * the update succeeds, the reference of the old object (if any) will be
8301 * consumed.
8302 */
8303static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8304 struct drm_i915_gem_object *obj,
8305 uint32_t width, uint32_t height)
79e53945
JB
8306{
8307 struct drm_device *dev = crtc->dev;
79e53945 8308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8309 enum pipe pipe = intel_crtc->pipe;
dc41c154 8310 unsigned old_width, stride;
cda4b7d3 8311 uint32_t addr;
3f8bc370 8312 int ret;
79e53945 8313
79e53945 8314 /* if we want to turn off the cursor ignore width and height */
e3287951 8315 if (!obj) {
28c97730 8316 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8317 addr = 0;
5004417d 8318 mutex_lock(&dev->struct_mutex);
3f8bc370 8319 goto finish;
79e53945
JB
8320 }
8321
4726e0b0 8322 /* Check for which cursor types we support */
dc41c154 8323 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8324 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8325 return -EINVAL;
8326 }
8327
dc41c154
VS
8328 stride = roundup_pow_of_two(width) * 4;
8329 if (obj->base.size < stride * height) {
e3287951 8330 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8331 ret = -ENOMEM;
8332 goto fail;
79e53945
JB
8333 }
8334
71acb5eb 8335 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8336 mutex_lock(&dev->struct_mutex);
3d13ef2e 8337 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8338 unsigned alignment;
8339
d9e86c0e 8340 if (obj->tiling_mode) {
3b25b31f 8341 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8342 ret = -EINVAL;
8343 goto fail_locked;
8344 }
8345
693db184
CW
8346 /* Note that the w/a also requires 2 PTE of padding following
8347 * the bo. We currently fill all unused PTE with the shadow
8348 * page and so we should always have valid PTE following the
8349 * cursor preventing the VT-d warning.
8350 */
8351 alignment = 0;
8352 if (need_vtd_wa(dev))
8353 alignment = 64*1024;
8354
8355 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8356 if (ret) {
3b25b31f 8357 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 8358 goto fail_locked;
e7b526bb
CW
8359 }
8360
d9e86c0e
CW
8361 ret = i915_gem_object_put_fence(obj);
8362 if (ret) {
3b25b31f 8363 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
8364 goto fail_unpin;
8365 }
8366
f343c5f6 8367 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 8368 } else {
6eeefaf3 8369 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8370 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8371 if (ret) {
3b25b31f 8372 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8373 goto fail_locked;
71acb5eb 8374 }
00731155 8375 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8376 }
8377
3f8bc370 8378 finish:
3f8bc370 8379 if (intel_crtc->cursor_bo) {
00731155 8380 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8381 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8382 }
80824003 8383
a071fa00
DV
8384 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8385 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8386 mutex_unlock(&dev->struct_mutex);
3f8bc370 8387
64f962e3
CW
8388 old_width = intel_crtc->cursor_width;
8389
3f8bc370 8390 intel_crtc->cursor_addr = addr;
05394f39 8391 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8392 intel_crtc->cursor_width = width;
8393 intel_crtc->cursor_height = height;
8394
64f962e3
CW
8395 if (intel_crtc->active) {
8396 if (old_width != width)
8397 intel_update_watermarks(crtc);
f2f5f771 8398 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8399 }
3f8bc370 8400
f99d7069
DV
8401 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8402
79e53945 8403 return 0;
e7b526bb 8404fail_unpin:
cc98b413 8405 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8406fail_locked:
34b8686e 8407 mutex_unlock(&dev->struct_mutex);
bc9025bd 8408fail:
05394f39 8409 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8410 return ret;
79e53945
JB
8411}
8412
79e53945 8413static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8414 u16 *blue, uint32_t start, uint32_t size)
79e53945 8415{
7203425a 8416 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8418
7203425a 8419 for (i = start; i < end; i++) {
79e53945
JB
8420 intel_crtc->lut_r[i] = red[i] >> 8;
8421 intel_crtc->lut_g[i] = green[i] >> 8;
8422 intel_crtc->lut_b[i] = blue[i] >> 8;
8423 }
8424
8425 intel_crtc_load_lut(crtc);
8426}
8427
79e53945
JB
8428/* VESA 640x480x72Hz mode to set on the pipe */
8429static struct drm_display_mode load_detect_mode = {
8430 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8431 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8432};
8433
a8bb6818
DV
8434struct drm_framebuffer *
8435__intel_framebuffer_create(struct drm_device *dev,
8436 struct drm_mode_fb_cmd2 *mode_cmd,
8437 struct drm_i915_gem_object *obj)
d2dff872
CW
8438{
8439 struct intel_framebuffer *intel_fb;
8440 int ret;
8441
8442 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8443 if (!intel_fb) {
8444 drm_gem_object_unreference_unlocked(&obj->base);
8445 return ERR_PTR(-ENOMEM);
8446 }
8447
8448 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8449 if (ret)
8450 goto err;
d2dff872
CW
8451
8452 return &intel_fb->base;
dd4916c5
DV
8453err:
8454 drm_gem_object_unreference_unlocked(&obj->base);
8455 kfree(intel_fb);
8456
8457 return ERR_PTR(ret);
d2dff872
CW
8458}
8459
b5ea642a 8460static struct drm_framebuffer *
a8bb6818
DV
8461intel_framebuffer_create(struct drm_device *dev,
8462 struct drm_mode_fb_cmd2 *mode_cmd,
8463 struct drm_i915_gem_object *obj)
8464{
8465 struct drm_framebuffer *fb;
8466 int ret;
8467
8468 ret = i915_mutex_lock_interruptible(dev);
8469 if (ret)
8470 return ERR_PTR(ret);
8471 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8472 mutex_unlock(&dev->struct_mutex);
8473
8474 return fb;
8475}
8476
d2dff872
CW
8477static u32
8478intel_framebuffer_pitch_for_width(int width, int bpp)
8479{
8480 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8481 return ALIGN(pitch, 64);
8482}
8483
8484static u32
8485intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8486{
8487 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8488 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8489}
8490
8491static struct drm_framebuffer *
8492intel_framebuffer_create_for_mode(struct drm_device *dev,
8493 struct drm_display_mode *mode,
8494 int depth, int bpp)
8495{
8496 struct drm_i915_gem_object *obj;
0fed39bd 8497 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8498
8499 obj = i915_gem_alloc_object(dev,
8500 intel_framebuffer_size_for_mode(mode, bpp));
8501 if (obj == NULL)
8502 return ERR_PTR(-ENOMEM);
8503
8504 mode_cmd.width = mode->hdisplay;
8505 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8506 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8507 bpp);
5ca0c34a 8508 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8509
8510 return intel_framebuffer_create(dev, &mode_cmd, obj);
8511}
8512
8513static struct drm_framebuffer *
8514mode_fits_in_fbdev(struct drm_device *dev,
8515 struct drm_display_mode *mode)
8516{
4520f53a 8517#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8518 struct drm_i915_private *dev_priv = dev->dev_private;
8519 struct drm_i915_gem_object *obj;
8520 struct drm_framebuffer *fb;
8521
4c0e5528 8522 if (!dev_priv->fbdev)
d2dff872
CW
8523 return NULL;
8524
4c0e5528 8525 if (!dev_priv->fbdev->fb)
d2dff872
CW
8526 return NULL;
8527
4c0e5528
DV
8528 obj = dev_priv->fbdev->fb->obj;
8529 BUG_ON(!obj);
8530
8bcd4553 8531 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8532 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8533 fb->bits_per_pixel))
d2dff872
CW
8534 return NULL;
8535
01f2c773 8536 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8537 return NULL;
8538
8539 return fb;
4520f53a
DV
8540#else
8541 return NULL;
8542#endif
d2dff872
CW
8543}
8544
d2434ab7 8545bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8546 struct drm_display_mode *mode,
51fd371b
RC
8547 struct intel_load_detect_pipe *old,
8548 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8549{
8550 struct intel_crtc *intel_crtc;
d2434ab7
DV
8551 struct intel_encoder *intel_encoder =
8552 intel_attached_encoder(connector);
79e53945 8553 struct drm_crtc *possible_crtc;
4ef69c7a 8554 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8555 struct drm_crtc *crtc = NULL;
8556 struct drm_device *dev = encoder->dev;
94352cf9 8557 struct drm_framebuffer *fb;
51fd371b
RC
8558 struct drm_mode_config *config = &dev->mode_config;
8559 int ret, i = -1;
79e53945 8560
d2dff872 8561 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8562 connector->base.id, connector->name,
8e329a03 8563 encoder->base.id, encoder->name);
d2dff872 8564
51fd371b
RC
8565retry:
8566 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8567 if (ret)
8568 goto fail_unlock;
6e9f798d 8569
79e53945
JB
8570 /*
8571 * Algorithm gets a little messy:
7a5e4805 8572 *
79e53945
JB
8573 * - if the connector already has an assigned crtc, use it (but make
8574 * sure it's on first)
7a5e4805 8575 *
79e53945
JB
8576 * - try to find the first unused crtc that can drive this connector,
8577 * and use that if we find one
79e53945
JB
8578 */
8579
8580 /* See if we already have a CRTC for this connector */
8581 if (encoder->crtc) {
8582 crtc = encoder->crtc;
8261b191 8583
51fd371b
RC
8584 ret = drm_modeset_lock(&crtc->mutex, ctx);
8585 if (ret)
8586 goto fail_unlock;
7b24056b 8587
24218aac 8588 old->dpms_mode = connector->dpms;
8261b191
CW
8589 old->load_detect_temp = false;
8590
8591 /* Make sure the crtc and connector are running */
24218aac
DV
8592 if (connector->dpms != DRM_MODE_DPMS_ON)
8593 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8594
7173188d 8595 return true;
79e53945
JB
8596 }
8597
8598 /* Find an unused one (if possible) */
70e1e0ec 8599 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8600 i++;
8601 if (!(encoder->possible_crtcs & (1 << i)))
8602 continue;
a459249c
VS
8603 if (possible_crtc->enabled)
8604 continue;
8605 /* This can occur when applying the pipe A quirk on resume. */
8606 if (to_intel_crtc(possible_crtc)->new_enabled)
8607 continue;
8608
8609 crtc = possible_crtc;
8610 break;
79e53945
JB
8611 }
8612
8613 /*
8614 * If we didn't find an unused CRTC, don't use any.
8615 */
8616 if (!crtc) {
7173188d 8617 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8618 goto fail_unlock;
79e53945
JB
8619 }
8620
51fd371b
RC
8621 ret = drm_modeset_lock(&crtc->mutex, ctx);
8622 if (ret)
8623 goto fail_unlock;
fc303101
DV
8624 intel_encoder->new_crtc = to_intel_crtc(crtc);
8625 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8626
8627 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8628 intel_crtc->new_enabled = true;
8629 intel_crtc->new_config = &intel_crtc->config;
24218aac 8630 old->dpms_mode = connector->dpms;
8261b191 8631 old->load_detect_temp = true;
d2dff872 8632 old->release_fb = NULL;
79e53945 8633
6492711d
CW
8634 if (!mode)
8635 mode = &load_detect_mode;
79e53945 8636
d2dff872
CW
8637 /* We need a framebuffer large enough to accommodate all accesses
8638 * that the plane may generate whilst we perform load detection.
8639 * We can not rely on the fbcon either being present (we get called
8640 * during its initialisation to detect all boot displays, or it may
8641 * not even exist) or that it is large enough to satisfy the
8642 * requested mode.
8643 */
94352cf9
DV
8644 fb = mode_fits_in_fbdev(dev, mode);
8645 if (fb == NULL) {
d2dff872 8646 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8647 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8648 old->release_fb = fb;
d2dff872
CW
8649 } else
8650 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8651 if (IS_ERR(fb)) {
d2dff872 8652 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8653 goto fail;
79e53945 8654 }
79e53945 8655
c0c36b94 8656 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8657 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8658 if (old->release_fb)
8659 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8660 goto fail;
79e53945 8661 }
7173188d 8662
79e53945 8663 /* let the connector get through one full cycle before testing */
9d0498a2 8664 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8665 return true;
412b61d8
VS
8666
8667 fail:
8668 intel_crtc->new_enabled = crtc->enabled;
8669 if (intel_crtc->new_enabled)
8670 intel_crtc->new_config = &intel_crtc->config;
8671 else
8672 intel_crtc->new_config = NULL;
51fd371b
RC
8673fail_unlock:
8674 if (ret == -EDEADLK) {
8675 drm_modeset_backoff(ctx);
8676 goto retry;
8677 }
8678
412b61d8 8679 return false;
79e53945
JB
8680}
8681
d2434ab7 8682void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8683 struct intel_load_detect_pipe *old)
79e53945 8684{
d2434ab7
DV
8685 struct intel_encoder *intel_encoder =
8686 intel_attached_encoder(connector);
4ef69c7a 8687 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8688 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8690
d2dff872 8691 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8692 connector->base.id, connector->name,
8e329a03 8693 encoder->base.id, encoder->name);
d2dff872 8694
8261b191 8695 if (old->load_detect_temp) {
fc303101
DV
8696 to_intel_connector(connector)->new_encoder = NULL;
8697 intel_encoder->new_crtc = NULL;
412b61d8
VS
8698 intel_crtc->new_enabled = false;
8699 intel_crtc->new_config = NULL;
fc303101 8700 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8701
36206361
DV
8702 if (old->release_fb) {
8703 drm_framebuffer_unregister_private(old->release_fb);
8704 drm_framebuffer_unreference(old->release_fb);
8705 }
d2dff872 8706
0622a53c 8707 return;
79e53945
JB
8708 }
8709
c751ce4f 8710 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8711 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8712 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8713}
8714
da4a1efa
VS
8715static int i9xx_pll_refclk(struct drm_device *dev,
8716 const struct intel_crtc_config *pipe_config)
8717{
8718 struct drm_i915_private *dev_priv = dev->dev_private;
8719 u32 dpll = pipe_config->dpll_hw_state.dpll;
8720
8721 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8722 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8723 else if (HAS_PCH_SPLIT(dev))
8724 return 120000;
8725 else if (!IS_GEN2(dev))
8726 return 96000;
8727 else
8728 return 48000;
8729}
8730
79e53945 8731/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8732static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8733 struct intel_crtc_config *pipe_config)
79e53945 8734{
f1f644dc 8735 struct drm_device *dev = crtc->base.dev;
79e53945 8736 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8737 int pipe = pipe_config->cpu_transcoder;
293623f7 8738 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8739 u32 fp;
8740 intel_clock_t clock;
da4a1efa 8741 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8742
8743 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8744 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8745 else
293623f7 8746 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8747
8748 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8749 if (IS_PINEVIEW(dev)) {
8750 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8751 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8752 } else {
8753 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8754 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8755 }
8756
a6c45cf0 8757 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8758 if (IS_PINEVIEW(dev))
8759 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8760 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8761 else
8762 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8763 DPLL_FPA01_P1_POST_DIV_SHIFT);
8764
8765 switch (dpll & DPLL_MODE_MASK) {
8766 case DPLLB_MODE_DAC_SERIAL:
8767 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8768 5 : 10;
8769 break;
8770 case DPLLB_MODE_LVDS:
8771 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8772 7 : 14;
8773 break;
8774 default:
28c97730 8775 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8776 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8777 return;
79e53945
JB
8778 }
8779
ac58c3f0 8780 if (IS_PINEVIEW(dev))
da4a1efa 8781 pineview_clock(refclk, &clock);
ac58c3f0 8782 else
da4a1efa 8783 i9xx_clock(refclk, &clock);
79e53945 8784 } else {
0fb58223 8785 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8786 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8787
8788 if (is_lvds) {
8789 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8790 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8791
8792 if (lvds & LVDS_CLKB_POWER_UP)
8793 clock.p2 = 7;
8794 else
8795 clock.p2 = 14;
79e53945
JB
8796 } else {
8797 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8798 clock.p1 = 2;
8799 else {
8800 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8801 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8802 }
8803 if (dpll & PLL_P2_DIVIDE_BY_4)
8804 clock.p2 = 4;
8805 else
8806 clock.p2 = 2;
79e53945 8807 }
da4a1efa
VS
8808
8809 i9xx_clock(refclk, &clock);
79e53945
JB
8810 }
8811
18442d08
VS
8812 /*
8813 * This value includes pixel_multiplier. We will use
241bfc38 8814 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8815 * encoder's get_config() function.
8816 */
8817 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8818}
8819
6878da05
VS
8820int intel_dotclock_calculate(int link_freq,
8821 const struct intel_link_m_n *m_n)
f1f644dc 8822{
f1f644dc
JB
8823 /*
8824 * The calculation for the data clock is:
1041a02f 8825 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8826 * But we want to avoid losing precison if possible, so:
1041a02f 8827 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8828 *
8829 * and the link clock is simpler:
1041a02f 8830 * link_clock = (m * link_clock) / n
f1f644dc
JB
8831 */
8832
6878da05
VS
8833 if (!m_n->link_n)
8834 return 0;
f1f644dc 8835
6878da05
VS
8836 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8837}
f1f644dc 8838
18442d08
VS
8839static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8840 struct intel_crtc_config *pipe_config)
6878da05
VS
8841{
8842 struct drm_device *dev = crtc->base.dev;
79e53945 8843
18442d08
VS
8844 /* read out port_clock from the DPLL */
8845 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8846
f1f644dc 8847 /*
18442d08 8848 * This value does not include pixel_multiplier.
241bfc38 8849 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8850 * agree once we know their relationship in the encoder's
8851 * get_config() function.
79e53945 8852 */
241bfc38 8853 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8854 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8855 &pipe_config->fdi_m_n);
79e53945
JB
8856}
8857
8858/** Returns the currently programmed mode of the given pipe. */
8859struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8860 struct drm_crtc *crtc)
8861{
548f245b 8862 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8864 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8865 struct drm_display_mode *mode;
f1f644dc 8866 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8867 int htot = I915_READ(HTOTAL(cpu_transcoder));
8868 int hsync = I915_READ(HSYNC(cpu_transcoder));
8869 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8870 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8871 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8872
8873 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8874 if (!mode)
8875 return NULL;
8876
f1f644dc
JB
8877 /*
8878 * Construct a pipe_config sufficient for getting the clock info
8879 * back out of crtc_clock_get.
8880 *
8881 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8882 * to use a real value here instead.
8883 */
293623f7 8884 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8885 pipe_config.pixel_multiplier = 1;
293623f7
VS
8886 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8887 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8888 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8889 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8890
773ae034 8891 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8892 mode->hdisplay = (htot & 0xffff) + 1;
8893 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8894 mode->hsync_start = (hsync & 0xffff) + 1;
8895 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8896 mode->vdisplay = (vtot & 0xffff) + 1;
8897 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8898 mode->vsync_start = (vsync & 0xffff) + 1;
8899 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8900
8901 drm_mode_set_name(mode);
79e53945
JB
8902
8903 return mode;
8904}
8905
cc36513c
DV
8906static void intel_increase_pllclock(struct drm_device *dev,
8907 enum pipe pipe)
652c393a 8908{
fbee40df 8909 struct drm_i915_private *dev_priv = dev->dev_private;
dbdc6479
JB
8910 int dpll_reg = DPLL(pipe);
8911 int dpll;
652c393a 8912
baff296c 8913 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8914 return;
8915
8916 if (!dev_priv->lvds_downclock_avail)
8917 return;
8918
dbdc6479 8919 dpll = I915_READ(dpll_reg);
652c393a 8920 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8921 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8922
8ac5a6d5 8923 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8924
8925 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8926 I915_WRITE(dpll_reg, dpll);
9d0498a2 8927 intel_wait_for_vblank(dev, pipe);
dbdc6479 8928
652c393a
JB
8929 dpll = I915_READ(dpll_reg);
8930 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8931 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8932 }
652c393a
JB
8933}
8934
8935static void intel_decrease_pllclock(struct drm_crtc *crtc)
8936{
8937 struct drm_device *dev = crtc->dev;
fbee40df 8938 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8940
baff296c 8941 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8942 return;
8943
8944 if (!dev_priv->lvds_downclock_avail)
8945 return;
8946
8947 /*
8948 * Since this is called by a timer, we should never get here in
8949 * the manual case.
8950 */
8951 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8952 int pipe = intel_crtc->pipe;
8953 int dpll_reg = DPLL(pipe);
8954 int dpll;
f6e5b160 8955
44d98a61 8956 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8957
8ac5a6d5 8958 assert_panel_unlocked(dev_priv, pipe);
652c393a 8959
dc257cf1 8960 dpll = I915_READ(dpll_reg);
652c393a
JB
8961 dpll |= DISPLAY_RATE_SELECT_FPA1;
8962 I915_WRITE(dpll_reg, dpll);
9d0498a2 8963 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8964 dpll = I915_READ(dpll_reg);
8965 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8966 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8967 }
8968
8969}
8970
f047e395
CW
8971void intel_mark_busy(struct drm_device *dev)
8972{
c67a470b
PZ
8973 struct drm_i915_private *dev_priv = dev->dev_private;
8974
f62a0076
CW
8975 if (dev_priv->mm.busy)
8976 return;
8977
43694d69 8978 intel_runtime_pm_get(dev_priv);
c67a470b 8979 i915_update_gfx_val(dev_priv);
f62a0076 8980 dev_priv->mm.busy = true;
f047e395
CW
8981}
8982
8983void intel_mark_idle(struct drm_device *dev)
652c393a 8984{
c67a470b 8985 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8986 struct drm_crtc *crtc;
652c393a 8987
f62a0076
CW
8988 if (!dev_priv->mm.busy)
8989 return;
8990
8991 dev_priv->mm.busy = false;
8992
d330a953 8993 if (!i915.powersave)
bb4cdd53 8994 goto out;
652c393a 8995
70e1e0ec 8996 for_each_crtc(dev, crtc) {
f4510a27 8997 if (!crtc->primary->fb)
652c393a
JB
8998 continue;
8999
725a5b54 9000 intel_decrease_pllclock(crtc);
652c393a 9001 }
b29c19b6 9002
3d13ef2e 9003 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9004 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9005
9006out:
43694d69 9007 intel_runtime_pm_put(dev_priv);
652c393a
JB
9008}
9009
7c8f8a70 9010
f99d7069
DV
9011/**
9012 * intel_mark_fb_busy - mark given planes as busy
9013 * @dev: DRM device
9014 * @frontbuffer_bits: bits for the affected planes
9015 * @ring: optional ring for asynchronous commands
9016 *
9017 * This function gets called every time the screen contents change. It can be
9018 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9019 */
9020static void intel_mark_fb_busy(struct drm_device *dev,
9021 unsigned frontbuffer_bits,
9022 struct intel_engine_cs *ring)
652c393a 9023{
055e393f 9024 struct drm_i915_private *dev_priv = dev->dev_private;
cc36513c 9025 enum pipe pipe;
652c393a 9026
d330a953 9027 if (!i915.powersave)
acb87dfb
CW
9028 return;
9029
055e393f 9030 for_each_pipe(dev_priv, pipe) {
f99d7069 9031 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
c65355bb
CW
9032 continue;
9033
cc36513c 9034 intel_increase_pllclock(dev, pipe);
c65355bb
CW
9035 if (ring && intel_fbc_enabled(dev))
9036 ring->fbc_dirty = true;
652c393a
JB
9037 }
9038}
9039
f99d7069
DV
9040/**
9041 * intel_fb_obj_invalidate - invalidate frontbuffer object
9042 * @obj: GEM object to invalidate
9043 * @ring: set for asynchronous rendering
9044 *
9045 * This function gets called every time rendering on the given object starts and
9046 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9047 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9048 * until the rendering completes or a flip on this frontbuffer plane is
9049 * scheduled.
9050 */
9051void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9052 struct intel_engine_cs *ring)
9053{
9054 struct drm_device *dev = obj->base.dev;
9055 struct drm_i915_private *dev_priv = dev->dev_private;
9056
9057 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9058
9059 if (!obj->frontbuffer_bits)
9060 return;
9061
9062 if (ring) {
9063 mutex_lock(&dev_priv->fb_tracking.lock);
9064 dev_priv->fb_tracking.busy_bits
9065 |= obj->frontbuffer_bits;
9066 dev_priv->fb_tracking.flip_bits
9067 &= ~obj->frontbuffer_bits;
9068 mutex_unlock(&dev_priv->fb_tracking.lock);
9069 }
9070
9071 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9072
9ca15301 9073 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
f99d7069
DV
9074}
9075
9076/**
9077 * intel_frontbuffer_flush - flush frontbuffer
9078 * @dev: DRM device
9079 * @frontbuffer_bits: frontbuffer plane tracking bits
9080 *
9081 * This function gets called every time rendering on the given planes has
9082 * completed and frontbuffer caching can be started again. Flushes will get
9083 * delayed if they're blocked by some oustanding asynchronous rendering.
9084 *
9085 * Can be called without any locks held.
9086 */
9087void intel_frontbuffer_flush(struct drm_device *dev,
9088 unsigned frontbuffer_bits)
9089{
9090 struct drm_i915_private *dev_priv = dev->dev_private;
9091
9092 /* Delay flushing when rings are still busy.*/
9093 mutex_lock(&dev_priv->fb_tracking.lock);
9094 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9095 mutex_unlock(&dev_priv->fb_tracking.lock);
9096
9097 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9098
9ca15301 9099 intel_edp_psr_flush(dev, frontbuffer_bits);
c5ad011d 9100
c317adcd
VS
9101 /*
9102 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9103 * needs to be reworked into a proper frontbuffer tracking scheme like
9104 * psr employs.
9105 */
9106 if (IS_BROADWELL(dev))
c5ad011d 9107 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
f99d7069
DV
9108}
9109
9110/**
9111 * intel_fb_obj_flush - flush frontbuffer object
9112 * @obj: GEM object to flush
9113 * @retire: set when retiring asynchronous rendering
9114 *
9115 * This function gets called every time rendering on the given object has
9116 * completed and frontbuffer caching can be started again. If @retire is true
9117 * then any delayed flushes will be unblocked.
9118 */
9119void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9120 bool retire)
9121{
9122 struct drm_device *dev = obj->base.dev;
9123 struct drm_i915_private *dev_priv = dev->dev_private;
9124 unsigned frontbuffer_bits;
9125
9126 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9127
9128 if (!obj->frontbuffer_bits)
9129 return;
9130
9131 frontbuffer_bits = obj->frontbuffer_bits;
9132
9133 if (retire) {
9134 mutex_lock(&dev_priv->fb_tracking.lock);
9135 /* Filter out new bits since rendering started. */
9136 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9137
9138 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9139 mutex_unlock(&dev_priv->fb_tracking.lock);
9140 }
9141
9142 intel_frontbuffer_flush(dev, frontbuffer_bits);
9143}
9144
9145/**
9146 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9147 * @dev: DRM device
9148 * @frontbuffer_bits: frontbuffer plane tracking bits
9149 *
9150 * This function gets called after scheduling a flip on @obj. The actual
9151 * frontbuffer flushing will be delayed until completion is signalled with
9152 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9153 * flush will be cancelled.
9154 *
9155 * Can be called without any locks held.
9156 */
9157void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9158 unsigned frontbuffer_bits)
9159{
9160 struct drm_i915_private *dev_priv = dev->dev_private;
9161
9162 mutex_lock(&dev_priv->fb_tracking.lock);
9163 dev_priv->fb_tracking.flip_bits
9164 |= frontbuffer_bits;
9165 mutex_unlock(&dev_priv->fb_tracking.lock);
9166}
9167
9168/**
9169 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9170 * @dev: DRM device
9171 * @frontbuffer_bits: frontbuffer plane tracking bits
9172 *
9173 * This function gets called after the flip has been latched and will complete
9174 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9175 *
9176 * Can be called without any locks held.
9177 */
9178void intel_frontbuffer_flip_complete(struct drm_device *dev,
9179 unsigned frontbuffer_bits)
9180{
9181 struct drm_i915_private *dev_priv = dev->dev_private;
9182
9183 mutex_lock(&dev_priv->fb_tracking.lock);
9184 /* Mask any cancelled flips. */
9185 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9186 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9187 mutex_unlock(&dev_priv->fb_tracking.lock);
9188
9189 intel_frontbuffer_flush(dev, frontbuffer_bits);
9190}
9191
79e53945
JB
9192static void intel_crtc_destroy(struct drm_crtc *crtc)
9193{
9194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9195 struct drm_device *dev = crtc->dev;
9196 struct intel_unpin_work *work;
9197 unsigned long flags;
9198
9199 spin_lock_irqsave(&dev->event_lock, flags);
9200 work = intel_crtc->unpin_work;
9201 intel_crtc->unpin_work = NULL;
9202 spin_unlock_irqrestore(&dev->event_lock, flags);
9203
9204 if (work) {
9205 cancel_work_sync(&work->work);
9206 kfree(work);
9207 }
79e53945
JB
9208
9209 drm_crtc_cleanup(crtc);
67e77c5a 9210
79e53945
JB
9211 kfree(intel_crtc);
9212}
9213
6b95a207
KH
9214static void intel_unpin_work_fn(struct work_struct *__work)
9215{
9216 struct intel_unpin_work *work =
9217 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9218 struct drm_device *dev = work->crtc->dev;
f99d7069 9219 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9220
b4a98e57 9221 mutex_lock(&dev->struct_mutex);
1690e1eb 9222 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9223 drm_gem_object_unreference(&work->pending_flip_obj->base);
9224 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9225
b4a98e57
CW
9226 intel_update_fbc(dev);
9227 mutex_unlock(&dev->struct_mutex);
9228
f99d7069
DV
9229 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9230
b4a98e57
CW
9231 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9232 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9233
6b95a207
KH
9234 kfree(work);
9235}
9236
1afe3e9d 9237static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9238 struct drm_crtc *crtc)
6b95a207 9239{
fbee40df 9240 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9242 struct intel_unpin_work *work;
6b95a207
KH
9243 unsigned long flags;
9244
9245 /* Ignore early vblank irqs */
9246 if (intel_crtc == NULL)
9247 return;
9248
9249 spin_lock_irqsave(&dev->event_lock, flags);
9250 work = intel_crtc->unpin_work;
e7d841ca
CW
9251
9252 /* Ensure we don't miss a work->pending update ... */
9253 smp_rmb();
9254
9255 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9256 spin_unlock_irqrestore(&dev->event_lock, flags);
9257 return;
9258 }
9259
e7d841ca
CW
9260 /* and that the unpin work is consistent wrt ->pending. */
9261 smp_rmb();
9262
6b95a207 9263 intel_crtc->unpin_work = NULL;
6b95a207 9264
45a066eb
RC
9265 if (work->event)
9266 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 9267
87b6b101 9268 drm_crtc_vblank_put(crtc);
0af7e4df 9269
6b95a207
KH
9270 spin_unlock_irqrestore(&dev->event_lock, flags);
9271
2c10d571 9272 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
9273
9274 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
9275
9276 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
9277}
9278
1afe3e9d
JB
9279void intel_finish_page_flip(struct drm_device *dev, int pipe)
9280{
fbee40df 9281 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9282 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9283
49b14a5c 9284 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9285}
9286
9287void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9288{
fbee40df 9289 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9290 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9291
49b14a5c 9292 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9293}
9294
75f7f3ec
VS
9295/* Is 'a' after or equal to 'b'? */
9296static bool g4x_flip_count_after_eq(u32 a, u32 b)
9297{
9298 return !((a - b) & 0x80000000);
9299}
9300
9301static bool page_flip_finished(struct intel_crtc *crtc)
9302{
9303 struct drm_device *dev = crtc->base.dev;
9304 struct drm_i915_private *dev_priv = dev->dev_private;
9305
9306 /*
9307 * The relevant registers doen't exist on pre-ctg.
9308 * As the flip done interrupt doesn't trigger for mmio
9309 * flips on gmch platforms, a flip count check isn't
9310 * really needed there. But since ctg has the registers,
9311 * include it in the check anyway.
9312 */
9313 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9314 return true;
9315
9316 /*
9317 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9318 * used the same base address. In that case the mmio flip might
9319 * have completed, but the CS hasn't even executed the flip yet.
9320 *
9321 * A flip count check isn't enough as the CS might have updated
9322 * the base address just after start of vblank, but before we
9323 * managed to process the interrupt. This means we'd complete the
9324 * CS flip too soon.
9325 *
9326 * Combining both checks should get us a good enough result. It may
9327 * still happen that the CS flip has been executed, but has not
9328 * yet actually completed. But in case the base address is the same
9329 * anyway, we don't really care.
9330 */
9331 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9332 crtc->unpin_work->gtt_offset &&
9333 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9334 crtc->unpin_work->flip_count);
9335}
9336
6b95a207
KH
9337void intel_prepare_page_flip(struct drm_device *dev, int plane)
9338{
fbee40df 9339 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9340 struct intel_crtc *intel_crtc =
9341 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9342 unsigned long flags;
9343
e7d841ca
CW
9344 /* NB: An MMIO update of the plane base pointer will also
9345 * generate a page-flip completion irq, i.e. every modeset
9346 * is also accompanied by a spurious intel_prepare_page_flip().
9347 */
6b95a207 9348 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9349 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9350 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9351 spin_unlock_irqrestore(&dev->event_lock, flags);
9352}
9353
eba905b2 9354static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9355{
9356 /* Ensure that the work item is consistent when activating it ... */
9357 smp_wmb();
9358 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9359 /* and that it is marked active as soon as the irq could fire. */
9360 smp_wmb();
9361}
9362
8c9f3aaf
JB
9363static int intel_gen2_queue_flip(struct drm_device *dev,
9364 struct drm_crtc *crtc,
9365 struct drm_framebuffer *fb,
ed8d1975 9366 struct drm_i915_gem_object *obj,
a4872ba6 9367 struct intel_engine_cs *ring,
ed8d1975 9368 uint32_t flags)
8c9f3aaf 9369{
8c9f3aaf 9370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9371 u32 flip_mask;
9372 int ret;
9373
6d90c952 9374 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9375 if (ret)
4fa62c89 9376 return ret;
8c9f3aaf
JB
9377
9378 /* Can't queue multiple flips, so wait for the previous
9379 * one to finish before executing the next.
9380 */
9381 if (intel_crtc->plane)
9382 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9383 else
9384 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9385 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9386 intel_ring_emit(ring, MI_NOOP);
9387 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9388 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9389 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9390 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9391 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9392
9393 intel_mark_page_flip_active(intel_crtc);
09246732 9394 __intel_ring_advance(ring);
83d4092b 9395 return 0;
8c9f3aaf
JB
9396}
9397
9398static int intel_gen3_queue_flip(struct drm_device *dev,
9399 struct drm_crtc *crtc,
9400 struct drm_framebuffer *fb,
ed8d1975 9401 struct drm_i915_gem_object *obj,
a4872ba6 9402 struct intel_engine_cs *ring,
ed8d1975 9403 uint32_t flags)
8c9f3aaf 9404{
8c9f3aaf 9405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9406 u32 flip_mask;
9407 int ret;
9408
6d90c952 9409 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9410 if (ret)
4fa62c89 9411 return ret;
8c9f3aaf
JB
9412
9413 if (intel_crtc->plane)
9414 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9415 else
9416 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9417 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9418 intel_ring_emit(ring, MI_NOOP);
9419 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9420 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9421 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9422 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9423 intel_ring_emit(ring, MI_NOOP);
9424
e7d841ca 9425 intel_mark_page_flip_active(intel_crtc);
09246732 9426 __intel_ring_advance(ring);
83d4092b 9427 return 0;
8c9f3aaf
JB
9428}
9429
9430static int intel_gen4_queue_flip(struct drm_device *dev,
9431 struct drm_crtc *crtc,
9432 struct drm_framebuffer *fb,
ed8d1975 9433 struct drm_i915_gem_object *obj,
a4872ba6 9434 struct intel_engine_cs *ring,
ed8d1975 9435 uint32_t flags)
8c9f3aaf
JB
9436{
9437 struct drm_i915_private *dev_priv = dev->dev_private;
9438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9439 uint32_t pf, pipesrc;
9440 int ret;
9441
6d90c952 9442 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9443 if (ret)
4fa62c89 9444 return ret;
8c9f3aaf
JB
9445
9446 /* i965+ uses the linear or tiled offsets from the
9447 * Display Registers (which do not change across a page-flip)
9448 * so we need only reprogram the base address.
9449 */
6d90c952
DV
9450 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9451 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9452 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9453 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9454 obj->tiling_mode);
8c9f3aaf
JB
9455
9456 /* XXX Enabling the panel-fitter across page-flip is so far
9457 * untested on non-native modes, so ignore it for now.
9458 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9459 */
9460 pf = 0;
9461 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9462 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9463
9464 intel_mark_page_flip_active(intel_crtc);
09246732 9465 __intel_ring_advance(ring);
83d4092b 9466 return 0;
8c9f3aaf
JB
9467}
9468
9469static int intel_gen6_queue_flip(struct drm_device *dev,
9470 struct drm_crtc *crtc,
9471 struct drm_framebuffer *fb,
ed8d1975 9472 struct drm_i915_gem_object *obj,
a4872ba6 9473 struct intel_engine_cs *ring,
ed8d1975 9474 uint32_t flags)
8c9f3aaf
JB
9475{
9476 struct drm_i915_private *dev_priv = dev->dev_private;
9477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9478 uint32_t pf, pipesrc;
9479 int ret;
9480
6d90c952 9481 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9482 if (ret)
4fa62c89 9483 return ret;
8c9f3aaf 9484
6d90c952
DV
9485 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9486 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9487 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9488 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9489
dc257cf1
DV
9490 /* Contrary to the suggestions in the documentation,
9491 * "Enable Panel Fitter" does not seem to be required when page
9492 * flipping with a non-native mode, and worse causes a normal
9493 * modeset to fail.
9494 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9495 */
9496 pf = 0;
8c9f3aaf 9497 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9498 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9499
9500 intel_mark_page_flip_active(intel_crtc);
09246732 9501 __intel_ring_advance(ring);
83d4092b 9502 return 0;
8c9f3aaf
JB
9503}
9504
7c9017e5
JB
9505static int intel_gen7_queue_flip(struct drm_device *dev,
9506 struct drm_crtc *crtc,
9507 struct drm_framebuffer *fb,
ed8d1975 9508 struct drm_i915_gem_object *obj,
a4872ba6 9509 struct intel_engine_cs *ring,
ed8d1975 9510 uint32_t flags)
7c9017e5 9511{
7c9017e5 9512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9513 uint32_t plane_bit = 0;
ffe74d75
CW
9514 int len, ret;
9515
eba905b2 9516 switch (intel_crtc->plane) {
cb05d8de
DV
9517 case PLANE_A:
9518 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9519 break;
9520 case PLANE_B:
9521 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9522 break;
9523 case PLANE_C:
9524 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9525 break;
9526 default:
9527 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9528 return -ENODEV;
cb05d8de
DV
9529 }
9530
ffe74d75 9531 len = 4;
f476828a 9532 if (ring->id == RCS) {
ffe74d75 9533 len += 6;
f476828a
DL
9534 /*
9535 * On Gen 8, SRM is now taking an extra dword to accommodate
9536 * 48bits addresses, and we need a NOOP for the batch size to
9537 * stay even.
9538 */
9539 if (IS_GEN8(dev))
9540 len += 2;
9541 }
ffe74d75 9542
f66fab8e
VS
9543 /*
9544 * BSpec MI_DISPLAY_FLIP for IVB:
9545 * "The full packet must be contained within the same cache line."
9546 *
9547 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9548 * cacheline, if we ever start emitting more commands before
9549 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9550 * then do the cacheline alignment, and finally emit the
9551 * MI_DISPLAY_FLIP.
9552 */
9553 ret = intel_ring_cacheline_align(ring);
9554 if (ret)
4fa62c89 9555 return ret;
f66fab8e 9556
ffe74d75 9557 ret = intel_ring_begin(ring, len);
7c9017e5 9558 if (ret)
4fa62c89 9559 return ret;
7c9017e5 9560
ffe74d75
CW
9561 /* Unmask the flip-done completion message. Note that the bspec says that
9562 * we should do this for both the BCS and RCS, and that we must not unmask
9563 * more than one flip event at any time (or ensure that one flip message
9564 * can be sent by waiting for flip-done prior to queueing new flips).
9565 * Experimentation says that BCS works despite DERRMR masking all
9566 * flip-done completion events and that unmasking all planes at once
9567 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9568 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9569 */
9570 if (ring->id == RCS) {
9571 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9572 intel_ring_emit(ring, DERRMR);
9573 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9574 DERRMR_PIPEB_PRI_FLIP_DONE |
9575 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9576 if (IS_GEN8(dev))
9577 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9578 MI_SRM_LRM_GLOBAL_GTT);
9579 else
9580 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9581 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9582 intel_ring_emit(ring, DERRMR);
9583 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9584 if (IS_GEN8(dev)) {
9585 intel_ring_emit(ring, 0);
9586 intel_ring_emit(ring, MI_NOOP);
9587 }
ffe74d75
CW
9588 }
9589
cb05d8de 9590 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9591 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9592 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9593 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9594
9595 intel_mark_page_flip_active(intel_crtc);
09246732 9596 __intel_ring_advance(ring);
83d4092b 9597 return 0;
7c9017e5
JB
9598}
9599
84c33a64
SG
9600static bool use_mmio_flip(struct intel_engine_cs *ring,
9601 struct drm_i915_gem_object *obj)
9602{
9603 /*
9604 * This is not being used for older platforms, because
9605 * non-availability of flip done interrupt forces us to use
9606 * CS flips. Older platforms derive flip done using some clever
9607 * tricks involving the flip_pending status bits and vblank irqs.
9608 * So using MMIO flips there would disrupt this mechanism.
9609 */
9610
8e09bf83
CW
9611 if (ring == NULL)
9612 return true;
9613
84c33a64
SG
9614 if (INTEL_INFO(ring->dev)->gen < 5)
9615 return false;
9616
9617 if (i915.use_mmio_flip < 0)
9618 return false;
9619 else if (i915.use_mmio_flip > 0)
9620 return true;
14bf993e
OM
9621 else if (i915.enable_execlists)
9622 return true;
84c33a64
SG
9623 else
9624 return ring != obj->ring;
9625}
9626
9627static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9628{
9629 struct drm_device *dev = intel_crtc->base.dev;
9630 struct drm_i915_private *dev_priv = dev->dev_private;
9631 struct intel_framebuffer *intel_fb =
9632 to_intel_framebuffer(intel_crtc->base.primary->fb);
9633 struct drm_i915_gem_object *obj = intel_fb->obj;
9634 u32 dspcntr;
9635 u32 reg;
9636
9637 intel_mark_page_flip_active(intel_crtc);
9638
9639 reg = DSPCNTR(intel_crtc->plane);
9640 dspcntr = I915_READ(reg);
9641
9642 if (INTEL_INFO(dev)->gen >= 4) {
9643 if (obj->tiling_mode != I915_TILING_NONE)
9644 dspcntr |= DISPPLANE_TILED;
9645 else
9646 dspcntr &= ~DISPPLANE_TILED;
9647 }
9648 I915_WRITE(reg, dspcntr);
9649
9650 I915_WRITE(DSPSURF(intel_crtc->plane),
9651 intel_crtc->unpin_work->gtt_offset);
9652 POSTING_READ(DSPSURF(intel_crtc->plane));
9653}
9654
9655static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9656{
9657 struct intel_engine_cs *ring;
9658 int ret;
9659
9660 lockdep_assert_held(&obj->base.dev->struct_mutex);
9661
9662 if (!obj->last_write_seqno)
9663 return 0;
9664
9665 ring = obj->ring;
9666
9667 if (i915_seqno_passed(ring->get_seqno(ring, true),
9668 obj->last_write_seqno))
9669 return 0;
9670
9671 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9672 if (ret)
9673 return ret;
9674
9675 if (WARN_ON(!ring->irq_get(ring)))
9676 return 0;
9677
9678 return 1;
9679}
9680
9681void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9682{
9683 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9684 struct intel_crtc *intel_crtc;
9685 unsigned long irq_flags;
9686 u32 seqno;
9687
9688 seqno = ring->get_seqno(ring, false);
9689
9690 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9691 for_each_intel_crtc(ring->dev, intel_crtc) {
9692 struct intel_mmio_flip *mmio_flip;
9693
9694 mmio_flip = &intel_crtc->mmio_flip;
9695 if (mmio_flip->seqno == 0)
9696 continue;
9697
9698 if (ring->id != mmio_flip->ring_id)
9699 continue;
9700
9701 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9702 intel_do_mmio_flip(intel_crtc);
9703 mmio_flip->seqno = 0;
9704 ring->irq_put(ring);
9705 }
9706 }
9707 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9708}
9709
9710static int intel_queue_mmio_flip(struct drm_device *dev,
9711 struct drm_crtc *crtc,
9712 struct drm_framebuffer *fb,
9713 struct drm_i915_gem_object *obj,
9714 struct intel_engine_cs *ring,
9715 uint32_t flags)
9716{
9717 struct drm_i915_private *dev_priv = dev->dev_private;
9718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9719 unsigned long irq_flags;
9720 int ret;
9721
9722 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9723 return -EBUSY;
9724
9725 ret = intel_postpone_flip(obj);
9726 if (ret < 0)
9727 return ret;
9728 if (ret == 0) {
9729 intel_do_mmio_flip(intel_crtc);
9730 return 0;
9731 }
9732
9733 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9734 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9735 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9736 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9737
9738 /*
9739 * Double check to catch cases where irq fired before
9740 * mmio flip data was ready
9741 */
9742 intel_notify_mmio_flip(obj->ring);
9743 return 0;
9744}
9745
8c9f3aaf
JB
9746static int intel_default_queue_flip(struct drm_device *dev,
9747 struct drm_crtc *crtc,
9748 struct drm_framebuffer *fb,
ed8d1975 9749 struct drm_i915_gem_object *obj,
a4872ba6 9750 struct intel_engine_cs *ring,
ed8d1975 9751 uint32_t flags)
8c9f3aaf
JB
9752{
9753 return -ENODEV;
9754}
9755
6b95a207
KH
9756static int intel_crtc_page_flip(struct drm_crtc *crtc,
9757 struct drm_framebuffer *fb,
ed8d1975
KP
9758 struct drm_pending_vblank_event *event,
9759 uint32_t page_flip_flags)
6b95a207
KH
9760{
9761 struct drm_device *dev = crtc->dev;
9762 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9763 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9764 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9766 enum pipe pipe = intel_crtc->pipe;
6b95a207 9767 struct intel_unpin_work *work;
a4872ba6 9768 struct intel_engine_cs *ring;
8c9f3aaf 9769 unsigned long flags;
52e68630 9770 int ret;
6b95a207 9771
c76bb61a
DS
9772 //trigger software GT busyness calculation
9773 gen8_flip_interrupt(dev);
9774
2ff8fde1
MR
9775 /*
9776 * drm_mode_page_flip_ioctl() should already catch this, but double
9777 * check to be safe. In the future we may enable pageflipping from
9778 * a disabled primary plane.
9779 */
9780 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9781 return -EBUSY;
9782
e6a595d2 9783 /* Can't change pixel format via MI display flips. */
f4510a27 9784 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9785 return -EINVAL;
9786
9787 /*
9788 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9789 * Note that pitch changes could also affect these register.
9790 */
9791 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9792 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9793 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9794 return -EINVAL;
9795
f900db47
CW
9796 if (i915_terminally_wedged(&dev_priv->gpu_error))
9797 goto out_hang;
9798
b14c5679 9799 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9800 if (work == NULL)
9801 return -ENOMEM;
9802
6b95a207 9803 work->event = event;
b4a98e57 9804 work->crtc = crtc;
2ff8fde1 9805 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
9806 INIT_WORK(&work->work, intel_unpin_work_fn);
9807
87b6b101 9808 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9809 if (ret)
9810 goto free_work;
9811
6b95a207
KH
9812 /* We borrow the event spin lock for protecting unpin_work */
9813 spin_lock_irqsave(&dev->event_lock, flags);
9814 if (intel_crtc->unpin_work) {
9815 spin_unlock_irqrestore(&dev->event_lock, flags);
9816 kfree(work);
87b6b101 9817 drm_crtc_vblank_put(crtc);
468f0b44
CW
9818
9819 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
9820 return -EBUSY;
9821 }
9822 intel_crtc->unpin_work = work;
9823 spin_unlock_irqrestore(&dev->event_lock, flags);
9824
b4a98e57
CW
9825 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9826 flush_workqueue(dev_priv->wq);
9827
79158103
CW
9828 ret = i915_mutex_lock_interruptible(dev);
9829 if (ret)
9830 goto cleanup;
6b95a207 9831
75dfca80 9832 /* Reference the objects for the scheduled work. */
05394f39
CW
9833 drm_gem_object_reference(&work->old_fb_obj->base);
9834 drm_gem_object_reference(&obj->base);
6b95a207 9835
f4510a27 9836 crtc->primary->fb = fb;
96b099fd 9837
e1f99ce6 9838 work->pending_flip_obj = obj;
e1f99ce6 9839
4e5359cd
SF
9840 work->enable_stall_check = true;
9841
b4a98e57 9842 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9843 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9844
75f7f3ec 9845 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9846 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9847
4fa62c89
VS
9848 if (IS_VALLEYVIEW(dev)) {
9849 ring = &dev_priv->ring[BCS];
8e09bf83
CW
9850 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9851 /* vlv: DISPLAY_FLIP fails to change tiling */
9852 ring = NULL;
2a92d5bc
CW
9853 } else if (IS_IVYBRIDGE(dev)) {
9854 ring = &dev_priv->ring[BCS];
4fa62c89
VS
9855 } else if (INTEL_INFO(dev)->gen >= 7) {
9856 ring = obj->ring;
9857 if (ring == NULL || ring->id != RCS)
9858 ring = &dev_priv->ring[BCS];
9859 } else {
9860 ring = &dev_priv->ring[RCS];
9861 }
9862
9863 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9864 if (ret)
9865 goto cleanup_pending;
6b95a207 9866
4fa62c89
VS
9867 work->gtt_offset =
9868 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9869
84c33a64
SG
9870 if (use_mmio_flip(ring, obj))
9871 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9872 page_flip_flags);
9873 else
9874 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
9875 page_flip_flags);
4fa62c89
VS
9876 if (ret)
9877 goto cleanup_unpin;
9878
a071fa00
DV
9879 i915_gem_track_fb(work->old_fb_obj, obj,
9880 INTEL_FRONTBUFFER_PRIMARY(pipe));
9881
7782de3b 9882 intel_disable_fbc(dev);
f99d7069 9883 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9884 mutex_unlock(&dev->struct_mutex);
9885
e5510fac
JB
9886 trace_i915_flip_request(intel_crtc->plane, obj);
9887
6b95a207 9888 return 0;
96b099fd 9889
4fa62c89
VS
9890cleanup_unpin:
9891 intel_unpin_fb_obj(obj);
8c9f3aaf 9892cleanup_pending:
b4a98e57 9893 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9894 crtc->primary->fb = old_fb;
05394f39
CW
9895 drm_gem_object_unreference(&work->old_fb_obj->base);
9896 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9897 mutex_unlock(&dev->struct_mutex);
9898
79158103 9899cleanup:
96b099fd
CW
9900 spin_lock_irqsave(&dev->event_lock, flags);
9901 intel_crtc->unpin_work = NULL;
9902 spin_unlock_irqrestore(&dev->event_lock, flags);
9903
87b6b101 9904 drm_crtc_vblank_put(crtc);
7317c75e 9905free_work:
96b099fd
CW
9906 kfree(work);
9907
f900db47
CW
9908 if (ret == -EIO) {
9909out_hang:
9910 intel_crtc_wait_for_pending_flips(crtc);
9911 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9912 if (ret == 0 && event)
a071fa00 9913 drm_send_vblank_event(dev, pipe, event);
f900db47 9914 }
96b099fd 9915 return ret;
6b95a207
KH
9916}
9917
f6e5b160 9918static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9919 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9920 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9921};
9922
9a935856
DV
9923/**
9924 * intel_modeset_update_staged_output_state
9925 *
9926 * Updates the staged output configuration state, e.g. after we've read out the
9927 * current hw state.
9928 */
9929static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9930{
7668851f 9931 struct intel_crtc *crtc;
9a935856
DV
9932 struct intel_encoder *encoder;
9933 struct intel_connector *connector;
f6e5b160 9934
9a935856
DV
9935 list_for_each_entry(connector, &dev->mode_config.connector_list,
9936 base.head) {
9937 connector->new_encoder =
9938 to_intel_encoder(connector->base.encoder);
9939 }
f6e5b160 9940
b2784e15 9941 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9942 encoder->new_crtc =
9943 to_intel_crtc(encoder->base.crtc);
9944 }
7668851f 9945
d3fcc808 9946 for_each_intel_crtc(dev, crtc) {
7668851f 9947 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9948
9949 if (crtc->new_enabled)
9950 crtc->new_config = &crtc->config;
9951 else
9952 crtc->new_config = NULL;
7668851f 9953 }
f6e5b160
CW
9954}
9955
9a935856
DV
9956/**
9957 * intel_modeset_commit_output_state
9958 *
9959 * This function copies the stage display pipe configuration to the real one.
9960 */
9961static void intel_modeset_commit_output_state(struct drm_device *dev)
9962{
7668851f 9963 struct intel_crtc *crtc;
9a935856
DV
9964 struct intel_encoder *encoder;
9965 struct intel_connector *connector;
f6e5b160 9966
9a935856
DV
9967 list_for_each_entry(connector, &dev->mode_config.connector_list,
9968 base.head) {
9969 connector->base.encoder = &connector->new_encoder->base;
9970 }
f6e5b160 9971
b2784e15 9972 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9973 encoder->base.crtc = &encoder->new_crtc->base;
9974 }
7668851f 9975
d3fcc808 9976 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9977 crtc->base.enabled = crtc->new_enabled;
9978 }
9a935856
DV
9979}
9980
050f7aeb 9981static void
eba905b2 9982connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
9983 struct intel_crtc_config *pipe_config)
9984{
9985 int bpp = pipe_config->pipe_bpp;
9986
9987 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9988 connector->base.base.id,
c23cc417 9989 connector->base.name);
050f7aeb
DV
9990
9991 /* Don't use an invalid EDID bpc value */
9992 if (connector->base.display_info.bpc &&
9993 connector->base.display_info.bpc * 3 < bpp) {
9994 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9995 bpp, connector->base.display_info.bpc*3);
9996 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9997 }
9998
9999 /* Clamp bpp to 8 on screens without EDID 1.4 */
10000 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10001 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10002 bpp);
10003 pipe_config->pipe_bpp = 24;
10004 }
10005}
10006
4e53c2e0 10007static int
050f7aeb
DV
10008compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10009 struct drm_framebuffer *fb,
10010 struct intel_crtc_config *pipe_config)
4e53c2e0 10011{
050f7aeb
DV
10012 struct drm_device *dev = crtc->base.dev;
10013 struct intel_connector *connector;
4e53c2e0
DV
10014 int bpp;
10015
d42264b1
DV
10016 switch (fb->pixel_format) {
10017 case DRM_FORMAT_C8:
4e53c2e0
DV
10018 bpp = 8*3; /* since we go through a colormap */
10019 break;
d42264b1
DV
10020 case DRM_FORMAT_XRGB1555:
10021 case DRM_FORMAT_ARGB1555:
10022 /* checked in intel_framebuffer_init already */
10023 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10024 return -EINVAL;
10025 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10026 bpp = 6*3; /* min is 18bpp */
10027 break;
d42264b1
DV
10028 case DRM_FORMAT_XBGR8888:
10029 case DRM_FORMAT_ABGR8888:
10030 /* checked in intel_framebuffer_init already */
10031 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10032 return -EINVAL;
10033 case DRM_FORMAT_XRGB8888:
10034 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10035 bpp = 8*3;
10036 break;
d42264b1
DV
10037 case DRM_FORMAT_XRGB2101010:
10038 case DRM_FORMAT_ARGB2101010:
10039 case DRM_FORMAT_XBGR2101010:
10040 case DRM_FORMAT_ABGR2101010:
10041 /* checked in intel_framebuffer_init already */
10042 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10043 return -EINVAL;
4e53c2e0
DV
10044 bpp = 10*3;
10045 break;
baba133a 10046 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10047 default:
10048 DRM_DEBUG_KMS("unsupported depth\n");
10049 return -EINVAL;
10050 }
10051
4e53c2e0
DV
10052 pipe_config->pipe_bpp = bpp;
10053
10054 /* Clamp display bpp to EDID value */
10055 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10056 base.head) {
1b829e05
DV
10057 if (!connector->new_encoder ||
10058 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10059 continue;
10060
050f7aeb 10061 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10062 }
10063
10064 return bpp;
10065}
10066
644db711
DV
10067static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10068{
10069 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10070 "type: 0x%x flags: 0x%x\n",
1342830c 10071 mode->crtc_clock,
644db711
DV
10072 mode->crtc_hdisplay, mode->crtc_hsync_start,
10073 mode->crtc_hsync_end, mode->crtc_htotal,
10074 mode->crtc_vdisplay, mode->crtc_vsync_start,
10075 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10076}
10077
c0b03411
DV
10078static void intel_dump_pipe_config(struct intel_crtc *crtc,
10079 struct intel_crtc_config *pipe_config,
10080 const char *context)
10081{
10082 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10083 context, pipe_name(crtc->pipe));
10084
10085 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10086 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10087 pipe_config->pipe_bpp, pipe_config->dither);
10088 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10089 pipe_config->has_pch_encoder,
10090 pipe_config->fdi_lanes,
10091 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10092 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10093 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10094 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10095 pipe_config->has_dp_encoder,
10096 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10097 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10098 pipe_config->dp_m_n.tu);
b95af8be
VK
10099
10100 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10101 pipe_config->has_dp_encoder,
10102 pipe_config->dp_m2_n2.gmch_m,
10103 pipe_config->dp_m2_n2.gmch_n,
10104 pipe_config->dp_m2_n2.link_m,
10105 pipe_config->dp_m2_n2.link_n,
10106 pipe_config->dp_m2_n2.tu);
10107
c0b03411
DV
10108 DRM_DEBUG_KMS("requested mode:\n");
10109 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10110 DRM_DEBUG_KMS("adjusted mode:\n");
10111 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10112 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10113 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10114 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10115 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10116 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10117 pipe_config->gmch_pfit.control,
10118 pipe_config->gmch_pfit.pgm_ratios,
10119 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10120 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10121 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10122 pipe_config->pch_pfit.size,
10123 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10124 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10125 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10126}
10127
bc079e8b
VS
10128static bool encoders_cloneable(const struct intel_encoder *a,
10129 const struct intel_encoder *b)
accfc0c5 10130{
bc079e8b
VS
10131 /* masks could be asymmetric, so check both ways */
10132 return a == b || (a->cloneable & (1 << b->type) &&
10133 b->cloneable & (1 << a->type));
10134}
10135
10136static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10137 struct intel_encoder *encoder)
10138{
10139 struct drm_device *dev = crtc->base.dev;
10140 struct intel_encoder *source_encoder;
10141
b2784e15 10142 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10143 if (source_encoder->new_crtc != crtc)
10144 continue;
10145
10146 if (!encoders_cloneable(encoder, source_encoder))
10147 return false;
10148 }
10149
10150 return true;
10151}
10152
10153static bool check_encoder_cloning(struct intel_crtc *crtc)
10154{
10155 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10156 struct intel_encoder *encoder;
10157
b2784e15 10158 for_each_intel_encoder(dev, encoder) {
bc079e8b 10159 if (encoder->new_crtc != crtc)
accfc0c5
DV
10160 continue;
10161
bc079e8b
VS
10162 if (!check_single_encoder_cloning(crtc, encoder))
10163 return false;
accfc0c5
DV
10164 }
10165
bc079e8b 10166 return true;
accfc0c5
DV
10167}
10168
b8cecdf5
DV
10169static struct intel_crtc_config *
10170intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10171 struct drm_framebuffer *fb,
b8cecdf5 10172 struct drm_display_mode *mode)
ee7b9f93 10173{
7758a113 10174 struct drm_device *dev = crtc->dev;
7758a113 10175 struct intel_encoder *encoder;
b8cecdf5 10176 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10177 int plane_bpp, ret = -EINVAL;
10178 bool retry = true;
ee7b9f93 10179
bc079e8b 10180 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10181 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10182 return ERR_PTR(-EINVAL);
10183 }
10184
b8cecdf5
DV
10185 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10186 if (!pipe_config)
7758a113
DV
10187 return ERR_PTR(-ENOMEM);
10188
b8cecdf5
DV
10189 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10190 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10191
e143a21c
DV
10192 pipe_config->cpu_transcoder =
10193 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10194 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10195
2960bc9c
ID
10196 /*
10197 * Sanitize sync polarity flags based on requested ones. If neither
10198 * positive or negative polarity is requested, treat this as meaning
10199 * negative polarity.
10200 */
10201 if (!(pipe_config->adjusted_mode.flags &
10202 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10203 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10204
10205 if (!(pipe_config->adjusted_mode.flags &
10206 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10207 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10208
050f7aeb
DV
10209 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10210 * plane pixel format and any sink constraints into account. Returns the
10211 * source plane bpp so that dithering can be selected on mismatches
10212 * after encoders and crtc also have had their say. */
10213 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10214 fb, pipe_config);
4e53c2e0
DV
10215 if (plane_bpp < 0)
10216 goto fail;
10217
e41a56be
VS
10218 /*
10219 * Determine the real pipe dimensions. Note that stereo modes can
10220 * increase the actual pipe size due to the frame doubling and
10221 * insertion of additional space for blanks between the frame. This
10222 * is stored in the crtc timings. We use the requested mode to do this
10223 * computation to clearly distinguish it from the adjusted mode, which
10224 * can be changed by the connectors in the below retry loop.
10225 */
10226 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10227 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10228 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10229
e29c22c0 10230encoder_retry:
ef1b460d 10231 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10232 pipe_config->port_clock = 0;
ef1b460d 10233 pipe_config->pixel_multiplier = 1;
ff9a6750 10234
135c81b8 10235 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10236 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10237
7758a113
DV
10238 /* Pass our mode to the connectors and the CRTC to give them a chance to
10239 * adjust it according to limitations or connector properties, and also
10240 * a chance to reject the mode entirely.
47f1c6c9 10241 */
b2784e15 10242 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10243
7758a113
DV
10244 if (&encoder->new_crtc->base != crtc)
10245 continue;
7ae89233 10246
efea6e8e
DV
10247 if (!(encoder->compute_config(encoder, pipe_config))) {
10248 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10249 goto fail;
10250 }
ee7b9f93 10251 }
47f1c6c9 10252
ff9a6750
DV
10253 /* Set default port clock if not overwritten by the encoder. Needs to be
10254 * done afterwards in case the encoder adjusts the mode. */
10255 if (!pipe_config->port_clock)
241bfc38
DL
10256 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10257 * pipe_config->pixel_multiplier;
ff9a6750 10258
a43f6e0f 10259 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10260 if (ret < 0) {
7758a113
DV
10261 DRM_DEBUG_KMS("CRTC fixup failed\n");
10262 goto fail;
ee7b9f93 10263 }
e29c22c0
DV
10264
10265 if (ret == RETRY) {
10266 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10267 ret = -EINVAL;
10268 goto fail;
10269 }
10270
10271 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10272 retry = false;
10273 goto encoder_retry;
10274 }
10275
4e53c2e0
DV
10276 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10277 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10278 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10279
b8cecdf5 10280 return pipe_config;
7758a113 10281fail:
b8cecdf5 10282 kfree(pipe_config);
e29c22c0 10283 return ERR_PTR(ret);
ee7b9f93 10284}
47f1c6c9 10285
e2e1ed41
DV
10286/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10287 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10288static void
10289intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10290 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10291{
10292 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10293 struct drm_device *dev = crtc->dev;
10294 struct intel_encoder *encoder;
10295 struct intel_connector *connector;
10296 struct drm_crtc *tmp_crtc;
79e53945 10297
e2e1ed41 10298 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10299
e2e1ed41
DV
10300 /* Check which crtcs have changed outputs connected to them, these need
10301 * to be part of the prepare_pipes mask. We don't (yet) support global
10302 * modeset across multiple crtcs, so modeset_pipes will only have one
10303 * bit set at most. */
10304 list_for_each_entry(connector, &dev->mode_config.connector_list,
10305 base.head) {
10306 if (connector->base.encoder == &connector->new_encoder->base)
10307 continue;
79e53945 10308
e2e1ed41
DV
10309 if (connector->base.encoder) {
10310 tmp_crtc = connector->base.encoder->crtc;
10311
10312 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10313 }
10314
10315 if (connector->new_encoder)
10316 *prepare_pipes |=
10317 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10318 }
10319
b2784e15 10320 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10321 if (encoder->base.crtc == &encoder->new_crtc->base)
10322 continue;
10323
10324 if (encoder->base.crtc) {
10325 tmp_crtc = encoder->base.crtc;
10326
10327 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10328 }
10329
10330 if (encoder->new_crtc)
10331 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10332 }
10333
7668851f 10334 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10335 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10336 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10337 continue;
7e7d76c3 10338
7668851f 10339 if (!intel_crtc->new_enabled)
e2e1ed41 10340 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10341 else
10342 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10343 }
10344
e2e1ed41
DV
10345
10346 /* set_mode is also used to update properties on life display pipes. */
10347 intel_crtc = to_intel_crtc(crtc);
7668851f 10348 if (intel_crtc->new_enabled)
e2e1ed41
DV
10349 *prepare_pipes |= 1 << intel_crtc->pipe;
10350
b6c5164d
DV
10351 /*
10352 * For simplicity do a full modeset on any pipe where the output routing
10353 * changed. We could be more clever, but that would require us to be
10354 * more careful with calling the relevant encoder->mode_set functions.
10355 */
e2e1ed41
DV
10356 if (*prepare_pipes)
10357 *modeset_pipes = *prepare_pipes;
10358
10359 /* ... and mask these out. */
10360 *modeset_pipes &= ~(*disable_pipes);
10361 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10362
10363 /*
10364 * HACK: We don't (yet) fully support global modesets. intel_set_config
10365 * obies this rule, but the modeset restore mode of
10366 * intel_modeset_setup_hw_state does not.
10367 */
10368 *modeset_pipes &= 1 << intel_crtc->pipe;
10369 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10370
10371 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10372 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10373}
79e53945 10374
ea9d758d 10375static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10376{
ea9d758d 10377 struct drm_encoder *encoder;
f6e5b160 10378 struct drm_device *dev = crtc->dev;
f6e5b160 10379
ea9d758d
DV
10380 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10381 if (encoder->crtc == crtc)
10382 return true;
10383
10384 return false;
10385}
10386
10387static void
10388intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10389{
10390 struct intel_encoder *intel_encoder;
10391 struct intel_crtc *intel_crtc;
10392 struct drm_connector *connector;
10393
b2784e15 10394 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10395 if (!intel_encoder->base.crtc)
10396 continue;
10397
10398 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10399
10400 if (prepare_pipes & (1 << intel_crtc->pipe))
10401 intel_encoder->connectors_active = false;
10402 }
10403
10404 intel_modeset_commit_output_state(dev);
10405
7668851f 10406 /* Double check state. */
d3fcc808 10407 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10408 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10409 WARN_ON(intel_crtc->new_config &&
10410 intel_crtc->new_config != &intel_crtc->config);
10411 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10412 }
10413
10414 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10415 if (!connector->encoder || !connector->encoder->crtc)
10416 continue;
10417
10418 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10419
10420 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10421 struct drm_property *dpms_property =
10422 dev->mode_config.dpms_property;
10423
ea9d758d 10424 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10425 drm_object_property_set_value(&connector->base,
68d34720
DV
10426 dpms_property,
10427 DRM_MODE_DPMS_ON);
ea9d758d
DV
10428
10429 intel_encoder = to_intel_encoder(connector->encoder);
10430 intel_encoder->connectors_active = true;
10431 }
10432 }
10433
10434}
10435
3bd26263 10436static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10437{
3bd26263 10438 int diff;
f1f644dc
JB
10439
10440 if (clock1 == clock2)
10441 return true;
10442
10443 if (!clock1 || !clock2)
10444 return false;
10445
10446 diff = abs(clock1 - clock2);
10447
10448 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10449 return true;
10450
10451 return false;
10452}
10453
25c5b266
DV
10454#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10455 list_for_each_entry((intel_crtc), \
10456 &(dev)->mode_config.crtc_list, \
10457 base.head) \
0973f18f 10458 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10459
0e8ffe1b 10460static bool
2fa2fe9a
DV
10461intel_pipe_config_compare(struct drm_device *dev,
10462 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10463 struct intel_crtc_config *pipe_config)
10464{
66e985c0
DV
10465#define PIPE_CONF_CHECK_X(name) \
10466 if (current_config->name != pipe_config->name) { \
10467 DRM_ERROR("mismatch in " #name " " \
10468 "(expected 0x%08x, found 0x%08x)\n", \
10469 current_config->name, \
10470 pipe_config->name); \
10471 return false; \
10472 }
10473
08a24034
DV
10474#define PIPE_CONF_CHECK_I(name) \
10475 if (current_config->name != pipe_config->name) { \
10476 DRM_ERROR("mismatch in " #name " " \
10477 "(expected %i, found %i)\n", \
10478 current_config->name, \
10479 pipe_config->name); \
10480 return false; \
88adfff1
DV
10481 }
10482
b95af8be
VK
10483/* This is required for BDW+ where there is only one set of registers for
10484 * switching between high and low RR.
10485 * This macro can be used whenever a comparison has to be made between one
10486 * hw state and multiple sw state variables.
10487 */
10488#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10489 if ((current_config->name != pipe_config->name) && \
10490 (current_config->alt_name != pipe_config->name)) { \
10491 DRM_ERROR("mismatch in " #name " " \
10492 "(expected %i or %i, found %i)\n", \
10493 current_config->name, \
10494 current_config->alt_name, \
10495 pipe_config->name); \
10496 return false; \
10497 }
10498
1bd1bd80
DV
10499#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10500 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10501 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10502 "(expected %i, found %i)\n", \
10503 current_config->name & (mask), \
10504 pipe_config->name & (mask)); \
10505 return false; \
10506 }
10507
5e550656
VS
10508#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10509 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10510 DRM_ERROR("mismatch in " #name " " \
10511 "(expected %i, found %i)\n", \
10512 current_config->name, \
10513 pipe_config->name); \
10514 return false; \
10515 }
10516
bb760063
DV
10517#define PIPE_CONF_QUIRK(quirk) \
10518 ((current_config->quirks | pipe_config->quirks) & (quirk))
10519
eccb140b
DV
10520 PIPE_CONF_CHECK_I(cpu_transcoder);
10521
08a24034
DV
10522 PIPE_CONF_CHECK_I(has_pch_encoder);
10523 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10524 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10525 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10526 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10527 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10528 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10529
eb14cb74 10530 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10531
10532 if (INTEL_INFO(dev)->gen < 8) {
10533 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10534 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10535 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10536 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10537 PIPE_CONF_CHECK_I(dp_m_n.tu);
10538
10539 if (current_config->has_drrs) {
10540 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10541 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10542 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10543 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10544 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10545 }
10546 } else {
10547 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10548 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10549 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10550 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10551 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10552 }
eb14cb74 10553
1bd1bd80
DV
10554 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10555 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10556 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10557 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10558 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10559 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10560
10561 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10562 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10563 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10564 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10565 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10566 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10567
c93f54cf 10568 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10569 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10570 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10571 IS_VALLEYVIEW(dev))
10572 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10573
9ed109a7
DV
10574 PIPE_CONF_CHECK_I(has_audio);
10575
1bd1bd80
DV
10576 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10577 DRM_MODE_FLAG_INTERLACE);
10578
bb760063
DV
10579 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10580 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10581 DRM_MODE_FLAG_PHSYNC);
10582 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10583 DRM_MODE_FLAG_NHSYNC);
10584 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10585 DRM_MODE_FLAG_PVSYNC);
10586 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10587 DRM_MODE_FLAG_NVSYNC);
10588 }
045ac3b5 10589
37327abd
VS
10590 PIPE_CONF_CHECK_I(pipe_src_w);
10591 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10592
9953599b
DV
10593 /*
10594 * FIXME: BIOS likes to set up a cloned config with lvds+external
10595 * screen. Since we don't yet re-compute the pipe config when moving
10596 * just the lvds port away to another pipe the sw tracking won't match.
10597 *
10598 * Proper atomic modesets with recomputed global state will fix this.
10599 * Until then just don't check gmch state for inherited modes.
10600 */
10601 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10602 PIPE_CONF_CHECK_I(gmch_pfit.control);
10603 /* pfit ratios are autocomputed by the hw on gen4+ */
10604 if (INTEL_INFO(dev)->gen < 4)
10605 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10606 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10607 }
10608
fd4daa9c
CW
10609 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10610 if (current_config->pch_pfit.enabled) {
10611 PIPE_CONF_CHECK_I(pch_pfit.pos);
10612 PIPE_CONF_CHECK_I(pch_pfit.size);
10613 }
2fa2fe9a 10614
e59150dc
JB
10615 /* BDW+ don't expose a synchronous way to read the state */
10616 if (IS_HASWELL(dev))
10617 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10618
282740f7
VS
10619 PIPE_CONF_CHECK_I(double_wide);
10620
26804afd
DV
10621 PIPE_CONF_CHECK_X(ddi_pll_sel);
10622
c0d43d62 10623 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10624 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10625 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10626 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10627 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10628 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10629
42571aef
VS
10630 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10631 PIPE_CONF_CHECK_I(pipe_bpp);
10632
a9a7e98a
JB
10633 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10634 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10635
66e985c0 10636#undef PIPE_CONF_CHECK_X
08a24034 10637#undef PIPE_CONF_CHECK_I
b95af8be 10638#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10639#undef PIPE_CONF_CHECK_FLAGS
5e550656 10640#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10641#undef PIPE_CONF_QUIRK
88adfff1 10642
0e8ffe1b
DV
10643 return true;
10644}
10645
91d1b4bd
DV
10646static void
10647check_connector_state(struct drm_device *dev)
8af6cf88 10648{
8af6cf88
DV
10649 struct intel_connector *connector;
10650
10651 list_for_each_entry(connector, &dev->mode_config.connector_list,
10652 base.head) {
10653 /* This also checks the encoder/connector hw state with the
10654 * ->get_hw_state callbacks. */
10655 intel_connector_check_state(connector);
10656
10657 WARN(&connector->new_encoder->base != connector->base.encoder,
10658 "connector's staged encoder doesn't match current encoder\n");
10659 }
91d1b4bd
DV
10660}
10661
10662static void
10663check_encoder_state(struct drm_device *dev)
10664{
10665 struct intel_encoder *encoder;
10666 struct intel_connector *connector;
8af6cf88 10667
b2784e15 10668 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10669 bool enabled = false;
10670 bool active = false;
10671 enum pipe pipe, tracked_pipe;
10672
10673 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10674 encoder->base.base.id,
8e329a03 10675 encoder->base.name);
8af6cf88
DV
10676
10677 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10678 "encoder's stage crtc doesn't match current crtc\n");
10679 WARN(encoder->connectors_active && !encoder->base.crtc,
10680 "encoder's active_connectors set, but no crtc\n");
10681
10682 list_for_each_entry(connector, &dev->mode_config.connector_list,
10683 base.head) {
10684 if (connector->base.encoder != &encoder->base)
10685 continue;
10686 enabled = true;
10687 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10688 active = true;
10689 }
0e32b39c
DA
10690 /*
10691 * for MST connectors if we unplug the connector is gone
10692 * away but the encoder is still connected to a crtc
10693 * until a modeset happens in response to the hotplug.
10694 */
10695 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10696 continue;
10697
8af6cf88
DV
10698 WARN(!!encoder->base.crtc != enabled,
10699 "encoder's enabled state mismatch "
10700 "(expected %i, found %i)\n",
10701 !!encoder->base.crtc, enabled);
10702 WARN(active && !encoder->base.crtc,
10703 "active encoder with no crtc\n");
10704
10705 WARN(encoder->connectors_active != active,
10706 "encoder's computed active state doesn't match tracked active state "
10707 "(expected %i, found %i)\n", active, encoder->connectors_active);
10708
10709 active = encoder->get_hw_state(encoder, &pipe);
10710 WARN(active != encoder->connectors_active,
10711 "encoder's hw state doesn't match sw tracking "
10712 "(expected %i, found %i)\n",
10713 encoder->connectors_active, active);
10714
10715 if (!encoder->base.crtc)
10716 continue;
10717
10718 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10719 WARN(active && pipe != tracked_pipe,
10720 "active encoder's pipe doesn't match"
10721 "(expected %i, found %i)\n",
10722 tracked_pipe, pipe);
10723
10724 }
91d1b4bd
DV
10725}
10726
10727static void
10728check_crtc_state(struct drm_device *dev)
10729{
fbee40df 10730 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10731 struct intel_crtc *crtc;
10732 struct intel_encoder *encoder;
10733 struct intel_crtc_config pipe_config;
8af6cf88 10734
d3fcc808 10735 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10736 bool enabled = false;
10737 bool active = false;
10738
045ac3b5
JB
10739 memset(&pipe_config, 0, sizeof(pipe_config));
10740
8af6cf88
DV
10741 DRM_DEBUG_KMS("[CRTC:%d]\n",
10742 crtc->base.base.id);
10743
10744 WARN(crtc->active && !crtc->base.enabled,
10745 "active crtc, but not enabled in sw tracking\n");
10746
b2784e15 10747 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10748 if (encoder->base.crtc != &crtc->base)
10749 continue;
10750 enabled = true;
10751 if (encoder->connectors_active)
10752 active = true;
10753 }
6c49f241 10754
8af6cf88
DV
10755 WARN(active != crtc->active,
10756 "crtc's computed active state doesn't match tracked active state "
10757 "(expected %i, found %i)\n", active, crtc->active);
10758 WARN(enabled != crtc->base.enabled,
10759 "crtc's computed enabled state doesn't match tracked enabled state "
10760 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10761
0e8ffe1b
DV
10762 active = dev_priv->display.get_pipe_config(crtc,
10763 &pipe_config);
d62cf62a 10764
b6b5d049
VS
10765 /* hw state is inconsistent with the pipe quirk */
10766 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10767 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10768 active = crtc->active;
10769
b2784e15 10770 for_each_intel_encoder(dev, encoder) {
3eaba51c 10771 enum pipe pipe;
6c49f241
DV
10772 if (encoder->base.crtc != &crtc->base)
10773 continue;
1d37b689 10774 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10775 encoder->get_config(encoder, &pipe_config);
10776 }
10777
0e8ffe1b
DV
10778 WARN(crtc->active != active,
10779 "crtc active state doesn't match with hw state "
10780 "(expected %i, found %i)\n", crtc->active, active);
10781
c0b03411
DV
10782 if (active &&
10783 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10784 WARN(1, "pipe state doesn't match!\n");
10785 intel_dump_pipe_config(crtc, &pipe_config,
10786 "[hw state]");
10787 intel_dump_pipe_config(crtc, &crtc->config,
10788 "[sw state]");
10789 }
8af6cf88
DV
10790 }
10791}
10792
91d1b4bd
DV
10793static void
10794check_shared_dpll_state(struct drm_device *dev)
10795{
fbee40df 10796 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10797 struct intel_crtc *crtc;
10798 struct intel_dpll_hw_state dpll_hw_state;
10799 int i;
5358901f
DV
10800
10801 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10802 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10803 int enabled_crtcs = 0, active_crtcs = 0;
10804 bool active;
10805
10806 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10807
10808 DRM_DEBUG_KMS("%s\n", pll->name);
10809
10810 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10811
10812 WARN(pll->active > pll->refcount,
10813 "more active pll users than references: %i vs %i\n",
10814 pll->active, pll->refcount);
10815 WARN(pll->active && !pll->on,
10816 "pll in active use but not on in sw tracking\n");
35c95375
DV
10817 WARN(pll->on && !pll->active,
10818 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10819 WARN(pll->on != active,
10820 "pll on state mismatch (expected %i, found %i)\n",
10821 pll->on, active);
10822
d3fcc808 10823 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10824 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10825 enabled_crtcs++;
10826 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10827 active_crtcs++;
10828 }
10829 WARN(pll->active != active_crtcs,
10830 "pll active crtcs mismatch (expected %i, found %i)\n",
10831 pll->active, active_crtcs);
10832 WARN(pll->refcount != enabled_crtcs,
10833 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10834 pll->refcount, enabled_crtcs);
66e985c0
DV
10835
10836 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10837 sizeof(dpll_hw_state)),
10838 "pll hw state mismatch\n");
5358901f 10839 }
8af6cf88
DV
10840}
10841
91d1b4bd
DV
10842void
10843intel_modeset_check_state(struct drm_device *dev)
10844{
10845 check_connector_state(dev);
10846 check_encoder_state(dev);
10847 check_crtc_state(dev);
10848 check_shared_dpll_state(dev);
10849}
10850
18442d08
VS
10851void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10852 int dotclock)
10853{
10854 /*
10855 * FDI already provided one idea for the dotclock.
10856 * Yell if the encoder disagrees.
10857 */
241bfc38 10858 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10859 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10860 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10861}
10862
80715b2f
VS
10863static void update_scanline_offset(struct intel_crtc *crtc)
10864{
10865 struct drm_device *dev = crtc->base.dev;
10866
10867 /*
10868 * The scanline counter increments at the leading edge of hsync.
10869 *
10870 * On most platforms it starts counting from vtotal-1 on the
10871 * first active line. That means the scanline counter value is
10872 * always one less than what we would expect. Ie. just after
10873 * start of vblank, which also occurs at start of hsync (on the
10874 * last active line), the scanline counter will read vblank_start-1.
10875 *
10876 * On gen2 the scanline counter starts counting from 1 instead
10877 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10878 * to keep the value positive), instead of adding one.
10879 *
10880 * On HSW+ the behaviour of the scanline counter depends on the output
10881 * type. For DP ports it behaves like most other platforms, but on HDMI
10882 * there's an extra 1 line difference. So we need to add two instead of
10883 * one to the value.
10884 */
10885 if (IS_GEN2(dev)) {
10886 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10887 int vtotal;
10888
10889 vtotal = mode->crtc_vtotal;
10890 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10891 vtotal /= 2;
10892
10893 crtc->scanline_offset = vtotal - 1;
10894 } else if (HAS_DDI(dev) &&
10895 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10896 crtc->scanline_offset = 2;
10897 } else
10898 crtc->scanline_offset = 1;
10899}
10900
f30da187
DV
10901static int __intel_set_mode(struct drm_crtc *crtc,
10902 struct drm_display_mode *mode,
10903 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10904{
10905 struct drm_device *dev = crtc->dev;
fbee40df 10906 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10907 struct drm_display_mode *saved_mode;
b8cecdf5 10908 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10909 struct intel_crtc *intel_crtc;
10910 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10911 int ret = 0;
a6778b3c 10912
4b4b9238 10913 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10914 if (!saved_mode)
10915 return -ENOMEM;
a6778b3c 10916
e2e1ed41 10917 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10918 &prepare_pipes, &disable_pipes);
10919
3ac18232 10920 *saved_mode = crtc->mode;
a6778b3c 10921
25c5b266
DV
10922 /* Hack: Because we don't (yet) support global modeset on multiple
10923 * crtcs, we don't keep track of the new mode for more than one crtc.
10924 * Hence simply check whether any bit is set in modeset_pipes in all the
10925 * pieces of code that are not yet converted to deal with mutliple crtcs
10926 * changing their mode at the same time. */
25c5b266 10927 if (modeset_pipes) {
4e53c2e0 10928 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10929 if (IS_ERR(pipe_config)) {
10930 ret = PTR_ERR(pipe_config);
10931 pipe_config = NULL;
10932
3ac18232 10933 goto out;
25c5b266 10934 }
c0b03411
DV
10935 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10936 "[modeset]");
50741abc 10937 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10938 }
a6778b3c 10939
30a970c6
JB
10940 /*
10941 * See if the config requires any additional preparation, e.g.
10942 * to adjust global state with pipes off. We need to do this
10943 * here so we can get the modeset_pipe updated config for the new
10944 * mode set on this crtc. For other crtcs we need to use the
10945 * adjusted_mode bits in the crtc directly.
10946 */
c164f833 10947 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10948 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10949
c164f833
VS
10950 /* may have added more to prepare_pipes than we should */
10951 prepare_pipes &= ~disable_pipes;
10952 }
10953
460da916
DV
10954 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10955 intel_crtc_disable(&intel_crtc->base);
10956
ea9d758d
DV
10957 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10958 if (intel_crtc->base.enabled)
10959 dev_priv->display.crtc_disable(&intel_crtc->base);
10960 }
a6778b3c 10961
6c4c86f5
DV
10962 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10963 * to set it here already despite that we pass it down the callchain.
f6e5b160 10964 */
b8cecdf5 10965 if (modeset_pipes) {
25c5b266 10966 crtc->mode = *mode;
b8cecdf5
DV
10967 /* mode_set/enable/disable functions rely on a correct pipe
10968 * config. */
10969 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10970 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10971
10972 /*
10973 * Calculate and store various constants which
10974 * are later needed by vblank and swap-completion
10975 * timestamping. They are derived from true hwmode.
10976 */
10977 drm_calc_timestamping_constants(crtc,
10978 &pipe_config->adjusted_mode);
b8cecdf5 10979 }
7758a113 10980
ea9d758d
DV
10981 /* Only after disabling all output pipelines that will be changed can we
10982 * update the the output configuration. */
10983 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10984
47fab737
DV
10985 if (dev_priv->display.modeset_global_resources)
10986 dev_priv->display.modeset_global_resources(dev);
10987
a6778b3c
DV
10988 /* Set up the DPLL and any encoders state that needs to adjust or depend
10989 * on the DPLL.
f6e5b160 10990 */
25c5b266 10991 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
10992 struct drm_framebuffer *old_fb = crtc->primary->fb;
10993 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
10994 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
10995
10996 mutex_lock(&dev->struct_mutex);
10997 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 10998 obj,
4c10794f
DV
10999 NULL);
11000 if (ret != 0) {
11001 DRM_ERROR("pin & fence failed\n");
11002 mutex_unlock(&dev->struct_mutex);
11003 goto done;
11004 }
2ff8fde1 11005 if (old_fb)
a071fa00 11006 intel_unpin_fb_obj(old_obj);
a071fa00
DV
11007 i915_gem_track_fb(old_obj, obj,
11008 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
11009 mutex_unlock(&dev->struct_mutex);
11010
11011 crtc->primary->fb = fb;
11012 crtc->x = x;
11013 crtc->y = y;
11014
4271b753
DV
11015 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11016 x, y, fb);
c0c36b94
CW
11017 if (ret)
11018 goto done;
a6778b3c
DV
11019 }
11020
11021 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11022 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11023 update_scanline_offset(intel_crtc);
11024
25c5b266 11025 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11026 }
a6778b3c 11027
a6778b3c
DV
11028 /* FIXME: add subpixel order */
11029done:
4b4b9238 11030 if (ret && crtc->enabled)
3ac18232 11031 crtc->mode = *saved_mode;
a6778b3c 11032
3ac18232 11033out:
b8cecdf5 11034 kfree(pipe_config);
3ac18232 11035 kfree(saved_mode);
a6778b3c 11036 return ret;
f6e5b160
CW
11037}
11038
e7457a9a
DL
11039static int intel_set_mode(struct drm_crtc *crtc,
11040 struct drm_display_mode *mode,
11041 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11042{
11043 int ret;
11044
11045 ret = __intel_set_mode(crtc, mode, x, y, fb);
11046
11047 if (ret == 0)
11048 intel_modeset_check_state(crtc->dev);
11049
11050 return ret;
11051}
11052
c0c36b94
CW
11053void intel_crtc_restore_mode(struct drm_crtc *crtc)
11054{
f4510a27 11055 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11056}
11057
25c5b266
DV
11058#undef for_each_intel_crtc_masked
11059
d9e55608
DV
11060static void intel_set_config_free(struct intel_set_config *config)
11061{
11062 if (!config)
11063 return;
11064
1aa4b628
DV
11065 kfree(config->save_connector_encoders);
11066 kfree(config->save_encoder_crtcs);
7668851f 11067 kfree(config->save_crtc_enabled);
d9e55608
DV
11068 kfree(config);
11069}
11070
85f9eb71
DV
11071static int intel_set_config_save_state(struct drm_device *dev,
11072 struct intel_set_config *config)
11073{
7668851f 11074 struct drm_crtc *crtc;
85f9eb71
DV
11075 struct drm_encoder *encoder;
11076 struct drm_connector *connector;
11077 int count;
11078
7668851f
VS
11079 config->save_crtc_enabled =
11080 kcalloc(dev->mode_config.num_crtc,
11081 sizeof(bool), GFP_KERNEL);
11082 if (!config->save_crtc_enabled)
11083 return -ENOMEM;
11084
1aa4b628
DV
11085 config->save_encoder_crtcs =
11086 kcalloc(dev->mode_config.num_encoder,
11087 sizeof(struct drm_crtc *), GFP_KERNEL);
11088 if (!config->save_encoder_crtcs)
85f9eb71
DV
11089 return -ENOMEM;
11090
1aa4b628
DV
11091 config->save_connector_encoders =
11092 kcalloc(dev->mode_config.num_connector,
11093 sizeof(struct drm_encoder *), GFP_KERNEL);
11094 if (!config->save_connector_encoders)
85f9eb71
DV
11095 return -ENOMEM;
11096
11097 /* Copy data. Note that driver private data is not affected.
11098 * Should anything bad happen only the expected state is
11099 * restored, not the drivers personal bookkeeping.
11100 */
7668851f 11101 count = 0;
70e1e0ec 11102 for_each_crtc(dev, crtc) {
7668851f
VS
11103 config->save_crtc_enabled[count++] = crtc->enabled;
11104 }
11105
85f9eb71
DV
11106 count = 0;
11107 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11108 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11109 }
11110
11111 count = 0;
11112 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11113 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11114 }
11115
11116 return 0;
11117}
11118
11119static void intel_set_config_restore_state(struct drm_device *dev,
11120 struct intel_set_config *config)
11121{
7668851f 11122 struct intel_crtc *crtc;
9a935856
DV
11123 struct intel_encoder *encoder;
11124 struct intel_connector *connector;
85f9eb71
DV
11125 int count;
11126
7668851f 11127 count = 0;
d3fcc808 11128 for_each_intel_crtc(dev, crtc) {
7668851f 11129 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11130
11131 if (crtc->new_enabled)
11132 crtc->new_config = &crtc->config;
11133 else
11134 crtc->new_config = NULL;
7668851f
VS
11135 }
11136
85f9eb71 11137 count = 0;
b2784e15 11138 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11139 encoder->new_crtc =
11140 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11141 }
11142
11143 count = 0;
9a935856
DV
11144 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11145 connector->new_encoder =
11146 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11147 }
11148}
11149
e3de42b6 11150static bool
2e57f47d 11151is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11152{
11153 int i;
11154
2e57f47d
CW
11155 if (set->num_connectors == 0)
11156 return false;
11157
11158 if (WARN_ON(set->connectors == NULL))
11159 return false;
11160
11161 for (i = 0; i < set->num_connectors; i++)
11162 if (set->connectors[i]->encoder &&
11163 set->connectors[i]->encoder->crtc == set->crtc &&
11164 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11165 return true;
11166
11167 return false;
11168}
11169
5e2b584e
DV
11170static void
11171intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11172 struct intel_set_config *config)
11173{
11174
11175 /* We should be able to check here if the fb has the same properties
11176 * and then just flip_or_move it */
2e57f47d
CW
11177 if (is_crtc_connector_off(set)) {
11178 config->mode_changed = true;
f4510a27 11179 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11180 /*
11181 * If we have no fb, we can only flip as long as the crtc is
11182 * active, otherwise we need a full mode set. The crtc may
11183 * be active if we've only disabled the primary plane, or
11184 * in fastboot situations.
11185 */
f4510a27 11186 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11187 struct intel_crtc *intel_crtc =
11188 to_intel_crtc(set->crtc);
11189
3b150f08 11190 if (intel_crtc->active) {
319d9827
JB
11191 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11192 config->fb_changed = true;
11193 } else {
11194 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11195 config->mode_changed = true;
11196 }
5e2b584e
DV
11197 } else if (set->fb == NULL) {
11198 config->mode_changed = true;
72f4901e 11199 } else if (set->fb->pixel_format !=
f4510a27 11200 set->crtc->primary->fb->pixel_format) {
5e2b584e 11201 config->mode_changed = true;
e3de42b6 11202 } else {
5e2b584e 11203 config->fb_changed = true;
e3de42b6 11204 }
5e2b584e
DV
11205 }
11206
835c5873 11207 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11208 config->fb_changed = true;
11209
11210 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11211 DRM_DEBUG_KMS("modes are different, full mode set\n");
11212 drm_mode_debug_printmodeline(&set->crtc->mode);
11213 drm_mode_debug_printmodeline(set->mode);
11214 config->mode_changed = true;
11215 }
a1d95703
CW
11216
11217 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11218 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11219}
11220
2e431051 11221static int
9a935856
DV
11222intel_modeset_stage_output_state(struct drm_device *dev,
11223 struct drm_mode_set *set,
11224 struct intel_set_config *config)
50f56119 11225{
9a935856
DV
11226 struct intel_connector *connector;
11227 struct intel_encoder *encoder;
7668851f 11228 struct intel_crtc *crtc;
f3f08572 11229 int ro;
50f56119 11230
9abdda74 11231 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11232 * of connectors. For paranoia, double-check this. */
11233 WARN_ON(!set->fb && (set->num_connectors != 0));
11234 WARN_ON(set->fb && (set->num_connectors == 0));
11235
9a935856
DV
11236 list_for_each_entry(connector, &dev->mode_config.connector_list,
11237 base.head) {
11238 /* Otherwise traverse passed in connector list and get encoders
11239 * for them. */
50f56119 11240 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11241 if (set->connectors[ro] == &connector->base) {
0e32b39c 11242 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11243 break;
11244 }
11245 }
11246
9a935856
DV
11247 /* If we disable the crtc, disable all its connectors. Also, if
11248 * the connector is on the changing crtc but not on the new
11249 * connector list, disable it. */
11250 if ((!set->fb || ro == set->num_connectors) &&
11251 connector->base.encoder &&
11252 connector->base.encoder->crtc == set->crtc) {
11253 connector->new_encoder = NULL;
11254
11255 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11256 connector->base.base.id,
c23cc417 11257 connector->base.name);
9a935856
DV
11258 }
11259
11260
11261 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11262 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11263 config->mode_changed = true;
50f56119
DV
11264 }
11265 }
9a935856 11266 /* connector->new_encoder is now updated for all connectors. */
50f56119 11267
9a935856 11268 /* Update crtc of enabled connectors. */
9a935856
DV
11269 list_for_each_entry(connector, &dev->mode_config.connector_list,
11270 base.head) {
7668851f
VS
11271 struct drm_crtc *new_crtc;
11272
9a935856 11273 if (!connector->new_encoder)
50f56119
DV
11274 continue;
11275
9a935856 11276 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11277
11278 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11279 if (set->connectors[ro] == &connector->base)
50f56119
DV
11280 new_crtc = set->crtc;
11281 }
11282
11283 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11284 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11285 new_crtc)) {
5e2b584e 11286 return -EINVAL;
50f56119 11287 }
0e32b39c 11288 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11289
11290 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11291 connector->base.base.id,
c23cc417 11292 connector->base.name,
9a935856
DV
11293 new_crtc->base.id);
11294 }
11295
11296 /* Check for any encoders that needs to be disabled. */
b2784e15 11297 for_each_intel_encoder(dev, encoder) {
5a65f358 11298 int num_connectors = 0;
9a935856
DV
11299 list_for_each_entry(connector,
11300 &dev->mode_config.connector_list,
11301 base.head) {
11302 if (connector->new_encoder == encoder) {
11303 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11304 num_connectors++;
9a935856
DV
11305 }
11306 }
5a65f358
PZ
11307
11308 if (num_connectors == 0)
11309 encoder->new_crtc = NULL;
11310 else if (num_connectors > 1)
11311 return -EINVAL;
11312
9a935856
DV
11313 /* Only now check for crtc changes so we don't miss encoders
11314 * that will be disabled. */
11315 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11316 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11317 config->mode_changed = true;
50f56119
DV
11318 }
11319 }
9a935856 11320 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11321 list_for_each_entry(connector, &dev->mode_config.connector_list,
11322 base.head) {
11323 if (connector->new_encoder)
11324 if (connector->new_encoder != connector->encoder)
11325 connector->encoder = connector->new_encoder;
11326 }
d3fcc808 11327 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11328 crtc->new_enabled = false;
11329
b2784e15 11330 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11331 if (encoder->new_crtc == crtc) {
11332 crtc->new_enabled = true;
11333 break;
11334 }
11335 }
11336
11337 if (crtc->new_enabled != crtc->base.enabled) {
11338 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11339 crtc->new_enabled ? "en" : "dis");
11340 config->mode_changed = true;
11341 }
7bd0a8e7
VS
11342
11343 if (crtc->new_enabled)
11344 crtc->new_config = &crtc->config;
11345 else
11346 crtc->new_config = NULL;
7668851f
VS
11347 }
11348
2e431051
DV
11349 return 0;
11350}
11351
7d00a1f5
VS
11352static void disable_crtc_nofb(struct intel_crtc *crtc)
11353{
11354 struct drm_device *dev = crtc->base.dev;
11355 struct intel_encoder *encoder;
11356 struct intel_connector *connector;
11357
11358 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11359 pipe_name(crtc->pipe));
11360
11361 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11362 if (connector->new_encoder &&
11363 connector->new_encoder->new_crtc == crtc)
11364 connector->new_encoder = NULL;
11365 }
11366
b2784e15 11367 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11368 if (encoder->new_crtc == crtc)
11369 encoder->new_crtc = NULL;
11370 }
11371
11372 crtc->new_enabled = false;
7bd0a8e7 11373 crtc->new_config = NULL;
7d00a1f5
VS
11374}
11375
2e431051
DV
11376static int intel_crtc_set_config(struct drm_mode_set *set)
11377{
11378 struct drm_device *dev;
2e431051
DV
11379 struct drm_mode_set save_set;
11380 struct intel_set_config *config;
11381 int ret;
2e431051 11382
8d3e375e
DV
11383 BUG_ON(!set);
11384 BUG_ON(!set->crtc);
11385 BUG_ON(!set->crtc->helper_private);
2e431051 11386
7e53f3a4
DV
11387 /* Enforce sane interface api - has been abused by the fb helper. */
11388 BUG_ON(!set->mode && set->fb);
11389 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11390
2e431051
DV
11391 if (set->fb) {
11392 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11393 set->crtc->base.id, set->fb->base.id,
11394 (int)set->num_connectors, set->x, set->y);
11395 } else {
11396 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11397 }
11398
11399 dev = set->crtc->dev;
11400
11401 ret = -ENOMEM;
11402 config = kzalloc(sizeof(*config), GFP_KERNEL);
11403 if (!config)
11404 goto out_config;
11405
11406 ret = intel_set_config_save_state(dev, config);
11407 if (ret)
11408 goto out_config;
11409
11410 save_set.crtc = set->crtc;
11411 save_set.mode = &set->crtc->mode;
11412 save_set.x = set->crtc->x;
11413 save_set.y = set->crtc->y;
f4510a27 11414 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11415
11416 /* Compute whether we need a full modeset, only an fb base update or no
11417 * change at all. In the future we might also check whether only the
11418 * mode changed, e.g. for LVDS where we only change the panel fitter in
11419 * such cases. */
11420 intel_set_config_compute_mode_changes(set, config);
11421
9a935856 11422 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11423 if (ret)
11424 goto fail;
11425
5e2b584e 11426 if (config->mode_changed) {
c0c36b94
CW
11427 ret = intel_set_mode(set->crtc, set->mode,
11428 set->x, set->y, set->fb);
5e2b584e 11429 } else if (config->fb_changed) {
3b150f08
MR
11430 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11431
4878cae2
VS
11432 intel_crtc_wait_for_pending_flips(set->crtc);
11433
4f660f49 11434 ret = intel_pipe_set_base(set->crtc,
94352cf9 11435 set->x, set->y, set->fb);
3b150f08
MR
11436
11437 /*
11438 * We need to make sure the primary plane is re-enabled if it
11439 * has previously been turned off.
11440 */
11441 if (!intel_crtc->primary_enabled && ret == 0) {
11442 WARN_ON(!intel_crtc->active);
fdd508a6 11443 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11444 }
11445
7ca51a3a
JB
11446 /*
11447 * In the fastboot case this may be our only check of the
11448 * state after boot. It would be better to only do it on
11449 * the first update, but we don't have a nice way of doing that
11450 * (and really, set_config isn't used much for high freq page
11451 * flipping, so increasing its cost here shouldn't be a big
11452 * deal).
11453 */
d330a953 11454 if (i915.fastboot && ret == 0)
7ca51a3a 11455 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11456 }
11457
2d05eae1 11458 if (ret) {
bf67dfeb
DV
11459 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11460 set->crtc->base.id, ret);
50f56119 11461fail:
2d05eae1 11462 intel_set_config_restore_state(dev, config);
50f56119 11463
7d00a1f5
VS
11464 /*
11465 * HACK: if the pipe was on, but we didn't have a framebuffer,
11466 * force the pipe off to avoid oopsing in the modeset code
11467 * due to fb==NULL. This should only happen during boot since
11468 * we don't yet reconstruct the FB from the hardware state.
11469 */
11470 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11471 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11472
2d05eae1
CW
11473 /* Try to restore the config */
11474 if (config->mode_changed &&
11475 intel_set_mode(save_set.crtc, save_set.mode,
11476 save_set.x, save_set.y, save_set.fb))
11477 DRM_ERROR("failed to restore config after modeset failure\n");
11478 }
50f56119 11479
d9e55608
DV
11480out_config:
11481 intel_set_config_free(config);
50f56119
DV
11482 return ret;
11483}
f6e5b160
CW
11484
11485static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11486 .gamma_set = intel_crtc_gamma_set,
50f56119 11487 .set_config = intel_crtc_set_config,
f6e5b160
CW
11488 .destroy = intel_crtc_destroy,
11489 .page_flip = intel_crtc_page_flip,
11490};
11491
5358901f
DV
11492static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11493 struct intel_shared_dpll *pll,
11494 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11495{
5358901f 11496 uint32_t val;
ee7b9f93 11497
bd2bb1b9
PZ
11498 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11499 return false;
11500
5358901f 11501 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11502 hw_state->dpll = val;
11503 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11504 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11505
11506 return val & DPLL_VCO_ENABLE;
11507}
11508
15bdd4cf
DV
11509static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11510 struct intel_shared_dpll *pll)
11511{
11512 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11513 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11514}
11515
e7b903d2
DV
11516static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11517 struct intel_shared_dpll *pll)
11518{
e7b903d2 11519 /* PCH refclock must be enabled first */
89eff4be 11520 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11521
15bdd4cf
DV
11522 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11523
11524 /* Wait for the clocks to stabilize. */
11525 POSTING_READ(PCH_DPLL(pll->id));
11526 udelay(150);
11527
11528 /* The pixel multiplier can only be updated once the
11529 * DPLL is enabled and the clocks are stable.
11530 *
11531 * So write it again.
11532 */
11533 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11534 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11535 udelay(200);
11536}
11537
11538static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11539 struct intel_shared_dpll *pll)
11540{
11541 struct drm_device *dev = dev_priv->dev;
11542 struct intel_crtc *crtc;
e7b903d2
DV
11543
11544 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11545 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11546 if (intel_crtc_to_shared_dpll(crtc) == pll)
11547 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11548 }
11549
15bdd4cf
DV
11550 I915_WRITE(PCH_DPLL(pll->id), 0);
11551 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11552 udelay(200);
11553}
11554
46edb027
DV
11555static char *ibx_pch_dpll_names[] = {
11556 "PCH DPLL A",
11557 "PCH DPLL B",
11558};
11559
7c74ade1 11560static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11561{
e7b903d2 11562 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11563 int i;
11564
7c74ade1 11565 dev_priv->num_shared_dpll = 2;
ee7b9f93 11566
e72f9fbf 11567 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11568 dev_priv->shared_dplls[i].id = i;
11569 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11570 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11571 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11572 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11573 dev_priv->shared_dplls[i].get_hw_state =
11574 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11575 }
11576}
11577
7c74ade1
DV
11578static void intel_shared_dpll_init(struct drm_device *dev)
11579{
e7b903d2 11580 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11581
9cd86933
DV
11582 if (HAS_DDI(dev))
11583 intel_ddi_pll_init(dev);
11584 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11585 ibx_pch_dpll_init(dev);
11586 else
11587 dev_priv->num_shared_dpll = 0;
11588
11589 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11590}
11591
465c120c
MR
11592static int
11593intel_primary_plane_disable(struct drm_plane *plane)
11594{
11595 struct drm_device *dev = plane->dev;
465c120c
MR
11596 struct intel_crtc *intel_crtc;
11597
11598 if (!plane->fb)
11599 return 0;
11600
11601 BUG_ON(!plane->crtc);
11602
11603 intel_crtc = to_intel_crtc(plane->crtc);
11604
11605 /*
11606 * Even though we checked plane->fb above, it's still possible that
11607 * the primary plane has been implicitly disabled because the crtc
11608 * coordinates given weren't visible, or because we detected
11609 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11610 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11611 * In either case, we need to unpin the FB and let the fb pointer get
11612 * updated, but otherwise we don't need to touch the hardware.
11613 */
11614 if (!intel_crtc->primary_enabled)
11615 goto disable_unpin;
11616
11617 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11618 intel_disable_primary_hw_plane(plane, plane->crtc);
11619
465c120c 11620disable_unpin:
4c34574f 11621 mutex_lock(&dev->struct_mutex);
2ff8fde1 11622 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11623 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11624 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11625 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11626 plane->fb = NULL;
11627
11628 return 0;
11629}
11630
11631static int
11632intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11633 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11634 unsigned int crtc_w, unsigned int crtc_h,
11635 uint32_t src_x, uint32_t src_y,
11636 uint32_t src_w, uint32_t src_h)
11637{
11638 struct drm_device *dev = crtc->dev;
48404c1e 11639 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2ff8fde1
MR
11641 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11642 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
465c120c
MR
11643 struct drm_rect dest = {
11644 /* integer pixels */
11645 .x1 = crtc_x,
11646 .y1 = crtc_y,
11647 .x2 = crtc_x + crtc_w,
11648 .y2 = crtc_y + crtc_h,
11649 };
11650 struct drm_rect src = {
11651 /* 16.16 fixed point */
11652 .x1 = src_x,
11653 .y1 = src_y,
11654 .x2 = src_x + src_w,
11655 .y2 = src_y + src_h,
11656 };
11657 const struct drm_rect clip = {
11658 /* integer pixels */
11659 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11660 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11661 };
ce54d85a
SJ
11662 const struct {
11663 int crtc_x, crtc_y;
11664 unsigned int crtc_w, crtc_h;
11665 uint32_t src_x, src_y, src_w, src_h;
11666 } orig = {
11667 .crtc_x = crtc_x,
11668 .crtc_y = crtc_y,
11669 .crtc_w = crtc_w,
11670 .crtc_h = crtc_h,
11671 .src_x = src_x,
11672 .src_y = src_y,
11673 .src_w = src_w,
11674 .src_h = src_h,
11675 };
11676 struct intel_plane *intel_plane = to_intel_plane(plane);
465c120c
MR
11677 bool visible;
11678 int ret;
11679
11680 ret = drm_plane_helper_check_update(plane, crtc, fb,
11681 &src, &dest, &clip,
11682 DRM_PLANE_HELPER_NO_SCALING,
11683 DRM_PLANE_HELPER_NO_SCALING,
11684 false, true, &visible);
11685
11686 if (ret)
11687 return ret;
11688
11689 /*
11690 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11691 * updating the fb pointer, and returning without touching the
11692 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11693 * turn on the display with all planes setup as desired.
11694 */
11695 if (!crtc->enabled) {
4c34574f
MR
11696 mutex_lock(&dev->struct_mutex);
11697
465c120c
MR
11698 /*
11699 * If we already called setplane while the crtc was disabled,
11700 * we may have an fb pinned; unpin it.
11701 */
11702 if (plane->fb)
a071fa00
DV
11703 intel_unpin_fb_obj(old_obj);
11704
11705 i915_gem_track_fb(old_obj, obj,
11706 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
465c120c
MR
11707
11708 /* Pin and return without programming hardware */
4c34574f
MR
11709 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11710 mutex_unlock(&dev->struct_mutex);
11711
11712 return ret;
465c120c
MR
11713 }
11714
11715 intel_crtc_wait_for_pending_flips(crtc);
11716
11717 /*
11718 * If clipping results in a non-visible primary plane, we'll disable
11719 * the primary plane. Note that this is a bit different than what
11720 * happens if userspace explicitly disables the plane by passing fb=0
11721 * because plane->fb still gets set and pinned.
11722 */
11723 if (!visible) {
4c34574f
MR
11724 mutex_lock(&dev->struct_mutex);
11725
465c120c
MR
11726 /*
11727 * Try to pin the new fb first so that we can bail out if we
11728 * fail.
11729 */
11730 if (plane->fb != fb) {
a071fa00 11731 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
4c34574f
MR
11732 if (ret) {
11733 mutex_unlock(&dev->struct_mutex);
465c120c 11734 return ret;
4c34574f 11735 }
465c120c
MR
11736 }
11737
a071fa00
DV
11738 i915_gem_track_fb(old_obj, obj,
11739 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11740
465c120c 11741 if (intel_crtc->primary_enabled)
fdd508a6 11742 intel_disable_primary_hw_plane(plane, crtc);
465c120c
MR
11743
11744
11745 if (plane->fb != fb)
11746 if (plane->fb)
a071fa00 11747 intel_unpin_fb_obj(old_obj);
465c120c 11748
4c34574f
MR
11749 mutex_unlock(&dev->struct_mutex);
11750
ce54d85a 11751 } else {
48404c1e
SJ
11752 if (intel_crtc && intel_crtc->active &&
11753 intel_crtc->primary_enabled) {
11754 /*
11755 * FBC does not work on some platforms for rotated
11756 * planes, so disable it when rotation is not 0 and
11757 * update it when rotation is set back to 0.
11758 *
11759 * FIXME: This is redundant with the fbc update done in
11760 * the primary plane enable function except that that
11761 * one is done too late. We eventually need to unify
11762 * this.
11763 */
11764 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11765 dev_priv->fbc.plane == intel_crtc->plane &&
11766 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11767 intel_disable_fbc(dev);
11768 }
11769 }
ce54d85a
SJ
11770 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11771 if (ret)
11772 return ret;
465c120c 11773
ce54d85a
SJ
11774 if (!intel_crtc->primary_enabled)
11775 intel_enable_primary_hw_plane(plane, crtc);
11776 }
465c120c 11777
ce54d85a
SJ
11778 intel_plane->crtc_x = orig.crtc_x;
11779 intel_plane->crtc_y = orig.crtc_y;
11780 intel_plane->crtc_w = orig.crtc_w;
11781 intel_plane->crtc_h = orig.crtc_h;
11782 intel_plane->src_x = orig.src_x;
11783 intel_plane->src_y = orig.src_y;
11784 intel_plane->src_w = orig.src_w;
11785 intel_plane->src_h = orig.src_h;
11786 intel_plane->obj = obj;
465c120c
MR
11787
11788 return 0;
11789}
11790
3d7d6510
MR
11791/* Common destruction function for both primary and cursor planes */
11792static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11793{
11794 struct intel_plane *intel_plane = to_intel_plane(plane);
11795 drm_plane_cleanup(plane);
11796 kfree(intel_plane);
11797}
11798
11799static const struct drm_plane_funcs intel_primary_plane_funcs = {
11800 .update_plane = intel_primary_plane_setplane,
11801 .disable_plane = intel_primary_plane_disable,
3d7d6510 11802 .destroy = intel_plane_destroy,
48404c1e 11803 .set_property = intel_plane_set_property
465c120c
MR
11804};
11805
11806static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11807 int pipe)
11808{
11809 struct intel_plane *primary;
11810 const uint32_t *intel_primary_formats;
11811 int num_formats;
11812
11813 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11814 if (primary == NULL)
11815 return NULL;
11816
11817 primary->can_scale = false;
11818 primary->max_downscale = 1;
11819 primary->pipe = pipe;
11820 primary->plane = pipe;
48404c1e 11821 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
11822 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11823 primary->plane = !pipe;
11824
11825 if (INTEL_INFO(dev)->gen <= 3) {
11826 intel_primary_formats = intel_primary_formats_gen2;
11827 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11828 } else {
11829 intel_primary_formats = intel_primary_formats_gen4;
11830 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11831 }
11832
11833 drm_universal_plane_init(dev, &primary->base, 0,
11834 &intel_primary_plane_funcs,
11835 intel_primary_formats, num_formats,
11836 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
11837
11838 if (INTEL_INFO(dev)->gen >= 4) {
11839 if (!dev->mode_config.rotation_property)
11840 dev->mode_config.rotation_property =
11841 drm_mode_create_rotation_property(dev,
11842 BIT(DRM_ROTATE_0) |
11843 BIT(DRM_ROTATE_180));
11844 if (dev->mode_config.rotation_property)
11845 drm_object_attach_property(&primary->base.base,
11846 dev->mode_config.rotation_property,
11847 primary->rotation);
11848 }
11849
465c120c
MR
11850 return &primary->base;
11851}
11852
3d7d6510
MR
11853static int
11854intel_cursor_plane_disable(struct drm_plane *plane)
11855{
11856 if (!plane->fb)
11857 return 0;
11858
11859 BUG_ON(!plane->crtc);
11860
11861 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11862}
11863
11864static int
11865intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11866 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11867 unsigned int crtc_w, unsigned int crtc_h,
11868 uint32_t src_x, uint32_t src_y,
11869 uint32_t src_w, uint32_t src_h)
11870{
11871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11872 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11873 struct drm_i915_gem_object *obj = intel_fb->obj;
11874 struct drm_rect dest = {
11875 /* integer pixels */
11876 .x1 = crtc_x,
11877 .y1 = crtc_y,
11878 .x2 = crtc_x + crtc_w,
11879 .y2 = crtc_y + crtc_h,
11880 };
11881 struct drm_rect src = {
11882 /* 16.16 fixed point */
11883 .x1 = src_x,
11884 .y1 = src_y,
11885 .x2 = src_x + src_w,
11886 .y2 = src_y + src_h,
11887 };
11888 const struct drm_rect clip = {
11889 /* integer pixels */
1add143c
VS
11890 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11891 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
3d7d6510
MR
11892 };
11893 bool visible;
11894 int ret;
11895
11896 ret = drm_plane_helper_check_update(plane, crtc, fb,
11897 &src, &dest, &clip,
11898 DRM_PLANE_HELPER_NO_SCALING,
11899 DRM_PLANE_HELPER_NO_SCALING,
11900 true, true, &visible);
11901 if (ret)
11902 return ret;
11903
11904 crtc->cursor_x = crtc_x;
11905 crtc->cursor_y = crtc_y;
11906 if (fb != crtc->cursor->fb) {
11907 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11908 } else {
11909 intel_crtc_update_cursor(crtc, visible);
4ed91096
DV
11910
11911 intel_frontbuffer_flip(crtc->dev,
11912 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11913
3d7d6510
MR
11914 return 0;
11915 }
11916}
11917static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11918 .update_plane = intel_cursor_plane_update,
11919 .disable_plane = intel_cursor_plane_disable,
11920 .destroy = intel_plane_destroy,
11921};
11922
11923static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11924 int pipe)
11925{
11926 struct intel_plane *cursor;
11927
11928 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11929 if (cursor == NULL)
11930 return NULL;
11931
11932 cursor->can_scale = false;
11933 cursor->max_downscale = 1;
11934 cursor->pipe = pipe;
11935 cursor->plane = pipe;
11936
11937 drm_universal_plane_init(dev, &cursor->base, 0,
11938 &intel_cursor_plane_funcs,
11939 intel_cursor_formats,
11940 ARRAY_SIZE(intel_cursor_formats),
11941 DRM_PLANE_TYPE_CURSOR);
11942 return &cursor->base;
11943}
11944
b358d0a6 11945static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 11946{
fbee40df 11947 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 11948 struct intel_crtc *intel_crtc;
3d7d6510
MR
11949 struct drm_plane *primary = NULL;
11950 struct drm_plane *cursor = NULL;
465c120c 11951 int i, ret;
79e53945 11952
955382f3 11953 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
11954 if (intel_crtc == NULL)
11955 return;
11956
465c120c 11957 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
11958 if (!primary)
11959 goto fail;
11960
11961 cursor = intel_cursor_plane_create(dev, pipe);
11962 if (!cursor)
11963 goto fail;
11964
465c120c 11965 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
11966 cursor, &intel_crtc_funcs);
11967 if (ret)
11968 goto fail;
79e53945
JB
11969
11970 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
11971 for (i = 0; i < 256; i++) {
11972 intel_crtc->lut_r[i] = i;
11973 intel_crtc->lut_g[i] = i;
11974 intel_crtc->lut_b[i] = i;
11975 }
11976
1f1c2e24
VS
11977 /*
11978 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 11979 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 11980 */
80824003
JB
11981 intel_crtc->pipe = pipe;
11982 intel_crtc->plane = pipe;
3a77c4c4 11983 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 11984 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 11985 intel_crtc->plane = !pipe;
80824003
JB
11986 }
11987
4b0e333e
CW
11988 intel_crtc->cursor_base = ~0;
11989 intel_crtc->cursor_cntl = ~0;
dc41c154 11990 intel_crtc->cursor_size = ~0;
4b0e333e 11991
22fd0fab
JB
11992 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11993 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11994 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11995 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11996
79e53945 11997 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
11998
11999 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12000 return;
12001
12002fail:
12003 if (primary)
12004 drm_plane_cleanup(primary);
12005 if (cursor)
12006 drm_plane_cleanup(cursor);
12007 kfree(intel_crtc);
79e53945
JB
12008}
12009
752aa88a
JB
12010enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12011{
12012 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12013 struct drm_device *dev = connector->base.dev;
752aa88a 12014
51fd371b 12015 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12016
12017 if (!encoder)
12018 return INVALID_PIPE;
12019
12020 return to_intel_crtc(encoder->crtc)->pipe;
12021}
12022
08d7b3d1 12023int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12024 struct drm_file *file)
08d7b3d1 12025{
08d7b3d1 12026 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12027 struct drm_crtc *drmmode_crtc;
c05422d5 12028 struct intel_crtc *crtc;
08d7b3d1 12029
1cff8f6b
DV
12030 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12031 return -ENODEV;
08d7b3d1 12032
7707e653 12033 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12034
7707e653 12035 if (!drmmode_crtc) {
08d7b3d1 12036 DRM_ERROR("no such CRTC id\n");
3f2c2057 12037 return -ENOENT;
08d7b3d1
CW
12038 }
12039
7707e653 12040 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12041 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12042
c05422d5 12043 return 0;
08d7b3d1
CW
12044}
12045
66a9278e 12046static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12047{
66a9278e
DV
12048 struct drm_device *dev = encoder->base.dev;
12049 struct intel_encoder *source_encoder;
79e53945 12050 int index_mask = 0;
79e53945
JB
12051 int entry = 0;
12052
b2784e15 12053 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12054 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12055 index_mask |= (1 << entry);
12056
79e53945
JB
12057 entry++;
12058 }
4ef69c7a 12059
79e53945
JB
12060 return index_mask;
12061}
12062
4d302442
CW
12063static bool has_edp_a(struct drm_device *dev)
12064{
12065 struct drm_i915_private *dev_priv = dev->dev_private;
12066
12067 if (!IS_MOBILE(dev))
12068 return false;
12069
12070 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12071 return false;
12072
e3589908 12073 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12074 return false;
12075
12076 return true;
12077}
12078
ba0fbca4
DL
12079const char *intel_output_name(int output)
12080{
12081 static const char *names[] = {
12082 [INTEL_OUTPUT_UNUSED] = "Unused",
12083 [INTEL_OUTPUT_ANALOG] = "Analog",
12084 [INTEL_OUTPUT_DVO] = "DVO",
12085 [INTEL_OUTPUT_SDVO] = "SDVO",
12086 [INTEL_OUTPUT_LVDS] = "LVDS",
12087 [INTEL_OUTPUT_TVOUT] = "TV",
12088 [INTEL_OUTPUT_HDMI] = "HDMI",
12089 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12090 [INTEL_OUTPUT_EDP] = "eDP",
12091 [INTEL_OUTPUT_DSI] = "DSI",
12092 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12093 };
12094
12095 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12096 return "Invalid";
12097
12098 return names[output];
12099}
12100
84b4e042
JB
12101static bool intel_crt_present(struct drm_device *dev)
12102{
12103 struct drm_i915_private *dev_priv = dev->dev_private;
12104
12105 if (IS_ULT(dev))
12106 return false;
12107
12108 if (IS_CHERRYVIEW(dev))
12109 return false;
12110
12111 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12112 return false;
12113
12114 return true;
12115}
12116
79e53945
JB
12117static void intel_setup_outputs(struct drm_device *dev)
12118{
725e30ad 12119 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12120 struct intel_encoder *encoder;
cb0953d7 12121 bool dpd_is_edp = false;
79e53945 12122
c9093354 12123 intel_lvds_init(dev);
79e53945 12124
84b4e042 12125 if (intel_crt_present(dev))
79935fca 12126 intel_crt_init(dev);
cb0953d7 12127
affa9354 12128 if (HAS_DDI(dev)) {
0e72a5b5
ED
12129 int found;
12130
12131 /* Haswell uses DDI functions to detect digital outputs */
12132 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12133 /* DDI A only supports eDP */
12134 if (found)
12135 intel_ddi_init(dev, PORT_A);
12136
12137 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12138 * register */
12139 found = I915_READ(SFUSE_STRAP);
12140
12141 if (found & SFUSE_STRAP_DDIB_DETECTED)
12142 intel_ddi_init(dev, PORT_B);
12143 if (found & SFUSE_STRAP_DDIC_DETECTED)
12144 intel_ddi_init(dev, PORT_C);
12145 if (found & SFUSE_STRAP_DDID_DETECTED)
12146 intel_ddi_init(dev, PORT_D);
12147 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12148 int found;
5d8a7752 12149 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12150
12151 if (has_edp_a(dev))
12152 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12153
dc0fa718 12154 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12155 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12156 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12157 if (!found)
e2debe91 12158 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12159 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12160 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12161 }
12162
dc0fa718 12163 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12164 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12165
dc0fa718 12166 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12167 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12168
5eb08b69 12169 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12170 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12171
270b3042 12172 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12173 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12174 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
12175 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12176 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12177 PORT_B);
12178 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12179 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12180 }
12181
6f6005a5
JB
12182 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12183 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12184 PORT_C);
12185 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 12186 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 12187 }
19c03924 12188
9418c1f1
VS
12189 if (IS_CHERRYVIEW(dev)) {
12190 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12191 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12192 PORT_D);
12193 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12194 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12195 }
12196 }
12197
3cfca973 12198 intel_dsi_init(dev);
103a196f 12199 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12200 bool found = false;
7d57382e 12201
e2debe91 12202 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12203 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12204 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12205 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12206 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12207 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12208 }
27185ae1 12209
e7281eab 12210 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12211 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12212 }
13520b05
KH
12213
12214 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12215
e2debe91 12216 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12217 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12218 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12219 }
27185ae1 12220
e2debe91 12221 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12222
b01f2c3a
JB
12223 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12224 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12225 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12226 }
e7281eab 12227 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12228 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12229 }
27185ae1 12230
b01f2c3a 12231 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12232 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12233 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12234 } else if (IS_GEN2(dev))
79e53945
JB
12235 intel_dvo_init(dev);
12236
103a196f 12237 if (SUPPORTS_TV(dev))
79e53945
JB
12238 intel_tv_init(dev);
12239
7c8f8a70
RV
12240 intel_edp_psr_init(dev);
12241
b2784e15 12242 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12243 encoder->base.possible_crtcs = encoder->crtc_mask;
12244 encoder->base.possible_clones =
66a9278e 12245 intel_encoder_clones(encoder);
79e53945 12246 }
47356eb6 12247
dde86e2d 12248 intel_init_pch_refclk(dev);
270b3042
DV
12249
12250 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12251}
12252
12253static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12254{
60a5ca01 12255 struct drm_device *dev = fb->dev;
79e53945 12256 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12257
ef2d633e 12258 drm_framebuffer_cleanup(fb);
60a5ca01 12259 mutex_lock(&dev->struct_mutex);
ef2d633e 12260 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12261 drm_gem_object_unreference(&intel_fb->obj->base);
12262 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12263 kfree(intel_fb);
12264}
12265
12266static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12267 struct drm_file *file,
79e53945
JB
12268 unsigned int *handle)
12269{
12270 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12271 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12272
05394f39 12273 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12274}
12275
12276static const struct drm_framebuffer_funcs intel_fb_funcs = {
12277 .destroy = intel_user_framebuffer_destroy,
12278 .create_handle = intel_user_framebuffer_create_handle,
12279};
12280
b5ea642a
DV
12281static int intel_framebuffer_init(struct drm_device *dev,
12282 struct intel_framebuffer *intel_fb,
12283 struct drm_mode_fb_cmd2 *mode_cmd,
12284 struct drm_i915_gem_object *obj)
79e53945 12285{
a57ce0b2 12286 int aligned_height;
a35cdaa0 12287 int pitch_limit;
79e53945
JB
12288 int ret;
12289
dd4916c5
DV
12290 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12291
c16ed4be
CW
12292 if (obj->tiling_mode == I915_TILING_Y) {
12293 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12294 return -EINVAL;
c16ed4be 12295 }
57cd6508 12296
c16ed4be
CW
12297 if (mode_cmd->pitches[0] & 63) {
12298 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12299 mode_cmd->pitches[0]);
57cd6508 12300 return -EINVAL;
c16ed4be 12301 }
57cd6508 12302
a35cdaa0
CW
12303 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12304 pitch_limit = 32*1024;
12305 } else if (INTEL_INFO(dev)->gen >= 4) {
12306 if (obj->tiling_mode)
12307 pitch_limit = 16*1024;
12308 else
12309 pitch_limit = 32*1024;
12310 } else if (INTEL_INFO(dev)->gen >= 3) {
12311 if (obj->tiling_mode)
12312 pitch_limit = 8*1024;
12313 else
12314 pitch_limit = 16*1024;
12315 } else
12316 /* XXX DSPC is limited to 4k tiled */
12317 pitch_limit = 8*1024;
12318
12319 if (mode_cmd->pitches[0] > pitch_limit) {
12320 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12321 obj->tiling_mode ? "tiled" : "linear",
12322 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12323 return -EINVAL;
c16ed4be 12324 }
5d7bd705
VS
12325
12326 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12327 mode_cmd->pitches[0] != obj->stride) {
12328 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12329 mode_cmd->pitches[0], obj->stride);
5d7bd705 12330 return -EINVAL;
c16ed4be 12331 }
5d7bd705 12332
57779d06 12333 /* Reject formats not supported by any plane early. */
308e5bcb 12334 switch (mode_cmd->pixel_format) {
57779d06 12335 case DRM_FORMAT_C8:
04b3924d
VS
12336 case DRM_FORMAT_RGB565:
12337 case DRM_FORMAT_XRGB8888:
12338 case DRM_FORMAT_ARGB8888:
57779d06
VS
12339 break;
12340 case DRM_FORMAT_XRGB1555:
12341 case DRM_FORMAT_ARGB1555:
c16ed4be 12342 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12343 DRM_DEBUG("unsupported pixel format: %s\n",
12344 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12345 return -EINVAL;
c16ed4be 12346 }
57779d06
VS
12347 break;
12348 case DRM_FORMAT_XBGR8888:
12349 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12350 case DRM_FORMAT_XRGB2101010:
12351 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12352 case DRM_FORMAT_XBGR2101010:
12353 case DRM_FORMAT_ABGR2101010:
c16ed4be 12354 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12355 DRM_DEBUG("unsupported pixel format: %s\n",
12356 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12357 return -EINVAL;
c16ed4be 12358 }
b5626747 12359 break;
04b3924d
VS
12360 case DRM_FORMAT_YUYV:
12361 case DRM_FORMAT_UYVY:
12362 case DRM_FORMAT_YVYU:
12363 case DRM_FORMAT_VYUY:
c16ed4be 12364 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12365 DRM_DEBUG("unsupported pixel format: %s\n",
12366 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12367 return -EINVAL;
c16ed4be 12368 }
57cd6508
CW
12369 break;
12370 default:
4ee62c76
VS
12371 DRM_DEBUG("unsupported pixel format: %s\n",
12372 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12373 return -EINVAL;
12374 }
12375
90f9a336
VS
12376 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12377 if (mode_cmd->offsets[0] != 0)
12378 return -EINVAL;
12379
a57ce0b2
JB
12380 aligned_height = intel_align_height(dev, mode_cmd->height,
12381 obj->tiling_mode);
53155c0a
DV
12382 /* FIXME drm helper for size checks (especially planar formats)? */
12383 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12384 return -EINVAL;
12385
c7d73f6a
DV
12386 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12387 intel_fb->obj = obj;
80075d49 12388 intel_fb->obj->framebuffer_references++;
c7d73f6a 12389
79e53945
JB
12390 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12391 if (ret) {
12392 DRM_ERROR("framebuffer init failed %d\n", ret);
12393 return ret;
12394 }
12395
79e53945
JB
12396 return 0;
12397}
12398
79e53945
JB
12399static struct drm_framebuffer *
12400intel_user_framebuffer_create(struct drm_device *dev,
12401 struct drm_file *filp,
308e5bcb 12402 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12403{
05394f39 12404 struct drm_i915_gem_object *obj;
79e53945 12405
308e5bcb
JB
12406 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12407 mode_cmd->handles[0]));
c8725226 12408 if (&obj->base == NULL)
cce13ff7 12409 return ERR_PTR(-ENOENT);
79e53945 12410
d2dff872 12411 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12412}
12413
4520f53a 12414#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12415static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12416{
12417}
12418#endif
12419
79e53945 12420static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12421 .fb_create = intel_user_framebuffer_create,
0632fef6 12422 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12423};
12424
e70236a8
JB
12425/* Set up chip specific display functions */
12426static void intel_init_display(struct drm_device *dev)
12427{
12428 struct drm_i915_private *dev_priv = dev->dev_private;
12429
ee9300bb
DV
12430 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12431 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12432 else if (IS_CHERRYVIEW(dev))
12433 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12434 else if (IS_VALLEYVIEW(dev))
12435 dev_priv->display.find_dpll = vlv_find_best_dpll;
12436 else if (IS_PINEVIEW(dev))
12437 dev_priv->display.find_dpll = pnv_find_best_dpll;
12438 else
12439 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12440
affa9354 12441 if (HAS_DDI(dev)) {
0e8ffe1b 12442 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12443 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12444 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12445 dev_priv->display.crtc_enable = haswell_crtc_enable;
12446 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12447 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12448 dev_priv->display.update_primary_plane =
12449 ironlake_update_primary_plane;
09b4ddf9 12450 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12451 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12452 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12453 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12454 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12455 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12456 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12457 dev_priv->display.update_primary_plane =
12458 ironlake_update_primary_plane;
89b667f8
JB
12459 } else if (IS_VALLEYVIEW(dev)) {
12460 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12461 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12462 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12463 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12464 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12465 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12466 dev_priv->display.update_primary_plane =
12467 i9xx_update_primary_plane;
f564048e 12468 } else {
0e8ffe1b 12469 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12470 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12471 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12472 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12473 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12474 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12475 dev_priv->display.update_primary_plane =
12476 i9xx_update_primary_plane;
f564048e 12477 }
e70236a8 12478
e70236a8 12479 /* Returns the core display clock speed */
25eb05fc
JB
12480 if (IS_VALLEYVIEW(dev))
12481 dev_priv->display.get_display_clock_speed =
12482 valleyview_get_display_clock_speed;
12483 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12484 dev_priv->display.get_display_clock_speed =
12485 i945_get_display_clock_speed;
12486 else if (IS_I915G(dev))
12487 dev_priv->display.get_display_clock_speed =
12488 i915_get_display_clock_speed;
257a7ffc 12489 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12490 dev_priv->display.get_display_clock_speed =
12491 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12492 else if (IS_PINEVIEW(dev))
12493 dev_priv->display.get_display_clock_speed =
12494 pnv_get_display_clock_speed;
e70236a8
JB
12495 else if (IS_I915GM(dev))
12496 dev_priv->display.get_display_clock_speed =
12497 i915gm_get_display_clock_speed;
12498 else if (IS_I865G(dev))
12499 dev_priv->display.get_display_clock_speed =
12500 i865_get_display_clock_speed;
f0f8a9ce 12501 else if (IS_I85X(dev))
e70236a8
JB
12502 dev_priv->display.get_display_clock_speed =
12503 i855_get_display_clock_speed;
12504 else /* 852, 830 */
12505 dev_priv->display.get_display_clock_speed =
12506 i830_get_display_clock_speed;
12507
3bb11b53 12508 if (IS_G4X(dev)) {
e0dac65e 12509 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12510 } else if (IS_GEN5(dev)) {
12511 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12512 dev_priv->display.write_eld = ironlake_write_eld;
12513 } else if (IS_GEN6(dev)) {
12514 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12515 dev_priv->display.write_eld = ironlake_write_eld;
12516 dev_priv->display.modeset_global_resources =
12517 snb_modeset_global_resources;
12518 } else if (IS_IVYBRIDGE(dev)) {
12519 /* FIXME: detect B0+ stepping and use auto training */
12520 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12521 dev_priv->display.write_eld = ironlake_write_eld;
12522 dev_priv->display.modeset_global_resources =
12523 ivb_modeset_global_resources;
059b2fe9 12524 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53
SJ
12525 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12526 dev_priv->display.write_eld = haswell_write_eld;
12527 dev_priv->display.modeset_global_resources =
12528 haswell_modeset_global_resources;
30a970c6
JB
12529 } else if (IS_VALLEYVIEW(dev)) {
12530 dev_priv->display.modeset_global_resources =
12531 valleyview_modeset_global_resources;
9ca2fe73 12532 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 12533 }
8c9f3aaf
JB
12534
12535 /* Default just returns -ENODEV to indicate unsupported */
12536 dev_priv->display.queue_flip = intel_default_queue_flip;
12537
12538 switch (INTEL_INFO(dev)->gen) {
12539 case 2:
12540 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12541 break;
12542
12543 case 3:
12544 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12545 break;
12546
12547 case 4:
12548 case 5:
12549 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12550 break;
12551
12552 case 6:
12553 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12554 break;
7c9017e5 12555 case 7:
4e0bbc31 12556 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12557 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12558 break;
8c9f3aaf 12559 }
7bd688cd
JN
12560
12561 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12562
12563 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12564}
12565
b690e96c
JB
12566/*
12567 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12568 * resume, or other times. This quirk makes sure that's the case for
12569 * affected systems.
12570 */
0206e353 12571static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12572{
12573 struct drm_i915_private *dev_priv = dev->dev_private;
12574
12575 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12576 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12577}
12578
b6b5d049
VS
12579static void quirk_pipeb_force(struct drm_device *dev)
12580{
12581 struct drm_i915_private *dev_priv = dev->dev_private;
12582
12583 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12584 DRM_INFO("applying pipe b force quirk\n");
12585}
12586
435793df
KP
12587/*
12588 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12589 */
12590static void quirk_ssc_force_disable(struct drm_device *dev)
12591{
12592 struct drm_i915_private *dev_priv = dev->dev_private;
12593 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12594 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12595}
12596
4dca20ef 12597/*
5a15ab5b
CE
12598 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12599 * brightness value
4dca20ef
CE
12600 */
12601static void quirk_invert_brightness(struct drm_device *dev)
12602{
12603 struct drm_i915_private *dev_priv = dev->dev_private;
12604 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12605 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12606}
12607
9c72cc6f
SD
12608/* Some VBT's incorrectly indicate no backlight is present */
12609static void quirk_backlight_present(struct drm_device *dev)
12610{
12611 struct drm_i915_private *dev_priv = dev->dev_private;
12612 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12613 DRM_INFO("applying backlight present quirk\n");
12614}
12615
b690e96c
JB
12616struct intel_quirk {
12617 int device;
12618 int subsystem_vendor;
12619 int subsystem_device;
12620 void (*hook)(struct drm_device *dev);
12621};
12622
5f85f176
EE
12623/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12624struct intel_dmi_quirk {
12625 void (*hook)(struct drm_device *dev);
12626 const struct dmi_system_id (*dmi_id_list)[];
12627};
12628
12629static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12630{
12631 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12632 return 1;
12633}
12634
12635static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12636 {
12637 .dmi_id_list = &(const struct dmi_system_id[]) {
12638 {
12639 .callback = intel_dmi_reverse_brightness,
12640 .ident = "NCR Corporation",
12641 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12642 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12643 },
12644 },
12645 { } /* terminating entry */
12646 },
12647 .hook = quirk_invert_brightness,
12648 },
12649};
12650
c43b5634 12651static struct intel_quirk intel_quirks[] = {
b690e96c 12652 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12653 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12654
b690e96c
JB
12655 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12656 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12657
b690e96c
JB
12658 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12659 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12660
5f080c0f
VS
12661 /* 830 needs to leave pipe A & dpll A up */
12662 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12663
b6b5d049
VS
12664 /* 830 needs to leave pipe B & dpll B up */
12665 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12666
435793df
KP
12667 /* Lenovo U160 cannot use SSC on LVDS */
12668 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12669
12670 /* Sony Vaio Y cannot use SSC on LVDS */
12671 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12672
be505f64
AH
12673 /* Acer Aspire 5734Z must invert backlight brightness */
12674 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12675
12676 /* Acer/eMachines G725 */
12677 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12678
12679 /* Acer/eMachines e725 */
12680 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12681
12682 /* Acer/Packard Bell NCL20 */
12683 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12684
12685 /* Acer Aspire 4736Z */
12686 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12687
12688 /* Acer Aspire 5336 */
12689 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12690
12691 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12692 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c
SD
12693
12694 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12695 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12696
12697 /* HP Chromebook 14 (Celeron 2955U) */
12698 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12699};
12700
12701static void intel_init_quirks(struct drm_device *dev)
12702{
12703 struct pci_dev *d = dev->pdev;
12704 int i;
12705
12706 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12707 struct intel_quirk *q = &intel_quirks[i];
12708
12709 if (d->device == q->device &&
12710 (d->subsystem_vendor == q->subsystem_vendor ||
12711 q->subsystem_vendor == PCI_ANY_ID) &&
12712 (d->subsystem_device == q->subsystem_device ||
12713 q->subsystem_device == PCI_ANY_ID))
12714 q->hook(dev);
12715 }
5f85f176
EE
12716 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12717 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12718 intel_dmi_quirks[i].hook(dev);
12719 }
b690e96c
JB
12720}
12721
9cce37f4
JB
12722/* Disable the VGA plane that we never use */
12723static void i915_disable_vga(struct drm_device *dev)
12724{
12725 struct drm_i915_private *dev_priv = dev->dev_private;
12726 u8 sr1;
766aa1c4 12727 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12728
2b37c616 12729 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12730 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12731 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12732 sr1 = inb(VGA_SR_DATA);
12733 outb(sr1 | 1<<5, VGA_SR_DATA);
12734 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12735 udelay(300);
12736
69769f9a
VS
12737 /*
12738 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12739 * from S3 without preserving (some of?) the other bits.
12740 */
12741 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
9cce37f4
JB
12742 POSTING_READ(vga_reg);
12743}
12744
f817586c
DV
12745void intel_modeset_init_hw(struct drm_device *dev)
12746{
a8f78b58
ED
12747 intel_prepare_ddi(dev);
12748
f8bf63fd
VS
12749 if (IS_VALLEYVIEW(dev))
12750 vlv_update_cdclk(dev);
12751
f817586c
DV
12752 intel_init_clock_gating(dev);
12753
8090c6b9 12754 intel_enable_gt_powersave(dev);
f817586c
DV
12755}
12756
7d708ee4
ID
12757void intel_modeset_suspend_hw(struct drm_device *dev)
12758{
12759 intel_suspend_hw(dev);
12760}
12761
79e53945
JB
12762void intel_modeset_init(struct drm_device *dev)
12763{
652c393a 12764 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12765 int sprite, ret;
8cc87b75 12766 enum pipe pipe;
46f297fb 12767 struct intel_crtc *crtc;
79e53945
JB
12768
12769 drm_mode_config_init(dev);
12770
12771 dev->mode_config.min_width = 0;
12772 dev->mode_config.min_height = 0;
12773
019d96cb
DA
12774 dev->mode_config.preferred_depth = 24;
12775 dev->mode_config.prefer_shadow = 1;
12776
e6ecefaa 12777 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12778
b690e96c
JB
12779 intel_init_quirks(dev);
12780
1fa61106
ED
12781 intel_init_pm(dev);
12782
e3c74757
BW
12783 if (INTEL_INFO(dev)->num_pipes == 0)
12784 return;
12785
e70236a8
JB
12786 intel_init_display(dev);
12787
a6c45cf0
CW
12788 if (IS_GEN2(dev)) {
12789 dev->mode_config.max_width = 2048;
12790 dev->mode_config.max_height = 2048;
12791 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12792 dev->mode_config.max_width = 4096;
12793 dev->mode_config.max_height = 4096;
79e53945 12794 } else {
a6c45cf0
CW
12795 dev->mode_config.max_width = 8192;
12796 dev->mode_config.max_height = 8192;
79e53945 12797 }
068be561 12798
dc41c154
VS
12799 if (IS_845G(dev) || IS_I865G(dev)) {
12800 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12801 dev->mode_config.cursor_height = 1023;
12802 } else if (IS_GEN2(dev)) {
068be561
DL
12803 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12804 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12805 } else {
12806 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12807 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12808 }
12809
5d4545ae 12810 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12811
28c97730 12812 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12813 INTEL_INFO(dev)->num_pipes,
12814 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12815
055e393f 12816 for_each_pipe(dev_priv, pipe) {
8cc87b75 12817 intel_crtc_init(dev, pipe);
1fe47785
DL
12818 for_each_sprite(pipe, sprite) {
12819 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12820 if (ret)
06da8da2 12821 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12822 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12823 }
79e53945
JB
12824 }
12825
f42bb70d
JB
12826 intel_init_dpio(dev);
12827
e72f9fbf 12828 intel_shared_dpll_init(dev);
ee7b9f93 12829
69769f9a
VS
12830 /* save the BIOS value before clobbering it */
12831 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
9cce37f4
JB
12832 /* Just disable it once at startup */
12833 i915_disable_vga(dev);
79e53945 12834 intel_setup_outputs(dev);
11be49eb
CW
12835
12836 /* Just in case the BIOS is doing something questionable. */
12837 intel_disable_fbc(dev);
fa9fa083 12838
6e9f798d 12839 drm_modeset_lock_all(dev);
fa9fa083 12840 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12841 drm_modeset_unlock_all(dev);
46f297fb 12842
d3fcc808 12843 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12844 if (!crtc->active)
12845 continue;
12846
46f297fb 12847 /*
46f297fb
JB
12848 * Note that reserving the BIOS fb up front prevents us
12849 * from stuffing other stolen allocations like the ring
12850 * on top. This prevents some ugliness at boot time, and
12851 * can even allow for smooth boot transitions if the BIOS
12852 * fb is large enough for the active pipe configuration.
12853 */
12854 if (dev_priv->display.get_plane_config) {
12855 dev_priv->display.get_plane_config(crtc,
12856 &crtc->plane_config);
12857 /*
12858 * If the fb is shared between multiple heads, we'll
12859 * just get the first one.
12860 */
484b41dd 12861 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12862 }
46f297fb 12863 }
2c7111db
CW
12864}
12865
7fad798e
DV
12866static void intel_enable_pipe_a(struct drm_device *dev)
12867{
12868 struct intel_connector *connector;
12869 struct drm_connector *crt = NULL;
12870 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 12871 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
12872
12873 /* We can't just switch on the pipe A, we need to set things up with a
12874 * proper mode and output configuration. As a gross hack, enable pipe A
12875 * by enabling the load detect pipe once. */
12876 list_for_each_entry(connector,
12877 &dev->mode_config.connector_list,
12878 base.head) {
12879 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12880 crt = &connector->base;
12881 break;
12882 }
12883 }
12884
12885 if (!crt)
12886 return;
12887
208bf9fd
VS
12888 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12889 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
12890}
12891
fa555837
DV
12892static bool
12893intel_check_plane_mapping(struct intel_crtc *crtc)
12894{
7eb552ae
BW
12895 struct drm_device *dev = crtc->base.dev;
12896 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12897 u32 reg, val;
12898
7eb552ae 12899 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
12900 return true;
12901
12902 reg = DSPCNTR(!crtc->plane);
12903 val = I915_READ(reg);
12904
12905 if ((val & DISPLAY_PLANE_ENABLE) &&
12906 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12907 return false;
12908
12909 return true;
12910}
12911
24929352
DV
12912static void intel_sanitize_crtc(struct intel_crtc *crtc)
12913{
12914 struct drm_device *dev = crtc->base.dev;
12915 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 12916 u32 reg;
24929352 12917
24929352 12918 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 12919 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
12920 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12921
d3eaf884
VS
12922 /* restore vblank interrupts to correct state */
12923 if (crtc->active)
12924 drm_vblank_on(dev, crtc->pipe);
12925 else
12926 drm_vblank_off(dev, crtc->pipe);
12927
24929352 12928 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
12929 * disable the crtc (and hence change the state) if it is wrong. Note
12930 * that gen4+ has a fixed plane -> pipe mapping. */
12931 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
12932 struct intel_connector *connector;
12933 bool plane;
12934
24929352
DV
12935 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12936 crtc->base.base.id);
12937
12938 /* Pipe has the wrong plane attached and the plane is active.
12939 * Temporarily change the plane mapping and disable everything
12940 * ... */
12941 plane = crtc->plane;
12942 crtc->plane = !plane;
9c8958bc 12943 crtc->primary_enabled = true;
24929352
DV
12944 dev_priv->display.crtc_disable(&crtc->base);
12945 crtc->plane = plane;
12946
12947 /* ... and break all links. */
12948 list_for_each_entry(connector, &dev->mode_config.connector_list,
12949 base.head) {
12950 if (connector->encoder->base.crtc != &crtc->base)
12951 continue;
12952
7f1950fb
EE
12953 connector->base.dpms = DRM_MODE_DPMS_OFF;
12954 connector->base.encoder = NULL;
24929352 12955 }
7f1950fb
EE
12956 /* multiple connectors may have the same encoder:
12957 * handle them and break crtc link separately */
12958 list_for_each_entry(connector, &dev->mode_config.connector_list,
12959 base.head)
12960 if (connector->encoder->base.crtc == &crtc->base) {
12961 connector->encoder->base.crtc = NULL;
12962 connector->encoder->connectors_active = false;
12963 }
24929352
DV
12964
12965 WARN_ON(crtc->active);
12966 crtc->base.enabled = false;
12967 }
24929352 12968
7fad798e
DV
12969 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12970 crtc->pipe == PIPE_A && !crtc->active) {
12971 /* BIOS forgot to enable pipe A, this mostly happens after
12972 * resume. Force-enable the pipe to fix this, the update_dpms
12973 * call below we restore the pipe to the right state, but leave
12974 * the required bits on. */
12975 intel_enable_pipe_a(dev);
12976 }
12977
24929352
DV
12978 /* Adjust the state of the output pipe according to whether we
12979 * have active connectors/encoders. */
12980 intel_crtc_update_dpms(&crtc->base);
12981
12982 if (crtc->active != crtc->base.enabled) {
12983 struct intel_encoder *encoder;
12984
12985 /* This can happen either due to bugs in the get_hw_state
12986 * functions or because the pipe is force-enabled due to the
12987 * pipe A quirk. */
12988 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12989 crtc->base.base.id,
12990 crtc->base.enabled ? "enabled" : "disabled",
12991 crtc->active ? "enabled" : "disabled");
12992
12993 crtc->base.enabled = crtc->active;
12994
12995 /* Because we only establish the connector -> encoder ->
12996 * crtc links if something is active, this means the
12997 * crtc is now deactivated. Break the links. connector
12998 * -> encoder links are only establish when things are
12999 * actually up, hence no need to break them. */
13000 WARN_ON(crtc->active);
13001
13002 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13003 WARN_ON(encoder->connectors_active);
13004 encoder->base.crtc = NULL;
13005 }
13006 }
c5ab3bc0 13007
a3ed6aad 13008 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13009 /*
13010 * We start out with underrun reporting disabled to avoid races.
13011 * For correct bookkeeping mark this on active crtcs.
13012 *
c5ab3bc0
DV
13013 * Also on gmch platforms we dont have any hardware bits to
13014 * disable the underrun reporting. Which means we need to start
13015 * out with underrun reporting disabled also on inactive pipes,
13016 * since otherwise we'll complain about the garbage we read when
13017 * e.g. coming up after runtime pm.
13018 *
4cc31489
DV
13019 * No protection against concurrent access is required - at
13020 * worst a fifo underrun happens which also sets this to false.
13021 */
13022 crtc->cpu_fifo_underrun_disabled = true;
13023 crtc->pch_fifo_underrun_disabled = true;
80715b2f
VS
13024
13025 update_scanline_offset(crtc);
4cc31489 13026 }
24929352
DV
13027}
13028
13029static void intel_sanitize_encoder(struct intel_encoder *encoder)
13030{
13031 struct intel_connector *connector;
13032 struct drm_device *dev = encoder->base.dev;
13033
13034 /* We need to check both for a crtc link (meaning that the
13035 * encoder is active and trying to read from a pipe) and the
13036 * pipe itself being active. */
13037 bool has_active_crtc = encoder->base.crtc &&
13038 to_intel_crtc(encoder->base.crtc)->active;
13039
13040 if (encoder->connectors_active && !has_active_crtc) {
13041 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13042 encoder->base.base.id,
8e329a03 13043 encoder->base.name);
24929352
DV
13044
13045 /* Connector is active, but has no active pipe. This is
13046 * fallout from our resume register restoring. Disable
13047 * the encoder manually again. */
13048 if (encoder->base.crtc) {
13049 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13050 encoder->base.base.id,
8e329a03 13051 encoder->base.name);
24929352 13052 encoder->disable(encoder);
a62d1497
VS
13053 if (encoder->post_disable)
13054 encoder->post_disable(encoder);
24929352 13055 }
7f1950fb
EE
13056 encoder->base.crtc = NULL;
13057 encoder->connectors_active = false;
24929352
DV
13058
13059 /* Inconsistent output/port/pipe state happens presumably due to
13060 * a bug in one of the get_hw_state functions. Or someplace else
13061 * in our code, like the register restore mess on resume. Clamp
13062 * things to off as a safer default. */
13063 list_for_each_entry(connector,
13064 &dev->mode_config.connector_list,
13065 base.head) {
13066 if (connector->encoder != encoder)
13067 continue;
7f1950fb
EE
13068 connector->base.dpms = DRM_MODE_DPMS_OFF;
13069 connector->base.encoder = NULL;
24929352
DV
13070 }
13071 }
13072 /* Enabled encoders without active connectors will be fixed in
13073 * the crtc fixup. */
13074}
13075
04098753 13076void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13077{
13078 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13079 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13080
04098753
ID
13081 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13082 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13083 i915_disable_vga(dev);
13084 }
13085}
13086
13087void i915_redisable_vga(struct drm_device *dev)
13088{
13089 struct drm_i915_private *dev_priv = dev->dev_private;
13090
8dc8a27c
PZ
13091 /* This function can be called both from intel_modeset_setup_hw_state or
13092 * at a very early point in our resume sequence, where the power well
13093 * structures are not yet restored. Since this function is at a very
13094 * paranoid "someone might have enabled VGA while we were not looking"
13095 * level, just check if the power well is enabled instead of trying to
13096 * follow the "don't touch the power well if we don't need it" policy
13097 * the rest of the driver uses. */
04098753 13098 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13099 return;
13100
04098753 13101 i915_redisable_vga_power_on(dev);
0fde901f
KM
13102}
13103
98ec7739
VS
13104static bool primary_get_hw_state(struct intel_crtc *crtc)
13105{
13106 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13107
13108 if (!crtc->active)
13109 return false;
13110
13111 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13112}
13113
30e984df 13114static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13115{
13116 struct drm_i915_private *dev_priv = dev->dev_private;
13117 enum pipe pipe;
24929352
DV
13118 struct intel_crtc *crtc;
13119 struct intel_encoder *encoder;
13120 struct intel_connector *connector;
5358901f 13121 int i;
24929352 13122
d3fcc808 13123 for_each_intel_crtc(dev, crtc) {
88adfff1 13124 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13125
9953599b
DV
13126 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13127
0e8ffe1b
DV
13128 crtc->active = dev_priv->display.get_pipe_config(crtc,
13129 &crtc->config);
24929352
DV
13130
13131 crtc->base.enabled = crtc->active;
98ec7739 13132 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13133
13134 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13135 crtc->base.base.id,
13136 crtc->active ? "enabled" : "disabled");
13137 }
13138
5358901f
DV
13139 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13140 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13141
13142 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13143 pll->active = 0;
d3fcc808 13144 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13145 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13146 pll->active++;
13147 }
13148 pll->refcount = pll->active;
13149
35c95375
DV
13150 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13151 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13152
13153 if (pll->refcount)
13154 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13155 }
13156
b2784e15 13157 for_each_intel_encoder(dev, encoder) {
24929352
DV
13158 pipe = 0;
13159
13160 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13161 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13162 encoder->base.crtc = &crtc->base;
1d37b689 13163 encoder->get_config(encoder, &crtc->config);
24929352
DV
13164 } else {
13165 encoder->base.crtc = NULL;
13166 }
13167
13168 encoder->connectors_active = false;
6f2bcceb 13169 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13170 encoder->base.base.id,
8e329a03 13171 encoder->base.name,
24929352 13172 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13173 pipe_name(pipe));
24929352
DV
13174 }
13175
13176 list_for_each_entry(connector, &dev->mode_config.connector_list,
13177 base.head) {
13178 if (connector->get_hw_state(connector)) {
13179 connector->base.dpms = DRM_MODE_DPMS_ON;
13180 connector->encoder->connectors_active = true;
13181 connector->base.encoder = &connector->encoder->base;
13182 } else {
13183 connector->base.dpms = DRM_MODE_DPMS_OFF;
13184 connector->base.encoder = NULL;
13185 }
13186 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13187 connector->base.base.id,
c23cc417 13188 connector->base.name,
24929352
DV
13189 connector->base.encoder ? "enabled" : "disabled");
13190 }
30e984df
DV
13191}
13192
13193/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13194 * and i915 state tracking structures. */
13195void intel_modeset_setup_hw_state(struct drm_device *dev,
13196 bool force_restore)
13197{
13198 struct drm_i915_private *dev_priv = dev->dev_private;
13199 enum pipe pipe;
30e984df
DV
13200 struct intel_crtc *crtc;
13201 struct intel_encoder *encoder;
35c95375 13202 int i;
30e984df
DV
13203
13204 intel_modeset_readout_hw_state(dev);
24929352 13205
babea61d
JB
13206 /*
13207 * Now that we have the config, copy it to each CRTC struct
13208 * Note that this could go away if we move to using crtc_config
13209 * checking everywhere.
13210 */
d3fcc808 13211 for_each_intel_crtc(dev, crtc) {
d330a953 13212 if (crtc->active && i915.fastboot) {
f6a83288 13213 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13214 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13215 crtc->base.base.id);
13216 drm_mode_debug_printmodeline(&crtc->base.mode);
13217 }
13218 }
13219
24929352 13220 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13221 for_each_intel_encoder(dev, encoder) {
24929352
DV
13222 intel_sanitize_encoder(encoder);
13223 }
13224
055e393f 13225 for_each_pipe(dev_priv, pipe) {
24929352
DV
13226 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13227 intel_sanitize_crtc(crtc);
c0b03411 13228 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13229 }
9a935856 13230
35c95375
DV
13231 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13232 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13233
13234 if (!pll->on || pll->active)
13235 continue;
13236
13237 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13238
13239 pll->disable(dev_priv, pll);
13240 pll->on = false;
13241 }
13242
96f90c54 13243 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13244 ilk_wm_get_hw_state(dev);
13245
45e2b5f6 13246 if (force_restore) {
7d0bc1ea
VS
13247 i915_redisable_vga(dev);
13248
f30da187
DV
13249 /*
13250 * We need to use raw interfaces for restoring state to avoid
13251 * checking (bogus) intermediate states.
13252 */
055e393f 13253 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13254 struct drm_crtc *crtc =
13255 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13256
13257 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13258 crtc->primary->fb);
45e2b5f6
DV
13259 }
13260 } else {
13261 intel_modeset_update_staged_output_state(dev);
13262 }
8af6cf88
DV
13263
13264 intel_modeset_check_state(dev);
2c7111db
CW
13265}
13266
13267void intel_modeset_gem_init(struct drm_device *dev)
13268{
484b41dd 13269 struct drm_crtc *c;
2ff8fde1 13270 struct drm_i915_gem_object *obj;
484b41dd 13271
ae48434c
ID
13272 mutex_lock(&dev->struct_mutex);
13273 intel_init_gt_powersave(dev);
13274 mutex_unlock(&dev->struct_mutex);
13275
1833b134 13276 intel_modeset_init_hw(dev);
02e792fb
DV
13277
13278 intel_setup_overlay(dev);
484b41dd
JB
13279
13280 /*
13281 * Make sure any fbs we allocated at startup are properly
13282 * pinned & fenced. When we do the allocation it's too early
13283 * for this.
13284 */
13285 mutex_lock(&dev->struct_mutex);
70e1e0ec 13286 for_each_crtc(dev, c) {
2ff8fde1
MR
13287 obj = intel_fb_obj(c->primary->fb);
13288 if (obj == NULL)
484b41dd
JB
13289 continue;
13290
2ff8fde1 13291 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13292 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13293 to_intel_crtc(c)->pipe);
66e514c1
DA
13294 drm_framebuffer_unreference(c->primary->fb);
13295 c->primary->fb = NULL;
484b41dd
JB
13296 }
13297 }
13298 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13299}
13300
4932e2c3
ID
13301void intel_connector_unregister(struct intel_connector *intel_connector)
13302{
13303 struct drm_connector *connector = &intel_connector->base;
13304
13305 intel_panel_destroy_backlight(connector);
34ea3d38 13306 drm_connector_unregister(connector);
4932e2c3
ID
13307}
13308
79e53945
JB
13309void intel_modeset_cleanup(struct drm_device *dev)
13310{
652c393a 13311 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13312 struct drm_connector *connector;
652c393a 13313
fd0c0642
DV
13314 /*
13315 * Interrupts and polling as the first thing to avoid creating havoc.
13316 * Too much stuff here (turning of rps, connectors, ...) would
13317 * experience fancy races otherwise.
13318 */
13319 drm_irq_uninstall(dev);
1d0d343a 13320 intel_hpd_cancel_work(dev_priv);
eb21b92b
JB
13321 dev_priv->pm._irqs_disabled = true;
13322
fd0c0642
DV
13323 /*
13324 * Due to the hpd irq storm handling the hotplug work can re-arm the
13325 * poll handlers. Hence disable polling after hpd handling is shut down.
13326 */
f87ea761 13327 drm_kms_helper_poll_fini(dev);
fd0c0642 13328
652c393a
JB
13329 mutex_lock(&dev->struct_mutex);
13330
723bfd70
JB
13331 intel_unregister_dsm_handler();
13332
973d04f9 13333 intel_disable_fbc(dev);
e70236a8 13334
8090c6b9 13335 intel_disable_gt_powersave(dev);
0cdab21f 13336
930ebb46
DV
13337 ironlake_teardown_rc6(dev);
13338
69341a5e
KH
13339 mutex_unlock(&dev->struct_mutex);
13340
1630fe75
CW
13341 /* flush any delayed tasks or pending work */
13342 flush_scheduled_work();
13343
db31af1d
JN
13344 /* destroy the backlight and sysfs files before encoders/connectors */
13345 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13346 struct intel_connector *intel_connector;
13347
13348 intel_connector = to_intel_connector(connector);
13349 intel_connector->unregister(intel_connector);
db31af1d 13350 }
d9255d57 13351
79e53945 13352 drm_mode_config_cleanup(dev);
4d7bb011
DV
13353
13354 intel_cleanup_overlay(dev);
ae48434c
ID
13355
13356 mutex_lock(&dev->struct_mutex);
13357 intel_cleanup_gt_powersave(dev);
13358 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13359}
13360
f1c79df3
ZW
13361/*
13362 * Return which encoder is currently attached for connector.
13363 */
df0e9248 13364struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13365{
df0e9248
CW
13366 return &intel_attached_encoder(connector)->base;
13367}
f1c79df3 13368
df0e9248
CW
13369void intel_connector_attach_encoder(struct intel_connector *connector,
13370 struct intel_encoder *encoder)
13371{
13372 connector->encoder = encoder;
13373 drm_mode_connector_attach_encoder(&connector->base,
13374 &encoder->base);
79e53945 13375}
28d52043
DA
13376
13377/*
13378 * set vga decode state - true == enable VGA decode
13379 */
13380int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13381{
13382 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13383 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13384 u16 gmch_ctrl;
13385
75fa041d
CW
13386 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13387 DRM_ERROR("failed to read control word\n");
13388 return -EIO;
13389 }
13390
c0cc8a55
CW
13391 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13392 return 0;
13393
28d52043
DA
13394 if (state)
13395 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13396 else
13397 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13398
13399 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13400 DRM_ERROR("failed to write control word\n");
13401 return -EIO;
13402 }
13403
28d52043
DA
13404 return 0;
13405}
c4a1d9e4 13406
c4a1d9e4 13407struct intel_display_error_state {
ff57f1b0
PZ
13408
13409 u32 power_well_driver;
13410
63b66e5b
CW
13411 int num_transcoders;
13412
c4a1d9e4
CW
13413 struct intel_cursor_error_state {
13414 u32 control;
13415 u32 position;
13416 u32 base;
13417 u32 size;
52331309 13418 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13419
13420 struct intel_pipe_error_state {
ddf9c536 13421 bool power_domain_on;
c4a1d9e4 13422 u32 source;
f301b1e1 13423 u32 stat;
52331309 13424 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13425
13426 struct intel_plane_error_state {
13427 u32 control;
13428 u32 stride;
13429 u32 size;
13430 u32 pos;
13431 u32 addr;
13432 u32 surface;
13433 u32 tile_offset;
52331309 13434 } plane[I915_MAX_PIPES];
63b66e5b
CW
13435
13436 struct intel_transcoder_error_state {
ddf9c536 13437 bool power_domain_on;
63b66e5b
CW
13438 enum transcoder cpu_transcoder;
13439
13440 u32 conf;
13441
13442 u32 htotal;
13443 u32 hblank;
13444 u32 hsync;
13445 u32 vtotal;
13446 u32 vblank;
13447 u32 vsync;
13448 } transcoder[4];
c4a1d9e4
CW
13449};
13450
13451struct intel_display_error_state *
13452intel_display_capture_error_state(struct drm_device *dev)
13453{
fbee40df 13454 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13455 struct intel_display_error_state *error;
63b66e5b
CW
13456 int transcoders[] = {
13457 TRANSCODER_A,
13458 TRANSCODER_B,
13459 TRANSCODER_C,
13460 TRANSCODER_EDP,
13461 };
c4a1d9e4
CW
13462 int i;
13463
63b66e5b
CW
13464 if (INTEL_INFO(dev)->num_pipes == 0)
13465 return NULL;
13466
9d1cb914 13467 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13468 if (error == NULL)
13469 return NULL;
13470
190be112 13471 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13472 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13473
055e393f 13474 for_each_pipe(dev_priv, i) {
ddf9c536 13475 error->pipe[i].power_domain_on =
bfafe93a
ID
13476 intel_display_power_enabled_unlocked(dev_priv,
13477 POWER_DOMAIN_PIPE(i));
ddf9c536 13478 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13479 continue;
13480
5efb3e28
VS
13481 error->cursor[i].control = I915_READ(CURCNTR(i));
13482 error->cursor[i].position = I915_READ(CURPOS(i));
13483 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13484
13485 error->plane[i].control = I915_READ(DSPCNTR(i));
13486 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13487 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13488 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13489 error->plane[i].pos = I915_READ(DSPPOS(i));
13490 }
ca291363
PZ
13491 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13492 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13493 if (INTEL_INFO(dev)->gen >= 4) {
13494 error->plane[i].surface = I915_READ(DSPSURF(i));
13495 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13496 }
13497
c4a1d9e4 13498 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13499
3abfce77 13500 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13501 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13502 }
13503
13504 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13505 if (HAS_DDI(dev_priv->dev))
13506 error->num_transcoders++; /* Account for eDP. */
13507
13508 for (i = 0; i < error->num_transcoders; i++) {
13509 enum transcoder cpu_transcoder = transcoders[i];
13510
ddf9c536 13511 error->transcoder[i].power_domain_on =
bfafe93a 13512 intel_display_power_enabled_unlocked(dev_priv,
38cc1daf 13513 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13514 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13515 continue;
13516
63b66e5b
CW
13517 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13518
13519 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13520 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13521 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13522 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13523 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13524 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13525 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13526 }
13527
13528 return error;
13529}
13530
edc3d884
MK
13531#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13532
c4a1d9e4 13533void
edc3d884 13534intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13535 struct drm_device *dev,
13536 struct intel_display_error_state *error)
13537{
055e393f 13538 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13539 int i;
13540
63b66e5b
CW
13541 if (!error)
13542 return;
13543
edc3d884 13544 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13545 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13546 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13547 error->power_well_driver);
055e393f 13548 for_each_pipe(dev_priv, i) {
edc3d884 13549 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13550 err_printf(m, " Power: %s\n",
13551 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13552 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13553 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13554
13555 err_printf(m, "Plane [%d]:\n", i);
13556 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13557 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13558 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13559 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13560 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13561 }
4b71a570 13562 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13563 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13564 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13565 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13566 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13567 }
13568
edc3d884
MK
13569 err_printf(m, "Cursor [%d]:\n", i);
13570 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13571 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13572 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13573 }
63b66e5b
CW
13574
13575 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13576 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13577 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13578 err_printf(m, " Power: %s\n",
13579 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13580 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13581 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13582 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13583 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13584 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13585 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13586 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13587 }
c4a1d9e4 13588}
e2fcdaa9
VS
13589
13590void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13591{
13592 struct intel_crtc *crtc;
13593
13594 for_each_intel_crtc(dev, crtc) {
13595 struct intel_unpin_work *work;
13596 unsigned long irqflags;
13597
13598 spin_lock_irqsave(&dev->event_lock, irqflags);
13599
13600 work = crtc->unpin_work;
13601
13602 if (work && work->event &&
13603 work->event->base.file_priv == file) {
13604 kfree(work->event);
13605 work->event = NULL;
13606 }
13607
13608 spin_unlock_irqrestore(&dev->event_lock, irqflags);
13609 }
13610}