drm/i915: remove leftover from pre-universal planes days
[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
6b383a7f 76static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 77
f1f644dc
JB
78static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
79 struct intel_crtc_config *pipe_config);
18442d08
VS
80static void ironlake_pch_clock_get(struct intel_crtc *crtc,
81 struct intel_crtc_config *pipe_config);
f1f644dc 82
e7457a9a
DL
83static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
84 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
85static int intel_framebuffer_init(struct drm_device *dev,
86 struct intel_framebuffer *ifb,
87 struct drm_mode_fb_cmd2 *mode_cmd,
88 struct drm_i915_gem_object *obj);
5b18e57c
DV
89static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
90static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 91static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
92 struct intel_link_m_n *m_n,
93 struct intel_link_m_n *m2_n2);
29407aab 94static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
95static void haswell_set_pipeconf(struct drm_crtc *crtc);
96static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 97static void vlv_prepare_pll(struct intel_crtc *crtc);
1ae0d137 98static void chv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 99
0e32b39c
DA
100static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
101{
102 if (!connector->mst_port)
103 return connector->encoder;
104 else
105 return &connector->mst_port->mst_encoders[pipe]->base;
106}
107
79e53945 108typedef struct {
0206e353 109 int min, max;
79e53945
JB
110} intel_range_t;
111
112typedef struct {
0206e353
AJ
113 int dot_limit;
114 int p2_slow, p2_fast;
79e53945
JB
115} intel_p2_t;
116
d4906093
ML
117typedef struct intel_limit intel_limit_t;
118struct intel_limit {
0206e353
AJ
119 intel_range_t dot, vco, n, m, m1, m2, p, p1;
120 intel_p2_t p2;
d4906093 121};
79e53945 122
d2acd215
DV
123int
124intel_pch_rawclk(struct drm_device *dev)
125{
126 struct drm_i915_private *dev_priv = dev->dev_private;
127
128 WARN_ON(!HAS_PCH_SPLIT(dev));
129
130 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
131}
132
021357ac
CW
133static inline u32 /* units of 100MHz */
134intel_fdi_link_freq(struct drm_device *dev)
135{
8b99e68c
CW
136 if (IS_GEN5(dev)) {
137 struct drm_i915_private *dev_priv = dev->dev_private;
138 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
139 } else
140 return 27;
021357ac
CW
141}
142
5d536e28 143static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 144 .dot = { .min = 25000, .max = 350000 },
9c333719 145 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 146 .n = { .min = 2, .max = 16 },
0206e353
AJ
147 .m = { .min = 96, .max = 140 },
148 .m1 = { .min = 18, .max = 26 },
149 .m2 = { .min = 6, .max = 16 },
150 .p = { .min = 4, .max = 128 },
151 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
152 .p2 = { .dot_limit = 165000,
153 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
154};
155
5d536e28
DV
156static const intel_limit_t intel_limits_i8xx_dvo = {
157 .dot = { .min = 25000, .max = 350000 },
9c333719 158 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 159 .n = { .min = 2, .max = 16 },
5d536e28
DV
160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 4 },
167};
168
e4b36699 169static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 170 .dot = { .min = 25000, .max = 350000 },
9c333719 171 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 172 .n = { .min = 2, .max = 16 },
0206e353
AJ
173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 14, .p2_fast = 7 },
e4b36699 180};
273e27ca 181
e4b36699 182static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
183 .dot = { .min = 20000, .max = 400000 },
184 .vco = { .min = 1400000, .max = 2800000 },
185 .n = { .min = 1, .max = 6 },
186 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
187 .m1 = { .min = 8, .max = 18 },
188 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
189 .p = { .min = 5, .max = 80 },
190 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
191 .p2 = { .dot_limit = 200000,
192 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
193};
194
195static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
202 .p = { .min = 7, .max = 98 },
203 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
204 .p2 = { .dot_limit = 112000,
205 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
206};
207
273e27ca 208
e4b36699 209static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
210 .dot = { .min = 25000, .max = 270000 },
211 .vco = { .min = 1750000, .max = 3500000},
212 .n = { .min = 1, .max = 4 },
213 .m = { .min = 104, .max = 138 },
214 .m1 = { .min = 17, .max = 23 },
215 .m2 = { .min = 5, .max = 11 },
216 .p = { .min = 10, .max = 30 },
217 .p1 = { .min = 1, .max = 3},
218 .p2 = { .dot_limit = 270000,
219 .p2_slow = 10,
220 .p2_fast = 10
044c7c41 221 },
e4b36699
KP
222};
223
224static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
225 .dot = { .min = 22000, .max = 400000 },
226 .vco = { .min = 1750000, .max = 3500000},
227 .n = { .min = 1, .max = 4 },
228 .m = { .min = 104, .max = 138 },
229 .m1 = { .min = 16, .max = 23 },
230 .m2 = { .min = 5, .max = 11 },
231 .p = { .min = 5, .max = 80 },
232 .p1 = { .min = 1, .max = 8},
233 .p2 = { .dot_limit = 165000,
234 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
235};
236
237static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
238 .dot = { .min = 20000, .max = 115000 },
239 .vco = { .min = 1750000, .max = 3500000 },
240 .n = { .min = 1, .max = 3 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 17, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 28, .max = 112 },
245 .p1 = { .min = 2, .max = 8 },
246 .p2 = { .dot_limit = 0,
247 .p2_slow = 14, .p2_fast = 14
044c7c41 248 },
e4b36699
KP
249};
250
251static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
252 .dot = { .min = 80000, .max = 224000 },
253 .vco = { .min = 1750000, .max = 3500000 },
254 .n = { .min = 1, .max = 3 },
255 .m = { .min = 104, .max = 138 },
256 .m1 = { .min = 17, .max = 23 },
257 .m2 = { .min = 5, .max = 11 },
258 .p = { .min = 14, .max = 42 },
259 .p1 = { .min = 2, .max = 6 },
260 .p2 = { .dot_limit = 0,
261 .p2_slow = 7, .p2_fast = 7
044c7c41 262 },
e4b36699
KP
263};
264
f2b115e6 265static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
266 .dot = { .min = 20000, .max = 400000},
267 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 268 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
269 .n = { .min = 3, .max = 6 },
270 .m = { .min = 2, .max = 256 },
273e27ca 271 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
272 .m1 = { .min = 0, .max = 0 },
273 .m2 = { .min = 0, .max = 254 },
274 .p = { .min = 5, .max = 80 },
275 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
276 .p2 = { .dot_limit = 200000,
277 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
278};
279
f2b115e6 280static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
281 .dot = { .min = 20000, .max = 400000 },
282 .vco = { .min = 1700000, .max = 3500000 },
283 .n = { .min = 3, .max = 6 },
284 .m = { .min = 2, .max = 256 },
285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 7, .max = 112 },
288 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
289 .p2 = { .dot_limit = 112000,
290 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
291};
292
273e27ca
EA
293/* Ironlake / Sandybridge
294 *
295 * We calculate clock using (register_value + 2) for N/M1/M2, so here
296 * the range value for them is (actual_value - 2).
297 */
b91ad0ec 298static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
299 .dot = { .min = 25000, .max = 350000 },
300 .vco = { .min = 1760000, .max = 3510000 },
301 .n = { .min = 1, .max = 5 },
302 .m = { .min = 79, .max = 127 },
303 .m1 = { .min = 12, .max = 22 },
304 .m2 = { .min = 5, .max = 9 },
305 .p = { .min = 5, .max = 80 },
306 .p1 = { .min = 1, .max = 8 },
307 .p2 = { .dot_limit = 225000,
308 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
309};
310
b91ad0ec 311static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 3 },
315 .m = { .min = 79, .max = 118 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 28, .max = 112 },
319 .p1 = { .min = 2, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
322};
323
324static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 127 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 14, .max = 56 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
335};
336
273e27ca 337/* LVDS 100mhz refclk limits. */
b91ad0ec 338static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
339 .dot = { .min = 25000, .max = 350000 },
340 .vco = { .min = 1760000, .max = 3510000 },
341 .n = { .min = 1, .max = 2 },
342 .m = { .min = 79, .max = 126 },
343 .m1 = { .min = 12, .max = 22 },
344 .m2 = { .min = 5, .max = 9 },
345 .p = { .min = 28, .max = 112 },
0206e353 346 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
347 .p2 = { .dot_limit = 225000,
348 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
349};
350
351static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 14, .max = 42 },
0206e353 359 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
362};
363
dc730512 364static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
365 /*
366 * These are the data rate limits (measured in fast clocks)
367 * since those are the strictest limits we have. The fast
368 * clock and actual rate limits are more relaxed, so checking
369 * them would make no difference.
370 */
371 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 372 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 373 .n = { .min = 1, .max = 7 },
a0c4da24
JB
374 .m1 = { .min = 2, .max = 3 },
375 .m2 = { .min = 11, .max = 156 },
b99ab663 376 .p1 = { .min = 2, .max = 3 },
5fdc9c49 377 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
378};
379
ef9348c8
CML
380static const intel_limit_t intel_limits_chv = {
381 /*
382 * These are the data rate limits (measured in fast clocks)
383 * since those are the strictest limits we have. The fast
384 * clock and actual rate limits are more relaxed, so checking
385 * them would make no difference.
386 */
387 .dot = { .min = 25000 * 5, .max = 540000 * 5},
388 .vco = { .min = 4860000, .max = 6700000 },
389 .n = { .min = 1, .max = 1 },
390 .m1 = { .min = 2, .max = 2 },
391 .m2 = { .min = 24 << 22, .max = 175 << 22 },
392 .p1 = { .min = 2, .max = 4 },
393 .p2 = { .p2_slow = 1, .p2_fast = 14 },
394};
395
6b4bf1c4
VS
396static void vlv_clock(int refclk, intel_clock_t *clock)
397{
398 clock->m = clock->m1 * clock->m2;
399 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
400 if (WARN_ON(clock->n == 0 || clock->p == 0))
401 return;
fb03ac01
VS
402 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
403 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
404}
405
e0638cdf
PZ
406/**
407 * Returns whether any output on the specified pipe is of the specified type
408 */
409static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
410{
411 struct drm_device *dev = crtc->dev;
412 struct intel_encoder *encoder;
413
414 for_each_encoder_on_crtc(dev, crtc, encoder)
415 if (encoder->type == type)
416 return true;
417
418 return false;
419}
420
1b894b59
CW
421static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
422 int refclk)
2c07245f 423{
b91ad0ec 424 struct drm_device *dev = crtc->dev;
2c07245f 425 const intel_limit_t *limit;
b91ad0ec
ZW
426
427 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 428 if (intel_is_dual_link_lvds(dev)) {
1b894b59 429 if (refclk == 100000)
b91ad0ec
ZW
430 limit = &intel_limits_ironlake_dual_lvds_100m;
431 else
432 limit = &intel_limits_ironlake_dual_lvds;
433 } else {
1b894b59 434 if (refclk == 100000)
b91ad0ec
ZW
435 limit = &intel_limits_ironlake_single_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_single_lvds;
438 }
c6bb3538 439 } else
b91ad0ec 440 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
441
442 return limit;
443}
444
044c7c41
ML
445static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
446{
447 struct drm_device *dev = crtc->dev;
044c7c41
ML
448 const intel_limit_t *limit;
449
450 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 451 if (intel_is_dual_link_lvds(dev))
e4b36699 452 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 453 else
e4b36699 454 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
455 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
456 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 457 limit = &intel_limits_g4x_hdmi;
044c7c41 458 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 459 limit = &intel_limits_g4x_sdvo;
044c7c41 460 } else /* The option is for other outputs */
e4b36699 461 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
462
463 return limit;
464}
465
1b894b59 466static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
467{
468 struct drm_device *dev = crtc->dev;
469 const intel_limit_t *limit;
470
bad720ff 471 if (HAS_PCH_SPLIT(dev))
1b894b59 472 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 473 else if (IS_G4X(dev)) {
044c7c41 474 limit = intel_g4x_limit(crtc);
f2b115e6 475 } else if (IS_PINEVIEW(dev)) {
2177832f 476 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 477 limit = &intel_limits_pineview_lvds;
2177832f 478 else
f2b115e6 479 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
480 } else if (IS_CHERRYVIEW(dev)) {
481 limit = &intel_limits_chv;
a0c4da24 482 } else if (IS_VALLEYVIEW(dev)) {
dc730512 483 limit = &intel_limits_vlv;
a6c45cf0
CW
484 } else if (!IS_GEN2(dev)) {
485 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
486 limit = &intel_limits_i9xx_lvds;
487 else
488 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
489 } else {
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 491 limit = &intel_limits_i8xx_lvds;
5d536e28 492 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 493 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
494 else
495 limit = &intel_limits_i8xx_dac;
79e53945
JB
496 }
497 return limit;
498}
499
f2b115e6
AJ
500/* m1 is reserved as 0 in Pineview, n is a ring counter */
501static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 502{
2177832f
SL
503 clock->m = clock->m2 + 2;
504 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
505 if (WARN_ON(clock->n == 0 || clock->p == 0))
506 return;
fb03ac01
VS
507 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
508 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
509}
510
7429e9d4
DV
511static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
512{
513 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
514}
515
ac58c3f0 516static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 517{
7429e9d4 518 clock->m = i9xx_dpll_compute_m(clock);
79e53945 519 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
520 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
521 return;
fb03ac01
VS
522 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
523 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
524}
525
ef9348c8
CML
526static void chv_clock(int refclk, intel_clock_t *clock)
527{
528 clock->m = clock->m1 * clock->m2;
529 clock->p = clock->p1 * clock->p2;
530 if (WARN_ON(clock->n == 0 || clock->p == 0))
531 return;
532 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
533 clock->n << 22);
534 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
535}
536
7c04d1d9 537#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
538/**
539 * Returns whether the given set of divisors are valid for a given refclk with
540 * the given connectors.
541 */
542
1b894b59
CW
543static bool intel_PLL_is_valid(struct drm_device *dev,
544 const intel_limit_t *limit,
545 const intel_clock_t *clock)
79e53945 546{
f01b7962
VS
547 if (clock->n < limit->n.min || limit->n.max < clock->n)
548 INTELPllInvalid("n out of range\n");
79e53945 549 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 550 INTELPllInvalid("p1 out of range\n");
79e53945 551 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 552 INTELPllInvalid("m2 out of range\n");
79e53945 553 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 554 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
555
556 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
557 if (clock->m1 <= clock->m2)
558 INTELPllInvalid("m1 <= m2\n");
559
560 if (!IS_VALLEYVIEW(dev)) {
561 if (clock->p < limit->p.min || limit->p.max < clock->p)
562 INTELPllInvalid("p out of range\n");
563 if (clock->m < limit->m.min || limit->m.max < clock->m)
564 INTELPllInvalid("m out of range\n");
565 }
566
79e53945 567 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 568 INTELPllInvalid("vco out of range\n");
79e53945
JB
569 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
570 * connector, etc., rather than just a single range.
571 */
572 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 573 INTELPllInvalid("dot out of range\n");
79e53945
JB
574
575 return true;
576}
577
d4906093 578static bool
ee9300bb 579i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
580 int target, int refclk, intel_clock_t *match_clock,
581 intel_clock_t *best_clock)
79e53945
JB
582{
583 struct drm_device *dev = crtc->dev;
79e53945 584 intel_clock_t clock;
79e53945
JB
585 int err = target;
586
a210b028 587 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 588 /*
a210b028
DV
589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
79e53945 592 */
1974cad0 593 if (intel_is_dual_link_lvds(dev))
79e53945
JB
594 clock.p2 = limit->p2.p2_fast;
595 else
596 clock.p2 = limit->p2.p2_slow;
597 } else {
598 if (target < limit->p2.dot_limit)
599 clock.p2 = limit->p2.p2_slow;
600 else
601 clock.p2 = limit->p2.p2_fast;
602 }
603
0206e353 604 memset(best_clock, 0, sizeof(*best_clock));
79e53945 605
42158660
ZY
606 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
607 clock.m1++) {
608 for (clock.m2 = limit->m2.min;
609 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 610 if (clock.m2 >= clock.m1)
42158660
ZY
611 break;
612 for (clock.n = limit->n.min;
613 clock.n <= limit->n.max; clock.n++) {
614 for (clock.p1 = limit->p1.min;
615 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
616 int this_err;
617
ac58c3f0
DV
618 i9xx_clock(refclk, &clock);
619 if (!intel_PLL_is_valid(dev, limit,
620 &clock))
621 continue;
622 if (match_clock &&
623 clock.p != match_clock->p)
624 continue;
625
626 this_err = abs(clock.dot - target);
627 if (this_err < err) {
628 *best_clock = clock;
629 err = this_err;
630 }
631 }
632 }
633 }
634 }
635
636 return (err != target);
637}
638
639static bool
ee9300bb
DV
640pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
641 int target, int refclk, intel_clock_t *match_clock,
642 intel_clock_t *best_clock)
79e53945
JB
643{
644 struct drm_device *dev = crtc->dev;
79e53945 645 intel_clock_t clock;
79e53945
JB
646 int err = target;
647
a210b028 648 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 649 /*
a210b028
DV
650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
79e53945 653 */
1974cad0 654 if (intel_is_dual_link_lvds(dev))
79e53945
JB
655 clock.p2 = limit->p2.p2_fast;
656 else
657 clock.p2 = limit->p2.p2_slow;
658 } else {
659 if (target < limit->p2.dot_limit)
660 clock.p2 = limit->p2.p2_slow;
661 else
662 clock.p2 = limit->p2.p2_fast;
663 }
664
0206e353 665 memset(best_clock, 0, sizeof(*best_clock));
79e53945 666
42158660
ZY
667 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
668 clock.m1++) {
669 for (clock.m2 = limit->m2.min;
670 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
671 for (clock.n = limit->n.min;
672 clock.n <= limit->n.max; clock.n++) {
673 for (clock.p1 = limit->p1.min;
674 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
675 int this_err;
676
ac58c3f0 677 pineview_clock(refclk, &clock);
1b894b59
CW
678 if (!intel_PLL_is_valid(dev, limit,
679 &clock))
79e53945 680 continue;
cec2f356
SP
681 if (match_clock &&
682 clock.p != match_clock->p)
683 continue;
79e53945
JB
684
685 this_err = abs(clock.dot - target);
686 if (this_err < err) {
687 *best_clock = clock;
688 err = this_err;
689 }
690 }
691 }
692 }
693 }
694
695 return (err != target);
696}
697
d4906093 698static bool
ee9300bb
DV
699g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
700 int target, int refclk, intel_clock_t *match_clock,
701 intel_clock_t *best_clock)
d4906093
ML
702{
703 struct drm_device *dev = crtc->dev;
d4906093
ML
704 intel_clock_t clock;
705 int max_n;
706 bool found;
6ba770dc
AJ
707 /* approximately equals target * 0.00585 */
708 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
709 found = false;
710
711 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 712 if (intel_is_dual_link_lvds(dev))
d4906093
ML
713 clock.p2 = limit->p2.p2_fast;
714 else
715 clock.p2 = limit->p2.p2_slow;
716 } else {
717 if (target < limit->p2.dot_limit)
718 clock.p2 = limit->p2.p2_slow;
719 else
720 clock.p2 = limit->p2.p2_fast;
721 }
722
723 memset(best_clock, 0, sizeof(*best_clock));
724 max_n = limit->n.max;
f77f13e2 725 /* based on hardware requirement, prefer smaller n to precision */
d4906093 726 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 727 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
728 for (clock.m1 = limit->m1.max;
729 clock.m1 >= limit->m1.min; clock.m1--) {
730 for (clock.m2 = limit->m2.max;
731 clock.m2 >= limit->m2.min; clock.m2--) {
732 for (clock.p1 = limit->p1.max;
733 clock.p1 >= limit->p1.min; clock.p1--) {
734 int this_err;
735
ac58c3f0 736 i9xx_clock(refclk, &clock);
1b894b59
CW
737 if (!intel_PLL_is_valid(dev, limit,
738 &clock))
d4906093 739 continue;
1b894b59
CW
740
741 this_err = abs(clock.dot - target);
d4906093
ML
742 if (this_err < err_most) {
743 *best_clock = clock;
744 err_most = this_err;
745 max_n = clock.n;
746 found = true;
747 }
748 }
749 }
750 }
751 }
2c07245f
ZW
752 return found;
753}
754
a0c4da24 755static bool
ee9300bb
DV
756vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
757 int target, int refclk, intel_clock_t *match_clock,
758 intel_clock_t *best_clock)
a0c4da24 759{
f01b7962 760 struct drm_device *dev = crtc->dev;
6b4bf1c4 761 intel_clock_t clock;
69e4f900 762 unsigned int bestppm = 1000000;
27e639bf
VS
763 /* min update 19.2 MHz */
764 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 765 bool found = false;
a0c4da24 766
6b4bf1c4
VS
767 target *= 5; /* fast clock */
768
769 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
770
771 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 772 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 773 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 774 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 775 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 776 clock.p = clock.p1 * clock.p2;
a0c4da24 777 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 778 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
779 unsigned int ppm, diff;
780
6b4bf1c4
VS
781 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
782 refclk * clock.m1);
783
784 vlv_clock(refclk, &clock);
43b0ac53 785
f01b7962
VS
786 if (!intel_PLL_is_valid(dev, limit,
787 &clock))
43b0ac53
VS
788 continue;
789
6b4bf1c4
VS
790 diff = abs(clock.dot - target);
791 ppm = div_u64(1000000ULL * diff, target);
792
793 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 794 bestppm = 0;
6b4bf1c4 795 *best_clock = clock;
49e497ef 796 found = true;
43b0ac53 797 }
6b4bf1c4 798
c686122c 799 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 800 bestppm = ppm;
6b4bf1c4 801 *best_clock = clock;
49e497ef 802 found = true;
a0c4da24
JB
803 }
804 }
805 }
806 }
807 }
a0c4da24 808
49e497ef 809 return found;
a0c4da24 810}
a4fc5ed6 811
ef9348c8
CML
812static bool
813chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
814 int target, int refclk, intel_clock_t *match_clock,
815 intel_clock_t *best_clock)
816{
817 struct drm_device *dev = crtc->dev;
818 intel_clock_t clock;
819 uint64_t m2;
820 int found = false;
821
822 memset(best_clock, 0, sizeof(*best_clock));
823
824 /*
825 * Based on hardware doc, the n always set to 1, and m1 always
826 * set to 2. If requires to support 200Mhz refclk, we need to
827 * revisit this because n may not 1 anymore.
828 */
829 clock.n = 1, clock.m1 = 2;
830 target *= 5; /* fast clock */
831
832 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
833 for (clock.p2 = limit->p2.p2_fast;
834 clock.p2 >= limit->p2.p2_slow;
835 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
836
837 clock.p = clock.p1 * clock.p2;
838
839 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
840 clock.n) << 22, refclk * clock.m1);
841
842 if (m2 > INT_MAX/clock.m1)
843 continue;
844
845 clock.m2 = m2;
846
847 chv_clock(refclk, &clock);
848
849 if (!intel_PLL_is_valid(dev, limit, &clock))
850 continue;
851
852 /* based on hardware requirement, prefer bigger p
853 */
854 if (clock.p > best_clock->p) {
855 *best_clock = clock;
856 found = true;
857 }
858 }
859 }
860
861 return found;
862}
863
20ddf665
VS
864bool intel_crtc_active(struct drm_crtc *crtc)
865{
866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
867
868 /* Be paranoid as we can arrive here with only partial
869 * state retrieved from the hardware during setup.
870 *
241bfc38 871 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
872 * as Haswell has gained clock readout/fastboot support.
873 *
66e514c1 874 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
875 * properly reconstruct framebuffers.
876 */
f4510a27 877 return intel_crtc->active && crtc->primary->fb &&
241bfc38 878 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
879}
880
a5c961d1
PZ
881enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
882 enum pipe pipe)
883{
884 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
886
3b117c8f 887 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
888}
889
fbf49ea2
VS
890static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
891{
892 struct drm_i915_private *dev_priv = dev->dev_private;
893 u32 reg = PIPEDSL(pipe);
894 u32 line1, line2;
895 u32 line_mask;
896
897 if (IS_GEN2(dev))
898 line_mask = DSL_LINEMASK_GEN2;
899 else
900 line_mask = DSL_LINEMASK_GEN3;
901
902 line1 = I915_READ(reg) & line_mask;
903 mdelay(5);
904 line2 = I915_READ(reg) & line_mask;
905
906 return line1 == line2;
907}
908
ab7ad7f6
KP
909/*
910 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 911 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
912 *
913 * After disabling a pipe, we can't wait for vblank in the usual way,
914 * spinning on the vblank interrupt status bit, since we won't actually
915 * see an interrupt when the pipe is disabled.
916 *
ab7ad7f6
KP
917 * On Gen4 and above:
918 * wait for the pipe register state bit to turn off
919 *
920 * Otherwise:
921 * wait for the display line value to settle (it usually
922 * ends up stopping at the start of the next frame).
58e10eb9 923 *
9d0498a2 924 */
575f7ab7 925static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 926{
575f7ab7 927 struct drm_device *dev = crtc->base.dev;
9d0498a2 928 struct drm_i915_private *dev_priv = dev->dev_private;
575f7ab7
VS
929 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
930 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
931
932 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 933 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
934
935 /* Wait for the Pipe State to go off */
58e10eb9
CW
936 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
937 100))
284637d9 938 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 939 } else {
ab7ad7f6 940 /* Wait for the display line to settle */
fbf49ea2 941 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 942 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 943 }
79e53945
JB
944}
945
b0ea7d37
DL
946/*
947 * ibx_digital_port_connected - is the specified port connected?
948 * @dev_priv: i915 private structure
949 * @port: the port to test
950 *
951 * Returns true if @port is connected, false otherwise.
952 */
953bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
954 struct intel_digital_port *port)
955{
956 u32 bit;
957
c36346e3 958 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 959 switch (port->port) {
c36346e3
DL
960 case PORT_B:
961 bit = SDE_PORTB_HOTPLUG;
962 break;
963 case PORT_C:
964 bit = SDE_PORTC_HOTPLUG;
965 break;
966 case PORT_D:
967 bit = SDE_PORTD_HOTPLUG;
968 break;
969 default:
970 return true;
971 }
972 } else {
eba905b2 973 switch (port->port) {
c36346e3
DL
974 case PORT_B:
975 bit = SDE_PORTB_HOTPLUG_CPT;
976 break;
977 case PORT_C:
978 bit = SDE_PORTC_HOTPLUG_CPT;
979 break;
980 case PORT_D:
981 bit = SDE_PORTD_HOTPLUG_CPT;
982 break;
983 default:
984 return true;
985 }
b0ea7d37
DL
986 }
987
988 return I915_READ(SDEISR) & bit;
989}
990
b24e7179
JB
991static const char *state_string(bool enabled)
992{
993 return enabled ? "on" : "off";
994}
995
996/* Only for pre-ILK configs */
55607e8a
DV
997void assert_pll(struct drm_i915_private *dev_priv,
998 enum pipe pipe, bool state)
b24e7179
JB
999{
1000 int reg;
1001 u32 val;
1002 bool cur_state;
1003
1004 reg = DPLL(pipe);
1005 val = I915_READ(reg);
1006 cur_state = !!(val & DPLL_VCO_ENABLE);
1007 WARN(cur_state != state,
1008 "PLL state assertion failure (expected %s, current %s)\n",
1009 state_string(state), state_string(cur_state));
1010}
b24e7179 1011
23538ef1
JN
1012/* XXX: the dsi pll is shared between MIPI DSI ports */
1013static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1014{
1015 u32 val;
1016 bool cur_state;
1017
1018 mutex_lock(&dev_priv->dpio_lock);
1019 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1020 mutex_unlock(&dev_priv->dpio_lock);
1021
1022 cur_state = val & DSI_PLL_VCO_EN;
1023 WARN(cur_state != state,
1024 "DSI PLL state assertion failure (expected %s, current %s)\n",
1025 state_string(state), state_string(cur_state));
1026}
1027#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1028#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1029
55607e8a 1030struct intel_shared_dpll *
e2b78267
DV
1031intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1032{
1033 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1034
a43f6e0f 1035 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1036 return NULL;
1037
a43f6e0f 1038 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1039}
1040
040484af 1041/* For ILK+ */
55607e8a
DV
1042void assert_shared_dpll(struct drm_i915_private *dev_priv,
1043 struct intel_shared_dpll *pll,
1044 bool state)
040484af 1045{
040484af 1046 bool cur_state;
5358901f 1047 struct intel_dpll_hw_state hw_state;
040484af 1048
92b27b08 1049 if (WARN (!pll,
46edb027 1050 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1051 return;
ee7b9f93 1052
5358901f 1053 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1054 WARN(cur_state != state,
5358901f
DV
1055 "%s assertion failure (expected %s, current %s)\n",
1056 pll->name, state_string(state), state_string(cur_state));
040484af 1057}
040484af
JB
1058
1059static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1060 enum pipe pipe, bool state)
1061{
1062 int reg;
1063 u32 val;
1064 bool cur_state;
ad80a810
PZ
1065 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1066 pipe);
040484af 1067
affa9354
PZ
1068 if (HAS_DDI(dev_priv->dev)) {
1069 /* DDI does not have a specific FDI_TX register */
ad80a810 1070 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1071 val = I915_READ(reg);
ad80a810 1072 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1073 } else {
1074 reg = FDI_TX_CTL(pipe);
1075 val = I915_READ(reg);
1076 cur_state = !!(val & FDI_TX_ENABLE);
1077 }
040484af
JB
1078 WARN(cur_state != state,
1079 "FDI TX state assertion failure (expected %s, current %s)\n",
1080 state_string(state), state_string(cur_state));
1081}
1082#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1083#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1084
1085static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
1087{
1088 int reg;
1089 u32 val;
1090 bool cur_state;
1091
d63fa0dc
PZ
1092 reg = FDI_RX_CTL(pipe);
1093 val = I915_READ(reg);
1094 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1095 WARN(cur_state != state,
1096 "FDI RX state assertion failure (expected %s, current %s)\n",
1097 state_string(state), state_string(cur_state));
1098}
1099#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1100#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1101
1102static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1103 enum pipe pipe)
1104{
1105 int reg;
1106 u32 val;
1107
1108 /* ILK FDI PLL is always enabled */
3d13ef2e 1109 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1110 return;
1111
bf507ef7 1112 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1113 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1114 return;
1115
040484af
JB
1116 reg = FDI_TX_CTL(pipe);
1117 val = I915_READ(reg);
1118 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1119}
1120
55607e8a
DV
1121void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1122 enum pipe pipe, bool state)
040484af
JB
1123{
1124 int reg;
1125 u32 val;
55607e8a 1126 bool cur_state;
040484af
JB
1127
1128 reg = FDI_RX_CTL(pipe);
1129 val = I915_READ(reg);
55607e8a
DV
1130 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1131 WARN(cur_state != state,
1132 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1133 state_string(state), state_string(cur_state));
040484af
JB
1134}
1135
b680c37a
DV
1136void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1137 enum pipe pipe)
ea0760cf 1138{
bedd4dba
JN
1139 struct drm_device *dev = dev_priv->dev;
1140 int pp_reg;
ea0760cf
JB
1141 u32 val;
1142 enum pipe panel_pipe = PIPE_A;
0de3b485 1143 bool locked = true;
ea0760cf 1144
bedd4dba
JN
1145 if (WARN_ON(HAS_DDI(dev)))
1146 return;
1147
1148 if (HAS_PCH_SPLIT(dev)) {
1149 u32 port_sel;
1150
ea0760cf 1151 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1152 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1153
1154 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1155 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1156 panel_pipe = PIPE_B;
1157 /* XXX: else fix for eDP */
1158 } else if (IS_VALLEYVIEW(dev)) {
1159 /* presumably write lock depends on pipe, not port select */
1160 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1161 panel_pipe = pipe;
ea0760cf
JB
1162 } else {
1163 pp_reg = PP_CONTROL;
bedd4dba
JN
1164 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1165 panel_pipe = PIPE_B;
ea0760cf
JB
1166 }
1167
1168 val = I915_READ(pp_reg);
1169 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1170 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1171 locked = false;
1172
ea0760cf
JB
1173 WARN(panel_pipe == pipe && locked,
1174 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1175 pipe_name(pipe));
ea0760cf
JB
1176}
1177
93ce0ba6
JN
1178static void assert_cursor(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
1180{
1181 struct drm_device *dev = dev_priv->dev;
1182 bool cur_state;
1183
d9d82081 1184 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1185 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1186 else
5efb3e28 1187 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1188
1189 WARN(cur_state != state,
1190 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1191 pipe_name(pipe), state_string(state), state_string(cur_state));
1192}
1193#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1194#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1195
b840d907
JB
1196void assert_pipe(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, bool state)
b24e7179
JB
1198{
1199 int reg;
1200 u32 val;
63d7bbe9 1201 bool cur_state;
702e7a56
PZ
1202 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1203 pipe);
b24e7179 1204
b6b5d049
VS
1205 /* if we need the pipe quirk it must be always on */
1206 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1207 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1208 state = true;
1209
f458ebbc 1210 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1211 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1212 cur_state = false;
1213 } else {
1214 reg = PIPECONF(cpu_transcoder);
1215 val = I915_READ(reg);
1216 cur_state = !!(val & PIPECONF_ENABLE);
1217 }
1218
63d7bbe9
JB
1219 WARN(cur_state != state,
1220 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1221 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1222}
1223
931872fc
CW
1224static void assert_plane(struct drm_i915_private *dev_priv,
1225 enum plane plane, bool state)
b24e7179
JB
1226{
1227 int reg;
1228 u32 val;
931872fc 1229 bool cur_state;
b24e7179
JB
1230
1231 reg = DSPCNTR(plane);
1232 val = I915_READ(reg);
931872fc
CW
1233 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1234 WARN(cur_state != state,
1235 "plane %c assertion failure (expected %s, current %s)\n",
1236 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1237}
1238
931872fc
CW
1239#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1240#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1241
b24e7179
JB
1242static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1243 enum pipe pipe)
1244{
653e1026 1245 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1246 int reg, i;
1247 u32 val;
1248 int cur_pipe;
1249
653e1026
VS
1250 /* Primary planes are fixed to pipes on gen4+ */
1251 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1252 reg = DSPCNTR(pipe);
1253 val = I915_READ(reg);
83f26f16 1254 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1255 "plane %c assertion failure, should be disabled but not\n",
1256 plane_name(pipe));
19ec1358 1257 return;
28c05794 1258 }
19ec1358 1259
b24e7179 1260 /* Need to check both planes against the pipe */
055e393f 1261 for_each_pipe(dev_priv, i) {
b24e7179
JB
1262 reg = DSPCNTR(i);
1263 val = I915_READ(reg);
1264 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1265 DISPPLANE_SEL_PIPE_SHIFT;
1266 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1267 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1268 plane_name(i), pipe_name(pipe));
b24e7179
JB
1269 }
1270}
1271
19332d7a
JB
1272static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1273 enum pipe pipe)
1274{
20674eef 1275 struct drm_device *dev = dev_priv->dev;
1fe47785 1276 int reg, sprite;
19332d7a
JB
1277 u32 val;
1278
7feb8b88
DL
1279 if (INTEL_INFO(dev)->gen >= 9) {
1280 for_each_sprite(pipe, sprite) {
1281 val = I915_READ(PLANE_CTL(pipe, sprite));
1282 WARN(val & PLANE_CTL_ENABLE,
1283 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1284 sprite, pipe_name(pipe));
1285 }
1286 } else if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1287 for_each_sprite(pipe, sprite) {
1288 reg = SPCNTR(pipe, sprite);
20674eef 1289 val = I915_READ(reg);
83f26f16 1290 WARN(val & SP_ENABLE,
20674eef 1291 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1292 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1293 }
1294 } else if (INTEL_INFO(dev)->gen >= 7) {
1295 reg = SPRCTL(pipe);
19332d7a 1296 val = I915_READ(reg);
83f26f16 1297 WARN(val & SPRITE_ENABLE,
06da8da2 1298 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1299 plane_name(pipe), pipe_name(pipe));
1300 } else if (INTEL_INFO(dev)->gen >= 5) {
1301 reg = DVSCNTR(pipe);
19332d7a 1302 val = I915_READ(reg);
83f26f16 1303 WARN(val & DVS_ENABLE,
06da8da2 1304 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1305 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1306 }
1307}
1308
08c71e5e
VS
1309static void assert_vblank_disabled(struct drm_crtc *crtc)
1310{
1311 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1312 drm_crtc_vblank_put(crtc);
1313}
1314
89eff4be 1315static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1316{
1317 u32 val;
1318 bool enabled;
1319
89eff4be 1320 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1321
92f2584a
JB
1322 val = I915_READ(PCH_DREF_CONTROL);
1323 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1324 DREF_SUPERSPREAD_SOURCE_MASK));
1325 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1326}
1327
ab9412ba
DV
1328static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1329 enum pipe pipe)
92f2584a
JB
1330{
1331 int reg;
1332 u32 val;
1333 bool enabled;
1334
ab9412ba 1335 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1336 val = I915_READ(reg);
1337 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1338 WARN(enabled,
1339 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1340 pipe_name(pipe));
92f2584a
JB
1341}
1342
4e634389
KP
1343static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1344 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1345{
1346 if ((val & DP_PORT_EN) == 0)
1347 return false;
1348
1349 if (HAS_PCH_CPT(dev_priv->dev)) {
1350 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1351 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1352 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1353 return false;
44f37d1f
CML
1354 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1355 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1356 return false;
f0575e92
KP
1357 } else {
1358 if ((val & DP_PIPE_MASK) != (pipe << 30))
1359 return false;
1360 }
1361 return true;
1362}
1363
1519b995
KP
1364static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1365 enum pipe pipe, u32 val)
1366{
dc0fa718 1367 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1368 return false;
1369
1370 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1371 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1372 return false;
44f37d1f
CML
1373 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1374 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1375 return false;
1519b995 1376 } else {
dc0fa718 1377 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1378 return false;
1379 }
1380 return true;
1381}
1382
1383static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1384 enum pipe pipe, u32 val)
1385{
1386 if ((val & LVDS_PORT_EN) == 0)
1387 return false;
1388
1389 if (HAS_PCH_CPT(dev_priv->dev)) {
1390 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1391 return false;
1392 } else {
1393 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1394 return false;
1395 }
1396 return true;
1397}
1398
1399static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe, u32 val)
1401{
1402 if ((val & ADPA_DAC_ENABLE) == 0)
1403 return false;
1404 if (HAS_PCH_CPT(dev_priv->dev)) {
1405 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1406 return false;
1407 } else {
1408 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1409 return false;
1410 }
1411 return true;
1412}
1413
291906f1 1414static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1415 enum pipe pipe, int reg, u32 port_sel)
291906f1 1416{
47a05eca 1417 u32 val = I915_READ(reg);
4e634389 1418 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1419 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1420 reg, pipe_name(pipe));
de9a35ab 1421
75c5da27
DV
1422 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1423 && (val & DP_PIPEB_SELECT),
de9a35ab 1424 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1425}
1426
1427static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, int reg)
1429{
47a05eca 1430 u32 val = I915_READ(reg);
b70ad586 1431 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1432 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1433 reg, pipe_name(pipe));
de9a35ab 1434
dc0fa718 1435 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1436 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1437 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1438}
1439
1440static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1441 enum pipe pipe)
1442{
1443 int reg;
1444 u32 val;
291906f1 1445
f0575e92
KP
1446 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1447 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1448 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1449
1450 reg = PCH_ADPA;
1451 val = I915_READ(reg);
b70ad586 1452 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1453 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1454 pipe_name(pipe));
291906f1
JB
1455
1456 reg = PCH_LVDS;
1457 val = I915_READ(reg);
b70ad586 1458 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1459 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1460 pipe_name(pipe));
291906f1 1461
e2debe91
PZ
1462 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1463 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1464 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1465}
1466
40e9cf64
JB
1467static void intel_init_dpio(struct drm_device *dev)
1468{
1469 struct drm_i915_private *dev_priv = dev->dev_private;
1470
1471 if (!IS_VALLEYVIEW(dev))
1472 return;
1473
a09caddd
CML
1474 /*
1475 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1476 * CHV x1 PHY (DP/HDMI D)
1477 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1478 */
1479 if (IS_CHERRYVIEW(dev)) {
1480 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1481 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1482 } else {
1483 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1484 }
5382f5f3
JB
1485}
1486
426115cf 1487static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1488{
426115cf
DV
1489 struct drm_device *dev = crtc->base.dev;
1490 struct drm_i915_private *dev_priv = dev->dev_private;
1491 int reg = DPLL(crtc->pipe);
1492 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1493
426115cf 1494 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1495
1496 /* No really, not for ILK+ */
1497 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1498
1499 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1500 if (IS_MOBILE(dev_priv->dev))
426115cf 1501 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1502
426115cf
DV
1503 I915_WRITE(reg, dpll);
1504 POSTING_READ(reg);
1505 udelay(150);
1506
1507 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1508 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1509
1510 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1511 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1512
1513 /* We do this three times for luck */
426115cf 1514 I915_WRITE(reg, dpll);
87442f73
DV
1515 POSTING_READ(reg);
1516 udelay(150); /* wait for warmup */
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 */
1523}
1524
9d556c99
CML
1525static void chv_enable_pll(struct intel_crtc *crtc)
1526{
1527 struct drm_device *dev = crtc->base.dev;
1528 struct drm_i915_private *dev_priv = dev->dev_private;
1529 int pipe = crtc->pipe;
1530 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1531 u32 tmp;
1532
1533 assert_pipe_disabled(dev_priv, crtc->pipe);
1534
1535 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1536
1537 mutex_lock(&dev_priv->dpio_lock);
1538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
1544 /*
1545 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1546 */
1547 udelay(1);
1548
1549 /* Enable PLL */
a11b0703 1550 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1551
1552 /* Check PLL is locked */
a11b0703 1553 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1554 DRM_ERROR("PLL %d failed to lock\n", pipe);
1555
a11b0703
VS
1556 /* not sure when this should be written */
1557 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1558 POSTING_READ(DPLL_MD(pipe));
1559
9d556c99
CML
1560 mutex_unlock(&dev_priv->dpio_lock);
1561}
1562
1c4e0274
VS
1563static int intel_num_dvo_pipes(struct drm_device *dev)
1564{
1565 struct intel_crtc *crtc;
1566 int count = 0;
1567
1568 for_each_intel_crtc(dev, crtc)
1569 count += crtc->active &&
1570 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
1571
1572 return count;
1573}
1574
66e3d5c0 1575static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1576{
66e3d5c0
DV
1577 struct drm_device *dev = crtc->base.dev;
1578 struct drm_i915_private *dev_priv = dev->dev_private;
1579 int reg = DPLL(crtc->pipe);
1580 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1581
66e3d5c0 1582 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1583
63d7bbe9 1584 /* No really, not for ILK+ */
3d13ef2e 1585 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1586
1587 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1588 if (IS_MOBILE(dev) && !IS_I830(dev))
1589 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1590
1c4e0274
VS
1591 /* Enable DVO 2x clock on both PLLs if necessary */
1592 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1593 /*
1594 * It appears to be important that we don't enable this
1595 * for the current pipe before otherwise configuring the
1596 * PLL. No idea how this should be handled if multiple
1597 * DVO outputs are enabled simultaneosly.
1598 */
1599 dpll |= DPLL_DVO_2X_MODE;
1600 I915_WRITE(DPLL(!crtc->pipe),
1601 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1602 }
66e3d5c0
DV
1603
1604 /* Wait for the clocks to stabilize. */
1605 POSTING_READ(reg);
1606 udelay(150);
1607
1608 if (INTEL_INFO(dev)->gen >= 4) {
1609 I915_WRITE(DPLL_MD(crtc->pipe),
1610 crtc->config.dpll_hw_state.dpll_md);
1611 } else {
1612 /* The pixel multiplier can only be updated once the
1613 * DPLL is enabled and the clocks are stable.
1614 *
1615 * So write it again.
1616 */
1617 I915_WRITE(reg, dpll);
1618 }
63d7bbe9
JB
1619
1620 /* We do this three times for luck */
66e3d5c0 1621 I915_WRITE(reg, dpll);
63d7bbe9
JB
1622 POSTING_READ(reg);
1623 udelay(150); /* wait for warmup */
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 */
1630}
1631
1632/**
50b44a44 1633 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1634 * @dev_priv: i915 private structure
1635 * @pipe: pipe PLL to disable
1636 *
1637 * Disable the PLL for @pipe, making sure the pipe is off first.
1638 *
1639 * Note! This is for pre-ILK only.
1640 */
1c4e0274 1641static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1642{
1c4e0274
VS
1643 struct drm_device *dev = crtc->base.dev;
1644 struct drm_i915_private *dev_priv = dev->dev_private;
1645 enum pipe pipe = crtc->pipe;
1646
1647 /* Disable DVO 2x clock on both PLLs if necessary */
1648 if (IS_I830(dev) &&
1649 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
1650 intel_num_dvo_pipes(dev) == 1) {
1651 I915_WRITE(DPLL(PIPE_B),
1652 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1653 I915_WRITE(DPLL(PIPE_A),
1654 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1655 }
1656
b6b5d049
VS
1657 /* Don't disable pipe or pipe PLLs if needed */
1658 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1659 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1660 return;
1661
1662 /* Make sure the pipe isn't still relying on us */
1663 assert_pipe_disabled(dev_priv, pipe);
1664
50b44a44
DV
1665 I915_WRITE(DPLL(pipe), 0);
1666 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1667}
1668
f6071166
JB
1669static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1670{
1671 u32 val = 0;
1672
1673 /* Make sure the pipe isn't still relying on us */
1674 assert_pipe_disabled(dev_priv, pipe);
1675
e5cbfbfb
ID
1676 /*
1677 * Leave integrated clock source and reference clock enabled for pipe B.
1678 * The latter is needed for VGA hotplug / manual detection.
1679 */
f6071166 1680 if (pipe == PIPE_B)
e5cbfbfb 1681 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1682 I915_WRITE(DPLL(pipe), val);
1683 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1684
1685}
1686
1687static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1688{
d752048d 1689 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1690 u32 val;
1691
a11b0703
VS
1692 /* Make sure the pipe isn't still relying on us */
1693 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1694
a11b0703 1695 /* Set PLL en = 0 */
d17ec4ce 1696 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1697 if (pipe != PIPE_A)
1698 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1699 I915_WRITE(DPLL(pipe), val);
1700 POSTING_READ(DPLL(pipe));
d752048d
VS
1701
1702 mutex_lock(&dev_priv->dpio_lock);
1703
1704 /* Disable 10bit clock to display controller */
1705 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1706 val &= ~DPIO_DCLKP_EN;
1707 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1708
61407f6d
VS
1709 /* disable left/right clock distribution */
1710 if (pipe != PIPE_B) {
1711 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1712 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1713 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1714 } else {
1715 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1716 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1717 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1718 }
1719
d752048d 1720 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1721}
1722
e4607fcf
CML
1723void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1724 struct intel_digital_port *dport)
89b667f8
JB
1725{
1726 u32 port_mask;
00fc31b7 1727 int dpll_reg;
89b667f8 1728
e4607fcf
CML
1729 switch (dport->port) {
1730 case PORT_B:
89b667f8 1731 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1732 dpll_reg = DPLL(0);
e4607fcf
CML
1733 break;
1734 case PORT_C:
89b667f8 1735 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1736 dpll_reg = DPLL(0);
1737 break;
1738 case PORT_D:
1739 port_mask = DPLL_PORTD_READY_MASK;
1740 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1741 break;
1742 default:
1743 BUG();
1744 }
89b667f8 1745
00fc31b7 1746 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1747 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1748 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1749}
1750
b14b1055
DV
1751static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1752{
1753 struct drm_device *dev = crtc->base.dev;
1754 struct drm_i915_private *dev_priv = dev->dev_private;
1755 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1756
be19f0ff
CW
1757 if (WARN_ON(pll == NULL))
1758 return;
1759
b14b1055
DV
1760 WARN_ON(!pll->refcount);
1761 if (pll->active == 0) {
1762 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1763 WARN_ON(pll->on);
1764 assert_shared_dpll_disabled(dev_priv, pll);
1765
1766 pll->mode_set(dev_priv, pll);
1767 }
1768}
1769
92f2584a 1770/**
85b3894f 1771 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1772 * @dev_priv: i915 private structure
1773 * @pipe: pipe PLL to enable
1774 *
1775 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1776 * drives the transcoder clock.
1777 */
85b3894f 1778static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1779{
3d13ef2e
DL
1780 struct drm_device *dev = crtc->base.dev;
1781 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1782 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1783
87a875bb 1784 if (WARN_ON(pll == NULL))
48da64a8
CW
1785 return;
1786
1787 if (WARN_ON(pll->refcount == 0))
1788 return;
ee7b9f93 1789
74dd6928 1790 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1791 pll->name, pll->active, pll->on,
e2b78267 1792 crtc->base.base.id);
92f2584a 1793
cdbd2316
DV
1794 if (pll->active++) {
1795 WARN_ON(!pll->on);
e9d6944e 1796 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1797 return;
1798 }
f4a091c7 1799 WARN_ON(pll->on);
ee7b9f93 1800
bd2bb1b9
PZ
1801 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1802
46edb027 1803 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1804 pll->enable(dev_priv, pll);
ee7b9f93 1805 pll->on = true;
92f2584a
JB
1806}
1807
f6daaec2 1808static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1809{
3d13ef2e
DL
1810 struct drm_device *dev = crtc->base.dev;
1811 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1812 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1813
92f2584a 1814 /* PCH only available on ILK+ */
3d13ef2e 1815 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1816 if (WARN_ON(pll == NULL))
ee7b9f93 1817 return;
92f2584a 1818
48da64a8
CW
1819 if (WARN_ON(pll->refcount == 0))
1820 return;
7a419866 1821
46edb027
DV
1822 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1823 pll->name, pll->active, pll->on,
e2b78267 1824 crtc->base.base.id);
7a419866 1825
48da64a8 1826 if (WARN_ON(pll->active == 0)) {
e9d6944e 1827 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1828 return;
1829 }
1830
e9d6944e 1831 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1832 WARN_ON(!pll->on);
cdbd2316 1833 if (--pll->active)
7a419866 1834 return;
ee7b9f93 1835
46edb027 1836 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1837 pll->disable(dev_priv, pll);
ee7b9f93 1838 pll->on = false;
bd2bb1b9
PZ
1839
1840 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1841}
1842
b8a4f404
PZ
1843static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1844 enum pipe pipe)
040484af 1845{
23670b32 1846 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1847 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1849 uint32_t reg, val, pipeconf_val;
040484af
JB
1850
1851 /* PCH only available on ILK+ */
55522f37 1852 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1853
1854 /* Make sure PCH DPLL is enabled */
e72f9fbf 1855 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1856 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1857
1858 /* FDI must be feeding us bits for PCH ports */
1859 assert_fdi_tx_enabled(dev_priv, pipe);
1860 assert_fdi_rx_enabled(dev_priv, pipe);
1861
23670b32
DV
1862 if (HAS_PCH_CPT(dev)) {
1863 /* Workaround: Set the timing override bit before enabling the
1864 * pch transcoder. */
1865 reg = TRANS_CHICKEN2(pipe);
1866 val = I915_READ(reg);
1867 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1868 I915_WRITE(reg, val);
59c859d6 1869 }
23670b32 1870
ab9412ba 1871 reg = PCH_TRANSCONF(pipe);
040484af 1872 val = I915_READ(reg);
5f7f726d 1873 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1874
1875 if (HAS_PCH_IBX(dev_priv->dev)) {
1876 /*
1877 * make the BPC in transcoder be consistent with
1878 * that in pipeconf reg.
1879 */
dfd07d72
DV
1880 val &= ~PIPECONF_BPC_MASK;
1881 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1882 }
5f7f726d
PZ
1883
1884 val &= ~TRANS_INTERLACE_MASK;
1885 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1886 if (HAS_PCH_IBX(dev_priv->dev) &&
1887 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1888 val |= TRANS_LEGACY_INTERLACED_ILK;
1889 else
1890 val |= TRANS_INTERLACED;
5f7f726d
PZ
1891 else
1892 val |= TRANS_PROGRESSIVE;
1893
040484af
JB
1894 I915_WRITE(reg, val | TRANS_ENABLE);
1895 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1896 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1897}
1898
8fb033d7 1899static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1900 enum transcoder cpu_transcoder)
040484af 1901{
8fb033d7 1902 u32 val, pipeconf_val;
8fb033d7
PZ
1903
1904 /* PCH only available on ILK+ */
55522f37 1905 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1906
8fb033d7 1907 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1908 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1909 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1910
223a6fdf
PZ
1911 /* Workaround: set timing override bit. */
1912 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1913 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1914 I915_WRITE(_TRANSA_CHICKEN2, val);
1915
25f3ef11 1916 val = TRANS_ENABLE;
937bb610 1917 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1918
9a76b1c6
PZ
1919 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1920 PIPECONF_INTERLACED_ILK)
a35f2679 1921 val |= TRANS_INTERLACED;
8fb033d7
PZ
1922 else
1923 val |= TRANS_PROGRESSIVE;
1924
ab9412ba
DV
1925 I915_WRITE(LPT_TRANSCONF, val);
1926 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1927 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1928}
1929
b8a4f404
PZ
1930static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1931 enum pipe pipe)
040484af 1932{
23670b32
DV
1933 struct drm_device *dev = dev_priv->dev;
1934 uint32_t reg, val;
040484af
JB
1935
1936 /* FDI relies on the transcoder */
1937 assert_fdi_tx_disabled(dev_priv, pipe);
1938 assert_fdi_rx_disabled(dev_priv, pipe);
1939
291906f1
JB
1940 /* Ports must be off as well */
1941 assert_pch_ports_disabled(dev_priv, pipe);
1942
ab9412ba 1943 reg = PCH_TRANSCONF(pipe);
040484af
JB
1944 val = I915_READ(reg);
1945 val &= ~TRANS_ENABLE;
1946 I915_WRITE(reg, val);
1947 /* wait for PCH transcoder off, transcoder state */
1948 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1949 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1950
1951 if (!HAS_PCH_IBX(dev)) {
1952 /* Workaround: Clear the timing override chicken bit again. */
1953 reg = TRANS_CHICKEN2(pipe);
1954 val = I915_READ(reg);
1955 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1956 I915_WRITE(reg, val);
1957 }
040484af
JB
1958}
1959
ab4d966c 1960static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1961{
8fb033d7
PZ
1962 u32 val;
1963
ab9412ba 1964 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1965 val &= ~TRANS_ENABLE;
ab9412ba 1966 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1967 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1968 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1969 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1970
1971 /* Workaround: clear timing override bit. */
1972 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1973 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1974 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1975}
1976
b24e7179 1977/**
309cfea8 1978 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1979 * @crtc: crtc responsible for the pipe
b24e7179 1980 *
0372264a 1981 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1982 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1983 */
e1fdc473 1984static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1985{
0372264a
PZ
1986 struct drm_device *dev = crtc->base.dev;
1987 struct drm_i915_private *dev_priv = dev->dev_private;
1988 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1989 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1990 pipe);
1a240d4d 1991 enum pipe pch_transcoder;
b24e7179
JB
1992 int reg;
1993 u32 val;
1994
58c6eaa2 1995 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1996 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1997 assert_sprites_disabled(dev_priv, pipe);
1998
681e5811 1999 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2000 pch_transcoder = TRANSCODER_A;
2001 else
2002 pch_transcoder = pipe;
2003
b24e7179
JB
2004 /*
2005 * A pipe without a PLL won't actually be able to drive bits from
2006 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2007 * need the check.
2008 */
2009 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2010 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2011 assert_dsi_pll_enabled(dev_priv);
2012 else
2013 assert_pll_enabled(dev_priv, pipe);
040484af 2014 else {
30421c4f 2015 if (crtc->config.has_pch_encoder) {
040484af 2016 /* if driving the PCH, we need FDI enabled */
cc391bbb 2017 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2018 assert_fdi_tx_pll_enabled(dev_priv,
2019 (enum pipe) cpu_transcoder);
040484af
JB
2020 }
2021 /* FIXME: assert CPU port conditions for SNB+ */
2022 }
b24e7179 2023
702e7a56 2024 reg = PIPECONF(cpu_transcoder);
b24e7179 2025 val = I915_READ(reg);
7ad25d48 2026 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2027 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2028 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2029 return;
7ad25d48 2030 }
00d70b15
CW
2031
2032 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2033 POSTING_READ(reg);
b24e7179
JB
2034}
2035
2036/**
309cfea8 2037 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2038 * @crtc: crtc whose pipes is to be disabled
b24e7179 2039 *
575f7ab7
VS
2040 * Disable the pipe of @crtc, making sure that various hardware
2041 * specific requirements are met, if applicable, e.g. plane
2042 * disabled, panel fitter off, etc.
b24e7179
JB
2043 *
2044 * Will wait until the pipe has shut down before returning.
2045 */
575f7ab7 2046static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2047{
575f7ab7
VS
2048 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2049 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2050 enum pipe pipe = crtc->pipe;
b24e7179
JB
2051 int reg;
2052 u32 val;
2053
2054 /*
2055 * Make sure planes won't keep trying to pump pixels to us,
2056 * or we might hang the display.
2057 */
2058 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2059 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2060 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2061
702e7a56 2062 reg = PIPECONF(cpu_transcoder);
b24e7179 2063 val = I915_READ(reg);
00d70b15
CW
2064 if ((val & PIPECONF_ENABLE) == 0)
2065 return;
2066
67adc644
VS
2067 /*
2068 * Double wide has implications for planes
2069 * so best keep it disabled when not needed.
2070 */
2071 if (crtc->config.double_wide)
2072 val &= ~PIPECONF_DOUBLE_WIDE;
2073
2074 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2075 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2076 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2077 val &= ~PIPECONF_ENABLE;
2078
2079 I915_WRITE(reg, val);
2080 if ((val & PIPECONF_ENABLE) == 0)
2081 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2082}
2083
d74362c9
KP
2084/*
2085 * Plane regs are double buffered, going from enabled->disabled needs a
2086 * trigger in order to latch. The display address reg provides this.
2087 */
1dba99f4
VS
2088void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2089 enum plane plane)
d74362c9 2090{
3d13ef2e
DL
2091 struct drm_device *dev = dev_priv->dev;
2092 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2093
2094 I915_WRITE(reg, I915_READ(reg));
2095 POSTING_READ(reg);
d74362c9
KP
2096}
2097
b24e7179 2098/**
262ca2b0 2099 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2100 * @plane: plane to be enabled
2101 * @crtc: crtc for the plane
b24e7179 2102 *
fdd508a6 2103 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2104 */
fdd508a6
VS
2105static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2106 struct drm_crtc *crtc)
b24e7179 2107{
fdd508a6
VS
2108 struct drm_device *dev = plane->dev;
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2111
2112 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2113 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2114
98ec7739
VS
2115 if (intel_crtc->primary_enabled)
2116 return;
0037f71c 2117
4c445e0e 2118 intel_crtc->primary_enabled = true;
939c2fe8 2119
fdd508a6
VS
2120 dev_priv->display.update_primary_plane(crtc, plane->fb,
2121 crtc->x, crtc->y);
33c3b0d1
VS
2122
2123 /*
2124 * BDW signals flip done immediately if the plane
2125 * is disabled, even if the plane enable is already
2126 * armed to occur at the next vblank :(
2127 */
2128 if (IS_BROADWELL(dev))
2129 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2130}
2131
b24e7179 2132/**
262ca2b0 2133 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2134 * @plane: plane to be disabled
2135 * @crtc: crtc for the plane
b24e7179 2136 *
fdd508a6 2137 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2138 */
fdd508a6
VS
2139static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2140 struct drm_crtc *crtc)
b24e7179 2141{
fdd508a6
VS
2142 struct drm_device *dev = plane->dev;
2143 struct drm_i915_private *dev_priv = dev->dev_private;
2144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2145
2146 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2147
98ec7739
VS
2148 if (!intel_crtc->primary_enabled)
2149 return;
0037f71c 2150
4c445e0e 2151 intel_crtc->primary_enabled = false;
939c2fe8 2152
fdd508a6
VS
2153 dev_priv->display.update_primary_plane(crtc, plane->fb,
2154 crtc->x, crtc->y);
b24e7179
JB
2155}
2156
693db184
CW
2157static bool need_vtd_wa(struct drm_device *dev)
2158{
2159#ifdef CONFIG_INTEL_IOMMU
2160 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2161 return true;
2162#endif
2163 return false;
2164}
2165
a57ce0b2
JB
2166static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2167{
2168 int tile_height;
2169
2170 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2171 return ALIGN(height, tile_height);
2172}
2173
127bd2ac 2174int
48b956c5 2175intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2176 struct drm_i915_gem_object *obj,
a4872ba6 2177 struct intel_engine_cs *pipelined)
6b95a207 2178{
ce453d81 2179 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2180 u32 alignment;
2181 int ret;
2182
ebcdd39e
MR
2183 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2184
05394f39 2185 switch (obj->tiling_mode) {
6b95a207 2186 case I915_TILING_NONE:
1fada4cc
DL
2187 if (INTEL_INFO(dev)->gen >= 9)
2188 alignment = 256 * 1024;
2189 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2190 alignment = 128 * 1024;
a6c45cf0 2191 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2192 alignment = 4 * 1024;
2193 else
2194 alignment = 64 * 1024;
6b95a207
KH
2195 break;
2196 case I915_TILING_X:
1fada4cc
DL
2197 if (INTEL_INFO(dev)->gen >= 9)
2198 alignment = 256 * 1024;
2199 else {
2200 /* pin() will align the object as required by fence */
2201 alignment = 0;
2202 }
6b95a207
KH
2203 break;
2204 case I915_TILING_Y:
80075d49 2205 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2206 return -EINVAL;
2207 default:
2208 BUG();
2209 }
2210
693db184
CW
2211 /* Note that the w/a also requires 64 PTE of padding following the
2212 * bo. We currently fill all unused PTE with the shadow page and so
2213 * we should always have valid PTE following the scanout preventing
2214 * the VT-d warning.
2215 */
2216 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2217 alignment = 256 * 1024;
2218
d6dd6843
PZ
2219 /*
2220 * Global gtt pte registers are special registers which actually forward
2221 * writes to a chunk of system memory. Which means that there is no risk
2222 * that the register values disappear as soon as we call
2223 * intel_runtime_pm_put(), so it is correct to wrap only the
2224 * pin/unpin/fence and not more.
2225 */
2226 intel_runtime_pm_get(dev_priv);
2227
ce453d81 2228 dev_priv->mm.interruptible = false;
2da3b9b9 2229 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2230 if (ret)
ce453d81 2231 goto err_interruptible;
6b95a207
KH
2232
2233 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2234 * fence, whereas 965+ only requires a fence if using
2235 * framebuffer compression. For simplicity, we always install
2236 * a fence as the cost is not that onerous.
2237 */
06d98131 2238 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2239 if (ret)
2240 goto err_unpin;
1690e1eb 2241
9a5a53b3 2242 i915_gem_object_pin_fence(obj);
6b95a207 2243
ce453d81 2244 dev_priv->mm.interruptible = true;
d6dd6843 2245 intel_runtime_pm_put(dev_priv);
6b95a207 2246 return 0;
48b956c5
CW
2247
2248err_unpin:
cc98b413 2249 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2250err_interruptible:
2251 dev_priv->mm.interruptible = true;
d6dd6843 2252 intel_runtime_pm_put(dev_priv);
48b956c5 2253 return ret;
6b95a207
KH
2254}
2255
1690e1eb
CW
2256void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2257{
ebcdd39e
MR
2258 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2259
1690e1eb 2260 i915_gem_object_unpin_fence(obj);
cc98b413 2261 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2262}
2263
c2c75131
DV
2264/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2265 * is assumed to be a power-of-two. */
bc752862
CW
2266unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2267 unsigned int tiling_mode,
2268 unsigned int cpp,
2269 unsigned int pitch)
c2c75131 2270{
bc752862
CW
2271 if (tiling_mode != I915_TILING_NONE) {
2272 unsigned int tile_rows, tiles;
c2c75131 2273
bc752862
CW
2274 tile_rows = *y / 8;
2275 *y %= 8;
c2c75131 2276
bc752862
CW
2277 tiles = *x / (512/cpp);
2278 *x %= 512/cpp;
2279
2280 return tile_rows * pitch * 8 + tiles * 4096;
2281 } else {
2282 unsigned int offset;
2283
2284 offset = *y * pitch + *x * cpp;
2285 *y = 0;
2286 *x = (offset & 4095) / cpp;
2287 return offset & -4096;
2288 }
c2c75131
DV
2289}
2290
46f297fb
JB
2291int intel_format_to_fourcc(int format)
2292{
2293 switch (format) {
2294 case DISPPLANE_8BPP:
2295 return DRM_FORMAT_C8;
2296 case DISPPLANE_BGRX555:
2297 return DRM_FORMAT_XRGB1555;
2298 case DISPPLANE_BGRX565:
2299 return DRM_FORMAT_RGB565;
2300 default:
2301 case DISPPLANE_BGRX888:
2302 return DRM_FORMAT_XRGB8888;
2303 case DISPPLANE_RGBX888:
2304 return DRM_FORMAT_XBGR8888;
2305 case DISPPLANE_BGRX101010:
2306 return DRM_FORMAT_XRGB2101010;
2307 case DISPPLANE_RGBX101010:
2308 return DRM_FORMAT_XBGR2101010;
2309 }
2310}
2311
484b41dd 2312static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2313 struct intel_plane_config *plane_config)
2314{
2315 struct drm_device *dev = crtc->base.dev;
2316 struct drm_i915_gem_object *obj = NULL;
2317 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2318 u32 base = plane_config->base;
2319
ff2652ea
CW
2320 if (plane_config->size == 0)
2321 return false;
2322
46f297fb
JB
2323 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2324 plane_config->size);
2325 if (!obj)
484b41dd 2326 return false;
46f297fb
JB
2327
2328 if (plane_config->tiled) {
2329 obj->tiling_mode = I915_TILING_X;
66e514c1 2330 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2331 }
2332
66e514c1
DA
2333 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2334 mode_cmd.width = crtc->base.primary->fb->width;
2335 mode_cmd.height = crtc->base.primary->fb->height;
2336 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2337
2338 mutex_lock(&dev->struct_mutex);
2339
66e514c1 2340 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2341 &mode_cmd, obj)) {
46f297fb
JB
2342 DRM_DEBUG_KMS("intel fb init failed\n");
2343 goto out_unref_obj;
2344 }
2345
a071fa00 2346 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2347 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2348
2349 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2350 return true;
46f297fb
JB
2351
2352out_unref_obj:
2353 drm_gem_object_unreference(&obj->base);
2354 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2355 return false;
2356}
2357
2358static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2359 struct intel_plane_config *plane_config)
2360{
2361 struct drm_device *dev = intel_crtc->base.dev;
2362 struct drm_crtc *c;
2363 struct intel_crtc *i;
2ff8fde1 2364 struct drm_i915_gem_object *obj;
484b41dd 2365
66e514c1 2366 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2367 return;
2368
2369 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2370 return;
2371
66e514c1
DA
2372 kfree(intel_crtc->base.primary->fb);
2373 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2374
2375 /*
2376 * Failed to alloc the obj, check to see if we should share
2377 * an fb with another CRTC instead
2378 */
70e1e0ec 2379 for_each_crtc(dev, c) {
484b41dd
JB
2380 i = to_intel_crtc(c);
2381
2382 if (c == &intel_crtc->base)
2383 continue;
2384
2ff8fde1
MR
2385 if (!i->active)
2386 continue;
2387
2388 obj = intel_fb_obj(c->primary->fb);
2389 if (obj == NULL)
484b41dd
JB
2390 continue;
2391
2ff8fde1 2392 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2393 drm_framebuffer_reference(c->primary->fb);
2394 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2395 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2396 break;
2397 }
2398 }
46f297fb
JB
2399}
2400
29b9bde6
DV
2401static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2402 struct drm_framebuffer *fb,
2403 int x, int y)
81255565
JB
2404{
2405 struct drm_device *dev = crtc->dev;
2406 struct drm_i915_private *dev_priv = dev->dev_private;
2407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2408 struct drm_i915_gem_object *obj;
81255565 2409 int plane = intel_crtc->plane;
e506a0c6 2410 unsigned long linear_offset;
81255565 2411 u32 dspcntr;
f45651ba 2412 u32 reg = DSPCNTR(plane);
48404c1e 2413 int pixel_size;
f45651ba 2414
fdd508a6
VS
2415 if (!intel_crtc->primary_enabled) {
2416 I915_WRITE(reg, 0);
2417 if (INTEL_INFO(dev)->gen >= 4)
2418 I915_WRITE(DSPSURF(plane), 0);
2419 else
2420 I915_WRITE(DSPADDR(plane), 0);
2421 POSTING_READ(reg);
2422 return;
2423 }
2424
c9ba6fad
VS
2425 obj = intel_fb_obj(fb);
2426 if (WARN_ON(obj == NULL))
2427 return;
2428
2429 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2430
f45651ba
VS
2431 dspcntr = DISPPLANE_GAMMA_ENABLE;
2432
fdd508a6 2433 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2434
2435 if (INTEL_INFO(dev)->gen < 4) {
2436 if (intel_crtc->pipe == PIPE_B)
2437 dspcntr |= DISPPLANE_SEL_PIPE_B;
2438
2439 /* pipesrc and dspsize control the size that is scaled from,
2440 * which should always be the user's requested size.
2441 */
2442 I915_WRITE(DSPSIZE(plane),
2443 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2444 (intel_crtc->config.pipe_src_w - 1));
2445 I915_WRITE(DSPPOS(plane), 0);
2446 }
81255565 2447
57779d06
VS
2448 switch (fb->pixel_format) {
2449 case DRM_FORMAT_C8:
81255565
JB
2450 dspcntr |= DISPPLANE_8BPP;
2451 break;
57779d06
VS
2452 case DRM_FORMAT_XRGB1555:
2453 case DRM_FORMAT_ARGB1555:
2454 dspcntr |= DISPPLANE_BGRX555;
81255565 2455 break;
57779d06
VS
2456 case DRM_FORMAT_RGB565:
2457 dspcntr |= DISPPLANE_BGRX565;
2458 break;
2459 case DRM_FORMAT_XRGB8888:
2460 case DRM_FORMAT_ARGB8888:
2461 dspcntr |= DISPPLANE_BGRX888;
2462 break;
2463 case DRM_FORMAT_XBGR8888:
2464 case DRM_FORMAT_ABGR8888:
2465 dspcntr |= DISPPLANE_RGBX888;
2466 break;
2467 case DRM_FORMAT_XRGB2101010:
2468 case DRM_FORMAT_ARGB2101010:
2469 dspcntr |= DISPPLANE_BGRX101010;
2470 break;
2471 case DRM_FORMAT_XBGR2101010:
2472 case DRM_FORMAT_ABGR2101010:
2473 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2474 break;
2475 default:
baba133a 2476 BUG();
81255565 2477 }
57779d06 2478
f45651ba
VS
2479 if (INTEL_INFO(dev)->gen >= 4 &&
2480 obj->tiling_mode != I915_TILING_NONE)
2481 dspcntr |= DISPPLANE_TILED;
81255565 2482
de1aa629
VS
2483 if (IS_G4X(dev))
2484 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2485
b9897127 2486 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2487
c2c75131
DV
2488 if (INTEL_INFO(dev)->gen >= 4) {
2489 intel_crtc->dspaddr_offset =
bc752862 2490 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2491 pixel_size,
bc752862 2492 fb->pitches[0]);
c2c75131
DV
2493 linear_offset -= intel_crtc->dspaddr_offset;
2494 } else {
e506a0c6 2495 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2496 }
e506a0c6 2497
48404c1e
SJ
2498 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2499 dspcntr |= DISPPLANE_ROTATE_180;
2500
2501 x += (intel_crtc->config.pipe_src_w - 1);
2502 y += (intel_crtc->config.pipe_src_h - 1);
2503
2504 /* Finding the last pixel of the last line of the display
2505 data and adding to linear_offset*/
2506 linear_offset +=
2507 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2508 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2509 }
2510
2511 I915_WRITE(reg, dspcntr);
2512
f343c5f6
BW
2513 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2514 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2515 fb->pitches[0]);
01f2c773 2516 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2517 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2518 I915_WRITE(DSPSURF(plane),
2519 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2520 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2521 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2522 } else
f343c5f6 2523 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2524 POSTING_READ(reg);
17638cd6
JB
2525}
2526
29b9bde6
DV
2527static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2528 struct drm_framebuffer *fb,
2529 int x, int y)
17638cd6
JB
2530{
2531 struct drm_device *dev = crtc->dev;
2532 struct drm_i915_private *dev_priv = dev->dev_private;
2533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2534 struct drm_i915_gem_object *obj;
17638cd6 2535 int plane = intel_crtc->plane;
e506a0c6 2536 unsigned long linear_offset;
17638cd6 2537 u32 dspcntr;
f45651ba 2538 u32 reg = DSPCNTR(plane);
48404c1e 2539 int pixel_size;
f45651ba 2540
fdd508a6
VS
2541 if (!intel_crtc->primary_enabled) {
2542 I915_WRITE(reg, 0);
2543 I915_WRITE(DSPSURF(plane), 0);
2544 POSTING_READ(reg);
2545 return;
2546 }
2547
c9ba6fad
VS
2548 obj = intel_fb_obj(fb);
2549 if (WARN_ON(obj == NULL))
2550 return;
2551
2552 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2553
f45651ba
VS
2554 dspcntr = DISPPLANE_GAMMA_ENABLE;
2555
fdd508a6 2556 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2557
2558 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2559 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2560
57779d06
VS
2561 switch (fb->pixel_format) {
2562 case DRM_FORMAT_C8:
17638cd6
JB
2563 dspcntr |= DISPPLANE_8BPP;
2564 break;
57779d06
VS
2565 case DRM_FORMAT_RGB565:
2566 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2567 break;
57779d06
VS
2568 case DRM_FORMAT_XRGB8888:
2569 case DRM_FORMAT_ARGB8888:
2570 dspcntr |= DISPPLANE_BGRX888;
2571 break;
2572 case DRM_FORMAT_XBGR8888:
2573 case DRM_FORMAT_ABGR8888:
2574 dspcntr |= DISPPLANE_RGBX888;
2575 break;
2576 case DRM_FORMAT_XRGB2101010:
2577 case DRM_FORMAT_ARGB2101010:
2578 dspcntr |= DISPPLANE_BGRX101010;
2579 break;
2580 case DRM_FORMAT_XBGR2101010:
2581 case DRM_FORMAT_ABGR2101010:
2582 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2583 break;
2584 default:
baba133a 2585 BUG();
17638cd6
JB
2586 }
2587
2588 if (obj->tiling_mode != I915_TILING_NONE)
2589 dspcntr |= DISPPLANE_TILED;
17638cd6 2590
f45651ba 2591 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2592 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2593
b9897127 2594 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2595 intel_crtc->dspaddr_offset =
bc752862 2596 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2597 pixel_size,
bc752862 2598 fb->pitches[0]);
c2c75131 2599 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2600 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2601 dspcntr |= DISPPLANE_ROTATE_180;
2602
2603 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2604 x += (intel_crtc->config.pipe_src_w - 1);
2605 y += (intel_crtc->config.pipe_src_h - 1);
2606
2607 /* Finding the last pixel of the last line of the display
2608 data and adding to linear_offset*/
2609 linear_offset +=
2610 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2611 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2612 }
2613 }
2614
2615 I915_WRITE(reg, dspcntr);
17638cd6 2616
f343c5f6
BW
2617 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2618 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2619 fb->pitches[0]);
01f2c773 2620 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2621 I915_WRITE(DSPSURF(plane),
2622 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2623 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2624 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2625 } else {
2626 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2627 I915_WRITE(DSPLINOFF(plane), linear_offset);
2628 }
17638cd6 2629 POSTING_READ(reg);
17638cd6
JB
2630}
2631
70d21f0e
DL
2632static void skylake_update_primary_plane(struct drm_crtc *crtc,
2633 struct drm_framebuffer *fb,
2634 int x, int y)
2635{
2636 struct drm_device *dev = crtc->dev;
2637 struct drm_i915_private *dev_priv = dev->dev_private;
2638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2639 struct intel_framebuffer *intel_fb;
2640 struct drm_i915_gem_object *obj;
2641 int pipe = intel_crtc->pipe;
2642 u32 plane_ctl, stride;
2643
2644 if (!intel_crtc->primary_enabled) {
2645 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2646 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2647 POSTING_READ(PLANE_CTL(pipe, 0));
2648 return;
2649 }
2650
2651 plane_ctl = PLANE_CTL_ENABLE |
2652 PLANE_CTL_PIPE_GAMMA_ENABLE |
2653 PLANE_CTL_PIPE_CSC_ENABLE;
2654
2655 switch (fb->pixel_format) {
2656 case DRM_FORMAT_RGB565:
2657 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2658 break;
2659 case DRM_FORMAT_XRGB8888:
2660 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2661 break;
2662 case DRM_FORMAT_XBGR8888:
2663 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2664 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2665 break;
2666 case DRM_FORMAT_XRGB2101010:
2667 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2668 break;
2669 case DRM_FORMAT_XBGR2101010:
2670 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2671 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2672 break;
2673 default:
2674 BUG();
2675 }
2676
2677 intel_fb = to_intel_framebuffer(fb);
2678 obj = intel_fb->obj;
2679
2680 /*
2681 * The stride is either expressed as a multiple of 64 bytes chunks for
2682 * linear buffers or in number of tiles for tiled buffers.
2683 */
2684 switch (obj->tiling_mode) {
2685 case I915_TILING_NONE:
2686 stride = fb->pitches[0] >> 6;
2687 break;
2688 case I915_TILING_X:
2689 plane_ctl |= PLANE_CTL_TILED_X;
2690 stride = fb->pitches[0] >> 9;
2691 break;
2692 default:
2693 BUG();
2694 }
2695
2696 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
2697
2698 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2699
2700 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2701 i915_gem_obj_ggtt_offset(obj),
2702 x, y, fb->width, fb->height,
2703 fb->pitches[0]);
2704
2705 I915_WRITE(PLANE_POS(pipe, 0), 0);
2706 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2707 I915_WRITE(PLANE_SIZE(pipe, 0),
2708 (intel_crtc->config.pipe_src_h - 1) << 16 |
2709 (intel_crtc->config.pipe_src_w - 1));
2710 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2711 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2712
2713 POSTING_READ(PLANE_SURF(pipe, 0));
2714}
2715
17638cd6
JB
2716/* Assume fb object is pinned & idle & fenced and just update base pointers */
2717static int
2718intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2719 int x, int y, enum mode_set_atomic state)
2720{
2721 struct drm_device *dev = crtc->dev;
2722 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2723
6b8e6ed0
CW
2724 if (dev_priv->display.disable_fbc)
2725 dev_priv->display.disable_fbc(dev);
81255565 2726
29b9bde6
DV
2727 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2728
2729 return 0;
81255565
JB
2730}
2731
96a02917
VS
2732void intel_display_handle_reset(struct drm_device *dev)
2733{
2734 struct drm_i915_private *dev_priv = dev->dev_private;
2735 struct drm_crtc *crtc;
2736
2737 /*
2738 * Flips in the rings have been nuked by the reset,
2739 * so complete all pending flips so that user space
2740 * will get its events and not get stuck.
2741 *
2742 * Also update the base address of all primary
2743 * planes to the the last fb to make sure we're
2744 * showing the correct fb after a reset.
2745 *
2746 * Need to make two loops over the crtcs so that we
2747 * don't try to grab a crtc mutex before the
2748 * pending_flip_queue really got woken up.
2749 */
2750
70e1e0ec 2751 for_each_crtc(dev, crtc) {
96a02917
VS
2752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2753 enum plane plane = intel_crtc->plane;
2754
2755 intel_prepare_page_flip(dev, plane);
2756 intel_finish_page_flip_plane(dev, plane);
2757 }
2758
70e1e0ec 2759 for_each_crtc(dev, crtc) {
96a02917
VS
2760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2761
51fd371b 2762 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2763 /*
2764 * FIXME: Once we have proper support for primary planes (and
2765 * disabling them without disabling the entire crtc) allow again
66e514c1 2766 * a NULL crtc->primary->fb.
947fdaad 2767 */
f4510a27 2768 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2769 dev_priv->display.update_primary_plane(crtc,
66e514c1 2770 crtc->primary->fb,
262ca2b0
MR
2771 crtc->x,
2772 crtc->y);
51fd371b 2773 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2774 }
2775}
2776
14667a4b
CW
2777static int
2778intel_finish_fb(struct drm_framebuffer *old_fb)
2779{
2ff8fde1 2780 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2781 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2782 bool was_interruptible = dev_priv->mm.interruptible;
2783 int ret;
2784
14667a4b
CW
2785 /* Big Hammer, we also need to ensure that any pending
2786 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2787 * current scanout is retired before unpinning the old
2788 * framebuffer.
2789 *
2790 * This should only fail upon a hung GPU, in which case we
2791 * can safely continue.
2792 */
2793 dev_priv->mm.interruptible = false;
2794 ret = i915_gem_object_finish_gpu(obj);
2795 dev_priv->mm.interruptible = was_interruptible;
2796
2797 return ret;
2798}
2799
7d5e3799
CW
2800static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2801{
2802 struct drm_device *dev = crtc->dev;
2803 struct drm_i915_private *dev_priv = dev->dev_private;
2804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
2805 bool pending;
2806
2807 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2808 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2809 return false;
2810
5e2d7afc 2811 spin_lock_irq(&dev->event_lock);
7d5e3799 2812 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 2813 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
2814
2815 return pending;
2816}
2817
e30e8f75
GP
2818static void intel_update_pipe_size(struct intel_crtc *crtc)
2819{
2820 struct drm_device *dev = crtc->base.dev;
2821 struct drm_i915_private *dev_priv = dev->dev_private;
2822 const struct drm_display_mode *adjusted_mode;
2823
2824 if (!i915.fastboot)
2825 return;
2826
2827 /*
2828 * Update pipe size and adjust fitter if needed: the reason for this is
2829 * that in compute_mode_changes we check the native mode (not the pfit
2830 * mode) to see if we can flip rather than do a full mode set. In the
2831 * fastboot case, we'll flip, but if we don't update the pipesrc and
2832 * pfit state, we'll end up with a big fb scanned out into the wrong
2833 * sized surface.
2834 *
2835 * To fix this properly, we need to hoist the checks up into
2836 * compute_mode_changes (or above), check the actual pfit state and
2837 * whether the platform allows pfit disable with pipe active, and only
2838 * then update the pipesrc and pfit state, even on the flip path.
2839 */
2840
2841 adjusted_mode = &crtc->config.adjusted_mode;
2842
2843 I915_WRITE(PIPESRC(crtc->pipe),
2844 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2845 (adjusted_mode->crtc_vdisplay - 1));
2846 if (!crtc->config.pch_pfit.enabled &&
2847 (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) ||
2848 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))) {
2849 I915_WRITE(PF_CTL(crtc->pipe), 0);
2850 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2851 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2852 }
2853 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2854 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2855}
2856
5c3b82e2 2857static int
3c4fdcfb 2858intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2859 struct drm_framebuffer *fb)
79e53945
JB
2860{
2861 struct drm_device *dev = crtc->dev;
6b8e6ed0 2862 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2864 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2865 struct drm_framebuffer *old_fb = crtc->primary->fb;
2866 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2867 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2868 int ret;
79e53945 2869
7d5e3799
CW
2870 if (intel_crtc_has_pending_flip(crtc)) {
2871 DRM_ERROR("pipe is still busy with an old pageflip\n");
2872 return -EBUSY;
2873 }
2874
79e53945 2875 /* no fb bound */
94352cf9 2876 if (!fb) {
a5071c2f 2877 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2878 return 0;
2879 }
2880
7eb552ae 2881 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2882 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2883 plane_name(intel_crtc->plane),
2884 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2885 return -EINVAL;
79e53945
JB
2886 }
2887
5c3b82e2 2888 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2889 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2890 if (ret == 0)
91565c85 2891 i915_gem_track_fb(old_obj, obj,
a071fa00 2892 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2893 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2894 if (ret != 0) {
a5071c2f 2895 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2896 return ret;
2897 }
79e53945 2898
e30e8f75 2899 intel_update_pipe_size(intel_crtc);
4d6a3e63 2900
29b9bde6 2901 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2902
f99d7069
DV
2903 if (intel_crtc->active)
2904 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2905
f4510a27 2906 crtc->primary->fb = fb;
6c4c86f5
DV
2907 crtc->x = x;
2908 crtc->y = y;
94352cf9 2909
b7f1de28 2910 if (old_fb) {
d7697eea
DV
2911 if (intel_crtc->active && old_fb != fb)
2912 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2913 mutex_lock(&dev->struct_mutex);
2ff8fde1 2914 intel_unpin_fb_obj(old_obj);
8ac36ec1 2915 mutex_unlock(&dev->struct_mutex);
b7f1de28 2916 }
652c393a 2917
8ac36ec1 2918 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2919 intel_update_fbc(dev);
5c3b82e2 2920 mutex_unlock(&dev->struct_mutex);
79e53945 2921
5c3b82e2 2922 return 0;
79e53945
JB
2923}
2924
5e84e1a4
ZW
2925static void intel_fdi_normal_train(struct drm_crtc *crtc)
2926{
2927 struct drm_device *dev = crtc->dev;
2928 struct drm_i915_private *dev_priv = dev->dev_private;
2929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2930 int pipe = intel_crtc->pipe;
2931 u32 reg, temp;
2932
2933 /* enable normal train */
2934 reg = FDI_TX_CTL(pipe);
2935 temp = I915_READ(reg);
61e499bf 2936 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2937 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2938 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2939 } else {
2940 temp &= ~FDI_LINK_TRAIN_NONE;
2941 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2942 }
5e84e1a4
ZW
2943 I915_WRITE(reg, temp);
2944
2945 reg = FDI_RX_CTL(pipe);
2946 temp = I915_READ(reg);
2947 if (HAS_PCH_CPT(dev)) {
2948 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2949 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2950 } else {
2951 temp &= ~FDI_LINK_TRAIN_NONE;
2952 temp |= FDI_LINK_TRAIN_NONE;
2953 }
2954 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2955
2956 /* wait one idle pattern time */
2957 POSTING_READ(reg);
2958 udelay(1000);
357555c0
JB
2959
2960 /* IVB wants error correction enabled */
2961 if (IS_IVYBRIDGE(dev))
2962 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2963 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2964}
2965
1fbc0d78 2966static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2967{
1fbc0d78
DV
2968 return crtc->base.enabled && crtc->active &&
2969 crtc->config.has_pch_encoder;
1e833f40
DV
2970}
2971
01a415fd
DV
2972static void ivb_modeset_global_resources(struct drm_device *dev)
2973{
2974 struct drm_i915_private *dev_priv = dev->dev_private;
2975 struct intel_crtc *pipe_B_crtc =
2976 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2977 struct intel_crtc *pipe_C_crtc =
2978 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2979 uint32_t temp;
2980
1e833f40
DV
2981 /*
2982 * When everything is off disable fdi C so that we could enable fdi B
2983 * with all lanes. Note that we don't care about enabled pipes without
2984 * an enabled pch encoder.
2985 */
2986 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2987 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2988 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2989 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2990
2991 temp = I915_READ(SOUTH_CHICKEN1);
2992 temp &= ~FDI_BC_BIFURCATION_SELECT;
2993 DRM_DEBUG_KMS("disabling fdi C rx\n");
2994 I915_WRITE(SOUTH_CHICKEN1, temp);
2995 }
2996}
2997
8db9d77b
ZW
2998/* The FDI link training functions for ILK/Ibexpeak. */
2999static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3000{
3001 struct drm_device *dev = crtc->dev;
3002 struct drm_i915_private *dev_priv = dev->dev_private;
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3004 int pipe = intel_crtc->pipe;
5eddb70b 3005 u32 reg, temp, tries;
8db9d77b 3006
1c8562f6 3007 /* FDI needs bits from pipe first */
0fc932b8 3008 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3009
e1a44743
AJ
3010 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3011 for train result */
5eddb70b
CW
3012 reg = FDI_RX_IMR(pipe);
3013 temp = I915_READ(reg);
e1a44743
AJ
3014 temp &= ~FDI_RX_SYMBOL_LOCK;
3015 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3016 I915_WRITE(reg, temp);
3017 I915_READ(reg);
e1a44743
AJ
3018 udelay(150);
3019
8db9d77b 3020 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3021 reg = FDI_TX_CTL(pipe);
3022 temp = I915_READ(reg);
627eb5a3
DV
3023 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3024 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3025 temp &= ~FDI_LINK_TRAIN_NONE;
3026 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3027 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3028
5eddb70b
CW
3029 reg = FDI_RX_CTL(pipe);
3030 temp = I915_READ(reg);
8db9d77b
ZW
3031 temp &= ~FDI_LINK_TRAIN_NONE;
3032 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3033 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3034
3035 POSTING_READ(reg);
8db9d77b
ZW
3036 udelay(150);
3037
5b2adf89 3038 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3039 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3040 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3041 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3042
5eddb70b 3043 reg = FDI_RX_IIR(pipe);
e1a44743 3044 for (tries = 0; tries < 5; tries++) {
5eddb70b 3045 temp = I915_READ(reg);
8db9d77b
ZW
3046 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3047
3048 if ((temp & FDI_RX_BIT_LOCK)) {
3049 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3050 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3051 break;
3052 }
8db9d77b 3053 }
e1a44743 3054 if (tries == 5)
5eddb70b 3055 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3056
3057 /* Train 2 */
5eddb70b
CW
3058 reg = FDI_TX_CTL(pipe);
3059 temp = I915_READ(reg);
8db9d77b
ZW
3060 temp &= ~FDI_LINK_TRAIN_NONE;
3061 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3062 I915_WRITE(reg, temp);
8db9d77b 3063
5eddb70b
CW
3064 reg = FDI_RX_CTL(pipe);
3065 temp = I915_READ(reg);
8db9d77b
ZW
3066 temp &= ~FDI_LINK_TRAIN_NONE;
3067 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3068 I915_WRITE(reg, temp);
8db9d77b 3069
5eddb70b
CW
3070 POSTING_READ(reg);
3071 udelay(150);
8db9d77b 3072
5eddb70b 3073 reg = FDI_RX_IIR(pipe);
e1a44743 3074 for (tries = 0; tries < 5; tries++) {
5eddb70b 3075 temp = I915_READ(reg);
8db9d77b
ZW
3076 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3077
3078 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3079 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3080 DRM_DEBUG_KMS("FDI train 2 done.\n");
3081 break;
3082 }
8db9d77b 3083 }
e1a44743 3084 if (tries == 5)
5eddb70b 3085 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3086
3087 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3088
8db9d77b
ZW
3089}
3090
0206e353 3091static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3092 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3093 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3094 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3095 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3096};
3097
3098/* The FDI link training functions for SNB/Cougarpoint. */
3099static void gen6_fdi_link_train(struct drm_crtc *crtc)
3100{
3101 struct drm_device *dev = crtc->dev;
3102 struct drm_i915_private *dev_priv = dev->dev_private;
3103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3104 int pipe = intel_crtc->pipe;
fa37d39e 3105 u32 reg, temp, i, retry;
8db9d77b 3106
e1a44743
AJ
3107 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3108 for train result */
5eddb70b
CW
3109 reg = FDI_RX_IMR(pipe);
3110 temp = I915_READ(reg);
e1a44743
AJ
3111 temp &= ~FDI_RX_SYMBOL_LOCK;
3112 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3113 I915_WRITE(reg, temp);
3114
3115 POSTING_READ(reg);
e1a44743
AJ
3116 udelay(150);
3117
8db9d77b 3118 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3119 reg = FDI_TX_CTL(pipe);
3120 temp = I915_READ(reg);
627eb5a3
DV
3121 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3122 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3123 temp &= ~FDI_LINK_TRAIN_NONE;
3124 temp |= FDI_LINK_TRAIN_PATTERN_1;
3125 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3126 /* SNB-B */
3127 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3128 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3129
d74cf324
DV
3130 I915_WRITE(FDI_RX_MISC(pipe),
3131 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3132
5eddb70b
CW
3133 reg = FDI_RX_CTL(pipe);
3134 temp = I915_READ(reg);
8db9d77b
ZW
3135 if (HAS_PCH_CPT(dev)) {
3136 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3137 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3138 } else {
3139 temp &= ~FDI_LINK_TRAIN_NONE;
3140 temp |= FDI_LINK_TRAIN_PATTERN_1;
3141 }
5eddb70b
CW
3142 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3143
3144 POSTING_READ(reg);
8db9d77b
ZW
3145 udelay(150);
3146
0206e353 3147 for (i = 0; i < 4; i++) {
5eddb70b
CW
3148 reg = FDI_TX_CTL(pipe);
3149 temp = I915_READ(reg);
8db9d77b
ZW
3150 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3151 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3152 I915_WRITE(reg, temp);
3153
3154 POSTING_READ(reg);
8db9d77b
ZW
3155 udelay(500);
3156
fa37d39e
SP
3157 for (retry = 0; retry < 5; retry++) {
3158 reg = FDI_RX_IIR(pipe);
3159 temp = I915_READ(reg);
3160 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3161 if (temp & FDI_RX_BIT_LOCK) {
3162 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3163 DRM_DEBUG_KMS("FDI train 1 done.\n");
3164 break;
3165 }
3166 udelay(50);
8db9d77b 3167 }
fa37d39e
SP
3168 if (retry < 5)
3169 break;
8db9d77b
ZW
3170 }
3171 if (i == 4)
5eddb70b 3172 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3173
3174 /* Train 2 */
5eddb70b
CW
3175 reg = FDI_TX_CTL(pipe);
3176 temp = I915_READ(reg);
8db9d77b
ZW
3177 temp &= ~FDI_LINK_TRAIN_NONE;
3178 temp |= FDI_LINK_TRAIN_PATTERN_2;
3179 if (IS_GEN6(dev)) {
3180 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3181 /* SNB-B */
3182 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3183 }
5eddb70b 3184 I915_WRITE(reg, temp);
8db9d77b 3185
5eddb70b
CW
3186 reg = FDI_RX_CTL(pipe);
3187 temp = I915_READ(reg);
8db9d77b
ZW
3188 if (HAS_PCH_CPT(dev)) {
3189 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3190 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3191 } else {
3192 temp &= ~FDI_LINK_TRAIN_NONE;
3193 temp |= FDI_LINK_TRAIN_PATTERN_2;
3194 }
5eddb70b
CW
3195 I915_WRITE(reg, temp);
3196
3197 POSTING_READ(reg);
8db9d77b
ZW
3198 udelay(150);
3199
0206e353 3200 for (i = 0; i < 4; i++) {
5eddb70b
CW
3201 reg = FDI_TX_CTL(pipe);
3202 temp = I915_READ(reg);
8db9d77b
ZW
3203 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3204 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3205 I915_WRITE(reg, temp);
3206
3207 POSTING_READ(reg);
8db9d77b
ZW
3208 udelay(500);
3209
fa37d39e
SP
3210 for (retry = 0; retry < 5; retry++) {
3211 reg = FDI_RX_IIR(pipe);
3212 temp = I915_READ(reg);
3213 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3214 if (temp & FDI_RX_SYMBOL_LOCK) {
3215 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3216 DRM_DEBUG_KMS("FDI train 2 done.\n");
3217 break;
3218 }
3219 udelay(50);
8db9d77b 3220 }
fa37d39e
SP
3221 if (retry < 5)
3222 break;
8db9d77b
ZW
3223 }
3224 if (i == 4)
5eddb70b 3225 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3226
3227 DRM_DEBUG_KMS("FDI train done.\n");
3228}
3229
357555c0
JB
3230/* Manual link training for Ivy Bridge A0 parts */
3231static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3232{
3233 struct drm_device *dev = crtc->dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
3235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3236 int pipe = intel_crtc->pipe;
139ccd3f 3237 u32 reg, temp, i, j;
357555c0
JB
3238
3239 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3240 for train result */
3241 reg = FDI_RX_IMR(pipe);
3242 temp = I915_READ(reg);
3243 temp &= ~FDI_RX_SYMBOL_LOCK;
3244 temp &= ~FDI_RX_BIT_LOCK;
3245 I915_WRITE(reg, temp);
3246
3247 POSTING_READ(reg);
3248 udelay(150);
3249
01a415fd
DV
3250 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3251 I915_READ(FDI_RX_IIR(pipe)));
3252
139ccd3f
JB
3253 /* Try each vswing and preemphasis setting twice before moving on */
3254 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3255 /* disable first in case we need to retry */
3256 reg = FDI_TX_CTL(pipe);
3257 temp = I915_READ(reg);
3258 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3259 temp &= ~FDI_TX_ENABLE;
3260 I915_WRITE(reg, temp);
357555c0 3261
139ccd3f
JB
3262 reg = FDI_RX_CTL(pipe);
3263 temp = I915_READ(reg);
3264 temp &= ~FDI_LINK_TRAIN_AUTO;
3265 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3266 temp &= ~FDI_RX_ENABLE;
3267 I915_WRITE(reg, temp);
357555c0 3268
139ccd3f 3269 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3270 reg = FDI_TX_CTL(pipe);
3271 temp = I915_READ(reg);
139ccd3f
JB
3272 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3273 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3274 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3275 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3276 temp |= snb_b_fdi_train_param[j/2];
3277 temp |= FDI_COMPOSITE_SYNC;
3278 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3279
139ccd3f
JB
3280 I915_WRITE(FDI_RX_MISC(pipe),
3281 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3282
139ccd3f 3283 reg = FDI_RX_CTL(pipe);
357555c0 3284 temp = I915_READ(reg);
139ccd3f
JB
3285 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3286 temp |= FDI_COMPOSITE_SYNC;
3287 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3288
139ccd3f
JB
3289 POSTING_READ(reg);
3290 udelay(1); /* should be 0.5us */
357555c0 3291
139ccd3f
JB
3292 for (i = 0; i < 4; i++) {
3293 reg = FDI_RX_IIR(pipe);
3294 temp = I915_READ(reg);
3295 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3296
139ccd3f
JB
3297 if (temp & FDI_RX_BIT_LOCK ||
3298 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3299 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3300 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3301 i);
3302 break;
3303 }
3304 udelay(1); /* should be 0.5us */
3305 }
3306 if (i == 4) {
3307 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3308 continue;
3309 }
357555c0 3310
139ccd3f 3311 /* Train 2 */
357555c0
JB
3312 reg = FDI_TX_CTL(pipe);
3313 temp = I915_READ(reg);
139ccd3f
JB
3314 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3315 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3316 I915_WRITE(reg, temp);
3317
3318 reg = FDI_RX_CTL(pipe);
3319 temp = I915_READ(reg);
3320 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3321 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3322 I915_WRITE(reg, temp);
3323
3324 POSTING_READ(reg);
139ccd3f 3325 udelay(2); /* should be 1.5us */
357555c0 3326
139ccd3f
JB
3327 for (i = 0; i < 4; i++) {
3328 reg = FDI_RX_IIR(pipe);
3329 temp = I915_READ(reg);
3330 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3331
139ccd3f
JB
3332 if (temp & FDI_RX_SYMBOL_LOCK ||
3333 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3334 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3335 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3336 i);
3337 goto train_done;
3338 }
3339 udelay(2); /* should be 1.5us */
357555c0 3340 }
139ccd3f
JB
3341 if (i == 4)
3342 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3343 }
357555c0 3344
139ccd3f 3345train_done:
357555c0
JB
3346 DRM_DEBUG_KMS("FDI train done.\n");
3347}
3348
88cefb6c 3349static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3350{
88cefb6c 3351 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3352 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3353 int pipe = intel_crtc->pipe;
5eddb70b 3354 u32 reg, temp;
79e53945 3355
c64e311e 3356
c98e9dcf 3357 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3358 reg = FDI_RX_CTL(pipe);
3359 temp = I915_READ(reg);
627eb5a3
DV
3360 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3361 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3362 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3363 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3364
3365 POSTING_READ(reg);
c98e9dcf
JB
3366 udelay(200);
3367
3368 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3369 temp = I915_READ(reg);
3370 I915_WRITE(reg, temp | FDI_PCDCLK);
3371
3372 POSTING_READ(reg);
c98e9dcf
JB
3373 udelay(200);
3374
20749730
PZ
3375 /* Enable CPU FDI TX PLL, always on for Ironlake */
3376 reg = FDI_TX_CTL(pipe);
3377 temp = I915_READ(reg);
3378 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3379 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3380
20749730
PZ
3381 POSTING_READ(reg);
3382 udelay(100);
6be4a607 3383 }
0e23b99d
JB
3384}
3385
88cefb6c
DV
3386static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3387{
3388 struct drm_device *dev = intel_crtc->base.dev;
3389 struct drm_i915_private *dev_priv = dev->dev_private;
3390 int pipe = intel_crtc->pipe;
3391 u32 reg, temp;
3392
3393 /* Switch from PCDclk to Rawclk */
3394 reg = FDI_RX_CTL(pipe);
3395 temp = I915_READ(reg);
3396 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3397
3398 /* Disable CPU FDI TX PLL */
3399 reg = FDI_TX_CTL(pipe);
3400 temp = I915_READ(reg);
3401 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3402
3403 POSTING_READ(reg);
3404 udelay(100);
3405
3406 reg = FDI_RX_CTL(pipe);
3407 temp = I915_READ(reg);
3408 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3409
3410 /* Wait for the clocks to turn off. */
3411 POSTING_READ(reg);
3412 udelay(100);
3413}
3414
0fc932b8
JB
3415static void ironlake_fdi_disable(struct drm_crtc *crtc)
3416{
3417 struct drm_device *dev = crtc->dev;
3418 struct drm_i915_private *dev_priv = dev->dev_private;
3419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3420 int pipe = intel_crtc->pipe;
3421 u32 reg, temp;
3422
3423 /* disable CPU FDI tx and PCH FDI rx */
3424 reg = FDI_TX_CTL(pipe);
3425 temp = I915_READ(reg);
3426 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3427 POSTING_READ(reg);
3428
3429 reg = FDI_RX_CTL(pipe);
3430 temp = I915_READ(reg);
3431 temp &= ~(0x7 << 16);
dfd07d72 3432 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3433 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3434
3435 POSTING_READ(reg);
3436 udelay(100);
3437
3438 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3439 if (HAS_PCH_IBX(dev))
6f06ce18 3440 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3441
3442 /* still set train pattern 1 */
3443 reg = FDI_TX_CTL(pipe);
3444 temp = I915_READ(reg);
3445 temp &= ~FDI_LINK_TRAIN_NONE;
3446 temp |= FDI_LINK_TRAIN_PATTERN_1;
3447 I915_WRITE(reg, temp);
3448
3449 reg = FDI_RX_CTL(pipe);
3450 temp = I915_READ(reg);
3451 if (HAS_PCH_CPT(dev)) {
3452 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3453 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3454 } else {
3455 temp &= ~FDI_LINK_TRAIN_NONE;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1;
3457 }
3458 /* BPC in FDI rx is consistent with that in PIPECONF */
3459 temp &= ~(0x07 << 16);
dfd07d72 3460 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3461 I915_WRITE(reg, temp);
3462
3463 POSTING_READ(reg);
3464 udelay(100);
3465}
3466
5dce5b93
CW
3467bool intel_has_pending_fb_unpin(struct drm_device *dev)
3468{
3469 struct intel_crtc *crtc;
3470
3471 /* Note that we don't need to be called with mode_config.lock here
3472 * as our list of CRTC objects is static for the lifetime of the
3473 * device and so cannot disappear as we iterate. Similarly, we can
3474 * happily treat the predicates as racy, atomic checks as userspace
3475 * cannot claim and pin a new fb without at least acquring the
3476 * struct_mutex and so serialising with us.
3477 */
d3fcc808 3478 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3479 if (atomic_read(&crtc->unpin_work_count) == 0)
3480 continue;
3481
3482 if (crtc->unpin_work)
3483 intel_wait_for_vblank(dev, crtc->pipe);
3484
3485 return true;
3486 }
3487
3488 return false;
3489}
3490
d6bbafa1
CW
3491static void page_flip_completed(struct intel_crtc *intel_crtc)
3492{
3493 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3494 struct intel_unpin_work *work = intel_crtc->unpin_work;
3495
3496 /* ensure that the unpin work is consistent wrt ->pending. */
3497 smp_rmb();
3498 intel_crtc->unpin_work = NULL;
3499
3500 if (work->event)
3501 drm_send_vblank_event(intel_crtc->base.dev,
3502 intel_crtc->pipe,
3503 work->event);
3504
3505 drm_crtc_vblank_put(&intel_crtc->base);
3506
3507 wake_up_all(&dev_priv->pending_flip_queue);
3508 queue_work(dev_priv->wq, &work->work);
3509
3510 trace_i915_flip_complete(intel_crtc->plane,
3511 work->pending_flip_obj);
3512}
3513
46a55d30 3514void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3515{
0f91128d 3516 struct drm_device *dev = crtc->dev;
5bb61643 3517 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3518
2c10d571 3519 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3520 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3521 !intel_crtc_has_pending_flip(crtc),
3522 60*HZ) == 0)) {
3523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3524
5e2d7afc 3525 spin_lock_irq(&dev->event_lock);
9c787942
CW
3526 if (intel_crtc->unpin_work) {
3527 WARN_ONCE(1, "Removing stuck page flip\n");
3528 page_flip_completed(intel_crtc);
3529 }
5e2d7afc 3530 spin_unlock_irq(&dev->event_lock);
9c787942 3531 }
5bb61643 3532
975d568a
CW
3533 if (crtc->primary->fb) {
3534 mutex_lock(&dev->struct_mutex);
3535 intel_finish_fb(crtc->primary->fb);
3536 mutex_unlock(&dev->struct_mutex);
3537 }
e6c3a2a6
CW
3538}
3539
e615efe4
ED
3540/* Program iCLKIP clock to the desired frequency */
3541static void lpt_program_iclkip(struct drm_crtc *crtc)
3542{
3543 struct drm_device *dev = crtc->dev;
3544 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3545 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3546 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3547 u32 temp;
3548
09153000
DV
3549 mutex_lock(&dev_priv->dpio_lock);
3550
e615efe4
ED
3551 /* It is necessary to ungate the pixclk gate prior to programming
3552 * the divisors, and gate it back when it is done.
3553 */
3554 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3555
3556 /* Disable SSCCTL */
3557 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3558 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3559 SBI_SSCCTL_DISABLE,
3560 SBI_ICLK);
e615efe4
ED
3561
3562 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3563 if (clock == 20000) {
e615efe4
ED
3564 auxdiv = 1;
3565 divsel = 0x41;
3566 phaseinc = 0x20;
3567 } else {
3568 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3569 * but the adjusted_mode->crtc_clock in in KHz. To get the
3570 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3571 * convert the virtual clock precision to KHz here for higher
3572 * precision.
3573 */
3574 u32 iclk_virtual_root_freq = 172800 * 1000;
3575 u32 iclk_pi_range = 64;
3576 u32 desired_divisor, msb_divisor_value, pi_value;
3577
12d7ceed 3578 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3579 msb_divisor_value = desired_divisor / iclk_pi_range;
3580 pi_value = desired_divisor % iclk_pi_range;
3581
3582 auxdiv = 0;
3583 divsel = msb_divisor_value - 2;
3584 phaseinc = pi_value;
3585 }
3586
3587 /* This should not happen with any sane values */
3588 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3589 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3590 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3591 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3592
3593 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3594 clock,
e615efe4
ED
3595 auxdiv,
3596 divsel,
3597 phasedir,
3598 phaseinc);
3599
3600 /* Program SSCDIVINTPHASE6 */
988d6ee8 3601 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3602 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3603 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3604 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3605 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3606 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3607 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3608 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3609
3610 /* Program SSCAUXDIV */
988d6ee8 3611 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3612 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3613 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3614 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3615
3616 /* Enable modulator and associated divider */
988d6ee8 3617 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3618 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3619 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3620
3621 /* Wait for initialization time */
3622 udelay(24);
3623
3624 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3625
3626 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3627}
3628
275f01b2
DV
3629static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3630 enum pipe pch_transcoder)
3631{
3632 struct drm_device *dev = crtc->base.dev;
3633 struct drm_i915_private *dev_priv = dev->dev_private;
3634 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3635
3636 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3637 I915_READ(HTOTAL(cpu_transcoder)));
3638 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3639 I915_READ(HBLANK(cpu_transcoder)));
3640 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3641 I915_READ(HSYNC(cpu_transcoder)));
3642
3643 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3644 I915_READ(VTOTAL(cpu_transcoder)));
3645 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3646 I915_READ(VBLANK(cpu_transcoder)));
3647 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3648 I915_READ(VSYNC(cpu_transcoder)));
3649 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3650 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3651}
3652
1fbc0d78
DV
3653static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3654{
3655 struct drm_i915_private *dev_priv = dev->dev_private;
3656 uint32_t temp;
3657
3658 temp = I915_READ(SOUTH_CHICKEN1);
3659 if (temp & FDI_BC_BIFURCATION_SELECT)
3660 return;
3661
3662 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3663 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3664
3665 temp |= FDI_BC_BIFURCATION_SELECT;
3666 DRM_DEBUG_KMS("enabling fdi C rx\n");
3667 I915_WRITE(SOUTH_CHICKEN1, temp);
3668 POSTING_READ(SOUTH_CHICKEN1);
3669}
3670
3671static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3672{
3673 struct drm_device *dev = intel_crtc->base.dev;
3674 struct drm_i915_private *dev_priv = dev->dev_private;
3675
3676 switch (intel_crtc->pipe) {
3677 case PIPE_A:
3678 break;
3679 case PIPE_B:
3680 if (intel_crtc->config.fdi_lanes > 2)
3681 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3682 else
3683 cpt_enable_fdi_bc_bifurcation(dev);
3684
3685 break;
3686 case PIPE_C:
3687 cpt_enable_fdi_bc_bifurcation(dev);
3688
3689 break;
3690 default:
3691 BUG();
3692 }
3693}
3694
f67a559d
JB
3695/*
3696 * Enable PCH resources required for PCH ports:
3697 * - PCH PLLs
3698 * - FDI training & RX/TX
3699 * - update transcoder timings
3700 * - DP transcoding bits
3701 * - transcoder
3702 */
3703static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3704{
3705 struct drm_device *dev = crtc->dev;
3706 struct drm_i915_private *dev_priv = dev->dev_private;
3707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3708 int pipe = intel_crtc->pipe;
ee7b9f93 3709 u32 reg, temp;
2c07245f 3710
ab9412ba 3711 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3712
1fbc0d78
DV
3713 if (IS_IVYBRIDGE(dev))
3714 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3715
cd986abb
DV
3716 /* Write the TU size bits before fdi link training, so that error
3717 * detection works. */
3718 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3719 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3720
c98e9dcf 3721 /* For PCH output, training FDI link */
674cf967 3722 dev_priv->display.fdi_link_train(crtc);
2c07245f 3723
3ad8a208
DV
3724 /* We need to program the right clock selection before writing the pixel
3725 * mutliplier into the DPLL. */
303b81e0 3726 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3727 u32 sel;
4b645f14 3728
c98e9dcf 3729 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3730 temp |= TRANS_DPLL_ENABLE(pipe);
3731 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3732 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3733 temp |= sel;
3734 else
3735 temp &= ~sel;
c98e9dcf 3736 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3737 }
5eddb70b 3738
3ad8a208
DV
3739 /* XXX: pch pll's can be enabled any time before we enable the PCH
3740 * transcoder, and we actually should do this to not upset any PCH
3741 * transcoder that already use the clock when we share it.
3742 *
3743 * Note that enable_shared_dpll tries to do the right thing, but
3744 * get_shared_dpll unconditionally resets the pll - we need that to have
3745 * the right LVDS enable sequence. */
85b3894f 3746 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3747
d9b6cb56
JB
3748 /* set transcoder timing, panel must allow it */
3749 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3750 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3751
303b81e0 3752 intel_fdi_normal_train(crtc);
5e84e1a4 3753
c98e9dcf
JB
3754 /* For PCH DP, enable TRANS_DP_CTL */
3755 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3756 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3757 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3758 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3759 reg = TRANS_DP_CTL(pipe);
3760 temp = I915_READ(reg);
3761 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3762 TRANS_DP_SYNC_MASK |
3763 TRANS_DP_BPC_MASK);
5eddb70b
CW
3764 temp |= (TRANS_DP_OUTPUT_ENABLE |
3765 TRANS_DP_ENH_FRAMING);
9325c9f0 3766 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3767
3768 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3769 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3770 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3771 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3772
3773 switch (intel_trans_dp_port_sel(crtc)) {
3774 case PCH_DP_B:
5eddb70b 3775 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3776 break;
3777 case PCH_DP_C:
5eddb70b 3778 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3779 break;
3780 case PCH_DP_D:
5eddb70b 3781 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3782 break;
3783 default:
e95d41e1 3784 BUG();
32f9d658 3785 }
2c07245f 3786
5eddb70b 3787 I915_WRITE(reg, temp);
6be4a607 3788 }
b52eb4dc 3789
b8a4f404 3790 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3791}
3792
1507e5bd
PZ
3793static void lpt_pch_enable(struct drm_crtc *crtc)
3794{
3795 struct drm_device *dev = crtc->dev;
3796 struct drm_i915_private *dev_priv = dev->dev_private;
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3798 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3799
ab9412ba 3800 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3801
8c52b5e8 3802 lpt_program_iclkip(crtc);
1507e5bd 3803
0540e488 3804 /* Set transcoder timing. */
275f01b2 3805 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3806
937bb610 3807 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3808}
3809
716c2e55 3810void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3811{
e2b78267 3812 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3813
3814 if (pll == NULL)
3815 return;
3816
3817 if (pll->refcount == 0) {
46edb027 3818 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3819 return;
3820 }
3821
f4a091c7
DV
3822 if (--pll->refcount == 0) {
3823 WARN_ON(pll->on);
3824 WARN_ON(pll->active);
3825 }
3826
a43f6e0f 3827 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3828}
3829
716c2e55 3830struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3831{
e2b78267
DV
3832 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3833 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3834 enum intel_dpll_id i;
ee7b9f93 3835
ee7b9f93 3836 if (pll) {
46edb027
DV
3837 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3838 crtc->base.base.id, pll->name);
e2b78267 3839 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3840 }
3841
98b6bd99
DV
3842 if (HAS_PCH_IBX(dev_priv->dev)) {
3843 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3844 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3845 pll = &dev_priv->shared_dplls[i];
98b6bd99 3846
46edb027
DV
3847 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3848 crtc->base.base.id, pll->name);
98b6bd99 3849
f2a69f44
DV
3850 WARN_ON(pll->refcount);
3851
98b6bd99
DV
3852 goto found;
3853 }
3854
e72f9fbf
DV
3855 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3856 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3857
3858 /* Only want to check enabled timings first */
3859 if (pll->refcount == 0)
3860 continue;
3861
b89a1d39
DV
3862 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3863 sizeof(pll->hw_state)) == 0) {
46edb027 3864 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3865 crtc->base.base.id,
46edb027 3866 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3867
3868 goto found;
3869 }
3870 }
3871
3872 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3873 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3874 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3875 if (pll->refcount == 0) {
46edb027
DV
3876 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3877 crtc->base.base.id, pll->name);
ee7b9f93
JB
3878 goto found;
3879 }
3880 }
3881
3882 return NULL;
3883
3884found:
f2a69f44
DV
3885 if (pll->refcount == 0)
3886 pll->hw_state = crtc->config.dpll_hw_state;
3887
a43f6e0f 3888 crtc->config.shared_dpll = i;
46edb027
DV
3889 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3890 pipe_name(crtc->pipe));
ee7b9f93 3891
cdbd2316 3892 pll->refcount++;
e04c7350 3893
ee7b9f93
JB
3894 return pll;
3895}
3896
a1520318 3897static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3898{
3899 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3900 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3901 u32 temp;
3902
3903 temp = I915_READ(dslreg);
3904 udelay(500);
3905 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3906 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3907 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3908 }
3909}
3910
b074cec8
JB
3911static void ironlake_pfit_enable(struct intel_crtc *crtc)
3912{
3913 struct drm_device *dev = crtc->base.dev;
3914 struct drm_i915_private *dev_priv = dev->dev_private;
3915 int pipe = crtc->pipe;
3916
fd4daa9c 3917 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3918 /* Force use of hard-coded filter coefficients
3919 * as some pre-programmed values are broken,
3920 * e.g. x201.
3921 */
3922 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3923 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3924 PF_PIPE_SEL_IVB(pipe));
3925 else
3926 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3927 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3928 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3929 }
3930}
3931
bb53d4ae
VS
3932static void intel_enable_planes(struct drm_crtc *crtc)
3933{
3934 struct drm_device *dev = crtc->dev;
3935 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3936 struct drm_plane *plane;
bb53d4ae
VS
3937 struct intel_plane *intel_plane;
3938
af2b653b
MR
3939 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3940 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3941 if (intel_plane->pipe == pipe)
3942 intel_plane_restore(&intel_plane->base);
af2b653b 3943 }
bb53d4ae
VS
3944}
3945
3946static void intel_disable_planes(struct drm_crtc *crtc)
3947{
3948 struct drm_device *dev = crtc->dev;
3949 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3950 struct drm_plane *plane;
bb53d4ae
VS
3951 struct intel_plane *intel_plane;
3952
af2b653b
MR
3953 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3954 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3955 if (intel_plane->pipe == pipe)
3956 intel_plane_disable(&intel_plane->base);
af2b653b 3957 }
bb53d4ae
VS
3958}
3959
20bc8673 3960void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3961{
cea165c3
VS
3962 struct drm_device *dev = crtc->base.dev;
3963 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3964
3965 if (!crtc->config.ips_enabled)
3966 return;
3967
cea165c3
VS
3968 /* We can only enable IPS after we enable a plane and wait for a vblank */
3969 intel_wait_for_vblank(dev, crtc->pipe);
3970
d77e4531 3971 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3972 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3973 mutex_lock(&dev_priv->rps.hw_lock);
3974 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3975 mutex_unlock(&dev_priv->rps.hw_lock);
3976 /* Quoting Art Runyan: "its not safe to expect any particular
3977 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3978 * mailbox." Moreover, the mailbox may return a bogus state,
3979 * so we need to just enable it and continue on.
2a114cc1
BW
3980 */
3981 } else {
3982 I915_WRITE(IPS_CTL, IPS_ENABLE);
3983 /* The bit only becomes 1 in the next vblank, so this wait here
3984 * is essentially intel_wait_for_vblank. If we don't have this
3985 * and don't wait for vblanks until the end of crtc_enable, then
3986 * the HW state readout code will complain that the expected
3987 * IPS_CTL value is not the one we read. */
3988 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3989 DRM_ERROR("Timed out waiting for IPS enable\n");
3990 }
d77e4531
PZ
3991}
3992
20bc8673 3993void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3994{
3995 struct drm_device *dev = crtc->base.dev;
3996 struct drm_i915_private *dev_priv = dev->dev_private;
3997
3998 if (!crtc->config.ips_enabled)
3999 return;
4000
4001 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4002 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4003 mutex_lock(&dev_priv->rps.hw_lock);
4004 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4005 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4006 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4007 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4008 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4009 } else {
2a114cc1 4010 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4011 POSTING_READ(IPS_CTL);
4012 }
d77e4531
PZ
4013
4014 /* We need to wait for a vblank before we can disable the plane. */
4015 intel_wait_for_vblank(dev, crtc->pipe);
4016}
4017
4018/** Loads the palette/gamma unit for the CRTC with the prepared values */
4019static void intel_crtc_load_lut(struct drm_crtc *crtc)
4020{
4021 struct drm_device *dev = crtc->dev;
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4024 enum pipe pipe = intel_crtc->pipe;
4025 int palreg = PALETTE(pipe);
4026 int i;
4027 bool reenable_ips = false;
4028
4029 /* The clocks have to be on to load the palette. */
4030 if (!crtc->enabled || !intel_crtc->active)
4031 return;
4032
4033 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
4034 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4035 assert_dsi_pll_enabled(dev_priv);
4036 else
4037 assert_pll_enabled(dev_priv, pipe);
4038 }
4039
4040 /* use legacy palette for Ironlake */
7a1db49a 4041 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4042 palreg = LGC_PALETTE(pipe);
4043
4044 /* Workaround : Do not read or write the pipe palette/gamma data while
4045 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4046 */
41e6fc4c 4047 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
4048 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4049 GAMMA_MODE_MODE_SPLIT)) {
4050 hsw_disable_ips(intel_crtc);
4051 reenable_ips = true;
4052 }
4053
4054 for (i = 0; i < 256; i++) {
4055 I915_WRITE(palreg + 4 * i,
4056 (intel_crtc->lut_r[i] << 16) |
4057 (intel_crtc->lut_g[i] << 8) |
4058 intel_crtc->lut_b[i]);
4059 }
4060
4061 if (reenable_ips)
4062 hsw_enable_ips(intel_crtc);
4063}
4064
d3eedb1a
VS
4065static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4066{
4067 if (!enable && intel_crtc->overlay) {
4068 struct drm_device *dev = intel_crtc->base.dev;
4069 struct drm_i915_private *dev_priv = dev->dev_private;
4070
4071 mutex_lock(&dev->struct_mutex);
4072 dev_priv->mm.interruptible = false;
4073 (void) intel_overlay_switch_off(intel_crtc->overlay);
4074 dev_priv->mm.interruptible = true;
4075 mutex_unlock(&dev->struct_mutex);
4076 }
4077
4078 /* Let userspace switch the overlay on again. In most cases userspace
4079 * has to recompute where to put it anyway.
4080 */
4081}
4082
d3eedb1a 4083static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4084{
4085 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4087 int pipe = intel_crtc->pipe;
a5c4d7bc 4088
fdd508a6 4089 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
4090 intel_enable_planes(crtc);
4091 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4092 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4093
4094 hsw_enable_ips(intel_crtc);
4095
4096 mutex_lock(&dev->struct_mutex);
4097 intel_update_fbc(dev);
4098 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4099
4100 /*
4101 * FIXME: Once we grow proper nuclear flip support out of this we need
4102 * to compute the mask of flip planes precisely. For the time being
4103 * consider this a flip from a NULL plane.
4104 */
4105 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4106}
4107
d3eedb1a 4108static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4109{
4110 struct drm_device *dev = crtc->dev;
4111 struct drm_i915_private *dev_priv = dev->dev_private;
4112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4113 int pipe = intel_crtc->pipe;
4114 int plane = intel_crtc->plane;
4115
4116 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4117
4118 if (dev_priv->fbc.plane == plane)
4119 intel_disable_fbc(dev);
4120
4121 hsw_disable_ips(intel_crtc);
4122
d3eedb1a 4123 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
4124 intel_crtc_update_cursor(crtc, false);
4125 intel_disable_planes(crtc);
fdd508a6 4126 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4127
f99d7069
DV
4128 /*
4129 * FIXME: Once we grow proper nuclear flip support out of this we need
4130 * to compute the mask of flip planes precisely. For the time being
4131 * consider this a flip to a NULL plane.
4132 */
4133 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4134}
4135
f67a559d
JB
4136static void ironlake_crtc_enable(struct drm_crtc *crtc)
4137{
4138 struct drm_device *dev = crtc->dev;
4139 struct drm_i915_private *dev_priv = dev->dev_private;
4140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4141 struct intel_encoder *encoder;
f67a559d 4142 int pipe = intel_crtc->pipe;
f67a559d 4143
08a48469
DV
4144 WARN_ON(!crtc->enabled);
4145
f67a559d
JB
4146 if (intel_crtc->active)
4147 return;
4148
b14b1055
DV
4149 if (intel_crtc->config.has_pch_encoder)
4150 intel_prepare_shared_dpll(intel_crtc);
4151
29407aab
DV
4152 if (intel_crtc->config.has_dp_encoder)
4153 intel_dp_set_m_n(intel_crtc);
4154
4155 intel_set_pipe_timings(intel_crtc);
4156
4157 if (intel_crtc->config.has_pch_encoder) {
4158 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4159 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4160 }
4161
4162 ironlake_set_pipeconf(crtc);
4163
f67a559d 4164 intel_crtc->active = true;
8664281b 4165
a72e4c9f
DV
4166 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4167 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4168
f6736a1a 4169 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4170 if (encoder->pre_enable)
4171 encoder->pre_enable(encoder);
f67a559d 4172
5bfe2ac0 4173 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4174 /* Note: FDI PLL enabling _must_ be done before we enable the
4175 * cpu pipes, hence this is separate from all the other fdi/pch
4176 * enabling. */
88cefb6c 4177 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4178 } else {
4179 assert_fdi_tx_disabled(dev_priv, pipe);
4180 assert_fdi_rx_disabled(dev_priv, pipe);
4181 }
f67a559d 4182
b074cec8 4183 ironlake_pfit_enable(intel_crtc);
f67a559d 4184
9c54c0dd
JB
4185 /*
4186 * On ILK+ LUT must be loaded before the pipe is running but with
4187 * clocks enabled
4188 */
4189 intel_crtc_load_lut(crtc);
4190
f37fcc2a 4191 intel_update_watermarks(crtc);
e1fdc473 4192 intel_enable_pipe(intel_crtc);
f67a559d 4193
5bfe2ac0 4194 if (intel_crtc->config.has_pch_encoder)
f67a559d 4195 ironlake_pch_enable(crtc);
c98e9dcf 4196
fa5c73b1
DV
4197 for_each_encoder_on_crtc(dev, crtc, encoder)
4198 encoder->enable(encoder);
61b77ddd
DV
4199
4200 if (HAS_PCH_CPT(dev))
a1520318 4201 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4202
4b3a9526
VS
4203 assert_vblank_disabled(crtc);
4204 drm_crtc_vblank_on(crtc);
4205
d3eedb1a 4206 intel_crtc_enable_planes(crtc);
6be4a607
JB
4207}
4208
42db64ef
PZ
4209/* IPS only exists on ULT machines and is tied to pipe A. */
4210static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4211{
f5adf94e 4212 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4213}
4214
e4916946
PZ
4215/*
4216 * This implements the workaround described in the "notes" section of the mode
4217 * set sequence documentation. When going from no pipes or single pipe to
4218 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4219 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4220 */
4221static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4222{
4223 struct drm_device *dev = crtc->base.dev;
4224 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4225
4226 /* We want to get the other_active_crtc only if there's only 1 other
4227 * active crtc. */
d3fcc808 4228 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4229 if (!crtc_it->active || crtc_it == crtc)
4230 continue;
4231
4232 if (other_active_crtc)
4233 return;
4234
4235 other_active_crtc = crtc_it;
4236 }
4237 if (!other_active_crtc)
4238 return;
4239
4240 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4241 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4242}
4243
4f771f10
PZ
4244static void haswell_crtc_enable(struct drm_crtc *crtc)
4245{
4246 struct drm_device *dev = crtc->dev;
4247 struct drm_i915_private *dev_priv = dev->dev_private;
4248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4249 struct intel_encoder *encoder;
4250 int pipe = intel_crtc->pipe;
4f771f10
PZ
4251
4252 WARN_ON(!crtc->enabled);
4253
4254 if (intel_crtc->active)
4255 return;
4256
df8ad70c
DV
4257 if (intel_crtc_to_shared_dpll(intel_crtc))
4258 intel_enable_shared_dpll(intel_crtc);
4259
229fca97
DV
4260 if (intel_crtc->config.has_dp_encoder)
4261 intel_dp_set_m_n(intel_crtc);
4262
4263 intel_set_pipe_timings(intel_crtc);
4264
ebb69c95
CT
4265 if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
4266 I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
4267 intel_crtc->config.pixel_multiplier - 1);
4268 }
4269
229fca97
DV
4270 if (intel_crtc->config.has_pch_encoder) {
4271 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4272 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4273 }
4274
4275 haswell_set_pipeconf(crtc);
4276
4277 intel_set_pipe_csc(crtc);
4278
4f771f10 4279 intel_crtc->active = true;
8664281b 4280
a72e4c9f 4281 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4282 for_each_encoder_on_crtc(dev, crtc, encoder)
4283 if (encoder->pre_enable)
4284 encoder->pre_enable(encoder);
4285
4fe9467d 4286 if (intel_crtc->config.has_pch_encoder) {
a72e4c9f
DV
4287 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4288 true);
4fe9467d
ID
4289 dev_priv->display.fdi_link_train(crtc);
4290 }
4291
1f544388 4292 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4293
b074cec8 4294 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4295
4296 /*
4297 * On ILK+ LUT must be loaded before the pipe is running but with
4298 * clocks enabled
4299 */
4300 intel_crtc_load_lut(crtc);
4301
1f544388 4302 intel_ddi_set_pipe_settings(crtc);
8228c251 4303 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4304
f37fcc2a 4305 intel_update_watermarks(crtc);
e1fdc473 4306 intel_enable_pipe(intel_crtc);
42db64ef 4307
5bfe2ac0 4308 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4309 lpt_pch_enable(crtc);
4f771f10 4310
0e32b39c
DA
4311 if (intel_crtc->config.dp_encoder_is_mst)
4312 intel_ddi_set_vc_payload_alloc(crtc, true);
4313
8807e55b 4314 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4315 encoder->enable(encoder);
8807e55b
JN
4316 intel_opregion_notify_encoder(encoder, true);
4317 }
4f771f10 4318
4b3a9526
VS
4319 assert_vblank_disabled(crtc);
4320 drm_crtc_vblank_on(crtc);
4321
e4916946
PZ
4322 /* If we change the relative order between pipe/planes enabling, we need
4323 * to change the workaround. */
4324 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4325 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4326}
4327
3f8dce3a
DV
4328static void ironlake_pfit_disable(struct intel_crtc *crtc)
4329{
4330 struct drm_device *dev = crtc->base.dev;
4331 struct drm_i915_private *dev_priv = dev->dev_private;
4332 int pipe = crtc->pipe;
4333
4334 /* To avoid upsetting the power well on haswell only disable the pfit if
4335 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4336 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4337 I915_WRITE(PF_CTL(pipe), 0);
4338 I915_WRITE(PF_WIN_POS(pipe), 0);
4339 I915_WRITE(PF_WIN_SZ(pipe), 0);
4340 }
4341}
4342
6be4a607
JB
4343static void ironlake_crtc_disable(struct drm_crtc *crtc)
4344{
4345 struct drm_device *dev = crtc->dev;
4346 struct drm_i915_private *dev_priv = dev->dev_private;
4347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4348 struct intel_encoder *encoder;
6be4a607 4349 int pipe = intel_crtc->pipe;
5eddb70b 4350 u32 reg, temp;
b52eb4dc 4351
f7abfe8b
CW
4352 if (!intel_crtc->active)
4353 return;
4354
d3eedb1a 4355 intel_crtc_disable_planes(crtc);
a5c4d7bc 4356
4b3a9526
VS
4357 drm_crtc_vblank_off(crtc);
4358 assert_vblank_disabled(crtc);
4359
ea9d758d
DV
4360 for_each_encoder_on_crtc(dev, crtc, encoder)
4361 encoder->disable(encoder);
4362
d925c59a 4363 if (intel_crtc->config.has_pch_encoder)
a72e4c9f 4364 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4365
575f7ab7 4366 intel_disable_pipe(intel_crtc);
32f9d658 4367
3f8dce3a 4368 ironlake_pfit_disable(intel_crtc);
2c07245f 4369
bf49ec8c
DV
4370 for_each_encoder_on_crtc(dev, crtc, encoder)
4371 if (encoder->post_disable)
4372 encoder->post_disable(encoder);
2c07245f 4373
d925c59a
DV
4374 if (intel_crtc->config.has_pch_encoder) {
4375 ironlake_fdi_disable(crtc);
913d8d11 4376
d925c59a 4377 ironlake_disable_pch_transcoder(dev_priv, pipe);
a72e4c9f 4378 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 4379
d925c59a
DV
4380 if (HAS_PCH_CPT(dev)) {
4381 /* disable TRANS_DP_CTL */
4382 reg = TRANS_DP_CTL(pipe);
4383 temp = I915_READ(reg);
4384 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4385 TRANS_DP_PORT_SEL_MASK);
4386 temp |= TRANS_DP_PORT_SEL_NONE;
4387 I915_WRITE(reg, temp);
4388
4389 /* disable DPLL_SEL */
4390 temp = I915_READ(PCH_DPLL_SEL);
11887397 4391 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4392 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4393 }
e3421a18 4394
d925c59a 4395 /* disable PCH DPLL */
e72f9fbf 4396 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4397
d925c59a
DV
4398 ironlake_fdi_pll_disable(intel_crtc);
4399 }
6b383a7f 4400
f7abfe8b 4401 intel_crtc->active = false;
46ba614c 4402 intel_update_watermarks(crtc);
d1ebd816
BW
4403
4404 mutex_lock(&dev->struct_mutex);
6b383a7f 4405 intel_update_fbc(dev);
d1ebd816 4406 mutex_unlock(&dev->struct_mutex);
6be4a607 4407}
1b3c7a47 4408
4f771f10 4409static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4410{
4f771f10
PZ
4411 struct drm_device *dev = crtc->dev;
4412 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4414 struct intel_encoder *encoder;
3b117c8f 4415 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4416
4f771f10
PZ
4417 if (!intel_crtc->active)
4418 return;
4419
d3eedb1a 4420 intel_crtc_disable_planes(crtc);
dda9a66a 4421
4b3a9526
VS
4422 drm_crtc_vblank_off(crtc);
4423 assert_vblank_disabled(crtc);
4424
8807e55b
JN
4425 for_each_encoder_on_crtc(dev, crtc, encoder) {
4426 intel_opregion_notify_encoder(encoder, false);
4f771f10 4427 encoder->disable(encoder);
8807e55b 4428 }
4f771f10 4429
8664281b 4430 if (intel_crtc->config.has_pch_encoder)
a72e4c9f
DV
4431 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4432 false);
575f7ab7 4433 intel_disable_pipe(intel_crtc);
4f771f10 4434
a4bf214f
VS
4435 if (intel_crtc->config.dp_encoder_is_mst)
4436 intel_ddi_set_vc_payload_alloc(crtc, false);
4437
ad80a810 4438 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4439
3f8dce3a 4440 ironlake_pfit_disable(intel_crtc);
4f771f10 4441
1f544388 4442 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4443
88adfff1 4444 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4445 lpt_disable_pch_transcoder(dev_priv);
a72e4c9f
DV
4446 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4447 true);
1ad960f2 4448 intel_ddi_fdi_disable(crtc);
83616634 4449 }
4f771f10 4450
97b040aa
ID
4451 for_each_encoder_on_crtc(dev, crtc, encoder)
4452 if (encoder->post_disable)
4453 encoder->post_disable(encoder);
4454
4f771f10 4455 intel_crtc->active = false;
46ba614c 4456 intel_update_watermarks(crtc);
4f771f10
PZ
4457
4458 mutex_lock(&dev->struct_mutex);
4459 intel_update_fbc(dev);
4460 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4461
4462 if (intel_crtc_to_shared_dpll(intel_crtc))
4463 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4464}
4465
ee7b9f93
JB
4466static void ironlake_crtc_off(struct drm_crtc *crtc)
4467{
4468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4469 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4470}
4471
6441ab5f 4472
2dd24552
JB
4473static void i9xx_pfit_enable(struct intel_crtc *crtc)
4474{
4475 struct drm_device *dev = crtc->base.dev;
4476 struct drm_i915_private *dev_priv = dev->dev_private;
4477 struct intel_crtc_config *pipe_config = &crtc->config;
4478
328d8e82 4479 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4480 return;
4481
2dd24552 4482 /*
c0b03411
DV
4483 * The panel fitter should only be adjusted whilst the pipe is disabled,
4484 * according to register description and PRM.
2dd24552 4485 */
c0b03411
DV
4486 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4487 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4488
b074cec8
JB
4489 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4490 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4491
4492 /* Border color in case we don't scale up to the full screen. Black by
4493 * default, change to something else for debugging. */
4494 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4495}
4496
d05410f9
DA
4497static enum intel_display_power_domain port_to_power_domain(enum port port)
4498{
4499 switch (port) {
4500 case PORT_A:
4501 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4502 case PORT_B:
4503 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4504 case PORT_C:
4505 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4506 case PORT_D:
4507 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4508 default:
4509 WARN_ON_ONCE(1);
4510 return POWER_DOMAIN_PORT_OTHER;
4511 }
4512}
4513
77d22dca
ID
4514#define for_each_power_domain(domain, mask) \
4515 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4516 if ((1 << (domain)) & (mask))
4517
319be8ae
ID
4518enum intel_display_power_domain
4519intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4520{
4521 struct drm_device *dev = intel_encoder->base.dev;
4522 struct intel_digital_port *intel_dig_port;
4523
4524 switch (intel_encoder->type) {
4525 case INTEL_OUTPUT_UNKNOWN:
4526 /* Only DDI platforms should ever use this output type */
4527 WARN_ON_ONCE(!HAS_DDI(dev));
4528 case INTEL_OUTPUT_DISPLAYPORT:
4529 case INTEL_OUTPUT_HDMI:
4530 case INTEL_OUTPUT_EDP:
4531 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4532 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4533 case INTEL_OUTPUT_DP_MST:
4534 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4535 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4536 case INTEL_OUTPUT_ANALOG:
4537 return POWER_DOMAIN_PORT_CRT;
4538 case INTEL_OUTPUT_DSI:
4539 return POWER_DOMAIN_PORT_DSI;
4540 default:
4541 return POWER_DOMAIN_PORT_OTHER;
4542 }
4543}
4544
4545static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4546{
319be8ae
ID
4547 struct drm_device *dev = crtc->dev;
4548 struct intel_encoder *intel_encoder;
4549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4551 unsigned long mask;
4552 enum transcoder transcoder;
4553
4554 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4555
4556 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4557 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4558 if (intel_crtc->config.pch_pfit.enabled ||
4559 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4560 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4561
319be8ae
ID
4562 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4563 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4564
77d22dca
ID
4565 return mask;
4566}
4567
77d22dca
ID
4568static void modeset_update_crtc_power_domains(struct drm_device *dev)
4569{
4570 struct drm_i915_private *dev_priv = dev->dev_private;
4571 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4572 struct intel_crtc *crtc;
4573
4574 /*
4575 * First get all needed power domains, then put all unneeded, to avoid
4576 * any unnecessary toggling of the power wells.
4577 */
d3fcc808 4578 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4579 enum intel_display_power_domain domain;
4580
4581 if (!crtc->base.enabled)
4582 continue;
4583
319be8ae 4584 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4585
4586 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4587 intel_display_power_get(dev_priv, domain);
4588 }
4589
d3fcc808 4590 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4591 enum intel_display_power_domain domain;
4592
4593 for_each_power_domain(domain, crtc->enabled_power_domains)
4594 intel_display_power_put(dev_priv, domain);
4595
4596 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4597 }
4598
4599 intel_display_set_init_power(dev_priv, false);
4600}
4601
dfcab17e 4602/* returns HPLL frequency in kHz */
f8bf63fd 4603static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4604{
586f49dc 4605 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4606
586f49dc
JB
4607 /* Obtain SKU information */
4608 mutex_lock(&dev_priv->dpio_lock);
4609 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4610 CCK_FUSE_HPLL_FREQ_MASK;
4611 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4612
dfcab17e 4613 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4614}
4615
f8bf63fd
VS
4616static void vlv_update_cdclk(struct drm_device *dev)
4617{
4618 struct drm_i915_private *dev_priv = dev->dev_private;
4619
4620 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4621 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4622 dev_priv->vlv_cdclk_freq);
4623
4624 /*
4625 * Program the gmbus_freq based on the cdclk frequency.
4626 * BSpec erroneously claims we should aim for 4MHz, but
4627 * in fact 1MHz is the correct frequency.
4628 */
4629 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4630}
4631
30a970c6
JB
4632/* Adjust CDclk dividers to allow high res or save power if possible */
4633static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4634{
4635 struct drm_i915_private *dev_priv = dev->dev_private;
4636 u32 val, cmd;
4637
d197b7d3 4638 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4639
dfcab17e 4640 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4641 cmd = 2;
dfcab17e 4642 else if (cdclk == 266667)
30a970c6
JB
4643 cmd = 1;
4644 else
4645 cmd = 0;
4646
4647 mutex_lock(&dev_priv->rps.hw_lock);
4648 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4649 val &= ~DSPFREQGUAR_MASK;
4650 val |= (cmd << DSPFREQGUAR_SHIFT);
4651 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4652 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4653 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4654 50)) {
4655 DRM_ERROR("timed out waiting for CDclk change\n");
4656 }
4657 mutex_unlock(&dev_priv->rps.hw_lock);
4658
dfcab17e 4659 if (cdclk == 400000) {
30a970c6
JB
4660 u32 divider, vco;
4661
4662 vco = valleyview_get_vco(dev_priv);
dfcab17e 4663 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4664
4665 mutex_lock(&dev_priv->dpio_lock);
4666 /* adjust cdclk divider */
4667 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4668 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4669 val |= divider;
4670 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4671
4672 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4673 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4674 50))
4675 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4676 mutex_unlock(&dev_priv->dpio_lock);
4677 }
4678
4679 mutex_lock(&dev_priv->dpio_lock);
4680 /* adjust self-refresh exit latency value */
4681 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4682 val &= ~0x7f;
4683
4684 /*
4685 * For high bandwidth configs, we set a higher latency in the bunit
4686 * so that the core display fetch happens in time to avoid underruns.
4687 */
dfcab17e 4688 if (cdclk == 400000)
30a970c6
JB
4689 val |= 4500 / 250; /* 4.5 usec */
4690 else
4691 val |= 3000 / 250; /* 3.0 usec */
4692 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4693 mutex_unlock(&dev_priv->dpio_lock);
4694
f8bf63fd 4695 vlv_update_cdclk(dev);
30a970c6
JB
4696}
4697
383c5a6a
VS
4698static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4699{
4700 struct drm_i915_private *dev_priv = dev->dev_private;
4701 u32 val, cmd;
4702
4703 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4704
4705 switch (cdclk) {
4706 case 400000:
4707 cmd = 3;
4708 break;
4709 case 333333:
4710 case 320000:
4711 cmd = 2;
4712 break;
4713 case 266667:
4714 cmd = 1;
4715 break;
4716 case 200000:
4717 cmd = 0;
4718 break;
4719 default:
4720 WARN_ON(1);
4721 return;
4722 }
4723
4724 mutex_lock(&dev_priv->rps.hw_lock);
4725 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4726 val &= ~DSPFREQGUAR_MASK_CHV;
4727 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4728 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4729 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4730 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4731 50)) {
4732 DRM_ERROR("timed out waiting for CDclk change\n");
4733 }
4734 mutex_unlock(&dev_priv->rps.hw_lock);
4735
4736 vlv_update_cdclk(dev);
4737}
4738
30a970c6
JB
4739static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4740 int max_pixclk)
4741{
29dc7ef3
VS
4742 int vco = valleyview_get_vco(dev_priv);
4743 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4744
d49a340d
VS
4745 /* FIXME: Punit isn't quite ready yet */
4746 if (IS_CHERRYVIEW(dev_priv->dev))
4747 return 400000;
4748
30a970c6
JB
4749 /*
4750 * Really only a few cases to deal with, as only 4 CDclks are supported:
4751 * 200MHz
4752 * 267MHz
29dc7ef3 4753 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4754 * 400MHz
4755 * So we check to see whether we're above 90% of the lower bin and
4756 * adjust if needed.
e37c67a1
VS
4757 *
4758 * We seem to get an unstable or solid color picture at 200MHz.
4759 * Not sure what's wrong. For now use 200MHz only when all pipes
4760 * are off.
30a970c6 4761 */
29dc7ef3 4762 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4763 return 400000;
4764 else if (max_pixclk > 266667*9/10)
29dc7ef3 4765 return freq_320;
e37c67a1 4766 else if (max_pixclk > 0)
dfcab17e 4767 return 266667;
e37c67a1
VS
4768 else
4769 return 200000;
30a970c6
JB
4770}
4771
2f2d7aa1
VS
4772/* compute the max pixel clock for new configuration */
4773static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4774{
4775 struct drm_device *dev = dev_priv->dev;
4776 struct intel_crtc *intel_crtc;
4777 int max_pixclk = 0;
4778
d3fcc808 4779 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4780 if (intel_crtc->new_enabled)
30a970c6 4781 max_pixclk = max(max_pixclk,
2f2d7aa1 4782 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4783 }
4784
4785 return max_pixclk;
4786}
4787
4788static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4789 unsigned *prepare_pipes)
30a970c6
JB
4790{
4791 struct drm_i915_private *dev_priv = dev->dev_private;
4792 struct intel_crtc *intel_crtc;
2f2d7aa1 4793 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4794
d60c4473
ID
4795 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4796 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4797 return;
4798
2f2d7aa1 4799 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4800 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4801 if (intel_crtc->base.enabled)
4802 *prepare_pipes |= (1 << intel_crtc->pipe);
4803}
4804
4805static void valleyview_modeset_global_resources(struct drm_device *dev)
4806{
4807 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4808 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4809 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4810
383c5a6a
VS
4811 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4812 if (IS_CHERRYVIEW(dev))
4813 cherryview_set_cdclk(dev, req_cdclk);
4814 else
4815 valleyview_set_cdclk(dev, req_cdclk);
4816 }
4817
77961eb9 4818 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4819}
4820
89b667f8
JB
4821static void valleyview_crtc_enable(struct drm_crtc *crtc)
4822{
4823 struct drm_device *dev = crtc->dev;
a72e4c9f 4824 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
4825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4826 struct intel_encoder *encoder;
4827 int pipe = intel_crtc->pipe;
23538ef1 4828 bool is_dsi;
89b667f8
JB
4829
4830 WARN_ON(!crtc->enabled);
4831
4832 if (intel_crtc->active)
4833 return;
4834
8525a235
SK
4835 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4836
1ae0d137
VS
4837 if (!is_dsi) {
4838 if (IS_CHERRYVIEW(dev))
4839 chv_prepare_pll(intel_crtc);
4840 else
4841 vlv_prepare_pll(intel_crtc);
4842 }
5b18e57c
DV
4843
4844 if (intel_crtc->config.has_dp_encoder)
4845 intel_dp_set_m_n(intel_crtc);
4846
4847 intel_set_pipe_timings(intel_crtc);
4848
5b18e57c
DV
4849 i9xx_set_pipeconf(intel_crtc);
4850
89b667f8 4851 intel_crtc->active = true;
89b667f8 4852
a72e4c9f 4853 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 4854
89b667f8
JB
4855 for_each_encoder_on_crtc(dev, crtc, encoder)
4856 if (encoder->pre_pll_enable)
4857 encoder->pre_pll_enable(encoder);
4858
9d556c99
CML
4859 if (!is_dsi) {
4860 if (IS_CHERRYVIEW(dev))
4861 chv_enable_pll(intel_crtc);
4862 else
4863 vlv_enable_pll(intel_crtc);
4864 }
89b667f8
JB
4865
4866 for_each_encoder_on_crtc(dev, crtc, encoder)
4867 if (encoder->pre_enable)
4868 encoder->pre_enable(encoder);
4869
2dd24552
JB
4870 i9xx_pfit_enable(intel_crtc);
4871
63cbb074
VS
4872 intel_crtc_load_lut(crtc);
4873
f37fcc2a 4874 intel_update_watermarks(crtc);
e1fdc473 4875 intel_enable_pipe(intel_crtc);
be6a6f8e 4876
5004945f
JN
4877 for_each_encoder_on_crtc(dev, crtc, encoder)
4878 encoder->enable(encoder);
9ab0460b 4879
4b3a9526
VS
4880 assert_vblank_disabled(crtc);
4881 drm_crtc_vblank_on(crtc);
4882
9ab0460b 4883 intel_crtc_enable_planes(crtc);
d40d9187 4884
56b80e1f 4885 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 4886 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
4887}
4888
f13c2ef3
DV
4889static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4890{
4891 struct drm_device *dev = crtc->base.dev;
4892 struct drm_i915_private *dev_priv = dev->dev_private;
4893
4894 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4895 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4896}
4897
0b8765c6 4898static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4899{
4900 struct drm_device *dev = crtc->dev;
a72e4c9f 4901 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 4902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4903 struct intel_encoder *encoder;
79e53945 4904 int pipe = intel_crtc->pipe;
79e53945 4905
08a48469
DV
4906 WARN_ON(!crtc->enabled);
4907
f7abfe8b
CW
4908 if (intel_crtc->active)
4909 return;
4910
f13c2ef3
DV
4911 i9xx_set_pll_dividers(intel_crtc);
4912
5b18e57c
DV
4913 if (intel_crtc->config.has_dp_encoder)
4914 intel_dp_set_m_n(intel_crtc);
4915
4916 intel_set_pipe_timings(intel_crtc);
4917
5b18e57c
DV
4918 i9xx_set_pipeconf(intel_crtc);
4919
f7abfe8b 4920 intel_crtc->active = true;
6b383a7f 4921
4a3436e8 4922 if (!IS_GEN2(dev))
a72e4c9f 4923 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 4924
9d6d9f19
MK
4925 for_each_encoder_on_crtc(dev, crtc, encoder)
4926 if (encoder->pre_enable)
4927 encoder->pre_enable(encoder);
4928
f6736a1a
DV
4929 i9xx_enable_pll(intel_crtc);
4930
2dd24552
JB
4931 i9xx_pfit_enable(intel_crtc);
4932
63cbb074
VS
4933 intel_crtc_load_lut(crtc);
4934
f37fcc2a 4935 intel_update_watermarks(crtc);
e1fdc473 4936 intel_enable_pipe(intel_crtc);
be6a6f8e 4937
fa5c73b1
DV
4938 for_each_encoder_on_crtc(dev, crtc, encoder)
4939 encoder->enable(encoder);
9ab0460b 4940
4b3a9526
VS
4941 assert_vblank_disabled(crtc);
4942 drm_crtc_vblank_on(crtc);
4943
9ab0460b 4944 intel_crtc_enable_planes(crtc);
d40d9187 4945
4a3436e8
VS
4946 /*
4947 * Gen2 reports pipe underruns whenever all planes are disabled.
4948 * So don't enable underrun reporting before at least some planes
4949 * are enabled.
4950 * FIXME: Need to fix the logic to work when we turn off all planes
4951 * but leave the pipe running.
4952 */
4953 if (IS_GEN2(dev))
a72e4c9f 4954 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 4955
56b80e1f 4956 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 4957 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 4958}
79e53945 4959
87476d63
DV
4960static void i9xx_pfit_disable(struct intel_crtc *crtc)
4961{
4962 struct drm_device *dev = crtc->base.dev;
4963 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4964
328d8e82
DV
4965 if (!crtc->config.gmch_pfit.control)
4966 return;
87476d63 4967
328d8e82 4968 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4969
328d8e82
DV
4970 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4971 I915_READ(PFIT_CONTROL));
4972 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4973}
4974
0b8765c6
JB
4975static void i9xx_crtc_disable(struct drm_crtc *crtc)
4976{
4977 struct drm_device *dev = crtc->dev;
4978 struct drm_i915_private *dev_priv = dev->dev_private;
4979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4980 struct intel_encoder *encoder;
0b8765c6 4981 int pipe = intel_crtc->pipe;
ef9c3aee 4982
f7abfe8b
CW
4983 if (!intel_crtc->active)
4984 return;
4985
4a3436e8
VS
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
4992 if (IS_GEN2(dev))
a72e4c9f 4993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 4994
564ed191
ID
4995 /*
4996 * Vblank time updates from the shadow to live plane control register
4997 * are blocked if the memory self-refresh mode is active at that
4998 * moment. So to make sure the plane gets truly disabled, disable
4999 * first the self-refresh mode. The self-refresh enable bit in turn
5000 * will be checked/applied by the HW only at the next frame start
5001 * event which is after the vblank start event, so we need to have a
5002 * wait-for-vblank between disabling the plane and the pipe.
5003 */
5004 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
5005 intel_crtc_disable_planes(crtc);
5006
6304cd91
VS
5007 /*
5008 * On gen2 planes are double buffered but the pipe isn't, so we must
5009 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5010 * We also need to wait on all gmch platforms because of the
5011 * self-refresh mode constraint explained above.
6304cd91 5012 */
564ed191 5013 intel_wait_for_vblank(dev, pipe);
6304cd91 5014
4b3a9526
VS
5015 drm_crtc_vblank_off(crtc);
5016 assert_vblank_disabled(crtc);
5017
5018 for_each_encoder_on_crtc(dev, crtc, encoder)
5019 encoder->disable(encoder);
5020
575f7ab7 5021 intel_disable_pipe(intel_crtc);
24a1f16d 5022
87476d63 5023 i9xx_pfit_disable(intel_crtc);
24a1f16d 5024
89b667f8
JB
5025 for_each_encoder_on_crtc(dev, crtc, encoder)
5026 if (encoder->post_disable)
5027 encoder->post_disable(encoder);
5028
076ed3b2
CML
5029 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
5030 if (IS_CHERRYVIEW(dev))
5031 chv_disable_pll(dev_priv, pipe);
5032 else if (IS_VALLEYVIEW(dev))
5033 vlv_disable_pll(dev_priv, pipe);
5034 else
1c4e0274 5035 i9xx_disable_pll(intel_crtc);
076ed3b2 5036 }
0b8765c6 5037
4a3436e8 5038 if (!IS_GEN2(dev))
a72e4c9f 5039 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5040
f7abfe8b 5041 intel_crtc->active = false;
46ba614c 5042 intel_update_watermarks(crtc);
f37fcc2a 5043
efa9624e 5044 mutex_lock(&dev->struct_mutex);
6b383a7f 5045 intel_update_fbc(dev);
efa9624e 5046 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5047}
5048
ee7b9f93
JB
5049static void i9xx_crtc_off(struct drm_crtc *crtc)
5050{
5051}
5052
976f8a20
DV
5053static void intel_crtc_update_sarea(struct drm_crtc *crtc,
5054 bool enabled)
2c07245f
ZW
5055{
5056 struct drm_device *dev = crtc->dev;
5057 struct drm_i915_master_private *master_priv;
5058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5059 int pipe = intel_crtc->pipe;
79e53945
JB
5060
5061 if (!dev->primary->master)
5062 return;
5063
5064 master_priv = dev->primary->master->driver_priv;
5065 if (!master_priv->sarea_priv)
5066 return;
5067
79e53945
JB
5068 switch (pipe) {
5069 case 0:
5070 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
5071 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
5072 break;
5073 case 1:
5074 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
5075 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
5076 break;
5077 default:
9db4a9c7 5078 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
5079 break;
5080 }
79e53945
JB
5081}
5082
b04c5bd6
BF
5083/* Master function to enable/disable CRTC and corresponding power wells */
5084void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5085{
5086 struct drm_device *dev = crtc->dev;
5087 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5089 enum intel_display_power_domain domain;
5090 unsigned long domains;
976f8a20 5091
0e572fe7
DV
5092 if (enable) {
5093 if (!intel_crtc->active) {
e1e9fb84
DV
5094 domains = get_crtc_power_domains(crtc);
5095 for_each_power_domain(domain, domains)
5096 intel_display_power_get(dev_priv, domain);
5097 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5098
5099 dev_priv->display.crtc_enable(crtc);
5100 }
5101 } else {
5102 if (intel_crtc->active) {
5103 dev_priv->display.crtc_disable(crtc);
5104
e1e9fb84
DV
5105 domains = intel_crtc->enabled_power_domains;
5106 for_each_power_domain(domain, domains)
5107 intel_display_power_put(dev_priv, domain);
5108 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5109 }
5110 }
b04c5bd6
BF
5111}
5112
5113/**
5114 * Sets the power management mode of the pipe and plane.
5115 */
5116void intel_crtc_update_dpms(struct drm_crtc *crtc)
5117{
5118 struct drm_device *dev = crtc->dev;
5119 struct intel_encoder *intel_encoder;
5120 bool enable = false;
5121
5122 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5123 enable |= intel_encoder->connectors_active;
5124
5125 intel_crtc_control(crtc, enable);
976f8a20
DV
5126
5127 intel_crtc_update_sarea(crtc, enable);
5128}
5129
cdd59983
CW
5130static void intel_crtc_disable(struct drm_crtc *crtc)
5131{
cdd59983 5132 struct drm_device *dev = crtc->dev;
976f8a20 5133 struct drm_connector *connector;
ee7b9f93 5134 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 5135 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 5136 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 5137
976f8a20
DV
5138 /* crtc should still be enabled when we disable it. */
5139 WARN_ON(!crtc->enabled);
5140
5141 dev_priv->display.crtc_disable(crtc);
5142 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
5143 dev_priv->display.off(crtc);
5144
f4510a27 5145 if (crtc->primary->fb) {
cdd59983 5146 mutex_lock(&dev->struct_mutex);
a071fa00
DV
5147 intel_unpin_fb_obj(old_obj);
5148 i915_gem_track_fb(old_obj, NULL,
5149 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 5150 mutex_unlock(&dev->struct_mutex);
f4510a27 5151 crtc->primary->fb = NULL;
976f8a20
DV
5152 }
5153
5154 /* Update computed state. */
5155 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5156 if (!connector->encoder || !connector->encoder->crtc)
5157 continue;
5158
5159 if (connector->encoder->crtc != crtc)
5160 continue;
5161
5162 connector->dpms = DRM_MODE_DPMS_OFF;
5163 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5164 }
5165}
5166
ea5b213a 5167void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5168{
4ef69c7a 5169 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5170
ea5b213a
CW
5171 drm_encoder_cleanup(encoder);
5172 kfree(intel_encoder);
7e7d76c3
JB
5173}
5174
9237329d 5175/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5176 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5177 * state of the entire output pipe. */
9237329d 5178static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5179{
5ab432ef
DV
5180 if (mode == DRM_MODE_DPMS_ON) {
5181 encoder->connectors_active = true;
5182
b2cabb0e 5183 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5184 } else {
5185 encoder->connectors_active = false;
5186
b2cabb0e 5187 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5188 }
79e53945
JB
5189}
5190
0a91ca29
DV
5191/* Cross check the actual hw state with our own modeset state tracking (and it's
5192 * internal consistency). */
b980514c 5193static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5194{
0a91ca29
DV
5195 if (connector->get_hw_state(connector)) {
5196 struct intel_encoder *encoder = connector->encoder;
5197 struct drm_crtc *crtc;
5198 bool encoder_enabled;
5199 enum pipe pipe;
5200
5201 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5202 connector->base.base.id,
c23cc417 5203 connector->base.name);
0a91ca29 5204
0e32b39c
DA
5205 /* there is no real hw state for MST connectors */
5206 if (connector->mst_port)
5207 return;
5208
0a91ca29
DV
5209 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5210 "wrong connector dpms state\n");
5211 WARN(connector->base.encoder != &encoder->base,
5212 "active connector not linked to encoder\n");
0a91ca29 5213
36cd7444
DA
5214 if (encoder) {
5215 WARN(!encoder->connectors_active,
5216 "encoder->connectors_active not set\n");
5217
5218 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5219 WARN(!encoder_enabled, "encoder not enabled\n");
5220 if (WARN_ON(!encoder->base.crtc))
5221 return;
0a91ca29 5222
36cd7444 5223 crtc = encoder->base.crtc;
0a91ca29 5224
36cd7444
DA
5225 WARN(!crtc->enabled, "crtc not enabled\n");
5226 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5227 WARN(pipe != to_intel_crtc(crtc)->pipe,
5228 "encoder active on the wrong pipe\n");
5229 }
0a91ca29 5230 }
79e53945
JB
5231}
5232
5ab432ef
DV
5233/* Even simpler default implementation, if there's really no special case to
5234 * consider. */
5235void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5236{
5ab432ef
DV
5237 /* All the simple cases only support two dpms states. */
5238 if (mode != DRM_MODE_DPMS_ON)
5239 mode = DRM_MODE_DPMS_OFF;
d4270e57 5240
5ab432ef
DV
5241 if (mode == connector->dpms)
5242 return;
5243
5244 connector->dpms = mode;
5245
5246 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5247 if (connector->encoder)
5248 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5249
b980514c 5250 intel_modeset_check_state(connector->dev);
79e53945
JB
5251}
5252
f0947c37
DV
5253/* Simple connector->get_hw_state implementation for encoders that support only
5254 * one connector and no cloning and hence the encoder state determines the state
5255 * of the connector. */
5256bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5257{
24929352 5258 enum pipe pipe = 0;
f0947c37 5259 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5260
f0947c37 5261 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5262}
5263
1857e1da
DV
5264static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5265 struct intel_crtc_config *pipe_config)
5266{
5267 struct drm_i915_private *dev_priv = dev->dev_private;
5268 struct intel_crtc *pipe_B_crtc =
5269 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5270
5271 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5272 pipe_name(pipe), pipe_config->fdi_lanes);
5273 if (pipe_config->fdi_lanes > 4) {
5274 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5275 pipe_name(pipe), pipe_config->fdi_lanes);
5276 return false;
5277 }
5278
bafb6553 5279 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5280 if (pipe_config->fdi_lanes > 2) {
5281 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5282 pipe_config->fdi_lanes);
5283 return false;
5284 } else {
5285 return true;
5286 }
5287 }
5288
5289 if (INTEL_INFO(dev)->num_pipes == 2)
5290 return true;
5291
5292 /* Ivybridge 3 pipe is really complicated */
5293 switch (pipe) {
5294 case PIPE_A:
5295 return true;
5296 case PIPE_B:
5297 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5298 pipe_config->fdi_lanes > 2) {
5299 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5300 pipe_name(pipe), pipe_config->fdi_lanes);
5301 return false;
5302 }
5303 return true;
5304 case PIPE_C:
1e833f40 5305 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5306 pipe_B_crtc->config.fdi_lanes <= 2) {
5307 if (pipe_config->fdi_lanes > 2) {
5308 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5309 pipe_name(pipe), pipe_config->fdi_lanes);
5310 return false;
5311 }
5312 } else {
5313 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5314 return false;
5315 }
5316 return true;
5317 default:
5318 BUG();
5319 }
5320}
5321
e29c22c0
DV
5322#define RETRY 1
5323static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5324 struct intel_crtc_config *pipe_config)
877d48d5 5325{
1857e1da 5326 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5327 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5328 int lane, link_bw, fdi_dotclock;
e29c22c0 5329 bool setup_ok, needs_recompute = false;
877d48d5 5330
e29c22c0 5331retry:
877d48d5
DV
5332 /* FDI is a binary signal running at ~2.7GHz, encoding
5333 * each output octet as 10 bits. The actual frequency
5334 * is stored as a divider into a 100MHz clock, and the
5335 * mode pixel clock is stored in units of 1KHz.
5336 * Hence the bw of each lane in terms of the mode signal
5337 * is:
5338 */
5339 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5340
241bfc38 5341 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5342
2bd89a07 5343 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5344 pipe_config->pipe_bpp);
5345
5346 pipe_config->fdi_lanes = lane;
5347
2bd89a07 5348 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5349 link_bw, &pipe_config->fdi_m_n);
1857e1da 5350
e29c22c0
DV
5351 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5352 intel_crtc->pipe, pipe_config);
5353 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5354 pipe_config->pipe_bpp -= 2*3;
5355 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5356 pipe_config->pipe_bpp);
5357 needs_recompute = true;
5358 pipe_config->bw_constrained = true;
5359
5360 goto retry;
5361 }
5362
5363 if (needs_recompute)
5364 return RETRY;
5365
5366 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5367}
5368
42db64ef
PZ
5369static void hsw_compute_ips_config(struct intel_crtc *crtc,
5370 struct intel_crtc_config *pipe_config)
5371{
d330a953 5372 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5373 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5374 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5375}
5376
a43f6e0f 5377static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5378 struct intel_crtc_config *pipe_config)
79e53945 5379{
a43f6e0f 5380 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5381 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5382
ad3a4479 5383 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5384 if (INTEL_INFO(dev)->gen < 4) {
5385 struct drm_i915_private *dev_priv = dev->dev_private;
5386 int clock_limit =
5387 dev_priv->display.get_display_clock_speed(dev);
5388
5389 /*
5390 * Enable pixel doubling when the dot clock
5391 * is > 90% of the (display) core speed.
5392 *
b397c96b
VS
5393 * GDG double wide on either pipe,
5394 * otherwise pipe A only.
cf532bb2 5395 */
b397c96b 5396 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5397 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5398 clock_limit *= 2;
cf532bb2 5399 pipe_config->double_wide = true;
ad3a4479
VS
5400 }
5401
241bfc38 5402 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5403 return -EINVAL;
2c07245f 5404 }
89749350 5405
1d1d0e27
VS
5406 /*
5407 * Pipe horizontal size must be even in:
5408 * - DVO ganged mode
5409 * - LVDS dual channel mode
5410 * - Double wide pipe
5411 */
5412 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5413 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5414 pipe_config->pipe_src_w &= ~1;
5415
8693a824
DL
5416 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5417 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5418 */
5419 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5420 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5421 return -EINVAL;
44f46b42 5422
bd080ee5 5423 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5424 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5425 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5426 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5427 * for lvds. */
5428 pipe_config->pipe_bpp = 8*3;
5429 }
5430
f5adf94e 5431 if (HAS_IPS(dev))
a43f6e0f
DV
5432 hsw_compute_ips_config(crtc, pipe_config);
5433
12030431
DV
5434 /*
5435 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5436 * old clock survives for now.
5437 */
5438 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5439 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5440
877d48d5 5441 if (pipe_config->has_pch_encoder)
a43f6e0f 5442 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5443
e29c22c0 5444 return 0;
79e53945
JB
5445}
5446
25eb05fc
JB
5447static int valleyview_get_display_clock_speed(struct drm_device *dev)
5448{
d197b7d3
VS
5449 struct drm_i915_private *dev_priv = dev->dev_private;
5450 int vco = valleyview_get_vco(dev_priv);
5451 u32 val;
5452 int divider;
5453
d49a340d
VS
5454 /* FIXME: Punit isn't quite ready yet */
5455 if (IS_CHERRYVIEW(dev))
5456 return 400000;
5457
d197b7d3
VS
5458 mutex_lock(&dev_priv->dpio_lock);
5459 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5460 mutex_unlock(&dev_priv->dpio_lock);
5461
5462 divider = val & DISPLAY_FREQUENCY_VALUES;
5463
7d007f40
VS
5464 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5465 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5466 "cdclk change in progress\n");
5467
d197b7d3 5468 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5469}
5470
e70236a8
JB
5471static int i945_get_display_clock_speed(struct drm_device *dev)
5472{
5473 return 400000;
5474}
79e53945 5475
e70236a8 5476static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5477{
e70236a8
JB
5478 return 333000;
5479}
79e53945 5480
e70236a8
JB
5481static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5482{
5483 return 200000;
5484}
79e53945 5485
257a7ffc
DV
5486static int pnv_get_display_clock_speed(struct drm_device *dev)
5487{
5488 u16 gcfgc = 0;
5489
5490 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5491
5492 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5493 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5494 return 267000;
5495 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5496 return 333000;
5497 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5498 return 444000;
5499 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5500 return 200000;
5501 default:
5502 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5503 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5504 return 133000;
5505 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5506 return 167000;
5507 }
5508}
5509
e70236a8
JB
5510static int i915gm_get_display_clock_speed(struct drm_device *dev)
5511{
5512 u16 gcfgc = 0;
79e53945 5513
e70236a8
JB
5514 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5515
5516 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5517 return 133000;
5518 else {
5519 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5520 case GC_DISPLAY_CLOCK_333_MHZ:
5521 return 333000;
5522 default:
5523 case GC_DISPLAY_CLOCK_190_200_MHZ:
5524 return 190000;
79e53945 5525 }
e70236a8
JB
5526 }
5527}
5528
5529static int i865_get_display_clock_speed(struct drm_device *dev)
5530{
5531 return 266000;
5532}
5533
5534static int i855_get_display_clock_speed(struct drm_device *dev)
5535{
5536 u16 hpllcc = 0;
5537 /* Assume that the hardware is in the high speed state. This
5538 * should be the default.
5539 */
5540 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5541 case GC_CLOCK_133_200:
5542 case GC_CLOCK_100_200:
5543 return 200000;
5544 case GC_CLOCK_166_250:
5545 return 250000;
5546 case GC_CLOCK_100_133:
79e53945 5547 return 133000;
e70236a8 5548 }
79e53945 5549
e70236a8
JB
5550 /* Shouldn't happen */
5551 return 0;
5552}
79e53945 5553
e70236a8
JB
5554static int i830_get_display_clock_speed(struct drm_device *dev)
5555{
5556 return 133000;
79e53945
JB
5557}
5558
2c07245f 5559static void
a65851af 5560intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5561{
a65851af
VS
5562 while (*num > DATA_LINK_M_N_MASK ||
5563 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5564 *num >>= 1;
5565 *den >>= 1;
5566 }
5567}
5568
a65851af
VS
5569static void compute_m_n(unsigned int m, unsigned int n,
5570 uint32_t *ret_m, uint32_t *ret_n)
5571{
5572 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5573 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5574 intel_reduce_m_n_ratio(ret_m, ret_n);
5575}
5576
e69d0bc1
DV
5577void
5578intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5579 int pixel_clock, int link_clock,
5580 struct intel_link_m_n *m_n)
2c07245f 5581{
e69d0bc1 5582 m_n->tu = 64;
a65851af
VS
5583
5584 compute_m_n(bits_per_pixel * pixel_clock,
5585 link_clock * nlanes * 8,
5586 &m_n->gmch_m, &m_n->gmch_n);
5587
5588 compute_m_n(pixel_clock, link_clock,
5589 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5590}
5591
a7615030
CW
5592static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5593{
d330a953
JN
5594 if (i915.panel_use_ssc >= 0)
5595 return i915.panel_use_ssc != 0;
41aa3448 5596 return dev_priv->vbt.lvds_use_ssc
435793df 5597 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5598}
5599
c65d77d8
JB
5600static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5601{
5602 struct drm_device *dev = crtc->dev;
5603 struct drm_i915_private *dev_priv = dev->dev_private;
5604 int refclk;
5605
a0c4da24 5606 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5607 refclk = 100000;
a0c4da24 5608 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5609 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5610 refclk = dev_priv->vbt.lvds_ssc_freq;
5611 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5612 } else if (!IS_GEN2(dev)) {
5613 refclk = 96000;
5614 } else {
5615 refclk = 48000;
5616 }
5617
5618 return refclk;
5619}
5620
7429e9d4 5621static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5622{
7df00d7a 5623 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5624}
f47709a9 5625
7429e9d4
DV
5626static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5627{
5628 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5629}
5630
f47709a9 5631static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5632 intel_clock_t *reduced_clock)
5633{
f47709a9 5634 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5635 u32 fp, fp2 = 0;
5636
5637 if (IS_PINEVIEW(dev)) {
7429e9d4 5638 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5639 if (reduced_clock)
7429e9d4 5640 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5641 } else {
7429e9d4 5642 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5643 if (reduced_clock)
7429e9d4 5644 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5645 }
5646
8bcc2795 5647 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5648
f47709a9
DV
5649 crtc->lowfreq_avail = false;
5650 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5651 reduced_clock && i915.powersave) {
8bcc2795 5652 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5653 crtc->lowfreq_avail = true;
a7516a05 5654 } else {
8bcc2795 5655 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5656 }
5657}
5658
5e69f97f
CML
5659static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5660 pipe)
89b667f8
JB
5661{
5662 u32 reg_val;
5663
5664 /*
5665 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5666 * and set it to a reasonable value instead.
5667 */
ab3c759a 5668 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5669 reg_val &= 0xffffff00;
5670 reg_val |= 0x00000030;
ab3c759a 5671 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5672
ab3c759a 5673 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5674 reg_val &= 0x8cffffff;
5675 reg_val = 0x8c000000;
ab3c759a 5676 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5677
ab3c759a 5678 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5679 reg_val &= 0xffffff00;
ab3c759a 5680 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5681
ab3c759a 5682 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5683 reg_val &= 0x00ffffff;
5684 reg_val |= 0xb0000000;
ab3c759a 5685 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5686}
5687
b551842d
DV
5688static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5689 struct intel_link_m_n *m_n)
5690{
5691 struct drm_device *dev = crtc->base.dev;
5692 struct drm_i915_private *dev_priv = dev->dev_private;
5693 int pipe = crtc->pipe;
5694
e3b95f1e
DV
5695 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5696 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5697 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5698 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5699}
5700
5701static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5702 struct intel_link_m_n *m_n,
5703 struct intel_link_m_n *m2_n2)
b551842d
DV
5704{
5705 struct drm_device *dev = crtc->base.dev;
5706 struct drm_i915_private *dev_priv = dev->dev_private;
5707 int pipe = crtc->pipe;
5708 enum transcoder transcoder = crtc->config.cpu_transcoder;
5709
5710 if (INTEL_INFO(dev)->gen >= 5) {
5711 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5712 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5713 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5714 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5715 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5716 * for gen < 8) and if DRRS is supported (to make sure the
5717 * registers are not unnecessarily accessed).
5718 */
5719 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5720 crtc->config.has_drrs) {
5721 I915_WRITE(PIPE_DATA_M2(transcoder),
5722 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5723 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5724 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5725 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5726 }
b551842d 5727 } else {
e3b95f1e
DV
5728 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5729 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5730 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5731 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5732 }
5733}
5734
f769cd24 5735void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5736{
5737 if (crtc->config.has_pch_encoder)
5738 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5739 else
f769cd24
VK
5740 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5741 &crtc->config.dp_m2_n2);
03afc4a2
DV
5742}
5743
f47709a9 5744static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5745{
5746 u32 dpll, dpll_md;
5747
5748 /*
5749 * Enable DPIO clock input. We should never disable the reference
5750 * clock for pipe B, since VGA hotplug / manual detection depends
5751 * on it.
5752 */
5753 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5754 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5755 /* We should never disable this, set it here for state tracking */
5756 if (crtc->pipe == PIPE_B)
5757 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5758 dpll |= DPLL_VCO_ENABLE;
5759 crtc->config.dpll_hw_state.dpll = dpll;
5760
5761 dpll_md = (crtc->config.pixel_multiplier - 1)
5762 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5763 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5764}
5765
5766static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5767{
f47709a9 5768 struct drm_device *dev = crtc->base.dev;
a0c4da24 5769 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5770 int pipe = crtc->pipe;
bdd4b6a6 5771 u32 mdiv;
a0c4da24 5772 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5773 u32 coreclk, reg_val;
a0c4da24 5774
09153000
DV
5775 mutex_lock(&dev_priv->dpio_lock);
5776
f47709a9
DV
5777 bestn = crtc->config.dpll.n;
5778 bestm1 = crtc->config.dpll.m1;
5779 bestm2 = crtc->config.dpll.m2;
5780 bestp1 = crtc->config.dpll.p1;
5781 bestp2 = crtc->config.dpll.p2;
a0c4da24 5782
89b667f8
JB
5783 /* See eDP HDMI DPIO driver vbios notes doc */
5784
5785 /* PLL B needs special handling */
bdd4b6a6 5786 if (pipe == PIPE_B)
5e69f97f 5787 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5788
5789 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5790 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5791
5792 /* Disable target IRef on PLL */
ab3c759a 5793 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5794 reg_val &= 0x00ffffff;
ab3c759a 5795 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5796
5797 /* Disable fast lock */
ab3c759a 5798 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5799
5800 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5801 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5802 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5803 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5804 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5805
5806 /*
5807 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5808 * but we don't support that).
5809 * Note: don't use the DAC post divider as it seems unstable.
5810 */
5811 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5812 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5813
a0c4da24 5814 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5815 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5816
89b667f8 5817 /* Set HBR and RBR LPF coefficients */
ff9a6750 5818 if (crtc->config.port_clock == 162000 ||
99750bd4 5819 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5820 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5821 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5822 0x009f0003);
89b667f8 5823 else
ab3c759a 5824 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5825 0x00d0000f);
5826
5827 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5828 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5829 /* Use SSC source */
bdd4b6a6 5830 if (pipe == PIPE_A)
ab3c759a 5831 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5832 0x0df40000);
5833 else
ab3c759a 5834 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5835 0x0df70000);
5836 } else { /* HDMI or VGA */
5837 /* Use bend source */
bdd4b6a6 5838 if (pipe == PIPE_A)
ab3c759a 5839 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5840 0x0df70000);
5841 else
ab3c759a 5842 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5843 0x0df40000);
5844 }
a0c4da24 5845
ab3c759a 5846 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5847 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5848 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5849 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5850 coreclk |= 0x01000000;
ab3c759a 5851 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5852
ab3c759a 5853 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5854 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5855}
5856
9d556c99 5857static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5858{
5859 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5860 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5861 DPLL_VCO_ENABLE;
5862 if (crtc->pipe != PIPE_A)
5863 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5864
5865 crtc->config.dpll_hw_state.dpll_md =
5866 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5867}
5868
5869static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5870{
5871 struct drm_device *dev = crtc->base.dev;
5872 struct drm_i915_private *dev_priv = dev->dev_private;
5873 int pipe = crtc->pipe;
5874 int dpll_reg = DPLL(crtc->pipe);
5875 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5876 u32 loopfilter, intcoeff;
9d556c99
CML
5877 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5878 int refclk;
5879
9d556c99
CML
5880 bestn = crtc->config.dpll.n;
5881 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5882 bestm1 = crtc->config.dpll.m1;
5883 bestm2 = crtc->config.dpll.m2 >> 22;
5884 bestp1 = crtc->config.dpll.p1;
5885 bestp2 = crtc->config.dpll.p2;
5886
5887 /*
5888 * Enable Refclk and SSC
5889 */
a11b0703
VS
5890 I915_WRITE(dpll_reg,
5891 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5892
5893 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5894
9d556c99
CML
5895 /* p1 and p2 divider */
5896 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5897 5 << DPIO_CHV_S1_DIV_SHIFT |
5898 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5899 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5900 1 << DPIO_CHV_K_DIV_SHIFT);
5901
5902 /* Feedback post-divider - m2 */
5903 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5904
5905 /* Feedback refclk divider - n and m1 */
5906 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5907 DPIO_CHV_M1_DIV_BY_2 |
5908 1 << DPIO_CHV_N_DIV_SHIFT);
5909
5910 /* M2 fraction division */
5911 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5912
5913 /* M2 fraction division enable */
5914 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5915 DPIO_CHV_FRAC_DIV_EN |
5916 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5917
5918 /* Loop filter */
5919 refclk = i9xx_get_refclk(&crtc->base, 0);
5920 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5921 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5922 if (refclk == 100000)
5923 intcoeff = 11;
5924 else if (refclk == 38400)
5925 intcoeff = 10;
5926 else
5927 intcoeff = 9;
5928 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5929 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5930
5931 /* AFC Recal */
5932 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5933 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5934 DPIO_AFC_RECAL);
5935
5936 mutex_unlock(&dev_priv->dpio_lock);
5937}
5938
f47709a9
DV
5939static void i9xx_update_pll(struct intel_crtc *crtc,
5940 intel_clock_t *reduced_clock,
eb1cbe48
DV
5941 int num_connectors)
5942{
f47709a9 5943 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5944 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5945 u32 dpll;
5946 bool is_sdvo;
f47709a9 5947 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5948
f47709a9 5949 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5950
f47709a9
DV
5951 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5952 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5953
5954 dpll = DPLL_VGA_MODE_DIS;
5955
f47709a9 5956 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5957 dpll |= DPLLB_MODE_LVDS;
5958 else
5959 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5960
ef1b460d 5961 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5962 dpll |= (crtc->config.pixel_multiplier - 1)
5963 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5964 }
198a037f
DV
5965
5966 if (is_sdvo)
4a33e48d 5967 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5968
f47709a9 5969 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5970 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5971
5972 /* compute bitmask from p1 value */
5973 if (IS_PINEVIEW(dev))
5974 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5975 else {
5976 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5977 if (IS_G4X(dev) && reduced_clock)
5978 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5979 }
5980 switch (clock->p2) {
5981 case 5:
5982 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5983 break;
5984 case 7:
5985 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5986 break;
5987 case 10:
5988 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5989 break;
5990 case 14:
5991 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5992 break;
5993 }
5994 if (INTEL_INFO(dev)->gen >= 4)
5995 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5996
09ede541 5997 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5998 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5999 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6000 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6001 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6002 else
6003 dpll |= PLL_REF_INPUT_DREFCLK;
6004
6005 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
6006 crtc->config.dpll_hw_state.dpll = dpll;
6007
eb1cbe48 6008 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
6009 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
6010 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 6011 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6012 }
6013}
6014
f47709a9 6015static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 6016 intel_clock_t *reduced_clock,
eb1cbe48
DV
6017 int num_connectors)
6018{
f47709a9 6019 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6020 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6021 u32 dpll;
f47709a9 6022 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 6023
f47709a9 6024 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 6025
eb1cbe48
DV
6026 dpll = DPLL_VGA_MODE_DIS;
6027
f47709a9 6028 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6029 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6030 } else {
6031 if (clock->p1 == 2)
6032 dpll |= PLL_P1_DIVIDE_BY_TWO;
6033 else
6034 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6035 if (clock->p2 == 4)
6036 dpll |= PLL_P2_DIVIDE_BY_4;
6037 }
6038
1c4e0274 6039 if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4a33e48d
DV
6040 dpll |= DPLL_DVO_2X_MODE;
6041
f47709a9 6042 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6043 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6044 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6045 else
6046 dpll |= PLL_REF_INPUT_DREFCLK;
6047
6048 dpll |= DPLL_VCO_ENABLE;
8bcc2795 6049 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6050}
6051
8a654f3b 6052static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6053{
6054 struct drm_device *dev = intel_crtc->base.dev;
6055 struct drm_i915_private *dev_priv = dev->dev_private;
6056 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6057 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
6058 struct drm_display_mode *adjusted_mode =
6059 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
6060 uint32_t crtc_vtotal, crtc_vblank_end;
6061 int vsyncshift = 0;
4d8a62ea
DV
6062
6063 /* We need to be careful not to changed the adjusted mode, for otherwise
6064 * the hw state checker will get angry at the mismatch. */
6065 crtc_vtotal = adjusted_mode->crtc_vtotal;
6066 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6067
609aeaca 6068 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6069 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6070 crtc_vtotal -= 1;
6071 crtc_vblank_end -= 1;
609aeaca
VS
6072
6073 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6074 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6075 else
6076 vsyncshift = adjusted_mode->crtc_hsync_start -
6077 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6078 if (vsyncshift < 0)
6079 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6080 }
6081
6082 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6083 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6084
fe2b8f9d 6085 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6086 (adjusted_mode->crtc_hdisplay - 1) |
6087 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6088 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6089 (adjusted_mode->crtc_hblank_start - 1) |
6090 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6091 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6092 (adjusted_mode->crtc_hsync_start - 1) |
6093 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6094
fe2b8f9d 6095 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6096 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6097 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6098 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6099 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6100 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6101 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6102 (adjusted_mode->crtc_vsync_start - 1) |
6103 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6104
b5e508d4
PZ
6105 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6106 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6107 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6108 * bits. */
6109 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6110 (pipe == PIPE_B || pipe == PIPE_C))
6111 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6112
b0e77b9c
PZ
6113 /* pipesrc controls the size that is scaled from, which should
6114 * always be the user's requested size.
6115 */
6116 I915_WRITE(PIPESRC(pipe),
37327abd
VS
6117 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6118 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
6119}
6120
1bd1bd80
DV
6121static void intel_get_pipe_timings(struct intel_crtc *crtc,
6122 struct intel_crtc_config *pipe_config)
6123{
6124 struct drm_device *dev = crtc->base.dev;
6125 struct drm_i915_private *dev_priv = dev->dev_private;
6126 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6127 uint32_t tmp;
6128
6129 tmp = I915_READ(HTOTAL(cpu_transcoder));
6130 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6131 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6132 tmp = I915_READ(HBLANK(cpu_transcoder));
6133 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6134 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6135 tmp = I915_READ(HSYNC(cpu_transcoder));
6136 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6137 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6138
6139 tmp = I915_READ(VTOTAL(cpu_transcoder));
6140 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6141 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6142 tmp = I915_READ(VBLANK(cpu_transcoder));
6143 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6144 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6145 tmp = I915_READ(VSYNC(cpu_transcoder));
6146 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6147 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6148
6149 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6150 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6151 pipe_config->adjusted_mode.crtc_vtotal += 1;
6152 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6153 }
6154
6155 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6156 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6157 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6158
6159 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6160 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6161}
6162
f6a83288
DV
6163void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6164 struct intel_crtc_config *pipe_config)
babea61d 6165{
f6a83288
DV
6166 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6167 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6168 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6169 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6170
f6a83288
DV
6171 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6172 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6173 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6174 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6175
f6a83288 6176 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6177
f6a83288
DV
6178 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6179 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6180}
6181
84b046f3
DV
6182static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6183{
6184 struct drm_device *dev = intel_crtc->base.dev;
6185 struct drm_i915_private *dev_priv = dev->dev_private;
6186 uint32_t pipeconf;
6187
9f11a9e4 6188 pipeconf = 0;
84b046f3 6189
b6b5d049
VS
6190 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6191 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6192 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6193
cf532bb2
VS
6194 if (intel_crtc->config.double_wide)
6195 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6196
ff9ce46e
DV
6197 /* only g4x and later have fancy bpc/dither controls */
6198 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6199 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6200 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6201 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6202 PIPECONF_DITHER_TYPE_SP;
84b046f3 6203
ff9ce46e
DV
6204 switch (intel_crtc->config.pipe_bpp) {
6205 case 18:
6206 pipeconf |= PIPECONF_6BPC;
6207 break;
6208 case 24:
6209 pipeconf |= PIPECONF_8BPC;
6210 break;
6211 case 30:
6212 pipeconf |= PIPECONF_10BPC;
6213 break;
6214 default:
6215 /* Case prevented by intel_choose_pipe_bpp_dither. */
6216 BUG();
84b046f3
DV
6217 }
6218 }
6219
6220 if (HAS_PIPE_CXSR(dev)) {
6221 if (intel_crtc->lowfreq_avail) {
6222 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6223 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6224 } else {
6225 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6226 }
6227 }
6228
efc2cfff
VS
6229 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6230 if (INTEL_INFO(dev)->gen < 4 ||
6231 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6232 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6233 else
6234 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6235 } else
84b046f3
DV
6236 pipeconf |= PIPECONF_PROGRESSIVE;
6237
9f11a9e4
DV
6238 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6239 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6240
84b046f3
DV
6241 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6242 POSTING_READ(PIPECONF(intel_crtc->pipe));
6243}
6244
f564048e 6245static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6246 int x, int y,
94352cf9 6247 struct drm_framebuffer *fb)
79e53945
JB
6248{
6249 struct drm_device *dev = crtc->dev;
6250 struct drm_i915_private *dev_priv = dev->dev_private;
6251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6252 int refclk, num_connectors = 0;
652c393a 6253 intel_clock_t clock, reduced_clock;
a16af721 6254 bool ok, has_reduced_clock = false;
e9fd1c02 6255 bool is_lvds = false, is_dsi = false;
5eddb70b 6256 struct intel_encoder *encoder;
d4906093 6257 const intel_limit_t *limit;
79e53945 6258
6c2b7c12 6259 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6260 switch (encoder->type) {
79e53945
JB
6261 case INTEL_OUTPUT_LVDS:
6262 is_lvds = true;
6263 break;
e9fd1c02
JN
6264 case INTEL_OUTPUT_DSI:
6265 is_dsi = true;
6266 break;
79e53945 6267 }
43565a06 6268
c751ce4f 6269 num_connectors++;
79e53945
JB
6270 }
6271
f2335330 6272 if (is_dsi)
5b18e57c 6273 return 0;
f2335330
JN
6274
6275 if (!intel_crtc->config.clock_set) {
6276 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6277
e9fd1c02
JN
6278 /*
6279 * Returns a set of divisors for the desired target clock with
6280 * the given refclk, or FALSE. The returned values represent
6281 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6282 * 2) / p1 / p2.
6283 */
6284 limit = intel_limit(crtc, refclk);
6285 ok = dev_priv->display.find_dpll(limit, crtc,
6286 intel_crtc->config.port_clock,
6287 refclk, NULL, &clock);
f2335330 6288 if (!ok) {
e9fd1c02
JN
6289 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6290 return -EINVAL;
6291 }
79e53945 6292
f2335330
JN
6293 if (is_lvds && dev_priv->lvds_downclock_avail) {
6294 /*
6295 * Ensure we match the reduced clock's P to the target
6296 * clock. If the clocks don't match, we can't switch
6297 * the display clock by using the FP0/FP1. In such case
6298 * we will disable the LVDS downclock feature.
6299 */
6300 has_reduced_clock =
6301 dev_priv->display.find_dpll(limit, crtc,
6302 dev_priv->lvds_downclock,
6303 refclk, &clock,
6304 &reduced_clock);
6305 }
6306 /* Compat-code for transition, will disappear. */
f47709a9
DV
6307 intel_crtc->config.dpll.n = clock.n;
6308 intel_crtc->config.dpll.m1 = clock.m1;
6309 intel_crtc->config.dpll.m2 = clock.m2;
6310 intel_crtc->config.dpll.p1 = clock.p1;
6311 intel_crtc->config.dpll.p2 = clock.p2;
6312 }
7026d4ac 6313
e9fd1c02 6314 if (IS_GEN2(dev)) {
8a654f3b 6315 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6316 has_reduced_clock ? &reduced_clock : NULL,
6317 num_connectors);
9d556c99
CML
6318 } else if (IS_CHERRYVIEW(dev)) {
6319 chv_update_pll(intel_crtc);
e9fd1c02 6320 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6321 vlv_update_pll(intel_crtc);
e9fd1c02 6322 } else {
f47709a9 6323 i9xx_update_pll(intel_crtc,
eb1cbe48 6324 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6325 num_connectors);
e9fd1c02 6326 }
79e53945 6327
c8f7a0db 6328 return 0;
f564048e
EA
6329}
6330
2fa2fe9a
DV
6331static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6332 struct intel_crtc_config *pipe_config)
6333{
6334 struct drm_device *dev = crtc->base.dev;
6335 struct drm_i915_private *dev_priv = dev->dev_private;
6336 uint32_t tmp;
6337
dc9e7dec
VS
6338 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6339 return;
6340
2fa2fe9a 6341 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6342 if (!(tmp & PFIT_ENABLE))
6343 return;
2fa2fe9a 6344
06922821 6345 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6346 if (INTEL_INFO(dev)->gen < 4) {
6347 if (crtc->pipe != PIPE_B)
6348 return;
2fa2fe9a
DV
6349 } else {
6350 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6351 return;
6352 }
6353
06922821 6354 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6355 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6356 if (INTEL_INFO(dev)->gen < 5)
6357 pipe_config->gmch_pfit.lvds_border_bits =
6358 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6359}
6360
acbec814
JB
6361static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6362 struct intel_crtc_config *pipe_config)
6363{
6364 struct drm_device *dev = crtc->base.dev;
6365 struct drm_i915_private *dev_priv = dev->dev_private;
6366 int pipe = pipe_config->cpu_transcoder;
6367 intel_clock_t clock;
6368 u32 mdiv;
662c6ecb 6369 int refclk = 100000;
acbec814 6370
f573de5a
SK
6371 /* In case of MIPI DPLL will not even be used */
6372 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6373 return;
6374
acbec814 6375 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6376 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6377 mutex_unlock(&dev_priv->dpio_lock);
6378
6379 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6380 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6381 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6382 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6383 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6384
f646628b 6385 vlv_clock(refclk, &clock);
acbec814 6386
f646628b
VS
6387 /* clock.dot is the fast clock */
6388 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6389}
6390
1ad292b5
JB
6391static void i9xx_get_plane_config(struct intel_crtc *crtc,
6392 struct intel_plane_config *plane_config)
6393{
6394 struct drm_device *dev = crtc->base.dev;
6395 struct drm_i915_private *dev_priv = dev->dev_private;
6396 u32 val, base, offset;
6397 int pipe = crtc->pipe, plane = crtc->plane;
6398 int fourcc, pixel_format;
6399 int aligned_height;
6400
66e514c1
DA
6401 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6402 if (!crtc->base.primary->fb) {
1ad292b5
JB
6403 DRM_DEBUG_KMS("failed to alloc fb\n");
6404 return;
6405 }
6406
6407 val = I915_READ(DSPCNTR(plane));
6408
6409 if (INTEL_INFO(dev)->gen >= 4)
6410 if (val & DISPPLANE_TILED)
6411 plane_config->tiled = true;
6412
6413 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6414 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6415 crtc->base.primary->fb->pixel_format = fourcc;
6416 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6417 drm_format_plane_cpp(fourcc, 0) * 8;
6418
6419 if (INTEL_INFO(dev)->gen >= 4) {
6420 if (plane_config->tiled)
6421 offset = I915_READ(DSPTILEOFF(plane));
6422 else
6423 offset = I915_READ(DSPLINOFF(plane));
6424 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6425 } else {
6426 base = I915_READ(DSPADDR(plane));
6427 }
6428 plane_config->base = base;
6429
6430 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6431 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6432 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6433
6434 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6435 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6436
66e514c1 6437 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6438 plane_config->tiled);
6439
1267a26b
FF
6440 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6441 aligned_height);
1ad292b5
JB
6442
6443 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6444 pipe, plane, crtc->base.primary->fb->width,
6445 crtc->base.primary->fb->height,
6446 crtc->base.primary->fb->bits_per_pixel, base,
6447 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6448 plane_config->size);
6449
6450}
6451
70b23a98
VS
6452static void chv_crtc_clock_get(struct intel_crtc *crtc,
6453 struct intel_crtc_config *pipe_config)
6454{
6455 struct drm_device *dev = crtc->base.dev;
6456 struct drm_i915_private *dev_priv = dev->dev_private;
6457 int pipe = pipe_config->cpu_transcoder;
6458 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6459 intel_clock_t clock;
6460 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6461 int refclk = 100000;
6462
6463 mutex_lock(&dev_priv->dpio_lock);
6464 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6465 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6466 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6467 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6468 mutex_unlock(&dev_priv->dpio_lock);
6469
6470 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6471 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6472 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6473 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6474 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6475
6476 chv_clock(refclk, &clock);
6477
6478 /* clock.dot is the fast clock */
6479 pipe_config->port_clock = clock.dot / 5;
6480}
6481
0e8ffe1b
DV
6482static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6483 struct intel_crtc_config *pipe_config)
6484{
6485 struct drm_device *dev = crtc->base.dev;
6486 struct drm_i915_private *dev_priv = dev->dev_private;
6487 uint32_t tmp;
6488
f458ebbc
DV
6489 if (!intel_display_power_is_enabled(dev_priv,
6490 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
6491 return false;
6492
e143a21c 6493 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6494 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6495
0e8ffe1b
DV
6496 tmp = I915_READ(PIPECONF(crtc->pipe));
6497 if (!(tmp & PIPECONF_ENABLE))
6498 return false;
6499
42571aef
VS
6500 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6501 switch (tmp & PIPECONF_BPC_MASK) {
6502 case PIPECONF_6BPC:
6503 pipe_config->pipe_bpp = 18;
6504 break;
6505 case PIPECONF_8BPC:
6506 pipe_config->pipe_bpp = 24;
6507 break;
6508 case PIPECONF_10BPC:
6509 pipe_config->pipe_bpp = 30;
6510 break;
6511 default:
6512 break;
6513 }
6514 }
6515
b5a9fa09
DV
6516 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6517 pipe_config->limited_color_range = true;
6518
282740f7
VS
6519 if (INTEL_INFO(dev)->gen < 4)
6520 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6521
1bd1bd80
DV
6522 intel_get_pipe_timings(crtc, pipe_config);
6523
2fa2fe9a
DV
6524 i9xx_get_pfit_config(crtc, pipe_config);
6525
6c49f241
DV
6526 if (INTEL_INFO(dev)->gen >= 4) {
6527 tmp = I915_READ(DPLL_MD(crtc->pipe));
6528 pipe_config->pixel_multiplier =
6529 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6530 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6531 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6532 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6533 tmp = I915_READ(DPLL(crtc->pipe));
6534 pipe_config->pixel_multiplier =
6535 ((tmp & SDVO_MULTIPLIER_MASK)
6536 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6537 } else {
6538 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6539 * port and will be fixed up in the encoder->get_config
6540 * function. */
6541 pipe_config->pixel_multiplier = 1;
6542 }
8bcc2795
DV
6543 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6544 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6545 /*
6546 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6547 * on 830. Filter it out here so that we don't
6548 * report errors due to that.
6549 */
6550 if (IS_I830(dev))
6551 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6552
8bcc2795
DV
6553 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6554 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6555 } else {
6556 /* Mask out read-only status bits. */
6557 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6558 DPLL_PORTC_READY_MASK |
6559 DPLL_PORTB_READY_MASK);
8bcc2795 6560 }
6c49f241 6561
70b23a98
VS
6562 if (IS_CHERRYVIEW(dev))
6563 chv_crtc_clock_get(crtc, pipe_config);
6564 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6565 vlv_crtc_clock_get(crtc, pipe_config);
6566 else
6567 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6568
0e8ffe1b
DV
6569 return true;
6570}
6571
dde86e2d 6572static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6573{
6574 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6575 struct intel_encoder *encoder;
74cfd7ac 6576 u32 val, final;
13d83a67 6577 bool has_lvds = false;
199e5d79 6578 bool has_cpu_edp = false;
199e5d79 6579 bool has_panel = false;
99eb6a01
KP
6580 bool has_ck505 = false;
6581 bool can_ssc = false;
13d83a67
JB
6582
6583 /* We need to take the global config into account */
b2784e15 6584 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6585 switch (encoder->type) {
6586 case INTEL_OUTPUT_LVDS:
6587 has_panel = true;
6588 has_lvds = true;
6589 break;
6590 case INTEL_OUTPUT_EDP:
6591 has_panel = true;
2de6905f 6592 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6593 has_cpu_edp = true;
6594 break;
13d83a67
JB
6595 }
6596 }
6597
99eb6a01 6598 if (HAS_PCH_IBX(dev)) {
41aa3448 6599 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6600 can_ssc = has_ck505;
6601 } else {
6602 has_ck505 = false;
6603 can_ssc = true;
6604 }
6605
2de6905f
ID
6606 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6607 has_panel, has_lvds, has_ck505);
13d83a67
JB
6608
6609 /* Ironlake: try to setup display ref clock before DPLL
6610 * enabling. This is only under driver's control after
6611 * PCH B stepping, previous chipset stepping should be
6612 * ignoring this setting.
6613 */
74cfd7ac
CW
6614 val = I915_READ(PCH_DREF_CONTROL);
6615
6616 /* As we must carefully and slowly disable/enable each source in turn,
6617 * compute the final state we want first and check if we need to
6618 * make any changes at all.
6619 */
6620 final = val;
6621 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6622 if (has_ck505)
6623 final |= DREF_NONSPREAD_CK505_ENABLE;
6624 else
6625 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6626
6627 final &= ~DREF_SSC_SOURCE_MASK;
6628 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6629 final &= ~DREF_SSC1_ENABLE;
6630
6631 if (has_panel) {
6632 final |= DREF_SSC_SOURCE_ENABLE;
6633
6634 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6635 final |= DREF_SSC1_ENABLE;
6636
6637 if (has_cpu_edp) {
6638 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6639 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6640 else
6641 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6642 } else
6643 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6644 } else {
6645 final |= DREF_SSC_SOURCE_DISABLE;
6646 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6647 }
6648
6649 if (final == val)
6650 return;
6651
13d83a67 6652 /* Always enable nonspread source */
74cfd7ac 6653 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6654
99eb6a01 6655 if (has_ck505)
74cfd7ac 6656 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6657 else
74cfd7ac 6658 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6659
199e5d79 6660 if (has_panel) {
74cfd7ac
CW
6661 val &= ~DREF_SSC_SOURCE_MASK;
6662 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6663
199e5d79 6664 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6665 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6666 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6667 val |= DREF_SSC1_ENABLE;
e77166b5 6668 } else
74cfd7ac 6669 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6670
6671 /* Get SSC going before enabling the outputs */
74cfd7ac 6672 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6673 POSTING_READ(PCH_DREF_CONTROL);
6674 udelay(200);
6675
74cfd7ac 6676 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6677
6678 /* Enable CPU source on CPU attached eDP */
199e5d79 6679 if (has_cpu_edp) {
99eb6a01 6680 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6681 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6682 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6683 } else
74cfd7ac 6684 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6685 } else
74cfd7ac 6686 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6687
74cfd7ac 6688 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6689 POSTING_READ(PCH_DREF_CONTROL);
6690 udelay(200);
6691 } else {
6692 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6693
74cfd7ac 6694 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6695
6696 /* Turn off CPU output */
74cfd7ac 6697 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6698
74cfd7ac 6699 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6700 POSTING_READ(PCH_DREF_CONTROL);
6701 udelay(200);
6702
6703 /* Turn off the SSC source */
74cfd7ac
CW
6704 val &= ~DREF_SSC_SOURCE_MASK;
6705 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6706
6707 /* Turn off SSC1 */
74cfd7ac 6708 val &= ~DREF_SSC1_ENABLE;
199e5d79 6709
74cfd7ac 6710 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6711 POSTING_READ(PCH_DREF_CONTROL);
6712 udelay(200);
6713 }
74cfd7ac
CW
6714
6715 BUG_ON(val != final);
13d83a67
JB
6716}
6717
f31f2d55 6718static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6719{
f31f2d55 6720 uint32_t tmp;
dde86e2d 6721
0ff066a9
PZ
6722 tmp = I915_READ(SOUTH_CHICKEN2);
6723 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6724 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6725
0ff066a9
PZ
6726 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6727 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6728 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6729
0ff066a9
PZ
6730 tmp = I915_READ(SOUTH_CHICKEN2);
6731 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6732 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6733
0ff066a9
PZ
6734 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6735 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6736 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6737}
6738
6739/* WaMPhyProgramming:hsw */
6740static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6741{
6742 uint32_t tmp;
dde86e2d
PZ
6743
6744 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6745 tmp &= ~(0xFF << 24);
6746 tmp |= (0x12 << 24);
6747 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6748
dde86e2d
PZ
6749 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6750 tmp |= (1 << 11);
6751 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6752
6753 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6754 tmp |= (1 << 11);
6755 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6756
dde86e2d
PZ
6757 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6758 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6759 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6760
6761 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6762 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6763 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6764
0ff066a9
PZ
6765 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6766 tmp &= ~(7 << 13);
6767 tmp |= (5 << 13);
6768 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6769
0ff066a9
PZ
6770 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6771 tmp &= ~(7 << 13);
6772 tmp |= (5 << 13);
6773 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6774
6775 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6776 tmp &= ~0xFF;
6777 tmp |= 0x1C;
6778 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6779
6780 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6781 tmp &= ~0xFF;
6782 tmp |= 0x1C;
6783 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6784
6785 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6786 tmp &= ~(0xFF << 16);
6787 tmp |= (0x1C << 16);
6788 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6789
6790 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6791 tmp &= ~(0xFF << 16);
6792 tmp |= (0x1C << 16);
6793 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6794
0ff066a9
PZ
6795 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6796 tmp |= (1 << 27);
6797 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6798
0ff066a9
PZ
6799 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6800 tmp |= (1 << 27);
6801 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6802
0ff066a9
PZ
6803 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6804 tmp &= ~(0xF << 28);
6805 tmp |= (4 << 28);
6806 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6807
0ff066a9
PZ
6808 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6809 tmp &= ~(0xF << 28);
6810 tmp |= (4 << 28);
6811 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6812}
6813
2fa86a1f
PZ
6814/* Implements 3 different sequences from BSpec chapter "Display iCLK
6815 * Programming" based on the parameters passed:
6816 * - Sequence to enable CLKOUT_DP
6817 * - Sequence to enable CLKOUT_DP without spread
6818 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6819 */
6820static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6821 bool with_fdi)
f31f2d55
PZ
6822{
6823 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6824 uint32_t reg, tmp;
6825
6826 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6827 with_spread = true;
6828 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6829 with_fdi, "LP PCH doesn't have FDI\n"))
6830 with_fdi = false;
f31f2d55
PZ
6831
6832 mutex_lock(&dev_priv->dpio_lock);
6833
6834 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6835 tmp &= ~SBI_SSCCTL_DISABLE;
6836 tmp |= SBI_SSCCTL_PATHALT;
6837 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6838
6839 udelay(24);
6840
2fa86a1f
PZ
6841 if (with_spread) {
6842 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6843 tmp &= ~SBI_SSCCTL_PATHALT;
6844 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6845
2fa86a1f
PZ
6846 if (with_fdi) {
6847 lpt_reset_fdi_mphy(dev_priv);
6848 lpt_program_fdi_mphy(dev_priv);
6849 }
6850 }
dde86e2d 6851
2fa86a1f
PZ
6852 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6853 SBI_GEN0 : SBI_DBUFF0;
6854 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6855 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6856 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6857
6858 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6859}
6860
47701c3b
PZ
6861/* Sequence to disable CLKOUT_DP */
6862static void lpt_disable_clkout_dp(struct drm_device *dev)
6863{
6864 struct drm_i915_private *dev_priv = dev->dev_private;
6865 uint32_t reg, tmp;
6866
6867 mutex_lock(&dev_priv->dpio_lock);
6868
6869 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6870 SBI_GEN0 : SBI_DBUFF0;
6871 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6872 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6873 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6874
6875 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6876 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6877 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6878 tmp |= SBI_SSCCTL_PATHALT;
6879 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6880 udelay(32);
6881 }
6882 tmp |= SBI_SSCCTL_DISABLE;
6883 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6884 }
6885
6886 mutex_unlock(&dev_priv->dpio_lock);
6887}
6888
bf8fa3d3
PZ
6889static void lpt_init_pch_refclk(struct drm_device *dev)
6890{
bf8fa3d3
PZ
6891 struct intel_encoder *encoder;
6892 bool has_vga = false;
6893
b2784e15 6894 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6895 switch (encoder->type) {
6896 case INTEL_OUTPUT_ANALOG:
6897 has_vga = true;
6898 break;
6899 }
6900 }
6901
47701c3b
PZ
6902 if (has_vga)
6903 lpt_enable_clkout_dp(dev, true, true);
6904 else
6905 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6906}
6907
dde86e2d
PZ
6908/*
6909 * Initialize reference clocks when the driver loads
6910 */
6911void intel_init_pch_refclk(struct drm_device *dev)
6912{
6913 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6914 ironlake_init_pch_refclk(dev);
6915 else if (HAS_PCH_LPT(dev))
6916 lpt_init_pch_refclk(dev);
6917}
6918
d9d444cb
JB
6919static int ironlake_get_refclk(struct drm_crtc *crtc)
6920{
6921 struct drm_device *dev = crtc->dev;
6922 struct drm_i915_private *dev_priv = dev->dev_private;
6923 struct intel_encoder *encoder;
d9d444cb
JB
6924 int num_connectors = 0;
6925 bool is_lvds = false;
6926
6c2b7c12 6927 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6928 switch (encoder->type) {
6929 case INTEL_OUTPUT_LVDS:
6930 is_lvds = true;
6931 break;
d9d444cb
JB
6932 }
6933 num_connectors++;
6934 }
6935
6936 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6937 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6938 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6939 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6940 }
6941
6942 return 120000;
6943}
6944
6ff93609 6945static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6946{
c8203565 6947 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6949 int pipe = intel_crtc->pipe;
c8203565
PZ
6950 uint32_t val;
6951
78114071 6952 val = 0;
c8203565 6953
965e0c48 6954 switch (intel_crtc->config.pipe_bpp) {
c8203565 6955 case 18:
dfd07d72 6956 val |= PIPECONF_6BPC;
c8203565
PZ
6957 break;
6958 case 24:
dfd07d72 6959 val |= PIPECONF_8BPC;
c8203565
PZ
6960 break;
6961 case 30:
dfd07d72 6962 val |= PIPECONF_10BPC;
c8203565
PZ
6963 break;
6964 case 36:
dfd07d72 6965 val |= PIPECONF_12BPC;
c8203565
PZ
6966 break;
6967 default:
cc769b62
PZ
6968 /* Case prevented by intel_choose_pipe_bpp_dither. */
6969 BUG();
c8203565
PZ
6970 }
6971
d8b32247 6972 if (intel_crtc->config.dither)
c8203565
PZ
6973 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6974
6ff93609 6975 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6976 val |= PIPECONF_INTERLACED_ILK;
6977 else
6978 val |= PIPECONF_PROGRESSIVE;
6979
50f3b016 6980 if (intel_crtc->config.limited_color_range)
3685a8f3 6981 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6982
c8203565
PZ
6983 I915_WRITE(PIPECONF(pipe), val);
6984 POSTING_READ(PIPECONF(pipe));
6985}
6986
86d3efce
VS
6987/*
6988 * Set up the pipe CSC unit.
6989 *
6990 * Currently only full range RGB to limited range RGB conversion
6991 * is supported, but eventually this should handle various
6992 * RGB<->YCbCr scenarios as well.
6993 */
50f3b016 6994static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6995{
6996 struct drm_device *dev = crtc->dev;
6997 struct drm_i915_private *dev_priv = dev->dev_private;
6998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6999 int pipe = intel_crtc->pipe;
7000 uint16_t coeff = 0x7800; /* 1.0 */
7001
7002 /*
7003 * TODO: Check what kind of values actually come out of the pipe
7004 * with these coeff/postoff values and adjust to get the best
7005 * accuracy. Perhaps we even need to take the bpc value into
7006 * consideration.
7007 */
7008
50f3b016 7009 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7010 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7011
7012 /*
7013 * GY/GU and RY/RU should be the other way around according
7014 * to BSpec, but reality doesn't agree. Just set them up in
7015 * a way that results in the correct picture.
7016 */
7017 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7018 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7019
7020 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7021 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7022
7023 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7024 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7025
7026 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7027 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7028 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7029
7030 if (INTEL_INFO(dev)->gen > 6) {
7031 uint16_t postoff = 0;
7032
50f3b016 7033 if (intel_crtc->config.limited_color_range)
32cf0cb0 7034 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7035
7036 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7037 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7038 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7039
7040 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7041 } else {
7042 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7043
50f3b016 7044 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7045 mode |= CSC_BLACK_SCREEN_OFFSET;
7046
7047 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7048 }
7049}
7050
6ff93609 7051static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7052{
756f85cf
PZ
7053 struct drm_device *dev = crtc->dev;
7054 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7056 enum pipe pipe = intel_crtc->pipe;
3b117c8f 7057 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
7058 uint32_t val;
7059
3eff4faa 7060 val = 0;
ee2b0b38 7061
756f85cf 7062 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
7063 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7064
6ff93609 7065 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7066 val |= PIPECONF_INTERLACED_ILK;
7067 else
7068 val |= PIPECONF_PROGRESSIVE;
7069
702e7a56
PZ
7070 I915_WRITE(PIPECONF(cpu_transcoder), val);
7071 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7072
7073 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7074 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7075
3cdf122c 7076 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7077 val = 0;
7078
7079 switch (intel_crtc->config.pipe_bpp) {
7080 case 18:
7081 val |= PIPEMISC_DITHER_6_BPC;
7082 break;
7083 case 24:
7084 val |= PIPEMISC_DITHER_8_BPC;
7085 break;
7086 case 30:
7087 val |= PIPEMISC_DITHER_10_BPC;
7088 break;
7089 case 36:
7090 val |= PIPEMISC_DITHER_12_BPC;
7091 break;
7092 default:
7093 /* Case prevented by pipe_config_set_bpp. */
7094 BUG();
7095 }
7096
7097 if (intel_crtc->config.dither)
7098 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7099
7100 I915_WRITE(PIPEMISC(pipe), val);
7101 }
ee2b0b38
PZ
7102}
7103
6591c6e4 7104static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
7105 intel_clock_t *clock,
7106 bool *has_reduced_clock,
7107 intel_clock_t *reduced_clock)
7108{
7109 struct drm_device *dev = crtc->dev;
7110 struct drm_i915_private *dev_priv = dev->dev_private;
7111 struct intel_encoder *intel_encoder;
7112 int refclk;
d4906093 7113 const intel_limit_t *limit;
a16af721 7114 bool ret, is_lvds = false;
79e53945 7115
6591c6e4
PZ
7116 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7117 switch (intel_encoder->type) {
79e53945
JB
7118 case INTEL_OUTPUT_LVDS:
7119 is_lvds = true;
7120 break;
79e53945
JB
7121 }
7122 }
7123
d9d444cb 7124 refclk = ironlake_get_refclk(crtc);
79e53945 7125
d4906093
ML
7126 /*
7127 * Returns a set of divisors for the desired target clock with the given
7128 * refclk, or FALSE. The returned values represent the clock equation:
7129 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7130 */
1b894b59 7131 limit = intel_limit(crtc, refclk);
ff9a6750
DV
7132 ret = dev_priv->display.find_dpll(limit, crtc,
7133 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 7134 refclk, NULL, clock);
6591c6e4
PZ
7135 if (!ret)
7136 return false;
cda4b7d3 7137
ddc9003c 7138 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7139 /*
7140 * Ensure we match the reduced clock's P to the target clock.
7141 * If the clocks don't match, we can't switch the display clock
7142 * by using the FP0/FP1. In such case we will disable the LVDS
7143 * downclock feature.
7144 */
ee9300bb
DV
7145 *has_reduced_clock =
7146 dev_priv->display.find_dpll(limit, crtc,
7147 dev_priv->lvds_downclock,
7148 refclk, clock,
7149 reduced_clock);
652c393a 7150 }
61e9653f 7151
6591c6e4
PZ
7152 return true;
7153}
7154
d4b1931c
PZ
7155int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7156{
7157 /*
7158 * Account for spread spectrum to avoid
7159 * oversubscribing the link. Max center spread
7160 * is 2.5%; use 5% for safety's sake.
7161 */
7162 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7163 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7164}
7165
7429e9d4 7166static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7167{
7429e9d4 7168 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7169}
7170
de13a2e3 7171static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7172 u32 *fp,
9a7c7890 7173 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7174{
de13a2e3 7175 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7176 struct drm_device *dev = crtc->dev;
7177 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7178 struct intel_encoder *intel_encoder;
7179 uint32_t dpll;
6cc5f341 7180 int factor, num_connectors = 0;
09ede541 7181 bool is_lvds = false, is_sdvo = false;
79e53945 7182
de13a2e3
PZ
7183 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7184 switch (intel_encoder->type) {
79e53945
JB
7185 case INTEL_OUTPUT_LVDS:
7186 is_lvds = true;
7187 break;
7188 case INTEL_OUTPUT_SDVO:
7d57382e 7189 case INTEL_OUTPUT_HDMI:
79e53945 7190 is_sdvo = true;
79e53945 7191 break;
79e53945 7192 }
43565a06 7193
c751ce4f 7194 num_connectors++;
79e53945 7195 }
79e53945 7196
c1858123 7197 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7198 factor = 21;
7199 if (is_lvds) {
7200 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7201 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7202 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7203 factor = 25;
09ede541 7204 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7205 factor = 20;
c1858123 7206
7429e9d4 7207 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7208 *fp |= FP_CB_TUNE;
2c07245f 7209
9a7c7890
DV
7210 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7211 *fp2 |= FP_CB_TUNE;
7212
5eddb70b 7213 dpll = 0;
2c07245f 7214
a07d6787
EA
7215 if (is_lvds)
7216 dpll |= DPLLB_MODE_LVDS;
7217 else
7218 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7219
ef1b460d
DV
7220 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7221 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7222
7223 if (is_sdvo)
4a33e48d 7224 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7225 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7226 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7227
a07d6787 7228 /* compute bitmask from p1 value */
7429e9d4 7229 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7230 /* also FPA1 */
7429e9d4 7231 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7232
7429e9d4 7233 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7234 case 5:
7235 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7236 break;
7237 case 7:
7238 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7239 break;
7240 case 10:
7241 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7242 break;
7243 case 14:
7244 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7245 break;
79e53945
JB
7246 }
7247
b4c09f3b 7248 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7249 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7250 else
7251 dpll |= PLL_REF_INPUT_DREFCLK;
7252
959e16d6 7253 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7254}
7255
7256static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7257 int x, int y,
7258 struct drm_framebuffer *fb)
7259{
7260 struct drm_device *dev = crtc->dev;
de13a2e3 7261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7262 int num_connectors = 0;
7263 intel_clock_t clock, reduced_clock;
cbbab5bd 7264 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7265 bool ok, has_reduced_clock = false;
8b47047b 7266 bool is_lvds = false;
de13a2e3 7267 struct intel_encoder *encoder;
e2b78267 7268 struct intel_shared_dpll *pll;
de13a2e3
PZ
7269
7270 for_each_encoder_on_crtc(dev, crtc, encoder) {
7271 switch (encoder->type) {
7272 case INTEL_OUTPUT_LVDS:
7273 is_lvds = true;
7274 break;
de13a2e3
PZ
7275 }
7276
7277 num_connectors++;
a07d6787 7278 }
79e53945 7279
5dc5298b
PZ
7280 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7281 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7282
ff9a6750 7283 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7284 &has_reduced_clock, &reduced_clock);
ee9300bb 7285 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7286 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7287 return -EINVAL;
79e53945 7288 }
f47709a9
DV
7289 /* Compat-code for transition, will disappear. */
7290 if (!intel_crtc->config.clock_set) {
7291 intel_crtc->config.dpll.n = clock.n;
7292 intel_crtc->config.dpll.m1 = clock.m1;
7293 intel_crtc->config.dpll.m2 = clock.m2;
7294 intel_crtc->config.dpll.p1 = clock.p1;
7295 intel_crtc->config.dpll.p2 = clock.p2;
7296 }
79e53945 7297
5dc5298b 7298 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7299 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7300 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7301 if (has_reduced_clock)
7429e9d4 7302 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7303
7429e9d4 7304 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7305 &fp, &reduced_clock,
7306 has_reduced_clock ? &fp2 : NULL);
7307
959e16d6 7308 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7309 intel_crtc->config.dpll_hw_state.fp0 = fp;
7310 if (has_reduced_clock)
7311 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7312 else
7313 intel_crtc->config.dpll_hw_state.fp1 = fp;
7314
b89a1d39 7315 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7316 if (pll == NULL) {
84f44ce7 7317 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7318 pipe_name(intel_crtc->pipe));
4b645f14
JB
7319 return -EINVAL;
7320 }
ee7b9f93 7321 } else
e72f9fbf 7322 intel_put_shared_dpll(intel_crtc);
79e53945 7323
d330a953 7324 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7325 intel_crtc->lowfreq_avail = true;
7326 else
7327 intel_crtc->lowfreq_avail = false;
e2b78267 7328
c8f7a0db 7329 return 0;
79e53945
JB
7330}
7331
eb14cb74
VS
7332static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7333 struct intel_link_m_n *m_n)
7334{
7335 struct drm_device *dev = crtc->base.dev;
7336 struct drm_i915_private *dev_priv = dev->dev_private;
7337 enum pipe pipe = crtc->pipe;
7338
7339 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7340 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7341 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7342 & ~TU_SIZE_MASK;
7343 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7344 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7345 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7346}
7347
7348static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7349 enum transcoder transcoder,
b95af8be
VK
7350 struct intel_link_m_n *m_n,
7351 struct intel_link_m_n *m2_n2)
72419203
DV
7352{
7353 struct drm_device *dev = crtc->base.dev;
7354 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7355 enum pipe pipe = crtc->pipe;
72419203 7356
eb14cb74
VS
7357 if (INTEL_INFO(dev)->gen >= 5) {
7358 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7359 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7360 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7361 & ~TU_SIZE_MASK;
7362 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7363 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7364 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7365 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7366 * gen < 8) and if DRRS is supported (to make sure the
7367 * registers are not unnecessarily read).
7368 */
7369 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7370 crtc->config.has_drrs) {
7371 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7372 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7373 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7374 & ~TU_SIZE_MASK;
7375 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7376 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7377 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7378 }
eb14cb74
VS
7379 } else {
7380 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7381 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7382 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7383 & ~TU_SIZE_MASK;
7384 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7385 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7386 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7387 }
7388}
7389
7390void intel_dp_get_m_n(struct intel_crtc *crtc,
7391 struct intel_crtc_config *pipe_config)
7392{
7393 if (crtc->config.has_pch_encoder)
7394 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7395 else
7396 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7397 &pipe_config->dp_m_n,
7398 &pipe_config->dp_m2_n2);
eb14cb74 7399}
72419203 7400
eb14cb74
VS
7401static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7402 struct intel_crtc_config *pipe_config)
7403{
7404 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7405 &pipe_config->fdi_m_n, NULL);
72419203
DV
7406}
7407
2fa2fe9a
DV
7408static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7409 struct intel_crtc_config *pipe_config)
7410{
7411 struct drm_device *dev = crtc->base.dev;
7412 struct drm_i915_private *dev_priv = dev->dev_private;
7413 uint32_t tmp;
7414
7415 tmp = I915_READ(PF_CTL(crtc->pipe));
7416
7417 if (tmp & PF_ENABLE) {
fd4daa9c 7418 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7419 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7420 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7421
7422 /* We currently do not free assignements of panel fitters on
7423 * ivb/hsw (since we don't use the higher upscaling modes which
7424 * differentiates them) so just WARN about this case for now. */
7425 if (IS_GEN7(dev)) {
7426 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7427 PF_PIPE_SEL_IVB(crtc->pipe));
7428 }
2fa2fe9a 7429 }
79e53945
JB
7430}
7431
4c6baa59
JB
7432static void ironlake_get_plane_config(struct intel_crtc *crtc,
7433 struct intel_plane_config *plane_config)
7434{
7435 struct drm_device *dev = crtc->base.dev;
7436 struct drm_i915_private *dev_priv = dev->dev_private;
7437 u32 val, base, offset;
7438 int pipe = crtc->pipe, plane = crtc->plane;
7439 int fourcc, pixel_format;
7440 int aligned_height;
7441
66e514c1
DA
7442 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7443 if (!crtc->base.primary->fb) {
4c6baa59
JB
7444 DRM_DEBUG_KMS("failed to alloc fb\n");
7445 return;
7446 }
7447
7448 val = I915_READ(DSPCNTR(plane));
7449
7450 if (INTEL_INFO(dev)->gen >= 4)
7451 if (val & DISPPLANE_TILED)
7452 plane_config->tiled = true;
7453
7454 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7455 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7456 crtc->base.primary->fb->pixel_format = fourcc;
7457 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7458 drm_format_plane_cpp(fourcc, 0) * 8;
7459
7460 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7461 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7462 offset = I915_READ(DSPOFFSET(plane));
7463 } else {
7464 if (plane_config->tiled)
7465 offset = I915_READ(DSPTILEOFF(plane));
7466 else
7467 offset = I915_READ(DSPLINOFF(plane));
7468 }
7469 plane_config->base = base;
7470
7471 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7472 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7473 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7474
7475 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7476 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7477
66e514c1 7478 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7479 plane_config->tiled);
7480
1267a26b
FF
7481 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7482 aligned_height);
4c6baa59
JB
7483
7484 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7485 pipe, plane, crtc->base.primary->fb->width,
7486 crtc->base.primary->fb->height,
7487 crtc->base.primary->fb->bits_per_pixel, base,
7488 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7489 plane_config->size);
7490}
7491
0e8ffe1b
DV
7492static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7493 struct intel_crtc_config *pipe_config)
7494{
7495 struct drm_device *dev = crtc->base.dev;
7496 struct drm_i915_private *dev_priv = dev->dev_private;
7497 uint32_t tmp;
7498
f458ebbc
DV
7499 if (!intel_display_power_is_enabled(dev_priv,
7500 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
7501 return false;
7502
e143a21c 7503 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7504 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7505
0e8ffe1b
DV
7506 tmp = I915_READ(PIPECONF(crtc->pipe));
7507 if (!(tmp & PIPECONF_ENABLE))
7508 return false;
7509
42571aef
VS
7510 switch (tmp & PIPECONF_BPC_MASK) {
7511 case PIPECONF_6BPC:
7512 pipe_config->pipe_bpp = 18;
7513 break;
7514 case PIPECONF_8BPC:
7515 pipe_config->pipe_bpp = 24;
7516 break;
7517 case PIPECONF_10BPC:
7518 pipe_config->pipe_bpp = 30;
7519 break;
7520 case PIPECONF_12BPC:
7521 pipe_config->pipe_bpp = 36;
7522 break;
7523 default:
7524 break;
7525 }
7526
b5a9fa09
DV
7527 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7528 pipe_config->limited_color_range = true;
7529
ab9412ba 7530 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7531 struct intel_shared_dpll *pll;
7532
88adfff1
DV
7533 pipe_config->has_pch_encoder = true;
7534
627eb5a3
DV
7535 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7536 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7537 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7538
7539 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7540
c0d43d62 7541 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7542 pipe_config->shared_dpll =
7543 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7544 } else {
7545 tmp = I915_READ(PCH_DPLL_SEL);
7546 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7547 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7548 else
7549 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7550 }
66e985c0
DV
7551
7552 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7553
7554 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7555 &pipe_config->dpll_hw_state));
c93f54cf
DV
7556
7557 tmp = pipe_config->dpll_hw_state.dpll;
7558 pipe_config->pixel_multiplier =
7559 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7560 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7561
7562 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7563 } else {
7564 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7565 }
7566
1bd1bd80
DV
7567 intel_get_pipe_timings(crtc, pipe_config);
7568
2fa2fe9a
DV
7569 ironlake_get_pfit_config(crtc, pipe_config);
7570
0e8ffe1b
DV
7571 return true;
7572}
7573
be256dc7
PZ
7574static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7575{
7576 struct drm_device *dev = dev_priv->dev;
be256dc7 7577 struct intel_crtc *crtc;
be256dc7 7578
d3fcc808 7579 for_each_intel_crtc(dev, crtc)
798183c5 7580 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7581 pipe_name(crtc->pipe));
7582
7583 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7584 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7585 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7586 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7587 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7588 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7589 "CPU PWM1 enabled\n");
c5107b87
PZ
7590 if (IS_HASWELL(dev))
7591 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7592 "CPU PWM2 enabled\n");
be256dc7
PZ
7593 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7594 "PCH PWM1 enabled\n");
7595 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7596 "Utility pin enabled\n");
7597 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7598
9926ada1
PZ
7599 /*
7600 * In theory we can still leave IRQs enabled, as long as only the HPD
7601 * interrupts remain enabled. We used to check for that, but since it's
7602 * gen-specific and since we only disable LCPLL after we fully disable
7603 * the interrupts, the check below should be enough.
7604 */
9df7575f 7605 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7606}
7607
9ccd5aeb
PZ
7608static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7609{
7610 struct drm_device *dev = dev_priv->dev;
7611
7612 if (IS_HASWELL(dev))
7613 return I915_READ(D_COMP_HSW);
7614 else
7615 return I915_READ(D_COMP_BDW);
7616}
7617
3c4c9b81
PZ
7618static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7619{
7620 struct drm_device *dev = dev_priv->dev;
7621
7622 if (IS_HASWELL(dev)) {
7623 mutex_lock(&dev_priv->rps.hw_lock);
7624 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7625 val))
f475dadf 7626 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7627 mutex_unlock(&dev_priv->rps.hw_lock);
7628 } else {
9ccd5aeb
PZ
7629 I915_WRITE(D_COMP_BDW, val);
7630 POSTING_READ(D_COMP_BDW);
3c4c9b81 7631 }
be256dc7
PZ
7632}
7633
7634/*
7635 * This function implements pieces of two sequences from BSpec:
7636 * - Sequence for display software to disable LCPLL
7637 * - Sequence for display software to allow package C8+
7638 * The steps implemented here are just the steps that actually touch the LCPLL
7639 * register. Callers should take care of disabling all the display engine
7640 * functions, doing the mode unset, fixing interrupts, etc.
7641 */
6ff58d53
PZ
7642static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7643 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7644{
7645 uint32_t val;
7646
7647 assert_can_disable_lcpll(dev_priv);
7648
7649 val = I915_READ(LCPLL_CTL);
7650
7651 if (switch_to_fclk) {
7652 val |= LCPLL_CD_SOURCE_FCLK;
7653 I915_WRITE(LCPLL_CTL, val);
7654
7655 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7656 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7657 DRM_ERROR("Switching to FCLK failed\n");
7658
7659 val = I915_READ(LCPLL_CTL);
7660 }
7661
7662 val |= LCPLL_PLL_DISABLE;
7663 I915_WRITE(LCPLL_CTL, val);
7664 POSTING_READ(LCPLL_CTL);
7665
7666 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7667 DRM_ERROR("LCPLL still locked\n");
7668
9ccd5aeb 7669 val = hsw_read_dcomp(dev_priv);
be256dc7 7670 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7671 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7672 ndelay(100);
7673
9ccd5aeb
PZ
7674 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7675 1))
be256dc7
PZ
7676 DRM_ERROR("D_COMP RCOMP still in progress\n");
7677
7678 if (allow_power_down) {
7679 val = I915_READ(LCPLL_CTL);
7680 val |= LCPLL_POWER_DOWN_ALLOW;
7681 I915_WRITE(LCPLL_CTL, val);
7682 POSTING_READ(LCPLL_CTL);
7683 }
7684}
7685
7686/*
7687 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7688 * source.
7689 */
6ff58d53 7690static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7691{
7692 uint32_t val;
7693
7694 val = I915_READ(LCPLL_CTL);
7695
7696 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7697 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7698 return;
7699
a8a8bd54
PZ
7700 /*
7701 * Make sure we're not on PC8 state before disabling PC8, otherwise
7702 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7703 *
7704 * The other problem is that hsw_restore_lcpll() is called as part of
7705 * the runtime PM resume sequence, so we can't just call
7706 * gen6_gt_force_wake_get() because that function calls
7707 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7708 * while we are on the resume sequence. So to solve this problem we have
7709 * to call special forcewake code that doesn't touch runtime PM and
7710 * doesn't enable the forcewake delayed work.
7711 */
d2e40e27 7712 spin_lock_irq(&dev_priv->uncore.lock);
a8a8bd54
PZ
7713 if (dev_priv->uncore.forcewake_count++ == 0)
7714 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
d2e40e27 7715 spin_unlock_irq(&dev_priv->uncore.lock);
215733fa 7716
be256dc7
PZ
7717 if (val & LCPLL_POWER_DOWN_ALLOW) {
7718 val &= ~LCPLL_POWER_DOWN_ALLOW;
7719 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7720 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7721 }
7722
9ccd5aeb 7723 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7724 val |= D_COMP_COMP_FORCE;
7725 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7726 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7727
7728 val = I915_READ(LCPLL_CTL);
7729 val &= ~LCPLL_PLL_DISABLE;
7730 I915_WRITE(LCPLL_CTL, val);
7731
7732 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7733 DRM_ERROR("LCPLL not locked yet\n");
7734
7735 if (val & LCPLL_CD_SOURCE_FCLK) {
7736 val = I915_READ(LCPLL_CTL);
7737 val &= ~LCPLL_CD_SOURCE_FCLK;
7738 I915_WRITE(LCPLL_CTL, val);
7739
7740 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7741 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7742 DRM_ERROR("Switching back to LCPLL failed\n");
7743 }
215733fa 7744
a8a8bd54 7745 /* See the big comment above. */
d2e40e27 7746 spin_lock_irq(&dev_priv->uncore.lock);
a8a8bd54
PZ
7747 if (--dev_priv->uncore.forcewake_count == 0)
7748 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
d2e40e27 7749 spin_unlock_irq(&dev_priv->uncore.lock);
be256dc7
PZ
7750}
7751
765dab67
PZ
7752/*
7753 * Package states C8 and deeper are really deep PC states that can only be
7754 * reached when all the devices on the system allow it, so even if the graphics
7755 * device allows PC8+, it doesn't mean the system will actually get to these
7756 * states. Our driver only allows PC8+ when going into runtime PM.
7757 *
7758 * The requirements for PC8+ are that all the outputs are disabled, the power
7759 * well is disabled and most interrupts are disabled, and these are also
7760 * requirements for runtime PM. When these conditions are met, we manually do
7761 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7762 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7763 * hang the machine.
7764 *
7765 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7766 * the state of some registers, so when we come back from PC8+ we need to
7767 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7768 * need to take care of the registers kept by RC6. Notice that this happens even
7769 * if we don't put the device in PCI D3 state (which is what currently happens
7770 * because of the runtime PM support).
7771 *
7772 * For more, read "Display Sequences for Package C8" on the hardware
7773 * documentation.
7774 */
a14cb6fc 7775void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7776{
c67a470b
PZ
7777 struct drm_device *dev = dev_priv->dev;
7778 uint32_t val;
7779
c67a470b
PZ
7780 DRM_DEBUG_KMS("Enabling package C8+\n");
7781
c67a470b
PZ
7782 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7783 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7784 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7785 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7786 }
7787
7788 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7789 hsw_disable_lcpll(dev_priv, true, true);
7790}
7791
a14cb6fc 7792void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7793{
7794 struct drm_device *dev = dev_priv->dev;
7795 uint32_t val;
7796
c67a470b
PZ
7797 DRM_DEBUG_KMS("Disabling package C8+\n");
7798
7799 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7800 lpt_init_pch_refclk(dev);
7801
7802 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7803 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7804 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7805 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7806 }
7807
7808 intel_prepare_ddi(dev);
c67a470b
PZ
7809}
7810
9a952a0d
PZ
7811static void snb_modeset_global_resources(struct drm_device *dev)
7812{
7813 modeset_update_crtc_power_domains(dev);
7814}
7815
4f074129
ID
7816static void haswell_modeset_global_resources(struct drm_device *dev)
7817{
da723569 7818 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7819}
7820
09b4ddf9 7821static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7822 int x, int y,
7823 struct drm_framebuffer *fb)
7824{
09b4ddf9 7825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7826
566b734a 7827 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7828 return -EINVAL;
716c2e55 7829
644cef34
DV
7830 intel_crtc->lowfreq_avail = false;
7831
c8f7a0db 7832 return 0;
79e53945
JB
7833}
7834
7d2c8175
DL
7835static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7836 enum port port,
7837 struct intel_crtc_config *pipe_config)
7838{
7839 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7840
7841 switch (pipe_config->ddi_pll_sel) {
7842 case PORT_CLK_SEL_WRPLL1:
7843 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7844 break;
7845 case PORT_CLK_SEL_WRPLL2:
7846 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7847 break;
7848 }
7849}
7850
26804afd
DV
7851static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7852 struct intel_crtc_config *pipe_config)
7853{
7854 struct drm_device *dev = crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7856 struct intel_shared_dpll *pll;
26804afd
DV
7857 enum port port;
7858 uint32_t tmp;
7859
7860 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7861
7862 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7863
7d2c8175 7864 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7865
d452c5b6
DV
7866 if (pipe_config->shared_dpll >= 0) {
7867 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7868
7869 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7870 &pipe_config->dpll_hw_state));
7871 }
7872
26804afd
DV
7873 /*
7874 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7875 * DDI E. So just check whether this pipe is wired to DDI E and whether
7876 * the PCH transcoder is on.
7877 */
ca370455
DL
7878 if (INTEL_INFO(dev)->gen < 9 &&
7879 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
7880 pipe_config->has_pch_encoder = true;
7881
7882 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7883 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7884 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7885
7886 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7887 }
7888}
7889
0e8ffe1b
DV
7890static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7891 struct intel_crtc_config *pipe_config)
7892{
7893 struct drm_device *dev = crtc->base.dev;
7894 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7895 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7896 uint32_t tmp;
7897
f458ebbc 7898 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
7899 POWER_DOMAIN_PIPE(crtc->pipe)))
7900 return false;
7901
e143a21c 7902 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7903 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7904
eccb140b
DV
7905 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7906 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7907 enum pipe trans_edp_pipe;
7908 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7909 default:
7910 WARN(1, "unknown pipe linked to edp transcoder\n");
7911 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7912 case TRANS_DDI_EDP_INPUT_A_ON:
7913 trans_edp_pipe = PIPE_A;
7914 break;
7915 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7916 trans_edp_pipe = PIPE_B;
7917 break;
7918 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7919 trans_edp_pipe = PIPE_C;
7920 break;
7921 }
7922
7923 if (trans_edp_pipe == crtc->pipe)
7924 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7925 }
7926
f458ebbc 7927 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 7928 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7929 return false;
7930
eccb140b 7931 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7932 if (!(tmp & PIPECONF_ENABLE))
7933 return false;
7934
26804afd 7935 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7936
1bd1bd80
DV
7937 intel_get_pipe_timings(crtc, pipe_config);
7938
2fa2fe9a 7939 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
f458ebbc 7940 if (intel_display_power_is_enabled(dev_priv, pfit_domain))
2fa2fe9a 7941 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7942
e59150dc
JB
7943 if (IS_HASWELL(dev))
7944 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7945 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7946
ebb69c95
CT
7947 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
7948 pipe_config->pixel_multiplier =
7949 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
7950 } else {
7951 pipe_config->pixel_multiplier = 1;
7952 }
6c49f241 7953
0e8ffe1b
DV
7954 return true;
7955}
7956
1a91510d
JN
7957static struct {
7958 int clock;
7959 u32 config;
7960} hdmi_audio_clock[] = {
7961 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7962 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7963 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7964 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7965 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7966 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7967 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7968 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7969 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7970 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7971};
7972
7973/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7974static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7975{
7976 int i;
7977
7978 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7979 if (mode->clock == hdmi_audio_clock[i].clock)
7980 break;
7981 }
7982
7983 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7984 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7985 i = 1;
7986 }
7987
7988 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7989 hdmi_audio_clock[i].clock,
7990 hdmi_audio_clock[i].config);
7991
7992 return hdmi_audio_clock[i].config;
7993}
7994
3a9627f4
WF
7995static bool intel_eld_uptodate(struct drm_connector *connector,
7996 int reg_eldv, uint32_t bits_eldv,
7997 int reg_elda, uint32_t bits_elda,
7998 int reg_edid)
7999{
8000 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8001 uint8_t *eld = connector->eld;
8002 uint32_t i;
8003
8004 i = I915_READ(reg_eldv);
8005 i &= bits_eldv;
8006
8007 if (!eld[0])
8008 return !i;
8009
8010 if (!i)
8011 return false;
8012
8013 i = I915_READ(reg_elda);
8014 i &= ~bits_elda;
8015 I915_WRITE(reg_elda, i);
8016
8017 for (i = 0; i < eld[2]; i++)
8018 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
8019 return false;
8020
8021 return true;
8022}
8023
e0dac65e 8024static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
8025 struct drm_crtc *crtc,
8026 struct drm_display_mode *mode)
e0dac65e
WF
8027{
8028 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8029 uint8_t *eld = connector->eld;
8030 uint32_t eldv;
8031 uint32_t len;
8032 uint32_t i;
8033
8034 i = I915_READ(G4X_AUD_VID_DID);
8035
8036 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
8037 eldv = G4X_ELDV_DEVCL_DEVBLC;
8038 else
8039 eldv = G4X_ELDV_DEVCTG;
8040
3a9627f4
WF
8041 if (intel_eld_uptodate(connector,
8042 G4X_AUD_CNTL_ST, eldv,
8043 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
8044 G4X_HDMIW_HDMIEDID))
8045 return;
8046
e0dac65e
WF
8047 i = I915_READ(G4X_AUD_CNTL_ST);
8048 i &= ~(eldv | G4X_ELD_ADDR);
8049 len = (i >> 9) & 0x1f; /* ELD buffer size */
8050 I915_WRITE(G4X_AUD_CNTL_ST, i);
8051
8052 if (!eld[0])
8053 return;
8054
8055 len = min_t(uint8_t, eld[2], len);
8056 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8057 for (i = 0; i < len; i++)
8058 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
8059
8060 i = I915_READ(G4X_AUD_CNTL_ST);
8061 i |= eldv;
8062 I915_WRITE(G4X_AUD_CNTL_ST, i);
8063}
8064
83358c85 8065static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
8066 struct drm_crtc *crtc,
8067 struct drm_display_mode *mode)
83358c85
WX
8068{
8069 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8070 uint8_t *eld = connector->eld;
83358c85
WX
8071 uint32_t eldv;
8072 uint32_t i;
8073 int len;
8074 int pipe = to_intel_crtc(crtc)->pipe;
8075 int tmp;
8076
8077 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
8078 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
8079 int aud_config = HSW_AUD_CFG(pipe);
8080 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
8081
83358c85
WX
8082 /* Audio output enable */
8083 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
8084 tmp = I915_READ(aud_cntrl_st2);
8085 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
8086 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 8087 POSTING_READ(aud_cntrl_st2);
83358c85 8088
c7905792 8089 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
8090
8091 /* Set ELD valid state */
8092 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8093 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
8094 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
8095 I915_WRITE(aud_cntrl_st2, tmp);
8096 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8097 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
8098
8099 /* Enable HDMI mode */
8100 tmp = I915_READ(aud_config);
7e7cb34f 8101 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
8102 /* clear N_programing_enable and N_value_index */
8103 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8104 I915_WRITE(aud_config, tmp);
8105
8106 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8107
8108 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8109
8110 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8111 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8112 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8113 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8114 } else {
8115 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8116 }
83358c85
WX
8117
8118 if (intel_eld_uptodate(connector,
8119 aud_cntrl_st2, eldv,
8120 aud_cntl_st, IBX_ELD_ADDRESS,
8121 hdmiw_hdmiedid))
8122 return;
8123
8124 i = I915_READ(aud_cntrl_st2);
8125 i &= ~eldv;
8126 I915_WRITE(aud_cntrl_st2, i);
8127
8128 if (!eld[0])
8129 return;
8130
8131 i = I915_READ(aud_cntl_st);
8132 i &= ~IBX_ELD_ADDRESS;
8133 I915_WRITE(aud_cntl_st, i);
8134 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8135 DRM_DEBUG_DRIVER("port num:%d\n", i);
8136
8137 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8138 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8139 for (i = 0; i < len; i++)
8140 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8141
8142 i = I915_READ(aud_cntrl_st2);
8143 i |= eldv;
8144 I915_WRITE(aud_cntrl_st2, i);
8145
8146}
8147
e0dac65e 8148static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
8149 struct drm_crtc *crtc,
8150 struct drm_display_mode *mode)
e0dac65e
WF
8151{
8152 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8153 uint8_t *eld = connector->eld;
8154 uint32_t eldv;
8155 uint32_t i;
8156 int len;
8157 int hdmiw_hdmiedid;
b6daa025 8158 int aud_config;
e0dac65e
WF
8159 int aud_cntl_st;
8160 int aud_cntrl_st2;
9b138a83 8161 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 8162
b3f33cbf 8163 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
8164 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8165 aud_config = IBX_AUD_CFG(pipe);
8166 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8167 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8168 } else if (IS_VALLEYVIEW(connector->dev)) {
8169 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8170 aud_config = VLV_AUD_CFG(pipe);
8171 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8172 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8173 } else {
9b138a83
WX
8174 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8175 aud_config = CPT_AUD_CFG(pipe);
8176 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8177 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8178 }
8179
9b138a83 8180 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8181
9ca2fe73
ML
8182 if (IS_VALLEYVIEW(connector->dev)) {
8183 struct intel_encoder *intel_encoder;
8184 struct intel_digital_port *intel_dig_port;
8185
8186 intel_encoder = intel_attached_encoder(connector);
8187 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8188 i = intel_dig_port->port;
8189 } else {
8190 i = I915_READ(aud_cntl_st);
8191 i = (i >> 29) & DIP_PORT_SEL_MASK;
8192 /* DIP_Port_Select, 0x1 = PortB */
8193 }
8194
e0dac65e
WF
8195 if (!i) {
8196 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8197 /* operate blindly on all ports */
1202b4c6
WF
8198 eldv = IBX_ELD_VALIDB;
8199 eldv |= IBX_ELD_VALIDB << 4;
8200 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8201 } else {
2582a850 8202 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8203 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8204 }
8205
3a9627f4
WF
8206 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8207 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8208 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8209 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8210 } else {
8211 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8212 }
e0dac65e 8213
3a9627f4
WF
8214 if (intel_eld_uptodate(connector,
8215 aud_cntrl_st2, eldv,
8216 aud_cntl_st, IBX_ELD_ADDRESS,
8217 hdmiw_hdmiedid))
8218 return;
8219
e0dac65e
WF
8220 i = I915_READ(aud_cntrl_st2);
8221 i &= ~eldv;
8222 I915_WRITE(aud_cntrl_st2, i);
8223
8224 if (!eld[0])
8225 return;
8226
e0dac65e 8227 i = I915_READ(aud_cntl_st);
1202b4c6 8228 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8229 I915_WRITE(aud_cntl_st, i);
8230
8231 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8232 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8233 for (i = 0; i < len; i++)
8234 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8235
8236 i = I915_READ(aud_cntrl_st2);
8237 i |= eldv;
8238 I915_WRITE(aud_cntrl_st2, i);
8239}
8240
8241void intel_write_eld(struct drm_encoder *encoder,
8242 struct drm_display_mode *mode)
8243{
8244 struct drm_crtc *crtc = encoder->crtc;
8245 struct drm_connector *connector;
8246 struct drm_device *dev = encoder->dev;
8247 struct drm_i915_private *dev_priv = dev->dev_private;
8248
8249 connector = drm_select_eld(encoder, mode);
8250 if (!connector)
8251 return;
8252
8253 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8254 connector->base.id,
c23cc417 8255 connector->name,
e0dac65e 8256 connector->encoder->base.id,
8e329a03 8257 connector->encoder->name);
e0dac65e
WF
8258
8259 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8260
8261 if (dev_priv->display.write_eld)
34427052 8262 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8263}
8264
560b85bb
CW
8265static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8266{
8267 struct drm_device *dev = crtc->dev;
8268 struct drm_i915_private *dev_priv = dev->dev_private;
8269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8270 uint32_t cntl = 0, size = 0;
560b85bb 8271
dc41c154
VS
8272 if (base) {
8273 unsigned int width = intel_crtc->cursor_width;
8274 unsigned int height = intel_crtc->cursor_height;
8275 unsigned int stride = roundup_pow_of_two(width) * 4;
8276
8277 switch (stride) {
8278 default:
8279 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8280 width, stride);
8281 stride = 256;
8282 /* fallthrough */
8283 case 256:
8284 case 512:
8285 case 1024:
8286 case 2048:
8287 break;
4b0e333e
CW
8288 }
8289
dc41c154
VS
8290 cntl |= CURSOR_ENABLE |
8291 CURSOR_GAMMA_ENABLE |
8292 CURSOR_FORMAT_ARGB |
8293 CURSOR_STRIDE(stride);
8294
8295 size = (height << 12) | width;
4b0e333e 8296 }
560b85bb 8297
dc41c154
VS
8298 if (intel_crtc->cursor_cntl != 0 &&
8299 (intel_crtc->cursor_base != base ||
8300 intel_crtc->cursor_size != size ||
8301 intel_crtc->cursor_cntl != cntl)) {
8302 /* On these chipsets we can only modify the base/size/stride
8303 * whilst the cursor is disabled.
8304 */
8305 I915_WRITE(_CURACNTR, 0);
4b0e333e 8306 POSTING_READ(_CURACNTR);
dc41c154 8307 intel_crtc->cursor_cntl = 0;
4b0e333e 8308 }
560b85bb 8309
99d1f387 8310 if (intel_crtc->cursor_base != base) {
9db4a9c7 8311 I915_WRITE(_CURABASE, base);
99d1f387
VS
8312 intel_crtc->cursor_base = base;
8313 }
4726e0b0 8314
dc41c154
VS
8315 if (intel_crtc->cursor_size != size) {
8316 I915_WRITE(CURSIZE, size);
8317 intel_crtc->cursor_size = size;
4b0e333e 8318 }
560b85bb 8319
4b0e333e 8320 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8321 I915_WRITE(_CURACNTR, cntl);
8322 POSTING_READ(_CURACNTR);
4b0e333e 8323 intel_crtc->cursor_cntl = cntl;
560b85bb 8324 }
560b85bb
CW
8325}
8326
560b85bb 8327static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8328{
8329 struct drm_device *dev = crtc->dev;
8330 struct drm_i915_private *dev_priv = dev->dev_private;
8331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8332 int pipe = intel_crtc->pipe;
4b0e333e
CW
8333 uint32_t cntl;
8334
8335 cntl = 0;
8336 if (base) {
8337 cntl = MCURSOR_GAMMA_ENABLE;
8338 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8339 case 64:
8340 cntl |= CURSOR_MODE_64_ARGB_AX;
8341 break;
8342 case 128:
8343 cntl |= CURSOR_MODE_128_ARGB_AX;
8344 break;
8345 case 256:
8346 cntl |= CURSOR_MODE_256_ARGB_AX;
8347 break;
8348 default:
8349 WARN_ON(1);
8350 return;
65a21cd6 8351 }
4b0e333e 8352 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8353
8354 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8355 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8356 }
65a21cd6 8357
4b0e333e
CW
8358 if (intel_crtc->cursor_cntl != cntl) {
8359 I915_WRITE(CURCNTR(pipe), cntl);
8360 POSTING_READ(CURCNTR(pipe));
8361 intel_crtc->cursor_cntl = cntl;
65a21cd6 8362 }
4b0e333e 8363
65a21cd6 8364 /* and commit changes on next vblank */
5efb3e28
VS
8365 I915_WRITE(CURBASE(pipe), base);
8366 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8367
8368 intel_crtc->cursor_base = base;
65a21cd6
JB
8369}
8370
cda4b7d3 8371/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8372static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8373 bool on)
cda4b7d3
CW
8374{
8375 struct drm_device *dev = crtc->dev;
8376 struct drm_i915_private *dev_priv = dev->dev_private;
8377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8378 int pipe = intel_crtc->pipe;
3d7d6510
MR
8379 int x = crtc->cursor_x;
8380 int y = crtc->cursor_y;
d6e4db15 8381 u32 base = 0, pos = 0;
cda4b7d3 8382
d6e4db15 8383 if (on)
cda4b7d3 8384 base = intel_crtc->cursor_addr;
cda4b7d3 8385
d6e4db15
VS
8386 if (x >= intel_crtc->config.pipe_src_w)
8387 base = 0;
8388
8389 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8390 base = 0;
8391
8392 if (x < 0) {
efc9064e 8393 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8394 base = 0;
8395
8396 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8397 x = -x;
8398 }
8399 pos |= x << CURSOR_X_SHIFT;
8400
8401 if (y < 0) {
efc9064e 8402 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8403 base = 0;
8404
8405 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8406 y = -y;
8407 }
8408 pos |= y << CURSOR_Y_SHIFT;
8409
4b0e333e 8410 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8411 return;
8412
5efb3e28
VS
8413 I915_WRITE(CURPOS(pipe), pos);
8414
8ac54669 8415 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8416 i845_update_cursor(crtc, base);
8417 else
8418 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8419}
8420
dc41c154
VS
8421static bool cursor_size_ok(struct drm_device *dev,
8422 uint32_t width, uint32_t height)
8423{
8424 if (width == 0 || height == 0)
8425 return false;
8426
8427 /*
8428 * 845g/865g are special in that they are only limited by
8429 * the width of their cursors, the height is arbitrary up to
8430 * the precision of the register. Everything else requires
8431 * square cursors, limited to a few power-of-two sizes.
8432 */
8433 if (IS_845G(dev) || IS_I865G(dev)) {
8434 if ((width & 63) != 0)
8435 return false;
8436
8437 if (width > (IS_845G(dev) ? 64 : 512))
8438 return false;
8439
8440 if (height > 1023)
8441 return false;
8442 } else {
8443 switch (width | height) {
8444 case 256:
8445 case 128:
8446 if (IS_GEN2(dev))
8447 return false;
8448 case 64:
8449 break;
8450 default:
8451 return false;
8452 }
8453 }
8454
8455 return true;
8456}
8457
e3287951
MR
8458static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8459 struct drm_i915_gem_object *obj,
8460 uint32_t width, uint32_t height)
79e53945
JB
8461{
8462 struct drm_device *dev = crtc->dev;
8463 struct drm_i915_private *dev_priv = dev->dev_private;
8464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8465 enum pipe pipe = intel_crtc->pipe;
dc41c154 8466 unsigned old_width, stride;
cda4b7d3 8467 uint32_t addr;
3f8bc370 8468 int ret;
79e53945 8469
79e53945 8470 /* if we want to turn off the cursor ignore width and height */
e3287951 8471 if (!obj) {
28c97730 8472 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8473 addr = 0;
5004417d 8474 mutex_lock(&dev->struct_mutex);
3f8bc370 8475 goto finish;
79e53945
JB
8476 }
8477
4726e0b0 8478 /* Check for which cursor types we support */
dc41c154 8479 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8480 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8481 return -EINVAL;
8482 }
8483
dc41c154
VS
8484 stride = roundup_pow_of_two(width) * 4;
8485 if (obj->base.size < stride * height) {
e3287951 8486 DRM_DEBUG_KMS("buffer is too small\n");
5ee44638 8487 return -ENOMEM;
79e53945
JB
8488 }
8489
71acb5eb 8490 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8491 mutex_lock(&dev->struct_mutex);
3d13ef2e 8492 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8493 unsigned alignment;
8494
d9e86c0e 8495 if (obj->tiling_mode) {
3b25b31f 8496 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8497 ret = -EINVAL;
8498 goto fail_locked;
8499 }
8500
d6dd6843
PZ
8501 /*
8502 * Global gtt pte registers are special registers which actually
8503 * forward writes to a chunk of system memory. Which means that
8504 * there is no risk that the register values disappear as soon
8505 * as we call intel_runtime_pm_put(), so it is correct to wrap
8506 * only the pin/unpin/fence and not more.
8507 */
8508 intel_runtime_pm_get(dev_priv);
8509
693db184
CW
8510 /* Note that the w/a also requires 2 PTE of padding following
8511 * the bo. We currently fill all unused PTE with the shadow
8512 * page and so we should always have valid PTE following the
8513 * cursor preventing the VT-d warning.
8514 */
8515 alignment = 0;
8516 if (need_vtd_wa(dev))
8517 alignment = 64*1024;
8518
8519 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8520 if (ret) {
3b25b31f 8521 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
d6dd6843 8522 intel_runtime_pm_put(dev_priv);
2da3b9b9 8523 goto fail_locked;
e7b526bb
CW
8524 }
8525
d9e86c0e
CW
8526 ret = i915_gem_object_put_fence(obj);
8527 if (ret) {
3b25b31f 8528 DRM_DEBUG_KMS("failed to release fence for cursor");
d6dd6843 8529 intel_runtime_pm_put(dev_priv);
d9e86c0e
CW
8530 goto fail_unpin;
8531 }
8532
f343c5f6 8533 addr = i915_gem_obj_ggtt_offset(obj);
d6dd6843
PZ
8534
8535 intel_runtime_pm_put(dev_priv);
71acb5eb 8536 } else {
6eeefaf3 8537 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8538 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8539 if (ret) {
3b25b31f 8540 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8541 goto fail_locked;
71acb5eb 8542 }
00731155 8543 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8544 }
8545
3f8bc370 8546 finish:
3f8bc370 8547 if (intel_crtc->cursor_bo) {
00731155 8548 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8549 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8550 }
80824003 8551
a071fa00
DV
8552 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8553 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8554 mutex_unlock(&dev->struct_mutex);
3f8bc370 8555
64f962e3
CW
8556 old_width = intel_crtc->cursor_width;
8557
3f8bc370 8558 intel_crtc->cursor_addr = addr;
05394f39 8559 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8560 intel_crtc->cursor_width = width;
8561 intel_crtc->cursor_height = height;
8562
64f962e3
CW
8563 if (intel_crtc->active) {
8564 if (old_width != width)
8565 intel_update_watermarks(crtc);
f2f5f771 8566 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8567 }
3f8bc370 8568
f99d7069
DV
8569 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8570
79e53945 8571 return 0;
e7b526bb 8572fail_unpin:
cc98b413 8573 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8574fail_locked:
34b8686e
DA
8575 mutex_unlock(&dev->struct_mutex);
8576 return ret;
79e53945
JB
8577}
8578
79e53945 8579static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8580 u16 *blue, uint32_t start, uint32_t size)
79e53945 8581{
7203425a 8582 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8584
7203425a 8585 for (i = start; i < end; i++) {
79e53945
JB
8586 intel_crtc->lut_r[i] = red[i] >> 8;
8587 intel_crtc->lut_g[i] = green[i] >> 8;
8588 intel_crtc->lut_b[i] = blue[i] >> 8;
8589 }
8590
8591 intel_crtc_load_lut(crtc);
8592}
8593
79e53945
JB
8594/* VESA 640x480x72Hz mode to set on the pipe */
8595static struct drm_display_mode load_detect_mode = {
8596 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8597 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8598};
8599
a8bb6818
DV
8600struct drm_framebuffer *
8601__intel_framebuffer_create(struct drm_device *dev,
8602 struct drm_mode_fb_cmd2 *mode_cmd,
8603 struct drm_i915_gem_object *obj)
d2dff872
CW
8604{
8605 struct intel_framebuffer *intel_fb;
8606 int ret;
8607
8608 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8609 if (!intel_fb) {
8610 drm_gem_object_unreference_unlocked(&obj->base);
8611 return ERR_PTR(-ENOMEM);
8612 }
8613
8614 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8615 if (ret)
8616 goto err;
d2dff872
CW
8617
8618 return &intel_fb->base;
dd4916c5
DV
8619err:
8620 drm_gem_object_unreference_unlocked(&obj->base);
8621 kfree(intel_fb);
8622
8623 return ERR_PTR(ret);
d2dff872
CW
8624}
8625
b5ea642a 8626static struct drm_framebuffer *
a8bb6818
DV
8627intel_framebuffer_create(struct drm_device *dev,
8628 struct drm_mode_fb_cmd2 *mode_cmd,
8629 struct drm_i915_gem_object *obj)
8630{
8631 struct drm_framebuffer *fb;
8632 int ret;
8633
8634 ret = i915_mutex_lock_interruptible(dev);
8635 if (ret)
8636 return ERR_PTR(ret);
8637 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8638 mutex_unlock(&dev->struct_mutex);
8639
8640 return fb;
8641}
8642
d2dff872
CW
8643static u32
8644intel_framebuffer_pitch_for_width(int width, int bpp)
8645{
8646 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8647 return ALIGN(pitch, 64);
8648}
8649
8650static u32
8651intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8652{
8653 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8654 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8655}
8656
8657static struct drm_framebuffer *
8658intel_framebuffer_create_for_mode(struct drm_device *dev,
8659 struct drm_display_mode *mode,
8660 int depth, int bpp)
8661{
8662 struct drm_i915_gem_object *obj;
0fed39bd 8663 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8664
8665 obj = i915_gem_alloc_object(dev,
8666 intel_framebuffer_size_for_mode(mode, bpp));
8667 if (obj == NULL)
8668 return ERR_PTR(-ENOMEM);
8669
8670 mode_cmd.width = mode->hdisplay;
8671 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8672 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8673 bpp);
5ca0c34a 8674 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8675
8676 return intel_framebuffer_create(dev, &mode_cmd, obj);
8677}
8678
8679static struct drm_framebuffer *
8680mode_fits_in_fbdev(struct drm_device *dev,
8681 struct drm_display_mode *mode)
8682{
4520f53a 8683#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8684 struct drm_i915_private *dev_priv = dev->dev_private;
8685 struct drm_i915_gem_object *obj;
8686 struct drm_framebuffer *fb;
8687
4c0e5528 8688 if (!dev_priv->fbdev)
d2dff872
CW
8689 return NULL;
8690
4c0e5528 8691 if (!dev_priv->fbdev->fb)
d2dff872
CW
8692 return NULL;
8693
4c0e5528
DV
8694 obj = dev_priv->fbdev->fb->obj;
8695 BUG_ON(!obj);
8696
8bcd4553 8697 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8698 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8699 fb->bits_per_pixel))
d2dff872
CW
8700 return NULL;
8701
01f2c773 8702 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8703 return NULL;
8704
8705 return fb;
4520f53a
DV
8706#else
8707 return NULL;
8708#endif
d2dff872
CW
8709}
8710
d2434ab7 8711bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8712 struct drm_display_mode *mode,
51fd371b
RC
8713 struct intel_load_detect_pipe *old,
8714 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8715{
8716 struct intel_crtc *intel_crtc;
d2434ab7
DV
8717 struct intel_encoder *intel_encoder =
8718 intel_attached_encoder(connector);
79e53945 8719 struct drm_crtc *possible_crtc;
4ef69c7a 8720 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8721 struct drm_crtc *crtc = NULL;
8722 struct drm_device *dev = encoder->dev;
94352cf9 8723 struct drm_framebuffer *fb;
51fd371b
RC
8724 struct drm_mode_config *config = &dev->mode_config;
8725 int ret, i = -1;
79e53945 8726
d2dff872 8727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8728 connector->base.id, connector->name,
8e329a03 8729 encoder->base.id, encoder->name);
d2dff872 8730
51fd371b
RC
8731retry:
8732 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8733 if (ret)
8734 goto fail_unlock;
6e9f798d 8735
79e53945
JB
8736 /*
8737 * Algorithm gets a little messy:
7a5e4805 8738 *
79e53945
JB
8739 * - if the connector already has an assigned crtc, use it (but make
8740 * sure it's on first)
7a5e4805 8741 *
79e53945
JB
8742 * - try to find the first unused crtc that can drive this connector,
8743 * and use that if we find one
79e53945
JB
8744 */
8745
8746 /* See if we already have a CRTC for this connector */
8747 if (encoder->crtc) {
8748 crtc = encoder->crtc;
8261b191 8749
51fd371b
RC
8750 ret = drm_modeset_lock(&crtc->mutex, ctx);
8751 if (ret)
8752 goto fail_unlock;
7b24056b 8753
24218aac 8754 old->dpms_mode = connector->dpms;
8261b191
CW
8755 old->load_detect_temp = false;
8756
8757 /* Make sure the crtc and connector are running */
24218aac
DV
8758 if (connector->dpms != DRM_MODE_DPMS_ON)
8759 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8760
7173188d 8761 return true;
79e53945
JB
8762 }
8763
8764 /* Find an unused one (if possible) */
70e1e0ec 8765 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8766 i++;
8767 if (!(encoder->possible_crtcs & (1 << i)))
8768 continue;
a459249c
VS
8769 if (possible_crtc->enabled)
8770 continue;
8771 /* This can occur when applying the pipe A quirk on resume. */
8772 if (to_intel_crtc(possible_crtc)->new_enabled)
8773 continue;
8774
8775 crtc = possible_crtc;
8776 break;
79e53945
JB
8777 }
8778
8779 /*
8780 * If we didn't find an unused CRTC, don't use any.
8781 */
8782 if (!crtc) {
7173188d 8783 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8784 goto fail_unlock;
79e53945
JB
8785 }
8786
51fd371b
RC
8787 ret = drm_modeset_lock(&crtc->mutex, ctx);
8788 if (ret)
8789 goto fail_unlock;
fc303101
DV
8790 intel_encoder->new_crtc = to_intel_crtc(crtc);
8791 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8792
8793 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8794 intel_crtc->new_enabled = true;
8795 intel_crtc->new_config = &intel_crtc->config;
24218aac 8796 old->dpms_mode = connector->dpms;
8261b191 8797 old->load_detect_temp = true;
d2dff872 8798 old->release_fb = NULL;
79e53945 8799
6492711d
CW
8800 if (!mode)
8801 mode = &load_detect_mode;
79e53945 8802
d2dff872
CW
8803 /* We need a framebuffer large enough to accommodate all accesses
8804 * that the plane may generate whilst we perform load detection.
8805 * We can not rely on the fbcon either being present (we get called
8806 * during its initialisation to detect all boot displays, or it may
8807 * not even exist) or that it is large enough to satisfy the
8808 * requested mode.
8809 */
94352cf9
DV
8810 fb = mode_fits_in_fbdev(dev, mode);
8811 if (fb == NULL) {
d2dff872 8812 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8813 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8814 old->release_fb = fb;
d2dff872
CW
8815 } else
8816 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8817 if (IS_ERR(fb)) {
d2dff872 8818 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8819 goto fail;
79e53945 8820 }
79e53945 8821
c0c36b94 8822 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8823 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8824 if (old->release_fb)
8825 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8826 goto fail;
79e53945 8827 }
7173188d 8828
79e53945 8829 /* let the connector get through one full cycle before testing */
9d0498a2 8830 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8831 return true;
412b61d8
VS
8832
8833 fail:
8834 intel_crtc->new_enabled = crtc->enabled;
8835 if (intel_crtc->new_enabled)
8836 intel_crtc->new_config = &intel_crtc->config;
8837 else
8838 intel_crtc->new_config = NULL;
51fd371b
RC
8839fail_unlock:
8840 if (ret == -EDEADLK) {
8841 drm_modeset_backoff(ctx);
8842 goto retry;
8843 }
8844
412b61d8 8845 return false;
79e53945
JB
8846}
8847
d2434ab7 8848void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8849 struct intel_load_detect_pipe *old)
79e53945 8850{
d2434ab7
DV
8851 struct intel_encoder *intel_encoder =
8852 intel_attached_encoder(connector);
4ef69c7a 8853 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8854 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8856
d2dff872 8857 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8858 connector->base.id, connector->name,
8e329a03 8859 encoder->base.id, encoder->name);
d2dff872 8860
8261b191 8861 if (old->load_detect_temp) {
fc303101
DV
8862 to_intel_connector(connector)->new_encoder = NULL;
8863 intel_encoder->new_crtc = NULL;
412b61d8
VS
8864 intel_crtc->new_enabled = false;
8865 intel_crtc->new_config = NULL;
fc303101 8866 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8867
36206361
DV
8868 if (old->release_fb) {
8869 drm_framebuffer_unregister_private(old->release_fb);
8870 drm_framebuffer_unreference(old->release_fb);
8871 }
d2dff872 8872
0622a53c 8873 return;
79e53945
JB
8874 }
8875
c751ce4f 8876 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8877 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8878 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8879}
8880
da4a1efa
VS
8881static int i9xx_pll_refclk(struct drm_device *dev,
8882 const struct intel_crtc_config *pipe_config)
8883{
8884 struct drm_i915_private *dev_priv = dev->dev_private;
8885 u32 dpll = pipe_config->dpll_hw_state.dpll;
8886
8887 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8888 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8889 else if (HAS_PCH_SPLIT(dev))
8890 return 120000;
8891 else if (!IS_GEN2(dev))
8892 return 96000;
8893 else
8894 return 48000;
8895}
8896
79e53945 8897/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8898static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8899 struct intel_crtc_config *pipe_config)
79e53945 8900{
f1f644dc 8901 struct drm_device *dev = crtc->base.dev;
79e53945 8902 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8903 int pipe = pipe_config->cpu_transcoder;
293623f7 8904 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8905 u32 fp;
8906 intel_clock_t clock;
da4a1efa 8907 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8908
8909 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8910 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8911 else
293623f7 8912 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8913
8914 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8915 if (IS_PINEVIEW(dev)) {
8916 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8917 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8918 } else {
8919 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8920 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8921 }
8922
a6c45cf0 8923 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8924 if (IS_PINEVIEW(dev))
8925 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8926 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8927 else
8928 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8929 DPLL_FPA01_P1_POST_DIV_SHIFT);
8930
8931 switch (dpll & DPLL_MODE_MASK) {
8932 case DPLLB_MODE_DAC_SERIAL:
8933 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8934 5 : 10;
8935 break;
8936 case DPLLB_MODE_LVDS:
8937 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8938 7 : 14;
8939 break;
8940 default:
28c97730 8941 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8942 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8943 return;
79e53945
JB
8944 }
8945
ac58c3f0 8946 if (IS_PINEVIEW(dev))
da4a1efa 8947 pineview_clock(refclk, &clock);
ac58c3f0 8948 else
da4a1efa 8949 i9xx_clock(refclk, &clock);
79e53945 8950 } else {
0fb58223 8951 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8952 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8953
8954 if (is_lvds) {
8955 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8956 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8957
8958 if (lvds & LVDS_CLKB_POWER_UP)
8959 clock.p2 = 7;
8960 else
8961 clock.p2 = 14;
79e53945
JB
8962 } else {
8963 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8964 clock.p1 = 2;
8965 else {
8966 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8967 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8968 }
8969 if (dpll & PLL_P2_DIVIDE_BY_4)
8970 clock.p2 = 4;
8971 else
8972 clock.p2 = 2;
79e53945 8973 }
da4a1efa
VS
8974
8975 i9xx_clock(refclk, &clock);
79e53945
JB
8976 }
8977
18442d08
VS
8978 /*
8979 * This value includes pixel_multiplier. We will use
241bfc38 8980 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8981 * encoder's get_config() function.
8982 */
8983 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8984}
8985
6878da05
VS
8986int intel_dotclock_calculate(int link_freq,
8987 const struct intel_link_m_n *m_n)
f1f644dc 8988{
f1f644dc
JB
8989 /*
8990 * The calculation for the data clock is:
1041a02f 8991 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8992 * But we want to avoid losing precison if possible, so:
1041a02f 8993 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8994 *
8995 * and the link clock is simpler:
1041a02f 8996 * link_clock = (m * link_clock) / n
f1f644dc
JB
8997 */
8998
6878da05
VS
8999 if (!m_n->link_n)
9000 return 0;
f1f644dc 9001
6878da05
VS
9002 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9003}
f1f644dc 9004
18442d08
VS
9005static void ironlake_pch_clock_get(struct intel_crtc *crtc,
9006 struct intel_crtc_config *pipe_config)
6878da05
VS
9007{
9008 struct drm_device *dev = crtc->base.dev;
79e53945 9009
18442d08
VS
9010 /* read out port_clock from the DPLL */
9011 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9012
f1f644dc 9013 /*
18442d08 9014 * This value does not include pixel_multiplier.
241bfc38 9015 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
9016 * agree once we know their relationship in the encoder's
9017 * get_config() function.
79e53945 9018 */
241bfc38 9019 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
9020 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9021 &pipe_config->fdi_m_n);
79e53945
JB
9022}
9023
9024/** Returns the currently programmed mode of the given pipe. */
9025struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9026 struct drm_crtc *crtc)
9027{
548f245b 9028 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 9029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 9030 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 9031 struct drm_display_mode *mode;
f1f644dc 9032 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
9033 int htot = I915_READ(HTOTAL(cpu_transcoder));
9034 int hsync = I915_READ(HSYNC(cpu_transcoder));
9035 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9036 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9037 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9038
9039 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9040 if (!mode)
9041 return NULL;
9042
f1f644dc
JB
9043 /*
9044 * Construct a pipe_config sufficient for getting the clock info
9045 * back out of crtc_clock_get.
9046 *
9047 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9048 * to use a real value here instead.
9049 */
293623f7 9050 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 9051 pipe_config.pixel_multiplier = 1;
293623f7
VS
9052 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9053 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9054 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
9055 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9056
773ae034 9057 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
9058 mode->hdisplay = (htot & 0xffff) + 1;
9059 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9060 mode->hsync_start = (hsync & 0xffff) + 1;
9061 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9062 mode->vdisplay = (vtot & 0xffff) + 1;
9063 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9064 mode->vsync_start = (vsync & 0xffff) + 1;
9065 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9066
9067 drm_mode_set_name(mode);
79e53945
JB
9068
9069 return mode;
9070}
9071
652c393a
JB
9072static void intel_decrease_pllclock(struct drm_crtc *crtc)
9073{
9074 struct drm_device *dev = crtc->dev;
fbee40df 9075 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 9077
baff296c 9078 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9079 return;
9080
9081 if (!dev_priv->lvds_downclock_avail)
9082 return;
9083
9084 /*
9085 * Since this is called by a timer, we should never get here in
9086 * the manual case.
9087 */
9088 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
9089 int pipe = intel_crtc->pipe;
9090 int dpll_reg = DPLL(pipe);
9091 int dpll;
f6e5b160 9092
44d98a61 9093 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9094
8ac5a6d5 9095 assert_panel_unlocked(dev_priv, pipe);
652c393a 9096
dc257cf1 9097 dpll = I915_READ(dpll_reg);
652c393a
JB
9098 dpll |= DISPLAY_RATE_SELECT_FPA1;
9099 I915_WRITE(dpll_reg, dpll);
9d0498a2 9100 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9101 dpll = I915_READ(dpll_reg);
9102 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9103 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9104 }
9105
9106}
9107
f047e395
CW
9108void intel_mark_busy(struct drm_device *dev)
9109{
c67a470b
PZ
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111
f62a0076
CW
9112 if (dev_priv->mm.busy)
9113 return;
9114
43694d69 9115 intel_runtime_pm_get(dev_priv);
c67a470b 9116 i915_update_gfx_val(dev_priv);
f62a0076 9117 dev_priv->mm.busy = true;
f047e395
CW
9118}
9119
9120void intel_mark_idle(struct drm_device *dev)
652c393a 9121{
c67a470b 9122 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9123 struct drm_crtc *crtc;
652c393a 9124
f62a0076
CW
9125 if (!dev_priv->mm.busy)
9126 return;
9127
9128 dev_priv->mm.busy = false;
9129
d330a953 9130 if (!i915.powersave)
bb4cdd53 9131 goto out;
652c393a 9132
70e1e0ec 9133 for_each_crtc(dev, crtc) {
f4510a27 9134 if (!crtc->primary->fb)
652c393a
JB
9135 continue;
9136
725a5b54 9137 intel_decrease_pllclock(crtc);
652c393a 9138 }
b29c19b6 9139
3d13ef2e 9140 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9141 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9142
9143out:
43694d69 9144 intel_runtime_pm_put(dev_priv);
652c393a
JB
9145}
9146
79e53945
JB
9147static void intel_crtc_destroy(struct drm_crtc *crtc)
9148{
9149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9150 struct drm_device *dev = crtc->dev;
9151 struct intel_unpin_work *work;
67e77c5a 9152
5e2d7afc 9153 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
9154 work = intel_crtc->unpin_work;
9155 intel_crtc->unpin_work = NULL;
5e2d7afc 9156 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
9157
9158 if (work) {
9159 cancel_work_sync(&work->work);
9160 kfree(work);
9161 }
79e53945
JB
9162
9163 drm_crtc_cleanup(crtc);
67e77c5a 9164
79e53945
JB
9165 kfree(intel_crtc);
9166}
9167
6b95a207
KH
9168static void intel_unpin_work_fn(struct work_struct *__work)
9169{
9170 struct intel_unpin_work *work =
9171 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9172 struct drm_device *dev = work->crtc->dev;
f99d7069 9173 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9174
b4a98e57 9175 mutex_lock(&dev->struct_mutex);
1690e1eb 9176 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9177 drm_gem_object_unreference(&work->pending_flip_obj->base);
9178 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9179
b4a98e57
CW
9180 intel_update_fbc(dev);
9181 mutex_unlock(&dev->struct_mutex);
9182
f99d7069
DV
9183 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9184
b4a98e57
CW
9185 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9186 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9187
6b95a207
KH
9188 kfree(work);
9189}
9190
1afe3e9d 9191static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9192 struct drm_crtc *crtc)
6b95a207 9193{
6b95a207
KH
9194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9195 struct intel_unpin_work *work;
6b95a207
KH
9196 unsigned long flags;
9197
9198 /* Ignore early vblank irqs */
9199 if (intel_crtc == NULL)
9200 return;
9201
f326038a
DV
9202 /*
9203 * This is called both by irq handlers and the reset code (to complete
9204 * lost pageflips) so needs the full irqsave spinlocks.
9205 */
6b95a207
KH
9206 spin_lock_irqsave(&dev->event_lock, flags);
9207 work = intel_crtc->unpin_work;
e7d841ca
CW
9208
9209 /* Ensure we don't miss a work->pending update ... */
9210 smp_rmb();
9211
9212 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9213 spin_unlock_irqrestore(&dev->event_lock, flags);
9214 return;
9215 }
9216
d6bbafa1 9217 page_flip_completed(intel_crtc);
0af7e4df 9218
6b95a207 9219 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9220}
9221
1afe3e9d
JB
9222void intel_finish_page_flip(struct drm_device *dev, int pipe)
9223{
fbee40df 9224 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9225 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9226
49b14a5c 9227 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9228}
9229
9230void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9231{
fbee40df 9232 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9233 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9234
49b14a5c 9235 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9236}
9237
75f7f3ec
VS
9238/* Is 'a' after or equal to 'b'? */
9239static bool g4x_flip_count_after_eq(u32 a, u32 b)
9240{
9241 return !((a - b) & 0x80000000);
9242}
9243
9244static bool page_flip_finished(struct intel_crtc *crtc)
9245{
9246 struct drm_device *dev = crtc->base.dev;
9247 struct drm_i915_private *dev_priv = dev->dev_private;
9248
9249 /*
9250 * The relevant registers doen't exist on pre-ctg.
9251 * As the flip done interrupt doesn't trigger for mmio
9252 * flips on gmch platforms, a flip count check isn't
9253 * really needed there. But since ctg has the registers,
9254 * include it in the check anyway.
9255 */
9256 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9257 return true;
9258
9259 /*
9260 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9261 * used the same base address. In that case the mmio flip might
9262 * have completed, but the CS hasn't even executed the flip yet.
9263 *
9264 * A flip count check isn't enough as the CS might have updated
9265 * the base address just after start of vblank, but before we
9266 * managed to process the interrupt. This means we'd complete the
9267 * CS flip too soon.
9268 *
9269 * Combining both checks should get us a good enough result. It may
9270 * still happen that the CS flip has been executed, but has not
9271 * yet actually completed. But in case the base address is the same
9272 * anyway, we don't really care.
9273 */
9274 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9275 crtc->unpin_work->gtt_offset &&
9276 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9277 crtc->unpin_work->flip_count);
9278}
9279
6b95a207
KH
9280void intel_prepare_page_flip(struct drm_device *dev, int plane)
9281{
fbee40df 9282 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9283 struct intel_crtc *intel_crtc =
9284 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9285 unsigned long flags;
9286
f326038a
DV
9287
9288 /*
9289 * This is called both by irq handlers and the reset code (to complete
9290 * lost pageflips) so needs the full irqsave spinlocks.
9291 *
9292 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
9293 * generate a page-flip completion irq, i.e. every modeset
9294 * is also accompanied by a spurious intel_prepare_page_flip().
9295 */
6b95a207 9296 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9297 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9298 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9299 spin_unlock_irqrestore(&dev->event_lock, flags);
9300}
9301
eba905b2 9302static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9303{
9304 /* Ensure that the work item is consistent when activating it ... */
9305 smp_wmb();
9306 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9307 /* and that it is marked active as soon as the irq could fire. */
9308 smp_wmb();
9309}
9310
8c9f3aaf
JB
9311static int intel_gen2_queue_flip(struct drm_device *dev,
9312 struct drm_crtc *crtc,
9313 struct drm_framebuffer *fb,
ed8d1975 9314 struct drm_i915_gem_object *obj,
a4872ba6 9315 struct intel_engine_cs *ring,
ed8d1975 9316 uint32_t flags)
8c9f3aaf 9317{
8c9f3aaf 9318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9319 u32 flip_mask;
9320 int ret;
9321
6d90c952 9322 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9323 if (ret)
4fa62c89 9324 return ret;
8c9f3aaf
JB
9325
9326 /* Can't queue multiple flips, so wait for the previous
9327 * one to finish before executing the next.
9328 */
9329 if (intel_crtc->plane)
9330 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9331 else
9332 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9333 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9334 intel_ring_emit(ring, MI_NOOP);
9335 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9336 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9337 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9338 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9339 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9340
9341 intel_mark_page_flip_active(intel_crtc);
09246732 9342 __intel_ring_advance(ring);
83d4092b 9343 return 0;
8c9f3aaf
JB
9344}
9345
9346static int intel_gen3_queue_flip(struct drm_device *dev,
9347 struct drm_crtc *crtc,
9348 struct drm_framebuffer *fb,
ed8d1975 9349 struct drm_i915_gem_object *obj,
a4872ba6 9350 struct intel_engine_cs *ring,
ed8d1975 9351 uint32_t flags)
8c9f3aaf 9352{
8c9f3aaf 9353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9354 u32 flip_mask;
9355 int ret;
9356
6d90c952 9357 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9358 if (ret)
4fa62c89 9359 return ret;
8c9f3aaf
JB
9360
9361 if (intel_crtc->plane)
9362 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9363 else
9364 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9365 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9366 intel_ring_emit(ring, MI_NOOP);
9367 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9368 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9369 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9370 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9371 intel_ring_emit(ring, MI_NOOP);
9372
e7d841ca 9373 intel_mark_page_flip_active(intel_crtc);
09246732 9374 __intel_ring_advance(ring);
83d4092b 9375 return 0;
8c9f3aaf
JB
9376}
9377
9378static int intel_gen4_queue_flip(struct drm_device *dev,
9379 struct drm_crtc *crtc,
9380 struct drm_framebuffer *fb,
ed8d1975 9381 struct drm_i915_gem_object *obj,
a4872ba6 9382 struct intel_engine_cs *ring,
ed8d1975 9383 uint32_t flags)
8c9f3aaf
JB
9384{
9385 struct drm_i915_private *dev_priv = dev->dev_private;
9386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9387 uint32_t pf, pipesrc;
9388 int ret;
9389
6d90c952 9390 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9391 if (ret)
4fa62c89 9392 return ret;
8c9f3aaf
JB
9393
9394 /* i965+ uses the linear or tiled offsets from the
9395 * Display Registers (which do not change across a page-flip)
9396 * so we need only reprogram the base address.
9397 */
6d90c952
DV
9398 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9399 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9400 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9401 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9402 obj->tiling_mode);
8c9f3aaf
JB
9403
9404 /* XXX Enabling the panel-fitter across page-flip is so far
9405 * untested on non-native modes, so ignore it for now.
9406 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9407 */
9408 pf = 0;
9409 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9410 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9411
9412 intel_mark_page_flip_active(intel_crtc);
09246732 9413 __intel_ring_advance(ring);
83d4092b 9414 return 0;
8c9f3aaf
JB
9415}
9416
9417static int intel_gen6_queue_flip(struct drm_device *dev,
9418 struct drm_crtc *crtc,
9419 struct drm_framebuffer *fb,
ed8d1975 9420 struct drm_i915_gem_object *obj,
a4872ba6 9421 struct intel_engine_cs *ring,
ed8d1975 9422 uint32_t flags)
8c9f3aaf
JB
9423{
9424 struct drm_i915_private *dev_priv = dev->dev_private;
9425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9426 uint32_t pf, pipesrc;
9427 int ret;
9428
6d90c952 9429 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9430 if (ret)
4fa62c89 9431 return ret;
8c9f3aaf 9432
6d90c952
DV
9433 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9434 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9435 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9436 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9437
dc257cf1
DV
9438 /* Contrary to the suggestions in the documentation,
9439 * "Enable Panel Fitter" does not seem to be required when page
9440 * flipping with a non-native mode, and worse causes a normal
9441 * modeset to fail.
9442 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9443 */
9444 pf = 0;
8c9f3aaf 9445 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9446 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9447
9448 intel_mark_page_flip_active(intel_crtc);
09246732 9449 __intel_ring_advance(ring);
83d4092b 9450 return 0;
8c9f3aaf
JB
9451}
9452
7c9017e5
JB
9453static int intel_gen7_queue_flip(struct drm_device *dev,
9454 struct drm_crtc *crtc,
9455 struct drm_framebuffer *fb,
ed8d1975 9456 struct drm_i915_gem_object *obj,
a4872ba6 9457 struct intel_engine_cs *ring,
ed8d1975 9458 uint32_t flags)
7c9017e5 9459{
7c9017e5 9460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9461 uint32_t plane_bit = 0;
ffe74d75
CW
9462 int len, ret;
9463
eba905b2 9464 switch (intel_crtc->plane) {
cb05d8de
DV
9465 case PLANE_A:
9466 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9467 break;
9468 case PLANE_B:
9469 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9470 break;
9471 case PLANE_C:
9472 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9473 break;
9474 default:
9475 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9476 return -ENODEV;
cb05d8de
DV
9477 }
9478
ffe74d75 9479 len = 4;
f476828a 9480 if (ring->id == RCS) {
ffe74d75 9481 len += 6;
f476828a
DL
9482 /*
9483 * On Gen 8, SRM is now taking an extra dword to accommodate
9484 * 48bits addresses, and we need a NOOP for the batch size to
9485 * stay even.
9486 */
9487 if (IS_GEN8(dev))
9488 len += 2;
9489 }
ffe74d75 9490
f66fab8e
VS
9491 /*
9492 * BSpec MI_DISPLAY_FLIP for IVB:
9493 * "The full packet must be contained within the same cache line."
9494 *
9495 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9496 * cacheline, if we ever start emitting more commands before
9497 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9498 * then do the cacheline alignment, and finally emit the
9499 * MI_DISPLAY_FLIP.
9500 */
9501 ret = intel_ring_cacheline_align(ring);
9502 if (ret)
4fa62c89 9503 return ret;
f66fab8e 9504
ffe74d75 9505 ret = intel_ring_begin(ring, len);
7c9017e5 9506 if (ret)
4fa62c89 9507 return ret;
7c9017e5 9508
ffe74d75
CW
9509 /* Unmask the flip-done completion message. Note that the bspec says that
9510 * we should do this for both the BCS and RCS, and that we must not unmask
9511 * more than one flip event at any time (or ensure that one flip message
9512 * can be sent by waiting for flip-done prior to queueing new flips).
9513 * Experimentation says that BCS works despite DERRMR masking all
9514 * flip-done completion events and that unmasking all planes at once
9515 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9516 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9517 */
9518 if (ring->id == RCS) {
9519 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9520 intel_ring_emit(ring, DERRMR);
9521 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9522 DERRMR_PIPEB_PRI_FLIP_DONE |
9523 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9524 if (IS_GEN8(dev))
9525 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9526 MI_SRM_LRM_GLOBAL_GTT);
9527 else
9528 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9529 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9530 intel_ring_emit(ring, DERRMR);
9531 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9532 if (IS_GEN8(dev)) {
9533 intel_ring_emit(ring, 0);
9534 intel_ring_emit(ring, MI_NOOP);
9535 }
ffe74d75
CW
9536 }
9537
cb05d8de 9538 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9539 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9540 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9541 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9542
9543 intel_mark_page_flip_active(intel_crtc);
09246732 9544 __intel_ring_advance(ring);
83d4092b 9545 return 0;
7c9017e5
JB
9546}
9547
84c33a64
SG
9548static bool use_mmio_flip(struct intel_engine_cs *ring,
9549 struct drm_i915_gem_object *obj)
9550{
9551 /*
9552 * This is not being used for older platforms, because
9553 * non-availability of flip done interrupt forces us to use
9554 * CS flips. Older platforms derive flip done using some clever
9555 * tricks involving the flip_pending status bits and vblank irqs.
9556 * So using MMIO flips there would disrupt this mechanism.
9557 */
9558
8e09bf83
CW
9559 if (ring == NULL)
9560 return true;
9561
84c33a64
SG
9562 if (INTEL_INFO(ring->dev)->gen < 5)
9563 return false;
9564
9565 if (i915.use_mmio_flip < 0)
9566 return false;
9567 else if (i915.use_mmio_flip > 0)
9568 return true;
14bf993e
OM
9569 else if (i915.enable_execlists)
9570 return true;
84c33a64
SG
9571 else
9572 return ring != obj->ring;
9573}
9574
9575static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9576{
9577 struct drm_device *dev = intel_crtc->base.dev;
9578 struct drm_i915_private *dev_priv = dev->dev_private;
9579 struct intel_framebuffer *intel_fb =
9580 to_intel_framebuffer(intel_crtc->base.primary->fb);
9581 struct drm_i915_gem_object *obj = intel_fb->obj;
9582 u32 dspcntr;
9583 u32 reg;
9584
9585 intel_mark_page_flip_active(intel_crtc);
9586
9587 reg = DSPCNTR(intel_crtc->plane);
9588 dspcntr = I915_READ(reg);
9589
9590 if (INTEL_INFO(dev)->gen >= 4) {
9591 if (obj->tiling_mode != I915_TILING_NONE)
9592 dspcntr |= DISPPLANE_TILED;
9593 else
9594 dspcntr &= ~DISPPLANE_TILED;
9595 }
9596 I915_WRITE(reg, dspcntr);
9597
9598 I915_WRITE(DSPSURF(intel_crtc->plane),
9599 intel_crtc->unpin_work->gtt_offset);
9600 POSTING_READ(DSPSURF(intel_crtc->plane));
9601}
9602
9603static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9604{
9605 struct intel_engine_cs *ring;
9606 int ret;
9607
9608 lockdep_assert_held(&obj->base.dev->struct_mutex);
9609
9610 if (!obj->last_write_seqno)
9611 return 0;
9612
9613 ring = obj->ring;
9614
9615 if (i915_seqno_passed(ring->get_seqno(ring, true),
9616 obj->last_write_seqno))
9617 return 0;
9618
9619 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9620 if (ret)
9621 return ret;
9622
9623 if (WARN_ON(!ring->irq_get(ring)))
9624 return 0;
9625
9626 return 1;
9627}
9628
9629void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9630{
9631 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9632 struct intel_crtc *intel_crtc;
9633 unsigned long irq_flags;
9634 u32 seqno;
9635
9636 seqno = ring->get_seqno(ring, false);
9637
9638 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9639 for_each_intel_crtc(ring->dev, intel_crtc) {
9640 struct intel_mmio_flip *mmio_flip;
9641
9642 mmio_flip = &intel_crtc->mmio_flip;
9643 if (mmio_flip->seqno == 0)
9644 continue;
9645
9646 if (ring->id != mmio_flip->ring_id)
9647 continue;
9648
9649 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9650 intel_do_mmio_flip(intel_crtc);
9651 mmio_flip->seqno = 0;
9652 ring->irq_put(ring);
9653 }
9654 }
9655 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9656}
9657
9658static int intel_queue_mmio_flip(struct drm_device *dev,
9659 struct drm_crtc *crtc,
9660 struct drm_framebuffer *fb,
9661 struct drm_i915_gem_object *obj,
9662 struct intel_engine_cs *ring,
9663 uint32_t flags)
9664{
9665 struct drm_i915_private *dev_priv = dev->dev_private;
9666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64
SG
9667 int ret;
9668
9669 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9670 return -EBUSY;
9671
9672 ret = intel_postpone_flip(obj);
9673 if (ret < 0)
9674 return ret;
9675 if (ret == 0) {
9676 intel_do_mmio_flip(intel_crtc);
9677 return 0;
9678 }
9679
24955f24 9680 spin_lock_irq(&dev_priv->mmio_flip_lock);
84c33a64
SG
9681 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9682 intel_crtc->mmio_flip.ring_id = obj->ring->id;
24955f24 9683 spin_unlock_irq(&dev_priv->mmio_flip_lock);
84c33a64
SG
9684
9685 /*
9686 * Double check to catch cases where irq fired before
9687 * mmio flip data was ready
9688 */
9689 intel_notify_mmio_flip(obj->ring);
9690 return 0;
9691}
9692
8c9f3aaf
JB
9693static int intel_default_queue_flip(struct drm_device *dev,
9694 struct drm_crtc *crtc,
9695 struct drm_framebuffer *fb,
ed8d1975 9696 struct drm_i915_gem_object *obj,
a4872ba6 9697 struct intel_engine_cs *ring,
ed8d1975 9698 uint32_t flags)
8c9f3aaf
JB
9699{
9700 return -ENODEV;
9701}
9702
d6bbafa1
CW
9703static bool __intel_pageflip_stall_check(struct drm_device *dev,
9704 struct drm_crtc *crtc)
9705{
9706 struct drm_i915_private *dev_priv = dev->dev_private;
9707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9708 struct intel_unpin_work *work = intel_crtc->unpin_work;
9709 u32 addr;
9710
9711 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9712 return true;
9713
9714 if (!work->enable_stall_check)
9715 return false;
9716
9717 if (work->flip_ready_vblank == 0) {
9718 if (work->flip_queued_ring &&
9719 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9720 work->flip_queued_seqno))
9721 return false;
9722
9723 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9724 }
9725
9726 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9727 return false;
9728
9729 /* Potential stall - if we see that the flip has happened,
9730 * assume a missed interrupt. */
9731 if (INTEL_INFO(dev)->gen >= 4)
9732 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9733 else
9734 addr = I915_READ(DSPADDR(intel_crtc->plane));
9735
9736 /* There is a potential issue here with a false positive after a flip
9737 * to the same address. We could address this by checking for a
9738 * non-incrementing frame counter.
9739 */
9740 return addr == work->gtt_offset;
9741}
9742
9743void intel_check_page_flip(struct drm_device *dev, int pipe)
9744{
9745 struct drm_i915_private *dev_priv = dev->dev_private;
9746 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f326038a
DV
9748
9749 WARN_ON(!in_irq());
d6bbafa1
CW
9750
9751 if (crtc == NULL)
9752 return;
9753
f326038a 9754 spin_lock(&dev->event_lock);
d6bbafa1
CW
9755 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9756 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9757 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9758 page_flip_completed(intel_crtc);
9759 }
f326038a 9760 spin_unlock(&dev->event_lock);
d6bbafa1
CW
9761}
9762
6b95a207
KH
9763static int intel_crtc_page_flip(struct drm_crtc *crtc,
9764 struct drm_framebuffer *fb,
ed8d1975
KP
9765 struct drm_pending_vblank_event *event,
9766 uint32_t page_flip_flags)
6b95a207
KH
9767{
9768 struct drm_device *dev = crtc->dev;
9769 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9770 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9771 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9773 enum pipe pipe = intel_crtc->pipe;
6b95a207 9774 struct intel_unpin_work *work;
a4872ba6 9775 struct intel_engine_cs *ring;
52e68630 9776 int ret;
6b95a207 9777
2ff8fde1
MR
9778 /*
9779 * drm_mode_page_flip_ioctl() should already catch this, but double
9780 * check to be safe. In the future we may enable pageflipping from
9781 * a disabled primary plane.
9782 */
9783 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9784 return -EBUSY;
9785
e6a595d2 9786 /* Can't change pixel format via MI display flips. */
f4510a27 9787 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9788 return -EINVAL;
9789
9790 /*
9791 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9792 * Note that pitch changes could also affect these register.
9793 */
9794 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9795 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9796 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9797 return -EINVAL;
9798
f900db47
CW
9799 if (i915_terminally_wedged(&dev_priv->gpu_error))
9800 goto out_hang;
9801
b14c5679 9802 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9803 if (work == NULL)
9804 return -ENOMEM;
9805
6b95a207 9806 work->event = event;
b4a98e57 9807 work->crtc = crtc;
2ff8fde1 9808 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
9809 INIT_WORK(&work->work, intel_unpin_work_fn);
9810
87b6b101 9811 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9812 if (ret)
9813 goto free_work;
9814
6b95a207 9815 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 9816 spin_lock_irq(&dev->event_lock);
6b95a207 9817 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9818 /* Before declaring the flip queue wedged, check if
9819 * the hardware completed the operation behind our backs.
9820 */
9821 if (__intel_pageflip_stall_check(dev, crtc)) {
9822 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9823 page_flip_completed(intel_crtc);
9824 } else {
9825 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 9826 spin_unlock_irq(&dev->event_lock);
468f0b44 9827
d6bbafa1
CW
9828 drm_crtc_vblank_put(crtc);
9829 kfree(work);
9830 return -EBUSY;
9831 }
6b95a207
KH
9832 }
9833 intel_crtc->unpin_work = work;
5e2d7afc 9834 spin_unlock_irq(&dev->event_lock);
6b95a207 9835
b4a98e57
CW
9836 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9837 flush_workqueue(dev_priv->wq);
9838
79158103
CW
9839 ret = i915_mutex_lock_interruptible(dev);
9840 if (ret)
9841 goto cleanup;
6b95a207 9842
75dfca80 9843 /* Reference the objects for the scheduled work. */
05394f39
CW
9844 drm_gem_object_reference(&work->old_fb_obj->base);
9845 drm_gem_object_reference(&obj->base);
6b95a207 9846
f4510a27 9847 crtc->primary->fb = fb;
96b099fd 9848
e1f99ce6 9849 work->pending_flip_obj = obj;
e1f99ce6 9850
b4a98e57 9851 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9852 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9853
75f7f3ec 9854 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9855 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9856
4fa62c89
VS
9857 if (IS_VALLEYVIEW(dev)) {
9858 ring = &dev_priv->ring[BCS];
8e09bf83
CW
9859 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9860 /* vlv: DISPLAY_FLIP fails to change tiling */
9861 ring = NULL;
2a92d5bc
CW
9862 } else if (IS_IVYBRIDGE(dev)) {
9863 ring = &dev_priv->ring[BCS];
4fa62c89
VS
9864 } else if (INTEL_INFO(dev)->gen >= 7) {
9865 ring = obj->ring;
9866 if (ring == NULL || ring->id != RCS)
9867 ring = &dev_priv->ring[BCS];
9868 } else {
9869 ring = &dev_priv->ring[RCS];
9870 }
9871
9872 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9873 if (ret)
9874 goto cleanup_pending;
6b95a207 9875
4fa62c89
VS
9876 work->gtt_offset =
9877 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9878
d6bbafa1 9879 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
9880 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9881 page_flip_flags);
d6bbafa1
CW
9882 if (ret)
9883 goto cleanup_unpin;
9884
9885 work->flip_queued_seqno = obj->last_write_seqno;
9886 work->flip_queued_ring = obj->ring;
9887 } else {
84c33a64 9888 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
9889 page_flip_flags);
9890 if (ret)
9891 goto cleanup_unpin;
9892
9893 work->flip_queued_seqno = intel_ring_get_seqno(ring);
9894 work->flip_queued_ring = ring;
9895 }
9896
9897 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9898 work->enable_stall_check = true;
4fa62c89 9899
a071fa00
DV
9900 i915_gem_track_fb(work->old_fb_obj, obj,
9901 INTEL_FRONTBUFFER_PRIMARY(pipe));
9902
7782de3b 9903 intel_disable_fbc(dev);
f99d7069 9904 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9905 mutex_unlock(&dev->struct_mutex);
9906
e5510fac
JB
9907 trace_i915_flip_request(intel_crtc->plane, obj);
9908
6b95a207 9909 return 0;
96b099fd 9910
4fa62c89
VS
9911cleanup_unpin:
9912 intel_unpin_fb_obj(obj);
8c9f3aaf 9913cleanup_pending:
b4a98e57 9914 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9915 crtc->primary->fb = old_fb;
05394f39
CW
9916 drm_gem_object_unreference(&work->old_fb_obj->base);
9917 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9918 mutex_unlock(&dev->struct_mutex);
9919
79158103 9920cleanup:
5e2d7afc 9921 spin_lock_irq(&dev->event_lock);
96b099fd 9922 intel_crtc->unpin_work = NULL;
5e2d7afc 9923 spin_unlock_irq(&dev->event_lock);
96b099fd 9924
87b6b101 9925 drm_crtc_vblank_put(crtc);
7317c75e 9926free_work:
96b099fd
CW
9927 kfree(work);
9928
f900db47
CW
9929 if (ret == -EIO) {
9930out_hang:
9931 intel_crtc_wait_for_pending_flips(crtc);
9932 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
f0d3dad3 9933 if (ret == 0 && event) {
5e2d7afc 9934 spin_lock_irq(&dev->event_lock);
a071fa00 9935 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 9936 spin_unlock_irq(&dev->event_lock);
f0d3dad3 9937 }
f900db47 9938 }
96b099fd 9939 return ret;
6b95a207
KH
9940}
9941
f6e5b160 9942static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9943 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9944 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9945};
9946
9a935856
DV
9947/**
9948 * intel_modeset_update_staged_output_state
9949 *
9950 * Updates the staged output configuration state, e.g. after we've read out the
9951 * current hw state.
9952 */
9953static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9954{
7668851f 9955 struct intel_crtc *crtc;
9a935856
DV
9956 struct intel_encoder *encoder;
9957 struct intel_connector *connector;
f6e5b160 9958
9a935856
DV
9959 list_for_each_entry(connector, &dev->mode_config.connector_list,
9960 base.head) {
9961 connector->new_encoder =
9962 to_intel_encoder(connector->base.encoder);
9963 }
f6e5b160 9964
b2784e15 9965 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9966 encoder->new_crtc =
9967 to_intel_crtc(encoder->base.crtc);
9968 }
7668851f 9969
d3fcc808 9970 for_each_intel_crtc(dev, crtc) {
7668851f 9971 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9972
9973 if (crtc->new_enabled)
9974 crtc->new_config = &crtc->config;
9975 else
9976 crtc->new_config = NULL;
7668851f 9977 }
f6e5b160
CW
9978}
9979
9a935856
DV
9980/**
9981 * intel_modeset_commit_output_state
9982 *
9983 * This function copies the stage display pipe configuration to the real one.
9984 */
9985static void intel_modeset_commit_output_state(struct drm_device *dev)
9986{
7668851f 9987 struct intel_crtc *crtc;
9a935856
DV
9988 struct intel_encoder *encoder;
9989 struct intel_connector *connector;
f6e5b160 9990
9a935856
DV
9991 list_for_each_entry(connector, &dev->mode_config.connector_list,
9992 base.head) {
9993 connector->base.encoder = &connector->new_encoder->base;
9994 }
f6e5b160 9995
b2784e15 9996 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9997 encoder->base.crtc = &encoder->new_crtc->base;
9998 }
7668851f 9999
d3fcc808 10000 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10001 crtc->base.enabled = crtc->new_enabled;
10002 }
9a935856
DV
10003}
10004
050f7aeb 10005static void
eba905b2 10006connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
10007 struct intel_crtc_config *pipe_config)
10008{
10009 int bpp = pipe_config->pipe_bpp;
10010
10011 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10012 connector->base.base.id,
c23cc417 10013 connector->base.name);
050f7aeb
DV
10014
10015 /* Don't use an invalid EDID bpc value */
10016 if (connector->base.display_info.bpc &&
10017 connector->base.display_info.bpc * 3 < bpp) {
10018 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10019 bpp, connector->base.display_info.bpc*3);
10020 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10021 }
10022
10023 /* Clamp bpp to 8 on screens without EDID 1.4 */
10024 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10025 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10026 bpp);
10027 pipe_config->pipe_bpp = 24;
10028 }
10029}
10030
4e53c2e0 10031static int
050f7aeb
DV
10032compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10033 struct drm_framebuffer *fb,
10034 struct intel_crtc_config *pipe_config)
4e53c2e0 10035{
050f7aeb
DV
10036 struct drm_device *dev = crtc->base.dev;
10037 struct intel_connector *connector;
4e53c2e0
DV
10038 int bpp;
10039
d42264b1
DV
10040 switch (fb->pixel_format) {
10041 case DRM_FORMAT_C8:
4e53c2e0
DV
10042 bpp = 8*3; /* since we go through a colormap */
10043 break;
d42264b1
DV
10044 case DRM_FORMAT_XRGB1555:
10045 case DRM_FORMAT_ARGB1555:
10046 /* checked in intel_framebuffer_init already */
10047 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10048 return -EINVAL;
10049 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10050 bpp = 6*3; /* min is 18bpp */
10051 break;
d42264b1
DV
10052 case DRM_FORMAT_XBGR8888:
10053 case DRM_FORMAT_ABGR8888:
10054 /* checked in intel_framebuffer_init already */
10055 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10056 return -EINVAL;
10057 case DRM_FORMAT_XRGB8888:
10058 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10059 bpp = 8*3;
10060 break;
d42264b1
DV
10061 case DRM_FORMAT_XRGB2101010:
10062 case DRM_FORMAT_ARGB2101010:
10063 case DRM_FORMAT_XBGR2101010:
10064 case DRM_FORMAT_ABGR2101010:
10065 /* checked in intel_framebuffer_init already */
10066 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10067 return -EINVAL;
4e53c2e0
DV
10068 bpp = 10*3;
10069 break;
baba133a 10070 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10071 default:
10072 DRM_DEBUG_KMS("unsupported depth\n");
10073 return -EINVAL;
10074 }
10075
4e53c2e0
DV
10076 pipe_config->pipe_bpp = bpp;
10077
10078 /* Clamp display bpp to EDID value */
10079 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10080 base.head) {
1b829e05
DV
10081 if (!connector->new_encoder ||
10082 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10083 continue;
10084
050f7aeb 10085 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10086 }
10087
10088 return bpp;
10089}
10090
644db711
DV
10091static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10092{
10093 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10094 "type: 0x%x flags: 0x%x\n",
1342830c 10095 mode->crtc_clock,
644db711
DV
10096 mode->crtc_hdisplay, mode->crtc_hsync_start,
10097 mode->crtc_hsync_end, mode->crtc_htotal,
10098 mode->crtc_vdisplay, mode->crtc_vsync_start,
10099 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10100}
10101
c0b03411
DV
10102static void intel_dump_pipe_config(struct intel_crtc *crtc,
10103 struct intel_crtc_config *pipe_config,
10104 const char *context)
10105{
10106 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10107 context, pipe_name(crtc->pipe));
10108
10109 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10110 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10111 pipe_config->pipe_bpp, pipe_config->dither);
10112 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10113 pipe_config->has_pch_encoder,
10114 pipe_config->fdi_lanes,
10115 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10116 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10117 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10118 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10119 pipe_config->has_dp_encoder,
10120 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10121 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10122 pipe_config->dp_m_n.tu);
b95af8be
VK
10123
10124 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10125 pipe_config->has_dp_encoder,
10126 pipe_config->dp_m2_n2.gmch_m,
10127 pipe_config->dp_m2_n2.gmch_n,
10128 pipe_config->dp_m2_n2.link_m,
10129 pipe_config->dp_m2_n2.link_n,
10130 pipe_config->dp_m2_n2.tu);
10131
c0b03411
DV
10132 DRM_DEBUG_KMS("requested mode:\n");
10133 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10134 DRM_DEBUG_KMS("adjusted mode:\n");
10135 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10136 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10137 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10138 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10139 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10140 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10141 pipe_config->gmch_pfit.control,
10142 pipe_config->gmch_pfit.pgm_ratios,
10143 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10144 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10145 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10146 pipe_config->pch_pfit.size,
10147 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10148 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10149 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10150}
10151
bc079e8b
VS
10152static bool encoders_cloneable(const struct intel_encoder *a,
10153 const struct intel_encoder *b)
accfc0c5 10154{
bc079e8b
VS
10155 /* masks could be asymmetric, so check both ways */
10156 return a == b || (a->cloneable & (1 << b->type) &&
10157 b->cloneable & (1 << a->type));
10158}
10159
10160static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10161 struct intel_encoder *encoder)
10162{
10163 struct drm_device *dev = crtc->base.dev;
10164 struct intel_encoder *source_encoder;
10165
b2784e15 10166 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10167 if (source_encoder->new_crtc != crtc)
10168 continue;
10169
10170 if (!encoders_cloneable(encoder, source_encoder))
10171 return false;
10172 }
10173
10174 return true;
10175}
10176
10177static bool check_encoder_cloning(struct intel_crtc *crtc)
10178{
10179 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10180 struct intel_encoder *encoder;
10181
b2784e15 10182 for_each_intel_encoder(dev, encoder) {
bc079e8b 10183 if (encoder->new_crtc != crtc)
accfc0c5
DV
10184 continue;
10185
bc079e8b
VS
10186 if (!check_single_encoder_cloning(crtc, encoder))
10187 return false;
accfc0c5
DV
10188 }
10189
bc079e8b 10190 return true;
accfc0c5
DV
10191}
10192
b8cecdf5
DV
10193static struct intel_crtc_config *
10194intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10195 struct drm_framebuffer *fb,
b8cecdf5 10196 struct drm_display_mode *mode)
ee7b9f93 10197{
7758a113 10198 struct drm_device *dev = crtc->dev;
7758a113 10199 struct intel_encoder *encoder;
b8cecdf5 10200 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10201 int plane_bpp, ret = -EINVAL;
10202 bool retry = true;
ee7b9f93 10203
bc079e8b 10204 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10205 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10206 return ERR_PTR(-EINVAL);
10207 }
10208
b8cecdf5
DV
10209 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10210 if (!pipe_config)
7758a113
DV
10211 return ERR_PTR(-ENOMEM);
10212
b8cecdf5
DV
10213 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10214 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10215
e143a21c
DV
10216 pipe_config->cpu_transcoder =
10217 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10218 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10219
2960bc9c
ID
10220 /*
10221 * Sanitize sync polarity flags based on requested ones. If neither
10222 * positive or negative polarity is requested, treat this as meaning
10223 * negative polarity.
10224 */
10225 if (!(pipe_config->adjusted_mode.flags &
10226 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10227 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10228
10229 if (!(pipe_config->adjusted_mode.flags &
10230 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10231 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10232
050f7aeb
DV
10233 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10234 * plane pixel format and any sink constraints into account. Returns the
10235 * source plane bpp so that dithering can be selected on mismatches
10236 * after encoders and crtc also have had their say. */
10237 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10238 fb, pipe_config);
4e53c2e0
DV
10239 if (plane_bpp < 0)
10240 goto fail;
10241
e41a56be
VS
10242 /*
10243 * Determine the real pipe dimensions. Note that stereo modes can
10244 * increase the actual pipe size due to the frame doubling and
10245 * insertion of additional space for blanks between the frame. This
10246 * is stored in the crtc timings. We use the requested mode to do this
10247 * computation to clearly distinguish it from the adjusted mode, which
10248 * can be changed by the connectors in the below retry loop.
10249 */
10250 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10251 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10252 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10253
e29c22c0 10254encoder_retry:
ef1b460d 10255 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10256 pipe_config->port_clock = 0;
ef1b460d 10257 pipe_config->pixel_multiplier = 1;
ff9a6750 10258
135c81b8 10259 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10260 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10261
7758a113
DV
10262 /* Pass our mode to the connectors and the CRTC to give them a chance to
10263 * adjust it according to limitations or connector properties, and also
10264 * a chance to reject the mode entirely.
47f1c6c9 10265 */
b2784e15 10266 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10267
7758a113
DV
10268 if (&encoder->new_crtc->base != crtc)
10269 continue;
7ae89233 10270
efea6e8e
DV
10271 if (!(encoder->compute_config(encoder, pipe_config))) {
10272 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10273 goto fail;
10274 }
ee7b9f93 10275 }
47f1c6c9 10276
ff9a6750
DV
10277 /* Set default port clock if not overwritten by the encoder. Needs to be
10278 * done afterwards in case the encoder adjusts the mode. */
10279 if (!pipe_config->port_clock)
241bfc38
DL
10280 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10281 * pipe_config->pixel_multiplier;
ff9a6750 10282
a43f6e0f 10283 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10284 if (ret < 0) {
7758a113
DV
10285 DRM_DEBUG_KMS("CRTC fixup failed\n");
10286 goto fail;
ee7b9f93 10287 }
e29c22c0
DV
10288
10289 if (ret == RETRY) {
10290 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10291 ret = -EINVAL;
10292 goto fail;
10293 }
10294
10295 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10296 retry = false;
10297 goto encoder_retry;
10298 }
10299
4e53c2e0
DV
10300 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10301 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10302 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10303
b8cecdf5 10304 return pipe_config;
7758a113 10305fail:
b8cecdf5 10306 kfree(pipe_config);
e29c22c0 10307 return ERR_PTR(ret);
ee7b9f93 10308}
47f1c6c9 10309
e2e1ed41
DV
10310/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10311 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10312static void
10313intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10314 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10315{
10316 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10317 struct drm_device *dev = crtc->dev;
10318 struct intel_encoder *encoder;
10319 struct intel_connector *connector;
10320 struct drm_crtc *tmp_crtc;
79e53945 10321
e2e1ed41 10322 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10323
e2e1ed41
DV
10324 /* Check which crtcs have changed outputs connected to them, these need
10325 * to be part of the prepare_pipes mask. We don't (yet) support global
10326 * modeset across multiple crtcs, so modeset_pipes will only have one
10327 * bit set at most. */
10328 list_for_each_entry(connector, &dev->mode_config.connector_list,
10329 base.head) {
10330 if (connector->base.encoder == &connector->new_encoder->base)
10331 continue;
79e53945 10332
e2e1ed41
DV
10333 if (connector->base.encoder) {
10334 tmp_crtc = connector->base.encoder->crtc;
10335
10336 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10337 }
10338
10339 if (connector->new_encoder)
10340 *prepare_pipes |=
10341 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10342 }
10343
b2784e15 10344 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10345 if (encoder->base.crtc == &encoder->new_crtc->base)
10346 continue;
10347
10348 if (encoder->base.crtc) {
10349 tmp_crtc = encoder->base.crtc;
10350
10351 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10352 }
10353
10354 if (encoder->new_crtc)
10355 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10356 }
10357
7668851f 10358 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10359 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10360 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10361 continue;
7e7d76c3 10362
7668851f 10363 if (!intel_crtc->new_enabled)
e2e1ed41 10364 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10365 else
10366 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10367 }
10368
e2e1ed41
DV
10369
10370 /* set_mode is also used to update properties on life display pipes. */
10371 intel_crtc = to_intel_crtc(crtc);
7668851f 10372 if (intel_crtc->new_enabled)
e2e1ed41
DV
10373 *prepare_pipes |= 1 << intel_crtc->pipe;
10374
b6c5164d
DV
10375 /*
10376 * For simplicity do a full modeset on any pipe where the output routing
10377 * changed. We could be more clever, but that would require us to be
10378 * more careful with calling the relevant encoder->mode_set functions.
10379 */
e2e1ed41
DV
10380 if (*prepare_pipes)
10381 *modeset_pipes = *prepare_pipes;
10382
10383 /* ... and mask these out. */
10384 *modeset_pipes &= ~(*disable_pipes);
10385 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10386
10387 /*
10388 * HACK: We don't (yet) fully support global modesets. intel_set_config
10389 * obies this rule, but the modeset restore mode of
10390 * intel_modeset_setup_hw_state does not.
10391 */
10392 *modeset_pipes &= 1 << intel_crtc->pipe;
10393 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10394
10395 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10396 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10397}
79e53945 10398
ea9d758d 10399static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10400{
ea9d758d 10401 struct drm_encoder *encoder;
f6e5b160 10402 struct drm_device *dev = crtc->dev;
f6e5b160 10403
ea9d758d
DV
10404 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10405 if (encoder->crtc == crtc)
10406 return true;
10407
10408 return false;
10409}
10410
10411static void
10412intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10413{
10414 struct intel_encoder *intel_encoder;
10415 struct intel_crtc *intel_crtc;
10416 struct drm_connector *connector;
10417
b2784e15 10418 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10419 if (!intel_encoder->base.crtc)
10420 continue;
10421
10422 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10423
10424 if (prepare_pipes & (1 << intel_crtc->pipe))
10425 intel_encoder->connectors_active = false;
10426 }
10427
10428 intel_modeset_commit_output_state(dev);
10429
7668851f 10430 /* Double check state. */
d3fcc808 10431 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10432 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10433 WARN_ON(intel_crtc->new_config &&
10434 intel_crtc->new_config != &intel_crtc->config);
10435 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10436 }
10437
10438 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10439 if (!connector->encoder || !connector->encoder->crtc)
10440 continue;
10441
10442 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10443
10444 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10445 struct drm_property *dpms_property =
10446 dev->mode_config.dpms_property;
10447
ea9d758d 10448 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10449 drm_object_property_set_value(&connector->base,
68d34720
DV
10450 dpms_property,
10451 DRM_MODE_DPMS_ON);
ea9d758d
DV
10452
10453 intel_encoder = to_intel_encoder(connector->encoder);
10454 intel_encoder->connectors_active = true;
10455 }
10456 }
10457
10458}
10459
3bd26263 10460static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10461{
3bd26263 10462 int diff;
f1f644dc
JB
10463
10464 if (clock1 == clock2)
10465 return true;
10466
10467 if (!clock1 || !clock2)
10468 return false;
10469
10470 diff = abs(clock1 - clock2);
10471
10472 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10473 return true;
10474
10475 return false;
10476}
10477
25c5b266
DV
10478#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10479 list_for_each_entry((intel_crtc), \
10480 &(dev)->mode_config.crtc_list, \
10481 base.head) \
0973f18f 10482 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10483
0e8ffe1b 10484static bool
2fa2fe9a
DV
10485intel_pipe_config_compare(struct drm_device *dev,
10486 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10487 struct intel_crtc_config *pipe_config)
10488{
66e985c0
DV
10489#define PIPE_CONF_CHECK_X(name) \
10490 if (current_config->name != pipe_config->name) { \
10491 DRM_ERROR("mismatch in " #name " " \
10492 "(expected 0x%08x, found 0x%08x)\n", \
10493 current_config->name, \
10494 pipe_config->name); \
10495 return false; \
10496 }
10497
08a24034
DV
10498#define PIPE_CONF_CHECK_I(name) \
10499 if (current_config->name != pipe_config->name) { \
10500 DRM_ERROR("mismatch in " #name " " \
10501 "(expected %i, found %i)\n", \
10502 current_config->name, \
10503 pipe_config->name); \
10504 return false; \
88adfff1
DV
10505 }
10506
b95af8be
VK
10507/* This is required for BDW+ where there is only one set of registers for
10508 * switching between high and low RR.
10509 * This macro can be used whenever a comparison has to be made between one
10510 * hw state and multiple sw state variables.
10511 */
10512#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10513 if ((current_config->name != pipe_config->name) && \
10514 (current_config->alt_name != pipe_config->name)) { \
10515 DRM_ERROR("mismatch in " #name " " \
10516 "(expected %i or %i, found %i)\n", \
10517 current_config->name, \
10518 current_config->alt_name, \
10519 pipe_config->name); \
10520 return false; \
10521 }
10522
1bd1bd80
DV
10523#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10524 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10525 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10526 "(expected %i, found %i)\n", \
10527 current_config->name & (mask), \
10528 pipe_config->name & (mask)); \
10529 return false; \
10530 }
10531
5e550656
VS
10532#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10533 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10534 DRM_ERROR("mismatch in " #name " " \
10535 "(expected %i, found %i)\n", \
10536 current_config->name, \
10537 pipe_config->name); \
10538 return false; \
10539 }
10540
bb760063
DV
10541#define PIPE_CONF_QUIRK(quirk) \
10542 ((current_config->quirks | pipe_config->quirks) & (quirk))
10543
eccb140b
DV
10544 PIPE_CONF_CHECK_I(cpu_transcoder);
10545
08a24034
DV
10546 PIPE_CONF_CHECK_I(has_pch_encoder);
10547 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10548 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10549 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10550 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10551 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10552 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10553
eb14cb74 10554 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10555
10556 if (INTEL_INFO(dev)->gen < 8) {
10557 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10558 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10559 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10560 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10561 PIPE_CONF_CHECK_I(dp_m_n.tu);
10562
10563 if (current_config->has_drrs) {
10564 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10565 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10566 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10567 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10568 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10569 }
10570 } else {
10571 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10572 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10573 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10574 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10575 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10576 }
eb14cb74 10577
1bd1bd80
DV
10578 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10579 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10580 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10581 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10582 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10583 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10584
10585 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10586 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10587 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10588 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10589 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10590 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10591
c93f54cf 10592 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10593 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10594 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10595 IS_VALLEYVIEW(dev))
10596 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10597
9ed109a7
DV
10598 PIPE_CONF_CHECK_I(has_audio);
10599
1bd1bd80
DV
10600 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10601 DRM_MODE_FLAG_INTERLACE);
10602
bb760063
DV
10603 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10604 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10605 DRM_MODE_FLAG_PHSYNC);
10606 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10607 DRM_MODE_FLAG_NHSYNC);
10608 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10609 DRM_MODE_FLAG_PVSYNC);
10610 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10611 DRM_MODE_FLAG_NVSYNC);
10612 }
045ac3b5 10613
37327abd
VS
10614 PIPE_CONF_CHECK_I(pipe_src_w);
10615 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10616
9953599b
DV
10617 /*
10618 * FIXME: BIOS likes to set up a cloned config with lvds+external
10619 * screen. Since we don't yet re-compute the pipe config when moving
10620 * just the lvds port away to another pipe the sw tracking won't match.
10621 *
10622 * Proper atomic modesets with recomputed global state will fix this.
10623 * Until then just don't check gmch state for inherited modes.
10624 */
10625 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10626 PIPE_CONF_CHECK_I(gmch_pfit.control);
10627 /* pfit ratios are autocomputed by the hw on gen4+ */
10628 if (INTEL_INFO(dev)->gen < 4)
10629 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10630 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10631 }
10632
fd4daa9c
CW
10633 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10634 if (current_config->pch_pfit.enabled) {
10635 PIPE_CONF_CHECK_I(pch_pfit.pos);
10636 PIPE_CONF_CHECK_I(pch_pfit.size);
10637 }
2fa2fe9a 10638
e59150dc
JB
10639 /* BDW+ don't expose a synchronous way to read the state */
10640 if (IS_HASWELL(dev))
10641 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10642
282740f7
VS
10643 PIPE_CONF_CHECK_I(double_wide);
10644
26804afd
DV
10645 PIPE_CONF_CHECK_X(ddi_pll_sel);
10646
c0d43d62 10647 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10648 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10649 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10650 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10651 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10652 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10653
42571aef
VS
10654 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10655 PIPE_CONF_CHECK_I(pipe_bpp);
10656
a9a7e98a
JB
10657 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10658 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10659
66e985c0 10660#undef PIPE_CONF_CHECK_X
08a24034 10661#undef PIPE_CONF_CHECK_I
b95af8be 10662#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10663#undef PIPE_CONF_CHECK_FLAGS
5e550656 10664#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10665#undef PIPE_CONF_QUIRK
88adfff1 10666
0e8ffe1b
DV
10667 return true;
10668}
10669
91d1b4bd
DV
10670static void
10671check_connector_state(struct drm_device *dev)
8af6cf88 10672{
8af6cf88
DV
10673 struct intel_connector *connector;
10674
10675 list_for_each_entry(connector, &dev->mode_config.connector_list,
10676 base.head) {
10677 /* This also checks the encoder/connector hw state with the
10678 * ->get_hw_state callbacks. */
10679 intel_connector_check_state(connector);
10680
10681 WARN(&connector->new_encoder->base != connector->base.encoder,
10682 "connector's staged encoder doesn't match current encoder\n");
10683 }
91d1b4bd
DV
10684}
10685
10686static void
10687check_encoder_state(struct drm_device *dev)
10688{
10689 struct intel_encoder *encoder;
10690 struct intel_connector *connector;
8af6cf88 10691
b2784e15 10692 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10693 bool enabled = false;
10694 bool active = false;
10695 enum pipe pipe, tracked_pipe;
10696
10697 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10698 encoder->base.base.id,
8e329a03 10699 encoder->base.name);
8af6cf88
DV
10700
10701 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10702 "encoder's stage crtc doesn't match current crtc\n");
10703 WARN(encoder->connectors_active && !encoder->base.crtc,
10704 "encoder's active_connectors set, but no crtc\n");
10705
10706 list_for_each_entry(connector, &dev->mode_config.connector_list,
10707 base.head) {
10708 if (connector->base.encoder != &encoder->base)
10709 continue;
10710 enabled = true;
10711 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10712 active = true;
10713 }
0e32b39c
DA
10714 /*
10715 * for MST connectors if we unplug the connector is gone
10716 * away but the encoder is still connected to a crtc
10717 * until a modeset happens in response to the hotplug.
10718 */
10719 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10720 continue;
10721
8af6cf88
DV
10722 WARN(!!encoder->base.crtc != enabled,
10723 "encoder's enabled state mismatch "
10724 "(expected %i, found %i)\n",
10725 !!encoder->base.crtc, enabled);
10726 WARN(active && !encoder->base.crtc,
10727 "active encoder with no crtc\n");
10728
10729 WARN(encoder->connectors_active != active,
10730 "encoder's computed active state doesn't match tracked active state "
10731 "(expected %i, found %i)\n", active, encoder->connectors_active);
10732
10733 active = encoder->get_hw_state(encoder, &pipe);
10734 WARN(active != encoder->connectors_active,
10735 "encoder's hw state doesn't match sw tracking "
10736 "(expected %i, found %i)\n",
10737 encoder->connectors_active, active);
10738
10739 if (!encoder->base.crtc)
10740 continue;
10741
10742 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10743 WARN(active && pipe != tracked_pipe,
10744 "active encoder's pipe doesn't match"
10745 "(expected %i, found %i)\n",
10746 tracked_pipe, pipe);
10747
10748 }
91d1b4bd
DV
10749}
10750
10751static void
10752check_crtc_state(struct drm_device *dev)
10753{
fbee40df 10754 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10755 struct intel_crtc *crtc;
10756 struct intel_encoder *encoder;
10757 struct intel_crtc_config pipe_config;
8af6cf88 10758
d3fcc808 10759 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10760 bool enabled = false;
10761 bool active = false;
10762
045ac3b5
JB
10763 memset(&pipe_config, 0, sizeof(pipe_config));
10764
8af6cf88
DV
10765 DRM_DEBUG_KMS("[CRTC:%d]\n",
10766 crtc->base.base.id);
10767
10768 WARN(crtc->active && !crtc->base.enabled,
10769 "active crtc, but not enabled in sw tracking\n");
10770
b2784e15 10771 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10772 if (encoder->base.crtc != &crtc->base)
10773 continue;
10774 enabled = true;
10775 if (encoder->connectors_active)
10776 active = true;
10777 }
6c49f241 10778
8af6cf88
DV
10779 WARN(active != crtc->active,
10780 "crtc's computed active state doesn't match tracked active state "
10781 "(expected %i, found %i)\n", active, crtc->active);
10782 WARN(enabled != crtc->base.enabled,
10783 "crtc's computed enabled state doesn't match tracked enabled state "
10784 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10785
0e8ffe1b
DV
10786 active = dev_priv->display.get_pipe_config(crtc,
10787 &pipe_config);
d62cf62a 10788
b6b5d049
VS
10789 /* hw state is inconsistent with the pipe quirk */
10790 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10791 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10792 active = crtc->active;
10793
b2784e15 10794 for_each_intel_encoder(dev, encoder) {
3eaba51c 10795 enum pipe pipe;
6c49f241
DV
10796 if (encoder->base.crtc != &crtc->base)
10797 continue;
1d37b689 10798 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10799 encoder->get_config(encoder, &pipe_config);
10800 }
10801
0e8ffe1b
DV
10802 WARN(crtc->active != active,
10803 "crtc active state doesn't match with hw state "
10804 "(expected %i, found %i)\n", crtc->active, active);
10805
c0b03411
DV
10806 if (active &&
10807 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10808 WARN(1, "pipe state doesn't match!\n");
10809 intel_dump_pipe_config(crtc, &pipe_config,
10810 "[hw state]");
10811 intel_dump_pipe_config(crtc, &crtc->config,
10812 "[sw state]");
10813 }
8af6cf88
DV
10814 }
10815}
10816
91d1b4bd
DV
10817static void
10818check_shared_dpll_state(struct drm_device *dev)
10819{
fbee40df 10820 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10821 struct intel_crtc *crtc;
10822 struct intel_dpll_hw_state dpll_hw_state;
10823 int i;
5358901f
DV
10824
10825 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10826 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10827 int enabled_crtcs = 0, active_crtcs = 0;
10828 bool active;
10829
10830 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10831
10832 DRM_DEBUG_KMS("%s\n", pll->name);
10833
10834 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10835
10836 WARN(pll->active > pll->refcount,
10837 "more active pll users than references: %i vs %i\n",
10838 pll->active, pll->refcount);
10839 WARN(pll->active && !pll->on,
10840 "pll in active use but not on in sw tracking\n");
35c95375
DV
10841 WARN(pll->on && !pll->active,
10842 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10843 WARN(pll->on != active,
10844 "pll on state mismatch (expected %i, found %i)\n",
10845 pll->on, active);
10846
d3fcc808 10847 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10848 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10849 enabled_crtcs++;
10850 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10851 active_crtcs++;
10852 }
10853 WARN(pll->active != active_crtcs,
10854 "pll active crtcs mismatch (expected %i, found %i)\n",
10855 pll->active, active_crtcs);
10856 WARN(pll->refcount != enabled_crtcs,
10857 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10858 pll->refcount, enabled_crtcs);
66e985c0
DV
10859
10860 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10861 sizeof(dpll_hw_state)),
10862 "pll hw state mismatch\n");
5358901f 10863 }
8af6cf88
DV
10864}
10865
91d1b4bd
DV
10866void
10867intel_modeset_check_state(struct drm_device *dev)
10868{
10869 check_connector_state(dev);
10870 check_encoder_state(dev);
10871 check_crtc_state(dev);
10872 check_shared_dpll_state(dev);
10873}
10874
18442d08
VS
10875void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10876 int dotclock)
10877{
10878 /*
10879 * FDI already provided one idea for the dotclock.
10880 * Yell if the encoder disagrees.
10881 */
241bfc38 10882 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10883 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10884 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10885}
10886
80715b2f
VS
10887static void update_scanline_offset(struct intel_crtc *crtc)
10888{
10889 struct drm_device *dev = crtc->base.dev;
10890
10891 /*
10892 * The scanline counter increments at the leading edge of hsync.
10893 *
10894 * On most platforms it starts counting from vtotal-1 on the
10895 * first active line. That means the scanline counter value is
10896 * always one less than what we would expect. Ie. just after
10897 * start of vblank, which also occurs at start of hsync (on the
10898 * last active line), the scanline counter will read vblank_start-1.
10899 *
10900 * On gen2 the scanline counter starts counting from 1 instead
10901 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10902 * to keep the value positive), instead of adding one.
10903 *
10904 * On HSW+ the behaviour of the scanline counter depends on the output
10905 * type. For DP ports it behaves like most other platforms, but on HDMI
10906 * there's an extra 1 line difference. So we need to add two instead of
10907 * one to the value.
10908 */
10909 if (IS_GEN2(dev)) {
10910 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10911 int vtotal;
10912
10913 vtotal = mode->crtc_vtotal;
10914 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10915 vtotal /= 2;
10916
10917 crtc->scanline_offset = vtotal - 1;
10918 } else if (HAS_DDI(dev) &&
10919 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10920 crtc->scanline_offset = 2;
10921 } else
10922 crtc->scanline_offset = 1;
10923}
10924
f30da187
DV
10925static int __intel_set_mode(struct drm_crtc *crtc,
10926 struct drm_display_mode *mode,
10927 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10928{
10929 struct drm_device *dev = crtc->dev;
fbee40df 10930 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10931 struct drm_display_mode *saved_mode;
b8cecdf5 10932 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10933 struct intel_crtc *intel_crtc;
10934 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10935 int ret = 0;
a6778b3c 10936
4b4b9238 10937 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10938 if (!saved_mode)
10939 return -ENOMEM;
a6778b3c 10940
e2e1ed41 10941 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10942 &prepare_pipes, &disable_pipes);
10943
3ac18232 10944 *saved_mode = crtc->mode;
a6778b3c 10945
25c5b266
DV
10946 /* Hack: Because we don't (yet) support global modeset on multiple
10947 * crtcs, we don't keep track of the new mode for more than one crtc.
10948 * Hence simply check whether any bit is set in modeset_pipes in all the
10949 * pieces of code that are not yet converted to deal with mutliple crtcs
10950 * changing their mode at the same time. */
25c5b266 10951 if (modeset_pipes) {
4e53c2e0 10952 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10953 if (IS_ERR(pipe_config)) {
10954 ret = PTR_ERR(pipe_config);
10955 pipe_config = NULL;
10956
3ac18232 10957 goto out;
25c5b266 10958 }
c0b03411
DV
10959 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10960 "[modeset]");
50741abc 10961 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10962 }
a6778b3c 10963
30a970c6
JB
10964 /*
10965 * See if the config requires any additional preparation, e.g.
10966 * to adjust global state with pipes off. We need to do this
10967 * here so we can get the modeset_pipe updated config for the new
10968 * mode set on this crtc. For other crtcs we need to use the
10969 * adjusted_mode bits in the crtc directly.
10970 */
c164f833 10971 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10972 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10973
c164f833
VS
10974 /* may have added more to prepare_pipes than we should */
10975 prepare_pipes &= ~disable_pipes;
10976 }
10977
460da916
DV
10978 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10979 intel_crtc_disable(&intel_crtc->base);
10980
ea9d758d
DV
10981 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10982 if (intel_crtc->base.enabled)
10983 dev_priv->display.crtc_disable(&intel_crtc->base);
10984 }
a6778b3c 10985
6c4c86f5
DV
10986 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10987 * to set it here already despite that we pass it down the callchain.
f6e5b160 10988 */
b8cecdf5 10989 if (modeset_pipes) {
25c5b266 10990 crtc->mode = *mode;
b8cecdf5
DV
10991 /* mode_set/enable/disable functions rely on a correct pipe
10992 * config. */
10993 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10994 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10995
10996 /*
10997 * Calculate and store various constants which
10998 * are later needed by vblank and swap-completion
10999 * timestamping. They are derived from true hwmode.
11000 */
11001 drm_calc_timestamping_constants(crtc,
11002 &pipe_config->adjusted_mode);
b8cecdf5 11003 }
7758a113 11004
ea9d758d
DV
11005 /* Only after disabling all output pipelines that will be changed can we
11006 * update the the output configuration. */
11007 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11008
47fab737
DV
11009 if (dev_priv->display.modeset_global_resources)
11010 dev_priv->display.modeset_global_resources(dev);
11011
a6778b3c
DV
11012 /* Set up the DPLL and any encoders state that needs to adjust or depend
11013 * on the DPLL.
f6e5b160 11014 */
25c5b266 11015 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
11016 struct drm_framebuffer *old_fb = crtc->primary->fb;
11017 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11018 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
11019
11020 mutex_lock(&dev->struct_mutex);
11021 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 11022 obj,
4c10794f
DV
11023 NULL);
11024 if (ret != 0) {
11025 DRM_ERROR("pin & fence failed\n");
11026 mutex_unlock(&dev->struct_mutex);
11027 goto done;
11028 }
2ff8fde1 11029 if (old_fb)
a071fa00 11030 intel_unpin_fb_obj(old_obj);
a071fa00
DV
11031 i915_gem_track_fb(old_obj, obj,
11032 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
11033 mutex_unlock(&dev->struct_mutex);
11034
11035 crtc->primary->fb = fb;
11036 crtc->x = x;
11037 crtc->y = y;
11038
4271b753
DV
11039 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11040 x, y, fb);
c0c36b94
CW
11041 if (ret)
11042 goto done;
a6778b3c
DV
11043 }
11044
11045 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11046 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11047 update_scanline_offset(intel_crtc);
11048
25c5b266 11049 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11050 }
a6778b3c 11051
a6778b3c
DV
11052 /* FIXME: add subpixel order */
11053done:
4b4b9238 11054 if (ret && crtc->enabled)
3ac18232 11055 crtc->mode = *saved_mode;
a6778b3c 11056
3ac18232 11057out:
b8cecdf5 11058 kfree(pipe_config);
3ac18232 11059 kfree(saved_mode);
a6778b3c 11060 return ret;
f6e5b160
CW
11061}
11062
e7457a9a
DL
11063static int intel_set_mode(struct drm_crtc *crtc,
11064 struct drm_display_mode *mode,
11065 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11066{
11067 int ret;
11068
11069 ret = __intel_set_mode(crtc, mode, x, y, fb);
11070
11071 if (ret == 0)
11072 intel_modeset_check_state(crtc->dev);
11073
11074 return ret;
11075}
11076
c0c36b94
CW
11077void intel_crtc_restore_mode(struct drm_crtc *crtc)
11078{
f4510a27 11079 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11080}
11081
25c5b266
DV
11082#undef for_each_intel_crtc_masked
11083
d9e55608
DV
11084static void intel_set_config_free(struct intel_set_config *config)
11085{
11086 if (!config)
11087 return;
11088
1aa4b628
DV
11089 kfree(config->save_connector_encoders);
11090 kfree(config->save_encoder_crtcs);
7668851f 11091 kfree(config->save_crtc_enabled);
d9e55608
DV
11092 kfree(config);
11093}
11094
85f9eb71
DV
11095static int intel_set_config_save_state(struct drm_device *dev,
11096 struct intel_set_config *config)
11097{
7668851f 11098 struct drm_crtc *crtc;
85f9eb71
DV
11099 struct drm_encoder *encoder;
11100 struct drm_connector *connector;
11101 int count;
11102
7668851f
VS
11103 config->save_crtc_enabled =
11104 kcalloc(dev->mode_config.num_crtc,
11105 sizeof(bool), GFP_KERNEL);
11106 if (!config->save_crtc_enabled)
11107 return -ENOMEM;
11108
1aa4b628
DV
11109 config->save_encoder_crtcs =
11110 kcalloc(dev->mode_config.num_encoder,
11111 sizeof(struct drm_crtc *), GFP_KERNEL);
11112 if (!config->save_encoder_crtcs)
85f9eb71
DV
11113 return -ENOMEM;
11114
1aa4b628
DV
11115 config->save_connector_encoders =
11116 kcalloc(dev->mode_config.num_connector,
11117 sizeof(struct drm_encoder *), GFP_KERNEL);
11118 if (!config->save_connector_encoders)
85f9eb71
DV
11119 return -ENOMEM;
11120
11121 /* Copy data. Note that driver private data is not affected.
11122 * Should anything bad happen only the expected state is
11123 * restored, not the drivers personal bookkeeping.
11124 */
7668851f 11125 count = 0;
70e1e0ec 11126 for_each_crtc(dev, crtc) {
7668851f
VS
11127 config->save_crtc_enabled[count++] = crtc->enabled;
11128 }
11129
85f9eb71
DV
11130 count = 0;
11131 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11132 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11133 }
11134
11135 count = 0;
11136 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11137 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11138 }
11139
11140 return 0;
11141}
11142
11143static void intel_set_config_restore_state(struct drm_device *dev,
11144 struct intel_set_config *config)
11145{
7668851f 11146 struct intel_crtc *crtc;
9a935856
DV
11147 struct intel_encoder *encoder;
11148 struct intel_connector *connector;
85f9eb71
DV
11149 int count;
11150
7668851f 11151 count = 0;
d3fcc808 11152 for_each_intel_crtc(dev, crtc) {
7668851f 11153 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11154
11155 if (crtc->new_enabled)
11156 crtc->new_config = &crtc->config;
11157 else
11158 crtc->new_config = NULL;
7668851f
VS
11159 }
11160
85f9eb71 11161 count = 0;
b2784e15 11162 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11163 encoder->new_crtc =
11164 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11165 }
11166
11167 count = 0;
9a935856
DV
11168 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11169 connector->new_encoder =
11170 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11171 }
11172}
11173
e3de42b6 11174static bool
2e57f47d 11175is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11176{
11177 int i;
11178
2e57f47d
CW
11179 if (set->num_connectors == 0)
11180 return false;
11181
11182 if (WARN_ON(set->connectors == NULL))
11183 return false;
11184
11185 for (i = 0; i < set->num_connectors; i++)
11186 if (set->connectors[i]->encoder &&
11187 set->connectors[i]->encoder->crtc == set->crtc &&
11188 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11189 return true;
11190
11191 return false;
11192}
11193
5e2b584e
DV
11194static void
11195intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11196 struct intel_set_config *config)
11197{
11198
11199 /* We should be able to check here if the fb has the same properties
11200 * and then just flip_or_move it */
2e57f47d
CW
11201 if (is_crtc_connector_off(set)) {
11202 config->mode_changed = true;
f4510a27 11203 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11204 /*
11205 * If we have no fb, we can only flip as long as the crtc is
11206 * active, otherwise we need a full mode set. The crtc may
11207 * be active if we've only disabled the primary plane, or
11208 * in fastboot situations.
11209 */
f4510a27 11210 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11211 struct intel_crtc *intel_crtc =
11212 to_intel_crtc(set->crtc);
11213
3b150f08 11214 if (intel_crtc->active) {
319d9827
JB
11215 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11216 config->fb_changed = true;
11217 } else {
11218 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11219 config->mode_changed = true;
11220 }
5e2b584e
DV
11221 } else if (set->fb == NULL) {
11222 config->mode_changed = true;
72f4901e 11223 } else if (set->fb->pixel_format !=
f4510a27 11224 set->crtc->primary->fb->pixel_format) {
5e2b584e 11225 config->mode_changed = true;
e3de42b6 11226 } else {
5e2b584e 11227 config->fb_changed = true;
e3de42b6 11228 }
5e2b584e
DV
11229 }
11230
835c5873 11231 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11232 config->fb_changed = true;
11233
11234 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11235 DRM_DEBUG_KMS("modes are different, full mode set\n");
11236 drm_mode_debug_printmodeline(&set->crtc->mode);
11237 drm_mode_debug_printmodeline(set->mode);
11238 config->mode_changed = true;
11239 }
a1d95703
CW
11240
11241 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11242 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11243}
11244
2e431051 11245static int
9a935856
DV
11246intel_modeset_stage_output_state(struct drm_device *dev,
11247 struct drm_mode_set *set,
11248 struct intel_set_config *config)
50f56119 11249{
9a935856
DV
11250 struct intel_connector *connector;
11251 struct intel_encoder *encoder;
7668851f 11252 struct intel_crtc *crtc;
f3f08572 11253 int ro;
50f56119 11254
9abdda74 11255 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11256 * of connectors. For paranoia, double-check this. */
11257 WARN_ON(!set->fb && (set->num_connectors != 0));
11258 WARN_ON(set->fb && (set->num_connectors == 0));
11259
9a935856
DV
11260 list_for_each_entry(connector, &dev->mode_config.connector_list,
11261 base.head) {
11262 /* Otherwise traverse passed in connector list and get encoders
11263 * for them. */
50f56119 11264 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11265 if (set->connectors[ro] == &connector->base) {
0e32b39c 11266 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11267 break;
11268 }
11269 }
11270
9a935856
DV
11271 /* If we disable the crtc, disable all its connectors. Also, if
11272 * the connector is on the changing crtc but not on the new
11273 * connector list, disable it. */
11274 if ((!set->fb || ro == set->num_connectors) &&
11275 connector->base.encoder &&
11276 connector->base.encoder->crtc == set->crtc) {
11277 connector->new_encoder = NULL;
11278
11279 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11280 connector->base.base.id,
c23cc417 11281 connector->base.name);
9a935856
DV
11282 }
11283
11284
11285 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11286 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11287 config->mode_changed = true;
50f56119
DV
11288 }
11289 }
9a935856 11290 /* connector->new_encoder is now updated for all connectors. */
50f56119 11291
9a935856 11292 /* Update crtc of enabled connectors. */
9a935856
DV
11293 list_for_each_entry(connector, &dev->mode_config.connector_list,
11294 base.head) {
7668851f
VS
11295 struct drm_crtc *new_crtc;
11296
9a935856 11297 if (!connector->new_encoder)
50f56119
DV
11298 continue;
11299
9a935856 11300 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11301
11302 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11303 if (set->connectors[ro] == &connector->base)
50f56119
DV
11304 new_crtc = set->crtc;
11305 }
11306
11307 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11308 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11309 new_crtc)) {
5e2b584e 11310 return -EINVAL;
50f56119 11311 }
0e32b39c 11312 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11313
11314 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11315 connector->base.base.id,
c23cc417 11316 connector->base.name,
9a935856
DV
11317 new_crtc->base.id);
11318 }
11319
11320 /* Check for any encoders that needs to be disabled. */
b2784e15 11321 for_each_intel_encoder(dev, encoder) {
5a65f358 11322 int num_connectors = 0;
9a935856
DV
11323 list_for_each_entry(connector,
11324 &dev->mode_config.connector_list,
11325 base.head) {
11326 if (connector->new_encoder == encoder) {
11327 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11328 num_connectors++;
9a935856
DV
11329 }
11330 }
5a65f358
PZ
11331
11332 if (num_connectors == 0)
11333 encoder->new_crtc = NULL;
11334 else if (num_connectors > 1)
11335 return -EINVAL;
11336
9a935856
DV
11337 /* Only now check for crtc changes so we don't miss encoders
11338 * that will be disabled. */
11339 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11340 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11341 config->mode_changed = true;
50f56119
DV
11342 }
11343 }
9a935856 11344 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11345 list_for_each_entry(connector, &dev->mode_config.connector_list,
11346 base.head) {
11347 if (connector->new_encoder)
11348 if (connector->new_encoder != connector->encoder)
11349 connector->encoder = connector->new_encoder;
11350 }
d3fcc808 11351 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11352 crtc->new_enabled = false;
11353
b2784e15 11354 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11355 if (encoder->new_crtc == crtc) {
11356 crtc->new_enabled = true;
11357 break;
11358 }
11359 }
11360
11361 if (crtc->new_enabled != crtc->base.enabled) {
11362 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11363 crtc->new_enabled ? "en" : "dis");
11364 config->mode_changed = true;
11365 }
7bd0a8e7
VS
11366
11367 if (crtc->new_enabled)
11368 crtc->new_config = &crtc->config;
11369 else
11370 crtc->new_config = NULL;
7668851f
VS
11371 }
11372
2e431051
DV
11373 return 0;
11374}
11375
7d00a1f5
VS
11376static void disable_crtc_nofb(struct intel_crtc *crtc)
11377{
11378 struct drm_device *dev = crtc->base.dev;
11379 struct intel_encoder *encoder;
11380 struct intel_connector *connector;
11381
11382 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11383 pipe_name(crtc->pipe));
11384
11385 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11386 if (connector->new_encoder &&
11387 connector->new_encoder->new_crtc == crtc)
11388 connector->new_encoder = NULL;
11389 }
11390
b2784e15 11391 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11392 if (encoder->new_crtc == crtc)
11393 encoder->new_crtc = NULL;
11394 }
11395
11396 crtc->new_enabled = false;
7bd0a8e7 11397 crtc->new_config = NULL;
7d00a1f5
VS
11398}
11399
2e431051
DV
11400static int intel_crtc_set_config(struct drm_mode_set *set)
11401{
11402 struct drm_device *dev;
2e431051
DV
11403 struct drm_mode_set save_set;
11404 struct intel_set_config *config;
11405 int ret;
2e431051 11406
8d3e375e
DV
11407 BUG_ON(!set);
11408 BUG_ON(!set->crtc);
11409 BUG_ON(!set->crtc->helper_private);
2e431051 11410
7e53f3a4
DV
11411 /* Enforce sane interface api - has been abused by the fb helper. */
11412 BUG_ON(!set->mode && set->fb);
11413 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11414
2e431051
DV
11415 if (set->fb) {
11416 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11417 set->crtc->base.id, set->fb->base.id,
11418 (int)set->num_connectors, set->x, set->y);
11419 } else {
11420 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11421 }
11422
11423 dev = set->crtc->dev;
11424
11425 ret = -ENOMEM;
11426 config = kzalloc(sizeof(*config), GFP_KERNEL);
11427 if (!config)
11428 goto out_config;
11429
11430 ret = intel_set_config_save_state(dev, config);
11431 if (ret)
11432 goto out_config;
11433
11434 save_set.crtc = set->crtc;
11435 save_set.mode = &set->crtc->mode;
11436 save_set.x = set->crtc->x;
11437 save_set.y = set->crtc->y;
f4510a27 11438 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11439
11440 /* Compute whether we need a full modeset, only an fb base update or no
11441 * change at all. In the future we might also check whether only the
11442 * mode changed, e.g. for LVDS where we only change the panel fitter in
11443 * such cases. */
11444 intel_set_config_compute_mode_changes(set, config);
11445
9a935856 11446 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11447 if (ret)
11448 goto fail;
11449
5e2b584e 11450 if (config->mode_changed) {
c0c36b94
CW
11451 ret = intel_set_mode(set->crtc, set->mode,
11452 set->x, set->y, set->fb);
5e2b584e 11453 } else if (config->fb_changed) {
3b150f08
MR
11454 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11455
4878cae2
VS
11456 intel_crtc_wait_for_pending_flips(set->crtc);
11457
4f660f49 11458 ret = intel_pipe_set_base(set->crtc,
94352cf9 11459 set->x, set->y, set->fb);
3b150f08
MR
11460
11461 /*
11462 * We need to make sure the primary plane is re-enabled if it
11463 * has previously been turned off.
11464 */
11465 if (!intel_crtc->primary_enabled && ret == 0) {
11466 WARN_ON(!intel_crtc->active);
fdd508a6 11467 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11468 }
11469
7ca51a3a
JB
11470 /*
11471 * In the fastboot case this may be our only check of the
11472 * state after boot. It would be better to only do it on
11473 * the first update, but we don't have a nice way of doing that
11474 * (and really, set_config isn't used much for high freq page
11475 * flipping, so increasing its cost here shouldn't be a big
11476 * deal).
11477 */
d330a953 11478 if (i915.fastboot && ret == 0)
7ca51a3a 11479 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11480 }
11481
2d05eae1 11482 if (ret) {
bf67dfeb
DV
11483 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11484 set->crtc->base.id, ret);
50f56119 11485fail:
2d05eae1 11486 intel_set_config_restore_state(dev, config);
50f56119 11487
7d00a1f5
VS
11488 /*
11489 * HACK: if the pipe was on, but we didn't have a framebuffer,
11490 * force the pipe off to avoid oopsing in the modeset code
11491 * due to fb==NULL. This should only happen during boot since
11492 * we don't yet reconstruct the FB from the hardware state.
11493 */
11494 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11495 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11496
2d05eae1
CW
11497 /* Try to restore the config */
11498 if (config->mode_changed &&
11499 intel_set_mode(save_set.crtc, save_set.mode,
11500 save_set.x, save_set.y, save_set.fb))
11501 DRM_ERROR("failed to restore config after modeset failure\n");
11502 }
50f56119 11503
d9e55608
DV
11504out_config:
11505 intel_set_config_free(config);
50f56119
DV
11506 return ret;
11507}
f6e5b160
CW
11508
11509static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11510 .gamma_set = intel_crtc_gamma_set,
50f56119 11511 .set_config = intel_crtc_set_config,
f6e5b160
CW
11512 .destroy = intel_crtc_destroy,
11513 .page_flip = intel_crtc_page_flip,
11514};
11515
5358901f
DV
11516static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11517 struct intel_shared_dpll *pll,
11518 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11519{
5358901f 11520 uint32_t val;
ee7b9f93 11521
f458ebbc 11522 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
11523 return false;
11524
5358901f 11525 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11526 hw_state->dpll = val;
11527 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11528 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11529
11530 return val & DPLL_VCO_ENABLE;
11531}
11532
15bdd4cf
DV
11533static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11534 struct intel_shared_dpll *pll)
11535{
11536 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11537 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11538}
11539
e7b903d2
DV
11540static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11541 struct intel_shared_dpll *pll)
11542{
e7b903d2 11543 /* PCH refclock must be enabled first */
89eff4be 11544 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11545
15bdd4cf
DV
11546 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11547
11548 /* Wait for the clocks to stabilize. */
11549 POSTING_READ(PCH_DPLL(pll->id));
11550 udelay(150);
11551
11552 /* The pixel multiplier can only be updated once the
11553 * DPLL is enabled and the clocks are stable.
11554 *
11555 * So write it again.
11556 */
11557 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11558 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11559 udelay(200);
11560}
11561
11562static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11563 struct intel_shared_dpll *pll)
11564{
11565 struct drm_device *dev = dev_priv->dev;
11566 struct intel_crtc *crtc;
e7b903d2
DV
11567
11568 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11569 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11570 if (intel_crtc_to_shared_dpll(crtc) == pll)
11571 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11572 }
11573
15bdd4cf
DV
11574 I915_WRITE(PCH_DPLL(pll->id), 0);
11575 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11576 udelay(200);
11577}
11578
46edb027
DV
11579static char *ibx_pch_dpll_names[] = {
11580 "PCH DPLL A",
11581 "PCH DPLL B",
11582};
11583
7c74ade1 11584static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11585{
e7b903d2 11586 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11587 int i;
11588
7c74ade1 11589 dev_priv->num_shared_dpll = 2;
ee7b9f93 11590
e72f9fbf 11591 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11592 dev_priv->shared_dplls[i].id = i;
11593 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11594 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11595 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11596 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11597 dev_priv->shared_dplls[i].get_hw_state =
11598 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11599 }
11600}
11601
7c74ade1
DV
11602static void intel_shared_dpll_init(struct drm_device *dev)
11603{
e7b903d2 11604 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11605
9cd86933
DV
11606 if (HAS_DDI(dev))
11607 intel_ddi_pll_init(dev);
11608 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11609 ibx_pch_dpll_init(dev);
11610 else
11611 dev_priv->num_shared_dpll = 0;
11612
11613 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11614}
11615
465c120c
MR
11616static int
11617intel_primary_plane_disable(struct drm_plane *plane)
11618{
11619 struct drm_device *dev = plane->dev;
465c120c
MR
11620 struct intel_crtc *intel_crtc;
11621
11622 if (!plane->fb)
11623 return 0;
11624
11625 BUG_ON(!plane->crtc);
11626
11627 intel_crtc = to_intel_crtc(plane->crtc);
11628
11629 /*
11630 * Even though we checked plane->fb above, it's still possible that
11631 * the primary plane has been implicitly disabled because the crtc
11632 * coordinates given weren't visible, or because we detected
11633 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11634 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11635 * In either case, we need to unpin the FB and let the fb pointer get
11636 * updated, but otherwise we don't need to touch the hardware.
11637 */
11638 if (!intel_crtc->primary_enabled)
11639 goto disable_unpin;
11640
11641 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11642 intel_disable_primary_hw_plane(plane, plane->crtc);
11643
465c120c 11644disable_unpin:
4c34574f 11645 mutex_lock(&dev->struct_mutex);
2ff8fde1 11646 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11647 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11648 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11649 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11650 plane->fb = NULL;
11651
11652 return 0;
11653}
11654
11655static int
3c692a41
GP
11656intel_check_primary_plane(struct drm_plane *plane,
11657 struct intel_plane_state *state)
11658{
11659 struct drm_crtc *crtc = state->crtc;
11660 struct drm_framebuffer *fb = state->fb;
11661 struct drm_rect *dest = &state->dst;
11662 struct drm_rect *src = &state->src;
11663 const struct drm_rect *clip = &state->clip;
ccc759dc 11664 int ret;
3c692a41 11665
ccc759dc 11666 ret = drm_plane_helper_check_update(plane, crtc, fb,
3c692a41
GP
11667 src, dest, clip,
11668 DRM_PLANE_HELPER_NO_SCALING,
11669 DRM_PLANE_HELPER_NO_SCALING,
11670 false, true, &state->visible);
ccc759dc
GP
11671 if (ret)
11672 return ret;
11673
11674 /* no fb bound */
11675 if (state->visible && !fb) {
11676 DRM_ERROR("No FB bound\n");
11677 return -EINVAL;
11678 }
11679
11680 return 0;
3c692a41
GP
11681}
11682
11683static int
11684intel_commit_primary_plane(struct drm_plane *plane,
11685 struct intel_plane_state *state)
465c120c 11686{
3c692a41
GP
11687 struct drm_crtc *crtc = state->crtc;
11688 struct drm_framebuffer *fb = state->fb;
465c120c 11689 struct drm_device *dev = crtc->dev;
48404c1e 11690 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ccc759dc
GP
11692 enum pipe pipe = intel_crtc->pipe;
11693 struct drm_framebuffer *old_fb = plane->fb;
2ff8fde1
MR
11694 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11695 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
ce54d85a 11696 struct intel_plane *intel_plane = to_intel_plane(plane);
3c692a41 11697 struct drm_rect *src = &state->src;
465c120c
MR
11698 int ret;
11699
465c120c
MR
11700 intel_crtc_wait_for_pending_flips(crtc);
11701
ccc759dc
GP
11702 if (intel_crtc_has_pending_flip(crtc)) {
11703 DRM_ERROR("pipe is still busy with an old pageflip\n");
11704 return -EBUSY;
11705 }
11706
11707 if (plane->fb != fb) {
4c34574f 11708 mutex_lock(&dev->struct_mutex);
ccc759dc
GP
11709 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11710 if (ret == 0)
11711 i915_gem_track_fb(old_obj, obj,
11712 INTEL_FRONTBUFFER_PRIMARY(pipe));
11713 mutex_unlock(&dev->struct_mutex);
11714 if (ret != 0) {
11715 DRM_DEBUG_KMS("pin & fence failed\n");
11716 return ret;
11717 }
11718 }
11719
11720 crtc->primary->fb = fb;
11721 crtc->x = src->x1;
11722 crtc->y = src->y1;
11723
11724 intel_plane->crtc_x = state->orig_dst.x1;
11725 intel_plane->crtc_y = state->orig_dst.y1;
11726 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11727 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11728 intel_plane->src_x = state->orig_src.x1;
11729 intel_plane->src_y = state->orig_src.y1;
11730 intel_plane->src_w = drm_rect_width(&state->orig_src);
11731 intel_plane->src_h = drm_rect_height(&state->orig_src);
11732 intel_plane->obj = obj;
4c34574f 11733
ccc759dc 11734 if (intel_crtc->active) {
465c120c 11735 /*
ccc759dc
GP
11736 * FBC does not work on some platforms for rotated
11737 * planes, so disable it when rotation is not 0 and
11738 * update it when rotation is set back to 0.
11739 *
11740 * FIXME: This is redundant with the fbc update done in
11741 * the primary plane enable function except that that
11742 * one is done too late. We eventually need to unify
11743 * this.
465c120c 11744 */
ccc759dc
GP
11745 if (intel_crtc->primary_enabled &&
11746 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11747 dev_priv->fbc.plane == intel_crtc->plane &&
11748 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11749 intel_disable_fbc(dev);
465c120c
MR
11750 }
11751
ccc759dc
GP
11752 if (state->visible) {
11753 bool was_enabled = intel_crtc->primary_enabled;
465c120c 11754
ccc759dc
GP
11755 /* FIXME: kill this fastboot hack */
11756 intel_update_pipe_size(intel_crtc);
465c120c 11757
ccc759dc 11758 intel_crtc->primary_enabled = true;
465c120c 11759
ccc759dc
GP
11760 dev_priv->display.update_primary_plane(crtc, plane->fb,
11761 crtc->x, crtc->y);
4c34574f 11762
48404c1e 11763 /*
ccc759dc
GP
11764 * BDW signals flip done immediately if the plane
11765 * is disabled, even if the plane enable is already
11766 * armed to occur at the next vblank :(
48404c1e 11767 */
ccc759dc
GP
11768 if (IS_BROADWELL(dev) && !was_enabled)
11769 intel_wait_for_vblank(dev, intel_crtc->pipe);
11770 } else {
11771 /*
11772 * If clipping results in a non-visible primary plane,
11773 * we'll disable the primary plane. Note that this is
11774 * a bit different than what happens if userspace
11775 * explicitly disables the plane by passing fb=0
11776 * because plane->fb still gets set and pinned.
11777 */
11778 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 11779 }
465c120c 11780
ccc759dc
GP
11781 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
11782
11783 mutex_lock(&dev->struct_mutex);
11784 intel_update_fbc(dev);
11785 mutex_unlock(&dev->struct_mutex);
ce54d85a 11786 }
465c120c 11787
ccc759dc
GP
11788 if (old_fb && old_fb != fb) {
11789 if (intel_crtc->active)
11790 intel_wait_for_vblank(dev, intel_crtc->pipe);
11791
11792 mutex_lock(&dev->struct_mutex);
11793 intel_unpin_fb_obj(old_obj);
11794 mutex_unlock(&dev->struct_mutex);
11795 }
465c120c
MR
11796
11797 return 0;
11798}
11799
3c692a41
GP
11800static int
11801intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11802 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11803 unsigned int crtc_w, unsigned int crtc_h,
11804 uint32_t src_x, uint32_t src_y,
11805 uint32_t src_w, uint32_t src_h)
11806{
11807 struct intel_plane_state state;
11808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11809 int ret;
11810
11811 state.crtc = crtc;
11812 state.fb = fb;
11813
11814 /* sample coordinates in 16.16 fixed point */
11815 state.src.x1 = src_x;
11816 state.src.x2 = src_x + src_w;
11817 state.src.y1 = src_y;
11818 state.src.y2 = src_y + src_h;
11819
11820 /* integer pixels */
11821 state.dst.x1 = crtc_x;
11822 state.dst.x2 = crtc_x + crtc_w;
11823 state.dst.y1 = crtc_y;
11824 state.dst.y2 = crtc_y + crtc_h;
11825
11826 state.clip.x1 = 0;
11827 state.clip.y1 = 0;
11828 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11829 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11830
11831 state.orig_src = state.src;
11832 state.orig_dst = state.dst;
11833
11834 ret = intel_check_primary_plane(plane, &state);
11835 if (ret)
11836 return ret;
11837
11838 intel_commit_primary_plane(plane, &state);
11839
11840 return 0;
11841}
11842
3d7d6510
MR
11843/* Common destruction function for both primary and cursor planes */
11844static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11845{
11846 struct intel_plane *intel_plane = to_intel_plane(plane);
11847 drm_plane_cleanup(plane);
11848 kfree(intel_plane);
11849}
11850
11851static const struct drm_plane_funcs intel_primary_plane_funcs = {
11852 .update_plane = intel_primary_plane_setplane,
11853 .disable_plane = intel_primary_plane_disable,
3d7d6510 11854 .destroy = intel_plane_destroy,
48404c1e 11855 .set_property = intel_plane_set_property
465c120c
MR
11856};
11857
11858static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11859 int pipe)
11860{
11861 struct intel_plane *primary;
11862 const uint32_t *intel_primary_formats;
11863 int num_formats;
11864
11865 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11866 if (primary == NULL)
11867 return NULL;
11868
11869 primary->can_scale = false;
11870 primary->max_downscale = 1;
11871 primary->pipe = pipe;
11872 primary->plane = pipe;
48404c1e 11873 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
11874 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11875 primary->plane = !pipe;
11876
11877 if (INTEL_INFO(dev)->gen <= 3) {
11878 intel_primary_formats = intel_primary_formats_gen2;
11879 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11880 } else {
11881 intel_primary_formats = intel_primary_formats_gen4;
11882 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11883 }
11884
11885 drm_universal_plane_init(dev, &primary->base, 0,
11886 &intel_primary_plane_funcs,
11887 intel_primary_formats, num_formats,
11888 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
11889
11890 if (INTEL_INFO(dev)->gen >= 4) {
11891 if (!dev->mode_config.rotation_property)
11892 dev->mode_config.rotation_property =
11893 drm_mode_create_rotation_property(dev,
11894 BIT(DRM_ROTATE_0) |
11895 BIT(DRM_ROTATE_180));
11896 if (dev->mode_config.rotation_property)
11897 drm_object_attach_property(&primary->base.base,
11898 dev->mode_config.rotation_property,
11899 primary->rotation);
11900 }
11901
465c120c
MR
11902 return &primary->base;
11903}
11904
3d7d6510
MR
11905static int
11906intel_cursor_plane_disable(struct drm_plane *plane)
11907{
11908 if (!plane->fb)
11909 return 0;
11910
11911 BUG_ON(!plane->crtc);
11912
11913 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11914}
11915
11916static int
852e787c
GP
11917intel_check_cursor_plane(struct drm_plane *plane,
11918 struct intel_plane_state *state)
3d7d6510 11919{
852e787c
GP
11920 struct drm_crtc *crtc = state->crtc;
11921 struct drm_framebuffer *fb = state->fb;
11922 struct drm_rect *dest = &state->dst;
11923 struct drm_rect *src = &state->src;
11924 const struct drm_rect *clip = &state->clip;
3d7d6510 11925
852e787c
GP
11926 return drm_plane_helper_check_update(plane, crtc, fb,
11927 src, dest, clip,
3d7d6510
MR
11928 DRM_PLANE_HELPER_NO_SCALING,
11929 DRM_PLANE_HELPER_NO_SCALING,
852e787c
GP
11930 true, true, &state->visible);
11931}
3d7d6510 11932
852e787c
GP
11933static int
11934intel_commit_cursor_plane(struct drm_plane *plane,
11935 struct intel_plane_state *state)
11936{
11937 struct drm_crtc *crtc = state->crtc;
11938 struct drm_framebuffer *fb = state->fb;
11939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11940 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11941 struct drm_i915_gem_object *obj = intel_fb->obj;
11942 int crtc_w, crtc_h;
11943
11944 crtc->cursor_x = state->orig_dst.x1;
11945 crtc->cursor_y = state->orig_dst.y1;
3d7d6510 11946 if (fb != crtc->cursor->fb) {
852e787c
GP
11947 crtc_w = drm_rect_width(&state->orig_dst);
11948 crtc_h = drm_rect_height(&state->orig_dst);
3d7d6510
MR
11949 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11950 } else {
852e787c 11951 intel_crtc_update_cursor(crtc, state->visible);
4ed91096
DV
11952
11953 intel_frontbuffer_flip(crtc->dev,
11954 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11955
3d7d6510
MR
11956 return 0;
11957 }
11958}
852e787c
GP
11959
11960static int
11961intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11962 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11963 unsigned int crtc_w, unsigned int crtc_h,
11964 uint32_t src_x, uint32_t src_y,
11965 uint32_t src_w, uint32_t src_h)
11966{
11967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11968 struct intel_plane_state state;
11969 int ret;
11970
11971 state.crtc = crtc;
11972 state.fb = fb;
11973
11974 /* sample coordinates in 16.16 fixed point */
11975 state.src.x1 = src_x;
11976 state.src.x2 = src_x + src_w;
11977 state.src.y1 = src_y;
11978 state.src.y2 = src_y + src_h;
11979
11980 /* integer pixels */
11981 state.dst.x1 = crtc_x;
11982 state.dst.x2 = crtc_x + crtc_w;
11983 state.dst.y1 = crtc_y;
11984 state.dst.y2 = crtc_y + crtc_h;
11985
11986 state.clip.x1 = 0;
11987 state.clip.y1 = 0;
11988 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11989 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11990
11991 state.orig_src = state.src;
11992 state.orig_dst = state.dst;
11993
11994 ret = intel_check_cursor_plane(plane, &state);
11995 if (ret)
11996 return ret;
11997
11998 return intel_commit_cursor_plane(plane, &state);
11999}
12000
3d7d6510
MR
12001static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12002 .update_plane = intel_cursor_plane_update,
12003 .disable_plane = intel_cursor_plane_disable,
12004 .destroy = intel_plane_destroy,
12005};
12006
12007static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12008 int pipe)
12009{
12010 struct intel_plane *cursor;
12011
12012 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12013 if (cursor == NULL)
12014 return NULL;
12015
12016 cursor->can_scale = false;
12017 cursor->max_downscale = 1;
12018 cursor->pipe = pipe;
12019 cursor->plane = pipe;
12020
12021 drm_universal_plane_init(dev, &cursor->base, 0,
12022 &intel_cursor_plane_funcs,
12023 intel_cursor_formats,
12024 ARRAY_SIZE(intel_cursor_formats),
12025 DRM_PLANE_TYPE_CURSOR);
12026 return &cursor->base;
12027}
12028
b358d0a6 12029static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12030{
fbee40df 12031 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12032 struct intel_crtc *intel_crtc;
3d7d6510
MR
12033 struct drm_plane *primary = NULL;
12034 struct drm_plane *cursor = NULL;
465c120c 12035 int i, ret;
79e53945 12036
955382f3 12037 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12038 if (intel_crtc == NULL)
12039 return;
12040
465c120c 12041 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12042 if (!primary)
12043 goto fail;
12044
12045 cursor = intel_cursor_plane_create(dev, pipe);
12046 if (!cursor)
12047 goto fail;
12048
465c120c 12049 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12050 cursor, &intel_crtc_funcs);
12051 if (ret)
12052 goto fail;
79e53945
JB
12053
12054 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12055 for (i = 0; i < 256; i++) {
12056 intel_crtc->lut_r[i] = i;
12057 intel_crtc->lut_g[i] = i;
12058 intel_crtc->lut_b[i] = i;
12059 }
12060
1f1c2e24
VS
12061 /*
12062 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12063 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12064 */
80824003
JB
12065 intel_crtc->pipe = pipe;
12066 intel_crtc->plane = pipe;
3a77c4c4 12067 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12068 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12069 intel_crtc->plane = !pipe;
80824003
JB
12070 }
12071
4b0e333e
CW
12072 intel_crtc->cursor_base = ~0;
12073 intel_crtc->cursor_cntl = ~0;
dc41c154 12074 intel_crtc->cursor_size = ~0;
8d7849db 12075
22fd0fab
JB
12076 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12077 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12078 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12079 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12080
79e53945 12081 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12082
12083 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12084 return;
12085
12086fail:
12087 if (primary)
12088 drm_plane_cleanup(primary);
12089 if (cursor)
12090 drm_plane_cleanup(cursor);
12091 kfree(intel_crtc);
79e53945
JB
12092}
12093
752aa88a
JB
12094enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12095{
12096 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12097 struct drm_device *dev = connector->base.dev;
752aa88a 12098
51fd371b 12099 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12100
12101 if (!encoder)
12102 return INVALID_PIPE;
12103
12104 return to_intel_crtc(encoder->crtc)->pipe;
12105}
12106
08d7b3d1 12107int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12108 struct drm_file *file)
08d7b3d1 12109{
08d7b3d1 12110 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12111 struct drm_crtc *drmmode_crtc;
c05422d5 12112 struct intel_crtc *crtc;
08d7b3d1 12113
1cff8f6b
DV
12114 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12115 return -ENODEV;
08d7b3d1 12116
7707e653 12117 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12118
7707e653 12119 if (!drmmode_crtc) {
08d7b3d1 12120 DRM_ERROR("no such CRTC id\n");
3f2c2057 12121 return -ENOENT;
08d7b3d1
CW
12122 }
12123
7707e653 12124 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12125 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12126
c05422d5 12127 return 0;
08d7b3d1
CW
12128}
12129
66a9278e 12130static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12131{
66a9278e
DV
12132 struct drm_device *dev = encoder->base.dev;
12133 struct intel_encoder *source_encoder;
79e53945 12134 int index_mask = 0;
79e53945
JB
12135 int entry = 0;
12136
b2784e15 12137 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12138 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12139 index_mask |= (1 << entry);
12140
79e53945
JB
12141 entry++;
12142 }
4ef69c7a 12143
79e53945
JB
12144 return index_mask;
12145}
12146
4d302442
CW
12147static bool has_edp_a(struct drm_device *dev)
12148{
12149 struct drm_i915_private *dev_priv = dev->dev_private;
12150
12151 if (!IS_MOBILE(dev))
12152 return false;
12153
12154 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12155 return false;
12156
e3589908 12157 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12158 return false;
12159
12160 return true;
12161}
12162
ba0fbca4
DL
12163const char *intel_output_name(int output)
12164{
12165 static const char *names[] = {
12166 [INTEL_OUTPUT_UNUSED] = "Unused",
12167 [INTEL_OUTPUT_ANALOG] = "Analog",
12168 [INTEL_OUTPUT_DVO] = "DVO",
12169 [INTEL_OUTPUT_SDVO] = "SDVO",
12170 [INTEL_OUTPUT_LVDS] = "LVDS",
12171 [INTEL_OUTPUT_TVOUT] = "TV",
12172 [INTEL_OUTPUT_HDMI] = "HDMI",
12173 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12174 [INTEL_OUTPUT_EDP] = "eDP",
12175 [INTEL_OUTPUT_DSI] = "DSI",
12176 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12177 };
12178
12179 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12180 return "Invalid";
12181
12182 return names[output];
12183}
12184
84b4e042
JB
12185static bool intel_crt_present(struct drm_device *dev)
12186{
12187 struct drm_i915_private *dev_priv = dev->dev_private;
12188
884497ed
DL
12189 if (INTEL_INFO(dev)->gen >= 9)
12190 return false;
12191
cf404ce4 12192 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
12193 return false;
12194
12195 if (IS_CHERRYVIEW(dev))
12196 return false;
12197
12198 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12199 return false;
12200
12201 return true;
12202}
12203
79e53945
JB
12204static void intel_setup_outputs(struct drm_device *dev)
12205{
725e30ad 12206 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12207 struct intel_encoder *encoder;
cb0953d7 12208 bool dpd_is_edp = false;
79e53945 12209
c9093354 12210 intel_lvds_init(dev);
79e53945 12211
84b4e042 12212 if (intel_crt_present(dev))
79935fca 12213 intel_crt_init(dev);
cb0953d7 12214
affa9354 12215 if (HAS_DDI(dev)) {
0e72a5b5
ED
12216 int found;
12217
12218 /* Haswell uses DDI functions to detect digital outputs */
12219 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12220 /* DDI A only supports eDP */
12221 if (found)
12222 intel_ddi_init(dev, PORT_A);
12223
12224 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12225 * register */
12226 found = I915_READ(SFUSE_STRAP);
12227
12228 if (found & SFUSE_STRAP_DDIB_DETECTED)
12229 intel_ddi_init(dev, PORT_B);
12230 if (found & SFUSE_STRAP_DDIC_DETECTED)
12231 intel_ddi_init(dev, PORT_C);
12232 if (found & SFUSE_STRAP_DDID_DETECTED)
12233 intel_ddi_init(dev, PORT_D);
12234 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12235 int found;
5d8a7752 12236 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12237
12238 if (has_edp_a(dev))
12239 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12240
dc0fa718 12241 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12242 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12243 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12244 if (!found)
e2debe91 12245 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12246 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12247 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12248 }
12249
dc0fa718 12250 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12251 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12252
dc0fa718 12253 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12254 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12255
5eb08b69 12256 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12257 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12258
270b3042 12259 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12260 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12261 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
12262 /*
12263 * The DP_DETECTED bit is the latched state of the DDC
12264 * SDA pin at boot. However since eDP doesn't require DDC
12265 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12266 * eDP ports may have been muxed to an alternate function.
12267 * Thus we can't rely on the DP_DETECTED bit alone to detect
12268 * eDP ports. Consult the VBT as well as DP_DETECTED to
12269 * detect eDP ports.
12270 */
12271 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
585a94b8
AB
12272 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12273 PORT_B);
e17ac6db
VS
12274 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12275 intel_dp_is_edp(dev, PORT_B))
12276 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 12277
e17ac6db 12278 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
6f6005a5
JB
12279 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12280 PORT_C);
e17ac6db
VS
12281 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12282 intel_dp_is_edp(dev, PORT_C))
12283 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 12284
9418c1f1 12285 if (IS_CHERRYVIEW(dev)) {
e17ac6db 12286 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
12287 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12288 PORT_D);
e17ac6db
VS
12289 /* eDP not supported on port D, so don't check VBT */
12290 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12291 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
12292 }
12293
3cfca973 12294 intel_dsi_init(dev);
103a196f 12295 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12296 bool found = false;
7d57382e 12297
e2debe91 12298 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12299 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12300 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12301 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12302 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12303 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12304 }
27185ae1 12305
e7281eab 12306 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12307 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12308 }
13520b05
KH
12309
12310 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12311
e2debe91 12312 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12313 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12314 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12315 }
27185ae1 12316
e2debe91 12317 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12318
b01f2c3a
JB
12319 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12320 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12321 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12322 }
e7281eab 12323 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12324 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12325 }
27185ae1 12326
b01f2c3a 12327 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12328 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12329 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12330 } else if (IS_GEN2(dev))
79e53945
JB
12331 intel_dvo_init(dev);
12332
103a196f 12333 if (SUPPORTS_TV(dev))
79e53945
JB
12334 intel_tv_init(dev);
12335
7c8f8a70
RV
12336 intel_edp_psr_init(dev);
12337
b2784e15 12338 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12339 encoder->base.possible_crtcs = encoder->crtc_mask;
12340 encoder->base.possible_clones =
66a9278e 12341 intel_encoder_clones(encoder);
79e53945 12342 }
47356eb6 12343
dde86e2d 12344 intel_init_pch_refclk(dev);
270b3042
DV
12345
12346 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12347}
12348
12349static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12350{
60a5ca01 12351 struct drm_device *dev = fb->dev;
79e53945 12352 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12353
ef2d633e 12354 drm_framebuffer_cleanup(fb);
60a5ca01 12355 mutex_lock(&dev->struct_mutex);
ef2d633e 12356 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12357 drm_gem_object_unreference(&intel_fb->obj->base);
12358 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12359 kfree(intel_fb);
12360}
12361
12362static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12363 struct drm_file *file,
79e53945
JB
12364 unsigned int *handle)
12365{
12366 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12367 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12368
05394f39 12369 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12370}
12371
12372static const struct drm_framebuffer_funcs intel_fb_funcs = {
12373 .destroy = intel_user_framebuffer_destroy,
12374 .create_handle = intel_user_framebuffer_create_handle,
12375};
12376
b5ea642a
DV
12377static int intel_framebuffer_init(struct drm_device *dev,
12378 struct intel_framebuffer *intel_fb,
12379 struct drm_mode_fb_cmd2 *mode_cmd,
12380 struct drm_i915_gem_object *obj)
79e53945 12381{
a57ce0b2 12382 int aligned_height;
a35cdaa0 12383 int pitch_limit;
79e53945
JB
12384 int ret;
12385
dd4916c5
DV
12386 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12387
c16ed4be
CW
12388 if (obj->tiling_mode == I915_TILING_Y) {
12389 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12390 return -EINVAL;
c16ed4be 12391 }
57cd6508 12392
c16ed4be
CW
12393 if (mode_cmd->pitches[0] & 63) {
12394 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12395 mode_cmd->pitches[0]);
57cd6508 12396 return -EINVAL;
c16ed4be 12397 }
57cd6508 12398
a35cdaa0
CW
12399 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12400 pitch_limit = 32*1024;
12401 } else if (INTEL_INFO(dev)->gen >= 4) {
12402 if (obj->tiling_mode)
12403 pitch_limit = 16*1024;
12404 else
12405 pitch_limit = 32*1024;
12406 } else if (INTEL_INFO(dev)->gen >= 3) {
12407 if (obj->tiling_mode)
12408 pitch_limit = 8*1024;
12409 else
12410 pitch_limit = 16*1024;
12411 } else
12412 /* XXX DSPC is limited to 4k tiled */
12413 pitch_limit = 8*1024;
12414
12415 if (mode_cmd->pitches[0] > pitch_limit) {
12416 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12417 obj->tiling_mode ? "tiled" : "linear",
12418 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12419 return -EINVAL;
c16ed4be 12420 }
5d7bd705
VS
12421
12422 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12423 mode_cmd->pitches[0] != obj->stride) {
12424 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12425 mode_cmd->pitches[0], obj->stride);
5d7bd705 12426 return -EINVAL;
c16ed4be 12427 }
5d7bd705 12428
57779d06 12429 /* Reject formats not supported by any plane early. */
308e5bcb 12430 switch (mode_cmd->pixel_format) {
57779d06 12431 case DRM_FORMAT_C8:
04b3924d
VS
12432 case DRM_FORMAT_RGB565:
12433 case DRM_FORMAT_XRGB8888:
12434 case DRM_FORMAT_ARGB8888:
57779d06
VS
12435 break;
12436 case DRM_FORMAT_XRGB1555:
12437 case DRM_FORMAT_ARGB1555:
c16ed4be 12438 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12439 DRM_DEBUG("unsupported pixel format: %s\n",
12440 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12441 return -EINVAL;
c16ed4be 12442 }
57779d06
VS
12443 break;
12444 case DRM_FORMAT_XBGR8888:
12445 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12446 case DRM_FORMAT_XRGB2101010:
12447 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12448 case DRM_FORMAT_XBGR2101010:
12449 case DRM_FORMAT_ABGR2101010:
c16ed4be 12450 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12451 DRM_DEBUG("unsupported pixel format: %s\n",
12452 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12453 return -EINVAL;
c16ed4be 12454 }
b5626747 12455 break;
04b3924d
VS
12456 case DRM_FORMAT_YUYV:
12457 case DRM_FORMAT_UYVY:
12458 case DRM_FORMAT_YVYU:
12459 case DRM_FORMAT_VYUY:
c16ed4be 12460 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12461 DRM_DEBUG("unsupported pixel format: %s\n",
12462 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12463 return -EINVAL;
c16ed4be 12464 }
57cd6508
CW
12465 break;
12466 default:
4ee62c76
VS
12467 DRM_DEBUG("unsupported pixel format: %s\n",
12468 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12469 return -EINVAL;
12470 }
12471
90f9a336
VS
12472 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12473 if (mode_cmd->offsets[0] != 0)
12474 return -EINVAL;
12475
a57ce0b2
JB
12476 aligned_height = intel_align_height(dev, mode_cmd->height,
12477 obj->tiling_mode);
53155c0a
DV
12478 /* FIXME drm helper for size checks (especially planar formats)? */
12479 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12480 return -EINVAL;
12481
c7d73f6a
DV
12482 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12483 intel_fb->obj = obj;
80075d49 12484 intel_fb->obj->framebuffer_references++;
c7d73f6a 12485
79e53945
JB
12486 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12487 if (ret) {
12488 DRM_ERROR("framebuffer init failed %d\n", ret);
12489 return ret;
12490 }
12491
79e53945
JB
12492 return 0;
12493}
12494
79e53945
JB
12495static struct drm_framebuffer *
12496intel_user_framebuffer_create(struct drm_device *dev,
12497 struct drm_file *filp,
308e5bcb 12498 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12499{
05394f39 12500 struct drm_i915_gem_object *obj;
79e53945 12501
308e5bcb
JB
12502 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12503 mode_cmd->handles[0]));
c8725226 12504 if (&obj->base == NULL)
cce13ff7 12505 return ERR_PTR(-ENOENT);
79e53945 12506
d2dff872 12507 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12508}
12509
4520f53a 12510#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12511static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12512{
12513}
12514#endif
12515
79e53945 12516static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12517 .fb_create = intel_user_framebuffer_create,
0632fef6 12518 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12519};
12520
e70236a8
JB
12521/* Set up chip specific display functions */
12522static void intel_init_display(struct drm_device *dev)
12523{
12524 struct drm_i915_private *dev_priv = dev->dev_private;
12525
ee9300bb
DV
12526 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12527 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12528 else if (IS_CHERRYVIEW(dev))
12529 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12530 else if (IS_VALLEYVIEW(dev))
12531 dev_priv->display.find_dpll = vlv_find_best_dpll;
12532 else if (IS_PINEVIEW(dev))
12533 dev_priv->display.find_dpll = pnv_find_best_dpll;
12534 else
12535 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12536
affa9354 12537 if (HAS_DDI(dev)) {
0e8ffe1b 12538 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12539 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12540 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12541 dev_priv->display.crtc_enable = haswell_crtc_enable;
12542 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12543 dev_priv->display.off = ironlake_crtc_off;
70d21f0e
DL
12544 if (INTEL_INFO(dev)->gen >= 9)
12545 dev_priv->display.update_primary_plane =
12546 skylake_update_primary_plane;
12547 else
12548 dev_priv->display.update_primary_plane =
12549 ironlake_update_primary_plane;
09b4ddf9 12550 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12551 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12552 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12553 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12554 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12555 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12556 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12557 dev_priv->display.update_primary_plane =
12558 ironlake_update_primary_plane;
89b667f8
JB
12559 } else if (IS_VALLEYVIEW(dev)) {
12560 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12561 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12562 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12563 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12564 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12565 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12566 dev_priv->display.update_primary_plane =
12567 i9xx_update_primary_plane;
f564048e 12568 } else {
0e8ffe1b 12569 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12570 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12571 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12572 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12573 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12574 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12575 dev_priv->display.update_primary_plane =
12576 i9xx_update_primary_plane;
f564048e 12577 }
e70236a8 12578
e70236a8 12579 /* Returns the core display clock speed */
25eb05fc
JB
12580 if (IS_VALLEYVIEW(dev))
12581 dev_priv->display.get_display_clock_speed =
12582 valleyview_get_display_clock_speed;
12583 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12584 dev_priv->display.get_display_clock_speed =
12585 i945_get_display_clock_speed;
12586 else if (IS_I915G(dev))
12587 dev_priv->display.get_display_clock_speed =
12588 i915_get_display_clock_speed;
257a7ffc 12589 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12590 dev_priv->display.get_display_clock_speed =
12591 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12592 else if (IS_PINEVIEW(dev))
12593 dev_priv->display.get_display_clock_speed =
12594 pnv_get_display_clock_speed;
e70236a8
JB
12595 else if (IS_I915GM(dev))
12596 dev_priv->display.get_display_clock_speed =
12597 i915gm_get_display_clock_speed;
12598 else if (IS_I865G(dev))
12599 dev_priv->display.get_display_clock_speed =
12600 i865_get_display_clock_speed;
f0f8a9ce 12601 else if (IS_I85X(dev))
e70236a8
JB
12602 dev_priv->display.get_display_clock_speed =
12603 i855_get_display_clock_speed;
12604 else /* 852, 830 */
12605 dev_priv->display.get_display_clock_speed =
12606 i830_get_display_clock_speed;
12607
3bb11b53 12608 if (IS_G4X(dev)) {
e0dac65e 12609 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12610 } else if (IS_GEN5(dev)) {
12611 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12612 dev_priv->display.write_eld = ironlake_write_eld;
12613 } else if (IS_GEN6(dev)) {
12614 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12615 dev_priv->display.write_eld = ironlake_write_eld;
12616 dev_priv->display.modeset_global_resources =
12617 snb_modeset_global_resources;
12618 } else if (IS_IVYBRIDGE(dev)) {
12619 /* FIXME: detect B0+ stepping and use auto training */
12620 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12621 dev_priv->display.write_eld = ironlake_write_eld;
12622 dev_priv->display.modeset_global_resources =
12623 ivb_modeset_global_resources;
059b2fe9 12624 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53
SJ
12625 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12626 dev_priv->display.write_eld = haswell_write_eld;
12627 dev_priv->display.modeset_global_resources =
12628 haswell_modeset_global_resources;
30a970c6
JB
12629 } else if (IS_VALLEYVIEW(dev)) {
12630 dev_priv->display.modeset_global_resources =
12631 valleyview_modeset_global_resources;
9ca2fe73 12632 dev_priv->display.write_eld = ironlake_write_eld;
02c29259
S
12633 } else if (INTEL_INFO(dev)->gen >= 9) {
12634 dev_priv->display.write_eld = haswell_write_eld;
12635 dev_priv->display.modeset_global_resources =
12636 haswell_modeset_global_resources;
e70236a8 12637 }
8c9f3aaf
JB
12638
12639 /* Default just returns -ENODEV to indicate unsupported */
12640 dev_priv->display.queue_flip = intel_default_queue_flip;
12641
12642 switch (INTEL_INFO(dev)->gen) {
12643 case 2:
12644 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12645 break;
12646
12647 case 3:
12648 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12649 break;
12650
12651 case 4:
12652 case 5:
12653 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12654 break;
12655
12656 case 6:
12657 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12658 break;
7c9017e5 12659 case 7:
4e0bbc31 12660 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12661 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12662 break;
8c9f3aaf 12663 }
7bd688cd
JN
12664
12665 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12666
12667 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12668}
12669
b690e96c
JB
12670/*
12671 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12672 * resume, or other times. This quirk makes sure that's the case for
12673 * affected systems.
12674 */
0206e353 12675static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12676{
12677 struct drm_i915_private *dev_priv = dev->dev_private;
12678
12679 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12680 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12681}
12682
b6b5d049
VS
12683static void quirk_pipeb_force(struct drm_device *dev)
12684{
12685 struct drm_i915_private *dev_priv = dev->dev_private;
12686
12687 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12688 DRM_INFO("applying pipe b force quirk\n");
12689}
12690
435793df
KP
12691/*
12692 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12693 */
12694static void quirk_ssc_force_disable(struct drm_device *dev)
12695{
12696 struct drm_i915_private *dev_priv = dev->dev_private;
12697 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12698 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12699}
12700
4dca20ef 12701/*
5a15ab5b
CE
12702 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12703 * brightness value
4dca20ef
CE
12704 */
12705static void quirk_invert_brightness(struct drm_device *dev)
12706{
12707 struct drm_i915_private *dev_priv = dev->dev_private;
12708 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12709 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12710}
12711
9c72cc6f
SD
12712/* Some VBT's incorrectly indicate no backlight is present */
12713static void quirk_backlight_present(struct drm_device *dev)
12714{
12715 struct drm_i915_private *dev_priv = dev->dev_private;
12716 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12717 DRM_INFO("applying backlight present quirk\n");
12718}
12719
b690e96c
JB
12720struct intel_quirk {
12721 int device;
12722 int subsystem_vendor;
12723 int subsystem_device;
12724 void (*hook)(struct drm_device *dev);
12725};
12726
5f85f176
EE
12727/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12728struct intel_dmi_quirk {
12729 void (*hook)(struct drm_device *dev);
12730 const struct dmi_system_id (*dmi_id_list)[];
12731};
12732
12733static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12734{
12735 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12736 return 1;
12737}
12738
12739static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12740 {
12741 .dmi_id_list = &(const struct dmi_system_id[]) {
12742 {
12743 .callback = intel_dmi_reverse_brightness,
12744 .ident = "NCR Corporation",
12745 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12746 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12747 },
12748 },
12749 { } /* terminating entry */
12750 },
12751 .hook = quirk_invert_brightness,
12752 },
12753};
12754
c43b5634 12755static struct intel_quirk intel_quirks[] = {
b690e96c 12756 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12757 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12758
b690e96c
JB
12759 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12760 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12761
b690e96c
JB
12762 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12763 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12764
5f080c0f
VS
12765 /* 830 needs to leave pipe A & dpll A up */
12766 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12767
b6b5d049
VS
12768 /* 830 needs to leave pipe B & dpll B up */
12769 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12770
435793df
KP
12771 /* Lenovo U160 cannot use SSC on LVDS */
12772 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12773
12774 /* Sony Vaio Y cannot use SSC on LVDS */
12775 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12776
be505f64
AH
12777 /* Acer Aspire 5734Z must invert backlight brightness */
12778 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12779
12780 /* Acer/eMachines G725 */
12781 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12782
12783 /* Acer/eMachines e725 */
12784 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12785
12786 /* Acer/Packard Bell NCL20 */
12787 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12788
12789 /* Acer Aspire 4736Z */
12790 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12791
12792 /* Acer Aspire 5336 */
12793 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12794
12795 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12796 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 12797
dfb3d47b
SD
12798 /* Acer C720 Chromebook (Core i3 4005U) */
12799 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12800
d4967d8c
SD
12801 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12802 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12803
12804 /* HP Chromebook 14 (Celeron 2955U) */
12805 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12806};
12807
12808static void intel_init_quirks(struct drm_device *dev)
12809{
12810 struct pci_dev *d = dev->pdev;
12811 int i;
12812
12813 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12814 struct intel_quirk *q = &intel_quirks[i];
12815
12816 if (d->device == q->device &&
12817 (d->subsystem_vendor == q->subsystem_vendor ||
12818 q->subsystem_vendor == PCI_ANY_ID) &&
12819 (d->subsystem_device == q->subsystem_device ||
12820 q->subsystem_device == PCI_ANY_ID))
12821 q->hook(dev);
12822 }
5f85f176
EE
12823 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12824 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12825 intel_dmi_quirks[i].hook(dev);
12826 }
b690e96c
JB
12827}
12828
9cce37f4
JB
12829/* Disable the VGA plane that we never use */
12830static void i915_disable_vga(struct drm_device *dev)
12831{
12832 struct drm_i915_private *dev_priv = dev->dev_private;
12833 u8 sr1;
766aa1c4 12834 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12835
2b37c616 12836 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12837 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12838 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12839 sr1 = inb(VGA_SR_DATA);
12840 outb(sr1 | 1<<5, VGA_SR_DATA);
12841 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12842 udelay(300);
12843
69769f9a
VS
12844 /*
12845 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12846 * from S3 without preserving (some of?) the other bits.
12847 */
12848 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
9cce37f4
JB
12849 POSTING_READ(vga_reg);
12850}
12851
f817586c
DV
12852void intel_modeset_init_hw(struct drm_device *dev)
12853{
a8f78b58
ED
12854 intel_prepare_ddi(dev);
12855
f8bf63fd
VS
12856 if (IS_VALLEYVIEW(dev))
12857 vlv_update_cdclk(dev);
12858
f817586c
DV
12859 intel_init_clock_gating(dev);
12860
8090c6b9 12861 intel_enable_gt_powersave(dev);
f817586c
DV
12862}
12863
79e53945
JB
12864void intel_modeset_init(struct drm_device *dev)
12865{
652c393a 12866 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12867 int sprite, ret;
8cc87b75 12868 enum pipe pipe;
46f297fb 12869 struct intel_crtc *crtc;
79e53945
JB
12870
12871 drm_mode_config_init(dev);
12872
12873 dev->mode_config.min_width = 0;
12874 dev->mode_config.min_height = 0;
12875
019d96cb
DA
12876 dev->mode_config.preferred_depth = 24;
12877 dev->mode_config.prefer_shadow = 1;
12878
e6ecefaa 12879 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12880
b690e96c
JB
12881 intel_init_quirks(dev);
12882
1fa61106
ED
12883 intel_init_pm(dev);
12884
e3c74757
BW
12885 if (INTEL_INFO(dev)->num_pipes == 0)
12886 return;
12887
e70236a8
JB
12888 intel_init_display(dev);
12889
a6c45cf0
CW
12890 if (IS_GEN2(dev)) {
12891 dev->mode_config.max_width = 2048;
12892 dev->mode_config.max_height = 2048;
12893 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12894 dev->mode_config.max_width = 4096;
12895 dev->mode_config.max_height = 4096;
79e53945 12896 } else {
a6c45cf0
CW
12897 dev->mode_config.max_width = 8192;
12898 dev->mode_config.max_height = 8192;
79e53945 12899 }
068be561 12900
dc41c154
VS
12901 if (IS_845G(dev) || IS_I865G(dev)) {
12902 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12903 dev->mode_config.cursor_height = 1023;
12904 } else if (IS_GEN2(dev)) {
068be561
DL
12905 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12906 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12907 } else {
12908 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12909 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12910 }
12911
5d4545ae 12912 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12913
28c97730 12914 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12915 INTEL_INFO(dev)->num_pipes,
12916 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12917
055e393f 12918 for_each_pipe(dev_priv, pipe) {
8cc87b75 12919 intel_crtc_init(dev, pipe);
1fe47785
DL
12920 for_each_sprite(pipe, sprite) {
12921 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12922 if (ret)
06da8da2 12923 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12924 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12925 }
79e53945
JB
12926 }
12927
f42bb70d
JB
12928 intel_init_dpio(dev);
12929
e72f9fbf 12930 intel_shared_dpll_init(dev);
ee7b9f93 12931
69769f9a
VS
12932 /* save the BIOS value before clobbering it */
12933 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
9cce37f4
JB
12934 /* Just disable it once at startup */
12935 i915_disable_vga(dev);
79e53945 12936 intel_setup_outputs(dev);
11be49eb
CW
12937
12938 /* Just in case the BIOS is doing something questionable. */
12939 intel_disable_fbc(dev);
fa9fa083 12940
6e9f798d 12941 drm_modeset_lock_all(dev);
fa9fa083 12942 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12943 drm_modeset_unlock_all(dev);
46f297fb 12944
d3fcc808 12945 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12946 if (!crtc->active)
12947 continue;
12948
46f297fb 12949 /*
46f297fb
JB
12950 * Note that reserving the BIOS fb up front prevents us
12951 * from stuffing other stolen allocations like the ring
12952 * on top. This prevents some ugliness at boot time, and
12953 * can even allow for smooth boot transitions if the BIOS
12954 * fb is large enough for the active pipe configuration.
12955 */
12956 if (dev_priv->display.get_plane_config) {
12957 dev_priv->display.get_plane_config(crtc,
12958 &crtc->plane_config);
12959 /*
12960 * If the fb is shared between multiple heads, we'll
12961 * just get the first one.
12962 */
484b41dd 12963 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12964 }
46f297fb 12965 }
2c7111db
CW
12966}
12967
7fad798e
DV
12968static void intel_enable_pipe_a(struct drm_device *dev)
12969{
12970 struct intel_connector *connector;
12971 struct drm_connector *crt = NULL;
12972 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 12973 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
12974
12975 /* We can't just switch on the pipe A, we need to set things up with a
12976 * proper mode and output configuration. As a gross hack, enable pipe A
12977 * by enabling the load detect pipe once. */
12978 list_for_each_entry(connector,
12979 &dev->mode_config.connector_list,
12980 base.head) {
12981 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12982 crt = &connector->base;
12983 break;
12984 }
12985 }
12986
12987 if (!crt)
12988 return;
12989
208bf9fd
VS
12990 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12991 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
12992}
12993
fa555837
DV
12994static bool
12995intel_check_plane_mapping(struct intel_crtc *crtc)
12996{
7eb552ae
BW
12997 struct drm_device *dev = crtc->base.dev;
12998 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12999 u32 reg, val;
13000
7eb552ae 13001 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13002 return true;
13003
13004 reg = DSPCNTR(!crtc->plane);
13005 val = I915_READ(reg);
13006
13007 if ((val & DISPLAY_PLANE_ENABLE) &&
13008 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13009 return false;
13010
13011 return true;
13012}
13013
24929352
DV
13014static void intel_sanitize_crtc(struct intel_crtc *crtc)
13015{
13016 struct drm_device *dev = crtc->base.dev;
13017 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13018 u32 reg;
24929352 13019
24929352 13020 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 13021 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
13022 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13023
d3eaf884 13024 /* restore vblank interrupts to correct state */
d297e103
VS
13025 if (crtc->active) {
13026 update_scanline_offset(crtc);
d3eaf884 13027 drm_vblank_on(dev, crtc->pipe);
d297e103 13028 } else
d3eaf884
VS
13029 drm_vblank_off(dev, crtc->pipe);
13030
24929352 13031 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13032 * disable the crtc (and hence change the state) if it is wrong. Note
13033 * that gen4+ has a fixed plane -> pipe mapping. */
13034 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13035 struct intel_connector *connector;
13036 bool plane;
13037
24929352
DV
13038 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13039 crtc->base.base.id);
13040
13041 /* Pipe has the wrong plane attached and the plane is active.
13042 * Temporarily change the plane mapping and disable everything
13043 * ... */
13044 plane = crtc->plane;
13045 crtc->plane = !plane;
9c8958bc 13046 crtc->primary_enabled = true;
24929352
DV
13047 dev_priv->display.crtc_disable(&crtc->base);
13048 crtc->plane = plane;
13049
13050 /* ... and break all links. */
13051 list_for_each_entry(connector, &dev->mode_config.connector_list,
13052 base.head) {
13053 if (connector->encoder->base.crtc != &crtc->base)
13054 continue;
13055
7f1950fb
EE
13056 connector->base.dpms = DRM_MODE_DPMS_OFF;
13057 connector->base.encoder = NULL;
24929352 13058 }
7f1950fb
EE
13059 /* multiple connectors may have the same encoder:
13060 * handle them and break crtc link separately */
13061 list_for_each_entry(connector, &dev->mode_config.connector_list,
13062 base.head)
13063 if (connector->encoder->base.crtc == &crtc->base) {
13064 connector->encoder->base.crtc = NULL;
13065 connector->encoder->connectors_active = false;
13066 }
24929352
DV
13067
13068 WARN_ON(crtc->active);
13069 crtc->base.enabled = false;
13070 }
24929352 13071
7fad798e
DV
13072 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13073 crtc->pipe == PIPE_A && !crtc->active) {
13074 /* BIOS forgot to enable pipe A, this mostly happens after
13075 * resume. Force-enable the pipe to fix this, the update_dpms
13076 * call below we restore the pipe to the right state, but leave
13077 * the required bits on. */
13078 intel_enable_pipe_a(dev);
13079 }
13080
24929352
DV
13081 /* Adjust the state of the output pipe according to whether we
13082 * have active connectors/encoders. */
13083 intel_crtc_update_dpms(&crtc->base);
13084
13085 if (crtc->active != crtc->base.enabled) {
13086 struct intel_encoder *encoder;
13087
13088 /* This can happen either due to bugs in the get_hw_state
13089 * functions or because the pipe is force-enabled due to the
13090 * pipe A quirk. */
13091 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13092 crtc->base.base.id,
13093 crtc->base.enabled ? "enabled" : "disabled",
13094 crtc->active ? "enabled" : "disabled");
13095
13096 crtc->base.enabled = crtc->active;
13097
13098 /* Because we only establish the connector -> encoder ->
13099 * crtc links if something is active, this means the
13100 * crtc is now deactivated. Break the links. connector
13101 * -> encoder links are only establish when things are
13102 * actually up, hence no need to break them. */
13103 WARN_ON(crtc->active);
13104
13105 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13106 WARN_ON(encoder->connectors_active);
13107 encoder->base.crtc = NULL;
13108 }
13109 }
c5ab3bc0 13110
a3ed6aad 13111 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13112 /*
13113 * We start out with underrun reporting disabled to avoid races.
13114 * For correct bookkeeping mark this on active crtcs.
13115 *
c5ab3bc0
DV
13116 * Also on gmch platforms we dont have any hardware bits to
13117 * disable the underrun reporting. Which means we need to start
13118 * out with underrun reporting disabled also on inactive pipes,
13119 * since otherwise we'll complain about the garbage we read when
13120 * e.g. coming up after runtime pm.
13121 *
4cc31489
DV
13122 * No protection against concurrent access is required - at
13123 * worst a fifo underrun happens which also sets this to false.
13124 */
13125 crtc->cpu_fifo_underrun_disabled = true;
13126 crtc->pch_fifo_underrun_disabled = true;
13127 }
24929352
DV
13128}
13129
13130static void intel_sanitize_encoder(struct intel_encoder *encoder)
13131{
13132 struct intel_connector *connector;
13133 struct drm_device *dev = encoder->base.dev;
13134
13135 /* We need to check both for a crtc link (meaning that the
13136 * encoder is active and trying to read from a pipe) and the
13137 * pipe itself being active. */
13138 bool has_active_crtc = encoder->base.crtc &&
13139 to_intel_crtc(encoder->base.crtc)->active;
13140
13141 if (encoder->connectors_active && !has_active_crtc) {
13142 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13143 encoder->base.base.id,
8e329a03 13144 encoder->base.name);
24929352
DV
13145
13146 /* Connector is active, but has no active pipe. This is
13147 * fallout from our resume register restoring. Disable
13148 * the encoder manually again. */
13149 if (encoder->base.crtc) {
13150 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13151 encoder->base.base.id,
8e329a03 13152 encoder->base.name);
24929352 13153 encoder->disable(encoder);
a62d1497
VS
13154 if (encoder->post_disable)
13155 encoder->post_disable(encoder);
24929352 13156 }
7f1950fb
EE
13157 encoder->base.crtc = NULL;
13158 encoder->connectors_active = false;
24929352
DV
13159
13160 /* Inconsistent output/port/pipe state happens presumably due to
13161 * a bug in one of the get_hw_state functions. Or someplace else
13162 * in our code, like the register restore mess on resume. Clamp
13163 * things to off as a safer default. */
13164 list_for_each_entry(connector,
13165 &dev->mode_config.connector_list,
13166 base.head) {
13167 if (connector->encoder != encoder)
13168 continue;
7f1950fb
EE
13169 connector->base.dpms = DRM_MODE_DPMS_OFF;
13170 connector->base.encoder = NULL;
24929352
DV
13171 }
13172 }
13173 /* Enabled encoders without active connectors will be fixed in
13174 * the crtc fixup. */
13175}
13176
04098753 13177void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13178{
13179 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13180 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13181
04098753
ID
13182 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13183 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13184 i915_disable_vga(dev);
13185 }
13186}
13187
13188void i915_redisable_vga(struct drm_device *dev)
13189{
13190 struct drm_i915_private *dev_priv = dev->dev_private;
13191
8dc8a27c
PZ
13192 /* This function can be called both from intel_modeset_setup_hw_state or
13193 * at a very early point in our resume sequence, where the power well
13194 * structures are not yet restored. Since this function is at a very
13195 * paranoid "someone might have enabled VGA while we were not looking"
13196 * level, just check if the power well is enabled instead of trying to
13197 * follow the "don't touch the power well if we don't need it" policy
13198 * the rest of the driver uses. */
f458ebbc 13199 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13200 return;
13201
04098753 13202 i915_redisable_vga_power_on(dev);
0fde901f
KM
13203}
13204
98ec7739
VS
13205static bool primary_get_hw_state(struct intel_crtc *crtc)
13206{
13207 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13208
13209 if (!crtc->active)
13210 return false;
13211
13212 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13213}
13214
30e984df 13215static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13216{
13217 struct drm_i915_private *dev_priv = dev->dev_private;
13218 enum pipe pipe;
24929352
DV
13219 struct intel_crtc *crtc;
13220 struct intel_encoder *encoder;
13221 struct intel_connector *connector;
5358901f 13222 int i;
24929352 13223
d3fcc808 13224 for_each_intel_crtc(dev, crtc) {
88adfff1 13225 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13226
9953599b
DV
13227 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13228
0e8ffe1b
DV
13229 crtc->active = dev_priv->display.get_pipe_config(crtc,
13230 &crtc->config);
24929352
DV
13231
13232 crtc->base.enabled = crtc->active;
98ec7739 13233 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13234
13235 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13236 crtc->base.base.id,
13237 crtc->active ? "enabled" : "disabled");
13238 }
13239
5358901f
DV
13240 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13241 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13242
13243 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13244 pll->active = 0;
d3fcc808 13245 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13246 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13247 pll->active++;
13248 }
13249 pll->refcount = pll->active;
13250
35c95375
DV
13251 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13252 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13253
13254 if (pll->refcount)
13255 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13256 }
13257
b2784e15 13258 for_each_intel_encoder(dev, encoder) {
24929352
DV
13259 pipe = 0;
13260
13261 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13262 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13263 encoder->base.crtc = &crtc->base;
1d37b689 13264 encoder->get_config(encoder, &crtc->config);
24929352
DV
13265 } else {
13266 encoder->base.crtc = NULL;
13267 }
13268
13269 encoder->connectors_active = false;
6f2bcceb 13270 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13271 encoder->base.base.id,
8e329a03 13272 encoder->base.name,
24929352 13273 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13274 pipe_name(pipe));
24929352
DV
13275 }
13276
13277 list_for_each_entry(connector, &dev->mode_config.connector_list,
13278 base.head) {
13279 if (connector->get_hw_state(connector)) {
13280 connector->base.dpms = DRM_MODE_DPMS_ON;
13281 connector->encoder->connectors_active = true;
13282 connector->base.encoder = &connector->encoder->base;
13283 } else {
13284 connector->base.dpms = DRM_MODE_DPMS_OFF;
13285 connector->base.encoder = NULL;
13286 }
13287 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13288 connector->base.base.id,
c23cc417 13289 connector->base.name,
24929352
DV
13290 connector->base.encoder ? "enabled" : "disabled");
13291 }
30e984df
DV
13292}
13293
13294/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13295 * and i915 state tracking structures. */
13296void intel_modeset_setup_hw_state(struct drm_device *dev,
13297 bool force_restore)
13298{
13299 struct drm_i915_private *dev_priv = dev->dev_private;
13300 enum pipe pipe;
30e984df
DV
13301 struct intel_crtc *crtc;
13302 struct intel_encoder *encoder;
35c95375 13303 int i;
30e984df
DV
13304
13305 intel_modeset_readout_hw_state(dev);
24929352 13306
babea61d
JB
13307 /*
13308 * Now that we have the config, copy it to each CRTC struct
13309 * Note that this could go away if we move to using crtc_config
13310 * checking everywhere.
13311 */
d3fcc808 13312 for_each_intel_crtc(dev, crtc) {
d330a953 13313 if (crtc->active && i915.fastboot) {
f6a83288 13314 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13315 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13316 crtc->base.base.id);
13317 drm_mode_debug_printmodeline(&crtc->base.mode);
13318 }
13319 }
13320
24929352 13321 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13322 for_each_intel_encoder(dev, encoder) {
24929352
DV
13323 intel_sanitize_encoder(encoder);
13324 }
13325
055e393f 13326 for_each_pipe(dev_priv, pipe) {
24929352
DV
13327 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13328 intel_sanitize_crtc(crtc);
c0b03411 13329 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13330 }
9a935856 13331
35c95375
DV
13332 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13333 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13334
13335 if (!pll->on || pll->active)
13336 continue;
13337
13338 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13339
13340 pll->disable(dev_priv, pll);
13341 pll->on = false;
13342 }
13343
96f90c54 13344 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13345 ilk_wm_get_hw_state(dev);
13346
45e2b5f6 13347 if (force_restore) {
7d0bc1ea
VS
13348 i915_redisable_vga(dev);
13349
f30da187
DV
13350 /*
13351 * We need to use raw interfaces for restoring state to avoid
13352 * checking (bogus) intermediate states.
13353 */
055e393f 13354 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13355 struct drm_crtc *crtc =
13356 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13357
13358 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13359 crtc->primary->fb);
45e2b5f6
DV
13360 }
13361 } else {
13362 intel_modeset_update_staged_output_state(dev);
13363 }
8af6cf88
DV
13364
13365 intel_modeset_check_state(dev);
2c7111db
CW
13366}
13367
13368void intel_modeset_gem_init(struct drm_device *dev)
13369{
484b41dd 13370 struct drm_crtc *c;
2ff8fde1 13371 struct drm_i915_gem_object *obj;
484b41dd 13372
ae48434c
ID
13373 mutex_lock(&dev->struct_mutex);
13374 intel_init_gt_powersave(dev);
13375 mutex_unlock(&dev->struct_mutex);
13376
1833b134 13377 intel_modeset_init_hw(dev);
02e792fb
DV
13378
13379 intel_setup_overlay(dev);
484b41dd
JB
13380
13381 /*
13382 * Make sure any fbs we allocated at startup are properly
13383 * pinned & fenced. When we do the allocation it's too early
13384 * for this.
13385 */
13386 mutex_lock(&dev->struct_mutex);
70e1e0ec 13387 for_each_crtc(dev, c) {
2ff8fde1
MR
13388 obj = intel_fb_obj(c->primary->fb);
13389 if (obj == NULL)
484b41dd
JB
13390 continue;
13391
2ff8fde1 13392 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13393 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13394 to_intel_crtc(c)->pipe);
66e514c1
DA
13395 drm_framebuffer_unreference(c->primary->fb);
13396 c->primary->fb = NULL;
484b41dd
JB
13397 }
13398 }
13399 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13400}
13401
4932e2c3
ID
13402void intel_connector_unregister(struct intel_connector *intel_connector)
13403{
13404 struct drm_connector *connector = &intel_connector->base;
13405
13406 intel_panel_destroy_backlight(connector);
34ea3d38 13407 drm_connector_unregister(connector);
4932e2c3
ID
13408}
13409
79e53945
JB
13410void intel_modeset_cleanup(struct drm_device *dev)
13411{
652c393a 13412 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13413 struct drm_connector *connector;
652c393a 13414
fd0c0642
DV
13415 /*
13416 * Interrupts and polling as the first thing to avoid creating havoc.
13417 * Too much stuff here (turning of rps, connectors, ...) would
13418 * experience fancy races otherwise.
13419 */
2aeb7d3a 13420 intel_irq_uninstall(dev_priv);
eb21b92b 13421
fd0c0642
DV
13422 /*
13423 * Due to the hpd irq storm handling the hotplug work can re-arm the
13424 * poll handlers. Hence disable polling after hpd handling is shut down.
13425 */
f87ea761 13426 drm_kms_helper_poll_fini(dev);
fd0c0642 13427
652c393a
JB
13428 mutex_lock(&dev->struct_mutex);
13429
723bfd70
JB
13430 intel_unregister_dsm_handler();
13431
973d04f9 13432 intel_disable_fbc(dev);
e70236a8 13433
8090c6b9 13434 intel_disable_gt_powersave(dev);
0cdab21f 13435
930ebb46
DV
13436 ironlake_teardown_rc6(dev);
13437
69341a5e
KH
13438 mutex_unlock(&dev->struct_mutex);
13439
1630fe75
CW
13440 /* flush any delayed tasks or pending work */
13441 flush_scheduled_work();
13442
db31af1d
JN
13443 /* destroy the backlight and sysfs files before encoders/connectors */
13444 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13445 struct intel_connector *intel_connector;
13446
13447 intel_connector = to_intel_connector(connector);
13448 intel_connector->unregister(intel_connector);
db31af1d 13449 }
d9255d57 13450
79e53945 13451 drm_mode_config_cleanup(dev);
4d7bb011
DV
13452
13453 intel_cleanup_overlay(dev);
ae48434c
ID
13454
13455 mutex_lock(&dev->struct_mutex);
13456 intel_cleanup_gt_powersave(dev);
13457 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13458}
13459
f1c79df3
ZW
13460/*
13461 * Return which encoder is currently attached for connector.
13462 */
df0e9248 13463struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13464{
df0e9248
CW
13465 return &intel_attached_encoder(connector)->base;
13466}
f1c79df3 13467
df0e9248
CW
13468void intel_connector_attach_encoder(struct intel_connector *connector,
13469 struct intel_encoder *encoder)
13470{
13471 connector->encoder = encoder;
13472 drm_mode_connector_attach_encoder(&connector->base,
13473 &encoder->base);
79e53945 13474}
28d52043
DA
13475
13476/*
13477 * set vga decode state - true == enable VGA decode
13478 */
13479int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13480{
13481 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13482 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13483 u16 gmch_ctrl;
13484
75fa041d
CW
13485 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13486 DRM_ERROR("failed to read control word\n");
13487 return -EIO;
13488 }
13489
c0cc8a55
CW
13490 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13491 return 0;
13492
28d52043
DA
13493 if (state)
13494 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13495 else
13496 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13497
13498 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13499 DRM_ERROR("failed to write control word\n");
13500 return -EIO;
13501 }
13502
28d52043
DA
13503 return 0;
13504}
c4a1d9e4 13505
c4a1d9e4 13506struct intel_display_error_state {
ff57f1b0
PZ
13507
13508 u32 power_well_driver;
13509
63b66e5b
CW
13510 int num_transcoders;
13511
c4a1d9e4
CW
13512 struct intel_cursor_error_state {
13513 u32 control;
13514 u32 position;
13515 u32 base;
13516 u32 size;
52331309 13517 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13518
13519 struct intel_pipe_error_state {
ddf9c536 13520 bool power_domain_on;
c4a1d9e4 13521 u32 source;
f301b1e1 13522 u32 stat;
52331309 13523 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13524
13525 struct intel_plane_error_state {
13526 u32 control;
13527 u32 stride;
13528 u32 size;
13529 u32 pos;
13530 u32 addr;
13531 u32 surface;
13532 u32 tile_offset;
52331309 13533 } plane[I915_MAX_PIPES];
63b66e5b
CW
13534
13535 struct intel_transcoder_error_state {
ddf9c536 13536 bool power_domain_on;
63b66e5b
CW
13537 enum transcoder cpu_transcoder;
13538
13539 u32 conf;
13540
13541 u32 htotal;
13542 u32 hblank;
13543 u32 hsync;
13544 u32 vtotal;
13545 u32 vblank;
13546 u32 vsync;
13547 } transcoder[4];
c4a1d9e4
CW
13548};
13549
13550struct intel_display_error_state *
13551intel_display_capture_error_state(struct drm_device *dev)
13552{
fbee40df 13553 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13554 struct intel_display_error_state *error;
63b66e5b
CW
13555 int transcoders[] = {
13556 TRANSCODER_A,
13557 TRANSCODER_B,
13558 TRANSCODER_C,
13559 TRANSCODER_EDP,
13560 };
c4a1d9e4
CW
13561 int i;
13562
63b66e5b
CW
13563 if (INTEL_INFO(dev)->num_pipes == 0)
13564 return NULL;
13565
9d1cb914 13566 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13567 if (error == NULL)
13568 return NULL;
13569
190be112 13570 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13571 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13572
055e393f 13573 for_each_pipe(dev_priv, i) {
ddf9c536 13574 error->pipe[i].power_domain_on =
f458ebbc
DV
13575 __intel_display_power_is_enabled(dev_priv,
13576 POWER_DOMAIN_PIPE(i));
ddf9c536 13577 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13578 continue;
13579
5efb3e28
VS
13580 error->cursor[i].control = I915_READ(CURCNTR(i));
13581 error->cursor[i].position = I915_READ(CURPOS(i));
13582 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13583
13584 error->plane[i].control = I915_READ(DSPCNTR(i));
13585 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13586 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13587 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13588 error->plane[i].pos = I915_READ(DSPPOS(i));
13589 }
ca291363
PZ
13590 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13591 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13592 if (INTEL_INFO(dev)->gen >= 4) {
13593 error->plane[i].surface = I915_READ(DSPSURF(i));
13594 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13595 }
13596
c4a1d9e4 13597 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13598
3abfce77 13599 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13600 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13601 }
13602
13603 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13604 if (HAS_DDI(dev_priv->dev))
13605 error->num_transcoders++; /* Account for eDP. */
13606
13607 for (i = 0; i < error->num_transcoders; i++) {
13608 enum transcoder cpu_transcoder = transcoders[i];
13609
ddf9c536 13610 error->transcoder[i].power_domain_on =
f458ebbc 13611 __intel_display_power_is_enabled(dev_priv,
38cc1daf 13612 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13613 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13614 continue;
13615
63b66e5b
CW
13616 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13617
13618 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13619 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13620 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13621 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13622 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13623 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13624 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13625 }
13626
13627 return error;
13628}
13629
edc3d884
MK
13630#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13631
c4a1d9e4 13632void
edc3d884 13633intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13634 struct drm_device *dev,
13635 struct intel_display_error_state *error)
13636{
055e393f 13637 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13638 int i;
13639
63b66e5b
CW
13640 if (!error)
13641 return;
13642
edc3d884 13643 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13644 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13645 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13646 error->power_well_driver);
055e393f 13647 for_each_pipe(dev_priv, i) {
edc3d884 13648 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13649 err_printf(m, " Power: %s\n",
13650 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13651 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13652 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13653
13654 err_printf(m, "Plane [%d]:\n", i);
13655 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13656 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13657 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13658 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13659 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13660 }
4b71a570 13661 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13662 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13663 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13664 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13665 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13666 }
13667
edc3d884
MK
13668 err_printf(m, "Cursor [%d]:\n", i);
13669 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13670 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13671 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13672 }
63b66e5b
CW
13673
13674 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13675 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13676 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13677 err_printf(m, " Power: %s\n",
13678 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13679 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13680 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13681 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13682 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13683 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13684 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13685 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13686 }
c4a1d9e4 13687}
e2fcdaa9
VS
13688
13689void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13690{
13691 struct intel_crtc *crtc;
13692
13693 for_each_intel_crtc(dev, crtc) {
13694 struct intel_unpin_work *work;
e2fcdaa9 13695
5e2d7afc 13696 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
13697
13698 work = crtc->unpin_work;
13699
13700 if (work && work->event &&
13701 work->event->base.file_priv == file) {
13702 kfree(work->event);
13703 work->event = NULL;
13704 }
13705
5e2d7afc 13706 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
13707 }
13708}
This page took 2.518173 seconds and 5 git commands to generate.