]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Remove implicitly disabling primary plane for now
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
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 85static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
83a57153
ACO
86 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
eb1bfe80
JB
88static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
5b18e57c
DV
92static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 94static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
95 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
29407aab 97static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
98static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 100static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 101 const struct intel_crtc_state *pipe_config);
d288f65f 102static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
106static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
107 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
108static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
109 int num_connectors);
e7457a9a 110
0e32b39c
DA
111static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
112{
113 if (!connector->mst_port)
114 return connector->encoder;
115 else
116 return &connector->mst_port->mst_encoders[pipe]->base;
117}
118
79e53945 119typedef struct {
0206e353 120 int min, max;
79e53945
JB
121} intel_range_t;
122
123typedef struct {
0206e353
AJ
124 int dot_limit;
125 int p2_slow, p2_fast;
79e53945
JB
126} intel_p2_t;
127
d4906093
ML
128typedef struct intel_limit intel_limit_t;
129struct intel_limit {
0206e353
AJ
130 intel_range_t dot, vco, n, m, m1, m2, p, p1;
131 intel_p2_t p2;
d4906093 132};
79e53945 133
d2acd215
DV
134int
135intel_pch_rawclk(struct drm_device *dev)
136{
137 struct drm_i915_private *dev_priv = dev->dev_private;
138
139 WARN_ON(!HAS_PCH_SPLIT(dev));
140
141 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
142}
143
021357ac
CW
144static inline u32 /* units of 100MHz */
145intel_fdi_link_freq(struct drm_device *dev)
146{
8b99e68c
CW
147 if (IS_GEN5(dev)) {
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
150 } else
151 return 27;
021357ac
CW
152}
153
5d536e28 154static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 155 .dot = { .min = 25000, .max = 350000 },
9c333719 156 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 157 .n = { .min = 2, .max = 16 },
0206e353
AJ
158 .m = { .min = 96, .max = 140 },
159 .m1 = { .min = 18, .max = 26 },
160 .m2 = { .min = 6, .max = 16 },
161 .p = { .min = 4, .max = 128 },
162 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
163 .p2 = { .dot_limit = 165000,
164 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
165};
166
5d536e28
DV
167static const intel_limit_t intel_limits_i8xx_dvo = {
168 .dot = { .min = 25000, .max = 350000 },
9c333719 169 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 170 .n = { .min = 2, .max = 16 },
5d536e28
DV
171 .m = { .min = 96, .max = 140 },
172 .m1 = { .min = 18, .max = 26 },
173 .m2 = { .min = 6, .max = 16 },
174 .p = { .min = 4, .max = 128 },
175 .p1 = { .min = 2, .max = 33 },
176 .p2 = { .dot_limit = 165000,
177 .p2_slow = 4, .p2_fast = 4 },
178};
179
e4b36699 180static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 181 .dot = { .min = 25000, .max = 350000 },
9c333719 182 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 183 .n = { .min = 2, .max = 16 },
0206e353
AJ
184 .m = { .min = 96, .max = 140 },
185 .m1 = { .min = 18, .max = 26 },
186 .m2 = { .min = 6, .max = 16 },
187 .p = { .min = 4, .max = 128 },
188 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
189 .p2 = { .dot_limit = 165000,
190 .p2_slow = 14, .p2_fast = 7 },
e4b36699 191};
273e27ca 192
e4b36699 193static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
194 .dot = { .min = 20000, .max = 400000 },
195 .vco = { .min = 1400000, .max = 2800000 },
196 .n = { .min = 1, .max = 6 },
197 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
198 .m1 = { .min = 8, .max = 18 },
199 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
200 .p = { .min = 5, .max = 80 },
201 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
202 .p2 = { .dot_limit = 200000,
203 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
204};
205
206static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
207 .dot = { .min = 20000, .max = 400000 },
208 .vco = { .min = 1400000, .max = 2800000 },
209 .n = { .min = 1, .max = 6 },
210 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
211 .m1 = { .min = 8, .max = 18 },
212 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
213 .p = { .min = 7, .max = 98 },
214 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
215 .p2 = { .dot_limit = 112000,
216 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
217};
218
273e27ca 219
e4b36699 220static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
221 .dot = { .min = 25000, .max = 270000 },
222 .vco = { .min = 1750000, .max = 3500000},
223 .n = { .min = 1, .max = 4 },
224 .m = { .min = 104, .max = 138 },
225 .m1 = { .min = 17, .max = 23 },
226 .m2 = { .min = 5, .max = 11 },
227 .p = { .min = 10, .max = 30 },
228 .p1 = { .min = 1, .max = 3},
229 .p2 = { .dot_limit = 270000,
230 .p2_slow = 10,
231 .p2_fast = 10
044c7c41 232 },
e4b36699
KP
233};
234
235static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
236 .dot = { .min = 22000, .max = 400000 },
237 .vco = { .min = 1750000, .max = 3500000},
238 .n = { .min = 1, .max = 4 },
239 .m = { .min = 104, .max = 138 },
240 .m1 = { .min = 16, .max = 23 },
241 .m2 = { .min = 5, .max = 11 },
242 .p = { .min = 5, .max = 80 },
243 .p1 = { .min = 1, .max = 8},
244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
246};
247
248static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
249 .dot = { .min = 20000, .max = 115000 },
250 .vco = { .min = 1750000, .max = 3500000 },
251 .n = { .min = 1, .max = 3 },
252 .m = { .min = 104, .max = 138 },
253 .m1 = { .min = 17, .max = 23 },
254 .m2 = { .min = 5, .max = 11 },
255 .p = { .min = 28, .max = 112 },
256 .p1 = { .min = 2, .max = 8 },
257 .p2 = { .dot_limit = 0,
258 .p2_slow = 14, .p2_fast = 14
044c7c41 259 },
e4b36699
KP
260};
261
262static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
263 .dot = { .min = 80000, .max = 224000 },
264 .vco = { .min = 1750000, .max = 3500000 },
265 .n = { .min = 1, .max = 3 },
266 .m = { .min = 104, .max = 138 },
267 .m1 = { .min = 17, .max = 23 },
268 .m2 = { .min = 5, .max = 11 },
269 .p = { .min = 14, .max = 42 },
270 .p1 = { .min = 2, .max = 6 },
271 .p2 = { .dot_limit = 0,
272 .p2_slow = 7, .p2_fast = 7
044c7c41 273 },
e4b36699
KP
274};
275
f2b115e6 276static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
277 .dot = { .min = 20000, .max = 400000},
278 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 279 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
280 .n = { .min = 3, .max = 6 },
281 .m = { .min = 2, .max = 256 },
273e27ca 282 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
283 .m1 = { .min = 0, .max = 0 },
284 .m2 = { .min = 0, .max = 254 },
285 .p = { .min = 5, .max = 80 },
286 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
287 .p2 = { .dot_limit = 200000,
288 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
289};
290
f2b115e6 291static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
292 .dot = { .min = 20000, .max = 400000 },
293 .vco = { .min = 1700000, .max = 3500000 },
294 .n = { .min = 3, .max = 6 },
295 .m = { .min = 2, .max = 256 },
296 .m1 = { .min = 0, .max = 0 },
297 .m2 = { .min = 0, .max = 254 },
298 .p = { .min = 7, .max = 112 },
299 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
300 .p2 = { .dot_limit = 112000,
301 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
302};
303
273e27ca
EA
304/* Ironlake / Sandybridge
305 *
306 * We calculate clock using (register_value + 2) for N/M1/M2, so here
307 * the range value for them is (actual_value - 2).
308 */
b91ad0ec 309static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
310 .dot = { .min = 25000, .max = 350000 },
311 .vco = { .min = 1760000, .max = 3510000 },
312 .n = { .min = 1, .max = 5 },
313 .m = { .min = 79, .max = 127 },
314 .m1 = { .min = 12, .max = 22 },
315 .m2 = { .min = 5, .max = 9 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8 },
318 .p2 = { .dot_limit = 225000,
319 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
320};
321
b91ad0ec 322static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 118 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 28, .max = 112 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
333};
334
335static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
336 .dot = { .min = 25000, .max = 350000 },
337 .vco = { .min = 1760000, .max = 3510000 },
338 .n = { .min = 1, .max = 3 },
339 .m = { .min = 79, .max = 127 },
340 .m1 = { .min = 12, .max = 22 },
341 .m2 = { .min = 5, .max = 9 },
342 .p = { .min = 14, .max = 56 },
343 .p1 = { .min = 2, .max = 8 },
344 .p2 = { .dot_limit = 225000,
345 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
346};
347
273e27ca 348/* LVDS 100mhz refclk limits. */
b91ad0ec 349static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 79, .max = 126 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 28, .max = 112 },
0206e353 357 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
360};
361
362static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
363 .dot = { .min = 25000, .max = 350000 },
364 .vco = { .min = 1760000, .max = 3510000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 79, .max = 126 },
367 .m1 = { .min = 12, .max = 22 },
368 .m2 = { .min = 5, .max = 9 },
369 .p = { .min = 14, .max = 42 },
0206e353 370 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
371 .p2 = { .dot_limit = 225000,
372 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
373};
374
dc730512 375static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 383 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 384 .n = { .min = 1, .max = 7 },
a0c4da24
JB
385 .m1 = { .min = 2, .max = 3 },
386 .m2 = { .min = 11, .max = 156 },
b99ab663 387 .p1 = { .min = 2, .max = 3 },
5fdc9c49 388 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
389};
390
ef9348c8
CML
391static const intel_limit_t intel_limits_chv = {
392 /*
393 * These are the data rate limits (measured in fast clocks)
394 * since those are the strictest limits we have. The fast
395 * clock and actual rate limits are more relaxed, so checking
396 * them would make no difference.
397 */
398 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 399 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
400 .n = { .min = 1, .max = 1 },
401 .m1 = { .min = 2, .max = 2 },
402 .m2 = { .min = 24 << 22, .max = 175 << 22 },
403 .p1 = { .min = 2, .max = 4 },
404 .p2 = { .p2_slow = 1, .p2_fast = 14 },
405};
406
5ab7b0b7
ID
407static const intel_limit_t intel_limits_bxt = {
408 /* FIXME: find real dot limits */
409 .dot = { .min = 0, .max = INT_MAX },
410 .vco = { .min = 4800000, .max = 6480000 },
411 .n = { .min = 1, .max = 1 },
412 .m1 = { .min = 2, .max = 2 },
413 /* FIXME: find real m2 limits */
414 .m2 = { .min = 2 << 22, .max = 255 << 22 },
415 .p1 = { .min = 2, .max = 4 },
416 .p2 = { .p2_slow = 1, .p2_fast = 20 },
417};
418
6b4bf1c4
VS
419static void vlv_clock(int refclk, intel_clock_t *clock)
420{
421 clock->m = clock->m1 * clock->m2;
422 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
423 if (WARN_ON(clock->n == 0 || clock->p == 0))
424 return;
fb03ac01
VS
425 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
426 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
427}
428
e0638cdf
PZ
429/**
430 * Returns whether any output on the specified pipe is of the specified type
431 */
4093561b 432bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 433{
409ee761 434 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
435 struct intel_encoder *encoder;
436
409ee761 437 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
438 if (encoder->type == type)
439 return true;
440
441 return false;
442}
443
d0737e1d
ACO
444/**
445 * Returns whether any output on the specified pipe will have the specified
446 * type after a staged modeset is complete, i.e., the same as
447 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
448 * encoder->crtc.
449 */
a93e255f
ACO
450static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
451 int type)
d0737e1d 452{
a93e255f
ACO
453 struct drm_atomic_state *state = crtc_state->base.state;
454 struct drm_connector_state *connector_state;
d0737e1d 455 struct intel_encoder *encoder;
a93e255f
ACO
456 int i, num_connectors = 0;
457
458 for (i = 0; i < state->num_connector; i++) {
459 if (!state->connectors[i])
460 continue;
461
462 connector_state = state->connector_states[i];
463 if (connector_state->crtc != crtc_state->base.crtc)
464 continue;
465
466 num_connectors++;
d0737e1d 467
a93e255f
ACO
468 encoder = to_intel_encoder(connector_state->best_encoder);
469 if (encoder->type == type)
d0737e1d 470 return true;
a93e255f
ACO
471 }
472
473 WARN_ON(num_connectors == 0);
d0737e1d
ACO
474
475 return false;
476}
477
a93e255f
ACO
478static const intel_limit_t *
479intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 480{
a93e255f 481 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 482 const intel_limit_t *limit;
b91ad0ec 483
a93e255f 484 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 485 if (intel_is_dual_link_lvds(dev)) {
1b894b59 486 if (refclk == 100000)
b91ad0ec
ZW
487 limit = &intel_limits_ironlake_dual_lvds_100m;
488 else
489 limit = &intel_limits_ironlake_dual_lvds;
490 } else {
1b894b59 491 if (refclk == 100000)
b91ad0ec
ZW
492 limit = &intel_limits_ironlake_single_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_single_lvds;
495 }
c6bb3538 496 } else
b91ad0ec 497 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
498
499 return limit;
500}
501
a93e255f
ACO
502static const intel_limit_t *
503intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 504{
a93e255f 505 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
506 const intel_limit_t *limit;
507
a93e255f 508 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 509 if (intel_is_dual_link_lvds(dev))
e4b36699 510 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 511 else
e4b36699 512 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
513 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
514 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 515 limit = &intel_limits_g4x_hdmi;
a93e255f 516 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 517 limit = &intel_limits_g4x_sdvo;
044c7c41 518 } else /* The option is for other outputs */
e4b36699 519 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
520
521 return limit;
522}
523
a93e255f
ACO
524static const intel_limit_t *
525intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 526{
a93e255f 527 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
528 const intel_limit_t *limit;
529
5ab7b0b7
ID
530 if (IS_BROXTON(dev))
531 limit = &intel_limits_bxt;
532 else if (HAS_PCH_SPLIT(dev))
a93e255f 533 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 534 else if (IS_G4X(dev)) {
a93e255f 535 limit = intel_g4x_limit(crtc_state);
f2b115e6 536 } else if (IS_PINEVIEW(dev)) {
a93e255f 537 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 538 limit = &intel_limits_pineview_lvds;
2177832f 539 else
f2b115e6 540 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
541 } else if (IS_CHERRYVIEW(dev)) {
542 limit = &intel_limits_chv;
a0c4da24 543 } else if (IS_VALLEYVIEW(dev)) {
dc730512 544 limit = &intel_limits_vlv;
a6c45cf0 545 } else if (!IS_GEN2(dev)) {
a93e255f 546 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
547 limit = &intel_limits_i9xx_lvds;
548 else
549 limit = &intel_limits_i9xx_sdvo;
79e53945 550 } else {
a93e255f 551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 552 limit = &intel_limits_i8xx_lvds;
a93e255f 553 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 554 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
555 else
556 limit = &intel_limits_i8xx_dac;
79e53945
JB
557 }
558 return limit;
559}
560
f2b115e6
AJ
561/* m1 is reserved as 0 in Pineview, n is a ring counter */
562static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 563{
2177832f
SL
564 clock->m = clock->m2 + 2;
565 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
566 if (WARN_ON(clock->n == 0 || clock->p == 0))
567 return;
fb03ac01
VS
568 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
569 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
570}
571
7429e9d4
DV
572static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
573{
574 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
575}
576
ac58c3f0 577static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 578{
7429e9d4 579 clock->m = i9xx_dpll_compute_m(clock);
79e53945 580 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
581 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
582 return;
fb03ac01
VS
583 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
584 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
585}
586
ef9348c8
CML
587static void chv_clock(int refclk, intel_clock_t *clock)
588{
589 clock->m = clock->m1 * clock->m2;
590 clock->p = clock->p1 * clock->p2;
591 if (WARN_ON(clock->n == 0 || clock->p == 0))
592 return;
593 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
594 clock->n << 22);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
596}
597
7c04d1d9 598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
1b894b59
CW
604static bool intel_PLL_is_valid(struct drm_device *dev,
605 const intel_limit_t *limit,
606 const intel_clock_t *clock)
79e53945 607{
f01b7962
VS
608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
79e53945 610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 611 INTELPllInvalid("p1 out of range\n");
79e53945 612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 613 INTELPllInvalid("m2 out of range\n");
79e53945 614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 615 INTELPllInvalid("m1 out of range\n");
f01b7962 616
5ab7b0b7 617 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
5ab7b0b7 621 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
79e53945 628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 629 INTELPllInvalid("vco out of range\n");
79e53945
JB
630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 634 INTELPllInvalid("dot out of range\n");
79e53945
JB
635
636 return true;
637}
638
d4906093 639static bool
a93e255f
ACO
640i9xx_find_best_dpll(const intel_limit_t *limit,
641 struct intel_crtc_state *crtc_state,
cec2f356
SP
642 int target, int refclk, intel_clock_t *match_clock,
643 intel_clock_t *best_clock)
79e53945 644{
a93e255f 645 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 646 struct drm_device *dev = crtc->base.dev;
79e53945 647 intel_clock_t clock;
79e53945
JB
648 int err = target;
649
a93e255f 650 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 651 /*
a210b028
DV
652 * For LVDS just rely on its current settings for dual-channel.
653 * We haven't figured out how to reliably set up different
654 * single/dual channel state, if we even can.
79e53945 655 */
1974cad0 656 if (intel_is_dual_link_lvds(dev))
79e53945
JB
657 clock.p2 = limit->p2.p2_fast;
658 else
659 clock.p2 = limit->p2.p2_slow;
660 } else {
661 if (target < limit->p2.dot_limit)
662 clock.p2 = limit->p2.p2_slow;
663 else
664 clock.p2 = limit->p2.p2_fast;
665 }
666
0206e353 667 memset(best_clock, 0, sizeof(*best_clock));
79e53945 668
42158660
ZY
669 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
670 clock.m1++) {
671 for (clock.m2 = limit->m2.min;
672 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 673 if (clock.m2 >= clock.m1)
42158660
ZY
674 break;
675 for (clock.n = limit->n.min;
676 clock.n <= limit->n.max; clock.n++) {
677 for (clock.p1 = limit->p1.min;
678 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
679 int this_err;
680
ac58c3f0
DV
681 i9xx_clock(refclk, &clock);
682 if (!intel_PLL_is_valid(dev, limit,
683 &clock))
684 continue;
685 if (match_clock &&
686 clock.p != match_clock->p)
687 continue;
688
689 this_err = abs(clock.dot - target);
690 if (this_err < err) {
691 *best_clock = clock;
692 err = this_err;
693 }
694 }
695 }
696 }
697 }
698
699 return (err != target);
700}
701
702static bool
a93e255f
ACO
703pnv_find_best_dpll(const intel_limit_t *limit,
704 struct intel_crtc_state *crtc_state,
ee9300bb
DV
705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
79e53945 707{
a93e255f 708 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 709 struct drm_device *dev = crtc->base.dev;
79e53945 710 intel_clock_t clock;
79e53945
JB
711 int err = target;
712
a93e255f 713 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 714 /*
a210b028
DV
715 * For LVDS just rely on its current settings for dual-channel.
716 * We haven't figured out how to reliably set up different
717 * single/dual channel state, if we even can.
79e53945 718 */
1974cad0 719 if (intel_is_dual_link_lvds(dev))
79e53945
JB
720 clock.p2 = limit->p2.p2_fast;
721 else
722 clock.p2 = limit->p2.p2_slow;
723 } else {
724 if (target < limit->p2.dot_limit)
725 clock.p2 = limit->p2.p2_slow;
726 else
727 clock.p2 = limit->p2.p2_fast;
728 }
729
0206e353 730 memset(best_clock, 0, sizeof(*best_clock));
79e53945 731
42158660
ZY
732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
733 clock.m1++) {
734 for (clock.m2 = limit->m2.min;
735 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
736 for (clock.n = limit->n.min;
737 clock.n <= limit->n.max; clock.n++) {
738 for (clock.p1 = limit->p1.min;
739 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
740 int this_err;
741
ac58c3f0 742 pineview_clock(refclk, &clock);
1b894b59
CW
743 if (!intel_PLL_is_valid(dev, limit,
744 &clock))
79e53945 745 continue;
cec2f356
SP
746 if (match_clock &&
747 clock.p != match_clock->p)
748 continue;
79e53945
JB
749
750 this_err = abs(clock.dot - target);
751 if (this_err < err) {
752 *best_clock = clock;
753 err = this_err;
754 }
755 }
756 }
757 }
758 }
759
760 return (err != target);
761}
762
d4906093 763static bool
a93e255f
ACO
764g4x_find_best_dpll(const intel_limit_t *limit,
765 struct intel_crtc_state *crtc_state,
ee9300bb
DV
766 int target, int refclk, intel_clock_t *match_clock,
767 intel_clock_t *best_clock)
d4906093 768{
a93e255f 769 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 770 struct drm_device *dev = crtc->base.dev;
d4906093
ML
771 intel_clock_t clock;
772 int max_n;
773 bool found;
6ba770dc
AJ
774 /* approximately equals target * 0.00585 */
775 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
776 found = false;
777
a93e255f 778 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 779 if (intel_is_dual_link_lvds(dev))
d4906093
ML
780 clock.p2 = limit->p2.p2_fast;
781 else
782 clock.p2 = limit->p2.p2_slow;
783 } else {
784 if (target < limit->p2.dot_limit)
785 clock.p2 = limit->p2.p2_slow;
786 else
787 clock.p2 = limit->p2.p2_fast;
788 }
789
790 memset(best_clock, 0, sizeof(*best_clock));
791 max_n = limit->n.max;
f77f13e2 792 /* based on hardware requirement, prefer smaller n to precision */
d4906093 793 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 794 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
795 for (clock.m1 = limit->m1.max;
796 clock.m1 >= limit->m1.min; clock.m1--) {
797 for (clock.m2 = limit->m2.max;
798 clock.m2 >= limit->m2.min; clock.m2--) {
799 for (clock.p1 = limit->p1.max;
800 clock.p1 >= limit->p1.min; clock.p1--) {
801 int this_err;
802
ac58c3f0 803 i9xx_clock(refclk, &clock);
1b894b59
CW
804 if (!intel_PLL_is_valid(dev, limit,
805 &clock))
d4906093 806 continue;
1b894b59
CW
807
808 this_err = abs(clock.dot - target);
d4906093
ML
809 if (this_err < err_most) {
810 *best_clock = clock;
811 err_most = this_err;
812 max_n = clock.n;
813 found = true;
814 }
815 }
816 }
817 }
818 }
2c07245f
ZW
819 return found;
820}
821
d5dd62bd
ID
822/*
823 * Check if the calculated PLL configuration is more optimal compared to the
824 * best configuration and error found so far. Return the calculated error.
825 */
826static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
827 const intel_clock_t *calculated_clock,
828 const intel_clock_t *best_clock,
829 unsigned int best_error_ppm,
830 unsigned int *error_ppm)
831{
9ca3ba01
ID
832 /*
833 * For CHV ignore the error and consider only the P value.
834 * Prefer a bigger P value based on HW requirements.
835 */
836 if (IS_CHERRYVIEW(dev)) {
837 *error_ppm = 0;
838
839 return calculated_clock->p > best_clock->p;
840 }
841
24be4e46
ID
842 if (WARN_ON_ONCE(!target_freq))
843 return false;
844
d5dd62bd
ID
845 *error_ppm = div_u64(1000000ULL *
846 abs(target_freq - calculated_clock->dot),
847 target_freq);
848 /*
849 * Prefer a better P value over a better (smaller) error if the error
850 * is small. Ensure this preference for future configurations too by
851 * setting the error to 0.
852 */
853 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
854 *error_ppm = 0;
855
856 return true;
857 }
858
859 return *error_ppm + 10 < best_error_ppm;
860}
861
a0c4da24 862static bool
a93e255f
ACO
863vlv_find_best_dpll(const intel_limit_t *limit,
864 struct intel_crtc_state *crtc_state,
ee9300bb
DV
865 int target, int refclk, intel_clock_t *match_clock,
866 intel_clock_t *best_clock)
a0c4da24 867{
a93e255f 868 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 869 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 870 intel_clock_t clock;
69e4f900 871 unsigned int bestppm = 1000000;
27e639bf
VS
872 /* min update 19.2 MHz */
873 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 874 bool found = false;
a0c4da24 875
6b4bf1c4
VS
876 target *= 5; /* fast clock */
877
878 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
879
880 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 881 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 882 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 883 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 884 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 885 clock.p = clock.p1 * clock.p2;
a0c4da24 886 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 887 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 888 unsigned int ppm;
69e4f900 889
6b4bf1c4
VS
890 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
891 refclk * clock.m1);
892
893 vlv_clock(refclk, &clock);
43b0ac53 894
f01b7962
VS
895 if (!intel_PLL_is_valid(dev, limit,
896 &clock))
43b0ac53
VS
897 continue;
898
d5dd62bd
ID
899 if (!vlv_PLL_is_optimal(dev, target,
900 &clock,
901 best_clock,
902 bestppm, &ppm))
903 continue;
6b4bf1c4 904
d5dd62bd
ID
905 *best_clock = clock;
906 bestppm = ppm;
907 found = true;
a0c4da24
JB
908 }
909 }
910 }
911 }
a0c4da24 912
49e497ef 913 return found;
a0c4da24 914}
a4fc5ed6 915
ef9348c8 916static bool
a93e255f
ACO
917chv_find_best_dpll(const intel_limit_t *limit,
918 struct intel_crtc_state *crtc_state,
ef9348c8
CML
919 int target, int refclk, intel_clock_t *match_clock,
920 intel_clock_t *best_clock)
921{
a93e255f 922 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 923 struct drm_device *dev = crtc->base.dev;
9ca3ba01 924 unsigned int best_error_ppm;
ef9348c8
CML
925 intel_clock_t clock;
926 uint64_t m2;
927 int found = false;
928
929 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 930 best_error_ppm = 1000000;
ef9348c8
CML
931
932 /*
933 * Based on hardware doc, the n always set to 1, and m1 always
934 * set to 2. If requires to support 200Mhz refclk, we need to
935 * revisit this because n may not 1 anymore.
936 */
937 clock.n = 1, clock.m1 = 2;
938 target *= 5; /* fast clock */
939
940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
941 for (clock.p2 = limit->p2.p2_fast;
942 clock.p2 >= limit->p2.p2_slow;
943 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 944 unsigned int error_ppm;
ef9348c8
CML
945
946 clock.p = clock.p1 * clock.p2;
947
948 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
949 clock.n) << 22, refclk * clock.m1);
950
951 if (m2 > INT_MAX/clock.m1)
952 continue;
953
954 clock.m2 = m2;
955
956 chv_clock(refclk, &clock);
957
958 if (!intel_PLL_is_valid(dev, limit, &clock))
959 continue;
960
9ca3ba01
ID
961 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
962 best_error_ppm, &error_ppm))
963 continue;
964
965 *best_clock = clock;
966 best_error_ppm = error_ppm;
967 found = true;
ef9348c8
CML
968 }
969 }
970
971 return found;
972}
973
5ab7b0b7
ID
974bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
975 intel_clock_t *best_clock)
976{
977 int refclk = i9xx_get_refclk(crtc_state, 0);
978
979 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
980 target_clock, refclk, NULL, best_clock);
981}
982
20ddf665
VS
983bool intel_crtc_active(struct drm_crtc *crtc)
984{
985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
986
987 /* Be paranoid as we can arrive here with only partial
988 * state retrieved from the hardware during setup.
989 *
241bfc38 990 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
991 * as Haswell has gained clock readout/fastboot support.
992 *
66e514c1 993 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 994 * properly reconstruct framebuffers.
c3d1f436
MR
995 *
996 * FIXME: The intel_crtc->active here should be switched to
997 * crtc->state->active once we have proper CRTC states wired up
998 * for atomic.
20ddf665 999 */
c3d1f436 1000 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1001 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1002}
1003
a5c961d1
PZ
1004enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1005 enum pipe pipe)
1006{
1007 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
6e3c9717 1010 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1011}
1012
fbf49ea2
VS
1013static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1014{
1015 struct drm_i915_private *dev_priv = dev->dev_private;
1016 u32 reg = PIPEDSL(pipe);
1017 u32 line1, line2;
1018 u32 line_mask;
1019
1020 if (IS_GEN2(dev))
1021 line_mask = DSL_LINEMASK_GEN2;
1022 else
1023 line_mask = DSL_LINEMASK_GEN3;
1024
1025 line1 = I915_READ(reg) & line_mask;
1026 mdelay(5);
1027 line2 = I915_READ(reg) & line_mask;
1028
1029 return line1 == line2;
1030}
1031
ab7ad7f6
KP
1032/*
1033 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1034 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1035 *
1036 * After disabling a pipe, we can't wait for vblank in the usual way,
1037 * spinning on the vblank interrupt status bit, since we won't actually
1038 * see an interrupt when the pipe is disabled.
1039 *
ab7ad7f6
KP
1040 * On Gen4 and above:
1041 * wait for the pipe register state bit to turn off
1042 *
1043 * Otherwise:
1044 * wait for the display line value to settle (it usually
1045 * ends up stopping at the start of the next frame).
58e10eb9 1046 *
9d0498a2 1047 */
575f7ab7 1048static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1049{
575f7ab7 1050 struct drm_device *dev = crtc->base.dev;
9d0498a2 1051 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1052 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1053 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1054
1055 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1056 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1057
1058 /* Wait for the Pipe State to go off */
58e10eb9
CW
1059 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1060 100))
284637d9 1061 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1062 } else {
ab7ad7f6 1063 /* Wait for the display line to settle */
fbf49ea2 1064 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1065 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1066 }
79e53945
JB
1067}
1068
b0ea7d37
DL
1069/*
1070 * ibx_digital_port_connected - is the specified port connected?
1071 * @dev_priv: i915 private structure
1072 * @port: the port to test
1073 *
1074 * Returns true if @port is connected, false otherwise.
1075 */
1076bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1077 struct intel_digital_port *port)
1078{
1079 u32 bit;
1080
c36346e3 1081 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1082 switch (port->port) {
c36346e3
DL
1083 case PORT_B:
1084 bit = SDE_PORTB_HOTPLUG;
1085 break;
1086 case PORT_C:
1087 bit = SDE_PORTC_HOTPLUG;
1088 break;
1089 case PORT_D:
1090 bit = SDE_PORTD_HOTPLUG;
1091 break;
1092 default:
1093 return true;
1094 }
1095 } else {
eba905b2 1096 switch (port->port) {
c36346e3
DL
1097 case PORT_B:
1098 bit = SDE_PORTB_HOTPLUG_CPT;
1099 break;
1100 case PORT_C:
1101 bit = SDE_PORTC_HOTPLUG_CPT;
1102 break;
1103 case PORT_D:
1104 bit = SDE_PORTD_HOTPLUG_CPT;
1105 break;
1106 default:
1107 return true;
1108 }
b0ea7d37
DL
1109 }
1110
1111 return I915_READ(SDEISR) & bit;
1112}
1113
b24e7179
JB
1114static const char *state_string(bool enabled)
1115{
1116 return enabled ? "on" : "off";
1117}
1118
1119/* Only for pre-ILK configs */
55607e8a
DV
1120void assert_pll(struct drm_i915_private *dev_priv,
1121 enum pipe pipe, bool state)
b24e7179
JB
1122{
1123 int reg;
1124 u32 val;
1125 bool cur_state;
1126
1127 reg = DPLL(pipe);
1128 val = I915_READ(reg);
1129 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1130 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1131 "PLL state assertion failure (expected %s, current %s)\n",
1132 state_string(state), state_string(cur_state));
1133}
b24e7179 1134
23538ef1
JN
1135/* XXX: the dsi pll is shared between MIPI DSI ports */
1136static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1137{
1138 u32 val;
1139 bool cur_state;
1140
1141 mutex_lock(&dev_priv->dpio_lock);
1142 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1143 mutex_unlock(&dev_priv->dpio_lock);
1144
1145 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1146 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1147 "DSI PLL state assertion failure (expected %s, current %s)\n",
1148 state_string(state), state_string(cur_state));
1149}
1150#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1151#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1152
55607e8a 1153struct intel_shared_dpll *
e2b78267
DV
1154intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1155{
1156 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1157
6e3c9717 1158 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1159 return NULL;
1160
6e3c9717 1161 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1162}
1163
040484af 1164/* For ILK+ */
55607e8a
DV
1165void assert_shared_dpll(struct drm_i915_private *dev_priv,
1166 struct intel_shared_dpll *pll,
1167 bool state)
040484af 1168{
040484af 1169 bool cur_state;
5358901f 1170 struct intel_dpll_hw_state hw_state;
040484af 1171
92b27b08 1172 if (WARN (!pll,
46edb027 1173 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1174 return;
ee7b9f93 1175
5358901f 1176 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1177 I915_STATE_WARN(cur_state != state,
5358901f
DV
1178 "%s assertion failure (expected %s, current %s)\n",
1179 pll->name, state_string(state), state_string(cur_state));
040484af 1180}
040484af
JB
1181
1182static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1183 enum pipe pipe, bool state)
1184{
1185 int reg;
1186 u32 val;
1187 bool cur_state;
ad80a810
PZ
1188 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1189 pipe);
040484af 1190
affa9354
PZ
1191 if (HAS_DDI(dev_priv->dev)) {
1192 /* DDI does not have a specific FDI_TX register */
ad80a810 1193 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1194 val = I915_READ(reg);
ad80a810 1195 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1196 } else {
1197 reg = FDI_TX_CTL(pipe);
1198 val = I915_READ(reg);
1199 cur_state = !!(val & FDI_TX_ENABLE);
1200 }
e2c719b7 1201 I915_STATE_WARN(cur_state != state,
040484af
JB
1202 "FDI TX state assertion failure (expected %s, current %s)\n",
1203 state_string(state), state_string(cur_state));
1204}
1205#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1206#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1207
1208static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1209 enum pipe pipe, bool state)
1210{
1211 int reg;
1212 u32 val;
1213 bool cur_state;
1214
d63fa0dc
PZ
1215 reg = FDI_RX_CTL(pipe);
1216 val = I915_READ(reg);
1217 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1218 I915_STATE_WARN(cur_state != state,
040484af
JB
1219 "FDI RX state assertion failure (expected %s, current %s)\n",
1220 state_string(state), state_string(cur_state));
1221}
1222#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1223#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1224
1225static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1226 enum pipe pipe)
1227{
1228 int reg;
1229 u32 val;
1230
1231 /* ILK FDI PLL is always enabled */
3d13ef2e 1232 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1233 return;
1234
bf507ef7 1235 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1236 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1237 return;
1238
040484af
JB
1239 reg = FDI_TX_CTL(pipe);
1240 val = I915_READ(reg);
e2c719b7 1241 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1242}
1243
55607e8a
DV
1244void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1245 enum pipe pipe, bool state)
040484af
JB
1246{
1247 int reg;
1248 u32 val;
55607e8a 1249 bool cur_state;
040484af
JB
1250
1251 reg = FDI_RX_CTL(pipe);
1252 val = I915_READ(reg);
55607e8a 1253 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1254 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1255 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1256 state_string(state), state_string(cur_state));
040484af
JB
1257}
1258
b680c37a
DV
1259void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1260 enum pipe pipe)
ea0760cf 1261{
bedd4dba
JN
1262 struct drm_device *dev = dev_priv->dev;
1263 int pp_reg;
ea0760cf
JB
1264 u32 val;
1265 enum pipe panel_pipe = PIPE_A;
0de3b485 1266 bool locked = true;
ea0760cf 1267
bedd4dba
JN
1268 if (WARN_ON(HAS_DDI(dev)))
1269 return;
1270
1271 if (HAS_PCH_SPLIT(dev)) {
1272 u32 port_sel;
1273
ea0760cf 1274 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1275 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1276
1277 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1278 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1279 panel_pipe = PIPE_B;
1280 /* XXX: else fix for eDP */
1281 } else if (IS_VALLEYVIEW(dev)) {
1282 /* presumably write lock depends on pipe, not port select */
1283 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1284 panel_pipe = pipe;
ea0760cf
JB
1285 } else {
1286 pp_reg = PP_CONTROL;
bedd4dba
JN
1287 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1288 panel_pipe = PIPE_B;
ea0760cf
JB
1289 }
1290
1291 val = I915_READ(pp_reg);
1292 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1293 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1294 locked = false;
1295
e2c719b7 1296 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1297 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1298 pipe_name(pipe));
ea0760cf
JB
1299}
1300
93ce0ba6
JN
1301static void assert_cursor(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, bool state)
1303{
1304 struct drm_device *dev = dev_priv->dev;
1305 bool cur_state;
1306
d9d82081 1307 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1308 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1309 else
5efb3e28 1310 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1311
e2c719b7 1312 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1313 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1314 pipe_name(pipe), state_string(state), state_string(cur_state));
1315}
1316#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1317#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1318
b840d907
JB
1319void assert_pipe(struct drm_i915_private *dev_priv,
1320 enum pipe pipe, bool state)
b24e7179
JB
1321{
1322 int reg;
1323 u32 val;
63d7bbe9 1324 bool cur_state;
702e7a56
PZ
1325 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1326 pipe);
b24e7179 1327
b6b5d049
VS
1328 /* if we need the pipe quirk it must be always on */
1329 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1330 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1331 state = true;
1332
f458ebbc 1333 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1334 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1335 cur_state = false;
1336 } else {
1337 reg = PIPECONF(cpu_transcoder);
1338 val = I915_READ(reg);
1339 cur_state = !!(val & PIPECONF_ENABLE);
1340 }
1341
e2c719b7 1342 I915_STATE_WARN(cur_state != state,
63d7bbe9 1343 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1344 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1345}
1346
931872fc
CW
1347static void assert_plane(struct drm_i915_private *dev_priv,
1348 enum plane plane, bool state)
b24e7179
JB
1349{
1350 int reg;
1351 u32 val;
931872fc 1352 bool cur_state;
b24e7179
JB
1353
1354 reg = DSPCNTR(plane);
1355 val = I915_READ(reg);
931872fc 1356 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1357 I915_STATE_WARN(cur_state != state,
931872fc
CW
1358 "plane %c assertion failure (expected %s, current %s)\n",
1359 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1360}
1361
931872fc
CW
1362#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1363#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1364
b24e7179
JB
1365static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
653e1026 1368 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1369 int reg, i;
1370 u32 val;
1371 int cur_pipe;
1372
653e1026
VS
1373 /* Primary planes are fixed to pipes on gen4+ */
1374 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1375 reg = DSPCNTR(pipe);
1376 val = I915_READ(reg);
e2c719b7 1377 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1378 "plane %c assertion failure, should be disabled but not\n",
1379 plane_name(pipe));
19ec1358 1380 return;
28c05794 1381 }
19ec1358 1382
b24e7179 1383 /* Need to check both planes against the pipe */
055e393f 1384 for_each_pipe(dev_priv, i) {
b24e7179
JB
1385 reg = DSPCNTR(i);
1386 val = I915_READ(reg);
1387 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1388 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1389 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1390 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1391 plane_name(i), pipe_name(pipe));
b24e7179
JB
1392 }
1393}
1394
19332d7a
JB
1395static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe)
1397{
20674eef 1398 struct drm_device *dev = dev_priv->dev;
1fe47785 1399 int reg, sprite;
19332d7a
JB
1400 u32 val;
1401
7feb8b88 1402 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1403 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1404 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1405 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1406 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1407 sprite, pipe_name(pipe));
1408 }
1409 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1410 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1411 reg = SPCNTR(pipe, sprite);
20674eef 1412 val = I915_READ(reg);
e2c719b7 1413 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1415 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1416 }
1417 } else if (INTEL_INFO(dev)->gen >= 7) {
1418 reg = SPRCTL(pipe);
19332d7a 1419 val = I915_READ(reg);
e2c719b7 1420 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1422 plane_name(pipe), pipe_name(pipe));
1423 } else if (INTEL_INFO(dev)->gen >= 5) {
1424 reg = DVSCNTR(pipe);
19332d7a 1425 val = I915_READ(reg);
e2c719b7 1426 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1427 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1428 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1429 }
1430}
1431
08c71e5e
VS
1432static void assert_vblank_disabled(struct drm_crtc *crtc)
1433{
e2c719b7 1434 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1435 drm_crtc_vblank_put(crtc);
1436}
1437
89eff4be 1438static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1439{
1440 u32 val;
1441 bool enabled;
1442
e2c719b7 1443 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1444
92f2584a
JB
1445 val = I915_READ(PCH_DREF_CONTROL);
1446 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1447 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1448 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1449}
1450
ab9412ba
DV
1451static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe)
92f2584a
JB
1453{
1454 int reg;
1455 u32 val;
1456 bool enabled;
1457
ab9412ba 1458 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1459 val = I915_READ(reg);
1460 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1461 I915_STATE_WARN(enabled,
9db4a9c7
JB
1462 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1463 pipe_name(pipe));
92f2584a
JB
1464}
1465
4e634389
KP
1466static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1468{
1469 if ((val & DP_PORT_EN) == 0)
1470 return false;
1471
1472 if (HAS_PCH_CPT(dev_priv->dev)) {
1473 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1474 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1475 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1476 return false;
44f37d1f
CML
1477 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1478 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1479 return false;
f0575e92
KP
1480 } else {
1481 if ((val & DP_PIPE_MASK) != (pipe << 30))
1482 return false;
1483 }
1484 return true;
1485}
1486
1519b995
KP
1487static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1488 enum pipe pipe, u32 val)
1489{
dc0fa718 1490 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1491 return false;
1492
1493 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1494 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1495 return false;
44f37d1f
CML
1496 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1497 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1498 return false;
1519b995 1499 } else {
dc0fa718 1500 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1501 return false;
1502 }
1503 return true;
1504}
1505
1506static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1507 enum pipe pipe, u32 val)
1508{
1509 if ((val & LVDS_PORT_EN) == 0)
1510 return false;
1511
1512 if (HAS_PCH_CPT(dev_priv->dev)) {
1513 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1514 return false;
1515 } else {
1516 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1517 return false;
1518 }
1519 return true;
1520}
1521
1522static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1523 enum pipe pipe, u32 val)
1524{
1525 if ((val & ADPA_DAC_ENABLE) == 0)
1526 return false;
1527 if (HAS_PCH_CPT(dev_priv->dev)) {
1528 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1529 return false;
1530 } else {
1531 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1532 return false;
1533 }
1534 return true;
1535}
1536
291906f1 1537static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1538 enum pipe pipe, int reg, u32 port_sel)
291906f1 1539{
47a05eca 1540 u32 val = I915_READ(reg);
e2c719b7 1541 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1542 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1543 reg, pipe_name(pipe));
de9a35ab 1544
e2c719b7 1545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1546 && (val & DP_PIPEB_SELECT),
de9a35ab 1547 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1548}
1549
1550static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe, int reg)
1552{
47a05eca 1553 u32 val = I915_READ(reg);
e2c719b7 1554 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1555 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1556 reg, pipe_name(pipe));
de9a35ab 1557
e2c719b7 1558 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1559 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1560 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1561}
1562
1563static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1564 enum pipe pipe)
1565{
1566 int reg;
1567 u32 val;
291906f1 1568
f0575e92
KP
1569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1570 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1572
1573 reg = PCH_ADPA;
1574 val = I915_READ(reg);
e2c719b7 1575 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1576 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1577 pipe_name(pipe));
291906f1
JB
1578
1579 reg = PCH_LVDS;
1580 val = I915_READ(reg);
e2c719b7 1581 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1582 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1583 pipe_name(pipe));
291906f1 1584
e2debe91
PZ
1585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1586 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1588}
1589
40e9cf64
JB
1590static void intel_init_dpio(struct drm_device *dev)
1591{
1592 struct drm_i915_private *dev_priv = dev->dev_private;
1593
1594 if (!IS_VALLEYVIEW(dev))
1595 return;
1596
a09caddd
CML
1597 /*
1598 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1599 * CHV x1 PHY (DP/HDMI D)
1600 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1601 */
1602 if (IS_CHERRYVIEW(dev)) {
1603 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1604 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1605 } else {
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1607 }
5382f5f3
JB
1608}
1609
d288f65f 1610static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1611 const struct intel_crtc_state *pipe_config)
87442f73 1612{
426115cf
DV
1613 struct drm_device *dev = crtc->base.dev;
1614 struct drm_i915_private *dev_priv = dev->dev_private;
1615 int reg = DPLL(crtc->pipe);
d288f65f 1616 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1617
426115cf 1618 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1619
1620 /* No really, not for ILK+ */
1621 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1622
1623 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1624 if (IS_MOBILE(dev_priv->dev))
426115cf 1625 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1626
426115cf
DV
1627 I915_WRITE(reg, dpll);
1628 POSTING_READ(reg);
1629 udelay(150);
1630
1631 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1632 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1633
d288f65f 1634 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1635 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1636
1637 /* We do this three times for luck */
426115cf 1638 I915_WRITE(reg, dpll);
87442f73
DV
1639 POSTING_READ(reg);
1640 udelay(150); /* wait for warmup */
426115cf 1641 I915_WRITE(reg, dpll);
87442f73
DV
1642 POSTING_READ(reg);
1643 udelay(150); /* wait for warmup */
426115cf 1644 I915_WRITE(reg, dpll);
87442f73
DV
1645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
1647}
1648
d288f65f 1649static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1650 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1651{
1652 struct drm_device *dev = crtc->base.dev;
1653 struct drm_i915_private *dev_priv = dev->dev_private;
1654 int pipe = crtc->pipe;
1655 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1656 u32 tmp;
1657
1658 assert_pipe_disabled(dev_priv, crtc->pipe);
1659
1660 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1661
1662 mutex_lock(&dev_priv->dpio_lock);
1663
1664 /* Enable back the 10bit clock to display controller */
1665 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1666 tmp |= DPIO_DCLKP_EN;
1667 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1668
1669 /*
1670 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1671 */
1672 udelay(1);
1673
1674 /* Enable PLL */
d288f65f 1675 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1676
1677 /* Check PLL is locked */
a11b0703 1678 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1679 DRM_ERROR("PLL %d failed to lock\n", pipe);
1680
a11b0703 1681 /* not sure when this should be written */
d288f65f 1682 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1683 POSTING_READ(DPLL_MD(pipe));
1684
9d556c99
CML
1685 mutex_unlock(&dev_priv->dpio_lock);
1686}
1687
1c4e0274
VS
1688static int intel_num_dvo_pipes(struct drm_device *dev)
1689{
1690 struct intel_crtc *crtc;
1691 int count = 0;
1692
1693 for_each_intel_crtc(dev, crtc)
1694 count += crtc->active &&
409ee761 1695 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1696
1697 return count;
1698}
1699
66e3d5c0 1700static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1701{
66e3d5c0
DV
1702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 int reg = DPLL(crtc->pipe);
6e3c9717 1705 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1706
66e3d5c0 1707 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1708
63d7bbe9 1709 /* No really, not for ILK+ */
3d13ef2e 1710 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1711
1712 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1713 if (IS_MOBILE(dev) && !IS_I830(dev))
1714 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1715
1c4e0274
VS
1716 /* Enable DVO 2x clock on both PLLs if necessary */
1717 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1718 /*
1719 * It appears to be important that we don't enable this
1720 * for the current pipe before otherwise configuring the
1721 * PLL. No idea how this should be handled if multiple
1722 * DVO outputs are enabled simultaneosly.
1723 */
1724 dpll |= DPLL_DVO_2X_MODE;
1725 I915_WRITE(DPLL(!crtc->pipe),
1726 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1727 }
66e3d5c0
DV
1728
1729 /* Wait for the clocks to stabilize. */
1730 POSTING_READ(reg);
1731 udelay(150);
1732
1733 if (INTEL_INFO(dev)->gen >= 4) {
1734 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1735 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1736 } else {
1737 /* The pixel multiplier can only be updated once the
1738 * DPLL is enabled and the clocks are stable.
1739 *
1740 * So write it again.
1741 */
1742 I915_WRITE(reg, dpll);
1743 }
63d7bbe9
JB
1744
1745 /* We do this three times for luck */
66e3d5c0 1746 I915_WRITE(reg, dpll);
63d7bbe9
JB
1747 POSTING_READ(reg);
1748 udelay(150); /* wait for warmup */
66e3d5c0 1749 I915_WRITE(reg, dpll);
63d7bbe9
JB
1750 POSTING_READ(reg);
1751 udelay(150); /* wait for warmup */
66e3d5c0 1752 I915_WRITE(reg, dpll);
63d7bbe9
JB
1753 POSTING_READ(reg);
1754 udelay(150); /* wait for warmup */
1755}
1756
1757/**
50b44a44 1758 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1759 * @dev_priv: i915 private structure
1760 * @pipe: pipe PLL to disable
1761 *
1762 * Disable the PLL for @pipe, making sure the pipe is off first.
1763 *
1764 * Note! This is for pre-ILK only.
1765 */
1c4e0274 1766static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1767{
1c4e0274
VS
1768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 enum pipe pipe = crtc->pipe;
1771
1772 /* Disable DVO 2x clock on both PLLs if necessary */
1773 if (IS_I830(dev) &&
409ee761 1774 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1775 intel_num_dvo_pipes(dev) == 1) {
1776 I915_WRITE(DPLL(PIPE_B),
1777 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1778 I915_WRITE(DPLL(PIPE_A),
1779 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1780 }
1781
b6b5d049
VS
1782 /* Don't disable pipe or pipe PLLs if needed */
1783 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1784 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1785 return;
1786
1787 /* Make sure the pipe isn't still relying on us */
1788 assert_pipe_disabled(dev_priv, pipe);
1789
50b44a44
DV
1790 I915_WRITE(DPLL(pipe), 0);
1791 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1792}
1793
f6071166
JB
1794static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1795{
1796 u32 val = 0;
1797
1798 /* Make sure the pipe isn't still relying on us */
1799 assert_pipe_disabled(dev_priv, pipe);
1800
e5cbfbfb
ID
1801 /*
1802 * Leave integrated clock source and reference clock enabled for pipe B.
1803 * The latter is needed for VGA hotplug / manual detection.
1804 */
f6071166 1805 if (pipe == PIPE_B)
e5cbfbfb 1806 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1807 I915_WRITE(DPLL(pipe), val);
1808 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1809
1810}
1811
1812static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1813{
d752048d 1814 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1815 u32 val;
1816
a11b0703
VS
1817 /* Make sure the pipe isn't still relying on us */
1818 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1819
a11b0703 1820 /* Set PLL en = 0 */
d17ec4ce 1821 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1822 if (pipe != PIPE_A)
1823 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1824 I915_WRITE(DPLL(pipe), val);
1825 POSTING_READ(DPLL(pipe));
d752048d
VS
1826
1827 mutex_lock(&dev_priv->dpio_lock);
1828
1829 /* Disable 10bit clock to display controller */
1830 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1831 val &= ~DPIO_DCLKP_EN;
1832 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1833
61407f6d
VS
1834 /* disable left/right clock distribution */
1835 if (pipe != PIPE_B) {
1836 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1837 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1838 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1839 } else {
1840 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1841 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1842 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1843 }
1844
d752048d 1845 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1846}
1847
e4607fcf
CML
1848void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1849 struct intel_digital_port *dport)
89b667f8
JB
1850{
1851 u32 port_mask;
00fc31b7 1852 int dpll_reg;
89b667f8 1853
e4607fcf
CML
1854 switch (dport->port) {
1855 case PORT_B:
89b667f8 1856 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1857 dpll_reg = DPLL(0);
e4607fcf
CML
1858 break;
1859 case PORT_C:
89b667f8 1860 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1861 dpll_reg = DPLL(0);
1862 break;
1863 case PORT_D:
1864 port_mask = DPLL_PORTD_READY_MASK;
1865 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1866 break;
1867 default:
1868 BUG();
1869 }
89b667f8 1870
00fc31b7 1871 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1872 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1873 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1874}
1875
b14b1055
DV
1876static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1877{
1878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1881
be19f0ff
CW
1882 if (WARN_ON(pll == NULL))
1883 return;
1884
3e369b76 1885 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1886 if (pll->active == 0) {
1887 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1888 WARN_ON(pll->on);
1889 assert_shared_dpll_disabled(dev_priv, pll);
1890
1891 pll->mode_set(dev_priv, pll);
1892 }
1893}
1894
92f2584a 1895/**
85b3894f 1896 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1897 * @dev_priv: i915 private structure
1898 * @pipe: pipe PLL to enable
1899 *
1900 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1901 * drives the transcoder clock.
1902 */
85b3894f 1903static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1904{
3d13ef2e
DL
1905 struct drm_device *dev = crtc->base.dev;
1906 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1907 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1908
87a875bb 1909 if (WARN_ON(pll == NULL))
48da64a8
CW
1910 return;
1911
3e369b76 1912 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1913 return;
ee7b9f93 1914
74dd6928 1915 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1916 pll->name, pll->active, pll->on,
e2b78267 1917 crtc->base.base.id);
92f2584a 1918
cdbd2316
DV
1919 if (pll->active++) {
1920 WARN_ON(!pll->on);
e9d6944e 1921 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1922 return;
1923 }
f4a091c7 1924 WARN_ON(pll->on);
ee7b9f93 1925
bd2bb1b9
PZ
1926 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1927
46edb027 1928 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1929 pll->enable(dev_priv, pll);
ee7b9f93 1930 pll->on = true;
92f2584a
JB
1931}
1932
f6daaec2 1933static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1934{
3d13ef2e
DL
1935 struct drm_device *dev = crtc->base.dev;
1936 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1937 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1938
92f2584a 1939 /* PCH only available on ILK+ */
3d13ef2e 1940 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1941 if (WARN_ON(pll == NULL))
ee7b9f93 1942 return;
92f2584a 1943
3e369b76 1944 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1945 return;
7a419866 1946
46edb027
DV
1947 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1948 pll->name, pll->active, pll->on,
e2b78267 1949 crtc->base.base.id);
7a419866 1950
48da64a8 1951 if (WARN_ON(pll->active == 0)) {
e9d6944e 1952 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1953 return;
1954 }
1955
e9d6944e 1956 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1957 WARN_ON(!pll->on);
cdbd2316 1958 if (--pll->active)
7a419866 1959 return;
ee7b9f93 1960
46edb027 1961 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1962 pll->disable(dev_priv, pll);
ee7b9f93 1963 pll->on = false;
bd2bb1b9
PZ
1964
1965 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1966}
1967
b8a4f404
PZ
1968static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1969 enum pipe pipe)
040484af 1970{
23670b32 1971 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1972 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1974 uint32_t reg, val, pipeconf_val;
040484af
JB
1975
1976 /* PCH only available on ILK+ */
55522f37 1977 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1978
1979 /* Make sure PCH DPLL is enabled */
e72f9fbf 1980 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1981 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1982
1983 /* FDI must be feeding us bits for PCH ports */
1984 assert_fdi_tx_enabled(dev_priv, pipe);
1985 assert_fdi_rx_enabled(dev_priv, pipe);
1986
23670b32
DV
1987 if (HAS_PCH_CPT(dev)) {
1988 /* Workaround: Set the timing override bit before enabling the
1989 * pch transcoder. */
1990 reg = TRANS_CHICKEN2(pipe);
1991 val = I915_READ(reg);
1992 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1993 I915_WRITE(reg, val);
59c859d6 1994 }
23670b32 1995
ab9412ba 1996 reg = PCH_TRANSCONF(pipe);
040484af 1997 val = I915_READ(reg);
5f7f726d 1998 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1999
2000 if (HAS_PCH_IBX(dev_priv->dev)) {
2001 /*
2002 * make the BPC in transcoder be consistent with
2003 * that in pipeconf reg.
2004 */
dfd07d72
DV
2005 val &= ~PIPECONF_BPC_MASK;
2006 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2007 }
5f7f726d
PZ
2008
2009 val &= ~TRANS_INTERLACE_MASK;
2010 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2011 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2012 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2013 val |= TRANS_LEGACY_INTERLACED_ILK;
2014 else
2015 val |= TRANS_INTERLACED;
5f7f726d
PZ
2016 else
2017 val |= TRANS_PROGRESSIVE;
2018
040484af
JB
2019 I915_WRITE(reg, val | TRANS_ENABLE);
2020 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2021 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2022}
2023
8fb033d7 2024static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2025 enum transcoder cpu_transcoder)
040484af 2026{
8fb033d7 2027 u32 val, pipeconf_val;
8fb033d7
PZ
2028
2029 /* PCH only available on ILK+ */
55522f37 2030 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2031
8fb033d7 2032 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2033 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2034 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2035
223a6fdf
PZ
2036 /* Workaround: set timing override bit. */
2037 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2038 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2039 I915_WRITE(_TRANSA_CHICKEN2, val);
2040
25f3ef11 2041 val = TRANS_ENABLE;
937bb610 2042 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2043
9a76b1c6
PZ
2044 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2045 PIPECONF_INTERLACED_ILK)
a35f2679 2046 val |= TRANS_INTERLACED;
8fb033d7
PZ
2047 else
2048 val |= TRANS_PROGRESSIVE;
2049
ab9412ba
DV
2050 I915_WRITE(LPT_TRANSCONF, val);
2051 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2052 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2053}
2054
b8a4f404
PZ
2055static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2056 enum pipe pipe)
040484af 2057{
23670b32
DV
2058 struct drm_device *dev = dev_priv->dev;
2059 uint32_t reg, val;
040484af
JB
2060
2061 /* FDI relies on the transcoder */
2062 assert_fdi_tx_disabled(dev_priv, pipe);
2063 assert_fdi_rx_disabled(dev_priv, pipe);
2064
291906f1
JB
2065 /* Ports must be off as well */
2066 assert_pch_ports_disabled(dev_priv, pipe);
2067
ab9412ba 2068 reg = PCH_TRANSCONF(pipe);
040484af
JB
2069 val = I915_READ(reg);
2070 val &= ~TRANS_ENABLE;
2071 I915_WRITE(reg, val);
2072 /* wait for PCH transcoder off, transcoder state */
2073 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2074 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2075
2076 if (!HAS_PCH_IBX(dev)) {
2077 /* Workaround: Clear the timing override chicken bit again. */
2078 reg = TRANS_CHICKEN2(pipe);
2079 val = I915_READ(reg);
2080 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2081 I915_WRITE(reg, val);
2082 }
040484af
JB
2083}
2084
ab4d966c 2085static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2086{
8fb033d7
PZ
2087 u32 val;
2088
ab9412ba 2089 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2090 val &= ~TRANS_ENABLE;
ab9412ba 2091 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2092 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2093 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2094 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2095
2096 /* Workaround: clear timing override bit. */
2097 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2098 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2099 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2100}
2101
b24e7179 2102/**
309cfea8 2103 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2104 * @crtc: crtc responsible for the pipe
b24e7179 2105 *
0372264a 2106 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2107 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2108 */
e1fdc473 2109static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2110{
0372264a
PZ
2111 struct drm_device *dev = crtc->base.dev;
2112 struct drm_i915_private *dev_priv = dev->dev_private;
2113 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2114 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2115 pipe);
1a240d4d 2116 enum pipe pch_transcoder;
b24e7179
JB
2117 int reg;
2118 u32 val;
2119
58c6eaa2 2120 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2121 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2122 assert_sprites_disabled(dev_priv, pipe);
2123
681e5811 2124 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2125 pch_transcoder = TRANSCODER_A;
2126 else
2127 pch_transcoder = pipe;
2128
b24e7179
JB
2129 /*
2130 * A pipe without a PLL won't actually be able to drive bits from
2131 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2132 * need the check.
2133 */
50360403 2134 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2135 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2136 assert_dsi_pll_enabled(dev_priv);
2137 else
2138 assert_pll_enabled(dev_priv, pipe);
040484af 2139 else {
6e3c9717 2140 if (crtc->config->has_pch_encoder) {
040484af 2141 /* if driving the PCH, we need FDI enabled */
cc391bbb 2142 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2143 assert_fdi_tx_pll_enabled(dev_priv,
2144 (enum pipe) cpu_transcoder);
040484af
JB
2145 }
2146 /* FIXME: assert CPU port conditions for SNB+ */
2147 }
b24e7179 2148
702e7a56 2149 reg = PIPECONF(cpu_transcoder);
b24e7179 2150 val = I915_READ(reg);
7ad25d48 2151 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2152 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2153 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2154 return;
7ad25d48 2155 }
00d70b15
CW
2156
2157 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2158 POSTING_READ(reg);
b24e7179
JB
2159}
2160
2161/**
309cfea8 2162 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2163 * @crtc: crtc whose pipes is to be disabled
b24e7179 2164 *
575f7ab7
VS
2165 * Disable the pipe of @crtc, making sure that various hardware
2166 * specific requirements are met, if applicable, e.g. plane
2167 * disabled, panel fitter off, etc.
b24e7179
JB
2168 *
2169 * Will wait until the pipe has shut down before returning.
2170 */
575f7ab7 2171static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2172{
575f7ab7 2173 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2174 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2175 enum pipe pipe = crtc->pipe;
b24e7179
JB
2176 int reg;
2177 u32 val;
2178
2179 /*
2180 * Make sure planes won't keep trying to pump pixels to us,
2181 * or we might hang the display.
2182 */
2183 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2184 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2185 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2186
702e7a56 2187 reg = PIPECONF(cpu_transcoder);
b24e7179 2188 val = I915_READ(reg);
00d70b15
CW
2189 if ((val & PIPECONF_ENABLE) == 0)
2190 return;
2191
67adc644
VS
2192 /*
2193 * Double wide has implications for planes
2194 * so best keep it disabled when not needed.
2195 */
6e3c9717 2196 if (crtc->config->double_wide)
67adc644
VS
2197 val &= ~PIPECONF_DOUBLE_WIDE;
2198
2199 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2200 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2201 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2202 val &= ~PIPECONF_ENABLE;
2203
2204 I915_WRITE(reg, val);
2205 if ((val & PIPECONF_ENABLE) == 0)
2206 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2207}
2208
d74362c9
KP
2209/*
2210 * Plane regs are double buffered, going from enabled->disabled needs a
2211 * trigger in order to latch. The display address reg provides this.
2212 */
1dba99f4
VS
2213void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2214 enum plane plane)
d74362c9 2215{
3d13ef2e
DL
2216 struct drm_device *dev = dev_priv->dev;
2217 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2218
2219 I915_WRITE(reg, I915_READ(reg));
2220 POSTING_READ(reg);
d74362c9
KP
2221}
2222
b24e7179 2223/**
262ca2b0 2224 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2225 * @plane: plane to be enabled
2226 * @crtc: crtc for the plane
b24e7179 2227 *
fdd508a6 2228 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2229 */
fdd508a6
VS
2230static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2231 struct drm_crtc *crtc)
b24e7179 2232{
fdd508a6
VS
2233 struct drm_device *dev = plane->dev;
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2236
2237 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2238 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2239
98ec7739
VS
2240 if (intel_crtc->primary_enabled)
2241 return;
0037f71c 2242
4c445e0e 2243 intel_crtc->primary_enabled = true;
939c2fe8 2244
fdd508a6
VS
2245 dev_priv->display.update_primary_plane(crtc, plane->fb,
2246 crtc->x, crtc->y);
33c3b0d1
VS
2247
2248 /*
2249 * BDW signals flip done immediately if the plane
2250 * is disabled, even if the plane enable is already
2251 * armed to occur at the next vblank :(
2252 */
2253 if (IS_BROADWELL(dev))
2254 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2255}
2256
b24e7179 2257/**
262ca2b0 2258 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2259 * @plane: plane to be disabled
2260 * @crtc: crtc for the plane
b24e7179 2261 *
fdd508a6 2262 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2263 */
fdd508a6
VS
2264static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2265 struct drm_crtc *crtc)
b24e7179 2266{
fdd508a6
VS
2267 struct drm_device *dev = plane->dev;
2268 struct drm_i915_private *dev_priv = dev->dev_private;
2269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2270
32b7eeec
MR
2271 if (WARN_ON(!intel_crtc->active))
2272 return;
b24e7179 2273
98ec7739
VS
2274 if (!intel_crtc->primary_enabled)
2275 return;
0037f71c 2276
4c445e0e 2277 intel_crtc->primary_enabled = false;
939c2fe8 2278
fdd508a6
VS
2279 dev_priv->display.update_primary_plane(crtc, plane->fb,
2280 crtc->x, crtc->y);
b24e7179
JB
2281}
2282
693db184
CW
2283static bool need_vtd_wa(struct drm_device *dev)
2284{
2285#ifdef CONFIG_INTEL_IOMMU
2286 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2287 return true;
2288#endif
2289 return false;
2290}
2291
50470bb0 2292unsigned int
6761dd31
TU
2293intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2294 uint64_t fb_format_modifier)
a57ce0b2 2295{
6761dd31
TU
2296 unsigned int tile_height;
2297 uint32_t pixel_bytes;
a57ce0b2 2298
b5d0e9bf
DL
2299 switch (fb_format_modifier) {
2300 case DRM_FORMAT_MOD_NONE:
2301 tile_height = 1;
2302 break;
2303 case I915_FORMAT_MOD_X_TILED:
2304 tile_height = IS_GEN2(dev) ? 16 : 8;
2305 break;
2306 case I915_FORMAT_MOD_Y_TILED:
2307 tile_height = 32;
2308 break;
2309 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2310 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2311 switch (pixel_bytes) {
b5d0e9bf 2312 default:
6761dd31 2313 case 1:
b5d0e9bf
DL
2314 tile_height = 64;
2315 break;
6761dd31
TU
2316 case 2:
2317 case 4:
b5d0e9bf
DL
2318 tile_height = 32;
2319 break;
6761dd31 2320 case 8:
b5d0e9bf
DL
2321 tile_height = 16;
2322 break;
6761dd31 2323 case 16:
b5d0e9bf
DL
2324 WARN_ONCE(1,
2325 "128-bit pixels are not supported for display!");
2326 tile_height = 16;
2327 break;
2328 }
2329 break;
2330 default:
2331 MISSING_CASE(fb_format_modifier);
2332 tile_height = 1;
2333 break;
2334 }
091df6cb 2335
6761dd31
TU
2336 return tile_height;
2337}
2338
2339unsigned int
2340intel_fb_align_height(struct drm_device *dev, unsigned int height,
2341 uint32_t pixel_format, uint64_t fb_format_modifier)
2342{
2343 return ALIGN(height, intel_tile_height(dev, pixel_format,
2344 fb_format_modifier));
a57ce0b2
JB
2345}
2346
f64b98cd
TU
2347static int
2348intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2349 const struct drm_plane_state *plane_state)
2350{
50470bb0 2351 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2352
f64b98cd
TU
2353 *view = i915_ggtt_view_normal;
2354
50470bb0
TU
2355 if (!plane_state)
2356 return 0;
2357
121920fa 2358 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2359 return 0;
2360
9abc4648 2361 *view = i915_ggtt_view_rotated;
50470bb0
TU
2362
2363 info->height = fb->height;
2364 info->pixel_format = fb->pixel_format;
2365 info->pitch = fb->pitches[0];
2366 info->fb_modifier = fb->modifier[0];
2367
f64b98cd
TU
2368 return 0;
2369}
2370
127bd2ac 2371int
850c4cdc
TU
2372intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2373 struct drm_framebuffer *fb,
82bc3b2d 2374 const struct drm_plane_state *plane_state,
a4872ba6 2375 struct intel_engine_cs *pipelined)
6b95a207 2376{
850c4cdc 2377 struct drm_device *dev = fb->dev;
ce453d81 2378 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2379 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2380 struct i915_ggtt_view view;
6b95a207
KH
2381 u32 alignment;
2382 int ret;
2383
ebcdd39e
MR
2384 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2385
7b911adc
TU
2386 switch (fb->modifier[0]) {
2387 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2388 if (INTEL_INFO(dev)->gen >= 9)
2389 alignment = 256 * 1024;
2390 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2391 alignment = 128 * 1024;
a6c45cf0 2392 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2393 alignment = 4 * 1024;
2394 else
2395 alignment = 64 * 1024;
6b95a207 2396 break;
7b911adc 2397 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2398 if (INTEL_INFO(dev)->gen >= 9)
2399 alignment = 256 * 1024;
2400 else {
2401 /* pin() will align the object as required by fence */
2402 alignment = 0;
2403 }
6b95a207 2404 break;
7b911adc 2405 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2406 case I915_FORMAT_MOD_Yf_TILED:
2407 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2408 "Y tiling bo slipped through, driver bug!\n"))
2409 return -EINVAL;
2410 alignment = 1 * 1024 * 1024;
2411 break;
6b95a207 2412 default:
7b911adc
TU
2413 MISSING_CASE(fb->modifier[0]);
2414 return -EINVAL;
6b95a207
KH
2415 }
2416
f64b98cd
TU
2417 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2418 if (ret)
2419 return ret;
2420
693db184
CW
2421 /* Note that the w/a also requires 64 PTE of padding following the
2422 * bo. We currently fill all unused PTE with the shadow page and so
2423 * we should always have valid PTE following the scanout preventing
2424 * the VT-d warning.
2425 */
2426 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2427 alignment = 256 * 1024;
2428
d6dd6843
PZ
2429 /*
2430 * Global gtt pte registers are special registers which actually forward
2431 * writes to a chunk of system memory. Which means that there is no risk
2432 * that the register values disappear as soon as we call
2433 * intel_runtime_pm_put(), so it is correct to wrap only the
2434 * pin/unpin/fence and not more.
2435 */
2436 intel_runtime_pm_get(dev_priv);
2437
ce453d81 2438 dev_priv->mm.interruptible = false;
e6617330 2439 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2440 &view);
48b956c5 2441 if (ret)
ce453d81 2442 goto err_interruptible;
6b95a207
KH
2443
2444 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2445 * fence, whereas 965+ only requires a fence if using
2446 * framebuffer compression. For simplicity, we always install
2447 * a fence as the cost is not that onerous.
2448 */
06d98131 2449 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2450 if (ret)
2451 goto err_unpin;
1690e1eb 2452
9a5a53b3 2453 i915_gem_object_pin_fence(obj);
6b95a207 2454
ce453d81 2455 dev_priv->mm.interruptible = true;
d6dd6843 2456 intel_runtime_pm_put(dev_priv);
6b95a207 2457 return 0;
48b956c5
CW
2458
2459err_unpin:
f64b98cd 2460 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2461err_interruptible:
2462 dev_priv->mm.interruptible = true;
d6dd6843 2463 intel_runtime_pm_put(dev_priv);
48b956c5 2464 return ret;
6b95a207
KH
2465}
2466
82bc3b2d
TU
2467static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2468 const struct drm_plane_state *plane_state)
1690e1eb 2469{
82bc3b2d 2470 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2471 struct i915_ggtt_view view;
2472 int ret;
82bc3b2d 2473
ebcdd39e
MR
2474 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2475
f64b98cd
TU
2476 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2477 WARN_ONCE(ret, "Couldn't get view from plane state!");
2478
1690e1eb 2479 i915_gem_object_unpin_fence(obj);
f64b98cd 2480 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2481}
2482
c2c75131
DV
2483/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2484 * is assumed to be a power-of-two. */
bc752862
CW
2485unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2486 unsigned int tiling_mode,
2487 unsigned int cpp,
2488 unsigned int pitch)
c2c75131 2489{
bc752862
CW
2490 if (tiling_mode != I915_TILING_NONE) {
2491 unsigned int tile_rows, tiles;
c2c75131 2492
bc752862
CW
2493 tile_rows = *y / 8;
2494 *y %= 8;
c2c75131 2495
bc752862
CW
2496 tiles = *x / (512/cpp);
2497 *x %= 512/cpp;
2498
2499 return tile_rows * pitch * 8 + tiles * 4096;
2500 } else {
2501 unsigned int offset;
2502
2503 offset = *y * pitch + *x * cpp;
2504 *y = 0;
2505 *x = (offset & 4095) / cpp;
2506 return offset & -4096;
2507 }
c2c75131
DV
2508}
2509
b35d63fa 2510static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2511{
2512 switch (format) {
2513 case DISPPLANE_8BPP:
2514 return DRM_FORMAT_C8;
2515 case DISPPLANE_BGRX555:
2516 return DRM_FORMAT_XRGB1555;
2517 case DISPPLANE_BGRX565:
2518 return DRM_FORMAT_RGB565;
2519 default:
2520 case DISPPLANE_BGRX888:
2521 return DRM_FORMAT_XRGB8888;
2522 case DISPPLANE_RGBX888:
2523 return DRM_FORMAT_XBGR8888;
2524 case DISPPLANE_BGRX101010:
2525 return DRM_FORMAT_XRGB2101010;
2526 case DISPPLANE_RGBX101010:
2527 return DRM_FORMAT_XBGR2101010;
2528 }
2529}
2530
bc8d7dff
DL
2531static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2532{
2533 switch (format) {
2534 case PLANE_CTL_FORMAT_RGB_565:
2535 return DRM_FORMAT_RGB565;
2536 default:
2537 case PLANE_CTL_FORMAT_XRGB_8888:
2538 if (rgb_order) {
2539 if (alpha)
2540 return DRM_FORMAT_ABGR8888;
2541 else
2542 return DRM_FORMAT_XBGR8888;
2543 } else {
2544 if (alpha)
2545 return DRM_FORMAT_ARGB8888;
2546 else
2547 return DRM_FORMAT_XRGB8888;
2548 }
2549 case PLANE_CTL_FORMAT_XRGB_2101010:
2550 if (rgb_order)
2551 return DRM_FORMAT_XBGR2101010;
2552 else
2553 return DRM_FORMAT_XRGB2101010;
2554 }
2555}
2556
5724dbd1 2557static bool
f6936e29
DV
2558intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2559 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2560{
2561 struct drm_device *dev = crtc->base.dev;
2562 struct drm_i915_gem_object *obj = NULL;
2563 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2564 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2565 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2566 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2567 PAGE_SIZE);
2568
2569 size_aligned -= base_aligned;
46f297fb 2570
ff2652ea
CW
2571 if (plane_config->size == 0)
2572 return false;
2573
f37b5c2b
DV
2574 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2575 base_aligned,
2576 base_aligned,
2577 size_aligned);
46f297fb 2578 if (!obj)
484b41dd 2579 return false;
46f297fb 2580
49af449b
DL
2581 obj->tiling_mode = plane_config->tiling;
2582 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2583 obj->stride = fb->pitches[0];
46f297fb 2584
6bf129df
DL
2585 mode_cmd.pixel_format = fb->pixel_format;
2586 mode_cmd.width = fb->width;
2587 mode_cmd.height = fb->height;
2588 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2589 mode_cmd.modifier[0] = fb->modifier[0];
2590 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2591
2592 mutex_lock(&dev->struct_mutex);
6bf129df 2593 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2594 &mode_cmd, obj)) {
46f297fb
JB
2595 DRM_DEBUG_KMS("intel fb init failed\n");
2596 goto out_unref_obj;
2597 }
46f297fb 2598 mutex_unlock(&dev->struct_mutex);
484b41dd 2599
f6936e29 2600 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2601 return true;
46f297fb
JB
2602
2603out_unref_obj:
2604 drm_gem_object_unreference(&obj->base);
2605 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2606 return false;
2607}
2608
afd65eb4
MR
2609/* Update plane->state->fb to match plane->fb after driver-internal updates */
2610static void
2611update_state_fb(struct drm_plane *plane)
2612{
2613 if (plane->fb == plane->state->fb)
2614 return;
2615
2616 if (plane->state->fb)
2617 drm_framebuffer_unreference(plane->state->fb);
2618 plane->state->fb = plane->fb;
2619 if (plane->state->fb)
2620 drm_framebuffer_reference(plane->state->fb);
2621}
2622
5724dbd1 2623static void
f6936e29
DV
2624intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2625 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2626{
2627 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2628 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2629 struct drm_crtc *c;
2630 struct intel_crtc *i;
2ff8fde1 2631 struct drm_i915_gem_object *obj;
88595ac9
DV
2632 struct drm_plane *primary = intel_crtc->base.primary;
2633 struct drm_framebuffer *fb;
484b41dd 2634
2d14030b 2635 if (!plane_config->fb)
484b41dd
JB
2636 return;
2637
f6936e29 2638 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2639 fb = &plane_config->fb->base;
2640 goto valid_fb;
f55548b5 2641 }
484b41dd 2642
2d14030b 2643 kfree(plane_config->fb);
484b41dd
JB
2644
2645 /*
2646 * Failed to alloc the obj, check to see if we should share
2647 * an fb with another CRTC instead
2648 */
70e1e0ec 2649 for_each_crtc(dev, c) {
484b41dd
JB
2650 i = to_intel_crtc(c);
2651
2652 if (c == &intel_crtc->base)
2653 continue;
2654
2ff8fde1
MR
2655 if (!i->active)
2656 continue;
2657
88595ac9
DV
2658 fb = c->primary->fb;
2659 if (!fb)
484b41dd
JB
2660 continue;
2661
88595ac9 2662 obj = intel_fb_obj(fb);
2ff8fde1 2663 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2664 drm_framebuffer_reference(fb);
2665 goto valid_fb;
484b41dd
JB
2666 }
2667 }
88595ac9
DV
2668
2669 return;
2670
2671valid_fb:
2672 obj = intel_fb_obj(fb);
2673 if (obj->tiling_mode != I915_TILING_NONE)
2674 dev_priv->preserve_bios_swizzle = true;
2675
2676 primary->fb = fb;
2677 primary->state->crtc = &intel_crtc->base;
2678 primary->crtc = &intel_crtc->base;
2679 update_state_fb(primary);
2680 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2681}
2682
29b9bde6
DV
2683static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2684 struct drm_framebuffer *fb,
2685 int x, int y)
81255565
JB
2686{
2687 struct drm_device *dev = crtc->dev;
2688 struct drm_i915_private *dev_priv = dev->dev_private;
2689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2690 struct drm_i915_gem_object *obj;
81255565 2691 int plane = intel_crtc->plane;
e506a0c6 2692 unsigned long linear_offset;
81255565 2693 u32 dspcntr;
f45651ba 2694 u32 reg = DSPCNTR(plane);
48404c1e 2695 int pixel_size;
f45651ba 2696
fdd508a6
VS
2697 if (!intel_crtc->primary_enabled) {
2698 I915_WRITE(reg, 0);
2699 if (INTEL_INFO(dev)->gen >= 4)
2700 I915_WRITE(DSPSURF(plane), 0);
2701 else
2702 I915_WRITE(DSPADDR(plane), 0);
2703 POSTING_READ(reg);
2704 return;
2705 }
2706
c9ba6fad
VS
2707 obj = intel_fb_obj(fb);
2708 if (WARN_ON(obj == NULL))
2709 return;
2710
2711 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2712
f45651ba
VS
2713 dspcntr = DISPPLANE_GAMMA_ENABLE;
2714
fdd508a6 2715 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2716
2717 if (INTEL_INFO(dev)->gen < 4) {
2718 if (intel_crtc->pipe == PIPE_B)
2719 dspcntr |= DISPPLANE_SEL_PIPE_B;
2720
2721 /* pipesrc and dspsize control the size that is scaled from,
2722 * which should always be the user's requested size.
2723 */
2724 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2725 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2726 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2727 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2728 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2729 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2730 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2731 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2732 I915_WRITE(PRIMPOS(plane), 0);
2733 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2734 }
81255565 2735
57779d06
VS
2736 switch (fb->pixel_format) {
2737 case DRM_FORMAT_C8:
81255565
JB
2738 dspcntr |= DISPPLANE_8BPP;
2739 break;
57779d06
VS
2740 case DRM_FORMAT_XRGB1555:
2741 case DRM_FORMAT_ARGB1555:
2742 dspcntr |= DISPPLANE_BGRX555;
81255565 2743 break;
57779d06
VS
2744 case DRM_FORMAT_RGB565:
2745 dspcntr |= DISPPLANE_BGRX565;
2746 break;
2747 case DRM_FORMAT_XRGB8888:
2748 case DRM_FORMAT_ARGB8888:
2749 dspcntr |= DISPPLANE_BGRX888;
2750 break;
2751 case DRM_FORMAT_XBGR8888:
2752 case DRM_FORMAT_ABGR8888:
2753 dspcntr |= DISPPLANE_RGBX888;
2754 break;
2755 case DRM_FORMAT_XRGB2101010:
2756 case DRM_FORMAT_ARGB2101010:
2757 dspcntr |= DISPPLANE_BGRX101010;
2758 break;
2759 case DRM_FORMAT_XBGR2101010:
2760 case DRM_FORMAT_ABGR2101010:
2761 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2762 break;
2763 default:
baba133a 2764 BUG();
81255565 2765 }
57779d06 2766
f45651ba
VS
2767 if (INTEL_INFO(dev)->gen >= 4 &&
2768 obj->tiling_mode != I915_TILING_NONE)
2769 dspcntr |= DISPPLANE_TILED;
81255565 2770
de1aa629
VS
2771 if (IS_G4X(dev))
2772 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2773
b9897127 2774 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2775
c2c75131
DV
2776 if (INTEL_INFO(dev)->gen >= 4) {
2777 intel_crtc->dspaddr_offset =
bc752862 2778 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2779 pixel_size,
bc752862 2780 fb->pitches[0]);
c2c75131
DV
2781 linear_offset -= intel_crtc->dspaddr_offset;
2782 } else {
e506a0c6 2783 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2784 }
e506a0c6 2785
8e7d688b 2786 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2787 dspcntr |= DISPPLANE_ROTATE_180;
2788
6e3c9717
ACO
2789 x += (intel_crtc->config->pipe_src_w - 1);
2790 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2791
2792 /* Finding the last pixel of the last line of the display
2793 data and adding to linear_offset*/
2794 linear_offset +=
6e3c9717
ACO
2795 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2796 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2797 }
2798
2799 I915_WRITE(reg, dspcntr);
2800
01f2c773 2801 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2802 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2803 I915_WRITE(DSPSURF(plane),
2804 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2805 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2806 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2807 } else
f343c5f6 2808 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2809 POSTING_READ(reg);
17638cd6
JB
2810}
2811
29b9bde6
DV
2812static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2813 struct drm_framebuffer *fb,
2814 int x, int y)
17638cd6
JB
2815{
2816 struct drm_device *dev = crtc->dev;
2817 struct drm_i915_private *dev_priv = dev->dev_private;
2818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2819 struct drm_i915_gem_object *obj;
17638cd6 2820 int plane = intel_crtc->plane;
e506a0c6 2821 unsigned long linear_offset;
17638cd6 2822 u32 dspcntr;
f45651ba 2823 u32 reg = DSPCNTR(plane);
48404c1e 2824 int pixel_size;
f45651ba 2825
fdd508a6
VS
2826 if (!intel_crtc->primary_enabled) {
2827 I915_WRITE(reg, 0);
2828 I915_WRITE(DSPSURF(plane), 0);
2829 POSTING_READ(reg);
2830 return;
2831 }
2832
c9ba6fad
VS
2833 obj = intel_fb_obj(fb);
2834 if (WARN_ON(obj == NULL))
2835 return;
2836
2837 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2838
f45651ba
VS
2839 dspcntr = DISPPLANE_GAMMA_ENABLE;
2840
fdd508a6 2841 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2842
2843 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2844 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2845
57779d06
VS
2846 switch (fb->pixel_format) {
2847 case DRM_FORMAT_C8:
17638cd6
JB
2848 dspcntr |= DISPPLANE_8BPP;
2849 break;
57779d06
VS
2850 case DRM_FORMAT_RGB565:
2851 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2852 break;
57779d06
VS
2853 case DRM_FORMAT_XRGB8888:
2854 case DRM_FORMAT_ARGB8888:
2855 dspcntr |= DISPPLANE_BGRX888;
2856 break;
2857 case DRM_FORMAT_XBGR8888:
2858 case DRM_FORMAT_ABGR8888:
2859 dspcntr |= DISPPLANE_RGBX888;
2860 break;
2861 case DRM_FORMAT_XRGB2101010:
2862 case DRM_FORMAT_ARGB2101010:
2863 dspcntr |= DISPPLANE_BGRX101010;
2864 break;
2865 case DRM_FORMAT_XBGR2101010:
2866 case DRM_FORMAT_ABGR2101010:
2867 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2868 break;
2869 default:
baba133a 2870 BUG();
17638cd6
JB
2871 }
2872
2873 if (obj->tiling_mode != I915_TILING_NONE)
2874 dspcntr |= DISPPLANE_TILED;
17638cd6 2875
f45651ba 2876 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2877 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2878
b9897127 2879 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2880 intel_crtc->dspaddr_offset =
bc752862 2881 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2882 pixel_size,
bc752862 2883 fb->pitches[0]);
c2c75131 2884 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2885 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2886 dspcntr |= DISPPLANE_ROTATE_180;
2887
2888 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2889 x += (intel_crtc->config->pipe_src_w - 1);
2890 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2891
2892 /* Finding the last pixel of the last line of the display
2893 data and adding to linear_offset*/
2894 linear_offset +=
6e3c9717
ACO
2895 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2896 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2897 }
2898 }
2899
2900 I915_WRITE(reg, dspcntr);
17638cd6 2901
01f2c773 2902 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2903 I915_WRITE(DSPSURF(plane),
2904 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2905 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2906 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2907 } else {
2908 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2909 I915_WRITE(DSPLINOFF(plane), linear_offset);
2910 }
17638cd6 2911 POSTING_READ(reg);
17638cd6
JB
2912}
2913
b321803d
DL
2914u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2915 uint32_t pixel_format)
2916{
2917 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2918
2919 /*
2920 * The stride is either expressed as a multiple of 64 bytes
2921 * chunks for linear buffers or in number of tiles for tiled
2922 * buffers.
2923 */
2924 switch (fb_modifier) {
2925 case DRM_FORMAT_MOD_NONE:
2926 return 64;
2927 case I915_FORMAT_MOD_X_TILED:
2928 if (INTEL_INFO(dev)->gen == 2)
2929 return 128;
2930 return 512;
2931 case I915_FORMAT_MOD_Y_TILED:
2932 /* No need to check for old gens and Y tiling since this is
2933 * about the display engine and those will be blocked before
2934 * we get here.
2935 */
2936 return 128;
2937 case I915_FORMAT_MOD_Yf_TILED:
2938 if (bits_per_pixel == 8)
2939 return 64;
2940 else
2941 return 128;
2942 default:
2943 MISSING_CASE(fb_modifier);
2944 return 64;
2945 }
2946}
2947
121920fa
TU
2948unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2949 struct drm_i915_gem_object *obj)
2950{
9abc4648 2951 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2952
2953 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2954 view = &i915_ggtt_view_rotated;
121920fa
TU
2955
2956 return i915_gem_obj_ggtt_offset_view(obj, view);
2957}
2958
a1b2278e
CK
2959/*
2960 * This function detaches (aka. unbinds) unused scalers in hardware
2961 */
2962void skl_detach_scalers(struct intel_crtc *intel_crtc)
2963{
2964 struct drm_device *dev;
2965 struct drm_i915_private *dev_priv;
2966 struct intel_crtc_scaler_state *scaler_state;
2967 int i;
2968
2969 if (!intel_crtc || !intel_crtc->config)
2970 return;
2971
2972 dev = intel_crtc->base.dev;
2973 dev_priv = dev->dev_private;
2974 scaler_state = &intel_crtc->config->scaler_state;
2975
2976 /* loop through and disable scalers that aren't in use */
2977 for (i = 0; i < intel_crtc->num_scalers; i++) {
2978 if (!scaler_state->scalers[i].in_use) {
2979 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2980 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2981 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2982 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2983 intel_crtc->base.base.id, intel_crtc->pipe, i);
2984 }
2985 }
2986}
2987
6156a456 2988u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2989{
6156a456
CK
2990 u32 plane_ctl_format = 0;
2991 switch (pixel_format) {
70d21f0e 2992 case DRM_FORMAT_RGB565:
6156a456 2993 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
f75fb42a 2994 break;
70d21f0e 2995 case DRM_FORMAT_XBGR8888:
6156a456
CK
2996 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2997 break;
2998 case DRM_FORMAT_XRGB8888:
2999 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
70d21f0e 3000 break;
6156a456
CK
3001 /*
3002 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3003 * to be already pre-multiplied. We need to add a knob (or a different
3004 * DRM_FORMAT) for user-space to configure that.
3005 */
f75fb42a 3006 case DRM_FORMAT_ABGR8888:
6156a456
CK
3007 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
3008 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3009 break;
3010 case DRM_FORMAT_ARGB8888:
3011 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
3012 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
f75fb42a 3013 break;
70d21f0e 3014 case DRM_FORMAT_XRGB2101010:
6156a456 3015 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e
DL
3016 break;
3017 case DRM_FORMAT_XBGR2101010:
6156a456
CK
3018 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
3019 break;
3020 case DRM_FORMAT_YUYV:
3021 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
3022 break;
3023 case DRM_FORMAT_YVYU:
3024 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
3025 break;
3026 case DRM_FORMAT_UYVY:
3027 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3028 break;
3029 case DRM_FORMAT_VYUY:
3030 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e
DL
3031 break;
3032 default:
3033 BUG();
3034 }
6156a456
CK
3035 return plane_ctl_format;
3036}
70d21f0e 3037
6156a456
CK
3038u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3039{
3040 u32 plane_ctl_tiling = 0;
3041 switch (fb_modifier) {
30af77c4 3042 case DRM_FORMAT_MOD_NONE:
70d21f0e 3043 break;
30af77c4 3044 case I915_FORMAT_MOD_X_TILED:
6156a456 3045 plane_ctl_tiling = PLANE_CTL_TILED_X;
b321803d
DL
3046 break;
3047 case I915_FORMAT_MOD_Y_TILED:
6156a456 3048 plane_ctl_tiling = PLANE_CTL_TILED_Y;
b321803d
DL
3049 break;
3050 case I915_FORMAT_MOD_Yf_TILED:
6156a456 3051 plane_ctl_tiling = PLANE_CTL_TILED_YF;
70d21f0e
DL
3052 break;
3053 default:
6156a456 3054 MISSING_CASE(fb_modifier);
70d21f0e 3055 }
6156a456
CK
3056 return plane_ctl_tiling;
3057}
70d21f0e 3058
6156a456
CK
3059u32 skl_plane_ctl_rotation(unsigned int rotation)
3060{
3061 u32 plane_ctl_rotation = 0;
3b7a5119 3062 switch (rotation) {
6156a456
CK
3063 case BIT(DRM_ROTATE_0):
3064 break;
3b7a5119 3065 case BIT(DRM_ROTATE_90):
6156a456 3066 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3b7a5119 3067 break;
3b7a5119 3068 case BIT(DRM_ROTATE_180):
6156a456 3069 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3b7a5119 3070 break;
3b7a5119 3071 case BIT(DRM_ROTATE_270):
6156a456 3072 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3b7a5119 3073 break;
6156a456
CK
3074 default:
3075 MISSING_CASE(rotation);
3076 }
3077
3078 return plane_ctl_rotation;
3079}
3080
3081static void skylake_update_primary_plane(struct drm_crtc *crtc,
3082 struct drm_framebuffer *fb,
3083 int x, int y)
3084{
3085 struct drm_device *dev = crtc->dev;
3086 struct drm_i915_private *dev_priv = dev->dev_private;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3088 struct drm_i915_gem_object *obj;
3089 int pipe = intel_crtc->pipe;
3090 u32 plane_ctl, stride_div, stride;
3091 u32 tile_height, plane_offset, plane_size;
3092 unsigned int rotation;
3093 int x_offset, y_offset;
3094 unsigned long surf_addr;
3095 struct drm_plane *plane;
3096 struct intel_crtc_state *crtc_state = intel_crtc->config;
3097 struct intel_plane_state *plane_state;
3098 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3099 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3100 int scaler_id = -1;
3101
3102 plane = crtc->primary;
3103 plane_state = to_intel_plane_state(plane->state);
3104
3105 if (!intel_crtc->primary_enabled) {
3106 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3107 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3108 POSTING_READ(PLANE_CTL(pipe, 0));
3109 return;
3b7a5119 3110 }
70d21f0e 3111
6156a456
CK
3112 plane_ctl = PLANE_CTL_ENABLE |
3113 PLANE_CTL_PIPE_GAMMA_ENABLE |
3114 PLANE_CTL_PIPE_CSC_ENABLE;
3115
3116 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3117 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3118 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3119
3120 rotation = plane->state->rotation;
3121 plane_ctl |= skl_plane_ctl_rotation(rotation);
3122
b321803d
DL
3123 obj = intel_fb_obj(fb);
3124 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3125 fb->pixel_format);
3b7a5119
SJ
3126 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3127
6156a456
CK
3128 /*
3129 * FIXME: intel_plane_state->src, dst aren't set when transitional
3130 * update_plane helpers are called from legacy paths.
3131 * Once full atomic crtc is available, below check can be avoided.
3132 */
3133 if (drm_rect_width(&plane_state->src)) {
3134 scaler_id = plane_state->scaler_id;
3135 src_x = plane_state->src.x1 >> 16;
3136 src_y = plane_state->src.y1 >> 16;
3137 src_w = drm_rect_width(&plane_state->src) >> 16;
3138 src_h = drm_rect_height(&plane_state->src) >> 16;
3139 dst_x = plane_state->dst.x1;
3140 dst_y = plane_state->dst.y1;
3141 dst_w = drm_rect_width(&plane_state->dst);
3142 dst_h = drm_rect_height(&plane_state->dst);
3143
3144 WARN_ON(x != src_x || y != src_y);
3145 } else {
3146 src_w = intel_crtc->config->pipe_src_w;
3147 src_h = intel_crtc->config->pipe_src_h;
3148 }
3149
3b7a5119
SJ
3150 if (intel_rotation_90_or_270(rotation)) {
3151 /* stride = Surface height in tiles */
3152 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3153 fb->modifier[0]);
3154 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3155 x_offset = stride * tile_height - y - src_h;
3b7a5119 3156 y_offset = x;
6156a456 3157 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3158 } else {
3159 stride = fb->pitches[0] / stride_div;
3160 x_offset = x;
3161 y_offset = y;
6156a456 3162 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3163 }
3164 plane_offset = y_offset << 16 | x_offset;
b321803d 3165
70d21f0e 3166 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3167 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3168 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3169 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3170
3171 if (scaler_id >= 0) {
3172 uint32_t ps_ctrl = 0;
3173
3174 WARN_ON(!dst_w || !dst_h);
3175 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3176 crtc_state->scaler_state.scalers[scaler_id].mode;
3177 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3178 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3179 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3180 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3181 I915_WRITE(PLANE_POS(pipe, 0), 0);
3182 } else {
3183 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3184 }
3185
121920fa 3186 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3187
3188 POSTING_READ(PLANE_SURF(pipe, 0));
3189}
3190
17638cd6
JB
3191/* Assume fb object is pinned & idle & fenced and just update base pointers */
3192static int
3193intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3194 int x, int y, enum mode_set_atomic state)
3195{
3196 struct drm_device *dev = crtc->dev;
3197 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3198
6b8e6ed0
CW
3199 if (dev_priv->display.disable_fbc)
3200 dev_priv->display.disable_fbc(dev);
81255565 3201
29b9bde6
DV
3202 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3203
3204 return 0;
81255565
JB
3205}
3206
7514747d 3207static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3208{
96a02917
VS
3209 struct drm_crtc *crtc;
3210
70e1e0ec 3211 for_each_crtc(dev, crtc) {
96a02917
VS
3212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3213 enum plane plane = intel_crtc->plane;
3214
3215 intel_prepare_page_flip(dev, plane);
3216 intel_finish_page_flip_plane(dev, plane);
3217 }
7514747d
VS
3218}
3219
3220static void intel_update_primary_planes(struct drm_device *dev)
3221{
3222 struct drm_i915_private *dev_priv = dev->dev_private;
3223 struct drm_crtc *crtc;
96a02917 3224
70e1e0ec 3225 for_each_crtc(dev, crtc) {
96a02917
VS
3226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227
51fd371b 3228 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3229 /*
3230 * FIXME: Once we have proper support for primary planes (and
3231 * disabling them without disabling the entire crtc) allow again
66e514c1 3232 * a NULL crtc->primary->fb.
947fdaad 3233 */
f4510a27 3234 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3235 dev_priv->display.update_primary_plane(crtc,
66e514c1 3236 crtc->primary->fb,
262ca2b0
MR
3237 crtc->x,
3238 crtc->y);
51fd371b 3239 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3240 }
3241}
3242
7514747d
VS
3243void intel_prepare_reset(struct drm_device *dev)
3244{
f98ce92f
VS
3245 struct drm_i915_private *dev_priv = to_i915(dev);
3246 struct intel_crtc *crtc;
3247
7514747d
VS
3248 /* no reset support for gen2 */
3249 if (IS_GEN2(dev))
3250 return;
3251
3252 /* reset doesn't touch the display */
3253 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3254 return;
3255
3256 drm_modeset_lock_all(dev);
f98ce92f
VS
3257
3258 /*
3259 * Disabling the crtcs gracefully seems nicer. Also the
3260 * g33 docs say we should at least disable all the planes.
3261 */
3262 for_each_intel_crtc(dev, crtc) {
3263 if (crtc->active)
3264 dev_priv->display.crtc_disable(&crtc->base);
3265 }
7514747d
VS
3266}
3267
3268void intel_finish_reset(struct drm_device *dev)
3269{
3270 struct drm_i915_private *dev_priv = to_i915(dev);
3271
3272 /*
3273 * Flips in the rings will be nuked by the reset,
3274 * so complete all pending flips so that user space
3275 * will get its events and not get stuck.
3276 */
3277 intel_complete_page_flips(dev);
3278
3279 /* no reset support for gen2 */
3280 if (IS_GEN2(dev))
3281 return;
3282
3283 /* reset doesn't touch the display */
3284 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3285 /*
3286 * Flips in the rings have been nuked by the reset,
3287 * so update the base address of all primary
3288 * planes to the the last fb to make sure we're
3289 * showing the correct fb after a reset.
3290 */
3291 intel_update_primary_planes(dev);
3292 return;
3293 }
3294
3295 /*
3296 * The display has been reset as well,
3297 * so need a full re-initialization.
3298 */
3299 intel_runtime_pm_disable_interrupts(dev_priv);
3300 intel_runtime_pm_enable_interrupts(dev_priv);
3301
3302 intel_modeset_init_hw(dev);
3303
3304 spin_lock_irq(&dev_priv->irq_lock);
3305 if (dev_priv->display.hpd_irq_setup)
3306 dev_priv->display.hpd_irq_setup(dev);
3307 spin_unlock_irq(&dev_priv->irq_lock);
3308
3309 intel_modeset_setup_hw_state(dev, true);
3310
3311 intel_hpd_init(dev_priv);
3312
3313 drm_modeset_unlock_all(dev);
3314}
3315
14667a4b
CW
3316static int
3317intel_finish_fb(struct drm_framebuffer *old_fb)
3318{
2ff8fde1 3319 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
3320 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3321 bool was_interruptible = dev_priv->mm.interruptible;
3322 int ret;
3323
14667a4b
CW
3324 /* Big Hammer, we also need to ensure that any pending
3325 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3326 * current scanout is retired before unpinning the old
3327 * framebuffer.
3328 *
3329 * This should only fail upon a hung GPU, in which case we
3330 * can safely continue.
3331 */
3332 dev_priv->mm.interruptible = false;
3333 ret = i915_gem_object_finish_gpu(obj);
3334 dev_priv->mm.interruptible = was_interruptible;
3335
3336 return ret;
3337}
3338
7d5e3799
CW
3339static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3340{
3341 struct drm_device *dev = crtc->dev;
3342 struct drm_i915_private *dev_priv = dev->dev_private;
3343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3344 bool pending;
3345
3346 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3347 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3348 return false;
3349
5e2d7afc 3350 spin_lock_irq(&dev->event_lock);
7d5e3799 3351 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3352 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3353
3354 return pending;
3355}
3356
e30e8f75
GP
3357static void intel_update_pipe_size(struct intel_crtc *crtc)
3358{
3359 struct drm_device *dev = crtc->base.dev;
3360 struct drm_i915_private *dev_priv = dev->dev_private;
3361 const struct drm_display_mode *adjusted_mode;
3362
3363 if (!i915.fastboot)
3364 return;
3365
3366 /*
3367 * Update pipe size and adjust fitter if needed: the reason for this is
3368 * that in compute_mode_changes we check the native mode (not the pfit
3369 * mode) to see if we can flip rather than do a full mode set. In the
3370 * fastboot case, we'll flip, but if we don't update the pipesrc and
3371 * pfit state, we'll end up with a big fb scanned out into the wrong
3372 * sized surface.
3373 *
3374 * To fix this properly, we need to hoist the checks up into
3375 * compute_mode_changes (or above), check the actual pfit state and
3376 * whether the platform allows pfit disable with pipe active, and only
3377 * then update the pipesrc and pfit state, even on the flip path.
3378 */
3379
6e3c9717 3380 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3381
3382 I915_WRITE(PIPESRC(crtc->pipe),
3383 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3384 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3385 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3386 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3387 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3388 I915_WRITE(PF_CTL(crtc->pipe), 0);
3389 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3390 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3391 }
6e3c9717
ACO
3392 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3393 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3394}
3395
5e84e1a4
ZW
3396static void intel_fdi_normal_train(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3401 int pipe = intel_crtc->pipe;
3402 u32 reg, temp;
3403
3404 /* enable normal train */
3405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
61e499bf 3407 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3408 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3409 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3410 } else {
3411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3413 }
5e84e1a4
ZW
3414 I915_WRITE(reg, temp);
3415
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
3418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_NONE;
3424 }
3425 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3426
3427 /* wait one idle pattern time */
3428 POSTING_READ(reg);
3429 udelay(1000);
357555c0
JB
3430
3431 /* IVB wants error correction enabled */
3432 if (IS_IVYBRIDGE(dev))
3433 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3434 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3435}
3436
8db9d77b
ZW
3437/* The FDI link training functions for ILK/Ibexpeak. */
3438static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3439{
3440 struct drm_device *dev = crtc->dev;
3441 struct drm_i915_private *dev_priv = dev->dev_private;
3442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3443 int pipe = intel_crtc->pipe;
5eddb70b 3444 u32 reg, temp, tries;
8db9d77b 3445
1c8562f6 3446 /* FDI needs bits from pipe first */
0fc932b8 3447 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3448
e1a44743
AJ
3449 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3450 for train result */
5eddb70b
CW
3451 reg = FDI_RX_IMR(pipe);
3452 temp = I915_READ(reg);
e1a44743
AJ
3453 temp &= ~FDI_RX_SYMBOL_LOCK;
3454 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3455 I915_WRITE(reg, temp);
3456 I915_READ(reg);
e1a44743
AJ
3457 udelay(150);
3458
8db9d77b 3459 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3460 reg = FDI_TX_CTL(pipe);
3461 temp = I915_READ(reg);
627eb5a3 3462 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3463 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3464 temp &= ~FDI_LINK_TRAIN_NONE;
3465 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3466 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3467
5eddb70b
CW
3468 reg = FDI_RX_CTL(pipe);
3469 temp = I915_READ(reg);
8db9d77b
ZW
3470 temp &= ~FDI_LINK_TRAIN_NONE;
3471 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3472 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3473
3474 POSTING_READ(reg);
8db9d77b
ZW
3475 udelay(150);
3476
5b2adf89 3477 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3478 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3479 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3480 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3481
5eddb70b 3482 reg = FDI_RX_IIR(pipe);
e1a44743 3483 for (tries = 0; tries < 5; tries++) {
5eddb70b 3484 temp = I915_READ(reg);
8db9d77b
ZW
3485 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3486
3487 if ((temp & FDI_RX_BIT_LOCK)) {
3488 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3489 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3490 break;
3491 }
8db9d77b 3492 }
e1a44743 3493 if (tries == 5)
5eddb70b 3494 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3495
3496 /* Train 2 */
5eddb70b
CW
3497 reg = FDI_TX_CTL(pipe);
3498 temp = I915_READ(reg);
8db9d77b
ZW
3499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3501 I915_WRITE(reg, temp);
8db9d77b 3502
5eddb70b
CW
3503 reg = FDI_RX_CTL(pipe);
3504 temp = I915_READ(reg);
8db9d77b
ZW
3505 temp &= ~FDI_LINK_TRAIN_NONE;
3506 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3507 I915_WRITE(reg, temp);
8db9d77b 3508
5eddb70b
CW
3509 POSTING_READ(reg);
3510 udelay(150);
8db9d77b 3511
5eddb70b 3512 reg = FDI_RX_IIR(pipe);
e1a44743 3513 for (tries = 0; tries < 5; tries++) {
5eddb70b 3514 temp = I915_READ(reg);
8db9d77b
ZW
3515 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3516
3517 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3518 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3519 DRM_DEBUG_KMS("FDI train 2 done.\n");
3520 break;
3521 }
8db9d77b 3522 }
e1a44743 3523 if (tries == 5)
5eddb70b 3524 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3525
3526 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3527
8db9d77b
ZW
3528}
3529
0206e353 3530static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3531 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3532 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3533 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3534 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3535};
3536
3537/* The FDI link training functions for SNB/Cougarpoint. */
3538static void gen6_fdi_link_train(struct drm_crtc *crtc)
3539{
3540 struct drm_device *dev = crtc->dev;
3541 struct drm_i915_private *dev_priv = dev->dev_private;
3542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3543 int pipe = intel_crtc->pipe;
fa37d39e 3544 u32 reg, temp, i, retry;
8db9d77b 3545
e1a44743
AJ
3546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
5eddb70b
CW
3548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
e1a44743
AJ
3550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
e1a44743
AJ
3555 udelay(150);
3556
8db9d77b 3557 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
627eb5a3 3560 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3561 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3562 temp &= ~FDI_LINK_TRAIN_NONE;
3563 temp |= FDI_LINK_TRAIN_PATTERN_1;
3564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 /* SNB-B */
3566 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3567 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3568
d74cf324
DV
3569 I915_WRITE(FDI_RX_MISC(pipe),
3570 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3571
5eddb70b
CW
3572 reg = FDI_RX_CTL(pipe);
3573 temp = I915_READ(reg);
8db9d77b
ZW
3574 if (HAS_PCH_CPT(dev)) {
3575 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3576 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3577 } else {
3578 temp &= ~FDI_LINK_TRAIN_NONE;
3579 temp |= FDI_LINK_TRAIN_PATTERN_1;
3580 }
5eddb70b
CW
3581 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3582
3583 POSTING_READ(reg);
8db9d77b
ZW
3584 udelay(150);
3585
0206e353 3586 for (i = 0; i < 4; i++) {
5eddb70b
CW
3587 reg = FDI_TX_CTL(pipe);
3588 temp = I915_READ(reg);
8db9d77b
ZW
3589 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3590 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3591 I915_WRITE(reg, temp);
3592
3593 POSTING_READ(reg);
8db9d77b
ZW
3594 udelay(500);
3595
fa37d39e
SP
3596 for (retry = 0; retry < 5; retry++) {
3597 reg = FDI_RX_IIR(pipe);
3598 temp = I915_READ(reg);
3599 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3600 if (temp & FDI_RX_BIT_LOCK) {
3601 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3602 DRM_DEBUG_KMS("FDI train 1 done.\n");
3603 break;
3604 }
3605 udelay(50);
8db9d77b 3606 }
fa37d39e
SP
3607 if (retry < 5)
3608 break;
8db9d77b
ZW
3609 }
3610 if (i == 4)
5eddb70b 3611 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3612
3613 /* Train 2 */
5eddb70b
CW
3614 reg = FDI_TX_CTL(pipe);
3615 temp = I915_READ(reg);
8db9d77b
ZW
3616 temp &= ~FDI_LINK_TRAIN_NONE;
3617 temp |= FDI_LINK_TRAIN_PATTERN_2;
3618 if (IS_GEN6(dev)) {
3619 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3620 /* SNB-B */
3621 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3622 }
5eddb70b 3623 I915_WRITE(reg, temp);
8db9d77b 3624
5eddb70b
CW
3625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
8db9d77b
ZW
3627 if (HAS_PCH_CPT(dev)) {
3628 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3629 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3630 } else {
3631 temp &= ~FDI_LINK_TRAIN_NONE;
3632 temp |= FDI_LINK_TRAIN_PATTERN_2;
3633 }
5eddb70b
CW
3634 I915_WRITE(reg, temp);
3635
3636 POSTING_READ(reg);
8db9d77b
ZW
3637 udelay(150);
3638
0206e353 3639 for (i = 0; i < 4; i++) {
5eddb70b
CW
3640 reg = FDI_TX_CTL(pipe);
3641 temp = I915_READ(reg);
8db9d77b
ZW
3642 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3643 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3644 I915_WRITE(reg, temp);
3645
3646 POSTING_READ(reg);
8db9d77b
ZW
3647 udelay(500);
3648
fa37d39e
SP
3649 for (retry = 0; retry < 5; retry++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3653 if (temp & FDI_RX_SYMBOL_LOCK) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done.\n");
3656 break;
3657 }
3658 udelay(50);
8db9d77b 3659 }
fa37d39e
SP
3660 if (retry < 5)
3661 break;
8db9d77b
ZW
3662 }
3663 if (i == 4)
5eddb70b 3664 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3665
3666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
357555c0
JB
3669/* Manual link training for Ivy Bridge A0 parts */
3670static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3671{
3672 struct drm_device *dev = crtc->dev;
3673 struct drm_i915_private *dev_priv = dev->dev_private;
3674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3675 int pipe = intel_crtc->pipe;
139ccd3f 3676 u32 reg, temp, i, j;
357555c0
JB
3677
3678 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3679 for train result */
3680 reg = FDI_RX_IMR(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~FDI_RX_SYMBOL_LOCK;
3683 temp &= ~FDI_RX_BIT_LOCK;
3684 I915_WRITE(reg, temp);
3685
3686 POSTING_READ(reg);
3687 udelay(150);
3688
01a415fd
DV
3689 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3690 I915_READ(FDI_RX_IIR(pipe)));
3691
139ccd3f
JB
3692 /* Try each vswing and preemphasis setting twice before moving on */
3693 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3694 /* disable first in case we need to retry */
3695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
3697 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3698 temp &= ~FDI_TX_ENABLE;
3699 I915_WRITE(reg, temp);
357555c0 3700
139ccd3f
JB
3701 reg = FDI_RX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 temp &= ~FDI_LINK_TRAIN_AUTO;
3704 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3705 temp &= ~FDI_RX_ENABLE;
3706 I915_WRITE(reg, temp);
357555c0 3707
139ccd3f 3708 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3709 reg = FDI_TX_CTL(pipe);
3710 temp = I915_READ(reg);
139ccd3f 3711 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3712 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3713 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3714 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3715 temp |= snb_b_fdi_train_param[j/2];
3716 temp |= FDI_COMPOSITE_SYNC;
3717 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3718
139ccd3f
JB
3719 I915_WRITE(FDI_RX_MISC(pipe),
3720 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3721
139ccd3f 3722 reg = FDI_RX_CTL(pipe);
357555c0 3723 temp = I915_READ(reg);
139ccd3f
JB
3724 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3725 temp |= FDI_COMPOSITE_SYNC;
3726 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3727
139ccd3f
JB
3728 POSTING_READ(reg);
3729 udelay(1); /* should be 0.5us */
357555c0 3730
139ccd3f
JB
3731 for (i = 0; i < 4; i++) {
3732 reg = FDI_RX_IIR(pipe);
3733 temp = I915_READ(reg);
3734 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3735
139ccd3f
JB
3736 if (temp & FDI_RX_BIT_LOCK ||
3737 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3738 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3739 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3740 i);
3741 break;
3742 }
3743 udelay(1); /* should be 0.5us */
3744 }
3745 if (i == 4) {
3746 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3747 continue;
3748 }
357555c0 3749
139ccd3f 3750 /* Train 2 */
357555c0
JB
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
139ccd3f
JB
3753 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3754 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3755 I915_WRITE(reg, temp);
3756
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3761 I915_WRITE(reg, temp);
3762
3763 POSTING_READ(reg);
139ccd3f 3764 udelay(2); /* should be 1.5us */
357555c0 3765
139ccd3f
JB
3766 for (i = 0; i < 4; i++) {
3767 reg = FDI_RX_IIR(pipe);
3768 temp = I915_READ(reg);
3769 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3770
139ccd3f
JB
3771 if (temp & FDI_RX_SYMBOL_LOCK ||
3772 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3774 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3775 i);
3776 goto train_done;
3777 }
3778 udelay(2); /* should be 1.5us */
357555c0 3779 }
139ccd3f
JB
3780 if (i == 4)
3781 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3782 }
357555c0 3783
139ccd3f 3784train_done:
357555c0
JB
3785 DRM_DEBUG_KMS("FDI train done.\n");
3786}
3787
88cefb6c 3788static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3789{
88cefb6c 3790 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3791 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3792 int pipe = intel_crtc->pipe;
5eddb70b 3793 u32 reg, temp;
79e53945 3794
c64e311e 3795
c98e9dcf 3796 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3797 reg = FDI_RX_CTL(pipe);
3798 temp = I915_READ(reg);
627eb5a3 3799 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3800 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3801 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3802 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3803
3804 POSTING_READ(reg);
c98e9dcf
JB
3805 udelay(200);
3806
3807 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3808 temp = I915_READ(reg);
3809 I915_WRITE(reg, temp | FDI_PCDCLK);
3810
3811 POSTING_READ(reg);
c98e9dcf
JB
3812 udelay(200);
3813
20749730
PZ
3814 /* Enable CPU FDI TX PLL, always on for Ironlake */
3815 reg = FDI_TX_CTL(pipe);
3816 temp = I915_READ(reg);
3817 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3818 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3819
20749730
PZ
3820 POSTING_READ(reg);
3821 udelay(100);
6be4a607 3822 }
0e23b99d
JB
3823}
3824
88cefb6c
DV
3825static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3826{
3827 struct drm_device *dev = intel_crtc->base.dev;
3828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 int pipe = intel_crtc->pipe;
3830 u32 reg, temp;
3831
3832 /* Switch from PCDclk to Rawclk */
3833 reg = FDI_RX_CTL(pipe);
3834 temp = I915_READ(reg);
3835 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3836
3837 /* Disable CPU FDI TX PLL */
3838 reg = FDI_TX_CTL(pipe);
3839 temp = I915_READ(reg);
3840 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3841
3842 POSTING_READ(reg);
3843 udelay(100);
3844
3845 reg = FDI_RX_CTL(pipe);
3846 temp = I915_READ(reg);
3847 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3848
3849 /* Wait for the clocks to turn off. */
3850 POSTING_READ(reg);
3851 udelay(100);
3852}
3853
0fc932b8
JB
3854static void ironlake_fdi_disable(struct drm_crtc *crtc)
3855{
3856 struct drm_device *dev = crtc->dev;
3857 struct drm_i915_private *dev_priv = dev->dev_private;
3858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3859 int pipe = intel_crtc->pipe;
3860 u32 reg, temp;
3861
3862 /* disable CPU FDI tx and PCH FDI rx */
3863 reg = FDI_TX_CTL(pipe);
3864 temp = I915_READ(reg);
3865 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3866 POSTING_READ(reg);
3867
3868 reg = FDI_RX_CTL(pipe);
3869 temp = I915_READ(reg);
3870 temp &= ~(0x7 << 16);
dfd07d72 3871 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3872 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3873
3874 POSTING_READ(reg);
3875 udelay(100);
3876
3877 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3878 if (HAS_PCH_IBX(dev))
6f06ce18 3879 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3880
3881 /* still set train pattern 1 */
3882 reg = FDI_TX_CTL(pipe);
3883 temp = I915_READ(reg);
3884 temp &= ~FDI_LINK_TRAIN_NONE;
3885 temp |= FDI_LINK_TRAIN_PATTERN_1;
3886 I915_WRITE(reg, temp);
3887
3888 reg = FDI_RX_CTL(pipe);
3889 temp = I915_READ(reg);
3890 if (HAS_PCH_CPT(dev)) {
3891 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3892 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3893 } else {
3894 temp &= ~FDI_LINK_TRAIN_NONE;
3895 temp |= FDI_LINK_TRAIN_PATTERN_1;
3896 }
3897 /* BPC in FDI rx is consistent with that in PIPECONF */
3898 temp &= ~(0x07 << 16);
dfd07d72 3899 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
3903 udelay(100);
3904}
3905
5dce5b93
CW
3906bool intel_has_pending_fb_unpin(struct drm_device *dev)
3907{
3908 struct intel_crtc *crtc;
3909
3910 /* Note that we don't need to be called with mode_config.lock here
3911 * as our list of CRTC objects is static for the lifetime of the
3912 * device and so cannot disappear as we iterate. Similarly, we can
3913 * happily treat the predicates as racy, atomic checks as userspace
3914 * cannot claim and pin a new fb without at least acquring the
3915 * struct_mutex and so serialising with us.
3916 */
d3fcc808 3917 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3918 if (atomic_read(&crtc->unpin_work_count) == 0)
3919 continue;
3920
3921 if (crtc->unpin_work)
3922 intel_wait_for_vblank(dev, crtc->pipe);
3923
3924 return true;
3925 }
3926
3927 return false;
3928}
3929
d6bbafa1
CW
3930static void page_flip_completed(struct intel_crtc *intel_crtc)
3931{
3932 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3933 struct intel_unpin_work *work = intel_crtc->unpin_work;
3934
3935 /* ensure that the unpin work is consistent wrt ->pending. */
3936 smp_rmb();
3937 intel_crtc->unpin_work = NULL;
3938
3939 if (work->event)
3940 drm_send_vblank_event(intel_crtc->base.dev,
3941 intel_crtc->pipe,
3942 work->event);
3943
3944 drm_crtc_vblank_put(&intel_crtc->base);
3945
3946 wake_up_all(&dev_priv->pending_flip_queue);
3947 queue_work(dev_priv->wq, &work->work);
3948
3949 trace_i915_flip_complete(intel_crtc->plane,
3950 work->pending_flip_obj);
3951}
3952
46a55d30 3953void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3954{
0f91128d 3955 struct drm_device *dev = crtc->dev;
5bb61643 3956 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3957
2c10d571 3958 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3959 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3960 !intel_crtc_has_pending_flip(crtc),
3961 60*HZ) == 0)) {
3962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3963
5e2d7afc 3964 spin_lock_irq(&dev->event_lock);
9c787942
CW
3965 if (intel_crtc->unpin_work) {
3966 WARN_ONCE(1, "Removing stuck page flip\n");
3967 page_flip_completed(intel_crtc);
3968 }
5e2d7afc 3969 spin_unlock_irq(&dev->event_lock);
9c787942 3970 }
5bb61643 3971
975d568a
CW
3972 if (crtc->primary->fb) {
3973 mutex_lock(&dev->struct_mutex);
3974 intel_finish_fb(crtc->primary->fb);
3975 mutex_unlock(&dev->struct_mutex);
3976 }
e6c3a2a6
CW
3977}
3978
e615efe4
ED
3979/* Program iCLKIP clock to the desired frequency */
3980static void lpt_program_iclkip(struct drm_crtc *crtc)
3981{
3982 struct drm_device *dev = crtc->dev;
3983 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3984 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3985 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3986 u32 temp;
3987
09153000
DV
3988 mutex_lock(&dev_priv->dpio_lock);
3989
e615efe4
ED
3990 /* It is necessary to ungate the pixclk gate prior to programming
3991 * the divisors, and gate it back when it is done.
3992 */
3993 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3994
3995 /* Disable SSCCTL */
3996 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3997 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3998 SBI_SSCCTL_DISABLE,
3999 SBI_ICLK);
e615efe4
ED
4000
4001 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 4002 if (clock == 20000) {
e615efe4
ED
4003 auxdiv = 1;
4004 divsel = 0x41;
4005 phaseinc = 0x20;
4006 } else {
4007 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
4008 * but the adjusted_mode->crtc_clock in in KHz. To get the
4009 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
4010 * convert the virtual clock precision to KHz here for higher
4011 * precision.
4012 */
4013 u32 iclk_virtual_root_freq = 172800 * 1000;
4014 u32 iclk_pi_range = 64;
4015 u32 desired_divisor, msb_divisor_value, pi_value;
4016
12d7ceed 4017 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
4018 msb_divisor_value = desired_divisor / iclk_pi_range;
4019 pi_value = desired_divisor % iclk_pi_range;
4020
4021 auxdiv = 0;
4022 divsel = msb_divisor_value - 2;
4023 phaseinc = pi_value;
4024 }
4025
4026 /* This should not happen with any sane values */
4027 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4028 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4029 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4030 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4031
4032 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4033 clock,
e615efe4
ED
4034 auxdiv,
4035 divsel,
4036 phasedir,
4037 phaseinc);
4038
4039 /* Program SSCDIVINTPHASE6 */
988d6ee8 4040 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4041 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4042 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4043 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4044 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4045 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4046 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4047 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4048
4049 /* Program SSCAUXDIV */
988d6ee8 4050 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4051 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4052 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4053 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4054
4055 /* Enable modulator and associated divider */
988d6ee8 4056 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4057 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4058 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4059
4060 /* Wait for initialization time */
4061 udelay(24);
4062
4063 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
4064
4065 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
4066}
4067
275f01b2
DV
4068static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4069 enum pipe pch_transcoder)
4070{
4071 struct drm_device *dev = crtc->base.dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4073 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4074
4075 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4076 I915_READ(HTOTAL(cpu_transcoder)));
4077 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4078 I915_READ(HBLANK(cpu_transcoder)));
4079 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4080 I915_READ(HSYNC(cpu_transcoder)));
4081
4082 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4083 I915_READ(VTOTAL(cpu_transcoder)));
4084 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4085 I915_READ(VBLANK(cpu_transcoder)));
4086 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4087 I915_READ(VSYNC(cpu_transcoder)));
4088 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4089 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4090}
4091
003632d9 4092static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4093{
4094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 uint32_t temp;
4096
4097 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4098 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4099 return;
4100
4101 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4102 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4103
003632d9
ACO
4104 temp &= ~FDI_BC_BIFURCATION_SELECT;
4105 if (enable)
4106 temp |= FDI_BC_BIFURCATION_SELECT;
4107
4108 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4109 I915_WRITE(SOUTH_CHICKEN1, temp);
4110 POSTING_READ(SOUTH_CHICKEN1);
4111}
4112
4113static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4114{
4115 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4116
4117 switch (intel_crtc->pipe) {
4118 case PIPE_A:
4119 break;
4120 case PIPE_B:
6e3c9717 4121 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4122 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4123 else
003632d9 4124 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4125
4126 break;
4127 case PIPE_C:
003632d9 4128 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4129
4130 break;
4131 default:
4132 BUG();
4133 }
4134}
4135
f67a559d
JB
4136/*
4137 * Enable PCH resources required for PCH ports:
4138 * - PCH PLLs
4139 * - FDI training & RX/TX
4140 * - update transcoder timings
4141 * - DP transcoding bits
4142 * - transcoder
4143 */
4144static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4145{
4146 struct drm_device *dev = crtc->dev;
4147 struct drm_i915_private *dev_priv = dev->dev_private;
4148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4149 int pipe = intel_crtc->pipe;
ee7b9f93 4150 u32 reg, temp;
2c07245f 4151
ab9412ba 4152 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4153
1fbc0d78
DV
4154 if (IS_IVYBRIDGE(dev))
4155 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4156
cd986abb
DV
4157 /* Write the TU size bits before fdi link training, so that error
4158 * detection works. */
4159 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4160 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4161
c98e9dcf 4162 /* For PCH output, training FDI link */
674cf967 4163 dev_priv->display.fdi_link_train(crtc);
2c07245f 4164
3ad8a208
DV
4165 /* We need to program the right clock selection before writing the pixel
4166 * mutliplier into the DPLL. */
303b81e0 4167 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4168 u32 sel;
4b645f14 4169
c98e9dcf 4170 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4171 temp |= TRANS_DPLL_ENABLE(pipe);
4172 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4173 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4174 temp |= sel;
4175 else
4176 temp &= ~sel;
c98e9dcf 4177 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4178 }
5eddb70b 4179
3ad8a208
DV
4180 /* XXX: pch pll's can be enabled any time before we enable the PCH
4181 * transcoder, and we actually should do this to not upset any PCH
4182 * transcoder that already use the clock when we share it.
4183 *
4184 * Note that enable_shared_dpll tries to do the right thing, but
4185 * get_shared_dpll unconditionally resets the pll - we need that to have
4186 * the right LVDS enable sequence. */
85b3894f 4187 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4188
d9b6cb56
JB
4189 /* set transcoder timing, panel must allow it */
4190 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4191 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4192
303b81e0 4193 intel_fdi_normal_train(crtc);
5e84e1a4 4194
c98e9dcf 4195 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4196 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4197 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4198 reg = TRANS_DP_CTL(pipe);
4199 temp = I915_READ(reg);
4200 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4201 TRANS_DP_SYNC_MASK |
4202 TRANS_DP_BPC_MASK);
5eddb70b
CW
4203 temp |= (TRANS_DP_OUTPUT_ENABLE |
4204 TRANS_DP_ENH_FRAMING);
9325c9f0 4205 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4206
4207 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4208 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4209 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4210 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4211
4212 switch (intel_trans_dp_port_sel(crtc)) {
4213 case PCH_DP_B:
5eddb70b 4214 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4215 break;
4216 case PCH_DP_C:
5eddb70b 4217 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4218 break;
4219 case PCH_DP_D:
5eddb70b 4220 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4221 break;
4222 default:
e95d41e1 4223 BUG();
32f9d658 4224 }
2c07245f 4225
5eddb70b 4226 I915_WRITE(reg, temp);
6be4a607 4227 }
b52eb4dc 4228
b8a4f404 4229 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4230}
4231
1507e5bd
PZ
4232static void lpt_pch_enable(struct drm_crtc *crtc)
4233{
4234 struct drm_device *dev = crtc->dev;
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4237 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4238
ab9412ba 4239 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4240
8c52b5e8 4241 lpt_program_iclkip(crtc);
1507e5bd 4242
0540e488 4243 /* Set transcoder timing. */
275f01b2 4244 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4245
937bb610 4246 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4247}
4248
716c2e55 4249void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 4250{
e2b78267 4251 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
4252
4253 if (pll == NULL)
4254 return;
4255
3e369b76 4256 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 4257 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
4258 return;
4259 }
4260
3e369b76
ACO
4261 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4262 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
4263 WARN_ON(pll->on);
4264 WARN_ON(pll->active);
4265 }
4266
6e3c9717 4267 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
4268}
4269
190f68c5
ACO
4270struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4271 struct intel_crtc_state *crtc_state)
ee7b9f93 4272{
e2b78267 4273 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4274 struct intel_shared_dpll *pll;
e2b78267 4275 enum intel_dpll_id i;
ee7b9f93 4276
98b6bd99
DV
4277 if (HAS_PCH_IBX(dev_priv->dev)) {
4278 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4279 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4280 pll = &dev_priv->shared_dplls[i];
98b6bd99 4281
46edb027
DV
4282 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4283 crtc->base.base.id, pll->name);
98b6bd99 4284
8bd31e67 4285 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 4286
98b6bd99
DV
4287 goto found;
4288 }
4289
bcddf610
S
4290 if (IS_BROXTON(dev_priv->dev)) {
4291 /* PLL is attached to port in bxt */
4292 struct intel_encoder *encoder;
4293 struct intel_digital_port *intel_dig_port;
4294
4295 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4296 if (WARN_ON(!encoder))
4297 return NULL;
4298
4299 intel_dig_port = enc_to_dig_port(&encoder->base);
4300 /* 1:1 mapping between ports and PLLs */
4301 i = (enum intel_dpll_id)intel_dig_port->port;
4302 pll = &dev_priv->shared_dplls[i];
4303 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4304 crtc->base.base.id, pll->name);
4305 WARN_ON(pll->new_config->crtc_mask);
4306
4307 goto found;
4308 }
4309
e72f9fbf
DV
4310 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4311 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4312
4313 /* Only want to check enabled timings first */
8bd31e67 4314 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
4315 continue;
4316
190f68c5 4317 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
4318 &pll->new_config->hw_state,
4319 sizeof(pll->new_config->hw_state)) == 0) {
4320 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4321 crtc->base.base.id, pll->name,
8bd31e67
ACO
4322 pll->new_config->crtc_mask,
4323 pll->active);
ee7b9f93
JB
4324 goto found;
4325 }
4326 }
4327
4328 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4329 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4330 pll = &dev_priv->shared_dplls[i];
8bd31e67 4331 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
4332 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4333 crtc->base.base.id, pll->name);
ee7b9f93
JB
4334 goto found;
4335 }
4336 }
4337
4338 return NULL;
4339
4340found:
8bd31e67 4341 if (pll->new_config->crtc_mask == 0)
190f68c5 4342 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 4343
190f68c5 4344 crtc_state->shared_dpll = i;
46edb027
DV
4345 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4346 pipe_name(crtc->pipe));
ee7b9f93 4347
8bd31e67 4348 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 4349
ee7b9f93
JB
4350 return pll;
4351}
4352
8bd31e67
ACO
4353/**
4354 * intel_shared_dpll_start_config - start a new PLL staged config
4355 * @dev_priv: DRM device
4356 * @clear_pipes: mask of pipes that will have their PLLs freed
4357 *
4358 * Starts a new PLL staged config, copying the current config but
4359 * releasing the references of pipes specified in clear_pipes.
4360 */
4361static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4362 unsigned clear_pipes)
4363{
4364 struct intel_shared_dpll *pll;
4365 enum intel_dpll_id i;
4366
4367 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4368 pll = &dev_priv->shared_dplls[i];
4369
4370 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4371 GFP_KERNEL);
4372 if (!pll->new_config)
4373 goto cleanup;
4374
4375 pll->new_config->crtc_mask &= ~clear_pipes;
4376 }
4377
4378 return 0;
4379
4380cleanup:
4381 while (--i >= 0) {
4382 pll = &dev_priv->shared_dplls[i];
f354d733 4383 kfree(pll->new_config);
8bd31e67
ACO
4384 pll->new_config = NULL;
4385 }
4386
4387 return -ENOMEM;
4388}
4389
4390static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4391{
4392 struct intel_shared_dpll *pll;
4393 enum intel_dpll_id i;
4394
4395 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4396 pll = &dev_priv->shared_dplls[i];
4397
4398 WARN_ON(pll->new_config == &pll->config);
4399
4400 pll->config = *pll->new_config;
4401 kfree(pll->new_config);
4402 pll->new_config = NULL;
4403 }
4404}
4405
4406static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4407{
4408 struct intel_shared_dpll *pll;
4409 enum intel_dpll_id i;
4410
4411 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4412 pll = &dev_priv->shared_dplls[i];
4413
4414 WARN_ON(pll->new_config == &pll->config);
4415
4416 kfree(pll->new_config);
4417 pll->new_config = NULL;
4418 }
4419}
4420
a1520318 4421static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4422{
4423 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4424 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4425 u32 temp;
4426
4427 temp = I915_READ(dslreg);
4428 udelay(500);
4429 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4430 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4431 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4432 }
4433}
4434
a1b2278e
CK
4435/**
4436 * skl_update_scaler_users - Stages update to crtc's scaler state
4437 * @intel_crtc: crtc
4438 * @crtc_state: crtc_state
4439 * @plane: plane (NULL indicates crtc is requesting update)
4440 * @plane_state: plane's state
4441 * @force_detach: request unconditional detachment of scaler
4442 *
4443 * This function updates scaler state for requested plane or crtc.
4444 * To request scaler usage update for a plane, caller shall pass plane pointer.
4445 * To request scaler usage update for crtc, caller shall pass plane pointer
4446 * as NULL.
4447 *
4448 * Return
4449 * 0 - scaler_usage updated successfully
4450 * error - requested scaling cannot be supported or other error condition
4451 */
4452int
4453skl_update_scaler_users(
4454 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4455 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4456 int force_detach)
4457{
4458 int need_scaling;
4459 int idx;
4460 int src_w, src_h, dst_w, dst_h;
4461 int *scaler_id;
4462 struct drm_framebuffer *fb;
4463 struct intel_crtc_scaler_state *scaler_state;
6156a456 4464 unsigned int rotation;
a1b2278e
CK
4465
4466 if (!intel_crtc || !crtc_state)
4467 return 0;
4468
4469 scaler_state = &crtc_state->scaler_state;
4470
4471 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4472 fb = intel_plane ? plane_state->base.fb : NULL;
4473
4474 if (intel_plane) {
4475 src_w = drm_rect_width(&plane_state->src) >> 16;
4476 src_h = drm_rect_height(&plane_state->src) >> 16;
4477 dst_w = drm_rect_width(&plane_state->dst);
4478 dst_h = drm_rect_height(&plane_state->dst);
4479 scaler_id = &plane_state->scaler_id;
6156a456 4480 rotation = plane_state->base.rotation;
a1b2278e
CK
4481 } else {
4482 struct drm_display_mode *adjusted_mode =
4483 &crtc_state->base.adjusted_mode;
4484 src_w = crtc_state->pipe_src_w;
4485 src_h = crtc_state->pipe_src_h;
4486 dst_w = adjusted_mode->hdisplay;
4487 dst_h = adjusted_mode->vdisplay;
4488 scaler_id = &scaler_state->scaler_id;
6156a456 4489 rotation = DRM_ROTATE_0;
a1b2278e 4490 }
6156a456
CK
4491
4492 need_scaling = intel_rotation_90_or_270(rotation) ?
4493 (src_h != dst_w || src_w != dst_h):
4494 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4495
4496 /*
4497 * if plane is being disabled or scaler is no more required or force detach
4498 * - free scaler binded to this plane/crtc
4499 * - in order to do this, update crtc->scaler_usage
4500 *
4501 * Here scaler state in crtc_state is set free so that
4502 * scaler can be assigned to other user. Actual register
4503 * update to free the scaler is done in plane/panel-fit programming.
4504 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4505 */
4506 if (force_detach || !need_scaling || (intel_plane &&
4507 (!fb || !plane_state->visible))) {
4508 if (*scaler_id >= 0) {
4509 scaler_state->scaler_users &= ~(1 << idx);
4510 scaler_state->scalers[*scaler_id].in_use = 0;
4511
4512 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4513 "crtc_state = %p scaler_users = 0x%x\n",
4514 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4515 intel_plane ? intel_plane->base.base.id :
4516 intel_crtc->base.base.id, crtc_state,
4517 scaler_state->scaler_users);
4518 *scaler_id = -1;
4519 }
4520 return 0;
4521 }
4522
4523 /* range checks */
4524 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4525 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4526
4527 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4528 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4529 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4530 "size is out of scaler range\n",
4531 intel_plane ? "PLANE" : "CRTC",
4532 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4533 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4534 return -EINVAL;
4535 }
4536
4537 /* check colorkey */
4538 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4539 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4540 intel_plane->base.base.id);
4541 return -EINVAL;
4542 }
4543
4544 /* Check src format */
4545 if (intel_plane) {
4546 switch (fb->pixel_format) {
4547 case DRM_FORMAT_RGB565:
4548 case DRM_FORMAT_XBGR8888:
4549 case DRM_FORMAT_XRGB8888:
4550 case DRM_FORMAT_ABGR8888:
4551 case DRM_FORMAT_ARGB8888:
4552 case DRM_FORMAT_XRGB2101010:
4553 case DRM_FORMAT_ARGB2101010:
4554 case DRM_FORMAT_XBGR2101010:
4555 case DRM_FORMAT_ABGR2101010:
4556 case DRM_FORMAT_YUYV:
4557 case DRM_FORMAT_YVYU:
4558 case DRM_FORMAT_UYVY:
4559 case DRM_FORMAT_VYUY:
4560 break;
4561 default:
4562 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4563 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4564 return -EINVAL;
4565 }
4566 }
4567
4568 /* mark this plane as a scaler user in crtc_state */
4569 scaler_state->scaler_users |= (1 << idx);
4570 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4571 "crtc_state = %p scaler_users = 0x%x\n",
4572 intel_plane ? "PLANE" : "CRTC",
4573 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4574 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4575 return 0;
4576}
4577
4578static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4579{
4580 struct drm_device *dev = crtc->base.dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 int pipe = crtc->pipe;
a1b2278e
CK
4583 struct intel_crtc_scaler_state *scaler_state =
4584 &crtc->config->scaler_state;
4585
4586 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4587
4588 /* To update pfit, first update scaler state */
4589 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4590 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4591 skl_detach_scalers(crtc);
4592 if (!enable)
4593 return;
bd2e244f 4594
6e3c9717 4595 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4596 int id;
4597
4598 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4599 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4600 return;
4601 }
4602
4603 id = scaler_state->scaler_id;
4604 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4605 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4606 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4607 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4608
4609 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4610 }
4611}
4612
b074cec8
JB
4613static void ironlake_pfit_enable(struct intel_crtc *crtc)
4614{
4615 struct drm_device *dev = crtc->base.dev;
4616 struct drm_i915_private *dev_priv = dev->dev_private;
4617 int pipe = crtc->pipe;
4618
6e3c9717 4619 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4620 /* Force use of hard-coded filter coefficients
4621 * as some pre-programmed values are broken,
4622 * e.g. x201.
4623 */
4624 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4625 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4626 PF_PIPE_SEL_IVB(pipe));
4627 else
4628 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4629 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4630 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4631 }
4632}
4633
4a3b8769 4634static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4635{
4636 struct drm_device *dev = crtc->dev;
4637 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4638 struct drm_plane *plane;
bb53d4ae
VS
4639 struct intel_plane *intel_plane;
4640
af2b653b
MR
4641 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4642 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4643 if (intel_plane->pipe == pipe)
4644 intel_plane_restore(&intel_plane->base);
af2b653b 4645 }
bb53d4ae
VS
4646}
4647
0d703d4e
MR
4648/*
4649 * Disable a plane internally without actually modifying the plane's state.
4650 * This will allow us to easily restore the plane later by just reprogramming
4651 * its state.
4652 */
4653static void disable_plane_internal(struct drm_plane *plane)
4654{
4655 struct intel_plane *intel_plane = to_intel_plane(plane);
4656 struct drm_plane_state *state =
4657 plane->funcs->atomic_duplicate_state(plane);
4658 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4659
4660 intel_state->visible = false;
4661 intel_plane->commit_plane(plane, intel_state);
4662
4663 intel_plane_destroy_state(plane, state);
4664}
4665
4a3b8769 4666static void intel_disable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4667{
4668 struct drm_device *dev = crtc->dev;
4669 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4670 struct drm_plane *plane;
bb53d4ae
VS
4671 struct intel_plane *intel_plane;
4672
af2b653b
MR
4673 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4674 intel_plane = to_intel_plane(plane);
0d703d4e
MR
4675 if (plane->fb && intel_plane->pipe == pipe)
4676 disable_plane_internal(plane);
af2b653b 4677 }
bb53d4ae
VS
4678}
4679
20bc8673 4680void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4681{
cea165c3
VS
4682 struct drm_device *dev = crtc->base.dev;
4683 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4684
6e3c9717 4685 if (!crtc->config->ips_enabled)
d77e4531
PZ
4686 return;
4687
cea165c3
VS
4688 /* We can only enable IPS after we enable a plane and wait for a vblank */
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690
d77e4531 4691 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4692 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4693 mutex_lock(&dev_priv->rps.hw_lock);
4694 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4695 mutex_unlock(&dev_priv->rps.hw_lock);
4696 /* Quoting Art Runyan: "its not safe to expect any particular
4697 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4698 * mailbox." Moreover, the mailbox may return a bogus state,
4699 * so we need to just enable it and continue on.
2a114cc1
BW
4700 */
4701 } else {
4702 I915_WRITE(IPS_CTL, IPS_ENABLE);
4703 /* The bit only becomes 1 in the next vblank, so this wait here
4704 * is essentially intel_wait_for_vblank. If we don't have this
4705 * and don't wait for vblanks until the end of crtc_enable, then
4706 * the HW state readout code will complain that the expected
4707 * IPS_CTL value is not the one we read. */
4708 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4709 DRM_ERROR("Timed out waiting for IPS enable\n");
4710 }
d77e4531
PZ
4711}
4712
20bc8673 4713void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4714{
4715 struct drm_device *dev = crtc->base.dev;
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717
6e3c9717 4718 if (!crtc->config->ips_enabled)
d77e4531
PZ
4719 return;
4720
4721 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4722 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4723 mutex_lock(&dev_priv->rps.hw_lock);
4724 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4725 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4726 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4727 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4728 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4729 } else {
2a114cc1 4730 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4731 POSTING_READ(IPS_CTL);
4732 }
d77e4531
PZ
4733
4734 /* We need to wait for a vblank before we can disable the plane. */
4735 intel_wait_for_vblank(dev, crtc->pipe);
4736}
4737
4738/** Loads the palette/gamma unit for the CRTC with the prepared values */
4739static void intel_crtc_load_lut(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744 enum pipe pipe = intel_crtc->pipe;
4745 int palreg = PALETTE(pipe);
4746 int i;
4747 bool reenable_ips = false;
4748
4749 /* The clocks have to be on to load the palette. */
83d65738 4750 if (!crtc->state->enable || !intel_crtc->active)
d77e4531
PZ
4751 return;
4752
50360403 4753 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4754 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4755 assert_dsi_pll_enabled(dev_priv);
4756 else
4757 assert_pll_enabled(dev_priv, pipe);
4758 }
4759
4760 /* use legacy palette for Ironlake */
7a1db49a 4761 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4762 palreg = LGC_PALETTE(pipe);
4763
4764 /* Workaround : Do not read or write the pipe palette/gamma data while
4765 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4766 */
6e3c9717 4767 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4768 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4769 GAMMA_MODE_MODE_SPLIT)) {
4770 hsw_disable_ips(intel_crtc);
4771 reenable_ips = true;
4772 }
4773
4774 for (i = 0; i < 256; i++) {
4775 I915_WRITE(palreg + 4 * i,
4776 (intel_crtc->lut_r[i] << 16) |
4777 (intel_crtc->lut_g[i] << 8) |
4778 intel_crtc->lut_b[i]);
4779 }
4780
4781 if (reenable_ips)
4782 hsw_enable_ips(intel_crtc);
4783}
4784
d3eedb1a
VS
4785static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4786{
4787 if (!enable && intel_crtc->overlay) {
4788 struct drm_device *dev = intel_crtc->base.dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4790
4791 mutex_lock(&dev->struct_mutex);
4792 dev_priv->mm.interruptible = false;
4793 (void) intel_overlay_switch_off(intel_crtc->overlay);
4794 dev_priv->mm.interruptible = true;
4795 mutex_unlock(&dev->struct_mutex);
4796 }
4797
4798 /* Let userspace switch the overlay on again. In most cases userspace
4799 * has to recompute where to put it anyway.
4800 */
4801}
4802
d3eedb1a 4803static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4804{
4805 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4807 int pipe = intel_crtc->pipe;
a5c4d7bc 4808
fdd508a6 4809 intel_enable_primary_hw_plane(crtc->primary, crtc);
4a3b8769 4810 intel_enable_sprite_planes(crtc);
a5c4d7bc 4811 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4812 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4813
4814 hsw_enable_ips(intel_crtc);
4815
4816 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4817 intel_fbc_update(dev);
a5c4d7bc 4818 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4819
4820 /*
4821 * FIXME: Once we grow proper nuclear flip support out of this we need
4822 * to compute the mask of flip planes precisely. For the time being
4823 * consider this a flip from a NULL plane.
4824 */
4825 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4826}
4827
d3eedb1a 4828static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4829{
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_private *dev_priv = dev->dev_private;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 int pipe = intel_crtc->pipe;
a5c4d7bc
VS
4834
4835 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc 4836
e35fef21 4837 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4838 intel_fbc_disable(dev);
a5c4d7bc
VS
4839
4840 hsw_disable_ips(intel_crtc);
4841
d3eedb1a 4842 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc 4843 intel_crtc_update_cursor(crtc, false);
4a3b8769 4844 intel_disable_sprite_planes(crtc);
fdd508a6 4845 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4846
f99d7069
DV
4847 /*
4848 * FIXME: Once we grow proper nuclear flip support out of this we need
4849 * to compute the mask of flip planes precisely. For the time being
4850 * consider this a flip to a NULL plane.
4851 */
4852 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4853}
4854
f67a559d
JB
4855static void ironlake_crtc_enable(struct drm_crtc *crtc)
4856{
4857 struct drm_device *dev = crtc->dev;
4858 struct drm_i915_private *dev_priv = dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4860 struct intel_encoder *encoder;
f67a559d 4861 int pipe = intel_crtc->pipe;
f67a559d 4862
83d65738 4863 WARN_ON(!crtc->state->enable);
08a48469 4864
f67a559d
JB
4865 if (intel_crtc->active)
4866 return;
4867
6e3c9717 4868 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4869 intel_prepare_shared_dpll(intel_crtc);
4870
6e3c9717 4871 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4872 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4873
4874 intel_set_pipe_timings(intel_crtc);
4875
6e3c9717 4876 if (intel_crtc->config->has_pch_encoder) {
29407aab 4877 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4878 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4879 }
4880
4881 ironlake_set_pipeconf(crtc);
4882
f67a559d 4883 intel_crtc->active = true;
8664281b 4884
a72e4c9f
DV
4885 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4886 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4887
f6736a1a 4888 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4889 if (encoder->pre_enable)
4890 encoder->pre_enable(encoder);
f67a559d 4891
6e3c9717 4892 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4893 /* Note: FDI PLL enabling _must_ be done before we enable the
4894 * cpu pipes, hence this is separate from all the other fdi/pch
4895 * enabling. */
88cefb6c 4896 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4897 } else {
4898 assert_fdi_tx_disabled(dev_priv, pipe);
4899 assert_fdi_rx_disabled(dev_priv, pipe);
4900 }
f67a559d 4901
b074cec8 4902 ironlake_pfit_enable(intel_crtc);
f67a559d 4903
9c54c0dd
JB
4904 /*
4905 * On ILK+ LUT must be loaded before the pipe is running but with
4906 * clocks enabled
4907 */
4908 intel_crtc_load_lut(crtc);
4909
f37fcc2a 4910 intel_update_watermarks(crtc);
e1fdc473 4911 intel_enable_pipe(intel_crtc);
f67a559d 4912
6e3c9717 4913 if (intel_crtc->config->has_pch_encoder)
f67a559d 4914 ironlake_pch_enable(crtc);
c98e9dcf 4915
f9b61ff6
DV
4916 assert_vblank_disabled(crtc);
4917 drm_crtc_vblank_on(crtc);
4918
fa5c73b1
DV
4919 for_each_encoder_on_crtc(dev, crtc, encoder)
4920 encoder->enable(encoder);
61b77ddd
DV
4921
4922 if (HAS_PCH_CPT(dev))
a1520318 4923 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4924
d3eedb1a 4925 intel_crtc_enable_planes(crtc);
6be4a607
JB
4926}
4927
42db64ef
PZ
4928/* IPS only exists on ULT machines and is tied to pipe A. */
4929static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4930{
f5adf94e 4931 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4932}
4933
e4916946
PZ
4934/*
4935 * This implements the workaround described in the "notes" section of the mode
4936 * set sequence documentation. When going from no pipes or single pipe to
4937 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4938 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4939 */
4940static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4941{
4942 struct drm_device *dev = crtc->base.dev;
4943 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4944
4945 /* We want to get the other_active_crtc only if there's only 1 other
4946 * active crtc. */
d3fcc808 4947 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4948 if (!crtc_it->active || crtc_it == crtc)
4949 continue;
4950
4951 if (other_active_crtc)
4952 return;
4953
4954 other_active_crtc = crtc_it;
4955 }
4956 if (!other_active_crtc)
4957 return;
4958
4959 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4960 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4961}
4962
4f771f10
PZ
4963static void haswell_crtc_enable(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4968 struct intel_encoder *encoder;
4969 int pipe = intel_crtc->pipe;
4f771f10 4970
83d65738 4971 WARN_ON(!crtc->state->enable);
4f771f10
PZ
4972
4973 if (intel_crtc->active)
4974 return;
4975
df8ad70c
DV
4976 if (intel_crtc_to_shared_dpll(intel_crtc))
4977 intel_enable_shared_dpll(intel_crtc);
4978
6e3c9717 4979 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4980 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4981
4982 intel_set_pipe_timings(intel_crtc);
4983
6e3c9717
ACO
4984 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4985 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4986 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4987 }
4988
6e3c9717 4989 if (intel_crtc->config->has_pch_encoder) {
229fca97 4990 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4991 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4992 }
4993
4994 haswell_set_pipeconf(crtc);
4995
4996 intel_set_pipe_csc(crtc);
4997
4f771f10 4998 intel_crtc->active = true;
8664281b 4999
a72e4c9f 5000 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
5001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->pre_enable)
5003 encoder->pre_enable(encoder);
5004
6e3c9717 5005 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
5006 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5007 true);
4fe9467d
ID
5008 dev_priv->display.fdi_link_train(crtc);
5009 }
5010
1f544388 5011 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5012
ff6d9f55 5013 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5014 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 5015 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5016 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
5017 else
5018 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
5019
5020 /*
5021 * On ILK+ LUT must be loaded before the pipe is running but with
5022 * clocks enabled
5023 */
5024 intel_crtc_load_lut(crtc);
5025
1f544388 5026 intel_ddi_set_pipe_settings(crtc);
8228c251 5027 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5028
f37fcc2a 5029 intel_update_watermarks(crtc);
e1fdc473 5030 intel_enable_pipe(intel_crtc);
42db64ef 5031
6e3c9717 5032 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5033 lpt_pch_enable(crtc);
4f771f10 5034
6e3c9717 5035 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5036 intel_ddi_set_vc_payload_alloc(crtc, true);
5037
f9b61ff6
DV
5038 assert_vblank_disabled(crtc);
5039 drm_crtc_vblank_on(crtc);
5040
8807e55b 5041 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 5042 encoder->enable(encoder);
8807e55b
JN
5043 intel_opregion_notify_encoder(encoder, true);
5044 }
4f771f10 5045
e4916946
PZ
5046 /* If we change the relative order between pipe/planes enabling, we need
5047 * to change the workaround. */
5048 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 5049 intel_crtc_enable_planes(crtc);
4f771f10
PZ
5050}
5051
3f8dce3a
DV
5052static void ironlake_pfit_disable(struct intel_crtc *crtc)
5053{
5054 struct drm_device *dev = crtc->base.dev;
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 int pipe = crtc->pipe;
5057
5058 /* To avoid upsetting the power well on haswell only disable the pfit if
5059 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 5060 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5061 I915_WRITE(PF_CTL(pipe), 0);
5062 I915_WRITE(PF_WIN_POS(pipe), 0);
5063 I915_WRITE(PF_WIN_SZ(pipe), 0);
5064 }
5065}
5066
6be4a607
JB
5067static void ironlake_crtc_disable(struct drm_crtc *crtc)
5068{
5069 struct drm_device *dev = crtc->dev;
5070 struct drm_i915_private *dev_priv = dev->dev_private;
5071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5072 struct intel_encoder *encoder;
6be4a607 5073 int pipe = intel_crtc->pipe;
5eddb70b 5074 u32 reg, temp;
b52eb4dc 5075
f7abfe8b
CW
5076 if (!intel_crtc->active)
5077 return;
5078
d3eedb1a 5079 intel_crtc_disable_planes(crtc);
a5c4d7bc 5080
ea9d758d
DV
5081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 encoder->disable(encoder);
5083
f9b61ff6
DV
5084 drm_crtc_vblank_off(crtc);
5085 assert_vblank_disabled(crtc);
5086
6e3c9717 5087 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5088 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5089
575f7ab7 5090 intel_disable_pipe(intel_crtc);
32f9d658 5091
3f8dce3a 5092 ironlake_pfit_disable(intel_crtc);
2c07245f 5093
bf49ec8c
DV
5094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 if (encoder->post_disable)
5096 encoder->post_disable(encoder);
2c07245f 5097
6e3c9717 5098 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5099 ironlake_fdi_disable(crtc);
913d8d11 5100
d925c59a 5101 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5102
d925c59a
DV
5103 if (HAS_PCH_CPT(dev)) {
5104 /* disable TRANS_DP_CTL */
5105 reg = TRANS_DP_CTL(pipe);
5106 temp = I915_READ(reg);
5107 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5108 TRANS_DP_PORT_SEL_MASK);
5109 temp |= TRANS_DP_PORT_SEL_NONE;
5110 I915_WRITE(reg, temp);
5111
5112 /* disable DPLL_SEL */
5113 temp = I915_READ(PCH_DPLL_SEL);
11887397 5114 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5115 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5116 }
e3421a18 5117
d925c59a 5118 /* disable PCH DPLL */
e72f9fbf 5119 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5120
d925c59a
DV
5121 ironlake_fdi_pll_disable(intel_crtc);
5122 }
6b383a7f 5123
f7abfe8b 5124 intel_crtc->active = false;
46ba614c 5125 intel_update_watermarks(crtc);
d1ebd816
BW
5126
5127 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5128 intel_fbc_update(dev);
d1ebd816 5129 mutex_unlock(&dev->struct_mutex);
6be4a607 5130}
1b3c7a47 5131
4f771f10 5132static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5133{
4f771f10
PZ
5134 struct drm_device *dev = crtc->dev;
5135 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5137 struct intel_encoder *encoder;
6e3c9717 5138 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5139
4f771f10
PZ
5140 if (!intel_crtc->active)
5141 return;
5142
d3eedb1a 5143 intel_crtc_disable_planes(crtc);
dda9a66a 5144
8807e55b
JN
5145 for_each_encoder_on_crtc(dev, crtc, encoder) {
5146 intel_opregion_notify_encoder(encoder, false);
4f771f10 5147 encoder->disable(encoder);
8807e55b 5148 }
4f771f10 5149
f9b61ff6
DV
5150 drm_crtc_vblank_off(crtc);
5151 assert_vblank_disabled(crtc);
5152
6e3c9717 5153 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5154 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5155 false);
575f7ab7 5156 intel_disable_pipe(intel_crtc);
4f771f10 5157
6e3c9717 5158 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5159 intel_ddi_set_vc_payload_alloc(crtc, false);
5160
ad80a810 5161 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5162
ff6d9f55 5163 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5164 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5165 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5166 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5167 else
5168 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5169
1f544388 5170 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5171
6e3c9717 5172 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5173 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5174 intel_ddi_fdi_disable(crtc);
83616634 5175 }
4f771f10 5176
97b040aa
ID
5177 for_each_encoder_on_crtc(dev, crtc, encoder)
5178 if (encoder->post_disable)
5179 encoder->post_disable(encoder);
5180
4f771f10 5181 intel_crtc->active = false;
46ba614c 5182 intel_update_watermarks(crtc);
4f771f10
PZ
5183
5184 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5185 intel_fbc_update(dev);
4f771f10 5186 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5187
5188 if (intel_crtc_to_shared_dpll(intel_crtc))
5189 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5190}
5191
ee7b9f93
JB
5192static void ironlake_crtc_off(struct drm_crtc *crtc)
5193{
5194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 5195 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
5196}
5197
6441ab5f 5198
2dd24552
JB
5199static void i9xx_pfit_enable(struct intel_crtc *crtc)
5200{
5201 struct drm_device *dev = crtc->base.dev;
5202 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5203 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5204
681a8504 5205 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5206 return;
5207
2dd24552 5208 /*
c0b03411
DV
5209 * The panel fitter should only be adjusted whilst the pipe is disabled,
5210 * according to register description and PRM.
2dd24552 5211 */
c0b03411
DV
5212 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5213 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5214
b074cec8
JB
5215 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5216 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5217
5218 /* Border color in case we don't scale up to the full screen. Black by
5219 * default, change to something else for debugging. */
5220 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5221}
5222
d05410f9
DA
5223static enum intel_display_power_domain port_to_power_domain(enum port port)
5224{
5225 switch (port) {
5226 case PORT_A:
5227 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5228 case PORT_B:
5229 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5230 case PORT_C:
5231 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5232 case PORT_D:
5233 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5234 default:
5235 WARN_ON_ONCE(1);
5236 return POWER_DOMAIN_PORT_OTHER;
5237 }
5238}
5239
77d22dca
ID
5240#define for_each_power_domain(domain, mask) \
5241 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5242 if ((1 << (domain)) & (mask))
5243
319be8ae
ID
5244enum intel_display_power_domain
5245intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5246{
5247 struct drm_device *dev = intel_encoder->base.dev;
5248 struct intel_digital_port *intel_dig_port;
5249
5250 switch (intel_encoder->type) {
5251 case INTEL_OUTPUT_UNKNOWN:
5252 /* Only DDI platforms should ever use this output type */
5253 WARN_ON_ONCE(!HAS_DDI(dev));
5254 case INTEL_OUTPUT_DISPLAYPORT:
5255 case INTEL_OUTPUT_HDMI:
5256 case INTEL_OUTPUT_EDP:
5257 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5258 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5259 case INTEL_OUTPUT_DP_MST:
5260 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5261 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5262 case INTEL_OUTPUT_ANALOG:
5263 return POWER_DOMAIN_PORT_CRT;
5264 case INTEL_OUTPUT_DSI:
5265 return POWER_DOMAIN_PORT_DSI;
5266 default:
5267 return POWER_DOMAIN_PORT_OTHER;
5268 }
5269}
5270
5271static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5272{
319be8ae
ID
5273 struct drm_device *dev = crtc->dev;
5274 struct intel_encoder *intel_encoder;
5275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5276 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5277 unsigned long mask;
5278 enum transcoder transcoder;
5279
5280 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5281
5282 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5283 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5284 if (intel_crtc->config->pch_pfit.enabled ||
5285 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5286 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5287
319be8ae
ID
5288 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5289 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5290
77d22dca
ID
5291 return mask;
5292}
5293
679dacd4 5294static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5295{
679dacd4 5296 struct drm_device *dev = state->dev;
77d22dca
ID
5297 struct drm_i915_private *dev_priv = dev->dev_private;
5298 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5299 struct intel_crtc *crtc;
5300
5301 /*
5302 * First get all needed power domains, then put all unneeded, to avoid
5303 * any unnecessary toggling of the power wells.
5304 */
d3fcc808 5305 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5306 enum intel_display_power_domain domain;
5307
83d65738 5308 if (!crtc->base.state->enable)
77d22dca
ID
5309 continue;
5310
319be8ae 5311 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5312
5313 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5314 intel_display_power_get(dev_priv, domain);
5315 }
5316
50f6e502 5317 if (dev_priv->display.modeset_global_resources)
679dacd4 5318 dev_priv->display.modeset_global_resources(state);
50f6e502 5319
d3fcc808 5320 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5321 enum intel_display_power_domain domain;
5322
5323 for_each_power_domain(domain, crtc->enabled_power_domains)
5324 intel_display_power_put(dev_priv, domain);
5325
5326 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5327 }
5328
5329 intel_display_set_init_power(dev_priv, false);
5330}
5331
f8437dd1
VK
5332void broxton_set_cdclk(struct drm_device *dev, int frequency)
5333{
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335 uint32_t divider;
5336 uint32_t ratio;
5337 uint32_t current_freq;
5338 int ret;
5339
5340 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5341 switch (frequency) {
5342 case 144000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 288000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 384000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 576000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 624000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5360 ratio = BXT_DE_PLL_RATIO(65);
5361 break;
5362 case 19200:
5363 /*
5364 * Bypass frequency with DE PLL disabled. Init ratio, divider
5365 * to suppress GCC warning.
5366 */
5367 ratio = 0;
5368 divider = 0;
5369 break;
5370 default:
5371 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5372
5373 return;
5374 }
5375
5376 mutex_lock(&dev_priv->rps.hw_lock);
5377 /* Inform power controller of upcoming frequency change */
5378 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5379 0x80000000);
5380 mutex_unlock(&dev_priv->rps.hw_lock);
5381
5382 if (ret) {
5383 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5384 ret, frequency);
5385 return;
5386 }
5387
5388 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5389 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5390 current_freq = current_freq * 500 + 1000;
5391
5392 /*
5393 * DE PLL has to be disabled when
5394 * - setting to 19.2MHz (bypass, PLL isn't used)
5395 * - before setting to 624MHz (PLL needs toggling)
5396 * - before setting to any frequency from 624MHz (PLL needs toggling)
5397 */
5398 if (frequency == 19200 || frequency == 624000 ||
5399 current_freq == 624000) {
5400 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5401 /* Timeout 200us */
5402 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5403 1))
5404 DRM_ERROR("timout waiting for DE PLL unlock\n");
5405 }
5406
5407 if (frequency != 19200) {
5408 uint32_t val;
5409
5410 val = I915_READ(BXT_DE_PLL_CTL);
5411 val &= ~BXT_DE_PLL_RATIO_MASK;
5412 val |= ratio;
5413 I915_WRITE(BXT_DE_PLL_CTL, val);
5414
5415 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5418 DRM_ERROR("timeout waiting for DE PLL lock\n");
5419
5420 val = I915_READ(CDCLK_CTL);
5421 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5422 val |= divider;
5423 /*
5424 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5425 * enable otherwise.
5426 */
5427 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5428 if (frequency >= 500000)
5429 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5430
5431 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5432 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5433 val |= (frequency - 1000) / 500;
5434 I915_WRITE(CDCLK_CTL, val);
5435 }
5436
5437 mutex_lock(&dev_priv->rps.hw_lock);
5438 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5439 DIV_ROUND_UP(frequency, 25000));
5440 mutex_unlock(&dev_priv->rps.hw_lock);
5441
5442 if (ret) {
5443 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5444 ret, frequency);
5445 return;
5446 }
5447
5448 dev_priv->cdclk_freq = frequency;
5449}
5450
5451void broxton_init_cdclk(struct drm_device *dev)
5452{
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 uint32_t val;
5455
5456 /*
5457 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5458 * or else the reset will hang because there is no PCH to respond.
5459 * Move the handshake programming to initialization sequence.
5460 * Previously was left up to BIOS.
5461 */
5462 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5463 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5464 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5465
5466 /* Enable PG1 for cdclk */
5467 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5468
5469 /* check if cd clock is enabled */
5470 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5471 DRM_DEBUG_KMS("Display already initialized\n");
5472 return;
5473 }
5474
5475 /*
5476 * FIXME:
5477 * - The initial CDCLK needs to be read from VBT.
5478 * Need to make this change after VBT has changes for BXT.
5479 * - check if setting the max (or any) cdclk freq is really necessary
5480 * here, it belongs to modeset time
5481 */
5482 broxton_set_cdclk(dev, 624000);
5483
5484 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5485 POSTING_READ(DBUF_CTL);
5486
f8437dd1
VK
5487 udelay(10);
5488
5489 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5490 DRM_ERROR("DBuf power enable timeout!\n");
5491}
5492
5493void broxton_uninit_cdclk(struct drm_device *dev)
5494{
5495 struct drm_i915_private *dev_priv = dev->dev_private;
5496
5497 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5498 POSTING_READ(DBUF_CTL);
5499
f8437dd1
VK
5500 udelay(10);
5501
5502 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5503 DRM_ERROR("DBuf power disable timeout!\n");
5504
5505 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5506 broxton_set_cdclk(dev, 19200);
5507
5508 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5509}
5510
dfcab17e 5511/* returns HPLL frequency in kHz */
f8bf63fd 5512static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5513{
586f49dc 5514 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5515
586f49dc
JB
5516 /* Obtain SKU information */
5517 mutex_lock(&dev_priv->dpio_lock);
5518 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5519 CCK_FUSE_HPLL_FREQ_MASK;
5520 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 5521
dfcab17e 5522 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5523}
5524
f8bf63fd
VS
5525static void vlv_update_cdclk(struct drm_device *dev)
5526{
5527 struct drm_i915_private *dev_priv = dev->dev_private;
5528
164dfd28 5529 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 5530 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
164dfd28 5531 dev_priv->cdclk_freq);
f8bf63fd
VS
5532
5533 /*
5534 * Program the gmbus_freq based on the cdclk frequency.
5535 * BSpec erroneously claims we should aim for 4MHz, but
5536 * in fact 1MHz is the correct frequency.
5537 */
164dfd28 5538 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
f8bf63fd
VS
5539}
5540
30a970c6
JB
5541/* Adjust CDclk dividers to allow high res or save power if possible */
5542static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5543{
5544 struct drm_i915_private *dev_priv = dev->dev_private;
5545 u32 val, cmd;
5546
164dfd28
VK
5547 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5548 != dev_priv->cdclk_freq);
d60c4473 5549
dfcab17e 5550 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5551 cmd = 2;
dfcab17e 5552 else if (cdclk == 266667)
30a970c6
JB
5553 cmd = 1;
5554 else
5555 cmd = 0;
5556
5557 mutex_lock(&dev_priv->rps.hw_lock);
5558 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5559 val &= ~DSPFREQGUAR_MASK;
5560 val |= (cmd << DSPFREQGUAR_SHIFT);
5561 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5562 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5563 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5564 50)) {
5565 DRM_ERROR("timed out waiting for CDclk change\n");
5566 }
5567 mutex_unlock(&dev_priv->rps.hw_lock);
5568
dfcab17e 5569 if (cdclk == 400000) {
6bcda4f0 5570 u32 divider;
30a970c6 5571
6bcda4f0 5572 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
5573
5574 mutex_lock(&dev_priv->dpio_lock);
5575 /* adjust cdclk divider */
5576 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5577 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5578 val |= divider;
5579 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5580
5581 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5582 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5583 50))
5584 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5585 mutex_unlock(&dev_priv->dpio_lock);
5586 }
5587
5588 mutex_lock(&dev_priv->dpio_lock);
5589 /* adjust self-refresh exit latency value */
5590 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5591 val &= ~0x7f;
5592
5593 /*
5594 * For high bandwidth configs, we set a higher latency in the bunit
5595 * so that the core display fetch happens in time to avoid underruns.
5596 */
dfcab17e 5597 if (cdclk == 400000)
30a970c6
JB
5598 val |= 4500 / 250; /* 4.5 usec */
5599 else
5600 val |= 3000 / 250; /* 3.0 usec */
5601 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5602 mutex_unlock(&dev_priv->dpio_lock);
5603
f8bf63fd 5604 vlv_update_cdclk(dev);
30a970c6
JB
5605}
5606
383c5a6a
VS
5607static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5608{
5609 struct drm_i915_private *dev_priv = dev->dev_private;
5610 u32 val, cmd;
5611
164dfd28
VK
5612 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5613 != dev_priv->cdclk_freq);
383c5a6a
VS
5614
5615 switch (cdclk) {
383c5a6a
VS
5616 case 333333:
5617 case 320000:
383c5a6a 5618 case 266667:
383c5a6a 5619 case 200000:
383c5a6a
VS
5620 break;
5621 default:
5f77eeb0 5622 MISSING_CASE(cdclk);
383c5a6a
VS
5623 return;
5624 }
5625
9d0d3fda
VS
5626 /*
5627 * Specs are full of misinformation, but testing on actual
5628 * hardware has shown that we just need to write the desired
5629 * CCK divider into the Punit register.
5630 */
5631 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5632
383c5a6a
VS
5633 mutex_lock(&dev_priv->rps.hw_lock);
5634 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5635 val &= ~DSPFREQGUAR_MASK_CHV;
5636 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5637 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5638 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5639 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5640 50)) {
5641 DRM_ERROR("timed out waiting for CDclk change\n");
5642 }
5643 mutex_unlock(&dev_priv->rps.hw_lock);
5644
5645 vlv_update_cdclk(dev);
5646}
5647
30a970c6
JB
5648static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5649 int max_pixclk)
5650{
6bcda4f0 5651 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5652 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5653
30a970c6
JB
5654 /*
5655 * Really only a few cases to deal with, as only 4 CDclks are supported:
5656 * 200MHz
5657 * 267MHz
29dc7ef3 5658 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5659 * 400MHz (VLV only)
5660 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5661 * of the lower bin and adjust if needed.
e37c67a1
VS
5662 *
5663 * We seem to get an unstable or solid color picture at 200MHz.
5664 * Not sure what's wrong. For now use 200MHz only when all pipes
5665 * are off.
30a970c6 5666 */
6cca3195
VS
5667 if (!IS_CHERRYVIEW(dev_priv) &&
5668 max_pixclk > freq_320*limit/100)
dfcab17e 5669 return 400000;
6cca3195 5670 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5671 return freq_320;
e37c67a1 5672 else if (max_pixclk > 0)
dfcab17e 5673 return 266667;
e37c67a1
VS
5674 else
5675 return 200000;
30a970c6
JB
5676}
5677
f8437dd1
VK
5678static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5679 int max_pixclk)
5680{
5681 /*
5682 * FIXME:
5683 * - remove the guardband, it's not needed on BXT
5684 * - set 19.2MHz bypass frequency if there are no active pipes
5685 */
5686 if (max_pixclk > 576000*9/10)
5687 return 624000;
5688 else if (max_pixclk > 384000*9/10)
5689 return 576000;
5690 else if (max_pixclk > 288000*9/10)
5691 return 384000;
5692 else if (max_pixclk > 144000*9/10)
5693 return 288000;
5694 else
5695 return 144000;
5696}
5697
2f2d7aa1 5698/* compute the max pixel clock for new configuration */
304603f4 5699static int intel_mode_max_pixclk(struct drm_atomic_state *state)
30a970c6 5700{
304603f4 5701 struct drm_device *dev = state->dev;
30a970c6 5702 struct intel_crtc *intel_crtc;
304603f4 5703 struct intel_crtc_state *crtc_state;
30a970c6
JB
5704 int max_pixclk = 0;
5705
d3fcc808 5706 for_each_intel_crtc(dev, intel_crtc) {
304603f4
ACO
5707 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5708 if (IS_ERR(crtc_state))
5709 return PTR_ERR(crtc_state);
5710
5711 if (!crtc_state->base.enable)
5712 continue;
5713
5714 max_pixclk = max(max_pixclk,
5715 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5716 }
5717
5718 return max_pixclk;
5719}
5720
304603f4 5721static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
2f2d7aa1 5722 unsigned *prepare_pipes)
30a970c6 5723{
304603f4 5724 struct drm_i915_private *dev_priv = to_i915(state->dev);
30a970c6 5725 struct intel_crtc *intel_crtc;
304603f4 5726 int max_pixclk = intel_mode_max_pixclk(state);
f8437dd1 5727 int cdclk;
30a970c6 5728
304603f4
ACO
5729 if (max_pixclk < 0)
5730 return max_pixclk;
30a970c6 5731
f8437dd1
VK
5732 if (IS_VALLEYVIEW(dev_priv))
5733 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5734 else
5735 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5736
5737 if (cdclk == dev_priv->cdclk_freq)
304603f4 5738 return 0;
30a970c6 5739
2f2d7aa1 5740 /* disable/enable all currently active pipes while we change cdclk */
304603f4 5741 for_each_intel_crtc(state->dev, intel_crtc)
83d65738 5742 if (intel_crtc->base.state->enable)
30a970c6 5743 *prepare_pipes |= (1 << intel_crtc->pipe);
304603f4
ACO
5744
5745 return 0;
30a970c6
JB
5746}
5747
1e69cd74
VS
5748static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5749{
5750 unsigned int credits, default_credits;
5751
5752 if (IS_CHERRYVIEW(dev_priv))
5753 default_credits = PFI_CREDIT(12);
5754 else
5755 default_credits = PFI_CREDIT(8);
5756
164dfd28 5757 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5758 /* CHV suggested value is 31 or 63 */
5759 if (IS_CHERRYVIEW(dev_priv))
5760 credits = PFI_CREDIT_31;
5761 else
5762 credits = PFI_CREDIT(15);
5763 } else {
5764 credits = default_credits;
5765 }
5766
5767 /*
5768 * WA - write default credits before re-programming
5769 * FIXME: should we also set the resend bit here?
5770 */
5771 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5772 default_credits);
5773
5774 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5775 credits | PFI_CREDIT_RESEND);
5776
5777 /*
5778 * FIXME is this guaranteed to clear
5779 * immediately or should we poll for it?
5780 */
5781 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5782}
5783
679dacd4 5784static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
30a970c6 5785{
679dacd4 5786 struct drm_device *dev = state->dev;
30a970c6 5787 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4
ACO
5788 int max_pixclk = intel_mode_max_pixclk(state);
5789 int req_cdclk;
5790
5791 /* The only reason this can fail is if we fail to add the crtc_state
5792 * to the atomic state. But that can't happen since the call to
5793 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5794 * can't have failed otherwise the mode set would be aborted) added all
5795 * the states already. */
5796 if (WARN_ON(max_pixclk < 0))
5797 return;
30a970c6 5798
304603f4 5799 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5800
164dfd28 5801 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5802 /*
5803 * FIXME: We can end up here with all power domains off, yet
5804 * with a CDCLK frequency other than the minimum. To account
5805 * for this take the PIPE-A power domain, which covers the HW
5806 * blocks needed for the following programming. This can be
5807 * removed once it's guaranteed that we get here either with
5808 * the minimum CDCLK set, or the required power domains
5809 * enabled.
5810 */
5811 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5812
383c5a6a
VS
5813 if (IS_CHERRYVIEW(dev))
5814 cherryview_set_cdclk(dev, req_cdclk);
5815 else
5816 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5817
1e69cd74
VS
5818 vlv_program_pfi_credits(dev_priv);
5819
738c05c0 5820 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5821 }
30a970c6
JB
5822}
5823
89b667f8
JB
5824static void valleyview_crtc_enable(struct drm_crtc *crtc)
5825{
5826 struct drm_device *dev = crtc->dev;
a72e4c9f 5827 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5829 struct intel_encoder *encoder;
5830 int pipe = intel_crtc->pipe;
23538ef1 5831 bool is_dsi;
89b667f8 5832
83d65738 5833 WARN_ON(!crtc->state->enable);
89b667f8
JB
5834
5835 if (intel_crtc->active)
5836 return;
5837
409ee761 5838 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5839
1ae0d137
VS
5840 if (!is_dsi) {
5841 if (IS_CHERRYVIEW(dev))
6e3c9717 5842 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5843 else
6e3c9717 5844 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5845 }
5b18e57c 5846
6e3c9717 5847 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5848 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5849
5850 intel_set_pipe_timings(intel_crtc);
5851
c14b0485
VS
5852 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5853 struct drm_i915_private *dev_priv = dev->dev_private;
5854
5855 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5856 I915_WRITE(CHV_CANVAS(pipe), 0);
5857 }
5858
5b18e57c
DV
5859 i9xx_set_pipeconf(intel_crtc);
5860
89b667f8 5861 intel_crtc->active = true;
89b667f8 5862
a72e4c9f 5863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5864
89b667f8
JB
5865 for_each_encoder_on_crtc(dev, crtc, encoder)
5866 if (encoder->pre_pll_enable)
5867 encoder->pre_pll_enable(encoder);
5868
9d556c99
CML
5869 if (!is_dsi) {
5870 if (IS_CHERRYVIEW(dev))
6e3c9717 5871 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5872 else
6e3c9717 5873 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5874 }
89b667f8
JB
5875
5876 for_each_encoder_on_crtc(dev, crtc, encoder)
5877 if (encoder->pre_enable)
5878 encoder->pre_enable(encoder);
5879
2dd24552
JB
5880 i9xx_pfit_enable(intel_crtc);
5881
63cbb074
VS
5882 intel_crtc_load_lut(crtc);
5883
f37fcc2a 5884 intel_update_watermarks(crtc);
e1fdc473 5885 intel_enable_pipe(intel_crtc);
be6a6f8e 5886
4b3a9526
VS
5887 assert_vblank_disabled(crtc);
5888 drm_crtc_vblank_on(crtc);
5889
f9b61ff6
DV
5890 for_each_encoder_on_crtc(dev, crtc, encoder)
5891 encoder->enable(encoder);
5892
9ab0460b 5893 intel_crtc_enable_planes(crtc);
d40d9187 5894
56b80e1f 5895 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5896 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
5897}
5898
f13c2ef3
DV
5899static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5900{
5901 struct drm_device *dev = crtc->base.dev;
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903
6e3c9717
ACO
5904 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5905 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5906}
5907
0b8765c6 5908static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5909{
5910 struct drm_device *dev = crtc->dev;
a72e4c9f 5911 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5913 struct intel_encoder *encoder;
79e53945 5914 int pipe = intel_crtc->pipe;
79e53945 5915
83d65738 5916 WARN_ON(!crtc->state->enable);
08a48469 5917
f7abfe8b
CW
5918 if (intel_crtc->active)
5919 return;
5920
f13c2ef3
DV
5921 i9xx_set_pll_dividers(intel_crtc);
5922
6e3c9717 5923 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5924 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5925
5926 intel_set_pipe_timings(intel_crtc);
5927
5b18e57c
DV
5928 i9xx_set_pipeconf(intel_crtc);
5929
f7abfe8b 5930 intel_crtc->active = true;
6b383a7f 5931
4a3436e8 5932 if (!IS_GEN2(dev))
a72e4c9f 5933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5934
9d6d9f19
MK
5935 for_each_encoder_on_crtc(dev, crtc, encoder)
5936 if (encoder->pre_enable)
5937 encoder->pre_enable(encoder);
5938
f6736a1a
DV
5939 i9xx_enable_pll(intel_crtc);
5940
2dd24552
JB
5941 i9xx_pfit_enable(intel_crtc);
5942
63cbb074
VS
5943 intel_crtc_load_lut(crtc);
5944
f37fcc2a 5945 intel_update_watermarks(crtc);
e1fdc473 5946 intel_enable_pipe(intel_crtc);
be6a6f8e 5947
4b3a9526
VS
5948 assert_vblank_disabled(crtc);
5949 drm_crtc_vblank_on(crtc);
5950
f9b61ff6
DV
5951 for_each_encoder_on_crtc(dev, crtc, encoder)
5952 encoder->enable(encoder);
5953
9ab0460b 5954 intel_crtc_enable_planes(crtc);
d40d9187 5955
4a3436e8
VS
5956 /*
5957 * Gen2 reports pipe underruns whenever all planes are disabled.
5958 * So don't enable underrun reporting before at least some planes
5959 * are enabled.
5960 * FIXME: Need to fix the logic to work when we turn off all planes
5961 * but leave the pipe running.
5962 */
5963 if (IS_GEN2(dev))
a72e4c9f 5964 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5965
56b80e1f 5966 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5967 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 5968}
79e53945 5969
87476d63
DV
5970static void i9xx_pfit_disable(struct intel_crtc *crtc)
5971{
5972 struct drm_device *dev = crtc->base.dev;
5973 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5974
6e3c9717 5975 if (!crtc->config->gmch_pfit.control)
328d8e82 5976 return;
87476d63 5977
328d8e82 5978 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5979
328d8e82
DV
5980 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5981 I915_READ(PFIT_CONTROL));
5982 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5983}
5984
0b8765c6
JB
5985static void i9xx_crtc_disable(struct drm_crtc *crtc)
5986{
5987 struct drm_device *dev = crtc->dev;
5988 struct drm_i915_private *dev_priv = dev->dev_private;
5989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5990 struct intel_encoder *encoder;
0b8765c6 5991 int pipe = intel_crtc->pipe;
ef9c3aee 5992
f7abfe8b
CW
5993 if (!intel_crtc->active)
5994 return;
5995
4a3436e8
VS
5996 /*
5997 * Gen2 reports pipe underruns whenever all planes are disabled.
5998 * So diasble underrun reporting before all the planes get disabled.
5999 * FIXME: Need to fix the logic to work when we turn off all planes
6000 * but leave the pipe running.
6001 */
6002 if (IS_GEN2(dev))
a72e4c9f 6003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6004
564ed191
ID
6005 /*
6006 * Vblank time updates from the shadow to live plane control register
6007 * are blocked if the memory self-refresh mode is active at that
6008 * moment. So to make sure the plane gets truly disabled, disable
6009 * first the self-refresh mode. The self-refresh enable bit in turn
6010 * will be checked/applied by the HW only at the next frame start
6011 * event which is after the vblank start event, so we need to have a
6012 * wait-for-vblank between disabling the plane and the pipe.
6013 */
6014 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
6015 intel_crtc_disable_planes(crtc);
6016
6304cd91
VS
6017 /*
6018 * On gen2 planes are double buffered but the pipe isn't, so we must
6019 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6020 * We also need to wait on all gmch platforms because of the
6021 * self-refresh mode constraint explained above.
6304cd91 6022 */
564ed191 6023 intel_wait_for_vblank(dev, pipe);
6304cd91 6024
4b3a9526
VS
6025 for_each_encoder_on_crtc(dev, crtc, encoder)
6026 encoder->disable(encoder);
6027
f9b61ff6
DV
6028 drm_crtc_vblank_off(crtc);
6029 assert_vblank_disabled(crtc);
6030
575f7ab7 6031 intel_disable_pipe(intel_crtc);
24a1f16d 6032
87476d63 6033 i9xx_pfit_disable(intel_crtc);
24a1f16d 6034
89b667f8
JB
6035 for_each_encoder_on_crtc(dev, crtc, encoder)
6036 if (encoder->post_disable)
6037 encoder->post_disable(encoder);
6038
409ee761 6039 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6040 if (IS_CHERRYVIEW(dev))
6041 chv_disable_pll(dev_priv, pipe);
6042 else if (IS_VALLEYVIEW(dev))
6043 vlv_disable_pll(dev_priv, pipe);
6044 else
1c4e0274 6045 i9xx_disable_pll(intel_crtc);
076ed3b2 6046 }
0b8765c6 6047
4a3436e8 6048 if (!IS_GEN2(dev))
a72e4c9f 6049 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6050
f7abfe8b 6051 intel_crtc->active = false;
46ba614c 6052 intel_update_watermarks(crtc);
f37fcc2a 6053
efa9624e 6054 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6055 intel_fbc_update(dev);
efa9624e 6056 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6057}
6058
ee7b9f93
JB
6059static void i9xx_crtc_off(struct drm_crtc *crtc)
6060{
6061}
6062
b04c5bd6
BF
6063/* Master function to enable/disable CRTC and corresponding power wells */
6064void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6065{
6066 struct drm_device *dev = crtc->dev;
6067 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 6068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
6069 enum intel_display_power_domain domain;
6070 unsigned long domains;
976f8a20 6071
0e572fe7
DV
6072 if (enable) {
6073 if (!intel_crtc->active) {
e1e9fb84
DV
6074 domains = get_crtc_power_domains(crtc);
6075 for_each_power_domain(domain, domains)
6076 intel_display_power_get(dev_priv, domain);
6077 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
6078
6079 dev_priv->display.crtc_enable(crtc);
6080 }
6081 } else {
6082 if (intel_crtc->active) {
6083 dev_priv->display.crtc_disable(crtc);
6084
e1e9fb84
DV
6085 domains = intel_crtc->enabled_power_domains;
6086 for_each_power_domain(domain, domains)
6087 intel_display_power_put(dev_priv, domain);
6088 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
6089 }
6090 }
b04c5bd6
BF
6091}
6092
6093/**
6094 * Sets the power management mode of the pipe and plane.
6095 */
6096void intel_crtc_update_dpms(struct drm_crtc *crtc)
6097{
6098 struct drm_device *dev = crtc->dev;
6099 struct intel_encoder *intel_encoder;
6100 bool enable = false;
6101
6102 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6103 enable |= intel_encoder->connectors_active;
6104
6105 intel_crtc_control(crtc, enable);
976f8a20
DV
6106}
6107
cdd59983
CW
6108static void intel_crtc_disable(struct drm_crtc *crtc)
6109{
cdd59983 6110 struct drm_device *dev = crtc->dev;
976f8a20 6111 struct drm_connector *connector;
ee7b9f93 6112 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 6113
976f8a20 6114 /* crtc should still be enabled when we disable it. */
83d65738 6115 WARN_ON(!crtc->state->enable);
976f8a20
DV
6116
6117 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
6118 dev_priv->display.off(crtc);
6119
70a101f8 6120 drm_plane_helper_disable(crtc->primary);
976f8a20
DV
6121
6122 /* Update computed state. */
6123 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6124 if (!connector->encoder || !connector->encoder->crtc)
6125 continue;
6126
6127 if (connector->encoder->crtc != crtc)
6128 continue;
6129
6130 connector->dpms = DRM_MODE_DPMS_OFF;
6131 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
6132 }
6133}
6134
ea5b213a 6135void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6136{
4ef69c7a 6137 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6138
ea5b213a
CW
6139 drm_encoder_cleanup(encoder);
6140 kfree(intel_encoder);
7e7d76c3
JB
6141}
6142
9237329d 6143/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6144 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6145 * state of the entire output pipe. */
9237329d 6146static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6147{
5ab432ef
DV
6148 if (mode == DRM_MODE_DPMS_ON) {
6149 encoder->connectors_active = true;
6150
b2cabb0e 6151 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6152 } else {
6153 encoder->connectors_active = false;
6154
b2cabb0e 6155 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6156 }
79e53945
JB
6157}
6158
0a91ca29
DV
6159/* Cross check the actual hw state with our own modeset state tracking (and it's
6160 * internal consistency). */
b980514c 6161static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6162{
0a91ca29
DV
6163 if (connector->get_hw_state(connector)) {
6164 struct intel_encoder *encoder = connector->encoder;
6165 struct drm_crtc *crtc;
6166 bool encoder_enabled;
6167 enum pipe pipe;
6168
6169 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6170 connector->base.base.id,
c23cc417 6171 connector->base.name);
0a91ca29 6172
0e32b39c
DA
6173 /* there is no real hw state for MST connectors */
6174 if (connector->mst_port)
6175 return;
6176
e2c719b7 6177 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6178 "wrong connector dpms state\n");
e2c719b7 6179 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6180 "active connector not linked to encoder\n");
0a91ca29 6181
36cd7444 6182 if (encoder) {
e2c719b7 6183 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6184 "encoder->connectors_active not set\n");
6185
6186 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6187 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6188 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6189 return;
0a91ca29 6190
36cd7444 6191 crtc = encoder->base.crtc;
0a91ca29 6192
83d65738
MR
6193 I915_STATE_WARN(!crtc->state->enable,
6194 "crtc not enabled\n");
e2c719b7
RC
6195 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6196 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6197 "encoder active on the wrong pipe\n");
6198 }
0a91ca29 6199 }
79e53945
JB
6200}
6201
08d9bc92
ACO
6202int intel_connector_init(struct intel_connector *connector)
6203{
6204 struct drm_connector_state *connector_state;
6205
6206 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6207 if (!connector_state)
6208 return -ENOMEM;
6209
6210 connector->base.state = connector_state;
6211 return 0;
6212}
6213
6214struct intel_connector *intel_connector_alloc(void)
6215{
6216 struct intel_connector *connector;
6217
6218 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6219 if (!connector)
6220 return NULL;
6221
6222 if (intel_connector_init(connector) < 0) {
6223 kfree(connector);
6224 return NULL;
6225 }
6226
6227 return connector;
6228}
6229
5ab432ef
DV
6230/* Even simpler default implementation, if there's really no special case to
6231 * consider. */
6232void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6233{
5ab432ef
DV
6234 /* All the simple cases only support two dpms states. */
6235 if (mode != DRM_MODE_DPMS_ON)
6236 mode = DRM_MODE_DPMS_OFF;
d4270e57 6237
5ab432ef
DV
6238 if (mode == connector->dpms)
6239 return;
6240
6241 connector->dpms = mode;
6242
6243 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6244 if (connector->encoder)
6245 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6246
b980514c 6247 intel_modeset_check_state(connector->dev);
79e53945
JB
6248}
6249
f0947c37
DV
6250/* Simple connector->get_hw_state implementation for encoders that support only
6251 * one connector and no cloning and hence the encoder state determines the state
6252 * of the connector. */
6253bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6254{
24929352 6255 enum pipe pipe = 0;
f0947c37 6256 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6257
f0947c37 6258 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6259}
6260
6d293983 6261static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6262{
6d293983
ACO
6263 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6264 return crtc_state->fdi_lanes;
d272ddfa
VS
6265
6266 return 0;
6267}
6268
6d293983 6269static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6270 struct intel_crtc_state *pipe_config)
1857e1da 6271{
6d293983
ACO
6272 struct drm_atomic_state *state = pipe_config->base.state;
6273 struct intel_crtc *other_crtc;
6274 struct intel_crtc_state *other_crtc_state;
6275
1857e1da
DV
6276 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6277 pipe_name(pipe), pipe_config->fdi_lanes);
6278 if (pipe_config->fdi_lanes > 4) {
6279 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6280 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6281 return -EINVAL;
1857e1da
DV
6282 }
6283
bafb6553 6284 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6285 if (pipe_config->fdi_lanes > 2) {
6286 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6287 pipe_config->fdi_lanes);
6d293983 6288 return -EINVAL;
1857e1da 6289 } else {
6d293983 6290 return 0;
1857e1da
DV
6291 }
6292 }
6293
6294 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6295 return 0;
1857e1da
DV
6296
6297 /* Ivybridge 3 pipe is really complicated */
6298 switch (pipe) {
6299 case PIPE_A:
6d293983 6300 return 0;
1857e1da 6301 case PIPE_B:
6d293983
ACO
6302 if (pipe_config->fdi_lanes <= 2)
6303 return 0;
6304
6305 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6306 other_crtc_state =
6307 intel_atomic_get_crtc_state(state, other_crtc);
6308 if (IS_ERR(other_crtc_state))
6309 return PTR_ERR(other_crtc_state);
6310
6311 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6312 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6313 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6314 return -EINVAL;
1857e1da 6315 }
6d293983 6316 return 0;
1857e1da 6317 case PIPE_C:
251cc67c
VS
6318 if (pipe_config->fdi_lanes > 2) {
6319 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6320 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6321 return -EINVAL;
251cc67c 6322 }
6d293983
ACO
6323
6324 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6325 other_crtc_state =
6326 intel_atomic_get_crtc_state(state, other_crtc);
6327 if (IS_ERR(other_crtc_state))
6328 return PTR_ERR(other_crtc_state);
6329
6330 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6331 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6332 return -EINVAL;
1857e1da 6333 }
6d293983 6334 return 0;
1857e1da
DV
6335 default:
6336 BUG();
6337 }
6338}
6339
e29c22c0
DV
6340#define RETRY 1
6341static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6342 struct intel_crtc_state *pipe_config)
877d48d5 6343{
1857e1da 6344 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6345 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6346 int lane, link_bw, fdi_dotclock, ret;
6347 bool needs_recompute = false;
877d48d5 6348
e29c22c0 6349retry:
877d48d5
DV
6350 /* FDI is a binary signal running at ~2.7GHz, encoding
6351 * each output octet as 10 bits. The actual frequency
6352 * is stored as a divider into a 100MHz clock, and the
6353 * mode pixel clock is stored in units of 1KHz.
6354 * Hence the bw of each lane in terms of the mode signal
6355 * is:
6356 */
6357 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6358
241bfc38 6359 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6360
2bd89a07 6361 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6362 pipe_config->pipe_bpp);
6363
6364 pipe_config->fdi_lanes = lane;
6365
2bd89a07 6366 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6367 link_bw, &pipe_config->fdi_m_n);
1857e1da 6368
6d293983
ACO
6369 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6370 intel_crtc->pipe, pipe_config);
6371 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6372 pipe_config->pipe_bpp -= 2*3;
6373 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6374 pipe_config->pipe_bpp);
6375 needs_recompute = true;
6376 pipe_config->bw_constrained = true;
6377
6378 goto retry;
6379 }
6380
6381 if (needs_recompute)
6382 return RETRY;
6383
6d293983 6384 return ret;
877d48d5
DV
6385}
6386
42db64ef 6387static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6388 struct intel_crtc_state *pipe_config)
42db64ef 6389{
d330a953 6390 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 6391 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 6392 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
6393}
6394
a43f6e0f 6395static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6396 struct intel_crtc_state *pipe_config)
79e53945 6397{
a43f6e0f 6398 struct drm_device *dev = crtc->base.dev;
8bd31e67 6399 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6400 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6401 int ret;
89749350 6402
ad3a4479 6403 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6404 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
6405 int clock_limit =
6406 dev_priv->display.get_display_clock_speed(dev);
6407
6408 /*
6409 * Enable pixel doubling when the dot clock
6410 * is > 90% of the (display) core speed.
6411 *
b397c96b
VS
6412 * GDG double wide on either pipe,
6413 * otherwise pipe A only.
cf532bb2 6414 */
b397c96b 6415 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6416 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6417 clock_limit *= 2;
cf532bb2 6418 pipe_config->double_wide = true;
ad3a4479
VS
6419 }
6420
241bfc38 6421 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6422 return -EINVAL;
2c07245f 6423 }
89749350 6424
1d1d0e27
VS
6425 /*
6426 * Pipe horizontal size must be even in:
6427 * - DVO ganged mode
6428 * - LVDS dual channel mode
6429 * - Double wide pipe
6430 */
a93e255f 6431 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6432 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6433 pipe_config->pipe_src_w &= ~1;
6434
8693a824
DL
6435 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6436 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6437 */
6438 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6439 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6440 return -EINVAL;
44f46b42 6441
f5adf94e 6442 if (HAS_IPS(dev))
a43f6e0f
DV
6443 hsw_compute_ips_config(crtc, pipe_config);
6444
877d48d5 6445 if (pipe_config->has_pch_encoder)
a43f6e0f 6446 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6447
d03c93d4
CK
6448 /* FIXME: remove below call once atomic mode set is place and all crtc
6449 * related checks called from atomic_crtc_check function */
6450 ret = 0;
6451 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6452 crtc, pipe_config->base.state);
6453 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6454
6455 return ret;
79e53945
JB
6456}
6457
1652d19e
VS
6458static int skylake_get_display_clock_speed(struct drm_device *dev)
6459{
6460 struct drm_i915_private *dev_priv = to_i915(dev);
6461 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6462 uint32_t cdctl = I915_READ(CDCLK_CTL);
6463 uint32_t linkrate;
6464
6465 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6466 WARN(1, "LCPLL1 not enabled\n");
6467 return 24000; /* 24MHz is the cd freq with NSSC ref */
6468 }
6469
6470 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6471 return 540000;
6472
6473 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6474 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6475
71cd8423
DL
6476 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6477 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6478 /* vco 8640 */
6479 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6480 case CDCLK_FREQ_450_432:
6481 return 432000;
6482 case CDCLK_FREQ_337_308:
6483 return 308570;
6484 case CDCLK_FREQ_675_617:
6485 return 617140;
6486 default:
6487 WARN(1, "Unknown cd freq selection\n");
6488 }
6489 } else {
6490 /* vco 8100 */
6491 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6492 case CDCLK_FREQ_450_432:
6493 return 450000;
6494 case CDCLK_FREQ_337_308:
6495 return 337500;
6496 case CDCLK_FREQ_675_617:
6497 return 675000;
6498 default:
6499 WARN(1, "Unknown cd freq selection\n");
6500 }
6501 }
6502
6503 /* error case, do as if DPLL0 isn't enabled */
6504 return 24000;
6505}
6506
6507static int broadwell_get_display_clock_speed(struct drm_device *dev)
6508{
6509 struct drm_i915_private *dev_priv = dev->dev_private;
6510 uint32_t lcpll = I915_READ(LCPLL_CTL);
6511 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6512
6513 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6514 return 800000;
6515 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6516 return 450000;
6517 else if (freq == LCPLL_CLK_FREQ_450)
6518 return 450000;
6519 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6520 return 540000;
6521 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6522 return 337500;
6523 else
6524 return 675000;
6525}
6526
6527static int haswell_get_display_clock_speed(struct drm_device *dev)
6528{
6529 struct drm_i915_private *dev_priv = dev->dev_private;
6530 uint32_t lcpll = I915_READ(LCPLL_CTL);
6531 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6532
6533 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6534 return 800000;
6535 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6536 return 450000;
6537 else if (freq == LCPLL_CLK_FREQ_450)
6538 return 450000;
6539 else if (IS_HSW_ULT(dev))
6540 return 337500;
6541 else
6542 return 540000;
79e53945
JB
6543}
6544
25eb05fc
JB
6545static int valleyview_get_display_clock_speed(struct drm_device *dev)
6546{
d197b7d3 6547 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6548 u32 val;
6549 int divider;
6550
6bcda4f0
VS
6551 if (dev_priv->hpll_freq == 0)
6552 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6553
d197b7d3
VS
6554 mutex_lock(&dev_priv->dpio_lock);
6555 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6556 mutex_unlock(&dev_priv->dpio_lock);
6557
6558 divider = val & DISPLAY_FREQUENCY_VALUES;
6559
7d007f40
VS
6560 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6561 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6562 "cdclk change in progress\n");
6563
6bcda4f0 6564 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6565}
6566
b37a6434
VS
6567static int ilk_get_display_clock_speed(struct drm_device *dev)
6568{
6569 return 450000;
6570}
6571
e70236a8
JB
6572static int i945_get_display_clock_speed(struct drm_device *dev)
6573{
6574 return 400000;
6575}
79e53945 6576
e70236a8 6577static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6578{
e907f170 6579 return 333333;
e70236a8 6580}
79e53945 6581
e70236a8
JB
6582static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6583{
6584 return 200000;
6585}
79e53945 6586
257a7ffc
DV
6587static int pnv_get_display_clock_speed(struct drm_device *dev)
6588{
6589 u16 gcfgc = 0;
6590
6591 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6592
6593 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6594 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6595 return 266667;
257a7ffc 6596 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6597 return 333333;
257a7ffc 6598 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6599 return 444444;
257a7ffc
DV
6600 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6601 return 200000;
6602 default:
6603 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6604 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6605 return 133333;
257a7ffc 6606 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6607 return 166667;
257a7ffc
DV
6608 }
6609}
6610
e70236a8
JB
6611static int i915gm_get_display_clock_speed(struct drm_device *dev)
6612{
6613 u16 gcfgc = 0;
79e53945 6614
e70236a8
JB
6615 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6616
6617 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6618 return 133333;
e70236a8
JB
6619 else {
6620 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6621 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6622 return 333333;
e70236a8
JB
6623 default:
6624 case GC_DISPLAY_CLOCK_190_200_MHZ:
6625 return 190000;
79e53945 6626 }
e70236a8
JB
6627 }
6628}
6629
6630static int i865_get_display_clock_speed(struct drm_device *dev)
6631{
e907f170 6632 return 266667;
e70236a8
JB
6633}
6634
6635static int i855_get_display_clock_speed(struct drm_device *dev)
6636{
6637 u16 hpllcc = 0;
6638 /* Assume that the hardware is in the high speed state. This
6639 * should be the default.
6640 */
6641 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6642 case GC_CLOCK_133_200:
6643 case GC_CLOCK_100_200:
6644 return 200000;
6645 case GC_CLOCK_166_250:
6646 return 250000;
6647 case GC_CLOCK_100_133:
e907f170 6648 return 133333;
e70236a8 6649 }
79e53945 6650
e70236a8
JB
6651 /* Shouldn't happen */
6652 return 0;
6653}
79e53945 6654
e70236a8
JB
6655static int i830_get_display_clock_speed(struct drm_device *dev)
6656{
e907f170 6657 return 133333;
79e53945
JB
6658}
6659
2c07245f 6660static void
a65851af 6661intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6662{
a65851af
VS
6663 while (*num > DATA_LINK_M_N_MASK ||
6664 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6665 *num >>= 1;
6666 *den >>= 1;
6667 }
6668}
6669
a65851af
VS
6670static void compute_m_n(unsigned int m, unsigned int n,
6671 uint32_t *ret_m, uint32_t *ret_n)
6672{
6673 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6674 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6675 intel_reduce_m_n_ratio(ret_m, ret_n);
6676}
6677
e69d0bc1
DV
6678void
6679intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6680 int pixel_clock, int link_clock,
6681 struct intel_link_m_n *m_n)
2c07245f 6682{
e69d0bc1 6683 m_n->tu = 64;
a65851af
VS
6684
6685 compute_m_n(bits_per_pixel * pixel_clock,
6686 link_clock * nlanes * 8,
6687 &m_n->gmch_m, &m_n->gmch_n);
6688
6689 compute_m_n(pixel_clock, link_clock,
6690 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6691}
6692
a7615030
CW
6693static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6694{
d330a953
JN
6695 if (i915.panel_use_ssc >= 0)
6696 return i915.panel_use_ssc != 0;
41aa3448 6697 return dev_priv->vbt.lvds_use_ssc
435793df 6698 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6699}
6700
a93e255f
ACO
6701static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6702 int num_connectors)
c65d77d8 6703{
a93e255f 6704 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
6705 struct drm_i915_private *dev_priv = dev->dev_private;
6706 int refclk;
6707
a93e255f
ACO
6708 WARN_ON(!crtc_state->base.state);
6709
5ab7b0b7 6710 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 6711 refclk = 100000;
a93e255f 6712 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 6713 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
6714 refclk = dev_priv->vbt.lvds_ssc_freq;
6715 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
6716 } else if (!IS_GEN2(dev)) {
6717 refclk = 96000;
6718 } else {
6719 refclk = 48000;
6720 }
6721
6722 return refclk;
6723}
6724
7429e9d4 6725static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6726{
7df00d7a 6727 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6728}
f47709a9 6729
7429e9d4
DV
6730static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6731{
6732 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6733}
6734
f47709a9 6735static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6736 struct intel_crtc_state *crtc_state,
a7516a05
JB
6737 intel_clock_t *reduced_clock)
6738{
f47709a9 6739 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
6740 u32 fp, fp2 = 0;
6741
6742 if (IS_PINEVIEW(dev)) {
190f68c5 6743 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6744 if (reduced_clock)
7429e9d4 6745 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6746 } else {
190f68c5 6747 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6748 if (reduced_clock)
7429e9d4 6749 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6750 }
6751
190f68c5 6752 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6753
f47709a9 6754 crtc->lowfreq_avail = false;
a93e255f 6755 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6756 reduced_clock) {
190f68c5 6757 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6758 crtc->lowfreq_avail = true;
a7516a05 6759 } else {
190f68c5 6760 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6761 }
6762}
6763
5e69f97f
CML
6764static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6765 pipe)
89b667f8
JB
6766{
6767 u32 reg_val;
6768
6769 /*
6770 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6771 * and set it to a reasonable value instead.
6772 */
ab3c759a 6773 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6774 reg_val &= 0xffffff00;
6775 reg_val |= 0x00000030;
ab3c759a 6776 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6777
ab3c759a 6778 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6779 reg_val &= 0x8cffffff;
6780 reg_val = 0x8c000000;
ab3c759a 6781 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6782
ab3c759a 6783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6784 reg_val &= 0xffffff00;
ab3c759a 6785 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6786
ab3c759a 6787 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6788 reg_val &= 0x00ffffff;
6789 reg_val |= 0xb0000000;
ab3c759a 6790 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6791}
6792
b551842d
DV
6793static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6794 struct intel_link_m_n *m_n)
6795{
6796 struct drm_device *dev = crtc->base.dev;
6797 struct drm_i915_private *dev_priv = dev->dev_private;
6798 int pipe = crtc->pipe;
6799
e3b95f1e
DV
6800 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6801 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6802 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6803 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6804}
6805
6806static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6807 struct intel_link_m_n *m_n,
6808 struct intel_link_m_n *m2_n2)
b551842d
DV
6809{
6810 struct drm_device *dev = crtc->base.dev;
6811 struct drm_i915_private *dev_priv = dev->dev_private;
6812 int pipe = crtc->pipe;
6e3c9717 6813 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6814
6815 if (INTEL_INFO(dev)->gen >= 5) {
6816 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6817 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6818 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6819 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6820 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6821 * for gen < 8) and if DRRS is supported (to make sure the
6822 * registers are not unnecessarily accessed).
6823 */
44395bfe 6824 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6825 crtc->config->has_drrs) {
f769cd24
VK
6826 I915_WRITE(PIPE_DATA_M2(transcoder),
6827 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6828 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6829 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6830 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6831 }
b551842d 6832 } else {
e3b95f1e
DV
6833 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6834 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6835 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6836 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6837 }
6838}
6839
fe3cd48d 6840void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6841{
fe3cd48d
R
6842 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6843
6844 if (m_n == M1_N1) {
6845 dp_m_n = &crtc->config->dp_m_n;
6846 dp_m2_n2 = &crtc->config->dp_m2_n2;
6847 } else if (m_n == M2_N2) {
6848
6849 /*
6850 * M2_N2 registers are not supported. Hence m2_n2 divider value
6851 * needs to be programmed into M1_N1.
6852 */
6853 dp_m_n = &crtc->config->dp_m2_n2;
6854 } else {
6855 DRM_ERROR("Unsupported divider value\n");
6856 return;
6857 }
6858
6e3c9717
ACO
6859 if (crtc->config->has_pch_encoder)
6860 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6861 else
fe3cd48d 6862 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6863}
6864
d288f65f 6865static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6866 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6867{
6868 u32 dpll, dpll_md;
6869
6870 /*
6871 * Enable DPIO clock input. We should never disable the reference
6872 * clock for pipe B, since VGA hotplug / manual detection depends
6873 * on it.
6874 */
6875 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6876 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6877 /* We should never disable this, set it here for state tracking */
6878 if (crtc->pipe == PIPE_B)
6879 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6880 dpll |= DPLL_VCO_ENABLE;
d288f65f 6881 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6882
d288f65f 6883 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6884 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6885 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6886}
6887
d288f65f 6888static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6889 const struct intel_crtc_state *pipe_config)
a0c4da24 6890{
f47709a9 6891 struct drm_device *dev = crtc->base.dev;
a0c4da24 6892 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6893 int pipe = crtc->pipe;
bdd4b6a6 6894 u32 mdiv;
a0c4da24 6895 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6896 u32 coreclk, reg_val;
a0c4da24 6897
09153000
DV
6898 mutex_lock(&dev_priv->dpio_lock);
6899
d288f65f
VS
6900 bestn = pipe_config->dpll.n;
6901 bestm1 = pipe_config->dpll.m1;
6902 bestm2 = pipe_config->dpll.m2;
6903 bestp1 = pipe_config->dpll.p1;
6904 bestp2 = pipe_config->dpll.p2;
a0c4da24 6905
89b667f8
JB
6906 /* See eDP HDMI DPIO driver vbios notes doc */
6907
6908 /* PLL B needs special handling */
bdd4b6a6 6909 if (pipe == PIPE_B)
5e69f97f 6910 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6911
6912 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6913 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6914
6915 /* Disable target IRef on PLL */
ab3c759a 6916 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6917 reg_val &= 0x00ffffff;
ab3c759a 6918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6919
6920 /* Disable fast lock */
ab3c759a 6921 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6922
6923 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6924 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6925 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6926 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6927 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6928
6929 /*
6930 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6931 * but we don't support that).
6932 * Note: don't use the DAC post divider as it seems unstable.
6933 */
6934 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6936
a0c4da24 6937 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6938 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6939
89b667f8 6940 /* Set HBR and RBR LPF coefficients */
d288f65f 6941 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6942 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6943 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6945 0x009f0003);
89b667f8 6946 else
ab3c759a 6947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6948 0x00d0000f);
6949
681a8504 6950 if (pipe_config->has_dp_encoder) {
89b667f8 6951 /* Use SSC source */
bdd4b6a6 6952 if (pipe == PIPE_A)
ab3c759a 6953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6954 0x0df40000);
6955 else
ab3c759a 6956 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6957 0x0df70000);
6958 } else { /* HDMI or VGA */
6959 /* Use bend source */
bdd4b6a6 6960 if (pipe == PIPE_A)
ab3c759a 6961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6962 0x0df70000);
6963 else
ab3c759a 6964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6965 0x0df40000);
6966 }
a0c4da24 6967
ab3c759a 6968 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6969 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6970 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6971 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6972 coreclk |= 0x01000000;
ab3c759a 6973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6974
ab3c759a 6975 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6976 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6977}
6978
d288f65f 6979static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6980 struct intel_crtc_state *pipe_config)
1ae0d137 6981{
d288f65f 6982 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6983 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6984 DPLL_VCO_ENABLE;
6985 if (crtc->pipe != PIPE_A)
d288f65f 6986 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6987
d288f65f
VS
6988 pipe_config->dpll_hw_state.dpll_md =
6989 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6990}
6991
d288f65f 6992static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6993 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6994{
6995 struct drm_device *dev = crtc->base.dev;
6996 struct drm_i915_private *dev_priv = dev->dev_private;
6997 int pipe = crtc->pipe;
6998 int dpll_reg = DPLL(crtc->pipe);
6999 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7000 u32 loopfilter, tribuf_calcntr;
9d556c99 7001 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7002 u32 dpio_val;
9cbe40c1 7003 int vco;
9d556c99 7004
d288f65f
VS
7005 bestn = pipe_config->dpll.n;
7006 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7007 bestm1 = pipe_config->dpll.m1;
7008 bestm2 = pipe_config->dpll.m2 >> 22;
7009 bestp1 = pipe_config->dpll.p1;
7010 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7011 vco = pipe_config->dpll.vco;
a945ce7e 7012 dpio_val = 0;
9cbe40c1 7013 loopfilter = 0;
9d556c99
CML
7014
7015 /*
7016 * Enable Refclk and SSC
7017 */
a11b0703 7018 I915_WRITE(dpll_reg,
d288f65f 7019 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
7020
7021 mutex_lock(&dev_priv->dpio_lock);
9d556c99 7022
9d556c99
CML
7023 /* p1 and p2 divider */
7024 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7025 5 << DPIO_CHV_S1_DIV_SHIFT |
7026 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7027 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7028 1 << DPIO_CHV_K_DIV_SHIFT);
7029
7030 /* Feedback post-divider - m2 */
7031 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7032
7033 /* Feedback refclk divider - n and m1 */
7034 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7035 DPIO_CHV_M1_DIV_BY_2 |
7036 1 << DPIO_CHV_N_DIV_SHIFT);
7037
7038 /* M2 fraction division */
a945ce7e
VP
7039 if (bestm2_frac)
7040 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7041
7042 /* M2 fraction division enable */
a945ce7e
VP
7043 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7044 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7045 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7046 if (bestm2_frac)
7047 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7048 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7049
de3a0fde
VP
7050 /* Program digital lock detect threshold */
7051 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7052 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7053 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7054 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7055 if (!bestm2_frac)
7056 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7057 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7058
9d556c99 7059 /* Loop filter */
9cbe40c1
VP
7060 if (vco == 5400000) {
7061 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7062 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7063 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7064 tribuf_calcntr = 0x9;
7065 } else if (vco <= 6200000) {
7066 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7067 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7068 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7069 tribuf_calcntr = 0x9;
7070 } else if (vco <= 6480000) {
7071 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7072 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7074 tribuf_calcntr = 0x8;
7075 } else {
7076 /* Not supported. Apply the same limits as in the max case */
7077 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7078 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7079 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7080 tribuf_calcntr = 0;
7081 }
9d556c99
CML
7082 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7083
968040b2 7084 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7085 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7086 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7088
9d556c99
CML
7089 /* AFC Recal */
7090 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7091 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7092 DPIO_AFC_RECAL);
7093
7094 mutex_unlock(&dev_priv->dpio_lock);
7095}
7096
d288f65f
VS
7097/**
7098 * vlv_force_pll_on - forcibly enable just the PLL
7099 * @dev_priv: i915 private structure
7100 * @pipe: pipe PLL to enable
7101 * @dpll: PLL configuration
7102 *
7103 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7104 * in cases where we need the PLL enabled even when @pipe is not going to
7105 * be enabled.
7106 */
7107void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7108 const struct dpll *dpll)
7109{
7110 struct intel_crtc *crtc =
7111 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7112 struct intel_crtc_state pipe_config = {
a93e255f 7113 .base.crtc = &crtc->base,
d288f65f
VS
7114 .pixel_multiplier = 1,
7115 .dpll = *dpll,
7116 };
7117
7118 if (IS_CHERRYVIEW(dev)) {
7119 chv_update_pll(crtc, &pipe_config);
7120 chv_prepare_pll(crtc, &pipe_config);
7121 chv_enable_pll(crtc, &pipe_config);
7122 } else {
7123 vlv_update_pll(crtc, &pipe_config);
7124 vlv_prepare_pll(crtc, &pipe_config);
7125 vlv_enable_pll(crtc, &pipe_config);
7126 }
7127}
7128
7129/**
7130 * vlv_force_pll_off - forcibly disable just the PLL
7131 * @dev_priv: i915 private structure
7132 * @pipe: pipe PLL to disable
7133 *
7134 * Disable the PLL for @pipe. To be used in cases where we need
7135 * the PLL enabled even when @pipe is not going to be enabled.
7136 */
7137void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7138{
7139 if (IS_CHERRYVIEW(dev))
7140 chv_disable_pll(to_i915(dev), pipe);
7141 else
7142 vlv_disable_pll(to_i915(dev), pipe);
7143}
7144
f47709a9 7145static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7146 struct intel_crtc_state *crtc_state,
f47709a9 7147 intel_clock_t *reduced_clock,
eb1cbe48
DV
7148 int num_connectors)
7149{
f47709a9 7150 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7151 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7152 u32 dpll;
7153 bool is_sdvo;
190f68c5 7154 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7155
190f68c5 7156 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7157
a93e255f
ACO
7158 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7159 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7160
7161 dpll = DPLL_VGA_MODE_DIS;
7162
a93e255f 7163 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7164 dpll |= DPLLB_MODE_LVDS;
7165 else
7166 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7167
ef1b460d 7168 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7169 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7170 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7171 }
198a037f
DV
7172
7173 if (is_sdvo)
4a33e48d 7174 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7175
190f68c5 7176 if (crtc_state->has_dp_encoder)
4a33e48d 7177 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7178
7179 /* compute bitmask from p1 value */
7180 if (IS_PINEVIEW(dev))
7181 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7182 else {
7183 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7184 if (IS_G4X(dev) && reduced_clock)
7185 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7186 }
7187 switch (clock->p2) {
7188 case 5:
7189 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7190 break;
7191 case 7:
7192 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7193 break;
7194 case 10:
7195 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7196 break;
7197 case 14:
7198 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7199 break;
7200 }
7201 if (INTEL_INFO(dev)->gen >= 4)
7202 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7203
190f68c5 7204 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7205 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7206 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7207 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7208 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7209 else
7210 dpll |= PLL_REF_INPUT_DREFCLK;
7211
7212 dpll |= DPLL_VCO_ENABLE;
190f68c5 7213 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7214
eb1cbe48 7215 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7216 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7217 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7218 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7219 }
7220}
7221
f47709a9 7222static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7223 struct intel_crtc_state *crtc_state,
f47709a9 7224 intel_clock_t *reduced_clock,
eb1cbe48
DV
7225 int num_connectors)
7226{
f47709a9 7227 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7228 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7229 u32 dpll;
190f68c5 7230 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7231
190f68c5 7232 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7233
eb1cbe48
DV
7234 dpll = DPLL_VGA_MODE_DIS;
7235
a93e255f 7236 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7237 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7238 } else {
7239 if (clock->p1 == 2)
7240 dpll |= PLL_P1_DIVIDE_BY_TWO;
7241 else
7242 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7243 if (clock->p2 == 4)
7244 dpll |= PLL_P2_DIVIDE_BY_4;
7245 }
7246
a93e255f 7247 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7248 dpll |= DPLL_DVO_2X_MODE;
7249
a93e255f 7250 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7251 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7252 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7253 else
7254 dpll |= PLL_REF_INPUT_DREFCLK;
7255
7256 dpll |= DPLL_VCO_ENABLE;
190f68c5 7257 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7258}
7259
8a654f3b 7260static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7261{
7262 struct drm_device *dev = intel_crtc->base.dev;
7263 struct drm_i915_private *dev_priv = dev->dev_private;
7264 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7265 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7266 struct drm_display_mode *adjusted_mode =
6e3c9717 7267 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7268 uint32_t crtc_vtotal, crtc_vblank_end;
7269 int vsyncshift = 0;
4d8a62ea
DV
7270
7271 /* We need to be careful not to changed the adjusted mode, for otherwise
7272 * the hw state checker will get angry at the mismatch. */
7273 crtc_vtotal = adjusted_mode->crtc_vtotal;
7274 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7275
609aeaca 7276 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7277 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7278 crtc_vtotal -= 1;
7279 crtc_vblank_end -= 1;
609aeaca 7280
409ee761 7281 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7282 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7283 else
7284 vsyncshift = adjusted_mode->crtc_hsync_start -
7285 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7286 if (vsyncshift < 0)
7287 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7288 }
7289
7290 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7291 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7292
fe2b8f9d 7293 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7294 (adjusted_mode->crtc_hdisplay - 1) |
7295 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7296 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7297 (adjusted_mode->crtc_hblank_start - 1) |
7298 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7299 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7300 (adjusted_mode->crtc_hsync_start - 1) |
7301 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7302
fe2b8f9d 7303 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7304 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7305 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7306 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7307 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7308 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7309 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7310 (adjusted_mode->crtc_vsync_start - 1) |
7311 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7312
b5e508d4
PZ
7313 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7314 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7315 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7316 * bits. */
7317 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7318 (pipe == PIPE_B || pipe == PIPE_C))
7319 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7320
b0e77b9c
PZ
7321 /* pipesrc controls the size that is scaled from, which should
7322 * always be the user's requested size.
7323 */
7324 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7325 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7326 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7327}
7328
1bd1bd80 7329static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7330 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7331{
7332 struct drm_device *dev = crtc->base.dev;
7333 struct drm_i915_private *dev_priv = dev->dev_private;
7334 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7335 uint32_t tmp;
7336
7337 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7338 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7339 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7340 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7341 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7342 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7343 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7344 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7345 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7346
7347 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7348 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7349 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7350 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7351 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7352 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7353 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7354 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7355 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7356
7357 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7358 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7359 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7360 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7361 }
7362
7363 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7364 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7365 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7366
2d112de7
ACO
7367 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7368 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7369}
7370
f6a83288 7371void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7372 struct intel_crtc_state *pipe_config)
babea61d 7373{
2d112de7
ACO
7374 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7375 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7376 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7377 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7378
2d112de7
ACO
7379 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7380 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7381 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7382 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7383
2d112de7 7384 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7385
2d112de7
ACO
7386 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7387 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7388}
7389
84b046f3
DV
7390static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7391{
7392 struct drm_device *dev = intel_crtc->base.dev;
7393 struct drm_i915_private *dev_priv = dev->dev_private;
7394 uint32_t pipeconf;
7395
9f11a9e4 7396 pipeconf = 0;
84b046f3 7397
b6b5d049
VS
7398 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7399 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7400 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7401
6e3c9717 7402 if (intel_crtc->config->double_wide)
cf532bb2 7403 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7404
ff9ce46e
DV
7405 /* only g4x and later have fancy bpc/dither controls */
7406 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7407 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7408 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7409 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7410 PIPECONF_DITHER_TYPE_SP;
84b046f3 7411
6e3c9717 7412 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7413 case 18:
7414 pipeconf |= PIPECONF_6BPC;
7415 break;
7416 case 24:
7417 pipeconf |= PIPECONF_8BPC;
7418 break;
7419 case 30:
7420 pipeconf |= PIPECONF_10BPC;
7421 break;
7422 default:
7423 /* Case prevented by intel_choose_pipe_bpp_dither. */
7424 BUG();
84b046f3
DV
7425 }
7426 }
7427
7428 if (HAS_PIPE_CXSR(dev)) {
7429 if (intel_crtc->lowfreq_avail) {
7430 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7431 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7432 } else {
7433 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7434 }
7435 }
7436
6e3c9717 7437 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7438 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7439 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7440 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7441 else
7442 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7443 } else
84b046f3
DV
7444 pipeconf |= PIPECONF_PROGRESSIVE;
7445
6e3c9717 7446 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7447 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7448
84b046f3
DV
7449 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7450 POSTING_READ(PIPECONF(intel_crtc->pipe));
7451}
7452
190f68c5
ACO
7453static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7454 struct intel_crtc_state *crtc_state)
79e53945 7455{
c7653199 7456 struct drm_device *dev = crtc->base.dev;
79e53945 7457 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7458 int refclk, num_connectors = 0;
652c393a 7459 intel_clock_t clock, reduced_clock;
a16af721 7460 bool ok, has_reduced_clock = false;
e9fd1c02 7461 bool is_lvds = false, is_dsi = false;
5eddb70b 7462 struct intel_encoder *encoder;
d4906093 7463 const intel_limit_t *limit;
55bb9992
ACO
7464 struct drm_atomic_state *state = crtc_state->base.state;
7465 struct drm_connector_state *connector_state;
7466 int i;
79e53945 7467
55bb9992
ACO
7468 for (i = 0; i < state->num_connector; i++) {
7469 if (!state->connectors[i])
d0737e1d
ACO
7470 continue;
7471
55bb9992
ACO
7472 connector_state = state->connector_states[i];
7473 if (connector_state->crtc != &crtc->base)
7474 continue;
7475
7476 encoder = to_intel_encoder(connector_state->best_encoder);
7477
5eddb70b 7478 switch (encoder->type) {
79e53945
JB
7479 case INTEL_OUTPUT_LVDS:
7480 is_lvds = true;
7481 break;
e9fd1c02
JN
7482 case INTEL_OUTPUT_DSI:
7483 is_dsi = true;
7484 break;
6847d71b
PZ
7485 default:
7486 break;
79e53945 7487 }
43565a06 7488
c751ce4f 7489 num_connectors++;
79e53945
JB
7490 }
7491
f2335330 7492 if (is_dsi)
5b18e57c 7493 return 0;
f2335330 7494
190f68c5 7495 if (!crtc_state->clock_set) {
a93e255f 7496 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7497
e9fd1c02
JN
7498 /*
7499 * Returns a set of divisors for the desired target clock with
7500 * the given refclk, or FALSE. The returned values represent
7501 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7502 * 2) / p1 / p2.
7503 */
a93e255f
ACO
7504 limit = intel_limit(crtc_state, refclk);
7505 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7506 crtc_state->port_clock,
e9fd1c02 7507 refclk, NULL, &clock);
f2335330 7508 if (!ok) {
e9fd1c02
JN
7509 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7510 return -EINVAL;
7511 }
79e53945 7512
f2335330
JN
7513 if (is_lvds && dev_priv->lvds_downclock_avail) {
7514 /*
7515 * Ensure we match the reduced clock's P to the target
7516 * clock. If the clocks don't match, we can't switch
7517 * the display clock by using the FP0/FP1. In such case
7518 * we will disable the LVDS downclock feature.
7519 */
7520 has_reduced_clock =
a93e255f 7521 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7522 dev_priv->lvds_downclock,
7523 refclk, &clock,
7524 &reduced_clock);
7525 }
7526 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7527 crtc_state->dpll.n = clock.n;
7528 crtc_state->dpll.m1 = clock.m1;
7529 crtc_state->dpll.m2 = clock.m2;
7530 crtc_state->dpll.p1 = clock.p1;
7531 crtc_state->dpll.p2 = clock.p2;
f47709a9 7532 }
7026d4ac 7533
e9fd1c02 7534 if (IS_GEN2(dev)) {
190f68c5 7535 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7536 has_reduced_clock ? &reduced_clock : NULL,
7537 num_connectors);
9d556c99 7538 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7539 chv_update_pll(crtc, crtc_state);
e9fd1c02 7540 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7541 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7542 } else {
190f68c5 7543 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7544 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7545 num_connectors);
e9fd1c02 7546 }
79e53945 7547
c8f7a0db 7548 return 0;
f564048e
EA
7549}
7550
2fa2fe9a 7551static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7552 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7553{
7554 struct drm_device *dev = crtc->base.dev;
7555 struct drm_i915_private *dev_priv = dev->dev_private;
7556 uint32_t tmp;
7557
dc9e7dec
VS
7558 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7559 return;
7560
2fa2fe9a 7561 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7562 if (!(tmp & PFIT_ENABLE))
7563 return;
2fa2fe9a 7564
06922821 7565 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7566 if (INTEL_INFO(dev)->gen < 4) {
7567 if (crtc->pipe != PIPE_B)
7568 return;
2fa2fe9a
DV
7569 } else {
7570 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7571 return;
7572 }
7573
06922821 7574 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7575 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7576 if (INTEL_INFO(dev)->gen < 5)
7577 pipe_config->gmch_pfit.lvds_border_bits =
7578 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7579}
7580
acbec814 7581static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7582 struct intel_crtc_state *pipe_config)
acbec814
JB
7583{
7584 struct drm_device *dev = crtc->base.dev;
7585 struct drm_i915_private *dev_priv = dev->dev_private;
7586 int pipe = pipe_config->cpu_transcoder;
7587 intel_clock_t clock;
7588 u32 mdiv;
662c6ecb 7589 int refclk = 100000;
acbec814 7590
f573de5a
SK
7591 /* In case of MIPI DPLL will not even be used */
7592 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7593 return;
7594
acbec814 7595 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 7596 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
7597 mutex_unlock(&dev_priv->dpio_lock);
7598
7599 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7600 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7601 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7602 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7603 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7604
f646628b 7605 vlv_clock(refclk, &clock);
acbec814 7606
f646628b
VS
7607 /* clock.dot is the fast clock */
7608 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7609}
7610
5724dbd1
DL
7611static void
7612i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7613 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7614{
7615 struct drm_device *dev = crtc->base.dev;
7616 struct drm_i915_private *dev_priv = dev->dev_private;
7617 u32 val, base, offset;
7618 int pipe = crtc->pipe, plane = crtc->plane;
7619 int fourcc, pixel_format;
6761dd31 7620 unsigned int aligned_height;
b113d5ee 7621 struct drm_framebuffer *fb;
1b842c89 7622 struct intel_framebuffer *intel_fb;
1ad292b5 7623
42a7b088
DL
7624 val = I915_READ(DSPCNTR(plane));
7625 if (!(val & DISPLAY_PLANE_ENABLE))
7626 return;
7627
d9806c9f 7628 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7629 if (!intel_fb) {
1ad292b5
JB
7630 DRM_DEBUG_KMS("failed to alloc fb\n");
7631 return;
7632 }
7633
1b842c89
DL
7634 fb = &intel_fb->base;
7635
18c5247e
DV
7636 if (INTEL_INFO(dev)->gen >= 4) {
7637 if (val & DISPPLANE_TILED) {
49af449b 7638 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7639 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7640 }
7641 }
1ad292b5
JB
7642
7643 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7644 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7645 fb->pixel_format = fourcc;
7646 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7647
7648 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7649 if (plane_config->tiling)
1ad292b5
JB
7650 offset = I915_READ(DSPTILEOFF(plane));
7651 else
7652 offset = I915_READ(DSPLINOFF(plane));
7653 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7654 } else {
7655 base = I915_READ(DSPADDR(plane));
7656 }
7657 plane_config->base = base;
7658
7659 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7660 fb->width = ((val >> 16) & 0xfff) + 1;
7661 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7662
7663 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7664 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7665
b113d5ee 7666 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7667 fb->pixel_format,
7668 fb->modifier[0]);
1ad292b5 7669
f37b5c2b 7670 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7671
2844a921
DL
7672 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7673 pipe_name(pipe), plane, fb->width, fb->height,
7674 fb->bits_per_pixel, base, fb->pitches[0],
7675 plane_config->size);
1ad292b5 7676
2d14030b 7677 plane_config->fb = intel_fb;
1ad292b5
JB
7678}
7679
70b23a98 7680static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7681 struct intel_crtc_state *pipe_config)
70b23a98
VS
7682{
7683 struct drm_device *dev = crtc->base.dev;
7684 struct drm_i915_private *dev_priv = dev->dev_private;
7685 int pipe = pipe_config->cpu_transcoder;
7686 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7687 intel_clock_t clock;
7688 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7689 int refclk = 100000;
7690
7691 mutex_lock(&dev_priv->dpio_lock);
7692 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7693 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7694 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7695 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7696 mutex_unlock(&dev_priv->dpio_lock);
7697
7698 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7699 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7700 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7701 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7702 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7703
7704 chv_clock(refclk, &clock);
7705
7706 /* clock.dot is the fast clock */
7707 pipe_config->port_clock = clock.dot / 5;
7708}
7709
0e8ffe1b 7710static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7711 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7712{
7713 struct drm_device *dev = crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 uint32_t tmp;
7716
f458ebbc
DV
7717 if (!intel_display_power_is_enabled(dev_priv,
7718 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7719 return false;
7720
e143a21c 7721 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7722 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7723
0e8ffe1b
DV
7724 tmp = I915_READ(PIPECONF(crtc->pipe));
7725 if (!(tmp & PIPECONF_ENABLE))
7726 return false;
7727
42571aef
VS
7728 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7729 switch (tmp & PIPECONF_BPC_MASK) {
7730 case PIPECONF_6BPC:
7731 pipe_config->pipe_bpp = 18;
7732 break;
7733 case PIPECONF_8BPC:
7734 pipe_config->pipe_bpp = 24;
7735 break;
7736 case PIPECONF_10BPC:
7737 pipe_config->pipe_bpp = 30;
7738 break;
7739 default:
7740 break;
7741 }
7742 }
7743
b5a9fa09
DV
7744 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7745 pipe_config->limited_color_range = true;
7746
282740f7
VS
7747 if (INTEL_INFO(dev)->gen < 4)
7748 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7749
1bd1bd80
DV
7750 intel_get_pipe_timings(crtc, pipe_config);
7751
2fa2fe9a
DV
7752 i9xx_get_pfit_config(crtc, pipe_config);
7753
6c49f241
DV
7754 if (INTEL_INFO(dev)->gen >= 4) {
7755 tmp = I915_READ(DPLL_MD(crtc->pipe));
7756 pipe_config->pixel_multiplier =
7757 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7758 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7759 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
7760 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7761 tmp = I915_READ(DPLL(crtc->pipe));
7762 pipe_config->pixel_multiplier =
7763 ((tmp & SDVO_MULTIPLIER_MASK)
7764 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7765 } else {
7766 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7767 * port and will be fixed up in the encoder->get_config
7768 * function. */
7769 pipe_config->pixel_multiplier = 1;
7770 }
8bcc2795
DV
7771 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7772 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
7773 /*
7774 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7775 * on 830. Filter it out here so that we don't
7776 * report errors due to that.
7777 */
7778 if (IS_I830(dev))
7779 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7780
8bcc2795
DV
7781 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7782 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7783 } else {
7784 /* Mask out read-only status bits. */
7785 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7786 DPLL_PORTC_READY_MASK |
7787 DPLL_PORTB_READY_MASK);
8bcc2795 7788 }
6c49f241 7789
70b23a98
VS
7790 if (IS_CHERRYVIEW(dev))
7791 chv_crtc_clock_get(crtc, pipe_config);
7792 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7793 vlv_crtc_clock_get(crtc, pipe_config);
7794 else
7795 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7796
0e8ffe1b
DV
7797 return true;
7798}
7799
dde86e2d 7800static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7801{
7802 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7803 struct intel_encoder *encoder;
74cfd7ac 7804 u32 val, final;
13d83a67 7805 bool has_lvds = false;
199e5d79 7806 bool has_cpu_edp = false;
199e5d79 7807 bool has_panel = false;
99eb6a01
KP
7808 bool has_ck505 = false;
7809 bool can_ssc = false;
13d83a67
JB
7810
7811 /* We need to take the global config into account */
b2784e15 7812 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7813 switch (encoder->type) {
7814 case INTEL_OUTPUT_LVDS:
7815 has_panel = true;
7816 has_lvds = true;
7817 break;
7818 case INTEL_OUTPUT_EDP:
7819 has_panel = true;
2de6905f 7820 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7821 has_cpu_edp = true;
7822 break;
6847d71b
PZ
7823 default:
7824 break;
13d83a67
JB
7825 }
7826 }
7827
99eb6a01 7828 if (HAS_PCH_IBX(dev)) {
41aa3448 7829 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7830 can_ssc = has_ck505;
7831 } else {
7832 has_ck505 = false;
7833 can_ssc = true;
7834 }
7835
2de6905f
ID
7836 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7837 has_panel, has_lvds, has_ck505);
13d83a67
JB
7838
7839 /* Ironlake: try to setup display ref clock before DPLL
7840 * enabling. This is only under driver's control after
7841 * PCH B stepping, previous chipset stepping should be
7842 * ignoring this setting.
7843 */
74cfd7ac
CW
7844 val = I915_READ(PCH_DREF_CONTROL);
7845
7846 /* As we must carefully and slowly disable/enable each source in turn,
7847 * compute the final state we want first and check if we need to
7848 * make any changes at all.
7849 */
7850 final = val;
7851 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7852 if (has_ck505)
7853 final |= DREF_NONSPREAD_CK505_ENABLE;
7854 else
7855 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7856
7857 final &= ~DREF_SSC_SOURCE_MASK;
7858 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7859 final &= ~DREF_SSC1_ENABLE;
7860
7861 if (has_panel) {
7862 final |= DREF_SSC_SOURCE_ENABLE;
7863
7864 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7865 final |= DREF_SSC1_ENABLE;
7866
7867 if (has_cpu_edp) {
7868 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7869 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7870 else
7871 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7872 } else
7873 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7874 } else {
7875 final |= DREF_SSC_SOURCE_DISABLE;
7876 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7877 }
7878
7879 if (final == val)
7880 return;
7881
13d83a67 7882 /* Always enable nonspread source */
74cfd7ac 7883 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7884
99eb6a01 7885 if (has_ck505)
74cfd7ac 7886 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7887 else
74cfd7ac 7888 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7889
199e5d79 7890 if (has_panel) {
74cfd7ac
CW
7891 val &= ~DREF_SSC_SOURCE_MASK;
7892 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7893
199e5d79 7894 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7895 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7896 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7897 val |= DREF_SSC1_ENABLE;
e77166b5 7898 } else
74cfd7ac 7899 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7900
7901 /* Get SSC going before enabling the outputs */
74cfd7ac 7902 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7903 POSTING_READ(PCH_DREF_CONTROL);
7904 udelay(200);
7905
74cfd7ac 7906 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7907
7908 /* Enable CPU source on CPU attached eDP */
199e5d79 7909 if (has_cpu_edp) {
99eb6a01 7910 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7911 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7912 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7913 } else
74cfd7ac 7914 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7915 } else
74cfd7ac 7916 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7917
74cfd7ac 7918 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7919 POSTING_READ(PCH_DREF_CONTROL);
7920 udelay(200);
7921 } else {
7922 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7923
74cfd7ac 7924 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7925
7926 /* Turn off CPU output */
74cfd7ac 7927 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7928
74cfd7ac 7929 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7930 POSTING_READ(PCH_DREF_CONTROL);
7931 udelay(200);
7932
7933 /* Turn off the SSC source */
74cfd7ac
CW
7934 val &= ~DREF_SSC_SOURCE_MASK;
7935 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7936
7937 /* Turn off SSC1 */
74cfd7ac 7938 val &= ~DREF_SSC1_ENABLE;
199e5d79 7939
74cfd7ac 7940 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7941 POSTING_READ(PCH_DREF_CONTROL);
7942 udelay(200);
7943 }
74cfd7ac
CW
7944
7945 BUG_ON(val != final);
13d83a67
JB
7946}
7947
f31f2d55 7948static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7949{
f31f2d55 7950 uint32_t tmp;
dde86e2d 7951
0ff066a9
PZ
7952 tmp = I915_READ(SOUTH_CHICKEN2);
7953 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7954 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7955
0ff066a9
PZ
7956 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7957 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7958 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7959
0ff066a9
PZ
7960 tmp = I915_READ(SOUTH_CHICKEN2);
7961 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7962 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7963
0ff066a9
PZ
7964 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7965 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7966 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7967}
7968
7969/* WaMPhyProgramming:hsw */
7970static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7971{
7972 uint32_t tmp;
dde86e2d
PZ
7973
7974 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7975 tmp &= ~(0xFF << 24);
7976 tmp |= (0x12 << 24);
7977 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7978
dde86e2d
PZ
7979 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7980 tmp |= (1 << 11);
7981 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7982
7983 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7984 tmp |= (1 << 11);
7985 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7986
dde86e2d
PZ
7987 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7988 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7989 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7990
7991 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7992 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7993 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7994
0ff066a9
PZ
7995 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7996 tmp &= ~(7 << 13);
7997 tmp |= (5 << 13);
7998 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7999
0ff066a9
PZ
8000 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8001 tmp &= ~(7 << 13);
8002 tmp |= (5 << 13);
8003 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8004
8005 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8006 tmp &= ~0xFF;
8007 tmp |= 0x1C;
8008 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8009
8010 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8011 tmp &= ~0xFF;
8012 tmp |= 0x1C;
8013 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8014
8015 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8016 tmp &= ~(0xFF << 16);
8017 tmp |= (0x1C << 16);
8018 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8019
8020 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8021 tmp &= ~(0xFF << 16);
8022 tmp |= (0x1C << 16);
8023 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8024
0ff066a9
PZ
8025 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8026 tmp |= (1 << 27);
8027 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8028
0ff066a9
PZ
8029 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8030 tmp |= (1 << 27);
8031 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8032
0ff066a9
PZ
8033 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8034 tmp &= ~(0xF << 28);
8035 tmp |= (4 << 28);
8036 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8037
0ff066a9
PZ
8038 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8039 tmp &= ~(0xF << 28);
8040 tmp |= (4 << 28);
8041 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8042}
8043
2fa86a1f
PZ
8044/* Implements 3 different sequences from BSpec chapter "Display iCLK
8045 * Programming" based on the parameters passed:
8046 * - Sequence to enable CLKOUT_DP
8047 * - Sequence to enable CLKOUT_DP without spread
8048 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8049 */
8050static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8051 bool with_fdi)
f31f2d55
PZ
8052{
8053 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8054 uint32_t reg, tmp;
8055
8056 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8057 with_spread = true;
8058 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8059 with_fdi, "LP PCH doesn't have FDI\n"))
8060 with_fdi = false;
f31f2d55
PZ
8061
8062 mutex_lock(&dev_priv->dpio_lock);
8063
8064 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8065 tmp &= ~SBI_SSCCTL_DISABLE;
8066 tmp |= SBI_SSCCTL_PATHALT;
8067 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8068
8069 udelay(24);
8070
2fa86a1f
PZ
8071 if (with_spread) {
8072 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8073 tmp &= ~SBI_SSCCTL_PATHALT;
8074 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8075
2fa86a1f
PZ
8076 if (with_fdi) {
8077 lpt_reset_fdi_mphy(dev_priv);
8078 lpt_program_fdi_mphy(dev_priv);
8079 }
8080 }
dde86e2d 8081
2fa86a1f
PZ
8082 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8083 SBI_GEN0 : SBI_DBUFF0;
8084 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8085 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8086 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
8087
8088 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
8089}
8090
47701c3b
PZ
8091/* Sequence to disable CLKOUT_DP */
8092static void lpt_disable_clkout_dp(struct drm_device *dev)
8093{
8094 struct drm_i915_private *dev_priv = dev->dev_private;
8095 uint32_t reg, tmp;
8096
8097 mutex_lock(&dev_priv->dpio_lock);
8098
8099 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8100 SBI_GEN0 : SBI_DBUFF0;
8101 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8102 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8103 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8104
8105 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8106 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8107 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8108 tmp |= SBI_SSCCTL_PATHALT;
8109 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8110 udelay(32);
8111 }
8112 tmp |= SBI_SSCCTL_DISABLE;
8113 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8114 }
8115
8116 mutex_unlock(&dev_priv->dpio_lock);
8117}
8118
bf8fa3d3
PZ
8119static void lpt_init_pch_refclk(struct drm_device *dev)
8120{
bf8fa3d3
PZ
8121 struct intel_encoder *encoder;
8122 bool has_vga = false;
8123
b2784e15 8124 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8125 switch (encoder->type) {
8126 case INTEL_OUTPUT_ANALOG:
8127 has_vga = true;
8128 break;
6847d71b
PZ
8129 default:
8130 break;
bf8fa3d3
PZ
8131 }
8132 }
8133
47701c3b
PZ
8134 if (has_vga)
8135 lpt_enable_clkout_dp(dev, true, true);
8136 else
8137 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8138}
8139
dde86e2d
PZ
8140/*
8141 * Initialize reference clocks when the driver loads
8142 */
8143void intel_init_pch_refclk(struct drm_device *dev)
8144{
8145 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8146 ironlake_init_pch_refclk(dev);
8147 else if (HAS_PCH_LPT(dev))
8148 lpt_init_pch_refclk(dev);
8149}
8150
55bb9992 8151static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8152{
55bb9992 8153 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8154 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8155 struct drm_atomic_state *state = crtc_state->base.state;
8156 struct drm_connector_state *connector_state;
d9d444cb 8157 struct intel_encoder *encoder;
55bb9992 8158 int num_connectors = 0, i;
d9d444cb
JB
8159 bool is_lvds = false;
8160
55bb9992
ACO
8161 for (i = 0; i < state->num_connector; i++) {
8162 if (!state->connectors[i])
d0737e1d
ACO
8163 continue;
8164
55bb9992
ACO
8165 connector_state = state->connector_states[i];
8166 if (connector_state->crtc != crtc_state->base.crtc)
8167 continue;
8168
8169 encoder = to_intel_encoder(connector_state->best_encoder);
8170
d9d444cb
JB
8171 switch (encoder->type) {
8172 case INTEL_OUTPUT_LVDS:
8173 is_lvds = true;
8174 break;
6847d71b
PZ
8175 default:
8176 break;
d9d444cb
JB
8177 }
8178 num_connectors++;
8179 }
8180
8181 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8182 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8183 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8184 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8185 }
8186
8187 return 120000;
8188}
8189
6ff93609 8190static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8191{
c8203565 8192 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8194 int pipe = intel_crtc->pipe;
c8203565
PZ
8195 uint32_t val;
8196
78114071 8197 val = 0;
c8203565 8198
6e3c9717 8199 switch (intel_crtc->config->pipe_bpp) {
c8203565 8200 case 18:
dfd07d72 8201 val |= PIPECONF_6BPC;
c8203565
PZ
8202 break;
8203 case 24:
dfd07d72 8204 val |= PIPECONF_8BPC;
c8203565
PZ
8205 break;
8206 case 30:
dfd07d72 8207 val |= PIPECONF_10BPC;
c8203565
PZ
8208 break;
8209 case 36:
dfd07d72 8210 val |= PIPECONF_12BPC;
c8203565
PZ
8211 break;
8212 default:
cc769b62
PZ
8213 /* Case prevented by intel_choose_pipe_bpp_dither. */
8214 BUG();
c8203565
PZ
8215 }
8216
6e3c9717 8217 if (intel_crtc->config->dither)
c8203565
PZ
8218 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8219
6e3c9717 8220 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8221 val |= PIPECONF_INTERLACED_ILK;
8222 else
8223 val |= PIPECONF_PROGRESSIVE;
8224
6e3c9717 8225 if (intel_crtc->config->limited_color_range)
3685a8f3 8226 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8227
c8203565
PZ
8228 I915_WRITE(PIPECONF(pipe), val);
8229 POSTING_READ(PIPECONF(pipe));
8230}
8231
86d3efce
VS
8232/*
8233 * Set up the pipe CSC unit.
8234 *
8235 * Currently only full range RGB to limited range RGB conversion
8236 * is supported, but eventually this should handle various
8237 * RGB<->YCbCr scenarios as well.
8238 */
50f3b016 8239static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8240{
8241 struct drm_device *dev = crtc->dev;
8242 struct drm_i915_private *dev_priv = dev->dev_private;
8243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8244 int pipe = intel_crtc->pipe;
8245 uint16_t coeff = 0x7800; /* 1.0 */
8246
8247 /*
8248 * TODO: Check what kind of values actually come out of the pipe
8249 * with these coeff/postoff values and adjust to get the best
8250 * accuracy. Perhaps we even need to take the bpc value into
8251 * consideration.
8252 */
8253
6e3c9717 8254 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8255 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8256
8257 /*
8258 * GY/GU and RY/RU should be the other way around according
8259 * to BSpec, but reality doesn't agree. Just set them up in
8260 * a way that results in the correct picture.
8261 */
8262 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8263 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8264
8265 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8266 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8267
8268 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8269 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8270
8271 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8272 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8273 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8274
8275 if (INTEL_INFO(dev)->gen > 6) {
8276 uint16_t postoff = 0;
8277
6e3c9717 8278 if (intel_crtc->config->limited_color_range)
32cf0cb0 8279 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8280
8281 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8282 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8283 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8284
8285 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8286 } else {
8287 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8288
6e3c9717 8289 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8290 mode |= CSC_BLACK_SCREEN_OFFSET;
8291
8292 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8293 }
8294}
8295
6ff93609 8296static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8297{
756f85cf
PZ
8298 struct drm_device *dev = crtc->dev;
8299 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8301 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8302 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8303 uint32_t val;
8304
3eff4faa 8305 val = 0;
ee2b0b38 8306
6e3c9717 8307 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8308 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8309
6e3c9717 8310 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8311 val |= PIPECONF_INTERLACED_ILK;
8312 else
8313 val |= PIPECONF_PROGRESSIVE;
8314
702e7a56
PZ
8315 I915_WRITE(PIPECONF(cpu_transcoder), val);
8316 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8317
8318 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8319 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8320
3cdf122c 8321 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8322 val = 0;
8323
6e3c9717 8324 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8325 case 18:
8326 val |= PIPEMISC_DITHER_6_BPC;
8327 break;
8328 case 24:
8329 val |= PIPEMISC_DITHER_8_BPC;
8330 break;
8331 case 30:
8332 val |= PIPEMISC_DITHER_10_BPC;
8333 break;
8334 case 36:
8335 val |= PIPEMISC_DITHER_12_BPC;
8336 break;
8337 default:
8338 /* Case prevented by pipe_config_set_bpp. */
8339 BUG();
8340 }
8341
6e3c9717 8342 if (intel_crtc->config->dither)
756f85cf
PZ
8343 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8344
8345 I915_WRITE(PIPEMISC(pipe), val);
8346 }
ee2b0b38
PZ
8347}
8348
6591c6e4 8349static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8350 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8351 intel_clock_t *clock,
8352 bool *has_reduced_clock,
8353 intel_clock_t *reduced_clock)
8354{
8355 struct drm_device *dev = crtc->dev;
8356 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8357 int refclk;
d4906093 8358 const intel_limit_t *limit;
a16af721 8359 bool ret, is_lvds = false;
79e53945 8360
a93e255f 8361 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8362
55bb9992 8363 refclk = ironlake_get_refclk(crtc_state);
79e53945 8364
d4906093
ML
8365 /*
8366 * Returns a set of divisors for the desired target clock with the given
8367 * refclk, or FALSE. The returned values represent the clock equation:
8368 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8369 */
a93e255f
ACO
8370 limit = intel_limit(crtc_state, refclk);
8371 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8372 crtc_state->port_clock,
ee9300bb 8373 refclk, NULL, clock);
6591c6e4
PZ
8374 if (!ret)
8375 return false;
cda4b7d3 8376
ddc9003c 8377 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8378 /*
8379 * Ensure we match the reduced clock's P to the target clock.
8380 * If the clocks don't match, we can't switch the display clock
8381 * by using the FP0/FP1. In such case we will disable the LVDS
8382 * downclock feature.
8383 */
ee9300bb 8384 *has_reduced_clock =
a93e255f 8385 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8386 dev_priv->lvds_downclock,
8387 refclk, clock,
8388 reduced_clock);
652c393a 8389 }
61e9653f 8390
6591c6e4
PZ
8391 return true;
8392}
8393
d4b1931c
PZ
8394int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8395{
8396 /*
8397 * Account for spread spectrum to avoid
8398 * oversubscribing the link. Max center spread
8399 * is 2.5%; use 5% for safety's sake.
8400 */
8401 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8402 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8403}
8404
7429e9d4 8405static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8406{
7429e9d4 8407 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8408}
8409
de13a2e3 8410static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8411 struct intel_crtc_state *crtc_state,
7429e9d4 8412 u32 *fp,
9a7c7890 8413 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8414{
de13a2e3 8415 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8416 struct drm_device *dev = crtc->dev;
8417 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8418 struct drm_atomic_state *state = crtc_state->base.state;
8419 struct drm_connector_state *connector_state;
8420 struct intel_encoder *encoder;
de13a2e3 8421 uint32_t dpll;
55bb9992 8422 int factor, num_connectors = 0, i;
09ede541 8423 bool is_lvds = false, is_sdvo = false;
79e53945 8424
55bb9992
ACO
8425 for (i = 0; i < state->num_connector; i++) {
8426 if (!state->connectors[i])
d0737e1d
ACO
8427 continue;
8428
55bb9992
ACO
8429 connector_state = state->connector_states[i];
8430 if (connector_state->crtc != crtc_state->base.crtc)
8431 continue;
8432
8433 encoder = to_intel_encoder(connector_state->best_encoder);
8434
8435 switch (encoder->type) {
79e53945
JB
8436 case INTEL_OUTPUT_LVDS:
8437 is_lvds = true;
8438 break;
8439 case INTEL_OUTPUT_SDVO:
7d57382e 8440 case INTEL_OUTPUT_HDMI:
79e53945 8441 is_sdvo = true;
79e53945 8442 break;
6847d71b
PZ
8443 default:
8444 break;
79e53945 8445 }
43565a06 8446
c751ce4f 8447 num_connectors++;
79e53945 8448 }
79e53945 8449
c1858123 8450 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8451 factor = 21;
8452 if (is_lvds) {
8453 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8454 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8455 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8456 factor = 25;
190f68c5 8457 } else if (crtc_state->sdvo_tv_clock)
8febb297 8458 factor = 20;
c1858123 8459
190f68c5 8460 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8461 *fp |= FP_CB_TUNE;
2c07245f 8462
9a7c7890
DV
8463 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8464 *fp2 |= FP_CB_TUNE;
8465
5eddb70b 8466 dpll = 0;
2c07245f 8467
a07d6787
EA
8468 if (is_lvds)
8469 dpll |= DPLLB_MODE_LVDS;
8470 else
8471 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8472
190f68c5 8473 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8474 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8475
8476 if (is_sdvo)
4a33e48d 8477 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8478 if (crtc_state->has_dp_encoder)
4a33e48d 8479 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8480
a07d6787 8481 /* compute bitmask from p1 value */
190f68c5 8482 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8483 /* also FPA1 */
190f68c5 8484 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8485
190f68c5 8486 switch (crtc_state->dpll.p2) {
a07d6787
EA
8487 case 5:
8488 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8489 break;
8490 case 7:
8491 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8492 break;
8493 case 10:
8494 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8495 break;
8496 case 14:
8497 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8498 break;
79e53945
JB
8499 }
8500
b4c09f3b 8501 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8502 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8503 else
8504 dpll |= PLL_REF_INPUT_DREFCLK;
8505
959e16d6 8506 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8507}
8508
190f68c5
ACO
8509static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8510 struct intel_crtc_state *crtc_state)
de13a2e3 8511{
c7653199 8512 struct drm_device *dev = crtc->base.dev;
de13a2e3 8513 intel_clock_t clock, reduced_clock;
cbbab5bd 8514 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8515 bool ok, has_reduced_clock = false;
8b47047b 8516 bool is_lvds = false;
e2b78267 8517 struct intel_shared_dpll *pll;
de13a2e3 8518
409ee761 8519 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8520
5dc5298b
PZ
8521 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8522 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8523
190f68c5 8524 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8525 &has_reduced_clock, &reduced_clock);
190f68c5 8526 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8527 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8528 return -EINVAL;
79e53945 8529 }
f47709a9 8530 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8531 if (!crtc_state->clock_set) {
8532 crtc_state->dpll.n = clock.n;
8533 crtc_state->dpll.m1 = clock.m1;
8534 crtc_state->dpll.m2 = clock.m2;
8535 crtc_state->dpll.p1 = clock.p1;
8536 crtc_state->dpll.p2 = clock.p2;
f47709a9 8537 }
79e53945 8538
5dc5298b 8539 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8540 if (crtc_state->has_pch_encoder) {
8541 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8542 if (has_reduced_clock)
7429e9d4 8543 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8544
190f68c5 8545 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8546 &fp, &reduced_clock,
8547 has_reduced_clock ? &fp2 : NULL);
8548
190f68c5
ACO
8549 crtc_state->dpll_hw_state.dpll = dpll;
8550 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8551 if (has_reduced_clock)
190f68c5 8552 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8553 else
190f68c5 8554 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8555
190f68c5 8556 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8557 if (pll == NULL) {
84f44ce7 8558 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8559 pipe_name(crtc->pipe));
4b645f14
JB
8560 return -EINVAL;
8561 }
3fb37703 8562 }
79e53945 8563
ab585dea 8564 if (is_lvds && has_reduced_clock)
c7653199 8565 crtc->lowfreq_avail = true;
bcd644e0 8566 else
c7653199 8567 crtc->lowfreq_avail = false;
e2b78267 8568
c8f7a0db 8569 return 0;
79e53945
JB
8570}
8571
eb14cb74
VS
8572static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8573 struct intel_link_m_n *m_n)
8574{
8575 struct drm_device *dev = crtc->base.dev;
8576 struct drm_i915_private *dev_priv = dev->dev_private;
8577 enum pipe pipe = crtc->pipe;
8578
8579 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8580 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8581 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8582 & ~TU_SIZE_MASK;
8583 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8584 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8585 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8586}
8587
8588static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8589 enum transcoder transcoder,
b95af8be
VK
8590 struct intel_link_m_n *m_n,
8591 struct intel_link_m_n *m2_n2)
72419203
DV
8592{
8593 struct drm_device *dev = crtc->base.dev;
8594 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8595 enum pipe pipe = crtc->pipe;
72419203 8596
eb14cb74
VS
8597 if (INTEL_INFO(dev)->gen >= 5) {
8598 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8599 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8600 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8601 & ~TU_SIZE_MASK;
8602 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8603 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8604 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8605 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8606 * gen < 8) and if DRRS is supported (to make sure the
8607 * registers are not unnecessarily read).
8608 */
8609 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8610 crtc->config->has_drrs) {
b95af8be
VK
8611 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8612 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8613 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8614 & ~TU_SIZE_MASK;
8615 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8616 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8617 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8618 }
eb14cb74
VS
8619 } else {
8620 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8621 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8622 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8623 & ~TU_SIZE_MASK;
8624 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8625 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8626 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8627 }
8628}
8629
8630void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8631 struct intel_crtc_state *pipe_config)
eb14cb74 8632{
681a8504 8633 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8634 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8635 else
8636 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8637 &pipe_config->dp_m_n,
8638 &pipe_config->dp_m2_n2);
eb14cb74 8639}
72419203 8640
eb14cb74 8641static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8642 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8643{
8644 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8645 &pipe_config->fdi_m_n, NULL);
72419203
DV
8646}
8647
bd2e244f 8648static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8649 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8650{
8651 struct drm_device *dev = crtc->base.dev;
8652 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8653 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8654 uint32_t ps_ctrl = 0;
8655 int id = -1;
8656 int i;
bd2e244f 8657
a1b2278e
CK
8658 /* find scaler attached to this pipe */
8659 for (i = 0; i < crtc->num_scalers; i++) {
8660 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8661 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8662 id = i;
8663 pipe_config->pch_pfit.enabled = true;
8664 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8665 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8666 break;
8667 }
8668 }
bd2e244f 8669
a1b2278e
CK
8670 scaler_state->scaler_id = id;
8671 if (id >= 0) {
8672 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8673 } else {
8674 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8675 }
8676}
8677
5724dbd1
DL
8678static void
8679skylake_get_initial_plane_config(struct intel_crtc *crtc,
8680 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8681{
8682 struct drm_device *dev = crtc->base.dev;
8683 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8684 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8685 int pipe = crtc->pipe;
8686 int fourcc, pixel_format;
6761dd31 8687 unsigned int aligned_height;
bc8d7dff 8688 struct drm_framebuffer *fb;
1b842c89 8689 struct intel_framebuffer *intel_fb;
bc8d7dff 8690
d9806c9f 8691 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8692 if (!intel_fb) {
bc8d7dff
DL
8693 DRM_DEBUG_KMS("failed to alloc fb\n");
8694 return;
8695 }
8696
1b842c89
DL
8697 fb = &intel_fb->base;
8698
bc8d7dff 8699 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8700 if (!(val & PLANE_CTL_ENABLE))
8701 goto error;
8702
bc8d7dff
DL
8703 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8704 fourcc = skl_format_to_fourcc(pixel_format,
8705 val & PLANE_CTL_ORDER_RGBX,
8706 val & PLANE_CTL_ALPHA_MASK);
8707 fb->pixel_format = fourcc;
8708 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8709
40f46283
DL
8710 tiling = val & PLANE_CTL_TILED_MASK;
8711 switch (tiling) {
8712 case PLANE_CTL_TILED_LINEAR:
8713 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8714 break;
8715 case PLANE_CTL_TILED_X:
8716 plane_config->tiling = I915_TILING_X;
8717 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8718 break;
8719 case PLANE_CTL_TILED_Y:
8720 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8721 break;
8722 case PLANE_CTL_TILED_YF:
8723 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8724 break;
8725 default:
8726 MISSING_CASE(tiling);
8727 goto error;
8728 }
8729
bc8d7dff
DL
8730 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8731 plane_config->base = base;
8732
8733 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8734
8735 val = I915_READ(PLANE_SIZE(pipe, 0));
8736 fb->height = ((val >> 16) & 0xfff) + 1;
8737 fb->width = ((val >> 0) & 0x1fff) + 1;
8738
8739 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
8740 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8741 fb->pixel_format);
bc8d7dff
DL
8742 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8743
8744 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8745 fb->pixel_format,
8746 fb->modifier[0]);
bc8d7dff 8747
f37b5c2b 8748 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8749
8750 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8751 pipe_name(pipe), fb->width, fb->height,
8752 fb->bits_per_pixel, base, fb->pitches[0],
8753 plane_config->size);
8754
2d14030b 8755 plane_config->fb = intel_fb;
bc8d7dff
DL
8756 return;
8757
8758error:
8759 kfree(fb);
8760}
8761
2fa2fe9a 8762static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8763 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8764{
8765 struct drm_device *dev = crtc->base.dev;
8766 struct drm_i915_private *dev_priv = dev->dev_private;
8767 uint32_t tmp;
8768
8769 tmp = I915_READ(PF_CTL(crtc->pipe));
8770
8771 if (tmp & PF_ENABLE) {
fd4daa9c 8772 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8773 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8774 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8775
8776 /* We currently do not free assignements of panel fitters on
8777 * ivb/hsw (since we don't use the higher upscaling modes which
8778 * differentiates them) so just WARN about this case for now. */
8779 if (IS_GEN7(dev)) {
8780 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8781 PF_PIPE_SEL_IVB(crtc->pipe));
8782 }
2fa2fe9a 8783 }
79e53945
JB
8784}
8785
5724dbd1
DL
8786static void
8787ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8788 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8789{
8790 struct drm_device *dev = crtc->base.dev;
8791 struct drm_i915_private *dev_priv = dev->dev_private;
8792 u32 val, base, offset;
aeee5a49 8793 int pipe = crtc->pipe;
4c6baa59 8794 int fourcc, pixel_format;
6761dd31 8795 unsigned int aligned_height;
b113d5ee 8796 struct drm_framebuffer *fb;
1b842c89 8797 struct intel_framebuffer *intel_fb;
4c6baa59 8798
42a7b088
DL
8799 val = I915_READ(DSPCNTR(pipe));
8800 if (!(val & DISPLAY_PLANE_ENABLE))
8801 return;
8802
d9806c9f 8803 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8804 if (!intel_fb) {
4c6baa59
JB
8805 DRM_DEBUG_KMS("failed to alloc fb\n");
8806 return;
8807 }
8808
1b842c89
DL
8809 fb = &intel_fb->base;
8810
18c5247e
DV
8811 if (INTEL_INFO(dev)->gen >= 4) {
8812 if (val & DISPPLANE_TILED) {
49af449b 8813 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8814 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8815 }
8816 }
4c6baa59
JB
8817
8818 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8819 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8820 fb->pixel_format = fourcc;
8821 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8822
aeee5a49 8823 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8824 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8825 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8826 } else {
49af449b 8827 if (plane_config->tiling)
aeee5a49 8828 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8829 else
aeee5a49 8830 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8831 }
8832 plane_config->base = base;
8833
8834 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8835 fb->width = ((val >> 16) & 0xfff) + 1;
8836 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8837
8838 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8839 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8840
b113d5ee 8841 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8842 fb->pixel_format,
8843 fb->modifier[0]);
4c6baa59 8844
f37b5c2b 8845 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8846
2844a921
DL
8847 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8848 pipe_name(pipe), fb->width, fb->height,
8849 fb->bits_per_pixel, base, fb->pitches[0],
8850 plane_config->size);
b113d5ee 8851
2d14030b 8852 plane_config->fb = intel_fb;
4c6baa59
JB
8853}
8854
0e8ffe1b 8855static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8856 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8857{
8858 struct drm_device *dev = crtc->base.dev;
8859 struct drm_i915_private *dev_priv = dev->dev_private;
8860 uint32_t tmp;
8861
f458ebbc
DV
8862 if (!intel_display_power_is_enabled(dev_priv,
8863 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8864 return false;
8865
e143a21c 8866 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8867 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8868
0e8ffe1b
DV
8869 tmp = I915_READ(PIPECONF(crtc->pipe));
8870 if (!(tmp & PIPECONF_ENABLE))
8871 return false;
8872
42571aef
VS
8873 switch (tmp & PIPECONF_BPC_MASK) {
8874 case PIPECONF_6BPC:
8875 pipe_config->pipe_bpp = 18;
8876 break;
8877 case PIPECONF_8BPC:
8878 pipe_config->pipe_bpp = 24;
8879 break;
8880 case PIPECONF_10BPC:
8881 pipe_config->pipe_bpp = 30;
8882 break;
8883 case PIPECONF_12BPC:
8884 pipe_config->pipe_bpp = 36;
8885 break;
8886 default:
8887 break;
8888 }
8889
b5a9fa09
DV
8890 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8891 pipe_config->limited_color_range = true;
8892
ab9412ba 8893 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8894 struct intel_shared_dpll *pll;
8895
88adfff1
DV
8896 pipe_config->has_pch_encoder = true;
8897
627eb5a3
DV
8898 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8899 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8900 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8901
8902 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8903
c0d43d62 8904 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8905 pipe_config->shared_dpll =
8906 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8907 } else {
8908 tmp = I915_READ(PCH_DPLL_SEL);
8909 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8910 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8911 else
8912 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8913 }
66e985c0
DV
8914
8915 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8916
8917 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8918 &pipe_config->dpll_hw_state));
c93f54cf
DV
8919
8920 tmp = pipe_config->dpll_hw_state.dpll;
8921 pipe_config->pixel_multiplier =
8922 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8923 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8924
8925 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8926 } else {
8927 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8928 }
8929
1bd1bd80
DV
8930 intel_get_pipe_timings(crtc, pipe_config);
8931
2fa2fe9a
DV
8932 ironlake_get_pfit_config(crtc, pipe_config);
8933
0e8ffe1b
DV
8934 return true;
8935}
8936
be256dc7
PZ
8937static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8938{
8939 struct drm_device *dev = dev_priv->dev;
be256dc7 8940 struct intel_crtc *crtc;
be256dc7 8941
d3fcc808 8942 for_each_intel_crtc(dev, crtc)
e2c719b7 8943 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8944 pipe_name(crtc->pipe));
8945
e2c719b7
RC
8946 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8947 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8948 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8949 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8950 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8951 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8952 "CPU PWM1 enabled\n");
c5107b87 8953 if (IS_HASWELL(dev))
e2c719b7 8954 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8955 "CPU PWM2 enabled\n");
e2c719b7 8956 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8957 "PCH PWM1 enabled\n");
e2c719b7 8958 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8959 "Utility pin enabled\n");
e2c719b7 8960 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8961
9926ada1
PZ
8962 /*
8963 * In theory we can still leave IRQs enabled, as long as only the HPD
8964 * interrupts remain enabled. We used to check for that, but since it's
8965 * gen-specific and since we only disable LCPLL after we fully disable
8966 * the interrupts, the check below should be enough.
8967 */
e2c719b7 8968 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8969}
8970
9ccd5aeb
PZ
8971static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8972{
8973 struct drm_device *dev = dev_priv->dev;
8974
8975 if (IS_HASWELL(dev))
8976 return I915_READ(D_COMP_HSW);
8977 else
8978 return I915_READ(D_COMP_BDW);
8979}
8980
3c4c9b81
PZ
8981static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8982{
8983 struct drm_device *dev = dev_priv->dev;
8984
8985 if (IS_HASWELL(dev)) {
8986 mutex_lock(&dev_priv->rps.hw_lock);
8987 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8988 val))
f475dadf 8989 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8990 mutex_unlock(&dev_priv->rps.hw_lock);
8991 } else {
9ccd5aeb
PZ
8992 I915_WRITE(D_COMP_BDW, val);
8993 POSTING_READ(D_COMP_BDW);
3c4c9b81 8994 }
be256dc7
PZ
8995}
8996
8997/*
8998 * This function implements pieces of two sequences from BSpec:
8999 * - Sequence for display software to disable LCPLL
9000 * - Sequence for display software to allow package C8+
9001 * The steps implemented here are just the steps that actually touch the LCPLL
9002 * register. Callers should take care of disabling all the display engine
9003 * functions, doing the mode unset, fixing interrupts, etc.
9004 */
6ff58d53
PZ
9005static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9006 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9007{
9008 uint32_t val;
9009
9010 assert_can_disable_lcpll(dev_priv);
9011
9012 val = I915_READ(LCPLL_CTL);
9013
9014 if (switch_to_fclk) {
9015 val |= LCPLL_CD_SOURCE_FCLK;
9016 I915_WRITE(LCPLL_CTL, val);
9017
9018 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9019 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9020 DRM_ERROR("Switching to FCLK failed\n");
9021
9022 val = I915_READ(LCPLL_CTL);
9023 }
9024
9025 val |= LCPLL_PLL_DISABLE;
9026 I915_WRITE(LCPLL_CTL, val);
9027 POSTING_READ(LCPLL_CTL);
9028
9029 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9030 DRM_ERROR("LCPLL still locked\n");
9031
9ccd5aeb 9032 val = hsw_read_dcomp(dev_priv);
be256dc7 9033 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9034 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9035 ndelay(100);
9036
9ccd5aeb
PZ
9037 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9038 1))
be256dc7
PZ
9039 DRM_ERROR("D_COMP RCOMP still in progress\n");
9040
9041 if (allow_power_down) {
9042 val = I915_READ(LCPLL_CTL);
9043 val |= LCPLL_POWER_DOWN_ALLOW;
9044 I915_WRITE(LCPLL_CTL, val);
9045 POSTING_READ(LCPLL_CTL);
9046 }
9047}
9048
9049/*
9050 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9051 * source.
9052 */
6ff58d53 9053static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9054{
9055 uint32_t val;
9056
9057 val = I915_READ(LCPLL_CTL);
9058
9059 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9060 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9061 return;
9062
a8a8bd54
PZ
9063 /*
9064 * Make sure we're not on PC8 state before disabling PC8, otherwise
9065 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9066 */
59bad947 9067 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9068
be256dc7
PZ
9069 if (val & LCPLL_POWER_DOWN_ALLOW) {
9070 val &= ~LCPLL_POWER_DOWN_ALLOW;
9071 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9072 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9073 }
9074
9ccd5aeb 9075 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9076 val |= D_COMP_COMP_FORCE;
9077 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9078 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9079
9080 val = I915_READ(LCPLL_CTL);
9081 val &= ~LCPLL_PLL_DISABLE;
9082 I915_WRITE(LCPLL_CTL, val);
9083
9084 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9085 DRM_ERROR("LCPLL not locked yet\n");
9086
9087 if (val & LCPLL_CD_SOURCE_FCLK) {
9088 val = I915_READ(LCPLL_CTL);
9089 val &= ~LCPLL_CD_SOURCE_FCLK;
9090 I915_WRITE(LCPLL_CTL, val);
9091
9092 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9093 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9094 DRM_ERROR("Switching back to LCPLL failed\n");
9095 }
215733fa 9096
59bad947 9097 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
9098}
9099
765dab67
PZ
9100/*
9101 * Package states C8 and deeper are really deep PC states that can only be
9102 * reached when all the devices on the system allow it, so even if the graphics
9103 * device allows PC8+, it doesn't mean the system will actually get to these
9104 * states. Our driver only allows PC8+ when going into runtime PM.
9105 *
9106 * The requirements for PC8+ are that all the outputs are disabled, the power
9107 * well is disabled and most interrupts are disabled, and these are also
9108 * requirements for runtime PM. When these conditions are met, we manually do
9109 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9110 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9111 * hang the machine.
9112 *
9113 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9114 * the state of some registers, so when we come back from PC8+ we need to
9115 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9116 * need to take care of the registers kept by RC6. Notice that this happens even
9117 * if we don't put the device in PCI D3 state (which is what currently happens
9118 * because of the runtime PM support).
9119 *
9120 * For more, read "Display Sequences for Package C8" on the hardware
9121 * documentation.
9122 */
a14cb6fc 9123void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9124{
c67a470b
PZ
9125 struct drm_device *dev = dev_priv->dev;
9126 uint32_t val;
9127
c67a470b
PZ
9128 DRM_DEBUG_KMS("Enabling package C8+\n");
9129
c67a470b
PZ
9130 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9131 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9132 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9133 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9134 }
9135
9136 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9137 hsw_disable_lcpll(dev_priv, true, true);
9138}
9139
a14cb6fc 9140void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9141{
9142 struct drm_device *dev = dev_priv->dev;
9143 uint32_t val;
9144
c67a470b
PZ
9145 DRM_DEBUG_KMS("Disabling package C8+\n");
9146
9147 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9148 lpt_init_pch_refclk(dev);
9149
9150 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9151 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9152 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9153 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9154 }
9155
9156 intel_prepare_ddi(dev);
c67a470b
PZ
9157}
9158
f8437dd1
VK
9159static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9160{
9161 struct drm_device *dev = state->dev;
9162 struct drm_i915_private *dev_priv = dev->dev_private;
9163 int max_pixclk = intel_mode_max_pixclk(state);
9164 int req_cdclk;
9165
9166 /* see the comment in valleyview_modeset_global_resources */
9167 if (WARN_ON(max_pixclk < 0))
9168 return;
9169
9170 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9171
9172 if (req_cdclk != dev_priv->cdclk_freq)
9173 broxton_set_cdclk(dev, req_cdclk);
9174}
9175
190f68c5
ACO
9176static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9177 struct intel_crtc_state *crtc_state)
09b4ddf9 9178{
190f68c5 9179 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9180 return -EINVAL;
716c2e55 9181
c7653199 9182 crtc->lowfreq_avail = false;
644cef34 9183
c8f7a0db 9184 return 0;
79e53945
JB
9185}
9186
3760b59c
S
9187static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9188 enum port port,
9189 struct intel_crtc_state *pipe_config)
9190{
9191 switch (port) {
9192 case PORT_A:
9193 pipe_config->ddi_pll_sel = SKL_DPLL0;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9195 break;
9196 case PORT_B:
9197 pipe_config->ddi_pll_sel = SKL_DPLL1;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9199 break;
9200 case PORT_C:
9201 pipe_config->ddi_pll_sel = SKL_DPLL2;
9202 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9203 break;
9204 default:
9205 DRM_ERROR("Incorrect port type\n");
9206 }
9207}
9208
96b7dfb7
S
9209static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9210 enum port port,
5cec258b 9211 struct intel_crtc_state *pipe_config)
96b7dfb7 9212{
3148ade7 9213 u32 temp, dpll_ctl1;
96b7dfb7
S
9214
9215 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9216 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9217
9218 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9219 case SKL_DPLL0:
9220 /*
9221 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9222 * of the shared DPLL framework and thus needs to be read out
9223 * separately
9224 */
9225 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9226 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9227 break;
96b7dfb7
S
9228 case SKL_DPLL1:
9229 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9230 break;
9231 case SKL_DPLL2:
9232 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9233 break;
9234 case SKL_DPLL3:
9235 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9236 break;
96b7dfb7
S
9237 }
9238}
9239
7d2c8175
DL
9240static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9241 enum port port,
5cec258b 9242 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9243{
9244 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9245
9246 switch (pipe_config->ddi_pll_sel) {
9247 case PORT_CLK_SEL_WRPLL1:
9248 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9249 break;
9250 case PORT_CLK_SEL_WRPLL2:
9251 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9252 break;
9253 }
9254}
9255
26804afd 9256static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9257 struct intel_crtc_state *pipe_config)
26804afd
DV
9258{
9259 struct drm_device *dev = crtc->base.dev;
9260 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9261 struct intel_shared_dpll *pll;
26804afd
DV
9262 enum port port;
9263 uint32_t tmp;
9264
9265 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9266
9267 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9268
96b7dfb7
S
9269 if (IS_SKYLAKE(dev))
9270 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9271 else if (IS_BROXTON(dev))
9272 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9273 else
9274 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9275
d452c5b6
DV
9276 if (pipe_config->shared_dpll >= 0) {
9277 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9278
9279 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9280 &pipe_config->dpll_hw_state));
9281 }
9282
26804afd
DV
9283 /*
9284 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9285 * DDI E. So just check whether this pipe is wired to DDI E and whether
9286 * the PCH transcoder is on.
9287 */
ca370455
DL
9288 if (INTEL_INFO(dev)->gen < 9 &&
9289 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9290 pipe_config->has_pch_encoder = true;
9291
9292 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9293 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9294 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9295
9296 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9297 }
9298}
9299
0e8ffe1b 9300static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9301 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9302{
9303 struct drm_device *dev = crtc->base.dev;
9304 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9305 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9306 uint32_t tmp;
9307
f458ebbc 9308 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9309 POWER_DOMAIN_PIPE(crtc->pipe)))
9310 return false;
9311
e143a21c 9312 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9313 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9314
eccb140b
DV
9315 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9316 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9317 enum pipe trans_edp_pipe;
9318 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9319 default:
9320 WARN(1, "unknown pipe linked to edp transcoder\n");
9321 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9322 case TRANS_DDI_EDP_INPUT_A_ON:
9323 trans_edp_pipe = PIPE_A;
9324 break;
9325 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9326 trans_edp_pipe = PIPE_B;
9327 break;
9328 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9329 trans_edp_pipe = PIPE_C;
9330 break;
9331 }
9332
9333 if (trans_edp_pipe == crtc->pipe)
9334 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9335 }
9336
f458ebbc 9337 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9338 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9339 return false;
9340
eccb140b 9341 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9342 if (!(tmp & PIPECONF_ENABLE))
9343 return false;
9344
26804afd 9345 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9346
1bd1bd80
DV
9347 intel_get_pipe_timings(crtc, pipe_config);
9348
a1b2278e
CK
9349 if (INTEL_INFO(dev)->gen >= 9) {
9350 skl_init_scalers(dev, crtc, pipe_config);
9351 }
9352
2fa2fe9a 9353 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f 9354 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9355 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9356 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9357 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9358 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9359 else
9360 MISSING_CASE(INTEL_INFO(dev)->gen);
9361
a1b2278e
CK
9362 } else {
9363 pipe_config->scaler_state.scaler_id = -1;
9364 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f 9365 }
88adfff1 9366
e59150dc
JB
9367 if (IS_HASWELL(dev))
9368 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9369 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9370
ebb69c95
CT
9371 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9372 pipe_config->pixel_multiplier =
9373 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9374 } else {
9375 pipe_config->pixel_multiplier = 1;
9376 }
6c49f241 9377
0e8ffe1b
DV
9378 return true;
9379}
9380
560b85bb
CW
9381static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9382{
9383 struct drm_device *dev = crtc->dev;
9384 struct drm_i915_private *dev_priv = dev->dev_private;
9385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9386 uint32_t cntl = 0, size = 0;
560b85bb 9387
dc41c154 9388 if (base) {
3dd512fb
MR
9389 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9390 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9391 unsigned int stride = roundup_pow_of_two(width) * 4;
9392
9393 switch (stride) {
9394 default:
9395 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9396 width, stride);
9397 stride = 256;
9398 /* fallthrough */
9399 case 256:
9400 case 512:
9401 case 1024:
9402 case 2048:
9403 break;
4b0e333e
CW
9404 }
9405
dc41c154
VS
9406 cntl |= CURSOR_ENABLE |
9407 CURSOR_GAMMA_ENABLE |
9408 CURSOR_FORMAT_ARGB |
9409 CURSOR_STRIDE(stride);
9410
9411 size = (height << 12) | width;
4b0e333e 9412 }
560b85bb 9413
dc41c154
VS
9414 if (intel_crtc->cursor_cntl != 0 &&
9415 (intel_crtc->cursor_base != base ||
9416 intel_crtc->cursor_size != size ||
9417 intel_crtc->cursor_cntl != cntl)) {
9418 /* On these chipsets we can only modify the base/size/stride
9419 * whilst the cursor is disabled.
9420 */
9421 I915_WRITE(_CURACNTR, 0);
4b0e333e 9422 POSTING_READ(_CURACNTR);
dc41c154 9423 intel_crtc->cursor_cntl = 0;
4b0e333e 9424 }
560b85bb 9425
99d1f387 9426 if (intel_crtc->cursor_base != base) {
9db4a9c7 9427 I915_WRITE(_CURABASE, base);
99d1f387
VS
9428 intel_crtc->cursor_base = base;
9429 }
4726e0b0 9430
dc41c154
VS
9431 if (intel_crtc->cursor_size != size) {
9432 I915_WRITE(CURSIZE, size);
9433 intel_crtc->cursor_size = size;
4b0e333e 9434 }
560b85bb 9435
4b0e333e 9436 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9437 I915_WRITE(_CURACNTR, cntl);
9438 POSTING_READ(_CURACNTR);
4b0e333e 9439 intel_crtc->cursor_cntl = cntl;
560b85bb 9440 }
560b85bb
CW
9441}
9442
560b85bb 9443static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9444{
9445 struct drm_device *dev = crtc->dev;
9446 struct drm_i915_private *dev_priv = dev->dev_private;
9447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9448 int pipe = intel_crtc->pipe;
4b0e333e
CW
9449 uint32_t cntl;
9450
9451 cntl = 0;
9452 if (base) {
9453 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9454 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9455 case 64:
9456 cntl |= CURSOR_MODE_64_ARGB_AX;
9457 break;
9458 case 128:
9459 cntl |= CURSOR_MODE_128_ARGB_AX;
9460 break;
9461 case 256:
9462 cntl |= CURSOR_MODE_256_ARGB_AX;
9463 break;
9464 default:
3dd512fb 9465 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9466 return;
65a21cd6 9467 }
4b0e333e 9468 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9469
9470 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9471 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9472 }
65a21cd6 9473
8e7d688b 9474 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9475 cntl |= CURSOR_ROTATE_180;
9476
4b0e333e
CW
9477 if (intel_crtc->cursor_cntl != cntl) {
9478 I915_WRITE(CURCNTR(pipe), cntl);
9479 POSTING_READ(CURCNTR(pipe));
9480 intel_crtc->cursor_cntl = cntl;
65a21cd6 9481 }
4b0e333e 9482
65a21cd6 9483 /* and commit changes on next vblank */
5efb3e28
VS
9484 I915_WRITE(CURBASE(pipe), base);
9485 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9486
9487 intel_crtc->cursor_base = base;
65a21cd6
JB
9488}
9489
cda4b7d3 9490/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9491static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9492 bool on)
cda4b7d3
CW
9493{
9494 struct drm_device *dev = crtc->dev;
9495 struct drm_i915_private *dev_priv = dev->dev_private;
9496 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9497 int pipe = intel_crtc->pipe;
3d7d6510
MR
9498 int x = crtc->cursor_x;
9499 int y = crtc->cursor_y;
d6e4db15 9500 u32 base = 0, pos = 0;
cda4b7d3 9501
d6e4db15 9502 if (on)
cda4b7d3 9503 base = intel_crtc->cursor_addr;
cda4b7d3 9504
6e3c9717 9505 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9506 base = 0;
9507
6e3c9717 9508 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9509 base = 0;
9510
9511 if (x < 0) {
3dd512fb 9512 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9513 base = 0;
9514
9515 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9516 x = -x;
9517 }
9518 pos |= x << CURSOR_X_SHIFT;
9519
9520 if (y < 0) {
3dd512fb 9521 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9522 base = 0;
9523
9524 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9525 y = -y;
9526 }
9527 pos |= y << CURSOR_Y_SHIFT;
9528
4b0e333e 9529 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9530 return;
9531
5efb3e28
VS
9532 I915_WRITE(CURPOS(pipe), pos);
9533
4398ad45
VS
9534 /* ILK+ do this automagically */
9535 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9536 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9537 base += (intel_crtc->base.cursor->state->crtc_h *
9538 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9539 }
9540
8ac54669 9541 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9542 i845_update_cursor(crtc, base);
9543 else
9544 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9545}
9546
dc41c154
VS
9547static bool cursor_size_ok(struct drm_device *dev,
9548 uint32_t width, uint32_t height)
9549{
9550 if (width == 0 || height == 0)
9551 return false;
9552
9553 /*
9554 * 845g/865g are special in that they are only limited by
9555 * the width of their cursors, the height is arbitrary up to
9556 * the precision of the register. Everything else requires
9557 * square cursors, limited to a few power-of-two sizes.
9558 */
9559 if (IS_845G(dev) || IS_I865G(dev)) {
9560 if ((width & 63) != 0)
9561 return false;
9562
9563 if (width > (IS_845G(dev) ? 64 : 512))
9564 return false;
9565
9566 if (height > 1023)
9567 return false;
9568 } else {
9569 switch (width | height) {
9570 case 256:
9571 case 128:
9572 if (IS_GEN2(dev))
9573 return false;
9574 case 64:
9575 break;
9576 default:
9577 return false;
9578 }
9579 }
9580
9581 return true;
9582}
9583
79e53945 9584static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9585 u16 *blue, uint32_t start, uint32_t size)
79e53945 9586{
7203425a 9587 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9589
7203425a 9590 for (i = start; i < end; i++) {
79e53945
JB
9591 intel_crtc->lut_r[i] = red[i] >> 8;
9592 intel_crtc->lut_g[i] = green[i] >> 8;
9593 intel_crtc->lut_b[i] = blue[i] >> 8;
9594 }
9595
9596 intel_crtc_load_lut(crtc);
9597}
9598
79e53945
JB
9599/* VESA 640x480x72Hz mode to set on the pipe */
9600static struct drm_display_mode load_detect_mode = {
9601 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9602 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9603};
9604
a8bb6818
DV
9605struct drm_framebuffer *
9606__intel_framebuffer_create(struct drm_device *dev,
9607 struct drm_mode_fb_cmd2 *mode_cmd,
9608 struct drm_i915_gem_object *obj)
d2dff872
CW
9609{
9610 struct intel_framebuffer *intel_fb;
9611 int ret;
9612
9613 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9614 if (!intel_fb) {
6ccb81f2 9615 drm_gem_object_unreference(&obj->base);
d2dff872
CW
9616 return ERR_PTR(-ENOMEM);
9617 }
9618
9619 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
9620 if (ret)
9621 goto err;
d2dff872
CW
9622
9623 return &intel_fb->base;
dd4916c5 9624err:
6ccb81f2 9625 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
9626 kfree(intel_fb);
9627
9628 return ERR_PTR(ret);
d2dff872
CW
9629}
9630
b5ea642a 9631static struct drm_framebuffer *
a8bb6818
DV
9632intel_framebuffer_create(struct drm_device *dev,
9633 struct drm_mode_fb_cmd2 *mode_cmd,
9634 struct drm_i915_gem_object *obj)
9635{
9636 struct drm_framebuffer *fb;
9637 int ret;
9638
9639 ret = i915_mutex_lock_interruptible(dev);
9640 if (ret)
9641 return ERR_PTR(ret);
9642 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9643 mutex_unlock(&dev->struct_mutex);
9644
9645 return fb;
9646}
9647
d2dff872
CW
9648static u32
9649intel_framebuffer_pitch_for_width(int width, int bpp)
9650{
9651 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9652 return ALIGN(pitch, 64);
9653}
9654
9655static u32
9656intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9657{
9658 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9659 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9660}
9661
9662static struct drm_framebuffer *
9663intel_framebuffer_create_for_mode(struct drm_device *dev,
9664 struct drm_display_mode *mode,
9665 int depth, int bpp)
9666{
9667 struct drm_i915_gem_object *obj;
0fed39bd 9668 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
9669
9670 obj = i915_gem_alloc_object(dev,
9671 intel_framebuffer_size_for_mode(mode, bpp));
9672 if (obj == NULL)
9673 return ERR_PTR(-ENOMEM);
9674
9675 mode_cmd.width = mode->hdisplay;
9676 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9677 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9678 bpp);
5ca0c34a 9679 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
9680
9681 return intel_framebuffer_create(dev, &mode_cmd, obj);
9682}
9683
9684static struct drm_framebuffer *
9685mode_fits_in_fbdev(struct drm_device *dev,
9686 struct drm_display_mode *mode)
9687{
4520f53a 9688#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
9689 struct drm_i915_private *dev_priv = dev->dev_private;
9690 struct drm_i915_gem_object *obj;
9691 struct drm_framebuffer *fb;
9692
4c0e5528 9693 if (!dev_priv->fbdev)
d2dff872
CW
9694 return NULL;
9695
4c0e5528 9696 if (!dev_priv->fbdev->fb)
d2dff872
CW
9697 return NULL;
9698
4c0e5528
DV
9699 obj = dev_priv->fbdev->fb->obj;
9700 BUG_ON(!obj);
9701
8bcd4553 9702 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
9703 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9704 fb->bits_per_pixel))
d2dff872
CW
9705 return NULL;
9706
01f2c773 9707 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9708 return NULL;
9709
9710 return fb;
4520f53a
DV
9711#else
9712 return NULL;
9713#endif
d2dff872
CW
9714}
9715
d2434ab7 9716bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9717 struct drm_display_mode *mode,
51fd371b
RC
9718 struct intel_load_detect_pipe *old,
9719 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9720{
9721 struct intel_crtc *intel_crtc;
d2434ab7
DV
9722 struct intel_encoder *intel_encoder =
9723 intel_attached_encoder(connector);
79e53945 9724 struct drm_crtc *possible_crtc;
4ef69c7a 9725 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9726 struct drm_crtc *crtc = NULL;
9727 struct drm_device *dev = encoder->dev;
94352cf9 9728 struct drm_framebuffer *fb;
51fd371b 9729 struct drm_mode_config *config = &dev->mode_config;
83a57153 9730 struct drm_atomic_state *state = NULL;
944b0c76 9731 struct drm_connector_state *connector_state;
51fd371b 9732 int ret, i = -1;
79e53945 9733
d2dff872 9734 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9735 connector->base.id, connector->name,
8e329a03 9736 encoder->base.id, encoder->name);
d2dff872 9737
51fd371b
RC
9738retry:
9739 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9740 if (ret)
9741 goto fail_unlock;
6e9f798d 9742
79e53945
JB
9743 /*
9744 * Algorithm gets a little messy:
7a5e4805 9745 *
79e53945
JB
9746 * - if the connector already has an assigned crtc, use it (but make
9747 * sure it's on first)
7a5e4805 9748 *
79e53945
JB
9749 * - try to find the first unused crtc that can drive this connector,
9750 * and use that if we find one
79e53945
JB
9751 */
9752
9753 /* See if we already have a CRTC for this connector */
9754 if (encoder->crtc) {
9755 crtc = encoder->crtc;
8261b191 9756
51fd371b 9757 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
9758 if (ret)
9759 goto fail_unlock;
9760 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
9761 if (ret)
9762 goto fail_unlock;
7b24056b 9763
24218aac 9764 old->dpms_mode = connector->dpms;
8261b191
CW
9765 old->load_detect_temp = false;
9766
9767 /* Make sure the crtc and connector are running */
24218aac
DV
9768 if (connector->dpms != DRM_MODE_DPMS_ON)
9769 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 9770
7173188d 9771 return true;
79e53945
JB
9772 }
9773
9774 /* Find an unused one (if possible) */
70e1e0ec 9775 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9776 i++;
9777 if (!(encoder->possible_crtcs & (1 << i)))
9778 continue;
83d65738 9779 if (possible_crtc->state->enable)
a459249c
VS
9780 continue;
9781 /* This can occur when applying the pipe A quirk on resume. */
9782 if (to_intel_crtc(possible_crtc)->new_enabled)
9783 continue;
9784
9785 crtc = possible_crtc;
9786 break;
79e53945
JB
9787 }
9788
9789 /*
9790 * If we didn't find an unused CRTC, don't use any.
9791 */
9792 if (!crtc) {
7173188d 9793 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 9794 goto fail_unlock;
79e53945
JB
9795 }
9796
51fd371b
RC
9797 ret = drm_modeset_lock(&crtc->mutex, ctx);
9798 if (ret)
4d02e2de
DV
9799 goto fail_unlock;
9800 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9801 if (ret)
51fd371b 9802 goto fail_unlock;
fc303101
DV
9803 intel_encoder->new_crtc = to_intel_crtc(crtc);
9804 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
9805
9806 intel_crtc = to_intel_crtc(crtc);
412b61d8 9807 intel_crtc->new_enabled = true;
24218aac 9808 old->dpms_mode = connector->dpms;
8261b191 9809 old->load_detect_temp = true;
d2dff872 9810 old->release_fb = NULL;
79e53945 9811
83a57153
ACO
9812 state = drm_atomic_state_alloc(dev);
9813 if (!state)
9814 return false;
9815
9816 state->acquire_ctx = ctx;
9817
944b0c76
ACO
9818 connector_state = drm_atomic_get_connector_state(state, connector);
9819 if (IS_ERR(connector_state)) {
9820 ret = PTR_ERR(connector_state);
9821 goto fail;
9822 }
9823
9824 connector_state->crtc = crtc;
9825 connector_state->best_encoder = &intel_encoder->base;
9826
6492711d
CW
9827 if (!mode)
9828 mode = &load_detect_mode;
79e53945 9829
d2dff872
CW
9830 /* We need a framebuffer large enough to accommodate all accesses
9831 * that the plane may generate whilst we perform load detection.
9832 * We can not rely on the fbcon either being present (we get called
9833 * during its initialisation to detect all boot displays, or it may
9834 * not even exist) or that it is large enough to satisfy the
9835 * requested mode.
9836 */
94352cf9
DV
9837 fb = mode_fits_in_fbdev(dev, mode);
9838 if (fb == NULL) {
d2dff872 9839 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
9840 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9841 old->release_fb = fb;
d2dff872
CW
9842 } else
9843 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9844 if (IS_ERR(fb)) {
d2dff872 9845 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9846 goto fail;
79e53945 9847 }
79e53945 9848
83a57153 9849 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
6492711d 9850 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
9851 if (old->release_fb)
9852 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 9853 goto fail;
79e53945 9854 }
9128b040 9855 crtc->primary->crtc = crtc;
7173188d 9856
79e53945 9857 /* let the connector get through one full cycle before testing */
9d0498a2 9858 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 9859 return true;
412b61d8
VS
9860
9861 fail:
83d65738 9862 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 9863fail_unlock:
83a57153
ACO
9864 if (state) {
9865 drm_atomic_state_free(state);
9866 state = NULL;
9867 }
9868
51fd371b
RC
9869 if (ret == -EDEADLK) {
9870 drm_modeset_backoff(ctx);
9871 goto retry;
9872 }
9873
412b61d8 9874 return false;
79e53945
JB
9875}
9876
d2434ab7 9877void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9878 struct intel_load_detect_pipe *old,
9879 struct drm_modeset_acquire_ctx *ctx)
79e53945 9880{
83a57153 9881 struct drm_device *dev = connector->dev;
d2434ab7
DV
9882 struct intel_encoder *intel_encoder =
9883 intel_attached_encoder(connector);
4ef69c7a 9884 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9885 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 9887 struct drm_atomic_state *state;
944b0c76 9888 struct drm_connector_state *connector_state;
79e53945 9889
d2dff872 9890 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9891 connector->base.id, connector->name,
8e329a03 9892 encoder->base.id, encoder->name);
d2dff872 9893
8261b191 9894 if (old->load_detect_temp) {
83a57153 9895 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
9896 if (!state)
9897 goto fail;
83a57153
ACO
9898
9899 state->acquire_ctx = ctx;
9900
944b0c76
ACO
9901 connector_state = drm_atomic_get_connector_state(state, connector);
9902 if (IS_ERR(connector_state))
9903 goto fail;
9904
fc303101
DV
9905 to_intel_connector(connector)->new_encoder = NULL;
9906 intel_encoder->new_crtc = NULL;
412b61d8 9907 intel_crtc->new_enabled = false;
944b0c76
ACO
9908
9909 connector_state->best_encoder = NULL;
9910 connector_state->crtc = NULL;
9911
83a57153
ACO
9912 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9913
9914 drm_atomic_state_free(state);
d2dff872 9915
36206361
DV
9916 if (old->release_fb) {
9917 drm_framebuffer_unregister_private(old->release_fb);
9918 drm_framebuffer_unreference(old->release_fb);
9919 }
d2dff872 9920
0622a53c 9921 return;
79e53945
JB
9922 }
9923
c751ce4f 9924 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9925 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9926 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
9927
9928 return;
9929fail:
9930 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9931 drm_atomic_state_free(state);
79e53945
JB
9932}
9933
da4a1efa 9934static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9935 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9936{
9937 struct drm_i915_private *dev_priv = dev->dev_private;
9938 u32 dpll = pipe_config->dpll_hw_state.dpll;
9939
9940 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9941 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9942 else if (HAS_PCH_SPLIT(dev))
9943 return 120000;
9944 else if (!IS_GEN2(dev))
9945 return 96000;
9946 else
9947 return 48000;
9948}
9949
79e53945 9950/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9951static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9952 struct intel_crtc_state *pipe_config)
79e53945 9953{
f1f644dc 9954 struct drm_device *dev = crtc->base.dev;
79e53945 9955 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 9956 int pipe = pipe_config->cpu_transcoder;
293623f7 9957 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
9958 u32 fp;
9959 intel_clock_t clock;
da4a1efa 9960 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9961
9962 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9963 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9964 else
293623f7 9965 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9966
9967 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
9968 if (IS_PINEVIEW(dev)) {
9969 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9970 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9971 } else {
9972 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9973 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9974 }
9975
a6c45cf0 9976 if (!IS_GEN2(dev)) {
f2b115e6
AJ
9977 if (IS_PINEVIEW(dev))
9978 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9979 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9980 else
9981 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9982 DPLL_FPA01_P1_POST_DIV_SHIFT);
9983
9984 switch (dpll & DPLL_MODE_MASK) {
9985 case DPLLB_MODE_DAC_SERIAL:
9986 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9987 5 : 10;
9988 break;
9989 case DPLLB_MODE_LVDS:
9990 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9991 7 : 14;
9992 break;
9993 default:
28c97730 9994 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9995 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9996 return;
79e53945
JB
9997 }
9998
ac58c3f0 9999 if (IS_PINEVIEW(dev))
da4a1efa 10000 pineview_clock(refclk, &clock);
ac58c3f0 10001 else
da4a1efa 10002 i9xx_clock(refclk, &clock);
79e53945 10003 } else {
0fb58223 10004 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10005 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10006
10007 if (is_lvds) {
10008 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10009 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10010
10011 if (lvds & LVDS_CLKB_POWER_UP)
10012 clock.p2 = 7;
10013 else
10014 clock.p2 = 14;
79e53945
JB
10015 } else {
10016 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10017 clock.p1 = 2;
10018 else {
10019 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10020 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10021 }
10022 if (dpll & PLL_P2_DIVIDE_BY_4)
10023 clock.p2 = 4;
10024 else
10025 clock.p2 = 2;
79e53945 10026 }
da4a1efa
VS
10027
10028 i9xx_clock(refclk, &clock);
79e53945
JB
10029 }
10030
18442d08
VS
10031 /*
10032 * This value includes pixel_multiplier. We will use
241bfc38 10033 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10034 * encoder's get_config() function.
10035 */
10036 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10037}
10038
6878da05
VS
10039int intel_dotclock_calculate(int link_freq,
10040 const struct intel_link_m_n *m_n)
f1f644dc 10041{
f1f644dc
JB
10042 /*
10043 * The calculation for the data clock is:
1041a02f 10044 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10045 * But we want to avoid losing precison if possible, so:
1041a02f 10046 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10047 *
10048 * and the link clock is simpler:
1041a02f 10049 * link_clock = (m * link_clock) / n
f1f644dc
JB
10050 */
10051
6878da05
VS
10052 if (!m_n->link_n)
10053 return 0;
f1f644dc 10054
6878da05
VS
10055 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10056}
f1f644dc 10057
18442d08 10058static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10059 struct intel_crtc_state *pipe_config)
6878da05
VS
10060{
10061 struct drm_device *dev = crtc->base.dev;
79e53945 10062
18442d08
VS
10063 /* read out port_clock from the DPLL */
10064 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10065
f1f644dc 10066 /*
18442d08 10067 * This value does not include pixel_multiplier.
241bfc38 10068 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10069 * agree once we know their relationship in the encoder's
10070 * get_config() function.
79e53945 10071 */
2d112de7 10072 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10073 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10074 &pipe_config->fdi_m_n);
79e53945
JB
10075}
10076
10077/** Returns the currently programmed mode of the given pipe. */
10078struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10079 struct drm_crtc *crtc)
10080{
548f245b 10081 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10083 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10084 struct drm_display_mode *mode;
5cec258b 10085 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10086 int htot = I915_READ(HTOTAL(cpu_transcoder));
10087 int hsync = I915_READ(HSYNC(cpu_transcoder));
10088 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10089 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10090 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10091
10092 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10093 if (!mode)
10094 return NULL;
10095
f1f644dc
JB
10096 /*
10097 * Construct a pipe_config sufficient for getting the clock info
10098 * back out of crtc_clock_get.
10099 *
10100 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10101 * to use a real value here instead.
10102 */
293623f7 10103 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10104 pipe_config.pixel_multiplier = 1;
293623f7
VS
10105 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10106 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10107 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10108 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10109
773ae034 10110 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10111 mode->hdisplay = (htot & 0xffff) + 1;
10112 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10113 mode->hsync_start = (hsync & 0xffff) + 1;
10114 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10115 mode->vdisplay = (vtot & 0xffff) + 1;
10116 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10117 mode->vsync_start = (vsync & 0xffff) + 1;
10118 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10119
10120 drm_mode_set_name(mode);
79e53945
JB
10121
10122 return mode;
10123}
10124
652c393a
JB
10125static void intel_decrease_pllclock(struct drm_crtc *crtc)
10126{
10127 struct drm_device *dev = crtc->dev;
fbee40df 10128 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10130
baff296c 10131 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10132 return;
10133
10134 if (!dev_priv->lvds_downclock_avail)
10135 return;
10136
10137 /*
10138 * Since this is called by a timer, we should never get here in
10139 * the manual case.
10140 */
10141 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10142 int pipe = intel_crtc->pipe;
10143 int dpll_reg = DPLL(pipe);
10144 int dpll;
f6e5b160 10145
44d98a61 10146 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10147
8ac5a6d5 10148 assert_panel_unlocked(dev_priv, pipe);
652c393a 10149
dc257cf1 10150 dpll = I915_READ(dpll_reg);
652c393a
JB
10151 dpll |= DISPLAY_RATE_SELECT_FPA1;
10152 I915_WRITE(dpll_reg, dpll);
9d0498a2 10153 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10154 dpll = I915_READ(dpll_reg);
10155 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10156 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10157 }
10158
10159}
10160
f047e395
CW
10161void intel_mark_busy(struct drm_device *dev)
10162{
c67a470b
PZ
10163 struct drm_i915_private *dev_priv = dev->dev_private;
10164
f62a0076
CW
10165 if (dev_priv->mm.busy)
10166 return;
10167
43694d69 10168 intel_runtime_pm_get(dev_priv);
c67a470b 10169 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10170 if (INTEL_INFO(dev)->gen >= 6)
10171 gen6_rps_busy(dev_priv);
f62a0076 10172 dev_priv->mm.busy = true;
f047e395
CW
10173}
10174
10175void intel_mark_idle(struct drm_device *dev)
652c393a 10176{
c67a470b 10177 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10178 struct drm_crtc *crtc;
652c393a 10179
f62a0076
CW
10180 if (!dev_priv->mm.busy)
10181 return;
10182
10183 dev_priv->mm.busy = false;
10184
70e1e0ec 10185 for_each_crtc(dev, crtc) {
f4510a27 10186 if (!crtc->primary->fb)
652c393a
JB
10187 continue;
10188
725a5b54 10189 intel_decrease_pllclock(crtc);
652c393a 10190 }
b29c19b6 10191
3d13ef2e 10192 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10193 gen6_rps_idle(dev->dev_private);
bb4cdd53 10194
43694d69 10195 intel_runtime_pm_put(dev_priv);
652c393a
JB
10196}
10197
f5de6e07
ACO
10198static void intel_crtc_set_state(struct intel_crtc *crtc,
10199 struct intel_crtc_state *crtc_state)
10200{
10201 kfree(crtc->config);
10202 crtc->config = crtc_state;
16f3f658 10203 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
10204}
10205
79e53945
JB
10206static void intel_crtc_destroy(struct drm_crtc *crtc)
10207{
10208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10209 struct drm_device *dev = crtc->dev;
10210 struct intel_unpin_work *work;
67e77c5a 10211
5e2d7afc 10212 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10213 work = intel_crtc->unpin_work;
10214 intel_crtc->unpin_work = NULL;
5e2d7afc 10215 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10216
10217 if (work) {
10218 cancel_work_sync(&work->work);
10219 kfree(work);
10220 }
79e53945 10221
f5de6e07 10222 intel_crtc_set_state(intel_crtc, NULL);
79e53945 10223 drm_crtc_cleanup(crtc);
67e77c5a 10224
79e53945
JB
10225 kfree(intel_crtc);
10226}
10227
6b95a207
KH
10228static void intel_unpin_work_fn(struct work_struct *__work)
10229{
10230 struct intel_unpin_work *work =
10231 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10232 struct drm_device *dev = work->crtc->dev;
f99d7069 10233 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10234
b4a98e57 10235 mutex_lock(&dev->struct_mutex);
82bc3b2d 10236 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10237 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10238
7ff0ebcc 10239 intel_fbc_update(dev);
f06cc1b9
JH
10240
10241 if (work->flip_queued_req)
146d84f0 10242 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10243 mutex_unlock(&dev->struct_mutex);
10244
f99d7069 10245 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10246 drm_framebuffer_unreference(work->old_fb);
f99d7069 10247
b4a98e57
CW
10248 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10249 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10250
6b95a207
KH
10251 kfree(work);
10252}
10253
1afe3e9d 10254static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10255 struct drm_crtc *crtc)
6b95a207 10256{
6b95a207
KH
10257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10258 struct intel_unpin_work *work;
6b95a207
KH
10259 unsigned long flags;
10260
10261 /* Ignore early vblank irqs */
10262 if (intel_crtc == NULL)
10263 return;
10264
f326038a
DV
10265 /*
10266 * This is called both by irq handlers and the reset code (to complete
10267 * lost pageflips) so needs the full irqsave spinlocks.
10268 */
6b95a207
KH
10269 spin_lock_irqsave(&dev->event_lock, flags);
10270 work = intel_crtc->unpin_work;
e7d841ca
CW
10271
10272 /* Ensure we don't miss a work->pending update ... */
10273 smp_rmb();
10274
10275 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10276 spin_unlock_irqrestore(&dev->event_lock, flags);
10277 return;
10278 }
10279
d6bbafa1 10280 page_flip_completed(intel_crtc);
0af7e4df 10281
6b95a207 10282 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10283}
10284
1afe3e9d
JB
10285void intel_finish_page_flip(struct drm_device *dev, int pipe)
10286{
fbee40df 10287 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10288 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10289
49b14a5c 10290 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10291}
10292
10293void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10294{
fbee40df 10295 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10296 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10297
49b14a5c 10298 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10299}
10300
75f7f3ec
VS
10301/* Is 'a' after or equal to 'b'? */
10302static bool g4x_flip_count_after_eq(u32 a, u32 b)
10303{
10304 return !((a - b) & 0x80000000);
10305}
10306
10307static bool page_flip_finished(struct intel_crtc *crtc)
10308{
10309 struct drm_device *dev = crtc->base.dev;
10310 struct drm_i915_private *dev_priv = dev->dev_private;
10311
bdfa7542
VS
10312 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10313 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10314 return true;
10315
75f7f3ec
VS
10316 /*
10317 * The relevant registers doen't exist on pre-ctg.
10318 * As the flip done interrupt doesn't trigger for mmio
10319 * flips on gmch platforms, a flip count check isn't
10320 * really needed there. But since ctg has the registers,
10321 * include it in the check anyway.
10322 */
10323 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10324 return true;
10325
10326 /*
10327 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10328 * used the same base address. In that case the mmio flip might
10329 * have completed, but the CS hasn't even executed the flip yet.
10330 *
10331 * A flip count check isn't enough as the CS might have updated
10332 * the base address just after start of vblank, but before we
10333 * managed to process the interrupt. This means we'd complete the
10334 * CS flip too soon.
10335 *
10336 * Combining both checks should get us a good enough result. It may
10337 * still happen that the CS flip has been executed, but has not
10338 * yet actually completed. But in case the base address is the same
10339 * anyway, we don't really care.
10340 */
10341 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10342 crtc->unpin_work->gtt_offset &&
10343 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10344 crtc->unpin_work->flip_count);
10345}
10346
6b95a207
KH
10347void intel_prepare_page_flip(struct drm_device *dev, int plane)
10348{
fbee40df 10349 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10350 struct intel_crtc *intel_crtc =
10351 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10352 unsigned long flags;
10353
f326038a
DV
10354
10355 /*
10356 * This is called both by irq handlers and the reset code (to complete
10357 * lost pageflips) so needs the full irqsave spinlocks.
10358 *
10359 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10360 * generate a page-flip completion irq, i.e. every modeset
10361 * is also accompanied by a spurious intel_prepare_page_flip().
10362 */
6b95a207 10363 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10364 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10365 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10366 spin_unlock_irqrestore(&dev->event_lock, flags);
10367}
10368
eba905b2 10369static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10370{
10371 /* Ensure that the work item is consistent when activating it ... */
10372 smp_wmb();
10373 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10374 /* and that it is marked active as soon as the irq could fire. */
10375 smp_wmb();
10376}
10377
8c9f3aaf
JB
10378static int intel_gen2_queue_flip(struct drm_device *dev,
10379 struct drm_crtc *crtc,
10380 struct drm_framebuffer *fb,
ed8d1975 10381 struct drm_i915_gem_object *obj,
a4872ba6 10382 struct intel_engine_cs *ring,
ed8d1975 10383 uint32_t flags)
8c9f3aaf 10384{
8c9f3aaf 10385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10386 u32 flip_mask;
10387 int ret;
10388
6d90c952 10389 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10390 if (ret)
4fa62c89 10391 return ret;
8c9f3aaf
JB
10392
10393 /* Can't queue multiple flips, so wait for the previous
10394 * one to finish before executing the next.
10395 */
10396 if (intel_crtc->plane)
10397 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10398 else
10399 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10400 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10401 intel_ring_emit(ring, MI_NOOP);
10402 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10403 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10404 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10405 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10406 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10407
10408 intel_mark_page_flip_active(intel_crtc);
09246732 10409 __intel_ring_advance(ring);
83d4092b 10410 return 0;
8c9f3aaf
JB
10411}
10412
10413static int intel_gen3_queue_flip(struct drm_device *dev,
10414 struct drm_crtc *crtc,
10415 struct drm_framebuffer *fb,
ed8d1975 10416 struct drm_i915_gem_object *obj,
a4872ba6 10417 struct intel_engine_cs *ring,
ed8d1975 10418 uint32_t flags)
8c9f3aaf 10419{
8c9f3aaf 10420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10421 u32 flip_mask;
10422 int ret;
10423
6d90c952 10424 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10425 if (ret)
4fa62c89 10426 return ret;
8c9f3aaf
JB
10427
10428 if (intel_crtc->plane)
10429 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10430 else
10431 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10432 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10433 intel_ring_emit(ring, MI_NOOP);
10434 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10435 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10436 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10437 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10438 intel_ring_emit(ring, MI_NOOP);
10439
e7d841ca 10440 intel_mark_page_flip_active(intel_crtc);
09246732 10441 __intel_ring_advance(ring);
83d4092b 10442 return 0;
8c9f3aaf
JB
10443}
10444
10445static int intel_gen4_queue_flip(struct drm_device *dev,
10446 struct drm_crtc *crtc,
10447 struct drm_framebuffer *fb,
ed8d1975 10448 struct drm_i915_gem_object *obj,
a4872ba6 10449 struct intel_engine_cs *ring,
ed8d1975 10450 uint32_t flags)
8c9f3aaf
JB
10451{
10452 struct drm_i915_private *dev_priv = dev->dev_private;
10453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10454 uint32_t pf, pipesrc;
10455 int ret;
10456
6d90c952 10457 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10458 if (ret)
4fa62c89 10459 return ret;
8c9f3aaf
JB
10460
10461 /* i965+ uses the linear or tiled offsets from the
10462 * Display Registers (which do not change across a page-flip)
10463 * so we need only reprogram the base address.
10464 */
6d90c952
DV
10465 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10466 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10467 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10468 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10469 obj->tiling_mode);
8c9f3aaf
JB
10470
10471 /* XXX Enabling the panel-fitter across page-flip is so far
10472 * untested on non-native modes, so ignore it for now.
10473 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10474 */
10475 pf = 0;
10476 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10477 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10478
10479 intel_mark_page_flip_active(intel_crtc);
09246732 10480 __intel_ring_advance(ring);
83d4092b 10481 return 0;
8c9f3aaf
JB
10482}
10483
10484static int intel_gen6_queue_flip(struct drm_device *dev,
10485 struct drm_crtc *crtc,
10486 struct drm_framebuffer *fb,
ed8d1975 10487 struct drm_i915_gem_object *obj,
a4872ba6 10488 struct intel_engine_cs *ring,
ed8d1975 10489 uint32_t flags)
8c9f3aaf
JB
10490{
10491 struct drm_i915_private *dev_priv = dev->dev_private;
10492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10493 uint32_t pf, pipesrc;
10494 int ret;
10495
6d90c952 10496 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10497 if (ret)
4fa62c89 10498 return ret;
8c9f3aaf 10499
6d90c952
DV
10500 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10501 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10502 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10503 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10504
dc257cf1
DV
10505 /* Contrary to the suggestions in the documentation,
10506 * "Enable Panel Fitter" does not seem to be required when page
10507 * flipping with a non-native mode, and worse causes a normal
10508 * modeset to fail.
10509 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10510 */
10511 pf = 0;
8c9f3aaf 10512 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10513 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10514
10515 intel_mark_page_flip_active(intel_crtc);
09246732 10516 __intel_ring_advance(ring);
83d4092b 10517 return 0;
8c9f3aaf
JB
10518}
10519
7c9017e5
JB
10520static int intel_gen7_queue_flip(struct drm_device *dev,
10521 struct drm_crtc *crtc,
10522 struct drm_framebuffer *fb,
ed8d1975 10523 struct drm_i915_gem_object *obj,
a4872ba6 10524 struct intel_engine_cs *ring,
ed8d1975 10525 uint32_t flags)
7c9017e5 10526{
7c9017e5 10527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10528 uint32_t plane_bit = 0;
ffe74d75
CW
10529 int len, ret;
10530
eba905b2 10531 switch (intel_crtc->plane) {
cb05d8de
DV
10532 case PLANE_A:
10533 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10534 break;
10535 case PLANE_B:
10536 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10537 break;
10538 case PLANE_C:
10539 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10540 break;
10541 default:
10542 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10543 return -ENODEV;
cb05d8de
DV
10544 }
10545
ffe74d75 10546 len = 4;
f476828a 10547 if (ring->id == RCS) {
ffe74d75 10548 len += 6;
f476828a
DL
10549 /*
10550 * On Gen 8, SRM is now taking an extra dword to accommodate
10551 * 48bits addresses, and we need a NOOP for the batch size to
10552 * stay even.
10553 */
10554 if (IS_GEN8(dev))
10555 len += 2;
10556 }
ffe74d75 10557
f66fab8e
VS
10558 /*
10559 * BSpec MI_DISPLAY_FLIP for IVB:
10560 * "The full packet must be contained within the same cache line."
10561 *
10562 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10563 * cacheline, if we ever start emitting more commands before
10564 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10565 * then do the cacheline alignment, and finally emit the
10566 * MI_DISPLAY_FLIP.
10567 */
10568 ret = intel_ring_cacheline_align(ring);
10569 if (ret)
4fa62c89 10570 return ret;
f66fab8e 10571
ffe74d75 10572 ret = intel_ring_begin(ring, len);
7c9017e5 10573 if (ret)
4fa62c89 10574 return ret;
7c9017e5 10575
ffe74d75
CW
10576 /* Unmask the flip-done completion message. Note that the bspec says that
10577 * we should do this for both the BCS and RCS, and that we must not unmask
10578 * more than one flip event at any time (or ensure that one flip message
10579 * can be sent by waiting for flip-done prior to queueing new flips).
10580 * Experimentation says that BCS works despite DERRMR masking all
10581 * flip-done completion events and that unmasking all planes at once
10582 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10583 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10584 */
10585 if (ring->id == RCS) {
10586 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10587 intel_ring_emit(ring, DERRMR);
10588 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10589 DERRMR_PIPEB_PRI_FLIP_DONE |
10590 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
10591 if (IS_GEN8(dev))
10592 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10593 MI_SRM_LRM_GLOBAL_GTT);
10594 else
10595 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10596 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
10597 intel_ring_emit(ring, DERRMR);
10598 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
10599 if (IS_GEN8(dev)) {
10600 intel_ring_emit(ring, 0);
10601 intel_ring_emit(ring, MI_NOOP);
10602 }
ffe74d75
CW
10603 }
10604
cb05d8de 10605 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 10606 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 10607 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 10608 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
10609
10610 intel_mark_page_flip_active(intel_crtc);
09246732 10611 __intel_ring_advance(ring);
83d4092b 10612 return 0;
7c9017e5
JB
10613}
10614
84c33a64
SG
10615static bool use_mmio_flip(struct intel_engine_cs *ring,
10616 struct drm_i915_gem_object *obj)
10617{
10618 /*
10619 * This is not being used for older platforms, because
10620 * non-availability of flip done interrupt forces us to use
10621 * CS flips. Older platforms derive flip done using some clever
10622 * tricks involving the flip_pending status bits and vblank irqs.
10623 * So using MMIO flips there would disrupt this mechanism.
10624 */
10625
8e09bf83
CW
10626 if (ring == NULL)
10627 return true;
10628
84c33a64
SG
10629 if (INTEL_INFO(ring->dev)->gen < 5)
10630 return false;
10631
10632 if (i915.use_mmio_flip < 0)
10633 return false;
10634 else if (i915.use_mmio_flip > 0)
10635 return true;
14bf993e
OM
10636 else if (i915.enable_execlists)
10637 return true;
84c33a64 10638 else
41c52415 10639 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
10640}
10641
ff944564
DL
10642static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10643{
10644 struct drm_device *dev = intel_crtc->base.dev;
10645 struct drm_i915_private *dev_priv = dev->dev_private;
10646 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
10647 const enum pipe pipe = intel_crtc->pipe;
10648 u32 ctl, stride;
10649
10650 ctl = I915_READ(PLANE_CTL(pipe, 0));
10651 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
10652 switch (fb->modifier[0]) {
10653 case DRM_FORMAT_MOD_NONE:
10654 break;
10655 case I915_FORMAT_MOD_X_TILED:
ff944564 10656 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
10657 break;
10658 case I915_FORMAT_MOD_Y_TILED:
10659 ctl |= PLANE_CTL_TILED_Y;
10660 break;
10661 case I915_FORMAT_MOD_Yf_TILED:
10662 ctl |= PLANE_CTL_TILED_YF;
10663 break;
10664 default:
10665 MISSING_CASE(fb->modifier[0]);
10666 }
ff944564
DL
10667
10668 /*
10669 * The stride is either expressed as a multiple of 64 bytes chunks for
10670 * linear buffers or in number of tiles for tiled buffers.
10671 */
2ebef630
TU
10672 stride = fb->pitches[0] /
10673 intel_fb_stride_alignment(dev, fb->modifier[0],
10674 fb->pixel_format);
ff944564
DL
10675
10676 /*
10677 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10678 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10679 */
10680 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10681 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10682
10683 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10684 POSTING_READ(PLANE_SURF(pipe, 0));
10685}
10686
10687static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
10688{
10689 struct drm_device *dev = intel_crtc->base.dev;
10690 struct drm_i915_private *dev_priv = dev->dev_private;
10691 struct intel_framebuffer *intel_fb =
10692 to_intel_framebuffer(intel_crtc->base.primary->fb);
10693 struct drm_i915_gem_object *obj = intel_fb->obj;
10694 u32 dspcntr;
10695 u32 reg;
10696
84c33a64
SG
10697 reg = DSPCNTR(intel_crtc->plane);
10698 dspcntr = I915_READ(reg);
10699
c5d97472
DL
10700 if (obj->tiling_mode != I915_TILING_NONE)
10701 dspcntr |= DISPPLANE_TILED;
10702 else
10703 dspcntr &= ~DISPPLANE_TILED;
10704
84c33a64
SG
10705 I915_WRITE(reg, dspcntr);
10706
10707 I915_WRITE(DSPSURF(intel_crtc->plane),
10708 intel_crtc->unpin_work->gtt_offset);
10709 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 10710
ff944564
DL
10711}
10712
10713/*
10714 * XXX: This is the temporary way to update the plane registers until we get
10715 * around to using the usual plane update functions for MMIO flips
10716 */
10717static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10718{
10719 struct drm_device *dev = intel_crtc->base.dev;
10720 bool atomic_update;
10721 u32 start_vbl_count;
10722
10723 intel_mark_page_flip_active(intel_crtc);
10724
10725 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10726
10727 if (INTEL_INFO(dev)->gen >= 9)
10728 skl_do_mmio_flip(intel_crtc);
10729 else
10730 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10731 ilk_do_mmio_flip(intel_crtc);
10732
9362c7c5
ACO
10733 if (atomic_update)
10734 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
10735}
10736
9362c7c5 10737static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 10738{
cc8c4cc2 10739 struct intel_crtc *crtc =
9362c7c5 10740 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 10741 struct intel_mmio_flip *mmio_flip;
84c33a64 10742
cc8c4cc2
JH
10743 mmio_flip = &crtc->mmio_flip;
10744 if (mmio_flip->req)
9c654818
JH
10745 WARN_ON(__i915_wait_request(mmio_flip->req,
10746 crtc->reset_counter,
10747 false, NULL, NULL) != 0);
84c33a64 10748
cc8c4cc2
JH
10749 intel_do_mmio_flip(crtc);
10750 if (mmio_flip->req) {
10751 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 10752 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
10753 mutex_unlock(&crtc->base.dev->struct_mutex);
10754 }
84c33a64
SG
10755}
10756
10757static int intel_queue_mmio_flip(struct drm_device *dev,
10758 struct drm_crtc *crtc,
10759 struct drm_framebuffer *fb,
10760 struct drm_i915_gem_object *obj,
10761 struct intel_engine_cs *ring,
10762 uint32_t flags)
10763{
84c33a64 10764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 10765
cc8c4cc2
JH
10766 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10767 obj->last_write_req);
536f5b5e
ACO
10768
10769 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 10770
84c33a64
SG
10771 return 0;
10772}
10773
8c9f3aaf
JB
10774static int intel_default_queue_flip(struct drm_device *dev,
10775 struct drm_crtc *crtc,
10776 struct drm_framebuffer *fb,
ed8d1975 10777 struct drm_i915_gem_object *obj,
a4872ba6 10778 struct intel_engine_cs *ring,
ed8d1975 10779 uint32_t flags)
8c9f3aaf
JB
10780{
10781 return -ENODEV;
10782}
10783
d6bbafa1
CW
10784static bool __intel_pageflip_stall_check(struct drm_device *dev,
10785 struct drm_crtc *crtc)
10786{
10787 struct drm_i915_private *dev_priv = dev->dev_private;
10788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10789 struct intel_unpin_work *work = intel_crtc->unpin_work;
10790 u32 addr;
10791
10792 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10793 return true;
10794
10795 if (!work->enable_stall_check)
10796 return false;
10797
10798 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
10799 if (work->flip_queued_req &&
10800 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
10801 return false;
10802
1e3feefd 10803 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
10804 }
10805
1e3feefd 10806 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
10807 return false;
10808
10809 /* Potential stall - if we see that the flip has happened,
10810 * assume a missed interrupt. */
10811 if (INTEL_INFO(dev)->gen >= 4)
10812 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10813 else
10814 addr = I915_READ(DSPADDR(intel_crtc->plane));
10815
10816 /* There is a potential issue here with a false positive after a flip
10817 * to the same address. We could address this by checking for a
10818 * non-incrementing frame counter.
10819 */
10820 return addr == work->gtt_offset;
10821}
10822
10823void intel_check_page_flip(struct drm_device *dev, int pipe)
10824{
10825 struct drm_i915_private *dev_priv = dev->dev_private;
10826 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 10828 struct intel_unpin_work *work;
f326038a 10829
6c51d46f 10830 WARN_ON(!in_interrupt());
d6bbafa1
CW
10831
10832 if (crtc == NULL)
10833 return;
10834
f326038a 10835 spin_lock(&dev->event_lock);
6ad790c0
CW
10836 work = intel_crtc->unpin_work;
10837 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 10838 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 10839 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 10840 page_flip_completed(intel_crtc);
6ad790c0 10841 work = NULL;
d6bbafa1 10842 }
6ad790c0
CW
10843 if (work != NULL &&
10844 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10845 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 10846 spin_unlock(&dev->event_lock);
d6bbafa1
CW
10847}
10848
6b95a207
KH
10849static int intel_crtc_page_flip(struct drm_crtc *crtc,
10850 struct drm_framebuffer *fb,
ed8d1975
KP
10851 struct drm_pending_vblank_event *event,
10852 uint32_t page_flip_flags)
6b95a207
KH
10853{
10854 struct drm_device *dev = crtc->dev;
10855 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 10856 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 10857 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 10858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 10859 struct drm_plane *primary = crtc->primary;
a071fa00 10860 enum pipe pipe = intel_crtc->pipe;
6b95a207 10861 struct intel_unpin_work *work;
a4872ba6 10862 struct intel_engine_cs *ring;
cf5d8a46 10863 bool mmio_flip;
52e68630 10864 int ret;
6b95a207 10865
2ff8fde1
MR
10866 /*
10867 * drm_mode_page_flip_ioctl() should already catch this, but double
10868 * check to be safe. In the future we may enable pageflipping from
10869 * a disabled primary plane.
10870 */
10871 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10872 return -EBUSY;
10873
e6a595d2 10874 /* Can't change pixel format via MI display flips. */
f4510a27 10875 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
10876 return -EINVAL;
10877
10878 /*
10879 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10880 * Note that pitch changes could also affect these register.
10881 */
10882 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
10883 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10884 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
10885 return -EINVAL;
10886
f900db47
CW
10887 if (i915_terminally_wedged(&dev_priv->gpu_error))
10888 goto out_hang;
10889
b14c5679 10890 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10891 if (work == NULL)
10892 return -ENOMEM;
10893
6b95a207 10894 work->event = event;
b4a98e57 10895 work->crtc = crtc;
ab8d6675 10896 work->old_fb = old_fb;
6b95a207
KH
10897 INIT_WORK(&work->work, intel_unpin_work_fn);
10898
87b6b101 10899 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10900 if (ret)
10901 goto free_work;
10902
6b95a207 10903 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 10904 spin_lock_irq(&dev->event_lock);
6b95a207 10905 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10906 /* Before declaring the flip queue wedged, check if
10907 * the hardware completed the operation behind our backs.
10908 */
10909 if (__intel_pageflip_stall_check(dev, crtc)) {
10910 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10911 page_flip_completed(intel_crtc);
10912 } else {
10913 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10914 spin_unlock_irq(&dev->event_lock);
468f0b44 10915
d6bbafa1
CW
10916 drm_crtc_vblank_put(crtc);
10917 kfree(work);
10918 return -EBUSY;
10919 }
6b95a207
KH
10920 }
10921 intel_crtc->unpin_work = work;
5e2d7afc 10922 spin_unlock_irq(&dev->event_lock);
6b95a207 10923
b4a98e57
CW
10924 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10925 flush_workqueue(dev_priv->wq);
10926
75dfca80 10927 /* Reference the objects for the scheduled work. */
ab8d6675 10928 drm_framebuffer_reference(work->old_fb);
05394f39 10929 drm_gem_object_reference(&obj->base);
6b95a207 10930
f4510a27 10931 crtc->primary->fb = fb;
afd65eb4 10932 update_state_fb(crtc->primary);
1ed1f968 10933
e1f99ce6 10934 work->pending_flip_obj = obj;
e1f99ce6 10935
89ed88ba
CW
10936 ret = i915_mutex_lock_interruptible(dev);
10937 if (ret)
10938 goto cleanup;
10939
b4a98e57 10940 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10941 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10942
75f7f3ec 10943 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10944 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10945
4fa62c89
VS
10946 if (IS_VALLEYVIEW(dev)) {
10947 ring = &dev_priv->ring[BCS];
ab8d6675 10948 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
10949 /* vlv: DISPLAY_FLIP fails to change tiling */
10950 ring = NULL;
48bf5b2d 10951 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 10952 ring = &dev_priv->ring[BCS];
4fa62c89 10953 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 10954 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
10955 if (ring == NULL || ring->id != RCS)
10956 ring = &dev_priv->ring[BCS];
10957 } else {
10958 ring = &dev_priv->ring[RCS];
10959 }
10960
cf5d8a46
CW
10961 mmio_flip = use_mmio_flip(ring, obj);
10962
10963 /* When using CS flips, we want to emit semaphores between rings.
10964 * However, when using mmio flips we will create a task to do the
10965 * synchronisation, so all we want here is to pin the framebuffer
10966 * into the display plane and skip any waits.
10967 */
82bc3b2d 10968 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46
CW
10969 crtc->primary->state,
10970 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
8c9f3aaf
JB
10971 if (ret)
10972 goto cleanup_pending;
6b95a207 10973
121920fa
TU
10974 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10975 + intel_crtc->dspaddr_offset;
4fa62c89 10976
cf5d8a46 10977 if (mmio_flip) {
84c33a64
SG
10978 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10979 page_flip_flags);
d6bbafa1
CW
10980 if (ret)
10981 goto cleanup_unpin;
10982
f06cc1b9
JH
10983 i915_gem_request_assign(&work->flip_queued_req,
10984 obj->last_write_req);
d6bbafa1 10985 } else {
84c33a64 10986 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10987 page_flip_flags);
10988 if (ret)
10989 goto cleanup_unpin;
10990
f06cc1b9
JH
10991 i915_gem_request_assign(&work->flip_queued_req,
10992 intel_ring_get_request(ring));
d6bbafa1
CW
10993 }
10994
1e3feefd 10995 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 10996 work->enable_stall_check = true;
4fa62c89 10997
ab8d6675 10998 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
10999 INTEL_FRONTBUFFER_PRIMARY(pipe));
11000
7ff0ebcc 11001 intel_fbc_disable(dev);
f99d7069 11002 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11003 mutex_unlock(&dev->struct_mutex);
11004
e5510fac
JB
11005 trace_i915_flip_request(intel_crtc->plane, obj);
11006
6b95a207 11007 return 0;
96b099fd 11008
4fa62c89 11009cleanup_unpin:
82bc3b2d 11010 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11011cleanup_pending:
b4a98e57 11012 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11013 mutex_unlock(&dev->struct_mutex);
11014cleanup:
f4510a27 11015 crtc->primary->fb = old_fb;
afd65eb4 11016 update_state_fb(crtc->primary);
89ed88ba
CW
11017
11018 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11019 drm_framebuffer_unreference(work->old_fb);
96b099fd 11020
5e2d7afc 11021 spin_lock_irq(&dev->event_lock);
96b099fd 11022 intel_crtc->unpin_work = NULL;
5e2d7afc 11023 spin_unlock_irq(&dev->event_lock);
96b099fd 11024
87b6b101 11025 drm_crtc_vblank_put(crtc);
7317c75e 11026free_work:
96b099fd
CW
11027 kfree(work);
11028
f900db47
CW
11029 if (ret == -EIO) {
11030out_hang:
53a366b9 11031 ret = intel_plane_restore(primary);
f0d3dad3 11032 if (ret == 0 && event) {
5e2d7afc 11033 spin_lock_irq(&dev->event_lock);
a071fa00 11034 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11035 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11036 }
f900db47 11037 }
96b099fd 11038 return ret;
6b95a207
KH
11039}
11040
65b38e0d 11041static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11042 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11043 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11044 .atomic_begin = intel_begin_crtc_commit,
11045 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11046};
11047
9a935856
DV
11048/**
11049 * intel_modeset_update_staged_output_state
11050 *
11051 * Updates the staged output configuration state, e.g. after we've read out the
11052 * current hw state.
11053 */
11054static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11055{
7668851f 11056 struct intel_crtc *crtc;
9a935856
DV
11057 struct intel_encoder *encoder;
11058 struct intel_connector *connector;
f6e5b160 11059
3a3371ff 11060 for_each_intel_connector(dev, connector) {
9a935856
DV
11061 connector->new_encoder =
11062 to_intel_encoder(connector->base.encoder);
11063 }
f6e5b160 11064
b2784e15 11065 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11066 encoder->new_crtc =
11067 to_intel_crtc(encoder->base.crtc);
11068 }
7668851f 11069
d3fcc808 11070 for_each_intel_crtc(dev, crtc) {
83d65738 11071 crtc->new_enabled = crtc->base.state->enable;
7668851f 11072 }
f6e5b160
CW
11073}
11074
d29b2f9d
ACO
11075/* Transitional helper to copy current connector/encoder state to
11076 * connector->state. This is needed so that code that is partially
11077 * converted to atomic does the right thing.
11078 */
11079static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11080{
11081 struct intel_connector *connector;
11082
11083 for_each_intel_connector(dev, connector) {
11084 if (connector->base.encoder) {
11085 connector->base.state->best_encoder =
11086 connector->base.encoder;
11087 connector->base.state->crtc =
11088 connector->base.encoder->crtc;
11089 } else {
11090 connector->base.state->best_encoder = NULL;
11091 connector->base.state->crtc = NULL;
11092 }
11093 }
11094}
11095
9a935856
DV
11096/**
11097 * intel_modeset_commit_output_state
11098 *
11099 * This function copies the stage display pipe configuration to the real one.
11100 */
11101static void intel_modeset_commit_output_state(struct drm_device *dev)
11102{
7668851f 11103 struct intel_crtc *crtc;
9a935856
DV
11104 struct intel_encoder *encoder;
11105 struct intel_connector *connector;
f6e5b160 11106
3a3371ff 11107 for_each_intel_connector(dev, connector) {
9a935856
DV
11108 connector->base.encoder = &connector->new_encoder->base;
11109 }
f6e5b160 11110
b2784e15 11111 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11112 encoder->base.crtc = &encoder->new_crtc->base;
11113 }
7668851f 11114
d3fcc808 11115 for_each_intel_crtc(dev, crtc) {
83d65738 11116 crtc->base.state->enable = crtc->new_enabled;
7668851f
VS
11117 crtc->base.enabled = crtc->new_enabled;
11118 }
d29b2f9d
ACO
11119
11120 intel_modeset_update_connector_atomic_state(dev);
9a935856
DV
11121}
11122
050f7aeb 11123static void
eba905b2 11124connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11125 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11126{
11127 int bpp = pipe_config->pipe_bpp;
11128
11129 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11130 connector->base.base.id,
c23cc417 11131 connector->base.name);
050f7aeb
DV
11132
11133 /* Don't use an invalid EDID bpc value */
11134 if (connector->base.display_info.bpc &&
11135 connector->base.display_info.bpc * 3 < bpp) {
11136 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11137 bpp, connector->base.display_info.bpc*3);
11138 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11139 }
11140
11141 /* Clamp bpp to 8 on screens without EDID 1.4 */
11142 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11143 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11144 bpp);
11145 pipe_config->pipe_bpp = 24;
11146 }
11147}
11148
4e53c2e0 11149static int
050f7aeb 11150compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11151 struct intel_crtc_state *pipe_config)
4e53c2e0 11152{
050f7aeb 11153 struct drm_device *dev = crtc->base.dev;
1486017f 11154 struct drm_atomic_state *state;
050f7aeb 11155 struct intel_connector *connector;
1486017f 11156 int bpp, i;
4e53c2e0 11157
d328c9d7 11158 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11159 bpp = 10*3;
d328c9d7
DV
11160 else if (INTEL_INFO(dev)->gen >= 5)
11161 bpp = 12*3;
11162 else
11163 bpp = 8*3;
11164
4e53c2e0 11165
4e53c2e0
DV
11166 pipe_config->pipe_bpp = bpp;
11167
1486017f
ACO
11168 state = pipe_config->base.state;
11169
4e53c2e0 11170 /* Clamp display bpp to EDID value */
1486017f
ACO
11171 for (i = 0; i < state->num_connector; i++) {
11172 if (!state->connectors[i])
11173 continue;
11174
11175 connector = to_intel_connector(state->connectors[i]);
11176 if (state->connector_states[i]->crtc != &crtc->base)
4e53c2e0
DV
11177 continue;
11178
050f7aeb 11179 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
11180 }
11181
11182 return bpp;
11183}
11184
644db711
DV
11185static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11186{
11187 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11188 "type: 0x%x flags: 0x%x\n",
1342830c 11189 mode->crtc_clock,
644db711
DV
11190 mode->crtc_hdisplay, mode->crtc_hsync_start,
11191 mode->crtc_hsync_end, mode->crtc_htotal,
11192 mode->crtc_vdisplay, mode->crtc_vsync_start,
11193 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11194}
11195
c0b03411 11196static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11197 struct intel_crtc_state *pipe_config,
c0b03411
DV
11198 const char *context)
11199{
6a60cd87
CK
11200 struct drm_device *dev = crtc->base.dev;
11201 struct drm_plane *plane;
11202 struct intel_plane *intel_plane;
11203 struct intel_plane_state *state;
11204 struct drm_framebuffer *fb;
11205
11206 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11207 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11208
11209 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11210 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11211 pipe_config->pipe_bpp, pipe_config->dither);
11212 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11213 pipe_config->has_pch_encoder,
11214 pipe_config->fdi_lanes,
11215 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11216 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11217 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11218 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11219 pipe_config->has_dp_encoder,
11220 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11221 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11222 pipe_config->dp_m_n.tu);
b95af8be
VK
11223
11224 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11225 pipe_config->has_dp_encoder,
11226 pipe_config->dp_m2_n2.gmch_m,
11227 pipe_config->dp_m2_n2.gmch_n,
11228 pipe_config->dp_m2_n2.link_m,
11229 pipe_config->dp_m2_n2.link_n,
11230 pipe_config->dp_m2_n2.tu);
11231
55072d19
DV
11232 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11233 pipe_config->has_audio,
11234 pipe_config->has_infoframe);
11235
c0b03411 11236 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11237 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11238 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11239 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11240 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11241 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11242 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
6a60cd87
CK
11244 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11245 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11246 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
c0b03411
DV
11247 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11248 pipe_config->gmch_pfit.control,
11249 pipe_config->gmch_pfit.pgm_ratios,
11250 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11251 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11252 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11253 pipe_config->pch_pfit.size,
11254 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11255 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11256 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87
CK
11257
11258 DRM_DEBUG_KMS("planes on this crtc\n");
11259 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11260 intel_plane = to_intel_plane(plane);
11261 if (intel_plane->pipe != crtc->pipe)
11262 continue;
11263
11264 state = to_intel_plane_state(plane->state);
11265 fb = state->base.fb;
11266 if (!fb) {
11267 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11268 "disabled, scaler_id = %d\n",
11269 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11270 plane->base.id, intel_plane->pipe,
11271 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11272 drm_plane_index(plane), state->scaler_id);
11273 continue;
11274 }
11275
11276 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11277 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11278 plane->base.id, intel_plane->pipe,
11279 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11280 drm_plane_index(plane));
11281 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11282 fb->base.id, fb->width, fb->height, fb->pixel_format);
11283 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11284 state->scaler_id,
11285 state->src.x1 >> 16, state->src.y1 >> 16,
11286 drm_rect_width(&state->src) >> 16,
11287 drm_rect_height(&state->src) >> 16,
11288 state->dst.x1, state->dst.y1,
11289 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11290 }
c0b03411
DV
11291}
11292
bc079e8b
VS
11293static bool encoders_cloneable(const struct intel_encoder *a,
11294 const struct intel_encoder *b)
accfc0c5 11295{
bc079e8b
VS
11296 /* masks could be asymmetric, so check both ways */
11297 return a == b || (a->cloneable & (1 << b->type) &&
11298 b->cloneable & (1 << a->type));
11299}
11300
98a221da
ACO
11301static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11302 struct intel_crtc *crtc,
bc079e8b
VS
11303 struct intel_encoder *encoder)
11304{
bc079e8b 11305 struct intel_encoder *source_encoder;
98a221da
ACO
11306 struct drm_connector_state *connector_state;
11307 int i;
bc079e8b 11308
98a221da
ACO
11309 for (i = 0; i < state->num_connector; i++) {
11310 if (!state->connectors[i])
11311 continue;
bc079e8b 11312
98a221da
ACO
11313 connector_state = state->connector_states[i];
11314 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11315 continue;
11316
98a221da
ACO
11317 source_encoder =
11318 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11319 if (!encoders_cloneable(encoder, source_encoder))
11320 return false;
11321 }
11322
11323 return true;
11324}
11325
98a221da
ACO
11326static bool check_encoder_cloning(struct drm_atomic_state *state,
11327 struct intel_crtc *crtc)
bc079e8b 11328{
accfc0c5 11329 struct intel_encoder *encoder;
98a221da
ACO
11330 struct drm_connector_state *connector_state;
11331 int i;
accfc0c5 11332
98a221da
ACO
11333 for (i = 0; i < state->num_connector; i++) {
11334 if (!state->connectors[i])
accfc0c5
DV
11335 continue;
11336
98a221da
ACO
11337 connector_state = state->connector_states[i];
11338 if (connector_state->crtc != &crtc->base)
11339 continue;
11340
11341 encoder = to_intel_encoder(connector_state->best_encoder);
11342 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11343 return false;
accfc0c5
DV
11344 }
11345
bc079e8b 11346 return true;
accfc0c5
DV
11347}
11348
5448a00d 11349static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11350{
5448a00d
ACO
11351 struct drm_device *dev = state->dev;
11352 struct intel_encoder *encoder;
11353 struct drm_connector_state *connector_state;
00f0b378 11354 unsigned int used_ports = 0;
5448a00d 11355 int i;
00f0b378
VS
11356
11357 /*
11358 * Walk the connector list instead of the encoder
11359 * list to detect the problem on ddi platforms
11360 * where there's just one encoder per digital port.
11361 */
5448a00d
ACO
11362 for (i = 0; i < state->num_connector; i++) {
11363 if (!state->connectors[i])
11364 continue;
00f0b378 11365
5448a00d
ACO
11366 connector_state = state->connector_states[i];
11367 if (!connector_state->best_encoder)
00f0b378
VS
11368 continue;
11369
5448a00d
ACO
11370 encoder = to_intel_encoder(connector_state->best_encoder);
11371
11372 WARN_ON(!connector_state->crtc);
00f0b378
VS
11373
11374 switch (encoder->type) {
11375 unsigned int port_mask;
11376 case INTEL_OUTPUT_UNKNOWN:
11377 if (WARN_ON(!HAS_DDI(dev)))
11378 break;
11379 case INTEL_OUTPUT_DISPLAYPORT:
11380 case INTEL_OUTPUT_HDMI:
11381 case INTEL_OUTPUT_EDP:
11382 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11383
11384 /* the same port mustn't appear more than once */
11385 if (used_ports & port_mask)
11386 return false;
11387
11388 used_ports |= port_mask;
11389 default:
11390 break;
11391 }
11392 }
11393
11394 return true;
11395}
11396
83a57153
ACO
11397static void
11398clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11399{
11400 struct drm_crtc_state tmp_state;
663a3640 11401 struct intel_crtc_scaler_state scaler_state;
83a57153 11402
663a3640 11403 /* Clear only the intel specific part of the crtc state excluding scalers */
83a57153 11404 tmp_state = crtc_state->base;
663a3640 11405 scaler_state = crtc_state->scaler_state;
83a57153
ACO
11406 memset(crtc_state, 0, sizeof *crtc_state);
11407 crtc_state->base = tmp_state;
663a3640 11408 crtc_state->scaler_state = scaler_state;
83a57153
ACO
11409}
11410
5cec258b 11411static struct intel_crtc_state *
b8cecdf5 11412intel_modeset_pipe_config(struct drm_crtc *crtc,
83a57153
ACO
11413 struct drm_display_mode *mode,
11414 struct drm_atomic_state *state)
ee7b9f93 11415{
7758a113 11416 struct intel_encoder *encoder;
0b901879
ACO
11417 struct intel_connector *connector;
11418 struct drm_connector_state *connector_state;
5cec258b 11419 struct intel_crtc_state *pipe_config;
d328c9d7 11420 int base_bpp, ret = -EINVAL;
0b901879 11421 int i;
e29c22c0 11422 bool retry = true;
ee7b9f93 11423
98a221da 11424 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5
DV
11425 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11426 return ERR_PTR(-EINVAL);
11427 }
11428
5448a00d 11429 if (!check_digital_port_conflicts(state)) {
00f0b378
VS
11430 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11431 return ERR_PTR(-EINVAL);
11432 }
11433
83a57153
ACO
11434 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11435 if (IS_ERR(pipe_config))
11436 return pipe_config;
11437
11438 clear_intel_crtc_state(pipe_config);
7758a113 11439
07878248 11440 pipe_config->base.crtc = crtc;
2d112de7
ACO
11441 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11442 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 11443
e143a21c
DV
11444 pipe_config->cpu_transcoder =
11445 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 11446 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 11447
2960bc9c
ID
11448 /*
11449 * Sanitize sync polarity flags based on requested ones. If neither
11450 * positive or negative polarity is requested, treat this as meaning
11451 * negative polarity.
11452 */
2d112de7 11453 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11454 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11456
2d112de7 11457 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11458 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11459 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11460
050f7aeb
DV
11461 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11462 * plane pixel format and any sink constraints into account. Returns the
11463 * source plane bpp so that dithering can be selected on mismatches
11464 * after encoders and crtc also have had their say. */
d328c9d7
DV
11465 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11466 pipe_config);
11467 if (base_bpp < 0)
4e53c2e0
DV
11468 goto fail;
11469
e41a56be
VS
11470 /*
11471 * Determine the real pipe dimensions. Note that stereo modes can
11472 * increase the actual pipe size due to the frame doubling and
11473 * insertion of additional space for blanks between the frame. This
11474 * is stored in the crtc timings. We use the requested mode to do this
11475 * computation to clearly distinguish it from the adjusted mode, which
11476 * can be changed by the connectors in the below retry loop.
11477 */
2d112de7 11478 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11479 &pipe_config->pipe_src_w,
11480 &pipe_config->pipe_src_h);
e41a56be 11481
e29c22c0 11482encoder_retry:
ef1b460d 11483 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11484 pipe_config->port_clock = 0;
ef1b460d 11485 pipe_config->pixel_multiplier = 1;
ff9a6750 11486
135c81b8 11487 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11488 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11489 CRTC_STEREO_DOUBLE);
135c81b8 11490
7758a113
DV
11491 /* Pass our mode to the connectors and the CRTC to give them a chance to
11492 * adjust it according to limitations or connector properties, and also
11493 * a chance to reject the mode entirely.
47f1c6c9 11494 */
0b901879
ACO
11495 for (i = 0; i < state->num_connector; i++) {
11496 connector = to_intel_connector(state->connectors[i]);
11497 if (!connector)
11498 continue;
47f1c6c9 11499
0b901879
ACO
11500 connector_state = state->connector_states[i];
11501 if (connector_state->crtc != crtc)
7758a113 11502 continue;
7ae89233 11503
0b901879
ACO
11504 encoder = to_intel_encoder(connector_state->best_encoder);
11505
efea6e8e
DV
11506 if (!(encoder->compute_config(encoder, pipe_config))) {
11507 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11508 goto fail;
11509 }
ee7b9f93 11510 }
47f1c6c9 11511
ff9a6750
DV
11512 /* Set default port clock if not overwritten by the encoder. Needs to be
11513 * done afterwards in case the encoder adjusts the mode. */
11514 if (!pipe_config->port_clock)
2d112de7 11515 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11516 * pipe_config->pixel_multiplier;
ff9a6750 11517
a43f6e0f 11518 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11519 if (ret < 0) {
7758a113
DV
11520 DRM_DEBUG_KMS("CRTC fixup failed\n");
11521 goto fail;
ee7b9f93 11522 }
e29c22c0
DV
11523
11524 if (ret == RETRY) {
11525 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11526 ret = -EINVAL;
11527 goto fail;
11528 }
11529
11530 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11531 retry = false;
11532 goto encoder_retry;
11533 }
11534
d328c9d7 11535 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 11536 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11537 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11538
b8cecdf5 11539 return pipe_config;
7758a113 11540fail:
e29c22c0 11541 return ERR_PTR(ret);
ee7b9f93 11542}
47f1c6c9 11543
e2e1ed41
DV
11544/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11545 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11546static void
11547intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11548 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
11549{
11550 struct intel_crtc *intel_crtc;
e2e1ed41
DV
11551 struct drm_device *dev = crtc->dev;
11552 struct intel_encoder *encoder;
11553 struct intel_connector *connector;
11554 struct drm_crtc *tmp_crtc;
79e53945 11555
e2e1ed41 11556 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 11557
e2e1ed41
DV
11558 /* Check which crtcs have changed outputs connected to them, these need
11559 * to be part of the prepare_pipes mask. We don't (yet) support global
11560 * modeset across multiple crtcs, so modeset_pipes will only have one
11561 * bit set at most. */
3a3371ff 11562 for_each_intel_connector(dev, connector) {
e2e1ed41
DV
11563 if (connector->base.encoder == &connector->new_encoder->base)
11564 continue;
79e53945 11565
e2e1ed41
DV
11566 if (connector->base.encoder) {
11567 tmp_crtc = connector->base.encoder->crtc;
11568
11569 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11570 }
11571
11572 if (connector->new_encoder)
11573 *prepare_pipes |=
11574 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
11575 }
11576
b2784e15 11577 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
11578 if (encoder->base.crtc == &encoder->new_crtc->base)
11579 continue;
11580
11581 if (encoder->base.crtc) {
11582 tmp_crtc = encoder->base.crtc;
11583
11584 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11585 }
11586
11587 if (encoder->new_crtc)
11588 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
11589 }
11590
7668851f 11591 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 11592 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11593 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
e2e1ed41 11594 continue;
7e7d76c3 11595
7668851f 11596 if (!intel_crtc->new_enabled)
e2e1ed41 11597 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
11598 else
11599 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
11600 }
11601
e2e1ed41
DV
11602
11603 /* set_mode is also used to update properties on life display pipes. */
11604 intel_crtc = to_intel_crtc(crtc);
7668851f 11605 if (intel_crtc->new_enabled)
e2e1ed41
DV
11606 *prepare_pipes |= 1 << intel_crtc->pipe;
11607
b6c5164d
DV
11608 /*
11609 * For simplicity do a full modeset on any pipe where the output routing
11610 * changed. We could be more clever, but that would require us to be
11611 * more careful with calling the relevant encoder->mode_set functions.
11612 */
e2e1ed41
DV
11613 if (*prepare_pipes)
11614 *modeset_pipes = *prepare_pipes;
11615
11616 /* ... and mask these out. */
11617 *modeset_pipes &= ~(*disable_pipes);
11618 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
11619
11620 /*
11621 * HACK: We don't (yet) fully support global modesets. intel_set_config
11622 * obies this rule, but the modeset restore mode of
11623 * intel_modeset_setup_hw_state does not.
11624 */
11625 *modeset_pipes &= 1 << intel_crtc->pipe;
11626 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
11627
11628 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11629 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 11630}
79e53945 11631
ea9d758d 11632static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 11633{
ea9d758d 11634 struct drm_encoder *encoder;
f6e5b160 11635 struct drm_device *dev = crtc->dev;
f6e5b160 11636
ea9d758d
DV
11637 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11638 if (encoder->crtc == crtc)
11639 return true;
11640
11641 return false;
11642}
11643
11644static void
11645intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11646{
ba41c0de 11647 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
11648 struct intel_encoder *intel_encoder;
11649 struct intel_crtc *intel_crtc;
11650 struct drm_connector *connector;
11651
ba41c0de
DV
11652 intel_shared_dpll_commit(dev_priv);
11653
b2784e15 11654 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
11655 if (!intel_encoder->base.crtc)
11656 continue;
11657
11658 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11659
11660 if (prepare_pipes & (1 << intel_crtc->pipe))
11661 intel_encoder->connectors_active = false;
11662 }
11663
11664 intel_modeset_commit_output_state(dev);
11665
7668851f 11666 /* Double check state. */
d3fcc808 11667 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11668 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
ea9d758d
DV
11669 }
11670
11671 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11672 if (!connector->encoder || !connector->encoder->crtc)
11673 continue;
11674
11675 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11676
11677 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
11678 struct drm_property *dpms_property =
11679 dev->mode_config.dpms_property;
11680
ea9d758d 11681 connector->dpms = DRM_MODE_DPMS_ON;
662595df 11682 drm_object_property_set_value(&connector->base,
68d34720
DV
11683 dpms_property,
11684 DRM_MODE_DPMS_ON);
ea9d758d
DV
11685
11686 intel_encoder = to_intel_encoder(connector->encoder);
11687 intel_encoder->connectors_active = true;
11688 }
11689 }
11690
11691}
11692
3bd26263 11693static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11694{
3bd26263 11695 int diff;
f1f644dc
JB
11696
11697 if (clock1 == clock2)
11698 return true;
11699
11700 if (!clock1 || !clock2)
11701 return false;
11702
11703 diff = abs(clock1 - clock2);
11704
11705 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11706 return true;
11707
11708 return false;
11709}
11710
25c5b266
DV
11711#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11712 list_for_each_entry((intel_crtc), \
11713 &(dev)->mode_config.crtc_list, \
11714 base.head) \
0973f18f 11715 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11716
0e8ffe1b 11717static bool
2fa2fe9a 11718intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
11719 struct intel_crtc_state *current_config,
11720 struct intel_crtc_state *pipe_config)
0e8ffe1b 11721{
66e985c0
DV
11722#define PIPE_CONF_CHECK_X(name) \
11723 if (current_config->name != pipe_config->name) { \
11724 DRM_ERROR("mismatch in " #name " " \
11725 "(expected 0x%08x, found 0x%08x)\n", \
11726 current_config->name, \
11727 pipe_config->name); \
11728 return false; \
11729 }
11730
08a24034
DV
11731#define PIPE_CONF_CHECK_I(name) \
11732 if (current_config->name != pipe_config->name) { \
11733 DRM_ERROR("mismatch in " #name " " \
11734 "(expected %i, found %i)\n", \
11735 current_config->name, \
11736 pipe_config->name); \
11737 return false; \
88adfff1
DV
11738 }
11739
b95af8be
VK
11740/* This is required for BDW+ where there is only one set of registers for
11741 * switching between high and low RR.
11742 * This macro can be used whenever a comparison has to be made between one
11743 * hw state and multiple sw state variables.
11744 */
11745#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11746 if ((current_config->name != pipe_config->name) && \
11747 (current_config->alt_name != pipe_config->name)) { \
11748 DRM_ERROR("mismatch in " #name " " \
11749 "(expected %i or %i, found %i)\n", \
11750 current_config->name, \
11751 current_config->alt_name, \
11752 pipe_config->name); \
11753 return false; \
11754 }
11755
1bd1bd80
DV
11756#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11757 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 11758 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
11759 "(expected %i, found %i)\n", \
11760 current_config->name & (mask), \
11761 pipe_config->name & (mask)); \
11762 return false; \
11763 }
11764
5e550656
VS
11765#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11766 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11767 DRM_ERROR("mismatch in " #name " " \
11768 "(expected %i, found %i)\n", \
11769 current_config->name, \
11770 pipe_config->name); \
11771 return false; \
11772 }
11773
bb760063
DV
11774#define PIPE_CONF_QUIRK(quirk) \
11775 ((current_config->quirks | pipe_config->quirks) & (quirk))
11776
eccb140b
DV
11777 PIPE_CONF_CHECK_I(cpu_transcoder);
11778
08a24034
DV
11779 PIPE_CONF_CHECK_I(has_pch_encoder);
11780 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
11781 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11782 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11783 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11784 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11785 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 11786
eb14cb74 11787 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
11788
11789 if (INTEL_INFO(dev)->gen < 8) {
11790 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11791 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11792 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11793 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11794 PIPE_CONF_CHECK_I(dp_m_n.tu);
11795
11796 if (current_config->has_drrs) {
11797 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11798 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11799 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11800 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11801 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11802 }
11803 } else {
11804 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11805 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11806 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11807 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11808 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11809 }
eb14cb74 11810
2d112de7
ACO
11811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11812 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11813 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11817
2d112de7
ACO
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11824
c93f54cf 11825 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11826 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
11827 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11828 IS_VALLEYVIEW(dev))
11829 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11830 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11831
9ed109a7
DV
11832 PIPE_CONF_CHECK_I(has_audio);
11833
2d112de7 11834 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11835 DRM_MODE_FLAG_INTERLACE);
11836
bb760063 11837 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11838 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11839 DRM_MODE_FLAG_PHSYNC);
2d112de7 11840 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11841 DRM_MODE_FLAG_NHSYNC);
2d112de7 11842 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11843 DRM_MODE_FLAG_PVSYNC);
2d112de7 11844 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11845 DRM_MODE_FLAG_NVSYNC);
11846 }
045ac3b5 11847
37327abd
VS
11848 PIPE_CONF_CHECK_I(pipe_src_w);
11849 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 11850
9953599b
DV
11851 /*
11852 * FIXME: BIOS likes to set up a cloned config with lvds+external
11853 * screen. Since we don't yet re-compute the pipe config when moving
11854 * just the lvds port away to another pipe the sw tracking won't match.
11855 *
11856 * Proper atomic modesets with recomputed global state will fix this.
11857 * Until then just don't check gmch state for inherited modes.
11858 */
11859 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11860 PIPE_CONF_CHECK_I(gmch_pfit.control);
11861 /* pfit ratios are autocomputed by the hw on gen4+ */
11862 if (INTEL_INFO(dev)->gen < 4)
11863 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11864 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11865 }
11866
fd4daa9c
CW
11867 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11868 if (current_config->pch_pfit.enabled) {
11869 PIPE_CONF_CHECK_I(pch_pfit.pos);
11870 PIPE_CONF_CHECK_I(pch_pfit.size);
11871 }
2fa2fe9a 11872
a1b2278e
CK
11873 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11874
e59150dc
JB
11875 /* BDW+ don't expose a synchronous way to read the state */
11876 if (IS_HASWELL(dev))
11877 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11878
282740f7
VS
11879 PIPE_CONF_CHECK_I(double_wide);
11880
26804afd
DV
11881 PIPE_CONF_CHECK_X(ddi_pll_sel);
11882
c0d43d62 11883 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 11884 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11885 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11886 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11887 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11888 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
11889 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11890 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11891 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11892
42571aef
VS
11893 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11894 PIPE_CONF_CHECK_I(pipe_bpp);
11895
2d112de7 11896 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11897 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11898
66e985c0 11899#undef PIPE_CONF_CHECK_X
08a24034 11900#undef PIPE_CONF_CHECK_I
b95af8be 11901#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 11902#undef PIPE_CONF_CHECK_FLAGS
5e550656 11903#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11904#undef PIPE_CONF_QUIRK
88adfff1 11905
0e8ffe1b
DV
11906 return true;
11907}
11908
08db6652
DL
11909static void check_wm_state(struct drm_device *dev)
11910{
11911 struct drm_i915_private *dev_priv = dev->dev_private;
11912 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11913 struct intel_crtc *intel_crtc;
11914 int plane;
11915
11916 if (INTEL_INFO(dev)->gen < 9)
11917 return;
11918
11919 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11920 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11921
11922 for_each_intel_crtc(dev, intel_crtc) {
11923 struct skl_ddb_entry *hw_entry, *sw_entry;
11924 const enum pipe pipe = intel_crtc->pipe;
11925
11926 if (!intel_crtc->active)
11927 continue;
11928
11929 /* planes */
dd740780 11930 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
11931 hw_entry = &hw_ddb.plane[pipe][plane];
11932 sw_entry = &sw_ddb->plane[pipe][plane];
11933
11934 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11935 continue;
11936
11937 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11938 "(expected (%u,%u), found (%u,%u))\n",
11939 pipe_name(pipe), plane + 1,
11940 sw_entry->start, sw_entry->end,
11941 hw_entry->start, hw_entry->end);
11942 }
11943
11944 /* cursor */
11945 hw_entry = &hw_ddb.cursor[pipe];
11946 sw_entry = &sw_ddb->cursor[pipe];
11947
11948 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11949 continue;
11950
11951 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11952 "(expected (%u,%u), found (%u,%u))\n",
11953 pipe_name(pipe),
11954 sw_entry->start, sw_entry->end,
11955 hw_entry->start, hw_entry->end);
11956 }
11957}
11958
91d1b4bd
DV
11959static void
11960check_connector_state(struct drm_device *dev)
8af6cf88 11961{
8af6cf88
DV
11962 struct intel_connector *connector;
11963
3a3371ff 11964 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11965 /* This also checks the encoder/connector hw state with the
11966 * ->get_hw_state callbacks. */
11967 intel_connector_check_state(connector);
11968
e2c719b7 11969 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
11970 "connector's staged encoder doesn't match current encoder\n");
11971 }
91d1b4bd
DV
11972}
11973
11974static void
11975check_encoder_state(struct drm_device *dev)
11976{
11977 struct intel_encoder *encoder;
11978 struct intel_connector *connector;
8af6cf88 11979
b2784e15 11980 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11981 bool enabled = false;
11982 bool active = false;
11983 enum pipe pipe, tracked_pipe;
11984
11985 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11986 encoder->base.base.id,
8e329a03 11987 encoder->base.name);
8af6cf88 11988
e2c719b7 11989 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 11990 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 11991 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
11992 "encoder's active_connectors set, but no crtc\n");
11993
3a3371ff 11994 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11995 if (connector->base.encoder != &encoder->base)
11996 continue;
11997 enabled = true;
11998 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11999 active = true;
12000 }
0e32b39c
DA
12001 /*
12002 * for MST connectors if we unplug the connector is gone
12003 * away but the encoder is still connected to a crtc
12004 * until a modeset happens in response to the hotplug.
12005 */
12006 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12007 continue;
12008
e2c719b7 12009 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12010 "encoder's enabled state mismatch "
12011 "(expected %i, found %i)\n",
12012 !!encoder->base.crtc, enabled);
e2c719b7 12013 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12014 "active encoder with no crtc\n");
12015
e2c719b7 12016 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12017 "encoder's computed active state doesn't match tracked active state "
12018 "(expected %i, found %i)\n", active, encoder->connectors_active);
12019
12020 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12021 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12022 "encoder's hw state doesn't match sw tracking "
12023 "(expected %i, found %i)\n",
12024 encoder->connectors_active, active);
12025
12026 if (!encoder->base.crtc)
12027 continue;
12028
12029 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12030 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12031 "active encoder's pipe doesn't match"
12032 "(expected %i, found %i)\n",
12033 tracked_pipe, pipe);
12034
12035 }
91d1b4bd
DV
12036}
12037
12038static void
12039check_crtc_state(struct drm_device *dev)
12040{
fbee40df 12041 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12042 struct intel_crtc *crtc;
12043 struct intel_encoder *encoder;
5cec258b 12044 struct intel_crtc_state pipe_config;
8af6cf88 12045
d3fcc808 12046 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12047 bool enabled = false;
12048 bool active = false;
12049
045ac3b5
JB
12050 memset(&pipe_config, 0, sizeof(pipe_config));
12051
8af6cf88
DV
12052 DRM_DEBUG_KMS("[CRTC:%d]\n",
12053 crtc->base.base.id);
12054
83d65738 12055 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12056 "active crtc, but not enabled in sw tracking\n");
12057
b2784e15 12058 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12059 if (encoder->base.crtc != &crtc->base)
12060 continue;
12061 enabled = true;
12062 if (encoder->connectors_active)
12063 active = true;
12064 }
6c49f241 12065
e2c719b7 12066 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12067 "crtc's computed active state doesn't match tracked active state "
12068 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12069 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12070 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12071 "(expected %i, found %i)\n", enabled,
12072 crtc->base.state->enable);
8af6cf88 12073
0e8ffe1b
DV
12074 active = dev_priv->display.get_pipe_config(crtc,
12075 &pipe_config);
d62cf62a 12076
b6b5d049
VS
12077 /* hw state is inconsistent with the pipe quirk */
12078 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12079 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12080 active = crtc->active;
12081
b2784e15 12082 for_each_intel_encoder(dev, encoder) {
3eaba51c 12083 enum pipe pipe;
6c49f241
DV
12084 if (encoder->base.crtc != &crtc->base)
12085 continue;
1d37b689 12086 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12087 encoder->get_config(encoder, &pipe_config);
12088 }
12089
e2c719b7 12090 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12091 "crtc active state doesn't match with hw state "
12092 "(expected %i, found %i)\n", crtc->active, active);
12093
c0b03411 12094 if (active &&
6e3c9717 12095 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12096 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12097 intel_dump_pipe_config(crtc, &pipe_config,
12098 "[hw state]");
6e3c9717 12099 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12100 "[sw state]");
12101 }
8af6cf88
DV
12102 }
12103}
12104
91d1b4bd
DV
12105static void
12106check_shared_dpll_state(struct drm_device *dev)
12107{
fbee40df 12108 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12109 struct intel_crtc *crtc;
12110 struct intel_dpll_hw_state dpll_hw_state;
12111 int i;
5358901f
DV
12112
12113 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12114 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12115 int enabled_crtcs = 0, active_crtcs = 0;
12116 bool active;
12117
12118 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12119
12120 DRM_DEBUG_KMS("%s\n", pll->name);
12121
12122 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12123
e2c719b7 12124 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12125 "more active pll users than references: %i vs %i\n",
3e369b76 12126 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12127 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12128 "pll in active use but not on in sw tracking\n");
e2c719b7 12129 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12130 "pll in on but not on in use in sw tracking\n");
e2c719b7 12131 I915_STATE_WARN(pll->on != active,
5358901f
DV
12132 "pll on state mismatch (expected %i, found %i)\n",
12133 pll->on, active);
12134
d3fcc808 12135 for_each_intel_crtc(dev, crtc) {
83d65738 12136 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12137 enabled_crtcs++;
12138 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12139 active_crtcs++;
12140 }
e2c719b7 12141 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12142 "pll active crtcs mismatch (expected %i, found %i)\n",
12143 pll->active, active_crtcs);
e2c719b7 12144 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12145 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12146 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12147
e2c719b7 12148 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12149 sizeof(dpll_hw_state)),
12150 "pll hw state mismatch\n");
5358901f 12151 }
8af6cf88
DV
12152}
12153
91d1b4bd
DV
12154void
12155intel_modeset_check_state(struct drm_device *dev)
12156{
08db6652 12157 check_wm_state(dev);
91d1b4bd
DV
12158 check_connector_state(dev);
12159 check_encoder_state(dev);
12160 check_crtc_state(dev);
12161 check_shared_dpll_state(dev);
12162}
12163
5cec258b 12164void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12165 int dotclock)
12166{
12167 /*
12168 * FDI already provided one idea for the dotclock.
12169 * Yell if the encoder disagrees.
12170 */
2d112de7 12171 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12172 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12173 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12174}
12175
80715b2f
VS
12176static void update_scanline_offset(struct intel_crtc *crtc)
12177{
12178 struct drm_device *dev = crtc->base.dev;
12179
12180 /*
12181 * The scanline counter increments at the leading edge of hsync.
12182 *
12183 * On most platforms it starts counting from vtotal-1 on the
12184 * first active line. That means the scanline counter value is
12185 * always one less than what we would expect. Ie. just after
12186 * start of vblank, which also occurs at start of hsync (on the
12187 * last active line), the scanline counter will read vblank_start-1.
12188 *
12189 * On gen2 the scanline counter starts counting from 1 instead
12190 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12191 * to keep the value positive), instead of adding one.
12192 *
12193 * On HSW+ the behaviour of the scanline counter depends on the output
12194 * type. For DP ports it behaves like most other platforms, but on HDMI
12195 * there's an extra 1 line difference. So we need to add two instead of
12196 * one to the value.
12197 */
12198 if (IS_GEN2(dev)) {
6e3c9717 12199 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12200 int vtotal;
12201
12202 vtotal = mode->crtc_vtotal;
12203 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12204 vtotal /= 2;
12205
12206 crtc->scanline_offset = vtotal - 1;
12207 } else if (HAS_DDI(dev) &&
409ee761 12208 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12209 crtc->scanline_offset = 2;
12210 } else
12211 crtc->scanline_offset = 1;
12212}
12213
5cec258b 12214static struct intel_crtc_state *
7f27126e
JB
12215intel_modeset_compute_config(struct drm_crtc *crtc,
12216 struct drm_display_mode *mode,
83a57153 12217 struct drm_atomic_state *state,
7f27126e
JB
12218 unsigned *modeset_pipes,
12219 unsigned *prepare_pipes,
12220 unsigned *disable_pipes)
12221{
db7542dd 12222 struct drm_device *dev = crtc->dev;
5cec258b 12223 struct intel_crtc_state *pipe_config = NULL;
db7542dd 12224 struct intel_crtc *intel_crtc;
0b901879
ACO
12225 int ret = 0;
12226
12227 ret = drm_atomic_add_affected_connectors(state, crtc);
12228 if (ret)
12229 return ERR_PTR(ret);
7f27126e
JB
12230
12231 intel_modeset_affected_pipes(crtc, modeset_pipes,
12232 prepare_pipes, disable_pipes);
12233
db7542dd
ACO
12234 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12235 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12236 if (IS_ERR(pipe_config))
12237 return pipe_config;
12238
12239 pipe_config->base.enable = false;
12240 }
7f27126e
JB
12241
12242 /*
12243 * Note this needs changes when we start tracking multiple modes
12244 * and crtcs. At that point we'll need to compute the whole config
12245 * (i.e. one pipe_config for each crtc) rather than just the one
12246 * for this crtc.
12247 */
db7542dd
ACO
12248 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12249 /* FIXME: For now we still expect modeset_pipes has at most
12250 * one bit set. */
12251 if (WARN_ON(&intel_crtc->base != crtc))
12252 continue;
83a57153 12253
8805aa71 12254 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
db7542dd
ACO
12255 if (IS_ERR(pipe_config))
12256 return pipe_config;
7f27126e 12257
304603f4
ACO
12258 pipe_config->base.enable = true;
12259
db7542dd
ACO
12260 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12261 "[modeset]");
12262 }
12263
12264 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
7f27126e
JB
12265}
12266
225da59b 12267static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
ed6739ef
ACO
12268 unsigned modeset_pipes,
12269 unsigned disable_pipes)
12270{
225da59b 12271 struct drm_device *dev = state->dev;
ed6739ef
ACO
12272 struct drm_i915_private *dev_priv = to_i915(dev);
12273 unsigned clear_pipes = modeset_pipes | disable_pipes;
12274 struct intel_crtc *intel_crtc;
12275 int ret = 0;
12276
12277 if (!dev_priv->display.crtc_compute_clock)
12278 return 0;
12279
12280 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12281 if (ret)
12282 goto done;
12283
12284 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
225da59b
ACO
12285 struct intel_crtc_state *crtc_state =
12286 intel_atomic_get_crtc_state(state, intel_crtc);
12287
12288 /* Modeset pipes should have a new state by now */
12289 if (WARN_ON(IS_ERR(crtc_state)))
12290 continue;
12291
ed6739ef 12292 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
225da59b 12293 crtc_state);
ed6739ef
ACO
12294 if (ret) {
12295 intel_shared_dpll_abort_config(dev_priv);
12296 goto done;
12297 }
12298 }
12299
12300done:
12301 return ret;
12302}
12303
f30da187
DV
12304static int __intel_set_mode(struct drm_crtc *crtc,
12305 struct drm_display_mode *mode,
7f27126e 12306 int x, int y, struct drm_framebuffer *fb,
5cec258b 12307 struct intel_crtc_state *pipe_config,
7f27126e
JB
12308 unsigned modeset_pipes,
12309 unsigned prepare_pipes,
12310 unsigned disable_pipes)
a6778b3c
DV
12311{
12312 struct drm_device *dev = crtc->dev;
fbee40df 12313 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 12314 struct drm_display_mode *saved_mode;
304603f4 12315 struct drm_atomic_state *state = pipe_config->base.state;
83a57153 12316 struct intel_crtc_state *crtc_state_copy = NULL;
25c5b266 12317 struct intel_crtc *intel_crtc;
c0c36b94 12318 int ret = 0;
a6778b3c 12319
4b4b9238 12320 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
12321 if (!saved_mode)
12322 return -ENOMEM;
a6778b3c 12323
83a57153
ACO
12324 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12325 if (!crtc_state_copy) {
12326 ret = -ENOMEM;
12327 goto done;
12328 }
12329
3ac18232 12330 *saved_mode = crtc->mode;
a6778b3c 12331
30a970c6
JB
12332 /*
12333 * See if the config requires any additional preparation, e.g.
12334 * to adjust global state with pipes off. We need to do this
12335 * here so we can get the modeset_pipe updated config for the new
12336 * mode set on this crtc. For other crtcs we need to use the
12337 * adjusted_mode bits in the crtc directly.
12338 */
f8437dd1 12339 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
304603f4
ACO
12340 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12341 if (ret)
12342 goto done;
30a970c6 12343
c164f833
VS
12344 /* may have added more to prepare_pipes than we should */
12345 prepare_pipes &= ~disable_pipes;
12346 }
12347
225da59b 12348 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
ed6739ef
ACO
12349 if (ret)
12350 goto done;
8bd31e67 12351
460da916
DV
12352 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12353 intel_crtc_disable(&intel_crtc->base);
12354
ea9d758d 12355 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
83d65738 12356 if (intel_crtc->base.state->enable)
ea9d758d
DV
12357 dev_priv->display.crtc_disable(&intel_crtc->base);
12358 }
a6778b3c 12359
6c4c86f5
DV
12360 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12361 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
12362 *
12363 * Note we'll need to fix this up when we start tracking multiple
12364 * pipes; here we assume a single modeset_pipe and only track the
12365 * single crtc and mode.
f6e5b160 12366 */
b8cecdf5 12367 if (modeset_pipes) {
25c5b266 12368 crtc->mode = *mode;
b8cecdf5
DV
12369 /* mode_set/enable/disable functions rely on a correct pipe
12370 * config. */
f5de6e07 12371 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
12372
12373 /*
12374 * Calculate and store various constants which
12375 * are later needed by vblank and swap-completion
12376 * timestamping. They are derived from true hwmode.
12377 */
12378 drm_calc_timestamping_constants(crtc,
2d112de7 12379 &pipe_config->base.adjusted_mode);
b8cecdf5 12380 }
7758a113 12381
ea9d758d
DV
12382 /* Only after disabling all output pipelines that will be changed can we
12383 * update the the output configuration. */
12384 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 12385
304603f4 12386 modeset_update_crtc_power_domains(state);
47fab737 12387
25c5b266 12388 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
12389 struct drm_plane *primary = intel_crtc->base.primary;
12390 int vdisplay, hdisplay;
4c10794f 12391
455a6808 12392 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
70a101f8
MR
12393 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12394 fb, 0, 0,
12395 hdisplay, vdisplay,
12396 x << 16, y << 16,
12397 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
12398 }
12399
12400 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
12401 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12402 update_scanline_offset(intel_crtc);
12403
25c5b266 12404 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 12405 }
a6778b3c 12406
a6778b3c
DV
12407 /* FIXME: add subpixel order */
12408done:
83d65738 12409 if (ret && crtc->state->enable)
3ac18232 12410 crtc->mode = *saved_mode;
a6778b3c 12411
83a57153
ACO
12412 if (ret == 0 && pipe_config) {
12413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12414
12415 /* The pipe_config will be freed with the atomic state, so
12416 * make a copy. */
12417 memcpy(crtc_state_copy, intel_crtc->config,
12418 sizeof *crtc_state_copy);
12419 intel_crtc->config = crtc_state_copy;
12420 intel_crtc->base.state = &crtc_state_copy->base;
83a57153
ACO
12421 } else {
12422 kfree(crtc_state_copy);
12423 }
12424
3ac18232 12425 kfree(saved_mode);
a6778b3c 12426 return ret;
f6e5b160
CW
12427}
12428
7f27126e
JB
12429static int intel_set_mode_pipes(struct drm_crtc *crtc,
12430 struct drm_display_mode *mode,
12431 int x, int y, struct drm_framebuffer *fb,
5cec258b 12432 struct intel_crtc_state *pipe_config,
7f27126e
JB
12433 unsigned modeset_pipes,
12434 unsigned prepare_pipes,
12435 unsigned disable_pipes)
f30da187
DV
12436{
12437 int ret;
12438
7f27126e
JB
12439 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12440 prepare_pipes, disable_pipes);
f30da187
DV
12441
12442 if (ret == 0)
12443 intel_modeset_check_state(crtc->dev);
12444
12445 return ret;
12446}
12447
7f27126e
JB
12448static int intel_set_mode(struct drm_crtc *crtc,
12449 struct drm_display_mode *mode,
83a57153
ACO
12450 int x, int y, struct drm_framebuffer *fb,
12451 struct drm_atomic_state *state)
7f27126e 12452{
5cec258b 12453 struct intel_crtc_state *pipe_config;
7f27126e 12454 unsigned modeset_pipes, prepare_pipes, disable_pipes;
83a57153 12455 int ret = 0;
7f27126e 12456
8805aa71 12457 pipe_config = intel_modeset_compute_config(crtc, mode, state,
7f27126e
JB
12458 &modeset_pipes,
12459 &prepare_pipes,
12460 &disable_pipes);
12461
83a57153
ACO
12462 if (IS_ERR(pipe_config)) {
12463 ret = PTR_ERR(pipe_config);
12464 goto out;
12465 }
12466
12467 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12468 modeset_pipes, prepare_pipes,
12469 disable_pipes);
12470 if (ret)
12471 goto out;
7f27126e 12472
83a57153
ACO
12473out:
12474 return ret;
7f27126e
JB
12475}
12476
c0c36b94
CW
12477void intel_crtc_restore_mode(struct drm_crtc *crtc)
12478{
83a57153
ACO
12479 struct drm_device *dev = crtc->dev;
12480 struct drm_atomic_state *state;
12481 struct intel_encoder *encoder;
12482 struct intel_connector *connector;
12483 struct drm_connector_state *connector_state;
12484
12485 state = drm_atomic_state_alloc(dev);
12486 if (!state) {
12487 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12488 crtc->base.id);
12489 return;
12490 }
12491
12492 state->acquire_ctx = dev->mode_config.acquire_ctx;
12493
12494 /* The force restore path in the HW readout code relies on the staged
12495 * config still keeping the user requested config while the actual
12496 * state has been overwritten by the configuration read from HW. We
12497 * need to copy the staged config to the atomic state, otherwise the
12498 * mode set will just reapply the state the HW is already in. */
12499 for_each_intel_encoder(dev, encoder) {
12500 if (&encoder->new_crtc->base != crtc)
12501 continue;
12502
12503 for_each_intel_connector(dev, connector) {
12504 if (connector->new_encoder != encoder)
12505 continue;
12506
12507 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12508 if (IS_ERR(connector_state)) {
12509 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12510 connector->base.base.id,
12511 connector->base.name,
12512 PTR_ERR(connector_state));
12513 continue;
12514 }
12515
12516 connector_state->crtc = crtc;
12517 connector_state->best_encoder = &encoder->base;
12518 }
12519 }
12520
12521 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12522 state);
12523
12524 drm_atomic_state_free(state);
c0c36b94
CW
12525}
12526
25c5b266
DV
12527#undef for_each_intel_crtc_masked
12528
d9e55608
DV
12529static void intel_set_config_free(struct intel_set_config *config)
12530{
12531 if (!config)
12532 return;
12533
1aa4b628
DV
12534 kfree(config->save_connector_encoders);
12535 kfree(config->save_encoder_crtcs);
7668851f 12536 kfree(config->save_crtc_enabled);
d9e55608
DV
12537 kfree(config);
12538}
12539
85f9eb71
DV
12540static int intel_set_config_save_state(struct drm_device *dev,
12541 struct intel_set_config *config)
12542{
7668851f 12543 struct drm_crtc *crtc;
85f9eb71
DV
12544 struct drm_encoder *encoder;
12545 struct drm_connector *connector;
12546 int count;
12547
7668851f
VS
12548 config->save_crtc_enabled =
12549 kcalloc(dev->mode_config.num_crtc,
12550 sizeof(bool), GFP_KERNEL);
12551 if (!config->save_crtc_enabled)
12552 return -ENOMEM;
12553
1aa4b628
DV
12554 config->save_encoder_crtcs =
12555 kcalloc(dev->mode_config.num_encoder,
12556 sizeof(struct drm_crtc *), GFP_KERNEL);
12557 if (!config->save_encoder_crtcs)
85f9eb71
DV
12558 return -ENOMEM;
12559
1aa4b628
DV
12560 config->save_connector_encoders =
12561 kcalloc(dev->mode_config.num_connector,
12562 sizeof(struct drm_encoder *), GFP_KERNEL);
12563 if (!config->save_connector_encoders)
85f9eb71
DV
12564 return -ENOMEM;
12565
12566 /* Copy data. Note that driver private data is not affected.
12567 * Should anything bad happen only the expected state is
12568 * restored, not the drivers personal bookkeeping.
12569 */
7668851f 12570 count = 0;
70e1e0ec 12571 for_each_crtc(dev, crtc) {
83d65738 12572 config->save_crtc_enabled[count++] = crtc->state->enable;
7668851f
VS
12573 }
12574
85f9eb71
DV
12575 count = 0;
12576 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 12577 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
12578 }
12579
12580 count = 0;
12581 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 12582 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
12583 }
12584
12585 return 0;
12586}
12587
12588static void intel_set_config_restore_state(struct drm_device *dev,
12589 struct intel_set_config *config)
12590{
7668851f 12591 struct intel_crtc *crtc;
9a935856
DV
12592 struct intel_encoder *encoder;
12593 struct intel_connector *connector;
85f9eb71
DV
12594 int count;
12595
7668851f 12596 count = 0;
d3fcc808 12597 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12598 crtc->new_enabled = config->save_crtc_enabled[count++];
12599 }
12600
85f9eb71 12601 count = 0;
b2784e15 12602 for_each_intel_encoder(dev, encoder) {
9a935856
DV
12603 encoder->new_crtc =
12604 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
12605 }
12606
12607 count = 0;
3a3371ff 12608 for_each_intel_connector(dev, connector) {
9a935856
DV
12609 connector->new_encoder =
12610 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
12611 }
12612}
12613
e3de42b6 12614static bool
2e57f47d 12615is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
12616{
12617 int i;
12618
2e57f47d
CW
12619 if (set->num_connectors == 0)
12620 return false;
12621
12622 if (WARN_ON(set->connectors == NULL))
12623 return false;
12624
12625 for (i = 0; i < set->num_connectors; i++)
12626 if (set->connectors[i]->encoder &&
12627 set->connectors[i]->encoder->crtc == set->crtc &&
12628 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
12629 return true;
12630
12631 return false;
12632}
12633
5e2b584e
DV
12634static void
12635intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12636 struct intel_set_config *config)
12637{
12638
12639 /* We should be able to check here if the fb has the same properties
12640 * and then just flip_or_move it */
2e57f47d
CW
12641 if (is_crtc_connector_off(set)) {
12642 config->mode_changed = true;
f4510a27 12643 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
12644 /*
12645 * If we have no fb, we can only flip as long as the crtc is
12646 * active, otherwise we need a full mode set. The crtc may
12647 * be active if we've only disabled the primary plane, or
12648 * in fastboot situations.
12649 */
f4510a27 12650 if (set->crtc->primary->fb == NULL) {
319d9827
JB
12651 struct intel_crtc *intel_crtc =
12652 to_intel_crtc(set->crtc);
12653
3b150f08 12654 if (intel_crtc->active) {
319d9827
JB
12655 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12656 config->fb_changed = true;
12657 } else {
12658 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12659 config->mode_changed = true;
12660 }
5e2b584e
DV
12661 } else if (set->fb == NULL) {
12662 config->mode_changed = true;
72f4901e 12663 } else if (set->fb->pixel_format !=
f4510a27 12664 set->crtc->primary->fb->pixel_format) {
5e2b584e 12665 config->mode_changed = true;
e3de42b6 12666 } else {
5e2b584e 12667 config->fb_changed = true;
e3de42b6 12668 }
5e2b584e
DV
12669 }
12670
835c5873 12671 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
12672 config->fb_changed = true;
12673
12674 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12675 DRM_DEBUG_KMS("modes are different, full mode set\n");
12676 drm_mode_debug_printmodeline(&set->crtc->mode);
12677 drm_mode_debug_printmodeline(set->mode);
12678 config->mode_changed = true;
12679 }
a1d95703
CW
12680
12681 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12682 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
12683}
12684
2e431051 12685static int
9a935856
DV
12686intel_modeset_stage_output_state(struct drm_device *dev,
12687 struct drm_mode_set *set,
944b0c76
ACO
12688 struct intel_set_config *config,
12689 struct drm_atomic_state *state)
50f56119 12690{
9a935856 12691 struct intel_connector *connector;
944b0c76 12692 struct drm_connector_state *connector_state;
9a935856 12693 struct intel_encoder *encoder;
7668851f 12694 struct intel_crtc *crtc;
f3f08572 12695 int ro;
50f56119 12696
9abdda74 12697 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
12698 * of connectors. For paranoia, double-check this. */
12699 WARN_ON(!set->fb && (set->num_connectors != 0));
12700 WARN_ON(set->fb && (set->num_connectors == 0));
12701
3a3371ff 12702 for_each_intel_connector(dev, connector) {
9a935856
DV
12703 /* Otherwise traverse passed in connector list and get encoders
12704 * for them. */
50f56119 12705 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12706 if (set->connectors[ro] == &connector->base) {
0e32b39c 12707 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
12708 break;
12709 }
12710 }
12711
9a935856
DV
12712 /* If we disable the crtc, disable all its connectors. Also, if
12713 * the connector is on the changing crtc but not on the new
12714 * connector list, disable it. */
12715 if ((!set->fb || ro == set->num_connectors) &&
12716 connector->base.encoder &&
12717 connector->base.encoder->crtc == set->crtc) {
12718 connector->new_encoder = NULL;
12719
12720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12721 connector->base.base.id,
c23cc417 12722 connector->base.name);
9a935856
DV
12723 }
12724
12725
12726 if (&connector->new_encoder->base != connector->base.encoder) {
10634189
ACO
12727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12728 connector->base.base.id,
12729 connector->base.name);
5e2b584e 12730 config->mode_changed = true;
50f56119
DV
12731 }
12732 }
9a935856 12733 /* connector->new_encoder is now updated for all connectors. */
50f56119 12734
9a935856 12735 /* Update crtc of enabled connectors. */
3a3371ff 12736 for_each_intel_connector(dev, connector) {
7668851f
VS
12737 struct drm_crtc *new_crtc;
12738
9a935856 12739 if (!connector->new_encoder)
50f56119
DV
12740 continue;
12741
9a935856 12742 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
12743
12744 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12745 if (set->connectors[ro] == &connector->base)
50f56119
DV
12746 new_crtc = set->crtc;
12747 }
12748
12749 /* Make sure the new CRTC will work with the encoder */
14509916
TR
12750 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12751 new_crtc)) {
5e2b584e 12752 return -EINVAL;
50f56119 12753 }
0e32b39c 12754 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856 12755
944b0c76
ACO
12756 connector_state =
12757 drm_atomic_get_connector_state(state, &connector->base);
12758 if (IS_ERR(connector_state))
12759 return PTR_ERR(connector_state);
12760
12761 connector_state->crtc = new_crtc;
12762 connector_state->best_encoder = &connector->new_encoder->base;
12763
9a935856
DV
12764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12765 connector->base.base.id,
c23cc417 12766 connector->base.name,
9a935856
DV
12767 new_crtc->base.id);
12768 }
12769
12770 /* Check for any encoders that needs to be disabled. */
b2784e15 12771 for_each_intel_encoder(dev, encoder) {
5a65f358 12772 int num_connectors = 0;
3a3371ff 12773 for_each_intel_connector(dev, connector) {
9a935856
DV
12774 if (connector->new_encoder == encoder) {
12775 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 12776 num_connectors++;
9a935856
DV
12777 }
12778 }
5a65f358
PZ
12779
12780 if (num_connectors == 0)
12781 encoder->new_crtc = NULL;
12782 else if (num_connectors > 1)
12783 return -EINVAL;
12784
9a935856
DV
12785 /* Only now check for crtc changes so we don't miss encoders
12786 * that will be disabled. */
12787 if (&encoder->new_crtc->base != encoder->base.crtc) {
10634189
ACO
12788 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12789 encoder->base.base.id,
12790 encoder->base.name);
5e2b584e 12791 config->mode_changed = true;
50f56119
DV
12792 }
12793 }
9a935856 12794 /* Now we've also updated encoder->new_crtc for all encoders. */
3a3371ff 12795 for_each_intel_connector(dev, connector) {
944b0c76
ACO
12796 connector_state =
12797 drm_atomic_get_connector_state(state, &connector->base);
9d918c15
ACO
12798 if (IS_ERR(connector_state))
12799 return PTR_ERR(connector_state);
944b0c76
ACO
12800
12801 if (connector->new_encoder) {
0e32b39c
DA
12802 if (connector->new_encoder != connector->encoder)
12803 connector->encoder = connector->new_encoder;
944b0c76
ACO
12804 } else {
12805 connector_state->crtc = NULL;
f61cccf3 12806 connector_state->best_encoder = NULL;
944b0c76 12807 }
0e32b39c 12808 }
d3fcc808 12809 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12810 crtc->new_enabled = false;
12811
b2784e15 12812 for_each_intel_encoder(dev, encoder) {
7668851f
VS
12813 if (encoder->new_crtc == crtc) {
12814 crtc->new_enabled = true;
12815 break;
12816 }
12817 }
12818
83d65738 12819 if (crtc->new_enabled != crtc->base.state->enable) {
10634189
ACO
12820 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12821 crtc->base.base.id,
7668851f
VS
12822 crtc->new_enabled ? "en" : "dis");
12823 config->mode_changed = true;
12824 }
12825 }
12826
2e431051
DV
12827 return 0;
12828}
12829
7d00a1f5
VS
12830static void disable_crtc_nofb(struct intel_crtc *crtc)
12831{
12832 struct drm_device *dev = crtc->base.dev;
12833 struct intel_encoder *encoder;
12834 struct intel_connector *connector;
12835
12836 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12837 pipe_name(crtc->pipe));
12838
3a3371ff 12839 for_each_intel_connector(dev, connector) {
7d00a1f5
VS
12840 if (connector->new_encoder &&
12841 connector->new_encoder->new_crtc == crtc)
12842 connector->new_encoder = NULL;
12843 }
12844
b2784e15 12845 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
12846 if (encoder->new_crtc == crtc)
12847 encoder->new_crtc = NULL;
12848 }
12849
12850 crtc->new_enabled = false;
12851}
12852
2e431051
DV
12853static int intel_crtc_set_config(struct drm_mode_set *set)
12854{
12855 struct drm_device *dev;
2e431051 12856 struct drm_mode_set save_set;
83a57153 12857 struct drm_atomic_state *state = NULL;
2e431051 12858 struct intel_set_config *config;
5cec258b 12859 struct intel_crtc_state *pipe_config;
50f52756 12860 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 12861 int ret;
2e431051 12862
8d3e375e
DV
12863 BUG_ON(!set);
12864 BUG_ON(!set->crtc);
12865 BUG_ON(!set->crtc->helper_private);
2e431051 12866
7e53f3a4
DV
12867 /* Enforce sane interface api - has been abused by the fb helper. */
12868 BUG_ON(!set->mode && set->fb);
12869 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 12870
2e431051
DV
12871 if (set->fb) {
12872 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12873 set->crtc->base.id, set->fb->base.id,
12874 (int)set->num_connectors, set->x, set->y);
12875 } else {
12876 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
12877 }
12878
12879 dev = set->crtc->dev;
12880
12881 ret = -ENOMEM;
12882 config = kzalloc(sizeof(*config), GFP_KERNEL);
12883 if (!config)
12884 goto out_config;
12885
12886 ret = intel_set_config_save_state(dev, config);
12887 if (ret)
12888 goto out_config;
12889
12890 save_set.crtc = set->crtc;
12891 save_set.mode = &set->crtc->mode;
12892 save_set.x = set->crtc->x;
12893 save_set.y = set->crtc->y;
f4510a27 12894 save_set.fb = set->crtc->primary->fb;
2e431051
DV
12895
12896 /* Compute whether we need a full modeset, only an fb base update or no
12897 * change at all. In the future we might also check whether only the
12898 * mode changed, e.g. for LVDS where we only change the panel fitter in
12899 * such cases. */
12900 intel_set_config_compute_mode_changes(set, config);
12901
83a57153
ACO
12902 state = drm_atomic_state_alloc(dev);
12903 if (!state) {
12904 ret = -ENOMEM;
12905 goto out_config;
12906 }
12907
12908 state->acquire_ctx = dev->mode_config.acquire_ctx;
12909
944b0c76 12910 ret = intel_modeset_stage_output_state(dev, set, config, state);
2e431051
DV
12911 if (ret)
12912 goto fail;
12913
50f52756 12914 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
8805aa71 12915 state,
50f52756
JB
12916 &modeset_pipes,
12917 &prepare_pipes,
12918 &disable_pipes);
20664591 12919 if (IS_ERR(pipe_config)) {
6ac0483b 12920 ret = PTR_ERR(pipe_config);
50f52756 12921 goto fail;
20664591 12922 } else if (pipe_config) {
b9950a13 12923 if (pipe_config->has_audio !=
6e3c9717 12924 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
12925 config->mode_changed = true;
12926
af15d2ce
JB
12927 /*
12928 * Note we have an issue here with infoframes: current code
12929 * only updates them on the full mode set path per hw
12930 * requirements. So here we should be checking for any
12931 * required changes and forcing a mode set.
12932 */
20664591 12933 }
50f52756 12934
1f9954d0
JB
12935 intel_update_pipe_size(to_intel_crtc(set->crtc));
12936
5e2b584e 12937 if (config->mode_changed) {
50f52756
JB
12938 ret = intel_set_mode_pipes(set->crtc, set->mode,
12939 set->x, set->y, set->fb, pipe_config,
12940 modeset_pipes, prepare_pipes,
12941 disable_pipes);
5e2b584e 12942 } else if (config->fb_changed) {
3b150f08 12943 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808
GP
12944 struct drm_plane *primary = set->crtc->primary;
12945 int vdisplay, hdisplay;
3b150f08 12946
455a6808 12947 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
70a101f8
MR
12948 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12949 0, 0, hdisplay, vdisplay,
12950 set->x << 16, set->y << 16,
12951 hdisplay << 16, vdisplay << 16);
3b150f08
MR
12952
12953 /*
12954 * We need to make sure the primary plane is re-enabled if it
12955 * has previously been turned off.
12956 */
12957 if (!intel_crtc->primary_enabled && ret == 0) {
12958 WARN_ON(!intel_crtc->active);
fdd508a6 12959 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
12960 }
12961
7ca51a3a
JB
12962 /*
12963 * In the fastboot case this may be our only check of the
12964 * state after boot. It would be better to only do it on
12965 * the first update, but we don't have a nice way of doing that
12966 * (and really, set_config isn't used much for high freq page
12967 * flipping, so increasing its cost here shouldn't be a big
12968 * deal).
12969 */
d330a953 12970 if (i915.fastboot && ret == 0)
7ca51a3a 12971 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
12972 }
12973
2d05eae1 12974 if (ret) {
bf67dfeb
DV
12975 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12976 set->crtc->base.id, ret);
50f56119 12977fail:
2d05eae1 12978 intel_set_config_restore_state(dev, config);
50f56119 12979
83a57153
ACO
12980 drm_atomic_state_clear(state);
12981
7d00a1f5
VS
12982 /*
12983 * HACK: if the pipe was on, but we didn't have a framebuffer,
12984 * force the pipe off to avoid oopsing in the modeset code
12985 * due to fb==NULL. This should only happen during boot since
12986 * we don't yet reconstruct the FB from the hardware state.
12987 */
12988 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12989 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12990
2d05eae1
CW
12991 /* Try to restore the config */
12992 if (config->mode_changed &&
12993 intel_set_mode(save_set.crtc, save_set.mode,
83a57153
ACO
12994 save_set.x, save_set.y, save_set.fb,
12995 state))
2d05eae1
CW
12996 DRM_ERROR("failed to restore config after modeset failure\n");
12997 }
50f56119 12998
d9e55608 12999out_config:
83a57153
ACO
13000 if (state)
13001 drm_atomic_state_free(state);
13002
d9e55608 13003 intel_set_config_free(config);
50f56119
DV
13004 return ret;
13005}
f6e5b160
CW
13006
13007static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13008 .gamma_set = intel_crtc_gamma_set,
50f56119 13009 .set_config = intel_crtc_set_config,
f6e5b160
CW
13010 .destroy = intel_crtc_destroy,
13011 .page_flip = intel_crtc_page_flip,
1356837e
MR
13012 .atomic_duplicate_state = intel_crtc_duplicate_state,
13013 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13014};
13015
5358901f
DV
13016static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13017 struct intel_shared_dpll *pll,
13018 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13019{
5358901f 13020 uint32_t val;
ee7b9f93 13021
f458ebbc 13022 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13023 return false;
13024
5358901f 13025 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13026 hw_state->dpll = val;
13027 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13028 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13029
13030 return val & DPLL_VCO_ENABLE;
13031}
13032
15bdd4cf
DV
13033static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13034 struct intel_shared_dpll *pll)
13035{
3e369b76
ACO
13036 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13037 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13038}
13039
e7b903d2
DV
13040static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13041 struct intel_shared_dpll *pll)
13042{
e7b903d2 13043 /* PCH refclock must be enabled first */
89eff4be 13044 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13045
3e369b76 13046 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13047
13048 /* Wait for the clocks to stabilize. */
13049 POSTING_READ(PCH_DPLL(pll->id));
13050 udelay(150);
13051
13052 /* The pixel multiplier can only be updated once the
13053 * DPLL is enabled and the clocks are stable.
13054 *
13055 * So write it again.
13056 */
3e369b76 13057 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13058 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13059 udelay(200);
13060}
13061
13062static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13063 struct intel_shared_dpll *pll)
13064{
13065 struct drm_device *dev = dev_priv->dev;
13066 struct intel_crtc *crtc;
e7b903d2
DV
13067
13068 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13069 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13070 if (intel_crtc_to_shared_dpll(crtc) == pll)
13071 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13072 }
13073
15bdd4cf
DV
13074 I915_WRITE(PCH_DPLL(pll->id), 0);
13075 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13076 udelay(200);
13077}
13078
46edb027
DV
13079static char *ibx_pch_dpll_names[] = {
13080 "PCH DPLL A",
13081 "PCH DPLL B",
13082};
13083
7c74ade1 13084static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13085{
e7b903d2 13086 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13087 int i;
13088
7c74ade1 13089 dev_priv->num_shared_dpll = 2;
ee7b9f93 13090
e72f9fbf 13091 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13092 dev_priv->shared_dplls[i].id = i;
13093 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13094 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13095 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13096 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13097 dev_priv->shared_dplls[i].get_hw_state =
13098 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13099 }
13100}
13101
7c74ade1
DV
13102static void intel_shared_dpll_init(struct drm_device *dev)
13103{
e7b903d2 13104 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13105
9cd86933
DV
13106 if (HAS_DDI(dev))
13107 intel_ddi_pll_init(dev);
13108 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13109 ibx_pch_dpll_init(dev);
13110 else
13111 dev_priv->num_shared_dpll = 0;
13112
13113 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13114}
13115
1fc0a8f7
TU
13116/**
13117 * intel_wm_need_update - Check whether watermarks need updating
13118 * @plane: drm plane
13119 * @state: new plane state
13120 *
13121 * Check current plane state versus the new one to determine whether
13122 * watermarks need to be recalculated.
13123 *
13124 * Returns true or false.
13125 */
13126bool intel_wm_need_update(struct drm_plane *plane,
13127 struct drm_plane_state *state)
13128{
13129 /* Update watermarks on tiling changes. */
13130 if (!plane->state->fb || !state->fb ||
13131 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13132 plane->state->rotation != state->rotation)
13133 return true;
13134
13135 return false;
13136}
13137
6beb8c23
MR
13138/**
13139 * intel_prepare_plane_fb - Prepare fb for usage on plane
13140 * @plane: drm plane to prepare for
13141 * @fb: framebuffer to prepare for presentation
13142 *
13143 * Prepares a framebuffer for usage on a display plane. Generally this
13144 * involves pinning the underlying object and updating the frontbuffer tracking
13145 * bits. Some older platforms need special physical address handling for
13146 * cursor planes.
13147 *
13148 * Returns 0 on success, negative error code on failure.
13149 */
13150int
13151intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13152 struct drm_framebuffer *fb,
13153 const struct drm_plane_state *new_state)
465c120c
MR
13154{
13155 struct drm_device *dev = plane->dev;
6beb8c23
MR
13156 struct intel_plane *intel_plane = to_intel_plane(plane);
13157 enum pipe pipe = intel_plane->pipe;
13158 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13159 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13160 unsigned frontbuffer_bits = 0;
13161 int ret = 0;
465c120c 13162
ea2c67bb 13163 if (!obj)
465c120c
MR
13164 return 0;
13165
6beb8c23
MR
13166 switch (plane->type) {
13167 case DRM_PLANE_TYPE_PRIMARY:
13168 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13169 break;
13170 case DRM_PLANE_TYPE_CURSOR:
13171 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13172 break;
13173 case DRM_PLANE_TYPE_OVERLAY:
13174 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13175 break;
13176 }
465c120c 13177
6beb8c23 13178 mutex_lock(&dev->struct_mutex);
465c120c 13179
6beb8c23
MR
13180 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13181 INTEL_INFO(dev)->cursor_needs_physical) {
13182 int align = IS_I830(dev) ? 16 * 1024 : 256;
13183 ret = i915_gem_object_attach_phys(obj, align);
13184 if (ret)
13185 DRM_DEBUG_KMS("failed to attach phys object\n");
13186 } else {
82bc3b2d 13187 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13188 }
465c120c 13189
6beb8c23
MR
13190 if (ret == 0)
13191 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13192
4c34574f 13193 mutex_unlock(&dev->struct_mutex);
465c120c 13194
6beb8c23
MR
13195 return ret;
13196}
13197
38f3ce3a
MR
13198/**
13199 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13200 * @plane: drm plane to clean up for
13201 * @fb: old framebuffer that was on plane
13202 *
13203 * Cleans up a framebuffer that has just been removed from a plane.
13204 */
13205void
13206intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13207 struct drm_framebuffer *fb,
13208 const struct drm_plane_state *old_state)
38f3ce3a
MR
13209{
13210 struct drm_device *dev = plane->dev;
13211 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13212
13213 if (WARN_ON(!obj))
13214 return;
13215
13216 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13217 !INTEL_INFO(dev)->cursor_needs_physical) {
13218 mutex_lock(&dev->struct_mutex);
82bc3b2d 13219 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13220 mutex_unlock(&dev->struct_mutex);
13221 }
465c120c
MR
13222}
13223
6156a456
CK
13224int
13225skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13226{
13227 int max_scale;
13228 struct drm_device *dev;
13229 struct drm_i915_private *dev_priv;
13230 int crtc_clock, cdclk;
13231
13232 if (!intel_crtc || !crtc_state)
13233 return DRM_PLANE_HELPER_NO_SCALING;
13234
13235 dev = intel_crtc->base.dev;
13236 dev_priv = dev->dev_private;
13237 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13238 cdclk = dev_priv->display.get_display_clock_speed(dev);
13239
13240 if (!crtc_clock || !cdclk)
13241 return DRM_PLANE_HELPER_NO_SCALING;
13242
13243 /*
13244 * skl max scale is lower of:
13245 * close to 3 but not 3, -1 is for that purpose
13246 * or
13247 * cdclk/crtc_clock
13248 */
13249 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13250
13251 return max_scale;
13252}
13253
465c120c 13254static int
3c692a41
GP
13255intel_check_primary_plane(struct drm_plane *plane,
13256 struct intel_plane_state *state)
13257{
32b7eeec
MR
13258 struct drm_device *dev = plane->dev;
13259 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13260 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13261 struct intel_crtc *intel_crtc;
6156a456 13262 struct intel_crtc_state *crtc_state;
2b875c22 13263 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13264 struct drm_rect *dest = &state->dst;
13265 struct drm_rect *src = &state->src;
13266 const struct drm_rect *clip = &state->clip;
d8106366 13267 bool can_position = false;
6156a456
CK
13268 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13269 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13270 int ret;
13271
ea2c67bb
MR
13272 crtc = crtc ? crtc : plane->crtc;
13273 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13274 crtc_state = state->base.state ?
13275 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13276
6156a456
CK
13277 if (INTEL_INFO(dev)->gen >= 9) {
13278 min_scale = 1;
13279 max_scale = skl_max_scale(intel_crtc, crtc_state);
d8106366 13280 can_position = true;
6156a456 13281 }
d8106366 13282
c59cb179
MR
13283 ret = drm_plane_helper_check_update(plane, crtc, fb,
13284 src, dest, clip,
6156a456
CK
13285 min_scale,
13286 max_scale,
d8106366
SJ
13287 can_position, true,
13288 &state->visible);
c59cb179
MR
13289 if (ret)
13290 return ret;
465c120c 13291
32b7eeec
MR
13292 if (intel_crtc->active) {
13293 intel_crtc->atomic.wait_for_flips = true;
13294
13295 /*
13296 * FBC does not work on some platforms for rotated
13297 * planes, so disable it when rotation is not 0 and
13298 * update it when rotation is set back to 0.
13299 *
13300 * FIXME: This is redundant with the fbc update done in
13301 * the primary plane enable function except that that
13302 * one is done too late. We eventually need to unify
13303 * this.
13304 */
13305 if (intel_crtc->primary_enabled &&
13306 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13307 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13308 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13309 intel_crtc->atomic.disable_fbc = true;
13310 }
13311
13312 if (state->visible) {
13313 /*
13314 * BDW signals flip done immediately if the plane
13315 * is disabled, even if the plane enable is already
13316 * armed to occur at the next vblank :(
13317 */
13318 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13319 intel_crtc->atomic.wait_vblank = true;
13320 }
13321
13322 intel_crtc->atomic.fb_bits |=
13323 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13324
13325 intel_crtc->atomic.update_fbc = true;
0fda6568 13326
1fc0a8f7 13327 if (intel_wm_need_update(plane, &state->base))
0fda6568 13328 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13329 }
13330
6156a456
CK
13331 if (INTEL_INFO(dev)->gen >= 9) {
13332 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13333 to_intel_plane(plane), state, 0);
13334 if (ret)
13335 return ret;
13336 }
13337
14af293f
GP
13338 return 0;
13339}
13340
13341static void
13342intel_commit_primary_plane(struct drm_plane *plane,
13343 struct intel_plane_state *state)
13344{
2b875c22
MR
13345 struct drm_crtc *crtc = state->base.crtc;
13346 struct drm_framebuffer *fb = state->base.fb;
13347 struct drm_device *dev = plane->dev;
14af293f 13348 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13349 struct intel_crtc *intel_crtc;
14af293f
GP
13350 struct drm_rect *src = &state->src;
13351
ea2c67bb
MR
13352 crtc = crtc ? crtc : plane->crtc;
13353 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13354
13355 plane->fb = fb;
9dc806fc
MR
13356 crtc->x = src->x1 >> 16;
13357 crtc->y = src->y1 >> 16;
ccc759dc 13358
ccc759dc 13359 if (intel_crtc->active) {
ccc759dc 13360 if (state->visible) {
ccc759dc
GP
13361 /* FIXME: kill this fastboot hack */
13362 intel_update_pipe_size(intel_crtc);
465c120c 13363
ccc759dc 13364 intel_crtc->primary_enabled = true;
465c120c 13365
ccc759dc
GP
13366 dev_priv->display.update_primary_plane(crtc, plane->fb,
13367 crtc->x, crtc->y);
ccc759dc
GP
13368 } else {
13369 /*
13370 * If clipping results in a non-visible primary plane,
13371 * we'll disable the primary plane. Note that this is
13372 * a bit different than what happens if userspace
13373 * explicitly disables the plane by passing fb=0
13374 * because plane->fb still gets set and pinned.
13375 */
13376 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 13377 }
ccc759dc 13378 }
465c120c
MR
13379}
13380
32b7eeec 13381static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13382{
32b7eeec 13383 struct drm_device *dev = crtc->dev;
140fd38d 13384 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13386 struct intel_plane *intel_plane;
13387 struct drm_plane *p;
13388 unsigned fb_bits = 0;
13389
13390 /* Track fb's for any planes being disabled */
13391 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13392 intel_plane = to_intel_plane(p);
13393
13394 if (intel_crtc->atomic.disabled_planes &
13395 (1 << drm_plane_index(p))) {
13396 switch (p->type) {
13397 case DRM_PLANE_TYPE_PRIMARY:
13398 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13399 break;
13400 case DRM_PLANE_TYPE_CURSOR:
13401 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13402 break;
13403 case DRM_PLANE_TYPE_OVERLAY:
13404 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13405 break;
13406 }
3c692a41 13407
ea2c67bb
MR
13408 mutex_lock(&dev->struct_mutex);
13409 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13410 mutex_unlock(&dev->struct_mutex);
13411 }
13412 }
3c692a41 13413
32b7eeec
MR
13414 if (intel_crtc->atomic.wait_for_flips)
13415 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13416
32b7eeec
MR
13417 if (intel_crtc->atomic.disable_fbc)
13418 intel_fbc_disable(dev);
3c692a41 13419
32b7eeec
MR
13420 if (intel_crtc->atomic.pre_disable_primary)
13421 intel_pre_disable_primary(crtc);
3c692a41 13422
32b7eeec
MR
13423 if (intel_crtc->atomic.update_wm)
13424 intel_update_watermarks(crtc);
3c692a41 13425
32b7eeec 13426 intel_runtime_pm_get(dev_priv);
3c692a41 13427
c34c9ee4
MR
13428 /* Perform vblank evasion around commit operation */
13429 if (intel_crtc->active)
13430 intel_crtc->atomic.evade =
13431 intel_pipe_update_start(intel_crtc,
13432 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13433}
13434
13435static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13436{
13437 struct drm_device *dev = crtc->dev;
13438 struct drm_i915_private *dev_priv = dev->dev_private;
13439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13440 struct drm_plane *p;
13441
c34c9ee4
MR
13442 if (intel_crtc->atomic.evade)
13443 intel_pipe_update_end(intel_crtc,
13444 intel_crtc->atomic.start_vbl_count);
3c692a41 13445
140fd38d 13446 intel_runtime_pm_put(dev_priv);
3c692a41 13447
32b7eeec
MR
13448 if (intel_crtc->atomic.wait_vblank)
13449 intel_wait_for_vblank(dev, intel_crtc->pipe);
13450
13451 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13452
13453 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13454 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13455 intel_fbc_update(dev);
ccc759dc 13456 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13457 }
3c692a41 13458
32b7eeec
MR
13459 if (intel_crtc->atomic.post_enable_primary)
13460 intel_post_enable_primary(crtc);
3c692a41 13461
32b7eeec
MR
13462 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13463 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13464 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13465 false, false);
13466
13467 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13468}
13469
cf4c7c12 13470/**
4a3b8769
MR
13471 * intel_plane_destroy - destroy a plane
13472 * @plane: plane to destroy
cf4c7c12 13473 *
4a3b8769
MR
13474 * Common destruction function for all types of planes (primary, cursor,
13475 * sprite).
cf4c7c12 13476 */
4a3b8769 13477void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13478{
13479 struct intel_plane *intel_plane = to_intel_plane(plane);
13480 drm_plane_cleanup(plane);
13481 kfree(intel_plane);
13482}
13483
65a3fea0 13484const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13485 .update_plane = drm_atomic_helper_update_plane,
13486 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13487 .destroy = intel_plane_destroy,
c196e1d6 13488 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13489 .atomic_get_property = intel_plane_atomic_get_property,
13490 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13491 .atomic_duplicate_state = intel_plane_duplicate_state,
13492 .atomic_destroy_state = intel_plane_destroy_state,
13493
465c120c
MR
13494};
13495
13496static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13497 int pipe)
13498{
13499 struct intel_plane *primary;
8e7d688b 13500 struct intel_plane_state *state;
465c120c
MR
13501 const uint32_t *intel_primary_formats;
13502 int num_formats;
13503
13504 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13505 if (primary == NULL)
13506 return NULL;
13507
8e7d688b
MR
13508 state = intel_create_plane_state(&primary->base);
13509 if (!state) {
ea2c67bb
MR
13510 kfree(primary);
13511 return NULL;
13512 }
8e7d688b 13513 primary->base.state = &state->base;
ea2c67bb 13514
465c120c
MR
13515 primary->can_scale = false;
13516 primary->max_downscale = 1;
6156a456
CK
13517 if (INTEL_INFO(dev)->gen >= 9) {
13518 primary->can_scale = true;
13519 }
549e2bfb 13520 state->scaler_id = -1;
465c120c
MR
13521 primary->pipe = pipe;
13522 primary->plane = pipe;
c59cb179
MR
13523 primary->check_plane = intel_check_primary_plane;
13524 primary->commit_plane = intel_commit_primary_plane;
08e221fb 13525 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13526 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13527 primary->plane = !pipe;
13528
13529 if (INTEL_INFO(dev)->gen <= 3) {
13530 intel_primary_formats = intel_primary_formats_gen2;
13531 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13532 } else {
13533 intel_primary_formats = intel_primary_formats_gen4;
13534 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13535 }
13536
13537 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13538 &intel_plane_funcs,
465c120c
MR
13539 intel_primary_formats, num_formats,
13540 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13541
3b7a5119
SJ
13542 if (INTEL_INFO(dev)->gen >= 4)
13543 intel_create_rotation_property(dev, primary);
48404c1e 13544
ea2c67bb
MR
13545 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13546
465c120c
MR
13547 return &primary->base;
13548}
13549
3b7a5119
SJ
13550void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13551{
13552 if (!dev->mode_config.rotation_property) {
13553 unsigned long flags = BIT(DRM_ROTATE_0) |
13554 BIT(DRM_ROTATE_180);
13555
13556 if (INTEL_INFO(dev)->gen >= 9)
13557 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13558
13559 dev->mode_config.rotation_property =
13560 drm_mode_create_rotation_property(dev, flags);
13561 }
13562 if (dev->mode_config.rotation_property)
13563 drm_object_attach_property(&plane->base.base,
13564 dev->mode_config.rotation_property,
13565 plane->base.state->rotation);
13566}
13567
3d7d6510 13568static int
852e787c
GP
13569intel_check_cursor_plane(struct drm_plane *plane,
13570 struct intel_plane_state *state)
3d7d6510 13571{
2b875c22 13572 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13573 struct drm_device *dev = plane->dev;
2b875c22 13574 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13575 struct drm_rect *dest = &state->dst;
13576 struct drm_rect *src = &state->src;
13577 const struct drm_rect *clip = &state->clip;
757f9a3e 13578 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13579 struct intel_crtc *intel_crtc;
757f9a3e
GP
13580 unsigned stride;
13581 int ret;
3d7d6510 13582
ea2c67bb
MR
13583 crtc = crtc ? crtc : plane->crtc;
13584 intel_crtc = to_intel_crtc(crtc);
13585
757f9a3e 13586 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13587 src, dest, clip,
3d7d6510
MR
13588 DRM_PLANE_HELPER_NO_SCALING,
13589 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13590 true, true, &state->visible);
757f9a3e
GP
13591 if (ret)
13592 return ret;
13593
13594
13595 /* if we want to turn off the cursor ignore width and height */
13596 if (!obj)
32b7eeec 13597 goto finish;
757f9a3e 13598
757f9a3e 13599 /* Check for which cursor types we support */
ea2c67bb
MR
13600 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13601 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13602 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13603 return -EINVAL;
13604 }
13605
ea2c67bb
MR
13606 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13607 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13608 DRM_DEBUG_KMS("buffer is too small\n");
13609 return -ENOMEM;
13610 }
13611
3a656b54 13612 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13613 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13614 ret = -EINVAL;
13615 }
757f9a3e 13616
32b7eeec
MR
13617finish:
13618 if (intel_crtc->active) {
3749f463 13619 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13620 intel_crtc->atomic.update_wm = true;
13621
13622 intel_crtc->atomic.fb_bits |=
13623 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13624 }
13625
757f9a3e 13626 return ret;
852e787c 13627}
3d7d6510 13628
f4a2cf29 13629static void
852e787c
GP
13630intel_commit_cursor_plane(struct drm_plane *plane,
13631 struct intel_plane_state *state)
13632{
2b875c22 13633 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13634 struct drm_device *dev = plane->dev;
13635 struct intel_crtc *intel_crtc;
2b875c22 13636 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13637 uint32_t addr;
852e787c 13638
ea2c67bb
MR
13639 crtc = crtc ? crtc : plane->crtc;
13640 intel_crtc = to_intel_crtc(crtc);
13641
2b875c22 13642 plane->fb = state->base.fb;
ea2c67bb
MR
13643 crtc->cursor_x = state->base.crtc_x;
13644 crtc->cursor_y = state->base.crtc_y;
13645
a912f12f
GP
13646 if (intel_crtc->cursor_bo == obj)
13647 goto update;
4ed91096 13648
f4a2cf29 13649 if (!obj)
a912f12f 13650 addr = 0;
f4a2cf29 13651 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13652 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13653 else
a912f12f 13654 addr = obj->phys_handle->busaddr;
852e787c 13655
a912f12f
GP
13656 intel_crtc->cursor_addr = addr;
13657 intel_crtc->cursor_bo = obj;
13658update:
852e787c 13659
32b7eeec 13660 if (intel_crtc->active)
a912f12f 13661 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13662}
13663
3d7d6510
MR
13664static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13665 int pipe)
13666{
13667 struct intel_plane *cursor;
8e7d688b 13668 struct intel_plane_state *state;
3d7d6510
MR
13669
13670 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13671 if (cursor == NULL)
13672 return NULL;
13673
8e7d688b
MR
13674 state = intel_create_plane_state(&cursor->base);
13675 if (!state) {
ea2c67bb
MR
13676 kfree(cursor);
13677 return NULL;
13678 }
8e7d688b 13679 cursor->base.state = &state->base;
ea2c67bb 13680
3d7d6510
MR
13681 cursor->can_scale = false;
13682 cursor->max_downscale = 1;
13683 cursor->pipe = pipe;
13684 cursor->plane = pipe;
549e2bfb 13685 state->scaler_id = -1;
c59cb179
MR
13686 cursor->check_plane = intel_check_cursor_plane;
13687 cursor->commit_plane = intel_commit_cursor_plane;
3d7d6510
MR
13688
13689 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13690 &intel_plane_funcs,
3d7d6510
MR
13691 intel_cursor_formats,
13692 ARRAY_SIZE(intel_cursor_formats),
13693 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13694
13695 if (INTEL_INFO(dev)->gen >= 4) {
13696 if (!dev->mode_config.rotation_property)
13697 dev->mode_config.rotation_property =
13698 drm_mode_create_rotation_property(dev,
13699 BIT(DRM_ROTATE_0) |
13700 BIT(DRM_ROTATE_180));
13701 if (dev->mode_config.rotation_property)
13702 drm_object_attach_property(&cursor->base.base,
13703 dev->mode_config.rotation_property,
8e7d688b 13704 state->base.rotation);
4398ad45
VS
13705 }
13706
ea2c67bb
MR
13707 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13708
3d7d6510
MR
13709 return &cursor->base;
13710}
13711
549e2bfb
CK
13712static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13713 struct intel_crtc_state *crtc_state)
13714{
13715 int i;
13716 struct intel_scaler *intel_scaler;
13717 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13718
13719 for (i = 0; i < intel_crtc->num_scalers; i++) {
13720 intel_scaler = &scaler_state->scalers[i];
13721 intel_scaler->in_use = 0;
13722 intel_scaler->id = i;
13723
13724 intel_scaler->mode = PS_SCALER_MODE_DYN;
13725 }
13726
13727 scaler_state->scaler_id = -1;
13728}
13729
b358d0a6 13730static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13731{
fbee40df 13732 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13733 struct intel_crtc *intel_crtc;
f5de6e07 13734 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13735 struct drm_plane *primary = NULL;
13736 struct drm_plane *cursor = NULL;
465c120c 13737 int i, ret;
79e53945 13738
955382f3 13739 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13740 if (intel_crtc == NULL)
13741 return;
13742
f5de6e07
ACO
13743 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13744 if (!crtc_state)
13745 goto fail;
13746 intel_crtc_set_state(intel_crtc, crtc_state);
07878248 13747 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13748
549e2bfb
CK
13749 /* initialize shared scalers */
13750 if (INTEL_INFO(dev)->gen >= 9) {
13751 if (pipe == PIPE_C)
13752 intel_crtc->num_scalers = 1;
13753 else
13754 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13755
13756 skl_init_scalers(dev, intel_crtc, crtc_state);
13757 }
13758
465c120c 13759 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13760 if (!primary)
13761 goto fail;
13762
13763 cursor = intel_cursor_plane_create(dev, pipe);
13764 if (!cursor)
13765 goto fail;
13766
465c120c 13767 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
13768 cursor, &intel_crtc_funcs);
13769 if (ret)
13770 goto fail;
79e53945
JB
13771
13772 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
13773 for (i = 0; i < 256; i++) {
13774 intel_crtc->lut_r[i] = i;
13775 intel_crtc->lut_g[i] = i;
13776 intel_crtc->lut_b[i] = i;
13777 }
13778
1f1c2e24
VS
13779 /*
13780 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13781 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13782 */
80824003
JB
13783 intel_crtc->pipe = pipe;
13784 intel_crtc->plane = pipe;
3a77c4c4 13785 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13786 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13787 intel_crtc->plane = !pipe;
80824003
JB
13788 }
13789
4b0e333e
CW
13790 intel_crtc->cursor_base = ~0;
13791 intel_crtc->cursor_cntl = ~0;
dc41c154 13792 intel_crtc->cursor_size = ~0;
8d7849db 13793
22fd0fab
JB
13794 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13795 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13796 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13797 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13798
9362c7c5
ACO
13799 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13800
79e53945 13801 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
13802
13803 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13804 return;
13805
13806fail:
13807 if (primary)
13808 drm_plane_cleanup(primary);
13809 if (cursor)
13810 drm_plane_cleanup(cursor);
f5de6e07 13811 kfree(crtc_state);
3d7d6510 13812 kfree(intel_crtc);
79e53945
JB
13813}
13814
752aa88a
JB
13815enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13816{
13817 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13818 struct drm_device *dev = connector->base.dev;
752aa88a 13819
51fd371b 13820 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13821
d3babd3f 13822 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13823 return INVALID_PIPE;
13824
13825 return to_intel_crtc(encoder->crtc)->pipe;
13826}
13827
08d7b3d1 13828int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13829 struct drm_file *file)
08d7b3d1 13830{
08d7b3d1 13831 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13832 struct drm_crtc *drmmode_crtc;
c05422d5 13833 struct intel_crtc *crtc;
08d7b3d1 13834
7707e653 13835 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13836
7707e653 13837 if (!drmmode_crtc) {
08d7b3d1 13838 DRM_ERROR("no such CRTC id\n");
3f2c2057 13839 return -ENOENT;
08d7b3d1
CW
13840 }
13841
7707e653 13842 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13843 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13844
c05422d5 13845 return 0;
08d7b3d1
CW
13846}
13847
66a9278e 13848static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13849{
66a9278e
DV
13850 struct drm_device *dev = encoder->base.dev;
13851 struct intel_encoder *source_encoder;
79e53945 13852 int index_mask = 0;
79e53945
JB
13853 int entry = 0;
13854
b2784e15 13855 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13856 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13857 index_mask |= (1 << entry);
13858
79e53945
JB
13859 entry++;
13860 }
4ef69c7a 13861
79e53945
JB
13862 return index_mask;
13863}
13864
4d302442
CW
13865static bool has_edp_a(struct drm_device *dev)
13866{
13867 struct drm_i915_private *dev_priv = dev->dev_private;
13868
13869 if (!IS_MOBILE(dev))
13870 return false;
13871
13872 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13873 return false;
13874
e3589908 13875 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13876 return false;
13877
13878 return true;
13879}
13880
84b4e042
JB
13881static bool intel_crt_present(struct drm_device *dev)
13882{
13883 struct drm_i915_private *dev_priv = dev->dev_private;
13884
884497ed
DL
13885 if (INTEL_INFO(dev)->gen >= 9)
13886 return false;
13887
cf404ce4 13888 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13889 return false;
13890
13891 if (IS_CHERRYVIEW(dev))
13892 return false;
13893
13894 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13895 return false;
13896
13897 return true;
13898}
13899
79e53945
JB
13900static void intel_setup_outputs(struct drm_device *dev)
13901{
725e30ad 13902 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13903 struct intel_encoder *encoder;
cb0953d7 13904 bool dpd_is_edp = false;
79e53945 13905
c9093354 13906 intel_lvds_init(dev);
79e53945 13907
84b4e042 13908 if (intel_crt_present(dev))
79935fca 13909 intel_crt_init(dev);
cb0953d7 13910
c776eb2e
VK
13911 if (IS_BROXTON(dev)) {
13912 /*
13913 * FIXME: Broxton doesn't support port detection via the
13914 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13915 * detect the ports.
13916 */
13917 intel_ddi_init(dev, PORT_A);
13918 intel_ddi_init(dev, PORT_B);
13919 intel_ddi_init(dev, PORT_C);
13920 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13921 int found;
13922
de31facd
JB
13923 /*
13924 * Haswell uses DDI functions to detect digital outputs.
13925 * On SKL pre-D0 the strap isn't connected, so we assume
13926 * it's there.
13927 */
0e72a5b5 13928 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
13929 /* WaIgnoreDDIAStrap: skl */
13930 if (found ||
13931 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
13932 intel_ddi_init(dev, PORT_A);
13933
13934 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13935 * register */
13936 found = I915_READ(SFUSE_STRAP);
13937
13938 if (found & SFUSE_STRAP_DDIB_DETECTED)
13939 intel_ddi_init(dev, PORT_B);
13940 if (found & SFUSE_STRAP_DDIC_DETECTED)
13941 intel_ddi_init(dev, PORT_C);
13942 if (found & SFUSE_STRAP_DDID_DETECTED)
13943 intel_ddi_init(dev, PORT_D);
13944 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 13945 int found;
5d8a7752 13946 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
13947
13948 if (has_edp_a(dev))
13949 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 13950
dc0fa718 13951 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13952 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 13953 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 13954 if (!found)
e2debe91 13955 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 13956 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 13957 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
13958 }
13959
dc0fa718 13960 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 13961 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 13962
dc0fa718 13963 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 13964 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 13965
5eb08b69 13966 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 13967 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 13968
270b3042 13969 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 13970 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 13971 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
13972 /*
13973 * The DP_DETECTED bit is the latched state of the DDC
13974 * SDA pin at boot. However since eDP doesn't require DDC
13975 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13976 * eDP ports may have been muxed to an alternate function.
13977 * Thus we can't rely on the DP_DETECTED bit alone to detect
13978 * eDP ports. Consult the VBT as well as DP_DETECTED to
13979 * detect eDP ports.
13980 */
d2182a66
VS
13981 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13982 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
13983 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13984 PORT_B);
e17ac6db
VS
13985 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13986 intel_dp_is_edp(dev, PORT_B))
13987 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 13988
d2182a66
VS
13989 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13990 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
13991 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13992 PORT_C);
e17ac6db
VS
13993 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13994 intel_dp_is_edp(dev, PORT_C))
13995 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 13996
9418c1f1 13997 if (IS_CHERRYVIEW(dev)) {
e17ac6db 13998 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
13999 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14000 PORT_D);
e17ac6db
VS
14001 /* eDP not supported on port D, so don't check VBT */
14002 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14003 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14004 }
14005
3cfca973 14006 intel_dsi_init(dev);
103a196f 14007 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14008 bool found = false;
7d57382e 14009
e2debe91 14010 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14011 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14012 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14013 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14014 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14015 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14016 }
27185ae1 14017
e7281eab 14018 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14019 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14020 }
13520b05
KH
14021
14022 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14023
e2debe91 14024 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14025 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14026 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14027 }
27185ae1 14028
e2debe91 14029 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14030
b01f2c3a
JB
14031 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14032 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14033 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14034 }
e7281eab 14035 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14036 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14037 }
27185ae1 14038
b01f2c3a 14039 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14040 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14041 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14042 } else if (IS_GEN2(dev))
79e53945
JB
14043 intel_dvo_init(dev);
14044
103a196f 14045 if (SUPPORTS_TV(dev))
79e53945
JB
14046 intel_tv_init(dev);
14047
0bc12bcb 14048 intel_psr_init(dev);
7c8f8a70 14049
b2784e15 14050 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14051 encoder->base.possible_crtcs = encoder->crtc_mask;
14052 encoder->base.possible_clones =
66a9278e 14053 intel_encoder_clones(encoder);
79e53945 14054 }
47356eb6 14055
dde86e2d 14056 intel_init_pch_refclk(dev);
270b3042
DV
14057
14058 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14059}
14060
14061static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14062{
60a5ca01 14063 struct drm_device *dev = fb->dev;
79e53945 14064 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14065
ef2d633e 14066 drm_framebuffer_cleanup(fb);
60a5ca01 14067 mutex_lock(&dev->struct_mutex);
ef2d633e 14068 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14069 drm_gem_object_unreference(&intel_fb->obj->base);
14070 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14071 kfree(intel_fb);
14072}
14073
14074static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14075 struct drm_file *file,
79e53945
JB
14076 unsigned int *handle)
14077{
14078 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14079 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14080
05394f39 14081 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14082}
14083
14084static const struct drm_framebuffer_funcs intel_fb_funcs = {
14085 .destroy = intel_user_framebuffer_destroy,
14086 .create_handle = intel_user_framebuffer_create_handle,
14087};
14088
b321803d
DL
14089static
14090u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14091 uint32_t pixel_format)
14092{
14093 u32 gen = INTEL_INFO(dev)->gen;
14094
14095 if (gen >= 9) {
14096 /* "The stride in bytes must not exceed the of the size of 8K
14097 * pixels and 32K bytes."
14098 */
14099 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14100 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14101 return 32*1024;
14102 } else if (gen >= 4) {
14103 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14104 return 16*1024;
14105 else
14106 return 32*1024;
14107 } else if (gen >= 3) {
14108 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14109 return 8*1024;
14110 else
14111 return 16*1024;
14112 } else {
14113 /* XXX DSPC is limited to 4k tiled */
14114 return 8*1024;
14115 }
14116}
14117
b5ea642a
DV
14118static int intel_framebuffer_init(struct drm_device *dev,
14119 struct intel_framebuffer *intel_fb,
14120 struct drm_mode_fb_cmd2 *mode_cmd,
14121 struct drm_i915_gem_object *obj)
79e53945 14122{
6761dd31 14123 unsigned int aligned_height;
79e53945 14124 int ret;
b321803d 14125 u32 pitch_limit, stride_alignment;
79e53945 14126
dd4916c5
DV
14127 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14128
2a80eada
DV
14129 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14130 /* Enforce that fb modifier and tiling mode match, but only for
14131 * X-tiled. This is needed for FBC. */
14132 if (!!(obj->tiling_mode == I915_TILING_X) !=
14133 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14134 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14135 return -EINVAL;
14136 }
14137 } else {
14138 if (obj->tiling_mode == I915_TILING_X)
14139 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14140 else if (obj->tiling_mode == I915_TILING_Y) {
14141 DRM_DEBUG("No Y tiling for legacy addfb\n");
14142 return -EINVAL;
14143 }
14144 }
14145
9a8f0a12
TU
14146 /* Passed in modifier sanity checking. */
14147 switch (mode_cmd->modifier[0]) {
14148 case I915_FORMAT_MOD_Y_TILED:
14149 case I915_FORMAT_MOD_Yf_TILED:
14150 if (INTEL_INFO(dev)->gen < 9) {
14151 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14152 mode_cmd->modifier[0]);
14153 return -EINVAL;
14154 }
14155 case DRM_FORMAT_MOD_NONE:
14156 case I915_FORMAT_MOD_X_TILED:
14157 break;
14158 default:
c0f40428
JB
14159 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14160 mode_cmd->modifier[0]);
57cd6508 14161 return -EINVAL;
c16ed4be 14162 }
57cd6508 14163
b321803d
DL
14164 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14165 mode_cmd->pixel_format);
14166 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14167 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14168 mode_cmd->pitches[0], stride_alignment);
57cd6508 14169 return -EINVAL;
c16ed4be 14170 }
57cd6508 14171
b321803d
DL
14172 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14173 mode_cmd->pixel_format);
a35cdaa0 14174 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14175 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14176 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14177 "tiled" : "linear",
a35cdaa0 14178 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14179 return -EINVAL;
c16ed4be 14180 }
5d7bd705 14181
2a80eada 14182 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14183 mode_cmd->pitches[0] != obj->stride) {
14184 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14185 mode_cmd->pitches[0], obj->stride);
5d7bd705 14186 return -EINVAL;
c16ed4be 14187 }
5d7bd705 14188
57779d06 14189 /* Reject formats not supported by any plane early. */
308e5bcb 14190 switch (mode_cmd->pixel_format) {
57779d06 14191 case DRM_FORMAT_C8:
04b3924d
VS
14192 case DRM_FORMAT_RGB565:
14193 case DRM_FORMAT_XRGB8888:
14194 case DRM_FORMAT_ARGB8888:
57779d06
VS
14195 break;
14196 case DRM_FORMAT_XRGB1555:
14197 case DRM_FORMAT_ARGB1555:
c16ed4be 14198 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14199 DRM_DEBUG("unsupported pixel format: %s\n",
14200 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14201 return -EINVAL;
c16ed4be 14202 }
57779d06
VS
14203 break;
14204 case DRM_FORMAT_XBGR8888:
14205 case DRM_FORMAT_ABGR8888:
04b3924d
VS
14206 case DRM_FORMAT_XRGB2101010:
14207 case DRM_FORMAT_ARGB2101010:
57779d06
VS
14208 case DRM_FORMAT_XBGR2101010:
14209 case DRM_FORMAT_ABGR2101010:
c16ed4be 14210 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14211 DRM_DEBUG("unsupported pixel format: %s\n",
14212 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14213 return -EINVAL;
c16ed4be 14214 }
b5626747 14215 break;
04b3924d
VS
14216 case DRM_FORMAT_YUYV:
14217 case DRM_FORMAT_UYVY:
14218 case DRM_FORMAT_YVYU:
14219 case DRM_FORMAT_VYUY:
c16ed4be 14220 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14221 DRM_DEBUG("unsupported pixel format: %s\n",
14222 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14223 return -EINVAL;
c16ed4be 14224 }
57cd6508
CW
14225 break;
14226 default:
4ee62c76
VS
14227 DRM_DEBUG("unsupported pixel format: %s\n",
14228 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14229 return -EINVAL;
14230 }
14231
90f9a336
VS
14232 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14233 if (mode_cmd->offsets[0] != 0)
14234 return -EINVAL;
14235
ec2c981e 14236 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14237 mode_cmd->pixel_format,
14238 mode_cmd->modifier[0]);
53155c0a
DV
14239 /* FIXME drm helper for size checks (especially planar formats)? */
14240 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14241 return -EINVAL;
14242
c7d73f6a
DV
14243 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14244 intel_fb->obj = obj;
80075d49 14245 intel_fb->obj->framebuffer_references++;
c7d73f6a 14246
79e53945
JB
14247 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14248 if (ret) {
14249 DRM_ERROR("framebuffer init failed %d\n", ret);
14250 return ret;
14251 }
14252
79e53945
JB
14253 return 0;
14254}
14255
79e53945
JB
14256static struct drm_framebuffer *
14257intel_user_framebuffer_create(struct drm_device *dev,
14258 struct drm_file *filp,
308e5bcb 14259 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14260{
05394f39 14261 struct drm_i915_gem_object *obj;
79e53945 14262
308e5bcb
JB
14263 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14264 mode_cmd->handles[0]));
c8725226 14265 if (&obj->base == NULL)
cce13ff7 14266 return ERR_PTR(-ENOENT);
79e53945 14267
d2dff872 14268 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14269}
14270
4520f53a 14271#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14272static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14273{
14274}
14275#endif
14276
79e53945 14277static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14278 .fb_create = intel_user_framebuffer_create,
0632fef6 14279 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14280 .atomic_check = intel_atomic_check,
14281 .atomic_commit = intel_atomic_commit,
79e53945
JB
14282};
14283
e70236a8
JB
14284/* Set up chip specific display functions */
14285static void intel_init_display(struct drm_device *dev)
14286{
14287 struct drm_i915_private *dev_priv = dev->dev_private;
14288
ee9300bb
DV
14289 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14290 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14291 else if (IS_CHERRYVIEW(dev))
14292 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14293 else if (IS_VALLEYVIEW(dev))
14294 dev_priv->display.find_dpll = vlv_find_best_dpll;
14295 else if (IS_PINEVIEW(dev))
14296 dev_priv->display.find_dpll = pnv_find_best_dpll;
14297 else
14298 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14299
bc8d7dff
DL
14300 if (INTEL_INFO(dev)->gen >= 9) {
14301 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14302 dev_priv->display.get_initial_plane_config =
14303 skylake_get_initial_plane_config;
bc8d7dff
DL
14304 dev_priv->display.crtc_compute_clock =
14305 haswell_crtc_compute_clock;
14306 dev_priv->display.crtc_enable = haswell_crtc_enable;
14307 dev_priv->display.crtc_disable = haswell_crtc_disable;
14308 dev_priv->display.off = ironlake_crtc_off;
14309 dev_priv->display.update_primary_plane =
14310 skylake_update_primary_plane;
14311 } else if (HAS_DDI(dev)) {
0e8ffe1b 14312 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14313 dev_priv->display.get_initial_plane_config =
14314 ironlake_get_initial_plane_config;
797d0259
ACO
14315 dev_priv->display.crtc_compute_clock =
14316 haswell_crtc_compute_clock;
4f771f10
PZ
14317 dev_priv->display.crtc_enable = haswell_crtc_enable;
14318 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 14319 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
14320 dev_priv->display.update_primary_plane =
14321 ironlake_update_primary_plane;
09b4ddf9 14322 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14323 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14324 dev_priv->display.get_initial_plane_config =
14325 ironlake_get_initial_plane_config;
3fb37703
ACO
14326 dev_priv->display.crtc_compute_clock =
14327 ironlake_crtc_compute_clock;
76e5a89c
DV
14328 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14329 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 14330 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
14331 dev_priv->display.update_primary_plane =
14332 ironlake_update_primary_plane;
89b667f8
JB
14333 } else if (IS_VALLEYVIEW(dev)) {
14334 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14335 dev_priv->display.get_initial_plane_config =
14336 i9xx_get_initial_plane_config;
d6dfee7a 14337 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14338 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14339 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14340 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14341 dev_priv->display.update_primary_plane =
14342 i9xx_update_primary_plane;
f564048e 14343 } else {
0e8ffe1b 14344 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14345 dev_priv->display.get_initial_plane_config =
14346 i9xx_get_initial_plane_config;
d6dfee7a 14347 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14348 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14349 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 14350 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14351 dev_priv->display.update_primary_plane =
14352 i9xx_update_primary_plane;
f564048e 14353 }
e70236a8 14354
e70236a8 14355 /* Returns the core display clock speed */
1652d19e
VS
14356 if (IS_SKYLAKE(dev))
14357 dev_priv->display.get_display_clock_speed =
14358 skylake_get_display_clock_speed;
14359 else if (IS_BROADWELL(dev))
14360 dev_priv->display.get_display_clock_speed =
14361 broadwell_get_display_clock_speed;
14362 else if (IS_HASWELL(dev))
14363 dev_priv->display.get_display_clock_speed =
14364 haswell_get_display_clock_speed;
14365 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14366 dev_priv->display.get_display_clock_speed =
14367 valleyview_get_display_clock_speed;
b37a6434
VS
14368 else if (IS_GEN5(dev))
14369 dev_priv->display.get_display_clock_speed =
14370 ilk_get_display_clock_speed;
a7c66cd8
VS
14371 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14372 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
14373 dev_priv->display.get_display_clock_speed =
14374 i945_get_display_clock_speed;
14375 else if (IS_I915G(dev))
14376 dev_priv->display.get_display_clock_speed =
14377 i915_get_display_clock_speed;
257a7ffc 14378 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14379 dev_priv->display.get_display_clock_speed =
14380 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14381 else if (IS_PINEVIEW(dev))
14382 dev_priv->display.get_display_clock_speed =
14383 pnv_get_display_clock_speed;
e70236a8
JB
14384 else if (IS_I915GM(dev))
14385 dev_priv->display.get_display_clock_speed =
14386 i915gm_get_display_clock_speed;
14387 else if (IS_I865G(dev))
14388 dev_priv->display.get_display_clock_speed =
14389 i865_get_display_clock_speed;
f0f8a9ce 14390 else if (IS_I85X(dev))
e70236a8
JB
14391 dev_priv->display.get_display_clock_speed =
14392 i855_get_display_clock_speed;
14393 else /* 852, 830 */
14394 dev_priv->display.get_display_clock_speed =
14395 i830_get_display_clock_speed;
14396
7c10a2b5 14397 if (IS_GEN5(dev)) {
3bb11b53 14398 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14399 } else if (IS_GEN6(dev)) {
14400 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14401 } else if (IS_IVYBRIDGE(dev)) {
14402 /* FIXME: detect B0+ stepping and use auto training */
14403 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14404 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14405 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
14406 } else if (IS_VALLEYVIEW(dev)) {
14407 dev_priv->display.modeset_global_resources =
14408 valleyview_modeset_global_resources;
f8437dd1
VK
14409 } else if (IS_BROXTON(dev)) {
14410 dev_priv->display.modeset_global_resources =
14411 broxton_modeset_global_resources;
e70236a8 14412 }
8c9f3aaf 14413
8c9f3aaf
JB
14414 switch (INTEL_INFO(dev)->gen) {
14415 case 2:
14416 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14417 break;
14418
14419 case 3:
14420 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14421 break;
14422
14423 case 4:
14424 case 5:
14425 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14426 break;
14427
14428 case 6:
14429 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14430 break;
7c9017e5 14431 case 7:
4e0bbc31 14432 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14433 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14434 break;
830c81db 14435 case 9:
ba343e02
TU
14436 /* Drop through - unsupported since execlist only. */
14437 default:
14438 /* Default just returns -ENODEV to indicate unsupported */
14439 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14440 }
7bd688cd
JN
14441
14442 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14443
14444 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14445}
14446
b690e96c
JB
14447/*
14448 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14449 * resume, or other times. This quirk makes sure that's the case for
14450 * affected systems.
14451 */
0206e353 14452static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14453{
14454 struct drm_i915_private *dev_priv = dev->dev_private;
14455
14456 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14457 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14458}
14459
b6b5d049
VS
14460static void quirk_pipeb_force(struct drm_device *dev)
14461{
14462 struct drm_i915_private *dev_priv = dev->dev_private;
14463
14464 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14465 DRM_INFO("applying pipe b force quirk\n");
14466}
14467
435793df
KP
14468/*
14469 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14470 */
14471static void quirk_ssc_force_disable(struct drm_device *dev)
14472{
14473 struct drm_i915_private *dev_priv = dev->dev_private;
14474 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14475 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14476}
14477
4dca20ef 14478/*
5a15ab5b
CE
14479 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14480 * brightness value
4dca20ef
CE
14481 */
14482static void quirk_invert_brightness(struct drm_device *dev)
14483{
14484 struct drm_i915_private *dev_priv = dev->dev_private;
14485 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14486 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14487}
14488
9c72cc6f
SD
14489/* Some VBT's incorrectly indicate no backlight is present */
14490static void quirk_backlight_present(struct drm_device *dev)
14491{
14492 struct drm_i915_private *dev_priv = dev->dev_private;
14493 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14494 DRM_INFO("applying backlight present quirk\n");
14495}
14496
b690e96c
JB
14497struct intel_quirk {
14498 int device;
14499 int subsystem_vendor;
14500 int subsystem_device;
14501 void (*hook)(struct drm_device *dev);
14502};
14503
5f85f176
EE
14504/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14505struct intel_dmi_quirk {
14506 void (*hook)(struct drm_device *dev);
14507 const struct dmi_system_id (*dmi_id_list)[];
14508};
14509
14510static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14511{
14512 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14513 return 1;
14514}
14515
14516static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14517 {
14518 .dmi_id_list = &(const struct dmi_system_id[]) {
14519 {
14520 .callback = intel_dmi_reverse_brightness,
14521 .ident = "NCR Corporation",
14522 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14523 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14524 },
14525 },
14526 { } /* terminating entry */
14527 },
14528 .hook = quirk_invert_brightness,
14529 },
14530};
14531
c43b5634 14532static struct intel_quirk intel_quirks[] = {
b690e96c 14533 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 14534 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 14535
b690e96c
JB
14536 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14537 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14538
b690e96c
JB
14539 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14540 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14541
5f080c0f
VS
14542 /* 830 needs to leave pipe A & dpll A up */
14543 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14544
b6b5d049
VS
14545 /* 830 needs to leave pipe B & dpll B up */
14546 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14547
435793df
KP
14548 /* Lenovo U160 cannot use SSC on LVDS */
14549 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14550
14551 /* Sony Vaio Y cannot use SSC on LVDS */
14552 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14553
be505f64
AH
14554 /* Acer Aspire 5734Z must invert backlight brightness */
14555 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14556
14557 /* Acer/eMachines G725 */
14558 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14559
14560 /* Acer/eMachines e725 */
14561 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14562
14563 /* Acer/Packard Bell NCL20 */
14564 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14565
14566 /* Acer Aspire 4736Z */
14567 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14568
14569 /* Acer Aspire 5336 */
14570 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14571
14572 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14573 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14574
dfb3d47b
SD
14575 /* Acer C720 Chromebook (Core i3 4005U) */
14576 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14577
b2a9601c 14578 /* Apple Macbook 2,1 (Core 2 T7400) */
14579 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14580
d4967d8c
SD
14581 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14582 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14583
14584 /* HP Chromebook 14 (Celeron 2955U) */
14585 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14586
14587 /* Dell Chromebook 11 */
14588 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14589};
14590
14591static void intel_init_quirks(struct drm_device *dev)
14592{
14593 struct pci_dev *d = dev->pdev;
14594 int i;
14595
14596 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14597 struct intel_quirk *q = &intel_quirks[i];
14598
14599 if (d->device == q->device &&
14600 (d->subsystem_vendor == q->subsystem_vendor ||
14601 q->subsystem_vendor == PCI_ANY_ID) &&
14602 (d->subsystem_device == q->subsystem_device ||
14603 q->subsystem_device == PCI_ANY_ID))
14604 q->hook(dev);
14605 }
5f85f176
EE
14606 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14607 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14608 intel_dmi_quirks[i].hook(dev);
14609 }
b690e96c
JB
14610}
14611
9cce37f4
JB
14612/* Disable the VGA plane that we never use */
14613static void i915_disable_vga(struct drm_device *dev)
14614{
14615 struct drm_i915_private *dev_priv = dev->dev_private;
14616 u8 sr1;
766aa1c4 14617 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14618
2b37c616 14619 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14620 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14621 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14622 sr1 = inb(VGA_SR_DATA);
14623 outb(sr1 | 1<<5, VGA_SR_DATA);
14624 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14625 udelay(300);
14626
01f5a626 14627 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14628 POSTING_READ(vga_reg);
14629}
14630
f817586c
DV
14631void intel_modeset_init_hw(struct drm_device *dev)
14632{
a8f78b58
ED
14633 intel_prepare_ddi(dev);
14634
f8bf63fd
VS
14635 if (IS_VALLEYVIEW(dev))
14636 vlv_update_cdclk(dev);
14637
f817586c
DV
14638 intel_init_clock_gating(dev);
14639
8090c6b9 14640 intel_enable_gt_powersave(dev);
f817586c
DV
14641}
14642
79e53945
JB
14643void intel_modeset_init(struct drm_device *dev)
14644{
652c393a 14645 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14646 int sprite, ret;
8cc87b75 14647 enum pipe pipe;
46f297fb 14648 struct intel_crtc *crtc;
79e53945
JB
14649
14650 drm_mode_config_init(dev);
14651
14652 dev->mode_config.min_width = 0;
14653 dev->mode_config.min_height = 0;
14654
019d96cb
DA
14655 dev->mode_config.preferred_depth = 24;
14656 dev->mode_config.prefer_shadow = 1;
14657
25bab385
TU
14658 dev->mode_config.allow_fb_modifiers = true;
14659
e6ecefaa 14660 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14661
b690e96c
JB
14662 intel_init_quirks(dev);
14663
1fa61106
ED
14664 intel_init_pm(dev);
14665
e3c74757
BW
14666 if (INTEL_INFO(dev)->num_pipes == 0)
14667 return;
14668
e70236a8 14669 intel_init_display(dev);
7c10a2b5 14670 intel_init_audio(dev);
e70236a8 14671
a6c45cf0
CW
14672 if (IS_GEN2(dev)) {
14673 dev->mode_config.max_width = 2048;
14674 dev->mode_config.max_height = 2048;
14675 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14676 dev->mode_config.max_width = 4096;
14677 dev->mode_config.max_height = 4096;
79e53945 14678 } else {
a6c45cf0
CW
14679 dev->mode_config.max_width = 8192;
14680 dev->mode_config.max_height = 8192;
79e53945 14681 }
068be561 14682
dc41c154
VS
14683 if (IS_845G(dev) || IS_I865G(dev)) {
14684 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14685 dev->mode_config.cursor_height = 1023;
14686 } else if (IS_GEN2(dev)) {
068be561
DL
14687 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14688 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14689 } else {
14690 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14691 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14692 }
14693
5d4545ae 14694 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14695
28c97730 14696 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14697 INTEL_INFO(dev)->num_pipes,
14698 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14699
055e393f 14700 for_each_pipe(dev_priv, pipe) {
8cc87b75 14701 intel_crtc_init(dev, pipe);
3bdcfc0c 14702 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14703 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14704 if (ret)
06da8da2 14705 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14706 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14707 }
79e53945
JB
14708 }
14709
f42bb70d
JB
14710 intel_init_dpio(dev);
14711
e72f9fbf 14712 intel_shared_dpll_init(dev);
ee7b9f93 14713
9cce37f4
JB
14714 /* Just disable it once at startup */
14715 i915_disable_vga(dev);
79e53945 14716 intel_setup_outputs(dev);
11be49eb
CW
14717
14718 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14719 intel_fbc_disable(dev);
fa9fa083 14720
6e9f798d 14721 drm_modeset_lock_all(dev);
fa9fa083 14722 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14723 drm_modeset_unlock_all(dev);
46f297fb 14724
d3fcc808 14725 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14726 if (!crtc->active)
14727 continue;
14728
46f297fb 14729 /*
46f297fb
JB
14730 * Note that reserving the BIOS fb up front prevents us
14731 * from stuffing other stolen allocations like the ring
14732 * on top. This prevents some ugliness at boot time, and
14733 * can even allow for smooth boot transitions if the BIOS
14734 * fb is large enough for the active pipe configuration.
14735 */
5724dbd1
DL
14736 if (dev_priv->display.get_initial_plane_config) {
14737 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14738 &crtc->plane_config);
14739 /*
14740 * If the fb is shared between multiple heads, we'll
14741 * just get the first one.
14742 */
f6936e29 14743 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 14744 }
46f297fb 14745 }
2c7111db
CW
14746}
14747
7fad798e
DV
14748static void intel_enable_pipe_a(struct drm_device *dev)
14749{
14750 struct intel_connector *connector;
14751 struct drm_connector *crt = NULL;
14752 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14753 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14754
14755 /* We can't just switch on the pipe A, we need to set things up with a
14756 * proper mode and output configuration. As a gross hack, enable pipe A
14757 * by enabling the load detect pipe once. */
3a3371ff 14758 for_each_intel_connector(dev, connector) {
7fad798e
DV
14759 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14760 crt = &connector->base;
14761 break;
14762 }
14763 }
14764
14765 if (!crt)
14766 return;
14767
208bf9fd 14768 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14769 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14770}
14771
fa555837
DV
14772static bool
14773intel_check_plane_mapping(struct intel_crtc *crtc)
14774{
7eb552ae
BW
14775 struct drm_device *dev = crtc->base.dev;
14776 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
14777 u32 reg, val;
14778
7eb552ae 14779 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14780 return true;
14781
14782 reg = DSPCNTR(!crtc->plane);
14783 val = I915_READ(reg);
14784
14785 if ((val & DISPLAY_PLANE_ENABLE) &&
14786 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14787 return false;
14788
14789 return true;
14790}
14791
24929352
DV
14792static void intel_sanitize_crtc(struct intel_crtc *crtc)
14793{
14794 struct drm_device *dev = crtc->base.dev;
14795 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 14796 u32 reg;
24929352 14797
24929352 14798 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 14799 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
14800 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14801
d3eaf884 14802 /* restore vblank interrupts to correct state */
9625604c 14803 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
14804 if (crtc->active) {
14805 update_scanline_offset(crtc);
9625604c
DV
14806 drm_crtc_vblank_on(&crtc->base);
14807 }
d3eaf884 14808
24929352 14809 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14810 * disable the crtc (and hence change the state) if it is wrong. Note
14811 * that gen4+ has a fixed plane -> pipe mapping. */
14812 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14813 struct intel_connector *connector;
14814 bool plane;
14815
24929352
DV
14816 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14817 crtc->base.base.id);
14818
14819 /* Pipe has the wrong plane attached and the plane is active.
14820 * Temporarily change the plane mapping and disable everything
14821 * ... */
14822 plane = crtc->plane;
14823 crtc->plane = !plane;
9c8958bc 14824 crtc->primary_enabled = true;
24929352
DV
14825 dev_priv->display.crtc_disable(&crtc->base);
14826 crtc->plane = plane;
14827
14828 /* ... and break all links. */
3a3371ff 14829 for_each_intel_connector(dev, connector) {
24929352
DV
14830 if (connector->encoder->base.crtc != &crtc->base)
14831 continue;
14832
7f1950fb
EE
14833 connector->base.dpms = DRM_MODE_DPMS_OFF;
14834 connector->base.encoder = NULL;
24929352 14835 }
7f1950fb
EE
14836 /* multiple connectors may have the same encoder:
14837 * handle them and break crtc link separately */
3a3371ff 14838 for_each_intel_connector(dev, connector)
7f1950fb
EE
14839 if (connector->encoder->base.crtc == &crtc->base) {
14840 connector->encoder->base.crtc = NULL;
14841 connector->encoder->connectors_active = false;
14842 }
24929352
DV
14843
14844 WARN_ON(crtc->active);
83d65738 14845 crtc->base.state->enable = false;
24929352
DV
14846 crtc->base.enabled = false;
14847 }
24929352 14848
7fad798e
DV
14849 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14850 crtc->pipe == PIPE_A && !crtc->active) {
14851 /* BIOS forgot to enable pipe A, this mostly happens after
14852 * resume. Force-enable the pipe to fix this, the update_dpms
14853 * call below we restore the pipe to the right state, but leave
14854 * the required bits on. */
14855 intel_enable_pipe_a(dev);
14856 }
14857
24929352
DV
14858 /* Adjust the state of the output pipe according to whether we
14859 * have active connectors/encoders. */
14860 intel_crtc_update_dpms(&crtc->base);
14861
83d65738 14862 if (crtc->active != crtc->base.state->enable) {
24929352
DV
14863 struct intel_encoder *encoder;
14864
14865 /* This can happen either due to bugs in the get_hw_state
14866 * functions or because the pipe is force-enabled due to the
14867 * pipe A quirk. */
14868 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14869 crtc->base.base.id,
83d65738 14870 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
14871 crtc->active ? "enabled" : "disabled");
14872
83d65738 14873 crtc->base.state->enable = crtc->active;
24929352
DV
14874 crtc->base.enabled = crtc->active;
14875
14876 /* Because we only establish the connector -> encoder ->
14877 * crtc links if something is active, this means the
14878 * crtc is now deactivated. Break the links. connector
14879 * -> encoder links are only establish when things are
14880 * actually up, hence no need to break them. */
14881 WARN_ON(crtc->active);
14882
14883 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14884 WARN_ON(encoder->connectors_active);
14885 encoder->base.crtc = NULL;
14886 }
14887 }
c5ab3bc0 14888
a3ed6aad 14889 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
14890 /*
14891 * We start out with underrun reporting disabled to avoid races.
14892 * For correct bookkeeping mark this on active crtcs.
14893 *
c5ab3bc0
DV
14894 * Also on gmch platforms we dont have any hardware bits to
14895 * disable the underrun reporting. Which means we need to start
14896 * out with underrun reporting disabled also on inactive pipes,
14897 * since otherwise we'll complain about the garbage we read when
14898 * e.g. coming up after runtime pm.
14899 *
4cc31489
DV
14900 * No protection against concurrent access is required - at
14901 * worst a fifo underrun happens which also sets this to false.
14902 */
14903 crtc->cpu_fifo_underrun_disabled = true;
14904 crtc->pch_fifo_underrun_disabled = true;
14905 }
24929352
DV
14906}
14907
14908static void intel_sanitize_encoder(struct intel_encoder *encoder)
14909{
14910 struct intel_connector *connector;
14911 struct drm_device *dev = encoder->base.dev;
14912
14913 /* We need to check both for a crtc link (meaning that the
14914 * encoder is active and trying to read from a pipe) and the
14915 * pipe itself being active. */
14916 bool has_active_crtc = encoder->base.crtc &&
14917 to_intel_crtc(encoder->base.crtc)->active;
14918
14919 if (encoder->connectors_active && !has_active_crtc) {
14920 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14921 encoder->base.base.id,
8e329a03 14922 encoder->base.name);
24929352
DV
14923
14924 /* Connector is active, but has no active pipe. This is
14925 * fallout from our resume register restoring. Disable
14926 * the encoder manually again. */
14927 if (encoder->base.crtc) {
14928 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14929 encoder->base.base.id,
8e329a03 14930 encoder->base.name);
24929352 14931 encoder->disable(encoder);
a62d1497
VS
14932 if (encoder->post_disable)
14933 encoder->post_disable(encoder);
24929352 14934 }
7f1950fb
EE
14935 encoder->base.crtc = NULL;
14936 encoder->connectors_active = false;
24929352
DV
14937
14938 /* Inconsistent output/port/pipe state happens presumably due to
14939 * a bug in one of the get_hw_state functions. Or someplace else
14940 * in our code, like the register restore mess on resume. Clamp
14941 * things to off as a safer default. */
3a3371ff 14942 for_each_intel_connector(dev, connector) {
24929352
DV
14943 if (connector->encoder != encoder)
14944 continue;
7f1950fb
EE
14945 connector->base.dpms = DRM_MODE_DPMS_OFF;
14946 connector->base.encoder = NULL;
24929352
DV
14947 }
14948 }
14949 /* Enabled encoders without active connectors will be fixed in
14950 * the crtc fixup. */
14951}
14952
04098753 14953void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
14954{
14955 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 14956 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 14957
04098753
ID
14958 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14959 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14960 i915_disable_vga(dev);
14961 }
14962}
14963
14964void i915_redisable_vga(struct drm_device *dev)
14965{
14966 struct drm_i915_private *dev_priv = dev->dev_private;
14967
8dc8a27c
PZ
14968 /* This function can be called both from intel_modeset_setup_hw_state or
14969 * at a very early point in our resume sequence, where the power well
14970 * structures are not yet restored. Since this function is at a very
14971 * paranoid "someone might have enabled VGA while we were not looking"
14972 * level, just check if the power well is enabled instead of trying to
14973 * follow the "don't touch the power well if we don't need it" policy
14974 * the rest of the driver uses. */
f458ebbc 14975 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14976 return;
14977
04098753 14978 i915_redisable_vga_power_on(dev);
0fde901f
KM
14979}
14980
98ec7739
VS
14981static bool primary_get_hw_state(struct intel_crtc *crtc)
14982{
14983 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14984
14985 if (!crtc->active)
14986 return false;
14987
14988 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14989}
14990
30e984df 14991static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
14992{
14993 struct drm_i915_private *dev_priv = dev->dev_private;
14994 enum pipe pipe;
24929352
DV
14995 struct intel_crtc *crtc;
14996 struct intel_encoder *encoder;
14997 struct intel_connector *connector;
5358901f 14998 int i;
24929352 14999
d3fcc808 15000 for_each_intel_crtc(dev, crtc) {
6e3c9717 15001 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 15002
6e3c9717 15003 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15004
0e8ffe1b 15005 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15006 crtc->config);
24929352 15007
83d65738 15008 crtc->base.state->enable = crtc->active;
24929352 15009 crtc->base.enabled = crtc->active;
98ec7739 15010 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
15011
15012 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15013 crtc->base.base.id,
15014 crtc->active ? "enabled" : "disabled");
15015 }
15016
5358901f
DV
15017 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15018 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15019
3e369b76
ACO
15020 pll->on = pll->get_hw_state(dev_priv, pll,
15021 &pll->config.hw_state);
5358901f 15022 pll->active = 0;
3e369b76 15023 pll->config.crtc_mask = 0;
d3fcc808 15024 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15025 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15026 pll->active++;
3e369b76 15027 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15028 }
5358901f 15029 }
5358901f 15030
1e6f2ddc 15031 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15032 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15033
3e369b76 15034 if (pll->config.crtc_mask)
bd2bb1b9 15035 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15036 }
15037
b2784e15 15038 for_each_intel_encoder(dev, encoder) {
24929352
DV
15039 pipe = 0;
15040
15041 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15042 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15043 encoder->base.crtc = &crtc->base;
6e3c9717 15044 encoder->get_config(encoder, crtc->config);
24929352
DV
15045 } else {
15046 encoder->base.crtc = NULL;
15047 }
15048
15049 encoder->connectors_active = false;
6f2bcceb 15050 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15051 encoder->base.base.id,
8e329a03 15052 encoder->base.name,
24929352 15053 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15054 pipe_name(pipe));
24929352
DV
15055 }
15056
3a3371ff 15057 for_each_intel_connector(dev, connector) {
24929352
DV
15058 if (connector->get_hw_state(connector)) {
15059 connector->base.dpms = DRM_MODE_DPMS_ON;
15060 connector->encoder->connectors_active = true;
15061 connector->base.encoder = &connector->encoder->base;
15062 } else {
15063 connector->base.dpms = DRM_MODE_DPMS_OFF;
15064 connector->base.encoder = NULL;
15065 }
15066 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15067 connector->base.base.id,
c23cc417 15068 connector->base.name,
24929352
DV
15069 connector->base.encoder ? "enabled" : "disabled");
15070 }
30e984df
DV
15071}
15072
15073/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15074 * and i915 state tracking structures. */
15075void intel_modeset_setup_hw_state(struct drm_device *dev,
15076 bool force_restore)
15077{
15078 struct drm_i915_private *dev_priv = dev->dev_private;
15079 enum pipe pipe;
30e984df
DV
15080 struct intel_crtc *crtc;
15081 struct intel_encoder *encoder;
35c95375 15082 int i;
30e984df
DV
15083
15084 intel_modeset_readout_hw_state(dev);
24929352 15085
babea61d
JB
15086 /*
15087 * Now that we have the config, copy it to each CRTC struct
15088 * Note that this could go away if we move to using crtc_config
15089 * checking everywhere.
15090 */
d3fcc808 15091 for_each_intel_crtc(dev, crtc) {
d330a953 15092 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15093 intel_mode_from_pipe_config(&crtc->base.mode,
15094 crtc->config);
babea61d
JB
15095 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15096 crtc->base.base.id);
15097 drm_mode_debug_printmodeline(&crtc->base.mode);
15098 }
15099 }
15100
24929352 15101 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15102 for_each_intel_encoder(dev, encoder) {
24929352
DV
15103 intel_sanitize_encoder(encoder);
15104 }
15105
055e393f 15106 for_each_pipe(dev_priv, pipe) {
24929352
DV
15107 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15108 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15109 intel_dump_pipe_config(crtc, crtc->config,
15110 "[setup_hw_state]");
24929352 15111 }
9a935856 15112
d29b2f9d
ACO
15113 intel_modeset_update_connector_atomic_state(dev);
15114
35c95375
DV
15115 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15116 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15117
15118 if (!pll->on || pll->active)
15119 continue;
15120
15121 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15122
15123 pll->disable(dev_priv, pll);
15124 pll->on = false;
15125 }
15126
3078999f
PB
15127 if (IS_GEN9(dev))
15128 skl_wm_get_hw_state(dev);
15129 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15130 ilk_wm_get_hw_state(dev);
15131
45e2b5f6 15132 if (force_restore) {
7d0bc1ea
VS
15133 i915_redisable_vga(dev);
15134
f30da187
DV
15135 /*
15136 * We need to use raw interfaces for restoring state to avoid
15137 * checking (bogus) intermediate states.
15138 */
055e393f 15139 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15140 struct drm_crtc *crtc =
15141 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15142
83a57153 15143 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15144 }
15145 } else {
15146 intel_modeset_update_staged_output_state(dev);
15147 }
8af6cf88
DV
15148
15149 intel_modeset_check_state(dev);
2c7111db
CW
15150}
15151
15152void intel_modeset_gem_init(struct drm_device *dev)
15153{
92122789 15154 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15155 struct drm_crtc *c;
2ff8fde1 15156 struct drm_i915_gem_object *obj;
e0d6149b 15157 int ret;
484b41dd 15158
ae48434c
ID
15159 mutex_lock(&dev->struct_mutex);
15160 intel_init_gt_powersave(dev);
15161 mutex_unlock(&dev->struct_mutex);
15162
92122789
JB
15163 /*
15164 * There may be no VBT; and if the BIOS enabled SSC we can
15165 * just keep using it to avoid unnecessary flicker. Whereas if the
15166 * BIOS isn't using it, don't assume it will work even if the VBT
15167 * indicates as much.
15168 */
15169 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15170 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15171 DREF_SSC1_ENABLE);
15172
1833b134 15173 intel_modeset_init_hw(dev);
02e792fb
DV
15174
15175 intel_setup_overlay(dev);
484b41dd
JB
15176
15177 /*
15178 * Make sure any fbs we allocated at startup are properly
15179 * pinned & fenced. When we do the allocation it's too early
15180 * for this.
15181 */
70e1e0ec 15182 for_each_crtc(dev, c) {
2ff8fde1
MR
15183 obj = intel_fb_obj(c->primary->fb);
15184 if (obj == NULL)
484b41dd
JB
15185 continue;
15186
e0d6149b
TU
15187 mutex_lock(&dev->struct_mutex);
15188 ret = intel_pin_and_fence_fb_obj(c->primary,
15189 c->primary->fb,
15190 c->primary->state,
15191 NULL);
15192 mutex_unlock(&dev->struct_mutex);
15193 if (ret) {
484b41dd
JB
15194 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15195 to_intel_crtc(c)->pipe);
66e514c1
DA
15196 drm_framebuffer_unreference(c->primary->fb);
15197 c->primary->fb = NULL;
afd65eb4 15198 update_state_fb(c->primary);
484b41dd
JB
15199 }
15200 }
0962c3c9
VS
15201
15202 intel_backlight_register(dev);
79e53945
JB
15203}
15204
4932e2c3
ID
15205void intel_connector_unregister(struct intel_connector *intel_connector)
15206{
15207 struct drm_connector *connector = &intel_connector->base;
15208
15209 intel_panel_destroy_backlight(connector);
34ea3d38 15210 drm_connector_unregister(connector);
4932e2c3
ID
15211}
15212
79e53945
JB
15213void intel_modeset_cleanup(struct drm_device *dev)
15214{
652c393a 15215 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15216 struct drm_connector *connector;
652c393a 15217
2eb5252e
ID
15218 intel_disable_gt_powersave(dev);
15219
0962c3c9
VS
15220 intel_backlight_unregister(dev);
15221
fd0c0642
DV
15222 /*
15223 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15224 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15225 * experience fancy races otherwise.
15226 */
2aeb7d3a 15227 intel_irq_uninstall(dev_priv);
eb21b92b 15228
fd0c0642
DV
15229 /*
15230 * Due to the hpd irq storm handling the hotplug work can re-arm the
15231 * poll handlers. Hence disable polling after hpd handling is shut down.
15232 */
f87ea761 15233 drm_kms_helper_poll_fini(dev);
fd0c0642 15234
652c393a
JB
15235 mutex_lock(&dev->struct_mutex);
15236
723bfd70
JB
15237 intel_unregister_dsm_handler();
15238
7ff0ebcc 15239 intel_fbc_disable(dev);
e70236a8 15240
69341a5e
KH
15241 mutex_unlock(&dev->struct_mutex);
15242
1630fe75
CW
15243 /* flush any delayed tasks or pending work */
15244 flush_scheduled_work();
15245
db31af1d
JN
15246 /* destroy the backlight and sysfs files before encoders/connectors */
15247 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15248 struct intel_connector *intel_connector;
15249
15250 intel_connector = to_intel_connector(connector);
15251 intel_connector->unregister(intel_connector);
db31af1d 15252 }
d9255d57 15253
79e53945 15254 drm_mode_config_cleanup(dev);
4d7bb011
DV
15255
15256 intel_cleanup_overlay(dev);
ae48434c
ID
15257
15258 mutex_lock(&dev->struct_mutex);
15259 intel_cleanup_gt_powersave(dev);
15260 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15261}
15262
f1c79df3
ZW
15263/*
15264 * Return which encoder is currently attached for connector.
15265 */
df0e9248 15266struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15267{
df0e9248
CW
15268 return &intel_attached_encoder(connector)->base;
15269}
f1c79df3 15270
df0e9248
CW
15271void intel_connector_attach_encoder(struct intel_connector *connector,
15272 struct intel_encoder *encoder)
15273{
15274 connector->encoder = encoder;
15275 drm_mode_connector_attach_encoder(&connector->base,
15276 &encoder->base);
79e53945 15277}
28d52043
DA
15278
15279/*
15280 * set vga decode state - true == enable VGA decode
15281 */
15282int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15283{
15284 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15285 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15286 u16 gmch_ctrl;
15287
75fa041d
CW
15288 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15289 DRM_ERROR("failed to read control word\n");
15290 return -EIO;
15291 }
15292
c0cc8a55
CW
15293 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15294 return 0;
15295
28d52043
DA
15296 if (state)
15297 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15298 else
15299 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15300
15301 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15302 DRM_ERROR("failed to write control word\n");
15303 return -EIO;
15304 }
15305
28d52043
DA
15306 return 0;
15307}
c4a1d9e4 15308
c4a1d9e4 15309struct intel_display_error_state {
ff57f1b0
PZ
15310
15311 u32 power_well_driver;
15312
63b66e5b
CW
15313 int num_transcoders;
15314
c4a1d9e4
CW
15315 struct intel_cursor_error_state {
15316 u32 control;
15317 u32 position;
15318 u32 base;
15319 u32 size;
52331309 15320 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15321
15322 struct intel_pipe_error_state {
ddf9c536 15323 bool power_domain_on;
c4a1d9e4 15324 u32 source;
f301b1e1 15325 u32 stat;
52331309 15326 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15327
15328 struct intel_plane_error_state {
15329 u32 control;
15330 u32 stride;
15331 u32 size;
15332 u32 pos;
15333 u32 addr;
15334 u32 surface;
15335 u32 tile_offset;
52331309 15336 } plane[I915_MAX_PIPES];
63b66e5b
CW
15337
15338 struct intel_transcoder_error_state {
ddf9c536 15339 bool power_domain_on;
63b66e5b
CW
15340 enum transcoder cpu_transcoder;
15341
15342 u32 conf;
15343
15344 u32 htotal;
15345 u32 hblank;
15346 u32 hsync;
15347 u32 vtotal;
15348 u32 vblank;
15349 u32 vsync;
15350 } transcoder[4];
c4a1d9e4
CW
15351};
15352
15353struct intel_display_error_state *
15354intel_display_capture_error_state(struct drm_device *dev)
15355{
fbee40df 15356 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15357 struct intel_display_error_state *error;
63b66e5b
CW
15358 int transcoders[] = {
15359 TRANSCODER_A,
15360 TRANSCODER_B,
15361 TRANSCODER_C,
15362 TRANSCODER_EDP,
15363 };
c4a1d9e4
CW
15364 int i;
15365
63b66e5b
CW
15366 if (INTEL_INFO(dev)->num_pipes == 0)
15367 return NULL;
15368
9d1cb914 15369 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15370 if (error == NULL)
15371 return NULL;
15372
190be112 15373 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15374 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15375
055e393f 15376 for_each_pipe(dev_priv, i) {
ddf9c536 15377 error->pipe[i].power_domain_on =
f458ebbc
DV
15378 __intel_display_power_is_enabled(dev_priv,
15379 POWER_DOMAIN_PIPE(i));
ddf9c536 15380 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15381 continue;
15382
5efb3e28
VS
15383 error->cursor[i].control = I915_READ(CURCNTR(i));
15384 error->cursor[i].position = I915_READ(CURPOS(i));
15385 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15386
15387 error->plane[i].control = I915_READ(DSPCNTR(i));
15388 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15389 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15390 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15391 error->plane[i].pos = I915_READ(DSPPOS(i));
15392 }
ca291363
PZ
15393 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15394 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15395 if (INTEL_INFO(dev)->gen >= 4) {
15396 error->plane[i].surface = I915_READ(DSPSURF(i));
15397 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15398 }
15399
c4a1d9e4 15400 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15401
3abfce77 15402 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15403 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15404 }
15405
15406 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15407 if (HAS_DDI(dev_priv->dev))
15408 error->num_transcoders++; /* Account for eDP. */
15409
15410 for (i = 0; i < error->num_transcoders; i++) {
15411 enum transcoder cpu_transcoder = transcoders[i];
15412
ddf9c536 15413 error->transcoder[i].power_domain_on =
f458ebbc 15414 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15415 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15416 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15417 continue;
15418
63b66e5b
CW
15419 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15420
15421 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15422 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15423 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15424 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15425 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15426 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15427 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15428 }
15429
15430 return error;
15431}
15432
edc3d884
MK
15433#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15434
c4a1d9e4 15435void
edc3d884 15436intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15437 struct drm_device *dev,
15438 struct intel_display_error_state *error)
15439{
055e393f 15440 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15441 int i;
15442
63b66e5b
CW
15443 if (!error)
15444 return;
15445
edc3d884 15446 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15447 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15448 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15449 error->power_well_driver);
055e393f 15450 for_each_pipe(dev_priv, i) {
edc3d884 15451 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15452 err_printf(m, " Power: %s\n",
15453 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15454 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15455 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15456
15457 err_printf(m, "Plane [%d]:\n", i);
15458 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15459 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15460 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15461 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15462 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15463 }
4b71a570 15464 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15465 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15466 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15467 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15468 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15469 }
15470
edc3d884
MK
15471 err_printf(m, "Cursor [%d]:\n", i);
15472 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15473 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15474 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15475 }
63b66e5b
CW
15476
15477 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15478 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15479 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15480 err_printf(m, " Power: %s\n",
15481 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15482 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15483 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15484 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15485 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15486 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15487 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15488 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15489 }
c4a1d9e4 15490}
e2fcdaa9
VS
15491
15492void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15493{
15494 struct intel_crtc *crtc;
15495
15496 for_each_intel_crtc(dev, crtc) {
15497 struct intel_unpin_work *work;
e2fcdaa9 15498
5e2d7afc 15499 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15500
15501 work = crtc->unpin_work;
15502
15503 if (work && work->event &&
15504 work->event->base.file_priv == file) {
15505 kfree(work->event);
15506 work->event = NULL;
15507 }
15508
5e2d7afc 15509 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15510 }
15511}