drm/i915: Helper function to determine GGTT view from plane state
[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"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c
MR
48/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
3d7d6510
MR
73/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
6b383a7f 78static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 79
f1f644dc 80static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 81 struct intel_crtc_state *pipe_config);
18442d08 82static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 83 struct intel_crtc_state *pipe_config);
f1f644dc 84
e7457a9a
DL
85static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
86 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
87static int intel_framebuffer_init(struct drm_device *dev,
88 struct intel_framebuffer *ifb,
89 struct drm_mode_fb_cmd2 *mode_cmd,
90 struct drm_i915_gem_object *obj);
5b18e57c
DV
91static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
92static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 93static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
94 struct intel_link_m_n *m_n,
95 struct intel_link_m_n *m2_n2);
29407aab 96static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
97static void haswell_set_pipeconf(struct drm_crtc *crtc);
98static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 99static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 100 const struct intel_crtc_state *pipe_config);
d288f65f 101static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 102 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
103static void intel_begin_crtc_commit(struct drm_crtc *crtc);
104static void intel_finish_crtc_commit(struct drm_crtc *crtc);
e7457a9a 105
0e32b39c
DA
106static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
107{
108 if (!connector->mst_port)
109 return connector->encoder;
110 else
111 return &connector->mst_port->mst_encoders[pipe]->base;
112}
113
79e53945 114typedef struct {
0206e353 115 int min, max;
79e53945
JB
116} intel_range_t;
117
118typedef struct {
0206e353
AJ
119 int dot_limit;
120 int p2_slow, p2_fast;
79e53945
JB
121} intel_p2_t;
122
d4906093
ML
123typedef struct intel_limit intel_limit_t;
124struct intel_limit {
0206e353
AJ
125 intel_range_t dot, vco, n, m, m1, m2, p, p1;
126 intel_p2_t p2;
d4906093 127};
79e53945 128
d2acd215
DV
129int
130intel_pch_rawclk(struct drm_device *dev)
131{
132 struct drm_i915_private *dev_priv = dev->dev_private;
133
134 WARN_ON(!HAS_PCH_SPLIT(dev));
135
136 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
137}
138
021357ac
CW
139static inline u32 /* units of 100MHz */
140intel_fdi_link_freq(struct drm_device *dev)
141{
8b99e68c
CW
142 if (IS_GEN5(dev)) {
143 struct drm_i915_private *dev_priv = dev->dev_private;
144 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
145 } else
146 return 27;
021357ac
CW
147}
148
5d536e28 149static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 150 .dot = { .min = 25000, .max = 350000 },
9c333719 151 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 152 .n = { .min = 2, .max = 16 },
0206e353
AJ
153 .m = { .min = 96, .max = 140 },
154 .m1 = { .min = 18, .max = 26 },
155 .m2 = { .min = 6, .max = 16 },
156 .p = { .min = 4, .max = 128 },
157 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
158 .p2 = { .dot_limit = 165000,
159 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
160};
161
5d536e28
DV
162static const intel_limit_t intel_limits_i8xx_dvo = {
163 .dot = { .min = 25000, .max = 350000 },
9c333719 164 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 165 .n = { .min = 2, .max = 16 },
5d536e28
DV
166 .m = { .min = 96, .max = 140 },
167 .m1 = { .min = 18, .max = 26 },
168 .m2 = { .min = 6, .max = 16 },
169 .p = { .min = 4, .max = 128 },
170 .p1 = { .min = 2, .max = 33 },
171 .p2 = { .dot_limit = 165000,
172 .p2_slow = 4, .p2_fast = 4 },
173};
174
e4b36699 175static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 176 .dot = { .min = 25000, .max = 350000 },
9c333719 177 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 178 .n = { .min = 2, .max = 16 },
0206e353
AJ
179 .m = { .min = 96, .max = 140 },
180 .m1 = { .min = 18, .max = 26 },
181 .m2 = { .min = 6, .max = 16 },
182 .p = { .min = 4, .max = 128 },
183 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
184 .p2 = { .dot_limit = 165000,
185 .p2_slow = 14, .p2_fast = 7 },
e4b36699 186};
273e27ca 187
e4b36699 188static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
189 .dot = { .min = 20000, .max = 400000 },
190 .vco = { .min = 1400000, .max = 2800000 },
191 .n = { .min = 1, .max = 6 },
192 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
193 .m1 = { .min = 8, .max = 18 },
194 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
195 .p = { .min = 5, .max = 80 },
196 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
197 .p2 = { .dot_limit = 200000,
198 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
199};
200
201static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
202 .dot = { .min = 20000, .max = 400000 },
203 .vco = { .min = 1400000, .max = 2800000 },
204 .n = { .min = 1, .max = 6 },
205 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
206 .m1 = { .min = 8, .max = 18 },
207 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
208 .p = { .min = 7, .max = 98 },
209 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
210 .p2 = { .dot_limit = 112000,
211 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
212};
213
273e27ca 214
e4b36699 215static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
216 .dot = { .min = 25000, .max = 270000 },
217 .vco = { .min = 1750000, .max = 3500000},
218 .n = { .min = 1, .max = 4 },
219 .m = { .min = 104, .max = 138 },
220 .m1 = { .min = 17, .max = 23 },
221 .m2 = { .min = 5, .max = 11 },
222 .p = { .min = 10, .max = 30 },
223 .p1 = { .min = 1, .max = 3},
224 .p2 = { .dot_limit = 270000,
225 .p2_slow = 10,
226 .p2_fast = 10
044c7c41 227 },
e4b36699
KP
228};
229
230static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
231 .dot = { .min = 22000, .max = 400000 },
232 .vco = { .min = 1750000, .max = 3500000},
233 .n = { .min = 1, .max = 4 },
234 .m = { .min = 104, .max = 138 },
235 .m1 = { .min = 16, .max = 23 },
236 .m2 = { .min = 5, .max = 11 },
237 .p = { .min = 5, .max = 80 },
238 .p1 = { .min = 1, .max = 8},
239 .p2 = { .dot_limit = 165000,
240 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
241};
242
243static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
244 .dot = { .min = 20000, .max = 115000 },
245 .vco = { .min = 1750000, .max = 3500000 },
246 .n = { .min = 1, .max = 3 },
247 .m = { .min = 104, .max = 138 },
248 .m1 = { .min = 17, .max = 23 },
249 .m2 = { .min = 5, .max = 11 },
250 .p = { .min = 28, .max = 112 },
251 .p1 = { .min = 2, .max = 8 },
252 .p2 = { .dot_limit = 0,
253 .p2_slow = 14, .p2_fast = 14
044c7c41 254 },
e4b36699
KP
255};
256
257static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
258 .dot = { .min = 80000, .max = 224000 },
259 .vco = { .min = 1750000, .max = 3500000 },
260 .n = { .min = 1, .max = 3 },
261 .m = { .min = 104, .max = 138 },
262 .m1 = { .min = 17, .max = 23 },
263 .m2 = { .min = 5, .max = 11 },
264 .p = { .min = 14, .max = 42 },
265 .p1 = { .min = 2, .max = 6 },
266 .p2 = { .dot_limit = 0,
267 .p2_slow = 7, .p2_fast = 7
044c7c41 268 },
e4b36699
KP
269};
270
f2b115e6 271static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
272 .dot = { .min = 20000, .max = 400000},
273 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 274 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
275 .n = { .min = 3, .max = 6 },
276 .m = { .min = 2, .max = 256 },
273e27ca 277 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
278 .m1 = { .min = 0, .max = 0 },
279 .m2 = { .min = 0, .max = 254 },
280 .p = { .min = 5, .max = 80 },
281 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
282 .p2 = { .dot_limit = 200000,
283 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
284};
285
f2b115e6 286static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
287 .dot = { .min = 20000, .max = 400000 },
288 .vco = { .min = 1700000, .max = 3500000 },
289 .n = { .min = 3, .max = 6 },
290 .m = { .min = 2, .max = 256 },
291 .m1 = { .min = 0, .max = 0 },
292 .m2 = { .min = 0, .max = 254 },
293 .p = { .min = 7, .max = 112 },
294 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
295 .p2 = { .dot_limit = 112000,
296 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
297};
298
273e27ca
EA
299/* Ironlake / Sandybridge
300 *
301 * We calculate clock using (register_value + 2) for N/M1/M2, so here
302 * the range value for them is (actual_value - 2).
303 */
b91ad0ec 304static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 5 },
308 .m = { .min = 79, .max = 127 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 5, .max = 80 },
312 .p1 = { .min = 1, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
315};
316
b91ad0ec 317static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
318 .dot = { .min = 25000, .max = 350000 },
319 .vco = { .min = 1760000, .max = 3510000 },
320 .n = { .min = 1, .max = 3 },
321 .m = { .min = 79, .max = 118 },
322 .m1 = { .min = 12, .max = 22 },
323 .m2 = { .min = 5, .max = 9 },
324 .p = { .min = 28, .max = 112 },
325 .p1 = { .min = 2, .max = 8 },
326 .p2 = { .dot_limit = 225000,
327 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
328};
329
330static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
331 .dot = { .min = 25000, .max = 350000 },
332 .vco = { .min = 1760000, .max = 3510000 },
333 .n = { .min = 1, .max = 3 },
334 .m = { .min = 79, .max = 127 },
335 .m1 = { .min = 12, .max = 22 },
336 .m2 = { .min = 5, .max = 9 },
337 .p = { .min = 14, .max = 56 },
338 .p1 = { .min = 2, .max = 8 },
339 .p2 = { .dot_limit = 225000,
340 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
341};
342
273e27ca 343/* LVDS 100mhz refclk limits. */
b91ad0ec 344static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
345 .dot = { .min = 25000, .max = 350000 },
346 .vco = { .min = 1760000, .max = 3510000 },
347 .n = { .min = 1, .max = 2 },
348 .m = { .min = 79, .max = 126 },
349 .m1 = { .min = 12, .max = 22 },
350 .m2 = { .min = 5, .max = 9 },
351 .p = { .min = 28, .max = 112 },
0206e353 352 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
353 .p2 = { .dot_limit = 225000,
354 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
355};
356
357static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
358 .dot = { .min = 25000, .max = 350000 },
359 .vco = { .min = 1760000, .max = 3510000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 79, .max = 126 },
362 .m1 = { .min = 12, .max = 22 },
363 .m2 = { .min = 5, .max = 9 },
364 .p = { .min = 14, .max = 42 },
0206e353 365 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
366 .p2 = { .dot_limit = 225000,
367 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
368};
369
dc730512 370static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
371 /*
372 * These are the data rate limits (measured in fast clocks)
373 * since those are the strictest limits we have. The fast
374 * clock and actual rate limits are more relaxed, so checking
375 * them would make no difference.
376 */
377 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 378 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 379 .n = { .min = 1, .max = 7 },
a0c4da24
JB
380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
b99ab663 382 .p1 = { .min = 2, .max = 3 },
5fdc9c49 383 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
384};
385
ef9348c8
CML
386static const intel_limit_t intel_limits_chv = {
387 /*
388 * These are the data rate limits (measured in fast clocks)
389 * since those are the strictest limits we have. The fast
390 * clock and actual rate limits are more relaxed, so checking
391 * them would make no difference.
392 */
393 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 394 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
395 .n = { .min = 1, .max = 1 },
396 .m1 = { .min = 2, .max = 2 },
397 .m2 = { .min = 24 << 22, .max = 175 << 22 },
398 .p1 = { .min = 2, .max = 4 },
399 .p2 = { .p2_slow = 1, .p2_fast = 14 },
400};
401
6b4bf1c4
VS
402static void vlv_clock(int refclk, intel_clock_t *clock)
403{
404 clock->m = clock->m1 * clock->m2;
405 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
406 if (WARN_ON(clock->n == 0 || clock->p == 0))
407 return;
fb03ac01
VS
408 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
409 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
410}
411
e0638cdf
PZ
412/**
413 * Returns whether any output on the specified pipe is of the specified type
414 */
4093561b 415bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 416{
409ee761 417 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
418 struct intel_encoder *encoder;
419
409ee761 420 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
421 if (encoder->type == type)
422 return true;
423
424 return false;
425}
426
d0737e1d
ACO
427/**
428 * Returns whether any output on the specified pipe will have the specified
429 * type after a staged modeset is complete, i.e., the same as
430 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
431 * encoder->crtc.
432 */
433static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
434{
435 struct drm_device *dev = crtc->base.dev;
436 struct intel_encoder *encoder;
437
438 for_each_intel_encoder(dev, encoder)
439 if (encoder->new_crtc == crtc && encoder->type == type)
440 return true;
441
442 return false;
443}
444
409ee761 445static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
1b894b59 446 int refclk)
2c07245f 447{
409ee761 448 struct drm_device *dev = crtc->base.dev;
2c07245f 449 const intel_limit_t *limit;
b91ad0ec 450
d0737e1d 451 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 452 if (intel_is_dual_link_lvds(dev)) {
1b894b59 453 if (refclk == 100000)
b91ad0ec
ZW
454 limit = &intel_limits_ironlake_dual_lvds_100m;
455 else
456 limit = &intel_limits_ironlake_dual_lvds;
457 } else {
1b894b59 458 if (refclk == 100000)
b91ad0ec
ZW
459 limit = &intel_limits_ironlake_single_lvds_100m;
460 else
461 limit = &intel_limits_ironlake_single_lvds;
462 }
c6bb3538 463 } else
b91ad0ec 464 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
465
466 return limit;
467}
468
409ee761 469static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
044c7c41 470{
409ee761 471 struct drm_device *dev = crtc->base.dev;
044c7c41
ML
472 const intel_limit_t *limit;
473
d0737e1d 474 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 475 if (intel_is_dual_link_lvds(dev))
e4b36699 476 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 477 else
e4b36699 478 limit = &intel_limits_g4x_single_channel_lvds;
d0737e1d
ACO
479 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
480 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 481 limit = &intel_limits_g4x_hdmi;
d0737e1d 482 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 483 limit = &intel_limits_g4x_sdvo;
044c7c41 484 } else /* The option is for other outputs */
e4b36699 485 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
486
487 return limit;
488}
489
409ee761 490static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
79e53945 491{
409ee761 492 struct drm_device *dev = crtc->base.dev;
79e53945
JB
493 const intel_limit_t *limit;
494
bad720ff 495 if (HAS_PCH_SPLIT(dev))
1b894b59 496 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 497 else if (IS_G4X(dev)) {
044c7c41 498 limit = intel_g4x_limit(crtc);
f2b115e6 499 } else if (IS_PINEVIEW(dev)) {
d0737e1d 500 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 501 limit = &intel_limits_pineview_lvds;
2177832f 502 else
f2b115e6 503 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
504 } else if (IS_CHERRYVIEW(dev)) {
505 limit = &intel_limits_chv;
a0c4da24 506 } else if (IS_VALLEYVIEW(dev)) {
dc730512 507 limit = &intel_limits_vlv;
a6c45cf0 508 } else if (!IS_GEN2(dev)) {
d0737e1d 509 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
510 limit = &intel_limits_i9xx_lvds;
511 else
512 limit = &intel_limits_i9xx_sdvo;
79e53945 513 } else {
d0737e1d 514 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 515 limit = &intel_limits_i8xx_lvds;
d0737e1d 516 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 517 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
518 else
519 limit = &intel_limits_i8xx_dac;
79e53945
JB
520 }
521 return limit;
522}
523
f2b115e6
AJ
524/* m1 is reserved as 0 in Pineview, n is a ring counter */
525static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 526{
2177832f
SL
527 clock->m = clock->m2 + 2;
528 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
529 if (WARN_ON(clock->n == 0 || clock->p == 0))
530 return;
fb03ac01
VS
531 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
533}
534
7429e9d4
DV
535static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
536{
537 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
538}
539
ac58c3f0 540static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 541{
7429e9d4 542 clock->m = i9xx_dpll_compute_m(clock);
79e53945 543 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
544 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
545 return;
fb03ac01
VS
546 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
547 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
548}
549
ef9348c8
CML
550static void chv_clock(int refclk, intel_clock_t *clock)
551{
552 clock->m = clock->m1 * clock->m2;
553 clock->p = clock->p1 * clock->p2;
554 if (WARN_ON(clock->n == 0 || clock->p == 0))
555 return;
556 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
557 clock->n << 22);
558 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
559}
560
7c04d1d9 561#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
562/**
563 * Returns whether the given set of divisors are valid for a given refclk with
564 * the given connectors.
565 */
566
1b894b59
CW
567static bool intel_PLL_is_valid(struct drm_device *dev,
568 const intel_limit_t *limit,
569 const intel_clock_t *clock)
79e53945 570{
f01b7962
VS
571 if (clock->n < limit->n.min || limit->n.max < clock->n)
572 INTELPllInvalid("n out of range\n");
79e53945 573 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 574 INTELPllInvalid("p1 out of range\n");
79e53945 575 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 576 INTELPllInvalid("m2 out of range\n");
79e53945 577 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 578 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
579
580 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
581 if (clock->m1 <= clock->m2)
582 INTELPllInvalid("m1 <= m2\n");
583
584 if (!IS_VALLEYVIEW(dev)) {
585 if (clock->p < limit->p.min || limit->p.max < clock->p)
586 INTELPllInvalid("p out of range\n");
587 if (clock->m < limit->m.min || limit->m.max < clock->m)
588 INTELPllInvalid("m out of range\n");
589 }
590
79e53945 591 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 592 INTELPllInvalid("vco out of range\n");
79e53945
JB
593 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
594 * connector, etc., rather than just a single range.
595 */
596 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 597 INTELPllInvalid("dot out of range\n");
79e53945
JB
598
599 return true;
600}
601
d4906093 602static bool
a919ff14 603i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
cec2f356
SP
604 int target, int refclk, intel_clock_t *match_clock,
605 intel_clock_t *best_clock)
79e53945 606{
a919ff14 607 struct drm_device *dev = crtc->base.dev;
79e53945 608 intel_clock_t clock;
79e53945
JB
609 int err = target;
610
d0737e1d 611 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 612 /*
a210b028
DV
613 * For LVDS just rely on its current settings for dual-channel.
614 * We haven't figured out how to reliably set up different
615 * single/dual channel state, if we even can.
79e53945 616 */
1974cad0 617 if (intel_is_dual_link_lvds(dev))
79e53945
JB
618 clock.p2 = limit->p2.p2_fast;
619 else
620 clock.p2 = limit->p2.p2_slow;
621 } else {
622 if (target < limit->p2.dot_limit)
623 clock.p2 = limit->p2.p2_slow;
624 else
625 clock.p2 = limit->p2.p2_fast;
626 }
627
0206e353 628 memset(best_clock, 0, sizeof(*best_clock));
79e53945 629
42158660
ZY
630 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
631 clock.m1++) {
632 for (clock.m2 = limit->m2.min;
633 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 634 if (clock.m2 >= clock.m1)
42158660
ZY
635 break;
636 for (clock.n = limit->n.min;
637 clock.n <= limit->n.max; clock.n++) {
638 for (clock.p1 = limit->p1.min;
639 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
640 int this_err;
641
ac58c3f0
DV
642 i9xx_clock(refclk, &clock);
643 if (!intel_PLL_is_valid(dev, limit,
644 &clock))
645 continue;
646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
663static bool
a919ff14 664pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
665 int target, int refclk, intel_clock_t *match_clock,
666 intel_clock_t *best_clock)
79e53945 667{
a919ff14 668 struct drm_device *dev = crtc->base.dev;
79e53945 669 intel_clock_t clock;
79e53945
JB
670 int err = target;
671
d0737e1d 672 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 673 /*
a210b028
DV
674 * For LVDS just rely on its current settings for dual-channel.
675 * We haven't figured out how to reliably set up different
676 * single/dual channel state, if we even can.
79e53945 677 */
1974cad0 678 if (intel_is_dual_link_lvds(dev))
79e53945
JB
679 clock.p2 = limit->p2.p2_fast;
680 else
681 clock.p2 = limit->p2.p2_slow;
682 } else {
683 if (target < limit->p2.dot_limit)
684 clock.p2 = limit->p2.p2_slow;
685 else
686 clock.p2 = limit->p2.p2_fast;
687 }
688
0206e353 689 memset(best_clock, 0, sizeof(*best_clock));
79e53945 690
42158660
ZY
691 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
692 clock.m1++) {
693 for (clock.m2 = limit->m2.min;
694 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
699 int this_err;
700
ac58c3f0 701 pineview_clock(refclk, &clock);
1b894b59
CW
702 if (!intel_PLL_is_valid(dev, limit,
703 &clock))
79e53945 704 continue;
cec2f356
SP
705 if (match_clock &&
706 clock.p != match_clock->p)
707 continue;
79e53945
JB
708
709 this_err = abs(clock.dot - target);
710 if (this_err < err) {
711 *best_clock = clock;
712 err = this_err;
713 }
714 }
715 }
716 }
717 }
718
719 return (err != target);
720}
721
d4906093 722static bool
a919ff14 723g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
724 int target, int refclk, intel_clock_t *match_clock,
725 intel_clock_t *best_clock)
d4906093 726{
a919ff14 727 struct drm_device *dev = crtc->base.dev;
d4906093
ML
728 intel_clock_t clock;
729 int max_n;
730 bool found;
6ba770dc
AJ
731 /* approximately equals target * 0.00585 */
732 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
733 found = false;
734
d0737e1d 735 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 736 if (intel_is_dual_link_lvds(dev))
d4906093
ML
737 clock.p2 = limit->p2.p2_fast;
738 else
739 clock.p2 = limit->p2.p2_slow;
740 } else {
741 if (target < limit->p2.dot_limit)
742 clock.p2 = limit->p2.p2_slow;
743 else
744 clock.p2 = limit->p2.p2_fast;
745 }
746
747 memset(best_clock, 0, sizeof(*best_clock));
748 max_n = limit->n.max;
f77f13e2 749 /* based on hardware requirement, prefer smaller n to precision */
d4906093 750 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 751 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
752 for (clock.m1 = limit->m1.max;
753 clock.m1 >= limit->m1.min; clock.m1--) {
754 for (clock.m2 = limit->m2.max;
755 clock.m2 >= limit->m2.min; clock.m2--) {
756 for (clock.p1 = limit->p1.max;
757 clock.p1 >= limit->p1.min; clock.p1--) {
758 int this_err;
759
ac58c3f0 760 i9xx_clock(refclk, &clock);
1b894b59
CW
761 if (!intel_PLL_is_valid(dev, limit,
762 &clock))
d4906093 763 continue;
1b894b59
CW
764
765 this_err = abs(clock.dot - target);
d4906093
ML
766 if (this_err < err_most) {
767 *best_clock = clock;
768 err_most = this_err;
769 max_n = clock.n;
770 found = true;
771 }
772 }
773 }
774 }
775 }
2c07245f
ZW
776 return found;
777}
778
d5dd62bd
ID
779/*
780 * Check if the calculated PLL configuration is more optimal compared to the
781 * best configuration and error found so far. Return the calculated error.
782 */
783static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
784 const intel_clock_t *calculated_clock,
785 const intel_clock_t *best_clock,
786 unsigned int best_error_ppm,
787 unsigned int *error_ppm)
788{
9ca3ba01
ID
789 /*
790 * For CHV ignore the error and consider only the P value.
791 * Prefer a bigger P value based on HW requirements.
792 */
793 if (IS_CHERRYVIEW(dev)) {
794 *error_ppm = 0;
795
796 return calculated_clock->p > best_clock->p;
797 }
798
24be4e46
ID
799 if (WARN_ON_ONCE(!target_freq))
800 return false;
801
d5dd62bd
ID
802 *error_ppm = div_u64(1000000ULL *
803 abs(target_freq - calculated_clock->dot),
804 target_freq);
805 /*
806 * Prefer a better P value over a better (smaller) error if the error
807 * is small. Ensure this preference for future configurations too by
808 * setting the error to 0.
809 */
810 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
811 *error_ppm = 0;
812
813 return true;
814 }
815
816 return *error_ppm + 10 < best_error_ppm;
817}
818
a0c4da24 819static bool
a919ff14 820vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ee9300bb
DV
821 int target, int refclk, intel_clock_t *match_clock,
822 intel_clock_t *best_clock)
a0c4da24 823{
a919ff14 824 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 825 intel_clock_t clock;
69e4f900 826 unsigned int bestppm = 1000000;
27e639bf
VS
827 /* min update 19.2 MHz */
828 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 829 bool found = false;
a0c4da24 830
6b4bf1c4
VS
831 target *= 5; /* fast clock */
832
833 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
834
835 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 836 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 838 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 839 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 840 clock.p = clock.p1 * clock.p2;
a0c4da24 841 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 842 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 843 unsigned int ppm;
69e4f900 844
6b4bf1c4
VS
845 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
846 refclk * clock.m1);
847
848 vlv_clock(refclk, &clock);
43b0ac53 849
f01b7962
VS
850 if (!intel_PLL_is_valid(dev, limit,
851 &clock))
43b0ac53
VS
852 continue;
853
d5dd62bd
ID
854 if (!vlv_PLL_is_optimal(dev, target,
855 &clock,
856 best_clock,
857 bestppm, &ppm))
858 continue;
6b4bf1c4 859
d5dd62bd
ID
860 *best_clock = clock;
861 bestppm = ppm;
862 found = true;
a0c4da24
JB
863 }
864 }
865 }
866 }
a0c4da24 867
49e497ef 868 return found;
a0c4da24 869}
a4fc5ed6 870
ef9348c8 871static bool
a919ff14 872chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
ef9348c8
CML
873 int target, int refclk, intel_clock_t *match_clock,
874 intel_clock_t *best_clock)
875{
a919ff14 876 struct drm_device *dev = crtc->base.dev;
9ca3ba01 877 unsigned int best_error_ppm;
ef9348c8
CML
878 intel_clock_t clock;
879 uint64_t m2;
880 int found = false;
881
882 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 883 best_error_ppm = 1000000;
ef9348c8
CML
884
885 /*
886 * Based on hardware doc, the n always set to 1, and m1 always
887 * set to 2. If requires to support 200Mhz refclk, we need to
888 * revisit this because n may not 1 anymore.
889 */
890 clock.n = 1, clock.m1 = 2;
891 target *= 5; /* fast clock */
892
893 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
894 for (clock.p2 = limit->p2.p2_fast;
895 clock.p2 >= limit->p2.p2_slow;
896 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 897 unsigned int error_ppm;
ef9348c8
CML
898
899 clock.p = clock.p1 * clock.p2;
900
901 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
902 clock.n) << 22, refclk * clock.m1);
903
904 if (m2 > INT_MAX/clock.m1)
905 continue;
906
907 clock.m2 = m2;
908
909 chv_clock(refclk, &clock);
910
911 if (!intel_PLL_is_valid(dev, limit, &clock))
912 continue;
913
9ca3ba01
ID
914 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
915 best_error_ppm, &error_ppm))
916 continue;
917
918 *best_clock = clock;
919 best_error_ppm = error_ppm;
920 found = true;
ef9348c8
CML
921 }
922 }
923
924 return found;
925}
926
20ddf665
VS
927bool intel_crtc_active(struct drm_crtc *crtc)
928{
929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
930
931 /* Be paranoid as we can arrive here with only partial
932 * state retrieved from the hardware during setup.
933 *
241bfc38 934 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
935 * as Haswell has gained clock readout/fastboot support.
936 *
66e514c1 937 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 938 * properly reconstruct framebuffers.
c3d1f436
MR
939 *
940 * FIXME: The intel_crtc->active here should be switched to
941 * crtc->state->active once we have proper CRTC states wired up
942 * for atomic.
20ddf665 943 */
c3d1f436 944 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 945 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
946}
947
a5c961d1
PZ
948enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
949 enum pipe pipe)
950{
951 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
953
6e3c9717 954 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
955}
956
fbf49ea2
VS
957static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
958{
959 struct drm_i915_private *dev_priv = dev->dev_private;
960 u32 reg = PIPEDSL(pipe);
961 u32 line1, line2;
962 u32 line_mask;
963
964 if (IS_GEN2(dev))
965 line_mask = DSL_LINEMASK_GEN2;
966 else
967 line_mask = DSL_LINEMASK_GEN3;
968
969 line1 = I915_READ(reg) & line_mask;
970 mdelay(5);
971 line2 = I915_READ(reg) & line_mask;
972
973 return line1 == line2;
974}
975
ab7ad7f6
KP
976/*
977 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 978 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
979 *
980 * After disabling a pipe, we can't wait for vblank in the usual way,
981 * spinning on the vblank interrupt status bit, since we won't actually
982 * see an interrupt when the pipe is disabled.
983 *
ab7ad7f6
KP
984 * On Gen4 and above:
985 * wait for the pipe register state bit to turn off
986 *
987 * Otherwise:
988 * wait for the display line value to settle (it usually
989 * ends up stopping at the start of the next frame).
58e10eb9 990 *
9d0498a2 991 */
575f7ab7 992static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 993{
575f7ab7 994 struct drm_device *dev = crtc->base.dev;
9d0498a2 995 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 996 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 997 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
998
999 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1000 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1001
1002 /* Wait for the Pipe State to go off */
58e10eb9
CW
1003 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1004 100))
284637d9 1005 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1006 } else {
ab7ad7f6 1007 /* Wait for the display line to settle */
fbf49ea2 1008 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1009 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1010 }
79e53945
JB
1011}
1012
b0ea7d37
DL
1013/*
1014 * ibx_digital_port_connected - is the specified port connected?
1015 * @dev_priv: i915 private structure
1016 * @port: the port to test
1017 *
1018 * Returns true if @port is connected, false otherwise.
1019 */
1020bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1021 struct intel_digital_port *port)
1022{
1023 u32 bit;
1024
c36346e3 1025 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1026 switch (port->port) {
c36346e3
DL
1027 case PORT_B:
1028 bit = SDE_PORTB_HOTPLUG;
1029 break;
1030 case PORT_C:
1031 bit = SDE_PORTC_HOTPLUG;
1032 break;
1033 case PORT_D:
1034 bit = SDE_PORTD_HOTPLUG;
1035 break;
1036 default:
1037 return true;
1038 }
1039 } else {
eba905b2 1040 switch (port->port) {
c36346e3
DL
1041 case PORT_B:
1042 bit = SDE_PORTB_HOTPLUG_CPT;
1043 break;
1044 case PORT_C:
1045 bit = SDE_PORTC_HOTPLUG_CPT;
1046 break;
1047 case PORT_D:
1048 bit = SDE_PORTD_HOTPLUG_CPT;
1049 break;
1050 default:
1051 return true;
1052 }
b0ea7d37
DL
1053 }
1054
1055 return I915_READ(SDEISR) & bit;
1056}
1057
b24e7179
JB
1058static const char *state_string(bool enabled)
1059{
1060 return enabled ? "on" : "off";
1061}
1062
1063/* Only for pre-ILK configs */
55607e8a
DV
1064void assert_pll(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
b24e7179
JB
1066{
1067 int reg;
1068 u32 val;
1069 bool cur_state;
1070
1071 reg = DPLL(pipe);
1072 val = I915_READ(reg);
1073 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1074 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1075 "PLL state assertion failure (expected %s, current %s)\n",
1076 state_string(state), state_string(cur_state));
1077}
b24e7179 1078
23538ef1
JN
1079/* XXX: the dsi pll is shared between MIPI DSI ports */
1080static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1081{
1082 u32 val;
1083 bool cur_state;
1084
1085 mutex_lock(&dev_priv->dpio_lock);
1086 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1087 mutex_unlock(&dev_priv->dpio_lock);
1088
1089 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1090 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1091 "DSI PLL state assertion failure (expected %s, current %s)\n",
1092 state_string(state), state_string(cur_state));
1093}
1094#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1095#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1096
55607e8a 1097struct intel_shared_dpll *
e2b78267
DV
1098intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1099{
1100 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1101
6e3c9717 1102 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1103 return NULL;
1104
6e3c9717 1105 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1106}
1107
040484af 1108/* For ILK+ */
55607e8a
DV
1109void assert_shared_dpll(struct drm_i915_private *dev_priv,
1110 struct intel_shared_dpll *pll,
1111 bool state)
040484af 1112{
040484af 1113 bool cur_state;
5358901f 1114 struct intel_dpll_hw_state hw_state;
040484af 1115
92b27b08 1116 if (WARN (!pll,
46edb027 1117 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1118 return;
ee7b9f93 1119
5358901f 1120 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1121 I915_STATE_WARN(cur_state != state,
5358901f
DV
1122 "%s assertion failure (expected %s, current %s)\n",
1123 pll->name, state_string(state), state_string(cur_state));
040484af 1124}
040484af
JB
1125
1126static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
1128{
1129 int reg;
1130 u32 val;
1131 bool cur_state;
ad80a810
PZ
1132 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1133 pipe);
040484af 1134
affa9354
PZ
1135 if (HAS_DDI(dev_priv->dev)) {
1136 /* DDI does not have a specific FDI_TX register */
ad80a810 1137 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1138 val = I915_READ(reg);
ad80a810 1139 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1140 } else {
1141 reg = FDI_TX_CTL(pipe);
1142 val = I915_READ(reg);
1143 cur_state = !!(val & FDI_TX_ENABLE);
1144 }
e2c719b7 1145 I915_STATE_WARN(cur_state != state,
040484af
JB
1146 "FDI TX state assertion failure (expected %s, current %s)\n",
1147 state_string(state), state_string(cur_state));
1148}
1149#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1150#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1151
1152static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1153 enum pipe pipe, bool state)
1154{
1155 int reg;
1156 u32 val;
1157 bool cur_state;
1158
d63fa0dc
PZ
1159 reg = FDI_RX_CTL(pipe);
1160 val = I915_READ(reg);
1161 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1162 I915_STATE_WARN(cur_state != state,
040484af
JB
1163 "FDI RX state assertion failure (expected %s, current %s)\n",
1164 state_string(state), state_string(cur_state));
1165}
1166#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1167#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1168
1169static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1170 enum pipe pipe)
1171{
1172 int reg;
1173 u32 val;
1174
1175 /* ILK FDI PLL is always enabled */
3d13ef2e 1176 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1177 return;
1178
bf507ef7 1179 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1180 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1181 return;
1182
040484af
JB
1183 reg = FDI_TX_CTL(pipe);
1184 val = I915_READ(reg);
e2c719b7 1185 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1186}
1187
55607e8a
DV
1188void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
040484af
JB
1190{
1191 int reg;
1192 u32 val;
55607e8a 1193 bool cur_state;
040484af
JB
1194
1195 reg = FDI_RX_CTL(pipe);
1196 val = I915_READ(reg);
55607e8a 1197 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1198 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1199 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1200 state_string(state), state_string(cur_state));
040484af
JB
1201}
1202
b680c37a
DV
1203void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1204 enum pipe pipe)
ea0760cf 1205{
bedd4dba
JN
1206 struct drm_device *dev = dev_priv->dev;
1207 int pp_reg;
ea0760cf
JB
1208 u32 val;
1209 enum pipe panel_pipe = PIPE_A;
0de3b485 1210 bool locked = true;
ea0760cf 1211
bedd4dba
JN
1212 if (WARN_ON(HAS_DDI(dev)))
1213 return;
1214
1215 if (HAS_PCH_SPLIT(dev)) {
1216 u32 port_sel;
1217
ea0760cf 1218 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1219 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1220
1221 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1222 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1223 panel_pipe = PIPE_B;
1224 /* XXX: else fix for eDP */
1225 } else if (IS_VALLEYVIEW(dev)) {
1226 /* presumably write lock depends on pipe, not port select */
1227 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1228 panel_pipe = pipe;
ea0760cf
JB
1229 } else {
1230 pp_reg = PP_CONTROL;
bedd4dba
JN
1231 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1232 panel_pipe = PIPE_B;
ea0760cf
JB
1233 }
1234
1235 val = I915_READ(pp_reg);
1236 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1237 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1238 locked = false;
1239
e2c719b7 1240 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1241 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1242 pipe_name(pipe));
ea0760cf
JB
1243}
1244
93ce0ba6
JN
1245static void assert_cursor(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, bool state)
1247{
1248 struct drm_device *dev = dev_priv->dev;
1249 bool cur_state;
1250
d9d82081 1251 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1252 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1253 else
5efb3e28 1254 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1255
e2c719b7 1256 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1257 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1258 pipe_name(pipe), state_string(state), state_string(cur_state));
1259}
1260#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1261#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1262
b840d907
JB
1263void assert_pipe(struct drm_i915_private *dev_priv,
1264 enum pipe pipe, bool state)
b24e7179
JB
1265{
1266 int reg;
1267 u32 val;
63d7bbe9 1268 bool cur_state;
702e7a56
PZ
1269 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1270 pipe);
b24e7179 1271
b6b5d049
VS
1272 /* if we need the pipe quirk it must be always on */
1273 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1274 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1275 state = true;
1276
f458ebbc 1277 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1278 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1279 cur_state = false;
1280 } else {
1281 reg = PIPECONF(cpu_transcoder);
1282 val = I915_READ(reg);
1283 cur_state = !!(val & PIPECONF_ENABLE);
1284 }
1285
e2c719b7 1286 I915_STATE_WARN(cur_state != state,
63d7bbe9 1287 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1288 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1289}
1290
931872fc
CW
1291static void assert_plane(struct drm_i915_private *dev_priv,
1292 enum plane plane, bool state)
b24e7179
JB
1293{
1294 int reg;
1295 u32 val;
931872fc 1296 bool cur_state;
b24e7179
JB
1297
1298 reg = DSPCNTR(plane);
1299 val = I915_READ(reg);
931872fc 1300 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1301 I915_STATE_WARN(cur_state != state,
931872fc
CW
1302 "plane %c assertion failure (expected %s, current %s)\n",
1303 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1304}
1305
931872fc
CW
1306#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1307#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1308
b24e7179
JB
1309static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1310 enum pipe pipe)
1311{
653e1026 1312 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1313 int reg, i;
1314 u32 val;
1315 int cur_pipe;
1316
653e1026
VS
1317 /* Primary planes are fixed to pipes on gen4+ */
1318 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1319 reg = DSPCNTR(pipe);
1320 val = I915_READ(reg);
e2c719b7 1321 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1322 "plane %c assertion failure, should be disabled but not\n",
1323 plane_name(pipe));
19ec1358 1324 return;
28c05794 1325 }
19ec1358 1326
b24e7179 1327 /* Need to check both planes against the pipe */
055e393f 1328 for_each_pipe(dev_priv, i) {
b24e7179
JB
1329 reg = DSPCNTR(i);
1330 val = I915_READ(reg);
1331 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1332 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1333 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1334 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1335 plane_name(i), pipe_name(pipe));
b24e7179
JB
1336 }
1337}
1338
19332d7a
JB
1339static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
20674eef 1342 struct drm_device *dev = dev_priv->dev;
1fe47785 1343 int reg, sprite;
19332d7a
JB
1344 u32 val;
1345
7feb8b88 1346 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1347 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1348 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1349 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1350 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1351 sprite, pipe_name(pipe));
1352 }
1353 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1354 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1355 reg = SPCNTR(pipe, sprite);
20674eef 1356 val = I915_READ(reg);
e2c719b7 1357 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1358 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1359 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1360 }
1361 } else if (INTEL_INFO(dev)->gen >= 7) {
1362 reg = SPRCTL(pipe);
19332d7a 1363 val = I915_READ(reg);
e2c719b7 1364 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1365 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1366 plane_name(pipe), pipe_name(pipe));
1367 } else if (INTEL_INFO(dev)->gen >= 5) {
1368 reg = DVSCNTR(pipe);
19332d7a 1369 val = I915_READ(reg);
e2c719b7 1370 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1371 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1372 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1373 }
1374}
1375
08c71e5e
VS
1376static void assert_vblank_disabled(struct drm_crtc *crtc)
1377{
e2c719b7 1378 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1379 drm_crtc_vblank_put(crtc);
1380}
1381
89eff4be 1382static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1383{
1384 u32 val;
1385 bool enabled;
1386
e2c719b7 1387 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1388
92f2584a
JB
1389 val = I915_READ(PCH_DREF_CONTROL);
1390 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1391 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1392 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1393}
1394
ab9412ba
DV
1395static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe)
92f2584a
JB
1397{
1398 int reg;
1399 u32 val;
1400 bool enabled;
1401
ab9412ba 1402 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1403 val = I915_READ(reg);
1404 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1405 I915_STATE_WARN(enabled,
9db4a9c7
JB
1406 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1407 pipe_name(pipe));
92f2584a
JB
1408}
1409
4e634389
KP
1410static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1412{
1413 if ((val & DP_PORT_EN) == 0)
1414 return false;
1415
1416 if (HAS_PCH_CPT(dev_priv->dev)) {
1417 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1418 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1419 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1420 return false;
44f37d1f
CML
1421 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1422 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1423 return false;
f0575e92
KP
1424 } else {
1425 if ((val & DP_PIPE_MASK) != (pipe << 30))
1426 return false;
1427 }
1428 return true;
1429}
1430
1519b995
KP
1431static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1432 enum pipe pipe, u32 val)
1433{
dc0fa718 1434 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1435 return false;
1436
1437 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1438 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1439 return false;
44f37d1f
CML
1440 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1441 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1442 return false;
1519b995 1443 } else {
dc0fa718 1444 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1445 return false;
1446 }
1447 return true;
1448}
1449
1450static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, u32 val)
1452{
1453 if ((val & LVDS_PORT_EN) == 0)
1454 return false;
1455
1456 if (HAS_PCH_CPT(dev_priv->dev)) {
1457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
1466static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 val)
1468{
1469 if ((val & ADPA_DAC_ENABLE) == 0)
1470 return false;
1471 if (HAS_PCH_CPT(dev_priv->dev)) {
1472 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1473 return false;
1474 } else {
1475 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1476 return false;
1477 }
1478 return true;
1479}
1480
291906f1 1481static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1482 enum pipe pipe, int reg, u32 port_sel)
291906f1 1483{
47a05eca 1484 u32 val = I915_READ(reg);
e2c719b7 1485 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1486 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1487 reg, pipe_name(pipe));
de9a35ab 1488
e2c719b7 1489 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1490 && (val & DP_PIPEB_SELECT),
de9a35ab 1491 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1492}
1493
1494static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1495 enum pipe pipe, int reg)
1496{
47a05eca 1497 u32 val = I915_READ(reg);
e2c719b7 1498 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1499 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1500 reg, pipe_name(pipe));
de9a35ab 1501
e2c719b7 1502 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1503 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1504 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1505}
1506
1507static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1508 enum pipe pipe)
1509{
1510 int reg;
1511 u32 val;
291906f1 1512
f0575e92
KP
1513 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1516
1517 reg = PCH_ADPA;
1518 val = I915_READ(reg);
e2c719b7 1519 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1520 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1521 pipe_name(pipe));
291906f1
JB
1522
1523 reg = PCH_LVDS;
1524 val = I915_READ(reg);
e2c719b7 1525 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1526 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1527 pipe_name(pipe));
291906f1 1528
e2debe91
PZ
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1530 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1531 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1532}
1533
40e9cf64
JB
1534static void intel_init_dpio(struct drm_device *dev)
1535{
1536 struct drm_i915_private *dev_priv = dev->dev_private;
1537
1538 if (!IS_VALLEYVIEW(dev))
1539 return;
1540
a09caddd
CML
1541 /*
1542 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1543 * CHV x1 PHY (DP/HDMI D)
1544 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1545 */
1546 if (IS_CHERRYVIEW(dev)) {
1547 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1548 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1549 } else {
1550 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1551 }
5382f5f3
JB
1552}
1553
d288f65f 1554static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1555 const struct intel_crtc_state *pipe_config)
87442f73 1556{
426115cf
DV
1557 struct drm_device *dev = crtc->base.dev;
1558 struct drm_i915_private *dev_priv = dev->dev_private;
1559 int reg = DPLL(crtc->pipe);
d288f65f 1560 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1561
426115cf 1562 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1563
1564 /* No really, not for ILK+ */
1565 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1566
1567 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1568 if (IS_MOBILE(dev_priv->dev))
426115cf 1569 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1570
426115cf
DV
1571 I915_WRITE(reg, dpll);
1572 POSTING_READ(reg);
1573 udelay(150);
1574
1575 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1576 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1577
d288f65f 1578 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1579 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1580
1581 /* We do this three times for luck */
426115cf 1582 I915_WRITE(reg, dpll);
87442f73
DV
1583 POSTING_READ(reg);
1584 udelay(150); /* wait for warmup */
426115cf 1585 I915_WRITE(reg, dpll);
87442f73
DV
1586 POSTING_READ(reg);
1587 udelay(150); /* wait for warmup */
426115cf 1588 I915_WRITE(reg, dpll);
87442f73
DV
1589 POSTING_READ(reg);
1590 udelay(150); /* wait for warmup */
1591}
1592
d288f65f 1593static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1594 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1595{
1596 struct drm_device *dev = crtc->base.dev;
1597 struct drm_i915_private *dev_priv = dev->dev_private;
1598 int pipe = crtc->pipe;
1599 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1600 u32 tmp;
1601
1602 assert_pipe_disabled(dev_priv, crtc->pipe);
1603
1604 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1605
1606 mutex_lock(&dev_priv->dpio_lock);
1607
1608 /* Enable back the 10bit clock to display controller */
1609 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1610 tmp |= DPIO_DCLKP_EN;
1611 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1612
1613 /*
1614 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1615 */
1616 udelay(1);
1617
1618 /* Enable PLL */
d288f65f 1619 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1620
1621 /* Check PLL is locked */
a11b0703 1622 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1623 DRM_ERROR("PLL %d failed to lock\n", pipe);
1624
a11b0703 1625 /* not sure when this should be written */
d288f65f 1626 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1627 POSTING_READ(DPLL_MD(pipe));
1628
9d556c99
CML
1629 mutex_unlock(&dev_priv->dpio_lock);
1630}
1631
1c4e0274
VS
1632static int intel_num_dvo_pipes(struct drm_device *dev)
1633{
1634 struct intel_crtc *crtc;
1635 int count = 0;
1636
1637 for_each_intel_crtc(dev, crtc)
1638 count += crtc->active &&
409ee761 1639 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1640
1641 return count;
1642}
1643
66e3d5c0 1644static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1645{
66e3d5c0
DV
1646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648 int reg = DPLL(crtc->pipe);
6e3c9717 1649 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1650
66e3d5c0 1651 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1652
63d7bbe9 1653 /* No really, not for ILK+ */
3d13ef2e 1654 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1655
1656 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1657 if (IS_MOBILE(dev) && !IS_I830(dev))
1658 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1659
1c4e0274
VS
1660 /* Enable DVO 2x clock on both PLLs if necessary */
1661 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1662 /*
1663 * It appears to be important that we don't enable this
1664 * for the current pipe before otherwise configuring the
1665 * PLL. No idea how this should be handled if multiple
1666 * DVO outputs are enabled simultaneosly.
1667 */
1668 dpll |= DPLL_DVO_2X_MODE;
1669 I915_WRITE(DPLL(!crtc->pipe),
1670 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1671 }
66e3d5c0
DV
1672
1673 /* Wait for the clocks to stabilize. */
1674 POSTING_READ(reg);
1675 udelay(150);
1676
1677 if (INTEL_INFO(dev)->gen >= 4) {
1678 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1679 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1680 } else {
1681 /* The pixel multiplier can only be updated once the
1682 * DPLL is enabled and the clocks are stable.
1683 *
1684 * So write it again.
1685 */
1686 I915_WRITE(reg, dpll);
1687 }
63d7bbe9
JB
1688
1689 /* We do this three times for luck */
66e3d5c0 1690 I915_WRITE(reg, dpll);
63d7bbe9
JB
1691 POSTING_READ(reg);
1692 udelay(150); /* wait for warmup */
66e3d5c0 1693 I915_WRITE(reg, dpll);
63d7bbe9
JB
1694 POSTING_READ(reg);
1695 udelay(150); /* wait for warmup */
66e3d5c0 1696 I915_WRITE(reg, dpll);
63d7bbe9
JB
1697 POSTING_READ(reg);
1698 udelay(150); /* wait for warmup */
1699}
1700
1701/**
50b44a44 1702 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1703 * @dev_priv: i915 private structure
1704 * @pipe: pipe PLL to disable
1705 *
1706 * Disable the PLL for @pipe, making sure the pipe is off first.
1707 *
1708 * Note! This is for pre-ILK only.
1709 */
1c4e0274 1710static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1711{
1c4e0274
VS
1712 struct drm_device *dev = crtc->base.dev;
1713 struct drm_i915_private *dev_priv = dev->dev_private;
1714 enum pipe pipe = crtc->pipe;
1715
1716 /* Disable DVO 2x clock on both PLLs if necessary */
1717 if (IS_I830(dev) &&
409ee761 1718 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1719 intel_num_dvo_pipes(dev) == 1) {
1720 I915_WRITE(DPLL(PIPE_B),
1721 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1722 I915_WRITE(DPLL(PIPE_A),
1723 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1724 }
1725
b6b5d049
VS
1726 /* Don't disable pipe or pipe PLLs if needed */
1727 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1728 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1729 return;
1730
1731 /* Make sure the pipe isn't still relying on us */
1732 assert_pipe_disabled(dev_priv, pipe);
1733
50b44a44
DV
1734 I915_WRITE(DPLL(pipe), 0);
1735 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1736}
1737
f6071166
JB
1738static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1739{
1740 u32 val = 0;
1741
1742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
1744
e5cbfbfb
ID
1745 /*
1746 * Leave integrated clock source and reference clock enabled for pipe B.
1747 * The latter is needed for VGA hotplug / manual detection.
1748 */
f6071166 1749 if (pipe == PIPE_B)
e5cbfbfb 1750 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1751 I915_WRITE(DPLL(pipe), val);
1752 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1753
1754}
1755
1756static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
d752048d 1758 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1759 u32 val;
1760
a11b0703
VS
1761 /* Make sure the pipe isn't still relying on us */
1762 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1763
a11b0703 1764 /* Set PLL en = 0 */
d17ec4ce 1765 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1766 if (pipe != PIPE_A)
1767 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
d752048d
VS
1770
1771 mutex_lock(&dev_priv->dpio_lock);
1772
1773 /* Disable 10bit clock to display controller */
1774 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1775 val &= ~DPIO_DCLKP_EN;
1776 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1777
61407f6d
VS
1778 /* disable left/right clock distribution */
1779 if (pipe != PIPE_B) {
1780 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1781 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1782 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1783 } else {
1784 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1785 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1786 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1787 }
1788
d752048d 1789 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1790}
1791
e4607fcf
CML
1792void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1793 struct intel_digital_port *dport)
89b667f8
JB
1794{
1795 u32 port_mask;
00fc31b7 1796 int dpll_reg;
89b667f8 1797
e4607fcf
CML
1798 switch (dport->port) {
1799 case PORT_B:
89b667f8 1800 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1801 dpll_reg = DPLL(0);
e4607fcf
CML
1802 break;
1803 case PORT_C:
89b667f8 1804 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1805 dpll_reg = DPLL(0);
1806 break;
1807 case PORT_D:
1808 port_mask = DPLL_PORTD_READY_MASK;
1809 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1810 break;
1811 default:
1812 BUG();
1813 }
89b667f8 1814
00fc31b7 1815 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1816 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1817 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1818}
1819
b14b1055
DV
1820static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1821{
1822 struct drm_device *dev = crtc->base.dev;
1823 struct drm_i915_private *dev_priv = dev->dev_private;
1824 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1825
be19f0ff
CW
1826 if (WARN_ON(pll == NULL))
1827 return;
1828
3e369b76 1829 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1830 if (pll->active == 0) {
1831 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1832 WARN_ON(pll->on);
1833 assert_shared_dpll_disabled(dev_priv, pll);
1834
1835 pll->mode_set(dev_priv, pll);
1836 }
1837}
1838
92f2584a 1839/**
85b3894f 1840 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1841 * @dev_priv: i915 private structure
1842 * @pipe: pipe PLL to enable
1843 *
1844 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1845 * drives the transcoder clock.
1846 */
85b3894f 1847static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1848{
3d13ef2e
DL
1849 struct drm_device *dev = crtc->base.dev;
1850 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1851 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1852
87a875bb 1853 if (WARN_ON(pll == NULL))
48da64a8
CW
1854 return;
1855
3e369b76 1856 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1857 return;
ee7b9f93 1858
74dd6928 1859 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1860 pll->name, pll->active, pll->on,
e2b78267 1861 crtc->base.base.id);
92f2584a 1862
cdbd2316
DV
1863 if (pll->active++) {
1864 WARN_ON(!pll->on);
e9d6944e 1865 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1866 return;
1867 }
f4a091c7 1868 WARN_ON(pll->on);
ee7b9f93 1869
bd2bb1b9
PZ
1870 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1871
46edb027 1872 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1873 pll->enable(dev_priv, pll);
ee7b9f93 1874 pll->on = true;
92f2584a
JB
1875}
1876
f6daaec2 1877static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1878{
3d13ef2e
DL
1879 struct drm_device *dev = crtc->base.dev;
1880 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1881 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1882
92f2584a 1883 /* PCH only available on ILK+ */
3d13ef2e 1884 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1885 if (WARN_ON(pll == NULL))
ee7b9f93 1886 return;
92f2584a 1887
3e369b76 1888 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1889 return;
7a419866 1890
46edb027
DV
1891 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1892 pll->name, pll->active, pll->on,
e2b78267 1893 crtc->base.base.id);
7a419866 1894
48da64a8 1895 if (WARN_ON(pll->active == 0)) {
e9d6944e 1896 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1897 return;
1898 }
1899
e9d6944e 1900 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1901 WARN_ON(!pll->on);
cdbd2316 1902 if (--pll->active)
7a419866 1903 return;
ee7b9f93 1904
46edb027 1905 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1906 pll->disable(dev_priv, pll);
ee7b9f93 1907 pll->on = false;
bd2bb1b9
PZ
1908
1909 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1910}
1911
b8a4f404
PZ
1912static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1913 enum pipe pipe)
040484af 1914{
23670b32 1915 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1916 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1918 uint32_t reg, val, pipeconf_val;
040484af
JB
1919
1920 /* PCH only available on ILK+ */
55522f37 1921 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1922
1923 /* Make sure PCH DPLL is enabled */
e72f9fbf 1924 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1925 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1926
1927 /* FDI must be feeding us bits for PCH ports */
1928 assert_fdi_tx_enabled(dev_priv, pipe);
1929 assert_fdi_rx_enabled(dev_priv, pipe);
1930
23670b32
DV
1931 if (HAS_PCH_CPT(dev)) {
1932 /* Workaround: Set the timing override bit before enabling the
1933 * pch transcoder. */
1934 reg = TRANS_CHICKEN2(pipe);
1935 val = I915_READ(reg);
1936 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1937 I915_WRITE(reg, val);
59c859d6 1938 }
23670b32 1939
ab9412ba 1940 reg = PCH_TRANSCONF(pipe);
040484af 1941 val = I915_READ(reg);
5f7f726d 1942 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1943
1944 if (HAS_PCH_IBX(dev_priv->dev)) {
1945 /*
1946 * make the BPC in transcoder be consistent with
1947 * that in pipeconf reg.
1948 */
dfd07d72
DV
1949 val &= ~PIPECONF_BPC_MASK;
1950 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1951 }
5f7f726d
PZ
1952
1953 val &= ~TRANS_INTERLACE_MASK;
1954 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1955 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1956 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1957 val |= TRANS_LEGACY_INTERLACED_ILK;
1958 else
1959 val |= TRANS_INTERLACED;
5f7f726d
PZ
1960 else
1961 val |= TRANS_PROGRESSIVE;
1962
040484af
JB
1963 I915_WRITE(reg, val | TRANS_ENABLE);
1964 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1965 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1966}
1967
8fb033d7 1968static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1969 enum transcoder cpu_transcoder)
040484af 1970{
8fb033d7 1971 u32 val, pipeconf_val;
8fb033d7
PZ
1972
1973 /* PCH only available on ILK+ */
55522f37 1974 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1975
8fb033d7 1976 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1977 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1978 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1979
223a6fdf
PZ
1980 /* Workaround: set timing override bit. */
1981 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1982 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1983 I915_WRITE(_TRANSA_CHICKEN2, val);
1984
25f3ef11 1985 val = TRANS_ENABLE;
937bb610 1986 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1987
9a76b1c6
PZ
1988 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1989 PIPECONF_INTERLACED_ILK)
a35f2679 1990 val |= TRANS_INTERLACED;
8fb033d7
PZ
1991 else
1992 val |= TRANS_PROGRESSIVE;
1993
ab9412ba
DV
1994 I915_WRITE(LPT_TRANSCONF, val);
1995 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1996 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1997}
1998
b8a4f404
PZ
1999static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2000 enum pipe pipe)
040484af 2001{
23670b32
DV
2002 struct drm_device *dev = dev_priv->dev;
2003 uint32_t reg, val;
040484af
JB
2004
2005 /* FDI relies on the transcoder */
2006 assert_fdi_tx_disabled(dev_priv, pipe);
2007 assert_fdi_rx_disabled(dev_priv, pipe);
2008
291906f1
JB
2009 /* Ports must be off as well */
2010 assert_pch_ports_disabled(dev_priv, pipe);
2011
ab9412ba 2012 reg = PCH_TRANSCONF(pipe);
040484af
JB
2013 val = I915_READ(reg);
2014 val &= ~TRANS_ENABLE;
2015 I915_WRITE(reg, val);
2016 /* wait for PCH transcoder off, transcoder state */
2017 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2018 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2019
2020 if (!HAS_PCH_IBX(dev)) {
2021 /* Workaround: Clear the timing override chicken bit again. */
2022 reg = TRANS_CHICKEN2(pipe);
2023 val = I915_READ(reg);
2024 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2025 I915_WRITE(reg, val);
2026 }
040484af
JB
2027}
2028
ab4d966c 2029static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2030{
8fb033d7
PZ
2031 u32 val;
2032
ab9412ba 2033 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2034 val &= ~TRANS_ENABLE;
ab9412ba 2035 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2036 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2037 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2038 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2039
2040 /* Workaround: clear timing override bit. */
2041 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2042 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2043 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2044}
2045
b24e7179 2046/**
309cfea8 2047 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2048 * @crtc: crtc responsible for the pipe
b24e7179 2049 *
0372264a 2050 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2051 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2052 */
e1fdc473 2053static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2054{
0372264a
PZ
2055 struct drm_device *dev = crtc->base.dev;
2056 struct drm_i915_private *dev_priv = dev->dev_private;
2057 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2058 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2059 pipe);
1a240d4d 2060 enum pipe pch_transcoder;
b24e7179
JB
2061 int reg;
2062 u32 val;
2063
58c6eaa2 2064 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2065 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2066 assert_sprites_disabled(dev_priv, pipe);
2067
681e5811 2068 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2069 pch_transcoder = TRANSCODER_A;
2070 else
2071 pch_transcoder = pipe;
2072
b24e7179
JB
2073 /*
2074 * A pipe without a PLL won't actually be able to drive bits from
2075 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2076 * need the check.
2077 */
2078 if (!HAS_PCH_SPLIT(dev_priv->dev))
409ee761 2079 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2080 assert_dsi_pll_enabled(dev_priv);
2081 else
2082 assert_pll_enabled(dev_priv, pipe);
040484af 2083 else {
6e3c9717 2084 if (crtc->config->has_pch_encoder) {
040484af 2085 /* if driving the PCH, we need FDI enabled */
cc391bbb 2086 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2087 assert_fdi_tx_pll_enabled(dev_priv,
2088 (enum pipe) cpu_transcoder);
040484af
JB
2089 }
2090 /* FIXME: assert CPU port conditions for SNB+ */
2091 }
b24e7179 2092
702e7a56 2093 reg = PIPECONF(cpu_transcoder);
b24e7179 2094 val = I915_READ(reg);
7ad25d48 2095 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2096 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2097 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2098 return;
7ad25d48 2099 }
00d70b15
CW
2100
2101 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2102 POSTING_READ(reg);
b24e7179
JB
2103}
2104
2105/**
309cfea8 2106 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2107 * @crtc: crtc whose pipes is to be disabled
b24e7179 2108 *
575f7ab7
VS
2109 * Disable the pipe of @crtc, making sure that various hardware
2110 * specific requirements are met, if applicable, e.g. plane
2111 * disabled, panel fitter off, etc.
b24e7179
JB
2112 *
2113 * Will wait until the pipe has shut down before returning.
2114 */
575f7ab7 2115static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2116{
575f7ab7 2117 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2118 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2119 enum pipe pipe = crtc->pipe;
b24e7179
JB
2120 int reg;
2121 u32 val;
2122
2123 /*
2124 * Make sure planes won't keep trying to pump pixels to us,
2125 * or we might hang the display.
2126 */
2127 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2128 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2129 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2130
702e7a56 2131 reg = PIPECONF(cpu_transcoder);
b24e7179 2132 val = I915_READ(reg);
00d70b15
CW
2133 if ((val & PIPECONF_ENABLE) == 0)
2134 return;
2135
67adc644
VS
2136 /*
2137 * Double wide has implications for planes
2138 * so best keep it disabled when not needed.
2139 */
6e3c9717 2140 if (crtc->config->double_wide)
67adc644
VS
2141 val &= ~PIPECONF_DOUBLE_WIDE;
2142
2143 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2144 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2145 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2146 val &= ~PIPECONF_ENABLE;
2147
2148 I915_WRITE(reg, val);
2149 if ((val & PIPECONF_ENABLE) == 0)
2150 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2151}
2152
d74362c9
KP
2153/*
2154 * Plane regs are double buffered, going from enabled->disabled needs a
2155 * trigger in order to latch. The display address reg provides this.
2156 */
1dba99f4
VS
2157void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2158 enum plane plane)
d74362c9 2159{
3d13ef2e
DL
2160 struct drm_device *dev = dev_priv->dev;
2161 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2162
2163 I915_WRITE(reg, I915_READ(reg));
2164 POSTING_READ(reg);
d74362c9
KP
2165}
2166
b24e7179 2167/**
262ca2b0 2168 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2169 * @plane: plane to be enabled
2170 * @crtc: crtc for the plane
b24e7179 2171 *
fdd508a6 2172 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2173 */
fdd508a6
VS
2174static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2175 struct drm_crtc *crtc)
b24e7179 2176{
fdd508a6
VS
2177 struct drm_device *dev = plane->dev;
2178 struct drm_i915_private *dev_priv = dev->dev_private;
2179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2180
2181 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2182 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2183
98ec7739
VS
2184 if (intel_crtc->primary_enabled)
2185 return;
0037f71c 2186
4c445e0e 2187 intel_crtc->primary_enabled = true;
939c2fe8 2188
fdd508a6
VS
2189 dev_priv->display.update_primary_plane(crtc, plane->fb,
2190 crtc->x, crtc->y);
33c3b0d1
VS
2191
2192 /*
2193 * BDW signals flip done immediately if the plane
2194 * is disabled, even if the plane enable is already
2195 * armed to occur at the next vblank :(
2196 */
2197 if (IS_BROADWELL(dev))
2198 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2199}
2200
b24e7179 2201/**
262ca2b0 2202 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2203 * @plane: plane to be disabled
2204 * @crtc: crtc for the plane
b24e7179 2205 *
fdd508a6 2206 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2207 */
fdd508a6
VS
2208static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2209 struct drm_crtc *crtc)
b24e7179 2210{
fdd508a6
VS
2211 struct drm_device *dev = plane->dev;
2212 struct drm_i915_private *dev_priv = dev->dev_private;
2213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2214
32b7eeec
MR
2215 if (WARN_ON(!intel_crtc->active))
2216 return;
b24e7179 2217
98ec7739
VS
2218 if (!intel_crtc->primary_enabled)
2219 return;
0037f71c 2220
4c445e0e 2221 intel_crtc->primary_enabled = false;
939c2fe8 2222
fdd508a6
VS
2223 dev_priv->display.update_primary_plane(crtc, plane->fb,
2224 crtc->x, crtc->y);
b24e7179
JB
2225}
2226
693db184
CW
2227static bool need_vtd_wa(struct drm_device *dev)
2228{
2229#ifdef CONFIG_INTEL_IOMMU
2230 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2231 return true;
2232#endif
2233 return false;
2234}
2235
6761dd31
TU
2236static unsigned int
2237intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2238 uint64_t fb_format_modifier)
a57ce0b2 2239{
6761dd31
TU
2240 unsigned int tile_height;
2241 uint32_t pixel_bytes;
a57ce0b2 2242
b5d0e9bf
DL
2243 switch (fb_format_modifier) {
2244 case DRM_FORMAT_MOD_NONE:
2245 tile_height = 1;
2246 break;
2247 case I915_FORMAT_MOD_X_TILED:
2248 tile_height = IS_GEN2(dev) ? 16 : 8;
2249 break;
2250 case I915_FORMAT_MOD_Y_TILED:
2251 tile_height = 32;
2252 break;
2253 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2254 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2255 switch (pixel_bytes) {
b5d0e9bf 2256 default:
6761dd31 2257 case 1:
b5d0e9bf
DL
2258 tile_height = 64;
2259 break;
6761dd31
TU
2260 case 2:
2261 case 4:
b5d0e9bf
DL
2262 tile_height = 32;
2263 break;
6761dd31 2264 case 8:
b5d0e9bf
DL
2265 tile_height = 16;
2266 break;
6761dd31 2267 case 16:
b5d0e9bf
DL
2268 WARN_ONCE(1,
2269 "128-bit pixels are not supported for display!");
2270 tile_height = 16;
2271 break;
2272 }
2273 break;
2274 default:
2275 MISSING_CASE(fb_format_modifier);
2276 tile_height = 1;
2277 break;
2278 }
091df6cb 2279
6761dd31
TU
2280 return tile_height;
2281}
2282
2283unsigned int
2284intel_fb_align_height(struct drm_device *dev, unsigned int height,
2285 uint32_t pixel_format, uint64_t fb_format_modifier)
2286{
2287 return ALIGN(height, intel_tile_height(dev, pixel_format,
2288 fb_format_modifier));
a57ce0b2
JB
2289}
2290
f64b98cd
TU
2291static int
2292intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2293 const struct drm_plane_state *plane_state)
2294{
2295 *view = i915_ggtt_view_normal;
2296
2297 return 0;
2298}
2299
127bd2ac 2300int
850c4cdc
TU
2301intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2302 struct drm_framebuffer *fb,
82bc3b2d 2303 const struct drm_plane_state *plane_state,
a4872ba6 2304 struct intel_engine_cs *pipelined)
6b95a207 2305{
850c4cdc 2306 struct drm_device *dev = fb->dev;
ce453d81 2307 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2308 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2309 struct i915_ggtt_view view;
6b95a207
KH
2310 u32 alignment;
2311 int ret;
2312
ebcdd39e
MR
2313 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2314
7b911adc
TU
2315 switch (fb->modifier[0]) {
2316 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2317 if (INTEL_INFO(dev)->gen >= 9)
2318 alignment = 256 * 1024;
2319 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2320 alignment = 128 * 1024;
a6c45cf0 2321 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2322 alignment = 4 * 1024;
2323 else
2324 alignment = 64 * 1024;
6b95a207 2325 break;
7b911adc 2326 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2327 if (INTEL_INFO(dev)->gen >= 9)
2328 alignment = 256 * 1024;
2329 else {
2330 /* pin() will align the object as required by fence */
2331 alignment = 0;
2332 }
6b95a207 2333 break;
7b911adc 2334 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2335 case I915_FORMAT_MOD_Yf_TILED:
2336 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2337 "Y tiling bo slipped through, driver bug!\n"))
2338 return -EINVAL;
2339 alignment = 1 * 1024 * 1024;
2340 break;
6b95a207 2341 default:
7b911adc
TU
2342 MISSING_CASE(fb->modifier[0]);
2343 return -EINVAL;
6b95a207
KH
2344 }
2345
f64b98cd
TU
2346 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2347 if (ret)
2348 return ret;
2349
693db184
CW
2350 /* Note that the w/a also requires 64 PTE of padding following the
2351 * bo. We currently fill all unused PTE with the shadow page and so
2352 * we should always have valid PTE following the scanout preventing
2353 * the VT-d warning.
2354 */
2355 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2356 alignment = 256 * 1024;
2357
d6dd6843
PZ
2358 /*
2359 * Global gtt pte registers are special registers which actually forward
2360 * writes to a chunk of system memory. Which means that there is no risk
2361 * that the register values disappear as soon as we call
2362 * intel_runtime_pm_put(), so it is correct to wrap only the
2363 * pin/unpin/fence and not more.
2364 */
2365 intel_runtime_pm_get(dev_priv);
2366
ce453d81 2367 dev_priv->mm.interruptible = false;
e6617330 2368 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2369 &view);
48b956c5 2370 if (ret)
ce453d81 2371 goto err_interruptible;
6b95a207
KH
2372
2373 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2374 * fence, whereas 965+ only requires a fence if using
2375 * framebuffer compression. For simplicity, we always install
2376 * a fence as the cost is not that onerous.
2377 */
06d98131 2378 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2379 if (ret)
2380 goto err_unpin;
1690e1eb 2381
9a5a53b3 2382 i915_gem_object_pin_fence(obj);
6b95a207 2383
ce453d81 2384 dev_priv->mm.interruptible = true;
d6dd6843 2385 intel_runtime_pm_put(dev_priv);
6b95a207 2386 return 0;
48b956c5
CW
2387
2388err_unpin:
f64b98cd 2389 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2390err_interruptible:
2391 dev_priv->mm.interruptible = true;
d6dd6843 2392 intel_runtime_pm_put(dev_priv);
48b956c5 2393 return ret;
6b95a207
KH
2394}
2395
82bc3b2d
TU
2396static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2397 const struct drm_plane_state *plane_state)
1690e1eb 2398{
82bc3b2d 2399 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2400 struct i915_ggtt_view view;
2401 int ret;
82bc3b2d 2402
ebcdd39e
MR
2403 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2404
f64b98cd
TU
2405 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2406 WARN_ONCE(ret, "Couldn't get view from plane state!");
2407
1690e1eb 2408 i915_gem_object_unpin_fence(obj);
f64b98cd 2409 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2410}
2411
c2c75131
DV
2412/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2413 * is assumed to be a power-of-two. */
bc752862
CW
2414unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2415 unsigned int tiling_mode,
2416 unsigned int cpp,
2417 unsigned int pitch)
c2c75131 2418{
bc752862
CW
2419 if (tiling_mode != I915_TILING_NONE) {
2420 unsigned int tile_rows, tiles;
c2c75131 2421
bc752862
CW
2422 tile_rows = *y / 8;
2423 *y %= 8;
c2c75131 2424
bc752862
CW
2425 tiles = *x / (512/cpp);
2426 *x %= 512/cpp;
2427
2428 return tile_rows * pitch * 8 + tiles * 4096;
2429 } else {
2430 unsigned int offset;
2431
2432 offset = *y * pitch + *x * cpp;
2433 *y = 0;
2434 *x = (offset & 4095) / cpp;
2435 return offset & -4096;
2436 }
c2c75131
DV
2437}
2438
b35d63fa 2439static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2440{
2441 switch (format) {
2442 case DISPPLANE_8BPP:
2443 return DRM_FORMAT_C8;
2444 case DISPPLANE_BGRX555:
2445 return DRM_FORMAT_XRGB1555;
2446 case DISPPLANE_BGRX565:
2447 return DRM_FORMAT_RGB565;
2448 default:
2449 case DISPPLANE_BGRX888:
2450 return DRM_FORMAT_XRGB8888;
2451 case DISPPLANE_RGBX888:
2452 return DRM_FORMAT_XBGR8888;
2453 case DISPPLANE_BGRX101010:
2454 return DRM_FORMAT_XRGB2101010;
2455 case DISPPLANE_RGBX101010:
2456 return DRM_FORMAT_XBGR2101010;
2457 }
2458}
2459
bc8d7dff
DL
2460static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2461{
2462 switch (format) {
2463 case PLANE_CTL_FORMAT_RGB_565:
2464 return DRM_FORMAT_RGB565;
2465 default:
2466 case PLANE_CTL_FORMAT_XRGB_8888:
2467 if (rgb_order) {
2468 if (alpha)
2469 return DRM_FORMAT_ABGR8888;
2470 else
2471 return DRM_FORMAT_XBGR8888;
2472 } else {
2473 if (alpha)
2474 return DRM_FORMAT_ARGB8888;
2475 else
2476 return DRM_FORMAT_XRGB8888;
2477 }
2478 case PLANE_CTL_FORMAT_XRGB_2101010:
2479 if (rgb_order)
2480 return DRM_FORMAT_XBGR2101010;
2481 else
2482 return DRM_FORMAT_XRGB2101010;
2483 }
2484}
2485
5724dbd1
DL
2486static bool
2487intel_alloc_plane_obj(struct intel_crtc *crtc,
2488 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2489{
2490 struct drm_device *dev = crtc->base.dev;
2491 struct drm_i915_gem_object *obj = NULL;
2492 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2493 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2494 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2495 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2496 PAGE_SIZE);
2497
2498 size_aligned -= base_aligned;
46f297fb 2499
ff2652ea
CW
2500 if (plane_config->size == 0)
2501 return false;
2502
f37b5c2b
DV
2503 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2504 base_aligned,
2505 base_aligned,
2506 size_aligned);
46f297fb 2507 if (!obj)
484b41dd 2508 return false;
46f297fb 2509
49af449b
DL
2510 obj->tiling_mode = plane_config->tiling;
2511 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2512 obj->stride = fb->pitches[0];
46f297fb 2513
6bf129df
DL
2514 mode_cmd.pixel_format = fb->pixel_format;
2515 mode_cmd.width = fb->width;
2516 mode_cmd.height = fb->height;
2517 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2518 mode_cmd.modifier[0] = fb->modifier[0];
2519 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2520
2521 mutex_lock(&dev->struct_mutex);
2522
6bf129df 2523 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2524 &mode_cmd, obj)) {
46f297fb
JB
2525 DRM_DEBUG_KMS("intel fb init failed\n");
2526 goto out_unref_obj;
2527 }
2528
a071fa00 2529 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2530 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2531
2532 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2533 return true;
46f297fb
JB
2534
2535out_unref_obj:
2536 drm_gem_object_unreference(&obj->base);
2537 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2538 return false;
2539}
2540
afd65eb4
MR
2541/* Update plane->state->fb to match plane->fb after driver-internal updates */
2542static void
2543update_state_fb(struct drm_plane *plane)
2544{
2545 if (plane->fb == plane->state->fb)
2546 return;
2547
2548 if (plane->state->fb)
2549 drm_framebuffer_unreference(plane->state->fb);
2550 plane->state->fb = plane->fb;
2551 if (plane->state->fb)
2552 drm_framebuffer_reference(plane->state->fb);
2553}
2554
5724dbd1
DL
2555static void
2556intel_find_plane_obj(struct intel_crtc *intel_crtc,
2557 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2558{
2559 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2560 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2561 struct drm_crtc *c;
2562 struct intel_crtc *i;
2ff8fde1 2563 struct drm_i915_gem_object *obj;
484b41dd 2564
2d14030b 2565 if (!plane_config->fb)
484b41dd
JB
2566 return;
2567
f55548b5 2568 if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
fb9981aa
DL
2569 struct drm_plane *primary = intel_crtc->base.primary;
2570
2571 primary->fb = &plane_config->fb->base;
2572 primary->state->crtc = &intel_crtc->base;
2573 update_state_fb(primary);
2574
484b41dd 2575 return;
f55548b5 2576 }
484b41dd 2577
2d14030b 2578 kfree(plane_config->fb);
484b41dd
JB
2579
2580 /*
2581 * Failed to alloc the obj, check to see if we should share
2582 * an fb with another CRTC instead
2583 */
70e1e0ec 2584 for_each_crtc(dev, c) {
484b41dd
JB
2585 i = to_intel_crtc(c);
2586
2587 if (c == &intel_crtc->base)
2588 continue;
2589
2ff8fde1
MR
2590 if (!i->active)
2591 continue;
2592
2593 obj = intel_fb_obj(c->primary->fb);
2594 if (obj == NULL)
484b41dd
JB
2595 continue;
2596
2ff8fde1 2597 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
fb9981aa
DL
2598 struct drm_plane *primary = intel_crtc->base.primary;
2599
d9ceb816
JB
2600 if (obj->tiling_mode != I915_TILING_NONE)
2601 dev_priv->preserve_bios_swizzle = true;
2602
66e514c1 2603 drm_framebuffer_reference(c->primary->fb);
fb9981aa
DL
2604 primary->fb = c->primary->fb;
2605 primary->state->crtc = &intel_crtc->base;
5ba76c41 2606 update_state_fb(intel_crtc->base.primary);
2ff8fde1 2607 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2608 break;
2609 }
2610 }
46f297fb
JB
2611}
2612
29b9bde6
DV
2613static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2614 struct drm_framebuffer *fb,
2615 int x, int y)
81255565
JB
2616{
2617 struct drm_device *dev = crtc->dev;
2618 struct drm_i915_private *dev_priv = dev->dev_private;
2619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2620 struct drm_i915_gem_object *obj;
81255565 2621 int plane = intel_crtc->plane;
e506a0c6 2622 unsigned long linear_offset;
81255565 2623 u32 dspcntr;
f45651ba 2624 u32 reg = DSPCNTR(plane);
48404c1e 2625 int pixel_size;
f45651ba 2626
fdd508a6
VS
2627 if (!intel_crtc->primary_enabled) {
2628 I915_WRITE(reg, 0);
2629 if (INTEL_INFO(dev)->gen >= 4)
2630 I915_WRITE(DSPSURF(plane), 0);
2631 else
2632 I915_WRITE(DSPADDR(plane), 0);
2633 POSTING_READ(reg);
2634 return;
2635 }
2636
c9ba6fad
VS
2637 obj = intel_fb_obj(fb);
2638 if (WARN_ON(obj == NULL))
2639 return;
2640
2641 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2642
f45651ba
VS
2643 dspcntr = DISPPLANE_GAMMA_ENABLE;
2644
fdd508a6 2645 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2646
2647 if (INTEL_INFO(dev)->gen < 4) {
2648 if (intel_crtc->pipe == PIPE_B)
2649 dspcntr |= DISPPLANE_SEL_PIPE_B;
2650
2651 /* pipesrc and dspsize control the size that is scaled from,
2652 * which should always be the user's requested size.
2653 */
2654 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2655 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2656 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2657 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2658 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2659 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2660 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2661 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2662 I915_WRITE(PRIMPOS(plane), 0);
2663 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2664 }
81255565 2665
57779d06
VS
2666 switch (fb->pixel_format) {
2667 case DRM_FORMAT_C8:
81255565
JB
2668 dspcntr |= DISPPLANE_8BPP;
2669 break;
57779d06
VS
2670 case DRM_FORMAT_XRGB1555:
2671 case DRM_FORMAT_ARGB1555:
2672 dspcntr |= DISPPLANE_BGRX555;
81255565 2673 break;
57779d06
VS
2674 case DRM_FORMAT_RGB565:
2675 dspcntr |= DISPPLANE_BGRX565;
2676 break;
2677 case DRM_FORMAT_XRGB8888:
2678 case DRM_FORMAT_ARGB8888:
2679 dspcntr |= DISPPLANE_BGRX888;
2680 break;
2681 case DRM_FORMAT_XBGR8888:
2682 case DRM_FORMAT_ABGR8888:
2683 dspcntr |= DISPPLANE_RGBX888;
2684 break;
2685 case DRM_FORMAT_XRGB2101010:
2686 case DRM_FORMAT_ARGB2101010:
2687 dspcntr |= DISPPLANE_BGRX101010;
2688 break;
2689 case DRM_FORMAT_XBGR2101010:
2690 case DRM_FORMAT_ABGR2101010:
2691 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2692 break;
2693 default:
baba133a 2694 BUG();
81255565 2695 }
57779d06 2696
f45651ba
VS
2697 if (INTEL_INFO(dev)->gen >= 4 &&
2698 obj->tiling_mode != I915_TILING_NONE)
2699 dspcntr |= DISPPLANE_TILED;
81255565 2700
de1aa629
VS
2701 if (IS_G4X(dev))
2702 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2703
b9897127 2704 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2705
c2c75131
DV
2706 if (INTEL_INFO(dev)->gen >= 4) {
2707 intel_crtc->dspaddr_offset =
bc752862 2708 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2709 pixel_size,
bc752862 2710 fb->pitches[0]);
c2c75131
DV
2711 linear_offset -= intel_crtc->dspaddr_offset;
2712 } else {
e506a0c6 2713 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2714 }
e506a0c6 2715
8e7d688b 2716 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2717 dspcntr |= DISPPLANE_ROTATE_180;
2718
6e3c9717
ACO
2719 x += (intel_crtc->config->pipe_src_w - 1);
2720 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2721
2722 /* Finding the last pixel of the last line of the display
2723 data and adding to linear_offset*/
2724 linear_offset +=
6e3c9717
ACO
2725 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2726 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2727 }
2728
2729 I915_WRITE(reg, dspcntr);
2730
01f2c773 2731 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2732 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2733 I915_WRITE(DSPSURF(plane),
2734 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2735 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2736 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2737 } else
f343c5f6 2738 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2739 POSTING_READ(reg);
17638cd6
JB
2740}
2741
29b9bde6
DV
2742static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2743 struct drm_framebuffer *fb,
2744 int x, int y)
17638cd6
JB
2745{
2746 struct drm_device *dev = crtc->dev;
2747 struct drm_i915_private *dev_priv = dev->dev_private;
2748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2749 struct drm_i915_gem_object *obj;
17638cd6 2750 int plane = intel_crtc->plane;
e506a0c6 2751 unsigned long linear_offset;
17638cd6 2752 u32 dspcntr;
f45651ba 2753 u32 reg = DSPCNTR(plane);
48404c1e 2754 int pixel_size;
f45651ba 2755
fdd508a6
VS
2756 if (!intel_crtc->primary_enabled) {
2757 I915_WRITE(reg, 0);
2758 I915_WRITE(DSPSURF(plane), 0);
2759 POSTING_READ(reg);
2760 return;
2761 }
2762
c9ba6fad
VS
2763 obj = intel_fb_obj(fb);
2764 if (WARN_ON(obj == NULL))
2765 return;
2766
2767 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2768
f45651ba
VS
2769 dspcntr = DISPPLANE_GAMMA_ENABLE;
2770
fdd508a6 2771 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2772
2773 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2774 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2775
57779d06
VS
2776 switch (fb->pixel_format) {
2777 case DRM_FORMAT_C8:
17638cd6
JB
2778 dspcntr |= DISPPLANE_8BPP;
2779 break;
57779d06
VS
2780 case DRM_FORMAT_RGB565:
2781 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2782 break;
57779d06
VS
2783 case DRM_FORMAT_XRGB8888:
2784 case DRM_FORMAT_ARGB8888:
2785 dspcntr |= DISPPLANE_BGRX888;
2786 break;
2787 case DRM_FORMAT_XBGR8888:
2788 case DRM_FORMAT_ABGR8888:
2789 dspcntr |= DISPPLANE_RGBX888;
2790 break;
2791 case DRM_FORMAT_XRGB2101010:
2792 case DRM_FORMAT_ARGB2101010:
2793 dspcntr |= DISPPLANE_BGRX101010;
2794 break;
2795 case DRM_FORMAT_XBGR2101010:
2796 case DRM_FORMAT_ABGR2101010:
2797 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2798 break;
2799 default:
baba133a 2800 BUG();
17638cd6
JB
2801 }
2802
2803 if (obj->tiling_mode != I915_TILING_NONE)
2804 dspcntr |= DISPPLANE_TILED;
17638cd6 2805
f45651ba 2806 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2807 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2808
b9897127 2809 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2810 intel_crtc->dspaddr_offset =
bc752862 2811 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2812 pixel_size,
bc752862 2813 fb->pitches[0]);
c2c75131 2814 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2815 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2816 dspcntr |= DISPPLANE_ROTATE_180;
2817
2818 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2819 x += (intel_crtc->config->pipe_src_w - 1);
2820 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2821
2822 /* Finding the last pixel of the last line of the display
2823 data and adding to linear_offset*/
2824 linear_offset +=
6e3c9717
ACO
2825 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2826 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2827 }
2828 }
2829
2830 I915_WRITE(reg, dspcntr);
17638cd6 2831
01f2c773 2832 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2833 I915_WRITE(DSPSURF(plane),
2834 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2835 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2836 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2837 } else {
2838 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2839 I915_WRITE(DSPLINOFF(plane), linear_offset);
2840 }
17638cd6 2841 POSTING_READ(reg);
17638cd6
JB
2842}
2843
b321803d
DL
2844u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2845 uint32_t pixel_format)
2846{
2847 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2848
2849 /*
2850 * The stride is either expressed as a multiple of 64 bytes
2851 * chunks for linear buffers or in number of tiles for tiled
2852 * buffers.
2853 */
2854 switch (fb_modifier) {
2855 case DRM_FORMAT_MOD_NONE:
2856 return 64;
2857 case I915_FORMAT_MOD_X_TILED:
2858 if (INTEL_INFO(dev)->gen == 2)
2859 return 128;
2860 return 512;
2861 case I915_FORMAT_MOD_Y_TILED:
2862 /* No need to check for old gens and Y tiling since this is
2863 * about the display engine and those will be blocked before
2864 * we get here.
2865 */
2866 return 128;
2867 case I915_FORMAT_MOD_Yf_TILED:
2868 if (bits_per_pixel == 8)
2869 return 64;
2870 else
2871 return 128;
2872 default:
2873 MISSING_CASE(fb_modifier);
2874 return 64;
2875 }
2876}
2877
70d21f0e
DL
2878static void skylake_update_primary_plane(struct drm_crtc *crtc,
2879 struct drm_framebuffer *fb,
2880 int x, int y)
2881{
2882 struct drm_device *dev = crtc->dev;
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
70d21f0e
DL
2885 struct drm_i915_gem_object *obj;
2886 int pipe = intel_crtc->pipe;
b321803d 2887 u32 plane_ctl, stride_div;
70d21f0e
DL
2888
2889 if (!intel_crtc->primary_enabled) {
2890 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2891 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2892 POSTING_READ(PLANE_CTL(pipe, 0));
2893 return;
2894 }
2895
2896 plane_ctl = PLANE_CTL_ENABLE |
2897 PLANE_CTL_PIPE_GAMMA_ENABLE |
2898 PLANE_CTL_PIPE_CSC_ENABLE;
2899
2900 switch (fb->pixel_format) {
2901 case DRM_FORMAT_RGB565:
2902 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2903 break;
2904 case DRM_FORMAT_XRGB8888:
2905 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2906 break;
f75fb42a
JN
2907 case DRM_FORMAT_ARGB8888:
2908 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2909 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2910 break;
70d21f0e
DL
2911 case DRM_FORMAT_XBGR8888:
2912 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2913 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2914 break;
f75fb42a
JN
2915 case DRM_FORMAT_ABGR8888:
2916 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2917 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2918 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2919 break;
70d21f0e
DL
2920 case DRM_FORMAT_XRGB2101010:
2921 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2922 break;
2923 case DRM_FORMAT_XBGR2101010:
2924 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2925 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2926 break;
2927 default:
2928 BUG();
2929 }
2930
30af77c4
DV
2931 switch (fb->modifier[0]) {
2932 case DRM_FORMAT_MOD_NONE:
70d21f0e 2933 break;
30af77c4 2934 case I915_FORMAT_MOD_X_TILED:
70d21f0e 2935 plane_ctl |= PLANE_CTL_TILED_X;
b321803d
DL
2936 break;
2937 case I915_FORMAT_MOD_Y_TILED:
2938 plane_ctl |= PLANE_CTL_TILED_Y;
2939 break;
2940 case I915_FORMAT_MOD_Yf_TILED:
2941 plane_ctl |= PLANE_CTL_TILED_YF;
70d21f0e
DL
2942 break;
2943 default:
b321803d 2944 MISSING_CASE(fb->modifier[0]);
70d21f0e
DL
2945 }
2946
2947 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
8e7d688b 2948 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
1447dde0 2949 plane_ctl |= PLANE_CTL_ROTATE_180;
70d21f0e 2950
b321803d
DL
2951 obj = intel_fb_obj(fb);
2952 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
2953 fb->pixel_format);
2954
70d21f0e
DL
2955 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2956
70d21f0e
DL
2957 I915_WRITE(PLANE_POS(pipe, 0), 0);
2958 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2959 I915_WRITE(PLANE_SIZE(pipe, 0),
6e3c9717
ACO
2960 (intel_crtc->config->pipe_src_h - 1) << 16 |
2961 (intel_crtc->config->pipe_src_w - 1));
b321803d 2962 I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
70d21f0e
DL
2963 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2964
2965 POSTING_READ(PLANE_SURF(pipe, 0));
2966}
2967
17638cd6
JB
2968/* Assume fb object is pinned & idle & fenced and just update base pointers */
2969static int
2970intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2971 int x, int y, enum mode_set_atomic state)
2972{
2973 struct drm_device *dev = crtc->dev;
2974 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2975
6b8e6ed0
CW
2976 if (dev_priv->display.disable_fbc)
2977 dev_priv->display.disable_fbc(dev);
81255565 2978
29b9bde6
DV
2979 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2980
2981 return 0;
81255565
JB
2982}
2983
7514747d 2984static void intel_complete_page_flips(struct drm_device *dev)
96a02917 2985{
96a02917
VS
2986 struct drm_crtc *crtc;
2987
70e1e0ec 2988 for_each_crtc(dev, crtc) {
96a02917
VS
2989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2990 enum plane plane = intel_crtc->plane;
2991
2992 intel_prepare_page_flip(dev, plane);
2993 intel_finish_page_flip_plane(dev, plane);
2994 }
7514747d
VS
2995}
2996
2997static void intel_update_primary_planes(struct drm_device *dev)
2998{
2999 struct drm_i915_private *dev_priv = dev->dev_private;
3000 struct drm_crtc *crtc;
96a02917 3001
70e1e0ec 3002 for_each_crtc(dev, crtc) {
96a02917
VS
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3004
51fd371b 3005 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3006 /*
3007 * FIXME: Once we have proper support for primary planes (and
3008 * disabling them without disabling the entire crtc) allow again
66e514c1 3009 * a NULL crtc->primary->fb.
947fdaad 3010 */
f4510a27 3011 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3012 dev_priv->display.update_primary_plane(crtc,
66e514c1 3013 crtc->primary->fb,
262ca2b0
MR
3014 crtc->x,
3015 crtc->y);
51fd371b 3016 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3017 }
3018}
3019
7514747d
VS
3020void intel_prepare_reset(struct drm_device *dev)
3021{
f98ce92f
VS
3022 struct drm_i915_private *dev_priv = to_i915(dev);
3023 struct intel_crtc *crtc;
3024
7514747d
VS
3025 /* no reset support for gen2 */
3026 if (IS_GEN2(dev))
3027 return;
3028
3029 /* reset doesn't touch the display */
3030 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3031 return;
3032
3033 drm_modeset_lock_all(dev);
f98ce92f
VS
3034
3035 /*
3036 * Disabling the crtcs gracefully seems nicer. Also the
3037 * g33 docs say we should at least disable all the planes.
3038 */
3039 for_each_intel_crtc(dev, crtc) {
3040 if (crtc->active)
3041 dev_priv->display.crtc_disable(&crtc->base);
3042 }
7514747d
VS
3043}
3044
3045void intel_finish_reset(struct drm_device *dev)
3046{
3047 struct drm_i915_private *dev_priv = to_i915(dev);
3048
3049 /*
3050 * Flips in the rings will be nuked by the reset,
3051 * so complete all pending flips so that user space
3052 * will get its events and not get stuck.
3053 */
3054 intel_complete_page_flips(dev);
3055
3056 /* no reset support for gen2 */
3057 if (IS_GEN2(dev))
3058 return;
3059
3060 /* reset doesn't touch the display */
3061 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3062 /*
3063 * Flips in the rings have been nuked by the reset,
3064 * so update the base address of all primary
3065 * planes to the the last fb to make sure we're
3066 * showing the correct fb after a reset.
3067 */
3068 intel_update_primary_planes(dev);
3069 return;
3070 }
3071
3072 /*
3073 * The display has been reset as well,
3074 * so need a full re-initialization.
3075 */
3076 intel_runtime_pm_disable_interrupts(dev_priv);
3077 intel_runtime_pm_enable_interrupts(dev_priv);
3078
3079 intel_modeset_init_hw(dev);
3080
3081 spin_lock_irq(&dev_priv->irq_lock);
3082 if (dev_priv->display.hpd_irq_setup)
3083 dev_priv->display.hpd_irq_setup(dev);
3084 spin_unlock_irq(&dev_priv->irq_lock);
3085
3086 intel_modeset_setup_hw_state(dev, true);
3087
3088 intel_hpd_init(dev_priv);
3089
3090 drm_modeset_unlock_all(dev);
3091}
3092
14667a4b
CW
3093static int
3094intel_finish_fb(struct drm_framebuffer *old_fb)
3095{
2ff8fde1 3096 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
3097 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3098 bool was_interruptible = dev_priv->mm.interruptible;
3099 int ret;
3100
14667a4b
CW
3101 /* Big Hammer, we also need to ensure that any pending
3102 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3103 * current scanout is retired before unpinning the old
3104 * framebuffer.
3105 *
3106 * This should only fail upon a hung GPU, in which case we
3107 * can safely continue.
3108 */
3109 dev_priv->mm.interruptible = false;
3110 ret = i915_gem_object_finish_gpu(obj);
3111 dev_priv->mm.interruptible = was_interruptible;
3112
3113 return ret;
3114}
3115
7d5e3799
CW
3116static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3117{
3118 struct drm_device *dev = crtc->dev;
3119 struct drm_i915_private *dev_priv = dev->dev_private;
3120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3121 bool pending;
3122
3123 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3124 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3125 return false;
3126
5e2d7afc 3127 spin_lock_irq(&dev->event_lock);
7d5e3799 3128 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3129 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3130
3131 return pending;
3132}
3133
e30e8f75
GP
3134static void intel_update_pipe_size(struct intel_crtc *crtc)
3135{
3136 struct drm_device *dev = crtc->base.dev;
3137 struct drm_i915_private *dev_priv = dev->dev_private;
3138 const struct drm_display_mode *adjusted_mode;
3139
3140 if (!i915.fastboot)
3141 return;
3142
3143 /*
3144 * Update pipe size and adjust fitter if needed: the reason for this is
3145 * that in compute_mode_changes we check the native mode (not the pfit
3146 * mode) to see if we can flip rather than do a full mode set. In the
3147 * fastboot case, we'll flip, but if we don't update the pipesrc and
3148 * pfit state, we'll end up with a big fb scanned out into the wrong
3149 * sized surface.
3150 *
3151 * To fix this properly, we need to hoist the checks up into
3152 * compute_mode_changes (or above), check the actual pfit state and
3153 * whether the platform allows pfit disable with pipe active, and only
3154 * then update the pipesrc and pfit state, even on the flip path.
3155 */
3156
6e3c9717 3157 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3158
3159 I915_WRITE(PIPESRC(crtc->pipe),
3160 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3161 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3162 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3163 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3164 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3165 I915_WRITE(PF_CTL(crtc->pipe), 0);
3166 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3167 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3168 }
6e3c9717
ACO
3169 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3170 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3171}
3172
5e84e1a4
ZW
3173static void intel_fdi_normal_train(struct drm_crtc *crtc)
3174{
3175 struct drm_device *dev = crtc->dev;
3176 struct drm_i915_private *dev_priv = dev->dev_private;
3177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3178 int pipe = intel_crtc->pipe;
3179 u32 reg, temp;
3180
3181 /* enable normal train */
3182 reg = FDI_TX_CTL(pipe);
3183 temp = I915_READ(reg);
61e499bf 3184 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3185 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3186 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3187 } else {
3188 temp &= ~FDI_LINK_TRAIN_NONE;
3189 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3190 }
5e84e1a4
ZW
3191 I915_WRITE(reg, temp);
3192
3193 reg = FDI_RX_CTL(pipe);
3194 temp = I915_READ(reg);
3195 if (HAS_PCH_CPT(dev)) {
3196 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3197 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3198 } else {
3199 temp &= ~FDI_LINK_TRAIN_NONE;
3200 temp |= FDI_LINK_TRAIN_NONE;
3201 }
3202 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3203
3204 /* wait one idle pattern time */
3205 POSTING_READ(reg);
3206 udelay(1000);
357555c0
JB
3207
3208 /* IVB wants error correction enabled */
3209 if (IS_IVYBRIDGE(dev))
3210 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3211 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3212}
3213
8db9d77b
ZW
3214/* The FDI link training functions for ILK/Ibexpeak. */
3215static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3216{
3217 struct drm_device *dev = crtc->dev;
3218 struct drm_i915_private *dev_priv = dev->dev_private;
3219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3220 int pipe = intel_crtc->pipe;
5eddb70b 3221 u32 reg, temp, tries;
8db9d77b 3222
1c8562f6 3223 /* FDI needs bits from pipe first */
0fc932b8 3224 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3225
e1a44743
AJ
3226 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3227 for train result */
5eddb70b
CW
3228 reg = FDI_RX_IMR(pipe);
3229 temp = I915_READ(reg);
e1a44743
AJ
3230 temp &= ~FDI_RX_SYMBOL_LOCK;
3231 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3232 I915_WRITE(reg, temp);
3233 I915_READ(reg);
e1a44743
AJ
3234 udelay(150);
3235
8db9d77b 3236 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3237 reg = FDI_TX_CTL(pipe);
3238 temp = I915_READ(reg);
627eb5a3 3239 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3240 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3241 temp &= ~FDI_LINK_TRAIN_NONE;
3242 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3243 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3244
5eddb70b
CW
3245 reg = FDI_RX_CTL(pipe);
3246 temp = I915_READ(reg);
8db9d77b
ZW
3247 temp &= ~FDI_LINK_TRAIN_NONE;
3248 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3249 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3250
3251 POSTING_READ(reg);
8db9d77b
ZW
3252 udelay(150);
3253
5b2adf89 3254 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3255 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3256 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3257 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3258
5eddb70b 3259 reg = FDI_RX_IIR(pipe);
e1a44743 3260 for (tries = 0; tries < 5; tries++) {
5eddb70b 3261 temp = I915_READ(reg);
8db9d77b
ZW
3262 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3263
3264 if ((temp & FDI_RX_BIT_LOCK)) {
3265 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3266 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3267 break;
3268 }
8db9d77b 3269 }
e1a44743 3270 if (tries == 5)
5eddb70b 3271 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3272
3273 /* Train 2 */
5eddb70b
CW
3274 reg = FDI_TX_CTL(pipe);
3275 temp = I915_READ(reg);
8db9d77b
ZW
3276 temp &= ~FDI_LINK_TRAIN_NONE;
3277 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3278 I915_WRITE(reg, temp);
8db9d77b 3279
5eddb70b
CW
3280 reg = FDI_RX_CTL(pipe);
3281 temp = I915_READ(reg);
8db9d77b
ZW
3282 temp &= ~FDI_LINK_TRAIN_NONE;
3283 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3284 I915_WRITE(reg, temp);
8db9d77b 3285
5eddb70b
CW
3286 POSTING_READ(reg);
3287 udelay(150);
8db9d77b 3288
5eddb70b 3289 reg = FDI_RX_IIR(pipe);
e1a44743 3290 for (tries = 0; tries < 5; tries++) {
5eddb70b 3291 temp = I915_READ(reg);
8db9d77b
ZW
3292 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3293
3294 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3295 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3296 DRM_DEBUG_KMS("FDI train 2 done.\n");
3297 break;
3298 }
8db9d77b 3299 }
e1a44743 3300 if (tries == 5)
5eddb70b 3301 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3302
3303 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3304
8db9d77b
ZW
3305}
3306
0206e353 3307static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3308 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3309 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3310 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3311 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3312};
3313
3314/* The FDI link training functions for SNB/Cougarpoint. */
3315static void gen6_fdi_link_train(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3320 int pipe = intel_crtc->pipe;
fa37d39e 3321 u32 reg, temp, i, retry;
8db9d77b 3322
e1a44743
AJ
3323 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3324 for train result */
5eddb70b
CW
3325 reg = FDI_RX_IMR(pipe);
3326 temp = I915_READ(reg);
e1a44743
AJ
3327 temp &= ~FDI_RX_SYMBOL_LOCK;
3328 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3329 I915_WRITE(reg, temp);
3330
3331 POSTING_READ(reg);
e1a44743
AJ
3332 udelay(150);
3333
8db9d77b 3334 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3335 reg = FDI_TX_CTL(pipe);
3336 temp = I915_READ(reg);
627eb5a3 3337 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3338 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3339 temp &= ~FDI_LINK_TRAIN_NONE;
3340 temp |= FDI_LINK_TRAIN_PATTERN_1;
3341 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3342 /* SNB-B */
3343 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3344 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3345
d74cf324
DV
3346 I915_WRITE(FDI_RX_MISC(pipe),
3347 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3348
5eddb70b
CW
3349 reg = FDI_RX_CTL(pipe);
3350 temp = I915_READ(reg);
8db9d77b
ZW
3351 if (HAS_PCH_CPT(dev)) {
3352 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3353 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3354 } else {
3355 temp &= ~FDI_LINK_TRAIN_NONE;
3356 temp |= FDI_LINK_TRAIN_PATTERN_1;
3357 }
5eddb70b
CW
3358 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3359
3360 POSTING_READ(reg);
8db9d77b
ZW
3361 udelay(150);
3362
0206e353 3363 for (i = 0; i < 4; i++) {
5eddb70b
CW
3364 reg = FDI_TX_CTL(pipe);
3365 temp = I915_READ(reg);
8db9d77b
ZW
3366 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3367 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3368 I915_WRITE(reg, temp);
3369
3370 POSTING_READ(reg);
8db9d77b
ZW
3371 udelay(500);
3372
fa37d39e
SP
3373 for (retry = 0; retry < 5; retry++) {
3374 reg = FDI_RX_IIR(pipe);
3375 temp = I915_READ(reg);
3376 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3377 if (temp & FDI_RX_BIT_LOCK) {
3378 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3379 DRM_DEBUG_KMS("FDI train 1 done.\n");
3380 break;
3381 }
3382 udelay(50);
8db9d77b 3383 }
fa37d39e
SP
3384 if (retry < 5)
3385 break;
8db9d77b
ZW
3386 }
3387 if (i == 4)
5eddb70b 3388 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3389
3390 /* Train 2 */
5eddb70b
CW
3391 reg = FDI_TX_CTL(pipe);
3392 temp = I915_READ(reg);
8db9d77b
ZW
3393 temp &= ~FDI_LINK_TRAIN_NONE;
3394 temp |= FDI_LINK_TRAIN_PATTERN_2;
3395 if (IS_GEN6(dev)) {
3396 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3397 /* SNB-B */
3398 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3399 }
5eddb70b 3400 I915_WRITE(reg, temp);
8db9d77b 3401
5eddb70b
CW
3402 reg = FDI_RX_CTL(pipe);
3403 temp = I915_READ(reg);
8db9d77b
ZW
3404 if (HAS_PCH_CPT(dev)) {
3405 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3406 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3407 } else {
3408 temp &= ~FDI_LINK_TRAIN_NONE;
3409 temp |= FDI_LINK_TRAIN_PATTERN_2;
3410 }
5eddb70b
CW
3411 I915_WRITE(reg, temp);
3412
3413 POSTING_READ(reg);
8db9d77b
ZW
3414 udelay(150);
3415
0206e353 3416 for (i = 0; i < 4; i++) {
5eddb70b
CW
3417 reg = FDI_TX_CTL(pipe);
3418 temp = I915_READ(reg);
8db9d77b
ZW
3419 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3420 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3421 I915_WRITE(reg, temp);
3422
3423 POSTING_READ(reg);
8db9d77b
ZW
3424 udelay(500);
3425
fa37d39e
SP
3426 for (retry = 0; retry < 5; retry++) {
3427 reg = FDI_RX_IIR(pipe);
3428 temp = I915_READ(reg);
3429 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3430 if (temp & FDI_RX_SYMBOL_LOCK) {
3431 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3432 DRM_DEBUG_KMS("FDI train 2 done.\n");
3433 break;
3434 }
3435 udelay(50);
8db9d77b 3436 }
fa37d39e
SP
3437 if (retry < 5)
3438 break;
8db9d77b
ZW
3439 }
3440 if (i == 4)
5eddb70b 3441 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3442
3443 DRM_DEBUG_KMS("FDI train done.\n");
3444}
3445
357555c0
JB
3446/* Manual link training for Ivy Bridge A0 parts */
3447static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3448{
3449 struct drm_device *dev = crtc->dev;
3450 struct drm_i915_private *dev_priv = dev->dev_private;
3451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3452 int pipe = intel_crtc->pipe;
139ccd3f 3453 u32 reg, temp, i, j;
357555c0
JB
3454
3455 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3456 for train result */
3457 reg = FDI_RX_IMR(pipe);
3458 temp = I915_READ(reg);
3459 temp &= ~FDI_RX_SYMBOL_LOCK;
3460 temp &= ~FDI_RX_BIT_LOCK;
3461 I915_WRITE(reg, temp);
3462
3463 POSTING_READ(reg);
3464 udelay(150);
3465
01a415fd
DV
3466 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3467 I915_READ(FDI_RX_IIR(pipe)));
3468
139ccd3f
JB
3469 /* Try each vswing and preemphasis setting twice before moving on */
3470 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3471 /* disable first in case we need to retry */
3472 reg = FDI_TX_CTL(pipe);
3473 temp = I915_READ(reg);
3474 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3475 temp &= ~FDI_TX_ENABLE;
3476 I915_WRITE(reg, temp);
357555c0 3477
139ccd3f
JB
3478 reg = FDI_RX_CTL(pipe);
3479 temp = I915_READ(reg);
3480 temp &= ~FDI_LINK_TRAIN_AUTO;
3481 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3482 temp &= ~FDI_RX_ENABLE;
3483 I915_WRITE(reg, temp);
357555c0 3484
139ccd3f 3485 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3486 reg = FDI_TX_CTL(pipe);
3487 temp = I915_READ(reg);
139ccd3f 3488 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3489 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3490 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3491 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3492 temp |= snb_b_fdi_train_param[j/2];
3493 temp |= FDI_COMPOSITE_SYNC;
3494 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3495
139ccd3f
JB
3496 I915_WRITE(FDI_RX_MISC(pipe),
3497 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3498
139ccd3f 3499 reg = FDI_RX_CTL(pipe);
357555c0 3500 temp = I915_READ(reg);
139ccd3f
JB
3501 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3502 temp |= FDI_COMPOSITE_SYNC;
3503 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3504
139ccd3f
JB
3505 POSTING_READ(reg);
3506 udelay(1); /* should be 0.5us */
357555c0 3507
139ccd3f
JB
3508 for (i = 0; i < 4; i++) {
3509 reg = FDI_RX_IIR(pipe);
3510 temp = I915_READ(reg);
3511 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3512
139ccd3f
JB
3513 if (temp & FDI_RX_BIT_LOCK ||
3514 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3515 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3516 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3517 i);
3518 break;
3519 }
3520 udelay(1); /* should be 0.5us */
3521 }
3522 if (i == 4) {
3523 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3524 continue;
3525 }
357555c0 3526
139ccd3f 3527 /* Train 2 */
357555c0
JB
3528 reg = FDI_TX_CTL(pipe);
3529 temp = I915_READ(reg);
139ccd3f
JB
3530 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3531 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3532 I915_WRITE(reg, temp);
3533
3534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3537 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3538 I915_WRITE(reg, temp);
3539
3540 POSTING_READ(reg);
139ccd3f 3541 udelay(2); /* should be 1.5us */
357555c0 3542
139ccd3f
JB
3543 for (i = 0; i < 4; i++) {
3544 reg = FDI_RX_IIR(pipe);
3545 temp = I915_READ(reg);
3546 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3547
139ccd3f
JB
3548 if (temp & FDI_RX_SYMBOL_LOCK ||
3549 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3550 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3551 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3552 i);
3553 goto train_done;
3554 }
3555 udelay(2); /* should be 1.5us */
357555c0 3556 }
139ccd3f
JB
3557 if (i == 4)
3558 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3559 }
357555c0 3560
139ccd3f 3561train_done:
357555c0
JB
3562 DRM_DEBUG_KMS("FDI train done.\n");
3563}
3564
88cefb6c 3565static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3566{
88cefb6c 3567 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3568 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3569 int pipe = intel_crtc->pipe;
5eddb70b 3570 u32 reg, temp;
79e53945 3571
c64e311e 3572
c98e9dcf 3573 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3574 reg = FDI_RX_CTL(pipe);
3575 temp = I915_READ(reg);
627eb5a3 3576 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3577 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3578 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3579 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3580
3581 POSTING_READ(reg);
c98e9dcf
JB
3582 udelay(200);
3583
3584 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3585 temp = I915_READ(reg);
3586 I915_WRITE(reg, temp | FDI_PCDCLK);
3587
3588 POSTING_READ(reg);
c98e9dcf
JB
3589 udelay(200);
3590
20749730
PZ
3591 /* Enable CPU FDI TX PLL, always on for Ironlake */
3592 reg = FDI_TX_CTL(pipe);
3593 temp = I915_READ(reg);
3594 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3595 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3596
20749730
PZ
3597 POSTING_READ(reg);
3598 udelay(100);
6be4a607 3599 }
0e23b99d
JB
3600}
3601
88cefb6c
DV
3602static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3603{
3604 struct drm_device *dev = intel_crtc->base.dev;
3605 struct drm_i915_private *dev_priv = dev->dev_private;
3606 int pipe = intel_crtc->pipe;
3607 u32 reg, temp;
3608
3609 /* Switch from PCDclk to Rawclk */
3610 reg = FDI_RX_CTL(pipe);
3611 temp = I915_READ(reg);
3612 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3613
3614 /* Disable CPU FDI TX PLL */
3615 reg = FDI_TX_CTL(pipe);
3616 temp = I915_READ(reg);
3617 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3618
3619 POSTING_READ(reg);
3620 udelay(100);
3621
3622 reg = FDI_RX_CTL(pipe);
3623 temp = I915_READ(reg);
3624 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3625
3626 /* Wait for the clocks to turn off. */
3627 POSTING_READ(reg);
3628 udelay(100);
3629}
3630
0fc932b8
JB
3631static void ironlake_fdi_disable(struct drm_crtc *crtc)
3632{
3633 struct drm_device *dev = crtc->dev;
3634 struct drm_i915_private *dev_priv = dev->dev_private;
3635 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3636 int pipe = intel_crtc->pipe;
3637 u32 reg, temp;
3638
3639 /* disable CPU FDI tx and PCH FDI rx */
3640 reg = FDI_TX_CTL(pipe);
3641 temp = I915_READ(reg);
3642 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3643 POSTING_READ(reg);
3644
3645 reg = FDI_RX_CTL(pipe);
3646 temp = I915_READ(reg);
3647 temp &= ~(0x7 << 16);
dfd07d72 3648 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3649 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3650
3651 POSTING_READ(reg);
3652 udelay(100);
3653
3654 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3655 if (HAS_PCH_IBX(dev))
6f06ce18 3656 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3657
3658 /* still set train pattern 1 */
3659 reg = FDI_TX_CTL(pipe);
3660 temp = I915_READ(reg);
3661 temp &= ~FDI_LINK_TRAIN_NONE;
3662 temp |= FDI_LINK_TRAIN_PATTERN_1;
3663 I915_WRITE(reg, temp);
3664
3665 reg = FDI_RX_CTL(pipe);
3666 temp = I915_READ(reg);
3667 if (HAS_PCH_CPT(dev)) {
3668 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3669 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3670 } else {
3671 temp &= ~FDI_LINK_TRAIN_NONE;
3672 temp |= FDI_LINK_TRAIN_PATTERN_1;
3673 }
3674 /* BPC in FDI rx is consistent with that in PIPECONF */
3675 temp &= ~(0x07 << 16);
dfd07d72 3676 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3677 I915_WRITE(reg, temp);
3678
3679 POSTING_READ(reg);
3680 udelay(100);
3681}
3682
5dce5b93
CW
3683bool intel_has_pending_fb_unpin(struct drm_device *dev)
3684{
3685 struct intel_crtc *crtc;
3686
3687 /* Note that we don't need to be called with mode_config.lock here
3688 * as our list of CRTC objects is static for the lifetime of the
3689 * device and so cannot disappear as we iterate. Similarly, we can
3690 * happily treat the predicates as racy, atomic checks as userspace
3691 * cannot claim and pin a new fb without at least acquring the
3692 * struct_mutex and so serialising with us.
3693 */
d3fcc808 3694 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3695 if (atomic_read(&crtc->unpin_work_count) == 0)
3696 continue;
3697
3698 if (crtc->unpin_work)
3699 intel_wait_for_vblank(dev, crtc->pipe);
3700
3701 return true;
3702 }
3703
3704 return false;
3705}
3706
d6bbafa1
CW
3707static void page_flip_completed(struct intel_crtc *intel_crtc)
3708{
3709 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3710 struct intel_unpin_work *work = intel_crtc->unpin_work;
3711
3712 /* ensure that the unpin work is consistent wrt ->pending. */
3713 smp_rmb();
3714 intel_crtc->unpin_work = NULL;
3715
3716 if (work->event)
3717 drm_send_vblank_event(intel_crtc->base.dev,
3718 intel_crtc->pipe,
3719 work->event);
3720
3721 drm_crtc_vblank_put(&intel_crtc->base);
3722
3723 wake_up_all(&dev_priv->pending_flip_queue);
3724 queue_work(dev_priv->wq, &work->work);
3725
3726 trace_i915_flip_complete(intel_crtc->plane,
3727 work->pending_flip_obj);
3728}
3729
46a55d30 3730void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3731{
0f91128d 3732 struct drm_device *dev = crtc->dev;
5bb61643 3733 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3734
2c10d571 3735 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3736 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3737 !intel_crtc_has_pending_flip(crtc),
3738 60*HZ) == 0)) {
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3740
5e2d7afc 3741 spin_lock_irq(&dev->event_lock);
9c787942
CW
3742 if (intel_crtc->unpin_work) {
3743 WARN_ONCE(1, "Removing stuck page flip\n");
3744 page_flip_completed(intel_crtc);
3745 }
5e2d7afc 3746 spin_unlock_irq(&dev->event_lock);
9c787942 3747 }
5bb61643 3748
975d568a
CW
3749 if (crtc->primary->fb) {
3750 mutex_lock(&dev->struct_mutex);
3751 intel_finish_fb(crtc->primary->fb);
3752 mutex_unlock(&dev->struct_mutex);
3753 }
e6c3a2a6
CW
3754}
3755
e615efe4
ED
3756/* Program iCLKIP clock to the desired frequency */
3757static void lpt_program_iclkip(struct drm_crtc *crtc)
3758{
3759 struct drm_device *dev = crtc->dev;
3760 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3761 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3762 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3763 u32 temp;
3764
09153000
DV
3765 mutex_lock(&dev_priv->dpio_lock);
3766
e615efe4
ED
3767 /* It is necessary to ungate the pixclk gate prior to programming
3768 * the divisors, and gate it back when it is done.
3769 */
3770 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3771
3772 /* Disable SSCCTL */
3773 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3774 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3775 SBI_SSCCTL_DISABLE,
3776 SBI_ICLK);
e615efe4
ED
3777
3778 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3779 if (clock == 20000) {
e615efe4
ED
3780 auxdiv = 1;
3781 divsel = 0x41;
3782 phaseinc = 0x20;
3783 } else {
3784 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3785 * but the adjusted_mode->crtc_clock in in KHz. To get the
3786 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3787 * convert the virtual clock precision to KHz here for higher
3788 * precision.
3789 */
3790 u32 iclk_virtual_root_freq = 172800 * 1000;
3791 u32 iclk_pi_range = 64;
3792 u32 desired_divisor, msb_divisor_value, pi_value;
3793
12d7ceed 3794 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3795 msb_divisor_value = desired_divisor / iclk_pi_range;
3796 pi_value = desired_divisor % iclk_pi_range;
3797
3798 auxdiv = 0;
3799 divsel = msb_divisor_value - 2;
3800 phaseinc = pi_value;
3801 }
3802
3803 /* This should not happen with any sane values */
3804 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3805 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3806 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3807 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3808
3809 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3810 clock,
e615efe4
ED
3811 auxdiv,
3812 divsel,
3813 phasedir,
3814 phaseinc);
3815
3816 /* Program SSCDIVINTPHASE6 */
988d6ee8 3817 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3818 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3819 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3820 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3821 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3822 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3823 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3824 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3825
3826 /* Program SSCAUXDIV */
988d6ee8 3827 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3828 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3829 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3830 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3831
3832 /* Enable modulator and associated divider */
988d6ee8 3833 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3834 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3835 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3836
3837 /* Wait for initialization time */
3838 udelay(24);
3839
3840 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3841
3842 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3843}
3844
275f01b2
DV
3845static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3846 enum pipe pch_transcoder)
3847{
3848 struct drm_device *dev = crtc->base.dev;
3849 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3850 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3851
3852 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3853 I915_READ(HTOTAL(cpu_transcoder)));
3854 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3855 I915_READ(HBLANK(cpu_transcoder)));
3856 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3857 I915_READ(HSYNC(cpu_transcoder)));
3858
3859 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3860 I915_READ(VTOTAL(cpu_transcoder)));
3861 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3862 I915_READ(VBLANK(cpu_transcoder)));
3863 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3864 I915_READ(VSYNC(cpu_transcoder)));
3865 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3866 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3867}
3868
003632d9 3869static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
3870{
3871 struct drm_i915_private *dev_priv = dev->dev_private;
3872 uint32_t temp;
3873
3874 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 3875 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
3876 return;
3877
3878 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3879 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3880
003632d9
ACO
3881 temp &= ~FDI_BC_BIFURCATION_SELECT;
3882 if (enable)
3883 temp |= FDI_BC_BIFURCATION_SELECT;
3884
3885 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
3886 I915_WRITE(SOUTH_CHICKEN1, temp);
3887 POSTING_READ(SOUTH_CHICKEN1);
3888}
3889
3890static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3891{
3892 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
3893
3894 switch (intel_crtc->pipe) {
3895 case PIPE_A:
3896 break;
3897 case PIPE_B:
6e3c9717 3898 if (intel_crtc->config->fdi_lanes > 2)
003632d9 3899 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 3900 else
003632d9 3901 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
3902
3903 break;
3904 case PIPE_C:
003632d9 3905 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
3906
3907 break;
3908 default:
3909 BUG();
3910 }
3911}
3912
f67a559d
JB
3913/*
3914 * Enable PCH resources required for PCH ports:
3915 * - PCH PLLs
3916 * - FDI training & RX/TX
3917 * - update transcoder timings
3918 * - DP transcoding bits
3919 * - transcoder
3920 */
3921static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3922{
3923 struct drm_device *dev = crtc->dev;
3924 struct drm_i915_private *dev_priv = dev->dev_private;
3925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3926 int pipe = intel_crtc->pipe;
ee7b9f93 3927 u32 reg, temp;
2c07245f 3928
ab9412ba 3929 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3930
1fbc0d78
DV
3931 if (IS_IVYBRIDGE(dev))
3932 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3933
cd986abb
DV
3934 /* Write the TU size bits before fdi link training, so that error
3935 * detection works. */
3936 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3937 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3938
c98e9dcf 3939 /* For PCH output, training FDI link */
674cf967 3940 dev_priv->display.fdi_link_train(crtc);
2c07245f 3941
3ad8a208
DV
3942 /* We need to program the right clock selection before writing the pixel
3943 * mutliplier into the DPLL. */
303b81e0 3944 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3945 u32 sel;
4b645f14 3946
c98e9dcf 3947 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3948 temp |= TRANS_DPLL_ENABLE(pipe);
3949 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 3950 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3951 temp |= sel;
3952 else
3953 temp &= ~sel;
c98e9dcf 3954 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3955 }
5eddb70b 3956
3ad8a208
DV
3957 /* XXX: pch pll's can be enabled any time before we enable the PCH
3958 * transcoder, and we actually should do this to not upset any PCH
3959 * transcoder that already use the clock when we share it.
3960 *
3961 * Note that enable_shared_dpll tries to do the right thing, but
3962 * get_shared_dpll unconditionally resets the pll - we need that to have
3963 * the right LVDS enable sequence. */
85b3894f 3964 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3965
d9b6cb56
JB
3966 /* set transcoder timing, panel must allow it */
3967 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3968 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3969
303b81e0 3970 intel_fdi_normal_train(crtc);
5e84e1a4 3971
c98e9dcf 3972 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 3973 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 3974 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3975 reg = TRANS_DP_CTL(pipe);
3976 temp = I915_READ(reg);
3977 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3978 TRANS_DP_SYNC_MASK |
3979 TRANS_DP_BPC_MASK);
5eddb70b
CW
3980 temp |= (TRANS_DP_OUTPUT_ENABLE |
3981 TRANS_DP_ENH_FRAMING);
9325c9f0 3982 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3983
3984 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3985 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3986 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3987 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3988
3989 switch (intel_trans_dp_port_sel(crtc)) {
3990 case PCH_DP_B:
5eddb70b 3991 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3992 break;
3993 case PCH_DP_C:
5eddb70b 3994 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3995 break;
3996 case PCH_DP_D:
5eddb70b 3997 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3998 break;
3999 default:
e95d41e1 4000 BUG();
32f9d658 4001 }
2c07245f 4002
5eddb70b 4003 I915_WRITE(reg, temp);
6be4a607 4004 }
b52eb4dc 4005
b8a4f404 4006 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4007}
4008
1507e5bd
PZ
4009static void lpt_pch_enable(struct drm_crtc *crtc)
4010{
4011 struct drm_device *dev = crtc->dev;
4012 struct drm_i915_private *dev_priv = dev->dev_private;
4013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4014 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4015
ab9412ba 4016 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4017
8c52b5e8 4018 lpt_program_iclkip(crtc);
1507e5bd 4019
0540e488 4020 /* Set transcoder timing. */
275f01b2 4021 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4022
937bb610 4023 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4024}
4025
716c2e55 4026void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 4027{
e2b78267 4028 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
4029
4030 if (pll == NULL)
4031 return;
4032
3e369b76 4033 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 4034 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
4035 return;
4036 }
4037
3e369b76
ACO
4038 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4039 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
4040 WARN_ON(pll->on);
4041 WARN_ON(pll->active);
4042 }
4043
6e3c9717 4044 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
4045}
4046
190f68c5
ACO
4047struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4048 struct intel_crtc_state *crtc_state)
ee7b9f93 4049{
e2b78267 4050 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4051 struct intel_shared_dpll *pll;
e2b78267 4052 enum intel_dpll_id i;
ee7b9f93 4053
98b6bd99
DV
4054 if (HAS_PCH_IBX(dev_priv->dev)) {
4055 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4056 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4057 pll = &dev_priv->shared_dplls[i];
98b6bd99 4058
46edb027
DV
4059 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4060 crtc->base.base.id, pll->name);
98b6bd99 4061
8bd31e67 4062 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 4063
98b6bd99
DV
4064 goto found;
4065 }
4066
e72f9fbf
DV
4067 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4068 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4069
4070 /* Only want to check enabled timings first */
8bd31e67 4071 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
4072 continue;
4073
190f68c5 4074 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
4075 &pll->new_config->hw_state,
4076 sizeof(pll->new_config->hw_state)) == 0) {
4077 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4078 crtc->base.base.id, pll->name,
8bd31e67
ACO
4079 pll->new_config->crtc_mask,
4080 pll->active);
ee7b9f93
JB
4081 goto found;
4082 }
4083 }
4084
4085 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4086 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4087 pll = &dev_priv->shared_dplls[i];
8bd31e67 4088 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
4089 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4090 crtc->base.base.id, pll->name);
ee7b9f93
JB
4091 goto found;
4092 }
4093 }
4094
4095 return NULL;
4096
4097found:
8bd31e67 4098 if (pll->new_config->crtc_mask == 0)
190f68c5 4099 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 4100
190f68c5 4101 crtc_state->shared_dpll = i;
46edb027
DV
4102 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4103 pipe_name(crtc->pipe));
ee7b9f93 4104
8bd31e67 4105 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 4106
ee7b9f93
JB
4107 return pll;
4108}
4109
8bd31e67
ACO
4110/**
4111 * intel_shared_dpll_start_config - start a new PLL staged config
4112 * @dev_priv: DRM device
4113 * @clear_pipes: mask of pipes that will have their PLLs freed
4114 *
4115 * Starts a new PLL staged config, copying the current config but
4116 * releasing the references of pipes specified in clear_pipes.
4117 */
4118static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4119 unsigned clear_pipes)
4120{
4121 struct intel_shared_dpll *pll;
4122 enum intel_dpll_id i;
4123
4124 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4125 pll = &dev_priv->shared_dplls[i];
4126
4127 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4128 GFP_KERNEL);
4129 if (!pll->new_config)
4130 goto cleanup;
4131
4132 pll->new_config->crtc_mask &= ~clear_pipes;
4133 }
4134
4135 return 0;
4136
4137cleanup:
4138 while (--i >= 0) {
4139 pll = &dev_priv->shared_dplls[i];
f354d733 4140 kfree(pll->new_config);
8bd31e67
ACO
4141 pll->new_config = NULL;
4142 }
4143
4144 return -ENOMEM;
4145}
4146
4147static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4148{
4149 struct intel_shared_dpll *pll;
4150 enum intel_dpll_id i;
4151
4152 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4153 pll = &dev_priv->shared_dplls[i];
4154
4155 WARN_ON(pll->new_config == &pll->config);
4156
4157 pll->config = *pll->new_config;
4158 kfree(pll->new_config);
4159 pll->new_config = NULL;
4160 }
4161}
4162
4163static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4164{
4165 struct intel_shared_dpll *pll;
4166 enum intel_dpll_id i;
4167
4168 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4169 pll = &dev_priv->shared_dplls[i];
4170
4171 WARN_ON(pll->new_config == &pll->config);
4172
4173 kfree(pll->new_config);
4174 pll->new_config = NULL;
4175 }
4176}
4177
a1520318 4178static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4179{
4180 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4181 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4182 u32 temp;
4183
4184 temp = I915_READ(dslreg);
4185 udelay(500);
4186 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4187 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4188 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4189 }
4190}
4191
bd2e244f
JB
4192static void skylake_pfit_enable(struct intel_crtc *crtc)
4193{
4194 struct drm_device *dev = crtc->base.dev;
4195 struct drm_i915_private *dev_priv = dev->dev_private;
4196 int pipe = crtc->pipe;
4197
6e3c9717 4198 if (crtc->config->pch_pfit.enabled) {
bd2e244f 4199 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
6e3c9717
ACO
4200 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4201 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
bd2e244f
JB
4202 }
4203}
4204
b074cec8
JB
4205static void ironlake_pfit_enable(struct intel_crtc *crtc)
4206{
4207 struct drm_device *dev = crtc->base.dev;
4208 struct drm_i915_private *dev_priv = dev->dev_private;
4209 int pipe = crtc->pipe;
4210
6e3c9717 4211 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4212 /* Force use of hard-coded filter coefficients
4213 * as some pre-programmed values are broken,
4214 * e.g. x201.
4215 */
4216 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4217 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4218 PF_PIPE_SEL_IVB(pipe));
4219 else
4220 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4221 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4222 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4223 }
4224}
4225
4a3b8769 4226static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4227{
4228 struct drm_device *dev = crtc->dev;
4229 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4230 struct drm_plane *plane;
bb53d4ae
VS
4231 struct intel_plane *intel_plane;
4232
af2b653b
MR
4233 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4234 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4235 if (intel_plane->pipe == pipe)
4236 intel_plane_restore(&intel_plane->base);
af2b653b 4237 }
bb53d4ae
VS
4238}
4239
0d703d4e
MR
4240/*
4241 * Disable a plane internally without actually modifying the plane's state.
4242 * This will allow us to easily restore the plane later by just reprogramming
4243 * its state.
4244 */
4245static void disable_plane_internal(struct drm_plane *plane)
4246{
4247 struct intel_plane *intel_plane = to_intel_plane(plane);
4248 struct drm_plane_state *state =
4249 plane->funcs->atomic_duplicate_state(plane);
4250 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4251
4252 intel_state->visible = false;
4253 intel_plane->commit_plane(plane, intel_state);
4254
4255 intel_plane_destroy_state(plane, state);
4256}
4257
4a3b8769 4258static void intel_disable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4259{
4260 struct drm_device *dev = crtc->dev;
4261 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4262 struct drm_plane *plane;
bb53d4ae
VS
4263 struct intel_plane *intel_plane;
4264
af2b653b
MR
4265 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4266 intel_plane = to_intel_plane(plane);
0d703d4e
MR
4267 if (plane->fb && intel_plane->pipe == pipe)
4268 disable_plane_internal(plane);
af2b653b 4269 }
bb53d4ae
VS
4270}
4271
20bc8673 4272void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4273{
cea165c3
VS
4274 struct drm_device *dev = crtc->base.dev;
4275 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4276
6e3c9717 4277 if (!crtc->config->ips_enabled)
d77e4531
PZ
4278 return;
4279
cea165c3
VS
4280 /* We can only enable IPS after we enable a plane and wait for a vblank */
4281 intel_wait_for_vblank(dev, crtc->pipe);
4282
d77e4531 4283 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4284 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4285 mutex_lock(&dev_priv->rps.hw_lock);
4286 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4287 mutex_unlock(&dev_priv->rps.hw_lock);
4288 /* Quoting Art Runyan: "its not safe to expect any particular
4289 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4290 * mailbox." Moreover, the mailbox may return a bogus state,
4291 * so we need to just enable it and continue on.
2a114cc1
BW
4292 */
4293 } else {
4294 I915_WRITE(IPS_CTL, IPS_ENABLE);
4295 /* The bit only becomes 1 in the next vblank, so this wait here
4296 * is essentially intel_wait_for_vblank. If we don't have this
4297 * and don't wait for vblanks until the end of crtc_enable, then
4298 * the HW state readout code will complain that the expected
4299 * IPS_CTL value is not the one we read. */
4300 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4301 DRM_ERROR("Timed out waiting for IPS enable\n");
4302 }
d77e4531
PZ
4303}
4304
20bc8673 4305void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4306{
4307 struct drm_device *dev = crtc->base.dev;
4308 struct drm_i915_private *dev_priv = dev->dev_private;
4309
6e3c9717 4310 if (!crtc->config->ips_enabled)
d77e4531
PZ
4311 return;
4312
4313 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4314 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4315 mutex_lock(&dev_priv->rps.hw_lock);
4316 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4317 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4318 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4319 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4320 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4321 } else {
2a114cc1 4322 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4323 POSTING_READ(IPS_CTL);
4324 }
d77e4531
PZ
4325
4326 /* We need to wait for a vblank before we can disable the plane. */
4327 intel_wait_for_vblank(dev, crtc->pipe);
4328}
4329
4330/** Loads the palette/gamma unit for the CRTC with the prepared values */
4331static void intel_crtc_load_lut(struct drm_crtc *crtc)
4332{
4333 struct drm_device *dev = crtc->dev;
4334 struct drm_i915_private *dev_priv = dev->dev_private;
4335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4336 enum pipe pipe = intel_crtc->pipe;
4337 int palreg = PALETTE(pipe);
4338 int i;
4339 bool reenable_ips = false;
4340
4341 /* The clocks have to be on to load the palette. */
83d65738 4342 if (!crtc->state->enable || !intel_crtc->active)
d77e4531
PZ
4343 return;
4344
4345 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
409ee761 4346 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4347 assert_dsi_pll_enabled(dev_priv);
4348 else
4349 assert_pll_enabled(dev_priv, pipe);
4350 }
4351
4352 /* use legacy palette for Ironlake */
7a1db49a 4353 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4354 palreg = LGC_PALETTE(pipe);
4355
4356 /* Workaround : Do not read or write the pipe palette/gamma data while
4357 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4358 */
6e3c9717 4359 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4360 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4361 GAMMA_MODE_MODE_SPLIT)) {
4362 hsw_disable_ips(intel_crtc);
4363 reenable_ips = true;
4364 }
4365
4366 for (i = 0; i < 256; i++) {
4367 I915_WRITE(palreg + 4 * i,
4368 (intel_crtc->lut_r[i] << 16) |
4369 (intel_crtc->lut_g[i] << 8) |
4370 intel_crtc->lut_b[i]);
4371 }
4372
4373 if (reenable_ips)
4374 hsw_enable_ips(intel_crtc);
4375}
4376
d3eedb1a
VS
4377static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4378{
4379 if (!enable && intel_crtc->overlay) {
4380 struct drm_device *dev = intel_crtc->base.dev;
4381 struct drm_i915_private *dev_priv = dev->dev_private;
4382
4383 mutex_lock(&dev->struct_mutex);
4384 dev_priv->mm.interruptible = false;
4385 (void) intel_overlay_switch_off(intel_crtc->overlay);
4386 dev_priv->mm.interruptible = true;
4387 mutex_unlock(&dev->struct_mutex);
4388 }
4389
4390 /* Let userspace switch the overlay on again. In most cases userspace
4391 * has to recompute where to put it anyway.
4392 */
4393}
4394
d3eedb1a 4395static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4396{
4397 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4399 int pipe = intel_crtc->pipe;
a5c4d7bc 4400
fdd508a6 4401 intel_enable_primary_hw_plane(crtc->primary, crtc);
4a3b8769 4402 intel_enable_sprite_planes(crtc);
a5c4d7bc 4403 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4404 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4405
4406 hsw_enable_ips(intel_crtc);
4407
4408 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4409 intel_fbc_update(dev);
a5c4d7bc 4410 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4411
4412 /*
4413 * FIXME: Once we grow proper nuclear flip support out of this we need
4414 * to compute the mask of flip planes precisely. For the time being
4415 * consider this a flip from a NULL plane.
4416 */
4417 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4418}
4419
d3eedb1a 4420static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4421{
4422 struct drm_device *dev = crtc->dev;
4423 struct drm_i915_private *dev_priv = dev->dev_private;
4424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4425 int pipe = intel_crtc->pipe;
a5c4d7bc
VS
4426
4427 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc 4428
e35fef21 4429 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4430 intel_fbc_disable(dev);
a5c4d7bc
VS
4431
4432 hsw_disable_ips(intel_crtc);
4433
d3eedb1a 4434 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc 4435 intel_crtc_update_cursor(crtc, false);
4a3b8769 4436 intel_disable_sprite_planes(crtc);
fdd508a6 4437 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4438
f99d7069
DV
4439 /*
4440 * FIXME: Once we grow proper nuclear flip support out of this we need
4441 * to compute the mask of flip planes precisely. For the time being
4442 * consider this a flip to a NULL plane.
4443 */
4444 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4445}
4446
f67a559d
JB
4447static void ironlake_crtc_enable(struct drm_crtc *crtc)
4448{
4449 struct drm_device *dev = crtc->dev;
4450 struct drm_i915_private *dev_priv = dev->dev_private;
4451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4452 struct intel_encoder *encoder;
f67a559d 4453 int pipe = intel_crtc->pipe;
f67a559d 4454
83d65738 4455 WARN_ON(!crtc->state->enable);
08a48469 4456
f67a559d
JB
4457 if (intel_crtc->active)
4458 return;
4459
6e3c9717 4460 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4461 intel_prepare_shared_dpll(intel_crtc);
4462
6e3c9717 4463 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4464 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4465
4466 intel_set_pipe_timings(intel_crtc);
4467
6e3c9717 4468 if (intel_crtc->config->has_pch_encoder) {
29407aab 4469 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4470 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4471 }
4472
4473 ironlake_set_pipeconf(crtc);
4474
f67a559d 4475 intel_crtc->active = true;
8664281b 4476
a72e4c9f
DV
4477 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4478 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4479
f6736a1a 4480 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4481 if (encoder->pre_enable)
4482 encoder->pre_enable(encoder);
f67a559d 4483
6e3c9717 4484 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4485 /* Note: FDI PLL enabling _must_ be done before we enable the
4486 * cpu pipes, hence this is separate from all the other fdi/pch
4487 * enabling. */
88cefb6c 4488 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4489 } else {
4490 assert_fdi_tx_disabled(dev_priv, pipe);
4491 assert_fdi_rx_disabled(dev_priv, pipe);
4492 }
f67a559d 4493
b074cec8 4494 ironlake_pfit_enable(intel_crtc);
f67a559d 4495
9c54c0dd
JB
4496 /*
4497 * On ILK+ LUT must be loaded before the pipe is running but with
4498 * clocks enabled
4499 */
4500 intel_crtc_load_lut(crtc);
4501
f37fcc2a 4502 intel_update_watermarks(crtc);
e1fdc473 4503 intel_enable_pipe(intel_crtc);
f67a559d 4504
6e3c9717 4505 if (intel_crtc->config->has_pch_encoder)
f67a559d 4506 ironlake_pch_enable(crtc);
c98e9dcf 4507
f9b61ff6
DV
4508 assert_vblank_disabled(crtc);
4509 drm_crtc_vblank_on(crtc);
4510
fa5c73b1
DV
4511 for_each_encoder_on_crtc(dev, crtc, encoder)
4512 encoder->enable(encoder);
61b77ddd
DV
4513
4514 if (HAS_PCH_CPT(dev))
a1520318 4515 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4516
d3eedb1a 4517 intel_crtc_enable_planes(crtc);
6be4a607
JB
4518}
4519
42db64ef
PZ
4520/* IPS only exists on ULT machines and is tied to pipe A. */
4521static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4522{
f5adf94e 4523 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4524}
4525
e4916946
PZ
4526/*
4527 * This implements the workaround described in the "notes" section of the mode
4528 * set sequence documentation. When going from no pipes or single pipe to
4529 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4530 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4531 */
4532static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4533{
4534 struct drm_device *dev = crtc->base.dev;
4535 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4536
4537 /* We want to get the other_active_crtc only if there's only 1 other
4538 * active crtc. */
d3fcc808 4539 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4540 if (!crtc_it->active || crtc_it == crtc)
4541 continue;
4542
4543 if (other_active_crtc)
4544 return;
4545
4546 other_active_crtc = crtc_it;
4547 }
4548 if (!other_active_crtc)
4549 return;
4550
4551 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4552 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4553}
4554
4f771f10
PZ
4555static void haswell_crtc_enable(struct drm_crtc *crtc)
4556{
4557 struct drm_device *dev = crtc->dev;
4558 struct drm_i915_private *dev_priv = dev->dev_private;
4559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4560 struct intel_encoder *encoder;
4561 int pipe = intel_crtc->pipe;
4f771f10 4562
83d65738 4563 WARN_ON(!crtc->state->enable);
4f771f10
PZ
4564
4565 if (intel_crtc->active)
4566 return;
4567
df8ad70c
DV
4568 if (intel_crtc_to_shared_dpll(intel_crtc))
4569 intel_enable_shared_dpll(intel_crtc);
4570
6e3c9717 4571 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4572 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4573
4574 intel_set_pipe_timings(intel_crtc);
4575
6e3c9717
ACO
4576 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4577 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4578 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4579 }
4580
6e3c9717 4581 if (intel_crtc->config->has_pch_encoder) {
229fca97 4582 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4583 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4584 }
4585
4586 haswell_set_pipeconf(crtc);
4587
4588 intel_set_pipe_csc(crtc);
4589
4f771f10 4590 intel_crtc->active = true;
8664281b 4591
a72e4c9f 4592 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4593 for_each_encoder_on_crtc(dev, crtc, encoder)
4594 if (encoder->pre_enable)
4595 encoder->pre_enable(encoder);
4596
6e3c9717 4597 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4598 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4599 true);
4fe9467d
ID
4600 dev_priv->display.fdi_link_train(crtc);
4601 }
4602
1f544388 4603 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4604
bd2e244f
JB
4605 if (IS_SKYLAKE(dev))
4606 skylake_pfit_enable(intel_crtc);
4607 else
4608 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4609
4610 /*
4611 * On ILK+ LUT must be loaded before the pipe is running but with
4612 * clocks enabled
4613 */
4614 intel_crtc_load_lut(crtc);
4615
1f544388 4616 intel_ddi_set_pipe_settings(crtc);
8228c251 4617 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4618
f37fcc2a 4619 intel_update_watermarks(crtc);
e1fdc473 4620 intel_enable_pipe(intel_crtc);
42db64ef 4621
6e3c9717 4622 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4623 lpt_pch_enable(crtc);
4f771f10 4624
6e3c9717 4625 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4626 intel_ddi_set_vc_payload_alloc(crtc, true);
4627
f9b61ff6
DV
4628 assert_vblank_disabled(crtc);
4629 drm_crtc_vblank_on(crtc);
4630
8807e55b 4631 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4632 encoder->enable(encoder);
8807e55b
JN
4633 intel_opregion_notify_encoder(encoder, true);
4634 }
4f771f10 4635
e4916946
PZ
4636 /* If we change the relative order between pipe/planes enabling, we need
4637 * to change the workaround. */
4638 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4639 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4640}
4641
bd2e244f
JB
4642static void skylake_pfit_disable(struct intel_crtc *crtc)
4643{
4644 struct drm_device *dev = crtc->base.dev;
4645 struct drm_i915_private *dev_priv = dev->dev_private;
4646 int pipe = crtc->pipe;
4647
4648 /* To avoid upsetting the power well on haswell only disable the pfit if
4649 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4650 if (crtc->config->pch_pfit.enabled) {
bd2e244f
JB
4651 I915_WRITE(PS_CTL(pipe), 0);
4652 I915_WRITE(PS_WIN_POS(pipe), 0);
4653 I915_WRITE(PS_WIN_SZ(pipe), 0);
4654 }
4655}
4656
3f8dce3a
DV
4657static void ironlake_pfit_disable(struct intel_crtc *crtc)
4658{
4659 struct drm_device *dev = crtc->base.dev;
4660 struct drm_i915_private *dev_priv = dev->dev_private;
4661 int pipe = crtc->pipe;
4662
4663 /* To avoid upsetting the power well on haswell only disable the pfit if
4664 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4665 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4666 I915_WRITE(PF_CTL(pipe), 0);
4667 I915_WRITE(PF_WIN_POS(pipe), 0);
4668 I915_WRITE(PF_WIN_SZ(pipe), 0);
4669 }
4670}
4671
6be4a607
JB
4672static void ironlake_crtc_disable(struct drm_crtc *crtc)
4673{
4674 struct drm_device *dev = crtc->dev;
4675 struct drm_i915_private *dev_priv = dev->dev_private;
4676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4677 struct intel_encoder *encoder;
6be4a607 4678 int pipe = intel_crtc->pipe;
5eddb70b 4679 u32 reg, temp;
b52eb4dc 4680
f7abfe8b
CW
4681 if (!intel_crtc->active)
4682 return;
4683
d3eedb1a 4684 intel_crtc_disable_planes(crtc);
a5c4d7bc 4685
ea9d758d
DV
4686 for_each_encoder_on_crtc(dev, crtc, encoder)
4687 encoder->disable(encoder);
4688
f9b61ff6
DV
4689 drm_crtc_vblank_off(crtc);
4690 assert_vblank_disabled(crtc);
4691
6e3c9717 4692 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 4693 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4694
575f7ab7 4695 intel_disable_pipe(intel_crtc);
32f9d658 4696
3f8dce3a 4697 ironlake_pfit_disable(intel_crtc);
2c07245f 4698
bf49ec8c
DV
4699 for_each_encoder_on_crtc(dev, crtc, encoder)
4700 if (encoder->post_disable)
4701 encoder->post_disable(encoder);
2c07245f 4702
6e3c9717 4703 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4704 ironlake_fdi_disable(crtc);
913d8d11 4705
d925c59a 4706 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4707
d925c59a
DV
4708 if (HAS_PCH_CPT(dev)) {
4709 /* disable TRANS_DP_CTL */
4710 reg = TRANS_DP_CTL(pipe);
4711 temp = I915_READ(reg);
4712 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4713 TRANS_DP_PORT_SEL_MASK);
4714 temp |= TRANS_DP_PORT_SEL_NONE;
4715 I915_WRITE(reg, temp);
4716
4717 /* disable DPLL_SEL */
4718 temp = I915_READ(PCH_DPLL_SEL);
11887397 4719 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4720 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4721 }
e3421a18 4722
d925c59a 4723 /* disable PCH DPLL */
e72f9fbf 4724 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4725
d925c59a
DV
4726 ironlake_fdi_pll_disable(intel_crtc);
4727 }
6b383a7f 4728
f7abfe8b 4729 intel_crtc->active = false;
46ba614c 4730 intel_update_watermarks(crtc);
d1ebd816
BW
4731
4732 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4733 intel_fbc_update(dev);
d1ebd816 4734 mutex_unlock(&dev->struct_mutex);
6be4a607 4735}
1b3c7a47 4736
4f771f10 4737static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4738{
4f771f10
PZ
4739 struct drm_device *dev = crtc->dev;
4740 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4742 struct intel_encoder *encoder;
6e3c9717 4743 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 4744
4f771f10
PZ
4745 if (!intel_crtc->active)
4746 return;
4747
d3eedb1a 4748 intel_crtc_disable_planes(crtc);
dda9a66a 4749
8807e55b
JN
4750 for_each_encoder_on_crtc(dev, crtc, encoder) {
4751 intel_opregion_notify_encoder(encoder, false);
4f771f10 4752 encoder->disable(encoder);
8807e55b 4753 }
4f771f10 4754
f9b61ff6
DV
4755 drm_crtc_vblank_off(crtc);
4756 assert_vblank_disabled(crtc);
4757
6e3c9717 4758 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
4759 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4760 false);
575f7ab7 4761 intel_disable_pipe(intel_crtc);
4f771f10 4762
6e3c9717 4763 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
4764 intel_ddi_set_vc_payload_alloc(crtc, false);
4765
ad80a810 4766 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4767
bd2e244f
JB
4768 if (IS_SKYLAKE(dev))
4769 skylake_pfit_disable(intel_crtc);
4770 else
4771 ironlake_pfit_disable(intel_crtc);
4f771f10 4772
1f544388 4773 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4774
6e3c9717 4775 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 4776 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 4777 intel_ddi_fdi_disable(crtc);
83616634 4778 }
4f771f10 4779
97b040aa
ID
4780 for_each_encoder_on_crtc(dev, crtc, encoder)
4781 if (encoder->post_disable)
4782 encoder->post_disable(encoder);
4783
4f771f10 4784 intel_crtc->active = false;
46ba614c 4785 intel_update_watermarks(crtc);
4f771f10
PZ
4786
4787 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4788 intel_fbc_update(dev);
4f771f10 4789 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4790
4791 if (intel_crtc_to_shared_dpll(intel_crtc))
4792 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4793}
4794
ee7b9f93
JB
4795static void ironlake_crtc_off(struct drm_crtc *crtc)
4796{
4797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4798 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4799}
4800
6441ab5f 4801
2dd24552
JB
4802static void i9xx_pfit_enable(struct intel_crtc *crtc)
4803{
4804 struct drm_device *dev = crtc->base.dev;
4805 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4806 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 4807
681a8504 4808 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
4809 return;
4810
2dd24552 4811 /*
c0b03411
DV
4812 * The panel fitter should only be adjusted whilst the pipe is disabled,
4813 * according to register description and PRM.
2dd24552 4814 */
c0b03411
DV
4815 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4816 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4817
b074cec8
JB
4818 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4819 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4820
4821 /* Border color in case we don't scale up to the full screen. Black by
4822 * default, change to something else for debugging. */
4823 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4824}
4825
d05410f9
DA
4826static enum intel_display_power_domain port_to_power_domain(enum port port)
4827{
4828 switch (port) {
4829 case PORT_A:
4830 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4831 case PORT_B:
4832 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4833 case PORT_C:
4834 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4835 case PORT_D:
4836 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4837 default:
4838 WARN_ON_ONCE(1);
4839 return POWER_DOMAIN_PORT_OTHER;
4840 }
4841}
4842
77d22dca
ID
4843#define for_each_power_domain(domain, mask) \
4844 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4845 if ((1 << (domain)) & (mask))
4846
319be8ae
ID
4847enum intel_display_power_domain
4848intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4849{
4850 struct drm_device *dev = intel_encoder->base.dev;
4851 struct intel_digital_port *intel_dig_port;
4852
4853 switch (intel_encoder->type) {
4854 case INTEL_OUTPUT_UNKNOWN:
4855 /* Only DDI platforms should ever use this output type */
4856 WARN_ON_ONCE(!HAS_DDI(dev));
4857 case INTEL_OUTPUT_DISPLAYPORT:
4858 case INTEL_OUTPUT_HDMI:
4859 case INTEL_OUTPUT_EDP:
4860 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4861 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4862 case INTEL_OUTPUT_DP_MST:
4863 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4864 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4865 case INTEL_OUTPUT_ANALOG:
4866 return POWER_DOMAIN_PORT_CRT;
4867 case INTEL_OUTPUT_DSI:
4868 return POWER_DOMAIN_PORT_DSI;
4869 default:
4870 return POWER_DOMAIN_PORT_OTHER;
4871 }
4872}
4873
4874static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4875{
319be8ae
ID
4876 struct drm_device *dev = crtc->dev;
4877 struct intel_encoder *intel_encoder;
4878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4879 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4880 unsigned long mask;
4881 enum transcoder transcoder;
4882
4883 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4884
4885 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4886 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
4887 if (intel_crtc->config->pch_pfit.enabled ||
4888 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
4889 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4890
319be8ae
ID
4891 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4892 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4893
77d22dca
ID
4894 return mask;
4895}
4896
77d22dca
ID
4897static void modeset_update_crtc_power_domains(struct drm_device *dev)
4898{
4899 struct drm_i915_private *dev_priv = dev->dev_private;
4900 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4901 struct intel_crtc *crtc;
4902
4903 /*
4904 * First get all needed power domains, then put all unneeded, to avoid
4905 * any unnecessary toggling of the power wells.
4906 */
d3fcc808 4907 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4908 enum intel_display_power_domain domain;
4909
83d65738 4910 if (!crtc->base.state->enable)
77d22dca
ID
4911 continue;
4912
319be8ae 4913 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4914
4915 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4916 intel_display_power_get(dev_priv, domain);
4917 }
4918
50f6e502
VS
4919 if (dev_priv->display.modeset_global_resources)
4920 dev_priv->display.modeset_global_resources(dev);
4921
d3fcc808 4922 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4923 enum intel_display_power_domain domain;
4924
4925 for_each_power_domain(domain, crtc->enabled_power_domains)
4926 intel_display_power_put(dev_priv, domain);
4927
4928 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4929 }
4930
4931 intel_display_set_init_power(dev_priv, false);
4932}
4933
dfcab17e 4934/* returns HPLL frequency in kHz */
f8bf63fd 4935static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4936{
586f49dc 4937 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4938
586f49dc
JB
4939 /* Obtain SKU information */
4940 mutex_lock(&dev_priv->dpio_lock);
4941 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4942 CCK_FUSE_HPLL_FREQ_MASK;
4943 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4944
dfcab17e 4945 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4946}
4947
f8bf63fd
VS
4948static void vlv_update_cdclk(struct drm_device *dev)
4949{
4950 struct drm_i915_private *dev_priv = dev->dev_private;
4951
4952 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 4953 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
f8bf63fd
VS
4954 dev_priv->vlv_cdclk_freq);
4955
4956 /*
4957 * Program the gmbus_freq based on the cdclk frequency.
4958 * BSpec erroneously claims we should aim for 4MHz, but
4959 * in fact 1MHz is the correct frequency.
4960 */
6be1e3d3 4961 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
f8bf63fd
VS
4962}
4963
30a970c6
JB
4964/* Adjust CDclk dividers to allow high res or save power if possible */
4965static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4966{
4967 struct drm_i915_private *dev_priv = dev->dev_private;
4968 u32 val, cmd;
4969
d197b7d3 4970 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4971
dfcab17e 4972 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4973 cmd = 2;
dfcab17e 4974 else if (cdclk == 266667)
30a970c6
JB
4975 cmd = 1;
4976 else
4977 cmd = 0;
4978
4979 mutex_lock(&dev_priv->rps.hw_lock);
4980 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4981 val &= ~DSPFREQGUAR_MASK;
4982 val |= (cmd << DSPFREQGUAR_SHIFT);
4983 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4984 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4985 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4986 50)) {
4987 DRM_ERROR("timed out waiting for CDclk change\n");
4988 }
4989 mutex_unlock(&dev_priv->rps.hw_lock);
4990
dfcab17e 4991 if (cdclk == 400000) {
6bcda4f0 4992 u32 divider;
30a970c6 4993
6bcda4f0 4994 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
4995
4996 mutex_lock(&dev_priv->dpio_lock);
4997 /* adjust cdclk divider */
4998 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4999 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5000 val |= divider;
5001 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5002
5003 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5004 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5005 50))
5006 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5007 mutex_unlock(&dev_priv->dpio_lock);
5008 }
5009
5010 mutex_lock(&dev_priv->dpio_lock);
5011 /* adjust self-refresh exit latency value */
5012 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5013 val &= ~0x7f;
5014
5015 /*
5016 * For high bandwidth configs, we set a higher latency in the bunit
5017 * so that the core display fetch happens in time to avoid underruns.
5018 */
dfcab17e 5019 if (cdclk == 400000)
30a970c6
JB
5020 val |= 4500 / 250; /* 4.5 usec */
5021 else
5022 val |= 3000 / 250; /* 3.0 usec */
5023 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5024 mutex_unlock(&dev_priv->dpio_lock);
5025
f8bf63fd 5026 vlv_update_cdclk(dev);
30a970c6
JB
5027}
5028
383c5a6a
VS
5029static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5030{
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 u32 val, cmd;
5033
5034 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5035
5036 switch (cdclk) {
383c5a6a
VS
5037 case 333333:
5038 case 320000:
383c5a6a 5039 case 266667:
383c5a6a 5040 case 200000:
383c5a6a
VS
5041 break;
5042 default:
5f77eeb0 5043 MISSING_CASE(cdclk);
383c5a6a
VS
5044 return;
5045 }
5046
9d0d3fda
VS
5047 /*
5048 * Specs are full of misinformation, but testing on actual
5049 * hardware has shown that we just need to write the desired
5050 * CCK divider into the Punit register.
5051 */
5052 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5053
383c5a6a
VS
5054 mutex_lock(&dev_priv->rps.hw_lock);
5055 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5056 val &= ~DSPFREQGUAR_MASK_CHV;
5057 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5058 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5059 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5060 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5061 50)) {
5062 DRM_ERROR("timed out waiting for CDclk change\n");
5063 }
5064 mutex_unlock(&dev_priv->rps.hw_lock);
5065
5066 vlv_update_cdclk(dev);
5067}
5068
30a970c6
JB
5069static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5070 int max_pixclk)
5071{
6bcda4f0 5072 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5073 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5074
30a970c6
JB
5075 /*
5076 * Really only a few cases to deal with, as only 4 CDclks are supported:
5077 * 200MHz
5078 * 267MHz
29dc7ef3 5079 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5080 * 400MHz (VLV only)
5081 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5082 * of the lower bin and adjust if needed.
e37c67a1
VS
5083 *
5084 * We seem to get an unstable or solid color picture at 200MHz.
5085 * Not sure what's wrong. For now use 200MHz only when all pipes
5086 * are off.
30a970c6 5087 */
6cca3195
VS
5088 if (!IS_CHERRYVIEW(dev_priv) &&
5089 max_pixclk > freq_320*limit/100)
dfcab17e 5090 return 400000;
6cca3195 5091 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5092 return freq_320;
e37c67a1 5093 else if (max_pixclk > 0)
dfcab17e 5094 return 266667;
e37c67a1
VS
5095 else
5096 return 200000;
30a970c6
JB
5097}
5098
2f2d7aa1
VS
5099/* compute the max pixel clock for new configuration */
5100static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
5101{
5102 struct drm_device *dev = dev_priv->dev;
5103 struct intel_crtc *intel_crtc;
5104 int max_pixclk = 0;
5105
d3fcc808 5106 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 5107 if (intel_crtc->new_enabled)
30a970c6 5108 max_pixclk = max(max_pixclk,
2d112de7 5109 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
30a970c6
JB
5110 }
5111
5112 return max_pixclk;
5113}
5114
5115static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 5116 unsigned *prepare_pipes)
30a970c6
JB
5117{
5118 struct drm_i915_private *dev_priv = dev->dev_private;
5119 struct intel_crtc *intel_crtc;
2f2d7aa1 5120 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 5121
d60c4473
ID
5122 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5123 dev_priv->vlv_cdclk_freq)
30a970c6
JB
5124 return;
5125
2f2d7aa1 5126 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 5127 for_each_intel_crtc(dev, intel_crtc)
83d65738 5128 if (intel_crtc->base.state->enable)
30a970c6
JB
5129 *prepare_pipes |= (1 << intel_crtc->pipe);
5130}
5131
1e69cd74
VS
5132static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5133{
5134 unsigned int credits, default_credits;
5135
5136 if (IS_CHERRYVIEW(dev_priv))
5137 default_credits = PFI_CREDIT(12);
5138 else
5139 default_credits = PFI_CREDIT(8);
5140
5141 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5142 /* CHV suggested value is 31 or 63 */
5143 if (IS_CHERRYVIEW(dev_priv))
5144 credits = PFI_CREDIT_31;
5145 else
5146 credits = PFI_CREDIT(15);
5147 } else {
5148 credits = default_credits;
5149 }
5150
5151 /*
5152 * WA - write default credits before re-programming
5153 * FIXME: should we also set the resend bit here?
5154 */
5155 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5156 default_credits);
5157
5158 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5159 credits | PFI_CREDIT_RESEND);
5160
5161 /*
5162 * FIXME is this guaranteed to clear
5163 * immediately or should we poll for it?
5164 */
5165 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5166}
5167
30a970c6
JB
5168static void valleyview_modeset_global_resources(struct drm_device *dev)
5169{
5170 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 5171 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
5172 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5173
383c5a6a 5174 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
738c05c0
ID
5175 /*
5176 * FIXME: We can end up here with all power domains off, yet
5177 * with a CDCLK frequency other than the minimum. To account
5178 * for this take the PIPE-A power domain, which covers the HW
5179 * blocks needed for the following programming. This can be
5180 * removed once it's guaranteed that we get here either with
5181 * the minimum CDCLK set, or the required power domains
5182 * enabled.
5183 */
5184 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5185
383c5a6a
VS
5186 if (IS_CHERRYVIEW(dev))
5187 cherryview_set_cdclk(dev, req_cdclk);
5188 else
5189 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5190
1e69cd74
VS
5191 vlv_program_pfi_credits(dev_priv);
5192
738c05c0 5193 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5194 }
30a970c6
JB
5195}
5196
89b667f8
JB
5197static void valleyview_crtc_enable(struct drm_crtc *crtc)
5198{
5199 struct drm_device *dev = crtc->dev;
a72e4c9f 5200 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5202 struct intel_encoder *encoder;
5203 int pipe = intel_crtc->pipe;
23538ef1 5204 bool is_dsi;
89b667f8 5205
83d65738 5206 WARN_ON(!crtc->state->enable);
89b667f8
JB
5207
5208 if (intel_crtc->active)
5209 return;
5210
409ee761 5211 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5212
1ae0d137
VS
5213 if (!is_dsi) {
5214 if (IS_CHERRYVIEW(dev))
6e3c9717 5215 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5216 else
6e3c9717 5217 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5218 }
5b18e57c 5219
6e3c9717 5220 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5221 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5222
5223 intel_set_pipe_timings(intel_crtc);
5224
c14b0485
VS
5225 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5226 struct drm_i915_private *dev_priv = dev->dev_private;
5227
5228 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5229 I915_WRITE(CHV_CANVAS(pipe), 0);
5230 }
5231
5b18e57c
DV
5232 i9xx_set_pipeconf(intel_crtc);
5233
89b667f8 5234 intel_crtc->active = true;
89b667f8 5235
a72e4c9f 5236 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5237
89b667f8
JB
5238 for_each_encoder_on_crtc(dev, crtc, encoder)
5239 if (encoder->pre_pll_enable)
5240 encoder->pre_pll_enable(encoder);
5241
9d556c99
CML
5242 if (!is_dsi) {
5243 if (IS_CHERRYVIEW(dev))
6e3c9717 5244 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5245 else
6e3c9717 5246 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5247 }
89b667f8
JB
5248
5249 for_each_encoder_on_crtc(dev, crtc, encoder)
5250 if (encoder->pre_enable)
5251 encoder->pre_enable(encoder);
5252
2dd24552
JB
5253 i9xx_pfit_enable(intel_crtc);
5254
63cbb074
VS
5255 intel_crtc_load_lut(crtc);
5256
f37fcc2a 5257 intel_update_watermarks(crtc);
e1fdc473 5258 intel_enable_pipe(intel_crtc);
be6a6f8e 5259
4b3a9526
VS
5260 assert_vblank_disabled(crtc);
5261 drm_crtc_vblank_on(crtc);
5262
f9b61ff6
DV
5263 for_each_encoder_on_crtc(dev, crtc, encoder)
5264 encoder->enable(encoder);
5265
9ab0460b 5266 intel_crtc_enable_planes(crtc);
d40d9187 5267
56b80e1f 5268 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5269 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
5270}
5271
f13c2ef3
DV
5272static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5273{
5274 struct drm_device *dev = crtc->base.dev;
5275 struct drm_i915_private *dev_priv = dev->dev_private;
5276
6e3c9717
ACO
5277 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5278 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5279}
5280
0b8765c6 5281static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5282{
5283 struct drm_device *dev = crtc->dev;
a72e4c9f 5284 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5286 struct intel_encoder *encoder;
79e53945 5287 int pipe = intel_crtc->pipe;
79e53945 5288
83d65738 5289 WARN_ON(!crtc->state->enable);
08a48469 5290
f7abfe8b
CW
5291 if (intel_crtc->active)
5292 return;
5293
f13c2ef3
DV
5294 i9xx_set_pll_dividers(intel_crtc);
5295
6e3c9717 5296 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5297 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5298
5299 intel_set_pipe_timings(intel_crtc);
5300
5b18e57c
DV
5301 i9xx_set_pipeconf(intel_crtc);
5302
f7abfe8b 5303 intel_crtc->active = true;
6b383a7f 5304
4a3436e8 5305 if (!IS_GEN2(dev))
a72e4c9f 5306 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5307
9d6d9f19
MK
5308 for_each_encoder_on_crtc(dev, crtc, encoder)
5309 if (encoder->pre_enable)
5310 encoder->pre_enable(encoder);
5311
f6736a1a
DV
5312 i9xx_enable_pll(intel_crtc);
5313
2dd24552
JB
5314 i9xx_pfit_enable(intel_crtc);
5315
63cbb074
VS
5316 intel_crtc_load_lut(crtc);
5317
f37fcc2a 5318 intel_update_watermarks(crtc);
e1fdc473 5319 intel_enable_pipe(intel_crtc);
be6a6f8e 5320
4b3a9526
VS
5321 assert_vblank_disabled(crtc);
5322 drm_crtc_vblank_on(crtc);
5323
f9b61ff6
DV
5324 for_each_encoder_on_crtc(dev, crtc, encoder)
5325 encoder->enable(encoder);
5326
9ab0460b 5327 intel_crtc_enable_planes(crtc);
d40d9187 5328
4a3436e8
VS
5329 /*
5330 * Gen2 reports pipe underruns whenever all planes are disabled.
5331 * So don't enable underrun reporting before at least some planes
5332 * are enabled.
5333 * FIXME: Need to fix the logic to work when we turn off all planes
5334 * but leave the pipe running.
5335 */
5336 if (IS_GEN2(dev))
a72e4c9f 5337 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5338
56b80e1f 5339 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5340 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 5341}
79e53945 5342
87476d63
DV
5343static void i9xx_pfit_disable(struct intel_crtc *crtc)
5344{
5345 struct drm_device *dev = crtc->base.dev;
5346 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5347
6e3c9717 5348 if (!crtc->config->gmch_pfit.control)
328d8e82 5349 return;
87476d63 5350
328d8e82 5351 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5352
328d8e82
DV
5353 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5354 I915_READ(PFIT_CONTROL));
5355 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5356}
5357
0b8765c6
JB
5358static void i9xx_crtc_disable(struct drm_crtc *crtc)
5359{
5360 struct drm_device *dev = crtc->dev;
5361 struct drm_i915_private *dev_priv = dev->dev_private;
5362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5363 struct intel_encoder *encoder;
0b8765c6 5364 int pipe = intel_crtc->pipe;
ef9c3aee 5365
f7abfe8b
CW
5366 if (!intel_crtc->active)
5367 return;
5368
4a3436e8
VS
5369 /*
5370 * Gen2 reports pipe underruns whenever all planes are disabled.
5371 * So diasble underrun reporting before all the planes get disabled.
5372 * FIXME: Need to fix the logic to work when we turn off all planes
5373 * but leave the pipe running.
5374 */
5375 if (IS_GEN2(dev))
a72e4c9f 5376 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5377
564ed191
ID
5378 /*
5379 * Vblank time updates from the shadow to live plane control register
5380 * are blocked if the memory self-refresh mode is active at that
5381 * moment. So to make sure the plane gets truly disabled, disable
5382 * first the self-refresh mode. The self-refresh enable bit in turn
5383 * will be checked/applied by the HW only at the next frame start
5384 * event which is after the vblank start event, so we need to have a
5385 * wait-for-vblank between disabling the plane and the pipe.
5386 */
5387 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
5388 intel_crtc_disable_planes(crtc);
5389
6304cd91
VS
5390 /*
5391 * On gen2 planes are double buffered but the pipe isn't, so we must
5392 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5393 * We also need to wait on all gmch platforms because of the
5394 * self-refresh mode constraint explained above.
6304cd91 5395 */
564ed191 5396 intel_wait_for_vblank(dev, pipe);
6304cd91 5397
4b3a9526
VS
5398 for_each_encoder_on_crtc(dev, crtc, encoder)
5399 encoder->disable(encoder);
5400
f9b61ff6
DV
5401 drm_crtc_vblank_off(crtc);
5402 assert_vblank_disabled(crtc);
5403
575f7ab7 5404 intel_disable_pipe(intel_crtc);
24a1f16d 5405
87476d63 5406 i9xx_pfit_disable(intel_crtc);
24a1f16d 5407
89b667f8
JB
5408 for_each_encoder_on_crtc(dev, crtc, encoder)
5409 if (encoder->post_disable)
5410 encoder->post_disable(encoder);
5411
409ee761 5412 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
5413 if (IS_CHERRYVIEW(dev))
5414 chv_disable_pll(dev_priv, pipe);
5415 else if (IS_VALLEYVIEW(dev))
5416 vlv_disable_pll(dev_priv, pipe);
5417 else
1c4e0274 5418 i9xx_disable_pll(intel_crtc);
076ed3b2 5419 }
0b8765c6 5420
4a3436e8 5421 if (!IS_GEN2(dev))
a72e4c9f 5422 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 5423
f7abfe8b 5424 intel_crtc->active = false;
46ba614c 5425 intel_update_watermarks(crtc);
f37fcc2a 5426
efa9624e 5427 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5428 intel_fbc_update(dev);
efa9624e 5429 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5430}
5431
ee7b9f93
JB
5432static void i9xx_crtc_off(struct drm_crtc *crtc)
5433{
5434}
5435
b04c5bd6
BF
5436/* Master function to enable/disable CRTC and corresponding power wells */
5437void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5438{
5439 struct drm_device *dev = crtc->dev;
5440 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5442 enum intel_display_power_domain domain;
5443 unsigned long domains;
976f8a20 5444
0e572fe7
DV
5445 if (enable) {
5446 if (!intel_crtc->active) {
e1e9fb84
DV
5447 domains = get_crtc_power_domains(crtc);
5448 for_each_power_domain(domain, domains)
5449 intel_display_power_get(dev_priv, domain);
5450 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5451
5452 dev_priv->display.crtc_enable(crtc);
5453 }
5454 } else {
5455 if (intel_crtc->active) {
5456 dev_priv->display.crtc_disable(crtc);
5457
e1e9fb84
DV
5458 domains = intel_crtc->enabled_power_domains;
5459 for_each_power_domain(domain, domains)
5460 intel_display_power_put(dev_priv, domain);
5461 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5462 }
5463 }
b04c5bd6
BF
5464}
5465
5466/**
5467 * Sets the power management mode of the pipe and plane.
5468 */
5469void intel_crtc_update_dpms(struct drm_crtc *crtc)
5470{
5471 struct drm_device *dev = crtc->dev;
5472 struct intel_encoder *intel_encoder;
5473 bool enable = false;
5474
5475 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5476 enable |= intel_encoder->connectors_active;
5477
5478 intel_crtc_control(crtc, enable);
976f8a20
DV
5479}
5480
cdd59983
CW
5481static void intel_crtc_disable(struct drm_crtc *crtc)
5482{
cdd59983 5483 struct drm_device *dev = crtc->dev;
976f8a20 5484 struct drm_connector *connector;
ee7b9f93 5485 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 5486
976f8a20 5487 /* crtc should still be enabled when we disable it. */
83d65738 5488 WARN_ON(!crtc->state->enable);
976f8a20
DV
5489
5490 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
5491 dev_priv->display.off(crtc);
5492
455a6808 5493 crtc->primary->funcs->disable_plane(crtc->primary);
976f8a20
DV
5494
5495 /* Update computed state. */
5496 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5497 if (!connector->encoder || !connector->encoder->crtc)
5498 continue;
5499
5500 if (connector->encoder->crtc != crtc)
5501 continue;
5502
5503 connector->dpms = DRM_MODE_DPMS_OFF;
5504 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5505 }
5506}
5507
ea5b213a 5508void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5509{
4ef69c7a 5510 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5511
ea5b213a
CW
5512 drm_encoder_cleanup(encoder);
5513 kfree(intel_encoder);
7e7d76c3
JB
5514}
5515
9237329d 5516/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5517 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5518 * state of the entire output pipe. */
9237329d 5519static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5520{
5ab432ef
DV
5521 if (mode == DRM_MODE_DPMS_ON) {
5522 encoder->connectors_active = true;
5523
b2cabb0e 5524 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5525 } else {
5526 encoder->connectors_active = false;
5527
b2cabb0e 5528 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5529 }
79e53945
JB
5530}
5531
0a91ca29
DV
5532/* Cross check the actual hw state with our own modeset state tracking (and it's
5533 * internal consistency). */
b980514c 5534static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5535{
0a91ca29
DV
5536 if (connector->get_hw_state(connector)) {
5537 struct intel_encoder *encoder = connector->encoder;
5538 struct drm_crtc *crtc;
5539 bool encoder_enabled;
5540 enum pipe pipe;
5541
5542 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5543 connector->base.base.id,
c23cc417 5544 connector->base.name);
0a91ca29 5545
0e32b39c
DA
5546 /* there is no real hw state for MST connectors */
5547 if (connector->mst_port)
5548 return;
5549
e2c719b7 5550 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 5551 "wrong connector dpms state\n");
e2c719b7 5552 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 5553 "active connector not linked to encoder\n");
0a91ca29 5554
36cd7444 5555 if (encoder) {
e2c719b7 5556 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
5557 "encoder->connectors_active not set\n");
5558
5559 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
5560 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5561 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 5562 return;
0a91ca29 5563
36cd7444 5564 crtc = encoder->base.crtc;
0a91ca29 5565
83d65738
MR
5566 I915_STATE_WARN(!crtc->state->enable,
5567 "crtc not enabled\n");
e2c719b7
RC
5568 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5569 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
5570 "encoder active on the wrong pipe\n");
5571 }
0a91ca29 5572 }
79e53945
JB
5573}
5574
5ab432ef
DV
5575/* Even simpler default implementation, if there's really no special case to
5576 * consider. */
5577void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5578{
5ab432ef
DV
5579 /* All the simple cases only support two dpms states. */
5580 if (mode != DRM_MODE_DPMS_ON)
5581 mode = DRM_MODE_DPMS_OFF;
d4270e57 5582
5ab432ef
DV
5583 if (mode == connector->dpms)
5584 return;
5585
5586 connector->dpms = mode;
5587
5588 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5589 if (connector->encoder)
5590 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5591
b980514c 5592 intel_modeset_check_state(connector->dev);
79e53945
JB
5593}
5594
f0947c37
DV
5595/* Simple connector->get_hw_state implementation for encoders that support only
5596 * one connector and no cloning and hence the encoder state determines the state
5597 * of the connector. */
5598bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5599{
24929352 5600 enum pipe pipe = 0;
f0947c37 5601 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5602
f0947c37 5603 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5604}
5605
d272ddfa
VS
5606static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
5607{
5608 struct intel_crtc *crtc =
5609 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5610
5611 if (crtc->base.state->enable &&
5612 crtc->config->has_pch_encoder)
5613 return crtc->config->fdi_lanes;
5614
5615 return 0;
5616}
5617
1857e1da 5618static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 5619 struct intel_crtc_state *pipe_config)
1857e1da 5620{
1857e1da
DV
5621 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5622 pipe_name(pipe), pipe_config->fdi_lanes);
5623 if (pipe_config->fdi_lanes > 4) {
5624 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5625 pipe_name(pipe), pipe_config->fdi_lanes);
5626 return false;
5627 }
5628
bafb6553 5629 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5630 if (pipe_config->fdi_lanes > 2) {
5631 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5632 pipe_config->fdi_lanes);
5633 return false;
5634 } else {
5635 return true;
5636 }
5637 }
5638
5639 if (INTEL_INFO(dev)->num_pipes == 2)
5640 return true;
5641
5642 /* Ivybridge 3 pipe is really complicated */
5643 switch (pipe) {
5644 case PIPE_A:
5645 return true;
5646 case PIPE_B:
d272ddfa
VS
5647 if (pipe_config->fdi_lanes > 2 &&
5648 pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
1857e1da
DV
5649 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5650 pipe_name(pipe), pipe_config->fdi_lanes);
5651 return false;
5652 }
5653 return true;
5654 case PIPE_C:
251cc67c
VS
5655 if (pipe_config->fdi_lanes > 2) {
5656 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5657 pipe_name(pipe), pipe_config->fdi_lanes);
5658 return false;
5659 }
d272ddfa 5660 if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
1857e1da
DV
5661 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5662 return false;
5663 }
5664 return true;
5665 default:
5666 BUG();
5667 }
5668}
5669
e29c22c0
DV
5670#define RETRY 1
5671static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 5672 struct intel_crtc_state *pipe_config)
877d48d5 5673{
1857e1da 5674 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 5675 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
ff9a6750 5676 int lane, link_bw, fdi_dotclock;
e29c22c0 5677 bool setup_ok, needs_recompute = false;
877d48d5 5678
e29c22c0 5679retry:
877d48d5
DV
5680 /* FDI is a binary signal running at ~2.7GHz, encoding
5681 * each output octet as 10 bits. The actual frequency
5682 * is stored as a divider into a 100MHz clock, and the
5683 * mode pixel clock is stored in units of 1KHz.
5684 * Hence the bw of each lane in terms of the mode signal
5685 * is:
5686 */
5687 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5688
241bfc38 5689 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5690
2bd89a07 5691 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5692 pipe_config->pipe_bpp);
5693
5694 pipe_config->fdi_lanes = lane;
5695
2bd89a07 5696 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5697 link_bw, &pipe_config->fdi_m_n);
1857e1da 5698
e29c22c0
DV
5699 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5700 intel_crtc->pipe, pipe_config);
5701 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5702 pipe_config->pipe_bpp -= 2*3;
5703 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5704 pipe_config->pipe_bpp);
5705 needs_recompute = true;
5706 pipe_config->bw_constrained = true;
5707
5708 goto retry;
5709 }
5710
5711 if (needs_recompute)
5712 return RETRY;
5713
5714 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5715}
5716
42db64ef 5717static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 5718 struct intel_crtc_state *pipe_config)
42db64ef 5719{
d330a953 5720 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5721 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5722 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5723}
5724
a43f6e0f 5725static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 5726 struct intel_crtc_state *pipe_config)
79e53945 5727{
a43f6e0f 5728 struct drm_device *dev = crtc->base.dev;
8bd31e67 5729 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 5730 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 5731
ad3a4479 5732 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 5733 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
5734 int clock_limit =
5735 dev_priv->display.get_display_clock_speed(dev);
5736
5737 /*
5738 * Enable pixel doubling when the dot clock
5739 * is > 90% of the (display) core speed.
5740 *
b397c96b
VS
5741 * GDG double wide on either pipe,
5742 * otherwise pipe A only.
cf532bb2 5743 */
b397c96b 5744 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5745 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5746 clock_limit *= 2;
cf532bb2 5747 pipe_config->double_wide = true;
ad3a4479
VS
5748 }
5749
241bfc38 5750 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5751 return -EINVAL;
2c07245f 5752 }
89749350 5753
1d1d0e27
VS
5754 /*
5755 * Pipe horizontal size must be even in:
5756 * - DVO ganged mode
5757 * - LVDS dual channel mode
5758 * - Double wide pipe
5759 */
b4f2bf4c 5760 if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
5761 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5762 pipe_config->pipe_src_w &= ~1;
5763
8693a824
DL
5764 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5765 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5766 */
5767 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5768 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5769 return -EINVAL;
44f46b42 5770
bd080ee5 5771 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5772 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5773 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5774 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5775 * for lvds. */
5776 pipe_config->pipe_bpp = 8*3;
5777 }
5778
f5adf94e 5779 if (HAS_IPS(dev))
a43f6e0f
DV
5780 hsw_compute_ips_config(crtc, pipe_config);
5781
877d48d5 5782 if (pipe_config->has_pch_encoder)
a43f6e0f 5783 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5784
e29c22c0 5785 return 0;
79e53945
JB
5786}
5787
25eb05fc
JB
5788static int valleyview_get_display_clock_speed(struct drm_device *dev)
5789{
d197b7d3 5790 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
5791 u32 val;
5792 int divider;
5793
6bcda4f0
VS
5794 if (dev_priv->hpll_freq == 0)
5795 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5796
d197b7d3
VS
5797 mutex_lock(&dev_priv->dpio_lock);
5798 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5799 mutex_unlock(&dev_priv->dpio_lock);
5800
5801 divider = val & DISPLAY_FREQUENCY_VALUES;
5802
7d007f40
VS
5803 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5804 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5805 "cdclk change in progress\n");
5806
6bcda4f0 5807 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
5808}
5809
e70236a8
JB
5810static int i945_get_display_clock_speed(struct drm_device *dev)
5811{
5812 return 400000;
5813}
79e53945 5814
e70236a8 5815static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5816{
e70236a8
JB
5817 return 333000;
5818}
79e53945 5819
e70236a8
JB
5820static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5821{
5822 return 200000;
5823}
79e53945 5824
257a7ffc
DV
5825static int pnv_get_display_clock_speed(struct drm_device *dev)
5826{
5827 u16 gcfgc = 0;
5828
5829 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5830
5831 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5832 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5833 return 267000;
5834 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5835 return 333000;
5836 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5837 return 444000;
5838 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5839 return 200000;
5840 default:
5841 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5842 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5843 return 133000;
5844 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5845 return 167000;
5846 }
5847}
5848
e70236a8
JB
5849static int i915gm_get_display_clock_speed(struct drm_device *dev)
5850{
5851 u16 gcfgc = 0;
79e53945 5852
e70236a8
JB
5853 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5854
5855 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5856 return 133000;
5857 else {
5858 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5859 case GC_DISPLAY_CLOCK_333_MHZ:
5860 return 333000;
5861 default:
5862 case GC_DISPLAY_CLOCK_190_200_MHZ:
5863 return 190000;
79e53945 5864 }
e70236a8
JB
5865 }
5866}
5867
5868static int i865_get_display_clock_speed(struct drm_device *dev)
5869{
5870 return 266000;
5871}
5872
5873static int i855_get_display_clock_speed(struct drm_device *dev)
5874{
5875 u16 hpllcc = 0;
5876 /* Assume that the hardware is in the high speed state. This
5877 * should be the default.
5878 */
5879 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5880 case GC_CLOCK_133_200:
5881 case GC_CLOCK_100_200:
5882 return 200000;
5883 case GC_CLOCK_166_250:
5884 return 250000;
5885 case GC_CLOCK_100_133:
79e53945 5886 return 133000;
e70236a8 5887 }
79e53945 5888
e70236a8
JB
5889 /* Shouldn't happen */
5890 return 0;
5891}
79e53945 5892
e70236a8
JB
5893static int i830_get_display_clock_speed(struct drm_device *dev)
5894{
5895 return 133000;
79e53945
JB
5896}
5897
2c07245f 5898static void
a65851af 5899intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5900{
a65851af
VS
5901 while (*num > DATA_LINK_M_N_MASK ||
5902 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5903 *num >>= 1;
5904 *den >>= 1;
5905 }
5906}
5907
a65851af
VS
5908static void compute_m_n(unsigned int m, unsigned int n,
5909 uint32_t *ret_m, uint32_t *ret_n)
5910{
5911 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5912 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5913 intel_reduce_m_n_ratio(ret_m, ret_n);
5914}
5915
e69d0bc1
DV
5916void
5917intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5918 int pixel_clock, int link_clock,
5919 struct intel_link_m_n *m_n)
2c07245f 5920{
e69d0bc1 5921 m_n->tu = 64;
a65851af
VS
5922
5923 compute_m_n(bits_per_pixel * pixel_clock,
5924 link_clock * nlanes * 8,
5925 &m_n->gmch_m, &m_n->gmch_n);
5926
5927 compute_m_n(pixel_clock, link_clock,
5928 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5929}
5930
a7615030
CW
5931static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5932{
d330a953
JN
5933 if (i915.panel_use_ssc >= 0)
5934 return i915.panel_use_ssc != 0;
41aa3448 5935 return dev_priv->vbt.lvds_use_ssc
435793df 5936 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5937}
5938
409ee761 5939static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
c65d77d8 5940{
409ee761 5941 struct drm_device *dev = crtc->base.dev;
c65d77d8
JB
5942 struct drm_i915_private *dev_priv = dev->dev_private;
5943 int refclk;
5944
a0c4da24 5945 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5946 refclk = 100000;
d0737e1d 5947 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5948 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5949 refclk = dev_priv->vbt.lvds_ssc_freq;
5950 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5951 } else if (!IS_GEN2(dev)) {
5952 refclk = 96000;
5953 } else {
5954 refclk = 48000;
5955 }
5956
5957 return refclk;
5958}
5959
7429e9d4 5960static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5961{
7df00d7a 5962 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5963}
f47709a9 5964
7429e9d4
DV
5965static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5966{
5967 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5968}
5969
f47709a9 5970static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 5971 struct intel_crtc_state *crtc_state,
a7516a05
JB
5972 intel_clock_t *reduced_clock)
5973{
f47709a9 5974 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5975 u32 fp, fp2 = 0;
5976
5977 if (IS_PINEVIEW(dev)) {
190f68c5 5978 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5979 if (reduced_clock)
7429e9d4 5980 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5981 } else {
190f68c5 5982 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 5983 if (reduced_clock)
7429e9d4 5984 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5985 }
5986
190f68c5 5987 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 5988
f47709a9 5989 crtc->lowfreq_avail = false;
e1f234bd 5990 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
d330a953 5991 reduced_clock && i915.powersave) {
190f68c5 5992 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 5993 crtc->lowfreq_avail = true;
a7516a05 5994 } else {
190f68c5 5995 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
5996 }
5997}
5998
5e69f97f
CML
5999static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6000 pipe)
89b667f8
JB
6001{
6002 u32 reg_val;
6003
6004 /*
6005 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6006 * and set it to a reasonable value instead.
6007 */
ab3c759a 6008 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6009 reg_val &= 0xffffff00;
6010 reg_val |= 0x00000030;
ab3c759a 6011 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6012
ab3c759a 6013 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6014 reg_val &= 0x8cffffff;
6015 reg_val = 0x8c000000;
ab3c759a 6016 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6017
ab3c759a 6018 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6019 reg_val &= 0xffffff00;
ab3c759a 6020 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6021
ab3c759a 6022 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6023 reg_val &= 0x00ffffff;
6024 reg_val |= 0xb0000000;
ab3c759a 6025 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6026}
6027
b551842d
DV
6028static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6029 struct intel_link_m_n *m_n)
6030{
6031 struct drm_device *dev = crtc->base.dev;
6032 struct drm_i915_private *dev_priv = dev->dev_private;
6033 int pipe = crtc->pipe;
6034
e3b95f1e
DV
6035 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6036 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6037 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6038 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6039}
6040
6041static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6042 struct intel_link_m_n *m_n,
6043 struct intel_link_m_n *m2_n2)
b551842d
DV
6044{
6045 struct drm_device *dev = crtc->base.dev;
6046 struct drm_i915_private *dev_priv = dev->dev_private;
6047 int pipe = crtc->pipe;
6e3c9717 6048 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6049
6050 if (INTEL_INFO(dev)->gen >= 5) {
6051 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6052 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6053 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6054 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6055 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6056 * for gen < 8) and if DRRS is supported (to make sure the
6057 * registers are not unnecessarily accessed).
6058 */
44395bfe 6059 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6060 crtc->config->has_drrs) {
f769cd24
VK
6061 I915_WRITE(PIPE_DATA_M2(transcoder),
6062 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6063 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6064 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6065 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6066 }
b551842d 6067 } else {
e3b95f1e
DV
6068 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6069 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6070 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6071 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6072 }
6073}
6074
fe3cd48d 6075void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6076{
fe3cd48d
R
6077 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6078
6079 if (m_n == M1_N1) {
6080 dp_m_n = &crtc->config->dp_m_n;
6081 dp_m2_n2 = &crtc->config->dp_m2_n2;
6082 } else if (m_n == M2_N2) {
6083
6084 /*
6085 * M2_N2 registers are not supported. Hence m2_n2 divider value
6086 * needs to be programmed into M1_N1.
6087 */
6088 dp_m_n = &crtc->config->dp_m2_n2;
6089 } else {
6090 DRM_ERROR("Unsupported divider value\n");
6091 return;
6092 }
6093
6e3c9717
ACO
6094 if (crtc->config->has_pch_encoder)
6095 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6096 else
fe3cd48d 6097 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6098}
6099
d288f65f 6100static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6101 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6102{
6103 u32 dpll, dpll_md;
6104
6105 /*
6106 * Enable DPIO clock input. We should never disable the reference
6107 * clock for pipe B, since VGA hotplug / manual detection depends
6108 * on it.
6109 */
6110 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6111 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6112 /* We should never disable this, set it here for state tracking */
6113 if (crtc->pipe == PIPE_B)
6114 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6115 dpll |= DPLL_VCO_ENABLE;
d288f65f 6116 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6117
d288f65f 6118 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6119 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6120 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6121}
6122
d288f65f 6123static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6124 const struct intel_crtc_state *pipe_config)
a0c4da24 6125{
f47709a9 6126 struct drm_device *dev = crtc->base.dev;
a0c4da24 6127 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6128 int pipe = crtc->pipe;
bdd4b6a6 6129 u32 mdiv;
a0c4da24 6130 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6131 u32 coreclk, reg_val;
a0c4da24 6132
09153000
DV
6133 mutex_lock(&dev_priv->dpio_lock);
6134
d288f65f
VS
6135 bestn = pipe_config->dpll.n;
6136 bestm1 = pipe_config->dpll.m1;
6137 bestm2 = pipe_config->dpll.m2;
6138 bestp1 = pipe_config->dpll.p1;
6139 bestp2 = pipe_config->dpll.p2;
a0c4da24 6140
89b667f8
JB
6141 /* See eDP HDMI DPIO driver vbios notes doc */
6142
6143 /* PLL B needs special handling */
bdd4b6a6 6144 if (pipe == PIPE_B)
5e69f97f 6145 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6146
6147 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6148 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6149
6150 /* Disable target IRef on PLL */
ab3c759a 6151 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6152 reg_val &= 0x00ffffff;
ab3c759a 6153 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6154
6155 /* Disable fast lock */
ab3c759a 6156 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6157
6158 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6159 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6160 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6161 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6162 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6163
6164 /*
6165 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6166 * but we don't support that).
6167 * Note: don't use the DAC post divider as it seems unstable.
6168 */
6169 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6170 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6171
a0c4da24 6172 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6173 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6174
89b667f8 6175 /* Set HBR and RBR LPF coefficients */
d288f65f 6176 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6177 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6178 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6179 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6180 0x009f0003);
89b667f8 6181 else
ab3c759a 6182 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6183 0x00d0000f);
6184
681a8504 6185 if (pipe_config->has_dp_encoder) {
89b667f8 6186 /* Use SSC source */
bdd4b6a6 6187 if (pipe == PIPE_A)
ab3c759a 6188 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6189 0x0df40000);
6190 else
ab3c759a 6191 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6192 0x0df70000);
6193 } else { /* HDMI or VGA */
6194 /* Use bend source */
bdd4b6a6 6195 if (pipe == PIPE_A)
ab3c759a 6196 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6197 0x0df70000);
6198 else
ab3c759a 6199 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6200 0x0df40000);
6201 }
a0c4da24 6202
ab3c759a 6203 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6204 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6205 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6206 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6207 coreclk |= 0x01000000;
ab3c759a 6208 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6209
ab3c759a 6210 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6211 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6212}
6213
d288f65f 6214static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6215 struct intel_crtc_state *pipe_config)
1ae0d137 6216{
d288f65f 6217 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6218 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6219 DPLL_VCO_ENABLE;
6220 if (crtc->pipe != PIPE_A)
d288f65f 6221 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6222
d288f65f
VS
6223 pipe_config->dpll_hw_state.dpll_md =
6224 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6225}
6226
d288f65f 6227static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6228 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6229{
6230 struct drm_device *dev = crtc->base.dev;
6231 struct drm_i915_private *dev_priv = dev->dev_private;
6232 int pipe = crtc->pipe;
6233 int dpll_reg = DPLL(crtc->pipe);
6234 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6235 u32 loopfilter, tribuf_calcntr;
9d556c99 6236 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6237 u32 dpio_val;
9cbe40c1 6238 int vco;
9d556c99 6239
d288f65f
VS
6240 bestn = pipe_config->dpll.n;
6241 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6242 bestm1 = pipe_config->dpll.m1;
6243 bestm2 = pipe_config->dpll.m2 >> 22;
6244 bestp1 = pipe_config->dpll.p1;
6245 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6246 vco = pipe_config->dpll.vco;
a945ce7e 6247 dpio_val = 0;
9cbe40c1 6248 loopfilter = 0;
9d556c99
CML
6249
6250 /*
6251 * Enable Refclk and SSC
6252 */
a11b0703 6253 I915_WRITE(dpll_reg,
d288f65f 6254 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
6255
6256 mutex_lock(&dev_priv->dpio_lock);
9d556c99 6257
9d556c99
CML
6258 /* p1 and p2 divider */
6259 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6260 5 << DPIO_CHV_S1_DIV_SHIFT |
6261 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6262 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6263 1 << DPIO_CHV_K_DIV_SHIFT);
6264
6265 /* Feedback post-divider - m2 */
6266 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6267
6268 /* Feedback refclk divider - n and m1 */
6269 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6270 DPIO_CHV_M1_DIV_BY_2 |
6271 1 << DPIO_CHV_N_DIV_SHIFT);
6272
6273 /* M2 fraction division */
a945ce7e
VP
6274 if (bestm2_frac)
6275 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6276
6277 /* M2 fraction division enable */
a945ce7e
VP
6278 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6279 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6280 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6281 if (bestm2_frac)
6282 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6283 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6284
de3a0fde
VP
6285 /* Program digital lock detect threshold */
6286 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6287 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6288 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6289 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6290 if (!bestm2_frac)
6291 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6292 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6293
9d556c99 6294 /* Loop filter */
9cbe40c1
VP
6295 if (vco == 5400000) {
6296 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6297 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6298 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6299 tribuf_calcntr = 0x9;
6300 } else if (vco <= 6200000) {
6301 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6302 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6303 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6304 tribuf_calcntr = 0x9;
6305 } else if (vco <= 6480000) {
6306 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6307 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6308 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6309 tribuf_calcntr = 0x8;
6310 } else {
6311 /* Not supported. Apply the same limits as in the max case */
6312 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6313 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6314 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6315 tribuf_calcntr = 0;
6316 }
9d556c99
CML
6317 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6318
968040b2 6319 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6320 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6321 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6322 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6323
9d556c99
CML
6324 /* AFC Recal */
6325 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6326 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6327 DPIO_AFC_RECAL);
6328
6329 mutex_unlock(&dev_priv->dpio_lock);
6330}
6331
d288f65f
VS
6332/**
6333 * vlv_force_pll_on - forcibly enable just the PLL
6334 * @dev_priv: i915 private structure
6335 * @pipe: pipe PLL to enable
6336 * @dpll: PLL configuration
6337 *
6338 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6339 * in cases where we need the PLL enabled even when @pipe is not going to
6340 * be enabled.
6341 */
6342void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6343 const struct dpll *dpll)
6344{
6345 struct intel_crtc *crtc =
6346 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 6347 struct intel_crtc_state pipe_config = {
d288f65f
VS
6348 .pixel_multiplier = 1,
6349 .dpll = *dpll,
6350 };
6351
6352 if (IS_CHERRYVIEW(dev)) {
6353 chv_update_pll(crtc, &pipe_config);
6354 chv_prepare_pll(crtc, &pipe_config);
6355 chv_enable_pll(crtc, &pipe_config);
6356 } else {
6357 vlv_update_pll(crtc, &pipe_config);
6358 vlv_prepare_pll(crtc, &pipe_config);
6359 vlv_enable_pll(crtc, &pipe_config);
6360 }
6361}
6362
6363/**
6364 * vlv_force_pll_off - forcibly disable just the PLL
6365 * @dev_priv: i915 private structure
6366 * @pipe: pipe PLL to disable
6367 *
6368 * Disable the PLL for @pipe. To be used in cases where we need
6369 * the PLL enabled even when @pipe is not going to be enabled.
6370 */
6371void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6372{
6373 if (IS_CHERRYVIEW(dev))
6374 chv_disable_pll(to_i915(dev), pipe);
6375 else
6376 vlv_disable_pll(to_i915(dev), pipe);
6377}
6378
f47709a9 6379static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 6380 struct intel_crtc_state *crtc_state,
f47709a9 6381 intel_clock_t *reduced_clock,
eb1cbe48
DV
6382 int num_connectors)
6383{
f47709a9 6384 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6385 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
6386 u32 dpll;
6387 bool is_sdvo;
190f68c5 6388 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6389
190f68c5 6390 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6391
d0737e1d
ACO
6392 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6393 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
6394
6395 dpll = DPLL_VGA_MODE_DIS;
6396
d0737e1d 6397 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6398 dpll |= DPLLB_MODE_LVDS;
6399 else
6400 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6401
ef1b460d 6402 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 6403 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6404 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6405 }
198a037f
DV
6406
6407 if (is_sdvo)
4a33e48d 6408 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6409
190f68c5 6410 if (crtc_state->has_dp_encoder)
4a33e48d 6411 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6412
6413 /* compute bitmask from p1 value */
6414 if (IS_PINEVIEW(dev))
6415 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6416 else {
6417 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6418 if (IS_G4X(dev) && reduced_clock)
6419 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6420 }
6421 switch (clock->p2) {
6422 case 5:
6423 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6424 break;
6425 case 7:
6426 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6427 break;
6428 case 10:
6429 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6430 break;
6431 case 14:
6432 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6433 break;
6434 }
6435 if (INTEL_INFO(dev)->gen >= 4)
6436 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6437
190f68c5 6438 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6439 dpll |= PLL_REF_INPUT_TVCLKINBC;
d0737e1d 6440 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6441 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6442 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6443 else
6444 dpll |= PLL_REF_INPUT_DREFCLK;
6445
6446 dpll |= DPLL_VCO_ENABLE;
190f68c5 6447 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6448
eb1cbe48 6449 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 6450 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6451 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6452 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6453 }
6454}
6455
f47709a9 6456static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 6457 struct intel_crtc_state *crtc_state,
f47709a9 6458 intel_clock_t *reduced_clock,
eb1cbe48
DV
6459 int num_connectors)
6460{
f47709a9 6461 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6462 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6463 u32 dpll;
190f68c5 6464 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6465
190f68c5 6466 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6467
eb1cbe48
DV
6468 dpll = DPLL_VGA_MODE_DIS;
6469
d0737e1d 6470 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6471 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6472 } else {
6473 if (clock->p1 == 2)
6474 dpll |= PLL_P1_DIVIDE_BY_TWO;
6475 else
6476 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6477 if (clock->p2 == 4)
6478 dpll |= PLL_P2_DIVIDE_BY_4;
6479 }
6480
d0737e1d 6481 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
4a33e48d
DV
6482 dpll |= DPLL_DVO_2X_MODE;
6483
d0737e1d 6484 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6485 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6486 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6487 else
6488 dpll |= PLL_REF_INPUT_DREFCLK;
6489
6490 dpll |= DPLL_VCO_ENABLE;
190f68c5 6491 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6492}
6493
8a654f3b 6494static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6495{
6496 struct drm_device *dev = intel_crtc->base.dev;
6497 struct drm_i915_private *dev_priv = dev->dev_private;
6498 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6499 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 6500 struct drm_display_mode *adjusted_mode =
6e3c9717 6501 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6502 uint32_t crtc_vtotal, crtc_vblank_end;
6503 int vsyncshift = 0;
4d8a62ea
DV
6504
6505 /* We need to be careful not to changed the adjusted mode, for otherwise
6506 * the hw state checker will get angry at the mismatch. */
6507 crtc_vtotal = adjusted_mode->crtc_vtotal;
6508 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6509
609aeaca 6510 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6511 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6512 crtc_vtotal -= 1;
6513 crtc_vblank_end -= 1;
609aeaca 6514
409ee761 6515 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
6516 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6517 else
6518 vsyncshift = adjusted_mode->crtc_hsync_start -
6519 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6520 if (vsyncshift < 0)
6521 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6522 }
6523
6524 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6525 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6526
fe2b8f9d 6527 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6528 (adjusted_mode->crtc_hdisplay - 1) |
6529 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6530 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6531 (adjusted_mode->crtc_hblank_start - 1) |
6532 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6533 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6534 (adjusted_mode->crtc_hsync_start - 1) |
6535 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6536
fe2b8f9d 6537 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6538 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6539 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6540 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6541 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6542 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6543 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6544 (adjusted_mode->crtc_vsync_start - 1) |
6545 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6546
b5e508d4
PZ
6547 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6548 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6549 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6550 * bits. */
6551 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6552 (pipe == PIPE_B || pipe == PIPE_C))
6553 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6554
b0e77b9c
PZ
6555 /* pipesrc controls the size that is scaled from, which should
6556 * always be the user's requested size.
6557 */
6558 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
6559 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6560 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
6561}
6562
1bd1bd80 6563static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 6564 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
6565{
6566 struct drm_device *dev = crtc->base.dev;
6567 struct drm_i915_private *dev_priv = dev->dev_private;
6568 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6569 uint32_t tmp;
6570
6571 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
6572 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6573 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6574 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
6575 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6576 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6577 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
6578 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6579 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6580
6581 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
6582 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6583 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6584 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
6585 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6586 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 6587 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
6588 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6589 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
6590
6591 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
6592 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6593 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6594 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
6595 }
6596
6597 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6598 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6599 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6600
2d112de7
ACO
6601 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6602 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6603}
6604
f6a83288 6605void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 6606 struct intel_crtc_state *pipe_config)
babea61d 6607{
2d112de7
ACO
6608 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6609 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6610 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6611 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 6612
2d112de7
ACO
6613 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6614 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6615 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6616 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 6617
2d112de7 6618 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 6619
2d112de7
ACO
6620 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6621 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
6622}
6623
84b046f3
DV
6624static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6625{
6626 struct drm_device *dev = intel_crtc->base.dev;
6627 struct drm_i915_private *dev_priv = dev->dev_private;
6628 uint32_t pipeconf;
6629
9f11a9e4 6630 pipeconf = 0;
84b046f3 6631
b6b5d049
VS
6632 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6633 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6634 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6635
6e3c9717 6636 if (intel_crtc->config->double_wide)
cf532bb2 6637 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6638
ff9ce46e
DV
6639 /* only g4x and later have fancy bpc/dither controls */
6640 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 6641 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 6642 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 6643 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6644 PIPECONF_DITHER_TYPE_SP;
84b046f3 6645
6e3c9717 6646 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
6647 case 18:
6648 pipeconf |= PIPECONF_6BPC;
6649 break;
6650 case 24:
6651 pipeconf |= PIPECONF_8BPC;
6652 break;
6653 case 30:
6654 pipeconf |= PIPECONF_10BPC;
6655 break;
6656 default:
6657 /* Case prevented by intel_choose_pipe_bpp_dither. */
6658 BUG();
84b046f3
DV
6659 }
6660 }
6661
6662 if (HAS_PIPE_CXSR(dev)) {
6663 if (intel_crtc->lowfreq_avail) {
6664 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6665 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6666 } else {
6667 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6668 }
6669 }
6670
6e3c9717 6671 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 6672 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 6673 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
6674 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6675 else
6676 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6677 } else
84b046f3
DV
6678 pipeconf |= PIPECONF_PROGRESSIVE;
6679
6e3c9717 6680 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 6681 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6682
84b046f3
DV
6683 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6684 POSTING_READ(PIPECONF(intel_crtc->pipe));
6685}
6686
190f68c5
ACO
6687static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6688 struct intel_crtc_state *crtc_state)
79e53945 6689{
c7653199 6690 struct drm_device *dev = crtc->base.dev;
79e53945 6691 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 6692 int refclk, num_connectors = 0;
652c393a 6693 intel_clock_t clock, reduced_clock;
a16af721 6694 bool ok, has_reduced_clock = false;
e9fd1c02 6695 bool is_lvds = false, is_dsi = false;
5eddb70b 6696 struct intel_encoder *encoder;
d4906093 6697 const intel_limit_t *limit;
79e53945 6698
d0737e1d
ACO
6699 for_each_intel_encoder(dev, encoder) {
6700 if (encoder->new_crtc != crtc)
6701 continue;
6702
5eddb70b 6703 switch (encoder->type) {
79e53945
JB
6704 case INTEL_OUTPUT_LVDS:
6705 is_lvds = true;
6706 break;
e9fd1c02
JN
6707 case INTEL_OUTPUT_DSI:
6708 is_dsi = true;
6709 break;
6847d71b
PZ
6710 default:
6711 break;
79e53945 6712 }
43565a06 6713
c751ce4f 6714 num_connectors++;
79e53945
JB
6715 }
6716
f2335330 6717 if (is_dsi)
5b18e57c 6718 return 0;
f2335330 6719
190f68c5 6720 if (!crtc_state->clock_set) {
409ee761 6721 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6722
e9fd1c02
JN
6723 /*
6724 * Returns a set of divisors for the desired target clock with
6725 * the given refclk, or FALSE. The returned values represent
6726 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6727 * 2) / p1 / p2.
6728 */
409ee761 6729 limit = intel_limit(crtc, refclk);
c7653199 6730 ok = dev_priv->display.find_dpll(limit, crtc,
190f68c5 6731 crtc_state->port_clock,
e9fd1c02 6732 refclk, NULL, &clock);
f2335330 6733 if (!ok) {
e9fd1c02
JN
6734 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6735 return -EINVAL;
6736 }
79e53945 6737
f2335330
JN
6738 if (is_lvds && dev_priv->lvds_downclock_avail) {
6739 /*
6740 * Ensure we match the reduced clock's P to the target
6741 * clock. If the clocks don't match, we can't switch
6742 * the display clock by using the FP0/FP1. In such case
6743 * we will disable the LVDS downclock feature.
6744 */
6745 has_reduced_clock =
c7653199 6746 dev_priv->display.find_dpll(limit, crtc,
f2335330
JN
6747 dev_priv->lvds_downclock,
6748 refclk, &clock,
6749 &reduced_clock);
6750 }
6751 /* Compat-code for transition, will disappear. */
190f68c5
ACO
6752 crtc_state->dpll.n = clock.n;
6753 crtc_state->dpll.m1 = clock.m1;
6754 crtc_state->dpll.m2 = clock.m2;
6755 crtc_state->dpll.p1 = clock.p1;
6756 crtc_state->dpll.p2 = clock.p2;
f47709a9 6757 }
7026d4ac 6758
e9fd1c02 6759 if (IS_GEN2(dev)) {
190f68c5 6760 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
6761 has_reduced_clock ? &reduced_clock : NULL,
6762 num_connectors);
9d556c99 6763 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 6764 chv_update_pll(crtc, crtc_state);
e9fd1c02 6765 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 6766 vlv_update_pll(crtc, crtc_state);
e9fd1c02 6767 } else {
190f68c5 6768 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 6769 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6770 num_connectors);
e9fd1c02 6771 }
79e53945 6772
c8f7a0db 6773 return 0;
f564048e
EA
6774}
6775
2fa2fe9a 6776static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 6777 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
6778{
6779 struct drm_device *dev = crtc->base.dev;
6780 struct drm_i915_private *dev_priv = dev->dev_private;
6781 uint32_t tmp;
6782
dc9e7dec
VS
6783 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6784 return;
6785
2fa2fe9a 6786 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6787 if (!(tmp & PFIT_ENABLE))
6788 return;
2fa2fe9a 6789
06922821 6790 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6791 if (INTEL_INFO(dev)->gen < 4) {
6792 if (crtc->pipe != PIPE_B)
6793 return;
2fa2fe9a
DV
6794 } else {
6795 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6796 return;
6797 }
6798
06922821 6799 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6800 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6801 if (INTEL_INFO(dev)->gen < 5)
6802 pipe_config->gmch_pfit.lvds_border_bits =
6803 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6804}
6805
acbec814 6806static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6807 struct intel_crtc_state *pipe_config)
acbec814
JB
6808{
6809 struct drm_device *dev = crtc->base.dev;
6810 struct drm_i915_private *dev_priv = dev->dev_private;
6811 int pipe = pipe_config->cpu_transcoder;
6812 intel_clock_t clock;
6813 u32 mdiv;
662c6ecb 6814 int refclk = 100000;
acbec814 6815
f573de5a
SK
6816 /* In case of MIPI DPLL will not even be used */
6817 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6818 return;
6819
acbec814 6820 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6821 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6822 mutex_unlock(&dev_priv->dpio_lock);
6823
6824 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6825 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6826 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6827 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6828 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6829
f646628b 6830 vlv_clock(refclk, &clock);
acbec814 6831
f646628b
VS
6832 /* clock.dot is the fast clock */
6833 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6834}
6835
5724dbd1
DL
6836static void
6837i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6838 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
6839{
6840 struct drm_device *dev = crtc->base.dev;
6841 struct drm_i915_private *dev_priv = dev->dev_private;
6842 u32 val, base, offset;
6843 int pipe = crtc->pipe, plane = crtc->plane;
6844 int fourcc, pixel_format;
6761dd31 6845 unsigned int aligned_height;
b113d5ee 6846 struct drm_framebuffer *fb;
1b842c89 6847 struct intel_framebuffer *intel_fb;
1ad292b5 6848
42a7b088
DL
6849 val = I915_READ(DSPCNTR(plane));
6850 if (!(val & DISPLAY_PLANE_ENABLE))
6851 return;
6852
d9806c9f 6853 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 6854 if (!intel_fb) {
1ad292b5
JB
6855 DRM_DEBUG_KMS("failed to alloc fb\n");
6856 return;
6857 }
6858
1b842c89
DL
6859 fb = &intel_fb->base;
6860
18c5247e
DV
6861 if (INTEL_INFO(dev)->gen >= 4) {
6862 if (val & DISPPLANE_TILED) {
49af449b 6863 plane_config->tiling = I915_TILING_X;
18c5247e
DV
6864 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
6865 }
6866 }
1ad292b5
JB
6867
6868 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 6869 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
6870 fb->pixel_format = fourcc;
6871 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
6872
6873 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 6874 if (plane_config->tiling)
1ad292b5
JB
6875 offset = I915_READ(DSPTILEOFF(plane));
6876 else
6877 offset = I915_READ(DSPLINOFF(plane));
6878 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6879 } else {
6880 base = I915_READ(DSPADDR(plane));
6881 }
6882 plane_config->base = base;
6883
6884 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
6885 fb->width = ((val >> 16) & 0xfff) + 1;
6886 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6887
6888 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 6889 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6890
b113d5ee 6891 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
6892 fb->pixel_format,
6893 fb->modifier[0]);
1ad292b5 6894
f37b5c2b 6895 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 6896
2844a921
DL
6897 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6898 pipe_name(pipe), plane, fb->width, fb->height,
6899 fb->bits_per_pixel, base, fb->pitches[0],
6900 plane_config->size);
1ad292b5 6901
2d14030b 6902 plane_config->fb = intel_fb;
1ad292b5
JB
6903}
6904
70b23a98 6905static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 6906 struct intel_crtc_state *pipe_config)
70b23a98
VS
6907{
6908 struct drm_device *dev = crtc->base.dev;
6909 struct drm_i915_private *dev_priv = dev->dev_private;
6910 int pipe = pipe_config->cpu_transcoder;
6911 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6912 intel_clock_t clock;
6913 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6914 int refclk = 100000;
6915
6916 mutex_lock(&dev_priv->dpio_lock);
6917 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6918 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6919 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6920 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6921 mutex_unlock(&dev_priv->dpio_lock);
6922
6923 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6924 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6925 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6926 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6927 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6928
6929 chv_clock(refclk, &clock);
6930
6931 /* clock.dot is the fast clock */
6932 pipe_config->port_clock = clock.dot / 5;
6933}
6934
0e8ffe1b 6935static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 6936 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
6937{
6938 struct drm_device *dev = crtc->base.dev;
6939 struct drm_i915_private *dev_priv = dev->dev_private;
6940 uint32_t tmp;
6941
f458ebbc
DV
6942 if (!intel_display_power_is_enabled(dev_priv,
6943 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
6944 return false;
6945
e143a21c 6946 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6947 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6948
0e8ffe1b
DV
6949 tmp = I915_READ(PIPECONF(crtc->pipe));
6950 if (!(tmp & PIPECONF_ENABLE))
6951 return false;
6952
42571aef
VS
6953 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6954 switch (tmp & PIPECONF_BPC_MASK) {
6955 case PIPECONF_6BPC:
6956 pipe_config->pipe_bpp = 18;
6957 break;
6958 case PIPECONF_8BPC:
6959 pipe_config->pipe_bpp = 24;
6960 break;
6961 case PIPECONF_10BPC:
6962 pipe_config->pipe_bpp = 30;
6963 break;
6964 default:
6965 break;
6966 }
6967 }
6968
b5a9fa09
DV
6969 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6970 pipe_config->limited_color_range = true;
6971
282740f7
VS
6972 if (INTEL_INFO(dev)->gen < 4)
6973 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6974
1bd1bd80
DV
6975 intel_get_pipe_timings(crtc, pipe_config);
6976
2fa2fe9a
DV
6977 i9xx_get_pfit_config(crtc, pipe_config);
6978
6c49f241
DV
6979 if (INTEL_INFO(dev)->gen >= 4) {
6980 tmp = I915_READ(DPLL_MD(crtc->pipe));
6981 pipe_config->pixel_multiplier =
6982 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6983 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6984 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6985 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6986 tmp = I915_READ(DPLL(crtc->pipe));
6987 pipe_config->pixel_multiplier =
6988 ((tmp & SDVO_MULTIPLIER_MASK)
6989 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6990 } else {
6991 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6992 * port and will be fixed up in the encoder->get_config
6993 * function. */
6994 pipe_config->pixel_multiplier = 1;
6995 }
8bcc2795
DV
6996 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6997 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6998 /*
6999 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7000 * on 830. Filter it out here so that we don't
7001 * report errors due to that.
7002 */
7003 if (IS_I830(dev))
7004 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7005
8bcc2795
DV
7006 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7007 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7008 } else {
7009 /* Mask out read-only status bits. */
7010 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7011 DPLL_PORTC_READY_MASK |
7012 DPLL_PORTB_READY_MASK);
8bcc2795 7013 }
6c49f241 7014
70b23a98
VS
7015 if (IS_CHERRYVIEW(dev))
7016 chv_crtc_clock_get(crtc, pipe_config);
7017 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7018 vlv_crtc_clock_get(crtc, pipe_config);
7019 else
7020 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7021
0e8ffe1b
DV
7022 return true;
7023}
7024
dde86e2d 7025static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7026{
7027 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7028 struct intel_encoder *encoder;
74cfd7ac 7029 u32 val, final;
13d83a67 7030 bool has_lvds = false;
199e5d79 7031 bool has_cpu_edp = false;
199e5d79 7032 bool has_panel = false;
99eb6a01
KP
7033 bool has_ck505 = false;
7034 bool can_ssc = false;
13d83a67
JB
7035
7036 /* We need to take the global config into account */
b2784e15 7037 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7038 switch (encoder->type) {
7039 case INTEL_OUTPUT_LVDS:
7040 has_panel = true;
7041 has_lvds = true;
7042 break;
7043 case INTEL_OUTPUT_EDP:
7044 has_panel = true;
2de6905f 7045 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7046 has_cpu_edp = true;
7047 break;
6847d71b
PZ
7048 default:
7049 break;
13d83a67
JB
7050 }
7051 }
7052
99eb6a01 7053 if (HAS_PCH_IBX(dev)) {
41aa3448 7054 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7055 can_ssc = has_ck505;
7056 } else {
7057 has_ck505 = false;
7058 can_ssc = true;
7059 }
7060
2de6905f
ID
7061 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7062 has_panel, has_lvds, has_ck505);
13d83a67
JB
7063
7064 /* Ironlake: try to setup display ref clock before DPLL
7065 * enabling. This is only under driver's control after
7066 * PCH B stepping, previous chipset stepping should be
7067 * ignoring this setting.
7068 */
74cfd7ac
CW
7069 val = I915_READ(PCH_DREF_CONTROL);
7070
7071 /* As we must carefully and slowly disable/enable each source in turn,
7072 * compute the final state we want first and check if we need to
7073 * make any changes at all.
7074 */
7075 final = val;
7076 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7077 if (has_ck505)
7078 final |= DREF_NONSPREAD_CK505_ENABLE;
7079 else
7080 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7081
7082 final &= ~DREF_SSC_SOURCE_MASK;
7083 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7084 final &= ~DREF_SSC1_ENABLE;
7085
7086 if (has_panel) {
7087 final |= DREF_SSC_SOURCE_ENABLE;
7088
7089 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7090 final |= DREF_SSC1_ENABLE;
7091
7092 if (has_cpu_edp) {
7093 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7094 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7095 else
7096 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7097 } else
7098 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7099 } else {
7100 final |= DREF_SSC_SOURCE_DISABLE;
7101 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7102 }
7103
7104 if (final == val)
7105 return;
7106
13d83a67 7107 /* Always enable nonspread source */
74cfd7ac 7108 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7109
99eb6a01 7110 if (has_ck505)
74cfd7ac 7111 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7112 else
74cfd7ac 7113 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7114
199e5d79 7115 if (has_panel) {
74cfd7ac
CW
7116 val &= ~DREF_SSC_SOURCE_MASK;
7117 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7118
199e5d79 7119 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7120 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7121 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7122 val |= DREF_SSC1_ENABLE;
e77166b5 7123 } else
74cfd7ac 7124 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7125
7126 /* Get SSC going before enabling the outputs */
74cfd7ac 7127 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7128 POSTING_READ(PCH_DREF_CONTROL);
7129 udelay(200);
7130
74cfd7ac 7131 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7132
7133 /* Enable CPU source on CPU attached eDP */
199e5d79 7134 if (has_cpu_edp) {
99eb6a01 7135 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7136 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7137 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7138 } else
74cfd7ac 7139 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7140 } else
74cfd7ac 7141 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7142
74cfd7ac 7143 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7144 POSTING_READ(PCH_DREF_CONTROL);
7145 udelay(200);
7146 } else {
7147 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7148
74cfd7ac 7149 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7150
7151 /* Turn off CPU output */
74cfd7ac 7152 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7153
74cfd7ac 7154 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7155 POSTING_READ(PCH_DREF_CONTROL);
7156 udelay(200);
7157
7158 /* Turn off the SSC source */
74cfd7ac
CW
7159 val &= ~DREF_SSC_SOURCE_MASK;
7160 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7161
7162 /* Turn off SSC1 */
74cfd7ac 7163 val &= ~DREF_SSC1_ENABLE;
199e5d79 7164
74cfd7ac 7165 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7166 POSTING_READ(PCH_DREF_CONTROL);
7167 udelay(200);
7168 }
74cfd7ac
CW
7169
7170 BUG_ON(val != final);
13d83a67
JB
7171}
7172
f31f2d55 7173static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7174{
f31f2d55 7175 uint32_t tmp;
dde86e2d 7176
0ff066a9
PZ
7177 tmp = I915_READ(SOUTH_CHICKEN2);
7178 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7179 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7180
0ff066a9
PZ
7181 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7182 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7183 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7184
0ff066a9
PZ
7185 tmp = I915_READ(SOUTH_CHICKEN2);
7186 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7187 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7188
0ff066a9
PZ
7189 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7190 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7191 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7192}
7193
7194/* WaMPhyProgramming:hsw */
7195static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7196{
7197 uint32_t tmp;
dde86e2d
PZ
7198
7199 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7200 tmp &= ~(0xFF << 24);
7201 tmp |= (0x12 << 24);
7202 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7203
dde86e2d
PZ
7204 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7205 tmp |= (1 << 11);
7206 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7207
7208 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7209 tmp |= (1 << 11);
7210 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7211
dde86e2d
PZ
7212 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7213 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7214 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7215
7216 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7217 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7218 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7219
0ff066a9
PZ
7220 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7221 tmp &= ~(7 << 13);
7222 tmp |= (5 << 13);
7223 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7224
0ff066a9
PZ
7225 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7226 tmp &= ~(7 << 13);
7227 tmp |= (5 << 13);
7228 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7229
7230 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7231 tmp &= ~0xFF;
7232 tmp |= 0x1C;
7233 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7234
7235 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7236 tmp &= ~0xFF;
7237 tmp |= 0x1C;
7238 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7239
7240 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7241 tmp &= ~(0xFF << 16);
7242 tmp |= (0x1C << 16);
7243 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7244
7245 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7246 tmp &= ~(0xFF << 16);
7247 tmp |= (0x1C << 16);
7248 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7249
0ff066a9
PZ
7250 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7251 tmp |= (1 << 27);
7252 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7253
0ff066a9
PZ
7254 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7255 tmp |= (1 << 27);
7256 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7257
0ff066a9
PZ
7258 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7259 tmp &= ~(0xF << 28);
7260 tmp |= (4 << 28);
7261 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7262
0ff066a9
PZ
7263 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7264 tmp &= ~(0xF << 28);
7265 tmp |= (4 << 28);
7266 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7267}
7268
2fa86a1f
PZ
7269/* Implements 3 different sequences from BSpec chapter "Display iCLK
7270 * Programming" based on the parameters passed:
7271 * - Sequence to enable CLKOUT_DP
7272 * - Sequence to enable CLKOUT_DP without spread
7273 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7274 */
7275static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7276 bool with_fdi)
f31f2d55
PZ
7277{
7278 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
7279 uint32_t reg, tmp;
7280
7281 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7282 with_spread = true;
7283 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7284 with_fdi, "LP PCH doesn't have FDI\n"))
7285 with_fdi = false;
f31f2d55
PZ
7286
7287 mutex_lock(&dev_priv->dpio_lock);
7288
7289 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7290 tmp &= ~SBI_SSCCTL_DISABLE;
7291 tmp |= SBI_SSCCTL_PATHALT;
7292 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7293
7294 udelay(24);
7295
2fa86a1f
PZ
7296 if (with_spread) {
7297 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7298 tmp &= ~SBI_SSCCTL_PATHALT;
7299 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7300
2fa86a1f
PZ
7301 if (with_fdi) {
7302 lpt_reset_fdi_mphy(dev_priv);
7303 lpt_program_fdi_mphy(dev_priv);
7304 }
7305 }
dde86e2d 7306
2fa86a1f
PZ
7307 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7308 SBI_GEN0 : SBI_DBUFF0;
7309 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7310 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7311 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
7312
7313 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
7314}
7315
47701c3b
PZ
7316/* Sequence to disable CLKOUT_DP */
7317static void lpt_disable_clkout_dp(struct drm_device *dev)
7318{
7319 struct drm_i915_private *dev_priv = dev->dev_private;
7320 uint32_t reg, tmp;
7321
7322 mutex_lock(&dev_priv->dpio_lock);
7323
7324 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7325 SBI_GEN0 : SBI_DBUFF0;
7326 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7327 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7328 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7329
7330 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7331 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7332 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7333 tmp |= SBI_SSCCTL_PATHALT;
7334 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7335 udelay(32);
7336 }
7337 tmp |= SBI_SSCCTL_DISABLE;
7338 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7339 }
7340
7341 mutex_unlock(&dev_priv->dpio_lock);
7342}
7343
bf8fa3d3
PZ
7344static void lpt_init_pch_refclk(struct drm_device *dev)
7345{
bf8fa3d3
PZ
7346 struct intel_encoder *encoder;
7347 bool has_vga = false;
7348
b2784e15 7349 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
7350 switch (encoder->type) {
7351 case INTEL_OUTPUT_ANALOG:
7352 has_vga = true;
7353 break;
6847d71b
PZ
7354 default:
7355 break;
bf8fa3d3
PZ
7356 }
7357 }
7358
47701c3b
PZ
7359 if (has_vga)
7360 lpt_enable_clkout_dp(dev, true, true);
7361 else
7362 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
7363}
7364
dde86e2d
PZ
7365/*
7366 * Initialize reference clocks when the driver loads
7367 */
7368void intel_init_pch_refclk(struct drm_device *dev)
7369{
7370 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7371 ironlake_init_pch_refclk(dev);
7372 else if (HAS_PCH_LPT(dev))
7373 lpt_init_pch_refclk(dev);
7374}
7375
d9d444cb
JB
7376static int ironlake_get_refclk(struct drm_crtc *crtc)
7377{
7378 struct drm_device *dev = crtc->dev;
7379 struct drm_i915_private *dev_priv = dev->dev_private;
7380 struct intel_encoder *encoder;
d9d444cb
JB
7381 int num_connectors = 0;
7382 bool is_lvds = false;
7383
d0737e1d
ACO
7384 for_each_intel_encoder(dev, encoder) {
7385 if (encoder->new_crtc != to_intel_crtc(crtc))
7386 continue;
7387
d9d444cb
JB
7388 switch (encoder->type) {
7389 case INTEL_OUTPUT_LVDS:
7390 is_lvds = true;
7391 break;
6847d71b
PZ
7392 default:
7393 break;
d9d444cb
JB
7394 }
7395 num_connectors++;
7396 }
7397
7398 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 7399 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 7400 dev_priv->vbt.lvds_ssc_freq);
e91e941b 7401 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
7402 }
7403
7404 return 120000;
7405}
7406
6ff93609 7407static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 7408{
c8203565 7409 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
7410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7411 int pipe = intel_crtc->pipe;
c8203565
PZ
7412 uint32_t val;
7413
78114071 7414 val = 0;
c8203565 7415
6e3c9717 7416 switch (intel_crtc->config->pipe_bpp) {
c8203565 7417 case 18:
dfd07d72 7418 val |= PIPECONF_6BPC;
c8203565
PZ
7419 break;
7420 case 24:
dfd07d72 7421 val |= PIPECONF_8BPC;
c8203565
PZ
7422 break;
7423 case 30:
dfd07d72 7424 val |= PIPECONF_10BPC;
c8203565
PZ
7425 break;
7426 case 36:
dfd07d72 7427 val |= PIPECONF_12BPC;
c8203565
PZ
7428 break;
7429 default:
cc769b62
PZ
7430 /* Case prevented by intel_choose_pipe_bpp_dither. */
7431 BUG();
c8203565
PZ
7432 }
7433
6e3c9717 7434 if (intel_crtc->config->dither)
c8203565
PZ
7435 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7436
6e3c9717 7437 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
7438 val |= PIPECONF_INTERLACED_ILK;
7439 else
7440 val |= PIPECONF_PROGRESSIVE;
7441
6e3c9717 7442 if (intel_crtc->config->limited_color_range)
3685a8f3 7443 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 7444
c8203565
PZ
7445 I915_WRITE(PIPECONF(pipe), val);
7446 POSTING_READ(PIPECONF(pipe));
7447}
7448
86d3efce
VS
7449/*
7450 * Set up the pipe CSC unit.
7451 *
7452 * Currently only full range RGB to limited range RGB conversion
7453 * is supported, but eventually this should handle various
7454 * RGB<->YCbCr scenarios as well.
7455 */
50f3b016 7456static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
7457{
7458 struct drm_device *dev = crtc->dev;
7459 struct drm_i915_private *dev_priv = dev->dev_private;
7460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7461 int pipe = intel_crtc->pipe;
7462 uint16_t coeff = 0x7800; /* 1.0 */
7463
7464 /*
7465 * TODO: Check what kind of values actually come out of the pipe
7466 * with these coeff/postoff values and adjust to get the best
7467 * accuracy. Perhaps we even need to take the bpc value into
7468 * consideration.
7469 */
7470
6e3c9717 7471 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7472 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7473
7474 /*
7475 * GY/GU and RY/RU should be the other way around according
7476 * to BSpec, but reality doesn't agree. Just set them up in
7477 * a way that results in the correct picture.
7478 */
7479 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7480 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7481
7482 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7483 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7484
7485 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7486 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7487
7488 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7489 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7490 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7491
7492 if (INTEL_INFO(dev)->gen > 6) {
7493 uint16_t postoff = 0;
7494
6e3c9717 7495 if (intel_crtc->config->limited_color_range)
32cf0cb0 7496 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7497
7498 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7499 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7500 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7501
7502 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7503 } else {
7504 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7505
6e3c9717 7506 if (intel_crtc->config->limited_color_range)
86d3efce
VS
7507 mode |= CSC_BLACK_SCREEN_OFFSET;
7508
7509 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7510 }
7511}
7512
6ff93609 7513static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7514{
756f85cf
PZ
7515 struct drm_device *dev = crtc->dev;
7516 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7518 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7519 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
7520 uint32_t val;
7521
3eff4faa 7522 val = 0;
ee2b0b38 7523
6e3c9717 7524 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
7525 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7526
6e3c9717 7527 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7528 val |= PIPECONF_INTERLACED_ILK;
7529 else
7530 val |= PIPECONF_PROGRESSIVE;
7531
702e7a56
PZ
7532 I915_WRITE(PIPECONF(cpu_transcoder), val);
7533 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7534
7535 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7536 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7537
3cdf122c 7538 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7539 val = 0;
7540
6e3c9717 7541 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
7542 case 18:
7543 val |= PIPEMISC_DITHER_6_BPC;
7544 break;
7545 case 24:
7546 val |= PIPEMISC_DITHER_8_BPC;
7547 break;
7548 case 30:
7549 val |= PIPEMISC_DITHER_10_BPC;
7550 break;
7551 case 36:
7552 val |= PIPEMISC_DITHER_12_BPC;
7553 break;
7554 default:
7555 /* Case prevented by pipe_config_set_bpp. */
7556 BUG();
7557 }
7558
6e3c9717 7559 if (intel_crtc->config->dither)
756f85cf
PZ
7560 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7561
7562 I915_WRITE(PIPEMISC(pipe), val);
7563 }
ee2b0b38
PZ
7564}
7565
6591c6e4 7566static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 7567 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
7568 intel_clock_t *clock,
7569 bool *has_reduced_clock,
7570 intel_clock_t *reduced_clock)
7571{
7572 struct drm_device *dev = crtc->dev;
7573 struct drm_i915_private *dev_priv = dev->dev_private;
a919ff14 7574 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6591c6e4 7575 int refclk;
d4906093 7576 const intel_limit_t *limit;
a16af721 7577 bool ret, is_lvds = false;
79e53945 7578
d0737e1d 7579 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
79e53945 7580
d9d444cb 7581 refclk = ironlake_get_refclk(crtc);
79e53945 7582
d4906093
ML
7583 /*
7584 * Returns a set of divisors for the desired target clock with the given
7585 * refclk, or FALSE. The returned values represent the clock equation:
7586 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7587 */
409ee761 7588 limit = intel_limit(intel_crtc, refclk);
a919ff14 7589 ret = dev_priv->display.find_dpll(limit, intel_crtc,
190f68c5 7590 crtc_state->port_clock,
ee9300bb 7591 refclk, NULL, clock);
6591c6e4
PZ
7592 if (!ret)
7593 return false;
cda4b7d3 7594
ddc9003c 7595 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7596 /*
7597 * Ensure we match the reduced clock's P to the target clock.
7598 * If the clocks don't match, we can't switch the display clock
7599 * by using the FP0/FP1. In such case we will disable the LVDS
7600 * downclock feature.
7601 */
ee9300bb 7602 *has_reduced_clock =
a919ff14 7603 dev_priv->display.find_dpll(limit, intel_crtc,
ee9300bb
DV
7604 dev_priv->lvds_downclock,
7605 refclk, clock,
7606 reduced_clock);
652c393a 7607 }
61e9653f 7608
6591c6e4
PZ
7609 return true;
7610}
7611
d4b1931c
PZ
7612int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7613{
7614 /*
7615 * Account for spread spectrum to avoid
7616 * oversubscribing the link. Max center spread
7617 * is 2.5%; use 5% for safety's sake.
7618 */
7619 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7620 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7621}
7622
7429e9d4 7623static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7624{
7429e9d4 7625 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7626}
7627
de13a2e3 7628static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 7629 struct intel_crtc_state *crtc_state,
7429e9d4 7630 u32 *fp,
9a7c7890 7631 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7632{
de13a2e3 7633 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7634 struct drm_device *dev = crtc->dev;
7635 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7636 struct intel_encoder *intel_encoder;
7637 uint32_t dpll;
6cc5f341 7638 int factor, num_connectors = 0;
09ede541 7639 bool is_lvds = false, is_sdvo = false;
79e53945 7640
d0737e1d
ACO
7641 for_each_intel_encoder(dev, intel_encoder) {
7642 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7643 continue;
7644
de13a2e3 7645 switch (intel_encoder->type) {
79e53945
JB
7646 case INTEL_OUTPUT_LVDS:
7647 is_lvds = true;
7648 break;
7649 case INTEL_OUTPUT_SDVO:
7d57382e 7650 case INTEL_OUTPUT_HDMI:
79e53945 7651 is_sdvo = true;
79e53945 7652 break;
6847d71b
PZ
7653 default:
7654 break;
79e53945 7655 }
43565a06 7656
c751ce4f 7657 num_connectors++;
79e53945 7658 }
79e53945 7659
c1858123 7660 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7661 factor = 21;
7662 if (is_lvds) {
7663 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7664 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7665 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7666 factor = 25;
190f68c5 7667 } else if (crtc_state->sdvo_tv_clock)
8febb297 7668 factor = 20;
c1858123 7669
190f68c5 7670 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 7671 *fp |= FP_CB_TUNE;
2c07245f 7672
9a7c7890
DV
7673 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7674 *fp2 |= FP_CB_TUNE;
7675
5eddb70b 7676 dpll = 0;
2c07245f 7677
a07d6787
EA
7678 if (is_lvds)
7679 dpll |= DPLLB_MODE_LVDS;
7680 else
7681 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7682
190f68c5 7683 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 7684 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7685
7686 if (is_sdvo)
4a33e48d 7687 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 7688 if (crtc_state->has_dp_encoder)
4a33e48d 7689 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7690
a07d6787 7691 /* compute bitmask from p1 value */
190f68c5 7692 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7693 /* also FPA1 */
190f68c5 7694 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7695
190f68c5 7696 switch (crtc_state->dpll.p2) {
a07d6787
EA
7697 case 5:
7698 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7699 break;
7700 case 7:
7701 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7702 break;
7703 case 10:
7704 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7705 break;
7706 case 14:
7707 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7708 break;
79e53945
JB
7709 }
7710
b4c09f3b 7711 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7712 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7713 else
7714 dpll |= PLL_REF_INPUT_DREFCLK;
7715
959e16d6 7716 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7717}
7718
190f68c5
ACO
7719static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7720 struct intel_crtc_state *crtc_state)
de13a2e3 7721{
c7653199 7722 struct drm_device *dev = crtc->base.dev;
de13a2e3 7723 intel_clock_t clock, reduced_clock;
cbbab5bd 7724 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7725 bool ok, has_reduced_clock = false;
8b47047b 7726 bool is_lvds = false;
e2b78267 7727 struct intel_shared_dpll *pll;
de13a2e3 7728
409ee761 7729 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 7730
5dc5298b
PZ
7731 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7732 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7733
190f68c5 7734 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 7735 &has_reduced_clock, &reduced_clock);
190f68c5 7736 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
7737 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7738 return -EINVAL;
79e53945 7739 }
f47709a9 7740 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7741 if (!crtc_state->clock_set) {
7742 crtc_state->dpll.n = clock.n;
7743 crtc_state->dpll.m1 = clock.m1;
7744 crtc_state->dpll.m2 = clock.m2;
7745 crtc_state->dpll.p1 = clock.p1;
7746 crtc_state->dpll.p2 = clock.p2;
f47709a9 7747 }
79e53945 7748
5dc5298b 7749 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
7750 if (crtc_state->has_pch_encoder) {
7751 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 7752 if (has_reduced_clock)
7429e9d4 7753 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7754
190f68c5 7755 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
7756 &fp, &reduced_clock,
7757 has_reduced_clock ? &fp2 : NULL);
7758
190f68c5
ACO
7759 crtc_state->dpll_hw_state.dpll = dpll;
7760 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 7761 if (has_reduced_clock)
190f68c5 7762 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 7763 else
190f68c5 7764 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 7765
190f68c5 7766 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 7767 if (pll == NULL) {
84f44ce7 7768 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 7769 pipe_name(crtc->pipe));
4b645f14
JB
7770 return -EINVAL;
7771 }
3fb37703 7772 }
79e53945 7773
d330a953 7774 if (is_lvds && has_reduced_clock && i915.powersave)
c7653199 7775 crtc->lowfreq_avail = true;
bcd644e0 7776 else
c7653199 7777 crtc->lowfreq_avail = false;
e2b78267 7778
c8f7a0db 7779 return 0;
79e53945
JB
7780}
7781
eb14cb74
VS
7782static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7783 struct intel_link_m_n *m_n)
7784{
7785 struct drm_device *dev = crtc->base.dev;
7786 struct drm_i915_private *dev_priv = dev->dev_private;
7787 enum pipe pipe = crtc->pipe;
7788
7789 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7790 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7791 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7792 & ~TU_SIZE_MASK;
7793 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7794 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7795 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7796}
7797
7798static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7799 enum transcoder transcoder,
b95af8be
VK
7800 struct intel_link_m_n *m_n,
7801 struct intel_link_m_n *m2_n2)
72419203
DV
7802{
7803 struct drm_device *dev = crtc->base.dev;
7804 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7805 enum pipe pipe = crtc->pipe;
72419203 7806
eb14cb74
VS
7807 if (INTEL_INFO(dev)->gen >= 5) {
7808 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7809 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7810 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7811 & ~TU_SIZE_MASK;
7812 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7813 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7814 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7815 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7816 * gen < 8) and if DRRS is supported (to make sure the
7817 * registers are not unnecessarily read).
7818 */
7819 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 7820 crtc->config->has_drrs) {
b95af8be
VK
7821 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7822 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7823 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7824 & ~TU_SIZE_MASK;
7825 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7826 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7827 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7828 }
eb14cb74
VS
7829 } else {
7830 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7831 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7832 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7833 & ~TU_SIZE_MASK;
7834 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7835 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7836 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7837 }
7838}
7839
7840void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 7841 struct intel_crtc_state *pipe_config)
eb14cb74 7842{
681a8504 7843 if (pipe_config->has_pch_encoder)
eb14cb74
VS
7844 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7845 else
7846 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7847 &pipe_config->dp_m_n,
7848 &pipe_config->dp_m2_n2);
eb14cb74 7849}
72419203 7850
eb14cb74 7851static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 7852 struct intel_crtc_state *pipe_config)
eb14cb74
VS
7853{
7854 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7855 &pipe_config->fdi_m_n, NULL);
72419203
DV
7856}
7857
bd2e244f 7858static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7859 struct intel_crtc_state *pipe_config)
bd2e244f
JB
7860{
7861 struct drm_device *dev = crtc->base.dev;
7862 struct drm_i915_private *dev_priv = dev->dev_private;
7863 uint32_t tmp;
7864
7865 tmp = I915_READ(PS_CTL(crtc->pipe));
7866
7867 if (tmp & PS_ENABLE) {
7868 pipe_config->pch_pfit.enabled = true;
7869 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7870 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7871 }
7872}
7873
5724dbd1
DL
7874static void
7875skylake_get_initial_plane_config(struct intel_crtc *crtc,
7876 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
7877{
7878 struct drm_device *dev = crtc->base.dev;
7879 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 7880 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
7881 int pipe = crtc->pipe;
7882 int fourcc, pixel_format;
6761dd31 7883 unsigned int aligned_height;
bc8d7dff 7884 struct drm_framebuffer *fb;
1b842c89 7885 struct intel_framebuffer *intel_fb;
bc8d7dff 7886
d9806c9f 7887 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7888 if (!intel_fb) {
bc8d7dff
DL
7889 DRM_DEBUG_KMS("failed to alloc fb\n");
7890 return;
7891 }
7892
1b842c89
DL
7893 fb = &intel_fb->base;
7894
bc8d7dff 7895 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
7896 if (!(val & PLANE_CTL_ENABLE))
7897 goto error;
7898
bc8d7dff
DL
7899 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7900 fourcc = skl_format_to_fourcc(pixel_format,
7901 val & PLANE_CTL_ORDER_RGBX,
7902 val & PLANE_CTL_ALPHA_MASK);
7903 fb->pixel_format = fourcc;
7904 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7905
40f46283
DL
7906 tiling = val & PLANE_CTL_TILED_MASK;
7907 switch (tiling) {
7908 case PLANE_CTL_TILED_LINEAR:
7909 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
7910 break;
7911 case PLANE_CTL_TILED_X:
7912 plane_config->tiling = I915_TILING_X;
7913 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7914 break;
7915 case PLANE_CTL_TILED_Y:
7916 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
7917 break;
7918 case PLANE_CTL_TILED_YF:
7919 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
7920 break;
7921 default:
7922 MISSING_CASE(tiling);
7923 goto error;
7924 }
7925
bc8d7dff
DL
7926 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7927 plane_config->base = base;
7928
7929 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7930
7931 val = I915_READ(PLANE_SIZE(pipe, 0));
7932 fb->height = ((val >> 16) & 0xfff) + 1;
7933 fb->width = ((val >> 0) & 0x1fff) + 1;
7934
7935 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
7936 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
7937 fb->pixel_format);
bc8d7dff
DL
7938 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7939
7940 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7941 fb->pixel_format,
7942 fb->modifier[0]);
bc8d7dff 7943
f37b5c2b 7944 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
7945
7946 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7947 pipe_name(pipe), fb->width, fb->height,
7948 fb->bits_per_pixel, base, fb->pitches[0],
7949 plane_config->size);
7950
2d14030b 7951 plane_config->fb = intel_fb;
bc8d7dff
DL
7952 return;
7953
7954error:
7955 kfree(fb);
7956}
7957
2fa2fe9a 7958static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7959 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7960{
7961 struct drm_device *dev = crtc->base.dev;
7962 struct drm_i915_private *dev_priv = dev->dev_private;
7963 uint32_t tmp;
7964
7965 tmp = I915_READ(PF_CTL(crtc->pipe));
7966
7967 if (tmp & PF_ENABLE) {
fd4daa9c 7968 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7969 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7970 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7971
7972 /* We currently do not free assignements of panel fitters on
7973 * ivb/hsw (since we don't use the higher upscaling modes which
7974 * differentiates them) so just WARN about this case for now. */
7975 if (IS_GEN7(dev)) {
7976 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7977 PF_PIPE_SEL_IVB(crtc->pipe));
7978 }
2fa2fe9a 7979 }
79e53945
JB
7980}
7981
5724dbd1
DL
7982static void
7983ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7984 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
7985{
7986 struct drm_device *dev = crtc->base.dev;
7987 struct drm_i915_private *dev_priv = dev->dev_private;
7988 u32 val, base, offset;
aeee5a49 7989 int pipe = crtc->pipe;
4c6baa59 7990 int fourcc, pixel_format;
6761dd31 7991 unsigned int aligned_height;
b113d5ee 7992 struct drm_framebuffer *fb;
1b842c89 7993 struct intel_framebuffer *intel_fb;
4c6baa59 7994
42a7b088
DL
7995 val = I915_READ(DSPCNTR(pipe));
7996 if (!(val & DISPLAY_PLANE_ENABLE))
7997 return;
7998
d9806c9f 7999 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8000 if (!intel_fb) {
4c6baa59
JB
8001 DRM_DEBUG_KMS("failed to alloc fb\n");
8002 return;
8003 }
8004
1b842c89
DL
8005 fb = &intel_fb->base;
8006
18c5247e
DV
8007 if (INTEL_INFO(dev)->gen >= 4) {
8008 if (val & DISPPLANE_TILED) {
49af449b 8009 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8010 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8011 }
8012 }
4c6baa59
JB
8013
8014 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8015 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8016 fb->pixel_format = fourcc;
8017 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8018
aeee5a49 8019 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8020 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8021 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8022 } else {
49af449b 8023 if (plane_config->tiling)
aeee5a49 8024 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8025 else
aeee5a49 8026 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8027 }
8028 plane_config->base = base;
8029
8030 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8031 fb->width = ((val >> 16) & 0xfff) + 1;
8032 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8033
8034 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8035 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8036
b113d5ee 8037 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8038 fb->pixel_format,
8039 fb->modifier[0]);
4c6baa59 8040
f37b5c2b 8041 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8042
2844a921
DL
8043 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8044 pipe_name(pipe), fb->width, fb->height,
8045 fb->bits_per_pixel, base, fb->pitches[0],
8046 plane_config->size);
b113d5ee 8047
2d14030b 8048 plane_config->fb = intel_fb;
4c6baa59
JB
8049}
8050
0e8ffe1b 8051static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8052 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8053{
8054 struct drm_device *dev = crtc->base.dev;
8055 struct drm_i915_private *dev_priv = dev->dev_private;
8056 uint32_t tmp;
8057
f458ebbc
DV
8058 if (!intel_display_power_is_enabled(dev_priv,
8059 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8060 return false;
8061
e143a21c 8062 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8063 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8064
0e8ffe1b
DV
8065 tmp = I915_READ(PIPECONF(crtc->pipe));
8066 if (!(tmp & PIPECONF_ENABLE))
8067 return false;
8068
42571aef
VS
8069 switch (tmp & PIPECONF_BPC_MASK) {
8070 case PIPECONF_6BPC:
8071 pipe_config->pipe_bpp = 18;
8072 break;
8073 case PIPECONF_8BPC:
8074 pipe_config->pipe_bpp = 24;
8075 break;
8076 case PIPECONF_10BPC:
8077 pipe_config->pipe_bpp = 30;
8078 break;
8079 case PIPECONF_12BPC:
8080 pipe_config->pipe_bpp = 36;
8081 break;
8082 default:
8083 break;
8084 }
8085
b5a9fa09
DV
8086 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8087 pipe_config->limited_color_range = true;
8088
ab9412ba 8089 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8090 struct intel_shared_dpll *pll;
8091
88adfff1
DV
8092 pipe_config->has_pch_encoder = true;
8093
627eb5a3
DV
8094 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8095 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8096 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8097
8098 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8099
c0d43d62 8100 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8101 pipe_config->shared_dpll =
8102 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8103 } else {
8104 tmp = I915_READ(PCH_DPLL_SEL);
8105 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8107 else
8108 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8109 }
66e985c0
DV
8110
8111 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8112
8113 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8114 &pipe_config->dpll_hw_state));
c93f54cf
DV
8115
8116 tmp = pipe_config->dpll_hw_state.dpll;
8117 pipe_config->pixel_multiplier =
8118 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8119 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8120
8121 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8122 } else {
8123 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8124 }
8125
1bd1bd80
DV
8126 intel_get_pipe_timings(crtc, pipe_config);
8127
2fa2fe9a
DV
8128 ironlake_get_pfit_config(crtc, pipe_config);
8129
0e8ffe1b
DV
8130 return true;
8131}
8132
be256dc7
PZ
8133static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8134{
8135 struct drm_device *dev = dev_priv->dev;
be256dc7 8136 struct intel_crtc *crtc;
be256dc7 8137
d3fcc808 8138 for_each_intel_crtc(dev, crtc)
e2c719b7 8139 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8140 pipe_name(crtc->pipe));
8141
e2c719b7
RC
8142 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8143 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8144 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8145 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8146 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8147 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8148 "CPU PWM1 enabled\n");
c5107b87 8149 if (IS_HASWELL(dev))
e2c719b7 8150 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8151 "CPU PWM2 enabled\n");
e2c719b7 8152 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8153 "PCH PWM1 enabled\n");
e2c719b7 8154 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8155 "Utility pin enabled\n");
e2c719b7 8156 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8157
9926ada1
PZ
8158 /*
8159 * In theory we can still leave IRQs enabled, as long as only the HPD
8160 * interrupts remain enabled. We used to check for that, but since it's
8161 * gen-specific and since we only disable LCPLL after we fully disable
8162 * the interrupts, the check below should be enough.
8163 */
e2c719b7 8164 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8165}
8166
9ccd5aeb
PZ
8167static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8168{
8169 struct drm_device *dev = dev_priv->dev;
8170
8171 if (IS_HASWELL(dev))
8172 return I915_READ(D_COMP_HSW);
8173 else
8174 return I915_READ(D_COMP_BDW);
8175}
8176
3c4c9b81
PZ
8177static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8178{
8179 struct drm_device *dev = dev_priv->dev;
8180
8181 if (IS_HASWELL(dev)) {
8182 mutex_lock(&dev_priv->rps.hw_lock);
8183 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8184 val))
f475dadf 8185 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8186 mutex_unlock(&dev_priv->rps.hw_lock);
8187 } else {
9ccd5aeb
PZ
8188 I915_WRITE(D_COMP_BDW, val);
8189 POSTING_READ(D_COMP_BDW);
3c4c9b81 8190 }
be256dc7
PZ
8191}
8192
8193/*
8194 * This function implements pieces of two sequences from BSpec:
8195 * - Sequence for display software to disable LCPLL
8196 * - Sequence for display software to allow package C8+
8197 * The steps implemented here are just the steps that actually touch the LCPLL
8198 * register. Callers should take care of disabling all the display engine
8199 * functions, doing the mode unset, fixing interrupts, etc.
8200 */
6ff58d53
PZ
8201static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8202 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8203{
8204 uint32_t val;
8205
8206 assert_can_disable_lcpll(dev_priv);
8207
8208 val = I915_READ(LCPLL_CTL);
8209
8210 if (switch_to_fclk) {
8211 val |= LCPLL_CD_SOURCE_FCLK;
8212 I915_WRITE(LCPLL_CTL, val);
8213
8214 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8215 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8216 DRM_ERROR("Switching to FCLK failed\n");
8217
8218 val = I915_READ(LCPLL_CTL);
8219 }
8220
8221 val |= LCPLL_PLL_DISABLE;
8222 I915_WRITE(LCPLL_CTL, val);
8223 POSTING_READ(LCPLL_CTL);
8224
8225 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8226 DRM_ERROR("LCPLL still locked\n");
8227
9ccd5aeb 8228 val = hsw_read_dcomp(dev_priv);
be256dc7 8229 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8230 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8231 ndelay(100);
8232
9ccd5aeb
PZ
8233 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8234 1))
be256dc7
PZ
8235 DRM_ERROR("D_COMP RCOMP still in progress\n");
8236
8237 if (allow_power_down) {
8238 val = I915_READ(LCPLL_CTL);
8239 val |= LCPLL_POWER_DOWN_ALLOW;
8240 I915_WRITE(LCPLL_CTL, val);
8241 POSTING_READ(LCPLL_CTL);
8242 }
8243}
8244
8245/*
8246 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8247 * source.
8248 */
6ff58d53 8249static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8250{
8251 uint32_t val;
8252
8253 val = I915_READ(LCPLL_CTL);
8254
8255 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8256 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8257 return;
8258
a8a8bd54
PZ
8259 /*
8260 * Make sure we're not on PC8 state before disabling PC8, otherwise
8261 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8262 */
59bad947 8263 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8264
be256dc7
PZ
8265 if (val & LCPLL_POWER_DOWN_ALLOW) {
8266 val &= ~LCPLL_POWER_DOWN_ALLOW;
8267 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8268 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8269 }
8270
9ccd5aeb 8271 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8272 val |= D_COMP_COMP_FORCE;
8273 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8274 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8275
8276 val = I915_READ(LCPLL_CTL);
8277 val &= ~LCPLL_PLL_DISABLE;
8278 I915_WRITE(LCPLL_CTL, val);
8279
8280 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8281 DRM_ERROR("LCPLL not locked yet\n");
8282
8283 if (val & LCPLL_CD_SOURCE_FCLK) {
8284 val = I915_READ(LCPLL_CTL);
8285 val &= ~LCPLL_CD_SOURCE_FCLK;
8286 I915_WRITE(LCPLL_CTL, val);
8287
8288 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8289 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8290 DRM_ERROR("Switching back to LCPLL failed\n");
8291 }
215733fa 8292
59bad947 8293 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
8294}
8295
765dab67
PZ
8296/*
8297 * Package states C8 and deeper are really deep PC states that can only be
8298 * reached when all the devices on the system allow it, so even if the graphics
8299 * device allows PC8+, it doesn't mean the system will actually get to these
8300 * states. Our driver only allows PC8+ when going into runtime PM.
8301 *
8302 * The requirements for PC8+ are that all the outputs are disabled, the power
8303 * well is disabled and most interrupts are disabled, and these are also
8304 * requirements for runtime PM. When these conditions are met, we manually do
8305 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8306 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8307 * hang the machine.
8308 *
8309 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8310 * the state of some registers, so when we come back from PC8+ we need to
8311 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8312 * need to take care of the registers kept by RC6. Notice that this happens even
8313 * if we don't put the device in PCI D3 state (which is what currently happens
8314 * because of the runtime PM support).
8315 *
8316 * For more, read "Display Sequences for Package C8" on the hardware
8317 * documentation.
8318 */
a14cb6fc 8319void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8320{
c67a470b
PZ
8321 struct drm_device *dev = dev_priv->dev;
8322 uint32_t val;
8323
c67a470b
PZ
8324 DRM_DEBUG_KMS("Enabling package C8+\n");
8325
c67a470b
PZ
8326 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8327 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8328 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8329 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8330 }
8331
8332 lpt_disable_clkout_dp(dev);
c67a470b
PZ
8333 hsw_disable_lcpll(dev_priv, true, true);
8334}
8335
a14cb6fc 8336void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
8337{
8338 struct drm_device *dev = dev_priv->dev;
8339 uint32_t val;
8340
c67a470b
PZ
8341 DRM_DEBUG_KMS("Disabling package C8+\n");
8342
8343 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
8344 lpt_init_pch_refclk(dev);
8345
8346 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8347 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8348 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8349 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8350 }
8351
8352 intel_prepare_ddi(dev);
c67a470b
PZ
8353}
8354
190f68c5
ACO
8355static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8356 struct intel_crtc_state *crtc_state)
09b4ddf9 8357{
190f68c5 8358 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 8359 return -EINVAL;
716c2e55 8360
c7653199 8361 crtc->lowfreq_avail = false;
644cef34 8362
c8f7a0db 8363 return 0;
79e53945
JB
8364}
8365
96b7dfb7
S
8366static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8367 enum port port,
5cec258b 8368 struct intel_crtc_state *pipe_config)
96b7dfb7 8369{
3148ade7 8370 u32 temp, dpll_ctl1;
96b7dfb7
S
8371
8372 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8373 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8374
8375 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
8376 case SKL_DPLL0:
8377 /*
8378 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8379 * of the shared DPLL framework and thus needs to be read out
8380 * separately
8381 */
8382 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8383 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8384 break;
96b7dfb7
S
8385 case SKL_DPLL1:
8386 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8387 break;
8388 case SKL_DPLL2:
8389 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8390 break;
8391 case SKL_DPLL3:
8392 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8393 break;
96b7dfb7
S
8394 }
8395}
8396
7d2c8175
DL
8397static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8398 enum port port,
5cec258b 8399 struct intel_crtc_state *pipe_config)
7d2c8175
DL
8400{
8401 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8402
8403 switch (pipe_config->ddi_pll_sel) {
8404 case PORT_CLK_SEL_WRPLL1:
8405 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8406 break;
8407 case PORT_CLK_SEL_WRPLL2:
8408 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8409 break;
8410 }
8411}
8412
26804afd 8413static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 8414 struct intel_crtc_state *pipe_config)
26804afd
DV
8415{
8416 struct drm_device *dev = crtc->base.dev;
8417 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 8418 struct intel_shared_dpll *pll;
26804afd
DV
8419 enum port port;
8420 uint32_t tmp;
8421
8422 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8423
8424 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8425
96b7dfb7
S
8426 if (IS_SKYLAKE(dev))
8427 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8428 else
8429 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 8430
d452c5b6
DV
8431 if (pipe_config->shared_dpll >= 0) {
8432 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8433
8434 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8435 &pipe_config->dpll_hw_state));
8436 }
8437
26804afd
DV
8438 /*
8439 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8440 * DDI E. So just check whether this pipe is wired to DDI E and whether
8441 * the PCH transcoder is on.
8442 */
ca370455
DL
8443 if (INTEL_INFO(dev)->gen < 9 &&
8444 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
8445 pipe_config->has_pch_encoder = true;
8446
8447 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8448 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8449 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8450
8451 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8452 }
8453}
8454
0e8ffe1b 8455static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8456 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8457{
8458 struct drm_device *dev = crtc->base.dev;
8459 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 8460 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
8461 uint32_t tmp;
8462
f458ebbc 8463 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
8464 POWER_DOMAIN_PIPE(crtc->pipe)))
8465 return false;
8466
e143a21c 8467 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
8468 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8469
eccb140b
DV
8470 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8471 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8472 enum pipe trans_edp_pipe;
8473 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8474 default:
8475 WARN(1, "unknown pipe linked to edp transcoder\n");
8476 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8477 case TRANS_DDI_EDP_INPUT_A_ON:
8478 trans_edp_pipe = PIPE_A;
8479 break;
8480 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8481 trans_edp_pipe = PIPE_B;
8482 break;
8483 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8484 trans_edp_pipe = PIPE_C;
8485 break;
8486 }
8487
8488 if (trans_edp_pipe == crtc->pipe)
8489 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8490 }
8491
f458ebbc 8492 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 8493 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
8494 return false;
8495
eccb140b 8496 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
8497 if (!(tmp & PIPECONF_ENABLE))
8498 return false;
8499
26804afd 8500 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 8501
1bd1bd80
DV
8502 intel_get_pipe_timings(crtc, pipe_config);
8503
2fa2fe9a 8504 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f
JB
8505 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8506 if (IS_SKYLAKE(dev))
8507 skylake_get_pfit_config(crtc, pipe_config);
8508 else
8509 ironlake_get_pfit_config(crtc, pipe_config);
8510 }
88adfff1 8511
e59150dc
JB
8512 if (IS_HASWELL(dev))
8513 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8514 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 8515
ebb69c95
CT
8516 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8517 pipe_config->pixel_multiplier =
8518 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8519 } else {
8520 pipe_config->pixel_multiplier = 1;
8521 }
6c49f241 8522
0e8ffe1b
DV
8523 return true;
8524}
8525
560b85bb
CW
8526static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8527{
8528 struct drm_device *dev = crtc->dev;
8529 struct drm_i915_private *dev_priv = dev->dev_private;
8530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8531 uint32_t cntl = 0, size = 0;
560b85bb 8532
dc41c154 8533 if (base) {
3dd512fb
MR
8534 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8535 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
8536 unsigned int stride = roundup_pow_of_two(width) * 4;
8537
8538 switch (stride) {
8539 default:
8540 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8541 width, stride);
8542 stride = 256;
8543 /* fallthrough */
8544 case 256:
8545 case 512:
8546 case 1024:
8547 case 2048:
8548 break;
4b0e333e
CW
8549 }
8550
dc41c154
VS
8551 cntl |= CURSOR_ENABLE |
8552 CURSOR_GAMMA_ENABLE |
8553 CURSOR_FORMAT_ARGB |
8554 CURSOR_STRIDE(stride);
8555
8556 size = (height << 12) | width;
4b0e333e 8557 }
560b85bb 8558
dc41c154
VS
8559 if (intel_crtc->cursor_cntl != 0 &&
8560 (intel_crtc->cursor_base != base ||
8561 intel_crtc->cursor_size != size ||
8562 intel_crtc->cursor_cntl != cntl)) {
8563 /* On these chipsets we can only modify the base/size/stride
8564 * whilst the cursor is disabled.
8565 */
8566 I915_WRITE(_CURACNTR, 0);
4b0e333e 8567 POSTING_READ(_CURACNTR);
dc41c154 8568 intel_crtc->cursor_cntl = 0;
4b0e333e 8569 }
560b85bb 8570
99d1f387 8571 if (intel_crtc->cursor_base != base) {
9db4a9c7 8572 I915_WRITE(_CURABASE, base);
99d1f387
VS
8573 intel_crtc->cursor_base = base;
8574 }
4726e0b0 8575
dc41c154
VS
8576 if (intel_crtc->cursor_size != size) {
8577 I915_WRITE(CURSIZE, size);
8578 intel_crtc->cursor_size = size;
4b0e333e 8579 }
560b85bb 8580
4b0e333e 8581 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8582 I915_WRITE(_CURACNTR, cntl);
8583 POSTING_READ(_CURACNTR);
4b0e333e 8584 intel_crtc->cursor_cntl = cntl;
560b85bb 8585 }
560b85bb
CW
8586}
8587
560b85bb 8588static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8589{
8590 struct drm_device *dev = crtc->dev;
8591 struct drm_i915_private *dev_priv = dev->dev_private;
8592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8593 int pipe = intel_crtc->pipe;
4b0e333e
CW
8594 uint32_t cntl;
8595
8596 cntl = 0;
8597 if (base) {
8598 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 8599 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
8600 case 64:
8601 cntl |= CURSOR_MODE_64_ARGB_AX;
8602 break;
8603 case 128:
8604 cntl |= CURSOR_MODE_128_ARGB_AX;
8605 break;
8606 case 256:
8607 cntl |= CURSOR_MODE_256_ARGB_AX;
8608 break;
8609 default:
3dd512fb 8610 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 8611 return;
65a21cd6 8612 }
4b0e333e 8613 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8614
8615 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8616 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8617 }
65a21cd6 8618
8e7d688b 8619 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
8620 cntl |= CURSOR_ROTATE_180;
8621
4b0e333e
CW
8622 if (intel_crtc->cursor_cntl != cntl) {
8623 I915_WRITE(CURCNTR(pipe), cntl);
8624 POSTING_READ(CURCNTR(pipe));
8625 intel_crtc->cursor_cntl = cntl;
65a21cd6 8626 }
4b0e333e 8627
65a21cd6 8628 /* and commit changes on next vblank */
5efb3e28
VS
8629 I915_WRITE(CURBASE(pipe), base);
8630 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8631
8632 intel_crtc->cursor_base = base;
65a21cd6
JB
8633}
8634
cda4b7d3 8635/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8636static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8637 bool on)
cda4b7d3
CW
8638{
8639 struct drm_device *dev = crtc->dev;
8640 struct drm_i915_private *dev_priv = dev->dev_private;
8641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8642 int pipe = intel_crtc->pipe;
3d7d6510
MR
8643 int x = crtc->cursor_x;
8644 int y = crtc->cursor_y;
d6e4db15 8645 u32 base = 0, pos = 0;
cda4b7d3 8646
d6e4db15 8647 if (on)
cda4b7d3 8648 base = intel_crtc->cursor_addr;
cda4b7d3 8649
6e3c9717 8650 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
8651 base = 0;
8652
6e3c9717 8653 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
8654 base = 0;
8655
8656 if (x < 0) {
3dd512fb 8657 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
8658 base = 0;
8659
8660 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8661 x = -x;
8662 }
8663 pos |= x << CURSOR_X_SHIFT;
8664
8665 if (y < 0) {
3dd512fb 8666 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
8667 base = 0;
8668
8669 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8670 y = -y;
8671 }
8672 pos |= y << CURSOR_Y_SHIFT;
8673
4b0e333e 8674 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8675 return;
8676
5efb3e28
VS
8677 I915_WRITE(CURPOS(pipe), pos);
8678
4398ad45
VS
8679 /* ILK+ do this automagically */
8680 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 8681 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
8682 base += (intel_crtc->base.cursor->state->crtc_h *
8683 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
8684 }
8685
8ac54669 8686 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8687 i845_update_cursor(crtc, base);
8688 else
8689 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8690}
8691
dc41c154
VS
8692static bool cursor_size_ok(struct drm_device *dev,
8693 uint32_t width, uint32_t height)
8694{
8695 if (width == 0 || height == 0)
8696 return false;
8697
8698 /*
8699 * 845g/865g are special in that they are only limited by
8700 * the width of their cursors, the height is arbitrary up to
8701 * the precision of the register. Everything else requires
8702 * square cursors, limited to a few power-of-two sizes.
8703 */
8704 if (IS_845G(dev) || IS_I865G(dev)) {
8705 if ((width & 63) != 0)
8706 return false;
8707
8708 if (width > (IS_845G(dev) ? 64 : 512))
8709 return false;
8710
8711 if (height > 1023)
8712 return false;
8713 } else {
8714 switch (width | height) {
8715 case 256:
8716 case 128:
8717 if (IS_GEN2(dev))
8718 return false;
8719 case 64:
8720 break;
8721 default:
8722 return false;
8723 }
8724 }
8725
8726 return true;
8727}
8728
79e53945 8729static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8730 u16 *blue, uint32_t start, uint32_t size)
79e53945 8731{
7203425a 8732 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8734
7203425a 8735 for (i = start; i < end; i++) {
79e53945
JB
8736 intel_crtc->lut_r[i] = red[i] >> 8;
8737 intel_crtc->lut_g[i] = green[i] >> 8;
8738 intel_crtc->lut_b[i] = blue[i] >> 8;
8739 }
8740
8741 intel_crtc_load_lut(crtc);
8742}
8743
79e53945
JB
8744/* VESA 640x480x72Hz mode to set on the pipe */
8745static struct drm_display_mode load_detect_mode = {
8746 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8747 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8748};
8749
a8bb6818
DV
8750struct drm_framebuffer *
8751__intel_framebuffer_create(struct drm_device *dev,
8752 struct drm_mode_fb_cmd2 *mode_cmd,
8753 struct drm_i915_gem_object *obj)
d2dff872
CW
8754{
8755 struct intel_framebuffer *intel_fb;
8756 int ret;
8757
8758 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8759 if (!intel_fb) {
6ccb81f2 8760 drm_gem_object_unreference(&obj->base);
d2dff872
CW
8761 return ERR_PTR(-ENOMEM);
8762 }
8763
8764 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8765 if (ret)
8766 goto err;
d2dff872
CW
8767
8768 return &intel_fb->base;
dd4916c5 8769err:
6ccb81f2 8770 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
8771 kfree(intel_fb);
8772
8773 return ERR_PTR(ret);
d2dff872
CW
8774}
8775
b5ea642a 8776static struct drm_framebuffer *
a8bb6818
DV
8777intel_framebuffer_create(struct drm_device *dev,
8778 struct drm_mode_fb_cmd2 *mode_cmd,
8779 struct drm_i915_gem_object *obj)
8780{
8781 struct drm_framebuffer *fb;
8782 int ret;
8783
8784 ret = i915_mutex_lock_interruptible(dev);
8785 if (ret)
8786 return ERR_PTR(ret);
8787 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8788 mutex_unlock(&dev->struct_mutex);
8789
8790 return fb;
8791}
8792
d2dff872
CW
8793static u32
8794intel_framebuffer_pitch_for_width(int width, int bpp)
8795{
8796 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8797 return ALIGN(pitch, 64);
8798}
8799
8800static u32
8801intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8802{
8803 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8804 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8805}
8806
8807static struct drm_framebuffer *
8808intel_framebuffer_create_for_mode(struct drm_device *dev,
8809 struct drm_display_mode *mode,
8810 int depth, int bpp)
8811{
8812 struct drm_i915_gem_object *obj;
0fed39bd 8813 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8814
8815 obj = i915_gem_alloc_object(dev,
8816 intel_framebuffer_size_for_mode(mode, bpp));
8817 if (obj == NULL)
8818 return ERR_PTR(-ENOMEM);
8819
8820 mode_cmd.width = mode->hdisplay;
8821 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8822 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8823 bpp);
5ca0c34a 8824 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8825
8826 return intel_framebuffer_create(dev, &mode_cmd, obj);
8827}
8828
8829static struct drm_framebuffer *
8830mode_fits_in_fbdev(struct drm_device *dev,
8831 struct drm_display_mode *mode)
8832{
4520f53a 8833#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8834 struct drm_i915_private *dev_priv = dev->dev_private;
8835 struct drm_i915_gem_object *obj;
8836 struct drm_framebuffer *fb;
8837
4c0e5528 8838 if (!dev_priv->fbdev)
d2dff872
CW
8839 return NULL;
8840
4c0e5528 8841 if (!dev_priv->fbdev->fb)
d2dff872
CW
8842 return NULL;
8843
4c0e5528
DV
8844 obj = dev_priv->fbdev->fb->obj;
8845 BUG_ON(!obj);
8846
8bcd4553 8847 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8848 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8849 fb->bits_per_pixel))
d2dff872
CW
8850 return NULL;
8851
01f2c773 8852 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8853 return NULL;
8854
8855 return fb;
4520f53a
DV
8856#else
8857 return NULL;
8858#endif
d2dff872
CW
8859}
8860
d2434ab7 8861bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8862 struct drm_display_mode *mode,
51fd371b
RC
8863 struct intel_load_detect_pipe *old,
8864 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8865{
8866 struct intel_crtc *intel_crtc;
d2434ab7
DV
8867 struct intel_encoder *intel_encoder =
8868 intel_attached_encoder(connector);
79e53945 8869 struct drm_crtc *possible_crtc;
4ef69c7a 8870 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8871 struct drm_crtc *crtc = NULL;
8872 struct drm_device *dev = encoder->dev;
94352cf9 8873 struct drm_framebuffer *fb;
51fd371b
RC
8874 struct drm_mode_config *config = &dev->mode_config;
8875 int ret, i = -1;
79e53945 8876
d2dff872 8877 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8878 connector->base.id, connector->name,
8e329a03 8879 encoder->base.id, encoder->name);
d2dff872 8880
51fd371b
RC
8881retry:
8882 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8883 if (ret)
8884 goto fail_unlock;
6e9f798d 8885
79e53945
JB
8886 /*
8887 * Algorithm gets a little messy:
7a5e4805 8888 *
79e53945
JB
8889 * - if the connector already has an assigned crtc, use it (but make
8890 * sure it's on first)
7a5e4805 8891 *
79e53945
JB
8892 * - try to find the first unused crtc that can drive this connector,
8893 * and use that if we find one
79e53945
JB
8894 */
8895
8896 /* See if we already have a CRTC for this connector */
8897 if (encoder->crtc) {
8898 crtc = encoder->crtc;
8261b191 8899
51fd371b 8900 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
8901 if (ret)
8902 goto fail_unlock;
8903 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
8904 if (ret)
8905 goto fail_unlock;
7b24056b 8906
24218aac 8907 old->dpms_mode = connector->dpms;
8261b191
CW
8908 old->load_detect_temp = false;
8909
8910 /* Make sure the crtc and connector are running */
24218aac
DV
8911 if (connector->dpms != DRM_MODE_DPMS_ON)
8912 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8913
7173188d 8914 return true;
79e53945
JB
8915 }
8916
8917 /* Find an unused one (if possible) */
70e1e0ec 8918 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8919 i++;
8920 if (!(encoder->possible_crtcs & (1 << i)))
8921 continue;
83d65738 8922 if (possible_crtc->state->enable)
a459249c
VS
8923 continue;
8924 /* This can occur when applying the pipe A quirk on resume. */
8925 if (to_intel_crtc(possible_crtc)->new_enabled)
8926 continue;
8927
8928 crtc = possible_crtc;
8929 break;
79e53945
JB
8930 }
8931
8932 /*
8933 * If we didn't find an unused CRTC, don't use any.
8934 */
8935 if (!crtc) {
7173188d 8936 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8937 goto fail_unlock;
79e53945
JB
8938 }
8939
51fd371b
RC
8940 ret = drm_modeset_lock(&crtc->mutex, ctx);
8941 if (ret)
4d02e2de
DV
8942 goto fail_unlock;
8943 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8944 if (ret)
51fd371b 8945 goto fail_unlock;
fc303101
DV
8946 intel_encoder->new_crtc = to_intel_crtc(crtc);
8947 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8948
8949 intel_crtc = to_intel_crtc(crtc);
412b61d8 8950 intel_crtc->new_enabled = true;
6e3c9717 8951 intel_crtc->new_config = intel_crtc->config;
24218aac 8952 old->dpms_mode = connector->dpms;
8261b191 8953 old->load_detect_temp = true;
d2dff872 8954 old->release_fb = NULL;
79e53945 8955
6492711d
CW
8956 if (!mode)
8957 mode = &load_detect_mode;
79e53945 8958
d2dff872
CW
8959 /* We need a framebuffer large enough to accommodate all accesses
8960 * that the plane may generate whilst we perform load detection.
8961 * We can not rely on the fbcon either being present (we get called
8962 * during its initialisation to detect all boot displays, or it may
8963 * not even exist) or that it is large enough to satisfy the
8964 * requested mode.
8965 */
94352cf9
DV
8966 fb = mode_fits_in_fbdev(dev, mode);
8967 if (fb == NULL) {
d2dff872 8968 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8969 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8970 old->release_fb = fb;
d2dff872
CW
8971 } else
8972 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8973 if (IS_ERR(fb)) {
d2dff872 8974 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8975 goto fail;
79e53945 8976 }
79e53945 8977
c0c36b94 8978 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8979 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8980 if (old->release_fb)
8981 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8982 goto fail;
79e53945 8983 }
9128b040 8984 crtc->primary->crtc = crtc;
7173188d 8985
79e53945 8986 /* let the connector get through one full cycle before testing */
9d0498a2 8987 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8988 return true;
412b61d8
VS
8989
8990 fail:
83d65738 8991 intel_crtc->new_enabled = crtc->state->enable;
412b61d8 8992 if (intel_crtc->new_enabled)
6e3c9717 8993 intel_crtc->new_config = intel_crtc->config;
412b61d8
VS
8994 else
8995 intel_crtc->new_config = NULL;
51fd371b
RC
8996fail_unlock:
8997 if (ret == -EDEADLK) {
8998 drm_modeset_backoff(ctx);
8999 goto retry;
9000 }
9001
412b61d8 9002 return false;
79e53945
JB
9003}
9004
d2434ab7 9005void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 9006 struct intel_load_detect_pipe *old)
79e53945 9007{
d2434ab7
DV
9008 struct intel_encoder *intel_encoder =
9009 intel_attached_encoder(connector);
4ef69c7a 9010 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9011 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9013
d2dff872 9014 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9015 connector->base.id, connector->name,
8e329a03 9016 encoder->base.id, encoder->name);
d2dff872 9017
8261b191 9018 if (old->load_detect_temp) {
fc303101
DV
9019 to_intel_connector(connector)->new_encoder = NULL;
9020 intel_encoder->new_crtc = NULL;
412b61d8
VS
9021 intel_crtc->new_enabled = false;
9022 intel_crtc->new_config = NULL;
fc303101 9023 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 9024
36206361
DV
9025 if (old->release_fb) {
9026 drm_framebuffer_unregister_private(old->release_fb);
9027 drm_framebuffer_unreference(old->release_fb);
9028 }
d2dff872 9029
0622a53c 9030 return;
79e53945
JB
9031 }
9032
c751ce4f 9033 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9034 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9035 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
9036}
9037
da4a1efa 9038static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9039 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9040{
9041 struct drm_i915_private *dev_priv = dev->dev_private;
9042 u32 dpll = pipe_config->dpll_hw_state.dpll;
9043
9044 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9045 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9046 else if (HAS_PCH_SPLIT(dev))
9047 return 120000;
9048 else if (!IS_GEN2(dev))
9049 return 96000;
9050 else
9051 return 48000;
9052}
9053
79e53945 9054/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9055static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9056 struct intel_crtc_state *pipe_config)
79e53945 9057{
f1f644dc 9058 struct drm_device *dev = crtc->base.dev;
79e53945 9059 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 9060 int pipe = pipe_config->cpu_transcoder;
293623f7 9061 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
9062 u32 fp;
9063 intel_clock_t clock;
da4a1efa 9064 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9065
9066 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9067 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9068 else
293623f7 9069 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9070
9071 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
9072 if (IS_PINEVIEW(dev)) {
9073 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9074 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9075 } else {
9076 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9077 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9078 }
9079
a6c45cf0 9080 if (!IS_GEN2(dev)) {
f2b115e6
AJ
9081 if (IS_PINEVIEW(dev))
9082 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9083 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9084 else
9085 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9086 DPLL_FPA01_P1_POST_DIV_SHIFT);
9087
9088 switch (dpll & DPLL_MODE_MASK) {
9089 case DPLLB_MODE_DAC_SERIAL:
9090 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9091 5 : 10;
9092 break;
9093 case DPLLB_MODE_LVDS:
9094 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9095 7 : 14;
9096 break;
9097 default:
28c97730 9098 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9099 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9100 return;
79e53945
JB
9101 }
9102
ac58c3f0 9103 if (IS_PINEVIEW(dev))
da4a1efa 9104 pineview_clock(refclk, &clock);
ac58c3f0 9105 else
da4a1efa 9106 i9xx_clock(refclk, &clock);
79e53945 9107 } else {
0fb58223 9108 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 9109 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9110
9111 if (is_lvds) {
9112 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9113 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9114
9115 if (lvds & LVDS_CLKB_POWER_UP)
9116 clock.p2 = 7;
9117 else
9118 clock.p2 = 14;
79e53945
JB
9119 } else {
9120 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9121 clock.p1 = 2;
9122 else {
9123 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9124 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9125 }
9126 if (dpll & PLL_P2_DIVIDE_BY_4)
9127 clock.p2 = 4;
9128 else
9129 clock.p2 = 2;
79e53945 9130 }
da4a1efa
VS
9131
9132 i9xx_clock(refclk, &clock);
79e53945
JB
9133 }
9134
18442d08
VS
9135 /*
9136 * This value includes pixel_multiplier. We will use
241bfc38 9137 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9138 * encoder's get_config() function.
9139 */
9140 pipe_config->port_clock = clock.dot;
f1f644dc
JB
9141}
9142
6878da05
VS
9143int intel_dotclock_calculate(int link_freq,
9144 const struct intel_link_m_n *m_n)
f1f644dc 9145{
f1f644dc
JB
9146 /*
9147 * The calculation for the data clock is:
1041a02f 9148 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 9149 * But we want to avoid losing precison if possible, so:
1041a02f 9150 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
9151 *
9152 * and the link clock is simpler:
1041a02f 9153 * link_clock = (m * link_clock) / n
f1f644dc
JB
9154 */
9155
6878da05
VS
9156 if (!m_n->link_n)
9157 return 0;
f1f644dc 9158
6878da05
VS
9159 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9160}
f1f644dc 9161
18442d08 9162static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 9163 struct intel_crtc_state *pipe_config)
6878da05
VS
9164{
9165 struct drm_device *dev = crtc->base.dev;
79e53945 9166
18442d08
VS
9167 /* read out port_clock from the DPLL */
9168 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9169
f1f644dc 9170 /*
18442d08 9171 * This value does not include pixel_multiplier.
241bfc38 9172 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
9173 * agree once we know their relationship in the encoder's
9174 * get_config() function.
79e53945 9175 */
2d112de7 9176 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
9177 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9178 &pipe_config->fdi_m_n);
79e53945
JB
9179}
9180
9181/** Returns the currently programmed mode of the given pipe. */
9182struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9183 struct drm_crtc *crtc)
9184{
548f245b 9185 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 9186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9187 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 9188 struct drm_display_mode *mode;
5cec258b 9189 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
9190 int htot = I915_READ(HTOTAL(cpu_transcoder));
9191 int hsync = I915_READ(HSYNC(cpu_transcoder));
9192 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9193 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9194 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9195
9196 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9197 if (!mode)
9198 return NULL;
9199
f1f644dc
JB
9200 /*
9201 * Construct a pipe_config sufficient for getting the clock info
9202 * back out of crtc_clock_get.
9203 *
9204 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9205 * to use a real value here instead.
9206 */
293623f7 9207 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 9208 pipe_config.pixel_multiplier = 1;
293623f7
VS
9209 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9210 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9211 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
9212 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9213
773ae034 9214 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
9215 mode->hdisplay = (htot & 0xffff) + 1;
9216 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9217 mode->hsync_start = (hsync & 0xffff) + 1;
9218 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9219 mode->vdisplay = (vtot & 0xffff) + 1;
9220 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9221 mode->vsync_start = (vsync & 0xffff) + 1;
9222 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9223
9224 drm_mode_set_name(mode);
79e53945
JB
9225
9226 return mode;
9227}
9228
652c393a
JB
9229static void intel_decrease_pllclock(struct drm_crtc *crtc)
9230{
9231 struct drm_device *dev = crtc->dev;
fbee40df 9232 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 9234
baff296c 9235 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9236 return;
9237
9238 if (!dev_priv->lvds_downclock_avail)
9239 return;
9240
9241 /*
9242 * Since this is called by a timer, we should never get here in
9243 * the manual case.
9244 */
9245 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
9246 int pipe = intel_crtc->pipe;
9247 int dpll_reg = DPLL(pipe);
9248 int dpll;
f6e5b160 9249
44d98a61 9250 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9251
8ac5a6d5 9252 assert_panel_unlocked(dev_priv, pipe);
652c393a 9253
dc257cf1 9254 dpll = I915_READ(dpll_reg);
652c393a
JB
9255 dpll |= DISPLAY_RATE_SELECT_FPA1;
9256 I915_WRITE(dpll_reg, dpll);
9d0498a2 9257 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9258 dpll = I915_READ(dpll_reg);
9259 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9260 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9261 }
9262
9263}
9264
f047e395
CW
9265void intel_mark_busy(struct drm_device *dev)
9266{
c67a470b
PZ
9267 struct drm_i915_private *dev_priv = dev->dev_private;
9268
f62a0076
CW
9269 if (dev_priv->mm.busy)
9270 return;
9271
43694d69 9272 intel_runtime_pm_get(dev_priv);
c67a470b 9273 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
9274 if (INTEL_INFO(dev)->gen >= 6)
9275 gen6_rps_busy(dev_priv);
f62a0076 9276 dev_priv->mm.busy = true;
f047e395
CW
9277}
9278
9279void intel_mark_idle(struct drm_device *dev)
652c393a 9280{
c67a470b 9281 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9282 struct drm_crtc *crtc;
652c393a 9283
f62a0076
CW
9284 if (!dev_priv->mm.busy)
9285 return;
9286
9287 dev_priv->mm.busy = false;
9288
d330a953 9289 if (!i915.powersave)
bb4cdd53 9290 goto out;
652c393a 9291
70e1e0ec 9292 for_each_crtc(dev, crtc) {
f4510a27 9293 if (!crtc->primary->fb)
652c393a
JB
9294 continue;
9295
725a5b54 9296 intel_decrease_pllclock(crtc);
652c393a 9297 }
b29c19b6 9298
3d13ef2e 9299 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9300 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9301
9302out:
43694d69 9303 intel_runtime_pm_put(dev_priv);
652c393a
JB
9304}
9305
f5de6e07
ACO
9306static void intel_crtc_set_state(struct intel_crtc *crtc,
9307 struct intel_crtc_state *crtc_state)
9308{
9309 kfree(crtc->config);
9310 crtc->config = crtc_state;
16f3f658 9311 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
9312}
9313
79e53945
JB
9314static void intel_crtc_destroy(struct drm_crtc *crtc)
9315{
9316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9317 struct drm_device *dev = crtc->dev;
9318 struct intel_unpin_work *work;
67e77c5a 9319
5e2d7afc 9320 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
9321 work = intel_crtc->unpin_work;
9322 intel_crtc->unpin_work = NULL;
5e2d7afc 9323 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
9324
9325 if (work) {
9326 cancel_work_sync(&work->work);
9327 kfree(work);
9328 }
79e53945 9329
f5de6e07 9330 intel_crtc_set_state(intel_crtc, NULL);
79e53945 9331 drm_crtc_cleanup(crtc);
67e77c5a 9332
79e53945
JB
9333 kfree(intel_crtc);
9334}
9335
6b95a207
KH
9336static void intel_unpin_work_fn(struct work_struct *__work)
9337{
9338 struct intel_unpin_work *work =
9339 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9340 struct drm_device *dev = work->crtc->dev;
f99d7069 9341 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9342
b4a98e57 9343 mutex_lock(&dev->struct_mutex);
82bc3b2d 9344 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 9345 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 9346
7ff0ebcc 9347 intel_fbc_update(dev);
f06cc1b9
JH
9348
9349 if (work->flip_queued_req)
146d84f0 9350 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
9351 mutex_unlock(&dev->struct_mutex);
9352
f99d7069 9353 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 9354 drm_framebuffer_unreference(work->old_fb);
f99d7069 9355
b4a98e57
CW
9356 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9357 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9358
6b95a207
KH
9359 kfree(work);
9360}
9361
1afe3e9d 9362static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9363 struct drm_crtc *crtc)
6b95a207 9364{
6b95a207
KH
9365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9366 struct intel_unpin_work *work;
6b95a207
KH
9367 unsigned long flags;
9368
9369 /* Ignore early vblank irqs */
9370 if (intel_crtc == NULL)
9371 return;
9372
f326038a
DV
9373 /*
9374 * This is called both by irq handlers and the reset code (to complete
9375 * lost pageflips) so needs the full irqsave spinlocks.
9376 */
6b95a207
KH
9377 spin_lock_irqsave(&dev->event_lock, flags);
9378 work = intel_crtc->unpin_work;
e7d841ca
CW
9379
9380 /* Ensure we don't miss a work->pending update ... */
9381 smp_rmb();
9382
9383 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9384 spin_unlock_irqrestore(&dev->event_lock, flags);
9385 return;
9386 }
9387
d6bbafa1 9388 page_flip_completed(intel_crtc);
0af7e4df 9389
6b95a207 9390 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9391}
9392
1afe3e9d
JB
9393void intel_finish_page_flip(struct drm_device *dev, int pipe)
9394{
fbee40df 9395 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9396 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9397
49b14a5c 9398 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9399}
9400
9401void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9402{
fbee40df 9403 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9404 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9405
49b14a5c 9406 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9407}
9408
75f7f3ec
VS
9409/* Is 'a' after or equal to 'b'? */
9410static bool g4x_flip_count_after_eq(u32 a, u32 b)
9411{
9412 return !((a - b) & 0x80000000);
9413}
9414
9415static bool page_flip_finished(struct intel_crtc *crtc)
9416{
9417 struct drm_device *dev = crtc->base.dev;
9418 struct drm_i915_private *dev_priv = dev->dev_private;
9419
bdfa7542
VS
9420 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9421 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9422 return true;
9423
75f7f3ec
VS
9424 /*
9425 * The relevant registers doen't exist on pre-ctg.
9426 * As the flip done interrupt doesn't trigger for mmio
9427 * flips on gmch platforms, a flip count check isn't
9428 * really needed there. But since ctg has the registers,
9429 * include it in the check anyway.
9430 */
9431 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9432 return true;
9433
9434 /*
9435 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9436 * used the same base address. In that case the mmio flip might
9437 * have completed, but the CS hasn't even executed the flip yet.
9438 *
9439 * A flip count check isn't enough as the CS might have updated
9440 * the base address just after start of vblank, but before we
9441 * managed to process the interrupt. This means we'd complete the
9442 * CS flip too soon.
9443 *
9444 * Combining both checks should get us a good enough result. It may
9445 * still happen that the CS flip has been executed, but has not
9446 * yet actually completed. But in case the base address is the same
9447 * anyway, we don't really care.
9448 */
9449 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9450 crtc->unpin_work->gtt_offset &&
9451 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9452 crtc->unpin_work->flip_count);
9453}
9454
6b95a207
KH
9455void intel_prepare_page_flip(struct drm_device *dev, int plane)
9456{
fbee40df 9457 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9458 struct intel_crtc *intel_crtc =
9459 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9460 unsigned long flags;
9461
f326038a
DV
9462
9463 /*
9464 * This is called both by irq handlers and the reset code (to complete
9465 * lost pageflips) so needs the full irqsave spinlocks.
9466 *
9467 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
9468 * generate a page-flip completion irq, i.e. every modeset
9469 * is also accompanied by a spurious intel_prepare_page_flip().
9470 */
6b95a207 9471 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9472 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9473 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9474 spin_unlock_irqrestore(&dev->event_lock, flags);
9475}
9476
eba905b2 9477static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9478{
9479 /* Ensure that the work item is consistent when activating it ... */
9480 smp_wmb();
9481 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9482 /* and that it is marked active as soon as the irq could fire. */
9483 smp_wmb();
9484}
9485
8c9f3aaf
JB
9486static int intel_gen2_queue_flip(struct drm_device *dev,
9487 struct drm_crtc *crtc,
9488 struct drm_framebuffer *fb,
ed8d1975 9489 struct drm_i915_gem_object *obj,
a4872ba6 9490 struct intel_engine_cs *ring,
ed8d1975 9491 uint32_t flags)
8c9f3aaf 9492{
8c9f3aaf 9493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9494 u32 flip_mask;
9495 int ret;
9496
6d90c952 9497 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9498 if (ret)
4fa62c89 9499 return ret;
8c9f3aaf
JB
9500
9501 /* Can't queue multiple flips, so wait for the previous
9502 * one to finish before executing the next.
9503 */
9504 if (intel_crtc->plane)
9505 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9506 else
9507 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9508 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9509 intel_ring_emit(ring, MI_NOOP);
9510 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9511 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9512 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9513 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9514 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9515
9516 intel_mark_page_flip_active(intel_crtc);
09246732 9517 __intel_ring_advance(ring);
83d4092b 9518 return 0;
8c9f3aaf
JB
9519}
9520
9521static int intel_gen3_queue_flip(struct drm_device *dev,
9522 struct drm_crtc *crtc,
9523 struct drm_framebuffer *fb,
ed8d1975 9524 struct drm_i915_gem_object *obj,
a4872ba6 9525 struct intel_engine_cs *ring,
ed8d1975 9526 uint32_t flags)
8c9f3aaf 9527{
8c9f3aaf 9528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9529 u32 flip_mask;
9530 int ret;
9531
6d90c952 9532 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9533 if (ret)
4fa62c89 9534 return ret;
8c9f3aaf
JB
9535
9536 if (intel_crtc->plane)
9537 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9538 else
9539 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9540 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9541 intel_ring_emit(ring, MI_NOOP);
9542 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9543 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9544 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9545 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9546 intel_ring_emit(ring, MI_NOOP);
9547
e7d841ca 9548 intel_mark_page_flip_active(intel_crtc);
09246732 9549 __intel_ring_advance(ring);
83d4092b 9550 return 0;
8c9f3aaf
JB
9551}
9552
9553static int intel_gen4_queue_flip(struct drm_device *dev,
9554 struct drm_crtc *crtc,
9555 struct drm_framebuffer *fb,
ed8d1975 9556 struct drm_i915_gem_object *obj,
a4872ba6 9557 struct intel_engine_cs *ring,
ed8d1975 9558 uint32_t flags)
8c9f3aaf
JB
9559{
9560 struct drm_i915_private *dev_priv = dev->dev_private;
9561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9562 uint32_t pf, pipesrc;
9563 int ret;
9564
6d90c952 9565 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9566 if (ret)
4fa62c89 9567 return ret;
8c9f3aaf
JB
9568
9569 /* i965+ uses the linear or tiled offsets from the
9570 * Display Registers (which do not change across a page-flip)
9571 * so we need only reprogram the base address.
9572 */
6d90c952
DV
9573 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9574 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9575 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9576 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9577 obj->tiling_mode);
8c9f3aaf
JB
9578
9579 /* XXX Enabling the panel-fitter across page-flip is so far
9580 * untested on non-native modes, so ignore it for now.
9581 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9582 */
9583 pf = 0;
9584 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9585 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9586
9587 intel_mark_page_flip_active(intel_crtc);
09246732 9588 __intel_ring_advance(ring);
83d4092b 9589 return 0;
8c9f3aaf
JB
9590}
9591
9592static int intel_gen6_queue_flip(struct drm_device *dev,
9593 struct drm_crtc *crtc,
9594 struct drm_framebuffer *fb,
ed8d1975 9595 struct drm_i915_gem_object *obj,
a4872ba6 9596 struct intel_engine_cs *ring,
ed8d1975 9597 uint32_t flags)
8c9f3aaf
JB
9598{
9599 struct drm_i915_private *dev_priv = dev->dev_private;
9600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9601 uint32_t pf, pipesrc;
9602 int ret;
9603
6d90c952 9604 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9605 if (ret)
4fa62c89 9606 return ret;
8c9f3aaf 9607
6d90c952
DV
9608 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9609 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9610 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9611 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9612
dc257cf1
DV
9613 /* Contrary to the suggestions in the documentation,
9614 * "Enable Panel Fitter" does not seem to be required when page
9615 * flipping with a non-native mode, and worse causes a normal
9616 * modeset to fail.
9617 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9618 */
9619 pf = 0;
8c9f3aaf 9620 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9621 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9622
9623 intel_mark_page_flip_active(intel_crtc);
09246732 9624 __intel_ring_advance(ring);
83d4092b 9625 return 0;
8c9f3aaf
JB
9626}
9627
7c9017e5
JB
9628static int intel_gen7_queue_flip(struct drm_device *dev,
9629 struct drm_crtc *crtc,
9630 struct drm_framebuffer *fb,
ed8d1975 9631 struct drm_i915_gem_object *obj,
a4872ba6 9632 struct intel_engine_cs *ring,
ed8d1975 9633 uint32_t flags)
7c9017e5 9634{
7c9017e5 9635 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9636 uint32_t plane_bit = 0;
ffe74d75
CW
9637 int len, ret;
9638
eba905b2 9639 switch (intel_crtc->plane) {
cb05d8de
DV
9640 case PLANE_A:
9641 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9642 break;
9643 case PLANE_B:
9644 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9645 break;
9646 case PLANE_C:
9647 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9648 break;
9649 default:
9650 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9651 return -ENODEV;
cb05d8de
DV
9652 }
9653
ffe74d75 9654 len = 4;
f476828a 9655 if (ring->id == RCS) {
ffe74d75 9656 len += 6;
f476828a
DL
9657 /*
9658 * On Gen 8, SRM is now taking an extra dword to accommodate
9659 * 48bits addresses, and we need a NOOP for the batch size to
9660 * stay even.
9661 */
9662 if (IS_GEN8(dev))
9663 len += 2;
9664 }
ffe74d75 9665
f66fab8e
VS
9666 /*
9667 * BSpec MI_DISPLAY_FLIP for IVB:
9668 * "The full packet must be contained within the same cache line."
9669 *
9670 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9671 * cacheline, if we ever start emitting more commands before
9672 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9673 * then do the cacheline alignment, and finally emit the
9674 * MI_DISPLAY_FLIP.
9675 */
9676 ret = intel_ring_cacheline_align(ring);
9677 if (ret)
4fa62c89 9678 return ret;
f66fab8e 9679
ffe74d75 9680 ret = intel_ring_begin(ring, len);
7c9017e5 9681 if (ret)
4fa62c89 9682 return ret;
7c9017e5 9683
ffe74d75
CW
9684 /* Unmask the flip-done completion message. Note that the bspec says that
9685 * we should do this for both the BCS and RCS, and that we must not unmask
9686 * more than one flip event at any time (or ensure that one flip message
9687 * can be sent by waiting for flip-done prior to queueing new flips).
9688 * Experimentation says that BCS works despite DERRMR masking all
9689 * flip-done completion events and that unmasking all planes at once
9690 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9691 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9692 */
9693 if (ring->id == RCS) {
9694 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9695 intel_ring_emit(ring, DERRMR);
9696 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9697 DERRMR_PIPEB_PRI_FLIP_DONE |
9698 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9699 if (IS_GEN8(dev))
9700 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9701 MI_SRM_LRM_GLOBAL_GTT);
9702 else
9703 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9704 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9705 intel_ring_emit(ring, DERRMR);
9706 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9707 if (IS_GEN8(dev)) {
9708 intel_ring_emit(ring, 0);
9709 intel_ring_emit(ring, MI_NOOP);
9710 }
ffe74d75
CW
9711 }
9712
cb05d8de 9713 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9714 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9715 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9716 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9717
9718 intel_mark_page_flip_active(intel_crtc);
09246732 9719 __intel_ring_advance(ring);
83d4092b 9720 return 0;
7c9017e5
JB
9721}
9722
84c33a64
SG
9723static bool use_mmio_flip(struct intel_engine_cs *ring,
9724 struct drm_i915_gem_object *obj)
9725{
9726 /*
9727 * This is not being used for older platforms, because
9728 * non-availability of flip done interrupt forces us to use
9729 * CS flips. Older platforms derive flip done using some clever
9730 * tricks involving the flip_pending status bits and vblank irqs.
9731 * So using MMIO flips there would disrupt this mechanism.
9732 */
9733
8e09bf83
CW
9734 if (ring == NULL)
9735 return true;
9736
84c33a64
SG
9737 if (INTEL_INFO(ring->dev)->gen < 5)
9738 return false;
9739
9740 if (i915.use_mmio_flip < 0)
9741 return false;
9742 else if (i915.use_mmio_flip > 0)
9743 return true;
14bf993e
OM
9744 else if (i915.enable_execlists)
9745 return true;
84c33a64 9746 else
41c52415 9747 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
9748}
9749
ff944564
DL
9750static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9751{
9752 struct drm_device *dev = intel_crtc->base.dev;
9753 struct drm_i915_private *dev_priv = dev->dev_private;
9754 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9755 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9756 struct drm_i915_gem_object *obj = intel_fb->obj;
9757 const enum pipe pipe = intel_crtc->pipe;
9758 u32 ctl, stride;
9759
9760 ctl = I915_READ(PLANE_CTL(pipe, 0));
9761 ctl &= ~PLANE_CTL_TILED_MASK;
9762 if (obj->tiling_mode == I915_TILING_X)
9763 ctl |= PLANE_CTL_TILED_X;
9764
9765 /*
9766 * The stride is either expressed as a multiple of 64 bytes chunks for
9767 * linear buffers or in number of tiles for tiled buffers.
9768 */
9769 stride = fb->pitches[0] >> 6;
9770 if (obj->tiling_mode == I915_TILING_X)
9771 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9772
9773 /*
9774 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9775 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9776 */
9777 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9778 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9779
9780 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9781 POSTING_READ(PLANE_SURF(pipe, 0));
9782}
9783
9784static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
9785{
9786 struct drm_device *dev = intel_crtc->base.dev;
9787 struct drm_i915_private *dev_priv = dev->dev_private;
9788 struct intel_framebuffer *intel_fb =
9789 to_intel_framebuffer(intel_crtc->base.primary->fb);
9790 struct drm_i915_gem_object *obj = intel_fb->obj;
9791 u32 dspcntr;
9792 u32 reg;
9793
84c33a64
SG
9794 reg = DSPCNTR(intel_crtc->plane);
9795 dspcntr = I915_READ(reg);
9796
c5d97472
DL
9797 if (obj->tiling_mode != I915_TILING_NONE)
9798 dspcntr |= DISPPLANE_TILED;
9799 else
9800 dspcntr &= ~DISPPLANE_TILED;
9801
84c33a64
SG
9802 I915_WRITE(reg, dspcntr);
9803
9804 I915_WRITE(DSPSURF(intel_crtc->plane),
9805 intel_crtc->unpin_work->gtt_offset);
9806 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 9807
ff944564
DL
9808}
9809
9810/*
9811 * XXX: This is the temporary way to update the plane registers until we get
9812 * around to using the usual plane update functions for MMIO flips
9813 */
9814static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9815{
9816 struct drm_device *dev = intel_crtc->base.dev;
9817 bool atomic_update;
9818 u32 start_vbl_count;
9819
9820 intel_mark_page_flip_active(intel_crtc);
9821
9822 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9823
9824 if (INTEL_INFO(dev)->gen >= 9)
9825 skl_do_mmio_flip(intel_crtc);
9826 else
9827 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9828 ilk_do_mmio_flip(intel_crtc);
9829
9362c7c5
ACO
9830 if (atomic_update)
9831 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
9832}
9833
9362c7c5 9834static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 9835{
cc8c4cc2 9836 struct intel_crtc *crtc =
9362c7c5 9837 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 9838 struct intel_mmio_flip *mmio_flip;
84c33a64 9839
cc8c4cc2
JH
9840 mmio_flip = &crtc->mmio_flip;
9841 if (mmio_flip->req)
9c654818
JH
9842 WARN_ON(__i915_wait_request(mmio_flip->req,
9843 crtc->reset_counter,
9844 false, NULL, NULL) != 0);
84c33a64 9845
cc8c4cc2
JH
9846 intel_do_mmio_flip(crtc);
9847 if (mmio_flip->req) {
9848 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 9849 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
9850 mutex_unlock(&crtc->base.dev->struct_mutex);
9851 }
84c33a64
SG
9852}
9853
9854static int intel_queue_mmio_flip(struct drm_device *dev,
9855 struct drm_crtc *crtc,
9856 struct drm_framebuffer *fb,
9857 struct drm_i915_gem_object *obj,
9858 struct intel_engine_cs *ring,
9859 uint32_t flags)
9860{
84c33a64 9861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 9862
cc8c4cc2
JH
9863 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9864 obj->last_write_req);
536f5b5e
ACO
9865
9866 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 9867
84c33a64
SG
9868 return 0;
9869}
9870
8c9f3aaf
JB
9871static int intel_default_queue_flip(struct drm_device *dev,
9872 struct drm_crtc *crtc,
9873 struct drm_framebuffer *fb,
ed8d1975 9874 struct drm_i915_gem_object *obj,
a4872ba6 9875 struct intel_engine_cs *ring,
ed8d1975 9876 uint32_t flags)
8c9f3aaf
JB
9877{
9878 return -ENODEV;
9879}
9880
d6bbafa1
CW
9881static bool __intel_pageflip_stall_check(struct drm_device *dev,
9882 struct drm_crtc *crtc)
9883{
9884 struct drm_i915_private *dev_priv = dev->dev_private;
9885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9886 struct intel_unpin_work *work = intel_crtc->unpin_work;
9887 u32 addr;
9888
9889 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9890 return true;
9891
9892 if (!work->enable_stall_check)
9893 return false;
9894
9895 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
9896 if (work->flip_queued_req &&
9897 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
9898 return false;
9899
1e3feefd 9900 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
9901 }
9902
1e3feefd 9903 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
9904 return false;
9905
9906 /* Potential stall - if we see that the flip has happened,
9907 * assume a missed interrupt. */
9908 if (INTEL_INFO(dev)->gen >= 4)
9909 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9910 else
9911 addr = I915_READ(DSPADDR(intel_crtc->plane));
9912
9913 /* There is a potential issue here with a false positive after a flip
9914 * to the same address. We could address this by checking for a
9915 * non-incrementing frame counter.
9916 */
9917 return addr == work->gtt_offset;
9918}
9919
9920void intel_check_page_flip(struct drm_device *dev, int pipe)
9921{
9922 struct drm_i915_private *dev_priv = dev->dev_private;
9923 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f326038a 9925
6c51d46f 9926 WARN_ON(!in_interrupt());
d6bbafa1
CW
9927
9928 if (crtc == NULL)
9929 return;
9930
f326038a 9931 spin_lock(&dev->event_lock);
d6bbafa1
CW
9932 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9933 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
1e3feefd
DV
9934 intel_crtc->unpin_work->flip_queued_vblank,
9935 drm_vblank_count(dev, pipe));
d6bbafa1
CW
9936 page_flip_completed(intel_crtc);
9937 }
f326038a 9938 spin_unlock(&dev->event_lock);
d6bbafa1
CW
9939}
9940
6b95a207
KH
9941static int intel_crtc_page_flip(struct drm_crtc *crtc,
9942 struct drm_framebuffer *fb,
ed8d1975
KP
9943 struct drm_pending_vblank_event *event,
9944 uint32_t page_flip_flags)
6b95a207
KH
9945{
9946 struct drm_device *dev = crtc->dev;
9947 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9948 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9949 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 9951 struct drm_plane *primary = crtc->primary;
a071fa00 9952 enum pipe pipe = intel_crtc->pipe;
6b95a207 9953 struct intel_unpin_work *work;
a4872ba6 9954 struct intel_engine_cs *ring;
52e68630 9955 int ret;
6b95a207 9956
2ff8fde1
MR
9957 /*
9958 * drm_mode_page_flip_ioctl() should already catch this, but double
9959 * check to be safe. In the future we may enable pageflipping from
9960 * a disabled primary plane.
9961 */
9962 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9963 return -EBUSY;
9964
e6a595d2 9965 /* Can't change pixel format via MI display flips. */
f4510a27 9966 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9967 return -EINVAL;
9968
9969 /*
9970 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9971 * Note that pitch changes could also affect these register.
9972 */
9973 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9974 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9975 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9976 return -EINVAL;
9977
f900db47
CW
9978 if (i915_terminally_wedged(&dev_priv->gpu_error))
9979 goto out_hang;
9980
b14c5679 9981 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9982 if (work == NULL)
9983 return -ENOMEM;
9984
6b95a207 9985 work->event = event;
b4a98e57 9986 work->crtc = crtc;
ab8d6675 9987 work->old_fb = old_fb;
6b95a207
KH
9988 INIT_WORK(&work->work, intel_unpin_work_fn);
9989
87b6b101 9990 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9991 if (ret)
9992 goto free_work;
9993
6b95a207 9994 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 9995 spin_lock_irq(&dev->event_lock);
6b95a207 9996 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9997 /* Before declaring the flip queue wedged, check if
9998 * the hardware completed the operation behind our backs.
9999 */
10000 if (__intel_pageflip_stall_check(dev, crtc)) {
10001 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10002 page_flip_completed(intel_crtc);
10003 } else {
10004 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10005 spin_unlock_irq(&dev->event_lock);
468f0b44 10006
d6bbafa1
CW
10007 drm_crtc_vblank_put(crtc);
10008 kfree(work);
10009 return -EBUSY;
10010 }
6b95a207
KH
10011 }
10012 intel_crtc->unpin_work = work;
5e2d7afc 10013 spin_unlock_irq(&dev->event_lock);
6b95a207 10014
b4a98e57
CW
10015 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10016 flush_workqueue(dev_priv->wq);
10017
75dfca80 10018 /* Reference the objects for the scheduled work. */
ab8d6675 10019 drm_framebuffer_reference(work->old_fb);
05394f39 10020 drm_gem_object_reference(&obj->base);
6b95a207 10021
f4510a27 10022 crtc->primary->fb = fb;
afd65eb4 10023 update_state_fb(crtc->primary);
1ed1f968 10024
e1f99ce6 10025 work->pending_flip_obj = obj;
e1f99ce6 10026
89ed88ba
CW
10027 ret = i915_mutex_lock_interruptible(dev);
10028 if (ret)
10029 goto cleanup;
10030
b4a98e57 10031 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10032 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10033
75f7f3ec 10034 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10035 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10036
4fa62c89
VS
10037 if (IS_VALLEYVIEW(dev)) {
10038 ring = &dev_priv->ring[BCS];
ab8d6675 10039 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
10040 /* vlv: DISPLAY_FLIP fails to change tiling */
10041 ring = NULL;
48bf5b2d 10042 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 10043 ring = &dev_priv->ring[BCS];
4fa62c89 10044 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 10045 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
10046 if (ring == NULL || ring->id != RCS)
10047 ring = &dev_priv->ring[BCS];
10048 } else {
10049 ring = &dev_priv->ring[RCS];
10050 }
10051
82bc3b2d
TU
10052 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
10053 crtc->primary->state, ring);
8c9f3aaf
JB
10054 if (ret)
10055 goto cleanup_pending;
6b95a207 10056
4fa62c89
VS
10057 work->gtt_offset =
10058 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
10059
d6bbafa1 10060 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
10061 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10062 page_flip_flags);
d6bbafa1
CW
10063 if (ret)
10064 goto cleanup_unpin;
10065
f06cc1b9
JH
10066 i915_gem_request_assign(&work->flip_queued_req,
10067 obj->last_write_req);
d6bbafa1 10068 } else {
84c33a64 10069 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10070 page_flip_flags);
10071 if (ret)
10072 goto cleanup_unpin;
10073
f06cc1b9
JH
10074 i915_gem_request_assign(&work->flip_queued_req,
10075 intel_ring_get_request(ring));
d6bbafa1
CW
10076 }
10077
1e3feefd 10078 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 10079 work->enable_stall_check = true;
4fa62c89 10080
ab8d6675 10081 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
10082 INTEL_FRONTBUFFER_PRIMARY(pipe));
10083
7ff0ebcc 10084 intel_fbc_disable(dev);
f99d7069 10085 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
10086 mutex_unlock(&dev->struct_mutex);
10087
e5510fac
JB
10088 trace_i915_flip_request(intel_crtc->plane, obj);
10089
6b95a207 10090 return 0;
96b099fd 10091
4fa62c89 10092cleanup_unpin:
82bc3b2d 10093 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 10094cleanup_pending:
b4a98e57 10095 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
10096 mutex_unlock(&dev->struct_mutex);
10097cleanup:
f4510a27 10098 crtc->primary->fb = old_fb;
afd65eb4 10099 update_state_fb(crtc->primary);
89ed88ba
CW
10100
10101 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 10102 drm_framebuffer_unreference(work->old_fb);
96b099fd 10103
5e2d7afc 10104 spin_lock_irq(&dev->event_lock);
96b099fd 10105 intel_crtc->unpin_work = NULL;
5e2d7afc 10106 spin_unlock_irq(&dev->event_lock);
96b099fd 10107
87b6b101 10108 drm_crtc_vblank_put(crtc);
7317c75e 10109free_work:
96b099fd
CW
10110 kfree(work);
10111
f900db47
CW
10112 if (ret == -EIO) {
10113out_hang:
53a366b9 10114 ret = intel_plane_restore(primary);
f0d3dad3 10115 if (ret == 0 && event) {
5e2d7afc 10116 spin_lock_irq(&dev->event_lock);
a071fa00 10117 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 10118 spin_unlock_irq(&dev->event_lock);
f0d3dad3 10119 }
f900db47 10120 }
96b099fd 10121 return ret;
6b95a207
KH
10122}
10123
f6e5b160 10124static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
10125 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10126 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
10127 .atomic_begin = intel_begin_crtc_commit,
10128 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
10129};
10130
9a935856
DV
10131/**
10132 * intel_modeset_update_staged_output_state
10133 *
10134 * Updates the staged output configuration state, e.g. after we've read out the
10135 * current hw state.
10136 */
10137static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 10138{
7668851f 10139 struct intel_crtc *crtc;
9a935856
DV
10140 struct intel_encoder *encoder;
10141 struct intel_connector *connector;
f6e5b160 10142
3a3371ff 10143 for_each_intel_connector(dev, connector) {
9a935856
DV
10144 connector->new_encoder =
10145 to_intel_encoder(connector->base.encoder);
10146 }
f6e5b160 10147
b2784e15 10148 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10149 encoder->new_crtc =
10150 to_intel_crtc(encoder->base.crtc);
10151 }
7668851f 10152
d3fcc808 10153 for_each_intel_crtc(dev, crtc) {
83d65738 10154 crtc->new_enabled = crtc->base.state->enable;
7bd0a8e7
VS
10155
10156 if (crtc->new_enabled)
6e3c9717 10157 crtc->new_config = crtc->config;
7bd0a8e7
VS
10158 else
10159 crtc->new_config = NULL;
7668851f 10160 }
f6e5b160
CW
10161}
10162
9a935856
DV
10163/**
10164 * intel_modeset_commit_output_state
10165 *
10166 * This function copies the stage display pipe configuration to the real one.
10167 */
10168static void intel_modeset_commit_output_state(struct drm_device *dev)
10169{
7668851f 10170 struct intel_crtc *crtc;
9a935856
DV
10171 struct intel_encoder *encoder;
10172 struct intel_connector *connector;
f6e5b160 10173
3a3371ff 10174 for_each_intel_connector(dev, connector) {
9a935856
DV
10175 connector->base.encoder = &connector->new_encoder->base;
10176 }
f6e5b160 10177
b2784e15 10178 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10179 encoder->base.crtc = &encoder->new_crtc->base;
10180 }
7668851f 10181
d3fcc808 10182 for_each_intel_crtc(dev, crtc) {
83d65738 10183 crtc->base.state->enable = crtc->new_enabled;
7668851f
VS
10184 crtc->base.enabled = crtc->new_enabled;
10185 }
9a935856
DV
10186}
10187
050f7aeb 10188static void
eba905b2 10189connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 10190 struct intel_crtc_state *pipe_config)
050f7aeb
DV
10191{
10192 int bpp = pipe_config->pipe_bpp;
10193
10194 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10195 connector->base.base.id,
c23cc417 10196 connector->base.name);
050f7aeb
DV
10197
10198 /* Don't use an invalid EDID bpc value */
10199 if (connector->base.display_info.bpc &&
10200 connector->base.display_info.bpc * 3 < bpp) {
10201 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10202 bpp, connector->base.display_info.bpc*3);
10203 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10204 }
10205
10206 /* Clamp bpp to 8 on screens without EDID 1.4 */
10207 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10208 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10209 bpp);
10210 pipe_config->pipe_bpp = 24;
10211 }
10212}
10213
4e53c2e0 10214static int
050f7aeb
DV
10215compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10216 struct drm_framebuffer *fb,
5cec258b 10217 struct intel_crtc_state *pipe_config)
4e53c2e0 10218{
050f7aeb
DV
10219 struct drm_device *dev = crtc->base.dev;
10220 struct intel_connector *connector;
4e53c2e0
DV
10221 int bpp;
10222
d42264b1
DV
10223 switch (fb->pixel_format) {
10224 case DRM_FORMAT_C8:
4e53c2e0
DV
10225 bpp = 8*3; /* since we go through a colormap */
10226 break;
d42264b1
DV
10227 case DRM_FORMAT_XRGB1555:
10228 case DRM_FORMAT_ARGB1555:
10229 /* checked in intel_framebuffer_init already */
10230 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10231 return -EINVAL;
10232 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10233 bpp = 6*3; /* min is 18bpp */
10234 break;
d42264b1
DV
10235 case DRM_FORMAT_XBGR8888:
10236 case DRM_FORMAT_ABGR8888:
10237 /* checked in intel_framebuffer_init already */
10238 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10239 return -EINVAL;
10240 case DRM_FORMAT_XRGB8888:
10241 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10242 bpp = 8*3;
10243 break;
d42264b1
DV
10244 case DRM_FORMAT_XRGB2101010:
10245 case DRM_FORMAT_ARGB2101010:
10246 case DRM_FORMAT_XBGR2101010:
10247 case DRM_FORMAT_ABGR2101010:
10248 /* checked in intel_framebuffer_init already */
10249 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10250 return -EINVAL;
4e53c2e0
DV
10251 bpp = 10*3;
10252 break;
baba133a 10253 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10254 default:
10255 DRM_DEBUG_KMS("unsupported depth\n");
10256 return -EINVAL;
10257 }
10258
4e53c2e0
DV
10259 pipe_config->pipe_bpp = bpp;
10260
10261 /* Clamp display bpp to EDID value */
3a3371ff 10262 for_each_intel_connector(dev, connector) {
1b829e05
DV
10263 if (!connector->new_encoder ||
10264 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10265 continue;
10266
050f7aeb 10267 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10268 }
10269
10270 return bpp;
10271}
10272
644db711
DV
10273static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10274{
10275 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10276 "type: 0x%x flags: 0x%x\n",
1342830c 10277 mode->crtc_clock,
644db711
DV
10278 mode->crtc_hdisplay, mode->crtc_hsync_start,
10279 mode->crtc_hsync_end, mode->crtc_htotal,
10280 mode->crtc_vdisplay, mode->crtc_vsync_start,
10281 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10282}
10283
c0b03411 10284static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 10285 struct intel_crtc_state *pipe_config,
c0b03411
DV
10286 const char *context)
10287{
10288 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10289 context, pipe_name(crtc->pipe));
10290
10291 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10292 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10293 pipe_config->pipe_bpp, pipe_config->dither);
10294 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10295 pipe_config->has_pch_encoder,
10296 pipe_config->fdi_lanes,
10297 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10298 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10299 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10300 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10301 pipe_config->has_dp_encoder,
10302 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10303 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10304 pipe_config->dp_m_n.tu);
b95af8be
VK
10305
10306 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10307 pipe_config->has_dp_encoder,
10308 pipe_config->dp_m2_n2.gmch_m,
10309 pipe_config->dp_m2_n2.gmch_n,
10310 pipe_config->dp_m2_n2.link_m,
10311 pipe_config->dp_m2_n2.link_n,
10312 pipe_config->dp_m2_n2.tu);
10313
55072d19
DV
10314 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10315 pipe_config->has_audio,
10316 pipe_config->has_infoframe);
10317
c0b03411 10318 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 10319 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 10320 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
10321 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10322 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 10323 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10324 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10325 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10326 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10327 pipe_config->gmch_pfit.control,
10328 pipe_config->gmch_pfit.pgm_ratios,
10329 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10330 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10331 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10332 pipe_config->pch_pfit.size,
10333 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10334 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10335 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10336}
10337
bc079e8b
VS
10338static bool encoders_cloneable(const struct intel_encoder *a,
10339 const struct intel_encoder *b)
accfc0c5 10340{
bc079e8b
VS
10341 /* masks could be asymmetric, so check both ways */
10342 return a == b || (a->cloneable & (1 << b->type) &&
10343 b->cloneable & (1 << a->type));
10344}
10345
10346static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10347 struct intel_encoder *encoder)
10348{
10349 struct drm_device *dev = crtc->base.dev;
10350 struct intel_encoder *source_encoder;
10351
b2784e15 10352 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10353 if (source_encoder->new_crtc != crtc)
10354 continue;
10355
10356 if (!encoders_cloneable(encoder, source_encoder))
10357 return false;
10358 }
10359
10360 return true;
10361}
10362
10363static bool check_encoder_cloning(struct intel_crtc *crtc)
10364{
10365 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10366 struct intel_encoder *encoder;
10367
b2784e15 10368 for_each_intel_encoder(dev, encoder) {
bc079e8b 10369 if (encoder->new_crtc != crtc)
accfc0c5
DV
10370 continue;
10371
bc079e8b
VS
10372 if (!check_single_encoder_cloning(crtc, encoder))
10373 return false;
accfc0c5
DV
10374 }
10375
bc079e8b 10376 return true;
accfc0c5
DV
10377}
10378
00f0b378
VS
10379static bool check_digital_port_conflicts(struct drm_device *dev)
10380{
10381 struct intel_connector *connector;
10382 unsigned int used_ports = 0;
10383
10384 /*
10385 * Walk the connector list instead of the encoder
10386 * list to detect the problem on ddi platforms
10387 * where there's just one encoder per digital port.
10388 */
3a3371ff 10389 for_each_intel_connector(dev, connector) {
00f0b378
VS
10390 struct intel_encoder *encoder = connector->new_encoder;
10391
10392 if (!encoder)
10393 continue;
10394
10395 WARN_ON(!encoder->new_crtc);
10396
10397 switch (encoder->type) {
10398 unsigned int port_mask;
10399 case INTEL_OUTPUT_UNKNOWN:
10400 if (WARN_ON(!HAS_DDI(dev)))
10401 break;
10402 case INTEL_OUTPUT_DISPLAYPORT:
10403 case INTEL_OUTPUT_HDMI:
10404 case INTEL_OUTPUT_EDP:
10405 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10406
10407 /* the same port mustn't appear more than once */
10408 if (used_ports & port_mask)
10409 return false;
10410
10411 used_ports |= port_mask;
10412 default:
10413 break;
10414 }
10415 }
10416
10417 return true;
10418}
10419
5cec258b 10420static struct intel_crtc_state *
b8cecdf5 10421intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10422 struct drm_framebuffer *fb,
b8cecdf5 10423 struct drm_display_mode *mode)
ee7b9f93 10424{
7758a113 10425 struct drm_device *dev = crtc->dev;
7758a113 10426 struct intel_encoder *encoder;
5cec258b 10427 struct intel_crtc_state *pipe_config;
e29c22c0
DV
10428 int plane_bpp, ret = -EINVAL;
10429 bool retry = true;
ee7b9f93 10430
bc079e8b 10431 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10432 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10433 return ERR_PTR(-EINVAL);
10434 }
10435
00f0b378
VS
10436 if (!check_digital_port_conflicts(dev)) {
10437 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10438 return ERR_PTR(-EINVAL);
10439 }
10440
b8cecdf5
DV
10441 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10442 if (!pipe_config)
7758a113
DV
10443 return ERR_PTR(-ENOMEM);
10444
07878248 10445 pipe_config->base.crtc = crtc;
2d112de7
ACO
10446 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10447 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 10448
e143a21c
DV
10449 pipe_config->cpu_transcoder =
10450 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10451 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10452
2960bc9c
ID
10453 /*
10454 * Sanitize sync polarity flags based on requested ones. If neither
10455 * positive or negative polarity is requested, treat this as meaning
10456 * negative polarity.
10457 */
2d112de7 10458 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10459 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 10460 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 10461
2d112de7 10462 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10463 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 10464 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 10465
050f7aeb
DV
10466 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10467 * plane pixel format and any sink constraints into account. Returns the
10468 * source plane bpp so that dithering can be selected on mismatches
10469 * after encoders and crtc also have had their say. */
10470 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10471 fb, pipe_config);
4e53c2e0
DV
10472 if (plane_bpp < 0)
10473 goto fail;
10474
e41a56be
VS
10475 /*
10476 * Determine the real pipe dimensions. Note that stereo modes can
10477 * increase the actual pipe size due to the frame doubling and
10478 * insertion of additional space for blanks between the frame. This
10479 * is stored in the crtc timings. We use the requested mode to do this
10480 * computation to clearly distinguish it from the adjusted mode, which
10481 * can be changed by the connectors in the below retry loop.
10482 */
2d112de7 10483 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
10484 &pipe_config->pipe_src_w,
10485 &pipe_config->pipe_src_h);
e41a56be 10486
e29c22c0 10487encoder_retry:
ef1b460d 10488 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10489 pipe_config->port_clock = 0;
ef1b460d 10490 pipe_config->pixel_multiplier = 1;
ff9a6750 10491
135c81b8 10492 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
10493 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10494 CRTC_STEREO_DOUBLE);
135c81b8 10495
7758a113
DV
10496 /* Pass our mode to the connectors and the CRTC to give them a chance to
10497 * adjust it according to limitations or connector properties, and also
10498 * a chance to reject the mode entirely.
47f1c6c9 10499 */
b2784e15 10500 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10501
7758a113
DV
10502 if (&encoder->new_crtc->base != crtc)
10503 continue;
7ae89233 10504
efea6e8e
DV
10505 if (!(encoder->compute_config(encoder, pipe_config))) {
10506 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10507 goto fail;
10508 }
ee7b9f93 10509 }
47f1c6c9 10510
ff9a6750
DV
10511 /* Set default port clock if not overwritten by the encoder. Needs to be
10512 * done afterwards in case the encoder adjusts the mode. */
10513 if (!pipe_config->port_clock)
2d112de7 10514 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 10515 * pipe_config->pixel_multiplier;
ff9a6750 10516
a43f6e0f 10517 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10518 if (ret < 0) {
7758a113
DV
10519 DRM_DEBUG_KMS("CRTC fixup failed\n");
10520 goto fail;
ee7b9f93 10521 }
e29c22c0
DV
10522
10523 if (ret == RETRY) {
10524 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10525 ret = -EINVAL;
10526 goto fail;
10527 }
10528
10529 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10530 retry = false;
10531 goto encoder_retry;
10532 }
10533
4e53c2e0
DV
10534 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10535 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10536 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10537
b8cecdf5 10538 return pipe_config;
7758a113 10539fail:
b8cecdf5 10540 kfree(pipe_config);
e29c22c0 10541 return ERR_PTR(ret);
ee7b9f93 10542}
47f1c6c9 10543
e2e1ed41
DV
10544/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10545 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10546static void
10547intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10548 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10549{
10550 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10551 struct drm_device *dev = crtc->dev;
10552 struct intel_encoder *encoder;
10553 struct intel_connector *connector;
10554 struct drm_crtc *tmp_crtc;
79e53945 10555
e2e1ed41 10556 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10557
e2e1ed41
DV
10558 /* Check which crtcs have changed outputs connected to them, these need
10559 * to be part of the prepare_pipes mask. We don't (yet) support global
10560 * modeset across multiple crtcs, so modeset_pipes will only have one
10561 * bit set at most. */
3a3371ff 10562 for_each_intel_connector(dev, connector) {
e2e1ed41
DV
10563 if (connector->base.encoder == &connector->new_encoder->base)
10564 continue;
79e53945 10565
e2e1ed41
DV
10566 if (connector->base.encoder) {
10567 tmp_crtc = connector->base.encoder->crtc;
10568
10569 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10570 }
10571
10572 if (connector->new_encoder)
10573 *prepare_pipes |=
10574 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10575 }
10576
b2784e15 10577 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10578 if (encoder->base.crtc == &encoder->new_crtc->base)
10579 continue;
10580
10581 if (encoder->base.crtc) {
10582 tmp_crtc = encoder->base.crtc;
10583
10584 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10585 }
10586
10587 if (encoder->new_crtc)
10588 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10589 }
10590
7668851f 10591 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10592 for_each_intel_crtc(dev, intel_crtc) {
83d65738 10593 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
e2e1ed41 10594 continue;
7e7d76c3 10595
7668851f 10596 if (!intel_crtc->new_enabled)
e2e1ed41 10597 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10598 else
10599 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10600 }
10601
e2e1ed41
DV
10602
10603 /* set_mode is also used to update properties on life display pipes. */
10604 intel_crtc = to_intel_crtc(crtc);
7668851f 10605 if (intel_crtc->new_enabled)
e2e1ed41
DV
10606 *prepare_pipes |= 1 << intel_crtc->pipe;
10607
b6c5164d
DV
10608 /*
10609 * For simplicity do a full modeset on any pipe where the output routing
10610 * changed. We could be more clever, but that would require us to be
10611 * more careful with calling the relevant encoder->mode_set functions.
10612 */
e2e1ed41
DV
10613 if (*prepare_pipes)
10614 *modeset_pipes = *prepare_pipes;
10615
10616 /* ... and mask these out. */
10617 *modeset_pipes &= ~(*disable_pipes);
10618 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10619
10620 /*
10621 * HACK: We don't (yet) fully support global modesets. intel_set_config
10622 * obies this rule, but the modeset restore mode of
10623 * intel_modeset_setup_hw_state does not.
10624 */
10625 *modeset_pipes &= 1 << intel_crtc->pipe;
10626 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10627
10628 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10629 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10630}
79e53945 10631
ea9d758d 10632static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10633{
ea9d758d 10634 struct drm_encoder *encoder;
f6e5b160 10635 struct drm_device *dev = crtc->dev;
f6e5b160 10636
ea9d758d
DV
10637 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10638 if (encoder->crtc == crtc)
10639 return true;
10640
10641 return false;
10642}
10643
10644static void
10645intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10646{
ba41c0de 10647 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
10648 struct intel_encoder *intel_encoder;
10649 struct intel_crtc *intel_crtc;
10650 struct drm_connector *connector;
10651
ba41c0de
DV
10652 intel_shared_dpll_commit(dev_priv);
10653
b2784e15 10654 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10655 if (!intel_encoder->base.crtc)
10656 continue;
10657
10658 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10659
10660 if (prepare_pipes & (1 << intel_crtc->pipe))
10661 intel_encoder->connectors_active = false;
10662 }
10663
10664 intel_modeset_commit_output_state(dev);
10665
7668851f 10666 /* Double check state. */
d3fcc808 10667 for_each_intel_crtc(dev, intel_crtc) {
83d65738 10668 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7 10669 WARN_ON(intel_crtc->new_config &&
6e3c9717 10670 intel_crtc->new_config != intel_crtc->config);
83d65738 10671 WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
ea9d758d
DV
10672 }
10673
10674 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10675 if (!connector->encoder || !connector->encoder->crtc)
10676 continue;
10677
10678 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10679
10680 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10681 struct drm_property *dpms_property =
10682 dev->mode_config.dpms_property;
10683
ea9d758d 10684 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10685 drm_object_property_set_value(&connector->base,
68d34720
DV
10686 dpms_property,
10687 DRM_MODE_DPMS_ON);
ea9d758d
DV
10688
10689 intel_encoder = to_intel_encoder(connector->encoder);
10690 intel_encoder->connectors_active = true;
10691 }
10692 }
10693
10694}
10695
3bd26263 10696static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10697{
3bd26263 10698 int diff;
f1f644dc
JB
10699
10700 if (clock1 == clock2)
10701 return true;
10702
10703 if (!clock1 || !clock2)
10704 return false;
10705
10706 diff = abs(clock1 - clock2);
10707
10708 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10709 return true;
10710
10711 return false;
10712}
10713
25c5b266
DV
10714#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10715 list_for_each_entry((intel_crtc), \
10716 &(dev)->mode_config.crtc_list, \
10717 base.head) \
0973f18f 10718 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10719
0e8ffe1b 10720static bool
2fa2fe9a 10721intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
10722 struct intel_crtc_state *current_config,
10723 struct intel_crtc_state *pipe_config)
0e8ffe1b 10724{
66e985c0
DV
10725#define PIPE_CONF_CHECK_X(name) \
10726 if (current_config->name != pipe_config->name) { \
10727 DRM_ERROR("mismatch in " #name " " \
10728 "(expected 0x%08x, found 0x%08x)\n", \
10729 current_config->name, \
10730 pipe_config->name); \
10731 return false; \
10732 }
10733
08a24034
DV
10734#define PIPE_CONF_CHECK_I(name) \
10735 if (current_config->name != pipe_config->name) { \
10736 DRM_ERROR("mismatch in " #name " " \
10737 "(expected %i, found %i)\n", \
10738 current_config->name, \
10739 pipe_config->name); \
10740 return false; \
88adfff1
DV
10741 }
10742
b95af8be
VK
10743/* This is required for BDW+ where there is only one set of registers for
10744 * switching between high and low RR.
10745 * This macro can be used whenever a comparison has to be made between one
10746 * hw state and multiple sw state variables.
10747 */
10748#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10749 if ((current_config->name != pipe_config->name) && \
10750 (current_config->alt_name != pipe_config->name)) { \
10751 DRM_ERROR("mismatch in " #name " " \
10752 "(expected %i or %i, found %i)\n", \
10753 current_config->name, \
10754 current_config->alt_name, \
10755 pipe_config->name); \
10756 return false; \
10757 }
10758
1bd1bd80
DV
10759#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10760 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10761 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10762 "(expected %i, found %i)\n", \
10763 current_config->name & (mask), \
10764 pipe_config->name & (mask)); \
10765 return false; \
10766 }
10767
5e550656
VS
10768#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10769 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10770 DRM_ERROR("mismatch in " #name " " \
10771 "(expected %i, found %i)\n", \
10772 current_config->name, \
10773 pipe_config->name); \
10774 return false; \
10775 }
10776
bb760063
DV
10777#define PIPE_CONF_QUIRK(quirk) \
10778 ((current_config->quirks | pipe_config->quirks) & (quirk))
10779
eccb140b
DV
10780 PIPE_CONF_CHECK_I(cpu_transcoder);
10781
08a24034
DV
10782 PIPE_CONF_CHECK_I(has_pch_encoder);
10783 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10784 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10785 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10786 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10787 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10788 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10789
eb14cb74 10790 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10791
10792 if (INTEL_INFO(dev)->gen < 8) {
10793 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10794 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10795 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10796 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10797 PIPE_CONF_CHECK_I(dp_m_n.tu);
10798
10799 if (current_config->has_drrs) {
10800 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10801 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10802 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10803 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10804 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10805 }
10806 } else {
10807 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10808 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10809 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10810 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10811 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10812 }
eb14cb74 10813
2d112de7
ACO
10814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 10820
2d112de7
ACO
10821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10824 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10825 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10826 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 10827
c93f54cf 10828 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10829 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10830 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10831 IS_VALLEYVIEW(dev))
10832 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 10833 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 10834
9ed109a7
DV
10835 PIPE_CONF_CHECK_I(has_audio);
10836
2d112de7 10837 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
10838 DRM_MODE_FLAG_INTERLACE);
10839
bb760063 10840 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 10841 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10842 DRM_MODE_FLAG_PHSYNC);
2d112de7 10843 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10844 DRM_MODE_FLAG_NHSYNC);
2d112de7 10845 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 10846 DRM_MODE_FLAG_PVSYNC);
2d112de7 10847 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
10848 DRM_MODE_FLAG_NVSYNC);
10849 }
045ac3b5 10850
37327abd
VS
10851 PIPE_CONF_CHECK_I(pipe_src_w);
10852 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10853
9953599b
DV
10854 /*
10855 * FIXME: BIOS likes to set up a cloned config with lvds+external
10856 * screen. Since we don't yet re-compute the pipe config when moving
10857 * just the lvds port away to another pipe the sw tracking won't match.
10858 *
10859 * Proper atomic modesets with recomputed global state will fix this.
10860 * Until then just don't check gmch state for inherited modes.
10861 */
10862 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10863 PIPE_CONF_CHECK_I(gmch_pfit.control);
10864 /* pfit ratios are autocomputed by the hw on gen4+ */
10865 if (INTEL_INFO(dev)->gen < 4)
10866 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10867 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10868 }
10869
fd4daa9c
CW
10870 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10871 if (current_config->pch_pfit.enabled) {
10872 PIPE_CONF_CHECK_I(pch_pfit.pos);
10873 PIPE_CONF_CHECK_I(pch_pfit.size);
10874 }
2fa2fe9a 10875
e59150dc
JB
10876 /* BDW+ don't expose a synchronous way to read the state */
10877 if (IS_HASWELL(dev))
10878 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10879
282740f7
VS
10880 PIPE_CONF_CHECK_I(double_wide);
10881
26804afd
DV
10882 PIPE_CONF_CHECK_X(ddi_pll_sel);
10883
c0d43d62 10884 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10885 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10886 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10887 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10888 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10889 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
10890 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10891 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10892 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 10893
42571aef
VS
10894 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10895 PIPE_CONF_CHECK_I(pipe_bpp);
10896
2d112de7 10897 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 10898 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10899
66e985c0 10900#undef PIPE_CONF_CHECK_X
08a24034 10901#undef PIPE_CONF_CHECK_I
b95af8be 10902#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10903#undef PIPE_CONF_CHECK_FLAGS
5e550656 10904#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10905#undef PIPE_CONF_QUIRK
88adfff1 10906
0e8ffe1b
DV
10907 return true;
10908}
10909
08db6652
DL
10910static void check_wm_state(struct drm_device *dev)
10911{
10912 struct drm_i915_private *dev_priv = dev->dev_private;
10913 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10914 struct intel_crtc *intel_crtc;
10915 int plane;
10916
10917 if (INTEL_INFO(dev)->gen < 9)
10918 return;
10919
10920 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10921 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10922
10923 for_each_intel_crtc(dev, intel_crtc) {
10924 struct skl_ddb_entry *hw_entry, *sw_entry;
10925 const enum pipe pipe = intel_crtc->pipe;
10926
10927 if (!intel_crtc->active)
10928 continue;
10929
10930 /* planes */
dd740780 10931 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
10932 hw_entry = &hw_ddb.plane[pipe][plane];
10933 sw_entry = &sw_ddb->plane[pipe][plane];
10934
10935 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10936 continue;
10937
10938 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10939 "(expected (%u,%u), found (%u,%u))\n",
10940 pipe_name(pipe), plane + 1,
10941 sw_entry->start, sw_entry->end,
10942 hw_entry->start, hw_entry->end);
10943 }
10944
10945 /* cursor */
10946 hw_entry = &hw_ddb.cursor[pipe];
10947 sw_entry = &sw_ddb->cursor[pipe];
10948
10949 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10950 continue;
10951
10952 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10953 "(expected (%u,%u), found (%u,%u))\n",
10954 pipe_name(pipe),
10955 sw_entry->start, sw_entry->end,
10956 hw_entry->start, hw_entry->end);
10957 }
10958}
10959
91d1b4bd
DV
10960static void
10961check_connector_state(struct drm_device *dev)
8af6cf88 10962{
8af6cf88
DV
10963 struct intel_connector *connector;
10964
3a3371ff 10965 for_each_intel_connector(dev, connector) {
8af6cf88
DV
10966 /* This also checks the encoder/connector hw state with the
10967 * ->get_hw_state callbacks. */
10968 intel_connector_check_state(connector);
10969
e2c719b7 10970 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
10971 "connector's staged encoder doesn't match current encoder\n");
10972 }
91d1b4bd
DV
10973}
10974
10975static void
10976check_encoder_state(struct drm_device *dev)
10977{
10978 struct intel_encoder *encoder;
10979 struct intel_connector *connector;
8af6cf88 10980
b2784e15 10981 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10982 bool enabled = false;
10983 bool active = false;
10984 enum pipe pipe, tracked_pipe;
10985
10986 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10987 encoder->base.base.id,
8e329a03 10988 encoder->base.name);
8af6cf88 10989
e2c719b7 10990 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 10991 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 10992 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
10993 "encoder's active_connectors set, but no crtc\n");
10994
3a3371ff 10995 for_each_intel_connector(dev, connector) {
8af6cf88
DV
10996 if (connector->base.encoder != &encoder->base)
10997 continue;
10998 enabled = true;
10999 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11000 active = true;
11001 }
0e32b39c
DA
11002 /*
11003 * for MST connectors if we unplug the connector is gone
11004 * away but the encoder is still connected to a crtc
11005 * until a modeset happens in response to the hotplug.
11006 */
11007 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11008 continue;
11009
e2c719b7 11010 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11011 "encoder's enabled state mismatch "
11012 "(expected %i, found %i)\n",
11013 !!encoder->base.crtc, enabled);
e2c719b7 11014 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
11015 "active encoder with no crtc\n");
11016
e2c719b7 11017 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
11018 "encoder's computed active state doesn't match tracked active state "
11019 "(expected %i, found %i)\n", active, encoder->connectors_active);
11020
11021 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 11022 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
11023 "encoder's hw state doesn't match sw tracking "
11024 "(expected %i, found %i)\n",
11025 encoder->connectors_active, active);
11026
11027 if (!encoder->base.crtc)
11028 continue;
11029
11030 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 11031 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
11032 "active encoder's pipe doesn't match"
11033 "(expected %i, found %i)\n",
11034 tracked_pipe, pipe);
11035
11036 }
91d1b4bd
DV
11037}
11038
11039static void
11040check_crtc_state(struct drm_device *dev)
11041{
fbee40df 11042 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
11043 struct intel_crtc *crtc;
11044 struct intel_encoder *encoder;
5cec258b 11045 struct intel_crtc_state pipe_config;
8af6cf88 11046
d3fcc808 11047 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
11048 bool enabled = false;
11049 bool active = false;
11050
045ac3b5
JB
11051 memset(&pipe_config, 0, sizeof(pipe_config));
11052
8af6cf88
DV
11053 DRM_DEBUG_KMS("[CRTC:%d]\n",
11054 crtc->base.base.id);
11055
83d65738 11056 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
11057 "active crtc, but not enabled in sw tracking\n");
11058
b2784e15 11059 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11060 if (encoder->base.crtc != &crtc->base)
11061 continue;
11062 enabled = true;
11063 if (encoder->connectors_active)
11064 active = true;
11065 }
6c49f241 11066
e2c719b7 11067 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
11068 "crtc's computed active state doesn't match tracked active state "
11069 "(expected %i, found %i)\n", active, crtc->active);
83d65738 11070 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 11071 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
11072 "(expected %i, found %i)\n", enabled,
11073 crtc->base.state->enable);
8af6cf88 11074
0e8ffe1b
DV
11075 active = dev_priv->display.get_pipe_config(crtc,
11076 &pipe_config);
d62cf62a 11077
b6b5d049
VS
11078 /* hw state is inconsistent with the pipe quirk */
11079 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11080 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
11081 active = crtc->active;
11082
b2784e15 11083 for_each_intel_encoder(dev, encoder) {
3eaba51c 11084 enum pipe pipe;
6c49f241
DV
11085 if (encoder->base.crtc != &crtc->base)
11086 continue;
1d37b689 11087 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
11088 encoder->get_config(encoder, &pipe_config);
11089 }
11090
e2c719b7 11091 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
11092 "crtc active state doesn't match with hw state "
11093 "(expected %i, found %i)\n", crtc->active, active);
11094
c0b03411 11095 if (active &&
6e3c9717 11096 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 11097 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
11098 intel_dump_pipe_config(crtc, &pipe_config,
11099 "[hw state]");
6e3c9717 11100 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
11101 "[sw state]");
11102 }
8af6cf88
DV
11103 }
11104}
11105
91d1b4bd
DV
11106static void
11107check_shared_dpll_state(struct drm_device *dev)
11108{
fbee40df 11109 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
11110 struct intel_crtc *crtc;
11111 struct intel_dpll_hw_state dpll_hw_state;
11112 int i;
5358901f
DV
11113
11114 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11115 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11116 int enabled_crtcs = 0, active_crtcs = 0;
11117 bool active;
11118
11119 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11120
11121 DRM_DEBUG_KMS("%s\n", pll->name);
11122
11123 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11124
e2c719b7 11125 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 11126 "more active pll users than references: %i vs %i\n",
3e369b76 11127 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 11128 I915_STATE_WARN(pll->active && !pll->on,
5358901f 11129 "pll in active use but not on in sw tracking\n");
e2c719b7 11130 I915_STATE_WARN(pll->on && !pll->active,
35c95375 11131 "pll in on but not on in use in sw tracking\n");
e2c719b7 11132 I915_STATE_WARN(pll->on != active,
5358901f
DV
11133 "pll on state mismatch (expected %i, found %i)\n",
11134 pll->on, active);
11135
d3fcc808 11136 for_each_intel_crtc(dev, crtc) {
83d65738 11137 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
11138 enabled_crtcs++;
11139 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11140 active_crtcs++;
11141 }
e2c719b7 11142 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
11143 "pll active crtcs mismatch (expected %i, found %i)\n",
11144 pll->active, active_crtcs);
e2c719b7 11145 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 11146 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 11147 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 11148
e2c719b7 11149 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
11150 sizeof(dpll_hw_state)),
11151 "pll hw state mismatch\n");
5358901f 11152 }
8af6cf88
DV
11153}
11154
91d1b4bd
DV
11155void
11156intel_modeset_check_state(struct drm_device *dev)
11157{
08db6652 11158 check_wm_state(dev);
91d1b4bd
DV
11159 check_connector_state(dev);
11160 check_encoder_state(dev);
11161 check_crtc_state(dev);
11162 check_shared_dpll_state(dev);
11163}
11164
5cec258b 11165void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
11166 int dotclock)
11167{
11168 /*
11169 * FDI already provided one idea for the dotclock.
11170 * Yell if the encoder disagrees.
11171 */
2d112de7 11172 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 11173 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 11174 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
11175}
11176
80715b2f
VS
11177static void update_scanline_offset(struct intel_crtc *crtc)
11178{
11179 struct drm_device *dev = crtc->base.dev;
11180
11181 /*
11182 * The scanline counter increments at the leading edge of hsync.
11183 *
11184 * On most platforms it starts counting from vtotal-1 on the
11185 * first active line. That means the scanline counter value is
11186 * always one less than what we would expect. Ie. just after
11187 * start of vblank, which also occurs at start of hsync (on the
11188 * last active line), the scanline counter will read vblank_start-1.
11189 *
11190 * On gen2 the scanline counter starts counting from 1 instead
11191 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11192 * to keep the value positive), instead of adding one.
11193 *
11194 * On HSW+ the behaviour of the scanline counter depends on the output
11195 * type. For DP ports it behaves like most other platforms, but on HDMI
11196 * there's an extra 1 line difference. So we need to add two instead of
11197 * one to the value.
11198 */
11199 if (IS_GEN2(dev)) {
6e3c9717 11200 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
11201 int vtotal;
11202
11203 vtotal = mode->crtc_vtotal;
11204 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11205 vtotal /= 2;
11206
11207 crtc->scanline_offset = vtotal - 1;
11208 } else if (HAS_DDI(dev) &&
409ee761 11209 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
11210 crtc->scanline_offset = 2;
11211 } else
11212 crtc->scanline_offset = 1;
11213}
11214
5cec258b 11215static struct intel_crtc_state *
7f27126e
JB
11216intel_modeset_compute_config(struct drm_crtc *crtc,
11217 struct drm_display_mode *mode,
11218 struct drm_framebuffer *fb,
11219 unsigned *modeset_pipes,
11220 unsigned *prepare_pipes,
11221 unsigned *disable_pipes)
11222{
5cec258b 11223 struct intel_crtc_state *pipe_config = NULL;
7f27126e
JB
11224
11225 intel_modeset_affected_pipes(crtc, modeset_pipes,
11226 prepare_pipes, disable_pipes);
11227
11228 if ((*modeset_pipes) == 0)
11229 goto out;
11230
11231 /*
11232 * Note this needs changes when we start tracking multiple modes
11233 * and crtcs. At that point we'll need to compute the whole config
11234 * (i.e. one pipe_config for each crtc) rather than just the one
11235 * for this crtc.
11236 */
11237 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11238 if (IS_ERR(pipe_config)) {
11239 goto out;
11240 }
11241 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11242 "[modeset]");
7f27126e
JB
11243
11244out:
11245 return pipe_config;
11246}
11247
ed6739ef
ACO
11248static int __intel_set_mode_setup_plls(struct drm_device *dev,
11249 unsigned modeset_pipes,
11250 unsigned disable_pipes)
11251{
11252 struct drm_i915_private *dev_priv = to_i915(dev);
11253 unsigned clear_pipes = modeset_pipes | disable_pipes;
11254 struct intel_crtc *intel_crtc;
11255 int ret = 0;
11256
11257 if (!dev_priv->display.crtc_compute_clock)
11258 return 0;
11259
11260 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11261 if (ret)
11262 goto done;
11263
11264 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11265 struct intel_crtc_state *state = intel_crtc->new_config;
11266 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11267 state);
11268 if (ret) {
11269 intel_shared_dpll_abort_config(dev_priv);
11270 goto done;
11271 }
11272 }
11273
11274done:
11275 return ret;
11276}
11277
f30da187
DV
11278static int __intel_set_mode(struct drm_crtc *crtc,
11279 struct drm_display_mode *mode,
7f27126e 11280 int x, int y, struct drm_framebuffer *fb,
5cec258b 11281 struct intel_crtc_state *pipe_config,
7f27126e
JB
11282 unsigned modeset_pipes,
11283 unsigned prepare_pipes,
11284 unsigned disable_pipes)
a6778b3c
DV
11285{
11286 struct drm_device *dev = crtc->dev;
fbee40df 11287 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 11288 struct drm_display_mode *saved_mode;
25c5b266 11289 struct intel_crtc *intel_crtc;
c0c36b94 11290 int ret = 0;
a6778b3c 11291
4b4b9238 11292 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
11293 if (!saved_mode)
11294 return -ENOMEM;
a6778b3c 11295
3ac18232 11296 *saved_mode = crtc->mode;
a6778b3c 11297
b9950a13
VS
11298 if (modeset_pipes)
11299 to_intel_crtc(crtc)->new_config = pipe_config;
11300
30a970c6
JB
11301 /*
11302 * See if the config requires any additional preparation, e.g.
11303 * to adjust global state with pipes off. We need to do this
11304 * here so we can get the modeset_pipe updated config for the new
11305 * mode set on this crtc. For other crtcs we need to use the
11306 * adjusted_mode bits in the crtc directly.
11307 */
c164f833 11308 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 11309 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 11310
c164f833
VS
11311 /* may have added more to prepare_pipes than we should */
11312 prepare_pipes &= ~disable_pipes;
11313 }
11314
ed6739ef
ACO
11315 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11316 if (ret)
11317 goto done;
8bd31e67 11318
460da916
DV
11319 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11320 intel_crtc_disable(&intel_crtc->base);
11321
ea9d758d 11322 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
83d65738 11323 if (intel_crtc->base.state->enable)
ea9d758d
DV
11324 dev_priv->display.crtc_disable(&intel_crtc->base);
11325 }
a6778b3c 11326
6c4c86f5
DV
11327 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11328 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
11329 *
11330 * Note we'll need to fix this up when we start tracking multiple
11331 * pipes; here we assume a single modeset_pipe and only track the
11332 * single crtc and mode.
f6e5b160 11333 */
b8cecdf5 11334 if (modeset_pipes) {
25c5b266 11335 crtc->mode = *mode;
b8cecdf5
DV
11336 /* mode_set/enable/disable functions rely on a correct pipe
11337 * config. */
f5de6e07 11338 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
11339
11340 /*
11341 * Calculate and store various constants which
11342 * are later needed by vblank and swap-completion
11343 * timestamping. They are derived from true hwmode.
11344 */
11345 drm_calc_timestamping_constants(crtc,
2d112de7 11346 &pipe_config->base.adjusted_mode);
b8cecdf5 11347 }
7758a113 11348
ea9d758d
DV
11349 /* Only after disabling all output pipelines that will be changed can we
11350 * update the the output configuration. */
11351 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11352
50f6e502 11353 modeset_update_crtc_power_domains(dev);
47fab737 11354
a6778b3c
DV
11355 /* Set up the DPLL and any encoders state that needs to adjust or depend
11356 * on the DPLL.
f6e5b160 11357 */
25c5b266 11358 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
11359 struct drm_plane *primary = intel_crtc->base.primary;
11360 int vdisplay, hdisplay;
4c10794f 11361
455a6808
GP
11362 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11363 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11364 fb, 0, 0,
11365 hdisplay, vdisplay,
11366 x << 16, y << 16,
11367 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
11368 }
11369
11370 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11371 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11372 update_scanline_offset(intel_crtc);
11373
25c5b266 11374 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11375 }
a6778b3c 11376
a6778b3c
DV
11377 /* FIXME: add subpixel order */
11378done:
83d65738 11379 if (ret && crtc->state->enable)
3ac18232 11380 crtc->mode = *saved_mode;
a6778b3c 11381
3ac18232 11382 kfree(saved_mode);
a6778b3c 11383 return ret;
f6e5b160
CW
11384}
11385
7f27126e
JB
11386static int intel_set_mode_pipes(struct drm_crtc *crtc,
11387 struct drm_display_mode *mode,
11388 int x, int y, struct drm_framebuffer *fb,
5cec258b 11389 struct intel_crtc_state *pipe_config,
7f27126e
JB
11390 unsigned modeset_pipes,
11391 unsigned prepare_pipes,
11392 unsigned disable_pipes)
f30da187
DV
11393{
11394 int ret;
11395
7f27126e
JB
11396 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11397 prepare_pipes, disable_pipes);
f30da187
DV
11398
11399 if (ret == 0)
11400 intel_modeset_check_state(crtc->dev);
11401
11402 return ret;
11403}
11404
7f27126e
JB
11405static int intel_set_mode(struct drm_crtc *crtc,
11406 struct drm_display_mode *mode,
11407 int x, int y, struct drm_framebuffer *fb)
11408{
5cec258b 11409 struct intel_crtc_state *pipe_config;
7f27126e
JB
11410 unsigned modeset_pipes, prepare_pipes, disable_pipes;
11411
11412 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11413 &modeset_pipes,
11414 &prepare_pipes,
11415 &disable_pipes);
11416
11417 if (IS_ERR(pipe_config))
11418 return PTR_ERR(pipe_config);
11419
11420 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11421 modeset_pipes, prepare_pipes,
11422 disable_pipes);
11423}
11424
c0c36b94
CW
11425void intel_crtc_restore_mode(struct drm_crtc *crtc)
11426{
f4510a27 11427 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11428}
11429
25c5b266
DV
11430#undef for_each_intel_crtc_masked
11431
d9e55608
DV
11432static void intel_set_config_free(struct intel_set_config *config)
11433{
11434 if (!config)
11435 return;
11436
1aa4b628
DV
11437 kfree(config->save_connector_encoders);
11438 kfree(config->save_encoder_crtcs);
7668851f 11439 kfree(config->save_crtc_enabled);
d9e55608
DV
11440 kfree(config);
11441}
11442
85f9eb71
DV
11443static int intel_set_config_save_state(struct drm_device *dev,
11444 struct intel_set_config *config)
11445{
7668851f 11446 struct drm_crtc *crtc;
85f9eb71
DV
11447 struct drm_encoder *encoder;
11448 struct drm_connector *connector;
11449 int count;
11450
7668851f
VS
11451 config->save_crtc_enabled =
11452 kcalloc(dev->mode_config.num_crtc,
11453 sizeof(bool), GFP_KERNEL);
11454 if (!config->save_crtc_enabled)
11455 return -ENOMEM;
11456
1aa4b628
DV
11457 config->save_encoder_crtcs =
11458 kcalloc(dev->mode_config.num_encoder,
11459 sizeof(struct drm_crtc *), GFP_KERNEL);
11460 if (!config->save_encoder_crtcs)
85f9eb71
DV
11461 return -ENOMEM;
11462
1aa4b628
DV
11463 config->save_connector_encoders =
11464 kcalloc(dev->mode_config.num_connector,
11465 sizeof(struct drm_encoder *), GFP_KERNEL);
11466 if (!config->save_connector_encoders)
85f9eb71
DV
11467 return -ENOMEM;
11468
11469 /* Copy data. Note that driver private data is not affected.
11470 * Should anything bad happen only the expected state is
11471 * restored, not the drivers personal bookkeeping.
11472 */
7668851f 11473 count = 0;
70e1e0ec 11474 for_each_crtc(dev, crtc) {
83d65738 11475 config->save_crtc_enabled[count++] = crtc->state->enable;
7668851f
VS
11476 }
11477
85f9eb71
DV
11478 count = 0;
11479 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11480 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11481 }
11482
11483 count = 0;
11484 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11485 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11486 }
11487
11488 return 0;
11489}
11490
11491static void intel_set_config_restore_state(struct drm_device *dev,
11492 struct intel_set_config *config)
11493{
7668851f 11494 struct intel_crtc *crtc;
9a935856
DV
11495 struct intel_encoder *encoder;
11496 struct intel_connector *connector;
85f9eb71
DV
11497 int count;
11498
7668851f 11499 count = 0;
d3fcc808 11500 for_each_intel_crtc(dev, crtc) {
7668851f 11501 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11502
11503 if (crtc->new_enabled)
6e3c9717 11504 crtc->new_config = crtc->config;
7bd0a8e7
VS
11505 else
11506 crtc->new_config = NULL;
7668851f
VS
11507 }
11508
85f9eb71 11509 count = 0;
b2784e15 11510 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11511 encoder->new_crtc =
11512 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11513 }
11514
11515 count = 0;
3a3371ff 11516 for_each_intel_connector(dev, connector) {
9a935856
DV
11517 connector->new_encoder =
11518 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11519 }
11520}
11521
e3de42b6 11522static bool
2e57f47d 11523is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11524{
11525 int i;
11526
2e57f47d
CW
11527 if (set->num_connectors == 0)
11528 return false;
11529
11530 if (WARN_ON(set->connectors == NULL))
11531 return false;
11532
11533 for (i = 0; i < set->num_connectors; i++)
11534 if (set->connectors[i]->encoder &&
11535 set->connectors[i]->encoder->crtc == set->crtc &&
11536 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11537 return true;
11538
11539 return false;
11540}
11541
5e2b584e
DV
11542static void
11543intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11544 struct intel_set_config *config)
11545{
11546
11547 /* We should be able to check here if the fb has the same properties
11548 * and then just flip_or_move it */
2e57f47d
CW
11549 if (is_crtc_connector_off(set)) {
11550 config->mode_changed = true;
f4510a27 11551 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11552 /*
11553 * If we have no fb, we can only flip as long as the crtc is
11554 * active, otherwise we need a full mode set. The crtc may
11555 * be active if we've only disabled the primary plane, or
11556 * in fastboot situations.
11557 */
f4510a27 11558 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11559 struct intel_crtc *intel_crtc =
11560 to_intel_crtc(set->crtc);
11561
3b150f08 11562 if (intel_crtc->active) {
319d9827
JB
11563 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11564 config->fb_changed = true;
11565 } else {
11566 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11567 config->mode_changed = true;
11568 }
5e2b584e
DV
11569 } else if (set->fb == NULL) {
11570 config->mode_changed = true;
72f4901e 11571 } else if (set->fb->pixel_format !=
f4510a27 11572 set->crtc->primary->fb->pixel_format) {
5e2b584e 11573 config->mode_changed = true;
e3de42b6 11574 } else {
5e2b584e 11575 config->fb_changed = true;
e3de42b6 11576 }
5e2b584e
DV
11577 }
11578
835c5873 11579 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11580 config->fb_changed = true;
11581
11582 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11583 DRM_DEBUG_KMS("modes are different, full mode set\n");
11584 drm_mode_debug_printmodeline(&set->crtc->mode);
11585 drm_mode_debug_printmodeline(set->mode);
11586 config->mode_changed = true;
11587 }
a1d95703
CW
11588
11589 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11590 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11591}
11592
2e431051 11593static int
9a935856
DV
11594intel_modeset_stage_output_state(struct drm_device *dev,
11595 struct drm_mode_set *set,
11596 struct intel_set_config *config)
50f56119 11597{
9a935856
DV
11598 struct intel_connector *connector;
11599 struct intel_encoder *encoder;
7668851f 11600 struct intel_crtc *crtc;
f3f08572 11601 int ro;
50f56119 11602
9abdda74 11603 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11604 * of connectors. For paranoia, double-check this. */
11605 WARN_ON(!set->fb && (set->num_connectors != 0));
11606 WARN_ON(set->fb && (set->num_connectors == 0));
11607
3a3371ff 11608 for_each_intel_connector(dev, connector) {
9a935856
DV
11609 /* Otherwise traverse passed in connector list and get encoders
11610 * for them. */
50f56119 11611 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11612 if (set->connectors[ro] == &connector->base) {
0e32b39c 11613 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11614 break;
11615 }
11616 }
11617
9a935856
DV
11618 /* If we disable the crtc, disable all its connectors. Also, if
11619 * the connector is on the changing crtc but not on the new
11620 * connector list, disable it. */
11621 if ((!set->fb || ro == set->num_connectors) &&
11622 connector->base.encoder &&
11623 connector->base.encoder->crtc == set->crtc) {
11624 connector->new_encoder = NULL;
11625
11626 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11627 connector->base.base.id,
c23cc417 11628 connector->base.name);
9a935856
DV
11629 }
11630
11631
11632 if (&connector->new_encoder->base != connector->base.encoder) {
10634189
ACO
11633 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
11634 connector->base.base.id,
11635 connector->base.name);
5e2b584e 11636 config->mode_changed = true;
50f56119
DV
11637 }
11638 }
9a935856 11639 /* connector->new_encoder is now updated for all connectors. */
50f56119 11640
9a935856 11641 /* Update crtc of enabled connectors. */
3a3371ff 11642 for_each_intel_connector(dev, connector) {
7668851f
VS
11643 struct drm_crtc *new_crtc;
11644
9a935856 11645 if (!connector->new_encoder)
50f56119
DV
11646 continue;
11647
9a935856 11648 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11649
11650 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11651 if (set->connectors[ro] == &connector->base)
50f56119
DV
11652 new_crtc = set->crtc;
11653 }
11654
11655 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11656 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11657 new_crtc)) {
5e2b584e 11658 return -EINVAL;
50f56119 11659 }
0e32b39c 11660 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11661
11662 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11663 connector->base.base.id,
c23cc417 11664 connector->base.name,
9a935856
DV
11665 new_crtc->base.id);
11666 }
11667
11668 /* Check for any encoders that needs to be disabled. */
b2784e15 11669 for_each_intel_encoder(dev, encoder) {
5a65f358 11670 int num_connectors = 0;
3a3371ff 11671 for_each_intel_connector(dev, connector) {
9a935856
DV
11672 if (connector->new_encoder == encoder) {
11673 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11674 num_connectors++;
9a935856
DV
11675 }
11676 }
5a65f358
PZ
11677
11678 if (num_connectors == 0)
11679 encoder->new_crtc = NULL;
11680 else if (num_connectors > 1)
11681 return -EINVAL;
11682
9a935856
DV
11683 /* Only now check for crtc changes so we don't miss encoders
11684 * that will be disabled. */
11685 if (&encoder->new_crtc->base != encoder->base.crtc) {
10634189
ACO
11686 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
11687 encoder->base.base.id,
11688 encoder->base.name);
5e2b584e 11689 config->mode_changed = true;
50f56119
DV
11690 }
11691 }
9a935856 11692 /* Now we've also updated encoder->new_crtc for all encoders. */
3a3371ff 11693 for_each_intel_connector(dev, connector) {
0e32b39c
DA
11694 if (connector->new_encoder)
11695 if (connector->new_encoder != connector->encoder)
11696 connector->encoder = connector->new_encoder;
11697 }
d3fcc808 11698 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11699 crtc->new_enabled = false;
11700
b2784e15 11701 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11702 if (encoder->new_crtc == crtc) {
11703 crtc->new_enabled = true;
11704 break;
11705 }
11706 }
11707
83d65738 11708 if (crtc->new_enabled != crtc->base.state->enable) {
10634189
ACO
11709 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
11710 crtc->base.base.id,
7668851f
VS
11711 crtc->new_enabled ? "en" : "dis");
11712 config->mode_changed = true;
11713 }
7bd0a8e7
VS
11714
11715 if (crtc->new_enabled)
6e3c9717 11716 crtc->new_config = crtc->config;
7bd0a8e7
VS
11717 else
11718 crtc->new_config = NULL;
7668851f
VS
11719 }
11720
2e431051
DV
11721 return 0;
11722}
11723
7d00a1f5
VS
11724static void disable_crtc_nofb(struct intel_crtc *crtc)
11725{
11726 struct drm_device *dev = crtc->base.dev;
11727 struct intel_encoder *encoder;
11728 struct intel_connector *connector;
11729
11730 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11731 pipe_name(crtc->pipe));
11732
3a3371ff 11733 for_each_intel_connector(dev, connector) {
7d00a1f5
VS
11734 if (connector->new_encoder &&
11735 connector->new_encoder->new_crtc == crtc)
11736 connector->new_encoder = NULL;
11737 }
11738
b2784e15 11739 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11740 if (encoder->new_crtc == crtc)
11741 encoder->new_crtc = NULL;
11742 }
11743
11744 crtc->new_enabled = false;
7bd0a8e7 11745 crtc->new_config = NULL;
7d00a1f5
VS
11746}
11747
2e431051
DV
11748static int intel_crtc_set_config(struct drm_mode_set *set)
11749{
11750 struct drm_device *dev;
2e431051
DV
11751 struct drm_mode_set save_set;
11752 struct intel_set_config *config;
5cec258b 11753 struct intel_crtc_state *pipe_config;
50f52756 11754 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 11755 int ret;
2e431051 11756
8d3e375e
DV
11757 BUG_ON(!set);
11758 BUG_ON(!set->crtc);
11759 BUG_ON(!set->crtc->helper_private);
2e431051 11760
7e53f3a4
DV
11761 /* Enforce sane interface api - has been abused by the fb helper. */
11762 BUG_ON(!set->mode && set->fb);
11763 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11764
2e431051
DV
11765 if (set->fb) {
11766 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11767 set->crtc->base.id, set->fb->base.id,
11768 (int)set->num_connectors, set->x, set->y);
11769 } else {
11770 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11771 }
11772
11773 dev = set->crtc->dev;
11774
11775 ret = -ENOMEM;
11776 config = kzalloc(sizeof(*config), GFP_KERNEL);
11777 if (!config)
11778 goto out_config;
11779
11780 ret = intel_set_config_save_state(dev, config);
11781 if (ret)
11782 goto out_config;
11783
11784 save_set.crtc = set->crtc;
11785 save_set.mode = &set->crtc->mode;
11786 save_set.x = set->crtc->x;
11787 save_set.y = set->crtc->y;
f4510a27 11788 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11789
11790 /* Compute whether we need a full modeset, only an fb base update or no
11791 * change at all. In the future we might also check whether only the
11792 * mode changed, e.g. for LVDS where we only change the panel fitter in
11793 * such cases. */
11794 intel_set_config_compute_mode_changes(set, config);
11795
9a935856 11796 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11797 if (ret)
11798 goto fail;
11799
50f52756
JB
11800 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11801 set->fb,
11802 &modeset_pipes,
11803 &prepare_pipes,
11804 &disable_pipes);
20664591 11805 if (IS_ERR(pipe_config)) {
6ac0483b 11806 ret = PTR_ERR(pipe_config);
50f52756 11807 goto fail;
20664591 11808 } else if (pipe_config) {
b9950a13 11809 if (pipe_config->has_audio !=
6e3c9717 11810 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
11811 config->mode_changed = true;
11812
af15d2ce
JB
11813 /*
11814 * Note we have an issue here with infoframes: current code
11815 * only updates them on the full mode set path per hw
11816 * requirements. So here we should be checking for any
11817 * required changes and forcing a mode set.
11818 */
20664591 11819 }
50f52756
JB
11820
11821 /* set_mode will free it in the mode_changed case */
11822 if (!config->mode_changed)
11823 kfree(pipe_config);
11824
1f9954d0
JB
11825 intel_update_pipe_size(to_intel_crtc(set->crtc));
11826
5e2b584e 11827 if (config->mode_changed) {
50f52756
JB
11828 ret = intel_set_mode_pipes(set->crtc, set->mode,
11829 set->x, set->y, set->fb, pipe_config,
11830 modeset_pipes, prepare_pipes,
11831 disable_pipes);
5e2b584e 11832 } else if (config->fb_changed) {
3b150f08 11833 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808
GP
11834 struct drm_plane *primary = set->crtc->primary;
11835 int vdisplay, hdisplay;
3b150f08 11836
455a6808
GP
11837 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11838 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11839 0, 0, hdisplay, vdisplay,
11840 set->x << 16, set->y << 16,
11841 hdisplay << 16, vdisplay << 16);
3b150f08
MR
11842
11843 /*
11844 * We need to make sure the primary plane is re-enabled if it
11845 * has previously been turned off.
11846 */
11847 if (!intel_crtc->primary_enabled && ret == 0) {
11848 WARN_ON(!intel_crtc->active);
fdd508a6 11849 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11850 }
11851
7ca51a3a
JB
11852 /*
11853 * In the fastboot case this may be our only check of the
11854 * state after boot. It would be better to only do it on
11855 * the first update, but we don't have a nice way of doing that
11856 * (and really, set_config isn't used much for high freq page
11857 * flipping, so increasing its cost here shouldn't be a big
11858 * deal).
11859 */
d330a953 11860 if (i915.fastboot && ret == 0)
7ca51a3a 11861 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11862 }
11863
2d05eae1 11864 if (ret) {
bf67dfeb
DV
11865 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11866 set->crtc->base.id, ret);
50f56119 11867fail:
2d05eae1 11868 intel_set_config_restore_state(dev, config);
50f56119 11869
7d00a1f5
VS
11870 /*
11871 * HACK: if the pipe was on, but we didn't have a framebuffer,
11872 * force the pipe off to avoid oopsing in the modeset code
11873 * due to fb==NULL. This should only happen during boot since
11874 * we don't yet reconstruct the FB from the hardware state.
11875 */
11876 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11877 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11878
2d05eae1
CW
11879 /* Try to restore the config */
11880 if (config->mode_changed &&
11881 intel_set_mode(save_set.crtc, save_set.mode,
11882 save_set.x, save_set.y, save_set.fb))
11883 DRM_ERROR("failed to restore config after modeset failure\n");
11884 }
50f56119 11885
d9e55608
DV
11886out_config:
11887 intel_set_config_free(config);
50f56119
DV
11888 return ret;
11889}
f6e5b160
CW
11890
11891static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11892 .gamma_set = intel_crtc_gamma_set,
50f56119 11893 .set_config = intel_crtc_set_config,
f6e5b160
CW
11894 .destroy = intel_crtc_destroy,
11895 .page_flip = intel_crtc_page_flip,
1356837e
MR
11896 .atomic_duplicate_state = intel_crtc_duplicate_state,
11897 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
11898};
11899
5358901f
DV
11900static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11901 struct intel_shared_dpll *pll,
11902 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11903{
5358901f 11904 uint32_t val;
ee7b9f93 11905
f458ebbc 11906 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
11907 return false;
11908
5358901f 11909 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11910 hw_state->dpll = val;
11911 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11912 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11913
11914 return val & DPLL_VCO_ENABLE;
11915}
11916
15bdd4cf
DV
11917static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11918 struct intel_shared_dpll *pll)
11919{
3e369b76
ACO
11920 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11921 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
11922}
11923
e7b903d2
DV
11924static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11925 struct intel_shared_dpll *pll)
11926{
e7b903d2 11927 /* PCH refclock must be enabled first */
89eff4be 11928 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11929
3e369b76 11930 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
11931
11932 /* Wait for the clocks to stabilize. */
11933 POSTING_READ(PCH_DPLL(pll->id));
11934 udelay(150);
11935
11936 /* The pixel multiplier can only be updated once the
11937 * DPLL is enabled and the clocks are stable.
11938 *
11939 * So write it again.
11940 */
3e369b76 11941 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 11942 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11943 udelay(200);
11944}
11945
11946static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11947 struct intel_shared_dpll *pll)
11948{
11949 struct drm_device *dev = dev_priv->dev;
11950 struct intel_crtc *crtc;
e7b903d2
DV
11951
11952 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11953 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11954 if (intel_crtc_to_shared_dpll(crtc) == pll)
11955 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11956 }
11957
15bdd4cf
DV
11958 I915_WRITE(PCH_DPLL(pll->id), 0);
11959 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11960 udelay(200);
11961}
11962
46edb027
DV
11963static char *ibx_pch_dpll_names[] = {
11964 "PCH DPLL A",
11965 "PCH DPLL B",
11966};
11967
7c74ade1 11968static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11969{
e7b903d2 11970 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11971 int i;
11972
7c74ade1 11973 dev_priv->num_shared_dpll = 2;
ee7b9f93 11974
e72f9fbf 11975 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11976 dev_priv->shared_dplls[i].id = i;
11977 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11978 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11979 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11980 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11981 dev_priv->shared_dplls[i].get_hw_state =
11982 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11983 }
11984}
11985
7c74ade1
DV
11986static void intel_shared_dpll_init(struct drm_device *dev)
11987{
e7b903d2 11988 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11989
9cd86933
DV
11990 if (HAS_DDI(dev))
11991 intel_ddi_pll_init(dev);
11992 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11993 ibx_pch_dpll_init(dev);
11994 else
11995 dev_priv->num_shared_dpll = 0;
11996
11997 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11998}
11999
6beb8c23
MR
12000/**
12001 * intel_prepare_plane_fb - Prepare fb for usage on plane
12002 * @plane: drm plane to prepare for
12003 * @fb: framebuffer to prepare for presentation
12004 *
12005 * Prepares a framebuffer for usage on a display plane. Generally this
12006 * involves pinning the underlying object and updating the frontbuffer tracking
12007 * bits. Some older platforms need special physical address handling for
12008 * cursor planes.
12009 *
12010 * Returns 0 on success, negative error code on failure.
12011 */
12012int
12013intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
12014 struct drm_framebuffer *fb,
12015 const struct drm_plane_state *new_state)
465c120c
MR
12016{
12017 struct drm_device *dev = plane->dev;
6beb8c23
MR
12018 struct intel_plane *intel_plane = to_intel_plane(plane);
12019 enum pipe pipe = intel_plane->pipe;
12020 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12021 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12022 unsigned frontbuffer_bits = 0;
12023 int ret = 0;
465c120c 12024
ea2c67bb 12025 if (!obj)
465c120c
MR
12026 return 0;
12027
6beb8c23
MR
12028 switch (plane->type) {
12029 case DRM_PLANE_TYPE_PRIMARY:
12030 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12031 break;
12032 case DRM_PLANE_TYPE_CURSOR:
12033 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12034 break;
12035 case DRM_PLANE_TYPE_OVERLAY:
12036 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12037 break;
12038 }
465c120c 12039
6beb8c23 12040 mutex_lock(&dev->struct_mutex);
465c120c 12041
6beb8c23
MR
12042 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12043 INTEL_INFO(dev)->cursor_needs_physical) {
12044 int align = IS_I830(dev) ? 16 * 1024 : 256;
12045 ret = i915_gem_object_attach_phys(obj, align);
12046 if (ret)
12047 DRM_DEBUG_KMS("failed to attach phys object\n");
12048 } else {
82bc3b2d 12049 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 12050 }
465c120c 12051
6beb8c23
MR
12052 if (ret == 0)
12053 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 12054
4c34574f 12055 mutex_unlock(&dev->struct_mutex);
465c120c 12056
6beb8c23
MR
12057 return ret;
12058}
12059
38f3ce3a
MR
12060/**
12061 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12062 * @plane: drm plane to clean up for
12063 * @fb: old framebuffer that was on plane
12064 *
12065 * Cleans up a framebuffer that has just been removed from a plane.
12066 */
12067void
12068intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
12069 struct drm_framebuffer *fb,
12070 const struct drm_plane_state *old_state)
38f3ce3a
MR
12071{
12072 struct drm_device *dev = plane->dev;
12073 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12074
12075 if (WARN_ON(!obj))
12076 return;
12077
12078 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12079 !INTEL_INFO(dev)->cursor_needs_physical) {
12080 mutex_lock(&dev->struct_mutex);
82bc3b2d 12081 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
12082 mutex_unlock(&dev->struct_mutex);
12083 }
465c120c
MR
12084}
12085
12086static int
3c692a41
GP
12087intel_check_primary_plane(struct drm_plane *plane,
12088 struct intel_plane_state *state)
12089{
32b7eeec
MR
12090 struct drm_device *dev = plane->dev;
12091 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 12092 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 12093 struct intel_crtc *intel_crtc;
2b875c22 12094 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
12095 struct drm_rect *dest = &state->dst;
12096 struct drm_rect *src = &state->src;
12097 const struct drm_rect *clip = &state->clip;
465c120c
MR
12098 int ret;
12099
ea2c67bb
MR
12100 crtc = crtc ? crtc : plane->crtc;
12101 intel_crtc = to_intel_crtc(crtc);
12102
c59cb179
MR
12103 ret = drm_plane_helper_check_update(plane, crtc, fb,
12104 src, dest, clip,
12105 DRM_PLANE_HELPER_NO_SCALING,
12106 DRM_PLANE_HELPER_NO_SCALING,
12107 false, true, &state->visible);
12108 if (ret)
12109 return ret;
465c120c 12110
32b7eeec
MR
12111 if (intel_crtc->active) {
12112 intel_crtc->atomic.wait_for_flips = true;
12113
12114 /*
12115 * FBC does not work on some platforms for rotated
12116 * planes, so disable it when rotation is not 0 and
12117 * update it when rotation is set back to 0.
12118 *
12119 * FIXME: This is redundant with the fbc update done in
12120 * the primary plane enable function except that that
12121 * one is done too late. We eventually need to unify
12122 * this.
12123 */
12124 if (intel_crtc->primary_enabled &&
12125 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 12126 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 12127 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
12128 intel_crtc->atomic.disable_fbc = true;
12129 }
12130
12131 if (state->visible) {
12132 /*
12133 * BDW signals flip done immediately if the plane
12134 * is disabled, even if the plane enable is already
12135 * armed to occur at the next vblank :(
12136 */
12137 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12138 intel_crtc->atomic.wait_vblank = true;
12139 }
12140
12141 intel_crtc->atomic.fb_bits |=
12142 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12143
12144 intel_crtc->atomic.update_fbc = true;
0fda6568
TU
12145
12146 /* Update watermarks on tiling changes. */
12147 if (!plane->state->fb || !state->base.fb ||
12148 plane->state->fb->modifier[0] !=
12149 state->base.fb->modifier[0])
12150 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
12151 }
12152
14af293f
GP
12153 return 0;
12154}
12155
12156static void
12157intel_commit_primary_plane(struct drm_plane *plane,
12158 struct intel_plane_state *state)
12159{
2b875c22
MR
12160 struct drm_crtc *crtc = state->base.crtc;
12161 struct drm_framebuffer *fb = state->base.fb;
12162 struct drm_device *dev = plane->dev;
14af293f 12163 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 12164 struct intel_crtc *intel_crtc;
14af293f
GP
12165 struct drm_rect *src = &state->src;
12166
ea2c67bb
MR
12167 crtc = crtc ? crtc : plane->crtc;
12168 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
12169
12170 plane->fb = fb;
9dc806fc
MR
12171 crtc->x = src->x1 >> 16;
12172 crtc->y = src->y1 >> 16;
ccc759dc 12173
ccc759dc 12174 if (intel_crtc->active) {
ccc759dc 12175 if (state->visible) {
ccc759dc
GP
12176 /* FIXME: kill this fastboot hack */
12177 intel_update_pipe_size(intel_crtc);
465c120c 12178
ccc759dc 12179 intel_crtc->primary_enabled = true;
465c120c 12180
ccc759dc
GP
12181 dev_priv->display.update_primary_plane(crtc, plane->fb,
12182 crtc->x, crtc->y);
ccc759dc
GP
12183 } else {
12184 /*
12185 * If clipping results in a non-visible primary plane,
12186 * we'll disable the primary plane. Note that this is
12187 * a bit different than what happens if userspace
12188 * explicitly disables the plane by passing fb=0
12189 * because plane->fb still gets set and pinned.
12190 */
12191 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 12192 }
ccc759dc 12193 }
465c120c
MR
12194}
12195
32b7eeec 12196static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 12197{
32b7eeec 12198 struct drm_device *dev = crtc->dev;
140fd38d 12199 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 12200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
12201 struct intel_plane *intel_plane;
12202 struct drm_plane *p;
12203 unsigned fb_bits = 0;
12204
12205 /* Track fb's for any planes being disabled */
12206 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12207 intel_plane = to_intel_plane(p);
12208
12209 if (intel_crtc->atomic.disabled_planes &
12210 (1 << drm_plane_index(p))) {
12211 switch (p->type) {
12212 case DRM_PLANE_TYPE_PRIMARY:
12213 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12214 break;
12215 case DRM_PLANE_TYPE_CURSOR:
12216 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12217 break;
12218 case DRM_PLANE_TYPE_OVERLAY:
12219 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12220 break;
12221 }
3c692a41 12222
ea2c67bb
MR
12223 mutex_lock(&dev->struct_mutex);
12224 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12225 mutex_unlock(&dev->struct_mutex);
12226 }
12227 }
3c692a41 12228
32b7eeec
MR
12229 if (intel_crtc->atomic.wait_for_flips)
12230 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 12231
32b7eeec
MR
12232 if (intel_crtc->atomic.disable_fbc)
12233 intel_fbc_disable(dev);
3c692a41 12234
32b7eeec
MR
12235 if (intel_crtc->atomic.pre_disable_primary)
12236 intel_pre_disable_primary(crtc);
3c692a41 12237
32b7eeec
MR
12238 if (intel_crtc->atomic.update_wm)
12239 intel_update_watermarks(crtc);
3c692a41 12240
32b7eeec 12241 intel_runtime_pm_get(dev_priv);
3c692a41 12242
c34c9ee4
MR
12243 /* Perform vblank evasion around commit operation */
12244 if (intel_crtc->active)
12245 intel_crtc->atomic.evade =
12246 intel_pipe_update_start(intel_crtc,
12247 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
12248}
12249
12250static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12251{
12252 struct drm_device *dev = crtc->dev;
12253 struct drm_i915_private *dev_priv = dev->dev_private;
12254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12255 struct drm_plane *p;
12256
c34c9ee4
MR
12257 if (intel_crtc->atomic.evade)
12258 intel_pipe_update_end(intel_crtc,
12259 intel_crtc->atomic.start_vbl_count);
3c692a41 12260
140fd38d 12261 intel_runtime_pm_put(dev_priv);
3c692a41 12262
32b7eeec
MR
12263 if (intel_crtc->atomic.wait_vblank)
12264 intel_wait_for_vblank(dev, intel_crtc->pipe);
12265
12266 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12267
12268 if (intel_crtc->atomic.update_fbc) {
ccc759dc 12269 mutex_lock(&dev->struct_mutex);
7ff0ebcc 12270 intel_fbc_update(dev);
ccc759dc 12271 mutex_unlock(&dev->struct_mutex);
38f3ce3a 12272 }
3c692a41 12273
32b7eeec
MR
12274 if (intel_crtc->atomic.post_enable_primary)
12275 intel_post_enable_primary(crtc);
3c692a41 12276
32b7eeec
MR
12277 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12278 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12279 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12280 false, false);
12281
12282 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
12283}
12284
cf4c7c12 12285/**
4a3b8769
MR
12286 * intel_plane_destroy - destroy a plane
12287 * @plane: plane to destroy
cf4c7c12 12288 *
4a3b8769
MR
12289 * Common destruction function for all types of planes (primary, cursor,
12290 * sprite).
cf4c7c12 12291 */
4a3b8769 12292void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
12293{
12294 struct intel_plane *intel_plane = to_intel_plane(plane);
12295 drm_plane_cleanup(plane);
12296 kfree(intel_plane);
12297}
12298
65a3fea0 12299const struct drm_plane_funcs intel_plane_funcs = {
ff42e093
DV
12300 .update_plane = drm_plane_helper_update,
12301 .disable_plane = drm_plane_helper_disable,
3d7d6510 12302 .destroy = intel_plane_destroy,
c196e1d6 12303 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
12304 .atomic_get_property = intel_plane_atomic_get_property,
12305 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
12306 .atomic_duplicate_state = intel_plane_duplicate_state,
12307 .atomic_destroy_state = intel_plane_destroy_state,
12308
465c120c
MR
12309};
12310
12311static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12312 int pipe)
12313{
12314 struct intel_plane *primary;
8e7d688b 12315 struct intel_plane_state *state;
465c120c
MR
12316 const uint32_t *intel_primary_formats;
12317 int num_formats;
12318
12319 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12320 if (primary == NULL)
12321 return NULL;
12322
8e7d688b
MR
12323 state = intel_create_plane_state(&primary->base);
12324 if (!state) {
ea2c67bb
MR
12325 kfree(primary);
12326 return NULL;
12327 }
8e7d688b 12328 primary->base.state = &state->base;
ea2c67bb 12329
465c120c
MR
12330 primary->can_scale = false;
12331 primary->max_downscale = 1;
12332 primary->pipe = pipe;
12333 primary->plane = pipe;
c59cb179
MR
12334 primary->check_plane = intel_check_primary_plane;
12335 primary->commit_plane = intel_commit_primary_plane;
465c120c
MR
12336 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12337 primary->plane = !pipe;
12338
12339 if (INTEL_INFO(dev)->gen <= 3) {
12340 intel_primary_formats = intel_primary_formats_gen2;
12341 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12342 } else {
12343 intel_primary_formats = intel_primary_formats_gen4;
12344 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12345 }
12346
12347 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 12348 &intel_plane_funcs,
465c120c
MR
12349 intel_primary_formats, num_formats,
12350 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
12351
12352 if (INTEL_INFO(dev)->gen >= 4) {
12353 if (!dev->mode_config.rotation_property)
12354 dev->mode_config.rotation_property =
12355 drm_mode_create_rotation_property(dev,
12356 BIT(DRM_ROTATE_0) |
12357 BIT(DRM_ROTATE_180));
12358 if (dev->mode_config.rotation_property)
12359 drm_object_attach_property(&primary->base.base,
12360 dev->mode_config.rotation_property,
8e7d688b 12361 state->base.rotation);
48404c1e
SJ
12362 }
12363
ea2c67bb
MR
12364 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12365
465c120c
MR
12366 return &primary->base;
12367}
12368
3d7d6510 12369static int
852e787c
GP
12370intel_check_cursor_plane(struct drm_plane *plane,
12371 struct intel_plane_state *state)
3d7d6510 12372{
2b875c22 12373 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 12374 struct drm_device *dev = plane->dev;
2b875c22 12375 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
12376 struct drm_rect *dest = &state->dst;
12377 struct drm_rect *src = &state->src;
12378 const struct drm_rect *clip = &state->clip;
757f9a3e 12379 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 12380 struct intel_crtc *intel_crtc;
757f9a3e
GP
12381 unsigned stride;
12382 int ret;
3d7d6510 12383
ea2c67bb
MR
12384 crtc = crtc ? crtc : plane->crtc;
12385 intel_crtc = to_intel_crtc(crtc);
12386
757f9a3e 12387 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 12388 src, dest, clip,
3d7d6510
MR
12389 DRM_PLANE_HELPER_NO_SCALING,
12390 DRM_PLANE_HELPER_NO_SCALING,
852e787c 12391 true, true, &state->visible);
757f9a3e
GP
12392 if (ret)
12393 return ret;
12394
12395
12396 /* if we want to turn off the cursor ignore width and height */
12397 if (!obj)
32b7eeec 12398 goto finish;
757f9a3e 12399
757f9a3e 12400 /* Check for which cursor types we support */
ea2c67bb
MR
12401 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12402 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12403 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
12404 return -EINVAL;
12405 }
12406
ea2c67bb
MR
12407 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12408 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
12409 DRM_DEBUG_KMS("buffer is too small\n");
12410 return -ENOMEM;
12411 }
12412
3a656b54 12413 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
12414 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12415 ret = -EINVAL;
12416 }
757f9a3e 12417
32b7eeec
MR
12418finish:
12419 if (intel_crtc->active) {
3749f463 12420 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
12421 intel_crtc->atomic.update_wm = true;
12422
12423 intel_crtc->atomic.fb_bits |=
12424 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12425 }
12426
757f9a3e 12427 return ret;
852e787c 12428}
3d7d6510 12429
f4a2cf29 12430static void
852e787c
GP
12431intel_commit_cursor_plane(struct drm_plane *plane,
12432 struct intel_plane_state *state)
12433{
2b875c22 12434 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
12435 struct drm_device *dev = plane->dev;
12436 struct intel_crtc *intel_crtc;
2b875c22 12437 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 12438 uint32_t addr;
852e787c 12439
ea2c67bb
MR
12440 crtc = crtc ? crtc : plane->crtc;
12441 intel_crtc = to_intel_crtc(crtc);
12442
2b875c22 12443 plane->fb = state->base.fb;
ea2c67bb
MR
12444 crtc->cursor_x = state->base.crtc_x;
12445 crtc->cursor_y = state->base.crtc_y;
12446
a912f12f
GP
12447 if (intel_crtc->cursor_bo == obj)
12448 goto update;
4ed91096 12449
f4a2cf29 12450 if (!obj)
a912f12f 12451 addr = 0;
f4a2cf29 12452 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 12453 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 12454 else
a912f12f 12455 addr = obj->phys_handle->busaddr;
852e787c 12456
a912f12f
GP
12457 intel_crtc->cursor_addr = addr;
12458 intel_crtc->cursor_bo = obj;
12459update:
852e787c 12460
32b7eeec 12461 if (intel_crtc->active)
a912f12f 12462 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
12463}
12464
3d7d6510
MR
12465static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12466 int pipe)
12467{
12468 struct intel_plane *cursor;
8e7d688b 12469 struct intel_plane_state *state;
3d7d6510
MR
12470
12471 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12472 if (cursor == NULL)
12473 return NULL;
12474
8e7d688b
MR
12475 state = intel_create_plane_state(&cursor->base);
12476 if (!state) {
ea2c67bb
MR
12477 kfree(cursor);
12478 return NULL;
12479 }
8e7d688b 12480 cursor->base.state = &state->base;
ea2c67bb 12481
3d7d6510
MR
12482 cursor->can_scale = false;
12483 cursor->max_downscale = 1;
12484 cursor->pipe = pipe;
12485 cursor->plane = pipe;
c59cb179
MR
12486 cursor->check_plane = intel_check_cursor_plane;
12487 cursor->commit_plane = intel_commit_cursor_plane;
3d7d6510
MR
12488
12489 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 12490 &intel_plane_funcs,
3d7d6510
MR
12491 intel_cursor_formats,
12492 ARRAY_SIZE(intel_cursor_formats),
12493 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
12494
12495 if (INTEL_INFO(dev)->gen >= 4) {
12496 if (!dev->mode_config.rotation_property)
12497 dev->mode_config.rotation_property =
12498 drm_mode_create_rotation_property(dev,
12499 BIT(DRM_ROTATE_0) |
12500 BIT(DRM_ROTATE_180));
12501 if (dev->mode_config.rotation_property)
12502 drm_object_attach_property(&cursor->base.base,
12503 dev->mode_config.rotation_property,
8e7d688b 12504 state->base.rotation);
4398ad45
VS
12505 }
12506
ea2c67bb
MR
12507 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12508
3d7d6510
MR
12509 return &cursor->base;
12510}
12511
b358d0a6 12512static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12513{
fbee40df 12514 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12515 struct intel_crtc *intel_crtc;
f5de6e07 12516 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
12517 struct drm_plane *primary = NULL;
12518 struct drm_plane *cursor = NULL;
465c120c 12519 int i, ret;
79e53945 12520
955382f3 12521 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12522 if (intel_crtc == NULL)
12523 return;
12524
f5de6e07
ACO
12525 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12526 if (!crtc_state)
12527 goto fail;
12528 intel_crtc_set_state(intel_crtc, crtc_state);
07878248 12529 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 12530
465c120c 12531 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12532 if (!primary)
12533 goto fail;
12534
12535 cursor = intel_cursor_plane_create(dev, pipe);
12536 if (!cursor)
12537 goto fail;
12538
465c120c 12539 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12540 cursor, &intel_crtc_funcs);
12541 if (ret)
12542 goto fail;
79e53945
JB
12543
12544 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12545 for (i = 0; i < 256; i++) {
12546 intel_crtc->lut_r[i] = i;
12547 intel_crtc->lut_g[i] = i;
12548 intel_crtc->lut_b[i] = i;
12549 }
12550
1f1c2e24
VS
12551 /*
12552 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12553 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12554 */
80824003
JB
12555 intel_crtc->pipe = pipe;
12556 intel_crtc->plane = pipe;
3a77c4c4 12557 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12558 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12559 intel_crtc->plane = !pipe;
80824003
JB
12560 }
12561
4b0e333e
CW
12562 intel_crtc->cursor_base = ~0;
12563 intel_crtc->cursor_cntl = ~0;
dc41c154 12564 intel_crtc->cursor_size = ~0;
8d7849db 12565
22fd0fab
JB
12566 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12567 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12568 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12569 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12570
9362c7c5
ACO
12571 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12572
79e53945 12573 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12574
12575 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12576 return;
12577
12578fail:
12579 if (primary)
12580 drm_plane_cleanup(primary);
12581 if (cursor)
12582 drm_plane_cleanup(cursor);
f5de6e07 12583 kfree(crtc_state);
3d7d6510 12584 kfree(intel_crtc);
79e53945
JB
12585}
12586
752aa88a
JB
12587enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12588{
12589 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12590 struct drm_device *dev = connector->base.dev;
752aa88a 12591
51fd371b 12592 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 12593
d3babd3f 12594 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
12595 return INVALID_PIPE;
12596
12597 return to_intel_crtc(encoder->crtc)->pipe;
12598}
12599
08d7b3d1 12600int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12601 struct drm_file *file)
08d7b3d1 12602{
08d7b3d1 12603 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12604 struct drm_crtc *drmmode_crtc;
c05422d5 12605 struct intel_crtc *crtc;
08d7b3d1 12606
7707e653 12607 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12608
7707e653 12609 if (!drmmode_crtc) {
08d7b3d1 12610 DRM_ERROR("no such CRTC id\n");
3f2c2057 12611 return -ENOENT;
08d7b3d1
CW
12612 }
12613
7707e653 12614 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12615 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12616
c05422d5 12617 return 0;
08d7b3d1
CW
12618}
12619
66a9278e 12620static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12621{
66a9278e
DV
12622 struct drm_device *dev = encoder->base.dev;
12623 struct intel_encoder *source_encoder;
79e53945 12624 int index_mask = 0;
79e53945
JB
12625 int entry = 0;
12626
b2784e15 12627 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12628 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12629 index_mask |= (1 << entry);
12630
79e53945
JB
12631 entry++;
12632 }
4ef69c7a 12633
79e53945
JB
12634 return index_mask;
12635}
12636
4d302442
CW
12637static bool has_edp_a(struct drm_device *dev)
12638{
12639 struct drm_i915_private *dev_priv = dev->dev_private;
12640
12641 if (!IS_MOBILE(dev))
12642 return false;
12643
12644 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12645 return false;
12646
e3589908 12647 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12648 return false;
12649
12650 return true;
12651}
12652
84b4e042
JB
12653static bool intel_crt_present(struct drm_device *dev)
12654{
12655 struct drm_i915_private *dev_priv = dev->dev_private;
12656
884497ed
DL
12657 if (INTEL_INFO(dev)->gen >= 9)
12658 return false;
12659
cf404ce4 12660 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
12661 return false;
12662
12663 if (IS_CHERRYVIEW(dev))
12664 return false;
12665
12666 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12667 return false;
12668
12669 return true;
12670}
12671
79e53945
JB
12672static void intel_setup_outputs(struct drm_device *dev)
12673{
725e30ad 12674 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12675 struct intel_encoder *encoder;
c6f95f27 12676 struct drm_connector *connector;
cb0953d7 12677 bool dpd_is_edp = false;
79e53945 12678
c9093354 12679 intel_lvds_init(dev);
79e53945 12680
84b4e042 12681 if (intel_crt_present(dev))
79935fca 12682 intel_crt_init(dev);
cb0953d7 12683
affa9354 12684 if (HAS_DDI(dev)) {
0e72a5b5
ED
12685 int found;
12686
de31facd
JB
12687 /*
12688 * Haswell uses DDI functions to detect digital outputs.
12689 * On SKL pre-D0 the strap isn't connected, so we assume
12690 * it's there.
12691 */
0e72a5b5 12692 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
12693 /* WaIgnoreDDIAStrap: skl */
12694 if (found ||
12695 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
12696 intel_ddi_init(dev, PORT_A);
12697
12698 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12699 * register */
12700 found = I915_READ(SFUSE_STRAP);
12701
12702 if (found & SFUSE_STRAP_DDIB_DETECTED)
12703 intel_ddi_init(dev, PORT_B);
12704 if (found & SFUSE_STRAP_DDIC_DETECTED)
12705 intel_ddi_init(dev, PORT_C);
12706 if (found & SFUSE_STRAP_DDID_DETECTED)
12707 intel_ddi_init(dev, PORT_D);
12708 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12709 int found;
5d8a7752 12710 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12711
12712 if (has_edp_a(dev))
12713 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12714
dc0fa718 12715 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12716 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12717 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12718 if (!found)
e2debe91 12719 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12720 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12721 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12722 }
12723
dc0fa718 12724 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12725 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12726
dc0fa718 12727 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12728 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12729
5eb08b69 12730 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12731 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12732
270b3042 12733 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12734 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12735 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
12736 /*
12737 * The DP_DETECTED bit is the latched state of the DDC
12738 * SDA pin at boot. However since eDP doesn't require DDC
12739 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12740 * eDP ports may have been muxed to an alternate function.
12741 * Thus we can't rely on the DP_DETECTED bit alone to detect
12742 * eDP ports. Consult the VBT as well as DP_DETECTED to
12743 * detect eDP ports.
12744 */
d2182a66
VS
12745 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12746 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
12747 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12748 PORT_B);
e17ac6db
VS
12749 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12750 intel_dp_is_edp(dev, PORT_B))
12751 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 12752
d2182a66
VS
12753 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12754 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
12755 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12756 PORT_C);
e17ac6db
VS
12757 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12758 intel_dp_is_edp(dev, PORT_C))
12759 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 12760
9418c1f1 12761 if (IS_CHERRYVIEW(dev)) {
e17ac6db 12762 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
12763 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12764 PORT_D);
e17ac6db
VS
12765 /* eDP not supported on port D, so don't check VBT */
12766 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12767 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
12768 }
12769
3cfca973 12770 intel_dsi_init(dev);
103a196f 12771 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12772 bool found = false;
7d57382e 12773
e2debe91 12774 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12775 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12776 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12777 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12778 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12779 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12780 }
27185ae1 12781
e7281eab 12782 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12783 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12784 }
13520b05
KH
12785
12786 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12787
e2debe91 12788 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12789 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12790 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12791 }
27185ae1 12792
e2debe91 12793 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12794
b01f2c3a
JB
12795 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12796 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12797 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12798 }
e7281eab 12799 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12800 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12801 }
27185ae1 12802
b01f2c3a 12803 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12804 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12805 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12806 } else if (IS_GEN2(dev))
79e53945
JB
12807 intel_dvo_init(dev);
12808
103a196f 12809 if (SUPPORTS_TV(dev))
79e53945
JB
12810 intel_tv_init(dev);
12811
c6f95f27
MR
12812 /*
12813 * FIXME: We don't have full atomic support yet, but we want to be
12814 * able to enable/test plane updates via the atomic interface in the
12815 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12816 * will take some atomic codepaths to lookup properties during
12817 * drmModeGetConnector() that unconditionally dereference
12818 * connector->state.
12819 *
12820 * We create a dummy connector state here for each connector to ensure
12821 * the DRM core doesn't try to dereference a NULL connector->state.
12822 * The actual connector properties will never be updated or contain
12823 * useful information, but since we're doing this specifically for
12824 * testing/debug of the plane operations (and only when a specific
12825 * kernel module option is given), that shouldn't really matter.
12826 *
12827 * Once atomic support for crtc's + connectors lands, this loop should
12828 * be removed since we'll be setting up real connector state, which
12829 * will contain Intel-specific properties.
12830 */
12831 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12832 list_for_each_entry(connector,
12833 &dev->mode_config.connector_list,
12834 head) {
12835 if (!WARN_ON(connector->state)) {
12836 connector->state =
12837 kzalloc(sizeof(*connector->state),
12838 GFP_KERNEL);
12839 }
12840 }
12841 }
12842
0bc12bcb 12843 intel_psr_init(dev);
7c8f8a70 12844
b2784e15 12845 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12846 encoder->base.possible_crtcs = encoder->crtc_mask;
12847 encoder->base.possible_clones =
66a9278e 12848 intel_encoder_clones(encoder);
79e53945 12849 }
47356eb6 12850
dde86e2d 12851 intel_init_pch_refclk(dev);
270b3042
DV
12852
12853 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12854}
12855
12856static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12857{
60a5ca01 12858 struct drm_device *dev = fb->dev;
79e53945 12859 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12860
ef2d633e 12861 drm_framebuffer_cleanup(fb);
60a5ca01 12862 mutex_lock(&dev->struct_mutex);
ef2d633e 12863 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12864 drm_gem_object_unreference(&intel_fb->obj->base);
12865 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12866 kfree(intel_fb);
12867}
12868
12869static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12870 struct drm_file *file,
79e53945
JB
12871 unsigned int *handle)
12872{
12873 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12874 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12875
05394f39 12876 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12877}
12878
12879static const struct drm_framebuffer_funcs intel_fb_funcs = {
12880 .destroy = intel_user_framebuffer_destroy,
12881 .create_handle = intel_user_framebuffer_create_handle,
12882};
12883
b321803d
DL
12884static
12885u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
12886 uint32_t pixel_format)
12887{
12888 u32 gen = INTEL_INFO(dev)->gen;
12889
12890 if (gen >= 9) {
12891 /* "The stride in bytes must not exceed the of the size of 8K
12892 * pixels and 32K bytes."
12893 */
12894 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
12895 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
12896 return 32*1024;
12897 } else if (gen >= 4) {
12898 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12899 return 16*1024;
12900 else
12901 return 32*1024;
12902 } else if (gen >= 3) {
12903 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12904 return 8*1024;
12905 else
12906 return 16*1024;
12907 } else {
12908 /* XXX DSPC is limited to 4k tiled */
12909 return 8*1024;
12910 }
12911}
12912
b5ea642a
DV
12913static int intel_framebuffer_init(struct drm_device *dev,
12914 struct intel_framebuffer *intel_fb,
12915 struct drm_mode_fb_cmd2 *mode_cmd,
12916 struct drm_i915_gem_object *obj)
79e53945 12917{
6761dd31 12918 unsigned int aligned_height;
79e53945 12919 int ret;
b321803d 12920 u32 pitch_limit, stride_alignment;
79e53945 12921
dd4916c5
DV
12922 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12923
2a80eada
DV
12924 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
12925 /* Enforce that fb modifier and tiling mode match, but only for
12926 * X-tiled. This is needed for FBC. */
12927 if (!!(obj->tiling_mode == I915_TILING_X) !=
12928 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
12929 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
12930 return -EINVAL;
12931 }
12932 } else {
12933 if (obj->tiling_mode == I915_TILING_X)
12934 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
12935 else if (obj->tiling_mode == I915_TILING_Y) {
12936 DRM_DEBUG("No Y tiling for legacy addfb\n");
12937 return -EINVAL;
12938 }
12939 }
12940
9a8f0a12
TU
12941 /* Passed in modifier sanity checking. */
12942 switch (mode_cmd->modifier[0]) {
12943 case I915_FORMAT_MOD_Y_TILED:
12944 case I915_FORMAT_MOD_Yf_TILED:
12945 if (INTEL_INFO(dev)->gen < 9) {
12946 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
12947 mode_cmd->modifier[0]);
12948 return -EINVAL;
12949 }
12950 case DRM_FORMAT_MOD_NONE:
12951 case I915_FORMAT_MOD_X_TILED:
12952 break;
12953 default:
12954 DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
12955 mode_cmd->modifier[0]);
57cd6508 12956 return -EINVAL;
c16ed4be 12957 }
57cd6508 12958
b321803d
DL
12959 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
12960 mode_cmd->pixel_format);
12961 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
12962 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
12963 mode_cmd->pitches[0], stride_alignment);
57cd6508 12964 return -EINVAL;
c16ed4be 12965 }
57cd6508 12966
b321803d
DL
12967 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
12968 mode_cmd->pixel_format);
a35cdaa0 12969 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
12970 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
12971 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 12972 "tiled" : "linear",
a35cdaa0 12973 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12974 return -EINVAL;
c16ed4be 12975 }
5d7bd705 12976
2a80eada 12977 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
12978 mode_cmd->pitches[0] != obj->stride) {
12979 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12980 mode_cmd->pitches[0], obj->stride);
5d7bd705 12981 return -EINVAL;
c16ed4be 12982 }
5d7bd705 12983
57779d06 12984 /* Reject formats not supported by any plane early. */
308e5bcb 12985 switch (mode_cmd->pixel_format) {
57779d06 12986 case DRM_FORMAT_C8:
04b3924d
VS
12987 case DRM_FORMAT_RGB565:
12988 case DRM_FORMAT_XRGB8888:
12989 case DRM_FORMAT_ARGB8888:
57779d06
VS
12990 break;
12991 case DRM_FORMAT_XRGB1555:
12992 case DRM_FORMAT_ARGB1555:
c16ed4be 12993 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12994 DRM_DEBUG("unsupported pixel format: %s\n",
12995 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12996 return -EINVAL;
c16ed4be 12997 }
57779d06
VS
12998 break;
12999 case DRM_FORMAT_XBGR8888:
13000 case DRM_FORMAT_ABGR8888:
04b3924d
VS
13001 case DRM_FORMAT_XRGB2101010:
13002 case DRM_FORMAT_ARGB2101010:
57779d06
VS
13003 case DRM_FORMAT_XBGR2101010:
13004 case DRM_FORMAT_ABGR2101010:
c16ed4be 13005 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
13006 DRM_DEBUG("unsupported pixel format: %s\n",
13007 drm_get_format_name(mode_cmd->pixel_format));
57779d06 13008 return -EINVAL;
c16ed4be 13009 }
b5626747 13010 break;
04b3924d
VS
13011 case DRM_FORMAT_YUYV:
13012 case DRM_FORMAT_UYVY:
13013 case DRM_FORMAT_YVYU:
13014 case DRM_FORMAT_VYUY:
c16ed4be 13015 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
13016 DRM_DEBUG("unsupported pixel format: %s\n",
13017 drm_get_format_name(mode_cmd->pixel_format));
57779d06 13018 return -EINVAL;
c16ed4be 13019 }
57cd6508
CW
13020 break;
13021 default:
4ee62c76
VS
13022 DRM_DEBUG("unsupported pixel format: %s\n",
13023 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
13024 return -EINVAL;
13025 }
13026
90f9a336
VS
13027 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13028 if (mode_cmd->offsets[0] != 0)
13029 return -EINVAL;
13030
ec2c981e 13031 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
13032 mode_cmd->pixel_format,
13033 mode_cmd->modifier[0]);
53155c0a
DV
13034 /* FIXME drm helper for size checks (especially planar formats)? */
13035 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13036 return -EINVAL;
13037
c7d73f6a
DV
13038 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13039 intel_fb->obj = obj;
80075d49 13040 intel_fb->obj->framebuffer_references++;
c7d73f6a 13041
79e53945
JB
13042 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13043 if (ret) {
13044 DRM_ERROR("framebuffer init failed %d\n", ret);
13045 return ret;
13046 }
13047
79e53945
JB
13048 return 0;
13049}
13050
79e53945
JB
13051static struct drm_framebuffer *
13052intel_user_framebuffer_create(struct drm_device *dev,
13053 struct drm_file *filp,
308e5bcb 13054 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 13055{
05394f39 13056 struct drm_i915_gem_object *obj;
79e53945 13057
308e5bcb
JB
13058 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13059 mode_cmd->handles[0]));
c8725226 13060 if (&obj->base == NULL)
cce13ff7 13061 return ERR_PTR(-ENOENT);
79e53945 13062
d2dff872 13063 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
13064}
13065
4520f53a 13066#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 13067static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
13068{
13069}
13070#endif
13071
79e53945 13072static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 13073 .fb_create = intel_user_framebuffer_create,
0632fef6 13074 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
13075 .atomic_check = intel_atomic_check,
13076 .atomic_commit = intel_atomic_commit,
79e53945
JB
13077};
13078
e70236a8
JB
13079/* Set up chip specific display functions */
13080static void intel_init_display(struct drm_device *dev)
13081{
13082 struct drm_i915_private *dev_priv = dev->dev_private;
13083
ee9300bb
DV
13084 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13085 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
13086 else if (IS_CHERRYVIEW(dev))
13087 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
13088 else if (IS_VALLEYVIEW(dev))
13089 dev_priv->display.find_dpll = vlv_find_best_dpll;
13090 else if (IS_PINEVIEW(dev))
13091 dev_priv->display.find_dpll = pnv_find_best_dpll;
13092 else
13093 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13094
bc8d7dff
DL
13095 if (INTEL_INFO(dev)->gen >= 9) {
13096 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
13097 dev_priv->display.get_initial_plane_config =
13098 skylake_get_initial_plane_config;
bc8d7dff
DL
13099 dev_priv->display.crtc_compute_clock =
13100 haswell_crtc_compute_clock;
13101 dev_priv->display.crtc_enable = haswell_crtc_enable;
13102 dev_priv->display.crtc_disable = haswell_crtc_disable;
13103 dev_priv->display.off = ironlake_crtc_off;
13104 dev_priv->display.update_primary_plane =
13105 skylake_update_primary_plane;
13106 } else if (HAS_DDI(dev)) {
0e8ffe1b 13107 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
13108 dev_priv->display.get_initial_plane_config =
13109 ironlake_get_initial_plane_config;
797d0259
ACO
13110 dev_priv->display.crtc_compute_clock =
13111 haswell_crtc_compute_clock;
4f771f10
PZ
13112 dev_priv->display.crtc_enable = haswell_crtc_enable;
13113 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 13114 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
13115 dev_priv->display.update_primary_plane =
13116 ironlake_update_primary_plane;
09b4ddf9 13117 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 13118 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
13119 dev_priv->display.get_initial_plane_config =
13120 ironlake_get_initial_plane_config;
3fb37703
ACO
13121 dev_priv->display.crtc_compute_clock =
13122 ironlake_crtc_compute_clock;
76e5a89c
DV
13123 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13124 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 13125 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
13126 dev_priv->display.update_primary_plane =
13127 ironlake_update_primary_plane;
89b667f8
JB
13128 } else if (IS_VALLEYVIEW(dev)) {
13129 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
13130 dev_priv->display.get_initial_plane_config =
13131 i9xx_get_initial_plane_config;
d6dfee7a 13132 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
13133 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13134 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13135 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
13136 dev_priv->display.update_primary_plane =
13137 i9xx_update_primary_plane;
f564048e 13138 } else {
0e8ffe1b 13139 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
13140 dev_priv->display.get_initial_plane_config =
13141 i9xx_get_initial_plane_config;
d6dfee7a 13142 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
13143 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13144 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 13145 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
13146 dev_priv->display.update_primary_plane =
13147 i9xx_update_primary_plane;
f564048e 13148 }
e70236a8 13149
e70236a8 13150 /* Returns the core display clock speed */
25eb05fc
JB
13151 if (IS_VALLEYVIEW(dev))
13152 dev_priv->display.get_display_clock_speed =
13153 valleyview_get_display_clock_speed;
13154 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
13155 dev_priv->display.get_display_clock_speed =
13156 i945_get_display_clock_speed;
13157 else if (IS_I915G(dev))
13158 dev_priv->display.get_display_clock_speed =
13159 i915_get_display_clock_speed;
257a7ffc 13160 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
13161 dev_priv->display.get_display_clock_speed =
13162 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
13163 else if (IS_PINEVIEW(dev))
13164 dev_priv->display.get_display_clock_speed =
13165 pnv_get_display_clock_speed;
e70236a8
JB
13166 else if (IS_I915GM(dev))
13167 dev_priv->display.get_display_clock_speed =
13168 i915gm_get_display_clock_speed;
13169 else if (IS_I865G(dev))
13170 dev_priv->display.get_display_clock_speed =
13171 i865_get_display_clock_speed;
f0f8a9ce 13172 else if (IS_I85X(dev))
e70236a8
JB
13173 dev_priv->display.get_display_clock_speed =
13174 i855_get_display_clock_speed;
13175 else /* 852, 830 */
13176 dev_priv->display.get_display_clock_speed =
13177 i830_get_display_clock_speed;
13178
7c10a2b5 13179 if (IS_GEN5(dev)) {
3bb11b53 13180 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
13181 } else if (IS_GEN6(dev)) {
13182 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
13183 } else if (IS_IVYBRIDGE(dev)) {
13184 /* FIXME: detect B0+ stepping and use auto training */
13185 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 13186 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 13187 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
13188 } else if (IS_VALLEYVIEW(dev)) {
13189 dev_priv->display.modeset_global_resources =
13190 valleyview_modeset_global_resources;
e70236a8 13191 }
8c9f3aaf 13192
8c9f3aaf
JB
13193 switch (INTEL_INFO(dev)->gen) {
13194 case 2:
13195 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13196 break;
13197
13198 case 3:
13199 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13200 break;
13201
13202 case 4:
13203 case 5:
13204 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13205 break;
13206
13207 case 6:
13208 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13209 break;
7c9017e5 13210 case 7:
4e0bbc31 13211 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
13212 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13213 break;
830c81db 13214 case 9:
ba343e02
TU
13215 /* Drop through - unsupported since execlist only. */
13216 default:
13217 /* Default just returns -ENODEV to indicate unsupported */
13218 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 13219 }
7bd688cd
JN
13220
13221 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
13222
13223 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
13224}
13225
b690e96c
JB
13226/*
13227 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13228 * resume, or other times. This quirk makes sure that's the case for
13229 * affected systems.
13230 */
0206e353 13231static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
13232{
13233 struct drm_i915_private *dev_priv = dev->dev_private;
13234
13235 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 13236 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
13237}
13238
b6b5d049
VS
13239static void quirk_pipeb_force(struct drm_device *dev)
13240{
13241 struct drm_i915_private *dev_priv = dev->dev_private;
13242
13243 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13244 DRM_INFO("applying pipe b force quirk\n");
13245}
13246
435793df
KP
13247/*
13248 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13249 */
13250static void quirk_ssc_force_disable(struct drm_device *dev)
13251{
13252 struct drm_i915_private *dev_priv = dev->dev_private;
13253 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 13254 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
13255}
13256
4dca20ef 13257/*
5a15ab5b
CE
13258 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13259 * brightness value
4dca20ef
CE
13260 */
13261static void quirk_invert_brightness(struct drm_device *dev)
13262{
13263 struct drm_i915_private *dev_priv = dev->dev_private;
13264 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 13265 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
13266}
13267
9c72cc6f
SD
13268/* Some VBT's incorrectly indicate no backlight is present */
13269static void quirk_backlight_present(struct drm_device *dev)
13270{
13271 struct drm_i915_private *dev_priv = dev->dev_private;
13272 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13273 DRM_INFO("applying backlight present quirk\n");
13274}
13275
b690e96c
JB
13276struct intel_quirk {
13277 int device;
13278 int subsystem_vendor;
13279 int subsystem_device;
13280 void (*hook)(struct drm_device *dev);
13281};
13282
5f85f176
EE
13283/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13284struct intel_dmi_quirk {
13285 void (*hook)(struct drm_device *dev);
13286 const struct dmi_system_id (*dmi_id_list)[];
13287};
13288
13289static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13290{
13291 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13292 return 1;
13293}
13294
13295static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13296 {
13297 .dmi_id_list = &(const struct dmi_system_id[]) {
13298 {
13299 .callback = intel_dmi_reverse_brightness,
13300 .ident = "NCR Corporation",
13301 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13302 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13303 },
13304 },
13305 { } /* terminating entry */
13306 },
13307 .hook = quirk_invert_brightness,
13308 },
13309};
13310
c43b5634 13311static struct intel_quirk intel_quirks[] = {
b690e96c 13312 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 13313 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 13314
b690e96c
JB
13315 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13316 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13317
b690e96c
JB
13318 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13319 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13320
5f080c0f
VS
13321 /* 830 needs to leave pipe A & dpll A up */
13322 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13323
b6b5d049
VS
13324 /* 830 needs to leave pipe B & dpll B up */
13325 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13326
435793df
KP
13327 /* Lenovo U160 cannot use SSC on LVDS */
13328 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
13329
13330 /* Sony Vaio Y cannot use SSC on LVDS */
13331 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 13332
be505f64
AH
13333 /* Acer Aspire 5734Z must invert backlight brightness */
13334 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13335
13336 /* Acer/eMachines G725 */
13337 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13338
13339 /* Acer/eMachines e725 */
13340 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13341
13342 /* Acer/Packard Bell NCL20 */
13343 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13344
13345 /* Acer Aspire 4736Z */
13346 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
13347
13348 /* Acer Aspire 5336 */
13349 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
13350
13351 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13352 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 13353
dfb3d47b
SD
13354 /* Acer C720 Chromebook (Core i3 4005U) */
13355 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13356
b2a9601c 13357 /* Apple Macbook 2,1 (Core 2 T7400) */
13358 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13359
d4967d8c
SD
13360 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13361 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
13362
13363 /* HP Chromebook 14 (Celeron 2955U) */
13364 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
13365
13366 /* Dell Chromebook 11 */
13367 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
13368};
13369
13370static void intel_init_quirks(struct drm_device *dev)
13371{
13372 struct pci_dev *d = dev->pdev;
13373 int i;
13374
13375 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13376 struct intel_quirk *q = &intel_quirks[i];
13377
13378 if (d->device == q->device &&
13379 (d->subsystem_vendor == q->subsystem_vendor ||
13380 q->subsystem_vendor == PCI_ANY_ID) &&
13381 (d->subsystem_device == q->subsystem_device ||
13382 q->subsystem_device == PCI_ANY_ID))
13383 q->hook(dev);
13384 }
5f85f176
EE
13385 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13386 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13387 intel_dmi_quirks[i].hook(dev);
13388 }
b690e96c
JB
13389}
13390
9cce37f4
JB
13391/* Disable the VGA plane that we never use */
13392static void i915_disable_vga(struct drm_device *dev)
13393{
13394 struct drm_i915_private *dev_priv = dev->dev_private;
13395 u8 sr1;
766aa1c4 13396 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 13397
2b37c616 13398 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 13399 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 13400 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
13401 sr1 = inb(VGA_SR_DATA);
13402 outb(sr1 | 1<<5, VGA_SR_DATA);
13403 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13404 udelay(300);
13405
01f5a626 13406 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
13407 POSTING_READ(vga_reg);
13408}
13409
f817586c
DV
13410void intel_modeset_init_hw(struct drm_device *dev)
13411{
a8f78b58
ED
13412 intel_prepare_ddi(dev);
13413
f8bf63fd
VS
13414 if (IS_VALLEYVIEW(dev))
13415 vlv_update_cdclk(dev);
13416
f817586c
DV
13417 intel_init_clock_gating(dev);
13418
8090c6b9 13419 intel_enable_gt_powersave(dev);
f817586c
DV
13420}
13421
79e53945
JB
13422void intel_modeset_init(struct drm_device *dev)
13423{
652c393a 13424 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 13425 int sprite, ret;
8cc87b75 13426 enum pipe pipe;
46f297fb 13427 struct intel_crtc *crtc;
79e53945
JB
13428
13429 drm_mode_config_init(dev);
13430
13431 dev->mode_config.min_width = 0;
13432 dev->mode_config.min_height = 0;
13433
019d96cb
DA
13434 dev->mode_config.preferred_depth = 24;
13435 dev->mode_config.prefer_shadow = 1;
13436
25bab385
TU
13437 dev->mode_config.allow_fb_modifiers = true;
13438
e6ecefaa 13439 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 13440
b690e96c
JB
13441 intel_init_quirks(dev);
13442
1fa61106
ED
13443 intel_init_pm(dev);
13444
e3c74757
BW
13445 if (INTEL_INFO(dev)->num_pipes == 0)
13446 return;
13447
e70236a8 13448 intel_init_display(dev);
7c10a2b5 13449 intel_init_audio(dev);
e70236a8 13450
a6c45cf0
CW
13451 if (IS_GEN2(dev)) {
13452 dev->mode_config.max_width = 2048;
13453 dev->mode_config.max_height = 2048;
13454 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
13455 dev->mode_config.max_width = 4096;
13456 dev->mode_config.max_height = 4096;
79e53945 13457 } else {
a6c45cf0
CW
13458 dev->mode_config.max_width = 8192;
13459 dev->mode_config.max_height = 8192;
79e53945 13460 }
068be561 13461
dc41c154
VS
13462 if (IS_845G(dev) || IS_I865G(dev)) {
13463 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13464 dev->mode_config.cursor_height = 1023;
13465 } else if (IS_GEN2(dev)) {
068be561
DL
13466 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13467 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13468 } else {
13469 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13470 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13471 }
13472
5d4545ae 13473 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 13474
28c97730 13475 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
13476 INTEL_INFO(dev)->num_pipes,
13477 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 13478
055e393f 13479 for_each_pipe(dev_priv, pipe) {
8cc87b75 13480 intel_crtc_init(dev, pipe);
3bdcfc0c 13481 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 13482 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 13483 if (ret)
06da8da2 13484 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 13485 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 13486 }
79e53945
JB
13487 }
13488
f42bb70d
JB
13489 intel_init_dpio(dev);
13490
e72f9fbf 13491 intel_shared_dpll_init(dev);
ee7b9f93 13492
9cce37f4
JB
13493 /* Just disable it once at startup */
13494 i915_disable_vga(dev);
79e53945 13495 intel_setup_outputs(dev);
11be49eb
CW
13496
13497 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 13498 intel_fbc_disable(dev);
fa9fa083 13499
6e9f798d 13500 drm_modeset_lock_all(dev);
fa9fa083 13501 intel_modeset_setup_hw_state(dev, false);
6e9f798d 13502 drm_modeset_unlock_all(dev);
46f297fb 13503
d3fcc808 13504 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
13505 if (!crtc->active)
13506 continue;
13507
46f297fb 13508 /*
46f297fb
JB
13509 * Note that reserving the BIOS fb up front prevents us
13510 * from stuffing other stolen allocations like the ring
13511 * on top. This prevents some ugliness at boot time, and
13512 * can even allow for smooth boot transitions if the BIOS
13513 * fb is large enough for the active pipe configuration.
13514 */
5724dbd1
DL
13515 if (dev_priv->display.get_initial_plane_config) {
13516 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
13517 &crtc->plane_config);
13518 /*
13519 * If the fb is shared between multiple heads, we'll
13520 * just get the first one.
13521 */
484b41dd 13522 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 13523 }
46f297fb 13524 }
2c7111db
CW
13525}
13526
7fad798e
DV
13527static void intel_enable_pipe_a(struct drm_device *dev)
13528{
13529 struct intel_connector *connector;
13530 struct drm_connector *crt = NULL;
13531 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 13532 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
13533
13534 /* We can't just switch on the pipe A, we need to set things up with a
13535 * proper mode and output configuration. As a gross hack, enable pipe A
13536 * by enabling the load detect pipe once. */
3a3371ff 13537 for_each_intel_connector(dev, connector) {
7fad798e
DV
13538 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13539 crt = &connector->base;
13540 break;
13541 }
13542 }
13543
13544 if (!crt)
13545 return;
13546
208bf9fd
VS
13547 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13548 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
13549}
13550
fa555837
DV
13551static bool
13552intel_check_plane_mapping(struct intel_crtc *crtc)
13553{
7eb552ae
BW
13554 struct drm_device *dev = crtc->base.dev;
13555 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
13556 u32 reg, val;
13557
7eb552ae 13558 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13559 return true;
13560
13561 reg = DSPCNTR(!crtc->plane);
13562 val = I915_READ(reg);
13563
13564 if ((val & DISPLAY_PLANE_ENABLE) &&
13565 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13566 return false;
13567
13568 return true;
13569}
13570
24929352
DV
13571static void intel_sanitize_crtc(struct intel_crtc *crtc)
13572{
13573 struct drm_device *dev = crtc->base.dev;
13574 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13575 u32 reg;
24929352 13576
24929352 13577 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 13578 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
13579 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13580
d3eaf884 13581 /* restore vblank interrupts to correct state */
9625604c 13582 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
13583 if (crtc->active) {
13584 update_scanline_offset(crtc);
9625604c
DV
13585 drm_crtc_vblank_on(&crtc->base);
13586 }
d3eaf884 13587
24929352 13588 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13589 * disable the crtc (and hence change the state) if it is wrong. Note
13590 * that gen4+ has a fixed plane -> pipe mapping. */
13591 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13592 struct intel_connector *connector;
13593 bool plane;
13594
24929352
DV
13595 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13596 crtc->base.base.id);
13597
13598 /* Pipe has the wrong plane attached and the plane is active.
13599 * Temporarily change the plane mapping and disable everything
13600 * ... */
13601 plane = crtc->plane;
13602 crtc->plane = !plane;
9c8958bc 13603 crtc->primary_enabled = true;
24929352
DV
13604 dev_priv->display.crtc_disable(&crtc->base);
13605 crtc->plane = plane;
13606
13607 /* ... and break all links. */
3a3371ff 13608 for_each_intel_connector(dev, connector) {
24929352
DV
13609 if (connector->encoder->base.crtc != &crtc->base)
13610 continue;
13611
7f1950fb
EE
13612 connector->base.dpms = DRM_MODE_DPMS_OFF;
13613 connector->base.encoder = NULL;
24929352 13614 }
7f1950fb
EE
13615 /* multiple connectors may have the same encoder:
13616 * handle them and break crtc link separately */
3a3371ff 13617 for_each_intel_connector(dev, connector)
7f1950fb
EE
13618 if (connector->encoder->base.crtc == &crtc->base) {
13619 connector->encoder->base.crtc = NULL;
13620 connector->encoder->connectors_active = false;
13621 }
24929352
DV
13622
13623 WARN_ON(crtc->active);
83d65738 13624 crtc->base.state->enable = false;
24929352
DV
13625 crtc->base.enabled = false;
13626 }
24929352 13627
7fad798e
DV
13628 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13629 crtc->pipe == PIPE_A && !crtc->active) {
13630 /* BIOS forgot to enable pipe A, this mostly happens after
13631 * resume. Force-enable the pipe to fix this, the update_dpms
13632 * call below we restore the pipe to the right state, but leave
13633 * the required bits on. */
13634 intel_enable_pipe_a(dev);
13635 }
13636
24929352
DV
13637 /* Adjust the state of the output pipe according to whether we
13638 * have active connectors/encoders. */
13639 intel_crtc_update_dpms(&crtc->base);
13640
83d65738 13641 if (crtc->active != crtc->base.state->enable) {
24929352
DV
13642 struct intel_encoder *encoder;
13643
13644 /* This can happen either due to bugs in the get_hw_state
13645 * functions or because the pipe is force-enabled due to the
13646 * pipe A quirk. */
13647 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13648 crtc->base.base.id,
83d65738 13649 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
13650 crtc->active ? "enabled" : "disabled");
13651
83d65738 13652 crtc->base.state->enable = crtc->active;
24929352
DV
13653 crtc->base.enabled = crtc->active;
13654
13655 /* Because we only establish the connector -> encoder ->
13656 * crtc links if something is active, this means the
13657 * crtc is now deactivated. Break the links. connector
13658 * -> encoder links are only establish when things are
13659 * actually up, hence no need to break them. */
13660 WARN_ON(crtc->active);
13661
13662 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13663 WARN_ON(encoder->connectors_active);
13664 encoder->base.crtc = NULL;
13665 }
13666 }
c5ab3bc0 13667
a3ed6aad 13668 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13669 /*
13670 * We start out with underrun reporting disabled to avoid races.
13671 * For correct bookkeeping mark this on active crtcs.
13672 *
c5ab3bc0
DV
13673 * Also on gmch platforms we dont have any hardware bits to
13674 * disable the underrun reporting. Which means we need to start
13675 * out with underrun reporting disabled also on inactive pipes,
13676 * since otherwise we'll complain about the garbage we read when
13677 * e.g. coming up after runtime pm.
13678 *
4cc31489
DV
13679 * No protection against concurrent access is required - at
13680 * worst a fifo underrun happens which also sets this to false.
13681 */
13682 crtc->cpu_fifo_underrun_disabled = true;
13683 crtc->pch_fifo_underrun_disabled = true;
13684 }
24929352
DV
13685}
13686
13687static void intel_sanitize_encoder(struct intel_encoder *encoder)
13688{
13689 struct intel_connector *connector;
13690 struct drm_device *dev = encoder->base.dev;
13691
13692 /* We need to check both for a crtc link (meaning that the
13693 * encoder is active and trying to read from a pipe) and the
13694 * pipe itself being active. */
13695 bool has_active_crtc = encoder->base.crtc &&
13696 to_intel_crtc(encoder->base.crtc)->active;
13697
13698 if (encoder->connectors_active && !has_active_crtc) {
13699 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13700 encoder->base.base.id,
8e329a03 13701 encoder->base.name);
24929352
DV
13702
13703 /* Connector is active, but has no active pipe. This is
13704 * fallout from our resume register restoring. Disable
13705 * the encoder manually again. */
13706 if (encoder->base.crtc) {
13707 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13708 encoder->base.base.id,
8e329a03 13709 encoder->base.name);
24929352 13710 encoder->disable(encoder);
a62d1497
VS
13711 if (encoder->post_disable)
13712 encoder->post_disable(encoder);
24929352 13713 }
7f1950fb
EE
13714 encoder->base.crtc = NULL;
13715 encoder->connectors_active = false;
24929352
DV
13716
13717 /* Inconsistent output/port/pipe state happens presumably due to
13718 * a bug in one of the get_hw_state functions. Or someplace else
13719 * in our code, like the register restore mess on resume. Clamp
13720 * things to off as a safer default. */
3a3371ff 13721 for_each_intel_connector(dev, connector) {
24929352
DV
13722 if (connector->encoder != encoder)
13723 continue;
7f1950fb
EE
13724 connector->base.dpms = DRM_MODE_DPMS_OFF;
13725 connector->base.encoder = NULL;
24929352
DV
13726 }
13727 }
13728 /* Enabled encoders without active connectors will be fixed in
13729 * the crtc fixup. */
13730}
13731
04098753 13732void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13733{
13734 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13735 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13736
04098753
ID
13737 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13738 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13739 i915_disable_vga(dev);
13740 }
13741}
13742
13743void i915_redisable_vga(struct drm_device *dev)
13744{
13745 struct drm_i915_private *dev_priv = dev->dev_private;
13746
8dc8a27c
PZ
13747 /* This function can be called both from intel_modeset_setup_hw_state or
13748 * at a very early point in our resume sequence, where the power well
13749 * structures are not yet restored. Since this function is at a very
13750 * paranoid "someone might have enabled VGA while we were not looking"
13751 * level, just check if the power well is enabled instead of trying to
13752 * follow the "don't touch the power well if we don't need it" policy
13753 * the rest of the driver uses. */
f458ebbc 13754 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13755 return;
13756
04098753 13757 i915_redisable_vga_power_on(dev);
0fde901f
KM
13758}
13759
98ec7739
VS
13760static bool primary_get_hw_state(struct intel_crtc *crtc)
13761{
13762 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13763
13764 if (!crtc->active)
13765 return false;
13766
13767 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13768}
13769
30e984df 13770static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13771{
13772 struct drm_i915_private *dev_priv = dev->dev_private;
13773 enum pipe pipe;
24929352
DV
13774 struct intel_crtc *crtc;
13775 struct intel_encoder *encoder;
13776 struct intel_connector *connector;
5358901f 13777 int i;
24929352 13778
d3fcc808 13779 for_each_intel_crtc(dev, crtc) {
6e3c9717 13780 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 13781
6e3c9717 13782 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 13783
0e8ffe1b 13784 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 13785 crtc->config);
24929352 13786
83d65738 13787 crtc->base.state->enable = crtc->active;
24929352 13788 crtc->base.enabled = crtc->active;
98ec7739 13789 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13790
13791 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13792 crtc->base.base.id,
13793 crtc->active ? "enabled" : "disabled");
13794 }
13795
5358901f
DV
13796 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13797 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13798
3e369b76
ACO
13799 pll->on = pll->get_hw_state(dev_priv, pll,
13800 &pll->config.hw_state);
5358901f 13801 pll->active = 0;
3e369b76 13802 pll->config.crtc_mask = 0;
d3fcc808 13803 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 13804 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 13805 pll->active++;
3e369b76 13806 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 13807 }
5358901f 13808 }
5358901f 13809
1e6f2ddc 13810 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 13811 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 13812
3e369b76 13813 if (pll->config.crtc_mask)
bd2bb1b9 13814 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13815 }
13816
b2784e15 13817 for_each_intel_encoder(dev, encoder) {
24929352
DV
13818 pipe = 0;
13819
13820 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13821 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13822 encoder->base.crtc = &crtc->base;
6e3c9717 13823 encoder->get_config(encoder, crtc->config);
24929352
DV
13824 } else {
13825 encoder->base.crtc = NULL;
13826 }
13827
13828 encoder->connectors_active = false;
6f2bcceb 13829 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13830 encoder->base.base.id,
8e329a03 13831 encoder->base.name,
24929352 13832 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13833 pipe_name(pipe));
24929352
DV
13834 }
13835
3a3371ff 13836 for_each_intel_connector(dev, connector) {
24929352
DV
13837 if (connector->get_hw_state(connector)) {
13838 connector->base.dpms = DRM_MODE_DPMS_ON;
13839 connector->encoder->connectors_active = true;
13840 connector->base.encoder = &connector->encoder->base;
13841 } else {
13842 connector->base.dpms = DRM_MODE_DPMS_OFF;
13843 connector->base.encoder = NULL;
13844 }
13845 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13846 connector->base.base.id,
c23cc417 13847 connector->base.name,
24929352
DV
13848 connector->base.encoder ? "enabled" : "disabled");
13849 }
30e984df
DV
13850}
13851
13852/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13853 * and i915 state tracking structures. */
13854void intel_modeset_setup_hw_state(struct drm_device *dev,
13855 bool force_restore)
13856{
13857 struct drm_i915_private *dev_priv = dev->dev_private;
13858 enum pipe pipe;
30e984df
DV
13859 struct intel_crtc *crtc;
13860 struct intel_encoder *encoder;
35c95375 13861 int i;
30e984df
DV
13862
13863 intel_modeset_readout_hw_state(dev);
24929352 13864
babea61d
JB
13865 /*
13866 * Now that we have the config, copy it to each CRTC struct
13867 * Note that this could go away if we move to using crtc_config
13868 * checking everywhere.
13869 */
d3fcc808 13870 for_each_intel_crtc(dev, crtc) {
d330a953 13871 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
13872 intel_mode_from_pipe_config(&crtc->base.mode,
13873 crtc->config);
babea61d
JB
13874 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13875 crtc->base.base.id);
13876 drm_mode_debug_printmodeline(&crtc->base.mode);
13877 }
13878 }
13879
24929352 13880 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13881 for_each_intel_encoder(dev, encoder) {
24929352
DV
13882 intel_sanitize_encoder(encoder);
13883 }
13884
055e393f 13885 for_each_pipe(dev_priv, pipe) {
24929352
DV
13886 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13887 intel_sanitize_crtc(crtc);
6e3c9717
ACO
13888 intel_dump_pipe_config(crtc, crtc->config,
13889 "[setup_hw_state]");
24929352 13890 }
9a935856 13891
35c95375
DV
13892 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13893 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13894
13895 if (!pll->on || pll->active)
13896 continue;
13897
13898 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13899
13900 pll->disable(dev_priv, pll);
13901 pll->on = false;
13902 }
13903
3078999f
PB
13904 if (IS_GEN9(dev))
13905 skl_wm_get_hw_state(dev);
13906 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13907 ilk_wm_get_hw_state(dev);
13908
45e2b5f6 13909 if (force_restore) {
7d0bc1ea
VS
13910 i915_redisable_vga(dev);
13911
f30da187
DV
13912 /*
13913 * We need to use raw interfaces for restoring state to avoid
13914 * checking (bogus) intermediate states.
13915 */
055e393f 13916 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13917 struct drm_crtc *crtc =
13918 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 13919
7f27126e
JB
13920 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13921 crtc->primary->fb);
45e2b5f6
DV
13922 }
13923 } else {
13924 intel_modeset_update_staged_output_state(dev);
13925 }
8af6cf88
DV
13926
13927 intel_modeset_check_state(dev);
2c7111db
CW
13928}
13929
13930void intel_modeset_gem_init(struct drm_device *dev)
13931{
92122789 13932 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 13933 struct drm_crtc *c;
2ff8fde1 13934 struct drm_i915_gem_object *obj;
484b41dd 13935
ae48434c
ID
13936 mutex_lock(&dev->struct_mutex);
13937 intel_init_gt_powersave(dev);
13938 mutex_unlock(&dev->struct_mutex);
13939
92122789
JB
13940 /*
13941 * There may be no VBT; and if the BIOS enabled SSC we can
13942 * just keep using it to avoid unnecessary flicker. Whereas if the
13943 * BIOS isn't using it, don't assume it will work even if the VBT
13944 * indicates as much.
13945 */
13946 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13947 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13948 DREF_SSC1_ENABLE);
13949
1833b134 13950 intel_modeset_init_hw(dev);
02e792fb
DV
13951
13952 intel_setup_overlay(dev);
484b41dd
JB
13953
13954 /*
13955 * Make sure any fbs we allocated at startup are properly
13956 * pinned & fenced. When we do the allocation it's too early
13957 * for this.
13958 */
13959 mutex_lock(&dev->struct_mutex);
70e1e0ec 13960 for_each_crtc(dev, c) {
2ff8fde1
MR
13961 obj = intel_fb_obj(c->primary->fb);
13962 if (obj == NULL)
484b41dd
JB
13963 continue;
13964
850c4cdc
TU
13965 if (intel_pin_and_fence_fb_obj(c->primary,
13966 c->primary->fb,
82bc3b2d 13967 c->primary->state,
850c4cdc 13968 NULL)) {
484b41dd
JB
13969 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13970 to_intel_crtc(c)->pipe);
66e514c1
DA
13971 drm_framebuffer_unreference(c->primary->fb);
13972 c->primary->fb = NULL;
afd65eb4 13973 update_state_fb(c->primary);
484b41dd
JB
13974 }
13975 }
13976 mutex_unlock(&dev->struct_mutex);
0962c3c9
VS
13977
13978 intel_backlight_register(dev);
79e53945
JB
13979}
13980
4932e2c3
ID
13981void intel_connector_unregister(struct intel_connector *intel_connector)
13982{
13983 struct drm_connector *connector = &intel_connector->base;
13984
13985 intel_panel_destroy_backlight(connector);
34ea3d38 13986 drm_connector_unregister(connector);
4932e2c3
ID
13987}
13988
79e53945
JB
13989void intel_modeset_cleanup(struct drm_device *dev)
13990{
652c393a 13991 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13992 struct drm_connector *connector;
652c393a 13993
2eb5252e
ID
13994 intel_disable_gt_powersave(dev);
13995
0962c3c9
VS
13996 intel_backlight_unregister(dev);
13997
fd0c0642
DV
13998 /*
13999 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 14000 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
14001 * experience fancy races otherwise.
14002 */
2aeb7d3a 14003 intel_irq_uninstall(dev_priv);
eb21b92b 14004
fd0c0642
DV
14005 /*
14006 * Due to the hpd irq storm handling the hotplug work can re-arm the
14007 * poll handlers. Hence disable polling after hpd handling is shut down.
14008 */
f87ea761 14009 drm_kms_helper_poll_fini(dev);
fd0c0642 14010
652c393a
JB
14011 mutex_lock(&dev->struct_mutex);
14012
723bfd70
JB
14013 intel_unregister_dsm_handler();
14014
7ff0ebcc 14015 intel_fbc_disable(dev);
e70236a8 14016
69341a5e
KH
14017 mutex_unlock(&dev->struct_mutex);
14018
1630fe75
CW
14019 /* flush any delayed tasks or pending work */
14020 flush_scheduled_work();
14021
db31af1d
JN
14022 /* destroy the backlight and sysfs files before encoders/connectors */
14023 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
14024 struct intel_connector *intel_connector;
14025
14026 intel_connector = to_intel_connector(connector);
14027 intel_connector->unregister(intel_connector);
db31af1d 14028 }
d9255d57 14029
79e53945 14030 drm_mode_config_cleanup(dev);
4d7bb011
DV
14031
14032 intel_cleanup_overlay(dev);
ae48434c
ID
14033
14034 mutex_lock(&dev->struct_mutex);
14035 intel_cleanup_gt_powersave(dev);
14036 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14037}
14038
f1c79df3
ZW
14039/*
14040 * Return which encoder is currently attached for connector.
14041 */
df0e9248 14042struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 14043{
df0e9248
CW
14044 return &intel_attached_encoder(connector)->base;
14045}
f1c79df3 14046
df0e9248
CW
14047void intel_connector_attach_encoder(struct intel_connector *connector,
14048 struct intel_encoder *encoder)
14049{
14050 connector->encoder = encoder;
14051 drm_mode_connector_attach_encoder(&connector->base,
14052 &encoder->base);
79e53945 14053}
28d52043
DA
14054
14055/*
14056 * set vga decode state - true == enable VGA decode
14057 */
14058int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14059{
14060 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 14061 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
14062 u16 gmch_ctrl;
14063
75fa041d
CW
14064 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14065 DRM_ERROR("failed to read control word\n");
14066 return -EIO;
14067 }
14068
c0cc8a55
CW
14069 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14070 return 0;
14071
28d52043
DA
14072 if (state)
14073 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14074 else
14075 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
14076
14077 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14078 DRM_ERROR("failed to write control word\n");
14079 return -EIO;
14080 }
14081
28d52043
DA
14082 return 0;
14083}
c4a1d9e4 14084
c4a1d9e4 14085struct intel_display_error_state {
ff57f1b0
PZ
14086
14087 u32 power_well_driver;
14088
63b66e5b
CW
14089 int num_transcoders;
14090
c4a1d9e4
CW
14091 struct intel_cursor_error_state {
14092 u32 control;
14093 u32 position;
14094 u32 base;
14095 u32 size;
52331309 14096 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
14097
14098 struct intel_pipe_error_state {
ddf9c536 14099 bool power_domain_on;
c4a1d9e4 14100 u32 source;
f301b1e1 14101 u32 stat;
52331309 14102 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
14103
14104 struct intel_plane_error_state {
14105 u32 control;
14106 u32 stride;
14107 u32 size;
14108 u32 pos;
14109 u32 addr;
14110 u32 surface;
14111 u32 tile_offset;
52331309 14112 } plane[I915_MAX_PIPES];
63b66e5b
CW
14113
14114 struct intel_transcoder_error_state {
ddf9c536 14115 bool power_domain_on;
63b66e5b
CW
14116 enum transcoder cpu_transcoder;
14117
14118 u32 conf;
14119
14120 u32 htotal;
14121 u32 hblank;
14122 u32 hsync;
14123 u32 vtotal;
14124 u32 vblank;
14125 u32 vsync;
14126 } transcoder[4];
c4a1d9e4
CW
14127};
14128
14129struct intel_display_error_state *
14130intel_display_capture_error_state(struct drm_device *dev)
14131{
fbee40df 14132 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 14133 struct intel_display_error_state *error;
63b66e5b
CW
14134 int transcoders[] = {
14135 TRANSCODER_A,
14136 TRANSCODER_B,
14137 TRANSCODER_C,
14138 TRANSCODER_EDP,
14139 };
c4a1d9e4
CW
14140 int i;
14141
63b66e5b
CW
14142 if (INTEL_INFO(dev)->num_pipes == 0)
14143 return NULL;
14144
9d1cb914 14145 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
14146 if (error == NULL)
14147 return NULL;
14148
190be112 14149 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
14150 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14151
055e393f 14152 for_each_pipe(dev_priv, i) {
ddf9c536 14153 error->pipe[i].power_domain_on =
f458ebbc
DV
14154 __intel_display_power_is_enabled(dev_priv,
14155 POWER_DOMAIN_PIPE(i));
ddf9c536 14156 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
14157 continue;
14158
5efb3e28
VS
14159 error->cursor[i].control = I915_READ(CURCNTR(i));
14160 error->cursor[i].position = I915_READ(CURPOS(i));
14161 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
14162
14163 error->plane[i].control = I915_READ(DSPCNTR(i));
14164 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 14165 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 14166 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
14167 error->plane[i].pos = I915_READ(DSPPOS(i));
14168 }
ca291363
PZ
14169 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14170 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
14171 if (INTEL_INFO(dev)->gen >= 4) {
14172 error->plane[i].surface = I915_READ(DSPSURF(i));
14173 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14174 }
14175
c4a1d9e4 14176 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 14177
3abfce77 14178 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 14179 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
14180 }
14181
14182 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14183 if (HAS_DDI(dev_priv->dev))
14184 error->num_transcoders++; /* Account for eDP. */
14185
14186 for (i = 0; i < error->num_transcoders; i++) {
14187 enum transcoder cpu_transcoder = transcoders[i];
14188
ddf9c536 14189 error->transcoder[i].power_domain_on =
f458ebbc 14190 __intel_display_power_is_enabled(dev_priv,
38cc1daf 14191 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 14192 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
14193 continue;
14194
63b66e5b
CW
14195 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14196
14197 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14198 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14199 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14200 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14201 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14202 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14203 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
14204 }
14205
14206 return error;
14207}
14208
edc3d884
MK
14209#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14210
c4a1d9e4 14211void
edc3d884 14212intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
14213 struct drm_device *dev,
14214 struct intel_display_error_state *error)
14215{
055e393f 14216 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
14217 int i;
14218
63b66e5b
CW
14219 if (!error)
14220 return;
14221
edc3d884 14222 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 14223 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 14224 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 14225 error->power_well_driver);
055e393f 14226 for_each_pipe(dev_priv, i) {
edc3d884 14227 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
14228 err_printf(m, " Power: %s\n",
14229 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 14230 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 14231 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
14232
14233 err_printf(m, "Plane [%d]:\n", i);
14234 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14235 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 14236 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
14237 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14238 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 14239 }
4b71a570 14240 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 14241 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 14242 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
14243 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14244 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
14245 }
14246
edc3d884
MK
14247 err_printf(m, "Cursor [%d]:\n", i);
14248 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14249 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14250 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 14251 }
63b66e5b
CW
14252
14253 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 14254 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 14255 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
14256 err_printf(m, " Power: %s\n",
14257 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
14258 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14259 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14260 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14261 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14262 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14263 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14264 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14265 }
c4a1d9e4 14266}
e2fcdaa9
VS
14267
14268void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14269{
14270 struct intel_crtc *crtc;
14271
14272 for_each_intel_crtc(dev, crtc) {
14273 struct intel_unpin_work *work;
e2fcdaa9 14274
5e2d7afc 14275 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
14276
14277 work = crtc->unpin_work;
14278
14279 if (work && work->event &&
14280 work->event->base.file_priv == file) {
14281 kfree(work->event);
14282 work->event = NULL;
14283 }
14284
5e2d7afc 14285 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
14286 }
14287}
This page took 2.719227 seconds and 5 git commands to generate.