]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915/skl: Add the INIT power domain to the MISC I/O power well
[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);
5485 udelay(10);
5486
5487 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5488 DRM_ERROR("DBuf power enable timeout!\n");
5489}
5490
5491void broxton_uninit_cdclk(struct drm_device *dev)
5492{
5493 struct drm_i915_private *dev_priv = dev->dev_private;
5494
5495 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5496 udelay(10);
5497
5498 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5499 DRM_ERROR("DBuf power disable timeout!\n");
5500
5501 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5502 broxton_set_cdclk(dev, 19200);
5503
5504 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5505}
5506
dfcab17e 5507/* returns HPLL frequency in kHz */
f8bf63fd 5508static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5509{
586f49dc 5510 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5511
586f49dc
JB
5512 /* Obtain SKU information */
5513 mutex_lock(&dev_priv->dpio_lock);
5514 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5515 CCK_FUSE_HPLL_FREQ_MASK;
5516 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 5517
dfcab17e 5518 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5519}
5520
f8bf63fd
VS
5521static void vlv_update_cdclk(struct drm_device *dev)
5522{
5523 struct drm_i915_private *dev_priv = dev->dev_private;
5524
164dfd28 5525 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 5526 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
164dfd28 5527 dev_priv->cdclk_freq);
f8bf63fd
VS
5528
5529 /*
5530 * Program the gmbus_freq based on the cdclk frequency.
5531 * BSpec erroneously claims we should aim for 4MHz, but
5532 * in fact 1MHz is the correct frequency.
5533 */
164dfd28 5534 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
f8bf63fd
VS
5535}
5536
30a970c6
JB
5537/* Adjust CDclk dividers to allow high res or save power if possible */
5538static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541 u32 val, cmd;
5542
164dfd28
VK
5543 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5544 != dev_priv->cdclk_freq);
d60c4473 5545
dfcab17e 5546 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5547 cmd = 2;
dfcab17e 5548 else if (cdclk == 266667)
30a970c6
JB
5549 cmd = 1;
5550 else
5551 cmd = 0;
5552
5553 mutex_lock(&dev_priv->rps.hw_lock);
5554 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5555 val &= ~DSPFREQGUAR_MASK;
5556 val |= (cmd << DSPFREQGUAR_SHIFT);
5557 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5558 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5559 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5560 50)) {
5561 DRM_ERROR("timed out waiting for CDclk change\n");
5562 }
5563 mutex_unlock(&dev_priv->rps.hw_lock);
5564
dfcab17e 5565 if (cdclk == 400000) {
6bcda4f0 5566 u32 divider;
30a970c6 5567
6bcda4f0 5568 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
5569
5570 mutex_lock(&dev_priv->dpio_lock);
5571 /* adjust cdclk divider */
5572 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5573 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5574 val |= divider;
5575 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5576
5577 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5578 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5579 50))
5580 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5581 mutex_unlock(&dev_priv->dpio_lock);
5582 }
5583
5584 mutex_lock(&dev_priv->dpio_lock);
5585 /* adjust self-refresh exit latency value */
5586 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5587 val &= ~0x7f;
5588
5589 /*
5590 * For high bandwidth configs, we set a higher latency in the bunit
5591 * so that the core display fetch happens in time to avoid underruns.
5592 */
dfcab17e 5593 if (cdclk == 400000)
30a970c6
JB
5594 val |= 4500 / 250; /* 4.5 usec */
5595 else
5596 val |= 3000 / 250; /* 3.0 usec */
5597 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5598 mutex_unlock(&dev_priv->dpio_lock);
5599
f8bf63fd 5600 vlv_update_cdclk(dev);
30a970c6
JB
5601}
5602
383c5a6a
VS
5603static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5604{
5605 struct drm_i915_private *dev_priv = dev->dev_private;
5606 u32 val, cmd;
5607
164dfd28
VK
5608 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5609 != dev_priv->cdclk_freq);
383c5a6a
VS
5610
5611 switch (cdclk) {
383c5a6a
VS
5612 case 333333:
5613 case 320000:
383c5a6a 5614 case 266667:
383c5a6a 5615 case 200000:
383c5a6a
VS
5616 break;
5617 default:
5f77eeb0 5618 MISSING_CASE(cdclk);
383c5a6a
VS
5619 return;
5620 }
5621
9d0d3fda
VS
5622 /*
5623 * Specs are full of misinformation, but testing on actual
5624 * hardware has shown that we just need to write the desired
5625 * CCK divider into the Punit register.
5626 */
5627 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5628
383c5a6a
VS
5629 mutex_lock(&dev_priv->rps.hw_lock);
5630 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5631 val &= ~DSPFREQGUAR_MASK_CHV;
5632 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5633 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5634 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5635 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5636 50)) {
5637 DRM_ERROR("timed out waiting for CDclk change\n");
5638 }
5639 mutex_unlock(&dev_priv->rps.hw_lock);
5640
5641 vlv_update_cdclk(dev);
5642}
5643
30a970c6
JB
5644static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5645 int max_pixclk)
5646{
6bcda4f0 5647 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5648 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5649
30a970c6
JB
5650 /*
5651 * Really only a few cases to deal with, as only 4 CDclks are supported:
5652 * 200MHz
5653 * 267MHz
29dc7ef3 5654 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5655 * 400MHz (VLV only)
5656 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5657 * of the lower bin and adjust if needed.
e37c67a1
VS
5658 *
5659 * We seem to get an unstable or solid color picture at 200MHz.
5660 * Not sure what's wrong. For now use 200MHz only when all pipes
5661 * are off.
30a970c6 5662 */
6cca3195
VS
5663 if (!IS_CHERRYVIEW(dev_priv) &&
5664 max_pixclk > freq_320*limit/100)
dfcab17e 5665 return 400000;
6cca3195 5666 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5667 return freq_320;
e37c67a1 5668 else if (max_pixclk > 0)
dfcab17e 5669 return 266667;
e37c67a1
VS
5670 else
5671 return 200000;
30a970c6
JB
5672}
5673
f8437dd1
VK
5674static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5675 int max_pixclk)
5676{
5677 /*
5678 * FIXME:
5679 * - remove the guardband, it's not needed on BXT
5680 * - set 19.2MHz bypass frequency if there are no active pipes
5681 */
5682 if (max_pixclk > 576000*9/10)
5683 return 624000;
5684 else if (max_pixclk > 384000*9/10)
5685 return 576000;
5686 else if (max_pixclk > 288000*9/10)
5687 return 384000;
5688 else if (max_pixclk > 144000*9/10)
5689 return 288000;
5690 else
5691 return 144000;
5692}
5693
2f2d7aa1 5694/* compute the max pixel clock for new configuration */
304603f4 5695static int intel_mode_max_pixclk(struct drm_atomic_state *state)
30a970c6 5696{
304603f4 5697 struct drm_device *dev = state->dev;
30a970c6 5698 struct intel_crtc *intel_crtc;
304603f4 5699 struct intel_crtc_state *crtc_state;
30a970c6
JB
5700 int max_pixclk = 0;
5701
d3fcc808 5702 for_each_intel_crtc(dev, intel_crtc) {
304603f4
ACO
5703 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5704 if (IS_ERR(crtc_state))
5705 return PTR_ERR(crtc_state);
5706
5707 if (!crtc_state->base.enable)
5708 continue;
5709
5710 max_pixclk = max(max_pixclk,
5711 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5712 }
5713
5714 return max_pixclk;
5715}
5716
304603f4 5717static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
2f2d7aa1 5718 unsigned *prepare_pipes)
30a970c6 5719{
304603f4 5720 struct drm_i915_private *dev_priv = to_i915(state->dev);
30a970c6 5721 struct intel_crtc *intel_crtc;
304603f4 5722 int max_pixclk = intel_mode_max_pixclk(state);
f8437dd1 5723 int cdclk;
30a970c6 5724
304603f4
ACO
5725 if (max_pixclk < 0)
5726 return max_pixclk;
30a970c6 5727
f8437dd1
VK
5728 if (IS_VALLEYVIEW(dev_priv))
5729 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5730 else
5731 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5732
5733 if (cdclk == dev_priv->cdclk_freq)
304603f4 5734 return 0;
30a970c6 5735
2f2d7aa1 5736 /* disable/enable all currently active pipes while we change cdclk */
304603f4 5737 for_each_intel_crtc(state->dev, intel_crtc)
83d65738 5738 if (intel_crtc->base.state->enable)
30a970c6 5739 *prepare_pipes |= (1 << intel_crtc->pipe);
304603f4
ACO
5740
5741 return 0;
30a970c6
JB
5742}
5743
1e69cd74
VS
5744static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5745{
5746 unsigned int credits, default_credits;
5747
5748 if (IS_CHERRYVIEW(dev_priv))
5749 default_credits = PFI_CREDIT(12);
5750 else
5751 default_credits = PFI_CREDIT(8);
5752
164dfd28 5753 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5754 /* CHV suggested value is 31 or 63 */
5755 if (IS_CHERRYVIEW(dev_priv))
5756 credits = PFI_CREDIT_31;
5757 else
5758 credits = PFI_CREDIT(15);
5759 } else {
5760 credits = default_credits;
5761 }
5762
5763 /*
5764 * WA - write default credits before re-programming
5765 * FIXME: should we also set the resend bit here?
5766 */
5767 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5768 default_credits);
5769
5770 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5771 credits | PFI_CREDIT_RESEND);
5772
5773 /*
5774 * FIXME is this guaranteed to clear
5775 * immediately or should we poll for it?
5776 */
5777 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5778}
5779
679dacd4 5780static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
30a970c6 5781{
679dacd4 5782 struct drm_device *dev = state->dev;
30a970c6 5783 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4
ACO
5784 int max_pixclk = intel_mode_max_pixclk(state);
5785 int req_cdclk;
5786
5787 /* The only reason this can fail is if we fail to add the crtc_state
5788 * to the atomic state. But that can't happen since the call to
5789 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5790 * can't have failed otherwise the mode set would be aborted) added all
5791 * the states already. */
5792 if (WARN_ON(max_pixclk < 0))
5793 return;
30a970c6 5794
304603f4 5795 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5796
164dfd28 5797 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5798 /*
5799 * FIXME: We can end up here with all power domains off, yet
5800 * with a CDCLK frequency other than the minimum. To account
5801 * for this take the PIPE-A power domain, which covers the HW
5802 * blocks needed for the following programming. This can be
5803 * removed once it's guaranteed that we get here either with
5804 * the minimum CDCLK set, or the required power domains
5805 * enabled.
5806 */
5807 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5808
383c5a6a
VS
5809 if (IS_CHERRYVIEW(dev))
5810 cherryview_set_cdclk(dev, req_cdclk);
5811 else
5812 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5813
1e69cd74
VS
5814 vlv_program_pfi_credits(dev_priv);
5815
738c05c0 5816 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5817 }
30a970c6
JB
5818}
5819
89b667f8
JB
5820static void valleyview_crtc_enable(struct drm_crtc *crtc)
5821{
5822 struct drm_device *dev = crtc->dev;
a72e4c9f 5823 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5825 struct intel_encoder *encoder;
5826 int pipe = intel_crtc->pipe;
23538ef1 5827 bool is_dsi;
89b667f8 5828
83d65738 5829 WARN_ON(!crtc->state->enable);
89b667f8
JB
5830
5831 if (intel_crtc->active)
5832 return;
5833
409ee761 5834 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5835
1ae0d137
VS
5836 if (!is_dsi) {
5837 if (IS_CHERRYVIEW(dev))
6e3c9717 5838 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5839 else
6e3c9717 5840 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5841 }
5b18e57c 5842
6e3c9717 5843 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5844 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5845
5846 intel_set_pipe_timings(intel_crtc);
5847
c14b0485
VS
5848 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5849 struct drm_i915_private *dev_priv = dev->dev_private;
5850
5851 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5852 I915_WRITE(CHV_CANVAS(pipe), 0);
5853 }
5854
5b18e57c
DV
5855 i9xx_set_pipeconf(intel_crtc);
5856
89b667f8 5857 intel_crtc->active = true;
89b667f8 5858
a72e4c9f 5859 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5860
89b667f8
JB
5861 for_each_encoder_on_crtc(dev, crtc, encoder)
5862 if (encoder->pre_pll_enable)
5863 encoder->pre_pll_enable(encoder);
5864
9d556c99
CML
5865 if (!is_dsi) {
5866 if (IS_CHERRYVIEW(dev))
6e3c9717 5867 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5868 else
6e3c9717 5869 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5870 }
89b667f8
JB
5871
5872 for_each_encoder_on_crtc(dev, crtc, encoder)
5873 if (encoder->pre_enable)
5874 encoder->pre_enable(encoder);
5875
2dd24552
JB
5876 i9xx_pfit_enable(intel_crtc);
5877
63cbb074
VS
5878 intel_crtc_load_lut(crtc);
5879
f37fcc2a 5880 intel_update_watermarks(crtc);
e1fdc473 5881 intel_enable_pipe(intel_crtc);
be6a6f8e 5882
4b3a9526
VS
5883 assert_vblank_disabled(crtc);
5884 drm_crtc_vblank_on(crtc);
5885
f9b61ff6
DV
5886 for_each_encoder_on_crtc(dev, crtc, encoder)
5887 encoder->enable(encoder);
5888
9ab0460b 5889 intel_crtc_enable_planes(crtc);
d40d9187 5890
56b80e1f 5891 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5892 i9xx_check_fifo_underruns(dev_priv);
89b667f8
JB
5893}
5894
f13c2ef3
DV
5895static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5896{
5897 struct drm_device *dev = crtc->base.dev;
5898 struct drm_i915_private *dev_priv = dev->dev_private;
5899
6e3c9717
ACO
5900 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5901 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5902}
5903
0b8765c6 5904static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5905{
5906 struct drm_device *dev = crtc->dev;
a72e4c9f 5907 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5909 struct intel_encoder *encoder;
79e53945 5910 int pipe = intel_crtc->pipe;
79e53945 5911
83d65738 5912 WARN_ON(!crtc->state->enable);
08a48469 5913
f7abfe8b
CW
5914 if (intel_crtc->active)
5915 return;
5916
f13c2ef3
DV
5917 i9xx_set_pll_dividers(intel_crtc);
5918
6e3c9717 5919 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5920 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5921
5922 intel_set_pipe_timings(intel_crtc);
5923
5b18e57c
DV
5924 i9xx_set_pipeconf(intel_crtc);
5925
f7abfe8b 5926 intel_crtc->active = true;
6b383a7f 5927
4a3436e8 5928 if (!IS_GEN2(dev))
a72e4c9f 5929 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5930
9d6d9f19
MK
5931 for_each_encoder_on_crtc(dev, crtc, encoder)
5932 if (encoder->pre_enable)
5933 encoder->pre_enable(encoder);
5934
f6736a1a
DV
5935 i9xx_enable_pll(intel_crtc);
5936
2dd24552
JB
5937 i9xx_pfit_enable(intel_crtc);
5938
63cbb074
VS
5939 intel_crtc_load_lut(crtc);
5940
f37fcc2a 5941 intel_update_watermarks(crtc);
e1fdc473 5942 intel_enable_pipe(intel_crtc);
be6a6f8e 5943
4b3a9526
VS
5944 assert_vblank_disabled(crtc);
5945 drm_crtc_vblank_on(crtc);
5946
f9b61ff6
DV
5947 for_each_encoder_on_crtc(dev, crtc, encoder)
5948 encoder->enable(encoder);
5949
9ab0460b 5950 intel_crtc_enable_planes(crtc);
d40d9187 5951
4a3436e8
VS
5952 /*
5953 * Gen2 reports pipe underruns whenever all planes are disabled.
5954 * So don't enable underrun reporting before at least some planes
5955 * are enabled.
5956 * FIXME: Need to fix the logic to work when we turn off all planes
5957 * but leave the pipe running.
5958 */
5959 if (IS_GEN2(dev))
a72e4c9f 5960 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5961
56b80e1f 5962 /* Underruns don't raise interrupts, so check manually. */
a72e4c9f 5963 i9xx_check_fifo_underruns(dev_priv);
0b8765c6 5964}
79e53945 5965
87476d63
DV
5966static void i9xx_pfit_disable(struct intel_crtc *crtc)
5967{
5968 struct drm_device *dev = crtc->base.dev;
5969 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5970
6e3c9717 5971 if (!crtc->config->gmch_pfit.control)
328d8e82 5972 return;
87476d63 5973
328d8e82 5974 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5975
328d8e82
DV
5976 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5977 I915_READ(PFIT_CONTROL));
5978 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5979}
5980
0b8765c6
JB
5981static void i9xx_crtc_disable(struct drm_crtc *crtc)
5982{
5983 struct drm_device *dev = crtc->dev;
5984 struct drm_i915_private *dev_priv = dev->dev_private;
5985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5986 struct intel_encoder *encoder;
0b8765c6 5987 int pipe = intel_crtc->pipe;
ef9c3aee 5988
f7abfe8b
CW
5989 if (!intel_crtc->active)
5990 return;
5991
4a3436e8
VS
5992 /*
5993 * Gen2 reports pipe underruns whenever all planes are disabled.
5994 * So diasble underrun reporting before all the planes get disabled.
5995 * FIXME: Need to fix the logic to work when we turn off all planes
5996 * but leave the pipe running.
5997 */
5998 if (IS_GEN2(dev))
a72e4c9f 5999 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6000
564ed191
ID
6001 /*
6002 * Vblank time updates from the shadow to live plane control register
6003 * are blocked if the memory self-refresh mode is active at that
6004 * moment. So to make sure the plane gets truly disabled, disable
6005 * first the self-refresh mode. The self-refresh enable bit in turn
6006 * will be checked/applied by the HW only at the next frame start
6007 * event which is after the vblank start event, so we need to have a
6008 * wait-for-vblank between disabling the plane and the pipe.
6009 */
6010 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
6011 intel_crtc_disable_planes(crtc);
6012
6304cd91
VS
6013 /*
6014 * On gen2 planes are double buffered but the pipe isn't, so we must
6015 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6016 * We also need to wait on all gmch platforms because of the
6017 * self-refresh mode constraint explained above.
6304cd91 6018 */
564ed191 6019 intel_wait_for_vblank(dev, pipe);
6304cd91 6020
4b3a9526
VS
6021 for_each_encoder_on_crtc(dev, crtc, encoder)
6022 encoder->disable(encoder);
6023
f9b61ff6
DV
6024 drm_crtc_vblank_off(crtc);
6025 assert_vblank_disabled(crtc);
6026
575f7ab7 6027 intel_disable_pipe(intel_crtc);
24a1f16d 6028
87476d63 6029 i9xx_pfit_disable(intel_crtc);
24a1f16d 6030
89b667f8
JB
6031 for_each_encoder_on_crtc(dev, crtc, encoder)
6032 if (encoder->post_disable)
6033 encoder->post_disable(encoder);
6034
409ee761 6035 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6036 if (IS_CHERRYVIEW(dev))
6037 chv_disable_pll(dev_priv, pipe);
6038 else if (IS_VALLEYVIEW(dev))
6039 vlv_disable_pll(dev_priv, pipe);
6040 else
1c4e0274 6041 i9xx_disable_pll(intel_crtc);
076ed3b2 6042 }
0b8765c6 6043
4a3436e8 6044 if (!IS_GEN2(dev))
a72e4c9f 6045 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6046
f7abfe8b 6047 intel_crtc->active = false;
46ba614c 6048 intel_update_watermarks(crtc);
f37fcc2a 6049
efa9624e 6050 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6051 intel_fbc_update(dev);
efa9624e 6052 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6053}
6054
ee7b9f93
JB
6055static void i9xx_crtc_off(struct drm_crtc *crtc)
6056{
6057}
6058
b04c5bd6
BF
6059/* Master function to enable/disable CRTC and corresponding power wells */
6060void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6061{
6062 struct drm_device *dev = crtc->dev;
6063 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 6064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
6065 enum intel_display_power_domain domain;
6066 unsigned long domains;
976f8a20 6067
0e572fe7
DV
6068 if (enable) {
6069 if (!intel_crtc->active) {
e1e9fb84
DV
6070 domains = get_crtc_power_domains(crtc);
6071 for_each_power_domain(domain, domains)
6072 intel_display_power_get(dev_priv, domain);
6073 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
6074
6075 dev_priv->display.crtc_enable(crtc);
6076 }
6077 } else {
6078 if (intel_crtc->active) {
6079 dev_priv->display.crtc_disable(crtc);
6080
e1e9fb84
DV
6081 domains = intel_crtc->enabled_power_domains;
6082 for_each_power_domain(domain, domains)
6083 intel_display_power_put(dev_priv, domain);
6084 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
6085 }
6086 }
b04c5bd6
BF
6087}
6088
6089/**
6090 * Sets the power management mode of the pipe and plane.
6091 */
6092void intel_crtc_update_dpms(struct drm_crtc *crtc)
6093{
6094 struct drm_device *dev = crtc->dev;
6095 struct intel_encoder *intel_encoder;
6096 bool enable = false;
6097
6098 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6099 enable |= intel_encoder->connectors_active;
6100
6101 intel_crtc_control(crtc, enable);
976f8a20
DV
6102}
6103
cdd59983
CW
6104static void intel_crtc_disable(struct drm_crtc *crtc)
6105{
cdd59983 6106 struct drm_device *dev = crtc->dev;
976f8a20 6107 struct drm_connector *connector;
ee7b9f93 6108 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 6109
976f8a20 6110 /* crtc should still be enabled when we disable it. */
83d65738 6111 WARN_ON(!crtc->state->enable);
976f8a20
DV
6112
6113 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
6114 dev_priv->display.off(crtc);
6115
70a101f8 6116 drm_plane_helper_disable(crtc->primary);
976f8a20
DV
6117
6118 /* Update computed state. */
6119 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6120 if (!connector->encoder || !connector->encoder->crtc)
6121 continue;
6122
6123 if (connector->encoder->crtc != crtc)
6124 continue;
6125
6126 connector->dpms = DRM_MODE_DPMS_OFF;
6127 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
6128 }
6129}
6130
ea5b213a 6131void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6132{
4ef69c7a 6133 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6134
ea5b213a
CW
6135 drm_encoder_cleanup(encoder);
6136 kfree(intel_encoder);
7e7d76c3
JB
6137}
6138
9237329d 6139/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6140 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6141 * state of the entire output pipe. */
9237329d 6142static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6143{
5ab432ef
DV
6144 if (mode == DRM_MODE_DPMS_ON) {
6145 encoder->connectors_active = true;
6146
b2cabb0e 6147 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6148 } else {
6149 encoder->connectors_active = false;
6150
b2cabb0e 6151 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6152 }
79e53945
JB
6153}
6154
0a91ca29
DV
6155/* Cross check the actual hw state with our own modeset state tracking (and it's
6156 * internal consistency). */
b980514c 6157static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6158{
0a91ca29
DV
6159 if (connector->get_hw_state(connector)) {
6160 struct intel_encoder *encoder = connector->encoder;
6161 struct drm_crtc *crtc;
6162 bool encoder_enabled;
6163 enum pipe pipe;
6164
6165 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6166 connector->base.base.id,
c23cc417 6167 connector->base.name);
0a91ca29 6168
0e32b39c
DA
6169 /* there is no real hw state for MST connectors */
6170 if (connector->mst_port)
6171 return;
6172
e2c719b7 6173 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6174 "wrong connector dpms state\n");
e2c719b7 6175 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6176 "active connector not linked to encoder\n");
0a91ca29 6177
36cd7444 6178 if (encoder) {
e2c719b7 6179 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6180 "encoder->connectors_active not set\n");
6181
6182 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6183 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6184 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6185 return;
0a91ca29 6186
36cd7444 6187 crtc = encoder->base.crtc;
0a91ca29 6188
83d65738
MR
6189 I915_STATE_WARN(!crtc->state->enable,
6190 "crtc not enabled\n");
e2c719b7
RC
6191 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6192 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6193 "encoder active on the wrong pipe\n");
6194 }
0a91ca29 6195 }
79e53945
JB
6196}
6197
08d9bc92
ACO
6198int intel_connector_init(struct intel_connector *connector)
6199{
6200 struct drm_connector_state *connector_state;
6201
6202 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6203 if (!connector_state)
6204 return -ENOMEM;
6205
6206 connector->base.state = connector_state;
6207 return 0;
6208}
6209
6210struct intel_connector *intel_connector_alloc(void)
6211{
6212 struct intel_connector *connector;
6213
6214 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6215 if (!connector)
6216 return NULL;
6217
6218 if (intel_connector_init(connector) < 0) {
6219 kfree(connector);
6220 return NULL;
6221 }
6222
6223 return connector;
6224}
6225
5ab432ef
DV
6226/* Even simpler default implementation, if there's really no special case to
6227 * consider. */
6228void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6229{
5ab432ef
DV
6230 /* All the simple cases only support two dpms states. */
6231 if (mode != DRM_MODE_DPMS_ON)
6232 mode = DRM_MODE_DPMS_OFF;
d4270e57 6233
5ab432ef
DV
6234 if (mode == connector->dpms)
6235 return;
6236
6237 connector->dpms = mode;
6238
6239 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6240 if (connector->encoder)
6241 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6242
b980514c 6243 intel_modeset_check_state(connector->dev);
79e53945
JB
6244}
6245
f0947c37
DV
6246/* Simple connector->get_hw_state implementation for encoders that support only
6247 * one connector and no cloning and hence the encoder state determines the state
6248 * of the connector. */
6249bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6250{
24929352 6251 enum pipe pipe = 0;
f0947c37 6252 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6253
f0947c37 6254 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6255}
6256
6d293983 6257static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6258{
6d293983
ACO
6259 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6260 return crtc_state->fdi_lanes;
d272ddfa
VS
6261
6262 return 0;
6263}
6264
6d293983 6265static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6266 struct intel_crtc_state *pipe_config)
1857e1da 6267{
6d293983
ACO
6268 struct drm_atomic_state *state = pipe_config->base.state;
6269 struct intel_crtc *other_crtc;
6270 struct intel_crtc_state *other_crtc_state;
6271
1857e1da
DV
6272 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6273 pipe_name(pipe), pipe_config->fdi_lanes);
6274 if (pipe_config->fdi_lanes > 4) {
6275 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6276 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6277 return -EINVAL;
1857e1da
DV
6278 }
6279
bafb6553 6280 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6281 if (pipe_config->fdi_lanes > 2) {
6282 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6283 pipe_config->fdi_lanes);
6d293983 6284 return -EINVAL;
1857e1da 6285 } else {
6d293983 6286 return 0;
1857e1da
DV
6287 }
6288 }
6289
6290 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6291 return 0;
1857e1da
DV
6292
6293 /* Ivybridge 3 pipe is really complicated */
6294 switch (pipe) {
6295 case PIPE_A:
6d293983 6296 return 0;
1857e1da 6297 case PIPE_B:
6d293983
ACO
6298 if (pipe_config->fdi_lanes <= 2)
6299 return 0;
6300
6301 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6302 other_crtc_state =
6303 intel_atomic_get_crtc_state(state, other_crtc);
6304 if (IS_ERR(other_crtc_state))
6305 return PTR_ERR(other_crtc_state);
6306
6307 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6308 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6309 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6310 return -EINVAL;
1857e1da 6311 }
6d293983 6312 return 0;
1857e1da 6313 case PIPE_C:
251cc67c
VS
6314 if (pipe_config->fdi_lanes > 2) {
6315 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6316 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6317 return -EINVAL;
251cc67c 6318 }
6d293983
ACO
6319
6320 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6321 other_crtc_state =
6322 intel_atomic_get_crtc_state(state, other_crtc);
6323 if (IS_ERR(other_crtc_state))
6324 return PTR_ERR(other_crtc_state);
6325
6326 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6327 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6328 return -EINVAL;
1857e1da 6329 }
6d293983 6330 return 0;
1857e1da
DV
6331 default:
6332 BUG();
6333 }
6334}
6335
e29c22c0
DV
6336#define RETRY 1
6337static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6338 struct intel_crtc_state *pipe_config)
877d48d5 6339{
1857e1da 6340 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6341 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6342 int lane, link_bw, fdi_dotclock, ret;
6343 bool needs_recompute = false;
877d48d5 6344
e29c22c0 6345retry:
877d48d5
DV
6346 /* FDI is a binary signal running at ~2.7GHz, encoding
6347 * each output octet as 10 bits. The actual frequency
6348 * is stored as a divider into a 100MHz clock, and the
6349 * mode pixel clock is stored in units of 1KHz.
6350 * Hence the bw of each lane in terms of the mode signal
6351 * is:
6352 */
6353 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6354
241bfc38 6355 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6356
2bd89a07 6357 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6358 pipe_config->pipe_bpp);
6359
6360 pipe_config->fdi_lanes = lane;
6361
2bd89a07 6362 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6363 link_bw, &pipe_config->fdi_m_n);
1857e1da 6364
6d293983
ACO
6365 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6366 intel_crtc->pipe, pipe_config);
6367 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6368 pipe_config->pipe_bpp -= 2*3;
6369 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6370 pipe_config->pipe_bpp);
6371 needs_recompute = true;
6372 pipe_config->bw_constrained = true;
6373
6374 goto retry;
6375 }
6376
6377 if (needs_recompute)
6378 return RETRY;
6379
6d293983 6380 return ret;
877d48d5
DV
6381}
6382
42db64ef 6383static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6384 struct intel_crtc_state *pipe_config)
42db64ef 6385{
d330a953 6386 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 6387 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 6388 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
6389}
6390
a43f6e0f 6391static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6392 struct intel_crtc_state *pipe_config)
79e53945 6393{
a43f6e0f 6394 struct drm_device *dev = crtc->base.dev;
8bd31e67 6395 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6396 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6397 int ret;
89749350 6398
ad3a4479 6399 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6400 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
6401 int clock_limit =
6402 dev_priv->display.get_display_clock_speed(dev);
6403
6404 /*
6405 * Enable pixel doubling when the dot clock
6406 * is > 90% of the (display) core speed.
6407 *
b397c96b
VS
6408 * GDG double wide on either pipe,
6409 * otherwise pipe A only.
cf532bb2 6410 */
b397c96b 6411 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6412 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6413 clock_limit *= 2;
cf532bb2 6414 pipe_config->double_wide = true;
ad3a4479
VS
6415 }
6416
241bfc38 6417 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6418 return -EINVAL;
2c07245f 6419 }
89749350 6420
1d1d0e27
VS
6421 /*
6422 * Pipe horizontal size must be even in:
6423 * - DVO ganged mode
6424 * - LVDS dual channel mode
6425 * - Double wide pipe
6426 */
a93e255f 6427 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6428 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6429 pipe_config->pipe_src_w &= ~1;
6430
8693a824
DL
6431 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6432 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6433 */
6434 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6435 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6436 return -EINVAL;
44f46b42 6437
f5adf94e 6438 if (HAS_IPS(dev))
a43f6e0f
DV
6439 hsw_compute_ips_config(crtc, pipe_config);
6440
877d48d5 6441 if (pipe_config->has_pch_encoder)
a43f6e0f 6442 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6443
d03c93d4
CK
6444 /* FIXME: remove below call once atomic mode set is place and all crtc
6445 * related checks called from atomic_crtc_check function */
6446 ret = 0;
6447 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6448 crtc, pipe_config->base.state);
6449 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6450
6451 return ret;
79e53945
JB
6452}
6453
1652d19e
VS
6454static int skylake_get_display_clock_speed(struct drm_device *dev)
6455{
6456 struct drm_i915_private *dev_priv = to_i915(dev);
6457 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6458 uint32_t cdctl = I915_READ(CDCLK_CTL);
6459 uint32_t linkrate;
6460
6461 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6462 WARN(1, "LCPLL1 not enabled\n");
6463 return 24000; /* 24MHz is the cd freq with NSSC ref */
6464 }
6465
6466 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6467 return 540000;
6468
6469 linkrate = (I915_READ(DPLL_CTRL1) &
6470 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6471
6472 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
6473 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
6474 /* vco 8640 */
6475 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6476 case CDCLK_FREQ_450_432:
6477 return 432000;
6478 case CDCLK_FREQ_337_308:
6479 return 308570;
6480 case CDCLK_FREQ_675_617:
6481 return 617140;
6482 default:
6483 WARN(1, "Unknown cd freq selection\n");
6484 }
6485 } else {
6486 /* vco 8100 */
6487 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6488 case CDCLK_FREQ_450_432:
6489 return 450000;
6490 case CDCLK_FREQ_337_308:
6491 return 337500;
6492 case CDCLK_FREQ_675_617:
6493 return 675000;
6494 default:
6495 WARN(1, "Unknown cd freq selection\n");
6496 }
6497 }
6498
6499 /* error case, do as if DPLL0 isn't enabled */
6500 return 24000;
6501}
6502
6503static int broadwell_get_display_clock_speed(struct drm_device *dev)
6504{
6505 struct drm_i915_private *dev_priv = dev->dev_private;
6506 uint32_t lcpll = I915_READ(LCPLL_CTL);
6507 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6508
6509 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6510 return 800000;
6511 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6512 return 450000;
6513 else if (freq == LCPLL_CLK_FREQ_450)
6514 return 450000;
6515 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6516 return 540000;
6517 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6518 return 337500;
6519 else
6520 return 675000;
6521}
6522
6523static int haswell_get_display_clock_speed(struct drm_device *dev)
6524{
6525 struct drm_i915_private *dev_priv = dev->dev_private;
6526 uint32_t lcpll = I915_READ(LCPLL_CTL);
6527 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6528
6529 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6530 return 800000;
6531 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6532 return 450000;
6533 else if (freq == LCPLL_CLK_FREQ_450)
6534 return 450000;
6535 else if (IS_HSW_ULT(dev))
6536 return 337500;
6537 else
6538 return 540000;
79e53945
JB
6539}
6540
25eb05fc
JB
6541static int valleyview_get_display_clock_speed(struct drm_device *dev)
6542{
d197b7d3 6543 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6544 u32 val;
6545 int divider;
6546
6bcda4f0
VS
6547 if (dev_priv->hpll_freq == 0)
6548 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6549
d197b7d3
VS
6550 mutex_lock(&dev_priv->dpio_lock);
6551 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6552 mutex_unlock(&dev_priv->dpio_lock);
6553
6554 divider = val & DISPLAY_FREQUENCY_VALUES;
6555
7d007f40
VS
6556 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6557 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6558 "cdclk change in progress\n");
6559
6bcda4f0 6560 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6561}
6562
b37a6434
VS
6563static int ilk_get_display_clock_speed(struct drm_device *dev)
6564{
6565 return 450000;
6566}
6567
e70236a8
JB
6568static int i945_get_display_clock_speed(struct drm_device *dev)
6569{
6570 return 400000;
6571}
79e53945 6572
e70236a8 6573static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6574{
e907f170 6575 return 333333;
e70236a8 6576}
79e53945 6577
e70236a8
JB
6578static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6579{
6580 return 200000;
6581}
79e53945 6582
257a7ffc
DV
6583static int pnv_get_display_clock_speed(struct drm_device *dev)
6584{
6585 u16 gcfgc = 0;
6586
6587 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6588
6589 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6590 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6591 return 266667;
257a7ffc 6592 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6593 return 333333;
257a7ffc 6594 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6595 return 444444;
257a7ffc
DV
6596 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6597 return 200000;
6598 default:
6599 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6600 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6601 return 133333;
257a7ffc 6602 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6603 return 166667;
257a7ffc
DV
6604 }
6605}
6606
e70236a8
JB
6607static int i915gm_get_display_clock_speed(struct drm_device *dev)
6608{
6609 u16 gcfgc = 0;
79e53945 6610
e70236a8
JB
6611 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6612
6613 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6614 return 133333;
e70236a8
JB
6615 else {
6616 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6617 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6618 return 333333;
e70236a8
JB
6619 default:
6620 case GC_DISPLAY_CLOCK_190_200_MHZ:
6621 return 190000;
79e53945 6622 }
e70236a8
JB
6623 }
6624}
6625
6626static int i865_get_display_clock_speed(struct drm_device *dev)
6627{
e907f170 6628 return 266667;
e70236a8
JB
6629}
6630
6631static int i855_get_display_clock_speed(struct drm_device *dev)
6632{
6633 u16 hpllcc = 0;
6634 /* Assume that the hardware is in the high speed state. This
6635 * should be the default.
6636 */
6637 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6638 case GC_CLOCK_133_200:
6639 case GC_CLOCK_100_200:
6640 return 200000;
6641 case GC_CLOCK_166_250:
6642 return 250000;
6643 case GC_CLOCK_100_133:
e907f170 6644 return 133333;
e70236a8 6645 }
79e53945 6646
e70236a8
JB
6647 /* Shouldn't happen */
6648 return 0;
6649}
79e53945 6650
e70236a8
JB
6651static int i830_get_display_clock_speed(struct drm_device *dev)
6652{
e907f170 6653 return 133333;
79e53945
JB
6654}
6655
2c07245f 6656static void
a65851af 6657intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6658{
a65851af
VS
6659 while (*num > DATA_LINK_M_N_MASK ||
6660 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6661 *num >>= 1;
6662 *den >>= 1;
6663 }
6664}
6665
a65851af
VS
6666static void compute_m_n(unsigned int m, unsigned int n,
6667 uint32_t *ret_m, uint32_t *ret_n)
6668{
6669 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6670 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6671 intel_reduce_m_n_ratio(ret_m, ret_n);
6672}
6673
e69d0bc1
DV
6674void
6675intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6676 int pixel_clock, int link_clock,
6677 struct intel_link_m_n *m_n)
2c07245f 6678{
e69d0bc1 6679 m_n->tu = 64;
a65851af
VS
6680
6681 compute_m_n(bits_per_pixel * pixel_clock,
6682 link_clock * nlanes * 8,
6683 &m_n->gmch_m, &m_n->gmch_n);
6684
6685 compute_m_n(pixel_clock, link_clock,
6686 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6687}
6688
a7615030
CW
6689static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6690{
d330a953
JN
6691 if (i915.panel_use_ssc >= 0)
6692 return i915.panel_use_ssc != 0;
41aa3448 6693 return dev_priv->vbt.lvds_use_ssc
435793df 6694 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6695}
6696
a93e255f
ACO
6697static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6698 int num_connectors)
c65d77d8 6699{
a93e255f 6700 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
6701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 int refclk;
6703
a93e255f
ACO
6704 WARN_ON(!crtc_state->base.state);
6705
5ab7b0b7 6706 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 6707 refclk = 100000;
a93e255f 6708 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 6709 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
6710 refclk = dev_priv->vbt.lvds_ssc_freq;
6711 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
6712 } else if (!IS_GEN2(dev)) {
6713 refclk = 96000;
6714 } else {
6715 refclk = 48000;
6716 }
6717
6718 return refclk;
6719}
6720
7429e9d4 6721static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6722{
7df00d7a 6723 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6724}
f47709a9 6725
7429e9d4
DV
6726static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6727{
6728 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6729}
6730
f47709a9 6731static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6732 struct intel_crtc_state *crtc_state,
a7516a05
JB
6733 intel_clock_t *reduced_clock)
6734{
f47709a9 6735 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
6736 u32 fp, fp2 = 0;
6737
6738 if (IS_PINEVIEW(dev)) {
190f68c5 6739 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6740 if (reduced_clock)
7429e9d4 6741 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6742 } else {
190f68c5 6743 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6744 if (reduced_clock)
7429e9d4 6745 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6746 }
6747
190f68c5 6748 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6749
f47709a9 6750 crtc->lowfreq_avail = false;
a93e255f 6751 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6752 reduced_clock) {
190f68c5 6753 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6754 crtc->lowfreq_avail = true;
a7516a05 6755 } else {
190f68c5 6756 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6757 }
6758}
6759
5e69f97f
CML
6760static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6761 pipe)
89b667f8
JB
6762{
6763 u32 reg_val;
6764
6765 /*
6766 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6767 * and set it to a reasonable value instead.
6768 */
ab3c759a 6769 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6770 reg_val &= 0xffffff00;
6771 reg_val |= 0x00000030;
ab3c759a 6772 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6773
ab3c759a 6774 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6775 reg_val &= 0x8cffffff;
6776 reg_val = 0x8c000000;
ab3c759a 6777 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6778
ab3c759a 6779 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6780 reg_val &= 0xffffff00;
ab3c759a 6781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6782
ab3c759a 6783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6784 reg_val &= 0x00ffffff;
6785 reg_val |= 0xb0000000;
ab3c759a 6786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6787}
6788
b551842d
DV
6789static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6790 struct intel_link_m_n *m_n)
6791{
6792 struct drm_device *dev = crtc->base.dev;
6793 struct drm_i915_private *dev_priv = dev->dev_private;
6794 int pipe = crtc->pipe;
6795
e3b95f1e
DV
6796 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6797 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6798 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6799 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6800}
6801
6802static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6803 struct intel_link_m_n *m_n,
6804 struct intel_link_m_n *m2_n2)
b551842d
DV
6805{
6806 struct drm_device *dev = crtc->base.dev;
6807 struct drm_i915_private *dev_priv = dev->dev_private;
6808 int pipe = crtc->pipe;
6e3c9717 6809 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6810
6811 if (INTEL_INFO(dev)->gen >= 5) {
6812 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6813 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6814 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6815 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6816 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6817 * for gen < 8) and if DRRS is supported (to make sure the
6818 * registers are not unnecessarily accessed).
6819 */
44395bfe 6820 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6821 crtc->config->has_drrs) {
f769cd24
VK
6822 I915_WRITE(PIPE_DATA_M2(transcoder),
6823 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6824 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6825 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6826 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6827 }
b551842d 6828 } else {
e3b95f1e
DV
6829 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6830 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6831 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6832 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6833 }
6834}
6835
fe3cd48d 6836void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6837{
fe3cd48d
R
6838 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6839
6840 if (m_n == M1_N1) {
6841 dp_m_n = &crtc->config->dp_m_n;
6842 dp_m2_n2 = &crtc->config->dp_m2_n2;
6843 } else if (m_n == M2_N2) {
6844
6845 /*
6846 * M2_N2 registers are not supported. Hence m2_n2 divider value
6847 * needs to be programmed into M1_N1.
6848 */
6849 dp_m_n = &crtc->config->dp_m2_n2;
6850 } else {
6851 DRM_ERROR("Unsupported divider value\n");
6852 return;
6853 }
6854
6e3c9717
ACO
6855 if (crtc->config->has_pch_encoder)
6856 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6857 else
fe3cd48d 6858 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6859}
6860
d288f65f 6861static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6862 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6863{
6864 u32 dpll, dpll_md;
6865
6866 /*
6867 * Enable DPIO clock input. We should never disable the reference
6868 * clock for pipe B, since VGA hotplug / manual detection depends
6869 * on it.
6870 */
6871 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6872 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6873 /* We should never disable this, set it here for state tracking */
6874 if (crtc->pipe == PIPE_B)
6875 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6876 dpll |= DPLL_VCO_ENABLE;
d288f65f 6877 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6878
d288f65f 6879 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6880 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6881 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6882}
6883
d288f65f 6884static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6885 const struct intel_crtc_state *pipe_config)
a0c4da24 6886{
f47709a9 6887 struct drm_device *dev = crtc->base.dev;
a0c4da24 6888 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6889 int pipe = crtc->pipe;
bdd4b6a6 6890 u32 mdiv;
a0c4da24 6891 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6892 u32 coreclk, reg_val;
a0c4da24 6893
09153000
DV
6894 mutex_lock(&dev_priv->dpio_lock);
6895
d288f65f
VS
6896 bestn = pipe_config->dpll.n;
6897 bestm1 = pipe_config->dpll.m1;
6898 bestm2 = pipe_config->dpll.m2;
6899 bestp1 = pipe_config->dpll.p1;
6900 bestp2 = pipe_config->dpll.p2;
a0c4da24 6901
89b667f8
JB
6902 /* See eDP HDMI DPIO driver vbios notes doc */
6903
6904 /* PLL B needs special handling */
bdd4b6a6 6905 if (pipe == PIPE_B)
5e69f97f 6906 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6907
6908 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6909 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6910
6911 /* Disable target IRef on PLL */
ab3c759a 6912 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6913 reg_val &= 0x00ffffff;
ab3c759a 6914 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6915
6916 /* Disable fast lock */
ab3c759a 6917 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6918
6919 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6920 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6921 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6922 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6923 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6924
6925 /*
6926 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6927 * but we don't support that).
6928 * Note: don't use the DAC post divider as it seems unstable.
6929 */
6930 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6931 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6932
a0c4da24 6933 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6934 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6935
89b667f8 6936 /* Set HBR and RBR LPF coefficients */
d288f65f 6937 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6938 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6939 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6941 0x009f0003);
89b667f8 6942 else
ab3c759a 6943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6944 0x00d0000f);
6945
681a8504 6946 if (pipe_config->has_dp_encoder) {
89b667f8 6947 /* Use SSC source */
bdd4b6a6 6948 if (pipe == PIPE_A)
ab3c759a 6949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6950 0x0df40000);
6951 else
ab3c759a 6952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6953 0x0df70000);
6954 } else { /* HDMI or VGA */
6955 /* Use bend source */
bdd4b6a6 6956 if (pipe == PIPE_A)
ab3c759a 6957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6958 0x0df70000);
6959 else
ab3c759a 6960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6961 0x0df40000);
6962 }
a0c4da24 6963
ab3c759a 6964 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6965 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6966 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6967 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6968 coreclk |= 0x01000000;
ab3c759a 6969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6970
ab3c759a 6971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6972 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6973}
6974
d288f65f 6975static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6976 struct intel_crtc_state *pipe_config)
1ae0d137 6977{
d288f65f 6978 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6979 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6980 DPLL_VCO_ENABLE;
6981 if (crtc->pipe != PIPE_A)
d288f65f 6982 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6983
d288f65f
VS
6984 pipe_config->dpll_hw_state.dpll_md =
6985 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6986}
6987
d288f65f 6988static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6989 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6990{
6991 struct drm_device *dev = crtc->base.dev;
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993 int pipe = crtc->pipe;
6994 int dpll_reg = DPLL(crtc->pipe);
6995 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6996 u32 loopfilter, tribuf_calcntr;
9d556c99 6997 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6998 u32 dpio_val;
9cbe40c1 6999 int vco;
9d556c99 7000
d288f65f
VS
7001 bestn = pipe_config->dpll.n;
7002 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7003 bestm1 = pipe_config->dpll.m1;
7004 bestm2 = pipe_config->dpll.m2 >> 22;
7005 bestp1 = pipe_config->dpll.p1;
7006 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7007 vco = pipe_config->dpll.vco;
a945ce7e 7008 dpio_val = 0;
9cbe40c1 7009 loopfilter = 0;
9d556c99
CML
7010
7011 /*
7012 * Enable Refclk and SSC
7013 */
a11b0703 7014 I915_WRITE(dpll_reg,
d288f65f 7015 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
7016
7017 mutex_lock(&dev_priv->dpio_lock);
9d556c99 7018
9d556c99
CML
7019 /* p1 and p2 divider */
7020 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7021 5 << DPIO_CHV_S1_DIV_SHIFT |
7022 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7023 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7024 1 << DPIO_CHV_K_DIV_SHIFT);
7025
7026 /* Feedback post-divider - m2 */
7027 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7028
7029 /* Feedback refclk divider - n and m1 */
7030 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7031 DPIO_CHV_M1_DIV_BY_2 |
7032 1 << DPIO_CHV_N_DIV_SHIFT);
7033
7034 /* M2 fraction division */
a945ce7e
VP
7035 if (bestm2_frac)
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7037
7038 /* M2 fraction division enable */
a945ce7e
VP
7039 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7040 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7041 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7042 if (bestm2_frac)
7043 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7044 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7045
de3a0fde
VP
7046 /* Program digital lock detect threshold */
7047 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7048 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7049 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7050 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7051 if (!bestm2_frac)
7052 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7054
9d556c99 7055 /* Loop filter */
9cbe40c1
VP
7056 if (vco == 5400000) {
7057 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7058 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7059 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7060 tribuf_calcntr = 0x9;
7061 } else if (vco <= 6200000) {
7062 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7063 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7064 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7065 tribuf_calcntr = 0x9;
7066 } else if (vco <= 6480000) {
7067 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7068 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7069 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7070 tribuf_calcntr = 0x8;
7071 } else {
7072 /* Not supported. Apply the same limits as in the max case */
7073 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7074 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7075 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7076 tribuf_calcntr = 0;
7077 }
9d556c99
CML
7078 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7079
968040b2 7080 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7081 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7082 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7083 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7084
9d556c99
CML
7085 /* AFC Recal */
7086 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7087 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7088 DPIO_AFC_RECAL);
7089
7090 mutex_unlock(&dev_priv->dpio_lock);
7091}
7092
d288f65f
VS
7093/**
7094 * vlv_force_pll_on - forcibly enable just the PLL
7095 * @dev_priv: i915 private structure
7096 * @pipe: pipe PLL to enable
7097 * @dpll: PLL configuration
7098 *
7099 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7100 * in cases where we need the PLL enabled even when @pipe is not going to
7101 * be enabled.
7102 */
7103void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7104 const struct dpll *dpll)
7105{
7106 struct intel_crtc *crtc =
7107 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7108 struct intel_crtc_state pipe_config = {
a93e255f 7109 .base.crtc = &crtc->base,
d288f65f
VS
7110 .pixel_multiplier = 1,
7111 .dpll = *dpll,
7112 };
7113
7114 if (IS_CHERRYVIEW(dev)) {
7115 chv_update_pll(crtc, &pipe_config);
7116 chv_prepare_pll(crtc, &pipe_config);
7117 chv_enable_pll(crtc, &pipe_config);
7118 } else {
7119 vlv_update_pll(crtc, &pipe_config);
7120 vlv_prepare_pll(crtc, &pipe_config);
7121 vlv_enable_pll(crtc, &pipe_config);
7122 }
7123}
7124
7125/**
7126 * vlv_force_pll_off - forcibly disable just the PLL
7127 * @dev_priv: i915 private structure
7128 * @pipe: pipe PLL to disable
7129 *
7130 * Disable the PLL for @pipe. To be used in cases where we need
7131 * the PLL enabled even when @pipe is not going to be enabled.
7132 */
7133void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7134{
7135 if (IS_CHERRYVIEW(dev))
7136 chv_disable_pll(to_i915(dev), pipe);
7137 else
7138 vlv_disable_pll(to_i915(dev), pipe);
7139}
7140
f47709a9 7141static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7142 struct intel_crtc_state *crtc_state,
f47709a9 7143 intel_clock_t *reduced_clock,
eb1cbe48
DV
7144 int num_connectors)
7145{
f47709a9 7146 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7147 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7148 u32 dpll;
7149 bool is_sdvo;
190f68c5 7150 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7151
190f68c5 7152 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7153
a93e255f
ACO
7154 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7155 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7156
7157 dpll = DPLL_VGA_MODE_DIS;
7158
a93e255f 7159 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7160 dpll |= DPLLB_MODE_LVDS;
7161 else
7162 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7163
ef1b460d 7164 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7165 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7166 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7167 }
198a037f
DV
7168
7169 if (is_sdvo)
4a33e48d 7170 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7171
190f68c5 7172 if (crtc_state->has_dp_encoder)
4a33e48d 7173 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7174
7175 /* compute bitmask from p1 value */
7176 if (IS_PINEVIEW(dev))
7177 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7178 else {
7179 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7180 if (IS_G4X(dev) && reduced_clock)
7181 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7182 }
7183 switch (clock->p2) {
7184 case 5:
7185 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7186 break;
7187 case 7:
7188 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7189 break;
7190 case 10:
7191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7192 break;
7193 case 14:
7194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7195 break;
7196 }
7197 if (INTEL_INFO(dev)->gen >= 4)
7198 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7199
190f68c5 7200 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7201 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7202 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7203 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7204 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7205 else
7206 dpll |= PLL_REF_INPUT_DREFCLK;
7207
7208 dpll |= DPLL_VCO_ENABLE;
190f68c5 7209 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7210
eb1cbe48 7211 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7212 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7213 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7214 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7215 }
7216}
7217
f47709a9 7218static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7219 struct intel_crtc_state *crtc_state,
f47709a9 7220 intel_clock_t *reduced_clock,
eb1cbe48
DV
7221 int num_connectors)
7222{
f47709a9 7223 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7224 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7225 u32 dpll;
190f68c5 7226 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7227
190f68c5 7228 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7229
eb1cbe48
DV
7230 dpll = DPLL_VGA_MODE_DIS;
7231
a93e255f 7232 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7233 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7234 } else {
7235 if (clock->p1 == 2)
7236 dpll |= PLL_P1_DIVIDE_BY_TWO;
7237 else
7238 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7239 if (clock->p2 == 4)
7240 dpll |= PLL_P2_DIVIDE_BY_4;
7241 }
7242
a93e255f 7243 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7244 dpll |= DPLL_DVO_2X_MODE;
7245
a93e255f 7246 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7247 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7248 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7249 else
7250 dpll |= PLL_REF_INPUT_DREFCLK;
7251
7252 dpll |= DPLL_VCO_ENABLE;
190f68c5 7253 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7254}
7255
8a654f3b 7256static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7257{
7258 struct drm_device *dev = intel_crtc->base.dev;
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7261 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7262 struct drm_display_mode *adjusted_mode =
6e3c9717 7263 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7264 uint32_t crtc_vtotal, crtc_vblank_end;
7265 int vsyncshift = 0;
4d8a62ea
DV
7266
7267 /* We need to be careful not to changed the adjusted mode, for otherwise
7268 * the hw state checker will get angry at the mismatch. */
7269 crtc_vtotal = adjusted_mode->crtc_vtotal;
7270 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7271
609aeaca 7272 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7273 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7274 crtc_vtotal -= 1;
7275 crtc_vblank_end -= 1;
609aeaca 7276
409ee761 7277 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7278 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7279 else
7280 vsyncshift = adjusted_mode->crtc_hsync_start -
7281 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7282 if (vsyncshift < 0)
7283 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7284 }
7285
7286 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7287 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7288
fe2b8f9d 7289 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7290 (adjusted_mode->crtc_hdisplay - 1) |
7291 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7292 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7293 (adjusted_mode->crtc_hblank_start - 1) |
7294 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7295 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7296 (adjusted_mode->crtc_hsync_start - 1) |
7297 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7298
fe2b8f9d 7299 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7300 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7301 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7302 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7303 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7304 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7305 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7306 (adjusted_mode->crtc_vsync_start - 1) |
7307 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7308
b5e508d4
PZ
7309 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7310 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7311 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7312 * bits. */
7313 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7314 (pipe == PIPE_B || pipe == PIPE_C))
7315 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7316
b0e77b9c
PZ
7317 /* pipesrc controls the size that is scaled from, which should
7318 * always be the user's requested size.
7319 */
7320 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7321 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7322 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7323}
7324
1bd1bd80 7325static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7326 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7327{
7328 struct drm_device *dev = crtc->base.dev;
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7331 uint32_t tmp;
7332
7333 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7334 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7335 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7336 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7337 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7338 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7339 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7340 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7341 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7342
7343 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7344 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7345 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7346 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7347 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7348 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7349 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7350 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7351 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7352
7353 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7354 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7355 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7356 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7357 }
7358
7359 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7360 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7361 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7362
2d112de7
ACO
7363 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7364 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7365}
7366
f6a83288 7367void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7368 struct intel_crtc_state *pipe_config)
babea61d 7369{
2d112de7
ACO
7370 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7371 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7372 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7373 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7374
2d112de7
ACO
7375 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7376 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7377 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7378 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7379
2d112de7 7380 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7381
2d112de7
ACO
7382 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7383 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7384}
7385
84b046f3
DV
7386static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7387{
7388 struct drm_device *dev = intel_crtc->base.dev;
7389 struct drm_i915_private *dev_priv = dev->dev_private;
7390 uint32_t pipeconf;
7391
9f11a9e4 7392 pipeconf = 0;
84b046f3 7393
b6b5d049
VS
7394 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7395 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7396 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7397
6e3c9717 7398 if (intel_crtc->config->double_wide)
cf532bb2 7399 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7400
ff9ce46e
DV
7401 /* only g4x and later have fancy bpc/dither controls */
7402 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7403 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7404 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7405 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7406 PIPECONF_DITHER_TYPE_SP;
84b046f3 7407
6e3c9717 7408 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7409 case 18:
7410 pipeconf |= PIPECONF_6BPC;
7411 break;
7412 case 24:
7413 pipeconf |= PIPECONF_8BPC;
7414 break;
7415 case 30:
7416 pipeconf |= PIPECONF_10BPC;
7417 break;
7418 default:
7419 /* Case prevented by intel_choose_pipe_bpp_dither. */
7420 BUG();
84b046f3
DV
7421 }
7422 }
7423
7424 if (HAS_PIPE_CXSR(dev)) {
7425 if (intel_crtc->lowfreq_avail) {
7426 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7427 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7428 } else {
7429 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7430 }
7431 }
7432
6e3c9717 7433 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7434 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7435 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7436 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7437 else
7438 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7439 } else
84b046f3
DV
7440 pipeconf |= PIPECONF_PROGRESSIVE;
7441
6e3c9717 7442 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7443 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7444
84b046f3
DV
7445 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7446 POSTING_READ(PIPECONF(intel_crtc->pipe));
7447}
7448
190f68c5
ACO
7449static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7450 struct intel_crtc_state *crtc_state)
79e53945 7451{
c7653199 7452 struct drm_device *dev = crtc->base.dev;
79e53945 7453 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7454 int refclk, num_connectors = 0;
652c393a 7455 intel_clock_t clock, reduced_clock;
a16af721 7456 bool ok, has_reduced_clock = false;
e9fd1c02 7457 bool is_lvds = false, is_dsi = false;
5eddb70b 7458 struct intel_encoder *encoder;
d4906093 7459 const intel_limit_t *limit;
55bb9992
ACO
7460 struct drm_atomic_state *state = crtc_state->base.state;
7461 struct drm_connector_state *connector_state;
7462 int i;
79e53945 7463
55bb9992
ACO
7464 for (i = 0; i < state->num_connector; i++) {
7465 if (!state->connectors[i])
d0737e1d
ACO
7466 continue;
7467
55bb9992
ACO
7468 connector_state = state->connector_states[i];
7469 if (connector_state->crtc != &crtc->base)
7470 continue;
7471
7472 encoder = to_intel_encoder(connector_state->best_encoder);
7473
5eddb70b 7474 switch (encoder->type) {
79e53945
JB
7475 case INTEL_OUTPUT_LVDS:
7476 is_lvds = true;
7477 break;
e9fd1c02
JN
7478 case INTEL_OUTPUT_DSI:
7479 is_dsi = true;
7480 break;
6847d71b
PZ
7481 default:
7482 break;
79e53945 7483 }
43565a06 7484
c751ce4f 7485 num_connectors++;
79e53945
JB
7486 }
7487
f2335330 7488 if (is_dsi)
5b18e57c 7489 return 0;
f2335330 7490
190f68c5 7491 if (!crtc_state->clock_set) {
a93e255f 7492 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7493
e9fd1c02
JN
7494 /*
7495 * Returns a set of divisors for the desired target clock with
7496 * the given refclk, or FALSE. The returned values represent
7497 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7498 * 2) / p1 / p2.
7499 */
a93e255f
ACO
7500 limit = intel_limit(crtc_state, refclk);
7501 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7502 crtc_state->port_clock,
e9fd1c02 7503 refclk, NULL, &clock);
f2335330 7504 if (!ok) {
e9fd1c02
JN
7505 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7506 return -EINVAL;
7507 }
79e53945 7508
f2335330
JN
7509 if (is_lvds && dev_priv->lvds_downclock_avail) {
7510 /*
7511 * Ensure we match the reduced clock's P to the target
7512 * clock. If the clocks don't match, we can't switch
7513 * the display clock by using the FP0/FP1. In such case
7514 * we will disable the LVDS downclock feature.
7515 */
7516 has_reduced_clock =
a93e255f 7517 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7518 dev_priv->lvds_downclock,
7519 refclk, &clock,
7520 &reduced_clock);
7521 }
7522 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7523 crtc_state->dpll.n = clock.n;
7524 crtc_state->dpll.m1 = clock.m1;
7525 crtc_state->dpll.m2 = clock.m2;
7526 crtc_state->dpll.p1 = clock.p1;
7527 crtc_state->dpll.p2 = clock.p2;
f47709a9 7528 }
7026d4ac 7529
e9fd1c02 7530 if (IS_GEN2(dev)) {
190f68c5 7531 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7532 has_reduced_clock ? &reduced_clock : NULL,
7533 num_connectors);
9d556c99 7534 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7535 chv_update_pll(crtc, crtc_state);
e9fd1c02 7536 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7537 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7538 } else {
190f68c5 7539 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7540 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7541 num_connectors);
e9fd1c02 7542 }
79e53945 7543
c8f7a0db 7544 return 0;
f564048e
EA
7545}
7546
2fa2fe9a 7547static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7548 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7549{
7550 struct drm_device *dev = crtc->base.dev;
7551 struct drm_i915_private *dev_priv = dev->dev_private;
7552 uint32_t tmp;
7553
dc9e7dec
VS
7554 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7555 return;
7556
2fa2fe9a 7557 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7558 if (!(tmp & PFIT_ENABLE))
7559 return;
2fa2fe9a 7560
06922821 7561 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7562 if (INTEL_INFO(dev)->gen < 4) {
7563 if (crtc->pipe != PIPE_B)
7564 return;
2fa2fe9a
DV
7565 } else {
7566 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7567 return;
7568 }
7569
06922821 7570 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7571 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7572 if (INTEL_INFO(dev)->gen < 5)
7573 pipe_config->gmch_pfit.lvds_border_bits =
7574 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7575}
7576
acbec814 7577static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7578 struct intel_crtc_state *pipe_config)
acbec814
JB
7579{
7580 struct drm_device *dev = crtc->base.dev;
7581 struct drm_i915_private *dev_priv = dev->dev_private;
7582 int pipe = pipe_config->cpu_transcoder;
7583 intel_clock_t clock;
7584 u32 mdiv;
662c6ecb 7585 int refclk = 100000;
acbec814 7586
f573de5a
SK
7587 /* In case of MIPI DPLL will not even be used */
7588 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7589 return;
7590
acbec814 7591 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 7592 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
7593 mutex_unlock(&dev_priv->dpio_lock);
7594
7595 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7596 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7597 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7598 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7599 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7600
f646628b 7601 vlv_clock(refclk, &clock);
acbec814 7602
f646628b
VS
7603 /* clock.dot is the fast clock */
7604 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7605}
7606
5724dbd1
DL
7607static void
7608i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7609 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7610{
7611 struct drm_device *dev = crtc->base.dev;
7612 struct drm_i915_private *dev_priv = dev->dev_private;
7613 u32 val, base, offset;
7614 int pipe = crtc->pipe, plane = crtc->plane;
7615 int fourcc, pixel_format;
6761dd31 7616 unsigned int aligned_height;
b113d5ee 7617 struct drm_framebuffer *fb;
1b842c89 7618 struct intel_framebuffer *intel_fb;
1ad292b5 7619
42a7b088
DL
7620 val = I915_READ(DSPCNTR(plane));
7621 if (!(val & DISPLAY_PLANE_ENABLE))
7622 return;
7623
d9806c9f 7624 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7625 if (!intel_fb) {
1ad292b5
JB
7626 DRM_DEBUG_KMS("failed to alloc fb\n");
7627 return;
7628 }
7629
1b842c89
DL
7630 fb = &intel_fb->base;
7631
18c5247e
DV
7632 if (INTEL_INFO(dev)->gen >= 4) {
7633 if (val & DISPPLANE_TILED) {
49af449b 7634 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7635 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7636 }
7637 }
1ad292b5
JB
7638
7639 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7640 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7641 fb->pixel_format = fourcc;
7642 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7643
7644 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7645 if (plane_config->tiling)
1ad292b5
JB
7646 offset = I915_READ(DSPTILEOFF(plane));
7647 else
7648 offset = I915_READ(DSPLINOFF(plane));
7649 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7650 } else {
7651 base = I915_READ(DSPADDR(plane));
7652 }
7653 plane_config->base = base;
7654
7655 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7656 fb->width = ((val >> 16) & 0xfff) + 1;
7657 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7658
7659 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7660 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7661
b113d5ee 7662 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7663 fb->pixel_format,
7664 fb->modifier[0]);
1ad292b5 7665
f37b5c2b 7666 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7667
2844a921
DL
7668 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7669 pipe_name(pipe), plane, fb->width, fb->height,
7670 fb->bits_per_pixel, base, fb->pitches[0],
7671 plane_config->size);
1ad292b5 7672
2d14030b 7673 plane_config->fb = intel_fb;
1ad292b5
JB
7674}
7675
70b23a98 7676static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7677 struct intel_crtc_state *pipe_config)
70b23a98
VS
7678{
7679 struct drm_device *dev = crtc->base.dev;
7680 struct drm_i915_private *dev_priv = dev->dev_private;
7681 int pipe = pipe_config->cpu_transcoder;
7682 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7683 intel_clock_t clock;
7684 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7685 int refclk = 100000;
7686
7687 mutex_lock(&dev_priv->dpio_lock);
7688 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7689 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7690 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7691 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7692 mutex_unlock(&dev_priv->dpio_lock);
7693
7694 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7695 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7696 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7697 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7698 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7699
7700 chv_clock(refclk, &clock);
7701
7702 /* clock.dot is the fast clock */
7703 pipe_config->port_clock = clock.dot / 5;
7704}
7705
0e8ffe1b 7706static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7707 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7708{
7709 struct drm_device *dev = crtc->base.dev;
7710 struct drm_i915_private *dev_priv = dev->dev_private;
7711 uint32_t tmp;
7712
f458ebbc
DV
7713 if (!intel_display_power_is_enabled(dev_priv,
7714 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7715 return false;
7716
e143a21c 7717 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7718 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7719
0e8ffe1b
DV
7720 tmp = I915_READ(PIPECONF(crtc->pipe));
7721 if (!(tmp & PIPECONF_ENABLE))
7722 return false;
7723
42571aef
VS
7724 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7725 switch (tmp & PIPECONF_BPC_MASK) {
7726 case PIPECONF_6BPC:
7727 pipe_config->pipe_bpp = 18;
7728 break;
7729 case PIPECONF_8BPC:
7730 pipe_config->pipe_bpp = 24;
7731 break;
7732 case PIPECONF_10BPC:
7733 pipe_config->pipe_bpp = 30;
7734 break;
7735 default:
7736 break;
7737 }
7738 }
7739
b5a9fa09
DV
7740 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7741 pipe_config->limited_color_range = true;
7742
282740f7
VS
7743 if (INTEL_INFO(dev)->gen < 4)
7744 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7745
1bd1bd80
DV
7746 intel_get_pipe_timings(crtc, pipe_config);
7747
2fa2fe9a
DV
7748 i9xx_get_pfit_config(crtc, pipe_config);
7749
6c49f241
DV
7750 if (INTEL_INFO(dev)->gen >= 4) {
7751 tmp = I915_READ(DPLL_MD(crtc->pipe));
7752 pipe_config->pixel_multiplier =
7753 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7754 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7755 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
7756 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7757 tmp = I915_READ(DPLL(crtc->pipe));
7758 pipe_config->pixel_multiplier =
7759 ((tmp & SDVO_MULTIPLIER_MASK)
7760 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7761 } else {
7762 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7763 * port and will be fixed up in the encoder->get_config
7764 * function. */
7765 pipe_config->pixel_multiplier = 1;
7766 }
8bcc2795
DV
7767 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7768 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
7769 /*
7770 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7771 * on 830. Filter it out here so that we don't
7772 * report errors due to that.
7773 */
7774 if (IS_I830(dev))
7775 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7776
8bcc2795
DV
7777 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7778 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7779 } else {
7780 /* Mask out read-only status bits. */
7781 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7782 DPLL_PORTC_READY_MASK |
7783 DPLL_PORTB_READY_MASK);
8bcc2795 7784 }
6c49f241 7785
70b23a98
VS
7786 if (IS_CHERRYVIEW(dev))
7787 chv_crtc_clock_get(crtc, pipe_config);
7788 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7789 vlv_crtc_clock_get(crtc, pipe_config);
7790 else
7791 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7792
0e8ffe1b
DV
7793 return true;
7794}
7795
dde86e2d 7796static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7797{
7798 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7799 struct intel_encoder *encoder;
74cfd7ac 7800 u32 val, final;
13d83a67 7801 bool has_lvds = false;
199e5d79 7802 bool has_cpu_edp = false;
199e5d79 7803 bool has_panel = false;
99eb6a01
KP
7804 bool has_ck505 = false;
7805 bool can_ssc = false;
13d83a67
JB
7806
7807 /* We need to take the global config into account */
b2784e15 7808 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7809 switch (encoder->type) {
7810 case INTEL_OUTPUT_LVDS:
7811 has_panel = true;
7812 has_lvds = true;
7813 break;
7814 case INTEL_OUTPUT_EDP:
7815 has_panel = true;
2de6905f 7816 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7817 has_cpu_edp = true;
7818 break;
6847d71b
PZ
7819 default:
7820 break;
13d83a67
JB
7821 }
7822 }
7823
99eb6a01 7824 if (HAS_PCH_IBX(dev)) {
41aa3448 7825 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7826 can_ssc = has_ck505;
7827 } else {
7828 has_ck505 = false;
7829 can_ssc = true;
7830 }
7831
2de6905f
ID
7832 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7833 has_panel, has_lvds, has_ck505);
13d83a67
JB
7834
7835 /* Ironlake: try to setup display ref clock before DPLL
7836 * enabling. This is only under driver's control after
7837 * PCH B stepping, previous chipset stepping should be
7838 * ignoring this setting.
7839 */
74cfd7ac
CW
7840 val = I915_READ(PCH_DREF_CONTROL);
7841
7842 /* As we must carefully and slowly disable/enable each source in turn,
7843 * compute the final state we want first and check if we need to
7844 * make any changes at all.
7845 */
7846 final = val;
7847 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7848 if (has_ck505)
7849 final |= DREF_NONSPREAD_CK505_ENABLE;
7850 else
7851 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7852
7853 final &= ~DREF_SSC_SOURCE_MASK;
7854 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7855 final &= ~DREF_SSC1_ENABLE;
7856
7857 if (has_panel) {
7858 final |= DREF_SSC_SOURCE_ENABLE;
7859
7860 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7861 final |= DREF_SSC1_ENABLE;
7862
7863 if (has_cpu_edp) {
7864 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7865 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7866 else
7867 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7868 } else
7869 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7870 } else {
7871 final |= DREF_SSC_SOURCE_DISABLE;
7872 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7873 }
7874
7875 if (final == val)
7876 return;
7877
13d83a67 7878 /* Always enable nonspread source */
74cfd7ac 7879 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7880
99eb6a01 7881 if (has_ck505)
74cfd7ac 7882 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7883 else
74cfd7ac 7884 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7885
199e5d79 7886 if (has_panel) {
74cfd7ac
CW
7887 val &= ~DREF_SSC_SOURCE_MASK;
7888 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7889
199e5d79 7890 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7891 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7892 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7893 val |= DREF_SSC1_ENABLE;
e77166b5 7894 } else
74cfd7ac 7895 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7896
7897 /* Get SSC going before enabling the outputs */
74cfd7ac 7898 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7899 POSTING_READ(PCH_DREF_CONTROL);
7900 udelay(200);
7901
74cfd7ac 7902 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7903
7904 /* Enable CPU source on CPU attached eDP */
199e5d79 7905 if (has_cpu_edp) {
99eb6a01 7906 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7907 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7908 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7909 } else
74cfd7ac 7910 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7911 } else
74cfd7ac 7912 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7913
74cfd7ac 7914 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7915 POSTING_READ(PCH_DREF_CONTROL);
7916 udelay(200);
7917 } else {
7918 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7919
74cfd7ac 7920 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7921
7922 /* Turn off CPU output */
74cfd7ac 7923 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7924
74cfd7ac 7925 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7926 POSTING_READ(PCH_DREF_CONTROL);
7927 udelay(200);
7928
7929 /* Turn off the SSC source */
74cfd7ac
CW
7930 val &= ~DREF_SSC_SOURCE_MASK;
7931 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7932
7933 /* Turn off SSC1 */
74cfd7ac 7934 val &= ~DREF_SSC1_ENABLE;
199e5d79 7935
74cfd7ac 7936 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7937 POSTING_READ(PCH_DREF_CONTROL);
7938 udelay(200);
7939 }
74cfd7ac
CW
7940
7941 BUG_ON(val != final);
13d83a67
JB
7942}
7943
f31f2d55 7944static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7945{
f31f2d55 7946 uint32_t tmp;
dde86e2d 7947
0ff066a9
PZ
7948 tmp = I915_READ(SOUTH_CHICKEN2);
7949 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7950 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7951
0ff066a9
PZ
7952 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7953 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7954 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7955
0ff066a9
PZ
7956 tmp = I915_READ(SOUTH_CHICKEN2);
7957 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7958 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7959
0ff066a9
PZ
7960 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7961 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7962 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7963}
7964
7965/* WaMPhyProgramming:hsw */
7966static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7967{
7968 uint32_t tmp;
dde86e2d
PZ
7969
7970 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7971 tmp &= ~(0xFF << 24);
7972 tmp |= (0x12 << 24);
7973 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7974
dde86e2d
PZ
7975 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7976 tmp |= (1 << 11);
7977 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7978
7979 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7980 tmp |= (1 << 11);
7981 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7982
dde86e2d
PZ
7983 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7984 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7985 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7986
7987 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7988 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7989 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7990
0ff066a9
PZ
7991 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7992 tmp &= ~(7 << 13);
7993 tmp |= (5 << 13);
7994 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7995
0ff066a9
PZ
7996 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7997 tmp &= ~(7 << 13);
7998 tmp |= (5 << 13);
7999 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8000
8001 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8002 tmp &= ~0xFF;
8003 tmp |= 0x1C;
8004 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8005
8006 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8007 tmp &= ~0xFF;
8008 tmp |= 0x1C;
8009 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8010
8011 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8012 tmp &= ~(0xFF << 16);
8013 tmp |= (0x1C << 16);
8014 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8015
8016 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8017 tmp &= ~(0xFF << 16);
8018 tmp |= (0x1C << 16);
8019 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8020
0ff066a9
PZ
8021 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8022 tmp |= (1 << 27);
8023 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8024
0ff066a9
PZ
8025 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8026 tmp |= (1 << 27);
8027 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8028
0ff066a9
PZ
8029 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8030 tmp &= ~(0xF << 28);
8031 tmp |= (4 << 28);
8032 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8033
0ff066a9
PZ
8034 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8035 tmp &= ~(0xF << 28);
8036 tmp |= (4 << 28);
8037 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8038}
8039
2fa86a1f
PZ
8040/* Implements 3 different sequences from BSpec chapter "Display iCLK
8041 * Programming" based on the parameters passed:
8042 * - Sequence to enable CLKOUT_DP
8043 * - Sequence to enable CLKOUT_DP without spread
8044 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8045 */
8046static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8047 bool with_fdi)
f31f2d55
PZ
8048{
8049 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8050 uint32_t reg, tmp;
8051
8052 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8053 with_spread = true;
8054 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8055 with_fdi, "LP PCH doesn't have FDI\n"))
8056 with_fdi = false;
f31f2d55
PZ
8057
8058 mutex_lock(&dev_priv->dpio_lock);
8059
8060 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8061 tmp &= ~SBI_SSCCTL_DISABLE;
8062 tmp |= SBI_SSCCTL_PATHALT;
8063 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8064
8065 udelay(24);
8066
2fa86a1f
PZ
8067 if (with_spread) {
8068 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8069 tmp &= ~SBI_SSCCTL_PATHALT;
8070 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8071
2fa86a1f
PZ
8072 if (with_fdi) {
8073 lpt_reset_fdi_mphy(dev_priv);
8074 lpt_program_fdi_mphy(dev_priv);
8075 }
8076 }
dde86e2d 8077
2fa86a1f
PZ
8078 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8079 SBI_GEN0 : SBI_DBUFF0;
8080 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8081 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8082 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
8083
8084 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
8085}
8086
47701c3b
PZ
8087/* Sequence to disable CLKOUT_DP */
8088static void lpt_disable_clkout_dp(struct drm_device *dev)
8089{
8090 struct drm_i915_private *dev_priv = dev->dev_private;
8091 uint32_t reg, tmp;
8092
8093 mutex_lock(&dev_priv->dpio_lock);
8094
8095 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8096 SBI_GEN0 : SBI_DBUFF0;
8097 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8098 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8099 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8100
8101 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8102 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8103 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8104 tmp |= SBI_SSCCTL_PATHALT;
8105 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8106 udelay(32);
8107 }
8108 tmp |= SBI_SSCCTL_DISABLE;
8109 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8110 }
8111
8112 mutex_unlock(&dev_priv->dpio_lock);
8113}
8114
bf8fa3d3
PZ
8115static void lpt_init_pch_refclk(struct drm_device *dev)
8116{
bf8fa3d3
PZ
8117 struct intel_encoder *encoder;
8118 bool has_vga = false;
8119
b2784e15 8120 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8121 switch (encoder->type) {
8122 case INTEL_OUTPUT_ANALOG:
8123 has_vga = true;
8124 break;
6847d71b
PZ
8125 default:
8126 break;
bf8fa3d3
PZ
8127 }
8128 }
8129
47701c3b
PZ
8130 if (has_vga)
8131 lpt_enable_clkout_dp(dev, true, true);
8132 else
8133 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8134}
8135
dde86e2d
PZ
8136/*
8137 * Initialize reference clocks when the driver loads
8138 */
8139void intel_init_pch_refclk(struct drm_device *dev)
8140{
8141 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8142 ironlake_init_pch_refclk(dev);
8143 else if (HAS_PCH_LPT(dev))
8144 lpt_init_pch_refclk(dev);
8145}
8146
55bb9992 8147static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8148{
55bb9992 8149 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8150 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8151 struct drm_atomic_state *state = crtc_state->base.state;
8152 struct drm_connector_state *connector_state;
d9d444cb 8153 struct intel_encoder *encoder;
55bb9992 8154 int num_connectors = 0, i;
d9d444cb
JB
8155 bool is_lvds = false;
8156
55bb9992
ACO
8157 for (i = 0; i < state->num_connector; i++) {
8158 if (!state->connectors[i])
d0737e1d
ACO
8159 continue;
8160
55bb9992
ACO
8161 connector_state = state->connector_states[i];
8162 if (connector_state->crtc != crtc_state->base.crtc)
8163 continue;
8164
8165 encoder = to_intel_encoder(connector_state->best_encoder);
8166
d9d444cb
JB
8167 switch (encoder->type) {
8168 case INTEL_OUTPUT_LVDS:
8169 is_lvds = true;
8170 break;
6847d71b
PZ
8171 default:
8172 break;
d9d444cb
JB
8173 }
8174 num_connectors++;
8175 }
8176
8177 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8178 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8179 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8180 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8181 }
8182
8183 return 120000;
8184}
8185
6ff93609 8186static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8187{
c8203565 8188 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8190 int pipe = intel_crtc->pipe;
c8203565
PZ
8191 uint32_t val;
8192
78114071 8193 val = 0;
c8203565 8194
6e3c9717 8195 switch (intel_crtc->config->pipe_bpp) {
c8203565 8196 case 18:
dfd07d72 8197 val |= PIPECONF_6BPC;
c8203565
PZ
8198 break;
8199 case 24:
dfd07d72 8200 val |= PIPECONF_8BPC;
c8203565
PZ
8201 break;
8202 case 30:
dfd07d72 8203 val |= PIPECONF_10BPC;
c8203565
PZ
8204 break;
8205 case 36:
dfd07d72 8206 val |= PIPECONF_12BPC;
c8203565
PZ
8207 break;
8208 default:
cc769b62
PZ
8209 /* Case prevented by intel_choose_pipe_bpp_dither. */
8210 BUG();
c8203565
PZ
8211 }
8212
6e3c9717 8213 if (intel_crtc->config->dither)
c8203565
PZ
8214 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8215
6e3c9717 8216 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8217 val |= PIPECONF_INTERLACED_ILK;
8218 else
8219 val |= PIPECONF_PROGRESSIVE;
8220
6e3c9717 8221 if (intel_crtc->config->limited_color_range)
3685a8f3 8222 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8223
c8203565
PZ
8224 I915_WRITE(PIPECONF(pipe), val);
8225 POSTING_READ(PIPECONF(pipe));
8226}
8227
86d3efce
VS
8228/*
8229 * Set up the pipe CSC unit.
8230 *
8231 * Currently only full range RGB to limited range RGB conversion
8232 * is supported, but eventually this should handle various
8233 * RGB<->YCbCr scenarios as well.
8234 */
50f3b016 8235static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8236{
8237 struct drm_device *dev = crtc->dev;
8238 struct drm_i915_private *dev_priv = dev->dev_private;
8239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8240 int pipe = intel_crtc->pipe;
8241 uint16_t coeff = 0x7800; /* 1.0 */
8242
8243 /*
8244 * TODO: Check what kind of values actually come out of the pipe
8245 * with these coeff/postoff values and adjust to get the best
8246 * accuracy. Perhaps we even need to take the bpc value into
8247 * consideration.
8248 */
8249
6e3c9717 8250 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8251 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8252
8253 /*
8254 * GY/GU and RY/RU should be the other way around according
8255 * to BSpec, but reality doesn't agree. Just set them up in
8256 * a way that results in the correct picture.
8257 */
8258 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8259 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8260
8261 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8262 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8263
8264 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8265 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8266
8267 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8268 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8269 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8270
8271 if (INTEL_INFO(dev)->gen > 6) {
8272 uint16_t postoff = 0;
8273
6e3c9717 8274 if (intel_crtc->config->limited_color_range)
32cf0cb0 8275 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8276
8277 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8278 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8279 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8280
8281 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8282 } else {
8283 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8284
6e3c9717 8285 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8286 mode |= CSC_BLACK_SCREEN_OFFSET;
8287
8288 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8289 }
8290}
8291
6ff93609 8292static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8293{
756f85cf
PZ
8294 struct drm_device *dev = crtc->dev;
8295 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8297 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8298 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8299 uint32_t val;
8300
3eff4faa 8301 val = 0;
ee2b0b38 8302
6e3c9717 8303 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8304 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8305
6e3c9717 8306 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8307 val |= PIPECONF_INTERLACED_ILK;
8308 else
8309 val |= PIPECONF_PROGRESSIVE;
8310
702e7a56
PZ
8311 I915_WRITE(PIPECONF(cpu_transcoder), val);
8312 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8313
8314 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8315 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8316
3cdf122c 8317 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8318 val = 0;
8319
6e3c9717 8320 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8321 case 18:
8322 val |= PIPEMISC_DITHER_6_BPC;
8323 break;
8324 case 24:
8325 val |= PIPEMISC_DITHER_8_BPC;
8326 break;
8327 case 30:
8328 val |= PIPEMISC_DITHER_10_BPC;
8329 break;
8330 case 36:
8331 val |= PIPEMISC_DITHER_12_BPC;
8332 break;
8333 default:
8334 /* Case prevented by pipe_config_set_bpp. */
8335 BUG();
8336 }
8337
6e3c9717 8338 if (intel_crtc->config->dither)
756f85cf
PZ
8339 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8340
8341 I915_WRITE(PIPEMISC(pipe), val);
8342 }
ee2b0b38
PZ
8343}
8344
6591c6e4 8345static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8346 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8347 intel_clock_t *clock,
8348 bool *has_reduced_clock,
8349 intel_clock_t *reduced_clock)
8350{
8351 struct drm_device *dev = crtc->dev;
8352 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8353 int refclk;
d4906093 8354 const intel_limit_t *limit;
a16af721 8355 bool ret, is_lvds = false;
79e53945 8356
a93e255f 8357 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8358
55bb9992 8359 refclk = ironlake_get_refclk(crtc_state);
79e53945 8360
d4906093
ML
8361 /*
8362 * Returns a set of divisors for the desired target clock with the given
8363 * refclk, or FALSE. The returned values represent the clock equation:
8364 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8365 */
a93e255f
ACO
8366 limit = intel_limit(crtc_state, refclk);
8367 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8368 crtc_state->port_clock,
ee9300bb 8369 refclk, NULL, clock);
6591c6e4
PZ
8370 if (!ret)
8371 return false;
cda4b7d3 8372
ddc9003c 8373 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8374 /*
8375 * Ensure we match the reduced clock's P to the target clock.
8376 * If the clocks don't match, we can't switch the display clock
8377 * by using the FP0/FP1. In such case we will disable the LVDS
8378 * downclock feature.
8379 */
ee9300bb 8380 *has_reduced_clock =
a93e255f 8381 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8382 dev_priv->lvds_downclock,
8383 refclk, clock,
8384 reduced_clock);
652c393a 8385 }
61e9653f 8386
6591c6e4
PZ
8387 return true;
8388}
8389
d4b1931c
PZ
8390int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8391{
8392 /*
8393 * Account for spread spectrum to avoid
8394 * oversubscribing the link. Max center spread
8395 * is 2.5%; use 5% for safety's sake.
8396 */
8397 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8398 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8399}
8400
7429e9d4 8401static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8402{
7429e9d4 8403 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8404}
8405
de13a2e3 8406static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8407 struct intel_crtc_state *crtc_state,
7429e9d4 8408 u32 *fp,
9a7c7890 8409 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8410{
de13a2e3 8411 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8412 struct drm_device *dev = crtc->dev;
8413 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8414 struct drm_atomic_state *state = crtc_state->base.state;
8415 struct drm_connector_state *connector_state;
8416 struct intel_encoder *encoder;
de13a2e3 8417 uint32_t dpll;
55bb9992 8418 int factor, num_connectors = 0, i;
09ede541 8419 bool is_lvds = false, is_sdvo = false;
79e53945 8420
55bb9992
ACO
8421 for (i = 0; i < state->num_connector; i++) {
8422 if (!state->connectors[i])
d0737e1d
ACO
8423 continue;
8424
55bb9992
ACO
8425 connector_state = state->connector_states[i];
8426 if (connector_state->crtc != crtc_state->base.crtc)
8427 continue;
8428
8429 encoder = to_intel_encoder(connector_state->best_encoder);
8430
8431 switch (encoder->type) {
79e53945
JB
8432 case INTEL_OUTPUT_LVDS:
8433 is_lvds = true;
8434 break;
8435 case INTEL_OUTPUT_SDVO:
7d57382e 8436 case INTEL_OUTPUT_HDMI:
79e53945 8437 is_sdvo = true;
79e53945 8438 break;
6847d71b
PZ
8439 default:
8440 break;
79e53945 8441 }
43565a06 8442
c751ce4f 8443 num_connectors++;
79e53945 8444 }
79e53945 8445
c1858123 8446 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8447 factor = 21;
8448 if (is_lvds) {
8449 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8450 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8451 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8452 factor = 25;
190f68c5 8453 } else if (crtc_state->sdvo_tv_clock)
8febb297 8454 factor = 20;
c1858123 8455
190f68c5 8456 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8457 *fp |= FP_CB_TUNE;
2c07245f 8458
9a7c7890
DV
8459 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8460 *fp2 |= FP_CB_TUNE;
8461
5eddb70b 8462 dpll = 0;
2c07245f 8463
a07d6787
EA
8464 if (is_lvds)
8465 dpll |= DPLLB_MODE_LVDS;
8466 else
8467 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8468
190f68c5 8469 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8470 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8471
8472 if (is_sdvo)
4a33e48d 8473 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8474 if (crtc_state->has_dp_encoder)
4a33e48d 8475 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8476
a07d6787 8477 /* compute bitmask from p1 value */
190f68c5 8478 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8479 /* also FPA1 */
190f68c5 8480 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8481
190f68c5 8482 switch (crtc_state->dpll.p2) {
a07d6787
EA
8483 case 5:
8484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8485 break;
8486 case 7:
8487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8488 break;
8489 case 10:
8490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8491 break;
8492 case 14:
8493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8494 break;
79e53945
JB
8495 }
8496
b4c09f3b 8497 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8498 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8499 else
8500 dpll |= PLL_REF_INPUT_DREFCLK;
8501
959e16d6 8502 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8503}
8504
190f68c5
ACO
8505static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8506 struct intel_crtc_state *crtc_state)
de13a2e3 8507{
c7653199 8508 struct drm_device *dev = crtc->base.dev;
de13a2e3 8509 intel_clock_t clock, reduced_clock;
cbbab5bd 8510 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8511 bool ok, has_reduced_clock = false;
8b47047b 8512 bool is_lvds = false;
e2b78267 8513 struct intel_shared_dpll *pll;
de13a2e3 8514
409ee761 8515 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8516
5dc5298b
PZ
8517 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8518 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8519
190f68c5 8520 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8521 &has_reduced_clock, &reduced_clock);
190f68c5 8522 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8523 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8524 return -EINVAL;
79e53945 8525 }
f47709a9 8526 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8527 if (!crtc_state->clock_set) {
8528 crtc_state->dpll.n = clock.n;
8529 crtc_state->dpll.m1 = clock.m1;
8530 crtc_state->dpll.m2 = clock.m2;
8531 crtc_state->dpll.p1 = clock.p1;
8532 crtc_state->dpll.p2 = clock.p2;
f47709a9 8533 }
79e53945 8534
5dc5298b 8535 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8536 if (crtc_state->has_pch_encoder) {
8537 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8538 if (has_reduced_clock)
7429e9d4 8539 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8540
190f68c5 8541 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8542 &fp, &reduced_clock,
8543 has_reduced_clock ? &fp2 : NULL);
8544
190f68c5
ACO
8545 crtc_state->dpll_hw_state.dpll = dpll;
8546 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8547 if (has_reduced_clock)
190f68c5 8548 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8549 else
190f68c5 8550 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8551
190f68c5 8552 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8553 if (pll == NULL) {
84f44ce7 8554 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8555 pipe_name(crtc->pipe));
4b645f14
JB
8556 return -EINVAL;
8557 }
3fb37703 8558 }
79e53945 8559
ab585dea 8560 if (is_lvds && has_reduced_clock)
c7653199 8561 crtc->lowfreq_avail = true;
bcd644e0 8562 else
c7653199 8563 crtc->lowfreq_avail = false;
e2b78267 8564
c8f7a0db 8565 return 0;
79e53945
JB
8566}
8567
eb14cb74
VS
8568static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8569 struct intel_link_m_n *m_n)
8570{
8571 struct drm_device *dev = crtc->base.dev;
8572 struct drm_i915_private *dev_priv = dev->dev_private;
8573 enum pipe pipe = crtc->pipe;
8574
8575 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8576 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8577 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8578 & ~TU_SIZE_MASK;
8579 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8580 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8581 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8582}
8583
8584static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8585 enum transcoder transcoder,
b95af8be
VK
8586 struct intel_link_m_n *m_n,
8587 struct intel_link_m_n *m2_n2)
72419203
DV
8588{
8589 struct drm_device *dev = crtc->base.dev;
8590 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8591 enum pipe pipe = crtc->pipe;
72419203 8592
eb14cb74
VS
8593 if (INTEL_INFO(dev)->gen >= 5) {
8594 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8595 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8596 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8597 & ~TU_SIZE_MASK;
8598 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8599 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8600 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8601 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8602 * gen < 8) and if DRRS is supported (to make sure the
8603 * registers are not unnecessarily read).
8604 */
8605 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8606 crtc->config->has_drrs) {
b95af8be
VK
8607 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8608 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8609 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8610 & ~TU_SIZE_MASK;
8611 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8612 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8613 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8614 }
eb14cb74
VS
8615 } else {
8616 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8617 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8618 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8619 & ~TU_SIZE_MASK;
8620 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8621 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8622 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8623 }
8624}
8625
8626void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8627 struct intel_crtc_state *pipe_config)
eb14cb74 8628{
681a8504 8629 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8630 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8631 else
8632 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8633 &pipe_config->dp_m_n,
8634 &pipe_config->dp_m2_n2);
eb14cb74 8635}
72419203 8636
eb14cb74 8637static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8638 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8639{
8640 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8641 &pipe_config->fdi_m_n, NULL);
72419203
DV
8642}
8643
bd2e244f 8644static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8645 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8646{
8647 struct drm_device *dev = crtc->base.dev;
8648 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8649 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8650 uint32_t ps_ctrl = 0;
8651 int id = -1;
8652 int i;
bd2e244f 8653
a1b2278e
CK
8654 /* find scaler attached to this pipe */
8655 for (i = 0; i < crtc->num_scalers; i++) {
8656 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8657 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8658 id = i;
8659 pipe_config->pch_pfit.enabled = true;
8660 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8661 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8662 break;
8663 }
8664 }
bd2e244f 8665
a1b2278e
CK
8666 scaler_state->scaler_id = id;
8667 if (id >= 0) {
8668 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8669 } else {
8670 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8671 }
8672}
8673
5724dbd1
DL
8674static void
8675skylake_get_initial_plane_config(struct intel_crtc *crtc,
8676 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8677{
8678 struct drm_device *dev = crtc->base.dev;
8679 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8680 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8681 int pipe = crtc->pipe;
8682 int fourcc, pixel_format;
6761dd31 8683 unsigned int aligned_height;
bc8d7dff 8684 struct drm_framebuffer *fb;
1b842c89 8685 struct intel_framebuffer *intel_fb;
bc8d7dff 8686
d9806c9f 8687 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8688 if (!intel_fb) {
bc8d7dff
DL
8689 DRM_DEBUG_KMS("failed to alloc fb\n");
8690 return;
8691 }
8692
1b842c89
DL
8693 fb = &intel_fb->base;
8694
bc8d7dff 8695 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8696 if (!(val & PLANE_CTL_ENABLE))
8697 goto error;
8698
bc8d7dff
DL
8699 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8700 fourcc = skl_format_to_fourcc(pixel_format,
8701 val & PLANE_CTL_ORDER_RGBX,
8702 val & PLANE_CTL_ALPHA_MASK);
8703 fb->pixel_format = fourcc;
8704 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8705
40f46283
DL
8706 tiling = val & PLANE_CTL_TILED_MASK;
8707 switch (tiling) {
8708 case PLANE_CTL_TILED_LINEAR:
8709 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8710 break;
8711 case PLANE_CTL_TILED_X:
8712 plane_config->tiling = I915_TILING_X;
8713 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8714 break;
8715 case PLANE_CTL_TILED_Y:
8716 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8717 break;
8718 case PLANE_CTL_TILED_YF:
8719 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8720 break;
8721 default:
8722 MISSING_CASE(tiling);
8723 goto error;
8724 }
8725
bc8d7dff
DL
8726 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8727 plane_config->base = base;
8728
8729 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8730
8731 val = I915_READ(PLANE_SIZE(pipe, 0));
8732 fb->height = ((val >> 16) & 0xfff) + 1;
8733 fb->width = ((val >> 0) & 0x1fff) + 1;
8734
8735 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
8736 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8737 fb->pixel_format);
bc8d7dff
DL
8738 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8739
8740 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8741 fb->pixel_format,
8742 fb->modifier[0]);
bc8d7dff 8743
f37b5c2b 8744 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8745
8746 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8747 pipe_name(pipe), fb->width, fb->height,
8748 fb->bits_per_pixel, base, fb->pitches[0],
8749 plane_config->size);
8750
2d14030b 8751 plane_config->fb = intel_fb;
bc8d7dff
DL
8752 return;
8753
8754error:
8755 kfree(fb);
8756}
8757
2fa2fe9a 8758static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8759 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8760{
8761 struct drm_device *dev = crtc->base.dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
8763 uint32_t tmp;
8764
8765 tmp = I915_READ(PF_CTL(crtc->pipe));
8766
8767 if (tmp & PF_ENABLE) {
fd4daa9c 8768 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8769 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8770 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8771
8772 /* We currently do not free assignements of panel fitters on
8773 * ivb/hsw (since we don't use the higher upscaling modes which
8774 * differentiates them) so just WARN about this case for now. */
8775 if (IS_GEN7(dev)) {
8776 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8777 PF_PIPE_SEL_IVB(crtc->pipe));
8778 }
2fa2fe9a 8779 }
79e53945
JB
8780}
8781
5724dbd1
DL
8782static void
8783ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8784 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8785{
8786 struct drm_device *dev = crtc->base.dev;
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 u32 val, base, offset;
aeee5a49 8789 int pipe = crtc->pipe;
4c6baa59 8790 int fourcc, pixel_format;
6761dd31 8791 unsigned int aligned_height;
b113d5ee 8792 struct drm_framebuffer *fb;
1b842c89 8793 struct intel_framebuffer *intel_fb;
4c6baa59 8794
42a7b088
DL
8795 val = I915_READ(DSPCNTR(pipe));
8796 if (!(val & DISPLAY_PLANE_ENABLE))
8797 return;
8798
d9806c9f 8799 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8800 if (!intel_fb) {
4c6baa59
JB
8801 DRM_DEBUG_KMS("failed to alloc fb\n");
8802 return;
8803 }
8804
1b842c89
DL
8805 fb = &intel_fb->base;
8806
18c5247e
DV
8807 if (INTEL_INFO(dev)->gen >= 4) {
8808 if (val & DISPPLANE_TILED) {
49af449b 8809 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8810 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8811 }
8812 }
4c6baa59
JB
8813
8814 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8815 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8816 fb->pixel_format = fourcc;
8817 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8818
aeee5a49 8819 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8820 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8821 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8822 } else {
49af449b 8823 if (plane_config->tiling)
aeee5a49 8824 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8825 else
aeee5a49 8826 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8827 }
8828 plane_config->base = base;
8829
8830 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8831 fb->width = ((val >> 16) & 0xfff) + 1;
8832 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8833
8834 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8835 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8836
b113d5ee 8837 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8838 fb->pixel_format,
8839 fb->modifier[0]);
4c6baa59 8840
f37b5c2b 8841 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8842
2844a921
DL
8843 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8844 pipe_name(pipe), fb->width, fb->height,
8845 fb->bits_per_pixel, base, fb->pitches[0],
8846 plane_config->size);
b113d5ee 8847
2d14030b 8848 plane_config->fb = intel_fb;
4c6baa59
JB
8849}
8850
0e8ffe1b 8851static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8852 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8853{
8854 struct drm_device *dev = crtc->base.dev;
8855 struct drm_i915_private *dev_priv = dev->dev_private;
8856 uint32_t tmp;
8857
f458ebbc
DV
8858 if (!intel_display_power_is_enabled(dev_priv,
8859 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8860 return false;
8861
e143a21c 8862 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8863 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8864
0e8ffe1b
DV
8865 tmp = I915_READ(PIPECONF(crtc->pipe));
8866 if (!(tmp & PIPECONF_ENABLE))
8867 return false;
8868
42571aef
VS
8869 switch (tmp & PIPECONF_BPC_MASK) {
8870 case PIPECONF_6BPC:
8871 pipe_config->pipe_bpp = 18;
8872 break;
8873 case PIPECONF_8BPC:
8874 pipe_config->pipe_bpp = 24;
8875 break;
8876 case PIPECONF_10BPC:
8877 pipe_config->pipe_bpp = 30;
8878 break;
8879 case PIPECONF_12BPC:
8880 pipe_config->pipe_bpp = 36;
8881 break;
8882 default:
8883 break;
8884 }
8885
b5a9fa09
DV
8886 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8887 pipe_config->limited_color_range = true;
8888
ab9412ba 8889 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8890 struct intel_shared_dpll *pll;
8891
88adfff1
DV
8892 pipe_config->has_pch_encoder = true;
8893
627eb5a3
DV
8894 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8895 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8896 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8897
8898 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8899
c0d43d62 8900 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8901 pipe_config->shared_dpll =
8902 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8903 } else {
8904 tmp = I915_READ(PCH_DPLL_SEL);
8905 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8906 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8907 else
8908 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8909 }
66e985c0
DV
8910
8911 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8912
8913 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8914 &pipe_config->dpll_hw_state));
c93f54cf
DV
8915
8916 tmp = pipe_config->dpll_hw_state.dpll;
8917 pipe_config->pixel_multiplier =
8918 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8919 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8920
8921 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8922 } else {
8923 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8924 }
8925
1bd1bd80
DV
8926 intel_get_pipe_timings(crtc, pipe_config);
8927
2fa2fe9a
DV
8928 ironlake_get_pfit_config(crtc, pipe_config);
8929
0e8ffe1b
DV
8930 return true;
8931}
8932
be256dc7
PZ
8933static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8934{
8935 struct drm_device *dev = dev_priv->dev;
be256dc7 8936 struct intel_crtc *crtc;
be256dc7 8937
d3fcc808 8938 for_each_intel_crtc(dev, crtc)
e2c719b7 8939 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8940 pipe_name(crtc->pipe));
8941
e2c719b7
RC
8942 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8943 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8944 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8945 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8946 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8947 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8948 "CPU PWM1 enabled\n");
c5107b87 8949 if (IS_HASWELL(dev))
e2c719b7 8950 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8951 "CPU PWM2 enabled\n");
e2c719b7 8952 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8953 "PCH PWM1 enabled\n");
e2c719b7 8954 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8955 "Utility pin enabled\n");
e2c719b7 8956 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8957
9926ada1
PZ
8958 /*
8959 * In theory we can still leave IRQs enabled, as long as only the HPD
8960 * interrupts remain enabled. We used to check for that, but since it's
8961 * gen-specific and since we only disable LCPLL after we fully disable
8962 * the interrupts, the check below should be enough.
8963 */
e2c719b7 8964 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8965}
8966
9ccd5aeb
PZ
8967static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8968{
8969 struct drm_device *dev = dev_priv->dev;
8970
8971 if (IS_HASWELL(dev))
8972 return I915_READ(D_COMP_HSW);
8973 else
8974 return I915_READ(D_COMP_BDW);
8975}
8976
3c4c9b81
PZ
8977static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8978{
8979 struct drm_device *dev = dev_priv->dev;
8980
8981 if (IS_HASWELL(dev)) {
8982 mutex_lock(&dev_priv->rps.hw_lock);
8983 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8984 val))
f475dadf 8985 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8986 mutex_unlock(&dev_priv->rps.hw_lock);
8987 } else {
9ccd5aeb
PZ
8988 I915_WRITE(D_COMP_BDW, val);
8989 POSTING_READ(D_COMP_BDW);
3c4c9b81 8990 }
be256dc7
PZ
8991}
8992
8993/*
8994 * This function implements pieces of two sequences from BSpec:
8995 * - Sequence for display software to disable LCPLL
8996 * - Sequence for display software to allow package C8+
8997 * The steps implemented here are just the steps that actually touch the LCPLL
8998 * register. Callers should take care of disabling all the display engine
8999 * functions, doing the mode unset, fixing interrupts, etc.
9000 */
6ff58d53
PZ
9001static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9002 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9003{
9004 uint32_t val;
9005
9006 assert_can_disable_lcpll(dev_priv);
9007
9008 val = I915_READ(LCPLL_CTL);
9009
9010 if (switch_to_fclk) {
9011 val |= LCPLL_CD_SOURCE_FCLK;
9012 I915_WRITE(LCPLL_CTL, val);
9013
9014 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9015 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9016 DRM_ERROR("Switching to FCLK failed\n");
9017
9018 val = I915_READ(LCPLL_CTL);
9019 }
9020
9021 val |= LCPLL_PLL_DISABLE;
9022 I915_WRITE(LCPLL_CTL, val);
9023 POSTING_READ(LCPLL_CTL);
9024
9025 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9026 DRM_ERROR("LCPLL still locked\n");
9027
9ccd5aeb 9028 val = hsw_read_dcomp(dev_priv);
be256dc7 9029 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9030 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9031 ndelay(100);
9032
9ccd5aeb
PZ
9033 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9034 1))
be256dc7
PZ
9035 DRM_ERROR("D_COMP RCOMP still in progress\n");
9036
9037 if (allow_power_down) {
9038 val = I915_READ(LCPLL_CTL);
9039 val |= LCPLL_POWER_DOWN_ALLOW;
9040 I915_WRITE(LCPLL_CTL, val);
9041 POSTING_READ(LCPLL_CTL);
9042 }
9043}
9044
9045/*
9046 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9047 * source.
9048 */
6ff58d53 9049static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9050{
9051 uint32_t val;
9052
9053 val = I915_READ(LCPLL_CTL);
9054
9055 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9056 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9057 return;
9058
a8a8bd54
PZ
9059 /*
9060 * Make sure we're not on PC8 state before disabling PC8, otherwise
9061 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9062 */
59bad947 9063 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9064
be256dc7
PZ
9065 if (val & LCPLL_POWER_DOWN_ALLOW) {
9066 val &= ~LCPLL_POWER_DOWN_ALLOW;
9067 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9068 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9069 }
9070
9ccd5aeb 9071 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9072 val |= D_COMP_COMP_FORCE;
9073 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9074 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9075
9076 val = I915_READ(LCPLL_CTL);
9077 val &= ~LCPLL_PLL_DISABLE;
9078 I915_WRITE(LCPLL_CTL, val);
9079
9080 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9081 DRM_ERROR("LCPLL not locked yet\n");
9082
9083 if (val & LCPLL_CD_SOURCE_FCLK) {
9084 val = I915_READ(LCPLL_CTL);
9085 val &= ~LCPLL_CD_SOURCE_FCLK;
9086 I915_WRITE(LCPLL_CTL, val);
9087
9088 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9089 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9090 DRM_ERROR("Switching back to LCPLL failed\n");
9091 }
215733fa 9092
59bad947 9093 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
9094}
9095
765dab67
PZ
9096/*
9097 * Package states C8 and deeper are really deep PC states that can only be
9098 * reached when all the devices on the system allow it, so even if the graphics
9099 * device allows PC8+, it doesn't mean the system will actually get to these
9100 * states. Our driver only allows PC8+ when going into runtime PM.
9101 *
9102 * The requirements for PC8+ are that all the outputs are disabled, the power
9103 * well is disabled and most interrupts are disabled, and these are also
9104 * requirements for runtime PM. When these conditions are met, we manually do
9105 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9106 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9107 * hang the machine.
9108 *
9109 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9110 * the state of some registers, so when we come back from PC8+ we need to
9111 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9112 * need to take care of the registers kept by RC6. Notice that this happens even
9113 * if we don't put the device in PCI D3 state (which is what currently happens
9114 * because of the runtime PM support).
9115 *
9116 * For more, read "Display Sequences for Package C8" on the hardware
9117 * documentation.
9118 */
a14cb6fc 9119void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9120{
c67a470b
PZ
9121 struct drm_device *dev = dev_priv->dev;
9122 uint32_t val;
9123
c67a470b
PZ
9124 DRM_DEBUG_KMS("Enabling package C8+\n");
9125
c67a470b
PZ
9126 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9127 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9128 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9129 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9130 }
9131
9132 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9133 hsw_disable_lcpll(dev_priv, true, true);
9134}
9135
a14cb6fc 9136void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9137{
9138 struct drm_device *dev = dev_priv->dev;
9139 uint32_t val;
9140
c67a470b
PZ
9141 DRM_DEBUG_KMS("Disabling package C8+\n");
9142
9143 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9144 lpt_init_pch_refclk(dev);
9145
9146 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9147 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9148 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9149 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9150 }
9151
9152 intel_prepare_ddi(dev);
c67a470b
PZ
9153}
9154
f8437dd1
VK
9155static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9156{
9157 struct drm_device *dev = state->dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 int max_pixclk = intel_mode_max_pixclk(state);
9160 int req_cdclk;
9161
9162 /* see the comment in valleyview_modeset_global_resources */
9163 if (WARN_ON(max_pixclk < 0))
9164 return;
9165
9166 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9167
9168 if (req_cdclk != dev_priv->cdclk_freq)
9169 broxton_set_cdclk(dev, req_cdclk);
9170}
9171
190f68c5
ACO
9172static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9173 struct intel_crtc_state *crtc_state)
09b4ddf9 9174{
190f68c5 9175 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9176 return -EINVAL;
716c2e55 9177
c7653199 9178 crtc->lowfreq_avail = false;
644cef34 9179
c8f7a0db 9180 return 0;
79e53945
JB
9181}
9182
3760b59c
S
9183static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9184 enum port port,
9185 struct intel_crtc_state *pipe_config)
9186{
9187 switch (port) {
9188 case PORT_A:
9189 pipe_config->ddi_pll_sel = SKL_DPLL0;
9190 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9191 break;
9192 case PORT_B:
9193 pipe_config->ddi_pll_sel = SKL_DPLL1;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9195 break;
9196 case PORT_C:
9197 pipe_config->ddi_pll_sel = SKL_DPLL2;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9199 break;
9200 default:
9201 DRM_ERROR("Incorrect port type\n");
9202 }
9203}
9204
96b7dfb7
S
9205static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9206 enum port port,
5cec258b 9207 struct intel_crtc_state *pipe_config)
96b7dfb7 9208{
3148ade7 9209 u32 temp, dpll_ctl1;
96b7dfb7
S
9210
9211 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9212 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9213
9214 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9215 case SKL_DPLL0:
9216 /*
9217 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9218 * of the shared DPLL framework and thus needs to be read out
9219 * separately
9220 */
9221 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9222 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9223 break;
96b7dfb7
S
9224 case SKL_DPLL1:
9225 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9226 break;
9227 case SKL_DPLL2:
9228 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9229 break;
9230 case SKL_DPLL3:
9231 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9232 break;
96b7dfb7
S
9233 }
9234}
9235
7d2c8175
DL
9236static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9237 enum port port,
5cec258b 9238 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9239{
9240 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9241
9242 switch (pipe_config->ddi_pll_sel) {
9243 case PORT_CLK_SEL_WRPLL1:
9244 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9245 break;
9246 case PORT_CLK_SEL_WRPLL2:
9247 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9248 break;
9249 }
9250}
9251
26804afd 9252static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9253 struct intel_crtc_state *pipe_config)
26804afd
DV
9254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9257 struct intel_shared_dpll *pll;
26804afd
DV
9258 enum port port;
9259 uint32_t tmp;
9260
9261 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9262
9263 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9264
96b7dfb7
S
9265 if (IS_SKYLAKE(dev))
9266 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9267 else if (IS_BROXTON(dev))
9268 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9269 else
9270 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9271
d452c5b6
DV
9272 if (pipe_config->shared_dpll >= 0) {
9273 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9274
9275 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9276 &pipe_config->dpll_hw_state));
9277 }
9278
26804afd
DV
9279 /*
9280 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9281 * DDI E. So just check whether this pipe is wired to DDI E and whether
9282 * the PCH transcoder is on.
9283 */
ca370455
DL
9284 if (INTEL_INFO(dev)->gen < 9 &&
9285 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9286 pipe_config->has_pch_encoder = true;
9287
9288 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9289 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9290 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9291
9292 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9293 }
9294}
9295
0e8ffe1b 9296static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9297 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9298{
9299 struct drm_device *dev = crtc->base.dev;
9300 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9301 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9302 uint32_t tmp;
9303
f458ebbc 9304 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9305 POWER_DOMAIN_PIPE(crtc->pipe)))
9306 return false;
9307
e143a21c 9308 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9309 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9310
eccb140b
DV
9311 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9312 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9313 enum pipe trans_edp_pipe;
9314 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9315 default:
9316 WARN(1, "unknown pipe linked to edp transcoder\n");
9317 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9318 case TRANS_DDI_EDP_INPUT_A_ON:
9319 trans_edp_pipe = PIPE_A;
9320 break;
9321 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9322 trans_edp_pipe = PIPE_B;
9323 break;
9324 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9325 trans_edp_pipe = PIPE_C;
9326 break;
9327 }
9328
9329 if (trans_edp_pipe == crtc->pipe)
9330 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9331 }
9332
f458ebbc 9333 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9334 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9335 return false;
9336
eccb140b 9337 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9338 if (!(tmp & PIPECONF_ENABLE))
9339 return false;
9340
26804afd 9341 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9342
1bd1bd80
DV
9343 intel_get_pipe_timings(crtc, pipe_config);
9344
a1b2278e
CK
9345 if (INTEL_INFO(dev)->gen >= 9) {
9346 skl_init_scalers(dev, crtc, pipe_config);
9347 }
9348
2fa2fe9a 9349 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f 9350 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9351 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9352 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9353 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9354 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9355 else
9356 MISSING_CASE(INTEL_INFO(dev)->gen);
9357
a1b2278e
CK
9358 } else {
9359 pipe_config->scaler_state.scaler_id = -1;
9360 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f 9361 }
88adfff1 9362
e59150dc
JB
9363 if (IS_HASWELL(dev))
9364 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9365 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9366
ebb69c95
CT
9367 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9368 pipe_config->pixel_multiplier =
9369 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9370 } else {
9371 pipe_config->pixel_multiplier = 1;
9372 }
6c49f241 9373
0e8ffe1b
DV
9374 return true;
9375}
9376
560b85bb
CW
9377static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9378{
9379 struct drm_device *dev = crtc->dev;
9380 struct drm_i915_private *dev_priv = dev->dev_private;
9381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9382 uint32_t cntl = 0, size = 0;
560b85bb 9383
dc41c154 9384 if (base) {
3dd512fb
MR
9385 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9386 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9387 unsigned int stride = roundup_pow_of_two(width) * 4;
9388
9389 switch (stride) {
9390 default:
9391 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9392 width, stride);
9393 stride = 256;
9394 /* fallthrough */
9395 case 256:
9396 case 512:
9397 case 1024:
9398 case 2048:
9399 break;
4b0e333e
CW
9400 }
9401
dc41c154
VS
9402 cntl |= CURSOR_ENABLE |
9403 CURSOR_GAMMA_ENABLE |
9404 CURSOR_FORMAT_ARGB |
9405 CURSOR_STRIDE(stride);
9406
9407 size = (height << 12) | width;
4b0e333e 9408 }
560b85bb 9409
dc41c154
VS
9410 if (intel_crtc->cursor_cntl != 0 &&
9411 (intel_crtc->cursor_base != base ||
9412 intel_crtc->cursor_size != size ||
9413 intel_crtc->cursor_cntl != cntl)) {
9414 /* On these chipsets we can only modify the base/size/stride
9415 * whilst the cursor is disabled.
9416 */
9417 I915_WRITE(_CURACNTR, 0);
4b0e333e 9418 POSTING_READ(_CURACNTR);
dc41c154 9419 intel_crtc->cursor_cntl = 0;
4b0e333e 9420 }
560b85bb 9421
99d1f387 9422 if (intel_crtc->cursor_base != base) {
9db4a9c7 9423 I915_WRITE(_CURABASE, base);
99d1f387
VS
9424 intel_crtc->cursor_base = base;
9425 }
4726e0b0 9426
dc41c154
VS
9427 if (intel_crtc->cursor_size != size) {
9428 I915_WRITE(CURSIZE, size);
9429 intel_crtc->cursor_size = size;
4b0e333e 9430 }
560b85bb 9431
4b0e333e 9432 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9433 I915_WRITE(_CURACNTR, cntl);
9434 POSTING_READ(_CURACNTR);
4b0e333e 9435 intel_crtc->cursor_cntl = cntl;
560b85bb 9436 }
560b85bb
CW
9437}
9438
560b85bb 9439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9440{
9441 struct drm_device *dev = crtc->dev;
9442 struct drm_i915_private *dev_priv = dev->dev_private;
9443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9444 int pipe = intel_crtc->pipe;
4b0e333e
CW
9445 uint32_t cntl;
9446
9447 cntl = 0;
9448 if (base) {
9449 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9450 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9451 case 64:
9452 cntl |= CURSOR_MODE_64_ARGB_AX;
9453 break;
9454 case 128:
9455 cntl |= CURSOR_MODE_128_ARGB_AX;
9456 break;
9457 case 256:
9458 cntl |= CURSOR_MODE_256_ARGB_AX;
9459 break;
9460 default:
3dd512fb 9461 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9462 return;
65a21cd6 9463 }
4b0e333e 9464 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9465
9466 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9467 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9468 }
65a21cd6 9469
8e7d688b 9470 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9471 cntl |= CURSOR_ROTATE_180;
9472
4b0e333e
CW
9473 if (intel_crtc->cursor_cntl != cntl) {
9474 I915_WRITE(CURCNTR(pipe), cntl);
9475 POSTING_READ(CURCNTR(pipe));
9476 intel_crtc->cursor_cntl = cntl;
65a21cd6 9477 }
4b0e333e 9478
65a21cd6 9479 /* and commit changes on next vblank */
5efb3e28
VS
9480 I915_WRITE(CURBASE(pipe), base);
9481 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9482
9483 intel_crtc->cursor_base = base;
65a21cd6
JB
9484}
9485
cda4b7d3 9486/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9487static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9488 bool on)
cda4b7d3
CW
9489{
9490 struct drm_device *dev = crtc->dev;
9491 struct drm_i915_private *dev_priv = dev->dev_private;
9492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9493 int pipe = intel_crtc->pipe;
3d7d6510
MR
9494 int x = crtc->cursor_x;
9495 int y = crtc->cursor_y;
d6e4db15 9496 u32 base = 0, pos = 0;
cda4b7d3 9497
d6e4db15 9498 if (on)
cda4b7d3 9499 base = intel_crtc->cursor_addr;
cda4b7d3 9500
6e3c9717 9501 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9502 base = 0;
9503
6e3c9717 9504 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9505 base = 0;
9506
9507 if (x < 0) {
3dd512fb 9508 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9512 x = -x;
9513 }
9514 pos |= x << CURSOR_X_SHIFT;
9515
9516 if (y < 0) {
3dd512fb 9517 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9518 base = 0;
9519
9520 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9521 y = -y;
9522 }
9523 pos |= y << CURSOR_Y_SHIFT;
9524
4b0e333e 9525 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9526 return;
9527
5efb3e28
VS
9528 I915_WRITE(CURPOS(pipe), pos);
9529
4398ad45
VS
9530 /* ILK+ do this automagically */
9531 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9532 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9533 base += (intel_crtc->base.cursor->state->crtc_h *
9534 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9535 }
9536
8ac54669 9537 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9538 i845_update_cursor(crtc, base);
9539 else
9540 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9541}
9542
dc41c154
VS
9543static bool cursor_size_ok(struct drm_device *dev,
9544 uint32_t width, uint32_t height)
9545{
9546 if (width == 0 || height == 0)
9547 return false;
9548
9549 /*
9550 * 845g/865g are special in that they are only limited by
9551 * the width of their cursors, the height is arbitrary up to
9552 * the precision of the register. Everything else requires
9553 * square cursors, limited to a few power-of-two sizes.
9554 */
9555 if (IS_845G(dev) || IS_I865G(dev)) {
9556 if ((width & 63) != 0)
9557 return false;
9558
9559 if (width > (IS_845G(dev) ? 64 : 512))
9560 return false;
9561
9562 if (height > 1023)
9563 return false;
9564 } else {
9565 switch (width | height) {
9566 case 256:
9567 case 128:
9568 if (IS_GEN2(dev))
9569 return false;
9570 case 64:
9571 break;
9572 default:
9573 return false;
9574 }
9575 }
9576
9577 return true;
9578}
9579
79e53945 9580static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9581 u16 *blue, uint32_t start, uint32_t size)
79e53945 9582{
7203425a 9583 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9584 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9585
7203425a 9586 for (i = start; i < end; i++) {
79e53945
JB
9587 intel_crtc->lut_r[i] = red[i] >> 8;
9588 intel_crtc->lut_g[i] = green[i] >> 8;
9589 intel_crtc->lut_b[i] = blue[i] >> 8;
9590 }
9591
9592 intel_crtc_load_lut(crtc);
9593}
9594
79e53945
JB
9595/* VESA 640x480x72Hz mode to set on the pipe */
9596static struct drm_display_mode load_detect_mode = {
9597 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9598 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9599};
9600
a8bb6818
DV
9601struct drm_framebuffer *
9602__intel_framebuffer_create(struct drm_device *dev,
9603 struct drm_mode_fb_cmd2 *mode_cmd,
9604 struct drm_i915_gem_object *obj)
d2dff872
CW
9605{
9606 struct intel_framebuffer *intel_fb;
9607 int ret;
9608
9609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9610 if (!intel_fb) {
6ccb81f2 9611 drm_gem_object_unreference(&obj->base);
d2dff872
CW
9612 return ERR_PTR(-ENOMEM);
9613 }
9614
9615 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
9616 if (ret)
9617 goto err;
d2dff872
CW
9618
9619 return &intel_fb->base;
dd4916c5 9620err:
6ccb81f2 9621 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
9622 kfree(intel_fb);
9623
9624 return ERR_PTR(ret);
d2dff872
CW
9625}
9626
b5ea642a 9627static struct drm_framebuffer *
a8bb6818
DV
9628intel_framebuffer_create(struct drm_device *dev,
9629 struct drm_mode_fb_cmd2 *mode_cmd,
9630 struct drm_i915_gem_object *obj)
9631{
9632 struct drm_framebuffer *fb;
9633 int ret;
9634
9635 ret = i915_mutex_lock_interruptible(dev);
9636 if (ret)
9637 return ERR_PTR(ret);
9638 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9639 mutex_unlock(&dev->struct_mutex);
9640
9641 return fb;
9642}
9643
d2dff872
CW
9644static u32
9645intel_framebuffer_pitch_for_width(int width, int bpp)
9646{
9647 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9648 return ALIGN(pitch, 64);
9649}
9650
9651static u32
9652intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9653{
9654 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9655 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9656}
9657
9658static struct drm_framebuffer *
9659intel_framebuffer_create_for_mode(struct drm_device *dev,
9660 struct drm_display_mode *mode,
9661 int depth, int bpp)
9662{
9663 struct drm_i915_gem_object *obj;
0fed39bd 9664 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
9665
9666 obj = i915_gem_alloc_object(dev,
9667 intel_framebuffer_size_for_mode(mode, bpp));
9668 if (obj == NULL)
9669 return ERR_PTR(-ENOMEM);
9670
9671 mode_cmd.width = mode->hdisplay;
9672 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9673 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9674 bpp);
5ca0c34a 9675 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
9676
9677 return intel_framebuffer_create(dev, &mode_cmd, obj);
9678}
9679
9680static struct drm_framebuffer *
9681mode_fits_in_fbdev(struct drm_device *dev,
9682 struct drm_display_mode *mode)
9683{
4520f53a 9684#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
9685 struct drm_i915_private *dev_priv = dev->dev_private;
9686 struct drm_i915_gem_object *obj;
9687 struct drm_framebuffer *fb;
9688
4c0e5528 9689 if (!dev_priv->fbdev)
d2dff872
CW
9690 return NULL;
9691
4c0e5528 9692 if (!dev_priv->fbdev->fb)
d2dff872
CW
9693 return NULL;
9694
4c0e5528
DV
9695 obj = dev_priv->fbdev->fb->obj;
9696 BUG_ON(!obj);
9697
8bcd4553 9698 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
9699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9700 fb->bits_per_pixel))
d2dff872
CW
9701 return NULL;
9702
01f2c773 9703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9704 return NULL;
9705
9706 return fb;
4520f53a
DV
9707#else
9708 return NULL;
9709#endif
d2dff872
CW
9710}
9711
d2434ab7 9712bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9713 struct drm_display_mode *mode,
51fd371b
RC
9714 struct intel_load_detect_pipe *old,
9715 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9716{
9717 struct intel_crtc *intel_crtc;
d2434ab7
DV
9718 struct intel_encoder *intel_encoder =
9719 intel_attached_encoder(connector);
79e53945 9720 struct drm_crtc *possible_crtc;
4ef69c7a 9721 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9722 struct drm_crtc *crtc = NULL;
9723 struct drm_device *dev = encoder->dev;
94352cf9 9724 struct drm_framebuffer *fb;
51fd371b 9725 struct drm_mode_config *config = &dev->mode_config;
83a57153 9726 struct drm_atomic_state *state = NULL;
944b0c76 9727 struct drm_connector_state *connector_state;
51fd371b 9728 int ret, i = -1;
79e53945 9729
d2dff872 9730 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9731 connector->base.id, connector->name,
8e329a03 9732 encoder->base.id, encoder->name);
d2dff872 9733
51fd371b
RC
9734retry:
9735 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9736 if (ret)
9737 goto fail_unlock;
6e9f798d 9738
79e53945
JB
9739 /*
9740 * Algorithm gets a little messy:
7a5e4805 9741 *
79e53945
JB
9742 * - if the connector already has an assigned crtc, use it (but make
9743 * sure it's on first)
7a5e4805 9744 *
79e53945
JB
9745 * - try to find the first unused crtc that can drive this connector,
9746 * and use that if we find one
79e53945
JB
9747 */
9748
9749 /* See if we already have a CRTC for this connector */
9750 if (encoder->crtc) {
9751 crtc = encoder->crtc;
8261b191 9752
51fd371b 9753 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
9754 if (ret)
9755 goto fail_unlock;
9756 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
9757 if (ret)
9758 goto fail_unlock;
7b24056b 9759
24218aac 9760 old->dpms_mode = connector->dpms;
8261b191
CW
9761 old->load_detect_temp = false;
9762
9763 /* Make sure the crtc and connector are running */
24218aac
DV
9764 if (connector->dpms != DRM_MODE_DPMS_ON)
9765 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 9766
7173188d 9767 return true;
79e53945
JB
9768 }
9769
9770 /* Find an unused one (if possible) */
70e1e0ec 9771 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9772 i++;
9773 if (!(encoder->possible_crtcs & (1 << i)))
9774 continue;
83d65738 9775 if (possible_crtc->state->enable)
a459249c
VS
9776 continue;
9777 /* This can occur when applying the pipe A quirk on resume. */
9778 if (to_intel_crtc(possible_crtc)->new_enabled)
9779 continue;
9780
9781 crtc = possible_crtc;
9782 break;
79e53945
JB
9783 }
9784
9785 /*
9786 * If we didn't find an unused CRTC, don't use any.
9787 */
9788 if (!crtc) {
7173188d 9789 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 9790 goto fail_unlock;
79e53945
JB
9791 }
9792
51fd371b
RC
9793 ret = drm_modeset_lock(&crtc->mutex, ctx);
9794 if (ret)
4d02e2de
DV
9795 goto fail_unlock;
9796 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9797 if (ret)
51fd371b 9798 goto fail_unlock;
fc303101
DV
9799 intel_encoder->new_crtc = to_intel_crtc(crtc);
9800 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
9801
9802 intel_crtc = to_intel_crtc(crtc);
412b61d8 9803 intel_crtc->new_enabled = true;
24218aac 9804 old->dpms_mode = connector->dpms;
8261b191 9805 old->load_detect_temp = true;
d2dff872 9806 old->release_fb = NULL;
79e53945 9807
83a57153
ACO
9808 state = drm_atomic_state_alloc(dev);
9809 if (!state)
9810 return false;
9811
9812 state->acquire_ctx = ctx;
9813
944b0c76
ACO
9814 connector_state = drm_atomic_get_connector_state(state, connector);
9815 if (IS_ERR(connector_state)) {
9816 ret = PTR_ERR(connector_state);
9817 goto fail;
9818 }
9819
9820 connector_state->crtc = crtc;
9821 connector_state->best_encoder = &intel_encoder->base;
9822
6492711d
CW
9823 if (!mode)
9824 mode = &load_detect_mode;
79e53945 9825
d2dff872
CW
9826 /* We need a framebuffer large enough to accommodate all accesses
9827 * that the plane may generate whilst we perform load detection.
9828 * We can not rely on the fbcon either being present (we get called
9829 * during its initialisation to detect all boot displays, or it may
9830 * not even exist) or that it is large enough to satisfy the
9831 * requested mode.
9832 */
94352cf9
DV
9833 fb = mode_fits_in_fbdev(dev, mode);
9834 if (fb == NULL) {
d2dff872 9835 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
9836 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9837 old->release_fb = fb;
d2dff872
CW
9838 } else
9839 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9840 if (IS_ERR(fb)) {
d2dff872 9841 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9842 goto fail;
79e53945 9843 }
79e53945 9844
83a57153 9845 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
6492711d 9846 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
9847 if (old->release_fb)
9848 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 9849 goto fail;
79e53945 9850 }
9128b040 9851 crtc->primary->crtc = crtc;
7173188d 9852
79e53945 9853 /* let the connector get through one full cycle before testing */
9d0498a2 9854 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 9855 return true;
412b61d8
VS
9856
9857 fail:
83d65738 9858 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 9859fail_unlock:
83a57153
ACO
9860 if (state) {
9861 drm_atomic_state_free(state);
9862 state = NULL;
9863 }
9864
51fd371b
RC
9865 if (ret == -EDEADLK) {
9866 drm_modeset_backoff(ctx);
9867 goto retry;
9868 }
9869
412b61d8 9870 return false;
79e53945
JB
9871}
9872
d2434ab7 9873void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9874 struct intel_load_detect_pipe *old,
9875 struct drm_modeset_acquire_ctx *ctx)
79e53945 9876{
83a57153 9877 struct drm_device *dev = connector->dev;
d2434ab7
DV
9878 struct intel_encoder *intel_encoder =
9879 intel_attached_encoder(connector);
4ef69c7a 9880 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9881 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 9883 struct drm_atomic_state *state;
944b0c76 9884 struct drm_connector_state *connector_state;
79e53945 9885
d2dff872 9886 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9887 connector->base.id, connector->name,
8e329a03 9888 encoder->base.id, encoder->name);
d2dff872 9889
8261b191 9890 if (old->load_detect_temp) {
83a57153 9891 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
9892 if (!state)
9893 goto fail;
83a57153
ACO
9894
9895 state->acquire_ctx = ctx;
9896
944b0c76
ACO
9897 connector_state = drm_atomic_get_connector_state(state, connector);
9898 if (IS_ERR(connector_state))
9899 goto fail;
9900
fc303101
DV
9901 to_intel_connector(connector)->new_encoder = NULL;
9902 intel_encoder->new_crtc = NULL;
412b61d8 9903 intel_crtc->new_enabled = false;
944b0c76
ACO
9904
9905 connector_state->best_encoder = NULL;
9906 connector_state->crtc = NULL;
9907
83a57153
ACO
9908 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9909
9910 drm_atomic_state_free(state);
d2dff872 9911
36206361
DV
9912 if (old->release_fb) {
9913 drm_framebuffer_unregister_private(old->release_fb);
9914 drm_framebuffer_unreference(old->release_fb);
9915 }
d2dff872 9916
0622a53c 9917 return;
79e53945
JB
9918 }
9919
c751ce4f 9920 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9921 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9922 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
9923
9924 return;
9925fail:
9926 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9927 drm_atomic_state_free(state);
79e53945
JB
9928}
9929
da4a1efa 9930static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9931 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9932{
9933 struct drm_i915_private *dev_priv = dev->dev_private;
9934 u32 dpll = pipe_config->dpll_hw_state.dpll;
9935
9936 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9937 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9938 else if (HAS_PCH_SPLIT(dev))
9939 return 120000;
9940 else if (!IS_GEN2(dev))
9941 return 96000;
9942 else
9943 return 48000;
9944}
9945
79e53945 9946/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9947static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9948 struct intel_crtc_state *pipe_config)
79e53945 9949{
f1f644dc 9950 struct drm_device *dev = crtc->base.dev;
79e53945 9951 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 9952 int pipe = pipe_config->cpu_transcoder;
293623f7 9953 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
9954 u32 fp;
9955 intel_clock_t clock;
da4a1efa 9956 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9957
9958 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9959 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9960 else
293623f7 9961 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9962
9963 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
9964 if (IS_PINEVIEW(dev)) {
9965 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9966 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9967 } else {
9968 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9969 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9970 }
9971
a6c45cf0 9972 if (!IS_GEN2(dev)) {
f2b115e6
AJ
9973 if (IS_PINEVIEW(dev))
9974 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9975 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9976 else
9977 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9978 DPLL_FPA01_P1_POST_DIV_SHIFT);
9979
9980 switch (dpll & DPLL_MODE_MASK) {
9981 case DPLLB_MODE_DAC_SERIAL:
9982 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9983 5 : 10;
9984 break;
9985 case DPLLB_MODE_LVDS:
9986 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9987 7 : 14;
9988 break;
9989 default:
28c97730 9990 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9991 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9992 return;
79e53945
JB
9993 }
9994
ac58c3f0 9995 if (IS_PINEVIEW(dev))
da4a1efa 9996 pineview_clock(refclk, &clock);
ac58c3f0 9997 else
da4a1efa 9998 i9xx_clock(refclk, &clock);
79e53945 9999 } else {
0fb58223 10000 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10001 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10002
10003 if (is_lvds) {
10004 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10005 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10006
10007 if (lvds & LVDS_CLKB_POWER_UP)
10008 clock.p2 = 7;
10009 else
10010 clock.p2 = 14;
79e53945
JB
10011 } else {
10012 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10013 clock.p1 = 2;
10014 else {
10015 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10016 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10017 }
10018 if (dpll & PLL_P2_DIVIDE_BY_4)
10019 clock.p2 = 4;
10020 else
10021 clock.p2 = 2;
79e53945 10022 }
da4a1efa
VS
10023
10024 i9xx_clock(refclk, &clock);
79e53945
JB
10025 }
10026
18442d08
VS
10027 /*
10028 * This value includes pixel_multiplier. We will use
241bfc38 10029 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10030 * encoder's get_config() function.
10031 */
10032 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10033}
10034
6878da05
VS
10035int intel_dotclock_calculate(int link_freq,
10036 const struct intel_link_m_n *m_n)
f1f644dc 10037{
f1f644dc
JB
10038 /*
10039 * The calculation for the data clock is:
1041a02f 10040 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10041 * But we want to avoid losing precison if possible, so:
1041a02f 10042 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10043 *
10044 * and the link clock is simpler:
1041a02f 10045 * link_clock = (m * link_clock) / n
f1f644dc
JB
10046 */
10047
6878da05
VS
10048 if (!m_n->link_n)
10049 return 0;
f1f644dc 10050
6878da05
VS
10051 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10052}
f1f644dc 10053
18442d08 10054static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10055 struct intel_crtc_state *pipe_config)
6878da05
VS
10056{
10057 struct drm_device *dev = crtc->base.dev;
79e53945 10058
18442d08
VS
10059 /* read out port_clock from the DPLL */
10060 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10061
f1f644dc 10062 /*
18442d08 10063 * This value does not include pixel_multiplier.
241bfc38 10064 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10065 * agree once we know their relationship in the encoder's
10066 * get_config() function.
79e53945 10067 */
2d112de7 10068 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10069 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10070 &pipe_config->fdi_m_n);
79e53945
JB
10071}
10072
10073/** Returns the currently programmed mode of the given pipe. */
10074struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10075 struct drm_crtc *crtc)
10076{
548f245b 10077 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10079 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10080 struct drm_display_mode *mode;
5cec258b 10081 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10082 int htot = I915_READ(HTOTAL(cpu_transcoder));
10083 int hsync = I915_READ(HSYNC(cpu_transcoder));
10084 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10085 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10086 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10087
10088 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10089 if (!mode)
10090 return NULL;
10091
f1f644dc
JB
10092 /*
10093 * Construct a pipe_config sufficient for getting the clock info
10094 * back out of crtc_clock_get.
10095 *
10096 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10097 * to use a real value here instead.
10098 */
293623f7 10099 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10100 pipe_config.pixel_multiplier = 1;
293623f7
VS
10101 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10102 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10103 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10104 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10105
773ae034 10106 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10107 mode->hdisplay = (htot & 0xffff) + 1;
10108 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10109 mode->hsync_start = (hsync & 0xffff) + 1;
10110 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10111 mode->vdisplay = (vtot & 0xffff) + 1;
10112 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10113 mode->vsync_start = (vsync & 0xffff) + 1;
10114 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10115
10116 drm_mode_set_name(mode);
79e53945
JB
10117
10118 return mode;
10119}
10120
652c393a
JB
10121static void intel_decrease_pllclock(struct drm_crtc *crtc)
10122{
10123 struct drm_device *dev = crtc->dev;
fbee40df 10124 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10126
baff296c 10127 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10128 return;
10129
10130 if (!dev_priv->lvds_downclock_avail)
10131 return;
10132
10133 /*
10134 * Since this is called by a timer, we should never get here in
10135 * the manual case.
10136 */
10137 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10138 int pipe = intel_crtc->pipe;
10139 int dpll_reg = DPLL(pipe);
10140 int dpll;
f6e5b160 10141
44d98a61 10142 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10143
8ac5a6d5 10144 assert_panel_unlocked(dev_priv, pipe);
652c393a 10145
dc257cf1 10146 dpll = I915_READ(dpll_reg);
652c393a
JB
10147 dpll |= DISPLAY_RATE_SELECT_FPA1;
10148 I915_WRITE(dpll_reg, dpll);
9d0498a2 10149 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10150 dpll = I915_READ(dpll_reg);
10151 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10152 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10153 }
10154
10155}
10156
f047e395
CW
10157void intel_mark_busy(struct drm_device *dev)
10158{
c67a470b
PZ
10159 struct drm_i915_private *dev_priv = dev->dev_private;
10160
f62a0076
CW
10161 if (dev_priv->mm.busy)
10162 return;
10163
43694d69 10164 intel_runtime_pm_get(dev_priv);
c67a470b 10165 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10166 if (INTEL_INFO(dev)->gen >= 6)
10167 gen6_rps_busy(dev_priv);
f62a0076 10168 dev_priv->mm.busy = true;
f047e395
CW
10169}
10170
10171void intel_mark_idle(struct drm_device *dev)
652c393a 10172{
c67a470b 10173 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10174 struct drm_crtc *crtc;
652c393a 10175
f62a0076
CW
10176 if (!dev_priv->mm.busy)
10177 return;
10178
10179 dev_priv->mm.busy = false;
10180
70e1e0ec 10181 for_each_crtc(dev, crtc) {
f4510a27 10182 if (!crtc->primary->fb)
652c393a
JB
10183 continue;
10184
725a5b54 10185 intel_decrease_pllclock(crtc);
652c393a 10186 }
b29c19b6 10187
3d13ef2e 10188 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10189 gen6_rps_idle(dev->dev_private);
bb4cdd53 10190
43694d69 10191 intel_runtime_pm_put(dev_priv);
652c393a
JB
10192}
10193
f5de6e07
ACO
10194static void intel_crtc_set_state(struct intel_crtc *crtc,
10195 struct intel_crtc_state *crtc_state)
10196{
10197 kfree(crtc->config);
10198 crtc->config = crtc_state;
16f3f658 10199 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
10200}
10201
79e53945
JB
10202static void intel_crtc_destroy(struct drm_crtc *crtc)
10203{
10204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10205 struct drm_device *dev = crtc->dev;
10206 struct intel_unpin_work *work;
67e77c5a 10207
5e2d7afc 10208 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10209 work = intel_crtc->unpin_work;
10210 intel_crtc->unpin_work = NULL;
5e2d7afc 10211 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10212
10213 if (work) {
10214 cancel_work_sync(&work->work);
10215 kfree(work);
10216 }
79e53945 10217
f5de6e07 10218 intel_crtc_set_state(intel_crtc, NULL);
79e53945 10219 drm_crtc_cleanup(crtc);
67e77c5a 10220
79e53945
JB
10221 kfree(intel_crtc);
10222}
10223
6b95a207
KH
10224static void intel_unpin_work_fn(struct work_struct *__work)
10225{
10226 struct intel_unpin_work *work =
10227 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10228 struct drm_device *dev = work->crtc->dev;
f99d7069 10229 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10230
b4a98e57 10231 mutex_lock(&dev->struct_mutex);
82bc3b2d 10232 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10233 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10234
7ff0ebcc 10235 intel_fbc_update(dev);
f06cc1b9
JH
10236
10237 if (work->flip_queued_req)
146d84f0 10238 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10239 mutex_unlock(&dev->struct_mutex);
10240
f99d7069 10241 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10242 drm_framebuffer_unreference(work->old_fb);
f99d7069 10243
b4a98e57
CW
10244 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10245 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10246
6b95a207
KH
10247 kfree(work);
10248}
10249
1afe3e9d 10250static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10251 struct drm_crtc *crtc)
6b95a207 10252{
6b95a207
KH
10253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10254 struct intel_unpin_work *work;
6b95a207
KH
10255 unsigned long flags;
10256
10257 /* Ignore early vblank irqs */
10258 if (intel_crtc == NULL)
10259 return;
10260
f326038a
DV
10261 /*
10262 * This is called both by irq handlers and the reset code (to complete
10263 * lost pageflips) so needs the full irqsave spinlocks.
10264 */
6b95a207
KH
10265 spin_lock_irqsave(&dev->event_lock, flags);
10266 work = intel_crtc->unpin_work;
e7d841ca
CW
10267
10268 /* Ensure we don't miss a work->pending update ... */
10269 smp_rmb();
10270
10271 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10272 spin_unlock_irqrestore(&dev->event_lock, flags);
10273 return;
10274 }
10275
d6bbafa1 10276 page_flip_completed(intel_crtc);
0af7e4df 10277
6b95a207 10278 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10279}
10280
1afe3e9d
JB
10281void intel_finish_page_flip(struct drm_device *dev, int pipe)
10282{
fbee40df 10283 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10284 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10285
49b14a5c 10286 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10287}
10288
10289void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10290{
fbee40df 10291 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10292 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10293
49b14a5c 10294 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10295}
10296
75f7f3ec
VS
10297/* Is 'a' after or equal to 'b'? */
10298static bool g4x_flip_count_after_eq(u32 a, u32 b)
10299{
10300 return !((a - b) & 0x80000000);
10301}
10302
10303static bool page_flip_finished(struct intel_crtc *crtc)
10304{
10305 struct drm_device *dev = crtc->base.dev;
10306 struct drm_i915_private *dev_priv = dev->dev_private;
10307
bdfa7542
VS
10308 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10309 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10310 return true;
10311
75f7f3ec
VS
10312 /*
10313 * The relevant registers doen't exist on pre-ctg.
10314 * As the flip done interrupt doesn't trigger for mmio
10315 * flips on gmch platforms, a flip count check isn't
10316 * really needed there. But since ctg has the registers,
10317 * include it in the check anyway.
10318 */
10319 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10320 return true;
10321
10322 /*
10323 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10324 * used the same base address. In that case the mmio flip might
10325 * have completed, but the CS hasn't even executed the flip yet.
10326 *
10327 * A flip count check isn't enough as the CS might have updated
10328 * the base address just after start of vblank, but before we
10329 * managed to process the interrupt. This means we'd complete the
10330 * CS flip too soon.
10331 *
10332 * Combining both checks should get us a good enough result. It may
10333 * still happen that the CS flip has been executed, but has not
10334 * yet actually completed. But in case the base address is the same
10335 * anyway, we don't really care.
10336 */
10337 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10338 crtc->unpin_work->gtt_offset &&
10339 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10340 crtc->unpin_work->flip_count);
10341}
10342
6b95a207
KH
10343void intel_prepare_page_flip(struct drm_device *dev, int plane)
10344{
fbee40df 10345 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10346 struct intel_crtc *intel_crtc =
10347 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10348 unsigned long flags;
10349
f326038a
DV
10350
10351 /*
10352 * This is called both by irq handlers and the reset code (to complete
10353 * lost pageflips) so needs the full irqsave spinlocks.
10354 *
10355 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10356 * generate a page-flip completion irq, i.e. every modeset
10357 * is also accompanied by a spurious intel_prepare_page_flip().
10358 */
6b95a207 10359 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10360 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10361 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10362 spin_unlock_irqrestore(&dev->event_lock, flags);
10363}
10364
eba905b2 10365static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10366{
10367 /* Ensure that the work item is consistent when activating it ... */
10368 smp_wmb();
10369 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10370 /* and that it is marked active as soon as the irq could fire. */
10371 smp_wmb();
10372}
10373
8c9f3aaf
JB
10374static int intel_gen2_queue_flip(struct drm_device *dev,
10375 struct drm_crtc *crtc,
10376 struct drm_framebuffer *fb,
ed8d1975 10377 struct drm_i915_gem_object *obj,
a4872ba6 10378 struct intel_engine_cs *ring,
ed8d1975 10379 uint32_t flags)
8c9f3aaf 10380{
8c9f3aaf 10381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10382 u32 flip_mask;
10383 int ret;
10384
6d90c952 10385 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10386 if (ret)
4fa62c89 10387 return ret;
8c9f3aaf
JB
10388
10389 /* Can't queue multiple flips, so wait for the previous
10390 * one to finish before executing the next.
10391 */
10392 if (intel_crtc->plane)
10393 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10394 else
10395 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10396 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10397 intel_ring_emit(ring, MI_NOOP);
10398 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10399 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10400 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10401 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10402 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10403
10404 intel_mark_page_flip_active(intel_crtc);
09246732 10405 __intel_ring_advance(ring);
83d4092b 10406 return 0;
8c9f3aaf
JB
10407}
10408
10409static int intel_gen3_queue_flip(struct drm_device *dev,
10410 struct drm_crtc *crtc,
10411 struct drm_framebuffer *fb,
ed8d1975 10412 struct drm_i915_gem_object *obj,
a4872ba6 10413 struct intel_engine_cs *ring,
ed8d1975 10414 uint32_t flags)
8c9f3aaf 10415{
8c9f3aaf 10416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10417 u32 flip_mask;
10418 int ret;
10419
6d90c952 10420 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10421 if (ret)
4fa62c89 10422 return ret;
8c9f3aaf
JB
10423
10424 if (intel_crtc->plane)
10425 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10426 else
10427 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10428 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10429 intel_ring_emit(ring, MI_NOOP);
10430 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10431 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10432 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10433 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10434 intel_ring_emit(ring, MI_NOOP);
10435
e7d841ca 10436 intel_mark_page_flip_active(intel_crtc);
09246732 10437 __intel_ring_advance(ring);
83d4092b 10438 return 0;
8c9f3aaf
JB
10439}
10440
10441static int intel_gen4_queue_flip(struct drm_device *dev,
10442 struct drm_crtc *crtc,
10443 struct drm_framebuffer *fb,
ed8d1975 10444 struct drm_i915_gem_object *obj,
a4872ba6 10445 struct intel_engine_cs *ring,
ed8d1975 10446 uint32_t flags)
8c9f3aaf
JB
10447{
10448 struct drm_i915_private *dev_priv = dev->dev_private;
10449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10450 uint32_t pf, pipesrc;
10451 int ret;
10452
6d90c952 10453 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10454 if (ret)
4fa62c89 10455 return ret;
8c9f3aaf
JB
10456
10457 /* i965+ uses the linear or tiled offsets from the
10458 * Display Registers (which do not change across a page-flip)
10459 * so we need only reprogram the base address.
10460 */
6d90c952
DV
10461 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10462 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10463 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10464 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10465 obj->tiling_mode);
8c9f3aaf
JB
10466
10467 /* XXX Enabling the panel-fitter across page-flip is so far
10468 * untested on non-native modes, so ignore it for now.
10469 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10470 */
10471 pf = 0;
10472 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10473 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10474
10475 intel_mark_page_flip_active(intel_crtc);
09246732 10476 __intel_ring_advance(ring);
83d4092b 10477 return 0;
8c9f3aaf
JB
10478}
10479
10480static int intel_gen6_queue_flip(struct drm_device *dev,
10481 struct drm_crtc *crtc,
10482 struct drm_framebuffer *fb,
ed8d1975 10483 struct drm_i915_gem_object *obj,
a4872ba6 10484 struct intel_engine_cs *ring,
ed8d1975 10485 uint32_t flags)
8c9f3aaf
JB
10486{
10487 struct drm_i915_private *dev_priv = dev->dev_private;
10488 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10489 uint32_t pf, pipesrc;
10490 int ret;
10491
6d90c952 10492 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10493 if (ret)
4fa62c89 10494 return ret;
8c9f3aaf 10495
6d90c952
DV
10496 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10497 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10498 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10499 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10500
dc257cf1
DV
10501 /* Contrary to the suggestions in the documentation,
10502 * "Enable Panel Fitter" does not seem to be required when page
10503 * flipping with a non-native mode, and worse causes a normal
10504 * modeset to fail.
10505 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10506 */
10507 pf = 0;
8c9f3aaf 10508 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10509 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10510
10511 intel_mark_page_flip_active(intel_crtc);
09246732 10512 __intel_ring_advance(ring);
83d4092b 10513 return 0;
8c9f3aaf
JB
10514}
10515
7c9017e5
JB
10516static int intel_gen7_queue_flip(struct drm_device *dev,
10517 struct drm_crtc *crtc,
10518 struct drm_framebuffer *fb,
ed8d1975 10519 struct drm_i915_gem_object *obj,
a4872ba6 10520 struct intel_engine_cs *ring,
ed8d1975 10521 uint32_t flags)
7c9017e5 10522{
7c9017e5 10523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10524 uint32_t plane_bit = 0;
ffe74d75
CW
10525 int len, ret;
10526
eba905b2 10527 switch (intel_crtc->plane) {
cb05d8de
DV
10528 case PLANE_A:
10529 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10530 break;
10531 case PLANE_B:
10532 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10533 break;
10534 case PLANE_C:
10535 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10536 break;
10537 default:
10538 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10539 return -ENODEV;
cb05d8de
DV
10540 }
10541
ffe74d75 10542 len = 4;
f476828a 10543 if (ring->id == RCS) {
ffe74d75 10544 len += 6;
f476828a
DL
10545 /*
10546 * On Gen 8, SRM is now taking an extra dword to accommodate
10547 * 48bits addresses, and we need a NOOP for the batch size to
10548 * stay even.
10549 */
10550 if (IS_GEN8(dev))
10551 len += 2;
10552 }
ffe74d75 10553
f66fab8e
VS
10554 /*
10555 * BSpec MI_DISPLAY_FLIP for IVB:
10556 * "The full packet must be contained within the same cache line."
10557 *
10558 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10559 * cacheline, if we ever start emitting more commands before
10560 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10561 * then do the cacheline alignment, and finally emit the
10562 * MI_DISPLAY_FLIP.
10563 */
10564 ret = intel_ring_cacheline_align(ring);
10565 if (ret)
4fa62c89 10566 return ret;
f66fab8e 10567
ffe74d75 10568 ret = intel_ring_begin(ring, len);
7c9017e5 10569 if (ret)
4fa62c89 10570 return ret;
7c9017e5 10571
ffe74d75
CW
10572 /* Unmask the flip-done completion message. Note that the bspec says that
10573 * we should do this for both the BCS and RCS, and that we must not unmask
10574 * more than one flip event at any time (or ensure that one flip message
10575 * can be sent by waiting for flip-done prior to queueing new flips).
10576 * Experimentation says that BCS works despite DERRMR masking all
10577 * flip-done completion events and that unmasking all planes at once
10578 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10579 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10580 */
10581 if (ring->id == RCS) {
10582 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10583 intel_ring_emit(ring, DERRMR);
10584 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10585 DERRMR_PIPEB_PRI_FLIP_DONE |
10586 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
10587 if (IS_GEN8(dev))
10588 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10589 MI_SRM_LRM_GLOBAL_GTT);
10590 else
10591 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10592 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
10593 intel_ring_emit(ring, DERRMR);
10594 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
10595 if (IS_GEN8(dev)) {
10596 intel_ring_emit(ring, 0);
10597 intel_ring_emit(ring, MI_NOOP);
10598 }
ffe74d75
CW
10599 }
10600
cb05d8de 10601 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 10602 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 10603 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 10604 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
10605
10606 intel_mark_page_flip_active(intel_crtc);
09246732 10607 __intel_ring_advance(ring);
83d4092b 10608 return 0;
7c9017e5
JB
10609}
10610
84c33a64
SG
10611static bool use_mmio_flip(struct intel_engine_cs *ring,
10612 struct drm_i915_gem_object *obj)
10613{
10614 /*
10615 * This is not being used for older platforms, because
10616 * non-availability of flip done interrupt forces us to use
10617 * CS flips. Older platforms derive flip done using some clever
10618 * tricks involving the flip_pending status bits and vblank irqs.
10619 * So using MMIO flips there would disrupt this mechanism.
10620 */
10621
8e09bf83
CW
10622 if (ring == NULL)
10623 return true;
10624
84c33a64
SG
10625 if (INTEL_INFO(ring->dev)->gen < 5)
10626 return false;
10627
10628 if (i915.use_mmio_flip < 0)
10629 return false;
10630 else if (i915.use_mmio_flip > 0)
10631 return true;
14bf993e
OM
10632 else if (i915.enable_execlists)
10633 return true;
84c33a64 10634 else
41c52415 10635 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
10636}
10637
ff944564
DL
10638static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10639{
10640 struct drm_device *dev = intel_crtc->base.dev;
10641 struct drm_i915_private *dev_priv = dev->dev_private;
10642 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
10643 const enum pipe pipe = intel_crtc->pipe;
10644 u32 ctl, stride;
10645
10646 ctl = I915_READ(PLANE_CTL(pipe, 0));
10647 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
10648 switch (fb->modifier[0]) {
10649 case DRM_FORMAT_MOD_NONE:
10650 break;
10651 case I915_FORMAT_MOD_X_TILED:
ff944564 10652 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
10653 break;
10654 case I915_FORMAT_MOD_Y_TILED:
10655 ctl |= PLANE_CTL_TILED_Y;
10656 break;
10657 case I915_FORMAT_MOD_Yf_TILED:
10658 ctl |= PLANE_CTL_TILED_YF;
10659 break;
10660 default:
10661 MISSING_CASE(fb->modifier[0]);
10662 }
ff944564
DL
10663
10664 /*
10665 * The stride is either expressed as a multiple of 64 bytes chunks for
10666 * linear buffers or in number of tiles for tiled buffers.
10667 */
2ebef630
TU
10668 stride = fb->pitches[0] /
10669 intel_fb_stride_alignment(dev, fb->modifier[0],
10670 fb->pixel_format);
ff944564
DL
10671
10672 /*
10673 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10674 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10675 */
10676 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10677 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10678
10679 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10680 POSTING_READ(PLANE_SURF(pipe, 0));
10681}
10682
10683static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
10684{
10685 struct drm_device *dev = intel_crtc->base.dev;
10686 struct drm_i915_private *dev_priv = dev->dev_private;
10687 struct intel_framebuffer *intel_fb =
10688 to_intel_framebuffer(intel_crtc->base.primary->fb);
10689 struct drm_i915_gem_object *obj = intel_fb->obj;
10690 u32 dspcntr;
10691 u32 reg;
10692
84c33a64
SG
10693 reg = DSPCNTR(intel_crtc->plane);
10694 dspcntr = I915_READ(reg);
10695
c5d97472
DL
10696 if (obj->tiling_mode != I915_TILING_NONE)
10697 dspcntr |= DISPPLANE_TILED;
10698 else
10699 dspcntr &= ~DISPPLANE_TILED;
10700
84c33a64
SG
10701 I915_WRITE(reg, dspcntr);
10702
10703 I915_WRITE(DSPSURF(intel_crtc->plane),
10704 intel_crtc->unpin_work->gtt_offset);
10705 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 10706
ff944564
DL
10707}
10708
10709/*
10710 * XXX: This is the temporary way to update the plane registers until we get
10711 * around to using the usual plane update functions for MMIO flips
10712 */
10713static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10714{
10715 struct drm_device *dev = intel_crtc->base.dev;
10716 bool atomic_update;
10717 u32 start_vbl_count;
10718
10719 intel_mark_page_flip_active(intel_crtc);
10720
10721 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10722
10723 if (INTEL_INFO(dev)->gen >= 9)
10724 skl_do_mmio_flip(intel_crtc);
10725 else
10726 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10727 ilk_do_mmio_flip(intel_crtc);
10728
9362c7c5
ACO
10729 if (atomic_update)
10730 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
10731}
10732
9362c7c5 10733static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 10734{
cc8c4cc2 10735 struct intel_crtc *crtc =
9362c7c5 10736 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 10737 struct intel_mmio_flip *mmio_flip;
84c33a64 10738
cc8c4cc2
JH
10739 mmio_flip = &crtc->mmio_flip;
10740 if (mmio_flip->req)
9c654818
JH
10741 WARN_ON(__i915_wait_request(mmio_flip->req,
10742 crtc->reset_counter,
10743 false, NULL, NULL) != 0);
84c33a64 10744
cc8c4cc2
JH
10745 intel_do_mmio_flip(crtc);
10746 if (mmio_flip->req) {
10747 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 10748 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
10749 mutex_unlock(&crtc->base.dev->struct_mutex);
10750 }
84c33a64
SG
10751}
10752
10753static int intel_queue_mmio_flip(struct drm_device *dev,
10754 struct drm_crtc *crtc,
10755 struct drm_framebuffer *fb,
10756 struct drm_i915_gem_object *obj,
10757 struct intel_engine_cs *ring,
10758 uint32_t flags)
10759{
84c33a64 10760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 10761
cc8c4cc2
JH
10762 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10763 obj->last_write_req);
536f5b5e
ACO
10764
10765 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 10766
84c33a64
SG
10767 return 0;
10768}
10769
8c9f3aaf
JB
10770static int intel_default_queue_flip(struct drm_device *dev,
10771 struct drm_crtc *crtc,
10772 struct drm_framebuffer *fb,
ed8d1975 10773 struct drm_i915_gem_object *obj,
a4872ba6 10774 struct intel_engine_cs *ring,
ed8d1975 10775 uint32_t flags)
8c9f3aaf
JB
10776{
10777 return -ENODEV;
10778}
10779
d6bbafa1
CW
10780static bool __intel_pageflip_stall_check(struct drm_device *dev,
10781 struct drm_crtc *crtc)
10782{
10783 struct drm_i915_private *dev_priv = dev->dev_private;
10784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10785 struct intel_unpin_work *work = intel_crtc->unpin_work;
10786 u32 addr;
10787
10788 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10789 return true;
10790
10791 if (!work->enable_stall_check)
10792 return false;
10793
10794 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
10795 if (work->flip_queued_req &&
10796 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
10797 return false;
10798
1e3feefd 10799 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
10800 }
10801
1e3feefd 10802 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
10803 return false;
10804
10805 /* Potential stall - if we see that the flip has happened,
10806 * assume a missed interrupt. */
10807 if (INTEL_INFO(dev)->gen >= 4)
10808 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10809 else
10810 addr = I915_READ(DSPADDR(intel_crtc->plane));
10811
10812 /* There is a potential issue here with a false positive after a flip
10813 * to the same address. We could address this by checking for a
10814 * non-incrementing frame counter.
10815 */
10816 return addr == work->gtt_offset;
10817}
10818
10819void intel_check_page_flip(struct drm_device *dev, int pipe)
10820{
10821 struct drm_i915_private *dev_priv = dev->dev_private;
10822 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 10824 struct intel_unpin_work *work;
f326038a 10825
6c51d46f 10826 WARN_ON(!in_interrupt());
d6bbafa1
CW
10827
10828 if (crtc == NULL)
10829 return;
10830
f326038a 10831 spin_lock(&dev->event_lock);
6ad790c0
CW
10832 work = intel_crtc->unpin_work;
10833 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 10834 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 10835 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 10836 page_flip_completed(intel_crtc);
6ad790c0 10837 work = NULL;
d6bbafa1 10838 }
6ad790c0
CW
10839 if (work != NULL &&
10840 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10841 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 10842 spin_unlock(&dev->event_lock);
d6bbafa1
CW
10843}
10844
6b95a207
KH
10845static int intel_crtc_page_flip(struct drm_crtc *crtc,
10846 struct drm_framebuffer *fb,
ed8d1975
KP
10847 struct drm_pending_vblank_event *event,
10848 uint32_t page_flip_flags)
6b95a207
KH
10849{
10850 struct drm_device *dev = crtc->dev;
10851 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 10852 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 10853 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 10854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 10855 struct drm_plane *primary = crtc->primary;
a071fa00 10856 enum pipe pipe = intel_crtc->pipe;
6b95a207 10857 struct intel_unpin_work *work;
a4872ba6 10858 struct intel_engine_cs *ring;
cf5d8a46 10859 bool mmio_flip;
52e68630 10860 int ret;
6b95a207 10861
2ff8fde1
MR
10862 /*
10863 * drm_mode_page_flip_ioctl() should already catch this, but double
10864 * check to be safe. In the future we may enable pageflipping from
10865 * a disabled primary plane.
10866 */
10867 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10868 return -EBUSY;
10869
e6a595d2 10870 /* Can't change pixel format via MI display flips. */
f4510a27 10871 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
10872 return -EINVAL;
10873
10874 /*
10875 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10876 * Note that pitch changes could also affect these register.
10877 */
10878 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
10879 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10880 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
10881 return -EINVAL;
10882
f900db47
CW
10883 if (i915_terminally_wedged(&dev_priv->gpu_error))
10884 goto out_hang;
10885
b14c5679 10886 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10887 if (work == NULL)
10888 return -ENOMEM;
10889
6b95a207 10890 work->event = event;
b4a98e57 10891 work->crtc = crtc;
ab8d6675 10892 work->old_fb = old_fb;
6b95a207
KH
10893 INIT_WORK(&work->work, intel_unpin_work_fn);
10894
87b6b101 10895 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10896 if (ret)
10897 goto free_work;
10898
6b95a207 10899 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 10900 spin_lock_irq(&dev->event_lock);
6b95a207 10901 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10902 /* Before declaring the flip queue wedged, check if
10903 * the hardware completed the operation behind our backs.
10904 */
10905 if (__intel_pageflip_stall_check(dev, crtc)) {
10906 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10907 page_flip_completed(intel_crtc);
10908 } else {
10909 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10910 spin_unlock_irq(&dev->event_lock);
468f0b44 10911
d6bbafa1
CW
10912 drm_crtc_vblank_put(crtc);
10913 kfree(work);
10914 return -EBUSY;
10915 }
6b95a207
KH
10916 }
10917 intel_crtc->unpin_work = work;
5e2d7afc 10918 spin_unlock_irq(&dev->event_lock);
6b95a207 10919
b4a98e57
CW
10920 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10921 flush_workqueue(dev_priv->wq);
10922
75dfca80 10923 /* Reference the objects for the scheduled work. */
ab8d6675 10924 drm_framebuffer_reference(work->old_fb);
05394f39 10925 drm_gem_object_reference(&obj->base);
6b95a207 10926
f4510a27 10927 crtc->primary->fb = fb;
afd65eb4 10928 update_state_fb(crtc->primary);
1ed1f968 10929
e1f99ce6 10930 work->pending_flip_obj = obj;
e1f99ce6 10931
89ed88ba
CW
10932 ret = i915_mutex_lock_interruptible(dev);
10933 if (ret)
10934 goto cleanup;
10935
b4a98e57 10936 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10937 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10938
75f7f3ec 10939 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10940 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10941
4fa62c89
VS
10942 if (IS_VALLEYVIEW(dev)) {
10943 ring = &dev_priv->ring[BCS];
ab8d6675 10944 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
10945 /* vlv: DISPLAY_FLIP fails to change tiling */
10946 ring = NULL;
48bf5b2d 10947 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 10948 ring = &dev_priv->ring[BCS];
4fa62c89 10949 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 10950 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
10951 if (ring == NULL || ring->id != RCS)
10952 ring = &dev_priv->ring[BCS];
10953 } else {
10954 ring = &dev_priv->ring[RCS];
10955 }
10956
cf5d8a46
CW
10957 mmio_flip = use_mmio_flip(ring, obj);
10958
10959 /* When using CS flips, we want to emit semaphores between rings.
10960 * However, when using mmio flips we will create a task to do the
10961 * synchronisation, so all we want here is to pin the framebuffer
10962 * into the display plane and skip any waits.
10963 */
82bc3b2d 10964 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46
CW
10965 crtc->primary->state,
10966 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
8c9f3aaf
JB
10967 if (ret)
10968 goto cleanup_pending;
6b95a207 10969
121920fa
TU
10970 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10971 + intel_crtc->dspaddr_offset;
4fa62c89 10972
cf5d8a46 10973 if (mmio_flip) {
84c33a64
SG
10974 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10975 page_flip_flags);
d6bbafa1
CW
10976 if (ret)
10977 goto cleanup_unpin;
10978
f06cc1b9
JH
10979 i915_gem_request_assign(&work->flip_queued_req,
10980 obj->last_write_req);
d6bbafa1 10981 } else {
84c33a64 10982 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10983 page_flip_flags);
10984 if (ret)
10985 goto cleanup_unpin;
10986
f06cc1b9
JH
10987 i915_gem_request_assign(&work->flip_queued_req,
10988 intel_ring_get_request(ring));
d6bbafa1
CW
10989 }
10990
1e3feefd 10991 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 10992 work->enable_stall_check = true;
4fa62c89 10993
ab8d6675 10994 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
10995 INTEL_FRONTBUFFER_PRIMARY(pipe));
10996
7ff0ebcc 10997 intel_fbc_disable(dev);
f99d7069 10998 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
10999 mutex_unlock(&dev->struct_mutex);
11000
e5510fac
JB
11001 trace_i915_flip_request(intel_crtc->plane, obj);
11002
6b95a207 11003 return 0;
96b099fd 11004
4fa62c89 11005cleanup_unpin:
82bc3b2d 11006 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11007cleanup_pending:
b4a98e57 11008 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11009 mutex_unlock(&dev->struct_mutex);
11010cleanup:
f4510a27 11011 crtc->primary->fb = old_fb;
afd65eb4 11012 update_state_fb(crtc->primary);
89ed88ba
CW
11013
11014 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11015 drm_framebuffer_unreference(work->old_fb);
96b099fd 11016
5e2d7afc 11017 spin_lock_irq(&dev->event_lock);
96b099fd 11018 intel_crtc->unpin_work = NULL;
5e2d7afc 11019 spin_unlock_irq(&dev->event_lock);
96b099fd 11020
87b6b101 11021 drm_crtc_vblank_put(crtc);
7317c75e 11022free_work:
96b099fd
CW
11023 kfree(work);
11024
f900db47
CW
11025 if (ret == -EIO) {
11026out_hang:
53a366b9 11027 ret = intel_plane_restore(primary);
f0d3dad3 11028 if (ret == 0 && event) {
5e2d7afc 11029 spin_lock_irq(&dev->event_lock);
a071fa00 11030 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11031 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11032 }
f900db47 11033 }
96b099fd 11034 return ret;
6b95a207
KH
11035}
11036
65b38e0d 11037static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11038 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11039 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11040 .atomic_begin = intel_begin_crtc_commit,
11041 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11042};
11043
9a935856
DV
11044/**
11045 * intel_modeset_update_staged_output_state
11046 *
11047 * Updates the staged output configuration state, e.g. after we've read out the
11048 * current hw state.
11049 */
11050static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11051{
7668851f 11052 struct intel_crtc *crtc;
9a935856
DV
11053 struct intel_encoder *encoder;
11054 struct intel_connector *connector;
f6e5b160 11055
3a3371ff 11056 for_each_intel_connector(dev, connector) {
9a935856
DV
11057 connector->new_encoder =
11058 to_intel_encoder(connector->base.encoder);
11059 }
f6e5b160 11060
b2784e15 11061 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11062 encoder->new_crtc =
11063 to_intel_crtc(encoder->base.crtc);
11064 }
7668851f 11065
d3fcc808 11066 for_each_intel_crtc(dev, crtc) {
83d65738 11067 crtc->new_enabled = crtc->base.state->enable;
7668851f 11068 }
f6e5b160
CW
11069}
11070
d29b2f9d
ACO
11071/* Transitional helper to copy current connector/encoder state to
11072 * connector->state. This is needed so that code that is partially
11073 * converted to atomic does the right thing.
11074 */
11075static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11076{
11077 struct intel_connector *connector;
11078
11079 for_each_intel_connector(dev, connector) {
11080 if (connector->base.encoder) {
11081 connector->base.state->best_encoder =
11082 connector->base.encoder;
11083 connector->base.state->crtc =
11084 connector->base.encoder->crtc;
11085 } else {
11086 connector->base.state->best_encoder = NULL;
11087 connector->base.state->crtc = NULL;
11088 }
11089 }
11090}
11091
9a935856
DV
11092/**
11093 * intel_modeset_commit_output_state
11094 *
11095 * This function copies the stage display pipe configuration to the real one.
11096 */
11097static void intel_modeset_commit_output_state(struct drm_device *dev)
11098{
7668851f 11099 struct intel_crtc *crtc;
9a935856
DV
11100 struct intel_encoder *encoder;
11101 struct intel_connector *connector;
f6e5b160 11102
3a3371ff 11103 for_each_intel_connector(dev, connector) {
9a935856
DV
11104 connector->base.encoder = &connector->new_encoder->base;
11105 }
f6e5b160 11106
b2784e15 11107 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11108 encoder->base.crtc = &encoder->new_crtc->base;
11109 }
7668851f 11110
d3fcc808 11111 for_each_intel_crtc(dev, crtc) {
83d65738 11112 crtc->base.state->enable = crtc->new_enabled;
7668851f
VS
11113 crtc->base.enabled = crtc->new_enabled;
11114 }
d29b2f9d
ACO
11115
11116 intel_modeset_update_connector_atomic_state(dev);
9a935856
DV
11117}
11118
050f7aeb 11119static void
eba905b2 11120connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11121 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11122{
11123 int bpp = pipe_config->pipe_bpp;
11124
11125 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11126 connector->base.base.id,
c23cc417 11127 connector->base.name);
050f7aeb
DV
11128
11129 /* Don't use an invalid EDID bpc value */
11130 if (connector->base.display_info.bpc &&
11131 connector->base.display_info.bpc * 3 < bpp) {
11132 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11133 bpp, connector->base.display_info.bpc*3);
11134 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11135 }
11136
11137 /* Clamp bpp to 8 on screens without EDID 1.4 */
11138 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11139 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11140 bpp);
11141 pipe_config->pipe_bpp = 24;
11142 }
11143}
11144
4e53c2e0 11145static int
050f7aeb 11146compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11147 struct intel_crtc_state *pipe_config)
4e53c2e0 11148{
050f7aeb 11149 struct drm_device *dev = crtc->base.dev;
1486017f 11150 struct drm_atomic_state *state;
050f7aeb 11151 struct intel_connector *connector;
1486017f 11152 int bpp, i;
4e53c2e0 11153
d328c9d7 11154 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11155 bpp = 10*3;
d328c9d7
DV
11156 else if (INTEL_INFO(dev)->gen >= 5)
11157 bpp = 12*3;
11158 else
11159 bpp = 8*3;
11160
4e53c2e0 11161
4e53c2e0
DV
11162 pipe_config->pipe_bpp = bpp;
11163
1486017f
ACO
11164 state = pipe_config->base.state;
11165
4e53c2e0 11166 /* Clamp display bpp to EDID value */
1486017f
ACO
11167 for (i = 0; i < state->num_connector; i++) {
11168 if (!state->connectors[i])
11169 continue;
11170
11171 connector = to_intel_connector(state->connectors[i]);
11172 if (state->connector_states[i]->crtc != &crtc->base)
4e53c2e0
DV
11173 continue;
11174
050f7aeb 11175 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
11176 }
11177
11178 return bpp;
11179}
11180
644db711
DV
11181static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11182{
11183 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11184 "type: 0x%x flags: 0x%x\n",
1342830c 11185 mode->crtc_clock,
644db711
DV
11186 mode->crtc_hdisplay, mode->crtc_hsync_start,
11187 mode->crtc_hsync_end, mode->crtc_htotal,
11188 mode->crtc_vdisplay, mode->crtc_vsync_start,
11189 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11190}
11191
c0b03411 11192static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11193 struct intel_crtc_state *pipe_config,
c0b03411
DV
11194 const char *context)
11195{
6a60cd87
CK
11196 struct drm_device *dev = crtc->base.dev;
11197 struct drm_plane *plane;
11198 struct intel_plane *intel_plane;
11199 struct intel_plane_state *state;
11200 struct drm_framebuffer *fb;
11201
11202 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11203 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11204
11205 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11206 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11207 pipe_config->pipe_bpp, pipe_config->dither);
11208 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11209 pipe_config->has_pch_encoder,
11210 pipe_config->fdi_lanes,
11211 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11212 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11213 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11214 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11215 pipe_config->has_dp_encoder,
11216 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11217 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11218 pipe_config->dp_m_n.tu);
b95af8be
VK
11219
11220 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11221 pipe_config->has_dp_encoder,
11222 pipe_config->dp_m2_n2.gmch_m,
11223 pipe_config->dp_m2_n2.gmch_n,
11224 pipe_config->dp_m2_n2.link_m,
11225 pipe_config->dp_m2_n2.link_n,
11226 pipe_config->dp_m2_n2.tu);
11227
55072d19
DV
11228 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11229 pipe_config->has_audio,
11230 pipe_config->has_infoframe);
11231
c0b03411 11232 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11233 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11234 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11235 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11236 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11237 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11238 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11239 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
6a60cd87
CK
11240 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11241 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11242 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
c0b03411
DV
11243 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11244 pipe_config->gmch_pfit.control,
11245 pipe_config->gmch_pfit.pgm_ratios,
11246 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11247 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11248 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11249 pipe_config->pch_pfit.size,
11250 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11251 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11252 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87
CK
11253
11254 DRM_DEBUG_KMS("planes on this crtc\n");
11255 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11256 intel_plane = to_intel_plane(plane);
11257 if (intel_plane->pipe != crtc->pipe)
11258 continue;
11259
11260 state = to_intel_plane_state(plane->state);
11261 fb = state->base.fb;
11262 if (!fb) {
11263 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11264 "disabled, scaler_id = %d\n",
11265 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11266 plane->base.id, intel_plane->pipe,
11267 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11268 drm_plane_index(plane), state->scaler_id);
11269 continue;
11270 }
11271
11272 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11273 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11274 plane->base.id, intel_plane->pipe,
11275 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11276 drm_plane_index(plane));
11277 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11278 fb->base.id, fb->width, fb->height, fb->pixel_format);
11279 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11280 state->scaler_id,
11281 state->src.x1 >> 16, state->src.y1 >> 16,
11282 drm_rect_width(&state->src) >> 16,
11283 drm_rect_height(&state->src) >> 16,
11284 state->dst.x1, state->dst.y1,
11285 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11286 }
c0b03411
DV
11287}
11288
bc079e8b
VS
11289static bool encoders_cloneable(const struct intel_encoder *a,
11290 const struct intel_encoder *b)
accfc0c5 11291{
bc079e8b
VS
11292 /* masks could be asymmetric, so check both ways */
11293 return a == b || (a->cloneable & (1 << b->type) &&
11294 b->cloneable & (1 << a->type));
11295}
11296
98a221da
ACO
11297static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11298 struct intel_crtc *crtc,
bc079e8b
VS
11299 struct intel_encoder *encoder)
11300{
bc079e8b 11301 struct intel_encoder *source_encoder;
98a221da
ACO
11302 struct drm_connector_state *connector_state;
11303 int i;
bc079e8b 11304
98a221da
ACO
11305 for (i = 0; i < state->num_connector; i++) {
11306 if (!state->connectors[i])
11307 continue;
bc079e8b 11308
98a221da
ACO
11309 connector_state = state->connector_states[i];
11310 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11311 continue;
11312
98a221da
ACO
11313 source_encoder =
11314 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11315 if (!encoders_cloneable(encoder, source_encoder))
11316 return false;
11317 }
11318
11319 return true;
11320}
11321
98a221da
ACO
11322static bool check_encoder_cloning(struct drm_atomic_state *state,
11323 struct intel_crtc *crtc)
bc079e8b 11324{
accfc0c5 11325 struct intel_encoder *encoder;
98a221da
ACO
11326 struct drm_connector_state *connector_state;
11327 int i;
accfc0c5 11328
98a221da
ACO
11329 for (i = 0; i < state->num_connector; i++) {
11330 if (!state->connectors[i])
accfc0c5
DV
11331 continue;
11332
98a221da
ACO
11333 connector_state = state->connector_states[i];
11334 if (connector_state->crtc != &crtc->base)
11335 continue;
11336
11337 encoder = to_intel_encoder(connector_state->best_encoder);
11338 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11339 return false;
accfc0c5
DV
11340 }
11341
bc079e8b 11342 return true;
accfc0c5
DV
11343}
11344
5448a00d 11345static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11346{
5448a00d
ACO
11347 struct drm_device *dev = state->dev;
11348 struct intel_encoder *encoder;
11349 struct drm_connector_state *connector_state;
00f0b378 11350 unsigned int used_ports = 0;
5448a00d 11351 int i;
00f0b378
VS
11352
11353 /*
11354 * Walk the connector list instead of the encoder
11355 * list to detect the problem on ddi platforms
11356 * where there's just one encoder per digital port.
11357 */
5448a00d
ACO
11358 for (i = 0; i < state->num_connector; i++) {
11359 if (!state->connectors[i])
11360 continue;
00f0b378 11361
5448a00d
ACO
11362 connector_state = state->connector_states[i];
11363 if (!connector_state->best_encoder)
00f0b378
VS
11364 continue;
11365
5448a00d
ACO
11366 encoder = to_intel_encoder(connector_state->best_encoder);
11367
11368 WARN_ON(!connector_state->crtc);
00f0b378
VS
11369
11370 switch (encoder->type) {
11371 unsigned int port_mask;
11372 case INTEL_OUTPUT_UNKNOWN:
11373 if (WARN_ON(!HAS_DDI(dev)))
11374 break;
11375 case INTEL_OUTPUT_DISPLAYPORT:
11376 case INTEL_OUTPUT_HDMI:
11377 case INTEL_OUTPUT_EDP:
11378 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11379
11380 /* the same port mustn't appear more than once */
11381 if (used_ports & port_mask)
11382 return false;
11383
11384 used_ports |= port_mask;
11385 default:
11386 break;
11387 }
11388 }
11389
11390 return true;
11391}
11392
83a57153
ACO
11393static void
11394clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11395{
11396 struct drm_crtc_state tmp_state;
663a3640 11397 struct intel_crtc_scaler_state scaler_state;
83a57153 11398
663a3640 11399 /* Clear only the intel specific part of the crtc state excluding scalers */
83a57153 11400 tmp_state = crtc_state->base;
663a3640 11401 scaler_state = crtc_state->scaler_state;
83a57153
ACO
11402 memset(crtc_state, 0, sizeof *crtc_state);
11403 crtc_state->base = tmp_state;
663a3640 11404 crtc_state->scaler_state = scaler_state;
83a57153
ACO
11405}
11406
5cec258b 11407static struct intel_crtc_state *
b8cecdf5 11408intel_modeset_pipe_config(struct drm_crtc *crtc,
83a57153
ACO
11409 struct drm_display_mode *mode,
11410 struct drm_atomic_state *state)
ee7b9f93 11411{
7758a113 11412 struct intel_encoder *encoder;
0b901879
ACO
11413 struct intel_connector *connector;
11414 struct drm_connector_state *connector_state;
5cec258b 11415 struct intel_crtc_state *pipe_config;
d328c9d7 11416 int base_bpp, ret = -EINVAL;
0b901879 11417 int i;
e29c22c0 11418 bool retry = true;
ee7b9f93 11419
98a221da 11420 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5
DV
11421 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11422 return ERR_PTR(-EINVAL);
11423 }
11424
5448a00d 11425 if (!check_digital_port_conflicts(state)) {
00f0b378
VS
11426 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11427 return ERR_PTR(-EINVAL);
11428 }
11429
83a57153
ACO
11430 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11431 if (IS_ERR(pipe_config))
11432 return pipe_config;
11433
11434 clear_intel_crtc_state(pipe_config);
7758a113 11435
07878248 11436 pipe_config->base.crtc = crtc;
2d112de7
ACO
11437 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11438 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 11439
e143a21c
DV
11440 pipe_config->cpu_transcoder =
11441 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 11442 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 11443
2960bc9c
ID
11444 /*
11445 * Sanitize sync polarity flags based on requested ones. If neither
11446 * positive or negative polarity is requested, treat this as meaning
11447 * negative polarity.
11448 */
2d112de7 11449 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11450 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11451 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11452
2d112de7 11453 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11454 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11456
050f7aeb
DV
11457 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11458 * plane pixel format and any sink constraints into account. Returns the
11459 * source plane bpp so that dithering can be selected on mismatches
11460 * after encoders and crtc also have had their say. */
d328c9d7
DV
11461 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11462 pipe_config);
11463 if (base_bpp < 0)
4e53c2e0
DV
11464 goto fail;
11465
e41a56be
VS
11466 /*
11467 * Determine the real pipe dimensions. Note that stereo modes can
11468 * increase the actual pipe size due to the frame doubling and
11469 * insertion of additional space for blanks between the frame. This
11470 * is stored in the crtc timings. We use the requested mode to do this
11471 * computation to clearly distinguish it from the adjusted mode, which
11472 * can be changed by the connectors in the below retry loop.
11473 */
2d112de7 11474 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11475 &pipe_config->pipe_src_w,
11476 &pipe_config->pipe_src_h);
e41a56be 11477
e29c22c0 11478encoder_retry:
ef1b460d 11479 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11480 pipe_config->port_clock = 0;
ef1b460d 11481 pipe_config->pixel_multiplier = 1;
ff9a6750 11482
135c81b8 11483 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11484 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11485 CRTC_STEREO_DOUBLE);
135c81b8 11486
7758a113
DV
11487 /* Pass our mode to the connectors and the CRTC to give them a chance to
11488 * adjust it according to limitations or connector properties, and also
11489 * a chance to reject the mode entirely.
47f1c6c9 11490 */
0b901879
ACO
11491 for (i = 0; i < state->num_connector; i++) {
11492 connector = to_intel_connector(state->connectors[i]);
11493 if (!connector)
11494 continue;
47f1c6c9 11495
0b901879
ACO
11496 connector_state = state->connector_states[i];
11497 if (connector_state->crtc != crtc)
7758a113 11498 continue;
7ae89233 11499
0b901879
ACO
11500 encoder = to_intel_encoder(connector_state->best_encoder);
11501
efea6e8e
DV
11502 if (!(encoder->compute_config(encoder, pipe_config))) {
11503 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11504 goto fail;
11505 }
ee7b9f93 11506 }
47f1c6c9 11507
ff9a6750
DV
11508 /* Set default port clock if not overwritten by the encoder. Needs to be
11509 * done afterwards in case the encoder adjusts the mode. */
11510 if (!pipe_config->port_clock)
2d112de7 11511 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11512 * pipe_config->pixel_multiplier;
ff9a6750 11513
a43f6e0f 11514 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11515 if (ret < 0) {
7758a113
DV
11516 DRM_DEBUG_KMS("CRTC fixup failed\n");
11517 goto fail;
ee7b9f93 11518 }
e29c22c0
DV
11519
11520 if (ret == RETRY) {
11521 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11522 ret = -EINVAL;
11523 goto fail;
11524 }
11525
11526 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11527 retry = false;
11528 goto encoder_retry;
11529 }
11530
d328c9d7 11531 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 11532 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11533 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11534
b8cecdf5 11535 return pipe_config;
7758a113 11536fail:
e29c22c0 11537 return ERR_PTR(ret);
ee7b9f93 11538}
47f1c6c9 11539
e2e1ed41
DV
11540/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11541 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11542static void
11543intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11544 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
11545{
11546 struct intel_crtc *intel_crtc;
e2e1ed41
DV
11547 struct drm_device *dev = crtc->dev;
11548 struct intel_encoder *encoder;
11549 struct intel_connector *connector;
11550 struct drm_crtc *tmp_crtc;
79e53945 11551
e2e1ed41 11552 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 11553
e2e1ed41
DV
11554 /* Check which crtcs have changed outputs connected to them, these need
11555 * to be part of the prepare_pipes mask. We don't (yet) support global
11556 * modeset across multiple crtcs, so modeset_pipes will only have one
11557 * bit set at most. */
3a3371ff 11558 for_each_intel_connector(dev, connector) {
e2e1ed41
DV
11559 if (connector->base.encoder == &connector->new_encoder->base)
11560 continue;
79e53945 11561
e2e1ed41
DV
11562 if (connector->base.encoder) {
11563 tmp_crtc = connector->base.encoder->crtc;
11564
11565 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11566 }
11567
11568 if (connector->new_encoder)
11569 *prepare_pipes |=
11570 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
11571 }
11572
b2784e15 11573 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
11574 if (encoder->base.crtc == &encoder->new_crtc->base)
11575 continue;
11576
11577 if (encoder->base.crtc) {
11578 tmp_crtc = encoder->base.crtc;
11579
11580 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11581 }
11582
11583 if (encoder->new_crtc)
11584 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
11585 }
11586
7668851f 11587 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 11588 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11589 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
e2e1ed41 11590 continue;
7e7d76c3 11591
7668851f 11592 if (!intel_crtc->new_enabled)
e2e1ed41 11593 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
11594 else
11595 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
11596 }
11597
e2e1ed41
DV
11598
11599 /* set_mode is also used to update properties on life display pipes. */
11600 intel_crtc = to_intel_crtc(crtc);
7668851f 11601 if (intel_crtc->new_enabled)
e2e1ed41
DV
11602 *prepare_pipes |= 1 << intel_crtc->pipe;
11603
b6c5164d
DV
11604 /*
11605 * For simplicity do a full modeset on any pipe where the output routing
11606 * changed. We could be more clever, but that would require us to be
11607 * more careful with calling the relevant encoder->mode_set functions.
11608 */
e2e1ed41
DV
11609 if (*prepare_pipes)
11610 *modeset_pipes = *prepare_pipes;
11611
11612 /* ... and mask these out. */
11613 *modeset_pipes &= ~(*disable_pipes);
11614 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
11615
11616 /*
11617 * HACK: We don't (yet) fully support global modesets. intel_set_config
11618 * obies this rule, but the modeset restore mode of
11619 * intel_modeset_setup_hw_state does not.
11620 */
11621 *modeset_pipes &= 1 << intel_crtc->pipe;
11622 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
11623
11624 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11625 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 11626}
79e53945 11627
ea9d758d 11628static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 11629{
ea9d758d 11630 struct drm_encoder *encoder;
f6e5b160 11631 struct drm_device *dev = crtc->dev;
f6e5b160 11632
ea9d758d
DV
11633 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11634 if (encoder->crtc == crtc)
11635 return true;
11636
11637 return false;
11638}
11639
11640static void
11641intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11642{
ba41c0de 11643 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
11644 struct intel_encoder *intel_encoder;
11645 struct intel_crtc *intel_crtc;
11646 struct drm_connector *connector;
11647
ba41c0de
DV
11648 intel_shared_dpll_commit(dev_priv);
11649
b2784e15 11650 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
11651 if (!intel_encoder->base.crtc)
11652 continue;
11653
11654 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11655
11656 if (prepare_pipes & (1 << intel_crtc->pipe))
11657 intel_encoder->connectors_active = false;
11658 }
11659
11660 intel_modeset_commit_output_state(dev);
11661
7668851f 11662 /* Double check state. */
d3fcc808 11663 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11664 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
ea9d758d
DV
11665 }
11666
11667 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11668 if (!connector->encoder || !connector->encoder->crtc)
11669 continue;
11670
11671 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11672
11673 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
11674 struct drm_property *dpms_property =
11675 dev->mode_config.dpms_property;
11676
ea9d758d 11677 connector->dpms = DRM_MODE_DPMS_ON;
662595df 11678 drm_object_property_set_value(&connector->base,
68d34720
DV
11679 dpms_property,
11680 DRM_MODE_DPMS_ON);
ea9d758d
DV
11681
11682 intel_encoder = to_intel_encoder(connector->encoder);
11683 intel_encoder->connectors_active = true;
11684 }
11685 }
11686
11687}
11688
3bd26263 11689static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11690{
3bd26263 11691 int diff;
f1f644dc
JB
11692
11693 if (clock1 == clock2)
11694 return true;
11695
11696 if (!clock1 || !clock2)
11697 return false;
11698
11699 diff = abs(clock1 - clock2);
11700
11701 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11702 return true;
11703
11704 return false;
11705}
11706
25c5b266
DV
11707#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11708 list_for_each_entry((intel_crtc), \
11709 &(dev)->mode_config.crtc_list, \
11710 base.head) \
0973f18f 11711 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11712
0e8ffe1b 11713static bool
2fa2fe9a 11714intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
11715 struct intel_crtc_state *current_config,
11716 struct intel_crtc_state *pipe_config)
0e8ffe1b 11717{
66e985c0
DV
11718#define PIPE_CONF_CHECK_X(name) \
11719 if (current_config->name != pipe_config->name) { \
11720 DRM_ERROR("mismatch in " #name " " \
11721 "(expected 0x%08x, found 0x%08x)\n", \
11722 current_config->name, \
11723 pipe_config->name); \
11724 return false; \
11725 }
11726
08a24034
DV
11727#define PIPE_CONF_CHECK_I(name) \
11728 if (current_config->name != pipe_config->name) { \
11729 DRM_ERROR("mismatch in " #name " " \
11730 "(expected %i, found %i)\n", \
11731 current_config->name, \
11732 pipe_config->name); \
11733 return false; \
88adfff1
DV
11734 }
11735
b95af8be
VK
11736/* This is required for BDW+ where there is only one set of registers for
11737 * switching between high and low RR.
11738 * This macro can be used whenever a comparison has to be made between one
11739 * hw state and multiple sw state variables.
11740 */
11741#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11742 if ((current_config->name != pipe_config->name) && \
11743 (current_config->alt_name != pipe_config->name)) { \
11744 DRM_ERROR("mismatch in " #name " " \
11745 "(expected %i or %i, found %i)\n", \
11746 current_config->name, \
11747 current_config->alt_name, \
11748 pipe_config->name); \
11749 return false; \
11750 }
11751
1bd1bd80
DV
11752#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11753 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 11754 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
11755 "(expected %i, found %i)\n", \
11756 current_config->name & (mask), \
11757 pipe_config->name & (mask)); \
11758 return false; \
11759 }
11760
5e550656
VS
11761#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11762 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11763 DRM_ERROR("mismatch in " #name " " \
11764 "(expected %i, found %i)\n", \
11765 current_config->name, \
11766 pipe_config->name); \
11767 return false; \
11768 }
11769
bb760063
DV
11770#define PIPE_CONF_QUIRK(quirk) \
11771 ((current_config->quirks | pipe_config->quirks) & (quirk))
11772
eccb140b
DV
11773 PIPE_CONF_CHECK_I(cpu_transcoder);
11774
08a24034
DV
11775 PIPE_CONF_CHECK_I(has_pch_encoder);
11776 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
11777 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11778 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11779 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11780 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11781 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 11782
eb14cb74 11783 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
11784
11785 if (INTEL_INFO(dev)->gen < 8) {
11786 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11787 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11788 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11789 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11790 PIPE_CONF_CHECK_I(dp_m_n.tu);
11791
11792 if (current_config->has_drrs) {
11793 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11794 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11795 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11796 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11797 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11798 }
11799 } else {
11800 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11801 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11802 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11803 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11804 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11805 }
eb14cb74 11806
2d112de7
ACO
11807 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11808 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11809 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11810 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11812 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11813
2d112de7
ACO
11814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11820
c93f54cf 11821 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11822 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
11823 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11824 IS_VALLEYVIEW(dev))
11825 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11826 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11827
9ed109a7
DV
11828 PIPE_CONF_CHECK_I(has_audio);
11829
2d112de7 11830 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11831 DRM_MODE_FLAG_INTERLACE);
11832
bb760063 11833 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11834 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11835 DRM_MODE_FLAG_PHSYNC);
2d112de7 11836 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11837 DRM_MODE_FLAG_NHSYNC);
2d112de7 11838 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11839 DRM_MODE_FLAG_PVSYNC);
2d112de7 11840 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11841 DRM_MODE_FLAG_NVSYNC);
11842 }
045ac3b5 11843
37327abd
VS
11844 PIPE_CONF_CHECK_I(pipe_src_w);
11845 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 11846
9953599b
DV
11847 /*
11848 * FIXME: BIOS likes to set up a cloned config with lvds+external
11849 * screen. Since we don't yet re-compute the pipe config when moving
11850 * just the lvds port away to another pipe the sw tracking won't match.
11851 *
11852 * Proper atomic modesets with recomputed global state will fix this.
11853 * Until then just don't check gmch state for inherited modes.
11854 */
11855 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11856 PIPE_CONF_CHECK_I(gmch_pfit.control);
11857 /* pfit ratios are autocomputed by the hw on gen4+ */
11858 if (INTEL_INFO(dev)->gen < 4)
11859 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11860 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11861 }
11862
fd4daa9c
CW
11863 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11864 if (current_config->pch_pfit.enabled) {
11865 PIPE_CONF_CHECK_I(pch_pfit.pos);
11866 PIPE_CONF_CHECK_I(pch_pfit.size);
11867 }
2fa2fe9a 11868
a1b2278e
CK
11869 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11870
e59150dc
JB
11871 /* BDW+ don't expose a synchronous way to read the state */
11872 if (IS_HASWELL(dev))
11873 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11874
282740f7
VS
11875 PIPE_CONF_CHECK_I(double_wide);
11876
26804afd
DV
11877 PIPE_CONF_CHECK_X(ddi_pll_sel);
11878
c0d43d62 11879 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 11880 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11881 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11882 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11883 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11884 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
11885 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11886 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11887 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11888
42571aef
VS
11889 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11890 PIPE_CONF_CHECK_I(pipe_bpp);
11891
2d112de7 11892 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11893 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11894
66e985c0 11895#undef PIPE_CONF_CHECK_X
08a24034 11896#undef PIPE_CONF_CHECK_I
b95af8be 11897#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 11898#undef PIPE_CONF_CHECK_FLAGS
5e550656 11899#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11900#undef PIPE_CONF_QUIRK
88adfff1 11901
0e8ffe1b
DV
11902 return true;
11903}
11904
08db6652
DL
11905static void check_wm_state(struct drm_device *dev)
11906{
11907 struct drm_i915_private *dev_priv = dev->dev_private;
11908 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11909 struct intel_crtc *intel_crtc;
11910 int plane;
11911
11912 if (INTEL_INFO(dev)->gen < 9)
11913 return;
11914
11915 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11916 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11917
11918 for_each_intel_crtc(dev, intel_crtc) {
11919 struct skl_ddb_entry *hw_entry, *sw_entry;
11920 const enum pipe pipe = intel_crtc->pipe;
11921
11922 if (!intel_crtc->active)
11923 continue;
11924
11925 /* planes */
dd740780 11926 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
11927 hw_entry = &hw_ddb.plane[pipe][plane];
11928 sw_entry = &sw_ddb->plane[pipe][plane];
11929
11930 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11931 continue;
11932
11933 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11934 "(expected (%u,%u), found (%u,%u))\n",
11935 pipe_name(pipe), plane + 1,
11936 sw_entry->start, sw_entry->end,
11937 hw_entry->start, hw_entry->end);
11938 }
11939
11940 /* cursor */
11941 hw_entry = &hw_ddb.cursor[pipe];
11942 sw_entry = &sw_ddb->cursor[pipe];
11943
11944 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11945 continue;
11946
11947 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11948 "(expected (%u,%u), found (%u,%u))\n",
11949 pipe_name(pipe),
11950 sw_entry->start, sw_entry->end,
11951 hw_entry->start, hw_entry->end);
11952 }
11953}
11954
91d1b4bd
DV
11955static void
11956check_connector_state(struct drm_device *dev)
8af6cf88 11957{
8af6cf88
DV
11958 struct intel_connector *connector;
11959
3a3371ff 11960 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11961 /* This also checks the encoder/connector hw state with the
11962 * ->get_hw_state callbacks. */
11963 intel_connector_check_state(connector);
11964
e2c719b7 11965 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
11966 "connector's staged encoder doesn't match current encoder\n");
11967 }
91d1b4bd
DV
11968}
11969
11970static void
11971check_encoder_state(struct drm_device *dev)
11972{
11973 struct intel_encoder *encoder;
11974 struct intel_connector *connector;
8af6cf88 11975
b2784e15 11976 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11977 bool enabled = false;
11978 bool active = false;
11979 enum pipe pipe, tracked_pipe;
11980
11981 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11982 encoder->base.base.id,
8e329a03 11983 encoder->base.name);
8af6cf88 11984
e2c719b7 11985 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 11986 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 11987 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
11988 "encoder's active_connectors set, but no crtc\n");
11989
3a3371ff 11990 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11991 if (connector->base.encoder != &encoder->base)
11992 continue;
11993 enabled = true;
11994 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11995 active = true;
11996 }
0e32b39c
DA
11997 /*
11998 * for MST connectors if we unplug the connector is gone
11999 * away but the encoder is still connected to a crtc
12000 * until a modeset happens in response to the hotplug.
12001 */
12002 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12003 continue;
12004
e2c719b7 12005 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12006 "encoder's enabled state mismatch "
12007 "(expected %i, found %i)\n",
12008 !!encoder->base.crtc, enabled);
e2c719b7 12009 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12010 "active encoder with no crtc\n");
12011
e2c719b7 12012 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12013 "encoder's computed active state doesn't match tracked active state "
12014 "(expected %i, found %i)\n", active, encoder->connectors_active);
12015
12016 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12017 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12018 "encoder's hw state doesn't match sw tracking "
12019 "(expected %i, found %i)\n",
12020 encoder->connectors_active, active);
12021
12022 if (!encoder->base.crtc)
12023 continue;
12024
12025 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12026 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12027 "active encoder's pipe doesn't match"
12028 "(expected %i, found %i)\n",
12029 tracked_pipe, pipe);
12030
12031 }
91d1b4bd
DV
12032}
12033
12034static void
12035check_crtc_state(struct drm_device *dev)
12036{
fbee40df 12037 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12038 struct intel_crtc *crtc;
12039 struct intel_encoder *encoder;
5cec258b 12040 struct intel_crtc_state pipe_config;
8af6cf88 12041
d3fcc808 12042 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12043 bool enabled = false;
12044 bool active = false;
12045
045ac3b5
JB
12046 memset(&pipe_config, 0, sizeof(pipe_config));
12047
8af6cf88
DV
12048 DRM_DEBUG_KMS("[CRTC:%d]\n",
12049 crtc->base.base.id);
12050
83d65738 12051 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12052 "active crtc, but not enabled in sw tracking\n");
12053
b2784e15 12054 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12055 if (encoder->base.crtc != &crtc->base)
12056 continue;
12057 enabled = true;
12058 if (encoder->connectors_active)
12059 active = true;
12060 }
6c49f241 12061
e2c719b7 12062 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12063 "crtc's computed active state doesn't match tracked active state "
12064 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12065 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12066 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12067 "(expected %i, found %i)\n", enabled,
12068 crtc->base.state->enable);
8af6cf88 12069
0e8ffe1b
DV
12070 active = dev_priv->display.get_pipe_config(crtc,
12071 &pipe_config);
d62cf62a 12072
b6b5d049
VS
12073 /* hw state is inconsistent with the pipe quirk */
12074 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12075 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12076 active = crtc->active;
12077
b2784e15 12078 for_each_intel_encoder(dev, encoder) {
3eaba51c 12079 enum pipe pipe;
6c49f241
DV
12080 if (encoder->base.crtc != &crtc->base)
12081 continue;
1d37b689 12082 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12083 encoder->get_config(encoder, &pipe_config);
12084 }
12085
e2c719b7 12086 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12087 "crtc active state doesn't match with hw state "
12088 "(expected %i, found %i)\n", crtc->active, active);
12089
c0b03411 12090 if (active &&
6e3c9717 12091 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12092 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12093 intel_dump_pipe_config(crtc, &pipe_config,
12094 "[hw state]");
6e3c9717 12095 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12096 "[sw state]");
12097 }
8af6cf88
DV
12098 }
12099}
12100
91d1b4bd
DV
12101static void
12102check_shared_dpll_state(struct drm_device *dev)
12103{
fbee40df 12104 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12105 struct intel_crtc *crtc;
12106 struct intel_dpll_hw_state dpll_hw_state;
12107 int i;
5358901f
DV
12108
12109 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12110 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12111 int enabled_crtcs = 0, active_crtcs = 0;
12112 bool active;
12113
12114 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12115
12116 DRM_DEBUG_KMS("%s\n", pll->name);
12117
12118 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12119
e2c719b7 12120 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12121 "more active pll users than references: %i vs %i\n",
3e369b76 12122 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12123 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12124 "pll in active use but not on in sw tracking\n");
e2c719b7 12125 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12126 "pll in on but not on in use in sw tracking\n");
e2c719b7 12127 I915_STATE_WARN(pll->on != active,
5358901f
DV
12128 "pll on state mismatch (expected %i, found %i)\n",
12129 pll->on, active);
12130
d3fcc808 12131 for_each_intel_crtc(dev, crtc) {
83d65738 12132 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12133 enabled_crtcs++;
12134 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12135 active_crtcs++;
12136 }
e2c719b7 12137 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12138 "pll active crtcs mismatch (expected %i, found %i)\n",
12139 pll->active, active_crtcs);
e2c719b7 12140 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12141 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12142 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12143
e2c719b7 12144 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12145 sizeof(dpll_hw_state)),
12146 "pll hw state mismatch\n");
5358901f 12147 }
8af6cf88
DV
12148}
12149
91d1b4bd
DV
12150void
12151intel_modeset_check_state(struct drm_device *dev)
12152{
08db6652 12153 check_wm_state(dev);
91d1b4bd
DV
12154 check_connector_state(dev);
12155 check_encoder_state(dev);
12156 check_crtc_state(dev);
12157 check_shared_dpll_state(dev);
12158}
12159
5cec258b 12160void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12161 int dotclock)
12162{
12163 /*
12164 * FDI already provided one idea for the dotclock.
12165 * Yell if the encoder disagrees.
12166 */
2d112de7 12167 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12168 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12169 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12170}
12171
80715b2f
VS
12172static void update_scanline_offset(struct intel_crtc *crtc)
12173{
12174 struct drm_device *dev = crtc->base.dev;
12175
12176 /*
12177 * The scanline counter increments at the leading edge of hsync.
12178 *
12179 * On most platforms it starts counting from vtotal-1 on the
12180 * first active line. That means the scanline counter value is
12181 * always one less than what we would expect. Ie. just after
12182 * start of vblank, which also occurs at start of hsync (on the
12183 * last active line), the scanline counter will read vblank_start-1.
12184 *
12185 * On gen2 the scanline counter starts counting from 1 instead
12186 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12187 * to keep the value positive), instead of adding one.
12188 *
12189 * On HSW+ the behaviour of the scanline counter depends on the output
12190 * type. For DP ports it behaves like most other platforms, but on HDMI
12191 * there's an extra 1 line difference. So we need to add two instead of
12192 * one to the value.
12193 */
12194 if (IS_GEN2(dev)) {
6e3c9717 12195 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12196 int vtotal;
12197
12198 vtotal = mode->crtc_vtotal;
12199 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12200 vtotal /= 2;
12201
12202 crtc->scanline_offset = vtotal - 1;
12203 } else if (HAS_DDI(dev) &&
409ee761 12204 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12205 crtc->scanline_offset = 2;
12206 } else
12207 crtc->scanline_offset = 1;
12208}
12209
5cec258b 12210static struct intel_crtc_state *
7f27126e
JB
12211intel_modeset_compute_config(struct drm_crtc *crtc,
12212 struct drm_display_mode *mode,
83a57153 12213 struct drm_atomic_state *state,
7f27126e
JB
12214 unsigned *modeset_pipes,
12215 unsigned *prepare_pipes,
12216 unsigned *disable_pipes)
12217{
db7542dd 12218 struct drm_device *dev = crtc->dev;
5cec258b 12219 struct intel_crtc_state *pipe_config = NULL;
db7542dd 12220 struct intel_crtc *intel_crtc;
0b901879
ACO
12221 int ret = 0;
12222
12223 ret = drm_atomic_add_affected_connectors(state, crtc);
12224 if (ret)
12225 return ERR_PTR(ret);
7f27126e
JB
12226
12227 intel_modeset_affected_pipes(crtc, modeset_pipes,
12228 prepare_pipes, disable_pipes);
12229
db7542dd
ACO
12230 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12231 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12232 if (IS_ERR(pipe_config))
12233 return pipe_config;
12234
12235 pipe_config->base.enable = false;
12236 }
7f27126e
JB
12237
12238 /*
12239 * Note this needs changes when we start tracking multiple modes
12240 * and crtcs. At that point we'll need to compute the whole config
12241 * (i.e. one pipe_config for each crtc) rather than just the one
12242 * for this crtc.
12243 */
db7542dd
ACO
12244 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12245 /* FIXME: For now we still expect modeset_pipes has at most
12246 * one bit set. */
12247 if (WARN_ON(&intel_crtc->base != crtc))
12248 continue;
83a57153 12249
8805aa71 12250 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
db7542dd
ACO
12251 if (IS_ERR(pipe_config))
12252 return pipe_config;
7f27126e 12253
304603f4
ACO
12254 pipe_config->base.enable = true;
12255
db7542dd
ACO
12256 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12257 "[modeset]");
12258 }
12259
12260 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
7f27126e
JB
12261}
12262
225da59b 12263static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
ed6739ef
ACO
12264 unsigned modeset_pipes,
12265 unsigned disable_pipes)
12266{
225da59b 12267 struct drm_device *dev = state->dev;
ed6739ef
ACO
12268 struct drm_i915_private *dev_priv = to_i915(dev);
12269 unsigned clear_pipes = modeset_pipes | disable_pipes;
12270 struct intel_crtc *intel_crtc;
12271 int ret = 0;
12272
12273 if (!dev_priv->display.crtc_compute_clock)
12274 return 0;
12275
12276 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12277 if (ret)
12278 goto done;
12279
12280 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
225da59b
ACO
12281 struct intel_crtc_state *crtc_state =
12282 intel_atomic_get_crtc_state(state, intel_crtc);
12283
12284 /* Modeset pipes should have a new state by now */
12285 if (WARN_ON(IS_ERR(crtc_state)))
12286 continue;
12287
ed6739ef 12288 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
225da59b 12289 crtc_state);
ed6739ef
ACO
12290 if (ret) {
12291 intel_shared_dpll_abort_config(dev_priv);
12292 goto done;
12293 }
12294 }
12295
12296done:
12297 return ret;
12298}
12299
f30da187
DV
12300static int __intel_set_mode(struct drm_crtc *crtc,
12301 struct drm_display_mode *mode,
7f27126e 12302 int x, int y, struct drm_framebuffer *fb,
5cec258b 12303 struct intel_crtc_state *pipe_config,
7f27126e
JB
12304 unsigned modeset_pipes,
12305 unsigned prepare_pipes,
12306 unsigned disable_pipes)
a6778b3c
DV
12307{
12308 struct drm_device *dev = crtc->dev;
fbee40df 12309 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 12310 struct drm_display_mode *saved_mode;
304603f4 12311 struct drm_atomic_state *state = pipe_config->base.state;
83a57153 12312 struct intel_crtc_state *crtc_state_copy = NULL;
25c5b266 12313 struct intel_crtc *intel_crtc;
c0c36b94 12314 int ret = 0;
a6778b3c 12315
4b4b9238 12316 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
12317 if (!saved_mode)
12318 return -ENOMEM;
a6778b3c 12319
83a57153
ACO
12320 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12321 if (!crtc_state_copy) {
12322 ret = -ENOMEM;
12323 goto done;
12324 }
12325
3ac18232 12326 *saved_mode = crtc->mode;
a6778b3c 12327
30a970c6
JB
12328 /*
12329 * See if the config requires any additional preparation, e.g.
12330 * to adjust global state with pipes off. We need to do this
12331 * here so we can get the modeset_pipe updated config for the new
12332 * mode set on this crtc. For other crtcs we need to use the
12333 * adjusted_mode bits in the crtc directly.
12334 */
f8437dd1 12335 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
304603f4
ACO
12336 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12337 if (ret)
12338 goto done;
30a970c6 12339
c164f833
VS
12340 /* may have added more to prepare_pipes than we should */
12341 prepare_pipes &= ~disable_pipes;
12342 }
12343
225da59b 12344 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
ed6739ef
ACO
12345 if (ret)
12346 goto done;
8bd31e67 12347
460da916
DV
12348 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12349 intel_crtc_disable(&intel_crtc->base);
12350
ea9d758d 12351 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
83d65738 12352 if (intel_crtc->base.state->enable)
ea9d758d
DV
12353 dev_priv->display.crtc_disable(&intel_crtc->base);
12354 }
a6778b3c 12355
6c4c86f5
DV
12356 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12357 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
12358 *
12359 * Note we'll need to fix this up when we start tracking multiple
12360 * pipes; here we assume a single modeset_pipe and only track the
12361 * single crtc and mode.
f6e5b160 12362 */
b8cecdf5 12363 if (modeset_pipes) {
25c5b266 12364 crtc->mode = *mode;
b8cecdf5
DV
12365 /* mode_set/enable/disable functions rely on a correct pipe
12366 * config. */
f5de6e07 12367 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
12368
12369 /*
12370 * Calculate and store various constants which
12371 * are later needed by vblank and swap-completion
12372 * timestamping. They are derived from true hwmode.
12373 */
12374 drm_calc_timestamping_constants(crtc,
2d112de7 12375 &pipe_config->base.adjusted_mode);
b8cecdf5 12376 }
7758a113 12377
ea9d758d
DV
12378 /* Only after disabling all output pipelines that will be changed can we
12379 * update the the output configuration. */
12380 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 12381
304603f4 12382 modeset_update_crtc_power_domains(state);
47fab737 12383
25c5b266 12384 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
12385 struct drm_plane *primary = intel_crtc->base.primary;
12386 int vdisplay, hdisplay;
4c10794f 12387
455a6808 12388 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
70a101f8
MR
12389 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12390 fb, 0, 0,
12391 hdisplay, vdisplay,
12392 x << 16, y << 16,
12393 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
12394 }
12395
12396 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
12397 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12398 update_scanline_offset(intel_crtc);
12399
25c5b266 12400 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 12401 }
a6778b3c 12402
a6778b3c
DV
12403 /* FIXME: add subpixel order */
12404done:
83d65738 12405 if (ret && crtc->state->enable)
3ac18232 12406 crtc->mode = *saved_mode;
a6778b3c 12407
83a57153
ACO
12408 if (ret == 0 && pipe_config) {
12409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12410
12411 /* The pipe_config will be freed with the atomic state, so
12412 * make a copy. */
12413 memcpy(crtc_state_copy, intel_crtc->config,
12414 sizeof *crtc_state_copy);
12415 intel_crtc->config = crtc_state_copy;
12416 intel_crtc->base.state = &crtc_state_copy->base;
83a57153
ACO
12417 } else {
12418 kfree(crtc_state_copy);
12419 }
12420
3ac18232 12421 kfree(saved_mode);
a6778b3c 12422 return ret;
f6e5b160
CW
12423}
12424
7f27126e
JB
12425static int intel_set_mode_pipes(struct drm_crtc *crtc,
12426 struct drm_display_mode *mode,
12427 int x, int y, struct drm_framebuffer *fb,
5cec258b 12428 struct intel_crtc_state *pipe_config,
7f27126e
JB
12429 unsigned modeset_pipes,
12430 unsigned prepare_pipes,
12431 unsigned disable_pipes)
f30da187
DV
12432{
12433 int ret;
12434
7f27126e
JB
12435 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12436 prepare_pipes, disable_pipes);
f30da187
DV
12437
12438 if (ret == 0)
12439 intel_modeset_check_state(crtc->dev);
12440
12441 return ret;
12442}
12443
7f27126e
JB
12444static int intel_set_mode(struct drm_crtc *crtc,
12445 struct drm_display_mode *mode,
83a57153
ACO
12446 int x, int y, struct drm_framebuffer *fb,
12447 struct drm_atomic_state *state)
7f27126e 12448{
5cec258b 12449 struct intel_crtc_state *pipe_config;
7f27126e 12450 unsigned modeset_pipes, prepare_pipes, disable_pipes;
83a57153 12451 int ret = 0;
7f27126e 12452
8805aa71 12453 pipe_config = intel_modeset_compute_config(crtc, mode, state,
7f27126e
JB
12454 &modeset_pipes,
12455 &prepare_pipes,
12456 &disable_pipes);
12457
83a57153
ACO
12458 if (IS_ERR(pipe_config)) {
12459 ret = PTR_ERR(pipe_config);
12460 goto out;
12461 }
12462
12463 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12464 modeset_pipes, prepare_pipes,
12465 disable_pipes);
12466 if (ret)
12467 goto out;
7f27126e 12468
83a57153
ACO
12469out:
12470 return ret;
7f27126e
JB
12471}
12472
c0c36b94
CW
12473void intel_crtc_restore_mode(struct drm_crtc *crtc)
12474{
83a57153
ACO
12475 struct drm_device *dev = crtc->dev;
12476 struct drm_atomic_state *state;
12477 struct intel_encoder *encoder;
12478 struct intel_connector *connector;
12479 struct drm_connector_state *connector_state;
12480
12481 state = drm_atomic_state_alloc(dev);
12482 if (!state) {
12483 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12484 crtc->base.id);
12485 return;
12486 }
12487
12488 state->acquire_ctx = dev->mode_config.acquire_ctx;
12489
12490 /* The force restore path in the HW readout code relies on the staged
12491 * config still keeping the user requested config while the actual
12492 * state has been overwritten by the configuration read from HW. We
12493 * need to copy the staged config to the atomic state, otherwise the
12494 * mode set will just reapply the state the HW is already in. */
12495 for_each_intel_encoder(dev, encoder) {
12496 if (&encoder->new_crtc->base != crtc)
12497 continue;
12498
12499 for_each_intel_connector(dev, connector) {
12500 if (connector->new_encoder != encoder)
12501 continue;
12502
12503 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12504 if (IS_ERR(connector_state)) {
12505 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12506 connector->base.base.id,
12507 connector->base.name,
12508 PTR_ERR(connector_state));
12509 continue;
12510 }
12511
12512 connector_state->crtc = crtc;
12513 connector_state->best_encoder = &encoder->base;
12514 }
12515 }
12516
12517 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12518 state);
12519
12520 drm_atomic_state_free(state);
c0c36b94
CW
12521}
12522
25c5b266
DV
12523#undef for_each_intel_crtc_masked
12524
d9e55608
DV
12525static void intel_set_config_free(struct intel_set_config *config)
12526{
12527 if (!config)
12528 return;
12529
1aa4b628
DV
12530 kfree(config->save_connector_encoders);
12531 kfree(config->save_encoder_crtcs);
7668851f 12532 kfree(config->save_crtc_enabled);
d9e55608
DV
12533 kfree(config);
12534}
12535
85f9eb71
DV
12536static int intel_set_config_save_state(struct drm_device *dev,
12537 struct intel_set_config *config)
12538{
7668851f 12539 struct drm_crtc *crtc;
85f9eb71
DV
12540 struct drm_encoder *encoder;
12541 struct drm_connector *connector;
12542 int count;
12543
7668851f
VS
12544 config->save_crtc_enabled =
12545 kcalloc(dev->mode_config.num_crtc,
12546 sizeof(bool), GFP_KERNEL);
12547 if (!config->save_crtc_enabled)
12548 return -ENOMEM;
12549
1aa4b628
DV
12550 config->save_encoder_crtcs =
12551 kcalloc(dev->mode_config.num_encoder,
12552 sizeof(struct drm_crtc *), GFP_KERNEL);
12553 if (!config->save_encoder_crtcs)
85f9eb71
DV
12554 return -ENOMEM;
12555
1aa4b628
DV
12556 config->save_connector_encoders =
12557 kcalloc(dev->mode_config.num_connector,
12558 sizeof(struct drm_encoder *), GFP_KERNEL);
12559 if (!config->save_connector_encoders)
85f9eb71
DV
12560 return -ENOMEM;
12561
12562 /* Copy data. Note that driver private data is not affected.
12563 * Should anything bad happen only the expected state is
12564 * restored, not the drivers personal bookkeeping.
12565 */
7668851f 12566 count = 0;
70e1e0ec 12567 for_each_crtc(dev, crtc) {
83d65738 12568 config->save_crtc_enabled[count++] = crtc->state->enable;
7668851f
VS
12569 }
12570
85f9eb71
DV
12571 count = 0;
12572 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 12573 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
12574 }
12575
12576 count = 0;
12577 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 12578 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
12579 }
12580
12581 return 0;
12582}
12583
12584static void intel_set_config_restore_state(struct drm_device *dev,
12585 struct intel_set_config *config)
12586{
7668851f 12587 struct intel_crtc *crtc;
9a935856
DV
12588 struct intel_encoder *encoder;
12589 struct intel_connector *connector;
85f9eb71
DV
12590 int count;
12591
7668851f 12592 count = 0;
d3fcc808 12593 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12594 crtc->new_enabled = config->save_crtc_enabled[count++];
12595 }
12596
85f9eb71 12597 count = 0;
b2784e15 12598 for_each_intel_encoder(dev, encoder) {
9a935856
DV
12599 encoder->new_crtc =
12600 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
12601 }
12602
12603 count = 0;
3a3371ff 12604 for_each_intel_connector(dev, connector) {
9a935856
DV
12605 connector->new_encoder =
12606 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
12607 }
12608}
12609
e3de42b6 12610static bool
2e57f47d 12611is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
12612{
12613 int i;
12614
2e57f47d
CW
12615 if (set->num_connectors == 0)
12616 return false;
12617
12618 if (WARN_ON(set->connectors == NULL))
12619 return false;
12620
12621 for (i = 0; i < set->num_connectors; i++)
12622 if (set->connectors[i]->encoder &&
12623 set->connectors[i]->encoder->crtc == set->crtc &&
12624 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
12625 return true;
12626
12627 return false;
12628}
12629
5e2b584e
DV
12630static void
12631intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12632 struct intel_set_config *config)
12633{
12634
12635 /* We should be able to check here if the fb has the same properties
12636 * and then just flip_or_move it */
2e57f47d
CW
12637 if (is_crtc_connector_off(set)) {
12638 config->mode_changed = true;
f4510a27 12639 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
12640 /*
12641 * If we have no fb, we can only flip as long as the crtc is
12642 * active, otherwise we need a full mode set. The crtc may
12643 * be active if we've only disabled the primary plane, or
12644 * in fastboot situations.
12645 */
f4510a27 12646 if (set->crtc->primary->fb == NULL) {
319d9827
JB
12647 struct intel_crtc *intel_crtc =
12648 to_intel_crtc(set->crtc);
12649
3b150f08 12650 if (intel_crtc->active) {
319d9827
JB
12651 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12652 config->fb_changed = true;
12653 } else {
12654 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12655 config->mode_changed = true;
12656 }
5e2b584e
DV
12657 } else if (set->fb == NULL) {
12658 config->mode_changed = true;
72f4901e 12659 } else if (set->fb->pixel_format !=
f4510a27 12660 set->crtc->primary->fb->pixel_format) {
5e2b584e 12661 config->mode_changed = true;
e3de42b6 12662 } else {
5e2b584e 12663 config->fb_changed = true;
e3de42b6 12664 }
5e2b584e
DV
12665 }
12666
835c5873 12667 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
12668 config->fb_changed = true;
12669
12670 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12671 DRM_DEBUG_KMS("modes are different, full mode set\n");
12672 drm_mode_debug_printmodeline(&set->crtc->mode);
12673 drm_mode_debug_printmodeline(set->mode);
12674 config->mode_changed = true;
12675 }
a1d95703
CW
12676
12677 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12678 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
12679}
12680
2e431051 12681static int
9a935856
DV
12682intel_modeset_stage_output_state(struct drm_device *dev,
12683 struct drm_mode_set *set,
944b0c76
ACO
12684 struct intel_set_config *config,
12685 struct drm_atomic_state *state)
50f56119 12686{
9a935856 12687 struct intel_connector *connector;
944b0c76 12688 struct drm_connector_state *connector_state;
9a935856 12689 struct intel_encoder *encoder;
7668851f 12690 struct intel_crtc *crtc;
f3f08572 12691 int ro;
50f56119 12692
9abdda74 12693 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
12694 * of connectors. For paranoia, double-check this. */
12695 WARN_ON(!set->fb && (set->num_connectors != 0));
12696 WARN_ON(set->fb && (set->num_connectors == 0));
12697
3a3371ff 12698 for_each_intel_connector(dev, connector) {
9a935856
DV
12699 /* Otherwise traverse passed in connector list and get encoders
12700 * for them. */
50f56119 12701 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12702 if (set->connectors[ro] == &connector->base) {
0e32b39c 12703 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
12704 break;
12705 }
12706 }
12707
9a935856
DV
12708 /* If we disable the crtc, disable all its connectors. Also, if
12709 * the connector is on the changing crtc but not on the new
12710 * connector list, disable it. */
12711 if ((!set->fb || ro == set->num_connectors) &&
12712 connector->base.encoder &&
12713 connector->base.encoder->crtc == set->crtc) {
12714 connector->new_encoder = NULL;
12715
12716 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12717 connector->base.base.id,
c23cc417 12718 connector->base.name);
9a935856
DV
12719 }
12720
12721
12722 if (&connector->new_encoder->base != connector->base.encoder) {
10634189
ACO
12723 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12724 connector->base.base.id,
12725 connector->base.name);
5e2b584e 12726 config->mode_changed = true;
50f56119
DV
12727 }
12728 }
9a935856 12729 /* connector->new_encoder is now updated for all connectors. */
50f56119 12730
9a935856 12731 /* Update crtc of enabled connectors. */
3a3371ff 12732 for_each_intel_connector(dev, connector) {
7668851f
VS
12733 struct drm_crtc *new_crtc;
12734
9a935856 12735 if (!connector->new_encoder)
50f56119
DV
12736 continue;
12737
9a935856 12738 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
12739
12740 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12741 if (set->connectors[ro] == &connector->base)
50f56119
DV
12742 new_crtc = set->crtc;
12743 }
12744
12745 /* Make sure the new CRTC will work with the encoder */
14509916
TR
12746 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12747 new_crtc)) {
5e2b584e 12748 return -EINVAL;
50f56119 12749 }
0e32b39c 12750 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856 12751
944b0c76
ACO
12752 connector_state =
12753 drm_atomic_get_connector_state(state, &connector->base);
12754 if (IS_ERR(connector_state))
12755 return PTR_ERR(connector_state);
12756
12757 connector_state->crtc = new_crtc;
12758 connector_state->best_encoder = &connector->new_encoder->base;
12759
9a935856
DV
12760 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12761 connector->base.base.id,
c23cc417 12762 connector->base.name,
9a935856
DV
12763 new_crtc->base.id);
12764 }
12765
12766 /* Check for any encoders that needs to be disabled. */
b2784e15 12767 for_each_intel_encoder(dev, encoder) {
5a65f358 12768 int num_connectors = 0;
3a3371ff 12769 for_each_intel_connector(dev, connector) {
9a935856
DV
12770 if (connector->new_encoder == encoder) {
12771 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 12772 num_connectors++;
9a935856
DV
12773 }
12774 }
5a65f358
PZ
12775
12776 if (num_connectors == 0)
12777 encoder->new_crtc = NULL;
12778 else if (num_connectors > 1)
12779 return -EINVAL;
12780
9a935856
DV
12781 /* Only now check for crtc changes so we don't miss encoders
12782 * that will be disabled. */
12783 if (&encoder->new_crtc->base != encoder->base.crtc) {
10634189
ACO
12784 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12785 encoder->base.base.id,
12786 encoder->base.name);
5e2b584e 12787 config->mode_changed = true;
50f56119
DV
12788 }
12789 }
9a935856 12790 /* Now we've also updated encoder->new_crtc for all encoders. */
3a3371ff 12791 for_each_intel_connector(dev, connector) {
944b0c76
ACO
12792 connector_state =
12793 drm_atomic_get_connector_state(state, &connector->base);
9d918c15
ACO
12794 if (IS_ERR(connector_state))
12795 return PTR_ERR(connector_state);
944b0c76
ACO
12796
12797 if (connector->new_encoder) {
0e32b39c
DA
12798 if (connector->new_encoder != connector->encoder)
12799 connector->encoder = connector->new_encoder;
944b0c76
ACO
12800 } else {
12801 connector_state->crtc = NULL;
f61cccf3 12802 connector_state->best_encoder = NULL;
944b0c76 12803 }
0e32b39c 12804 }
d3fcc808 12805 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12806 crtc->new_enabled = false;
12807
b2784e15 12808 for_each_intel_encoder(dev, encoder) {
7668851f
VS
12809 if (encoder->new_crtc == crtc) {
12810 crtc->new_enabled = true;
12811 break;
12812 }
12813 }
12814
83d65738 12815 if (crtc->new_enabled != crtc->base.state->enable) {
10634189
ACO
12816 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12817 crtc->base.base.id,
7668851f
VS
12818 crtc->new_enabled ? "en" : "dis");
12819 config->mode_changed = true;
12820 }
12821 }
12822
2e431051
DV
12823 return 0;
12824}
12825
7d00a1f5
VS
12826static void disable_crtc_nofb(struct intel_crtc *crtc)
12827{
12828 struct drm_device *dev = crtc->base.dev;
12829 struct intel_encoder *encoder;
12830 struct intel_connector *connector;
12831
12832 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12833 pipe_name(crtc->pipe));
12834
3a3371ff 12835 for_each_intel_connector(dev, connector) {
7d00a1f5
VS
12836 if (connector->new_encoder &&
12837 connector->new_encoder->new_crtc == crtc)
12838 connector->new_encoder = NULL;
12839 }
12840
b2784e15 12841 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
12842 if (encoder->new_crtc == crtc)
12843 encoder->new_crtc = NULL;
12844 }
12845
12846 crtc->new_enabled = false;
12847}
12848
2e431051
DV
12849static int intel_crtc_set_config(struct drm_mode_set *set)
12850{
12851 struct drm_device *dev;
2e431051 12852 struct drm_mode_set save_set;
83a57153 12853 struct drm_atomic_state *state = NULL;
2e431051 12854 struct intel_set_config *config;
5cec258b 12855 struct intel_crtc_state *pipe_config;
50f52756 12856 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 12857 int ret;
2e431051 12858
8d3e375e
DV
12859 BUG_ON(!set);
12860 BUG_ON(!set->crtc);
12861 BUG_ON(!set->crtc->helper_private);
2e431051 12862
7e53f3a4
DV
12863 /* Enforce sane interface api - has been abused by the fb helper. */
12864 BUG_ON(!set->mode && set->fb);
12865 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 12866
2e431051
DV
12867 if (set->fb) {
12868 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12869 set->crtc->base.id, set->fb->base.id,
12870 (int)set->num_connectors, set->x, set->y);
12871 } else {
12872 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
12873 }
12874
12875 dev = set->crtc->dev;
12876
12877 ret = -ENOMEM;
12878 config = kzalloc(sizeof(*config), GFP_KERNEL);
12879 if (!config)
12880 goto out_config;
12881
12882 ret = intel_set_config_save_state(dev, config);
12883 if (ret)
12884 goto out_config;
12885
12886 save_set.crtc = set->crtc;
12887 save_set.mode = &set->crtc->mode;
12888 save_set.x = set->crtc->x;
12889 save_set.y = set->crtc->y;
f4510a27 12890 save_set.fb = set->crtc->primary->fb;
2e431051
DV
12891
12892 /* Compute whether we need a full modeset, only an fb base update or no
12893 * change at all. In the future we might also check whether only the
12894 * mode changed, e.g. for LVDS where we only change the panel fitter in
12895 * such cases. */
12896 intel_set_config_compute_mode_changes(set, config);
12897
83a57153
ACO
12898 state = drm_atomic_state_alloc(dev);
12899 if (!state) {
12900 ret = -ENOMEM;
12901 goto out_config;
12902 }
12903
12904 state->acquire_ctx = dev->mode_config.acquire_ctx;
12905
944b0c76 12906 ret = intel_modeset_stage_output_state(dev, set, config, state);
2e431051
DV
12907 if (ret)
12908 goto fail;
12909
50f52756 12910 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
8805aa71 12911 state,
50f52756
JB
12912 &modeset_pipes,
12913 &prepare_pipes,
12914 &disable_pipes);
20664591 12915 if (IS_ERR(pipe_config)) {
6ac0483b 12916 ret = PTR_ERR(pipe_config);
50f52756 12917 goto fail;
20664591 12918 } else if (pipe_config) {
b9950a13 12919 if (pipe_config->has_audio !=
6e3c9717 12920 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
12921 config->mode_changed = true;
12922
af15d2ce
JB
12923 /*
12924 * Note we have an issue here with infoframes: current code
12925 * only updates them on the full mode set path per hw
12926 * requirements. So here we should be checking for any
12927 * required changes and forcing a mode set.
12928 */
20664591 12929 }
50f52756 12930
1f9954d0
JB
12931 intel_update_pipe_size(to_intel_crtc(set->crtc));
12932
5e2b584e 12933 if (config->mode_changed) {
50f52756
JB
12934 ret = intel_set_mode_pipes(set->crtc, set->mode,
12935 set->x, set->y, set->fb, pipe_config,
12936 modeset_pipes, prepare_pipes,
12937 disable_pipes);
5e2b584e 12938 } else if (config->fb_changed) {
3b150f08 12939 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808
GP
12940 struct drm_plane *primary = set->crtc->primary;
12941 int vdisplay, hdisplay;
3b150f08 12942
455a6808 12943 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
70a101f8
MR
12944 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12945 0, 0, hdisplay, vdisplay,
12946 set->x << 16, set->y << 16,
12947 hdisplay << 16, vdisplay << 16);
3b150f08
MR
12948
12949 /*
12950 * We need to make sure the primary plane is re-enabled if it
12951 * has previously been turned off.
12952 */
12953 if (!intel_crtc->primary_enabled && ret == 0) {
12954 WARN_ON(!intel_crtc->active);
fdd508a6 12955 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
12956 }
12957
7ca51a3a
JB
12958 /*
12959 * In the fastboot case this may be our only check of the
12960 * state after boot. It would be better to only do it on
12961 * the first update, but we don't have a nice way of doing that
12962 * (and really, set_config isn't used much for high freq page
12963 * flipping, so increasing its cost here shouldn't be a big
12964 * deal).
12965 */
d330a953 12966 if (i915.fastboot && ret == 0)
7ca51a3a 12967 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
12968 }
12969
2d05eae1 12970 if (ret) {
bf67dfeb
DV
12971 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12972 set->crtc->base.id, ret);
50f56119 12973fail:
2d05eae1 12974 intel_set_config_restore_state(dev, config);
50f56119 12975
83a57153
ACO
12976 drm_atomic_state_clear(state);
12977
7d00a1f5
VS
12978 /*
12979 * HACK: if the pipe was on, but we didn't have a framebuffer,
12980 * force the pipe off to avoid oopsing in the modeset code
12981 * due to fb==NULL. This should only happen during boot since
12982 * we don't yet reconstruct the FB from the hardware state.
12983 */
12984 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12985 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12986
2d05eae1
CW
12987 /* Try to restore the config */
12988 if (config->mode_changed &&
12989 intel_set_mode(save_set.crtc, save_set.mode,
83a57153
ACO
12990 save_set.x, save_set.y, save_set.fb,
12991 state))
2d05eae1
CW
12992 DRM_ERROR("failed to restore config after modeset failure\n");
12993 }
50f56119 12994
d9e55608 12995out_config:
83a57153
ACO
12996 if (state)
12997 drm_atomic_state_free(state);
12998
d9e55608 12999 intel_set_config_free(config);
50f56119
DV
13000 return ret;
13001}
f6e5b160
CW
13002
13003static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13004 .gamma_set = intel_crtc_gamma_set,
50f56119 13005 .set_config = intel_crtc_set_config,
f6e5b160
CW
13006 .destroy = intel_crtc_destroy,
13007 .page_flip = intel_crtc_page_flip,
1356837e
MR
13008 .atomic_duplicate_state = intel_crtc_duplicate_state,
13009 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13010};
13011
5358901f
DV
13012static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13013 struct intel_shared_dpll *pll,
13014 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13015{
5358901f 13016 uint32_t val;
ee7b9f93 13017
f458ebbc 13018 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13019 return false;
13020
5358901f 13021 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13022 hw_state->dpll = val;
13023 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13024 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13025
13026 return val & DPLL_VCO_ENABLE;
13027}
13028
15bdd4cf
DV
13029static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13030 struct intel_shared_dpll *pll)
13031{
3e369b76
ACO
13032 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13033 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13034}
13035
e7b903d2
DV
13036static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13037 struct intel_shared_dpll *pll)
13038{
e7b903d2 13039 /* PCH refclock must be enabled first */
89eff4be 13040 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13041
3e369b76 13042 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13043
13044 /* Wait for the clocks to stabilize. */
13045 POSTING_READ(PCH_DPLL(pll->id));
13046 udelay(150);
13047
13048 /* The pixel multiplier can only be updated once the
13049 * DPLL is enabled and the clocks are stable.
13050 *
13051 * So write it again.
13052 */
3e369b76 13053 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13054 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13055 udelay(200);
13056}
13057
13058static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13059 struct intel_shared_dpll *pll)
13060{
13061 struct drm_device *dev = dev_priv->dev;
13062 struct intel_crtc *crtc;
e7b903d2
DV
13063
13064 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13065 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13066 if (intel_crtc_to_shared_dpll(crtc) == pll)
13067 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13068 }
13069
15bdd4cf
DV
13070 I915_WRITE(PCH_DPLL(pll->id), 0);
13071 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13072 udelay(200);
13073}
13074
46edb027
DV
13075static char *ibx_pch_dpll_names[] = {
13076 "PCH DPLL A",
13077 "PCH DPLL B",
13078};
13079
7c74ade1 13080static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13081{
e7b903d2 13082 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13083 int i;
13084
7c74ade1 13085 dev_priv->num_shared_dpll = 2;
ee7b9f93 13086
e72f9fbf 13087 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13088 dev_priv->shared_dplls[i].id = i;
13089 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13090 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13091 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13092 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13093 dev_priv->shared_dplls[i].get_hw_state =
13094 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13095 }
13096}
13097
7c74ade1
DV
13098static void intel_shared_dpll_init(struct drm_device *dev)
13099{
e7b903d2 13100 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13101
9cd86933
DV
13102 if (HAS_DDI(dev))
13103 intel_ddi_pll_init(dev);
13104 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13105 ibx_pch_dpll_init(dev);
13106 else
13107 dev_priv->num_shared_dpll = 0;
13108
13109 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13110}
13111
1fc0a8f7
TU
13112/**
13113 * intel_wm_need_update - Check whether watermarks need updating
13114 * @plane: drm plane
13115 * @state: new plane state
13116 *
13117 * Check current plane state versus the new one to determine whether
13118 * watermarks need to be recalculated.
13119 *
13120 * Returns true or false.
13121 */
13122bool intel_wm_need_update(struct drm_plane *plane,
13123 struct drm_plane_state *state)
13124{
13125 /* Update watermarks on tiling changes. */
13126 if (!plane->state->fb || !state->fb ||
13127 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13128 plane->state->rotation != state->rotation)
13129 return true;
13130
13131 return false;
13132}
13133
6beb8c23
MR
13134/**
13135 * intel_prepare_plane_fb - Prepare fb for usage on plane
13136 * @plane: drm plane to prepare for
13137 * @fb: framebuffer to prepare for presentation
13138 *
13139 * Prepares a framebuffer for usage on a display plane. Generally this
13140 * involves pinning the underlying object and updating the frontbuffer tracking
13141 * bits. Some older platforms need special physical address handling for
13142 * cursor planes.
13143 *
13144 * Returns 0 on success, negative error code on failure.
13145 */
13146int
13147intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13148 struct drm_framebuffer *fb,
13149 const struct drm_plane_state *new_state)
465c120c
MR
13150{
13151 struct drm_device *dev = plane->dev;
6beb8c23
MR
13152 struct intel_plane *intel_plane = to_intel_plane(plane);
13153 enum pipe pipe = intel_plane->pipe;
13154 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13155 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13156 unsigned frontbuffer_bits = 0;
13157 int ret = 0;
465c120c 13158
ea2c67bb 13159 if (!obj)
465c120c
MR
13160 return 0;
13161
6beb8c23
MR
13162 switch (plane->type) {
13163 case DRM_PLANE_TYPE_PRIMARY:
13164 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13165 break;
13166 case DRM_PLANE_TYPE_CURSOR:
13167 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13168 break;
13169 case DRM_PLANE_TYPE_OVERLAY:
13170 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13171 break;
13172 }
465c120c 13173
6beb8c23 13174 mutex_lock(&dev->struct_mutex);
465c120c 13175
6beb8c23
MR
13176 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13177 INTEL_INFO(dev)->cursor_needs_physical) {
13178 int align = IS_I830(dev) ? 16 * 1024 : 256;
13179 ret = i915_gem_object_attach_phys(obj, align);
13180 if (ret)
13181 DRM_DEBUG_KMS("failed to attach phys object\n");
13182 } else {
82bc3b2d 13183 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13184 }
465c120c 13185
6beb8c23
MR
13186 if (ret == 0)
13187 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13188
4c34574f 13189 mutex_unlock(&dev->struct_mutex);
465c120c 13190
6beb8c23
MR
13191 return ret;
13192}
13193
38f3ce3a
MR
13194/**
13195 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13196 * @plane: drm plane to clean up for
13197 * @fb: old framebuffer that was on plane
13198 *
13199 * Cleans up a framebuffer that has just been removed from a plane.
13200 */
13201void
13202intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13203 struct drm_framebuffer *fb,
13204 const struct drm_plane_state *old_state)
38f3ce3a
MR
13205{
13206 struct drm_device *dev = plane->dev;
13207 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13208
13209 if (WARN_ON(!obj))
13210 return;
13211
13212 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13213 !INTEL_INFO(dev)->cursor_needs_physical) {
13214 mutex_lock(&dev->struct_mutex);
82bc3b2d 13215 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13216 mutex_unlock(&dev->struct_mutex);
13217 }
465c120c
MR
13218}
13219
6156a456
CK
13220int
13221skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13222{
13223 int max_scale;
13224 struct drm_device *dev;
13225 struct drm_i915_private *dev_priv;
13226 int crtc_clock, cdclk;
13227
13228 if (!intel_crtc || !crtc_state)
13229 return DRM_PLANE_HELPER_NO_SCALING;
13230
13231 dev = intel_crtc->base.dev;
13232 dev_priv = dev->dev_private;
13233 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13234 cdclk = dev_priv->display.get_display_clock_speed(dev);
13235
13236 if (!crtc_clock || !cdclk)
13237 return DRM_PLANE_HELPER_NO_SCALING;
13238
13239 /*
13240 * skl max scale is lower of:
13241 * close to 3 but not 3, -1 is for that purpose
13242 * or
13243 * cdclk/crtc_clock
13244 */
13245 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13246
13247 return max_scale;
13248}
13249
465c120c 13250static int
3c692a41
GP
13251intel_check_primary_plane(struct drm_plane *plane,
13252 struct intel_plane_state *state)
13253{
32b7eeec
MR
13254 struct drm_device *dev = plane->dev;
13255 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13256 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13257 struct intel_crtc *intel_crtc;
6156a456 13258 struct intel_crtc_state *crtc_state;
2b875c22 13259 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13260 struct drm_rect *dest = &state->dst;
13261 struct drm_rect *src = &state->src;
13262 const struct drm_rect *clip = &state->clip;
d8106366 13263 bool can_position = false;
6156a456
CK
13264 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13265 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13266 int ret;
13267
ea2c67bb
MR
13268 crtc = crtc ? crtc : plane->crtc;
13269 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13270 crtc_state = state->base.state ?
13271 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13272
6156a456
CK
13273 if (INTEL_INFO(dev)->gen >= 9) {
13274 min_scale = 1;
13275 max_scale = skl_max_scale(intel_crtc, crtc_state);
d8106366 13276 can_position = true;
6156a456 13277 }
d8106366 13278
c59cb179
MR
13279 ret = drm_plane_helper_check_update(plane, crtc, fb,
13280 src, dest, clip,
6156a456
CK
13281 min_scale,
13282 max_scale,
d8106366
SJ
13283 can_position, true,
13284 &state->visible);
c59cb179
MR
13285 if (ret)
13286 return ret;
465c120c 13287
32b7eeec
MR
13288 if (intel_crtc->active) {
13289 intel_crtc->atomic.wait_for_flips = true;
13290
13291 /*
13292 * FBC does not work on some platforms for rotated
13293 * planes, so disable it when rotation is not 0 and
13294 * update it when rotation is set back to 0.
13295 *
13296 * FIXME: This is redundant with the fbc update done in
13297 * the primary plane enable function except that that
13298 * one is done too late. We eventually need to unify
13299 * this.
13300 */
13301 if (intel_crtc->primary_enabled &&
13302 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13303 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13304 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13305 intel_crtc->atomic.disable_fbc = true;
13306 }
13307
13308 if (state->visible) {
13309 /*
13310 * BDW signals flip done immediately if the plane
13311 * is disabled, even if the plane enable is already
13312 * armed to occur at the next vblank :(
13313 */
13314 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13315 intel_crtc->atomic.wait_vblank = true;
13316 }
13317
13318 intel_crtc->atomic.fb_bits |=
13319 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13320
13321 intel_crtc->atomic.update_fbc = true;
0fda6568 13322
1fc0a8f7 13323 if (intel_wm_need_update(plane, &state->base))
0fda6568 13324 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13325 }
13326
6156a456
CK
13327 if (INTEL_INFO(dev)->gen >= 9) {
13328 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13329 to_intel_plane(plane), state, 0);
13330 if (ret)
13331 return ret;
13332 }
13333
14af293f
GP
13334 return 0;
13335}
13336
13337static void
13338intel_commit_primary_plane(struct drm_plane *plane,
13339 struct intel_plane_state *state)
13340{
2b875c22
MR
13341 struct drm_crtc *crtc = state->base.crtc;
13342 struct drm_framebuffer *fb = state->base.fb;
13343 struct drm_device *dev = plane->dev;
14af293f 13344 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13345 struct intel_crtc *intel_crtc;
14af293f
GP
13346 struct drm_rect *src = &state->src;
13347
ea2c67bb
MR
13348 crtc = crtc ? crtc : plane->crtc;
13349 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13350
13351 plane->fb = fb;
9dc806fc
MR
13352 crtc->x = src->x1 >> 16;
13353 crtc->y = src->y1 >> 16;
ccc759dc 13354
ccc759dc 13355 if (intel_crtc->active) {
ccc759dc 13356 if (state->visible) {
ccc759dc
GP
13357 /* FIXME: kill this fastboot hack */
13358 intel_update_pipe_size(intel_crtc);
465c120c 13359
ccc759dc 13360 intel_crtc->primary_enabled = true;
465c120c 13361
ccc759dc
GP
13362 dev_priv->display.update_primary_plane(crtc, plane->fb,
13363 crtc->x, crtc->y);
ccc759dc
GP
13364 } else {
13365 /*
13366 * If clipping results in a non-visible primary plane,
13367 * we'll disable the primary plane. Note that this is
13368 * a bit different than what happens if userspace
13369 * explicitly disables the plane by passing fb=0
13370 * because plane->fb still gets set and pinned.
13371 */
13372 intel_disable_primary_hw_plane(plane, crtc);
48404c1e 13373 }
ccc759dc 13374 }
465c120c
MR
13375}
13376
32b7eeec 13377static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13378{
32b7eeec 13379 struct drm_device *dev = crtc->dev;
140fd38d 13380 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13382 struct intel_plane *intel_plane;
13383 struct drm_plane *p;
13384 unsigned fb_bits = 0;
13385
13386 /* Track fb's for any planes being disabled */
13387 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13388 intel_plane = to_intel_plane(p);
13389
13390 if (intel_crtc->atomic.disabled_planes &
13391 (1 << drm_plane_index(p))) {
13392 switch (p->type) {
13393 case DRM_PLANE_TYPE_PRIMARY:
13394 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13395 break;
13396 case DRM_PLANE_TYPE_CURSOR:
13397 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13398 break;
13399 case DRM_PLANE_TYPE_OVERLAY:
13400 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13401 break;
13402 }
3c692a41 13403
ea2c67bb
MR
13404 mutex_lock(&dev->struct_mutex);
13405 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13406 mutex_unlock(&dev->struct_mutex);
13407 }
13408 }
3c692a41 13409
32b7eeec
MR
13410 if (intel_crtc->atomic.wait_for_flips)
13411 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13412
32b7eeec
MR
13413 if (intel_crtc->atomic.disable_fbc)
13414 intel_fbc_disable(dev);
3c692a41 13415
32b7eeec
MR
13416 if (intel_crtc->atomic.pre_disable_primary)
13417 intel_pre_disable_primary(crtc);
3c692a41 13418
32b7eeec
MR
13419 if (intel_crtc->atomic.update_wm)
13420 intel_update_watermarks(crtc);
3c692a41 13421
32b7eeec 13422 intel_runtime_pm_get(dev_priv);
3c692a41 13423
c34c9ee4
MR
13424 /* Perform vblank evasion around commit operation */
13425 if (intel_crtc->active)
13426 intel_crtc->atomic.evade =
13427 intel_pipe_update_start(intel_crtc,
13428 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13429}
13430
13431static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13432{
13433 struct drm_device *dev = crtc->dev;
13434 struct drm_i915_private *dev_priv = dev->dev_private;
13435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13436 struct drm_plane *p;
13437
c34c9ee4
MR
13438 if (intel_crtc->atomic.evade)
13439 intel_pipe_update_end(intel_crtc,
13440 intel_crtc->atomic.start_vbl_count);
3c692a41 13441
140fd38d 13442 intel_runtime_pm_put(dev_priv);
3c692a41 13443
32b7eeec
MR
13444 if (intel_crtc->atomic.wait_vblank)
13445 intel_wait_for_vblank(dev, intel_crtc->pipe);
13446
13447 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13448
13449 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13450 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13451 intel_fbc_update(dev);
ccc759dc 13452 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13453 }
3c692a41 13454
32b7eeec
MR
13455 if (intel_crtc->atomic.post_enable_primary)
13456 intel_post_enable_primary(crtc);
3c692a41 13457
32b7eeec
MR
13458 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13459 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13460 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13461 false, false);
13462
13463 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13464}
13465
cf4c7c12 13466/**
4a3b8769
MR
13467 * intel_plane_destroy - destroy a plane
13468 * @plane: plane to destroy
cf4c7c12 13469 *
4a3b8769
MR
13470 * Common destruction function for all types of planes (primary, cursor,
13471 * sprite).
cf4c7c12 13472 */
4a3b8769 13473void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13474{
13475 struct intel_plane *intel_plane = to_intel_plane(plane);
13476 drm_plane_cleanup(plane);
13477 kfree(intel_plane);
13478}
13479
65a3fea0 13480const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13481 .update_plane = drm_atomic_helper_update_plane,
13482 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13483 .destroy = intel_plane_destroy,
c196e1d6 13484 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13485 .atomic_get_property = intel_plane_atomic_get_property,
13486 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13487 .atomic_duplicate_state = intel_plane_duplicate_state,
13488 .atomic_destroy_state = intel_plane_destroy_state,
13489
465c120c
MR
13490};
13491
13492static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13493 int pipe)
13494{
13495 struct intel_plane *primary;
8e7d688b 13496 struct intel_plane_state *state;
465c120c
MR
13497 const uint32_t *intel_primary_formats;
13498 int num_formats;
13499
13500 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13501 if (primary == NULL)
13502 return NULL;
13503
8e7d688b
MR
13504 state = intel_create_plane_state(&primary->base);
13505 if (!state) {
ea2c67bb
MR
13506 kfree(primary);
13507 return NULL;
13508 }
8e7d688b 13509 primary->base.state = &state->base;
ea2c67bb 13510
465c120c
MR
13511 primary->can_scale = false;
13512 primary->max_downscale = 1;
6156a456
CK
13513 if (INTEL_INFO(dev)->gen >= 9) {
13514 primary->can_scale = true;
13515 }
549e2bfb 13516 state->scaler_id = -1;
465c120c
MR
13517 primary->pipe = pipe;
13518 primary->plane = pipe;
c59cb179
MR
13519 primary->check_plane = intel_check_primary_plane;
13520 primary->commit_plane = intel_commit_primary_plane;
08e221fb 13521 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13522 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13523 primary->plane = !pipe;
13524
13525 if (INTEL_INFO(dev)->gen <= 3) {
13526 intel_primary_formats = intel_primary_formats_gen2;
13527 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13528 } else {
13529 intel_primary_formats = intel_primary_formats_gen4;
13530 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13531 }
13532
13533 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13534 &intel_plane_funcs,
465c120c
MR
13535 intel_primary_formats, num_formats,
13536 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13537
3b7a5119
SJ
13538 if (INTEL_INFO(dev)->gen >= 4)
13539 intel_create_rotation_property(dev, primary);
48404c1e 13540
ea2c67bb
MR
13541 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13542
465c120c
MR
13543 return &primary->base;
13544}
13545
3b7a5119
SJ
13546void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13547{
13548 if (!dev->mode_config.rotation_property) {
13549 unsigned long flags = BIT(DRM_ROTATE_0) |
13550 BIT(DRM_ROTATE_180);
13551
13552 if (INTEL_INFO(dev)->gen >= 9)
13553 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13554
13555 dev->mode_config.rotation_property =
13556 drm_mode_create_rotation_property(dev, flags);
13557 }
13558 if (dev->mode_config.rotation_property)
13559 drm_object_attach_property(&plane->base.base,
13560 dev->mode_config.rotation_property,
13561 plane->base.state->rotation);
13562}
13563
3d7d6510 13564static int
852e787c
GP
13565intel_check_cursor_plane(struct drm_plane *plane,
13566 struct intel_plane_state *state)
3d7d6510 13567{
2b875c22 13568 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13569 struct drm_device *dev = plane->dev;
2b875c22 13570 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13571 struct drm_rect *dest = &state->dst;
13572 struct drm_rect *src = &state->src;
13573 const struct drm_rect *clip = &state->clip;
757f9a3e 13574 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13575 struct intel_crtc *intel_crtc;
757f9a3e
GP
13576 unsigned stride;
13577 int ret;
3d7d6510 13578
ea2c67bb
MR
13579 crtc = crtc ? crtc : plane->crtc;
13580 intel_crtc = to_intel_crtc(crtc);
13581
757f9a3e 13582 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13583 src, dest, clip,
3d7d6510
MR
13584 DRM_PLANE_HELPER_NO_SCALING,
13585 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13586 true, true, &state->visible);
757f9a3e
GP
13587 if (ret)
13588 return ret;
13589
13590
13591 /* if we want to turn off the cursor ignore width and height */
13592 if (!obj)
32b7eeec 13593 goto finish;
757f9a3e 13594
757f9a3e 13595 /* Check for which cursor types we support */
ea2c67bb
MR
13596 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13597 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13598 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13599 return -EINVAL;
13600 }
13601
ea2c67bb
MR
13602 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13603 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13604 DRM_DEBUG_KMS("buffer is too small\n");
13605 return -ENOMEM;
13606 }
13607
3a656b54 13608 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13609 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13610 ret = -EINVAL;
13611 }
757f9a3e 13612
32b7eeec
MR
13613finish:
13614 if (intel_crtc->active) {
3749f463 13615 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13616 intel_crtc->atomic.update_wm = true;
13617
13618 intel_crtc->atomic.fb_bits |=
13619 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13620 }
13621
757f9a3e 13622 return ret;
852e787c 13623}
3d7d6510 13624
f4a2cf29 13625static void
852e787c
GP
13626intel_commit_cursor_plane(struct drm_plane *plane,
13627 struct intel_plane_state *state)
13628{
2b875c22 13629 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13630 struct drm_device *dev = plane->dev;
13631 struct intel_crtc *intel_crtc;
2b875c22 13632 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13633 uint32_t addr;
852e787c 13634
ea2c67bb
MR
13635 crtc = crtc ? crtc : plane->crtc;
13636 intel_crtc = to_intel_crtc(crtc);
13637
2b875c22 13638 plane->fb = state->base.fb;
ea2c67bb
MR
13639 crtc->cursor_x = state->base.crtc_x;
13640 crtc->cursor_y = state->base.crtc_y;
13641
a912f12f
GP
13642 if (intel_crtc->cursor_bo == obj)
13643 goto update;
4ed91096 13644
f4a2cf29 13645 if (!obj)
a912f12f 13646 addr = 0;
f4a2cf29 13647 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13648 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13649 else
a912f12f 13650 addr = obj->phys_handle->busaddr;
852e787c 13651
a912f12f
GP
13652 intel_crtc->cursor_addr = addr;
13653 intel_crtc->cursor_bo = obj;
13654update:
852e787c 13655
32b7eeec 13656 if (intel_crtc->active)
a912f12f 13657 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13658}
13659
3d7d6510
MR
13660static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13661 int pipe)
13662{
13663 struct intel_plane *cursor;
8e7d688b 13664 struct intel_plane_state *state;
3d7d6510
MR
13665
13666 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13667 if (cursor == NULL)
13668 return NULL;
13669
8e7d688b
MR
13670 state = intel_create_plane_state(&cursor->base);
13671 if (!state) {
ea2c67bb
MR
13672 kfree(cursor);
13673 return NULL;
13674 }
8e7d688b 13675 cursor->base.state = &state->base;
ea2c67bb 13676
3d7d6510
MR
13677 cursor->can_scale = false;
13678 cursor->max_downscale = 1;
13679 cursor->pipe = pipe;
13680 cursor->plane = pipe;
549e2bfb 13681 state->scaler_id = -1;
c59cb179
MR
13682 cursor->check_plane = intel_check_cursor_plane;
13683 cursor->commit_plane = intel_commit_cursor_plane;
3d7d6510
MR
13684
13685 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13686 &intel_plane_funcs,
3d7d6510
MR
13687 intel_cursor_formats,
13688 ARRAY_SIZE(intel_cursor_formats),
13689 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13690
13691 if (INTEL_INFO(dev)->gen >= 4) {
13692 if (!dev->mode_config.rotation_property)
13693 dev->mode_config.rotation_property =
13694 drm_mode_create_rotation_property(dev,
13695 BIT(DRM_ROTATE_0) |
13696 BIT(DRM_ROTATE_180));
13697 if (dev->mode_config.rotation_property)
13698 drm_object_attach_property(&cursor->base.base,
13699 dev->mode_config.rotation_property,
8e7d688b 13700 state->base.rotation);
4398ad45
VS
13701 }
13702
ea2c67bb
MR
13703 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13704
3d7d6510
MR
13705 return &cursor->base;
13706}
13707
549e2bfb
CK
13708static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13709 struct intel_crtc_state *crtc_state)
13710{
13711 int i;
13712 struct intel_scaler *intel_scaler;
13713 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13714
13715 for (i = 0; i < intel_crtc->num_scalers; i++) {
13716 intel_scaler = &scaler_state->scalers[i];
13717 intel_scaler->in_use = 0;
13718 intel_scaler->id = i;
13719
13720 intel_scaler->mode = PS_SCALER_MODE_DYN;
13721 }
13722
13723 scaler_state->scaler_id = -1;
13724}
13725
b358d0a6 13726static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13727{
fbee40df 13728 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13729 struct intel_crtc *intel_crtc;
f5de6e07 13730 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13731 struct drm_plane *primary = NULL;
13732 struct drm_plane *cursor = NULL;
465c120c 13733 int i, ret;
79e53945 13734
955382f3 13735 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13736 if (intel_crtc == NULL)
13737 return;
13738
f5de6e07
ACO
13739 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13740 if (!crtc_state)
13741 goto fail;
13742 intel_crtc_set_state(intel_crtc, crtc_state);
07878248 13743 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13744
549e2bfb
CK
13745 /* initialize shared scalers */
13746 if (INTEL_INFO(dev)->gen >= 9) {
13747 if (pipe == PIPE_C)
13748 intel_crtc->num_scalers = 1;
13749 else
13750 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13751
13752 skl_init_scalers(dev, intel_crtc, crtc_state);
13753 }
13754
465c120c 13755 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13756 if (!primary)
13757 goto fail;
13758
13759 cursor = intel_cursor_plane_create(dev, pipe);
13760 if (!cursor)
13761 goto fail;
13762
465c120c 13763 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
13764 cursor, &intel_crtc_funcs);
13765 if (ret)
13766 goto fail;
79e53945
JB
13767
13768 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
13769 for (i = 0; i < 256; i++) {
13770 intel_crtc->lut_r[i] = i;
13771 intel_crtc->lut_g[i] = i;
13772 intel_crtc->lut_b[i] = i;
13773 }
13774
1f1c2e24
VS
13775 /*
13776 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13777 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13778 */
80824003
JB
13779 intel_crtc->pipe = pipe;
13780 intel_crtc->plane = pipe;
3a77c4c4 13781 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13782 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13783 intel_crtc->plane = !pipe;
80824003
JB
13784 }
13785
4b0e333e
CW
13786 intel_crtc->cursor_base = ~0;
13787 intel_crtc->cursor_cntl = ~0;
dc41c154 13788 intel_crtc->cursor_size = ~0;
8d7849db 13789
22fd0fab
JB
13790 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13791 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13792 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13793 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13794
9362c7c5
ACO
13795 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13796
79e53945 13797 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
13798
13799 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13800 return;
13801
13802fail:
13803 if (primary)
13804 drm_plane_cleanup(primary);
13805 if (cursor)
13806 drm_plane_cleanup(cursor);
f5de6e07 13807 kfree(crtc_state);
3d7d6510 13808 kfree(intel_crtc);
79e53945
JB
13809}
13810
752aa88a
JB
13811enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13812{
13813 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13814 struct drm_device *dev = connector->base.dev;
752aa88a 13815
51fd371b 13816 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13817
d3babd3f 13818 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13819 return INVALID_PIPE;
13820
13821 return to_intel_crtc(encoder->crtc)->pipe;
13822}
13823
08d7b3d1 13824int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13825 struct drm_file *file)
08d7b3d1 13826{
08d7b3d1 13827 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13828 struct drm_crtc *drmmode_crtc;
c05422d5 13829 struct intel_crtc *crtc;
08d7b3d1 13830
7707e653 13831 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13832
7707e653 13833 if (!drmmode_crtc) {
08d7b3d1 13834 DRM_ERROR("no such CRTC id\n");
3f2c2057 13835 return -ENOENT;
08d7b3d1
CW
13836 }
13837
7707e653 13838 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13839 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13840
c05422d5 13841 return 0;
08d7b3d1
CW
13842}
13843
66a9278e 13844static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13845{
66a9278e
DV
13846 struct drm_device *dev = encoder->base.dev;
13847 struct intel_encoder *source_encoder;
79e53945 13848 int index_mask = 0;
79e53945
JB
13849 int entry = 0;
13850
b2784e15 13851 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13852 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13853 index_mask |= (1 << entry);
13854
79e53945
JB
13855 entry++;
13856 }
4ef69c7a 13857
79e53945
JB
13858 return index_mask;
13859}
13860
4d302442
CW
13861static bool has_edp_a(struct drm_device *dev)
13862{
13863 struct drm_i915_private *dev_priv = dev->dev_private;
13864
13865 if (!IS_MOBILE(dev))
13866 return false;
13867
13868 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13869 return false;
13870
e3589908 13871 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13872 return false;
13873
13874 return true;
13875}
13876
84b4e042
JB
13877static bool intel_crt_present(struct drm_device *dev)
13878{
13879 struct drm_i915_private *dev_priv = dev->dev_private;
13880
884497ed
DL
13881 if (INTEL_INFO(dev)->gen >= 9)
13882 return false;
13883
cf404ce4 13884 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13885 return false;
13886
13887 if (IS_CHERRYVIEW(dev))
13888 return false;
13889
13890 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13891 return false;
13892
13893 return true;
13894}
13895
79e53945
JB
13896static void intel_setup_outputs(struct drm_device *dev)
13897{
725e30ad 13898 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13899 struct intel_encoder *encoder;
cb0953d7 13900 bool dpd_is_edp = false;
79e53945 13901
c9093354 13902 intel_lvds_init(dev);
79e53945 13903
84b4e042 13904 if (intel_crt_present(dev))
79935fca 13905 intel_crt_init(dev);
cb0953d7 13906
c776eb2e
VK
13907 if (IS_BROXTON(dev)) {
13908 /*
13909 * FIXME: Broxton doesn't support port detection via the
13910 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13911 * detect the ports.
13912 */
13913 intel_ddi_init(dev, PORT_A);
13914 intel_ddi_init(dev, PORT_B);
13915 intel_ddi_init(dev, PORT_C);
13916 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13917 int found;
13918
de31facd
JB
13919 /*
13920 * Haswell uses DDI functions to detect digital outputs.
13921 * On SKL pre-D0 the strap isn't connected, so we assume
13922 * it's there.
13923 */
0e72a5b5 13924 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
13925 /* WaIgnoreDDIAStrap: skl */
13926 if (found ||
13927 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
13928 intel_ddi_init(dev, PORT_A);
13929
13930 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13931 * register */
13932 found = I915_READ(SFUSE_STRAP);
13933
13934 if (found & SFUSE_STRAP_DDIB_DETECTED)
13935 intel_ddi_init(dev, PORT_B);
13936 if (found & SFUSE_STRAP_DDIC_DETECTED)
13937 intel_ddi_init(dev, PORT_C);
13938 if (found & SFUSE_STRAP_DDID_DETECTED)
13939 intel_ddi_init(dev, PORT_D);
13940 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 13941 int found;
5d8a7752 13942 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
13943
13944 if (has_edp_a(dev))
13945 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 13946
dc0fa718 13947 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13948 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 13949 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 13950 if (!found)
e2debe91 13951 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 13952 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 13953 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
13954 }
13955
dc0fa718 13956 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 13957 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 13958
dc0fa718 13959 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 13960 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 13961
5eb08b69 13962 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 13963 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 13964
270b3042 13965 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 13966 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 13967 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
13968 /*
13969 * The DP_DETECTED bit is the latched state of the DDC
13970 * SDA pin at boot. However since eDP doesn't require DDC
13971 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13972 * eDP ports may have been muxed to an alternate function.
13973 * Thus we can't rely on the DP_DETECTED bit alone to detect
13974 * eDP ports. Consult the VBT as well as DP_DETECTED to
13975 * detect eDP ports.
13976 */
d2182a66
VS
13977 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13978 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
13979 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13980 PORT_B);
e17ac6db
VS
13981 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13982 intel_dp_is_edp(dev, PORT_B))
13983 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 13984
d2182a66
VS
13985 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13986 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
13987 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13988 PORT_C);
e17ac6db
VS
13989 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13990 intel_dp_is_edp(dev, PORT_C))
13991 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 13992
9418c1f1 13993 if (IS_CHERRYVIEW(dev)) {
e17ac6db 13994 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
13995 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13996 PORT_D);
e17ac6db
VS
13997 /* eDP not supported on port D, so don't check VBT */
13998 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13999 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14000 }
14001
3cfca973 14002 intel_dsi_init(dev);
103a196f 14003 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14004 bool found = false;
7d57382e 14005
e2debe91 14006 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14007 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14008 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14009 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14010 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14011 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14012 }
27185ae1 14013
e7281eab 14014 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14015 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14016 }
13520b05
KH
14017
14018 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14019
e2debe91 14020 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14021 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14022 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14023 }
27185ae1 14024
e2debe91 14025 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14026
b01f2c3a
JB
14027 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14028 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14029 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14030 }
e7281eab 14031 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14032 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14033 }
27185ae1 14034
b01f2c3a 14035 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14036 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14037 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14038 } else if (IS_GEN2(dev))
79e53945
JB
14039 intel_dvo_init(dev);
14040
103a196f 14041 if (SUPPORTS_TV(dev))
79e53945
JB
14042 intel_tv_init(dev);
14043
0bc12bcb 14044 intel_psr_init(dev);
7c8f8a70 14045
b2784e15 14046 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14047 encoder->base.possible_crtcs = encoder->crtc_mask;
14048 encoder->base.possible_clones =
66a9278e 14049 intel_encoder_clones(encoder);
79e53945 14050 }
47356eb6 14051
dde86e2d 14052 intel_init_pch_refclk(dev);
270b3042
DV
14053
14054 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14055}
14056
14057static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14058{
60a5ca01 14059 struct drm_device *dev = fb->dev;
79e53945 14060 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14061
ef2d633e 14062 drm_framebuffer_cleanup(fb);
60a5ca01 14063 mutex_lock(&dev->struct_mutex);
ef2d633e 14064 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14065 drm_gem_object_unreference(&intel_fb->obj->base);
14066 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14067 kfree(intel_fb);
14068}
14069
14070static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14071 struct drm_file *file,
79e53945
JB
14072 unsigned int *handle)
14073{
14074 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14075 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14076
05394f39 14077 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14078}
14079
14080static const struct drm_framebuffer_funcs intel_fb_funcs = {
14081 .destroy = intel_user_framebuffer_destroy,
14082 .create_handle = intel_user_framebuffer_create_handle,
14083};
14084
b321803d
DL
14085static
14086u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14087 uint32_t pixel_format)
14088{
14089 u32 gen = INTEL_INFO(dev)->gen;
14090
14091 if (gen >= 9) {
14092 /* "The stride in bytes must not exceed the of the size of 8K
14093 * pixels and 32K bytes."
14094 */
14095 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14096 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14097 return 32*1024;
14098 } else if (gen >= 4) {
14099 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14100 return 16*1024;
14101 else
14102 return 32*1024;
14103 } else if (gen >= 3) {
14104 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14105 return 8*1024;
14106 else
14107 return 16*1024;
14108 } else {
14109 /* XXX DSPC is limited to 4k tiled */
14110 return 8*1024;
14111 }
14112}
14113
b5ea642a
DV
14114static int intel_framebuffer_init(struct drm_device *dev,
14115 struct intel_framebuffer *intel_fb,
14116 struct drm_mode_fb_cmd2 *mode_cmd,
14117 struct drm_i915_gem_object *obj)
79e53945 14118{
6761dd31 14119 unsigned int aligned_height;
79e53945 14120 int ret;
b321803d 14121 u32 pitch_limit, stride_alignment;
79e53945 14122
dd4916c5
DV
14123 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14124
2a80eada
DV
14125 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14126 /* Enforce that fb modifier and tiling mode match, but only for
14127 * X-tiled. This is needed for FBC. */
14128 if (!!(obj->tiling_mode == I915_TILING_X) !=
14129 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14130 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14131 return -EINVAL;
14132 }
14133 } else {
14134 if (obj->tiling_mode == I915_TILING_X)
14135 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14136 else if (obj->tiling_mode == I915_TILING_Y) {
14137 DRM_DEBUG("No Y tiling for legacy addfb\n");
14138 return -EINVAL;
14139 }
14140 }
14141
9a8f0a12
TU
14142 /* Passed in modifier sanity checking. */
14143 switch (mode_cmd->modifier[0]) {
14144 case I915_FORMAT_MOD_Y_TILED:
14145 case I915_FORMAT_MOD_Yf_TILED:
14146 if (INTEL_INFO(dev)->gen < 9) {
14147 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14148 mode_cmd->modifier[0]);
14149 return -EINVAL;
14150 }
14151 case DRM_FORMAT_MOD_NONE:
14152 case I915_FORMAT_MOD_X_TILED:
14153 break;
14154 default:
c0f40428
JB
14155 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14156 mode_cmd->modifier[0]);
57cd6508 14157 return -EINVAL;
c16ed4be 14158 }
57cd6508 14159
b321803d
DL
14160 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14161 mode_cmd->pixel_format);
14162 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14163 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14164 mode_cmd->pitches[0], stride_alignment);
57cd6508 14165 return -EINVAL;
c16ed4be 14166 }
57cd6508 14167
b321803d
DL
14168 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14169 mode_cmd->pixel_format);
a35cdaa0 14170 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14171 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14172 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14173 "tiled" : "linear",
a35cdaa0 14174 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14175 return -EINVAL;
c16ed4be 14176 }
5d7bd705 14177
2a80eada 14178 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14179 mode_cmd->pitches[0] != obj->stride) {
14180 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14181 mode_cmd->pitches[0], obj->stride);
5d7bd705 14182 return -EINVAL;
c16ed4be 14183 }
5d7bd705 14184
57779d06 14185 /* Reject formats not supported by any plane early. */
308e5bcb 14186 switch (mode_cmd->pixel_format) {
57779d06 14187 case DRM_FORMAT_C8:
04b3924d
VS
14188 case DRM_FORMAT_RGB565:
14189 case DRM_FORMAT_XRGB8888:
14190 case DRM_FORMAT_ARGB8888:
57779d06
VS
14191 break;
14192 case DRM_FORMAT_XRGB1555:
14193 case DRM_FORMAT_ARGB1555:
c16ed4be 14194 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14195 DRM_DEBUG("unsupported pixel format: %s\n",
14196 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14197 return -EINVAL;
c16ed4be 14198 }
57779d06
VS
14199 break;
14200 case DRM_FORMAT_XBGR8888:
14201 case DRM_FORMAT_ABGR8888:
04b3924d
VS
14202 case DRM_FORMAT_XRGB2101010:
14203 case DRM_FORMAT_ARGB2101010:
57779d06
VS
14204 case DRM_FORMAT_XBGR2101010:
14205 case DRM_FORMAT_ABGR2101010:
c16ed4be 14206 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14207 DRM_DEBUG("unsupported pixel format: %s\n",
14208 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14209 return -EINVAL;
c16ed4be 14210 }
b5626747 14211 break;
04b3924d
VS
14212 case DRM_FORMAT_YUYV:
14213 case DRM_FORMAT_UYVY:
14214 case DRM_FORMAT_YVYU:
14215 case DRM_FORMAT_VYUY:
c16ed4be 14216 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14217 DRM_DEBUG("unsupported pixel format: %s\n",
14218 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14219 return -EINVAL;
c16ed4be 14220 }
57cd6508
CW
14221 break;
14222 default:
4ee62c76
VS
14223 DRM_DEBUG("unsupported pixel format: %s\n",
14224 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14225 return -EINVAL;
14226 }
14227
90f9a336
VS
14228 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14229 if (mode_cmd->offsets[0] != 0)
14230 return -EINVAL;
14231
ec2c981e 14232 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14233 mode_cmd->pixel_format,
14234 mode_cmd->modifier[0]);
53155c0a
DV
14235 /* FIXME drm helper for size checks (especially planar formats)? */
14236 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14237 return -EINVAL;
14238
c7d73f6a
DV
14239 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14240 intel_fb->obj = obj;
80075d49 14241 intel_fb->obj->framebuffer_references++;
c7d73f6a 14242
79e53945
JB
14243 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14244 if (ret) {
14245 DRM_ERROR("framebuffer init failed %d\n", ret);
14246 return ret;
14247 }
14248
79e53945
JB
14249 return 0;
14250}
14251
79e53945
JB
14252static struct drm_framebuffer *
14253intel_user_framebuffer_create(struct drm_device *dev,
14254 struct drm_file *filp,
308e5bcb 14255 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14256{
05394f39 14257 struct drm_i915_gem_object *obj;
79e53945 14258
308e5bcb
JB
14259 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14260 mode_cmd->handles[0]));
c8725226 14261 if (&obj->base == NULL)
cce13ff7 14262 return ERR_PTR(-ENOENT);
79e53945 14263
d2dff872 14264 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14265}
14266
4520f53a 14267#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14268static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14269{
14270}
14271#endif
14272
79e53945 14273static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14274 .fb_create = intel_user_framebuffer_create,
0632fef6 14275 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14276 .atomic_check = intel_atomic_check,
14277 .atomic_commit = intel_atomic_commit,
79e53945
JB
14278};
14279
e70236a8
JB
14280/* Set up chip specific display functions */
14281static void intel_init_display(struct drm_device *dev)
14282{
14283 struct drm_i915_private *dev_priv = dev->dev_private;
14284
ee9300bb
DV
14285 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14286 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14287 else if (IS_CHERRYVIEW(dev))
14288 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14289 else if (IS_VALLEYVIEW(dev))
14290 dev_priv->display.find_dpll = vlv_find_best_dpll;
14291 else if (IS_PINEVIEW(dev))
14292 dev_priv->display.find_dpll = pnv_find_best_dpll;
14293 else
14294 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14295
bc8d7dff
DL
14296 if (INTEL_INFO(dev)->gen >= 9) {
14297 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14298 dev_priv->display.get_initial_plane_config =
14299 skylake_get_initial_plane_config;
bc8d7dff
DL
14300 dev_priv->display.crtc_compute_clock =
14301 haswell_crtc_compute_clock;
14302 dev_priv->display.crtc_enable = haswell_crtc_enable;
14303 dev_priv->display.crtc_disable = haswell_crtc_disable;
14304 dev_priv->display.off = ironlake_crtc_off;
14305 dev_priv->display.update_primary_plane =
14306 skylake_update_primary_plane;
14307 } else if (HAS_DDI(dev)) {
0e8ffe1b 14308 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14309 dev_priv->display.get_initial_plane_config =
14310 ironlake_get_initial_plane_config;
797d0259
ACO
14311 dev_priv->display.crtc_compute_clock =
14312 haswell_crtc_compute_clock;
4f771f10
PZ
14313 dev_priv->display.crtc_enable = haswell_crtc_enable;
14314 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 14315 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
14316 dev_priv->display.update_primary_plane =
14317 ironlake_update_primary_plane;
09b4ddf9 14318 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14319 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14320 dev_priv->display.get_initial_plane_config =
14321 ironlake_get_initial_plane_config;
3fb37703
ACO
14322 dev_priv->display.crtc_compute_clock =
14323 ironlake_crtc_compute_clock;
76e5a89c
DV
14324 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14325 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 14326 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
14327 dev_priv->display.update_primary_plane =
14328 ironlake_update_primary_plane;
89b667f8
JB
14329 } else if (IS_VALLEYVIEW(dev)) {
14330 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14331 dev_priv->display.get_initial_plane_config =
14332 i9xx_get_initial_plane_config;
d6dfee7a 14333 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14334 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14335 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14336 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14337 dev_priv->display.update_primary_plane =
14338 i9xx_update_primary_plane;
f564048e 14339 } else {
0e8ffe1b 14340 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14341 dev_priv->display.get_initial_plane_config =
14342 i9xx_get_initial_plane_config;
d6dfee7a 14343 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14344 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14345 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 14346 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14347 dev_priv->display.update_primary_plane =
14348 i9xx_update_primary_plane;
f564048e 14349 }
e70236a8 14350
e70236a8 14351 /* Returns the core display clock speed */
1652d19e
VS
14352 if (IS_SKYLAKE(dev))
14353 dev_priv->display.get_display_clock_speed =
14354 skylake_get_display_clock_speed;
14355 else if (IS_BROADWELL(dev))
14356 dev_priv->display.get_display_clock_speed =
14357 broadwell_get_display_clock_speed;
14358 else if (IS_HASWELL(dev))
14359 dev_priv->display.get_display_clock_speed =
14360 haswell_get_display_clock_speed;
14361 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14362 dev_priv->display.get_display_clock_speed =
14363 valleyview_get_display_clock_speed;
b37a6434
VS
14364 else if (IS_GEN5(dev))
14365 dev_priv->display.get_display_clock_speed =
14366 ilk_get_display_clock_speed;
a7c66cd8
VS
14367 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14368 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
14369 dev_priv->display.get_display_clock_speed =
14370 i945_get_display_clock_speed;
14371 else if (IS_I915G(dev))
14372 dev_priv->display.get_display_clock_speed =
14373 i915_get_display_clock_speed;
257a7ffc 14374 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14375 dev_priv->display.get_display_clock_speed =
14376 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14377 else if (IS_PINEVIEW(dev))
14378 dev_priv->display.get_display_clock_speed =
14379 pnv_get_display_clock_speed;
e70236a8
JB
14380 else if (IS_I915GM(dev))
14381 dev_priv->display.get_display_clock_speed =
14382 i915gm_get_display_clock_speed;
14383 else if (IS_I865G(dev))
14384 dev_priv->display.get_display_clock_speed =
14385 i865_get_display_clock_speed;
f0f8a9ce 14386 else if (IS_I85X(dev))
e70236a8
JB
14387 dev_priv->display.get_display_clock_speed =
14388 i855_get_display_clock_speed;
14389 else /* 852, 830 */
14390 dev_priv->display.get_display_clock_speed =
14391 i830_get_display_clock_speed;
14392
7c10a2b5 14393 if (IS_GEN5(dev)) {
3bb11b53 14394 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14395 } else if (IS_GEN6(dev)) {
14396 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14397 } else if (IS_IVYBRIDGE(dev)) {
14398 /* FIXME: detect B0+ stepping and use auto training */
14399 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14400 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14401 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
14402 } else if (IS_VALLEYVIEW(dev)) {
14403 dev_priv->display.modeset_global_resources =
14404 valleyview_modeset_global_resources;
f8437dd1
VK
14405 } else if (IS_BROXTON(dev)) {
14406 dev_priv->display.modeset_global_resources =
14407 broxton_modeset_global_resources;
e70236a8 14408 }
8c9f3aaf 14409
8c9f3aaf
JB
14410 switch (INTEL_INFO(dev)->gen) {
14411 case 2:
14412 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14413 break;
14414
14415 case 3:
14416 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14417 break;
14418
14419 case 4:
14420 case 5:
14421 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14422 break;
14423
14424 case 6:
14425 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14426 break;
7c9017e5 14427 case 7:
4e0bbc31 14428 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14429 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14430 break;
830c81db 14431 case 9:
ba343e02
TU
14432 /* Drop through - unsupported since execlist only. */
14433 default:
14434 /* Default just returns -ENODEV to indicate unsupported */
14435 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14436 }
7bd688cd
JN
14437
14438 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14439
14440 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14441}
14442
b690e96c
JB
14443/*
14444 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14445 * resume, or other times. This quirk makes sure that's the case for
14446 * affected systems.
14447 */
0206e353 14448static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14449{
14450 struct drm_i915_private *dev_priv = dev->dev_private;
14451
14452 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14453 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14454}
14455
b6b5d049
VS
14456static void quirk_pipeb_force(struct drm_device *dev)
14457{
14458 struct drm_i915_private *dev_priv = dev->dev_private;
14459
14460 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14461 DRM_INFO("applying pipe b force quirk\n");
14462}
14463
435793df
KP
14464/*
14465 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14466 */
14467static void quirk_ssc_force_disable(struct drm_device *dev)
14468{
14469 struct drm_i915_private *dev_priv = dev->dev_private;
14470 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14471 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14472}
14473
4dca20ef 14474/*
5a15ab5b
CE
14475 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14476 * brightness value
4dca20ef
CE
14477 */
14478static void quirk_invert_brightness(struct drm_device *dev)
14479{
14480 struct drm_i915_private *dev_priv = dev->dev_private;
14481 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14482 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14483}
14484
9c72cc6f
SD
14485/* Some VBT's incorrectly indicate no backlight is present */
14486static void quirk_backlight_present(struct drm_device *dev)
14487{
14488 struct drm_i915_private *dev_priv = dev->dev_private;
14489 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14490 DRM_INFO("applying backlight present quirk\n");
14491}
14492
b690e96c
JB
14493struct intel_quirk {
14494 int device;
14495 int subsystem_vendor;
14496 int subsystem_device;
14497 void (*hook)(struct drm_device *dev);
14498};
14499
5f85f176
EE
14500/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14501struct intel_dmi_quirk {
14502 void (*hook)(struct drm_device *dev);
14503 const struct dmi_system_id (*dmi_id_list)[];
14504};
14505
14506static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14507{
14508 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14509 return 1;
14510}
14511
14512static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14513 {
14514 .dmi_id_list = &(const struct dmi_system_id[]) {
14515 {
14516 .callback = intel_dmi_reverse_brightness,
14517 .ident = "NCR Corporation",
14518 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14519 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14520 },
14521 },
14522 { } /* terminating entry */
14523 },
14524 .hook = quirk_invert_brightness,
14525 },
14526};
14527
c43b5634 14528static struct intel_quirk intel_quirks[] = {
b690e96c 14529 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 14530 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 14531
b690e96c
JB
14532 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14533 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14534
b690e96c
JB
14535 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14536 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14537
5f080c0f
VS
14538 /* 830 needs to leave pipe A & dpll A up */
14539 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14540
b6b5d049
VS
14541 /* 830 needs to leave pipe B & dpll B up */
14542 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14543
435793df
KP
14544 /* Lenovo U160 cannot use SSC on LVDS */
14545 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14546
14547 /* Sony Vaio Y cannot use SSC on LVDS */
14548 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14549
be505f64
AH
14550 /* Acer Aspire 5734Z must invert backlight brightness */
14551 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14552
14553 /* Acer/eMachines G725 */
14554 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14555
14556 /* Acer/eMachines e725 */
14557 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14558
14559 /* Acer/Packard Bell NCL20 */
14560 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14561
14562 /* Acer Aspire 4736Z */
14563 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14564
14565 /* Acer Aspire 5336 */
14566 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14567
14568 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14569 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14570
dfb3d47b
SD
14571 /* Acer C720 Chromebook (Core i3 4005U) */
14572 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14573
b2a9601c 14574 /* Apple Macbook 2,1 (Core 2 T7400) */
14575 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14576
d4967d8c
SD
14577 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14578 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14579
14580 /* HP Chromebook 14 (Celeron 2955U) */
14581 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14582
14583 /* Dell Chromebook 11 */
14584 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14585};
14586
14587static void intel_init_quirks(struct drm_device *dev)
14588{
14589 struct pci_dev *d = dev->pdev;
14590 int i;
14591
14592 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14593 struct intel_quirk *q = &intel_quirks[i];
14594
14595 if (d->device == q->device &&
14596 (d->subsystem_vendor == q->subsystem_vendor ||
14597 q->subsystem_vendor == PCI_ANY_ID) &&
14598 (d->subsystem_device == q->subsystem_device ||
14599 q->subsystem_device == PCI_ANY_ID))
14600 q->hook(dev);
14601 }
5f85f176
EE
14602 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14603 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14604 intel_dmi_quirks[i].hook(dev);
14605 }
b690e96c
JB
14606}
14607
9cce37f4
JB
14608/* Disable the VGA plane that we never use */
14609static void i915_disable_vga(struct drm_device *dev)
14610{
14611 struct drm_i915_private *dev_priv = dev->dev_private;
14612 u8 sr1;
766aa1c4 14613 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14614
2b37c616 14615 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14616 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14617 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14618 sr1 = inb(VGA_SR_DATA);
14619 outb(sr1 | 1<<5, VGA_SR_DATA);
14620 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14621 udelay(300);
14622
01f5a626 14623 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14624 POSTING_READ(vga_reg);
14625}
14626
f817586c
DV
14627void intel_modeset_init_hw(struct drm_device *dev)
14628{
a8f78b58
ED
14629 intel_prepare_ddi(dev);
14630
f8bf63fd
VS
14631 if (IS_VALLEYVIEW(dev))
14632 vlv_update_cdclk(dev);
14633
f817586c
DV
14634 intel_init_clock_gating(dev);
14635
8090c6b9 14636 intel_enable_gt_powersave(dev);
f817586c
DV
14637}
14638
79e53945
JB
14639void intel_modeset_init(struct drm_device *dev)
14640{
652c393a 14641 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14642 int sprite, ret;
8cc87b75 14643 enum pipe pipe;
46f297fb 14644 struct intel_crtc *crtc;
79e53945
JB
14645
14646 drm_mode_config_init(dev);
14647
14648 dev->mode_config.min_width = 0;
14649 dev->mode_config.min_height = 0;
14650
019d96cb
DA
14651 dev->mode_config.preferred_depth = 24;
14652 dev->mode_config.prefer_shadow = 1;
14653
25bab385
TU
14654 dev->mode_config.allow_fb_modifiers = true;
14655
e6ecefaa 14656 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14657
b690e96c
JB
14658 intel_init_quirks(dev);
14659
1fa61106
ED
14660 intel_init_pm(dev);
14661
e3c74757
BW
14662 if (INTEL_INFO(dev)->num_pipes == 0)
14663 return;
14664
e70236a8 14665 intel_init_display(dev);
7c10a2b5 14666 intel_init_audio(dev);
e70236a8 14667
a6c45cf0
CW
14668 if (IS_GEN2(dev)) {
14669 dev->mode_config.max_width = 2048;
14670 dev->mode_config.max_height = 2048;
14671 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14672 dev->mode_config.max_width = 4096;
14673 dev->mode_config.max_height = 4096;
79e53945 14674 } else {
a6c45cf0
CW
14675 dev->mode_config.max_width = 8192;
14676 dev->mode_config.max_height = 8192;
79e53945 14677 }
068be561 14678
dc41c154
VS
14679 if (IS_845G(dev) || IS_I865G(dev)) {
14680 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14681 dev->mode_config.cursor_height = 1023;
14682 } else if (IS_GEN2(dev)) {
068be561
DL
14683 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14684 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14685 } else {
14686 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14687 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14688 }
14689
5d4545ae 14690 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14691
28c97730 14692 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14693 INTEL_INFO(dev)->num_pipes,
14694 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14695
055e393f 14696 for_each_pipe(dev_priv, pipe) {
8cc87b75 14697 intel_crtc_init(dev, pipe);
3bdcfc0c 14698 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14699 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14700 if (ret)
06da8da2 14701 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14702 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14703 }
79e53945
JB
14704 }
14705
f42bb70d
JB
14706 intel_init_dpio(dev);
14707
e72f9fbf 14708 intel_shared_dpll_init(dev);
ee7b9f93 14709
9cce37f4
JB
14710 /* Just disable it once at startup */
14711 i915_disable_vga(dev);
79e53945 14712 intel_setup_outputs(dev);
11be49eb
CW
14713
14714 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14715 intel_fbc_disable(dev);
fa9fa083 14716
6e9f798d 14717 drm_modeset_lock_all(dev);
fa9fa083 14718 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14719 drm_modeset_unlock_all(dev);
46f297fb 14720
d3fcc808 14721 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14722 if (!crtc->active)
14723 continue;
14724
46f297fb 14725 /*
46f297fb
JB
14726 * Note that reserving the BIOS fb up front prevents us
14727 * from stuffing other stolen allocations like the ring
14728 * on top. This prevents some ugliness at boot time, and
14729 * can even allow for smooth boot transitions if the BIOS
14730 * fb is large enough for the active pipe configuration.
14731 */
5724dbd1
DL
14732 if (dev_priv->display.get_initial_plane_config) {
14733 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14734 &crtc->plane_config);
14735 /*
14736 * If the fb is shared between multiple heads, we'll
14737 * just get the first one.
14738 */
f6936e29 14739 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 14740 }
46f297fb 14741 }
2c7111db
CW
14742}
14743
7fad798e
DV
14744static void intel_enable_pipe_a(struct drm_device *dev)
14745{
14746 struct intel_connector *connector;
14747 struct drm_connector *crt = NULL;
14748 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14749 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14750
14751 /* We can't just switch on the pipe A, we need to set things up with a
14752 * proper mode and output configuration. As a gross hack, enable pipe A
14753 * by enabling the load detect pipe once. */
3a3371ff 14754 for_each_intel_connector(dev, connector) {
7fad798e
DV
14755 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14756 crt = &connector->base;
14757 break;
14758 }
14759 }
14760
14761 if (!crt)
14762 return;
14763
208bf9fd 14764 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14765 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14766}
14767
fa555837
DV
14768static bool
14769intel_check_plane_mapping(struct intel_crtc *crtc)
14770{
7eb552ae
BW
14771 struct drm_device *dev = crtc->base.dev;
14772 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
14773 u32 reg, val;
14774
7eb552ae 14775 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14776 return true;
14777
14778 reg = DSPCNTR(!crtc->plane);
14779 val = I915_READ(reg);
14780
14781 if ((val & DISPLAY_PLANE_ENABLE) &&
14782 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14783 return false;
14784
14785 return true;
14786}
14787
24929352
DV
14788static void intel_sanitize_crtc(struct intel_crtc *crtc)
14789{
14790 struct drm_device *dev = crtc->base.dev;
14791 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 14792 u32 reg;
24929352 14793
24929352 14794 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 14795 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
14796 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14797
d3eaf884 14798 /* restore vblank interrupts to correct state */
9625604c 14799 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
14800 if (crtc->active) {
14801 update_scanline_offset(crtc);
9625604c
DV
14802 drm_crtc_vblank_on(&crtc->base);
14803 }
d3eaf884 14804
24929352 14805 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14806 * disable the crtc (and hence change the state) if it is wrong. Note
14807 * that gen4+ has a fixed plane -> pipe mapping. */
14808 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14809 struct intel_connector *connector;
14810 bool plane;
14811
24929352
DV
14812 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14813 crtc->base.base.id);
14814
14815 /* Pipe has the wrong plane attached and the plane is active.
14816 * Temporarily change the plane mapping and disable everything
14817 * ... */
14818 plane = crtc->plane;
14819 crtc->plane = !plane;
9c8958bc 14820 crtc->primary_enabled = true;
24929352
DV
14821 dev_priv->display.crtc_disable(&crtc->base);
14822 crtc->plane = plane;
14823
14824 /* ... and break all links. */
3a3371ff 14825 for_each_intel_connector(dev, connector) {
24929352
DV
14826 if (connector->encoder->base.crtc != &crtc->base)
14827 continue;
14828
7f1950fb
EE
14829 connector->base.dpms = DRM_MODE_DPMS_OFF;
14830 connector->base.encoder = NULL;
24929352 14831 }
7f1950fb
EE
14832 /* multiple connectors may have the same encoder:
14833 * handle them and break crtc link separately */
3a3371ff 14834 for_each_intel_connector(dev, connector)
7f1950fb
EE
14835 if (connector->encoder->base.crtc == &crtc->base) {
14836 connector->encoder->base.crtc = NULL;
14837 connector->encoder->connectors_active = false;
14838 }
24929352
DV
14839
14840 WARN_ON(crtc->active);
83d65738 14841 crtc->base.state->enable = false;
24929352
DV
14842 crtc->base.enabled = false;
14843 }
24929352 14844
7fad798e
DV
14845 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14846 crtc->pipe == PIPE_A && !crtc->active) {
14847 /* BIOS forgot to enable pipe A, this mostly happens after
14848 * resume. Force-enable the pipe to fix this, the update_dpms
14849 * call below we restore the pipe to the right state, but leave
14850 * the required bits on. */
14851 intel_enable_pipe_a(dev);
14852 }
14853
24929352
DV
14854 /* Adjust the state of the output pipe according to whether we
14855 * have active connectors/encoders. */
14856 intel_crtc_update_dpms(&crtc->base);
14857
83d65738 14858 if (crtc->active != crtc->base.state->enable) {
24929352
DV
14859 struct intel_encoder *encoder;
14860
14861 /* This can happen either due to bugs in the get_hw_state
14862 * functions or because the pipe is force-enabled due to the
14863 * pipe A quirk. */
14864 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14865 crtc->base.base.id,
83d65738 14866 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
14867 crtc->active ? "enabled" : "disabled");
14868
83d65738 14869 crtc->base.state->enable = crtc->active;
24929352
DV
14870 crtc->base.enabled = crtc->active;
14871
14872 /* Because we only establish the connector -> encoder ->
14873 * crtc links if something is active, this means the
14874 * crtc is now deactivated. Break the links. connector
14875 * -> encoder links are only establish when things are
14876 * actually up, hence no need to break them. */
14877 WARN_ON(crtc->active);
14878
14879 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14880 WARN_ON(encoder->connectors_active);
14881 encoder->base.crtc = NULL;
14882 }
14883 }
c5ab3bc0 14884
a3ed6aad 14885 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
14886 /*
14887 * We start out with underrun reporting disabled to avoid races.
14888 * For correct bookkeeping mark this on active crtcs.
14889 *
c5ab3bc0
DV
14890 * Also on gmch platforms we dont have any hardware bits to
14891 * disable the underrun reporting. Which means we need to start
14892 * out with underrun reporting disabled also on inactive pipes,
14893 * since otherwise we'll complain about the garbage we read when
14894 * e.g. coming up after runtime pm.
14895 *
4cc31489
DV
14896 * No protection against concurrent access is required - at
14897 * worst a fifo underrun happens which also sets this to false.
14898 */
14899 crtc->cpu_fifo_underrun_disabled = true;
14900 crtc->pch_fifo_underrun_disabled = true;
14901 }
24929352
DV
14902}
14903
14904static void intel_sanitize_encoder(struct intel_encoder *encoder)
14905{
14906 struct intel_connector *connector;
14907 struct drm_device *dev = encoder->base.dev;
14908
14909 /* We need to check both for a crtc link (meaning that the
14910 * encoder is active and trying to read from a pipe) and the
14911 * pipe itself being active. */
14912 bool has_active_crtc = encoder->base.crtc &&
14913 to_intel_crtc(encoder->base.crtc)->active;
14914
14915 if (encoder->connectors_active && !has_active_crtc) {
14916 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14917 encoder->base.base.id,
8e329a03 14918 encoder->base.name);
24929352
DV
14919
14920 /* Connector is active, but has no active pipe. This is
14921 * fallout from our resume register restoring. Disable
14922 * the encoder manually again. */
14923 if (encoder->base.crtc) {
14924 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14925 encoder->base.base.id,
8e329a03 14926 encoder->base.name);
24929352 14927 encoder->disable(encoder);
a62d1497
VS
14928 if (encoder->post_disable)
14929 encoder->post_disable(encoder);
24929352 14930 }
7f1950fb
EE
14931 encoder->base.crtc = NULL;
14932 encoder->connectors_active = false;
24929352
DV
14933
14934 /* Inconsistent output/port/pipe state happens presumably due to
14935 * a bug in one of the get_hw_state functions. Or someplace else
14936 * in our code, like the register restore mess on resume. Clamp
14937 * things to off as a safer default. */
3a3371ff 14938 for_each_intel_connector(dev, connector) {
24929352
DV
14939 if (connector->encoder != encoder)
14940 continue;
7f1950fb
EE
14941 connector->base.dpms = DRM_MODE_DPMS_OFF;
14942 connector->base.encoder = NULL;
24929352
DV
14943 }
14944 }
14945 /* Enabled encoders without active connectors will be fixed in
14946 * the crtc fixup. */
14947}
14948
04098753 14949void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
14950{
14951 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 14952 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 14953
04098753
ID
14954 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14955 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14956 i915_disable_vga(dev);
14957 }
14958}
14959
14960void i915_redisable_vga(struct drm_device *dev)
14961{
14962 struct drm_i915_private *dev_priv = dev->dev_private;
14963
8dc8a27c
PZ
14964 /* This function can be called both from intel_modeset_setup_hw_state or
14965 * at a very early point in our resume sequence, where the power well
14966 * structures are not yet restored. Since this function is at a very
14967 * paranoid "someone might have enabled VGA while we were not looking"
14968 * level, just check if the power well is enabled instead of trying to
14969 * follow the "don't touch the power well if we don't need it" policy
14970 * the rest of the driver uses. */
f458ebbc 14971 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14972 return;
14973
04098753 14974 i915_redisable_vga_power_on(dev);
0fde901f
KM
14975}
14976
98ec7739
VS
14977static bool primary_get_hw_state(struct intel_crtc *crtc)
14978{
14979 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14980
14981 if (!crtc->active)
14982 return false;
14983
14984 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14985}
14986
30e984df 14987static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
14988{
14989 struct drm_i915_private *dev_priv = dev->dev_private;
14990 enum pipe pipe;
24929352
DV
14991 struct intel_crtc *crtc;
14992 struct intel_encoder *encoder;
14993 struct intel_connector *connector;
5358901f 14994 int i;
24929352 14995
d3fcc808 14996 for_each_intel_crtc(dev, crtc) {
6e3c9717 14997 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 14998
6e3c9717 14999 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15000
0e8ffe1b 15001 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15002 crtc->config);
24929352 15003
83d65738 15004 crtc->base.state->enable = crtc->active;
24929352 15005 crtc->base.enabled = crtc->active;
98ec7739 15006 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
15007
15008 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15009 crtc->base.base.id,
15010 crtc->active ? "enabled" : "disabled");
15011 }
15012
5358901f
DV
15013 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15014 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15015
3e369b76
ACO
15016 pll->on = pll->get_hw_state(dev_priv, pll,
15017 &pll->config.hw_state);
5358901f 15018 pll->active = 0;
3e369b76 15019 pll->config.crtc_mask = 0;
d3fcc808 15020 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15021 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15022 pll->active++;
3e369b76 15023 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15024 }
5358901f 15025 }
5358901f 15026
1e6f2ddc 15027 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15028 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15029
3e369b76 15030 if (pll->config.crtc_mask)
bd2bb1b9 15031 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15032 }
15033
b2784e15 15034 for_each_intel_encoder(dev, encoder) {
24929352
DV
15035 pipe = 0;
15036
15037 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15038 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15039 encoder->base.crtc = &crtc->base;
6e3c9717 15040 encoder->get_config(encoder, crtc->config);
24929352
DV
15041 } else {
15042 encoder->base.crtc = NULL;
15043 }
15044
15045 encoder->connectors_active = false;
6f2bcceb 15046 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15047 encoder->base.base.id,
8e329a03 15048 encoder->base.name,
24929352 15049 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15050 pipe_name(pipe));
24929352
DV
15051 }
15052
3a3371ff 15053 for_each_intel_connector(dev, connector) {
24929352
DV
15054 if (connector->get_hw_state(connector)) {
15055 connector->base.dpms = DRM_MODE_DPMS_ON;
15056 connector->encoder->connectors_active = true;
15057 connector->base.encoder = &connector->encoder->base;
15058 } else {
15059 connector->base.dpms = DRM_MODE_DPMS_OFF;
15060 connector->base.encoder = NULL;
15061 }
15062 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15063 connector->base.base.id,
c23cc417 15064 connector->base.name,
24929352
DV
15065 connector->base.encoder ? "enabled" : "disabled");
15066 }
30e984df
DV
15067}
15068
15069/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15070 * and i915 state tracking structures. */
15071void intel_modeset_setup_hw_state(struct drm_device *dev,
15072 bool force_restore)
15073{
15074 struct drm_i915_private *dev_priv = dev->dev_private;
15075 enum pipe pipe;
30e984df
DV
15076 struct intel_crtc *crtc;
15077 struct intel_encoder *encoder;
35c95375 15078 int i;
30e984df
DV
15079
15080 intel_modeset_readout_hw_state(dev);
24929352 15081
babea61d
JB
15082 /*
15083 * Now that we have the config, copy it to each CRTC struct
15084 * Note that this could go away if we move to using crtc_config
15085 * checking everywhere.
15086 */
d3fcc808 15087 for_each_intel_crtc(dev, crtc) {
d330a953 15088 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15089 intel_mode_from_pipe_config(&crtc->base.mode,
15090 crtc->config);
babea61d
JB
15091 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15092 crtc->base.base.id);
15093 drm_mode_debug_printmodeline(&crtc->base.mode);
15094 }
15095 }
15096
24929352 15097 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15098 for_each_intel_encoder(dev, encoder) {
24929352
DV
15099 intel_sanitize_encoder(encoder);
15100 }
15101
055e393f 15102 for_each_pipe(dev_priv, pipe) {
24929352
DV
15103 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15104 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15105 intel_dump_pipe_config(crtc, crtc->config,
15106 "[setup_hw_state]");
24929352 15107 }
9a935856 15108
d29b2f9d
ACO
15109 intel_modeset_update_connector_atomic_state(dev);
15110
35c95375
DV
15111 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15112 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15113
15114 if (!pll->on || pll->active)
15115 continue;
15116
15117 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15118
15119 pll->disable(dev_priv, pll);
15120 pll->on = false;
15121 }
15122
3078999f
PB
15123 if (IS_GEN9(dev))
15124 skl_wm_get_hw_state(dev);
15125 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15126 ilk_wm_get_hw_state(dev);
15127
45e2b5f6 15128 if (force_restore) {
7d0bc1ea
VS
15129 i915_redisable_vga(dev);
15130
f30da187
DV
15131 /*
15132 * We need to use raw interfaces for restoring state to avoid
15133 * checking (bogus) intermediate states.
15134 */
055e393f 15135 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15136 struct drm_crtc *crtc =
15137 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15138
83a57153 15139 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15140 }
15141 } else {
15142 intel_modeset_update_staged_output_state(dev);
15143 }
8af6cf88
DV
15144
15145 intel_modeset_check_state(dev);
2c7111db
CW
15146}
15147
15148void intel_modeset_gem_init(struct drm_device *dev)
15149{
92122789 15150 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15151 struct drm_crtc *c;
2ff8fde1 15152 struct drm_i915_gem_object *obj;
e0d6149b 15153 int ret;
484b41dd 15154
ae48434c
ID
15155 mutex_lock(&dev->struct_mutex);
15156 intel_init_gt_powersave(dev);
15157 mutex_unlock(&dev->struct_mutex);
15158
92122789
JB
15159 /*
15160 * There may be no VBT; and if the BIOS enabled SSC we can
15161 * just keep using it to avoid unnecessary flicker. Whereas if the
15162 * BIOS isn't using it, don't assume it will work even if the VBT
15163 * indicates as much.
15164 */
15165 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15166 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15167 DREF_SSC1_ENABLE);
15168
1833b134 15169 intel_modeset_init_hw(dev);
02e792fb
DV
15170
15171 intel_setup_overlay(dev);
484b41dd
JB
15172
15173 /*
15174 * Make sure any fbs we allocated at startup are properly
15175 * pinned & fenced. When we do the allocation it's too early
15176 * for this.
15177 */
70e1e0ec 15178 for_each_crtc(dev, c) {
2ff8fde1
MR
15179 obj = intel_fb_obj(c->primary->fb);
15180 if (obj == NULL)
484b41dd
JB
15181 continue;
15182
e0d6149b
TU
15183 mutex_lock(&dev->struct_mutex);
15184 ret = intel_pin_and_fence_fb_obj(c->primary,
15185 c->primary->fb,
15186 c->primary->state,
15187 NULL);
15188 mutex_unlock(&dev->struct_mutex);
15189 if (ret) {
484b41dd
JB
15190 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15191 to_intel_crtc(c)->pipe);
66e514c1
DA
15192 drm_framebuffer_unreference(c->primary->fb);
15193 c->primary->fb = NULL;
afd65eb4 15194 update_state_fb(c->primary);
484b41dd
JB
15195 }
15196 }
0962c3c9
VS
15197
15198 intel_backlight_register(dev);
79e53945
JB
15199}
15200
4932e2c3
ID
15201void intel_connector_unregister(struct intel_connector *intel_connector)
15202{
15203 struct drm_connector *connector = &intel_connector->base;
15204
15205 intel_panel_destroy_backlight(connector);
34ea3d38 15206 drm_connector_unregister(connector);
4932e2c3
ID
15207}
15208
79e53945
JB
15209void intel_modeset_cleanup(struct drm_device *dev)
15210{
652c393a 15211 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15212 struct drm_connector *connector;
652c393a 15213
2eb5252e
ID
15214 intel_disable_gt_powersave(dev);
15215
0962c3c9
VS
15216 intel_backlight_unregister(dev);
15217
fd0c0642
DV
15218 /*
15219 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15220 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15221 * experience fancy races otherwise.
15222 */
2aeb7d3a 15223 intel_irq_uninstall(dev_priv);
eb21b92b 15224
fd0c0642
DV
15225 /*
15226 * Due to the hpd irq storm handling the hotplug work can re-arm the
15227 * poll handlers. Hence disable polling after hpd handling is shut down.
15228 */
f87ea761 15229 drm_kms_helper_poll_fini(dev);
fd0c0642 15230
652c393a
JB
15231 mutex_lock(&dev->struct_mutex);
15232
723bfd70
JB
15233 intel_unregister_dsm_handler();
15234
7ff0ebcc 15235 intel_fbc_disable(dev);
e70236a8 15236
69341a5e
KH
15237 mutex_unlock(&dev->struct_mutex);
15238
1630fe75
CW
15239 /* flush any delayed tasks or pending work */
15240 flush_scheduled_work();
15241
db31af1d
JN
15242 /* destroy the backlight and sysfs files before encoders/connectors */
15243 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15244 struct intel_connector *intel_connector;
15245
15246 intel_connector = to_intel_connector(connector);
15247 intel_connector->unregister(intel_connector);
db31af1d 15248 }
d9255d57 15249
79e53945 15250 drm_mode_config_cleanup(dev);
4d7bb011
DV
15251
15252 intel_cleanup_overlay(dev);
ae48434c
ID
15253
15254 mutex_lock(&dev->struct_mutex);
15255 intel_cleanup_gt_powersave(dev);
15256 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15257}
15258
f1c79df3
ZW
15259/*
15260 * Return which encoder is currently attached for connector.
15261 */
df0e9248 15262struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15263{
df0e9248
CW
15264 return &intel_attached_encoder(connector)->base;
15265}
f1c79df3 15266
df0e9248
CW
15267void intel_connector_attach_encoder(struct intel_connector *connector,
15268 struct intel_encoder *encoder)
15269{
15270 connector->encoder = encoder;
15271 drm_mode_connector_attach_encoder(&connector->base,
15272 &encoder->base);
79e53945 15273}
28d52043
DA
15274
15275/*
15276 * set vga decode state - true == enable VGA decode
15277 */
15278int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15279{
15280 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15281 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15282 u16 gmch_ctrl;
15283
75fa041d
CW
15284 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15285 DRM_ERROR("failed to read control word\n");
15286 return -EIO;
15287 }
15288
c0cc8a55
CW
15289 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15290 return 0;
15291
28d52043
DA
15292 if (state)
15293 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15294 else
15295 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15296
15297 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15298 DRM_ERROR("failed to write control word\n");
15299 return -EIO;
15300 }
15301
28d52043
DA
15302 return 0;
15303}
c4a1d9e4 15304
c4a1d9e4 15305struct intel_display_error_state {
ff57f1b0
PZ
15306
15307 u32 power_well_driver;
15308
63b66e5b
CW
15309 int num_transcoders;
15310
c4a1d9e4
CW
15311 struct intel_cursor_error_state {
15312 u32 control;
15313 u32 position;
15314 u32 base;
15315 u32 size;
52331309 15316 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15317
15318 struct intel_pipe_error_state {
ddf9c536 15319 bool power_domain_on;
c4a1d9e4 15320 u32 source;
f301b1e1 15321 u32 stat;
52331309 15322 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15323
15324 struct intel_plane_error_state {
15325 u32 control;
15326 u32 stride;
15327 u32 size;
15328 u32 pos;
15329 u32 addr;
15330 u32 surface;
15331 u32 tile_offset;
52331309 15332 } plane[I915_MAX_PIPES];
63b66e5b
CW
15333
15334 struct intel_transcoder_error_state {
ddf9c536 15335 bool power_domain_on;
63b66e5b
CW
15336 enum transcoder cpu_transcoder;
15337
15338 u32 conf;
15339
15340 u32 htotal;
15341 u32 hblank;
15342 u32 hsync;
15343 u32 vtotal;
15344 u32 vblank;
15345 u32 vsync;
15346 } transcoder[4];
c4a1d9e4
CW
15347};
15348
15349struct intel_display_error_state *
15350intel_display_capture_error_state(struct drm_device *dev)
15351{
fbee40df 15352 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15353 struct intel_display_error_state *error;
63b66e5b
CW
15354 int transcoders[] = {
15355 TRANSCODER_A,
15356 TRANSCODER_B,
15357 TRANSCODER_C,
15358 TRANSCODER_EDP,
15359 };
c4a1d9e4
CW
15360 int i;
15361
63b66e5b
CW
15362 if (INTEL_INFO(dev)->num_pipes == 0)
15363 return NULL;
15364
9d1cb914 15365 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15366 if (error == NULL)
15367 return NULL;
15368
190be112 15369 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15370 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15371
055e393f 15372 for_each_pipe(dev_priv, i) {
ddf9c536 15373 error->pipe[i].power_domain_on =
f458ebbc
DV
15374 __intel_display_power_is_enabled(dev_priv,
15375 POWER_DOMAIN_PIPE(i));
ddf9c536 15376 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15377 continue;
15378
5efb3e28
VS
15379 error->cursor[i].control = I915_READ(CURCNTR(i));
15380 error->cursor[i].position = I915_READ(CURPOS(i));
15381 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15382
15383 error->plane[i].control = I915_READ(DSPCNTR(i));
15384 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15385 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15386 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15387 error->plane[i].pos = I915_READ(DSPPOS(i));
15388 }
ca291363
PZ
15389 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15390 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15391 if (INTEL_INFO(dev)->gen >= 4) {
15392 error->plane[i].surface = I915_READ(DSPSURF(i));
15393 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15394 }
15395
c4a1d9e4 15396 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15397
3abfce77 15398 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15399 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15400 }
15401
15402 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15403 if (HAS_DDI(dev_priv->dev))
15404 error->num_transcoders++; /* Account for eDP. */
15405
15406 for (i = 0; i < error->num_transcoders; i++) {
15407 enum transcoder cpu_transcoder = transcoders[i];
15408
ddf9c536 15409 error->transcoder[i].power_domain_on =
f458ebbc 15410 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15411 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15412 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15413 continue;
15414
63b66e5b
CW
15415 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15416
15417 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15418 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15419 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15420 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15421 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15422 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15423 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15424 }
15425
15426 return error;
15427}
15428
edc3d884
MK
15429#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15430
c4a1d9e4 15431void
edc3d884 15432intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15433 struct drm_device *dev,
15434 struct intel_display_error_state *error)
15435{
055e393f 15436 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15437 int i;
15438
63b66e5b
CW
15439 if (!error)
15440 return;
15441
edc3d884 15442 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15443 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15444 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15445 error->power_well_driver);
055e393f 15446 for_each_pipe(dev_priv, i) {
edc3d884 15447 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15448 err_printf(m, " Power: %s\n",
15449 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15450 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15451 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15452
15453 err_printf(m, "Plane [%d]:\n", i);
15454 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15455 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15456 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15457 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15458 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15459 }
4b71a570 15460 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15461 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15462 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15463 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15464 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15465 }
15466
edc3d884
MK
15467 err_printf(m, "Cursor [%d]:\n", i);
15468 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15469 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15470 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15471 }
63b66e5b
CW
15472
15473 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15474 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15475 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15476 err_printf(m, " Power: %s\n",
15477 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15478 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15479 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15480 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15481 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15482 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15483 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15484 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15485 }
c4a1d9e4 15486}
e2fcdaa9
VS
15487
15488void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15489{
15490 struct intel_crtc *crtc;
15491
15492 for_each_intel_crtc(dev, crtc) {
15493 struct intel_unpin_work *work;
e2fcdaa9 15494
5e2d7afc 15495 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15496
15497 work = crtc->unpin_work;
15498
15499 if (work && work->event &&
15500 work->event->base.file_priv == file) {
15501 kfree(work->event);
15502 work->event = NULL;
15503 }
15504
5e2d7afc 15505 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15506 }
15507}