drm/i915: Clear TX FIFO reset master override bits on chv
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
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
6b383a7f 79static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 80
f1f644dc
JB
81static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
82 struct intel_crtc_config *pipe_config);
18442d08
VS
83static void ironlake_pch_clock_get(struct intel_crtc *crtc,
84 struct intel_crtc_config *pipe_config);
f1f644dc 85
e7457a9a
DL
86static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
87 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
88static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
5b18e57c
DV
92static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 94static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
95 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
29407aab 97static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
98static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 100static void vlv_prepare_pll(struct intel_crtc *crtc);
1ae0d137 101static void chv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 102
0e32b39c
DA
103static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
104{
105 if (!connector->mst_port)
106 return connector->encoder;
107 else
108 return &connector->mst_port->mst_encoders[pipe]->base;
109}
110
79e53945 111typedef struct {
0206e353 112 int min, max;
79e53945
JB
113} intel_range_t;
114
115typedef struct {
0206e353
AJ
116 int dot_limit;
117 int p2_slow, p2_fast;
79e53945
JB
118} intel_p2_t;
119
d4906093
ML
120typedef struct intel_limit intel_limit_t;
121struct intel_limit {
0206e353
AJ
122 intel_range_t dot, vco, n, m, m1, m2, p, p1;
123 intel_p2_t p2;
d4906093 124};
79e53945 125
d2acd215
DV
126int
127intel_pch_rawclk(struct drm_device *dev)
128{
129 struct drm_i915_private *dev_priv = dev->dev_private;
130
131 WARN_ON(!HAS_PCH_SPLIT(dev));
132
133 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
134}
135
021357ac
CW
136static inline u32 /* units of 100MHz */
137intel_fdi_link_freq(struct drm_device *dev)
138{
8b99e68c
CW
139 if (IS_GEN5(dev)) {
140 struct drm_i915_private *dev_priv = dev->dev_private;
141 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
142 } else
143 return 27;
021357ac
CW
144}
145
5d536e28 146static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 147 .dot = { .min = 25000, .max = 350000 },
9c333719 148 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 149 .n = { .min = 2, .max = 16 },
0206e353
AJ
150 .m = { .min = 96, .max = 140 },
151 .m1 = { .min = 18, .max = 26 },
152 .m2 = { .min = 6, .max = 16 },
153 .p = { .min = 4, .max = 128 },
154 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
155 .p2 = { .dot_limit = 165000,
156 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
157};
158
5d536e28
DV
159static const intel_limit_t intel_limits_i8xx_dvo = {
160 .dot = { .min = 25000, .max = 350000 },
9c333719 161 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 162 .n = { .min = 2, .max = 16 },
5d536e28
DV
163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 2, .max = 33 },
168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 4, .p2_fast = 4 },
170};
171
e4b36699 172static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 173 .dot = { .min = 25000, .max = 350000 },
9c333719 174 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 175 .n = { .min = 2, .max = 16 },
0206e353
AJ
176 .m = { .min = 96, .max = 140 },
177 .m1 = { .min = 18, .max = 26 },
178 .m2 = { .min = 6, .max = 16 },
179 .p = { .min = 4, .max = 128 },
180 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 14, .p2_fast = 7 },
e4b36699 183};
273e27ca 184
e4b36699 185static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
186 .dot = { .min = 20000, .max = 400000 },
187 .vco = { .min = 1400000, .max = 2800000 },
188 .n = { .min = 1, .max = 6 },
189 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
190 .m1 = { .min = 8, .max = 18 },
191 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
192 .p = { .min = 5, .max = 80 },
193 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
194 .p2 = { .dot_limit = 200000,
195 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
196};
197
198static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
199 .dot = { .min = 20000, .max = 400000 },
200 .vco = { .min = 1400000, .max = 2800000 },
201 .n = { .min = 1, .max = 6 },
202 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
203 .m1 = { .min = 8, .max = 18 },
204 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
205 .p = { .min = 7, .max = 98 },
206 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
207 .p2 = { .dot_limit = 112000,
208 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
209};
210
273e27ca 211
e4b36699 212static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
213 .dot = { .min = 25000, .max = 270000 },
214 .vco = { .min = 1750000, .max = 3500000},
215 .n = { .min = 1, .max = 4 },
216 .m = { .min = 104, .max = 138 },
217 .m1 = { .min = 17, .max = 23 },
218 .m2 = { .min = 5, .max = 11 },
219 .p = { .min = 10, .max = 30 },
220 .p1 = { .min = 1, .max = 3},
221 .p2 = { .dot_limit = 270000,
222 .p2_slow = 10,
223 .p2_fast = 10
044c7c41 224 },
e4b36699
KP
225};
226
227static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
228 .dot = { .min = 22000, .max = 400000 },
229 .vco = { .min = 1750000, .max = 3500000},
230 .n = { .min = 1, .max = 4 },
231 .m = { .min = 104, .max = 138 },
232 .m1 = { .min = 16, .max = 23 },
233 .m2 = { .min = 5, .max = 11 },
234 .p = { .min = 5, .max = 80 },
235 .p1 = { .min = 1, .max = 8},
236 .p2 = { .dot_limit = 165000,
237 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
238};
239
240static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
241 .dot = { .min = 20000, .max = 115000 },
242 .vco = { .min = 1750000, .max = 3500000 },
243 .n = { .min = 1, .max = 3 },
244 .m = { .min = 104, .max = 138 },
245 .m1 = { .min = 17, .max = 23 },
246 .m2 = { .min = 5, .max = 11 },
247 .p = { .min = 28, .max = 112 },
248 .p1 = { .min = 2, .max = 8 },
249 .p2 = { .dot_limit = 0,
250 .p2_slow = 14, .p2_fast = 14
044c7c41 251 },
e4b36699
KP
252};
253
254static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
255 .dot = { .min = 80000, .max = 224000 },
256 .vco = { .min = 1750000, .max = 3500000 },
257 .n = { .min = 1, .max = 3 },
258 .m = { .min = 104, .max = 138 },
259 .m1 = { .min = 17, .max = 23 },
260 .m2 = { .min = 5, .max = 11 },
261 .p = { .min = 14, .max = 42 },
262 .p1 = { .min = 2, .max = 6 },
263 .p2 = { .dot_limit = 0,
264 .p2_slow = 7, .p2_fast = 7
044c7c41 265 },
e4b36699
KP
266};
267
f2b115e6 268static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
269 .dot = { .min = 20000, .max = 400000},
270 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 271 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
272 .n = { .min = 3, .max = 6 },
273 .m = { .min = 2, .max = 256 },
273e27ca 274 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
275 .m1 = { .min = 0, .max = 0 },
276 .m2 = { .min = 0, .max = 254 },
277 .p = { .min = 5, .max = 80 },
278 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
279 .p2 = { .dot_limit = 200000,
280 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
281};
282
f2b115e6 283static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
284 .dot = { .min = 20000, .max = 400000 },
285 .vco = { .min = 1700000, .max = 3500000 },
286 .n = { .min = 3, .max = 6 },
287 .m = { .min = 2, .max = 256 },
288 .m1 = { .min = 0, .max = 0 },
289 .m2 = { .min = 0, .max = 254 },
290 .p = { .min = 7, .max = 112 },
291 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
292 .p2 = { .dot_limit = 112000,
293 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
294};
295
273e27ca
EA
296/* Ironlake / Sandybridge
297 *
298 * We calculate clock using (register_value + 2) for N/M1/M2, so here
299 * the range value for them is (actual_value - 2).
300 */
b91ad0ec 301static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 5 },
305 .m = { .min = 79, .max = 127 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
312};
313
b91ad0ec 314static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 3 },
318 .m = { .min = 79, .max = 118 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 28, .max = 112 },
322 .p1 = { .min = 2, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
325};
326
327static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
328 .dot = { .min = 25000, .max = 350000 },
329 .vco = { .min = 1760000, .max = 3510000 },
330 .n = { .min = 1, .max = 3 },
331 .m = { .min = 79, .max = 127 },
332 .m1 = { .min = 12, .max = 22 },
333 .m2 = { .min = 5, .max = 9 },
334 .p = { .min = 14, .max = 56 },
335 .p1 = { .min = 2, .max = 8 },
336 .p2 = { .dot_limit = 225000,
337 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
338};
339
273e27ca 340/* LVDS 100mhz refclk limits. */
b91ad0ec 341static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
342 .dot = { .min = 25000, .max = 350000 },
343 .vco = { .min = 1760000, .max = 3510000 },
344 .n = { .min = 1, .max = 2 },
345 .m = { .min = 79, .max = 126 },
346 .m1 = { .min = 12, .max = 22 },
347 .m2 = { .min = 5, .max = 9 },
348 .p = { .min = 28, .max = 112 },
0206e353 349 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
350 .p2 = { .dot_limit = 225000,
351 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
352};
353
354static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
355 .dot = { .min = 25000, .max = 350000 },
356 .vco = { .min = 1760000, .max = 3510000 },
357 .n = { .min = 1, .max = 3 },
358 .m = { .min = 79, .max = 126 },
359 .m1 = { .min = 12, .max = 22 },
360 .m2 = { .min = 5, .max = 9 },
361 .p = { .min = 14, .max = 42 },
0206e353 362 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
363 .p2 = { .dot_limit = 225000,
364 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
365};
366
dc730512 367static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
368 /*
369 * These are the data rate limits (measured in fast clocks)
370 * since those are the strictest limits we have. The fast
371 * clock and actual rate limits are more relaxed, so checking
372 * them would make no difference.
373 */
374 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 375 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 376 .n = { .min = 1, .max = 7 },
a0c4da24
JB
377 .m1 = { .min = 2, .max = 3 },
378 .m2 = { .min = 11, .max = 156 },
b99ab663 379 .p1 = { .min = 2, .max = 3 },
5fdc9c49 380 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
381};
382
ef9348c8
CML
383static const intel_limit_t intel_limits_chv = {
384 /*
385 * These are the data rate limits (measured in fast clocks)
386 * since those are the strictest limits we have. The fast
387 * clock and actual rate limits are more relaxed, so checking
388 * them would make no difference.
389 */
390 .dot = { .min = 25000 * 5, .max = 540000 * 5},
391 .vco = { .min = 4860000, .max = 6700000 },
392 .n = { .min = 1, .max = 1 },
393 .m1 = { .min = 2, .max = 2 },
394 .m2 = { .min = 24 << 22, .max = 175 << 22 },
395 .p1 = { .min = 2, .max = 4 },
396 .p2 = { .p2_slow = 1, .p2_fast = 14 },
397};
398
6b4bf1c4
VS
399static void vlv_clock(int refclk, intel_clock_t *clock)
400{
401 clock->m = clock->m1 * clock->m2;
402 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
403 if (WARN_ON(clock->n == 0 || clock->p == 0))
404 return;
fb03ac01
VS
405 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
406 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
407}
408
e0638cdf
PZ
409/**
410 * Returns whether any output on the specified pipe is of the specified type
411 */
412static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
413{
414 struct drm_device *dev = crtc->dev;
415 struct intel_encoder *encoder;
416
417 for_each_encoder_on_crtc(dev, crtc, encoder)
418 if (encoder->type == type)
419 return true;
420
421 return false;
422}
423
1b894b59
CW
424static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
425 int refclk)
2c07245f 426{
b91ad0ec 427 struct drm_device *dev = crtc->dev;
2c07245f 428 const intel_limit_t *limit;
b91ad0ec
ZW
429
430 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 431 if (intel_is_dual_link_lvds(dev)) {
1b894b59 432 if (refclk == 100000)
b91ad0ec
ZW
433 limit = &intel_limits_ironlake_dual_lvds_100m;
434 else
435 limit = &intel_limits_ironlake_dual_lvds;
436 } else {
1b894b59 437 if (refclk == 100000)
b91ad0ec
ZW
438 limit = &intel_limits_ironlake_single_lvds_100m;
439 else
440 limit = &intel_limits_ironlake_single_lvds;
441 }
c6bb3538 442 } else
b91ad0ec 443 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
444
445 return limit;
446}
447
044c7c41
ML
448static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
449{
450 struct drm_device *dev = crtc->dev;
044c7c41
ML
451 const intel_limit_t *limit;
452
453 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 454 if (intel_is_dual_link_lvds(dev))
e4b36699 455 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 456 else
e4b36699 457 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
458 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
459 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 460 limit = &intel_limits_g4x_hdmi;
044c7c41 461 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 462 limit = &intel_limits_g4x_sdvo;
044c7c41 463 } else /* The option is for other outputs */
e4b36699 464 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
465
466 return limit;
467}
468
1b894b59 469static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
470{
471 struct drm_device *dev = crtc->dev;
472 const intel_limit_t *limit;
473
bad720ff 474 if (HAS_PCH_SPLIT(dev))
1b894b59 475 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 476 else if (IS_G4X(dev)) {
044c7c41 477 limit = intel_g4x_limit(crtc);
f2b115e6 478 } else if (IS_PINEVIEW(dev)) {
2177832f 479 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 480 limit = &intel_limits_pineview_lvds;
2177832f 481 else
f2b115e6 482 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
483 } else if (IS_CHERRYVIEW(dev)) {
484 limit = &intel_limits_chv;
a0c4da24 485 } else if (IS_VALLEYVIEW(dev)) {
dc730512 486 limit = &intel_limits_vlv;
a6c45cf0
CW
487 } else if (!IS_GEN2(dev)) {
488 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
489 limit = &intel_limits_i9xx_lvds;
490 else
491 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
492 } else {
493 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 494 limit = &intel_limits_i8xx_lvds;
5d536e28 495 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 496 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
497 else
498 limit = &intel_limits_i8xx_dac;
79e53945
JB
499 }
500 return limit;
501}
502
f2b115e6
AJ
503/* m1 is reserved as 0 in Pineview, n is a ring counter */
504static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 505{
2177832f
SL
506 clock->m = clock->m2 + 2;
507 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
508 if (WARN_ON(clock->n == 0 || clock->p == 0))
509 return;
fb03ac01
VS
510 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
511 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
512}
513
7429e9d4
DV
514static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
515{
516 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
517}
518
ac58c3f0 519static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 520{
7429e9d4 521 clock->m = i9xx_dpll_compute_m(clock);
79e53945 522 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
523 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
524 return;
fb03ac01
VS
525 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
526 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
527}
528
ef9348c8
CML
529static void chv_clock(int refclk, intel_clock_t *clock)
530{
531 clock->m = clock->m1 * clock->m2;
532 clock->p = clock->p1 * clock->p2;
533 if (WARN_ON(clock->n == 0 || clock->p == 0))
534 return;
535 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
536 clock->n << 22);
537 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
538}
539
7c04d1d9 540#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
541/**
542 * Returns whether the given set of divisors are valid for a given refclk with
543 * the given connectors.
544 */
545
1b894b59
CW
546static bool intel_PLL_is_valid(struct drm_device *dev,
547 const intel_limit_t *limit,
548 const intel_clock_t *clock)
79e53945 549{
f01b7962
VS
550 if (clock->n < limit->n.min || limit->n.max < clock->n)
551 INTELPllInvalid("n out of range\n");
79e53945 552 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 553 INTELPllInvalid("p1 out of range\n");
79e53945 554 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 555 INTELPllInvalid("m2 out of range\n");
79e53945 556 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 557 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
558
559 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
560 if (clock->m1 <= clock->m2)
561 INTELPllInvalid("m1 <= m2\n");
562
563 if (!IS_VALLEYVIEW(dev)) {
564 if (clock->p < limit->p.min || limit->p.max < clock->p)
565 INTELPllInvalid("p out of range\n");
566 if (clock->m < limit->m.min || limit->m.max < clock->m)
567 INTELPllInvalid("m out of range\n");
568 }
569
79e53945 570 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 571 INTELPllInvalid("vco out of range\n");
79e53945
JB
572 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
573 * connector, etc., rather than just a single range.
574 */
575 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 576 INTELPllInvalid("dot out of range\n");
79e53945
JB
577
578 return true;
579}
580
d4906093 581static bool
ee9300bb 582i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
583 int target, int refclk, intel_clock_t *match_clock,
584 intel_clock_t *best_clock)
79e53945
JB
585{
586 struct drm_device *dev = crtc->dev;
79e53945 587 intel_clock_t clock;
79e53945
JB
588 int err = target;
589
a210b028 590 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 591 /*
a210b028
DV
592 * For LVDS just rely on its current settings for dual-channel.
593 * We haven't figured out how to reliably set up different
594 * single/dual channel state, if we even can.
79e53945 595 */
1974cad0 596 if (intel_is_dual_link_lvds(dev))
79e53945
JB
597 clock.p2 = limit->p2.p2_fast;
598 else
599 clock.p2 = limit->p2.p2_slow;
600 } else {
601 if (target < limit->p2.dot_limit)
602 clock.p2 = limit->p2.p2_slow;
603 else
604 clock.p2 = limit->p2.p2_fast;
605 }
606
0206e353 607 memset(best_clock, 0, sizeof(*best_clock));
79e53945 608
42158660
ZY
609 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
610 clock.m1++) {
611 for (clock.m2 = limit->m2.min;
612 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 613 if (clock.m2 >= clock.m1)
42158660
ZY
614 break;
615 for (clock.n = limit->n.min;
616 clock.n <= limit->n.max; clock.n++) {
617 for (clock.p1 = limit->p1.min;
618 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
619 int this_err;
620
ac58c3f0
DV
621 i9xx_clock(refclk, &clock);
622 if (!intel_PLL_is_valid(dev, limit,
623 &clock))
624 continue;
625 if (match_clock &&
626 clock.p != match_clock->p)
627 continue;
628
629 this_err = abs(clock.dot - target);
630 if (this_err < err) {
631 *best_clock = clock;
632 err = this_err;
633 }
634 }
635 }
636 }
637 }
638
639 return (err != target);
640}
641
642static bool
ee9300bb
DV
643pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
644 int target, int refclk, intel_clock_t *match_clock,
645 intel_clock_t *best_clock)
79e53945
JB
646{
647 struct drm_device *dev = crtc->dev;
79e53945 648 intel_clock_t clock;
79e53945
JB
649 int err = target;
650
a210b028 651 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 652 /*
a210b028
DV
653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
79e53945 656 */
1974cad0 657 if (intel_is_dual_link_lvds(dev))
79e53945
JB
658 clock.p2 = limit->p2.p2_fast;
659 else
660 clock.p2 = limit->p2.p2_slow;
661 } else {
662 if (target < limit->p2.dot_limit)
663 clock.p2 = limit->p2.p2_slow;
664 else
665 clock.p2 = limit->p2.p2_fast;
666 }
667
0206e353 668 memset(best_clock, 0, sizeof(*best_clock));
79e53945 669
42158660
ZY
670 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
671 clock.m1++) {
672 for (clock.m2 = limit->m2.min;
673 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
674 for (clock.n = limit->n.min;
675 clock.n <= limit->n.max; clock.n++) {
676 for (clock.p1 = limit->p1.min;
677 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
678 int this_err;
679
ac58c3f0 680 pineview_clock(refclk, &clock);
1b894b59
CW
681 if (!intel_PLL_is_valid(dev, limit,
682 &clock))
79e53945 683 continue;
cec2f356
SP
684 if (match_clock &&
685 clock.p != match_clock->p)
686 continue;
79e53945
JB
687
688 this_err = abs(clock.dot - target);
689 if (this_err < err) {
690 *best_clock = clock;
691 err = this_err;
692 }
693 }
694 }
695 }
696 }
697
698 return (err != target);
699}
700
d4906093 701static bool
ee9300bb
DV
702g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
703 int target, int refclk, intel_clock_t *match_clock,
704 intel_clock_t *best_clock)
d4906093
ML
705{
706 struct drm_device *dev = crtc->dev;
d4906093
ML
707 intel_clock_t clock;
708 int max_n;
709 bool found;
6ba770dc
AJ
710 /* approximately equals target * 0.00585 */
711 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
712 found = false;
713
714 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 715 if (intel_is_dual_link_lvds(dev))
d4906093
ML
716 clock.p2 = limit->p2.p2_fast;
717 else
718 clock.p2 = limit->p2.p2_slow;
719 } else {
720 if (target < limit->p2.dot_limit)
721 clock.p2 = limit->p2.p2_slow;
722 else
723 clock.p2 = limit->p2.p2_fast;
724 }
725
726 memset(best_clock, 0, sizeof(*best_clock));
727 max_n = limit->n.max;
f77f13e2 728 /* based on hardware requirement, prefer smaller n to precision */
d4906093 729 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 730 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
731 for (clock.m1 = limit->m1.max;
732 clock.m1 >= limit->m1.min; clock.m1--) {
733 for (clock.m2 = limit->m2.max;
734 clock.m2 >= limit->m2.min; clock.m2--) {
735 for (clock.p1 = limit->p1.max;
736 clock.p1 >= limit->p1.min; clock.p1--) {
737 int this_err;
738
ac58c3f0 739 i9xx_clock(refclk, &clock);
1b894b59
CW
740 if (!intel_PLL_is_valid(dev, limit,
741 &clock))
d4906093 742 continue;
1b894b59
CW
743
744 this_err = abs(clock.dot - target);
d4906093
ML
745 if (this_err < err_most) {
746 *best_clock = clock;
747 err_most = this_err;
748 max_n = clock.n;
749 found = true;
750 }
751 }
752 }
753 }
754 }
2c07245f
ZW
755 return found;
756}
757
a0c4da24 758static bool
ee9300bb
DV
759vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
760 int target, int refclk, intel_clock_t *match_clock,
761 intel_clock_t *best_clock)
a0c4da24 762{
f01b7962 763 struct drm_device *dev = crtc->dev;
6b4bf1c4 764 intel_clock_t clock;
69e4f900 765 unsigned int bestppm = 1000000;
27e639bf
VS
766 /* min update 19.2 MHz */
767 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 768 bool found = false;
a0c4da24 769
6b4bf1c4
VS
770 target *= 5; /* fast clock */
771
772 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
773
774 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 775 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 776 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 777 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 778 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 779 clock.p = clock.p1 * clock.p2;
a0c4da24 780 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 781 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
782 unsigned int ppm, diff;
783
6b4bf1c4
VS
784 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
785 refclk * clock.m1);
786
787 vlv_clock(refclk, &clock);
43b0ac53 788
f01b7962
VS
789 if (!intel_PLL_is_valid(dev, limit,
790 &clock))
43b0ac53
VS
791 continue;
792
6b4bf1c4
VS
793 diff = abs(clock.dot - target);
794 ppm = div_u64(1000000ULL * diff, target);
795
796 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 797 bestppm = 0;
6b4bf1c4 798 *best_clock = clock;
49e497ef 799 found = true;
43b0ac53 800 }
6b4bf1c4 801
c686122c 802 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 803 bestppm = ppm;
6b4bf1c4 804 *best_clock = clock;
49e497ef 805 found = true;
a0c4da24
JB
806 }
807 }
808 }
809 }
810 }
a0c4da24 811
49e497ef 812 return found;
a0c4da24 813}
a4fc5ed6 814
ef9348c8
CML
815static bool
816chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
817 int target, int refclk, intel_clock_t *match_clock,
818 intel_clock_t *best_clock)
819{
820 struct drm_device *dev = crtc->dev;
821 intel_clock_t clock;
822 uint64_t m2;
823 int found = false;
824
825 memset(best_clock, 0, sizeof(*best_clock));
826
827 /*
828 * Based on hardware doc, the n always set to 1, and m1 always
829 * set to 2. If requires to support 200Mhz refclk, we need to
830 * revisit this because n may not 1 anymore.
831 */
832 clock.n = 1, clock.m1 = 2;
833 target *= 5; /* fast clock */
834
835 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
836 for (clock.p2 = limit->p2.p2_fast;
837 clock.p2 >= limit->p2.p2_slow;
838 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
839
840 clock.p = clock.p1 * clock.p2;
841
842 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
843 clock.n) << 22, refclk * clock.m1);
844
845 if (m2 > INT_MAX/clock.m1)
846 continue;
847
848 clock.m2 = m2;
849
850 chv_clock(refclk, &clock);
851
852 if (!intel_PLL_is_valid(dev, limit, &clock))
853 continue;
854
855 /* based on hardware requirement, prefer bigger p
856 */
857 if (clock.p > best_clock->p) {
858 *best_clock = clock;
859 found = true;
860 }
861 }
862 }
863
864 return found;
865}
866
20ddf665
VS
867bool intel_crtc_active(struct drm_crtc *crtc)
868{
869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
870
871 /* Be paranoid as we can arrive here with only partial
872 * state retrieved from the hardware during setup.
873 *
241bfc38 874 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
875 * as Haswell has gained clock readout/fastboot support.
876 *
66e514c1 877 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
878 * properly reconstruct framebuffers.
879 */
f4510a27 880 return intel_crtc->active && crtc->primary->fb &&
241bfc38 881 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
882}
883
a5c961d1
PZ
884enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
885 enum pipe pipe)
886{
887 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
889
3b117c8f 890 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
891}
892
fbf49ea2
VS
893static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
894{
895 struct drm_i915_private *dev_priv = dev->dev_private;
896 u32 reg = PIPEDSL(pipe);
897 u32 line1, line2;
898 u32 line_mask;
899
900 if (IS_GEN2(dev))
901 line_mask = DSL_LINEMASK_GEN2;
902 else
903 line_mask = DSL_LINEMASK_GEN3;
904
905 line1 = I915_READ(reg) & line_mask;
906 mdelay(5);
907 line2 = I915_READ(reg) & line_mask;
908
909 return line1 == line2;
910}
911
ab7ad7f6
KP
912/*
913 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 914 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
915 *
916 * After disabling a pipe, we can't wait for vblank in the usual way,
917 * spinning on the vblank interrupt status bit, since we won't actually
918 * see an interrupt when the pipe is disabled.
919 *
ab7ad7f6
KP
920 * On Gen4 and above:
921 * wait for the pipe register state bit to turn off
922 *
923 * Otherwise:
924 * wait for the display line value to settle (it usually
925 * ends up stopping at the start of the next frame).
58e10eb9 926 *
9d0498a2 927 */
575f7ab7 928static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 929{
575f7ab7 930 struct drm_device *dev = crtc->base.dev;
9d0498a2 931 struct drm_i915_private *dev_priv = dev->dev_private;
575f7ab7
VS
932 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
933 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
934
935 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 936 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
937
938 /* Wait for the Pipe State to go off */
58e10eb9
CW
939 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
940 100))
284637d9 941 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 942 } else {
ab7ad7f6 943 /* Wait for the display line to settle */
fbf49ea2 944 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 945 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 946 }
79e53945
JB
947}
948
b0ea7d37
DL
949/*
950 * ibx_digital_port_connected - is the specified port connected?
951 * @dev_priv: i915 private structure
952 * @port: the port to test
953 *
954 * Returns true if @port is connected, false otherwise.
955 */
956bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
957 struct intel_digital_port *port)
958{
959 u32 bit;
960
c36346e3 961 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 962 switch (port->port) {
c36346e3
DL
963 case PORT_B:
964 bit = SDE_PORTB_HOTPLUG;
965 break;
966 case PORT_C:
967 bit = SDE_PORTC_HOTPLUG;
968 break;
969 case PORT_D:
970 bit = SDE_PORTD_HOTPLUG;
971 break;
972 default:
973 return true;
974 }
975 } else {
eba905b2 976 switch (port->port) {
c36346e3
DL
977 case PORT_B:
978 bit = SDE_PORTB_HOTPLUG_CPT;
979 break;
980 case PORT_C:
981 bit = SDE_PORTC_HOTPLUG_CPT;
982 break;
983 case PORT_D:
984 bit = SDE_PORTD_HOTPLUG_CPT;
985 break;
986 default:
987 return true;
988 }
b0ea7d37
DL
989 }
990
991 return I915_READ(SDEISR) & bit;
992}
993
b24e7179
JB
994static const char *state_string(bool enabled)
995{
996 return enabled ? "on" : "off";
997}
998
999/* Only for pre-ILK configs */
55607e8a
DV
1000void assert_pll(struct drm_i915_private *dev_priv,
1001 enum pipe pipe, bool state)
b24e7179
JB
1002{
1003 int reg;
1004 u32 val;
1005 bool cur_state;
1006
1007 reg = DPLL(pipe);
1008 val = I915_READ(reg);
1009 cur_state = !!(val & DPLL_VCO_ENABLE);
1010 WARN(cur_state != state,
1011 "PLL state assertion failure (expected %s, current %s)\n",
1012 state_string(state), state_string(cur_state));
1013}
b24e7179 1014
23538ef1
JN
1015/* XXX: the dsi pll is shared between MIPI DSI ports */
1016static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1017{
1018 u32 val;
1019 bool cur_state;
1020
1021 mutex_lock(&dev_priv->dpio_lock);
1022 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1023 mutex_unlock(&dev_priv->dpio_lock);
1024
1025 cur_state = val & DSI_PLL_VCO_EN;
1026 WARN(cur_state != state,
1027 "DSI PLL state assertion failure (expected %s, current %s)\n",
1028 state_string(state), state_string(cur_state));
1029}
1030#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1031#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1032
55607e8a 1033struct intel_shared_dpll *
e2b78267
DV
1034intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1035{
1036 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1037
a43f6e0f 1038 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1039 return NULL;
1040
a43f6e0f 1041 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1042}
1043
040484af 1044/* For ILK+ */
55607e8a
DV
1045void assert_shared_dpll(struct drm_i915_private *dev_priv,
1046 struct intel_shared_dpll *pll,
1047 bool state)
040484af 1048{
040484af 1049 bool cur_state;
5358901f 1050 struct intel_dpll_hw_state hw_state;
040484af 1051
92b27b08 1052 if (WARN (!pll,
46edb027 1053 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1054 return;
ee7b9f93 1055
5358901f 1056 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1057 WARN(cur_state != state,
5358901f
DV
1058 "%s assertion failure (expected %s, current %s)\n",
1059 pll->name, state_string(state), state_string(cur_state));
040484af 1060}
040484af
JB
1061
1062static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1063 enum pipe pipe, bool state)
1064{
1065 int reg;
1066 u32 val;
1067 bool cur_state;
ad80a810
PZ
1068 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1069 pipe);
040484af 1070
affa9354
PZ
1071 if (HAS_DDI(dev_priv->dev)) {
1072 /* DDI does not have a specific FDI_TX register */
ad80a810 1073 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1074 val = I915_READ(reg);
ad80a810 1075 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1076 } else {
1077 reg = FDI_TX_CTL(pipe);
1078 val = I915_READ(reg);
1079 cur_state = !!(val & FDI_TX_ENABLE);
1080 }
040484af
JB
1081 WARN(cur_state != state,
1082 "FDI TX state assertion failure (expected %s, current %s)\n",
1083 state_string(state), state_string(cur_state));
1084}
1085#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1086#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1087
1088static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1089 enum pipe pipe, bool state)
1090{
1091 int reg;
1092 u32 val;
1093 bool cur_state;
1094
d63fa0dc
PZ
1095 reg = FDI_RX_CTL(pipe);
1096 val = I915_READ(reg);
1097 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1098 WARN(cur_state != state,
1099 "FDI RX state assertion failure (expected %s, current %s)\n",
1100 state_string(state), state_string(cur_state));
1101}
1102#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1103#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1104
1105static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1106 enum pipe pipe)
1107{
1108 int reg;
1109 u32 val;
1110
1111 /* ILK FDI PLL is always enabled */
3d13ef2e 1112 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1113 return;
1114
bf507ef7 1115 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1116 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1117 return;
1118
040484af
JB
1119 reg = FDI_TX_CTL(pipe);
1120 val = I915_READ(reg);
1121 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1122}
1123
55607e8a
DV
1124void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
040484af
JB
1126{
1127 int reg;
1128 u32 val;
55607e8a 1129 bool cur_state;
040484af
JB
1130
1131 reg = FDI_RX_CTL(pipe);
1132 val = I915_READ(reg);
55607e8a
DV
1133 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1134 WARN(cur_state != state,
1135 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1136 state_string(state), state_string(cur_state));
040484af
JB
1137}
1138
b680c37a
DV
1139void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1140 enum pipe pipe)
ea0760cf 1141{
bedd4dba
JN
1142 struct drm_device *dev = dev_priv->dev;
1143 int pp_reg;
ea0760cf
JB
1144 u32 val;
1145 enum pipe panel_pipe = PIPE_A;
0de3b485 1146 bool locked = true;
ea0760cf 1147
bedd4dba
JN
1148 if (WARN_ON(HAS_DDI(dev)))
1149 return;
1150
1151 if (HAS_PCH_SPLIT(dev)) {
1152 u32 port_sel;
1153
ea0760cf 1154 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1155 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1156
1157 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1158 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1159 panel_pipe = PIPE_B;
1160 /* XXX: else fix for eDP */
1161 } else if (IS_VALLEYVIEW(dev)) {
1162 /* presumably write lock depends on pipe, not port select */
1163 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1164 panel_pipe = pipe;
ea0760cf
JB
1165 } else {
1166 pp_reg = PP_CONTROL;
bedd4dba
JN
1167 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1168 panel_pipe = PIPE_B;
ea0760cf
JB
1169 }
1170
1171 val = I915_READ(pp_reg);
1172 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1173 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1174 locked = false;
1175
ea0760cf
JB
1176 WARN(panel_pipe == pipe && locked,
1177 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1178 pipe_name(pipe));
ea0760cf
JB
1179}
1180
93ce0ba6
JN
1181static void assert_cursor(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
1183{
1184 struct drm_device *dev = dev_priv->dev;
1185 bool cur_state;
1186
d9d82081 1187 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1188 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1189 else
5efb3e28 1190 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1191
1192 WARN(cur_state != state,
1193 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1194 pipe_name(pipe), state_string(state), state_string(cur_state));
1195}
1196#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1197#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1198
b840d907
JB
1199void assert_pipe(struct drm_i915_private *dev_priv,
1200 enum pipe pipe, bool state)
b24e7179
JB
1201{
1202 int reg;
1203 u32 val;
63d7bbe9 1204 bool cur_state;
702e7a56
PZ
1205 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1206 pipe);
b24e7179 1207
b6b5d049
VS
1208 /* if we need the pipe quirk it must be always on */
1209 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1210 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1211 state = true;
1212
f458ebbc 1213 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1214 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1215 cur_state = false;
1216 } else {
1217 reg = PIPECONF(cpu_transcoder);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & PIPECONF_ENABLE);
1220 }
1221
63d7bbe9
JB
1222 WARN(cur_state != state,
1223 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1224 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1225}
1226
931872fc
CW
1227static void assert_plane(struct drm_i915_private *dev_priv,
1228 enum plane plane, bool state)
b24e7179
JB
1229{
1230 int reg;
1231 u32 val;
931872fc 1232 bool cur_state;
b24e7179
JB
1233
1234 reg = DSPCNTR(plane);
1235 val = I915_READ(reg);
931872fc
CW
1236 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1237 WARN(cur_state != state,
1238 "plane %c assertion failure (expected %s, current %s)\n",
1239 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1240}
1241
931872fc
CW
1242#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1243#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1244
b24e7179
JB
1245static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1246 enum pipe pipe)
1247{
653e1026 1248 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1249 int reg, i;
1250 u32 val;
1251 int cur_pipe;
1252
653e1026
VS
1253 /* Primary planes are fixed to pipes on gen4+ */
1254 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1255 reg = DSPCNTR(pipe);
1256 val = I915_READ(reg);
83f26f16 1257 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1258 "plane %c assertion failure, should be disabled but not\n",
1259 plane_name(pipe));
19ec1358 1260 return;
28c05794 1261 }
19ec1358 1262
b24e7179 1263 /* Need to check both planes against the pipe */
055e393f 1264 for_each_pipe(dev_priv, i) {
b24e7179
JB
1265 reg = DSPCNTR(i);
1266 val = I915_READ(reg);
1267 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1268 DISPPLANE_SEL_PIPE_SHIFT;
1269 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1270 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1271 plane_name(i), pipe_name(pipe));
b24e7179
JB
1272 }
1273}
1274
19332d7a
JB
1275static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1276 enum pipe pipe)
1277{
20674eef 1278 struct drm_device *dev = dev_priv->dev;
1fe47785 1279 int reg, sprite;
19332d7a
JB
1280 u32 val;
1281
7feb8b88
DL
1282 if (INTEL_INFO(dev)->gen >= 9) {
1283 for_each_sprite(pipe, sprite) {
1284 val = I915_READ(PLANE_CTL(pipe, sprite));
1285 WARN(val & PLANE_CTL_ENABLE,
1286 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1287 sprite, pipe_name(pipe));
1288 }
1289 } else if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1290 for_each_sprite(pipe, sprite) {
1291 reg = SPCNTR(pipe, sprite);
20674eef 1292 val = I915_READ(reg);
83f26f16 1293 WARN(val & SP_ENABLE,
20674eef 1294 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1295 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1296 }
1297 } else if (INTEL_INFO(dev)->gen >= 7) {
1298 reg = SPRCTL(pipe);
19332d7a 1299 val = I915_READ(reg);
83f26f16 1300 WARN(val & SPRITE_ENABLE,
06da8da2 1301 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1302 plane_name(pipe), pipe_name(pipe));
1303 } else if (INTEL_INFO(dev)->gen >= 5) {
1304 reg = DVSCNTR(pipe);
19332d7a 1305 val = I915_READ(reg);
83f26f16 1306 WARN(val & DVS_ENABLE,
06da8da2 1307 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1308 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1309 }
1310}
1311
08c71e5e
VS
1312static void assert_vblank_disabled(struct drm_crtc *crtc)
1313{
1314 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1315 drm_crtc_vblank_put(crtc);
1316}
1317
89eff4be 1318static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1319{
1320 u32 val;
1321 bool enabled;
1322
89eff4be 1323 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1324
92f2584a
JB
1325 val = I915_READ(PCH_DREF_CONTROL);
1326 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1327 DREF_SUPERSPREAD_SOURCE_MASK));
1328 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1329}
1330
ab9412ba
DV
1331static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1332 enum pipe pipe)
92f2584a
JB
1333{
1334 int reg;
1335 u32 val;
1336 bool enabled;
1337
ab9412ba 1338 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1339 val = I915_READ(reg);
1340 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1341 WARN(enabled,
1342 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1343 pipe_name(pipe));
92f2584a
JB
1344}
1345
4e634389
KP
1346static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1348{
1349 if ((val & DP_PORT_EN) == 0)
1350 return false;
1351
1352 if (HAS_PCH_CPT(dev_priv->dev)) {
1353 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1354 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1355 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1356 return false;
44f37d1f
CML
1357 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1358 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1359 return false;
f0575e92
KP
1360 } else {
1361 if ((val & DP_PIPE_MASK) != (pipe << 30))
1362 return false;
1363 }
1364 return true;
1365}
1366
1519b995
KP
1367static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 val)
1369{
dc0fa718 1370 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1371 return false;
1372
1373 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1374 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1375 return false;
44f37d1f
CML
1376 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1377 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1378 return false;
1519b995 1379 } else {
dc0fa718 1380 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1381 return false;
1382 }
1383 return true;
1384}
1385
1386static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1387 enum pipe pipe, u32 val)
1388{
1389 if ((val & LVDS_PORT_EN) == 0)
1390 return false;
1391
1392 if (HAS_PCH_CPT(dev_priv->dev)) {
1393 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1394 return false;
1395 } else {
1396 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1397 return false;
1398 }
1399 return true;
1400}
1401
1402static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe, u32 val)
1404{
1405 if ((val & ADPA_DAC_ENABLE) == 0)
1406 return false;
1407 if (HAS_PCH_CPT(dev_priv->dev)) {
1408 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1409 return false;
1410 } else {
1411 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1412 return false;
1413 }
1414 return true;
1415}
1416
291906f1 1417static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1418 enum pipe pipe, int reg, u32 port_sel)
291906f1 1419{
47a05eca 1420 u32 val = I915_READ(reg);
4e634389 1421 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1422 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1423 reg, pipe_name(pipe));
de9a35ab 1424
75c5da27
DV
1425 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1426 && (val & DP_PIPEB_SELECT),
de9a35ab 1427 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1428}
1429
1430static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1431 enum pipe pipe, int reg)
1432{
47a05eca 1433 u32 val = I915_READ(reg);
b70ad586 1434 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1435 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1436 reg, pipe_name(pipe));
de9a35ab 1437
dc0fa718 1438 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1439 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1440 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1441}
1442
1443static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1444 enum pipe pipe)
1445{
1446 int reg;
1447 u32 val;
291906f1 1448
f0575e92
KP
1449 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1450 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1451 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1452
1453 reg = PCH_ADPA;
1454 val = I915_READ(reg);
b70ad586 1455 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1456 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1457 pipe_name(pipe));
291906f1
JB
1458
1459 reg = PCH_LVDS;
1460 val = I915_READ(reg);
b70ad586 1461 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1462 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1463 pipe_name(pipe));
291906f1 1464
e2debe91
PZ
1465 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1466 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1467 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1468}
1469
40e9cf64
JB
1470static void intel_init_dpio(struct drm_device *dev)
1471{
1472 struct drm_i915_private *dev_priv = dev->dev_private;
1473
1474 if (!IS_VALLEYVIEW(dev))
1475 return;
1476
a09caddd
CML
1477 /*
1478 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1479 * CHV x1 PHY (DP/HDMI D)
1480 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1481 */
1482 if (IS_CHERRYVIEW(dev)) {
1483 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1484 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1485 } else {
1486 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1487 }
5382f5f3
JB
1488}
1489
426115cf 1490static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1491{
426115cf
DV
1492 struct drm_device *dev = crtc->base.dev;
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494 int reg = DPLL(crtc->pipe);
1495 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1496
426115cf 1497 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1498
1499 /* No really, not for ILK+ */
1500 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1501
1502 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1503 if (IS_MOBILE(dev_priv->dev))
426115cf 1504 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1505
426115cf
DV
1506 I915_WRITE(reg, dpll);
1507 POSTING_READ(reg);
1508 udelay(150);
1509
1510 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1511 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1512
1513 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1514 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1515
1516 /* We do this three times for luck */
426115cf 1517 I915_WRITE(reg, dpll);
87442f73
DV
1518 POSTING_READ(reg);
1519 udelay(150); /* wait for warmup */
426115cf 1520 I915_WRITE(reg, dpll);
87442f73
DV
1521 POSTING_READ(reg);
1522 udelay(150); /* wait for warmup */
426115cf 1523 I915_WRITE(reg, dpll);
87442f73
DV
1524 POSTING_READ(reg);
1525 udelay(150); /* wait for warmup */
1526}
1527
9d556c99
CML
1528static void chv_enable_pll(struct intel_crtc *crtc)
1529{
1530 struct drm_device *dev = crtc->base.dev;
1531 struct drm_i915_private *dev_priv = dev->dev_private;
1532 int pipe = crtc->pipe;
1533 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1534 u32 tmp;
1535
1536 assert_pipe_disabled(dev_priv, crtc->pipe);
1537
1538 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1539
1540 mutex_lock(&dev_priv->dpio_lock);
1541
1542 /* Enable back the 10bit clock to display controller */
1543 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1544 tmp |= DPIO_DCLKP_EN;
1545 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1546
1547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
a11b0703 1553 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1554
1555 /* Check PLL is locked */
a11b0703 1556 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1557 DRM_ERROR("PLL %d failed to lock\n", pipe);
1558
a11b0703
VS
1559 /* not sure when this should be written */
1560 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1561 POSTING_READ(DPLL_MD(pipe));
1562
9d556c99
CML
1563 mutex_unlock(&dev_priv->dpio_lock);
1564}
1565
1c4e0274
VS
1566static int intel_num_dvo_pipes(struct drm_device *dev)
1567{
1568 struct intel_crtc *crtc;
1569 int count = 0;
1570
1571 for_each_intel_crtc(dev, crtc)
1572 count += crtc->active &&
1573 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
1574
1575 return count;
1576}
1577
66e3d5c0 1578static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1579{
66e3d5c0
DV
1580 struct drm_device *dev = crtc->base.dev;
1581 struct drm_i915_private *dev_priv = dev->dev_private;
1582 int reg = DPLL(crtc->pipe);
1583 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1584
66e3d5c0 1585 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1586
63d7bbe9 1587 /* No really, not for ILK+ */
3d13ef2e 1588 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1589
1590 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1591 if (IS_MOBILE(dev) && !IS_I830(dev))
1592 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1593
1c4e0274
VS
1594 /* Enable DVO 2x clock on both PLLs if necessary */
1595 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1596 /*
1597 * It appears to be important that we don't enable this
1598 * for the current pipe before otherwise configuring the
1599 * PLL. No idea how this should be handled if multiple
1600 * DVO outputs are enabled simultaneosly.
1601 */
1602 dpll |= DPLL_DVO_2X_MODE;
1603 I915_WRITE(DPLL(!crtc->pipe),
1604 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1605 }
66e3d5c0
DV
1606
1607 /* Wait for the clocks to stabilize. */
1608 POSTING_READ(reg);
1609 udelay(150);
1610
1611 if (INTEL_INFO(dev)->gen >= 4) {
1612 I915_WRITE(DPLL_MD(crtc->pipe),
1613 crtc->config.dpll_hw_state.dpll_md);
1614 } else {
1615 /* The pixel multiplier can only be updated once the
1616 * DPLL is enabled and the clocks are stable.
1617 *
1618 * So write it again.
1619 */
1620 I915_WRITE(reg, dpll);
1621 }
63d7bbe9
JB
1622
1623 /* We do this three times for luck */
66e3d5c0 1624 I915_WRITE(reg, dpll);
63d7bbe9
JB
1625 POSTING_READ(reg);
1626 udelay(150); /* wait for warmup */
66e3d5c0 1627 I915_WRITE(reg, dpll);
63d7bbe9
JB
1628 POSTING_READ(reg);
1629 udelay(150); /* wait for warmup */
66e3d5c0 1630 I915_WRITE(reg, dpll);
63d7bbe9
JB
1631 POSTING_READ(reg);
1632 udelay(150); /* wait for warmup */
1633}
1634
1635/**
50b44a44 1636 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1637 * @dev_priv: i915 private structure
1638 * @pipe: pipe PLL to disable
1639 *
1640 * Disable the PLL for @pipe, making sure the pipe is off first.
1641 *
1642 * Note! This is for pre-ILK only.
1643 */
1c4e0274 1644static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1645{
1c4e0274
VS
1646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648 enum pipe pipe = crtc->pipe;
1649
1650 /* Disable DVO 2x clock on both PLLs if necessary */
1651 if (IS_I830(dev) &&
1652 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
1653 intel_num_dvo_pipes(dev) == 1) {
1654 I915_WRITE(DPLL(PIPE_B),
1655 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1656 I915_WRITE(DPLL(PIPE_A),
1657 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1658 }
1659
b6b5d049
VS
1660 /* Don't disable pipe or pipe PLLs if needed */
1661 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1662 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1663 return;
1664
1665 /* Make sure the pipe isn't still relying on us */
1666 assert_pipe_disabled(dev_priv, pipe);
1667
50b44a44
DV
1668 I915_WRITE(DPLL(pipe), 0);
1669 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1670}
1671
f6071166
JB
1672static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1673{
1674 u32 val = 0;
1675
1676 /* Make sure the pipe isn't still relying on us */
1677 assert_pipe_disabled(dev_priv, pipe);
1678
e5cbfbfb
ID
1679 /*
1680 * Leave integrated clock source and reference clock enabled for pipe B.
1681 * The latter is needed for VGA hotplug / manual detection.
1682 */
f6071166 1683 if (pipe == PIPE_B)
e5cbfbfb 1684 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1685 I915_WRITE(DPLL(pipe), val);
1686 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1687
1688}
1689
1690static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1691{
d752048d 1692 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1693 u32 val;
1694
a11b0703
VS
1695 /* Make sure the pipe isn't still relying on us */
1696 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1697
a11b0703 1698 /* Set PLL en = 0 */
d17ec4ce 1699 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1700 if (pipe != PIPE_A)
1701 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1702 I915_WRITE(DPLL(pipe), val);
1703 POSTING_READ(DPLL(pipe));
d752048d
VS
1704
1705 mutex_lock(&dev_priv->dpio_lock);
1706
1707 /* Disable 10bit clock to display controller */
1708 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1709 val &= ~DPIO_DCLKP_EN;
1710 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1711
61407f6d
VS
1712 /* disable left/right clock distribution */
1713 if (pipe != PIPE_B) {
1714 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1715 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1716 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1717 } else {
1718 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1719 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1720 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1721 }
1722
d752048d 1723 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1724}
1725
e4607fcf
CML
1726void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1727 struct intel_digital_port *dport)
89b667f8
JB
1728{
1729 u32 port_mask;
00fc31b7 1730 int dpll_reg;
89b667f8 1731
e4607fcf
CML
1732 switch (dport->port) {
1733 case PORT_B:
89b667f8 1734 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1735 dpll_reg = DPLL(0);
e4607fcf
CML
1736 break;
1737 case PORT_C:
89b667f8 1738 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1739 dpll_reg = DPLL(0);
1740 break;
1741 case PORT_D:
1742 port_mask = DPLL_PORTD_READY_MASK;
1743 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1744 break;
1745 default:
1746 BUG();
1747 }
89b667f8 1748
00fc31b7 1749 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1750 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1751 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1752}
1753
b14b1055
DV
1754static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1755{
1756 struct drm_device *dev = crtc->base.dev;
1757 struct drm_i915_private *dev_priv = dev->dev_private;
1758 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1759
be19f0ff
CW
1760 if (WARN_ON(pll == NULL))
1761 return;
1762
b14b1055
DV
1763 WARN_ON(!pll->refcount);
1764 if (pll->active == 0) {
1765 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1766 WARN_ON(pll->on);
1767 assert_shared_dpll_disabled(dev_priv, pll);
1768
1769 pll->mode_set(dev_priv, pll);
1770 }
1771}
1772
92f2584a 1773/**
85b3894f 1774 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1775 * @dev_priv: i915 private structure
1776 * @pipe: pipe PLL to enable
1777 *
1778 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1779 * drives the transcoder clock.
1780 */
85b3894f 1781static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1782{
3d13ef2e
DL
1783 struct drm_device *dev = crtc->base.dev;
1784 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1785 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1786
87a875bb 1787 if (WARN_ON(pll == NULL))
48da64a8
CW
1788 return;
1789
1790 if (WARN_ON(pll->refcount == 0))
1791 return;
ee7b9f93 1792
74dd6928 1793 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1794 pll->name, pll->active, pll->on,
e2b78267 1795 crtc->base.base.id);
92f2584a 1796
cdbd2316
DV
1797 if (pll->active++) {
1798 WARN_ON(!pll->on);
e9d6944e 1799 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1800 return;
1801 }
f4a091c7 1802 WARN_ON(pll->on);
ee7b9f93 1803
bd2bb1b9
PZ
1804 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1805
46edb027 1806 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1807 pll->enable(dev_priv, pll);
ee7b9f93 1808 pll->on = true;
92f2584a
JB
1809}
1810
f6daaec2 1811static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1812{
3d13ef2e
DL
1813 struct drm_device *dev = crtc->base.dev;
1814 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1815 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1816
92f2584a 1817 /* PCH only available on ILK+ */
3d13ef2e 1818 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1819 if (WARN_ON(pll == NULL))
ee7b9f93 1820 return;
92f2584a 1821
48da64a8
CW
1822 if (WARN_ON(pll->refcount == 0))
1823 return;
7a419866 1824
46edb027
DV
1825 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1826 pll->name, pll->active, pll->on,
e2b78267 1827 crtc->base.base.id);
7a419866 1828
48da64a8 1829 if (WARN_ON(pll->active == 0)) {
e9d6944e 1830 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1831 return;
1832 }
1833
e9d6944e 1834 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1835 WARN_ON(!pll->on);
cdbd2316 1836 if (--pll->active)
7a419866 1837 return;
ee7b9f93 1838
46edb027 1839 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1840 pll->disable(dev_priv, pll);
ee7b9f93 1841 pll->on = false;
bd2bb1b9
PZ
1842
1843 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1844}
1845
b8a4f404
PZ
1846static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1847 enum pipe pipe)
040484af 1848{
23670b32 1849 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1850 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1852 uint32_t reg, val, pipeconf_val;
040484af
JB
1853
1854 /* PCH only available on ILK+ */
55522f37 1855 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1856
1857 /* Make sure PCH DPLL is enabled */
e72f9fbf 1858 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1859 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1860
1861 /* FDI must be feeding us bits for PCH ports */
1862 assert_fdi_tx_enabled(dev_priv, pipe);
1863 assert_fdi_rx_enabled(dev_priv, pipe);
1864
23670b32
DV
1865 if (HAS_PCH_CPT(dev)) {
1866 /* Workaround: Set the timing override bit before enabling the
1867 * pch transcoder. */
1868 reg = TRANS_CHICKEN2(pipe);
1869 val = I915_READ(reg);
1870 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1871 I915_WRITE(reg, val);
59c859d6 1872 }
23670b32 1873
ab9412ba 1874 reg = PCH_TRANSCONF(pipe);
040484af 1875 val = I915_READ(reg);
5f7f726d 1876 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1877
1878 if (HAS_PCH_IBX(dev_priv->dev)) {
1879 /*
1880 * make the BPC in transcoder be consistent with
1881 * that in pipeconf reg.
1882 */
dfd07d72
DV
1883 val &= ~PIPECONF_BPC_MASK;
1884 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1885 }
5f7f726d
PZ
1886
1887 val &= ~TRANS_INTERLACE_MASK;
1888 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1889 if (HAS_PCH_IBX(dev_priv->dev) &&
1890 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1891 val |= TRANS_LEGACY_INTERLACED_ILK;
1892 else
1893 val |= TRANS_INTERLACED;
5f7f726d
PZ
1894 else
1895 val |= TRANS_PROGRESSIVE;
1896
040484af
JB
1897 I915_WRITE(reg, val | TRANS_ENABLE);
1898 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1899 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1900}
1901
8fb033d7 1902static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1903 enum transcoder cpu_transcoder)
040484af 1904{
8fb033d7 1905 u32 val, pipeconf_val;
8fb033d7
PZ
1906
1907 /* PCH only available on ILK+ */
55522f37 1908 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1909
8fb033d7 1910 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1911 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1912 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1913
223a6fdf
PZ
1914 /* Workaround: set timing override bit. */
1915 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1916 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1917 I915_WRITE(_TRANSA_CHICKEN2, val);
1918
25f3ef11 1919 val = TRANS_ENABLE;
937bb610 1920 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1921
9a76b1c6
PZ
1922 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1923 PIPECONF_INTERLACED_ILK)
a35f2679 1924 val |= TRANS_INTERLACED;
8fb033d7
PZ
1925 else
1926 val |= TRANS_PROGRESSIVE;
1927
ab9412ba
DV
1928 I915_WRITE(LPT_TRANSCONF, val);
1929 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1930 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1931}
1932
b8a4f404
PZ
1933static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1934 enum pipe pipe)
040484af 1935{
23670b32
DV
1936 struct drm_device *dev = dev_priv->dev;
1937 uint32_t reg, val;
040484af
JB
1938
1939 /* FDI relies on the transcoder */
1940 assert_fdi_tx_disabled(dev_priv, pipe);
1941 assert_fdi_rx_disabled(dev_priv, pipe);
1942
291906f1
JB
1943 /* Ports must be off as well */
1944 assert_pch_ports_disabled(dev_priv, pipe);
1945
ab9412ba 1946 reg = PCH_TRANSCONF(pipe);
040484af
JB
1947 val = I915_READ(reg);
1948 val &= ~TRANS_ENABLE;
1949 I915_WRITE(reg, val);
1950 /* wait for PCH transcoder off, transcoder state */
1951 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1952 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1953
1954 if (!HAS_PCH_IBX(dev)) {
1955 /* Workaround: Clear the timing override chicken bit again. */
1956 reg = TRANS_CHICKEN2(pipe);
1957 val = I915_READ(reg);
1958 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1959 I915_WRITE(reg, val);
1960 }
040484af
JB
1961}
1962
ab4d966c 1963static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1964{
8fb033d7
PZ
1965 u32 val;
1966
ab9412ba 1967 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1968 val &= ~TRANS_ENABLE;
ab9412ba 1969 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1970 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1971 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1972 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1973
1974 /* Workaround: clear timing override bit. */
1975 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1976 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1977 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1978}
1979
b24e7179 1980/**
309cfea8 1981 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1982 * @crtc: crtc responsible for the pipe
b24e7179 1983 *
0372264a 1984 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1985 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1986 */
e1fdc473 1987static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1988{
0372264a
PZ
1989 struct drm_device *dev = crtc->base.dev;
1990 struct drm_i915_private *dev_priv = dev->dev_private;
1991 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1992 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1993 pipe);
1a240d4d 1994 enum pipe pch_transcoder;
b24e7179
JB
1995 int reg;
1996 u32 val;
1997
58c6eaa2 1998 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1999 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2000 assert_sprites_disabled(dev_priv, pipe);
2001
681e5811 2002 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2003 pch_transcoder = TRANSCODER_A;
2004 else
2005 pch_transcoder = pipe;
2006
b24e7179
JB
2007 /*
2008 * A pipe without a PLL won't actually be able to drive bits from
2009 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2010 * need the check.
2011 */
2012 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2013 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2014 assert_dsi_pll_enabled(dev_priv);
2015 else
2016 assert_pll_enabled(dev_priv, pipe);
040484af 2017 else {
30421c4f 2018 if (crtc->config.has_pch_encoder) {
040484af 2019 /* if driving the PCH, we need FDI enabled */
cc391bbb 2020 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2021 assert_fdi_tx_pll_enabled(dev_priv,
2022 (enum pipe) cpu_transcoder);
040484af
JB
2023 }
2024 /* FIXME: assert CPU port conditions for SNB+ */
2025 }
b24e7179 2026
702e7a56 2027 reg = PIPECONF(cpu_transcoder);
b24e7179 2028 val = I915_READ(reg);
7ad25d48 2029 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2030 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2031 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2032 return;
7ad25d48 2033 }
00d70b15
CW
2034
2035 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2036 POSTING_READ(reg);
b24e7179
JB
2037}
2038
2039/**
309cfea8 2040 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2041 * @crtc: crtc whose pipes is to be disabled
b24e7179 2042 *
575f7ab7
VS
2043 * Disable the pipe of @crtc, making sure that various hardware
2044 * specific requirements are met, if applicable, e.g. plane
2045 * disabled, panel fitter off, etc.
b24e7179
JB
2046 *
2047 * Will wait until the pipe has shut down before returning.
2048 */
575f7ab7 2049static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2050{
575f7ab7
VS
2051 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2052 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2053 enum pipe pipe = crtc->pipe;
b24e7179
JB
2054 int reg;
2055 u32 val;
2056
2057 /*
2058 * Make sure planes won't keep trying to pump pixels to us,
2059 * or we might hang the display.
2060 */
2061 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2062 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2063 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2064
702e7a56 2065 reg = PIPECONF(cpu_transcoder);
b24e7179 2066 val = I915_READ(reg);
00d70b15
CW
2067 if ((val & PIPECONF_ENABLE) == 0)
2068 return;
2069
67adc644
VS
2070 /*
2071 * Double wide has implications for planes
2072 * so best keep it disabled when not needed.
2073 */
2074 if (crtc->config.double_wide)
2075 val &= ~PIPECONF_DOUBLE_WIDE;
2076
2077 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2078 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2079 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2080 val &= ~PIPECONF_ENABLE;
2081
2082 I915_WRITE(reg, val);
2083 if ((val & PIPECONF_ENABLE) == 0)
2084 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2085}
2086
d74362c9
KP
2087/*
2088 * Plane regs are double buffered, going from enabled->disabled needs a
2089 * trigger in order to latch. The display address reg provides this.
2090 */
1dba99f4
VS
2091void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2092 enum plane plane)
d74362c9 2093{
3d13ef2e
DL
2094 struct drm_device *dev = dev_priv->dev;
2095 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2096
2097 I915_WRITE(reg, I915_READ(reg));
2098 POSTING_READ(reg);
d74362c9
KP
2099}
2100
b24e7179 2101/**
262ca2b0 2102 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2103 * @plane: plane to be enabled
2104 * @crtc: crtc for the plane
b24e7179 2105 *
fdd508a6 2106 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2107 */
fdd508a6
VS
2108static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2109 struct drm_crtc *crtc)
b24e7179 2110{
fdd508a6
VS
2111 struct drm_device *dev = plane->dev;
2112 struct drm_i915_private *dev_priv = dev->dev_private;
2113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2114
2115 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2116 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2117
98ec7739
VS
2118 if (intel_crtc->primary_enabled)
2119 return;
0037f71c 2120
4c445e0e 2121 intel_crtc->primary_enabled = true;
939c2fe8 2122
fdd508a6
VS
2123 dev_priv->display.update_primary_plane(crtc, plane->fb,
2124 crtc->x, crtc->y);
33c3b0d1
VS
2125
2126 /*
2127 * BDW signals flip done immediately if the plane
2128 * is disabled, even if the plane enable is already
2129 * armed to occur at the next vblank :(
2130 */
2131 if (IS_BROADWELL(dev))
2132 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2133}
2134
b24e7179 2135/**
262ca2b0 2136 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2137 * @plane: plane to be disabled
2138 * @crtc: crtc for the plane
b24e7179 2139 *
fdd508a6 2140 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2141 */
fdd508a6
VS
2142static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2143 struct drm_crtc *crtc)
b24e7179 2144{
fdd508a6
VS
2145 struct drm_device *dev = plane->dev;
2146 struct drm_i915_private *dev_priv = dev->dev_private;
2147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2148
2149 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2150
98ec7739
VS
2151 if (!intel_crtc->primary_enabled)
2152 return;
0037f71c 2153
4c445e0e 2154 intel_crtc->primary_enabled = false;
939c2fe8 2155
fdd508a6
VS
2156 dev_priv->display.update_primary_plane(crtc, plane->fb,
2157 crtc->x, crtc->y);
b24e7179
JB
2158}
2159
693db184
CW
2160static bool need_vtd_wa(struct drm_device *dev)
2161{
2162#ifdef CONFIG_INTEL_IOMMU
2163 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2164 return true;
2165#endif
2166 return false;
2167}
2168
a57ce0b2
JB
2169static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2170{
2171 int tile_height;
2172
2173 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2174 return ALIGN(height, tile_height);
2175}
2176
127bd2ac 2177int
48b956c5 2178intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2179 struct drm_i915_gem_object *obj,
a4872ba6 2180 struct intel_engine_cs *pipelined)
6b95a207 2181{
ce453d81 2182 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2183 u32 alignment;
2184 int ret;
2185
ebcdd39e
MR
2186 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2187
05394f39 2188 switch (obj->tiling_mode) {
6b95a207 2189 case I915_TILING_NONE:
1fada4cc
DL
2190 if (INTEL_INFO(dev)->gen >= 9)
2191 alignment = 256 * 1024;
2192 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2193 alignment = 128 * 1024;
a6c45cf0 2194 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2195 alignment = 4 * 1024;
2196 else
2197 alignment = 64 * 1024;
6b95a207
KH
2198 break;
2199 case I915_TILING_X:
1fada4cc
DL
2200 if (INTEL_INFO(dev)->gen >= 9)
2201 alignment = 256 * 1024;
2202 else {
2203 /* pin() will align the object as required by fence */
2204 alignment = 0;
2205 }
6b95a207
KH
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
d6dd6843
PZ
2222 /*
2223 * Global gtt pte registers are special registers which actually forward
2224 * writes to a chunk of system memory. Which means that there is no risk
2225 * that the register values disappear as soon as we call
2226 * intel_runtime_pm_put(), so it is correct to wrap only the
2227 * pin/unpin/fence and not more.
2228 */
2229 intel_runtime_pm_get(dev_priv);
2230
ce453d81 2231 dev_priv->mm.interruptible = false;
2da3b9b9 2232 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2233 if (ret)
ce453d81 2234 goto err_interruptible;
6b95a207
KH
2235
2236 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2237 * fence, whereas 965+ only requires a fence if using
2238 * framebuffer compression. For simplicity, we always install
2239 * a fence as the cost is not that onerous.
2240 */
06d98131 2241 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2242 if (ret)
2243 goto err_unpin;
1690e1eb 2244
9a5a53b3 2245 i915_gem_object_pin_fence(obj);
6b95a207 2246
ce453d81 2247 dev_priv->mm.interruptible = true;
d6dd6843 2248 intel_runtime_pm_put(dev_priv);
6b95a207 2249 return 0;
48b956c5
CW
2250
2251err_unpin:
cc98b413 2252 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2253err_interruptible:
2254 dev_priv->mm.interruptible = true;
d6dd6843 2255 intel_runtime_pm_put(dev_priv);
48b956c5 2256 return ret;
6b95a207
KH
2257}
2258
1690e1eb
CW
2259void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2260{
ebcdd39e
MR
2261 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2262
1690e1eb 2263 i915_gem_object_unpin_fence(obj);
cc98b413 2264 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2265}
2266
c2c75131
DV
2267/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2268 * is assumed to be a power-of-two. */
bc752862
CW
2269unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2270 unsigned int tiling_mode,
2271 unsigned int cpp,
2272 unsigned int pitch)
c2c75131 2273{
bc752862
CW
2274 if (tiling_mode != I915_TILING_NONE) {
2275 unsigned int tile_rows, tiles;
c2c75131 2276
bc752862
CW
2277 tile_rows = *y / 8;
2278 *y %= 8;
c2c75131 2279
bc752862
CW
2280 tiles = *x / (512/cpp);
2281 *x %= 512/cpp;
2282
2283 return tile_rows * pitch * 8 + tiles * 4096;
2284 } else {
2285 unsigned int offset;
2286
2287 offset = *y * pitch + *x * cpp;
2288 *y = 0;
2289 *x = (offset & 4095) / cpp;
2290 return offset & -4096;
2291 }
c2c75131
DV
2292}
2293
46f297fb
JB
2294int intel_format_to_fourcc(int format)
2295{
2296 switch (format) {
2297 case DISPPLANE_8BPP:
2298 return DRM_FORMAT_C8;
2299 case DISPPLANE_BGRX555:
2300 return DRM_FORMAT_XRGB1555;
2301 case DISPPLANE_BGRX565:
2302 return DRM_FORMAT_RGB565;
2303 default:
2304 case DISPPLANE_BGRX888:
2305 return DRM_FORMAT_XRGB8888;
2306 case DISPPLANE_RGBX888:
2307 return DRM_FORMAT_XBGR8888;
2308 case DISPPLANE_BGRX101010:
2309 return DRM_FORMAT_XRGB2101010;
2310 case DISPPLANE_RGBX101010:
2311 return DRM_FORMAT_XBGR2101010;
2312 }
2313}
2314
484b41dd 2315static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2316 struct intel_plane_config *plane_config)
2317{
2318 struct drm_device *dev = crtc->base.dev;
2319 struct drm_i915_gem_object *obj = NULL;
2320 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2321 u32 base = plane_config->base;
2322
ff2652ea
CW
2323 if (plane_config->size == 0)
2324 return false;
2325
46f297fb
JB
2326 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2327 plane_config->size);
2328 if (!obj)
484b41dd 2329 return false;
46f297fb
JB
2330
2331 if (plane_config->tiled) {
2332 obj->tiling_mode = I915_TILING_X;
66e514c1 2333 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2334 }
2335
66e514c1
DA
2336 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2337 mode_cmd.width = crtc->base.primary->fb->width;
2338 mode_cmd.height = crtc->base.primary->fb->height;
2339 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2340
2341 mutex_lock(&dev->struct_mutex);
2342
66e514c1 2343 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2344 &mode_cmd, obj)) {
46f297fb
JB
2345 DRM_DEBUG_KMS("intel fb init failed\n");
2346 goto out_unref_obj;
2347 }
2348
a071fa00 2349 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2350 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2351
2352 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2353 return true;
46f297fb
JB
2354
2355out_unref_obj:
2356 drm_gem_object_unreference(&obj->base);
2357 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2358 return false;
2359}
2360
2361static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2362 struct intel_plane_config *plane_config)
2363{
2364 struct drm_device *dev = intel_crtc->base.dev;
2365 struct drm_crtc *c;
2366 struct intel_crtc *i;
2ff8fde1 2367 struct drm_i915_gem_object *obj;
484b41dd 2368
66e514c1 2369 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2370 return;
2371
2372 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2373 return;
2374
66e514c1
DA
2375 kfree(intel_crtc->base.primary->fb);
2376 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2377
2378 /*
2379 * Failed to alloc the obj, check to see if we should share
2380 * an fb with another CRTC instead
2381 */
70e1e0ec 2382 for_each_crtc(dev, c) {
484b41dd
JB
2383 i = to_intel_crtc(c);
2384
2385 if (c == &intel_crtc->base)
2386 continue;
2387
2ff8fde1
MR
2388 if (!i->active)
2389 continue;
2390
2391 obj = intel_fb_obj(c->primary->fb);
2392 if (obj == NULL)
484b41dd
JB
2393 continue;
2394
2ff8fde1 2395 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2396 drm_framebuffer_reference(c->primary->fb);
2397 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2398 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2399 break;
2400 }
2401 }
46f297fb
JB
2402}
2403
29b9bde6
DV
2404static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2405 struct drm_framebuffer *fb,
2406 int x, int y)
81255565
JB
2407{
2408 struct drm_device *dev = crtc->dev;
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2411 struct drm_i915_gem_object *obj;
81255565 2412 int plane = intel_crtc->plane;
e506a0c6 2413 unsigned long linear_offset;
81255565 2414 u32 dspcntr;
f45651ba 2415 u32 reg = DSPCNTR(plane);
48404c1e 2416 int pixel_size;
f45651ba 2417
fdd508a6
VS
2418 if (!intel_crtc->primary_enabled) {
2419 I915_WRITE(reg, 0);
2420 if (INTEL_INFO(dev)->gen >= 4)
2421 I915_WRITE(DSPSURF(plane), 0);
2422 else
2423 I915_WRITE(DSPADDR(plane), 0);
2424 POSTING_READ(reg);
2425 return;
2426 }
2427
c9ba6fad
VS
2428 obj = intel_fb_obj(fb);
2429 if (WARN_ON(obj == NULL))
2430 return;
2431
2432 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2433
f45651ba
VS
2434 dspcntr = DISPPLANE_GAMMA_ENABLE;
2435
fdd508a6 2436 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2437
2438 if (INTEL_INFO(dev)->gen < 4) {
2439 if (intel_crtc->pipe == PIPE_B)
2440 dspcntr |= DISPPLANE_SEL_PIPE_B;
2441
2442 /* pipesrc and dspsize control the size that is scaled from,
2443 * which should always be the user's requested size.
2444 */
2445 I915_WRITE(DSPSIZE(plane),
2446 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2447 (intel_crtc->config.pipe_src_w - 1));
2448 I915_WRITE(DSPPOS(plane), 0);
2449 }
81255565 2450
57779d06
VS
2451 switch (fb->pixel_format) {
2452 case DRM_FORMAT_C8:
81255565
JB
2453 dspcntr |= DISPPLANE_8BPP;
2454 break;
57779d06
VS
2455 case DRM_FORMAT_XRGB1555:
2456 case DRM_FORMAT_ARGB1555:
2457 dspcntr |= DISPPLANE_BGRX555;
81255565 2458 break;
57779d06
VS
2459 case DRM_FORMAT_RGB565:
2460 dspcntr |= DISPPLANE_BGRX565;
2461 break;
2462 case DRM_FORMAT_XRGB8888:
2463 case DRM_FORMAT_ARGB8888:
2464 dspcntr |= DISPPLANE_BGRX888;
2465 break;
2466 case DRM_FORMAT_XBGR8888:
2467 case DRM_FORMAT_ABGR8888:
2468 dspcntr |= DISPPLANE_RGBX888;
2469 break;
2470 case DRM_FORMAT_XRGB2101010:
2471 case DRM_FORMAT_ARGB2101010:
2472 dspcntr |= DISPPLANE_BGRX101010;
2473 break;
2474 case DRM_FORMAT_XBGR2101010:
2475 case DRM_FORMAT_ABGR2101010:
2476 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2477 break;
2478 default:
baba133a 2479 BUG();
81255565 2480 }
57779d06 2481
f45651ba
VS
2482 if (INTEL_INFO(dev)->gen >= 4 &&
2483 obj->tiling_mode != I915_TILING_NONE)
2484 dspcntr |= DISPPLANE_TILED;
81255565 2485
de1aa629
VS
2486 if (IS_G4X(dev))
2487 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2488
b9897127 2489 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2490
c2c75131
DV
2491 if (INTEL_INFO(dev)->gen >= 4) {
2492 intel_crtc->dspaddr_offset =
bc752862 2493 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2494 pixel_size,
bc752862 2495 fb->pitches[0]);
c2c75131
DV
2496 linear_offset -= intel_crtc->dspaddr_offset;
2497 } else {
e506a0c6 2498 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2499 }
e506a0c6 2500
48404c1e
SJ
2501 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2502 dspcntr |= DISPPLANE_ROTATE_180;
2503
2504 x += (intel_crtc->config.pipe_src_w - 1);
2505 y += (intel_crtc->config.pipe_src_h - 1);
2506
2507 /* Finding the last pixel of the last line of the display
2508 data and adding to linear_offset*/
2509 linear_offset +=
2510 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2511 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2512 }
2513
2514 I915_WRITE(reg, dspcntr);
2515
f343c5f6
BW
2516 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2517 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2518 fb->pitches[0]);
01f2c773 2519 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2520 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2521 I915_WRITE(DSPSURF(plane),
2522 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2523 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2524 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2525 } else
f343c5f6 2526 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2527 POSTING_READ(reg);
17638cd6
JB
2528}
2529
29b9bde6
DV
2530static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2531 struct drm_framebuffer *fb,
2532 int x, int y)
17638cd6
JB
2533{
2534 struct drm_device *dev = crtc->dev;
2535 struct drm_i915_private *dev_priv = dev->dev_private;
2536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2537 struct drm_i915_gem_object *obj;
17638cd6 2538 int plane = intel_crtc->plane;
e506a0c6 2539 unsigned long linear_offset;
17638cd6 2540 u32 dspcntr;
f45651ba 2541 u32 reg = DSPCNTR(plane);
48404c1e 2542 int pixel_size;
f45651ba 2543
fdd508a6
VS
2544 if (!intel_crtc->primary_enabled) {
2545 I915_WRITE(reg, 0);
2546 I915_WRITE(DSPSURF(plane), 0);
2547 POSTING_READ(reg);
2548 return;
2549 }
2550
c9ba6fad
VS
2551 obj = intel_fb_obj(fb);
2552 if (WARN_ON(obj == NULL))
2553 return;
2554
2555 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2556
f45651ba
VS
2557 dspcntr = DISPPLANE_GAMMA_ENABLE;
2558
fdd508a6 2559 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2560
2561 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2562 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2563
57779d06
VS
2564 switch (fb->pixel_format) {
2565 case DRM_FORMAT_C8:
17638cd6
JB
2566 dspcntr |= DISPPLANE_8BPP;
2567 break;
57779d06
VS
2568 case DRM_FORMAT_RGB565:
2569 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2570 break;
57779d06
VS
2571 case DRM_FORMAT_XRGB8888:
2572 case DRM_FORMAT_ARGB8888:
2573 dspcntr |= DISPPLANE_BGRX888;
2574 break;
2575 case DRM_FORMAT_XBGR8888:
2576 case DRM_FORMAT_ABGR8888:
2577 dspcntr |= DISPPLANE_RGBX888;
2578 break;
2579 case DRM_FORMAT_XRGB2101010:
2580 case DRM_FORMAT_ARGB2101010:
2581 dspcntr |= DISPPLANE_BGRX101010;
2582 break;
2583 case DRM_FORMAT_XBGR2101010:
2584 case DRM_FORMAT_ABGR2101010:
2585 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2586 break;
2587 default:
baba133a 2588 BUG();
17638cd6
JB
2589 }
2590
2591 if (obj->tiling_mode != I915_TILING_NONE)
2592 dspcntr |= DISPPLANE_TILED;
17638cd6 2593
f45651ba 2594 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2595 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2596
b9897127 2597 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2598 intel_crtc->dspaddr_offset =
bc752862 2599 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2600 pixel_size,
bc752862 2601 fb->pitches[0]);
c2c75131 2602 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2603 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2604 dspcntr |= DISPPLANE_ROTATE_180;
2605
2606 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2607 x += (intel_crtc->config.pipe_src_w - 1);
2608 y += (intel_crtc->config.pipe_src_h - 1);
2609
2610 /* Finding the last pixel of the last line of the display
2611 data and adding to linear_offset*/
2612 linear_offset +=
2613 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2614 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2615 }
2616 }
2617
2618 I915_WRITE(reg, dspcntr);
17638cd6 2619
f343c5f6
BW
2620 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2621 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2622 fb->pitches[0]);
01f2c773 2623 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2624 I915_WRITE(DSPSURF(plane),
2625 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2626 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2627 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2628 } else {
2629 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2630 I915_WRITE(DSPLINOFF(plane), linear_offset);
2631 }
17638cd6 2632 POSTING_READ(reg);
17638cd6
JB
2633}
2634
70d21f0e
DL
2635static void skylake_update_primary_plane(struct drm_crtc *crtc,
2636 struct drm_framebuffer *fb,
2637 int x, int y)
2638{
2639 struct drm_device *dev = crtc->dev;
2640 struct drm_i915_private *dev_priv = dev->dev_private;
2641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2642 struct intel_framebuffer *intel_fb;
2643 struct drm_i915_gem_object *obj;
2644 int pipe = intel_crtc->pipe;
2645 u32 plane_ctl, stride;
2646
2647 if (!intel_crtc->primary_enabled) {
2648 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2649 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2650 POSTING_READ(PLANE_CTL(pipe, 0));
2651 return;
2652 }
2653
2654 plane_ctl = PLANE_CTL_ENABLE |
2655 PLANE_CTL_PIPE_GAMMA_ENABLE |
2656 PLANE_CTL_PIPE_CSC_ENABLE;
2657
2658 switch (fb->pixel_format) {
2659 case DRM_FORMAT_RGB565:
2660 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2661 break;
2662 case DRM_FORMAT_XRGB8888:
2663 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2664 break;
2665 case DRM_FORMAT_XBGR8888:
2666 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2667 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2668 break;
2669 case DRM_FORMAT_XRGB2101010:
2670 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2671 break;
2672 case DRM_FORMAT_XBGR2101010:
2673 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2674 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2675 break;
2676 default:
2677 BUG();
2678 }
2679
2680 intel_fb = to_intel_framebuffer(fb);
2681 obj = intel_fb->obj;
2682
2683 /*
2684 * The stride is either expressed as a multiple of 64 bytes chunks for
2685 * linear buffers or in number of tiles for tiled buffers.
2686 */
2687 switch (obj->tiling_mode) {
2688 case I915_TILING_NONE:
2689 stride = fb->pitches[0] >> 6;
2690 break;
2691 case I915_TILING_X:
2692 plane_ctl |= PLANE_CTL_TILED_X;
2693 stride = fb->pitches[0] >> 9;
2694 break;
2695 default:
2696 BUG();
2697 }
2698
2699 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
2700
2701 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2702
2703 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2704 i915_gem_obj_ggtt_offset(obj),
2705 x, y, fb->width, fb->height,
2706 fb->pitches[0]);
2707
2708 I915_WRITE(PLANE_POS(pipe, 0), 0);
2709 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2710 I915_WRITE(PLANE_SIZE(pipe, 0),
2711 (intel_crtc->config.pipe_src_h - 1) << 16 |
2712 (intel_crtc->config.pipe_src_w - 1));
2713 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2714 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2715
2716 POSTING_READ(PLANE_SURF(pipe, 0));
2717}
2718
17638cd6
JB
2719/* Assume fb object is pinned & idle & fenced and just update base pointers */
2720static int
2721intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2722 int x, int y, enum mode_set_atomic state)
2723{
2724 struct drm_device *dev = crtc->dev;
2725 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2726
6b8e6ed0
CW
2727 if (dev_priv->display.disable_fbc)
2728 dev_priv->display.disable_fbc(dev);
81255565 2729
29b9bde6
DV
2730 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2731
2732 return 0;
81255565
JB
2733}
2734
96a02917
VS
2735void intel_display_handle_reset(struct drm_device *dev)
2736{
2737 struct drm_i915_private *dev_priv = dev->dev_private;
2738 struct drm_crtc *crtc;
2739
2740 /*
2741 * Flips in the rings have been nuked by the reset,
2742 * so complete all pending flips so that user space
2743 * will get its events and not get stuck.
2744 *
2745 * Also update the base address of all primary
2746 * planes to the the last fb to make sure we're
2747 * showing the correct fb after a reset.
2748 *
2749 * Need to make two loops over the crtcs so that we
2750 * don't try to grab a crtc mutex before the
2751 * pending_flip_queue really got woken up.
2752 */
2753
70e1e0ec 2754 for_each_crtc(dev, crtc) {
96a02917
VS
2755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2756 enum plane plane = intel_crtc->plane;
2757
2758 intel_prepare_page_flip(dev, plane);
2759 intel_finish_page_flip_plane(dev, plane);
2760 }
2761
70e1e0ec 2762 for_each_crtc(dev, crtc) {
96a02917
VS
2763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2764
51fd371b 2765 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2766 /*
2767 * FIXME: Once we have proper support for primary planes (and
2768 * disabling them without disabling the entire crtc) allow again
66e514c1 2769 * a NULL crtc->primary->fb.
947fdaad 2770 */
f4510a27 2771 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2772 dev_priv->display.update_primary_plane(crtc,
66e514c1 2773 crtc->primary->fb,
262ca2b0
MR
2774 crtc->x,
2775 crtc->y);
51fd371b 2776 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2777 }
2778}
2779
14667a4b
CW
2780static int
2781intel_finish_fb(struct drm_framebuffer *old_fb)
2782{
2ff8fde1 2783 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2784 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2785 bool was_interruptible = dev_priv->mm.interruptible;
2786 int ret;
2787
14667a4b
CW
2788 /* Big Hammer, we also need to ensure that any pending
2789 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2790 * current scanout is retired before unpinning the old
2791 * framebuffer.
2792 *
2793 * This should only fail upon a hung GPU, in which case we
2794 * can safely continue.
2795 */
2796 dev_priv->mm.interruptible = false;
2797 ret = i915_gem_object_finish_gpu(obj);
2798 dev_priv->mm.interruptible = was_interruptible;
2799
2800 return ret;
2801}
2802
7d5e3799
CW
2803static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2804{
2805 struct drm_device *dev = crtc->dev;
2806 struct drm_i915_private *dev_priv = dev->dev_private;
2807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
2808 bool pending;
2809
2810 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2811 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2812 return false;
2813
5e2d7afc 2814 spin_lock_irq(&dev->event_lock);
7d5e3799 2815 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 2816 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
2817
2818 return pending;
2819}
2820
e30e8f75
GP
2821static void intel_update_pipe_size(struct intel_crtc *crtc)
2822{
2823 struct drm_device *dev = crtc->base.dev;
2824 struct drm_i915_private *dev_priv = dev->dev_private;
2825 const struct drm_display_mode *adjusted_mode;
2826
2827 if (!i915.fastboot)
2828 return;
2829
2830 /*
2831 * Update pipe size and adjust fitter if needed: the reason for this is
2832 * that in compute_mode_changes we check the native mode (not the pfit
2833 * mode) to see if we can flip rather than do a full mode set. In the
2834 * fastboot case, we'll flip, but if we don't update the pipesrc and
2835 * pfit state, we'll end up with a big fb scanned out into the wrong
2836 * sized surface.
2837 *
2838 * To fix this properly, we need to hoist the checks up into
2839 * compute_mode_changes (or above), check the actual pfit state and
2840 * whether the platform allows pfit disable with pipe active, and only
2841 * then update the pipesrc and pfit state, even on the flip path.
2842 */
2843
2844 adjusted_mode = &crtc->config.adjusted_mode;
2845
2846 I915_WRITE(PIPESRC(crtc->pipe),
2847 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2848 (adjusted_mode->crtc_vdisplay - 1));
2849 if (!crtc->config.pch_pfit.enabled &&
2850 (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) ||
2851 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))) {
2852 I915_WRITE(PF_CTL(crtc->pipe), 0);
2853 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2854 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2855 }
2856 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2857 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2858}
2859
5c3b82e2 2860static int
3c4fdcfb 2861intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2862 struct drm_framebuffer *fb)
79e53945
JB
2863{
2864 struct drm_device *dev = crtc->dev;
6b8e6ed0 2865 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2867 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2868 struct drm_framebuffer *old_fb = crtc->primary->fb;
2869 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2870 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2871 int ret;
79e53945 2872
7d5e3799
CW
2873 if (intel_crtc_has_pending_flip(crtc)) {
2874 DRM_ERROR("pipe is still busy with an old pageflip\n");
2875 return -EBUSY;
2876 }
2877
79e53945 2878 /* no fb bound */
94352cf9 2879 if (!fb) {
a5071c2f 2880 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2881 return 0;
2882 }
2883
7eb552ae 2884 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2885 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2886 plane_name(intel_crtc->plane),
2887 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2888 return -EINVAL;
79e53945
JB
2889 }
2890
5c3b82e2 2891 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2892 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2893 if (ret == 0)
91565c85 2894 i915_gem_track_fb(old_obj, obj,
a071fa00 2895 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2896 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2897 if (ret != 0) {
a5071c2f 2898 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2899 return ret;
2900 }
79e53945 2901
e30e8f75 2902 intel_update_pipe_size(intel_crtc);
4d6a3e63 2903
29b9bde6 2904 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2905
f99d7069
DV
2906 if (intel_crtc->active)
2907 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2908
f4510a27 2909 crtc->primary->fb = fb;
6c4c86f5
DV
2910 crtc->x = x;
2911 crtc->y = y;
94352cf9 2912
b7f1de28 2913 if (old_fb) {
d7697eea
DV
2914 if (intel_crtc->active && old_fb != fb)
2915 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2916 mutex_lock(&dev->struct_mutex);
2ff8fde1 2917 intel_unpin_fb_obj(old_obj);
8ac36ec1 2918 mutex_unlock(&dev->struct_mutex);
b7f1de28 2919 }
652c393a 2920
8ac36ec1 2921 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2922 intel_update_fbc(dev);
5c3b82e2 2923 mutex_unlock(&dev->struct_mutex);
79e53945 2924
5c3b82e2 2925 return 0;
79e53945
JB
2926}
2927
5e84e1a4
ZW
2928static void intel_fdi_normal_train(struct drm_crtc *crtc)
2929{
2930 struct drm_device *dev = crtc->dev;
2931 struct drm_i915_private *dev_priv = dev->dev_private;
2932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2933 int pipe = intel_crtc->pipe;
2934 u32 reg, temp;
2935
2936 /* enable normal train */
2937 reg = FDI_TX_CTL(pipe);
2938 temp = I915_READ(reg);
61e499bf 2939 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2940 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2941 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2942 } else {
2943 temp &= ~FDI_LINK_TRAIN_NONE;
2944 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2945 }
5e84e1a4
ZW
2946 I915_WRITE(reg, temp);
2947
2948 reg = FDI_RX_CTL(pipe);
2949 temp = I915_READ(reg);
2950 if (HAS_PCH_CPT(dev)) {
2951 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2952 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2953 } else {
2954 temp &= ~FDI_LINK_TRAIN_NONE;
2955 temp |= FDI_LINK_TRAIN_NONE;
2956 }
2957 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2958
2959 /* wait one idle pattern time */
2960 POSTING_READ(reg);
2961 udelay(1000);
357555c0
JB
2962
2963 /* IVB wants error correction enabled */
2964 if (IS_IVYBRIDGE(dev))
2965 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2966 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2967}
2968
1fbc0d78 2969static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2970{
1fbc0d78
DV
2971 return crtc->base.enabled && crtc->active &&
2972 crtc->config.has_pch_encoder;
1e833f40
DV
2973}
2974
01a415fd
DV
2975static void ivb_modeset_global_resources(struct drm_device *dev)
2976{
2977 struct drm_i915_private *dev_priv = dev->dev_private;
2978 struct intel_crtc *pipe_B_crtc =
2979 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2980 struct intel_crtc *pipe_C_crtc =
2981 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2982 uint32_t temp;
2983
1e833f40
DV
2984 /*
2985 * When everything is off disable fdi C so that we could enable fdi B
2986 * with all lanes. Note that we don't care about enabled pipes without
2987 * an enabled pch encoder.
2988 */
2989 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2990 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2991 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2992 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2993
2994 temp = I915_READ(SOUTH_CHICKEN1);
2995 temp &= ~FDI_BC_BIFURCATION_SELECT;
2996 DRM_DEBUG_KMS("disabling fdi C rx\n");
2997 I915_WRITE(SOUTH_CHICKEN1, temp);
2998 }
2999}
3000
8db9d77b
ZW
3001/* The FDI link training functions for ILK/Ibexpeak. */
3002static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3003{
3004 struct drm_device *dev = crtc->dev;
3005 struct drm_i915_private *dev_priv = dev->dev_private;
3006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3007 int pipe = intel_crtc->pipe;
5eddb70b 3008 u32 reg, temp, tries;
8db9d77b 3009
1c8562f6 3010 /* FDI needs bits from pipe first */
0fc932b8 3011 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3012
e1a44743
AJ
3013 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3014 for train result */
5eddb70b
CW
3015 reg = FDI_RX_IMR(pipe);
3016 temp = I915_READ(reg);
e1a44743
AJ
3017 temp &= ~FDI_RX_SYMBOL_LOCK;
3018 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3019 I915_WRITE(reg, temp);
3020 I915_READ(reg);
e1a44743
AJ
3021 udelay(150);
3022
8db9d77b 3023 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3024 reg = FDI_TX_CTL(pipe);
3025 temp = I915_READ(reg);
627eb5a3
DV
3026 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3027 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3028 temp &= ~FDI_LINK_TRAIN_NONE;
3029 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3030 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3031
5eddb70b
CW
3032 reg = FDI_RX_CTL(pipe);
3033 temp = I915_READ(reg);
8db9d77b
ZW
3034 temp &= ~FDI_LINK_TRAIN_NONE;
3035 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3036 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3037
3038 POSTING_READ(reg);
8db9d77b
ZW
3039 udelay(150);
3040
5b2adf89 3041 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3042 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3043 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3044 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3045
5eddb70b 3046 reg = FDI_RX_IIR(pipe);
e1a44743 3047 for (tries = 0; tries < 5; tries++) {
5eddb70b 3048 temp = I915_READ(reg);
8db9d77b
ZW
3049 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3050
3051 if ((temp & FDI_RX_BIT_LOCK)) {
3052 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3053 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3054 break;
3055 }
8db9d77b 3056 }
e1a44743 3057 if (tries == 5)
5eddb70b 3058 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3059
3060 /* Train 2 */
5eddb70b
CW
3061 reg = FDI_TX_CTL(pipe);
3062 temp = I915_READ(reg);
8db9d77b
ZW
3063 temp &= ~FDI_LINK_TRAIN_NONE;
3064 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3065 I915_WRITE(reg, temp);
8db9d77b 3066
5eddb70b
CW
3067 reg = FDI_RX_CTL(pipe);
3068 temp = I915_READ(reg);
8db9d77b
ZW
3069 temp &= ~FDI_LINK_TRAIN_NONE;
3070 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3071 I915_WRITE(reg, temp);
8db9d77b 3072
5eddb70b
CW
3073 POSTING_READ(reg);
3074 udelay(150);
8db9d77b 3075
5eddb70b 3076 reg = FDI_RX_IIR(pipe);
e1a44743 3077 for (tries = 0; tries < 5; tries++) {
5eddb70b 3078 temp = I915_READ(reg);
8db9d77b
ZW
3079 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3080
3081 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3082 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3083 DRM_DEBUG_KMS("FDI train 2 done.\n");
3084 break;
3085 }
8db9d77b 3086 }
e1a44743 3087 if (tries == 5)
5eddb70b 3088 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3089
3090 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3091
8db9d77b
ZW
3092}
3093
0206e353 3094static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3095 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3096 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3097 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3098 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3099};
3100
3101/* The FDI link training functions for SNB/Cougarpoint. */
3102static void gen6_fdi_link_train(struct drm_crtc *crtc)
3103{
3104 struct drm_device *dev = crtc->dev;
3105 struct drm_i915_private *dev_priv = dev->dev_private;
3106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3107 int pipe = intel_crtc->pipe;
fa37d39e 3108 u32 reg, temp, i, retry;
8db9d77b 3109
e1a44743
AJ
3110 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3111 for train result */
5eddb70b
CW
3112 reg = FDI_RX_IMR(pipe);
3113 temp = I915_READ(reg);
e1a44743
AJ
3114 temp &= ~FDI_RX_SYMBOL_LOCK;
3115 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3116 I915_WRITE(reg, temp);
3117
3118 POSTING_READ(reg);
e1a44743
AJ
3119 udelay(150);
3120
8db9d77b 3121 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3122 reg = FDI_TX_CTL(pipe);
3123 temp = I915_READ(reg);
627eb5a3
DV
3124 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3125 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3126 temp &= ~FDI_LINK_TRAIN_NONE;
3127 temp |= FDI_LINK_TRAIN_PATTERN_1;
3128 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3129 /* SNB-B */
3130 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3131 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3132
d74cf324
DV
3133 I915_WRITE(FDI_RX_MISC(pipe),
3134 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3135
5eddb70b
CW
3136 reg = FDI_RX_CTL(pipe);
3137 temp = I915_READ(reg);
8db9d77b
ZW
3138 if (HAS_PCH_CPT(dev)) {
3139 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3140 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3141 } else {
3142 temp &= ~FDI_LINK_TRAIN_NONE;
3143 temp |= FDI_LINK_TRAIN_PATTERN_1;
3144 }
5eddb70b
CW
3145 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3146
3147 POSTING_READ(reg);
8db9d77b
ZW
3148 udelay(150);
3149
0206e353 3150 for (i = 0; i < 4; i++) {
5eddb70b
CW
3151 reg = FDI_TX_CTL(pipe);
3152 temp = I915_READ(reg);
8db9d77b
ZW
3153 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3154 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3155 I915_WRITE(reg, temp);
3156
3157 POSTING_READ(reg);
8db9d77b
ZW
3158 udelay(500);
3159
fa37d39e
SP
3160 for (retry = 0; retry < 5; retry++) {
3161 reg = FDI_RX_IIR(pipe);
3162 temp = I915_READ(reg);
3163 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3164 if (temp & FDI_RX_BIT_LOCK) {
3165 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3166 DRM_DEBUG_KMS("FDI train 1 done.\n");
3167 break;
3168 }
3169 udelay(50);
8db9d77b 3170 }
fa37d39e
SP
3171 if (retry < 5)
3172 break;
8db9d77b
ZW
3173 }
3174 if (i == 4)
5eddb70b 3175 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3176
3177 /* Train 2 */
5eddb70b
CW
3178 reg = FDI_TX_CTL(pipe);
3179 temp = I915_READ(reg);
8db9d77b
ZW
3180 temp &= ~FDI_LINK_TRAIN_NONE;
3181 temp |= FDI_LINK_TRAIN_PATTERN_2;
3182 if (IS_GEN6(dev)) {
3183 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3184 /* SNB-B */
3185 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3186 }
5eddb70b 3187 I915_WRITE(reg, temp);
8db9d77b 3188
5eddb70b
CW
3189 reg = FDI_RX_CTL(pipe);
3190 temp = I915_READ(reg);
8db9d77b
ZW
3191 if (HAS_PCH_CPT(dev)) {
3192 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3193 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3194 } else {
3195 temp &= ~FDI_LINK_TRAIN_NONE;
3196 temp |= FDI_LINK_TRAIN_PATTERN_2;
3197 }
5eddb70b
CW
3198 I915_WRITE(reg, temp);
3199
3200 POSTING_READ(reg);
8db9d77b
ZW
3201 udelay(150);
3202
0206e353 3203 for (i = 0; i < 4; i++) {
5eddb70b
CW
3204 reg = FDI_TX_CTL(pipe);
3205 temp = I915_READ(reg);
8db9d77b
ZW
3206 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3207 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3208 I915_WRITE(reg, temp);
3209
3210 POSTING_READ(reg);
8db9d77b
ZW
3211 udelay(500);
3212
fa37d39e
SP
3213 for (retry = 0; retry < 5; retry++) {
3214 reg = FDI_RX_IIR(pipe);
3215 temp = I915_READ(reg);
3216 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3217 if (temp & FDI_RX_SYMBOL_LOCK) {
3218 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3219 DRM_DEBUG_KMS("FDI train 2 done.\n");
3220 break;
3221 }
3222 udelay(50);
8db9d77b 3223 }
fa37d39e
SP
3224 if (retry < 5)
3225 break;
8db9d77b
ZW
3226 }
3227 if (i == 4)
5eddb70b 3228 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3229
3230 DRM_DEBUG_KMS("FDI train done.\n");
3231}
3232
357555c0
JB
3233/* Manual link training for Ivy Bridge A0 parts */
3234static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3235{
3236 struct drm_device *dev = crtc->dev;
3237 struct drm_i915_private *dev_priv = dev->dev_private;
3238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3239 int pipe = intel_crtc->pipe;
139ccd3f 3240 u32 reg, temp, i, j;
357555c0
JB
3241
3242 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3243 for train result */
3244 reg = FDI_RX_IMR(pipe);
3245 temp = I915_READ(reg);
3246 temp &= ~FDI_RX_SYMBOL_LOCK;
3247 temp &= ~FDI_RX_BIT_LOCK;
3248 I915_WRITE(reg, temp);
3249
3250 POSTING_READ(reg);
3251 udelay(150);
3252
01a415fd
DV
3253 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3254 I915_READ(FDI_RX_IIR(pipe)));
3255
139ccd3f
JB
3256 /* Try each vswing and preemphasis setting twice before moving on */
3257 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3258 /* disable first in case we need to retry */
3259 reg = FDI_TX_CTL(pipe);
3260 temp = I915_READ(reg);
3261 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3262 temp &= ~FDI_TX_ENABLE;
3263 I915_WRITE(reg, temp);
357555c0 3264
139ccd3f
JB
3265 reg = FDI_RX_CTL(pipe);
3266 temp = I915_READ(reg);
3267 temp &= ~FDI_LINK_TRAIN_AUTO;
3268 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3269 temp &= ~FDI_RX_ENABLE;
3270 I915_WRITE(reg, temp);
357555c0 3271
139ccd3f 3272 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3273 reg = FDI_TX_CTL(pipe);
3274 temp = I915_READ(reg);
139ccd3f
JB
3275 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3276 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3277 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3278 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3279 temp |= snb_b_fdi_train_param[j/2];
3280 temp |= FDI_COMPOSITE_SYNC;
3281 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3282
139ccd3f
JB
3283 I915_WRITE(FDI_RX_MISC(pipe),
3284 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3285
139ccd3f 3286 reg = FDI_RX_CTL(pipe);
357555c0 3287 temp = I915_READ(reg);
139ccd3f
JB
3288 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3289 temp |= FDI_COMPOSITE_SYNC;
3290 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3291
139ccd3f
JB
3292 POSTING_READ(reg);
3293 udelay(1); /* should be 0.5us */
357555c0 3294
139ccd3f
JB
3295 for (i = 0; i < 4; i++) {
3296 reg = FDI_RX_IIR(pipe);
3297 temp = I915_READ(reg);
3298 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3299
139ccd3f
JB
3300 if (temp & FDI_RX_BIT_LOCK ||
3301 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3302 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3303 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3304 i);
3305 break;
3306 }
3307 udelay(1); /* should be 0.5us */
3308 }
3309 if (i == 4) {
3310 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3311 continue;
3312 }
357555c0 3313
139ccd3f 3314 /* Train 2 */
357555c0
JB
3315 reg = FDI_TX_CTL(pipe);
3316 temp = I915_READ(reg);
139ccd3f
JB
3317 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3318 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3319 I915_WRITE(reg, temp);
3320
3321 reg = FDI_RX_CTL(pipe);
3322 temp = I915_READ(reg);
3323 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3324 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3325 I915_WRITE(reg, temp);
3326
3327 POSTING_READ(reg);
139ccd3f 3328 udelay(2); /* should be 1.5us */
357555c0 3329
139ccd3f
JB
3330 for (i = 0; i < 4; i++) {
3331 reg = FDI_RX_IIR(pipe);
3332 temp = I915_READ(reg);
3333 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3334
139ccd3f
JB
3335 if (temp & FDI_RX_SYMBOL_LOCK ||
3336 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3337 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3338 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3339 i);
3340 goto train_done;
3341 }
3342 udelay(2); /* should be 1.5us */
357555c0 3343 }
139ccd3f
JB
3344 if (i == 4)
3345 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3346 }
357555c0 3347
139ccd3f 3348train_done:
357555c0
JB
3349 DRM_DEBUG_KMS("FDI train done.\n");
3350}
3351
88cefb6c 3352static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3353{
88cefb6c 3354 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3355 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3356 int pipe = intel_crtc->pipe;
5eddb70b 3357 u32 reg, temp;
79e53945 3358
c64e311e 3359
c98e9dcf 3360 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3361 reg = FDI_RX_CTL(pipe);
3362 temp = I915_READ(reg);
627eb5a3
DV
3363 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3364 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3365 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3366 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3367
3368 POSTING_READ(reg);
c98e9dcf
JB
3369 udelay(200);
3370
3371 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3372 temp = I915_READ(reg);
3373 I915_WRITE(reg, temp | FDI_PCDCLK);
3374
3375 POSTING_READ(reg);
c98e9dcf
JB
3376 udelay(200);
3377
20749730
PZ
3378 /* Enable CPU FDI TX PLL, always on for Ironlake */
3379 reg = FDI_TX_CTL(pipe);
3380 temp = I915_READ(reg);
3381 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3382 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3383
20749730
PZ
3384 POSTING_READ(reg);
3385 udelay(100);
6be4a607 3386 }
0e23b99d
JB
3387}
3388
88cefb6c
DV
3389static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3390{
3391 struct drm_device *dev = intel_crtc->base.dev;
3392 struct drm_i915_private *dev_priv = dev->dev_private;
3393 int pipe = intel_crtc->pipe;
3394 u32 reg, temp;
3395
3396 /* Switch from PCDclk to Rawclk */
3397 reg = FDI_RX_CTL(pipe);
3398 temp = I915_READ(reg);
3399 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3400
3401 /* Disable CPU FDI TX PLL */
3402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
3404 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3405
3406 POSTING_READ(reg);
3407 udelay(100);
3408
3409 reg = FDI_RX_CTL(pipe);
3410 temp = I915_READ(reg);
3411 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3412
3413 /* Wait for the clocks to turn off. */
3414 POSTING_READ(reg);
3415 udelay(100);
3416}
3417
0fc932b8
JB
3418static void ironlake_fdi_disable(struct drm_crtc *crtc)
3419{
3420 struct drm_device *dev = crtc->dev;
3421 struct drm_i915_private *dev_priv = dev->dev_private;
3422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3423 int pipe = intel_crtc->pipe;
3424 u32 reg, temp;
3425
3426 /* disable CPU FDI tx and PCH FDI rx */
3427 reg = FDI_TX_CTL(pipe);
3428 temp = I915_READ(reg);
3429 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3430 POSTING_READ(reg);
3431
3432 reg = FDI_RX_CTL(pipe);
3433 temp = I915_READ(reg);
3434 temp &= ~(0x7 << 16);
dfd07d72 3435 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3436 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3437
3438 POSTING_READ(reg);
3439 udelay(100);
3440
3441 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3442 if (HAS_PCH_IBX(dev))
6f06ce18 3443 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3444
3445 /* still set train pattern 1 */
3446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
3448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_1;
3450 I915_WRITE(reg, temp);
3451
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
3454 if (HAS_PCH_CPT(dev)) {
3455 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3457 } else {
3458 temp &= ~FDI_LINK_TRAIN_NONE;
3459 temp |= FDI_LINK_TRAIN_PATTERN_1;
3460 }
3461 /* BPC in FDI rx is consistent with that in PIPECONF */
3462 temp &= ~(0x07 << 16);
dfd07d72 3463 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3464 I915_WRITE(reg, temp);
3465
3466 POSTING_READ(reg);
3467 udelay(100);
3468}
3469
5dce5b93
CW
3470bool intel_has_pending_fb_unpin(struct drm_device *dev)
3471{
3472 struct intel_crtc *crtc;
3473
3474 /* Note that we don't need to be called with mode_config.lock here
3475 * as our list of CRTC objects is static for the lifetime of the
3476 * device and so cannot disappear as we iterate. Similarly, we can
3477 * happily treat the predicates as racy, atomic checks as userspace
3478 * cannot claim and pin a new fb without at least acquring the
3479 * struct_mutex and so serialising with us.
3480 */
d3fcc808 3481 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3482 if (atomic_read(&crtc->unpin_work_count) == 0)
3483 continue;
3484
3485 if (crtc->unpin_work)
3486 intel_wait_for_vblank(dev, crtc->pipe);
3487
3488 return true;
3489 }
3490
3491 return false;
3492}
3493
d6bbafa1
CW
3494static void page_flip_completed(struct intel_crtc *intel_crtc)
3495{
3496 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3497 struct intel_unpin_work *work = intel_crtc->unpin_work;
3498
3499 /* ensure that the unpin work is consistent wrt ->pending. */
3500 smp_rmb();
3501 intel_crtc->unpin_work = NULL;
3502
3503 if (work->event)
3504 drm_send_vblank_event(intel_crtc->base.dev,
3505 intel_crtc->pipe,
3506 work->event);
3507
3508 drm_crtc_vblank_put(&intel_crtc->base);
3509
3510 wake_up_all(&dev_priv->pending_flip_queue);
3511 queue_work(dev_priv->wq, &work->work);
3512
3513 trace_i915_flip_complete(intel_crtc->plane,
3514 work->pending_flip_obj);
3515}
3516
46a55d30 3517void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3518{
0f91128d 3519 struct drm_device *dev = crtc->dev;
5bb61643 3520 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3521
2c10d571 3522 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3523 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3524 !intel_crtc_has_pending_flip(crtc),
3525 60*HZ) == 0)) {
3526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3527
5e2d7afc 3528 spin_lock_irq(&dev->event_lock);
9c787942
CW
3529 if (intel_crtc->unpin_work) {
3530 WARN_ONCE(1, "Removing stuck page flip\n");
3531 page_flip_completed(intel_crtc);
3532 }
5e2d7afc 3533 spin_unlock_irq(&dev->event_lock);
9c787942 3534 }
5bb61643 3535
975d568a
CW
3536 if (crtc->primary->fb) {
3537 mutex_lock(&dev->struct_mutex);
3538 intel_finish_fb(crtc->primary->fb);
3539 mutex_unlock(&dev->struct_mutex);
3540 }
e6c3a2a6
CW
3541}
3542
e615efe4
ED
3543/* Program iCLKIP clock to the desired frequency */
3544static void lpt_program_iclkip(struct drm_crtc *crtc)
3545{
3546 struct drm_device *dev = crtc->dev;
3547 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3548 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3549 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3550 u32 temp;
3551
09153000
DV
3552 mutex_lock(&dev_priv->dpio_lock);
3553
e615efe4
ED
3554 /* It is necessary to ungate the pixclk gate prior to programming
3555 * the divisors, and gate it back when it is done.
3556 */
3557 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3558
3559 /* Disable SSCCTL */
3560 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3561 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3562 SBI_SSCCTL_DISABLE,
3563 SBI_ICLK);
e615efe4
ED
3564
3565 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3566 if (clock == 20000) {
e615efe4
ED
3567 auxdiv = 1;
3568 divsel = 0x41;
3569 phaseinc = 0x20;
3570 } else {
3571 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3572 * but the adjusted_mode->crtc_clock in in KHz. To get the
3573 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3574 * convert the virtual clock precision to KHz here for higher
3575 * precision.
3576 */
3577 u32 iclk_virtual_root_freq = 172800 * 1000;
3578 u32 iclk_pi_range = 64;
3579 u32 desired_divisor, msb_divisor_value, pi_value;
3580
12d7ceed 3581 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3582 msb_divisor_value = desired_divisor / iclk_pi_range;
3583 pi_value = desired_divisor % iclk_pi_range;
3584
3585 auxdiv = 0;
3586 divsel = msb_divisor_value - 2;
3587 phaseinc = pi_value;
3588 }
3589
3590 /* This should not happen with any sane values */
3591 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3592 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3593 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3594 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3595
3596 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3597 clock,
e615efe4
ED
3598 auxdiv,
3599 divsel,
3600 phasedir,
3601 phaseinc);
3602
3603 /* Program SSCDIVINTPHASE6 */
988d6ee8 3604 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3605 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3606 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3607 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3608 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3609 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3610 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3611 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3612
3613 /* Program SSCAUXDIV */
988d6ee8 3614 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3615 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3616 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3617 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3618
3619 /* Enable modulator and associated divider */
988d6ee8 3620 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3621 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3622 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3623
3624 /* Wait for initialization time */
3625 udelay(24);
3626
3627 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3628
3629 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3630}
3631
275f01b2
DV
3632static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3633 enum pipe pch_transcoder)
3634{
3635 struct drm_device *dev = crtc->base.dev;
3636 struct drm_i915_private *dev_priv = dev->dev_private;
3637 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3638
3639 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3640 I915_READ(HTOTAL(cpu_transcoder)));
3641 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3642 I915_READ(HBLANK(cpu_transcoder)));
3643 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3644 I915_READ(HSYNC(cpu_transcoder)));
3645
3646 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3647 I915_READ(VTOTAL(cpu_transcoder)));
3648 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3649 I915_READ(VBLANK(cpu_transcoder)));
3650 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3651 I915_READ(VSYNC(cpu_transcoder)));
3652 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3653 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3654}
3655
1fbc0d78
DV
3656static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3657{
3658 struct drm_i915_private *dev_priv = dev->dev_private;
3659 uint32_t temp;
3660
3661 temp = I915_READ(SOUTH_CHICKEN1);
3662 if (temp & FDI_BC_BIFURCATION_SELECT)
3663 return;
3664
3665 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3666 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3667
3668 temp |= FDI_BC_BIFURCATION_SELECT;
3669 DRM_DEBUG_KMS("enabling fdi C rx\n");
3670 I915_WRITE(SOUTH_CHICKEN1, temp);
3671 POSTING_READ(SOUTH_CHICKEN1);
3672}
3673
3674static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3675{
3676 struct drm_device *dev = intel_crtc->base.dev;
3677 struct drm_i915_private *dev_priv = dev->dev_private;
3678
3679 switch (intel_crtc->pipe) {
3680 case PIPE_A:
3681 break;
3682 case PIPE_B:
3683 if (intel_crtc->config.fdi_lanes > 2)
3684 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3685 else
3686 cpt_enable_fdi_bc_bifurcation(dev);
3687
3688 break;
3689 case PIPE_C:
3690 cpt_enable_fdi_bc_bifurcation(dev);
3691
3692 break;
3693 default:
3694 BUG();
3695 }
3696}
3697
f67a559d
JB
3698/*
3699 * Enable PCH resources required for PCH ports:
3700 * - PCH PLLs
3701 * - FDI training & RX/TX
3702 * - update transcoder timings
3703 * - DP transcoding bits
3704 * - transcoder
3705 */
3706static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3707{
3708 struct drm_device *dev = crtc->dev;
3709 struct drm_i915_private *dev_priv = dev->dev_private;
3710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3711 int pipe = intel_crtc->pipe;
ee7b9f93 3712 u32 reg, temp;
2c07245f 3713
ab9412ba 3714 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3715
1fbc0d78
DV
3716 if (IS_IVYBRIDGE(dev))
3717 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3718
cd986abb
DV
3719 /* Write the TU size bits before fdi link training, so that error
3720 * detection works. */
3721 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3722 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3723
c98e9dcf 3724 /* For PCH output, training FDI link */
674cf967 3725 dev_priv->display.fdi_link_train(crtc);
2c07245f 3726
3ad8a208
DV
3727 /* We need to program the right clock selection before writing the pixel
3728 * mutliplier into the DPLL. */
303b81e0 3729 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3730 u32 sel;
4b645f14 3731
c98e9dcf 3732 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3733 temp |= TRANS_DPLL_ENABLE(pipe);
3734 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3735 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3736 temp |= sel;
3737 else
3738 temp &= ~sel;
c98e9dcf 3739 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3740 }
5eddb70b 3741
3ad8a208
DV
3742 /* XXX: pch pll's can be enabled any time before we enable the PCH
3743 * transcoder, and we actually should do this to not upset any PCH
3744 * transcoder that already use the clock when we share it.
3745 *
3746 * Note that enable_shared_dpll tries to do the right thing, but
3747 * get_shared_dpll unconditionally resets the pll - we need that to have
3748 * the right LVDS enable sequence. */
85b3894f 3749 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3750
d9b6cb56
JB
3751 /* set transcoder timing, panel must allow it */
3752 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3753 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3754
303b81e0 3755 intel_fdi_normal_train(crtc);
5e84e1a4 3756
c98e9dcf
JB
3757 /* For PCH DP, enable TRANS_DP_CTL */
3758 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3759 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3760 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3761 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3762 reg = TRANS_DP_CTL(pipe);
3763 temp = I915_READ(reg);
3764 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3765 TRANS_DP_SYNC_MASK |
3766 TRANS_DP_BPC_MASK);
5eddb70b
CW
3767 temp |= (TRANS_DP_OUTPUT_ENABLE |
3768 TRANS_DP_ENH_FRAMING);
9325c9f0 3769 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3770
3771 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3772 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3773 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3774 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3775
3776 switch (intel_trans_dp_port_sel(crtc)) {
3777 case PCH_DP_B:
5eddb70b 3778 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3779 break;
3780 case PCH_DP_C:
5eddb70b 3781 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3782 break;
3783 case PCH_DP_D:
5eddb70b 3784 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3785 break;
3786 default:
e95d41e1 3787 BUG();
32f9d658 3788 }
2c07245f 3789
5eddb70b 3790 I915_WRITE(reg, temp);
6be4a607 3791 }
b52eb4dc 3792
b8a4f404 3793 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3794}
3795
1507e5bd
PZ
3796static void lpt_pch_enable(struct drm_crtc *crtc)
3797{
3798 struct drm_device *dev = crtc->dev;
3799 struct drm_i915_private *dev_priv = dev->dev_private;
3800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3801 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3802
ab9412ba 3803 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3804
8c52b5e8 3805 lpt_program_iclkip(crtc);
1507e5bd 3806
0540e488 3807 /* Set transcoder timing. */
275f01b2 3808 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3809
937bb610 3810 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3811}
3812
716c2e55 3813void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3814{
e2b78267 3815 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3816
3817 if (pll == NULL)
3818 return;
3819
3820 if (pll->refcount == 0) {
46edb027 3821 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3822 return;
3823 }
3824
f4a091c7
DV
3825 if (--pll->refcount == 0) {
3826 WARN_ON(pll->on);
3827 WARN_ON(pll->active);
3828 }
3829
a43f6e0f 3830 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3831}
3832
716c2e55 3833struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3834{
e2b78267
DV
3835 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3836 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3837 enum intel_dpll_id i;
ee7b9f93 3838
ee7b9f93 3839 if (pll) {
46edb027
DV
3840 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3841 crtc->base.base.id, pll->name);
e2b78267 3842 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3843 }
3844
98b6bd99
DV
3845 if (HAS_PCH_IBX(dev_priv->dev)) {
3846 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3847 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3848 pll = &dev_priv->shared_dplls[i];
98b6bd99 3849
46edb027
DV
3850 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3851 crtc->base.base.id, pll->name);
98b6bd99 3852
f2a69f44
DV
3853 WARN_ON(pll->refcount);
3854
98b6bd99
DV
3855 goto found;
3856 }
3857
e72f9fbf
DV
3858 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3859 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3860
3861 /* Only want to check enabled timings first */
3862 if (pll->refcount == 0)
3863 continue;
3864
b89a1d39
DV
3865 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3866 sizeof(pll->hw_state)) == 0) {
46edb027 3867 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3868 crtc->base.base.id,
46edb027 3869 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3870
3871 goto found;
3872 }
3873 }
3874
3875 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3876 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3877 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3878 if (pll->refcount == 0) {
46edb027
DV
3879 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3880 crtc->base.base.id, pll->name);
ee7b9f93
JB
3881 goto found;
3882 }
3883 }
3884
3885 return NULL;
3886
3887found:
f2a69f44
DV
3888 if (pll->refcount == 0)
3889 pll->hw_state = crtc->config.dpll_hw_state;
3890
a43f6e0f 3891 crtc->config.shared_dpll = i;
46edb027
DV
3892 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3893 pipe_name(crtc->pipe));
ee7b9f93 3894
cdbd2316 3895 pll->refcount++;
e04c7350 3896
ee7b9f93
JB
3897 return pll;
3898}
3899
a1520318 3900static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3901{
3902 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3903 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3904 u32 temp;
3905
3906 temp = I915_READ(dslreg);
3907 udelay(500);
3908 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3909 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3910 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3911 }
3912}
3913
b074cec8
JB
3914static void ironlake_pfit_enable(struct intel_crtc *crtc)
3915{
3916 struct drm_device *dev = crtc->base.dev;
3917 struct drm_i915_private *dev_priv = dev->dev_private;
3918 int pipe = crtc->pipe;
3919
fd4daa9c 3920 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3921 /* Force use of hard-coded filter coefficients
3922 * as some pre-programmed values are broken,
3923 * e.g. x201.
3924 */
3925 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3926 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3927 PF_PIPE_SEL_IVB(pipe));
3928 else
3929 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3930 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3931 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3932 }
3933}
3934
bb53d4ae
VS
3935static void intel_enable_planes(struct drm_crtc *crtc)
3936{
3937 struct drm_device *dev = crtc->dev;
3938 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3939 struct drm_plane *plane;
bb53d4ae
VS
3940 struct intel_plane *intel_plane;
3941
af2b653b
MR
3942 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3943 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3944 if (intel_plane->pipe == pipe)
3945 intel_plane_restore(&intel_plane->base);
af2b653b 3946 }
bb53d4ae
VS
3947}
3948
3949static void intel_disable_planes(struct drm_crtc *crtc)
3950{
3951 struct drm_device *dev = crtc->dev;
3952 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3953 struct drm_plane *plane;
bb53d4ae
VS
3954 struct intel_plane *intel_plane;
3955
af2b653b
MR
3956 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3957 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3958 if (intel_plane->pipe == pipe)
3959 intel_plane_disable(&intel_plane->base);
af2b653b 3960 }
bb53d4ae
VS
3961}
3962
20bc8673 3963void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3964{
cea165c3
VS
3965 struct drm_device *dev = crtc->base.dev;
3966 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3967
3968 if (!crtc->config.ips_enabled)
3969 return;
3970
cea165c3
VS
3971 /* We can only enable IPS after we enable a plane and wait for a vblank */
3972 intel_wait_for_vblank(dev, crtc->pipe);
3973
d77e4531 3974 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3975 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3976 mutex_lock(&dev_priv->rps.hw_lock);
3977 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3978 mutex_unlock(&dev_priv->rps.hw_lock);
3979 /* Quoting Art Runyan: "its not safe to expect any particular
3980 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3981 * mailbox." Moreover, the mailbox may return a bogus state,
3982 * so we need to just enable it and continue on.
2a114cc1
BW
3983 */
3984 } else {
3985 I915_WRITE(IPS_CTL, IPS_ENABLE);
3986 /* The bit only becomes 1 in the next vblank, so this wait here
3987 * is essentially intel_wait_for_vblank. If we don't have this
3988 * and don't wait for vblanks until the end of crtc_enable, then
3989 * the HW state readout code will complain that the expected
3990 * IPS_CTL value is not the one we read. */
3991 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3992 DRM_ERROR("Timed out waiting for IPS enable\n");
3993 }
d77e4531
PZ
3994}
3995
20bc8673 3996void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3997{
3998 struct drm_device *dev = crtc->base.dev;
3999 struct drm_i915_private *dev_priv = dev->dev_private;
4000
4001 if (!crtc->config.ips_enabled)
4002 return;
4003
4004 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4005 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4006 mutex_lock(&dev_priv->rps.hw_lock);
4007 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4008 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4009 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4010 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4011 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4012 } else {
2a114cc1 4013 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4014 POSTING_READ(IPS_CTL);
4015 }
d77e4531
PZ
4016
4017 /* We need to wait for a vblank before we can disable the plane. */
4018 intel_wait_for_vblank(dev, crtc->pipe);
4019}
4020
4021/** Loads the palette/gamma unit for the CRTC with the prepared values */
4022static void intel_crtc_load_lut(struct drm_crtc *crtc)
4023{
4024 struct drm_device *dev = crtc->dev;
4025 struct drm_i915_private *dev_priv = dev->dev_private;
4026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4027 enum pipe pipe = intel_crtc->pipe;
4028 int palreg = PALETTE(pipe);
4029 int i;
4030 bool reenable_ips = false;
4031
4032 /* The clocks have to be on to load the palette. */
4033 if (!crtc->enabled || !intel_crtc->active)
4034 return;
4035
4036 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
4037 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4038 assert_dsi_pll_enabled(dev_priv);
4039 else
4040 assert_pll_enabled(dev_priv, pipe);
4041 }
4042
4043 /* use legacy palette for Ironlake */
7a1db49a 4044 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4045 palreg = LGC_PALETTE(pipe);
4046
4047 /* Workaround : Do not read or write the pipe palette/gamma data while
4048 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4049 */
41e6fc4c 4050 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
4051 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4052 GAMMA_MODE_MODE_SPLIT)) {
4053 hsw_disable_ips(intel_crtc);
4054 reenable_ips = true;
4055 }
4056
4057 for (i = 0; i < 256; i++) {
4058 I915_WRITE(palreg + 4 * i,
4059 (intel_crtc->lut_r[i] << 16) |
4060 (intel_crtc->lut_g[i] << 8) |
4061 intel_crtc->lut_b[i]);
4062 }
4063
4064 if (reenable_ips)
4065 hsw_enable_ips(intel_crtc);
4066}
4067
d3eedb1a
VS
4068static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4069{
4070 if (!enable && intel_crtc->overlay) {
4071 struct drm_device *dev = intel_crtc->base.dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
4073
4074 mutex_lock(&dev->struct_mutex);
4075 dev_priv->mm.interruptible = false;
4076 (void) intel_overlay_switch_off(intel_crtc->overlay);
4077 dev_priv->mm.interruptible = true;
4078 mutex_unlock(&dev->struct_mutex);
4079 }
4080
4081 /* Let userspace switch the overlay on again. In most cases userspace
4082 * has to recompute where to put it anyway.
4083 */
4084}
4085
d3eedb1a 4086static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4087{
4088 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4090 int pipe = intel_crtc->pipe;
a5c4d7bc 4091
fdd508a6 4092 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
4093 intel_enable_planes(crtc);
4094 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4095 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4096
4097 hsw_enable_ips(intel_crtc);
4098
4099 mutex_lock(&dev->struct_mutex);
4100 intel_update_fbc(dev);
4101 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4102
4103 /*
4104 * FIXME: Once we grow proper nuclear flip support out of this we need
4105 * to compute the mask of flip planes precisely. For the time being
4106 * consider this a flip from a NULL plane.
4107 */
4108 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4109}
4110
d3eedb1a 4111static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4112{
4113 struct drm_device *dev = crtc->dev;
4114 struct drm_i915_private *dev_priv = dev->dev_private;
4115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4116 int pipe = intel_crtc->pipe;
4117 int plane = intel_crtc->plane;
4118
4119 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4120
4121 if (dev_priv->fbc.plane == plane)
4122 intel_disable_fbc(dev);
4123
4124 hsw_disable_ips(intel_crtc);
4125
d3eedb1a 4126 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
4127 intel_crtc_update_cursor(crtc, false);
4128 intel_disable_planes(crtc);
fdd508a6 4129 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4130
f99d7069
DV
4131 /*
4132 * FIXME: Once we grow proper nuclear flip support out of this we need
4133 * to compute the mask of flip planes precisely. For the time being
4134 * consider this a flip to a NULL plane.
4135 */
4136 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4137}
4138
f67a559d
JB
4139static void ironlake_crtc_enable(struct drm_crtc *crtc)
4140{
4141 struct drm_device *dev = crtc->dev;
4142 struct drm_i915_private *dev_priv = dev->dev_private;
4143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4144 struct intel_encoder *encoder;
f67a559d 4145 int pipe = intel_crtc->pipe;
f67a559d 4146
08a48469
DV
4147 WARN_ON(!crtc->enabled);
4148
f67a559d
JB
4149 if (intel_crtc->active)
4150 return;
4151
b14b1055
DV
4152 if (intel_crtc->config.has_pch_encoder)
4153 intel_prepare_shared_dpll(intel_crtc);
4154
29407aab
DV
4155 if (intel_crtc->config.has_dp_encoder)
4156 intel_dp_set_m_n(intel_crtc);
4157
4158 intel_set_pipe_timings(intel_crtc);
4159
4160 if (intel_crtc->config.has_pch_encoder) {
4161 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4162 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4163 }
4164
4165 ironlake_set_pipeconf(crtc);
4166
f67a559d 4167 intel_crtc->active = true;
8664281b
PZ
4168
4169 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4170 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4171
f6736a1a 4172 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4173 if (encoder->pre_enable)
4174 encoder->pre_enable(encoder);
f67a559d 4175
5bfe2ac0 4176 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4177 /* Note: FDI PLL enabling _must_ be done before we enable the
4178 * cpu pipes, hence this is separate from all the other fdi/pch
4179 * enabling. */
88cefb6c 4180 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4181 } else {
4182 assert_fdi_tx_disabled(dev_priv, pipe);
4183 assert_fdi_rx_disabled(dev_priv, pipe);
4184 }
f67a559d 4185
b074cec8 4186 ironlake_pfit_enable(intel_crtc);
f67a559d 4187
9c54c0dd
JB
4188 /*
4189 * On ILK+ LUT must be loaded before the pipe is running but with
4190 * clocks enabled
4191 */
4192 intel_crtc_load_lut(crtc);
4193
f37fcc2a 4194 intel_update_watermarks(crtc);
e1fdc473 4195 intel_enable_pipe(intel_crtc);
f67a559d 4196
5bfe2ac0 4197 if (intel_crtc->config.has_pch_encoder)
f67a559d 4198 ironlake_pch_enable(crtc);
c98e9dcf 4199
fa5c73b1
DV
4200 for_each_encoder_on_crtc(dev, crtc, encoder)
4201 encoder->enable(encoder);
61b77ddd
DV
4202
4203 if (HAS_PCH_CPT(dev))
a1520318 4204 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4205
4b3a9526
VS
4206 assert_vblank_disabled(crtc);
4207 drm_crtc_vblank_on(crtc);
4208
d3eedb1a 4209 intel_crtc_enable_planes(crtc);
6be4a607
JB
4210}
4211
42db64ef
PZ
4212/* IPS only exists on ULT machines and is tied to pipe A. */
4213static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4214{
f5adf94e 4215 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4216}
4217
e4916946
PZ
4218/*
4219 * This implements the workaround described in the "notes" section of the mode
4220 * set sequence documentation. When going from no pipes or single pipe to
4221 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4222 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4223 */
4224static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4225{
4226 struct drm_device *dev = crtc->base.dev;
4227 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4228
4229 /* We want to get the other_active_crtc only if there's only 1 other
4230 * active crtc. */
d3fcc808 4231 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4232 if (!crtc_it->active || crtc_it == crtc)
4233 continue;
4234
4235 if (other_active_crtc)
4236 return;
4237
4238 other_active_crtc = crtc_it;
4239 }
4240 if (!other_active_crtc)
4241 return;
4242
4243 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4244 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4245}
4246
4f771f10
PZ
4247static void haswell_crtc_enable(struct drm_crtc *crtc)
4248{
4249 struct drm_device *dev = crtc->dev;
4250 struct drm_i915_private *dev_priv = dev->dev_private;
4251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4252 struct intel_encoder *encoder;
4253 int pipe = intel_crtc->pipe;
4f771f10
PZ
4254
4255 WARN_ON(!crtc->enabled);
4256
4257 if (intel_crtc->active)
4258 return;
4259
df8ad70c
DV
4260 if (intel_crtc_to_shared_dpll(intel_crtc))
4261 intel_enable_shared_dpll(intel_crtc);
4262
229fca97
DV
4263 if (intel_crtc->config.has_dp_encoder)
4264 intel_dp_set_m_n(intel_crtc);
4265
4266 intel_set_pipe_timings(intel_crtc);
4267
4268 if (intel_crtc->config.has_pch_encoder) {
4269 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4270 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4271 }
4272
4273 haswell_set_pipeconf(crtc);
4274
4275 intel_set_pipe_csc(crtc);
4276
4f771f10 4277 intel_crtc->active = true;
8664281b
PZ
4278
4279 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4f771f10
PZ
4280 for_each_encoder_on_crtc(dev, crtc, encoder)
4281 if (encoder->pre_enable)
4282 encoder->pre_enable(encoder);
4283
4fe9467d
ID
4284 if (intel_crtc->config.has_pch_encoder) {
4285 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4286 dev_priv->display.fdi_link_train(crtc);
4287 }
4288
1f544388 4289 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4290
b074cec8 4291 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4292
4293 /*
4294 * On ILK+ LUT must be loaded before the pipe is running but with
4295 * clocks enabled
4296 */
4297 intel_crtc_load_lut(crtc);
4298
1f544388 4299 intel_ddi_set_pipe_settings(crtc);
8228c251 4300 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4301
f37fcc2a 4302 intel_update_watermarks(crtc);
e1fdc473 4303 intel_enable_pipe(intel_crtc);
42db64ef 4304
5bfe2ac0 4305 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4306 lpt_pch_enable(crtc);
4f771f10 4307
0e32b39c
DA
4308 if (intel_crtc->config.dp_encoder_is_mst)
4309 intel_ddi_set_vc_payload_alloc(crtc, true);
4310
8807e55b 4311 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4312 encoder->enable(encoder);
8807e55b
JN
4313 intel_opregion_notify_encoder(encoder, true);
4314 }
4f771f10 4315
4b3a9526
VS
4316 assert_vblank_disabled(crtc);
4317 drm_crtc_vblank_on(crtc);
4318
e4916946
PZ
4319 /* If we change the relative order between pipe/planes enabling, we need
4320 * to change the workaround. */
4321 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4322 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4323}
4324
3f8dce3a
DV
4325static void ironlake_pfit_disable(struct intel_crtc *crtc)
4326{
4327 struct drm_device *dev = crtc->base.dev;
4328 struct drm_i915_private *dev_priv = dev->dev_private;
4329 int pipe = crtc->pipe;
4330
4331 /* To avoid upsetting the power well on haswell only disable the pfit if
4332 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4333 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4334 I915_WRITE(PF_CTL(pipe), 0);
4335 I915_WRITE(PF_WIN_POS(pipe), 0);
4336 I915_WRITE(PF_WIN_SZ(pipe), 0);
4337 }
4338}
4339
6be4a607
JB
4340static void ironlake_crtc_disable(struct drm_crtc *crtc)
4341{
4342 struct drm_device *dev = crtc->dev;
4343 struct drm_i915_private *dev_priv = dev->dev_private;
4344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4345 struct intel_encoder *encoder;
6be4a607 4346 int pipe = intel_crtc->pipe;
5eddb70b 4347 u32 reg, temp;
b52eb4dc 4348
f7abfe8b
CW
4349 if (!intel_crtc->active)
4350 return;
4351
d3eedb1a 4352 intel_crtc_disable_planes(crtc);
a5c4d7bc 4353
4b3a9526
VS
4354 drm_crtc_vblank_off(crtc);
4355 assert_vblank_disabled(crtc);
4356
ea9d758d
DV
4357 for_each_encoder_on_crtc(dev, crtc, encoder)
4358 encoder->disable(encoder);
4359
d925c59a
DV
4360 if (intel_crtc->config.has_pch_encoder)
4361 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4362
575f7ab7 4363 intel_disable_pipe(intel_crtc);
32f9d658 4364
3f8dce3a 4365 ironlake_pfit_disable(intel_crtc);
2c07245f 4366
bf49ec8c
DV
4367 for_each_encoder_on_crtc(dev, crtc, encoder)
4368 if (encoder->post_disable)
4369 encoder->post_disable(encoder);
2c07245f 4370
d925c59a
DV
4371 if (intel_crtc->config.has_pch_encoder) {
4372 ironlake_fdi_disable(crtc);
913d8d11 4373
d925c59a
DV
4374 ironlake_disable_pch_transcoder(dev_priv, pipe);
4375 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4376
d925c59a
DV
4377 if (HAS_PCH_CPT(dev)) {
4378 /* disable TRANS_DP_CTL */
4379 reg = TRANS_DP_CTL(pipe);
4380 temp = I915_READ(reg);
4381 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4382 TRANS_DP_PORT_SEL_MASK);
4383 temp |= TRANS_DP_PORT_SEL_NONE;
4384 I915_WRITE(reg, temp);
4385
4386 /* disable DPLL_SEL */
4387 temp = I915_READ(PCH_DPLL_SEL);
11887397 4388 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4389 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4390 }
e3421a18 4391
d925c59a 4392 /* disable PCH DPLL */
e72f9fbf 4393 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4394
d925c59a
DV
4395 ironlake_fdi_pll_disable(intel_crtc);
4396 }
6b383a7f 4397
f7abfe8b 4398 intel_crtc->active = false;
46ba614c 4399 intel_update_watermarks(crtc);
d1ebd816
BW
4400
4401 mutex_lock(&dev->struct_mutex);
6b383a7f 4402 intel_update_fbc(dev);
d1ebd816 4403 mutex_unlock(&dev->struct_mutex);
6be4a607 4404}
1b3c7a47 4405
4f771f10 4406static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4407{
4f771f10
PZ
4408 struct drm_device *dev = crtc->dev;
4409 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4411 struct intel_encoder *encoder;
3b117c8f 4412 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4413
4f771f10
PZ
4414 if (!intel_crtc->active)
4415 return;
4416
d3eedb1a 4417 intel_crtc_disable_planes(crtc);
dda9a66a 4418
4b3a9526
VS
4419 drm_crtc_vblank_off(crtc);
4420 assert_vblank_disabled(crtc);
4421
8807e55b
JN
4422 for_each_encoder_on_crtc(dev, crtc, encoder) {
4423 intel_opregion_notify_encoder(encoder, false);
4f771f10 4424 encoder->disable(encoder);
8807e55b 4425 }
4f771f10 4426
8664281b
PZ
4427 if (intel_crtc->config.has_pch_encoder)
4428 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
575f7ab7 4429 intel_disable_pipe(intel_crtc);
4f771f10 4430
a4bf214f
VS
4431 if (intel_crtc->config.dp_encoder_is_mst)
4432 intel_ddi_set_vc_payload_alloc(crtc, false);
4433
ad80a810 4434 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4435
3f8dce3a 4436 ironlake_pfit_disable(intel_crtc);
4f771f10 4437
1f544388 4438 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4439
88adfff1 4440 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4441 lpt_disable_pch_transcoder(dev_priv);
8664281b 4442 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4443 intel_ddi_fdi_disable(crtc);
83616634 4444 }
4f771f10 4445
97b040aa
ID
4446 for_each_encoder_on_crtc(dev, crtc, encoder)
4447 if (encoder->post_disable)
4448 encoder->post_disable(encoder);
4449
4f771f10 4450 intel_crtc->active = false;
46ba614c 4451 intel_update_watermarks(crtc);
4f771f10
PZ
4452
4453 mutex_lock(&dev->struct_mutex);
4454 intel_update_fbc(dev);
4455 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4456
4457 if (intel_crtc_to_shared_dpll(intel_crtc))
4458 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4459}
4460
ee7b9f93
JB
4461static void ironlake_crtc_off(struct drm_crtc *crtc)
4462{
4463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4464 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4465}
4466
6441ab5f 4467
2dd24552
JB
4468static void i9xx_pfit_enable(struct intel_crtc *crtc)
4469{
4470 struct drm_device *dev = crtc->base.dev;
4471 struct drm_i915_private *dev_priv = dev->dev_private;
4472 struct intel_crtc_config *pipe_config = &crtc->config;
4473
328d8e82 4474 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4475 return;
4476
2dd24552 4477 /*
c0b03411
DV
4478 * The panel fitter should only be adjusted whilst the pipe is disabled,
4479 * according to register description and PRM.
2dd24552 4480 */
c0b03411
DV
4481 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4482 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4483
b074cec8
JB
4484 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4485 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4486
4487 /* Border color in case we don't scale up to the full screen. Black by
4488 * default, change to something else for debugging. */
4489 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4490}
4491
d05410f9
DA
4492static enum intel_display_power_domain port_to_power_domain(enum port port)
4493{
4494 switch (port) {
4495 case PORT_A:
4496 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4497 case PORT_B:
4498 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4499 case PORT_C:
4500 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4501 case PORT_D:
4502 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4503 default:
4504 WARN_ON_ONCE(1);
4505 return POWER_DOMAIN_PORT_OTHER;
4506 }
4507}
4508
77d22dca
ID
4509#define for_each_power_domain(domain, mask) \
4510 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4511 if ((1 << (domain)) & (mask))
4512
319be8ae
ID
4513enum intel_display_power_domain
4514intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4515{
4516 struct drm_device *dev = intel_encoder->base.dev;
4517 struct intel_digital_port *intel_dig_port;
4518
4519 switch (intel_encoder->type) {
4520 case INTEL_OUTPUT_UNKNOWN:
4521 /* Only DDI platforms should ever use this output type */
4522 WARN_ON_ONCE(!HAS_DDI(dev));
4523 case INTEL_OUTPUT_DISPLAYPORT:
4524 case INTEL_OUTPUT_HDMI:
4525 case INTEL_OUTPUT_EDP:
4526 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4527 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4528 case INTEL_OUTPUT_DP_MST:
4529 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4530 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4531 case INTEL_OUTPUT_ANALOG:
4532 return POWER_DOMAIN_PORT_CRT;
4533 case INTEL_OUTPUT_DSI:
4534 return POWER_DOMAIN_PORT_DSI;
4535 default:
4536 return POWER_DOMAIN_PORT_OTHER;
4537 }
4538}
4539
4540static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4541{
319be8ae
ID
4542 struct drm_device *dev = crtc->dev;
4543 struct intel_encoder *intel_encoder;
4544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4545 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4546 unsigned long mask;
4547 enum transcoder transcoder;
4548
4549 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4550
4551 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4552 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4553 if (intel_crtc->config.pch_pfit.enabled ||
4554 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4555 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4556
319be8ae
ID
4557 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4558 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4559
77d22dca
ID
4560 return mask;
4561}
4562
77d22dca
ID
4563static void modeset_update_crtc_power_domains(struct drm_device *dev)
4564{
4565 struct drm_i915_private *dev_priv = dev->dev_private;
4566 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4567 struct intel_crtc *crtc;
4568
4569 /*
4570 * First get all needed power domains, then put all unneeded, to avoid
4571 * any unnecessary toggling of the power wells.
4572 */
d3fcc808 4573 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4574 enum intel_display_power_domain domain;
4575
4576 if (!crtc->base.enabled)
4577 continue;
4578
319be8ae 4579 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4580
4581 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4582 intel_display_power_get(dev_priv, domain);
4583 }
4584
d3fcc808 4585 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4586 enum intel_display_power_domain domain;
4587
4588 for_each_power_domain(domain, crtc->enabled_power_domains)
4589 intel_display_power_put(dev_priv, domain);
4590
4591 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4592 }
4593
4594 intel_display_set_init_power(dev_priv, false);
4595}
4596
dfcab17e 4597/* returns HPLL frequency in kHz */
f8bf63fd 4598static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4599{
586f49dc 4600 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4601
586f49dc
JB
4602 /* Obtain SKU information */
4603 mutex_lock(&dev_priv->dpio_lock);
4604 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4605 CCK_FUSE_HPLL_FREQ_MASK;
4606 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4607
dfcab17e 4608 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4609}
4610
f8bf63fd
VS
4611static void vlv_update_cdclk(struct drm_device *dev)
4612{
4613 struct drm_i915_private *dev_priv = dev->dev_private;
4614
4615 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4616 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4617 dev_priv->vlv_cdclk_freq);
4618
4619 /*
4620 * Program the gmbus_freq based on the cdclk frequency.
4621 * BSpec erroneously claims we should aim for 4MHz, but
4622 * in fact 1MHz is the correct frequency.
4623 */
4624 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4625}
4626
30a970c6
JB
4627/* Adjust CDclk dividers to allow high res or save power if possible */
4628static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4629{
4630 struct drm_i915_private *dev_priv = dev->dev_private;
4631 u32 val, cmd;
4632
d197b7d3 4633 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4634
dfcab17e 4635 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4636 cmd = 2;
dfcab17e 4637 else if (cdclk == 266667)
30a970c6
JB
4638 cmd = 1;
4639 else
4640 cmd = 0;
4641
4642 mutex_lock(&dev_priv->rps.hw_lock);
4643 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4644 val &= ~DSPFREQGUAR_MASK;
4645 val |= (cmd << DSPFREQGUAR_SHIFT);
4646 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4647 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4648 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4649 50)) {
4650 DRM_ERROR("timed out waiting for CDclk change\n");
4651 }
4652 mutex_unlock(&dev_priv->rps.hw_lock);
4653
dfcab17e 4654 if (cdclk == 400000) {
30a970c6
JB
4655 u32 divider, vco;
4656
4657 vco = valleyview_get_vco(dev_priv);
dfcab17e 4658 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4659
4660 mutex_lock(&dev_priv->dpio_lock);
4661 /* adjust cdclk divider */
4662 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4663 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4664 val |= divider;
4665 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4666
4667 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4668 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4669 50))
4670 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4671 mutex_unlock(&dev_priv->dpio_lock);
4672 }
4673
4674 mutex_lock(&dev_priv->dpio_lock);
4675 /* adjust self-refresh exit latency value */
4676 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4677 val &= ~0x7f;
4678
4679 /*
4680 * For high bandwidth configs, we set a higher latency in the bunit
4681 * so that the core display fetch happens in time to avoid underruns.
4682 */
dfcab17e 4683 if (cdclk == 400000)
30a970c6
JB
4684 val |= 4500 / 250; /* 4.5 usec */
4685 else
4686 val |= 3000 / 250; /* 3.0 usec */
4687 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4688 mutex_unlock(&dev_priv->dpio_lock);
4689
f8bf63fd 4690 vlv_update_cdclk(dev);
30a970c6
JB
4691}
4692
383c5a6a
VS
4693static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4694{
4695 struct drm_i915_private *dev_priv = dev->dev_private;
4696 u32 val, cmd;
4697
4698 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4699
4700 switch (cdclk) {
4701 case 400000:
4702 cmd = 3;
4703 break;
4704 case 333333:
4705 case 320000:
4706 cmd = 2;
4707 break;
4708 case 266667:
4709 cmd = 1;
4710 break;
4711 case 200000:
4712 cmd = 0;
4713 break;
4714 default:
4715 WARN_ON(1);
4716 return;
4717 }
4718
4719 mutex_lock(&dev_priv->rps.hw_lock);
4720 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4721 val &= ~DSPFREQGUAR_MASK_CHV;
4722 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4723 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4724 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4725 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4726 50)) {
4727 DRM_ERROR("timed out waiting for CDclk change\n");
4728 }
4729 mutex_unlock(&dev_priv->rps.hw_lock);
4730
4731 vlv_update_cdclk(dev);
4732}
4733
30a970c6
JB
4734static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4735 int max_pixclk)
4736{
29dc7ef3
VS
4737 int vco = valleyview_get_vco(dev_priv);
4738 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4739
d49a340d
VS
4740 /* FIXME: Punit isn't quite ready yet */
4741 if (IS_CHERRYVIEW(dev_priv->dev))
4742 return 400000;
4743
30a970c6
JB
4744 /*
4745 * Really only a few cases to deal with, as only 4 CDclks are supported:
4746 * 200MHz
4747 * 267MHz
29dc7ef3 4748 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4749 * 400MHz
4750 * So we check to see whether we're above 90% of the lower bin and
4751 * adjust if needed.
e37c67a1
VS
4752 *
4753 * We seem to get an unstable or solid color picture at 200MHz.
4754 * Not sure what's wrong. For now use 200MHz only when all pipes
4755 * are off.
30a970c6 4756 */
29dc7ef3 4757 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4758 return 400000;
4759 else if (max_pixclk > 266667*9/10)
29dc7ef3 4760 return freq_320;
e37c67a1 4761 else if (max_pixclk > 0)
dfcab17e 4762 return 266667;
e37c67a1
VS
4763 else
4764 return 200000;
30a970c6
JB
4765}
4766
2f2d7aa1
VS
4767/* compute the max pixel clock for new configuration */
4768static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4769{
4770 struct drm_device *dev = dev_priv->dev;
4771 struct intel_crtc *intel_crtc;
4772 int max_pixclk = 0;
4773
d3fcc808 4774 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4775 if (intel_crtc->new_enabled)
30a970c6 4776 max_pixclk = max(max_pixclk,
2f2d7aa1 4777 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4778 }
4779
4780 return max_pixclk;
4781}
4782
4783static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4784 unsigned *prepare_pipes)
30a970c6
JB
4785{
4786 struct drm_i915_private *dev_priv = dev->dev_private;
4787 struct intel_crtc *intel_crtc;
2f2d7aa1 4788 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4789
d60c4473
ID
4790 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4791 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4792 return;
4793
2f2d7aa1 4794 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4795 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4796 if (intel_crtc->base.enabled)
4797 *prepare_pipes |= (1 << intel_crtc->pipe);
4798}
4799
4800static void valleyview_modeset_global_resources(struct drm_device *dev)
4801{
4802 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4803 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4804 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4805
383c5a6a
VS
4806 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4807 if (IS_CHERRYVIEW(dev))
4808 cherryview_set_cdclk(dev, req_cdclk);
4809 else
4810 valleyview_set_cdclk(dev, req_cdclk);
4811 }
4812
77961eb9 4813 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4814}
4815
89b667f8
JB
4816static void valleyview_crtc_enable(struct drm_crtc *crtc)
4817{
4818 struct drm_device *dev = crtc->dev;
89b667f8
JB
4819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4820 struct intel_encoder *encoder;
4821 int pipe = intel_crtc->pipe;
23538ef1 4822 bool is_dsi;
89b667f8
JB
4823
4824 WARN_ON(!crtc->enabled);
4825
4826 if (intel_crtc->active)
4827 return;
4828
8525a235
SK
4829 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4830
1ae0d137
VS
4831 if (!is_dsi) {
4832 if (IS_CHERRYVIEW(dev))
4833 chv_prepare_pll(intel_crtc);
4834 else
4835 vlv_prepare_pll(intel_crtc);
4836 }
5b18e57c
DV
4837
4838 if (intel_crtc->config.has_dp_encoder)
4839 intel_dp_set_m_n(intel_crtc);
4840
4841 intel_set_pipe_timings(intel_crtc);
4842
5b18e57c
DV
4843 i9xx_set_pipeconf(intel_crtc);
4844
89b667f8 4845 intel_crtc->active = true;
89b667f8 4846
4a3436e8
VS
4847 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4848
89b667f8
JB
4849 for_each_encoder_on_crtc(dev, crtc, encoder)
4850 if (encoder->pre_pll_enable)
4851 encoder->pre_pll_enable(encoder);
4852
9d556c99
CML
4853 if (!is_dsi) {
4854 if (IS_CHERRYVIEW(dev))
4855 chv_enable_pll(intel_crtc);
4856 else
4857 vlv_enable_pll(intel_crtc);
4858 }
89b667f8
JB
4859
4860 for_each_encoder_on_crtc(dev, crtc, encoder)
4861 if (encoder->pre_enable)
4862 encoder->pre_enable(encoder);
4863
2dd24552
JB
4864 i9xx_pfit_enable(intel_crtc);
4865
63cbb074
VS
4866 intel_crtc_load_lut(crtc);
4867
f37fcc2a 4868 intel_update_watermarks(crtc);
e1fdc473 4869 intel_enable_pipe(intel_crtc);
be6a6f8e 4870
5004945f
JN
4871 for_each_encoder_on_crtc(dev, crtc, encoder)
4872 encoder->enable(encoder);
9ab0460b 4873
4b3a9526
VS
4874 assert_vblank_disabled(crtc);
4875 drm_crtc_vblank_on(crtc);
4876
9ab0460b 4877 intel_crtc_enable_planes(crtc);
d40d9187 4878
56b80e1f
VS
4879 /* Underruns don't raise interrupts, so check manually. */
4880 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4881}
4882
f13c2ef3
DV
4883static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4884{
4885 struct drm_device *dev = crtc->base.dev;
4886 struct drm_i915_private *dev_priv = dev->dev_private;
4887
4888 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4889 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4890}
4891
0b8765c6 4892static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4893{
4894 struct drm_device *dev = crtc->dev;
79e53945 4895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4896 struct intel_encoder *encoder;
79e53945 4897 int pipe = intel_crtc->pipe;
79e53945 4898
08a48469
DV
4899 WARN_ON(!crtc->enabled);
4900
f7abfe8b
CW
4901 if (intel_crtc->active)
4902 return;
4903
f13c2ef3
DV
4904 i9xx_set_pll_dividers(intel_crtc);
4905
5b18e57c
DV
4906 if (intel_crtc->config.has_dp_encoder)
4907 intel_dp_set_m_n(intel_crtc);
4908
4909 intel_set_pipe_timings(intel_crtc);
4910
5b18e57c
DV
4911 i9xx_set_pipeconf(intel_crtc);
4912
f7abfe8b 4913 intel_crtc->active = true;
6b383a7f 4914
4a3436e8
VS
4915 if (!IS_GEN2(dev))
4916 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4917
9d6d9f19
MK
4918 for_each_encoder_on_crtc(dev, crtc, encoder)
4919 if (encoder->pre_enable)
4920 encoder->pre_enable(encoder);
4921
f6736a1a
DV
4922 i9xx_enable_pll(intel_crtc);
4923
2dd24552
JB
4924 i9xx_pfit_enable(intel_crtc);
4925
63cbb074
VS
4926 intel_crtc_load_lut(crtc);
4927
f37fcc2a 4928 intel_update_watermarks(crtc);
e1fdc473 4929 intel_enable_pipe(intel_crtc);
be6a6f8e 4930
fa5c73b1
DV
4931 for_each_encoder_on_crtc(dev, crtc, encoder)
4932 encoder->enable(encoder);
9ab0460b 4933
4b3a9526
VS
4934 assert_vblank_disabled(crtc);
4935 drm_crtc_vblank_on(crtc);
4936
9ab0460b 4937 intel_crtc_enable_planes(crtc);
d40d9187 4938
4a3436e8
VS
4939 /*
4940 * Gen2 reports pipe underruns whenever all planes are disabled.
4941 * So don't enable underrun reporting before at least some planes
4942 * are enabled.
4943 * FIXME: Need to fix the logic to work when we turn off all planes
4944 * but leave the pipe running.
4945 */
4946 if (IS_GEN2(dev))
4947 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4948
56b80e1f
VS
4949 /* Underruns don't raise interrupts, so check manually. */
4950 i9xx_check_fifo_underruns(dev);
0b8765c6 4951}
79e53945 4952
87476d63
DV
4953static void i9xx_pfit_disable(struct intel_crtc *crtc)
4954{
4955 struct drm_device *dev = crtc->base.dev;
4956 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4957
328d8e82
DV
4958 if (!crtc->config.gmch_pfit.control)
4959 return;
87476d63 4960
328d8e82 4961 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4962
328d8e82
DV
4963 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4964 I915_READ(PFIT_CONTROL));
4965 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4966}
4967
0b8765c6
JB
4968static void i9xx_crtc_disable(struct drm_crtc *crtc)
4969{
4970 struct drm_device *dev = crtc->dev;
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4973 struct intel_encoder *encoder;
0b8765c6 4974 int pipe = intel_crtc->pipe;
ef9c3aee 4975
f7abfe8b
CW
4976 if (!intel_crtc->active)
4977 return;
4978
4a3436e8
VS
4979 /*
4980 * Gen2 reports pipe underruns whenever all planes are disabled.
4981 * So diasble underrun reporting before all the planes get disabled.
4982 * FIXME: Need to fix the logic to work when we turn off all planes
4983 * but leave the pipe running.
4984 */
4985 if (IS_GEN2(dev))
4986 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4987
564ed191
ID
4988 /*
4989 * Vblank time updates from the shadow to live plane control register
4990 * are blocked if the memory self-refresh mode is active at that
4991 * moment. So to make sure the plane gets truly disabled, disable
4992 * first the self-refresh mode. The self-refresh enable bit in turn
4993 * will be checked/applied by the HW only at the next frame start
4994 * event which is after the vblank start event, so we need to have a
4995 * wait-for-vblank between disabling the plane and the pipe.
4996 */
4997 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
4998 intel_crtc_disable_planes(crtc);
4999
6304cd91
VS
5000 /*
5001 * On gen2 planes are double buffered but the pipe isn't, so we must
5002 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5003 * We also need to wait on all gmch platforms because of the
5004 * self-refresh mode constraint explained above.
6304cd91 5005 */
564ed191 5006 intel_wait_for_vblank(dev, pipe);
6304cd91 5007
4b3a9526
VS
5008 drm_crtc_vblank_off(crtc);
5009 assert_vblank_disabled(crtc);
5010
5011 for_each_encoder_on_crtc(dev, crtc, encoder)
5012 encoder->disable(encoder);
5013
575f7ab7 5014 intel_disable_pipe(intel_crtc);
24a1f16d 5015
87476d63 5016 i9xx_pfit_disable(intel_crtc);
24a1f16d 5017
89b667f8
JB
5018 for_each_encoder_on_crtc(dev, crtc, encoder)
5019 if (encoder->post_disable)
5020 encoder->post_disable(encoder);
5021
076ed3b2
CML
5022 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
5023 if (IS_CHERRYVIEW(dev))
5024 chv_disable_pll(dev_priv, pipe);
5025 else if (IS_VALLEYVIEW(dev))
5026 vlv_disable_pll(dev_priv, pipe);
5027 else
1c4e0274 5028 i9xx_disable_pll(intel_crtc);
076ed3b2 5029 }
0b8765c6 5030
4a3436e8
VS
5031 if (!IS_GEN2(dev))
5032 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
5033
f7abfe8b 5034 intel_crtc->active = false;
46ba614c 5035 intel_update_watermarks(crtc);
f37fcc2a 5036
efa9624e 5037 mutex_lock(&dev->struct_mutex);
6b383a7f 5038 intel_update_fbc(dev);
efa9624e 5039 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5040}
5041
ee7b9f93
JB
5042static void i9xx_crtc_off(struct drm_crtc *crtc)
5043{
5044}
5045
976f8a20
DV
5046static void intel_crtc_update_sarea(struct drm_crtc *crtc,
5047 bool enabled)
2c07245f
ZW
5048{
5049 struct drm_device *dev = crtc->dev;
5050 struct drm_i915_master_private *master_priv;
5051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5052 int pipe = intel_crtc->pipe;
79e53945
JB
5053
5054 if (!dev->primary->master)
5055 return;
5056
5057 master_priv = dev->primary->master->driver_priv;
5058 if (!master_priv->sarea_priv)
5059 return;
5060
79e53945
JB
5061 switch (pipe) {
5062 case 0:
5063 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
5064 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
5065 break;
5066 case 1:
5067 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
5068 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
5069 break;
5070 default:
9db4a9c7 5071 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
5072 break;
5073 }
79e53945
JB
5074}
5075
b04c5bd6
BF
5076/* Master function to enable/disable CRTC and corresponding power wells */
5077void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5078{
5079 struct drm_device *dev = crtc->dev;
5080 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5082 enum intel_display_power_domain domain;
5083 unsigned long domains;
976f8a20 5084
0e572fe7
DV
5085 if (enable) {
5086 if (!intel_crtc->active) {
e1e9fb84
DV
5087 domains = get_crtc_power_domains(crtc);
5088 for_each_power_domain(domain, domains)
5089 intel_display_power_get(dev_priv, domain);
5090 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5091
5092 dev_priv->display.crtc_enable(crtc);
5093 }
5094 } else {
5095 if (intel_crtc->active) {
5096 dev_priv->display.crtc_disable(crtc);
5097
e1e9fb84
DV
5098 domains = intel_crtc->enabled_power_domains;
5099 for_each_power_domain(domain, domains)
5100 intel_display_power_put(dev_priv, domain);
5101 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5102 }
5103 }
b04c5bd6
BF
5104}
5105
5106/**
5107 * Sets the power management mode of the pipe and plane.
5108 */
5109void intel_crtc_update_dpms(struct drm_crtc *crtc)
5110{
5111 struct drm_device *dev = crtc->dev;
5112 struct intel_encoder *intel_encoder;
5113 bool enable = false;
5114
5115 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5116 enable |= intel_encoder->connectors_active;
5117
5118 intel_crtc_control(crtc, enable);
976f8a20
DV
5119
5120 intel_crtc_update_sarea(crtc, enable);
5121}
5122
cdd59983
CW
5123static void intel_crtc_disable(struct drm_crtc *crtc)
5124{
cdd59983 5125 struct drm_device *dev = crtc->dev;
976f8a20 5126 struct drm_connector *connector;
ee7b9f93 5127 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 5128 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 5129 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 5130
976f8a20
DV
5131 /* crtc should still be enabled when we disable it. */
5132 WARN_ON(!crtc->enabled);
5133
5134 dev_priv->display.crtc_disable(crtc);
5135 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
5136 dev_priv->display.off(crtc);
5137
f4510a27 5138 if (crtc->primary->fb) {
cdd59983 5139 mutex_lock(&dev->struct_mutex);
a071fa00
DV
5140 intel_unpin_fb_obj(old_obj);
5141 i915_gem_track_fb(old_obj, NULL,
5142 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 5143 mutex_unlock(&dev->struct_mutex);
f4510a27 5144 crtc->primary->fb = NULL;
976f8a20
DV
5145 }
5146
5147 /* Update computed state. */
5148 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5149 if (!connector->encoder || !connector->encoder->crtc)
5150 continue;
5151
5152 if (connector->encoder->crtc != crtc)
5153 continue;
5154
5155 connector->dpms = DRM_MODE_DPMS_OFF;
5156 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5157 }
5158}
5159
ea5b213a 5160void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5161{
4ef69c7a 5162 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5163
ea5b213a
CW
5164 drm_encoder_cleanup(encoder);
5165 kfree(intel_encoder);
7e7d76c3
JB
5166}
5167
9237329d 5168/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5169 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5170 * state of the entire output pipe. */
9237329d 5171static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5172{
5ab432ef
DV
5173 if (mode == DRM_MODE_DPMS_ON) {
5174 encoder->connectors_active = true;
5175
b2cabb0e 5176 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5177 } else {
5178 encoder->connectors_active = false;
5179
b2cabb0e 5180 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5181 }
79e53945
JB
5182}
5183
0a91ca29
DV
5184/* Cross check the actual hw state with our own modeset state tracking (and it's
5185 * internal consistency). */
b980514c 5186static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5187{
0a91ca29
DV
5188 if (connector->get_hw_state(connector)) {
5189 struct intel_encoder *encoder = connector->encoder;
5190 struct drm_crtc *crtc;
5191 bool encoder_enabled;
5192 enum pipe pipe;
5193
5194 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5195 connector->base.base.id,
c23cc417 5196 connector->base.name);
0a91ca29 5197
0e32b39c
DA
5198 /* there is no real hw state for MST connectors */
5199 if (connector->mst_port)
5200 return;
5201
0a91ca29
DV
5202 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5203 "wrong connector dpms state\n");
5204 WARN(connector->base.encoder != &encoder->base,
5205 "active connector not linked to encoder\n");
0a91ca29 5206
36cd7444
DA
5207 if (encoder) {
5208 WARN(!encoder->connectors_active,
5209 "encoder->connectors_active not set\n");
5210
5211 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5212 WARN(!encoder_enabled, "encoder not enabled\n");
5213 if (WARN_ON(!encoder->base.crtc))
5214 return;
0a91ca29 5215
36cd7444 5216 crtc = encoder->base.crtc;
0a91ca29 5217
36cd7444
DA
5218 WARN(!crtc->enabled, "crtc not enabled\n");
5219 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5220 WARN(pipe != to_intel_crtc(crtc)->pipe,
5221 "encoder active on the wrong pipe\n");
5222 }
0a91ca29 5223 }
79e53945
JB
5224}
5225
5ab432ef
DV
5226/* Even simpler default implementation, if there's really no special case to
5227 * consider. */
5228void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5229{
5ab432ef
DV
5230 /* All the simple cases only support two dpms states. */
5231 if (mode != DRM_MODE_DPMS_ON)
5232 mode = DRM_MODE_DPMS_OFF;
d4270e57 5233
5ab432ef
DV
5234 if (mode == connector->dpms)
5235 return;
5236
5237 connector->dpms = mode;
5238
5239 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5240 if (connector->encoder)
5241 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5242
b980514c 5243 intel_modeset_check_state(connector->dev);
79e53945
JB
5244}
5245
f0947c37
DV
5246/* Simple connector->get_hw_state implementation for encoders that support only
5247 * one connector and no cloning and hence the encoder state determines the state
5248 * of the connector. */
5249bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5250{
24929352 5251 enum pipe pipe = 0;
f0947c37 5252 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5253
f0947c37 5254 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5255}
5256
1857e1da
DV
5257static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5258 struct intel_crtc_config *pipe_config)
5259{
5260 struct drm_i915_private *dev_priv = dev->dev_private;
5261 struct intel_crtc *pipe_B_crtc =
5262 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5263
5264 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5265 pipe_name(pipe), pipe_config->fdi_lanes);
5266 if (pipe_config->fdi_lanes > 4) {
5267 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5268 pipe_name(pipe), pipe_config->fdi_lanes);
5269 return false;
5270 }
5271
bafb6553 5272 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5273 if (pipe_config->fdi_lanes > 2) {
5274 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5275 pipe_config->fdi_lanes);
5276 return false;
5277 } else {
5278 return true;
5279 }
5280 }
5281
5282 if (INTEL_INFO(dev)->num_pipes == 2)
5283 return true;
5284
5285 /* Ivybridge 3 pipe is really complicated */
5286 switch (pipe) {
5287 case PIPE_A:
5288 return true;
5289 case PIPE_B:
5290 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5291 pipe_config->fdi_lanes > 2) {
5292 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5293 pipe_name(pipe), pipe_config->fdi_lanes);
5294 return false;
5295 }
5296 return true;
5297 case PIPE_C:
1e833f40 5298 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5299 pipe_B_crtc->config.fdi_lanes <= 2) {
5300 if (pipe_config->fdi_lanes > 2) {
5301 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5302 pipe_name(pipe), pipe_config->fdi_lanes);
5303 return false;
5304 }
5305 } else {
5306 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5307 return false;
5308 }
5309 return true;
5310 default:
5311 BUG();
5312 }
5313}
5314
e29c22c0
DV
5315#define RETRY 1
5316static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5317 struct intel_crtc_config *pipe_config)
877d48d5 5318{
1857e1da 5319 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5320 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5321 int lane, link_bw, fdi_dotclock;
e29c22c0 5322 bool setup_ok, needs_recompute = false;
877d48d5 5323
e29c22c0 5324retry:
877d48d5
DV
5325 /* FDI is a binary signal running at ~2.7GHz, encoding
5326 * each output octet as 10 bits. The actual frequency
5327 * is stored as a divider into a 100MHz clock, and the
5328 * mode pixel clock is stored in units of 1KHz.
5329 * Hence the bw of each lane in terms of the mode signal
5330 * is:
5331 */
5332 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5333
241bfc38 5334 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5335
2bd89a07 5336 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5337 pipe_config->pipe_bpp);
5338
5339 pipe_config->fdi_lanes = lane;
5340
2bd89a07 5341 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5342 link_bw, &pipe_config->fdi_m_n);
1857e1da 5343
e29c22c0
DV
5344 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5345 intel_crtc->pipe, pipe_config);
5346 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5347 pipe_config->pipe_bpp -= 2*3;
5348 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5349 pipe_config->pipe_bpp);
5350 needs_recompute = true;
5351 pipe_config->bw_constrained = true;
5352
5353 goto retry;
5354 }
5355
5356 if (needs_recompute)
5357 return RETRY;
5358
5359 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5360}
5361
42db64ef
PZ
5362static void hsw_compute_ips_config(struct intel_crtc *crtc,
5363 struct intel_crtc_config *pipe_config)
5364{
d330a953 5365 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5366 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5367 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5368}
5369
a43f6e0f 5370static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5371 struct intel_crtc_config *pipe_config)
79e53945 5372{
a43f6e0f 5373 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5374 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5375
ad3a4479 5376 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5377 if (INTEL_INFO(dev)->gen < 4) {
5378 struct drm_i915_private *dev_priv = dev->dev_private;
5379 int clock_limit =
5380 dev_priv->display.get_display_clock_speed(dev);
5381
5382 /*
5383 * Enable pixel doubling when the dot clock
5384 * is > 90% of the (display) core speed.
5385 *
b397c96b
VS
5386 * GDG double wide on either pipe,
5387 * otherwise pipe A only.
cf532bb2 5388 */
b397c96b 5389 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5390 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5391 clock_limit *= 2;
cf532bb2 5392 pipe_config->double_wide = true;
ad3a4479
VS
5393 }
5394
241bfc38 5395 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5396 return -EINVAL;
2c07245f 5397 }
89749350 5398
1d1d0e27
VS
5399 /*
5400 * Pipe horizontal size must be even in:
5401 * - DVO ganged mode
5402 * - LVDS dual channel mode
5403 * - Double wide pipe
5404 */
5405 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5406 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5407 pipe_config->pipe_src_w &= ~1;
5408
8693a824
DL
5409 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5410 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5411 */
5412 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5413 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5414 return -EINVAL;
44f46b42 5415
bd080ee5 5416 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5417 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5418 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5419 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5420 * for lvds. */
5421 pipe_config->pipe_bpp = 8*3;
5422 }
5423
f5adf94e 5424 if (HAS_IPS(dev))
a43f6e0f
DV
5425 hsw_compute_ips_config(crtc, pipe_config);
5426
12030431
DV
5427 /*
5428 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5429 * old clock survives for now.
5430 */
5431 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5432 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5433
877d48d5 5434 if (pipe_config->has_pch_encoder)
a43f6e0f 5435 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5436
e29c22c0 5437 return 0;
79e53945
JB
5438}
5439
25eb05fc
JB
5440static int valleyview_get_display_clock_speed(struct drm_device *dev)
5441{
d197b7d3
VS
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443 int vco = valleyview_get_vco(dev_priv);
5444 u32 val;
5445 int divider;
5446
d49a340d
VS
5447 /* FIXME: Punit isn't quite ready yet */
5448 if (IS_CHERRYVIEW(dev))
5449 return 400000;
5450
d197b7d3
VS
5451 mutex_lock(&dev_priv->dpio_lock);
5452 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5453 mutex_unlock(&dev_priv->dpio_lock);
5454
5455 divider = val & DISPLAY_FREQUENCY_VALUES;
5456
7d007f40
VS
5457 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5458 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5459 "cdclk change in progress\n");
5460
d197b7d3 5461 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5462}
5463
e70236a8
JB
5464static int i945_get_display_clock_speed(struct drm_device *dev)
5465{
5466 return 400000;
5467}
79e53945 5468
e70236a8 5469static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5470{
e70236a8
JB
5471 return 333000;
5472}
79e53945 5473
e70236a8
JB
5474static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5475{
5476 return 200000;
5477}
79e53945 5478
257a7ffc
DV
5479static int pnv_get_display_clock_speed(struct drm_device *dev)
5480{
5481 u16 gcfgc = 0;
5482
5483 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5484
5485 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5486 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5487 return 267000;
5488 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5489 return 333000;
5490 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5491 return 444000;
5492 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5493 return 200000;
5494 default:
5495 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5496 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5497 return 133000;
5498 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5499 return 167000;
5500 }
5501}
5502
e70236a8
JB
5503static int i915gm_get_display_clock_speed(struct drm_device *dev)
5504{
5505 u16 gcfgc = 0;
79e53945 5506
e70236a8
JB
5507 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5508
5509 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5510 return 133000;
5511 else {
5512 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5513 case GC_DISPLAY_CLOCK_333_MHZ:
5514 return 333000;
5515 default:
5516 case GC_DISPLAY_CLOCK_190_200_MHZ:
5517 return 190000;
79e53945 5518 }
e70236a8
JB
5519 }
5520}
5521
5522static int i865_get_display_clock_speed(struct drm_device *dev)
5523{
5524 return 266000;
5525}
5526
5527static int i855_get_display_clock_speed(struct drm_device *dev)
5528{
5529 u16 hpllcc = 0;
5530 /* Assume that the hardware is in the high speed state. This
5531 * should be the default.
5532 */
5533 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5534 case GC_CLOCK_133_200:
5535 case GC_CLOCK_100_200:
5536 return 200000;
5537 case GC_CLOCK_166_250:
5538 return 250000;
5539 case GC_CLOCK_100_133:
79e53945 5540 return 133000;
e70236a8 5541 }
79e53945 5542
e70236a8
JB
5543 /* Shouldn't happen */
5544 return 0;
5545}
79e53945 5546
e70236a8
JB
5547static int i830_get_display_clock_speed(struct drm_device *dev)
5548{
5549 return 133000;
79e53945
JB
5550}
5551
2c07245f 5552static void
a65851af 5553intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5554{
a65851af
VS
5555 while (*num > DATA_LINK_M_N_MASK ||
5556 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5557 *num >>= 1;
5558 *den >>= 1;
5559 }
5560}
5561
a65851af
VS
5562static void compute_m_n(unsigned int m, unsigned int n,
5563 uint32_t *ret_m, uint32_t *ret_n)
5564{
5565 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5566 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5567 intel_reduce_m_n_ratio(ret_m, ret_n);
5568}
5569
e69d0bc1
DV
5570void
5571intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5572 int pixel_clock, int link_clock,
5573 struct intel_link_m_n *m_n)
2c07245f 5574{
e69d0bc1 5575 m_n->tu = 64;
a65851af
VS
5576
5577 compute_m_n(bits_per_pixel * pixel_clock,
5578 link_clock * nlanes * 8,
5579 &m_n->gmch_m, &m_n->gmch_n);
5580
5581 compute_m_n(pixel_clock, link_clock,
5582 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5583}
5584
a7615030
CW
5585static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5586{
d330a953
JN
5587 if (i915.panel_use_ssc >= 0)
5588 return i915.panel_use_ssc != 0;
41aa3448 5589 return dev_priv->vbt.lvds_use_ssc
435793df 5590 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5591}
5592
c65d77d8
JB
5593static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5594{
5595 struct drm_device *dev = crtc->dev;
5596 struct drm_i915_private *dev_priv = dev->dev_private;
5597 int refclk;
5598
a0c4da24 5599 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5600 refclk = 100000;
a0c4da24 5601 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5602 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5603 refclk = dev_priv->vbt.lvds_ssc_freq;
5604 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5605 } else if (!IS_GEN2(dev)) {
5606 refclk = 96000;
5607 } else {
5608 refclk = 48000;
5609 }
5610
5611 return refclk;
5612}
5613
7429e9d4 5614static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5615{
7df00d7a 5616 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5617}
f47709a9 5618
7429e9d4
DV
5619static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5620{
5621 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5622}
5623
f47709a9 5624static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5625 intel_clock_t *reduced_clock)
5626{
f47709a9 5627 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5628 u32 fp, fp2 = 0;
5629
5630 if (IS_PINEVIEW(dev)) {
7429e9d4 5631 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5632 if (reduced_clock)
7429e9d4 5633 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5634 } else {
7429e9d4 5635 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5636 if (reduced_clock)
7429e9d4 5637 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5638 }
5639
8bcc2795 5640 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5641
f47709a9
DV
5642 crtc->lowfreq_avail = false;
5643 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5644 reduced_clock && i915.powersave) {
8bcc2795 5645 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5646 crtc->lowfreq_avail = true;
a7516a05 5647 } else {
8bcc2795 5648 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5649 }
5650}
5651
5e69f97f
CML
5652static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5653 pipe)
89b667f8
JB
5654{
5655 u32 reg_val;
5656
5657 /*
5658 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5659 * and set it to a reasonable value instead.
5660 */
ab3c759a 5661 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5662 reg_val &= 0xffffff00;
5663 reg_val |= 0x00000030;
ab3c759a 5664 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5665
ab3c759a 5666 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5667 reg_val &= 0x8cffffff;
5668 reg_val = 0x8c000000;
ab3c759a 5669 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5670
ab3c759a 5671 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5672 reg_val &= 0xffffff00;
ab3c759a 5673 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5674
ab3c759a 5675 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5676 reg_val &= 0x00ffffff;
5677 reg_val |= 0xb0000000;
ab3c759a 5678 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5679}
5680
b551842d
DV
5681static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5682 struct intel_link_m_n *m_n)
5683{
5684 struct drm_device *dev = crtc->base.dev;
5685 struct drm_i915_private *dev_priv = dev->dev_private;
5686 int pipe = crtc->pipe;
5687
e3b95f1e
DV
5688 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5689 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5690 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5691 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5692}
5693
5694static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5695 struct intel_link_m_n *m_n,
5696 struct intel_link_m_n *m2_n2)
b551842d
DV
5697{
5698 struct drm_device *dev = crtc->base.dev;
5699 struct drm_i915_private *dev_priv = dev->dev_private;
5700 int pipe = crtc->pipe;
5701 enum transcoder transcoder = crtc->config.cpu_transcoder;
5702
5703 if (INTEL_INFO(dev)->gen >= 5) {
5704 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5705 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5706 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5707 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5708 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5709 * for gen < 8) and if DRRS is supported (to make sure the
5710 * registers are not unnecessarily accessed).
5711 */
5712 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5713 crtc->config.has_drrs) {
5714 I915_WRITE(PIPE_DATA_M2(transcoder),
5715 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5716 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5717 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5718 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5719 }
b551842d 5720 } else {
e3b95f1e
DV
5721 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5722 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5723 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5724 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5725 }
5726}
5727
f769cd24 5728void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5729{
5730 if (crtc->config.has_pch_encoder)
5731 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5732 else
f769cd24
VK
5733 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5734 &crtc->config.dp_m2_n2);
03afc4a2
DV
5735}
5736
f47709a9 5737static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5738{
5739 u32 dpll, dpll_md;
5740
5741 /*
5742 * Enable DPIO clock input. We should never disable the reference
5743 * clock for pipe B, since VGA hotplug / manual detection depends
5744 * on it.
5745 */
5746 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5747 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5748 /* We should never disable this, set it here for state tracking */
5749 if (crtc->pipe == PIPE_B)
5750 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5751 dpll |= DPLL_VCO_ENABLE;
5752 crtc->config.dpll_hw_state.dpll = dpll;
5753
5754 dpll_md = (crtc->config.pixel_multiplier - 1)
5755 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5756 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5757}
5758
5759static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5760{
f47709a9 5761 struct drm_device *dev = crtc->base.dev;
a0c4da24 5762 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5763 int pipe = crtc->pipe;
bdd4b6a6 5764 u32 mdiv;
a0c4da24 5765 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5766 u32 coreclk, reg_val;
a0c4da24 5767
09153000
DV
5768 mutex_lock(&dev_priv->dpio_lock);
5769
f47709a9
DV
5770 bestn = crtc->config.dpll.n;
5771 bestm1 = crtc->config.dpll.m1;
5772 bestm2 = crtc->config.dpll.m2;
5773 bestp1 = crtc->config.dpll.p1;
5774 bestp2 = crtc->config.dpll.p2;
a0c4da24 5775
89b667f8
JB
5776 /* See eDP HDMI DPIO driver vbios notes doc */
5777
5778 /* PLL B needs special handling */
bdd4b6a6 5779 if (pipe == PIPE_B)
5e69f97f 5780 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5781
5782 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5783 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5784
5785 /* Disable target IRef on PLL */
ab3c759a 5786 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5787 reg_val &= 0x00ffffff;
ab3c759a 5788 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5789
5790 /* Disable fast lock */
ab3c759a 5791 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5792
5793 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5794 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5795 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5796 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5797 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5798
5799 /*
5800 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5801 * but we don't support that).
5802 * Note: don't use the DAC post divider as it seems unstable.
5803 */
5804 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5805 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5806
a0c4da24 5807 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5808 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5809
89b667f8 5810 /* Set HBR and RBR LPF coefficients */
ff9a6750 5811 if (crtc->config.port_clock == 162000 ||
99750bd4 5812 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5813 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5814 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5815 0x009f0003);
89b667f8 5816 else
ab3c759a 5817 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5818 0x00d0000f);
5819
5820 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5821 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5822 /* Use SSC source */
bdd4b6a6 5823 if (pipe == PIPE_A)
ab3c759a 5824 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5825 0x0df40000);
5826 else
ab3c759a 5827 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5828 0x0df70000);
5829 } else { /* HDMI or VGA */
5830 /* Use bend source */
bdd4b6a6 5831 if (pipe == PIPE_A)
ab3c759a 5832 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5833 0x0df70000);
5834 else
ab3c759a 5835 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5836 0x0df40000);
5837 }
a0c4da24 5838
ab3c759a 5839 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5840 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5841 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5842 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5843 coreclk |= 0x01000000;
ab3c759a 5844 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5845
ab3c759a 5846 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5847 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5848}
5849
9d556c99 5850static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5851{
5852 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5853 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5854 DPLL_VCO_ENABLE;
5855 if (crtc->pipe != PIPE_A)
5856 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5857
5858 crtc->config.dpll_hw_state.dpll_md =
5859 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5860}
5861
5862static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5863{
5864 struct drm_device *dev = crtc->base.dev;
5865 struct drm_i915_private *dev_priv = dev->dev_private;
5866 int pipe = crtc->pipe;
5867 int dpll_reg = DPLL(crtc->pipe);
5868 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5869 u32 loopfilter, intcoeff;
9d556c99
CML
5870 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5871 int refclk;
5872
9d556c99
CML
5873 bestn = crtc->config.dpll.n;
5874 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5875 bestm1 = crtc->config.dpll.m1;
5876 bestm2 = crtc->config.dpll.m2 >> 22;
5877 bestp1 = crtc->config.dpll.p1;
5878 bestp2 = crtc->config.dpll.p2;
5879
5880 /*
5881 * Enable Refclk and SSC
5882 */
a11b0703
VS
5883 I915_WRITE(dpll_reg,
5884 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5885
5886 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5887
9d556c99
CML
5888 /* p1 and p2 divider */
5889 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5890 5 << DPIO_CHV_S1_DIV_SHIFT |
5891 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5892 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5893 1 << DPIO_CHV_K_DIV_SHIFT);
5894
5895 /* Feedback post-divider - m2 */
5896 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5897
5898 /* Feedback refclk divider - n and m1 */
5899 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5900 DPIO_CHV_M1_DIV_BY_2 |
5901 1 << DPIO_CHV_N_DIV_SHIFT);
5902
5903 /* M2 fraction division */
5904 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5905
5906 /* M2 fraction division enable */
5907 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5908 DPIO_CHV_FRAC_DIV_EN |
5909 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5910
5911 /* Loop filter */
5912 refclk = i9xx_get_refclk(&crtc->base, 0);
5913 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5914 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5915 if (refclk == 100000)
5916 intcoeff = 11;
5917 else if (refclk == 38400)
5918 intcoeff = 10;
5919 else
5920 intcoeff = 9;
5921 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5922 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5923
5924 /* AFC Recal */
5925 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5926 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5927 DPIO_AFC_RECAL);
5928
5929 mutex_unlock(&dev_priv->dpio_lock);
5930}
5931
f47709a9
DV
5932static void i9xx_update_pll(struct intel_crtc *crtc,
5933 intel_clock_t *reduced_clock,
eb1cbe48
DV
5934 int num_connectors)
5935{
f47709a9 5936 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5937 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5938 u32 dpll;
5939 bool is_sdvo;
f47709a9 5940 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5941
f47709a9 5942 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5943
f47709a9
DV
5944 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5945 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5946
5947 dpll = DPLL_VGA_MODE_DIS;
5948
f47709a9 5949 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5950 dpll |= DPLLB_MODE_LVDS;
5951 else
5952 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5953
ef1b460d 5954 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5955 dpll |= (crtc->config.pixel_multiplier - 1)
5956 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5957 }
198a037f
DV
5958
5959 if (is_sdvo)
4a33e48d 5960 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5961
f47709a9 5962 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5963 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5964
5965 /* compute bitmask from p1 value */
5966 if (IS_PINEVIEW(dev))
5967 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5968 else {
5969 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5970 if (IS_G4X(dev) && reduced_clock)
5971 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5972 }
5973 switch (clock->p2) {
5974 case 5:
5975 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5976 break;
5977 case 7:
5978 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5979 break;
5980 case 10:
5981 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5982 break;
5983 case 14:
5984 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5985 break;
5986 }
5987 if (INTEL_INFO(dev)->gen >= 4)
5988 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5989
09ede541 5990 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5991 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5992 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5993 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5994 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5995 else
5996 dpll |= PLL_REF_INPUT_DREFCLK;
5997
5998 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5999 crtc->config.dpll_hw_state.dpll = dpll;
6000
eb1cbe48 6001 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
6002 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
6003 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 6004 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6005 }
6006}
6007
f47709a9 6008static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 6009 intel_clock_t *reduced_clock,
eb1cbe48
DV
6010 int num_connectors)
6011{
f47709a9 6012 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6013 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6014 u32 dpll;
f47709a9 6015 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 6016
f47709a9 6017 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 6018
eb1cbe48
DV
6019 dpll = DPLL_VGA_MODE_DIS;
6020
f47709a9 6021 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6022 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6023 } else {
6024 if (clock->p1 == 2)
6025 dpll |= PLL_P1_DIVIDE_BY_TWO;
6026 else
6027 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6028 if (clock->p2 == 4)
6029 dpll |= PLL_P2_DIVIDE_BY_4;
6030 }
6031
1c4e0274 6032 if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4a33e48d
DV
6033 dpll |= DPLL_DVO_2X_MODE;
6034
f47709a9 6035 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6036 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6037 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6038 else
6039 dpll |= PLL_REF_INPUT_DREFCLK;
6040
6041 dpll |= DPLL_VCO_ENABLE;
8bcc2795 6042 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6043}
6044
8a654f3b 6045static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6046{
6047 struct drm_device *dev = intel_crtc->base.dev;
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6049 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6050 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
6051 struct drm_display_mode *adjusted_mode =
6052 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
6053 uint32_t crtc_vtotal, crtc_vblank_end;
6054 int vsyncshift = 0;
4d8a62ea
DV
6055
6056 /* We need to be careful not to changed the adjusted mode, for otherwise
6057 * the hw state checker will get angry at the mismatch. */
6058 crtc_vtotal = adjusted_mode->crtc_vtotal;
6059 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6060
609aeaca 6061 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6062 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6063 crtc_vtotal -= 1;
6064 crtc_vblank_end -= 1;
609aeaca
VS
6065
6066 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6067 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6068 else
6069 vsyncshift = adjusted_mode->crtc_hsync_start -
6070 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6071 if (vsyncshift < 0)
6072 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6073 }
6074
6075 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6076 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6077
fe2b8f9d 6078 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6079 (adjusted_mode->crtc_hdisplay - 1) |
6080 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6081 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6082 (adjusted_mode->crtc_hblank_start - 1) |
6083 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6084 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6085 (adjusted_mode->crtc_hsync_start - 1) |
6086 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6087
fe2b8f9d 6088 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6089 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6090 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6091 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6092 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6093 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6094 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6095 (adjusted_mode->crtc_vsync_start - 1) |
6096 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6097
b5e508d4
PZ
6098 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6099 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6100 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6101 * bits. */
6102 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6103 (pipe == PIPE_B || pipe == PIPE_C))
6104 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6105
b0e77b9c
PZ
6106 /* pipesrc controls the size that is scaled from, which should
6107 * always be the user's requested size.
6108 */
6109 I915_WRITE(PIPESRC(pipe),
37327abd
VS
6110 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6111 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
6112}
6113
1bd1bd80
DV
6114static void intel_get_pipe_timings(struct intel_crtc *crtc,
6115 struct intel_crtc_config *pipe_config)
6116{
6117 struct drm_device *dev = crtc->base.dev;
6118 struct drm_i915_private *dev_priv = dev->dev_private;
6119 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6120 uint32_t tmp;
6121
6122 tmp = I915_READ(HTOTAL(cpu_transcoder));
6123 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6124 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6125 tmp = I915_READ(HBLANK(cpu_transcoder));
6126 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6127 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6128 tmp = I915_READ(HSYNC(cpu_transcoder));
6129 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6130 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6131
6132 tmp = I915_READ(VTOTAL(cpu_transcoder));
6133 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6134 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6135 tmp = I915_READ(VBLANK(cpu_transcoder));
6136 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6137 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6138 tmp = I915_READ(VSYNC(cpu_transcoder));
6139 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6140 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6141
6142 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6143 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6144 pipe_config->adjusted_mode.crtc_vtotal += 1;
6145 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6146 }
6147
6148 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6149 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6150 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6151
6152 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6153 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6154}
6155
f6a83288
DV
6156void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6157 struct intel_crtc_config *pipe_config)
babea61d 6158{
f6a83288
DV
6159 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6160 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6161 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6162 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6163
f6a83288
DV
6164 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6165 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6166 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6167 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6168
f6a83288 6169 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6170
f6a83288
DV
6171 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6172 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6173}
6174
84b046f3
DV
6175static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6176{
6177 struct drm_device *dev = intel_crtc->base.dev;
6178 struct drm_i915_private *dev_priv = dev->dev_private;
6179 uint32_t pipeconf;
6180
9f11a9e4 6181 pipeconf = 0;
84b046f3 6182
b6b5d049
VS
6183 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6184 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6185 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6186
cf532bb2
VS
6187 if (intel_crtc->config.double_wide)
6188 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6189
ff9ce46e
DV
6190 /* only g4x and later have fancy bpc/dither controls */
6191 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6192 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6193 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6194 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6195 PIPECONF_DITHER_TYPE_SP;
84b046f3 6196
ff9ce46e
DV
6197 switch (intel_crtc->config.pipe_bpp) {
6198 case 18:
6199 pipeconf |= PIPECONF_6BPC;
6200 break;
6201 case 24:
6202 pipeconf |= PIPECONF_8BPC;
6203 break;
6204 case 30:
6205 pipeconf |= PIPECONF_10BPC;
6206 break;
6207 default:
6208 /* Case prevented by intel_choose_pipe_bpp_dither. */
6209 BUG();
84b046f3
DV
6210 }
6211 }
6212
6213 if (HAS_PIPE_CXSR(dev)) {
6214 if (intel_crtc->lowfreq_avail) {
6215 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6216 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6217 } else {
6218 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6219 }
6220 }
6221
efc2cfff
VS
6222 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6223 if (INTEL_INFO(dev)->gen < 4 ||
6224 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6225 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6226 else
6227 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6228 } else
84b046f3
DV
6229 pipeconf |= PIPECONF_PROGRESSIVE;
6230
9f11a9e4
DV
6231 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6232 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6233
84b046f3
DV
6234 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6235 POSTING_READ(PIPECONF(intel_crtc->pipe));
6236}
6237
f564048e 6238static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6239 int x, int y,
94352cf9 6240 struct drm_framebuffer *fb)
79e53945
JB
6241{
6242 struct drm_device *dev = crtc->dev;
6243 struct drm_i915_private *dev_priv = dev->dev_private;
6244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6245 int refclk, num_connectors = 0;
652c393a 6246 intel_clock_t clock, reduced_clock;
a16af721 6247 bool ok, has_reduced_clock = false;
e9fd1c02 6248 bool is_lvds = false, is_dsi = false;
5eddb70b 6249 struct intel_encoder *encoder;
d4906093 6250 const intel_limit_t *limit;
79e53945 6251
6c2b7c12 6252 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6253 switch (encoder->type) {
79e53945
JB
6254 case INTEL_OUTPUT_LVDS:
6255 is_lvds = true;
6256 break;
e9fd1c02
JN
6257 case INTEL_OUTPUT_DSI:
6258 is_dsi = true;
6259 break;
79e53945 6260 }
43565a06 6261
c751ce4f 6262 num_connectors++;
79e53945
JB
6263 }
6264
f2335330 6265 if (is_dsi)
5b18e57c 6266 return 0;
f2335330
JN
6267
6268 if (!intel_crtc->config.clock_set) {
6269 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6270
e9fd1c02
JN
6271 /*
6272 * Returns a set of divisors for the desired target clock with
6273 * the given refclk, or FALSE. The returned values represent
6274 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6275 * 2) / p1 / p2.
6276 */
6277 limit = intel_limit(crtc, refclk);
6278 ok = dev_priv->display.find_dpll(limit, crtc,
6279 intel_crtc->config.port_clock,
6280 refclk, NULL, &clock);
f2335330 6281 if (!ok) {
e9fd1c02
JN
6282 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6283 return -EINVAL;
6284 }
79e53945 6285
f2335330
JN
6286 if (is_lvds && dev_priv->lvds_downclock_avail) {
6287 /*
6288 * Ensure we match the reduced clock's P to the target
6289 * clock. If the clocks don't match, we can't switch
6290 * the display clock by using the FP0/FP1. In such case
6291 * we will disable the LVDS downclock feature.
6292 */
6293 has_reduced_clock =
6294 dev_priv->display.find_dpll(limit, crtc,
6295 dev_priv->lvds_downclock,
6296 refclk, &clock,
6297 &reduced_clock);
6298 }
6299 /* Compat-code for transition, will disappear. */
f47709a9
DV
6300 intel_crtc->config.dpll.n = clock.n;
6301 intel_crtc->config.dpll.m1 = clock.m1;
6302 intel_crtc->config.dpll.m2 = clock.m2;
6303 intel_crtc->config.dpll.p1 = clock.p1;
6304 intel_crtc->config.dpll.p2 = clock.p2;
6305 }
7026d4ac 6306
e9fd1c02 6307 if (IS_GEN2(dev)) {
8a654f3b 6308 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6309 has_reduced_clock ? &reduced_clock : NULL,
6310 num_connectors);
9d556c99
CML
6311 } else if (IS_CHERRYVIEW(dev)) {
6312 chv_update_pll(intel_crtc);
e9fd1c02 6313 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6314 vlv_update_pll(intel_crtc);
e9fd1c02 6315 } else {
f47709a9 6316 i9xx_update_pll(intel_crtc,
eb1cbe48 6317 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6318 num_connectors);
e9fd1c02 6319 }
79e53945 6320
c8f7a0db 6321 return 0;
f564048e
EA
6322}
6323
2fa2fe9a
DV
6324static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6325 struct intel_crtc_config *pipe_config)
6326{
6327 struct drm_device *dev = crtc->base.dev;
6328 struct drm_i915_private *dev_priv = dev->dev_private;
6329 uint32_t tmp;
6330
dc9e7dec
VS
6331 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6332 return;
6333
2fa2fe9a 6334 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6335 if (!(tmp & PFIT_ENABLE))
6336 return;
2fa2fe9a 6337
06922821 6338 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6339 if (INTEL_INFO(dev)->gen < 4) {
6340 if (crtc->pipe != PIPE_B)
6341 return;
2fa2fe9a
DV
6342 } else {
6343 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6344 return;
6345 }
6346
06922821 6347 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6348 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6349 if (INTEL_INFO(dev)->gen < 5)
6350 pipe_config->gmch_pfit.lvds_border_bits =
6351 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6352}
6353
acbec814
JB
6354static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6355 struct intel_crtc_config *pipe_config)
6356{
6357 struct drm_device *dev = crtc->base.dev;
6358 struct drm_i915_private *dev_priv = dev->dev_private;
6359 int pipe = pipe_config->cpu_transcoder;
6360 intel_clock_t clock;
6361 u32 mdiv;
662c6ecb 6362 int refclk = 100000;
acbec814 6363
f573de5a
SK
6364 /* In case of MIPI DPLL will not even be used */
6365 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6366 return;
6367
acbec814 6368 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6369 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6370 mutex_unlock(&dev_priv->dpio_lock);
6371
6372 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6373 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6374 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6375 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6376 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6377
f646628b 6378 vlv_clock(refclk, &clock);
acbec814 6379
f646628b
VS
6380 /* clock.dot is the fast clock */
6381 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6382}
6383
1ad292b5
JB
6384static void i9xx_get_plane_config(struct intel_crtc *crtc,
6385 struct intel_plane_config *plane_config)
6386{
6387 struct drm_device *dev = crtc->base.dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
6389 u32 val, base, offset;
6390 int pipe = crtc->pipe, plane = crtc->plane;
6391 int fourcc, pixel_format;
6392 int aligned_height;
6393
66e514c1
DA
6394 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6395 if (!crtc->base.primary->fb) {
1ad292b5
JB
6396 DRM_DEBUG_KMS("failed to alloc fb\n");
6397 return;
6398 }
6399
6400 val = I915_READ(DSPCNTR(plane));
6401
6402 if (INTEL_INFO(dev)->gen >= 4)
6403 if (val & DISPPLANE_TILED)
6404 plane_config->tiled = true;
6405
6406 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6407 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6408 crtc->base.primary->fb->pixel_format = fourcc;
6409 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6410 drm_format_plane_cpp(fourcc, 0) * 8;
6411
6412 if (INTEL_INFO(dev)->gen >= 4) {
6413 if (plane_config->tiled)
6414 offset = I915_READ(DSPTILEOFF(plane));
6415 else
6416 offset = I915_READ(DSPLINOFF(plane));
6417 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6418 } else {
6419 base = I915_READ(DSPADDR(plane));
6420 }
6421 plane_config->base = base;
6422
6423 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6424 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6425 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6426
6427 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6428 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6429
66e514c1 6430 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6431 plane_config->tiled);
6432
1267a26b
FF
6433 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6434 aligned_height);
1ad292b5
JB
6435
6436 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6437 pipe, plane, crtc->base.primary->fb->width,
6438 crtc->base.primary->fb->height,
6439 crtc->base.primary->fb->bits_per_pixel, base,
6440 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6441 plane_config->size);
6442
6443}
6444
70b23a98
VS
6445static void chv_crtc_clock_get(struct intel_crtc *crtc,
6446 struct intel_crtc_config *pipe_config)
6447{
6448 struct drm_device *dev = crtc->base.dev;
6449 struct drm_i915_private *dev_priv = dev->dev_private;
6450 int pipe = pipe_config->cpu_transcoder;
6451 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6452 intel_clock_t clock;
6453 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6454 int refclk = 100000;
6455
6456 mutex_lock(&dev_priv->dpio_lock);
6457 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6458 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6459 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6460 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6461 mutex_unlock(&dev_priv->dpio_lock);
6462
6463 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6464 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6465 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6466 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6467 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6468
6469 chv_clock(refclk, &clock);
6470
6471 /* clock.dot is the fast clock */
6472 pipe_config->port_clock = clock.dot / 5;
6473}
6474
0e8ffe1b
DV
6475static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6476 struct intel_crtc_config *pipe_config)
6477{
6478 struct drm_device *dev = crtc->base.dev;
6479 struct drm_i915_private *dev_priv = dev->dev_private;
6480 uint32_t tmp;
6481
f458ebbc
DV
6482 if (!intel_display_power_is_enabled(dev_priv,
6483 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
6484 return false;
6485
e143a21c 6486 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6487 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6488
0e8ffe1b
DV
6489 tmp = I915_READ(PIPECONF(crtc->pipe));
6490 if (!(tmp & PIPECONF_ENABLE))
6491 return false;
6492
42571aef
VS
6493 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6494 switch (tmp & PIPECONF_BPC_MASK) {
6495 case PIPECONF_6BPC:
6496 pipe_config->pipe_bpp = 18;
6497 break;
6498 case PIPECONF_8BPC:
6499 pipe_config->pipe_bpp = 24;
6500 break;
6501 case PIPECONF_10BPC:
6502 pipe_config->pipe_bpp = 30;
6503 break;
6504 default:
6505 break;
6506 }
6507 }
6508
b5a9fa09
DV
6509 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6510 pipe_config->limited_color_range = true;
6511
282740f7
VS
6512 if (INTEL_INFO(dev)->gen < 4)
6513 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6514
1bd1bd80
DV
6515 intel_get_pipe_timings(crtc, pipe_config);
6516
2fa2fe9a
DV
6517 i9xx_get_pfit_config(crtc, pipe_config);
6518
6c49f241
DV
6519 if (INTEL_INFO(dev)->gen >= 4) {
6520 tmp = I915_READ(DPLL_MD(crtc->pipe));
6521 pipe_config->pixel_multiplier =
6522 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6523 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6524 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6525 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6526 tmp = I915_READ(DPLL(crtc->pipe));
6527 pipe_config->pixel_multiplier =
6528 ((tmp & SDVO_MULTIPLIER_MASK)
6529 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6530 } else {
6531 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6532 * port and will be fixed up in the encoder->get_config
6533 * function. */
6534 pipe_config->pixel_multiplier = 1;
6535 }
8bcc2795
DV
6536 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6537 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6538 /*
6539 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6540 * on 830. Filter it out here so that we don't
6541 * report errors due to that.
6542 */
6543 if (IS_I830(dev))
6544 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6545
8bcc2795
DV
6546 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6547 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6548 } else {
6549 /* Mask out read-only status bits. */
6550 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6551 DPLL_PORTC_READY_MASK |
6552 DPLL_PORTB_READY_MASK);
8bcc2795 6553 }
6c49f241 6554
70b23a98
VS
6555 if (IS_CHERRYVIEW(dev))
6556 chv_crtc_clock_get(crtc, pipe_config);
6557 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6558 vlv_crtc_clock_get(crtc, pipe_config);
6559 else
6560 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6561
0e8ffe1b
DV
6562 return true;
6563}
6564
dde86e2d 6565static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6566{
6567 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6568 struct intel_encoder *encoder;
74cfd7ac 6569 u32 val, final;
13d83a67 6570 bool has_lvds = false;
199e5d79 6571 bool has_cpu_edp = false;
199e5d79 6572 bool has_panel = false;
99eb6a01
KP
6573 bool has_ck505 = false;
6574 bool can_ssc = false;
13d83a67
JB
6575
6576 /* We need to take the global config into account */
b2784e15 6577 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6578 switch (encoder->type) {
6579 case INTEL_OUTPUT_LVDS:
6580 has_panel = true;
6581 has_lvds = true;
6582 break;
6583 case INTEL_OUTPUT_EDP:
6584 has_panel = true;
2de6905f 6585 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6586 has_cpu_edp = true;
6587 break;
13d83a67
JB
6588 }
6589 }
6590
99eb6a01 6591 if (HAS_PCH_IBX(dev)) {
41aa3448 6592 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6593 can_ssc = has_ck505;
6594 } else {
6595 has_ck505 = false;
6596 can_ssc = true;
6597 }
6598
2de6905f
ID
6599 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6600 has_panel, has_lvds, has_ck505);
13d83a67
JB
6601
6602 /* Ironlake: try to setup display ref clock before DPLL
6603 * enabling. This is only under driver's control after
6604 * PCH B stepping, previous chipset stepping should be
6605 * ignoring this setting.
6606 */
74cfd7ac
CW
6607 val = I915_READ(PCH_DREF_CONTROL);
6608
6609 /* As we must carefully and slowly disable/enable each source in turn,
6610 * compute the final state we want first and check if we need to
6611 * make any changes at all.
6612 */
6613 final = val;
6614 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6615 if (has_ck505)
6616 final |= DREF_NONSPREAD_CK505_ENABLE;
6617 else
6618 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6619
6620 final &= ~DREF_SSC_SOURCE_MASK;
6621 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6622 final &= ~DREF_SSC1_ENABLE;
6623
6624 if (has_panel) {
6625 final |= DREF_SSC_SOURCE_ENABLE;
6626
6627 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6628 final |= DREF_SSC1_ENABLE;
6629
6630 if (has_cpu_edp) {
6631 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6632 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6633 else
6634 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6635 } else
6636 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6637 } else {
6638 final |= DREF_SSC_SOURCE_DISABLE;
6639 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6640 }
6641
6642 if (final == val)
6643 return;
6644
13d83a67 6645 /* Always enable nonspread source */
74cfd7ac 6646 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6647
99eb6a01 6648 if (has_ck505)
74cfd7ac 6649 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6650 else
74cfd7ac 6651 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6652
199e5d79 6653 if (has_panel) {
74cfd7ac
CW
6654 val &= ~DREF_SSC_SOURCE_MASK;
6655 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6656
199e5d79 6657 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6658 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6659 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6660 val |= DREF_SSC1_ENABLE;
e77166b5 6661 } else
74cfd7ac 6662 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6663
6664 /* Get SSC going before enabling the outputs */
74cfd7ac 6665 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6666 POSTING_READ(PCH_DREF_CONTROL);
6667 udelay(200);
6668
74cfd7ac 6669 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6670
6671 /* Enable CPU source on CPU attached eDP */
199e5d79 6672 if (has_cpu_edp) {
99eb6a01 6673 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6674 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6675 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6676 } else
74cfd7ac 6677 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6678 } else
74cfd7ac 6679 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6680
74cfd7ac 6681 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6682 POSTING_READ(PCH_DREF_CONTROL);
6683 udelay(200);
6684 } else {
6685 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6686
74cfd7ac 6687 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6688
6689 /* Turn off CPU output */
74cfd7ac 6690 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6691
74cfd7ac 6692 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6693 POSTING_READ(PCH_DREF_CONTROL);
6694 udelay(200);
6695
6696 /* Turn off the SSC source */
74cfd7ac
CW
6697 val &= ~DREF_SSC_SOURCE_MASK;
6698 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6699
6700 /* Turn off SSC1 */
74cfd7ac 6701 val &= ~DREF_SSC1_ENABLE;
199e5d79 6702
74cfd7ac 6703 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6704 POSTING_READ(PCH_DREF_CONTROL);
6705 udelay(200);
6706 }
74cfd7ac
CW
6707
6708 BUG_ON(val != final);
13d83a67
JB
6709}
6710
f31f2d55 6711static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6712{
f31f2d55 6713 uint32_t tmp;
dde86e2d 6714
0ff066a9
PZ
6715 tmp = I915_READ(SOUTH_CHICKEN2);
6716 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6717 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6718
0ff066a9
PZ
6719 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6720 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6721 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6722
0ff066a9
PZ
6723 tmp = I915_READ(SOUTH_CHICKEN2);
6724 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6725 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6726
0ff066a9
PZ
6727 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6728 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6729 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6730}
6731
6732/* WaMPhyProgramming:hsw */
6733static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6734{
6735 uint32_t tmp;
dde86e2d
PZ
6736
6737 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6738 tmp &= ~(0xFF << 24);
6739 tmp |= (0x12 << 24);
6740 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6741
dde86e2d
PZ
6742 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6743 tmp |= (1 << 11);
6744 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6745
6746 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6747 tmp |= (1 << 11);
6748 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6749
dde86e2d
PZ
6750 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6751 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6752 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6753
6754 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6755 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6756 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6757
0ff066a9
PZ
6758 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6759 tmp &= ~(7 << 13);
6760 tmp |= (5 << 13);
6761 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6762
0ff066a9
PZ
6763 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6764 tmp &= ~(7 << 13);
6765 tmp |= (5 << 13);
6766 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6767
6768 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6769 tmp &= ~0xFF;
6770 tmp |= 0x1C;
6771 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6772
6773 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6774 tmp &= ~0xFF;
6775 tmp |= 0x1C;
6776 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6777
6778 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6779 tmp &= ~(0xFF << 16);
6780 tmp |= (0x1C << 16);
6781 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6782
6783 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6784 tmp &= ~(0xFF << 16);
6785 tmp |= (0x1C << 16);
6786 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6787
0ff066a9
PZ
6788 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6789 tmp |= (1 << 27);
6790 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6791
0ff066a9
PZ
6792 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6793 tmp |= (1 << 27);
6794 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6795
0ff066a9
PZ
6796 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6797 tmp &= ~(0xF << 28);
6798 tmp |= (4 << 28);
6799 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6800
0ff066a9
PZ
6801 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6802 tmp &= ~(0xF << 28);
6803 tmp |= (4 << 28);
6804 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6805}
6806
2fa86a1f
PZ
6807/* Implements 3 different sequences from BSpec chapter "Display iCLK
6808 * Programming" based on the parameters passed:
6809 * - Sequence to enable CLKOUT_DP
6810 * - Sequence to enable CLKOUT_DP without spread
6811 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6812 */
6813static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6814 bool with_fdi)
f31f2d55
PZ
6815{
6816 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6817 uint32_t reg, tmp;
6818
6819 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6820 with_spread = true;
6821 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6822 with_fdi, "LP PCH doesn't have FDI\n"))
6823 with_fdi = false;
f31f2d55
PZ
6824
6825 mutex_lock(&dev_priv->dpio_lock);
6826
6827 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6828 tmp &= ~SBI_SSCCTL_DISABLE;
6829 tmp |= SBI_SSCCTL_PATHALT;
6830 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6831
6832 udelay(24);
6833
2fa86a1f
PZ
6834 if (with_spread) {
6835 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6836 tmp &= ~SBI_SSCCTL_PATHALT;
6837 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6838
2fa86a1f
PZ
6839 if (with_fdi) {
6840 lpt_reset_fdi_mphy(dev_priv);
6841 lpt_program_fdi_mphy(dev_priv);
6842 }
6843 }
dde86e2d 6844
2fa86a1f
PZ
6845 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6846 SBI_GEN0 : SBI_DBUFF0;
6847 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6848 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6849 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6850
6851 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6852}
6853
47701c3b
PZ
6854/* Sequence to disable CLKOUT_DP */
6855static void lpt_disable_clkout_dp(struct drm_device *dev)
6856{
6857 struct drm_i915_private *dev_priv = dev->dev_private;
6858 uint32_t reg, tmp;
6859
6860 mutex_lock(&dev_priv->dpio_lock);
6861
6862 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6863 SBI_GEN0 : SBI_DBUFF0;
6864 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6865 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6866 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6867
6868 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6869 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6870 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6871 tmp |= SBI_SSCCTL_PATHALT;
6872 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6873 udelay(32);
6874 }
6875 tmp |= SBI_SSCCTL_DISABLE;
6876 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6877 }
6878
6879 mutex_unlock(&dev_priv->dpio_lock);
6880}
6881
bf8fa3d3
PZ
6882static void lpt_init_pch_refclk(struct drm_device *dev)
6883{
bf8fa3d3
PZ
6884 struct intel_encoder *encoder;
6885 bool has_vga = false;
6886
b2784e15 6887 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6888 switch (encoder->type) {
6889 case INTEL_OUTPUT_ANALOG:
6890 has_vga = true;
6891 break;
6892 }
6893 }
6894
47701c3b
PZ
6895 if (has_vga)
6896 lpt_enable_clkout_dp(dev, true, true);
6897 else
6898 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6899}
6900
dde86e2d
PZ
6901/*
6902 * Initialize reference clocks when the driver loads
6903 */
6904void intel_init_pch_refclk(struct drm_device *dev)
6905{
6906 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6907 ironlake_init_pch_refclk(dev);
6908 else if (HAS_PCH_LPT(dev))
6909 lpt_init_pch_refclk(dev);
6910}
6911
d9d444cb
JB
6912static int ironlake_get_refclk(struct drm_crtc *crtc)
6913{
6914 struct drm_device *dev = crtc->dev;
6915 struct drm_i915_private *dev_priv = dev->dev_private;
6916 struct intel_encoder *encoder;
d9d444cb
JB
6917 int num_connectors = 0;
6918 bool is_lvds = false;
6919
6c2b7c12 6920 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6921 switch (encoder->type) {
6922 case INTEL_OUTPUT_LVDS:
6923 is_lvds = true;
6924 break;
d9d444cb
JB
6925 }
6926 num_connectors++;
6927 }
6928
6929 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6930 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6931 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6932 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6933 }
6934
6935 return 120000;
6936}
6937
6ff93609 6938static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6939{
c8203565 6940 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6942 int pipe = intel_crtc->pipe;
c8203565
PZ
6943 uint32_t val;
6944
78114071 6945 val = 0;
c8203565 6946
965e0c48 6947 switch (intel_crtc->config.pipe_bpp) {
c8203565 6948 case 18:
dfd07d72 6949 val |= PIPECONF_6BPC;
c8203565
PZ
6950 break;
6951 case 24:
dfd07d72 6952 val |= PIPECONF_8BPC;
c8203565
PZ
6953 break;
6954 case 30:
dfd07d72 6955 val |= PIPECONF_10BPC;
c8203565
PZ
6956 break;
6957 case 36:
dfd07d72 6958 val |= PIPECONF_12BPC;
c8203565
PZ
6959 break;
6960 default:
cc769b62
PZ
6961 /* Case prevented by intel_choose_pipe_bpp_dither. */
6962 BUG();
c8203565
PZ
6963 }
6964
d8b32247 6965 if (intel_crtc->config.dither)
c8203565
PZ
6966 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6967
6ff93609 6968 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6969 val |= PIPECONF_INTERLACED_ILK;
6970 else
6971 val |= PIPECONF_PROGRESSIVE;
6972
50f3b016 6973 if (intel_crtc->config.limited_color_range)
3685a8f3 6974 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6975
c8203565
PZ
6976 I915_WRITE(PIPECONF(pipe), val);
6977 POSTING_READ(PIPECONF(pipe));
6978}
6979
86d3efce
VS
6980/*
6981 * Set up the pipe CSC unit.
6982 *
6983 * Currently only full range RGB to limited range RGB conversion
6984 * is supported, but eventually this should handle various
6985 * RGB<->YCbCr scenarios as well.
6986 */
50f3b016 6987static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6988{
6989 struct drm_device *dev = crtc->dev;
6990 struct drm_i915_private *dev_priv = dev->dev_private;
6991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6992 int pipe = intel_crtc->pipe;
6993 uint16_t coeff = 0x7800; /* 1.0 */
6994
6995 /*
6996 * TODO: Check what kind of values actually come out of the pipe
6997 * with these coeff/postoff values and adjust to get the best
6998 * accuracy. Perhaps we even need to take the bpc value into
6999 * consideration.
7000 */
7001
50f3b016 7002 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7003 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7004
7005 /*
7006 * GY/GU and RY/RU should be the other way around according
7007 * to BSpec, but reality doesn't agree. Just set them up in
7008 * a way that results in the correct picture.
7009 */
7010 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7011 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7012
7013 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7014 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7015
7016 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7017 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7018
7019 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7020 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7021 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7022
7023 if (INTEL_INFO(dev)->gen > 6) {
7024 uint16_t postoff = 0;
7025
50f3b016 7026 if (intel_crtc->config.limited_color_range)
32cf0cb0 7027 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7028
7029 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7030 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7031 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7032
7033 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7034 } else {
7035 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7036
50f3b016 7037 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7038 mode |= CSC_BLACK_SCREEN_OFFSET;
7039
7040 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7041 }
7042}
7043
6ff93609 7044static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7045{
756f85cf
PZ
7046 struct drm_device *dev = crtc->dev;
7047 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7049 enum pipe pipe = intel_crtc->pipe;
3b117c8f 7050 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
7051 uint32_t val;
7052
3eff4faa 7053 val = 0;
ee2b0b38 7054
756f85cf 7055 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
7056 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7057
6ff93609 7058 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7059 val |= PIPECONF_INTERLACED_ILK;
7060 else
7061 val |= PIPECONF_PROGRESSIVE;
7062
702e7a56
PZ
7063 I915_WRITE(PIPECONF(cpu_transcoder), val);
7064 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7065
7066 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7067 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7068
3cdf122c 7069 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7070 val = 0;
7071
7072 switch (intel_crtc->config.pipe_bpp) {
7073 case 18:
7074 val |= PIPEMISC_DITHER_6_BPC;
7075 break;
7076 case 24:
7077 val |= PIPEMISC_DITHER_8_BPC;
7078 break;
7079 case 30:
7080 val |= PIPEMISC_DITHER_10_BPC;
7081 break;
7082 case 36:
7083 val |= PIPEMISC_DITHER_12_BPC;
7084 break;
7085 default:
7086 /* Case prevented by pipe_config_set_bpp. */
7087 BUG();
7088 }
7089
7090 if (intel_crtc->config.dither)
7091 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7092
7093 I915_WRITE(PIPEMISC(pipe), val);
7094 }
ee2b0b38
PZ
7095}
7096
6591c6e4 7097static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
7098 intel_clock_t *clock,
7099 bool *has_reduced_clock,
7100 intel_clock_t *reduced_clock)
7101{
7102 struct drm_device *dev = crtc->dev;
7103 struct drm_i915_private *dev_priv = dev->dev_private;
7104 struct intel_encoder *intel_encoder;
7105 int refclk;
d4906093 7106 const intel_limit_t *limit;
a16af721 7107 bool ret, is_lvds = false;
79e53945 7108
6591c6e4
PZ
7109 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7110 switch (intel_encoder->type) {
79e53945
JB
7111 case INTEL_OUTPUT_LVDS:
7112 is_lvds = true;
7113 break;
79e53945
JB
7114 }
7115 }
7116
d9d444cb 7117 refclk = ironlake_get_refclk(crtc);
79e53945 7118
d4906093
ML
7119 /*
7120 * Returns a set of divisors for the desired target clock with the given
7121 * refclk, or FALSE. The returned values represent the clock equation:
7122 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7123 */
1b894b59 7124 limit = intel_limit(crtc, refclk);
ff9a6750
DV
7125 ret = dev_priv->display.find_dpll(limit, crtc,
7126 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 7127 refclk, NULL, clock);
6591c6e4
PZ
7128 if (!ret)
7129 return false;
cda4b7d3 7130
ddc9003c 7131 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7132 /*
7133 * Ensure we match the reduced clock's P to the target clock.
7134 * If the clocks don't match, we can't switch the display clock
7135 * by using the FP0/FP1. In such case we will disable the LVDS
7136 * downclock feature.
7137 */
ee9300bb
DV
7138 *has_reduced_clock =
7139 dev_priv->display.find_dpll(limit, crtc,
7140 dev_priv->lvds_downclock,
7141 refclk, clock,
7142 reduced_clock);
652c393a 7143 }
61e9653f 7144
6591c6e4
PZ
7145 return true;
7146}
7147
d4b1931c
PZ
7148int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7149{
7150 /*
7151 * Account for spread spectrum to avoid
7152 * oversubscribing the link. Max center spread
7153 * is 2.5%; use 5% for safety's sake.
7154 */
7155 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7156 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7157}
7158
7429e9d4 7159static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7160{
7429e9d4 7161 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7162}
7163
de13a2e3 7164static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7165 u32 *fp,
9a7c7890 7166 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7167{
de13a2e3 7168 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7169 struct drm_device *dev = crtc->dev;
7170 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7171 struct intel_encoder *intel_encoder;
7172 uint32_t dpll;
6cc5f341 7173 int factor, num_connectors = 0;
09ede541 7174 bool is_lvds = false, is_sdvo = false;
79e53945 7175
de13a2e3
PZ
7176 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7177 switch (intel_encoder->type) {
79e53945
JB
7178 case INTEL_OUTPUT_LVDS:
7179 is_lvds = true;
7180 break;
7181 case INTEL_OUTPUT_SDVO:
7d57382e 7182 case INTEL_OUTPUT_HDMI:
79e53945 7183 is_sdvo = true;
79e53945 7184 break;
79e53945 7185 }
43565a06 7186
c751ce4f 7187 num_connectors++;
79e53945 7188 }
79e53945 7189
c1858123 7190 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7191 factor = 21;
7192 if (is_lvds) {
7193 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7194 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7195 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7196 factor = 25;
09ede541 7197 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7198 factor = 20;
c1858123 7199
7429e9d4 7200 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7201 *fp |= FP_CB_TUNE;
2c07245f 7202
9a7c7890
DV
7203 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7204 *fp2 |= FP_CB_TUNE;
7205
5eddb70b 7206 dpll = 0;
2c07245f 7207
a07d6787
EA
7208 if (is_lvds)
7209 dpll |= DPLLB_MODE_LVDS;
7210 else
7211 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7212
ef1b460d
DV
7213 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7214 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7215
7216 if (is_sdvo)
4a33e48d 7217 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7218 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7219 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7220
a07d6787 7221 /* compute bitmask from p1 value */
7429e9d4 7222 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7223 /* also FPA1 */
7429e9d4 7224 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7225
7429e9d4 7226 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7227 case 5:
7228 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7229 break;
7230 case 7:
7231 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7232 break;
7233 case 10:
7234 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7235 break;
7236 case 14:
7237 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7238 break;
79e53945
JB
7239 }
7240
b4c09f3b 7241 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7242 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7243 else
7244 dpll |= PLL_REF_INPUT_DREFCLK;
7245
959e16d6 7246 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7247}
7248
7249static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7250 int x, int y,
7251 struct drm_framebuffer *fb)
7252{
7253 struct drm_device *dev = crtc->dev;
de13a2e3 7254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7255 int num_connectors = 0;
7256 intel_clock_t clock, reduced_clock;
cbbab5bd 7257 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7258 bool ok, has_reduced_clock = false;
8b47047b 7259 bool is_lvds = false;
de13a2e3 7260 struct intel_encoder *encoder;
e2b78267 7261 struct intel_shared_dpll *pll;
de13a2e3
PZ
7262
7263 for_each_encoder_on_crtc(dev, crtc, encoder) {
7264 switch (encoder->type) {
7265 case INTEL_OUTPUT_LVDS:
7266 is_lvds = true;
7267 break;
de13a2e3
PZ
7268 }
7269
7270 num_connectors++;
a07d6787 7271 }
79e53945 7272
5dc5298b
PZ
7273 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7274 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7275
ff9a6750 7276 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7277 &has_reduced_clock, &reduced_clock);
ee9300bb 7278 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7279 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7280 return -EINVAL;
79e53945 7281 }
f47709a9
DV
7282 /* Compat-code for transition, will disappear. */
7283 if (!intel_crtc->config.clock_set) {
7284 intel_crtc->config.dpll.n = clock.n;
7285 intel_crtc->config.dpll.m1 = clock.m1;
7286 intel_crtc->config.dpll.m2 = clock.m2;
7287 intel_crtc->config.dpll.p1 = clock.p1;
7288 intel_crtc->config.dpll.p2 = clock.p2;
7289 }
79e53945 7290
5dc5298b 7291 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7292 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7293 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7294 if (has_reduced_clock)
7429e9d4 7295 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7296
7429e9d4 7297 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7298 &fp, &reduced_clock,
7299 has_reduced_clock ? &fp2 : NULL);
7300
959e16d6 7301 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7302 intel_crtc->config.dpll_hw_state.fp0 = fp;
7303 if (has_reduced_clock)
7304 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7305 else
7306 intel_crtc->config.dpll_hw_state.fp1 = fp;
7307
b89a1d39 7308 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7309 if (pll == NULL) {
84f44ce7 7310 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7311 pipe_name(intel_crtc->pipe));
4b645f14
JB
7312 return -EINVAL;
7313 }
ee7b9f93 7314 } else
e72f9fbf 7315 intel_put_shared_dpll(intel_crtc);
79e53945 7316
d330a953 7317 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7318 intel_crtc->lowfreq_avail = true;
7319 else
7320 intel_crtc->lowfreq_avail = false;
e2b78267 7321
c8f7a0db 7322 return 0;
79e53945
JB
7323}
7324
eb14cb74
VS
7325static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7326 struct intel_link_m_n *m_n)
7327{
7328 struct drm_device *dev = crtc->base.dev;
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 enum pipe pipe = crtc->pipe;
7331
7332 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7333 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7334 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7335 & ~TU_SIZE_MASK;
7336 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7337 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7338 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7339}
7340
7341static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7342 enum transcoder transcoder,
b95af8be
VK
7343 struct intel_link_m_n *m_n,
7344 struct intel_link_m_n *m2_n2)
72419203
DV
7345{
7346 struct drm_device *dev = crtc->base.dev;
7347 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7348 enum pipe pipe = crtc->pipe;
72419203 7349
eb14cb74
VS
7350 if (INTEL_INFO(dev)->gen >= 5) {
7351 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7352 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7353 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7354 & ~TU_SIZE_MASK;
7355 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7356 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7357 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7358 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7359 * gen < 8) and if DRRS is supported (to make sure the
7360 * registers are not unnecessarily read).
7361 */
7362 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7363 crtc->config.has_drrs) {
7364 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7365 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7366 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7367 & ~TU_SIZE_MASK;
7368 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7369 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7370 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7371 }
eb14cb74
VS
7372 } else {
7373 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7374 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7375 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7376 & ~TU_SIZE_MASK;
7377 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7378 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7379 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7380 }
7381}
7382
7383void intel_dp_get_m_n(struct intel_crtc *crtc,
7384 struct intel_crtc_config *pipe_config)
7385{
7386 if (crtc->config.has_pch_encoder)
7387 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7388 else
7389 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7390 &pipe_config->dp_m_n,
7391 &pipe_config->dp_m2_n2);
eb14cb74 7392}
72419203 7393
eb14cb74
VS
7394static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7395 struct intel_crtc_config *pipe_config)
7396{
7397 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7398 &pipe_config->fdi_m_n, NULL);
72419203
DV
7399}
7400
2fa2fe9a
DV
7401static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7402 struct intel_crtc_config *pipe_config)
7403{
7404 struct drm_device *dev = crtc->base.dev;
7405 struct drm_i915_private *dev_priv = dev->dev_private;
7406 uint32_t tmp;
7407
7408 tmp = I915_READ(PF_CTL(crtc->pipe));
7409
7410 if (tmp & PF_ENABLE) {
fd4daa9c 7411 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7412 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7413 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7414
7415 /* We currently do not free assignements of panel fitters on
7416 * ivb/hsw (since we don't use the higher upscaling modes which
7417 * differentiates them) so just WARN about this case for now. */
7418 if (IS_GEN7(dev)) {
7419 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7420 PF_PIPE_SEL_IVB(crtc->pipe));
7421 }
2fa2fe9a 7422 }
79e53945
JB
7423}
7424
4c6baa59
JB
7425static void ironlake_get_plane_config(struct intel_crtc *crtc,
7426 struct intel_plane_config *plane_config)
7427{
7428 struct drm_device *dev = crtc->base.dev;
7429 struct drm_i915_private *dev_priv = dev->dev_private;
7430 u32 val, base, offset;
7431 int pipe = crtc->pipe, plane = crtc->plane;
7432 int fourcc, pixel_format;
7433 int aligned_height;
7434
66e514c1
DA
7435 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7436 if (!crtc->base.primary->fb) {
4c6baa59
JB
7437 DRM_DEBUG_KMS("failed to alloc fb\n");
7438 return;
7439 }
7440
7441 val = I915_READ(DSPCNTR(plane));
7442
7443 if (INTEL_INFO(dev)->gen >= 4)
7444 if (val & DISPPLANE_TILED)
7445 plane_config->tiled = true;
7446
7447 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7448 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7449 crtc->base.primary->fb->pixel_format = fourcc;
7450 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7451 drm_format_plane_cpp(fourcc, 0) * 8;
7452
7453 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7454 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7455 offset = I915_READ(DSPOFFSET(plane));
7456 } else {
7457 if (plane_config->tiled)
7458 offset = I915_READ(DSPTILEOFF(plane));
7459 else
7460 offset = I915_READ(DSPLINOFF(plane));
7461 }
7462 plane_config->base = base;
7463
7464 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7465 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7466 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7467
7468 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7469 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7470
66e514c1 7471 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7472 plane_config->tiled);
7473
1267a26b
FF
7474 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7475 aligned_height);
4c6baa59
JB
7476
7477 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7478 pipe, plane, crtc->base.primary->fb->width,
7479 crtc->base.primary->fb->height,
7480 crtc->base.primary->fb->bits_per_pixel, base,
7481 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7482 plane_config->size);
7483}
7484
0e8ffe1b
DV
7485static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7486 struct intel_crtc_config *pipe_config)
7487{
7488 struct drm_device *dev = crtc->base.dev;
7489 struct drm_i915_private *dev_priv = dev->dev_private;
7490 uint32_t tmp;
7491
f458ebbc
DV
7492 if (!intel_display_power_is_enabled(dev_priv,
7493 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
7494 return false;
7495
e143a21c 7496 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7497 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7498
0e8ffe1b
DV
7499 tmp = I915_READ(PIPECONF(crtc->pipe));
7500 if (!(tmp & PIPECONF_ENABLE))
7501 return false;
7502
42571aef
VS
7503 switch (tmp & PIPECONF_BPC_MASK) {
7504 case PIPECONF_6BPC:
7505 pipe_config->pipe_bpp = 18;
7506 break;
7507 case PIPECONF_8BPC:
7508 pipe_config->pipe_bpp = 24;
7509 break;
7510 case PIPECONF_10BPC:
7511 pipe_config->pipe_bpp = 30;
7512 break;
7513 case PIPECONF_12BPC:
7514 pipe_config->pipe_bpp = 36;
7515 break;
7516 default:
7517 break;
7518 }
7519
b5a9fa09
DV
7520 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7521 pipe_config->limited_color_range = true;
7522
ab9412ba 7523 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7524 struct intel_shared_dpll *pll;
7525
88adfff1
DV
7526 pipe_config->has_pch_encoder = true;
7527
627eb5a3
DV
7528 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7529 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7530 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7531
7532 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7533
c0d43d62 7534 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7535 pipe_config->shared_dpll =
7536 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7537 } else {
7538 tmp = I915_READ(PCH_DPLL_SEL);
7539 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7540 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7541 else
7542 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7543 }
66e985c0
DV
7544
7545 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7546
7547 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7548 &pipe_config->dpll_hw_state));
c93f54cf
DV
7549
7550 tmp = pipe_config->dpll_hw_state.dpll;
7551 pipe_config->pixel_multiplier =
7552 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7553 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7554
7555 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7556 } else {
7557 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7558 }
7559
1bd1bd80
DV
7560 intel_get_pipe_timings(crtc, pipe_config);
7561
2fa2fe9a
DV
7562 ironlake_get_pfit_config(crtc, pipe_config);
7563
0e8ffe1b
DV
7564 return true;
7565}
7566
be256dc7
PZ
7567static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7568{
7569 struct drm_device *dev = dev_priv->dev;
be256dc7 7570 struct intel_crtc *crtc;
be256dc7 7571
d3fcc808 7572 for_each_intel_crtc(dev, crtc)
798183c5 7573 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7574 pipe_name(crtc->pipe));
7575
7576 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7577 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7578 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7579 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7580 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7581 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7582 "CPU PWM1 enabled\n");
c5107b87
PZ
7583 if (IS_HASWELL(dev))
7584 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7585 "CPU PWM2 enabled\n");
be256dc7
PZ
7586 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7587 "PCH PWM1 enabled\n");
7588 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7589 "Utility pin enabled\n");
7590 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7591
9926ada1
PZ
7592 /*
7593 * In theory we can still leave IRQs enabled, as long as only the HPD
7594 * interrupts remain enabled. We used to check for that, but since it's
7595 * gen-specific and since we only disable LCPLL after we fully disable
7596 * the interrupts, the check below should be enough.
7597 */
9df7575f 7598 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7599}
7600
9ccd5aeb
PZ
7601static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7602{
7603 struct drm_device *dev = dev_priv->dev;
7604
7605 if (IS_HASWELL(dev))
7606 return I915_READ(D_COMP_HSW);
7607 else
7608 return I915_READ(D_COMP_BDW);
7609}
7610
3c4c9b81
PZ
7611static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7612{
7613 struct drm_device *dev = dev_priv->dev;
7614
7615 if (IS_HASWELL(dev)) {
7616 mutex_lock(&dev_priv->rps.hw_lock);
7617 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7618 val))
f475dadf 7619 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7620 mutex_unlock(&dev_priv->rps.hw_lock);
7621 } else {
9ccd5aeb
PZ
7622 I915_WRITE(D_COMP_BDW, val);
7623 POSTING_READ(D_COMP_BDW);
3c4c9b81 7624 }
be256dc7
PZ
7625}
7626
7627/*
7628 * This function implements pieces of two sequences from BSpec:
7629 * - Sequence for display software to disable LCPLL
7630 * - Sequence for display software to allow package C8+
7631 * The steps implemented here are just the steps that actually touch the LCPLL
7632 * register. Callers should take care of disabling all the display engine
7633 * functions, doing the mode unset, fixing interrupts, etc.
7634 */
6ff58d53
PZ
7635static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7636 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7637{
7638 uint32_t val;
7639
7640 assert_can_disable_lcpll(dev_priv);
7641
7642 val = I915_READ(LCPLL_CTL);
7643
7644 if (switch_to_fclk) {
7645 val |= LCPLL_CD_SOURCE_FCLK;
7646 I915_WRITE(LCPLL_CTL, val);
7647
7648 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7649 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7650 DRM_ERROR("Switching to FCLK failed\n");
7651
7652 val = I915_READ(LCPLL_CTL);
7653 }
7654
7655 val |= LCPLL_PLL_DISABLE;
7656 I915_WRITE(LCPLL_CTL, val);
7657 POSTING_READ(LCPLL_CTL);
7658
7659 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7660 DRM_ERROR("LCPLL still locked\n");
7661
9ccd5aeb 7662 val = hsw_read_dcomp(dev_priv);
be256dc7 7663 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7664 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7665 ndelay(100);
7666
9ccd5aeb
PZ
7667 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7668 1))
be256dc7
PZ
7669 DRM_ERROR("D_COMP RCOMP still in progress\n");
7670
7671 if (allow_power_down) {
7672 val = I915_READ(LCPLL_CTL);
7673 val |= LCPLL_POWER_DOWN_ALLOW;
7674 I915_WRITE(LCPLL_CTL, val);
7675 POSTING_READ(LCPLL_CTL);
7676 }
7677}
7678
7679/*
7680 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7681 * source.
7682 */
6ff58d53 7683static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7684{
7685 uint32_t val;
7686
7687 val = I915_READ(LCPLL_CTL);
7688
7689 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7690 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7691 return;
7692
a8a8bd54
PZ
7693 /*
7694 * Make sure we're not on PC8 state before disabling PC8, otherwise
7695 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7696 *
7697 * The other problem is that hsw_restore_lcpll() is called as part of
7698 * the runtime PM resume sequence, so we can't just call
7699 * gen6_gt_force_wake_get() because that function calls
7700 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7701 * while we are on the resume sequence. So to solve this problem we have
7702 * to call special forcewake code that doesn't touch runtime PM and
7703 * doesn't enable the forcewake delayed work.
7704 */
d2e40e27 7705 spin_lock_irq(&dev_priv->uncore.lock);
a8a8bd54
PZ
7706 if (dev_priv->uncore.forcewake_count++ == 0)
7707 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
d2e40e27 7708 spin_unlock_irq(&dev_priv->uncore.lock);
215733fa 7709
be256dc7
PZ
7710 if (val & LCPLL_POWER_DOWN_ALLOW) {
7711 val &= ~LCPLL_POWER_DOWN_ALLOW;
7712 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7713 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7714 }
7715
9ccd5aeb 7716 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7717 val |= D_COMP_COMP_FORCE;
7718 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7719 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7720
7721 val = I915_READ(LCPLL_CTL);
7722 val &= ~LCPLL_PLL_DISABLE;
7723 I915_WRITE(LCPLL_CTL, val);
7724
7725 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7726 DRM_ERROR("LCPLL not locked yet\n");
7727
7728 if (val & LCPLL_CD_SOURCE_FCLK) {
7729 val = I915_READ(LCPLL_CTL);
7730 val &= ~LCPLL_CD_SOURCE_FCLK;
7731 I915_WRITE(LCPLL_CTL, val);
7732
7733 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7734 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7735 DRM_ERROR("Switching back to LCPLL failed\n");
7736 }
215733fa 7737
a8a8bd54 7738 /* See the big comment above. */
d2e40e27 7739 spin_lock_irq(&dev_priv->uncore.lock);
a8a8bd54
PZ
7740 if (--dev_priv->uncore.forcewake_count == 0)
7741 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
d2e40e27 7742 spin_unlock_irq(&dev_priv->uncore.lock);
be256dc7
PZ
7743}
7744
765dab67
PZ
7745/*
7746 * Package states C8 and deeper are really deep PC states that can only be
7747 * reached when all the devices on the system allow it, so even if the graphics
7748 * device allows PC8+, it doesn't mean the system will actually get to these
7749 * states. Our driver only allows PC8+ when going into runtime PM.
7750 *
7751 * The requirements for PC8+ are that all the outputs are disabled, the power
7752 * well is disabled and most interrupts are disabled, and these are also
7753 * requirements for runtime PM. When these conditions are met, we manually do
7754 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7755 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7756 * hang the machine.
7757 *
7758 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7759 * the state of some registers, so when we come back from PC8+ we need to
7760 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7761 * need to take care of the registers kept by RC6. Notice that this happens even
7762 * if we don't put the device in PCI D3 state (which is what currently happens
7763 * because of the runtime PM support).
7764 *
7765 * For more, read "Display Sequences for Package C8" on the hardware
7766 * documentation.
7767 */
a14cb6fc 7768void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7769{
c67a470b
PZ
7770 struct drm_device *dev = dev_priv->dev;
7771 uint32_t val;
7772
c67a470b
PZ
7773 DRM_DEBUG_KMS("Enabling package C8+\n");
7774
c67a470b
PZ
7775 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7776 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7777 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7778 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7779 }
7780
7781 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7782 hsw_disable_lcpll(dev_priv, true, true);
7783}
7784
a14cb6fc 7785void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7786{
7787 struct drm_device *dev = dev_priv->dev;
7788 uint32_t val;
7789
c67a470b
PZ
7790 DRM_DEBUG_KMS("Disabling package C8+\n");
7791
7792 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7793 lpt_init_pch_refclk(dev);
7794
7795 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7796 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7797 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7798 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7799 }
7800
7801 intel_prepare_ddi(dev);
c67a470b
PZ
7802}
7803
9a952a0d
PZ
7804static void snb_modeset_global_resources(struct drm_device *dev)
7805{
7806 modeset_update_crtc_power_domains(dev);
7807}
7808
4f074129
ID
7809static void haswell_modeset_global_resources(struct drm_device *dev)
7810{
da723569 7811 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7812}
7813
09b4ddf9 7814static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7815 int x, int y,
7816 struct drm_framebuffer *fb)
7817{
09b4ddf9 7818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7819
566b734a 7820 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7821 return -EINVAL;
716c2e55 7822
644cef34
DV
7823 intel_crtc->lowfreq_avail = false;
7824
c8f7a0db 7825 return 0;
79e53945
JB
7826}
7827
7d2c8175
DL
7828static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7829 enum port port,
7830 struct intel_crtc_config *pipe_config)
7831{
7832 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7833
7834 switch (pipe_config->ddi_pll_sel) {
7835 case PORT_CLK_SEL_WRPLL1:
7836 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7837 break;
7838 case PORT_CLK_SEL_WRPLL2:
7839 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7840 break;
7841 }
7842}
7843
26804afd
DV
7844static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7845 struct intel_crtc_config *pipe_config)
7846{
7847 struct drm_device *dev = crtc->base.dev;
7848 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7849 struct intel_shared_dpll *pll;
26804afd
DV
7850 enum port port;
7851 uint32_t tmp;
7852
7853 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7854
7855 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7856
7d2c8175 7857 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7858
d452c5b6
DV
7859 if (pipe_config->shared_dpll >= 0) {
7860 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7861
7862 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7863 &pipe_config->dpll_hw_state));
7864 }
7865
26804afd
DV
7866 /*
7867 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7868 * DDI E. So just check whether this pipe is wired to DDI E and whether
7869 * the PCH transcoder is on.
7870 */
ca370455
DL
7871 if (INTEL_INFO(dev)->gen < 9 &&
7872 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
7873 pipe_config->has_pch_encoder = true;
7874
7875 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7876 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7877 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7878
7879 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7880 }
7881}
7882
0e8ffe1b
DV
7883static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7884 struct intel_crtc_config *pipe_config)
7885{
7886 struct drm_device *dev = crtc->base.dev;
7887 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7888 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7889 uint32_t tmp;
7890
f458ebbc 7891 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
7892 POWER_DOMAIN_PIPE(crtc->pipe)))
7893 return false;
7894
e143a21c 7895 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7896 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7897
eccb140b
DV
7898 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7899 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7900 enum pipe trans_edp_pipe;
7901 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7902 default:
7903 WARN(1, "unknown pipe linked to edp transcoder\n");
7904 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7905 case TRANS_DDI_EDP_INPUT_A_ON:
7906 trans_edp_pipe = PIPE_A;
7907 break;
7908 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7909 trans_edp_pipe = PIPE_B;
7910 break;
7911 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7912 trans_edp_pipe = PIPE_C;
7913 break;
7914 }
7915
7916 if (trans_edp_pipe == crtc->pipe)
7917 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7918 }
7919
f458ebbc 7920 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 7921 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7922 return false;
7923
eccb140b 7924 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7925 if (!(tmp & PIPECONF_ENABLE))
7926 return false;
7927
26804afd 7928 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7929
1bd1bd80
DV
7930 intel_get_pipe_timings(crtc, pipe_config);
7931
2fa2fe9a 7932 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
f458ebbc 7933 if (intel_display_power_is_enabled(dev_priv, pfit_domain))
2fa2fe9a 7934 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7935
e59150dc
JB
7936 if (IS_HASWELL(dev))
7937 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7938 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7939
6c49f241
DV
7940 pipe_config->pixel_multiplier = 1;
7941
0e8ffe1b
DV
7942 return true;
7943}
7944
1a91510d
JN
7945static struct {
7946 int clock;
7947 u32 config;
7948} hdmi_audio_clock[] = {
7949 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7950 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7951 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7952 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7953 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7954 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7955 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7956 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7957 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7958 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7959};
7960
7961/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7962static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7963{
7964 int i;
7965
7966 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7967 if (mode->clock == hdmi_audio_clock[i].clock)
7968 break;
7969 }
7970
7971 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7972 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7973 i = 1;
7974 }
7975
7976 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7977 hdmi_audio_clock[i].clock,
7978 hdmi_audio_clock[i].config);
7979
7980 return hdmi_audio_clock[i].config;
7981}
7982
3a9627f4
WF
7983static bool intel_eld_uptodate(struct drm_connector *connector,
7984 int reg_eldv, uint32_t bits_eldv,
7985 int reg_elda, uint32_t bits_elda,
7986 int reg_edid)
7987{
7988 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7989 uint8_t *eld = connector->eld;
7990 uint32_t i;
7991
7992 i = I915_READ(reg_eldv);
7993 i &= bits_eldv;
7994
7995 if (!eld[0])
7996 return !i;
7997
7998 if (!i)
7999 return false;
8000
8001 i = I915_READ(reg_elda);
8002 i &= ~bits_elda;
8003 I915_WRITE(reg_elda, i);
8004
8005 for (i = 0; i < eld[2]; i++)
8006 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
8007 return false;
8008
8009 return true;
8010}
8011
e0dac65e 8012static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
8013 struct drm_crtc *crtc,
8014 struct drm_display_mode *mode)
e0dac65e
WF
8015{
8016 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8017 uint8_t *eld = connector->eld;
8018 uint32_t eldv;
8019 uint32_t len;
8020 uint32_t i;
8021
8022 i = I915_READ(G4X_AUD_VID_DID);
8023
8024 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
8025 eldv = G4X_ELDV_DEVCL_DEVBLC;
8026 else
8027 eldv = G4X_ELDV_DEVCTG;
8028
3a9627f4
WF
8029 if (intel_eld_uptodate(connector,
8030 G4X_AUD_CNTL_ST, eldv,
8031 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
8032 G4X_HDMIW_HDMIEDID))
8033 return;
8034
e0dac65e
WF
8035 i = I915_READ(G4X_AUD_CNTL_ST);
8036 i &= ~(eldv | G4X_ELD_ADDR);
8037 len = (i >> 9) & 0x1f; /* ELD buffer size */
8038 I915_WRITE(G4X_AUD_CNTL_ST, i);
8039
8040 if (!eld[0])
8041 return;
8042
8043 len = min_t(uint8_t, eld[2], len);
8044 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8045 for (i = 0; i < len; i++)
8046 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
8047
8048 i = I915_READ(G4X_AUD_CNTL_ST);
8049 i |= eldv;
8050 I915_WRITE(G4X_AUD_CNTL_ST, i);
8051}
8052
83358c85 8053static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
8054 struct drm_crtc *crtc,
8055 struct drm_display_mode *mode)
83358c85
WX
8056{
8057 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8058 uint8_t *eld = connector->eld;
83358c85
WX
8059 uint32_t eldv;
8060 uint32_t i;
8061 int len;
8062 int pipe = to_intel_crtc(crtc)->pipe;
8063 int tmp;
8064
8065 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
8066 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
8067 int aud_config = HSW_AUD_CFG(pipe);
8068 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
8069
83358c85
WX
8070 /* Audio output enable */
8071 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
8072 tmp = I915_READ(aud_cntrl_st2);
8073 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
8074 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 8075 POSTING_READ(aud_cntrl_st2);
83358c85 8076
c7905792 8077 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
8078
8079 /* Set ELD valid state */
8080 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8081 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
8082 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
8083 I915_WRITE(aud_cntrl_st2, tmp);
8084 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8085 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
8086
8087 /* Enable HDMI mode */
8088 tmp = I915_READ(aud_config);
7e7cb34f 8089 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
8090 /* clear N_programing_enable and N_value_index */
8091 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8092 I915_WRITE(aud_config, tmp);
8093
8094 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8095
8096 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8097
8098 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8099 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8100 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8101 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8102 } else {
8103 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8104 }
83358c85
WX
8105
8106 if (intel_eld_uptodate(connector,
8107 aud_cntrl_st2, eldv,
8108 aud_cntl_st, IBX_ELD_ADDRESS,
8109 hdmiw_hdmiedid))
8110 return;
8111
8112 i = I915_READ(aud_cntrl_st2);
8113 i &= ~eldv;
8114 I915_WRITE(aud_cntrl_st2, i);
8115
8116 if (!eld[0])
8117 return;
8118
8119 i = I915_READ(aud_cntl_st);
8120 i &= ~IBX_ELD_ADDRESS;
8121 I915_WRITE(aud_cntl_st, i);
8122 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8123 DRM_DEBUG_DRIVER("port num:%d\n", i);
8124
8125 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8126 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8127 for (i = 0; i < len; i++)
8128 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8129
8130 i = I915_READ(aud_cntrl_st2);
8131 i |= eldv;
8132 I915_WRITE(aud_cntrl_st2, i);
8133
8134}
8135
e0dac65e 8136static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
8137 struct drm_crtc *crtc,
8138 struct drm_display_mode *mode)
e0dac65e
WF
8139{
8140 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8141 uint8_t *eld = connector->eld;
8142 uint32_t eldv;
8143 uint32_t i;
8144 int len;
8145 int hdmiw_hdmiedid;
b6daa025 8146 int aud_config;
e0dac65e
WF
8147 int aud_cntl_st;
8148 int aud_cntrl_st2;
9b138a83 8149 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 8150
b3f33cbf 8151 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
8152 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8153 aud_config = IBX_AUD_CFG(pipe);
8154 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8155 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8156 } else if (IS_VALLEYVIEW(connector->dev)) {
8157 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8158 aud_config = VLV_AUD_CFG(pipe);
8159 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8160 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8161 } else {
9b138a83
WX
8162 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8163 aud_config = CPT_AUD_CFG(pipe);
8164 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8165 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8166 }
8167
9b138a83 8168 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8169
9ca2fe73
ML
8170 if (IS_VALLEYVIEW(connector->dev)) {
8171 struct intel_encoder *intel_encoder;
8172 struct intel_digital_port *intel_dig_port;
8173
8174 intel_encoder = intel_attached_encoder(connector);
8175 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8176 i = intel_dig_port->port;
8177 } else {
8178 i = I915_READ(aud_cntl_st);
8179 i = (i >> 29) & DIP_PORT_SEL_MASK;
8180 /* DIP_Port_Select, 0x1 = PortB */
8181 }
8182
e0dac65e
WF
8183 if (!i) {
8184 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8185 /* operate blindly on all ports */
1202b4c6
WF
8186 eldv = IBX_ELD_VALIDB;
8187 eldv |= IBX_ELD_VALIDB << 4;
8188 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8189 } else {
2582a850 8190 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8191 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8192 }
8193
3a9627f4
WF
8194 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8195 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8196 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8197 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8198 } else {
8199 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8200 }
e0dac65e 8201
3a9627f4
WF
8202 if (intel_eld_uptodate(connector,
8203 aud_cntrl_st2, eldv,
8204 aud_cntl_st, IBX_ELD_ADDRESS,
8205 hdmiw_hdmiedid))
8206 return;
8207
e0dac65e
WF
8208 i = I915_READ(aud_cntrl_st2);
8209 i &= ~eldv;
8210 I915_WRITE(aud_cntrl_st2, i);
8211
8212 if (!eld[0])
8213 return;
8214
e0dac65e 8215 i = I915_READ(aud_cntl_st);
1202b4c6 8216 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8217 I915_WRITE(aud_cntl_st, i);
8218
8219 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8220 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8221 for (i = 0; i < len; i++)
8222 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8223
8224 i = I915_READ(aud_cntrl_st2);
8225 i |= eldv;
8226 I915_WRITE(aud_cntrl_st2, i);
8227}
8228
8229void intel_write_eld(struct drm_encoder *encoder,
8230 struct drm_display_mode *mode)
8231{
8232 struct drm_crtc *crtc = encoder->crtc;
8233 struct drm_connector *connector;
8234 struct drm_device *dev = encoder->dev;
8235 struct drm_i915_private *dev_priv = dev->dev_private;
8236
8237 connector = drm_select_eld(encoder, mode);
8238 if (!connector)
8239 return;
8240
8241 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8242 connector->base.id,
c23cc417 8243 connector->name,
e0dac65e 8244 connector->encoder->base.id,
8e329a03 8245 connector->encoder->name);
e0dac65e
WF
8246
8247 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8248
8249 if (dev_priv->display.write_eld)
34427052 8250 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8251}
8252
560b85bb
CW
8253static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8254{
8255 struct drm_device *dev = crtc->dev;
8256 struct drm_i915_private *dev_priv = dev->dev_private;
8257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8258 uint32_t cntl = 0, size = 0;
560b85bb 8259
dc41c154
VS
8260 if (base) {
8261 unsigned int width = intel_crtc->cursor_width;
8262 unsigned int height = intel_crtc->cursor_height;
8263 unsigned int stride = roundup_pow_of_two(width) * 4;
8264
8265 switch (stride) {
8266 default:
8267 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8268 width, stride);
8269 stride = 256;
8270 /* fallthrough */
8271 case 256:
8272 case 512:
8273 case 1024:
8274 case 2048:
8275 break;
4b0e333e
CW
8276 }
8277
dc41c154
VS
8278 cntl |= CURSOR_ENABLE |
8279 CURSOR_GAMMA_ENABLE |
8280 CURSOR_FORMAT_ARGB |
8281 CURSOR_STRIDE(stride);
8282
8283 size = (height << 12) | width;
4b0e333e 8284 }
560b85bb 8285
dc41c154
VS
8286 if (intel_crtc->cursor_cntl != 0 &&
8287 (intel_crtc->cursor_base != base ||
8288 intel_crtc->cursor_size != size ||
8289 intel_crtc->cursor_cntl != cntl)) {
8290 /* On these chipsets we can only modify the base/size/stride
8291 * whilst the cursor is disabled.
8292 */
8293 I915_WRITE(_CURACNTR, 0);
4b0e333e 8294 POSTING_READ(_CURACNTR);
dc41c154 8295 intel_crtc->cursor_cntl = 0;
4b0e333e 8296 }
560b85bb 8297
99d1f387 8298 if (intel_crtc->cursor_base != base) {
9db4a9c7 8299 I915_WRITE(_CURABASE, base);
99d1f387
VS
8300 intel_crtc->cursor_base = base;
8301 }
4726e0b0 8302
dc41c154
VS
8303 if (intel_crtc->cursor_size != size) {
8304 I915_WRITE(CURSIZE, size);
8305 intel_crtc->cursor_size = size;
4b0e333e 8306 }
560b85bb 8307
4b0e333e 8308 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8309 I915_WRITE(_CURACNTR, cntl);
8310 POSTING_READ(_CURACNTR);
4b0e333e 8311 intel_crtc->cursor_cntl = cntl;
560b85bb 8312 }
560b85bb
CW
8313}
8314
560b85bb 8315static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8316{
8317 struct drm_device *dev = crtc->dev;
8318 struct drm_i915_private *dev_priv = dev->dev_private;
8319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8320 int pipe = intel_crtc->pipe;
4b0e333e
CW
8321 uint32_t cntl;
8322
8323 cntl = 0;
8324 if (base) {
8325 cntl = MCURSOR_GAMMA_ENABLE;
8326 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8327 case 64:
8328 cntl |= CURSOR_MODE_64_ARGB_AX;
8329 break;
8330 case 128:
8331 cntl |= CURSOR_MODE_128_ARGB_AX;
8332 break;
8333 case 256:
8334 cntl |= CURSOR_MODE_256_ARGB_AX;
8335 break;
8336 default:
8337 WARN_ON(1);
8338 return;
65a21cd6 8339 }
4b0e333e 8340 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8341
8342 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8343 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8344 }
65a21cd6 8345
4b0e333e
CW
8346 if (intel_crtc->cursor_cntl != cntl) {
8347 I915_WRITE(CURCNTR(pipe), cntl);
8348 POSTING_READ(CURCNTR(pipe));
8349 intel_crtc->cursor_cntl = cntl;
65a21cd6 8350 }
4b0e333e 8351
65a21cd6 8352 /* and commit changes on next vblank */
5efb3e28
VS
8353 I915_WRITE(CURBASE(pipe), base);
8354 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8355
8356 intel_crtc->cursor_base = base;
65a21cd6
JB
8357}
8358
cda4b7d3 8359/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8360static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8361 bool on)
cda4b7d3
CW
8362{
8363 struct drm_device *dev = crtc->dev;
8364 struct drm_i915_private *dev_priv = dev->dev_private;
8365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8366 int pipe = intel_crtc->pipe;
3d7d6510
MR
8367 int x = crtc->cursor_x;
8368 int y = crtc->cursor_y;
d6e4db15 8369 u32 base = 0, pos = 0;
cda4b7d3 8370
d6e4db15 8371 if (on)
cda4b7d3 8372 base = intel_crtc->cursor_addr;
cda4b7d3 8373
d6e4db15
VS
8374 if (x >= intel_crtc->config.pipe_src_w)
8375 base = 0;
8376
8377 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8378 base = 0;
8379
8380 if (x < 0) {
efc9064e 8381 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8382 base = 0;
8383
8384 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8385 x = -x;
8386 }
8387 pos |= x << CURSOR_X_SHIFT;
8388
8389 if (y < 0) {
efc9064e 8390 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8391 base = 0;
8392
8393 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8394 y = -y;
8395 }
8396 pos |= y << CURSOR_Y_SHIFT;
8397
4b0e333e 8398 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8399 return;
8400
5efb3e28
VS
8401 I915_WRITE(CURPOS(pipe), pos);
8402
8ac54669 8403 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8404 i845_update_cursor(crtc, base);
8405 else
8406 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8407}
8408
dc41c154
VS
8409static bool cursor_size_ok(struct drm_device *dev,
8410 uint32_t width, uint32_t height)
8411{
8412 if (width == 0 || height == 0)
8413 return false;
8414
8415 /*
8416 * 845g/865g are special in that they are only limited by
8417 * the width of their cursors, the height is arbitrary up to
8418 * the precision of the register. Everything else requires
8419 * square cursors, limited to a few power-of-two sizes.
8420 */
8421 if (IS_845G(dev) || IS_I865G(dev)) {
8422 if ((width & 63) != 0)
8423 return false;
8424
8425 if (width > (IS_845G(dev) ? 64 : 512))
8426 return false;
8427
8428 if (height > 1023)
8429 return false;
8430 } else {
8431 switch (width | height) {
8432 case 256:
8433 case 128:
8434 if (IS_GEN2(dev))
8435 return false;
8436 case 64:
8437 break;
8438 default:
8439 return false;
8440 }
8441 }
8442
8443 return true;
8444}
8445
e3287951
MR
8446/*
8447 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8448 *
8449 * Note that the object's reference will be consumed if the update fails. If
8450 * the update succeeds, the reference of the old object (if any) will be
8451 * consumed.
8452 */
8453static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8454 struct drm_i915_gem_object *obj,
8455 uint32_t width, uint32_t height)
79e53945
JB
8456{
8457 struct drm_device *dev = crtc->dev;
8458 struct drm_i915_private *dev_priv = dev->dev_private;
8459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8460 enum pipe pipe = intel_crtc->pipe;
dc41c154 8461 unsigned old_width, stride;
cda4b7d3 8462 uint32_t addr;
3f8bc370 8463 int ret;
79e53945 8464
79e53945 8465 /* if we want to turn off the cursor ignore width and height */
e3287951 8466 if (!obj) {
28c97730 8467 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8468 addr = 0;
5004417d 8469 mutex_lock(&dev->struct_mutex);
3f8bc370 8470 goto finish;
79e53945
JB
8471 }
8472
4726e0b0 8473 /* Check for which cursor types we support */
dc41c154 8474 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8475 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8476 return -EINVAL;
8477 }
8478
dc41c154
VS
8479 stride = roundup_pow_of_two(width) * 4;
8480 if (obj->base.size < stride * height) {
e3287951 8481 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8482 ret = -ENOMEM;
8483 goto fail;
79e53945
JB
8484 }
8485
71acb5eb 8486 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8487 mutex_lock(&dev->struct_mutex);
3d13ef2e 8488 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8489 unsigned alignment;
8490
d9e86c0e 8491 if (obj->tiling_mode) {
3b25b31f 8492 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8493 ret = -EINVAL;
8494 goto fail_locked;
8495 }
8496
d6dd6843
PZ
8497 /*
8498 * Global gtt pte registers are special registers which actually
8499 * forward writes to a chunk of system memory. Which means that
8500 * there is no risk that the register values disappear as soon
8501 * as we call intel_runtime_pm_put(), so it is correct to wrap
8502 * only the pin/unpin/fence and not more.
8503 */
8504 intel_runtime_pm_get(dev_priv);
8505
693db184
CW
8506 /* Note that the w/a also requires 2 PTE of padding following
8507 * the bo. We currently fill all unused PTE with the shadow
8508 * page and so we should always have valid PTE following the
8509 * cursor preventing the VT-d warning.
8510 */
8511 alignment = 0;
8512 if (need_vtd_wa(dev))
8513 alignment = 64*1024;
8514
8515 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8516 if (ret) {
3b25b31f 8517 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
d6dd6843 8518 intel_runtime_pm_put(dev_priv);
2da3b9b9 8519 goto fail_locked;
e7b526bb
CW
8520 }
8521
d9e86c0e
CW
8522 ret = i915_gem_object_put_fence(obj);
8523 if (ret) {
3b25b31f 8524 DRM_DEBUG_KMS("failed to release fence for cursor");
d6dd6843 8525 intel_runtime_pm_put(dev_priv);
d9e86c0e
CW
8526 goto fail_unpin;
8527 }
8528
f343c5f6 8529 addr = i915_gem_obj_ggtt_offset(obj);
d6dd6843
PZ
8530
8531 intel_runtime_pm_put(dev_priv);
71acb5eb 8532 } else {
6eeefaf3 8533 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8534 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8535 if (ret) {
3b25b31f 8536 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8537 goto fail_locked;
71acb5eb 8538 }
00731155 8539 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8540 }
8541
3f8bc370 8542 finish:
3f8bc370 8543 if (intel_crtc->cursor_bo) {
00731155 8544 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8545 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8546 }
80824003 8547
a071fa00
DV
8548 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8549 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8550 mutex_unlock(&dev->struct_mutex);
3f8bc370 8551
64f962e3
CW
8552 old_width = intel_crtc->cursor_width;
8553
3f8bc370 8554 intel_crtc->cursor_addr = addr;
05394f39 8555 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8556 intel_crtc->cursor_width = width;
8557 intel_crtc->cursor_height = height;
8558
64f962e3
CW
8559 if (intel_crtc->active) {
8560 if (old_width != width)
8561 intel_update_watermarks(crtc);
f2f5f771 8562 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8563 }
3f8bc370 8564
f99d7069
DV
8565 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8566
79e53945 8567 return 0;
e7b526bb 8568fail_unpin:
cc98b413 8569 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8570fail_locked:
34b8686e 8571 mutex_unlock(&dev->struct_mutex);
bc9025bd 8572fail:
05394f39 8573 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8574 return ret;
79e53945
JB
8575}
8576
79e53945 8577static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8578 u16 *blue, uint32_t start, uint32_t size)
79e53945 8579{
7203425a 8580 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8582
7203425a 8583 for (i = start; i < end; i++) {
79e53945
JB
8584 intel_crtc->lut_r[i] = red[i] >> 8;
8585 intel_crtc->lut_g[i] = green[i] >> 8;
8586 intel_crtc->lut_b[i] = blue[i] >> 8;
8587 }
8588
8589 intel_crtc_load_lut(crtc);
8590}
8591
79e53945
JB
8592/* VESA 640x480x72Hz mode to set on the pipe */
8593static struct drm_display_mode load_detect_mode = {
8594 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8595 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8596};
8597
a8bb6818
DV
8598struct drm_framebuffer *
8599__intel_framebuffer_create(struct drm_device *dev,
8600 struct drm_mode_fb_cmd2 *mode_cmd,
8601 struct drm_i915_gem_object *obj)
d2dff872
CW
8602{
8603 struct intel_framebuffer *intel_fb;
8604 int ret;
8605
8606 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8607 if (!intel_fb) {
8608 drm_gem_object_unreference_unlocked(&obj->base);
8609 return ERR_PTR(-ENOMEM);
8610 }
8611
8612 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8613 if (ret)
8614 goto err;
d2dff872
CW
8615
8616 return &intel_fb->base;
dd4916c5
DV
8617err:
8618 drm_gem_object_unreference_unlocked(&obj->base);
8619 kfree(intel_fb);
8620
8621 return ERR_PTR(ret);
d2dff872
CW
8622}
8623
b5ea642a 8624static struct drm_framebuffer *
a8bb6818
DV
8625intel_framebuffer_create(struct drm_device *dev,
8626 struct drm_mode_fb_cmd2 *mode_cmd,
8627 struct drm_i915_gem_object *obj)
8628{
8629 struct drm_framebuffer *fb;
8630 int ret;
8631
8632 ret = i915_mutex_lock_interruptible(dev);
8633 if (ret)
8634 return ERR_PTR(ret);
8635 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8636 mutex_unlock(&dev->struct_mutex);
8637
8638 return fb;
8639}
8640
d2dff872
CW
8641static u32
8642intel_framebuffer_pitch_for_width(int width, int bpp)
8643{
8644 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8645 return ALIGN(pitch, 64);
8646}
8647
8648static u32
8649intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8650{
8651 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8652 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8653}
8654
8655static struct drm_framebuffer *
8656intel_framebuffer_create_for_mode(struct drm_device *dev,
8657 struct drm_display_mode *mode,
8658 int depth, int bpp)
8659{
8660 struct drm_i915_gem_object *obj;
0fed39bd 8661 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8662
8663 obj = i915_gem_alloc_object(dev,
8664 intel_framebuffer_size_for_mode(mode, bpp));
8665 if (obj == NULL)
8666 return ERR_PTR(-ENOMEM);
8667
8668 mode_cmd.width = mode->hdisplay;
8669 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8670 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8671 bpp);
5ca0c34a 8672 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8673
8674 return intel_framebuffer_create(dev, &mode_cmd, obj);
8675}
8676
8677static struct drm_framebuffer *
8678mode_fits_in_fbdev(struct drm_device *dev,
8679 struct drm_display_mode *mode)
8680{
4520f53a 8681#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8682 struct drm_i915_private *dev_priv = dev->dev_private;
8683 struct drm_i915_gem_object *obj;
8684 struct drm_framebuffer *fb;
8685
4c0e5528 8686 if (!dev_priv->fbdev)
d2dff872
CW
8687 return NULL;
8688
4c0e5528 8689 if (!dev_priv->fbdev->fb)
d2dff872
CW
8690 return NULL;
8691
4c0e5528
DV
8692 obj = dev_priv->fbdev->fb->obj;
8693 BUG_ON(!obj);
8694
8bcd4553 8695 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8696 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8697 fb->bits_per_pixel))
d2dff872
CW
8698 return NULL;
8699
01f2c773 8700 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8701 return NULL;
8702
8703 return fb;
4520f53a
DV
8704#else
8705 return NULL;
8706#endif
d2dff872
CW
8707}
8708
d2434ab7 8709bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8710 struct drm_display_mode *mode,
51fd371b
RC
8711 struct intel_load_detect_pipe *old,
8712 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8713{
8714 struct intel_crtc *intel_crtc;
d2434ab7
DV
8715 struct intel_encoder *intel_encoder =
8716 intel_attached_encoder(connector);
79e53945 8717 struct drm_crtc *possible_crtc;
4ef69c7a 8718 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8719 struct drm_crtc *crtc = NULL;
8720 struct drm_device *dev = encoder->dev;
94352cf9 8721 struct drm_framebuffer *fb;
51fd371b
RC
8722 struct drm_mode_config *config = &dev->mode_config;
8723 int ret, i = -1;
79e53945 8724
d2dff872 8725 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8726 connector->base.id, connector->name,
8e329a03 8727 encoder->base.id, encoder->name);
d2dff872 8728
51fd371b
RC
8729retry:
8730 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8731 if (ret)
8732 goto fail_unlock;
6e9f798d 8733
79e53945
JB
8734 /*
8735 * Algorithm gets a little messy:
7a5e4805 8736 *
79e53945
JB
8737 * - if the connector already has an assigned crtc, use it (but make
8738 * sure it's on first)
7a5e4805 8739 *
79e53945
JB
8740 * - try to find the first unused crtc that can drive this connector,
8741 * and use that if we find one
79e53945
JB
8742 */
8743
8744 /* See if we already have a CRTC for this connector */
8745 if (encoder->crtc) {
8746 crtc = encoder->crtc;
8261b191 8747
51fd371b
RC
8748 ret = drm_modeset_lock(&crtc->mutex, ctx);
8749 if (ret)
8750 goto fail_unlock;
7b24056b 8751
24218aac 8752 old->dpms_mode = connector->dpms;
8261b191
CW
8753 old->load_detect_temp = false;
8754
8755 /* Make sure the crtc and connector are running */
24218aac
DV
8756 if (connector->dpms != DRM_MODE_DPMS_ON)
8757 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8758
7173188d 8759 return true;
79e53945
JB
8760 }
8761
8762 /* Find an unused one (if possible) */
70e1e0ec 8763 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8764 i++;
8765 if (!(encoder->possible_crtcs & (1 << i)))
8766 continue;
a459249c
VS
8767 if (possible_crtc->enabled)
8768 continue;
8769 /* This can occur when applying the pipe A quirk on resume. */
8770 if (to_intel_crtc(possible_crtc)->new_enabled)
8771 continue;
8772
8773 crtc = possible_crtc;
8774 break;
79e53945
JB
8775 }
8776
8777 /*
8778 * If we didn't find an unused CRTC, don't use any.
8779 */
8780 if (!crtc) {
7173188d 8781 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8782 goto fail_unlock;
79e53945
JB
8783 }
8784
51fd371b
RC
8785 ret = drm_modeset_lock(&crtc->mutex, ctx);
8786 if (ret)
8787 goto fail_unlock;
fc303101
DV
8788 intel_encoder->new_crtc = to_intel_crtc(crtc);
8789 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8790
8791 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8792 intel_crtc->new_enabled = true;
8793 intel_crtc->new_config = &intel_crtc->config;
24218aac 8794 old->dpms_mode = connector->dpms;
8261b191 8795 old->load_detect_temp = true;
d2dff872 8796 old->release_fb = NULL;
79e53945 8797
6492711d
CW
8798 if (!mode)
8799 mode = &load_detect_mode;
79e53945 8800
d2dff872
CW
8801 /* We need a framebuffer large enough to accommodate all accesses
8802 * that the plane may generate whilst we perform load detection.
8803 * We can not rely on the fbcon either being present (we get called
8804 * during its initialisation to detect all boot displays, or it may
8805 * not even exist) or that it is large enough to satisfy the
8806 * requested mode.
8807 */
94352cf9
DV
8808 fb = mode_fits_in_fbdev(dev, mode);
8809 if (fb == NULL) {
d2dff872 8810 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8811 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8812 old->release_fb = fb;
d2dff872
CW
8813 } else
8814 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8815 if (IS_ERR(fb)) {
d2dff872 8816 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8817 goto fail;
79e53945 8818 }
79e53945 8819
c0c36b94 8820 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8821 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8822 if (old->release_fb)
8823 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8824 goto fail;
79e53945 8825 }
7173188d 8826
79e53945 8827 /* let the connector get through one full cycle before testing */
9d0498a2 8828 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8829 return true;
412b61d8
VS
8830
8831 fail:
8832 intel_crtc->new_enabled = crtc->enabled;
8833 if (intel_crtc->new_enabled)
8834 intel_crtc->new_config = &intel_crtc->config;
8835 else
8836 intel_crtc->new_config = NULL;
51fd371b
RC
8837fail_unlock:
8838 if (ret == -EDEADLK) {
8839 drm_modeset_backoff(ctx);
8840 goto retry;
8841 }
8842
412b61d8 8843 return false;
79e53945
JB
8844}
8845
d2434ab7 8846void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8847 struct intel_load_detect_pipe *old)
79e53945 8848{
d2434ab7
DV
8849 struct intel_encoder *intel_encoder =
8850 intel_attached_encoder(connector);
4ef69c7a 8851 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8852 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8854
d2dff872 8855 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8856 connector->base.id, connector->name,
8e329a03 8857 encoder->base.id, encoder->name);
d2dff872 8858
8261b191 8859 if (old->load_detect_temp) {
fc303101
DV
8860 to_intel_connector(connector)->new_encoder = NULL;
8861 intel_encoder->new_crtc = NULL;
412b61d8
VS
8862 intel_crtc->new_enabled = false;
8863 intel_crtc->new_config = NULL;
fc303101 8864 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8865
36206361
DV
8866 if (old->release_fb) {
8867 drm_framebuffer_unregister_private(old->release_fb);
8868 drm_framebuffer_unreference(old->release_fb);
8869 }
d2dff872 8870
0622a53c 8871 return;
79e53945
JB
8872 }
8873
c751ce4f 8874 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8875 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8876 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8877}
8878
da4a1efa
VS
8879static int i9xx_pll_refclk(struct drm_device *dev,
8880 const struct intel_crtc_config *pipe_config)
8881{
8882 struct drm_i915_private *dev_priv = dev->dev_private;
8883 u32 dpll = pipe_config->dpll_hw_state.dpll;
8884
8885 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8886 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8887 else if (HAS_PCH_SPLIT(dev))
8888 return 120000;
8889 else if (!IS_GEN2(dev))
8890 return 96000;
8891 else
8892 return 48000;
8893}
8894
79e53945 8895/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8896static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8897 struct intel_crtc_config *pipe_config)
79e53945 8898{
f1f644dc 8899 struct drm_device *dev = crtc->base.dev;
79e53945 8900 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8901 int pipe = pipe_config->cpu_transcoder;
293623f7 8902 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8903 u32 fp;
8904 intel_clock_t clock;
da4a1efa 8905 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8906
8907 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8908 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8909 else
293623f7 8910 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8911
8912 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8913 if (IS_PINEVIEW(dev)) {
8914 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8915 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8916 } else {
8917 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8918 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8919 }
8920
a6c45cf0 8921 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8922 if (IS_PINEVIEW(dev))
8923 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8924 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8925 else
8926 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8927 DPLL_FPA01_P1_POST_DIV_SHIFT);
8928
8929 switch (dpll & DPLL_MODE_MASK) {
8930 case DPLLB_MODE_DAC_SERIAL:
8931 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8932 5 : 10;
8933 break;
8934 case DPLLB_MODE_LVDS:
8935 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8936 7 : 14;
8937 break;
8938 default:
28c97730 8939 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8940 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8941 return;
79e53945
JB
8942 }
8943
ac58c3f0 8944 if (IS_PINEVIEW(dev))
da4a1efa 8945 pineview_clock(refclk, &clock);
ac58c3f0 8946 else
da4a1efa 8947 i9xx_clock(refclk, &clock);
79e53945 8948 } else {
0fb58223 8949 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8950 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8951
8952 if (is_lvds) {
8953 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8954 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8955
8956 if (lvds & LVDS_CLKB_POWER_UP)
8957 clock.p2 = 7;
8958 else
8959 clock.p2 = 14;
79e53945
JB
8960 } else {
8961 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8962 clock.p1 = 2;
8963 else {
8964 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8965 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8966 }
8967 if (dpll & PLL_P2_DIVIDE_BY_4)
8968 clock.p2 = 4;
8969 else
8970 clock.p2 = 2;
79e53945 8971 }
da4a1efa
VS
8972
8973 i9xx_clock(refclk, &clock);
79e53945
JB
8974 }
8975
18442d08
VS
8976 /*
8977 * This value includes pixel_multiplier. We will use
241bfc38 8978 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8979 * encoder's get_config() function.
8980 */
8981 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8982}
8983
6878da05
VS
8984int intel_dotclock_calculate(int link_freq,
8985 const struct intel_link_m_n *m_n)
f1f644dc 8986{
f1f644dc
JB
8987 /*
8988 * The calculation for the data clock is:
1041a02f 8989 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8990 * But we want to avoid losing precison if possible, so:
1041a02f 8991 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8992 *
8993 * and the link clock is simpler:
1041a02f 8994 * link_clock = (m * link_clock) / n
f1f644dc
JB
8995 */
8996
6878da05
VS
8997 if (!m_n->link_n)
8998 return 0;
f1f644dc 8999
6878da05
VS
9000 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9001}
f1f644dc 9002
18442d08
VS
9003static void ironlake_pch_clock_get(struct intel_crtc *crtc,
9004 struct intel_crtc_config *pipe_config)
6878da05
VS
9005{
9006 struct drm_device *dev = crtc->base.dev;
79e53945 9007
18442d08
VS
9008 /* read out port_clock from the DPLL */
9009 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9010
f1f644dc 9011 /*
18442d08 9012 * This value does not include pixel_multiplier.
241bfc38 9013 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
9014 * agree once we know their relationship in the encoder's
9015 * get_config() function.
79e53945 9016 */
241bfc38 9017 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
9018 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9019 &pipe_config->fdi_m_n);
79e53945
JB
9020}
9021
9022/** Returns the currently programmed mode of the given pipe. */
9023struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9024 struct drm_crtc *crtc)
9025{
548f245b 9026 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 9027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 9028 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 9029 struct drm_display_mode *mode;
f1f644dc 9030 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
9031 int htot = I915_READ(HTOTAL(cpu_transcoder));
9032 int hsync = I915_READ(HSYNC(cpu_transcoder));
9033 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9034 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9035 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9036
9037 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9038 if (!mode)
9039 return NULL;
9040
f1f644dc
JB
9041 /*
9042 * Construct a pipe_config sufficient for getting the clock info
9043 * back out of crtc_clock_get.
9044 *
9045 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9046 * to use a real value here instead.
9047 */
293623f7 9048 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 9049 pipe_config.pixel_multiplier = 1;
293623f7
VS
9050 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9051 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9052 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
9053 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9054
773ae034 9055 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
9056 mode->hdisplay = (htot & 0xffff) + 1;
9057 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9058 mode->hsync_start = (hsync & 0xffff) + 1;
9059 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9060 mode->vdisplay = (vtot & 0xffff) + 1;
9061 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9062 mode->vsync_start = (vsync & 0xffff) + 1;
9063 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9064
9065 drm_mode_set_name(mode);
79e53945
JB
9066
9067 return mode;
9068}
9069
652c393a
JB
9070static void intel_decrease_pllclock(struct drm_crtc *crtc)
9071{
9072 struct drm_device *dev = crtc->dev;
fbee40df 9073 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 9075
baff296c 9076 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9077 return;
9078
9079 if (!dev_priv->lvds_downclock_avail)
9080 return;
9081
9082 /*
9083 * Since this is called by a timer, we should never get here in
9084 * the manual case.
9085 */
9086 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
9087 int pipe = intel_crtc->pipe;
9088 int dpll_reg = DPLL(pipe);
9089 int dpll;
f6e5b160 9090
44d98a61 9091 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9092
8ac5a6d5 9093 assert_panel_unlocked(dev_priv, pipe);
652c393a 9094
dc257cf1 9095 dpll = I915_READ(dpll_reg);
652c393a
JB
9096 dpll |= DISPLAY_RATE_SELECT_FPA1;
9097 I915_WRITE(dpll_reg, dpll);
9d0498a2 9098 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9099 dpll = I915_READ(dpll_reg);
9100 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9101 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9102 }
9103
9104}
9105
f047e395
CW
9106void intel_mark_busy(struct drm_device *dev)
9107{
c67a470b
PZ
9108 struct drm_i915_private *dev_priv = dev->dev_private;
9109
f62a0076
CW
9110 if (dev_priv->mm.busy)
9111 return;
9112
43694d69 9113 intel_runtime_pm_get(dev_priv);
c67a470b 9114 i915_update_gfx_val(dev_priv);
f62a0076 9115 dev_priv->mm.busy = true;
f047e395
CW
9116}
9117
9118void intel_mark_idle(struct drm_device *dev)
652c393a 9119{
c67a470b 9120 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9121 struct drm_crtc *crtc;
652c393a 9122
f62a0076
CW
9123 if (!dev_priv->mm.busy)
9124 return;
9125
9126 dev_priv->mm.busy = false;
9127
d330a953 9128 if (!i915.powersave)
bb4cdd53 9129 goto out;
652c393a 9130
70e1e0ec 9131 for_each_crtc(dev, crtc) {
f4510a27 9132 if (!crtc->primary->fb)
652c393a
JB
9133 continue;
9134
725a5b54 9135 intel_decrease_pllclock(crtc);
652c393a 9136 }
b29c19b6 9137
3d13ef2e 9138 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9139 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9140
9141out:
43694d69 9142 intel_runtime_pm_put(dev_priv);
652c393a
JB
9143}
9144
79e53945
JB
9145static void intel_crtc_destroy(struct drm_crtc *crtc)
9146{
9147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9148 struct drm_device *dev = crtc->dev;
9149 struct intel_unpin_work *work;
67e77c5a 9150
5e2d7afc 9151 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
9152 work = intel_crtc->unpin_work;
9153 intel_crtc->unpin_work = NULL;
5e2d7afc 9154 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
9155
9156 if (work) {
9157 cancel_work_sync(&work->work);
9158 kfree(work);
9159 }
79e53945
JB
9160
9161 drm_crtc_cleanup(crtc);
67e77c5a 9162
79e53945
JB
9163 kfree(intel_crtc);
9164}
9165
6b95a207
KH
9166static void intel_unpin_work_fn(struct work_struct *__work)
9167{
9168 struct intel_unpin_work *work =
9169 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9170 struct drm_device *dev = work->crtc->dev;
f99d7069 9171 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9172
b4a98e57 9173 mutex_lock(&dev->struct_mutex);
1690e1eb 9174 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9175 drm_gem_object_unreference(&work->pending_flip_obj->base);
9176 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9177
b4a98e57
CW
9178 intel_update_fbc(dev);
9179 mutex_unlock(&dev->struct_mutex);
9180
f99d7069
DV
9181 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9182
b4a98e57
CW
9183 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9184 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9185
6b95a207
KH
9186 kfree(work);
9187}
9188
1afe3e9d 9189static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9190 struct drm_crtc *crtc)
6b95a207 9191{
6b95a207
KH
9192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9193 struct intel_unpin_work *work;
6b95a207
KH
9194 unsigned long flags;
9195
9196 /* Ignore early vblank irqs */
9197 if (intel_crtc == NULL)
9198 return;
9199
f326038a
DV
9200 /*
9201 * This is called both by irq handlers and the reset code (to complete
9202 * lost pageflips) so needs the full irqsave spinlocks.
9203 */
6b95a207
KH
9204 spin_lock_irqsave(&dev->event_lock, flags);
9205 work = intel_crtc->unpin_work;
e7d841ca
CW
9206
9207 /* Ensure we don't miss a work->pending update ... */
9208 smp_rmb();
9209
9210 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9211 spin_unlock_irqrestore(&dev->event_lock, flags);
9212 return;
9213 }
9214
d6bbafa1 9215 page_flip_completed(intel_crtc);
0af7e4df 9216
6b95a207 9217 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9218}
9219
1afe3e9d
JB
9220void intel_finish_page_flip(struct drm_device *dev, int pipe)
9221{
fbee40df 9222 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9223 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9224
49b14a5c 9225 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9226}
9227
9228void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9229{
fbee40df 9230 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9231 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9232
49b14a5c 9233 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9234}
9235
75f7f3ec
VS
9236/* Is 'a' after or equal to 'b'? */
9237static bool g4x_flip_count_after_eq(u32 a, u32 b)
9238{
9239 return !((a - b) & 0x80000000);
9240}
9241
9242static bool page_flip_finished(struct intel_crtc *crtc)
9243{
9244 struct drm_device *dev = crtc->base.dev;
9245 struct drm_i915_private *dev_priv = dev->dev_private;
9246
9247 /*
9248 * The relevant registers doen't exist on pre-ctg.
9249 * As the flip done interrupt doesn't trigger for mmio
9250 * flips on gmch platforms, a flip count check isn't
9251 * really needed there. But since ctg has the registers,
9252 * include it in the check anyway.
9253 */
9254 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9255 return true;
9256
9257 /*
9258 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9259 * used the same base address. In that case the mmio flip might
9260 * have completed, but the CS hasn't even executed the flip yet.
9261 *
9262 * A flip count check isn't enough as the CS might have updated
9263 * the base address just after start of vblank, but before we
9264 * managed to process the interrupt. This means we'd complete the
9265 * CS flip too soon.
9266 *
9267 * Combining both checks should get us a good enough result. It may
9268 * still happen that the CS flip has been executed, but has not
9269 * yet actually completed. But in case the base address is the same
9270 * anyway, we don't really care.
9271 */
9272 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9273 crtc->unpin_work->gtt_offset &&
9274 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9275 crtc->unpin_work->flip_count);
9276}
9277
6b95a207
KH
9278void intel_prepare_page_flip(struct drm_device *dev, int plane)
9279{
fbee40df 9280 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9281 struct intel_crtc *intel_crtc =
9282 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9283 unsigned long flags;
9284
f326038a
DV
9285
9286 /*
9287 * This is called both by irq handlers and the reset code (to complete
9288 * lost pageflips) so needs the full irqsave spinlocks.
9289 *
9290 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
9291 * generate a page-flip completion irq, i.e. every modeset
9292 * is also accompanied by a spurious intel_prepare_page_flip().
9293 */
6b95a207 9294 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9295 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9296 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9297 spin_unlock_irqrestore(&dev->event_lock, flags);
9298}
9299
eba905b2 9300static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9301{
9302 /* Ensure that the work item is consistent when activating it ... */
9303 smp_wmb();
9304 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9305 /* and that it is marked active as soon as the irq could fire. */
9306 smp_wmb();
9307}
9308
8c9f3aaf
JB
9309static int intel_gen2_queue_flip(struct drm_device *dev,
9310 struct drm_crtc *crtc,
9311 struct drm_framebuffer *fb,
ed8d1975 9312 struct drm_i915_gem_object *obj,
a4872ba6 9313 struct intel_engine_cs *ring,
ed8d1975 9314 uint32_t flags)
8c9f3aaf 9315{
8c9f3aaf 9316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9317 u32 flip_mask;
9318 int ret;
9319
6d90c952 9320 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9321 if (ret)
4fa62c89 9322 return ret;
8c9f3aaf
JB
9323
9324 /* Can't queue multiple flips, so wait for the previous
9325 * one to finish before executing the next.
9326 */
9327 if (intel_crtc->plane)
9328 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9329 else
9330 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9331 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9332 intel_ring_emit(ring, MI_NOOP);
9333 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9334 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9335 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9336 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9337 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9338
9339 intel_mark_page_flip_active(intel_crtc);
09246732 9340 __intel_ring_advance(ring);
83d4092b 9341 return 0;
8c9f3aaf
JB
9342}
9343
9344static int intel_gen3_queue_flip(struct drm_device *dev,
9345 struct drm_crtc *crtc,
9346 struct drm_framebuffer *fb,
ed8d1975 9347 struct drm_i915_gem_object *obj,
a4872ba6 9348 struct intel_engine_cs *ring,
ed8d1975 9349 uint32_t flags)
8c9f3aaf 9350{
8c9f3aaf 9351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9352 u32 flip_mask;
9353 int ret;
9354
6d90c952 9355 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9356 if (ret)
4fa62c89 9357 return ret;
8c9f3aaf
JB
9358
9359 if (intel_crtc->plane)
9360 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9361 else
9362 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9363 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9364 intel_ring_emit(ring, MI_NOOP);
9365 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9366 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9367 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9368 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9369 intel_ring_emit(ring, MI_NOOP);
9370
e7d841ca 9371 intel_mark_page_flip_active(intel_crtc);
09246732 9372 __intel_ring_advance(ring);
83d4092b 9373 return 0;
8c9f3aaf
JB
9374}
9375
9376static int intel_gen4_queue_flip(struct drm_device *dev,
9377 struct drm_crtc *crtc,
9378 struct drm_framebuffer *fb,
ed8d1975 9379 struct drm_i915_gem_object *obj,
a4872ba6 9380 struct intel_engine_cs *ring,
ed8d1975 9381 uint32_t flags)
8c9f3aaf
JB
9382{
9383 struct drm_i915_private *dev_priv = dev->dev_private;
9384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9385 uint32_t pf, pipesrc;
9386 int ret;
9387
6d90c952 9388 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9389 if (ret)
4fa62c89 9390 return ret;
8c9f3aaf
JB
9391
9392 /* i965+ uses the linear or tiled offsets from the
9393 * Display Registers (which do not change across a page-flip)
9394 * so we need only reprogram the base address.
9395 */
6d90c952
DV
9396 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9397 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9398 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9399 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9400 obj->tiling_mode);
8c9f3aaf
JB
9401
9402 /* XXX Enabling the panel-fitter across page-flip is so far
9403 * untested on non-native modes, so ignore it for now.
9404 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9405 */
9406 pf = 0;
9407 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9408 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9409
9410 intel_mark_page_flip_active(intel_crtc);
09246732 9411 __intel_ring_advance(ring);
83d4092b 9412 return 0;
8c9f3aaf
JB
9413}
9414
9415static int intel_gen6_queue_flip(struct drm_device *dev,
9416 struct drm_crtc *crtc,
9417 struct drm_framebuffer *fb,
ed8d1975 9418 struct drm_i915_gem_object *obj,
a4872ba6 9419 struct intel_engine_cs *ring,
ed8d1975 9420 uint32_t flags)
8c9f3aaf
JB
9421{
9422 struct drm_i915_private *dev_priv = dev->dev_private;
9423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9424 uint32_t pf, pipesrc;
9425 int ret;
9426
6d90c952 9427 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9428 if (ret)
4fa62c89 9429 return ret;
8c9f3aaf 9430
6d90c952
DV
9431 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9432 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9433 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9434 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9435
dc257cf1
DV
9436 /* Contrary to the suggestions in the documentation,
9437 * "Enable Panel Fitter" does not seem to be required when page
9438 * flipping with a non-native mode, and worse causes a normal
9439 * modeset to fail.
9440 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9441 */
9442 pf = 0;
8c9f3aaf 9443 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9444 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9445
9446 intel_mark_page_flip_active(intel_crtc);
09246732 9447 __intel_ring_advance(ring);
83d4092b 9448 return 0;
8c9f3aaf
JB
9449}
9450
7c9017e5
JB
9451static int intel_gen7_queue_flip(struct drm_device *dev,
9452 struct drm_crtc *crtc,
9453 struct drm_framebuffer *fb,
ed8d1975 9454 struct drm_i915_gem_object *obj,
a4872ba6 9455 struct intel_engine_cs *ring,
ed8d1975 9456 uint32_t flags)
7c9017e5 9457{
7c9017e5 9458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9459 uint32_t plane_bit = 0;
ffe74d75
CW
9460 int len, ret;
9461
eba905b2 9462 switch (intel_crtc->plane) {
cb05d8de
DV
9463 case PLANE_A:
9464 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9465 break;
9466 case PLANE_B:
9467 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9468 break;
9469 case PLANE_C:
9470 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9471 break;
9472 default:
9473 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9474 return -ENODEV;
cb05d8de
DV
9475 }
9476
ffe74d75 9477 len = 4;
f476828a 9478 if (ring->id == RCS) {
ffe74d75 9479 len += 6;
f476828a
DL
9480 /*
9481 * On Gen 8, SRM is now taking an extra dword to accommodate
9482 * 48bits addresses, and we need a NOOP for the batch size to
9483 * stay even.
9484 */
9485 if (IS_GEN8(dev))
9486 len += 2;
9487 }
ffe74d75 9488
f66fab8e
VS
9489 /*
9490 * BSpec MI_DISPLAY_FLIP for IVB:
9491 * "The full packet must be contained within the same cache line."
9492 *
9493 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9494 * cacheline, if we ever start emitting more commands before
9495 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9496 * then do the cacheline alignment, and finally emit the
9497 * MI_DISPLAY_FLIP.
9498 */
9499 ret = intel_ring_cacheline_align(ring);
9500 if (ret)
4fa62c89 9501 return ret;
f66fab8e 9502
ffe74d75 9503 ret = intel_ring_begin(ring, len);
7c9017e5 9504 if (ret)
4fa62c89 9505 return ret;
7c9017e5 9506
ffe74d75
CW
9507 /* Unmask the flip-done completion message. Note that the bspec says that
9508 * we should do this for both the BCS and RCS, and that we must not unmask
9509 * more than one flip event at any time (or ensure that one flip message
9510 * can be sent by waiting for flip-done prior to queueing new flips).
9511 * Experimentation says that BCS works despite DERRMR masking all
9512 * flip-done completion events and that unmasking all planes at once
9513 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9514 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9515 */
9516 if (ring->id == RCS) {
9517 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9518 intel_ring_emit(ring, DERRMR);
9519 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9520 DERRMR_PIPEB_PRI_FLIP_DONE |
9521 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9522 if (IS_GEN8(dev))
9523 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9524 MI_SRM_LRM_GLOBAL_GTT);
9525 else
9526 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9527 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9528 intel_ring_emit(ring, DERRMR);
9529 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9530 if (IS_GEN8(dev)) {
9531 intel_ring_emit(ring, 0);
9532 intel_ring_emit(ring, MI_NOOP);
9533 }
ffe74d75
CW
9534 }
9535
cb05d8de 9536 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9537 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9538 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9539 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9540
9541 intel_mark_page_flip_active(intel_crtc);
09246732 9542 __intel_ring_advance(ring);
83d4092b 9543 return 0;
7c9017e5
JB
9544}
9545
84c33a64
SG
9546static bool use_mmio_flip(struct intel_engine_cs *ring,
9547 struct drm_i915_gem_object *obj)
9548{
9549 /*
9550 * This is not being used for older platforms, because
9551 * non-availability of flip done interrupt forces us to use
9552 * CS flips. Older platforms derive flip done using some clever
9553 * tricks involving the flip_pending status bits and vblank irqs.
9554 * So using MMIO flips there would disrupt this mechanism.
9555 */
9556
8e09bf83
CW
9557 if (ring == NULL)
9558 return true;
9559
84c33a64
SG
9560 if (INTEL_INFO(ring->dev)->gen < 5)
9561 return false;
9562
9563 if (i915.use_mmio_flip < 0)
9564 return false;
9565 else if (i915.use_mmio_flip > 0)
9566 return true;
14bf993e
OM
9567 else if (i915.enable_execlists)
9568 return true;
84c33a64
SG
9569 else
9570 return ring != obj->ring;
9571}
9572
9573static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9574{
9575 struct drm_device *dev = intel_crtc->base.dev;
9576 struct drm_i915_private *dev_priv = dev->dev_private;
9577 struct intel_framebuffer *intel_fb =
9578 to_intel_framebuffer(intel_crtc->base.primary->fb);
9579 struct drm_i915_gem_object *obj = intel_fb->obj;
9580 u32 dspcntr;
9581 u32 reg;
9582
9583 intel_mark_page_flip_active(intel_crtc);
9584
9585 reg = DSPCNTR(intel_crtc->plane);
9586 dspcntr = I915_READ(reg);
9587
9588 if (INTEL_INFO(dev)->gen >= 4) {
9589 if (obj->tiling_mode != I915_TILING_NONE)
9590 dspcntr |= DISPPLANE_TILED;
9591 else
9592 dspcntr &= ~DISPPLANE_TILED;
9593 }
9594 I915_WRITE(reg, dspcntr);
9595
9596 I915_WRITE(DSPSURF(intel_crtc->plane),
9597 intel_crtc->unpin_work->gtt_offset);
9598 POSTING_READ(DSPSURF(intel_crtc->plane));
9599}
9600
9601static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9602{
9603 struct intel_engine_cs *ring;
9604 int ret;
9605
9606 lockdep_assert_held(&obj->base.dev->struct_mutex);
9607
9608 if (!obj->last_write_seqno)
9609 return 0;
9610
9611 ring = obj->ring;
9612
9613 if (i915_seqno_passed(ring->get_seqno(ring, true),
9614 obj->last_write_seqno))
9615 return 0;
9616
9617 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9618 if (ret)
9619 return ret;
9620
9621 if (WARN_ON(!ring->irq_get(ring)))
9622 return 0;
9623
9624 return 1;
9625}
9626
9627void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9628{
9629 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9630 struct intel_crtc *intel_crtc;
9631 unsigned long irq_flags;
9632 u32 seqno;
9633
9634 seqno = ring->get_seqno(ring, false);
9635
9636 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9637 for_each_intel_crtc(ring->dev, intel_crtc) {
9638 struct intel_mmio_flip *mmio_flip;
9639
9640 mmio_flip = &intel_crtc->mmio_flip;
9641 if (mmio_flip->seqno == 0)
9642 continue;
9643
9644 if (ring->id != mmio_flip->ring_id)
9645 continue;
9646
9647 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9648 intel_do_mmio_flip(intel_crtc);
9649 mmio_flip->seqno = 0;
9650 ring->irq_put(ring);
9651 }
9652 }
9653 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9654}
9655
9656static int intel_queue_mmio_flip(struct drm_device *dev,
9657 struct drm_crtc *crtc,
9658 struct drm_framebuffer *fb,
9659 struct drm_i915_gem_object *obj,
9660 struct intel_engine_cs *ring,
9661 uint32_t flags)
9662{
9663 struct drm_i915_private *dev_priv = dev->dev_private;
9664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64
SG
9665 int ret;
9666
9667 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9668 return -EBUSY;
9669
9670 ret = intel_postpone_flip(obj);
9671 if (ret < 0)
9672 return ret;
9673 if (ret == 0) {
9674 intel_do_mmio_flip(intel_crtc);
9675 return 0;
9676 }
9677
24955f24 9678 spin_lock_irq(&dev_priv->mmio_flip_lock);
84c33a64
SG
9679 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9680 intel_crtc->mmio_flip.ring_id = obj->ring->id;
24955f24 9681 spin_unlock_irq(&dev_priv->mmio_flip_lock);
84c33a64
SG
9682
9683 /*
9684 * Double check to catch cases where irq fired before
9685 * mmio flip data was ready
9686 */
9687 intel_notify_mmio_flip(obj->ring);
9688 return 0;
9689}
9690
8c9f3aaf
JB
9691static int intel_default_queue_flip(struct drm_device *dev,
9692 struct drm_crtc *crtc,
9693 struct drm_framebuffer *fb,
ed8d1975 9694 struct drm_i915_gem_object *obj,
a4872ba6 9695 struct intel_engine_cs *ring,
ed8d1975 9696 uint32_t flags)
8c9f3aaf
JB
9697{
9698 return -ENODEV;
9699}
9700
d6bbafa1
CW
9701static bool __intel_pageflip_stall_check(struct drm_device *dev,
9702 struct drm_crtc *crtc)
9703{
9704 struct drm_i915_private *dev_priv = dev->dev_private;
9705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9706 struct intel_unpin_work *work = intel_crtc->unpin_work;
9707 u32 addr;
9708
9709 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9710 return true;
9711
9712 if (!work->enable_stall_check)
9713 return false;
9714
9715 if (work->flip_ready_vblank == 0) {
9716 if (work->flip_queued_ring &&
9717 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9718 work->flip_queued_seqno))
9719 return false;
9720
9721 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9722 }
9723
9724 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9725 return false;
9726
9727 /* Potential stall - if we see that the flip has happened,
9728 * assume a missed interrupt. */
9729 if (INTEL_INFO(dev)->gen >= 4)
9730 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9731 else
9732 addr = I915_READ(DSPADDR(intel_crtc->plane));
9733
9734 /* There is a potential issue here with a false positive after a flip
9735 * to the same address. We could address this by checking for a
9736 * non-incrementing frame counter.
9737 */
9738 return addr == work->gtt_offset;
9739}
9740
9741void intel_check_page_flip(struct drm_device *dev, int pipe)
9742{
9743 struct drm_i915_private *dev_priv = dev->dev_private;
9744 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f326038a
DV
9746
9747 WARN_ON(!in_irq());
d6bbafa1
CW
9748
9749 if (crtc == NULL)
9750 return;
9751
f326038a 9752 spin_lock(&dev->event_lock);
d6bbafa1
CW
9753 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9754 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9755 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9756 page_flip_completed(intel_crtc);
9757 }
f326038a 9758 spin_unlock(&dev->event_lock);
d6bbafa1
CW
9759}
9760
6b95a207
KH
9761static int intel_crtc_page_flip(struct drm_crtc *crtc,
9762 struct drm_framebuffer *fb,
ed8d1975
KP
9763 struct drm_pending_vblank_event *event,
9764 uint32_t page_flip_flags)
6b95a207
KH
9765{
9766 struct drm_device *dev = crtc->dev;
9767 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9768 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9769 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9771 enum pipe pipe = intel_crtc->pipe;
6b95a207 9772 struct intel_unpin_work *work;
a4872ba6 9773 struct intel_engine_cs *ring;
52e68630 9774 int ret;
6b95a207 9775
c76bb61a
DS
9776 //trigger software GT busyness calculation
9777 gen8_flip_interrupt(dev);
9778
2ff8fde1
MR
9779 /*
9780 * drm_mode_page_flip_ioctl() should already catch this, but double
9781 * check to be safe. In the future we may enable pageflipping from
9782 * a disabled primary plane.
9783 */
9784 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9785 return -EBUSY;
9786
e6a595d2 9787 /* Can't change pixel format via MI display flips. */
f4510a27 9788 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9789 return -EINVAL;
9790
9791 /*
9792 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9793 * Note that pitch changes could also affect these register.
9794 */
9795 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9796 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9797 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9798 return -EINVAL;
9799
f900db47
CW
9800 if (i915_terminally_wedged(&dev_priv->gpu_error))
9801 goto out_hang;
9802
b14c5679 9803 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9804 if (work == NULL)
9805 return -ENOMEM;
9806
6b95a207 9807 work->event = event;
b4a98e57 9808 work->crtc = crtc;
2ff8fde1 9809 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
9810 INIT_WORK(&work->work, intel_unpin_work_fn);
9811
87b6b101 9812 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9813 if (ret)
9814 goto free_work;
9815
6b95a207 9816 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 9817 spin_lock_irq(&dev->event_lock);
6b95a207 9818 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9819 /* Before declaring the flip queue wedged, check if
9820 * the hardware completed the operation behind our backs.
9821 */
9822 if (__intel_pageflip_stall_check(dev, crtc)) {
9823 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9824 page_flip_completed(intel_crtc);
9825 } else {
9826 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 9827 spin_unlock_irq(&dev->event_lock);
468f0b44 9828
d6bbafa1
CW
9829 drm_crtc_vblank_put(crtc);
9830 kfree(work);
9831 return -EBUSY;
9832 }
6b95a207
KH
9833 }
9834 intel_crtc->unpin_work = work;
5e2d7afc 9835 spin_unlock_irq(&dev->event_lock);
6b95a207 9836
b4a98e57
CW
9837 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9838 flush_workqueue(dev_priv->wq);
9839
79158103
CW
9840 ret = i915_mutex_lock_interruptible(dev);
9841 if (ret)
9842 goto cleanup;
6b95a207 9843
75dfca80 9844 /* Reference the objects for the scheduled work. */
05394f39
CW
9845 drm_gem_object_reference(&work->old_fb_obj->base);
9846 drm_gem_object_reference(&obj->base);
6b95a207 9847
f4510a27 9848 crtc->primary->fb = fb;
96b099fd 9849
e1f99ce6 9850 work->pending_flip_obj = obj;
e1f99ce6 9851
b4a98e57 9852 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9853 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9854
75f7f3ec 9855 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9856 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9857
4fa62c89
VS
9858 if (IS_VALLEYVIEW(dev)) {
9859 ring = &dev_priv->ring[BCS];
8e09bf83
CW
9860 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9861 /* vlv: DISPLAY_FLIP fails to change tiling */
9862 ring = NULL;
2a92d5bc
CW
9863 } else if (IS_IVYBRIDGE(dev)) {
9864 ring = &dev_priv->ring[BCS];
4fa62c89
VS
9865 } else if (INTEL_INFO(dev)->gen >= 7) {
9866 ring = obj->ring;
9867 if (ring == NULL || ring->id != RCS)
9868 ring = &dev_priv->ring[BCS];
9869 } else {
9870 ring = &dev_priv->ring[RCS];
9871 }
9872
9873 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9874 if (ret)
9875 goto cleanup_pending;
6b95a207 9876
4fa62c89
VS
9877 work->gtt_offset =
9878 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9879
d6bbafa1 9880 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
9881 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9882 page_flip_flags);
d6bbafa1
CW
9883 if (ret)
9884 goto cleanup_unpin;
9885
9886 work->flip_queued_seqno = obj->last_write_seqno;
9887 work->flip_queued_ring = obj->ring;
9888 } else {
84c33a64 9889 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
9890 page_flip_flags);
9891 if (ret)
9892 goto cleanup_unpin;
9893
9894 work->flip_queued_seqno = intel_ring_get_seqno(ring);
9895 work->flip_queued_ring = ring;
9896 }
9897
9898 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9899 work->enable_stall_check = true;
4fa62c89 9900
a071fa00
DV
9901 i915_gem_track_fb(work->old_fb_obj, obj,
9902 INTEL_FRONTBUFFER_PRIMARY(pipe));
9903
7782de3b 9904 intel_disable_fbc(dev);
f99d7069 9905 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9906 mutex_unlock(&dev->struct_mutex);
9907
e5510fac
JB
9908 trace_i915_flip_request(intel_crtc->plane, obj);
9909
6b95a207 9910 return 0;
96b099fd 9911
4fa62c89
VS
9912cleanup_unpin:
9913 intel_unpin_fb_obj(obj);
8c9f3aaf 9914cleanup_pending:
b4a98e57 9915 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9916 crtc->primary->fb = old_fb;
05394f39
CW
9917 drm_gem_object_unreference(&work->old_fb_obj->base);
9918 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9919 mutex_unlock(&dev->struct_mutex);
9920
79158103 9921cleanup:
5e2d7afc 9922 spin_lock_irq(&dev->event_lock);
96b099fd 9923 intel_crtc->unpin_work = NULL;
5e2d7afc 9924 spin_unlock_irq(&dev->event_lock);
96b099fd 9925
87b6b101 9926 drm_crtc_vblank_put(crtc);
7317c75e 9927free_work:
96b099fd
CW
9928 kfree(work);
9929
f900db47
CW
9930 if (ret == -EIO) {
9931out_hang:
9932 intel_crtc_wait_for_pending_flips(crtc);
9933 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
f0d3dad3 9934 if (ret == 0 && event) {
5e2d7afc 9935 spin_lock_irq(&dev->event_lock);
a071fa00 9936 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 9937 spin_unlock_irq(&dev->event_lock);
f0d3dad3 9938 }
f900db47 9939 }
96b099fd 9940 return ret;
6b95a207
KH
9941}
9942
f6e5b160 9943static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9944 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9945 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9946};
9947
9a935856
DV
9948/**
9949 * intel_modeset_update_staged_output_state
9950 *
9951 * Updates the staged output configuration state, e.g. after we've read out the
9952 * current hw state.
9953 */
9954static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9955{
7668851f 9956 struct intel_crtc *crtc;
9a935856
DV
9957 struct intel_encoder *encoder;
9958 struct intel_connector *connector;
f6e5b160 9959
9a935856
DV
9960 list_for_each_entry(connector, &dev->mode_config.connector_list,
9961 base.head) {
9962 connector->new_encoder =
9963 to_intel_encoder(connector->base.encoder);
9964 }
f6e5b160 9965
b2784e15 9966 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9967 encoder->new_crtc =
9968 to_intel_crtc(encoder->base.crtc);
9969 }
7668851f 9970
d3fcc808 9971 for_each_intel_crtc(dev, crtc) {
7668851f 9972 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9973
9974 if (crtc->new_enabled)
9975 crtc->new_config = &crtc->config;
9976 else
9977 crtc->new_config = NULL;
7668851f 9978 }
f6e5b160
CW
9979}
9980
9a935856
DV
9981/**
9982 * intel_modeset_commit_output_state
9983 *
9984 * This function copies the stage display pipe configuration to the real one.
9985 */
9986static void intel_modeset_commit_output_state(struct drm_device *dev)
9987{
7668851f 9988 struct intel_crtc *crtc;
9a935856
DV
9989 struct intel_encoder *encoder;
9990 struct intel_connector *connector;
f6e5b160 9991
9a935856
DV
9992 list_for_each_entry(connector, &dev->mode_config.connector_list,
9993 base.head) {
9994 connector->base.encoder = &connector->new_encoder->base;
9995 }
f6e5b160 9996
b2784e15 9997 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9998 encoder->base.crtc = &encoder->new_crtc->base;
9999 }
7668851f 10000
d3fcc808 10001 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10002 crtc->base.enabled = crtc->new_enabled;
10003 }
9a935856
DV
10004}
10005
050f7aeb 10006static void
eba905b2 10007connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
10008 struct intel_crtc_config *pipe_config)
10009{
10010 int bpp = pipe_config->pipe_bpp;
10011
10012 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10013 connector->base.base.id,
c23cc417 10014 connector->base.name);
050f7aeb
DV
10015
10016 /* Don't use an invalid EDID bpc value */
10017 if (connector->base.display_info.bpc &&
10018 connector->base.display_info.bpc * 3 < bpp) {
10019 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10020 bpp, connector->base.display_info.bpc*3);
10021 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10022 }
10023
10024 /* Clamp bpp to 8 on screens without EDID 1.4 */
10025 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10026 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10027 bpp);
10028 pipe_config->pipe_bpp = 24;
10029 }
10030}
10031
4e53c2e0 10032static int
050f7aeb
DV
10033compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10034 struct drm_framebuffer *fb,
10035 struct intel_crtc_config *pipe_config)
4e53c2e0 10036{
050f7aeb
DV
10037 struct drm_device *dev = crtc->base.dev;
10038 struct intel_connector *connector;
4e53c2e0
DV
10039 int bpp;
10040
d42264b1
DV
10041 switch (fb->pixel_format) {
10042 case DRM_FORMAT_C8:
4e53c2e0
DV
10043 bpp = 8*3; /* since we go through a colormap */
10044 break;
d42264b1
DV
10045 case DRM_FORMAT_XRGB1555:
10046 case DRM_FORMAT_ARGB1555:
10047 /* checked in intel_framebuffer_init already */
10048 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10049 return -EINVAL;
10050 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10051 bpp = 6*3; /* min is 18bpp */
10052 break;
d42264b1
DV
10053 case DRM_FORMAT_XBGR8888:
10054 case DRM_FORMAT_ABGR8888:
10055 /* checked in intel_framebuffer_init already */
10056 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10057 return -EINVAL;
10058 case DRM_FORMAT_XRGB8888:
10059 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10060 bpp = 8*3;
10061 break;
d42264b1
DV
10062 case DRM_FORMAT_XRGB2101010:
10063 case DRM_FORMAT_ARGB2101010:
10064 case DRM_FORMAT_XBGR2101010:
10065 case DRM_FORMAT_ABGR2101010:
10066 /* checked in intel_framebuffer_init already */
10067 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10068 return -EINVAL;
4e53c2e0
DV
10069 bpp = 10*3;
10070 break;
baba133a 10071 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10072 default:
10073 DRM_DEBUG_KMS("unsupported depth\n");
10074 return -EINVAL;
10075 }
10076
4e53c2e0
DV
10077 pipe_config->pipe_bpp = bpp;
10078
10079 /* Clamp display bpp to EDID value */
10080 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10081 base.head) {
1b829e05
DV
10082 if (!connector->new_encoder ||
10083 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10084 continue;
10085
050f7aeb 10086 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10087 }
10088
10089 return bpp;
10090}
10091
644db711
DV
10092static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10093{
10094 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10095 "type: 0x%x flags: 0x%x\n",
1342830c 10096 mode->crtc_clock,
644db711
DV
10097 mode->crtc_hdisplay, mode->crtc_hsync_start,
10098 mode->crtc_hsync_end, mode->crtc_htotal,
10099 mode->crtc_vdisplay, mode->crtc_vsync_start,
10100 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10101}
10102
c0b03411
DV
10103static void intel_dump_pipe_config(struct intel_crtc *crtc,
10104 struct intel_crtc_config *pipe_config,
10105 const char *context)
10106{
10107 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10108 context, pipe_name(crtc->pipe));
10109
10110 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10111 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10112 pipe_config->pipe_bpp, pipe_config->dither);
10113 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10114 pipe_config->has_pch_encoder,
10115 pipe_config->fdi_lanes,
10116 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10117 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10118 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10119 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10120 pipe_config->has_dp_encoder,
10121 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10122 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10123 pipe_config->dp_m_n.tu);
b95af8be
VK
10124
10125 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10126 pipe_config->has_dp_encoder,
10127 pipe_config->dp_m2_n2.gmch_m,
10128 pipe_config->dp_m2_n2.gmch_n,
10129 pipe_config->dp_m2_n2.link_m,
10130 pipe_config->dp_m2_n2.link_n,
10131 pipe_config->dp_m2_n2.tu);
10132
c0b03411
DV
10133 DRM_DEBUG_KMS("requested mode:\n");
10134 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10135 DRM_DEBUG_KMS("adjusted mode:\n");
10136 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10137 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10138 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10139 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10140 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10141 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10142 pipe_config->gmch_pfit.control,
10143 pipe_config->gmch_pfit.pgm_ratios,
10144 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10145 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10146 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10147 pipe_config->pch_pfit.size,
10148 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10149 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10150 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10151}
10152
bc079e8b
VS
10153static bool encoders_cloneable(const struct intel_encoder *a,
10154 const struct intel_encoder *b)
accfc0c5 10155{
bc079e8b
VS
10156 /* masks could be asymmetric, so check both ways */
10157 return a == b || (a->cloneable & (1 << b->type) &&
10158 b->cloneable & (1 << a->type));
10159}
10160
10161static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10162 struct intel_encoder *encoder)
10163{
10164 struct drm_device *dev = crtc->base.dev;
10165 struct intel_encoder *source_encoder;
10166
b2784e15 10167 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10168 if (source_encoder->new_crtc != crtc)
10169 continue;
10170
10171 if (!encoders_cloneable(encoder, source_encoder))
10172 return false;
10173 }
10174
10175 return true;
10176}
10177
10178static bool check_encoder_cloning(struct intel_crtc *crtc)
10179{
10180 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10181 struct intel_encoder *encoder;
10182
b2784e15 10183 for_each_intel_encoder(dev, encoder) {
bc079e8b 10184 if (encoder->new_crtc != crtc)
accfc0c5
DV
10185 continue;
10186
bc079e8b
VS
10187 if (!check_single_encoder_cloning(crtc, encoder))
10188 return false;
accfc0c5
DV
10189 }
10190
bc079e8b 10191 return true;
accfc0c5
DV
10192}
10193
b8cecdf5
DV
10194static struct intel_crtc_config *
10195intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10196 struct drm_framebuffer *fb,
b8cecdf5 10197 struct drm_display_mode *mode)
ee7b9f93 10198{
7758a113 10199 struct drm_device *dev = crtc->dev;
7758a113 10200 struct intel_encoder *encoder;
b8cecdf5 10201 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10202 int plane_bpp, ret = -EINVAL;
10203 bool retry = true;
ee7b9f93 10204
bc079e8b 10205 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10206 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10207 return ERR_PTR(-EINVAL);
10208 }
10209
b8cecdf5
DV
10210 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10211 if (!pipe_config)
7758a113
DV
10212 return ERR_PTR(-ENOMEM);
10213
b8cecdf5
DV
10214 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10215 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10216
e143a21c
DV
10217 pipe_config->cpu_transcoder =
10218 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10219 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10220
2960bc9c
ID
10221 /*
10222 * Sanitize sync polarity flags based on requested ones. If neither
10223 * positive or negative polarity is requested, treat this as meaning
10224 * negative polarity.
10225 */
10226 if (!(pipe_config->adjusted_mode.flags &
10227 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10228 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10229
10230 if (!(pipe_config->adjusted_mode.flags &
10231 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10232 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10233
050f7aeb
DV
10234 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10235 * plane pixel format and any sink constraints into account. Returns the
10236 * source plane bpp so that dithering can be selected on mismatches
10237 * after encoders and crtc also have had their say. */
10238 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10239 fb, pipe_config);
4e53c2e0
DV
10240 if (plane_bpp < 0)
10241 goto fail;
10242
e41a56be
VS
10243 /*
10244 * Determine the real pipe dimensions. Note that stereo modes can
10245 * increase the actual pipe size due to the frame doubling and
10246 * insertion of additional space for blanks between the frame. This
10247 * is stored in the crtc timings. We use the requested mode to do this
10248 * computation to clearly distinguish it from the adjusted mode, which
10249 * can be changed by the connectors in the below retry loop.
10250 */
10251 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10252 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10253 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10254
e29c22c0 10255encoder_retry:
ef1b460d 10256 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10257 pipe_config->port_clock = 0;
ef1b460d 10258 pipe_config->pixel_multiplier = 1;
ff9a6750 10259
135c81b8 10260 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10261 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10262
7758a113
DV
10263 /* Pass our mode to the connectors and the CRTC to give them a chance to
10264 * adjust it according to limitations or connector properties, and also
10265 * a chance to reject the mode entirely.
47f1c6c9 10266 */
b2784e15 10267 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10268
7758a113
DV
10269 if (&encoder->new_crtc->base != crtc)
10270 continue;
7ae89233 10271
efea6e8e
DV
10272 if (!(encoder->compute_config(encoder, pipe_config))) {
10273 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10274 goto fail;
10275 }
ee7b9f93 10276 }
47f1c6c9 10277
ff9a6750
DV
10278 /* Set default port clock if not overwritten by the encoder. Needs to be
10279 * done afterwards in case the encoder adjusts the mode. */
10280 if (!pipe_config->port_clock)
241bfc38
DL
10281 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10282 * pipe_config->pixel_multiplier;
ff9a6750 10283
a43f6e0f 10284 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10285 if (ret < 0) {
7758a113
DV
10286 DRM_DEBUG_KMS("CRTC fixup failed\n");
10287 goto fail;
ee7b9f93 10288 }
e29c22c0
DV
10289
10290 if (ret == RETRY) {
10291 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10292 ret = -EINVAL;
10293 goto fail;
10294 }
10295
10296 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10297 retry = false;
10298 goto encoder_retry;
10299 }
10300
4e53c2e0
DV
10301 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10302 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10303 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10304
b8cecdf5 10305 return pipe_config;
7758a113 10306fail:
b8cecdf5 10307 kfree(pipe_config);
e29c22c0 10308 return ERR_PTR(ret);
ee7b9f93 10309}
47f1c6c9 10310
e2e1ed41
DV
10311/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10312 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10313static void
10314intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10315 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10316{
10317 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10318 struct drm_device *dev = crtc->dev;
10319 struct intel_encoder *encoder;
10320 struct intel_connector *connector;
10321 struct drm_crtc *tmp_crtc;
79e53945 10322
e2e1ed41 10323 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10324
e2e1ed41
DV
10325 /* Check which crtcs have changed outputs connected to them, these need
10326 * to be part of the prepare_pipes mask. We don't (yet) support global
10327 * modeset across multiple crtcs, so modeset_pipes will only have one
10328 * bit set at most. */
10329 list_for_each_entry(connector, &dev->mode_config.connector_list,
10330 base.head) {
10331 if (connector->base.encoder == &connector->new_encoder->base)
10332 continue;
79e53945 10333
e2e1ed41
DV
10334 if (connector->base.encoder) {
10335 tmp_crtc = connector->base.encoder->crtc;
10336
10337 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10338 }
10339
10340 if (connector->new_encoder)
10341 *prepare_pipes |=
10342 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10343 }
10344
b2784e15 10345 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10346 if (encoder->base.crtc == &encoder->new_crtc->base)
10347 continue;
10348
10349 if (encoder->base.crtc) {
10350 tmp_crtc = encoder->base.crtc;
10351
10352 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10353 }
10354
10355 if (encoder->new_crtc)
10356 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10357 }
10358
7668851f 10359 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10360 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10361 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10362 continue;
7e7d76c3 10363
7668851f 10364 if (!intel_crtc->new_enabled)
e2e1ed41 10365 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10366 else
10367 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10368 }
10369
e2e1ed41
DV
10370
10371 /* set_mode is also used to update properties on life display pipes. */
10372 intel_crtc = to_intel_crtc(crtc);
7668851f 10373 if (intel_crtc->new_enabled)
e2e1ed41
DV
10374 *prepare_pipes |= 1 << intel_crtc->pipe;
10375
b6c5164d
DV
10376 /*
10377 * For simplicity do a full modeset on any pipe where the output routing
10378 * changed. We could be more clever, but that would require us to be
10379 * more careful with calling the relevant encoder->mode_set functions.
10380 */
e2e1ed41
DV
10381 if (*prepare_pipes)
10382 *modeset_pipes = *prepare_pipes;
10383
10384 /* ... and mask these out. */
10385 *modeset_pipes &= ~(*disable_pipes);
10386 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10387
10388 /*
10389 * HACK: We don't (yet) fully support global modesets. intel_set_config
10390 * obies this rule, but the modeset restore mode of
10391 * intel_modeset_setup_hw_state does not.
10392 */
10393 *modeset_pipes &= 1 << intel_crtc->pipe;
10394 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10395
10396 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10397 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10398}
79e53945 10399
ea9d758d 10400static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10401{
ea9d758d 10402 struct drm_encoder *encoder;
f6e5b160 10403 struct drm_device *dev = crtc->dev;
f6e5b160 10404
ea9d758d
DV
10405 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10406 if (encoder->crtc == crtc)
10407 return true;
10408
10409 return false;
10410}
10411
10412static void
10413intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10414{
10415 struct intel_encoder *intel_encoder;
10416 struct intel_crtc *intel_crtc;
10417 struct drm_connector *connector;
10418
b2784e15 10419 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10420 if (!intel_encoder->base.crtc)
10421 continue;
10422
10423 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10424
10425 if (prepare_pipes & (1 << intel_crtc->pipe))
10426 intel_encoder->connectors_active = false;
10427 }
10428
10429 intel_modeset_commit_output_state(dev);
10430
7668851f 10431 /* Double check state. */
d3fcc808 10432 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10433 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10434 WARN_ON(intel_crtc->new_config &&
10435 intel_crtc->new_config != &intel_crtc->config);
10436 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10437 }
10438
10439 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10440 if (!connector->encoder || !connector->encoder->crtc)
10441 continue;
10442
10443 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10444
10445 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10446 struct drm_property *dpms_property =
10447 dev->mode_config.dpms_property;
10448
ea9d758d 10449 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10450 drm_object_property_set_value(&connector->base,
68d34720
DV
10451 dpms_property,
10452 DRM_MODE_DPMS_ON);
ea9d758d
DV
10453
10454 intel_encoder = to_intel_encoder(connector->encoder);
10455 intel_encoder->connectors_active = true;
10456 }
10457 }
10458
10459}
10460
3bd26263 10461static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10462{
3bd26263 10463 int diff;
f1f644dc
JB
10464
10465 if (clock1 == clock2)
10466 return true;
10467
10468 if (!clock1 || !clock2)
10469 return false;
10470
10471 diff = abs(clock1 - clock2);
10472
10473 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10474 return true;
10475
10476 return false;
10477}
10478
25c5b266
DV
10479#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10480 list_for_each_entry((intel_crtc), \
10481 &(dev)->mode_config.crtc_list, \
10482 base.head) \
0973f18f 10483 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10484
0e8ffe1b 10485static bool
2fa2fe9a
DV
10486intel_pipe_config_compare(struct drm_device *dev,
10487 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10488 struct intel_crtc_config *pipe_config)
10489{
66e985c0
DV
10490#define PIPE_CONF_CHECK_X(name) \
10491 if (current_config->name != pipe_config->name) { \
10492 DRM_ERROR("mismatch in " #name " " \
10493 "(expected 0x%08x, found 0x%08x)\n", \
10494 current_config->name, \
10495 pipe_config->name); \
10496 return false; \
10497 }
10498
08a24034
DV
10499#define PIPE_CONF_CHECK_I(name) \
10500 if (current_config->name != pipe_config->name) { \
10501 DRM_ERROR("mismatch in " #name " " \
10502 "(expected %i, found %i)\n", \
10503 current_config->name, \
10504 pipe_config->name); \
10505 return false; \
88adfff1
DV
10506 }
10507
b95af8be
VK
10508/* This is required for BDW+ where there is only one set of registers for
10509 * switching between high and low RR.
10510 * This macro can be used whenever a comparison has to be made between one
10511 * hw state and multiple sw state variables.
10512 */
10513#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10514 if ((current_config->name != pipe_config->name) && \
10515 (current_config->alt_name != pipe_config->name)) { \
10516 DRM_ERROR("mismatch in " #name " " \
10517 "(expected %i or %i, found %i)\n", \
10518 current_config->name, \
10519 current_config->alt_name, \
10520 pipe_config->name); \
10521 return false; \
10522 }
10523
1bd1bd80
DV
10524#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10525 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10526 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10527 "(expected %i, found %i)\n", \
10528 current_config->name & (mask), \
10529 pipe_config->name & (mask)); \
10530 return false; \
10531 }
10532
5e550656
VS
10533#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10534 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10535 DRM_ERROR("mismatch in " #name " " \
10536 "(expected %i, found %i)\n", \
10537 current_config->name, \
10538 pipe_config->name); \
10539 return false; \
10540 }
10541
bb760063
DV
10542#define PIPE_CONF_QUIRK(quirk) \
10543 ((current_config->quirks | pipe_config->quirks) & (quirk))
10544
eccb140b
DV
10545 PIPE_CONF_CHECK_I(cpu_transcoder);
10546
08a24034
DV
10547 PIPE_CONF_CHECK_I(has_pch_encoder);
10548 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10549 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10550 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10551 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10552 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10553 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10554
eb14cb74 10555 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10556
10557 if (INTEL_INFO(dev)->gen < 8) {
10558 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10559 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10560 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10561 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10562 PIPE_CONF_CHECK_I(dp_m_n.tu);
10563
10564 if (current_config->has_drrs) {
10565 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10566 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10567 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10568 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10569 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10570 }
10571 } else {
10572 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10573 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10574 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10575 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10576 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10577 }
eb14cb74 10578
1bd1bd80
DV
10579 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10580 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10581 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10582 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10583 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10584 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10585
10586 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10587 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10588 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10589 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10590 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10591 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10592
c93f54cf 10593 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10594 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10595 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10596 IS_VALLEYVIEW(dev))
10597 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10598
9ed109a7
DV
10599 PIPE_CONF_CHECK_I(has_audio);
10600
1bd1bd80
DV
10601 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10602 DRM_MODE_FLAG_INTERLACE);
10603
bb760063
DV
10604 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10605 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10606 DRM_MODE_FLAG_PHSYNC);
10607 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10608 DRM_MODE_FLAG_NHSYNC);
10609 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10610 DRM_MODE_FLAG_PVSYNC);
10611 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10612 DRM_MODE_FLAG_NVSYNC);
10613 }
045ac3b5 10614
37327abd
VS
10615 PIPE_CONF_CHECK_I(pipe_src_w);
10616 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10617
9953599b
DV
10618 /*
10619 * FIXME: BIOS likes to set up a cloned config with lvds+external
10620 * screen. Since we don't yet re-compute the pipe config when moving
10621 * just the lvds port away to another pipe the sw tracking won't match.
10622 *
10623 * Proper atomic modesets with recomputed global state will fix this.
10624 * Until then just don't check gmch state for inherited modes.
10625 */
10626 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10627 PIPE_CONF_CHECK_I(gmch_pfit.control);
10628 /* pfit ratios are autocomputed by the hw on gen4+ */
10629 if (INTEL_INFO(dev)->gen < 4)
10630 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10631 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10632 }
10633
fd4daa9c
CW
10634 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10635 if (current_config->pch_pfit.enabled) {
10636 PIPE_CONF_CHECK_I(pch_pfit.pos);
10637 PIPE_CONF_CHECK_I(pch_pfit.size);
10638 }
2fa2fe9a 10639
e59150dc
JB
10640 /* BDW+ don't expose a synchronous way to read the state */
10641 if (IS_HASWELL(dev))
10642 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10643
282740f7
VS
10644 PIPE_CONF_CHECK_I(double_wide);
10645
26804afd
DV
10646 PIPE_CONF_CHECK_X(ddi_pll_sel);
10647
c0d43d62 10648 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10649 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10650 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10651 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10652 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10653 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10654
42571aef
VS
10655 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10656 PIPE_CONF_CHECK_I(pipe_bpp);
10657
a9a7e98a
JB
10658 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10659 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10660
66e985c0 10661#undef PIPE_CONF_CHECK_X
08a24034 10662#undef PIPE_CONF_CHECK_I
b95af8be 10663#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10664#undef PIPE_CONF_CHECK_FLAGS
5e550656 10665#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10666#undef PIPE_CONF_QUIRK
88adfff1 10667
0e8ffe1b
DV
10668 return true;
10669}
10670
91d1b4bd
DV
10671static void
10672check_connector_state(struct drm_device *dev)
8af6cf88 10673{
8af6cf88
DV
10674 struct intel_connector *connector;
10675
10676 list_for_each_entry(connector, &dev->mode_config.connector_list,
10677 base.head) {
10678 /* This also checks the encoder/connector hw state with the
10679 * ->get_hw_state callbacks. */
10680 intel_connector_check_state(connector);
10681
10682 WARN(&connector->new_encoder->base != connector->base.encoder,
10683 "connector's staged encoder doesn't match current encoder\n");
10684 }
91d1b4bd
DV
10685}
10686
10687static void
10688check_encoder_state(struct drm_device *dev)
10689{
10690 struct intel_encoder *encoder;
10691 struct intel_connector *connector;
8af6cf88 10692
b2784e15 10693 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10694 bool enabled = false;
10695 bool active = false;
10696 enum pipe pipe, tracked_pipe;
10697
10698 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10699 encoder->base.base.id,
8e329a03 10700 encoder->base.name);
8af6cf88
DV
10701
10702 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10703 "encoder's stage crtc doesn't match current crtc\n");
10704 WARN(encoder->connectors_active && !encoder->base.crtc,
10705 "encoder's active_connectors set, but no crtc\n");
10706
10707 list_for_each_entry(connector, &dev->mode_config.connector_list,
10708 base.head) {
10709 if (connector->base.encoder != &encoder->base)
10710 continue;
10711 enabled = true;
10712 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10713 active = true;
10714 }
0e32b39c
DA
10715 /*
10716 * for MST connectors if we unplug the connector is gone
10717 * away but the encoder is still connected to a crtc
10718 * until a modeset happens in response to the hotplug.
10719 */
10720 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10721 continue;
10722
8af6cf88
DV
10723 WARN(!!encoder->base.crtc != enabled,
10724 "encoder's enabled state mismatch "
10725 "(expected %i, found %i)\n",
10726 !!encoder->base.crtc, enabled);
10727 WARN(active && !encoder->base.crtc,
10728 "active encoder with no crtc\n");
10729
10730 WARN(encoder->connectors_active != active,
10731 "encoder's computed active state doesn't match tracked active state "
10732 "(expected %i, found %i)\n", active, encoder->connectors_active);
10733
10734 active = encoder->get_hw_state(encoder, &pipe);
10735 WARN(active != encoder->connectors_active,
10736 "encoder's hw state doesn't match sw tracking "
10737 "(expected %i, found %i)\n",
10738 encoder->connectors_active, active);
10739
10740 if (!encoder->base.crtc)
10741 continue;
10742
10743 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10744 WARN(active && pipe != tracked_pipe,
10745 "active encoder's pipe doesn't match"
10746 "(expected %i, found %i)\n",
10747 tracked_pipe, pipe);
10748
10749 }
91d1b4bd
DV
10750}
10751
10752static void
10753check_crtc_state(struct drm_device *dev)
10754{
fbee40df 10755 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10756 struct intel_crtc *crtc;
10757 struct intel_encoder *encoder;
10758 struct intel_crtc_config pipe_config;
8af6cf88 10759
d3fcc808 10760 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10761 bool enabled = false;
10762 bool active = false;
10763
045ac3b5
JB
10764 memset(&pipe_config, 0, sizeof(pipe_config));
10765
8af6cf88
DV
10766 DRM_DEBUG_KMS("[CRTC:%d]\n",
10767 crtc->base.base.id);
10768
10769 WARN(crtc->active && !crtc->base.enabled,
10770 "active crtc, but not enabled in sw tracking\n");
10771
b2784e15 10772 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10773 if (encoder->base.crtc != &crtc->base)
10774 continue;
10775 enabled = true;
10776 if (encoder->connectors_active)
10777 active = true;
10778 }
6c49f241 10779
8af6cf88
DV
10780 WARN(active != crtc->active,
10781 "crtc's computed active state doesn't match tracked active state "
10782 "(expected %i, found %i)\n", active, crtc->active);
10783 WARN(enabled != crtc->base.enabled,
10784 "crtc's computed enabled state doesn't match tracked enabled state "
10785 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10786
0e8ffe1b
DV
10787 active = dev_priv->display.get_pipe_config(crtc,
10788 &pipe_config);
d62cf62a 10789
b6b5d049
VS
10790 /* hw state is inconsistent with the pipe quirk */
10791 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10792 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10793 active = crtc->active;
10794
b2784e15 10795 for_each_intel_encoder(dev, encoder) {
3eaba51c 10796 enum pipe pipe;
6c49f241
DV
10797 if (encoder->base.crtc != &crtc->base)
10798 continue;
1d37b689 10799 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10800 encoder->get_config(encoder, &pipe_config);
10801 }
10802
0e8ffe1b
DV
10803 WARN(crtc->active != active,
10804 "crtc active state doesn't match with hw state "
10805 "(expected %i, found %i)\n", crtc->active, active);
10806
c0b03411
DV
10807 if (active &&
10808 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10809 WARN(1, "pipe state doesn't match!\n");
10810 intel_dump_pipe_config(crtc, &pipe_config,
10811 "[hw state]");
10812 intel_dump_pipe_config(crtc, &crtc->config,
10813 "[sw state]");
10814 }
8af6cf88
DV
10815 }
10816}
10817
91d1b4bd
DV
10818static void
10819check_shared_dpll_state(struct drm_device *dev)
10820{
fbee40df 10821 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10822 struct intel_crtc *crtc;
10823 struct intel_dpll_hw_state dpll_hw_state;
10824 int i;
5358901f
DV
10825
10826 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10827 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10828 int enabled_crtcs = 0, active_crtcs = 0;
10829 bool active;
10830
10831 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10832
10833 DRM_DEBUG_KMS("%s\n", pll->name);
10834
10835 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10836
10837 WARN(pll->active > pll->refcount,
10838 "more active pll users than references: %i vs %i\n",
10839 pll->active, pll->refcount);
10840 WARN(pll->active && !pll->on,
10841 "pll in active use but not on in sw tracking\n");
35c95375
DV
10842 WARN(pll->on && !pll->active,
10843 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10844 WARN(pll->on != active,
10845 "pll on state mismatch (expected %i, found %i)\n",
10846 pll->on, active);
10847
d3fcc808 10848 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10849 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10850 enabled_crtcs++;
10851 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10852 active_crtcs++;
10853 }
10854 WARN(pll->active != active_crtcs,
10855 "pll active crtcs mismatch (expected %i, found %i)\n",
10856 pll->active, active_crtcs);
10857 WARN(pll->refcount != enabled_crtcs,
10858 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10859 pll->refcount, enabled_crtcs);
66e985c0
DV
10860
10861 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10862 sizeof(dpll_hw_state)),
10863 "pll hw state mismatch\n");
5358901f 10864 }
8af6cf88
DV
10865}
10866
91d1b4bd
DV
10867void
10868intel_modeset_check_state(struct drm_device *dev)
10869{
10870 check_connector_state(dev);
10871 check_encoder_state(dev);
10872 check_crtc_state(dev);
10873 check_shared_dpll_state(dev);
10874}
10875
18442d08
VS
10876void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10877 int dotclock)
10878{
10879 /*
10880 * FDI already provided one idea for the dotclock.
10881 * Yell if the encoder disagrees.
10882 */
241bfc38 10883 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10884 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10885 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10886}
10887
80715b2f
VS
10888static void update_scanline_offset(struct intel_crtc *crtc)
10889{
10890 struct drm_device *dev = crtc->base.dev;
10891
10892 /*
10893 * The scanline counter increments at the leading edge of hsync.
10894 *
10895 * On most platforms it starts counting from vtotal-1 on the
10896 * first active line. That means the scanline counter value is
10897 * always one less than what we would expect. Ie. just after
10898 * start of vblank, which also occurs at start of hsync (on the
10899 * last active line), the scanline counter will read vblank_start-1.
10900 *
10901 * On gen2 the scanline counter starts counting from 1 instead
10902 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10903 * to keep the value positive), instead of adding one.
10904 *
10905 * On HSW+ the behaviour of the scanline counter depends on the output
10906 * type. For DP ports it behaves like most other platforms, but on HDMI
10907 * there's an extra 1 line difference. So we need to add two instead of
10908 * one to the value.
10909 */
10910 if (IS_GEN2(dev)) {
10911 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10912 int vtotal;
10913
10914 vtotal = mode->crtc_vtotal;
10915 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10916 vtotal /= 2;
10917
10918 crtc->scanline_offset = vtotal - 1;
10919 } else if (HAS_DDI(dev) &&
10920 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10921 crtc->scanline_offset = 2;
10922 } else
10923 crtc->scanline_offset = 1;
10924}
10925
f30da187
DV
10926static int __intel_set_mode(struct drm_crtc *crtc,
10927 struct drm_display_mode *mode,
10928 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10929{
10930 struct drm_device *dev = crtc->dev;
fbee40df 10931 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10932 struct drm_display_mode *saved_mode;
b8cecdf5 10933 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10934 struct intel_crtc *intel_crtc;
10935 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10936 int ret = 0;
a6778b3c 10937
4b4b9238 10938 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10939 if (!saved_mode)
10940 return -ENOMEM;
a6778b3c 10941
e2e1ed41 10942 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10943 &prepare_pipes, &disable_pipes);
10944
3ac18232 10945 *saved_mode = crtc->mode;
a6778b3c 10946
25c5b266
DV
10947 /* Hack: Because we don't (yet) support global modeset on multiple
10948 * crtcs, we don't keep track of the new mode for more than one crtc.
10949 * Hence simply check whether any bit is set in modeset_pipes in all the
10950 * pieces of code that are not yet converted to deal with mutliple crtcs
10951 * changing their mode at the same time. */
25c5b266 10952 if (modeset_pipes) {
4e53c2e0 10953 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10954 if (IS_ERR(pipe_config)) {
10955 ret = PTR_ERR(pipe_config);
10956 pipe_config = NULL;
10957
3ac18232 10958 goto out;
25c5b266 10959 }
c0b03411
DV
10960 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10961 "[modeset]");
50741abc 10962 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10963 }
a6778b3c 10964
30a970c6
JB
10965 /*
10966 * See if the config requires any additional preparation, e.g.
10967 * to adjust global state with pipes off. We need to do this
10968 * here so we can get the modeset_pipe updated config for the new
10969 * mode set on this crtc. For other crtcs we need to use the
10970 * adjusted_mode bits in the crtc directly.
10971 */
c164f833 10972 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10973 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10974
c164f833
VS
10975 /* may have added more to prepare_pipes than we should */
10976 prepare_pipes &= ~disable_pipes;
10977 }
10978
460da916
DV
10979 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10980 intel_crtc_disable(&intel_crtc->base);
10981
ea9d758d
DV
10982 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10983 if (intel_crtc->base.enabled)
10984 dev_priv->display.crtc_disable(&intel_crtc->base);
10985 }
a6778b3c 10986
6c4c86f5
DV
10987 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10988 * to set it here already despite that we pass it down the callchain.
f6e5b160 10989 */
b8cecdf5 10990 if (modeset_pipes) {
25c5b266 10991 crtc->mode = *mode;
b8cecdf5
DV
10992 /* mode_set/enable/disable functions rely on a correct pipe
10993 * config. */
10994 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10995 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10996
10997 /*
10998 * Calculate and store various constants which
10999 * are later needed by vblank and swap-completion
11000 * timestamping. They are derived from true hwmode.
11001 */
11002 drm_calc_timestamping_constants(crtc,
11003 &pipe_config->adjusted_mode);
b8cecdf5 11004 }
7758a113 11005
ea9d758d
DV
11006 /* Only after disabling all output pipelines that will be changed can we
11007 * update the the output configuration. */
11008 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11009
47fab737
DV
11010 if (dev_priv->display.modeset_global_resources)
11011 dev_priv->display.modeset_global_resources(dev);
11012
a6778b3c
DV
11013 /* Set up the DPLL and any encoders state that needs to adjust or depend
11014 * on the DPLL.
f6e5b160 11015 */
25c5b266 11016 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
11017 struct drm_framebuffer *old_fb = crtc->primary->fb;
11018 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11019 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
11020
11021 mutex_lock(&dev->struct_mutex);
11022 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 11023 obj,
4c10794f
DV
11024 NULL);
11025 if (ret != 0) {
11026 DRM_ERROR("pin & fence failed\n");
11027 mutex_unlock(&dev->struct_mutex);
11028 goto done;
11029 }
2ff8fde1 11030 if (old_fb)
a071fa00 11031 intel_unpin_fb_obj(old_obj);
a071fa00
DV
11032 i915_gem_track_fb(old_obj, obj,
11033 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
11034 mutex_unlock(&dev->struct_mutex);
11035
11036 crtc->primary->fb = fb;
11037 crtc->x = x;
11038 crtc->y = y;
11039
4271b753
DV
11040 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11041 x, y, fb);
c0c36b94
CW
11042 if (ret)
11043 goto done;
a6778b3c
DV
11044 }
11045
11046 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11047 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11048 update_scanline_offset(intel_crtc);
11049
25c5b266 11050 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11051 }
a6778b3c 11052
a6778b3c
DV
11053 /* FIXME: add subpixel order */
11054done:
4b4b9238 11055 if (ret && crtc->enabled)
3ac18232 11056 crtc->mode = *saved_mode;
a6778b3c 11057
3ac18232 11058out:
b8cecdf5 11059 kfree(pipe_config);
3ac18232 11060 kfree(saved_mode);
a6778b3c 11061 return ret;
f6e5b160
CW
11062}
11063
e7457a9a
DL
11064static int intel_set_mode(struct drm_crtc *crtc,
11065 struct drm_display_mode *mode,
11066 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11067{
11068 int ret;
11069
11070 ret = __intel_set_mode(crtc, mode, x, y, fb);
11071
11072 if (ret == 0)
11073 intel_modeset_check_state(crtc->dev);
11074
11075 return ret;
11076}
11077
c0c36b94
CW
11078void intel_crtc_restore_mode(struct drm_crtc *crtc)
11079{
f4510a27 11080 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11081}
11082
25c5b266
DV
11083#undef for_each_intel_crtc_masked
11084
d9e55608
DV
11085static void intel_set_config_free(struct intel_set_config *config)
11086{
11087 if (!config)
11088 return;
11089
1aa4b628
DV
11090 kfree(config->save_connector_encoders);
11091 kfree(config->save_encoder_crtcs);
7668851f 11092 kfree(config->save_crtc_enabled);
d9e55608
DV
11093 kfree(config);
11094}
11095
85f9eb71
DV
11096static int intel_set_config_save_state(struct drm_device *dev,
11097 struct intel_set_config *config)
11098{
7668851f 11099 struct drm_crtc *crtc;
85f9eb71
DV
11100 struct drm_encoder *encoder;
11101 struct drm_connector *connector;
11102 int count;
11103
7668851f
VS
11104 config->save_crtc_enabled =
11105 kcalloc(dev->mode_config.num_crtc,
11106 sizeof(bool), GFP_KERNEL);
11107 if (!config->save_crtc_enabled)
11108 return -ENOMEM;
11109
1aa4b628
DV
11110 config->save_encoder_crtcs =
11111 kcalloc(dev->mode_config.num_encoder,
11112 sizeof(struct drm_crtc *), GFP_KERNEL);
11113 if (!config->save_encoder_crtcs)
85f9eb71
DV
11114 return -ENOMEM;
11115
1aa4b628
DV
11116 config->save_connector_encoders =
11117 kcalloc(dev->mode_config.num_connector,
11118 sizeof(struct drm_encoder *), GFP_KERNEL);
11119 if (!config->save_connector_encoders)
85f9eb71
DV
11120 return -ENOMEM;
11121
11122 /* Copy data. Note that driver private data is not affected.
11123 * Should anything bad happen only the expected state is
11124 * restored, not the drivers personal bookkeeping.
11125 */
7668851f 11126 count = 0;
70e1e0ec 11127 for_each_crtc(dev, crtc) {
7668851f
VS
11128 config->save_crtc_enabled[count++] = crtc->enabled;
11129 }
11130
85f9eb71
DV
11131 count = 0;
11132 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11133 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11134 }
11135
11136 count = 0;
11137 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11138 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11139 }
11140
11141 return 0;
11142}
11143
11144static void intel_set_config_restore_state(struct drm_device *dev,
11145 struct intel_set_config *config)
11146{
7668851f 11147 struct intel_crtc *crtc;
9a935856
DV
11148 struct intel_encoder *encoder;
11149 struct intel_connector *connector;
85f9eb71
DV
11150 int count;
11151
7668851f 11152 count = 0;
d3fcc808 11153 for_each_intel_crtc(dev, crtc) {
7668851f 11154 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11155
11156 if (crtc->new_enabled)
11157 crtc->new_config = &crtc->config;
11158 else
11159 crtc->new_config = NULL;
7668851f
VS
11160 }
11161
85f9eb71 11162 count = 0;
b2784e15 11163 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11164 encoder->new_crtc =
11165 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11166 }
11167
11168 count = 0;
9a935856
DV
11169 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11170 connector->new_encoder =
11171 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11172 }
11173}
11174
e3de42b6 11175static bool
2e57f47d 11176is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11177{
11178 int i;
11179
2e57f47d
CW
11180 if (set->num_connectors == 0)
11181 return false;
11182
11183 if (WARN_ON(set->connectors == NULL))
11184 return false;
11185
11186 for (i = 0; i < set->num_connectors; i++)
11187 if (set->connectors[i]->encoder &&
11188 set->connectors[i]->encoder->crtc == set->crtc &&
11189 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11190 return true;
11191
11192 return false;
11193}
11194
5e2b584e
DV
11195static void
11196intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11197 struct intel_set_config *config)
11198{
11199
11200 /* We should be able to check here if the fb has the same properties
11201 * and then just flip_or_move it */
2e57f47d
CW
11202 if (is_crtc_connector_off(set)) {
11203 config->mode_changed = true;
f4510a27 11204 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11205 /*
11206 * If we have no fb, we can only flip as long as the crtc is
11207 * active, otherwise we need a full mode set. The crtc may
11208 * be active if we've only disabled the primary plane, or
11209 * in fastboot situations.
11210 */
f4510a27 11211 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11212 struct intel_crtc *intel_crtc =
11213 to_intel_crtc(set->crtc);
11214
3b150f08 11215 if (intel_crtc->active) {
319d9827
JB
11216 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11217 config->fb_changed = true;
11218 } else {
11219 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11220 config->mode_changed = true;
11221 }
5e2b584e
DV
11222 } else if (set->fb == NULL) {
11223 config->mode_changed = true;
72f4901e 11224 } else if (set->fb->pixel_format !=
f4510a27 11225 set->crtc->primary->fb->pixel_format) {
5e2b584e 11226 config->mode_changed = true;
e3de42b6 11227 } else {
5e2b584e 11228 config->fb_changed = true;
e3de42b6 11229 }
5e2b584e
DV
11230 }
11231
835c5873 11232 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11233 config->fb_changed = true;
11234
11235 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11236 DRM_DEBUG_KMS("modes are different, full mode set\n");
11237 drm_mode_debug_printmodeline(&set->crtc->mode);
11238 drm_mode_debug_printmodeline(set->mode);
11239 config->mode_changed = true;
11240 }
a1d95703
CW
11241
11242 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11243 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11244}
11245
2e431051 11246static int
9a935856
DV
11247intel_modeset_stage_output_state(struct drm_device *dev,
11248 struct drm_mode_set *set,
11249 struct intel_set_config *config)
50f56119 11250{
9a935856
DV
11251 struct intel_connector *connector;
11252 struct intel_encoder *encoder;
7668851f 11253 struct intel_crtc *crtc;
f3f08572 11254 int ro;
50f56119 11255
9abdda74 11256 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11257 * of connectors. For paranoia, double-check this. */
11258 WARN_ON(!set->fb && (set->num_connectors != 0));
11259 WARN_ON(set->fb && (set->num_connectors == 0));
11260
9a935856
DV
11261 list_for_each_entry(connector, &dev->mode_config.connector_list,
11262 base.head) {
11263 /* Otherwise traverse passed in connector list and get encoders
11264 * for them. */
50f56119 11265 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11266 if (set->connectors[ro] == &connector->base) {
0e32b39c 11267 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11268 break;
11269 }
11270 }
11271
9a935856
DV
11272 /* If we disable the crtc, disable all its connectors. Also, if
11273 * the connector is on the changing crtc but not on the new
11274 * connector list, disable it. */
11275 if ((!set->fb || ro == set->num_connectors) &&
11276 connector->base.encoder &&
11277 connector->base.encoder->crtc == set->crtc) {
11278 connector->new_encoder = NULL;
11279
11280 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11281 connector->base.base.id,
c23cc417 11282 connector->base.name);
9a935856
DV
11283 }
11284
11285
11286 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11287 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11288 config->mode_changed = true;
50f56119
DV
11289 }
11290 }
9a935856 11291 /* connector->new_encoder is now updated for all connectors. */
50f56119 11292
9a935856 11293 /* Update crtc of enabled connectors. */
9a935856
DV
11294 list_for_each_entry(connector, &dev->mode_config.connector_list,
11295 base.head) {
7668851f
VS
11296 struct drm_crtc *new_crtc;
11297
9a935856 11298 if (!connector->new_encoder)
50f56119
DV
11299 continue;
11300
9a935856 11301 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11302
11303 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11304 if (set->connectors[ro] == &connector->base)
50f56119
DV
11305 new_crtc = set->crtc;
11306 }
11307
11308 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11309 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11310 new_crtc)) {
5e2b584e 11311 return -EINVAL;
50f56119 11312 }
0e32b39c 11313 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11314
11315 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11316 connector->base.base.id,
c23cc417 11317 connector->base.name,
9a935856
DV
11318 new_crtc->base.id);
11319 }
11320
11321 /* Check for any encoders that needs to be disabled. */
b2784e15 11322 for_each_intel_encoder(dev, encoder) {
5a65f358 11323 int num_connectors = 0;
9a935856
DV
11324 list_for_each_entry(connector,
11325 &dev->mode_config.connector_list,
11326 base.head) {
11327 if (connector->new_encoder == encoder) {
11328 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11329 num_connectors++;
9a935856
DV
11330 }
11331 }
5a65f358
PZ
11332
11333 if (num_connectors == 0)
11334 encoder->new_crtc = NULL;
11335 else if (num_connectors > 1)
11336 return -EINVAL;
11337
9a935856
DV
11338 /* Only now check for crtc changes so we don't miss encoders
11339 * that will be disabled. */
11340 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11341 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11342 config->mode_changed = true;
50f56119
DV
11343 }
11344 }
9a935856 11345 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11346 list_for_each_entry(connector, &dev->mode_config.connector_list,
11347 base.head) {
11348 if (connector->new_encoder)
11349 if (connector->new_encoder != connector->encoder)
11350 connector->encoder = connector->new_encoder;
11351 }
d3fcc808 11352 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11353 crtc->new_enabled = false;
11354
b2784e15 11355 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11356 if (encoder->new_crtc == crtc) {
11357 crtc->new_enabled = true;
11358 break;
11359 }
11360 }
11361
11362 if (crtc->new_enabled != crtc->base.enabled) {
11363 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11364 crtc->new_enabled ? "en" : "dis");
11365 config->mode_changed = true;
11366 }
7bd0a8e7
VS
11367
11368 if (crtc->new_enabled)
11369 crtc->new_config = &crtc->config;
11370 else
11371 crtc->new_config = NULL;
7668851f
VS
11372 }
11373
2e431051
DV
11374 return 0;
11375}
11376
7d00a1f5
VS
11377static void disable_crtc_nofb(struct intel_crtc *crtc)
11378{
11379 struct drm_device *dev = crtc->base.dev;
11380 struct intel_encoder *encoder;
11381 struct intel_connector *connector;
11382
11383 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11384 pipe_name(crtc->pipe));
11385
11386 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11387 if (connector->new_encoder &&
11388 connector->new_encoder->new_crtc == crtc)
11389 connector->new_encoder = NULL;
11390 }
11391
b2784e15 11392 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11393 if (encoder->new_crtc == crtc)
11394 encoder->new_crtc = NULL;
11395 }
11396
11397 crtc->new_enabled = false;
7bd0a8e7 11398 crtc->new_config = NULL;
7d00a1f5
VS
11399}
11400
2e431051
DV
11401static int intel_crtc_set_config(struct drm_mode_set *set)
11402{
11403 struct drm_device *dev;
2e431051
DV
11404 struct drm_mode_set save_set;
11405 struct intel_set_config *config;
11406 int ret;
2e431051 11407
8d3e375e
DV
11408 BUG_ON(!set);
11409 BUG_ON(!set->crtc);
11410 BUG_ON(!set->crtc->helper_private);
2e431051 11411
7e53f3a4
DV
11412 /* Enforce sane interface api - has been abused by the fb helper. */
11413 BUG_ON(!set->mode && set->fb);
11414 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11415
2e431051
DV
11416 if (set->fb) {
11417 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11418 set->crtc->base.id, set->fb->base.id,
11419 (int)set->num_connectors, set->x, set->y);
11420 } else {
11421 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11422 }
11423
11424 dev = set->crtc->dev;
11425
11426 ret = -ENOMEM;
11427 config = kzalloc(sizeof(*config), GFP_KERNEL);
11428 if (!config)
11429 goto out_config;
11430
11431 ret = intel_set_config_save_state(dev, config);
11432 if (ret)
11433 goto out_config;
11434
11435 save_set.crtc = set->crtc;
11436 save_set.mode = &set->crtc->mode;
11437 save_set.x = set->crtc->x;
11438 save_set.y = set->crtc->y;
f4510a27 11439 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11440
11441 /* Compute whether we need a full modeset, only an fb base update or no
11442 * change at all. In the future we might also check whether only the
11443 * mode changed, e.g. for LVDS where we only change the panel fitter in
11444 * such cases. */
11445 intel_set_config_compute_mode_changes(set, config);
11446
9a935856 11447 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11448 if (ret)
11449 goto fail;
11450
5e2b584e 11451 if (config->mode_changed) {
c0c36b94
CW
11452 ret = intel_set_mode(set->crtc, set->mode,
11453 set->x, set->y, set->fb);
5e2b584e 11454 } else if (config->fb_changed) {
3b150f08
MR
11455 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11456
4878cae2
VS
11457 intel_crtc_wait_for_pending_flips(set->crtc);
11458
4f660f49 11459 ret = intel_pipe_set_base(set->crtc,
94352cf9 11460 set->x, set->y, set->fb);
3b150f08
MR
11461
11462 /*
11463 * We need to make sure the primary plane is re-enabled if it
11464 * has previously been turned off.
11465 */
11466 if (!intel_crtc->primary_enabled && ret == 0) {
11467 WARN_ON(!intel_crtc->active);
fdd508a6 11468 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11469 }
11470
7ca51a3a
JB
11471 /*
11472 * In the fastboot case this may be our only check of the
11473 * state after boot. It would be better to only do it on
11474 * the first update, but we don't have a nice way of doing that
11475 * (and really, set_config isn't used much for high freq page
11476 * flipping, so increasing its cost here shouldn't be a big
11477 * deal).
11478 */
d330a953 11479 if (i915.fastboot && ret == 0)
7ca51a3a 11480 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11481 }
11482
2d05eae1 11483 if (ret) {
bf67dfeb
DV
11484 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11485 set->crtc->base.id, ret);
50f56119 11486fail:
2d05eae1 11487 intel_set_config_restore_state(dev, config);
50f56119 11488
7d00a1f5
VS
11489 /*
11490 * HACK: if the pipe was on, but we didn't have a framebuffer,
11491 * force the pipe off to avoid oopsing in the modeset code
11492 * due to fb==NULL. This should only happen during boot since
11493 * we don't yet reconstruct the FB from the hardware state.
11494 */
11495 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11496 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11497
2d05eae1
CW
11498 /* Try to restore the config */
11499 if (config->mode_changed &&
11500 intel_set_mode(save_set.crtc, save_set.mode,
11501 save_set.x, save_set.y, save_set.fb))
11502 DRM_ERROR("failed to restore config after modeset failure\n");
11503 }
50f56119 11504
d9e55608
DV
11505out_config:
11506 intel_set_config_free(config);
50f56119
DV
11507 return ret;
11508}
f6e5b160
CW
11509
11510static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11511 .gamma_set = intel_crtc_gamma_set,
50f56119 11512 .set_config = intel_crtc_set_config,
f6e5b160
CW
11513 .destroy = intel_crtc_destroy,
11514 .page_flip = intel_crtc_page_flip,
11515};
11516
5358901f
DV
11517static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11518 struct intel_shared_dpll *pll,
11519 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11520{
5358901f 11521 uint32_t val;
ee7b9f93 11522
f458ebbc 11523 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
11524 return false;
11525
5358901f 11526 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11527 hw_state->dpll = val;
11528 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11529 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11530
11531 return val & DPLL_VCO_ENABLE;
11532}
11533
15bdd4cf
DV
11534static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11535 struct intel_shared_dpll *pll)
11536{
11537 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11538 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11539}
11540
e7b903d2
DV
11541static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11542 struct intel_shared_dpll *pll)
11543{
e7b903d2 11544 /* PCH refclock must be enabled first */
89eff4be 11545 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11546
15bdd4cf
DV
11547 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11548
11549 /* Wait for the clocks to stabilize. */
11550 POSTING_READ(PCH_DPLL(pll->id));
11551 udelay(150);
11552
11553 /* The pixel multiplier can only be updated once the
11554 * DPLL is enabled and the clocks are stable.
11555 *
11556 * So write it again.
11557 */
11558 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11559 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11560 udelay(200);
11561}
11562
11563static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11564 struct intel_shared_dpll *pll)
11565{
11566 struct drm_device *dev = dev_priv->dev;
11567 struct intel_crtc *crtc;
e7b903d2
DV
11568
11569 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11570 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11571 if (intel_crtc_to_shared_dpll(crtc) == pll)
11572 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11573 }
11574
15bdd4cf
DV
11575 I915_WRITE(PCH_DPLL(pll->id), 0);
11576 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11577 udelay(200);
11578}
11579
46edb027
DV
11580static char *ibx_pch_dpll_names[] = {
11581 "PCH DPLL A",
11582 "PCH DPLL B",
11583};
11584
7c74ade1 11585static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11586{
e7b903d2 11587 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11588 int i;
11589
7c74ade1 11590 dev_priv->num_shared_dpll = 2;
ee7b9f93 11591
e72f9fbf 11592 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11593 dev_priv->shared_dplls[i].id = i;
11594 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11595 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11596 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11597 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11598 dev_priv->shared_dplls[i].get_hw_state =
11599 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11600 }
11601}
11602
7c74ade1
DV
11603static void intel_shared_dpll_init(struct drm_device *dev)
11604{
e7b903d2 11605 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11606
9cd86933
DV
11607 if (HAS_DDI(dev))
11608 intel_ddi_pll_init(dev);
11609 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11610 ibx_pch_dpll_init(dev);
11611 else
11612 dev_priv->num_shared_dpll = 0;
11613
11614 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11615}
11616
465c120c
MR
11617static int
11618intel_primary_plane_disable(struct drm_plane *plane)
11619{
11620 struct drm_device *dev = plane->dev;
465c120c
MR
11621 struct intel_crtc *intel_crtc;
11622
11623 if (!plane->fb)
11624 return 0;
11625
11626 BUG_ON(!plane->crtc);
11627
11628 intel_crtc = to_intel_crtc(plane->crtc);
11629
11630 /*
11631 * Even though we checked plane->fb above, it's still possible that
11632 * the primary plane has been implicitly disabled because the crtc
11633 * coordinates given weren't visible, or because we detected
11634 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11635 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11636 * In either case, we need to unpin the FB and let the fb pointer get
11637 * updated, but otherwise we don't need to touch the hardware.
11638 */
11639 if (!intel_crtc->primary_enabled)
11640 goto disable_unpin;
11641
11642 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11643 intel_disable_primary_hw_plane(plane, plane->crtc);
11644
465c120c 11645disable_unpin:
4c34574f 11646 mutex_lock(&dev->struct_mutex);
2ff8fde1 11647 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11648 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11649 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11650 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11651 plane->fb = NULL;
11652
11653 return 0;
11654}
11655
11656static int
3c692a41
GP
11657intel_check_primary_plane(struct drm_plane *plane,
11658 struct intel_plane_state *state)
11659{
11660 struct drm_crtc *crtc = state->crtc;
11661 struct drm_framebuffer *fb = state->fb;
11662 struct drm_rect *dest = &state->dst;
11663 struct drm_rect *src = &state->src;
11664 const struct drm_rect *clip = &state->clip;
11665
11666 return drm_plane_helper_check_update(plane, crtc, fb,
11667 src, dest, clip,
11668 DRM_PLANE_HELPER_NO_SCALING,
11669 DRM_PLANE_HELPER_NO_SCALING,
11670 false, true, &state->visible);
11671}
11672
11673static int
11674intel_commit_primary_plane(struct drm_plane *plane,
11675 struct intel_plane_state *state)
465c120c 11676{
3c692a41
GP
11677 struct drm_crtc *crtc = state->crtc;
11678 struct drm_framebuffer *fb = state->fb;
465c120c 11679 struct drm_device *dev = crtc->dev;
48404c1e 11680 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2ff8fde1
MR
11682 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11683 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
ce54d85a 11684 struct intel_plane *intel_plane = to_intel_plane(plane);
3c692a41 11685 struct drm_rect *src = &state->src;
465c120c
MR
11686 int ret;
11687
465c120c
MR
11688 intel_crtc_wait_for_pending_flips(crtc);
11689
11690 /*
11691 * If clipping results in a non-visible primary plane, we'll disable
11692 * the primary plane. Note that this is a bit different than what
11693 * happens if userspace explicitly disables the plane by passing fb=0
11694 * because plane->fb still gets set and pinned.
11695 */
3c692a41 11696 if (!state->visible) {
4c34574f
MR
11697 mutex_lock(&dev->struct_mutex);
11698
465c120c
MR
11699 /*
11700 * Try to pin the new fb first so that we can bail out if we
11701 * fail.
11702 */
11703 if (plane->fb != fb) {
a071fa00 11704 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
4c34574f
MR
11705 if (ret) {
11706 mutex_unlock(&dev->struct_mutex);
465c120c 11707 return ret;
4c34574f 11708 }
465c120c
MR
11709 }
11710
a071fa00
DV
11711 i915_gem_track_fb(old_obj, obj,
11712 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11713
465c120c 11714 if (intel_crtc->primary_enabled)
fdd508a6 11715 intel_disable_primary_hw_plane(plane, crtc);
465c120c
MR
11716
11717
11718 if (plane->fb != fb)
11719 if (plane->fb)
a071fa00 11720 intel_unpin_fb_obj(old_obj);
465c120c 11721
4c34574f
MR
11722 mutex_unlock(&dev->struct_mutex);
11723
ce54d85a 11724 } else {
48404c1e
SJ
11725 if (intel_crtc && intel_crtc->active &&
11726 intel_crtc->primary_enabled) {
11727 /*
11728 * FBC does not work on some platforms for rotated
11729 * planes, so disable it when rotation is not 0 and
11730 * update it when rotation is set back to 0.
11731 *
11732 * FIXME: This is redundant with the fbc update done in
11733 * the primary plane enable function except that that
11734 * one is done too late. We eventually need to unify
11735 * this.
11736 */
11737 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11738 dev_priv->fbc.plane == intel_crtc->plane &&
11739 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11740 intel_disable_fbc(dev);
11741 }
11742 }
3c692a41 11743 ret = intel_pipe_set_base(crtc, src->x1, src->y1, fb);
ce54d85a
SJ
11744 if (ret)
11745 return ret;
465c120c 11746
ce54d85a
SJ
11747 if (!intel_crtc->primary_enabled)
11748 intel_enable_primary_hw_plane(plane, crtc);
11749 }
465c120c 11750
3c692a41
GP
11751 intel_plane->crtc_x = state->orig_dst.x1;
11752 intel_plane->crtc_y = state->orig_dst.y1;
11753 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11754 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11755 intel_plane->src_x = state->orig_src.x1;
11756 intel_plane->src_y = state->orig_src.y1;
11757 intel_plane->src_w = drm_rect_width(&state->orig_src);
11758 intel_plane->src_h = drm_rect_height(&state->orig_src);
ce54d85a 11759 intel_plane->obj = obj;
465c120c
MR
11760
11761 return 0;
11762}
11763
3c692a41
GP
11764static int
11765intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11766 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11767 unsigned int crtc_w, unsigned int crtc_h,
11768 uint32_t src_x, uint32_t src_y,
11769 uint32_t src_w, uint32_t src_h)
11770{
11771 struct intel_plane_state state;
11772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11773 int ret;
11774
11775 state.crtc = crtc;
11776 state.fb = fb;
11777
11778 /* sample coordinates in 16.16 fixed point */
11779 state.src.x1 = src_x;
11780 state.src.x2 = src_x + src_w;
11781 state.src.y1 = src_y;
11782 state.src.y2 = src_y + src_h;
11783
11784 /* integer pixels */
11785 state.dst.x1 = crtc_x;
11786 state.dst.x2 = crtc_x + crtc_w;
11787 state.dst.y1 = crtc_y;
11788 state.dst.y2 = crtc_y + crtc_h;
11789
11790 state.clip.x1 = 0;
11791 state.clip.y1 = 0;
11792 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11793 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11794
11795 state.orig_src = state.src;
11796 state.orig_dst = state.dst;
11797
11798 ret = intel_check_primary_plane(plane, &state);
11799 if (ret)
11800 return ret;
11801
11802 intel_commit_primary_plane(plane, &state);
11803
11804 return 0;
11805}
11806
3d7d6510
MR
11807/* Common destruction function for both primary and cursor planes */
11808static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11809{
11810 struct intel_plane *intel_plane = to_intel_plane(plane);
11811 drm_plane_cleanup(plane);
11812 kfree(intel_plane);
11813}
11814
11815static const struct drm_plane_funcs intel_primary_plane_funcs = {
11816 .update_plane = intel_primary_plane_setplane,
11817 .disable_plane = intel_primary_plane_disable,
3d7d6510 11818 .destroy = intel_plane_destroy,
48404c1e 11819 .set_property = intel_plane_set_property
465c120c
MR
11820};
11821
11822static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11823 int pipe)
11824{
11825 struct intel_plane *primary;
11826 const uint32_t *intel_primary_formats;
11827 int num_formats;
11828
11829 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11830 if (primary == NULL)
11831 return NULL;
11832
11833 primary->can_scale = false;
11834 primary->max_downscale = 1;
11835 primary->pipe = pipe;
11836 primary->plane = pipe;
48404c1e 11837 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
11838 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11839 primary->plane = !pipe;
11840
11841 if (INTEL_INFO(dev)->gen <= 3) {
11842 intel_primary_formats = intel_primary_formats_gen2;
11843 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11844 } else {
11845 intel_primary_formats = intel_primary_formats_gen4;
11846 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11847 }
11848
11849 drm_universal_plane_init(dev, &primary->base, 0,
11850 &intel_primary_plane_funcs,
11851 intel_primary_formats, num_formats,
11852 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
11853
11854 if (INTEL_INFO(dev)->gen >= 4) {
11855 if (!dev->mode_config.rotation_property)
11856 dev->mode_config.rotation_property =
11857 drm_mode_create_rotation_property(dev,
11858 BIT(DRM_ROTATE_0) |
11859 BIT(DRM_ROTATE_180));
11860 if (dev->mode_config.rotation_property)
11861 drm_object_attach_property(&primary->base.base,
11862 dev->mode_config.rotation_property,
11863 primary->rotation);
11864 }
11865
465c120c
MR
11866 return &primary->base;
11867}
11868
3d7d6510
MR
11869static int
11870intel_cursor_plane_disable(struct drm_plane *plane)
11871{
11872 if (!plane->fb)
11873 return 0;
11874
11875 BUG_ON(!plane->crtc);
11876
11877 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11878}
11879
11880static int
852e787c
GP
11881intel_check_cursor_plane(struct drm_plane *plane,
11882 struct intel_plane_state *state)
3d7d6510 11883{
852e787c
GP
11884 struct drm_crtc *crtc = state->crtc;
11885 struct drm_framebuffer *fb = state->fb;
11886 struct drm_rect *dest = &state->dst;
11887 struct drm_rect *src = &state->src;
11888 const struct drm_rect *clip = &state->clip;
3d7d6510 11889
852e787c
GP
11890 return drm_plane_helper_check_update(plane, crtc, fb,
11891 src, dest, clip,
3d7d6510
MR
11892 DRM_PLANE_HELPER_NO_SCALING,
11893 DRM_PLANE_HELPER_NO_SCALING,
852e787c
GP
11894 true, true, &state->visible);
11895}
3d7d6510 11896
852e787c
GP
11897static int
11898intel_commit_cursor_plane(struct drm_plane *plane,
11899 struct intel_plane_state *state)
11900{
11901 struct drm_crtc *crtc = state->crtc;
11902 struct drm_framebuffer *fb = state->fb;
11903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11904 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11905 struct drm_i915_gem_object *obj = intel_fb->obj;
11906 int crtc_w, crtc_h;
11907
11908 crtc->cursor_x = state->orig_dst.x1;
11909 crtc->cursor_y = state->orig_dst.y1;
3d7d6510 11910 if (fb != crtc->cursor->fb) {
852e787c
GP
11911 crtc_w = drm_rect_width(&state->orig_dst);
11912 crtc_h = drm_rect_height(&state->orig_dst);
3d7d6510
MR
11913 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11914 } else {
852e787c 11915 intel_crtc_update_cursor(crtc, state->visible);
4ed91096
DV
11916
11917 intel_frontbuffer_flip(crtc->dev,
11918 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11919
3d7d6510
MR
11920 return 0;
11921 }
11922}
852e787c
GP
11923
11924static int
11925intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11926 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11927 unsigned int crtc_w, unsigned int crtc_h,
11928 uint32_t src_x, uint32_t src_y,
11929 uint32_t src_w, uint32_t src_h)
11930{
11931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11932 struct intel_plane_state state;
11933 int ret;
11934
11935 state.crtc = crtc;
11936 state.fb = fb;
11937
11938 /* sample coordinates in 16.16 fixed point */
11939 state.src.x1 = src_x;
11940 state.src.x2 = src_x + src_w;
11941 state.src.y1 = src_y;
11942 state.src.y2 = src_y + src_h;
11943
11944 /* integer pixels */
11945 state.dst.x1 = crtc_x;
11946 state.dst.x2 = crtc_x + crtc_w;
11947 state.dst.y1 = crtc_y;
11948 state.dst.y2 = crtc_y + crtc_h;
11949
11950 state.clip.x1 = 0;
11951 state.clip.y1 = 0;
11952 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11953 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11954
11955 state.orig_src = state.src;
11956 state.orig_dst = state.dst;
11957
11958 ret = intel_check_cursor_plane(plane, &state);
11959 if (ret)
11960 return ret;
11961
11962 return intel_commit_cursor_plane(plane, &state);
11963}
11964
3d7d6510
MR
11965static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11966 .update_plane = intel_cursor_plane_update,
11967 .disable_plane = intel_cursor_plane_disable,
11968 .destroy = intel_plane_destroy,
11969};
11970
11971static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11972 int pipe)
11973{
11974 struct intel_plane *cursor;
11975
11976 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11977 if (cursor == NULL)
11978 return NULL;
11979
11980 cursor->can_scale = false;
11981 cursor->max_downscale = 1;
11982 cursor->pipe = pipe;
11983 cursor->plane = pipe;
11984
11985 drm_universal_plane_init(dev, &cursor->base, 0,
11986 &intel_cursor_plane_funcs,
11987 intel_cursor_formats,
11988 ARRAY_SIZE(intel_cursor_formats),
11989 DRM_PLANE_TYPE_CURSOR);
11990 return &cursor->base;
11991}
11992
b358d0a6 11993static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 11994{
fbee40df 11995 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 11996 struct intel_crtc *intel_crtc;
3d7d6510
MR
11997 struct drm_plane *primary = NULL;
11998 struct drm_plane *cursor = NULL;
465c120c 11999 int i, ret;
79e53945 12000
955382f3 12001 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12002 if (intel_crtc == NULL)
12003 return;
12004
465c120c 12005 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12006 if (!primary)
12007 goto fail;
12008
12009 cursor = intel_cursor_plane_create(dev, pipe);
12010 if (!cursor)
12011 goto fail;
12012
465c120c 12013 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12014 cursor, &intel_crtc_funcs);
12015 if (ret)
12016 goto fail;
79e53945
JB
12017
12018 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12019 for (i = 0; i < 256; i++) {
12020 intel_crtc->lut_r[i] = i;
12021 intel_crtc->lut_g[i] = i;
12022 intel_crtc->lut_b[i] = i;
12023 }
12024
1f1c2e24
VS
12025 /*
12026 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12027 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12028 */
80824003
JB
12029 intel_crtc->pipe = pipe;
12030 intel_crtc->plane = pipe;
3a77c4c4 12031 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12032 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12033 intel_crtc->plane = !pipe;
80824003
JB
12034 }
12035
4b0e333e
CW
12036 intel_crtc->cursor_base = ~0;
12037 intel_crtc->cursor_cntl = ~0;
dc41c154 12038 intel_crtc->cursor_size = ~0;
8d7849db 12039
22fd0fab
JB
12040 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12041 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12042 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12043 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12044
79e53945 12045 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12046
12047 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12048 return;
12049
12050fail:
12051 if (primary)
12052 drm_plane_cleanup(primary);
12053 if (cursor)
12054 drm_plane_cleanup(cursor);
12055 kfree(intel_crtc);
79e53945
JB
12056}
12057
752aa88a
JB
12058enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12059{
12060 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12061 struct drm_device *dev = connector->base.dev;
752aa88a 12062
51fd371b 12063 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12064
12065 if (!encoder)
12066 return INVALID_PIPE;
12067
12068 return to_intel_crtc(encoder->crtc)->pipe;
12069}
12070
08d7b3d1 12071int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12072 struct drm_file *file)
08d7b3d1 12073{
08d7b3d1 12074 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12075 struct drm_crtc *drmmode_crtc;
c05422d5 12076 struct intel_crtc *crtc;
08d7b3d1 12077
1cff8f6b
DV
12078 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12079 return -ENODEV;
08d7b3d1 12080
7707e653 12081 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12082
7707e653 12083 if (!drmmode_crtc) {
08d7b3d1 12084 DRM_ERROR("no such CRTC id\n");
3f2c2057 12085 return -ENOENT;
08d7b3d1
CW
12086 }
12087
7707e653 12088 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12089 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12090
c05422d5 12091 return 0;
08d7b3d1
CW
12092}
12093
66a9278e 12094static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12095{
66a9278e
DV
12096 struct drm_device *dev = encoder->base.dev;
12097 struct intel_encoder *source_encoder;
79e53945 12098 int index_mask = 0;
79e53945
JB
12099 int entry = 0;
12100
b2784e15 12101 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12102 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12103 index_mask |= (1 << entry);
12104
79e53945
JB
12105 entry++;
12106 }
4ef69c7a 12107
79e53945
JB
12108 return index_mask;
12109}
12110
4d302442
CW
12111static bool has_edp_a(struct drm_device *dev)
12112{
12113 struct drm_i915_private *dev_priv = dev->dev_private;
12114
12115 if (!IS_MOBILE(dev))
12116 return false;
12117
12118 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12119 return false;
12120
e3589908 12121 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12122 return false;
12123
12124 return true;
12125}
12126
ba0fbca4
DL
12127const char *intel_output_name(int output)
12128{
12129 static const char *names[] = {
12130 [INTEL_OUTPUT_UNUSED] = "Unused",
12131 [INTEL_OUTPUT_ANALOG] = "Analog",
12132 [INTEL_OUTPUT_DVO] = "DVO",
12133 [INTEL_OUTPUT_SDVO] = "SDVO",
12134 [INTEL_OUTPUT_LVDS] = "LVDS",
12135 [INTEL_OUTPUT_TVOUT] = "TV",
12136 [INTEL_OUTPUT_HDMI] = "HDMI",
12137 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12138 [INTEL_OUTPUT_EDP] = "eDP",
12139 [INTEL_OUTPUT_DSI] = "DSI",
12140 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12141 };
12142
12143 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12144 return "Invalid";
12145
12146 return names[output];
12147}
12148
84b4e042
JB
12149static bool intel_crt_present(struct drm_device *dev)
12150{
12151 struct drm_i915_private *dev_priv = dev->dev_private;
12152
884497ed
DL
12153 if (INTEL_INFO(dev)->gen >= 9)
12154 return false;
12155
84b4e042
JB
12156 if (IS_ULT(dev))
12157 return false;
12158
12159 if (IS_CHERRYVIEW(dev))
12160 return false;
12161
12162 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12163 return false;
12164
12165 return true;
12166}
12167
79e53945
JB
12168static void intel_setup_outputs(struct drm_device *dev)
12169{
725e30ad 12170 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12171 struct intel_encoder *encoder;
cb0953d7 12172 bool dpd_is_edp = false;
79e53945 12173
c9093354 12174 intel_lvds_init(dev);
79e53945 12175
84b4e042 12176 if (intel_crt_present(dev))
79935fca 12177 intel_crt_init(dev);
cb0953d7 12178
affa9354 12179 if (HAS_DDI(dev)) {
0e72a5b5
ED
12180 int found;
12181
12182 /* Haswell uses DDI functions to detect digital outputs */
12183 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12184 /* DDI A only supports eDP */
12185 if (found)
12186 intel_ddi_init(dev, PORT_A);
12187
12188 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12189 * register */
12190 found = I915_READ(SFUSE_STRAP);
12191
12192 if (found & SFUSE_STRAP_DDIB_DETECTED)
12193 intel_ddi_init(dev, PORT_B);
12194 if (found & SFUSE_STRAP_DDIC_DETECTED)
12195 intel_ddi_init(dev, PORT_C);
12196 if (found & SFUSE_STRAP_DDID_DETECTED)
12197 intel_ddi_init(dev, PORT_D);
12198 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12199 int found;
5d8a7752 12200 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12201
12202 if (has_edp_a(dev))
12203 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12204
dc0fa718 12205 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12206 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12207 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12208 if (!found)
e2debe91 12209 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12210 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12211 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12212 }
12213
dc0fa718 12214 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12215 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12216
dc0fa718 12217 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12218 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12219
5eb08b69 12220 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12221 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12222
270b3042 12223 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12224 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12225 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
12226 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12227 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12228 PORT_B);
12229 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12230 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12231 }
12232
6f6005a5
JB
12233 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12234 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12235 PORT_C);
12236 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 12237 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 12238 }
19c03924 12239
9418c1f1
VS
12240 if (IS_CHERRYVIEW(dev)) {
12241 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12242 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12243 PORT_D);
12244 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12245 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12246 }
12247 }
12248
3cfca973 12249 intel_dsi_init(dev);
103a196f 12250 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12251 bool found = false;
7d57382e 12252
e2debe91 12253 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12254 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12255 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12256 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12257 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12258 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12259 }
27185ae1 12260
e7281eab 12261 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12262 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12263 }
13520b05
KH
12264
12265 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12266
e2debe91 12267 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12268 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12269 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12270 }
27185ae1 12271
e2debe91 12272 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12273
b01f2c3a
JB
12274 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12275 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12276 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12277 }
e7281eab 12278 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12279 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12280 }
27185ae1 12281
b01f2c3a 12282 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12283 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12284 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12285 } else if (IS_GEN2(dev))
79e53945
JB
12286 intel_dvo_init(dev);
12287
103a196f 12288 if (SUPPORTS_TV(dev))
79e53945
JB
12289 intel_tv_init(dev);
12290
7c8f8a70
RV
12291 intel_edp_psr_init(dev);
12292
b2784e15 12293 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12294 encoder->base.possible_crtcs = encoder->crtc_mask;
12295 encoder->base.possible_clones =
66a9278e 12296 intel_encoder_clones(encoder);
79e53945 12297 }
47356eb6 12298
dde86e2d 12299 intel_init_pch_refclk(dev);
270b3042
DV
12300
12301 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12302}
12303
12304static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12305{
60a5ca01 12306 struct drm_device *dev = fb->dev;
79e53945 12307 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12308
ef2d633e 12309 drm_framebuffer_cleanup(fb);
60a5ca01 12310 mutex_lock(&dev->struct_mutex);
ef2d633e 12311 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12312 drm_gem_object_unreference(&intel_fb->obj->base);
12313 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12314 kfree(intel_fb);
12315}
12316
12317static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12318 struct drm_file *file,
79e53945
JB
12319 unsigned int *handle)
12320{
12321 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12322 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12323
05394f39 12324 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12325}
12326
12327static const struct drm_framebuffer_funcs intel_fb_funcs = {
12328 .destroy = intel_user_framebuffer_destroy,
12329 .create_handle = intel_user_framebuffer_create_handle,
12330};
12331
b5ea642a
DV
12332static int intel_framebuffer_init(struct drm_device *dev,
12333 struct intel_framebuffer *intel_fb,
12334 struct drm_mode_fb_cmd2 *mode_cmd,
12335 struct drm_i915_gem_object *obj)
79e53945 12336{
a57ce0b2 12337 int aligned_height;
a35cdaa0 12338 int pitch_limit;
79e53945
JB
12339 int ret;
12340
dd4916c5
DV
12341 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12342
c16ed4be
CW
12343 if (obj->tiling_mode == I915_TILING_Y) {
12344 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12345 return -EINVAL;
c16ed4be 12346 }
57cd6508 12347
c16ed4be
CW
12348 if (mode_cmd->pitches[0] & 63) {
12349 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12350 mode_cmd->pitches[0]);
57cd6508 12351 return -EINVAL;
c16ed4be 12352 }
57cd6508 12353
a35cdaa0
CW
12354 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12355 pitch_limit = 32*1024;
12356 } else if (INTEL_INFO(dev)->gen >= 4) {
12357 if (obj->tiling_mode)
12358 pitch_limit = 16*1024;
12359 else
12360 pitch_limit = 32*1024;
12361 } else if (INTEL_INFO(dev)->gen >= 3) {
12362 if (obj->tiling_mode)
12363 pitch_limit = 8*1024;
12364 else
12365 pitch_limit = 16*1024;
12366 } else
12367 /* XXX DSPC is limited to 4k tiled */
12368 pitch_limit = 8*1024;
12369
12370 if (mode_cmd->pitches[0] > pitch_limit) {
12371 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12372 obj->tiling_mode ? "tiled" : "linear",
12373 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12374 return -EINVAL;
c16ed4be 12375 }
5d7bd705
VS
12376
12377 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12378 mode_cmd->pitches[0] != obj->stride) {
12379 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12380 mode_cmd->pitches[0], obj->stride);
5d7bd705 12381 return -EINVAL;
c16ed4be 12382 }
5d7bd705 12383
57779d06 12384 /* Reject formats not supported by any plane early. */
308e5bcb 12385 switch (mode_cmd->pixel_format) {
57779d06 12386 case DRM_FORMAT_C8:
04b3924d
VS
12387 case DRM_FORMAT_RGB565:
12388 case DRM_FORMAT_XRGB8888:
12389 case DRM_FORMAT_ARGB8888:
57779d06
VS
12390 break;
12391 case DRM_FORMAT_XRGB1555:
12392 case DRM_FORMAT_ARGB1555:
c16ed4be 12393 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12394 DRM_DEBUG("unsupported pixel format: %s\n",
12395 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12396 return -EINVAL;
c16ed4be 12397 }
57779d06
VS
12398 break;
12399 case DRM_FORMAT_XBGR8888:
12400 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12401 case DRM_FORMAT_XRGB2101010:
12402 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12403 case DRM_FORMAT_XBGR2101010:
12404 case DRM_FORMAT_ABGR2101010:
c16ed4be 12405 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12406 DRM_DEBUG("unsupported pixel format: %s\n",
12407 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12408 return -EINVAL;
c16ed4be 12409 }
b5626747 12410 break;
04b3924d
VS
12411 case DRM_FORMAT_YUYV:
12412 case DRM_FORMAT_UYVY:
12413 case DRM_FORMAT_YVYU:
12414 case DRM_FORMAT_VYUY:
c16ed4be 12415 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12416 DRM_DEBUG("unsupported pixel format: %s\n",
12417 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12418 return -EINVAL;
c16ed4be 12419 }
57cd6508
CW
12420 break;
12421 default:
4ee62c76
VS
12422 DRM_DEBUG("unsupported pixel format: %s\n",
12423 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12424 return -EINVAL;
12425 }
12426
90f9a336
VS
12427 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12428 if (mode_cmd->offsets[0] != 0)
12429 return -EINVAL;
12430
a57ce0b2
JB
12431 aligned_height = intel_align_height(dev, mode_cmd->height,
12432 obj->tiling_mode);
53155c0a
DV
12433 /* FIXME drm helper for size checks (especially planar formats)? */
12434 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12435 return -EINVAL;
12436
c7d73f6a
DV
12437 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12438 intel_fb->obj = obj;
80075d49 12439 intel_fb->obj->framebuffer_references++;
c7d73f6a 12440
79e53945
JB
12441 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12442 if (ret) {
12443 DRM_ERROR("framebuffer init failed %d\n", ret);
12444 return ret;
12445 }
12446
79e53945
JB
12447 return 0;
12448}
12449
79e53945
JB
12450static struct drm_framebuffer *
12451intel_user_framebuffer_create(struct drm_device *dev,
12452 struct drm_file *filp,
308e5bcb 12453 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12454{
05394f39 12455 struct drm_i915_gem_object *obj;
79e53945 12456
308e5bcb
JB
12457 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12458 mode_cmd->handles[0]));
c8725226 12459 if (&obj->base == NULL)
cce13ff7 12460 return ERR_PTR(-ENOENT);
79e53945 12461
d2dff872 12462 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12463}
12464
4520f53a 12465#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12466static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12467{
12468}
12469#endif
12470
79e53945 12471static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12472 .fb_create = intel_user_framebuffer_create,
0632fef6 12473 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12474};
12475
e70236a8
JB
12476/* Set up chip specific display functions */
12477static void intel_init_display(struct drm_device *dev)
12478{
12479 struct drm_i915_private *dev_priv = dev->dev_private;
12480
ee9300bb
DV
12481 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12482 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12483 else if (IS_CHERRYVIEW(dev))
12484 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12485 else if (IS_VALLEYVIEW(dev))
12486 dev_priv->display.find_dpll = vlv_find_best_dpll;
12487 else if (IS_PINEVIEW(dev))
12488 dev_priv->display.find_dpll = pnv_find_best_dpll;
12489 else
12490 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12491
affa9354 12492 if (HAS_DDI(dev)) {
0e8ffe1b 12493 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12494 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12495 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12496 dev_priv->display.crtc_enable = haswell_crtc_enable;
12497 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12498 dev_priv->display.off = ironlake_crtc_off;
70d21f0e
DL
12499 if (INTEL_INFO(dev)->gen >= 9)
12500 dev_priv->display.update_primary_plane =
12501 skylake_update_primary_plane;
12502 else
12503 dev_priv->display.update_primary_plane =
12504 ironlake_update_primary_plane;
09b4ddf9 12505 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12506 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12507 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12508 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12509 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12510 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12511 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12512 dev_priv->display.update_primary_plane =
12513 ironlake_update_primary_plane;
89b667f8
JB
12514 } else if (IS_VALLEYVIEW(dev)) {
12515 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12516 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12517 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12518 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12519 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12520 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12521 dev_priv->display.update_primary_plane =
12522 i9xx_update_primary_plane;
f564048e 12523 } else {
0e8ffe1b 12524 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12525 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12526 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12527 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12528 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12529 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12530 dev_priv->display.update_primary_plane =
12531 i9xx_update_primary_plane;
f564048e 12532 }
e70236a8 12533
e70236a8 12534 /* Returns the core display clock speed */
25eb05fc
JB
12535 if (IS_VALLEYVIEW(dev))
12536 dev_priv->display.get_display_clock_speed =
12537 valleyview_get_display_clock_speed;
12538 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12539 dev_priv->display.get_display_clock_speed =
12540 i945_get_display_clock_speed;
12541 else if (IS_I915G(dev))
12542 dev_priv->display.get_display_clock_speed =
12543 i915_get_display_clock_speed;
257a7ffc 12544 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12545 dev_priv->display.get_display_clock_speed =
12546 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12547 else if (IS_PINEVIEW(dev))
12548 dev_priv->display.get_display_clock_speed =
12549 pnv_get_display_clock_speed;
e70236a8
JB
12550 else if (IS_I915GM(dev))
12551 dev_priv->display.get_display_clock_speed =
12552 i915gm_get_display_clock_speed;
12553 else if (IS_I865G(dev))
12554 dev_priv->display.get_display_clock_speed =
12555 i865_get_display_clock_speed;
f0f8a9ce 12556 else if (IS_I85X(dev))
e70236a8
JB
12557 dev_priv->display.get_display_clock_speed =
12558 i855_get_display_clock_speed;
12559 else /* 852, 830 */
12560 dev_priv->display.get_display_clock_speed =
12561 i830_get_display_clock_speed;
12562
3bb11b53 12563 if (IS_G4X(dev)) {
e0dac65e 12564 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12565 } else if (IS_GEN5(dev)) {
12566 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12567 dev_priv->display.write_eld = ironlake_write_eld;
12568 } else if (IS_GEN6(dev)) {
12569 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12570 dev_priv->display.write_eld = ironlake_write_eld;
12571 dev_priv->display.modeset_global_resources =
12572 snb_modeset_global_resources;
12573 } else if (IS_IVYBRIDGE(dev)) {
12574 /* FIXME: detect B0+ stepping and use auto training */
12575 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12576 dev_priv->display.write_eld = ironlake_write_eld;
12577 dev_priv->display.modeset_global_resources =
12578 ivb_modeset_global_resources;
059b2fe9 12579 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53
SJ
12580 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12581 dev_priv->display.write_eld = haswell_write_eld;
12582 dev_priv->display.modeset_global_resources =
12583 haswell_modeset_global_resources;
30a970c6
JB
12584 } else if (IS_VALLEYVIEW(dev)) {
12585 dev_priv->display.modeset_global_resources =
12586 valleyview_modeset_global_resources;
9ca2fe73 12587 dev_priv->display.write_eld = ironlake_write_eld;
02c29259
S
12588 } else if (INTEL_INFO(dev)->gen >= 9) {
12589 dev_priv->display.write_eld = haswell_write_eld;
12590 dev_priv->display.modeset_global_resources =
12591 haswell_modeset_global_resources;
e70236a8 12592 }
8c9f3aaf
JB
12593
12594 /* Default just returns -ENODEV to indicate unsupported */
12595 dev_priv->display.queue_flip = intel_default_queue_flip;
12596
12597 switch (INTEL_INFO(dev)->gen) {
12598 case 2:
12599 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12600 break;
12601
12602 case 3:
12603 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12604 break;
12605
12606 case 4:
12607 case 5:
12608 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12609 break;
12610
12611 case 6:
12612 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12613 break;
7c9017e5 12614 case 7:
4e0bbc31 12615 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12616 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12617 break;
8c9f3aaf 12618 }
7bd688cd
JN
12619
12620 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12621
12622 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12623}
12624
b690e96c
JB
12625/*
12626 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12627 * resume, or other times. This quirk makes sure that's the case for
12628 * affected systems.
12629 */
0206e353 12630static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12631{
12632 struct drm_i915_private *dev_priv = dev->dev_private;
12633
12634 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12635 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12636}
12637
b6b5d049
VS
12638static void quirk_pipeb_force(struct drm_device *dev)
12639{
12640 struct drm_i915_private *dev_priv = dev->dev_private;
12641
12642 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12643 DRM_INFO("applying pipe b force quirk\n");
12644}
12645
435793df
KP
12646/*
12647 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12648 */
12649static void quirk_ssc_force_disable(struct drm_device *dev)
12650{
12651 struct drm_i915_private *dev_priv = dev->dev_private;
12652 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12653 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12654}
12655
4dca20ef 12656/*
5a15ab5b
CE
12657 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12658 * brightness value
4dca20ef
CE
12659 */
12660static void quirk_invert_brightness(struct drm_device *dev)
12661{
12662 struct drm_i915_private *dev_priv = dev->dev_private;
12663 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12664 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12665}
12666
9c72cc6f
SD
12667/* Some VBT's incorrectly indicate no backlight is present */
12668static void quirk_backlight_present(struct drm_device *dev)
12669{
12670 struct drm_i915_private *dev_priv = dev->dev_private;
12671 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12672 DRM_INFO("applying backlight present quirk\n");
12673}
12674
b690e96c
JB
12675struct intel_quirk {
12676 int device;
12677 int subsystem_vendor;
12678 int subsystem_device;
12679 void (*hook)(struct drm_device *dev);
12680};
12681
5f85f176
EE
12682/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12683struct intel_dmi_quirk {
12684 void (*hook)(struct drm_device *dev);
12685 const struct dmi_system_id (*dmi_id_list)[];
12686};
12687
12688static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12689{
12690 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12691 return 1;
12692}
12693
12694static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12695 {
12696 .dmi_id_list = &(const struct dmi_system_id[]) {
12697 {
12698 .callback = intel_dmi_reverse_brightness,
12699 .ident = "NCR Corporation",
12700 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12701 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12702 },
12703 },
12704 { } /* terminating entry */
12705 },
12706 .hook = quirk_invert_brightness,
12707 },
12708};
12709
c43b5634 12710static struct intel_quirk intel_quirks[] = {
b690e96c 12711 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12712 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12713
b690e96c
JB
12714 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12715 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12716
b690e96c
JB
12717 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12718 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12719
5f080c0f
VS
12720 /* 830 needs to leave pipe A & dpll A up */
12721 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12722
b6b5d049
VS
12723 /* 830 needs to leave pipe B & dpll B up */
12724 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12725
435793df
KP
12726 /* Lenovo U160 cannot use SSC on LVDS */
12727 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12728
12729 /* Sony Vaio Y cannot use SSC on LVDS */
12730 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12731
be505f64
AH
12732 /* Acer Aspire 5734Z must invert backlight brightness */
12733 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12734
12735 /* Acer/eMachines G725 */
12736 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12737
12738 /* Acer/eMachines e725 */
12739 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12740
12741 /* Acer/Packard Bell NCL20 */
12742 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12743
12744 /* Acer Aspire 4736Z */
12745 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12746
12747 /* Acer Aspire 5336 */
12748 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12749
12750 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12751 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 12752
dfb3d47b
SD
12753 /* Acer C720 Chromebook (Core i3 4005U) */
12754 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12755
d4967d8c
SD
12756 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12757 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12758
12759 /* HP Chromebook 14 (Celeron 2955U) */
12760 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12761};
12762
12763static void intel_init_quirks(struct drm_device *dev)
12764{
12765 struct pci_dev *d = dev->pdev;
12766 int i;
12767
12768 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12769 struct intel_quirk *q = &intel_quirks[i];
12770
12771 if (d->device == q->device &&
12772 (d->subsystem_vendor == q->subsystem_vendor ||
12773 q->subsystem_vendor == PCI_ANY_ID) &&
12774 (d->subsystem_device == q->subsystem_device ||
12775 q->subsystem_device == PCI_ANY_ID))
12776 q->hook(dev);
12777 }
5f85f176
EE
12778 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12779 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12780 intel_dmi_quirks[i].hook(dev);
12781 }
b690e96c
JB
12782}
12783
9cce37f4
JB
12784/* Disable the VGA plane that we never use */
12785static void i915_disable_vga(struct drm_device *dev)
12786{
12787 struct drm_i915_private *dev_priv = dev->dev_private;
12788 u8 sr1;
766aa1c4 12789 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12790
2b37c616 12791 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12792 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12793 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12794 sr1 = inb(VGA_SR_DATA);
12795 outb(sr1 | 1<<5, VGA_SR_DATA);
12796 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12797 udelay(300);
12798
69769f9a
VS
12799 /*
12800 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12801 * from S3 without preserving (some of?) the other bits.
12802 */
12803 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
9cce37f4
JB
12804 POSTING_READ(vga_reg);
12805}
12806
f817586c
DV
12807void intel_modeset_init_hw(struct drm_device *dev)
12808{
a8f78b58
ED
12809 intel_prepare_ddi(dev);
12810
f8bf63fd
VS
12811 if (IS_VALLEYVIEW(dev))
12812 vlv_update_cdclk(dev);
12813
f817586c
DV
12814 intel_init_clock_gating(dev);
12815
8090c6b9 12816 intel_enable_gt_powersave(dev);
f817586c
DV
12817}
12818
79e53945
JB
12819void intel_modeset_init(struct drm_device *dev)
12820{
652c393a 12821 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12822 int sprite, ret;
8cc87b75 12823 enum pipe pipe;
46f297fb 12824 struct intel_crtc *crtc;
79e53945
JB
12825
12826 drm_mode_config_init(dev);
12827
12828 dev->mode_config.min_width = 0;
12829 dev->mode_config.min_height = 0;
12830
019d96cb
DA
12831 dev->mode_config.preferred_depth = 24;
12832 dev->mode_config.prefer_shadow = 1;
12833
e6ecefaa 12834 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12835
b690e96c
JB
12836 intel_init_quirks(dev);
12837
1fa61106
ED
12838 intel_init_pm(dev);
12839
e3c74757
BW
12840 if (INTEL_INFO(dev)->num_pipes == 0)
12841 return;
12842
e70236a8
JB
12843 intel_init_display(dev);
12844
a6c45cf0
CW
12845 if (IS_GEN2(dev)) {
12846 dev->mode_config.max_width = 2048;
12847 dev->mode_config.max_height = 2048;
12848 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12849 dev->mode_config.max_width = 4096;
12850 dev->mode_config.max_height = 4096;
79e53945 12851 } else {
a6c45cf0
CW
12852 dev->mode_config.max_width = 8192;
12853 dev->mode_config.max_height = 8192;
79e53945 12854 }
068be561 12855
dc41c154
VS
12856 if (IS_845G(dev) || IS_I865G(dev)) {
12857 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12858 dev->mode_config.cursor_height = 1023;
12859 } else if (IS_GEN2(dev)) {
068be561
DL
12860 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12861 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12862 } else {
12863 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12864 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12865 }
12866
5d4545ae 12867 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12868
28c97730 12869 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12870 INTEL_INFO(dev)->num_pipes,
12871 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12872
055e393f 12873 for_each_pipe(dev_priv, pipe) {
8cc87b75 12874 intel_crtc_init(dev, pipe);
1fe47785
DL
12875 for_each_sprite(pipe, sprite) {
12876 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12877 if (ret)
06da8da2 12878 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12879 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12880 }
79e53945
JB
12881 }
12882
f42bb70d
JB
12883 intel_init_dpio(dev);
12884
e72f9fbf 12885 intel_shared_dpll_init(dev);
ee7b9f93 12886
69769f9a
VS
12887 /* save the BIOS value before clobbering it */
12888 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
9cce37f4
JB
12889 /* Just disable it once at startup */
12890 i915_disable_vga(dev);
79e53945 12891 intel_setup_outputs(dev);
11be49eb
CW
12892
12893 /* Just in case the BIOS is doing something questionable. */
12894 intel_disable_fbc(dev);
fa9fa083 12895
6e9f798d 12896 drm_modeset_lock_all(dev);
fa9fa083 12897 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12898 drm_modeset_unlock_all(dev);
46f297fb 12899
d3fcc808 12900 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12901 if (!crtc->active)
12902 continue;
12903
46f297fb 12904 /*
46f297fb
JB
12905 * Note that reserving the BIOS fb up front prevents us
12906 * from stuffing other stolen allocations like the ring
12907 * on top. This prevents some ugliness at boot time, and
12908 * can even allow for smooth boot transitions if the BIOS
12909 * fb is large enough for the active pipe configuration.
12910 */
12911 if (dev_priv->display.get_plane_config) {
12912 dev_priv->display.get_plane_config(crtc,
12913 &crtc->plane_config);
12914 /*
12915 * If the fb is shared between multiple heads, we'll
12916 * just get the first one.
12917 */
484b41dd 12918 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12919 }
46f297fb 12920 }
2c7111db
CW
12921}
12922
7fad798e
DV
12923static void intel_enable_pipe_a(struct drm_device *dev)
12924{
12925 struct intel_connector *connector;
12926 struct drm_connector *crt = NULL;
12927 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 12928 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
12929
12930 /* We can't just switch on the pipe A, we need to set things up with a
12931 * proper mode and output configuration. As a gross hack, enable pipe A
12932 * by enabling the load detect pipe once. */
12933 list_for_each_entry(connector,
12934 &dev->mode_config.connector_list,
12935 base.head) {
12936 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12937 crt = &connector->base;
12938 break;
12939 }
12940 }
12941
12942 if (!crt)
12943 return;
12944
208bf9fd
VS
12945 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12946 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
12947}
12948
fa555837
DV
12949static bool
12950intel_check_plane_mapping(struct intel_crtc *crtc)
12951{
7eb552ae
BW
12952 struct drm_device *dev = crtc->base.dev;
12953 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12954 u32 reg, val;
12955
7eb552ae 12956 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
12957 return true;
12958
12959 reg = DSPCNTR(!crtc->plane);
12960 val = I915_READ(reg);
12961
12962 if ((val & DISPLAY_PLANE_ENABLE) &&
12963 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12964 return false;
12965
12966 return true;
12967}
12968
24929352
DV
12969static void intel_sanitize_crtc(struct intel_crtc *crtc)
12970{
12971 struct drm_device *dev = crtc->base.dev;
12972 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 12973 u32 reg;
24929352 12974
24929352 12975 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 12976 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
12977 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12978
d3eaf884 12979 /* restore vblank interrupts to correct state */
d297e103
VS
12980 if (crtc->active) {
12981 update_scanline_offset(crtc);
d3eaf884 12982 drm_vblank_on(dev, crtc->pipe);
d297e103 12983 } else
d3eaf884
VS
12984 drm_vblank_off(dev, crtc->pipe);
12985
24929352 12986 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
12987 * disable the crtc (and hence change the state) if it is wrong. Note
12988 * that gen4+ has a fixed plane -> pipe mapping. */
12989 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
12990 struct intel_connector *connector;
12991 bool plane;
12992
24929352
DV
12993 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12994 crtc->base.base.id);
12995
12996 /* Pipe has the wrong plane attached and the plane is active.
12997 * Temporarily change the plane mapping and disable everything
12998 * ... */
12999 plane = crtc->plane;
13000 crtc->plane = !plane;
9c8958bc 13001 crtc->primary_enabled = true;
24929352
DV
13002 dev_priv->display.crtc_disable(&crtc->base);
13003 crtc->plane = plane;
13004
13005 /* ... and break all links. */
13006 list_for_each_entry(connector, &dev->mode_config.connector_list,
13007 base.head) {
13008 if (connector->encoder->base.crtc != &crtc->base)
13009 continue;
13010
7f1950fb
EE
13011 connector->base.dpms = DRM_MODE_DPMS_OFF;
13012 connector->base.encoder = NULL;
24929352 13013 }
7f1950fb
EE
13014 /* multiple connectors may have the same encoder:
13015 * handle them and break crtc link separately */
13016 list_for_each_entry(connector, &dev->mode_config.connector_list,
13017 base.head)
13018 if (connector->encoder->base.crtc == &crtc->base) {
13019 connector->encoder->base.crtc = NULL;
13020 connector->encoder->connectors_active = false;
13021 }
24929352
DV
13022
13023 WARN_ON(crtc->active);
13024 crtc->base.enabled = false;
13025 }
24929352 13026
7fad798e
DV
13027 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13028 crtc->pipe == PIPE_A && !crtc->active) {
13029 /* BIOS forgot to enable pipe A, this mostly happens after
13030 * resume. Force-enable the pipe to fix this, the update_dpms
13031 * call below we restore the pipe to the right state, but leave
13032 * the required bits on. */
13033 intel_enable_pipe_a(dev);
13034 }
13035
24929352
DV
13036 /* Adjust the state of the output pipe according to whether we
13037 * have active connectors/encoders. */
13038 intel_crtc_update_dpms(&crtc->base);
13039
13040 if (crtc->active != crtc->base.enabled) {
13041 struct intel_encoder *encoder;
13042
13043 /* This can happen either due to bugs in the get_hw_state
13044 * functions or because the pipe is force-enabled due to the
13045 * pipe A quirk. */
13046 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13047 crtc->base.base.id,
13048 crtc->base.enabled ? "enabled" : "disabled",
13049 crtc->active ? "enabled" : "disabled");
13050
13051 crtc->base.enabled = crtc->active;
13052
13053 /* Because we only establish the connector -> encoder ->
13054 * crtc links if something is active, this means the
13055 * crtc is now deactivated. Break the links. connector
13056 * -> encoder links are only establish when things are
13057 * actually up, hence no need to break them. */
13058 WARN_ON(crtc->active);
13059
13060 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13061 WARN_ON(encoder->connectors_active);
13062 encoder->base.crtc = NULL;
13063 }
13064 }
c5ab3bc0 13065
a3ed6aad 13066 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13067 /*
13068 * We start out with underrun reporting disabled to avoid races.
13069 * For correct bookkeeping mark this on active crtcs.
13070 *
c5ab3bc0
DV
13071 * Also on gmch platforms we dont have any hardware bits to
13072 * disable the underrun reporting. Which means we need to start
13073 * out with underrun reporting disabled also on inactive pipes,
13074 * since otherwise we'll complain about the garbage we read when
13075 * e.g. coming up after runtime pm.
13076 *
4cc31489
DV
13077 * No protection against concurrent access is required - at
13078 * worst a fifo underrun happens which also sets this to false.
13079 */
13080 crtc->cpu_fifo_underrun_disabled = true;
13081 crtc->pch_fifo_underrun_disabled = true;
13082 }
24929352
DV
13083}
13084
13085static void intel_sanitize_encoder(struct intel_encoder *encoder)
13086{
13087 struct intel_connector *connector;
13088 struct drm_device *dev = encoder->base.dev;
13089
13090 /* We need to check both for a crtc link (meaning that the
13091 * encoder is active and trying to read from a pipe) and the
13092 * pipe itself being active. */
13093 bool has_active_crtc = encoder->base.crtc &&
13094 to_intel_crtc(encoder->base.crtc)->active;
13095
13096 if (encoder->connectors_active && !has_active_crtc) {
13097 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13098 encoder->base.base.id,
8e329a03 13099 encoder->base.name);
24929352
DV
13100
13101 /* Connector is active, but has no active pipe. This is
13102 * fallout from our resume register restoring. Disable
13103 * the encoder manually again. */
13104 if (encoder->base.crtc) {
13105 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13106 encoder->base.base.id,
8e329a03 13107 encoder->base.name);
24929352 13108 encoder->disable(encoder);
a62d1497
VS
13109 if (encoder->post_disable)
13110 encoder->post_disable(encoder);
24929352 13111 }
7f1950fb
EE
13112 encoder->base.crtc = NULL;
13113 encoder->connectors_active = false;
24929352
DV
13114
13115 /* Inconsistent output/port/pipe state happens presumably due to
13116 * a bug in one of the get_hw_state functions. Or someplace else
13117 * in our code, like the register restore mess on resume. Clamp
13118 * things to off as a safer default. */
13119 list_for_each_entry(connector,
13120 &dev->mode_config.connector_list,
13121 base.head) {
13122 if (connector->encoder != encoder)
13123 continue;
7f1950fb
EE
13124 connector->base.dpms = DRM_MODE_DPMS_OFF;
13125 connector->base.encoder = NULL;
24929352
DV
13126 }
13127 }
13128 /* Enabled encoders without active connectors will be fixed in
13129 * the crtc fixup. */
13130}
13131
04098753 13132void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13133{
13134 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13135 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13136
04098753
ID
13137 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13138 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13139 i915_disable_vga(dev);
13140 }
13141}
13142
13143void i915_redisable_vga(struct drm_device *dev)
13144{
13145 struct drm_i915_private *dev_priv = dev->dev_private;
13146
8dc8a27c
PZ
13147 /* This function can be called both from intel_modeset_setup_hw_state or
13148 * at a very early point in our resume sequence, where the power well
13149 * structures are not yet restored. Since this function is at a very
13150 * paranoid "someone might have enabled VGA while we were not looking"
13151 * level, just check if the power well is enabled instead of trying to
13152 * follow the "don't touch the power well if we don't need it" policy
13153 * the rest of the driver uses. */
f458ebbc 13154 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13155 return;
13156
04098753 13157 i915_redisable_vga_power_on(dev);
0fde901f
KM
13158}
13159
98ec7739
VS
13160static bool primary_get_hw_state(struct intel_crtc *crtc)
13161{
13162 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13163
13164 if (!crtc->active)
13165 return false;
13166
13167 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13168}
13169
30e984df 13170static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13171{
13172 struct drm_i915_private *dev_priv = dev->dev_private;
13173 enum pipe pipe;
24929352
DV
13174 struct intel_crtc *crtc;
13175 struct intel_encoder *encoder;
13176 struct intel_connector *connector;
5358901f 13177 int i;
24929352 13178
d3fcc808 13179 for_each_intel_crtc(dev, crtc) {
88adfff1 13180 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13181
9953599b
DV
13182 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13183
0e8ffe1b
DV
13184 crtc->active = dev_priv->display.get_pipe_config(crtc,
13185 &crtc->config);
24929352
DV
13186
13187 crtc->base.enabled = crtc->active;
98ec7739 13188 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13189
13190 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13191 crtc->base.base.id,
13192 crtc->active ? "enabled" : "disabled");
13193 }
13194
5358901f
DV
13195 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13196 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13197
13198 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13199 pll->active = 0;
d3fcc808 13200 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13201 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13202 pll->active++;
13203 }
13204 pll->refcount = pll->active;
13205
35c95375
DV
13206 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13207 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13208
13209 if (pll->refcount)
13210 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13211 }
13212
b2784e15 13213 for_each_intel_encoder(dev, encoder) {
24929352
DV
13214 pipe = 0;
13215
13216 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13217 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13218 encoder->base.crtc = &crtc->base;
1d37b689 13219 encoder->get_config(encoder, &crtc->config);
24929352
DV
13220 } else {
13221 encoder->base.crtc = NULL;
13222 }
13223
13224 encoder->connectors_active = false;
6f2bcceb 13225 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13226 encoder->base.base.id,
8e329a03 13227 encoder->base.name,
24929352 13228 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13229 pipe_name(pipe));
24929352
DV
13230 }
13231
13232 list_for_each_entry(connector, &dev->mode_config.connector_list,
13233 base.head) {
13234 if (connector->get_hw_state(connector)) {
13235 connector->base.dpms = DRM_MODE_DPMS_ON;
13236 connector->encoder->connectors_active = true;
13237 connector->base.encoder = &connector->encoder->base;
13238 } else {
13239 connector->base.dpms = DRM_MODE_DPMS_OFF;
13240 connector->base.encoder = NULL;
13241 }
13242 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13243 connector->base.base.id,
c23cc417 13244 connector->base.name,
24929352
DV
13245 connector->base.encoder ? "enabled" : "disabled");
13246 }
30e984df
DV
13247}
13248
13249/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13250 * and i915 state tracking structures. */
13251void intel_modeset_setup_hw_state(struct drm_device *dev,
13252 bool force_restore)
13253{
13254 struct drm_i915_private *dev_priv = dev->dev_private;
13255 enum pipe pipe;
30e984df
DV
13256 struct intel_crtc *crtc;
13257 struct intel_encoder *encoder;
35c95375 13258 int i;
30e984df
DV
13259
13260 intel_modeset_readout_hw_state(dev);
24929352 13261
babea61d
JB
13262 /*
13263 * Now that we have the config, copy it to each CRTC struct
13264 * Note that this could go away if we move to using crtc_config
13265 * checking everywhere.
13266 */
d3fcc808 13267 for_each_intel_crtc(dev, crtc) {
d330a953 13268 if (crtc->active && i915.fastboot) {
f6a83288 13269 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13270 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13271 crtc->base.base.id);
13272 drm_mode_debug_printmodeline(&crtc->base.mode);
13273 }
13274 }
13275
24929352 13276 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13277 for_each_intel_encoder(dev, encoder) {
24929352
DV
13278 intel_sanitize_encoder(encoder);
13279 }
13280
055e393f 13281 for_each_pipe(dev_priv, pipe) {
24929352
DV
13282 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13283 intel_sanitize_crtc(crtc);
c0b03411 13284 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13285 }
9a935856 13286
35c95375
DV
13287 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13288 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13289
13290 if (!pll->on || pll->active)
13291 continue;
13292
13293 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13294
13295 pll->disable(dev_priv, pll);
13296 pll->on = false;
13297 }
13298
96f90c54 13299 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13300 ilk_wm_get_hw_state(dev);
13301
45e2b5f6 13302 if (force_restore) {
7d0bc1ea
VS
13303 i915_redisable_vga(dev);
13304
f30da187
DV
13305 /*
13306 * We need to use raw interfaces for restoring state to avoid
13307 * checking (bogus) intermediate states.
13308 */
055e393f 13309 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13310 struct drm_crtc *crtc =
13311 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13312
13313 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13314 crtc->primary->fb);
45e2b5f6
DV
13315 }
13316 } else {
13317 intel_modeset_update_staged_output_state(dev);
13318 }
8af6cf88
DV
13319
13320 intel_modeset_check_state(dev);
2c7111db
CW
13321}
13322
13323void intel_modeset_gem_init(struct drm_device *dev)
13324{
484b41dd 13325 struct drm_crtc *c;
2ff8fde1 13326 struct drm_i915_gem_object *obj;
484b41dd 13327
ae48434c
ID
13328 mutex_lock(&dev->struct_mutex);
13329 intel_init_gt_powersave(dev);
13330 mutex_unlock(&dev->struct_mutex);
13331
1833b134 13332 intel_modeset_init_hw(dev);
02e792fb
DV
13333
13334 intel_setup_overlay(dev);
484b41dd
JB
13335
13336 /*
13337 * Make sure any fbs we allocated at startup are properly
13338 * pinned & fenced. When we do the allocation it's too early
13339 * for this.
13340 */
13341 mutex_lock(&dev->struct_mutex);
70e1e0ec 13342 for_each_crtc(dev, c) {
2ff8fde1
MR
13343 obj = intel_fb_obj(c->primary->fb);
13344 if (obj == NULL)
484b41dd
JB
13345 continue;
13346
2ff8fde1 13347 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13348 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13349 to_intel_crtc(c)->pipe);
66e514c1
DA
13350 drm_framebuffer_unreference(c->primary->fb);
13351 c->primary->fb = NULL;
484b41dd
JB
13352 }
13353 }
13354 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13355}
13356
4932e2c3
ID
13357void intel_connector_unregister(struct intel_connector *intel_connector)
13358{
13359 struct drm_connector *connector = &intel_connector->base;
13360
13361 intel_panel_destroy_backlight(connector);
34ea3d38 13362 drm_connector_unregister(connector);
4932e2c3
ID
13363}
13364
79e53945
JB
13365void intel_modeset_cleanup(struct drm_device *dev)
13366{
652c393a 13367 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13368 struct drm_connector *connector;
652c393a 13369
fd0c0642
DV
13370 /*
13371 * Interrupts and polling as the first thing to avoid creating havoc.
13372 * Too much stuff here (turning of rps, connectors, ...) would
13373 * experience fancy races otherwise.
13374 */
13375 drm_irq_uninstall(dev);
1d0d343a 13376 intel_hpd_cancel_work(dev_priv);
eb21b92b
JB
13377 dev_priv->pm._irqs_disabled = true;
13378
fd0c0642
DV
13379 /*
13380 * Due to the hpd irq storm handling the hotplug work can re-arm the
13381 * poll handlers. Hence disable polling after hpd handling is shut down.
13382 */
f87ea761 13383 drm_kms_helper_poll_fini(dev);
fd0c0642 13384
652c393a
JB
13385 mutex_lock(&dev->struct_mutex);
13386
723bfd70
JB
13387 intel_unregister_dsm_handler();
13388
973d04f9 13389 intel_disable_fbc(dev);
e70236a8 13390
8090c6b9 13391 intel_disable_gt_powersave(dev);
0cdab21f 13392
930ebb46
DV
13393 ironlake_teardown_rc6(dev);
13394
69341a5e
KH
13395 mutex_unlock(&dev->struct_mutex);
13396
1630fe75
CW
13397 /* flush any delayed tasks or pending work */
13398 flush_scheduled_work();
13399
db31af1d
JN
13400 /* destroy the backlight and sysfs files before encoders/connectors */
13401 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13402 struct intel_connector *intel_connector;
13403
13404 intel_connector = to_intel_connector(connector);
13405 intel_connector->unregister(intel_connector);
db31af1d 13406 }
d9255d57 13407
79e53945 13408 drm_mode_config_cleanup(dev);
4d7bb011
DV
13409
13410 intel_cleanup_overlay(dev);
ae48434c
ID
13411
13412 mutex_lock(&dev->struct_mutex);
13413 intel_cleanup_gt_powersave(dev);
13414 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13415}
13416
f1c79df3
ZW
13417/*
13418 * Return which encoder is currently attached for connector.
13419 */
df0e9248 13420struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13421{
df0e9248
CW
13422 return &intel_attached_encoder(connector)->base;
13423}
f1c79df3 13424
df0e9248
CW
13425void intel_connector_attach_encoder(struct intel_connector *connector,
13426 struct intel_encoder *encoder)
13427{
13428 connector->encoder = encoder;
13429 drm_mode_connector_attach_encoder(&connector->base,
13430 &encoder->base);
79e53945 13431}
28d52043
DA
13432
13433/*
13434 * set vga decode state - true == enable VGA decode
13435 */
13436int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13437{
13438 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13439 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13440 u16 gmch_ctrl;
13441
75fa041d
CW
13442 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13443 DRM_ERROR("failed to read control word\n");
13444 return -EIO;
13445 }
13446
c0cc8a55
CW
13447 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13448 return 0;
13449
28d52043
DA
13450 if (state)
13451 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13452 else
13453 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13454
13455 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13456 DRM_ERROR("failed to write control word\n");
13457 return -EIO;
13458 }
13459
28d52043
DA
13460 return 0;
13461}
c4a1d9e4 13462
c4a1d9e4 13463struct intel_display_error_state {
ff57f1b0
PZ
13464
13465 u32 power_well_driver;
13466
63b66e5b
CW
13467 int num_transcoders;
13468
c4a1d9e4
CW
13469 struct intel_cursor_error_state {
13470 u32 control;
13471 u32 position;
13472 u32 base;
13473 u32 size;
52331309 13474 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13475
13476 struct intel_pipe_error_state {
ddf9c536 13477 bool power_domain_on;
c4a1d9e4 13478 u32 source;
f301b1e1 13479 u32 stat;
52331309 13480 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13481
13482 struct intel_plane_error_state {
13483 u32 control;
13484 u32 stride;
13485 u32 size;
13486 u32 pos;
13487 u32 addr;
13488 u32 surface;
13489 u32 tile_offset;
52331309 13490 } plane[I915_MAX_PIPES];
63b66e5b
CW
13491
13492 struct intel_transcoder_error_state {
ddf9c536 13493 bool power_domain_on;
63b66e5b
CW
13494 enum transcoder cpu_transcoder;
13495
13496 u32 conf;
13497
13498 u32 htotal;
13499 u32 hblank;
13500 u32 hsync;
13501 u32 vtotal;
13502 u32 vblank;
13503 u32 vsync;
13504 } transcoder[4];
c4a1d9e4
CW
13505};
13506
13507struct intel_display_error_state *
13508intel_display_capture_error_state(struct drm_device *dev)
13509{
fbee40df 13510 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13511 struct intel_display_error_state *error;
63b66e5b
CW
13512 int transcoders[] = {
13513 TRANSCODER_A,
13514 TRANSCODER_B,
13515 TRANSCODER_C,
13516 TRANSCODER_EDP,
13517 };
c4a1d9e4
CW
13518 int i;
13519
63b66e5b
CW
13520 if (INTEL_INFO(dev)->num_pipes == 0)
13521 return NULL;
13522
9d1cb914 13523 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13524 if (error == NULL)
13525 return NULL;
13526
190be112 13527 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13528 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13529
055e393f 13530 for_each_pipe(dev_priv, i) {
ddf9c536 13531 error->pipe[i].power_domain_on =
f458ebbc
DV
13532 __intel_display_power_is_enabled(dev_priv,
13533 POWER_DOMAIN_PIPE(i));
ddf9c536 13534 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13535 continue;
13536
5efb3e28
VS
13537 error->cursor[i].control = I915_READ(CURCNTR(i));
13538 error->cursor[i].position = I915_READ(CURPOS(i));
13539 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13540
13541 error->plane[i].control = I915_READ(DSPCNTR(i));
13542 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13543 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13544 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13545 error->plane[i].pos = I915_READ(DSPPOS(i));
13546 }
ca291363
PZ
13547 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13548 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13549 if (INTEL_INFO(dev)->gen >= 4) {
13550 error->plane[i].surface = I915_READ(DSPSURF(i));
13551 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13552 }
13553
c4a1d9e4 13554 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13555
3abfce77 13556 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13557 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13558 }
13559
13560 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13561 if (HAS_DDI(dev_priv->dev))
13562 error->num_transcoders++; /* Account for eDP. */
13563
13564 for (i = 0; i < error->num_transcoders; i++) {
13565 enum transcoder cpu_transcoder = transcoders[i];
13566
ddf9c536 13567 error->transcoder[i].power_domain_on =
f458ebbc 13568 __intel_display_power_is_enabled(dev_priv,
38cc1daf 13569 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13570 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13571 continue;
13572
63b66e5b
CW
13573 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13574
13575 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13576 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13577 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13578 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13579 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13580 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13581 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13582 }
13583
13584 return error;
13585}
13586
edc3d884
MK
13587#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13588
c4a1d9e4 13589void
edc3d884 13590intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13591 struct drm_device *dev,
13592 struct intel_display_error_state *error)
13593{
055e393f 13594 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13595 int i;
13596
63b66e5b
CW
13597 if (!error)
13598 return;
13599
edc3d884 13600 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13601 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13602 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13603 error->power_well_driver);
055e393f 13604 for_each_pipe(dev_priv, i) {
edc3d884 13605 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13606 err_printf(m, " Power: %s\n",
13607 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13608 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13609 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13610
13611 err_printf(m, "Plane [%d]:\n", i);
13612 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13613 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13614 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13615 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13616 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13617 }
4b71a570 13618 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13619 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13620 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13621 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13622 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13623 }
13624
edc3d884
MK
13625 err_printf(m, "Cursor [%d]:\n", i);
13626 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13627 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13628 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13629 }
63b66e5b
CW
13630
13631 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13632 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13633 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13634 err_printf(m, " Power: %s\n",
13635 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13636 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13637 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13638 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13639 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13640 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13641 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13642 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13643 }
c4a1d9e4 13644}
e2fcdaa9
VS
13645
13646void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13647{
13648 struct intel_crtc *crtc;
13649
13650 for_each_intel_crtc(dev, crtc) {
13651 struct intel_unpin_work *work;
e2fcdaa9 13652
5e2d7afc 13653 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
13654
13655 work = crtc->unpin_work;
13656
13657 if (work && work->event &&
13658 work->event->base.file_priv == file) {
13659 kfree(work->event);
13660 work->event = NULL;
13661 }
13662
5e2d7afc 13663 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
13664 }
13665}
This page took 3.043979 seconds and 5 git commands to generate.