drm/i915: Set hwmode during readout.
[linux-block.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);
ce22dba9
ML
112static void intel_crtc_enable_planes(struct drm_crtc *crtc);
113static void intel_crtc_disable_planes(struct drm_crtc *crtc);
e7457a9a 114
0e32b39c
DA
115static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
116{
117 if (!connector->mst_port)
118 return connector->encoder;
119 else
120 return &connector->mst_port->mst_encoders[pipe]->base;
121}
122
79e53945 123typedef struct {
0206e353 124 int min, max;
79e53945
JB
125} intel_range_t;
126
127typedef struct {
0206e353
AJ
128 int dot_limit;
129 int p2_slow, p2_fast;
79e53945
JB
130} intel_p2_t;
131
d4906093
ML
132typedef struct intel_limit intel_limit_t;
133struct intel_limit {
0206e353
AJ
134 intel_range_t dot, vco, n, m, m1, m2, p, p1;
135 intel_p2_t p2;
d4906093 136};
79e53945 137
d2acd215
DV
138int
139intel_pch_rawclk(struct drm_device *dev)
140{
141 struct drm_i915_private *dev_priv = dev->dev_private;
142
143 WARN_ON(!HAS_PCH_SPLIT(dev));
144
145 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
146}
147
021357ac
CW
148static inline u32 /* units of 100MHz */
149intel_fdi_link_freq(struct drm_device *dev)
150{
8b99e68c
CW
151 if (IS_GEN5(dev)) {
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
154 } else
155 return 27;
021357ac
CW
156}
157
5d536e28 158static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 159 .dot = { .min = 25000, .max = 350000 },
9c333719 160 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 161 .n = { .min = 2, .max = 16 },
0206e353
AJ
162 .m = { .min = 96, .max = 140 },
163 .m1 = { .min = 18, .max = 26 },
164 .m2 = { .min = 6, .max = 16 },
165 .p = { .min = 4, .max = 128 },
166 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
167 .p2 = { .dot_limit = 165000,
168 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
169};
170
5d536e28
DV
171static const intel_limit_t intel_limits_i8xx_dvo = {
172 .dot = { .min = 25000, .max = 350000 },
9c333719 173 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 174 .n = { .min = 2, .max = 16 },
5d536e28
DV
175 .m = { .min = 96, .max = 140 },
176 .m1 = { .min = 18, .max = 26 },
177 .m2 = { .min = 6, .max = 16 },
178 .p = { .min = 4, .max = 128 },
179 .p1 = { .min = 2, .max = 33 },
180 .p2 = { .dot_limit = 165000,
181 .p2_slow = 4, .p2_fast = 4 },
182};
183
e4b36699 184static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 185 .dot = { .min = 25000, .max = 350000 },
9c333719 186 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 187 .n = { .min = 2, .max = 16 },
0206e353
AJ
188 .m = { .min = 96, .max = 140 },
189 .m1 = { .min = 18, .max = 26 },
190 .m2 = { .min = 6, .max = 16 },
191 .p = { .min = 4, .max = 128 },
192 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
193 .p2 = { .dot_limit = 165000,
194 .p2_slow = 14, .p2_fast = 7 },
e4b36699 195};
273e27ca 196
e4b36699 197static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
198 .dot = { .min = 20000, .max = 400000 },
199 .vco = { .min = 1400000, .max = 2800000 },
200 .n = { .min = 1, .max = 6 },
201 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
202 .m1 = { .min = 8, .max = 18 },
203 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
206 .p2 = { .dot_limit = 200000,
207 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
208};
209
210static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
211 .dot = { .min = 20000, .max = 400000 },
212 .vco = { .min = 1400000, .max = 2800000 },
213 .n = { .min = 1, .max = 6 },
214 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
215 .m1 = { .min = 8, .max = 18 },
216 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
217 .p = { .min = 7, .max = 98 },
218 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
219 .p2 = { .dot_limit = 112000,
220 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
221};
222
273e27ca 223
e4b36699 224static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
225 .dot = { .min = 25000, .max = 270000 },
226 .vco = { .min = 1750000, .max = 3500000},
227 .n = { .min = 1, .max = 4 },
228 .m = { .min = 104, .max = 138 },
229 .m1 = { .min = 17, .max = 23 },
230 .m2 = { .min = 5, .max = 11 },
231 .p = { .min = 10, .max = 30 },
232 .p1 = { .min = 1, .max = 3},
233 .p2 = { .dot_limit = 270000,
234 .p2_slow = 10,
235 .p2_fast = 10
044c7c41 236 },
e4b36699
KP
237};
238
239static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
240 .dot = { .min = 22000, .max = 400000 },
241 .vco = { .min = 1750000, .max = 3500000},
242 .n = { .min = 1, .max = 4 },
243 .m = { .min = 104, .max = 138 },
244 .m1 = { .min = 16, .max = 23 },
245 .m2 = { .min = 5, .max = 11 },
246 .p = { .min = 5, .max = 80 },
247 .p1 = { .min = 1, .max = 8},
248 .p2 = { .dot_limit = 165000,
249 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
250};
251
252static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
253 .dot = { .min = 20000, .max = 115000 },
254 .vco = { .min = 1750000, .max = 3500000 },
255 .n = { .min = 1, .max = 3 },
256 .m = { .min = 104, .max = 138 },
257 .m1 = { .min = 17, .max = 23 },
258 .m2 = { .min = 5, .max = 11 },
259 .p = { .min = 28, .max = 112 },
260 .p1 = { .min = 2, .max = 8 },
261 .p2 = { .dot_limit = 0,
262 .p2_slow = 14, .p2_fast = 14
044c7c41 263 },
e4b36699
KP
264};
265
266static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
267 .dot = { .min = 80000, .max = 224000 },
268 .vco = { .min = 1750000, .max = 3500000 },
269 .n = { .min = 1, .max = 3 },
270 .m = { .min = 104, .max = 138 },
271 .m1 = { .min = 17, .max = 23 },
272 .m2 = { .min = 5, .max = 11 },
273 .p = { .min = 14, .max = 42 },
274 .p1 = { .min = 2, .max = 6 },
275 .p2 = { .dot_limit = 0,
276 .p2_slow = 7, .p2_fast = 7
044c7c41 277 },
e4b36699
KP
278};
279
f2b115e6 280static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
281 .dot = { .min = 20000, .max = 400000},
282 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 283 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
284 .n = { .min = 3, .max = 6 },
285 .m = { .min = 2, .max = 256 },
273e27ca 286 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
287 .m1 = { .min = 0, .max = 0 },
288 .m2 = { .min = 0, .max = 254 },
289 .p = { .min = 5, .max = 80 },
290 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
291 .p2 = { .dot_limit = 200000,
292 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
293};
294
f2b115e6 295static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
296 .dot = { .min = 20000, .max = 400000 },
297 .vco = { .min = 1700000, .max = 3500000 },
298 .n = { .min = 3, .max = 6 },
299 .m = { .min = 2, .max = 256 },
300 .m1 = { .min = 0, .max = 0 },
301 .m2 = { .min = 0, .max = 254 },
302 .p = { .min = 7, .max = 112 },
303 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
304 .p2 = { .dot_limit = 112000,
305 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
306};
307
273e27ca
EA
308/* Ironlake / Sandybridge
309 *
310 * We calculate clock using (register_value + 2) for N/M1/M2, so here
311 * the range value for them is (actual_value - 2).
312 */
b91ad0ec 313static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
314 .dot = { .min = 25000, .max = 350000 },
315 .vco = { .min = 1760000, .max = 3510000 },
316 .n = { .min = 1, .max = 5 },
317 .m = { .min = 79, .max = 127 },
318 .m1 = { .min = 12, .max = 22 },
319 .m2 = { .min = 5, .max = 9 },
320 .p = { .min = 5, .max = 80 },
321 .p1 = { .min = 1, .max = 8 },
322 .p2 = { .dot_limit = 225000,
323 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
324};
325
b91ad0ec 326static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
327 .dot = { .min = 25000, .max = 350000 },
328 .vco = { .min = 1760000, .max = 3510000 },
329 .n = { .min = 1, .max = 3 },
330 .m = { .min = 79, .max = 118 },
331 .m1 = { .min = 12, .max = 22 },
332 .m2 = { .min = 5, .max = 9 },
333 .p = { .min = 28, .max = 112 },
334 .p1 = { .min = 2, .max = 8 },
335 .p2 = { .dot_limit = 225000,
336 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
337};
338
339static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
340 .dot = { .min = 25000, .max = 350000 },
341 .vco = { .min = 1760000, .max = 3510000 },
342 .n = { .min = 1, .max = 3 },
343 .m = { .min = 79, .max = 127 },
344 .m1 = { .min = 12, .max = 22 },
345 .m2 = { .min = 5, .max = 9 },
346 .p = { .min = 14, .max = 56 },
347 .p1 = { .min = 2, .max = 8 },
348 .p2 = { .dot_limit = 225000,
349 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
350};
351
273e27ca 352/* LVDS 100mhz refclk limits. */
b91ad0ec 353static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 2 },
357 .m = { .min = 79, .max = 126 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 28, .max = 112 },
0206e353 361 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
364};
365
366static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
367 .dot = { .min = 25000, .max = 350000 },
368 .vco = { .min = 1760000, .max = 3510000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 79, .max = 126 },
371 .m1 = { .min = 12, .max = 22 },
372 .m2 = { .min = 5, .max = 9 },
373 .p = { .min = 14, .max = 42 },
0206e353 374 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
375 .p2 = { .dot_limit = 225000,
376 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
377};
378
dc730512 379static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
380 /*
381 * These are the data rate limits (measured in fast clocks)
382 * since those are the strictest limits we have. The fast
383 * clock and actual rate limits are more relaxed, so checking
384 * them would make no difference.
385 */
386 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 387 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 388 .n = { .min = 1, .max = 7 },
a0c4da24
JB
389 .m1 = { .min = 2, .max = 3 },
390 .m2 = { .min = 11, .max = 156 },
b99ab663 391 .p1 = { .min = 2, .max = 3 },
5fdc9c49 392 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
393};
394
ef9348c8
CML
395static const intel_limit_t intel_limits_chv = {
396 /*
397 * These are the data rate limits (measured in fast clocks)
398 * since those are the strictest limits we have. The fast
399 * clock and actual rate limits are more relaxed, so checking
400 * them would make no difference.
401 */
402 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 403 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
404 .n = { .min = 1, .max = 1 },
405 .m1 = { .min = 2, .max = 2 },
406 .m2 = { .min = 24 << 22, .max = 175 << 22 },
407 .p1 = { .min = 2, .max = 4 },
408 .p2 = { .p2_slow = 1, .p2_fast = 14 },
409};
410
5ab7b0b7
ID
411static const intel_limit_t intel_limits_bxt = {
412 /* FIXME: find real dot limits */
413 .dot = { .min = 0, .max = INT_MAX },
414 .vco = { .min = 4800000, .max = 6480000 },
415 .n = { .min = 1, .max = 1 },
416 .m1 = { .min = 2, .max = 2 },
417 /* FIXME: find real m2 limits */
418 .m2 = { .min = 2 << 22, .max = 255 << 22 },
419 .p1 = { .min = 2, .max = 4 },
420 .p2 = { .p2_slow = 1, .p2_fast = 20 },
421};
422
6b4bf1c4
VS
423static void vlv_clock(int refclk, intel_clock_t *clock)
424{
425 clock->m = clock->m1 * clock->m2;
426 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
427 if (WARN_ON(clock->n == 0 || clock->p == 0))
428 return;
fb03ac01
VS
429 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
430 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
431}
432
cdba954e
ACO
433static bool
434needs_modeset(struct drm_crtc_state *state)
435{
436 return state->mode_changed || state->active_changed;
437}
438
e0638cdf
PZ
439/**
440 * Returns whether any output on the specified pipe is of the specified type
441 */
4093561b 442bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 443{
409ee761 444 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
445 struct intel_encoder *encoder;
446
409ee761 447 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
448 if (encoder->type == type)
449 return true;
450
451 return false;
452}
453
d0737e1d
ACO
454/**
455 * Returns whether any output on the specified pipe will have the specified
456 * type after a staged modeset is complete, i.e., the same as
457 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
458 * encoder->crtc.
459 */
a93e255f
ACO
460static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
461 int type)
d0737e1d 462{
a93e255f 463 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 464 struct drm_connector *connector;
a93e255f 465 struct drm_connector_state *connector_state;
d0737e1d 466 struct intel_encoder *encoder;
a93e255f
ACO
467 int i, num_connectors = 0;
468
da3ced29 469 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
470 if (connector_state->crtc != crtc_state->base.crtc)
471 continue;
472
473 num_connectors++;
d0737e1d 474
a93e255f
ACO
475 encoder = to_intel_encoder(connector_state->best_encoder);
476 if (encoder->type == type)
d0737e1d 477 return true;
a93e255f
ACO
478 }
479
480 WARN_ON(num_connectors == 0);
d0737e1d
ACO
481
482 return false;
483}
484
a93e255f
ACO
485static const intel_limit_t *
486intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 487{
a93e255f 488 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 489 const intel_limit_t *limit;
b91ad0ec 490
a93e255f 491 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 492 if (intel_is_dual_link_lvds(dev)) {
1b894b59 493 if (refclk == 100000)
b91ad0ec
ZW
494 limit = &intel_limits_ironlake_dual_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_dual_lvds;
497 } else {
1b894b59 498 if (refclk == 100000)
b91ad0ec
ZW
499 limit = &intel_limits_ironlake_single_lvds_100m;
500 else
501 limit = &intel_limits_ironlake_single_lvds;
502 }
c6bb3538 503 } else
b91ad0ec 504 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
505
506 return limit;
507}
508
a93e255f
ACO
509static const intel_limit_t *
510intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 511{
a93e255f 512 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
513 const intel_limit_t *limit;
514
a93e255f 515 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 516 if (intel_is_dual_link_lvds(dev))
e4b36699 517 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 518 else
e4b36699 519 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
520 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
521 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 522 limit = &intel_limits_g4x_hdmi;
a93e255f 523 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 524 limit = &intel_limits_g4x_sdvo;
044c7c41 525 } else /* The option is for other outputs */
e4b36699 526 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
527
528 return limit;
529}
530
a93e255f
ACO
531static const intel_limit_t *
532intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 533{
a93e255f 534 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
535 const intel_limit_t *limit;
536
5ab7b0b7
ID
537 if (IS_BROXTON(dev))
538 limit = &intel_limits_bxt;
539 else if (HAS_PCH_SPLIT(dev))
a93e255f 540 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 541 else if (IS_G4X(dev)) {
a93e255f 542 limit = intel_g4x_limit(crtc_state);
f2b115e6 543 } else if (IS_PINEVIEW(dev)) {
a93e255f 544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 545 limit = &intel_limits_pineview_lvds;
2177832f 546 else
f2b115e6 547 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
548 } else if (IS_CHERRYVIEW(dev)) {
549 limit = &intel_limits_chv;
a0c4da24 550 } else if (IS_VALLEYVIEW(dev)) {
dc730512 551 limit = &intel_limits_vlv;
a6c45cf0 552 } else if (!IS_GEN2(dev)) {
a93e255f 553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
554 limit = &intel_limits_i9xx_lvds;
555 else
556 limit = &intel_limits_i9xx_sdvo;
79e53945 557 } else {
a93e255f 558 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 559 limit = &intel_limits_i8xx_lvds;
a93e255f 560 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 561 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
562 else
563 limit = &intel_limits_i8xx_dac;
79e53945
JB
564 }
565 return limit;
566}
567
f2b115e6
AJ
568/* m1 is reserved as 0 in Pineview, n is a ring counter */
569static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 570{
2177832f
SL
571 clock->m = clock->m2 + 2;
572 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
573 if (WARN_ON(clock->n == 0 || clock->p == 0))
574 return;
fb03ac01
VS
575 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
576 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
577}
578
7429e9d4
DV
579static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
580{
581 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
582}
583
ac58c3f0 584static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 585{
7429e9d4 586 clock->m = i9xx_dpll_compute_m(clock);
79e53945 587 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
588 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
589 return;
fb03ac01
VS
590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
592}
593
ef9348c8
CML
594static void chv_clock(int refclk, intel_clock_t *clock)
595{
596 clock->m = clock->m1 * clock->m2;
597 clock->p = clock->p1 * clock->p2;
598 if (WARN_ON(clock->n == 0 || clock->p == 0))
599 return;
600 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
601 clock->n << 22);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
603}
604
7c04d1d9 605#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
606/**
607 * Returns whether the given set of divisors are valid for a given refclk with
608 * the given connectors.
609 */
610
1b894b59
CW
611static bool intel_PLL_is_valid(struct drm_device *dev,
612 const intel_limit_t *limit,
613 const intel_clock_t *clock)
79e53945 614{
f01b7962
VS
615 if (clock->n < limit->n.min || limit->n.max < clock->n)
616 INTELPllInvalid("n out of range\n");
79e53945 617 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 618 INTELPllInvalid("p1 out of range\n");
79e53945 619 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 620 INTELPllInvalid("m2 out of range\n");
79e53945 621 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 622 INTELPllInvalid("m1 out of range\n");
f01b7962 623
5ab7b0b7 624 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
625 if (clock->m1 <= clock->m2)
626 INTELPllInvalid("m1 <= m2\n");
627
5ab7b0b7 628 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
629 if (clock->p < limit->p.min || limit->p.max < clock->p)
630 INTELPllInvalid("p out of range\n");
631 if (clock->m < limit->m.min || limit->m.max < clock->m)
632 INTELPllInvalid("m out of range\n");
633 }
634
79e53945 635 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 636 INTELPllInvalid("vco out of range\n");
79e53945
JB
637 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
638 * connector, etc., rather than just a single range.
639 */
640 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 641 INTELPllInvalid("dot out of range\n");
79e53945
JB
642
643 return true;
644}
645
d4906093 646static bool
a93e255f
ACO
647i9xx_find_best_dpll(const intel_limit_t *limit,
648 struct intel_crtc_state *crtc_state,
cec2f356
SP
649 int target, int refclk, intel_clock_t *match_clock,
650 intel_clock_t *best_clock)
79e53945 651{
a93e255f 652 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 653 struct drm_device *dev = crtc->base.dev;
79e53945 654 intel_clock_t clock;
79e53945
JB
655 int err = target;
656
a93e255f 657 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 658 /*
a210b028
DV
659 * For LVDS just rely on its current settings for dual-channel.
660 * We haven't figured out how to reliably set up different
661 * single/dual channel state, if we even can.
79e53945 662 */
1974cad0 663 if (intel_is_dual_link_lvds(dev))
79e53945
JB
664 clock.p2 = limit->p2.p2_fast;
665 else
666 clock.p2 = limit->p2.p2_slow;
667 } else {
668 if (target < limit->p2.dot_limit)
669 clock.p2 = limit->p2.p2_slow;
670 else
671 clock.p2 = limit->p2.p2_fast;
672 }
673
0206e353 674 memset(best_clock, 0, sizeof(*best_clock));
79e53945 675
42158660
ZY
676 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
677 clock.m1++) {
678 for (clock.m2 = limit->m2.min;
679 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 680 if (clock.m2 >= clock.m1)
42158660
ZY
681 break;
682 for (clock.n = limit->n.min;
683 clock.n <= limit->n.max; clock.n++) {
684 for (clock.p1 = limit->p1.min;
685 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
686 int this_err;
687
ac58c3f0
DV
688 i9xx_clock(refclk, &clock);
689 if (!intel_PLL_is_valid(dev, limit,
690 &clock))
691 continue;
692 if (match_clock &&
693 clock.p != match_clock->p)
694 continue;
695
696 this_err = abs(clock.dot - target);
697 if (this_err < err) {
698 *best_clock = clock;
699 err = this_err;
700 }
701 }
702 }
703 }
704 }
705
706 return (err != target);
707}
708
709static bool
a93e255f
ACO
710pnv_find_best_dpll(const intel_limit_t *limit,
711 struct intel_crtc_state *crtc_state,
ee9300bb
DV
712 int target, int refclk, intel_clock_t *match_clock,
713 intel_clock_t *best_clock)
79e53945 714{
a93e255f 715 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 716 struct drm_device *dev = crtc->base.dev;
79e53945 717 intel_clock_t clock;
79e53945
JB
718 int err = target;
719
a93e255f 720 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 721 /*
a210b028
DV
722 * For LVDS just rely on its current settings for dual-channel.
723 * We haven't figured out how to reliably set up different
724 * single/dual channel state, if we even can.
79e53945 725 */
1974cad0 726 if (intel_is_dual_link_lvds(dev))
79e53945
JB
727 clock.p2 = limit->p2.p2_fast;
728 else
729 clock.p2 = limit->p2.p2_slow;
730 } else {
731 if (target < limit->p2.dot_limit)
732 clock.p2 = limit->p2.p2_slow;
733 else
734 clock.p2 = limit->p2.p2_fast;
735 }
736
0206e353 737 memset(best_clock, 0, sizeof(*best_clock));
79e53945 738
42158660
ZY
739 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
740 clock.m1++) {
741 for (clock.m2 = limit->m2.min;
742 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
743 for (clock.n = limit->n.min;
744 clock.n <= limit->n.max; clock.n++) {
745 for (clock.p1 = limit->p1.min;
746 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
747 int this_err;
748
ac58c3f0 749 pineview_clock(refclk, &clock);
1b894b59
CW
750 if (!intel_PLL_is_valid(dev, limit,
751 &clock))
79e53945 752 continue;
cec2f356
SP
753 if (match_clock &&
754 clock.p != match_clock->p)
755 continue;
79e53945
JB
756
757 this_err = abs(clock.dot - target);
758 if (this_err < err) {
759 *best_clock = clock;
760 err = this_err;
761 }
762 }
763 }
764 }
765 }
766
767 return (err != target);
768}
769
d4906093 770static bool
a93e255f
ACO
771g4x_find_best_dpll(const intel_limit_t *limit,
772 struct intel_crtc_state *crtc_state,
ee9300bb
DV
773 int target, int refclk, intel_clock_t *match_clock,
774 intel_clock_t *best_clock)
d4906093 775{
a93e255f 776 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 777 struct drm_device *dev = crtc->base.dev;
d4906093
ML
778 intel_clock_t clock;
779 int max_n;
780 bool found;
6ba770dc
AJ
781 /* approximately equals target * 0.00585 */
782 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
783 found = false;
784
a93e255f 785 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 786 if (intel_is_dual_link_lvds(dev))
d4906093
ML
787 clock.p2 = limit->p2.p2_fast;
788 else
789 clock.p2 = limit->p2.p2_slow;
790 } else {
791 if (target < limit->p2.dot_limit)
792 clock.p2 = limit->p2.p2_slow;
793 else
794 clock.p2 = limit->p2.p2_fast;
795 }
796
797 memset(best_clock, 0, sizeof(*best_clock));
798 max_n = limit->n.max;
f77f13e2 799 /* based on hardware requirement, prefer smaller n to precision */
d4906093 800 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 801 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
802 for (clock.m1 = limit->m1.max;
803 clock.m1 >= limit->m1.min; clock.m1--) {
804 for (clock.m2 = limit->m2.max;
805 clock.m2 >= limit->m2.min; clock.m2--) {
806 for (clock.p1 = limit->p1.max;
807 clock.p1 >= limit->p1.min; clock.p1--) {
808 int this_err;
809
ac58c3f0 810 i9xx_clock(refclk, &clock);
1b894b59
CW
811 if (!intel_PLL_is_valid(dev, limit,
812 &clock))
d4906093 813 continue;
1b894b59
CW
814
815 this_err = abs(clock.dot - target);
d4906093
ML
816 if (this_err < err_most) {
817 *best_clock = clock;
818 err_most = this_err;
819 max_n = clock.n;
820 found = true;
821 }
822 }
823 }
824 }
825 }
2c07245f
ZW
826 return found;
827}
828
d5dd62bd
ID
829/*
830 * Check if the calculated PLL configuration is more optimal compared to the
831 * best configuration and error found so far. Return the calculated error.
832 */
833static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
834 const intel_clock_t *calculated_clock,
835 const intel_clock_t *best_clock,
836 unsigned int best_error_ppm,
837 unsigned int *error_ppm)
838{
9ca3ba01
ID
839 /*
840 * For CHV ignore the error and consider only the P value.
841 * Prefer a bigger P value based on HW requirements.
842 */
843 if (IS_CHERRYVIEW(dev)) {
844 *error_ppm = 0;
845
846 return calculated_clock->p > best_clock->p;
847 }
848
24be4e46
ID
849 if (WARN_ON_ONCE(!target_freq))
850 return false;
851
d5dd62bd
ID
852 *error_ppm = div_u64(1000000ULL *
853 abs(target_freq - calculated_clock->dot),
854 target_freq);
855 /*
856 * Prefer a better P value over a better (smaller) error if the error
857 * is small. Ensure this preference for future configurations too by
858 * setting the error to 0.
859 */
860 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
861 *error_ppm = 0;
862
863 return true;
864 }
865
866 return *error_ppm + 10 < best_error_ppm;
867}
868
a0c4da24 869static bool
a93e255f
ACO
870vlv_find_best_dpll(const intel_limit_t *limit,
871 struct intel_crtc_state *crtc_state,
ee9300bb
DV
872 int target, int refclk, intel_clock_t *match_clock,
873 intel_clock_t *best_clock)
a0c4da24 874{
a93e255f 875 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 876 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 877 intel_clock_t clock;
69e4f900 878 unsigned int bestppm = 1000000;
27e639bf
VS
879 /* min update 19.2 MHz */
880 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 881 bool found = false;
a0c4da24 882
6b4bf1c4
VS
883 target *= 5; /* fast clock */
884
885 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
886
887 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 888 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 889 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 890 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 891 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 892 clock.p = clock.p1 * clock.p2;
a0c4da24 893 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 894 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 895 unsigned int ppm;
69e4f900 896
6b4bf1c4
VS
897 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
898 refclk * clock.m1);
899
900 vlv_clock(refclk, &clock);
43b0ac53 901
f01b7962
VS
902 if (!intel_PLL_is_valid(dev, limit,
903 &clock))
43b0ac53
VS
904 continue;
905
d5dd62bd
ID
906 if (!vlv_PLL_is_optimal(dev, target,
907 &clock,
908 best_clock,
909 bestppm, &ppm))
910 continue;
6b4bf1c4 911
d5dd62bd
ID
912 *best_clock = clock;
913 bestppm = ppm;
914 found = true;
a0c4da24
JB
915 }
916 }
917 }
918 }
a0c4da24 919
49e497ef 920 return found;
a0c4da24 921}
a4fc5ed6 922
ef9348c8 923static bool
a93e255f
ACO
924chv_find_best_dpll(const intel_limit_t *limit,
925 struct intel_crtc_state *crtc_state,
ef9348c8
CML
926 int target, int refclk, intel_clock_t *match_clock,
927 intel_clock_t *best_clock)
928{
a93e255f 929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 930 struct drm_device *dev = crtc->base.dev;
9ca3ba01 931 unsigned int best_error_ppm;
ef9348c8
CML
932 intel_clock_t clock;
933 uint64_t m2;
934 int found = false;
935
936 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 937 best_error_ppm = 1000000;
ef9348c8
CML
938
939 /*
940 * Based on hardware doc, the n always set to 1, and m1 always
941 * set to 2. If requires to support 200Mhz refclk, we need to
942 * revisit this because n may not 1 anymore.
943 */
944 clock.n = 1, clock.m1 = 2;
945 target *= 5; /* fast clock */
946
947 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
948 for (clock.p2 = limit->p2.p2_fast;
949 clock.p2 >= limit->p2.p2_slow;
950 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 951 unsigned int error_ppm;
ef9348c8
CML
952
953 clock.p = clock.p1 * clock.p2;
954
955 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
956 clock.n) << 22, refclk * clock.m1);
957
958 if (m2 > INT_MAX/clock.m1)
959 continue;
960
961 clock.m2 = m2;
962
963 chv_clock(refclk, &clock);
964
965 if (!intel_PLL_is_valid(dev, limit, &clock))
966 continue;
967
9ca3ba01
ID
968 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
969 best_error_ppm, &error_ppm))
970 continue;
971
972 *best_clock = clock;
973 best_error_ppm = error_ppm;
974 found = true;
ef9348c8
CML
975 }
976 }
977
978 return found;
979}
980
5ab7b0b7
ID
981bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
982 intel_clock_t *best_clock)
983{
984 int refclk = i9xx_get_refclk(crtc_state, 0);
985
986 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
987 target_clock, refclk, NULL, best_clock);
988}
989
20ddf665
VS
990bool intel_crtc_active(struct drm_crtc *crtc)
991{
992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
993
994 /* Be paranoid as we can arrive here with only partial
995 * state retrieved from the hardware during setup.
996 *
241bfc38 997 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
998 * as Haswell has gained clock readout/fastboot support.
999 *
66e514c1 1000 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1001 * properly reconstruct framebuffers.
c3d1f436
MR
1002 *
1003 * FIXME: The intel_crtc->active here should be switched to
1004 * crtc->state->active once we have proper CRTC states wired up
1005 * for atomic.
20ddf665 1006 */
c3d1f436 1007 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1008 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1009}
1010
a5c961d1
PZ
1011enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1012 enum pipe pipe)
1013{
1014 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1016
6e3c9717 1017 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1018}
1019
fbf49ea2
VS
1020static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1021{
1022 struct drm_i915_private *dev_priv = dev->dev_private;
1023 u32 reg = PIPEDSL(pipe);
1024 u32 line1, line2;
1025 u32 line_mask;
1026
1027 if (IS_GEN2(dev))
1028 line_mask = DSL_LINEMASK_GEN2;
1029 else
1030 line_mask = DSL_LINEMASK_GEN3;
1031
1032 line1 = I915_READ(reg) & line_mask;
1033 mdelay(5);
1034 line2 = I915_READ(reg) & line_mask;
1035
1036 return line1 == line2;
1037}
1038
ab7ad7f6
KP
1039/*
1040 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1041 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1042 *
1043 * After disabling a pipe, we can't wait for vblank in the usual way,
1044 * spinning on the vblank interrupt status bit, since we won't actually
1045 * see an interrupt when the pipe is disabled.
1046 *
ab7ad7f6
KP
1047 * On Gen4 and above:
1048 * wait for the pipe register state bit to turn off
1049 *
1050 * Otherwise:
1051 * wait for the display line value to settle (it usually
1052 * ends up stopping at the start of the next frame).
58e10eb9 1053 *
9d0498a2 1054 */
575f7ab7 1055static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1056{
575f7ab7 1057 struct drm_device *dev = crtc->base.dev;
9d0498a2 1058 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1059 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1060 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1061
1062 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1063 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1064
1065 /* Wait for the Pipe State to go off */
58e10eb9
CW
1066 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1067 100))
284637d9 1068 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1069 } else {
ab7ad7f6 1070 /* Wait for the display line to settle */
fbf49ea2 1071 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1072 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1073 }
79e53945
JB
1074}
1075
b0ea7d37
DL
1076/*
1077 * ibx_digital_port_connected - is the specified port connected?
1078 * @dev_priv: i915 private structure
1079 * @port: the port to test
1080 *
1081 * Returns true if @port is connected, false otherwise.
1082 */
1083bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1084 struct intel_digital_port *port)
1085{
1086 u32 bit;
1087
c36346e3 1088 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1089 switch (port->port) {
c36346e3
DL
1090 case PORT_B:
1091 bit = SDE_PORTB_HOTPLUG;
1092 break;
1093 case PORT_C:
1094 bit = SDE_PORTC_HOTPLUG;
1095 break;
1096 case PORT_D:
1097 bit = SDE_PORTD_HOTPLUG;
1098 break;
1099 default:
1100 return true;
1101 }
1102 } else {
eba905b2 1103 switch (port->port) {
c36346e3
DL
1104 case PORT_B:
1105 bit = SDE_PORTB_HOTPLUG_CPT;
1106 break;
1107 case PORT_C:
1108 bit = SDE_PORTC_HOTPLUG_CPT;
1109 break;
1110 case PORT_D:
1111 bit = SDE_PORTD_HOTPLUG_CPT;
1112 break;
1113 default:
1114 return true;
1115 }
b0ea7d37
DL
1116 }
1117
1118 return I915_READ(SDEISR) & bit;
1119}
1120
b24e7179
JB
1121static const char *state_string(bool enabled)
1122{
1123 return enabled ? "on" : "off";
1124}
1125
1126/* Only for pre-ILK configs */
55607e8a
DV
1127void assert_pll(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
b24e7179
JB
1129{
1130 int reg;
1131 u32 val;
1132 bool cur_state;
1133
1134 reg = DPLL(pipe);
1135 val = I915_READ(reg);
1136 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1137 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1138 "PLL state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
b24e7179 1141
23538ef1
JN
1142/* XXX: the dsi pll is shared between MIPI DSI ports */
1143static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1144{
1145 u32 val;
1146 bool cur_state;
1147
a580516d 1148 mutex_lock(&dev_priv->sb_lock);
23538ef1 1149 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1150 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1151
1152 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1154 "DSI PLL state assertion failure (expected %s, current %s)\n",
1155 state_string(state), state_string(cur_state));
1156}
1157#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1158#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1159
55607e8a 1160struct intel_shared_dpll *
e2b78267
DV
1161intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1162{
1163 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1164
6e3c9717 1165 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1166 return NULL;
1167
6e3c9717 1168 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1169}
1170
040484af 1171/* For ILK+ */
55607e8a
DV
1172void assert_shared_dpll(struct drm_i915_private *dev_priv,
1173 struct intel_shared_dpll *pll,
1174 bool state)
040484af 1175{
040484af 1176 bool cur_state;
5358901f 1177 struct intel_dpll_hw_state hw_state;
040484af 1178
92b27b08 1179 if (WARN (!pll,
46edb027 1180 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1181 return;
ee7b9f93 1182
5358901f 1183 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1184 I915_STATE_WARN(cur_state != state,
5358901f
DV
1185 "%s assertion failure (expected %s, current %s)\n",
1186 pll->name, state_string(state), state_string(cur_state));
040484af 1187}
040484af
JB
1188
1189static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
1192 int reg;
1193 u32 val;
1194 bool cur_state;
ad80a810
PZ
1195 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1196 pipe);
040484af 1197
affa9354
PZ
1198 if (HAS_DDI(dev_priv->dev)) {
1199 /* DDI does not have a specific FDI_TX register */
ad80a810 1200 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1201 val = I915_READ(reg);
ad80a810 1202 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1203 } else {
1204 reg = FDI_TX_CTL(pipe);
1205 val = I915_READ(reg);
1206 cur_state = !!(val & FDI_TX_ENABLE);
1207 }
e2c719b7 1208 I915_STATE_WARN(cur_state != state,
040484af
JB
1209 "FDI TX state assertion failure (expected %s, current %s)\n",
1210 state_string(state), state_string(cur_state));
1211}
1212#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1213#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1214
1215static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1216 enum pipe pipe, bool state)
1217{
1218 int reg;
1219 u32 val;
1220 bool cur_state;
1221
d63fa0dc
PZ
1222 reg = FDI_RX_CTL(pipe);
1223 val = I915_READ(reg);
1224 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1225 I915_STATE_WARN(cur_state != state,
040484af
JB
1226 "FDI RX state assertion failure (expected %s, current %s)\n",
1227 state_string(state), state_string(cur_state));
1228}
1229#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1230#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1231
1232static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1233 enum pipe pipe)
1234{
1235 int reg;
1236 u32 val;
1237
1238 /* ILK FDI PLL is always enabled */
3d13ef2e 1239 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1240 return;
1241
bf507ef7 1242 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1243 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1244 return;
1245
040484af
JB
1246 reg = FDI_TX_CTL(pipe);
1247 val = I915_READ(reg);
e2c719b7 1248 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1249}
1250
55607e8a
DV
1251void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, bool state)
040484af
JB
1253{
1254 int reg;
1255 u32 val;
55607e8a 1256 bool cur_state;
040484af
JB
1257
1258 reg = FDI_RX_CTL(pipe);
1259 val = I915_READ(reg);
55607e8a 1260 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1261 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1262 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1263 state_string(state), state_string(cur_state));
040484af
JB
1264}
1265
b680c37a
DV
1266void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1267 enum pipe pipe)
ea0760cf 1268{
bedd4dba
JN
1269 struct drm_device *dev = dev_priv->dev;
1270 int pp_reg;
ea0760cf
JB
1271 u32 val;
1272 enum pipe panel_pipe = PIPE_A;
0de3b485 1273 bool locked = true;
ea0760cf 1274
bedd4dba
JN
1275 if (WARN_ON(HAS_DDI(dev)))
1276 return;
1277
1278 if (HAS_PCH_SPLIT(dev)) {
1279 u32 port_sel;
1280
ea0760cf 1281 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1282 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1283
1284 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1285 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
1287 /* XXX: else fix for eDP */
1288 } else if (IS_VALLEYVIEW(dev)) {
1289 /* presumably write lock depends on pipe, not port select */
1290 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1291 panel_pipe = pipe;
ea0760cf
JB
1292 } else {
1293 pp_reg = PP_CONTROL;
bedd4dba
JN
1294 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1295 panel_pipe = PIPE_B;
ea0760cf
JB
1296 }
1297
1298 val = I915_READ(pp_reg);
1299 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1300 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1301 locked = false;
1302
e2c719b7 1303 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1304 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1305 pipe_name(pipe));
ea0760cf
JB
1306}
1307
93ce0ba6
JN
1308static void assert_cursor(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, bool state)
1310{
1311 struct drm_device *dev = dev_priv->dev;
1312 bool cur_state;
1313
d9d82081 1314 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1315 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1316 else
5efb3e28 1317 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1318
e2c719b7 1319 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1320 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1321 pipe_name(pipe), state_string(state), state_string(cur_state));
1322}
1323#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1324#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1325
b840d907
JB
1326void assert_pipe(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
b24e7179
JB
1328{
1329 int reg;
1330 u32 val;
63d7bbe9 1331 bool cur_state;
702e7a56
PZ
1332 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1333 pipe);
b24e7179 1334
b6b5d049
VS
1335 /* if we need the pipe quirk it must be always on */
1336 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1337 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1338 state = true;
1339
f458ebbc 1340 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1341 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1342 cur_state = false;
1343 } else {
1344 reg = PIPECONF(cpu_transcoder);
1345 val = I915_READ(reg);
1346 cur_state = !!(val & PIPECONF_ENABLE);
1347 }
1348
e2c719b7 1349 I915_STATE_WARN(cur_state != state,
63d7bbe9 1350 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1351 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1352}
1353
931872fc
CW
1354static void assert_plane(struct drm_i915_private *dev_priv,
1355 enum plane plane, bool state)
b24e7179
JB
1356{
1357 int reg;
1358 u32 val;
931872fc 1359 bool cur_state;
b24e7179
JB
1360
1361 reg = DSPCNTR(plane);
1362 val = I915_READ(reg);
931872fc 1363 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1364 I915_STATE_WARN(cur_state != state,
931872fc
CW
1365 "plane %c assertion failure (expected %s, current %s)\n",
1366 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1367}
1368
931872fc
CW
1369#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1370#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1371
b24e7179
JB
1372static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe)
1374{
653e1026 1375 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1376 int reg, i;
1377 u32 val;
1378 int cur_pipe;
1379
653e1026
VS
1380 /* Primary planes are fixed to pipes on gen4+ */
1381 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1382 reg = DSPCNTR(pipe);
1383 val = I915_READ(reg);
e2c719b7 1384 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1385 "plane %c assertion failure, should be disabled but not\n",
1386 plane_name(pipe));
19ec1358 1387 return;
28c05794 1388 }
19ec1358 1389
b24e7179 1390 /* Need to check both planes against the pipe */
055e393f 1391 for_each_pipe(dev_priv, i) {
b24e7179
JB
1392 reg = DSPCNTR(i);
1393 val = I915_READ(reg);
1394 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1395 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1396 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1397 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1398 plane_name(i), pipe_name(pipe));
b24e7179
JB
1399 }
1400}
1401
19332d7a
JB
1402static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe)
1404{
20674eef 1405 struct drm_device *dev = dev_priv->dev;
1fe47785 1406 int reg, sprite;
19332d7a
JB
1407 u32 val;
1408
7feb8b88 1409 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1410 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1411 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1412 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1413 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1414 sprite, pipe_name(pipe));
1415 }
1416 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1417 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1418 reg = SPCNTR(pipe, sprite);
20674eef 1419 val = I915_READ(reg);
e2c719b7 1420 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1422 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1423 }
1424 } else if (INTEL_INFO(dev)->gen >= 7) {
1425 reg = SPRCTL(pipe);
19332d7a 1426 val = I915_READ(reg);
e2c719b7 1427 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1429 plane_name(pipe), pipe_name(pipe));
1430 } else if (INTEL_INFO(dev)->gen >= 5) {
1431 reg = DVSCNTR(pipe);
19332d7a 1432 val = I915_READ(reg);
e2c719b7 1433 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1434 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1435 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1436 }
1437}
1438
08c71e5e
VS
1439static void assert_vblank_disabled(struct drm_crtc *crtc)
1440{
e2c719b7 1441 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1442 drm_crtc_vblank_put(crtc);
1443}
1444
89eff4be 1445static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1446{
1447 u32 val;
1448 bool enabled;
1449
e2c719b7 1450 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1451
92f2584a
JB
1452 val = I915_READ(PCH_DREF_CONTROL);
1453 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1454 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1455 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1456}
1457
ab9412ba
DV
1458static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1459 enum pipe pipe)
92f2584a
JB
1460{
1461 int reg;
1462 u32 val;
1463 bool enabled;
1464
ab9412ba 1465 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1466 val = I915_READ(reg);
1467 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1468 I915_STATE_WARN(enabled,
9db4a9c7
JB
1469 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1470 pipe_name(pipe));
92f2584a
JB
1471}
1472
4e634389
KP
1473static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1474 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1475{
1476 if ((val & DP_PORT_EN) == 0)
1477 return false;
1478
1479 if (HAS_PCH_CPT(dev_priv->dev)) {
1480 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1481 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1482 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1483 return false;
44f37d1f
CML
1484 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1485 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1486 return false;
f0575e92
KP
1487 } else {
1488 if ((val & DP_PIPE_MASK) != (pipe << 30))
1489 return false;
1490 }
1491 return true;
1492}
1493
1519b995
KP
1494static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1495 enum pipe pipe, u32 val)
1496{
dc0fa718 1497 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1498 return false;
1499
1500 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1501 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1502 return false;
44f37d1f
CML
1503 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1504 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1505 return false;
1519b995 1506 } else {
dc0fa718 1507 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1508 return false;
1509 }
1510 return true;
1511}
1512
1513static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe, u32 val)
1515{
1516 if ((val & LVDS_PORT_EN) == 0)
1517 return false;
1518
1519 if (HAS_PCH_CPT(dev_priv->dev)) {
1520 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1521 return false;
1522 } else {
1523 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1524 return false;
1525 }
1526 return true;
1527}
1528
1529static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1530 enum pipe pipe, u32 val)
1531{
1532 if ((val & ADPA_DAC_ENABLE) == 0)
1533 return false;
1534 if (HAS_PCH_CPT(dev_priv->dev)) {
1535 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1536 return false;
1537 } else {
1538 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1539 return false;
1540 }
1541 return true;
1542}
1543
291906f1 1544static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1545 enum pipe pipe, int reg, u32 port_sel)
291906f1 1546{
47a05eca 1547 u32 val = I915_READ(reg);
e2c719b7 1548 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1549 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1550 reg, pipe_name(pipe));
de9a35ab 1551
e2c719b7 1552 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1553 && (val & DP_PIPEB_SELECT),
de9a35ab 1554 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1555}
1556
1557static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1558 enum pipe pipe, int reg)
1559{
47a05eca 1560 u32 val = I915_READ(reg);
e2c719b7 1561 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1562 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1563 reg, pipe_name(pipe));
de9a35ab 1564
e2c719b7 1565 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1566 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1567 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1568}
1569
1570static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1571 enum pipe pipe)
1572{
1573 int reg;
1574 u32 val;
291906f1 1575
f0575e92
KP
1576 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1579
1580 reg = PCH_ADPA;
1581 val = I915_READ(reg);
e2c719b7 1582 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1583 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1584 pipe_name(pipe));
291906f1
JB
1585
1586 reg = PCH_LVDS;
1587 val = I915_READ(reg);
e2c719b7 1588 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1589 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1590 pipe_name(pipe));
291906f1 1591
e2debe91
PZ
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1595}
1596
40e9cf64
JB
1597static void intel_init_dpio(struct drm_device *dev)
1598{
1599 struct drm_i915_private *dev_priv = dev->dev_private;
1600
1601 if (!IS_VALLEYVIEW(dev))
1602 return;
1603
a09caddd
CML
1604 /*
1605 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1606 * CHV x1 PHY (DP/HDMI D)
1607 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1608 */
1609 if (IS_CHERRYVIEW(dev)) {
1610 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1612 } else {
1613 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1614 }
5382f5f3
JB
1615}
1616
d288f65f 1617static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1618 const struct intel_crtc_state *pipe_config)
87442f73 1619{
426115cf
DV
1620 struct drm_device *dev = crtc->base.dev;
1621 struct drm_i915_private *dev_priv = dev->dev_private;
1622 int reg = DPLL(crtc->pipe);
d288f65f 1623 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1624
426115cf 1625 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1626
1627 /* No really, not for ILK+ */
1628 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1629
1630 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1631 if (IS_MOBILE(dev_priv->dev))
426115cf 1632 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1633
426115cf
DV
1634 I915_WRITE(reg, dpll);
1635 POSTING_READ(reg);
1636 udelay(150);
1637
1638 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1639 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1640
d288f65f 1641 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1642 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1643
1644 /* We do this three times for luck */
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 */
426115cf 1651 I915_WRITE(reg, dpll);
87442f73
DV
1652 POSTING_READ(reg);
1653 udelay(150); /* wait for warmup */
1654}
1655
d288f65f 1656static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1657 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1658{
1659 struct drm_device *dev = crtc->base.dev;
1660 struct drm_i915_private *dev_priv = dev->dev_private;
1661 int pipe = crtc->pipe;
1662 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1663 u32 tmp;
1664
1665 assert_pipe_disabled(dev_priv, crtc->pipe);
1666
1667 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1668
a580516d 1669 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1670
1671 /* Enable back the 10bit clock to display controller */
1672 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1673 tmp |= DPIO_DCLKP_EN;
1674 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1675
54433e91
VS
1676 mutex_unlock(&dev_priv->sb_lock);
1677
9d556c99
CML
1678 /*
1679 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1680 */
1681 udelay(1);
1682
1683 /* Enable PLL */
d288f65f 1684 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1685
1686 /* Check PLL is locked */
a11b0703 1687 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1688 DRM_ERROR("PLL %d failed to lock\n", pipe);
1689
a11b0703 1690 /* not sure when this should be written */
d288f65f 1691 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1692 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1693}
1694
1c4e0274
VS
1695static int intel_num_dvo_pipes(struct drm_device *dev)
1696{
1697 struct intel_crtc *crtc;
1698 int count = 0;
1699
1700 for_each_intel_crtc(dev, crtc)
3538b9df 1701 count += crtc->base.state->active &&
409ee761 1702 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1703
1704 return count;
1705}
1706
66e3d5c0 1707static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1708{
66e3d5c0
DV
1709 struct drm_device *dev = crtc->base.dev;
1710 struct drm_i915_private *dev_priv = dev->dev_private;
1711 int reg = DPLL(crtc->pipe);
6e3c9717 1712 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1713
66e3d5c0 1714 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1715
63d7bbe9 1716 /* No really, not for ILK+ */
3d13ef2e 1717 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1718
1719 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1720 if (IS_MOBILE(dev) && !IS_I830(dev))
1721 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1722
1c4e0274
VS
1723 /* Enable DVO 2x clock on both PLLs if necessary */
1724 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1725 /*
1726 * It appears to be important that we don't enable this
1727 * for the current pipe before otherwise configuring the
1728 * PLL. No idea how this should be handled if multiple
1729 * DVO outputs are enabled simultaneosly.
1730 */
1731 dpll |= DPLL_DVO_2X_MODE;
1732 I915_WRITE(DPLL(!crtc->pipe),
1733 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1734 }
66e3d5c0
DV
1735
1736 /* Wait for the clocks to stabilize. */
1737 POSTING_READ(reg);
1738 udelay(150);
1739
1740 if (INTEL_INFO(dev)->gen >= 4) {
1741 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1742 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1743 } else {
1744 /* The pixel multiplier can only be updated once the
1745 * DPLL is enabled and the clocks are stable.
1746 *
1747 * So write it again.
1748 */
1749 I915_WRITE(reg, dpll);
1750 }
63d7bbe9
JB
1751
1752 /* We do this three times for luck */
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 */
66e3d5c0 1759 I915_WRITE(reg, dpll);
63d7bbe9
JB
1760 POSTING_READ(reg);
1761 udelay(150); /* wait for warmup */
1762}
1763
1764/**
50b44a44 1765 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1766 * @dev_priv: i915 private structure
1767 * @pipe: pipe PLL to disable
1768 *
1769 * Disable the PLL for @pipe, making sure the pipe is off first.
1770 *
1771 * Note! This is for pre-ILK only.
1772 */
1c4e0274 1773static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1774{
1c4e0274
VS
1775 struct drm_device *dev = crtc->base.dev;
1776 struct drm_i915_private *dev_priv = dev->dev_private;
1777 enum pipe pipe = crtc->pipe;
1778
1779 /* Disable DVO 2x clock on both PLLs if necessary */
1780 if (IS_I830(dev) &&
409ee761 1781 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1782 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1783 I915_WRITE(DPLL(PIPE_B),
1784 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1785 I915_WRITE(DPLL(PIPE_A),
1786 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1787 }
1788
b6b5d049
VS
1789 /* Don't disable pipe or pipe PLLs if needed */
1790 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1791 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1792 return;
1793
1794 /* Make sure the pipe isn't still relying on us */
1795 assert_pipe_disabled(dev_priv, pipe);
1796
50b44a44
DV
1797 I915_WRITE(DPLL(pipe), 0);
1798 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1799}
1800
f6071166
JB
1801static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1802{
1803 u32 val = 0;
1804
1805 /* Make sure the pipe isn't still relying on us */
1806 assert_pipe_disabled(dev_priv, pipe);
1807
e5cbfbfb
ID
1808 /*
1809 * Leave integrated clock source and reference clock enabled for pipe B.
1810 * The latter is needed for VGA hotplug / manual detection.
1811 */
f6071166 1812 if (pipe == PIPE_B)
e5cbfbfb 1813 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1814 I915_WRITE(DPLL(pipe), val);
1815 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1816
1817}
1818
1819static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1820{
d752048d 1821 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1822 u32 val;
1823
a11b0703
VS
1824 /* Make sure the pipe isn't still relying on us */
1825 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1826
a11b0703 1827 /* Set PLL en = 0 */
d17ec4ce 1828 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1829 if (pipe != PIPE_A)
1830 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1831 I915_WRITE(DPLL(pipe), val);
1832 POSTING_READ(DPLL(pipe));
d752048d 1833
a580516d 1834 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1835
1836 /* Disable 10bit clock to display controller */
1837 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1838 val &= ~DPIO_DCLKP_EN;
1839 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1840
61407f6d
VS
1841 /* disable left/right clock distribution */
1842 if (pipe != PIPE_B) {
1843 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1844 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1845 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1846 } else {
1847 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1848 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1849 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1850 }
1851
a580516d 1852 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1853}
1854
e4607fcf 1855void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1856 struct intel_digital_port *dport,
1857 unsigned int expected_mask)
89b667f8
JB
1858{
1859 u32 port_mask;
00fc31b7 1860 int dpll_reg;
89b667f8 1861
e4607fcf
CML
1862 switch (dport->port) {
1863 case PORT_B:
89b667f8 1864 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1865 dpll_reg = DPLL(0);
e4607fcf
CML
1866 break;
1867 case PORT_C:
89b667f8 1868 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1869 dpll_reg = DPLL(0);
9b6de0a1 1870 expected_mask <<= 4;
00fc31b7
CML
1871 break;
1872 case PORT_D:
1873 port_mask = DPLL_PORTD_READY_MASK;
1874 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1875 break;
1876 default:
1877 BUG();
1878 }
89b667f8 1879
9b6de0a1
VS
1880 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1881 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1882 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1883}
1884
b14b1055
DV
1885static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1886{
1887 struct drm_device *dev = crtc->base.dev;
1888 struct drm_i915_private *dev_priv = dev->dev_private;
1889 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1890
be19f0ff
CW
1891 if (WARN_ON(pll == NULL))
1892 return;
1893
3e369b76 1894 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1895 if (pll->active == 0) {
1896 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1897 WARN_ON(pll->on);
1898 assert_shared_dpll_disabled(dev_priv, pll);
1899
1900 pll->mode_set(dev_priv, pll);
1901 }
1902}
1903
92f2584a 1904/**
85b3894f 1905 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1906 * @dev_priv: i915 private structure
1907 * @pipe: pipe PLL to enable
1908 *
1909 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1910 * drives the transcoder clock.
1911 */
85b3894f 1912static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1913{
3d13ef2e
DL
1914 struct drm_device *dev = crtc->base.dev;
1915 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1916 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1917
87a875bb 1918 if (WARN_ON(pll == NULL))
48da64a8
CW
1919 return;
1920
3e369b76 1921 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1922 return;
ee7b9f93 1923
74dd6928 1924 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1925 pll->name, pll->active, pll->on,
e2b78267 1926 crtc->base.base.id);
92f2584a 1927
cdbd2316
DV
1928 if (pll->active++) {
1929 WARN_ON(!pll->on);
e9d6944e 1930 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1931 return;
1932 }
f4a091c7 1933 WARN_ON(pll->on);
ee7b9f93 1934
bd2bb1b9
PZ
1935 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1936
46edb027 1937 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1938 pll->enable(dev_priv, pll);
ee7b9f93 1939 pll->on = true;
92f2584a
JB
1940}
1941
f6daaec2 1942static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1943{
3d13ef2e
DL
1944 struct drm_device *dev = crtc->base.dev;
1945 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1946 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1947
92f2584a 1948 /* PCH only available on ILK+ */
3d13ef2e 1949 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1950 if (WARN_ON(pll == NULL))
ee7b9f93 1951 return;
92f2584a 1952
3e369b76 1953 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1954 return;
7a419866 1955
46edb027
DV
1956 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1957 pll->name, pll->active, pll->on,
e2b78267 1958 crtc->base.base.id);
7a419866 1959
48da64a8 1960 if (WARN_ON(pll->active == 0)) {
e9d6944e 1961 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1962 return;
1963 }
1964
e9d6944e 1965 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1966 WARN_ON(!pll->on);
cdbd2316 1967 if (--pll->active)
7a419866 1968 return;
ee7b9f93 1969
46edb027 1970 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1971 pll->disable(dev_priv, pll);
ee7b9f93 1972 pll->on = false;
bd2bb1b9
PZ
1973
1974 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1975}
1976
b8a4f404
PZ
1977static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1978 enum pipe pipe)
040484af 1979{
23670b32 1980 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1981 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1983 uint32_t reg, val, pipeconf_val;
040484af
JB
1984
1985 /* PCH only available on ILK+ */
55522f37 1986 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1987
1988 /* Make sure PCH DPLL is enabled */
e72f9fbf 1989 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1990 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1991
1992 /* FDI must be feeding us bits for PCH ports */
1993 assert_fdi_tx_enabled(dev_priv, pipe);
1994 assert_fdi_rx_enabled(dev_priv, pipe);
1995
23670b32
DV
1996 if (HAS_PCH_CPT(dev)) {
1997 /* Workaround: Set the timing override bit before enabling the
1998 * pch transcoder. */
1999 reg = TRANS_CHICKEN2(pipe);
2000 val = I915_READ(reg);
2001 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2002 I915_WRITE(reg, val);
59c859d6 2003 }
23670b32 2004
ab9412ba 2005 reg = PCH_TRANSCONF(pipe);
040484af 2006 val = I915_READ(reg);
5f7f726d 2007 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2008
2009 if (HAS_PCH_IBX(dev_priv->dev)) {
2010 /*
2011 * make the BPC in transcoder be consistent with
2012 * that in pipeconf reg.
2013 */
dfd07d72
DV
2014 val &= ~PIPECONF_BPC_MASK;
2015 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2016 }
5f7f726d
PZ
2017
2018 val &= ~TRANS_INTERLACE_MASK;
2019 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2020 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2021 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2022 val |= TRANS_LEGACY_INTERLACED_ILK;
2023 else
2024 val |= TRANS_INTERLACED;
5f7f726d
PZ
2025 else
2026 val |= TRANS_PROGRESSIVE;
2027
040484af
JB
2028 I915_WRITE(reg, val | TRANS_ENABLE);
2029 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2030 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2031}
2032
8fb033d7 2033static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2034 enum transcoder cpu_transcoder)
040484af 2035{
8fb033d7 2036 u32 val, pipeconf_val;
8fb033d7
PZ
2037
2038 /* PCH only available on ILK+ */
55522f37 2039 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2040
8fb033d7 2041 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2042 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2043 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2044
223a6fdf
PZ
2045 /* Workaround: set timing override bit. */
2046 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2047 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2048 I915_WRITE(_TRANSA_CHICKEN2, val);
2049
25f3ef11 2050 val = TRANS_ENABLE;
937bb610 2051 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2052
9a76b1c6
PZ
2053 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2054 PIPECONF_INTERLACED_ILK)
a35f2679 2055 val |= TRANS_INTERLACED;
8fb033d7
PZ
2056 else
2057 val |= TRANS_PROGRESSIVE;
2058
ab9412ba
DV
2059 I915_WRITE(LPT_TRANSCONF, val);
2060 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2061 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2062}
2063
b8a4f404
PZ
2064static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2065 enum pipe pipe)
040484af 2066{
23670b32
DV
2067 struct drm_device *dev = dev_priv->dev;
2068 uint32_t reg, val;
040484af
JB
2069
2070 /* FDI relies on the transcoder */
2071 assert_fdi_tx_disabled(dev_priv, pipe);
2072 assert_fdi_rx_disabled(dev_priv, pipe);
2073
291906f1
JB
2074 /* Ports must be off as well */
2075 assert_pch_ports_disabled(dev_priv, pipe);
2076
ab9412ba 2077 reg = PCH_TRANSCONF(pipe);
040484af
JB
2078 val = I915_READ(reg);
2079 val &= ~TRANS_ENABLE;
2080 I915_WRITE(reg, val);
2081 /* wait for PCH transcoder off, transcoder state */
2082 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2083 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2084
2085 if (!HAS_PCH_IBX(dev)) {
2086 /* Workaround: Clear the timing override chicken bit again. */
2087 reg = TRANS_CHICKEN2(pipe);
2088 val = I915_READ(reg);
2089 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2090 I915_WRITE(reg, val);
2091 }
040484af
JB
2092}
2093
ab4d966c 2094static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2095{
8fb033d7
PZ
2096 u32 val;
2097
ab9412ba 2098 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2099 val &= ~TRANS_ENABLE;
ab9412ba 2100 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2101 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2102 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2103 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2104
2105 /* Workaround: clear timing override bit. */
2106 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2107 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2108 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2109}
2110
b24e7179 2111/**
309cfea8 2112 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2113 * @crtc: crtc responsible for the pipe
b24e7179 2114 *
0372264a 2115 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2116 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2117 */
e1fdc473 2118static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2119{
0372264a
PZ
2120 struct drm_device *dev = crtc->base.dev;
2121 struct drm_i915_private *dev_priv = dev->dev_private;
2122 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2123 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2124 pipe);
1a240d4d 2125 enum pipe pch_transcoder;
b24e7179
JB
2126 int reg;
2127 u32 val;
2128
58c6eaa2 2129 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2130 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2131 assert_sprites_disabled(dev_priv, pipe);
2132
681e5811 2133 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2134 pch_transcoder = TRANSCODER_A;
2135 else
2136 pch_transcoder = pipe;
2137
b24e7179
JB
2138 /*
2139 * A pipe without a PLL won't actually be able to drive bits from
2140 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2141 * need the check.
2142 */
50360403 2143 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2144 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2145 assert_dsi_pll_enabled(dev_priv);
2146 else
2147 assert_pll_enabled(dev_priv, pipe);
040484af 2148 else {
6e3c9717 2149 if (crtc->config->has_pch_encoder) {
040484af 2150 /* if driving the PCH, we need FDI enabled */
cc391bbb 2151 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2152 assert_fdi_tx_pll_enabled(dev_priv,
2153 (enum pipe) cpu_transcoder);
040484af
JB
2154 }
2155 /* FIXME: assert CPU port conditions for SNB+ */
2156 }
b24e7179 2157
702e7a56 2158 reg = PIPECONF(cpu_transcoder);
b24e7179 2159 val = I915_READ(reg);
7ad25d48 2160 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2161 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2162 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2163 return;
7ad25d48 2164 }
00d70b15
CW
2165
2166 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2167 POSTING_READ(reg);
b24e7179
JB
2168}
2169
2170/**
309cfea8 2171 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2172 * @crtc: crtc whose pipes is to be disabled
b24e7179 2173 *
575f7ab7
VS
2174 * Disable the pipe of @crtc, making sure that various hardware
2175 * specific requirements are met, if applicable, e.g. plane
2176 * disabled, panel fitter off, etc.
b24e7179
JB
2177 *
2178 * Will wait until the pipe has shut down before returning.
2179 */
575f7ab7 2180static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2181{
575f7ab7 2182 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2183 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2184 enum pipe pipe = crtc->pipe;
b24e7179
JB
2185 int reg;
2186 u32 val;
2187
2188 /*
2189 * Make sure planes won't keep trying to pump pixels to us,
2190 * or we might hang the display.
2191 */
2192 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2193 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2194 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2195
702e7a56 2196 reg = PIPECONF(cpu_transcoder);
b24e7179 2197 val = I915_READ(reg);
00d70b15
CW
2198 if ((val & PIPECONF_ENABLE) == 0)
2199 return;
2200
67adc644
VS
2201 /*
2202 * Double wide has implications for planes
2203 * so best keep it disabled when not needed.
2204 */
6e3c9717 2205 if (crtc->config->double_wide)
67adc644
VS
2206 val &= ~PIPECONF_DOUBLE_WIDE;
2207
2208 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2209 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2210 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2211 val &= ~PIPECONF_ENABLE;
2212
2213 I915_WRITE(reg, val);
2214 if ((val & PIPECONF_ENABLE) == 0)
2215 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2216}
2217
2218/**
262ca2b0 2219 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2220 * @plane: plane to be enabled
2221 * @crtc: crtc for the plane
b24e7179 2222 *
fdd508a6 2223 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2224 */
fdd508a6
VS
2225static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2226 struct drm_crtc *crtc)
b24e7179 2227{
fdd508a6
VS
2228 struct drm_device *dev = plane->dev;
2229 struct drm_i915_private *dev_priv = dev->dev_private;
2230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2231
2232 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2233 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2234 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2235
fdd508a6
VS
2236 dev_priv->display.update_primary_plane(crtc, plane->fb,
2237 crtc->x, crtc->y);
b24e7179
JB
2238}
2239
693db184
CW
2240static bool need_vtd_wa(struct drm_device *dev)
2241{
2242#ifdef CONFIG_INTEL_IOMMU
2243 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2244 return true;
2245#endif
2246 return false;
2247}
2248
50470bb0 2249unsigned int
6761dd31
TU
2250intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2251 uint64_t fb_format_modifier)
a57ce0b2 2252{
6761dd31
TU
2253 unsigned int tile_height;
2254 uint32_t pixel_bytes;
a57ce0b2 2255
b5d0e9bf
DL
2256 switch (fb_format_modifier) {
2257 case DRM_FORMAT_MOD_NONE:
2258 tile_height = 1;
2259 break;
2260 case I915_FORMAT_MOD_X_TILED:
2261 tile_height = IS_GEN2(dev) ? 16 : 8;
2262 break;
2263 case I915_FORMAT_MOD_Y_TILED:
2264 tile_height = 32;
2265 break;
2266 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2267 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2268 switch (pixel_bytes) {
b5d0e9bf 2269 default:
6761dd31 2270 case 1:
b5d0e9bf
DL
2271 tile_height = 64;
2272 break;
6761dd31
TU
2273 case 2:
2274 case 4:
b5d0e9bf
DL
2275 tile_height = 32;
2276 break;
6761dd31 2277 case 8:
b5d0e9bf
DL
2278 tile_height = 16;
2279 break;
6761dd31 2280 case 16:
b5d0e9bf
DL
2281 WARN_ONCE(1,
2282 "128-bit pixels are not supported for display!");
2283 tile_height = 16;
2284 break;
2285 }
2286 break;
2287 default:
2288 MISSING_CASE(fb_format_modifier);
2289 tile_height = 1;
2290 break;
2291 }
091df6cb 2292
6761dd31
TU
2293 return tile_height;
2294}
2295
2296unsigned int
2297intel_fb_align_height(struct drm_device *dev, unsigned int height,
2298 uint32_t pixel_format, uint64_t fb_format_modifier)
2299{
2300 return ALIGN(height, intel_tile_height(dev, pixel_format,
2301 fb_format_modifier));
a57ce0b2
JB
2302}
2303
f64b98cd
TU
2304static int
2305intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2306 const struct drm_plane_state *plane_state)
2307{
50470bb0 2308 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2309
f64b98cd
TU
2310 *view = i915_ggtt_view_normal;
2311
50470bb0
TU
2312 if (!plane_state)
2313 return 0;
2314
121920fa 2315 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2316 return 0;
2317
9abc4648 2318 *view = i915_ggtt_view_rotated;
50470bb0
TU
2319
2320 info->height = fb->height;
2321 info->pixel_format = fb->pixel_format;
2322 info->pitch = fb->pitches[0];
2323 info->fb_modifier = fb->modifier[0];
2324
f64b98cd
TU
2325 return 0;
2326}
2327
127bd2ac 2328int
850c4cdc
TU
2329intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2330 struct drm_framebuffer *fb,
82bc3b2d 2331 const struct drm_plane_state *plane_state,
a4872ba6 2332 struct intel_engine_cs *pipelined)
6b95a207 2333{
850c4cdc 2334 struct drm_device *dev = fb->dev;
ce453d81 2335 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2337 struct i915_ggtt_view view;
6b95a207
KH
2338 u32 alignment;
2339 int ret;
2340
ebcdd39e
MR
2341 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2342
7b911adc
TU
2343 switch (fb->modifier[0]) {
2344 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2345 if (INTEL_INFO(dev)->gen >= 9)
2346 alignment = 256 * 1024;
2347 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2348 alignment = 128 * 1024;
a6c45cf0 2349 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2350 alignment = 4 * 1024;
2351 else
2352 alignment = 64 * 1024;
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,
f64b98cd 2397 &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. */
bc752862
CW
2442unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2443 unsigned int tiling_mode,
2444 unsigned int cpp,
2445 unsigned int pitch)
c2c75131 2446{
bc752862
CW
2447 if (tiling_mode != I915_TILING_NONE) {
2448 unsigned int tile_rows, tiles;
c2c75131 2449
bc752862
CW
2450 tile_rows = *y / 8;
2451 *y %= 8;
c2c75131 2452
bc752862
CW
2453 tiles = *x / (512/cpp);
2454 *x %= 512/cpp;
2455
2456 return tile_rows * pitch * 8 + tiles * 4096;
2457 } else {
2458 unsigned int offset;
2459
2460 offset = *y * pitch + *x * cpp;
2461 *y = 0;
2462 *x = (offset & 4095) / cpp;
2463 return offset & -4096;
2464 }
c2c75131
DV
2465}
2466
b35d63fa 2467static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2468{
2469 switch (format) {
2470 case DISPPLANE_8BPP:
2471 return DRM_FORMAT_C8;
2472 case DISPPLANE_BGRX555:
2473 return DRM_FORMAT_XRGB1555;
2474 case DISPPLANE_BGRX565:
2475 return DRM_FORMAT_RGB565;
2476 default:
2477 case DISPPLANE_BGRX888:
2478 return DRM_FORMAT_XRGB8888;
2479 case DISPPLANE_RGBX888:
2480 return DRM_FORMAT_XBGR8888;
2481 case DISPPLANE_BGRX101010:
2482 return DRM_FORMAT_XRGB2101010;
2483 case DISPPLANE_RGBX101010:
2484 return DRM_FORMAT_XBGR2101010;
2485 }
2486}
2487
bc8d7dff
DL
2488static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2489{
2490 switch (format) {
2491 case PLANE_CTL_FORMAT_RGB_565:
2492 return DRM_FORMAT_RGB565;
2493 default:
2494 case PLANE_CTL_FORMAT_XRGB_8888:
2495 if (rgb_order) {
2496 if (alpha)
2497 return DRM_FORMAT_ABGR8888;
2498 else
2499 return DRM_FORMAT_XBGR8888;
2500 } else {
2501 if (alpha)
2502 return DRM_FORMAT_ARGB8888;
2503 else
2504 return DRM_FORMAT_XRGB8888;
2505 }
2506 case PLANE_CTL_FORMAT_XRGB_2101010:
2507 if (rgb_order)
2508 return DRM_FORMAT_XBGR2101010;
2509 else
2510 return DRM_FORMAT_XRGB2101010;
2511 }
2512}
2513
5724dbd1 2514static bool
f6936e29
DV
2515intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2516 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2517{
2518 struct drm_device *dev = crtc->base.dev;
2519 struct drm_i915_gem_object *obj = NULL;
2520 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2521 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2522 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2523 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2524 PAGE_SIZE);
2525
2526 size_aligned -= base_aligned;
46f297fb 2527
ff2652ea
CW
2528 if (plane_config->size == 0)
2529 return false;
2530
f37b5c2b
DV
2531 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2532 base_aligned,
2533 base_aligned,
2534 size_aligned);
46f297fb 2535 if (!obj)
484b41dd 2536 return false;
46f297fb 2537
49af449b
DL
2538 obj->tiling_mode = plane_config->tiling;
2539 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2540 obj->stride = fb->pitches[0];
46f297fb 2541
6bf129df
DL
2542 mode_cmd.pixel_format = fb->pixel_format;
2543 mode_cmd.width = fb->width;
2544 mode_cmd.height = fb->height;
2545 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2546 mode_cmd.modifier[0] = fb->modifier[0];
2547 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2548
2549 mutex_lock(&dev->struct_mutex);
6bf129df 2550 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2551 &mode_cmd, obj)) {
46f297fb
JB
2552 DRM_DEBUG_KMS("intel fb init failed\n");
2553 goto out_unref_obj;
2554 }
46f297fb 2555 mutex_unlock(&dev->struct_mutex);
484b41dd 2556
f6936e29 2557 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2558 return true;
46f297fb
JB
2559
2560out_unref_obj:
2561 drm_gem_object_unreference(&obj->base);
2562 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2563 return false;
2564}
2565
afd65eb4
MR
2566/* Update plane->state->fb to match plane->fb after driver-internal updates */
2567static void
2568update_state_fb(struct drm_plane *plane)
2569{
2570 if (plane->fb == plane->state->fb)
2571 return;
2572
2573 if (plane->state->fb)
2574 drm_framebuffer_unreference(plane->state->fb);
2575 plane->state->fb = plane->fb;
2576 if (plane->state->fb)
2577 drm_framebuffer_reference(plane->state->fb);
2578}
2579
5724dbd1 2580static void
f6936e29
DV
2581intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2582 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2583{
2584 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2585 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2586 struct drm_crtc *c;
2587 struct intel_crtc *i;
2ff8fde1 2588 struct drm_i915_gem_object *obj;
88595ac9
DV
2589 struct drm_plane *primary = intel_crtc->base.primary;
2590 struct drm_framebuffer *fb;
484b41dd 2591
2d14030b 2592 if (!plane_config->fb)
484b41dd
JB
2593 return;
2594
f6936e29 2595 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2596 fb = &plane_config->fb->base;
2597 goto valid_fb;
f55548b5 2598 }
484b41dd 2599
2d14030b 2600 kfree(plane_config->fb);
484b41dd
JB
2601
2602 /*
2603 * Failed to alloc the obj, check to see if we should share
2604 * an fb with another CRTC instead
2605 */
70e1e0ec 2606 for_each_crtc(dev, c) {
484b41dd
JB
2607 i = to_intel_crtc(c);
2608
2609 if (c == &intel_crtc->base)
2610 continue;
2611
2ff8fde1
MR
2612 if (!i->active)
2613 continue;
2614
88595ac9
DV
2615 fb = c->primary->fb;
2616 if (!fb)
484b41dd
JB
2617 continue;
2618
88595ac9 2619 obj = intel_fb_obj(fb);
2ff8fde1 2620 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2621 drm_framebuffer_reference(fb);
2622 goto valid_fb;
484b41dd
JB
2623 }
2624 }
88595ac9
DV
2625
2626 return;
2627
2628valid_fb:
2629 obj = intel_fb_obj(fb);
2630 if (obj->tiling_mode != I915_TILING_NONE)
2631 dev_priv->preserve_bios_swizzle = true;
2632
2633 primary->fb = fb;
36750f28 2634 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2635 update_state_fb(primary);
36750f28 2636 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
88595ac9 2637 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2638}
2639
29b9bde6
DV
2640static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2641 struct drm_framebuffer *fb,
2642 int x, int y)
81255565
JB
2643{
2644 struct drm_device *dev = crtc->dev;
2645 struct drm_i915_private *dev_priv = dev->dev_private;
2646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2647 struct drm_plane *primary = crtc->primary;
2648 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2649 struct drm_i915_gem_object *obj;
81255565 2650 int plane = intel_crtc->plane;
e506a0c6 2651 unsigned long linear_offset;
81255565 2652 u32 dspcntr;
f45651ba 2653 u32 reg = DSPCNTR(plane);
48404c1e 2654 int pixel_size;
f45651ba 2655
b70709a6 2656 if (!visible || !fb) {
fdd508a6
VS
2657 I915_WRITE(reg, 0);
2658 if (INTEL_INFO(dev)->gen >= 4)
2659 I915_WRITE(DSPSURF(plane), 0);
2660 else
2661 I915_WRITE(DSPADDR(plane), 0);
2662 POSTING_READ(reg);
2663 return;
2664 }
2665
c9ba6fad
VS
2666 obj = intel_fb_obj(fb);
2667 if (WARN_ON(obj == NULL))
2668 return;
2669
2670 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2671
f45651ba
VS
2672 dspcntr = DISPPLANE_GAMMA_ENABLE;
2673
fdd508a6 2674 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2675
2676 if (INTEL_INFO(dev)->gen < 4) {
2677 if (intel_crtc->pipe == PIPE_B)
2678 dspcntr |= DISPPLANE_SEL_PIPE_B;
2679
2680 /* pipesrc and dspsize control the size that is scaled from,
2681 * which should always be the user's requested size.
2682 */
2683 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2684 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2685 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2686 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2687 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2688 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2689 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2690 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2691 I915_WRITE(PRIMPOS(plane), 0);
2692 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2693 }
81255565 2694
57779d06
VS
2695 switch (fb->pixel_format) {
2696 case DRM_FORMAT_C8:
81255565
JB
2697 dspcntr |= DISPPLANE_8BPP;
2698 break;
57779d06 2699 case DRM_FORMAT_XRGB1555:
57779d06 2700 dspcntr |= DISPPLANE_BGRX555;
81255565 2701 break;
57779d06
VS
2702 case DRM_FORMAT_RGB565:
2703 dspcntr |= DISPPLANE_BGRX565;
2704 break;
2705 case DRM_FORMAT_XRGB8888:
57779d06
VS
2706 dspcntr |= DISPPLANE_BGRX888;
2707 break;
2708 case DRM_FORMAT_XBGR8888:
57779d06
VS
2709 dspcntr |= DISPPLANE_RGBX888;
2710 break;
2711 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2712 dspcntr |= DISPPLANE_BGRX101010;
2713 break;
2714 case DRM_FORMAT_XBGR2101010:
57779d06 2715 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2716 break;
2717 default:
baba133a 2718 BUG();
81255565 2719 }
57779d06 2720
f45651ba
VS
2721 if (INTEL_INFO(dev)->gen >= 4 &&
2722 obj->tiling_mode != I915_TILING_NONE)
2723 dspcntr |= DISPPLANE_TILED;
81255565 2724
de1aa629
VS
2725 if (IS_G4X(dev))
2726 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2727
b9897127 2728 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2729
c2c75131
DV
2730 if (INTEL_INFO(dev)->gen >= 4) {
2731 intel_crtc->dspaddr_offset =
bc752862 2732 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2733 pixel_size,
bc752862 2734 fb->pitches[0]);
c2c75131
DV
2735 linear_offset -= intel_crtc->dspaddr_offset;
2736 } else {
e506a0c6 2737 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2738 }
e506a0c6 2739
8e7d688b 2740 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2741 dspcntr |= DISPPLANE_ROTATE_180;
2742
6e3c9717
ACO
2743 x += (intel_crtc->config->pipe_src_w - 1);
2744 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2745
2746 /* Finding the last pixel of the last line of the display
2747 data and adding to linear_offset*/
2748 linear_offset +=
6e3c9717
ACO
2749 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2750 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2751 }
2752
2753 I915_WRITE(reg, dspcntr);
2754
01f2c773 2755 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2756 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2757 I915_WRITE(DSPSURF(plane),
2758 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2759 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2760 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2761 } else
f343c5f6 2762 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2763 POSTING_READ(reg);
17638cd6
JB
2764}
2765
29b9bde6
DV
2766static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2767 struct drm_framebuffer *fb,
2768 int x, int y)
17638cd6
JB
2769{
2770 struct drm_device *dev = crtc->dev;
2771 struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2773 struct drm_plane *primary = crtc->primary;
2774 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2775 struct drm_i915_gem_object *obj;
17638cd6 2776 int plane = intel_crtc->plane;
e506a0c6 2777 unsigned long linear_offset;
17638cd6 2778 u32 dspcntr;
f45651ba 2779 u32 reg = DSPCNTR(plane);
48404c1e 2780 int pixel_size;
f45651ba 2781
b70709a6 2782 if (!visible || !fb) {
fdd508a6
VS
2783 I915_WRITE(reg, 0);
2784 I915_WRITE(DSPSURF(plane), 0);
2785 POSTING_READ(reg);
2786 return;
2787 }
2788
c9ba6fad
VS
2789 obj = intel_fb_obj(fb);
2790 if (WARN_ON(obj == NULL))
2791 return;
2792
2793 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2794
f45651ba
VS
2795 dspcntr = DISPPLANE_GAMMA_ENABLE;
2796
fdd508a6 2797 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2798
2799 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2800 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2801
57779d06
VS
2802 switch (fb->pixel_format) {
2803 case DRM_FORMAT_C8:
17638cd6
JB
2804 dspcntr |= DISPPLANE_8BPP;
2805 break;
57779d06
VS
2806 case DRM_FORMAT_RGB565:
2807 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2808 break;
57779d06 2809 case DRM_FORMAT_XRGB8888:
57779d06
VS
2810 dspcntr |= DISPPLANE_BGRX888;
2811 break;
2812 case DRM_FORMAT_XBGR8888:
57779d06
VS
2813 dspcntr |= DISPPLANE_RGBX888;
2814 break;
2815 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2816 dspcntr |= DISPPLANE_BGRX101010;
2817 break;
2818 case DRM_FORMAT_XBGR2101010:
57779d06 2819 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2820 break;
2821 default:
baba133a 2822 BUG();
17638cd6
JB
2823 }
2824
2825 if (obj->tiling_mode != I915_TILING_NONE)
2826 dspcntr |= DISPPLANE_TILED;
17638cd6 2827
f45651ba 2828 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2829 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2830
b9897127 2831 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2832 intel_crtc->dspaddr_offset =
bc752862 2833 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2834 pixel_size,
bc752862 2835 fb->pitches[0]);
c2c75131 2836 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2837 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2838 dspcntr |= DISPPLANE_ROTATE_180;
2839
2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2841 x += (intel_crtc->config->pipe_src_w - 1);
2842 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2843
2844 /* Finding the last pixel of the last line of the display
2845 data and adding to linear_offset*/
2846 linear_offset +=
6e3c9717
ACO
2847 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2848 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2849 }
2850 }
2851
2852 I915_WRITE(reg, dspcntr);
17638cd6 2853
01f2c773 2854 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2855 I915_WRITE(DSPSURF(plane),
2856 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2857 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2858 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2859 } else {
2860 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2861 I915_WRITE(DSPLINOFF(plane), linear_offset);
2862 }
17638cd6 2863 POSTING_READ(reg);
17638cd6
JB
2864}
2865
b321803d
DL
2866u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2867 uint32_t pixel_format)
2868{
2869 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2870
2871 /*
2872 * The stride is either expressed as a multiple of 64 bytes
2873 * chunks for linear buffers or in number of tiles for tiled
2874 * buffers.
2875 */
2876 switch (fb_modifier) {
2877 case DRM_FORMAT_MOD_NONE:
2878 return 64;
2879 case I915_FORMAT_MOD_X_TILED:
2880 if (INTEL_INFO(dev)->gen == 2)
2881 return 128;
2882 return 512;
2883 case I915_FORMAT_MOD_Y_TILED:
2884 /* No need to check for old gens and Y tiling since this is
2885 * about the display engine and those will be blocked before
2886 * we get here.
2887 */
2888 return 128;
2889 case I915_FORMAT_MOD_Yf_TILED:
2890 if (bits_per_pixel == 8)
2891 return 64;
2892 else
2893 return 128;
2894 default:
2895 MISSING_CASE(fb_modifier);
2896 return 64;
2897 }
2898}
2899
121920fa
TU
2900unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2901 struct drm_i915_gem_object *obj)
2902{
9abc4648 2903 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2904
2905 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2906 view = &i915_ggtt_view_rotated;
121920fa
TU
2907
2908 return i915_gem_obj_ggtt_offset_view(obj, view);
2909}
2910
a1b2278e
CK
2911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
2914void skl_detach_scalers(struct intel_crtc *intel_crtc)
2915{
2916 struct drm_device *dev;
2917 struct drm_i915_private *dev_priv;
2918 struct intel_crtc_scaler_state *scaler_state;
2919 int i;
2920
2921 if (!intel_crtc || !intel_crtc->config)
2922 return;
2923
2924 dev = intel_crtc->base.dev;
2925 dev_priv = dev->dev_private;
2926 scaler_state = &intel_crtc->config->scaler_state;
2927
2928 /* loop through and disable scalers that aren't in use */
2929 for (i = 0; i < intel_crtc->num_scalers; i++) {
2930 if (!scaler_state->scalers[i].in_use) {
2931 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2932 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2933 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2934 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2935 intel_crtc->base.base.id, intel_crtc->pipe, i);
2936 }
2937 }
2938}
2939
6156a456 2940u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2941{
6156a456 2942 switch (pixel_format) {
d161cf7a 2943 case DRM_FORMAT_C8:
c34ce3d1 2944 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2945 case DRM_FORMAT_RGB565:
c34ce3d1 2946 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2947 case DRM_FORMAT_XBGR8888:
c34ce3d1 2948 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2949 case DRM_FORMAT_XRGB8888:
c34ce3d1 2950 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2951 /*
2952 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2953 * to be already pre-multiplied. We need to add a knob (or a different
2954 * DRM_FORMAT) for user-space to configure that.
2955 */
f75fb42a 2956 case DRM_FORMAT_ABGR8888:
c34ce3d1 2957 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2958 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2959 case DRM_FORMAT_ARGB8888:
c34ce3d1 2960 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2961 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2962 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2963 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2964 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2965 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2966 case DRM_FORMAT_YUYV:
c34ce3d1 2967 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2968 case DRM_FORMAT_YVYU:
c34ce3d1 2969 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2970 case DRM_FORMAT_UYVY:
c34ce3d1 2971 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2972 case DRM_FORMAT_VYUY:
c34ce3d1 2973 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2974 default:
4249eeef 2975 MISSING_CASE(pixel_format);
70d21f0e 2976 }
8cfcba41 2977
c34ce3d1 2978 return 0;
6156a456 2979}
70d21f0e 2980
6156a456
CK
2981u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2982{
6156a456 2983 switch (fb_modifier) {
30af77c4 2984 case DRM_FORMAT_MOD_NONE:
70d21f0e 2985 break;
30af77c4 2986 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2987 return PLANE_CTL_TILED_X;
b321803d 2988 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2989 return PLANE_CTL_TILED_Y;
b321803d 2990 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2991 return PLANE_CTL_TILED_YF;
70d21f0e 2992 default:
6156a456 2993 MISSING_CASE(fb_modifier);
70d21f0e 2994 }
8cfcba41 2995
c34ce3d1 2996 return 0;
6156a456 2997}
70d21f0e 2998
6156a456
CK
2999u32 skl_plane_ctl_rotation(unsigned int rotation)
3000{
3b7a5119 3001 switch (rotation) {
6156a456
CK
3002 case BIT(DRM_ROTATE_0):
3003 break;
1e8df167
SJ
3004 /*
3005 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3006 * while i915 HW rotation is clockwise, thats why this swapping.
3007 */
3b7a5119 3008 case BIT(DRM_ROTATE_90):
1e8df167 3009 return PLANE_CTL_ROTATE_270;
3b7a5119 3010 case BIT(DRM_ROTATE_180):
c34ce3d1 3011 return PLANE_CTL_ROTATE_180;
3b7a5119 3012 case BIT(DRM_ROTATE_270):
1e8df167 3013 return PLANE_CTL_ROTATE_90;
6156a456
CK
3014 default:
3015 MISSING_CASE(rotation);
3016 }
3017
c34ce3d1 3018 return 0;
6156a456
CK
3019}
3020
3021static void skylake_update_primary_plane(struct drm_crtc *crtc,
3022 struct drm_framebuffer *fb,
3023 int x, int y)
3024{
3025 struct drm_device *dev = crtc->dev;
3026 struct drm_i915_private *dev_priv = dev->dev_private;
3027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3028 struct drm_plane *plane = crtc->primary;
3029 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3030 struct drm_i915_gem_object *obj;
3031 int pipe = intel_crtc->pipe;
3032 u32 plane_ctl, stride_div, stride;
3033 u32 tile_height, plane_offset, plane_size;
3034 unsigned int rotation;
3035 int x_offset, y_offset;
3036 unsigned long surf_addr;
6156a456
CK
3037 struct intel_crtc_state *crtc_state = intel_crtc->config;
3038 struct intel_plane_state *plane_state;
3039 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3040 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3041 int scaler_id = -1;
3042
6156a456
CK
3043 plane_state = to_intel_plane_state(plane->state);
3044
b70709a6 3045 if (!visible || !fb) {
6156a456
CK
3046 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3047 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3048 POSTING_READ(PLANE_CTL(pipe, 0));
3049 return;
3b7a5119 3050 }
70d21f0e 3051
6156a456
CK
3052 plane_ctl = PLANE_CTL_ENABLE |
3053 PLANE_CTL_PIPE_GAMMA_ENABLE |
3054 PLANE_CTL_PIPE_CSC_ENABLE;
3055
3056 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3057 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3058 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3059
3060 rotation = plane->state->rotation;
3061 plane_ctl |= skl_plane_ctl_rotation(rotation);
3062
b321803d
DL
3063 obj = intel_fb_obj(fb);
3064 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3065 fb->pixel_format);
3b7a5119
SJ
3066 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3067
6156a456
CK
3068 /*
3069 * FIXME: intel_plane_state->src, dst aren't set when transitional
3070 * update_plane helpers are called from legacy paths.
3071 * Once full atomic crtc is available, below check can be avoided.
3072 */
3073 if (drm_rect_width(&plane_state->src)) {
3074 scaler_id = plane_state->scaler_id;
3075 src_x = plane_state->src.x1 >> 16;
3076 src_y = plane_state->src.y1 >> 16;
3077 src_w = drm_rect_width(&plane_state->src) >> 16;
3078 src_h = drm_rect_height(&plane_state->src) >> 16;
3079 dst_x = plane_state->dst.x1;
3080 dst_y = plane_state->dst.y1;
3081 dst_w = drm_rect_width(&plane_state->dst);
3082 dst_h = drm_rect_height(&plane_state->dst);
3083
3084 WARN_ON(x != src_x || y != src_y);
3085 } else {
3086 src_w = intel_crtc->config->pipe_src_w;
3087 src_h = intel_crtc->config->pipe_src_h;
3088 }
3089
3b7a5119
SJ
3090 if (intel_rotation_90_or_270(rotation)) {
3091 /* stride = Surface height in tiles */
2614f17d 3092 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3093 fb->modifier[0]);
3094 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3095 x_offset = stride * tile_height - y - src_h;
3b7a5119 3096 y_offset = x;
6156a456 3097 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3098 } else {
3099 stride = fb->pitches[0] / stride_div;
3100 x_offset = x;
3101 y_offset = y;
6156a456 3102 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3103 }
3104 plane_offset = y_offset << 16 | x_offset;
b321803d 3105
70d21f0e 3106 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3107 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3108 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3109 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3110
3111 if (scaler_id >= 0) {
3112 uint32_t ps_ctrl = 0;
3113
3114 WARN_ON(!dst_w || !dst_h);
3115 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3116 crtc_state->scaler_state.scalers[scaler_id].mode;
3117 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3118 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3119 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3120 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3121 I915_WRITE(PLANE_POS(pipe, 0), 0);
3122 } else {
3123 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3124 }
3125
121920fa 3126 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3127
3128 POSTING_READ(PLANE_SURF(pipe, 0));
3129}
3130
17638cd6
JB
3131/* Assume fb object is pinned & idle & fenced and just update base pointers */
3132static int
3133intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3134 int x, int y, enum mode_set_atomic state)
3135{
3136 struct drm_device *dev = crtc->dev;
3137 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3138
6b8e6ed0
CW
3139 if (dev_priv->display.disable_fbc)
3140 dev_priv->display.disable_fbc(dev);
81255565 3141
29b9bde6
DV
3142 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3143
3144 return 0;
81255565
JB
3145}
3146
7514747d 3147static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3148{
96a02917
VS
3149 struct drm_crtc *crtc;
3150
70e1e0ec 3151 for_each_crtc(dev, crtc) {
96a02917
VS
3152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 enum plane plane = intel_crtc->plane;
3154
3155 intel_prepare_page_flip(dev, plane);
3156 intel_finish_page_flip_plane(dev, plane);
3157 }
7514747d
VS
3158}
3159
3160static void intel_update_primary_planes(struct drm_device *dev)
3161{
3162 struct drm_i915_private *dev_priv = dev->dev_private;
3163 struct drm_crtc *crtc;
96a02917 3164
70e1e0ec 3165 for_each_crtc(dev, crtc) {
96a02917
VS
3166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167
51fd371b 3168 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3169 /*
3170 * FIXME: Once we have proper support for primary planes (and
3171 * disabling them without disabling the entire crtc) allow again
66e514c1 3172 * a NULL crtc->primary->fb.
947fdaad 3173 */
f4510a27 3174 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3175 dev_priv->display.update_primary_plane(crtc,
66e514c1 3176 crtc->primary->fb,
262ca2b0
MR
3177 crtc->x,
3178 crtc->y);
51fd371b 3179 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3180 }
3181}
3182
7514747d
VS
3183void intel_prepare_reset(struct drm_device *dev)
3184{
3185 /* no reset support for gen2 */
3186 if (IS_GEN2(dev))
3187 return;
3188
3189 /* reset doesn't touch the display */
3190 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3191 return;
3192
3193 drm_modeset_lock_all(dev);
f98ce92f
VS
3194 /*
3195 * Disabling the crtcs gracefully seems nicer. Also the
3196 * g33 docs say we should at least disable all the planes.
3197 */
6b72d486 3198 intel_display_suspend(dev);
7514747d
VS
3199}
3200
3201void intel_finish_reset(struct drm_device *dev)
3202{
3203 struct drm_i915_private *dev_priv = to_i915(dev);
3204
3205 /*
3206 * Flips in the rings will be nuked by the reset,
3207 * so complete all pending flips so that user space
3208 * will get its events and not get stuck.
3209 */
3210 intel_complete_page_flips(dev);
3211
3212 /* no reset support for gen2 */
3213 if (IS_GEN2(dev))
3214 return;
3215
3216 /* reset doesn't touch the display */
3217 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3218 /*
3219 * Flips in the rings have been nuked by the reset,
3220 * so update the base address of all primary
3221 * planes to the the last fb to make sure we're
3222 * showing the correct fb after a reset.
3223 */
3224 intel_update_primary_planes(dev);
3225 return;
3226 }
3227
3228 /*
3229 * The display has been reset as well,
3230 * so need a full re-initialization.
3231 */
3232 intel_runtime_pm_disable_interrupts(dev_priv);
3233 intel_runtime_pm_enable_interrupts(dev_priv);
3234
3235 intel_modeset_init_hw(dev);
3236
3237 spin_lock_irq(&dev_priv->irq_lock);
3238 if (dev_priv->display.hpd_irq_setup)
3239 dev_priv->display.hpd_irq_setup(dev);
3240 spin_unlock_irq(&dev_priv->irq_lock);
3241
3242 intel_modeset_setup_hw_state(dev, true);
3243
3244 intel_hpd_init(dev_priv);
3245
3246 drm_modeset_unlock_all(dev);
3247}
3248
2e2f351d 3249static void
14667a4b
CW
3250intel_finish_fb(struct drm_framebuffer *old_fb)
3251{
2ff8fde1 3252 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3253 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3254 bool was_interruptible = dev_priv->mm.interruptible;
3255 int ret;
3256
14667a4b
CW
3257 /* Big Hammer, we also need to ensure that any pending
3258 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3259 * current scanout is retired before unpinning the old
2e2f351d
CW
3260 * framebuffer. Note that we rely on userspace rendering
3261 * into the buffer attached to the pipe they are waiting
3262 * on. If not, userspace generates a GPU hang with IPEHR
3263 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3264 *
3265 * This should only fail upon a hung GPU, in which case we
3266 * can safely continue.
3267 */
3268 dev_priv->mm.interruptible = false;
2e2f351d 3269 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3270 dev_priv->mm.interruptible = was_interruptible;
3271
2e2f351d 3272 WARN_ON(ret);
14667a4b
CW
3273}
3274
7d5e3799
CW
3275static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3280 bool pending;
3281
3282 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3283 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3284 return false;
3285
5e2d7afc 3286 spin_lock_irq(&dev->event_lock);
7d5e3799 3287 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3288 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3289
3290 return pending;
3291}
3292
e30e8f75
GP
3293static void intel_update_pipe_size(struct intel_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->base.dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 const struct drm_display_mode *adjusted_mode;
3298
3299 if (!i915.fastboot)
3300 return;
3301
3302 /*
3303 * Update pipe size and adjust fitter if needed: the reason for this is
3304 * that in compute_mode_changes we check the native mode (not the pfit
3305 * mode) to see if we can flip rather than do a full mode set. In the
3306 * fastboot case, we'll flip, but if we don't update the pipesrc and
3307 * pfit state, we'll end up with a big fb scanned out into the wrong
3308 * sized surface.
3309 *
3310 * To fix this properly, we need to hoist the checks up into
3311 * compute_mode_changes (or above), check the actual pfit state and
3312 * whether the platform allows pfit disable with pipe active, and only
3313 * then update the pipesrc and pfit state, even on the flip path.
3314 */
3315
6e3c9717 3316 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3317
3318 I915_WRITE(PIPESRC(crtc->pipe),
3319 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3320 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3321 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3322 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3323 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3324 I915_WRITE(PF_CTL(crtc->pipe), 0);
3325 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3326 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3327 }
6e3c9717
ACO
3328 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3329 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3330}
3331
5e84e1a4
ZW
3332static void intel_fdi_normal_train(struct drm_crtc *crtc)
3333{
3334 struct drm_device *dev = crtc->dev;
3335 struct drm_i915_private *dev_priv = dev->dev_private;
3336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3337 int pipe = intel_crtc->pipe;
3338 u32 reg, temp;
3339
3340 /* enable normal train */
3341 reg = FDI_TX_CTL(pipe);
3342 temp = I915_READ(reg);
61e499bf 3343 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3344 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3345 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3346 } else {
3347 temp &= ~FDI_LINK_TRAIN_NONE;
3348 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3349 }
5e84e1a4
ZW
3350 I915_WRITE(reg, temp);
3351
3352 reg = FDI_RX_CTL(pipe);
3353 temp = I915_READ(reg);
3354 if (HAS_PCH_CPT(dev)) {
3355 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3356 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3357 } else {
3358 temp &= ~FDI_LINK_TRAIN_NONE;
3359 temp |= FDI_LINK_TRAIN_NONE;
3360 }
3361 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3362
3363 /* wait one idle pattern time */
3364 POSTING_READ(reg);
3365 udelay(1000);
357555c0
JB
3366
3367 /* IVB wants error correction enabled */
3368 if (IS_IVYBRIDGE(dev))
3369 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3370 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3371}
3372
8db9d77b
ZW
3373/* The FDI link training functions for ILK/Ibexpeak. */
3374static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3375{
3376 struct drm_device *dev = crtc->dev;
3377 struct drm_i915_private *dev_priv = dev->dev_private;
3378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3379 int pipe = intel_crtc->pipe;
5eddb70b 3380 u32 reg, temp, tries;
8db9d77b 3381
1c8562f6 3382 /* FDI needs bits from pipe first */
0fc932b8 3383 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3384
e1a44743
AJ
3385 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3386 for train result */
5eddb70b
CW
3387 reg = FDI_RX_IMR(pipe);
3388 temp = I915_READ(reg);
e1a44743
AJ
3389 temp &= ~FDI_RX_SYMBOL_LOCK;
3390 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3391 I915_WRITE(reg, temp);
3392 I915_READ(reg);
e1a44743
AJ
3393 udelay(150);
3394
8db9d77b 3395 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3396 reg = FDI_TX_CTL(pipe);
3397 temp = I915_READ(reg);
627eb5a3 3398 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3399 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3400 temp &= ~FDI_LINK_TRAIN_NONE;
3401 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3402 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3403
5eddb70b
CW
3404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
8db9d77b
ZW
3406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3408 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3409
3410 POSTING_READ(reg);
8db9d77b
ZW
3411 udelay(150);
3412
5b2adf89 3413 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3414 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3415 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3416 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3417
5eddb70b 3418 reg = FDI_RX_IIR(pipe);
e1a44743 3419 for (tries = 0; tries < 5; tries++) {
5eddb70b 3420 temp = I915_READ(reg);
8db9d77b
ZW
3421 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3422
3423 if ((temp & FDI_RX_BIT_LOCK)) {
3424 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3425 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3426 break;
3427 }
8db9d77b 3428 }
e1a44743 3429 if (tries == 5)
5eddb70b 3430 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3431
3432 /* Train 2 */
5eddb70b
CW
3433 reg = FDI_TX_CTL(pipe);
3434 temp = I915_READ(reg);
8db9d77b
ZW
3435 temp &= ~FDI_LINK_TRAIN_NONE;
3436 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3437 I915_WRITE(reg, temp);
8db9d77b 3438
5eddb70b
CW
3439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
8db9d77b
ZW
3441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3443 I915_WRITE(reg, temp);
8db9d77b 3444
5eddb70b
CW
3445 POSTING_READ(reg);
3446 udelay(150);
8db9d77b 3447
5eddb70b 3448 reg = FDI_RX_IIR(pipe);
e1a44743 3449 for (tries = 0; tries < 5; tries++) {
5eddb70b 3450 temp = I915_READ(reg);
8db9d77b
ZW
3451 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3452
3453 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3454 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3455 DRM_DEBUG_KMS("FDI train 2 done.\n");
3456 break;
3457 }
8db9d77b 3458 }
e1a44743 3459 if (tries == 5)
5eddb70b 3460 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3461
3462 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3463
8db9d77b
ZW
3464}
3465
0206e353 3466static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3467 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3468 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3469 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3470 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3471};
3472
3473/* The FDI link training functions for SNB/Cougarpoint. */
3474static void gen6_fdi_link_train(struct drm_crtc *crtc)
3475{
3476 struct drm_device *dev = crtc->dev;
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3479 int pipe = intel_crtc->pipe;
fa37d39e 3480 u32 reg, temp, i, retry;
8db9d77b 3481
e1a44743
AJ
3482 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3483 for train result */
5eddb70b
CW
3484 reg = FDI_RX_IMR(pipe);
3485 temp = I915_READ(reg);
e1a44743
AJ
3486 temp &= ~FDI_RX_SYMBOL_LOCK;
3487 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
e1a44743
AJ
3491 udelay(150);
3492
8db9d77b 3493 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
627eb5a3 3496 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3497 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1;
3500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3501 /* SNB-B */
3502 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3503 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3504
d74cf324
DV
3505 I915_WRITE(FDI_RX_MISC(pipe),
3506 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3507
5eddb70b
CW
3508 reg = FDI_RX_CTL(pipe);
3509 temp = I915_READ(reg);
8db9d77b
ZW
3510 if (HAS_PCH_CPT(dev)) {
3511 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3513 } else {
3514 temp &= ~FDI_LINK_TRAIN_NONE;
3515 temp |= FDI_LINK_TRAIN_PATTERN_1;
3516 }
5eddb70b
CW
3517 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3518
3519 POSTING_READ(reg);
8db9d77b
ZW
3520 udelay(150);
3521
0206e353 3522 for (i = 0; i < 4; i++) {
5eddb70b
CW
3523 reg = FDI_TX_CTL(pipe);
3524 temp = I915_READ(reg);
8db9d77b
ZW
3525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3527 I915_WRITE(reg, temp);
3528
3529 POSTING_READ(reg);
8db9d77b
ZW
3530 udelay(500);
3531
fa37d39e
SP
3532 for (retry = 0; retry < 5; retry++) {
3533 reg = FDI_RX_IIR(pipe);
3534 temp = I915_READ(reg);
3535 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3536 if (temp & FDI_RX_BIT_LOCK) {
3537 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3538 DRM_DEBUG_KMS("FDI train 1 done.\n");
3539 break;
3540 }
3541 udelay(50);
8db9d77b 3542 }
fa37d39e
SP
3543 if (retry < 5)
3544 break;
8db9d77b
ZW
3545 }
3546 if (i == 4)
5eddb70b 3547 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3548
3549 /* Train 2 */
5eddb70b
CW
3550 reg = FDI_TX_CTL(pipe);
3551 temp = I915_READ(reg);
8db9d77b
ZW
3552 temp &= ~FDI_LINK_TRAIN_NONE;
3553 temp |= FDI_LINK_TRAIN_PATTERN_2;
3554 if (IS_GEN6(dev)) {
3555 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3556 /* SNB-B */
3557 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3558 }
5eddb70b 3559 I915_WRITE(reg, temp);
8db9d77b 3560
5eddb70b
CW
3561 reg = FDI_RX_CTL(pipe);
3562 temp = I915_READ(reg);
8db9d77b
ZW
3563 if (HAS_PCH_CPT(dev)) {
3564 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3565 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3566 } else {
3567 temp &= ~FDI_LINK_TRAIN_NONE;
3568 temp |= FDI_LINK_TRAIN_PATTERN_2;
3569 }
5eddb70b
CW
3570 I915_WRITE(reg, temp);
3571
3572 POSTING_READ(reg);
8db9d77b
ZW
3573 udelay(150);
3574
0206e353 3575 for (i = 0; i < 4; i++) {
5eddb70b
CW
3576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
8db9d77b
ZW
3578 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3579 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3580 I915_WRITE(reg, temp);
3581
3582 POSTING_READ(reg);
8db9d77b
ZW
3583 udelay(500);
3584
fa37d39e
SP
3585 for (retry = 0; retry < 5; retry++) {
3586 reg = FDI_RX_IIR(pipe);
3587 temp = I915_READ(reg);
3588 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3589 if (temp & FDI_RX_SYMBOL_LOCK) {
3590 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3591 DRM_DEBUG_KMS("FDI train 2 done.\n");
3592 break;
3593 }
3594 udelay(50);
8db9d77b 3595 }
fa37d39e
SP
3596 if (retry < 5)
3597 break;
8db9d77b
ZW
3598 }
3599 if (i == 4)
5eddb70b 3600 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3601
3602 DRM_DEBUG_KMS("FDI train done.\n");
3603}
3604
357555c0
JB
3605/* Manual link training for Ivy Bridge A0 parts */
3606static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3607{
3608 struct drm_device *dev = crtc->dev;
3609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3611 int pipe = intel_crtc->pipe;
139ccd3f 3612 u32 reg, temp, i, j;
357555c0
JB
3613
3614 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3615 for train result */
3616 reg = FDI_RX_IMR(pipe);
3617 temp = I915_READ(reg);
3618 temp &= ~FDI_RX_SYMBOL_LOCK;
3619 temp &= ~FDI_RX_BIT_LOCK;
3620 I915_WRITE(reg, temp);
3621
3622 POSTING_READ(reg);
3623 udelay(150);
3624
01a415fd
DV
3625 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3626 I915_READ(FDI_RX_IIR(pipe)));
3627
139ccd3f
JB
3628 /* Try each vswing and preemphasis setting twice before moving on */
3629 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3630 /* disable first in case we need to retry */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3634 temp &= ~FDI_TX_ENABLE;
3635 I915_WRITE(reg, temp);
357555c0 3636
139ccd3f
JB
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_AUTO;
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp &= ~FDI_RX_ENABLE;
3642 I915_WRITE(reg, temp);
357555c0 3643
139ccd3f 3644 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3645 reg = FDI_TX_CTL(pipe);
3646 temp = I915_READ(reg);
139ccd3f 3647 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3648 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3649 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3650 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3651 temp |= snb_b_fdi_train_param[j/2];
3652 temp |= FDI_COMPOSITE_SYNC;
3653 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3654
139ccd3f
JB
3655 I915_WRITE(FDI_RX_MISC(pipe),
3656 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3657
139ccd3f 3658 reg = FDI_RX_CTL(pipe);
357555c0 3659 temp = I915_READ(reg);
139ccd3f
JB
3660 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3661 temp |= FDI_COMPOSITE_SYNC;
3662 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3663
139ccd3f
JB
3664 POSTING_READ(reg);
3665 udelay(1); /* should be 0.5us */
357555c0 3666
139ccd3f
JB
3667 for (i = 0; i < 4; i++) {
3668 reg = FDI_RX_IIR(pipe);
3669 temp = I915_READ(reg);
3670 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3671
139ccd3f
JB
3672 if (temp & FDI_RX_BIT_LOCK ||
3673 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3674 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3675 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3676 i);
3677 break;
3678 }
3679 udelay(1); /* should be 0.5us */
3680 }
3681 if (i == 4) {
3682 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3683 continue;
3684 }
357555c0 3685
139ccd3f 3686 /* Train 2 */
357555c0
JB
3687 reg = FDI_TX_CTL(pipe);
3688 temp = I915_READ(reg);
139ccd3f
JB
3689 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3690 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3691 I915_WRITE(reg, temp);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3696 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3697 I915_WRITE(reg, temp);
3698
3699 POSTING_READ(reg);
139ccd3f 3700 udelay(2); /* should be 1.5us */
357555c0 3701
139ccd3f
JB
3702 for (i = 0; i < 4; i++) {
3703 reg = FDI_RX_IIR(pipe);
3704 temp = I915_READ(reg);
3705 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3706
139ccd3f
JB
3707 if (temp & FDI_RX_SYMBOL_LOCK ||
3708 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3709 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3710 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3711 i);
3712 goto train_done;
3713 }
3714 udelay(2); /* should be 1.5us */
357555c0 3715 }
139ccd3f
JB
3716 if (i == 4)
3717 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3718 }
357555c0 3719
139ccd3f 3720train_done:
357555c0
JB
3721 DRM_DEBUG_KMS("FDI train done.\n");
3722}
3723
88cefb6c 3724static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3725{
88cefb6c 3726 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3727 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3728 int pipe = intel_crtc->pipe;
5eddb70b 3729 u32 reg, temp;
79e53945 3730
c64e311e 3731
c98e9dcf 3732 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
627eb5a3 3735 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3736 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3737 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3738 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3739
3740 POSTING_READ(reg);
c98e9dcf
JB
3741 udelay(200);
3742
3743 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3744 temp = I915_READ(reg);
3745 I915_WRITE(reg, temp | FDI_PCDCLK);
3746
3747 POSTING_READ(reg);
c98e9dcf
JB
3748 udelay(200);
3749
20749730
PZ
3750 /* Enable CPU FDI TX PLL, always on for Ironlake */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3754 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3755
20749730
PZ
3756 POSTING_READ(reg);
3757 udelay(100);
6be4a607 3758 }
0e23b99d
JB
3759}
3760
88cefb6c
DV
3761static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3762{
3763 struct drm_device *dev = intel_crtc->base.dev;
3764 struct drm_i915_private *dev_priv = dev->dev_private;
3765 int pipe = intel_crtc->pipe;
3766 u32 reg, temp;
3767
3768 /* Switch from PCDclk to Rawclk */
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3772
3773 /* Disable CPU FDI TX PLL */
3774 reg = FDI_TX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3777
3778 POSTING_READ(reg);
3779 udelay(100);
3780
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3784
3785 /* Wait for the clocks to turn off. */
3786 POSTING_READ(reg);
3787 udelay(100);
3788}
3789
0fc932b8
JB
3790static void ironlake_fdi_disable(struct drm_crtc *crtc)
3791{
3792 struct drm_device *dev = crtc->dev;
3793 struct drm_i915_private *dev_priv = dev->dev_private;
3794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3795 int pipe = intel_crtc->pipe;
3796 u32 reg, temp;
3797
3798 /* disable CPU FDI tx and PCH FDI rx */
3799 reg = FDI_TX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3802 POSTING_READ(reg);
3803
3804 reg = FDI_RX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~(0x7 << 16);
dfd07d72 3807 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3808 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812
3813 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3814 if (HAS_PCH_IBX(dev))
6f06ce18 3815 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3816
3817 /* still set train pattern 1 */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 temp &= ~FDI_LINK_TRAIN_NONE;
3821 temp |= FDI_LINK_TRAIN_PATTERN_1;
3822 I915_WRITE(reg, temp);
3823
3824 reg = FDI_RX_CTL(pipe);
3825 temp = I915_READ(reg);
3826 if (HAS_PCH_CPT(dev)) {
3827 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3828 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3829 } else {
3830 temp &= ~FDI_LINK_TRAIN_NONE;
3831 temp |= FDI_LINK_TRAIN_PATTERN_1;
3832 }
3833 /* BPC in FDI rx is consistent with that in PIPECONF */
3834 temp &= ~(0x07 << 16);
dfd07d72 3835 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3836 I915_WRITE(reg, temp);
3837
3838 POSTING_READ(reg);
3839 udelay(100);
3840}
3841
5dce5b93
CW
3842bool intel_has_pending_fb_unpin(struct drm_device *dev)
3843{
3844 struct intel_crtc *crtc;
3845
3846 /* Note that we don't need to be called with mode_config.lock here
3847 * as our list of CRTC objects is static for the lifetime of the
3848 * device and so cannot disappear as we iterate. Similarly, we can
3849 * happily treat the predicates as racy, atomic checks as userspace
3850 * cannot claim and pin a new fb without at least acquring the
3851 * struct_mutex and so serialising with us.
3852 */
d3fcc808 3853 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3854 if (atomic_read(&crtc->unpin_work_count) == 0)
3855 continue;
3856
3857 if (crtc->unpin_work)
3858 intel_wait_for_vblank(dev, crtc->pipe);
3859
3860 return true;
3861 }
3862
3863 return false;
3864}
3865
d6bbafa1
CW
3866static void page_flip_completed(struct intel_crtc *intel_crtc)
3867{
3868 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3869 struct intel_unpin_work *work = intel_crtc->unpin_work;
3870
3871 /* ensure that the unpin work is consistent wrt ->pending. */
3872 smp_rmb();
3873 intel_crtc->unpin_work = NULL;
3874
3875 if (work->event)
3876 drm_send_vblank_event(intel_crtc->base.dev,
3877 intel_crtc->pipe,
3878 work->event);
3879
3880 drm_crtc_vblank_put(&intel_crtc->base);
3881
3882 wake_up_all(&dev_priv->pending_flip_queue);
3883 queue_work(dev_priv->wq, &work->work);
3884
3885 trace_i915_flip_complete(intel_crtc->plane,
3886 work->pending_flip_obj);
3887}
3888
46a55d30 3889void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3890{
0f91128d 3891 struct drm_device *dev = crtc->dev;
5bb61643 3892 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3893
2c10d571 3894 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3895 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3896 !intel_crtc_has_pending_flip(crtc),
3897 60*HZ) == 0)) {
3898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3899
5e2d7afc 3900 spin_lock_irq(&dev->event_lock);
9c787942
CW
3901 if (intel_crtc->unpin_work) {
3902 WARN_ONCE(1, "Removing stuck page flip\n");
3903 page_flip_completed(intel_crtc);
3904 }
5e2d7afc 3905 spin_unlock_irq(&dev->event_lock);
9c787942 3906 }
5bb61643 3907
975d568a
CW
3908 if (crtc->primary->fb) {
3909 mutex_lock(&dev->struct_mutex);
3910 intel_finish_fb(crtc->primary->fb);
3911 mutex_unlock(&dev->struct_mutex);
3912 }
e6c3a2a6
CW
3913}
3914
e615efe4
ED
3915/* Program iCLKIP clock to the desired frequency */
3916static void lpt_program_iclkip(struct drm_crtc *crtc)
3917{
3918 struct drm_device *dev = crtc->dev;
3919 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3920 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3921 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3922 u32 temp;
3923
a580516d 3924 mutex_lock(&dev_priv->sb_lock);
09153000 3925
e615efe4
ED
3926 /* It is necessary to ungate the pixclk gate prior to programming
3927 * the divisors, and gate it back when it is done.
3928 */
3929 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3930
3931 /* Disable SSCCTL */
3932 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3933 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3934 SBI_SSCCTL_DISABLE,
3935 SBI_ICLK);
e615efe4
ED
3936
3937 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3938 if (clock == 20000) {
e615efe4
ED
3939 auxdiv = 1;
3940 divsel = 0x41;
3941 phaseinc = 0x20;
3942 } else {
3943 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3944 * but the adjusted_mode->crtc_clock in in KHz. To get the
3945 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3946 * convert the virtual clock precision to KHz here for higher
3947 * precision.
3948 */
3949 u32 iclk_virtual_root_freq = 172800 * 1000;
3950 u32 iclk_pi_range = 64;
3951 u32 desired_divisor, msb_divisor_value, pi_value;
3952
12d7ceed 3953 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3954 msb_divisor_value = desired_divisor / iclk_pi_range;
3955 pi_value = desired_divisor % iclk_pi_range;
3956
3957 auxdiv = 0;
3958 divsel = msb_divisor_value - 2;
3959 phaseinc = pi_value;
3960 }
3961
3962 /* This should not happen with any sane values */
3963 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3964 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3965 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3966 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3967
3968 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3969 clock,
e615efe4
ED
3970 auxdiv,
3971 divsel,
3972 phasedir,
3973 phaseinc);
3974
3975 /* Program SSCDIVINTPHASE6 */
988d6ee8 3976 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3977 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3978 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3979 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3980 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3981 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3982 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3983 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3984
3985 /* Program SSCAUXDIV */
988d6ee8 3986 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3987 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3988 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3989 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3990
3991 /* Enable modulator and associated divider */
988d6ee8 3992 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3993 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3994 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3995
3996 /* Wait for initialization time */
3997 udelay(24);
3998
3999 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 4000
a580516d 4001 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
4002}
4003
275f01b2
DV
4004static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4005 enum pipe pch_transcoder)
4006{
4007 struct drm_device *dev = crtc->base.dev;
4008 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4009 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4010
4011 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4012 I915_READ(HTOTAL(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4014 I915_READ(HBLANK(cpu_transcoder)));
4015 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4016 I915_READ(HSYNC(cpu_transcoder)));
4017
4018 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4019 I915_READ(VTOTAL(cpu_transcoder)));
4020 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4021 I915_READ(VBLANK(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4023 I915_READ(VSYNC(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4025 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4026}
4027
003632d9 4028static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 uint32_t temp;
4032
4033 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4034 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4035 return;
4036
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4039
003632d9
ACO
4040 temp &= ~FDI_BC_BIFURCATION_SELECT;
4041 if (enable)
4042 temp |= FDI_BC_BIFURCATION_SELECT;
4043
4044 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4045 I915_WRITE(SOUTH_CHICKEN1, temp);
4046 POSTING_READ(SOUTH_CHICKEN1);
4047}
4048
4049static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4050{
4051 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4052
4053 switch (intel_crtc->pipe) {
4054 case PIPE_A:
4055 break;
4056 case PIPE_B:
6e3c9717 4057 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4058 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4059 else
003632d9 4060 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4061
4062 break;
4063 case PIPE_C:
003632d9 4064 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4065
4066 break;
4067 default:
4068 BUG();
4069 }
4070}
4071
f67a559d
JB
4072/*
4073 * Enable PCH resources required for PCH ports:
4074 * - PCH PLLs
4075 * - FDI training & RX/TX
4076 * - update transcoder timings
4077 * - DP transcoding bits
4078 * - transcoder
4079 */
4080static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4081{
4082 struct drm_device *dev = crtc->dev;
4083 struct drm_i915_private *dev_priv = dev->dev_private;
4084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4085 int pipe = intel_crtc->pipe;
ee7b9f93 4086 u32 reg, temp;
2c07245f 4087
ab9412ba 4088 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4089
1fbc0d78
DV
4090 if (IS_IVYBRIDGE(dev))
4091 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4092
cd986abb
DV
4093 /* Write the TU size bits before fdi link training, so that error
4094 * detection works. */
4095 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4096 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4097
c98e9dcf 4098 /* For PCH output, training FDI link */
674cf967 4099 dev_priv->display.fdi_link_train(crtc);
2c07245f 4100
3ad8a208
DV
4101 /* We need to program the right clock selection before writing the pixel
4102 * mutliplier into the DPLL. */
303b81e0 4103 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4104 u32 sel;
4b645f14 4105
c98e9dcf 4106 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4107 temp |= TRANS_DPLL_ENABLE(pipe);
4108 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4109 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4110 temp |= sel;
4111 else
4112 temp &= ~sel;
c98e9dcf 4113 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4114 }
5eddb70b 4115
3ad8a208
DV
4116 /* XXX: pch pll's can be enabled any time before we enable the PCH
4117 * transcoder, and we actually should do this to not upset any PCH
4118 * transcoder that already use the clock when we share it.
4119 *
4120 * Note that enable_shared_dpll tries to do the right thing, but
4121 * get_shared_dpll unconditionally resets the pll - we need that to have
4122 * the right LVDS enable sequence. */
85b3894f 4123 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4124
d9b6cb56
JB
4125 /* set transcoder timing, panel must allow it */
4126 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4127 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4128
303b81e0 4129 intel_fdi_normal_train(crtc);
5e84e1a4 4130
c98e9dcf 4131 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4132 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4134 reg = TRANS_DP_CTL(pipe);
4135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
e3ef4479 4139 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4140 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4141
4142 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4144 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4146
4147 switch (intel_trans_dp_port_sel(crtc)) {
4148 case PCH_DP_B:
5eddb70b 4149 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4150 break;
4151 case PCH_DP_C:
5eddb70b 4152 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4153 break;
4154 case PCH_DP_D:
5eddb70b 4155 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4156 break;
4157 default:
e95d41e1 4158 BUG();
32f9d658 4159 }
2c07245f 4160
5eddb70b 4161 I915_WRITE(reg, temp);
6be4a607 4162 }
b52eb4dc 4163
b8a4f404 4164 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4165}
4166
1507e5bd
PZ
4167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4173
ab9412ba 4174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4175
8c52b5e8 4176 lpt_program_iclkip(crtc);
1507e5bd 4177
0540e488 4178 /* Set transcoder timing. */
275f01b2 4179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4180
937bb610 4181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4182}
4183
190f68c5
ACO
4184struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4185 struct intel_crtc_state *crtc_state)
ee7b9f93 4186{
e2b78267 4187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4188 struct intel_shared_dpll *pll;
de419ab6 4189 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4190 enum intel_dpll_id i;
ee7b9f93 4191
de419ab6
ML
4192 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4193
98b6bd99
DV
4194 if (HAS_PCH_IBX(dev_priv->dev)) {
4195 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4196 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4197 pll = &dev_priv->shared_dplls[i];
98b6bd99 4198
46edb027
DV
4199 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4200 crtc->base.base.id, pll->name);
98b6bd99 4201
de419ab6 4202 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4203
98b6bd99
DV
4204 goto found;
4205 }
4206
bcddf610
S
4207 if (IS_BROXTON(dev_priv->dev)) {
4208 /* PLL is attached to port in bxt */
4209 struct intel_encoder *encoder;
4210 struct intel_digital_port *intel_dig_port;
4211
4212 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4213 if (WARN_ON(!encoder))
4214 return NULL;
4215
4216 intel_dig_port = enc_to_dig_port(&encoder->base);
4217 /* 1:1 mapping between ports and PLLs */
4218 i = (enum intel_dpll_id)intel_dig_port->port;
4219 pll = &dev_priv->shared_dplls[i];
4220 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4221 crtc->base.base.id, pll->name);
de419ab6 4222 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4223
4224 goto found;
4225 }
4226
e72f9fbf
DV
4227 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4228 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4229
4230 /* Only want to check enabled timings first */
de419ab6 4231 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4232 continue;
4233
190f68c5 4234 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4235 &shared_dpll[i].hw_state,
4236 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4237 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4238 crtc->base.base.id, pll->name,
de419ab6 4239 shared_dpll[i].crtc_mask,
8bd31e67 4240 pll->active);
ee7b9f93
JB
4241 goto found;
4242 }
4243 }
4244
4245 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4246 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4247 pll = &dev_priv->shared_dplls[i];
de419ab6 4248 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4249 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4250 crtc->base.base.id, pll->name);
ee7b9f93
JB
4251 goto found;
4252 }
4253 }
4254
4255 return NULL;
4256
4257found:
de419ab6
ML
4258 if (shared_dpll[i].crtc_mask == 0)
4259 shared_dpll[i].hw_state =
4260 crtc_state->dpll_hw_state;
f2a69f44 4261
190f68c5 4262 crtc_state->shared_dpll = i;
46edb027
DV
4263 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4264 pipe_name(crtc->pipe));
ee7b9f93 4265
de419ab6 4266 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4267
ee7b9f93
JB
4268 return pll;
4269}
4270
de419ab6 4271static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4272{
de419ab6
ML
4273 struct drm_i915_private *dev_priv = to_i915(state->dev);
4274 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4275 struct intel_shared_dpll *pll;
4276 enum intel_dpll_id i;
4277
de419ab6
ML
4278 if (!to_intel_atomic_state(state)->dpll_set)
4279 return;
8bd31e67 4280
de419ab6 4281 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
de419ab6 4284 pll->config = shared_dpll[i];
8bd31e67
ACO
4285 }
4286}
4287
a1520318 4288static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4289{
4290 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4291 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4292 u32 temp;
4293
4294 temp = I915_READ(dslreg);
4295 udelay(500);
4296 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4297 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4298 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4299 }
4300}
4301
a1b2278e
CK
4302/**
4303 * skl_update_scaler_users - Stages update to crtc's scaler state
4304 * @intel_crtc: crtc
4305 * @crtc_state: crtc_state
4306 * @plane: plane (NULL indicates crtc is requesting update)
4307 * @plane_state: plane's state
4308 * @force_detach: request unconditional detachment of scaler
4309 *
4310 * This function updates scaler state for requested plane or crtc.
4311 * To request scaler usage update for a plane, caller shall pass plane pointer.
4312 * To request scaler usage update for crtc, caller shall pass plane pointer
4313 * as NULL.
4314 *
4315 * Return
4316 * 0 - scaler_usage updated successfully
4317 * error - requested scaling cannot be supported or other error condition
4318 */
4319int
4320skl_update_scaler_users(
4321 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4322 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4323 int force_detach)
4324{
4325 int need_scaling;
4326 int idx;
4327 int src_w, src_h, dst_w, dst_h;
4328 int *scaler_id;
4329 struct drm_framebuffer *fb;
4330 struct intel_crtc_scaler_state *scaler_state;
6156a456 4331 unsigned int rotation;
a1b2278e
CK
4332
4333 if (!intel_crtc || !crtc_state)
4334 return 0;
4335
4336 scaler_state = &crtc_state->scaler_state;
4337
4338 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4339 fb = intel_plane ? plane_state->base.fb : NULL;
4340
4341 if (intel_plane) {
4342 src_w = drm_rect_width(&plane_state->src) >> 16;
4343 src_h = drm_rect_height(&plane_state->src) >> 16;
4344 dst_w = drm_rect_width(&plane_state->dst);
4345 dst_h = drm_rect_height(&plane_state->dst);
4346 scaler_id = &plane_state->scaler_id;
6156a456 4347 rotation = plane_state->base.rotation;
a1b2278e
CK
4348 } else {
4349 struct drm_display_mode *adjusted_mode =
4350 &crtc_state->base.adjusted_mode;
4351 src_w = crtc_state->pipe_src_w;
4352 src_h = crtc_state->pipe_src_h;
4353 dst_w = adjusted_mode->hdisplay;
4354 dst_h = adjusted_mode->vdisplay;
4355 scaler_id = &scaler_state->scaler_id;
6156a456 4356 rotation = DRM_ROTATE_0;
a1b2278e 4357 }
6156a456
CK
4358
4359 need_scaling = intel_rotation_90_or_270(rotation) ?
4360 (src_h != dst_w || src_w != dst_h):
4361 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4362
4363 /*
4364 * if plane is being disabled or scaler is no more required or force detach
4365 * - free scaler binded to this plane/crtc
4366 * - in order to do this, update crtc->scaler_usage
4367 *
4368 * Here scaler state in crtc_state is set free so that
4369 * scaler can be assigned to other user. Actual register
4370 * update to free the scaler is done in plane/panel-fit programming.
4371 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4372 */
4373 if (force_detach || !need_scaling || (intel_plane &&
4374 (!fb || !plane_state->visible))) {
4375 if (*scaler_id >= 0) {
4376 scaler_state->scaler_users &= ~(1 << idx);
4377 scaler_state->scalers[*scaler_id].in_use = 0;
4378
4379 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4380 "crtc_state = %p scaler_users = 0x%x\n",
4381 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4382 intel_plane ? intel_plane->base.base.id :
4383 intel_crtc->base.base.id, crtc_state,
4384 scaler_state->scaler_users);
4385 *scaler_id = -1;
4386 }
4387 return 0;
4388 }
4389
4390 /* range checks */
4391 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4392 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4393
4394 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4395 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4396 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4397 "size is out of scaler range\n",
4398 intel_plane ? "PLANE" : "CRTC",
4399 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4400 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4401 return -EINVAL;
4402 }
4403
4404 /* check colorkey */
225c228a
CK
4405 if (WARN_ON(intel_plane &&
4406 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4407 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4408 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4409 return -EINVAL;
4410 }
4411
4412 /* Check src format */
4413 if (intel_plane) {
4414 switch (fb->pixel_format) {
4415 case DRM_FORMAT_RGB565:
4416 case DRM_FORMAT_XBGR8888:
4417 case DRM_FORMAT_XRGB8888:
4418 case DRM_FORMAT_ABGR8888:
4419 case DRM_FORMAT_ARGB8888:
4420 case DRM_FORMAT_XRGB2101010:
a1b2278e 4421 case DRM_FORMAT_XBGR2101010:
a1b2278e
CK
4422 case DRM_FORMAT_YUYV:
4423 case DRM_FORMAT_YVYU:
4424 case DRM_FORMAT_UYVY:
4425 case DRM_FORMAT_VYUY:
4426 break;
4427 default:
4428 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4429 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4430 return -EINVAL;
4431 }
4432 }
4433
4434 /* mark this plane as a scaler user in crtc_state */
4435 scaler_state->scaler_users |= (1 << idx);
4436 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4437 "crtc_state = %p scaler_users = 0x%x\n",
4438 intel_plane ? "PLANE" : "CRTC",
4439 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4440 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4441 return 0;
4442}
4443
4444static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4445{
4446 struct drm_device *dev = crtc->base.dev;
4447 struct drm_i915_private *dev_priv = dev->dev_private;
4448 int pipe = crtc->pipe;
a1b2278e
CK
4449 struct intel_crtc_scaler_state *scaler_state =
4450 &crtc->config->scaler_state;
4451
4452 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4453
4454 /* To update pfit, first update scaler state */
4455 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4456 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4457 skl_detach_scalers(crtc);
4458 if (!enable)
4459 return;
bd2e244f 4460
6e3c9717 4461 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4462 int id;
4463
4464 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4465 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4466 return;
4467 }
4468
4469 id = scaler_state->scaler_id;
4470 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4471 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4472 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4473 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4474
4475 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4476 }
4477}
4478
b074cec8
JB
4479static void ironlake_pfit_enable(struct intel_crtc *crtc)
4480{
4481 struct drm_device *dev = crtc->base.dev;
4482 struct drm_i915_private *dev_priv = dev->dev_private;
4483 int pipe = crtc->pipe;
4484
6e3c9717 4485 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4486 /* Force use of hard-coded filter coefficients
4487 * as some pre-programmed values are broken,
4488 * e.g. x201.
4489 */
4490 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4491 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4492 PF_PIPE_SEL_IVB(pipe));
4493 else
4494 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4495 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4496 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4497 }
4498}
4499
4a3b8769 4500static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4501{
4502 struct drm_device *dev = crtc->dev;
4503 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4504 struct drm_plane *plane;
bb53d4ae
VS
4505 struct intel_plane *intel_plane;
4506
af2b653b
MR
4507 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4508 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4509 if (intel_plane->pipe == pipe)
4510 intel_plane_restore(&intel_plane->base);
af2b653b 4511 }
bb53d4ae
VS
4512}
4513
20bc8673 4514void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4515{
cea165c3
VS
4516 struct drm_device *dev = crtc->base.dev;
4517 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4518
6e3c9717 4519 if (!crtc->config->ips_enabled)
d77e4531
PZ
4520 return;
4521
cea165c3
VS
4522 /* We can only enable IPS after we enable a plane and wait for a vblank */
4523 intel_wait_for_vblank(dev, crtc->pipe);
4524
d77e4531 4525 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4526 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4527 mutex_lock(&dev_priv->rps.hw_lock);
4528 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4529 mutex_unlock(&dev_priv->rps.hw_lock);
4530 /* Quoting Art Runyan: "its not safe to expect any particular
4531 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4532 * mailbox." Moreover, the mailbox may return a bogus state,
4533 * so we need to just enable it and continue on.
2a114cc1
BW
4534 */
4535 } else {
4536 I915_WRITE(IPS_CTL, IPS_ENABLE);
4537 /* The bit only becomes 1 in the next vblank, so this wait here
4538 * is essentially intel_wait_for_vblank. If we don't have this
4539 * and don't wait for vblanks until the end of crtc_enable, then
4540 * the HW state readout code will complain that the expected
4541 * IPS_CTL value is not the one we read. */
4542 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4543 DRM_ERROR("Timed out waiting for IPS enable\n");
4544 }
d77e4531
PZ
4545}
4546
20bc8673 4547void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4548{
4549 struct drm_device *dev = crtc->base.dev;
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551
6e3c9717 4552 if (!crtc->config->ips_enabled)
d77e4531
PZ
4553 return;
4554
4555 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4556 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4557 mutex_lock(&dev_priv->rps.hw_lock);
4558 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4559 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4560 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4561 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4562 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4563 } else {
2a114cc1 4564 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4565 POSTING_READ(IPS_CTL);
4566 }
d77e4531
PZ
4567
4568 /* We need to wait for a vblank before we can disable the plane. */
4569 intel_wait_for_vblank(dev, crtc->pipe);
4570}
4571
4572/** Loads the palette/gamma unit for the CRTC with the prepared values */
4573static void intel_crtc_load_lut(struct drm_crtc *crtc)
4574{
4575 struct drm_device *dev = crtc->dev;
4576 struct drm_i915_private *dev_priv = dev->dev_private;
4577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4578 enum pipe pipe = intel_crtc->pipe;
4579 int palreg = PALETTE(pipe);
4580 int i;
4581 bool reenable_ips = false;
4582
4583 /* The clocks have to be on to load the palette. */
53d9f4e9 4584 if (!crtc->state->active)
d77e4531
PZ
4585 return;
4586
50360403 4587 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4588 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4589 assert_dsi_pll_enabled(dev_priv);
4590 else
4591 assert_pll_enabled(dev_priv, pipe);
4592 }
4593
4594 /* use legacy palette for Ironlake */
7a1db49a 4595 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4596 palreg = LGC_PALETTE(pipe);
4597
4598 /* Workaround : Do not read or write the pipe palette/gamma data while
4599 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4600 */
6e3c9717 4601 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4602 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4603 GAMMA_MODE_MODE_SPLIT)) {
4604 hsw_disable_ips(intel_crtc);
4605 reenable_ips = true;
4606 }
4607
4608 for (i = 0; i < 256; i++) {
4609 I915_WRITE(palreg + 4 * i,
4610 (intel_crtc->lut_r[i] << 16) |
4611 (intel_crtc->lut_g[i] << 8) |
4612 intel_crtc->lut_b[i]);
4613 }
4614
4615 if (reenable_ips)
4616 hsw_enable_ips(intel_crtc);
4617}
4618
7cac945f 4619static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4620{
7cac945f 4621 if (intel_crtc->overlay) {
d3eedb1a
VS
4622 struct drm_device *dev = intel_crtc->base.dev;
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624
4625 mutex_lock(&dev->struct_mutex);
4626 dev_priv->mm.interruptible = false;
4627 (void) intel_overlay_switch_off(intel_crtc->overlay);
4628 dev_priv->mm.interruptible = true;
4629 mutex_unlock(&dev->struct_mutex);
4630 }
4631
4632 /* Let userspace switch the overlay on again. In most cases userspace
4633 * has to recompute where to put it anyway.
4634 */
4635}
4636
87d4300a
ML
4637/**
4638 * intel_post_enable_primary - Perform operations after enabling primary plane
4639 * @crtc: the CRTC whose primary plane was just enabled
4640 *
4641 * Performs potentially sleeping operations that must be done after the primary
4642 * plane is enabled, such as updating FBC and IPS. Note that this may be
4643 * called due to an explicit primary plane update, or due to an implicit
4644 * re-enable that is caused when a sprite plane is updated to no longer
4645 * completely hide the primary plane.
4646 */
4647static void
4648intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4649{
4650 struct drm_device *dev = crtc->dev;
87d4300a 4651 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4653 int pipe = intel_crtc->pipe;
a5c4d7bc 4654
87d4300a
ML
4655 /*
4656 * BDW signals flip done immediately if the plane
4657 * is disabled, even if the plane enable is already
4658 * armed to occur at the next vblank :(
4659 */
4660 if (IS_BROADWELL(dev))
4661 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4662
87d4300a
ML
4663 /*
4664 * FIXME IPS should be fine as long as one plane is
4665 * enabled, but in practice it seems to have problems
4666 * when going from primary only to sprite only and vice
4667 * versa.
4668 */
a5c4d7bc
VS
4669 hsw_enable_ips(intel_crtc);
4670
4671 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4672 intel_fbc_update(dev);
a5c4d7bc 4673 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4674
4675 /*
87d4300a
ML
4676 * Gen2 reports pipe underruns whenever all planes are disabled.
4677 * So don't enable underrun reporting before at least some planes
4678 * are enabled.
4679 * FIXME: Need to fix the logic to work when we turn off all planes
4680 * but leave the pipe running.
f99d7069 4681 */
87d4300a
ML
4682 if (IS_GEN2(dev))
4683 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4684
4685 /* Underruns don't raise interrupts, so check manually. */
4686 if (HAS_GMCH_DISPLAY(dev))
4687 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4688}
4689
87d4300a
ML
4690/**
4691 * intel_pre_disable_primary - Perform operations before disabling primary plane
4692 * @crtc: the CRTC whose primary plane is to be disabled
4693 *
4694 * Performs potentially sleeping operations that must be done before the
4695 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4696 * be called due to an explicit primary plane update, or due to an implicit
4697 * disable that is caused when a sprite plane completely hides the primary
4698 * plane.
4699 */
4700static void
4701intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4702{
4703 struct drm_device *dev = crtc->dev;
4704 struct drm_i915_private *dev_priv = dev->dev_private;
4705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4706 int pipe = intel_crtc->pipe;
a5c4d7bc 4707
87d4300a
ML
4708 /*
4709 * Gen2 reports pipe underruns whenever all planes are disabled.
4710 * So diasble underrun reporting before all the planes get disabled.
4711 * FIXME: Need to fix the logic to work when we turn off all planes
4712 * but leave the pipe running.
4713 */
4714 if (IS_GEN2(dev))
4715 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4716
87d4300a
ML
4717 /*
4718 * Vblank time updates from the shadow to live plane control register
4719 * are blocked if the memory self-refresh mode is active at that
4720 * moment. So to make sure the plane gets truly disabled, disable
4721 * first the self-refresh mode. The self-refresh enable bit in turn
4722 * will be checked/applied by the HW only at the next frame start
4723 * event which is after the vblank start event, so we need to have a
4724 * wait-for-vblank between disabling the plane and the pipe.
4725 */
4726 if (HAS_GMCH_DISPLAY(dev))
4727 intel_set_memory_cxsr(dev_priv, false);
4728
4729 mutex_lock(&dev->struct_mutex);
e35fef21 4730 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4731 intel_fbc_disable(dev);
87d4300a 4732 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4733
87d4300a
ML
4734 /*
4735 * FIXME IPS should be fine as long as one plane is
4736 * enabled, but in practice it seems to have problems
4737 * when going from primary only to sprite only and vice
4738 * versa.
4739 */
a5c4d7bc 4740 hsw_disable_ips(intel_crtc);
87d4300a
ML
4741}
4742
4743static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4744{
2d847d45
RV
4745 struct drm_device *dev = crtc->dev;
4746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4747 int pipe = intel_crtc->pipe;
4748
87d4300a
ML
4749 intel_enable_primary_hw_plane(crtc->primary, crtc);
4750 intel_enable_sprite_planes(crtc);
4751 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4752
4753 intel_post_enable_primary(crtc);
2d847d45
RV
4754
4755 /*
4756 * FIXME: Once we grow proper nuclear flip support out of this we need
4757 * to compute the mask of flip planes precisely. For the time being
4758 * consider this a flip to a NULL plane.
4759 */
4760 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
87d4300a
ML
4761}
4762
4763static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->dev;
4766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4767 struct intel_plane *intel_plane;
4768 int pipe = intel_crtc->pipe;
4769
4770 intel_crtc_wait_for_pending_flips(crtc);
4771
4772 intel_pre_disable_primary(crtc);
a5c4d7bc 4773
7cac945f 4774 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4775 for_each_intel_plane(dev, intel_plane) {
4776 if (intel_plane->pipe == pipe) {
4777 struct drm_crtc *from = intel_plane->base.crtc;
4778
4779 intel_plane->disable_plane(&intel_plane->base,
4780 from ?: crtc, true);
4781 }
4782 }
f98551ae 4783
f99d7069
DV
4784 /*
4785 * FIXME: Once we grow proper nuclear flip support out of this we need
4786 * to compute the mask of flip planes precisely. For the time being
4787 * consider this a flip to a NULL plane.
4788 */
4789 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4790}
4791
f67a559d
JB
4792static void ironlake_crtc_enable(struct drm_crtc *crtc)
4793{
4794 struct drm_device *dev = crtc->dev;
4795 struct drm_i915_private *dev_priv = dev->dev_private;
4796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4797 struct intel_encoder *encoder;
f67a559d 4798 int pipe = intel_crtc->pipe;
f67a559d 4799
53d9f4e9 4800 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4801 return;
4802
6e3c9717 4803 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4804 intel_prepare_shared_dpll(intel_crtc);
4805
6e3c9717 4806 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4807 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4808
4809 intel_set_pipe_timings(intel_crtc);
4810
6e3c9717 4811 if (intel_crtc->config->has_pch_encoder) {
29407aab 4812 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4813 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4814 }
4815
4816 ironlake_set_pipeconf(crtc);
4817
f67a559d 4818 intel_crtc->active = true;
8664281b 4819
a72e4c9f
DV
4820 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4821 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4822
f6736a1a 4823 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4824 if (encoder->pre_enable)
4825 encoder->pre_enable(encoder);
f67a559d 4826
6e3c9717 4827 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4828 /* Note: FDI PLL enabling _must_ be done before we enable the
4829 * cpu pipes, hence this is separate from all the other fdi/pch
4830 * enabling. */
88cefb6c 4831 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4832 } else {
4833 assert_fdi_tx_disabled(dev_priv, pipe);
4834 assert_fdi_rx_disabled(dev_priv, pipe);
4835 }
f67a559d 4836
b074cec8 4837 ironlake_pfit_enable(intel_crtc);
f67a559d 4838
9c54c0dd
JB
4839 /*
4840 * On ILK+ LUT must be loaded before the pipe is running but with
4841 * clocks enabled
4842 */
4843 intel_crtc_load_lut(crtc);
4844
f37fcc2a 4845 intel_update_watermarks(crtc);
e1fdc473 4846 intel_enable_pipe(intel_crtc);
f67a559d 4847
6e3c9717 4848 if (intel_crtc->config->has_pch_encoder)
f67a559d 4849 ironlake_pch_enable(crtc);
c98e9dcf 4850
f9b61ff6
DV
4851 assert_vblank_disabled(crtc);
4852 drm_crtc_vblank_on(crtc);
4853
fa5c73b1
DV
4854 for_each_encoder_on_crtc(dev, crtc, encoder)
4855 encoder->enable(encoder);
61b77ddd
DV
4856
4857 if (HAS_PCH_CPT(dev))
a1520318 4858 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4859}
4860
42db64ef
PZ
4861/* IPS only exists on ULT machines and is tied to pipe A. */
4862static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4863{
f5adf94e 4864 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4865}
4866
4f771f10
PZ
4867static void haswell_crtc_enable(struct drm_crtc *crtc)
4868{
4869 struct drm_device *dev = crtc->dev;
4870 struct drm_i915_private *dev_priv = dev->dev_private;
4871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4872 struct intel_encoder *encoder;
99d736a2
ML
4873 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4874 struct intel_crtc_state *pipe_config =
4875 to_intel_crtc_state(crtc->state);
4f771f10 4876
53d9f4e9 4877 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4878 return;
4879
df8ad70c
DV
4880 if (intel_crtc_to_shared_dpll(intel_crtc))
4881 intel_enable_shared_dpll(intel_crtc);
4882
6e3c9717 4883 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4884 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4885
4886 intel_set_pipe_timings(intel_crtc);
4887
6e3c9717
ACO
4888 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4889 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4890 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4891 }
4892
6e3c9717 4893 if (intel_crtc->config->has_pch_encoder) {
229fca97 4894 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4895 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4896 }
4897
4898 haswell_set_pipeconf(crtc);
4899
4900 intel_set_pipe_csc(crtc);
4901
4f771f10 4902 intel_crtc->active = true;
8664281b 4903
a72e4c9f 4904 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4905 for_each_encoder_on_crtc(dev, crtc, encoder)
4906 if (encoder->pre_enable)
4907 encoder->pre_enable(encoder);
4908
6e3c9717 4909 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4910 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4911 true);
4fe9467d
ID
4912 dev_priv->display.fdi_link_train(crtc);
4913 }
4914
1f544388 4915 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4916
ff6d9f55 4917 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4918 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4919 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4920 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4921 else
4922 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4923
4924 /*
4925 * On ILK+ LUT must be loaded before the pipe is running but with
4926 * clocks enabled
4927 */
4928 intel_crtc_load_lut(crtc);
4929
1f544388 4930 intel_ddi_set_pipe_settings(crtc);
8228c251 4931 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4932
f37fcc2a 4933 intel_update_watermarks(crtc);
e1fdc473 4934 intel_enable_pipe(intel_crtc);
42db64ef 4935
6e3c9717 4936 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4937 lpt_pch_enable(crtc);
4f771f10 4938
6e3c9717 4939 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4940 intel_ddi_set_vc_payload_alloc(crtc, true);
4941
f9b61ff6
DV
4942 assert_vblank_disabled(crtc);
4943 drm_crtc_vblank_on(crtc);
4944
8807e55b 4945 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4946 encoder->enable(encoder);
8807e55b
JN
4947 intel_opregion_notify_encoder(encoder, true);
4948 }
4f771f10 4949
e4916946
PZ
4950 /* If we change the relative order between pipe/planes enabling, we need
4951 * to change the workaround. */
99d736a2
ML
4952 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4953 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4954 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4955 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4956 }
4f771f10
PZ
4957}
4958
3f8dce3a
DV
4959static void ironlake_pfit_disable(struct intel_crtc *crtc)
4960{
4961 struct drm_device *dev = crtc->base.dev;
4962 struct drm_i915_private *dev_priv = dev->dev_private;
4963 int pipe = crtc->pipe;
4964
4965 /* To avoid upsetting the power well on haswell only disable the pfit if
4966 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4967 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4968 I915_WRITE(PF_CTL(pipe), 0);
4969 I915_WRITE(PF_WIN_POS(pipe), 0);
4970 I915_WRITE(PF_WIN_SZ(pipe), 0);
4971 }
4972}
4973
6be4a607
JB
4974static void ironlake_crtc_disable(struct drm_crtc *crtc)
4975{
4976 struct drm_device *dev = crtc->dev;
4977 struct drm_i915_private *dev_priv = dev->dev_private;
4978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4979 struct intel_encoder *encoder;
6be4a607 4980 int pipe = intel_crtc->pipe;
5eddb70b 4981 u32 reg, temp;
b52eb4dc 4982
53d9f4e9 4983 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
4984 return;
4985
ea9d758d
DV
4986 for_each_encoder_on_crtc(dev, crtc, encoder)
4987 encoder->disable(encoder);
4988
f9b61ff6
DV
4989 drm_crtc_vblank_off(crtc);
4990 assert_vblank_disabled(crtc);
4991
6e3c9717 4992 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 4993 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4994
575f7ab7 4995 intel_disable_pipe(intel_crtc);
32f9d658 4996
3f8dce3a 4997 ironlake_pfit_disable(intel_crtc);
2c07245f 4998
5a74f70a
VS
4999 if (intel_crtc->config->has_pch_encoder)
5000 ironlake_fdi_disable(crtc);
5001
bf49ec8c
DV
5002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 if (encoder->post_disable)
5004 encoder->post_disable(encoder);
2c07245f 5005
6e3c9717 5006 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5007 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5008
d925c59a
DV
5009 if (HAS_PCH_CPT(dev)) {
5010 /* disable TRANS_DP_CTL */
5011 reg = TRANS_DP_CTL(pipe);
5012 temp = I915_READ(reg);
5013 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5014 TRANS_DP_PORT_SEL_MASK);
5015 temp |= TRANS_DP_PORT_SEL_NONE;
5016 I915_WRITE(reg, temp);
5017
5018 /* disable DPLL_SEL */
5019 temp = I915_READ(PCH_DPLL_SEL);
11887397 5020 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5021 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5022 }
e3421a18 5023
d925c59a 5024 /* disable PCH DPLL */
e72f9fbf 5025 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5026
d925c59a
DV
5027 ironlake_fdi_pll_disable(intel_crtc);
5028 }
6b383a7f 5029
f7abfe8b 5030 intel_crtc->active = false;
46ba614c 5031 intel_update_watermarks(crtc);
d1ebd816
BW
5032
5033 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5034 intel_fbc_update(dev);
d1ebd816 5035 mutex_unlock(&dev->struct_mutex);
6be4a607 5036}
1b3c7a47 5037
4f771f10 5038static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5039{
4f771f10
PZ
5040 struct drm_device *dev = crtc->dev;
5041 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5043 struct intel_encoder *encoder;
6e3c9717 5044 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5045
53d9f4e9 5046 if (WARN_ON(!intel_crtc->active))
4f771f10
PZ
5047 return;
5048
8807e55b
JN
5049 for_each_encoder_on_crtc(dev, crtc, encoder) {
5050 intel_opregion_notify_encoder(encoder, false);
4f771f10 5051 encoder->disable(encoder);
8807e55b 5052 }
4f771f10 5053
f9b61ff6
DV
5054 drm_crtc_vblank_off(crtc);
5055 assert_vblank_disabled(crtc);
5056
6e3c9717 5057 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5058 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5059 false);
575f7ab7 5060 intel_disable_pipe(intel_crtc);
4f771f10 5061
6e3c9717 5062 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5063 intel_ddi_set_vc_payload_alloc(crtc, false);
5064
ad80a810 5065 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5066
ff6d9f55 5067 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5068 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5069 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5070 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5071 else
5072 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5073
1f544388 5074 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5075
6e3c9717 5076 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5077 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5078 intel_ddi_fdi_disable(crtc);
83616634 5079 }
4f771f10 5080
97b040aa
ID
5081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 if (encoder->post_disable)
5083 encoder->post_disable(encoder);
5084
4f771f10 5085 intel_crtc->active = false;
46ba614c 5086 intel_update_watermarks(crtc);
4f771f10
PZ
5087
5088 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5089 intel_fbc_update(dev);
4f771f10 5090 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5091
5092 if (intel_crtc_to_shared_dpll(intel_crtc))
5093 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5094}
5095
2dd24552
JB
5096static void i9xx_pfit_enable(struct intel_crtc *crtc)
5097{
5098 struct drm_device *dev = crtc->base.dev;
5099 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5100 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5101
681a8504 5102 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5103 return;
5104
2dd24552 5105 /*
c0b03411
DV
5106 * The panel fitter should only be adjusted whilst the pipe is disabled,
5107 * according to register description and PRM.
2dd24552 5108 */
c0b03411
DV
5109 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5110 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5111
b074cec8
JB
5112 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5113 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5114
5115 /* Border color in case we don't scale up to the full screen. Black by
5116 * default, change to something else for debugging. */
5117 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5118}
5119
d05410f9
DA
5120static enum intel_display_power_domain port_to_power_domain(enum port port)
5121{
5122 switch (port) {
5123 case PORT_A:
5124 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5125 case PORT_B:
5126 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5127 case PORT_C:
5128 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5129 case PORT_D:
5130 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5131 default:
5132 WARN_ON_ONCE(1);
5133 return POWER_DOMAIN_PORT_OTHER;
5134 }
5135}
5136
77d22dca
ID
5137#define for_each_power_domain(domain, mask) \
5138 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5139 if ((1 << (domain)) & (mask))
5140
319be8ae
ID
5141enum intel_display_power_domain
5142intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5143{
5144 struct drm_device *dev = intel_encoder->base.dev;
5145 struct intel_digital_port *intel_dig_port;
5146
5147 switch (intel_encoder->type) {
5148 case INTEL_OUTPUT_UNKNOWN:
5149 /* Only DDI platforms should ever use this output type */
5150 WARN_ON_ONCE(!HAS_DDI(dev));
5151 case INTEL_OUTPUT_DISPLAYPORT:
5152 case INTEL_OUTPUT_HDMI:
5153 case INTEL_OUTPUT_EDP:
5154 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5155 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5156 case INTEL_OUTPUT_DP_MST:
5157 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5158 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5159 case INTEL_OUTPUT_ANALOG:
5160 return POWER_DOMAIN_PORT_CRT;
5161 case INTEL_OUTPUT_DSI:
5162 return POWER_DOMAIN_PORT_DSI;
5163 default:
5164 return POWER_DOMAIN_PORT_OTHER;
5165 }
5166}
5167
5168static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5169{
319be8ae
ID
5170 struct drm_device *dev = crtc->dev;
5171 struct intel_encoder *intel_encoder;
5172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5173 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5174 unsigned long mask;
5175 enum transcoder transcoder;
5176
5177 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5178
5179 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5180 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5181 if (intel_crtc->config->pch_pfit.enabled ||
5182 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5183 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5184
319be8ae
ID
5185 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5186 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5187
77d22dca
ID
5188 return mask;
5189}
5190
679dacd4 5191static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5192{
679dacd4 5193 struct drm_device *dev = state->dev;
77d22dca
ID
5194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5196 struct intel_crtc *crtc;
5197
5198 /*
5199 * First get all needed power domains, then put all unneeded, to avoid
5200 * any unnecessary toggling of the power wells.
5201 */
d3fcc808 5202 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5203 enum intel_display_power_domain domain;
5204
83d65738 5205 if (!crtc->base.state->enable)
77d22dca
ID
5206 continue;
5207
319be8ae 5208 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5209
5210 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5211 intel_display_power_get(dev_priv, domain);
5212 }
5213
50f6e502 5214 if (dev_priv->display.modeset_global_resources)
679dacd4 5215 dev_priv->display.modeset_global_resources(state);
50f6e502 5216
d3fcc808 5217 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5218 enum intel_display_power_domain domain;
5219
5220 for_each_power_domain(domain, crtc->enabled_power_domains)
5221 intel_display_power_put(dev_priv, domain);
5222
5223 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5224 }
5225
5226 intel_display_set_init_power(dev_priv, false);
5227}
5228
560a7ae4
DL
5229static void intel_update_max_cdclk(struct drm_device *dev)
5230{
5231 struct drm_i915_private *dev_priv = dev->dev_private;
5232
5233 if (IS_SKYLAKE(dev)) {
5234 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5235
5236 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5237 dev_priv->max_cdclk_freq = 675000;
5238 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5239 dev_priv->max_cdclk_freq = 540000;
5240 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5241 dev_priv->max_cdclk_freq = 450000;
5242 else
5243 dev_priv->max_cdclk_freq = 337500;
5244 } else if (IS_BROADWELL(dev)) {
5245 /*
5246 * FIXME with extra cooling we can allow
5247 * 540 MHz for ULX and 675 Mhz for ULT.
5248 * How can we know if extra cooling is
5249 * available? PCI ID, VTB, something else?
5250 */
5251 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5252 dev_priv->max_cdclk_freq = 450000;
5253 else if (IS_BDW_ULX(dev))
5254 dev_priv->max_cdclk_freq = 450000;
5255 else if (IS_BDW_ULT(dev))
5256 dev_priv->max_cdclk_freq = 540000;
5257 else
5258 dev_priv->max_cdclk_freq = 675000;
5259 } else if (IS_VALLEYVIEW(dev)) {
5260 dev_priv->max_cdclk_freq = 400000;
5261 } else {
5262 /* otherwise assume cdclk is fixed */
5263 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5264 }
5265
5266 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5267 dev_priv->max_cdclk_freq);
5268}
5269
5270static void intel_update_cdclk(struct drm_device *dev)
5271{
5272 struct drm_i915_private *dev_priv = dev->dev_private;
5273
5274 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5275 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5276 dev_priv->cdclk_freq);
5277
5278 /*
5279 * Program the gmbus_freq based on the cdclk frequency.
5280 * BSpec erroneously claims we should aim for 4MHz, but
5281 * in fact 1MHz is the correct frequency.
5282 */
5283 if (IS_VALLEYVIEW(dev)) {
5284 /*
5285 * Program the gmbus_freq based on the cdclk frequency.
5286 * BSpec erroneously claims we should aim for 4MHz, but
5287 * in fact 1MHz is the correct frequency.
5288 */
5289 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5290 }
5291
5292 if (dev_priv->max_cdclk_freq == 0)
5293 intel_update_max_cdclk(dev);
5294}
5295
70d0c574 5296static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5297{
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 uint32_t divider;
5300 uint32_t ratio;
5301 uint32_t current_freq;
5302 int ret;
5303
5304 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5305 switch (frequency) {
5306 case 144000:
5307 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5308 ratio = BXT_DE_PLL_RATIO(60);
5309 break;
5310 case 288000:
5311 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5312 ratio = BXT_DE_PLL_RATIO(60);
5313 break;
5314 case 384000:
5315 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5316 ratio = BXT_DE_PLL_RATIO(60);
5317 break;
5318 case 576000:
5319 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5320 ratio = BXT_DE_PLL_RATIO(60);
5321 break;
5322 case 624000:
5323 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5324 ratio = BXT_DE_PLL_RATIO(65);
5325 break;
5326 case 19200:
5327 /*
5328 * Bypass frequency with DE PLL disabled. Init ratio, divider
5329 * to suppress GCC warning.
5330 */
5331 ratio = 0;
5332 divider = 0;
5333 break;
5334 default:
5335 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5336
5337 return;
5338 }
5339
5340 mutex_lock(&dev_priv->rps.hw_lock);
5341 /* Inform power controller of upcoming frequency change */
5342 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5343 0x80000000);
5344 mutex_unlock(&dev_priv->rps.hw_lock);
5345
5346 if (ret) {
5347 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5348 ret, frequency);
5349 return;
5350 }
5351
5352 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5353 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5354 current_freq = current_freq * 500 + 1000;
5355
5356 /*
5357 * DE PLL has to be disabled when
5358 * - setting to 19.2MHz (bypass, PLL isn't used)
5359 * - before setting to 624MHz (PLL needs toggling)
5360 * - before setting to any frequency from 624MHz (PLL needs toggling)
5361 */
5362 if (frequency == 19200 || frequency == 624000 ||
5363 current_freq == 624000) {
5364 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5365 /* Timeout 200us */
5366 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5367 1))
5368 DRM_ERROR("timout waiting for DE PLL unlock\n");
5369 }
5370
5371 if (frequency != 19200) {
5372 uint32_t val;
5373
5374 val = I915_READ(BXT_DE_PLL_CTL);
5375 val &= ~BXT_DE_PLL_RATIO_MASK;
5376 val |= ratio;
5377 I915_WRITE(BXT_DE_PLL_CTL, val);
5378
5379 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5380 /* Timeout 200us */
5381 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5382 DRM_ERROR("timeout waiting for DE PLL lock\n");
5383
5384 val = I915_READ(CDCLK_CTL);
5385 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5386 val |= divider;
5387 /*
5388 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5389 * enable otherwise.
5390 */
5391 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5392 if (frequency >= 500000)
5393 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5394
5395 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5396 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5397 val |= (frequency - 1000) / 500;
5398 I915_WRITE(CDCLK_CTL, val);
5399 }
5400
5401 mutex_lock(&dev_priv->rps.hw_lock);
5402 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5403 DIV_ROUND_UP(frequency, 25000));
5404 mutex_unlock(&dev_priv->rps.hw_lock);
5405
5406 if (ret) {
5407 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5408 ret, frequency);
5409 return;
5410 }
5411
a47871bd 5412 intel_update_cdclk(dev);
f8437dd1
VK
5413}
5414
5415void broxton_init_cdclk(struct drm_device *dev)
5416{
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 uint32_t val;
5419
5420 /*
5421 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5422 * or else the reset will hang because there is no PCH to respond.
5423 * Move the handshake programming to initialization sequence.
5424 * Previously was left up to BIOS.
5425 */
5426 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5427 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5428 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5429
5430 /* Enable PG1 for cdclk */
5431 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5432
5433 /* check if cd clock is enabled */
5434 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5435 DRM_DEBUG_KMS("Display already initialized\n");
5436 return;
5437 }
5438
5439 /*
5440 * FIXME:
5441 * - The initial CDCLK needs to be read from VBT.
5442 * Need to make this change after VBT has changes for BXT.
5443 * - check if setting the max (or any) cdclk freq is really necessary
5444 * here, it belongs to modeset time
5445 */
5446 broxton_set_cdclk(dev, 624000);
5447
5448 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5449 POSTING_READ(DBUF_CTL);
5450
f8437dd1
VK
5451 udelay(10);
5452
5453 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5454 DRM_ERROR("DBuf power enable timeout!\n");
5455}
5456
5457void broxton_uninit_cdclk(struct drm_device *dev)
5458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460
5461 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5462 POSTING_READ(DBUF_CTL);
5463
f8437dd1
VK
5464 udelay(10);
5465
5466 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5467 DRM_ERROR("DBuf power disable timeout!\n");
5468
5469 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5470 broxton_set_cdclk(dev, 19200);
5471
5472 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5473}
5474
5d96d8af
DL
5475static const struct skl_cdclk_entry {
5476 unsigned int freq;
5477 unsigned int vco;
5478} skl_cdclk_frequencies[] = {
5479 { .freq = 308570, .vco = 8640 },
5480 { .freq = 337500, .vco = 8100 },
5481 { .freq = 432000, .vco = 8640 },
5482 { .freq = 450000, .vco = 8100 },
5483 { .freq = 540000, .vco = 8100 },
5484 { .freq = 617140, .vco = 8640 },
5485 { .freq = 675000, .vco = 8100 },
5486};
5487
5488static unsigned int skl_cdclk_decimal(unsigned int freq)
5489{
5490 return (freq - 1000) / 500;
5491}
5492
5493static unsigned int skl_cdclk_get_vco(unsigned int freq)
5494{
5495 unsigned int i;
5496
5497 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5498 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5499
5500 if (e->freq == freq)
5501 return e->vco;
5502 }
5503
5504 return 8100;
5505}
5506
5507static void
5508skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5509{
5510 unsigned int min_freq;
5511 u32 val;
5512
5513 /* select the minimum CDCLK before enabling DPLL 0 */
5514 val = I915_READ(CDCLK_CTL);
5515 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5516 val |= CDCLK_FREQ_337_308;
5517
5518 if (required_vco == 8640)
5519 min_freq = 308570;
5520 else
5521 min_freq = 337500;
5522
5523 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5524
5525 I915_WRITE(CDCLK_CTL, val);
5526 POSTING_READ(CDCLK_CTL);
5527
5528 /*
5529 * We always enable DPLL0 with the lowest link rate possible, but still
5530 * taking into account the VCO required to operate the eDP panel at the
5531 * desired frequency. The usual DP link rates operate with a VCO of
5532 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5533 * The modeset code is responsible for the selection of the exact link
5534 * rate later on, with the constraint of choosing a frequency that
5535 * works with required_vco.
5536 */
5537 val = I915_READ(DPLL_CTRL1);
5538
5539 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5540 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5541 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5542 if (required_vco == 8640)
5543 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5544 SKL_DPLL0);
5545 else
5546 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5547 SKL_DPLL0);
5548
5549 I915_WRITE(DPLL_CTRL1, val);
5550 POSTING_READ(DPLL_CTRL1);
5551
5552 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5553
5554 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5555 DRM_ERROR("DPLL0 not locked\n");
5556}
5557
5558static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5559{
5560 int ret;
5561 u32 val;
5562
5563 /* inform PCU we want to change CDCLK */
5564 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5565 mutex_lock(&dev_priv->rps.hw_lock);
5566 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5567 mutex_unlock(&dev_priv->rps.hw_lock);
5568
5569 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5570}
5571
5572static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5573{
5574 unsigned int i;
5575
5576 for (i = 0; i < 15; i++) {
5577 if (skl_cdclk_pcu_ready(dev_priv))
5578 return true;
5579 udelay(10);
5580 }
5581
5582 return false;
5583}
5584
5585static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5586{
560a7ae4 5587 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5588 u32 freq_select, pcu_ack;
5589
5590 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5591
5592 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5593 DRM_ERROR("failed to inform PCU about cdclk change\n");
5594 return;
5595 }
5596
5597 /* set CDCLK_CTL */
5598 switch(freq) {
5599 case 450000:
5600 case 432000:
5601 freq_select = CDCLK_FREQ_450_432;
5602 pcu_ack = 1;
5603 break;
5604 case 540000:
5605 freq_select = CDCLK_FREQ_540;
5606 pcu_ack = 2;
5607 break;
5608 case 308570:
5609 case 337500:
5610 default:
5611 freq_select = CDCLK_FREQ_337_308;
5612 pcu_ack = 0;
5613 break;
5614 case 617140:
5615 case 675000:
5616 freq_select = CDCLK_FREQ_675_617;
5617 pcu_ack = 3;
5618 break;
5619 }
5620
5621 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5622 POSTING_READ(CDCLK_CTL);
5623
5624 /* inform PCU of the change */
5625 mutex_lock(&dev_priv->rps.hw_lock);
5626 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5627 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5628
5629 intel_update_cdclk(dev);
5d96d8af
DL
5630}
5631
5632void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5633{
5634 /* disable DBUF power */
5635 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5636 POSTING_READ(DBUF_CTL);
5637
5638 udelay(10);
5639
5640 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5641 DRM_ERROR("DBuf power disable timeout\n");
5642
5643 /* disable DPLL0 */
5644 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5645 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5646 DRM_ERROR("Couldn't disable DPLL0\n");
5647
5648 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5649}
5650
5651void skl_init_cdclk(struct drm_i915_private *dev_priv)
5652{
5653 u32 val;
5654 unsigned int required_vco;
5655
5656 /* enable PCH reset handshake */
5657 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5658 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5659
5660 /* enable PG1 and Misc I/O */
5661 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5662
5663 /* DPLL0 already enabed !? */
5664 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5665 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5666 return;
5667 }
5668
5669 /* enable DPLL0 */
5670 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5671 skl_dpll0_enable(dev_priv, required_vco);
5672
5673 /* set CDCLK to the frequency the BIOS chose */
5674 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5675
5676 /* enable DBUF power */
5677 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5678 POSTING_READ(DBUF_CTL);
5679
5680 udelay(10);
5681
5682 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5683 DRM_ERROR("DBuf power enable timeout\n");
5684}
5685
dfcab17e 5686/* returns HPLL frequency in kHz */
f8bf63fd 5687static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5688{
586f49dc 5689 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5690
586f49dc 5691 /* Obtain SKU information */
a580516d 5692 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5693 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5694 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5695 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5696
dfcab17e 5697 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5698}
5699
5700/* Adjust CDclk dividers to allow high res or save power if possible */
5701static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5702{
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704 u32 val, cmd;
5705
164dfd28
VK
5706 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5707 != dev_priv->cdclk_freq);
d60c4473 5708
dfcab17e 5709 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5710 cmd = 2;
dfcab17e 5711 else if (cdclk == 266667)
30a970c6
JB
5712 cmd = 1;
5713 else
5714 cmd = 0;
5715
5716 mutex_lock(&dev_priv->rps.hw_lock);
5717 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5718 val &= ~DSPFREQGUAR_MASK;
5719 val |= (cmd << DSPFREQGUAR_SHIFT);
5720 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5721 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5722 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5723 50)) {
5724 DRM_ERROR("timed out waiting for CDclk change\n");
5725 }
5726 mutex_unlock(&dev_priv->rps.hw_lock);
5727
54433e91
VS
5728 mutex_lock(&dev_priv->sb_lock);
5729
dfcab17e 5730 if (cdclk == 400000) {
6bcda4f0 5731 u32 divider;
30a970c6 5732
6bcda4f0 5733 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5734
30a970c6
JB
5735 /* adjust cdclk divider */
5736 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5737 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5738 val |= divider;
5739 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5740
5741 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5742 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5743 50))
5744 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5745 }
5746
30a970c6
JB
5747 /* adjust self-refresh exit latency value */
5748 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5749 val &= ~0x7f;
5750
5751 /*
5752 * For high bandwidth configs, we set a higher latency in the bunit
5753 * so that the core display fetch happens in time to avoid underruns.
5754 */
dfcab17e 5755 if (cdclk == 400000)
30a970c6
JB
5756 val |= 4500 / 250; /* 4.5 usec */
5757 else
5758 val |= 3000 / 250; /* 3.0 usec */
5759 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5760
a580516d 5761 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5762
b6283055 5763 intel_update_cdclk(dev);
30a970c6
JB
5764}
5765
383c5a6a
VS
5766static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5767{
5768 struct drm_i915_private *dev_priv = dev->dev_private;
5769 u32 val, cmd;
5770
164dfd28
VK
5771 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5772 != dev_priv->cdclk_freq);
383c5a6a
VS
5773
5774 switch (cdclk) {
383c5a6a
VS
5775 case 333333:
5776 case 320000:
383c5a6a 5777 case 266667:
383c5a6a 5778 case 200000:
383c5a6a
VS
5779 break;
5780 default:
5f77eeb0 5781 MISSING_CASE(cdclk);
383c5a6a
VS
5782 return;
5783 }
5784
9d0d3fda
VS
5785 /*
5786 * Specs are full of misinformation, but testing on actual
5787 * hardware has shown that we just need to write the desired
5788 * CCK divider into the Punit register.
5789 */
5790 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5791
383c5a6a
VS
5792 mutex_lock(&dev_priv->rps.hw_lock);
5793 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5794 val &= ~DSPFREQGUAR_MASK_CHV;
5795 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5796 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5797 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5798 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5799 50)) {
5800 DRM_ERROR("timed out waiting for CDclk change\n");
5801 }
5802 mutex_unlock(&dev_priv->rps.hw_lock);
5803
b6283055 5804 intel_update_cdclk(dev);
383c5a6a
VS
5805}
5806
30a970c6
JB
5807static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5808 int max_pixclk)
5809{
6bcda4f0 5810 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5811 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5812
30a970c6
JB
5813 /*
5814 * Really only a few cases to deal with, as only 4 CDclks are supported:
5815 * 200MHz
5816 * 267MHz
29dc7ef3 5817 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5818 * 400MHz (VLV only)
5819 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5820 * of the lower bin and adjust if needed.
e37c67a1
VS
5821 *
5822 * We seem to get an unstable or solid color picture at 200MHz.
5823 * Not sure what's wrong. For now use 200MHz only when all pipes
5824 * are off.
30a970c6 5825 */
6cca3195
VS
5826 if (!IS_CHERRYVIEW(dev_priv) &&
5827 max_pixclk > freq_320*limit/100)
dfcab17e 5828 return 400000;
6cca3195 5829 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5830 return freq_320;
e37c67a1 5831 else if (max_pixclk > 0)
dfcab17e 5832 return 266667;
e37c67a1
VS
5833 else
5834 return 200000;
30a970c6
JB
5835}
5836
f8437dd1
VK
5837static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5838 int max_pixclk)
5839{
5840 /*
5841 * FIXME:
5842 * - remove the guardband, it's not needed on BXT
5843 * - set 19.2MHz bypass frequency if there are no active pipes
5844 */
5845 if (max_pixclk > 576000*9/10)
5846 return 624000;
5847 else if (max_pixclk > 384000*9/10)
5848 return 576000;
5849 else if (max_pixclk > 288000*9/10)
5850 return 384000;
5851 else if (max_pixclk > 144000*9/10)
5852 return 288000;
5853 else
5854 return 144000;
5855}
5856
a821fc46
ACO
5857/* Compute the max pixel clock for new configuration. Uses atomic state if
5858 * that's non-NULL, look at current state otherwise. */
5859static int intel_mode_max_pixclk(struct drm_device *dev,
5860 struct drm_atomic_state *state)
30a970c6 5861{
30a970c6 5862 struct intel_crtc *intel_crtc;
304603f4 5863 struct intel_crtc_state *crtc_state;
30a970c6
JB
5864 int max_pixclk = 0;
5865
d3fcc808 5866 for_each_intel_crtc(dev, intel_crtc) {
a821fc46
ACO
5867 if (state)
5868 crtc_state =
5869 intel_atomic_get_crtc_state(state, intel_crtc);
5870 else
5871 crtc_state = intel_crtc->config;
304603f4
ACO
5872 if (IS_ERR(crtc_state))
5873 return PTR_ERR(crtc_state);
5874
5875 if (!crtc_state->base.enable)
5876 continue;
5877
5878 max_pixclk = max(max_pixclk,
5879 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5880 }
5881
5882 return max_pixclk;
5883}
5884
0a9ab303 5885static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
30a970c6 5886{
304603f4 5887 struct drm_i915_private *dev_priv = to_i915(state->dev);
0a9ab303
ACO
5888 struct drm_crtc *crtc;
5889 struct drm_crtc_state *crtc_state;
a821fc46 5890 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
85a96e7a 5891 int cdclk, ret = 0;
30a970c6 5892
304603f4
ACO
5893 if (max_pixclk < 0)
5894 return max_pixclk;
30a970c6 5895
f8437dd1
VK
5896 if (IS_VALLEYVIEW(dev_priv))
5897 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5898 else
5899 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5900
5901 if (cdclk == dev_priv->cdclk_freq)
304603f4 5902 return 0;
30a970c6 5903
0a9ab303
ACO
5904 /* add all active pipes to the state */
5905 for_each_crtc(state->dev, crtc) {
0a9ab303
ACO
5906 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5907 if (IS_ERR(crtc_state))
5908 return PTR_ERR(crtc_state);
0a9ab303 5909
85a96e7a
ML
5910 if (!crtc_state->active || needs_modeset(crtc_state))
5911 continue;
304603f4 5912
85a96e7a
ML
5913 crtc_state->mode_changed = true;
5914
5915 ret = drm_atomic_add_affected_connectors(state, crtc);
5916 if (ret)
5917 break;
5918
5919 ret = drm_atomic_add_affected_planes(state, crtc);
5920 if (ret)
5921 break;
5922 }
5923
5924 return ret;
30a970c6
JB
5925}
5926
1e69cd74
VS
5927static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5928{
5929 unsigned int credits, default_credits;
5930
5931 if (IS_CHERRYVIEW(dev_priv))
5932 default_credits = PFI_CREDIT(12);
5933 else
5934 default_credits = PFI_CREDIT(8);
5935
164dfd28 5936 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5937 /* CHV suggested value is 31 or 63 */
5938 if (IS_CHERRYVIEW(dev_priv))
5939 credits = PFI_CREDIT_31;
5940 else
5941 credits = PFI_CREDIT(15);
5942 } else {
5943 credits = default_credits;
5944 }
5945
5946 /*
5947 * WA - write default credits before re-programming
5948 * FIXME: should we also set the resend bit here?
5949 */
5950 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5951 default_credits);
5952
5953 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5954 credits | PFI_CREDIT_RESEND);
5955
5956 /*
5957 * FIXME is this guaranteed to clear
5958 * immediately or should we poll for it?
5959 */
5960 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5961}
5962
a821fc46 5963static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
30a970c6 5964{
a821fc46 5965 struct drm_device *dev = old_state->dev;
30a970c6 5966 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 5967 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
304603f4
ACO
5968 int req_cdclk;
5969
a821fc46
ACO
5970 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5971 * never fail. */
304603f4
ACO
5972 if (WARN_ON(max_pixclk < 0))
5973 return;
30a970c6 5974
304603f4 5975 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5976
164dfd28 5977 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5978 /*
5979 * FIXME: We can end up here with all power domains off, yet
5980 * with a CDCLK frequency other than the minimum. To account
5981 * for this take the PIPE-A power domain, which covers the HW
5982 * blocks needed for the following programming. This can be
5983 * removed once it's guaranteed that we get here either with
5984 * the minimum CDCLK set, or the required power domains
5985 * enabled.
5986 */
5987 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5988
383c5a6a
VS
5989 if (IS_CHERRYVIEW(dev))
5990 cherryview_set_cdclk(dev, req_cdclk);
5991 else
5992 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5993
1e69cd74
VS
5994 vlv_program_pfi_credits(dev_priv);
5995
738c05c0 5996 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5997 }
30a970c6
JB
5998}
5999
89b667f8
JB
6000static void valleyview_crtc_enable(struct drm_crtc *crtc)
6001{
6002 struct drm_device *dev = crtc->dev;
a72e4c9f 6003 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6005 struct intel_encoder *encoder;
6006 int pipe = intel_crtc->pipe;
23538ef1 6007 bool is_dsi;
89b667f8 6008
53d9f4e9 6009 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6010 return;
6011
409ee761 6012 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 6013
1ae0d137
VS
6014 if (!is_dsi) {
6015 if (IS_CHERRYVIEW(dev))
6e3c9717 6016 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6017 else
6e3c9717 6018 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6019 }
5b18e57c 6020
6e3c9717 6021 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6022 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6023
6024 intel_set_pipe_timings(intel_crtc);
6025
c14b0485
VS
6026 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6027 struct drm_i915_private *dev_priv = dev->dev_private;
6028
6029 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6030 I915_WRITE(CHV_CANVAS(pipe), 0);
6031 }
6032
5b18e57c
DV
6033 i9xx_set_pipeconf(intel_crtc);
6034
89b667f8 6035 intel_crtc->active = true;
89b667f8 6036
a72e4c9f 6037 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6038
89b667f8
JB
6039 for_each_encoder_on_crtc(dev, crtc, encoder)
6040 if (encoder->pre_pll_enable)
6041 encoder->pre_pll_enable(encoder);
6042
9d556c99
CML
6043 if (!is_dsi) {
6044 if (IS_CHERRYVIEW(dev))
6e3c9717 6045 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6046 else
6e3c9717 6047 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6048 }
89b667f8
JB
6049
6050 for_each_encoder_on_crtc(dev, crtc, encoder)
6051 if (encoder->pre_enable)
6052 encoder->pre_enable(encoder);
6053
2dd24552
JB
6054 i9xx_pfit_enable(intel_crtc);
6055
63cbb074
VS
6056 intel_crtc_load_lut(crtc);
6057
f37fcc2a 6058 intel_update_watermarks(crtc);
e1fdc473 6059 intel_enable_pipe(intel_crtc);
be6a6f8e 6060
4b3a9526
VS
6061 assert_vblank_disabled(crtc);
6062 drm_crtc_vblank_on(crtc);
6063
f9b61ff6
DV
6064 for_each_encoder_on_crtc(dev, crtc, encoder)
6065 encoder->enable(encoder);
89b667f8
JB
6066}
6067
f13c2ef3
DV
6068static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6069{
6070 struct drm_device *dev = crtc->base.dev;
6071 struct drm_i915_private *dev_priv = dev->dev_private;
6072
6e3c9717
ACO
6073 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6074 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6075}
6076
0b8765c6 6077static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6078{
6079 struct drm_device *dev = crtc->dev;
a72e4c9f 6080 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6082 struct intel_encoder *encoder;
79e53945 6083 int pipe = intel_crtc->pipe;
79e53945 6084
53d9f4e9 6085 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6086 return;
6087
f13c2ef3
DV
6088 i9xx_set_pll_dividers(intel_crtc);
6089
6e3c9717 6090 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6091 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6092
6093 intel_set_pipe_timings(intel_crtc);
6094
5b18e57c
DV
6095 i9xx_set_pipeconf(intel_crtc);
6096
f7abfe8b 6097 intel_crtc->active = true;
6b383a7f 6098
4a3436e8 6099 if (!IS_GEN2(dev))
a72e4c9f 6100 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6101
9d6d9f19
MK
6102 for_each_encoder_on_crtc(dev, crtc, encoder)
6103 if (encoder->pre_enable)
6104 encoder->pre_enable(encoder);
6105
f6736a1a
DV
6106 i9xx_enable_pll(intel_crtc);
6107
2dd24552
JB
6108 i9xx_pfit_enable(intel_crtc);
6109
63cbb074
VS
6110 intel_crtc_load_lut(crtc);
6111
f37fcc2a 6112 intel_update_watermarks(crtc);
e1fdc473 6113 intel_enable_pipe(intel_crtc);
be6a6f8e 6114
4b3a9526
VS
6115 assert_vblank_disabled(crtc);
6116 drm_crtc_vblank_on(crtc);
6117
f9b61ff6
DV
6118 for_each_encoder_on_crtc(dev, crtc, encoder)
6119 encoder->enable(encoder);
0b8765c6 6120}
79e53945 6121
87476d63
DV
6122static void i9xx_pfit_disable(struct intel_crtc *crtc)
6123{
6124 struct drm_device *dev = crtc->base.dev;
6125 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6126
6e3c9717 6127 if (!crtc->config->gmch_pfit.control)
328d8e82 6128 return;
87476d63 6129
328d8e82 6130 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6131
328d8e82
DV
6132 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6133 I915_READ(PFIT_CONTROL));
6134 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6135}
6136
0b8765c6
JB
6137static void i9xx_crtc_disable(struct drm_crtc *crtc)
6138{
6139 struct drm_device *dev = crtc->dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6142 struct intel_encoder *encoder;
0b8765c6 6143 int pipe = intel_crtc->pipe;
ef9c3aee 6144
53d9f4e9 6145 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
6146 return;
6147
6304cd91
VS
6148 /*
6149 * On gen2 planes are double buffered but the pipe isn't, so we must
6150 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6151 * We also need to wait on all gmch platforms because of the
6152 * self-refresh mode constraint explained above.
6304cd91 6153 */
564ed191 6154 intel_wait_for_vblank(dev, pipe);
6304cd91 6155
4b3a9526
VS
6156 for_each_encoder_on_crtc(dev, crtc, encoder)
6157 encoder->disable(encoder);
6158
f9b61ff6
DV
6159 drm_crtc_vblank_off(crtc);
6160 assert_vblank_disabled(crtc);
6161
575f7ab7 6162 intel_disable_pipe(intel_crtc);
24a1f16d 6163
87476d63 6164 i9xx_pfit_disable(intel_crtc);
24a1f16d 6165
89b667f8
JB
6166 for_each_encoder_on_crtc(dev, crtc, encoder)
6167 if (encoder->post_disable)
6168 encoder->post_disable(encoder);
6169
409ee761 6170 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6171 if (IS_CHERRYVIEW(dev))
6172 chv_disable_pll(dev_priv, pipe);
6173 else if (IS_VALLEYVIEW(dev))
6174 vlv_disable_pll(dev_priv, pipe);
6175 else
1c4e0274 6176 i9xx_disable_pll(intel_crtc);
076ed3b2 6177 }
0b8765c6 6178
4a3436e8 6179 if (!IS_GEN2(dev))
a72e4c9f 6180 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6181
f7abfe8b 6182 intel_crtc->active = false;
46ba614c 6183 intel_update_watermarks(crtc);
f37fcc2a 6184
efa9624e 6185 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6186 intel_fbc_update(dev);
efa9624e 6187 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6188}
6189
b17d48e2
ML
6190static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6191{
6192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6193 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6194 enum intel_display_power_domain domain;
6195 unsigned long domains;
6196
6197 if (!intel_crtc->active)
6198 return;
6199
6200 intel_crtc_disable_planes(crtc);
6201 dev_priv->display.crtc_disable(crtc);
6202
6203 domains = intel_crtc->enabled_power_domains;
6204 for_each_power_domain(domain, domains)
6205 intel_display_power_put(dev_priv, domain);
6206 intel_crtc->enabled_power_domains = 0;
6207}
6208
6b72d486
ML
6209/*
6210 * turn all crtc's off, but do not adjust state
6211 * This has to be paired with a call to intel_modeset_setup_hw_state.
6212 */
9716c691 6213void intel_display_suspend(struct drm_device *dev)
6b72d486 6214{
6b72d486
ML
6215 struct drm_crtc *crtc;
6216
b17d48e2
ML
6217 for_each_crtc(dev, crtc)
6218 intel_crtc_disable_noatomic(crtc);
6b72d486
ML
6219}
6220
b04c5bd6 6221/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6222int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6223{
6224 struct drm_device *dev = crtc->dev;
5da76e94
ML
6225 struct drm_mode_config *config = &dev->mode_config;
6226 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6228 struct intel_crtc_state *pipe_config;
6229 struct drm_atomic_state *state;
6230 int ret;
976f8a20 6231
1b509259 6232 if (enable == intel_crtc->active)
5da76e94 6233 return 0;
1b509259
ML
6234
6235 if (enable && !crtc->state->enable)
5da76e94 6236 return 0;
1b509259 6237
5da76e94
ML
6238 /* this function should be called with drm_modeset_lock_all for now */
6239 if (WARN_ON(!ctx))
6240 return -EIO;
6241 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6242
5da76e94
ML
6243 state = drm_atomic_state_alloc(dev);
6244 if (WARN_ON(!state))
6245 return -ENOMEM;
1b509259 6246
5da76e94
ML
6247 state->acquire_ctx = ctx;
6248 state->allow_modeset = true;
6249
6250 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6251 if (IS_ERR(pipe_config)) {
6252 ret = PTR_ERR(pipe_config);
6253 goto err;
0e572fe7 6254 }
5da76e94
ML
6255 pipe_config->base.active = enable;
6256
6257 ret = intel_set_mode(state);
6258 if (!ret)
6259 return ret;
6260
6261err:
6262 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6263 drm_atomic_state_free(state);
6264 return ret;
b04c5bd6
BF
6265}
6266
6267/**
6268 * Sets the power management mode of the pipe and plane.
6269 */
6270void intel_crtc_update_dpms(struct drm_crtc *crtc)
6271{
6272 struct drm_device *dev = crtc->dev;
6273 struct intel_encoder *intel_encoder;
6274 bool enable = false;
6275
6276 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6277 enable |= intel_encoder->connectors_active;
6278
6279 intel_crtc_control(crtc, enable);
976f8a20
DV
6280}
6281
ea5b213a 6282void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6283{
4ef69c7a 6284 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6285
ea5b213a
CW
6286 drm_encoder_cleanup(encoder);
6287 kfree(intel_encoder);
7e7d76c3
JB
6288}
6289
9237329d 6290/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6291 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6292 * state of the entire output pipe. */
9237329d 6293static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6294{
5ab432ef
DV
6295 if (mode == DRM_MODE_DPMS_ON) {
6296 encoder->connectors_active = true;
6297
b2cabb0e 6298 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6299 } else {
6300 encoder->connectors_active = false;
6301
b2cabb0e 6302 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6303 }
79e53945
JB
6304}
6305
0a91ca29
DV
6306/* Cross check the actual hw state with our own modeset state tracking (and it's
6307 * internal consistency). */
b980514c 6308static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6309{
0a91ca29
DV
6310 if (connector->get_hw_state(connector)) {
6311 struct intel_encoder *encoder = connector->encoder;
6312 struct drm_crtc *crtc;
6313 bool encoder_enabled;
6314 enum pipe pipe;
6315
6316 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6317 connector->base.base.id,
c23cc417 6318 connector->base.name);
0a91ca29 6319
0e32b39c
DA
6320 /* there is no real hw state for MST connectors */
6321 if (connector->mst_port)
6322 return;
6323
e2c719b7 6324 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6325 "wrong connector dpms state\n");
e2c719b7 6326 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6327 "active connector not linked to encoder\n");
0a91ca29 6328
36cd7444 6329 if (encoder) {
e2c719b7 6330 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6331 "encoder->connectors_active not set\n");
6332
6333 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6334 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6335 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6336 return;
0a91ca29 6337
36cd7444 6338 crtc = encoder->base.crtc;
0a91ca29 6339
83d65738
MR
6340 I915_STATE_WARN(!crtc->state->enable,
6341 "crtc not enabled\n");
e2c719b7
RC
6342 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6343 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6344 "encoder active on the wrong pipe\n");
6345 }
0a91ca29 6346 }
79e53945
JB
6347}
6348
08d9bc92
ACO
6349int intel_connector_init(struct intel_connector *connector)
6350{
6351 struct drm_connector_state *connector_state;
6352
6353 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6354 if (!connector_state)
6355 return -ENOMEM;
6356
6357 connector->base.state = connector_state;
6358 return 0;
6359}
6360
6361struct intel_connector *intel_connector_alloc(void)
6362{
6363 struct intel_connector *connector;
6364
6365 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6366 if (!connector)
6367 return NULL;
6368
6369 if (intel_connector_init(connector) < 0) {
6370 kfree(connector);
6371 return NULL;
6372 }
6373
6374 return connector;
6375}
6376
5ab432ef
DV
6377/* Even simpler default implementation, if there's really no special case to
6378 * consider. */
6379void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6380{
5ab432ef
DV
6381 /* All the simple cases only support two dpms states. */
6382 if (mode != DRM_MODE_DPMS_ON)
6383 mode = DRM_MODE_DPMS_OFF;
d4270e57 6384
5ab432ef
DV
6385 if (mode == connector->dpms)
6386 return;
6387
6388 connector->dpms = mode;
6389
6390 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6391 if (connector->encoder)
6392 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6393
b980514c 6394 intel_modeset_check_state(connector->dev);
79e53945
JB
6395}
6396
f0947c37
DV
6397/* Simple connector->get_hw_state implementation for encoders that support only
6398 * one connector and no cloning and hence the encoder state determines the state
6399 * of the connector. */
6400bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6401{
24929352 6402 enum pipe pipe = 0;
f0947c37 6403 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6404
f0947c37 6405 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6406}
6407
6d293983 6408static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6409{
6d293983
ACO
6410 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6411 return crtc_state->fdi_lanes;
d272ddfa
VS
6412
6413 return 0;
6414}
6415
6d293983 6416static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6417 struct intel_crtc_state *pipe_config)
1857e1da 6418{
6d293983
ACO
6419 struct drm_atomic_state *state = pipe_config->base.state;
6420 struct intel_crtc *other_crtc;
6421 struct intel_crtc_state *other_crtc_state;
6422
1857e1da
DV
6423 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6424 pipe_name(pipe), pipe_config->fdi_lanes);
6425 if (pipe_config->fdi_lanes > 4) {
6426 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6427 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6428 return -EINVAL;
1857e1da
DV
6429 }
6430
bafb6553 6431 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6432 if (pipe_config->fdi_lanes > 2) {
6433 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6434 pipe_config->fdi_lanes);
6d293983 6435 return -EINVAL;
1857e1da 6436 } else {
6d293983 6437 return 0;
1857e1da
DV
6438 }
6439 }
6440
6441 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6442 return 0;
1857e1da
DV
6443
6444 /* Ivybridge 3 pipe is really complicated */
6445 switch (pipe) {
6446 case PIPE_A:
6d293983 6447 return 0;
1857e1da 6448 case PIPE_B:
6d293983
ACO
6449 if (pipe_config->fdi_lanes <= 2)
6450 return 0;
6451
6452 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6453 other_crtc_state =
6454 intel_atomic_get_crtc_state(state, other_crtc);
6455 if (IS_ERR(other_crtc_state))
6456 return PTR_ERR(other_crtc_state);
6457
6458 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6459 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6460 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6461 return -EINVAL;
1857e1da 6462 }
6d293983 6463 return 0;
1857e1da 6464 case PIPE_C:
251cc67c
VS
6465 if (pipe_config->fdi_lanes > 2) {
6466 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6467 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6468 return -EINVAL;
251cc67c 6469 }
6d293983
ACO
6470
6471 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6472 other_crtc_state =
6473 intel_atomic_get_crtc_state(state, other_crtc);
6474 if (IS_ERR(other_crtc_state))
6475 return PTR_ERR(other_crtc_state);
6476
6477 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6478 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6479 return -EINVAL;
1857e1da 6480 }
6d293983 6481 return 0;
1857e1da
DV
6482 default:
6483 BUG();
6484 }
6485}
6486
e29c22c0
DV
6487#define RETRY 1
6488static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6489 struct intel_crtc_state *pipe_config)
877d48d5 6490{
1857e1da 6491 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6492 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6493 int lane, link_bw, fdi_dotclock, ret;
6494 bool needs_recompute = false;
877d48d5 6495
e29c22c0 6496retry:
877d48d5
DV
6497 /* FDI is a binary signal running at ~2.7GHz, encoding
6498 * each output octet as 10 bits. The actual frequency
6499 * is stored as a divider into a 100MHz clock, and the
6500 * mode pixel clock is stored in units of 1KHz.
6501 * Hence the bw of each lane in terms of the mode signal
6502 * is:
6503 */
6504 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6505
241bfc38 6506 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6507
2bd89a07 6508 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6509 pipe_config->pipe_bpp);
6510
6511 pipe_config->fdi_lanes = lane;
6512
2bd89a07 6513 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6514 link_bw, &pipe_config->fdi_m_n);
1857e1da 6515
6d293983
ACO
6516 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6517 intel_crtc->pipe, pipe_config);
6518 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6519 pipe_config->pipe_bpp -= 2*3;
6520 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6521 pipe_config->pipe_bpp);
6522 needs_recompute = true;
6523 pipe_config->bw_constrained = true;
6524
6525 goto retry;
6526 }
6527
6528 if (needs_recompute)
6529 return RETRY;
6530
6d293983 6531 return ret;
877d48d5
DV
6532}
6533
8cfb3407
VS
6534static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6535 struct intel_crtc_state *pipe_config)
6536{
6537 if (pipe_config->pipe_bpp > 24)
6538 return false;
6539
6540 /* HSW can handle pixel rate up to cdclk? */
6541 if (IS_HASWELL(dev_priv->dev))
6542 return true;
6543
6544 /*
b432e5cf
VS
6545 * We compare against max which means we must take
6546 * the increased cdclk requirement into account when
6547 * calculating the new cdclk.
6548 *
6549 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6550 */
6551 return ilk_pipe_pixel_rate(pipe_config) <=
6552 dev_priv->max_cdclk_freq * 95 / 100;
6553}
6554
42db64ef 6555static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6556 struct intel_crtc_state *pipe_config)
42db64ef 6557{
8cfb3407
VS
6558 struct drm_device *dev = crtc->base.dev;
6559 struct drm_i915_private *dev_priv = dev->dev_private;
6560
d330a953 6561 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6562 hsw_crtc_supports_ips(crtc) &&
6563 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6564}
6565
a43f6e0f 6566static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6567 struct intel_crtc_state *pipe_config)
79e53945 6568{
a43f6e0f 6569 struct drm_device *dev = crtc->base.dev;
8bd31e67 6570 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6571 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6572 int ret;
89749350 6573
ad3a4479 6574 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6575 if (INTEL_INFO(dev)->gen < 4) {
44913155 6576 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6577
6578 /*
6579 * Enable pixel doubling when the dot clock
6580 * is > 90% of the (display) core speed.
6581 *
b397c96b
VS
6582 * GDG double wide on either pipe,
6583 * otherwise pipe A only.
cf532bb2 6584 */
b397c96b 6585 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6586 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6587 clock_limit *= 2;
cf532bb2 6588 pipe_config->double_wide = true;
ad3a4479
VS
6589 }
6590
241bfc38 6591 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6592 return -EINVAL;
2c07245f 6593 }
89749350 6594
1d1d0e27
VS
6595 /*
6596 * Pipe horizontal size must be even in:
6597 * - DVO ganged mode
6598 * - LVDS dual channel mode
6599 * - Double wide pipe
6600 */
a93e255f 6601 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6602 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6603 pipe_config->pipe_src_w &= ~1;
6604
8693a824
DL
6605 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6606 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6607 */
6608 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6609 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6610 return -EINVAL;
44f46b42 6611
f5adf94e 6612 if (HAS_IPS(dev))
a43f6e0f
DV
6613 hsw_compute_ips_config(crtc, pipe_config);
6614
877d48d5 6615 if (pipe_config->has_pch_encoder)
a43f6e0f 6616 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6617
d03c93d4
CK
6618 /* FIXME: remove below call once atomic mode set is place and all crtc
6619 * related checks called from atomic_crtc_check function */
6620 ret = 0;
6621 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6622 crtc, pipe_config->base.state);
6623 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6624
6625 return ret;
79e53945
JB
6626}
6627
1652d19e
VS
6628static int skylake_get_display_clock_speed(struct drm_device *dev)
6629{
6630 struct drm_i915_private *dev_priv = to_i915(dev);
6631 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6632 uint32_t cdctl = I915_READ(CDCLK_CTL);
6633 uint32_t linkrate;
6634
414355a7 6635 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6636 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6637
6638 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6639 return 540000;
6640
6641 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6642 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6643
71cd8423
DL
6644 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6645 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6646 /* vco 8640 */
6647 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6648 case CDCLK_FREQ_450_432:
6649 return 432000;
6650 case CDCLK_FREQ_337_308:
6651 return 308570;
6652 case CDCLK_FREQ_675_617:
6653 return 617140;
6654 default:
6655 WARN(1, "Unknown cd freq selection\n");
6656 }
6657 } else {
6658 /* vco 8100 */
6659 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6660 case CDCLK_FREQ_450_432:
6661 return 450000;
6662 case CDCLK_FREQ_337_308:
6663 return 337500;
6664 case CDCLK_FREQ_675_617:
6665 return 675000;
6666 default:
6667 WARN(1, "Unknown cd freq selection\n");
6668 }
6669 }
6670
6671 /* error case, do as if DPLL0 isn't enabled */
6672 return 24000;
6673}
6674
6675static int broadwell_get_display_clock_speed(struct drm_device *dev)
6676{
6677 struct drm_i915_private *dev_priv = dev->dev_private;
6678 uint32_t lcpll = I915_READ(LCPLL_CTL);
6679 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6680
6681 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6682 return 800000;
6683 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6684 return 450000;
6685 else if (freq == LCPLL_CLK_FREQ_450)
6686 return 450000;
6687 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6688 return 540000;
6689 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6690 return 337500;
6691 else
6692 return 675000;
6693}
6694
6695static int haswell_get_display_clock_speed(struct drm_device *dev)
6696{
6697 struct drm_i915_private *dev_priv = dev->dev_private;
6698 uint32_t lcpll = I915_READ(LCPLL_CTL);
6699 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6700
6701 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6702 return 800000;
6703 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6704 return 450000;
6705 else if (freq == LCPLL_CLK_FREQ_450)
6706 return 450000;
6707 else if (IS_HSW_ULT(dev))
6708 return 337500;
6709 else
6710 return 540000;
79e53945
JB
6711}
6712
25eb05fc
JB
6713static int valleyview_get_display_clock_speed(struct drm_device *dev)
6714{
d197b7d3 6715 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6716 u32 val;
6717 int divider;
6718
6bcda4f0
VS
6719 if (dev_priv->hpll_freq == 0)
6720 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6721
a580516d 6722 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6723 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6724 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6725
6726 divider = val & DISPLAY_FREQUENCY_VALUES;
6727
7d007f40
VS
6728 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6729 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6730 "cdclk change in progress\n");
6731
6bcda4f0 6732 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6733}
6734
b37a6434
VS
6735static int ilk_get_display_clock_speed(struct drm_device *dev)
6736{
6737 return 450000;
6738}
6739
e70236a8
JB
6740static int i945_get_display_clock_speed(struct drm_device *dev)
6741{
6742 return 400000;
6743}
79e53945 6744
e70236a8 6745static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6746{
e907f170 6747 return 333333;
e70236a8 6748}
79e53945 6749
e70236a8
JB
6750static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6751{
6752 return 200000;
6753}
79e53945 6754
257a7ffc
DV
6755static int pnv_get_display_clock_speed(struct drm_device *dev)
6756{
6757 u16 gcfgc = 0;
6758
6759 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6760
6761 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6762 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6763 return 266667;
257a7ffc 6764 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6765 return 333333;
257a7ffc 6766 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6767 return 444444;
257a7ffc
DV
6768 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6769 return 200000;
6770 default:
6771 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6772 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6773 return 133333;
257a7ffc 6774 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6775 return 166667;
257a7ffc
DV
6776 }
6777}
6778
e70236a8
JB
6779static int i915gm_get_display_clock_speed(struct drm_device *dev)
6780{
6781 u16 gcfgc = 0;
79e53945 6782
e70236a8
JB
6783 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6784
6785 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6786 return 133333;
e70236a8
JB
6787 else {
6788 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6789 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6790 return 333333;
e70236a8
JB
6791 default:
6792 case GC_DISPLAY_CLOCK_190_200_MHZ:
6793 return 190000;
79e53945 6794 }
e70236a8
JB
6795 }
6796}
6797
6798static int i865_get_display_clock_speed(struct drm_device *dev)
6799{
e907f170 6800 return 266667;
e70236a8
JB
6801}
6802
1b1d2716 6803static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6804{
6805 u16 hpllcc = 0;
1b1d2716 6806
65cd2b3f
VS
6807 /*
6808 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6809 * encoding is different :(
6810 * FIXME is this the right way to detect 852GM/852GMV?
6811 */
6812 if (dev->pdev->revision == 0x1)
6813 return 133333;
6814
1b1d2716
VS
6815 pci_bus_read_config_word(dev->pdev->bus,
6816 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6817
e70236a8
JB
6818 /* Assume that the hardware is in the high speed state. This
6819 * should be the default.
6820 */
6821 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6822 case GC_CLOCK_133_200:
1b1d2716 6823 case GC_CLOCK_133_200_2:
e70236a8
JB
6824 case GC_CLOCK_100_200:
6825 return 200000;
6826 case GC_CLOCK_166_250:
6827 return 250000;
6828 case GC_CLOCK_100_133:
e907f170 6829 return 133333;
1b1d2716
VS
6830 case GC_CLOCK_133_266:
6831 case GC_CLOCK_133_266_2:
6832 case GC_CLOCK_166_266:
6833 return 266667;
e70236a8 6834 }
79e53945 6835
e70236a8
JB
6836 /* Shouldn't happen */
6837 return 0;
6838}
79e53945 6839
e70236a8
JB
6840static int i830_get_display_clock_speed(struct drm_device *dev)
6841{
e907f170 6842 return 133333;
79e53945
JB
6843}
6844
34edce2f
VS
6845static unsigned int intel_hpll_vco(struct drm_device *dev)
6846{
6847 struct drm_i915_private *dev_priv = dev->dev_private;
6848 static const unsigned int blb_vco[8] = {
6849 [0] = 3200000,
6850 [1] = 4000000,
6851 [2] = 5333333,
6852 [3] = 4800000,
6853 [4] = 6400000,
6854 };
6855 static const unsigned int pnv_vco[8] = {
6856 [0] = 3200000,
6857 [1] = 4000000,
6858 [2] = 5333333,
6859 [3] = 4800000,
6860 [4] = 2666667,
6861 };
6862 static const unsigned int cl_vco[8] = {
6863 [0] = 3200000,
6864 [1] = 4000000,
6865 [2] = 5333333,
6866 [3] = 6400000,
6867 [4] = 3333333,
6868 [5] = 3566667,
6869 [6] = 4266667,
6870 };
6871 static const unsigned int elk_vco[8] = {
6872 [0] = 3200000,
6873 [1] = 4000000,
6874 [2] = 5333333,
6875 [3] = 4800000,
6876 };
6877 static const unsigned int ctg_vco[8] = {
6878 [0] = 3200000,
6879 [1] = 4000000,
6880 [2] = 5333333,
6881 [3] = 6400000,
6882 [4] = 2666667,
6883 [5] = 4266667,
6884 };
6885 const unsigned int *vco_table;
6886 unsigned int vco;
6887 uint8_t tmp = 0;
6888
6889 /* FIXME other chipsets? */
6890 if (IS_GM45(dev))
6891 vco_table = ctg_vco;
6892 else if (IS_G4X(dev))
6893 vco_table = elk_vco;
6894 else if (IS_CRESTLINE(dev))
6895 vco_table = cl_vco;
6896 else if (IS_PINEVIEW(dev))
6897 vco_table = pnv_vco;
6898 else if (IS_G33(dev))
6899 vco_table = blb_vco;
6900 else
6901 return 0;
6902
6903 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6904
6905 vco = vco_table[tmp & 0x7];
6906 if (vco == 0)
6907 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6908 else
6909 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6910
6911 return vco;
6912}
6913
6914static int gm45_get_display_clock_speed(struct drm_device *dev)
6915{
6916 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6917 uint16_t tmp = 0;
6918
6919 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6920
6921 cdclk_sel = (tmp >> 12) & 0x1;
6922
6923 switch (vco) {
6924 case 2666667:
6925 case 4000000:
6926 case 5333333:
6927 return cdclk_sel ? 333333 : 222222;
6928 case 3200000:
6929 return cdclk_sel ? 320000 : 228571;
6930 default:
6931 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6932 return 222222;
6933 }
6934}
6935
6936static int i965gm_get_display_clock_speed(struct drm_device *dev)
6937{
6938 static const uint8_t div_3200[] = { 16, 10, 8 };
6939 static const uint8_t div_4000[] = { 20, 12, 10 };
6940 static const uint8_t div_5333[] = { 24, 16, 14 };
6941 const uint8_t *div_table;
6942 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6943 uint16_t tmp = 0;
6944
6945 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6946
6947 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6948
6949 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6950 goto fail;
6951
6952 switch (vco) {
6953 case 3200000:
6954 div_table = div_3200;
6955 break;
6956 case 4000000:
6957 div_table = div_4000;
6958 break;
6959 case 5333333:
6960 div_table = div_5333;
6961 break;
6962 default:
6963 goto fail;
6964 }
6965
6966 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6967
6968 fail:
6969 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6970 return 200000;
6971}
6972
6973static int g33_get_display_clock_speed(struct drm_device *dev)
6974{
6975 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6976 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6977 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6978 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6979 const uint8_t *div_table;
6980 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6981 uint16_t tmp = 0;
6982
6983 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6984
6985 cdclk_sel = (tmp >> 4) & 0x7;
6986
6987 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6988 goto fail;
6989
6990 switch (vco) {
6991 case 3200000:
6992 div_table = div_3200;
6993 break;
6994 case 4000000:
6995 div_table = div_4000;
6996 break;
6997 case 4800000:
6998 div_table = div_4800;
6999 break;
7000 case 5333333:
7001 div_table = div_5333;
7002 break;
7003 default:
7004 goto fail;
7005 }
7006
7007 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7008
7009 fail:
7010 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7011 return 190476;
7012}
7013
2c07245f 7014static void
a65851af 7015intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7016{
a65851af
VS
7017 while (*num > DATA_LINK_M_N_MASK ||
7018 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7019 *num >>= 1;
7020 *den >>= 1;
7021 }
7022}
7023
a65851af
VS
7024static void compute_m_n(unsigned int m, unsigned int n,
7025 uint32_t *ret_m, uint32_t *ret_n)
7026{
7027 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7028 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7029 intel_reduce_m_n_ratio(ret_m, ret_n);
7030}
7031
e69d0bc1
DV
7032void
7033intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7034 int pixel_clock, int link_clock,
7035 struct intel_link_m_n *m_n)
2c07245f 7036{
e69d0bc1 7037 m_n->tu = 64;
a65851af
VS
7038
7039 compute_m_n(bits_per_pixel * pixel_clock,
7040 link_clock * nlanes * 8,
7041 &m_n->gmch_m, &m_n->gmch_n);
7042
7043 compute_m_n(pixel_clock, link_clock,
7044 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7045}
7046
a7615030
CW
7047static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7048{
d330a953
JN
7049 if (i915.panel_use_ssc >= 0)
7050 return i915.panel_use_ssc != 0;
41aa3448 7051 return dev_priv->vbt.lvds_use_ssc
435793df 7052 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7053}
7054
a93e255f
ACO
7055static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7056 int num_connectors)
c65d77d8 7057{
a93e255f 7058 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7059 struct drm_i915_private *dev_priv = dev->dev_private;
7060 int refclk;
7061
a93e255f
ACO
7062 WARN_ON(!crtc_state->base.state);
7063
5ab7b0b7 7064 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7065 refclk = 100000;
a93e255f 7066 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7067 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7068 refclk = dev_priv->vbt.lvds_ssc_freq;
7069 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7070 } else if (!IS_GEN2(dev)) {
7071 refclk = 96000;
7072 } else {
7073 refclk = 48000;
7074 }
7075
7076 return refclk;
7077}
7078
7429e9d4 7079static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7080{
7df00d7a 7081 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7082}
f47709a9 7083
7429e9d4
DV
7084static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7085{
7086 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7087}
7088
f47709a9 7089static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7090 struct intel_crtc_state *crtc_state,
a7516a05
JB
7091 intel_clock_t *reduced_clock)
7092{
f47709a9 7093 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7094 u32 fp, fp2 = 0;
7095
7096 if (IS_PINEVIEW(dev)) {
190f68c5 7097 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7098 if (reduced_clock)
7429e9d4 7099 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7100 } else {
190f68c5 7101 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7102 if (reduced_clock)
7429e9d4 7103 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7104 }
7105
190f68c5 7106 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7107
f47709a9 7108 crtc->lowfreq_avail = false;
a93e255f 7109 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7110 reduced_clock) {
190f68c5 7111 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7112 crtc->lowfreq_avail = true;
a7516a05 7113 } else {
190f68c5 7114 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7115 }
7116}
7117
5e69f97f
CML
7118static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7119 pipe)
89b667f8
JB
7120{
7121 u32 reg_val;
7122
7123 /*
7124 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7125 * and set it to a reasonable value instead.
7126 */
ab3c759a 7127 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7128 reg_val &= 0xffffff00;
7129 reg_val |= 0x00000030;
ab3c759a 7130 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7131
ab3c759a 7132 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7133 reg_val &= 0x8cffffff;
7134 reg_val = 0x8c000000;
ab3c759a 7135 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7136
ab3c759a 7137 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7138 reg_val &= 0xffffff00;
ab3c759a 7139 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7140
ab3c759a 7141 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7142 reg_val &= 0x00ffffff;
7143 reg_val |= 0xb0000000;
ab3c759a 7144 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7145}
7146
b551842d
DV
7147static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7148 struct intel_link_m_n *m_n)
7149{
7150 struct drm_device *dev = crtc->base.dev;
7151 struct drm_i915_private *dev_priv = dev->dev_private;
7152 int pipe = crtc->pipe;
7153
e3b95f1e
DV
7154 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7155 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7156 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7157 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7158}
7159
7160static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7161 struct intel_link_m_n *m_n,
7162 struct intel_link_m_n *m2_n2)
b551842d
DV
7163{
7164 struct drm_device *dev = crtc->base.dev;
7165 struct drm_i915_private *dev_priv = dev->dev_private;
7166 int pipe = crtc->pipe;
6e3c9717 7167 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7168
7169 if (INTEL_INFO(dev)->gen >= 5) {
7170 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7171 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7172 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7173 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7174 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7175 * for gen < 8) and if DRRS is supported (to make sure the
7176 * registers are not unnecessarily accessed).
7177 */
44395bfe 7178 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7179 crtc->config->has_drrs) {
f769cd24
VK
7180 I915_WRITE(PIPE_DATA_M2(transcoder),
7181 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7182 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7183 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7184 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7185 }
b551842d 7186 } else {
e3b95f1e
DV
7187 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7188 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7189 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7190 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7191 }
7192}
7193
fe3cd48d 7194void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7195{
fe3cd48d
R
7196 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7197
7198 if (m_n == M1_N1) {
7199 dp_m_n = &crtc->config->dp_m_n;
7200 dp_m2_n2 = &crtc->config->dp_m2_n2;
7201 } else if (m_n == M2_N2) {
7202
7203 /*
7204 * M2_N2 registers are not supported. Hence m2_n2 divider value
7205 * needs to be programmed into M1_N1.
7206 */
7207 dp_m_n = &crtc->config->dp_m2_n2;
7208 } else {
7209 DRM_ERROR("Unsupported divider value\n");
7210 return;
7211 }
7212
6e3c9717
ACO
7213 if (crtc->config->has_pch_encoder)
7214 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7215 else
fe3cd48d 7216 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7217}
7218
d288f65f 7219static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 7220 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7221{
7222 u32 dpll, dpll_md;
7223
7224 /*
7225 * Enable DPIO clock input. We should never disable the reference
7226 * clock for pipe B, since VGA hotplug / manual detection depends
7227 * on it.
7228 */
7229 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7230 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7231 /* We should never disable this, set it here for state tracking */
7232 if (crtc->pipe == PIPE_B)
7233 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7234 dpll |= DPLL_VCO_ENABLE;
d288f65f 7235 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7236
d288f65f 7237 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7238 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7239 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7240}
7241
d288f65f 7242static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7243 const struct intel_crtc_state *pipe_config)
a0c4da24 7244{
f47709a9 7245 struct drm_device *dev = crtc->base.dev;
a0c4da24 7246 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7247 int pipe = crtc->pipe;
bdd4b6a6 7248 u32 mdiv;
a0c4da24 7249 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7250 u32 coreclk, reg_val;
a0c4da24 7251
a580516d 7252 mutex_lock(&dev_priv->sb_lock);
09153000 7253
d288f65f
VS
7254 bestn = pipe_config->dpll.n;
7255 bestm1 = pipe_config->dpll.m1;
7256 bestm2 = pipe_config->dpll.m2;
7257 bestp1 = pipe_config->dpll.p1;
7258 bestp2 = pipe_config->dpll.p2;
a0c4da24 7259
89b667f8
JB
7260 /* See eDP HDMI DPIO driver vbios notes doc */
7261
7262 /* PLL B needs special handling */
bdd4b6a6 7263 if (pipe == PIPE_B)
5e69f97f 7264 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7265
7266 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7267 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7268
7269 /* Disable target IRef on PLL */
ab3c759a 7270 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7271 reg_val &= 0x00ffffff;
ab3c759a 7272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7273
7274 /* Disable fast lock */
ab3c759a 7275 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7276
7277 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7278 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7279 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7280 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7281 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7282
7283 /*
7284 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7285 * but we don't support that).
7286 * Note: don't use the DAC post divider as it seems unstable.
7287 */
7288 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7289 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7290
a0c4da24 7291 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7293
89b667f8 7294 /* Set HBR and RBR LPF coefficients */
d288f65f 7295 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7296 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7297 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7298 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7299 0x009f0003);
89b667f8 7300 else
ab3c759a 7301 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7302 0x00d0000f);
7303
681a8504 7304 if (pipe_config->has_dp_encoder) {
89b667f8 7305 /* Use SSC source */
bdd4b6a6 7306 if (pipe == PIPE_A)
ab3c759a 7307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7308 0x0df40000);
7309 else
ab3c759a 7310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7311 0x0df70000);
7312 } else { /* HDMI or VGA */
7313 /* Use bend source */
bdd4b6a6 7314 if (pipe == PIPE_A)
ab3c759a 7315 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7316 0x0df70000);
7317 else
ab3c759a 7318 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7319 0x0df40000);
7320 }
a0c4da24 7321
ab3c759a 7322 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7323 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7324 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7325 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7326 coreclk |= 0x01000000;
ab3c759a 7327 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7328
ab3c759a 7329 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7330 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7331}
7332
d288f65f 7333static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 7334 struct intel_crtc_state *pipe_config)
1ae0d137 7335{
d288f65f 7336 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7337 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7338 DPLL_VCO_ENABLE;
7339 if (crtc->pipe != PIPE_A)
d288f65f 7340 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7341
d288f65f
VS
7342 pipe_config->dpll_hw_state.dpll_md =
7343 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7344}
7345
d288f65f 7346static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7347 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7348{
7349 struct drm_device *dev = crtc->base.dev;
7350 struct drm_i915_private *dev_priv = dev->dev_private;
7351 int pipe = crtc->pipe;
7352 int dpll_reg = DPLL(crtc->pipe);
7353 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7354 u32 loopfilter, tribuf_calcntr;
9d556c99 7355 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7356 u32 dpio_val;
9cbe40c1 7357 int vco;
9d556c99 7358
d288f65f
VS
7359 bestn = pipe_config->dpll.n;
7360 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7361 bestm1 = pipe_config->dpll.m1;
7362 bestm2 = pipe_config->dpll.m2 >> 22;
7363 bestp1 = pipe_config->dpll.p1;
7364 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7365 vco = pipe_config->dpll.vco;
a945ce7e 7366 dpio_val = 0;
9cbe40c1 7367 loopfilter = 0;
9d556c99
CML
7368
7369 /*
7370 * Enable Refclk and SSC
7371 */
a11b0703 7372 I915_WRITE(dpll_reg,
d288f65f 7373 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7374
a580516d 7375 mutex_lock(&dev_priv->sb_lock);
9d556c99 7376
9d556c99
CML
7377 /* p1 and p2 divider */
7378 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7379 5 << DPIO_CHV_S1_DIV_SHIFT |
7380 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7381 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7382 1 << DPIO_CHV_K_DIV_SHIFT);
7383
7384 /* Feedback post-divider - m2 */
7385 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7386
7387 /* Feedback refclk divider - n and m1 */
7388 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7389 DPIO_CHV_M1_DIV_BY_2 |
7390 1 << DPIO_CHV_N_DIV_SHIFT);
7391
7392 /* M2 fraction division */
a945ce7e
VP
7393 if (bestm2_frac)
7394 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7395
7396 /* M2 fraction division enable */
a945ce7e
VP
7397 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7398 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7399 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7400 if (bestm2_frac)
7401 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7402 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7403
de3a0fde
VP
7404 /* Program digital lock detect threshold */
7405 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7406 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7407 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7408 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7409 if (!bestm2_frac)
7410 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7411 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7412
9d556c99 7413 /* Loop filter */
9cbe40c1
VP
7414 if (vco == 5400000) {
7415 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7416 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7417 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7418 tribuf_calcntr = 0x9;
7419 } else if (vco <= 6200000) {
7420 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7421 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7422 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7423 tribuf_calcntr = 0x9;
7424 } else if (vco <= 6480000) {
7425 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7426 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7427 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7428 tribuf_calcntr = 0x8;
7429 } else {
7430 /* Not supported. Apply the same limits as in the max case */
7431 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7432 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7433 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7434 tribuf_calcntr = 0;
7435 }
9d556c99
CML
7436 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7437
968040b2 7438 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7439 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7440 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7441 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7442
9d556c99
CML
7443 /* AFC Recal */
7444 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7445 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7446 DPIO_AFC_RECAL);
7447
a580516d 7448 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7449}
7450
d288f65f
VS
7451/**
7452 * vlv_force_pll_on - forcibly enable just the PLL
7453 * @dev_priv: i915 private structure
7454 * @pipe: pipe PLL to enable
7455 * @dpll: PLL configuration
7456 *
7457 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7458 * in cases where we need the PLL enabled even when @pipe is not going to
7459 * be enabled.
7460 */
7461void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7462 const struct dpll *dpll)
7463{
7464 struct intel_crtc *crtc =
7465 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7466 struct intel_crtc_state pipe_config = {
a93e255f 7467 .base.crtc = &crtc->base,
d288f65f
VS
7468 .pixel_multiplier = 1,
7469 .dpll = *dpll,
7470 };
7471
7472 if (IS_CHERRYVIEW(dev)) {
7473 chv_update_pll(crtc, &pipe_config);
7474 chv_prepare_pll(crtc, &pipe_config);
7475 chv_enable_pll(crtc, &pipe_config);
7476 } else {
7477 vlv_update_pll(crtc, &pipe_config);
7478 vlv_prepare_pll(crtc, &pipe_config);
7479 vlv_enable_pll(crtc, &pipe_config);
7480 }
7481}
7482
7483/**
7484 * vlv_force_pll_off - forcibly disable just the PLL
7485 * @dev_priv: i915 private structure
7486 * @pipe: pipe PLL to disable
7487 *
7488 * Disable the PLL for @pipe. To be used in cases where we need
7489 * the PLL enabled even when @pipe is not going to be enabled.
7490 */
7491void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7492{
7493 if (IS_CHERRYVIEW(dev))
7494 chv_disable_pll(to_i915(dev), pipe);
7495 else
7496 vlv_disable_pll(to_i915(dev), pipe);
7497}
7498
f47709a9 7499static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7500 struct intel_crtc_state *crtc_state,
f47709a9 7501 intel_clock_t *reduced_clock,
eb1cbe48
DV
7502 int num_connectors)
7503{
f47709a9 7504 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7505 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7506 u32 dpll;
7507 bool is_sdvo;
190f68c5 7508 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7509
190f68c5 7510 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7511
a93e255f
ACO
7512 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7513 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7514
7515 dpll = DPLL_VGA_MODE_DIS;
7516
a93e255f 7517 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7518 dpll |= DPLLB_MODE_LVDS;
7519 else
7520 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7521
ef1b460d 7522 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7523 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7524 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7525 }
198a037f
DV
7526
7527 if (is_sdvo)
4a33e48d 7528 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7529
190f68c5 7530 if (crtc_state->has_dp_encoder)
4a33e48d 7531 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7532
7533 /* compute bitmask from p1 value */
7534 if (IS_PINEVIEW(dev))
7535 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7536 else {
7537 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7538 if (IS_G4X(dev) && reduced_clock)
7539 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7540 }
7541 switch (clock->p2) {
7542 case 5:
7543 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7544 break;
7545 case 7:
7546 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7547 break;
7548 case 10:
7549 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7550 break;
7551 case 14:
7552 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7553 break;
7554 }
7555 if (INTEL_INFO(dev)->gen >= 4)
7556 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7557
190f68c5 7558 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7559 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7560 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7561 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7562 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7563 else
7564 dpll |= PLL_REF_INPUT_DREFCLK;
7565
7566 dpll |= DPLL_VCO_ENABLE;
190f68c5 7567 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7568
eb1cbe48 7569 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7570 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7571 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7572 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7573 }
7574}
7575
f47709a9 7576static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7577 struct intel_crtc_state *crtc_state,
f47709a9 7578 intel_clock_t *reduced_clock,
eb1cbe48
DV
7579 int num_connectors)
7580{
f47709a9 7581 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7582 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7583 u32 dpll;
190f68c5 7584 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7585
190f68c5 7586 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7587
eb1cbe48
DV
7588 dpll = DPLL_VGA_MODE_DIS;
7589
a93e255f 7590 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7591 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7592 } else {
7593 if (clock->p1 == 2)
7594 dpll |= PLL_P1_DIVIDE_BY_TWO;
7595 else
7596 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7597 if (clock->p2 == 4)
7598 dpll |= PLL_P2_DIVIDE_BY_4;
7599 }
7600
a93e255f 7601 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7602 dpll |= DPLL_DVO_2X_MODE;
7603
a93e255f 7604 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7605 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7606 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7607 else
7608 dpll |= PLL_REF_INPUT_DREFCLK;
7609
7610 dpll |= DPLL_VCO_ENABLE;
190f68c5 7611 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7612}
7613
8a654f3b 7614static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7615{
7616 struct drm_device *dev = intel_crtc->base.dev;
7617 struct drm_i915_private *dev_priv = dev->dev_private;
7618 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7619 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7620 struct drm_display_mode *adjusted_mode =
6e3c9717 7621 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7622 uint32_t crtc_vtotal, crtc_vblank_end;
7623 int vsyncshift = 0;
4d8a62ea
DV
7624
7625 /* We need to be careful not to changed the adjusted mode, for otherwise
7626 * the hw state checker will get angry at the mismatch. */
7627 crtc_vtotal = adjusted_mode->crtc_vtotal;
7628 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7629
609aeaca 7630 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7631 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7632 crtc_vtotal -= 1;
7633 crtc_vblank_end -= 1;
609aeaca 7634
409ee761 7635 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7636 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7637 else
7638 vsyncshift = adjusted_mode->crtc_hsync_start -
7639 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7640 if (vsyncshift < 0)
7641 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7642 }
7643
7644 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7645 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7646
fe2b8f9d 7647 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7648 (adjusted_mode->crtc_hdisplay - 1) |
7649 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7650 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7651 (adjusted_mode->crtc_hblank_start - 1) |
7652 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7653 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7654 (adjusted_mode->crtc_hsync_start - 1) |
7655 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7656
fe2b8f9d 7657 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7658 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7659 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7660 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7661 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7662 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7663 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7664 (adjusted_mode->crtc_vsync_start - 1) |
7665 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7666
b5e508d4
PZ
7667 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7668 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7669 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7670 * bits. */
7671 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7672 (pipe == PIPE_B || pipe == PIPE_C))
7673 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7674
b0e77b9c
PZ
7675 /* pipesrc controls the size that is scaled from, which should
7676 * always be the user's requested size.
7677 */
7678 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7679 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7680 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7681}
7682
1bd1bd80 7683static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7684 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7685{
7686 struct drm_device *dev = crtc->base.dev;
7687 struct drm_i915_private *dev_priv = dev->dev_private;
7688 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7689 uint32_t tmp;
7690
7691 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7692 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7693 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7694 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7695 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7696 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7697 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7698 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7699 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7700
7701 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7702 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7703 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7704 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7705 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7706 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7707 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7708 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7709 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7710
7711 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7712 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7713 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7714 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7715 }
7716
7717 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7718 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7719 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7720
2d112de7
ACO
7721 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7722 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7723}
7724
f6a83288 7725void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7726 struct intel_crtc_state *pipe_config)
babea61d 7727{
2d112de7
ACO
7728 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7729 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7730 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7731 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7732
2d112de7
ACO
7733 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7734 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7735 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7736 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7737
2d112de7 7738 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7739
2d112de7
ACO
7740 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7741 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7742}
7743
84b046f3
DV
7744static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7745{
7746 struct drm_device *dev = intel_crtc->base.dev;
7747 struct drm_i915_private *dev_priv = dev->dev_private;
7748 uint32_t pipeconf;
7749
9f11a9e4 7750 pipeconf = 0;
84b046f3 7751
b6b5d049
VS
7752 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7753 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7754 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7755
6e3c9717 7756 if (intel_crtc->config->double_wide)
cf532bb2 7757 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7758
ff9ce46e
DV
7759 /* only g4x and later have fancy bpc/dither controls */
7760 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7761 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7762 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7763 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7764 PIPECONF_DITHER_TYPE_SP;
84b046f3 7765
6e3c9717 7766 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7767 case 18:
7768 pipeconf |= PIPECONF_6BPC;
7769 break;
7770 case 24:
7771 pipeconf |= PIPECONF_8BPC;
7772 break;
7773 case 30:
7774 pipeconf |= PIPECONF_10BPC;
7775 break;
7776 default:
7777 /* Case prevented by intel_choose_pipe_bpp_dither. */
7778 BUG();
84b046f3
DV
7779 }
7780 }
7781
7782 if (HAS_PIPE_CXSR(dev)) {
7783 if (intel_crtc->lowfreq_avail) {
7784 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7785 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7786 } else {
7787 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7788 }
7789 }
7790
6e3c9717 7791 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7792 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7793 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7794 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7795 else
7796 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7797 } else
84b046f3
DV
7798 pipeconf |= PIPECONF_PROGRESSIVE;
7799
6e3c9717 7800 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7801 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7802
84b046f3
DV
7803 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7804 POSTING_READ(PIPECONF(intel_crtc->pipe));
7805}
7806
190f68c5
ACO
7807static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7808 struct intel_crtc_state *crtc_state)
79e53945 7809{
c7653199 7810 struct drm_device *dev = crtc->base.dev;
79e53945 7811 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7812 int refclk, num_connectors = 0;
652c393a 7813 intel_clock_t clock, reduced_clock;
a16af721 7814 bool ok, has_reduced_clock = false;
e9fd1c02 7815 bool is_lvds = false, is_dsi = false;
5eddb70b 7816 struct intel_encoder *encoder;
d4906093 7817 const intel_limit_t *limit;
55bb9992 7818 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7819 struct drm_connector *connector;
55bb9992
ACO
7820 struct drm_connector_state *connector_state;
7821 int i;
79e53945 7822
dd3cd74a
ACO
7823 memset(&crtc_state->dpll_hw_state, 0,
7824 sizeof(crtc_state->dpll_hw_state));
7825
da3ced29 7826 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7827 if (connector_state->crtc != &crtc->base)
7828 continue;
7829
7830 encoder = to_intel_encoder(connector_state->best_encoder);
7831
5eddb70b 7832 switch (encoder->type) {
79e53945
JB
7833 case INTEL_OUTPUT_LVDS:
7834 is_lvds = true;
7835 break;
e9fd1c02
JN
7836 case INTEL_OUTPUT_DSI:
7837 is_dsi = true;
7838 break;
6847d71b
PZ
7839 default:
7840 break;
79e53945 7841 }
43565a06 7842
c751ce4f 7843 num_connectors++;
79e53945
JB
7844 }
7845
f2335330 7846 if (is_dsi)
5b18e57c 7847 return 0;
f2335330 7848
190f68c5 7849 if (!crtc_state->clock_set) {
a93e255f 7850 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7851
e9fd1c02
JN
7852 /*
7853 * Returns a set of divisors for the desired target clock with
7854 * the given refclk, or FALSE. The returned values represent
7855 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7856 * 2) / p1 / p2.
7857 */
a93e255f
ACO
7858 limit = intel_limit(crtc_state, refclk);
7859 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7860 crtc_state->port_clock,
e9fd1c02 7861 refclk, NULL, &clock);
f2335330 7862 if (!ok) {
e9fd1c02
JN
7863 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7864 return -EINVAL;
7865 }
79e53945 7866
f2335330
JN
7867 if (is_lvds && dev_priv->lvds_downclock_avail) {
7868 /*
7869 * Ensure we match the reduced clock's P to the target
7870 * clock. If the clocks don't match, we can't switch
7871 * the display clock by using the FP0/FP1. In such case
7872 * we will disable the LVDS downclock feature.
7873 */
7874 has_reduced_clock =
a93e255f 7875 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7876 dev_priv->lvds_downclock,
7877 refclk, &clock,
7878 &reduced_clock);
7879 }
7880 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7881 crtc_state->dpll.n = clock.n;
7882 crtc_state->dpll.m1 = clock.m1;
7883 crtc_state->dpll.m2 = clock.m2;
7884 crtc_state->dpll.p1 = clock.p1;
7885 crtc_state->dpll.p2 = clock.p2;
f47709a9 7886 }
7026d4ac 7887
e9fd1c02 7888 if (IS_GEN2(dev)) {
190f68c5 7889 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7890 has_reduced_clock ? &reduced_clock : NULL,
7891 num_connectors);
9d556c99 7892 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7893 chv_update_pll(crtc, crtc_state);
e9fd1c02 7894 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7895 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7896 } else {
190f68c5 7897 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7898 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7899 num_connectors);
e9fd1c02 7900 }
79e53945 7901
c8f7a0db 7902 return 0;
f564048e
EA
7903}
7904
2fa2fe9a 7905static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7906 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7907{
7908 struct drm_device *dev = crtc->base.dev;
7909 struct drm_i915_private *dev_priv = dev->dev_private;
7910 uint32_t tmp;
7911
dc9e7dec
VS
7912 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7913 return;
7914
2fa2fe9a 7915 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7916 if (!(tmp & PFIT_ENABLE))
7917 return;
2fa2fe9a 7918
06922821 7919 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7920 if (INTEL_INFO(dev)->gen < 4) {
7921 if (crtc->pipe != PIPE_B)
7922 return;
2fa2fe9a
DV
7923 } else {
7924 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7925 return;
7926 }
7927
06922821 7928 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7929 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7930 if (INTEL_INFO(dev)->gen < 5)
7931 pipe_config->gmch_pfit.lvds_border_bits =
7932 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7933}
7934
acbec814 7935static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7936 struct intel_crtc_state *pipe_config)
acbec814
JB
7937{
7938 struct drm_device *dev = crtc->base.dev;
7939 struct drm_i915_private *dev_priv = dev->dev_private;
7940 int pipe = pipe_config->cpu_transcoder;
7941 intel_clock_t clock;
7942 u32 mdiv;
662c6ecb 7943 int refclk = 100000;
acbec814 7944
f573de5a
SK
7945 /* In case of MIPI DPLL will not even be used */
7946 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7947 return;
7948
a580516d 7949 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7950 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7951 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7952
7953 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7954 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7955 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7956 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7957 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7958
f646628b 7959 vlv_clock(refclk, &clock);
acbec814 7960
f646628b
VS
7961 /* clock.dot is the fast clock */
7962 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7963}
7964
5724dbd1
DL
7965static void
7966i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7967 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7968{
7969 struct drm_device *dev = crtc->base.dev;
7970 struct drm_i915_private *dev_priv = dev->dev_private;
7971 u32 val, base, offset;
7972 int pipe = crtc->pipe, plane = crtc->plane;
7973 int fourcc, pixel_format;
6761dd31 7974 unsigned int aligned_height;
b113d5ee 7975 struct drm_framebuffer *fb;
1b842c89 7976 struct intel_framebuffer *intel_fb;
1ad292b5 7977
42a7b088
DL
7978 val = I915_READ(DSPCNTR(plane));
7979 if (!(val & DISPLAY_PLANE_ENABLE))
7980 return;
7981
d9806c9f 7982 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7983 if (!intel_fb) {
1ad292b5
JB
7984 DRM_DEBUG_KMS("failed to alloc fb\n");
7985 return;
7986 }
7987
1b842c89
DL
7988 fb = &intel_fb->base;
7989
18c5247e
DV
7990 if (INTEL_INFO(dev)->gen >= 4) {
7991 if (val & DISPPLANE_TILED) {
49af449b 7992 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7993 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7994 }
7995 }
1ad292b5
JB
7996
7997 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7998 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7999 fb->pixel_format = fourcc;
8000 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8001
8002 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8003 if (plane_config->tiling)
1ad292b5
JB
8004 offset = I915_READ(DSPTILEOFF(plane));
8005 else
8006 offset = I915_READ(DSPLINOFF(plane));
8007 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8008 } else {
8009 base = I915_READ(DSPADDR(plane));
8010 }
8011 plane_config->base = base;
8012
8013 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8014 fb->width = ((val >> 16) & 0xfff) + 1;
8015 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8016
8017 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8018 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8019
b113d5ee 8020 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8021 fb->pixel_format,
8022 fb->modifier[0]);
1ad292b5 8023
f37b5c2b 8024 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8025
2844a921
DL
8026 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8027 pipe_name(pipe), plane, fb->width, fb->height,
8028 fb->bits_per_pixel, base, fb->pitches[0],
8029 plane_config->size);
1ad292b5 8030
2d14030b 8031 plane_config->fb = intel_fb;
1ad292b5
JB
8032}
8033
70b23a98 8034static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8035 struct intel_crtc_state *pipe_config)
70b23a98
VS
8036{
8037 struct drm_device *dev = crtc->base.dev;
8038 struct drm_i915_private *dev_priv = dev->dev_private;
8039 int pipe = pipe_config->cpu_transcoder;
8040 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8041 intel_clock_t clock;
8042 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8043 int refclk = 100000;
8044
a580516d 8045 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8046 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8047 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8048 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8049 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8050 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8051
8052 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8053 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8054 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8055 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8056 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8057
8058 chv_clock(refclk, &clock);
8059
8060 /* clock.dot is the fast clock */
8061 pipe_config->port_clock = clock.dot / 5;
8062}
8063
0e8ffe1b 8064static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8065 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8066{
8067 struct drm_device *dev = crtc->base.dev;
8068 struct drm_i915_private *dev_priv = dev->dev_private;
8069 uint32_t tmp;
8070
f458ebbc
DV
8071 if (!intel_display_power_is_enabled(dev_priv,
8072 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8073 return false;
8074
e143a21c 8075 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8076 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8077
0e8ffe1b
DV
8078 tmp = I915_READ(PIPECONF(crtc->pipe));
8079 if (!(tmp & PIPECONF_ENABLE))
8080 return false;
8081
42571aef
VS
8082 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8083 switch (tmp & PIPECONF_BPC_MASK) {
8084 case PIPECONF_6BPC:
8085 pipe_config->pipe_bpp = 18;
8086 break;
8087 case PIPECONF_8BPC:
8088 pipe_config->pipe_bpp = 24;
8089 break;
8090 case PIPECONF_10BPC:
8091 pipe_config->pipe_bpp = 30;
8092 break;
8093 default:
8094 break;
8095 }
8096 }
8097
b5a9fa09
DV
8098 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8099 pipe_config->limited_color_range = true;
8100
282740f7
VS
8101 if (INTEL_INFO(dev)->gen < 4)
8102 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8103
1bd1bd80
DV
8104 intel_get_pipe_timings(crtc, pipe_config);
8105
2fa2fe9a
DV
8106 i9xx_get_pfit_config(crtc, pipe_config);
8107
6c49f241
DV
8108 if (INTEL_INFO(dev)->gen >= 4) {
8109 tmp = I915_READ(DPLL_MD(crtc->pipe));
8110 pipe_config->pixel_multiplier =
8111 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8112 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8113 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8114 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8115 tmp = I915_READ(DPLL(crtc->pipe));
8116 pipe_config->pixel_multiplier =
8117 ((tmp & SDVO_MULTIPLIER_MASK)
8118 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8119 } else {
8120 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8121 * port and will be fixed up in the encoder->get_config
8122 * function. */
8123 pipe_config->pixel_multiplier = 1;
8124 }
8bcc2795
DV
8125 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8126 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8127 /*
8128 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8129 * on 830. Filter it out here so that we don't
8130 * report errors due to that.
8131 */
8132 if (IS_I830(dev))
8133 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8134
8bcc2795
DV
8135 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8136 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8137 } else {
8138 /* Mask out read-only status bits. */
8139 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8140 DPLL_PORTC_READY_MASK |
8141 DPLL_PORTB_READY_MASK);
8bcc2795 8142 }
6c49f241 8143
70b23a98
VS
8144 if (IS_CHERRYVIEW(dev))
8145 chv_crtc_clock_get(crtc, pipe_config);
8146 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8147 vlv_crtc_clock_get(crtc, pipe_config);
8148 else
8149 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8150
0e8ffe1b
DV
8151 return true;
8152}
8153
dde86e2d 8154static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8155{
8156 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8157 struct intel_encoder *encoder;
74cfd7ac 8158 u32 val, final;
13d83a67 8159 bool has_lvds = false;
199e5d79 8160 bool has_cpu_edp = false;
199e5d79 8161 bool has_panel = false;
99eb6a01
KP
8162 bool has_ck505 = false;
8163 bool can_ssc = false;
13d83a67
JB
8164
8165 /* We need to take the global config into account */
b2784e15 8166 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8167 switch (encoder->type) {
8168 case INTEL_OUTPUT_LVDS:
8169 has_panel = true;
8170 has_lvds = true;
8171 break;
8172 case INTEL_OUTPUT_EDP:
8173 has_panel = true;
2de6905f 8174 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8175 has_cpu_edp = true;
8176 break;
6847d71b
PZ
8177 default:
8178 break;
13d83a67
JB
8179 }
8180 }
8181
99eb6a01 8182 if (HAS_PCH_IBX(dev)) {
41aa3448 8183 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8184 can_ssc = has_ck505;
8185 } else {
8186 has_ck505 = false;
8187 can_ssc = true;
8188 }
8189
2de6905f
ID
8190 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8191 has_panel, has_lvds, has_ck505);
13d83a67
JB
8192
8193 /* Ironlake: try to setup display ref clock before DPLL
8194 * enabling. This is only under driver's control after
8195 * PCH B stepping, previous chipset stepping should be
8196 * ignoring this setting.
8197 */
74cfd7ac
CW
8198 val = I915_READ(PCH_DREF_CONTROL);
8199
8200 /* As we must carefully and slowly disable/enable each source in turn,
8201 * compute the final state we want first and check if we need to
8202 * make any changes at all.
8203 */
8204 final = val;
8205 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8206 if (has_ck505)
8207 final |= DREF_NONSPREAD_CK505_ENABLE;
8208 else
8209 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8210
8211 final &= ~DREF_SSC_SOURCE_MASK;
8212 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8213 final &= ~DREF_SSC1_ENABLE;
8214
8215 if (has_panel) {
8216 final |= DREF_SSC_SOURCE_ENABLE;
8217
8218 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8219 final |= DREF_SSC1_ENABLE;
8220
8221 if (has_cpu_edp) {
8222 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8223 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8224 else
8225 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8226 } else
8227 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8228 } else {
8229 final |= DREF_SSC_SOURCE_DISABLE;
8230 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8231 }
8232
8233 if (final == val)
8234 return;
8235
13d83a67 8236 /* Always enable nonspread source */
74cfd7ac 8237 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8238
99eb6a01 8239 if (has_ck505)
74cfd7ac 8240 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8241 else
74cfd7ac 8242 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8243
199e5d79 8244 if (has_panel) {
74cfd7ac
CW
8245 val &= ~DREF_SSC_SOURCE_MASK;
8246 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8247
199e5d79 8248 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8249 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8250 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8251 val |= DREF_SSC1_ENABLE;
e77166b5 8252 } else
74cfd7ac 8253 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8254
8255 /* Get SSC going before enabling the outputs */
74cfd7ac 8256 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8257 POSTING_READ(PCH_DREF_CONTROL);
8258 udelay(200);
8259
74cfd7ac 8260 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8261
8262 /* Enable CPU source on CPU attached eDP */
199e5d79 8263 if (has_cpu_edp) {
99eb6a01 8264 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8265 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8266 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8267 } else
74cfd7ac 8268 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8269 } else
74cfd7ac 8270 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8271
74cfd7ac 8272 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8273 POSTING_READ(PCH_DREF_CONTROL);
8274 udelay(200);
8275 } else {
8276 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8277
74cfd7ac 8278 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8279
8280 /* Turn off CPU output */
74cfd7ac 8281 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8282
74cfd7ac 8283 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8284 POSTING_READ(PCH_DREF_CONTROL);
8285 udelay(200);
8286
8287 /* Turn off the SSC source */
74cfd7ac
CW
8288 val &= ~DREF_SSC_SOURCE_MASK;
8289 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8290
8291 /* Turn off SSC1 */
74cfd7ac 8292 val &= ~DREF_SSC1_ENABLE;
199e5d79 8293
74cfd7ac 8294 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8295 POSTING_READ(PCH_DREF_CONTROL);
8296 udelay(200);
8297 }
74cfd7ac
CW
8298
8299 BUG_ON(val != final);
13d83a67
JB
8300}
8301
f31f2d55 8302static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8303{
f31f2d55 8304 uint32_t tmp;
dde86e2d 8305
0ff066a9
PZ
8306 tmp = I915_READ(SOUTH_CHICKEN2);
8307 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8308 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8309
0ff066a9
PZ
8310 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8311 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8312 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8313
0ff066a9
PZ
8314 tmp = I915_READ(SOUTH_CHICKEN2);
8315 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8316 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8317
0ff066a9
PZ
8318 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8319 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8320 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8321}
8322
8323/* WaMPhyProgramming:hsw */
8324static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8325{
8326 uint32_t tmp;
dde86e2d
PZ
8327
8328 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8329 tmp &= ~(0xFF << 24);
8330 tmp |= (0x12 << 24);
8331 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8332
dde86e2d
PZ
8333 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8334 tmp |= (1 << 11);
8335 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8336
8337 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8338 tmp |= (1 << 11);
8339 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8340
dde86e2d
PZ
8341 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8342 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8343 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8344
8345 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8346 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8347 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8348
0ff066a9
PZ
8349 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8350 tmp &= ~(7 << 13);
8351 tmp |= (5 << 13);
8352 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8353
0ff066a9
PZ
8354 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8355 tmp &= ~(7 << 13);
8356 tmp |= (5 << 13);
8357 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8358
8359 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8360 tmp &= ~0xFF;
8361 tmp |= 0x1C;
8362 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8363
8364 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8365 tmp &= ~0xFF;
8366 tmp |= 0x1C;
8367 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8368
8369 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8370 tmp &= ~(0xFF << 16);
8371 tmp |= (0x1C << 16);
8372 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8373
8374 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8375 tmp &= ~(0xFF << 16);
8376 tmp |= (0x1C << 16);
8377 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8378
0ff066a9
PZ
8379 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8380 tmp |= (1 << 27);
8381 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8382
0ff066a9
PZ
8383 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8384 tmp |= (1 << 27);
8385 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8386
0ff066a9
PZ
8387 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8388 tmp &= ~(0xF << 28);
8389 tmp |= (4 << 28);
8390 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8391
0ff066a9
PZ
8392 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8393 tmp &= ~(0xF << 28);
8394 tmp |= (4 << 28);
8395 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8396}
8397
2fa86a1f
PZ
8398/* Implements 3 different sequences from BSpec chapter "Display iCLK
8399 * Programming" based on the parameters passed:
8400 * - Sequence to enable CLKOUT_DP
8401 * - Sequence to enable CLKOUT_DP without spread
8402 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8403 */
8404static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8405 bool with_fdi)
f31f2d55
PZ
8406{
8407 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8408 uint32_t reg, tmp;
8409
8410 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8411 with_spread = true;
8412 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8413 with_fdi, "LP PCH doesn't have FDI\n"))
8414 with_fdi = false;
f31f2d55 8415
a580516d 8416 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8417
8418 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8419 tmp &= ~SBI_SSCCTL_DISABLE;
8420 tmp |= SBI_SSCCTL_PATHALT;
8421 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8422
8423 udelay(24);
8424
2fa86a1f
PZ
8425 if (with_spread) {
8426 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8427 tmp &= ~SBI_SSCCTL_PATHALT;
8428 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8429
2fa86a1f
PZ
8430 if (with_fdi) {
8431 lpt_reset_fdi_mphy(dev_priv);
8432 lpt_program_fdi_mphy(dev_priv);
8433 }
8434 }
dde86e2d 8435
2fa86a1f
PZ
8436 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8437 SBI_GEN0 : SBI_DBUFF0;
8438 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8439 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8440 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8441
a580516d 8442 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8443}
8444
47701c3b
PZ
8445/* Sequence to disable CLKOUT_DP */
8446static void lpt_disable_clkout_dp(struct drm_device *dev)
8447{
8448 struct drm_i915_private *dev_priv = dev->dev_private;
8449 uint32_t reg, tmp;
8450
a580516d 8451 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8452
8453 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8454 SBI_GEN0 : SBI_DBUFF0;
8455 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8456 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8457 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8458
8459 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8460 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8461 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8462 tmp |= SBI_SSCCTL_PATHALT;
8463 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8464 udelay(32);
8465 }
8466 tmp |= SBI_SSCCTL_DISABLE;
8467 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8468 }
8469
a580516d 8470 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8471}
8472
bf8fa3d3
PZ
8473static void lpt_init_pch_refclk(struct drm_device *dev)
8474{
bf8fa3d3
PZ
8475 struct intel_encoder *encoder;
8476 bool has_vga = false;
8477
b2784e15 8478 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8479 switch (encoder->type) {
8480 case INTEL_OUTPUT_ANALOG:
8481 has_vga = true;
8482 break;
6847d71b
PZ
8483 default:
8484 break;
bf8fa3d3
PZ
8485 }
8486 }
8487
47701c3b
PZ
8488 if (has_vga)
8489 lpt_enable_clkout_dp(dev, true, true);
8490 else
8491 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8492}
8493
dde86e2d
PZ
8494/*
8495 * Initialize reference clocks when the driver loads
8496 */
8497void intel_init_pch_refclk(struct drm_device *dev)
8498{
8499 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8500 ironlake_init_pch_refclk(dev);
8501 else if (HAS_PCH_LPT(dev))
8502 lpt_init_pch_refclk(dev);
8503}
8504
55bb9992 8505static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8506{
55bb9992 8507 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8508 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8509 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8510 struct drm_connector *connector;
55bb9992 8511 struct drm_connector_state *connector_state;
d9d444cb 8512 struct intel_encoder *encoder;
55bb9992 8513 int num_connectors = 0, i;
d9d444cb
JB
8514 bool is_lvds = false;
8515
da3ced29 8516 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8517 if (connector_state->crtc != crtc_state->base.crtc)
8518 continue;
8519
8520 encoder = to_intel_encoder(connector_state->best_encoder);
8521
d9d444cb
JB
8522 switch (encoder->type) {
8523 case INTEL_OUTPUT_LVDS:
8524 is_lvds = true;
8525 break;
6847d71b
PZ
8526 default:
8527 break;
d9d444cb
JB
8528 }
8529 num_connectors++;
8530 }
8531
8532 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8533 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8534 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8535 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8536 }
8537
8538 return 120000;
8539}
8540
6ff93609 8541static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8542{
c8203565 8543 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8545 int pipe = intel_crtc->pipe;
c8203565
PZ
8546 uint32_t val;
8547
78114071 8548 val = 0;
c8203565 8549
6e3c9717 8550 switch (intel_crtc->config->pipe_bpp) {
c8203565 8551 case 18:
dfd07d72 8552 val |= PIPECONF_6BPC;
c8203565
PZ
8553 break;
8554 case 24:
dfd07d72 8555 val |= PIPECONF_8BPC;
c8203565
PZ
8556 break;
8557 case 30:
dfd07d72 8558 val |= PIPECONF_10BPC;
c8203565
PZ
8559 break;
8560 case 36:
dfd07d72 8561 val |= PIPECONF_12BPC;
c8203565
PZ
8562 break;
8563 default:
cc769b62
PZ
8564 /* Case prevented by intel_choose_pipe_bpp_dither. */
8565 BUG();
c8203565
PZ
8566 }
8567
6e3c9717 8568 if (intel_crtc->config->dither)
c8203565
PZ
8569 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8570
6e3c9717 8571 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8572 val |= PIPECONF_INTERLACED_ILK;
8573 else
8574 val |= PIPECONF_PROGRESSIVE;
8575
6e3c9717 8576 if (intel_crtc->config->limited_color_range)
3685a8f3 8577 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8578
c8203565
PZ
8579 I915_WRITE(PIPECONF(pipe), val);
8580 POSTING_READ(PIPECONF(pipe));
8581}
8582
86d3efce
VS
8583/*
8584 * Set up the pipe CSC unit.
8585 *
8586 * Currently only full range RGB to limited range RGB conversion
8587 * is supported, but eventually this should handle various
8588 * RGB<->YCbCr scenarios as well.
8589 */
50f3b016 8590static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8591{
8592 struct drm_device *dev = crtc->dev;
8593 struct drm_i915_private *dev_priv = dev->dev_private;
8594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8595 int pipe = intel_crtc->pipe;
8596 uint16_t coeff = 0x7800; /* 1.0 */
8597
8598 /*
8599 * TODO: Check what kind of values actually come out of the pipe
8600 * with these coeff/postoff values and adjust to get the best
8601 * accuracy. Perhaps we even need to take the bpc value into
8602 * consideration.
8603 */
8604
6e3c9717 8605 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8606 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8607
8608 /*
8609 * GY/GU and RY/RU should be the other way around according
8610 * to BSpec, but reality doesn't agree. Just set them up in
8611 * a way that results in the correct picture.
8612 */
8613 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8614 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8615
8616 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8617 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8618
8619 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8620 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8621
8622 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8623 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8624 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8625
8626 if (INTEL_INFO(dev)->gen > 6) {
8627 uint16_t postoff = 0;
8628
6e3c9717 8629 if (intel_crtc->config->limited_color_range)
32cf0cb0 8630 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8631
8632 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8633 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8634 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8635
8636 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8637 } else {
8638 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8639
6e3c9717 8640 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8641 mode |= CSC_BLACK_SCREEN_OFFSET;
8642
8643 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8644 }
8645}
8646
6ff93609 8647static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8648{
756f85cf
PZ
8649 struct drm_device *dev = crtc->dev;
8650 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8652 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8653 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8654 uint32_t val;
8655
3eff4faa 8656 val = 0;
ee2b0b38 8657
6e3c9717 8658 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8659 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8660
6e3c9717 8661 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8662 val |= PIPECONF_INTERLACED_ILK;
8663 else
8664 val |= PIPECONF_PROGRESSIVE;
8665
702e7a56
PZ
8666 I915_WRITE(PIPECONF(cpu_transcoder), val);
8667 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8668
8669 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8670 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8671
3cdf122c 8672 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8673 val = 0;
8674
6e3c9717 8675 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8676 case 18:
8677 val |= PIPEMISC_DITHER_6_BPC;
8678 break;
8679 case 24:
8680 val |= PIPEMISC_DITHER_8_BPC;
8681 break;
8682 case 30:
8683 val |= PIPEMISC_DITHER_10_BPC;
8684 break;
8685 case 36:
8686 val |= PIPEMISC_DITHER_12_BPC;
8687 break;
8688 default:
8689 /* Case prevented by pipe_config_set_bpp. */
8690 BUG();
8691 }
8692
6e3c9717 8693 if (intel_crtc->config->dither)
756f85cf
PZ
8694 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8695
8696 I915_WRITE(PIPEMISC(pipe), val);
8697 }
ee2b0b38
PZ
8698}
8699
6591c6e4 8700static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8701 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8702 intel_clock_t *clock,
8703 bool *has_reduced_clock,
8704 intel_clock_t *reduced_clock)
8705{
8706 struct drm_device *dev = crtc->dev;
8707 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8708 int refclk;
d4906093 8709 const intel_limit_t *limit;
a16af721 8710 bool ret, is_lvds = false;
79e53945 8711
a93e255f 8712 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8713
55bb9992 8714 refclk = ironlake_get_refclk(crtc_state);
79e53945 8715
d4906093
ML
8716 /*
8717 * Returns a set of divisors for the desired target clock with the given
8718 * refclk, or FALSE. The returned values represent the clock equation:
8719 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8720 */
a93e255f
ACO
8721 limit = intel_limit(crtc_state, refclk);
8722 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8723 crtc_state->port_clock,
ee9300bb 8724 refclk, NULL, clock);
6591c6e4
PZ
8725 if (!ret)
8726 return false;
cda4b7d3 8727
ddc9003c 8728 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8729 /*
8730 * Ensure we match the reduced clock's P to the target clock.
8731 * If the clocks don't match, we can't switch the display clock
8732 * by using the FP0/FP1. In such case we will disable the LVDS
8733 * downclock feature.
8734 */
ee9300bb 8735 *has_reduced_clock =
a93e255f 8736 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8737 dev_priv->lvds_downclock,
8738 refclk, clock,
8739 reduced_clock);
652c393a 8740 }
61e9653f 8741
6591c6e4
PZ
8742 return true;
8743}
8744
d4b1931c
PZ
8745int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8746{
8747 /*
8748 * Account for spread spectrum to avoid
8749 * oversubscribing the link. Max center spread
8750 * is 2.5%; use 5% for safety's sake.
8751 */
8752 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8753 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8754}
8755
7429e9d4 8756static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8757{
7429e9d4 8758 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8759}
8760
de13a2e3 8761static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8762 struct intel_crtc_state *crtc_state,
7429e9d4 8763 u32 *fp,
9a7c7890 8764 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8765{
de13a2e3 8766 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8767 struct drm_device *dev = crtc->dev;
8768 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8769 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8770 struct drm_connector *connector;
55bb9992
ACO
8771 struct drm_connector_state *connector_state;
8772 struct intel_encoder *encoder;
de13a2e3 8773 uint32_t dpll;
55bb9992 8774 int factor, num_connectors = 0, i;
09ede541 8775 bool is_lvds = false, is_sdvo = false;
79e53945 8776
da3ced29 8777 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8778 if (connector_state->crtc != crtc_state->base.crtc)
8779 continue;
8780
8781 encoder = to_intel_encoder(connector_state->best_encoder);
8782
8783 switch (encoder->type) {
79e53945
JB
8784 case INTEL_OUTPUT_LVDS:
8785 is_lvds = true;
8786 break;
8787 case INTEL_OUTPUT_SDVO:
7d57382e 8788 case INTEL_OUTPUT_HDMI:
79e53945 8789 is_sdvo = true;
79e53945 8790 break;
6847d71b
PZ
8791 default:
8792 break;
79e53945 8793 }
43565a06 8794
c751ce4f 8795 num_connectors++;
79e53945 8796 }
79e53945 8797
c1858123 8798 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8799 factor = 21;
8800 if (is_lvds) {
8801 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8802 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8803 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8804 factor = 25;
190f68c5 8805 } else if (crtc_state->sdvo_tv_clock)
8febb297 8806 factor = 20;
c1858123 8807
190f68c5 8808 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8809 *fp |= FP_CB_TUNE;
2c07245f 8810
9a7c7890
DV
8811 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8812 *fp2 |= FP_CB_TUNE;
8813
5eddb70b 8814 dpll = 0;
2c07245f 8815
a07d6787
EA
8816 if (is_lvds)
8817 dpll |= DPLLB_MODE_LVDS;
8818 else
8819 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8820
190f68c5 8821 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8822 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8823
8824 if (is_sdvo)
4a33e48d 8825 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8826 if (crtc_state->has_dp_encoder)
4a33e48d 8827 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8828
a07d6787 8829 /* compute bitmask from p1 value */
190f68c5 8830 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8831 /* also FPA1 */
190f68c5 8832 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8833
190f68c5 8834 switch (crtc_state->dpll.p2) {
a07d6787
EA
8835 case 5:
8836 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8837 break;
8838 case 7:
8839 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8840 break;
8841 case 10:
8842 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8843 break;
8844 case 14:
8845 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8846 break;
79e53945
JB
8847 }
8848
b4c09f3b 8849 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8850 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8851 else
8852 dpll |= PLL_REF_INPUT_DREFCLK;
8853
959e16d6 8854 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8855}
8856
190f68c5
ACO
8857static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8858 struct intel_crtc_state *crtc_state)
de13a2e3 8859{
c7653199 8860 struct drm_device *dev = crtc->base.dev;
de13a2e3 8861 intel_clock_t clock, reduced_clock;
cbbab5bd 8862 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8863 bool ok, has_reduced_clock = false;
8b47047b 8864 bool is_lvds = false;
e2b78267 8865 struct intel_shared_dpll *pll;
de13a2e3 8866
dd3cd74a
ACO
8867 memset(&crtc_state->dpll_hw_state, 0,
8868 sizeof(crtc_state->dpll_hw_state));
8869
409ee761 8870 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8871
5dc5298b
PZ
8872 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8873 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8874
190f68c5 8875 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8876 &has_reduced_clock, &reduced_clock);
190f68c5 8877 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8878 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8879 return -EINVAL;
79e53945 8880 }
f47709a9 8881 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8882 if (!crtc_state->clock_set) {
8883 crtc_state->dpll.n = clock.n;
8884 crtc_state->dpll.m1 = clock.m1;
8885 crtc_state->dpll.m2 = clock.m2;
8886 crtc_state->dpll.p1 = clock.p1;
8887 crtc_state->dpll.p2 = clock.p2;
f47709a9 8888 }
79e53945 8889
5dc5298b 8890 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8891 if (crtc_state->has_pch_encoder) {
8892 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8893 if (has_reduced_clock)
7429e9d4 8894 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8895
190f68c5 8896 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8897 &fp, &reduced_clock,
8898 has_reduced_clock ? &fp2 : NULL);
8899
190f68c5
ACO
8900 crtc_state->dpll_hw_state.dpll = dpll;
8901 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8902 if (has_reduced_clock)
190f68c5 8903 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8904 else
190f68c5 8905 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8906
190f68c5 8907 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8908 if (pll == NULL) {
84f44ce7 8909 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8910 pipe_name(crtc->pipe));
4b645f14
JB
8911 return -EINVAL;
8912 }
3fb37703 8913 }
79e53945 8914
ab585dea 8915 if (is_lvds && has_reduced_clock)
c7653199 8916 crtc->lowfreq_avail = true;
bcd644e0 8917 else
c7653199 8918 crtc->lowfreq_avail = false;
e2b78267 8919
c8f7a0db 8920 return 0;
79e53945
JB
8921}
8922
eb14cb74
VS
8923static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8924 struct intel_link_m_n *m_n)
8925{
8926 struct drm_device *dev = crtc->base.dev;
8927 struct drm_i915_private *dev_priv = dev->dev_private;
8928 enum pipe pipe = crtc->pipe;
8929
8930 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8931 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8932 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8933 & ~TU_SIZE_MASK;
8934 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8935 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8936 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8937}
8938
8939static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8940 enum transcoder transcoder,
b95af8be
VK
8941 struct intel_link_m_n *m_n,
8942 struct intel_link_m_n *m2_n2)
72419203
DV
8943{
8944 struct drm_device *dev = crtc->base.dev;
8945 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8946 enum pipe pipe = crtc->pipe;
72419203 8947
eb14cb74
VS
8948 if (INTEL_INFO(dev)->gen >= 5) {
8949 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8950 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8951 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8952 & ~TU_SIZE_MASK;
8953 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8954 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8955 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8956 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8957 * gen < 8) and if DRRS is supported (to make sure the
8958 * registers are not unnecessarily read).
8959 */
8960 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8961 crtc->config->has_drrs) {
b95af8be
VK
8962 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8963 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8964 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8965 & ~TU_SIZE_MASK;
8966 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8967 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8968 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8969 }
eb14cb74
VS
8970 } else {
8971 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8972 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8973 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8974 & ~TU_SIZE_MASK;
8975 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8976 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8977 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8978 }
8979}
8980
8981void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8982 struct intel_crtc_state *pipe_config)
eb14cb74 8983{
681a8504 8984 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8985 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8986 else
8987 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8988 &pipe_config->dp_m_n,
8989 &pipe_config->dp_m2_n2);
eb14cb74 8990}
72419203 8991
eb14cb74 8992static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8993 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8994{
8995 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8996 &pipe_config->fdi_m_n, NULL);
72419203
DV
8997}
8998
bd2e244f 8999static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9000 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9001{
9002 struct drm_device *dev = crtc->base.dev;
9003 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9004 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9005 uint32_t ps_ctrl = 0;
9006 int id = -1;
9007 int i;
bd2e244f 9008
a1b2278e
CK
9009 /* find scaler attached to this pipe */
9010 for (i = 0; i < crtc->num_scalers; i++) {
9011 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9012 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9013 id = i;
9014 pipe_config->pch_pfit.enabled = true;
9015 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9016 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9017 break;
9018 }
9019 }
bd2e244f 9020
a1b2278e
CK
9021 scaler_state->scaler_id = id;
9022 if (id >= 0) {
9023 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9024 } else {
9025 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9026 }
9027}
9028
5724dbd1
DL
9029static void
9030skylake_get_initial_plane_config(struct intel_crtc *crtc,
9031 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9032{
9033 struct drm_device *dev = crtc->base.dev;
9034 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9035 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9036 int pipe = crtc->pipe;
9037 int fourcc, pixel_format;
6761dd31 9038 unsigned int aligned_height;
bc8d7dff 9039 struct drm_framebuffer *fb;
1b842c89 9040 struct intel_framebuffer *intel_fb;
bc8d7dff 9041
d9806c9f 9042 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9043 if (!intel_fb) {
bc8d7dff
DL
9044 DRM_DEBUG_KMS("failed to alloc fb\n");
9045 return;
9046 }
9047
1b842c89
DL
9048 fb = &intel_fb->base;
9049
bc8d7dff 9050 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9051 if (!(val & PLANE_CTL_ENABLE))
9052 goto error;
9053
bc8d7dff
DL
9054 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9055 fourcc = skl_format_to_fourcc(pixel_format,
9056 val & PLANE_CTL_ORDER_RGBX,
9057 val & PLANE_CTL_ALPHA_MASK);
9058 fb->pixel_format = fourcc;
9059 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9060
40f46283
DL
9061 tiling = val & PLANE_CTL_TILED_MASK;
9062 switch (tiling) {
9063 case PLANE_CTL_TILED_LINEAR:
9064 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9065 break;
9066 case PLANE_CTL_TILED_X:
9067 plane_config->tiling = I915_TILING_X;
9068 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9069 break;
9070 case PLANE_CTL_TILED_Y:
9071 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9072 break;
9073 case PLANE_CTL_TILED_YF:
9074 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9075 break;
9076 default:
9077 MISSING_CASE(tiling);
9078 goto error;
9079 }
9080
bc8d7dff
DL
9081 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9082 plane_config->base = base;
9083
9084 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9085
9086 val = I915_READ(PLANE_SIZE(pipe, 0));
9087 fb->height = ((val >> 16) & 0xfff) + 1;
9088 fb->width = ((val >> 0) & 0x1fff) + 1;
9089
9090 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9091 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9092 fb->pixel_format);
bc8d7dff
DL
9093 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9094
9095 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9096 fb->pixel_format,
9097 fb->modifier[0]);
bc8d7dff 9098
f37b5c2b 9099 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9100
9101 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9102 pipe_name(pipe), fb->width, fb->height,
9103 fb->bits_per_pixel, base, fb->pitches[0],
9104 plane_config->size);
9105
2d14030b 9106 plane_config->fb = intel_fb;
bc8d7dff
DL
9107 return;
9108
9109error:
9110 kfree(fb);
9111}
9112
2fa2fe9a 9113static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9114 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9115{
9116 struct drm_device *dev = crtc->base.dev;
9117 struct drm_i915_private *dev_priv = dev->dev_private;
9118 uint32_t tmp;
9119
9120 tmp = I915_READ(PF_CTL(crtc->pipe));
9121
9122 if (tmp & PF_ENABLE) {
fd4daa9c 9123 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9124 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9125 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9126
9127 /* We currently do not free assignements of panel fitters on
9128 * ivb/hsw (since we don't use the higher upscaling modes which
9129 * differentiates them) so just WARN about this case for now. */
9130 if (IS_GEN7(dev)) {
9131 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9132 PF_PIPE_SEL_IVB(crtc->pipe));
9133 }
2fa2fe9a 9134 }
79e53945
JB
9135}
9136
5724dbd1
DL
9137static void
9138ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9139 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9140{
9141 struct drm_device *dev = crtc->base.dev;
9142 struct drm_i915_private *dev_priv = dev->dev_private;
9143 u32 val, base, offset;
aeee5a49 9144 int pipe = crtc->pipe;
4c6baa59 9145 int fourcc, pixel_format;
6761dd31 9146 unsigned int aligned_height;
b113d5ee 9147 struct drm_framebuffer *fb;
1b842c89 9148 struct intel_framebuffer *intel_fb;
4c6baa59 9149
42a7b088
DL
9150 val = I915_READ(DSPCNTR(pipe));
9151 if (!(val & DISPLAY_PLANE_ENABLE))
9152 return;
9153
d9806c9f 9154 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9155 if (!intel_fb) {
4c6baa59
JB
9156 DRM_DEBUG_KMS("failed to alloc fb\n");
9157 return;
9158 }
9159
1b842c89
DL
9160 fb = &intel_fb->base;
9161
18c5247e
DV
9162 if (INTEL_INFO(dev)->gen >= 4) {
9163 if (val & DISPPLANE_TILED) {
49af449b 9164 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9165 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9166 }
9167 }
4c6baa59
JB
9168
9169 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9170 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9171 fb->pixel_format = fourcc;
9172 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9173
aeee5a49 9174 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9175 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9176 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9177 } else {
49af449b 9178 if (plane_config->tiling)
aeee5a49 9179 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9180 else
aeee5a49 9181 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9182 }
9183 plane_config->base = base;
9184
9185 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9186 fb->width = ((val >> 16) & 0xfff) + 1;
9187 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9188
9189 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9190 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9191
b113d5ee 9192 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9193 fb->pixel_format,
9194 fb->modifier[0]);
4c6baa59 9195
f37b5c2b 9196 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9197
2844a921
DL
9198 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9199 pipe_name(pipe), fb->width, fb->height,
9200 fb->bits_per_pixel, base, fb->pitches[0],
9201 plane_config->size);
b113d5ee 9202
2d14030b 9203 plane_config->fb = intel_fb;
4c6baa59
JB
9204}
9205
0e8ffe1b 9206static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9207 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9208{
9209 struct drm_device *dev = crtc->base.dev;
9210 struct drm_i915_private *dev_priv = dev->dev_private;
9211 uint32_t tmp;
9212
f458ebbc
DV
9213 if (!intel_display_power_is_enabled(dev_priv,
9214 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9215 return false;
9216
e143a21c 9217 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9218 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9219
0e8ffe1b
DV
9220 tmp = I915_READ(PIPECONF(crtc->pipe));
9221 if (!(tmp & PIPECONF_ENABLE))
9222 return false;
9223
42571aef
VS
9224 switch (tmp & PIPECONF_BPC_MASK) {
9225 case PIPECONF_6BPC:
9226 pipe_config->pipe_bpp = 18;
9227 break;
9228 case PIPECONF_8BPC:
9229 pipe_config->pipe_bpp = 24;
9230 break;
9231 case PIPECONF_10BPC:
9232 pipe_config->pipe_bpp = 30;
9233 break;
9234 case PIPECONF_12BPC:
9235 pipe_config->pipe_bpp = 36;
9236 break;
9237 default:
9238 break;
9239 }
9240
b5a9fa09
DV
9241 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9242 pipe_config->limited_color_range = true;
9243
ab9412ba 9244 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9245 struct intel_shared_dpll *pll;
9246
88adfff1
DV
9247 pipe_config->has_pch_encoder = true;
9248
627eb5a3
DV
9249 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9250 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9251 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9252
9253 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9254
c0d43d62 9255 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9256 pipe_config->shared_dpll =
9257 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9258 } else {
9259 tmp = I915_READ(PCH_DPLL_SEL);
9260 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9261 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9262 else
9263 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9264 }
66e985c0
DV
9265
9266 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9267
9268 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9269 &pipe_config->dpll_hw_state));
c93f54cf
DV
9270
9271 tmp = pipe_config->dpll_hw_state.dpll;
9272 pipe_config->pixel_multiplier =
9273 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9274 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9275
9276 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9277 } else {
9278 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9279 }
9280
1bd1bd80
DV
9281 intel_get_pipe_timings(crtc, pipe_config);
9282
2fa2fe9a
DV
9283 ironlake_get_pfit_config(crtc, pipe_config);
9284
0e8ffe1b
DV
9285 return true;
9286}
9287
be256dc7
PZ
9288static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9289{
9290 struct drm_device *dev = dev_priv->dev;
be256dc7 9291 struct intel_crtc *crtc;
be256dc7 9292
d3fcc808 9293 for_each_intel_crtc(dev, crtc)
e2c719b7 9294 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9295 pipe_name(crtc->pipe));
9296
e2c719b7
RC
9297 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9298 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9299 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9300 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9301 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9302 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9303 "CPU PWM1 enabled\n");
c5107b87 9304 if (IS_HASWELL(dev))
e2c719b7 9305 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9306 "CPU PWM2 enabled\n");
e2c719b7 9307 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9308 "PCH PWM1 enabled\n");
e2c719b7 9309 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9310 "Utility pin enabled\n");
e2c719b7 9311 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9312
9926ada1
PZ
9313 /*
9314 * In theory we can still leave IRQs enabled, as long as only the HPD
9315 * interrupts remain enabled. We used to check for that, but since it's
9316 * gen-specific and since we only disable LCPLL after we fully disable
9317 * the interrupts, the check below should be enough.
9318 */
e2c719b7 9319 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9320}
9321
9ccd5aeb
PZ
9322static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9323{
9324 struct drm_device *dev = dev_priv->dev;
9325
9326 if (IS_HASWELL(dev))
9327 return I915_READ(D_COMP_HSW);
9328 else
9329 return I915_READ(D_COMP_BDW);
9330}
9331
3c4c9b81
PZ
9332static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9333{
9334 struct drm_device *dev = dev_priv->dev;
9335
9336 if (IS_HASWELL(dev)) {
9337 mutex_lock(&dev_priv->rps.hw_lock);
9338 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9339 val))
f475dadf 9340 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9341 mutex_unlock(&dev_priv->rps.hw_lock);
9342 } else {
9ccd5aeb
PZ
9343 I915_WRITE(D_COMP_BDW, val);
9344 POSTING_READ(D_COMP_BDW);
3c4c9b81 9345 }
be256dc7
PZ
9346}
9347
9348/*
9349 * This function implements pieces of two sequences from BSpec:
9350 * - Sequence for display software to disable LCPLL
9351 * - Sequence for display software to allow package C8+
9352 * The steps implemented here are just the steps that actually touch the LCPLL
9353 * register. Callers should take care of disabling all the display engine
9354 * functions, doing the mode unset, fixing interrupts, etc.
9355 */
6ff58d53
PZ
9356static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9357 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9358{
9359 uint32_t val;
9360
9361 assert_can_disable_lcpll(dev_priv);
9362
9363 val = I915_READ(LCPLL_CTL);
9364
9365 if (switch_to_fclk) {
9366 val |= LCPLL_CD_SOURCE_FCLK;
9367 I915_WRITE(LCPLL_CTL, val);
9368
9369 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9370 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9371 DRM_ERROR("Switching to FCLK failed\n");
9372
9373 val = I915_READ(LCPLL_CTL);
9374 }
9375
9376 val |= LCPLL_PLL_DISABLE;
9377 I915_WRITE(LCPLL_CTL, val);
9378 POSTING_READ(LCPLL_CTL);
9379
9380 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9381 DRM_ERROR("LCPLL still locked\n");
9382
9ccd5aeb 9383 val = hsw_read_dcomp(dev_priv);
be256dc7 9384 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9385 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9386 ndelay(100);
9387
9ccd5aeb
PZ
9388 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9389 1))
be256dc7
PZ
9390 DRM_ERROR("D_COMP RCOMP still in progress\n");
9391
9392 if (allow_power_down) {
9393 val = I915_READ(LCPLL_CTL);
9394 val |= LCPLL_POWER_DOWN_ALLOW;
9395 I915_WRITE(LCPLL_CTL, val);
9396 POSTING_READ(LCPLL_CTL);
9397 }
9398}
9399
9400/*
9401 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9402 * source.
9403 */
6ff58d53 9404static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9405{
9406 uint32_t val;
9407
9408 val = I915_READ(LCPLL_CTL);
9409
9410 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9411 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9412 return;
9413
a8a8bd54
PZ
9414 /*
9415 * Make sure we're not on PC8 state before disabling PC8, otherwise
9416 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9417 */
59bad947 9418 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9419
be256dc7
PZ
9420 if (val & LCPLL_POWER_DOWN_ALLOW) {
9421 val &= ~LCPLL_POWER_DOWN_ALLOW;
9422 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9423 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9424 }
9425
9ccd5aeb 9426 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9427 val |= D_COMP_COMP_FORCE;
9428 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9429 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9430
9431 val = I915_READ(LCPLL_CTL);
9432 val &= ~LCPLL_PLL_DISABLE;
9433 I915_WRITE(LCPLL_CTL, val);
9434
9435 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9436 DRM_ERROR("LCPLL not locked yet\n");
9437
9438 if (val & LCPLL_CD_SOURCE_FCLK) {
9439 val = I915_READ(LCPLL_CTL);
9440 val &= ~LCPLL_CD_SOURCE_FCLK;
9441 I915_WRITE(LCPLL_CTL, val);
9442
9443 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9444 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9445 DRM_ERROR("Switching back to LCPLL failed\n");
9446 }
215733fa 9447
59bad947 9448 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9449 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9450}
9451
765dab67
PZ
9452/*
9453 * Package states C8 and deeper are really deep PC states that can only be
9454 * reached when all the devices on the system allow it, so even if the graphics
9455 * device allows PC8+, it doesn't mean the system will actually get to these
9456 * states. Our driver only allows PC8+ when going into runtime PM.
9457 *
9458 * The requirements for PC8+ are that all the outputs are disabled, the power
9459 * well is disabled and most interrupts are disabled, and these are also
9460 * requirements for runtime PM. When these conditions are met, we manually do
9461 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9462 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9463 * hang the machine.
9464 *
9465 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9466 * the state of some registers, so when we come back from PC8+ we need to
9467 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9468 * need to take care of the registers kept by RC6. Notice that this happens even
9469 * if we don't put the device in PCI D3 state (which is what currently happens
9470 * because of the runtime PM support).
9471 *
9472 * For more, read "Display Sequences for Package C8" on the hardware
9473 * documentation.
9474 */
a14cb6fc 9475void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9476{
c67a470b
PZ
9477 struct drm_device *dev = dev_priv->dev;
9478 uint32_t val;
9479
c67a470b
PZ
9480 DRM_DEBUG_KMS("Enabling package C8+\n");
9481
c67a470b
PZ
9482 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9483 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9484 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9485 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9486 }
9487
9488 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9489 hsw_disable_lcpll(dev_priv, true, true);
9490}
9491
a14cb6fc 9492void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9493{
9494 struct drm_device *dev = dev_priv->dev;
9495 uint32_t val;
9496
c67a470b
PZ
9497 DRM_DEBUG_KMS("Disabling package C8+\n");
9498
9499 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9500 lpt_init_pch_refclk(dev);
9501
9502 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9503 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9504 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9505 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9506 }
9507
9508 intel_prepare_ddi(dev);
c67a470b
PZ
9509}
9510
a821fc46 9511static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
f8437dd1 9512{
a821fc46 9513 struct drm_device *dev = old_state->dev;
f8437dd1 9514 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 9515 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
f8437dd1
VK
9516 int req_cdclk;
9517
9518 /* see the comment in valleyview_modeset_global_resources */
9519 if (WARN_ON(max_pixclk < 0))
9520 return;
9521
9522 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9523
9524 if (req_cdclk != dev_priv->cdclk_freq)
9525 broxton_set_cdclk(dev, req_cdclk);
9526}
9527
b432e5cf
VS
9528/* compute the max rate for new configuration */
9529static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9530{
9531 struct drm_device *dev = dev_priv->dev;
9532 struct intel_crtc *intel_crtc;
9533 struct drm_crtc *crtc;
9534 int max_pixel_rate = 0;
9535 int pixel_rate;
9536
9537 for_each_crtc(dev, crtc) {
9538 if (!crtc->state->enable)
9539 continue;
9540
9541 intel_crtc = to_intel_crtc(crtc);
9542 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9543
9544 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9545 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9546 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9547
9548 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9549 }
9550
9551 return max_pixel_rate;
9552}
9553
9554static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9555{
9556 struct drm_i915_private *dev_priv = dev->dev_private;
9557 uint32_t val, data;
9558 int ret;
9559
9560 if (WARN((I915_READ(LCPLL_CTL) &
9561 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9562 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9563 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9564 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9565 "trying to change cdclk frequency with cdclk not enabled\n"))
9566 return;
9567
9568 mutex_lock(&dev_priv->rps.hw_lock);
9569 ret = sandybridge_pcode_write(dev_priv,
9570 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9571 mutex_unlock(&dev_priv->rps.hw_lock);
9572 if (ret) {
9573 DRM_ERROR("failed to inform pcode about cdclk change\n");
9574 return;
9575 }
9576
9577 val = I915_READ(LCPLL_CTL);
9578 val |= LCPLL_CD_SOURCE_FCLK;
9579 I915_WRITE(LCPLL_CTL, val);
9580
9581 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9582 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9583 DRM_ERROR("Switching to FCLK failed\n");
9584
9585 val = I915_READ(LCPLL_CTL);
9586 val &= ~LCPLL_CLK_FREQ_MASK;
9587
9588 switch (cdclk) {
9589 case 450000:
9590 val |= LCPLL_CLK_FREQ_450;
9591 data = 0;
9592 break;
9593 case 540000:
9594 val |= LCPLL_CLK_FREQ_54O_BDW;
9595 data = 1;
9596 break;
9597 case 337500:
9598 val |= LCPLL_CLK_FREQ_337_5_BDW;
9599 data = 2;
9600 break;
9601 case 675000:
9602 val |= LCPLL_CLK_FREQ_675_BDW;
9603 data = 3;
9604 break;
9605 default:
9606 WARN(1, "invalid cdclk frequency\n");
9607 return;
9608 }
9609
9610 I915_WRITE(LCPLL_CTL, val);
9611
9612 val = I915_READ(LCPLL_CTL);
9613 val &= ~LCPLL_CD_SOURCE_FCLK;
9614 I915_WRITE(LCPLL_CTL, val);
9615
9616 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9617 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9618 DRM_ERROR("Switching back to LCPLL failed\n");
9619
9620 mutex_lock(&dev_priv->rps.hw_lock);
9621 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9622 mutex_unlock(&dev_priv->rps.hw_lock);
9623
9624 intel_update_cdclk(dev);
9625
9626 WARN(cdclk != dev_priv->cdclk_freq,
9627 "cdclk requested %d kHz but got %d kHz\n",
9628 cdclk, dev_priv->cdclk_freq);
9629}
9630
9631static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9632 int max_pixel_rate)
9633{
9634 int cdclk;
9635
9636 /*
9637 * FIXME should also account for plane ratio
9638 * once 64bpp pixel formats are supported.
9639 */
9640 if (max_pixel_rate > 540000)
9641 cdclk = 675000;
9642 else if (max_pixel_rate > 450000)
9643 cdclk = 540000;
9644 else if (max_pixel_rate > 337500)
9645 cdclk = 450000;
9646 else
9647 cdclk = 337500;
9648
9649 /*
9650 * FIXME move the cdclk caclulation to
9651 * compute_config() so we can fail gracegully.
9652 */
9653 if (cdclk > dev_priv->max_cdclk_freq) {
9654 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9655 cdclk, dev_priv->max_cdclk_freq);
9656 cdclk = dev_priv->max_cdclk_freq;
9657 }
9658
9659 return cdclk;
9660}
9661
9662static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9663{
9664 struct drm_i915_private *dev_priv = to_i915(state->dev);
9665 struct drm_crtc *crtc;
9666 struct drm_crtc_state *crtc_state;
9667 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9668 int cdclk, i;
9669
9670 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9671
9672 if (cdclk == dev_priv->cdclk_freq)
9673 return 0;
9674
9675 /* add all active pipes to the state */
9676 for_each_crtc(state->dev, crtc) {
9677 if (!crtc->state->enable)
9678 continue;
9679
9680 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9681 if (IS_ERR(crtc_state))
9682 return PTR_ERR(crtc_state);
9683 }
9684
9685 /* disable/enable all currently active pipes while we change cdclk */
9686 for_each_crtc_in_state(state, crtc, crtc_state, i)
9687 if (crtc_state->enable)
9688 crtc_state->mode_changed = true;
9689
9690 return 0;
9691}
9692
9693static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9694{
9695 struct drm_device *dev = state->dev;
9696 struct drm_i915_private *dev_priv = dev->dev_private;
9697 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9698 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9699
9700 if (req_cdclk != dev_priv->cdclk_freq)
9701 broadwell_set_cdclk(dev, req_cdclk);
9702}
9703
190f68c5
ACO
9704static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9705 struct intel_crtc_state *crtc_state)
09b4ddf9 9706{
190f68c5 9707 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9708 return -EINVAL;
716c2e55 9709
c7653199 9710 crtc->lowfreq_avail = false;
644cef34 9711
c8f7a0db 9712 return 0;
79e53945
JB
9713}
9714
3760b59c
S
9715static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9716 enum port port,
9717 struct intel_crtc_state *pipe_config)
9718{
9719 switch (port) {
9720 case PORT_A:
9721 pipe_config->ddi_pll_sel = SKL_DPLL0;
9722 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9723 break;
9724 case PORT_B:
9725 pipe_config->ddi_pll_sel = SKL_DPLL1;
9726 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9727 break;
9728 case PORT_C:
9729 pipe_config->ddi_pll_sel = SKL_DPLL2;
9730 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9731 break;
9732 default:
9733 DRM_ERROR("Incorrect port type\n");
9734 }
9735}
9736
96b7dfb7
S
9737static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9738 enum port port,
5cec258b 9739 struct intel_crtc_state *pipe_config)
96b7dfb7 9740{
3148ade7 9741 u32 temp, dpll_ctl1;
96b7dfb7
S
9742
9743 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9744 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9745
9746 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9747 case SKL_DPLL0:
9748 /*
9749 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9750 * of the shared DPLL framework and thus needs to be read out
9751 * separately
9752 */
9753 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9754 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9755 break;
96b7dfb7
S
9756 case SKL_DPLL1:
9757 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9758 break;
9759 case SKL_DPLL2:
9760 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9761 break;
9762 case SKL_DPLL3:
9763 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9764 break;
96b7dfb7
S
9765 }
9766}
9767
7d2c8175
DL
9768static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9769 enum port port,
5cec258b 9770 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9771{
9772 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9773
9774 switch (pipe_config->ddi_pll_sel) {
9775 case PORT_CLK_SEL_WRPLL1:
9776 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9777 break;
9778 case PORT_CLK_SEL_WRPLL2:
9779 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9780 break;
9781 }
9782}
9783
26804afd 9784static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9785 struct intel_crtc_state *pipe_config)
26804afd
DV
9786{
9787 struct drm_device *dev = crtc->base.dev;
9788 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9789 struct intel_shared_dpll *pll;
26804afd
DV
9790 enum port port;
9791 uint32_t tmp;
9792
9793 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9794
9795 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9796
96b7dfb7
S
9797 if (IS_SKYLAKE(dev))
9798 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9799 else if (IS_BROXTON(dev))
9800 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9801 else
9802 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9803
d452c5b6
DV
9804 if (pipe_config->shared_dpll >= 0) {
9805 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9806
9807 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9808 &pipe_config->dpll_hw_state));
9809 }
9810
26804afd
DV
9811 /*
9812 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9813 * DDI E. So just check whether this pipe is wired to DDI E and whether
9814 * the PCH transcoder is on.
9815 */
ca370455
DL
9816 if (INTEL_INFO(dev)->gen < 9 &&
9817 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9818 pipe_config->has_pch_encoder = true;
9819
9820 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9821 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9822 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9823
9824 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9825 }
9826}
9827
0e8ffe1b 9828static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9829 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9830{
9831 struct drm_device *dev = crtc->base.dev;
9832 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9833 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9834 uint32_t tmp;
9835
f458ebbc 9836 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9837 POWER_DOMAIN_PIPE(crtc->pipe)))
9838 return false;
9839
e143a21c 9840 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9841 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9842
eccb140b
DV
9843 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9844 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9845 enum pipe trans_edp_pipe;
9846 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9847 default:
9848 WARN(1, "unknown pipe linked to edp transcoder\n");
9849 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9850 case TRANS_DDI_EDP_INPUT_A_ON:
9851 trans_edp_pipe = PIPE_A;
9852 break;
9853 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9854 trans_edp_pipe = PIPE_B;
9855 break;
9856 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9857 trans_edp_pipe = PIPE_C;
9858 break;
9859 }
9860
9861 if (trans_edp_pipe == crtc->pipe)
9862 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9863 }
9864
f458ebbc 9865 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9866 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9867 return false;
9868
eccb140b 9869 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9870 if (!(tmp & PIPECONF_ENABLE))
9871 return false;
9872
26804afd 9873 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9874
1bd1bd80
DV
9875 intel_get_pipe_timings(crtc, pipe_config);
9876
a1b2278e
CK
9877 if (INTEL_INFO(dev)->gen >= 9) {
9878 skl_init_scalers(dev, crtc, pipe_config);
9879 }
9880
2fa2fe9a 9881 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9882
9883 if (INTEL_INFO(dev)->gen >= 9) {
9884 pipe_config->scaler_state.scaler_id = -1;
9885 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9886 }
9887
bd2e244f 9888 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9889 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9890 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9891 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9892 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9893 else
9894 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9895 }
88adfff1 9896
e59150dc
JB
9897 if (IS_HASWELL(dev))
9898 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9899 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9900
ebb69c95
CT
9901 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9902 pipe_config->pixel_multiplier =
9903 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9904 } else {
9905 pipe_config->pixel_multiplier = 1;
9906 }
6c49f241 9907
0e8ffe1b
DV
9908 return true;
9909}
9910
560b85bb
CW
9911static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9912{
9913 struct drm_device *dev = crtc->dev;
9914 struct drm_i915_private *dev_priv = dev->dev_private;
9915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9916 uint32_t cntl = 0, size = 0;
560b85bb 9917
dc41c154 9918 if (base) {
3dd512fb
MR
9919 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9920 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9921 unsigned int stride = roundup_pow_of_two(width) * 4;
9922
9923 switch (stride) {
9924 default:
9925 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9926 width, stride);
9927 stride = 256;
9928 /* fallthrough */
9929 case 256:
9930 case 512:
9931 case 1024:
9932 case 2048:
9933 break;
4b0e333e
CW
9934 }
9935
dc41c154
VS
9936 cntl |= CURSOR_ENABLE |
9937 CURSOR_GAMMA_ENABLE |
9938 CURSOR_FORMAT_ARGB |
9939 CURSOR_STRIDE(stride);
9940
9941 size = (height << 12) | width;
4b0e333e 9942 }
560b85bb 9943
dc41c154
VS
9944 if (intel_crtc->cursor_cntl != 0 &&
9945 (intel_crtc->cursor_base != base ||
9946 intel_crtc->cursor_size != size ||
9947 intel_crtc->cursor_cntl != cntl)) {
9948 /* On these chipsets we can only modify the base/size/stride
9949 * whilst the cursor is disabled.
9950 */
9951 I915_WRITE(_CURACNTR, 0);
4b0e333e 9952 POSTING_READ(_CURACNTR);
dc41c154 9953 intel_crtc->cursor_cntl = 0;
4b0e333e 9954 }
560b85bb 9955
99d1f387 9956 if (intel_crtc->cursor_base != base) {
9db4a9c7 9957 I915_WRITE(_CURABASE, base);
99d1f387
VS
9958 intel_crtc->cursor_base = base;
9959 }
4726e0b0 9960
dc41c154
VS
9961 if (intel_crtc->cursor_size != size) {
9962 I915_WRITE(CURSIZE, size);
9963 intel_crtc->cursor_size = size;
4b0e333e 9964 }
560b85bb 9965
4b0e333e 9966 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9967 I915_WRITE(_CURACNTR, cntl);
9968 POSTING_READ(_CURACNTR);
4b0e333e 9969 intel_crtc->cursor_cntl = cntl;
560b85bb 9970 }
560b85bb
CW
9971}
9972
560b85bb 9973static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9974{
9975 struct drm_device *dev = crtc->dev;
9976 struct drm_i915_private *dev_priv = dev->dev_private;
9977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9978 int pipe = intel_crtc->pipe;
4b0e333e
CW
9979 uint32_t cntl;
9980
9981 cntl = 0;
9982 if (base) {
9983 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9984 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9985 case 64:
9986 cntl |= CURSOR_MODE_64_ARGB_AX;
9987 break;
9988 case 128:
9989 cntl |= CURSOR_MODE_128_ARGB_AX;
9990 break;
9991 case 256:
9992 cntl |= CURSOR_MODE_256_ARGB_AX;
9993 break;
9994 default:
3dd512fb 9995 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9996 return;
65a21cd6 9997 }
4b0e333e 9998 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9999
10000 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10001 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 10002 }
65a21cd6 10003
8e7d688b 10004 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
10005 cntl |= CURSOR_ROTATE_180;
10006
4b0e333e
CW
10007 if (intel_crtc->cursor_cntl != cntl) {
10008 I915_WRITE(CURCNTR(pipe), cntl);
10009 POSTING_READ(CURCNTR(pipe));
10010 intel_crtc->cursor_cntl = cntl;
65a21cd6 10011 }
4b0e333e 10012
65a21cd6 10013 /* and commit changes on next vblank */
5efb3e28
VS
10014 I915_WRITE(CURBASE(pipe), base);
10015 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10016
10017 intel_crtc->cursor_base = base;
65a21cd6
JB
10018}
10019
cda4b7d3 10020/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
10021static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10022 bool on)
cda4b7d3
CW
10023{
10024 struct drm_device *dev = crtc->dev;
10025 struct drm_i915_private *dev_priv = dev->dev_private;
10026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10027 int pipe = intel_crtc->pipe;
3d7d6510
MR
10028 int x = crtc->cursor_x;
10029 int y = crtc->cursor_y;
d6e4db15 10030 u32 base = 0, pos = 0;
cda4b7d3 10031
d6e4db15 10032 if (on)
cda4b7d3 10033 base = intel_crtc->cursor_addr;
cda4b7d3 10034
6e3c9717 10035 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
10036 base = 0;
10037
6e3c9717 10038 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
10039 base = 0;
10040
10041 if (x < 0) {
3dd512fb 10042 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
10043 base = 0;
10044
10045 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10046 x = -x;
10047 }
10048 pos |= x << CURSOR_X_SHIFT;
10049
10050 if (y < 0) {
3dd512fb 10051 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
10052 base = 0;
10053
10054 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10055 y = -y;
10056 }
10057 pos |= y << CURSOR_Y_SHIFT;
10058
4b0e333e 10059 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
10060 return;
10061
5efb3e28
VS
10062 I915_WRITE(CURPOS(pipe), pos);
10063
4398ad45
VS
10064 /* ILK+ do this automagically */
10065 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 10066 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
10067 base += (intel_crtc->base.cursor->state->crtc_h *
10068 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
10069 }
10070
8ac54669 10071 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
10072 i845_update_cursor(crtc, base);
10073 else
10074 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
10075}
10076
dc41c154
VS
10077static bool cursor_size_ok(struct drm_device *dev,
10078 uint32_t width, uint32_t height)
10079{
10080 if (width == 0 || height == 0)
10081 return false;
10082
10083 /*
10084 * 845g/865g are special in that they are only limited by
10085 * the width of their cursors, the height is arbitrary up to
10086 * the precision of the register. Everything else requires
10087 * square cursors, limited to a few power-of-two sizes.
10088 */
10089 if (IS_845G(dev) || IS_I865G(dev)) {
10090 if ((width & 63) != 0)
10091 return false;
10092
10093 if (width > (IS_845G(dev) ? 64 : 512))
10094 return false;
10095
10096 if (height > 1023)
10097 return false;
10098 } else {
10099 switch (width | height) {
10100 case 256:
10101 case 128:
10102 if (IS_GEN2(dev))
10103 return false;
10104 case 64:
10105 break;
10106 default:
10107 return false;
10108 }
10109 }
10110
10111 return true;
10112}
10113
79e53945 10114static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10115 u16 *blue, uint32_t start, uint32_t size)
79e53945 10116{
7203425a 10117 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10119
7203425a 10120 for (i = start; i < end; i++) {
79e53945
JB
10121 intel_crtc->lut_r[i] = red[i] >> 8;
10122 intel_crtc->lut_g[i] = green[i] >> 8;
10123 intel_crtc->lut_b[i] = blue[i] >> 8;
10124 }
10125
10126 intel_crtc_load_lut(crtc);
10127}
10128
79e53945
JB
10129/* VESA 640x480x72Hz mode to set on the pipe */
10130static struct drm_display_mode load_detect_mode = {
10131 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10132 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10133};
10134
a8bb6818
DV
10135struct drm_framebuffer *
10136__intel_framebuffer_create(struct drm_device *dev,
10137 struct drm_mode_fb_cmd2 *mode_cmd,
10138 struct drm_i915_gem_object *obj)
d2dff872
CW
10139{
10140 struct intel_framebuffer *intel_fb;
10141 int ret;
10142
10143 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10144 if (!intel_fb) {
6ccb81f2 10145 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10146 return ERR_PTR(-ENOMEM);
10147 }
10148
10149 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10150 if (ret)
10151 goto err;
d2dff872
CW
10152
10153 return &intel_fb->base;
dd4916c5 10154err:
6ccb81f2 10155 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10156 kfree(intel_fb);
10157
10158 return ERR_PTR(ret);
d2dff872
CW
10159}
10160
b5ea642a 10161static struct drm_framebuffer *
a8bb6818
DV
10162intel_framebuffer_create(struct drm_device *dev,
10163 struct drm_mode_fb_cmd2 *mode_cmd,
10164 struct drm_i915_gem_object *obj)
10165{
10166 struct drm_framebuffer *fb;
10167 int ret;
10168
10169 ret = i915_mutex_lock_interruptible(dev);
10170 if (ret)
10171 return ERR_PTR(ret);
10172 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10173 mutex_unlock(&dev->struct_mutex);
10174
10175 return fb;
10176}
10177
d2dff872
CW
10178static u32
10179intel_framebuffer_pitch_for_width(int width, int bpp)
10180{
10181 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10182 return ALIGN(pitch, 64);
10183}
10184
10185static u32
10186intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10187{
10188 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10189 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10190}
10191
10192static struct drm_framebuffer *
10193intel_framebuffer_create_for_mode(struct drm_device *dev,
10194 struct drm_display_mode *mode,
10195 int depth, int bpp)
10196{
10197 struct drm_i915_gem_object *obj;
0fed39bd 10198 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10199
10200 obj = i915_gem_alloc_object(dev,
10201 intel_framebuffer_size_for_mode(mode, bpp));
10202 if (obj == NULL)
10203 return ERR_PTR(-ENOMEM);
10204
10205 mode_cmd.width = mode->hdisplay;
10206 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10207 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10208 bpp);
5ca0c34a 10209 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10210
10211 return intel_framebuffer_create(dev, &mode_cmd, obj);
10212}
10213
10214static struct drm_framebuffer *
10215mode_fits_in_fbdev(struct drm_device *dev,
10216 struct drm_display_mode *mode)
10217{
4520f53a 10218#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10219 struct drm_i915_private *dev_priv = dev->dev_private;
10220 struct drm_i915_gem_object *obj;
10221 struct drm_framebuffer *fb;
10222
4c0e5528 10223 if (!dev_priv->fbdev)
d2dff872
CW
10224 return NULL;
10225
4c0e5528 10226 if (!dev_priv->fbdev->fb)
d2dff872
CW
10227 return NULL;
10228
4c0e5528
DV
10229 obj = dev_priv->fbdev->fb->obj;
10230 BUG_ON(!obj);
10231
8bcd4553 10232 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10233 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10234 fb->bits_per_pixel))
d2dff872
CW
10235 return NULL;
10236
01f2c773 10237 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10238 return NULL;
10239
10240 return fb;
4520f53a
DV
10241#else
10242 return NULL;
10243#endif
d2dff872
CW
10244}
10245
d3a40d1b
ACO
10246static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10247 struct drm_crtc *crtc,
10248 struct drm_display_mode *mode,
10249 struct drm_framebuffer *fb,
10250 int x, int y)
10251{
10252 struct drm_plane_state *plane_state;
10253 int hdisplay, vdisplay;
10254 int ret;
10255
10256 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10257 if (IS_ERR(plane_state))
10258 return PTR_ERR(plane_state);
10259
10260 if (mode)
10261 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10262 else
10263 hdisplay = vdisplay = 0;
10264
10265 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10266 if (ret)
10267 return ret;
10268 drm_atomic_set_fb_for_plane(plane_state, fb);
10269 plane_state->crtc_x = 0;
10270 plane_state->crtc_y = 0;
10271 plane_state->crtc_w = hdisplay;
10272 plane_state->crtc_h = vdisplay;
10273 plane_state->src_x = x << 16;
10274 plane_state->src_y = y << 16;
10275 plane_state->src_w = hdisplay << 16;
10276 plane_state->src_h = vdisplay << 16;
10277
10278 return 0;
10279}
10280
d2434ab7 10281bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10282 struct drm_display_mode *mode,
51fd371b
RC
10283 struct intel_load_detect_pipe *old,
10284 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10285{
10286 struct intel_crtc *intel_crtc;
d2434ab7
DV
10287 struct intel_encoder *intel_encoder =
10288 intel_attached_encoder(connector);
79e53945 10289 struct drm_crtc *possible_crtc;
4ef69c7a 10290 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10291 struct drm_crtc *crtc = NULL;
10292 struct drm_device *dev = encoder->dev;
94352cf9 10293 struct drm_framebuffer *fb;
51fd371b 10294 struct drm_mode_config *config = &dev->mode_config;
83a57153 10295 struct drm_atomic_state *state = NULL;
944b0c76 10296 struct drm_connector_state *connector_state;
4be07317 10297 struct intel_crtc_state *crtc_state;
51fd371b 10298 int ret, i = -1;
79e53945 10299
d2dff872 10300 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10301 connector->base.id, connector->name,
8e329a03 10302 encoder->base.id, encoder->name);
d2dff872 10303
51fd371b
RC
10304retry:
10305 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10306 if (ret)
f7217905 10307 goto fail_unlock;
6e9f798d 10308
79e53945
JB
10309 /*
10310 * Algorithm gets a little messy:
7a5e4805 10311 *
79e53945
JB
10312 * - if the connector already has an assigned crtc, use it (but make
10313 * sure it's on first)
7a5e4805 10314 *
79e53945
JB
10315 * - try to find the first unused crtc that can drive this connector,
10316 * and use that if we find one
79e53945
JB
10317 */
10318
10319 /* See if we already have a CRTC for this connector */
10320 if (encoder->crtc) {
10321 crtc = encoder->crtc;
8261b191 10322
51fd371b 10323 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10324 if (ret)
f7217905 10325 goto fail_unlock;
4d02e2de 10326 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b 10327 if (ret)
f7217905 10328 goto fail_unlock;
7b24056b 10329
24218aac 10330 old->dpms_mode = connector->dpms;
8261b191
CW
10331 old->load_detect_temp = false;
10332
10333 /* Make sure the crtc and connector are running */
24218aac
DV
10334 if (connector->dpms != DRM_MODE_DPMS_ON)
10335 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10336
7173188d 10337 return true;
79e53945
JB
10338 }
10339
10340 /* Find an unused one (if possible) */
70e1e0ec 10341 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10342 i++;
10343 if (!(encoder->possible_crtcs & (1 << i)))
10344 continue;
83d65738 10345 if (possible_crtc->state->enable)
a459249c 10346 continue;
f7217905
ML
10347 /* This can occur when applying the pipe A quirk on resume. */
10348 if (to_intel_crtc(possible_crtc)->new_enabled)
10349 continue;
a459249c
VS
10350
10351 crtc = possible_crtc;
10352 break;
79e53945
JB
10353 }
10354
10355 /*
10356 * If we didn't find an unused CRTC, don't use any.
10357 */
10358 if (!crtc) {
7173188d 10359 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f7217905 10360 goto fail_unlock;
79e53945
JB
10361 }
10362
51fd371b
RC
10363 ret = drm_modeset_lock(&crtc->mutex, ctx);
10364 if (ret)
f7217905 10365 goto fail_unlock;
4d02e2de
DV
10366 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10367 if (ret)
f7217905
ML
10368 goto fail_unlock;
10369 intel_encoder->new_crtc = to_intel_crtc(crtc);
10370 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
10371
10372 intel_crtc = to_intel_crtc(crtc);
f7217905 10373 intel_crtc->new_enabled = true;
24218aac 10374 old->dpms_mode = connector->dpms;
8261b191 10375 old->load_detect_temp = true;
d2dff872 10376 old->release_fb = NULL;
79e53945 10377
83a57153
ACO
10378 state = drm_atomic_state_alloc(dev);
10379 if (!state)
10380 return false;
10381
10382 state->acquire_ctx = ctx;
10383
944b0c76
ACO
10384 connector_state = drm_atomic_get_connector_state(state, connector);
10385 if (IS_ERR(connector_state)) {
10386 ret = PTR_ERR(connector_state);
10387 goto fail;
10388 }
10389
10390 connector_state->crtc = crtc;
10391 connector_state->best_encoder = &intel_encoder->base;
10392
4be07317
ACO
10393 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10394 if (IS_ERR(crtc_state)) {
10395 ret = PTR_ERR(crtc_state);
10396 goto fail;
10397 }
10398
49d6fa21 10399 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10400
6492711d
CW
10401 if (!mode)
10402 mode = &load_detect_mode;
79e53945 10403
d2dff872
CW
10404 /* We need a framebuffer large enough to accommodate all accesses
10405 * that the plane may generate whilst we perform load detection.
10406 * We can not rely on the fbcon either being present (we get called
10407 * during its initialisation to detect all boot displays, or it may
10408 * not even exist) or that it is large enough to satisfy the
10409 * requested mode.
10410 */
94352cf9
DV
10411 fb = mode_fits_in_fbdev(dev, mode);
10412 if (fb == NULL) {
d2dff872 10413 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10414 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10415 old->release_fb = fb;
d2dff872
CW
10416 } else
10417 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10418 if (IS_ERR(fb)) {
d2dff872 10419 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10420 goto fail;
79e53945 10421 }
79e53945 10422
d3a40d1b
ACO
10423 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10424 if (ret)
10425 goto fail;
10426
8c7b5ccb
ACO
10427 drm_mode_copy(&crtc_state->base.mode, mode);
10428
568c634a 10429 if (intel_set_mode(state)) {
6492711d 10430 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10431 if (old->release_fb)
10432 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10433 goto fail;
79e53945 10434 }
9128b040 10435 crtc->primary->crtc = crtc;
7173188d 10436
79e53945 10437 /* let the connector get through one full cycle before testing */
9d0498a2 10438 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10439 return true;
412b61d8 10440
f7217905
ML
10441 fail:
10442 intel_crtc->new_enabled = crtc->state->enable;
10443fail_unlock:
e5d958ef
ACO
10444 drm_atomic_state_free(state);
10445 state = NULL;
83a57153 10446
51fd371b
RC
10447 if (ret == -EDEADLK) {
10448 drm_modeset_backoff(ctx);
10449 goto retry;
10450 }
10451
412b61d8 10452 return false;
79e53945
JB
10453}
10454
d2434ab7 10455void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10456 struct intel_load_detect_pipe *old,
10457 struct drm_modeset_acquire_ctx *ctx)
79e53945 10458{
83a57153 10459 struct drm_device *dev = connector->dev;
d2434ab7
DV
10460 struct intel_encoder *intel_encoder =
10461 intel_attached_encoder(connector);
4ef69c7a 10462 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10463 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10465 struct drm_atomic_state *state;
944b0c76 10466 struct drm_connector_state *connector_state;
4be07317 10467 struct intel_crtc_state *crtc_state;
d3a40d1b 10468 int ret;
79e53945 10469
d2dff872 10470 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10471 connector->base.id, connector->name,
8e329a03 10472 encoder->base.id, encoder->name);
d2dff872 10473
8261b191 10474 if (old->load_detect_temp) {
83a57153 10475 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10476 if (!state)
10477 goto fail;
83a57153
ACO
10478
10479 state->acquire_ctx = ctx;
10480
944b0c76
ACO
10481 connector_state = drm_atomic_get_connector_state(state, connector);
10482 if (IS_ERR(connector_state))
10483 goto fail;
10484
4be07317
ACO
10485 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10486 if (IS_ERR(crtc_state))
10487 goto fail;
10488
f7217905
ML
10489 to_intel_connector(connector)->new_encoder = NULL;
10490 intel_encoder->new_crtc = NULL;
10491 intel_crtc->new_enabled = false;
10492
944b0c76
ACO
10493 connector_state->best_encoder = NULL;
10494 connector_state->crtc = NULL;
10495
49d6fa21 10496 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10497
d3a40d1b
ACO
10498 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10499 0, 0);
10500 if (ret)
10501 goto fail;
10502
568c634a 10503 ret = intel_set_mode(state);
2bfb4627
ACO
10504 if (ret)
10505 goto fail;
d2dff872 10506
36206361
DV
10507 if (old->release_fb) {
10508 drm_framebuffer_unregister_private(old->release_fb);
10509 drm_framebuffer_unreference(old->release_fb);
10510 }
d2dff872 10511
0622a53c 10512 return;
79e53945
JB
10513 }
10514
c751ce4f 10515 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10516 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10517 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10518
10519 return;
10520fail:
10521 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10522 drm_atomic_state_free(state);
79e53945
JB
10523}
10524
da4a1efa 10525static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10526 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10527{
10528 struct drm_i915_private *dev_priv = dev->dev_private;
10529 u32 dpll = pipe_config->dpll_hw_state.dpll;
10530
10531 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10532 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10533 else if (HAS_PCH_SPLIT(dev))
10534 return 120000;
10535 else if (!IS_GEN2(dev))
10536 return 96000;
10537 else
10538 return 48000;
10539}
10540
79e53945 10541/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10542static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10543 struct intel_crtc_state *pipe_config)
79e53945 10544{
f1f644dc 10545 struct drm_device *dev = crtc->base.dev;
79e53945 10546 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10547 int pipe = pipe_config->cpu_transcoder;
293623f7 10548 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10549 u32 fp;
10550 intel_clock_t clock;
da4a1efa 10551 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10552
10553 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10554 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10555 else
293623f7 10556 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10557
10558 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10559 if (IS_PINEVIEW(dev)) {
10560 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10561 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10562 } else {
10563 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10564 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10565 }
10566
a6c45cf0 10567 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10568 if (IS_PINEVIEW(dev))
10569 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10570 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10571 else
10572 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10573 DPLL_FPA01_P1_POST_DIV_SHIFT);
10574
10575 switch (dpll & DPLL_MODE_MASK) {
10576 case DPLLB_MODE_DAC_SERIAL:
10577 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10578 5 : 10;
10579 break;
10580 case DPLLB_MODE_LVDS:
10581 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10582 7 : 14;
10583 break;
10584 default:
28c97730 10585 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10586 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10587 return;
79e53945
JB
10588 }
10589
ac58c3f0 10590 if (IS_PINEVIEW(dev))
da4a1efa 10591 pineview_clock(refclk, &clock);
ac58c3f0 10592 else
da4a1efa 10593 i9xx_clock(refclk, &clock);
79e53945 10594 } else {
0fb58223 10595 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10596 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10597
10598 if (is_lvds) {
10599 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10600 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10601
10602 if (lvds & LVDS_CLKB_POWER_UP)
10603 clock.p2 = 7;
10604 else
10605 clock.p2 = 14;
79e53945
JB
10606 } else {
10607 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10608 clock.p1 = 2;
10609 else {
10610 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10611 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10612 }
10613 if (dpll & PLL_P2_DIVIDE_BY_4)
10614 clock.p2 = 4;
10615 else
10616 clock.p2 = 2;
79e53945 10617 }
da4a1efa
VS
10618
10619 i9xx_clock(refclk, &clock);
79e53945
JB
10620 }
10621
18442d08
VS
10622 /*
10623 * This value includes pixel_multiplier. We will use
241bfc38 10624 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10625 * encoder's get_config() function.
10626 */
10627 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10628}
10629
6878da05
VS
10630int intel_dotclock_calculate(int link_freq,
10631 const struct intel_link_m_n *m_n)
f1f644dc 10632{
f1f644dc
JB
10633 /*
10634 * The calculation for the data clock is:
1041a02f 10635 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10636 * But we want to avoid losing precison if possible, so:
1041a02f 10637 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10638 *
10639 * and the link clock is simpler:
1041a02f 10640 * link_clock = (m * link_clock) / n
f1f644dc
JB
10641 */
10642
6878da05
VS
10643 if (!m_n->link_n)
10644 return 0;
f1f644dc 10645
6878da05
VS
10646 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10647}
f1f644dc 10648
18442d08 10649static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10650 struct intel_crtc_state *pipe_config)
6878da05
VS
10651{
10652 struct drm_device *dev = crtc->base.dev;
79e53945 10653
18442d08
VS
10654 /* read out port_clock from the DPLL */
10655 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10656
f1f644dc 10657 /*
18442d08 10658 * This value does not include pixel_multiplier.
241bfc38 10659 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10660 * agree once we know their relationship in the encoder's
10661 * get_config() function.
79e53945 10662 */
2d112de7 10663 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10664 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10665 &pipe_config->fdi_m_n);
79e53945
JB
10666}
10667
10668/** Returns the currently programmed mode of the given pipe. */
10669struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10670 struct drm_crtc *crtc)
10671{
548f245b 10672 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10674 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10675 struct drm_display_mode *mode;
5cec258b 10676 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10677 int htot = I915_READ(HTOTAL(cpu_transcoder));
10678 int hsync = I915_READ(HSYNC(cpu_transcoder));
10679 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10680 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10681 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10682
10683 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10684 if (!mode)
10685 return NULL;
10686
f1f644dc
JB
10687 /*
10688 * Construct a pipe_config sufficient for getting the clock info
10689 * back out of crtc_clock_get.
10690 *
10691 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10692 * to use a real value here instead.
10693 */
293623f7 10694 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10695 pipe_config.pixel_multiplier = 1;
293623f7
VS
10696 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10697 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10698 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10699 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10700
773ae034 10701 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10702 mode->hdisplay = (htot & 0xffff) + 1;
10703 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10704 mode->hsync_start = (hsync & 0xffff) + 1;
10705 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10706 mode->vdisplay = (vtot & 0xffff) + 1;
10707 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10708 mode->vsync_start = (vsync & 0xffff) + 1;
10709 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10710
10711 drm_mode_set_name(mode);
79e53945
JB
10712
10713 return mode;
10714}
10715
652c393a
JB
10716static void intel_decrease_pllclock(struct drm_crtc *crtc)
10717{
10718 struct drm_device *dev = crtc->dev;
fbee40df 10719 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10721
baff296c 10722 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10723 return;
10724
10725 if (!dev_priv->lvds_downclock_avail)
10726 return;
10727
10728 /*
10729 * Since this is called by a timer, we should never get here in
10730 * the manual case.
10731 */
10732 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10733 int pipe = intel_crtc->pipe;
10734 int dpll_reg = DPLL(pipe);
10735 int dpll;
f6e5b160 10736
44d98a61 10737 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10738
8ac5a6d5 10739 assert_panel_unlocked(dev_priv, pipe);
652c393a 10740
dc257cf1 10741 dpll = I915_READ(dpll_reg);
652c393a
JB
10742 dpll |= DISPLAY_RATE_SELECT_FPA1;
10743 I915_WRITE(dpll_reg, dpll);
9d0498a2 10744 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10745 dpll = I915_READ(dpll_reg);
10746 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10747 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10748 }
10749
10750}
10751
f047e395
CW
10752void intel_mark_busy(struct drm_device *dev)
10753{
c67a470b
PZ
10754 struct drm_i915_private *dev_priv = dev->dev_private;
10755
f62a0076
CW
10756 if (dev_priv->mm.busy)
10757 return;
10758
43694d69 10759 intel_runtime_pm_get(dev_priv);
c67a470b 10760 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10761 if (INTEL_INFO(dev)->gen >= 6)
10762 gen6_rps_busy(dev_priv);
f62a0076 10763 dev_priv->mm.busy = true;
f047e395
CW
10764}
10765
10766void intel_mark_idle(struct drm_device *dev)
652c393a 10767{
c67a470b 10768 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10769 struct drm_crtc *crtc;
652c393a 10770
f62a0076
CW
10771 if (!dev_priv->mm.busy)
10772 return;
10773
10774 dev_priv->mm.busy = false;
10775
70e1e0ec 10776 for_each_crtc(dev, crtc) {
f4510a27 10777 if (!crtc->primary->fb)
652c393a
JB
10778 continue;
10779
725a5b54 10780 intel_decrease_pllclock(crtc);
652c393a 10781 }
b29c19b6 10782
3d13ef2e 10783 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10784 gen6_rps_idle(dev->dev_private);
bb4cdd53 10785
43694d69 10786 intel_runtime_pm_put(dev_priv);
652c393a
JB
10787}
10788
79e53945
JB
10789static void intel_crtc_destroy(struct drm_crtc *crtc)
10790{
10791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10792 struct drm_device *dev = crtc->dev;
10793 struct intel_unpin_work *work;
67e77c5a 10794
5e2d7afc 10795 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10796 work = intel_crtc->unpin_work;
10797 intel_crtc->unpin_work = NULL;
5e2d7afc 10798 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10799
10800 if (work) {
10801 cancel_work_sync(&work->work);
10802 kfree(work);
10803 }
79e53945
JB
10804
10805 drm_crtc_cleanup(crtc);
67e77c5a 10806
79e53945
JB
10807 kfree(intel_crtc);
10808}
10809
6b95a207
KH
10810static void intel_unpin_work_fn(struct work_struct *__work)
10811{
10812 struct intel_unpin_work *work =
10813 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10814 struct drm_device *dev = work->crtc->dev;
f99d7069 10815 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10816
b4a98e57 10817 mutex_lock(&dev->struct_mutex);
82bc3b2d 10818 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10819 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10820
7ff0ebcc 10821 intel_fbc_update(dev);
f06cc1b9
JH
10822
10823 if (work->flip_queued_req)
146d84f0 10824 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10825 mutex_unlock(&dev->struct_mutex);
10826
f99d7069 10827 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10828 drm_framebuffer_unreference(work->old_fb);
f99d7069 10829
b4a98e57
CW
10830 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10831 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10832
6b95a207
KH
10833 kfree(work);
10834}
10835
1afe3e9d 10836static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10837 struct drm_crtc *crtc)
6b95a207 10838{
6b95a207
KH
10839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10840 struct intel_unpin_work *work;
6b95a207
KH
10841 unsigned long flags;
10842
10843 /* Ignore early vblank irqs */
10844 if (intel_crtc == NULL)
10845 return;
10846
f326038a
DV
10847 /*
10848 * This is called both by irq handlers and the reset code (to complete
10849 * lost pageflips) so needs the full irqsave spinlocks.
10850 */
6b95a207
KH
10851 spin_lock_irqsave(&dev->event_lock, flags);
10852 work = intel_crtc->unpin_work;
e7d841ca
CW
10853
10854 /* Ensure we don't miss a work->pending update ... */
10855 smp_rmb();
10856
10857 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10858 spin_unlock_irqrestore(&dev->event_lock, flags);
10859 return;
10860 }
10861
d6bbafa1 10862 page_flip_completed(intel_crtc);
0af7e4df 10863
6b95a207 10864 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10865}
10866
1afe3e9d
JB
10867void intel_finish_page_flip(struct drm_device *dev, int pipe)
10868{
fbee40df 10869 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10870 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10871
49b14a5c 10872 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10873}
10874
10875void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10876{
fbee40df 10877 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10878 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10879
49b14a5c 10880 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10881}
10882
75f7f3ec
VS
10883/* Is 'a' after or equal to 'b'? */
10884static bool g4x_flip_count_after_eq(u32 a, u32 b)
10885{
10886 return !((a - b) & 0x80000000);
10887}
10888
10889static bool page_flip_finished(struct intel_crtc *crtc)
10890{
10891 struct drm_device *dev = crtc->base.dev;
10892 struct drm_i915_private *dev_priv = dev->dev_private;
10893
bdfa7542
VS
10894 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10895 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10896 return true;
10897
75f7f3ec
VS
10898 /*
10899 * The relevant registers doen't exist on pre-ctg.
10900 * As the flip done interrupt doesn't trigger for mmio
10901 * flips on gmch platforms, a flip count check isn't
10902 * really needed there. But since ctg has the registers,
10903 * include it in the check anyway.
10904 */
10905 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10906 return true;
10907
10908 /*
10909 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10910 * used the same base address. In that case the mmio flip might
10911 * have completed, but the CS hasn't even executed the flip yet.
10912 *
10913 * A flip count check isn't enough as the CS might have updated
10914 * the base address just after start of vblank, but before we
10915 * managed to process the interrupt. This means we'd complete the
10916 * CS flip too soon.
10917 *
10918 * Combining both checks should get us a good enough result. It may
10919 * still happen that the CS flip has been executed, but has not
10920 * yet actually completed. But in case the base address is the same
10921 * anyway, we don't really care.
10922 */
10923 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10924 crtc->unpin_work->gtt_offset &&
10925 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10926 crtc->unpin_work->flip_count);
10927}
10928
6b95a207
KH
10929void intel_prepare_page_flip(struct drm_device *dev, int plane)
10930{
fbee40df 10931 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10932 struct intel_crtc *intel_crtc =
10933 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10934 unsigned long flags;
10935
f326038a
DV
10936
10937 /*
10938 * This is called both by irq handlers and the reset code (to complete
10939 * lost pageflips) so needs the full irqsave spinlocks.
10940 *
10941 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10942 * generate a page-flip completion irq, i.e. every modeset
10943 * is also accompanied by a spurious intel_prepare_page_flip().
10944 */
6b95a207 10945 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10946 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10947 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10948 spin_unlock_irqrestore(&dev->event_lock, flags);
10949}
10950
eba905b2 10951static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10952{
10953 /* Ensure that the work item is consistent when activating it ... */
10954 smp_wmb();
10955 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10956 /* and that it is marked active as soon as the irq could fire. */
10957 smp_wmb();
10958}
10959
8c9f3aaf
JB
10960static int intel_gen2_queue_flip(struct drm_device *dev,
10961 struct drm_crtc *crtc,
10962 struct drm_framebuffer *fb,
ed8d1975 10963 struct drm_i915_gem_object *obj,
a4872ba6 10964 struct intel_engine_cs *ring,
ed8d1975 10965 uint32_t flags)
8c9f3aaf 10966{
8c9f3aaf 10967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10968 u32 flip_mask;
10969 int ret;
10970
6d90c952 10971 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10972 if (ret)
4fa62c89 10973 return ret;
8c9f3aaf
JB
10974
10975 /* Can't queue multiple flips, so wait for the previous
10976 * one to finish before executing the next.
10977 */
10978 if (intel_crtc->plane)
10979 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10980 else
10981 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10982 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10983 intel_ring_emit(ring, MI_NOOP);
10984 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10985 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10986 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10987 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10988 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10989
10990 intel_mark_page_flip_active(intel_crtc);
09246732 10991 __intel_ring_advance(ring);
83d4092b 10992 return 0;
8c9f3aaf
JB
10993}
10994
10995static int intel_gen3_queue_flip(struct drm_device *dev,
10996 struct drm_crtc *crtc,
10997 struct drm_framebuffer *fb,
ed8d1975 10998 struct drm_i915_gem_object *obj,
a4872ba6 10999 struct intel_engine_cs *ring,
ed8d1975 11000 uint32_t flags)
8c9f3aaf 11001{
8c9f3aaf 11002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11003 u32 flip_mask;
11004 int ret;
11005
6d90c952 11006 ret = intel_ring_begin(ring, 6);
8c9f3aaf 11007 if (ret)
4fa62c89 11008 return ret;
8c9f3aaf
JB
11009
11010 if (intel_crtc->plane)
11011 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11012 else
11013 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11014 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11015 intel_ring_emit(ring, MI_NOOP);
11016 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11017 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11018 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11019 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
11020 intel_ring_emit(ring, MI_NOOP);
11021
e7d841ca 11022 intel_mark_page_flip_active(intel_crtc);
09246732 11023 __intel_ring_advance(ring);
83d4092b 11024 return 0;
8c9f3aaf
JB
11025}
11026
11027static int intel_gen4_queue_flip(struct drm_device *dev,
11028 struct drm_crtc *crtc,
11029 struct drm_framebuffer *fb,
ed8d1975 11030 struct drm_i915_gem_object *obj,
a4872ba6 11031 struct intel_engine_cs *ring,
ed8d1975 11032 uint32_t flags)
8c9f3aaf
JB
11033{
11034 struct drm_i915_private *dev_priv = dev->dev_private;
11035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11036 uint32_t pf, pipesrc;
11037 int ret;
11038
6d90c952 11039 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11040 if (ret)
4fa62c89 11041 return ret;
8c9f3aaf
JB
11042
11043 /* i965+ uses the linear or tiled offsets from the
11044 * Display Registers (which do not change across a page-flip)
11045 * so we need only reprogram the base address.
11046 */
6d90c952
DV
11047 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11048 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11049 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11050 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 11051 obj->tiling_mode);
8c9f3aaf
JB
11052
11053 /* XXX Enabling the panel-fitter across page-flip is so far
11054 * untested on non-native modes, so ignore it for now.
11055 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11056 */
11057 pf = 0;
11058 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11059 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11060
11061 intel_mark_page_flip_active(intel_crtc);
09246732 11062 __intel_ring_advance(ring);
83d4092b 11063 return 0;
8c9f3aaf
JB
11064}
11065
11066static int intel_gen6_queue_flip(struct drm_device *dev,
11067 struct drm_crtc *crtc,
11068 struct drm_framebuffer *fb,
ed8d1975 11069 struct drm_i915_gem_object *obj,
a4872ba6 11070 struct intel_engine_cs *ring,
ed8d1975 11071 uint32_t flags)
8c9f3aaf
JB
11072{
11073 struct drm_i915_private *dev_priv = dev->dev_private;
11074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11075 uint32_t pf, pipesrc;
11076 int ret;
11077
6d90c952 11078 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11079 if (ret)
4fa62c89 11080 return ret;
8c9f3aaf 11081
6d90c952
DV
11082 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11083 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11084 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11085 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11086
dc257cf1
DV
11087 /* Contrary to the suggestions in the documentation,
11088 * "Enable Panel Fitter" does not seem to be required when page
11089 * flipping with a non-native mode, and worse causes a normal
11090 * modeset to fail.
11091 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11092 */
11093 pf = 0;
8c9f3aaf 11094 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11095 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11096
11097 intel_mark_page_flip_active(intel_crtc);
09246732 11098 __intel_ring_advance(ring);
83d4092b 11099 return 0;
8c9f3aaf
JB
11100}
11101
7c9017e5
JB
11102static int intel_gen7_queue_flip(struct drm_device *dev,
11103 struct drm_crtc *crtc,
11104 struct drm_framebuffer *fb,
ed8d1975 11105 struct drm_i915_gem_object *obj,
a4872ba6 11106 struct intel_engine_cs *ring,
ed8d1975 11107 uint32_t flags)
7c9017e5 11108{
7c9017e5 11109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11110 uint32_t plane_bit = 0;
ffe74d75
CW
11111 int len, ret;
11112
eba905b2 11113 switch (intel_crtc->plane) {
cb05d8de
DV
11114 case PLANE_A:
11115 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11116 break;
11117 case PLANE_B:
11118 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11119 break;
11120 case PLANE_C:
11121 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11122 break;
11123 default:
11124 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11125 return -ENODEV;
cb05d8de
DV
11126 }
11127
ffe74d75 11128 len = 4;
f476828a 11129 if (ring->id == RCS) {
ffe74d75 11130 len += 6;
f476828a
DL
11131 /*
11132 * On Gen 8, SRM is now taking an extra dword to accommodate
11133 * 48bits addresses, and we need a NOOP for the batch size to
11134 * stay even.
11135 */
11136 if (IS_GEN8(dev))
11137 len += 2;
11138 }
ffe74d75 11139
f66fab8e
VS
11140 /*
11141 * BSpec MI_DISPLAY_FLIP for IVB:
11142 * "The full packet must be contained within the same cache line."
11143 *
11144 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11145 * cacheline, if we ever start emitting more commands before
11146 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11147 * then do the cacheline alignment, and finally emit the
11148 * MI_DISPLAY_FLIP.
11149 */
11150 ret = intel_ring_cacheline_align(ring);
11151 if (ret)
4fa62c89 11152 return ret;
f66fab8e 11153
ffe74d75 11154 ret = intel_ring_begin(ring, len);
7c9017e5 11155 if (ret)
4fa62c89 11156 return ret;
7c9017e5 11157
ffe74d75
CW
11158 /* Unmask the flip-done completion message. Note that the bspec says that
11159 * we should do this for both the BCS and RCS, and that we must not unmask
11160 * more than one flip event at any time (or ensure that one flip message
11161 * can be sent by waiting for flip-done prior to queueing new flips).
11162 * Experimentation says that BCS works despite DERRMR masking all
11163 * flip-done completion events and that unmasking all planes at once
11164 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11165 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11166 */
11167 if (ring->id == RCS) {
11168 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11169 intel_ring_emit(ring, DERRMR);
11170 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11171 DERRMR_PIPEB_PRI_FLIP_DONE |
11172 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11173 if (IS_GEN8(dev))
11174 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11175 MI_SRM_LRM_GLOBAL_GTT);
11176 else
11177 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11178 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11179 intel_ring_emit(ring, DERRMR);
11180 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11181 if (IS_GEN8(dev)) {
11182 intel_ring_emit(ring, 0);
11183 intel_ring_emit(ring, MI_NOOP);
11184 }
ffe74d75
CW
11185 }
11186
cb05d8de 11187 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11188 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11189 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11190 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11191
11192 intel_mark_page_flip_active(intel_crtc);
09246732 11193 __intel_ring_advance(ring);
83d4092b 11194 return 0;
7c9017e5
JB
11195}
11196
84c33a64
SG
11197static bool use_mmio_flip(struct intel_engine_cs *ring,
11198 struct drm_i915_gem_object *obj)
11199{
11200 /*
11201 * This is not being used for older platforms, because
11202 * non-availability of flip done interrupt forces us to use
11203 * CS flips. Older platforms derive flip done using some clever
11204 * tricks involving the flip_pending status bits and vblank irqs.
11205 * So using MMIO flips there would disrupt this mechanism.
11206 */
11207
8e09bf83
CW
11208 if (ring == NULL)
11209 return true;
11210
84c33a64
SG
11211 if (INTEL_INFO(ring->dev)->gen < 5)
11212 return false;
11213
11214 if (i915.use_mmio_flip < 0)
11215 return false;
11216 else if (i915.use_mmio_flip > 0)
11217 return true;
14bf993e
OM
11218 else if (i915.enable_execlists)
11219 return true;
84c33a64 11220 else
b4716185 11221 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11222}
11223
ff944564
DL
11224static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11225{
11226 struct drm_device *dev = intel_crtc->base.dev;
11227 struct drm_i915_private *dev_priv = dev->dev_private;
11228 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11229 const enum pipe pipe = intel_crtc->pipe;
11230 u32 ctl, stride;
11231
11232 ctl = I915_READ(PLANE_CTL(pipe, 0));
11233 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11234 switch (fb->modifier[0]) {
11235 case DRM_FORMAT_MOD_NONE:
11236 break;
11237 case I915_FORMAT_MOD_X_TILED:
ff944564 11238 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11239 break;
11240 case I915_FORMAT_MOD_Y_TILED:
11241 ctl |= PLANE_CTL_TILED_Y;
11242 break;
11243 case I915_FORMAT_MOD_Yf_TILED:
11244 ctl |= PLANE_CTL_TILED_YF;
11245 break;
11246 default:
11247 MISSING_CASE(fb->modifier[0]);
11248 }
ff944564
DL
11249
11250 /*
11251 * The stride is either expressed as a multiple of 64 bytes chunks for
11252 * linear buffers or in number of tiles for tiled buffers.
11253 */
2ebef630
TU
11254 stride = fb->pitches[0] /
11255 intel_fb_stride_alignment(dev, fb->modifier[0],
11256 fb->pixel_format);
ff944564
DL
11257
11258 /*
11259 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11260 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11261 */
11262 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11263 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11264
11265 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11266 POSTING_READ(PLANE_SURF(pipe, 0));
11267}
11268
11269static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11270{
11271 struct drm_device *dev = intel_crtc->base.dev;
11272 struct drm_i915_private *dev_priv = dev->dev_private;
11273 struct intel_framebuffer *intel_fb =
11274 to_intel_framebuffer(intel_crtc->base.primary->fb);
11275 struct drm_i915_gem_object *obj = intel_fb->obj;
11276 u32 dspcntr;
11277 u32 reg;
11278
84c33a64
SG
11279 reg = DSPCNTR(intel_crtc->plane);
11280 dspcntr = I915_READ(reg);
11281
c5d97472
DL
11282 if (obj->tiling_mode != I915_TILING_NONE)
11283 dspcntr |= DISPPLANE_TILED;
11284 else
11285 dspcntr &= ~DISPPLANE_TILED;
11286
84c33a64
SG
11287 I915_WRITE(reg, dspcntr);
11288
11289 I915_WRITE(DSPSURF(intel_crtc->plane),
11290 intel_crtc->unpin_work->gtt_offset);
11291 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11292
ff944564
DL
11293}
11294
11295/*
11296 * XXX: This is the temporary way to update the plane registers until we get
11297 * around to using the usual plane update functions for MMIO flips
11298 */
11299static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11300{
11301 struct drm_device *dev = intel_crtc->base.dev;
11302 bool atomic_update;
11303 u32 start_vbl_count;
11304
11305 intel_mark_page_flip_active(intel_crtc);
11306
11307 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11308
11309 if (INTEL_INFO(dev)->gen >= 9)
11310 skl_do_mmio_flip(intel_crtc);
11311 else
11312 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11313 ilk_do_mmio_flip(intel_crtc);
11314
9362c7c5
ACO
11315 if (atomic_update)
11316 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11317}
11318
9362c7c5 11319static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11320{
b2cfe0ab
CW
11321 struct intel_mmio_flip *mmio_flip =
11322 container_of(work, struct intel_mmio_flip, work);
84c33a64 11323
eed29a5b
DV
11324 if (mmio_flip->req)
11325 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11326 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11327 false, NULL,
11328 &mmio_flip->i915->rps.mmioflips));
84c33a64 11329
b2cfe0ab
CW
11330 intel_do_mmio_flip(mmio_flip->crtc);
11331
eed29a5b 11332 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11333 kfree(mmio_flip);
84c33a64
SG
11334}
11335
11336static int intel_queue_mmio_flip(struct drm_device *dev,
11337 struct drm_crtc *crtc,
11338 struct drm_framebuffer *fb,
11339 struct drm_i915_gem_object *obj,
11340 struct intel_engine_cs *ring,
11341 uint32_t flags)
11342{
b2cfe0ab
CW
11343 struct intel_mmio_flip *mmio_flip;
11344
11345 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11346 if (mmio_flip == NULL)
11347 return -ENOMEM;
84c33a64 11348
bcafc4e3 11349 mmio_flip->i915 = to_i915(dev);
eed29a5b 11350 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11351 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11352
b2cfe0ab
CW
11353 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11354 schedule_work(&mmio_flip->work);
84c33a64 11355
84c33a64
SG
11356 return 0;
11357}
11358
8c9f3aaf
JB
11359static int intel_default_queue_flip(struct drm_device *dev,
11360 struct drm_crtc *crtc,
11361 struct drm_framebuffer *fb,
ed8d1975 11362 struct drm_i915_gem_object *obj,
a4872ba6 11363 struct intel_engine_cs *ring,
ed8d1975 11364 uint32_t flags)
8c9f3aaf
JB
11365{
11366 return -ENODEV;
11367}
11368
d6bbafa1
CW
11369static bool __intel_pageflip_stall_check(struct drm_device *dev,
11370 struct drm_crtc *crtc)
11371{
11372 struct drm_i915_private *dev_priv = dev->dev_private;
11373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11374 struct intel_unpin_work *work = intel_crtc->unpin_work;
11375 u32 addr;
11376
11377 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11378 return true;
11379
11380 if (!work->enable_stall_check)
11381 return false;
11382
11383 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11384 if (work->flip_queued_req &&
11385 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11386 return false;
11387
1e3feefd 11388 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11389 }
11390
1e3feefd 11391 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11392 return false;
11393
11394 /* Potential stall - if we see that the flip has happened,
11395 * assume a missed interrupt. */
11396 if (INTEL_INFO(dev)->gen >= 4)
11397 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11398 else
11399 addr = I915_READ(DSPADDR(intel_crtc->plane));
11400
11401 /* There is a potential issue here with a false positive after a flip
11402 * to the same address. We could address this by checking for a
11403 * non-incrementing frame counter.
11404 */
11405 return addr == work->gtt_offset;
11406}
11407
11408void intel_check_page_flip(struct drm_device *dev, int pipe)
11409{
11410 struct drm_i915_private *dev_priv = dev->dev_private;
11411 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11413 struct intel_unpin_work *work;
f326038a 11414
6c51d46f 11415 WARN_ON(!in_interrupt());
d6bbafa1
CW
11416
11417 if (crtc == NULL)
11418 return;
11419
f326038a 11420 spin_lock(&dev->event_lock);
6ad790c0
CW
11421 work = intel_crtc->unpin_work;
11422 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11423 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11424 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11425 page_flip_completed(intel_crtc);
6ad790c0 11426 work = NULL;
d6bbafa1 11427 }
6ad790c0
CW
11428 if (work != NULL &&
11429 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11430 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11431 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11432}
11433
6b95a207
KH
11434static int intel_crtc_page_flip(struct drm_crtc *crtc,
11435 struct drm_framebuffer *fb,
ed8d1975
KP
11436 struct drm_pending_vblank_event *event,
11437 uint32_t page_flip_flags)
6b95a207
KH
11438{
11439 struct drm_device *dev = crtc->dev;
11440 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11441 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11442 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11444 struct drm_plane *primary = crtc->primary;
a071fa00 11445 enum pipe pipe = intel_crtc->pipe;
6b95a207 11446 struct intel_unpin_work *work;
a4872ba6 11447 struct intel_engine_cs *ring;
cf5d8a46 11448 bool mmio_flip;
52e68630 11449 int ret;
6b95a207 11450
2ff8fde1
MR
11451 /*
11452 * drm_mode_page_flip_ioctl() should already catch this, but double
11453 * check to be safe. In the future we may enable pageflipping from
11454 * a disabled primary plane.
11455 */
11456 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11457 return -EBUSY;
11458
e6a595d2 11459 /* Can't change pixel format via MI display flips. */
f4510a27 11460 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11461 return -EINVAL;
11462
11463 /*
11464 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11465 * Note that pitch changes could also affect these register.
11466 */
11467 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11468 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11469 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11470 return -EINVAL;
11471
f900db47
CW
11472 if (i915_terminally_wedged(&dev_priv->gpu_error))
11473 goto out_hang;
11474
b14c5679 11475 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11476 if (work == NULL)
11477 return -ENOMEM;
11478
6b95a207 11479 work->event = event;
b4a98e57 11480 work->crtc = crtc;
ab8d6675 11481 work->old_fb = old_fb;
6b95a207
KH
11482 INIT_WORK(&work->work, intel_unpin_work_fn);
11483
87b6b101 11484 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11485 if (ret)
11486 goto free_work;
11487
6b95a207 11488 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11489 spin_lock_irq(&dev->event_lock);
6b95a207 11490 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11491 /* Before declaring the flip queue wedged, check if
11492 * the hardware completed the operation behind our backs.
11493 */
11494 if (__intel_pageflip_stall_check(dev, crtc)) {
11495 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11496 page_flip_completed(intel_crtc);
11497 } else {
11498 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11499 spin_unlock_irq(&dev->event_lock);
468f0b44 11500
d6bbafa1
CW
11501 drm_crtc_vblank_put(crtc);
11502 kfree(work);
11503 return -EBUSY;
11504 }
6b95a207
KH
11505 }
11506 intel_crtc->unpin_work = work;
5e2d7afc 11507 spin_unlock_irq(&dev->event_lock);
6b95a207 11508
b4a98e57
CW
11509 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11510 flush_workqueue(dev_priv->wq);
11511
75dfca80 11512 /* Reference the objects for the scheduled work. */
ab8d6675 11513 drm_framebuffer_reference(work->old_fb);
05394f39 11514 drm_gem_object_reference(&obj->base);
6b95a207 11515
f4510a27 11516 crtc->primary->fb = fb;
afd65eb4 11517 update_state_fb(crtc->primary);
1ed1f968 11518
e1f99ce6 11519 work->pending_flip_obj = obj;
e1f99ce6 11520
89ed88ba
CW
11521 ret = i915_mutex_lock_interruptible(dev);
11522 if (ret)
11523 goto cleanup;
11524
b4a98e57 11525 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11526 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11527
75f7f3ec 11528 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11529 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11530
4fa62c89
VS
11531 if (IS_VALLEYVIEW(dev)) {
11532 ring = &dev_priv->ring[BCS];
ab8d6675 11533 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11534 /* vlv: DISPLAY_FLIP fails to change tiling */
11535 ring = NULL;
48bf5b2d 11536 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11537 ring = &dev_priv->ring[BCS];
4fa62c89 11538 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11539 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11540 if (ring == NULL || ring->id != RCS)
11541 ring = &dev_priv->ring[BCS];
11542 } else {
11543 ring = &dev_priv->ring[RCS];
11544 }
11545
cf5d8a46
CW
11546 mmio_flip = use_mmio_flip(ring, obj);
11547
11548 /* When using CS flips, we want to emit semaphores between rings.
11549 * However, when using mmio flips we will create a task to do the
11550 * synchronisation, so all we want here is to pin the framebuffer
11551 * into the display plane and skip any waits.
11552 */
82bc3b2d 11553 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11554 crtc->primary->state,
b4716185 11555 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
8c9f3aaf
JB
11556 if (ret)
11557 goto cleanup_pending;
6b95a207 11558
121920fa
TU
11559 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11560 + intel_crtc->dspaddr_offset;
4fa62c89 11561
cf5d8a46 11562 if (mmio_flip) {
84c33a64
SG
11563 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11564 page_flip_flags);
d6bbafa1
CW
11565 if (ret)
11566 goto cleanup_unpin;
11567
f06cc1b9
JH
11568 i915_gem_request_assign(&work->flip_queued_req,
11569 obj->last_write_req);
d6bbafa1 11570 } else {
d94b5030
CW
11571 if (obj->last_write_req) {
11572 ret = i915_gem_check_olr(obj->last_write_req);
11573 if (ret)
11574 goto cleanup_unpin;
11575 }
11576
84c33a64 11577 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11578 page_flip_flags);
11579 if (ret)
11580 goto cleanup_unpin;
11581
f06cc1b9
JH
11582 i915_gem_request_assign(&work->flip_queued_req,
11583 intel_ring_get_request(ring));
d6bbafa1
CW
11584 }
11585
1e3feefd 11586 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11587 work->enable_stall_check = true;
4fa62c89 11588
ab8d6675 11589 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11590 INTEL_FRONTBUFFER_PRIMARY(pipe));
11591
7ff0ebcc 11592 intel_fbc_disable(dev);
f99d7069 11593 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11594 mutex_unlock(&dev->struct_mutex);
11595
e5510fac
JB
11596 trace_i915_flip_request(intel_crtc->plane, obj);
11597
6b95a207 11598 return 0;
96b099fd 11599
4fa62c89 11600cleanup_unpin:
82bc3b2d 11601 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11602cleanup_pending:
b4a98e57 11603 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11604 mutex_unlock(&dev->struct_mutex);
11605cleanup:
f4510a27 11606 crtc->primary->fb = old_fb;
afd65eb4 11607 update_state_fb(crtc->primary);
89ed88ba
CW
11608
11609 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11610 drm_framebuffer_unreference(work->old_fb);
96b099fd 11611
5e2d7afc 11612 spin_lock_irq(&dev->event_lock);
96b099fd 11613 intel_crtc->unpin_work = NULL;
5e2d7afc 11614 spin_unlock_irq(&dev->event_lock);
96b099fd 11615
87b6b101 11616 drm_crtc_vblank_put(crtc);
7317c75e 11617free_work:
96b099fd
CW
11618 kfree(work);
11619
f900db47 11620 if (ret == -EIO) {
02e0efb5
ML
11621 struct drm_atomic_state *state;
11622 struct drm_plane_state *plane_state;
11623
f900db47 11624out_hang:
02e0efb5
ML
11625 state = drm_atomic_state_alloc(dev);
11626 if (!state)
11627 return -ENOMEM;
11628 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11629
11630retry:
11631 plane_state = drm_atomic_get_plane_state(state, primary);
11632 ret = PTR_ERR_OR_ZERO(plane_state);
11633 if (!ret) {
11634 drm_atomic_set_fb_for_plane(plane_state, fb);
11635
11636 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11637 if (!ret)
11638 ret = drm_atomic_commit(state);
11639 }
11640
11641 if (ret == -EDEADLK) {
11642 drm_modeset_backoff(state->acquire_ctx);
11643 drm_atomic_state_clear(state);
11644 goto retry;
11645 }
11646
11647 if (ret)
11648 drm_atomic_state_free(state);
11649
f0d3dad3 11650 if (ret == 0 && event) {
5e2d7afc 11651 spin_lock_irq(&dev->event_lock);
a071fa00 11652 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11653 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11654 }
f900db47 11655 }
96b099fd 11656 return ret;
6b95a207
KH
11657}
11658
65b38e0d 11659static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11660 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11661 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11662 .atomic_begin = intel_begin_crtc_commit,
11663 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11664};
11665
f7217905
ML
11666/**
11667 * intel_modeset_update_staged_output_state
11668 *
11669 * Updates the staged output configuration state, e.g. after we've read out the
11670 * current hw state.
11671 */
11672static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11673{
11674 struct intel_crtc *crtc;
11675 struct intel_encoder *encoder;
11676 struct intel_connector *connector;
11677
11678 for_each_intel_connector(dev, connector) {
11679 connector->new_encoder =
11680 to_intel_encoder(connector->base.encoder);
11681 }
11682
11683 for_each_intel_encoder(dev, encoder) {
11684 encoder->new_crtc =
11685 to_intel_crtc(encoder->base.crtc);
11686 }
11687
11688 for_each_intel_crtc(dev, crtc) {
11689 crtc->new_enabled = crtc->base.state->enable;
11690 }
11691}
11692
d29b2f9d
ACO
11693/* Transitional helper to copy current connector/encoder state to
11694 * connector->state. This is needed so that code that is partially
11695 * converted to atomic does the right thing.
11696 */
11697static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11698{
11699 struct intel_connector *connector;
11700
11701 for_each_intel_connector(dev, connector) {
11702 if (connector->base.encoder) {
11703 connector->base.state->best_encoder =
11704 connector->base.encoder;
11705 connector->base.state->crtc =
11706 connector->base.encoder->crtc;
11707 } else {
11708 connector->base.state->best_encoder = NULL;
11709 connector->base.state->crtc = NULL;
11710 }
11711 }
11712}
11713
050f7aeb 11714static void
eba905b2 11715connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11716 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11717{
11718 int bpp = pipe_config->pipe_bpp;
11719
11720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11721 connector->base.base.id,
c23cc417 11722 connector->base.name);
050f7aeb
DV
11723
11724 /* Don't use an invalid EDID bpc value */
11725 if (connector->base.display_info.bpc &&
11726 connector->base.display_info.bpc * 3 < bpp) {
11727 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11728 bpp, connector->base.display_info.bpc*3);
11729 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11730 }
11731
11732 /* Clamp bpp to 8 on screens without EDID 1.4 */
11733 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11734 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11735 bpp);
11736 pipe_config->pipe_bpp = 24;
11737 }
11738}
11739
4e53c2e0 11740static int
050f7aeb 11741compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11742 struct intel_crtc_state *pipe_config)
4e53c2e0 11743{
050f7aeb 11744 struct drm_device *dev = crtc->base.dev;
1486017f 11745 struct drm_atomic_state *state;
da3ced29
ACO
11746 struct drm_connector *connector;
11747 struct drm_connector_state *connector_state;
1486017f 11748 int bpp, i;
4e53c2e0 11749
d328c9d7 11750 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11751 bpp = 10*3;
d328c9d7
DV
11752 else if (INTEL_INFO(dev)->gen >= 5)
11753 bpp = 12*3;
11754 else
11755 bpp = 8*3;
11756
4e53c2e0 11757
4e53c2e0
DV
11758 pipe_config->pipe_bpp = bpp;
11759
1486017f
ACO
11760 state = pipe_config->base.state;
11761
4e53c2e0 11762 /* Clamp display bpp to EDID value */
da3ced29
ACO
11763 for_each_connector_in_state(state, connector, connector_state, i) {
11764 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11765 continue;
11766
da3ced29
ACO
11767 connected_sink_compute_bpp(to_intel_connector(connector),
11768 pipe_config);
4e53c2e0
DV
11769 }
11770
11771 return bpp;
11772}
11773
644db711
DV
11774static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11775{
11776 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11777 "type: 0x%x flags: 0x%x\n",
1342830c 11778 mode->crtc_clock,
644db711
DV
11779 mode->crtc_hdisplay, mode->crtc_hsync_start,
11780 mode->crtc_hsync_end, mode->crtc_htotal,
11781 mode->crtc_vdisplay, mode->crtc_vsync_start,
11782 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11783}
11784
c0b03411 11785static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11786 struct intel_crtc_state *pipe_config,
c0b03411
DV
11787 const char *context)
11788{
6a60cd87
CK
11789 struct drm_device *dev = crtc->base.dev;
11790 struct drm_plane *plane;
11791 struct intel_plane *intel_plane;
11792 struct intel_plane_state *state;
11793 struct drm_framebuffer *fb;
11794
11795 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11796 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11797
11798 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11799 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11800 pipe_config->pipe_bpp, pipe_config->dither);
11801 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11802 pipe_config->has_pch_encoder,
11803 pipe_config->fdi_lanes,
11804 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11805 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11806 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11807 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11808 pipe_config->has_dp_encoder,
11809 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11810 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11811 pipe_config->dp_m_n.tu);
b95af8be
VK
11812
11813 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11814 pipe_config->has_dp_encoder,
11815 pipe_config->dp_m2_n2.gmch_m,
11816 pipe_config->dp_m2_n2.gmch_n,
11817 pipe_config->dp_m2_n2.link_m,
11818 pipe_config->dp_m2_n2.link_n,
11819 pipe_config->dp_m2_n2.tu);
11820
55072d19
DV
11821 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11822 pipe_config->has_audio,
11823 pipe_config->has_infoframe);
11824
c0b03411 11825 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11826 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11827 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11828 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11829 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11830 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11831 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11832 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11833 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11834 crtc->num_scalers,
11835 pipe_config->scaler_state.scaler_users,
11836 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11837 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11838 pipe_config->gmch_pfit.control,
11839 pipe_config->gmch_pfit.pgm_ratios,
11840 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11841 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11842 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11843 pipe_config->pch_pfit.size,
11844 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11845 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11846 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11847
415ff0f6
TU
11848 if (IS_BROXTON(dev)) {
11849 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11850 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11851 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11852 pipe_config->ddi_pll_sel,
11853 pipe_config->dpll_hw_state.ebb0,
11854 pipe_config->dpll_hw_state.pll0,
11855 pipe_config->dpll_hw_state.pll1,
11856 pipe_config->dpll_hw_state.pll2,
11857 pipe_config->dpll_hw_state.pll3,
11858 pipe_config->dpll_hw_state.pll6,
11859 pipe_config->dpll_hw_state.pll8,
11860 pipe_config->dpll_hw_state.pcsdw12);
11861 } else if (IS_SKYLAKE(dev)) {
11862 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11863 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11864 pipe_config->ddi_pll_sel,
11865 pipe_config->dpll_hw_state.ctrl1,
11866 pipe_config->dpll_hw_state.cfgcr1,
11867 pipe_config->dpll_hw_state.cfgcr2);
11868 } else if (HAS_DDI(dev)) {
11869 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11870 pipe_config->ddi_pll_sel,
11871 pipe_config->dpll_hw_state.wrpll);
11872 } else {
11873 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11874 "fp0: 0x%x, fp1: 0x%x\n",
11875 pipe_config->dpll_hw_state.dpll,
11876 pipe_config->dpll_hw_state.dpll_md,
11877 pipe_config->dpll_hw_state.fp0,
11878 pipe_config->dpll_hw_state.fp1);
11879 }
11880
6a60cd87
CK
11881 DRM_DEBUG_KMS("planes on this crtc\n");
11882 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11883 intel_plane = to_intel_plane(plane);
11884 if (intel_plane->pipe != crtc->pipe)
11885 continue;
11886
11887 state = to_intel_plane_state(plane->state);
11888 fb = state->base.fb;
11889 if (!fb) {
11890 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11891 "disabled, scaler_id = %d\n",
11892 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11893 plane->base.id, intel_plane->pipe,
11894 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11895 drm_plane_index(plane), state->scaler_id);
11896 continue;
11897 }
11898
11899 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11900 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11901 plane->base.id, intel_plane->pipe,
11902 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11903 drm_plane_index(plane));
11904 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11905 fb->base.id, fb->width, fb->height, fb->pixel_format);
11906 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11907 state->scaler_id,
11908 state->src.x1 >> 16, state->src.y1 >> 16,
11909 drm_rect_width(&state->src) >> 16,
11910 drm_rect_height(&state->src) >> 16,
11911 state->dst.x1, state->dst.y1,
11912 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11913 }
c0b03411
DV
11914}
11915
bc079e8b
VS
11916static bool encoders_cloneable(const struct intel_encoder *a,
11917 const struct intel_encoder *b)
accfc0c5 11918{
bc079e8b
VS
11919 /* masks could be asymmetric, so check both ways */
11920 return a == b || (a->cloneable & (1 << b->type) &&
11921 b->cloneable & (1 << a->type));
11922}
11923
98a221da
ACO
11924static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11925 struct intel_crtc *crtc,
bc079e8b
VS
11926 struct intel_encoder *encoder)
11927{
bc079e8b 11928 struct intel_encoder *source_encoder;
da3ced29 11929 struct drm_connector *connector;
98a221da
ACO
11930 struct drm_connector_state *connector_state;
11931 int i;
bc079e8b 11932
da3ced29 11933 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da 11934 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11935 continue;
11936
98a221da
ACO
11937 source_encoder =
11938 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11939 if (!encoders_cloneable(encoder, source_encoder))
11940 return false;
11941 }
11942
11943 return true;
11944}
11945
98a221da
ACO
11946static bool check_encoder_cloning(struct drm_atomic_state *state,
11947 struct intel_crtc *crtc)
bc079e8b 11948{
accfc0c5 11949 struct intel_encoder *encoder;
da3ced29 11950 struct drm_connector *connector;
98a221da
ACO
11951 struct drm_connector_state *connector_state;
11952 int i;
accfc0c5 11953
da3ced29 11954 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da
ACO
11955 if (connector_state->crtc != &crtc->base)
11956 continue;
11957
11958 encoder = to_intel_encoder(connector_state->best_encoder);
11959 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11960 return false;
accfc0c5
DV
11961 }
11962
bc079e8b 11963 return true;
accfc0c5
DV
11964}
11965
5448a00d 11966static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11967{
5448a00d
ACO
11968 struct drm_device *dev = state->dev;
11969 struct intel_encoder *encoder;
da3ced29 11970 struct drm_connector *connector;
5448a00d 11971 struct drm_connector_state *connector_state;
00f0b378 11972 unsigned int used_ports = 0;
5448a00d 11973 int i;
00f0b378
VS
11974
11975 /*
11976 * Walk the connector list instead of the encoder
11977 * list to detect the problem on ddi platforms
11978 * where there's just one encoder per digital port.
11979 */
da3ced29 11980 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 11981 if (!connector_state->best_encoder)
00f0b378
VS
11982 continue;
11983
5448a00d
ACO
11984 encoder = to_intel_encoder(connector_state->best_encoder);
11985
11986 WARN_ON(!connector_state->crtc);
00f0b378
VS
11987
11988 switch (encoder->type) {
11989 unsigned int port_mask;
11990 case INTEL_OUTPUT_UNKNOWN:
11991 if (WARN_ON(!HAS_DDI(dev)))
11992 break;
11993 case INTEL_OUTPUT_DISPLAYPORT:
11994 case INTEL_OUTPUT_HDMI:
11995 case INTEL_OUTPUT_EDP:
11996 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11997
11998 /* the same port mustn't appear more than once */
11999 if (used_ports & port_mask)
12000 return false;
12001
12002 used_ports |= port_mask;
12003 default:
12004 break;
12005 }
12006 }
12007
12008 return true;
12009}
12010
83a57153
ACO
12011static void
12012clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12013{
12014 struct drm_crtc_state tmp_state;
663a3640 12015 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12016 struct intel_dpll_hw_state dpll_hw_state;
12017 enum intel_dpll_id shared_dpll;
8504c74c 12018 uint32_t ddi_pll_sel;
83a57153 12019
7546a384
ACO
12020 /* FIXME: before the switch to atomic started, a new pipe_config was
12021 * kzalloc'd. Code that depends on any field being zero should be
12022 * fixed, so that the crtc_state can be safely duplicated. For now,
12023 * only fields that are know to not cause problems are preserved. */
12024
83a57153 12025 tmp_state = crtc_state->base;
663a3640 12026 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12027 shared_dpll = crtc_state->shared_dpll;
12028 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12029 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12030
83a57153 12031 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12032
83a57153 12033 crtc_state->base = tmp_state;
663a3640 12034 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12035 crtc_state->shared_dpll = shared_dpll;
12036 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12037 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12038}
12039
548ee15b 12040static int
b8cecdf5 12041intel_modeset_pipe_config(struct drm_crtc *crtc,
568c634a 12042 struct drm_atomic_state *state)
ee7b9f93 12043{
568c634a
ACO
12044 struct drm_crtc_state *crtc_state;
12045 struct intel_crtc_state *pipe_config;
7758a113 12046 struct intel_encoder *encoder;
da3ced29 12047 struct drm_connector *connector;
0b901879 12048 struct drm_connector_state *connector_state;
d328c9d7 12049 int base_bpp, ret = -EINVAL;
0b901879 12050 int i;
e29c22c0 12051 bool retry = true;
ee7b9f93 12052
98a221da 12053 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5 12054 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
548ee15b 12055 return -EINVAL;
accfc0c5
DV
12056 }
12057
5448a00d 12058 if (!check_digital_port_conflicts(state)) {
00f0b378 12059 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
548ee15b 12060 return -EINVAL;
00f0b378
VS
12061 }
12062
568c634a
ACO
12063 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12064 if (WARN_ON(!crtc_state))
12065 return -EINVAL;
12066
12067 pipe_config = to_intel_crtc_state(crtc_state);
12068
cdba954e
ACO
12069 /*
12070 * XXX: Add all connectors to make the crtc state match the encoders.
12071 */
12072 if (!needs_modeset(&pipe_config->base)) {
12073 ret = drm_atomic_add_affected_connectors(state, crtc);
12074 if (ret)
12075 return ret;
12076 }
12077
83a57153 12078 clear_intel_crtc_state(pipe_config);
7758a113 12079
e143a21c
DV
12080 pipe_config->cpu_transcoder =
12081 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12082
2960bc9c
ID
12083 /*
12084 * Sanitize sync polarity flags based on requested ones. If neither
12085 * positive or negative polarity is requested, treat this as meaning
12086 * negative polarity.
12087 */
2d112de7 12088 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12089 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12090 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12091
2d112de7 12092 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12093 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12094 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12095
050f7aeb
DV
12096 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12097 * plane pixel format and any sink constraints into account. Returns the
12098 * source plane bpp so that dithering can be selected on mismatches
12099 * after encoders and crtc also have had their say. */
d328c9d7
DV
12100 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12101 pipe_config);
12102 if (base_bpp < 0)
4e53c2e0
DV
12103 goto fail;
12104
e41a56be
VS
12105 /*
12106 * Determine the real pipe dimensions. Note that stereo modes can
12107 * increase the actual pipe size due to the frame doubling and
12108 * insertion of additional space for blanks between the frame. This
12109 * is stored in the crtc timings. We use the requested mode to do this
12110 * computation to clearly distinguish it from the adjusted mode, which
12111 * can be changed by the connectors in the below retry loop.
12112 */
2d112de7 12113 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12114 &pipe_config->pipe_src_w,
12115 &pipe_config->pipe_src_h);
e41a56be 12116
e29c22c0 12117encoder_retry:
ef1b460d 12118 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12119 pipe_config->port_clock = 0;
ef1b460d 12120 pipe_config->pixel_multiplier = 1;
ff9a6750 12121
135c81b8 12122 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12123 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12124 CRTC_STEREO_DOUBLE);
135c81b8 12125
7758a113
DV
12126 /* Pass our mode to the connectors and the CRTC to give them a chance to
12127 * adjust it according to limitations or connector properties, and also
12128 * a chance to reject the mode entirely.
47f1c6c9 12129 */
da3ced29 12130 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12131 if (connector_state->crtc != crtc)
7758a113 12132 continue;
7ae89233 12133
0b901879
ACO
12134 encoder = to_intel_encoder(connector_state->best_encoder);
12135
efea6e8e
DV
12136 if (!(encoder->compute_config(encoder, pipe_config))) {
12137 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12138 goto fail;
12139 }
ee7b9f93 12140 }
47f1c6c9 12141
ff9a6750
DV
12142 /* Set default port clock if not overwritten by the encoder. Needs to be
12143 * done afterwards in case the encoder adjusts the mode. */
12144 if (!pipe_config->port_clock)
2d112de7 12145 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12146 * pipe_config->pixel_multiplier;
ff9a6750 12147
a43f6e0f 12148 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12149 if (ret < 0) {
7758a113
DV
12150 DRM_DEBUG_KMS("CRTC fixup failed\n");
12151 goto fail;
ee7b9f93 12152 }
e29c22c0
DV
12153
12154 if (ret == RETRY) {
12155 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12156 ret = -EINVAL;
12157 goto fail;
12158 }
12159
12160 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12161 retry = false;
12162 goto encoder_retry;
12163 }
12164
d328c9d7 12165 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12166 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12167 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12168
cdba954e
ACO
12169 /* Check if we need to force a modeset */
12170 if (pipe_config->has_audio !=
85a96e7a 12171 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12172 pipe_config->base.mode_changed = true;
85a96e7a
ML
12173 ret = drm_atomic_add_affected_planes(state, crtc);
12174 }
cdba954e
ACO
12175
12176 /*
12177 * Note we have an issue here with infoframes: current code
12178 * only updates them on the full mode set path per hw
12179 * requirements. So here we should be checking for any
12180 * required changes and forcing a mode set.
12181 */
7758a113 12182fail:
548ee15b 12183 return ret;
ee7b9f93 12184}
47f1c6c9 12185
ea9d758d 12186static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12187{
ea9d758d 12188 struct drm_encoder *encoder;
f6e5b160 12189 struct drm_device *dev = crtc->dev;
f6e5b160 12190
ea9d758d
DV
12191 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12192 if (encoder->crtc == crtc)
12193 return true;
12194
12195 return false;
12196}
12197
12198static void
0a9ab303 12199intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12200{
0a9ab303 12201 struct drm_device *dev = state->dev;
ea9d758d 12202 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12203 struct drm_crtc *crtc;
12204 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12205 struct drm_connector *connector;
12206
de419ab6 12207 intel_shared_dpll_commit(state);
ba41c0de 12208
b2784e15 12209 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12210 if (!intel_encoder->base.crtc)
12211 continue;
12212
69024de8
ML
12213 crtc = intel_encoder->base.crtc;
12214 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12215 if (!crtc_state || !needs_modeset(crtc->state))
12216 continue;
ea9d758d 12217
69024de8 12218 intel_encoder->connectors_active = false;
ea9d758d
DV
12219 }
12220
3cb480bc 12221 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
f7217905 12222 intel_modeset_update_staged_output_state(state->dev);
ea9d758d 12223
7668851f 12224 /* Double check state. */
0a9ab303
ACO
12225 for_each_crtc(dev, crtc) {
12226 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12227
12228 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12229
12230 /* Update hwmode for vblank functions */
12231 if (crtc->state->active)
12232 crtc->hwmode = crtc->state->adjusted_mode;
12233 else
12234 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12235 }
12236
12237 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12238 if (!connector->encoder || !connector->encoder->crtc)
12239 continue;
12240
69024de8
ML
12241 crtc = connector->encoder->crtc;
12242 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12243 if (!crtc_state || !needs_modeset(crtc->state))
12244 continue;
ea9d758d 12245
53d9f4e9 12246 if (crtc->state->active) {
69024de8
ML
12247 struct drm_property *dpms_property =
12248 dev->mode_config.dpms_property;
68d34720 12249
69024de8
ML
12250 connector->dpms = DRM_MODE_DPMS_ON;
12251 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12252
69024de8
ML
12253 intel_encoder = to_intel_encoder(connector->encoder);
12254 intel_encoder->connectors_active = true;
12255 } else
12256 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12257 }
ea9d758d
DV
12258}
12259
3bd26263 12260static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12261{
3bd26263 12262 int diff;
f1f644dc
JB
12263
12264 if (clock1 == clock2)
12265 return true;
12266
12267 if (!clock1 || !clock2)
12268 return false;
12269
12270 diff = abs(clock1 - clock2);
12271
12272 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12273 return true;
12274
12275 return false;
12276}
12277
25c5b266
DV
12278#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12279 list_for_each_entry((intel_crtc), \
12280 &(dev)->mode_config.crtc_list, \
12281 base.head) \
0973f18f 12282 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12283
0e8ffe1b 12284static bool
2fa2fe9a 12285intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12286 struct intel_crtc_state *current_config,
12287 struct intel_crtc_state *pipe_config)
0e8ffe1b 12288{
66e985c0
DV
12289#define PIPE_CONF_CHECK_X(name) \
12290 if (current_config->name != pipe_config->name) { \
12291 DRM_ERROR("mismatch in " #name " " \
12292 "(expected 0x%08x, found 0x%08x)\n", \
12293 current_config->name, \
12294 pipe_config->name); \
12295 return false; \
12296 }
12297
08a24034
DV
12298#define PIPE_CONF_CHECK_I(name) \
12299 if (current_config->name != pipe_config->name) { \
12300 DRM_ERROR("mismatch in " #name " " \
12301 "(expected %i, found %i)\n", \
12302 current_config->name, \
12303 pipe_config->name); \
12304 return false; \
88adfff1
DV
12305 }
12306
b95af8be
VK
12307/* This is required for BDW+ where there is only one set of registers for
12308 * switching between high and low RR.
12309 * This macro can be used whenever a comparison has to be made between one
12310 * hw state and multiple sw state variables.
12311 */
12312#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12313 if ((current_config->name != pipe_config->name) && \
12314 (current_config->alt_name != pipe_config->name)) { \
12315 DRM_ERROR("mismatch in " #name " " \
12316 "(expected %i or %i, found %i)\n", \
12317 current_config->name, \
12318 current_config->alt_name, \
12319 pipe_config->name); \
12320 return false; \
12321 }
12322
1bd1bd80
DV
12323#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12324 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12325 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12326 "(expected %i, found %i)\n", \
12327 current_config->name & (mask), \
12328 pipe_config->name & (mask)); \
12329 return false; \
12330 }
12331
5e550656
VS
12332#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12333 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12334 DRM_ERROR("mismatch in " #name " " \
12335 "(expected %i, found %i)\n", \
12336 current_config->name, \
12337 pipe_config->name); \
12338 return false; \
12339 }
12340
bb760063
DV
12341#define PIPE_CONF_QUIRK(quirk) \
12342 ((current_config->quirks | pipe_config->quirks) & (quirk))
12343
eccb140b
DV
12344 PIPE_CONF_CHECK_I(cpu_transcoder);
12345
08a24034
DV
12346 PIPE_CONF_CHECK_I(has_pch_encoder);
12347 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12348 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12349 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12350 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12351 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12352 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12353
eb14cb74 12354 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12355
12356 if (INTEL_INFO(dev)->gen < 8) {
12357 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12358 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12359 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12360 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12361 PIPE_CONF_CHECK_I(dp_m_n.tu);
12362
12363 if (current_config->has_drrs) {
12364 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12365 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12366 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12367 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12368 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12369 }
12370 } else {
12371 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12372 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12373 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12374 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12375 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12376 }
eb14cb74 12377
2d112de7
ACO
12378 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12379 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12380 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12381 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12382 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12383 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12384
2d112de7
ACO
12385 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12386 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12387 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12388 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12389 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12390 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12391
c93f54cf 12392 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12393 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12394 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12395 IS_VALLEYVIEW(dev))
12396 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12397 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12398
9ed109a7
DV
12399 PIPE_CONF_CHECK_I(has_audio);
12400
2d112de7 12401 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12402 DRM_MODE_FLAG_INTERLACE);
12403
bb760063 12404 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12405 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12406 DRM_MODE_FLAG_PHSYNC);
2d112de7 12407 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12408 DRM_MODE_FLAG_NHSYNC);
2d112de7 12409 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12410 DRM_MODE_FLAG_PVSYNC);
2d112de7 12411 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12412 DRM_MODE_FLAG_NVSYNC);
12413 }
045ac3b5 12414
37327abd
VS
12415 PIPE_CONF_CHECK_I(pipe_src_w);
12416 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12417
9953599b
DV
12418 /*
12419 * FIXME: BIOS likes to set up a cloned config with lvds+external
12420 * screen. Since we don't yet re-compute the pipe config when moving
12421 * just the lvds port away to another pipe the sw tracking won't match.
12422 *
12423 * Proper atomic modesets with recomputed global state will fix this.
12424 * Until then just don't check gmch state for inherited modes.
12425 */
12426 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12427 PIPE_CONF_CHECK_I(gmch_pfit.control);
12428 /* pfit ratios are autocomputed by the hw on gen4+ */
12429 if (INTEL_INFO(dev)->gen < 4)
12430 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12431 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12432 }
12433
fd4daa9c
CW
12434 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12435 if (current_config->pch_pfit.enabled) {
12436 PIPE_CONF_CHECK_I(pch_pfit.pos);
12437 PIPE_CONF_CHECK_I(pch_pfit.size);
12438 }
2fa2fe9a 12439
a1b2278e
CK
12440 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12441
e59150dc
JB
12442 /* BDW+ don't expose a synchronous way to read the state */
12443 if (IS_HASWELL(dev))
12444 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12445
282740f7
VS
12446 PIPE_CONF_CHECK_I(double_wide);
12447
26804afd
DV
12448 PIPE_CONF_CHECK_X(ddi_pll_sel);
12449
c0d43d62 12450 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12451 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12452 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12453 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12454 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12455 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12456 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12457 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12458 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12459
42571aef
VS
12460 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12461 PIPE_CONF_CHECK_I(pipe_bpp);
12462
2d112de7 12463 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12464 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12465
66e985c0 12466#undef PIPE_CONF_CHECK_X
08a24034 12467#undef PIPE_CONF_CHECK_I
b95af8be 12468#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12469#undef PIPE_CONF_CHECK_FLAGS
5e550656 12470#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12471#undef PIPE_CONF_QUIRK
88adfff1 12472
0e8ffe1b
DV
12473 return true;
12474}
12475
08db6652
DL
12476static void check_wm_state(struct drm_device *dev)
12477{
12478 struct drm_i915_private *dev_priv = dev->dev_private;
12479 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12480 struct intel_crtc *intel_crtc;
12481 int plane;
12482
12483 if (INTEL_INFO(dev)->gen < 9)
12484 return;
12485
12486 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12487 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12488
12489 for_each_intel_crtc(dev, intel_crtc) {
12490 struct skl_ddb_entry *hw_entry, *sw_entry;
12491 const enum pipe pipe = intel_crtc->pipe;
12492
12493 if (!intel_crtc->active)
12494 continue;
12495
12496 /* planes */
dd740780 12497 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12498 hw_entry = &hw_ddb.plane[pipe][plane];
12499 sw_entry = &sw_ddb->plane[pipe][plane];
12500
12501 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12502 continue;
12503
12504 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12505 "(expected (%u,%u), found (%u,%u))\n",
12506 pipe_name(pipe), plane + 1,
12507 sw_entry->start, sw_entry->end,
12508 hw_entry->start, hw_entry->end);
12509 }
12510
12511 /* cursor */
12512 hw_entry = &hw_ddb.cursor[pipe];
12513 sw_entry = &sw_ddb->cursor[pipe];
12514
12515 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12516 continue;
12517
12518 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12519 "(expected (%u,%u), found (%u,%u))\n",
12520 pipe_name(pipe),
12521 sw_entry->start, sw_entry->end,
12522 hw_entry->start, hw_entry->end);
12523 }
12524}
12525
91d1b4bd
DV
12526static void
12527check_connector_state(struct drm_device *dev)
8af6cf88 12528{
8af6cf88
DV
12529 struct intel_connector *connector;
12530
3a3371ff 12531 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12532 /* This also checks the encoder/connector hw state with the
12533 * ->get_hw_state callbacks. */
12534 intel_connector_check_state(connector);
12535
f7217905 12536 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
12537 "connector's staged encoder doesn't match current encoder\n");
12538 }
91d1b4bd
DV
12539}
12540
12541static void
12542check_encoder_state(struct drm_device *dev)
12543{
12544 struct intel_encoder *encoder;
12545 struct intel_connector *connector;
8af6cf88 12546
b2784e15 12547 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12548 bool enabled = false;
12549 bool active = false;
12550 enum pipe pipe, tracked_pipe;
12551
12552 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12553 encoder->base.base.id,
8e329a03 12554 encoder->base.name);
8af6cf88 12555
f7217905
ML
12556 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
12557 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 12558 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12559 "encoder's active_connectors set, but no crtc\n");
12560
3a3371ff 12561 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12562 if (connector->base.encoder != &encoder->base)
12563 continue;
12564 enabled = true;
12565 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12566 active = true;
12567 }
0e32b39c
DA
12568 /*
12569 * for MST connectors if we unplug the connector is gone
12570 * away but the encoder is still connected to a crtc
12571 * until a modeset happens in response to the hotplug.
12572 */
12573 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12574 continue;
12575
e2c719b7 12576 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12577 "encoder's enabled state mismatch "
12578 "(expected %i, found %i)\n",
12579 !!encoder->base.crtc, enabled);
e2c719b7 12580 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12581 "active encoder with no crtc\n");
12582
e2c719b7 12583 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12584 "encoder's computed active state doesn't match tracked active state "
12585 "(expected %i, found %i)\n", active, encoder->connectors_active);
12586
12587 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12588 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12589 "encoder's hw state doesn't match sw tracking "
12590 "(expected %i, found %i)\n",
12591 encoder->connectors_active, active);
12592
12593 if (!encoder->base.crtc)
12594 continue;
12595
12596 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12597 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12598 "active encoder's pipe doesn't match"
12599 "(expected %i, found %i)\n",
12600 tracked_pipe, pipe);
12601
12602 }
91d1b4bd
DV
12603}
12604
12605static void
12606check_crtc_state(struct drm_device *dev)
12607{
fbee40df 12608 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12609 struct intel_crtc *crtc;
12610 struct intel_encoder *encoder;
5cec258b 12611 struct intel_crtc_state pipe_config;
8af6cf88 12612
d3fcc808 12613 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12614 bool enabled = false;
12615 bool active = false;
12616
045ac3b5
JB
12617 memset(&pipe_config, 0, sizeof(pipe_config));
12618
8af6cf88
DV
12619 DRM_DEBUG_KMS("[CRTC:%d]\n",
12620 crtc->base.base.id);
12621
83d65738 12622 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12623 "active crtc, but not enabled in sw tracking\n");
12624
b2784e15 12625 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12626 if (encoder->base.crtc != &crtc->base)
12627 continue;
12628 enabled = true;
12629 if (encoder->connectors_active)
12630 active = true;
12631 }
6c49f241 12632
e2c719b7 12633 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12634 "crtc's computed active state doesn't match tracked active state "
12635 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12636 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12637 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12638 "(expected %i, found %i)\n", enabled,
12639 crtc->base.state->enable);
8af6cf88 12640
0e8ffe1b
DV
12641 active = dev_priv->display.get_pipe_config(crtc,
12642 &pipe_config);
d62cf62a 12643
b6b5d049
VS
12644 /* hw state is inconsistent with the pipe quirk */
12645 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12646 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12647 active = crtc->active;
12648
b2784e15 12649 for_each_intel_encoder(dev, encoder) {
3eaba51c 12650 enum pipe pipe;
6c49f241
DV
12651 if (encoder->base.crtc != &crtc->base)
12652 continue;
1d37b689 12653 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12654 encoder->get_config(encoder, &pipe_config);
12655 }
12656
e2c719b7 12657 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12658 "crtc active state doesn't match with hw state "
12659 "(expected %i, found %i)\n", crtc->active, active);
12660
53d9f4e9
ML
12661 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12662 "transitional active state does not match atomic hw state "
12663 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12664
c0b03411 12665 if (active &&
6e3c9717 12666 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12667 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12668 intel_dump_pipe_config(crtc, &pipe_config,
12669 "[hw state]");
6e3c9717 12670 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12671 "[sw state]");
12672 }
8af6cf88
DV
12673 }
12674}
12675
91d1b4bd
DV
12676static void
12677check_shared_dpll_state(struct drm_device *dev)
12678{
fbee40df 12679 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12680 struct intel_crtc *crtc;
12681 struct intel_dpll_hw_state dpll_hw_state;
12682 int i;
5358901f
DV
12683
12684 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12685 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12686 int enabled_crtcs = 0, active_crtcs = 0;
12687 bool active;
12688
12689 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12690
12691 DRM_DEBUG_KMS("%s\n", pll->name);
12692
12693 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12694
e2c719b7 12695 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12696 "more active pll users than references: %i vs %i\n",
3e369b76 12697 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12698 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12699 "pll in active use but not on in sw tracking\n");
e2c719b7 12700 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12701 "pll in on but not on in use in sw tracking\n");
e2c719b7 12702 I915_STATE_WARN(pll->on != active,
5358901f
DV
12703 "pll on state mismatch (expected %i, found %i)\n",
12704 pll->on, active);
12705
d3fcc808 12706 for_each_intel_crtc(dev, crtc) {
83d65738 12707 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12708 enabled_crtcs++;
12709 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12710 active_crtcs++;
12711 }
e2c719b7 12712 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12713 "pll active crtcs mismatch (expected %i, found %i)\n",
12714 pll->active, active_crtcs);
e2c719b7 12715 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12716 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12717 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12718
e2c719b7 12719 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12720 sizeof(dpll_hw_state)),
12721 "pll hw state mismatch\n");
5358901f 12722 }
8af6cf88
DV
12723}
12724
91d1b4bd
DV
12725void
12726intel_modeset_check_state(struct drm_device *dev)
12727{
08db6652 12728 check_wm_state(dev);
91d1b4bd
DV
12729 check_connector_state(dev);
12730 check_encoder_state(dev);
12731 check_crtc_state(dev);
12732 check_shared_dpll_state(dev);
12733}
12734
5cec258b 12735void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12736 int dotclock)
12737{
12738 /*
12739 * FDI already provided one idea for the dotclock.
12740 * Yell if the encoder disagrees.
12741 */
2d112de7 12742 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12743 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12744 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12745}
12746
80715b2f
VS
12747static void update_scanline_offset(struct intel_crtc *crtc)
12748{
12749 struct drm_device *dev = crtc->base.dev;
12750
12751 /*
12752 * The scanline counter increments at the leading edge of hsync.
12753 *
12754 * On most platforms it starts counting from vtotal-1 on the
12755 * first active line. That means the scanline counter value is
12756 * always one less than what we would expect. Ie. just after
12757 * start of vblank, which also occurs at start of hsync (on the
12758 * last active line), the scanline counter will read vblank_start-1.
12759 *
12760 * On gen2 the scanline counter starts counting from 1 instead
12761 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12762 * to keep the value positive), instead of adding one.
12763 *
12764 * On HSW+ the behaviour of the scanline counter depends on the output
12765 * type. For DP ports it behaves like most other platforms, but on HDMI
12766 * there's an extra 1 line difference. So we need to add two instead of
12767 * one to the value.
12768 */
12769 if (IS_GEN2(dev)) {
6e3c9717 12770 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12771 int vtotal;
12772
12773 vtotal = mode->crtc_vtotal;
12774 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12775 vtotal /= 2;
12776
12777 crtc->scanline_offset = vtotal - 1;
12778 } else if (HAS_DDI(dev) &&
409ee761 12779 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12780 crtc->scanline_offset = 2;
12781 } else
12782 crtc->scanline_offset = 1;
12783}
12784
c347a676 12785static int intel_modeset_setup_plls(struct drm_atomic_state *state)
ed6739ef 12786{
225da59b 12787 struct drm_device *dev = state->dev;
ed6739ef 12788 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12789 unsigned clear_pipes = 0;
ed6739ef 12790 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12791 struct intel_crtc_state *intel_crtc_state;
12792 struct drm_crtc *crtc;
12793 struct drm_crtc_state *crtc_state;
ed6739ef 12794 int ret = 0;
0a9ab303 12795 int i;
ed6739ef
ACO
12796
12797 if (!dev_priv->display.crtc_compute_clock)
12798 return 0;
12799
0a9ab303
ACO
12800 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12801 intel_crtc = to_intel_crtc(crtc);
4978cc93 12802 intel_crtc_state = to_intel_crtc_state(crtc_state);
0a9ab303 12803
4978cc93 12804 if (needs_modeset(crtc_state)) {
0a9ab303 12805 clear_pipes |= 1 << intel_crtc->pipe;
4978cc93 12806 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
4978cc93 12807 }
0a9ab303
ACO
12808 }
12809
de419ab6
ML
12810 if (clear_pipes) {
12811 struct intel_shared_dpll_config *shared_dpll =
12812 intel_atomic_get_shared_dpll_state(state);
12813
12814 for (i = 0; i < dev_priv->num_shared_dpll; i++)
12815 shared_dpll[i].crtc_mask &= ~clear_pipes;
12816 }
ed6739ef 12817
0a9ab303
ACO
12818 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12819 if (!needs_modeset(crtc_state) || !crtc_state->enable)
225da59b
ACO
12820 continue;
12821
0a9ab303
ACO
12822 intel_crtc = to_intel_crtc(crtc);
12823 intel_crtc_state = to_intel_crtc_state(crtc_state);
12824
ed6739ef 12825 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
0a9ab303 12826 intel_crtc_state);
de419ab6
ML
12827 if (ret)
12828 return ret;
ed6739ef
ACO
12829 }
12830
ed6739ef
ACO
12831 return ret;
12832}
12833
99d736a2
ML
12834/*
12835 * This implements the workaround described in the "notes" section of the mode
12836 * set sequence documentation. When going from no pipes or single pipe to
12837 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12838 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12839 */
12840static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12841{
12842 struct drm_crtc_state *crtc_state;
12843 struct intel_crtc *intel_crtc;
12844 struct drm_crtc *crtc;
12845 struct intel_crtc_state *first_crtc_state = NULL;
12846 struct intel_crtc_state *other_crtc_state = NULL;
12847 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12848 int i;
12849
12850 /* look at all crtc's that are going to be enabled in during modeset */
12851 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12852 intel_crtc = to_intel_crtc(crtc);
12853
12854 if (!crtc_state->active || !needs_modeset(crtc_state))
12855 continue;
12856
12857 if (first_crtc_state) {
12858 other_crtc_state = to_intel_crtc_state(crtc_state);
12859 break;
12860 } else {
12861 first_crtc_state = to_intel_crtc_state(crtc_state);
12862 first_pipe = intel_crtc->pipe;
12863 }
12864 }
12865
12866 /* No workaround needed? */
12867 if (!first_crtc_state)
12868 return 0;
12869
12870 /* w/a possibly needed, check how many crtc's are already enabled. */
12871 for_each_intel_crtc(state->dev, intel_crtc) {
12872 struct intel_crtc_state *pipe_config;
12873
12874 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12875 if (IS_ERR(pipe_config))
12876 return PTR_ERR(pipe_config);
12877
12878 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12879
12880 if (!pipe_config->base.active ||
12881 needs_modeset(&pipe_config->base))
12882 continue;
12883
12884 /* 2 or more enabled crtcs means no need for w/a */
12885 if (enabled_pipe != INVALID_PIPE)
12886 return 0;
12887
12888 enabled_pipe = intel_crtc->pipe;
12889 }
12890
12891 if (enabled_pipe != INVALID_PIPE)
12892 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12893 else if (other_crtc_state)
12894 other_crtc_state->hsw_workaround_pipe = first_pipe;
12895
12896 return 0;
12897}
12898
054518dd 12899/* Code that should eventually be part of atomic_check() */
c347a676 12900static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
12901{
12902 struct drm_device *dev = state->dev;
12903 int ret;
12904
12905 /*
12906 * See if the config requires any additional preparation, e.g.
12907 * to adjust global state with pipes off. We need to do this
12908 * here so we can get the modeset_pipe updated config for the new
12909 * mode set on this crtc. For other crtcs we need to use the
12910 * adjusted_mode bits in the crtc directly.
12911 */
b432e5cf
VS
12912 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12913 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12914 ret = valleyview_modeset_global_pipes(state);
12915 else
12916 ret = broadwell_modeset_global_pipes(state);
12917
054518dd
ACO
12918 if (ret)
12919 return ret;
12920 }
12921
99d736a2
ML
12922 ret = intel_modeset_setup_plls(state);
12923 if (ret)
12924 return ret;
12925
12926 if (IS_HASWELL(dev))
12927 ret = haswell_mode_set_planes_workaround(state);
12928
12929 return ret;
c347a676
ACO
12930}
12931
12932static int
12933intel_modeset_compute_config(struct drm_atomic_state *state)
12934{
12935 struct drm_crtc *crtc;
12936 struct drm_crtc_state *crtc_state;
12937 int ret, i;
12938
12939 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
12940 if (ret)
12941 return ret;
12942
c347a676
ACO
12943 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12944 if (!crtc_state->enable &&
12945 WARN_ON(crtc_state->active))
12946 crtc_state->active = false;
12947
12948 if (!crtc_state->enable)
12949 continue;
12950
12951 ret = intel_modeset_pipe_config(crtc, state);
12952 if (ret)
12953 return ret;
12954
12955 intel_dump_pipe_config(to_intel_crtc(crtc),
12956 to_intel_crtc_state(crtc_state),
12957 "[modeset]");
12958 }
12959
12960 ret = intel_modeset_checks(state);
12961 if (ret)
12962 return ret;
12963
12964 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
12965}
12966
c72d969b 12967static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 12968{
c72d969b 12969 struct drm_device *dev = state->dev;
fbee40df 12970 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
12971 struct drm_crtc *crtc;
12972 struct drm_crtc_state *crtc_state;
c0c36b94 12973 int ret = 0;
0a9ab303 12974 int i;
a6778b3c 12975
d4afb8cc
ACO
12976 ret = drm_atomic_helper_prepare_planes(dev, state);
12977 if (ret)
12978 return ret;
12979
1c5e19f8
ML
12980 drm_atomic_helper_swap_state(dev, state);
12981
0a9ab303 12982 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1c5e19f8 12983 if (!needs_modeset(crtc->state) || !crtc_state->active)
0a9ab303 12984 continue;
460da916 12985
69024de8
ML
12986 intel_crtc_disable_planes(crtc);
12987 dev_priv->display.crtc_disable(crtc);
ea9d758d 12988 }
a6778b3c 12989
ea9d758d
DV
12990 /* Only after disabling all output pipelines that will be changed can we
12991 * update the the output configuration. */
0a9ab303 12992 intel_modeset_update_state(state);
f6e5b160 12993
a821fc46
ACO
12994 /* The state has been swaped above, so state actually contains the
12995 * old state now. */
12996
304603f4 12997 modeset_update_crtc_power_domains(state);
47fab737 12998
a6778b3c 12999 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13000 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5ac1c4bc
ML
13001 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
13002
53d9f4e9 13003 if (!needs_modeset(crtc->state) || !crtc->state->active)
0a9ab303
ACO
13004 continue;
13005
13006 update_scanline_offset(to_intel_crtc(crtc));
80715b2f 13007
0a9ab303
ACO
13008 dev_priv->display.crtc_enable(crtc);
13009 intel_crtc_enable_planes(crtc);
80715b2f 13010 }
a6778b3c 13011
a6778b3c 13012 /* FIXME: add subpixel order */
83a57153 13013
d4afb8cc
ACO
13014 drm_atomic_helper_cleanup_planes(dev, state);
13015
2bfb4627
ACO
13016 drm_atomic_state_free(state);
13017
9eb45f22 13018 return 0;
f6e5b160
CW
13019}
13020
568c634a 13021static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13022{
568c634a 13023 struct drm_device *dev = state->dev;
f30da187
DV
13024 int ret;
13025
568c634a 13026 ret = __intel_set_mode(state);
f30da187 13027 if (ret == 0)
568c634a 13028 intel_modeset_check_state(dev);
f30da187
DV
13029
13030 return ret;
13031}
13032
568c634a 13033static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13034{
568c634a 13035 int ret;
83a57153 13036
568c634a 13037 ret = intel_modeset_compute_config(state);
83a57153 13038 if (ret)
568c634a 13039 return ret;
7f27126e 13040
568c634a 13041 return intel_set_mode_checked(state);
7f27126e
JB
13042}
13043
c0c36b94
CW
13044void intel_crtc_restore_mode(struct drm_crtc *crtc)
13045{
83a57153
ACO
13046 struct drm_device *dev = crtc->dev;
13047 struct drm_atomic_state *state;
4be07317 13048 struct intel_crtc *intel_crtc;
83a57153
ACO
13049 struct intel_encoder *encoder;
13050 struct intel_connector *connector;
13051 struct drm_connector_state *connector_state;
4be07317 13052 struct intel_crtc_state *crtc_state;
2bfb4627 13053 int ret;
83a57153
ACO
13054
13055 state = drm_atomic_state_alloc(dev);
13056 if (!state) {
13057 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13058 crtc->base.id);
13059 return;
13060 }
13061
13062 state->acquire_ctx = dev->mode_config.acquire_ctx;
13063
13064 /* The force restore path in the HW readout code relies on the staged
13065 * config still keeping the user requested config while the actual
13066 * state has been overwritten by the configuration read from HW. We
13067 * need to copy the staged config to the atomic state, otherwise the
13068 * mode set will just reapply the state the HW is already in. */
13069 for_each_intel_encoder(dev, encoder) {
f7217905 13070 if (&encoder->new_crtc->base != crtc)
83a57153
ACO
13071 continue;
13072
13073 for_each_intel_connector(dev, connector) {
f7217905 13074 if (connector->new_encoder != encoder)
83a57153
ACO
13075 continue;
13076
13077 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13078 if (IS_ERR(connector_state)) {
13079 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13080 connector->base.base.id,
13081 connector->base.name,
13082 PTR_ERR(connector_state));
13083 continue;
13084 }
13085
13086 connector_state->crtc = crtc;
f7217905 13087 connector_state->best_encoder = &encoder->base;
83a57153
ACO
13088 }
13089 }
13090
4be07317 13091 for_each_intel_crtc(dev, intel_crtc) {
f7217905
ML
13092 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13093 continue;
13094
4be07317
ACO
13095 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13096 if (IS_ERR(crtc_state)) {
13097 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13098 intel_crtc->base.base.id,
13099 PTR_ERR(crtc_state));
13100 continue;
13101 }
13102
f7217905
ML
13103 crtc_state->base.active = crtc_state->base.enable =
13104 intel_crtc->new_enabled;
13105
8c7b5ccb
ACO
13106 if (&intel_crtc->base == crtc)
13107 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317
ACO
13108 }
13109
d3a40d1b
ACO
13110 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13111 crtc->primary->fb, crtc->x, crtc->y);
13112
568c634a 13113 ret = intel_set_mode(state);
2bfb4627
ACO
13114 if (ret)
13115 drm_atomic_state_free(state);
c0c36b94
CW
13116}
13117
25c5b266
DV
13118#undef for_each_intel_crtc_masked
13119
b7885264
ACO
13120static bool intel_connector_in_mode_set(struct intel_connector *connector,
13121 struct drm_mode_set *set)
13122{
13123 int ro;
13124
13125 for (ro = 0; ro < set->num_connectors; ro++)
13126 if (set->connectors[ro] == &connector->base)
13127 return true;
13128
13129 return false;
13130}
13131
2e431051 13132static int
9a935856
DV
13133intel_modeset_stage_output_state(struct drm_device *dev,
13134 struct drm_mode_set *set,
944b0c76 13135 struct drm_atomic_state *state)
50f56119 13136{
9a935856 13137 struct intel_connector *connector;
d5432a9d 13138 struct drm_connector *drm_connector;
944b0c76 13139 struct drm_connector_state *connector_state;
d5432a9d
ACO
13140 struct drm_crtc *crtc;
13141 struct drm_crtc_state *crtc_state;
13142 int i, ret;
50f56119 13143
9abdda74 13144 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13145 * of connectors. For paranoia, double-check this. */
13146 WARN_ON(!set->fb && (set->num_connectors != 0));
13147 WARN_ON(set->fb && (set->num_connectors == 0));
13148
3a3371ff 13149 for_each_intel_connector(dev, connector) {
b7885264
ACO
13150 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13151
d5432a9d
ACO
13152 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13153 continue;
13154
13155 connector_state =
13156 drm_atomic_get_connector_state(state, &connector->base);
13157 if (IS_ERR(connector_state))
13158 return PTR_ERR(connector_state);
13159
b7885264
ACO
13160 if (in_mode_set) {
13161 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13162 connector_state->best_encoder =
13163 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13164 }
13165
d5432a9d 13166 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13167 continue;
13168
9a935856
DV
13169 /* If we disable the crtc, disable all its connectors. Also, if
13170 * the connector is on the changing crtc but not on the new
13171 * connector list, disable it. */
b7885264 13172 if (!set->fb || !in_mode_set) {
d5432a9d 13173 connector_state->best_encoder = NULL;
9a935856
DV
13174
13175 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13176 connector->base.base.id,
c23cc417 13177 connector->base.name);
9a935856 13178 }
50f56119 13179 }
9a935856 13180 /* connector->new_encoder is now updated for all connectors. */
50f56119 13181
d5432a9d
ACO
13182 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13183 connector = to_intel_connector(drm_connector);
13184
13185 if (!connector_state->best_encoder) {
13186 ret = drm_atomic_set_crtc_for_connector(connector_state,
13187 NULL);
13188 if (ret)
13189 return ret;
7668851f 13190
50f56119 13191 continue;
d5432a9d 13192 }
50f56119 13193
d5432a9d
ACO
13194 if (intel_connector_in_mode_set(connector, set)) {
13195 struct drm_crtc *crtc = connector->base.state->crtc;
13196
13197 /* If this connector was in a previous crtc, add it
13198 * to the state. We might need to disable it. */
13199 if (crtc) {
13200 crtc_state =
13201 drm_atomic_get_crtc_state(state, crtc);
13202 if (IS_ERR(crtc_state))
13203 return PTR_ERR(crtc_state);
13204 }
13205
13206 ret = drm_atomic_set_crtc_for_connector(connector_state,
13207 set->crtc);
13208 if (ret)
13209 return ret;
13210 }
50f56119
DV
13211
13212 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13213 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13214 connector_state->crtc)) {
5e2b584e 13215 return -EINVAL;
50f56119 13216 }
944b0c76 13217
9a935856
DV
13218 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13219 connector->base.base.id,
c23cc417 13220 connector->base.name,
d5432a9d 13221 connector_state->crtc->base.id);
944b0c76 13222
d5432a9d
ACO
13223 if (connector_state->best_encoder != &connector->encoder->base)
13224 connector->encoder =
13225 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13226 }
7668851f 13227
d5432a9d 13228 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13229 bool has_connectors;
13230
d5432a9d
ACO
13231 ret = drm_atomic_add_affected_connectors(state, crtc);
13232 if (ret)
13233 return ret;
4be07317 13234
49d6fa21
ML
13235 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13236 if (has_connectors != crtc_state->enable)
13237 crtc_state->enable =
13238 crtc_state->active = has_connectors;
7668851f
VS
13239 }
13240
8c7b5ccb
ACO
13241 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13242 set->fb, set->x, set->y);
13243 if (ret)
13244 return ret;
13245
13246 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13247 if (IS_ERR(crtc_state))
13248 return PTR_ERR(crtc_state);
13249
13250 if (set->mode)
13251 drm_mode_copy(&crtc_state->mode, set->mode);
13252
13253 if (set->num_connectors)
13254 crtc_state->active = true;
13255
2e431051
DV
13256 return 0;
13257}
13258
13259static int intel_crtc_set_config(struct drm_mode_set *set)
13260{
13261 struct drm_device *dev;
83a57153 13262 struct drm_atomic_state *state = NULL;
2e431051 13263 int ret;
2e431051 13264
8d3e375e
DV
13265 BUG_ON(!set);
13266 BUG_ON(!set->crtc);
13267 BUG_ON(!set->crtc->helper_private);
2e431051 13268
7e53f3a4
DV
13269 /* Enforce sane interface api - has been abused by the fb helper. */
13270 BUG_ON(!set->mode && set->fb);
13271 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13272
2e431051
DV
13273 if (set->fb) {
13274 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13275 set->crtc->base.id, set->fb->base.id,
13276 (int)set->num_connectors, set->x, set->y);
13277 } else {
13278 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13279 }
13280
13281 dev = set->crtc->dev;
13282
83a57153 13283 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13284 if (!state)
13285 return -ENOMEM;
83a57153
ACO
13286
13287 state->acquire_ctx = dev->mode_config.acquire_ctx;
13288
462a425a 13289 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13290 if (ret)
7cbf41d6 13291 goto out;
2e431051 13292
568c634a
ACO
13293 ret = intel_modeset_compute_config(state);
13294 if (ret)
7cbf41d6 13295 goto out;
50f52756 13296
1f9954d0
JB
13297 intel_update_pipe_size(to_intel_crtc(set->crtc));
13298
568c634a 13299 ret = intel_set_mode_checked(state);
2d05eae1 13300 if (ret) {
bf67dfeb
DV
13301 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13302 set->crtc->base.id, ret);
2d05eae1 13303 }
50f56119 13304
7cbf41d6 13305out:
2bfb4627
ACO
13306 if (ret)
13307 drm_atomic_state_free(state);
50f56119
DV
13308 return ret;
13309}
f6e5b160
CW
13310
13311static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13312 .gamma_set = intel_crtc_gamma_set,
50f56119 13313 .set_config = intel_crtc_set_config,
f6e5b160
CW
13314 .destroy = intel_crtc_destroy,
13315 .page_flip = intel_crtc_page_flip,
1356837e
MR
13316 .atomic_duplicate_state = intel_crtc_duplicate_state,
13317 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13318};
13319
5358901f
DV
13320static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13321 struct intel_shared_dpll *pll,
13322 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13323{
5358901f 13324 uint32_t val;
ee7b9f93 13325
f458ebbc 13326 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13327 return false;
13328
5358901f 13329 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13330 hw_state->dpll = val;
13331 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13332 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13333
13334 return val & DPLL_VCO_ENABLE;
13335}
13336
15bdd4cf
DV
13337static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13338 struct intel_shared_dpll *pll)
13339{
3e369b76
ACO
13340 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13341 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13342}
13343
e7b903d2
DV
13344static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13345 struct intel_shared_dpll *pll)
13346{
e7b903d2 13347 /* PCH refclock must be enabled first */
89eff4be 13348 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13349
3e369b76 13350 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13351
13352 /* Wait for the clocks to stabilize. */
13353 POSTING_READ(PCH_DPLL(pll->id));
13354 udelay(150);
13355
13356 /* The pixel multiplier can only be updated once the
13357 * DPLL is enabled and the clocks are stable.
13358 *
13359 * So write it again.
13360 */
3e369b76 13361 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13362 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13363 udelay(200);
13364}
13365
13366static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13367 struct intel_shared_dpll *pll)
13368{
13369 struct drm_device *dev = dev_priv->dev;
13370 struct intel_crtc *crtc;
e7b903d2
DV
13371
13372 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13373 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13374 if (intel_crtc_to_shared_dpll(crtc) == pll)
13375 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13376 }
13377
15bdd4cf
DV
13378 I915_WRITE(PCH_DPLL(pll->id), 0);
13379 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13380 udelay(200);
13381}
13382
46edb027
DV
13383static char *ibx_pch_dpll_names[] = {
13384 "PCH DPLL A",
13385 "PCH DPLL B",
13386};
13387
7c74ade1 13388static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13389{
e7b903d2 13390 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13391 int i;
13392
7c74ade1 13393 dev_priv->num_shared_dpll = 2;
ee7b9f93 13394
e72f9fbf 13395 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13396 dev_priv->shared_dplls[i].id = i;
13397 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13398 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13399 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13400 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13401 dev_priv->shared_dplls[i].get_hw_state =
13402 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13403 }
13404}
13405
7c74ade1
DV
13406static void intel_shared_dpll_init(struct drm_device *dev)
13407{
e7b903d2 13408 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13409
b6283055
VS
13410 intel_update_cdclk(dev);
13411
9cd86933
DV
13412 if (HAS_DDI(dev))
13413 intel_ddi_pll_init(dev);
13414 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13415 ibx_pch_dpll_init(dev);
13416 else
13417 dev_priv->num_shared_dpll = 0;
13418
13419 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13420}
13421
1fc0a8f7
TU
13422/**
13423 * intel_wm_need_update - Check whether watermarks need updating
13424 * @plane: drm plane
13425 * @state: new plane state
13426 *
13427 * Check current plane state versus the new one to determine whether
13428 * watermarks need to be recalculated.
13429 *
13430 * Returns true or false.
13431 */
13432bool intel_wm_need_update(struct drm_plane *plane,
13433 struct drm_plane_state *state)
13434{
13435 /* Update watermarks on tiling changes. */
13436 if (!plane->state->fb || !state->fb ||
13437 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13438 plane->state->rotation != state->rotation)
13439 return true;
13440
13441 return false;
13442}
13443
6beb8c23
MR
13444/**
13445 * intel_prepare_plane_fb - Prepare fb for usage on plane
13446 * @plane: drm plane to prepare for
13447 * @fb: framebuffer to prepare for presentation
13448 *
13449 * Prepares a framebuffer for usage on a display plane. Generally this
13450 * involves pinning the underlying object and updating the frontbuffer tracking
13451 * bits. Some older platforms need special physical address handling for
13452 * cursor planes.
13453 *
13454 * Returns 0 on success, negative error code on failure.
13455 */
13456int
13457intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13458 struct drm_framebuffer *fb,
13459 const struct drm_plane_state *new_state)
465c120c
MR
13460{
13461 struct drm_device *dev = plane->dev;
6beb8c23
MR
13462 struct intel_plane *intel_plane = to_intel_plane(plane);
13463 enum pipe pipe = intel_plane->pipe;
13464 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13465 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13466 unsigned frontbuffer_bits = 0;
13467 int ret = 0;
465c120c 13468
ea2c67bb 13469 if (!obj)
465c120c
MR
13470 return 0;
13471
6beb8c23
MR
13472 switch (plane->type) {
13473 case DRM_PLANE_TYPE_PRIMARY:
13474 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13475 break;
13476 case DRM_PLANE_TYPE_CURSOR:
13477 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13478 break;
13479 case DRM_PLANE_TYPE_OVERLAY:
13480 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13481 break;
13482 }
465c120c 13483
6beb8c23 13484 mutex_lock(&dev->struct_mutex);
465c120c 13485
6beb8c23
MR
13486 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13487 INTEL_INFO(dev)->cursor_needs_physical) {
13488 int align = IS_I830(dev) ? 16 * 1024 : 256;
13489 ret = i915_gem_object_attach_phys(obj, align);
13490 if (ret)
13491 DRM_DEBUG_KMS("failed to attach phys object\n");
13492 } else {
82bc3b2d 13493 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13494 }
465c120c 13495
6beb8c23
MR
13496 if (ret == 0)
13497 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13498
4c34574f 13499 mutex_unlock(&dev->struct_mutex);
465c120c 13500
6beb8c23
MR
13501 return ret;
13502}
13503
38f3ce3a
MR
13504/**
13505 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13506 * @plane: drm plane to clean up for
13507 * @fb: old framebuffer that was on plane
13508 *
13509 * Cleans up a framebuffer that has just been removed from a plane.
13510 */
13511void
13512intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13513 struct drm_framebuffer *fb,
13514 const struct drm_plane_state *old_state)
38f3ce3a
MR
13515{
13516 struct drm_device *dev = plane->dev;
13517 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13518
13519 if (WARN_ON(!obj))
13520 return;
13521
13522 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13523 !INTEL_INFO(dev)->cursor_needs_physical) {
13524 mutex_lock(&dev->struct_mutex);
82bc3b2d 13525 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13526 mutex_unlock(&dev->struct_mutex);
13527 }
465c120c
MR
13528}
13529
6156a456
CK
13530int
13531skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13532{
13533 int max_scale;
13534 struct drm_device *dev;
13535 struct drm_i915_private *dev_priv;
13536 int crtc_clock, cdclk;
13537
13538 if (!intel_crtc || !crtc_state)
13539 return DRM_PLANE_HELPER_NO_SCALING;
13540
13541 dev = intel_crtc->base.dev;
13542 dev_priv = dev->dev_private;
13543 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13544 cdclk = dev_priv->display.get_display_clock_speed(dev);
13545
13546 if (!crtc_clock || !cdclk)
13547 return DRM_PLANE_HELPER_NO_SCALING;
13548
13549 /*
13550 * skl max scale is lower of:
13551 * close to 3 but not 3, -1 is for that purpose
13552 * or
13553 * cdclk/crtc_clock
13554 */
13555 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13556
13557 return max_scale;
13558}
13559
465c120c 13560static int
3c692a41
GP
13561intel_check_primary_plane(struct drm_plane *plane,
13562 struct intel_plane_state *state)
13563{
32b7eeec
MR
13564 struct drm_device *dev = plane->dev;
13565 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13566 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13567 struct intel_crtc *intel_crtc;
6156a456 13568 struct intel_crtc_state *crtc_state;
2b875c22 13569 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13570 struct drm_rect *dest = &state->dst;
13571 struct drm_rect *src = &state->src;
13572 const struct drm_rect *clip = &state->clip;
d8106366 13573 bool can_position = false;
6156a456
CK
13574 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13575 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13576 int ret;
13577
ea2c67bb
MR
13578 crtc = crtc ? crtc : plane->crtc;
13579 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13580 crtc_state = state->base.state ?
13581 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13582
6156a456 13583 if (INTEL_INFO(dev)->gen >= 9) {
225c228a
CK
13584 /* use scaler when colorkey is not required */
13585 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13586 min_scale = 1;
13587 max_scale = skl_max_scale(intel_crtc, crtc_state);
13588 }
d8106366 13589 can_position = true;
6156a456 13590 }
d8106366 13591
c59cb179
MR
13592 ret = drm_plane_helper_check_update(plane, crtc, fb,
13593 src, dest, clip,
6156a456
CK
13594 min_scale,
13595 max_scale,
d8106366
SJ
13596 can_position, true,
13597 &state->visible);
c59cb179
MR
13598 if (ret)
13599 return ret;
465c120c 13600
32b7eeec 13601 if (intel_crtc->active) {
b70709a6
ML
13602 struct intel_plane_state *old_state =
13603 to_intel_plane_state(plane->state);
13604
32b7eeec
MR
13605 intel_crtc->atomic.wait_for_flips = true;
13606
13607 /*
13608 * FBC does not work on some platforms for rotated
13609 * planes, so disable it when rotation is not 0 and
13610 * update it when rotation is set back to 0.
13611 *
13612 * FIXME: This is redundant with the fbc update done in
13613 * the primary plane enable function except that that
13614 * one is done too late. We eventually need to unify
13615 * this.
13616 */
b70709a6 13617 if (state->visible &&
32b7eeec 13618 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13619 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13620 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13621 intel_crtc->atomic.disable_fbc = true;
13622 }
13623
b70709a6 13624 if (state->visible && !old_state->visible) {
32b7eeec
MR
13625 /*
13626 * BDW signals flip done immediately if the plane
13627 * is disabled, even if the plane enable is already
13628 * armed to occur at the next vblank :(
13629 */
b70709a6 13630 if (IS_BROADWELL(dev))
32b7eeec 13631 intel_crtc->atomic.wait_vblank = true;
fb9d6cf8
ML
13632
13633 if (crtc_state && !needs_modeset(&crtc_state->base))
13634 intel_crtc->atomic.post_enable_primary = true;
32b7eeec
MR
13635 }
13636
fb9d6cf8
ML
13637 if (!state->visible && old_state->visible &&
13638 crtc_state && !needs_modeset(&crtc_state->base))
13639 intel_crtc->atomic.pre_disable_primary = true;
13640
32b7eeec
MR
13641 intel_crtc->atomic.fb_bits |=
13642 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13643
13644 intel_crtc->atomic.update_fbc = true;
0fda6568 13645
1fc0a8f7 13646 if (intel_wm_need_update(plane, &state->base))
0fda6568 13647 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13648 }
13649
6156a456
CK
13650 if (INTEL_INFO(dev)->gen >= 9) {
13651 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13652 to_intel_plane(plane), state, 0);
13653 if (ret)
13654 return ret;
13655 }
13656
14af293f
GP
13657 return 0;
13658}
13659
13660static void
13661intel_commit_primary_plane(struct drm_plane *plane,
13662 struct intel_plane_state *state)
13663{
2b875c22
MR
13664 struct drm_crtc *crtc = state->base.crtc;
13665 struct drm_framebuffer *fb = state->base.fb;
13666 struct drm_device *dev = plane->dev;
14af293f 13667 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13668 struct intel_crtc *intel_crtc;
14af293f
GP
13669 struct drm_rect *src = &state->src;
13670
ea2c67bb
MR
13671 crtc = crtc ? crtc : plane->crtc;
13672 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13673
13674 plane->fb = fb;
9dc806fc
MR
13675 crtc->x = src->x1 >> 16;
13676 crtc->y = src->y1 >> 16;
ccc759dc 13677
ccc759dc 13678 if (intel_crtc->active) {
27321ae8 13679 if (state->visible)
ccc759dc
GP
13680 /* FIXME: kill this fastboot hack */
13681 intel_update_pipe_size(intel_crtc);
465c120c 13682
27321ae8
ML
13683 dev_priv->display.update_primary_plane(crtc, plane->fb,
13684 crtc->x, crtc->y);
ccc759dc 13685 }
465c120c
MR
13686}
13687
a8ad0d8e
ML
13688static void
13689intel_disable_primary_plane(struct drm_plane *plane,
13690 struct drm_crtc *crtc,
13691 bool force)
13692{
13693 struct drm_device *dev = plane->dev;
13694 struct drm_i915_private *dev_priv = dev->dev_private;
13695
a8ad0d8e
ML
13696 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13697}
13698
32b7eeec 13699static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13700{
32b7eeec 13701 struct drm_device *dev = crtc->dev;
140fd38d 13702 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5c2db188 13704 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
ea2c67bb
MR
13705 struct intel_plane *intel_plane;
13706 struct drm_plane *p;
13707 unsigned fb_bits = 0;
13708
13709 /* Track fb's for any planes being disabled */
13710 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13711 intel_plane = to_intel_plane(p);
13712
13713 if (intel_crtc->atomic.disabled_planes &
13714 (1 << drm_plane_index(p))) {
13715 switch (p->type) {
13716 case DRM_PLANE_TYPE_PRIMARY:
13717 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13718 break;
13719 case DRM_PLANE_TYPE_CURSOR:
13720 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13721 break;
13722 case DRM_PLANE_TYPE_OVERLAY:
13723 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13724 break;
13725 }
3c692a41 13726
ea2c67bb
MR
13727 mutex_lock(&dev->struct_mutex);
13728 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13729 mutex_unlock(&dev->struct_mutex);
13730 }
13731 }
3c692a41 13732
32b7eeec
MR
13733 if (intel_crtc->atomic.wait_for_flips)
13734 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13735
32b7eeec
MR
13736 if (intel_crtc->atomic.disable_fbc)
13737 intel_fbc_disable(dev);
3c692a41 13738
32b7eeec
MR
13739 if (intel_crtc->atomic.pre_disable_primary)
13740 intel_pre_disable_primary(crtc);
3c692a41 13741
32b7eeec
MR
13742 if (intel_crtc->atomic.update_wm)
13743 intel_update_watermarks(crtc);
3c692a41 13744
32b7eeec 13745 intel_runtime_pm_get(dev_priv);
3c692a41 13746
c34c9ee4 13747 /* Perform vblank evasion around commit operation */
5c2db188 13748 if (crtc_state->active && !needs_modeset(crtc_state))
c34c9ee4
MR
13749 intel_crtc->atomic.evade =
13750 intel_pipe_update_start(intel_crtc,
13751 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13752}
13753
13754static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13755{
13756 struct drm_device *dev = crtc->dev;
13757 struct drm_i915_private *dev_priv = dev->dev_private;
13758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13759 struct drm_plane *p;
13760
c34c9ee4
MR
13761 if (intel_crtc->atomic.evade)
13762 intel_pipe_update_end(intel_crtc,
13763 intel_crtc->atomic.start_vbl_count);
3c692a41 13764
140fd38d 13765 intel_runtime_pm_put(dev_priv);
3c692a41 13766
8a8f7f44 13767 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
32b7eeec
MR
13768 intel_wait_for_vblank(dev, intel_crtc->pipe);
13769
13770 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13771
13772 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13773 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13774 intel_fbc_update(dev);
ccc759dc 13775 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13776 }
3c692a41 13777
32b7eeec
MR
13778 if (intel_crtc->atomic.post_enable_primary)
13779 intel_post_enable_primary(crtc);
3c692a41 13780
32b7eeec
MR
13781 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13782 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13783 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13784 false, false);
13785
13786 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13787}
13788
cf4c7c12 13789/**
4a3b8769
MR
13790 * intel_plane_destroy - destroy a plane
13791 * @plane: plane to destroy
cf4c7c12 13792 *
4a3b8769
MR
13793 * Common destruction function for all types of planes (primary, cursor,
13794 * sprite).
cf4c7c12 13795 */
4a3b8769 13796void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13797{
13798 struct intel_plane *intel_plane = to_intel_plane(plane);
13799 drm_plane_cleanup(plane);
13800 kfree(intel_plane);
13801}
13802
65a3fea0 13803const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13804 .update_plane = drm_atomic_helper_update_plane,
13805 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13806 .destroy = intel_plane_destroy,
c196e1d6 13807 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13808 .atomic_get_property = intel_plane_atomic_get_property,
13809 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13810 .atomic_duplicate_state = intel_plane_duplicate_state,
13811 .atomic_destroy_state = intel_plane_destroy_state,
13812
465c120c
MR
13813};
13814
13815static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13816 int pipe)
13817{
13818 struct intel_plane *primary;
8e7d688b 13819 struct intel_plane_state *state;
465c120c
MR
13820 const uint32_t *intel_primary_formats;
13821 int num_formats;
13822
13823 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13824 if (primary == NULL)
13825 return NULL;
13826
8e7d688b
MR
13827 state = intel_create_plane_state(&primary->base);
13828 if (!state) {
ea2c67bb
MR
13829 kfree(primary);
13830 return NULL;
13831 }
8e7d688b 13832 primary->base.state = &state->base;
ea2c67bb 13833
465c120c
MR
13834 primary->can_scale = false;
13835 primary->max_downscale = 1;
6156a456
CK
13836 if (INTEL_INFO(dev)->gen >= 9) {
13837 primary->can_scale = true;
af99ceda 13838 state->scaler_id = -1;
6156a456 13839 }
465c120c
MR
13840 primary->pipe = pipe;
13841 primary->plane = pipe;
c59cb179
MR
13842 primary->check_plane = intel_check_primary_plane;
13843 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13844 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13845 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13846 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13847 primary->plane = !pipe;
13848
6c0fd451
DL
13849 if (INTEL_INFO(dev)->gen >= 9) {
13850 intel_primary_formats = skl_primary_formats;
13851 num_formats = ARRAY_SIZE(skl_primary_formats);
13852 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13853 intel_primary_formats = i965_primary_formats;
13854 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13855 } else {
13856 intel_primary_formats = i8xx_primary_formats;
13857 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13858 }
13859
13860 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13861 &intel_plane_funcs,
465c120c
MR
13862 intel_primary_formats, num_formats,
13863 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13864
3b7a5119
SJ
13865 if (INTEL_INFO(dev)->gen >= 4)
13866 intel_create_rotation_property(dev, primary);
48404c1e 13867
ea2c67bb
MR
13868 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13869
465c120c
MR
13870 return &primary->base;
13871}
13872
3b7a5119
SJ
13873void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13874{
13875 if (!dev->mode_config.rotation_property) {
13876 unsigned long flags = BIT(DRM_ROTATE_0) |
13877 BIT(DRM_ROTATE_180);
13878
13879 if (INTEL_INFO(dev)->gen >= 9)
13880 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13881
13882 dev->mode_config.rotation_property =
13883 drm_mode_create_rotation_property(dev, flags);
13884 }
13885 if (dev->mode_config.rotation_property)
13886 drm_object_attach_property(&plane->base.base,
13887 dev->mode_config.rotation_property,
13888 plane->base.state->rotation);
13889}
13890
3d7d6510 13891static int
852e787c
GP
13892intel_check_cursor_plane(struct drm_plane *plane,
13893 struct intel_plane_state *state)
3d7d6510 13894{
2b875c22 13895 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13896 struct drm_device *dev = plane->dev;
2b875c22 13897 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13898 struct drm_rect *dest = &state->dst;
13899 struct drm_rect *src = &state->src;
13900 const struct drm_rect *clip = &state->clip;
757f9a3e 13901 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13902 struct intel_crtc *intel_crtc;
757f9a3e
GP
13903 unsigned stride;
13904 int ret;
3d7d6510 13905
ea2c67bb
MR
13906 crtc = crtc ? crtc : plane->crtc;
13907 intel_crtc = to_intel_crtc(crtc);
13908
757f9a3e 13909 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13910 src, dest, clip,
3d7d6510
MR
13911 DRM_PLANE_HELPER_NO_SCALING,
13912 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13913 true, true, &state->visible);
757f9a3e
GP
13914 if (ret)
13915 return ret;
13916
13917
13918 /* if we want to turn off the cursor ignore width and height */
13919 if (!obj)
32b7eeec 13920 goto finish;
757f9a3e 13921
757f9a3e 13922 /* Check for which cursor types we support */
ea2c67bb
MR
13923 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13924 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13925 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13926 return -EINVAL;
13927 }
13928
ea2c67bb
MR
13929 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13930 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13931 DRM_DEBUG_KMS("buffer is too small\n");
13932 return -ENOMEM;
13933 }
13934
3a656b54 13935 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13936 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13937 ret = -EINVAL;
13938 }
757f9a3e 13939
32b7eeec
MR
13940finish:
13941 if (intel_crtc->active) {
3749f463 13942 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13943 intel_crtc->atomic.update_wm = true;
13944
13945 intel_crtc->atomic.fb_bits |=
13946 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13947 }
13948
757f9a3e 13949 return ret;
852e787c 13950}
3d7d6510 13951
a8ad0d8e
ML
13952static void
13953intel_disable_cursor_plane(struct drm_plane *plane,
13954 struct drm_crtc *crtc,
13955 bool force)
13956{
13957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13958
13959 if (!force) {
13960 plane->fb = NULL;
13961 intel_crtc->cursor_bo = NULL;
13962 intel_crtc->cursor_addr = 0;
13963 }
13964
13965 intel_crtc_update_cursor(crtc, false);
13966}
13967
f4a2cf29 13968static void
852e787c
GP
13969intel_commit_cursor_plane(struct drm_plane *plane,
13970 struct intel_plane_state *state)
13971{
2b875c22 13972 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13973 struct drm_device *dev = plane->dev;
13974 struct intel_crtc *intel_crtc;
2b875c22 13975 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13976 uint32_t addr;
852e787c 13977
ea2c67bb
MR
13978 crtc = crtc ? crtc : plane->crtc;
13979 intel_crtc = to_intel_crtc(crtc);
13980
2b875c22 13981 plane->fb = state->base.fb;
ea2c67bb
MR
13982 crtc->cursor_x = state->base.crtc_x;
13983 crtc->cursor_y = state->base.crtc_y;
13984
a912f12f
GP
13985 if (intel_crtc->cursor_bo == obj)
13986 goto update;
4ed91096 13987
f4a2cf29 13988 if (!obj)
a912f12f 13989 addr = 0;
f4a2cf29 13990 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13991 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13992 else
a912f12f 13993 addr = obj->phys_handle->busaddr;
852e787c 13994
a912f12f
GP
13995 intel_crtc->cursor_addr = addr;
13996 intel_crtc->cursor_bo = obj;
13997update:
852e787c 13998
32b7eeec 13999 if (intel_crtc->active)
a912f12f 14000 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
14001}
14002
3d7d6510
MR
14003static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14004 int pipe)
14005{
14006 struct intel_plane *cursor;
8e7d688b 14007 struct intel_plane_state *state;
3d7d6510
MR
14008
14009 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14010 if (cursor == NULL)
14011 return NULL;
14012
8e7d688b
MR
14013 state = intel_create_plane_state(&cursor->base);
14014 if (!state) {
ea2c67bb
MR
14015 kfree(cursor);
14016 return NULL;
14017 }
8e7d688b 14018 cursor->base.state = &state->base;
ea2c67bb 14019
3d7d6510
MR
14020 cursor->can_scale = false;
14021 cursor->max_downscale = 1;
14022 cursor->pipe = pipe;
14023 cursor->plane = pipe;
c59cb179
MR
14024 cursor->check_plane = intel_check_cursor_plane;
14025 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 14026 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14027
14028 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14029 &intel_plane_funcs,
3d7d6510
MR
14030 intel_cursor_formats,
14031 ARRAY_SIZE(intel_cursor_formats),
14032 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
14033
14034 if (INTEL_INFO(dev)->gen >= 4) {
14035 if (!dev->mode_config.rotation_property)
14036 dev->mode_config.rotation_property =
14037 drm_mode_create_rotation_property(dev,
14038 BIT(DRM_ROTATE_0) |
14039 BIT(DRM_ROTATE_180));
14040 if (dev->mode_config.rotation_property)
14041 drm_object_attach_property(&cursor->base.base,
14042 dev->mode_config.rotation_property,
8e7d688b 14043 state->base.rotation);
4398ad45
VS
14044 }
14045
af99ceda
CK
14046 if (INTEL_INFO(dev)->gen >=9)
14047 state->scaler_id = -1;
14048
ea2c67bb
MR
14049 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14050
3d7d6510
MR
14051 return &cursor->base;
14052}
14053
549e2bfb
CK
14054static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14055 struct intel_crtc_state *crtc_state)
14056{
14057 int i;
14058 struct intel_scaler *intel_scaler;
14059 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14060
14061 for (i = 0; i < intel_crtc->num_scalers; i++) {
14062 intel_scaler = &scaler_state->scalers[i];
14063 intel_scaler->in_use = 0;
14064 intel_scaler->id = i;
14065
14066 intel_scaler->mode = PS_SCALER_MODE_DYN;
14067 }
14068
14069 scaler_state->scaler_id = -1;
14070}
14071
b358d0a6 14072static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14073{
fbee40df 14074 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14075 struct intel_crtc *intel_crtc;
f5de6e07 14076 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14077 struct drm_plane *primary = NULL;
14078 struct drm_plane *cursor = NULL;
465c120c 14079 int i, ret;
79e53945 14080
955382f3 14081 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14082 if (intel_crtc == NULL)
14083 return;
14084
f5de6e07
ACO
14085 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14086 if (!crtc_state)
14087 goto fail;
550acefd
ACO
14088 intel_crtc->config = crtc_state;
14089 intel_crtc->base.state = &crtc_state->base;
07878248 14090 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14091
549e2bfb
CK
14092 /* initialize shared scalers */
14093 if (INTEL_INFO(dev)->gen >= 9) {
14094 if (pipe == PIPE_C)
14095 intel_crtc->num_scalers = 1;
14096 else
14097 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14098
14099 skl_init_scalers(dev, intel_crtc, crtc_state);
14100 }
14101
465c120c 14102 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14103 if (!primary)
14104 goto fail;
14105
14106 cursor = intel_cursor_plane_create(dev, pipe);
14107 if (!cursor)
14108 goto fail;
14109
465c120c 14110 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14111 cursor, &intel_crtc_funcs);
14112 if (ret)
14113 goto fail;
79e53945
JB
14114
14115 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14116 for (i = 0; i < 256; i++) {
14117 intel_crtc->lut_r[i] = i;
14118 intel_crtc->lut_g[i] = i;
14119 intel_crtc->lut_b[i] = i;
14120 }
14121
1f1c2e24
VS
14122 /*
14123 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14124 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14125 */
80824003
JB
14126 intel_crtc->pipe = pipe;
14127 intel_crtc->plane = pipe;
3a77c4c4 14128 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14129 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14130 intel_crtc->plane = !pipe;
80824003
JB
14131 }
14132
4b0e333e
CW
14133 intel_crtc->cursor_base = ~0;
14134 intel_crtc->cursor_cntl = ~0;
dc41c154 14135 intel_crtc->cursor_size = ~0;
8d7849db 14136
22fd0fab
JB
14137 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14138 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14139 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14140 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14141
79e53945 14142 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14143
14144 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14145 return;
14146
14147fail:
14148 if (primary)
14149 drm_plane_cleanup(primary);
14150 if (cursor)
14151 drm_plane_cleanup(cursor);
f5de6e07 14152 kfree(crtc_state);
3d7d6510 14153 kfree(intel_crtc);
79e53945
JB
14154}
14155
752aa88a
JB
14156enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14157{
14158 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14159 struct drm_device *dev = connector->base.dev;
752aa88a 14160
51fd371b 14161 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14162
d3babd3f 14163 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14164 return INVALID_PIPE;
14165
14166 return to_intel_crtc(encoder->crtc)->pipe;
14167}
14168
08d7b3d1 14169int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14170 struct drm_file *file)
08d7b3d1 14171{
08d7b3d1 14172 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14173 struct drm_crtc *drmmode_crtc;
c05422d5 14174 struct intel_crtc *crtc;
08d7b3d1 14175
7707e653 14176 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14177
7707e653 14178 if (!drmmode_crtc) {
08d7b3d1 14179 DRM_ERROR("no such CRTC id\n");
3f2c2057 14180 return -ENOENT;
08d7b3d1
CW
14181 }
14182
7707e653 14183 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14184 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14185
c05422d5 14186 return 0;
08d7b3d1
CW
14187}
14188
66a9278e 14189static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14190{
66a9278e
DV
14191 struct drm_device *dev = encoder->base.dev;
14192 struct intel_encoder *source_encoder;
79e53945 14193 int index_mask = 0;
79e53945
JB
14194 int entry = 0;
14195
b2784e15 14196 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14197 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14198 index_mask |= (1 << entry);
14199
79e53945
JB
14200 entry++;
14201 }
4ef69c7a 14202
79e53945
JB
14203 return index_mask;
14204}
14205
4d302442
CW
14206static bool has_edp_a(struct drm_device *dev)
14207{
14208 struct drm_i915_private *dev_priv = dev->dev_private;
14209
14210 if (!IS_MOBILE(dev))
14211 return false;
14212
14213 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14214 return false;
14215
e3589908 14216 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14217 return false;
14218
14219 return true;
14220}
14221
84b4e042
JB
14222static bool intel_crt_present(struct drm_device *dev)
14223{
14224 struct drm_i915_private *dev_priv = dev->dev_private;
14225
884497ed
DL
14226 if (INTEL_INFO(dev)->gen >= 9)
14227 return false;
14228
cf404ce4 14229 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14230 return false;
14231
14232 if (IS_CHERRYVIEW(dev))
14233 return false;
14234
14235 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14236 return false;
14237
14238 return true;
14239}
14240
79e53945
JB
14241static void intel_setup_outputs(struct drm_device *dev)
14242{
725e30ad 14243 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14244 struct intel_encoder *encoder;
cb0953d7 14245 bool dpd_is_edp = false;
79e53945 14246
c9093354 14247 intel_lvds_init(dev);
79e53945 14248
84b4e042 14249 if (intel_crt_present(dev))
79935fca 14250 intel_crt_init(dev);
cb0953d7 14251
c776eb2e
VK
14252 if (IS_BROXTON(dev)) {
14253 /*
14254 * FIXME: Broxton doesn't support port detection via the
14255 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14256 * detect the ports.
14257 */
14258 intel_ddi_init(dev, PORT_A);
14259 intel_ddi_init(dev, PORT_B);
14260 intel_ddi_init(dev, PORT_C);
14261 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14262 int found;
14263
de31facd
JB
14264 /*
14265 * Haswell uses DDI functions to detect digital outputs.
14266 * On SKL pre-D0 the strap isn't connected, so we assume
14267 * it's there.
14268 */
0e72a5b5 14269 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14270 /* WaIgnoreDDIAStrap: skl */
14271 if (found ||
14272 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14273 intel_ddi_init(dev, PORT_A);
14274
14275 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14276 * register */
14277 found = I915_READ(SFUSE_STRAP);
14278
14279 if (found & SFUSE_STRAP_DDIB_DETECTED)
14280 intel_ddi_init(dev, PORT_B);
14281 if (found & SFUSE_STRAP_DDIC_DETECTED)
14282 intel_ddi_init(dev, PORT_C);
14283 if (found & SFUSE_STRAP_DDID_DETECTED)
14284 intel_ddi_init(dev, PORT_D);
14285 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14286 int found;
5d8a7752 14287 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14288
14289 if (has_edp_a(dev))
14290 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14291
dc0fa718 14292 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14293 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14294 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14295 if (!found)
e2debe91 14296 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14297 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14298 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14299 }
14300
dc0fa718 14301 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14302 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14303
dc0fa718 14304 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14305 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14306
5eb08b69 14307 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14308 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14309
270b3042 14310 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14311 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14312 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14313 /*
14314 * The DP_DETECTED bit is the latched state of the DDC
14315 * SDA pin at boot. However since eDP doesn't require DDC
14316 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14317 * eDP ports may have been muxed to an alternate function.
14318 * Thus we can't rely on the DP_DETECTED bit alone to detect
14319 * eDP ports. Consult the VBT as well as DP_DETECTED to
14320 * detect eDP ports.
14321 */
d2182a66
VS
14322 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14323 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14324 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14325 PORT_B);
e17ac6db
VS
14326 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14327 intel_dp_is_edp(dev, PORT_B))
14328 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14329
d2182a66
VS
14330 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14331 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14332 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14333 PORT_C);
e17ac6db
VS
14334 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14335 intel_dp_is_edp(dev, PORT_C))
14336 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14337
9418c1f1 14338 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14339 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14340 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14341 PORT_D);
e17ac6db
VS
14342 /* eDP not supported on port D, so don't check VBT */
14343 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14344 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14345 }
14346
3cfca973 14347 intel_dsi_init(dev);
103a196f 14348 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14349 bool found = false;
7d57382e 14350
e2debe91 14351 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14352 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14353 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14354 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14355 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14356 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14357 }
27185ae1 14358
e7281eab 14359 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14360 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14361 }
13520b05
KH
14362
14363 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14364
e2debe91 14365 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14366 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14367 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14368 }
27185ae1 14369
e2debe91 14370 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14371
b01f2c3a
JB
14372 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14373 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14374 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14375 }
e7281eab 14376 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14377 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14378 }
27185ae1 14379
b01f2c3a 14380 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14381 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14382 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14383 } else if (IS_GEN2(dev))
79e53945
JB
14384 intel_dvo_init(dev);
14385
103a196f 14386 if (SUPPORTS_TV(dev))
79e53945
JB
14387 intel_tv_init(dev);
14388
0bc12bcb 14389 intel_psr_init(dev);
7c8f8a70 14390
b2784e15 14391 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14392 encoder->base.possible_crtcs = encoder->crtc_mask;
14393 encoder->base.possible_clones =
66a9278e 14394 intel_encoder_clones(encoder);
79e53945 14395 }
47356eb6 14396
dde86e2d 14397 intel_init_pch_refclk(dev);
270b3042
DV
14398
14399 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14400}
14401
14402static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14403{
60a5ca01 14404 struct drm_device *dev = fb->dev;
79e53945 14405 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14406
ef2d633e 14407 drm_framebuffer_cleanup(fb);
60a5ca01 14408 mutex_lock(&dev->struct_mutex);
ef2d633e 14409 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14410 drm_gem_object_unreference(&intel_fb->obj->base);
14411 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14412 kfree(intel_fb);
14413}
14414
14415static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14416 struct drm_file *file,
79e53945
JB
14417 unsigned int *handle)
14418{
14419 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14420 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14421
05394f39 14422 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14423}
14424
14425static const struct drm_framebuffer_funcs intel_fb_funcs = {
14426 .destroy = intel_user_framebuffer_destroy,
14427 .create_handle = intel_user_framebuffer_create_handle,
14428};
14429
b321803d
DL
14430static
14431u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14432 uint32_t pixel_format)
14433{
14434 u32 gen = INTEL_INFO(dev)->gen;
14435
14436 if (gen >= 9) {
14437 /* "The stride in bytes must not exceed the of the size of 8K
14438 * pixels and 32K bytes."
14439 */
14440 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14441 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14442 return 32*1024;
14443 } else if (gen >= 4) {
14444 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14445 return 16*1024;
14446 else
14447 return 32*1024;
14448 } else if (gen >= 3) {
14449 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14450 return 8*1024;
14451 else
14452 return 16*1024;
14453 } else {
14454 /* XXX DSPC is limited to 4k tiled */
14455 return 8*1024;
14456 }
14457}
14458
b5ea642a
DV
14459static int intel_framebuffer_init(struct drm_device *dev,
14460 struct intel_framebuffer *intel_fb,
14461 struct drm_mode_fb_cmd2 *mode_cmd,
14462 struct drm_i915_gem_object *obj)
79e53945 14463{
6761dd31 14464 unsigned int aligned_height;
79e53945 14465 int ret;
b321803d 14466 u32 pitch_limit, stride_alignment;
79e53945 14467
dd4916c5
DV
14468 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14469
2a80eada
DV
14470 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14471 /* Enforce that fb modifier and tiling mode match, but only for
14472 * X-tiled. This is needed for FBC. */
14473 if (!!(obj->tiling_mode == I915_TILING_X) !=
14474 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14475 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14476 return -EINVAL;
14477 }
14478 } else {
14479 if (obj->tiling_mode == I915_TILING_X)
14480 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14481 else if (obj->tiling_mode == I915_TILING_Y) {
14482 DRM_DEBUG("No Y tiling for legacy addfb\n");
14483 return -EINVAL;
14484 }
14485 }
14486
9a8f0a12
TU
14487 /* Passed in modifier sanity checking. */
14488 switch (mode_cmd->modifier[0]) {
14489 case I915_FORMAT_MOD_Y_TILED:
14490 case I915_FORMAT_MOD_Yf_TILED:
14491 if (INTEL_INFO(dev)->gen < 9) {
14492 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14493 mode_cmd->modifier[0]);
14494 return -EINVAL;
14495 }
14496 case DRM_FORMAT_MOD_NONE:
14497 case I915_FORMAT_MOD_X_TILED:
14498 break;
14499 default:
c0f40428
JB
14500 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14501 mode_cmd->modifier[0]);
57cd6508 14502 return -EINVAL;
c16ed4be 14503 }
57cd6508 14504
b321803d
DL
14505 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14506 mode_cmd->pixel_format);
14507 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14508 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14509 mode_cmd->pitches[0], stride_alignment);
57cd6508 14510 return -EINVAL;
c16ed4be 14511 }
57cd6508 14512
b321803d
DL
14513 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14514 mode_cmd->pixel_format);
a35cdaa0 14515 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14516 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14517 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14518 "tiled" : "linear",
a35cdaa0 14519 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14520 return -EINVAL;
c16ed4be 14521 }
5d7bd705 14522
2a80eada 14523 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14524 mode_cmd->pitches[0] != obj->stride) {
14525 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14526 mode_cmd->pitches[0], obj->stride);
5d7bd705 14527 return -EINVAL;
c16ed4be 14528 }
5d7bd705 14529
57779d06 14530 /* Reject formats not supported by any plane early. */
308e5bcb 14531 switch (mode_cmd->pixel_format) {
57779d06 14532 case DRM_FORMAT_C8:
04b3924d
VS
14533 case DRM_FORMAT_RGB565:
14534 case DRM_FORMAT_XRGB8888:
14535 case DRM_FORMAT_ARGB8888:
57779d06
VS
14536 break;
14537 case DRM_FORMAT_XRGB1555:
c16ed4be 14538 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14539 DRM_DEBUG("unsupported pixel format: %s\n",
14540 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14541 return -EINVAL;
c16ed4be 14542 }
57779d06 14543 break;
57779d06 14544 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14545 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14546 DRM_DEBUG("unsupported pixel format: %s\n",
14547 drm_get_format_name(mode_cmd->pixel_format));
14548 return -EINVAL;
14549 }
14550 break;
14551 case DRM_FORMAT_XBGR8888:
04b3924d 14552 case DRM_FORMAT_XRGB2101010:
57779d06 14553 case DRM_FORMAT_XBGR2101010:
c16ed4be 14554 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14555 DRM_DEBUG("unsupported pixel format: %s\n",
14556 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14557 return -EINVAL;
c16ed4be 14558 }
b5626747 14559 break;
7531208b
DL
14560 case DRM_FORMAT_ABGR2101010:
14561 if (!IS_VALLEYVIEW(dev)) {
14562 DRM_DEBUG("unsupported pixel format: %s\n",
14563 drm_get_format_name(mode_cmd->pixel_format));
14564 return -EINVAL;
14565 }
14566 break;
04b3924d
VS
14567 case DRM_FORMAT_YUYV:
14568 case DRM_FORMAT_UYVY:
14569 case DRM_FORMAT_YVYU:
14570 case DRM_FORMAT_VYUY:
c16ed4be 14571 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14572 DRM_DEBUG("unsupported pixel format: %s\n",
14573 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14574 return -EINVAL;
c16ed4be 14575 }
57cd6508
CW
14576 break;
14577 default:
4ee62c76
VS
14578 DRM_DEBUG("unsupported pixel format: %s\n",
14579 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14580 return -EINVAL;
14581 }
14582
90f9a336
VS
14583 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14584 if (mode_cmd->offsets[0] != 0)
14585 return -EINVAL;
14586
ec2c981e 14587 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14588 mode_cmd->pixel_format,
14589 mode_cmd->modifier[0]);
53155c0a
DV
14590 /* FIXME drm helper for size checks (especially planar formats)? */
14591 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14592 return -EINVAL;
14593
c7d73f6a
DV
14594 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14595 intel_fb->obj = obj;
80075d49 14596 intel_fb->obj->framebuffer_references++;
c7d73f6a 14597
79e53945
JB
14598 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14599 if (ret) {
14600 DRM_ERROR("framebuffer init failed %d\n", ret);
14601 return ret;
14602 }
14603
79e53945
JB
14604 return 0;
14605}
14606
79e53945
JB
14607static struct drm_framebuffer *
14608intel_user_framebuffer_create(struct drm_device *dev,
14609 struct drm_file *filp,
308e5bcb 14610 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14611{
05394f39 14612 struct drm_i915_gem_object *obj;
79e53945 14613
308e5bcb
JB
14614 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14615 mode_cmd->handles[0]));
c8725226 14616 if (&obj->base == NULL)
cce13ff7 14617 return ERR_PTR(-ENOENT);
79e53945 14618
d2dff872 14619 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14620}
14621
4520f53a 14622#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14623static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14624{
14625}
14626#endif
14627
79e53945 14628static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14629 .fb_create = intel_user_framebuffer_create,
0632fef6 14630 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14631 .atomic_check = intel_atomic_check,
14632 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14633 .atomic_state_alloc = intel_atomic_state_alloc,
14634 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14635};
14636
e70236a8
JB
14637/* Set up chip specific display functions */
14638static void intel_init_display(struct drm_device *dev)
14639{
14640 struct drm_i915_private *dev_priv = dev->dev_private;
14641
ee9300bb
DV
14642 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14643 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14644 else if (IS_CHERRYVIEW(dev))
14645 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14646 else if (IS_VALLEYVIEW(dev))
14647 dev_priv->display.find_dpll = vlv_find_best_dpll;
14648 else if (IS_PINEVIEW(dev))
14649 dev_priv->display.find_dpll = pnv_find_best_dpll;
14650 else
14651 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14652
bc8d7dff
DL
14653 if (INTEL_INFO(dev)->gen >= 9) {
14654 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14655 dev_priv->display.get_initial_plane_config =
14656 skylake_get_initial_plane_config;
bc8d7dff
DL
14657 dev_priv->display.crtc_compute_clock =
14658 haswell_crtc_compute_clock;
14659 dev_priv->display.crtc_enable = haswell_crtc_enable;
14660 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14661 dev_priv->display.update_primary_plane =
14662 skylake_update_primary_plane;
14663 } else if (HAS_DDI(dev)) {
0e8ffe1b 14664 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14665 dev_priv->display.get_initial_plane_config =
14666 ironlake_get_initial_plane_config;
797d0259
ACO
14667 dev_priv->display.crtc_compute_clock =
14668 haswell_crtc_compute_clock;
4f771f10
PZ
14669 dev_priv->display.crtc_enable = haswell_crtc_enable;
14670 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14671 dev_priv->display.update_primary_plane =
14672 ironlake_update_primary_plane;
09b4ddf9 14673 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14674 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14675 dev_priv->display.get_initial_plane_config =
14676 ironlake_get_initial_plane_config;
3fb37703
ACO
14677 dev_priv->display.crtc_compute_clock =
14678 ironlake_crtc_compute_clock;
76e5a89c
DV
14679 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14680 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14681 dev_priv->display.update_primary_plane =
14682 ironlake_update_primary_plane;
89b667f8
JB
14683 } else if (IS_VALLEYVIEW(dev)) {
14684 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14685 dev_priv->display.get_initial_plane_config =
14686 i9xx_get_initial_plane_config;
d6dfee7a 14687 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14688 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14689 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14690 dev_priv->display.update_primary_plane =
14691 i9xx_update_primary_plane;
f564048e 14692 } else {
0e8ffe1b 14693 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14694 dev_priv->display.get_initial_plane_config =
14695 i9xx_get_initial_plane_config;
d6dfee7a 14696 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14697 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14698 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14699 dev_priv->display.update_primary_plane =
14700 i9xx_update_primary_plane;
f564048e 14701 }
e70236a8 14702
e70236a8 14703 /* Returns the core display clock speed */
1652d19e
VS
14704 if (IS_SKYLAKE(dev))
14705 dev_priv->display.get_display_clock_speed =
14706 skylake_get_display_clock_speed;
14707 else if (IS_BROADWELL(dev))
14708 dev_priv->display.get_display_clock_speed =
14709 broadwell_get_display_clock_speed;
14710 else if (IS_HASWELL(dev))
14711 dev_priv->display.get_display_clock_speed =
14712 haswell_get_display_clock_speed;
14713 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14714 dev_priv->display.get_display_clock_speed =
14715 valleyview_get_display_clock_speed;
b37a6434
VS
14716 else if (IS_GEN5(dev))
14717 dev_priv->display.get_display_clock_speed =
14718 ilk_get_display_clock_speed;
a7c66cd8 14719 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14720 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14721 dev_priv->display.get_display_clock_speed =
14722 i945_get_display_clock_speed;
34edce2f
VS
14723 else if (IS_GM45(dev))
14724 dev_priv->display.get_display_clock_speed =
14725 gm45_get_display_clock_speed;
14726 else if (IS_CRESTLINE(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 i965gm_get_display_clock_speed;
14729 else if (IS_PINEVIEW(dev))
14730 dev_priv->display.get_display_clock_speed =
14731 pnv_get_display_clock_speed;
14732 else if (IS_G33(dev) || IS_G4X(dev))
14733 dev_priv->display.get_display_clock_speed =
14734 g33_get_display_clock_speed;
e70236a8
JB
14735 else if (IS_I915G(dev))
14736 dev_priv->display.get_display_clock_speed =
14737 i915_get_display_clock_speed;
257a7ffc 14738 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14739 dev_priv->display.get_display_clock_speed =
14740 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14741 else if (IS_PINEVIEW(dev))
14742 dev_priv->display.get_display_clock_speed =
14743 pnv_get_display_clock_speed;
e70236a8
JB
14744 else if (IS_I915GM(dev))
14745 dev_priv->display.get_display_clock_speed =
14746 i915gm_get_display_clock_speed;
14747 else if (IS_I865G(dev))
14748 dev_priv->display.get_display_clock_speed =
14749 i865_get_display_clock_speed;
f0f8a9ce 14750 else if (IS_I85X(dev))
e70236a8 14751 dev_priv->display.get_display_clock_speed =
1b1d2716 14752 i85x_get_display_clock_speed;
623e01e5
VS
14753 else { /* 830 */
14754 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14755 dev_priv->display.get_display_clock_speed =
14756 i830_get_display_clock_speed;
623e01e5 14757 }
e70236a8 14758
7c10a2b5 14759 if (IS_GEN5(dev)) {
3bb11b53 14760 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14761 } else if (IS_GEN6(dev)) {
14762 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14763 } else if (IS_IVYBRIDGE(dev)) {
14764 /* FIXME: detect B0+ stepping and use auto training */
14765 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14766 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14767 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
b432e5cf
VS
14768 if (IS_BROADWELL(dev))
14769 dev_priv->display.modeset_global_resources =
14770 broadwell_modeset_global_resources;
30a970c6
JB
14771 } else if (IS_VALLEYVIEW(dev)) {
14772 dev_priv->display.modeset_global_resources =
14773 valleyview_modeset_global_resources;
f8437dd1
VK
14774 } else if (IS_BROXTON(dev)) {
14775 dev_priv->display.modeset_global_resources =
14776 broxton_modeset_global_resources;
e70236a8 14777 }
8c9f3aaf 14778
8c9f3aaf
JB
14779 switch (INTEL_INFO(dev)->gen) {
14780 case 2:
14781 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14782 break;
14783
14784 case 3:
14785 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14786 break;
14787
14788 case 4:
14789 case 5:
14790 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14791 break;
14792
14793 case 6:
14794 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14795 break;
7c9017e5 14796 case 7:
4e0bbc31 14797 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14798 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14799 break;
830c81db 14800 case 9:
ba343e02
TU
14801 /* Drop through - unsupported since execlist only. */
14802 default:
14803 /* Default just returns -ENODEV to indicate unsupported */
14804 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14805 }
7bd688cd
JN
14806
14807 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14808
14809 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14810}
14811
b690e96c
JB
14812/*
14813 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14814 * resume, or other times. This quirk makes sure that's the case for
14815 * affected systems.
14816 */
0206e353 14817static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14818{
14819 struct drm_i915_private *dev_priv = dev->dev_private;
14820
14821 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14822 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14823}
14824
b6b5d049
VS
14825static void quirk_pipeb_force(struct drm_device *dev)
14826{
14827 struct drm_i915_private *dev_priv = dev->dev_private;
14828
14829 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14830 DRM_INFO("applying pipe b force quirk\n");
14831}
14832
435793df
KP
14833/*
14834 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14835 */
14836static void quirk_ssc_force_disable(struct drm_device *dev)
14837{
14838 struct drm_i915_private *dev_priv = dev->dev_private;
14839 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14840 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14841}
14842
4dca20ef 14843/*
5a15ab5b
CE
14844 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14845 * brightness value
4dca20ef
CE
14846 */
14847static void quirk_invert_brightness(struct drm_device *dev)
14848{
14849 struct drm_i915_private *dev_priv = dev->dev_private;
14850 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14851 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14852}
14853
9c72cc6f
SD
14854/* Some VBT's incorrectly indicate no backlight is present */
14855static void quirk_backlight_present(struct drm_device *dev)
14856{
14857 struct drm_i915_private *dev_priv = dev->dev_private;
14858 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14859 DRM_INFO("applying backlight present quirk\n");
14860}
14861
b690e96c
JB
14862struct intel_quirk {
14863 int device;
14864 int subsystem_vendor;
14865 int subsystem_device;
14866 void (*hook)(struct drm_device *dev);
14867};
14868
5f85f176
EE
14869/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14870struct intel_dmi_quirk {
14871 void (*hook)(struct drm_device *dev);
14872 const struct dmi_system_id (*dmi_id_list)[];
14873};
14874
14875static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14876{
14877 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14878 return 1;
14879}
14880
14881static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14882 {
14883 .dmi_id_list = &(const struct dmi_system_id[]) {
14884 {
14885 .callback = intel_dmi_reverse_brightness,
14886 .ident = "NCR Corporation",
14887 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14888 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14889 },
14890 },
14891 { } /* terminating entry */
14892 },
14893 .hook = quirk_invert_brightness,
14894 },
14895};
14896
c43b5634 14897static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14898 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14899 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14900
b690e96c
JB
14901 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14902 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14903
5f080c0f
VS
14904 /* 830 needs to leave pipe A & dpll A up */
14905 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14906
b6b5d049
VS
14907 /* 830 needs to leave pipe B & dpll B up */
14908 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14909
435793df
KP
14910 /* Lenovo U160 cannot use SSC on LVDS */
14911 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14912
14913 /* Sony Vaio Y cannot use SSC on LVDS */
14914 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14915
be505f64
AH
14916 /* Acer Aspire 5734Z must invert backlight brightness */
14917 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14918
14919 /* Acer/eMachines G725 */
14920 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14921
14922 /* Acer/eMachines e725 */
14923 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14924
14925 /* Acer/Packard Bell NCL20 */
14926 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14927
14928 /* Acer Aspire 4736Z */
14929 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14930
14931 /* Acer Aspire 5336 */
14932 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14933
14934 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14935 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14936
dfb3d47b
SD
14937 /* Acer C720 Chromebook (Core i3 4005U) */
14938 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14939
b2a9601c 14940 /* Apple Macbook 2,1 (Core 2 T7400) */
14941 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14942
d4967d8c
SD
14943 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14944 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14945
14946 /* HP Chromebook 14 (Celeron 2955U) */
14947 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14948
14949 /* Dell Chromebook 11 */
14950 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14951};
14952
14953static void intel_init_quirks(struct drm_device *dev)
14954{
14955 struct pci_dev *d = dev->pdev;
14956 int i;
14957
14958 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14959 struct intel_quirk *q = &intel_quirks[i];
14960
14961 if (d->device == q->device &&
14962 (d->subsystem_vendor == q->subsystem_vendor ||
14963 q->subsystem_vendor == PCI_ANY_ID) &&
14964 (d->subsystem_device == q->subsystem_device ||
14965 q->subsystem_device == PCI_ANY_ID))
14966 q->hook(dev);
14967 }
5f85f176
EE
14968 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14969 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14970 intel_dmi_quirks[i].hook(dev);
14971 }
b690e96c
JB
14972}
14973
9cce37f4
JB
14974/* Disable the VGA plane that we never use */
14975static void i915_disable_vga(struct drm_device *dev)
14976{
14977 struct drm_i915_private *dev_priv = dev->dev_private;
14978 u8 sr1;
766aa1c4 14979 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14980
2b37c616 14981 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14982 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14983 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14984 sr1 = inb(VGA_SR_DATA);
14985 outb(sr1 | 1<<5, VGA_SR_DATA);
14986 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14987 udelay(300);
14988
01f5a626 14989 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14990 POSTING_READ(vga_reg);
14991}
14992
f817586c
DV
14993void intel_modeset_init_hw(struct drm_device *dev)
14994{
b6283055 14995 intel_update_cdclk(dev);
a8f78b58 14996 intel_prepare_ddi(dev);
f817586c 14997 intel_init_clock_gating(dev);
8090c6b9 14998 intel_enable_gt_powersave(dev);
f817586c
DV
14999}
15000
79e53945
JB
15001void intel_modeset_init(struct drm_device *dev)
15002{
652c393a 15003 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15004 int sprite, ret;
8cc87b75 15005 enum pipe pipe;
46f297fb 15006 struct intel_crtc *crtc;
79e53945
JB
15007
15008 drm_mode_config_init(dev);
15009
15010 dev->mode_config.min_width = 0;
15011 dev->mode_config.min_height = 0;
15012
019d96cb
DA
15013 dev->mode_config.preferred_depth = 24;
15014 dev->mode_config.prefer_shadow = 1;
15015
25bab385
TU
15016 dev->mode_config.allow_fb_modifiers = true;
15017
e6ecefaa 15018 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15019
b690e96c
JB
15020 intel_init_quirks(dev);
15021
1fa61106
ED
15022 intel_init_pm(dev);
15023
e3c74757
BW
15024 if (INTEL_INFO(dev)->num_pipes == 0)
15025 return;
15026
e70236a8 15027 intel_init_display(dev);
7c10a2b5 15028 intel_init_audio(dev);
e70236a8 15029
a6c45cf0
CW
15030 if (IS_GEN2(dev)) {
15031 dev->mode_config.max_width = 2048;
15032 dev->mode_config.max_height = 2048;
15033 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15034 dev->mode_config.max_width = 4096;
15035 dev->mode_config.max_height = 4096;
79e53945 15036 } else {
a6c45cf0
CW
15037 dev->mode_config.max_width = 8192;
15038 dev->mode_config.max_height = 8192;
79e53945 15039 }
068be561 15040
dc41c154
VS
15041 if (IS_845G(dev) || IS_I865G(dev)) {
15042 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15043 dev->mode_config.cursor_height = 1023;
15044 } else if (IS_GEN2(dev)) {
068be561
DL
15045 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15046 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15047 } else {
15048 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15049 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15050 }
15051
5d4545ae 15052 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15053
28c97730 15054 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15055 INTEL_INFO(dev)->num_pipes,
15056 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15057
055e393f 15058 for_each_pipe(dev_priv, pipe) {
8cc87b75 15059 intel_crtc_init(dev, pipe);
3bdcfc0c 15060 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15061 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15062 if (ret)
06da8da2 15063 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15064 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15065 }
79e53945
JB
15066 }
15067
f42bb70d
JB
15068 intel_init_dpio(dev);
15069
e72f9fbf 15070 intel_shared_dpll_init(dev);
ee7b9f93 15071
9cce37f4
JB
15072 /* Just disable it once at startup */
15073 i915_disable_vga(dev);
79e53945 15074 intel_setup_outputs(dev);
11be49eb
CW
15075
15076 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 15077 intel_fbc_disable(dev);
fa9fa083 15078
6e9f798d 15079 drm_modeset_lock_all(dev);
fa9fa083 15080 intel_modeset_setup_hw_state(dev, false);
6e9f798d 15081 drm_modeset_unlock_all(dev);
46f297fb 15082
d3fcc808 15083 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
15084 if (!crtc->active)
15085 continue;
15086
46f297fb 15087 /*
46f297fb
JB
15088 * Note that reserving the BIOS fb up front prevents us
15089 * from stuffing other stolen allocations like the ring
15090 * on top. This prevents some ugliness at boot time, and
15091 * can even allow for smooth boot transitions if the BIOS
15092 * fb is large enough for the active pipe configuration.
15093 */
5724dbd1
DL
15094 if (dev_priv->display.get_initial_plane_config) {
15095 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
15096 &crtc->plane_config);
15097 /*
15098 * If the fb is shared between multiple heads, we'll
15099 * just get the first one.
15100 */
f6936e29 15101 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15102 }
46f297fb 15103 }
2c7111db
CW
15104}
15105
7fad798e
DV
15106static void intel_enable_pipe_a(struct drm_device *dev)
15107{
15108 struct intel_connector *connector;
15109 struct drm_connector *crt = NULL;
15110 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15111 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15112
15113 /* We can't just switch on the pipe A, we need to set things up with a
15114 * proper mode and output configuration. As a gross hack, enable pipe A
15115 * by enabling the load detect pipe once. */
3a3371ff 15116 for_each_intel_connector(dev, connector) {
7fad798e
DV
15117 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15118 crt = &connector->base;
15119 break;
15120 }
15121 }
15122
15123 if (!crt)
15124 return;
15125
208bf9fd 15126 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15127 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15128}
15129
fa555837
DV
15130static bool
15131intel_check_plane_mapping(struct intel_crtc *crtc)
15132{
7eb552ae
BW
15133 struct drm_device *dev = crtc->base.dev;
15134 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15135 u32 reg, val;
15136
7eb552ae 15137 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15138 return true;
15139
15140 reg = DSPCNTR(!crtc->plane);
15141 val = I915_READ(reg);
15142
15143 if ((val & DISPLAY_PLANE_ENABLE) &&
15144 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15145 return false;
15146
15147 return true;
15148}
15149
24929352
DV
15150static void intel_sanitize_crtc(struct intel_crtc *crtc)
15151{
15152 struct drm_device *dev = crtc->base.dev;
15153 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15154 struct intel_encoder *encoder;
fa555837 15155 u32 reg;
b17d48e2 15156 bool enable;
24929352 15157
24929352 15158 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15159 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15160 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15161
d3eaf884 15162 /* restore vblank interrupts to correct state */
9625604c 15163 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15164 if (crtc->active) {
15165 update_scanline_offset(crtc);
9625604c
DV
15166 drm_crtc_vblank_on(&crtc->base);
15167 }
d3eaf884 15168
24929352 15169 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15170 * disable the crtc (and hence change the state) if it is wrong. Note
15171 * that gen4+ has a fixed plane -> pipe mapping. */
15172 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15173 bool plane;
15174
24929352
DV
15175 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15176 crtc->base.base.id);
15177
15178 /* Pipe has the wrong plane attached and the plane is active.
15179 * Temporarily change the plane mapping and disable everything
15180 * ... */
15181 plane = crtc->plane;
b70709a6 15182 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15183 crtc->plane = !plane;
b17d48e2 15184 intel_crtc_disable_noatomic(&crtc->base);
24929352 15185 crtc->plane = plane;
24929352 15186 }
24929352 15187
7fad798e
DV
15188 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15189 crtc->pipe == PIPE_A && !crtc->active) {
15190 /* BIOS forgot to enable pipe A, this mostly happens after
15191 * resume. Force-enable the pipe to fix this, the update_dpms
15192 * call below we restore the pipe to the right state, but leave
15193 * the required bits on. */
15194 intel_enable_pipe_a(dev);
15195 }
15196
24929352
DV
15197 /* Adjust the state of the output pipe according to whether we
15198 * have active connectors/encoders. */
b17d48e2
ML
15199 enable = false;
15200 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15201 enable |= encoder->connectors_active;
15202
15203 if (!enable)
15204 intel_crtc_disable_noatomic(&crtc->base);
24929352 15205
53d9f4e9 15206 if (crtc->active != crtc->base.state->active) {
24929352
DV
15207
15208 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15209 * functions or because of calls to intel_crtc_disable_noatomic,
15210 * or because the pipe is force-enabled due to the
24929352
DV
15211 * pipe A quirk. */
15212 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15213 crtc->base.base.id,
83d65738 15214 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15215 crtc->active ? "enabled" : "disabled");
15216
83d65738 15217 crtc->base.state->enable = crtc->active;
49d6fa21 15218 crtc->base.state->active = crtc->active;
24929352
DV
15219 crtc->base.enabled = crtc->active;
15220
15221 /* Because we only establish the connector -> encoder ->
15222 * crtc links if something is active, this means the
15223 * crtc is now deactivated. Break the links. connector
15224 * -> encoder links are only establish when things are
15225 * actually up, hence no need to break them. */
15226 WARN_ON(crtc->active);
15227
15228 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15229 WARN_ON(encoder->connectors_active);
15230 encoder->base.crtc = NULL;
15231 }
15232 }
c5ab3bc0 15233
a3ed6aad 15234 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15235 /*
15236 * We start out with underrun reporting disabled to avoid races.
15237 * For correct bookkeeping mark this on active crtcs.
15238 *
c5ab3bc0
DV
15239 * Also on gmch platforms we dont have any hardware bits to
15240 * disable the underrun reporting. Which means we need to start
15241 * out with underrun reporting disabled also on inactive pipes,
15242 * since otherwise we'll complain about the garbage we read when
15243 * e.g. coming up after runtime pm.
15244 *
4cc31489
DV
15245 * No protection against concurrent access is required - at
15246 * worst a fifo underrun happens which also sets this to false.
15247 */
15248 crtc->cpu_fifo_underrun_disabled = true;
15249 crtc->pch_fifo_underrun_disabled = true;
15250 }
24929352
DV
15251}
15252
15253static void intel_sanitize_encoder(struct intel_encoder *encoder)
15254{
15255 struct intel_connector *connector;
15256 struct drm_device *dev = encoder->base.dev;
15257
15258 /* We need to check both for a crtc link (meaning that the
15259 * encoder is active and trying to read from a pipe) and the
15260 * pipe itself being active. */
15261 bool has_active_crtc = encoder->base.crtc &&
15262 to_intel_crtc(encoder->base.crtc)->active;
15263
15264 if (encoder->connectors_active && !has_active_crtc) {
15265 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15266 encoder->base.base.id,
8e329a03 15267 encoder->base.name);
24929352
DV
15268
15269 /* Connector is active, but has no active pipe. This is
15270 * fallout from our resume register restoring. Disable
15271 * the encoder manually again. */
15272 if (encoder->base.crtc) {
15273 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15274 encoder->base.base.id,
8e329a03 15275 encoder->base.name);
24929352 15276 encoder->disable(encoder);
a62d1497
VS
15277 if (encoder->post_disable)
15278 encoder->post_disable(encoder);
24929352 15279 }
7f1950fb
EE
15280 encoder->base.crtc = NULL;
15281 encoder->connectors_active = false;
24929352
DV
15282
15283 /* Inconsistent output/port/pipe state happens presumably due to
15284 * a bug in one of the get_hw_state functions. Or someplace else
15285 * in our code, like the register restore mess on resume. Clamp
15286 * things to off as a safer default. */
3a3371ff 15287 for_each_intel_connector(dev, connector) {
24929352
DV
15288 if (connector->encoder != encoder)
15289 continue;
7f1950fb
EE
15290 connector->base.dpms = DRM_MODE_DPMS_OFF;
15291 connector->base.encoder = NULL;
24929352
DV
15292 }
15293 }
15294 /* Enabled encoders without active connectors will be fixed in
15295 * the crtc fixup. */
15296}
15297
04098753 15298void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15299{
15300 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15301 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15302
04098753
ID
15303 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15304 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15305 i915_disable_vga(dev);
15306 }
15307}
15308
15309void i915_redisable_vga(struct drm_device *dev)
15310{
15311 struct drm_i915_private *dev_priv = dev->dev_private;
15312
8dc8a27c
PZ
15313 /* This function can be called both from intel_modeset_setup_hw_state or
15314 * at a very early point in our resume sequence, where the power well
15315 * structures are not yet restored. Since this function is at a very
15316 * paranoid "someone might have enabled VGA while we were not looking"
15317 * level, just check if the power well is enabled instead of trying to
15318 * follow the "don't touch the power well if we don't need it" policy
15319 * the rest of the driver uses. */
f458ebbc 15320 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15321 return;
15322
04098753 15323 i915_redisable_vga_power_on(dev);
0fde901f
KM
15324}
15325
98ec7739
VS
15326static bool primary_get_hw_state(struct intel_crtc *crtc)
15327{
15328 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15329
f7217905 15330 if (!crtc->active)
98ec7739
VS
15331 return false;
15332
15333 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15334}
15335
f7217905 15336static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15337{
f7217905
ML
15338 struct drm_i915_private *dev_priv = dev->dev_private;
15339 enum pipe pipe;
15340 struct intel_crtc *crtc;
15341 struct intel_encoder *encoder;
15342 struct intel_connector *connector;
15343 int i;
b70709a6 15344
f7217905
ML
15345 for_each_intel_crtc(dev, crtc) {
15346 struct drm_plane *primary = crtc->base.primary;
15347 struct intel_plane_state *plane_state;
24929352 15348
f7217905
ML
15349 memset(crtc->config, 0, sizeof(*crtc->config));
15350 crtc->config->base.crtc = &crtc->base;
37ade417 15351
f7217905 15352 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
37ade417 15353
f7217905
ML
15354 crtc->active = dev_priv->display.get_pipe_config(crtc,
15355 crtc->config);
37ade417 15356
f7217905
ML
15357 crtc->base.state->enable = crtc->active;
15358 crtc->base.state->active = crtc->active;
15359 crtc->base.enabled = crtc->active;
b8b7fade 15360 crtc->base.hwmode = crtc->config->base.adjusted_mode;
37ade417 15361
f7217905
ML
15362 plane_state = to_intel_plane_state(primary->state);
15363 plane_state->visible = primary_get_hw_state(crtc);
24929352 15364
f7217905
ML
15365 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15366 crtc->base.base.id,
15367 crtc->active ? "enabled" : "disabled");
15368 }
37ade417 15369
5358901f
DV
15370 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15371 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15372
3e369b76 15373 pll->on = pll->get_hw_state(dev_priv, pll,
f7217905 15374 &pll->config.hw_state);
5358901f 15375 pll->active = 0;
f7217905
ML
15376 pll->config.crtc_mask = 0;
15377 for_each_intel_crtc(dev, crtc) {
15378 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15379 pll->active++;
f7217905 15380 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15381 }
5358901f 15382 }
5358901f 15383
1e6f2ddc 15384 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
f7217905 15385 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15386
f7217905 15387 if (pll->config.crtc_mask)
bd2bb1b9 15388 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15389 }
15390
37ade417
ACO
15391 for_each_intel_encoder(dev, encoder) {
15392 pipe = 0;
15393
37ade417 15394 if (encoder->get_hw_state(encoder, &pipe)) {
f7217905
ML
15395 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15396 encoder->base.crtc = &crtc->base;
15397 encoder->get_config(encoder, crtc->config);
37ade417
ACO
15398 } else {
15399 encoder->base.crtc = NULL;
15400 }
15401
f7217905 15402 encoder->connectors_active = false;
37ade417
ACO
15403 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
15404 encoder->base.base.id,
15405 encoder->base.name,
15406 encoder->base.crtc ? "enabled" : "disabled",
15407 pipe_name(pipe));
15408 }
15409
f7217905
ML
15410 for_each_intel_connector(dev, connector) {
15411 if (connector->get_hw_state(connector)) {
15412 connector->base.dpms = DRM_MODE_DPMS_ON;
15413 connector->encoder->connectors_active = true;
15414 connector->base.encoder = &connector->encoder->base;
15415 } else {
15416 connector->base.dpms = DRM_MODE_DPMS_OFF;
15417 connector->base.encoder = NULL;
15418 }
15419 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15420 connector->base.base.id,
15421 connector->base.name,
15422 connector->base.encoder ? "enabled" : "disabled");
37ade417 15423 }
30e984df
DV
15424}
15425
15426/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15427 * and i915 state tracking structures. */
15428void intel_modeset_setup_hw_state(struct drm_device *dev,
15429 bool force_restore)
15430{
15431 struct drm_i915_private *dev_priv = dev->dev_private;
f7217905
ML
15432 enum pipe pipe;
15433 struct intel_crtc *crtc;
30e984df 15434 struct intel_encoder *encoder;
35c95375 15435 int i;
30e984df 15436
f7217905 15437 intel_modeset_readout_hw_state(dev);
37ade417 15438
f7217905
ML
15439 /*
15440 * Now that we have the config, copy it to each CRTC struct
15441 * Note that this could go away if we move to using crtc_config
15442 * checking everywhere.
15443 */
15444 for_each_intel_crtc(dev, crtc) {
15445 if (crtc->active && i915.fastboot) {
15446 intel_mode_from_pipe_config(&crtc->base.mode,
15447 crtc->config);
15448 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15449 crtc->base.base.id);
15450 drm_mode_debug_printmodeline(&crtc->base.mode);
15451 }
15452 }
37ade417 15453
24929352 15454 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15455 for_each_intel_encoder(dev, encoder) {
24929352
DV
15456 intel_sanitize_encoder(encoder);
15457 }
15458
f7217905
ML
15459 for_each_pipe(dev_priv, pipe) {
15460 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15461 intel_sanitize_crtc(crtc);
15462 intel_dump_pipe_config(crtc, crtc->config,
6e3c9717 15463 "[setup_hw_state]");
24929352 15464 }
9a935856 15465
d29b2f9d
ACO
15466 intel_modeset_update_connector_atomic_state(dev);
15467
35c95375
DV
15468 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15469 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15470
15471 if (!pll->on || pll->active)
15472 continue;
15473
15474 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15475
15476 pll->disable(dev_priv, pll);
15477 pll->on = false;
15478 }
15479
3078999f
PB
15480 if (IS_GEN9(dev))
15481 skl_wm_get_hw_state(dev);
15482 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15483 ilk_wm_get_hw_state(dev);
15484
45e2b5f6 15485 if (force_restore) {
37ade417 15486 i915_redisable_vga(dev);
f30da187 15487
f7217905
ML
15488 /*
15489 * We need to use raw interfaces for restoring state to avoid
15490 * checking (bogus) intermediate states.
15491 */
15492 for_each_pipe(dev_priv, pipe) {
15493 struct drm_crtc *crtc =
15494 dev_priv->pipe_to_crtc_mapping[pipe];
15495
15496 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15497 }
15498 } else {
f7217905 15499 intel_modeset_update_staged_output_state(dev);
45e2b5f6 15500 }
8af6cf88
DV
15501
15502 intel_modeset_check_state(dev);
2c7111db
CW
15503}
15504
15505void intel_modeset_gem_init(struct drm_device *dev)
15506{
92122789 15507 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15508 struct drm_crtc *c;
2ff8fde1 15509 struct drm_i915_gem_object *obj;
e0d6149b 15510 int ret;
484b41dd 15511
ae48434c
ID
15512 mutex_lock(&dev->struct_mutex);
15513 intel_init_gt_powersave(dev);
15514 mutex_unlock(&dev->struct_mutex);
15515
92122789
JB
15516 /*
15517 * There may be no VBT; and if the BIOS enabled SSC we can
15518 * just keep using it to avoid unnecessary flicker. Whereas if the
15519 * BIOS isn't using it, don't assume it will work even if the VBT
15520 * indicates as much.
15521 */
15522 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15523 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15524 DREF_SSC1_ENABLE);
15525
1833b134 15526 intel_modeset_init_hw(dev);
02e792fb
DV
15527
15528 intel_setup_overlay(dev);
484b41dd
JB
15529
15530 /*
15531 * Make sure any fbs we allocated at startup are properly
15532 * pinned & fenced. When we do the allocation it's too early
15533 * for this.
15534 */
70e1e0ec 15535 for_each_crtc(dev, c) {
2ff8fde1
MR
15536 obj = intel_fb_obj(c->primary->fb);
15537 if (obj == NULL)
484b41dd
JB
15538 continue;
15539
e0d6149b
TU
15540 mutex_lock(&dev->struct_mutex);
15541 ret = intel_pin_and_fence_fb_obj(c->primary,
15542 c->primary->fb,
15543 c->primary->state,
15544 NULL);
15545 mutex_unlock(&dev->struct_mutex);
15546 if (ret) {
484b41dd
JB
15547 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15548 to_intel_crtc(c)->pipe);
66e514c1
DA
15549 drm_framebuffer_unreference(c->primary->fb);
15550 c->primary->fb = NULL;
36750f28 15551 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15552 update_state_fb(c->primary);
36750f28 15553 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15554 }
15555 }
0962c3c9
VS
15556
15557 intel_backlight_register(dev);
79e53945
JB
15558}
15559
4932e2c3
ID
15560void intel_connector_unregister(struct intel_connector *intel_connector)
15561{
15562 struct drm_connector *connector = &intel_connector->base;
15563
15564 intel_panel_destroy_backlight(connector);
34ea3d38 15565 drm_connector_unregister(connector);
4932e2c3
ID
15566}
15567
79e53945
JB
15568void intel_modeset_cleanup(struct drm_device *dev)
15569{
652c393a 15570 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15571 struct drm_connector *connector;
652c393a 15572
2eb5252e
ID
15573 intel_disable_gt_powersave(dev);
15574
0962c3c9
VS
15575 intel_backlight_unregister(dev);
15576
fd0c0642
DV
15577 /*
15578 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15579 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15580 * experience fancy races otherwise.
15581 */
2aeb7d3a 15582 intel_irq_uninstall(dev_priv);
eb21b92b 15583
fd0c0642
DV
15584 /*
15585 * Due to the hpd irq storm handling the hotplug work can re-arm the
15586 * poll handlers. Hence disable polling after hpd handling is shut down.
15587 */
f87ea761 15588 drm_kms_helper_poll_fini(dev);
fd0c0642 15589
652c393a
JB
15590 mutex_lock(&dev->struct_mutex);
15591
723bfd70
JB
15592 intel_unregister_dsm_handler();
15593
7ff0ebcc 15594 intel_fbc_disable(dev);
e70236a8 15595
69341a5e
KH
15596 mutex_unlock(&dev->struct_mutex);
15597
1630fe75
CW
15598 /* flush any delayed tasks or pending work */
15599 flush_scheduled_work();
15600
db31af1d
JN
15601 /* destroy the backlight and sysfs files before encoders/connectors */
15602 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15603 struct intel_connector *intel_connector;
15604
15605 intel_connector = to_intel_connector(connector);
15606 intel_connector->unregister(intel_connector);
db31af1d 15607 }
d9255d57 15608
79e53945 15609 drm_mode_config_cleanup(dev);
4d7bb011
DV
15610
15611 intel_cleanup_overlay(dev);
ae48434c
ID
15612
15613 mutex_lock(&dev->struct_mutex);
15614 intel_cleanup_gt_powersave(dev);
15615 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15616}
15617
f1c79df3
ZW
15618/*
15619 * Return which encoder is currently attached for connector.
15620 */
df0e9248 15621struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15622{
df0e9248
CW
15623 return &intel_attached_encoder(connector)->base;
15624}
f1c79df3 15625
df0e9248
CW
15626void intel_connector_attach_encoder(struct intel_connector *connector,
15627 struct intel_encoder *encoder)
15628{
15629 connector->encoder = encoder;
15630 drm_mode_connector_attach_encoder(&connector->base,
15631 &encoder->base);
79e53945 15632}
28d52043
DA
15633
15634/*
15635 * set vga decode state - true == enable VGA decode
15636 */
15637int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15638{
15639 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15640 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15641 u16 gmch_ctrl;
15642
75fa041d
CW
15643 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15644 DRM_ERROR("failed to read control word\n");
15645 return -EIO;
15646 }
15647
c0cc8a55
CW
15648 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15649 return 0;
15650
28d52043
DA
15651 if (state)
15652 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15653 else
15654 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15655
15656 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15657 DRM_ERROR("failed to write control word\n");
15658 return -EIO;
15659 }
15660
28d52043
DA
15661 return 0;
15662}
c4a1d9e4 15663
c4a1d9e4 15664struct intel_display_error_state {
ff57f1b0
PZ
15665
15666 u32 power_well_driver;
15667
63b66e5b
CW
15668 int num_transcoders;
15669
c4a1d9e4
CW
15670 struct intel_cursor_error_state {
15671 u32 control;
15672 u32 position;
15673 u32 base;
15674 u32 size;
52331309 15675 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15676
15677 struct intel_pipe_error_state {
ddf9c536 15678 bool power_domain_on;
c4a1d9e4 15679 u32 source;
f301b1e1 15680 u32 stat;
52331309 15681 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15682
15683 struct intel_plane_error_state {
15684 u32 control;
15685 u32 stride;
15686 u32 size;
15687 u32 pos;
15688 u32 addr;
15689 u32 surface;
15690 u32 tile_offset;
52331309 15691 } plane[I915_MAX_PIPES];
63b66e5b
CW
15692
15693 struct intel_transcoder_error_state {
ddf9c536 15694 bool power_domain_on;
63b66e5b
CW
15695 enum transcoder cpu_transcoder;
15696
15697 u32 conf;
15698
15699 u32 htotal;
15700 u32 hblank;
15701 u32 hsync;
15702 u32 vtotal;
15703 u32 vblank;
15704 u32 vsync;
15705 } transcoder[4];
c4a1d9e4
CW
15706};
15707
15708struct intel_display_error_state *
15709intel_display_capture_error_state(struct drm_device *dev)
15710{
fbee40df 15711 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15712 struct intel_display_error_state *error;
63b66e5b
CW
15713 int transcoders[] = {
15714 TRANSCODER_A,
15715 TRANSCODER_B,
15716 TRANSCODER_C,
15717 TRANSCODER_EDP,
15718 };
c4a1d9e4
CW
15719 int i;
15720
63b66e5b
CW
15721 if (INTEL_INFO(dev)->num_pipes == 0)
15722 return NULL;
15723
9d1cb914 15724 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15725 if (error == NULL)
15726 return NULL;
15727
190be112 15728 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15729 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15730
055e393f 15731 for_each_pipe(dev_priv, i) {
ddf9c536 15732 error->pipe[i].power_domain_on =
f458ebbc
DV
15733 __intel_display_power_is_enabled(dev_priv,
15734 POWER_DOMAIN_PIPE(i));
ddf9c536 15735 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15736 continue;
15737
5efb3e28
VS
15738 error->cursor[i].control = I915_READ(CURCNTR(i));
15739 error->cursor[i].position = I915_READ(CURPOS(i));
15740 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15741
15742 error->plane[i].control = I915_READ(DSPCNTR(i));
15743 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15744 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15745 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15746 error->plane[i].pos = I915_READ(DSPPOS(i));
15747 }
ca291363
PZ
15748 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15749 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15750 if (INTEL_INFO(dev)->gen >= 4) {
15751 error->plane[i].surface = I915_READ(DSPSURF(i));
15752 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15753 }
15754
c4a1d9e4 15755 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15756
3abfce77 15757 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15758 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15759 }
15760
15761 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15762 if (HAS_DDI(dev_priv->dev))
15763 error->num_transcoders++; /* Account for eDP. */
15764
15765 for (i = 0; i < error->num_transcoders; i++) {
15766 enum transcoder cpu_transcoder = transcoders[i];
15767
ddf9c536 15768 error->transcoder[i].power_domain_on =
f458ebbc 15769 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15770 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15771 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15772 continue;
15773
63b66e5b
CW
15774 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15775
15776 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15777 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15778 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15779 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15780 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15781 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15782 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15783 }
15784
15785 return error;
15786}
15787
edc3d884
MK
15788#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15789
c4a1d9e4 15790void
edc3d884 15791intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15792 struct drm_device *dev,
15793 struct intel_display_error_state *error)
15794{
055e393f 15795 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15796 int i;
15797
63b66e5b
CW
15798 if (!error)
15799 return;
15800
edc3d884 15801 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15802 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15803 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15804 error->power_well_driver);
055e393f 15805 for_each_pipe(dev_priv, i) {
edc3d884 15806 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15807 err_printf(m, " Power: %s\n",
15808 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15809 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15810 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15811
15812 err_printf(m, "Plane [%d]:\n", i);
15813 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15814 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15815 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15816 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15817 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15818 }
4b71a570 15819 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15820 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15821 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15822 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15823 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15824 }
15825
edc3d884
MK
15826 err_printf(m, "Cursor [%d]:\n", i);
15827 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15828 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15829 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15830 }
63b66e5b
CW
15831
15832 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15833 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15834 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15835 err_printf(m, " Power: %s\n",
15836 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15837 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15838 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15839 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15840 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15841 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15842 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15843 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15844 }
c4a1d9e4 15845}
e2fcdaa9
VS
15846
15847void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15848{
15849 struct intel_crtc *crtc;
15850
15851 for_each_intel_crtc(dev, crtc) {
15852 struct intel_unpin_work *work;
e2fcdaa9 15853
5e2d7afc 15854 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15855
15856 work = crtc->unpin_work;
15857
15858 if (work && work->event &&
15859 work->event->base.file_priv == file) {
15860 kfree(work->event);
15861 work->event = NULL;
15862 }
15863
5e2d7afc 15864 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15865 }
15866}