]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: fill in more mode members
[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 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5 53 DRM_FORMAT_XRGB8888,
465c120c
MR
54};
55
56/* Primary plane formats for gen >= 4 */
568db4f2 57static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
61 DRM_FORMAT_XBGR8888,
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
465c120c 70 DRM_FORMAT_XBGR8888,
67fe7dc5 71 DRM_FORMAT_ARGB8888,
465c120c
MR
72 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
465c120c 74 DRM_FORMAT_XBGR2101010,
465c120c
MR
75};
76
3d7d6510
MR
77/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
6b383a7f 82static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 83
f1f644dc 84static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 85 struct intel_crtc_state *pipe_config);
18442d08 86static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 87 struct intel_crtc_state *pipe_config);
f1f644dc 88
568c634a 89static int intel_set_mode(struct drm_atomic_state *state);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 102static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
d288f65f 104static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 105 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
043e9bda 112static void intel_modeset_setup_hw_state(struct drm_device *dev);
e7457a9a 113
0e32b39c
DA
114static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
115{
116 if (!connector->mst_port)
117 return connector->encoder;
118 else
119 return &connector->mst_port->mst_encoders[pipe]->base;
120}
121
79e53945 122typedef struct {
0206e353 123 int min, max;
79e53945
JB
124} intel_range_t;
125
126typedef struct {
0206e353
AJ
127 int dot_limit;
128 int p2_slow, p2_fast;
79e53945
JB
129} intel_p2_t;
130
d4906093
ML
131typedef struct intel_limit intel_limit_t;
132struct intel_limit {
0206e353
AJ
133 intel_range_t dot, vco, n, m, m1, m2, p, p1;
134 intel_p2_t p2;
d4906093 135};
79e53945 136
d2acd215
DV
137int
138intel_pch_rawclk(struct drm_device *dev)
139{
140 struct drm_i915_private *dev_priv = dev->dev_private;
141
142 WARN_ON(!HAS_PCH_SPLIT(dev));
143
144 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
145}
146
021357ac
CW
147static inline u32 /* units of 100MHz */
148intel_fdi_link_freq(struct drm_device *dev)
149{
8b99e68c
CW
150 if (IS_GEN5(dev)) {
151 struct drm_i915_private *dev_priv = dev->dev_private;
152 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
153 } else
154 return 27;
021357ac
CW
155}
156
5d536e28 157static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 158 .dot = { .min = 25000, .max = 350000 },
9c333719 159 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 160 .n = { .min = 2, .max = 16 },
0206e353
AJ
161 .m = { .min = 96, .max = 140 },
162 .m1 = { .min = 18, .max = 26 },
163 .m2 = { .min = 6, .max = 16 },
164 .p = { .min = 4, .max = 128 },
165 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
166 .p2 = { .dot_limit = 165000,
167 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
168};
169
5d536e28
DV
170static const intel_limit_t intel_limits_i8xx_dvo = {
171 .dot = { .min = 25000, .max = 350000 },
9c333719 172 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 173 .n = { .min = 2, .max = 16 },
5d536e28
DV
174 .m = { .min = 96, .max = 140 },
175 .m1 = { .min = 18, .max = 26 },
176 .m2 = { .min = 6, .max = 16 },
177 .p = { .min = 4, .max = 128 },
178 .p1 = { .min = 2, .max = 33 },
179 .p2 = { .dot_limit = 165000,
180 .p2_slow = 4, .p2_fast = 4 },
181};
182
e4b36699 183static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 184 .dot = { .min = 25000, .max = 350000 },
9c333719 185 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 186 .n = { .min = 2, .max = 16 },
0206e353
AJ
187 .m = { .min = 96, .max = 140 },
188 .m1 = { .min = 18, .max = 26 },
189 .m2 = { .min = 6, .max = 16 },
190 .p = { .min = 4, .max = 128 },
191 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
192 .p2 = { .dot_limit = 165000,
193 .p2_slow = 14, .p2_fast = 7 },
e4b36699 194};
273e27ca 195
e4b36699 196static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
197 .dot = { .min = 20000, .max = 400000 },
198 .vco = { .min = 1400000, .max = 2800000 },
199 .n = { .min = 1, .max = 6 },
200 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
201 .m1 = { .min = 8, .max = 18 },
202 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
203 .p = { .min = 5, .max = 80 },
204 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
205 .p2 = { .dot_limit = 200000,
206 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
207};
208
209static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
210 .dot = { .min = 20000, .max = 400000 },
211 .vco = { .min = 1400000, .max = 2800000 },
212 .n = { .min = 1, .max = 6 },
213 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
214 .m1 = { .min = 8, .max = 18 },
215 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
216 .p = { .min = 7, .max = 98 },
217 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
218 .p2 = { .dot_limit = 112000,
219 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
220};
221
273e27ca 222
e4b36699 223static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
224 .dot = { .min = 25000, .max = 270000 },
225 .vco = { .min = 1750000, .max = 3500000},
226 .n = { .min = 1, .max = 4 },
227 .m = { .min = 104, .max = 138 },
228 .m1 = { .min = 17, .max = 23 },
229 .m2 = { .min = 5, .max = 11 },
230 .p = { .min = 10, .max = 30 },
231 .p1 = { .min = 1, .max = 3},
232 .p2 = { .dot_limit = 270000,
233 .p2_slow = 10,
234 .p2_fast = 10
044c7c41 235 },
e4b36699
KP
236};
237
238static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
239 .dot = { .min = 22000, .max = 400000 },
240 .vco = { .min = 1750000, .max = 3500000},
241 .n = { .min = 1, .max = 4 },
242 .m = { .min = 104, .max = 138 },
243 .m1 = { .min = 16, .max = 23 },
244 .m2 = { .min = 5, .max = 11 },
245 .p = { .min = 5, .max = 80 },
246 .p1 = { .min = 1, .max = 8},
247 .p2 = { .dot_limit = 165000,
248 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
249};
250
251static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
252 .dot = { .min = 20000, .max = 115000 },
253 .vco = { .min = 1750000, .max = 3500000 },
254 .n = { .min = 1, .max = 3 },
255 .m = { .min = 104, .max = 138 },
256 .m1 = { .min = 17, .max = 23 },
257 .m2 = { .min = 5, .max = 11 },
258 .p = { .min = 28, .max = 112 },
259 .p1 = { .min = 2, .max = 8 },
260 .p2 = { .dot_limit = 0,
261 .p2_slow = 14, .p2_fast = 14
044c7c41 262 },
e4b36699
KP
263};
264
265static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
266 .dot = { .min = 80000, .max = 224000 },
267 .vco = { .min = 1750000, .max = 3500000 },
268 .n = { .min = 1, .max = 3 },
269 .m = { .min = 104, .max = 138 },
270 .m1 = { .min = 17, .max = 23 },
271 .m2 = { .min = 5, .max = 11 },
272 .p = { .min = 14, .max = 42 },
273 .p1 = { .min = 2, .max = 6 },
274 .p2 = { .dot_limit = 0,
275 .p2_slow = 7, .p2_fast = 7
044c7c41 276 },
e4b36699
KP
277};
278
f2b115e6 279static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
280 .dot = { .min = 20000, .max = 400000},
281 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 282 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
283 .n = { .min = 3, .max = 6 },
284 .m = { .min = 2, .max = 256 },
273e27ca 285 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
286 .m1 = { .min = 0, .max = 0 },
287 .m2 = { .min = 0, .max = 254 },
288 .p = { .min = 5, .max = 80 },
289 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
290 .p2 = { .dot_limit = 200000,
291 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
292};
293
f2b115e6 294static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
295 .dot = { .min = 20000, .max = 400000 },
296 .vco = { .min = 1700000, .max = 3500000 },
297 .n = { .min = 3, .max = 6 },
298 .m = { .min = 2, .max = 256 },
299 .m1 = { .min = 0, .max = 0 },
300 .m2 = { .min = 0, .max = 254 },
301 .p = { .min = 7, .max = 112 },
302 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
303 .p2 = { .dot_limit = 112000,
304 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
305};
306
273e27ca
EA
307/* Ironlake / Sandybridge
308 *
309 * We calculate clock using (register_value + 2) for N/M1/M2, so here
310 * the range value for them is (actual_value - 2).
311 */
b91ad0ec 312static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
313 .dot = { .min = 25000, .max = 350000 },
314 .vco = { .min = 1760000, .max = 3510000 },
315 .n = { .min = 1, .max = 5 },
316 .m = { .min = 79, .max = 127 },
317 .m1 = { .min = 12, .max = 22 },
318 .m2 = { .min = 5, .max = 9 },
319 .p = { .min = 5, .max = 80 },
320 .p1 = { .min = 1, .max = 8 },
321 .p2 = { .dot_limit = 225000,
322 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
323};
324
b91ad0ec 325static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
326 .dot = { .min = 25000, .max = 350000 },
327 .vco = { .min = 1760000, .max = 3510000 },
328 .n = { .min = 1, .max = 3 },
329 .m = { .min = 79, .max = 118 },
330 .m1 = { .min = 12, .max = 22 },
331 .m2 = { .min = 5, .max = 9 },
332 .p = { .min = 28, .max = 112 },
333 .p1 = { .min = 2, .max = 8 },
334 .p2 = { .dot_limit = 225000,
335 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
336};
337
338static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
339 .dot = { .min = 25000, .max = 350000 },
340 .vco = { .min = 1760000, .max = 3510000 },
341 .n = { .min = 1, .max = 3 },
342 .m = { .min = 79, .max = 127 },
343 .m1 = { .min = 12, .max = 22 },
344 .m2 = { .min = 5, .max = 9 },
345 .p = { .min = 14, .max = 56 },
346 .p1 = { .min = 2, .max = 8 },
347 .p2 = { .dot_limit = 225000,
348 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
349};
350
273e27ca 351/* LVDS 100mhz refclk limits. */
b91ad0ec 352static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
353 .dot = { .min = 25000, .max = 350000 },
354 .vco = { .min = 1760000, .max = 3510000 },
355 .n = { .min = 1, .max = 2 },
356 .m = { .min = 79, .max = 126 },
357 .m1 = { .min = 12, .max = 22 },
358 .m2 = { .min = 5, .max = 9 },
359 .p = { .min = 28, .max = 112 },
0206e353 360 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
361 .p2 = { .dot_limit = 225000,
362 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
363};
364
365static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
366 .dot = { .min = 25000, .max = 350000 },
367 .vco = { .min = 1760000, .max = 3510000 },
368 .n = { .min = 1, .max = 3 },
369 .m = { .min = 79, .max = 126 },
370 .m1 = { .min = 12, .max = 22 },
371 .m2 = { .min = 5, .max = 9 },
372 .p = { .min = 14, .max = 42 },
0206e353 373 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
374 .p2 = { .dot_limit = 225000,
375 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
376};
377
dc730512 378static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
379 /*
380 * These are the data rate limits (measured in fast clocks)
381 * since those are the strictest limits we have. The fast
382 * clock and actual rate limits are more relaxed, so checking
383 * them would make no difference.
384 */
385 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 386 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 387 .n = { .min = 1, .max = 7 },
a0c4da24
JB
388 .m1 = { .min = 2, .max = 3 },
389 .m2 = { .min = 11, .max = 156 },
b99ab663 390 .p1 = { .min = 2, .max = 3 },
5fdc9c49 391 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
392};
393
ef9348c8
CML
394static const intel_limit_t intel_limits_chv = {
395 /*
396 * These are the data rate limits (measured in fast clocks)
397 * since those are the strictest limits we have. The fast
398 * clock and actual rate limits are more relaxed, so checking
399 * them would make no difference.
400 */
401 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 402 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
403 .n = { .min = 1, .max = 1 },
404 .m1 = { .min = 2, .max = 2 },
405 .m2 = { .min = 24 << 22, .max = 175 << 22 },
406 .p1 = { .min = 2, .max = 4 },
407 .p2 = { .p2_slow = 1, .p2_fast = 14 },
408};
409
5ab7b0b7
ID
410static const intel_limit_t intel_limits_bxt = {
411 /* FIXME: find real dot limits */
412 .dot = { .min = 0, .max = INT_MAX },
e6292556 413 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
414 .n = { .min = 1, .max = 1 },
415 .m1 = { .min = 2, .max = 2 },
416 /* FIXME: find real m2 limits */
417 .m2 = { .min = 2 << 22, .max = 255 << 22 },
418 .p1 = { .min = 2, .max = 4 },
419 .p2 = { .p2_slow = 1, .p2_fast = 20 },
420};
421
cdba954e
ACO
422static bool
423needs_modeset(struct drm_crtc_state *state)
424{
425 return state->mode_changed || state->active_changed;
426}
427
e0638cdf
PZ
428/**
429 * Returns whether any output on the specified pipe is of the specified type
430 */
4093561b 431bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 432{
409ee761 433 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
434 struct intel_encoder *encoder;
435
409ee761 436 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
437 if (encoder->type == type)
438 return true;
439
440 return false;
441}
442
d0737e1d
ACO
443/**
444 * Returns whether any output on the specified pipe will have the specified
445 * type after a staged modeset is complete, i.e., the same as
446 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
447 * encoder->crtc.
448 */
a93e255f
ACO
449static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
450 int type)
d0737e1d 451{
a93e255f 452 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 453 struct drm_connector *connector;
a93e255f 454 struct drm_connector_state *connector_state;
d0737e1d 455 struct intel_encoder *encoder;
a93e255f
ACO
456 int i, num_connectors = 0;
457
da3ced29 458 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
459 if (connector_state->crtc != crtc_state->base.crtc)
460 continue;
461
462 num_connectors++;
d0737e1d 463
a93e255f
ACO
464 encoder = to_intel_encoder(connector_state->best_encoder);
465 if (encoder->type == type)
d0737e1d 466 return true;
a93e255f
ACO
467 }
468
469 WARN_ON(num_connectors == 0);
d0737e1d
ACO
470
471 return false;
472}
473
a93e255f
ACO
474static const intel_limit_t *
475intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 476{
a93e255f 477 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 478 const intel_limit_t *limit;
b91ad0ec 479
a93e255f 480 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 481 if (intel_is_dual_link_lvds(dev)) {
1b894b59 482 if (refclk == 100000)
b91ad0ec
ZW
483 limit = &intel_limits_ironlake_dual_lvds_100m;
484 else
485 limit = &intel_limits_ironlake_dual_lvds;
486 } else {
1b894b59 487 if (refclk == 100000)
b91ad0ec
ZW
488 limit = &intel_limits_ironlake_single_lvds_100m;
489 else
490 limit = &intel_limits_ironlake_single_lvds;
491 }
c6bb3538 492 } else
b91ad0ec 493 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
494
495 return limit;
496}
497
a93e255f
ACO
498static const intel_limit_t *
499intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 500{
a93e255f 501 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
502 const intel_limit_t *limit;
503
a93e255f 504 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 505 if (intel_is_dual_link_lvds(dev))
e4b36699 506 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 507 else
e4b36699 508 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
509 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
510 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 511 limit = &intel_limits_g4x_hdmi;
a93e255f 512 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 513 limit = &intel_limits_g4x_sdvo;
044c7c41 514 } else /* The option is for other outputs */
e4b36699 515 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
516
517 return limit;
518}
519
a93e255f
ACO
520static const intel_limit_t *
521intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 522{
a93e255f 523 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
524 const intel_limit_t *limit;
525
5ab7b0b7
ID
526 if (IS_BROXTON(dev))
527 limit = &intel_limits_bxt;
528 else if (HAS_PCH_SPLIT(dev))
a93e255f 529 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 530 else if (IS_G4X(dev)) {
a93e255f 531 limit = intel_g4x_limit(crtc_state);
f2b115e6 532 } else if (IS_PINEVIEW(dev)) {
a93e255f 533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 534 limit = &intel_limits_pineview_lvds;
2177832f 535 else
f2b115e6 536 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
537 } else if (IS_CHERRYVIEW(dev)) {
538 limit = &intel_limits_chv;
a0c4da24 539 } else if (IS_VALLEYVIEW(dev)) {
dc730512 540 limit = &intel_limits_vlv;
a6c45cf0 541 } else if (!IS_GEN2(dev)) {
a93e255f 542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
543 limit = &intel_limits_i9xx_lvds;
544 else
545 limit = &intel_limits_i9xx_sdvo;
79e53945 546 } else {
a93e255f 547 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 548 limit = &intel_limits_i8xx_lvds;
a93e255f 549 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 550 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
551 else
552 limit = &intel_limits_i8xx_dac;
79e53945
JB
553 }
554 return limit;
555}
556
dccbea3b
ID
557/*
558 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
559 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
560 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
561 * The helpers' return value is the rate of the clock that is fed to the
562 * display engine's pipe which can be the above fast dot clock rate or a
563 * divided-down version of it.
564 */
f2b115e6 565/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 566static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 567{
2177832f
SL
568 clock->m = clock->m2 + 2;
569 clock->p = clock->p1 * clock->p2;
ed5ca77e 570 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 571 return 0;
fb03ac01
VS
572 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
573 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
574
575 return clock->dot;
2177832f
SL
576}
577
7429e9d4
DV
578static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
579{
580 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
581}
582
dccbea3b 583static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 584{
7429e9d4 585 clock->m = i9xx_dpll_compute_m(clock);
79e53945 586 clock->p = clock->p1 * clock->p2;
ed5ca77e 587 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 588 return 0;
fb03ac01
VS
589 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
590 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
591
592 return clock->dot;
79e53945
JB
593}
594
dccbea3b 595static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
596{
597 clock->m = clock->m1 * clock->m2;
598 clock->p = clock->p1 * clock->p2;
599 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 600 return 0;
589eca67
ID
601 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
603
604 return clock->dot / 5;
589eca67
ID
605}
606
dccbea3b 607int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
608{
609 clock->m = clock->m1 * clock->m2;
610 clock->p = clock->p1 * clock->p2;
611 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 612 return 0;
ef9348c8
CML
613 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
614 clock->n << 22);
615 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
616
617 return clock->dot / 5;
ef9348c8
CML
618}
619
7c04d1d9 620#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
621/**
622 * Returns whether the given set of divisors are valid for a given refclk with
623 * the given connectors.
624 */
625
1b894b59
CW
626static bool intel_PLL_is_valid(struct drm_device *dev,
627 const intel_limit_t *limit,
628 const intel_clock_t *clock)
79e53945 629{
f01b7962
VS
630 if (clock->n < limit->n.min || limit->n.max < clock->n)
631 INTELPllInvalid("n out of range\n");
79e53945 632 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 633 INTELPllInvalid("p1 out of range\n");
79e53945 634 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 635 INTELPllInvalid("m2 out of range\n");
79e53945 636 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 637 INTELPllInvalid("m1 out of range\n");
f01b7962 638
5ab7b0b7 639 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
640 if (clock->m1 <= clock->m2)
641 INTELPllInvalid("m1 <= m2\n");
642
5ab7b0b7 643 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
644 if (clock->p < limit->p.min || limit->p.max < clock->p)
645 INTELPllInvalid("p out of range\n");
646 if (clock->m < limit->m.min || limit->m.max < clock->m)
647 INTELPllInvalid("m out of range\n");
648 }
649
79e53945 650 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 651 INTELPllInvalid("vco out of range\n");
79e53945
JB
652 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
653 * connector, etc., rather than just a single range.
654 */
655 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 656 INTELPllInvalid("dot out of range\n");
79e53945
JB
657
658 return true;
659}
660
3b1429d9
VS
661static int
662i9xx_select_p2_div(const intel_limit_t *limit,
663 const struct intel_crtc_state *crtc_state,
664 int target)
79e53945 665{
3b1429d9 666 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 667
a93e255f 668 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 669 /*
a210b028
DV
670 * For LVDS just rely on its current settings for dual-channel.
671 * We haven't figured out how to reliably set up different
672 * single/dual channel state, if we even can.
79e53945 673 */
1974cad0 674 if (intel_is_dual_link_lvds(dev))
3b1429d9 675 return limit->p2.p2_fast;
79e53945 676 else
3b1429d9 677 return limit->p2.p2_slow;
79e53945
JB
678 } else {
679 if (target < limit->p2.dot_limit)
3b1429d9 680 return limit->p2.p2_slow;
79e53945 681 else
3b1429d9 682 return limit->p2.p2_fast;
79e53945 683 }
3b1429d9
VS
684}
685
686static bool
687i9xx_find_best_dpll(const intel_limit_t *limit,
688 struct intel_crtc_state *crtc_state,
689 int target, int refclk, intel_clock_t *match_clock,
690 intel_clock_t *best_clock)
691{
692 struct drm_device *dev = crtc_state->base.crtc->dev;
693 intel_clock_t clock;
694 int err = target;
79e53945 695
0206e353 696 memset(best_clock, 0, sizeof(*best_clock));
79e53945 697
3b1429d9
VS
698 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
699
42158660
ZY
700 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
701 clock.m1++) {
702 for (clock.m2 = limit->m2.min;
703 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 704 if (clock.m2 >= clock.m1)
42158660
ZY
705 break;
706 for (clock.n = limit->n.min;
707 clock.n <= limit->n.max; clock.n++) {
708 for (clock.p1 = limit->p1.min;
709 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
710 int this_err;
711
dccbea3b 712 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
713 if (!intel_PLL_is_valid(dev, limit,
714 &clock))
715 continue;
716 if (match_clock &&
717 clock.p != match_clock->p)
718 continue;
719
720 this_err = abs(clock.dot - target);
721 if (this_err < err) {
722 *best_clock = clock;
723 err = this_err;
724 }
725 }
726 }
727 }
728 }
729
730 return (err != target);
731}
732
733static bool
a93e255f
ACO
734pnv_find_best_dpll(const intel_limit_t *limit,
735 struct intel_crtc_state *crtc_state,
ee9300bb
DV
736 int target, int refclk, intel_clock_t *match_clock,
737 intel_clock_t *best_clock)
79e53945 738{
3b1429d9 739 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 740 intel_clock_t clock;
79e53945
JB
741 int err = target;
742
0206e353 743 memset(best_clock, 0, sizeof(*best_clock));
79e53945 744
3b1429d9
VS
745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
42158660
ZY
747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
755 int this_err;
756
dccbea3b 757 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
758 if (!intel_PLL_is_valid(dev, limit,
759 &clock))
79e53945 760 continue;
cec2f356
SP
761 if (match_clock &&
762 clock.p != match_clock->p)
763 continue;
79e53945
JB
764
765 this_err = abs(clock.dot - target);
766 if (this_err < err) {
767 *best_clock = clock;
768 err = this_err;
769 }
770 }
771 }
772 }
773 }
774
775 return (err != target);
776}
777
d4906093 778static bool
a93e255f
ACO
779g4x_find_best_dpll(const intel_limit_t *limit,
780 struct intel_crtc_state *crtc_state,
ee9300bb
DV
781 int target, int refclk, intel_clock_t *match_clock,
782 intel_clock_t *best_clock)
d4906093 783{
3b1429d9 784 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
785 intel_clock_t clock;
786 int max_n;
3b1429d9 787 bool found = false;
6ba770dc
AJ
788 /* approximately equals target * 0.00585 */
789 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
790
791 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
792
793 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
794
d4906093 795 max_n = limit->n.max;
f77f13e2 796 /* based on hardware requirement, prefer smaller n to precision */
d4906093 797 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 798 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
799 for (clock.m1 = limit->m1.max;
800 clock.m1 >= limit->m1.min; clock.m1--) {
801 for (clock.m2 = limit->m2.max;
802 clock.m2 >= limit->m2.min; clock.m2--) {
803 for (clock.p1 = limit->p1.max;
804 clock.p1 >= limit->p1.min; clock.p1--) {
805 int this_err;
806
dccbea3b 807 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
808 if (!intel_PLL_is_valid(dev, limit,
809 &clock))
d4906093 810 continue;
1b894b59
CW
811
812 this_err = abs(clock.dot - target);
d4906093
ML
813 if (this_err < err_most) {
814 *best_clock = clock;
815 err_most = this_err;
816 max_n = clock.n;
817 found = true;
818 }
819 }
820 }
821 }
822 }
2c07245f
ZW
823 return found;
824}
825
d5dd62bd
ID
826/*
827 * Check if the calculated PLL configuration is more optimal compared to the
828 * best configuration and error found so far. Return the calculated error.
829 */
830static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
831 const intel_clock_t *calculated_clock,
832 const intel_clock_t *best_clock,
833 unsigned int best_error_ppm,
834 unsigned int *error_ppm)
835{
9ca3ba01
ID
836 /*
837 * For CHV ignore the error and consider only the P value.
838 * Prefer a bigger P value based on HW requirements.
839 */
840 if (IS_CHERRYVIEW(dev)) {
841 *error_ppm = 0;
842
843 return calculated_clock->p > best_clock->p;
844 }
845
24be4e46
ID
846 if (WARN_ON_ONCE(!target_freq))
847 return false;
848
d5dd62bd
ID
849 *error_ppm = div_u64(1000000ULL *
850 abs(target_freq - calculated_clock->dot),
851 target_freq);
852 /*
853 * Prefer a better P value over a better (smaller) error if the error
854 * is small. Ensure this preference for future configurations too by
855 * setting the error to 0.
856 */
857 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
858 *error_ppm = 0;
859
860 return true;
861 }
862
863 return *error_ppm + 10 < best_error_ppm;
864}
865
a0c4da24 866static bool
a93e255f
ACO
867vlv_find_best_dpll(const intel_limit_t *limit,
868 struct intel_crtc_state *crtc_state,
ee9300bb
DV
869 int target, int refclk, intel_clock_t *match_clock,
870 intel_clock_t *best_clock)
a0c4da24 871{
a93e255f 872 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 873 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 874 intel_clock_t clock;
69e4f900 875 unsigned int bestppm = 1000000;
27e639bf
VS
876 /* min update 19.2 MHz */
877 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 878 bool found = false;
a0c4da24 879
6b4bf1c4
VS
880 target *= 5; /* fast clock */
881
882 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
883
884 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 885 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 886 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 887 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 888 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 889 clock.p = clock.p1 * clock.p2;
a0c4da24 890 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 891 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 892 unsigned int ppm;
69e4f900 893
6b4bf1c4
VS
894 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
895 refclk * clock.m1);
896
dccbea3b 897 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 898
f01b7962
VS
899 if (!intel_PLL_is_valid(dev, limit,
900 &clock))
43b0ac53
VS
901 continue;
902
d5dd62bd
ID
903 if (!vlv_PLL_is_optimal(dev, target,
904 &clock,
905 best_clock,
906 bestppm, &ppm))
907 continue;
6b4bf1c4 908
d5dd62bd
ID
909 *best_clock = clock;
910 bestppm = ppm;
911 found = true;
a0c4da24
JB
912 }
913 }
914 }
915 }
a0c4da24 916
49e497ef 917 return found;
a0c4da24 918}
a4fc5ed6 919
ef9348c8 920static bool
a93e255f
ACO
921chv_find_best_dpll(const intel_limit_t *limit,
922 struct intel_crtc_state *crtc_state,
ef9348c8
CML
923 int target, int refclk, intel_clock_t *match_clock,
924 intel_clock_t *best_clock)
925{
a93e255f 926 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 927 struct drm_device *dev = crtc->base.dev;
9ca3ba01 928 unsigned int best_error_ppm;
ef9348c8
CML
929 intel_clock_t clock;
930 uint64_t m2;
931 int found = false;
932
933 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 934 best_error_ppm = 1000000;
ef9348c8
CML
935
936 /*
937 * Based on hardware doc, the n always set to 1, and m1 always
938 * set to 2. If requires to support 200Mhz refclk, we need to
939 * revisit this because n may not 1 anymore.
940 */
941 clock.n = 1, clock.m1 = 2;
942 target *= 5; /* fast clock */
943
944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
945 for (clock.p2 = limit->p2.p2_fast;
946 clock.p2 >= limit->p2.p2_slow;
947 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 948 unsigned int error_ppm;
ef9348c8
CML
949
950 clock.p = clock.p1 * clock.p2;
951
952 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
953 clock.n) << 22, refclk * clock.m1);
954
955 if (m2 > INT_MAX/clock.m1)
956 continue;
957
958 clock.m2 = m2;
959
dccbea3b 960 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
961
962 if (!intel_PLL_is_valid(dev, limit, &clock))
963 continue;
964
9ca3ba01
ID
965 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
966 best_error_ppm, &error_ppm))
967 continue;
968
969 *best_clock = clock;
970 best_error_ppm = error_ppm;
971 found = true;
ef9348c8
CML
972 }
973 }
974
975 return found;
976}
977
5ab7b0b7
ID
978bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
979 intel_clock_t *best_clock)
980{
981 int refclk = i9xx_get_refclk(crtc_state, 0);
982
983 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
984 target_clock, refclk, NULL, best_clock);
985}
986
20ddf665
VS
987bool intel_crtc_active(struct drm_crtc *crtc)
988{
989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
990
991 /* Be paranoid as we can arrive here with only partial
992 * state retrieved from the hardware during setup.
993 *
241bfc38 994 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
995 * as Haswell has gained clock readout/fastboot support.
996 *
66e514c1 997 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 998 * properly reconstruct framebuffers.
c3d1f436
MR
999 *
1000 * FIXME: The intel_crtc->active here should be switched to
1001 * crtc->state->active once we have proper CRTC states wired up
1002 * for atomic.
20ddf665 1003 */
c3d1f436 1004 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1005 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1006}
1007
a5c961d1
PZ
1008enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1009 enum pipe pipe)
1010{
1011 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1013
6e3c9717 1014 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1015}
1016
fbf49ea2
VS
1017static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1018{
1019 struct drm_i915_private *dev_priv = dev->dev_private;
1020 u32 reg = PIPEDSL(pipe);
1021 u32 line1, line2;
1022 u32 line_mask;
1023
1024 if (IS_GEN2(dev))
1025 line_mask = DSL_LINEMASK_GEN2;
1026 else
1027 line_mask = DSL_LINEMASK_GEN3;
1028
1029 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1030 msleep(5);
fbf49ea2
VS
1031 line2 = I915_READ(reg) & line_mask;
1032
1033 return line1 == line2;
1034}
1035
ab7ad7f6
KP
1036/*
1037 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1038 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1039 *
1040 * After disabling a pipe, we can't wait for vblank in the usual way,
1041 * spinning on the vblank interrupt status bit, since we won't actually
1042 * see an interrupt when the pipe is disabled.
1043 *
ab7ad7f6
KP
1044 * On Gen4 and above:
1045 * wait for the pipe register state bit to turn off
1046 *
1047 * Otherwise:
1048 * wait for the display line value to settle (it usually
1049 * ends up stopping at the start of the next frame).
58e10eb9 1050 *
9d0498a2 1051 */
575f7ab7 1052static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1053{
575f7ab7 1054 struct drm_device *dev = crtc->base.dev;
9d0498a2 1055 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1056 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1057 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1058
1059 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1060 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1061
1062 /* Wait for the Pipe State to go off */
58e10eb9
CW
1063 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1064 100))
284637d9 1065 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1066 } else {
ab7ad7f6 1067 /* Wait for the display line to settle */
fbf49ea2 1068 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1069 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1070 }
79e53945
JB
1071}
1072
b0ea7d37
DL
1073/*
1074 * ibx_digital_port_connected - is the specified port connected?
1075 * @dev_priv: i915 private structure
1076 * @port: the port to test
1077 *
1078 * Returns true if @port is connected, false otherwise.
1079 */
1080bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1081 struct intel_digital_port *port)
1082{
1083 u32 bit;
1084
c36346e3 1085 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1086 switch (port->port) {
c36346e3
DL
1087 case PORT_B:
1088 bit = SDE_PORTB_HOTPLUG;
1089 break;
1090 case PORT_C:
1091 bit = SDE_PORTC_HOTPLUG;
1092 break;
1093 case PORT_D:
1094 bit = SDE_PORTD_HOTPLUG;
1095 break;
1096 default:
1097 return true;
1098 }
1099 } else {
eba905b2 1100 switch (port->port) {
c36346e3
DL
1101 case PORT_B:
1102 bit = SDE_PORTB_HOTPLUG_CPT;
1103 break;
1104 case PORT_C:
1105 bit = SDE_PORTC_HOTPLUG_CPT;
1106 break;
1107 case PORT_D:
1108 bit = SDE_PORTD_HOTPLUG_CPT;
1109 break;
1110 default:
1111 return true;
1112 }
b0ea7d37
DL
1113 }
1114
1115 return I915_READ(SDEISR) & bit;
1116}
1117
b24e7179
JB
1118static const char *state_string(bool enabled)
1119{
1120 return enabled ? "on" : "off";
1121}
1122
1123/* Only for pre-ILK configs */
55607e8a
DV
1124void assert_pll(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
b24e7179
JB
1126{
1127 int reg;
1128 u32 val;
1129 bool cur_state;
1130
1131 reg = DPLL(pipe);
1132 val = I915_READ(reg);
1133 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1134 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1135 "PLL state assertion failure (expected %s, current %s)\n",
1136 state_string(state), state_string(cur_state));
1137}
b24e7179 1138
23538ef1
JN
1139/* XXX: the dsi pll is shared between MIPI DSI ports */
1140static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1141{
1142 u32 val;
1143 bool cur_state;
1144
a580516d 1145 mutex_lock(&dev_priv->sb_lock);
23538ef1 1146 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1147 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1148
1149 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1150 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1151 "DSI PLL state assertion failure (expected %s, current %s)\n",
1152 state_string(state), state_string(cur_state));
1153}
1154#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1155#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1156
55607e8a 1157struct intel_shared_dpll *
e2b78267
DV
1158intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1159{
1160 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1161
6e3c9717 1162 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1163 return NULL;
1164
6e3c9717 1165 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1166}
1167
040484af 1168/* For ILK+ */
55607e8a
DV
1169void assert_shared_dpll(struct drm_i915_private *dev_priv,
1170 struct intel_shared_dpll *pll,
1171 bool state)
040484af 1172{
040484af 1173 bool cur_state;
5358901f 1174 struct intel_dpll_hw_state hw_state;
040484af 1175
92b27b08 1176 if (WARN (!pll,
46edb027 1177 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1178 return;
ee7b9f93 1179
5358901f 1180 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1181 I915_STATE_WARN(cur_state != state,
5358901f
DV
1182 "%s assertion failure (expected %s, current %s)\n",
1183 pll->name, state_string(state), state_string(cur_state));
040484af 1184}
040484af
JB
1185
1186static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1187 enum pipe pipe, bool state)
1188{
1189 int reg;
1190 u32 val;
1191 bool cur_state;
ad80a810
PZ
1192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1193 pipe);
040484af 1194
affa9354
PZ
1195 if (HAS_DDI(dev_priv->dev)) {
1196 /* DDI does not have a specific FDI_TX register */
ad80a810 1197 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1198 val = I915_READ(reg);
ad80a810 1199 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1200 } else {
1201 reg = FDI_TX_CTL(pipe);
1202 val = I915_READ(reg);
1203 cur_state = !!(val & FDI_TX_ENABLE);
1204 }
e2c719b7 1205 I915_STATE_WARN(cur_state != state,
040484af
JB
1206 "FDI TX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1210#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1211
1212static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1213 enum pipe pipe, bool state)
1214{
1215 int reg;
1216 u32 val;
1217 bool cur_state;
1218
d63fa0dc
PZ
1219 reg = FDI_RX_CTL(pipe);
1220 val = I915_READ(reg);
1221 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1222 I915_STATE_WARN(cur_state != state,
040484af
JB
1223 "FDI RX state assertion failure (expected %s, current %s)\n",
1224 state_string(state), state_string(cur_state));
1225}
1226#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1227#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1228
1229static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1230 enum pipe pipe)
1231{
1232 int reg;
1233 u32 val;
1234
1235 /* ILK FDI PLL is always enabled */
3d13ef2e 1236 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1237 return;
1238
bf507ef7 1239 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1240 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1241 return;
1242
040484af
JB
1243 reg = FDI_TX_CTL(pipe);
1244 val = I915_READ(reg);
e2c719b7 1245 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1246}
1247
55607e8a
DV
1248void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1249 enum pipe pipe, bool state)
040484af
JB
1250{
1251 int reg;
1252 u32 val;
55607e8a 1253 bool cur_state;
040484af
JB
1254
1255 reg = FDI_RX_CTL(pipe);
1256 val = I915_READ(reg);
55607e8a 1257 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1258 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1259 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1260 state_string(state), state_string(cur_state));
040484af
JB
1261}
1262
b680c37a
DV
1263void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1264 enum pipe pipe)
ea0760cf 1265{
bedd4dba
JN
1266 struct drm_device *dev = dev_priv->dev;
1267 int pp_reg;
ea0760cf
JB
1268 u32 val;
1269 enum pipe panel_pipe = PIPE_A;
0de3b485 1270 bool locked = true;
ea0760cf 1271
bedd4dba
JN
1272 if (WARN_ON(HAS_DDI(dev)))
1273 return;
1274
1275 if (HAS_PCH_SPLIT(dev)) {
1276 u32 port_sel;
1277
ea0760cf 1278 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1279 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1280
1281 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1282 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1283 panel_pipe = PIPE_B;
1284 /* XXX: else fix for eDP */
1285 } else if (IS_VALLEYVIEW(dev)) {
1286 /* presumably write lock depends on pipe, not port select */
1287 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1288 panel_pipe = pipe;
ea0760cf
JB
1289 } else {
1290 pp_reg = PP_CONTROL;
bedd4dba
JN
1291 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1292 panel_pipe = PIPE_B;
ea0760cf
JB
1293 }
1294
1295 val = I915_READ(pp_reg);
1296 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1297 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1298 locked = false;
1299
e2c719b7 1300 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1301 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1302 pipe_name(pipe));
ea0760cf
JB
1303}
1304
93ce0ba6
JN
1305static void assert_cursor(struct drm_i915_private *dev_priv,
1306 enum pipe pipe, bool state)
1307{
1308 struct drm_device *dev = dev_priv->dev;
1309 bool cur_state;
1310
d9d82081 1311 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1312 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1313 else
5efb3e28 1314 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1315
e2c719b7 1316 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1317 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1318 pipe_name(pipe), state_string(state), state_string(cur_state));
1319}
1320#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1321#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1322
b840d907
JB
1323void assert_pipe(struct drm_i915_private *dev_priv,
1324 enum pipe pipe, bool state)
b24e7179
JB
1325{
1326 int reg;
1327 u32 val;
63d7bbe9 1328 bool cur_state;
702e7a56
PZ
1329 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1330 pipe);
b24e7179 1331
b6b5d049
VS
1332 /* if we need the pipe quirk it must be always on */
1333 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1334 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1335 state = true;
1336
f458ebbc 1337 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1338 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1339 cur_state = false;
1340 } else {
1341 reg = PIPECONF(cpu_transcoder);
1342 val = I915_READ(reg);
1343 cur_state = !!(val & PIPECONF_ENABLE);
1344 }
1345
e2c719b7 1346 I915_STATE_WARN(cur_state != state,
63d7bbe9 1347 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1348 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1349}
1350
931872fc
CW
1351static void assert_plane(struct drm_i915_private *dev_priv,
1352 enum plane plane, bool state)
b24e7179
JB
1353{
1354 int reg;
1355 u32 val;
931872fc 1356 bool cur_state;
b24e7179
JB
1357
1358 reg = DSPCNTR(plane);
1359 val = I915_READ(reg);
931872fc 1360 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1361 I915_STATE_WARN(cur_state != state,
931872fc
CW
1362 "plane %c assertion failure (expected %s, current %s)\n",
1363 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1364}
1365
931872fc
CW
1366#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1367#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1368
b24e7179
JB
1369static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1370 enum pipe pipe)
1371{
653e1026 1372 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1373 int reg, i;
1374 u32 val;
1375 int cur_pipe;
1376
653e1026
VS
1377 /* Primary planes are fixed to pipes on gen4+ */
1378 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1379 reg = DSPCNTR(pipe);
1380 val = I915_READ(reg);
e2c719b7 1381 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1382 "plane %c assertion failure, should be disabled but not\n",
1383 plane_name(pipe));
19ec1358 1384 return;
28c05794 1385 }
19ec1358 1386
b24e7179 1387 /* Need to check both planes against the pipe */
055e393f 1388 for_each_pipe(dev_priv, i) {
b24e7179
JB
1389 reg = DSPCNTR(i);
1390 val = I915_READ(reg);
1391 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1392 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1393 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1394 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1395 plane_name(i), pipe_name(pipe));
b24e7179
JB
1396 }
1397}
1398
19332d7a
JB
1399static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe)
1401{
20674eef 1402 struct drm_device *dev = dev_priv->dev;
1fe47785 1403 int reg, sprite;
19332d7a
JB
1404 u32 val;
1405
7feb8b88 1406 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1407 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1408 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1409 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1410 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1411 sprite, pipe_name(pipe));
1412 }
1413 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1414 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1415 reg = SPCNTR(pipe, sprite);
20674eef 1416 val = I915_READ(reg);
e2c719b7 1417 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1418 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1419 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1420 }
1421 } else if (INTEL_INFO(dev)->gen >= 7) {
1422 reg = SPRCTL(pipe);
19332d7a 1423 val = I915_READ(reg);
e2c719b7 1424 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1425 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1426 plane_name(pipe), pipe_name(pipe));
1427 } else if (INTEL_INFO(dev)->gen >= 5) {
1428 reg = DVSCNTR(pipe);
19332d7a 1429 val = I915_READ(reg);
e2c719b7 1430 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1431 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1432 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1433 }
1434}
1435
08c71e5e
VS
1436static void assert_vblank_disabled(struct drm_crtc *crtc)
1437{
e2c719b7 1438 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1439 drm_crtc_vblank_put(crtc);
1440}
1441
89eff4be 1442static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1443{
1444 u32 val;
1445 bool enabled;
1446
e2c719b7 1447 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1448
92f2584a
JB
1449 val = I915_READ(PCH_DREF_CONTROL);
1450 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1451 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1452 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1453}
1454
ab9412ba
DV
1455static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1456 enum pipe pipe)
92f2584a
JB
1457{
1458 int reg;
1459 u32 val;
1460 bool enabled;
1461
ab9412ba 1462 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1463 val = I915_READ(reg);
1464 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1465 I915_STATE_WARN(enabled,
9db4a9c7
JB
1466 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1467 pipe_name(pipe));
92f2584a
JB
1468}
1469
4e634389
KP
1470static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1471 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1472{
1473 if ((val & DP_PORT_EN) == 0)
1474 return false;
1475
1476 if (HAS_PCH_CPT(dev_priv->dev)) {
1477 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1478 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1479 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1480 return false;
44f37d1f
CML
1481 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1482 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1483 return false;
f0575e92
KP
1484 } else {
1485 if ((val & DP_PIPE_MASK) != (pipe << 30))
1486 return false;
1487 }
1488 return true;
1489}
1490
1519b995
KP
1491static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1492 enum pipe pipe, u32 val)
1493{
dc0fa718 1494 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1495 return false;
1496
1497 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1498 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1499 return false;
44f37d1f
CML
1500 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1501 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1502 return false;
1519b995 1503 } else {
dc0fa718 1504 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1505 return false;
1506 }
1507 return true;
1508}
1509
1510static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1511 enum pipe pipe, u32 val)
1512{
1513 if ((val & LVDS_PORT_EN) == 0)
1514 return false;
1515
1516 if (HAS_PCH_CPT(dev_priv->dev)) {
1517 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1518 return false;
1519 } else {
1520 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1521 return false;
1522 }
1523 return true;
1524}
1525
1526static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1527 enum pipe pipe, u32 val)
1528{
1529 if ((val & ADPA_DAC_ENABLE) == 0)
1530 return false;
1531 if (HAS_PCH_CPT(dev_priv->dev)) {
1532 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1533 return false;
1534 } else {
1535 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1536 return false;
1537 }
1538 return true;
1539}
1540
291906f1 1541static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1542 enum pipe pipe, int reg, u32 port_sel)
291906f1 1543{
47a05eca 1544 u32 val = I915_READ(reg);
e2c719b7 1545 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1546 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1547 reg, pipe_name(pipe));
de9a35ab 1548
e2c719b7 1549 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1550 && (val & DP_PIPEB_SELECT),
de9a35ab 1551 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1552}
1553
1554static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1555 enum pipe pipe, int reg)
1556{
47a05eca 1557 u32 val = I915_READ(reg);
e2c719b7 1558 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1559 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1560 reg, pipe_name(pipe));
de9a35ab 1561
e2c719b7 1562 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1563 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1564 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1565}
1566
1567static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1568 enum pipe pipe)
1569{
1570 int reg;
1571 u32 val;
291906f1 1572
f0575e92
KP
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1575 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1576
1577 reg = PCH_ADPA;
1578 val = I915_READ(reg);
e2c719b7 1579 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1580 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1581 pipe_name(pipe));
291906f1
JB
1582
1583 reg = PCH_LVDS;
1584 val = I915_READ(reg);
e2c719b7 1585 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1586 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1587 pipe_name(pipe));
291906f1 1588
e2debe91
PZ
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1591 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1592}
1593
40e9cf64
JB
1594static void intel_init_dpio(struct drm_device *dev)
1595{
1596 struct drm_i915_private *dev_priv = dev->dev_private;
1597
1598 if (!IS_VALLEYVIEW(dev))
1599 return;
1600
a09caddd
CML
1601 /*
1602 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1603 * CHV x1 PHY (DP/HDMI D)
1604 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1605 */
1606 if (IS_CHERRYVIEW(dev)) {
1607 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1609 } else {
1610 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1611 }
5382f5f3
JB
1612}
1613
d288f65f 1614static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1615 const struct intel_crtc_state *pipe_config)
87442f73 1616{
426115cf
DV
1617 struct drm_device *dev = crtc->base.dev;
1618 struct drm_i915_private *dev_priv = dev->dev_private;
1619 int reg = DPLL(crtc->pipe);
d288f65f 1620 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1621
426115cf 1622 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1623
1624 /* No really, not for ILK+ */
1625 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1626
1627 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1628 if (IS_MOBILE(dev_priv->dev))
426115cf 1629 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1630
426115cf
DV
1631 I915_WRITE(reg, dpll);
1632 POSTING_READ(reg);
1633 udelay(150);
1634
1635 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1636 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1637
d288f65f 1638 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1639 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1640
1641 /* We do this three times for luck */
426115cf 1642 I915_WRITE(reg, dpll);
87442f73
DV
1643 POSTING_READ(reg);
1644 udelay(150); /* wait for warmup */
426115cf 1645 I915_WRITE(reg, dpll);
87442f73
DV
1646 POSTING_READ(reg);
1647 udelay(150); /* wait for warmup */
426115cf 1648 I915_WRITE(reg, dpll);
87442f73
DV
1649 POSTING_READ(reg);
1650 udelay(150); /* wait for warmup */
1651}
1652
d288f65f 1653static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1654 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1655{
1656 struct drm_device *dev = crtc->base.dev;
1657 struct drm_i915_private *dev_priv = dev->dev_private;
1658 int pipe = crtc->pipe;
1659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1660 u32 tmp;
1661
1662 assert_pipe_disabled(dev_priv, crtc->pipe);
1663
1664 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1665
a580516d 1666 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1667
1668 /* Enable back the 10bit clock to display controller */
1669 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1670 tmp |= DPIO_DCLKP_EN;
1671 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1672
54433e91
VS
1673 mutex_unlock(&dev_priv->sb_lock);
1674
9d556c99
CML
1675 /*
1676 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1677 */
1678 udelay(1);
1679
1680 /* Enable PLL */
d288f65f 1681 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1682
1683 /* Check PLL is locked */
a11b0703 1684 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1685 DRM_ERROR("PLL %d failed to lock\n", pipe);
1686
a11b0703 1687 /* not sure when this should be written */
d288f65f 1688 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1689 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1690}
1691
1c4e0274
VS
1692static int intel_num_dvo_pipes(struct drm_device *dev)
1693{
1694 struct intel_crtc *crtc;
1695 int count = 0;
1696
1697 for_each_intel_crtc(dev, crtc)
3538b9df 1698 count += crtc->base.state->active &&
409ee761 1699 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1700
1701 return count;
1702}
1703
66e3d5c0 1704static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1705{
66e3d5c0
DV
1706 struct drm_device *dev = crtc->base.dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 int reg = DPLL(crtc->pipe);
6e3c9717 1709 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1710
66e3d5c0 1711 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1712
63d7bbe9 1713 /* No really, not for ILK+ */
3d13ef2e 1714 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1715
1716 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1717 if (IS_MOBILE(dev) && !IS_I830(dev))
1718 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1719
1c4e0274
VS
1720 /* Enable DVO 2x clock on both PLLs if necessary */
1721 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1722 /*
1723 * It appears to be important that we don't enable this
1724 * for the current pipe before otherwise configuring the
1725 * PLL. No idea how this should be handled if multiple
1726 * DVO outputs are enabled simultaneosly.
1727 */
1728 dpll |= DPLL_DVO_2X_MODE;
1729 I915_WRITE(DPLL(!crtc->pipe),
1730 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1731 }
66e3d5c0
DV
1732
1733 /* Wait for the clocks to stabilize. */
1734 POSTING_READ(reg);
1735 udelay(150);
1736
1737 if (INTEL_INFO(dev)->gen >= 4) {
1738 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1739 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1740 } else {
1741 /* The pixel multiplier can only be updated once the
1742 * DPLL is enabled and the clocks are stable.
1743 *
1744 * So write it again.
1745 */
1746 I915_WRITE(reg, dpll);
1747 }
63d7bbe9
JB
1748
1749 /* We do this three times for luck */
66e3d5c0 1750 I915_WRITE(reg, dpll);
63d7bbe9
JB
1751 POSTING_READ(reg);
1752 udelay(150); /* wait for warmup */
66e3d5c0 1753 I915_WRITE(reg, dpll);
63d7bbe9
JB
1754 POSTING_READ(reg);
1755 udelay(150); /* wait for warmup */
66e3d5c0 1756 I915_WRITE(reg, dpll);
63d7bbe9
JB
1757 POSTING_READ(reg);
1758 udelay(150); /* wait for warmup */
1759}
1760
1761/**
50b44a44 1762 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1763 * @dev_priv: i915 private structure
1764 * @pipe: pipe PLL to disable
1765 *
1766 * Disable the PLL for @pipe, making sure the pipe is off first.
1767 *
1768 * Note! This is for pre-ILK only.
1769 */
1c4e0274 1770static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1771{
1c4e0274
VS
1772 struct drm_device *dev = crtc->base.dev;
1773 struct drm_i915_private *dev_priv = dev->dev_private;
1774 enum pipe pipe = crtc->pipe;
1775
1776 /* Disable DVO 2x clock on both PLLs if necessary */
1777 if (IS_I830(dev) &&
409ee761 1778 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1779 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1780 I915_WRITE(DPLL(PIPE_B),
1781 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1782 I915_WRITE(DPLL(PIPE_A),
1783 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1784 }
1785
b6b5d049
VS
1786 /* Don't disable pipe or pipe PLLs if needed */
1787 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1788 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1789 return;
1790
1791 /* Make sure the pipe isn't still relying on us */
1792 assert_pipe_disabled(dev_priv, pipe);
1793
b8afb911 1794 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1795 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1796}
1797
f6071166
JB
1798static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1799{
b8afb911 1800 u32 val;
f6071166
JB
1801
1802 /* Make sure the pipe isn't still relying on us */
1803 assert_pipe_disabled(dev_priv, pipe);
1804
e5cbfbfb
ID
1805 /*
1806 * Leave integrated clock source and reference clock enabled for pipe B.
1807 * The latter is needed for VGA hotplug / manual detection.
1808 */
b8afb911 1809 val = DPLL_VGA_MODE_DIS;
f6071166 1810 if (pipe == PIPE_B)
60bfe44f 1811 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1812 I915_WRITE(DPLL(pipe), val);
1813 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1814
1815}
1816
1817static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1818{
d752048d 1819 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1820 u32 val;
1821
a11b0703
VS
1822 /* Make sure the pipe isn't still relying on us */
1823 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1824
a11b0703 1825 /* Set PLL en = 0 */
60bfe44f
VS
1826 val = DPLL_SSC_REF_CLK_CHV |
1827 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1828 if (pipe != PIPE_A)
1829 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1830 I915_WRITE(DPLL(pipe), val);
1831 POSTING_READ(DPLL(pipe));
d752048d 1832
a580516d 1833 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1834
1835 /* Disable 10bit clock to display controller */
1836 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1837 val &= ~DPIO_DCLKP_EN;
1838 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1839
61407f6d
VS
1840 /* disable left/right clock distribution */
1841 if (pipe != PIPE_B) {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1843 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1845 } else {
1846 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1847 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1848 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1849 }
1850
a580516d 1851 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1852}
1853
e4607fcf 1854void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1855 struct intel_digital_port *dport,
1856 unsigned int expected_mask)
89b667f8
JB
1857{
1858 u32 port_mask;
00fc31b7 1859 int dpll_reg;
89b667f8 1860
e4607fcf
CML
1861 switch (dport->port) {
1862 case PORT_B:
89b667f8 1863 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1864 dpll_reg = DPLL(0);
e4607fcf
CML
1865 break;
1866 case PORT_C:
89b667f8 1867 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1868 dpll_reg = DPLL(0);
9b6de0a1 1869 expected_mask <<= 4;
00fc31b7
CML
1870 break;
1871 case PORT_D:
1872 port_mask = DPLL_PORTD_READY_MASK;
1873 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1874 break;
1875 default:
1876 BUG();
1877 }
89b667f8 1878
9b6de0a1
VS
1879 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1880 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1881 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1882}
1883
b14b1055
DV
1884static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1885{
1886 struct drm_device *dev = crtc->base.dev;
1887 struct drm_i915_private *dev_priv = dev->dev_private;
1888 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1889
be19f0ff
CW
1890 if (WARN_ON(pll == NULL))
1891 return;
1892
3e369b76 1893 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1894 if (pll->active == 0) {
1895 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1896 WARN_ON(pll->on);
1897 assert_shared_dpll_disabled(dev_priv, pll);
1898
1899 pll->mode_set(dev_priv, pll);
1900 }
1901}
1902
92f2584a 1903/**
85b3894f 1904 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1905 * @dev_priv: i915 private structure
1906 * @pipe: pipe PLL to enable
1907 *
1908 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1909 * drives the transcoder clock.
1910 */
85b3894f 1911static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1912{
3d13ef2e
DL
1913 struct drm_device *dev = crtc->base.dev;
1914 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1915 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1916
87a875bb 1917 if (WARN_ON(pll == NULL))
48da64a8
CW
1918 return;
1919
3e369b76 1920 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1921 return;
ee7b9f93 1922
74dd6928 1923 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1924 pll->name, pll->active, pll->on,
e2b78267 1925 crtc->base.base.id);
92f2584a 1926
cdbd2316
DV
1927 if (pll->active++) {
1928 WARN_ON(!pll->on);
e9d6944e 1929 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1930 return;
1931 }
f4a091c7 1932 WARN_ON(pll->on);
ee7b9f93 1933
bd2bb1b9
PZ
1934 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1935
46edb027 1936 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1937 pll->enable(dev_priv, pll);
ee7b9f93 1938 pll->on = true;
92f2584a
JB
1939}
1940
f6daaec2 1941static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1942{
3d13ef2e
DL
1943 struct drm_device *dev = crtc->base.dev;
1944 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1945 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1946
92f2584a 1947 /* PCH only available on ILK+ */
3d13ef2e 1948 BUG_ON(INTEL_INFO(dev)->gen < 5);
eddfcbcd
ML
1949 if (pll == NULL)
1950 return;
92f2584a 1951
eddfcbcd 1952 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
48da64a8 1953 return;
7a419866 1954
46edb027
DV
1955 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1956 pll->name, pll->active, pll->on,
e2b78267 1957 crtc->base.base.id);
7a419866 1958
48da64a8 1959 if (WARN_ON(pll->active == 0)) {
e9d6944e 1960 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1961 return;
1962 }
1963
e9d6944e 1964 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1965 WARN_ON(!pll->on);
cdbd2316 1966 if (--pll->active)
7a419866 1967 return;
ee7b9f93 1968
46edb027 1969 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1970 pll->disable(dev_priv, pll);
ee7b9f93 1971 pll->on = false;
bd2bb1b9
PZ
1972
1973 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1974}
1975
b8a4f404
PZ
1976static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1977 enum pipe pipe)
040484af 1978{
23670b32 1979 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1980 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1982 uint32_t reg, val, pipeconf_val;
040484af
JB
1983
1984 /* PCH only available on ILK+ */
55522f37 1985 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1986
1987 /* Make sure PCH DPLL is enabled */
e72f9fbf 1988 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1989 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1990
1991 /* FDI must be feeding us bits for PCH ports */
1992 assert_fdi_tx_enabled(dev_priv, pipe);
1993 assert_fdi_rx_enabled(dev_priv, pipe);
1994
23670b32
DV
1995 if (HAS_PCH_CPT(dev)) {
1996 /* Workaround: Set the timing override bit before enabling the
1997 * pch transcoder. */
1998 reg = TRANS_CHICKEN2(pipe);
1999 val = I915_READ(reg);
2000 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2001 I915_WRITE(reg, val);
59c859d6 2002 }
23670b32 2003
ab9412ba 2004 reg = PCH_TRANSCONF(pipe);
040484af 2005 val = I915_READ(reg);
5f7f726d 2006 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2007
2008 if (HAS_PCH_IBX(dev_priv->dev)) {
2009 /*
c5de7c6f
VS
2010 * Make the BPC in transcoder be consistent with
2011 * that in pipeconf reg. For HDMI we must use 8bpc
2012 * here for both 8bpc and 12bpc.
e9bcff5c 2013 */
dfd07d72 2014 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
2015 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2016 val |= PIPECONF_8BPC;
2017 else
2018 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2019 }
5f7f726d
PZ
2020
2021 val &= ~TRANS_INTERLACE_MASK;
2022 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2023 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2024 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2025 val |= TRANS_LEGACY_INTERLACED_ILK;
2026 else
2027 val |= TRANS_INTERLACED;
5f7f726d
PZ
2028 else
2029 val |= TRANS_PROGRESSIVE;
2030
040484af
JB
2031 I915_WRITE(reg, val | TRANS_ENABLE);
2032 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2033 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2034}
2035
8fb033d7 2036static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2037 enum transcoder cpu_transcoder)
040484af 2038{
8fb033d7 2039 u32 val, pipeconf_val;
8fb033d7
PZ
2040
2041 /* PCH only available on ILK+ */
55522f37 2042 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2043
8fb033d7 2044 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2045 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2046 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2047
223a6fdf
PZ
2048 /* Workaround: set timing override bit. */
2049 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2050 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2051 I915_WRITE(_TRANSA_CHICKEN2, val);
2052
25f3ef11 2053 val = TRANS_ENABLE;
937bb610 2054 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2055
9a76b1c6
PZ
2056 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2057 PIPECONF_INTERLACED_ILK)
a35f2679 2058 val |= TRANS_INTERLACED;
8fb033d7
PZ
2059 else
2060 val |= TRANS_PROGRESSIVE;
2061
ab9412ba
DV
2062 I915_WRITE(LPT_TRANSCONF, val);
2063 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2064 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2065}
2066
b8a4f404
PZ
2067static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2068 enum pipe pipe)
040484af 2069{
23670b32
DV
2070 struct drm_device *dev = dev_priv->dev;
2071 uint32_t reg, val;
040484af
JB
2072
2073 /* FDI relies on the transcoder */
2074 assert_fdi_tx_disabled(dev_priv, pipe);
2075 assert_fdi_rx_disabled(dev_priv, pipe);
2076
291906f1
JB
2077 /* Ports must be off as well */
2078 assert_pch_ports_disabled(dev_priv, pipe);
2079
ab9412ba 2080 reg = PCH_TRANSCONF(pipe);
040484af
JB
2081 val = I915_READ(reg);
2082 val &= ~TRANS_ENABLE;
2083 I915_WRITE(reg, val);
2084 /* wait for PCH transcoder off, transcoder state */
2085 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2086 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2087
2088 if (!HAS_PCH_IBX(dev)) {
2089 /* Workaround: Clear the timing override chicken bit again. */
2090 reg = TRANS_CHICKEN2(pipe);
2091 val = I915_READ(reg);
2092 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2093 I915_WRITE(reg, val);
2094 }
040484af
JB
2095}
2096
ab4d966c 2097static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2098{
8fb033d7
PZ
2099 u32 val;
2100
ab9412ba 2101 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2102 val &= ~TRANS_ENABLE;
ab9412ba 2103 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2104 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2105 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2106 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2107
2108 /* Workaround: clear timing override bit. */
2109 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2110 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2111 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2112}
2113
b24e7179 2114/**
309cfea8 2115 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2116 * @crtc: crtc responsible for the pipe
b24e7179 2117 *
0372264a 2118 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2119 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2120 */
e1fdc473 2121static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2122{
0372264a
PZ
2123 struct drm_device *dev = crtc->base.dev;
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2127 pipe);
1a240d4d 2128 enum pipe pch_transcoder;
b24e7179
JB
2129 int reg;
2130 u32 val;
2131
9e2ee2dd
VS
2132 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2133
58c6eaa2 2134 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2135 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2136 assert_sprites_disabled(dev_priv, pipe);
2137
681e5811 2138 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2139 pch_transcoder = TRANSCODER_A;
2140 else
2141 pch_transcoder = pipe;
2142
b24e7179
JB
2143 /*
2144 * A pipe without a PLL won't actually be able to drive bits from
2145 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2146 * need the check.
2147 */
50360403 2148 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2149 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2150 assert_dsi_pll_enabled(dev_priv);
2151 else
2152 assert_pll_enabled(dev_priv, pipe);
040484af 2153 else {
6e3c9717 2154 if (crtc->config->has_pch_encoder) {
040484af 2155 /* if driving the PCH, we need FDI enabled */
cc391bbb 2156 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2157 assert_fdi_tx_pll_enabled(dev_priv,
2158 (enum pipe) cpu_transcoder);
040484af
JB
2159 }
2160 /* FIXME: assert CPU port conditions for SNB+ */
2161 }
b24e7179 2162
702e7a56 2163 reg = PIPECONF(cpu_transcoder);
b24e7179 2164 val = I915_READ(reg);
7ad25d48 2165 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2166 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2167 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2168 return;
7ad25d48 2169 }
00d70b15
CW
2170
2171 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2172 POSTING_READ(reg);
b24e7179
JB
2173}
2174
2175/**
309cfea8 2176 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2177 * @crtc: crtc whose pipes is to be disabled
b24e7179 2178 *
575f7ab7
VS
2179 * Disable the pipe of @crtc, making sure that various hardware
2180 * specific requirements are met, if applicable, e.g. plane
2181 * disabled, panel fitter off, etc.
b24e7179
JB
2182 *
2183 * Will wait until the pipe has shut down before returning.
2184 */
575f7ab7 2185static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2186{
575f7ab7 2187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2188 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2189 enum pipe pipe = crtc->pipe;
b24e7179
JB
2190 int reg;
2191 u32 val;
2192
9e2ee2dd
VS
2193 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2194
b24e7179
JB
2195 /*
2196 * Make sure planes won't keep trying to pump pixels to us,
2197 * or we might hang the display.
2198 */
2199 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2200 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2201 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2202
702e7a56 2203 reg = PIPECONF(cpu_transcoder);
b24e7179 2204 val = I915_READ(reg);
00d70b15
CW
2205 if ((val & PIPECONF_ENABLE) == 0)
2206 return;
2207
67adc644
VS
2208 /*
2209 * Double wide has implications for planes
2210 * so best keep it disabled when not needed.
2211 */
6e3c9717 2212 if (crtc->config->double_wide)
67adc644
VS
2213 val &= ~PIPECONF_DOUBLE_WIDE;
2214
2215 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2216 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2217 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2218 val &= ~PIPECONF_ENABLE;
2219
2220 I915_WRITE(reg, val);
2221 if ((val & PIPECONF_ENABLE) == 0)
2222 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2223}
2224
693db184
CW
2225static bool need_vtd_wa(struct drm_device *dev)
2226{
2227#ifdef CONFIG_INTEL_IOMMU
2228 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2229 return true;
2230#endif
2231 return false;
2232}
2233
50470bb0 2234unsigned int
6761dd31
TU
2235intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2236 uint64_t fb_format_modifier)
a57ce0b2 2237{
6761dd31
TU
2238 unsigned int tile_height;
2239 uint32_t pixel_bytes;
a57ce0b2 2240
b5d0e9bf
DL
2241 switch (fb_format_modifier) {
2242 case DRM_FORMAT_MOD_NONE:
2243 tile_height = 1;
2244 break;
2245 case I915_FORMAT_MOD_X_TILED:
2246 tile_height = IS_GEN2(dev) ? 16 : 8;
2247 break;
2248 case I915_FORMAT_MOD_Y_TILED:
2249 tile_height = 32;
2250 break;
2251 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2252 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2253 switch (pixel_bytes) {
b5d0e9bf 2254 default:
6761dd31 2255 case 1:
b5d0e9bf
DL
2256 tile_height = 64;
2257 break;
6761dd31
TU
2258 case 2:
2259 case 4:
b5d0e9bf
DL
2260 tile_height = 32;
2261 break;
6761dd31 2262 case 8:
b5d0e9bf
DL
2263 tile_height = 16;
2264 break;
6761dd31 2265 case 16:
b5d0e9bf
DL
2266 WARN_ONCE(1,
2267 "128-bit pixels are not supported for display!");
2268 tile_height = 16;
2269 break;
2270 }
2271 break;
2272 default:
2273 MISSING_CASE(fb_format_modifier);
2274 tile_height = 1;
2275 break;
2276 }
091df6cb 2277
6761dd31
TU
2278 return tile_height;
2279}
2280
2281unsigned int
2282intel_fb_align_height(struct drm_device *dev, unsigned int height,
2283 uint32_t pixel_format, uint64_t fb_format_modifier)
2284{
2285 return ALIGN(height, intel_tile_height(dev, pixel_format,
2286 fb_format_modifier));
a57ce0b2
JB
2287}
2288
f64b98cd
TU
2289static int
2290intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2291 const struct drm_plane_state *plane_state)
2292{
50470bb0 2293 struct intel_rotation_info *info = &view->rotation_info;
84fe03f7 2294 unsigned int tile_height, tile_pitch;
50470bb0 2295
f64b98cd
TU
2296 *view = i915_ggtt_view_normal;
2297
50470bb0
TU
2298 if (!plane_state)
2299 return 0;
2300
121920fa 2301 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2302 return 0;
2303
9abc4648 2304 *view = i915_ggtt_view_rotated;
50470bb0
TU
2305
2306 info->height = fb->height;
2307 info->pixel_format = fb->pixel_format;
2308 info->pitch = fb->pitches[0];
2309 info->fb_modifier = fb->modifier[0];
2310
84fe03f7
TU
2311 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2312 fb->modifier[0]);
2313 tile_pitch = PAGE_SIZE / tile_height;
2314 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2315 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2316 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2317
f64b98cd
TU
2318 return 0;
2319}
2320
4e9a86b6
VS
2321static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2322{
2323 if (INTEL_INFO(dev_priv)->gen >= 9)
2324 return 256 * 1024;
985b8bb4
VS
2325 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2326 IS_VALLEYVIEW(dev_priv))
4e9a86b6
VS
2327 return 128 * 1024;
2328 else if (INTEL_INFO(dev_priv)->gen >= 4)
2329 return 4 * 1024;
2330 else
44c5905e 2331 return 0;
4e9a86b6
VS
2332}
2333
127bd2ac 2334int
850c4cdc
TU
2335intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2336 struct drm_framebuffer *fb,
82bc3b2d 2337 const struct drm_plane_state *plane_state,
91af127f
JH
2338 struct intel_engine_cs *pipelined,
2339 struct drm_i915_gem_request **pipelined_request)
6b95a207 2340{
850c4cdc 2341 struct drm_device *dev = fb->dev;
ce453d81 2342 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2343 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2344 struct i915_ggtt_view view;
6b95a207
KH
2345 u32 alignment;
2346 int ret;
2347
ebcdd39e
MR
2348 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2349
7b911adc
TU
2350 switch (fb->modifier[0]) {
2351 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2352 alignment = intel_linear_alignment(dev_priv);
6b95a207 2353 break;
7b911adc 2354 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2355 if (INTEL_INFO(dev)->gen >= 9)
2356 alignment = 256 * 1024;
2357 else {
2358 /* pin() will align the object as required by fence */
2359 alignment = 0;
2360 }
6b95a207 2361 break;
7b911adc 2362 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2363 case I915_FORMAT_MOD_Yf_TILED:
2364 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2365 "Y tiling bo slipped through, driver bug!\n"))
2366 return -EINVAL;
2367 alignment = 1 * 1024 * 1024;
2368 break;
6b95a207 2369 default:
7b911adc
TU
2370 MISSING_CASE(fb->modifier[0]);
2371 return -EINVAL;
6b95a207
KH
2372 }
2373
f64b98cd
TU
2374 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2375 if (ret)
2376 return ret;
2377
693db184
CW
2378 /* Note that the w/a also requires 64 PTE of padding following the
2379 * bo. We currently fill all unused PTE with the shadow page and so
2380 * we should always have valid PTE following the scanout preventing
2381 * the VT-d warning.
2382 */
2383 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2384 alignment = 256 * 1024;
2385
d6dd6843
PZ
2386 /*
2387 * Global gtt pte registers are special registers which actually forward
2388 * writes to a chunk of system memory. Which means that there is no risk
2389 * that the register values disappear as soon as we call
2390 * intel_runtime_pm_put(), so it is correct to wrap only the
2391 * pin/unpin/fence and not more.
2392 */
2393 intel_runtime_pm_get(dev_priv);
2394
ce453d81 2395 dev_priv->mm.interruptible = false;
e6617330 2396 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
91af127f 2397 pipelined_request, &view);
48b956c5 2398 if (ret)
ce453d81 2399 goto err_interruptible;
6b95a207
KH
2400
2401 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2402 * fence, whereas 965+ only requires a fence if using
2403 * framebuffer compression. For simplicity, we always install
2404 * a fence as the cost is not that onerous.
2405 */
06d98131 2406 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2407 if (ret)
2408 goto err_unpin;
1690e1eb 2409
9a5a53b3 2410 i915_gem_object_pin_fence(obj);
6b95a207 2411
ce453d81 2412 dev_priv->mm.interruptible = true;
d6dd6843 2413 intel_runtime_pm_put(dev_priv);
6b95a207 2414 return 0;
48b956c5
CW
2415
2416err_unpin:
f64b98cd 2417 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2418err_interruptible:
2419 dev_priv->mm.interruptible = true;
d6dd6843 2420 intel_runtime_pm_put(dev_priv);
48b956c5 2421 return ret;
6b95a207
KH
2422}
2423
82bc3b2d
TU
2424static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2425 const struct drm_plane_state *plane_state)
1690e1eb 2426{
82bc3b2d 2427 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2428 struct i915_ggtt_view view;
2429 int ret;
82bc3b2d 2430
ebcdd39e
MR
2431 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2432
f64b98cd
TU
2433 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2434 WARN_ONCE(ret, "Couldn't get view from plane state!");
2435
1690e1eb 2436 i915_gem_object_unpin_fence(obj);
f64b98cd 2437 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2438}
2439
c2c75131
DV
2440/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2441 * is assumed to be a power-of-two. */
4e9a86b6
VS
2442unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2443 int *x, int *y,
bc752862
CW
2444 unsigned int tiling_mode,
2445 unsigned int cpp,
2446 unsigned int pitch)
c2c75131 2447{
bc752862
CW
2448 if (tiling_mode != I915_TILING_NONE) {
2449 unsigned int tile_rows, tiles;
c2c75131 2450
bc752862
CW
2451 tile_rows = *y / 8;
2452 *y %= 8;
c2c75131 2453
bc752862
CW
2454 tiles = *x / (512/cpp);
2455 *x %= 512/cpp;
2456
2457 return tile_rows * pitch * 8 + tiles * 4096;
2458 } else {
4e9a86b6 2459 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2460 unsigned int offset;
2461
2462 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2463 *y = (offset & alignment) / pitch;
2464 *x = ((offset & alignment) - *y * pitch) / cpp;
2465 return offset & ~alignment;
bc752862 2466 }
c2c75131
DV
2467}
2468
b35d63fa 2469static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2470{
2471 switch (format) {
2472 case DISPPLANE_8BPP:
2473 return DRM_FORMAT_C8;
2474 case DISPPLANE_BGRX555:
2475 return DRM_FORMAT_XRGB1555;
2476 case DISPPLANE_BGRX565:
2477 return DRM_FORMAT_RGB565;
2478 default:
2479 case DISPPLANE_BGRX888:
2480 return DRM_FORMAT_XRGB8888;
2481 case DISPPLANE_RGBX888:
2482 return DRM_FORMAT_XBGR8888;
2483 case DISPPLANE_BGRX101010:
2484 return DRM_FORMAT_XRGB2101010;
2485 case DISPPLANE_RGBX101010:
2486 return DRM_FORMAT_XBGR2101010;
2487 }
2488}
2489
bc8d7dff
DL
2490static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2491{
2492 switch (format) {
2493 case PLANE_CTL_FORMAT_RGB_565:
2494 return DRM_FORMAT_RGB565;
2495 default:
2496 case PLANE_CTL_FORMAT_XRGB_8888:
2497 if (rgb_order) {
2498 if (alpha)
2499 return DRM_FORMAT_ABGR8888;
2500 else
2501 return DRM_FORMAT_XBGR8888;
2502 } else {
2503 if (alpha)
2504 return DRM_FORMAT_ARGB8888;
2505 else
2506 return DRM_FORMAT_XRGB8888;
2507 }
2508 case PLANE_CTL_FORMAT_XRGB_2101010:
2509 if (rgb_order)
2510 return DRM_FORMAT_XBGR2101010;
2511 else
2512 return DRM_FORMAT_XRGB2101010;
2513 }
2514}
2515
5724dbd1 2516static bool
f6936e29
DV
2517intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2518 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2519{
2520 struct drm_device *dev = crtc->base.dev;
2521 struct drm_i915_gem_object *obj = NULL;
2522 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2523 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2524 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2525 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2526 PAGE_SIZE);
2527
2528 size_aligned -= base_aligned;
46f297fb 2529
ff2652ea
CW
2530 if (plane_config->size == 0)
2531 return false;
2532
f37b5c2b
DV
2533 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2534 base_aligned,
2535 base_aligned,
2536 size_aligned);
46f297fb 2537 if (!obj)
484b41dd 2538 return false;
46f297fb 2539
49af449b
DL
2540 obj->tiling_mode = plane_config->tiling;
2541 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2542 obj->stride = fb->pitches[0];
46f297fb 2543
6bf129df
DL
2544 mode_cmd.pixel_format = fb->pixel_format;
2545 mode_cmd.width = fb->width;
2546 mode_cmd.height = fb->height;
2547 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2548 mode_cmd.modifier[0] = fb->modifier[0];
2549 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2550
2551 mutex_lock(&dev->struct_mutex);
6bf129df 2552 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2553 &mode_cmd, obj)) {
46f297fb
JB
2554 DRM_DEBUG_KMS("intel fb init failed\n");
2555 goto out_unref_obj;
2556 }
46f297fb 2557 mutex_unlock(&dev->struct_mutex);
484b41dd 2558
f6936e29 2559 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2560 return true;
46f297fb
JB
2561
2562out_unref_obj:
2563 drm_gem_object_unreference(&obj->base);
2564 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2565 return false;
2566}
2567
afd65eb4
MR
2568/* Update plane->state->fb to match plane->fb after driver-internal updates */
2569static void
2570update_state_fb(struct drm_plane *plane)
2571{
2572 if (plane->fb == plane->state->fb)
2573 return;
2574
2575 if (plane->state->fb)
2576 drm_framebuffer_unreference(plane->state->fb);
2577 plane->state->fb = plane->fb;
2578 if (plane->state->fb)
2579 drm_framebuffer_reference(plane->state->fb);
2580}
2581
5724dbd1 2582static void
f6936e29
DV
2583intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2584 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2585{
2586 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2587 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2588 struct drm_crtc *c;
2589 struct intel_crtc *i;
2ff8fde1 2590 struct drm_i915_gem_object *obj;
88595ac9 2591 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2592 struct drm_plane_state *plane_state = primary->state;
88595ac9 2593 struct drm_framebuffer *fb;
484b41dd 2594
2d14030b 2595 if (!plane_config->fb)
484b41dd
JB
2596 return;
2597
f6936e29 2598 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2599 fb = &plane_config->fb->base;
2600 goto valid_fb;
f55548b5 2601 }
484b41dd 2602
2d14030b 2603 kfree(plane_config->fb);
484b41dd
JB
2604
2605 /*
2606 * Failed to alloc the obj, check to see if we should share
2607 * an fb with another CRTC instead
2608 */
70e1e0ec 2609 for_each_crtc(dev, c) {
484b41dd
JB
2610 i = to_intel_crtc(c);
2611
2612 if (c == &intel_crtc->base)
2613 continue;
2614
2ff8fde1
MR
2615 if (!i->active)
2616 continue;
2617
88595ac9
DV
2618 fb = c->primary->fb;
2619 if (!fb)
484b41dd
JB
2620 continue;
2621
88595ac9 2622 obj = intel_fb_obj(fb);
2ff8fde1 2623 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2624 drm_framebuffer_reference(fb);
2625 goto valid_fb;
484b41dd
JB
2626 }
2627 }
88595ac9
DV
2628
2629 return;
2630
2631valid_fb:
be5651f2
ML
2632 plane_state->src_x = plane_state->src_y = 0;
2633 plane_state->src_w = fb->width << 16;
2634 plane_state->src_h = fb->height << 16;
2635
2636 plane_state->crtc_x = plane_state->src_y = 0;
2637 plane_state->crtc_w = fb->width;
2638 plane_state->crtc_h = fb->height;
2639
88595ac9
DV
2640 obj = intel_fb_obj(fb);
2641 if (obj->tiling_mode != I915_TILING_NONE)
2642 dev_priv->preserve_bios_swizzle = true;
2643
be5651f2
ML
2644 drm_framebuffer_reference(fb);
2645 primary->fb = primary->state->fb = fb;
36750f28 2646 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2647 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2648 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2649}
2650
29b9bde6
DV
2651static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2652 struct drm_framebuffer *fb,
2653 int x, int y)
81255565
JB
2654{
2655 struct drm_device *dev = crtc->dev;
2656 struct drm_i915_private *dev_priv = dev->dev_private;
2657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2658 struct drm_plane *primary = crtc->primary;
2659 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2660 struct drm_i915_gem_object *obj;
81255565 2661 int plane = intel_crtc->plane;
e506a0c6 2662 unsigned long linear_offset;
81255565 2663 u32 dspcntr;
f45651ba 2664 u32 reg = DSPCNTR(plane);
48404c1e 2665 int pixel_size;
f45651ba 2666
b70709a6 2667 if (!visible || !fb) {
fdd508a6
VS
2668 I915_WRITE(reg, 0);
2669 if (INTEL_INFO(dev)->gen >= 4)
2670 I915_WRITE(DSPSURF(plane), 0);
2671 else
2672 I915_WRITE(DSPADDR(plane), 0);
2673 POSTING_READ(reg);
2674 return;
2675 }
2676
c9ba6fad
VS
2677 obj = intel_fb_obj(fb);
2678 if (WARN_ON(obj == NULL))
2679 return;
2680
2681 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2682
f45651ba
VS
2683 dspcntr = DISPPLANE_GAMMA_ENABLE;
2684
fdd508a6 2685 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2686
2687 if (INTEL_INFO(dev)->gen < 4) {
2688 if (intel_crtc->pipe == PIPE_B)
2689 dspcntr |= DISPPLANE_SEL_PIPE_B;
2690
2691 /* pipesrc and dspsize control the size that is scaled from,
2692 * which should always be the user's requested size.
2693 */
2694 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2695 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2696 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2697 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2698 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2699 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2700 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2701 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2702 I915_WRITE(PRIMPOS(plane), 0);
2703 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2704 }
81255565 2705
57779d06
VS
2706 switch (fb->pixel_format) {
2707 case DRM_FORMAT_C8:
81255565
JB
2708 dspcntr |= DISPPLANE_8BPP;
2709 break;
57779d06 2710 case DRM_FORMAT_XRGB1555:
57779d06 2711 dspcntr |= DISPPLANE_BGRX555;
81255565 2712 break;
57779d06
VS
2713 case DRM_FORMAT_RGB565:
2714 dspcntr |= DISPPLANE_BGRX565;
2715 break;
2716 case DRM_FORMAT_XRGB8888:
57779d06
VS
2717 dspcntr |= DISPPLANE_BGRX888;
2718 break;
2719 case DRM_FORMAT_XBGR8888:
57779d06
VS
2720 dspcntr |= DISPPLANE_RGBX888;
2721 break;
2722 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2723 dspcntr |= DISPPLANE_BGRX101010;
2724 break;
2725 case DRM_FORMAT_XBGR2101010:
57779d06 2726 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2727 break;
2728 default:
baba133a 2729 BUG();
81255565 2730 }
57779d06 2731
f45651ba
VS
2732 if (INTEL_INFO(dev)->gen >= 4 &&
2733 obj->tiling_mode != I915_TILING_NONE)
2734 dspcntr |= DISPPLANE_TILED;
81255565 2735
de1aa629
VS
2736 if (IS_G4X(dev))
2737 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2738
b9897127 2739 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2740
c2c75131
DV
2741 if (INTEL_INFO(dev)->gen >= 4) {
2742 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2743 intel_gen4_compute_page_offset(dev_priv,
2744 &x, &y, obj->tiling_mode,
b9897127 2745 pixel_size,
bc752862 2746 fb->pitches[0]);
c2c75131
DV
2747 linear_offset -= intel_crtc->dspaddr_offset;
2748 } else {
e506a0c6 2749 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2750 }
e506a0c6 2751
8e7d688b 2752 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2753 dspcntr |= DISPPLANE_ROTATE_180;
2754
6e3c9717
ACO
2755 x += (intel_crtc->config->pipe_src_w - 1);
2756 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2757
2758 /* Finding the last pixel of the last line of the display
2759 data and adding to linear_offset*/
2760 linear_offset +=
6e3c9717
ACO
2761 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2762 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2763 }
2764
2765 I915_WRITE(reg, dspcntr);
2766
01f2c773 2767 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2768 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2769 I915_WRITE(DSPSURF(plane),
2770 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2771 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2772 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2773 } else
f343c5f6 2774 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2775 POSTING_READ(reg);
17638cd6
JB
2776}
2777
29b9bde6
DV
2778static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2779 struct drm_framebuffer *fb,
2780 int x, int y)
17638cd6
JB
2781{
2782 struct drm_device *dev = crtc->dev;
2783 struct drm_i915_private *dev_priv = dev->dev_private;
2784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2785 struct drm_plane *primary = crtc->primary;
2786 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2787 struct drm_i915_gem_object *obj;
17638cd6 2788 int plane = intel_crtc->plane;
e506a0c6 2789 unsigned long linear_offset;
17638cd6 2790 u32 dspcntr;
f45651ba 2791 u32 reg = DSPCNTR(plane);
48404c1e 2792 int pixel_size;
f45651ba 2793
b70709a6 2794 if (!visible || !fb) {
fdd508a6
VS
2795 I915_WRITE(reg, 0);
2796 I915_WRITE(DSPSURF(plane), 0);
2797 POSTING_READ(reg);
2798 return;
2799 }
2800
c9ba6fad
VS
2801 obj = intel_fb_obj(fb);
2802 if (WARN_ON(obj == NULL))
2803 return;
2804
2805 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2806
f45651ba
VS
2807 dspcntr = DISPPLANE_GAMMA_ENABLE;
2808
fdd508a6 2809 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2813
57779d06
VS
2814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
17638cd6
JB
2816 dspcntr |= DISPPLANE_8BPP;
2817 break;
57779d06
VS
2818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2820 break;
57779d06 2821 case DRM_FORMAT_XRGB8888:
57779d06
VS
2822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
57779d06
VS
2825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
57779d06 2831 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2832 break;
2833 default:
baba133a 2834 BUG();
17638cd6
JB
2835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
17638cd6 2839
f45651ba 2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2842
b9897127 2843 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2844 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2845 intel_gen4_compute_page_offset(dev_priv,
2846 &x, &y, obj->tiling_mode,
b9897127 2847 pixel_size,
bc752862 2848 fb->pitches[0]);
c2c75131 2849 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2850 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2851 dspcntr |= DISPPLANE_ROTATE_180;
2852
2853 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2854 x += (intel_crtc->config->pipe_src_w - 1);
2855 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2856
2857 /* Finding the last pixel of the last line of the display
2858 data and adding to linear_offset*/
2859 linear_offset +=
6e3c9717
ACO
2860 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2861 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2862 }
2863 }
2864
2865 I915_WRITE(reg, dspcntr);
17638cd6 2866
01f2c773 2867 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2868 I915_WRITE(DSPSURF(plane),
2869 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2870 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2871 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2872 } else {
2873 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2874 I915_WRITE(DSPLINOFF(plane), linear_offset);
2875 }
17638cd6 2876 POSTING_READ(reg);
17638cd6
JB
2877}
2878
b321803d
DL
2879u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2880 uint32_t pixel_format)
2881{
2882 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2883
2884 /*
2885 * The stride is either expressed as a multiple of 64 bytes
2886 * chunks for linear buffers or in number of tiles for tiled
2887 * buffers.
2888 */
2889 switch (fb_modifier) {
2890 case DRM_FORMAT_MOD_NONE:
2891 return 64;
2892 case I915_FORMAT_MOD_X_TILED:
2893 if (INTEL_INFO(dev)->gen == 2)
2894 return 128;
2895 return 512;
2896 case I915_FORMAT_MOD_Y_TILED:
2897 /* No need to check for old gens and Y tiling since this is
2898 * about the display engine and those will be blocked before
2899 * we get here.
2900 */
2901 return 128;
2902 case I915_FORMAT_MOD_Yf_TILED:
2903 if (bits_per_pixel == 8)
2904 return 64;
2905 else
2906 return 128;
2907 default:
2908 MISSING_CASE(fb_modifier);
2909 return 64;
2910 }
2911}
2912
121920fa
TU
2913unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2914 struct drm_i915_gem_object *obj)
2915{
9abc4648 2916 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2917
2918 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2919 view = &i915_ggtt_view_rotated;
121920fa
TU
2920
2921 return i915_gem_obj_ggtt_offset_view(obj, view);
2922}
2923
e435d6e5
ML
2924static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2925{
2926 struct drm_device *dev = intel_crtc->base.dev;
2927 struct drm_i915_private *dev_priv = dev->dev_private;
2928
2929 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2930 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2931 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
2932 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2933 intel_crtc->base.base.id, intel_crtc->pipe, id);
2934}
2935
a1b2278e
CK
2936/*
2937 * This function detaches (aka. unbinds) unused scalers in hardware
2938 */
0583236e 2939static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2940{
a1b2278e
CK
2941 struct intel_crtc_scaler_state *scaler_state;
2942 int i;
2943
a1b2278e
CK
2944 scaler_state = &intel_crtc->config->scaler_state;
2945
2946 /* loop through and disable scalers that aren't in use */
2947 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2948 if (!scaler_state->scalers[i].in_use)
2949 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2950 }
2951}
2952
6156a456 2953u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2954{
6156a456 2955 switch (pixel_format) {
d161cf7a 2956 case DRM_FORMAT_C8:
c34ce3d1 2957 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2958 case DRM_FORMAT_RGB565:
c34ce3d1 2959 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2960 case DRM_FORMAT_XBGR8888:
c34ce3d1 2961 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2962 case DRM_FORMAT_XRGB8888:
c34ce3d1 2963 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2964 /*
2965 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2966 * to be already pre-multiplied. We need to add a knob (or a different
2967 * DRM_FORMAT) for user-space to configure that.
2968 */
f75fb42a 2969 case DRM_FORMAT_ABGR8888:
c34ce3d1 2970 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2971 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2972 case DRM_FORMAT_ARGB8888:
c34ce3d1 2973 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2974 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2975 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2976 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2977 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2978 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2979 case DRM_FORMAT_YUYV:
c34ce3d1 2980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2981 case DRM_FORMAT_YVYU:
c34ce3d1 2982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2983 case DRM_FORMAT_UYVY:
c34ce3d1 2984 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2985 case DRM_FORMAT_VYUY:
c34ce3d1 2986 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2987 default:
4249eeef 2988 MISSING_CASE(pixel_format);
70d21f0e 2989 }
8cfcba41 2990
c34ce3d1 2991 return 0;
6156a456 2992}
70d21f0e 2993
6156a456
CK
2994u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2995{
6156a456 2996 switch (fb_modifier) {
30af77c4 2997 case DRM_FORMAT_MOD_NONE:
70d21f0e 2998 break;
30af77c4 2999 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3000 return PLANE_CTL_TILED_X;
b321803d 3001 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3002 return PLANE_CTL_TILED_Y;
b321803d 3003 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3004 return PLANE_CTL_TILED_YF;
70d21f0e 3005 default:
6156a456 3006 MISSING_CASE(fb_modifier);
70d21f0e 3007 }
8cfcba41 3008
c34ce3d1 3009 return 0;
6156a456 3010}
70d21f0e 3011
6156a456
CK
3012u32 skl_plane_ctl_rotation(unsigned int rotation)
3013{
3b7a5119 3014 switch (rotation) {
6156a456
CK
3015 case BIT(DRM_ROTATE_0):
3016 break;
1e8df167
SJ
3017 /*
3018 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3019 * while i915 HW rotation is clockwise, thats why this swapping.
3020 */
3b7a5119 3021 case BIT(DRM_ROTATE_90):
1e8df167 3022 return PLANE_CTL_ROTATE_270;
3b7a5119 3023 case BIT(DRM_ROTATE_180):
c34ce3d1 3024 return PLANE_CTL_ROTATE_180;
3b7a5119 3025 case BIT(DRM_ROTATE_270):
1e8df167 3026 return PLANE_CTL_ROTATE_90;
6156a456
CK
3027 default:
3028 MISSING_CASE(rotation);
3029 }
3030
c34ce3d1 3031 return 0;
6156a456
CK
3032}
3033
3034static void skylake_update_primary_plane(struct drm_crtc *crtc,
3035 struct drm_framebuffer *fb,
3036 int x, int y)
3037{
3038 struct drm_device *dev = crtc->dev;
3039 struct drm_i915_private *dev_priv = dev->dev_private;
3040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3041 struct drm_plane *plane = crtc->primary;
3042 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3043 struct drm_i915_gem_object *obj;
3044 int pipe = intel_crtc->pipe;
3045 u32 plane_ctl, stride_div, stride;
3046 u32 tile_height, plane_offset, plane_size;
3047 unsigned int rotation;
3048 int x_offset, y_offset;
3049 unsigned long surf_addr;
6156a456
CK
3050 struct intel_crtc_state *crtc_state = intel_crtc->config;
3051 struct intel_plane_state *plane_state;
3052 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3053 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3054 int scaler_id = -1;
3055
6156a456
CK
3056 plane_state = to_intel_plane_state(plane->state);
3057
b70709a6 3058 if (!visible || !fb) {
6156a456
CK
3059 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3060 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3061 POSTING_READ(PLANE_CTL(pipe, 0));
3062 return;
3b7a5119 3063 }
70d21f0e 3064
6156a456
CK
3065 plane_ctl = PLANE_CTL_ENABLE |
3066 PLANE_CTL_PIPE_GAMMA_ENABLE |
3067 PLANE_CTL_PIPE_CSC_ENABLE;
3068
3069 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3070 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3071 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3072
3073 rotation = plane->state->rotation;
3074 plane_ctl |= skl_plane_ctl_rotation(rotation);
3075
b321803d
DL
3076 obj = intel_fb_obj(fb);
3077 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3078 fb->pixel_format);
3b7a5119
SJ
3079 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3080
6156a456
CK
3081 /*
3082 * FIXME: intel_plane_state->src, dst aren't set when transitional
3083 * update_plane helpers are called from legacy paths.
3084 * Once full atomic crtc is available, below check can be avoided.
3085 */
3086 if (drm_rect_width(&plane_state->src)) {
3087 scaler_id = plane_state->scaler_id;
3088 src_x = plane_state->src.x1 >> 16;
3089 src_y = plane_state->src.y1 >> 16;
3090 src_w = drm_rect_width(&plane_state->src) >> 16;
3091 src_h = drm_rect_height(&plane_state->src) >> 16;
3092 dst_x = plane_state->dst.x1;
3093 dst_y = plane_state->dst.y1;
3094 dst_w = drm_rect_width(&plane_state->dst);
3095 dst_h = drm_rect_height(&plane_state->dst);
3096
3097 WARN_ON(x != src_x || y != src_y);
3098 } else {
3099 src_w = intel_crtc->config->pipe_src_w;
3100 src_h = intel_crtc->config->pipe_src_h;
3101 }
3102
3b7a5119
SJ
3103 if (intel_rotation_90_or_270(rotation)) {
3104 /* stride = Surface height in tiles */
2614f17d 3105 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3106 fb->modifier[0]);
3107 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3108 x_offset = stride * tile_height - y - src_h;
3b7a5119 3109 y_offset = x;
6156a456 3110 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3111 } else {
3112 stride = fb->pitches[0] / stride_div;
3113 x_offset = x;
3114 y_offset = y;
6156a456 3115 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3116 }
3117 plane_offset = y_offset << 16 | x_offset;
b321803d 3118
70d21f0e 3119 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3120 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3121 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3122 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3123
3124 if (scaler_id >= 0) {
3125 uint32_t ps_ctrl = 0;
3126
3127 WARN_ON(!dst_w || !dst_h);
3128 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3129 crtc_state->scaler_state.scalers[scaler_id].mode;
3130 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3131 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3132 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3133 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3134 I915_WRITE(PLANE_POS(pipe, 0), 0);
3135 } else {
3136 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3137 }
3138
121920fa 3139 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3140
3141 POSTING_READ(PLANE_SURF(pipe, 0));
3142}
3143
17638cd6
JB
3144/* Assume fb object is pinned & idle & fenced and just update base pointers */
3145static int
3146intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3147 int x, int y, enum mode_set_atomic state)
3148{
3149 struct drm_device *dev = crtc->dev;
3150 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3151
ff2a3117 3152 if (dev_priv->fbc.disable_fbc)
7733b49b 3153 dev_priv->fbc.disable_fbc(dev_priv);
81255565 3154
29b9bde6
DV
3155 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3156
3157 return 0;
81255565
JB
3158}
3159
7514747d 3160static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3161{
96a02917
VS
3162 struct drm_crtc *crtc;
3163
70e1e0ec 3164 for_each_crtc(dev, crtc) {
96a02917
VS
3165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3166 enum plane plane = intel_crtc->plane;
3167
3168 intel_prepare_page_flip(dev, plane);
3169 intel_finish_page_flip_plane(dev, plane);
3170 }
7514747d
VS
3171}
3172
3173static void intel_update_primary_planes(struct drm_device *dev)
3174{
3175 struct drm_i915_private *dev_priv = dev->dev_private;
3176 struct drm_crtc *crtc;
96a02917 3177
70e1e0ec 3178 for_each_crtc(dev, crtc) {
96a02917
VS
3179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3180
51fd371b 3181 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3182 /*
3183 * FIXME: Once we have proper support for primary planes (and
3184 * disabling them without disabling the entire crtc) allow again
66e514c1 3185 * a NULL crtc->primary->fb.
947fdaad 3186 */
f4510a27 3187 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3188 dev_priv->display.update_primary_plane(crtc,
66e514c1 3189 crtc->primary->fb,
262ca2b0
MR
3190 crtc->x,
3191 crtc->y);
51fd371b 3192 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3193 }
3194}
3195
7514747d
VS
3196void intel_prepare_reset(struct drm_device *dev)
3197{
3198 /* no reset support for gen2 */
3199 if (IS_GEN2(dev))
3200 return;
3201
3202 /* reset doesn't touch the display */
3203 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3204 return;
3205
3206 drm_modeset_lock_all(dev);
f98ce92f
VS
3207 /*
3208 * Disabling the crtcs gracefully seems nicer. Also the
3209 * g33 docs say we should at least disable all the planes.
3210 */
6b72d486 3211 intel_display_suspend(dev);
7514747d
VS
3212}
3213
3214void intel_finish_reset(struct drm_device *dev)
3215{
3216 struct drm_i915_private *dev_priv = to_i915(dev);
3217
3218 /*
3219 * Flips in the rings will be nuked by the reset,
3220 * so complete all pending flips so that user space
3221 * will get its events and not get stuck.
3222 */
3223 intel_complete_page_flips(dev);
3224
3225 /* no reset support for gen2 */
3226 if (IS_GEN2(dev))
3227 return;
3228
3229 /* reset doesn't touch the display */
3230 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3231 /*
3232 * Flips in the rings have been nuked by the reset,
3233 * so update the base address of all primary
3234 * planes to the the last fb to make sure we're
3235 * showing the correct fb after a reset.
3236 */
3237 intel_update_primary_planes(dev);
3238 return;
3239 }
3240
3241 /*
3242 * The display has been reset as well,
3243 * so need a full re-initialization.
3244 */
3245 intel_runtime_pm_disable_interrupts(dev_priv);
3246 intel_runtime_pm_enable_interrupts(dev_priv);
3247
3248 intel_modeset_init_hw(dev);
3249
3250 spin_lock_irq(&dev_priv->irq_lock);
3251 if (dev_priv->display.hpd_irq_setup)
3252 dev_priv->display.hpd_irq_setup(dev);
3253 spin_unlock_irq(&dev_priv->irq_lock);
3254
043e9bda 3255 intel_display_resume(dev);
7514747d
VS
3256
3257 intel_hpd_init(dev_priv);
3258
3259 drm_modeset_unlock_all(dev);
3260}
3261
2e2f351d 3262static void
14667a4b
CW
3263intel_finish_fb(struct drm_framebuffer *old_fb)
3264{
2ff8fde1 3265 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3266 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3267 bool was_interruptible = dev_priv->mm.interruptible;
3268 int ret;
3269
14667a4b
CW
3270 /* Big Hammer, we also need to ensure that any pending
3271 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3272 * current scanout is retired before unpinning the old
2e2f351d
CW
3273 * framebuffer. Note that we rely on userspace rendering
3274 * into the buffer attached to the pipe they are waiting
3275 * on. If not, userspace generates a GPU hang with IPEHR
3276 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3277 *
3278 * This should only fail upon a hung GPU, in which case we
3279 * can safely continue.
3280 */
3281 dev_priv->mm.interruptible = false;
2e2f351d 3282 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3283 dev_priv->mm.interruptible = was_interruptible;
3284
2e2f351d 3285 WARN_ON(ret);
14667a4b
CW
3286}
3287
7d5e3799
CW
3288static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3289{
3290 struct drm_device *dev = crtc->dev;
3291 struct drm_i915_private *dev_priv = dev->dev_private;
3292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3293 bool pending;
3294
3295 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3296 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3297 return false;
3298
5e2d7afc 3299 spin_lock_irq(&dev->event_lock);
7d5e3799 3300 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3301 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3302
3303 return pending;
3304}
3305
e30e8f75
GP
3306static void intel_update_pipe_size(struct intel_crtc *crtc)
3307{
3308 struct drm_device *dev = crtc->base.dev;
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 const struct drm_display_mode *adjusted_mode;
3311
3312 if (!i915.fastboot)
3313 return;
3314
3315 /*
3316 * Update pipe size and adjust fitter if needed: the reason for this is
3317 * that in compute_mode_changes we check the native mode (not the pfit
3318 * mode) to see if we can flip rather than do a full mode set. In the
3319 * fastboot case, we'll flip, but if we don't update the pipesrc and
3320 * pfit state, we'll end up with a big fb scanned out into the wrong
3321 * sized surface.
3322 *
3323 * To fix this properly, we need to hoist the checks up into
3324 * compute_mode_changes (or above), check the actual pfit state and
3325 * whether the platform allows pfit disable with pipe active, and only
3326 * then update the pipesrc and pfit state, even on the flip path.
3327 */
3328
6e3c9717 3329 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3330
3331 I915_WRITE(PIPESRC(crtc->pipe),
3332 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3333 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3334 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3335 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3336 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3337 I915_WRITE(PF_CTL(crtc->pipe), 0);
3338 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3339 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3340 }
6e3c9717
ACO
3341 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3342 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3343}
3344
5e84e1a4
ZW
3345static void intel_fdi_normal_train(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3350 int pipe = intel_crtc->pipe;
3351 u32 reg, temp;
3352
3353 /* enable normal train */
3354 reg = FDI_TX_CTL(pipe);
3355 temp = I915_READ(reg);
61e499bf 3356 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3357 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3358 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3359 } else {
3360 temp &= ~FDI_LINK_TRAIN_NONE;
3361 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3362 }
5e84e1a4
ZW
3363 I915_WRITE(reg, temp);
3364
3365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
3367 if (HAS_PCH_CPT(dev)) {
3368 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3369 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3370 } else {
3371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_NONE;
3373 }
3374 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3375
3376 /* wait one idle pattern time */
3377 POSTING_READ(reg);
3378 udelay(1000);
357555c0
JB
3379
3380 /* IVB wants error correction enabled */
3381 if (IS_IVYBRIDGE(dev))
3382 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3383 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3384}
3385
8db9d77b
ZW
3386/* The FDI link training functions for ILK/Ibexpeak. */
3387static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3388{
3389 struct drm_device *dev = crtc->dev;
3390 struct drm_i915_private *dev_priv = dev->dev_private;
3391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3392 int pipe = intel_crtc->pipe;
5eddb70b 3393 u32 reg, temp, tries;
8db9d77b 3394
1c8562f6 3395 /* FDI needs bits from pipe first */
0fc932b8 3396 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3397
e1a44743
AJ
3398 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3399 for train result */
5eddb70b
CW
3400 reg = FDI_RX_IMR(pipe);
3401 temp = I915_READ(reg);
e1a44743
AJ
3402 temp &= ~FDI_RX_SYMBOL_LOCK;
3403 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3404 I915_WRITE(reg, temp);
3405 I915_READ(reg);
e1a44743
AJ
3406 udelay(150);
3407
8db9d77b 3408 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3409 reg = FDI_TX_CTL(pipe);
3410 temp = I915_READ(reg);
627eb5a3 3411 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3412 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3413 temp &= ~FDI_LINK_TRAIN_NONE;
3414 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3415 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3416
5eddb70b
CW
3417 reg = FDI_RX_CTL(pipe);
3418 temp = I915_READ(reg);
8db9d77b
ZW
3419 temp &= ~FDI_LINK_TRAIN_NONE;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3421 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3422
3423 POSTING_READ(reg);
8db9d77b
ZW
3424 udelay(150);
3425
5b2adf89 3426 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3427 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3428 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3429 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3430
5eddb70b 3431 reg = FDI_RX_IIR(pipe);
e1a44743 3432 for (tries = 0; tries < 5; tries++) {
5eddb70b 3433 temp = I915_READ(reg);
8db9d77b
ZW
3434 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3435
3436 if ((temp & FDI_RX_BIT_LOCK)) {
3437 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3438 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3439 break;
3440 }
8db9d77b 3441 }
e1a44743 3442 if (tries == 5)
5eddb70b 3443 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3444
3445 /* Train 2 */
5eddb70b
CW
3446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
8db9d77b
ZW
3448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3450 I915_WRITE(reg, temp);
8db9d77b 3451
5eddb70b
CW
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
8db9d77b
ZW
3454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3456 I915_WRITE(reg, temp);
8db9d77b 3457
5eddb70b
CW
3458 POSTING_READ(reg);
3459 udelay(150);
8db9d77b 3460
5eddb70b 3461 reg = FDI_RX_IIR(pipe);
e1a44743 3462 for (tries = 0; tries < 5; tries++) {
5eddb70b 3463 temp = I915_READ(reg);
8db9d77b
ZW
3464 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3465
3466 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3467 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3468 DRM_DEBUG_KMS("FDI train 2 done.\n");
3469 break;
3470 }
8db9d77b 3471 }
e1a44743 3472 if (tries == 5)
5eddb70b 3473 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3474
3475 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3476
8db9d77b
ZW
3477}
3478
0206e353 3479static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3480 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3481 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3482 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3483 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3484};
3485
3486/* The FDI link training functions for SNB/Cougarpoint. */
3487static void gen6_fdi_link_train(struct drm_crtc *crtc)
3488{
3489 struct drm_device *dev = crtc->dev;
3490 struct drm_i915_private *dev_priv = dev->dev_private;
3491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3492 int pipe = intel_crtc->pipe;
fa37d39e 3493 u32 reg, temp, i, retry;
8db9d77b 3494
e1a44743
AJ
3495 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3496 for train result */
5eddb70b
CW
3497 reg = FDI_RX_IMR(pipe);
3498 temp = I915_READ(reg);
e1a44743
AJ
3499 temp &= ~FDI_RX_SYMBOL_LOCK;
3500 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3501 I915_WRITE(reg, temp);
3502
3503 POSTING_READ(reg);
e1a44743
AJ
3504 udelay(150);
3505
8db9d77b 3506 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
627eb5a3 3509 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3510 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1;
3513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 /* SNB-B */
3515 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3516 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3517
d74cf324
DV
3518 I915_WRITE(FDI_RX_MISC(pipe),
3519 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3520
5eddb70b
CW
3521 reg = FDI_RX_CTL(pipe);
3522 temp = I915_READ(reg);
8db9d77b
ZW
3523 if (HAS_PCH_CPT(dev)) {
3524 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3525 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3526 } else {
3527 temp &= ~FDI_LINK_TRAIN_NONE;
3528 temp |= FDI_LINK_TRAIN_PATTERN_1;
3529 }
5eddb70b
CW
3530 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3531
3532 POSTING_READ(reg);
8db9d77b
ZW
3533 udelay(150);
3534
0206e353 3535 for (i = 0; i < 4; i++) {
5eddb70b
CW
3536 reg = FDI_TX_CTL(pipe);
3537 temp = I915_READ(reg);
8db9d77b
ZW
3538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3539 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
8db9d77b
ZW
3543 udelay(500);
3544
fa37d39e
SP
3545 for (retry = 0; retry < 5; retry++) {
3546 reg = FDI_RX_IIR(pipe);
3547 temp = I915_READ(reg);
3548 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3549 if (temp & FDI_RX_BIT_LOCK) {
3550 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3551 DRM_DEBUG_KMS("FDI train 1 done.\n");
3552 break;
3553 }
3554 udelay(50);
8db9d77b 3555 }
fa37d39e
SP
3556 if (retry < 5)
3557 break;
8db9d77b
ZW
3558 }
3559 if (i == 4)
5eddb70b 3560 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3561
3562 /* Train 2 */
5eddb70b
CW
3563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
8db9d77b
ZW
3565 temp &= ~FDI_LINK_TRAIN_NONE;
3566 temp |= FDI_LINK_TRAIN_PATTERN_2;
3567 if (IS_GEN6(dev)) {
3568 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3569 /* SNB-B */
3570 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3571 }
5eddb70b 3572 I915_WRITE(reg, temp);
8db9d77b 3573
5eddb70b
CW
3574 reg = FDI_RX_CTL(pipe);
3575 temp = I915_READ(reg);
8db9d77b
ZW
3576 if (HAS_PCH_CPT(dev)) {
3577 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3578 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3579 } else {
3580 temp &= ~FDI_LINK_TRAIN_NONE;
3581 temp |= FDI_LINK_TRAIN_PATTERN_2;
3582 }
5eddb70b
CW
3583 I915_WRITE(reg, temp);
3584
3585 POSTING_READ(reg);
8db9d77b
ZW
3586 udelay(150);
3587
0206e353 3588 for (i = 0; i < 4; i++) {
5eddb70b
CW
3589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
8db9d77b
ZW
3591 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3592 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3593 I915_WRITE(reg, temp);
3594
3595 POSTING_READ(reg);
8db9d77b
ZW
3596 udelay(500);
3597
fa37d39e
SP
3598 for (retry = 0; retry < 5; retry++) {
3599 reg = FDI_RX_IIR(pipe);
3600 temp = I915_READ(reg);
3601 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3602 if (temp & FDI_RX_SYMBOL_LOCK) {
3603 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3604 DRM_DEBUG_KMS("FDI train 2 done.\n");
3605 break;
3606 }
3607 udelay(50);
8db9d77b 3608 }
fa37d39e
SP
3609 if (retry < 5)
3610 break;
8db9d77b
ZW
3611 }
3612 if (i == 4)
5eddb70b 3613 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3614
3615 DRM_DEBUG_KMS("FDI train done.\n");
3616}
3617
357555c0
JB
3618/* Manual link training for Ivy Bridge A0 parts */
3619static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3624 int pipe = intel_crtc->pipe;
139ccd3f 3625 u32 reg, temp, i, j;
357555c0
JB
3626
3627 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3628 for train result */
3629 reg = FDI_RX_IMR(pipe);
3630 temp = I915_READ(reg);
3631 temp &= ~FDI_RX_SYMBOL_LOCK;
3632 temp &= ~FDI_RX_BIT_LOCK;
3633 I915_WRITE(reg, temp);
3634
3635 POSTING_READ(reg);
3636 udelay(150);
3637
01a415fd
DV
3638 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3639 I915_READ(FDI_RX_IIR(pipe)));
3640
139ccd3f
JB
3641 /* Try each vswing and preemphasis setting twice before moving on */
3642 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3643 /* disable first in case we need to retry */
3644 reg = FDI_TX_CTL(pipe);
3645 temp = I915_READ(reg);
3646 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3647 temp &= ~FDI_TX_ENABLE;
3648 I915_WRITE(reg, temp);
357555c0 3649
139ccd3f
JB
3650 reg = FDI_RX_CTL(pipe);
3651 temp = I915_READ(reg);
3652 temp &= ~FDI_LINK_TRAIN_AUTO;
3653 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3654 temp &= ~FDI_RX_ENABLE;
3655 I915_WRITE(reg, temp);
357555c0 3656
139ccd3f 3657 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3658 reg = FDI_TX_CTL(pipe);
3659 temp = I915_READ(reg);
139ccd3f 3660 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3661 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3662 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3663 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3664 temp |= snb_b_fdi_train_param[j/2];
3665 temp |= FDI_COMPOSITE_SYNC;
3666 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3667
139ccd3f
JB
3668 I915_WRITE(FDI_RX_MISC(pipe),
3669 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3670
139ccd3f 3671 reg = FDI_RX_CTL(pipe);
357555c0 3672 temp = I915_READ(reg);
139ccd3f
JB
3673 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3674 temp |= FDI_COMPOSITE_SYNC;
3675 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3676
139ccd3f
JB
3677 POSTING_READ(reg);
3678 udelay(1); /* should be 0.5us */
357555c0 3679
139ccd3f
JB
3680 for (i = 0; i < 4; i++) {
3681 reg = FDI_RX_IIR(pipe);
3682 temp = I915_READ(reg);
3683 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3684
139ccd3f
JB
3685 if (temp & FDI_RX_BIT_LOCK ||
3686 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3687 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3688 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3689 i);
3690 break;
3691 }
3692 udelay(1); /* should be 0.5us */
3693 }
3694 if (i == 4) {
3695 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3696 continue;
3697 }
357555c0 3698
139ccd3f 3699 /* Train 2 */
357555c0
JB
3700 reg = FDI_TX_CTL(pipe);
3701 temp = I915_READ(reg);
139ccd3f
JB
3702 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3703 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3704 I915_WRITE(reg, temp);
3705
3706 reg = FDI_RX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3709 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3710 I915_WRITE(reg, temp);
3711
3712 POSTING_READ(reg);
139ccd3f 3713 udelay(2); /* should be 1.5us */
357555c0 3714
139ccd3f
JB
3715 for (i = 0; i < 4; i++) {
3716 reg = FDI_RX_IIR(pipe);
3717 temp = I915_READ(reg);
3718 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3719
139ccd3f
JB
3720 if (temp & FDI_RX_SYMBOL_LOCK ||
3721 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3722 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3723 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3724 i);
3725 goto train_done;
3726 }
3727 udelay(2); /* should be 1.5us */
357555c0 3728 }
139ccd3f
JB
3729 if (i == 4)
3730 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3731 }
357555c0 3732
139ccd3f 3733train_done:
357555c0
JB
3734 DRM_DEBUG_KMS("FDI train done.\n");
3735}
3736
88cefb6c 3737static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3738{
88cefb6c 3739 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3740 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3741 int pipe = intel_crtc->pipe;
5eddb70b 3742 u32 reg, temp;
79e53945 3743
c64e311e 3744
c98e9dcf 3745 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3746 reg = FDI_RX_CTL(pipe);
3747 temp = I915_READ(reg);
627eb5a3 3748 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3749 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3750 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3751 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
c98e9dcf
JB
3754 udelay(200);
3755
3756 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp | FDI_PCDCLK);
3759
3760 POSTING_READ(reg);
c98e9dcf
JB
3761 udelay(200);
3762
20749730
PZ
3763 /* Enable CPU FDI TX PLL, always on for Ironlake */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3767 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3768
20749730
PZ
3769 POSTING_READ(reg);
3770 udelay(100);
6be4a607 3771 }
0e23b99d
JB
3772}
3773
88cefb6c
DV
3774static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3775{
3776 struct drm_device *dev = intel_crtc->base.dev;
3777 struct drm_i915_private *dev_priv = dev->dev_private;
3778 int pipe = intel_crtc->pipe;
3779 u32 reg, temp;
3780
3781 /* Switch from PCDclk to Rawclk */
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3785
3786 /* Disable CPU FDI TX PLL */
3787 reg = FDI_TX_CTL(pipe);
3788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3790
3791 POSTING_READ(reg);
3792 udelay(100);
3793
3794 reg = FDI_RX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3797
3798 /* Wait for the clocks to turn off. */
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
0fc932b8
JB
3803static void ironlake_fdi_disable(struct drm_crtc *crtc)
3804{
3805 struct drm_device *dev = crtc->dev;
3806 struct drm_i915_private *dev_priv = dev->dev_private;
3807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3808 int pipe = intel_crtc->pipe;
3809 u32 reg, temp;
3810
3811 /* disable CPU FDI tx and PCH FDI rx */
3812 reg = FDI_TX_CTL(pipe);
3813 temp = I915_READ(reg);
3814 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3815 POSTING_READ(reg);
3816
3817 reg = FDI_RX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 temp &= ~(0x7 << 16);
dfd07d72 3820 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3821 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3822
3823 POSTING_READ(reg);
3824 udelay(100);
3825
3826 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3827 if (HAS_PCH_IBX(dev))
6f06ce18 3828 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3829
3830 /* still set train pattern 1 */
3831 reg = FDI_TX_CTL(pipe);
3832 temp = I915_READ(reg);
3833 temp &= ~FDI_LINK_TRAIN_NONE;
3834 temp |= FDI_LINK_TRAIN_PATTERN_1;
3835 I915_WRITE(reg, temp);
3836
3837 reg = FDI_RX_CTL(pipe);
3838 temp = I915_READ(reg);
3839 if (HAS_PCH_CPT(dev)) {
3840 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3841 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3842 } else {
3843 temp &= ~FDI_LINK_TRAIN_NONE;
3844 temp |= FDI_LINK_TRAIN_PATTERN_1;
3845 }
3846 /* BPC in FDI rx is consistent with that in PIPECONF */
3847 temp &= ~(0x07 << 16);
dfd07d72 3848 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3849 I915_WRITE(reg, temp);
3850
3851 POSTING_READ(reg);
3852 udelay(100);
3853}
3854
5dce5b93
CW
3855bool intel_has_pending_fb_unpin(struct drm_device *dev)
3856{
3857 struct intel_crtc *crtc;
3858
3859 /* Note that we don't need to be called with mode_config.lock here
3860 * as our list of CRTC objects is static for the lifetime of the
3861 * device and so cannot disappear as we iterate. Similarly, we can
3862 * happily treat the predicates as racy, atomic checks as userspace
3863 * cannot claim and pin a new fb without at least acquring the
3864 * struct_mutex and so serialising with us.
3865 */
d3fcc808 3866 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3867 if (atomic_read(&crtc->unpin_work_count) == 0)
3868 continue;
3869
3870 if (crtc->unpin_work)
3871 intel_wait_for_vblank(dev, crtc->pipe);
3872
3873 return true;
3874 }
3875
3876 return false;
3877}
3878
d6bbafa1
CW
3879static void page_flip_completed(struct intel_crtc *intel_crtc)
3880{
3881 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3882 struct intel_unpin_work *work = intel_crtc->unpin_work;
3883
3884 /* ensure that the unpin work is consistent wrt ->pending. */
3885 smp_rmb();
3886 intel_crtc->unpin_work = NULL;
3887
3888 if (work->event)
3889 drm_send_vblank_event(intel_crtc->base.dev,
3890 intel_crtc->pipe,
3891 work->event);
3892
3893 drm_crtc_vblank_put(&intel_crtc->base);
3894
3895 wake_up_all(&dev_priv->pending_flip_queue);
3896 queue_work(dev_priv->wq, &work->work);
3897
3898 trace_i915_flip_complete(intel_crtc->plane,
3899 work->pending_flip_obj);
3900}
3901
46a55d30 3902void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3903{
0f91128d 3904 struct drm_device *dev = crtc->dev;
5bb61643 3905 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3906
2c10d571 3907 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3908 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3909 !intel_crtc_has_pending_flip(crtc),
3910 60*HZ) == 0)) {
3911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3912
5e2d7afc 3913 spin_lock_irq(&dev->event_lock);
9c787942
CW
3914 if (intel_crtc->unpin_work) {
3915 WARN_ONCE(1, "Removing stuck page flip\n");
3916 page_flip_completed(intel_crtc);
3917 }
5e2d7afc 3918 spin_unlock_irq(&dev->event_lock);
9c787942 3919 }
5bb61643 3920
975d568a
CW
3921 if (crtc->primary->fb) {
3922 mutex_lock(&dev->struct_mutex);
3923 intel_finish_fb(crtc->primary->fb);
3924 mutex_unlock(&dev->struct_mutex);
3925 }
e6c3a2a6
CW
3926}
3927
e615efe4
ED
3928/* Program iCLKIP clock to the desired frequency */
3929static void lpt_program_iclkip(struct drm_crtc *crtc)
3930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3933 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3934 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3935 u32 temp;
3936
a580516d 3937 mutex_lock(&dev_priv->sb_lock);
09153000 3938
e615efe4
ED
3939 /* It is necessary to ungate the pixclk gate prior to programming
3940 * the divisors, and gate it back when it is done.
3941 */
3942 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3943
3944 /* Disable SSCCTL */
3945 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3946 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3947 SBI_SSCCTL_DISABLE,
3948 SBI_ICLK);
e615efe4
ED
3949
3950 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3951 if (clock == 20000) {
e615efe4
ED
3952 auxdiv = 1;
3953 divsel = 0x41;
3954 phaseinc = 0x20;
3955 } else {
3956 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3957 * but the adjusted_mode->crtc_clock in in KHz. To get the
3958 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3959 * convert the virtual clock precision to KHz here for higher
3960 * precision.
3961 */
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor, msb_divisor_value, pi_value;
3965
12d7ceed 3966 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3967 msb_divisor_value = desired_divisor / iclk_pi_range;
3968 pi_value = desired_divisor % iclk_pi_range;
3969
3970 auxdiv = 0;
3971 divsel = msb_divisor_value - 2;
3972 phaseinc = pi_value;
3973 }
3974
3975 /* This should not happen with any sane values */
3976 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3977 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3978 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3979 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3980
3981 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3982 clock,
e615efe4
ED
3983 auxdiv,
3984 divsel,
3985 phasedir,
3986 phaseinc);
3987
3988 /* Program SSCDIVINTPHASE6 */
988d6ee8 3989 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3990 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3991 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3992 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3993 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3994 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3995 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3996 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3997
3998 /* Program SSCAUXDIV */
988d6ee8 3999 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4000 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4001 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4002 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4003
4004 /* Enable modulator and associated divider */
988d6ee8 4005 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4006 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4007 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4008
4009 /* Wait for initialization time */
4010 udelay(24);
4011
4012 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 4013
a580516d 4014 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
4015}
4016
275f01b2
DV
4017static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4018 enum pipe pch_transcoder)
4019{
4020 struct drm_device *dev = crtc->base.dev;
4021 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4022 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4023
4024 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4025 I915_READ(HTOTAL(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4027 I915_READ(HBLANK(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4029 I915_READ(HSYNC(cpu_transcoder)));
4030
4031 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4032 I915_READ(VTOTAL(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4034 I915_READ(VBLANK(cpu_transcoder)));
4035 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4036 I915_READ(VSYNC(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4038 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4039}
4040
003632d9 4041static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4042{
4043 struct drm_i915_private *dev_priv = dev->dev_private;
4044 uint32_t temp;
4045
4046 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4047 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4048 return;
4049
4050 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4051 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4052
003632d9
ACO
4053 temp &= ~FDI_BC_BIFURCATION_SELECT;
4054 if (enable)
4055 temp |= FDI_BC_BIFURCATION_SELECT;
4056
4057 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4058 I915_WRITE(SOUTH_CHICKEN1, temp);
4059 POSTING_READ(SOUTH_CHICKEN1);
4060}
4061
4062static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4063{
4064 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4065
4066 switch (intel_crtc->pipe) {
4067 case PIPE_A:
4068 break;
4069 case PIPE_B:
6e3c9717 4070 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4071 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4072 else
003632d9 4073 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4074
4075 break;
4076 case PIPE_C:
003632d9 4077 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4078
4079 break;
4080 default:
4081 BUG();
4082 }
4083}
4084
f67a559d
JB
4085/*
4086 * Enable PCH resources required for PCH ports:
4087 * - PCH PLLs
4088 * - FDI training & RX/TX
4089 * - update transcoder timings
4090 * - DP transcoding bits
4091 * - transcoder
4092 */
4093static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4094{
4095 struct drm_device *dev = crtc->dev;
4096 struct drm_i915_private *dev_priv = dev->dev_private;
4097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4098 int pipe = intel_crtc->pipe;
ee7b9f93 4099 u32 reg, temp;
2c07245f 4100
ab9412ba 4101 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4102
1fbc0d78
DV
4103 if (IS_IVYBRIDGE(dev))
4104 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4105
cd986abb
DV
4106 /* Write the TU size bits before fdi link training, so that error
4107 * detection works. */
4108 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4109 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4110
c98e9dcf 4111 /* For PCH output, training FDI link */
674cf967 4112 dev_priv->display.fdi_link_train(crtc);
2c07245f 4113
3ad8a208
DV
4114 /* We need to program the right clock selection before writing the pixel
4115 * mutliplier into the DPLL. */
303b81e0 4116 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4117 u32 sel;
4b645f14 4118
c98e9dcf 4119 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4120 temp |= TRANS_DPLL_ENABLE(pipe);
4121 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4122 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4123 temp |= sel;
4124 else
4125 temp &= ~sel;
c98e9dcf 4126 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4127 }
5eddb70b 4128
3ad8a208
DV
4129 /* XXX: pch pll's can be enabled any time before we enable the PCH
4130 * transcoder, and we actually should do this to not upset any PCH
4131 * transcoder that already use the clock when we share it.
4132 *
4133 * Note that enable_shared_dpll tries to do the right thing, but
4134 * get_shared_dpll unconditionally resets the pll - we need that to have
4135 * the right LVDS enable sequence. */
85b3894f 4136 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4137
d9b6cb56
JB
4138 /* set transcoder timing, panel must allow it */
4139 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4140 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4141
303b81e0 4142 intel_fdi_normal_train(crtc);
5e84e1a4 4143
c98e9dcf 4144 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4145 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4146 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4147 reg = TRANS_DP_CTL(pipe);
4148 temp = I915_READ(reg);
4149 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4150 TRANS_DP_SYNC_MASK |
4151 TRANS_DP_BPC_MASK);
e3ef4479 4152 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4153 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4154
4155 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4156 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4157 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4158 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4159
4160 switch (intel_trans_dp_port_sel(crtc)) {
4161 case PCH_DP_B:
5eddb70b 4162 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4163 break;
4164 case PCH_DP_C:
5eddb70b 4165 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4166 break;
4167 case PCH_DP_D:
5eddb70b 4168 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4169 break;
4170 default:
e95d41e1 4171 BUG();
32f9d658 4172 }
2c07245f 4173
5eddb70b 4174 I915_WRITE(reg, temp);
6be4a607 4175 }
b52eb4dc 4176
b8a4f404 4177 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4178}
4179
1507e5bd
PZ
4180static void lpt_pch_enable(struct drm_crtc *crtc)
4181{
4182 struct drm_device *dev = crtc->dev;
4183 struct drm_i915_private *dev_priv = dev->dev_private;
4184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4185 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4186
ab9412ba 4187 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4188
8c52b5e8 4189 lpt_program_iclkip(crtc);
1507e5bd 4190
0540e488 4191 /* Set transcoder timing. */
275f01b2 4192 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4193
937bb610 4194 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4195}
4196
190f68c5
ACO
4197struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4198 struct intel_crtc_state *crtc_state)
ee7b9f93 4199{
e2b78267 4200 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4201 struct intel_shared_dpll *pll;
de419ab6 4202 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4203 enum intel_dpll_id i;
ee7b9f93 4204
de419ab6
ML
4205 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4206
98b6bd99
DV
4207 if (HAS_PCH_IBX(dev_priv->dev)) {
4208 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4209 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4210 pll = &dev_priv->shared_dplls[i];
98b6bd99 4211
46edb027
DV
4212 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4213 crtc->base.base.id, pll->name);
98b6bd99 4214
de419ab6 4215 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4216
98b6bd99
DV
4217 goto found;
4218 }
4219
bcddf610
S
4220 if (IS_BROXTON(dev_priv->dev)) {
4221 /* PLL is attached to port in bxt */
4222 struct intel_encoder *encoder;
4223 struct intel_digital_port *intel_dig_port;
4224
4225 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4226 if (WARN_ON(!encoder))
4227 return NULL;
4228
4229 intel_dig_port = enc_to_dig_port(&encoder->base);
4230 /* 1:1 mapping between ports and PLLs */
4231 i = (enum intel_dpll_id)intel_dig_port->port;
4232 pll = &dev_priv->shared_dplls[i];
4233 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4234 crtc->base.base.id, pll->name);
de419ab6 4235 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4236
4237 goto found;
4238 }
4239
e72f9fbf
DV
4240 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4241 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4242
4243 /* Only want to check enabled timings first */
de419ab6 4244 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4245 continue;
4246
190f68c5 4247 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4248 &shared_dpll[i].hw_state,
4249 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4250 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4251 crtc->base.base.id, pll->name,
de419ab6 4252 shared_dpll[i].crtc_mask,
8bd31e67 4253 pll->active);
ee7b9f93
JB
4254 goto found;
4255 }
4256 }
4257
4258 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4259 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4260 pll = &dev_priv->shared_dplls[i];
de419ab6 4261 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4262 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4263 crtc->base.base.id, pll->name);
ee7b9f93
JB
4264 goto found;
4265 }
4266 }
4267
4268 return NULL;
4269
4270found:
de419ab6
ML
4271 if (shared_dpll[i].crtc_mask == 0)
4272 shared_dpll[i].hw_state =
4273 crtc_state->dpll_hw_state;
f2a69f44 4274
190f68c5 4275 crtc_state->shared_dpll = i;
46edb027
DV
4276 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4277 pipe_name(crtc->pipe));
ee7b9f93 4278
de419ab6 4279 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4280
ee7b9f93
JB
4281 return pll;
4282}
4283
de419ab6 4284static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4285{
de419ab6
ML
4286 struct drm_i915_private *dev_priv = to_i915(state->dev);
4287 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4288 struct intel_shared_dpll *pll;
4289 enum intel_dpll_id i;
4290
de419ab6
ML
4291 if (!to_intel_atomic_state(state)->dpll_set)
4292 return;
8bd31e67 4293
de419ab6 4294 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4295 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4296 pll = &dev_priv->shared_dplls[i];
de419ab6 4297 pll->config = shared_dpll[i];
8bd31e67
ACO
4298 }
4299}
4300
a1520318 4301static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4302{
4303 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4304 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4305 u32 temp;
4306
4307 temp = I915_READ(dslreg);
4308 udelay(500);
4309 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4310 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4311 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4312 }
4313}
4314
86adf9d7
ML
4315static int
4316skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4317 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4318 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4319{
86adf9d7
ML
4320 struct intel_crtc_scaler_state *scaler_state =
4321 &crtc_state->scaler_state;
4322 struct intel_crtc *intel_crtc =
4323 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4324 int need_scaling;
6156a456
CK
4325
4326 need_scaling = intel_rotation_90_or_270(rotation) ?
4327 (src_h != dst_w || src_w != dst_h):
4328 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4329
4330 /*
4331 * if plane is being disabled or scaler is no more required or force detach
4332 * - free scaler binded to this plane/crtc
4333 * - in order to do this, update crtc->scaler_usage
4334 *
4335 * Here scaler state in crtc_state is set free so that
4336 * scaler can be assigned to other user. Actual register
4337 * update to free the scaler is done in plane/panel-fit programming.
4338 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4339 */
86adf9d7 4340 if (force_detach || !need_scaling) {
a1b2278e 4341 if (*scaler_id >= 0) {
86adf9d7 4342 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4343 scaler_state->scalers[*scaler_id].in_use = 0;
4344
86adf9d7
ML
4345 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4346 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4347 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4348 scaler_state->scaler_users);
4349 *scaler_id = -1;
4350 }
4351 return 0;
4352 }
4353
4354 /* range checks */
4355 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4356 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4357
4358 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4359 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4360 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4361 "size is out of scaler range\n",
86adf9d7 4362 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4363 return -EINVAL;
4364 }
4365
86adf9d7
ML
4366 /* mark this plane as a scaler user in crtc_state */
4367 scaler_state->scaler_users |= (1 << scaler_user);
4368 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4369 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4370 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4371 scaler_state->scaler_users);
4372
4373 return 0;
4374}
4375
4376/**
4377 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4378 *
4379 * @state: crtc's scaler state
86adf9d7
ML
4380 *
4381 * Return
4382 * 0 - scaler_usage updated successfully
4383 * error - requested scaling cannot be supported or other error condition
4384 */
e435d6e5 4385int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4386{
4387 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4388 struct drm_display_mode *adjusted_mode =
4389 &state->base.adjusted_mode;
4390
4391 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4392 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4393
e435d6e5 4394 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
86adf9d7
ML
4395 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4396 state->pipe_src_w, state->pipe_src_h,
8c6cda29 4397 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
86adf9d7
ML
4398}
4399
4400/**
4401 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4402 *
4403 * @state: crtc's scaler state
86adf9d7
ML
4404 * @plane_state: atomic plane state to update
4405 *
4406 * Return
4407 * 0 - scaler_usage updated successfully
4408 * error - requested scaling cannot be supported or other error condition
4409 */
da20eabd
ML
4410static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4411 struct intel_plane_state *plane_state)
86adf9d7
ML
4412{
4413
4414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4415 struct intel_plane *intel_plane =
4416 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4417 struct drm_framebuffer *fb = plane_state->base.fb;
4418 int ret;
4419
4420 bool force_detach = !fb || !plane_state->visible;
4421
4422 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4423 intel_plane->base.base.id, intel_crtc->pipe,
4424 drm_plane_index(&intel_plane->base));
4425
4426 ret = skl_update_scaler(crtc_state, force_detach,
4427 drm_plane_index(&intel_plane->base),
4428 &plane_state->scaler_id,
4429 plane_state->base.rotation,
4430 drm_rect_width(&plane_state->src) >> 16,
4431 drm_rect_height(&plane_state->src) >> 16,
4432 drm_rect_width(&plane_state->dst),
4433 drm_rect_height(&plane_state->dst));
4434
4435 if (ret || plane_state->scaler_id < 0)
4436 return ret;
4437
a1b2278e 4438 /* check colorkey */
818ed961 4439 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4440 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4441 intel_plane->base.base.id);
a1b2278e
CK
4442 return -EINVAL;
4443 }
4444
4445 /* Check src format */
86adf9d7
ML
4446 switch (fb->pixel_format) {
4447 case DRM_FORMAT_RGB565:
4448 case DRM_FORMAT_XBGR8888:
4449 case DRM_FORMAT_XRGB8888:
4450 case DRM_FORMAT_ABGR8888:
4451 case DRM_FORMAT_ARGB8888:
4452 case DRM_FORMAT_XRGB2101010:
4453 case DRM_FORMAT_XBGR2101010:
4454 case DRM_FORMAT_YUYV:
4455 case DRM_FORMAT_YVYU:
4456 case DRM_FORMAT_UYVY:
4457 case DRM_FORMAT_VYUY:
4458 break;
4459 default:
4460 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4461 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4462 return -EINVAL;
a1b2278e
CK
4463 }
4464
a1b2278e
CK
4465 return 0;
4466}
4467
e435d6e5
ML
4468static void skylake_scaler_disable(struct intel_crtc *crtc)
4469{
4470 int i;
4471
4472 for (i = 0; i < crtc->num_scalers; i++)
4473 skl_detach_scaler(crtc, i);
4474}
4475
4476static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4477{
4478 struct drm_device *dev = crtc->base.dev;
4479 struct drm_i915_private *dev_priv = dev->dev_private;
4480 int pipe = crtc->pipe;
a1b2278e
CK
4481 struct intel_crtc_scaler_state *scaler_state =
4482 &crtc->config->scaler_state;
4483
4484 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4485
6e3c9717 4486 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4487 int id;
4488
4489 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4490 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4491 return;
4492 }
4493
4494 id = scaler_state->scaler_id;
4495 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4496 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4497 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4498 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4499
4500 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4501 }
4502}
4503
b074cec8
JB
4504static void ironlake_pfit_enable(struct intel_crtc *crtc)
4505{
4506 struct drm_device *dev = crtc->base.dev;
4507 struct drm_i915_private *dev_priv = dev->dev_private;
4508 int pipe = crtc->pipe;
4509
6e3c9717 4510 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4511 /* Force use of hard-coded filter coefficients
4512 * as some pre-programmed values are broken,
4513 * e.g. x201.
4514 */
4515 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4516 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4517 PF_PIPE_SEL_IVB(pipe));
4518 else
4519 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4520 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4521 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4522 }
4523}
4524
20bc8673 4525void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4526{
cea165c3
VS
4527 struct drm_device *dev = crtc->base.dev;
4528 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4529
6e3c9717 4530 if (!crtc->config->ips_enabled)
d77e4531
PZ
4531 return;
4532
cea165c3
VS
4533 /* We can only enable IPS after we enable a plane and wait for a vblank */
4534 intel_wait_for_vblank(dev, crtc->pipe);
4535
d77e4531 4536 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4537 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4538 mutex_lock(&dev_priv->rps.hw_lock);
4539 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4540 mutex_unlock(&dev_priv->rps.hw_lock);
4541 /* Quoting Art Runyan: "its not safe to expect any particular
4542 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4543 * mailbox." Moreover, the mailbox may return a bogus state,
4544 * so we need to just enable it and continue on.
2a114cc1
BW
4545 */
4546 } else {
4547 I915_WRITE(IPS_CTL, IPS_ENABLE);
4548 /* The bit only becomes 1 in the next vblank, so this wait here
4549 * is essentially intel_wait_for_vblank. If we don't have this
4550 * and don't wait for vblanks until the end of crtc_enable, then
4551 * the HW state readout code will complain that the expected
4552 * IPS_CTL value is not the one we read. */
4553 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4554 DRM_ERROR("Timed out waiting for IPS enable\n");
4555 }
d77e4531
PZ
4556}
4557
20bc8673 4558void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4559{
4560 struct drm_device *dev = crtc->base.dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562
6e3c9717 4563 if (!crtc->config->ips_enabled)
d77e4531
PZ
4564 return;
4565
4566 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4567 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4568 mutex_lock(&dev_priv->rps.hw_lock);
4569 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4570 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4571 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4572 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4573 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4574 } else {
2a114cc1 4575 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4576 POSTING_READ(IPS_CTL);
4577 }
d77e4531
PZ
4578
4579 /* We need to wait for a vblank before we can disable the plane. */
4580 intel_wait_for_vblank(dev, crtc->pipe);
4581}
4582
4583/** Loads the palette/gamma unit for the CRTC with the prepared values */
4584static void intel_crtc_load_lut(struct drm_crtc *crtc)
4585{
4586 struct drm_device *dev = crtc->dev;
4587 struct drm_i915_private *dev_priv = dev->dev_private;
4588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4589 enum pipe pipe = intel_crtc->pipe;
4590 int palreg = PALETTE(pipe);
4591 int i;
4592 bool reenable_ips = false;
4593
4594 /* The clocks have to be on to load the palette. */
53d9f4e9 4595 if (!crtc->state->active)
d77e4531
PZ
4596 return;
4597
50360403 4598 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4599 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4600 assert_dsi_pll_enabled(dev_priv);
4601 else
4602 assert_pll_enabled(dev_priv, pipe);
4603 }
4604
4605 /* use legacy palette for Ironlake */
7a1db49a 4606 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4607 palreg = LGC_PALETTE(pipe);
4608
4609 /* Workaround : Do not read or write the pipe palette/gamma data while
4610 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4611 */
6e3c9717 4612 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4613 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4614 GAMMA_MODE_MODE_SPLIT)) {
4615 hsw_disable_ips(intel_crtc);
4616 reenable_ips = true;
4617 }
4618
4619 for (i = 0; i < 256; i++) {
4620 I915_WRITE(palreg + 4 * i,
4621 (intel_crtc->lut_r[i] << 16) |
4622 (intel_crtc->lut_g[i] << 8) |
4623 intel_crtc->lut_b[i]);
4624 }
4625
4626 if (reenable_ips)
4627 hsw_enable_ips(intel_crtc);
4628}
4629
7cac945f 4630static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4631{
7cac945f 4632 if (intel_crtc->overlay) {
d3eedb1a
VS
4633 struct drm_device *dev = intel_crtc->base.dev;
4634 struct drm_i915_private *dev_priv = dev->dev_private;
4635
4636 mutex_lock(&dev->struct_mutex);
4637 dev_priv->mm.interruptible = false;
4638 (void) intel_overlay_switch_off(intel_crtc->overlay);
4639 dev_priv->mm.interruptible = true;
4640 mutex_unlock(&dev->struct_mutex);
4641 }
4642
4643 /* Let userspace switch the overlay on again. In most cases userspace
4644 * has to recompute where to put it anyway.
4645 */
4646}
4647
87d4300a
ML
4648/**
4649 * intel_post_enable_primary - Perform operations after enabling primary plane
4650 * @crtc: the CRTC whose primary plane was just enabled
4651 *
4652 * Performs potentially sleeping operations that must be done after the primary
4653 * plane is enabled, such as updating FBC and IPS. Note that this may be
4654 * called due to an explicit primary plane update, or due to an implicit
4655 * re-enable that is caused when a sprite plane is updated to no longer
4656 * completely hide the primary plane.
4657 */
4658static void
4659intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4660{
4661 struct drm_device *dev = crtc->dev;
87d4300a 4662 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4664 int pipe = intel_crtc->pipe;
a5c4d7bc 4665
87d4300a
ML
4666 /*
4667 * BDW signals flip done immediately if the plane
4668 * is disabled, even if the plane enable is already
4669 * armed to occur at the next vblank :(
4670 */
4671 if (IS_BROADWELL(dev))
4672 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4673
87d4300a
ML
4674 /*
4675 * FIXME IPS should be fine as long as one plane is
4676 * enabled, but in practice it seems to have problems
4677 * when going from primary only to sprite only and vice
4678 * versa.
4679 */
a5c4d7bc
VS
4680 hsw_enable_ips(intel_crtc);
4681
f99d7069 4682 /*
87d4300a
ML
4683 * Gen2 reports pipe underruns whenever all planes are disabled.
4684 * So don't enable underrun reporting before at least some planes
4685 * are enabled.
4686 * FIXME: Need to fix the logic to work when we turn off all planes
4687 * but leave the pipe running.
f99d7069 4688 */
87d4300a
ML
4689 if (IS_GEN2(dev))
4690 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4691
4692 /* Underruns don't raise interrupts, so check manually. */
4693 if (HAS_GMCH_DISPLAY(dev))
4694 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4695}
4696
87d4300a
ML
4697/**
4698 * intel_pre_disable_primary - Perform operations before disabling primary plane
4699 * @crtc: the CRTC whose primary plane is to be disabled
4700 *
4701 * Performs potentially sleeping operations that must be done before the
4702 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4703 * be called due to an explicit primary plane update, or due to an implicit
4704 * disable that is caused when a sprite plane completely hides the primary
4705 * plane.
4706 */
4707static void
4708intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4709{
4710 struct drm_device *dev = crtc->dev;
4711 struct drm_i915_private *dev_priv = dev->dev_private;
4712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4713 int pipe = intel_crtc->pipe;
a5c4d7bc 4714
87d4300a
ML
4715 /*
4716 * Gen2 reports pipe underruns whenever all planes are disabled.
4717 * So diasble underrun reporting before all the planes get disabled.
4718 * FIXME: Need to fix the logic to work when we turn off all planes
4719 * but leave the pipe running.
4720 */
4721 if (IS_GEN2(dev))
4722 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4723
87d4300a
ML
4724 /*
4725 * Vblank time updates from the shadow to live plane control register
4726 * are blocked if the memory self-refresh mode is active at that
4727 * moment. So to make sure the plane gets truly disabled, disable
4728 * first the self-refresh mode. The self-refresh enable bit in turn
4729 * will be checked/applied by the HW only at the next frame start
4730 * event which is after the vblank start event, so we need to have a
4731 * wait-for-vblank between disabling the plane and the pipe.
4732 */
262cd2e1 4733 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4734 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4735 dev_priv->wm.vlv.cxsr = false;
4736 intel_wait_for_vblank(dev, pipe);
4737 }
87d4300a 4738
87d4300a
ML
4739 /*
4740 * FIXME IPS should be fine as long as one plane is
4741 * enabled, but in practice it seems to have problems
4742 * when going from primary only to sprite only and vice
4743 * versa.
4744 */
a5c4d7bc 4745 hsw_disable_ips(intel_crtc);
87d4300a
ML
4746}
4747
ac21b225
ML
4748static void intel_post_plane_update(struct intel_crtc *crtc)
4749{
4750 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4751 struct drm_device *dev = crtc->base.dev;
7733b49b 4752 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4753 struct drm_plane *plane;
4754
4755 if (atomic->wait_vblank)
4756 intel_wait_for_vblank(dev, crtc->pipe);
4757
4758 intel_frontbuffer_flip(dev, atomic->fb_bits);
4759
852eb00d
VS
4760 if (atomic->disable_cxsr)
4761 crtc->wm.cxsr_allowed = true;
4762
f015c551
VS
4763 if (crtc->atomic.update_wm_post)
4764 intel_update_watermarks(&crtc->base);
4765
c80ac854 4766 if (atomic->update_fbc)
7733b49b 4767 intel_fbc_update(dev_priv);
ac21b225
ML
4768
4769 if (atomic->post_enable_primary)
4770 intel_post_enable_primary(&crtc->base);
4771
4772 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4773 intel_update_sprite_watermarks(plane, &crtc->base,
4774 0, 0, 0, false, false);
4775
4776 memset(atomic, 0, sizeof(*atomic));
4777}
4778
4779static void intel_pre_plane_update(struct intel_crtc *crtc)
4780{
4781 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4782 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4783 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4784 struct drm_plane *p;
4785
4786 /* Track fb's for any planes being disabled */
ac21b225
ML
4787 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4788 struct intel_plane *plane = to_intel_plane(p);
ac21b225
ML
4789
4790 mutex_lock(&dev->struct_mutex);
a9ff8714
VS
4791 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4792 plane->frontbuffer_bit);
ac21b225
ML
4793 mutex_unlock(&dev->struct_mutex);
4794 }
4795
4796 if (atomic->wait_for_flips)
4797 intel_crtc_wait_for_pending_flips(&crtc->base);
4798
c80ac854 4799 if (atomic->disable_fbc)
25ad93fd 4800 intel_fbc_disable_crtc(crtc);
ac21b225 4801
066cf55b
RV
4802 if (crtc->atomic.disable_ips)
4803 hsw_disable_ips(crtc);
4804
ac21b225
ML
4805 if (atomic->pre_disable_primary)
4806 intel_pre_disable_primary(&crtc->base);
852eb00d
VS
4807
4808 if (atomic->disable_cxsr) {
4809 crtc->wm.cxsr_allowed = false;
4810 intel_set_memory_cxsr(dev_priv, false);
4811 }
ac21b225
ML
4812}
4813
d032ffa0 4814static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4815{
4816 struct drm_device *dev = crtc->dev;
4817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4818 struct drm_plane *p;
87d4300a
ML
4819 int pipe = intel_crtc->pipe;
4820
7cac945f 4821 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4822
d032ffa0
ML
4823 drm_for_each_plane_mask(p, dev, plane_mask)
4824 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4825
f99d7069
DV
4826 /*
4827 * FIXME: Once we grow proper nuclear flip support out of this we need
4828 * to compute the mask of flip planes precisely. For the time being
4829 * consider this a flip to a NULL plane.
4830 */
4831 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4832}
4833
f67a559d
JB
4834static void ironlake_crtc_enable(struct drm_crtc *crtc)
4835{
4836 struct drm_device *dev = crtc->dev;
4837 struct drm_i915_private *dev_priv = dev->dev_private;
4838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4839 struct intel_encoder *encoder;
f67a559d 4840 int pipe = intel_crtc->pipe;
f67a559d 4841
53d9f4e9 4842 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4843 return;
4844
6e3c9717 4845 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4846 intel_prepare_shared_dpll(intel_crtc);
4847
6e3c9717 4848 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4849 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4850
4851 intel_set_pipe_timings(intel_crtc);
4852
6e3c9717 4853 if (intel_crtc->config->has_pch_encoder) {
29407aab 4854 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4855 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4856 }
4857
4858 ironlake_set_pipeconf(crtc);
4859
f67a559d 4860 intel_crtc->active = true;
8664281b 4861
a72e4c9f
DV
4862 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4863 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4864
f6736a1a 4865 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4866 if (encoder->pre_enable)
4867 encoder->pre_enable(encoder);
f67a559d 4868
6e3c9717 4869 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4870 /* Note: FDI PLL enabling _must_ be done before we enable the
4871 * cpu pipes, hence this is separate from all the other fdi/pch
4872 * enabling. */
88cefb6c 4873 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4874 } else {
4875 assert_fdi_tx_disabled(dev_priv, pipe);
4876 assert_fdi_rx_disabled(dev_priv, pipe);
4877 }
f67a559d 4878
b074cec8 4879 ironlake_pfit_enable(intel_crtc);
f67a559d 4880
9c54c0dd
JB
4881 /*
4882 * On ILK+ LUT must be loaded before the pipe is running but with
4883 * clocks enabled
4884 */
4885 intel_crtc_load_lut(crtc);
4886
f37fcc2a 4887 intel_update_watermarks(crtc);
e1fdc473 4888 intel_enable_pipe(intel_crtc);
f67a559d 4889
6e3c9717 4890 if (intel_crtc->config->has_pch_encoder)
f67a559d 4891 ironlake_pch_enable(crtc);
c98e9dcf 4892
f9b61ff6
DV
4893 assert_vblank_disabled(crtc);
4894 drm_crtc_vblank_on(crtc);
4895
fa5c73b1
DV
4896 for_each_encoder_on_crtc(dev, crtc, encoder)
4897 encoder->enable(encoder);
61b77ddd
DV
4898
4899 if (HAS_PCH_CPT(dev))
a1520318 4900 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4901}
4902
42db64ef
PZ
4903/* IPS only exists on ULT machines and is tied to pipe A. */
4904static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4905{
f5adf94e 4906 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4907}
4908
4f771f10
PZ
4909static void haswell_crtc_enable(struct drm_crtc *crtc)
4910{
4911 struct drm_device *dev = crtc->dev;
4912 struct drm_i915_private *dev_priv = dev->dev_private;
4913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4914 struct intel_encoder *encoder;
99d736a2
ML
4915 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4916 struct intel_crtc_state *pipe_config =
4917 to_intel_crtc_state(crtc->state);
4f771f10 4918
53d9f4e9 4919 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4920 return;
4921
df8ad70c
DV
4922 if (intel_crtc_to_shared_dpll(intel_crtc))
4923 intel_enable_shared_dpll(intel_crtc);
4924
6e3c9717 4925 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4926 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4927
4928 intel_set_pipe_timings(intel_crtc);
4929
6e3c9717
ACO
4930 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4931 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4932 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4933 }
4934
6e3c9717 4935 if (intel_crtc->config->has_pch_encoder) {
229fca97 4936 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4937 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4938 }
4939
4940 haswell_set_pipeconf(crtc);
4941
4942 intel_set_pipe_csc(crtc);
4943
4f771f10 4944 intel_crtc->active = true;
8664281b 4945
a72e4c9f 4946 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4947 for_each_encoder_on_crtc(dev, crtc, encoder)
4948 if (encoder->pre_enable)
4949 encoder->pre_enable(encoder);
4950
6e3c9717 4951 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4952 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4953 true);
4fe9467d
ID
4954 dev_priv->display.fdi_link_train(crtc);
4955 }
4956
1f544388 4957 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4958
ff6d9f55 4959 if (INTEL_INFO(dev)->gen == 9)
e435d6e5 4960 skylake_pfit_enable(intel_crtc);
ff6d9f55 4961 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4962 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4963 else
4964 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4965
4966 /*
4967 * On ILK+ LUT must be loaded before the pipe is running but with
4968 * clocks enabled
4969 */
4970 intel_crtc_load_lut(crtc);
4971
1f544388 4972 intel_ddi_set_pipe_settings(crtc);
8228c251 4973 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4974
f37fcc2a 4975 intel_update_watermarks(crtc);
e1fdc473 4976 intel_enable_pipe(intel_crtc);
42db64ef 4977
6e3c9717 4978 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4979 lpt_pch_enable(crtc);
4f771f10 4980
6e3c9717 4981 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4982 intel_ddi_set_vc_payload_alloc(crtc, true);
4983
f9b61ff6
DV
4984 assert_vblank_disabled(crtc);
4985 drm_crtc_vblank_on(crtc);
4986
8807e55b 4987 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4988 encoder->enable(encoder);
8807e55b
JN
4989 intel_opregion_notify_encoder(encoder, true);
4990 }
4f771f10 4991
e4916946
PZ
4992 /* If we change the relative order between pipe/planes enabling, we need
4993 * to change the workaround. */
99d736a2
ML
4994 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4995 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4996 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4997 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4998 }
4f771f10
PZ
4999}
5000
3f8dce3a
DV
5001static void ironlake_pfit_disable(struct intel_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->base.dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 int pipe = crtc->pipe;
5006
5007 /* To avoid upsetting the power well on haswell only disable the pfit if
5008 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 5009 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5010 I915_WRITE(PF_CTL(pipe), 0);
5011 I915_WRITE(PF_WIN_POS(pipe), 0);
5012 I915_WRITE(PF_WIN_SZ(pipe), 0);
5013 }
5014}
5015
6be4a607
JB
5016static void ironlake_crtc_disable(struct drm_crtc *crtc)
5017{
5018 struct drm_device *dev = crtc->dev;
5019 struct drm_i915_private *dev_priv = dev->dev_private;
5020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5021 struct intel_encoder *encoder;
6be4a607 5022 int pipe = intel_crtc->pipe;
5eddb70b 5023 u32 reg, temp;
b52eb4dc 5024
ea9d758d
DV
5025 for_each_encoder_on_crtc(dev, crtc, encoder)
5026 encoder->disable(encoder);
5027
f9b61ff6
DV
5028 drm_crtc_vblank_off(crtc);
5029 assert_vblank_disabled(crtc);
5030
6e3c9717 5031 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5032 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5033
575f7ab7 5034 intel_disable_pipe(intel_crtc);
32f9d658 5035
3f8dce3a 5036 ironlake_pfit_disable(intel_crtc);
2c07245f 5037
5a74f70a
VS
5038 if (intel_crtc->config->has_pch_encoder)
5039 ironlake_fdi_disable(crtc);
5040
bf49ec8c
DV
5041 for_each_encoder_on_crtc(dev, crtc, encoder)
5042 if (encoder->post_disable)
5043 encoder->post_disable(encoder);
2c07245f 5044
6e3c9717 5045 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5046 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5047
d925c59a
DV
5048 if (HAS_PCH_CPT(dev)) {
5049 /* disable TRANS_DP_CTL */
5050 reg = TRANS_DP_CTL(pipe);
5051 temp = I915_READ(reg);
5052 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5053 TRANS_DP_PORT_SEL_MASK);
5054 temp |= TRANS_DP_PORT_SEL_NONE;
5055 I915_WRITE(reg, temp);
5056
5057 /* disable DPLL_SEL */
5058 temp = I915_READ(PCH_DPLL_SEL);
11887397 5059 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5060 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5061 }
e3421a18 5062
d925c59a
DV
5063 ironlake_fdi_pll_disable(intel_crtc);
5064 }
e4ca0612
PJ
5065
5066 intel_crtc->active = false;
5067 intel_update_watermarks(crtc);
6be4a607 5068}
1b3c7a47 5069
4f771f10 5070static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5071{
4f771f10
PZ
5072 struct drm_device *dev = crtc->dev;
5073 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5075 struct intel_encoder *encoder;
6e3c9717 5076 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5077
8807e55b
JN
5078 for_each_encoder_on_crtc(dev, crtc, encoder) {
5079 intel_opregion_notify_encoder(encoder, false);
4f771f10 5080 encoder->disable(encoder);
8807e55b 5081 }
4f771f10 5082
f9b61ff6
DV
5083 drm_crtc_vblank_off(crtc);
5084 assert_vblank_disabled(crtc);
5085
6e3c9717 5086 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5087 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5088 false);
575f7ab7 5089 intel_disable_pipe(intel_crtc);
4f771f10 5090
6e3c9717 5091 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5092 intel_ddi_set_vc_payload_alloc(crtc, false);
5093
ad80a810 5094 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5095
ff6d9f55 5096 if (INTEL_INFO(dev)->gen == 9)
e435d6e5 5097 skylake_scaler_disable(intel_crtc);
ff6d9f55 5098 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5099 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5100 else
5101 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5102
1f544388 5103 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5104
6e3c9717 5105 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5106 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5107 intel_ddi_fdi_disable(crtc);
83616634 5108 }
4f771f10 5109
97b040aa
ID
5110 for_each_encoder_on_crtc(dev, crtc, encoder)
5111 if (encoder->post_disable)
5112 encoder->post_disable(encoder);
e4ca0612
PJ
5113
5114 intel_crtc->active = false;
5115 intel_update_watermarks(crtc);
4f771f10
PZ
5116}
5117
2dd24552
JB
5118static void i9xx_pfit_enable(struct intel_crtc *crtc)
5119{
5120 struct drm_device *dev = crtc->base.dev;
5121 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5122 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5123
681a8504 5124 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5125 return;
5126
2dd24552 5127 /*
c0b03411
DV
5128 * The panel fitter should only be adjusted whilst the pipe is disabled,
5129 * according to register description and PRM.
2dd24552 5130 */
c0b03411
DV
5131 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5132 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5133
b074cec8
JB
5134 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5135 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5136
5137 /* Border color in case we don't scale up to the full screen. Black by
5138 * default, change to something else for debugging. */
5139 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5140}
5141
d05410f9
DA
5142static enum intel_display_power_domain port_to_power_domain(enum port port)
5143{
5144 switch (port) {
5145 case PORT_A:
5146 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5147 case PORT_B:
5148 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5149 case PORT_C:
5150 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5151 case PORT_D:
5152 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5153 default:
5154 WARN_ON_ONCE(1);
5155 return POWER_DOMAIN_PORT_OTHER;
5156 }
5157}
5158
77d22dca
ID
5159#define for_each_power_domain(domain, mask) \
5160 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5161 if ((1 << (domain)) & (mask))
5162
319be8ae
ID
5163enum intel_display_power_domain
5164intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5165{
5166 struct drm_device *dev = intel_encoder->base.dev;
5167 struct intel_digital_port *intel_dig_port;
5168
5169 switch (intel_encoder->type) {
5170 case INTEL_OUTPUT_UNKNOWN:
5171 /* Only DDI platforms should ever use this output type */
5172 WARN_ON_ONCE(!HAS_DDI(dev));
5173 case INTEL_OUTPUT_DISPLAYPORT:
5174 case INTEL_OUTPUT_HDMI:
5175 case INTEL_OUTPUT_EDP:
5176 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5177 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5178 case INTEL_OUTPUT_DP_MST:
5179 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5180 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5181 case INTEL_OUTPUT_ANALOG:
5182 return POWER_DOMAIN_PORT_CRT;
5183 case INTEL_OUTPUT_DSI:
5184 return POWER_DOMAIN_PORT_DSI;
5185 default:
5186 return POWER_DOMAIN_PORT_OTHER;
5187 }
5188}
5189
5190static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5191{
319be8ae
ID
5192 struct drm_device *dev = crtc->dev;
5193 struct intel_encoder *intel_encoder;
5194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5195 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5196 unsigned long mask;
5197 enum transcoder transcoder;
5198
292b990e
ML
5199 if (!crtc->state->active)
5200 return 0;
5201
77d22dca
ID
5202 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5203
5204 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5205 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5206 if (intel_crtc->config->pch_pfit.enabled ||
5207 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5208 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5209
319be8ae
ID
5210 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5211 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5212
77d22dca
ID
5213 return mask;
5214}
5215
292b990e 5216static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5217{
292b990e
ML
5218 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5220 enum intel_display_power_domain domain;
5221 unsigned long domains, new_domains, old_domains;
77d22dca 5222
292b990e
ML
5223 old_domains = intel_crtc->enabled_power_domains;
5224 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
77d22dca 5225
292b990e
ML
5226 domains = new_domains & ~old_domains;
5227
5228 for_each_power_domain(domain, domains)
5229 intel_display_power_get(dev_priv, domain);
5230
5231 return old_domains & ~new_domains;
5232}
5233
5234static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5235 unsigned long domains)
5236{
5237 enum intel_display_power_domain domain;
5238
5239 for_each_power_domain(domain, domains)
5240 intel_display_power_put(dev_priv, domain);
5241}
77d22dca 5242
292b990e
ML
5243static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
5244{
5245 struct drm_device *dev = state->dev;
5246 struct drm_i915_private *dev_priv = dev->dev_private;
5247 unsigned long put_domains[I915_MAX_PIPES] = {};
5248 struct drm_crtc_state *crtc_state;
5249 struct drm_crtc *crtc;
5250 int i;
77d22dca 5251
292b990e
ML
5252 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5253 if (needs_modeset(crtc->state))
5254 put_domains[to_intel_crtc(crtc)->pipe] =
5255 modeset_get_crtc_power_domains(crtc);
77d22dca
ID
5256 }
5257
27c329ed
ML
5258 if (dev_priv->display.modeset_commit_cdclk) {
5259 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5260
5261 if (cdclk != dev_priv->cdclk_freq &&
5262 !WARN_ON(!state->allow_modeset))
5263 dev_priv->display.modeset_commit_cdclk(state);
5264 }
50f6e502 5265
292b990e
ML
5266 for (i = 0; i < I915_MAX_PIPES; i++)
5267 if (put_domains[i])
5268 modeset_put_power_domains(dev_priv, put_domains[i]);
77d22dca
ID
5269}
5270
560a7ae4
DL
5271static void intel_update_max_cdclk(struct drm_device *dev)
5272{
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274
5275 if (IS_SKYLAKE(dev)) {
5276 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5277
5278 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5279 dev_priv->max_cdclk_freq = 675000;
5280 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5281 dev_priv->max_cdclk_freq = 540000;
5282 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5283 dev_priv->max_cdclk_freq = 450000;
5284 else
5285 dev_priv->max_cdclk_freq = 337500;
5286 } else if (IS_BROADWELL(dev)) {
5287 /*
5288 * FIXME with extra cooling we can allow
5289 * 540 MHz for ULX and 675 Mhz for ULT.
5290 * How can we know if extra cooling is
5291 * available? PCI ID, VTB, something else?
5292 */
5293 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5294 dev_priv->max_cdclk_freq = 450000;
5295 else if (IS_BDW_ULX(dev))
5296 dev_priv->max_cdclk_freq = 450000;
5297 else if (IS_BDW_ULT(dev))
5298 dev_priv->max_cdclk_freq = 540000;
5299 else
5300 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5301 } else if (IS_CHERRYVIEW(dev)) {
5302 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5303 } else if (IS_VALLEYVIEW(dev)) {
5304 dev_priv->max_cdclk_freq = 400000;
5305 } else {
5306 /* otherwise assume cdclk is fixed */
5307 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5308 }
5309
5310 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5311 dev_priv->max_cdclk_freq);
5312}
5313
5314static void intel_update_cdclk(struct drm_device *dev)
5315{
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
5321
5322 /*
5323 * Program the gmbus_freq based on the cdclk frequency.
5324 * BSpec erroneously claims we should aim for 4MHz, but
5325 * in fact 1MHz is the correct frequency.
5326 */
5327 if (IS_VALLEYVIEW(dev)) {
5328 /*
5329 * Program the gmbus_freq based on the cdclk frequency.
5330 * BSpec erroneously claims we should aim for 4MHz, but
5331 * in fact 1MHz is the correct frequency.
5332 */
5333 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5334 }
5335
5336 if (dev_priv->max_cdclk_freq == 0)
5337 intel_update_max_cdclk(dev);
5338}
5339
70d0c574 5340static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343 uint32_t divider;
5344 uint32_t ratio;
5345 uint32_t current_freq;
5346 int ret;
5347
5348 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5349 switch (frequency) {
5350 case 144000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 288000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 384000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5360 ratio = BXT_DE_PLL_RATIO(60);
5361 break;
5362 case 576000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 624000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5368 ratio = BXT_DE_PLL_RATIO(65);
5369 break;
5370 case 19200:
5371 /*
5372 * Bypass frequency with DE PLL disabled. Init ratio, divider
5373 * to suppress GCC warning.
5374 */
5375 ratio = 0;
5376 divider = 0;
5377 break;
5378 default:
5379 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5380
5381 return;
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 /* Inform power controller of upcoming frequency change */
5386 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5387 0x80000000);
5388 mutex_unlock(&dev_priv->rps.hw_lock);
5389
5390 if (ret) {
5391 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5392 ret, frequency);
5393 return;
5394 }
5395
5396 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5397 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5398 current_freq = current_freq * 500 + 1000;
5399
5400 /*
5401 * DE PLL has to be disabled when
5402 * - setting to 19.2MHz (bypass, PLL isn't used)
5403 * - before setting to 624MHz (PLL needs toggling)
5404 * - before setting to any frequency from 624MHz (PLL needs toggling)
5405 */
5406 if (frequency == 19200 || frequency == 624000 ||
5407 current_freq == 624000) {
5408 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5409 /* Timeout 200us */
5410 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5411 1))
5412 DRM_ERROR("timout waiting for DE PLL unlock\n");
5413 }
5414
5415 if (frequency != 19200) {
5416 uint32_t val;
5417
5418 val = I915_READ(BXT_DE_PLL_CTL);
5419 val &= ~BXT_DE_PLL_RATIO_MASK;
5420 val |= ratio;
5421 I915_WRITE(BXT_DE_PLL_CTL, val);
5422
5423 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5424 /* Timeout 200us */
5425 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5426 DRM_ERROR("timeout waiting for DE PLL lock\n");
5427
5428 val = I915_READ(CDCLK_CTL);
5429 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5430 val |= divider;
5431 /*
5432 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5433 * enable otherwise.
5434 */
5435 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5436 if (frequency >= 500000)
5437 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5438
5439 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5440 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5441 val |= (frequency - 1000) / 500;
5442 I915_WRITE(CDCLK_CTL, val);
5443 }
5444
5445 mutex_lock(&dev_priv->rps.hw_lock);
5446 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5447 DIV_ROUND_UP(frequency, 25000));
5448 mutex_unlock(&dev_priv->rps.hw_lock);
5449
5450 if (ret) {
5451 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5452 ret, frequency);
5453 return;
5454 }
5455
a47871bd 5456 intel_update_cdclk(dev);
f8437dd1
VK
5457}
5458
5459void broxton_init_cdclk(struct drm_device *dev)
5460{
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462 uint32_t val;
5463
5464 /*
5465 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5466 * or else the reset will hang because there is no PCH to respond.
5467 * Move the handshake programming to initialization sequence.
5468 * Previously was left up to BIOS.
5469 */
5470 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5471 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5472 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5473
5474 /* Enable PG1 for cdclk */
5475 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5476
5477 /* check if cd clock is enabled */
5478 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5479 DRM_DEBUG_KMS("Display already initialized\n");
5480 return;
5481 }
5482
5483 /*
5484 * FIXME:
5485 * - The initial CDCLK needs to be read from VBT.
5486 * Need to make this change after VBT has changes for BXT.
5487 * - check if setting the max (or any) cdclk freq is really necessary
5488 * here, it belongs to modeset time
5489 */
5490 broxton_set_cdclk(dev, 624000);
5491
5492 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5493 POSTING_READ(DBUF_CTL);
5494
f8437dd1
VK
5495 udelay(10);
5496
5497 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5498 DRM_ERROR("DBuf power enable timeout!\n");
5499}
5500
5501void broxton_uninit_cdclk(struct drm_device *dev)
5502{
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5504
5505 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5506 POSTING_READ(DBUF_CTL);
5507
f8437dd1
VK
5508 udelay(10);
5509
5510 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5511 DRM_ERROR("DBuf power disable timeout!\n");
5512
5513 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5514 broxton_set_cdclk(dev, 19200);
5515
5516 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5517}
5518
5d96d8af
DL
5519static const struct skl_cdclk_entry {
5520 unsigned int freq;
5521 unsigned int vco;
5522} skl_cdclk_frequencies[] = {
5523 { .freq = 308570, .vco = 8640 },
5524 { .freq = 337500, .vco = 8100 },
5525 { .freq = 432000, .vco = 8640 },
5526 { .freq = 450000, .vco = 8100 },
5527 { .freq = 540000, .vco = 8100 },
5528 { .freq = 617140, .vco = 8640 },
5529 { .freq = 675000, .vco = 8100 },
5530};
5531
5532static unsigned int skl_cdclk_decimal(unsigned int freq)
5533{
5534 return (freq - 1000) / 500;
5535}
5536
5537static unsigned int skl_cdclk_get_vco(unsigned int freq)
5538{
5539 unsigned int i;
5540
5541 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5542 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5543
5544 if (e->freq == freq)
5545 return e->vco;
5546 }
5547
5548 return 8100;
5549}
5550
5551static void
5552skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5553{
5554 unsigned int min_freq;
5555 u32 val;
5556
5557 /* select the minimum CDCLK before enabling DPLL 0 */
5558 val = I915_READ(CDCLK_CTL);
5559 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5560 val |= CDCLK_FREQ_337_308;
5561
5562 if (required_vco == 8640)
5563 min_freq = 308570;
5564 else
5565 min_freq = 337500;
5566
5567 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5568
5569 I915_WRITE(CDCLK_CTL, val);
5570 POSTING_READ(CDCLK_CTL);
5571
5572 /*
5573 * We always enable DPLL0 with the lowest link rate possible, but still
5574 * taking into account the VCO required to operate the eDP panel at the
5575 * desired frequency. The usual DP link rates operate with a VCO of
5576 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5577 * The modeset code is responsible for the selection of the exact link
5578 * rate later on, with the constraint of choosing a frequency that
5579 * works with required_vco.
5580 */
5581 val = I915_READ(DPLL_CTRL1);
5582
5583 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5585 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5586 if (required_vco == 8640)
5587 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5588 SKL_DPLL0);
5589 else
5590 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5591 SKL_DPLL0);
5592
5593 I915_WRITE(DPLL_CTRL1, val);
5594 POSTING_READ(DPLL_CTRL1);
5595
5596 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5597
5598 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5599 DRM_ERROR("DPLL0 not locked\n");
5600}
5601
5602static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5603{
5604 int ret;
5605 u32 val;
5606
5607 /* inform PCU we want to change CDCLK */
5608 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5609 mutex_lock(&dev_priv->rps.hw_lock);
5610 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5611 mutex_unlock(&dev_priv->rps.hw_lock);
5612
5613 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5614}
5615
5616static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < 15; i++) {
5621 if (skl_cdclk_pcu_ready(dev_priv))
5622 return true;
5623 udelay(10);
5624 }
5625
5626 return false;
5627}
5628
5629static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5630{
560a7ae4 5631 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5632 u32 freq_select, pcu_ack;
5633
5634 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5635
5636 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5637 DRM_ERROR("failed to inform PCU about cdclk change\n");
5638 return;
5639 }
5640
5641 /* set CDCLK_CTL */
5642 switch(freq) {
5643 case 450000:
5644 case 432000:
5645 freq_select = CDCLK_FREQ_450_432;
5646 pcu_ack = 1;
5647 break;
5648 case 540000:
5649 freq_select = CDCLK_FREQ_540;
5650 pcu_ack = 2;
5651 break;
5652 case 308570:
5653 case 337500:
5654 default:
5655 freq_select = CDCLK_FREQ_337_308;
5656 pcu_ack = 0;
5657 break;
5658 case 617140:
5659 case 675000:
5660 freq_select = CDCLK_FREQ_675_617;
5661 pcu_ack = 3;
5662 break;
5663 }
5664
5665 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5666 POSTING_READ(CDCLK_CTL);
5667
5668 /* inform PCU of the change */
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5671 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5672
5673 intel_update_cdclk(dev);
5d96d8af
DL
5674}
5675
5676void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5677{
5678 /* disable DBUF power */
5679 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5680 POSTING_READ(DBUF_CTL);
5681
5682 udelay(10);
5683
5684 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5685 DRM_ERROR("DBuf power disable timeout\n");
5686
5687 /* disable DPLL0 */
5688 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5689 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5690 DRM_ERROR("Couldn't disable DPLL0\n");
5691
5692 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5693}
5694
5695void skl_init_cdclk(struct drm_i915_private *dev_priv)
5696{
5697 u32 val;
5698 unsigned int required_vco;
5699
5700 /* enable PCH reset handshake */
5701 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5702 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5703
5704 /* enable PG1 and Misc I/O */
5705 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5706
5707 /* DPLL0 already enabed !? */
5708 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5709 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5710 return;
5711 }
5712
5713 /* enable DPLL0 */
5714 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5715 skl_dpll0_enable(dev_priv, required_vco);
5716
5717 /* set CDCLK to the frequency the BIOS chose */
5718 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5719
5720 /* enable DBUF power */
5721 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5722 POSTING_READ(DBUF_CTL);
5723
5724 udelay(10);
5725
5726 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5727 DRM_ERROR("DBuf power enable timeout\n");
5728}
5729
dfcab17e 5730/* returns HPLL frequency in kHz */
f8bf63fd 5731static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5732{
586f49dc 5733 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5734
586f49dc 5735 /* Obtain SKU information */
a580516d 5736 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5737 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5738 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5739 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5740
dfcab17e 5741 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5742}
5743
5744/* Adjust CDclk dividers to allow high res or save power if possible */
5745static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5746{
5747 struct drm_i915_private *dev_priv = dev->dev_private;
5748 u32 val, cmd;
5749
164dfd28
VK
5750 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5751 != dev_priv->cdclk_freq);
d60c4473 5752
dfcab17e 5753 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5754 cmd = 2;
dfcab17e 5755 else if (cdclk == 266667)
30a970c6
JB
5756 cmd = 1;
5757 else
5758 cmd = 0;
5759
5760 mutex_lock(&dev_priv->rps.hw_lock);
5761 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5762 val &= ~DSPFREQGUAR_MASK;
5763 val |= (cmd << DSPFREQGUAR_SHIFT);
5764 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5765 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5766 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5767 50)) {
5768 DRM_ERROR("timed out waiting for CDclk change\n");
5769 }
5770 mutex_unlock(&dev_priv->rps.hw_lock);
5771
54433e91
VS
5772 mutex_lock(&dev_priv->sb_lock);
5773
dfcab17e 5774 if (cdclk == 400000) {
6bcda4f0 5775 u32 divider;
30a970c6 5776
6bcda4f0 5777 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5778
30a970c6
JB
5779 /* adjust cdclk divider */
5780 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5781 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5782 val |= divider;
5783 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5784
5785 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5786 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5787 50))
5788 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5789 }
5790
30a970c6
JB
5791 /* adjust self-refresh exit latency value */
5792 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5793 val &= ~0x7f;
5794
5795 /*
5796 * For high bandwidth configs, we set a higher latency in the bunit
5797 * so that the core display fetch happens in time to avoid underruns.
5798 */
dfcab17e 5799 if (cdclk == 400000)
30a970c6
JB
5800 val |= 4500 / 250; /* 4.5 usec */
5801 else
5802 val |= 3000 / 250; /* 3.0 usec */
5803 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5804
a580516d 5805 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5806
b6283055 5807 intel_update_cdclk(dev);
30a970c6
JB
5808}
5809
383c5a6a
VS
5810static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5811{
5812 struct drm_i915_private *dev_priv = dev->dev_private;
5813 u32 val, cmd;
5814
164dfd28
VK
5815 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5816 != dev_priv->cdclk_freq);
383c5a6a
VS
5817
5818 switch (cdclk) {
383c5a6a
VS
5819 case 333333:
5820 case 320000:
383c5a6a 5821 case 266667:
383c5a6a 5822 case 200000:
383c5a6a
VS
5823 break;
5824 default:
5f77eeb0 5825 MISSING_CASE(cdclk);
383c5a6a
VS
5826 return;
5827 }
5828
9d0d3fda
VS
5829 /*
5830 * Specs are full of misinformation, but testing on actual
5831 * hardware has shown that we just need to write the desired
5832 * CCK divider into the Punit register.
5833 */
5834 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5835
383c5a6a
VS
5836 mutex_lock(&dev_priv->rps.hw_lock);
5837 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5838 val &= ~DSPFREQGUAR_MASK_CHV;
5839 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5840 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5841 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5842 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5843 50)) {
5844 DRM_ERROR("timed out waiting for CDclk change\n");
5845 }
5846 mutex_unlock(&dev_priv->rps.hw_lock);
5847
b6283055 5848 intel_update_cdclk(dev);
383c5a6a
VS
5849}
5850
30a970c6
JB
5851static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5852 int max_pixclk)
5853{
6bcda4f0 5854 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5855 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5856
30a970c6
JB
5857 /*
5858 * Really only a few cases to deal with, as only 4 CDclks are supported:
5859 * 200MHz
5860 * 267MHz
29dc7ef3 5861 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5862 * 400MHz (VLV only)
5863 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5864 * of the lower bin and adjust if needed.
e37c67a1
VS
5865 *
5866 * We seem to get an unstable or solid color picture at 200MHz.
5867 * Not sure what's wrong. For now use 200MHz only when all pipes
5868 * are off.
30a970c6 5869 */
6cca3195
VS
5870 if (!IS_CHERRYVIEW(dev_priv) &&
5871 max_pixclk > freq_320*limit/100)
dfcab17e 5872 return 400000;
6cca3195 5873 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5874 return freq_320;
e37c67a1 5875 else if (max_pixclk > 0)
dfcab17e 5876 return 266667;
e37c67a1
VS
5877 else
5878 return 200000;
30a970c6
JB
5879}
5880
f8437dd1
VK
5881static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5882 int max_pixclk)
5883{
5884 /*
5885 * FIXME:
5886 * - remove the guardband, it's not needed on BXT
5887 * - set 19.2MHz bypass frequency if there are no active pipes
5888 */
5889 if (max_pixclk > 576000*9/10)
5890 return 624000;
5891 else if (max_pixclk > 384000*9/10)
5892 return 576000;
5893 else if (max_pixclk > 288000*9/10)
5894 return 384000;
5895 else if (max_pixclk > 144000*9/10)
5896 return 288000;
5897 else
5898 return 144000;
5899}
5900
a821fc46
ACO
5901/* Compute the max pixel clock for new configuration. Uses atomic state if
5902 * that's non-NULL, look at current state otherwise. */
5903static int intel_mode_max_pixclk(struct drm_device *dev,
5904 struct drm_atomic_state *state)
30a970c6 5905{
30a970c6 5906 struct intel_crtc *intel_crtc;
304603f4 5907 struct intel_crtc_state *crtc_state;
30a970c6
JB
5908 int max_pixclk = 0;
5909
d3fcc808 5910 for_each_intel_crtc(dev, intel_crtc) {
27c329ed 5911 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
304603f4
ACO
5912 if (IS_ERR(crtc_state))
5913 return PTR_ERR(crtc_state);
5914
5915 if (!crtc_state->base.enable)
5916 continue;
5917
5918 max_pixclk = max(max_pixclk,
5919 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5920 }
5921
5922 return max_pixclk;
5923}
5924
27c329ed 5925static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5926{
27c329ed
ML
5927 struct drm_device *dev = state->dev;
5928 struct drm_i915_private *dev_priv = dev->dev_private;
5929 int max_pixclk = intel_mode_max_pixclk(dev, state);
30a970c6 5930
304603f4
ACO
5931 if (max_pixclk < 0)
5932 return max_pixclk;
30a970c6 5933
27c329ed
ML
5934 to_intel_atomic_state(state)->cdclk =
5935 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5936
27c329ed
ML
5937 return 0;
5938}
304603f4 5939
27c329ed
ML
5940static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5941{
5942 struct drm_device *dev = state->dev;
5943 struct drm_i915_private *dev_priv = dev->dev_private;
5944 int max_pixclk = intel_mode_max_pixclk(dev, state);
85a96e7a 5945
27c329ed
ML
5946 if (max_pixclk < 0)
5947 return max_pixclk;
85a96e7a 5948
27c329ed
ML
5949 to_intel_atomic_state(state)->cdclk =
5950 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5951
27c329ed 5952 return 0;
30a970c6
JB
5953}
5954
1e69cd74
VS
5955static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5956{
5957 unsigned int credits, default_credits;
5958
5959 if (IS_CHERRYVIEW(dev_priv))
5960 default_credits = PFI_CREDIT(12);
5961 else
5962 default_credits = PFI_CREDIT(8);
5963
164dfd28 5964 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5965 /* CHV suggested value is 31 or 63 */
5966 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5967 credits = PFI_CREDIT_63;
1e69cd74
VS
5968 else
5969 credits = PFI_CREDIT(15);
5970 } else {
5971 credits = default_credits;
5972 }
5973
5974 /*
5975 * WA - write default credits before re-programming
5976 * FIXME: should we also set the resend bit here?
5977 */
5978 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5979 default_credits);
5980
5981 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5982 credits | PFI_CREDIT_RESEND);
5983
5984 /*
5985 * FIXME is this guaranteed to clear
5986 * immediately or should we poll for it?
5987 */
5988 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5989}
5990
27c329ed 5991static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 5992{
a821fc46 5993 struct drm_device *dev = old_state->dev;
27c329ed 5994 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
30a970c6 5995 struct drm_i915_private *dev_priv = dev->dev_private;
30a970c6 5996
27c329ed
ML
5997 /*
5998 * FIXME: We can end up here with all power domains off, yet
5999 * with a CDCLK frequency other than the minimum. To account
6000 * for this take the PIPE-A power domain, which covers the HW
6001 * blocks needed for the following programming. This can be
6002 * removed once it's guaranteed that we get here either with
6003 * the minimum CDCLK set, or the required power domains
6004 * enabled.
6005 */
6006 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6007
27c329ed
ML
6008 if (IS_CHERRYVIEW(dev))
6009 cherryview_set_cdclk(dev, req_cdclk);
6010 else
6011 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6012
27c329ed 6013 vlv_program_pfi_credits(dev_priv);
1e69cd74 6014
27c329ed 6015 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6016}
6017
89b667f8
JB
6018static void valleyview_crtc_enable(struct drm_crtc *crtc)
6019{
6020 struct drm_device *dev = crtc->dev;
a72e4c9f 6021 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6023 struct intel_encoder *encoder;
6024 int pipe = intel_crtc->pipe;
23538ef1 6025 bool is_dsi;
89b667f8 6026
53d9f4e9 6027 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6028 return;
6029
409ee761 6030 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 6031
1ae0d137
VS
6032 if (!is_dsi) {
6033 if (IS_CHERRYVIEW(dev))
6e3c9717 6034 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6035 else
6e3c9717 6036 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6037 }
5b18e57c 6038
6e3c9717 6039 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6040 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6041
6042 intel_set_pipe_timings(intel_crtc);
6043
c14b0485
VS
6044 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6045 struct drm_i915_private *dev_priv = dev->dev_private;
6046
6047 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6048 I915_WRITE(CHV_CANVAS(pipe), 0);
6049 }
6050
5b18e57c
DV
6051 i9xx_set_pipeconf(intel_crtc);
6052
89b667f8 6053 intel_crtc->active = true;
89b667f8 6054
a72e4c9f 6055 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6056
89b667f8
JB
6057 for_each_encoder_on_crtc(dev, crtc, encoder)
6058 if (encoder->pre_pll_enable)
6059 encoder->pre_pll_enable(encoder);
6060
9d556c99
CML
6061 if (!is_dsi) {
6062 if (IS_CHERRYVIEW(dev))
6e3c9717 6063 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6064 else
6e3c9717 6065 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6066 }
89b667f8
JB
6067
6068 for_each_encoder_on_crtc(dev, crtc, encoder)
6069 if (encoder->pre_enable)
6070 encoder->pre_enable(encoder);
6071
2dd24552
JB
6072 i9xx_pfit_enable(intel_crtc);
6073
63cbb074
VS
6074 intel_crtc_load_lut(crtc);
6075
e1fdc473 6076 intel_enable_pipe(intel_crtc);
be6a6f8e 6077
4b3a9526
VS
6078 assert_vblank_disabled(crtc);
6079 drm_crtc_vblank_on(crtc);
6080
f9b61ff6
DV
6081 for_each_encoder_on_crtc(dev, crtc, encoder)
6082 encoder->enable(encoder);
89b667f8
JB
6083}
6084
f13c2ef3
DV
6085static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6086{
6087 struct drm_device *dev = crtc->base.dev;
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089
6e3c9717
ACO
6090 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6091 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6092}
6093
0b8765c6 6094static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6095{
6096 struct drm_device *dev = crtc->dev;
a72e4c9f 6097 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6099 struct intel_encoder *encoder;
79e53945 6100 int pipe = intel_crtc->pipe;
79e53945 6101
53d9f4e9 6102 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6103 return;
6104
f13c2ef3
DV
6105 i9xx_set_pll_dividers(intel_crtc);
6106
6e3c9717 6107 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6108 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6109
6110 intel_set_pipe_timings(intel_crtc);
6111
5b18e57c
DV
6112 i9xx_set_pipeconf(intel_crtc);
6113
f7abfe8b 6114 intel_crtc->active = true;
6b383a7f 6115
4a3436e8 6116 if (!IS_GEN2(dev))
a72e4c9f 6117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6118
9d6d9f19
MK
6119 for_each_encoder_on_crtc(dev, crtc, encoder)
6120 if (encoder->pre_enable)
6121 encoder->pre_enable(encoder);
6122
f6736a1a
DV
6123 i9xx_enable_pll(intel_crtc);
6124
2dd24552
JB
6125 i9xx_pfit_enable(intel_crtc);
6126
63cbb074
VS
6127 intel_crtc_load_lut(crtc);
6128
f37fcc2a 6129 intel_update_watermarks(crtc);
e1fdc473 6130 intel_enable_pipe(intel_crtc);
be6a6f8e 6131
4b3a9526
VS
6132 assert_vblank_disabled(crtc);
6133 drm_crtc_vblank_on(crtc);
6134
f9b61ff6
DV
6135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 encoder->enable(encoder);
0b8765c6 6137}
79e53945 6138
87476d63
DV
6139static void i9xx_pfit_disable(struct intel_crtc *crtc)
6140{
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6143
6e3c9717 6144 if (!crtc->config->gmch_pfit.control)
328d8e82 6145 return;
87476d63 6146
328d8e82 6147 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6148
328d8e82
DV
6149 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6150 I915_READ(PFIT_CONTROL));
6151 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6152}
6153
0b8765c6
JB
6154static void i9xx_crtc_disable(struct drm_crtc *crtc)
6155{
6156 struct drm_device *dev = crtc->dev;
6157 struct drm_i915_private *dev_priv = dev->dev_private;
6158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6159 struct intel_encoder *encoder;
0b8765c6 6160 int pipe = intel_crtc->pipe;
ef9c3aee 6161
6304cd91
VS
6162 /*
6163 * On gen2 planes are double buffered but the pipe isn't, so we must
6164 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6165 * We also need to wait on all gmch platforms because of the
6166 * self-refresh mode constraint explained above.
6304cd91 6167 */
564ed191 6168 intel_wait_for_vblank(dev, pipe);
6304cd91 6169
4b3a9526
VS
6170 for_each_encoder_on_crtc(dev, crtc, encoder)
6171 encoder->disable(encoder);
6172
f9b61ff6
DV
6173 drm_crtc_vblank_off(crtc);
6174 assert_vblank_disabled(crtc);
6175
575f7ab7 6176 intel_disable_pipe(intel_crtc);
24a1f16d 6177
87476d63 6178 i9xx_pfit_disable(intel_crtc);
24a1f16d 6179
89b667f8
JB
6180 for_each_encoder_on_crtc(dev, crtc, encoder)
6181 if (encoder->post_disable)
6182 encoder->post_disable(encoder);
6183
409ee761 6184 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6185 if (IS_CHERRYVIEW(dev))
6186 chv_disable_pll(dev_priv, pipe);
6187 else if (IS_VALLEYVIEW(dev))
6188 vlv_disable_pll(dev_priv, pipe);
6189 else
1c4e0274 6190 i9xx_disable_pll(intel_crtc);
076ed3b2 6191 }
0b8765c6 6192
4a3436e8 6193 if (!IS_GEN2(dev))
a72e4c9f 6194 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
e4ca0612
PJ
6195
6196 intel_crtc->active = false;
6197 intel_update_watermarks(crtc);
0b8765c6
JB
6198}
6199
b17d48e2
ML
6200static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6201{
6202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6203 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6204 enum intel_display_power_domain domain;
6205 unsigned long domains;
6206
6207 if (!intel_crtc->active)
6208 return;
6209
a539205a
ML
6210 if (to_intel_plane_state(crtc->primary->state)->visible) {
6211 intel_crtc_wait_for_pending_flips(crtc);
6212 intel_pre_disable_primary(crtc);
6213 }
6214
d032ffa0 6215 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
b17d48e2
ML
6216 dev_priv->display.crtc_disable(crtc);
6217
6218 domains = intel_crtc->enabled_power_domains;
6219 for_each_power_domain(domain, domains)
6220 intel_display_power_put(dev_priv, domain);
6221 intel_crtc->enabled_power_domains = 0;
6222}
6223
6b72d486
ML
6224/*
6225 * turn all crtc's off, but do not adjust state
6226 * This has to be paired with a call to intel_modeset_setup_hw_state.
6227 */
70e0bd74 6228int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6229{
70e0bd74
ML
6230 struct drm_mode_config *config = &dev->mode_config;
6231 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6232 struct drm_atomic_state *state;
6b72d486 6233 struct drm_crtc *crtc;
70e0bd74
ML
6234 unsigned crtc_mask = 0;
6235 int ret = 0;
6236
6237 if (WARN_ON(!ctx))
6238 return 0;
6239
6240 lockdep_assert_held(&ctx->ww_ctx);
6241 state = drm_atomic_state_alloc(dev);
6242 if (WARN_ON(!state))
6243 return -ENOMEM;
6244
6245 state->acquire_ctx = ctx;
6246 state->allow_modeset = true;
6247
6248 for_each_crtc(dev, crtc) {
6249 struct drm_crtc_state *crtc_state =
6250 drm_atomic_get_crtc_state(state, crtc);
6b72d486 6251
70e0bd74
ML
6252 ret = PTR_ERR_OR_ZERO(crtc_state);
6253 if (ret)
6254 goto free;
6255
6256 if (!crtc_state->active)
6257 continue;
6258
6259 crtc_state->active = false;
6260 crtc_mask |= 1 << drm_crtc_index(crtc);
6261 }
6262
6263 if (crtc_mask) {
6264 ret = intel_set_mode(state);
6265
6266 if (!ret) {
6267 for_each_crtc(dev, crtc)
6268 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6269 crtc->state->active = true;
6270
6271 return ret;
6272 }
6273 }
6274
6275free:
6276 if (ret)
6277 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6278 drm_atomic_state_free(state);
6279 return ret;
ee7b9f93
JB
6280}
6281
b04c5bd6 6282/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6283int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6284{
6285 struct drm_device *dev = crtc->dev;
5da76e94
ML
6286 struct drm_mode_config *config = &dev->mode_config;
6287 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6289 struct intel_crtc_state *pipe_config;
6290 struct drm_atomic_state *state;
6291 int ret;
976f8a20 6292
1b509259 6293 if (enable == intel_crtc->active)
5da76e94 6294 return 0;
0e572fe7 6295
1b509259 6296 if (enable && !crtc->state->enable)
5da76e94 6297 return 0;
1b509259 6298
5da76e94
ML
6299 /* this function should be called with drm_modeset_lock_all for now */
6300 if (WARN_ON(!ctx))
6301 return -EIO;
6302 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6303
5da76e94
ML
6304 state = drm_atomic_state_alloc(dev);
6305 if (WARN_ON(!state))
6306 return -ENOMEM;
1b509259 6307
5da76e94
ML
6308 state->acquire_ctx = ctx;
6309 state->allow_modeset = true;
6310
6311 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6312 if (IS_ERR(pipe_config)) {
6313 ret = PTR_ERR(pipe_config);
6314 goto err;
0e572fe7 6315 }
5da76e94
ML
6316 pipe_config->base.active = enable;
6317
6318 ret = intel_set_mode(state);
6319 if (!ret)
6320 return ret;
6321
6322err:
6323 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6324 drm_atomic_state_free(state);
6325 return ret;
b04c5bd6
BF
6326}
6327
6328/**
6329 * Sets the power management mode of the pipe and plane.
6330 */
6331void intel_crtc_update_dpms(struct drm_crtc *crtc)
6332{
6333 struct drm_device *dev = crtc->dev;
6334 struct intel_encoder *intel_encoder;
6335 bool enable = false;
6336
6337 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6338 enable |= intel_encoder->connectors_active;
6339
6340 intel_crtc_control(crtc, enable);
cdd59983
CW
6341}
6342
ea5b213a 6343void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6344{
4ef69c7a 6345 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6346
ea5b213a
CW
6347 drm_encoder_cleanup(encoder);
6348 kfree(intel_encoder);
7e7d76c3
JB
6349}
6350
9237329d 6351/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6352 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6353 * state of the entire output pipe. */
9237329d 6354static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6355{
5ab432ef
DV
6356 if (mode == DRM_MODE_DPMS_ON) {
6357 encoder->connectors_active = true;
6358
b2cabb0e 6359 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6360 } else {
6361 encoder->connectors_active = false;
6362
b2cabb0e 6363 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6364 }
79e53945
JB
6365}
6366
0a91ca29
DV
6367/* Cross check the actual hw state with our own modeset state tracking (and it's
6368 * internal consistency). */
b980514c 6369static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6370{
0a91ca29
DV
6371 if (connector->get_hw_state(connector)) {
6372 struct intel_encoder *encoder = connector->encoder;
6373 struct drm_crtc *crtc;
6374 bool encoder_enabled;
6375 enum pipe pipe;
6376
6377 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6378 connector->base.base.id,
c23cc417 6379 connector->base.name);
0a91ca29 6380
0e32b39c
DA
6381 /* there is no real hw state for MST connectors */
6382 if (connector->mst_port)
6383 return;
6384
e2c719b7 6385 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6386 "wrong connector dpms state\n");
e2c719b7 6387 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6388 "active connector not linked to encoder\n");
0a91ca29 6389
36cd7444 6390 if (encoder) {
e2c719b7 6391 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6392 "encoder->connectors_active not set\n");
6393
6394 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6395 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6396 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6397 return;
0a91ca29 6398
36cd7444 6399 crtc = encoder->base.crtc;
0a91ca29 6400
83d65738
MR
6401 I915_STATE_WARN(!crtc->state->enable,
6402 "crtc not enabled\n");
e2c719b7
RC
6403 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6404 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6405 "encoder active on the wrong pipe\n");
6406 }
0a91ca29 6407 }
79e53945
JB
6408}
6409
08d9bc92
ACO
6410int intel_connector_init(struct intel_connector *connector)
6411{
6412 struct drm_connector_state *connector_state;
6413
6414 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6415 if (!connector_state)
6416 return -ENOMEM;
6417
6418 connector->base.state = connector_state;
6419 return 0;
6420}
6421
6422struct intel_connector *intel_connector_alloc(void)
6423{
6424 struct intel_connector *connector;
6425
6426 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6427 if (!connector)
6428 return NULL;
6429
6430 if (intel_connector_init(connector) < 0) {
6431 kfree(connector);
6432 return NULL;
6433 }
6434
6435 return connector;
6436}
6437
5ab432ef
DV
6438/* Even simpler default implementation, if there's really no special case to
6439 * consider. */
6440void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6441{
5ab432ef
DV
6442 /* All the simple cases only support two dpms states. */
6443 if (mode != DRM_MODE_DPMS_ON)
6444 mode = DRM_MODE_DPMS_OFF;
d4270e57 6445
5ab432ef
DV
6446 if (mode == connector->dpms)
6447 return;
6448
6449 connector->dpms = mode;
6450
6451 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6452 if (connector->encoder)
6453 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6454
b980514c 6455 intel_modeset_check_state(connector->dev);
79e53945
JB
6456}
6457
f0947c37
DV
6458/* Simple connector->get_hw_state implementation for encoders that support only
6459 * one connector and no cloning and hence the encoder state determines the state
6460 * of the connector. */
6461bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6462{
24929352 6463 enum pipe pipe = 0;
f0947c37 6464 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6465
f0947c37 6466 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6467}
6468
6d293983 6469static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6470{
6d293983
ACO
6471 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6472 return crtc_state->fdi_lanes;
d272ddfa
VS
6473
6474 return 0;
6475}
6476
6d293983 6477static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6478 struct intel_crtc_state *pipe_config)
1857e1da 6479{
6d293983
ACO
6480 struct drm_atomic_state *state = pipe_config->base.state;
6481 struct intel_crtc *other_crtc;
6482 struct intel_crtc_state *other_crtc_state;
6483
1857e1da
DV
6484 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6485 pipe_name(pipe), pipe_config->fdi_lanes);
6486 if (pipe_config->fdi_lanes > 4) {
6487 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6488 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6489 return -EINVAL;
1857e1da
DV
6490 }
6491
bafb6553 6492 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6493 if (pipe_config->fdi_lanes > 2) {
6494 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6495 pipe_config->fdi_lanes);
6d293983 6496 return -EINVAL;
1857e1da 6497 } else {
6d293983 6498 return 0;
1857e1da
DV
6499 }
6500 }
6501
6502 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6503 return 0;
1857e1da
DV
6504
6505 /* Ivybridge 3 pipe is really complicated */
6506 switch (pipe) {
6507 case PIPE_A:
6d293983 6508 return 0;
1857e1da 6509 case PIPE_B:
6d293983
ACO
6510 if (pipe_config->fdi_lanes <= 2)
6511 return 0;
6512
6513 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6514 other_crtc_state =
6515 intel_atomic_get_crtc_state(state, other_crtc);
6516 if (IS_ERR(other_crtc_state))
6517 return PTR_ERR(other_crtc_state);
6518
6519 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6520 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6521 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6522 return -EINVAL;
1857e1da 6523 }
6d293983 6524 return 0;
1857e1da 6525 case PIPE_C:
251cc67c
VS
6526 if (pipe_config->fdi_lanes > 2) {
6527 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6528 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6529 return -EINVAL;
251cc67c 6530 }
6d293983
ACO
6531
6532 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6533 other_crtc_state =
6534 intel_atomic_get_crtc_state(state, other_crtc);
6535 if (IS_ERR(other_crtc_state))
6536 return PTR_ERR(other_crtc_state);
6537
6538 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6539 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6540 return -EINVAL;
1857e1da 6541 }
6d293983 6542 return 0;
1857e1da
DV
6543 default:
6544 BUG();
6545 }
6546}
6547
e29c22c0
DV
6548#define RETRY 1
6549static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6550 struct intel_crtc_state *pipe_config)
877d48d5 6551{
1857e1da 6552 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6553 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6554 int lane, link_bw, fdi_dotclock, ret;
6555 bool needs_recompute = false;
877d48d5 6556
e29c22c0 6557retry:
877d48d5
DV
6558 /* FDI is a binary signal running at ~2.7GHz, encoding
6559 * each output octet as 10 bits. The actual frequency
6560 * is stored as a divider into a 100MHz clock, and the
6561 * mode pixel clock is stored in units of 1KHz.
6562 * Hence the bw of each lane in terms of the mode signal
6563 * is:
6564 */
6565 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6566
241bfc38 6567 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6568
2bd89a07 6569 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6570 pipe_config->pipe_bpp);
6571
6572 pipe_config->fdi_lanes = lane;
6573
2bd89a07 6574 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6575 link_bw, &pipe_config->fdi_m_n);
1857e1da 6576
6d293983
ACO
6577 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6578 intel_crtc->pipe, pipe_config);
6579 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6580 pipe_config->pipe_bpp -= 2*3;
6581 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6582 pipe_config->pipe_bpp);
6583 needs_recompute = true;
6584 pipe_config->bw_constrained = true;
6585
6586 goto retry;
6587 }
6588
6589 if (needs_recompute)
6590 return RETRY;
6591
6d293983 6592 return ret;
877d48d5
DV
6593}
6594
8cfb3407
VS
6595static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6596 struct intel_crtc_state *pipe_config)
6597{
6598 if (pipe_config->pipe_bpp > 24)
6599 return false;
6600
6601 /* HSW can handle pixel rate up to cdclk? */
6602 if (IS_HASWELL(dev_priv->dev))
6603 return true;
6604
6605 /*
b432e5cf
VS
6606 * We compare against max which means we must take
6607 * the increased cdclk requirement into account when
6608 * calculating the new cdclk.
6609 *
6610 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6611 */
6612 return ilk_pipe_pixel_rate(pipe_config) <=
6613 dev_priv->max_cdclk_freq * 95 / 100;
6614}
6615
42db64ef 6616static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6617 struct intel_crtc_state *pipe_config)
42db64ef 6618{
8cfb3407
VS
6619 struct drm_device *dev = crtc->base.dev;
6620 struct drm_i915_private *dev_priv = dev->dev_private;
6621
d330a953 6622 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6623 hsw_crtc_supports_ips(crtc) &&
6624 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6625}
6626
a43f6e0f 6627static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6628 struct intel_crtc_state *pipe_config)
79e53945 6629{
a43f6e0f 6630 struct drm_device *dev = crtc->base.dev;
8bd31e67 6631 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6632 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6633
ad3a4479 6634 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6635 if (INTEL_INFO(dev)->gen < 4) {
44913155 6636 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6637
6638 /*
6639 * Enable pixel doubling when the dot clock
6640 * is > 90% of the (display) core speed.
6641 *
b397c96b
VS
6642 * GDG double wide on either pipe,
6643 * otherwise pipe A only.
cf532bb2 6644 */
b397c96b 6645 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6646 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6647 clock_limit *= 2;
cf532bb2 6648 pipe_config->double_wide = true;
ad3a4479
VS
6649 }
6650
241bfc38 6651 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6652 return -EINVAL;
2c07245f 6653 }
89749350 6654
1d1d0e27
VS
6655 /*
6656 * Pipe horizontal size must be even in:
6657 * - DVO ganged mode
6658 * - LVDS dual channel mode
6659 * - Double wide pipe
6660 */
a93e255f 6661 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6662 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6663 pipe_config->pipe_src_w &= ~1;
6664
8693a824
DL
6665 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6666 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6667 */
6668 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6669 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6670 return -EINVAL;
44f46b42 6671
f5adf94e 6672 if (HAS_IPS(dev))
a43f6e0f
DV
6673 hsw_compute_ips_config(crtc, pipe_config);
6674
877d48d5 6675 if (pipe_config->has_pch_encoder)
a43f6e0f 6676 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6677
cf5a15be 6678 return 0;
79e53945
JB
6679}
6680
1652d19e
VS
6681static int skylake_get_display_clock_speed(struct drm_device *dev)
6682{
6683 struct drm_i915_private *dev_priv = to_i915(dev);
6684 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6685 uint32_t cdctl = I915_READ(CDCLK_CTL);
6686 uint32_t linkrate;
6687
414355a7 6688 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6689 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6690
6691 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6692 return 540000;
6693
6694 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6695 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6696
71cd8423
DL
6697 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6698 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6699 /* vco 8640 */
6700 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6701 case CDCLK_FREQ_450_432:
6702 return 432000;
6703 case CDCLK_FREQ_337_308:
6704 return 308570;
6705 case CDCLK_FREQ_675_617:
6706 return 617140;
6707 default:
6708 WARN(1, "Unknown cd freq selection\n");
6709 }
6710 } else {
6711 /* vco 8100 */
6712 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6713 case CDCLK_FREQ_450_432:
6714 return 450000;
6715 case CDCLK_FREQ_337_308:
6716 return 337500;
6717 case CDCLK_FREQ_675_617:
6718 return 675000;
6719 default:
6720 WARN(1, "Unknown cd freq selection\n");
6721 }
6722 }
6723
6724 /* error case, do as if DPLL0 isn't enabled */
6725 return 24000;
6726}
6727
acd3f3d3
BP
6728static int broxton_get_display_clock_speed(struct drm_device *dev)
6729{
6730 struct drm_i915_private *dev_priv = to_i915(dev);
6731 uint32_t cdctl = I915_READ(CDCLK_CTL);
6732 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6733 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6734 int cdclk;
6735
6736 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6737 return 19200;
6738
6739 cdclk = 19200 * pll_ratio / 2;
6740
6741 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6742 case BXT_CDCLK_CD2X_DIV_SEL_1:
6743 return cdclk; /* 576MHz or 624MHz */
6744 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6745 return cdclk * 2 / 3; /* 384MHz */
6746 case BXT_CDCLK_CD2X_DIV_SEL_2:
6747 return cdclk / 2; /* 288MHz */
6748 case BXT_CDCLK_CD2X_DIV_SEL_4:
6749 return cdclk / 4; /* 144MHz */
6750 }
6751
6752 /* error case, do as if DE PLL isn't enabled */
6753 return 19200;
6754}
6755
1652d19e
VS
6756static int broadwell_get_display_clock_speed(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = dev->dev_private;
6759 uint32_t lcpll = I915_READ(LCPLL_CTL);
6760 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6761
6762 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6763 return 800000;
6764 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6765 return 450000;
6766 else if (freq == LCPLL_CLK_FREQ_450)
6767 return 450000;
6768 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6769 return 540000;
6770 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6771 return 337500;
6772 else
6773 return 675000;
6774}
6775
6776static int haswell_get_display_clock_speed(struct drm_device *dev)
6777{
6778 struct drm_i915_private *dev_priv = dev->dev_private;
6779 uint32_t lcpll = I915_READ(LCPLL_CTL);
6780 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6781
6782 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6783 return 800000;
6784 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6785 return 450000;
6786 else if (freq == LCPLL_CLK_FREQ_450)
6787 return 450000;
6788 else if (IS_HSW_ULT(dev))
6789 return 337500;
6790 else
6791 return 540000;
79e53945
JB
6792}
6793
25eb05fc
JB
6794static int valleyview_get_display_clock_speed(struct drm_device *dev)
6795{
d197b7d3 6796 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6797 u32 val;
6798 int divider;
6799
6bcda4f0
VS
6800 if (dev_priv->hpll_freq == 0)
6801 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6802
a580516d 6803 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6804 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6805 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6806
6807 divider = val & DISPLAY_FREQUENCY_VALUES;
6808
7d007f40
VS
6809 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6810 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6811 "cdclk change in progress\n");
6812
6bcda4f0 6813 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6814}
6815
b37a6434
VS
6816static int ilk_get_display_clock_speed(struct drm_device *dev)
6817{
6818 return 450000;
6819}
6820
e70236a8
JB
6821static int i945_get_display_clock_speed(struct drm_device *dev)
6822{
6823 return 400000;
6824}
79e53945 6825
e70236a8 6826static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6827{
e907f170 6828 return 333333;
e70236a8 6829}
79e53945 6830
e70236a8
JB
6831static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6832{
6833 return 200000;
6834}
79e53945 6835
257a7ffc
DV
6836static int pnv_get_display_clock_speed(struct drm_device *dev)
6837{
6838 u16 gcfgc = 0;
6839
6840 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6841
6842 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6843 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6844 return 266667;
257a7ffc 6845 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6846 return 333333;
257a7ffc 6847 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6848 return 444444;
257a7ffc
DV
6849 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6850 return 200000;
6851 default:
6852 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6853 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6854 return 133333;
257a7ffc 6855 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6856 return 166667;
257a7ffc
DV
6857 }
6858}
6859
e70236a8
JB
6860static int i915gm_get_display_clock_speed(struct drm_device *dev)
6861{
6862 u16 gcfgc = 0;
79e53945 6863
e70236a8
JB
6864 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6865
6866 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6867 return 133333;
e70236a8
JB
6868 else {
6869 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6870 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6871 return 333333;
e70236a8
JB
6872 default:
6873 case GC_DISPLAY_CLOCK_190_200_MHZ:
6874 return 190000;
79e53945 6875 }
e70236a8
JB
6876 }
6877}
6878
6879static int i865_get_display_clock_speed(struct drm_device *dev)
6880{
e907f170 6881 return 266667;
e70236a8
JB
6882}
6883
1b1d2716 6884static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6885{
6886 u16 hpllcc = 0;
1b1d2716 6887
65cd2b3f
VS
6888 /*
6889 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6890 * encoding is different :(
6891 * FIXME is this the right way to detect 852GM/852GMV?
6892 */
6893 if (dev->pdev->revision == 0x1)
6894 return 133333;
6895
1b1d2716
VS
6896 pci_bus_read_config_word(dev->pdev->bus,
6897 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6898
e70236a8
JB
6899 /* Assume that the hardware is in the high speed state. This
6900 * should be the default.
6901 */
6902 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6903 case GC_CLOCK_133_200:
1b1d2716 6904 case GC_CLOCK_133_200_2:
e70236a8
JB
6905 case GC_CLOCK_100_200:
6906 return 200000;
6907 case GC_CLOCK_166_250:
6908 return 250000;
6909 case GC_CLOCK_100_133:
e907f170 6910 return 133333;
1b1d2716
VS
6911 case GC_CLOCK_133_266:
6912 case GC_CLOCK_133_266_2:
6913 case GC_CLOCK_166_266:
6914 return 266667;
e70236a8 6915 }
79e53945 6916
e70236a8
JB
6917 /* Shouldn't happen */
6918 return 0;
6919}
79e53945 6920
e70236a8
JB
6921static int i830_get_display_clock_speed(struct drm_device *dev)
6922{
e907f170 6923 return 133333;
79e53945
JB
6924}
6925
34edce2f
VS
6926static unsigned int intel_hpll_vco(struct drm_device *dev)
6927{
6928 struct drm_i915_private *dev_priv = dev->dev_private;
6929 static const unsigned int blb_vco[8] = {
6930 [0] = 3200000,
6931 [1] = 4000000,
6932 [2] = 5333333,
6933 [3] = 4800000,
6934 [4] = 6400000,
6935 };
6936 static const unsigned int pnv_vco[8] = {
6937 [0] = 3200000,
6938 [1] = 4000000,
6939 [2] = 5333333,
6940 [3] = 4800000,
6941 [4] = 2666667,
6942 };
6943 static const unsigned int cl_vco[8] = {
6944 [0] = 3200000,
6945 [1] = 4000000,
6946 [2] = 5333333,
6947 [3] = 6400000,
6948 [4] = 3333333,
6949 [5] = 3566667,
6950 [6] = 4266667,
6951 };
6952 static const unsigned int elk_vco[8] = {
6953 [0] = 3200000,
6954 [1] = 4000000,
6955 [2] = 5333333,
6956 [3] = 4800000,
6957 };
6958 static const unsigned int ctg_vco[8] = {
6959 [0] = 3200000,
6960 [1] = 4000000,
6961 [2] = 5333333,
6962 [3] = 6400000,
6963 [4] = 2666667,
6964 [5] = 4266667,
6965 };
6966 const unsigned int *vco_table;
6967 unsigned int vco;
6968 uint8_t tmp = 0;
6969
6970 /* FIXME other chipsets? */
6971 if (IS_GM45(dev))
6972 vco_table = ctg_vco;
6973 else if (IS_G4X(dev))
6974 vco_table = elk_vco;
6975 else if (IS_CRESTLINE(dev))
6976 vco_table = cl_vco;
6977 else if (IS_PINEVIEW(dev))
6978 vco_table = pnv_vco;
6979 else if (IS_G33(dev))
6980 vco_table = blb_vco;
6981 else
6982 return 0;
6983
6984 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6985
6986 vco = vco_table[tmp & 0x7];
6987 if (vco == 0)
6988 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6989 else
6990 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6991
6992 return vco;
6993}
6994
6995static int gm45_get_display_clock_speed(struct drm_device *dev)
6996{
6997 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6998 uint16_t tmp = 0;
6999
7000 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7001
7002 cdclk_sel = (tmp >> 12) & 0x1;
7003
7004 switch (vco) {
7005 case 2666667:
7006 case 4000000:
7007 case 5333333:
7008 return cdclk_sel ? 333333 : 222222;
7009 case 3200000:
7010 return cdclk_sel ? 320000 : 228571;
7011 default:
7012 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7013 return 222222;
7014 }
7015}
7016
7017static int i965gm_get_display_clock_speed(struct drm_device *dev)
7018{
7019 static const uint8_t div_3200[] = { 16, 10, 8 };
7020 static const uint8_t div_4000[] = { 20, 12, 10 };
7021 static const uint8_t div_5333[] = { 24, 16, 14 };
7022 const uint8_t *div_table;
7023 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7024 uint16_t tmp = 0;
7025
7026 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7027
7028 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7029
7030 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7031 goto fail;
7032
7033 switch (vco) {
7034 case 3200000:
7035 div_table = div_3200;
7036 break;
7037 case 4000000:
7038 div_table = div_4000;
7039 break;
7040 case 5333333:
7041 div_table = div_5333;
7042 break;
7043 default:
7044 goto fail;
7045 }
7046
7047 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7048
caf4e252 7049fail:
34edce2f
VS
7050 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7051 return 200000;
7052}
7053
7054static int g33_get_display_clock_speed(struct drm_device *dev)
7055{
7056 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7057 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7058 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7059 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7060 const uint8_t *div_table;
7061 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7062 uint16_t tmp = 0;
7063
7064 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7065
7066 cdclk_sel = (tmp >> 4) & 0x7;
7067
7068 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7069 goto fail;
7070
7071 switch (vco) {
7072 case 3200000:
7073 div_table = div_3200;
7074 break;
7075 case 4000000:
7076 div_table = div_4000;
7077 break;
7078 case 4800000:
7079 div_table = div_4800;
7080 break;
7081 case 5333333:
7082 div_table = div_5333;
7083 break;
7084 default:
7085 goto fail;
7086 }
7087
7088 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7089
caf4e252 7090fail:
34edce2f
VS
7091 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7092 return 190476;
7093}
7094
2c07245f 7095static void
a65851af 7096intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7097{
a65851af
VS
7098 while (*num > DATA_LINK_M_N_MASK ||
7099 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7100 *num >>= 1;
7101 *den >>= 1;
7102 }
7103}
7104
a65851af
VS
7105static void compute_m_n(unsigned int m, unsigned int n,
7106 uint32_t *ret_m, uint32_t *ret_n)
7107{
7108 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7109 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7110 intel_reduce_m_n_ratio(ret_m, ret_n);
7111}
7112
e69d0bc1
DV
7113void
7114intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7115 int pixel_clock, int link_clock,
7116 struct intel_link_m_n *m_n)
2c07245f 7117{
e69d0bc1 7118 m_n->tu = 64;
a65851af
VS
7119
7120 compute_m_n(bits_per_pixel * pixel_clock,
7121 link_clock * nlanes * 8,
7122 &m_n->gmch_m, &m_n->gmch_n);
7123
7124 compute_m_n(pixel_clock, link_clock,
7125 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7126}
7127
a7615030
CW
7128static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7129{
d330a953
JN
7130 if (i915.panel_use_ssc >= 0)
7131 return i915.panel_use_ssc != 0;
41aa3448 7132 return dev_priv->vbt.lvds_use_ssc
435793df 7133 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7134}
7135
a93e255f
ACO
7136static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7137 int num_connectors)
c65d77d8 7138{
a93e255f 7139 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7140 struct drm_i915_private *dev_priv = dev->dev_private;
7141 int refclk;
7142
a93e255f
ACO
7143 WARN_ON(!crtc_state->base.state);
7144
5ab7b0b7 7145 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7146 refclk = 100000;
a93e255f 7147 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7148 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7149 refclk = dev_priv->vbt.lvds_ssc_freq;
7150 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7151 } else if (!IS_GEN2(dev)) {
7152 refclk = 96000;
7153 } else {
7154 refclk = 48000;
7155 }
7156
7157 return refclk;
7158}
7159
7429e9d4 7160static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7161{
7df00d7a 7162 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7163}
f47709a9 7164
7429e9d4
DV
7165static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7166{
7167 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7168}
7169
f47709a9 7170static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7171 struct intel_crtc_state *crtc_state,
a7516a05
JB
7172 intel_clock_t *reduced_clock)
7173{
f47709a9 7174 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7175 u32 fp, fp2 = 0;
7176
7177 if (IS_PINEVIEW(dev)) {
190f68c5 7178 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7179 if (reduced_clock)
7429e9d4 7180 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7181 } else {
190f68c5 7182 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7183 if (reduced_clock)
7429e9d4 7184 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7185 }
7186
190f68c5 7187 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7188
f47709a9 7189 crtc->lowfreq_avail = false;
a93e255f 7190 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7191 reduced_clock) {
190f68c5 7192 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7193 crtc->lowfreq_avail = true;
a7516a05 7194 } else {
190f68c5 7195 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7196 }
7197}
7198
5e69f97f
CML
7199static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7200 pipe)
89b667f8
JB
7201{
7202 u32 reg_val;
7203
7204 /*
7205 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7206 * and set it to a reasonable value instead.
7207 */
ab3c759a 7208 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7209 reg_val &= 0xffffff00;
7210 reg_val |= 0x00000030;
ab3c759a 7211 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7212
ab3c759a 7213 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7214 reg_val &= 0x8cffffff;
7215 reg_val = 0x8c000000;
ab3c759a 7216 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7217
ab3c759a 7218 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7219 reg_val &= 0xffffff00;
ab3c759a 7220 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7221
ab3c759a 7222 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7223 reg_val &= 0x00ffffff;
7224 reg_val |= 0xb0000000;
ab3c759a 7225 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7226}
7227
b551842d
DV
7228static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7229 struct intel_link_m_n *m_n)
7230{
7231 struct drm_device *dev = crtc->base.dev;
7232 struct drm_i915_private *dev_priv = dev->dev_private;
7233 int pipe = crtc->pipe;
7234
e3b95f1e
DV
7235 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7236 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7237 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7238 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7239}
7240
7241static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7242 struct intel_link_m_n *m_n,
7243 struct intel_link_m_n *m2_n2)
b551842d
DV
7244{
7245 struct drm_device *dev = crtc->base.dev;
7246 struct drm_i915_private *dev_priv = dev->dev_private;
7247 int pipe = crtc->pipe;
6e3c9717 7248 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7249
7250 if (INTEL_INFO(dev)->gen >= 5) {
7251 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7252 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7253 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7254 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7255 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7256 * for gen < 8) and if DRRS is supported (to make sure the
7257 * registers are not unnecessarily accessed).
7258 */
44395bfe 7259 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7260 crtc->config->has_drrs) {
f769cd24
VK
7261 I915_WRITE(PIPE_DATA_M2(transcoder),
7262 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7263 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7264 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7265 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7266 }
b551842d 7267 } else {
e3b95f1e
DV
7268 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7269 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7270 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7271 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7272 }
7273}
7274
fe3cd48d 7275void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7276{
fe3cd48d
R
7277 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7278
7279 if (m_n == M1_N1) {
7280 dp_m_n = &crtc->config->dp_m_n;
7281 dp_m2_n2 = &crtc->config->dp_m2_n2;
7282 } else if (m_n == M2_N2) {
7283
7284 /*
7285 * M2_N2 registers are not supported. Hence m2_n2 divider value
7286 * needs to be programmed into M1_N1.
7287 */
7288 dp_m_n = &crtc->config->dp_m2_n2;
7289 } else {
7290 DRM_ERROR("Unsupported divider value\n");
7291 return;
7292 }
7293
6e3c9717
ACO
7294 if (crtc->config->has_pch_encoder)
7295 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7296 else
fe3cd48d 7297 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7298}
7299
251ac862
DV
7300static void vlv_compute_dpll(struct intel_crtc *crtc,
7301 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7302{
7303 u32 dpll, dpll_md;
7304
7305 /*
7306 * Enable DPIO clock input. We should never disable the reference
7307 * clock for pipe B, since VGA hotplug / manual detection depends
7308 * on it.
7309 */
60bfe44f
VS
7310 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7311 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7312 /* We should never disable this, set it here for state tracking */
7313 if (crtc->pipe == PIPE_B)
7314 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7315 dpll |= DPLL_VCO_ENABLE;
d288f65f 7316 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7317
d288f65f 7318 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7319 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7320 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7321}
7322
d288f65f 7323static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7324 const struct intel_crtc_state *pipe_config)
a0c4da24 7325{
f47709a9 7326 struct drm_device *dev = crtc->base.dev;
a0c4da24 7327 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7328 int pipe = crtc->pipe;
bdd4b6a6 7329 u32 mdiv;
a0c4da24 7330 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7331 u32 coreclk, reg_val;
a0c4da24 7332
a580516d 7333 mutex_lock(&dev_priv->sb_lock);
09153000 7334
d288f65f
VS
7335 bestn = pipe_config->dpll.n;
7336 bestm1 = pipe_config->dpll.m1;
7337 bestm2 = pipe_config->dpll.m2;
7338 bestp1 = pipe_config->dpll.p1;
7339 bestp2 = pipe_config->dpll.p2;
a0c4da24 7340
89b667f8
JB
7341 /* See eDP HDMI DPIO driver vbios notes doc */
7342
7343 /* PLL B needs special handling */
bdd4b6a6 7344 if (pipe == PIPE_B)
5e69f97f 7345 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7346
7347 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7349
7350 /* Disable target IRef on PLL */
ab3c759a 7351 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7352 reg_val &= 0x00ffffff;
ab3c759a 7353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7354
7355 /* Disable fast lock */
ab3c759a 7356 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7357
7358 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7359 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7360 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7361 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7362 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7363
7364 /*
7365 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7366 * but we don't support that).
7367 * Note: don't use the DAC post divider as it seems unstable.
7368 */
7369 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7371
a0c4da24 7372 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7374
89b667f8 7375 /* Set HBR and RBR LPF coefficients */
d288f65f 7376 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7377 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7378 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7380 0x009f0003);
89b667f8 7381 else
ab3c759a 7382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7383 0x00d0000f);
7384
681a8504 7385 if (pipe_config->has_dp_encoder) {
89b667f8 7386 /* Use SSC source */
bdd4b6a6 7387 if (pipe == PIPE_A)
ab3c759a 7388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7389 0x0df40000);
7390 else
ab3c759a 7391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7392 0x0df70000);
7393 } else { /* HDMI or VGA */
7394 /* Use bend source */
bdd4b6a6 7395 if (pipe == PIPE_A)
ab3c759a 7396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7397 0x0df70000);
7398 else
ab3c759a 7399 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7400 0x0df40000);
7401 }
a0c4da24 7402
ab3c759a 7403 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7404 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7405 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7406 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7407 coreclk |= 0x01000000;
ab3c759a 7408 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7409
ab3c759a 7410 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7411 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7412}
7413
251ac862
DV
7414static void chv_compute_dpll(struct intel_crtc *crtc,
7415 struct intel_crtc_state *pipe_config)
1ae0d137 7416{
60bfe44f
VS
7417 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7418 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7419 DPLL_VCO_ENABLE;
7420 if (crtc->pipe != PIPE_A)
d288f65f 7421 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7422
d288f65f
VS
7423 pipe_config->dpll_hw_state.dpll_md =
7424 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7425}
7426
d288f65f 7427static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7428 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7429{
7430 struct drm_device *dev = crtc->base.dev;
7431 struct drm_i915_private *dev_priv = dev->dev_private;
7432 int pipe = crtc->pipe;
7433 int dpll_reg = DPLL(crtc->pipe);
7434 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7435 u32 loopfilter, tribuf_calcntr;
9d556c99 7436 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7437 u32 dpio_val;
9cbe40c1 7438 int vco;
9d556c99 7439
d288f65f
VS
7440 bestn = pipe_config->dpll.n;
7441 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7442 bestm1 = pipe_config->dpll.m1;
7443 bestm2 = pipe_config->dpll.m2 >> 22;
7444 bestp1 = pipe_config->dpll.p1;
7445 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7446 vco = pipe_config->dpll.vco;
a945ce7e 7447 dpio_val = 0;
9cbe40c1 7448 loopfilter = 0;
9d556c99
CML
7449
7450 /*
7451 * Enable Refclk and SSC
7452 */
a11b0703 7453 I915_WRITE(dpll_reg,
d288f65f 7454 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7455
a580516d 7456 mutex_lock(&dev_priv->sb_lock);
9d556c99 7457
9d556c99
CML
7458 /* p1 and p2 divider */
7459 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7460 5 << DPIO_CHV_S1_DIV_SHIFT |
7461 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7462 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7463 1 << DPIO_CHV_K_DIV_SHIFT);
7464
7465 /* Feedback post-divider - m2 */
7466 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7467
7468 /* Feedback refclk divider - n and m1 */
7469 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7470 DPIO_CHV_M1_DIV_BY_2 |
7471 1 << DPIO_CHV_N_DIV_SHIFT);
7472
7473 /* M2 fraction division */
a945ce7e
VP
7474 if (bestm2_frac)
7475 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7476
7477 /* M2 fraction division enable */
a945ce7e
VP
7478 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7479 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7480 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7481 if (bestm2_frac)
7482 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7483 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7484
de3a0fde
VP
7485 /* Program digital lock detect threshold */
7486 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7487 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7488 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7489 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7490 if (!bestm2_frac)
7491 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7492 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7493
9d556c99 7494 /* Loop filter */
9cbe40c1
VP
7495 if (vco == 5400000) {
7496 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7497 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7498 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7499 tribuf_calcntr = 0x9;
7500 } else if (vco <= 6200000) {
7501 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7502 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7503 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7504 tribuf_calcntr = 0x9;
7505 } else if (vco <= 6480000) {
7506 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7507 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7508 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7509 tribuf_calcntr = 0x8;
7510 } else {
7511 /* Not supported. Apply the same limits as in the max case */
7512 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7513 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7514 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7515 tribuf_calcntr = 0;
7516 }
9d556c99
CML
7517 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7518
968040b2 7519 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7520 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7521 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7522 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7523
9d556c99
CML
7524 /* AFC Recal */
7525 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7526 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7527 DPIO_AFC_RECAL);
7528
a580516d 7529 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7530}
7531
d288f65f
VS
7532/**
7533 * vlv_force_pll_on - forcibly enable just the PLL
7534 * @dev_priv: i915 private structure
7535 * @pipe: pipe PLL to enable
7536 * @dpll: PLL configuration
7537 *
7538 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7539 * in cases where we need the PLL enabled even when @pipe is not going to
7540 * be enabled.
7541 */
7542void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7543 const struct dpll *dpll)
7544{
7545 struct intel_crtc *crtc =
7546 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7547 struct intel_crtc_state pipe_config = {
a93e255f 7548 .base.crtc = &crtc->base,
d288f65f
VS
7549 .pixel_multiplier = 1,
7550 .dpll = *dpll,
7551 };
7552
7553 if (IS_CHERRYVIEW(dev)) {
251ac862 7554 chv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7555 chv_prepare_pll(crtc, &pipe_config);
7556 chv_enable_pll(crtc, &pipe_config);
7557 } else {
251ac862 7558 vlv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7559 vlv_prepare_pll(crtc, &pipe_config);
7560 vlv_enable_pll(crtc, &pipe_config);
7561 }
7562}
7563
7564/**
7565 * vlv_force_pll_off - forcibly disable just the PLL
7566 * @dev_priv: i915 private structure
7567 * @pipe: pipe PLL to disable
7568 *
7569 * Disable the PLL for @pipe. To be used in cases where we need
7570 * the PLL enabled even when @pipe is not going to be enabled.
7571 */
7572void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7573{
7574 if (IS_CHERRYVIEW(dev))
7575 chv_disable_pll(to_i915(dev), pipe);
7576 else
7577 vlv_disable_pll(to_i915(dev), pipe);
7578}
7579
251ac862
DV
7580static void i9xx_compute_dpll(struct intel_crtc *crtc,
7581 struct intel_crtc_state *crtc_state,
7582 intel_clock_t *reduced_clock,
7583 int num_connectors)
eb1cbe48 7584{
f47709a9 7585 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7586 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7587 u32 dpll;
7588 bool is_sdvo;
190f68c5 7589 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7590
190f68c5 7591 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7592
a93e255f
ACO
7593 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7594 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7595
7596 dpll = DPLL_VGA_MODE_DIS;
7597
a93e255f 7598 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7599 dpll |= DPLLB_MODE_LVDS;
7600 else
7601 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7602
ef1b460d 7603 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7604 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7605 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7606 }
198a037f
DV
7607
7608 if (is_sdvo)
4a33e48d 7609 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7610
190f68c5 7611 if (crtc_state->has_dp_encoder)
4a33e48d 7612 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7613
7614 /* compute bitmask from p1 value */
7615 if (IS_PINEVIEW(dev))
7616 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7617 else {
7618 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7619 if (IS_G4X(dev) && reduced_clock)
7620 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7621 }
7622 switch (clock->p2) {
7623 case 5:
7624 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7625 break;
7626 case 7:
7627 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7628 break;
7629 case 10:
7630 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7631 break;
7632 case 14:
7633 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7634 break;
7635 }
7636 if (INTEL_INFO(dev)->gen >= 4)
7637 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7638
190f68c5 7639 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7640 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7641 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7642 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7643 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7644 else
7645 dpll |= PLL_REF_INPUT_DREFCLK;
7646
7647 dpll |= DPLL_VCO_ENABLE;
190f68c5 7648 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7649
eb1cbe48 7650 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7651 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7652 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7653 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7654 }
7655}
7656
251ac862
DV
7657static void i8xx_compute_dpll(struct intel_crtc *crtc,
7658 struct intel_crtc_state *crtc_state,
7659 intel_clock_t *reduced_clock,
7660 int num_connectors)
eb1cbe48 7661{
f47709a9 7662 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7663 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7664 u32 dpll;
190f68c5 7665 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7666
190f68c5 7667 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7668
eb1cbe48
DV
7669 dpll = DPLL_VGA_MODE_DIS;
7670
a93e255f 7671 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7672 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7673 } else {
7674 if (clock->p1 == 2)
7675 dpll |= PLL_P1_DIVIDE_BY_TWO;
7676 else
7677 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7678 if (clock->p2 == 4)
7679 dpll |= PLL_P2_DIVIDE_BY_4;
7680 }
7681
a93e255f 7682 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7683 dpll |= DPLL_DVO_2X_MODE;
7684
a93e255f 7685 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7686 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7687 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7688 else
7689 dpll |= PLL_REF_INPUT_DREFCLK;
7690
7691 dpll |= DPLL_VCO_ENABLE;
190f68c5 7692 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7693}
7694
8a654f3b 7695static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7696{
7697 struct drm_device *dev = intel_crtc->base.dev;
7698 struct drm_i915_private *dev_priv = dev->dev_private;
7699 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7700 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7701 struct drm_display_mode *adjusted_mode =
6e3c9717 7702 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7703 uint32_t crtc_vtotal, crtc_vblank_end;
7704 int vsyncshift = 0;
4d8a62ea
DV
7705
7706 /* We need to be careful not to changed the adjusted mode, for otherwise
7707 * the hw state checker will get angry at the mismatch. */
7708 crtc_vtotal = adjusted_mode->crtc_vtotal;
7709 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7710
609aeaca 7711 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7712 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7713 crtc_vtotal -= 1;
7714 crtc_vblank_end -= 1;
609aeaca 7715
409ee761 7716 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7717 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7718 else
7719 vsyncshift = adjusted_mode->crtc_hsync_start -
7720 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7721 if (vsyncshift < 0)
7722 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7723 }
7724
7725 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7726 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7727
fe2b8f9d 7728 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7729 (adjusted_mode->crtc_hdisplay - 1) |
7730 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7731 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7732 (adjusted_mode->crtc_hblank_start - 1) |
7733 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7734 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7735 (adjusted_mode->crtc_hsync_start - 1) |
7736 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7737
fe2b8f9d 7738 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7739 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7740 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7741 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7742 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7743 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7744 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7745 (adjusted_mode->crtc_vsync_start - 1) |
7746 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7747
b5e508d4
PZ
7748 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7749 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7750 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7751 * bits. */
7752 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7753 (pipe == PIPE_B || pipe == PIPE_C))
7754 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7755
b0e77b9c
PZ
7756 /* pipesrc controls the size that is scaled from, which should
7757 * always be the user's requested size.
7758 */
7759 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7760 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7761 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7762}
7763
1bd1bd80 7764static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7765 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7766{
7767 struct drm_device *dev = crtc->base.dev;
7768 struct drm_i915_private *dev_priv = dev->dev_private;
7769 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7770 uint32_t tmp;
7771
7772 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7773 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7775 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7776 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7777 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7778 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7779 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7780 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7781
7782 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7783 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7785 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7786 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7787 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7788 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7789 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7790 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7791
7792 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7793 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7794 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7795 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7796 }
7797
7798 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7799 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7800 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7801
2d112de7
ACO
7802 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7803 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7804}
7805
f6a83288 7806void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7807 struct intel_crtc_state *pipe_config)
babea61d 7808{
2d112de7
ACO
7809 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7810 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7811 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7812 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7813
2d112de7
ACO
7814 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7815 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7816 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7817 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7818
2d112de7 7819 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7820 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7821
2d112de7
ACO
7822 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7823 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7824
7825 mode->hsync = drm_mode_hsync(mode);
7826 mode->vrefresh = drm_mode_vrefresh(mode);
7827 drm_mode_set_name(mode);
babea61d
JB
7828}
7829
84b046f3
DV
7830static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7831{
7832 struct drm_device *dev = intel_crtc->base.dev;
7833 struct drm_i915_private *dev_priv = dev->dev_private;
7834 uint32_t pipeconf;
7835
9f11a9e4 7836 pipeconf = 0;
84b046f3 7837
b6b5d049
VS
7838 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7839 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7840 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7841
6e3c9717 7842 if (intel_crtc->config->double_wide)
cf532bb2 7843 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7844
ff9ce46e
DV
7845 /* only g4x and later have fancy bpc/dither controls */
7846 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7847 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7848 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7849 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7850 PIPECONF_DITHER_TYPE_SP;
84b046f3 7851
6e3c9717 7852 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7853 case 18:
7854 pipeconf |= PIPECONF_6BPC;
7855 break;
7856 case 24:
7857 pipeconf |= PIPECONF_8BPC;
7858 break;
7859 case 30:
7860 pipeconf |= PIPECONF_10BPC;
7861 break;
7862 default:
7863 /* Case prevented by intel_choose_pipe_bpp_dither. */
7864 BUG();
84b046f3
DV
7865 }
7866 }
7867
7868 if (HAS_PIPE_CXSR(dev)) {
7869 if (intel_crtc->lowfreq_avail) {
7870 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7871 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7872 } else {
7873 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7874 }
7875 }
7876
6e3c9717 7877 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7878 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7879 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7880 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7881 else
7882 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7883 } else
84b046f3
DV
7884 pipeconf |= PIPECONF_PROGRESSIVE;
7885
6e3c9717 7886 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7887 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7888
84b046f3
DV
7889 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7890 POSTING_READ(PIPECONF(intel_crtc->pipe));
7891}
7892
190f68c5
ACO
7893static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7894 struct intel_crtc_state *crtc_state)
79e53945 7895{
c7653199 7896 struct drm_device *dev = crtc->base.dev;
79e53945 7897 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7898 int refclk, num_connectors = 0;
c329a4ec
DV
7899 intel_clock_t clock;
7900 bool ok;
7901 bool is_dsi = false;
5eddb70b 7902 struct intel_encoder *encoder;
d4906093 7903 const intel_limit_t *limit;
55bb9992 7904 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7905 struct drm_connector *connector;
55bb9992
ACO
7906 struct drm_connector_state *connector_state;
7907 int i;
79e53945 7908
dd3cd74a
ACO
7909 memset(&crtc_state->dpll_hw_state, 0,
7910 sizeof(crtc_state->dpll_hw_state));
7911
da3ced29 7912 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7913 if (connector_state->crtc != &crtc->base)
7914 continue;
7915
7916 encoder = to_intel_encoder(connector_state->best_encoder);
7917
5eddb70b 7918 switch (encoder->type) {
e9fd1c02
JN
7919 case INTEL_OUTPUT_DSI:
7920 is_dsi = true;
7921 break;
6847d71b
PZ
7922 default:
7923 break;
79e53945 7924 }
43565a06 7925
c751ce4f 7926 num_connectors++;
79e53945
JB
7927 }
7928
f2335330 7929 if (is_dsi)
5b18e57c 7930 return 0;
f2335330 7931
190f68c5 7932 if (!crtc_state->clock_set) {
a93e255f 7933 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7934
e9fd1c02
JN
7935 /*
7936 * Returns a set of divisors for the desired target clock with
7937 * the given refclk, or FALSE. The returned values represent
7938 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7939 * 2) / p1 / p2.
7940 */
a93e255f
ACO
7941 limit = intel_limit(crtc_state, refclk);
7942 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7943 crtc_state->port_clock,
e9fd1c02 7944 refclk, NULL, &clock);
f2335330 7945 if (!ok) {
e9fd1c02
JN
7946 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7947 return -EINVAL;
7948 }
79e53945 7949
f2335330 7950 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7951 crtc_state->dpll.n = clock.n;
7952 crtc_state->dpll.m1 = clock.m1;
7953 crtc_state->dpll.m2 = clock.m2;
7954 crtc_state->dpll.p1 = clock.p1;
7955 crtc_state->dpll.p2 = clock.p2;
f47709a9 7956 }
7026d4ac 7957
e9fd1c02 7958 if (IS_GEN2(dev)) {
c329a4ec 7959 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7960 num_connectors);
9d556c99 7961 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7962 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7963 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7964 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7965 } else {
c329a4ec 7966 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7967 num_connectors);
e9fd1c02 7968 }
79e53945 7969
c8f7a0db 7970 return 0;
f564048e
EA
7971}
7972
2fa2fe9a 7973static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7974 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7975{
7976 struct drm_device *dev = crtc->base.dev;
7977 struct drm_i915_private *dev_priv = dev->dev_private;
7978 uint32_t tmp;
7979
dc9e7dec
VS
7980 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7981 return;
7982
2fa2fe9a 7983 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7984 if (!(tmp & PFIT_ENABLE))
7985 return;
2fa2fe9a 7986
06922821 7987 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7988 if (INTEL_INFO(dev)->gen < 4) {
7989 if (crtc->pipe != PIPE_B)
7990 return;
2fa2fe9a
DV
7991 } else {
7992 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7993 return;
7994 }
7995
06922821 7996 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7997 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7998 if (INTEL_INFO(dev)->gen < 5)
7999 pipe_config->gmch_pfit.lvds_border_bits =
8000 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8001}
8002
acbec814 8003static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8004 struct intel_crtc_state *pipe_config)
acbec814
JB
8005{
8006 struct drm_device *dev = crtc->base.dev;
8007 struct drm_i915_private *dev_priv = dev->dev_private;
8008 int pipe = pipe_config->cpu_transcoder;
8009 intel_clock_t clock;
8010 u32 mdiv;
662c6ecb 8011 int refclk = 100000;
acbec814 8012
f573de5a
SK
8013 /* In case of MIPI DPLL will not even be used */
8014 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8015 return;
8016
a580516d 8017 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8018 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8019 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8020
8021 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8022 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8023 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8024 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8025 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8026
dccbea3b 8027 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8028}
8029
5724dbd1
DL
8030static void
8031i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8032 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8033{
8034 struct drm_device *dev = crtc->base.dev;
8035 struct drm_i915_private *dev_priv = dev->dev_private;
8036 u32 val, base, offset;
8037 int pipe = crtc->pipe, plane = crtc->plane;
8038 int fourcc, pixel_format;
6761dd31 8039 unsigned int aligned_height;
b113d5ee 8040 struct drm_framebuffer *fb;
1b842c89 8041 struct intel_framebuffer *intel_fb;
1ad292b5 8042
42a7b088
DL
8043 val = I915_READ(DSPCNTR(plane));
8044 if (!(val & DISPLAY_PLANE_ENABLE))
8045 return;
8046
d9806c9f 8047 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8048 if (!intel_fb) {
1ad292b5
JB
8049 DRM_DEBUG_KMS("failed to alloc fb\n");
8050 return;
8051 }
8052
1b842c89
DL
8053 fb = &intel_fb->base;
8054
18c5247e
DV
8055 if (INTEL_INFO(dev)->gen >= 4) {
8056 if (val & DISPPLANE_TILED) {
49af449b 8057 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8058 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8059 }
8060 }
1ad292b5
JB
8061
8062 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8063 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8064 fb->pixel_format = fourcc;
8065 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8066
8067 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8068 if (plane_config->tiling)
1ad292b5
JB
8069 offset = I915_READ(DSPTILEOFF(plane));
8070 else
8071 offset = I915_READ(DSPLINOFF(plane));
8072 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8073 } else {
8074 base = I915_READ(DSPADDR(plane));
8075 }
8076 plane_config->base = base;
8077
8078 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8079 fb->width = ((val >> 16) & 0xfff) + 1;
8080 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8081
8082 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8083 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8084
b113d5ee 8085 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8086 fb->pixel_format,
8087 fb->modifier[0]);
1ad292b5 8088
f37b5c2b 8089 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8090
2844a921
DL
8091 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8092 pipe_name(pipe), plane, fb->width, fb->height,
8093 fb->bits_per_pixel, base, fb->pitches[0],
8094 plane_config->size);
1ad292b5 8095
2d14030b 8096 plane_config->fb = intel_fb;
1ad292b5
JB
8097}
8098
70b23a98 8099static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8100 struct intel_crtc_state *pipe_config)
70b23a98
VS
8101{
8102 struct drm_device *dev = crtc->base.dev;
8103 struct drm_i915_private *dev_priv = dev->dev_private;
8104 int pipe = pipe_config->cpu_transcoder;
8105 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8106 intel_clock_t clock;
8107 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8108 int refclk = 100000;
8109
a580516d 8110 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8111 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8112 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8113 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8114 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8115 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8116
8117 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8118 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8119 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8120 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8121 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8122
dccbea3b 8123 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8124}
8125
0e8ffe1b 8126static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8127 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8128{
8129 struct drm_device *dev = crtc->base.dev;
8130 struct drm_i915_private *dev_priv = dev->dev_private;
8131 uint32_t tmp;
8132
f458ebbc
DV
8133 if (!intel_display_power_is_enabled(dev_priv,
8134 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8135 return false;
8136
e143a21c 8137 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8138 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8139
0e8ffe1b
DV
8140 tmp = I915_READ(PIPECONF(crtc->pipe));
8141 if (!(tmp & PIPECONF_ENABLE))
8142 return false;
8143
42571aef
VS
8144 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8145 switch (tmp & PIPECONF_BPC_MASK) {
8146 case PIPECONF_6BPC:
8147 pipe_config->pipe_bpp = 18;
8148 break;
8149 case PIPECONF_8BPC:
8150 pipe_config->pipe_bpp = 24;
8151 break;
8152 case PIPECONF_10BPC:
8153 pipe_config->pipe_bpp = 30;
8154 break;
8155 default:
8156 break;
8157 }
8158 }
8159
b5a9fa09
DV
8160 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8161 pipe_config->limited_color_range = true;
8162
282740f7
VS
8163 if (INTEL_INFO(dev)->gen < 4)
8164 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8165
1bd1bd80
DV
8166 intel_get_pipe_timings(crtc, pipe_config);
8167
2fa2fe9a
DV
8168 i9xx_get_pfit_config(crtc, pipe_config);
8169
6c49f241
DV
8170 if (INTEL_INFO(dev)->gen >= 4) {
8171 tmp = I915_READ(DPLL_MD(crtc->pipe));
8172 pipe_config->pixel_multiplier =
8173 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8174 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8175 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8176 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8177 tmp = I915_READ(DPLL(crtc->pipe));
8178 pipe_config->pixel_multiplier =
8179 ((tmp & SDVO_MULTIPLIER_MASK)
8180 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8181 } else {
8182 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8183 * port and will be fixed up in the encoder->get_config
8184 * function. */
8185 pipe_config->pixel_multiplier = 1;
8186 }
8bcc2795
DV
8187 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8188 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8189 /*
8190 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8191 * on 830. Filter it out here so that we don't
8192 * report errors due to that.
8193 */
8194 if (IS_I830(dev))
8195 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8196
8bcc2795
DV
8197 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8198 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8199 } else {
8200 /* Mask out read-only status bits. */
8201 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8202 DPLL_PORTC_READY_MASK |
8203 DPLL_PORTB_READY_MASK);
8bcc2795 8204 }
6c49f241 8205
70b23a98
VS
8206 if (IS_CHERRYVIEW(dev))
8207 chv_crtc_clock_get(crtc, pipe_config);
8208 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8209 vlv_crtc_clock_get(crtc, pipe_config);
8210 else
8211 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8212
0e8ffe1b
DV
8213 return true;
8214}
8215
dde86e2d 8216static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8217{
8218 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8219 struct intel_encoder *encoder;
74cfd7ac 8220 u32 val, final;
13d83a67 8221 bool has_lvds = false;
199e5d79 8222 bool has_cpu_edp = false;
199e5d79 8223 bool has_panel = false;
99eb6a01
KP
8224 bool has_ck505 = false;
8225 bool can_ssc = false;
13d83a67
JB
8226
8227 /* We need to take the global config into account */
b2784e15 8228 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8229 switch (encoder->type) {
8230 case INTEL_OUTPUT_LVDS:
8231 has_panel = true;
8232 has_lvds = true;
8233 break;
8234 case INTEL_OUTPUT_EDP:
8235 has_panel = true;
2de6905f 8236 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8237 has_cpu_edp = true;
8238 break;
6847d71b
PZ
8239 default:
8240 break;
13d83a67
JB
8241 }
8242 }
8243
99eb6a01 8244 if (HAS_PCH_IBX(dev)) {
41aa3448 8245 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8246 can_ssc = has_ck505;
8247 } else {
8248 has_ck505 = false;
8249 can_ssc = true;
8250 }
8251
2de6905f
ID
8252 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8253 has_panel, has_lvds, has_ck505);
13d83a67
JB
8254
8255 /* Ironlake: try to setup display ref clock before DPLL
8256 * enabling. This is only under driver's control after
8257 * PCH B stepping, previous chipset stepping should be
8258 * ignoring this setting.
8259 */
74cfd7ac
CW
8260 val = I915_READ(PCH_DREF_CONTROL);
8261
8262 /* As we must carefully and slowly disable/enable each source in turn,
8263 * compute the final state we want first and check if we need to
8264 * make any changes at all.
8265 */
8266 final = val;
8267 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8268 if (has_ck505)
8269 final |= DREF_NONSPREAD_CK505_ENABLE;
8270 else
8271 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8272
8273 final &= ~DREF_SSC_SOURCE_MASK;
8274 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8275 final &= ~DREF_SSC1_ENABLE;
8276
8277 if (has_panel) {
8278 final |= DREF_SSC_SOURCE_ENABLE;
8279
8280 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8281 final |= DREF_SSC1_ENABLE;
8282
8283 if (has_cpu_edp) {
8284 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8285 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8286 else
8287 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8288 } else
8289 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8290 } else {
8291 final |= DREF_SSC_SOURCE_DISABLE;
8292 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8293 }
8294
8295 if (final == val)
8296 return;
8297
13d83a67 8298 /* Always enable nonspread source */
74cfd7ac 8299 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8300
99eb6a01 8301 if (has_ck505)
74cfd7ac 8302 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8303 else
74cfd7ac 8304 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8305
199e5d79 8306 if (has_panel) {
74cfd7ac
CW
8307 val &= ~DREF_SSC_SOURCE_MASK;
8308 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8309
199e5d79 8310 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8311 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8312 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8313 val |= DREF_SSC1_ENABLE;
e77166b5 8314 } else
74cfd7ac 8315 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8316
8317 /* Get SSC going before enabling the outputs */
74cfd7ac 8318 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8319 POSTING_READ(PCH_DREF_CONTROL);
8320 udelay(200);
8321
74cfd7ac 8322 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8323
8324 /* Enable CPU source on CPU attached eDP */
199e5d79 8325 if (has_cpu_edp) {
99eb6a01 8326 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8327 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8328 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8329 } else
74cfd7ac 8330 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8331 } else
74cfd7ac 8332 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8333
74cfd7ac 8334 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8335 POSTING_READ(PCH_DREF_CONTROL);
8336 udelay(200);
8337 } else {
8338 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8339
74cfd7ac 8340 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8341
8342 /* Turn off CPU output */
74cfd7ac 8343 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8344
74cfd7ac 8345 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8346 POSTING_READ(PCH_DREF_CONTROL);
8347 udelay(200);
8348
8349 /* Turn off the SSC source */
74cfd7ac
CW
8350 val &= ~DREF_SSC_SOURCE_MASK;
8351 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8352
8353 /* Turn off SSC1 */
74cfd7ac 8354 val &= ~DREF_SSC1_ENABLE;
199e5d79 8355
74cfd7ac 8356 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359 }
74cfd7ac
CW
8360
8361 BUG_ON(val != final);
13d83a67
JB
8362}
8363
f31f2d55 8364static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8365{
f31f2d55 8366 uint32_t tmp;
dde86e2d 8367
0ff066a9
PZ
8368 tmp = I915_READ(SOUTH_CHICKEN2);
8369 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8370 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8371
0ff066a9
PZ
8372 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8373 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8374 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8375
0ff066a9
PZ
8376 tmp = I915_READ(SOUTH_CHICKEN2);
8377 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8378 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8379
0ff066a9
PZ
8380 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8381 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8382 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8383}
8384
8385/* WaMPhyProgramming:hsw */
8386static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8387{
8388 uint32_t tmp;
dde86e2d
PZ
8389
8390 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8391 tmp &= ~(0xFF << 24);
8392 tmp |= (0x12 << 24);
8393 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8394
dde86e2d
PZ
8395 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8396 tmp |= (1 << 11);
8397 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8398
8399 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8400 tmp |= (1 << 11);
8401 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8402
dde86e2d
PZ
8403 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8404 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8405 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8406
8407 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8408 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8409 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8410
0ff066a9
PZ
8411 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8412 tmp &= ~(7 << 13);
8413 tmp |= (5 << 13);
8414 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8415
0ff066a9
PZ
8416 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8417 tmp &= ~(7 << 13);
8418 tmp |= (5 << 13);
8419 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8420
8421 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8422 tmp &= ~0xFF;
8423 tmp |= 0x1C;
8424 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8425
8426 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8427 tmp &= ~0xFF;
8428 tmp |= 0x1C;
8429 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8430
8431 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8432 tmp &= ~(0xFF << 16);
8433 tmp |= (0x1C << 16);
8434 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8435
8436 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8437 tmp &= ~(0xFF << 16);
8438 tmp |= (0x1C << 16);
8439 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8440
0ff066a9
PZ
8441 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8442 tmp |= (1 << 27);
8443 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8444
0ff066a9
PZ
8445 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8446 tmp |= (1 << 27);
8447 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8448
0ff066a9
PZ
8449 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8450 tmp &= ~(0xF << 28);
8451 tmp |= (4 << 28);
8452 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8453
0ff066a9
PZ
8454 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8455 tmp &= ~(0xF << 28);
8456 tmp |= (4 << 28);
8457 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8458}
8459
2fa86a1f
PZ
8460/* Implements 3 different sequences from BSpec chapter "Display iCLK
8461 * Programming" based on the parameters passed:
8462 * - Sequence to enable CLKOUT_DP
8463 * - Sequence to enable CLKOUT_DP without spread
8464 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8465 */
8466static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8467 bool with_fdi)
f31f2d55
PZ
8468{
8469 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8470 uint32_t reg, tmp;
8471
8472 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8473 with_spread = true;
8474 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8475 with_fdi, "LP PCH doesn't have FDI\n"))
8476 with_fdi = false;
f31f2d55 8477
a580516d 8478 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8479
8480 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8481 tmp &= ~SBI_SSCCTL_DISABLE;
8482 tmp |= SBI_SSCCTL_PATHALT;
8483 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8484
8485 udelay(24);
8486
2fa86a1f
PZ
8487 if (with_spread) {
8488 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8489 tmp &= ~SBI_SSCCTL_PATHALT;
8490 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8491
2fa86a1f
PZ
8492 if (with_fdi) {
8493 lpt_reset_fdi_mphy(dev_priv);
8494 lpt_program_fdi_mphy(dev_priv);
8495 }
8496 }
dde86e2d 8497
2fa86a1f
PZ
8498 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8499 SBI_GEN0 : SBI_DBUFF0;
8500 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8501 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8502 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8503
a580516d 8504 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8505}
8506
47701c3b
PZ
8507/* Sequence to disable CLKOUT_DP */
8508static void lpt_disable_clkout_dp(struct drm_device *dev)
8509{
8510 struct drm_i915_private *dev_priv = dev->dev_private;
8511 uint32_t reg, tmp;
8512
a580516d 8513 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8514
8515 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8516 SBI_GEN0 : SBI_DBUFF0;
8517 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8518 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8519 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8520
8521 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8522 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8523 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8524 tmp |= SBI_SSCCTL_PATHALT;
8525 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8526 udelay(32);
8527 }
8528 tmp |= SBI_SSCCTL_DISABLE;
8529 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8530 }
8531
a580516d 8532 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8533}
8534
bf8fa3d3
PZ
8535static void lpt_init_pch_refclk(struct drm_device *dev)
8536{
bf8fa3d3
PZ
8537 struct intel_encoder *encoder;
8538 bool has_vga = false;
8539
b2784e15 8540 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8541 switch (encoder->type) {
8542 case INTEL_OUTPUT_ANALOG:
8543 has_vga = true;
8544 break;
6847d71b
PZ
8545 default:
8546 break;
bf8fa3d3
PZ
8547 }
8548 }
8549
47701c3b
PZ
8550 if (has_vga)
8551 lpt_enable_clkout_dp(dev, true, true);
8552 else
8553 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8554}
8555
dde86e2d
PZ
8556/*
8557 * Initialize reference clocks when the driver loads
8558 */
8559void intel_init_pch_refclk(struct drm_device *dev)
8560{
8561 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8562 ironlake_init_pch_refclk(dev);
8563 else if (HAS_PCH_LPT(dev))
8564 lpt_init_pch_refclk(dev);
8565}
8566
55bb9992 8567static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8568{
55bb9992 8569 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8570 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8571 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8572 struct drm_connector *connector;
55bb9992 8573 struct drm_connector_state *connector_state;
d9d444cb 8574 struct intel_encoder *encoder;
55bb9992 8575 int num_connectors = 0, i;
d9d444cb
JB
8576 bool is_lvds = false;
8577
da3ced29 8578 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8579 if (connector_state->crtc != crtc_state->base.crtc)
8580 continue;
8581
8582 encoder = to_intel_encoder(connector_state->best_encoder);
8583
d9d444cb
JB
8584 switch (encoder->type) {
8585 case INTEL_OUTPUT_LVDS:
8586 is_lvds = true;
8587 break;
6847d71b
PZ
8588 default:
8589 break;
d9d444cb
JB
8590 }
8591 num_connectors++;
8592 }
8593
8594 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8595 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8596 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8597 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8598 }
8599
8600 return 120000;
8601}
8602
6ff93609 8603static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8604{
c8203565 8605 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8607 int pipe = intel_crtc->pipe;
c8203565
PZ
8608 uint32_t val;
8609
78114071 8610 val = 0;
c8203565 8611
6e3c9717 8612 switch (intel_crtc->config->pipe_bpp) {
c8203565 8613 case 18:
dfd07d72 8614 val |= PIPECONF_6BPC;
c8203565
PZ
8615 break;
8616 case 24:
dfd07d72 8617 val |= PIPECONF_8BPC;
c8203565
PZ
8618 break;
8619 case 30:
dfd07d72 8620 val |= PIPECONF_10BPC;
c8203565
PZ
8621 break;
8622 case 36:
dfd07d72 8623 val |= PIPECONF_12BPC;
c8203565
PZ
8624 break;
8625 default:
cc769b62
PZ
8626 /* Case prevented by intel_choose_pipe_bpp_dither. */
8627 BUG();
c8203565
PZ
8628 }
8629
6e3c9717 8630 if (intel_crtc->config->dither)
c8203565
PZ
8631 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8632
6e3c9717 8633 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8634 val |= PIPECONF_INTERLACED_ILK;
8635 else
8636 val |= PIPECONF_PROGRESSIVE;
8637
6e3c9717 8638 if (intel_crtc->config->limited_color_range)
3685a8f3 8639 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8640
c8203565
PZ
8641 I915_WRITE(PIPECONF(pipe), val);
8642 POSTING_READ(PIPECONF(pipe));
8643}
8644
86d3efce
VS
8645/*
8646 * Set up the pipe CSC unit.
8647 *
8648 * Currently only full range RGB to limited range RGB conversion
8649 * is supported, but eventually this should handle various
8650 * RGB<->YCbCr scenarios as well.
8651 */
50f3b016 8652static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8653{
8654 struct drm_device *dev = crtc->dev;
8655 struct drm_i915_private *dev_priv = dev->dev_private;
8656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8657 int pipe = intel_crtc->pipe;
8658 uint16_t coeff = 0x7800; /* 1.0 */
8659
8660 /*
8661 * TODO: Check what kind of values actually come out of the pipe
8662 * with these coeff/postoff values and adjust to get the best
8663 * accuracy. Perhaps we even need to take the bpc value into
8664 * consideration.
8665 */
8666
6e3c9717 8667 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8668 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8669
8670 /*
8671 * GY/GU and RY/RU should be the other way around according
8672 * to BSpec, but reality doesn't agree. Just set them up in
8673 * a way that results in the correct picture.
8674 */
8675 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8676 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8677
8678 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8679 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8680
8681 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8682 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8683
8684 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8685 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8686 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8687
8688 if (INTEL_INFO(dev)->gen > 6) {
8689 uint16_t postoff = 0;
8690
6e3c9717 8691 if (intel_crtc->config->limited_color_range)
32cf0cb0 8692 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8693
8694 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8695 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8696 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8697
8698 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8699 } else {
8700 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8701
6e3c9717 8702 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8703 mode |= CSC_BLACK_SCREEN_OFFSET;
8704
8705 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8706 }
8707}
8708
6ff93609 8709static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8710{
756f85cf
PZ
8711 struct drm_device *dev = crtc->dev;
8712 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8714 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8715 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8716 uint32_t val;
8717
3eff4faa 8718 val = 0;
ee2b0b38 8719
6e3c9717 8720 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8721 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8722
6e3c9717 8723 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8724 val |= PIPECONF_INTERLACED_ILK;
8725 else
8726 val |= PIPECONF_PROGRESSIVE;
8727
702e7a56
PZ
8728 I915_WRITE(PIPECONF(cpu_transcoder), val);
8729 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8730
8731 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8732 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8733
3cdf122c 8734 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8735 val = 0;
8736
6e3c9717 8737 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8738 case 18:
8739 val |= PIPEMISC_DITHER_6_BPC;
8740 break;
8741 case 24:
8742 val |= PIPEMISC_DITHER_8_BPC;
8743 break;
8744 case 30:
8745 val |= PIPEMISC_DITHER_10_BPC;
8746 break;
8747 case 36:
8748 val |= PIPEMISC_DITHER_12_BPC;
8749 break;
8750 default:
8751 /* Case prevented by pipe_config_set_bpp. */
8752 BUG();
8753 }
8754
6e3c9717 8755 if (intel_crtc->config->dither)
756f85cf
PZ
8756 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8757
8758 I915_WRITE(PIPEMISC(pipe), val);
8759 }
ee2b0b38
PZ
8760}
8761
6591c6e4 8762static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8763 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8764 intel_clock_t *clock,
8765 bool *has_reduced_clock,
8766 intel_clock_t *reduced_clock)
8767{
8768 struct drm_device *dev = crtc->dev;
8769 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8770 int refclk;
d4906093 8771 const intel_limit_t *limit;
c329a4ec 8772 bool ret;
79e53945 8773
55bb9992 8774 refclk = ironlake_get_refclk(crtc_state);
79e53945 8775
d4906093
ML
8776 /*
8777 * Returns a set of divisors for the desired target clock with the given
8778 * refclk, or FALSE. The returned values represent the clock equation:
8779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8780 */
a93e255f
ACO
8781 limit = intel_limit(crtc_state, refclk);
8782 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8783 crtc_state->port_clock,
ee9300bb 8784 refclk, NULL, clock);
6591c6e4
PZ
8785 if (!ret)
8786 return false;
cda4b7d3 8787
6591c6e4
PZ
8788 return true;
8789}
8790
d4b1931c
PZ
8791int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8792{
8793 /*
8794 * Account for spread spectrum to avoid
8795 * oversubscribing the link. Max center spread
8796 * is 2.5%; use 5% for safety's sake.
8797 */
8798 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8799 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8800}
8801
7429e9d4 8802static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8803{
7429e9d4 8804 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8805}
8806
de13a2e3 8807static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8808 struct intel_crtc_state *crtc_state,
7429e9d4 8809 u32 *fp,
9a7c7890 8810 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8811{
de13a2e3 8812 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8813 struct drm_device *dev = crtc->dev;
8814 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8815 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8816 struct drm_connector *connector;
55bb9992
ACO
8817 struct drm_connector_state *connector_state;
8818 struct intel_encoder *encoder;
de13a2e3 8819 uint32_t dpll;
55bb9992 8820 int factor, num_connectors = 0, i;
09ede541 8821 bool is_lvds = false, is_sdvo = false;
79e53945 8822
da3ced29 8823 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8824 if (connector_state->crtc != crtc_state->base.crtc)
8825 continue;
8826
8827 encoder = to_intel_encoder(connector_state->best_encoder);
8828
8829 switch (encoder->type) {
79e53945
JB
8830 case INTEL_OUTPUT_LVDS:
8831 is_lvds = true;
8832 break;
8833 case INTEL_OUTPUT_SDVO:
7d57382e 8834 case INTEL_OUTPUT_HDMI:
79e53945 8835 is_sdvo = true;
79e53945 8836 break;
6847d71b
PZ
8837 default:
8838 break;
79e53945 8839 }
43565a06 8840
c751ce4f 8841 num_connectors++;
79e53945 8842 }
79e53945 8843
c1858123 8844 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8845 factor = 21;
8846 if (is_lvds) {
8847 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8848 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8849 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8850 factor = 25;
190f68c5 8851 } else if (crtc_state->sdvo_tv_clock)
8febb297 8852 factor = 20;
c1858123 8853
190f68c5 8854 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8855 *fp |= FP_CB_TUNE;
2c07245f 8856
9a7c7890
DV
8857 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8858 *fp2 |= FP_CB_TUNE;
8859
5eddb70b 8860 dpll = 0;
2c07245f 8861
a07d6787
EA
8862 if (is_lvds)
8863 dpll |= DPLLB_MODE_LVDS;
8864 else
8865 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8866
190f68c5 8867 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8868 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8869
8870 if (is_sdvo)
4a33e48d 8871 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8872 if (crtc_state->has_dp_encoder)
4a33e48d 8873 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8874
a07d6787 8875 /* compute bitmask from p1 value */
190f68c5 8876 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8877 /* also FPA1 */
190f68c5 8878 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8879
190f68c5 8880 switch (crtc_state->dpll.p2) {
a07d6787
EA
8881 case 5:
8882 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8883 break;
8884 case 7:
8885 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8886 break;
8887 case 10:
8888 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8889 break;
8890 case 14:
8891 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8892 break;
79e53945
JB
8893 }
8894
b4c09f3b 8895 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8896 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8897 else
8898 dpll |= PLL_REF_INPUT_DREFCLK;
8899
959e16d6 8900 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8901}
8902
190f68c5
ACO
8903static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8904 struct intel_crtc_state *crtc_state)
de13a2e3 8905{
c7653199 8906 struct drm_device *dev = crtc->base.dev;
de13a2e3 8907 intel_clock_t clock, reduced_clock;
cbbab5bd 8908 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8909 bool ok, has_reduced_clock = false;
8b47047b 8910 bool is_lvds = false;
e2b78267 8911 struct intel_shared_dpll *pll;
de13a2e3 8912
dd3cd74a
ACO
8913 memset(&crtc_state->dpll_hw_state, 0,
8914 sizeof(crtc_state->dpll_hw_state));
8915
409ee761 8916 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8917
5dc5298b
PZ
8918 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8919 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8920
190f68c5 8921 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8922 &has_reduced_clock, &reduced_clock);
190f68c5 8923 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8924 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8925 return -EINVAL;
79e53945 8926 }
f47709a9 8927 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8928 if (!crtc_state->clock_set) {
8929 crtc_state->dpll.n = clock.n;
8930 crtc_state->dpll.m1 = clock.m1;
8931 crtc_state->dpll.m2 = clock.m2;
8932 crtc_state->dpll.p1 = clock.p1;
8933 crtc_state->dpll.p2 = clock.p2;
f47709a9 8934 }
79e53945 8935
5dc5298b 8936 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8937 if (crtc_state->has_pch_encoder) {
8938 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8939 if (has_reduced_clock)
7429e9d4 8940 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8941
190f68c5 8942 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8943 &fp, &reduced_clock,
8944 has_reduced_clock ? &fp2 : NULL);
8945
190f68c5
ACO
8946 crtc_state->dpll_hw_state.dpll = dpll;
8947 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8948 if (has_reduced_clock)
190f68c5 8949 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8950 else
190f68c5 8951 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8952
190f68c5 8953 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8954 if (pll == NULL) {
84f44ce7 8955 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8956 pipe_name(crtc->pipe));
4b645f14
JB
8957 return -EINVAL;
8958 }
3fb37703 8959 }
79e53945 8960
ab585dea 8961 if (is_lvds && has_reduced_clock)
c7653199 8962 crtc->lowfreq_avail = true;
bcd644e0 8963 else
c7653199 8964 crtc->lowfreq_avail = false;
e2b78267 8965
c8f7a0db 8966 return 0;
79e53945
JB
8967}
8968
eb14cb74
VS
8969static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8970 struct intel_link_m_n *m_n)
8971{
8972 struct drm_device *dev = crtc->base.dev;
8973 struct drm_i915_private *dev_priv = dev->dev_private;
8974 enum pipe pipe = crtc->pipe;
8975
8976 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8977 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8978 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8979 & ~TU_SIZE_MASK;
8980 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8981 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8982 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8983}
8984
8985static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8986 enum transcoder transcoder,
b95af8be
VK
8987 struct intel_link_m_n *m_n,
8988 struct intel_link_m_n *m2_n2)
72419203
DV
8989{
8990 struct drm_device *dev = crtc->base.dev;
8991 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8992 enum pipe pipe = crtc->pipe;
72419203 8993
eb14cb74
VS
8994 if (INTEL_INFO(dev)->gen >= 5) {
8995 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8996 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8997 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8998 & ~TU_SIZE_MASK;
8999 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9000 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9001 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9002 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9003 * gen < 8) and if DRRS is supported (to make sure the
9004 * registers are not unnecessarily read).
9005 */
9006 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9007 crtc->config->has_drrs) {
b95af8be
VK
9008 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9009 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9010 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9011 & ~TU_SIZE_MASK;
9012 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9013 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9014 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9015 }
eb14cb74
VS
9016 } else {
9017 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9018 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9019 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9020 & ~TU_SIZE_MASK;
9021 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9022 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9023 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9024 }
9025}
9026
9027void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9028 struct intel_crtc_state *pipe_config)
eb14cb74 9029{
681a8504 9030 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9031 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9032 else
9033 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9034 &pipe_config->dp_m_n,
9035 &pipe_config->dp_m2_n2);
eb14cb74 9036}
72419203 9037
eb14cb74 9038static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9039 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9040{
9041 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9042 &pipe_config->fdi_m_n, NULL);
72419203
DV
9043}
9044
bd2e244f 9045static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9046 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9047{
9048 struct drm_device *dev = crtc->base.dev;
9049 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9050 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9051 uint32_t ps_ctrl = 0;
9052 int id = -1;
9053 int i;
bd2e244f 9054
a1b2278e
CK
9055 /* find scaler attached to this pipe */
9056 for (i = 0; i < crtc->num_scalers; i++) {
9057 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9058 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9059 id = i;
9060 pipe_config->pch_pfit.enabled = true;
9061 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9062 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9063 break;
9064 }
9065 }
bd2e244f 9066
a1b2278e
CK
9067 scaler_state->scaler_id = id;
9068 if (id >= 0) {
9069 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9070 } else {
9071 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9072 }
9073}
9074
5724dbd1
DL
9075static void
9076skylake_get_initial_plane_config(struct intel_crtc *crtc,
9077 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9078{
9079 struct drm_device *dev = crtc->base.dev;
9080 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9081 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9082 int pipe = crtc->pipe;
9083 int fourcc, pixel_format;
6761dd31 9084 unsigned int aligned_height;
bc8d7dff 9085 struct drm_framebuffer *fb;
1b842c89 9086 struct intel_framebuffer *intel_fb;
bc8d7dff 9087
d9806c9f 9088 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9089 if (!intel_fb) {
bc8d7dff
DL
9090 DRM_DEBUG_KMS("failed to alloc fb\n");
9091 return;
9092 }
9093
1b842c89
DL
9094 fb = &intel_fb->base;
9095
bc8d7dff 9096 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9097 if (!(val & PLANE_CTL_ENABLE))
9098 goto error;
9099
bc8d7dff
DL
9100 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9101 fourcc = skl_format_to_fourcc(pixel_format,
9102 val & PLANE_CTL_ORDER_RGBX,
9103 val & PLANE_CTL_ALPHA_MASK);
9104 fb->pixel_format = fourcc;
9105 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9106
40f46283
DL
9107 tiling = val & PLANE_CTL_TILED_MASK;
9108 switch (tiling) {
9109 case PLANE_CTL_TILED_LINEAR:
9110 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9111 break;
9112 case PLANE_CTL_TILED_X:
9113 plane_config->tiling = I915_TILING_X;
9114 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9115 break;
9116 case PLANE_CTL_TILED_Y:
9117 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9118 break;
9119 case PLANE_CTL_TILED_YF:
9120 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9121 break;
9122 default:
9123 MISSING_CASE(tiling);
9124 goto error;
9125 }
9126
bc8d7dff
DL
9127 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9128 plane_config->base = base;
9129
9130 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9131
9132 val = I915_READ(PLANE_SIZE(pipe, 0));
9133 fb->height = ((val >> 16) & 0xfff) + 1;
9134 fb->width = ((val >> 0) & 0x1fff) + 1;
9135
9136 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9137 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9138 fb->pixel_format);
bc8d7dff
DL
9139 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9140
9141 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9142 fb->pixel_format,
9143 fb->modifier[0]);
bc8d7dff 9144
f37b5c2b 9145 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9146
9147 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9148 pipe_name(pipe), fb->width, fb->height,
9149 fb->bits_per_pixel, base, fb->pitches[0],
9150 plane_config->size);
9151
2d14030b 9152 plane_config->fb = intel_fb;
bc8d7dff
DL
9153 return;
9154
9155error:
9156 kfree(fb);
9157}
9158
2fa2fe9a 9159static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9160 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9161{
9162 struct drm_device *dev = crtc->base.dev;
9163 struct drm_i915_private *dev_priv = dev->dev_private;
9164 uint32_t tmp;
9165
9166 tmp = I915_READ(PF_CTL(crtc->pipe));
9167
9168 if (tmp & PF_ENABLE) {
fd4daa9c 9169 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9170 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9171 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9172
9173 /* We currently do not free assignements of panel fitters on
9174 * ivb/hsw (since we don't use the higher upscaling modes which
9175 * differentiates them) so just WARN about this case for now. */
9176 if (IS_GEN7(dev)) {
9177 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9178 PF_PIPE_SEL_IVB(crtc->pipe));
9179 }
2fa2fe9a 9180 }
79e53945
JB
9181}
9182
5724dbd1
DL
9183static void
9184ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9185 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9186{
9187 struct drm_device *dev = crtc->base.dev;
9188 struct drm_i915_private *dev_priv = dev->dev_private;
9189 u32 val, base, offset;
aeee5a49 9190 int pipe = crtc->pipe;
4c6baa59 9191 int fourcc, pixel_format;
6761dd31 9192 unsigned int aligned_height;
b113d5ee 9193 struct drm_framebuffer *fb;
1b842c89 9194 struct intel_framebuffer *intel_fb;
4c6baa59 9195
42a7b088
DL
9196 val = I915_READ(DSPCNTR(pipe));
9197 if (!(val & DISPLAY_PLANE_ENABLE))
9198 return;
9199
d9806c9f 9200 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9201 if (!intel_fb) {
4c6baa59
JB
9202 DRM_DEBUG_KMS("failed to alloc fb\n");
9203 return;
9204 }
9205
1b842c89
DL
9206 fb = &intel_fb->base;
9207
18c5247e
DV
9208 if (INTEL_INFO(dev)->gen >= 4) {
9209 if (val & DISPPLANE_TILED) {
49af449b 9210 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9211 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9212 }
9213 }
4c6baa59
JB
9214
9215 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9216 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9217 fb->pixel_format = fourcc;
9218 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9219
aeee5a49 9220 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9221 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9222 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9223 } else {
49af449b 9224 if (plane_config->tiling)
aeee5a49 9225 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9226 else
aeee5a49 9227 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9228 }
9229 plane_config->base = base;
9230
9231 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9232 fb->width = ((val >> 16) & 0xfff) + 1;
9233 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9234
9235 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9236 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9237
b113d5ee 9238 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9239 fb->pixel_format,
9240 fb->modifier[0]);
4c6baa59 9241
f37b5c2b 9242 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9243
2844a921
DL
9244 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9245 pipe_name(pipe), fb->width, fb->height,
9246 fb->bits_per_pixel, base, fb->pitches[0],
9247 plane_config->size);
b113d5ee 9248
2d14030b 9249 plane_config->fb = intel_fb;
4c6baa59
JB
9250}
9251
0e8ffe1b 9252static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9253 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
9257 uint32_t tmp;
9258
f458ebbc
DV
9259 if (!intel_display_power_is_enabled(dev_priv,
9260 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9261 return false;
9262
e143a21c 9263 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9264 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9265
0e8ffe1b
DV
9266 tmp = I915_READ(PIPECONF(crtc->pipe));
9267 if (!(tmp & PIPECONF_ENABLE))
9268 return false;
9269
42571aef
VS
9270 switch (tmp & PIPECONF_BPC_MASK) {
9271 case PIPECONF_6BPC:
9272 pipe_config->pipe_bpp = 18;
9273 break;
9274 case PIPECONF_8BPC:
9275 pipe_config->pipe_bpp = 24;
9276 break;
9277 case PIPECONF_10BPC:
9278 pipe_config->pipe_bpp = 30;
9279 break;
9280 case PIPECONF_12BPC:
9281 pipe_config->pipe_bpp = 36;
9282 break;
9283 default:
9284 break;
9285 }
9286
b5a9fa09
DV
9287 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9288 pipe_config->limited_color_range = true;
9289
ab9412ba 9290 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9291 struct intel_shared_dpll *pll;
9292
88adfff1
DV
9293 pipe_config->has_pch_encoder = true;
9294
627eb5a3
DV
9295 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9296 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9297 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9298
9299 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9300
c0d43d62 9301 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9302 pipe_config->shared_dpll =
9303 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9304 } else {
9305 tmp = I915_READ(PCH_DPLL_SEL);
9306 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9307 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9308 else
9309 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9310 }
66e985c0
DV
9311
9312 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9313
9314 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9315 &pipe_config->dpll_hw_state));
c93f54cf
DV
9316
9317 tmp = pipe_config->dpll_hw_state.dpll;
9318 pipe_config->pixel_multiplier =
9319 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9320 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9321
9322 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9323 } else {
9324 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9325 }
9326
1bd1bd80
DV
9327 intel_get_pipe_timings(crtc, pipe_config);
9328
2fa2fe9a
DV
9329 ironlake_get_pfit_config(crtc, pipe_config);
9330
0e8ffe1b
DV
9331 return true;
9332}
9333
be256dc7
PZ
9334static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9335{
9336 struct drm_device *dev = dev_priv->dev;
be256dc7 9337 struct intel_crtc *crtc;
be256dc7 9338
d3fcc808 9339 for_each_intel_crtc(dev, crtc)
e2c719b7 9340 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9341 pipe_name(crtc->pipe));
9342
e2c719b7
RC
9343 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9344 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9345 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9346 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9347 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9348 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9349 "CPU PWM1 enabled\n");
c5107b87 9350 if (IS_HASWELL(dev))
e2c719b7 9351 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9352 "CPU PWM2 enabled\n");
e2c719b7 9353 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9354 "PCH PWM1 enabled\n");
e2c719b7 9355 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9356 "Utility pin enabled\n");
e2c719b7 9357 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9358
9926ada1
PZ
9359 /*
9360 * In theory we can still leave IRQs enabled, as long as only the HPD
9361 * interrupts remain enabled. We used to check for that, but since it's
9362 * gen-specific and since we only disable LCPLL after we fully disable
9363 * the interrupts, the check below should be enough.
9364 */
e2c719b7 9365 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9366}
9367
9ccd5aeb
PZ
9368static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9369{
9370 struct drm_device *dev = dev_priv->dev;
9371
9372 if (IS_HASWELL(dev))
9373 return I915_READ(D_COMP_HSW);
9374 else
9375 return I915_READ(D_COMP_BDW);
9376}
9377
3c4c9b81
PZ
9378static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9379{
9380 struct drm_device *dev = dev_priv->dev;
9381
9382 if (IS_HASWELL(dev)) {
9383 mutex_lock(&dev_priv->rps.hw_lock);
9384 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9385 val))
f475dadf 9386 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9387 mutex_unlock(&dev_priv->rps.hw_lock);
9388 } else {
9ccd5aeb
PZ
9389 I915_WRITE(D_COMP_BDW, val);
9390 POSTING_READ(D_COMP_BDW);
3c4c9b81 9391 }
be256dc7
PZ
9392}
9393
9394/*
9395 * This function implements pieces of two sequences from BSpec:
9396 * - Sequence for display software to disable LCPLL
9397 * - Sequence for display software to allow package C8+
9398 * The steps implemented here are just the steps that actually touch the LCPLL
9399 * register. Callers should take care of disabling all the display engine
9400 * functions, doing the mode unset, fixing interrupts, etc.
9401 */
6ff58d53
PZ
9402static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9403 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9404{
9405 uint32_t val;
9406
9407 assert_can_disable_lcpll(dev_priv);
9408
9409 val = I915_READ(LCPLL_CTL);
9410
9411 if (switch_to_fclk) {
9412 val |= LCPLL_CD_SOURCE_FCLK;
9413 I915_WRITE(LCPLL_CTL, val);
9414
9415 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9416 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9417 DRM_ERROR("Switching to FCLK failed\n");
9418
9419 val = I915_READ(LCPLL_CTL);
9420 }
9421
9422 val |= LCPLL_PLL_DISABLE;
9423 I915_WRITE(LCPLL_CTL, val);
9424 POSTING_READ(LCPLL_CTL);
9425
9426 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9427 DRM_ERROR("LCPLL still locked\n");
9428
9ccd5aeb 9429 val = hsw_read_dcomp(dev_priv);
be256dc7 9430 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9431 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9432 ndelay(100);
9433
9ccd5aeb
PZ
9434 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9435 1))
be256dc7
PZ
9436 DRM_ERROR("D_COMP RCOMP still in progress\n");
9437
9438 if (allow_power_down) {
9439 val = I915_READ(LCPLL_CTL);
9440 val |= LCPLL_POWER_DOWN_ALLOW;
9441 I915_WRITE(LCPLL_CTL, val);
9442 POSTING_READ(LCPLL_CTL);
9443 }
9444}
9445
9446/*
9447 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9448 * source.
9449 */
6ff58d53 9450static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9451{
9452 uint32_t val;
9453
9454 val = I915_READ(LCPLL_CTL);
9455
9456 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9457 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9458 return;
9459
a8a8bd54
PZ
9460 /*
9461 * Make sure we're not on PC8 state before disabling PC8, otherwise
9462 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9463 */
59bad947 9464 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9465
be256dc7
PZ
9466 if (val & LCPLL_POWER_DOWN_ALLOW) {
9467 val &= ~LCPLL_POWER_DOWN_ALLOW;
9468 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9469 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9470 }
9471
9ccd5aeb 9472 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9473 val |= D_COMP_COMP_FORCE;
9474 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9475 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9476
9477 val = I915_READ(LCPLL_CTL);
9478 val &= ~LCPLL_PLL_DISABLE;
9479 I915_WRITE(LCPLL_CTL, val);
9480
9481 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9482 DRM_ERROR("LCPLL not locked yet\n");
9483
9484 if (val & LCPLL_CD_SOURCE_FCLK) {
9485 val = I915_READ(LCPLL_CTL);
9486 val &= ~LCPLL_CD_SOURCE_FCLK;
9487 I915_WRITE(LCPLL_CTL, val);
9488
9489 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9490 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9491 DRM_ERROR("Switching back to LCPLL failed\n");
9492 }
215733fa 9493
59bad947 9494 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9495 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9496}
9497
765dab67
PZ
9498/*
9499 * Package states C8 and deeper are really deep PC states that can only be
9500 * reached when all the devices on the system allow it, so even if the graphics
9501 * device allows PC8+, it doesn't mean the system will actually get to these
9502 * states. Our driver only allows PC8+ when going into runtime PM.
9503 *
9504 * The requirements for PC8+ are that all the outputs are disabled, the power
9505 * well is disabled and most interrupts are disabled, and these are also
9506 * requirements for runtime PM. When these conditions are met, we manually do
9507 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9508 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9509 * hang the machine.
9510 *
9511 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9512 * the state of some registers, so when we come back from PC8+ we need to
9513 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9514 * need to take care of the registers kept by RC6. Notice that this happens even
9515 * if we don't put the device in PCI D3 state (which is what currently happens
9516 * because of the runtime PM support).
9517 *
9518 * For more, read "Display Sequences for Package C8" on the hardware
9519 * documentation.
9520 */
a14cb6fc 9521void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9522{
c67a470b
PZ
9523 struct drm_device *dev = dev_priv->dev;
9524 uint32_t val;
9525
c67a470b
PZ
9526 DRM_DEBUG_KMS("Enabling package C8+\n");
9527
c67a470b
PZ
9528 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9529 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9530 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9531 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9532 }
9533
9534 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9535 hsw_disable_lcpll(dev_priv, true, true);
9536}
9537
a14cb6fc 9538void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9539{
9540 struct drm_device *dev = dev_priv->dev;
9541 uint32_t val;
9542
c67a470b
PZ
9543 DRM_DEBUG_KMS("Disabling package C8+\n");
9544
9545 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9546 lpt_init_pch_refclk(dev);
9547
9548 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9549 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9550 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9551 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9552 }
9553
9554 intel_prepare_ddi(dev);
c67a470b
PZ
9555}
9556
27c329ed 9557static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9558{
a821fc46 9559 struct drm_device *dev = old_state->dev;
27c329ed 9560 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
f8437dd1 9561
27c329ed 9562 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9563}
9564
b432e5cf 9565/* compute the max rate for new configuration */
27c329ed 9566static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9567{
b432e5cf 9568 struct intel_crtc *intel_crtc;
27c329ed 9569 struct intel_crtc_state *crtc_state;
b432e5cf 9570 int max_pixel_rate = 0;
b432e5cf 9571
27c329ed
ML
9572 for_each_intel_crtc(state->dev, intel_crtc) {
9573 int pixel_rate;
9574
9575 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9576 if (IS_ERR(crtc_state))
9577 return PTR_ERR(crtc_state);
9578
9579 if (!crtc_state->base.enable)
b432e5cf
VS
9580 continue;
9581
27c329ed 9582 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9583
9584 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
27c329ed 9585 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
b432e5cf
VS
9586 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9587
9588 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9589 }
9590
9591 return max_pixel_rate;
9592}
9593
9594static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9595{
9596 struct drm_i915_private *dev_priv = dev->dev_private;
9597 uint32_t val, data;
9598 int ret;
9599
9600 if (WARN((I915_READ(LCPLL_CTL) &
9601 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9602 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9603 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9604 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9605 "trying to change cdclk frequency with cdclk not enabled\n"))
9606 return;
9607
9608 mutex_lock(&dev_priv->rps.hw_lock);
9609 ret = sandybridge_pcode_write(dev_priv,
9610 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9611 mutex_unlock(&dev_priv->rps.hw_lock);
9612 if (ret) {
9613 DRM_ERROR("failed to inform pcode about cdclk change\n");
9614 return;
9615 }
9616
9617 val = I915_READ(LCPLL_CTL);
9618 val |= LCPLL_CD_SOURCE_FCLK;
9619 I915_WRITE(LCPLL_CTL, val);
9620
9621 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9622 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9623 DRM_ERROR("Switching to FCLK failed\n");
9624
9625 val = I915_READ(LCPLL_CTL);
9626 val &= ~LCPLL_CLK_FREQ_MASK;
9627
9628 switch (cdclk) {
9629 case 450000:
9630 val |= LCPLL_CLK_FREQ_450;
9631 data = 0;
9632 break;
9633 case 540000:
9634 val |= LCPLL_CLK_FREQ_54O_BDW;
9635 data = 1;
9636 break;
9637 case 337500:
9638 val |= LCPLL_CLK_FREQ_337_5_BDW;
9639 data = 2;
9640 break;
9641 case 675000:
9642 val |= LCPLL_CLK_FREQ_675_BDW;
9643 data = 3;
9644 break;
9645 default:
9646 WARN(1, "invalid cdclk frequency\n");
9647 return;
9648 }
9649
9650 I915_WRITE(LCPLL_CTL, val);
9651
9652 val = I915_READ(LCPLL_CTL);
9653 val &= ~LCPLL_CD_SOURCE_FCLK;
9654 I915_WRITE(LCPLL_CTL, val);
9655
9656 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9657 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9658 DRM_ERROR("Switching back to LCPLL failed\n");
9659
9660 mutex_lock(&dev_priv->rps.hw_lock);
9661 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9662 mutex_unlock(&dev_priv->rps.hw_lock);
9663
9664 intel_update_cdclk(dev);
9665
9666 WARN(cdclk != dev_priv->cdclk_freq,
9667 "cdclk requested %d kHz but got %d kHz\n",
9668 cdclk, dev_priv->cdclk_freq);
9669}
9670
27c329ed 9671static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9672{
27c329ed
ML
9673 struct drm_i915_private *dev_priv = to_i915(state->dev);
9674 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9675 int cdclk;
9676
9677 /*
9678 * FIXME should also account for plane ratio
9679 * once 64bpp pixel formats are supported.
9680 */
27c329ed 9681 if (max_pixclk > 540000)
b432e5cf 9682 cdclk = 675000;
27c329ed 9683 else if (max_pixclk > 450000)
b432e5cf 9684 cdclk = 540000;
27c329ed 9685 else if (max_pixclk > 337500)
b432e5cf
VS
9686 cdclk = 450000;
9687 else
9688 cdclk = 337500;
9689
9690 /*
9691 * FIXME move the cdclk caclulation to
9692 * compute_config() so we can fail gracegully.
9693 */
9694 if (cdclk > dev_priv->max_cdclk_freq) {
9695 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9696 cdclk, dev_priv->max_cdclk_freq);
9697 cdclk = dev_priv->max_cdclk_freq;
9698 }
9699
27c329ed 9700 to_intel_atomic_state(state)->cdclk = cdclk;
b432e5cf
VS
9701
9702 return 0;
9703}
9704
27c329ed 9705static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9706{
27c329ed
ML
9707 struct drm_device *dev = old_state->dev;
9708 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
b432e5cf 9709
27c329ed 9710 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9711}
9712
190f68c5
ACO
9713static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9714 struct intel_crtc_state *crtc_state)
09b4ddf9 9715{
190f68c5 9716 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9717 return -EINVAL;
716c2e55 9718
c7653199 9719 crtc->lowfreq_avail = false;
644cef34 9720
c8f7a0db 9721 return 0;
79e53945
JB
9722}
9723
3760b59c
S
9724static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9725 enum port port,
9726 struct intel_crtc_state *pipe_config)
9727{
9728 switch (port) {
9729 case PORT_A:
9730 pipe_config->ddi_pll_sel = SKL_DPLL0;
9731 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9732 break;
9733 case PORT_B:
9734 pipe_config->ddi_pll_sel = SKL_DPLL1;
9735 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9736 break;
9737 case PORT_C:
9738 pipe_config->ddi_pll_sel = SKL_DPLL2;
9739 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9740 break;
9741 default:
9742 DRM_ERROR("Incorrect port type\n");
9743 }
9744}
9745
96b7dfb7
S
9746static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9747 enum port port,
5cec258b 9748 struct intel_crtc_state *pipe_config)
96b7dfb7 9749{
3148ade7 9750 u32 temp, dpll_ctl1;
96b7dfb7
S
9751
9752 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9753 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9754
9755 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9756 case SKL_DPLL0:
9757 /*
9758 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9759 * of the shared DPLL framework and thus needs to be read out
9760 * separately
9761 */
9762 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9763 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9764 break;
96b7dfb7
S
9765 case SKL_DPLL1:
9766 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9767 break;
9768 case SKL_DPLL2:
9769 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9770 break;
9771 case SKL_DPLL3:
9772 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9773 break;
96b7dfb7
S
9774 }
9775}
9776
7d2c8175
DL
9777static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9778 enum port port,
5cec258b 9779 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9780{
9781 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9782
9783 switch (pipe_config->ddi_pll_sel) {
9784 case PORT_CLK_SEL_WRPLL1:
9785 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9786 break;
9787 case PORT_CLK_SEL_WRPLL2:
9788 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9789 break;
9790 }
9791}
9792
26804afd 9793static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9794 struct intel_crtc_state *pipe_config)
26804afd
DV
9795{
9796 struct drm_device *dev = crtc->base.dev;
9797 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9798 struct intel_shared_dpll *pll;
26804afd
DV
9799 enum port port;
9800 uint32_t tmp;
9801
9802 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9803
9804 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9805
96b7dfb7
S
9806 if (IS_SKYLAKE(dev))
9807 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9808 else if (IS_BROXTON(dev))
9809 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9810 else
9811 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9812
d452c5b6
DV
9813 if (pipe_config->shared_dpll >= 0) {
9814 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9815
9816 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9817 &pipe_config->dpll_hw_state));
9818 }
9819
26804afd
DV
9820 /*
9821 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9822 * DDI E. So just check whether this pipe is wired to DDI E and whether
9823 * the PCH transcoder is on.
9824 */
ca370455
DL
9825 if (INTEL_INFO(dev)->gen < 9 &&
9826 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9827 pipe_config->has_pch_encoder = true;
9828
9829 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9830 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9831 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9832
9833 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9834 }
9835}
9836
0e8ffe1b 9837static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9838 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9839{
9840 struct drm_device *dev = crtc->base.dev;
9841 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9842 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9843 uint32_t tmp;
9844
f458ebbc 9845 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9846 POWER_DOMAIN_PIPE(crtc->pipe)))
9847 return false;
9848
e143a21c 9849 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9850 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9851
eccb140b
DV
9852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9853 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9854 enum pipe trans_edp_pipe;
9855 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9856 default:
9857 WARN(1, "unknown pipe linked to edp transcoder\n");
9858 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9859 case TRANS_DDI_EDP_INPUT_A_ON:
9860 trans_edp_pipe = PIPE_A;
9861 break;
9862 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9863 trans_edp_pipe = PIPE_B;
9864 break;
9865 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9866 trans_edp_pipe = PIPE_C;
9867 break;
9868 }
9869
9870 if (trans_edp_pipe == crtc->pipe)
9871 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9872 }
9873
f458ebbc 9874 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9875 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9876 return false;
9877
eccb140b 9878 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9879 if (!(tmp & PIPECONF_ENABLE))
9880 return false;
9881
26804afd 9882 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9883
1bd1bd80
DV
9884 intel_get_pipe_timings(crtc, pipe_config);
9885
a1b2278e
CK
9886 if (INTEL_INFO(dev)->gen >= 9) {
9887 skl_init_scalers(dev, crtc, pipe_config);
9888 }
9889
2fa2fe9a 9890 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9891
9892 if (INTEL_INFO(dev)->gen >= 9) {
9893 pipe_config->scaler_state.scaler_id = -1;
9894 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9895 }
9896
bd2e244f 9897 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9898 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9899 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9900 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9901 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9902 else
9903 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9904 }
88adfff1 9905
e59150dc
JB
9906 if (IS_HASWELL(dev))
9907 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9908 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9909
ebb69c95
CT
9910 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9911 pipe_config->pixel_multiplier =
9912 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9913 } else {
9914 pipe_config->pixel_multiplier = 1;
9915 }
6c49f241 9916
0e8ffe1b
DV
9917 return true;
9918}
9919
560b85bb
CW
9920static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9921{
9922 struct drm_device *dev = crtc->dev;
9923 struct drm_i915_private *dev_priv = dev->dev_private;
9924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9925 uint32_t cntl = 0, size = 0;
560b85bb 9926
dc41c154 9927 if (base) {
3dd512fb
MR
9928 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9929 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9930 unsigned int stride = roundup_pow_of_two(width) * 4;
9931
9932 switch (stride) {
9933 default:
9934 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9935 width, stride);
9936 stride = 256;
9937 /* fallthrough */
9938 case 256:
9939 case 512:
9940 case 1024:
9941 case 2048:
9942 break;
4b0e333e
CW
9943 }
9944
dc41c154
VS
9945 cntl |= CURSOR_ENABLE |
9946 CURSOR_GAMMA_ENABLE |
9947 CURSOR_FORMAT_ARGB |
9948 CURSOR_STRIDE(stride);
9949
9950 size = (height << 12) | width;
4b0e333e 9951 }
560b85bb 9952
dc41c154
VS
9953 if (intel_crtc->cursor_cntl != 0 &&
9954 (intel_crtc->cursor_base != base ||
9955 intel_crtc->cursor_size != size ||
9956 intel_crtc->cursor_cntl != cntl)) {
9957 /* On these chipsets we can only modify the base/size/stride
9958 * whilst the cursor is disabled.
9959 */
9960 I915_WRITE(_CURACNTR, 0);
4b0e333e 9961 POSTING_READ(_CURACNTR);
dc41c154 9962 intel_crtc->cursor_cntl = 0;
4b0e333e 9963 }
560b85bb 9964
99d1f387 9965 if (intel_crtc->cursor_base != base) {
9db4a9c7 9966 I915_WRITE(_CURABASE, base);
99d1f387
VS
9967 intel_crtc->cursor_base = base;
9968 }
4726e0b0 9969
dc41c154
VS
9970 if (intel_crtc->cursor_size != size) {
9971 I915_WRITE(CURSIZE, size);
9972 intel_crtc->cursor_size = size;
4b0e333e 9973 }
560b85bb 9974
4b0e333e 9975 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9976 I915_WRITE(_CURACNTR, cntl);
9977 POSTING_READ(_CURACNTR);
4b0e333e 9978 intel_crtc->cursor_cntl = cntl;
560b85bb 9979 }
560b85bb
CW
9980}
9981
560b85bb 9982static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9983{
9984 struct drm_device *dev = crtc->dev;
9985 struct drm_i915_private *dev_priv = dev->dev_private;
9986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9987 int pipe = intel_crtc->pipe;
4b0e333e
CW
9988 uint32_t cntl;
9989
9990 cntl = 0;
9991 if (base) {
9992 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9993 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9994 case 64:
9995 cntl |= CURSOR_MODE_64_ARGB_AX;
9996 break;
9997 case 128:
9998 cntl |= CURSOR_MODE_128_ARGB_AX;
9999 break;
10000 case 256:
10001 cntl |= CURSOR_MODE_256_ARGB_AX;
10002 break;
10003 default:
3dd512fb 10004 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 10005 return;
65a21cd6 10006 }
4b0e333e 10007 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
10008
10009 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10010 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 10011 }
65a21cd6 10012
8e7d688b 10013 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
10014 cntl |= CURSOR_ROTATE_180;
10015
4b0e333e
CW
10016 if (intel_crtc->cursor_cntl != cntl) {
10017 I915_WRITE(CURCNTR(pipe), cntl);
10018 POSTING_READ(CURCNTR(pipe));
10019 intel_crtc->cursor_cntl = cntl;
65a21cd6 10020 }
4b0e333e 10021
65a21cd6 10022 /* and commit changes on next vblank */
5efb3e28
VS
10023 I915_WRITE(CURBASE(pipe), base);
10024 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10025
10026 intel_crtc->cursor_base = base;
65a21cd6
JB
10027}
10028
cda4b7d3 10029/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
10030static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10031 bool on)
cda4b7d3
CW
10032{
10033 struct drm_device *dev = crtc->dev;
10034 struct drm_i915_private *dev_priv = dev->dev_private;
10035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10036 int pipe = intel_crtc->pipe;
3d7d6510
MR
10037 int x = crtc->cursor_x;
10038 int y = crtc->cursor_y;
d6e4db15 10039 u32 base = 0, pos = 0;
cda4b7d3 10040
d6e4db15 10041 if (on)
cda4b7d3 10042 base = intel_crtc->cursor_addr;
cda4b7d3 10043
6e3c9717 10044 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
10045 base = 0;
10046
6e3c9717 10047 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
10048 base = 0;
10049
10050 if (x < 0) {
3dd512fb 10051 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
10052 base = 0;
10053
10054 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10055 x = -x;
10056 }
10057 pos |= x << CURSOR_X_SHIFT;
10058
10059 if (y < 0) {
3dd512fb 10060 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
10061 base = 0;
10062
10063 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10064 y = -y;
10065 }
10066 pos |= y << CURSOR_Y_SHIFT;
10067
4b0e333e 10068 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
10069 return;
10070
5efb3e28
VS
10071 I915_WRITE(CURPOS(pipe), pos);
10072
4398ad45
VS
10073 /* ILK+ do this automagically */
10074 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 10075 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
10076 base += (intel_crtc->base.cursor->state->crtc_h *
10077 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
10078 }
10079
8ac54669 10080 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
10081 i845_update_cursor(crtc, base);
10082 else
10083 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
10084}
10085
dc41c154
VS
10086static bool cursor_size_ok(struct drm_device *dev,
10087 uint32_t width, uint32_t height)
10088{
10089 if (width == 0 || height == 0)
10090 return false;
10091
10092 /*
10093 * 845g/865g are special in that they are only limited by
10094 * the width of their cursors, the height is arbitrary up to
10095 * the precision of the register. Everything else requires
10096 * square cursors, limited to a few power-of-two sizes.
10097 */
10098 if (IS_845G(dev) || IS_I865G(dev)) {
10099 if ((width & 63) != 0)
10100 return false;
10101
10102 if (width > (IS_845G(dev) ? 64 : 512))
10103 return false;
10104
10105 if (height > 1023)
10106 return false;
10107 } else {
10108 switch (width | height) {
10109 case 256:
10110 case 128:
10111 if (IS_GEN2(dev))
10112 return false;
10113 case 64:
10114 break;
10115 default:
10116 return false;
10117 }
10118 }
10119
10120 return true;
10121}
10122
79e53945 10123static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10124 u16 *blue, uint32_t start, uint32_t size)
79e53945 10125{
7203425a 10126 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10128
7203425a 10129 for (i = start; i < end; i++) {
79e53945
JB
10130 intel_crtc->lut_r[i] = red[i] >> 8;
10131 intel_crtc->lut_g[i] = green[i] >> 8;
10132 intel_crtc->lut_b[i] = blue[i] >> 8;
10133 }
10134
10135 intel_crtc_load_lut(crtc);
10136}
10137
79e53945
JB
10138/* VESA 640x480x72Hz mode to set on the pipe */
10139static struct drm_display_mode load_detect_mode = {
10140 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10141 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10142};
10143
a8bb6818
DV
10144struct drm_framebuffer *
10145__intel_framebuffer_create(struct drm_device *dev,
10146 struct drm_mode_fb_cmd2 *mode_cmd,
10147 struct drm_i915_gem_object *obj)
d2dff872
CW
10148{
10149 struct intel_framebuffer *intel_fb;
10150 int ret;
10151
10152 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10153 if (!intel_fb) {
6ccb81f2 10154 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10155 return ERR_PTR(-ENOMEM);
10156 }
10157
10158 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10159 if (ret)
10160 goto err;
d2dff872
CW
10161
10162 return &intel_fb->base;
dd4916c5 10163err:
6ccb81f2 10164 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10165 kfree(intel_fb);
10166
10167 return ERR_PTR(ret);
d2dff872
CW
10168}
10169
b5ea642a 10170static struct drm_framebuffer *
a8bb6818
DV
10171intel_framebuffer_create(struct drm_device *dev,
10172 struct drm_mode_fb_cmd2 *mode_cmd,
10173 struct drm_i915_gem_object *obj)
10174{
10175 struct drm_framebuffer *fb;
10176 int ret;
10177
10178 ret = i915_mutex_lock_interruptible(dev);
10179 if (ret)
10180 return ERR_PTR(ret);
10181 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10182 mutex_unlock(&dev->struct_mutex);
10183
10184 return fb;
10185}
10186
d2dff872
CW
10187static u32
10188intel_framebuffer_pitch_for_width(int width, int bpp)
10189{
10190 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10191 return ALIGN(pitch, 64);
10192}
10193
10194static u32
10195intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10196{
10197 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10198 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10199}
10200
10201static struct drm_framebuffer *
10202intel_framebuffer_create_for_mode(struct drm_device *dev,
10203 struct drm_display_mode *mode,
10204 int depth, int bpp)
10205{
10206 struct drm_i915_gem_object *obj;
0fed39bd 10207 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10208
10209 obj = i915_gem_alloc_object(dev,
10210 intel_framebuffer_size_for_mode(mode, bpp));
10211 if (obj == NULL)
10212 return ERR_PTR(-ENOMEM);
10213
10214 mode_cmd.width = mode->hdisplay;
10215 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10216 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10217 bpp);
5ca0c34a 10218 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10219
10220 return intel_framebuffer_create(dev, &mode_cmd, obj);
10221}
10222
10223static struct drm_framebuffer *
10224mode_fits_in_fbdev(struct drm_device *dev,
10225 struct drm_display_mode *mode)
10226{
4520f53a 10227#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10228 struct drm_i915_private *dev_priv = dev->dev_private;
10229 struct drm_i915_gem_object *obj;
10230 struct drm_framebuffer *fb;
10231
4c0e5528 10232 if (!dev_priv->fbdev)
d2dff872
CW
10233 return NULL;
10234
4c0e5528 10235 if (!dev_priv->fbdev->fb)
d2dff872
CW
10236 return NULL;
10237
4c0e5528
DV
10238 obj = dev_priv->fbdev->fb->obj;
10239 BUG_ON(!obj);
10240
8bcd4553 10241 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10242 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10243 fb->bits_per_pixel))
d2dff872
CW
10244 return NULL;
10245
01f2c773 10246 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10247 return NULL;
10248
10249 return fb;
4520f53a
DV
10250#else
10251 return NULL;
10252#endif
d2dff872
CW
10253}
10254
d3a40d1b
ACO
10255static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10256 struct drm_crtc *crtc,
10257 struct drm_display_mode *mode,
10258 struct drm_framebuffer *fb,
10259 int x, int y)
10260{
10261 struct drm_plane_state *plane_state;
10262 int hdisplay, vdisplay;
10263 int ret;
10264
10265 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10266 if (IS_ERR(plane_state))
10267 return PTR_ERR(plane_state);
10268
10269 if (mode)
10270 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10271 else
10272 hdisplay = vdisplay = 0;
10273
10274 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10275 if (ret)
10276 return ret;
10277 drm_atomic_set_fb_for_plane(plane_state, fb);
10278 plane_state->crtc_x = 0;
10279 plane_state->crtc_y = 0;
10280 plane_state->crtc_w = hdisplay;
10281 plane_state->crtc_h = vdisplay;
10282 plane_state->src_x = x << 16;
10283 plane_state->src_y = y << 16;
10284 plane_state->src_w = hdisplay << 16;
10285 plane_state->src_h = vdisplay << 16;
10286
10287 return 0;
10288}
10289
d2434ab7 10290bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10291 struct drm_display_mode *mode,
51fd371b
RC
10292 struct intel_load_detect_pipe *old,
10293 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10294{
10295 struct intel_crtc *intel_crtc;
d2434ab7
DV
10296 struct intel_encoder *intel_encoder =
10297 intel_attached_encoder(connector);
79e53945 10298 struct drm_crtc *possible_crtc;
4ef69c7a 10299 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10300 struct drm_crtc *crtc = NULL;
10301 struct drm_device *dev = encoder->dev;
94352cf9 10302 struct drm_framebuffer *fb;
51fd371b 10303 struct drm_mode_config *config = &dev->mode_config;
83a57153 10304 struct drm_atomic_state *state = NULL;
944b0c76 10305 struct drm_connector_state *connector_state;
4be07317 10306 struct intel_crtc_state *crtc_state;
51fd371b 10307 int ret, i = -1;
79e53945 10308
d2dff872 10309 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10310 connector->base.id, connector->name,
8e329a03 10311 encoder->base.id, encoder->name);
d2dff872 10312
51fd371b
RC
10313retry:
10314 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10315 if (ret)
ad3c558f 10316 goto fail;
6e9f798d 10317
79e53945
JB
10318 /*
10319 * Algorithm gets a little messy:
7a5e4805 10320 *
79e53945
JB
10321 * - if the connector already has an assigned crtc, use it (but make
10322 * sure it's on first)
7a5e4805 10323 *
79e53945
JB
10324 * - try to find the first unused crtc that can drive this connector,
10325 * and use that if we find one
79e53945
JB
10326 */
10327
10328 /* See if we already have a CRTC for this connector */
10329 if (encoder->crtc) {
10330 crtc = encoder->crtc;
8261b191 10331
51fd371b 10332 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10333 if (ret)
ad3c558f 10334 goto fail;
4d02e2de 10335 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b 10336 if (ret)
ad3c558f 10337 goto fail;
7b24056b 10338
24218aac 10339 old->dpms_mode = connector->dpms;
8261b191
CW
10340 old->load_detect_temp = false;
10341
10342 /* Make sure the crtc and connector are running */
24218aac
DV
10343 if (connector->dpms != DRM_MODE_DPMS_ON)
10344 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10345
7173188d 10346 return true;
79e53945
JB
10347 }
10348
10349 /* Find an unused one (if possible) */
70e1e0ec 10350 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10351 i++;
10352 if (!(encoder->possible_crtcs & (1 << i)))
10353 continue;
83d65738 10354 if (possible_crtc->state->enable)
a459249c 10355 continue;
a459249c
VS
10356
10357 crtc = possible_crtc;
10358 break;
79e53945
JB
10359 }
10360
10361 /*
10362 * If we didn't find an unused CRTC, don't use any.
10363 */
10364 if (!crtc) {
7173188d 10365 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10366 goto fail;
79e53945
JB
10367 }
10368
51fd371b
RC
10369 ret = drm_modeset_lock(&crtc->mutex, ctx);
10370 if (ret)
ad3c558f 10371 goto fail;
4d02e2de
DV
10372 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10373 if (ret)
ad3c558f 10374 goto fail;
79e53945
JB
10375
10376 intel_crtc = to_intel_crtc(crtc);
24218aac 10377 old->dpms_mode = connector->dpms;
8261b191 10378 old->load_detect_temp = true;
d2dff872 10379 old->release_fb = NULL;
79e53945 10380
83a57153
ACO
10381 state = drm_atomic_state_alloc(dev);
10382 if (!state)
10383 return false;
10384
10385 state->acquire_ctx = ctx;
10386
944b0c76
ACO
10387 connector_state = drm_atomic_get_connector_state(state, connector);
10388 if (IS_ERR(connector_state)) {
10389 ret = PTR_ERR(connector_state);
10390 goto fail;
10391 }
10392
10393 connector_state->crtc = crtc;
10394 connector_state->best_encoder = &intel_encoder->base;
10395
4be07317
ACO
10396 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10397 if (IS_ERR(crtc_state)) {
10398 ret = PTR_ERR(crtc_state);
10399 goto fail;
10400 }
10401
49d6fa21 10402 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10403
6492711d
CW
10404 if (!mode)
10405 mode = &load_detect_mode;
79e53945 10406
d2dff872
CW
10407 /* We need a framebuffer large enough to accommodate all accesses
10408 * that the plane may generate whilst we perform load detection.
10409 * We can not rely on the fbcon either being present (we get called
10410 * during its initialisation to detect all boot displays, or it may
10411 * not even exist) or that it is large enough to satisfy the
10412 * requested mode.
10413 */
94352cf9
DV
10414 fb = mode_fits_in_fbdev(dev, mode);
10415 if (fb == NULL) {
d2dff872 10416 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10417 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10418 old->release_fb = fb;
d2dff872
CW
10419 } else
10420 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10421 if (IS_ERR(fb)) {
d2dff872 10422 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10423 goto fail;
79e53945 10424 }
79e53945 10425
d3a40d1b
ACO
10426 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10427 if (ret)
10428 goto fail;
10429
8c7b5ccb
ACO
10430 drm_mode_copy(&crtc_state->base.mode, mode);
10431
568c634a 10432 if (intel_set_mode(state)) {
6492711d 10433 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10434 if (old->release_fb)
10435 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10436 goto fail;
79e53945 10437 }
9128b040 10438 crtc->primary->crtc = crtc;
7173188d 10439
79e53945 10440 /* let the connector get through one full cycle before testing */
9d0498a2 10441 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10442 return true;
412b61d8 10443
ad3c558f 10444fail:
e5d958ef
ACO
10445 drm_atomic_state_free(state);
10446 state = NULL;
83a57153 10447
51fd371b
RC
10448 if (ret == -EDEADLK) {
10449 drm_modeset_backoff(ctx);
10450 goto retry;
10451 }
10452
412b61d8 10453 return false;
79e53945
JB
10454}
10455
d2434ab7 10456void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10457 struct intel_load_detect_pipe *old,
10458 struct drm_modeset_acquire_ctx *ctx)
79e53945 10459{
83a57153 10460 struct drm_device *dev = connector->dev;
d2434ab7
DV
10461 struct intel_encoder *intel_encoder =
10462 intel_attached_encoder(connector);
4ef69c7a 10463 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10464 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10466 struct drm_atomic_state *state;
944b0c76 10467 struct drm_connector_state *connector_state;
4be07317 10468 struct intel_crtc_state *crtc_state;
d3a40d1b 10469 int ret;
79e53945 10470
d2dff872 10471 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10472 connector->base.id, connector->name,
8e329a03 10473 encoder->base.id, encoder->name);
d2dff872 10474
8261b191 10475 if (old->load_detect_temp) {
83a57153 10476 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10477 if (!state)
10478 goto fail;
83a57153
ACO
10479
10480 state->acquire_ctx = ctx;
10481
944b0c76
ACO
10482 connector_state = drm_atomic_get_connector_state(state, connector);
10483 if (IS_ERR(connector_state))
10484 goto fail;
10485
4be07317
ACO
10486 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10487 if (IS_ERR(crtc_state))
10488 goto fail;
10489
944b0c76
ACO
10490 connector_state->best_encoder = NULL;
10491 connector_state->crtc = NULL;
10492
49d6fa21 10493 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10494
d3a40d1b
ACO
10495 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10496 0, 0);
10497 if (ret)
10498 goto fail;
10499
568c634a 10500 ret = intel_set_mode(state);
2bfb4627
ACO
10501 if (ret)
10502 goto fail;
d2dff872 10503
36206361
DV
10504 if (old->release_fb) {
10505 drm_framebuffer_unregister_private(old->release_fb);
10506 drm_framebuffer_unreference(old->release_fb);
10507 }
d2dff872 10508
0622a53c 10509 return;
79e53945
JB
10510 }
10511
c751ce4f 10512 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10513 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10514 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10515
10516 return;
10517fail:
10518 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10519 drm_atomic_state_free(state);
79e53945
JB
10520}
10521
da4a1efa 10522static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10523 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10524{
10525 struct drm_i915_private *dev_priv = dev->dev_private;
10526 u32 dpll = pipe_config->dpll_hw_state.dpll;
10527
10528 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10529 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10530 else if (HAS_PCH_SPLIT(dev))
10531 return 120000;
10532 else if (!IS_GEN2(dev))
10533 return 96000;
10534 else
10535 return 48000;
10536}
10537
79e53945 10538/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10539static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10540 struct intel_crtc_state *pipe_config)
79e53945 10541{
f1f644dc 10542 struct drm_device *dev = crtc->base.dev;
79e53945 10543 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10544 int pipe = pipe_config->cpu_transcoder;
293623f7 10545 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10546 u32 fp;
10547 intel_clock_t clock;
dccbea3b 10548 int port_clock;
da4a1efa 10549 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10550
10551 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10552 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10553 else
293623f7 10554 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10555
10556 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10557 if (IS_PINEVIEW(dev)) {
10558 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10559 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10560 } else {
10561 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10562 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10563 }
10564
a6c45cf0 10565 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10566 if (IS_PINEVIEW(dev))
10567 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10568 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10569 else
10570 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10571 DPLL_FPA01_P1_POST_DIV_SHIFT);
10572
10573 switch (dpll & DPLL_MODE_MASK) {
10574 case DPLLB_MODE_DAC_SERIAL:
10575 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10576 5 : 10;
10577 break;
10578 case DPLLB_MODE_LVDS:
10579 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10580 7 : 14;
10581 break;
10582 default:
28c97730 10583 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10584 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10585 return;
79e53945
JB
10586 }
10587
ac58c3f0 10588 if (IS_PINEVIEW(dev))
dccbea3b 10589 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10590 else
dccbea3b 10591 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10592 } else {
0fb58223 10593 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10594 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10595
10596 if (is_lvds) {
10597 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10598 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10599
10600 if (lvds & LVDS_CLKB_POWER_UP)
10601 clock.p2 = 7;
10602 else
10603 clock.p2 = 14;
79e53945
JB
10604 } else {
10605 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10606 clock.p1 = 2;
10607 else {
10608 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10609 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10610 }
10611 if (dpll & PLL_P2_DIVIDE_BY_4)
10612 clock.p2 = 4;
10613 else
10614 clock.p2 = 2;
79e53945 10615 }
da4a1efa 10616
dccbea3b 10617 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10618 }
10619
18442d08
VS
10620 /*
10621 * This value includes pixel_multiplier. We will use
241bfc38 10622 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10623 * encoder's get_config() function.
10624 */
dccbea3b 10625 pipe_config->port_clock = port_clock;
f1f644dc
JB
10626}
10627
6878da05
VS
10628int intel_dotclock_calculate(int link_freq,
10629 const struct intel_link_m_n *m_n)
f1f644dc 10630{
f1f644dc
JB
10631 /*
10632 * The calculation for the data clock is:
1041a02f 10633 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10634 * But we want to avoid losing precison if possible, so:
1041a02f 10635 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10636 *
10637 * and the link clock is simpler:
1041a02f 10638 * link_clock = (m * link_clock) / n
f1f644dc
JB
10639 */
10640
6878da05
VS
10641 if (!m_n->link_n)
10642 return 0;
f1f644dc 10643
6878da05
VS
10644 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10645}
f1f644dc 10646
18442d08 10647static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10648 struct intel_crtc_state *pipe_config)
6878da05
VS
10649{
10650 struct drm_device *dev = crtc->base.dev;
79e53945 10651
18442d08
VS
10652 /* read out port_clock from the DPLL */
10653 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10654
f1f644dc 10655 /*
18442d08 10656 * This value does not include pixel_multiplier.
241bfc38 10657 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10658 * agree once we know their relationship in the encoder's
10659 * get_config() function.
79e53945 10660 */
2d112de7 10661 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10662 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10663 &pipe_config->fdi_m_n);
79e53945
JB
10664}
10665
10666/** Returns the currently programmed mode of the given pipe. */
10667struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10668 struct drm_crtc *crtc)
10669{
548f245b 10670 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10672 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10673 struct drm_display_mode *mode;
5cec258b 10674 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10675 int htot = I915_READ(HTOTAL(cpu_transcoder));
10676 int hsync = I915_READ(HSYNC(cpu_transcoder));
10677 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10678 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10679 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10680
10681 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10682 if (!mode)
10683 return NULL;
10684
f1f644dc
JB
10685 /*
10686 * Construct a pipe_config sufficient for getting the clock info
10687 * back out of crtc_clock_get.
10688 *
10689 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10690 * to use a real value here instead.
10691 */
293623f7 10692 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10693 pipe_config.pixel_multiplier = 1;
293623f7
VS
10694 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10695 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10696 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10697 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10698
773ae034 10699 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10700 mode->hdisplay = (htot & 0xffff) + 1;
10701 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10702 mode->hsync_start = (hsync & 0xffff) + 1;
10703 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10704 mode->vdisplay = (vtot & 0xffff) + 1;
10705 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10706 mode->vsync_start = (vsync & 0xffff) + 1;
10707 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10708
10709 drm_mode_set_name(mode);
79e53945
JB
10710
10711 return mode;
10712}
10713
f047e395
CW
10714void intel_mark_busy(struct drm_device *dev)
10715{
c67a470b
PZ
10716 struct drm_i915_private *dev_priv = dev->dev_private;
10717
f62a0076
CW
10718 if (dev_priv->mm.busy)
10719 return;
10720
43694d69 10721 intel_runtime_pm_get(dev_priv);
c67a470b 10722 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10723 if (INTEL_INFO(dev)->gen >= 6)
10724 gen6_rps_busy(dev_priv);
f62a0076 10725 dev_priv->mm.busy = true;
f047e395
CW
10726}
10727
10728void intel_mark_idle(struct drm_device *dev)
652c393a 10729{
c67a470b 10730 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10731
f62a0076
CW
10732 if (!dev_priv->mm.busy)
10733 return;
10734
10735 dev_priv->mm.busy = false;
10736
3d13ef2e 10737 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10738 gen6_rps_idle(dev->dev_private);
bb4cdd53 10739
43694d69 10740 intel_runtime_pm_put(dev_priv);
652c393a
JB
10741}
10742
79e53945
JB
10743static void intel_crtc_destroy(struct drm_crtc *crtc)
10744{
10745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10746 struct drm_device *dev = crtc->dev;
10747 struct intel_unpin_work *work;
67e77c5a 10748
5e2d7afc 10749 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10750 work = intel_crtc->unpin_work;
10751 intel_crtc->unpin_work = NULL;
5e2d7afc 10752 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10753
10754 if (work) {
10755 cancel_work_sync(&work->work);
10756 kfree(work);
10757 }
79e53945
JB
10758
10759 drm_crtc_cleanup(crtc);
67e77c5a 10760
79e53945
JB
10761 kfree(intel_crtc);
10762}
10763
6b95a207
KH
10764static void intel_unpin_work_fn(struct work_struct *__work)
10765{
10766 struct intel_unpin_work *work =
10767 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10768 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10769 struct drm_device *dev = crtc->base.dev;
7733b49b 10770 struct drm_i915_private *dev_priv = dev->dev_private;
a9ff8714 10771 struct drm_plane *primary = crtc->base.primary;
6b95a207 10772
b4a98e57 10773 mutex_lock(&dev->struct_mutex);
a9ff8714 10774 intel_unpin_fb_obj(work->old_fb, primary->state);
05394f39 10775 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10776
7733b49b 10777 intel_fbc_update(dev_priv);
f06cc1b9
JH
10778
10779 if (work->flip_queued_req)
146d84f0 10780 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10781 mutex_unlock(&dev->struct_mutex);
10782
a9ff8714 10783 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
89ed88ba 10784 drm_framebuffer_unreference(work->old_fb);
f99d7069 10785
a9ff8714
VS
10786 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10787 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10788
6b95a207
KH
10789 kfree(work);
10790}
10791
1afe3e9d 10792static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10793 struct drm_crtc *crtc)
6b95a207 10794{
6b95a207
KH
10795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10796 struct intel_unpin_work *work;
6b95a207
KH
10797 unsigned long flags;
10798
10799 /* Ignore early vblank irqs */
10800 if (intel_crtc == NULL)
10801 return;
10802
f326038a
DV
10803 /*
10804 * This is called both by irq handlers and the reset code (to complete
10805 * lost pageflips) so needs the full irqsave spinlocks.
10806 */
6b95a207
KH
10807 spin_lock_irqsave(&dev->event_lock, flags);
10808 work = intel_crtc->unpin_work;
e7d841ca
CW
10809
10810 /* Ensure we don't miss a work->pending update ... */
10811 smp_rmb();
10812
10813 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10814 spin_unlock_irqrestore(&dev->event_lock, flags);
10815 return;
10816 }
10817
d6bbafa1 10818 page_flip_completed(intel_crtc);
0af7e4df 10819
6b95a207 10820 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10821}
10822
1afe3e9d
JB
10823void intel_finish_page_flip(struct drm_device *dev, int pipe)
10824{
fbee40df 10825 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10826 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10827
49b14a5c 10828 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10829}
10830
10831void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10832{
fbee40df 10833 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10834 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10835
49b14a5c 10836 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10837}
10838
75f7f3ec
VS
10839/* Is 'a' after or equal to 'b'? */
10840static bool g4x_flip_count_after_eq(u32 a, u32 b)
10841{
10842 return !((a - b) & 0x80000000);
10843}
10844
10845static bool page_flip_finished(struct intel_crtc *crtc)
10846{
10847 struct drm_device *dev = crtc->base.dev;
10848 struct drm_i915_private *dev_priv = dev->dev_private;
10849
bdfa7542
VS
10850 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10851 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10852 return true;
10853
75f7f3ec
VS
10854 /*
10855 * The relevant registers doen't exist on pre-ctg.
10856 * As the flip done interrupt doesn't trigger for mmio
10857 * flips on gmch platforms, a flip count check isn't
10858 * really needed there. But since ctg has the registers,
10859 * include it in the check anyway.
10860 */
10861 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10862 return true;
10863
10864 /*
10865 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10866 * used the same base address. In that case the mmio flip might
10867 * have completed, but the CS hasn't even executed the flip yet.
10868 *
10869 * A flip count check isn't enough as the CS might have updated
10870 * the base address just after start of vblank, but before we
10871 * managed to process the interrupt. This means we'd complete the
10872 * CS flip too soon.
10873 *
10874 * Combining both checks should get us a good enough result. It may
10875 * still happen that the CS flip has been executed, but has not
10876 * yet actually completed. But in case the base address is the same
10877 * anyway, we don't really care.
10878 */
10879 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10880 crtc->unpin_work->gtt_offset &&
10881 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10882 crtc->unpin_work->flip_count);
10883}
10884
6b95a207
KH
10885void intel_prepare_page_flip(struct drm_device *dev, int plane)
10886{
fbee40df 10887 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10888 struct intel_crtc *intel_crtc =
10889 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10890 unsigned long flags;
10891
f326038a
DV
10892
10893 /*
10894 * This is called both by irq handlers and the reset code (to complete
10895 * lost pageflips) so needs the full irqsave spinlocks.
10896 *
10897 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10898 * generate a page-flip completion irq, i.e. every modeset
10899 * is also accompanied by a spurious intel_prepare_page_flip().
10900 */
6b95a207 10901 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10902 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10903 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10904 spin_unlock_irqrestore(&dev->event_lock, flags);
10905}
10906
eba905b2 10907static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10908{
10909 /* Ensure that the work item is consistent when activating it ... */
10910 smp_wmb();
10911 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10912 /* and that it is marked active as soon as the irq could fire. */
10913 smp_wmb();
10914}
10915
8c9f3aaf
JB
10916static int intel_gen2_queue_flip(struct drm_device *dev,
10917 struct drm_crtc *crtc,
10918 struct drm_framebuffer *fb,
ed8d1975 10919 struct drm_i915_gem_object *obj,
6258fbe2 10920 struct drm_i915_gem_request *req,
ed8d1975 10921 uint32_t flags)
8c9f3aaf 10922{
6258fbe2 10923 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10925 u32 flip_mask;
10926 int ret;
10927
5fb9de1a 10928 ret = intel_ring_begin(req, 6);
8c9f3aaf 10929 if (ret)
4fa62c89 10930 return ret;
8c9f3aaf
JB
10931
10932 /* Can't queue multiple flips, so wait for the previous
10933 * one to finish before executing the next.
10934 */
10935 if (intel_crtc->plane)
10936 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10937 else
10938 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10939 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10940 intel_ring_emit(ring, MI_NOOP);
10941 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10942 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10943 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10944 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10945 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10946
10947 intel_mark_page_flip_active(intel_crtc);
83d4092b 10948 return 0;
8c9f3aaf
JB
10949}
10950
10951static int intel_gen3_queue_flip(struct drm_device *dev,
10952 struct drm_crtc *crtc,
10953 struct drm_framebuffer *fb,
ed8d1975 10954 struct drm_i915_gem_object *obj,
6258fbe2 10955 struct drm_i915_gem_request *req,
ed8d1975 10956 uint32_t flags)
8c9f3aaf 10957{
6258fbe2 10958 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10960 u32 flip_mask;
10961 int ret;
10962
5fb9de1a 10963 ret = intel_ring_begin(req, 6);
8c9f3aaf 10964 if (ret)
4fa62c89 10965 return ret;
8c9f3aaf
JB
10966
10967 if (intel_crtc->plane)
10968 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10969 else
10970 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10971 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10972 intel_ring_emit(ring, MI_NOOP);
10973 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10974 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10975 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10976 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10977 intel_ring_emit(ring, MI_NOOP);
10978
e7d841ca 10979 intel_mark_page_flip_active(intel_crtc);
83d4092b 10980 return 0;
8c9f3aaf
JB
10981}
10982
10983static int intel_gen4_queue_flip(struct drm_device *dev,
10984 struct drm_crtc *crtc,
10985 struct drm_framebuffer *fb,
ed8d1975 10986 struct drm_i915_gem_object *obj,
6258fbe2 10987 struct drm_i915_gem_request *req,
ed8d1975 10988 uint32_t flags)
8c9f3aaf 10989{
6258fbe2 10990 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10991 struct drm_i915_private *dev_priv = dev->dev_private;
10992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10993 uint32_t pf, pipesrc;
10994 int ret;
10995
5fb9de1a 10996 ret = intel_ring_begin(req, 4);
8c9f3aaf 10997 if (ret)
4fa62c89 10998 return ret;
8c9f3aaf
JB
10999
11000 /* i965+ uses the linear or tiled offsets from the
11001 * Display Registers (which do not change across a page-flip)
11002 * so we need only reprogram the base address.
11003 */
6d90c952
DV
11004 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11005 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11006 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11007 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 11008 obj->tiling_mode);
8c9f3aaf
JB
11009
11010 /* XXX Enabling the panel-fitter across page-flip is so far
11011 * untested on non-native modes, so ignore it for now.
11012 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11013 */
11014 pf = 0;
11015 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11016 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11017
11018 intel_mark_page_flip_active(intel_crtc);
83d4092b 11019 return 0;
8c9f3aaf
JB
11020}
11021
11022static int intel_gen6_queue_flip(struct drm_device *dev,
11023 struct drm_crtc *crtc,
11024 struct drm_framebuffer *fb,
ed8d1975 11025 struct drm_i915_gem_object *obj,
6258fbe2 11026 struct drm_i915_gem_request *req,
ed8d1975 11027 uint32_t flags)
8c9f3aaf 11028{
6258fbe2 11029 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
11030 struct drm_i915_private *dev_priv = dev->dev_private;
11031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11032 uint32_t pf, pipesrc;
11033 int ret;
11034
5fb9de1a 11035 ret = intel_ring_begin(req, 4);
8c9f3aaf 11036 if (ret)
4fa62c89 11037 return ret;
8c9f3aaf 11038
6d90c952
DV
11039 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11040 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11041 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11042 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11043
dc257cf1
DV
11044 /* Contrary to the suggestions in the documentation,
11045 * "Enable Panel Fitter" does not seem to be required when page
11046 * flipping with a non-native mode, and worse causes a normal
11047 * modeset to fail.
11048 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11049 */
11050 pf = 0;
8c9f3aaf 11051 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11052 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11053
11054 intel_mark_page_flip_active(intel_crtc);
83d4092b 11055 return 0;
8c9f3aaf
JB
11056}
11057
7c9017e5
JB
11058static int intel_gen7_queue_flip(struct drm_device *dev,
11059 struct drm_crtc *crtc,
11060 struct drm_framebuffer *fb,
ed8d1975 11061 struct drm_i915_gem_object *obj,
6258fbe2 11062 struct drm_i915_gem_request *req,
ed8d1975 11063 uint32_t flags)
7c9017e5 11064{
6258fbe2 11065 struct intel_engine_cs *ring = req->ring;
7c9017e5 11066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11067 uint32_t plane_bit = 0;
ffe74d75
CW
11068 int len, ret;
11069
eba905b2 11070 switch (intel_crtc->plane) {
cb05d8de
DV
11071 case PLANE_A:
11072 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11073 break;
11074 case PLANE_B:
11075 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11076 break;
11077 case PLANE_C:
11078 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11079 break;
11080 default:
11081 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11082 return -ENODEV;
cb05d8de
DV
11083 }
11084
ffe74d75 11085 len = 4;
f476828a 11086 if (ring->id == RCS) {
ffe74d75 11087 len += 6;
f476828a
DL
11088 /*
11089 * On Gen 8, SRM is now taking an extra dword to accommodate
11090 * 48bits addresses, and we need a NOOP for the batch size to
11091 * stay even.
11092 */
11093 if (IS_GEN8(dev))
11094 len += 2;
11095 }
ffe74d75 11096
f66fab8e
VS
11097 /*
11098 * BSpec MI_DISPLAY_FLIP for IVB:
11099 * "The full packet must be contained within the same cache line."
11100 *
11101 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11102 * cacheline, if we ever start emitting more commands before
11103 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11104 * then do the cacheline alignment, and finally emit the
11105 * MI_DISPLAY_FLIP.
11106 */
bba09b12 11107 ret = intel_ring_cacheline_align(req);
f66fab8e 11108 if (ret)
4fa62c89 11109 return ret;
f66fab8e 11110
5fb9de1a 11111 ret = intel_ring_begin(req, len);
7c9017e5 11112 if (ret)
4fa62c89 11113 return ret;
7c9017e5 11114
ffe74d75
CW
11115 /* Unmask the flip-done completion message. Note that the bspec says that
11116 * we should do this for both the BCS and RCS, and that we must not unmask
11117 * more than one flip event at any time (or ensure that one flip message
11118 * can be sent by waiting for flip-done prior to queueing new flips).
11119 * Experimentation says that BCS works despite DERRMR masking all
11120 * flip-done completion events and that unmasking all planes at once
11121 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11122 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11123 */
11124 if (ring->id == RCS) {
11125 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11126 intel_ring_emit(ring, DERRMR);
11127 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11128 DERRMR_PIPEB_PRI_FLIP_DONE |
11129 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11130 if (IS_GEN8(dev))
11131 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11132 MI_SRM_LRM_GLOBAL_GTT);
11133 else
11134 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11135 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11136 intel_ring_emit(ring, DERRMR);
11137 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11138 if (IS_GEN8(dev)) {
11139 intel_ring_emit(ring, 0);
11140 intel_ring_emit(ring, MI_NOOP);
11141 }
ffe74d75
CW
11142 }
11143
cb05d8de 11144 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11145 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11146 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11147 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11148
11149 intel_mark_page_flip_active(intel_crtc);
83d4092b 11150 return 0;
7c9017e5
JB
11151}
11152
84c33a64
SG
11153static bool use_mmio_flip(struct intel_engine_cs *ring,
11154 struct drm_i915_gem_object *obj)
11155{
11156 /*
11157 * This is not being used for older platforms, because
11158 * non-availability of flip done interrupt forces us to use
11159 * CS flips. Older platforms derive flip done using some clever
11160 * tricks involving the flip_pending status bits and vblank irqs.
11161 * So using MMIO flips there would disrupt this mechanism.
11162 */
11163
8e09bf83
CW
11164 if (ring == NULL)
11165 return true;
11166
84c33a64
SG
11167 if (INTEL_INFO(ring->dev)->gen < 5)
11168 return false;
11169
11170 if (i915.use_mmio_flip < 0)
11171 return false;
11172 else if (i915.use_mmio_flip > 0)
11173 return true;
14bf993e
OM
11174 else if (i915.enable_execlists)
11175 return true;
84c33a64 11176 else
b4716185 11177 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11178}
11179
ff944564
DL
11180static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11181{
11182 struct drm_device *dev = intel_crtc->base.dev;
11183 struct drm_i915_private *dev_priv = dev->dev_private;
11184 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11185 const enum pipe pipe = intel_crtc->pipe;
11186 u32 ctl, stride;
11187
11188 ctl = I915_READ(PLANE_CTL(pipe, 0));
11189 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11190 switch (fb->modifier[0]) {
11191 case DRM_FORMAT_MOD_NONE:
11192 break;
11193 case I915_FORMAT_MOD_X_TILED:
ff944564 11194 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11195 break;
11196 case I915_FORMAT_MOD_Y_TILED:
11197 ctl |= PLANE_CTL_TILED_Y;
11198 break;
11199 case I915_FORMAT_MOD_Yf_TILED:
11200 ctl |= PLANE_CTL_TILED_YF;
11201 break;
11202 default:
11203 MISSING_CASE(fb->modifier[0]);
11204 }
ff944564
DL
11205
11206 /*
11207 * The stride is either expressed as a multiple of 64 bytes chunks for
11208 * linear buffers or in number of tiles for tiled buffers.
11209 */
2ebef630
TU
11210 stride = fb->pitches[0] /
11211 intel_fb_stride_alignment(dev, fb->modifier[0],
11212 fb->pixel_format);
ff944564
DL
11213
11214 /*
11215 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11216 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11217 */
11218 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11219 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11220
11221 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11222 POSTING_READ(PLANE_SURF(pipe, 0));
11223}
11224
11225static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11226{
11227 struct drm_device *dev = intel_crtc->base.dev;
11228 struct drm_i915_private *dev_priv = dev->dev_private;
11229 struct intel_framebuffer *intel_fb =
11230 to_intel_framebuffer(intel_crtc->base.primary->fb);
11231 struct drm_i915_gem_object *obj = intel_fb->obj;
11232 u32 dspcntr;
11233 u32 reg;
11234
84c33a64
SG
11235 reg = DSPCNTR(intel_crtc->plane);
11236 dspcntr = I915_READ(reg);
11237
c5d97472
DL
11238 if (obj->tiling_mode != I915_TILING_NONE)
11239 dspcntr |= DISPPLANE_TILED;
11240 else
11241 dspcntr &= ~DISPPLANE_TILED;
11242
84c33a64
SG
11243 I915_WRITE(reg, dspcntr);
11244
11245 I915_WRITE(DSPSURF(intel_crtc->plane),
11246 intel_crtc->unpin_work->gtt_offset);
11247 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11248
ff944564
DL
11249}
11250
11251/*
11252 * XXX: This is the temporary way to update the plane registers until we get
11253 * around to using the usual plane update functions for MMIO flips
11254 */
11255static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11256{
11257 struct drm_device *dev = intel_crtc->base.dev;
11258 bool atomic_update;
11259 u32 start_vbl_count;
11260
11261 intel_mark_page_flip_active(intel_crtc);
11262
11263 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11264
11265 if (INTEL_INFO(dev)->gen >= 9)
11266 skl_do_mmio_flip(intel_crtc);
11267 else
11268 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11269 ilk_do_mmio_flip(intel_crtc);
11270
9362c7c5
ACO
11271 if (atomic_update)
11272 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11273}
11274
9362c7c5 11275static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11276{
b2cfe0ab
CW
11277 struct intel_mmio_flip *mmio_flip =
11278 container_of(work, struct intel_mmio_flip, work);
84c33a64 11279
eed29a5b
DV
11280 if (mmio_flip->req)
11281 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11282 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11283 false, NULL,
11284 &mmio_flip->i915->rps.mmioflips));
84c33a64 11285
b2cfe0ab
CW
11286 intel_do_mmio_flip(mmio_flip->crtc);
11287
eed29a5b 11288 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11289 kfree(mmio_flip);
84c33a64
SG
11290}
11291
11292static int intel_queue_mmio_flip(struct drm_device *dev,
11293 struct drm_crtc *crtc,
11294 struct drm_framebuffer *fb,
11295 struct drm_i915_gem_object *obj,
11296 struct intel_engine_cs *ring,
11297 uint32_t flags)
11298{
b2cfe0ab
CW
11299 struct intel_mmio_flip *mmio_flip;
11300
11301 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11302 if (mmio_flip == NULL)
11303 return -ENOMEM;
84c33a64 11304
bcafc4e3 11305 mmio_flip->i915 = to_i915(dev);
eed29a5b 11306 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11307 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11308
b2cfe0ab
CW
11309 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11310 schedule_work(&mmio_flip->work);
84c33a64 11311
84c33a64
SG
11312 return 0;
11313}
11314
8c9f3aaf
JB
11315static int intel_default_queue_flip(struct drm_device *dev,
11316 struct drm_crtc *crtc,
11317 struct drm_framebuffer *fb,
ed8d1975 11318 struct drm_i915_gem_object *obj,
6258fbe2 11319 struct drm_i915_gem_request *req,
ed8d1975 11320 uint32_t flags)
8c9f3aaf
JB
11321{
11322 return -ENODEV;
11323}
11324
d6bbafa1
CW
11325static bool __intel_pageflip_stall_check(struct drm_device *dev,
11326 struct drm_crtc *crtc)
11327{
11328 struct drm_i915_private *dev_priv = dev->dev_private;
11329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11330 struct intel_unpin_work *work = intel_crtc->unpin_work;
11331 u32 addr;
11332
11333 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11334 return true;
11335
11336 if (!work->enable_stall_check)
11337 return false;
11338
11339 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11340 if (work->flip_queued_req &&
11341 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11342 return false;
11343
1e3feefd 11344 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11345 }
11346
1e3feefd 11347 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11348 return false;
11349
11350 /* Potential stall - if we see that the flip has happened,
11351 * assume a missed interrupt. */
11352 if (INTEL_INFO(dev)->gen >= 4)
11353 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11354 else
11355 addr = I915_READ(DSPADDR(intel_crtc->plane));
11356
11357 /* There is a potential issue here with a false positive after a flip
11358 * to the same address. We could address this by checking for a
11359 * non-incrementing frame counter.
11360 */
11361 return addr == work->gtt_offset;
11362}
11363
11364void intel_check_page_flip(struct drm_device *dev, int pipe)
11365{
11366 struct drm_i915_private *dev_priv = dev->dev_private;
11367 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11369 struct intel_unpin_work *work;
f326038a 11370
6c51d46f 11371 WARN_ON(!in_interrupt());
d6bbafa1
CW
11372
11373 if (crtc == NULL)
11374 return;
11375
f326038a 11376 spin_lock(&dev->event_lock);
6ad790c0
CW
11377 work = intel_crtc->unpin_work;
11378 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11379 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11380 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11381 page_flip_completed(intel_crtc);
6ad790c0 11382 work = NULL;
d6bbafa1 11383 }
6ad790c0
CW
11384 if (work != NULL &&
11385 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11386 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11387 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11388}
11389
6b95a207
KH
11390static int intel_crtc_page_flip(struct drm_crtc *crtc,
11391 struct drm_framebuffer *fb,
ed8d1975
KP
11392 struct drm_pending_vblank_event *event,
11393 uint32_t page_flip_flags)
6b95a207
KH
11394{
11395 struct drm_device *dev = crtc->dev;
11396 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11397 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11398 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11400 struct drm_plane *primary = crtc->primary;
a071fa00 11401 enum pipe pipe = intel_crtc->pipe;
6b95a207 11402 struct intel_unpin_work *work;
a4872ba6 11403 struct intel_engine_cs *ring;
cf5d8a46 11404 bool mmio_flip;
91af127f 11405 struct drm_i915_gem_request *request = NULL;
52e68630 11406 int ret;
6b95a207 11407
2ff8fde1
MR
11408 /*
11409 * drm_mode_page_flip_ioctl() should already catch this, but double
11410 * check to be safe. In the future we may enable pageflipping from
11411 * a disabled primary plane.
11412 */
11413 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11414 return -EBUSY;
11415
e6a595d2 11416 /* Can't change pixel format via MI display flips. */
f4510a27 11417 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11418 return -EINVAL;
11419
11420 /*
11421 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11422 * Note that pitch changes could also affect these register.
11423 */
11424 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11425 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11426 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11427 return -EINVAL;
11428
f900db47
CW
11429 if (i915_terminally_wedged(&dev_priv->gpu_error))
11430 goto out_hang;
11431
b14c5679 11432 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11433 if (work == NULL)
11434 return -ENOMEM;
11435
6b95a207 11436 work->event = event;
b4a98e57 11437 work->crtc = crtc;
ab8d6675 11438 work->old_fb = old_fb;
6b95a207
KH
11439 INIT_WORK(&work->work, intel_unpin_work_fn);
11440
87b6b101 11441 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11442 if (ret)
11443 goto free_work;
11444
6b95a207 11445 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11446 spin_lock_irq(&dev->event_lock);
6b95a207 11447 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11448 /* Before declaring the flip queue wedged, check if
11449 * the hardware completed the operation behind our backs.
11450 */
11451 if (__intel_pageflip_stall_check(dev, crtc)) {
11452 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11453 page_flip_completed(intel_crtc);
11454 } else {
11455 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11456 spin_unlock_irq(&dev->event_lock);
468f0b44 11457
d6bbafa1
CW
11458 drm_crtc_vblank_put(crtc);
11459 kfree(work);
11460 return -EBUSY;
11461 }
6b95a207
KH
11462 }
11463 intel_crtc->unpin_work = work;
5e2d7afc 11464 spin_unlock_irq(&dev->event_lock);
6b95a207 11465
b4a98e57
CW
11466 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11467 flush_workqueue(dev_priv->wq);
11468
75dfca80 11469 /* Reference the objects for the scheduled work. */
ab8d6675 11470 drm_framebuffer_reference(work->old_fb);
05394f39 11471 drm_gem_object_reference(&obj->base);
6b95a207 11472
f4510a27 11473 crtc->primary->fb = fb;
afd65eb4 11474 update_state_fb(crtc->primary);
1ed1f968 11475
e1f99ce6 11476 work->pending_flip_obj = obj;
e1f99ce6 11477
89ed88ba
CW
11478 ret = i915_mutex_lock_interruptible(dev);
11479 if (ret)
11480 goto cleanup;
11481
b4a98e57 11482 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11483 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11484
75f7f3ec 11485 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11486 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11487
4fa62c89
VS
11488 if (IS_VALLEYVIEW(dev)) {
11489 ring = &dev_priv->ring[BCS];
ab8d6675 11490 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11491 /* vlv: DISPLAY_FLIP fails to change tiling */
11492 ring = NULL;
48bf5b2d 11493 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11494 ring = &dev_priv->ring[BCS];
4fa62c89 11495 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11496 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11497 if (ring == NULL || ring->id != RCS)
11498 ring = &dev_priv->ring[BCS];
11499 } else {
11500 ring = &dev_priv->ring[RCS];
11501 }
11502
cf5d8a46
CW
11503 mmio_flip = use_mmio_flip(ring, obj);
11504
11505 /* When using CS flips, we want to emit semaphores between rings.
11506 * However, when using mmio flips we will create a task to do the
11507 * synchronisation, so all we want here is to pin the framebuffer
11508 * into the display plane and skip any waits.
11509 */
82bc3b2d 11510 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11511 crtc->primary->state,
91af127f 11512 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
8c9f3aaf
JB
11513 if (ret)
11514 goto cleanup_pending;
6b95a207 11515
121920fa
TU
11516 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11517 + intel_crtc->dspaddr_offset;
4fa62c89 11518
cf5d8a46 11519 if (mmio_flip) {
84c33a64
SG
11520 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11521 page_flip_flags);
d6bbafa1
CW
11522 if (ret)
11523 goto cleanup_unpin;
11524
f06cc1b9
JH
11525 i915_gem_request_assign(&work->flip_queued_req,
11526 obj->last_write_req);
d6bbafa1 11527 } else {
6258fbe2
JH
11528 if (!request) {
11529 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11530 if (ret)
11531 goto cleanup_unpin;
11532 }
11533
11534 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11535 page_flip_flags);
11536 if (ret)
11537 goto cleanup_unpin;
11538
6258fbe2 11539 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11540 }
11541
91af127f 11542 if (request)
75289874 11543 i915_add_request_no_flush(request);
91af127f 11544
1e3feefd 11545 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11546 work->enable_stall_check = true;
4fa62c89 11547
ab8d6675 11548 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11549 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11550 mutex_unlock(&dev->struct_mutex);
a071fa00 11551
7733b49b 11552 intel_fbc_disable(dev_priv);
a9ff8714
VS
11553 intel_frontbuffer_flip_prepare(dev,
11554 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11555
e5510fac
JB
11556 trace_i915_flip_request(intel_crtc->plane, obj);
11557
6b95a207 11558 return 0;
96b099fd 11559
4fa62c89 11560cleanup_unpin:
82bc3b2d 11561 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11562cleanup_pending:
91af127f
JH
11563 if (request)
11564 i915_gem_request_cancel(request);
b4a98e57 11565 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11566 mutex_unlock(&dev->struct_mutex);
11567cleanup:
f4510a27 11568 crtc->primary->fb = old_fb;
afd65eb4 11569 update_state_fb(crtc->primary);
89ed88ba
CW
11570
11571 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11572 drm_framebuffer_unreference(work->old_fb);
96b099fd 11573
5e2d7afc 11574 spin_lock_irq(&dev->event_lock);
96b099fd 11575 intel_crtc->unpin_work = NULL;
5e2d7afc 11576 spin_unlock_irq(&dev->event_lock);
96b099fd 11577
87b6b101 11578 drm_crtc_vblank_put(crtc);
7317c75e 11579free_work:
96b099fd
CW
11580 kfree(work);
11581
f900db47 11582 if (ret == -EIO) {
02e0efb5
ML
11583 struct drm_atomic_state *state;
11584 struct drm_plane_state *plane_state;
11585
f900db47 11586out_hang:
02e0efb5
ML
11587 state = drm_atomic_state_alloc(dev);
11588 if (!state)
11589 return -ENOMEM;
11590 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11591
11592retry:
11593 plane_state = drm_atomic_get_plane_state(state, primary);
11594 ret = PTR_ERR_OR_ZERO(plane_state);
11595 if (!ret) {
11596 drm_atomic_set_fb_for_plane(plane_state, fb);
11597
11598 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11599 if (!ret)
11600 ret = drm_atomic_commit(state);
11601 }
11602
11603 if (ret == -EDEADLK) {
11604 drm_modeset_backoff(state->acquire_ctx);
11605 drm_atomic_state_clear(state);
11606 goto retry;
11607 }
11608
11609 if (ret)
11610 drm_atomic_state_free(state);
11611
f0d3dad3 11612 if (ret == 0 && event) {
5e2d7afc 11613 spin_lock_irq(&dev->event_lock);
a071fa00 11614 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11615 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11616 }
f900db47 11617 }
96b099fd 11618 return ret;
6b95a207
KH
11619}
11620
da20eabd
ML
11621
11622/**
11623 * intel_wm_need_update - Check whether watermarks need updating
11624 * @plane: drm plane
11625 * @state: new plane state
11626 *
11627 * Check current plane state versus the new one to determine whether
11628 * watermarks need to be recalculated.
11629 *
11630 * Returns true or false.
11631 */
11632static bool intel_wm_need_update(struct drm_plane *plane,
11633 struct drm_plane_state *state)
11634{
11635 /* Update watermarks on tiling changes. */
11636 if (!plane->state->fb || !state->fb ||
11637 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11638 plane->state->rotation != state->rotation)
11639 return true;
11640
11641 if (plane->state->crtc_w != state->crtc_w)
11642 return true;
11643
11644 return false;
11645}
11646
11647int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11648 struct drm_plane_state *plane_state)
11649{
11650 struct drm_crtc *crtc = crtc_state->crtc;
11651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11652 struct drm_plane *plane = plane_state->plane;
11653 struct drm_device *dev = crtc->dev;
11654 struct drm_i915_private *dev_priv = dev->dev_private;
11655 struct intel_plane_state *old_plane_state =
11656 to_intel_plane_state(plane->state);
11657 int idx = intel_crtc->base.base.id, ret;
11658 int i = drm_plane_index(plane);
11659 bool mode_changed = needs_modeset(crtc_state);
11660 bool was_crtc_enabled = crtc->state->active;
11661 bool is_crtc_enabled = crtc_state->active;
11662
11663 bool turn_off, turn_on, visible, was_visible;
11664 struct drm_framebuffer *fb = plane_state->fb;
11665
11666 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11667 plane->type != DRM_PLANE_TYPE_CURSOR) {
11668 ret = skl_update_scaler_plane(
11669 to_intel_crtc_state(crtc_state),
11670 to_intel_plane_state(plane_state));
11671 if (ret)
11672 return ret;
11673 }
11674
11675 /*
11676 * Disabling a plane is always okay; we just need to update
11677 * fb tracking in a special way since cleanup_fb() won't
11678 * get called by the plane helpers.
11679 */
11680 if (old_plane_state->base.fb && !fb)
11681 intel_crtc->atomic.disabled_planes |= 1 << i;
11682
da20eabd
ML
11683 was_visible = old_plane_state->visible;
11684 visible = to_intel_plane_state(plane_state)->visible;
11685
11686 if (!was_crtc_enabled && WARN_ON(was_visible))
11687 was_visible = false;
11688
11689 if (!is_crtc_enabled && WARN_ON(visible))
11690 visible = false;
11691
11692 if (!was_visible && !visible)
11693 return 0;
11694
11695 turn_off = was_visible && (!visible || mode_changed);
11696 turn_on = visible && (!was_visible || mode_changed);
11697
11698 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11699 plane->base.id, fb ? fb->base.id : -1);
11700
11701 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11702 plane->base.id, was_visible, visible,
11703 turn_off, turn_on, mode_changed);
11704
852eb00d 11705 if (turn_on) {
f015c551 11706 intel_crtc->atomic.update_wm_pre = true;
852eb00d
VS
11707 /* must disable cxsr around plane enable/disable */
11708 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11709 intel_crtc->atomic.disable_cxsr = true;
11710 /* to potentially re-enable cxsr */
11711 intel_crtc->atomic.wait_vblank = true;
11712 intel_crtc->atomic.update_wm_post = true;
11713 }
11714 } else if (turn_off) {
f015c551 11715 intel_crtc->atomic.update_wm_post = true;
852eb00d
VS
11716 /* must disable cxsr around plane enable/disable */
11717 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11718 if (is_crtc_enabled)
11719 intel_crtc->atomic.wait_vblank = true;
11720 intel_crtc->atomic.disable_cxsr = true;
11721 }
11722 } else if (intel_wm_need_update(plane, plane_state)) {
f015c551 11723 intel_crtc->atomic.update_wm_pre = true;
852eb00d 11724 }
da20eabd 11725
a9ff8714
VS
11726 if (visible)
11727 intel_crtc->atomic.fb_bits |=
11728 to_intel_plane(plane)->frontbuffer_bit;
11729
da20eabd
ML
11730 switch (plane->type) {
11731 case DRM_PLANE_TYPE_PRIMARY:
da20eabd
ML
11732 intel_crtc->atomic.wait_for_flips = true;
11733 intel_crtc->atomic.pre_disable_primary = turn_off;
11734 intel_crtc->atomic.post_enable_primary = turn_on;
11735
066cf55b
RV
11736 if (turn_off) {
11737 /*
11738 * FIXME: Actually if we will still have any other
11739 * plane enabled on the pipe we could let IPS enabled
11740 * still, but for now lets consider that when we make
11741 * primary invisible by setting DSPCNTR to 0 on
11742 * update_primary_plane function IPS needs to be
11743 * disable.
11744 */
11745 intel_crtc->atomic.disable_ips = true;
11746
da20eabd 11747 intel_crtc->atomic.disable_fbc = true;
066cf55b 11748 }
da20eabd
ML
11749
11750 /*
11751 * FBC does not work on some platforms for rotated
11752 * planes, so disable it when rotation is not 0 and
11753 * update it when rotation is set back to 0.
11754 *
11755 * FIXME: This is redundant with the fbc update done in
11756 * the primary plane enable function except that that
11757 * one is done too late. We eventually need to unify
11758 * this.
11759 */
11760
11761 if (visible &&
11762 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11763 dev_priv->fbc.crtc == intel_crtc &&
11764 plane_state->rotation != BIT(DRM_ROTATE_0))
11765 intel_crtc->atomic.disable_fbc = true;
11766
11767 /*
11768 * BDW signals flip done immediately if the plane
11769 * is disabled, even if the plane enable is already
11770 * armed to occur at the next vblank :(
11771 */
11772 if (turn_on && IS_BROADWELL(dev))
11773 intel_crtc->atomic.wait_vblank = true;
11774
11775 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11776 break;
11777 case DRM_PLANE_TYPE_CURSOR:
da20eabd
ML
11778 break;
11779 case DRM_PLANE_TYPE_OVERLAY:
d032ffa0 11780 if (turn_off && !mode_changed) {
da20eabd
ML
11781 intel_crtc->atomic.wait_vblank = true;
11782 intel_crtc->atomic.update_sprite_watermarks |=
11783 1 << i;
11784 }
da20eabd
ML
11785 }
11786 return 0;
11787}
11788
6d3a1ce7
ML
11789static bool encoders_cloneable(const struct intel_encoder *a,
11790 const struct intel_encoder *b)
11791{
11792 /* masks could be asymmetric, so check both ways */
11793 return a == b || (a->cloneable & (1 << b->type) &&
11794 b->cloneable & (1 << a->type));
11795}
11796
11797static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11798 struct intel_crtc *crtc,
11799 struct intel_encoder *encoder)
11800{
11801 struct intel_encoder *source_encoder;
11802 struct drm_connector *connector;
11803 struct drm_connector_state *connector_state;
11804 int i;
11805
11806 for_each_connector_in_state(state, connector, connector_state, i) {
11807 if (connector_state->crtc != &crtc->base)
11808 continue;
11809
11810 source_encoder =
11811 to_intel_encoder(connector_state->best_encoder);
11812 if (!encoders_cloneable(encoder, source_encoder))
11813 return false;
11814 }
11815
11816 return true;
11817}
11818
11819static bool check_encoder_cloning(struct drm_atomic_state *state,
11820 struct intel_crtc *crtc)
11821{
11822 struct intel_encoder *encoder;
11823 struct drm_connector *connector;
11824 struct drm_connector_state *connector_state;
11825 int i;
11826
11827 for_each_connector_in_state(state, connector, connector_state, i) {
11828 if (connector_state->crtc != &crtc->base)
11829 continue;
11830
11831 encoder = to_intel_encoder(connector_state->best_encoder);
11832 if (!check_single_encoder_cloning(state, crtc, encoder))
11833 return false;
11834 }
11835
11836 return true;
11837}
11838
11839static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11840 struct drm_crtc_state *crtc_state)
11841{
cf5a15be 11842 struct drm_device *dev = crtc->dev;
ad421372 11843 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11845 struct intel_crtc_state *pipe_config =
11846 to_intel_crtc_state(crtc_state);
6d3a1ce7 11847 struct drm_atomic_state *state = crtc_state->state;
ad421372 11848 int ret, idx = crtc->base.id;
6d3a1ce7
ML
11849 bool mode_changed = needs_modeset(crtc_state);
11850
11851 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11852 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11853 return -EINVAL;
11854 }
11855
11856 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11857 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11858 idx, crtc->state->active, intel_crtc->active);
11859
852eb00d
VS
11860 if (mode_changed && !crtc_state->active)
11861 intel_crtc->atomic.update_wm_post = true;
eddfcbcd 11862
ad421372
ML
11863 if (mode_changed && crtc_state->enable &&
11864 dev_priv->display.crtc_compute_clock &&
11865 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11866 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11867 pipe_config);
11868 if (ret)
11869 return ret;
11870 }
11871
e435d6e5
ML
11872 ret = 0;
11873 if (INTEL_INFO(dev)->gen >= 9) {
11874 if (mode_changed)
11875 ret = skl_update_scaler_crtc(pipe_config);
11876
11877 if (!ret)
11878 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11879 pipe_config);
11880 }
11881
11882 return ret;
6d3a1ce7
ML
11883}
11884
65b38e0d 11885static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11886 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11887 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11888 .atomic_begin = intel_begin_crtc_commit,
11889 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11890 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11891};
11892
d29b2f9d
ACO
11893static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11894{
11895 struct intel_connector *connector;
11896
11897 for_each_intel_connector(dev, connector) {
11898 if (connector->base.encoder) {
11899 connector->base.state->best_encoder =
11900 connector->base.encoder;
11901 connector->base.state->crtc =
11902 connector->base.encoder->crtc;
11903 } else {
11904 connector->base.state->best_encoder = NULL;
11905 connector->base.state->crtc = NULL;
11906 }
11907 }
11908}
11909
050f7aeb 11910static void
eba905b2 11911connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11912 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11913{
11914 int bpp = pipe_config->pipe_bpp;
11915
11916 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11917 connector->base.base.id,
c23cc417 11918 connector->base.name);
050f7aeb
DV
11919
11920 /* Don't use an invalid EDID bpc value */
11921 if (connector->base.display_info.bpc &&
11922 connector->base.display_info.bpc * 3 < bpp) {
11923 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11924 bpp, connector->base.display_info.bpc*3);
11925 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11926 }
11927
11928 /* Clamp bpp to 8 on screens without EDID 1.4 */
11929 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11930 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11931 bpp);
11932 pipe_config->pipe_bpp = 24;
11933 }
11934}
11935
4e53c2e0 11936static int
050f7aeb 11937compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11938 struct intel_crtc_state *pipe_config)
4e53c2e0 11939{
050f7aeb 11940 struct drm_device *dev = crtc->base.dev;
1486017f 11941 struct drm_atomic_state *state;
da3ced29
ACO
11942 struct drm_connector *connector;
11943 struct drm_connector_state *connector_state;
1486017f 11944 int bpp, i;
4e53c2e0 11945
d328c9d7 11946 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11947 bpp = 10*3;
d328c9d7
DV
11948 else if (INTEL_INFO(dev)->gen >= 5)
11949 bpp = 12*3;
11950 else
11951 bpp = 8*3;
11952
4e53c2e0 11953
4e53c2e0
DV
11954 pipe_config->pipe_bpp = bpp;
11955
1486017f
ACO
11956 state = pipe_config->base.state;
11957
4e53c2e0 11958 /* Clamp display bpp to EDID value */
da3ced29
ACO
11959 for_each_connector_in_state(state, connector, connector_state, i) {
11960 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11961 continue;
11962
da3ced29
ACO
11963 connected_sink_compute_bpp(to_intel_connector(connector),
11964 pipe_config);
4e53c2e0
DV
11965 }
11966
11967 return bpp;
11968}
11969
644db711
DV
11970static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11971{
11972 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11973 "type: 0x%x flags: 0x%x\n",
1342830c 11974 mode->crtc_clock,
644db711
DV
11975 mode->crtc_hdisplay, mode->crtc_hsync_start,
11976 mode->crtc_hsync_end, mode->crtc_htotal,
11977 mode->crtc_vdisplay, mode->crtc_vsync_start,
11978 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11979}
11980
c0b03411 11981static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11982 struct intel_crtc_state *pipe_config,
c0b03411
DV
11983 const char *context)
11984{
6a60cd87
CK
11985 struct drm_device *dev = crtc->base.dev;
11986 struct drm_plane *plane;
11987 struct intel_plane *intel_plane;
11988 struct intel_plane_state *state;
11989 struct drm_framebuffer *fb;
11990
11991 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11992 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11993
11994 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11995 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11996 pipe_config->pipe_bpp, pipe_config->dither);
11997 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11998 pipe_config->has_pch_encoder,
11999 pipe_config->fdi_lanes,
12000 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12001 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12002 pipe_config->fdi_m_n.tu);
eb14cb74
VS
12003 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12004 pipe_config->has_dp_encoder,
12005 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12006 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12007 pipe_config->dp_m_n.tu);
b95af8be
VK
12008
12009 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12010 pipe_config->has_dp_encoder,
12011 pipe_config->dp_m2_n2.gmch_m,
12012 pipe_config->dp_m2_n2.gmch_n,
12013 pipe_config->dp_m2_n2.link_m,
12014 pipe_config->dp_m2_n2.link_n,
12015 pipe_config->dp_m2_n2.tu);
12016
55072d19
DV
12017 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12018 pipe_config->has_audio,
12019 pipe_config->has_infoframe);
12020
c0b03411 12021 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12022 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12023 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12024 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12025 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12026 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12027 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12028 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12029 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12030 crtc->num_scalers,
12031 pipe_config->scaler_state.scaler_users,
12032 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12033 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12034 pipe_config->gmch_pfit.control,
12035 pipe_config->gmch_pfit.pgm_ratios,
12036 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12037 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12038 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12039 pipe_config->pch_pfit.size,
12040 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12041 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12042 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12043
415ff0f6 12044 if (IS_BROXTON(dev)) {
05712c15 12045 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12046 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12047 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12048 pipe_config->ddi_pll_sel,
12049 pipe_config->dpll_hw_state.ebb0,
05712c15 12050 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12051 pipe_config->dpll_hw_state.pll0,
12052 pipe_config->dpll_hw_state.pll1,
12053 pipe_config->dpll_hw_state.pll2,
12054 pipe_config->dpll_hw_state.pll3,
12055 pipe_config->dpll_hw_state.pll6,
12056 pipe_config->dpll_hw_state.pll8,
05712c15 12057 pipe_config->dpll_hw_state.pll9,
c8453338 12058 pipe_config->dpll_hw_state.pll10,
415ff0f6
TU
12059 pipe_config->dpll_hw_state.pcsdw12);
12060 } else if (IS_SKYLAKE(dev)) {
12061 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12062 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12063 pipe_config->ddi_pll_sel,
12064 pipe_config->dpll_hw_state.ctrl1,
12065 pipe_config->dpll_hw_state.cfgcr1,
12066 pipe_config->dpll_hw_state.cfgcr2);
12067 } else if (HAS_DDI(dev)) {
12068 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12069 pipe_config->ddi_pll_sel,
12070 pipe_config->dpll_hw_state.wrpll);
12071 } else {
12072 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12073 "fp0: 0x%x, fp1: 0x%x\n",
12074 pipe_config->dpll_hw_state.dpll,
12075 pipe_config->dpll_hw_state.dpll_md,
12076 pipe_config->dpll_hw_state.fp0,
12077 pipe_config->dpll_hw_state.fp1);
12078 }
12079
6a60cd87
CK
12080 DRM_DEBUG_KMS("planes on this crtc\n");
12081 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12082 intel_plane = to_intel_plane(plane);
12083 if (intel_plane->pipe != crtc->pipe)
12084 continue;
12085
12086 state = to_intel_plane_state(plane->state);
12087 fb = state->base.fb;
12088 if (!fb) {
12089 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12090 "disabled, scaler_id = %d\n",
12091 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12092 plane->base.id, intel_plane->pipe,
12093 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12094 drm_plane_index(plane), state->scaler_id);
12095 continue;
12096 }
12097
12098 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12099 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12100 plane->base.id, intel_plane->pipe,
12101 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12102 drm_plane_index(plane));
12103 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12104 fb->base.id, fb->width, fb->height, fb->pixel_format);
12105 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12106 state->scaler_id,
12107 state->src.x1 >> 16, state->src.y1 >> 16,
12108 drm_rect_width(&state->src) >> 16,
12109 drm_rect_height(&state->src) >> 16,
12110 state->dst.x1, state->dst.y1,
12111 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12112 }
c0b03411
DV
12113}
12114
5448a00d 12115static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12116{
5448a00d
ACO
12117 struct drm_device *dev = state->dev;
12118 struct intel_encoder *encoder;
da3ced29 12119 struct drm_connector *connector;
5448a00d 12120 struct drm_connector_state *connector_state;
00f0b378 12121 unsigned int used_ports = 0;
5448a00d 12122 int i;
00f0b378
VS
12123
12124 /*
12125 * Walk the connector list instead of the encoder
12126 * list to detect the problem on ddi platforms
12127 * where there's just one encoder per digital port.
12128 */
da3ced29 12129 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 12130 if (!connector_state->best_encoder)
00f0b378
VS
12131 continue;
12132
5448a00d
ACO
12133 encoder = to_intel_encoder(connector_state->best_encoder);
12134
12135 WARN_ON(!connector_state->crtc);
00f0b378
VS
12136
12137 switch (encoder->type) {
12138 unsigned int port_mask;
12139 case INTEL_OUTPUT_UNKNOWN:
12140 if (WARN_ON(!HAS_DDI(dev)))
12141 break;
12142 case INTEL_OUTPUT_DISPLAYPORT:
12143 case INTEL_OUTPUT_HDMI:
12144 case INTEL_OUTPUT_EDP:
12145 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12146
12147 /* the same port mustn't appear more than once */
12148 if (used_ports & port_mask)
12149 return false;
12150
12151 used_ports |= port_mask;
12152 default:
12153 break;
12154 }
12155 }
12156
12157 return true;
12158}
12159
83a57153
ACO
12160static void
12161clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12162{
12163 struct drm_crtc_state tmp_state;
663a3640 12164 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12165 struct intel_dpll_hw_state dpll_hw_state;
12166 enum intel_dpll_id shared_dpll;
8504c74c 12167 uint32_t ddi_pll_sel;
83a57153 12168
7546a384
ACO
12169 /* FIXME: before the switch to atomic started, a new pipe_config was
12170 * kzalloc'd. Code that depends on any field being zero should be
12171 * fixed, so that the crtc_state can be safely duplicated. For now,
12172 * only fields that are know to not cause problems are preserved. */
12173
83a57153 12174 tmp_state = crtc_state->base;
663a3640 12175 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12176 shared_dpll = crtc_state->shared_dpll;
12177 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12178 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12179
83a57153 12180 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12181
83a57153 12182 crtc_state->base = tmp_state;
663a3640 12183 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12184 crtc_state->shared_dpll = shared_dpll;
12185 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12186 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12187}
12188
548ee15b 12189static int
b8cecdf5 12190intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12191 struct intel_crtc_state *pipe_config)
ee7b9f93 12192{
b359283a 12193 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12194 struct intel_encoder *encoder;
da3ced29 12195 struct drm_connector *connector;
0b901879 12196 struct drm_connector_state *connector_state;
d328c9d7 12197 int base_bpp, ret = -EINVAL;
0b901879 12198 int i;
e29c22c0 12199 bool retry = true;
ee7b9f93 12200
83a57153 12201 clear_intel_crtc_state(pipe_config);
7758a113 12202
e143a21c
DV
12203 pipe_config->cpu_transcoder =
12204 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12205
2960bc9c
ID
12206 /*
12207 * Sanitize sync polarity flags based on requested ones. If neither
12208 * positive or negative polarity is requested, treat this as meaning
12209 * negative polarity.
12210 */
2d112de7 12211 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12212 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12213 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12214
2d112de7 12215 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12216 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12217 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12218
050f7aeb
DV
12219 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12220 * plane pixel format and any sink constraints into account. Returns the
12221 * source plane bpp so that dithering can be selected on mismatches
12222 * after encoders and crtc also have had their say. */
d328c9d7
DV
12223 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12224 pipe_config);
12225 if (base_bpp < 0)
4e53c2e0
DV
12226 goto fail;
12227
e41a56be
VS
12228 /*
12229 * Determine the real pipe dimensions. Note that stereo modes can
12230 * increase the actual pipe size due to the frame doubling and
12231 * insertion of additional space for blanks between the frame. This
12232 * is stored in the crtc timings. We use the requested mode to do this
12233 * computation to clearly distinguish it from the adjusted mode, which
12234 * can be changed by the connectors in the below retry loop.
12235 */
2d112de7 12236 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12237 &pipe_config->pipe_src_w,
12238 &pipe_config->pipe_src_h);
e41a56be 12239
e29c22c0 12240encoder_retry:
ef1b460d 12241 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12242 pipe_config->port_clock = 0;
ef1b460d 12243 pipe_config->pixel_multiplier = 1;
ff9a6750 12244
135c81b8 12245 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12246 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12247 CRTC_STEREO_DOUBLE);
135c81b8 12248
7758a113
DV
12249 /* Pass our mode to the connectors and the CRTC to give them a chance to
12250 * adjust it according to limitations or connector properties, and also
12251 * a chance to reject the mode entirely.
47f1c6c9 12252 */
da3ced29 12253 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12254 if (connector_state->crtc != crtc)
7758a113 12255 continue;
7ae89233 12256
0b901879
ACO
12257 encoder = to_intel_encoder(connector_state->best_encoder);
12258
efea6e8e
DV
12259 if (!(encoder->compute_config(encoder, pipe_config))) {
12260 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12261 goto fail;
12262 }
ee7b9f93 12263 }
47f1c6c9 12264
ff9a6750
DV
12265 /* Set default port clock if not overwritten by the encoder. Needs to be
12266 * done afterwards in case the encoder adjusts the mode. */
12267 if (!pipe_config->port_clock)
2d112de7 12268 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12269 * pipe_config->pixel_multiplier;
ff9a6750 12270
a43f6e0f 12271 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12272 if (ret < 0) {
7758a113
DV
12273 DRM_DEBUG_KMS("CRTC fixup failed\n");
12274 goto fail;
ee7b9f93 12275 }
e29c22c0
DV
12276
12277 if (ret == RETRY) {
12278 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12279 ret = -EINVAL;
12280 goto fail;
12281 }
12282
12283 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12284 retry = false;
12285 goto encoder_retry;
12286 }
12287
d328c9d7 12288 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12289 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12290 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12291
7758a113 12292fail:
548ee15b 12293 return ret;
ee7b9f93 12294}
47f1c6c9 12295
ea9d758d 12296static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12297{
ea9d758d 12298 struct drm_encoder *encoder;
f6e5b160 12299 struct drm_device *dev = crtc->dev;
f6e5b160 12300
ea9d758d
DV
12301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12302 if (encoder->crtc == crtc)
12303 return true;
12304
12305 return false;
12306}
12307
12308static void
0a9ab303 12309intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12310{
0a9ab303 12311 struct drm_device *dev = state->dev;
ea9d758d 12312 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12313 struct drm_crtc *crtc;
12314 struct drm_crtc_state *crtc_state;
ea9d758d 12315 struct drm_connector *connector;
8a75d157 12316 int i;
ea9d758d 12317
de419ab6 12318 intel_shared_dpll_commit(state);
ba41c0de 12319
b2784e15 12320 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12321 if (!intel_encoder->base.crtc)
12322 continue;
12323
69024de8
ML
12324 crtc = intel_encoder->base.crtc;
12325 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12326 if (!crtc_state || !needs_modeset(crtc->state))
12327 continue;
ea9d758d 12328
69024de8 12329 intel_encoder->connectors_active = false;
ea9d758d
DV
12330 }
12331
3cb480bc 12332 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
ea9d758d 12333
7668851f 12334 /* Double check state. */
8a75d157 12335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
0a9ab303 12336 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12337
12338 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12339
12340 /* Update hwmode for vblank functions */
12341 if (crtc->state->active)
12342 crtc->hwmode = crtc->state->adjusted_mode;
12343 else
12344 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12345 }
12346
12347 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12348 if (!connector->encoder || !connector->encoder->crtc)
12349 continue;
12350
69024de8
ML
12351 crtc = connector->encoder->crtc;
12352 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12353 if (!crtc_state || !needs_modeset(crtc->state))
12354 continue;
ea9d758d 12355
53d9f4e9 12356 if (crtc->state->active) {
69024de8
ML
12357 struct drm_property *dpms_property =
12358 dev->mode_config.dpms_property;
68d34720 12359
69024de8
ML
12360 connector->dpms = DRM_MODE_DPMS_ON;
12361 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12362
69024de8
ML
12363 intel_encoder = to_intel_encoder(connector->encoder);
12364 intel_encoder->connectors_active = true;
12365 } else
12366 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12367 }
ea9d758d
DV
12368}
12369
3bd26263 12370static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12371{
3bd26263 12372 int diff;
f1f644dc
JB
12373
12374 if (clock1 == clock2)
12375 return true;
12376
12377 if (!clock1 || !clock2)
12378 return false;
12379
12380 diff = abs(clock1 - clock2);
12381
12382 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12383 return true;
12384
12385 return false;
12386}
12387
25c5b266
DV
12388#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12389 list_for_each_entry((intel_crtc), \
12390 &(dev)->mode_config.crtc_list, \
12391 base.head) \
0973f18f 12392 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12393
cfb23ed6
ML
12394
12395static bool
12396intel_compare_m_n(unsigned int m, unsigned int n,
12397 unsigned int m2, unsigned int n2,
12398 bool exact)
12399{
12400 if (m == m2 && n == n2)
12401 return true;
12402
12403 if (exact || !m || !n || !m2 || !n2)
12404 return false;
12405
12406 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12407
12408 if (m > m2) {
12409 while (m > m2) {
12410 m2 <<= 1;
12411 n2 <<= 1;
12412 }
12413 } else if (m < m2) {
12414 while (m < m2) {
12415 m <<= 1;
12416 n <<= 1;
12417 }
12418 }
12419
12420 return m == m2 && n == n2;
12421}
12422
12423static bool
12424intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12425 struct intel_link_m_n *m2_n2,
12426 bool adjust)
12427{
12428 if (m_n->tu == m2_n2->tu &&
12429 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12430 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12431 intel_compare_m_n(m_n->link_m, m_n->link_n,
12432 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12433 if (adjust)
12434 *m2_n2 = *m_n;
12435
12436 return true;
12437 }
12438
12439 return false;
12440}
12441
0e8ffe1b 12442static bool
2fa2fe9a 12443intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12444 struct intel_crtc_state *current_config,
cfb23ed6
ML
12445 struct intel_crtc_state *pipe_config,
12446 bool adjust)
0e8ffe1b 12447{
cfb23ed6
ML
12448 bool ret = true;
12449
12450#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12451 do { \
12452 if (!adjust) \
12453 DRM_ERROR(fmt, ##__VA_ARGS__); \
12454 else \
12455 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12456 } while (0)
12457
66e985c0
DV
12458#define PIPE_CONF_CHECK_X(name) \
12459 if (current_config->name != pipe_config->name) { \
cfb23ed6 12460 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12461 "(expected 0x%08x, found 0x%08x)\n", \
12462 current_config->name, \
12463 pipe_config->name); \
cfb23ed6 12464 ret = false; \
66e985c0
DV
12465 }
12466
08a24034
DV
12467#define PIPE_CONF_CHECK_I(name) \
12468 if (current_config->name != pipe_config->name) { \
cfb23ed6 12469 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12470 "(expected %i, found %i)\n", \
12471 current_config->name, \
12472 pipe_config->name); \
cfb23ed6
ML
12473 ret = false; \
12474 }
12475
12476#define PIPE_CONF_CHECK_M_N(name) \
12477 if (!intel_compare_link_m_n(&current_config->name, \
12478 &pipe_config->name,\
12479 adjust)) { \
12480 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12481 "(expected tu %i gmch %i/%i link %i/%i, " \
12482 "found tu %i, gmch %i/%i link %i/%i)\n", \
12483 current_config->name.tu, \
12484 current_config->name.gmch_m, \
12485 current_config->name.gmch_n, \
12486 current_config->name.link_m, \
12487 current_config->name.link_n, \
12488 pipe_config->name.tu, \
12489 pipe_config->name.gmch_m, \
12490 pipe_config->name.gmch_n, \
12491 pipe_config->name.link_m, \
12492 pipe_config->name.link_n); \
12493 ret = false; \
12494 }
12495
12496#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12497 if (!intel_compare_link_m_n(&current_config->name, \
12498 &pipe_config->name, adjust) && \
12499 !intel_compare_link_m_n(&current_config->alt_name, \
12500 &pipe_config->name, adjust)) { \
12501 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12502 "(expected tu %i gmch %i/%i link %i/%i, " \
12503 "or tu %i gmch %i/%i link %i/%i, " \
12504 "found tu %i, gmch %i/%i link %i/%i)\n", \
12505 current_config->name.tu, \
12506 current_config->name.gmch_m, \
12507 current_config->name.gmch_n, \
12508 current_config->name.link_m, \
12509 current_config->name.link_n, \
12510 current_config->alt_name.tu, \
12511 current_config->alt_name.gmch_m, \
12512 current_config->alt_name.gmch_n, \
12513 current_config->alt_name.link_m, \
12514 current_config->alt_name.link_n, \
12515 pipe_config->name.tu, \
12516 pipe_config->name.gmch_m, \
12517 pipe_config->name.gmch_n, \
12518 pipe_config->name.link_m, \
12519 pipe_config->name.link_n); \
12520 ret = false; \
88adfff1
DV
12521 }
12522
b95af8be
VK
12523/* This is required for BDW+ where there is only one set of registers for
12524 * switching between high and low RR.
12525 * This macro can be used whenever a comparison has to be made between one
12526 * hw state and multiple sw state variables.
12527 */
12528#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12529 if ((current_config->name != pipe_config->name) && \
12530 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12531 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12532 "(expected %i or %i, found %i)\n", \
12533 current_config->name, \
12534 current_config->alt_name, \
12535 pipe_config->name); \
cfb23ed6 12536 ret = false; \
b95af8be
VK
12537 }
12538
1bd1bd80
DV
12539#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12540 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12541 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12542 "(expected %i, found %i)\n", \
12543 current_config->name & (mask), \
12544 pipe_config->name & (mask)); \
cfb23ed6 12545 ret = false; \
1bd1bd80
DV
12546 }
12547
5e550656
VS
12548#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12549 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12550 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12551 "(expected %i, found %i)\n", \
12552 current_config->name, \
12553 pipe_config->name); \
cfb23ed6 12554 ret = false; \
5e550656
VS
12555 }
12556
bb760063
DV
12557#define PIPE_CONF_QUIRK(quirk) \
12558 ((current_config->quirks | pipe_config->quirks) & (quirk))
12559
eccb140b
DV
12560 PIPE_CONF_CHECK_I(cpu_transcoder);
12561
08a24034
DV
12562 PIPE_CONF_CHECK_I(has_pch_encoder);
12563 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12564 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12565
eb14cb74 12566 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12567
12568 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12569 PIPE_CONF_CHECK_M_N(dp_m_n);
12570
12571 PIPE_CONF_CHECK_I(has_drrs);
12572 if (current_config->has_drrs)
12573 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12574 } else
12575 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12576
2d112de7
ACO
12577 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12578 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12579 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12580 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12581 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12582 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12583
2d112de7
ACO
12584 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12585 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12586 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12587 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12588 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12589 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12590
c93f54cf 12591 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12592 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12593 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12594 IS_VALLEYVIEW(dev))
12595 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12596 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12597
9ed109a7
DV
12598 PIPE_CONF_CHECK_I(has_audio);
12599
2d112de7 12600 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12601 DRM_MODE_FLAG_INTERLACE);
12602
bb760063 12603 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12604 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12605 DRM_MODE_FLAG_PHSYNC);
2d112de7 12606 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12607 DRM_MODE_FLAG_NHSYNC);
2d112de7 12608 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12609 DRM_MODE_FLAG_PVSYNC);
2d112de7 12610 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12611 DRM_MODE_FLAG_NVSYNC);
12612 }
045ac3b5 12613
37327abd
VS
12614 PIPE_CONF_CHECK_I(pipe_src_w);
12615 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12616
9953599b
DV
12617 /*
12618 * FIXME: BIOS likes to set up a cloned config with lvds+external
12619 * screen. Since we don't yet re-compute the pipe config when moving
12620 * just the lvds port away to another pipe the sw tracking won't match.
12621 *
12622 * Proper atomic modesets with recomputed global state will fix this.
12623 * Until then just don't check gmch state for inherited modes.
12624 */
12625 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12626 PIPE_CONF_CHECK_I(gmch_pfit.control);
12627 /* pfit ratios are autocomputed by the hw on gen4+ */
12628 if (INTEL_INFO(dev)->gen < 4)
12629 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12630 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12631 }
12632
fd4daa9c
CW
12633 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12634 if (current_config->pch_pfit.enabled) {
12635 PIPE_CONF_CHECK_I(pch_pfit.pos);
12636 PIPE_CONF_CHECK_I(pch_pfit.size);
12637 }
2fa2fe9a 12638
a1b2278e
CK
12639 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12640
e59150dc
JB
12641 /* BDW+ don't expose a synchronous way to read the state */
12642 if (IS_HASWELL(dev))
12643 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12644
282740f7
VS
12645 PIPE_CONF_CHECK_I(double_wide);
12646
26804afd
DV
12647 PIPE_CONF_CHECK_X(ddi_pll_sel);
12648
c0d43d62 12649 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12650 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12651 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12652 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12653 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12654 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12655 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12656 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12657 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12658
42571aef
VS
12659 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12660 PIPE_CONF_CHECK_I(pipe_bpp);
12661
2d112de7 12662 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12663 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12664
66e985c0 12665#undef PIPE_CONF_CHECK_X
08a24034 12666#undef PIPE_CONF_CHECK_I
b95af8be 12667#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12668#undef PIPE_CONF_CHECK_FLAGS
5e550656 12669#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12670#undef PIPE_CONF_QUIRK
cfb23ed6 12671#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12672
cfb23ed6 12673 return ret;
0e8ffe1b
DV
12674}
12675
08db6652
DL
12676static void check_wm_state(struct drm_device *dev)
12677{
12678 struct drm_i915_private *dev_priv = dev->dev_private;
12679 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12680 struct intel_crtc *intel_crtc;
12681 int plane;
12682
12683 if (INTEL_INFO(dev)->gen < 9)
12684 return;
12685
12686 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12687 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12688
12689 for_each_intel_crtc(dev, intel_crtc) {
12690 struct skl_ddb_entry *hw_entry, *sw_entry;
12691 const enum pipe pipe = intel_crtc->pipe;
12692
12693 if (!intel_crtc->active)
12694 continue;
12695
12696 /* planes */
dd740780 12697 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12698 hw_entry = &hw_ddb.plane[pipe][plane];
12699 sw_entry = &sw_ddb->plane[pipe][plane];
12700
12701 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12702 continue;
12703
12704 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12705 "(expected (%u,%u), found (%u,%u))\n",
12706 pipe_name(pipe), plane + 1,
12707 sw_entry->start, sw_entry->end,
12708 hw_entry->start, hw_entry->end);
12709 }
12710
12711 /* cursor */
12712 hw_entry = &hw_ddb.cursor[pipe];
12713 sw_entry = &sw_ddb->cursor[pipe];
12714
12715 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12716 continue;
12717
12718 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12719 "(expected (%u,%u), found (%u,%u))\n",
12720 pipe_name(pipe),
12721 sw_entry->start, sw_entry->end,
12722 hw_entry->start, hw_entry->end);
12723 }
12724}
12725
91d1b4bd
DV
12726static void
12727check_connector_state(struct drm_device *dev)
8af6cf88 12728{
8af6cf88
DV
12729 struct intel_connector *connector;
12730
3a3371ff 12731 for_each_intel_connector(dev, connector) {
ad3c558f
ML
12732 struct drm_encoder *encoder = connector->base.encoder;
12733 struct drm_connector_state *state = connector->base.state;
12734
8af6cf88
DV
12735 /* This also checks the encoder/connector hw state with the
12736 * ->get_hw_state callbacks. */
12737 intel_connector_check_state(connector);
12738
ad3c558f 12739 I915_STATE_WARN(state->best_encoder != encoder,
8af6cf88
DV
12740 "connector's staged encoder doesn't match current encoder\n");
12741 }
91d1b4bd
DV
12742}
12743
12744static void
12745check_encoder_state(struct drm_device *dev)
12746{
12747 struct intel_encoder *encoder;
12748 struct intel_connector *connector;
8af6cf88 12749
b2784e15 12750 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12751 bool enabled = false;
12752 bool active = false;
12753 enum pipe pipe, tracked_pipe;
12754
12755 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12756 encoder->base.base.id,
8e329a03 12757 encoder->base.name);
8af6cf88 12758
e2c719b7 12759 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12760 "encoder's active_connectors set, but no crtc\n");
12761
3a3371ff 12762 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12763 if (connector->base.encoder != &encoder->base)
12764 continue;
12765 enabled = true;
12766 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12767 active = true;
ad3c558f
ML
12768
12769 I915_STATE_WARN(connector->base.state->crtc !=
12770 encoder->base.crtc,
12771 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12772 }
0e32b39c
DA
12773 /*
12774 * for MST connectors if we unplug the connector is gone
12775 * away but the encoder is still connected to a crtc
12776 * until a modeset happens in response to the hotplug.
12777 */
12778 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12779 continue;
12780
e2c719b7 12781 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12782 "encoder's enabled state mismatch "
12783 "(expected %i, found %i)\n",
12784 !!encoder->base.crtc, enabled);
e2c719b7 12785 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12786 "active encoder with no crtc\n");
12787
e2c719b7 12788 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12789 "encoder's computed active state doesn't match tracked active state "
12790 "(expected %i, found %i)\n", active, encoder->connectors_active);
12791
12792 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12793 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12794 "encoder's hw state doesn't match sw tracking "
12795 "(expected %i, found %i)\n",
12796 encoder->connectors_active, active);
12797
12798 if (!encoder->base.crtc)
12799 continue;
12800
12801 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12802 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12803 "active encoder's pipe doesn't match"
12804 "(expected %i, found %i)\n",
12805 tracked_pipe, pipe);
12806
12807 }
91d1b4bd
DV
12808}
12809
12810static void
12811check_crtc_state(struct drm_device *dev)
12812{
fbee40df 12813 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12814 struct intel_crtc *crtc;
12815 struct intel_encoder *encoder;
5cec258b 12816 struct intel_crtc_state pipe_config;
8af6cf88 12817
d3fcc808 12818 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12819 bool enabled = false;
12820 bool active = false;
12821
045ac3b5
JB
12822 memset(&pipe_config, 0, sizeof(pipe_config));
12823
8af6cf88
DV
12824 DRM_DEBUG_KMS("[CRTC:%d]\n",
12825 crtc->base.base.id);
12826
83d65738 12827 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12828 "active crtc, but not enabled in sw tracking\n");
12829
b2784e15 12830 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12831 if (encoder->base.crtc != &crtc->base)
12832 continue;
12833 enabled = true;
12834 if (encoder->connectors_active)
12835 active = true;
12836 }
6c49f241 12837
e2c719b7 12838 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12839 "crtc's computed active state doesn't match tracked active state "
12840 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12841 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12842 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12843 "(expected %i, found %i)\n", enabled,
12844 crtc->base.state->enable);
8af6cf88 12845
0e8ffe1b
DV
12846 active = dev_priv->display.get_pipe_config(crtc,
12847 &pipe_config);
d62cf62a 12848
b6b5d049
VS
12849 /* hw state is inconsistent with the pipe quirk */
12850 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12851 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12852 active = crtc->active;
12853
b2784e15 12854 for_each_intel_encoder(dev, encoder) {
3eaba51c 12855 enum pipe pipe;
6c49f241
DV
12856 if (encoder->base.crtc != &crtc->base)
12857 continue;
1d37b689 12858 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12859 encoder->get_config(encoder, &pipe_config);
12860 }
12861
e2c719b7 12862 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12863 "crtc active state doesn't match with hw state "
12864 "(expected %i, found %i)\n", crtc->active, active);
12865
53d9f4e9
ML
12866 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12867 "transitional active state does not match atomic hw state "
12868 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12869
cfb23ed6
ML
12870 if (!active)
12871 continue;
12872
12873 if (!intel_pipe_config_compare(dev, crtc->config,
12874 &pipe_config, false)) {
e2c719b7 12875 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12876 intel_dump_pipe_config(crtc, &pipe_config,
12877 "[hw state]");
6e3c9717 12878 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12879 "[sw state]");
12880 }
8af6cf88
DV
12881 }
12882}
12883
91d1b4bd
DV
12884static void
12885check_shared_dpll_state(struct drm_device *dev)
12886{
fbee40df 12887 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12888 struct intel_crtc *crtc;
12889 struct intel_dpll_hw_state dpll_hw_state;
12890 int i;
5358901f
DV
12891
12892 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12893 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12894 int enabled_crtcs = 0, active_crtcs = 0;
12895 bool active;
12896
12897 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12898
12899 DRM_DEBUG_KMS("%s\n", pll->name);
12900
12901 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12902
e2c719b7 12903 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12904 "more active pll users than references: %i vs %i\n",
3e369b76 12905 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12906 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12907 "pll in active use but not on in sw tracking\n");
e2c719b7 12908 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12909 "pll in on but not on in use in sw tracking\n");
e2c719b7 12910 I915_STATE_WARN(pll->on != active,
5358901f
DV
12911 "pll on state mismatch (expected %i, found %i)\n",
12912 pll->on, active);
12913
d3fcc808 12914 for_each_intel_crtc(dev, crtc) {
83d65738 12915 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12916 enabled_crtcs++;
12917 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12918 active_crtcs++;
12919 }
e2c719b7 12920 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12921 "pll active crtcs mismatch (expected %i, found %i)\n",
12922 pll->active, active_crtcs);
e2c719b7 12923 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12924 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12925 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12926
e2c719b7 12927 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12928 sizeof(dpll_hw_state)),
12929 "pll hw state mismatch\n");
5358901f 12930 }
8af6cf88
DV
12931}
12932
91d1b4bd
DV
12933void
12934intel_modeset_check_state(struct drm_device *dev)
12935{
08db6652 12936 check_wm_state(dev);
91d1b4bd
DV
12937 check_connector_state(dev);
12938 check_encoder_state(dev);
12939 check_crtc_state(dev);
12940 check_shared_dpll_state(dev);
12941}
12942
5cec258b 12943void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12944 int dotclock)
12945{
12946 /*
12947 * FDI already provided one idea for the dotclock.
12948 * Yell if the encoder disagrees.
12949 */
2d112de7 12950 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12951 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12952 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12953}
12954
80715b2f
VS
12955static void update_scanline_offset(struct intel_crtc *crtc)
12956{
12957 struct drm_device *dev = crtc->base.dev;
12958
12959 /*
12960 * The scanline counter increments at the leading edge of hsync.
12961 *
12962 * On most platforms it starts counting from vtotal-1 on the
12963 * first active line. That means the scanline counter value is
12964 * always one less than what we would expect. Ie. just after
12965 * start of vblank, which also occurs at start of hsync (on the
12966 * last active line), the scanline counter will read vblank_start-1.
12967 *
12968 * On gen2 the scanline counter starts counting from 1 instead
12969 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12970 * to keep the value positive), instead of adding one.
12971 *
12972 * On HSW+ the behaviour of the scanline counter depends on the output
12973 * type. For DP ports it behaves like most other platforms, but on HDMI
12974 * there's an extra 1 line difference. So we need to add two instead of
12975 * one to the value.
12976 */
12977 if (IS_GEN2(dev)) {
6e3c9717 12978 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12979 int vtotal;
12980
12981 vtotal = mode->crtc_vtotal;
12982 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12983 vtotal /= 2;
12984
12985 crtc->scanline_offset = vtotal - 1;
12986 } else if (HAS_DDI(dev) &&
409ee761 12987 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12988 crtc->scanline_offset = 2;
12989 } else
12990 crtc->scanline_offset = 1;
12991}
12992
ad421372 12993static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12994{
225da59b 12995 struct drm_device *dev = state->dev;
ed6739ef 12996 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12997 struct intel_shared_dpll_config *shared_dpll = NULL;
ed6739ef 12998 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12999 struct intel_crtc_state *intel_crtc_state;
13000 struct drm_crtc *crtc;
13001 struct drm_crtc_state *crtc_state;
0a9ab303 13002 int i;
ed6739ef
ACO
13003
13004 if (!dev_priv->display.crtc_compute_clock)
ad421372 13005 return;
ed6739ef 13006
0a9ab303 13007 for_each_crtc_in_state(state, crtc, crtc_state, i) {
ad421372
ML
13008 int dpll;
13009
0a9ab303 13010 intel_crtc = to_intel_crtc(crtc);
4978cc93 13011 intel_crtc_state = to_intel_crtc_state(crtc_state);
ad421372 13012 dpll = intel_crtc_state->shared_dpll;
0a9ab303 13013
ad421372 13014 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
225da59b
ACO
13015 continue;
13016
ad421372 13017 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
0a9ab303 13018
ad421372
ML
13019 if (!shared_dpll)
13020 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13021
ad421372
ML
13022 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
13023 }
ed6739ef
ACO
13024}
13025
99d736a2
ML
13026/*
13027 * This implements the workaround described in the "notes" section of the mode
13028 * set sequence documentation. When going from no pipes or single pipe to
13029 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13030 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13031 */
13032static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13033{
13034 struct drm_crtc_state *crtc_state;
13035 struct intel_crtc *intel_crtc;
13036 struct drm_crtc *crtc;
13037 struct intel_crtc_state *first_crtc_state = NULL;
13038 struct intel_crtc_state *other_crtc_state = NULL;
13039 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13040 int i;
13041
13042 /* look at all crtc's that are going to be enabled in during modeset */
13043 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13044 intel_crtc = to_intel_crtc(crtc);
13045
13046 if (!crtc_state->active || !needs_modeset(crtc_state))
13047 continue;
13048
13049 if (first_crtc_state) {
13050 other_crtc_state = to_intel_crtc_state(crtc_state);
13051 break;
13052 } else {
13053 first_crtc_state = to_intel_crtc_state(crtc_state);
13054 first_pipe = intel_crtc->pipe;
13055 }
13056 }
13057
13058 /* No workaround needed? */
13059 if (!first_crtc_state)
13060 return 0;
13061
13062 /* w/a possibly needed, check how many crtc's are already enabled. */
13063 for_each_intel_crtc(state->dev, intel_crtc) {
13064 struct intel_crtc_state *pipe_config;
13065
13066 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13067 if (IS_ERR(pipe_config))
13068 return PTR_ERR(pipe_config);
13069
13070 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13071
13072 if (!pipe_config->base.active ||
13073 needs_modeset(&pipe_config->base))
13074 continue;
13075
13076 /* 2 or more enabled crtcs means no need for w/a */
13077 if (enabled_pipe != INVALID_PIPE)
13078 return 0;
13079
13080 enabled_pipe = intel_crtc->pipe;
13081 }
13082
13083 if (enabled_pipe != INVALID_PIPE)
13084 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13085 else if (other_crtc_state)
13086 other_crtc_state->hsw_workaround_pipe = first_pipe;
13087
13088 return 0;
13089}
13090
27c329ed
ML
13091static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13092{
13093 struct drm_crtc *crtc;
13094 struct drm_crtc_state *crtc_state;
13095 int ret = 0;
13096
13097 /* add all active pipes to the state */
13098 for_each_crtc(state->dev, crtc) {
13099 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13100 if (IS_ERR(crtc_state))
13101 return PTR_ERR(crtc_state);
13102
13103 if (!crtc_state->active || needs_modeset(crtc_state))
13104 continue;
13105
13106 crtc_state->mode_changed = true;
13107
13108 ret = drm_atomic_add_affected_connectors(state, crtc);
13109 if (ret)
13110 break;
13111
13112 ret = drm_atomic_add_affected_planes(state, crtc);
13113 if (ret)
13114 break;
13115 }
13116
13117 return ret;
13118}
13119
13120
054518dd 13121/* Code that should eventually be part of atomic_check() */
c347a676 13122static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
13123{
13124 struct drm_device *dev = state->dev;
27c329ed 13125 struct drm_i915_private *dev_priv = dev->dev_private;
054518dd
ACO
13126 int ret;
13127
b359283a
ML
13128 if (!check_digital_port_conflicts(state)) {
13129 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13130 return -EINVAL;
13131 }
13132
054518dd
ACO
13133 /*
13134 * See if the config requires any additional preparation, e.g.
13135 * to adjust global state with pipes off. We need to do this
13136 * here so we can get the modeset_pipe updated config for the new
13137 * mode set on this crtc. For other crtcs we need to use the
13138 * adjusted_mode bits in the crtc directly.
13139 */
27c329ed
ML
13140 if (dev_priv->display.modeset_calc_cdclk) {
13141 unsigned int cdclk;
b432e5cf 13142
27c329ed
ML
13143 ret = dev_priv->display.modeset_calc_cdclk(state);
13144
13145 cdclk = to_intel_atomic_state(state)->cdclk;
13146 if (!ret && cdclk != dev_priv->cdclk_freq)
13147 ret = intel_modeset_all_pipes(state);
13148
13149 if (ret < 0)
054518dd 13150 return ret;
27c329ed
ML
13151 } else
13152 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
054518dd 13153
ad421372 13154 intel_modeset_clear_plls(state);
054518dd 13155
99d736a2 13156 if (IS_HASWELL(dev))
ad421372 13157 return haswell_mode_set_planes_workaround(state);
99d736a2 13158
ad421372 13159 return 0;
c347a676
ACO
13160}
13161
13162static int
13163intel_modeset_compute_config(struct drm_atomic_state *state)
13164{
13165 struct drm_crtc *crtc;
13166 struct drm_crtc_state *crtc_state;
13167 int ret, i;
61333b60 13168 bool any_ms = false;
c347a676
ACO
13169
13170 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
13171 if (ret)
13172 return ret;
13173
c347a676 13174 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13175 struct intel_crtc_state *pipe_config =
13176 to_intel_crtc_state(crtc_state);
5c1e3426 13177 bool modeset, recalc = false;
cfb23ed6 13178
61333b60
ML
13179 if (!crtc_state->enable) {
13180 if (needs_modeset(crtc_state))
13181 any_ms = true;
c347a676 13182 continue;
61333b60 13183 }
c347a676 13184
cfb23ed6 13185 modeset = needs_modeset(crtc_state);
5c1e3426
ML
13186 /* see comment in intel_modeset_readout_hw_state */
13187 if (!modeset && crtc_state->mode_blob != crtc->state->mode_blob &&
13188 pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE)
13189 recalc = true;
cfb23ed6
ML
13190
13191 if (!modeset && !recalc)
13192 continue;
13193
13194 if (recalc) {
b359283a
ML
13195 ret = drm_atomic_add_affected_connectors(state, crtc);
13196 if (ret)
13197 return ret;
13198 }
13199
cfb23ed6 13200 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13201 if (ret)
13202 return ret;
13203
5c1e3426
ML
13204 if (recalc && (!i915.fastboot ||
13205 !intel_pipe_config_compare(state->dev,
cfb23ed6 13206 to_intel_crtc_state(crtc->state),
5c1e3426 13207 pipe_config, true))) {
cfb23ed6
ML
13208 modeset = crtc_state->mode_changed = true;
13209
13210 ret = drm_atomic_add_affected_planes(state, crtc);
13211 if (ret)
13212 return ret;
13213 }
61333b60 13214
cfb23ed6 13215 any_ms = modeset;
c347a676 13216 intel_dump_pipe_config(to_intel_crtc(crtc),
cfb23ed6
ML
13217 pipe_config,
13218 modeset ? "[modeset]" : "[fastboot]");
c347a676
ACO
13219 }
13220
61333b60
ML
13221 if (any_ms) {
13222 ret = intel_modeset_checks(state);
13223
13224 if (ret)
13225 return ret;
27c329ed
ML
13226 } else
13227 to_intel_atomic_state(state)->cdclk =
13228 to_i915(state->dev)->cdclk_freq;
c347a676
ACO
13229
13230 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
13231}
13232
c72d969b 13233static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 13234{
c72d969b 13235 struct drm_device *dev = state->dev;
fbee40df 13236 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
13237 struct drm_crtc *crtc;
13238 struct drm_crtc_state *crtc_state;
c0c36b94 13239 int ret = 0;
0a9ab303 13240 int i;
61333b60 13241 bool any_ms = false;
a6778b3c 13242
d4afb8cc
ACO
13243 ret = drm_atomic_helper_prepare_planes(dev, state);
13244 if (ret)
13245 return ret;
13246
1c5e19f8
ML
13247 drm_atomic_helper_swap_state(dev, state);
13248
0a9ab303 13249 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13251
61333b60
ML
13252 if (!needs_modeset(crtc->state))
13253 continue;
13254
13255 any_ms = true;
a539205a 13256 intel_pre_plane_update(intel_crtc);
460da916 13257
a539205a
ML
13258 if (crtc_state->active) {
13259 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13260 dev_priv->display.crtc_disable(crtc);
eddfcbcd
ML
13261 intel_crtc->active = false;
13262 intel_disable_shared_dpll(intel_crtc);
a539205a 13263 }
b8cecdf5 13264 }
7758a113 13265
ea9d758d
DV
13266 /* Only after disabling all output pipelines that will be changed can we
13267 * update the the output configuration. */
0a9ab303 13268 intel_modeset_update_state(state);
f6e5b160 13269
a821fc46
ACO
13270 /* The state has been swaped above, so state actually contains the
13271 * old state now. */
61333b60
ML
13272 if (any_ms)
13273 modeset_update_crtc_power_domains(state);
47fab737 13274
a6778b3c 13275 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13276 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13277 if (needs_modeset(crtc->state) && crtc->state->active) {
13278 update_scanline_offset(to_intel_crtc(crtc));
13279 dev_priv->display.crtc_enable(crtc);
13280 }
80715b2f 13281
a539205a 13282 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
80715b2f 13283 }
a6778b3c 13284
a6778b3c 13285 /* FIXME: add subpixel order */
83a57153 13286
d4afb8cc
ACO
13287 drm_atomic_helper_cleanup_planes(dev, state);
13288
2bfb4627
ACO
13289 drm_atomic_state_free(state);
13290
9eb45f22 13291 return 0;
f6e5b160
CW
13292}
13293
568c634a 13294static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13295{
568c634a 13296 struct drm_device *dev = state->dev;
f30da187
DV
13297 int ret;
13298
568c634a 13299 ret = __intel_set_mode(state);
f30da187 13300 if (ret == 0)
568c634a 13301 intel_modeset_check_state(dev);
f30da187
DV
13302
13303 return ret;
13304}
13305
568c634a 13306static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13307{
568c634a 13308 int ret;
83a57153 13309
568c634a 13310 ret = intel_modeset_compute_config(state);
83a57153 13311 if (ret)
568c634a 13312 return ret;
7f27126e 13313
568c634a 13314 return intel_set_mode_checked(state);
7f27126e
JB
13315}
13316
c0c36b94
CW
13317void intel_crtc_restore_mode(struct drm_crtc *crtc)
13318{
83a57153
ACO
13319 struct drm_device *dev = crtc->dev;
13320 struct drm_atomic_state *state;
e694eb02 13321 struct drm_crtc_state *crtc_state;
2bfb4627 13322 int ret;
83a57153
ACO
13323
13324 state = drm_atomic_state_alloc(dev);
13325 if (!state) {
e694eb02 13326 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13327 crtc->base.id);
13328 return;
13329 }
13330
e694eb02 13331 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13332
e694eb02
ML
13333retry:
13334 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13335 ret = PTR_ERR_OR_ZERO(crtc_state);
13336 if (!ret) {
13337 if (!crtc_state->active)
13338 goto out;
83a57153 13339
e694eb02
ML
13340 crtc_state->mode_changed = true;
13341 ret = intel_set_mode(state);
83a57153
ACO
13342 }
13343
e694eb02
ML
13344 if (ret == -EDEADLK) {
13345 drm_atomic_state_clear(state);
13346 drm_modeset_backoff(state->acquire_ctx);
13347 goto retry;
4ed9fb37 13348 }
4be07317 13349
2bfb4627 13350 if (ret)
e694eb02 13351out:
2bfb4627 13352 drm_atomic_state_free(state);
c0c36b94
CW
13353}
13354
25c5b266
DV
13355#undef for_each_intel_crtc_masked
13356
b7885264
ACO
13357static bool intel_connector_in_mode_set(struct intel_connector *connector,
13358 struct drm_mode_set *set)
13359{
13360 int ro;
13361
13362 for (ro = 0; ro < set->num_connectors; ro++)
13363 if (set->connectors[ro] == &connector->base)
13364 return true;
13365
13366 return false;
13367}
13368
2e431051 13369static int
9a935856
DV
13370intel_modeset_stage_output_state(struct drm_device *dev,
13371 struct drm_mode_set *set,
944b0c76 13372 struct drm_atomic_state *state)
50f56119 13373{
9a935856 13374 struct intel_connector *connector;
d5432a9d 13375 struct drm_connector *drm_connector;
944b0c76 13376 struct drm_connector_state *connector_state;
d5432a9d
ACO
13377 struct drm_crtc *crtc;
13378 struct drm_crtc_state *crtc_state;
13379 int i, ret;
50f56119 13380
9abdda74 13381 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13382 * of connectors. For paranoia, double-check this. */
13383 WARN_ON(!set->fb && (set->num_connectors != 0));
13384 WARN_ON(set->fb && (set->num_connectors == 0));
13385
3a3371ff 13386 for_each_intel_connector(dev, connector) {
b7885264
ACO
13387 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13388
d5432a9d
ACO
13389 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13390 continue;
13391
13392 connector_state =
13393 drm_atomic_get_connector_state(state, &connector->base);
13394 if (IS_ERR(connector_state))
13395 return PTR_ERR(connector_state);
13396
b7885264
ACO
13397 if (in_mode_set) {
13398 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13399 connector_state->best_encoder =
13400 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13401 }
13402
d5432a9d 13403 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13404 continue;
13405
9a935856
DV
13406 /* If we disable the crtc, disable all its connectors. Also, if
13407 * the connector is on the changing crtc but not on the new
13408 * connector list, disable it. */
b7885264 13409 if (!set->fb || !in_mode_set) {
d5432a9d 13410 connector_state->best_encoder = NULL;
9a935856
DV
13411
13412 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13413 connector->base.base.id,
c23cc417 13414 connector->base.name);
9a935856 13415 }
50f56119 13416 }
9a935856 13417 /* connector->new_encoder is now updated for all connectors. */
50f56119 13418
d5432a9d
ACO
13419 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13420 connector = to_intel_connector(drm_connector);
13421
13422 if (!connector_state->best_encoder) {
13423 ret = drm_atomic_set_crtc_for_connector(connector_state,
13424 NULL);
13425 if (ret)
13426 return ret;
7668851f 13427
50f56119 13428 continue;
d5432a9d 13429 }
50f56119 13430
d5432a9d
ACO
13431 if (intel_connector_in_mode_set(connector, set)) {
13432 struct drm_crtc *crtc = connector->base.state->crtc;
13433
13434 /* If this connector was in a previous crtc, add it
13435 * to the state. We might need to disable it. */
13436 if (crtc) {
13437 crtc_state =
13438 drm_atomic_get_crtc_state(state, crtc);
13439 if (IS_ERR(crtc_state))
13440 return PTR_ERR(crtc_state);
13441 }
13442
13443 ret = drm_atomic_set_crtc_for_connector(connector_state,
13444 set->crtc);
13445 if (ret)
13446 return ret;
13447 }
50f56119
DV
13448
13449 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13450 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13451 connector_state->crtc)) {
5e2b584e 13452 return -EINVAL;
50f56119 13453 }
944b0c76 13454
9a935856
DV
13455 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13456 connector->base.base.id,
c23cc417 13457 connector->base.name,
d5432a9d 13458 connector_state->crtc->base.id);
944b0c76 13459
d5432a9d
ACO
13460 if (connector_state->best_encoder != &connector->encoder->base)
13461 connector->encoder =
13462 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13463 }
7668851f 13464
d5432a9d 13465 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13466 bool has_connectors;
13467
d5432a9d
ACO
13468 ret = drm_atomic_add_affected_connectors(state, crtc);
13469 if (ret)
13470 return ret;
4be07317 13471
49d6fa21
ML
13472 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13473 if (has_connectors != crtc_state->enable)
13474 crtc_state->enable =
13475 crtc_state->active = has_connectors;
7668851f
VS
13476 }
13477
8c7b5ccb
ACO
13478 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13479 set->fb, set->x, set->y);
13480 if (ret)
13481 return ret;
13482
13483 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13484 if (IS_ERR(crtc_state))
13485 return PTR_ERR(crtc_state);
13486
ce52299c
MR
13487 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13488 if (ret)
13489 return ret;
8c7b5ccb
ACO
13490
13491 if (set->num_connectors)
13492 crtc_state->active = true;
13493
2e431051
DV
13494 return 0;
13495}
13496
13497static int intel_crtc_set_config(struct drm_mode_set *set)
13498{
13499 struct drm_device *dev;
83a57153 13500 struct drm_atomic_state *state = NULL;
2e431051 13501 int ret;
2e431051 13502
8d3e375e
DV
13503 BUG_ON(!set);
13504 BUG_ON(!set->crtc);
13505 BUG_ON(!set->crtc->helper_private);
2e431051 13506
7e53f3a4
DV
13507 /* Enforce sane interface api - has been abused by the fb helper. */
13508 BUG_ON(!set->mode && set->fb);
13509 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13510
2e431051
DV
13511 if (set->fb) {
13512 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13513 set->crtc->base.id, set->fb->base.id,
13514 (int)set->num_connectors, set->x, set->y);
13515 } else {
13516 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13517 }
13518
13519 dev = set->crtc->dev;
13520
83a57153 13521 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13522 if (!state)
13523 return -ENOMEM;
83a57153
ACO
13524
13525 state->acquire_ctx = dev->mode_config.acquire_ctx;
13526
462a425a 13527 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13528 if (ret)
7cbf41d6 13529 goto out;
2e431051 13530
568c634a
ACO
13531 ret = intel_modeset_compute_config(state);
13532 if (ret)
7cbf41d6 13533 goto out;
50f52756 13534
1f9954d0
JB
13535 intel_update_pipe_size(to_intel_crtc(set->crtc));
13536
568c634a 13537 ret = intel_set_mode_checked(state);
2d05eae1 13538 if (ret) {
bf67dfeb
DV
13539 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13540 set->crtc->base.id, ret);
2d05eae1 13541 }
50f56119 13542
7cbf41d6 13543out:
2bfb4627
ACO
13544 if (ret)
13545 drm_atomic_state_free(state);
50f56119
DV
13546 return ret;
13547}
f6e5b160
CW
13548
13549static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13550 .gamma_set = intel_crtc_gamma_set,
50f56119 13551 .set_config = intel_crtc_set_config,
f6e5b160
CW
13552 .destroy = intel_crtc_destroy,
13553 .page_flip = intel_crtc_page_flip,
1356837e
MR
13554 .atomic_duplicate_state = intel_crtc_duplicate_state,
13555 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13556};
13557
5358901f
DV
13558static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13559 struct intel_shared_dpll *pll,
13560 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13561{
5358901f 13562 uint32_t val;
ee7b9f93 13563
f458ebbc 13564 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13565 return false;
13566
5358901f 13567 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13568 hw_state->dpll = val;
13569 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13570 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13571
13572 return val & DPLL_VCO_ENABLE;
13573}
13574
15bdd4cf
DV
13575static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13576 struct intel_shared_dpll *pll)
13577{
3e369b76
ACO
13578 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13579 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13580}
13581
e7b903d2
DV
13582static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13583 struct intel_shared_dpll *pll)
13584{
e7b903d2 13585 /* PCH refclock must be enabled first */
89eff4be 13586 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13587
3e369b76 13588 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13589
13590 /* Wait for the clocks to stabilize. */
13591 POSTING_READ(PCH_DPLL(pll->id));
13592 udelay(150);
13593
13594 /* The pixel multiplier can only be updated once the
13595 * DPLL is enabled and the clocks are stable.
13596 *
13597 * So write it again.
13598 */
3e369b76 13599 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13600 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13601 udelay(200);
13602}
13603
13604static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13605 struct intel_shared_dpll *pll)
13606{
13607 struct drm_device *dev = dev_priv->dev;
13608 struct intel_crtc *crtc;
e7b903d2
DV
13609
13610 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13611 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13612 if (intel_crtc_to_shared_dpll(crtc) == pll)
13613 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13614 }
13615
15bdd4cf
DV
13616 I915_WRITE(PCH_DPLL(pll->id), 0);
13617 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13618 udelay(200);
13619}
13620
46edb027
DV
13621static char *ibx_pch_dpll_names[] = {
13622 "PCH DPLL A",
13623 "PCH DPLL B",
13624};
13625
7c74ade1 13626static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13627{
e7b903d2 13628 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13629 int i;
13630
7c74ade1 13631 dev_priv->num_shared_dpll = 2;
ee7b9f93 13632
e72f9fbf 13633 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13634 dev_priv->shared_dplls[i].id = i;
13635 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13636 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13637 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13638 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13639 dev_priv->shared_dplls[i].get_hw_state =
13640 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13641 }
13642}
13643
7c74ade1
DV
13644static void intel_shared_dpll_init(struct drm_device *dev)
13645{
e7b903d2 13646 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13647
b6283055
VS
13648 intel_update_cdclk(dev);
13649
9cd86933
DV
13650 if (HAS_DDI(dev))
13651 intel_ddi_pll_init(dev);
13652 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13653 ibx_pch_dpll_init(dev);
13654 else
13655 dev_priv->num_shared_dpll = 0;
13656
13657 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13658}
13659
6beb8c23
MR
13660/**
13661 * intel_prepare_plane_fb - Prepare fb for usage on plane
13662 * @plane: drm plane to prepare for
13663 * @fb: framebuffer to prepare for presentation
13664 *
13665 * Prepares a framebuffer for usage on a display plane. Generally this
13666 * involves pinning the underlying object and updating the frontbuffer tracking
13667 * bits. Some older platforms need special physical address handling for
13668 * cursor planes.
13669 *
13670 * Returns 0 on success, negative error code on failure.
13671 */
13672int
13673intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13674 struct drm_framebuffer *fb,
13675 const struct drm_plane_state *new_state)
465c120c
MR
13676{
13677 struct drm_device *dev = plane->dev;
6beb8c23 13678 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23
MR
13679 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13680 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
6beb8c23 13681 int ret = 0;
465c120c 13682
ea2c67bb 13683 if (!obj)
465c120c
MR
13684 return 0;
13685
6beb8c23 13686 mutex_lock(&dev->struct_mutex);
465c120c 13687
6beb8c23
MR
13688 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13689 INTEL_INFO(dev)->cursor_needs_physical) {
13690 int align = IS_I830(dev) ? 16 * 1024 : 256;
13691 ret = i915_gem_object_attach_phys(obj, align);
13692 if (ret)
13693 DRM_DEBUG_KMS("failed to attach phys object\n");
13694 } else {
91af127f 13695 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
6beb8c23 13696 }
465c120c 13697
6beb8c23 13698 if (ret == 0)
a9ff8714 13699 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
fdd508a6 13700
4c34574f 13701 mutex_unlock(&dev->struct_mutex);
465c120c 13702
6beb8c23
MR
13703 return ret;
13704}
13705
38f3ce3a
MR
13706/**
13707 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13708 * @plane: drm plane to clean up for
13709 * @fb: old framebuffer that was on plane
13710 *
13711 * Cleans up a framebuffer that has just been removed from a plane.
13712 */
13713void
13714intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13715 struct drm_framebuffer *fb,
13716 const struct drm_plane_state *old_state)
38f3ce3a
MR
13717{
13718 struct drm_device *dev = plane->dev;
13719 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13720
13721 if (WARN_ON(!obj))
13722 return;
13723
13724 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13725 !INTEL_INFO(dev)->cursor_needs_physical) {
13726 mutex_lock(&dev->struct_mutex);
82bc3b2d 13727 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13728 mutex_unlock(&dev->struct_mutex);
13729 }
465c120c
MR
13730}
13731
6156a456
CK
13732int
13733skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13734{
13735 int max_scale;
13736 struct drm_device *dev;
13737 struct drm_i915_private *dev_priv;
13738 int crtc_clock, cdclk;
13739
13740 if (!intel_crtc || !crtc_state)
13741 return DRM_PLANE_HELPER_NO_SCALING;
13742
13743 dev = intel_crtc->base.dev;
13744 dev_priv = dev->dev_private;
13745 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13746 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456
CK
13747
13748 if (!crtc_clock || !cdclk)
13749 return DRM_PLANE_HELPER_NO_SCALING;
13750
13751 /*
13752 * skl max scale is lower of:
13753 * close to 3 but not 3, -1 is for that purpose
13754 * or
13755 * cdclk/crtc_clock
13756 */
13757 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13758
13759 return max_scale;
13760}
13761
465c120c 13762static int
3c692a41 13763intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13764 struct intel_crtc_state *crtc_state,
3c692a41
GP
13765 struct intel_plane_state *state)
13766{
2b875c22
MR
13767 struct drm_crtc *crtc = state->base.crtc;
13768 struct drm_framebuffer *fb = state->base.fb;
6156a456 13769 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13770 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13771 bool can_position = false;
465c120c 13772
061e4b8d
ML
13773 /* use scaler when colorkey is not required */
13774 if (INTEL_INFO(plane->dev)->gen >= 9 &&
818ed961 13775 state->ckey.flags == I915_SET_COLORKEY_NONE) {
061e4b8d
ML
13776 min_scale = 1;
13777 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
d8106366 13778 can_position = true;
6156a456 13779 }
d8106366 13780
061e4b8d
ML
13781 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13782 &state->dst, &state->clip,
da20eabd
ML
13783 min_scale, max_scale,
13784 can_position, true,
13785 &state->visible);
14af293f
GP
13786}
13787
13788static void
13789intel_commit_primary_plane(struct drm_plane *plane,
13790 struct intel_plane_state *state)
13791{
2b875c22
MR
13792 struct drm_crtc *crtc = state->base.crtc;
13793 struct drm_framebuffer *fb = state->base.fb;
13794 struct drm_device *dev = plane->dev;
14af293f 13795 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13796 struct intel_crtc *intel_crtc;
14af293f
GP
13797 struct drm_rect *src = &state->src;
13798
ea2c67bb
MR
13799 crtc = crtc ? crtc : plane->crtc;
13800 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13801
13802 plane->fb = fb;
9dc806fc
MR
13803 crtc->x = src->x1 >> 16;
13804 crtc->y = src->y1 >> 16;
ccc759dc 13805
a539205a 13806 if (!crtc->state->active)
302d19ac 13807 return;
465c120c 13808
302d19ac
ML
13809 if (state->visible)
13810 /* FIXME: kill this fastboot hack */
13811 intel_update_pipe_size(intel_crtc);
13812
13813 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
465c120c
MR
13814}
13815
a8ad0d8e
ML
13816static void
13817intel_disable_primary_plane(struct drm_plane *plane,
7fabf5ef 13818 struct drm_crtc *crtc)
a8ad0d8e
ML
13819{
13820 struct drm_device *dev = plane->dev;
13821 struct drm_i915_private *dev_priv = dev->dev_private;
13822
a8ad0d8e
ML
13823 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13824}
13825
32b7eeec 13826static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13827{
32b7eeec 13828 struct drm_device *dev = crtc->dev;
140fd38d 13829 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3c692a41 13831
a539205a
ML
13832 if (!needs_modeset(crtc->state))
13833 intel_pre_plane_update(intel_crtc);
3c692a41 13834
f015c551 13835 if (intel_crtc->atomic.update_wm_pre)
32b7eeec 13836 intel_update_watermarks(crtc);
3c692a41 13837
32b7eeec 13838 intel_runtime_pm_get(dev_priv);
3c692a41 13839
c34c9ee4 13840 /* Perform vblank evasion around commit operation */
a539205a 13841 if (crtc->state->active)
c34c9ee4
MR
13842 intel_crtc->atomic.evade =
13843 intel_pipe_update_start(intel_crtc,
13844 &intel_crtc->atomic.start_vbl_count);
0583236e
ML
13845
13846 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13847 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13848}
13849
13850static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13851{
13852 struct drm_device *dev = crtc->dev;
13853 struct drm_i915_private *dev_priv = dev->dev_private;
13854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13855
c34c9ee4
MR
13856 if (intel_crtc->atomic.evade)
13857 intel_pipe_update_end(intel_crtc,
13858 intel_crtc->atomic.start_vbl_count);
3c692a41 13859
140fd38d 13860 intel_runtime_pm_put(dev_priv);
3c692a41 13861
ac21b225 13862 intel_post_plane_update(intel_crtc);
3c692a41
GP
13863}
13864
cf4c7c12 13865/**
4a3b8769
MR
13866 * intel_plane_destroy - destroy a plane
13867 * @plane: plane to destroy
cf4c7c12 13868 *
4a3b8769
MR
13869 * Common destruction function for all types of planes (primary, cursor,
13870 * sprite).
cf4c7c12 13871 */
4a3b8769 13872void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13873{
13874 struct intel_plane *intel_plane = to_intel_plane(plane);
13875 drm_plane_cleanup(plane);
13876 kfree(intel_plane);
13877}
13878
65a3fea0 13879const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13880 .update_plane = drm_atomic_helper_update_plane,
13881 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13882 .destroy = intel_plane_destroy,
c196e1d6 13883 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13884 .atomic_get_property = intel_plane_atomic_get_property,
13885 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13886 .atomic_duplicate_state = intel_plane_duplicate_state,
13887 .atomic_destroy_state = intel_plane_destroy_state,
13888
465c120c
MR
13889};
13890
13891static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13892 int pipe)
13893{
13894 struct intel_plane *primary;
8e7d688b 13895 struct intel_plane_state *state;
465c120c
MR
13896 const uint32_t *intel_primary_formats;
13897 int num_formats;
13898
13899 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13900 if (primary == NULL)
13901 return NULL;
13902
8e7d688b
MR
13903 state = intel_create_plane_state(&primary->base);
13904 if (!state) {
ea2c67bb
MR
13905 kfree(primary);
13906 return NULL;
13907 }
8e7d688b 13908 primary->base.state = &state->base;
ea2c67bb 13909
465c120c
MR
13910 primary->can_scale = false;
13911 primary->max_downscale = 1;
6156a456
CK
13912 if (INTEL_INFO(dev)->gen >= 9) {
13913 primary->can_scale = true;
af99ceda 13914 state->scaler_id = -1;
6156a456 13915 }
465c120c
MR
13916 primary->pipe = pipe;
13917 primary->plane = pipe;
a9ff8714 13918 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179
MR
13919 primary->check_plane = intel_check_primary_plane;
13920 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13921 primary->disable_plane = intel_disable_primary_plane;
465c120c
MR
13922 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13923 primary->plane = !pipe;
13924
6c0fd451
DL
13925 if (INTEL_INFO(dev)->gen >= 9) {
13926 intel_primary_formats = skl_primary_formats;
13927 num_formats = ARRAY_SIZE(skl_primary_formats);
13928 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13929 intel_primary_formats = i965_primary_formats;
13930 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13931 } else {
13932 intel_primary_formats = i8xx_primary_formats;
13933 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13934 }
13935
13936 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13937 &intel_plane_funcs,
465c120c
MR
13938 intel_primary_formats, num_formats,
13939 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13940
3b7a5119
SJ
13941 if (INTEL_INFO(dev)->gen >= 4)
13942 intel_create_rotation_property(dev, primary);
48404c1e 13943
ea2c67bb
MR
13944 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13945
465c120c
MR
13946 return &primary->base;
13947}
13948
3b7a5119
SJ
13949void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13950{
13951 if (!dev->mode_config.rotation_property) {
13952 unsigned long flags = BIT(DRM_ROTATE_0) |
13953 BIT(DRM_ROTATE_180);
13954
13955 if (INTEL_INFO(dev)->gen >= 9)
13956 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13957
13958 dev->mode_config.rotation_property =
13959 drm_mode_create_rotation_property(dev, flags);
13960 }
13961 if (dev->mode_config.rotation_property)
13962 drm_object_attach_property(&plane->base.base,
13963 dev->mode_config.rotation_property,
13964 plane->base.state->rotation);
13965}
13966
3d7d6510 13967static int
852e787c 13968intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13969 struct intel_crtc_state *crtc_state,
852e787c 13970 struct intel_plane_state *state)
3d7d6510 13971{
061e4b8d 13972 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13973 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13974 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
757f9a3e
GP
13975 unsigned stride;
13976 int ret;
3d7d6510 13977
061e4b8d
ML
13978 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13979 &state->dst, &state->clip,
3d7d6510
MR
13980 DRM_PLANE_HELPER_NO_SCALING,
13981 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13982 true, true, &state->visible);
757f9a3e
GP
13983 if (ret)
13984 return ret;
13985
757f9a3e
GP
13986 /* if we want to turn off the cursor ignore width and height */
13987 if (!obj)
da20eabd 13988 return 0;
757f9a3e 13989
757f9a3e 13990 /* Check for which cursor types we support */
061e4b8d 13991 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13992 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13993 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13994 return -EINVAL;
13995 }
13996
ea2c67bb
MR
13997 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13998 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13999 DRM_DEBUG_KMS("buffer is too small\n");
14000 return -ENOMEM;
14001 }
14002
3a656b54 14003 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14004 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14005 return -EINVAL;
32b7eeec
MR
14006 }
14007
da20eabd 14008 return 0;
852e787c 14009}
3d7d6510 14010
a8ad0d8e
ML
14011static void
14012intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14013 struct drm_crtc *crtc)
a8ad0d8e 14014{
a8ad0d8e
ML
14015 intel_crtc_update_cursor(crtc, false);
14016}
14017
f4a2cf29 14018static void
852e787c
GP
14019intel_commit_cursor_plane(struct drm_plane *plane,
14020 struct intel_plane_state *state)
14021{
2b875c22 14022 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
14023 struct drm_device *dev = plane->dev;
14024 struct intel_crtc *intel_crtc;
2b875c22 14025 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14026 uint32_t addr;
852e787c 14027
ea2c67bb
MR
14028 crtc = crtc ? crtc : plane->crtc;
14029 intel_crtc = to_intel_crtc(crtc);
14030
2b875c22 14031 plane->fb = state->base.fb;
ea2c67bb
MR
14032 crtc->cursor_x = state->base.crtc_x;
14033 crtc->cursor_y = state->base.crtc_y;
14034
a912f12f
GP
14035 if (intel_crtc->cursor_bo == obj)
14036 goto update;
4ed91096 14037
f4a2cf29 14038 if (!obj)
a912f12f 14039 addr = 0;
f4a2cf29 14040 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14041 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14042 else
a912f12f 14043 addr = obj->phys_handle->busaddr;
852e787c 14044
a912f12f
GP
14045 intel_crtc->cursor_addr = addr;
14046 intel_crtc->cursor_bo = obj;
852e787c 14047
302d19ac 14048update:
a539205a 14049 if (crtc->state->active)
a912f12f 14050 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
14051}
14052
3d7d6510
MR
14053static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14054 int pipe)
14055{
14056 struct intel_plane *cursor;
8e7d688b 14057 struct intel_plane_state *state;
3d7d6510
MR
14058
14059 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14060 if (cursor == NULL)
14061 return NULL;
14062
8e7d688b
MR
14063 state = intel_create_plane_state(&cursor->base);
14064 if (!state) {
ea2c67bb
MR
14065 kfree(cursor);
14066 return NULL;
14067 }
8e7d688b 14068 cursor->base.state = &state->base;
ea2c67bb 14069
3d7d6510
MR
14070 cursor->can_scale = false;
14071 cursor->max_downscale = 1;
14072 cursor->pipe = pipe;
14073 cursor->plane = pipe;
a9ff8714 14074 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179
MR
14075 cursor->check_plane = intel_check_cursor_plane;
14076 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 14077 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14078
14079 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14080 &intel_plane_funcs,
3d7d6510
MR
14081 intel_cursor_formats,
14082 ARRAY_SIZE(intel_cursor_formats),
14083 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
14084
14085 if (INTEL_INFO(dev)->gen >= 4) {
14086 if (!dev->mode_config.rotation_property)
14087 dev->mode_config.rotation_property =
14088 drm_mode_create_rotation_property(dev,
14089 BIT(DRM_ROTATE_0) |
14090 BIT(DRM_ROTATE_180));
14091 if (dev->mode_config.rotation_property)
14092 drm_object_attach_property(&cursor->base.base,
14093 dev->mode_config.rotation_property,
8e7d688b 14094 state->base.rotation);
4398ad45
VS
14095 }
14096
af99ceda
CK
14097 if (INTEL_INFO(dev)->gen >=9)
14098 state->scaler_id = -1;
14099
ea2c67bb
MR
14100 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14101
3d7d6510
MR
14102 return &cursor->base;
14103}
14104
549e2bfb
CK
14105static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14106 struct intel_crtc_state *crtc_state)
14107{
14108 int i;
14109 struct intel_scaler *intel_scaler;
14110 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14111
14112 for (i = 0; i < intel_crtc->num_scalers; i++) {
14113 intel_scaler = &scaler_state->scalers[i];
14114 intel_scaler->in_use = 0;
549e2bfb
CK
14115 intel_scaler->mode = PS_SCALER_MODE_DYN;
14116 }
14117
14118 scaler_state->scaler_id = -1;
14119}
14120
b358d0a6 14121static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14122{
fbee40df 14123 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14124 struct intel_crtc *intel_crtc;
f5de6e07 14125 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14126 struct drm_plane *primary = NULL;
14127 struct drm_plane *cursor = NULL;
465c120c 14128 int i, ret;
79e53945 14129
955382f3 14130 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14131 if (intel_crtc == NULL)
14132 return;
14133
f5de6e07
ACO
14134 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14135 if (!crtc_state)
14136 goto fail;
550acefd
ACO
14137 intel_crtc->config = crtc_state;
14138 intel_crtc->base.state = &crtc_state->base;
07878248 14139 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14140
549e2bfb
CK
14141 /* initialize shared scalers */
14142 if (INTEL_INFO(dev)->gen >= 9) {
14143 if (pipe == PIPE_C)
14144 intel_crtc->num_scalers = 1;
14145 else
14146 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14147
14148 skl_init_scalers(dev, intel_crtc, crtc_state);
14149 }
14150
465c120c 14151 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14152 if (!primary)
14153 goto fail;
14154
14155 cursor = intel_cursor_plane_create(dev, pipe);
14156 if (!cursor)
14157 goto fail;
14158
465c120c 14159 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14160 cursor, &intel_crtc_funcs);
14161 if (ret)
14162 goto fail;
79e53945
JB
14163
14164 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14165 for (i = 0; i < 256; i++) {
14166 intel_crtc->lut_r[i] = i;
14167 intel_crtc->lut_g[i] = i;
14168 intel_crtc->lut_b[i] = i;
14169 }
14170
1f1c2e24
VS
14171 /*
14172 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14173 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14174 */
80824003
JB
14175 intel_crtc->pipe = pipe;
14176 intel_crtc->plane = pipe;
3a77c4c4 14177 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14178 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14179 intel_crtc->plane = !pipe;
80824003
JB
14180 }
14181
4b0e333e
CW
14182 intel_crtc->cursor_base = ~0;
14183 intel_crtc->cursor_cntl = ~0;
dc41c154 14184 intel_crtc->cursor_size = ~0;
8d7849db 14185
852eb00d
VS
14186 intel_crtc->wm.cxsr_allowed = true;
14187
22fd0fab
JB
14188 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14189 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14190 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14191 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14192
79e53945 14193 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14194
14195 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14196 return;
14197
14198fail:
14199 if (primary)
14200 drm_plane_cleanup(primary);
14201 if (cursor)
14202 drm_plane_cleanup(cursor);
f5de6e07 14203 kfree(crtc_state);
3d7d6510 14204 kfree(intel_crtc);
79e53945
JB
14205}
14206
752aa88a
JB
14207enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14208{
14209 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14210 struct drm_device *dev = connector->base.dev;
752aa88a 14211
51fd371b 14212 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14213
d3babd3f 14214 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14215 return INVALID_PIPE;
14216
14217 return to_intel_crtc(encoder->crtc)->pipe;
14218}
14219
08d7b3d1 14220int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14221 struct drm_file *file)
08d7b3d1 14222{
08d7b3d1 14223 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14224 struct drm_crtc *drmmode_crtc;
c05422d5 14225 struct intel_crtc *crtc;
08d7b3d1 14226
7707e653 14227 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14228
7707e653 14229 if (!drmmode_crtc) {
08d7b3d1 14230 DRM_ERROR("no such CRTC id\n");
3f2c2057 14231 return -ENOENT;
08d7b3d1
CW
14232 }
14233
7707e653 14234 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14235 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14236
c05422d5 14237 return 0;
08d7b3d1
CW
14238}
14239
66a9278e 14240static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14241{
66a9278e
DV
14242 struct drm_device *dev = encoder->base.dev;
14243 struct intel_encoder *source_encoder;
79e53945 14244 int index_mask = 0;
79e53945
JB
14245 int entry = 0;
14246
b2784e15 14247 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14248 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14249 index_mask |= (1 << entry);
14250
79e53945
JB
14251 entry++;
14252 }
4ef69c7a 14253
79e53945
JB
14254 return index_mask;
14255}
14256
4d302442
CW
14257static bool has_edp_a(struct drm_device *dev)
14258{
14259 struct drm_i915_private *dev_priv = dev->dev_private;
14260
14261 if (!IS_MOBILE(dev))
14262 return false;
14263
14264 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14265 return false;
14266
e3589908 14267 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14268 return false;
14269
14270 return true;
14271}
14272
84b4e042
JB
14273static bool intel_crt_present(struct drm_device *dev)
14274{
14275 struct drm_i915_private *dev_priv = dev->dev_private;
14276
884497ed
DL
14277 if (INTEL_INFO(dev)->gen >= 9)
14278 return false;
14279
cf404ce4 14280 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14281 return false;
14282
14283 if (IS_CHERRYVIEW(dev))
14284 return false;
14285
14286 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14287 return false;
14288
14289 return true;
14290}
14291
79e53945
JB
14292static void intel_setup_outputs(struct drm_device *dev)
14293{
725e30ad 14294 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14295 struct intel_encoder *encoder;
cb0953d7 14296 bool dpd_is_edp = false;
79e53945 14297
c9093354 14298 intel_lvds_init(dev);
79e53945 14299
84b4e042 14300 if (intel_crt_present(dev))
79935fca 14301 intel_crt_init(dev);
cb0953d7 14302
c776eb2e
VK
14303 if (IS_BROXTON(dev)) {
14304 /*
14305 * FIXME: Broxton doesn't support port detection via the
14306 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14307 * detect the ports.
14308 */
14309 intel_ddi_init(dev, PORT_A);
14310 intel_ddi_init(dev, PORT_B);
14311 intel_ddi_init(dev, PORT_C);
14312 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14313 int found;
14314
de31facd
JB
14315 /*
14316 * Haswell uses DDI functions to detect digital outputs.
14317 * On SKL pre-D0 the strap isn't connected, so we assume
14318 * it's there.
14319 */
0e72a5b5 14320 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14321 /* WaIgnoreDDIAStrap: skl */
14322 if (found ||
14323 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14324 intel_ddi_init(dev, PORT_A);
14325
14326 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14327 * register */
14328 found = I915_READ(SFUSE_STRAP);
14329
14330 if (found & SFUSE_STRAP_DDIB_DETECTED)
14331 intel_ddi_init(dev, PORT_B);
14332 if (found & SFUSE_STRAP_DDIC_DETECTED)
14333 intel_ddi_init(dev, PORT_C);
14334 if (found & SFUSE_STRAP_DDID_DETECTED)
14335 intel_ddi_init(dev, PORT_D);
14336 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14337 int found;
5d8a7752 14338 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14339
14340 if (has_edp_a(dev))
14341 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14342
dc0fa718 14343 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14344 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14345 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14346 if (!found)
e2debe91 14347 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14348 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14349 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14350 }
14351
dc0fa718 14352 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14353 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14354
dc0fa718 14355 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14356 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14357
5eb08b69 14358 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14359 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14360
270b3042 14361 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14362 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14363 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14364 /*
14365 * The DP_DETECTED bit is the latched state of the DDC
14366 * SDA pin at boot. However since eDP doesn't require DDC
14367 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14368 * eDP ports may have been muxed to an alternate function.
14369 * Thus we can't rely on the DP_DETECTED bit alone to detect
14370 * eDP ports. Consult the VBT as well as DP_DETECTED to
14371 * detect eDP ports.
14372 */
d2182a66
VS
14373 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14374 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14375 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14376 PORT_B);
e17ac6db
VS
14377 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14378 intel_dp_is_edp(dev, PORT_B))
14379 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14380
d2182a66
VS
14381 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14382 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14383 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14384 PORT_C);
e17ac6db
VS
14385 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14386 intel_dp_is_edp(dev, PORT_C))
14387 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14388
9418c1f1 14389 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14390 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14391 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14392 PORT_D);
e17ac6db
VS
14393 /* eDP not supported on port D, so don't check VBT */
14394 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14395 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14396 }
14397
3cfca973 14398 intel_dsi_init(dev);
09da55dc 14399 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14400 bool found = false;
7d57382e 14401
e2debe91 14402 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14403 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14404 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
3fec3d2f 14405 if (!found && IS_G4X(dev)) {
b01f2c3a 14406 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14407 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14408 }
27185ae1 14409
3fec3d2f 14410 if (!found && IS_G4X(dev))
ab9d7c30 14411 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14412 }
13520b05
KH
14413
14414 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14415
e2debe91 14416 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14417 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14418 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14419 }
27185ae1 14420
e2debe91 14421 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14422
3fec3d2f 14423 if (IS_G4X(dev)) {
b01f2c3a 14424 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14425 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14426 }
3fec3d2f 14427 if (IS_G4X(dev))
ab9d7c30 14428 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14429 }
27185ae1 14430
3fec3d2f 14431 if (IS_G4X(dev) &&
e7281eab 14432 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14433 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14434 } else if (IS_GEN2(dev))
79e53945
JB
14435 intel_dvo_init(dev);
14436
103a196f 14437 if (SUPPORTS_TV(dev))
79e53945
JB
14438 intel_tv_init(dev);
14439
0bc12bcb 14440 intel_psr_init(dev);
7c8f8a70 14441
b2784e15 14442 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14443 encoder->base.possible_crtcs = encoder->crtc_mask;
14444 encoder->base.possible_clones =
66a9278e 14445 intel_encoder_clones(encoder);
79e53945 14446 }
47356eb6 14447
dde86e2d 14448 intel_init_pch_refclk(dev);
270b3042
DV
14449
14450 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14451}
14452
14453static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14454{
60a5ca01 14455 struct drm_device *dev = fb->dev;
79e53945 14456 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14457
ef2d633e 14458 drm_framebuffer_cleanup(fb);
60a5ca01 14459 mutex_lock(&dev->struct_mutex);
ef2d633e 14460 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14461 drm_gem_object_unreference(&intel_fb->obj->base);
14462 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14463 kfree(intel_fb);
14464}
14465
14466static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14467 struct drm_file *file,
79e53945
JB
14468 unsigned int *handle)
14469{
14470 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14471 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14472
05394f39 14473 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14474}
14475
86c98588
RV
14476static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14477 struct drm_file *file,
14478 unsigned flags, unsigned color,
14479 struct drm_clip_rect *clips,
14480 unsigned num_clips)
14481{
14482 struct drm_device *dev = fb->dev;
14483 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14484 struct drm_i915_gem_object *obj = intel_fb->obj;
14485
14486 mutex_lock(&dev->struct_mutex);
14487 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
14488 mutex_unlock(&dev->struct_mutex);
14489
14490 return 0;
14491}
14492
79e53945
JB
14493static const struct drm_framebuffer_funcs intel_fb_funcs = {
14494 .destroy = intel_user_framebuffer_destroy,
14495 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14496 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14497};
14498
b321803d
DL
14499static
14500u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14501 uint32_t pixel_format)
14502{
14503 u32 gen = INTEL_INFO(dev)->gen;
14504
14505 if (gen >= 9) {
14506 /* "The stride in bytes must not exceed the of the size of 8K
14507 * pixels and 32K bytes."
14508 */
14509 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14510 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14511 return 32*1024;
14512 } else if (gen >= 4) {
14513 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14514 return 16*1024;
14515 else
14516 return 32*1024;
14517 } else if (gen >= 3) {
14518 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14519 return 8*1024;
14520 else
14521 return 16*1024;
14522 } else {
14523 /* XXX DSPC is limited to 4k tiled */
14524 return 8*1024;
14525 }
14526}
14527
b5ea642a
DV
14528static int intel_framebuffer_init(struct drm_device *dev,
14529 struct intel_framebuffer *intel_fb,
14530 struct drm_mode_fb_cmd2 *mode_cmd,
14531 struct drm_i915_gem_object *obj)
79e53945 14532{
6761dd31 14533 unsigned int aligned_height;
79e53945 14534 int ret;
b321803d 14535 u32 pitch_limit, stride_alignment;
79e53945 14536
dd4916c5
DV
14537 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14538
2a80eada
DV
14539 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14540 /* Enforce that fb modifier and tiling mode match, but only for
14541 * X-tiled. This is needed for FBC. */
14542 if (!!(obj->tiling_mode == I915_TILING_X) !=
14543 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14544 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14545 return -EINVAL;
14546 }
14547 } else {
14548 if (obj->tiling_mode == I915_TILING_X)
14549 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14550 else if (obj->tiling_mode == I915_TILING_Y) {
14551 DRM_DEBUG("No Y tiling for legacy addfb\n");
14552 return -EINVAL;
14553 }
14554 }
14555
9a8f0a12
TU
14556 /* Passed in modifier sanity checking. */
14557 switch (mode_cmd->modifier[0]) {
14558 case I915_FORMAT_MOD_Y_TILED:
14559 case I915_FORMAT_MOD_Yf_TILED:
14560 if (INTEL_INFO(dev)->gen < 9) {
14561 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14562 mode_cmd->modifier[0]);
14563 return -EINVAL;
14564 }
14565 case DRM_FORMAT_MOD_NONE:
14566 case I915_FORMAT_MOD_X_TILED:
14567 break;
14568 default:
c0f40428
JB
14569 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14570 mode_cmd->modifier[0]);
57cd6508 14571 return -EINVAL;
c16ed4be 14572 }
57cd6508 14573
b321803d
DL
14574 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14575 mode_cmd->pixel_format);
14576 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14577 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14578 mode_cmd->pitches[0], stride_alignment);
57cd6508 14579 return -EINVAL;
c16ed4be 14580 }
57cd6508 14581
b321803d
DL
14582 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14583 mode_cmd->pixel_format);
a35cdaa0 14584 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14585 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14586 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14587 "tiled" : "linear",
a35cdaa0 14588 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14589 return -EINVAL;
c16ed4be 14590 }
5d7bd705 14591
2a80eada 14592 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14593 mode_cmd->pitches[0] != obj->stride) {
14594 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14595 mode_cmd->pitches[0], obj->stride);
5d7bd705 14596 return -EINVAL;
c16ed4be 14597 }
5d7bd705 14598
57779d06 14599 /* Reject formats not supported by any plane early. */
308e5bcb 14600 switch (mode_cmd->pixel_format) {
57779d06 14601 case DRM_FORMAT_C8:
04b3924d
VS
14602 case DRM_FORMAT_RGB565:
14603 case DRM_FORMAT_XRGB8888:
14604 case DRM_FORMAT_ARGB8888:
57779d06
VS
14605 break;
14606 case DRM_FORMAT_XRGB1555:
c16ed4be 14607 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14608 DRM_DEBUG("unsupported pixel format: %s\n",
14609 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14610 return -EINVAL;
c16ed4be 14611 }
57779d06 14612 break;
57779d06 14613 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14614 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14615 DRM_DEBUG("unsupported pixel format: %s\n",
14616 drm_get_format_name(mode_cmd->pixel_format));
14617 return -EINVAL;
14618 }
14619 break;
14620 case DRM_FORMAT_XBGR8888:
04b3924d 14621 case DRM_FORMAT_XRGB2101010:
57779d06 14622 case DRM_FORMAT_XBGR2101010:
c16ed4be 14623 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14624 DRM_DEBUG("unsupported pixel format: %s\n",
14625 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14626 return -EINVAL;
c16ed4be 14627 }
b5626747 14628 break;
7531208b
DL
14629 case DRM_FORMAT_ABGR2101010:
14630 if (!IS_VALLEYVIEW(dev)) {
14631 DRM_DEBUG("unsupported pixel format: %s\n",
14632 drm_get_format_name(mode_cmd->pixel_format));
14633 return -EINVAL;
14634 }
14635 break;
04b3924d
VS
14636 case DRM_FORMAT_YUYV:
14637 case DRM_FORMAT_UYVY:
14638 case DRM_FORMAT_YVYU:
14639 case DRM_FORMAT_VYUY:
c16ed4be 14640 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14641 DRM_DEBUG("unsupported pixel format: %s\n",
14642 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14643 return -EINVAL;
c16ed4be 14644 }
57cd6508
CW
14645 break;
14646 default:
4ee62c76
VS
14647 DRM_DEBUG("unsupported pixel format: %s\n",
14648 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14649 return -EINVAL;
14650 }
14651
90f9a336
VS
14652 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14653 if (mode_cmd->offsets[0] != 0)
14654 return -EINVAL;
14655
ec2c981e 14656 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14657 mode_cmd->pixel_format,
14658 mode_cmd->modifier[0]);
53155c0a
DV
14659 /* FIXME drm helper for size checks (especially planar formats)? */
14660 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14661 return -EINVAL;
14662
c7d73f6a
DV
14663 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14664 intel_fb->obj = obj;
80075d49 14665 intel_fb->obj->framebuffer_references++;
c7d73f6a 14666
79e53945
JB
14667 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14668 if (ret) {
14669 DRM_ERROR("framebuffer init failed %d\n", ret);
14670 return ret;
14671 }
14672
79e53945
JB
14673 return 0;
14674}
14675
79e53945
JB
14676static struct drm_framebuffer *
14677intel_user_framebuffer_create(struct drm_device *dev,
14678 struct drm_file *filp,
308e5bcb 14679 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14680{
05394f39 14681 struct drm_i915_gem_object *obj;
79e53945 14682
308e5bcb
JB
14683 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14684 mode_cmd->handles[0]));
c8725226 14685 if (&obj->base == NULL)
cce13ff7 14686 return ERR_PTR(-ENOENT);
79e53945 14687
d2dff872 14688 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14689}
14690
4520f53a 14691#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14692static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14693{
14694}
14695#endif
14696
79e53945 14697static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14698 .fb_create = intel_user_framebuffer_create,
0632fef6 14699 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14700 .atomic_check = intel_atomic_check,
14701 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14702 .atomic_state_alloc = intel_atomic_state_alloc,
14703 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14704};
14705
e70236a8
JB
14706/* Set up chip specific display functions */
14707static void intel_init_display(struct drm_device *dev)
14708{
14709 struct drm_i915_private *dev_priv = dev->dev_private;
14710
ee9300bb
DV
14711 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14712 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14713 else if (IS_CHERRYVIEW(dev))
14714 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14715 else if (IS_VALLEYVIEW(dev))
14716 dev_priv->display.find_dpll = vlv_find_best_dpll;
14717 else if (IS_PINEVIEW(dev))
14718 dev_priv->display.find_dpll = pnv_find_best_dpll;
14719 else
14720 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14721
bc8d7dff
DL
14722 if (INTEL_INFO(dev)->gen >= 9) {
14723 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14724 dev_priv->display.get_initial_plane_config =
14725 skylake_get_initial_plane_config;
bc8d7dff
DL
14726 dev_priv->display.crtc_compute_clock =
14727 haswell_crtc_compute_clock;
14728 dev_priv->display.crtc_enable = haswell_crtc_enable;
14729 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14730 dev_priv->display.update_primary_plane =
14731 skylake_update_primary_plane;
14732 } else if (HAS_DDI(dev)) {
0e8ffe1b 14733 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14734 dev_priv->display.get_initial_plane_config =
14735 ironlake_get_initial_plane_config;
797d0259
ACO
14736 dev_priv->display.crtc_compute_clock =
14737 haswell_crtc_compute_clock;
4f771f10
PZ
14738 dev_priv->display.crtc_enable = haswell_crtc_enable;
14739 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14740 dev_priv->display.update_primary_plane =
14741 ironlake_update_primary_plane;
09b4ddf9 14742 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14743 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14744 dev_priv->display.get_initial_plane_config =
14745 ironlake_get_initial_plane_config;
3fb37703
ACO
14746 dev_priv->display.crtc_compute_clock =
14747 ironlake_crtc_compute_clock;
76e5a89c
DV
14748 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14749 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14750 dev_priv->display.update_primary_plane =
14751 ironlake_update_primary_plane;
89b667f8
JB
14752 } else if (IS_VALLEYVIEW(dev)) {
14753 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14754 dev_priv->display.get_initial_plane_config =
14755 i9xx_get_initial_plane_config;
d6dfee7a 14756 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14757 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14758 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14759 dev_priv->display.update_primary_plane =
14760 i9xx_update_primary_plane;
f564048e 14761 } else {
0e8ffe1b 14762 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14763 dev_priv->display.get_initial_plane_config =
14764 i9xx_get_initial_plane_config;
d6dfee7a 14765 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14766 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14767 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14768 dev_priv->display.update_primary_plane =
14769 i9xx_update_primary_plane;
f564048e 14770 }
e70236a8 14771
e70236a8 14772 /* Returns the core display clock speed */
1652d19e
VS
14773 if (IS_SKYLAKE(dev))
14774 dev_priv->display.get_display_clock_speed =
14775 skylake_get_display_clock_speed;
acd3f3d3
BP
14776 else if (IS_BROXTON(dev))
14777 dev_priv->display.get_display_clock_speed =
14778 broxton_get_display_clock_speed;
1652d19e
VS
14779 else if (IS_BROADWELL(dev))
14780 dev_priv->display.get_display_clock_speed =
14781 broadwell_get_display_clock_speed;
14782 else if (IS_HASWELL(dev))
14783 dev_priv->display.get_display_clock_speed =
14784 haswell_get_display_clock_speed;
14785 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14786 dev_priv->display.get_display_clock_speed =
14787 valleyview_get_display_clock_speed;
b37a6434
VS
14788 else if (IS_GEN5(dev))
14789 dev_priv->display.get_display_clock_speed =
14790 ilk_get_display_clock_speed;
a7c66cd8 14791 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14792 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14793 dev_priv->display.get_display_clock_speed =
14794 i945_get_display_clock_speed;
34edce2f
VS
14795 else if (IS_GM45(dev))
14796 dev_priv->display.get_display_clock_speed =
14797 gm45_get_display_clock_speed;
14798 else if (IS_CRESTLINE(dev))
14799 dev_priv->display.get_display_clock_speed =
14800 i965gm_get_display_clock_speed;
14801 else if (IS_PINEVIEW(dev))
14802 dev_priv->display.get_display_clock_speed =
14803 pnv_get_display_clock_speed;
14804 else if (IS_G33(dev) || IS_G4X(dev))
14805 dev_priv->display.get_display_clock_speed =
14806 g33_get_display_clock_speed;
e70236a8
JB
14807 else if (IS_I915G(dev))
14808 dev_priv->display.get_display_clock_speed =
14809 i915_get_display_clock_speed;
257a7ffc 14810 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14811 dev_priv->display.get_display_clock_speed =
14812 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14813 else if (IS_PINEVIEW(dev))
14814 dev_priv->display.get_display_clock_speed =
14815 pnv_get_display_clock_speed;
e70236a8
JB
14816 else if (IS_I915GM(dev))
14817 dev_priv->display.get_display_clock_speed =
14818 i915gm_get_display_clock_speed;
14819 else if (IS_I865G(dev))
14820 dev_priv->display.get_display_clock_speed =
14821 i865_get_display_clock_speed;
f0f8a9ce 14822 else if (IS_I85X(dev))
e70236a8 14823 dev_priv->display.get_display_clock_speed =
1b1d2716 14824 i85x_get_display_clock_speed;
623e01e5
VS
14825 else { /* 830 */
14826 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14827 dev_priv->display.get_display_clock_speed =
14828 i830_get_display_clock_speed;
623e01e5 14829 }
e70236a8 14830
7c10a2b5 14831 if (IS_GEN5(dev)) {
3bb11b53 14832 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14833 } else if (IS_GEN6(dev)) {
14834 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14835 } else if (IS_IVYBRIDGE(dev)) {
14836 /* FIXME: detect B0+ stepping and use auto training */
14837 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14838 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14839 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14840 if (IS_BROADWELL(dev)) {
14841 dev_priv->display.modeset_commit_cdclk =
14842 broadwell_modeset_commit_cdclk;
14843 dev_priv->display.modeset_calc_cdclk =
14844 broadwell_modeset_calc_cdclk;
14845 }
30a970c6 14846 } else if (IS_VALLEYVIEW(dev)) {
27c329ed
ML
14847 dev_priv->display.modeset_commit_cdclk =
14848 valleyview_modeset_commit_cdclk;
14849 dev_priv->display.modeset_calc_cdclk =
14850 valleyview_modeset_calc_cdclk;
f8437dd1 14851 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14852 dev_priv->display.modeset_commit_cdclk =
14853 broxton_modeset_commit_cdclk;
14854 dev_priv->display.modeset_calc_cdclk =
14855 broxton_modeset_calc_cdclk;
e70236a8 14856 }
8c9f3aaf 14857
8c9f3aaf
JB
14858 switch (INTEL_INFO(dev)->gen) {
14859 case 2:
14860 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14861 break;
14862
14863 case 3:
14864 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14865 break;
14866
14867 case 4:
14868 case 5:
14869 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14870 break;
14871
14872 case 6:
14873 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14874 break;
7c9017e5 14875 case 7:
4e0bbc31 14876 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14877 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14878 break;
830c81db 14879 case 9:
ba343e02
TU
14880 /* Drop through - unsupported since execlist only. */
14881 default:
14882 /* Default just returns -ENODEV to indicate unsupported */
14883 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14884 }
7bd688cd
JN
14885
14886 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14887
14888 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14889}
14890
b690e96c
JB
14891/*
14892 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14893 * resume, or other times. This quirk makes sure that's the case for
14894 * affected systems.
14895 */
0206e353 14896static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14897{
14898 struct drm_i915_private *dev_priv = dev->dev_private;
14899
14900 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14901 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14902}
14903
b6b5d049
VS
14904static void quirk_pipeb_force(struct drm_device *dev)
14905{
14906 struct drm_i915_private *dev_priv = dev->dev_private;
14907
14908 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14909 DRM_INFO("applying pipe b force quirk\n");
14910}
14911
435793df
KP
14912/*
14913 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14914 */
14915static void quirk_ssc_force_disable(struct drm_device *dev)
14916{
14917 struct drm_i915_private *dev_priv = dev->dev_private;
14918 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14919 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14920}
14921
4dca20ef 14922/*
5a15ab5b
CE
14923 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14924 * brightness value
4dca20ef
CE
14925 */
14926static void quirk_invert_brightness(struct drm_device *dev)
14927{
14928 struct drm_i915_private *dev_priv = dev->dev_private;
14929 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14930 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14931}
14932
9c72cc6f
SD
14933/* Some VBT's incorrectly indicate no backlight is present */
14934static void quirk_backlight_present(struct drm_device *dev)
14935{
14936 struct drm_i915_private *dev_priv = dev->dev_private;
14937 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14938 DRM_INFO("applying backlight present quirk\n");
14939}
14940
b690e96c
JB
14941struct intel_quirk {
14942 int device;
14943 int subsystem_vendor;
14944 int subsystem_device;
14945 void (*hook)(struct drm_device *dev);
14946};
14947
5f85f176
EE
14948/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14949struct intel_dmi_quirk {
14950 void (*hook)(struct drm_device *dev);
14951 const struct dmi_system_id (*dmi_id_list)[];
14952};
14953
14954static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14955{
14956 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14957 return 1;
14958}
14959
14960static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14961 {
14962 .dmi_id_list = &(const struct dmi_system_id[]) {
14963 {
14964 .callback = intel_dmi_reverse_brightness,
14965 .ident = "NCR Corporation",
14966 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14967 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14968 },
14969 },
14970 { } /* terminating entry */
14971 },
14972 .hook = quirk_invert_brightness,
14973 },
14974};
14975
c43b5634 14976static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14977 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14978 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14979
b690e96c
JB
14980 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14981 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14982
5f080c0f
VS
14983 /* 830 needs to leave pipe A & dpll A up */
14984 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14985
b6b5d049
VS
14986 /* 830 needs to leave pipe B & dpll B up */
14987 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14988
435793df
KP
14989 /* Lenovo U160 cannot use SSC on LVDS */
14990 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14991
14992 /* Sony Vaio Y cannot use SSC on LVDS */
14993 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14994
be505f64
AH
14995 /* Acer Aspire 5734Z must invert backlight brightness */
14996 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14997
14998 /* Acer/eMachines G725 */
14999 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15000
15001 /* Acer/eMachines e725 */
15002 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15003
15004 /* Acer/Packard Bell NCL20 */
15005 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15006
15007 /* Acer Aspire 4736Z */
15008 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15009
15010 /* Acer Aspire 5336 */
15011 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15012
15013 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15014 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15015
dfb3d47b
SD
15016 /* Acer C720 Chromebook (Core i3 4005U) */
15017 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15018
b2a9601c 15019 /* Apple Macbook 2,1 (Core 2 T7400) */
15020 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15021
d4967d8c
SD
15022 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15023 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15024
15025 /* HP Chromebook 14 (Celeron 2955U) */
15026 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15027
15028 /* Dell Chromebook 11 */
15029 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15030};
15031
15032static void intel_init_quirks(struct drm_device *dev)
15033{
15034 struct pci_dev *d = dev->pdev;
15035 int i;
15036
15037 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15038 struct intel_quirk *q = &intel_quirks[i];
15039
15040 if (d->device == q->device &&
15041 (d->subsystem_vendor == q->subsystem_vendor ||
15042 q->subsystem_vendor == PCI_ANY_ID) &&
15043 (d->subsystem_device == q->subsystem_device ||
15044 q->subsystem_device == PCI_ANY_ID))
15045 q->hook(dev);
15046 }
5f85f176
EE
15047 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15048 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15049 intel_dmi_quirks[i].hook(dev);
15050 }
b690e96c
JB
15051}
15052
9cce37f4
JB
15053/* Disable the VGA plane that we never use */
15054static void i915_disable_vga(struct drm_device *dev)
15055{
15056 struct drm_i915_private *dev_priv = dev->dev_private;
15057 u8 sr1;
766aa1c4 15058 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15059
2b37c616 15060 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15061 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15062 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15063 sr1 = inb(VGA_SR_DATA);
15064 outb(sr1 | 1<<5, VGA_SR_DATA);
15065 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15066 udelay(300);
15067
01f5a626 15068 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15069 POSTING_READ(vga_reg);
15070}
15071
f817586c
DV
15072void intel_modeset_init_hw(struct drm_device *dev)
15073{
b6283055 15074 intel_update_cdclk(dev);
a8f78b58 15075 intel_prepare_ddi(dev);
f817586c 15076 intel_init_clock_gating(dev);
8090c6b9 15077 intel_enable_gt_powersave(dev);
f817586c
DV
15078}
15079
79e53945
JB
15080void intel_modeset_init(struct drm_device *dev)
15081{
652c393a 15082 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15083 int sprite, ret;
8cc87b75 15084 enum pipe pipe;
46f297fb 15085 struct intel_crtc *crtc;
79e53945
JB
15086
15087 drm_mode_config_init(dev);
15088
15089 dev->mode_config.min_width = 0;
15090 dev->mode_config.min_height = 0;
15091
019d96cb
DA
15092 dev->mode_config.preferred_depth = 24;
15093 dev->mode_config.prefer_shadow = 1;
15094
25bab385
TU
15095 dev->mode_config.allow_fb_modifiers = true;
15096
e6ecefaa 15097 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15098
b690e96c
JB
15099 intel_init_quirks(dev);
15100
1fa61106
ED
15101 intel_init_pm(dev);
15102
e3c74757
BW
15103 if (INTEL_INFO(dev)->num_pipes == 0)
15104 return;
15105
e70236a8 15106 intel_init_display(dev);
7c10a2b5 15107 intel_init_audio(dev);
e70236a8 15108
a6c45cf0
CW
15109 if (IS_GEN2(dev)) {
15110 dev->mode_config.max_width = 2048;
15111 dev->mode_config.max_height = 2048;
15112 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15113 dev->mode_config.max_width = 4096;
15114 dev->mode_config.max_height = 4096;
79e53945 15115 } else {
a6c45cf0
CW
15116 dev->mode_config.max_width = 8192;
15117 dev->mode_config.max_height = 8192;
79e53945 15118 }
068be561 15119
dc41c154
VS
15120 if (IS_845G(dev) || IS_I865G(dev)) {
15121 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15122 dev->mode_config.cursor_height = 1023;
15123 } else if (IS_GEN2(dev)) {
068be561
DL
15124 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15125 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15126 } else {
15127 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15128 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15129 }
15130
5d4545ae 15131 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15132
28c97730 15133 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15134 INTEL_INFO(dev)->num_pipes,
15135 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15136
055e393f 15137 for_each_pipe(dev_priv, pipe) {
8cc87b75 15138 intel_crtc_init(dev, pipe);
3bdcfc0c 15139 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15140 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15141 if (ret)
06da8da2 15142 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15143 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15144 }
79e53945
JB
15145 }
15146
f42bb70d
JB
15147 intel_init_dpio(dev);
15148
e72f9fbf 15149 intel_shared_dpll_init(dev);
ee7b9f93 15150
9cce37f4
JB
15151 /* Just disable it once at startup */
15152 i915_disable_vga(dev);
79e53945 15153 intel_setup_outputs(dev);
11be49eb
CW
15154
15155 /* Just in case the BIOS is doing something questionable. */
7733b49b 15156 intel_fbc_disable(dev_priv);
fa9fa083 15157
6e9f798d 15158 drm_modeset_lock_all(dev);
043e9bda 15159 intel_modeset_setup_hw_state(dev);
6e9f798d 15160 drm_modeset_unlock_all(dev);
46f297fb 15161
d3fcc808 15162 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15163 struct intel_initial_plane_config plane_config = {};
15164
46f297fb
JB
15165 if (!crtc->active)
15166 continue;
15167
46f297fb 15168 /*
46f297fb
JB
15169 * Note that reserving the BIOS fb up front prevents us
15170 * from stuffing other stolen allocations like the ring
15171 * on top. This prevents some ugliness at boot time, and
15172 * can even allow for smooth boot transitions if the BIOS
15173 * fb is large enough for the active pipe configuration.
15174 */
eeebeac5
ML
15175 dev_priv->display.get_initial_plane_config(crtc,
15176 &plane_config);
15177
15178 /*
15179 * If the fb is shared between multiple heads, we'll
15180 * just get the first one.
15181 */
15182 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15183 }
2c7111db
CW
15184}
15185
7fad798e
DV
15186static void intel_enable_pipe_a(struct drm_device *dev)
15187{
15188 struct intel_connector *connector;
15189 struct drm_connector *crt = NULL;
15190 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15191 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15192
15193 /* We can't just switch on the pipe A, we need to set things up with a
15194 * proper mode and output configuration. As a gross hack, enable pipe A
15195 * by enabling the load detect pipe once. */
3a3371ff 15196 for_each_intel_connector(dev, connector) {
7fad798e
DV
15197 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15198 crt = &connector->base;
15199 break;
15200 }
15201 }
15202
15203 if (!crt)
15204 return;
15205
208bf9fd 15206 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15207 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15208}
15209
fa555837
DV
15210static bool
15211intel_check_plane_mapping(struct intel_crtc *crtc)
15212{
7eb552ae
BW
15213 struct drm_device *dev = crtc->base.dev;
15214 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15215 u32 reg, val;
15216
7eb552ae 15217 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15218 return true;
15219
15220 reg = DSPCNTR(!crtc->plane);
15221 val = I915_READ(reg);
15222
15223 if ((val & DISPLAY_PLANE_ENABLE) &&
15224 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15225 return false;
15226
15227 return true;
15228}
15229
24929352
DV
15230static void intel_sanitize_crtc(struct intel_crtc *crtc)
15231{
15232 struct drm_device *dev = crtc->base.dev;
15233 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15234 struct intel_encoder *encoder;
fa555837 15235 u32 reg;
b17d48e2 15236 bool enable;
24929352 15237
24929352 15238 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15239 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15240 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15241
d3eaf884 15242 /* restore vblank interrupts to correct state */
9625604c 15243 drm_crtc_vblank_reset(&crtc->base);
d297e103 15244 if (crtc->active) {
3a03dfb0 15245 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
d297e103 15246 update_scanline_offset(crtc);
9625604c
DV
15247 drm_crtc_vblank_on(&crtc->base);
15248 }
d3eaf884 15249
24929352 15250 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15251 * disable the crtc (and hence change the state) if it is wrong. Note
15252 * that gen4+ has a fixed plane -> pipe mapping. */
15253 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15254 bool plane;
15255
24929352
DV
15256 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15257 crtc->base.base.id);
15258
15259 /* Pipe has the wrong plane attached and the plane is active.
15260 * Temporarily change the plane mapping and disable everything
15261 * ... */
15262 plane = crtc->plane;
b70709a6 15263 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15264 crtc->plane = !plane;
b17d48e2 15265 intel_crtc_disable_noatomic(&crtc->base);
24929352 15266 crtc->plane = plane;
24929352 15267 }
24929352 15268
7fad798e
DV
15269 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15270 crtc->pipe == PIPE_A && !crtc->active) {
15271 /* BIOS forgot to enable pipe A, this mostly happens after
15272 * resume. Force-enable the pipe to fix this, the update_dpms
15273 * call below we restore the pipe to the right state, but leave
15274 * the required bits on. */
15275 intel_enable_pipe_a(dev);
15276 }
15277
24929352
DV
15278 /* Adjust the state of the output pipe according to whether we
15279 * have active connectors/encoders. */
b17d48e2
ML
15280 enable = false;
15281 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15282 enable |= encoder->connectors_active;
24929352 15283
b17d48e2
ML
15284 if (!enable)
15285 intel_crtc_disable_noatomic(&crtc->base);
24929352 15286
53d9f4e9 15287 if (crtc->active != crtc->base.state->active) {
24929352
DV
15288
15289 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15290 * functions or because of calls to intel_crtc_disable_noatomic,
15291 * or because the pipe is force-enabled due to the
24929352
DV
15292 * pipe A quirk. */
15293 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15294 crtc->base.base.id,
83d65738 15295 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15296 crtc->active ? "enabled" : "disabled");
15297
4be40c98 15298 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
49d6fa21 15299 crtc->base.state->active = crtc->active;
24929352
DV
15300 crtc->base.enabled = crtc->active;
15301
15302 /* Because we only establish the connector -> encoder ->
15303 * crtc links if something is active, this means the
15304 * crtc is now deactivated. Break the links. connector
15305 * -> encoder links are only establish when things are
15306 * actually up, hence no need to break them. */
15307 WARN_ON(crtc->active);
15308
15309 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15310 WARN_ON(encoder->connectors_active);
15311 encoder->base.crtc = NULL;
15312 }
15313 }
c5ab3bc0 15314
a3ed6aad 15315 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15316 /*
15317 * We start out with underrun reporting disabled to avoid races.
15318 * For correct bookkeeping mark this on active crtcs.
15319 *
c5ab3bc0
DV
15320 * Also on gmch platforms we dont have any hardware bits to
15321 * disable the underrun reporting. Which means we need to start
15322 * out with underrun reporting disabled also on inactive pipes,
15323 * since otherwise we'll complain about the garbage we read when
15324 * e.g. coming up after runtime pm.
15325 *
4cc31489
DV
15326 * No protection against concurrent access is required - at
15327 * worst a fifo underrun happens which also sets this to false.
15328 */
15329 crtc->cpu_fifo_underrun_disabled = true;
15330 crtc->pch_fifo_underrun_disabled = true;
15331 }
24929352
DV
15332}
15333
15334static void intel_sanitize_encoder(struct intel_encoder *encoder)
15335{
15336 struct intel_connector *connector;
15337 struct drm_device *dev = encoder->base.dev;
15338
15339 /* We need to check both for a crtc link (meaning that the
15340 * encoder is active and trying to read from a pipe) and the
15341 * pipe itself being active. */
15342 bool has_active_crtc = encoder->base.crtc &&
15343 to_intel_crtc(encoder->base.crtc)->active;
15344
15345 if (encoder->connectors_active && !has_active_crtc) {
15346 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15347 encoder->base.base.id,
8e329a03 15348 encoder->base.name);
24929352
DV
15349
15350 /* Connector is active, but has no active pipe. This is
15351 * fallout from our resume register restoring. Disable
15352 * the encoder manually again. */
15353 if (encoder->base.crtc) {
15354 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15355 encoder->base.base.id,
8e329a03 15356 encoder->base.name);
24929352 15357 encoder->disable(encoder);
a62d1497
VS
15358 if (encoder->post_disable)
15359 encoder->post_disable(encoder);
24929352 15360 }
7f1950fb
EE
15361 encoder->base.crtc = NULL;
15362 encoder->connectors_active = false;
24929352
DV
15363
15364 /* Inconsistent output/port/pipe state happens presumably due to
15365 * a bug in one of the get_hw_state functions. Or someplace else
15366 * in our code, like the register restore mess on resume. Clamp
15367 * things to off as a safer default. */
3a3371ff 15368 for_each_intel_connector(dev, connector) {
24929352
DV
15369 if (connector->encoder != encoder)
15370 continue;
7f1950fb
EE
15371 connector->base.dpms = DRM_MODE_DPMS_OFF;
15372 connector->base.encoder = NULL;
24929352
DV
15373 }
15374 }
15375 /* Enabled encoders without active connectors will be fixed in
15376 * the crtc fixup. */
15377}
15378
04098753 15379void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15380{
15381 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15382 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15383
04098753
ID
15384 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15385 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15386 i915_disable_vga(dev);
15387 }
15388}
15389
15390void i915_redisable_vga(struct drm_device *dev)
15391{
15392 struct drm_i915_private *dev_priv = dev->dev_private;
15393
8dc8a27c
PZ
15394 /* This function can be called both from intel_modeset_setup_hw_state or
15395 * at a very early point in our resume sequence, where the power well
15396 * structures are not yet restored. Since this function is at a very
15397 * paranoid "someone might have enabled VGA while we were not looking"
15398 * level, just check if the power well is enabled instead of trying to
15399 * follow the "don't touch the power well if we don't need it" policy
15400 * the rest of the driver uses. */
f458ebbc 15401 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15402 return;
15403
04098753 15404 i915_redisable_vga_power_on(dev);
0fde901f
KM
15405}
15406
98ec7739
VS
15407static bool primary_get_hw_state(struct intel_crtc *crtc)
15408{
15409 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15410
d032ffa0
ML
15411 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15412}
15413
15414static void readout_plane_state(struct intel_crtc *crtc,
15415 struct intel_crtc_state *crtc_state)
15416{
15417 struct intel_plane *p;
4cf0ebbd 15418 struct intel_plane_state *plane_state;
d032ffa0
ML
15419 bool active = crtc_state->base.active;
15420
d032ffa0 15421 for_each_intel_plane(crtc->base.dev, p) {
d032ffa0
ML
15422 if (crtc->pipe != p->pipe)
15423 continue;
15424
4cf0ebbd 15425 plane_state = to_intel_plane_state(p->base.state);
e435d6e5 15426
4cf0ebbd
ML
15427 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15428 plane_state->visible = primary_get_hw_state(crtc);
15429 else {
15430 if (active)
15431 p->disable_plane(&p->base, &crtc->base);
d032ffa0 15432
4cf0ebbd 15433 plane_state->visible = false;
d032ffa0
ML
15434 }
15435 }
98ec7739
VS
15436}
15437
30e984df 15438static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15439{
15440 struct drm_i915_private *dev_priv = dev->dev_private;
15441 enum pipe pipe;
24929352
DV
15442 struct intel_crtc *crtc;
15443 struct intel_encoder *encoder;
15444 struct intel_connector *connector;
5358901f 15445 int i;
24929352 15446
d3fcc808 15447 for_each_intel_crtc(dev, crtc) {
b06f8b0d 15448 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
6e3c9717 15449 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15450 crtc->config->base.crtc = &crtc->base;
3b117c8f 15451
6e3c9717 15452 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15453
0e8ffe1b 15454 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15455 crtc->config);
24929352 15456
49d6fa21 15457 crtc->base.state->active = crtc->active;
24929352 15458 crtc->base.enabled = crtc->active;
b70709a6 15459
5c1e3426
ML
15460 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15461 if (crtc->base.state->active) {
15462 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15463 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15464 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15465
15466 /*
15467 * The initial mode needs to be set in order to keep
15468 * the atomic core happy. It wants a valid mode if the
15469 * crtc's enabled, so we do the above call.
15470 *
15471 * At this point some state updated by the connectors
15472 * in their ->detect() callback has not run yet, so
15473 * no recalculation can be done yet.
15474 *
15475 * Even if we could do a recalculation and modeset
15476 * right now it would cause a double modeset if
15477 * fbdev or userspace chooses a different initial mode.
15478 *
15479 * So to prevent the double modeset, fail the memcmp
15480 * test in drm_atomic_set_mode_for_crtc to get a new
15481 * mode blob, and compare if the mode blob changed
15482 * when the PIPE_CONFIG_QUIRK_INHERITED_MODE quirk is
15483 * set.
15484 *
15485 * If that happens, someone indicated they wanted a
15486 * mode change, which means it's safe to do a full
15487 * recalculation.
15488 */
15489 crtc->base.state->mode.private_flags = ~0;
15490 }
15491
15492 crtc->base.hwmode = crtc->config->base.adjusted_mode;
d032ffa0 15493 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
24929352
DV
15494
15495 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15496 crtc->base.base.id,
15497 crtc->active ? "enabled" : "disabled");
15498 }
15499
5358901f
DV
15500 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15501 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15502
3e369b76
ACO
15503 pll->on = pll->get_hw_state(dev_priv, pll,
15504 &pll->config.hw_state);
5358901f 15505 pll->active = 0;
3e369b76 15506 pll->config.crtc_mask = 0;
d3fcc808 15507 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15508 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15509 pll->active++;
3e369b76 15510 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15511 }
5358901f 15512 }
5358901f 15513
1e6f2ddc 15514 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15515 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15516
3e369b76 15517 if (pll->config.crtc_mask)
bd2bb1b9 15518 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15519 }
15520
b2784e15 15521 for_each_intel_encoder(dev, encoder) {
24929352
DV
15522 pipe = 0;
15523
15524 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15525 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15526 encoder->base.crtc = &crtc->base;
6e3c9717 15527 encoder->get_config(encoder, crtc->config);
24929352
DV
15528 } else {
15529 encoder->base.crtc = NULL;
15530 }
15531
15532 encoder->connectors_active = false;
6f2bcceb 15533 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15534 encoder->base.base.id,
8e329a03 15535 encoder->base.name,
24929352 15536 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15537 pipe_name(pipe));
24929352
DV
15538 }
15539
3a3371ff 15540 for_each_intel_connector(dev, connector) {
24929352
DV
15541 if (connector->get_hw_state(connector)) {
15542 connector->base.dpms = DRM_MODE_DPMS_ON;
15543 connector->encoder->connectors_active = true;
15544 connector->base.encoder = &connector->encoder->base;
15545 } else {
15546 connector->base.dpms = DRM_MODE_DPMS_OFF;
15547 connector->base.encoder = NULL;
15548 }
15549 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15550 connector->base.base.id,
c23cc417 15551 connector->base.name,
24929352
DV
15552 connector->base.encoder ? "enabled" : "disabled");
15553 }
30e984df
DV
15554}
15555
043e9bda
ML
15556/* Scan out the current hw modeset state,
15557 * and sanitizes it to the current state
15558 */
15559static void
15560intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15561{
15562 struct drm_i915_private *dev_priv = dev->dev_private;
15563 enum pipe pipe;
30e984df
DV
15564 struct intel_crtc *crtc;
15565 struct intel_encoder *encoder;
35c95375 15566 int i;
30e984df
DV
15567
15568 intel_modeset_readout_hw_state(dev);
24929352
DV
15569
15570 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15571 for_each_intel_encoder(dev, encoder) {
24929352
DV
15572 intel_sanitize_encoder(encoder);
15573 }
15574
055e393f 15575 for_each_pipe(dev_priv, pipe) {
24929352
DV
15576 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15577 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15578 intel_dump_pipe_config(crtc, crtc->config,
15579 "[setup_hw_state]");
24929352 15580 }
9a935856 15581
d29b2f9d
ACO
15582 intel_modeset_update_connector_atomic_state(dev);
15583
35c95375
DV
15584 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15585 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15586
15587 if (!pll->on || pll->active)
15588 continue;
15589
15590 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15591
15592 pll->disable(dev_priv, pll);
15593 pll->on = false;
15594 }
15595
26e1fe4f 15596 if (IS_VALLEYVIEW(dev))
6eb1a681
VS
15597 vlv_wm_get_hw_state(dev);
15598 else if (IS_GEN9(dev))
3078999f
PB
15599 skl_wm_get_hw_state(dev);
15600 else if (HAS_PCH_SPLIT(dev))
243e6a44 15601 ilk_wm_get_hw_state(dev);
292b990e
ML
15602
15603 for_each_intel_crtc(dev, crtc) {
15604 unsigned long put_domains;
15605
15606 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15607 if (WARN_ON(put_domains))
15608 modeset_put_power_domains(dev_priv, put_domains);
15609 }
15610 intel_display_set_init_power(dev_priv, false);
043e9bda 15611}
7d0bc1ea 15612
043e9bda
ML
15613void intel_display_resume(struct drm_device *dev)
15614{
15615 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15616 struct intel_connector *conn;
15617 struct intel_plane *plane;
15618 struct drm_crtc *crtc;
15619 int ret;
f30da187 15620
043e9bda
ML
15621 if (!state)
15622 return;
15623
15624 state->acquire_ctx = dev->mode_config.acquire_ctx;
15625
15626 /* preserve complete old state, including dpll */
15627 intel_atomic_get_shared_dpll_state(state);
15628
15629 for_each_crtc(dev, crtc) {
15630 struct drm_crtc_state *crtc_state =
15631 drm_atomic_get_crtc_state(state, crtc);
15632
15633 ret = PTR_ERR_OR_ZERO(crtc_state);
15634 if (ret)
15635 goto err;
15636
15637 /* force a restore */
15638 crtc_state->mode_changed = true;
45e2b5f6 15639 }
8af6cf88 15640
043e9bda
ML
15641 for_each_intel_plane(dev, plane) {
15642 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15643 if (ret)
15644 goto err;
15645 }
15646
15647 for_each_intel_connector(dev, conn) {
15648 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15649 if (ret)
15650 goto err;
15651 }
15652
15653 intel_modeset_setup_hw_state(dev);
15654
15655 i915_redisable_vga(dev);
15656 ret = intel_set_mode(state);
15657 if (!ret)
15658 return;
15659
15660err:
15661 DRM_ERROR("Restoring old state failed with %i\n", ret);
15662 drm_atomic_state_free(state);
2c7111db
CW
15663}
15664
15665void intel_modeset_gem_init(struct drm_device *dev)
15666{
92122789 15667 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15668 struct drm_crtc *c;
2ff8fde1 15669 struct drm_i915_gem_object *obj;
e0d6149b 15670 int ret;
484b41dd 15671
ae48434c
ID
15672 mutex_lock(&dev->struct_mutex);
15673 intel_init_gt_powersave(dev);
15674 mutex_unlock(&dev->struct_mutex);
15675
92122789
JB
15676 /*
15677 * There may be no VBT; and if the BIOS enabled SSC we can
15678 * just keep using it to avoid unnecessary flicker. Whereas if the
15679 * BIOS isn't using it, don't assume it will work even if the VBT
15680 * indicates as much.
15681 */
15682 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15683 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15684 DREF_SSC1_ENABLE);
15685
1833b134 15686 intel_modeset_init_hw(dev);
02e792fb
DV
15687
15688 intel_setup_overlay(dev);
484b41dd
JB
15689
15690 /*
15691 * Make sure any fbs we allocated at startup are properly
15692 * pinned & fenced. When we do the allocation it's too early
15693 * for this.
15694 */
70e1e0ec 15695 for_each_crtc(dev, c) {
2ff8fde1
MR
15696 obj = intel_fb_obj(c->primary->fb);
15697 if (obj == NULL)
484b41dd
JB
15698 continue;
15699
e0d6149b
TU
15700 mutex_lock(&dev->struct_mutex);
15701 ret = intel_pin_and_fence_fb_obj(c->primary,
15702 c->primary->fb,
15703 c->primary->state,
91af127f 15704 NULL, NULL);
e0d6149b
TU
15705 mutex_unlock(&dev->struct_mutex);
15706 if (ret) {
484b41dd
JB
15707 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15708 to_intel_crtc(c)->pipe);
66e514c1
DA
15709 drm_framebuffer_unreference(c->primary->fb);
15710 c->primary->fb = NULL;
36750f28 15711 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15712 update_state_fb(c->primary);
36750f28 15713 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15714 }
15715 }
0962c3c9
VS
15716
15717 intel_backlight_register(dev);
79e53945
JB
15718}
15719
4932e2c3
ID
15720void intel_connector_unregister(struct intel_connector *intel_connector)
15721{
15722 struct drm_connector *connector = &intel_connector->base;
15723
15724 intel_panel_destroy_backlight(connector);
34ea3d38 15725 drm_connector_unregister(connector);
4932e2c3
ID
15726}
15727
79e53945
JB
15728void intel_modeset_cleanup(struct drm_device *dev)
15729{
652c393a 15730 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15731 struct drm_connector *connector;
652c393a 15732
2eb5252e
ID
15733 intel_disable_gt_powersave(dev);
15734
0962c3c9
VS
15735 intel_backlight_unregister(dev);
15736
fd0c0642
DV
15737 /*
15738 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15739 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15740 * experience fancy races otherwise.
15741 */
2aeb7d3a 15742 intel_irq_uninstall(dev_priv);
eb21b92b 15743
fd0c0642
DV
15744 /*
15745 * Due to the hpd irq storm handling the hotplug work can re-arm the
15746 * poll handlers. Hence disable polling after hpd handling is shut down.
15747 */
f87ea761 15748 drm_kms_helper_poll_fini(dev);
fd0c0642 15749
723bfd70
JB
15750 intel_unregister_dsm_handler();
15751
7733b49b 15752 intel_fbc_disable(dev_priv);
69341a5e 15753
1630fe75
CW
15754 /* flush any delayed tasks or pending work */
15755 flush_scheduled_work();
15756
db31af1d
JN
15757 /* destroy the backlight and sysfs files before encoders/connectors */
15758 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15759 struct intel_connector *intel_connector;
15760
15761 intel_connector = to_intel_connector(connector);
15762 intel_connector->unregister(intel_connector);
db31af1d 15763 }
d9255d57 15764
79e53945 15765 drm_mode_config_cleanup(dev);
4d7bb011
DV
15766
15767 intel_cleanup_overlay(dev);
ae48434c
ID
15768
15769 mutex_lock(&dev->struct_mutex);
15770 intel_cleanup_gt_powersave(dev);
15771 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15772}
15773
f1c79df3
ZW
15774/*
15775 * Return which encoder is currently attached for connector.
15776 */
df0e9248 15777struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15778{
df0e9248
CW
15779 return &intel_attached_encoder(connector)->base;
15780}
f1c79df3 15781
df0e9248
CW
15782void intel_connector_attach_encoder(struct intel_connector *connector,
15783 struct intel_encoder *encoder)
15784{
15785 connector->encoder = encoder;
15786 drm_mode_connector_attach_encoder(&connector->base,
15787 &encoder->base);
79e53945 15788}
28d52043
DA
15789
15790/*
15791 * set vga decode state - true == enable VGA decode
15792 */
15793int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15794{
15795 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15796 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15797 u16 gmch_ctrl;
15798
75fa041d
CW
15799 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15800 DRM_ERROR("failed to read control word\n");
15801 return -EIO;
15802 }
15803
c0cc8a55
CW
15804 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15805 return 0;
15806
28d52043
DA
15807 if (state)
15808 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15809 else
15810 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15811
15812 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15813 DRM_ERROR("failed to write control word\n");
15814 return -EIO;
15815 }
15816
28d52043
DA
15817 return 0;
15818}
c4a1d9e4 15819
c4a1d9e4 15820struct intel_display_error_state {
ff57f1b0
PZ
15821
15822 u32 power_well_driver;
15823
63b66e5b
CW
15824 int num_transcoders;
15825
c4a1d9e4
CW
15826 struct intel_cursor_error_state {
15827 u32 control;
15828 u32 position;
15829 u32 base;
15830 u32 size;
52331309 15831 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15832
15833 struct intel_pipe_error_state {
ddf9c536 15834 bool power_domain_on;
c4a1d9e4 15835 u32 source;
f301b1e1 15836 u32 stat;
52331309 15837 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15838
15839 struct intel_plane_error_state {
15840 u32 control;
15841 u32 stride;
15842 u32 size;
15843 u32 pos;
15844 u32 addr;
15845 u32 surface;
15846 u32 tile_offset;
52331309 15847 } plane[I915_MAX_PIPES];
63b66e5b
CW
15848
15849 struct intel_transcoder_error_state {
ddf9c536 15850 bool power_domain_on;
63b66e5b
CW
15851 enum transcoder cpu_transcoder;
15852
15853 u32 conf;
15854
15855 u32 htotal;
15856 u32 hblank;
15857 u32 hsync;
15858 u32 vtotal;
15859 u32 vblank;
15860 u32 vsync;
15861 } transcoder[4];
c4a1d9e4
CW
15862};
15863
15864struct intel_display_error_state *
15865intel_display_capture_error_state(struct drm_device *dev)
15866{
fbee40df 15867 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15868 struct intel_display_error_state *error;
63b66e5b
CW
15869 int transcoders[] = {
15870 TRANSCODER_A,
15871 TRANSCODER_B,
15872 TRANSCODER_C,
15873 TRANSCODER_EDP,
15874 };
c4a1d9e4
CW
15875 int i;
15876
63b66e5b
CW
15877 if (INTEL_INFO(dev)->num_pipes == 0)
15878 return NULL;
15879
9d1cb914 15880 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15881 if (error == NULL)
15882 return NULL;
15883
190be112 15884 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15885 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15886
055e393f 15887 for_each_pipe(dev_priv, i) {
ddf9c536 15888 error->pipe[i].power_domain_on =
f458ebbc
DV
15889 __intel_display_power_is_enabled(dev_priv,
15890 POWER_DOMAIN_PIPE(i));
ddf9c536 15891 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15892 continue;
15893
5efb3e28
VS
15894 error->cursor[i].control = I915_READ(CURCNTR(i));
15895 error->cursor[i].position = I915_READ(CURPOS(i));
15896 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15897
15898 error->plane[i].control = I915_READ(DSPCNTR(i));
15899 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15900 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15901 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15902 error->plane[i].pos = I915_READ(DSPPOS(i));
15903 }
ca291363
PZ
15904 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15905 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15906 if (INTEL_INFO(dev)->gen >= 4) {
15907 error->plane[i].surface = I915_READ(DSPSURF(i));
15908 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15909 }
15910
c4a1d9e4 15911 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15912
3abfce77 15913 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15914 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15915 }
15916
15917 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15918 if (HAS_DDI(dev_priv->dev))
15919 error->num_transcoders++; /* Account for eDP. */
15920
15921 for (i = 0; i < error->num_transcoders; i++) {
15922 enum transcoder cpu_transcoder = transcoders[i];
15923
ddf9c536 15924 error->transcoder[i].power_domain_on =
f458ebbc 15925 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15926 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15927 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15928 continue;
15929
63b66e5b
CW
15930 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15931
15932 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15933 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15934 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15935 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15936 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15937 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15938 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15939 }
15940
15941 return error;
15942}
15943
edc3d884
MK
15944#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15945
c4a1d9e4 15946void
edc3d884 15947intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15948 struct drm_device *dev,
15949 struct intel_display_error_state *error)
15950{
055e393f 15951 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15952 int i;
15953
63b66e5b
CW
15954 if (!error)
15955 return;
15956
edc3d884 15957 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15958 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15959 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15960 error->power_well_driver);
055e393f 15961 for_each_pipe(dev_priv, i) {
edc3d884 15962 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15963 err_printf(m, " Power: %s\n",
15964 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15965 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15966 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15967
15968 err_printf(m, "Plane [%d]:\n", i);
15969 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15970 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15971 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15972 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15973 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15974 }
4b71a570 15975 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15976 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15977 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15978 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15979 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15980 }
15981
edc3d884
MK
15982 err_printf(m, "Cursor [%d]:\n", i);
15983 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15984 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15985 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15986 }
63b66e5b
CW
15987
15988 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15989 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15990 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15991 err_printf(m, " Power: %s\n",
15992 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15993 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15994 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15995 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15996 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15997 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15998 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15999 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16000 }
c4a1d9e4 16001}
e2fcdaa9
VS
16002
16003void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
16004{
16005 struct intel_crtc *crtc;
16006
16007 for_each_intel_crtc(dev, crtc) {
16008 struct intel_unpin_work *work;
e2fcdaa9 16009
5e2d7afc 16010 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
16011
16012 work = crtc->unpin_work;
16013
16014 if (work && work->event &&
16015 work->event->base.file_priv == file) {
16016 kfree(work->event);
16017 work->event = NULL;
16018 }
16019
5e2d7afc 16020 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
16021 }
16022}